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