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