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