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