Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- SemaDecl.cpp - Semantic Analysis for Declarations ----------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements semantic analysis for declarations. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "Sema.h" |
Chris Lattner | e1e7985 | 2008-02-06 00:51:33 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTConsumer.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTContext.h" |
Anders Carlsson | f78915f | 2008-02-15 07:04:12 +0000 | [diff] [blame] | 17 | #include "clang/AST/Attr.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 18 | #include "clang/AST/Builtins.h" |
| 19 | #include "clang/AST/Decl.h" |
| 20 | #include "clang/AST/Expr.h" |
| 21 | #include "clang/AST/Type.h" |
| 22 | #include "clang/Parse/DeclSpec.h" |
| 23 | #include "clang/Parse/Scope.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 24 | #include "clang/Basic/LangOptions.h" |
| 25 | #include "clang/Basic/TargetInfo.h" |
Steve Naroff | 4c49a6c | 2008-01-30 23:46:05 +0000 | [diff] [blame] | 26 | #include "clang/Basic/SourceManager.h" |
| 27 | // FIXME: layering (ideally, Sema shouldn't be dependent on Lex API's) |
Chris Lattner | e1e7985 | 2008-02-06 00:51:33 +0000 | [diff] [blame] | 28 | #include "clang/Lex/Preprocessor.h" |
Steve Naroff | 4c49a6c | 2008-01-30 23:46:05 +0000 | [diff] [blame] | 29 | #include "clang/Lex/HeaderSearch.h" |
Steve Naroff | 563477d | 2007-09-18 23:55:05 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/SmallString.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/SmallSet.h" |
Fariborz Jahanian | 85ff264 | 2007-10-05 18:00:57 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/DenseSet.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 33 | using namespace clang; |
| 34 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 35 | Sema::DeclTy *Sema::isTypeName(const IdentifierInfo &II, Scope *S) const { |
Fariborz Jahanian | bece4ac | 2007-10-12 16:34:10 +0000 | [diff] [blame] | 36 | Decl *IIDecl = II.getFETokenInfo<Decl>(); |
| 37 | // Find first occurance of none-tagged declaration |
| 38 | while(IIDecl && IIDecl->getIdentifierNamespace() != Decl::IDNS_Ordinary) |
| 39 | IIDecl = cast<ScopedDecl>(IIDecl)->getNext(); |
| 40 | if (!IIDecl) |
| 41 | return 0; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 42 | if (isa<TypedefDecl>(IIDecl) || isa<ObjCInterfaceDecl>(IIDecl)) |
Fariborz Jahanian | bece4ac | 2007-10-12 16:34:10 +0000 | [diff] [blame] | 43 | return IIDecl; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 44 | if (ObjCCompatibleAliasDecl *ADecl = |
| 45 | dyn_cast<ObjCCompatibleAliasDecl>(IIDecl)) |
Fariborz Jahanian | bece4ac | 2007-10-12 16:34:10 +0000 | [diff] [blame] | 46 | return ADecl->getClassInterface(); |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 47 | return 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Steve Naroff | b216c88 | 2007-10-09 22:01:59 +0000 | [diff] [blame] | 50 | void Sema::ActOnPopScope(SourceLocation Loc, Scope *S) { |
Chris Lattner | 31e0572 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 51 | if (S->decl_empty()) return; |
| 52 | assert((S->getFlags() & Scope::DeclScope) &&"Scope shouldn't contain decls!"); |
| 53 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 54 | for (Scope::decl_iterator I = S->decl_begin(), E = S->decl_end(); |
| 55 | I != E; ++I) { |
Steve Naroff | c752d04 | 2007-09-13 18:10:37 +0000 | [diff] [blame] | 56 | Decl *TmpD = static_cast<Decl*>(*I); |
| 57 | assert(TmpD && "This decl didn't get pushed??"); |
| 58 | ScopedDecl *D = dyn_cast<ScopedDecl>(TmpD); |
| 59 | assert(D && "This decl isn't a ScopedDecl?"); |
| 60 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 61 | IdentifierInfo *II = D->getIdentifier(); |
| 62 | if (!II) continue; |
| 63 | |
| 64 | // Unlink this decl from the identifier. Because the scope contains decls |
| 65 | // in an unordered collection, and because we have multiple identifier |
| 66 | // namespaces (e.g. tag, normal, label),the decl may not be the first entry. |
| 67 | if (II->getFETokenInfo<Decl>() == D) { |
| 68 | // Normal case, no multiple decls in different namespaces. |
| 69 | II->setFETokenInfo(D->getNext()); |
| 70 | } else { |
| 71 | // Scan ahead. There are only three namespaces in C, so this loop can |
| 72 | // never execute more than 3 times. |
Steve Naroff | c752d04 | 2007-09-13 18:10:37 +0000 | [diff] [blame] | 73 | ScopedDecl *SomeDecl = II->getFETokenInfo<ScopedDecl>(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 74 | while (SomeDecl->getNext() != D) { |
| 75 | SomeDecl = SomeDecl->getNext(); |
| 76 | assert(SomeDecl && "Didn't find this decl on its identifier's chain!"); |
| 77 | } |
| 78 | SomeDecl->setNext(D->getNext()); |
| 79 | } |
| 80 | |
| 81 | // This will have to be revisited for C++: there we want to nest stuff in |
| 82 | // namespace decls etc. Even for C, we might want a top-level translation |
| 83 | // unit decl or something. |
| 84 | if (!CurFunctionDecl) |
| 85 | continue; |
| 86 | |
| 87 | // Chain this decl to the containing function, it now owns the memory for |
| 88 | // the decl. |
| 89 | D->setNext(CurFunctionDecl->getDeclChain()); |
| 90 | CurFunctionDecl->setDeclChain(D); |
| 91 | } |
| 92 | } |
| 93 | |
Fariborz Jahanian | 4cabdfc | 2007-10-12 19:38:20 +0000 | [diff] [blame] | 94 | /// LookupInterfaceDecl - Lookup interface declaration in the scope chain. |
| 95 | /// Return the first declaration found (which may or may not be a class |
Fariborz Jahanian | 3fe44e4 | 2007-10-12 19:53:08 +0000 | [diff] [blame] | 96 | /// declaration. Caller is responsible for handling the none-class case. |
Fariborz Jahanian | 4cabdfc | 2007-10-12 19:38:20 +0000 | [diff] [blame] | 97 | /// Bypassing the alias of a class by returning the aliased class. |
| 98 | ScopedDecl *Sema::LookupInterfaceDecl(IdentifierInfo *ClassName) { |
| 99 | ScopedDecl *IDecl; |
| 100 | // Scan up the scope chain looking for a decl that matches this identifier |
| 101 | // that is in the appropriate namespace. |
| 102 | for (IDecl = ClassName->getFETokenInfo<ScopedDecl>(); IDecl; |
| 103 | IDecl = IDecl->getNext()) |
| 104 | if (IDecl->getIdentifierNamespace() == Decl::IDNS_Ordinary) |
| 105 | break; |
| 106 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 107 | if (ObjCCompatibleAliasDecl *ADecl = |
| 108 | dyn_cast_or_null<ObjCCompatibleAliasDecl>(IDecl)) |
Fariborz Jahanian | 4cabdfc | 2007-10-12 19:38:20 +0000 | [diff] [blame] | 109 | return ADecl->getClassInterface(); |
| 110 | return IDecl; |
| 111 | } |
| 112 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 113 | /// getObjCInterfaceDecl - Look up a for a class declaration in the scope. |
Fariborz Jahanian | 9d048ff | 2007-09-29 00:54:24 +0000 | [diff] [blame] | 114 | /// return 0 if one not found. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 115 | ObjCInterfaceDecl *Sema::getObjCInterfaceDecl(IdentifierInfo *Id) { |
Fariborz Jahanian | 4cabdfc | 2007-10-12 19:38:20 +0000 | [diff] [blame] | 116 | ScopedDecl *IdDecl = LookupInterfaceDecl(Id); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 117 | return cast_or_null<ObjCInterfaceDecl>(IdDecl); |
Fariborz Jahanian | 9d048ff | 2007-09-29 00:54:24 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 120 | /// LookupScopedDecl - Look up the inner-most declaration in the specified |
| 121 | /// namespace. |
Steve Naroff | c752d04 | 2007-09-13 18:10:37 +0000 | [diff] [blame] | 122 | ScopedDecl *Sema::LookupScopedDecl(IdentifierInfo *II, unsigned NSI, |
| 123 | SourceLocation IdLoc, Scope *S) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 124 | if (II == 0) return 0; |
| 125 | Decl::IdentifierNamespace NS = (Decl::IdentifierNamespace)NSI; |
| 126 | |
| 127 | // Scan up the scope chain looking for a decl that matches this identifier |
| 128 | // that is in the appropriate namespace. This search should not take long, as |
| 129 | // shadowing of names is uncommon, and deep shadowing is extremely uncommon. |
Steve Naroff | c752d04 | 2007-09-13 18:10:37 +0000 | [diff] [blame] | 130 | for (ScopedDecl *D = II->getFETokenInfo<ScopedDecl>(); D; D = D->getNext()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 131 | if (D->getIdentifierNamespace() == NS) |
| 132 | return D; |
| 133 | |
| 134 | // If we didn't find a use of this identifier, and if the identifier |
| 135 | // corresponds to a compiler builtin, create the decl object for the builtin |
| 136 | // now, injecting it into translation unit scope, and return it. |
| 137 | if (NS == Decl::IDNS_Ordinary) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 138 | // If this is a builtin on this (or all) targets, create the decl. |
| 139 | if (unsigned BuiltinID = II->getBuiltinID()) |
| 140 | return LazilyCreateBuiltin(II, BuiltinID, S); |
| 141 | } |
| 142 | return 0; |
| 143 | } |
| 144 | |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 145 | void Sema::InitBuiltinVaListType() |
| 146 | { |
| 147 | if (!Context.getBuiltinVaListType().isNull()) |
| 148 | return; |
| 149 | |
| 150 | IdentifierInfo *VaIdent = &Context.Idents.get("__builtin_va_list"); |
| 151 | ScopedDecl *VaDecl = LookupScopedDecl(VaIdent, Decl::IDNS_Ordinary, |
| 152 | SourceLocation(), TUScope); |
Steve Naroff | 733002f | 2007-10-18 22:17:45 +0000 | [diff] [blame] | 153 | TypedefDecl *VaTypedef = cast<TypedefDecl>(VaDecl); |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 154 | Context.setBuiltinVaListType(Context.getTypedefType(VaTypedef)); |
| 155 | } |
| 156 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 157 | /// LazilyCreateBuiltin - The specified Builtin-ID was first used at file scope. |
| 158 | /// lazily create a decl for it. |
Chris Lattner | 22b73ba | 2007-10-10 23:42:28 +0000 | [diff] [blame] | 159 | ScopedDecl *Sema::LazilyCreateBuiltin(IdentifierInfo *II, unsigned bid, |
| 160 | Scope *S) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 161 | Builtin::ID BID = (Builtin::ID)bid; |
| 162 | |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 163 | if (BID == Builtin::BI__builtin_va_start || |
Anders Carlsson | 793680e | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 164 | BID == Builtin::BI__builtin_va_copy || |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 165 | BID == Builtin::BI__builtin_va_end) |
| 166 | InitBuiltinVaListType(); |
| 167 | |
Anders Carlsson | b2cf357 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 168 | QualType R = Context.BuiltinInfo.GetBuiltinType(BID, Context); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 169 | FunctionDecl *New = new FunctionDecl(SourceLocation(), II, R, |
Chris Lattner | 70c8b2e | 2007-08-26 04:02:13 +0000 | [diff] [blame] | 170 | FunctionDecl::Extern, false, 0); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 171 | |
| 172 | // Find translation-unit scope to insert this function into. |
Chris Lattner | 31e0572 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 173 | if (Scope *FnS = S->getFnParent()) |
| 174 | S = FnS->getParent(); // Skip all scopes in a function at once. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 175 | while (S->getParent()) |
| 176 | S = S->getParent(); |
| 177 | S->AddDecl(New); |
| 178 | |
| 179 | // Add this decl to the end of the identifier info. |
Steve Naroff | c752d04 | 2007-09-13 18:10:37 +0000 | [diff] [blame] | 180 | if (ScopedDecl *LastDecl = II->getFETokenInfo<ScopedDecl>()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 181 | // Scan until we find the last (outermost) decl in the id chain. |
| 182 | while (LastDecl->getNext()) |
| 183 | LastDecl = LastDecl->getNext(); |
| 184 | // Insert before (outside) it. |
| 185 | LastDecl->setNext(New); |
| 186 | } else { |
| 187 | II->setFETokenInfo(New); |
| 188 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 189 | return New; |
| 190 | } |
| 191 | |
| 192 | /// MergeTypeDefDecl - We just parsed a typedef 'New' which has the same name |
| 193 | /// and scope as a previous declaration 'Old'. Figure out how to resolve this |
| 194 | /// situation, merging decls or emitting diagnostics as appropriate. |
| 195 | /// |
Steve Naroff | 8e74c93 | 2007-09-13 21:41:19 +0000 | [diff] [blame] | 196 | TypedefDecl *Sema::MergeTypeDefDecl(TypedefDecl *New, ScopedDecl *OldD) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 197 | // Verify the old decl was also a typedef. |
| 198 | TypedefDecl *Old = dyn_cast<TypedefDecl>(OldD); |
| 199 | if (!Old) { |
| 200 | Diag(New->getLocation(), diag::err_redefinition_different_kind, |
| 201 | New->getName()); |
| 202 | Diag(OldD->getLocation(), diag::err_previous_definition); |
| 203 | return New; |
| 204 | } |
| 205 | |
Steve Naroff | 8ee529b | 2007-10-31 18:42:27 +0000 | [diff] [blame] | 206 | // Allow multiple definitions for ObjC built-in typedefs. |
| 207 | // FIXME: Verify the underlying types are equivalent! |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 208 | if (getLangOptions().ObjC1 && isBuiltinObjCType(New)) |
Steve Naroff | 8ee529b | 2007-10-31 18:42:27 +0000 | [diff] [blame] | 209 | return Old; |
Steve Naroff | 4c49a6c | 2008-01-30 23:46:05 +0000 | [diff] [blame] | 210 | |
| 211 | // Redeclaration of a type is a constraint violation (6.7.2.3p1). |
| 212 | // Apparently GCC, Intel, and Sun all silently ignore the redeclaration if |
| 213 | // *either* declaration is in a system header. The code below implements |
| 214 | // this adhoc compatibility rule. FIXME: The following code will not |
| 215 | // work properly when compiling ".i" files (containing preprocessed output). |
| 216 | SourceManager &SrcMgr = Context.getSourceManager(); |
| 217 | const FileEntry *OldDeclFile = SrcMgr.getFileEntryForLoc(Old->getLocation()); |
| 218 | const FileEntry *NewDeclFile = SrcMgr.getFileEntryForLoc(New->getLocation()); |
| 219 | HeaderSearch &HdrInfo = PP.getHeaderSearchInfo(); |
| 220 | DirectoryLookup::DirType OldDirType = HdrInfo.getFileDirFlavor(OldDeclFile); |
| 221 | DirectoryLookup::DirType NewDirType = HdrInfo.getFileDirFlavor(NewDeclFile); |
| 222 | |
Steve Naroff | d62701b | 2008-02-07 03:50:06 +0000 | [diff] [blame] | 223 | if ((OldDirType == DirectoryLookup::ExternCSystemHeaderDir || |
| 224 | NewDirType == DirectoryLookup::ExternCSystemHeaderDir) || |
| 225 | getLangOptions().Microsoft) |
Steve Naroff | 4c49a6c | 2008-01-30 23:46:05 +0000 | [diff] [blame] | 226 | return New; |
Steve Naroff | 8ee529b | 2007-10-31 18:42:27 +0000 | [diff] [blame] | 227 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 228 | // TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope. |
| 229 | // TODO: This is totally simplistic. It should handle merging functions |
| 230 | // together etc, merging extern int X; int X; ... |
| 231 | Diag(New->getLocation(), diag::err_redefinition, New->getName()); |
| 232 | Diag(Old->getLocation(), diag::err_previous_definition); |
| 233 | return New; |
| 234 | } |
| 235 | |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 236 | /// DeclhasAttr - returns true if decl Declaration already has the target attribute. |
| 237 | static bool DeclHasAttr(const Decl *decl, const Attr *target) { |
| 238 | for (const Attr *attr = decl->getAttrs(); attr; attr = attr->getNext()) |
| 239 | if (attr->getKind() == target->getKind()) |
| 240 | return true; |
| 241 | |
| 242 | return false; |
| 243 | } |
| 244 | |
| 245 | /// MergeAttributes - append attributes from the Old decl to the New one. |
| 246 | static void MergeAttributes(Decl *New, Decl *Old) { |
| 247 | Attr *attr = const_cast<Attr*>(Old->getAttrs()), *tmp; |
| 248 | |
| 249 | // FIXME: fix this code to cleanup the Old attrs correctly |
| 250 | while (attr) { |
| 251 | tmp = attr; |
| 252 | attr = attr->getNext(); |
| 253 | |
| 254 | if (!DeclHasAttr(New, tmp)) { |
| 255 | New->addAttr(tmp); |
| 256 | } else { |
| 257 | tmp->setNext(0); |
| 258 | delete(tmp); |
| 259 | } |
| 260 | } |
| 261 | } |
| 262 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 263 | /// MergeFunctionDecl - We just parsed a function 'New' which has the same name |
| 264 | /// and scope as a previous declaration 'Old'. Figure out how to resolve this |
| 265 | /// situation, merging decls or emitting diagnostics as appropriate. |
| 266 | /// |
Steve Naroff | 8e74c93 | 2007-09-13 21:41:19 +0000 | [diff] [blame] | 267 | FunctionDecl *Sema::MergeFunctionDecl(FunctionDecl *New, ScopedDecl *OldD) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 268 | // Verify the old decl was also a function. |
| 269 | FunctionDecl *Old = dyn_cast<FunctionDecl>(OldD); |
| 270 | if (!Old) { |
| 271 | Diag(New->getLocation(), diag::err_redefinition_different_kind, |
| 272 | New->getName()); |
| 273 | Diag(OldD->getLocation(), diag::err_previous_definition); |
| 274 | return New; |
| 275 | } |
Chris Lattner | 7e669b2 | 2008-02-29 16:48:43 +0000 | [diff] [blame] | 276 | |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 277 | MergeAttributes(New, Old); |
| 278 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 279 | |
Chris Lattner | 5519644 | 2007-11-20 19:04:50 +0000 | [diff] [blame] | 280 | QualType OldQType = Old->getCanonicalType(); |
| 281 | QualType NewQType = New->getCanonicalType(); |
| 282 | |
Steve Naroff | adbbd0c | 2008-01-14 20:51:29 +0000 | [diff] [blame] | 283 | // Function types need to be compatible, not identical. This handles |
| 284 | // duplicate function decls like "void f(int); void f(enum X);" properly. |
| 285 | if (Context.functionTypesAreCompatible(OldQType, NewQType)) |
| 286 | return New; |
Chris Lattner | e3995fe | 2007-11-06 06:07:26 +0000 | [diff] [blame] | 287 | |
Steve Naroff | 837618c | 2008-01-16 15:01:34 +0000 | [diff] [blame] | 288 | // A function that has already been declared has been redeclared or defined |
| 289 | // with a different type- show appropriate diagnostic |
| 290 | diag::kind PrevDiag = Old->getBody() ? diag::err_previous_definition : |
| 291 | diag::err_previous_declaration; |
| 292 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 293 | // TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope. |
| 294 | // TODO: This is totally simplistic. It should handle merging functions |
| 295 | // together etc, merging extern int X; int X; ... |
Steve Naroff | 837618c | 2008-01-16 15:01:34 +0000 | [diff] [blame] | 296 | Diag(New->getLocation(), diag::err_conflicting_types, New->getName()); |
| 297 | Diag(Old->getLocation(), PrevDiag); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 298 | return New; |
| 299 | } |
| 300 | |
Chris Lattner | fcc2d26 | 2007-11-06 04:28:31 +0000 | [diff] [blame] | 301 | /// equivalentArrayTypes - Used to determine whether two array types are |
| 302 | /// equivalent. |
| 303 | /// We need to check this explicitly as an incomplete array definition is |
| 304 | /// considered a VariableArrayType, so will not match a complete array |
| 305 | /// definition that would be otherwise equivalent. |
| 306 | static bool areEquivalentArrayTypes(QualType NewQType, QualType OldQType) { |
| 307 | const ArrayType *NewAT = NewQType->getAsArrayType(); |
| 308 | const ArrayType *OldAT = OldQType->getAsArrayType(); |
| 309 | |
| 310 | if (!NewAT || !OldAT) |
| 311 | return false; |
| 312 | |
| 313 | // If either (or both) array types in incomplete we need to strip off the |
| 314 | // outer VariableArrayType. Once the outer VAT is removed the remaining |
| 315 | // types must be identical if the array types are to be considered |
| 316 | // equivalent. |
| 317 | // eg. int[][1] and int[1][1] become |
| 318 | // VAT(null, CAT(1, int)) and CAT(1, CAT(1, int)) |
| 319 | // removing the outermost VAT gives |
| 320 | // CAT(1, int) and CAT(1, int) |
| 321 | // which are equal, therefore the array types are equivalent. |
Eli Friedman | 9db1397 | 2008-02-15 12:53:51 +0000 | [diff] [blame] | 322 | if (NewAT->isIncompleteArrayType() || OldAT->isIncompleteArrayType()) { |
Chris Lattner | fcc2d26 | 2007-11-06 04:28:31 +0000 | [diff] [blame] | 323 | if (NewAT->getIndexTypeQualifier() != OldAT->getIndexTypeQualifier()) |
| 324 | return false; |
Eli Friedman | 0493025 | 2008-01-29 07:51:12 +0000 | [diff] [blame] | 325 | NewQType = NewAT->getElementType().getCanonicalType(); |
| 326 | OldQType = OldAT->getElementType().getCanonicalType(); |
Chris Lattner | fcc2d26 | 2007-11-06 04:28:31 +0000 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | return NewQType == OldQType; |
| 330 | } |
| 331 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 332 | /// MergeVarDecl - We just parsed a variable 'New' which has the same name |
| 333 | /// and scope as a previous declaration 'Old'. Figure out how to resolve this |
| 334 | /// situation, merging decls or emitting diagnostics as appropriate. |
| 335 | /// |
| 336 | /// FIXME: Need to carefully consider tentative definition rules (C99 6.9.2p2). |
| 337 | /// For example, we incorrectly complain about i1, i4 from C99 6.9.2p4. |
| 338 | /// |
Steve Naroff | 8e74c93 | 2007-09-13 21:41:19 +0000 | [diff] [blame] | 339 | VarDecl *Sema::MergeVarDecl(VarDecl *New, ScopedDecl *OldD) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 340 | // Verify the old decl was also a variable. |
| 341 | VarDecl *Old = dyn_cast<VarDecl>(OldD); |
| 342 | if (!Old) { |
| 343 | Diag(New->getLocation(), diag::err_redefinition_different_kind, |
| 344 | New->getName()); |
| 345 | Diag(OldD->getLocation(), diag::err_previous_definition); |
| 346 | return New; |
| 347 | } |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 348 | |
| 349 | MergeAttributes(New, Old); |
| 350 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 351 | // Verify the types match. |
Chris Lattner | fcc2d26 | 2007-11-06 04:28:31 +0000 | [diff] [blame] | 352 | if (Old->getCanonicalType() != New->getCanonicalType() && |
| 353 | !areEquivalentArrayTypes(New->getCanonicalType(), Old->getCanonicalType())) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 354 | Diag(New->getLocation(), diag::err_redefinition, New->getName()); |
| 355 | Diag(Old->getLocation(), diag::err_previous_definition); |
| 356 | return New; |
| 357 | } |
Steve Naroff | b7b032e | 2008-01-30 00:44:01 +0000 | [diff] [blame] | 358 | // C99 6.2.2p4: Check if we have a static decl followed by a non-static. |
| 359 | if (New->getStorageClass() == VarDecl::Static && |
| 360 | (Old->getStorageClass() == VarDecl::None || |
| 361 | Old->getStorageClass() == VarDecl::Extern)) { |
| 362 | Diag(New->getLocation(), diag::err_static_non_static, New->getName()); |
| 363 | Diag(Old->getLocation(), diag::err_previous_definition); |
| 364 | return New; |
| 365 | } |
| 366 | // C99 6.2.2p4: Check if we have a non-static decl followed by a static. |
| 367 | if (New->getStorageClass() != VarDecl::Static && |
| 368 | Old->getStorageClass() == VarDecl::Static) { |
| 369 | Diag(New->getLocation(), diag::err_non_static_static, New->getName()); |
| 370 | Diag(Old->getLocation(), diag::err_previous_definition); |
| 371 | return New; |
| 372 | } |
| 373 | // We've verified the types match, now handle "tentative" definitions. |
| 374 | FileVarDecl *OldFSDecl = dyn_cast<FileVarDecl>(Old); |
| 375 | FileVarDecl *NewFSDecl = dyn_cast<FileVarDecl>(New); |
| 376 | |
| 377 | if (OldFSDecl && NewFSDecl) { |
| 378 | // Handle C "tentative" external object definitions (C99 6.9.2). |
| 379 | bool OldIsTentative = false; |
| 380 | bool NewIsTentative = false; |
| 381 | |
| 382 | if (!OldFSDecl->getInit() && |
| 383 | (OldFSDecl->getStorageClass() == VarDecl::None || |
| 384 | OldFSDecl->getStorageClass() == VarDecl::Static)) |
| 385 | OldIsTentative = true; |
| 386 | |
| 387 | // FIXME: this check doesn't work (since the initializer hasn't been |
| 388 | // attached yet). This check should be moved to FinalizeDeclaratorGroup. |
| 389 | // Unfortunately, by the time we get to FinializeDeclaratorGroup, we've |
| 390 | // thrown out the old decl. |
| 391 | if (!NewFSDecl->getInit() && |
| 392 | (NewFSDecl->getStorageClass() == VarDecl::None || |
| 393 | NewFSDecl->getStorageClass() == VarDecl::Static)) |
| 394 | ; // change to NewIsTentative = true; once the code is moved. |
| 395 | |
| 396 | if (NewIsTentative || OldIsTentative) |
| 397 | return New; |
| 398 | } |
| 399 | if (Old->getStorageClass() != VarDecl::Extern && |
| 400 | New->getStorageClass() != VarDecl::Extern) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 401 | Diag(New->getLocation(), diag::err_redefinition, New->getName()); |
| 402 | Diag(Old->getLocation(), diag::err_previous_definition); |
| 403 | } |
| 404 | return New; |
| 405 | } |
| 406 | |
| 407 | /// ParsedFreeStandingDeclSpec - This method is invoked when a declspec with |
| 408 | /// no declarator (e.g. "struct foo;") is parsed. |
| 409 | Sema::DeclTy *Sema::ParsedFreeStandingDeclSpec(Scope *S, DeclSpec &DS) { |
| 410 | // TODO: emit error on 'int;' or 'const enum foo;'. |
| 411 | // TODO: emit error on 'typedef int;' |
| 412 | // if (!DS.isMissingDeclaratorOk()) Diag(...); |
| 413 | |
Steve Naroff | 9219928 | 2007-11-17 21:37:36 +0000 | [diff] [blame] | 414 | return dyn_cast_or_null<TagDecl>(static_cast<Decl *>(DS.getTypeRep())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 415 | } |
| 416 | |
Steve Naroff | d0091aa | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 417 | bool Sema::CheckSingleInitializer(Expr *&Init, QualType DeclType) { |
Steve Naroff | f009063 | 2007-09-02 02:04:30 +0000 | [diff] [blame] | 418 | // Get the type before calling CheckSingleAssignmentConstraints(), since |
| 419 | // it can promote the expression. |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 420 | QualType InitType = Init->getType(); |
Steve Naroff | f009063 | 2007-09-02 02:04:30 +0000 | [diff] [blame] | 421 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 422 | AssignConvertType ConvTy = CheckSingleAssignmentConstraints(DeclType, Init); |
| 423 | return DiagnoseAssignmentResult(ConvTy, Init->getLocStart(), DeclType, |
| 424 | InitType, Init, "initializing"); |
Steve Naroff | f009063 | 2007-09-02 02:04:30 +0000 | [diff] [blame] | 425 | } |
| 426 | |
Steve Naroff | 9e8925e | 2007-09-04 14:36:54 +0000 | [diff] [blame] | 427 | bool Sema::CheckInitExpr(Expr *expr, InitListExpr *IList, unsigned slot, |
Steve Naroff | d0091aa | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 428 | QualType ElementType) { |
Chris Lattner | 33b7b06 | 2007-12-11 23:15:04 +0000 | [diff] [blame] | 429 | Expr *savExpr = expr; // Might be promoted by CheckSingleInitializer. |
Steve Naroff | d0091aa | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 430 | if (CheckSingleInitializer(expr, ElementType)) |
Chris Lattner | 33b7b06 | 2007-12-11 23:15:04 +0000 | [diff] [blame] | 431 | return true; // types weren't compatible. |
| 432 | |
Steve Naroff | 9e8925e | 2007-09-04 14:36:54 +0000 | [diff] [blame] | 433 | if (savExpr != expr) // The type was promoted, update initializer list. |
| 434 | IList->setInit(slot, expr); |
Steve Naroff | 371227d | 2007-09-04 02:20:04 +0000 | [diff] [blame] | 435 | return false; |
| 436 | } |
| 437 | |
Steve Naroff | a49e1fa | 2008-01-22 00:55:40 +0000 | [diff] [blame] | 438 | bool Sema::CheckStringLiteralInit(StringLiteral *strLiteral, QualType &DeclT) { |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 439 | if (const IncompleteArrayType *IAT = DeclT->getAsIncompleteArrayType()) { |
Steve Naroff | a49e1fa | 2008-01-22 00:55:40 +0000 | [diff] [blame] | 440 | // C99 6.7.8p14. We have an array of character type with unknown size |
| 441 | // being initialized to a string literal. |
| 442 | llvm::APSInt ConstVal(32); |
| 443 | ConstVal = strLiteral->getByteLength() + 1; |
| 444 | // Return a new array type (C99 6.7.8p22). |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 445 | DeclT = Context.getConstantArrayType(IAT->getElementType(), ConstVal, |
Steve Naroff | a49e1fa | 2008-01-22 00:55:40 +0000 | [diff] [blame] | 446 | ArrayType::Normal, 0); |
| 447 | } else if (const ConstantArrayType *CAT = DeclT->getAsConstantArrayType()) { |
| 448 | // C99 6.7.8p14. We have an array of character type with known size. |
| 449 | if (strLiteral->getByteLength() > (unsigned)CAT->getMaximumElements()) |
| 450 | Diag(strLiteral->getSourceRange().getBegin(), |
| 451 | diag::warn_initializer_string_for_char_array_too_long, |
| 452 | strLiteral->getSourceRange()); |
| 453 | } else { |
| 454 | assert(0 && "HandleStringLiteralInit(): Invalid array type"); |
| 455 | } |
| 456 | // Set type from "char *" to "constant array of char". |
| 457 | strLiteral->setType(DeclT); |
| 458 | // For now, we always return false (meaning success). |
| 459 | return false; |
| 460 | } |
| 461 | |
| 462 | StringLiteral *Sema::IsStringLiteralInit(Expr *Init, QualType DeclType) { |
Steve Naroff | a49e1fa | 2008-01-22 00:55:40 +0000 | [diff] [blame] | 463 | const ArrayType *AT = DeclType->getAsArrayType(); |
Steve Naroff | a996033 | 2008-01-25 00:51:06 +0000 | [diff] [blame] | 464 | if (AT && AT->getElementType()->isCharType()) { |
| 465 | return dyn_cast<StringLiteral>(Init); |
| 466 | } |
Steve Naroff | a49e1fa | 2008-01-22 00:55:40 +0000 | [diff] [blame] | 467 | return 0; |
| 468 | } |
| 469 | |
Steve Naroff | a996033 | 2008-01-25 00:51:06 +0000 | [diff] [blame] | 470 | // CheckInitializerListTypes - Checks the types of elements of an initializer |
| 471 | // list. This function is recursive: it calls itself to initialize subelements |
| 472 | // of aggregate types. Note that the topLevel parameter essentially refers to |
| 473 | // whether this expression "owns" the initializer list passed in, or if this |
| 474 | // initialization is taking elements out of a parent initializer. Each |
| 475 | // call to this function adds zero or more to startIndex, reports any errors, |
| 476 | // and returns true if it found any inconsistent types. |
| 477 | bool Sema::CheckInitializerListTypes(InitListExpr*& IList, QualType &DeclType, |
| 478 | bool topLevel, unsigned& startIndex) { |
Steve Naroff | 2fdc374 | 2007-12-10 22:44:33 +0000 | [diff] [blame] | 479 | bool hadError = false; |
Steve Naroff | a996033 | 2008-01-25 00:51:06 +0000 | [diff] [blame] | 480 | |
| 481 | if (DeclType->isScalarType()) { |
| 482 | // The simplest case: initializing a single scalar |
| 483 | if (topLevel) { |
| 484 | Diag(IList->getLocStart(), diag::warn_braces_around_scalar_init, |
| 485 | IList->getSourceRange()); |
| 486 | } |
| 487 | if (startIndex < IList->getNumInits()) { |
| 488 | Expr* expr = IList->getInit(startIndex); |
| 489 | if (InitListExpr *SubInitList = dyn_cast<InitListExpr>(expr)) { |
| 490 | // FIXME: Should an error be reported here instead? |
| 491 | unsigned newIndex = 0; |
| 492 | CheckInitializerListTypes(SubInitList, DeclType, true, newIndex); |
| 493 | } else { |
| 494 | hadError |= CheckInitExpr(expr, IList, startIndex, DeclType); |
| 495 | } |
| 496 | ++startIndex; |
| 497 | } |
| 498 | // FIXME: Should an error be reported for empty initializer list + scalar? |
| 499 | } else if (DeclType->isVectorType()) { |
| 500 | if (startIndex < IList->getNumInits()) { |
| 501 | const VectorType *VT = DeclType->getAsVectorType(); |
| 502 | int maxElements = VT->getNumElements(); |
| 503 | QualType elementType = VT->getElementType(); |
| 504 | |
| 505 | for (int i = 0; i < maxElements; ++i) { |
| 506 | // Don't attempt to go past the end of the init list |
| 507 | if (startIndex >= IList->getNumInits()) |
| 508 | break; |
| 509 | Expr* expr = IList->getInit(startIndex); |
| 510 | if (InitListExpr *SubInitList = dyn_cast<InitListExpr>(expr)) { |
| 511 | unsigned newIndex = 0; |
| 512 | hadError |= CheckInitializerListTypes(SubInitList, elementType, |
| 513 | true, newIndex); |
| 514 | ++startIndex; |
| 515 | } else { |
| 516 | hadError |= CheckInitializerListTypes(IList, elementType, |
| 517 | false, startIndex); |
| 518 | } |
| 519 | } |
| 520 | } |
| 521 | } else if (DeclType->isAggregateType() || DeclType->isUnionType()) { |
| 522 | if (DeclType->isStructureType() || DeclType->isUnionType()) { |
Steve Naroff | 578edc6 | 2008-01-28 02:00:41 +0000 | [diff] [blame] | 523 | if (startIndex < IList->getNumInits() && !topLevel && |
| 524 | Context.typesAreCompatible(IList->getInit(startIndex)->getType(), |
| 525 | DeclType)) { |
Steve Naroff | a996033 | 2008-01-25 00:51:06 +0000 | [diff] [blame] | 526 | // We found a compatible struct; per the standard, this initializes the |
| 527 | // struct. (The C standard technically says that this only applies for |
| 528 | // initializers for declarations with automatic scope; however, this |
| 529 | // construct is unambiguous anyway because a struct cannot contain |
| 530 | // a type compatible with itself. We'll output an error when we check |
| 531 | // if the initializer is constant.) |
| 532 | // FIXME: Is a call to CheckSingleInitializer required here? |
| 533 | ++startIndex; |
| 534 | } else { |
| 535 | RecordDecl* structDecl = DeclType->getAsRecordType()->getDecl(); |
Steve Naroff | b43eaa5 | 2008-02-11 00:06:17 +0000 | [diff] [blame] | 536 | |
Steve Naroff | 406db93 | 2008-02-11 21:52:37 +0000 | [diff] [blame] | 537 | // If the record is invalid, some of it's members are invalid. To avoid |
| 538 | // confusion, we forgo checking the intializer for the entire record. |
Steve Naroff | b43eaa5 | 2008-02-11 00:06:17 +0000 | [diff] [blame] | 539 | if (structDecl->isInvalidDecl()) |
| 540 | return true; |
| 541 | |
Steve Naroff | a996033 | 2008-01-25 00:51:06 +0000 | [diff] [blame] | 542 | // If structDecl is a forward declaration, this loop won't do anything; |
| 543 | // That's okay, because an error should get printed out elsewhere. It |
| 544 | // might be worthwhile to skip over the rest of the initializer, though. |
| 545 | int numMembers = structDecl->getNumMembers() - |
| 546 | structDecl->hasFlexibleArrayMember(); |
| 547 | for (int i = 0; i < numMembers; i++) { |
| 548 | // Don't attempt to go past the end of the init list |
| 549 | if (startIndex >= IList->getNumInits()) |
| 550 | break; |
| 551 | FieldDecl * curField = structDecl->getMember(i); |
| 552 | if (!curField->getIdentifier()) { |
| 553 | // Don't initialize unnamed fields, e.g. "int : 20;" |
| 554 | continue; |
| 555 | } |
| 556 | QualType fieldType = curField->getType(); |
| 557 | Expr* expr = IList->getInit(startIndex); |
| 558 | if (InitListExpr *SubInitList = dyn_cast<InitListExpr>(expr)) { |
| 559 | unsigned newStart = 0; |
| 560 | hadError |= CheckInitializerListTypes(SubInitList, fieldType, |
| 561 | true, newStart); |
| 562 | ++startIndex; |
| 563 | } else { |
| 564 | hadError |= CheckInitializerListTypes(IList, fieldType, |
| 565 | false, startIndex); |
| 566 | } |
| 567 | if (DeclType->isUnionType()) |
| 568 | break; |
| 569 | } |
| 570 | // FIXME: Implement flexible array initialization GCC extension (it's a |
| 571 | // really messy extension to implement, unfortunately...the necessary |
| 572 | // information isn't actually even here!) |
| 573 | } |
| 574 | } else if (DeclType->isArrayType()) { |
| 575 | // Check for the special-case of initializing an array with a string. |
| 576 | if (startIndex < IList->getNumInits()) { |
| 577 | if (StringLiteral *lit = IsStringLiteralInit(IList->getInit(startIndex), |
| 578 | DeclType)) { |
| 579 | CheckStringLiteralInit(lit, DeclType); |
| 580 | ++startIndex; |
| 581 | if (topLevel && startIndex < IList->getNumInits()) { |
| 582 | // We have leftover initializers; warn |
| 583 | Diag(IList->getInit(startIndex)->getLocStart(), |
| 584 | diag::err_excess_initializers_in_char_array_initializer, |
| 585 | IList->getInit(startIndex)->getSourceRange()); |
| 586 | } |
| 587 | return false; |
| 588 | } |
| 589 | } |
| 590 | int maxElements; |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 591 | if (DeclType->isIncompleteArrayType()) { |
Steve Naroff | a996033 | 2008-01-25 00:51:06 +0000 | [diff] [blame] | 592 | // FIXME: use a proper constant |
| 593 | maxElements = 0x7FFFFFFF; |
Chris Lattner | 212839c | 2008-02-20 23:17:35 +0000 | [diff] [blame] | 594 | } else if (const VariableArrayType *VAT = |
| 595 | DeclType->getAsVariableArrayType()) { |
Steve Naroff | a996033 | 2008-01-25 00:51:06 +0000 | [diff] [blame] | 596 | // Check for VLAs; in standard C it would be possible to check this |
| 597 | // earlier, but I don't know where clang accepts VLAs (gcc accepts |
| 598 | // them in all sorts of strange places). |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 599 | Diag(VAT->getSizeExpr()->getLocStart(), |
| 600 | diag::err_variable_object_no_init, |
| 601 | VAT->getSizeExpr()->getSourceRange()); |
| 602 | hadError = true; |
| 603 | maxElements = 0x7FFFFFFF; |
Steve Naroff | a996033 | 2008-01-25 00:51:06 +0000 | [diff] [blame] | 604 | } else { |
| 605 | const ConstantArrayType *CAT = DeclType->getAsConstantArrayType(); |
| 606 | maxElements = static_cast<int>(CAT->getSize().getZExtValue()); |
| 607 | } |
| 608 | QualType elementType = DeclType->getAsArrayType()->getElementType(); |
| 609 | int numElements = 0; |
| 610 | for (int i = 0; i < maxElements; ++i, ++numElements) { |
| 611 | // Don't attempt to go past the end of the init list |
| 612 | if (startIndex >= IList->getNumInits()) |
| 613 | break; |
| 614 | Expr* expr = IList->getInit(startIndex); |
| 615 | if (InitListExpr *SubInitList = dyn_cast<InitListExpr>(expr)) { |
| 616 | unsigned newIndex = 0; |
| 617 | hadError |= CheckInitializerListTypes(SubInitList, elementType, |
| 618 | true, newIndex); |
| 619 | ++startIndex; |
| 620 | } else { |
| 621 | hadError |= CheckInitializerListTypes(IList, elementType, |
| 622 | false, startIndex); |
| 623 | } |
| 624 | } |
Eli Friedman | 9db1397 | 2008-02-15 12:53:51 +0000 | [diff] [blame] | 625 | if (DeclType->isIncompleteArrayType()) { |
Steve Naroff | a996033 | 2008-01-25 00:51:06 +0000 | [diff] [blame] | 626 | // If this is an incomplete array type, the actual type needs to |
| 627 | // be calculated here |
| 628 | if (numElements == 0) { |
| 629 | // Sizing an array implicitly to zero is not allowed |
| 630 | // (It could in theory be allowed, but it doesn't really matter.) |
| 631 | Diag(IList->getLocStart(), |
| 632 | diag::err_at_least_one_initializer_needed_to_size_array); |
| 633 | hadError = true; |
| 634 | } else { |
| 635 | llvm::APSInt ConstVal(32); |
| 636 | ConstVal = numElements; |
| 637 | DeclType = Context.getConstantArrayType(elementType, ConstVal, |
| 638 | ArrayType::Normal, 0); |
| 639 | } |
| 640 | } |
| 641 | } else { |
| 642 | assert(0 && "Aggregate that isn't a function or array?!"); |
| 643 | } |
| 644 | } else { |
| 645 | // In C, all types are either scalars or aggregates, but |
| 646 | // additional handling is needed here for C++ (and possibly others?). |
| 647 | assert(0 && "Unsupported initializer type"); |
| 648 | } |
| 649 | |
| 650 | // If this init list is a base list, we set the type; an initializer doesn't |
| 651 | // fundamentally have a type, but this makes the ASTs a bit easier to read |
| 652 | if (topLevel) |
| 653 | IList->setType(DeclType); |
| 654 | |
| 655 | if (topLevel && startIndex < IList->getNumInits()) { |
| 656 | // We have leftover initializers; warn |
| 657 | Diag(IList->getInit(startIndex)->getLocStart(), |
| 658 | diag::warn_excess_initializers, |
| 659 | IList->getInit(startIndex)->getSourceRange()); |
| 660 | } |
| 661 | return hadError; |
| 662 | } |
| 663 | |
| 664 | bool Sema::CheckInitializerTypes(Expr *&Init, QualType &DeclType) { |
Steve Naroff | ca10730 | 2008-01-21 23:53:58 +0000 | [diff] [blame] | 665 | // C99 6.7.8p3: The type of the entity to be initialized shall be an array |
| 666 | // of unknown size ("[]") or an object type that is not a variable array type. |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 667 | if (const VariableArrayType *VAT = DeclType->getAsVariableArrayType()) |
Steve Naroff | ca10730 | 2008-01-21 23:53:58 +0000 | [diff] [blame] | 668 | return Diag(VAT->getSizeExpr()->getLocStart(), |
| 669 | diag::err_variable_object_no_init, |
| 670 | VAT->getSizeExpr()->getSourceRange()); |
| 671 | |
Steve Naroff | 2fdc374 | 2007-12-10 22:44:33 +0000 | [diff] [blame] | 672 | InitListExpr *InitList = dyn_cast<InitListExpr>(Init); |
| 673 | if (!InitList) { |
Steve Naroff | a49e1fa | 2008-01-22 00:55:40 +0000 | [diff] [blame] | 674 | // FIXME: Handle wide strings |
| 675 | if (StringLiteral *strLiteral = IsStringLiteralInit(Init, DeclType)) |
| 676 | return CheckStringLiteralInit(strLiteral, DeclType); |
Eli Friedman | a312ce2 | 2008-02-08 00:48:24 +0000 | [diff] [blame] | 677 | |
| 678 | if (DeclType->isArrayType()) |
| 679 | return Diag(Init->getLocStart(), |
| 680 | diag::err_array_init_list_required, |
| 681 | Init->getSourceRange()); |
| 682 | |
Steve Naroff | d0091aa | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 683 | return CheckSingleInitializer(Init, DeclType); |
Steve Naroff | 2fdc374 | 2007-12-10 22:44:33 +0000 | [diff] [blame] | 684 | } |
Steve Naroff | a996033 | 2008-01-25 00:51:06 +0000 | [diff] [blame] | 685 | unsigned newIndex = 0; |
| 686 | return CheckInitializerListTypes(InitList, DeclType, true, newIndex); |
Steve Naroff | f009063 | 2007-09-02 02:04:30 +0000 | [diff] [blame] | 687 | } |
| 688 | |
Fariborz Jahanian | 306d68f | 2007-11-08 23:49:49 +0000 | [diff] [blame] | 689 | Sema::DeclTy * |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 690 | Sema::ActOnDeclarator(Scope *S, Declarator &D, DeclTy *lastDecl) { |
Steve Naroff | 9474504 | 2007-09-13 23:52:58 +0000 | [diff] [blame] | 691 | ScopedDecl *LastDeclarator = dyn_cast_or_null<ScopedDecl>((Decl *)lastDecl); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 692 | IdentifierInfo *II = D.getIdentifier(); |
| 693 | |
Chris Lattner | e80a59c | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 694 | // All of these full declarators require an identifier. If it doesn't have |
| 695 | // one, the ParsedFreeStandingDeclSpec action should be used. |
| 696 | if (II == 0) { |
Chris Lattner | 311ff02 | 2007-10-16 22:36:42 +0000 | [diff] [blame] | 697 | Diag(D.getDeclSpec().getSourceRange().getBegin(), |
Chris Lattner | 98e0863 | 2007-08-28 06:17:15 +0000 | [diff] [blame] | 698 | diag::err_declarator_need_ident, |
Chris Lattner | e80a59c | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 699 | D.getDeclSpec().getSourceRange(), D.getSourceRange()); |
| 700 | return 0; |
| 701 | } |
| 702 | |
Chris Lattner | 31e0572 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 703 | // The scope passed in may not be a decl scope. Zip up the scope tree until |
| 704 | // we find one that is. |
| 705 | while ((S->getFlags() & Scope::DeclScope) == 0) |
| 706 | S = S->getParent(); |
| 707 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 708 | // See if this is a redefinition of a variable in the same scope. |
Steve Naroff | c752d04 | 2007-09-13 18:10:37 +0000 | [diff] [blame] | 709 | ScopedDecl *PrevDecl = LookupScopedDecl(II, Decl::IDNS_Ordinary, |
| 710 | D.getIdentifierLoc(), S); |
Steve Naroff | c752d04 | 2007-09-13 18:10:37 +0000 | [diff] [blame] | 711 | ScopedDecl *New; |
Steve Naroff | 5912a35 | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 712 | bool InvalidDecl = false; |
| 713 | |
Chris Lattner | 41af093 | 2007-11-14 06:34:38 +0000 | [diff] [blame] | 714 | QualType R = GetTypeForDeclarator(D, S); |
| 715 | assert(!R.isNull() && "GetTypeForDeclarator() returned null type"); |
| 716 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 717 | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) { |
Chris Lattner | 41af093 | 2007-11-14 06:34:38 +0000 | [diff] [blame] | 718 | TypedefDecl *NewTD = ParseTypedefDecl(S, D, R, LastDeclarator); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 719 | if (!NewTD) return 0; |
| 720 | |
| 721 | // Handle attributes prior to checking for duplicates in MergeVarDecl |
| 722 | HandleDeclAttributes(NewTD, D.getDeclSpec().getAttributes(), |
| 723 | D.getAttributes()); |
Steve Naroff | ffce4d5 | 2008-01-09 23:34:55 +0000 | [diff] [blame] | 724 | // Merge the decl with the existing one if appropriate. If the decl is |
| 725 | // in an outer scope, it isn't the same thing. |
| 726 | if (PrevDecl && S->isDeclScope(PrevDecl)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 727 | NewTD = MergeTypeDefDecl(NewTD, PrevDecl); |
| 728 | if (NewTD == 0) return 0; |
| 729 | } |
| 730 | New = NewTD; |
| 731 | if (S->getParent() == 0) { |
| 732 | // C99 6.7.7p2: If a typedef name specifies a variably modified type |
| 733 | // then it shall have block scope. |
Eli Friedman | 9db1397 | 2008-02-15 12:53:51 +0000 | [diff] [blame] | 734 | if (NewTD->getUnderlyingType()->isVariablyModifiedType()) { |
| 735 | // FIXME: Diagnostic needs to be fixed. |
| 736 | Diag(D.getIdentifierLoc(), diag::err_typecheck_illegal_vla); |
Steve Naroff | d7444aa | 2007-08-31 17:20:07 +0000 | [diff] [blame] | 737 | InvalidDecl = true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 738 | } |
| 739 | } |
Chris Lattner | 41af093 | 2007-11-14 06:34:38 +0000 | [diff] [blame] | 740 | } else if (R.getTypePtr()->isFunctionType()) { |
Chris Lattner | 271f1a6 | 2007-09-27 15:15:46 +0000 | [diff] [blame] | 741 | FunctionDecl::StorageClass SC = FunctionDecl::None; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 742 | switch (D.getDeclSpec().getStorageClassSpec()) { |
| 743 | default: assert(0 && "Unknown storage class!"); |
| 744 | case DeclSpec::SCS_auto: |
| 745 | case DeclSpec::SCS_register: |
| 746 | Diag(D.getIdentifierLoc(), diag::err_typecheck_sclass_func, |
| 747 | R.getAsString()); |
Steve Naroff | 5912a35 | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 748 | InvalidDecl = true; |
| 749 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 750 | case DeclSpec::SCS_unspecified: SC = FunctionDecl::None; break; |
| 751 | case DeclSpec::SCS_extern: SC = FunctionDecl::Extern; break; |
| 752 | case DeclSpec::SCS_static: SC = FunctionDecl::Static; break; |
Steve Naroff | 7dd0bd4 | 2008-01-28 21:57:15 +0000 | [diff] [blame] | 753 | case DeclSpec::SCS_private_extern: SC = FunctionDecl::PrivateExtern;break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 754 | } |
| 755 | |
| 756 | FunctionDecl *NewFD = new FunctionDecl(D.getIdentifierLoc(), II, R, SC, |
Chris Lattner | 70c8b2e | 2007-08-26 04:02:13 +0000 | [diff] [blame] | 757 | D.getDeclSpec().isInlineSpecified(), |
Anders Carlsson | f78915f | 2008-02-15 07:04:12 +0000 | [diff] [blame] | 758 | LastDeclarator); |
Ted Kremenek | f5c93c1 | 2008-02-27 22:18:07 +0000 | [diff] [blame] | 759 | // Handle attributes. |
| 760 | |
| 761 | HandleDeclAttributes(NewFD, D.getDeclSpec().getAttributes(), |
| 762 | D.getAttributes()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 763 | |
Steve Naroff | ffce4d5 | 2008-01-09 23:34:55 +0000 | [diff] [blame] | 764 | // Merge the decl with the existing one if appropriate. Since C functions |
| 765 | // are in a flat namespace, make sure we consider decls in outer scopes. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 766 | if (PrevDecl) { |
| 767 | NewFD = MergeFunctionDecl(NewFD, PrevDecl); |
| 768 | if (NewFD == 0) return 0; |
| 769 | } |
| 770 | New = NewFD; |
| 771 | } else { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 772 | if (R.getTypePtr()->isObjCInterfaceType()) { |
Fariborz Jahanian | e7f64cc | 2007-10-12 22:10:42 +0000 | [diff] [blame] | 773 | Diag(D.getIdentifierLoc(), diag::err_statically_allocated_object, |
| 774 | D.getIdentifier()->getName()); |
| 775 | InvalidDecl = true; |
| 776 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 777 | |
| 778 | VarDecl *NewVD; |
| 779 | VarDecl::StorageClass SC; |
| 780 | switch (D.getDeclSpec().getStorageClassSpec()) { |
| 781 | default: assert(0 && "Unknown storage class!"); |
Steve Naroff | d6326c6 | 2008-01-25 22:14:40 +0000 | [diff] [blame] | 782 | case DeclSpec::SCS_unspecified: SC = VarDecl::None; break; |
| 783 | case DeclSpec::SCS_extern: SC = VarDecl::Extern; break; |
| 784 | case DeclSpec::SCS_static: SC = VarDecl::Static; break; |
| 785 | case DeclSpec::SCS_auto: SC = VarDecl::Auto; break; |
| 786 | case DeclSpec::SCS_register: SC = VarDecl::Register; break; |
| 787 | case DeclSpec::SCS_private_extern: SC = VarDecl::PrivateExtern; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 788 | } |
| 789 | if (S->getParent() == 0) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 790 | // C99 6.9p2: The storage-class specifiers auto and register shall not |
| 791 | // appear in the declaration specifiers in an external declaration. |
| 792 | if (SC == VarDecl::Auto || SC == VarDecl::Register) { |
| 793 | Diag(D.getIdentifierLoc(), diag::err_typecheck_sclass_fscope, |
| 794 | R.getAsString()); |
Steve Naroff | 53a3234 | 2007-08-28 18:45:29 +0000 | [diff] [blame] | 795 | InvalidDecl = true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 796 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 797 | NewVD = new FileVarDecl(D.getIdentifierLoc(), II, R, SC, LastDeclarator); |
Steve Naroff | f009063 | 2007-09-02 02:04:30 +0000 | [diff] [blame] | 798 | } else { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 799 | NewVD = new BlockVarDecl(D.getIdentifierLoc(), II, R, SC, LastDeclarator); |
Steve Naroff | 53a3234 | 2007-08-28 18:45:29 +0000 | [diff] [blame] | 800 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 801 | // Handle attributes prior to checking for duplicates in MergeVarDecl |
| 802 | HandleDeclAttributes(NewVD, D.getDeclSpec().getAttributes(), |
| 803 | D.getAttributes()); |
| 804 | |
Steve Naroff | ffce4d5 | 2008-01-09 23:34:55 +0000 | [diff] [blame] | 805 | // Merge the decl with the existing one if appropriate. If the decl is |
| 806 | // in an outer scope, it isn't the same thing. |
| 807 | if (PrevDecl && S->isDeclScope(PrevDecl)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 808 | NewVD = MergeVarDecl(NewVD, PrevDecl); |
| 809 | if (NewVD == 0) return 0; |
| 810 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 811 | New = NewVD; |
| 812 | } |
| 813 | |
| 814 | // If this has an identifier, add it to the scope stack. |
| 815 | if (II) { |
Steve Naroff | c752d04 | 2007-09-13 18:10:37 +0000 | [diff] [blame] | 816 | New->setNext(II->getFETokenInfo<ScopedDecl>()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 817 | II->setFETokenInfo(New); |
| 818 | S->AddDecl(New); |
| 819 | } |
Steve Naroff | 5912a35 | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 820 | // If any semantic error occurred, mark the decl as invalid. |
| 821 | if (D.getInvalidType() || InvalidDecl) |
| 822 | New->setInvalidDecl(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 823 | |
| 824 | return New; |
| 825 | } |
| 826 | |
Steve Naroff | d0091aa | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 827 | bool Sema::CheckForConstantInitializer(Expr *Init, QualType DclT) { |
| 828 | SourceLocation loc; |
| 829 | // FIXME: Remove the isReference check and handle assignment to a reference. |
| 830 | if (!DclT->isReferenceType() && !Init->isConstantExpr(Context, &loc)) { |
| 831 | assert(loc.isValid() && "isConstantExpr didn't return a loc!"); |
| 832 | Diag(loc, diag::err_init_element_not_constant, Init->getSourceRange()); |
| 833 | return true; |
| 834 | } |
| 835 | return false; |
| 836 | } |
| 837 | |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 838 | void Sema::AddInitializerToDecl(DeclTy *dcl, ExprTy *init) { |
Steve Naroff | 410e3e2 | 2007-09-12 20:13:48 +0000 | [diff] [blame] | 839 | Decl *RealDecl = static_cast<Decl *>(dcl); |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 840 | Expr *Init = static_cast<Expr *>(init); |
Chris Lattner | 9a11b9a | 2007-10-19 20:10:30 +0000 | [diff] [blame] | 841 | assert(Init && "missing initializer"); |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 842 | |
Chris Lattner | 9a11b9a | 2007-10-19 20:10:30 +0000 | [diff] [blame] | 843 | // If there is no declaration, there was an error parsing it. Just ignore |
| 844 | // the initializer. |
| 845 | if (RealDecl == 0) { |
| 846 | delete Init; |
| 847 | return; |
| 848 | } |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 849 | |
Steve Naroff | 410e3e2 | 2007-09-12 20:13:48 +0000 | [diff] [blame] | 850 | VarDecl *VDecl = dyn_cast<VarDecl>(RealDecl); |
| 851 | if (!VDecl) { |
Steve Naroff | 8e74c93 | 2007-09-13 21:41:19 +0000 | [diff] [blame] | 852 | Diag(dyn_cast<ScopedDecl>(RealDecl)->getLocation(), |
| 853 | diag::err_illegal_initializer); |
Steve Naroff | 410e3e2 | 2007-09-12 20:13:48 +0000 | [diff] [blame] | 854 | RealDecl->setInvalidDecl(); |
| 855 | return; |
| 856 | } |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 857 | // Get the decls type and save a reference for later, since |
Steve Naroff | d0091aa | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 858 | // CheckInitializerTypes may change it. |
Steve Naroff | 410e3e2 | 2007-09-12 20:13:48 +0000 | [diff] [blame] | 859 | QualType DclT = VDecl->getType(), SavT = DclT; |
| 860 | if (BlockVarDecl *BVD = dyn_cast<BlockVarDecl>(VDecl)) { |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 861 | VarDecl::StorageClass SC = BVD->getStorageClass(); |
| 862 | if (SC == VarDecl::Extern) { // C99 6.7.8p5 |
Steve Naroff | 410e3e2 | 2007-09-12 20:13:48 +0000 | [diff] [blame] | 863 | Diag(VDecl->getLocation(), diag::err_block_extern_cant_init); |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 864 | BVD->setInvalidDecl(); |
| 865 | } else if (!BVD->isInvalidDecl()) { |
Steve Naroff | a996033 | 2008-01-25 00:51:06 +0000 | [diff] [blame] | 866 | if (CheckInitializerTypes(Init, DclT)) |
| 867 | BVD->setInvalidDecl(); |
Steve Naroff | d0091aa | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 868 | if (SC == VarDecl::Static) // C99 6.7.8p4. |
| 869 | CheckForConstantInitializer(Init, DclT); |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 870 | } |
Steve Naroff | 410e3e2 | 2007-09-12 20:13:48 +0000 | [diff] [blame] | 871 | } else if (FileVarDecl *FVD = dyn_cast<FileVarDecl>(VDecl)) { |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 872 | if (FVD->getStorageClass() == VarDecl::Extern) |
Steve Naroff | 410e3e2 | 2007-09-12 20:13:48 +0000 | [diff] [blame] | 873 | Diag(VDecl->getLocation(), diag::warn_extern_init); |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 874 | if (!FVD->isInvalidDecl()) |
Steve Naroff | a996033 | 2008-01-25 00:51:06 +0000 | [diff] [blame] | 875 | if (CheckInitializerTypes(Init, DclT)) |
| 876 | FVD->setInvalidDecl(); |
Steve Naroff | d0091aa | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 877 | |
| 878 | // C99 6.7.8p4. All file scoped initializers need to be constant. |
| 879 | CheckForConstantInitializer(Init, DclT); |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 880 | } |
| 881 | // If the type changed, it means we had an incomplete type that was |
| 882 | // completed by the initializer. For example: |
| 883 | // int ary[] = { 1, 3, 5 }; |
| 884 | // "ary" transitions from a VariableArrayType to a ConstantArrayType. |
Christopher Lamb | 48b1239 | 2007-11-29 19:09:19 +0000 | [diff] [blame] | 885 | if (!VDecl->isInvalidDecl() && (DclT != SavT)) { |
Steve Naroff | 410e3e2 | 2007-09-12 20:13:48 +0000 | [diff] [blame] | 886 | VDecl->setType(DclT); |
Christopher Lamb | 48b1239 | 2007-11-29 19:09:19 +0000 | [diff] [blame] | 887 | Init->setType(DclT); |
| 888 | } |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 889 | |
| 890 | // Attach the initializer to the decl. |
Steve Naroff | 410e3e2 | 2007-09-12 20:13:48 +0000 | [diff] [blame] | 891 | VDecl->setInit(Init); |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 892 | return; |
| 893 | } |
| 894 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 895 | /// The declarators are chained together backwards, reverse the list. |
| 896 | Sema::DeclTy *Sema::FinalizeDeclaratorGroup(Scope *S, DeclTy *group) { |
| 897 | // Often we have single declarators, handle them quickly. |
Steve Naroff | 9474504 | 2007-09-13 23:52:58 +0000 | [diff] [blame] | 898 | Decl *GroupDecl = static_cast<Decl*>(group); |
| 899 | if (GroupDecl == 0) |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 900 | return 0; |
Steve Naroff | 9474504 | 2007-09-13 23:52:58 +0000 | [diff] [blame] | 901 | |
| 902 | ScopedDecl *Group = dyn_cast<ScopedDecl>(GroupDecl); |
| 903 | ScopedDecl *NewGroup = 0; |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 904 | if (Group->getNextDeclarator() == 0) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 905 | NewGroup = Group; |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 906 | else { // reverse the list. |
| 907 | while (Group) { |
Steve Naroff | 9474504 | 2007-09-13 23:52:58 +0000 | [diff] [blame] | 908 | ScopedDecl *Next = Group->getNextDeclarator(); |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 909 | Group->setNextDeclarator(NewGroup); |
| 910 | NewGroup = Group; |
| 911 | Group = Next; |
| 912 | } |
| 913 | } |
| 914 | // Perform semantic analysis that depends on having fully processed both |
| 915 | // the declarator and initializer. |
Steve Naroff | 9474504 | 2007-09-13 23:52:58 +0000 | [diff] [blame] | 916 | for (ScopedDecl *ID = NewGroup; ID; ID = ID->getNextDeclarator()) { |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 917 | VarDecl *IDecl = dyn_cast<VarDecl>(ID); |
| 918 | if (!IDecl) |
| 919 | continue; |
| 920 | FileVarDecl *FVD = dyn_cast<FileVarDecl>(IDecl); |
| 921 | BlockVarDecl *BVD = dyn_cast<BlockVarDecl>(IDecl); |
| 922 | QualType T = IDecl->getType(); |
| 923 | |
| 924 | // C99 6.7.5.2p2: If an identifier is declared to be an object with |
| 925 | // static storage duration, it shall not have a variable length array. |
| 926 | if ((FVD || BVD) && IDecl->getStorageClass() == VarDecl::Static) { |
Eli Friedman | 3fe0293 | 2008-02-15 19:53:52 +0000 | [diff] [blame] | 927 | if (T->getAsVariableArrayType()) { |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 928 | Diag(IDecl->getLocation(), diag::err_typecheck_illegal_vla); |
| 929 | IDecl->setInvalidDecl(); |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 930 | } |
| 931 | } |
| 932 | // Block scope. C99 6.7p7: If an identifier for an object is declared with |
| 933 | // no linkage (C99 6.2.2p6), the type for the object shall be complete... |
| 934 | if (BVD && IDecl->getStorageClass() != VarDecl::Extern) { |
| 935 | if (T->isIncompleteType()) { |
Chris Lattner | 8b1be77 | 2007-12-02 07:50:03 +0000 | [diff] [blame] | 936 | Diag(IDecl->getLocation(), diag::err_typecheck_decl_incomplete_type, |
| 937 | T.getAsString()); |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 938 | IDecl->setInvalidDecl(); |
| 939 | } |
| 940 | } |
| 941 | // File scope. C99 6.9.2p2: A declaration of an identifier for and |
| 942 | // object that has file scope without an initializer, and without a |
| 943 | // storage-class specifier or with the storage-class specifier "static", |
| 944 | // constitutes a tentative definition. Note: A tentative definition with |
| 945 | // external linkage is valid (C99 6.2.2p5). |
Steve Naroff | d3cd1e5 | 2008-01-18 00:39:39 +0000 | [diff] [blame] | 946 | if (FVD && !FVD->getInit() && (FVD->getStorageClass() == VarDecl::Static || |
| 947 | FVD->getStorageClass() == VarDecl::None)) { |
Eli Friedman | 9db1397 | 2008-02-15 12:53:51 +0000 | [diff] [blame] | 948 | if (T->isIncompleteArrayType()) { |
Steve Naroff | 9a75f8a | 2008-01-18 20:40:52 +0000 | [diff] [blame] | 949 | // C99 6.9.2 (p2, p5): Implicit initialization causes an incomplete |
| 950 | // array to be completed. Don't issue a diagnostic. |
| 951 | } else if (T->isIncompleteType()) { |
| 952 | // C99 6.9.2p3: If the declaration of an identifier for an object is |
| 953 | // a tentative definition and has internal linkage (C99 6.2.2p3), the |
| 954 | // declared type shall not be an incomplete type. |
Chris Lattner | 8b1be77 | 2007-12-02 07:50:03 +0000 | [diff] [blame] | 955 | Diag(IDecl->getLocation(), diag::err_typecheck_decl_incomplete_type, |
| 956 | T.getAsString()); |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 957 | IDecl->setInvalidDecl(); |
| 958 | } |
| 959 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 960 | } |
| 961 | return NewGroup; |
| 962 | } |
Steve Naroff | e1223f7 | 2007-08-28 03:03:08 +0000 | [diff] [blame] | 963 | |
| 964 | // Called from Sema::ParseStartOfFunctionDef(). |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 965 | ParmVarDecl * |
Nate Begeman | 6d20d03 | 2008-02-17 21:02:04 +0000 | [diff] [blame] | 966 | Sema::ActOnParamDeclarator(struct DeclaratorChunk::ParamInfo &PI, |
| 967 | Scope *FnScope) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 968 | IdentifierInfo *II = PI.Ident; |
| 969 | // TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope. |
| 970 | // Can this happen for params? We already checked that they don't conflict |
| 971 | // among each other. Here they can only shadow globals, which is ok. |
Chris Lattner | 8b9023b | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 972 | if (/*Decl *PrevDecl = */LookupScopedDecl(II, Decl::IDNS_Ordinary, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 973 | PI.IdentLoc, FnScope)) { |
| 974 | |
| 975 | } |
| 976 | |
| 977 | // FIXME: Handle storage class (auto, register). No declarator? |
| 978 | // TODO: Chain to previous parameter with the prevdeclarator chain? |
Steve Naroff | 6a9f3e3 | 2007-08-07 22:44:21 +0000 | [diff] [blame] | 979 | |
| 980 | // Perform the default function/array conversion (C99 6.7.5.3p[7,8]). |
| 981 | // Doing the promotion here has a win and a loss. The win is the type for |
| 982 | // both Decl's and DeclRefExpr's will match (a convenient invariant for the |
| 983 | // code generator). The loss is the orginal type isn't preserved. For example: |
| 984 | // |
| 985 | // void func(int parmvardecl[5]) { // convert "int [5]" to "int *" |
| 986 | // int blockvardecl[5]; |
| 987 | // sizeof(parmvardecl); // size == 4 |
| 988 | // sizeof(blockvardecl); // size == 20 |
| 989 | // } |
| 990 | // |
| 991 | // For expressions, all implicit conversions are captured using the |
| 992 | // ImplicitCastExpr AST node (we have no such mechanism for Decl's). |
| 993 | // |
| 994 | // FIXME: If a source translation tool needs to see the original type, then |
| 995 | // we need to consider storing both types (in ParmVarDecl)... |
| 996 | // |
| 997 | QualType parmDeclType = QualType::getFromOpaquePtr(PI.TypeInfo); |
Chris Lattner | 529bd02 | 2008-01-02 22:50:48 +0000 | [diff] [blame] | 998 | if (const ArrayType *AT = parmDeclType->getAsArrayType()) { |
| 999 | // int x[restrict 4] -> int *restrict |
Steve Naroff | 6a9f3e3 | 2007-08-07 22:44:21 +0000 | [diff] [blame] | 1000 | parmDeclType = Context.getPointerType(AT->getElementType()); |
Chris Lattner | 529bd02 | 2008-01-02 22:50:48 +0000 | [diff] [blame] | 1001 | parmDeclType = parmDeclType.getQualifiedType(AT->getIndexTypeQualifier()); |
| 1002 | } else if (parmDeclType->isFunctionType()) |
Steve Naroff | 6a9f3e3 | 2007-08-07 22:44:21 +0000 | [diff] [blame] | 1003 | parmDeclType = Context.getPointerType(parmDeclType); |
| 1004 | |
| 1005 | ParmVarDecl *New = new ParmVarDecl(PI.IdentLoc, II, parmDeclType, |
Anders Carlsson | f78915f | 2008-02-15 07:04:12 +0000 | [diff] [blame] | 1006 | VarDecl::None, 0); |
Anders Carlsson | f78915f | 2008-02-15 07:04:12 +0000 | [diff] [blame] | 1007 | |
Steve Naroff | 53a3234 | 2007-08-28 18:45:29 +0000 | [diff] [blame] | 1008 | if (PI.InvalidType) |
| 1009 | New->setInvalidDecl(); |
| 1010 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1011 | // If this has an identifier, add it to the scope stack. |
| 1012 | if (II) { |
Steve Naroff | c752d04 | 2007-09-13 18:10:37 +0000 | [diff] [blame] | 1013 | New->setNext(II->getFETokenInfo<ScopedDecl>()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1014 | II->setFETokenInfo(New); |
| 1015 | FnScope->AddDecl(New); |
| 1016 | } |
Nate Begeman | b7894b5 | 2008-02-17 21:20:31 +0000 | [diff] [blame] | 1017 | |
| 1018 | HandleDeclAttributes(New, PI.AttrList, 0); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1019 | return New; |
| 1020 | } |
Fariborz Jahanian | 306d68f | 2007-11-08 23:49:49 +0000 | [diff] [blame] | 1021 | |
Chris Lattner | b652cea | 2007-10-09 17:14:05 +0000 | [diff] [blame] | 1022 | Sema::DeclTy *Sema::ActOnStartOfFunctionDef(Scope *FnBodyScope, Declarator &D) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1023 | assert(CurFunctionDecl == 0 && "Function parsing confused"); |
| 1024 | assert(D.getTypeObject(0).Kind == DeclaratorChunk::Function && |
| 1025 | "Not a function declarator!"); |
| 1026 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun; |
| 1027 | |
| 1028 | // Verify 6.9.1p6: 'every identifier in the identifier list shall be declared' |
| 1029 | // for a K&R function. |
| 1030 | if (!FTI.hasPrototype) { |
| 1031 | for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) { |
| 1032 | if (FTI.ArgInfo[i].TypeInfo == 0) { |
| 1033 | Diag(FTI.ArgInfo[i].IdentLoc, diag::ext_param_not_declared, |
| 1034 | FTI.ArgInfo[i].Ident->getName()); |
| 1035 | // Implicitly declare the argument as type 'int' for lack of a better |
| 1036 | // type. |
| 1037 | FTI.ArgInfo[i].TypeInfo = Context.IntTy.getAsOpaquePtr(); |
| 1038 | } |
| 1039 | } |
Chris Lattner | 5280408 | 2008-02-17 19:31:09 +0000 | [diff] [blame] | 1040 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1041 | // Since this is a function definition, act as though we have information |
| 1042 | // about the arguments. |
Chris Lattner | 5280408 | 2008-02-17 19:31:09 +0000 | [diff] [blame] | 1043 | if (FTI.NumArgs) |
| 1044 | FTI.hasPrototype = true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1045 | } else { |
| 1046 | // FIXME: Diagnose arguments without names in C. |
| 1047 | |
| 1048 | } |
| 1049 | |
| 1050 | Scope *GlobalScope = FnBodyScope->getParent(); |
Steve Naroff | adbbd0c | 2008-01-14 20:51:29 +0000 | [diff] [blame] | 1051 | |
| 1052 | // See if this is a redefinition. |
| 1053 | ScopedDecl *PrevDcl = LookupScopedDecl(D.getIdentifier(), Decl::IDNS_Ordinary, |
| 1054 | D.getIdentifierLoc(), GlobalScope); |
| 1055 | if (FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(PrevDcl)) { |
| 1056 | if (FD->getBody()) { |
| 1057 | Diag(D.getIdentifierLoc(), diag::err_redefinition, |
| 1058 | D.getIdentifier()->getName()); |
| 1059 | Diag(FD->getLocation(), diag::err_previous_definition); |
| 1060 | } |
| 1061 | } |
Steve Naroff | fabbc34 | 2008-02-12 01:09:36 +0000 | [diff] [blame] | 1062 | Decl *decl = static_cast<Decl*>(ActOnDeclarator(GlobalScope, D, 0)); |
Chris Lattner | e9ba323 | 2008-02-16 01:20:36 +0000 | [diff] [blame] | 1063 | FunctionDecl *FD = cast<FunctionDecl>(decl); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1064 | CurFunctionDecl = FD; |
| 1065 | |
| 1066 | // Create Decl objects for each parameter, adding them to the FunctionDecl. |
| 1067 | llvm::SmallVector<ParmVarDecl*, 16> Params; |
| 1068 | |
| 1069 | // Check for C99 6.7.5.3p10 - foo(void) is a non-varargs function that takes |
| 1070 | // no arguments, not a function that takes a single void argument. |
| 1071 | if (FTI.NumArgs == 1 && !FTI.isVariadic && FTI.ArgInfo[0].Ident == 0 && |
Chris Lattner | f46699c | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 1072 | !QualType::getFromOpaquePtr(FTI.ArgInfo[0].TypeInfo).getCVRQualifiers() && |
Chris Lattner | b751c28 | 2007-11-28 18:51:29 +0000 | [diff] [blame] | 1073 | QualType::getFromOpaquePtr(FTI.ArgInfo[0].TypeInfo)->isVoidType()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1074 | // empty arg list, don't push any params. |
| 1075 | } else { |
Steve Naroff | 6649992 | 2007-11-12 03:44:46 +0000 | [diff] [blame] | 1076 | for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) { |
Nate Begeman | bff5f5c | 2007-11-13 21:49:48 +0000 | [diff] [blame] | 1077 | Params.push_back(ActOnParamDeclarator(D.getTypeObject(0).Fun.ArgInfo[i], |
Steve Naroff | 6649992 | 2007-11-12 03:44:46 +0000 | [diff] [blame] | 1078 | FnBodyScope)); |
| 1079 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1080 | } |
| 1081 | |
| 1082 | FD->setParams(&Params[0], Params.size()); |
| 1083 | |
| 1084 | return FD; |
| 1085 | } |
| 1086 | |
Steve Naroff | d6d054d | 2007-11-11 23:20:51 +0000 | [diff] [blame] | 1087 | Sema::DeclTy *Sema::ActOnFinishFunctionBody(DeclTy *D, StmtTy *Body) { |
| 1088 | Decl *dcl = static_cast<Decl *>(D); |
| 1089 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(dcl)) { |
| 1090 | FD->setBody((Stmt*)Body); |
| 1091 | assert(FD == CurFunctionDecl && "Function parsing confused"); |
Steve Naroff | 4d83220 | 2007-12-13 18:18:56 +0000 | [diff] [blame] | 1092 | CurFunctionDecl = 0; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1093 | } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(dcl)) { |
Steve Naroff | d6d054d | 2007-11-11 23:20:51 +0000 | [diff] [blame] | 1094 | MD->setBody((Stmt*)Body); |
Steve Naroff | 0330071 | 2007-11-12 13:56:41 +0000 | [diff] [blame] | 1095 | CurMethodDecl = 0; |
Steve Naroff | 4d83220 | 2007-12-13 18:18:56 +0000 | [diff] [blame] | 1096 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1097 | // Verify and clean out per-function state. |
| 1098 | |
| 1099 | // Check goto/label use. |
| 1100 | for (llvm::DenseMap<IdentifierInfo*, LabelStmt*>::iterator |
| 1101 | I = LabelMap.begin(), E = LabelMap.end(); I != E; ++I) { |
| 1102 | // Verify that we have no forward references left. If so, there was a goto |
| 1103 | // or address of a label taken, but no definition of it. Label fwd |
| 1104 | // definitions are indicated with a null substmt. |
| 1105 | if (I->second->getSubStmt() == 0) { |
| 1106 | LabelStmt *L = I->second; |
| 1107 | // Emit error. |
| 1108 | Diag(L->getIdentLoc(), diag::err_undeclared_label_use, L->getName()); |
| 1109 | |
| 1110 | // At this point, we have gotos that use the bogus label. Stitch it into |
| 1111 | // the function body so that they aren't leaked and that the AST is well |
| 1112 | // formed. |
Chris Lattner | 0cbc215 | 2008-01-25 00:01:10 +0000 | [diff] [blame] | 1113 | if (Body) { |
| 1114 | L->setSubStmt(new NullStmt(L->getIdentLoc())); |
| 1115 | cast<CompoundStmt>((Stmt*)Body)->push_back(L); |
| 1116 | } else { |
| 1117 | // The whole function wasn't parsed correctly, just delete this. |
| 1118 | delete L; |
| 1119 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1120 | } |
| 1121 | } |
| 1122 | LabelMap.clear(); |
| 1123 | |
Steve Naroff | d6d054d | 2007-11-11 23:20:51 +0000 | [diff] [blame] | 1124 | return D; |
Fariborz Jahanian | 60fbca0 | 2007-11-10 16:31:34 +0000 | [diff] [blame] | 1125 | } |
| 1126 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1127 | /// ImplicitlyDefineFunction - An undeclared identifier was used in a function |
| 1128 | /// call, forming a call to an implicitly defined function (per C99 6.5.1p2). |
Steve Naroff | 8c9f13e | 2007-09-16 16:16:00 +0000 | [diff] [blame] | 1129 | ScopedDecl *Sema::ImplicitlyDefineFunction(SourceLocation Loc, |
| 1130 | IdentifierInfo &II, Scope *S) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1131 | if (getLangOptions().C99) // Extension in C99. |
| 1132 | Diag(Loc, diag::ext_implicit_function_decl, II.getName()); |
| 1133 | else // Legal in C90, but warn about it. |
| 1134 | Diag(Loc, diag::warn_implicit_function_decl, II.getName()); |
| 1135 | |
| 1136 | // FIXME: handle stuff like: |
| 1137 | // void foo() { extern float X(); } |
| 1138 | // void bar() { X(); } <-- implicit decl for X in another scope. |
| 1139 | |
| 1140 | // Set a Declarator for the implicit definition: int foo(); |
| 1141 | const char *Dummy; |
| 1142 | DeclSpec DS; |
| 1143 | bool Error = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, Dummy); |
| 1144 | Error = Error; // Silence warning. |
| 1145 | assert(!Error && "Error setting up implicit decl!"); |
| 1146 | Declarator D(DS, Declarator::BlockContext); |
| 1147 | D.AddTypeInfo(DeclaratorChunk::getFunction(false, false, 0, 0, Loc)); |
| 1148 | D.SetIdentifier(&II, Loc); |
| 1149 | |
| 1150 | // Find translation-unit scope to insert this function into. |
Chris Lattner | 31e0572 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 1151 | if (Scope *FnS = S->getFnParent()) |
| 1152 | S = FnS->getParent(); // Skip all scopes in a function at once. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1153 | while (S->getParent()) |
| 1154 | S = S->getParent(); |
| 1155 | |
Steve Naroff | 8c9f13e | 2007-09-16 16:16:00 +0000 | [diff] [blame] | 1156 | return dyn_cast<ScopedDecl>(static_cast<Decl*>(ActOnDeclarator(S, D, 0))); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1157 | } |
| 1158 | |
| 1159 | |
Chris Lattner | 41af093 | 2007-11-14 06:34:38 +0000 | [diff] [blame] | 1160 | TypedefDecl *Sema::ParseTypedefDecl(Scope *S, Declarator &D, QualType T, |
Steve Naroff | 9474504 | 2007-09-13 23:52:58 +0000 | [diff] [blame] | 1161 | ScopedDecl *LastDeclarator) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1162 | assert(D.getIdentifier() && "Wrong callback for declspec without declarator"); |
Steve Naroff | 5912a35 | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 1163 | assert(!T.isNull() && "GetTypeForDeclarator() returned null type"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1164 | |
| 1165 | // Scope manipulation handled by caller. |
Steve Naroff | 5912a35 | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 1166 | TypedefDecl *NewTD = new TypedefDecl(D.getIdentifierLoc(), D.getIdentifier(), |
| 1167 | T, LastDeclarator); |
| 1168 | if (D.getInvalidType()) |
| 1169 | NewTD->setInvalidDecl(); |
| 1170 | return NewTD; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1171 | } |
| 1172 | |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 1173 | /// ActOnTag - This is invoked when we see 'struct foo' or 'struct {'. In the |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1174 | /// former case, Name will be non-null. In the later case, Name will be null. |
| 1175 | /// TagType indicates what kind of tag this is. TK indicates whether this is a |
| 1176 | /// reference/declaration/definition of a tag. |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 1177 | Sema::DeclTy *Sema::ActOnTag(Scope *S, unsigned TagType, TagKind TK, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1178 | SourceLocation KWLoc, IdentifierInfo *Name, |
| 1179 | SourceLocation NameLoc, AttributeList *Attr) { |
| 1180 | // If this is a use of an existing tag, it must have a name. |
| 1181 | assert((Name != 0 || TK == TK_Definition) && |
| 1182 | "Nameless record must be a definition!"); |
| 1183 | |
| 1184 | Decl::Kind Kind; |
| 1185 | switch (TagType) { |
| 1186 | default: assert(0 && "Unknown tag type!"); |
| 1187 | case DeclSpec::TST_struct: Kind = Decl::Struct; break; |
| 1188 | case DeclSpec::TST_union: Kind = Decl::Union; break; |
| 1189 | //case DeclSpec::TST_class: Kind = Decl::Class; break; |
| 1190 | case DeclSpec::TST_enum: Kind = Decl::Enum; break; |
| 1191 | } |
| 1192 | |
| 1193 | // If this is a named struct, check to see if there was a previous forward |
| 1194 | // declaration or definition. |
| 1195 | if (TagDecl *PrevDecl = |
| 1196 | dyn_cast_or_null<TagDecl>(LookupScopedDecl(Name, Decl::IDNS_Tag, |
| 1197 | NameLoc, S))) { |
| 1198 | |
| 1199 | // If this is a use of a previous tag, or if the tag is already declared in |
| 1200 | // the same scope (so that the definition/declaration completes or |
| 1201 | // rementions the tag), reuse the decl. |
| 1202 | if (TK == TK_Reference || S->isDeclScope(PrevDecl)) { |
| 1203 | // Make sure that this wasn't declared as an enum and now used as a struct |
| 1204 | // or something similar. |
| 1205 | if (PrevDecl->getKind() != Kind) { |
| 1206 | Diag(KWLoc, diag::err_use_with_wrong_tag, Name->getName()); |
| 1207 | Diag(PrevDecl->getLocation(), diag::err_previous_use); |
| 1208 | } |
| 1209 | |
| 1210 | // If this is a use or a forward declaration, we're good. |
| 1211 | if (TK != TK_Definition) |
| 1212 | return PrevDecl; |
| 1213 | |
| 1214 | // Diagnose attempts to redefine a tag. |
| 1215 | if (PrevDecl->isDefinition()) { |
| 1216 | Diag(NameLoc, diag::err_redefinition, Name->getName()); |
| 1217 | Diag(PrevDecl->getLocation(), diag::err_previous_definition); |
| 1218 | // If this is a redefinition, recover by making this struct be |
| 1219 | // anonymous, which will make any later references get the previous |
| 1220 | // definition. |
| 1221 | Name = 0; |
| 1222 | } else { |
| 1223 | // Okay, this is definition of a previously declared or referenced tag. |
| 1224 | // Move the location of the decl to be the definition site. |
| 1225 | PrevDecl->setLocation(NameLoc); |
| 1226 | return PrevDecl; |
| 1227 | } |
| 1228 | } |
| 1229 | // If we get here, this is a definition of a new struct type in a nested |
| 1230 | // scope, e.g. "struct foo; void bar() { struct foo; }", just create a new |
| 1231 | // type. |
| 1232 | } |
| 1233 | |
| 1234 | // If there is an identifier, use the location of the identifier as the |
| 1235 | // location of the decl, otherwise use the location of the struct/union |
| 1236 | // keyword. |
| 1237 | SourceLocation Loc = NameLoc.isValid() ? NameLoc : KWLoc; |
| 1238 | |
| 1239 | // Otherwise, if this is the first time we've seen this tag, create the decl. |
| 1240 | TagDecl *New; |
| 1241 | switch (Kind) { |
| 1242 | default: assert(0 && "Unknown tag kind!"); |
| 1243 | case Decl::Enum: |
| 1244 | // FIXME: Tag decls should be chained to any simultaneous vardecls, e.g.: |
| 1245 | // enum X { A, B, C } D; D should chain to X. |
| 1246 | New = new EnumDecl(Loc, Name, 0); |
| 1247 | // If this is an undefined enum, warn. |
| 1248 | if (TK != TK_Definition) Diag(Loc, diag::ext_forward_ref_enum); |
| 1249 | break; |
| 1250 | case Decl::Union: |
| 1251 | case Decl::Struct: |
| 1252 | case Decl::Class: |
| 1253 | // FIXME: Tag decls should be chained to any simultaneous vardecls, e.g.: |
| 1254 | // struct X { int A; } D; D should chain to X. |
| 1255 | New = new RecordDecl(Kind, Loc, Name, 0); |
| 1256 | break; |
| 1257 | } |
| 1258 | |
| 1259 | // If this has an identifier, add it to the scope stack. |
| 1260 | if (Name) { |
Chris Lattner | 31e0572 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 1261 | // The scope passed in may not be a decl scope. Zip up the scope tree until |
| 1262 | // we find one that is. |
| 1263 | while ((S->getFlags() & Scope::DeclScope) == 0) |
| 1264 | S = S->getParent(); |
| 1265 | |
| 1266 | // Add it to the decl chain. |
Steve Naroff | c752d04 | 2007-09-13 18:10:37 +0000 | [diff] [blame] | 1267 | New->setNext(Name->getFETokenInfo<ScopedDecl>()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1268 | Name->setFETokenInfo(New); |
| 1269 | S->AddDecl(New); |
| 1270 | } |
Chris Lattner | e1e7985 | 2008-02-06 00:51:33 +0000 | [diff] [blame] | 1271 | |
Anders Carlsson | ad14806 | 2008-02-16 00:29:18 +0000 | [diff] [blame] | 1272 | HandleDeclAttributes(New, Attr, 0); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1273 | return New; |
| 1274 | } |
| 1275 | |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 1276 | /// ActOnField - Each field of a struct/union/class is passed into this in order |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1277 | /// to create a FieldDecl object for it. |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 1278 | Sema::DeclTy *Sema::ActOnField(Scope *S, DeclTy *TagDecl, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1279 | SourceLocation DeclStart, |
| 1280 | Declarator &D, ExprTy *BitfieldWidth) { |
| 1281 | IdentifierInfo *II = D.getIdentifier(); |
| 1282 | Expr *BitWidth = (Expr*)BitfieldWidth; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1283 | SourceLocation Loc = DeclStart; |
| 1284 | if (II) Loc = D.getIdentifierLoc(); |
| 1285 | |
| 1286 | // FIXME: Unnamed fields can be handled in various different ways, for |
| 1287 | // example, unnamed unions inject all members into the struct namespace! |
| 1288 | |
| 1289 | |
| 1290 | if (BitWidth) { |
| 1291 | // TODO: Validate. |
| 1292 | //printf("WARNING: BITFIELDS IGNORED!\n"); |
| 1293 | |
| 1294 | // 6.7.2.1p3 |
| 1295 | // 6.7.2.1p4 |
| 1296 | |
| 1297 | } else { |
| 1298 | // Not a bitfield. |
| 1299 | |
| 1300 | // validate II. |
| 1301 | |
| 1302 | } |
| 1303 | |
| 1304 | QualType T = GetTypeForDeclarator(D, S); |
Steve Naroff | 5912a35 | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 1305 | assert(!T.isNull() && "GetTypeForDeclarator() returned null type"); |
| 1306 | bool InvalidDecl = false; |
Steve Naroff | d7444aa | 2007-08-31 17:20:07 +0000 | [diff] [blame] | 1307 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1308 | // C99 6.7.2.1p8: A member of a structure or union may have any type other |
| 1309 | // than a variably modified type. |
Eli Friedman | 9db1397 | 2008-02-15 12:53:51 +0000 | [diff] [blame] | 1310 | if (T->isVariablyModifiedType()) { |
| 1311 | // FIXME: This diagnostic needs work |
| 1312 | Diag(Loc, diag::err_typecheck_illegal_vla, Loc); |
Steve Naroff | d7444aa | 2007-08-31 17:20:07 +0000 | [diff] [blame] | 1313 | InvalidDecl = true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1314 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1315 | // FIXME: Chain fielddecls together. |
Steve Naroff | 4473921 | 2007-09-11 21:17:26 +0000 | [diff] [blame] | 1316 | FieldDecl *NewFD; |
| 1317 | |
| 1318 | if (isa<RecordDecl>(static_cast<Decl *>(TagDecl))) |
Devang Patel | 54e6235 | 2007-11-01 16:29:56 +0000 | [diff] [blame] | 1319 | NewFD = new FieldDecl(Loc, II, T, BitWidth); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1320 | else if (isa<ObjCInterfaceDecl>(static_cast<Decl *>(TagDecl)) || |
| 1321 | isa<ObjCImplementationDecl>(static_cast<Decl *>(TagDecl)) || |
| 1322 | isa<ObjCCategoryDecl>(static_cast<Decl *>(TagDecl)) || |
Steve Naroff | ddd600f | 2007-11-14 14:15:31 +0000 | [diff] [blame] | 1323 | // FIXME: ivars are currently used to model properties, and |
| 1324 | // properties can appear within a protocol. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1325 | // See corresponding FIXME in DeclObjC.h:ObjCPropertyDecl. |
| 1326 | isa<ObjCProtocolDecl>(static_cast<Decl *>(TagDecl))) |
| 1327 | NewFD = new ObjCIvarDecl(Loc, II, T); |
Steve Naroff | 4473921 | 2007-09-11 21:17:26 +0000 | [diff] [blame] | 1328 | else |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 1329 | assert(0 && "Sema::ActOnField(): Unknown TagDecl"); |
Steve Naroff | 4473921 | 2007-09-11 21:17:26 +0000 | [diff] [blame] | 1330 | |
Anders Carlsson | ad14806 | 2008-02-16 00:29:18 +0000 | [diff] [blame] | 1331 | HandleDeclAttributes(NewFD, D.getDeclSpec().getAttributes(), |
| 1332 | D.getAttributes()); |
| 1333 | |
Steve Naroff | 5912a35 | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 1334 | if (D.getInvalidType() || InvalidDecl) |
| 1335 | NewFD->setInvalidDecl(); |
| 1336 | return NewFD; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1337 | } |
| 1338 | |
Fariborz Jahanian | 89204a1 | 2007-10-01 16:53:59 +0000 | [diff] [blame] | 1339 | /// TranslateIvarVisibility - Translate visibility from a token ID to an |
| 1340 | /// AST enum value. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1341 | static ObjCIvarDecl::AccessControl |
Fariborz Jahanian | 89204a1 | 2007-10-01 16:53:59 +0000 | [diff] [blame] | 1342 | TranslateIvarVisibility(tok::ObjCKeywordKind ivarVisibility) { |
Steve Naroff | f13271f | 2007-09-14 23:09:53 +0000 | [diff] [blame] | 1343 | switch (ivarVisibility) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1344 | case tok::objc_private: return ObjCIvarDecl::Private; |
| 1345 | case tok::objc_public: return ObjCIvarDecl::Public; |
| 1346 | case tok::objc_protected: return ObjCIvarDecl::Protected; |
| 1347 | case tok::objc_package: return ObjCIvarDecl::Package; |
Fariborz Jahanian | 89204a1 | 2007-10-01 16:53:59 +0000 | [diff] [blame] | 1348 | default: assert(false && "Unknown visitibility kind"); |
Steve Naroff | f13271f | 2007-09-14 23:09:53 +0000 | [diff] [blame] | 1349 | } |
| 1350 | } |
| 1351 | |
Fariborz Jahanian | 9d048ff | 2007-09-29 00:54:24 +0000 | [diff] [blame] | 1352 | void Sema::ActOnFields(Scope* S, |
Fariborz Jahanian | 3f5faf7 | 2007-10-04 00:45:27 +0000 | [diff] [blame] | 1353 | SourceLocation RecLoc, DeclTy *RecDecl, |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 1354 | DeclTy **Fields, unsigned NumFields, |
Steve Naroff | 60fccee | 2007-10-29 21:38:07 +0000 | [diff] [blame] | 1355 | SourceLocation LBrac, SourceLocation RBrac, |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 1356 | tok::ObjCKeywordKind *visibility) { |
Steve Naroff | 7421664 | 2007-09-14 22:20:54 +0000 | [diff] [blame] | 1357 | Decl *EnclosingDecl = static_cast<Decl*>(RecDecl); |
| 1358 | assert(EnclosingDecl && "missing record or interface decl"); |
| 1359 | RecordDecl *Record = dyn_cast<RecordDecl>(EnclosingDecl); |
| 1360 | |
Fariborz Jahanian | e267ab6 | 2007-09-14 16:27:55 +0000 | [diff] [blame] | 1361 | if (Record && Record->isDefinition()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1362 | // Diagnose code like: |
| 1363 | // struct S { struct S {} X; }; |
| 1364 | // We discover this when we complete the outer S. Reject and ignore the |
| 1365 | // outer S. |
| 1366 | Diag(Record->getLocation(), diag::err_nested_redefinition, |
| 1367 | Record->getKindName()); |
| 1368 | Diag(RecLoc, diag::err_previous_definition); |
Steve Naroff | 7421664 | 2007-09-14 22:20:54 +0000 | [diff] [blame] | 1369 | Record->setInvalidDecl(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1370 | return; |
| 1371 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1372 | // Verify that all the fields are okay. |
| 1373 | unsigned NumNamedMembers = 0; |
| 1374 | llvm::SmallVector<FieldDecl*, 32> RecFields; |
| 1375 | llvm::SmallSet<const IdentifierInfo*, 32> FieldIDs; |
Steve Naroff | 7421664 | 2007-09-14 22:20:54 +0000 | [diff] [blame] | 1376 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1377 | for (unsigned i = 0; i != NumFields; ++i) { |
Fariborz Jahanian | e267ab6 | 2007-09-14 16:27:55 +0000 | [diff] [blame] | 1378 | |
Steve Naroff | 7421664 | 2007-09-14 22:20:54 +0000 | [diff] [blame] | 1379 | FieldDecl *FD = cast_or_null<FieldDecl>(static_cast<Decl*>(Fields[i])); |
| 1380 | assert(FD && "missing field decl"); |
| 1381 | |
| 1382 | // Remember all fields. |
| 1383 | RecFields.push_back(FD); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1384 | |
| 1385 | // Get the type for the field. |
Chris Lattner | 02c642e | 2007-07-31 21:33:24 +0000 | [diff] [blame] | 1386 | Type *FDTy = FD->getType().getTypePtr(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1387 | |
Steve Naroff | f13271f | 2007-09-14 23:09:53 +0000 | [diff] [blame] | 1388 | // If we have visibility info, make sure the AST is set accordingly. |
| 1389 | if (visibility) |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1390 | cast<ObjCIvarDecl>(FD)->setAccessControl( |
Fariborz Jahanian | 89204a1 | 2007-10-01 16:53:59 +0000 | [diff] [blame] | 1391 | TranslateIvarVisibility(visibility[i])); |
Steve Naroff | f13271f | 2007-09-14 23:09:53 +0000 | [diff] [blame] | 1392 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1393 | // C99 6.7.2.1p2 - A field may not be a function type. |
Chris Lattner | 02c642e | 2007-07-31 21:33:24 +0000 | [diff] [blame] | 1394 | if (FDTy->isFunctionType()) { |
Steve Naroff | 7421664 | 2007-09-14 22:20:54 +0000 | [diff] [blame] | 1395 | Diag(FD->getLocation(), diag::err_field_declared_as_function, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1396 | FD->getName()); |
Steve Naroff | 7421664 | 2007-09-14 22:20:54 +0000 | [diff] [blame] | 1397 | FD->setInvalidDecl(); |
| 1398 | EnclosingDecl->setInvalidDecl(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1399 | continue; |
| 1400 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1401 | // C99 6.7.2.1p2 - A field may not be an incomplete type except... |
| 1402 | if (FDTy->isIncompleteType()) { |
Fariborz Jahanian | e267ab6 | 2007-09-14 16:27:55 +0000 | [diff] [blame] | 1403 | if (!Record) { // Incomplete ivar type is always an error. |
Fariborz Jahanian | 3f5faf7 | 2007-10-04 00:45:27 +0000 | [diff] [blame] | 1404 | Diag(FD->getLocation(), diag::err_field_incomplete, FD->getName()); |
Steve Naroff | 7421664 | 2007-09-14 22:20:54 +0000 | [diff] [blame] | 1405 | FD->setInvalidDecl(); |
| 1406 | EnclosingDecl->setInvalidDecl(); |
Fariborz Jahanian | 3f5faf7 | 2007-10-04 00:45:27 +0000 | [diff] [blame] | 1407 | continue; |
Fariborz Jahanian | e267ab6 | 2007-09-14 16:27:55 +0000 | [diff] [blame] | 1408 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1409 | if (i != NumFields-1 || // ... that the last member ... |
| 1410 | Record->getKind() != Decl::Struct || // ... of a structure ... |
Chris Lattner | 02c642e | 2007-07-31 21:33:24 +0000 | [diff] [blame] | 1411 | !FDTy->isArrayType()) { //... may have incomplete array type. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1412 | Diag(FD->getLocation(), diag::err_field_incomplete, FD->getName()); |
Steve Naroff | 7421664 | 2007-09-14 22:20:54 +0000 | [diff] [blame] | 1413 | FD->setInvalidDecl(); |
| 1414 | EnclosingDecl->setInvalidDecl(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1415 | continue; |
| 1416 | } |
Fariborz Jahanian | e267ab6 | 2007-09-14 16:27:55 +0000 | [diff] [blame] | 1417 | if (NumNamedMembers < 1) { //... must have more than named member ... |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1418 | Diag(FD->getLocation(), diag::err_flexible_array_empty_struct, |
| 1419 | FD->getName()); |
Steve Naroff | 7421664 | 2007-09-14 22:20:54 +0000 | [diff] [blame] | 1420 | FD->setInvalidDecl(); |
| 1421 | EnclosingDecl->setInvalidDecl(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1422 | continue; |
| 1423 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1424 | // Okay, we have a legal flexible array member at the end of the struct. |
Fariborz Jahanian | e267ab6 | 2007-09-14 16:27:55 +0000 | [diff] [blame] | 1425 | if (Record) |
| 1426 | Record->setHasFlexibleArrayMember(true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1427 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1428 | /// C99 6.7.2.1p2 - a struct ending in a flexible array member cannot be the |
| 1429 | /// field of another structure or the element of an array. |
Chris Lattner | 02c642e | 2007-07-31 21:33:24 +0000 | [diff] [blame] | 1430 | if (const RecordType *FDTTy = FDTy->getAsRecordType()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1431 | if (FDTTy->getDecl()->hasFlexibleArrayMember()) { |
| 1432 | // If this is a member of a union, then entire union becomes "flexible". |
Fariborz Jahanian | e267ab6 | 2007-09-14 16:27:55 +0000 | [diff] [blame] | 1433 | if (Record && Record->getKind() == Decl::Union) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1434 | Record->setHasFlexibleArrayMember(true); |
| 1435 | } else { |
| 1436 | // If this is a struct/class and this is not the last element, reject |
| 1437 | // it. Note that GCC supports variable sized arrays in the middle of |
| 1438 | // structures. |
| 1439 | if (i != NumFields-1) { |
| 1440 | Diag(FD->getLocation(), diag::err_variable_sized_type_in_struct, |
| 1441 | FD->getName()); |
Steve Naroff | 7421664 | 2007-09-14 22:20:54 +0000 | [diff] [blame] | 1442 | FD->setInvalidDecl(); |
| 1443 | EnclosingDecl->setInvalidDecl(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1444 | continue; |
| 1445 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1446 | // We support flexible arrays at the end of structs in other structs |
| 1447 | // as an extension. |
| 1448 | Diag(FD->getLocation(), diag::ext_flexible_array_in_struct, |
| 1449 | FD->getName()); |
Fariborz Jahanian | 3f5faf7 | 2007-10-04 00:45:27 +0000 | [diff] [blame] | 1450 | if (Record) |
Fariborz Jahanian | e267ab6 | 2007-09-14 16:27:55 +0000 | [diff] [blame] | 1451 | Record->setHasFlexibleArrayMember(true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1452 | } |
| 1453 | } |
| 1454 | } |
Fariborz Jahanian | e7f64cc | 2007-10-12 22:10:42 +0000 | [diff] [blame] | 1455 | /// A field cannot be an Objective-c object |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1456 | if (FDTy->isObjCInterfaceType()) { |
Fariborz Jahanian | e7f64cc | 2007-10-12 22:10:42 +0000 | [diff] [blame] | 1457 | Diag(FD->getLocation(), diag::err_statically_allocated_object, |
| 1458 | FD->getName()); |
| 1459 | FD->setInvalidDecl(); |
| 1460 | EnclosingDecl->setInvalidDecl(); |
| 1461 | continue; |
| 1462 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1463 | // Keep track of the number of named members. |
| 1464 | if (IdentifierInfo *II = FD->getIdentifier()) { |
| 1465 | // Detect duplicate member names. |
| 1466 | if (!FieldIDs.insert(II)) { |
| 1467 | Diag(FD->getLocation(), diag::err_duplicate_member, II->getName()); |
| 1468 | // Find the previous decl. |
| 1469 | SourceLocation PrevLoc; |
| 1470 | for (unsigned i = 0, e = RecFields.size(); ; ++i) { |
| 1471 | assert(i != e && "Didn't find previous def!"); |
| 1472 | if (RecFields[i]->getIdentifier() == II) { |
| 1473 | PrevLoc = RecFields[i]->getLocation(); |
| 1474 | break; |
| 1475 | } |
| 1476 | } |
| 1477 | Diag(PrevLoc, diag::err_previous_definition); |
Steve Naroff | 7421664 | 2007-09-14 22:20:54 +0000 | [diff] [blame] | 1478 | FD->setInvalidDecl(); |
| 1479 | EnclosingDecl->setInvalidDecl(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1480 | continue; |
| 1481 | } |
| 1482 | ++NumNamedMembers; |
| 1483 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1484 | } |
| 1485 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1486 | // Okay, we successfully defined 'Record'. |
Chris Lattner | e1e7985 | 2008-02-06 00:51:33 +0000 | [diff] [blame] | 1487 | if (Record) { |
Fariborz Jahanian | e267ab6 | 2007-09-14 16:27:55 +0000 | [diff] [blame] | 1488 | Record->defineBody(&RecFields[0], RecFields.size()); |
Chris Lattner | e1e7985 | 2008-02-06 00:51:33 +0000 | [diff] [blame] | 1489 | Consumer.HandleTagDeclDefinition(Record); |
| 1490 | } else { |
Chris Lattner | a91d381 | 2008-02-05 22:40:55 +0000 | [diff] [blame] | 1491 | ObjCIvarDecl **ClsFields = reinterpret_cast<ObjCIvarDecl**>(&RecFields[0]); |
| 1492 | if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(EnclosingDecl)) |
| 1493 | ID->addInstanceVariablesToClass(ClsFields, RecFields.size(), RBrac); |
| 1494 | else if (ObjCImplementationDecl *IMPDecl = |
| 1495 | dyn_cast<ObjCImplementationDecl>(EnclosingDecl)) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1496 | assert(IMPDecl && "ActOnFields - missing ObjCImplementationDecl"); |
| 1497 | IMPDecl->ObjCAddInstanceVariablesToClassImpl(ClsFields, RecFields.size()); |
Fariborz Jahanian | 3a3ca1b | 2007-10-31 18:48:14 +0000 | [diff] [blame] | 1498 | CheckImplementationIvars(IMPDecl, ClsFields, RecFields.size(), RBrac); |
Fariborz Jahanian | d0b90bf | 2007-09-26 18:27:25 +0000 | [diff] [blame] | 1499 | } |
Fariborz Jahanian | b04a021 | 2007-09-14 21:08:27 +0000 | [diff] [blame] | 1500 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1501 | } |
| 1502 | |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 1503 | Sema::DeclTy *Sema::ActOnEnumConstant(Scope *S, DeclTy *theEnumDecl, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1504 | DeclTy *lastEnumConst, |
| 1505 | SourceLocation IdLoc, IdentifierInfo *Id, |
| 1506 | SourceLocation EqualLoc, ExprTy *val) { |
| 1507 | theEnumDecl = theEnumDecl; // silence unused warning. |
| 1508 | EnumConstantDecl *LastEnumConst = |
| 1509 | cast_or_null<EnumConstantDecl>(static_cast<Decl*>(lastEnumConst)); |
| 1510 | Expr *Val = static_cast<Expr*>(val); |
| 1511 | |
Chris Lattner | 31e0572 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 1512 | // The scope passed in may not be a decl scope. Zip up the scope tree until |
| 1513 | // we find one that is. |
| 1514 | while ((S->getFlags() & Scope::DeclScope) == 0) |
| 1515 | S = S->getParent(); |
| 1516 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1517 | // Verify that there isn't already something declared with this name in this |
| 1518 | // scope. |
Steve Naroff | 8e74c93 | 2007-09-13 21:41:19 +0000 | [diff] [blame] | 1519 | if (ScopedDecl *PrevDecl = LookupScopedDecl(Id, Decl::IDNS_Ordinary, |
| 1520 | IdLoc, S)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1521 | if (S->isDeclScope(PrevDecl)) { |
| 1522 | if (isa<EnumConstantDecl>(PrevDecl)) |
| 1523 | Diag(IdLoc, diag::err_redefinition_of_enumerator, Id->getName()); |
| 1524 | else |
| 1525 | Diag(IdLoc, diag::err_redefinition, Id->getName()); |
| 1526 | Diag(PrevDecl->getLocation(), diag::err_previous_definition); |
Chris Lattner | a73349d | 2008-02-26 00:33:57 +0000 | [diff] [blame] | 1527 | delete Val; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1528 | return 0; |
| 1529 | } |
| 1530 | } |
| 1531 | |
| 1532 | llvm::APSInt EnumVal(32); |
| 1533 | QualType EltTy; |
| 1534 | if (Val) { |
Chris Lattner | 421a23d | 2007-08-27 21:16:18 +0000 | [diff] [blame] | 1535 | // Make sure to promote the operand type to int. |
| 1536 | UsualUnaryConversions(Val); |
| 1537 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1538 | // C99 6.7.2.2p2: Make sure we have an integer constant expression. |
| 1539 | SourceLocation ExpLoc; |
Chris Lattner | 590b664 | 2007-07-15 23:26:56 +0000 | [diff] [blame] | 1540 | if (!Val->isIntegerConstantExpr(EnumVal, Context, &ExpLoc)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1541 | Diag(ExpLoc, diag::err_enum_value_not_integer_constant_expr, |
| 1542 | Id->getName()); |
Chris Lattner | a73349d | 2008-02-26 00:33:57 +0000 | [diff] [blame] | 1543 | delete Val; |
Chris Lattner | b7416f9 | 2007-08-27 17:37:24 +0000 | [diff] [blame] | 1544 | Val = 0; // Just forget about it. |
Chris Lattner | e9ca851 | 2007-08-29 16:03:41 +0000 | [diff] [blame] | 1545 | } else { |
| 1546 | EltTy = Val->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1547 | } |
Chris Lattner | b7416f9 | 2007-08-27 17:37:24 +0000 | [diff] [blame] | 1548 | } |
| 1549 | |
| 1550 | if (!Val) { |
| 1551 | if (LastEnumConst) { |
| 1552 | // Assign the last value + 1. |
| 1553 | EnumVal = LastEnumConst->getInitVal(); |
| 1554 | ++EnumVal; |
Chris Lattner | 421a23d | 2007-08-27 21:16:18 +0000 | [diff] [blame] | 1555 | |
| 1556 | // Check for overflow on increment. |
| 1557 | if (EnumVal < LastEnumConst->getInitVal()) |
| 1558 | Diag(IdLoc, diag::warn_enum_value_overflow); |
| 1559 | |
Chris Lattner | b7416f9 | 2007-08-27 17:37:24 +0000 | [diff] [blame] | 1560 | EltTy = LastEnumConst->getType(); |
| 1561 | } else { |
| 1562 | // First value, set to zero. |
| 1563 | EltTy = Context.IntTy; |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1564 | EnumVal.zextOrTrunc(static_cast<uint32_t>(Context.getTypeSize(EltTy))); |
Chris Lattner | b7416f9 | 2007-08-27 17:37:24 +0000 | [diff] [blame] | 1565 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1566 | } |
| 1567 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1568 | EnumConstantDecl *New = new EnumConstantDecl(IdLoc, Id, EltTy, Val, EnumVal, |
| 1569 | LastEnumConst); |
| 1570 | |
| 1571 | // Register this decl in the current scope stack. |
Steve Naroff | c752d04 | 2007-09-13 18:10:37 +0000 | [diff] [blame] | 1572 | New->setNext(Id->getFETokenInfo<ScopedDecl>()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1573 | Id->setFETokenInfo(New); |
| 1574 | S->AddDecl(New); |
| 1575 | return New; |
| 1576 | } |
| 1577 | |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 1578 | void Sema::ActOnEnumBody(SourceLocation EnumLoc, DeclTy *EnumDeclX, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1579 | DeclTy **Elements, unsigned NumElements) { |
| 1580 | EnumDecl *Enum = cast<EnumDecl>(static_cast<Decl*>(EnumDeclX)); |
| 1581 | assert(!Enum->isDefinition() && "Enum redefinitions can't reach here"); |
| 1582 | |
Chris Lattner | e37f0be | 2007-08-28 05:10:31 +0000 | [diff] [blame] | 1583 | // TODO: If the result value doesn't fit in an int, it must be a long or long |
| 1584 | // long value. ISO C does not support this, but GCC does as an extension, |
| 1585 | // emit a warning. |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1586 | unsigned IntWidth = Context.Target.getIntWidth(); |
Chris Lattner | e37f0be | 2007-08-28 05:10:31 +0000 | [diff] [blame] | 1587 | |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 1588 | // Verify that all the values are okay, compute the size of the values, and |
| 1589 | // reverse the list. |
| 1590 | unsigned NumNegativeBits = 0; |
| 1591 | unsigned NumPositiveBits = 0; |
| 1592 | |
| 1593 | // Keep track of whether all elements have type int. |
| 1594 | bool AllElementsInt = true; |
| 1595 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1596 | EnumConstantDecl *EltList = 0; |
| 1597 | for (unsigned i = 0; i != NumElements; ++i) { |
| 1598 | EnumConstantDecl *ECD = |
| 1599 | cast_or_null<EnumConstantDecl>(static_cast<Decl*>(Elements[i])); |
| 1600 | if (!ECD) continue; // Already issued a diagnostic. |
Chris Lattner | 211a30e | 2007-08-28 05:27:00 +0000 | [diff] [blame] | 1601 | |
| 1602 | // If the enum value doesn't fit in an int, emit an extension warning. |
Chris Lattner | 211a30e | 2007-08-28 05:27:00 +0000 | [diff] [blame] | 1603 | const llvm::APSInt &InitVal = ECD->getInitVal(); |
Chris Lattner | a73349d | 2008-02-26 00:33:57 +0000 | [diff] [blame] | 1604 | assert(InitVal.getBitWidth() >= IntWidth && |
| 1605 | "Should have promoted value to int"); |
Chris Lattner | 211a30e | 2007-08-28 05:27:00 +0000 | [diff] [blame] | 1606 | if (InitVal.getBitWidth() > IntWidth) { |
| 1607 | llvm::APSInt V(InitVal); |
| 1608 | V.trunc(IntWidth); |
| 1609 | V.extend(InitVal.getBitWidth()); |
| 1610 | if (V != InitVal) |
| 1611 | Diag(ECD->getLocation(), diag::ext_enum_value_not_int, |
| 1612 | InitVal.toString()); |
| 1613 | } |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 1614 | |
| 1615 | // Keep track of the size of positive and negative values. |
Chris Lattner | a73349d | 2008-02-26 00:33:57 +0000 | [diff] [blame] | 1616 | if (InitVal.isUnsigned() || InitVal.isNonNegative()) |
Chris Lattner | 21dd821 | 2008-01-14 21:47:29 +0000 | [diff] [blame] | 1617 | NumPositiveBits = std::max(NumPositiveBits, |
| 1618 | (unsigned)InitVal.getActiveBits()); |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 1619 | else |
Chris Lattner | 21dd821 | 2008-01-14 21:47:29 +0000 | [diff] [blame] | 1620 | NumNegativeBits = std::max(NumNegativeBits, |
| 1621 | (unsigned)InitVal.getMinSignedBits()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1622 | |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 1623 | // Keep track of whether every enum element has type int (very commmon). |
| 1624 | if (AllElementsInt) |
| 1625 | AllElementsInt = ECD->getType() == Context.IntTy; |
| 1626 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1627 | ECD->setNextDeclarator(EltList); |
| 1628 | EltList = ECD; |
| 1629 | } |
| 1630 | |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 1631 | // Figure out the type that should be used for this enum. |
| 1632 | // FIXME: Support attribute(packed) on enums and -fshort-enums. |
| 1633 | QualType BestType; |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 1634 | unsigned BestWidth; |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 1635 | |
| 1636 | if (NumNegativeBits) { |
| 1637 | // If there is a negative value, figure out the smallest integer type (of |
| 1638 | // int/long/longlong) that fits. |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 1639 | if (NumNegativeBits <= IntWidth && NumPositiveBits < IntWidth) { |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 1640 | BestType = Context.IntTy; |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 1641 | BestWidth = IntWidth; |
| 1642 | } else { |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1643 | BestWidth = Context.Target.getLongWidth(); |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 1644 | |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 1645 | if (NumNegativeBits <= BestWidth && NumPositiveBits < BestWidth) |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 1646 | BestType = Context.LongTy; |
| 1647 | else { |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1648 | BestWidth = Context.Target.getLongLongWidth(); |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 1649 | |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 1650 | if (NumNegativeBits > BestWidth || NumPositiveBits >= BestWidth) |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 1651 | Diag(Enum->getLocation(), diag::warn_enum_too_large); |
| 1652 | BestType = Context.LongLongTy; |
| 1653 | } |
| 1654 | } |
| 1655 | } else { |
| 1656 | // If there is no negative value, figure out which of uint, ulong, ulonglong |
| 1657 | // fits. |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 1658 | if (NumPositiveBits <= IntWidth) { |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 1659 | BestType = Context.UnsignedIntTy; |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 1660 | BestWidth = IntWidth; |
| 1661 | } else if (NumPositiveBits <= |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1662 | (BestWidth = Context.Target.getLongWidth())) { |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 1663 | BestType = Context.UnsignedLongTy; |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1664 | } else { |
| 1665 | BestWidth = Context.Target.getLongLongWidth(); |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 1666 | assert(NumPositiveBits <= BestWidth && |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 1667 | "How could an initializer get larger than ULL?"); |
| 1668 | BestType = Context.UnsignedLongLongTy; |
| 1669 | } |
| 1670 | } |
| 1671 | |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 1672 | // Loop over all of the enumerator constants, changing their types to match |
| 1673 | // the type of the enum if needed. |
| 1674 | for (unsigned i = 0; i != NumElements; ++i) { |
| 1675 | EnumConstantDecl *ECD = |
| 1676 | cast_or_null<EnumConstantDecl>(static_cast<Decl*>(Elements[i])); |
| 1677 | if (!ECD) continue; // Already issued a diagnostic. |
| 1678 | |
| 1679 | // Standard C says the enumerators have int type, but we allow, as an |
| 1680 | // extension, the enumerators to be larger than int size. If each |
| 1681 | // enumerator value fits in an int, type it as an int, otherwise type it the |
| 1682 | // same as the enumerator decl itself. This means that in "enum { X = 1U }" |
| 1683 | // that X has type 'int', not 'unsigned'. |
Chris Lattner | a73349d | 2008-02-26 00:33:57 +0000 | [diff] [blame] | 1684 | if (ECD->getType() == Context.IntTy) { |
| 1685 | // Make sure the init value is signed. |
| 1686 | llvm::APSInt IV = ECD->getInitVal(); |
| 1687 | IV.setIsSigned(true); |
| 1688 | ECD->setInitVal(IV); |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 1689 | continue; // Already int type. |
Chris Lattner | a73349d | 2008-02-26 00:33:57 +0000 | [diff] [blame] | 1690 | } |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 1691 | |
| 1692 | // Determine whether the value fits into an int. |
| 1693 | llvm::APSInt InitVal = ECD->getInitVal(); |
| 1694 | bool FitsInInt; |
| 1695 | if (InitVal.isUnsigned() || !InitVal.isNegative()) |
| 1696 | FitsInInt = InitVal.getActiveBits() < IntWidth; |
| 1697 | else |
| 1698 | FitsInInt = InitVal.getMinSignedBits() <= IntWidth; |
| 1699 | |
| 1700 | // If it fits into an integer type, force it. Otherwise force it to match |
| 1701 | // the enum decl type. |
| 1702 | QualType NewTy; |
| 1703 | unsigned NewWidth; |
| 1704 | bool NewSign; |
| 1705 | if (FitsInInt) { |
| 1706 | NewTy = Context.IntTy; |
| 1707 | NewWidth = IntWidth; |
| 1708 | NewSign = true; |
| 1709 | } else if (ECD->getType() == BestType) { |
| 1710 | // Already the right type! |
| 1711 | continue; |
| 1712 | } else { |
| 1713 | NewTy = BestType; |
| 1714 | NewWidth = BestWidth; |
| 1715 | NewSign = BestType->isSignedIntegerType(); |
| 1716 | } |
| 1717 | |
| 1718 | // Adjust the APSInt value. |
| 1719 | InitVal.extOrTrunc(NewWidth); |
| 1720 | InitVal.setIsSigned(NewSign); |
| 1721 | ECD->setInitVal(InitVal); |
| 1722 | |
| 1723 | // Adjust the Expr initializer and type. |
| 1724 | ECD->setInitExpr(new ImplicitCastExpr(NewTy, ECD->getInitExpr())); |
| 1725 | ECD->setType(NewTy); |
| 1726 | } |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 1727 | |
Chris Lattner | e00b18c | 2007-08-28 18:24:31 +0000 | [diff] [blame] | 1728 | Enum->defineElements(EltList, BestType); |
Chris Lattner | e1e7985 | 2008-02-06 00:51:33 +0000 | [diff] [blame] | 1729 | Consumer.HandleTagDeclDefinition(Enum); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1730 | } |
| 1731 | |
Anders Carlsson | dfab6cb | 2008-02-08 00:33:21 +0000 | [diff] [blame] | 1732 | Sema::DeclTy *Sema::ActOnFileScopeAsmDecl(SourceLocation Loc, |
| 1733 | ExprTy *expr) { |
| 1734 | StringLiteral *AsmString = cast<StringLiteral>((Expr*)expr); |
| 1735 | |
| 1736 | return new FileScopeAsmDecl(Loc, AsmString); |
| 1737 | } |
| 1738 | |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 1739 | Sema::DeclTy* Sema::ActOnLinkageSpec(SourceLocation Loc, |
Chris Lattner | c81c814 | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 1740 | SourceLocation LBrace, |
| 1741 | SourceLocation RBrace, |
| 1742 | const char *Lang, |
| 1743 | unsigned StrSize, |
| 1744 | DeclTy *D) { |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 1745 | LinkageSpecDecl::LanguageIDs Language; |
| 1746 | Decl *dcl = static_cast<Decl *>(D); |
| 1747 | if (strncmp(Lang, "\"C\"", StrSize) == 0) |
| 1748 | Language = LinkageSpecDecl::lang_c; |
| 1749 | else if (strncmp(Lang, "\"C++\"", StrSize) == 0) |
| 1750 | Language = LinkageSpecDecl::lang_cxx; |
| 1751 | else { |
| 1752 | Diag(Loc, diag::err_bad_language); |
| 1753 | return 0; |
| 1754 | } |
| 1755 | |
| 1756 | // FIXME: Add all the various semantics of linkage specifications |
| 1757 | return new LinkageSpecDecl(Loc, Language, dcl); |
| 1758 | } |
| 1759 | |
Chris Lattner | 74788ba | 2008-02-21 00:48:22 +0000 | [diff] [blame] | 1760 | void Sema::HandleDeclAttribute(Decl *New, AttributeList *Attr) { |
Anders Carlsson | 6ede0ff | 2007-12-19 06:16:30 +0000 | [diff] [blame] | 1761 | |
Chris Lattner | 74788ba | 2008-02-21 00:48:22 +0000 | [diff] [blame] | 1762 | switch (Attr->getKind()) { |
Chris Lattner | 212839c | 2008-02-20 23:17:35 +0000 | [diff] [blame] | 1763 | case AttributeList::AT_vector_size: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1764 | if (ValueDecl *vDecl = dyn_cast<ValueDecl>(New)) { |
Chris Lattner | 74788ba | 2008-02-21 00:48:22 +0000 | [diff] [blame] | 1765 | QualType newType = HandleVectorTypeAttribute(vDecl->getType(), Attr); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1766 | if (!newType.isNull()) // install the new vector type into the decl |
| 1767 | vDecl->setType(newType); |
| 1768 | } |
| 1769 | if (TypedefDecl *tDecl = dyn_cast<TypedefDecl>(New)) { |
| 1770 | QualType newType = HandleVectorTypeAttribute(tDecl->getUnderlyingType(), |
Chris Lattner | 74788ba | 2008-02-21 00:48:22 +0000 | [diff] [blame] | 1771 | Attr); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1772 | if (!newType.isNull()) // install the new vector type into the decl |
| 1773 | tDecl->setUnderlyingType(newType); |
| 1774 | } |
Chris Lattner | 212839c | 2008-02-20 23:17:35 +0000 | [diff] [blame] | 1775 | break; |
| 1776 | case AttributeList::AT_ocu_vector_type: |
Steve Naroff | bea0b34 | 2007-07-29 16:33:31 +0000 | [diff] [blame] | 1777 | if (TypedefDecl *tDecl = dyn_cast<TypedefDecl>(New)) |
Chris Lattner | 74788ba | 2008-02-21 00:48:22 +0000 | [diff] [blame] | 1778 | HandleOCUVectorTypeAttribute(tDecl, Attr); |
Steve Naroff | bea0b34 | 2007-07-29 16:33:31 +0000 | [diff] [blame] | 1779 | else |
Chris Lattner | 74788ba | 2008-02-21 00:48:22 +0000 | [diff] [blame] | 1780 | Diag(Attr->getLoc(), |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1781 | diag::err_typecheck_ocu_vector_not_typedef); |
Chris Lattner | 212839c | 2008-02-20 23:17:35 +0000 | [diff] [blame] | 1782 | break; |
| 1783 | case AttributeList::AT_address_space: |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 1784 | if (TypedefDecl *tDecl = dyn_cast<TypedefDecl>(New)) { |
| 1785 | QualType newType = HandleAddressSpaceTypeAttribute( |
| 1786 | tDecl->getUnderlyingType(), |
Chris Lattner | 74788ba | 2008-02-21 00:48:22 +0000 | [diff] [blame] | 1787 | Attr); |
| 1788 | tDecl->setUnderlyingType(newType); |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 1789 | } else if (ValueDecl *vDecl = dyn_cast<ValueDecl>(New)) { |
| 1790 | QualType newType = HandleAddressSpaceTypeAttribute(vDecl->getType(), |
Chris Lattner | 74788ba | 2008-02-21 00:48:22 +0000 | [diff] [blame] | 1791 | Attr); |
| 1792 | // install the new addr spaced type into the decl |
| 1793 | vDecl->setType(newType); |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 1794 | } |
Chris Lattner | 212839c | 2008-02-20 23:17:35 +0000 | [diff] [blame] | 1795 | break; |
Chris Lattner | 7e669b2 | 2008-02-29 16:48:43 +0000 | [diff] [blame] | 1796 | case AttributeList::AT_deprecated: |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 1797 | HandleDeprecatedAttribute(New, Attr); |
| 1798 | break; |
| 1799 | case AttributeList::AT_visibility: |
| 1800 | HandleVisibilityAttribute(New, Attr); |
| 1801 | break; |
| 1802 | case AttributeList::AT_weak: |
| 1803 | HandleWeakAttribute(New, Attr); |
| 1804 | break; |
| 1805 | case AttributeList::AT_dllimport: |
| 1806 | HandleDLLImportAttribute(New, Attr); |
| 1807 | break; |
| 1808 | case AttributeList::AT_dllexport: |
| 1809 | HandleDLLExportAttribute(New, Attr); |
| 1810 | break; |
| 1811 | case AttributeList::AT_nothrow: |
| 1812 | HandleNothrowAttribute(New, Attr); |
Chris Lattner | 7e669b2 | 2008-02-29 16:48:43 +0000 | [diff] [blame] | 1813 | break; |
Nate Begeman | 440b456 | 2008-03-07 20:04:22 +0000 | [diff] [blame] | 1814 | case AttributeList::AT_stdcall: |
| 1815 | HandleStdCallAttribute(New, Attr); |
| 1816 | break; |
| 1817 | case AttributeList::AT_fastcall: |
| 1818 | HandleFastCallAttribute(New, Attr); |
| 1819 | break; |
Chris Lattner | 212839c | 2008-02-20 23:17:35 +0000 | [diff] [blame] | 1820 | case AttributeList::AT_aligned: |
Chris Lattner | 74788ba | 2008-02-21 00:48:22 +0000 | [diff] [blame] | 1821 | HandleAlignedAttribute(New, Attr); |
Chris Lattner | 212839c | 2008-02-20 23:17:35 +0000 | [diff] [blame] | 1822 | break; |
| 1823 | case AttributeList::AT_packed: |
Chris Lattner | 74788ba | 2008-02-21 00:48:22 +0000 | [diff] [blame] | 1824 | HandlePackedAttribute(New, Attr); |
Chris Lattner | 212839c | 2008-02-20 23:17:35 +0000 | [diff] [blame] | 1825 | break; |
Nate Begeman | c398f0b | 2008-02-21 19:30:49 +0000 | [diff] [blame] | 1826 | case AttributeList::AT_annotate: |
| 1827 | HandleAnnotateAttribute(New, Attr); |
| 1828 | break; |
Ted Kremenek | aecb383 | 2008-02-27 20:43:06 +0000 | [diff] [blame] | 1829 | case AttributeList::AT_noreturn: |
| 1830 | HandleNoReturnAttribute(New, Attr); |
| 1831 | break; |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 1832 | case AttributeList::AT_format: |
| 1833 | HandleFormatAttribute(New, Attr); |
| 1834 | break; |
Chris Lattner | 212839c | 2008-02-20 23:17:35 +0000 | [diff] [blame] | 1835 | default: |
Chris Lattner | 7e669b2 | 2008-02-29 16:48:43 +0000 | [diff] [blame] | 1836 | #if 0 |
| 1837 | // TODO: when we have the full set of attributes, warn about unknown ones. |
| 1838 | Diag(Attr->getLoc(), diag::warn_attribute_ignored, |
| 1839 | Attr->getName()->getName()); |
| 1840 | #endif |
Chris Lattner | 212839c | 2008-02-20 23:17:35 +0000 | [diff] [blame] | 1841 | break; |
| 1842 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1843 | } |
| 1844 | |
| 1845 | void Sema::HandleDeclAttributes(Decl *New, AttributeList *declspec_prefix, |
| 1846 | AttributeList *declarator_postfix) { |
| 1847 | while (declspec_prefix) { |
| 1848 | HandleDeclAttribute(New, declspec_prefix); |
| 1849 | declspec_prefix = declspec_prefix->getNext(); |
| 1850 | } |
| 1851 | while (declarator_postfix) { |
| 1852 | HandleDeclAttribute(New, declarator_postfix); |
| 1853 | declarator_postfix = declarator_postfix->getNext(); |
| 1854 | } |
| 1855 | } |
| 1856 | |
Steve Naroff | bea0b34 | 2007-07-29 16:33:31 +0000 | [diff] [blame] | 1857 | void Sema::HandleOCUVectorTypeAttribute(TypedefDecl *tDecl, |
| 1858 | AttributeList *rawAttr) { |
| 1859 | QualType curType = tDecl->getUnderlyingType(); |
Anders Carlsson | 78aaae9 | 2007-12-19 07:19:40 +0000 | [diff] [blame] | 1860 | // check the attribute arguments. |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1861 | if (rawAttr->getNumArgs() != 1) { |
Chris Lattner | 2070d80 | 2008-02-20 23:25:22 +0000 | [diff] [blame] | 1862 | Diag(rawAttr->getLoc(), diag::err_attribute_wrong_number_arguments, |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1863 | std::string("1")); |
Steve Naroff | bea0b34 | 2007-07-29 16:33:31 +0000 | [diff] [blame] | 1864 | return; |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1865 | } |
| 1866 | Expr *sizeExpr = static_cast<Expr *>(rawAttr->getArg(0)); |
| 1867 | llvm::APSInt vecSize(32); |
| 1868 | if (!sizeExpr->isIntegerConstantExpr(vecSize, Context)) { |
Chris Lattner | 2070d80 | 2008-02-20 23:25:22 +0000 | [diff] [blame] | 1869 | Diag(rawAttr->getLoc(), diag::err_attribute_argument_not_int, |
Anders Carlsson | 042c4e7 | 2008-02-16 19:51:27 +0000 | [diff] [blame] | 1870 | "ocu_vector_type", sizeExpr->getSourceRange()); |
Steve Naroff | bea0b34 | 2007-07-29 16:33:31 +0000 | [diff] [blame] | 1871 | return; |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1872 | } |
| 1873 | // unlike gcc's vector_size attribute, we do not allow vectors to be defined |
| 1874 | // in conjunction with complex types (pointers, arrays, functions, etc.). |
| 1875 | Type *canonType = curType.getCanonicalType().getTypePtr(); |
| 1876 | if (!(canonType->isIntegerType() || canonType->isRealFloatingType())) { |
Chris Lattner | 2070d80 | 2008-02-20 23:25:22 +0000 | [diff] [blame] | 1877 | Diag(rawAttr->getLoc(), diag::err_attribute_invalid_vector_type, |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1878 | curType.getCanonicalType().getAsString()); |
Steve Naroff | bea0b34 | 2007-07-29 16:33:31 +0000 | [diff] [blame] | 1879 | return; |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1880 | } |
| 1881 | // unlike gcc's vector_size attribute, the size is specified as the |
| 1882 | // number of elements, not the number of bytes. |
Chris Lattner | 701e5eb | 2007-09-04 02:45:27 +0000 | [diff] [blame] | 1883 | unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue()); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1884 | |
| 1885 | if (vectorSize == 0) { |
Chris Lattner | 2070d80 | 2008-02-20 23:25:22 +0000 | [diff] [blame] | 1886 | Diag(rawAttr->getLoc(), diag::err_attribute_zero_size, |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1887 | sizeExpr->getSourceRange()); |
Steve Naroff | bea0b34 | 2007-07-29 16:33:31 +0000 | [diff] [blame] | 1888 | return; |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1889 | } |
Steve Naroff | bea0b34 | 2007-07-29 16:33:31 +0000 | [diff] [blame] | 1890 | // Instantiate/Install the vector type, the number of elements is > 0. |
| 1891 | tDecl->setUnderlyingType(Context.getOCUVectorType(curType, vectorSize)); |
| 1892 | // Remember this typedef decl, we will need it later for diagnostics. |
| 1893 | OCUVectorDecls.push_back(tDecl); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1894 | } |
| 1895 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1896 | QualType Sema::HandleVectorTypeAttribute(QualType curType, |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1897 | AttributeList *rawAttr) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1898 | // check the attribute arugments. |
| 1899 | if (rawAttr->getNumArgs() != 1) { |
Chris Lattner | 2070d80 | 2008-02-20 23:25:22 +0000 | [diff] [blame] | 1900 | Diag(rawAttr->getLoc(), diag::err_attribute_wrong_number_arguments, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1901 | std::string("1")); |
| 1902 | return QualType(); |
| 1903 | } |
| 1904 | Expr *sizeExpr = static_cast<Expr *>(rawAttr->getArg(0)); |
| 1905 | llvm::APSInt vecSize(32); |
Chris Lattner | 590b664 | 2007-07-15 23:26:56 +0000 | [diff] [blame] | 1906 | if (!sizeExpr->isIntegerConstantExpr(vecSize, Context)) { |
Chris Lattner | 2070d80 | 2008-02-20 23:25:22 +0000 | [diff] [blame] | 1907 | Diag(rawAttr->getLoc(), diag::err_attribute_argument_not_int, |
Anders Carlsson | 042c4e7 | 2008-02-16 19:51:27 +0000 | [diff] [blame] | 1908 | "vector_size", sizeExpr->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1909 | return QualType(); |
| 1910 | } |
| 1911 | // navigate to the base type - we need to provide for vector pointers, |
| 1912 | // vector arrays, and functions returning vectors. |
| 1913 | Type *canonType = curType.getCanonicalType().getTypePtr(); |
| 1914 | |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1915 | if (canonType->isPointerType() || canonType->isArrayType() || |
| 1916 | canonType->isFunctionType()) { |
Chris Lattner | 54b263b | 2007-12-19 05:38:06 +0000 | [diff] [blame] | 1917 | assert(0 && "HandleVector(): Complex type construction unimplemented"); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1918 | /* FIXME: rebuild the type from the inside out, vectorizing the inner type. |
| 1919 | do { |
| 1920 | if (PointerType *PT = dyn_cast<PointerType>(canonType)) |
| 1921 | canonType = PT->getPointeeType().getTypePtr(); |
| 1922 | else if (ArrayType *AT = dyn_cast<ArrayType>(canonType)) |
| 1923 | canonType = AT->getElementType().getTypePtr(); |
| 1924 | else if (FunctionType *FT = dyn_cast<FunctionType>(canonType)) |
| 1925 | canonType = FT->getResultType().getTypePtr(); |
| 1926 | } while (canonType->isPointerType() || canonType->isArrayType() || |
| 1927 | canonType->isFunctionType()); |
| 1928 | */ |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1929 | } |
| 1930 | // the base type must be integer or float. |
| 1931 | if (!(canonType->isIntegerType() || canonType->isRealFloatingType())) { |
Chris Lattner | 2070d80 | 2008-02-20 23:25:22 +0000 | [diff] [blame] | 1932 | Diag(rawAttr->getLoc(), diag::err_attribute_invalid_vector_type, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1933 | curType.getCanonicalType().getAsString()); |
| 1934 | return QualType(); |
| 1935 | } |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1936 | unsigned typeSize = static_cast<unsigned>(Context.getTypeSize(curType)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1937 | // vecSize is specified in bytes - convert to bits. |
Chris Lattner | 701e5eb | 2007-09-04 02:45:27 +0000 | [diff] [blame] | 1938 | unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue() * 8); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1939 | |
| 1940 | // the vector size needs to be an integral multiple of the type size. |
| 1941 | if (vectorSize % typeSize) { |
Chris Lattner | 2070d80 | 2008-02-20 23:25:22 +0000 | [diff] [blame] | 1942 | Diag(rawAttr->getLoc(), diag::err_attribute_invalid_size, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1943 | sizeExpr->getSourceRange()); |
| 1944 | return QualType(); |
| 1945 | } |
| 1946 | if (vectorSize == 0) { |
Chris Lattner | 2070d80 | 2008-02-20 23:25:22 +0000 | [diff] [blame] | 1947 | Diag(rawAttr->getLoc(), diag::err_attribute_zero_size, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1948 | sizeExpr->getSourceRange()); |
| 1949 | return QualType(); |
| 1950 | } |
Nate Begeman | c398f0b | 2008-02-21 19:30:49 +0000 | [diff] [blame] | 1951 | // Instantiate the vector type, the number of elements is > 0, and not |
| 1952 | // required to be a power of 2, unlike GCC. |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1953 | return Context.getVectorType(curType, vectorSize/typeSize); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1954 | } |
| 1955 | |
Chris Lattner | 2070d80 | 2008-02-20 23:25:22 +0000 | [diff] [blame] | 1956 | void Sema::HandlePackedAttribute(Decl *d, AttributeList *rawAttr) { |
Anders Carlsson | ad14806 | 2008-02-16 00:29:18 +0000 | [diff] [blame] | 1957 | // check the attribute arguments. |
| 1958 | if (rawAttr->getNumArgs() > 0) { |
Chris Lattner | 2070d80 | 2008-02-20 23:25:22 +0000 | [diff] [blame] | 1959 | Diag(rawAttr->getLoc(), diag::err_attribute_wrong_number_arguments, |
Anders Carlsson | ad14806 | 2008-02-16 00:29:18 +0000 | [diff] [blame] | 1960 | std::string("0")); |
| 1961 | return; |
| 1962 | } |
| 1963 | |
| 1964 | if (TagDecl *TD = dyn_cast<TagDecl>(d)) |
| 1965 | TD->addAttr(new PackedAttr); |
| 1966 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(d)) { |
| 1967 | // If the alignment is less than or equal to 8 bits, the packed attribute |
| 1968 | // has no effect. |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1969 | if (Context.getTypeAlign(FD->getType()) <= 8) |
Chris Lattner | 2070d80 | 2008-02-20 23:25:22 +0000 | [diff] [blame] | 1970 | Diag(rawAttr->getLoc(), |
Anders Carlsson | ad14806 | 2008-02-16 00:29:18 +0000 | [diff] [blame] | 1971 | diag::warn_attribute_ignored_for_field_of_type, |
Chris Lattner | 2070d80 | 2008-02-20 23:25:22 +0000 | [diff] [blame] | 1972 | rawAttr->getName()->getName(), FD->getType().getAsString()); |
Anders Carlsson | ad14806 | 2008-02-16 00:29:18 +0000 | [diff] [blame] | 1973 | else |
Anders Carlsson | 425a609 | 2008-02-16 00:39:40 +0000 | [diff] [blame] | 1974 | FD->addAttr(new PackedAttr); |
Anders Carlsson | ad14806 | 2008-02-16 00:29:18 +0000 | [diff] [blame] | 1975 | } else |
Chris Lattner | 2070d80 | 2008-02-20 23:25:22 +0000 | [diff] [blame] | 1976 | Diag(rawAttr->getLoc(), diag::warn_attribute_ignored, |
| 1977 | rawAttr->getName()->getName()); |
Anders Carlsson | ad14806 | 2008-02-16 00:29:18 +0000 | [diff] [blame] | 1978 | } |
Nate Begeman | c398f0b | 2008-02-21 19:30:49 +0000 | [diff] [blame] | 1979 | |
Ted Kremenek | aecb383 | 2008-02-27 20:43:06 +0000 | [diff] [blame] | 1980 | void Sema::HandleNoReturnAttribute(Decl *d, AttributeList *rawAttr) { |
| 1981 | // check the attribute arguments. |
| 1982 | if (rawAttr->getNumArgs() != 0) { |
| 1983 | Diag(rawAttr->getLoc(), diag::err_attribute_wrong_number_arguments, |
| 1984 | std::string("0")); |
| 1985 | return; |
| 1986 | } |
| 1987 | |
Ted Kremenek | 3465fb3 | 2008-03-03 16:52:27 +0000 | [diff] [blame] | 1988 | FunctionDecl *Fn = dyn_cast<FunctionDecl>(d); |
| 1989 | |
| 1990 | if (!Fn) { |
| 1991 | Diag(rawAttr->getLoc(), diag::warn_attribute_wrong_decl_type, |
| 1992 | "noreturn", "function"); |
| 1993 | return; |
| 1994 | } |
| 1995 | |
Ted Kremenek | aecb383 | 2008-02-27 20:43:06 +0000 | [diff] [blame] | 1996 | d->addAttr(new NoReturnAttr()); |
| 1997 | } |
| 1998 | |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 1999 | void Sema::HandleDeprecatedAttribute(Decl *d, AttributeList *rawAttr) { |
| 2000 | // check the attribute arguments. |
| 2001 | if (rawAttr->getNumArgs() != 0) { |
| 2002 | Diag(rawAttr->getLoc(), diag::err_attribute_wrong_number_arguments, |
| 2003 | std::string("0")); |
| 2004 | return; |
| 2005 | } |
| 2006 | |
| 2007 | d->addAttr(new DeprecatedAttr()); |
| 2008 | } |
| 2009 | |
| 2010 | void Sema::HandleVisibilityAttribute(Decl *d, AttributeList *rawAttr) { |
| 2011 | // check the attribute arguments. |
Chris Lattner | 7b937ae | 2008-03-04 18:08:48 +0000 | [diff] [blame] | 2012 | if (rawAttr->getNumArgs() != 1) { |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 2013 | Diag(rawAttr->getLoc(), diag::err_attribute_wrong_number_arguments, |
| 2014 | std::string("1")); |
| 2015 | return; |
| 2016 | } |
| 2017 | |
Chris Lattner | 7b937ae | 2008-03-04 18:08:48 +0000 | [diff] [blame] | 2018 | Expr *Arg = static_cast<Expr*>(rawAttr->getArg(0)); |
| 2019 | Arg = Arg->IgnoreParenCasts(); |
| 2020 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
| 2021 | |
| 2022 | if (Str == 0 || Str->isWide()) { |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 2023 | Diag(rawAttr->getLoc(), diag::err_attribute_argument_n_not_string, |
Chris Lattner | 7b937ae | 2008-03-04 18:08:48 +0000 | [diff] [blame] | 2024 | "visibility", std::string("1")); |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 2025 | return; |
| 2026 | } |
| 2027 | |
Chris Lattner | 7b937ae | 2008-03-04 18:08:48 +0000 | [diff] [blame] | 2028 | const char *TypeStr = Str->getStrData(); |
| 2029 | unsigned TypeLen = Str->getByteLength(); |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 2030 | llvm::GlobalValue::VisibilityTypes type; |
| 2031 | |
Chris Lattner | 7b937ae | 2008-03-04 18:08:48 +0000 | [diff] [blame] | 2032 | if (TypeLen == 7 && !memcmp(TypeStr, "default", 7)) |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 2033 | type = llvm::GlobalValue::DefaultVisibility; |
Chris Lattner | 7b937ae | 2008-03-04 18:08:48 +0000 | [diff] [blame] | 2034 | else if (TypeLen == 6 && !memcmp(TypeStr, "hidden", 6)) |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 2035 | type = llvm::GlobalValue::HiddenVisibility; |
Chris Lattner | 7b937ae | 2008-03-04 18:08:48 +0000 | [diff] [blame] | 2036 | else if (TypeLen == 8 && !memcmp(TypeStr, "internal", 8)) |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 2037 | type = llvm::GlobalValue::HiddenVisibility; // FIXME |
Chris Lattner | 7b937ae | 2008-03-04 18:08:48 +0000 | [diff] [blame] | 2038 | else if (TypeLen == 9 && !memcmp(TypeStr, "protected", 9)) |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 2039 | type = llvm::GlobalValue::ProtectedVisibility; |
| 2040 | else { |
| 2041 | Diag(rawAttr->getLoc(), diag::warn_attribute_type_not_supported, |
Chris Lattner | 7b937ae | 2008-03-04 18:08:48 +0000 | [diff] [blame] | 2042 | "visibility", TypeStr); |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 2043 | return; |
| 2044 | } |
| 2045 | |
| 2046 | d->addAttr(new VisibilityAttr(type)); |
| 2047 | } |
| 2048 | |
| 2049 | void Sema::HandleWeakAttribute(Decl *d, AttributeList *rawAttr) { |
| 2050 | // check the attribute arguments. |
| 2051 | if (rawAttr->getNumArgs() != 0) { |
| 2052 | Diag(rawAttr->getLoc(), diag::err_attribute_wrong_number_arguments, |
| 2053 | std::string("0")); |
| 2054 | return; |
| 2055 | } |
| 2056 | |
| 2057 | d->addAttr(new WeakAttr()); |
| 2058 | } |
| 2059 | |
| 2060 | void Sema::HandleDLLImportAttribute(Decl *d, AttributeList *rawAttr) { |
| 2061 | // check the attribute arguments. |
| 2062 | if (rawAttr->getNumArgs() != 0) { |
| 2063 | Diag(rawAttr->getLoc(), diag::err_attribute_wrong_number_arguments, |
| 2064 | std::string("0")); |
| 2065 | return; |
| 2066 | } |
| 2067 | |
| 2068 | d->addAttr(new DLLImportAttr()); |
| 2069 | } |
| 2070 | |
| 2071 | void Sema::HandleDLLExportAttribute(Decl *d, AttributeList *rawAttr) { |
| 2072 | // check the attribute arguments. |
| 2073 | if (rawAttr->getNumArgs() != 0) { |
| 2074 | Diag(rawAttr->getLoc(), diag::err_attribute_wrong_number_arguments, |
| 2075 | std::string("0")); |
| 2076 | return; |
| 2077 | } |
| 2078 | |
| 2079 | d->addAttr(new DLLExportAttr()); |
| 2080 | } |
| 2081 | |
Nate Begeman | 440b456 | 2008-03-07 20:04:22 +0000 | [diff] [blame] | 2082 | void Sema::HandleStdCallAttribute(Decl *d, AttributeList *rawAttr) { |
| 2083 | // check the attribute arguments. |
| 2084 | if (rawAttr->getNumArgs() != 0) { |
| 2085 | Diag(rawAttr->getLoc(), diag::err_attribute_wrong_number_arguments, |
| 2086 | std::string("0")); |
| 2087 | return; |
| 2088 | } |
| 2089 | |
| 2090 | d->addAttr(new StdCallAttr()); |
| 2091 | } |
| 2092 | |
| 2093 | void Sema::HandleFastCallAttribute(Decl *d, AttributeList *rawAttr) { |
| 2094 | // check the attribute arguments. |
| 2095 | if (rawAttr->getNumArgs() != 0) { |
| 2096 | Diag(rawAttr->getLoc(), diag::err_attribute_wrong_number_arguments, |
| 2097 | std::string("0")); |
| 2098 | return; |
| 2099 | } |
| 2100 | |
| 2101 | d->addAttr(new FastCallAttr()); |
| 2102 | } |
| 2103 | |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 2104 | void Sema::HandleNothrowAttribute(Decl *d, AttributeList *rawAttr) { |
| 2105 | // check the attribute arguments. |
| 2106 | if (rawAttr->getNumArgs() != 0) { |
| 2107 | Diag(rawAttr->getLoc(), diag::err_attribute_wrong_number_arguments, |
| 2108 | std::string("0")); |
| 2109 | return; |
| 2110 | } |
| 2111 | |
| 2112 | d->addAttr(new NoThrowAttr()); |
| 2113 | } |
| 2114 | |
Ted Kremenek | aa8f976 | 2008-03-07 18:43:49 +0000 | [diff] [blame] | 2115 | /// Handle __attribute__((format(type,idx,firstarg))) attributes |
| 2116 | /// based on http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 2117 | void Sema::HandleFormatAttribute(Decl *d, AttributeList *rawAttr) { |
| 2118 | |
| 2119 | if (!rawAttr->getParameterName()) { |
| 2120 | Diag(rawAttr->getLoc(), diag::err_attribute_argument_n_not_string, |
| 2121 | "format", std::string("1")); |
| 2122 | return; |
| 2123 | } |
| 2124 | |
| 2125 | if (rawAttr->getNumArgs() != 2) { |
| 2126 | Diag(rawAttr->getLoc(), diag::err_attribute_wrong_number_arguments, |
| 2127 | std::string("3")); |
| 2128 | return; |
| 2129 | } |
| 2130 | |
| 2131 | FunctionDecl *Fn = dyn_cast<FunctionDecl>(d); |
| 2132 | if (!Fn) { |
| 2133 | Diag(rawAttr->getLoc(), diag::warn_attribute_wrong_decl_type, |
| 2134 | "format", "function"); |
| 2135 | return; |
| 2136 | } |
| 2137 | |
Ted Kremenek | aa8f976 | 2008-03-07 18:43:49 +0000 | [diff] [blame] | 2138 | const FunctionTypeProto *proto = |
| 2139 | dyn_cast<FunctionTypeProto>(Fn->getType()->getAsFunctionType()); |
| 2140 | if (!proto) |
| 2141 | return; |
| 2142 | |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 2143 | // FIXME: in C++ the implicit 'this' function parameter also counts. |
Ted Kremenek | aa8f976 | 2008-03-07 18:43:49 +0000 | [diff] [blame] | 2144 | // this is needed in order to be compatible with GCC |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 2145 | // the index must start in 1 and the limit is numargs+1 |
Ted Kremenek | aa8f976 | 2008-03-07 18:43:49 +0000 | [diff] [blame] | 2146 | unsigned NumArgs = Fn->getNumParams(); |
| 2147 | unsigned FirstIdx = 1; |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 2148 | |
| 2149 | const char *Format = rawAttr->getParameterName()->getName(); |
| 2150 | unsigned FormatLen = rawAttr->getParameterName()->getLength(); |
| 2151 | |
| 2152 | // Normalize the argument, __foo__ becomes foo. |
| 2153 | if (FormatLen > 4 && Format[0] == '_' && Format[1] == '_' && |
| 2154 | Format[FormatLen - 2] == '_' && Format[FormatLen - 1] == '_') { |
| 2155 | Format += 2; |
| 2156 | FormatLen -= 4; |
| 2157 | } |
| 2158 | |
| 2159 | if (!((FormatLen == 5 && !memcmp(Format, "scanf", 5)) |
| 2160 | || (FormatLen == 6 && !memcmp(Format, "printf", 6)) |
| 2161 | || (FormatLen == 7 && !memcmp(Format, "strfmon", 7)) |
| 2162 | || (FormatLen == 8 && !memcmp(Format, "strftime", 8)))) { |
| 2163 | Diag(rawAttr->getLoc(), diag::warn_attribute_type_not_supported, |
| 2164 | "format", rawAttr->getParameterName()->getName()); |
| 2165 | return; |
| 2166 | } |
| 2167 | |
Ted Kremenek | aa8f976 | 2008-03-07 18:43:49 +0000 | [diff] [blame] | 2168 | // checks for the 2nd argument |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 2169 | Expr *IdxExpr = static_cast<Expr *>(rawAttr->getArg(0)); |
Ted Kremenek | aa8f976 | 2008-03-07 18:43:49 +0000 | [diff] [blame] | 2170 | llvm::APSInt Idx(Context.getTypeSize(IdxExpr->getType())); |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 2171 | if (!IdxExpr->isIntegerConstantExpr(Idx, Context)) { |
| 2172 | Diag(rawAttr->getLoc(), diag::err_attribute_argument_n_not_int, |
| 2173 | "format", std::string("2"), IdxExpr->getSourceRange()); |
| 2174 | return; |
| 2175 | } |
| 2176 | |
Ted Kremenek | aa8f976 | 2008-03-07 18:43:49 +0000 | [diff] [blame] | 2177 | if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) { |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 2178 | Diag(rawAttr->getLoc(), diag::err_attribute_argument_out_of_bounds, |
| 2179 | "format", std::string("2"), IdxExpr->getSourceRange()); |
| 2180 | return; |
| 2181 | } |
| 2182 | |
Ted Kremenek | aa8f976 | 2008-03-07 18:43:49 +0000 | [diff] [blame] | 2183 | // make sure the format string is really a string |
| 2184 | QualType Ty = proto->getArgType(Idx.getZExtValue()-1); |
| 2185 | if (!Ty->isPointerType() || |
| 2186 | !Ty->getAsPointerType()->getPointeeType()->isCharType()) { |
| 2187 | Diag(rawAttr->getLoc(), diag::err_format_attribute_not_string, |
| 2188 | IdxExpr->getSourceRange()); |
| 2189 | return; |
| 2190 | } |
| 2191 | |
| 2192 | |
| 2193 | // check the 3rd argument |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 2194 | Expr *FirstArgExpr = static_cast<Expr *>(rawAttr->getArg(1)); |
Ted Kremenek | aa8f976 | 2008-03-07 18:43:49 +0000 | [diff] [blame] | 2195 | llvm::APSInt FirstArg(Context.getTypeSize(FirstArgExpr->getType())); |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 2196 | if (!FirstArgExpr->isIntegerConstantExpr(FirstArg, Context)) { |
| 2197 | Diag(rawAttr->getLoc(), diag::err_attribute_argument_n_not_int, |
| 2198 | "format", std::string("3"), FirstArgExpr->getSourceRange()); |
| 2199 | return; |
| 2200 | } |
| 2201 | |
Ted Kremenek | aa8f976 | 2008-03-07 18:43:49 +0000 | [diff] [blame] | 2202 | // check if the function is variadic if the 3rd argument non-zero |
| 2203 | if (FirstArg != 0) { |
| 2204 | if (proto->isVariadic()) { |
| 2205 | ++NumArgs; // +1 for ... |
| 2206 | } else { |
| 2207 | Diag(d->getLocation(), diag::err_format_attribute_requires_variadic); |
| 2208 | return; |
| 2209 | } |
| 2210 | } |
| 2211 | |
| 2212 | // strftime requires FirstArg to be 0 because it doesn't read from any variable |
| 2213 | // the input is just the current time + the format string |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 2214 | if (FormatLen == 8 && !memcmp(Format, "strftime", 8)) { |
Ted Kremenek | aa8f976 | 2008-03-07 18:43:49 +0000 | [diff] [blame] | 2215 | if (FirstArg != 0) { |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 2216 | Diag(rawAttr->getLoc(), diag::err_format_strftime_third_parameter, |
| 2217 | FirstArgExpr->getSourceRange()); |
| 2218 | return; |
| 2219 | } |
Ted Kremenek | aa8f976 | 2008-03-07 18:43:49 +0000 | [diff] [blame] | 2220 | // if 0 it disables parameter checking (to use with e.g. va_list) |
| 2221 | } else if (FirstArg != 0 && FirstArg != NumArgs) { |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 2222 | Diag(rawAttr->getLoc(), diag::err_attribute_argument_out_of_bounds, |
| 2223 | "format", std::string("3"), FirstArgExpr->getSourceRange()); |
| 2224 | return; |
| 2225 | } |
| 2226 | |
| 2227 | d->addAttr(new FormatAttr(std::string(Format, FormatLen), |
| 2228 | Idx.getZExtValue(), FirstArg.getZExtValue())); |
| 2229 | } |
| 2230 | |
Nate Begeman | c398f0b | 2008-02-21 19:30:49 +0000 | [diff] [blame] | 2231 | void Sema::HandleAnnotateAttribute(Decl *d, AttributeList *rawAttr) { |
| 2232 | // check the attribute arguments. |
| 2233 | if (rawAttr->getNumArgs() != 1) { |
| 2234 | Diag(rawAttr->getLoc(), diag::err_attribute_wrong_number_arguments, |
| 2235 | std::string("1")); |
| 2236 | return; |
| 2237 | } |
| 2238 | Expr *argExpr = static_cast<Expr *>(rawAttr->getArg(0)); |
| 2239 | StringLiteral *SE = dyn_cast<StringLiteral>(argExpr); |
Anders Carlsson | ad14806 | 2008-02-16 00:29:18 +0000 | [diff] [blame] | 2240 | |
Nate Begeman | c398f0b | 2008-02-21 19:30:49 +0000 | [diff] [blame] | 2241 | // Make sure that there is a string literal as the annotation's single |
| 2242 | // argument. |
| 2243 | if (!SE) { |
| 2244 | Diag(rawAttr->getLoc(), diag::err_attribute_annotate_no_string); |
| 2245 | return; |
| 2246 | } |
| 2247 | d->addAttr(new AnnotateAttr(std::string(SE->getStrData(), |
| 2248 | SE->getByteLength()))); |
| 2249 | } |
| 2250 | |
Anders Carlsson | 78aaae9 | 2007-12-19 07:19:40 +0000 | [diff] [blame] | 2251 | void Sema::HandleAlignedAttribute(Decl *d, AttributeList *rawAttr) |
| 2252 | { |
| 2253 | // check the attribute arguments. |
Eli Friedman | 4ca0867 | 2008-01-30 17:38:42 +0000 | [diff] [blame] | 2254 | if (rawAttr->getNumArgs() > 1) { |
Chris Lattner | 2070d80 | 2008-02-20 23:25:22 +0000 | [diff] [blame] | 2255 | Diag(rawAttr->getLoc(), diag::err_attribute_wrong_number_arguments, |
Anders Carlsson | 78aaae9 | 2007-12-19 07:19:40 +0000 | [diff] [blame] | 2256 | std::string("1")); |
| 2257 | return; |
| 2258 | } |
Eli Friedman | 4ca0867 | 2008-01-30 17:38:42 +0000 | [diff] [blame] | 2259 | |
Anders Carlsson | 042c4e7 | 2008-02-16 19:51:27 +0000 | [diff] [blame] | 2260 | unsigned Align = 0; |
| 2261 | |
| 2262 | if (rawAttr->getNumArgs() == 0) { |
| 2263 | // FIXME: This should be the target specific maximum alignment. |
| 2264 | // (For now we just use 128 bits which is the maximum on X86. |
| 2265 | Align = 128; |
Eli Friedman | 4ca0867 | 2008-01-30 17:38:42 +0000 | [diff] [blame] | 2266 | return; |
Anders Carlsson | 042c4e7 | 2008-02-16 19:51:27 +0000 | [diff] [blame] | 2267 | } else { |
| 2268 | Expr *alignmentExpr = static_cast<Expr *>(rawAttr->getArg(0)); |
| 2269 | llvm::APSInt alignment(32); |
| 2270 | if (!alignmentExpr->isIntegerConstantExpr(alignment, Context)) { |
Chris Lattner | 2070d80 | 2008-02-20 23:25:22 +0000 | [diff] [blame] | 2271 | Diag(rawAttr->getLoc(), diag::err_attribute_argument_not_int, |
Anders Carlsson | 042c4e7 | 2008-02-16 19:51:27 +0000 | [diff] [blame] | 2272 | "aligned", alignmentExpr->getSourceRange()); |
| 2273 | return; |
| 2274 | } |
| 2275 | |
| 2276 | Align = alignment.getZExtValue() * 8; |
| 2277 | } |
Eli Friedman | 4ca0867 | 2008-01-30 17:38:42 +0000 | [diff] [blame] | 2278 | |
Anders Carlsson | 042c4e7 | 2008-02-16 19:51:27 +0000 | [diff] [blame] | 2279 | d->addAttr(new AlignedAttr(Align)); |
Anders Carlsson | 78aaae9 | 2007-12-19 07:19:40 +0000 | [diff] [blame] | 2280 | } |