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