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