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