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