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