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