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" |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclObjC.h" |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 19 | #include "clang/AST/ExprCXX.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 20 | #include "clang/Parse/DeclSpec.h" |
Daniel Dunbar | e4858a6 | 2008-08-11 03:45:03 +0000 | [diff] [blame] | 21 | #include "clang/Basic/Diagnostic.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 22 | #include "clang/Basic/TargetInfo.h" |
Steve Naroff | 4c49a6c | 2008-01-30 23:46:05 +0000 | [diff] [blame] | 23 | #include "clang/Basic/SourceManager.h" |
| 24 | // FIXME: layering (ideally, Sema shouldn't be dependent on Lex API's) |
Chris Lattner | e1e7985 | 2008-02-06 00:51:33 +0000 | [diff] [blame] | 25 | #include "clang/Lex/Preprocessor.h" |
Steve Naroff | 4c49a6c | 2008-01-30 23:46:05 +0000 | [diff] [blame] | 26 | #include "clang/Lex/HeaderSearch.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/SmallSet.h" |
Daniel Dunbar | 4cde927 | 2008-10-14 05:35:18 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/StringExtras.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 29 | using namespace clang; |
| 30 | |
Argyrios Kyrtzidis | 39caa08 | 2008-08-01 10:35:27 +0000 | [diff] [blame] | 31 | Sema::TypeTy *Sema::isTypeName(const IdentifierInfo &II, Scope *S) { |
Steve Naroff | b327ce0 | 2008-04-02 14:35:35 +0000 | [diff] [blame] | 32 | Decl *IIDecl = LookupDecl(&II, Decl::IDNS_Ordinary, S, false); |
| 33 | |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 34 | if (IIDecl && (isa<TypedefDecl>(IIDecl) || |
| 35 | isa<ObjCInterfaceDecl>(IIDecl) || |
| 36 | isa<TagDecl>(IIDecl))) |
Fariborz Jahanian | bece4ac | 2007-10-12 16:34:10 +0000 | [diff] [blame] | 37 | return IIDecl; |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 38 | return 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 39 | } |
| 40 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 41 | DeclContext *Sema::getDCParent(DeclContext *DC) { |
| 42 | // If CurContext is a ObjC method, getParent() will return NULL. |
| 43 | if (isa<ObjCMethodDecl>(DC)) |
| 44 | return Context.getTranslationUnitDecl(); |
| 45 | |
| 46 | // A C++ inline method is parsed *after* the topmost class it was declared in |
| 47 | // is fully parsed (it's "complete"). |
| 48 | // The parsing of a C++ inline method happens at the declaration context of |
| 49 | // the topmost (non-nested) class it is declared in. |
| 50 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(DC)) { |
| 51 | assert(isa<CXXRecordDecl>(MD->getParent()) && "C++ method not in Record."); |
| 52 | DC = MD->getParent(); |
| 53 | while (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(DC->getParent())) |
| 54 | DC = RD; |
| 55 | |
| 56 | // Return the declaration context of the topmost class the inline method is |
| 57 | // declared in. |
| 58 | return DC; |
| 59 | } |
| 60 | |
| 61 | return DC->getParent(); |
| 62 | } |
| 63 | |
Chris Lattner | 9fdf9c6 | 2008-04-22 18:39:57 +0000 | [diff] [blame] | 64 | void Sema::PushDeclContext(DeclContext *DC) { |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 65 | assert(getDCParent(DC) == CurContext && |
| 66 | "The next DeclContext should be directly contained in the current one."); |
Chris Lattner | 9fdf9c6 | 2008-04-22 18:39:57 +0000 | [diff] [blame] | 67 | CurContext = DC; |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 70 | void Sema::PopDeclContext() { |
| 71 | assert(CurContext && "DeclContext imbalance!"); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 72 | CurContext = getDCParent(CurContext); |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Argyrios Kyrtzidis | 87f3ff0 | 2008-04-12 00:47:19 +0000 | [diff] [blame] | 75 | /// Add this decl to the scope shadowed decl chains. |
| 76 | void Sema::PushOnScopeChains(NamedDecl *D, Scope *S) { |
Argyrios Kyrtzidis | 87f3ff0 | 2008-04-12 00:47:19 +0000 | [diff] [blame] | 77 | S->AddDecl(D); |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 78 | |
| 79 | // C++ [basic.scope]p4: |
| 80 | // -- exactly one declaration shall declare a class name or |
| 81 | // enumeration name that is not a typedef name and the other |
| 82 | // declarations shall all refer to the same object or |
| 83 | // enumerator, or all refer to functions and function templates; |
| 84 | // in this case the class name or enumeration name is hidden. |
| 85 | if (TagDecl *TD = dyn_cast<TagDecl>(D)) { |
| 86 | // We are pushing the name of a tag (enum or class). |
Argyrios Kyrtzidis | 90eb539 | 2008-07-17 17:49:50 +0000 | [diff] [blame] | 87 | IdentifierResolver::iterator |
| 88 | I = IdResolver.begin(TD->getIdentifier(), |
| 89 | TD->getDeclContext(), false/*LookInParentCtx*/); |
Argyrios Kyrtzidis | 15a12d0 | 2008-09-09 21:18:04 +0000 | [diff] [blame] | 90 | if (I != IdResolver.end() && isDeclInScope(*I, TD->getDeclContext(), S)) { |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 91 | // There is already a declaration with the same name in the same |
| 92 | // scope. It must be found before we find the new declaration, |
| 93 | // so swap the order on the shadowed declaration chain. |
| 94 | |
Argyrios Kyrtzidis | 90eb539 | 2008-07-17 17:49:50 +0000 | [diff] [blame] | 95 | IdResolver.AddShadowedDecl(TD, *I); |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 96 | return; |
| 97 | } |
Argyrios Kyrtzidis | f1af6a7 | 2008-10-22 23:08:24 +0000 | [diff] [blame] | 98 | } else if (getLangOptions().CPlusPlus && isa<FunctionDecl>(D)) { |
| 99 | FunctionDecl *FD = cast<FunctionDecl>(D); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 100 | // We are pushing the name of a function, which might be an |
| 101 | // overloaded name. |
| 102 | IdentifierResolver::iterator |
| 103 | I = IdResolver.begin(FD->getIdentifier(), |
| 104 | FD->getDeclContext(), false/*LookInParentCtx*/); |
| 105 | if (I != IdResolver.end() && |
| 106 | IdResolver.isDeclInScope(*I, FD->getDeclContext(), S) && |
| 107 | (isa<OverloadedFunctionDecl>(*I) || isa<FunctionDecl>(*I))) { |
| 108 | // There is already a declaration with the same name in the same |
| 109 | // scope. It must be a function or an overloaded function. |
| 110 | OverloadedFunctionDecl* Ovl = dyn_cast<OverloadedFunctionDecl>(*I); |
| 111 | if (!Ovl) { |
| 112 | // We haven't yet overloaded this function. Take the existing |
| 113 | // FunctionDecl and put it into an OverloadedFunctionDecl. |
| 114 | Ovl = OverloadedFunctionDecl::Create(Context, |
| 115 | FD->getDeclContext(), |
| 116 | FD->getIdentifier()); |
| 117 | Ovl->addOverload(dyn_cast<FunctionDecl>(*I)); |
| 118 | |
| 119 | // Remove the name binding to the existing FunctionDecl... |
| 120 | IdResolver.RemoveDecl(*I); |
| 121 | |
| 122 | // ... and put the OverloadedFunctionDecl in its place. |
| 123 | IdResolver.AddDecl(Ovl); |
| 124 | } |
| 125 | |
| 126 | // We have an OverloadedFunctionDecl. Add the new FunctionDecl |
| 127 | // to its list of overloads. |
| 128 | Ovl->addOverload(FD); |
| 129 | |
| 130 | return; |
| 131 | } |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 132 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 133 | |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 134 | IdResolver.AddDecl(D); |
Argyrios Kyrtzidis | 87f3ff0 | 2008-04-12 00:47:19 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Steve Naroff | b216c88 | 2007-10-09 22:01:59 +0000 | [diff] [blame] | 137 | void Sema::ActOnPopScope(SourceLocation Loc, Scope *S) { |
Chris Lattner | 31e0572 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 138 | if (S->decl_empty()) return; |
| 139 | assert((S->getFlags() & Scope::DeclScope) &&"Scope shouldn't contain decls!"); |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 140 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 141 | for (Scope::decl_iterator I = S->decl_begin(), E = S->decl_end(); |
| 142 | I != E; ++I) { |
Steve Naroff | c752d04 | 2007-09-13 18:10:37 +0000 | [diff] [blame] | 143 | Decl *TmpD = static_cast<Decl*>(*I); |
| 144 | assert(TmpD && "This decl didn't get pushed??"); |
Argyrios Kyrtzidis | 7643536 | 2008-06-10 01:32:09 +0000 | [diff] [blame] | 145 | |
| 146 | if (isa<CXXFieldDecl>(TmpD)) continue; |
| 147 | |
| 148 | assert(isa<ScopedDecl>(TmpD) && "Decl isn't ScopedDecl?"); |
| 149 | ScopedDecl *D = cast<ScopedDecl>(TmpD); |
Steve Naroff | c752d04 | 2007-09-13 18:10:37 +0000 | [diff] [blame] | 150 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 151 | IdentifierInfo *II = D->getIdentifier(); |
| 152 | if (!II) continue; |
| 153 | |
Ted Kremenek | a89d197 | 2008-09-03 18:03:35 +0000 | [diff] [blame] | 154 | // We only want to remove the decls from the identifier decl chains for |
| 155 | // local scopes, when inside a function/method. |
Argyrios Kyrtzidis | 7643536 | 2008-06-10 01:32:09 +0000 | [diff] [blame] | 156 | if (S->getFnParent() != 0) |
| 157 | IdResolver.RemoveDecl(D); |
Chris Lattner | 7f925cc | 2008-04-11 07:00:53 +0000 | [diff] [blame] | 158 | |
Argyrios Kyrtzidis | 7643536 | 2008-06-10 01:32:09 +0000 | [diff] [blame] | 159 | // Chain this decl to the containing DeclContext. |
| 160 | D->setNext(CurContext->getDeclChain()); |
| 161 | CurContext->setDeclChain(D); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | |
Steve Naroff | e8043c3 | 2008-04-01 23:04:06 +0000 | [diff] [blame] | 165 | /// getObjCInterfaceDecl - Look up a for a class declaration in the scope. |
| 166 | /// return 0 if one not found. |
Steve Naroff | e8043c3 | 2008-04-01 23:04:06 +0000 | [diff] [blame] | 167 | ObjCInterfaceDecl *Sema::getObjCInterfaceDecl(IdentifierInfo *Id) { |
Steve Naroff | 3110251 | 2008-04-02 18:30:49 +0000 | [diff] [blame] | 168 | // The third "scope" argument is 0 since we aren't enabling lazy built-in |
| 169 | // creation from this context. |
| 170 | Decl *IDecl = LookupDecl(Id, Decl::IDNS_Ordinary, 0, false); |
Fariborz Jahanian | 4cabdfc | 2007-10-12 19:38:20 +0000 | [diff] [blame] | 171 | |
Steve Naroff | b327ce0 | 2008-04-02 14:35:35 +0000 | [diff] [blame] | 172 | return dyn_cast_or_null<ObjCInterfaceDecl>(IDecl); |
Fariborz Jahanian | 4cabdfc | 2007-10-12 19:38:20 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Steve Naroff | e8043c3 | 2008-04-01 23:04:06 +0000 | [diff] [blame] | 175 | /// LookupDecl - Look up the inner-most declaration in the specified |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 176 | /// namespace. |
Steve Naroff | b327ce0 | 2008-04-02 14:35:35 +0000 | [diff] [blame] | 177 | Decl *Sema::LookupDecl(const IdentifierInfo *II, unsigned NSI, |
| 178 | Scope *S, bool enableLazyBuiltinCreation) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 179 | if (II == 0) return 0; |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 180 | unsigned NS = NSI; |
| 181 | if (getLangOptions().CPlusPlus && (NS & Decl::IDNS_Ordinary)) |
| 182 | NS |= Decl::IDNS_Tag; |
Chris Lattner | 7f925cc | 2008-04-11 07:00:53 +0000 | [diff] [blame] | 183 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 184 | // Scan up the scope chain looking for a decl that matches this identifier |
| 185 | // that is in the appropriate namespace. This search should not take long, as |
| 186 | // shadowing of names is uncommon, and deep shadowing is extremely uncommon. |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 187 | for (IdentifierResolver::iterator |
Argyrios Kyrtzidis | 90eb539 | 2008-07-17 17:49:50 +0000 | [diff] [blame] | 188 | I = IdResolver.begin(II, CurContext), E = IdResolver.end(); I != E; ++I) |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 189 | if ((*I)->getIdentifierNamespace() & NS) |
| 190 | return *I; |
Chris Lattner | 7f925cc | 2008-04-11 07:00:53 +0000 | [diff] [blame] | 191 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 192 | // If we didn't find a use of this identifier, and if the identifier |
| 193 | // corresponds to a compiler builtin, create the decl object for the builtin |
| 194 | // now, injecting it into translation unit scope, and return it. |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 195 | if (NS & Decl::IDNS_Ordinary) { |
Steve Naroff | b327ce0 | 2008-04-02 14:35:35 +0000 | [diff] [blame] | 196 | if (enableLazyBuiltinCreation) { |
| 197 | // If this is a builtin on this (or all) targets, create the decl. |
| 198 | if (unsigned BuiltinID = II->getBuiltinID()) |
| 199 | return LazilyCreateBuiltin((IdentifierInfo *)II, BuiltinID, S); |
| 200 | } |
Steve Naroff | e8043c3 | 2008-04-01 23:04:06 +0000 | [diff] [blame] | 201 | if (getLangOptions().ObjC1) { |
| 202 | // @interface and @compatibility_alias introduce typedef-like names. |
| 203 | // 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] | 204 | // therefore not scoped decls). They can, however, be shadowed by |
Steve Naroff | e8043c3 | 2008-04-01 23:04:06 +0000 | [diff] [blame] | 205 | // other names in IDNS_Ordinary. |
Steve Naroff | 3110251 | 2008-04-02 18:30:49 +0000 | [diff] [blame] | 206 | ObjCInterfaceDeclsTy::iterator IDI = ObjCInterfaceDecls.find(II); |
| 207 | if (IDI != ObjCInterfaceDecls.end()) |
| 208 | return IDI->second; |
Steve Naroff | e8043c3 | 2008-04-01 23:04:06 +0000 | [diff] [blame] | 209 | ObjCAliasTy::iterator I = ObjCAliasDecls.find(II); |
| 210 | if (I != ObjCAliasDecls.end()) |
| 211 | return I->second->getClassInterface(); |
| 212 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 213 | } |
| 214 | return 0; |
| 215 | } |
| 216 | |
Chris Lattner | 95e2c71 | 2008-05-05 22:18:14 +0000 | [diff] [blame] | 217 | void Sema::InitBuiltinVaListType() { |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 218 | if (!Context.getBuiltinVaListType().isNull()) |
| 219 | return; |
| 220 | |
| 221 | IdentifierInfo *VaIdent = &Context.Idents.get("__builtin_va_list"); |
Steve Naroff | b327ce0 | 2008-04-02 14:35:35 +0000 | [diff] [blame] | 222 | Decl *VaDecl = LookupDecl(VaIdent, Decl::IDNS_Ordinary, TUScope); |
Steve Naroff | 733002f | 2007-10-18 22:17:45 +0000 | [diff] [blame] | 223 | TypedefDecl *VaTypedef = cast<TypedefDecl>(VaDecl); |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 224 | Context.setBuiltinVaListType(Context.getTypedefType(VaTypedef)); |
| 225 | } |
| 226 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 227 | /// LazilyCreateBuiltin - The specified Builtin-ID was first used at file scope. |
| 228 | /// lazily create a decl for it. |
Chris Lattner | 22b73ba | 2007-10-10 23:42:28 +0000 | [diff] [blame] | 229 | ScopedDecl *Sema::LazilyCreateBuiltin(IdentifierInfo *II, unsigned bid, |
| 230 | Scope *S) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 231 | Builtin::ID BID = (Builtin::ID)bid; |
| 232 | |
Chris Lattner | bd7eb1c | 2008-09-28 05:54:29 +0000 | [diff] [blame] | 233 | if (Context.BuiltinInfo.hasVAListUse(BID)) |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 234 | InitBuiltinVaListType(); |
| 235 | |
Anders Carlsson | b2cf357 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 236 | QualType R = Context.BuiltinInfo.GetBuiltinType(BID, Context); |
Argyrios Kyrtzidis | ff898cd | 2008-04-17 14:47:13 +0000 | [diff] [blame] | 237 | FunctionDecl *New = FunctionDecl::Create(Context, |
| 238 | Context.getTranslationUnitDecl(), |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 239 | SourceLocation(), II, R, |
Chris Lattner | a98e58d | 2008-03-15 21:24:04 +0000 | [diff] [blame] | 240 | FunctionDecl::Extern, false, 0); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 241 | |
Chris Lattner | 95e2c71 | 2008-05-05 22:18:14 +0000 | [diff] [blame] | 242 | // Create Decl objects for each parameter, adding them to the |
| 243 | // FunctionDecl. |
| 244 | if (FunctionTypeProto *FT = dyn_cast<FunctionTypeProto>(R)) { |
| 245 | llvm::SmallVector<ParmVarDecl*, 16> Params; |
| 246 | for (unsigned i = 0, e = FT->getNumArgs(); i != e; ++i) |
| 247 | Params.push_back(ParmVarDecl::Create(Context, New, SourceLocation(), 0, |
| 248 | FT->getArgType(i), VarDecl::None, 0, |
| 249 | 0)); |
| 250 | New->setParams(&Params[0], Params.size()); |
| 251 | } |
| 252 | |
| 253 | |
| 254 | |
Chris Lattner | 7f925cc | 2008-04-11 07:00:53 +0000 | [diff] [blame] | 255 | // TUScope is the translation-unit scope to insert this function into. |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 256 | PushOnScopeChains(New, TUScope); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 257 | return New; |
| 258 | } |
| 259 | |
| 260 | /// MergeTypeDefDecl - We just parsed a typedef 'New' which has the same name |
| 261 | /// and scope as a previous declaration 'Old'. Figure out how to resolve this |
| 262 | /// situation, merging decls or emitting diagnostics as appropriate. |
| 263 | /// |
Steve Naroff | e8043c3 | 2008-04-01 23:04:06 +0000 | [diff] [blame] | 264 | TypedefDecl *Sema::MergeTypeDefDecl(TypedefDecl *New, Decl *OldD) { |
Steve Naroff | 2b255c4 | 2008-09-09 14:32:20 +0000 | [diff] [blame] | 265 | // Allow multiple definitions for ObjC built-in typedefs. |
| 266 | // FIXME: Verify the underlying types are equivalent! |
| 267 | if (getLangOptions().ObjC1) { |
| 268 | const IdentifierInfo *typeIdent = New->getIdentifier(); |
| 269 | if (typeIdent == Ident_id) { |
| 270 | Context.setObjCIdType(New); |
| 271 | return New; |
| 272 | } else if (typeIdent == Ident_Class) { |
| 273 | Context.setObjCClassType(New); |
| 274 | return New; |
| 275 | } else if (typeIdent == Ident_SEL) { |
| 276 | Context.setObjCSelType(New); |
| 277 | return New; |
| 278 | } else if (typeIdent == Ident_Protocol) { |
| 279 | Context.setObjCProtoType(New->getUnderlyingType()); |
| 280 | return New; |
| 281 | } |
| 282 | // Fall through - the typedef name was not a builtin type. |
| 283 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 284 | // Verify the old decl was also a typedef. |
| 285 | TypedefDecl *Old = dyn_cast<TypedefDecl>(OldD); |
| 286 | if (!Old) { |
| 287 | Diag(New->getLocation(), diag::err_redefinition_different_kind, |
| 288 | New->getName()); |
| 289 | Diag(OldD->getLocation(), diag::err_previous_definition); |
| 290 | return New; |
| 291 | } |
| 292 | |
Chris Lattner | 99cb997 | 2008-07-25 18:44:27 +0000 | [diff] [blame] | 293 | // If the typedef types are not identical, reject them in all languages and |
| 294 | // with any extensions enabled. |
| 295 | if (Old->getUnderlyingType() != New->getUnderlyingType() && |
| 296 | Context.getCanonicalType(Old->getUnderlyingType()) != |
| 297 | Context.getCanonicalType(New->getUnderlyingType())) { |
| 298 | Diag(New->getLocation(), diag::err_redefinition_different_typedef, |
| 299 | New->getUnderlyingType().getAsString(), |
| 300 | Old->getUnderlyingType().getAsString()); |
| 301 | Diag(Old->getLocation(), diag::err_previous_definition); |
| 302 | return Old; |
| 303 | } |
| 304 | |
Eli Friedman | 54ecfce | 2008-06-11 06:20:39 +0000 | [diff] [blame] | 305 | if (getLangOptions().Microsoft) return New; |
| 306 | |
Steve Naroff | 4c49a6c | 2008-01-30 23:46:05 +0000 | [diff] [blame] | 307 | // Redeclaration of a type is a constraint violation (6.7.2.3p1). |
| 308 | // Apparently GCC, Intel, and Sun all silently ignore the redeclaration if |
| 309 | // *either* declaration is in a system header. The code below implements |
| 310 | // this adhoc compatibility rule. FIXME: The following code will not |
| 311 | // work properly when compiling ".i" files (containing preprocessed output). |
Daniel Dunbar | 2fe0997 | 2008-09-12 18:10:20 +0000 | [diff] [blame] | 312 | if (PP.getDiagnostics().getSuppressSystemWarnings()) { |
| 313 | SourceManager &SrcMgr = Context.getSourceManager(); |
| 314 | if (SrcMgr.isInSystemHeader(Old->getLocation())) |
| 315 | return New; |
| 316 | if (SrcMgr.isInSystemHeader(New->getLocation())) |
| 317 | return New; |
| 318 | } |
Eli Friedman | 54ecfce | 2008-06-11 06:20:39 +0000 | [diff] [blame] | 319 | |
Ted Kremenek | 2d05c08 | 2008-05-23 21:28:18 +0000 | [diff] [blame] | 320 | Diag(New->getLocation(), diag::err_redefinition, New->getName()); |
| 321 | Diag(Old->getLocation(), diag::err_previous_definition); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 322 | return New; |
| 323 | } |
| 324 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 325 | /// DeclhasAttr - returns true if decl Declaration already has the target |
| 326 | /// attribute. |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 327 | static bool DeclHasAttr(const Decl *decl, const Attr *target) { |
| 328 | for (const Attr *attr = decl->getAttrs(); attr; attr = attr->getNext()) |
| 329 | if (attr->getKind() == target->getKind()) |
| 330 | return true; |
| 331 | |
| 332 | return false; |
| 333 | } |
| 334 | |
| 335 | /// MergeAttributes - append attributes from the Old decl to the New one. |
| 336 | static void MergeAttributes(Decl *New, Decl *Old) { |
| 337 | Attr *attr = const_cast<Attr*>(Old->getAttrs()), *tmp; |
| 338 | |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 339 | while (attr) { |
| 340 | tmp = attr; |
| 341 | attr = attr->getNext(); |
| 342 | |
| 343 | if (!DeclHasAttr(New, tmp)) { |
| 344 | New->addAttr(tmp); |
| 345 | } else { |
| 346 | tmp->setNext(0); |
| 347 | delete(tmp); |
| 348 | } |
| 349 | } |
Nuno Lopes | 9141bee | 2008-06-01 22:53:53 +0000 | [diff] [blame] | 350 | |
| 351 | Old->invalidateAttrs(); |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 352 | } |
| 353 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 354 | /// MergeFunctionDecl - We just parsed a function 'New' from |
| 355 | /// declarator D which has the same name and scope as a previous |
| 356 | /// declaration 'Old'. Figure out how to resolve this situation, |
| 357 | /// merging decls or emitting diagnostics as appropriate. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 358 | /// Redeclaration will be set true if this New is a redeclaration OldD. |
| 359 | /// |
| 360 | /// In C++, New and Old must be declarations that are not |
| 361 | /// overloaded. Use IsOverload to determine whether New and Old are |
| 362 | /// overloaded, and to select the Old declaration that New should be |
| 363 | /// merged with. |
Douglas Gregor | f009795 | 2008-04-21 02:02:58 +0000 | [diff] [blame] | 364 | FunctionDecl * |
| 365 | Sema::MergeFunctionDecl(FunctionDecl *New, Decl *OldD, bool &Redeclaration) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 366 | assert(!isa<OverloadedFunctionDecl>(OldD) && |
| 367 | "Cannot merge with an overloaded function declaration"); |
| 368 | |
Douglas Gregor | f009795 | 2008-04-21 02:02:58 +0000 | [diff] [blame] | 369 | Redeclaration = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 370 | // Verify the old decl was also a function. |
| 371 | FunctionDecl *Old = dyn_cast<FunctionDecl>(OldD); |
| 372 | if (!Old) { |
| 373 | Diag(New->getLocation(), diag::err_redefinition_different_kind, |
| 374 | New->getName()); |
| 375 | Diag(OldD->getLocation(), diag::err_previous_definition); |
| 376 | return New; |
| 377 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 378 | |
| 379 | // Determine whether the previous declaration was a definition, |
| 380 | // implicit declaration, or a declaration. |
| 381 | diag::kind PrevDiag; |
| 382 | if (Old->isThisDeclarationADefinition()) |
| 383 | PrevDiag = diag::err_previous_definition; |
| 384 | else if (Old->isImplicit()) |
| 385 | PrevDiag = diag::err_previous_implicit_declaration; |
| 386 | else |
| 387 | PrevDiag = diag::err_previous_declaration; |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 388 | |
Chris Lattner | 8bcfc5b | 2008-04-06 23:10:54 +0000 | [diff] [blame] | 389 | QualType OldQType = Context.getCanonicalType(Old->getType()); |
| 390 | QualType NewQType = Context.getCanonicalType(New->getType()); |
Chris Lattner | 5519644 | 2007-11-20 19:04:50 +0000 | [diff] [blame] | 391 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 392 | if (getLangOptions().CPlusPlus) { |
| 393 | // (C++98 13.1p2): |
| 394 | // Certain function declarations cannot be overloaded: |
| 395 | // -- Function declarations that differ only in the return type |
| 396 | // cannot be overloaded. |
| 397 | QualType OldReturnType |
| 398 | = cast<FunctionType>(OldQType.getTypePtr())->getResultType(); |
| 399 | QualType NewReturnType |
| 400 | = cast<FunctionType>(NewQType.getTypePtr())->getResultType(); |
| 401 | if (OldReturnType != NewReturnType) { |
| 402 | Diag(New->getLocation(), diag::err_ovl_diff_return_type); |
| 403 | Diag(Old->getLocation(), PrevDiag); |
| 404 | return New; |
| 405 | } |
| 406 | |
| 407 | const CXXMethodDecl* OldMethod = dyn_cast<CXXMethodDecl>(Old); |
| 408 | const CXXMethodDecl* NewMethod = dyn_cast<CXXMethodDecl>(New); |
| 409 | if (OldMethod && NewMethod) { |
| 410 | // -- Member function declarations with the same name and the |
| 411 | // same parameter types cannot be overloaded if any of them |
| 412 | // is a static member function declaration. |
| 413 | if (OldMethod->isStatic() || NewMethod->isStatic()) { |
| 414 | Diag(New->getLocation(), diag::err_ovl_static_nonstatic_member); |
| 415 | Diag(Old->getLocation(), PrevDiag); |
| 416 | return New; |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | // (C++98 8.3.5p3): |
| 421 | // All declarations for a function shall agree exactly in both the |
| 422 | // return type and the parameter-type-list. |
| 423 | if (OldQType == NewQType) { |
| 424 | // We have a redeclaration. |
| 425 | MergeAttributes(New, Old); |
| 426 | Redeclaration = true; |
| 427 | return MergeCXXFunctionDecl(New, Old); |
| 428 | } |
| 429 | |
| 430 | // Fall through for conflicting redeclarations and redefinitions. |
Douglas Gregor | f009795 | 2008-04-21 02:02:58 +0000 | [diff] [blame] | 431 | } |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 432 | |
| 433 | // C: Function types need to be compatible, not identical. This handles |
Steve Naroff | adbbd0c | 2008-01-14 20:51:29 +0000 | [diff] [blame] | 434 | // duplicate function decls like "void f(int); void f(enum X);" properly. |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 435 | if (!getLangOptions().CPlusPlus && |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 436 | Context.typesAreCompatible(OldQType, NewQType)) { |
Douglas Gregor | f009795 | 2008-04-21 02:02:58 +0000 | [diff] [blame] | 437 | MergeAttributes(New, Old); |
| 438 | Redeclaration = true; |
Steve Naroff | adbbd0c | 2008-01-14 20:51:29 +0000 | [diff] [blame] | 439 | return New; |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 440 | } |
Chris Lattner | e3995fe | 2007-11-06 06:07:26 +0000 | [diff] [blame] | 441 | |
Steve Naroff | 837618c | 2008-01-16 15:01:34 +0000 | [diff] [blame] | 442 | // A function that has already been declared has been redeclared or defined |
| 443 | // with a different type- show appropriate diagnostic |
Steve Naroff | 837618c | 2008-01-16 15:01:34 +0000 | [diff] [blame] | 444 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 445 | // TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope. |
| 446 | // TODO: This is totally simplistic. It should handle merging functions |
| 447 | // together etc, merging extern int X; int X; ... |
Steve Naroff | 837618c | 2008-01-16 15:01:34 +0000 | [diff] [blame] | 448 | Diag(New->getLocation(), diag::err_conflicting_types, New->getName()); |
| 449 | Diag(Old->getLocation(), PrevDiag); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 450 | return New; |
| 451 | } |
| 452 | |
Steve Naroff | ff9eb1f | 2008-08-08 17:50:35 +0000 | [diff] [blame] | 453 | /// Predicate for C "tentative" external object definitions (C99 6.9.2). |
Steve Naroff | d4d46cd | 2008-08-10 15:28:06 +0000 | [diff] [blame] | 454 | static bool isTentativeDefinition(VarDecl *VD) { |
Steve Naroff | ff9eb1f | 2008-08-08 17:50:35 +0000 | [diff] [blame] | 455 | if (VD->isFileVarDecl()) |
| 456 | return (!VD->getInit() && |
| 457 | (VD->getStorageClass() == VarDecl::None || |
| 458 | VD->getStorageClass() == VarDecl::Static)); |
| 459 | return false; |
| 460 | } |
| 461 | |
| 462 | /// CheckForFileScopedRedefinitions - Make sure we forgo redefinition errors |
| 463 | /// when dealing with C "tentative" external object definitions (C99 6.9.2). |
| 464 | void Sema::CheckForFileScopedRedefinitions(Scope *S, VarDecl *VD) { |
| 465 | bool VDIsTentative = isTentativeDefinition(VD); |
Steve Naroff | f855e6f | 2008-08-10 15:20:13 +0000 | [diff] [blame] | 466 | bool VDIsIncompleteArray = VD->getType()->isIncompleteArrayType(); |
Steve Naroff | ff9eb1f | 2008-08-08 17:50:35 +0000 | [diff] [blame] | 467 | |
| 468 | for (IdentifierResolver::iterator |
| 469 | I = IdResolver.begin(VD->getIdentifier(), |
| 470 | VD->getDeclContext(), false/*LookInParentCtx*/), |
| 471 | E = IdResolver.end(); I != E; ++I) { |
Argyrios Kyrtzidis | 15a12d0 | 2008-09-09 21:18:04 +0000 | [diff] [blame] | 472 | if (*I != VD && isDeclInScope(*I, VD->getDeclContext(), S)) { |
Steve Naroff | ff9eb1f | 2008-08-08 17:50:35 +0000 | [diff] [blame] | 473 | VarDecl *OldDecl = dyn_cast<VarDecl>(*I); |
| 474 | |
Steve Naroff | f855e6f | 2008-08-10 15:20:13 +0000 | [diff] [blame] | 475 | // Handle the following case: |
| 476 | // int a[10]; |
| 477 | // int a[]; - the code below makes sure we set the correct type. |
| 478 | // int a[11]; - this is an error, size isn't 10. |
| 479 | if (OldDecl && VDIsTentative && VDIsIncompleteArray && |
| 480 | OldDecl->getType()->isConstantArrayType()) |
| 481 | VD->setType(OldDecl->getType()); |
| 482 | |
Steve Naroff | ff9eb1f | 2008-08-08 17:50:35 +0000 | [diff] [blame] | 483 | // Check for "tentative" definitions. We can't accomplish this in |
| 484 | // MergeVarDecl since the initializer hasn't been attached. |
| 485 | if (!OldDecl || isTentativeDefinition(OldDecl) || VDIsTentative) |
| 486 | continue; |
| 487 | |
| 488 | // Handle __private_extern__ just like extern. |
| 489 | if (OldDecl->getStorageClass() != VarDecl::Extern && |
| 490 | OldDecl->getStorageClass() != VarDecl::PrivateExtern && |
| 491 | VD->getStorageClass() != VarDecl::Extern && |
| 492 | VD->getStorageClass() != VarDecl::PrivateExtern) { |
| 493 | Diag(VD->getLocation(), diag::err_redefinition, VD->getName()); |
| 494 | Diag(OldDecl->getLocation(), diag::err_previous_definition); |
| 495 | } |
| 496 | } |
| 497 | } |
| 498 | } |
| 499 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 500 | /// MergeVarDecl - We just parsed a variable 'New' which has the same name |
| 501 | /// and scope as a previous declaration 'Old'. Figure out how to resolve this |
| 502 | /// situation, merging decls or emitting diagnostics as appropriate. |
| 503 | /// |
Steve Naroff | ff9eb1f | 2008-08-08 17:50:35 +0000 | [diff] [blame] | 504 | /// Tentative definition rules (C99 6.9.2p2) are checked by |
| 505 | /// FinalizeDeclaratorGroup. Unfortunately, we can't analyze tentative |
| 506 | /// definitions here, since the initializer hasn't been attached. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 507 | /// |
Steve Naroff | e8043c3 | 2008-04-01 23:04:06 +0000 | [diff] [blame] | 508 | VarDecl *Sema::MergeVarDecl(VarDecl *New, Decl *OldD) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 509 | // Verify the old decl was also a variable. |
| 510 | VarDecl *Old = dyn_cast<VarDecl>(OldD); |
| 511 | if (!Old) { |
| 512 | Diag(New->getLocation(), diag::err_redefinition_different_kind, |
| 513 | New->getName()); |
| 514 | Diag(OldD->getLocation(), diag::err_previous_definition); |
| 515 | return New; |
| 516 | } |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 517 | |
| 518 | MergeAttributes(New, Old); |
| 519 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 520 | // Verify the types match. |
Chris Lattner | 8bcfc5b | 2008-04-06 23:10:54 +0000 | [diff] [blame] | 521 | QualType OldCType = Context.getCanonicalType(Old->getType()); |
| 522 | QualType NewCType = Context.getCanonicalType(New->getType()); |
Steve Naroff | 907747b | 2008-08-09 16:04:40 +0000 | [diff] [blame] | 523 | if (OldCType != NewCType && !Context.typesAreCompatible(OldCType, NewCType)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 524 | Diag(New->getLocation(), diag::err_redefinition, New->getName()); |
| 525 | Diag(Old->getLocation(), diag::err_previous_definition); |
| 526 | return New; |
| 527 | } |
Steve Naroff | b7b032e | 2008-01-30 00:44:01 +0000 | [diff] [blame] | 528 | // C99 6.2.2p4: Check if we have a static decl followed by a non-static. |
| 529 | if (New->getStorageClass() == VarDecl::Static && |
| 530 | (Old->getStorageClass() == VarDecl::None || |
| 531 | Old->getStorageClass() == VarDecl::Extern)) { |
| 532 | Diag(New->getLocation(), diag::err_static_non_static, New->getName()); |
| 533 | Diag(Old->getLocation(), diag::err_previous_definition); |
| 534 | return New; |
| 535 | } |
| 536 | // C99 6.2.2p4: Check if we have a non-static decl followed by a static. |
| 537 | if (New->getStorageClass() != VarDecl::Static && |
| 538 | Old->getStorageClass() == VarDecl::Static) { |
| 539 | Diag(New->getLocation(), diag::err_non_static_static, New->getName()); |
| 540 | Diag(Old->getLocation(), diag::err_previous_definition); |
| 541 | return New; |
| 542 | } |
Steve Naroff | 094cefb | 2008-09-17 14:05:40 +0000 | [diff] [blame] | 543 | // Variables with external linkage are analyzed in FinalizeDeclaratorGroup. |
| 544 | if (New->getStorageClass() != VarDecl::Extern && !New->isFileVarDecl()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 545 | Diag(New->getLocation(), diag::err_redefinition, New->getName()); |
| 546 | Diag(Old->getLocation(), diag::err_previous_definition); |
| 547 | } |
| 548 | return New; |
| 549 | } |
| 550 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 551 | /// CheckParmsForFunctionDef - Check that the parameters of the given |
| 552 | /// function are appropriate for the definition of a function. This |
| 553 | /// takes care of any checks that cannot be performed on the |
| 554 | /// declaration itself, e.g., that the types of each of the function |
| 555 | /// parameters are complete. |
| 556 | bool Sema::CheckParmsForFunctionDef(FunctionDecl *FD) { |
| 557 | bool HasInvalidParm = false; |
| 558 | for (unsigned p = 0, NumParams = FD->getNumParams(); p < NumParams; ++p) { |
| 559 | ParmVarDecl *Param = FD->getParamDecl(p); |
| 560 | |
| 561 | // C99 6.7.5.3p4: the parameters in a parameter type list in a |
| 562 | // function declarator that is part of a function definition of |
| 563 | // that function shall not have incomplete type. |
| 564 | if (Param->getType()->isIncompleteType() && |
| 565 | !Param->isInvalidDecl()) { |
| 566 | Diag(Param->getLocation(), diag::err_typecheck_decl_incomplete_type, |
| 567 | Param->getType().getAsString()); |
| 568 | Param->setInvalidDecl(); |
| 569 | HasInvalidParm = true; |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | return HasInvalidParm; |
| 574 | } |
| 575 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 576 | /// ParsedFreeStandingDeclSpec - This method is invoked when a declspec with |
| 577 | /// no declarator (e.g. "struct foo;") is parsed. |
| 578 | Sema::DeclTy *Sema::ParsedFreeStandingDeclSpec(Scope *S, DeclSpec &DS) { |
| 579 | // TODO: emit error on 'int;' or 'const enum foo;'. |
| 580 | // TODO: emit error on 'typedef int;' |
| 581 | // if (!DS.isMissingDeclaratorOk()) Diag(...); |
| 582 | |
Steve Naroff | 9219928 | 2007-11-17 21:37:36 +0000 | [diff] [blame] | 583 | return dyn_cast_or_null<TagDecl>(static_cast<Decl *>(DS.getTypeRep())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 584 | } |
| 585 | |
Steve Naroff | d0091aa | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 586 | bool Sema::CheckSingleInitializer(Expr *&Init, QualType DeclType) { |
Steve Naroff | f009063 | 2007-09-02 02:04:30 +0000 | [diff] [blame] | 587 | // Get the type before calling CheckSingleAssignmentConstraints(), since |
| 588 | // it can promote the expression. |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 589 | QualType InitType = Init->getType(); |
Steve Naroff | f009063 | 2007-09-02 02:04:30 +0000 | [diff] [blame] | 590 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 591 | AssignConvertType ConvTy = CheckSingleAssignmentConstraints(DeclType, Init); |
| 592 | return DiagnoseAssignmentResult(ConvTy, Init->getLocStart(), DeclType, |
| 593 | InitType, Init, "initializing"); |
Steve Naroff | f009063 | 2007-09-02 02:04:30 +0000 | [diff] [blame] | 594 | } |
| 595 | |
Steve Naroff | a49e1fa | 2008-01-22 00:55:40 +0000 | [diff] [blame] | 596 | bool Sema::CheckStringLiteralInit(StringLiteral *strLiteral, QualType &DeclT) { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 597 | const ArrayType *AT = Context.getAsArrayType(DeclT); |
| 598 | |
| 599 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) { |
Steve Naroff | a49e1fa | 2008-01-22 00:55:40 +0000 | [diff] [blame] | 600 | // C99 6.7.8p14. We have an array of character type with unknown size |
| 601 | // being initialized to a string literal. |
| 602 | llvm::APSInt ConstVal(32); |
| 603 | ConstVal = strLiteral->getByteLength() + 1; |
| 604 | // Return a new array type (C99 6.7.8p22). |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 605 | DeclT = Context.getConstantArrayType(IAT->getElementType(), ConstVal, |
Steve Naroff | a49e1fa | 2008-01-22 00:55:40 +0000 | [diff] [blame] | 606 | ArrayType::Normal, 0); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 607 | } else { |
| 608 | const ConstantArrayType *CAT = cast<ConstantArrayType>(AT); |
Steve Naroff | a49e1fa | 2008-01-22 00:55:40 +0000 | [diff] [blame] | 609 | // C99 6.7.8p14. We have an array of character type with known size. |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 610 | // FIXME: Avoid truncation for 64-bit length strings. |
| 611 | if (strLiteral->getByteLength() > (unsigned)CAT->getSize().getZExtValue()) |
Steve Naroff | a49e1fa | 2008-01-22 00:55:40 +0000 | [diff] [blame] | 612 | Diag(strLiteral->getSourceRange().getBegin(), |
| 613 | diag::warn_initializer_string_for_char_array_too_long, |
| 614 | strLiteral->getSourceRange()); |
Steve Naroff | a49e1fa | 2008-01-22 00:55:40 +0000 | [diff] [blame] | 615 | } |
| 616 | // Set type from "char *" to "constant array of char". |
| 617 | strLiteral->setType(DeclT); |
| 618 | // For now, we always return false (meaning success). |
| 619 | return false; |
| 620 | } |
| 621 | |
| 622 | StringLiteral *Sema::IsStringLiteralInit(Expr *Init, QualType DeclType) { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 623 | const ArrayType *AT = Context.getAsArrayType(DeclType); |
Steve Naroff | a996033 | 2008-01-25 00:51:06 +0000 | [diff] [blame] | 624 | if (AT && AT->getElementType()->isCharType()) { |
| 625 | return dyn_cast<StringLiteral>(Init); |
| 626 | } |
Steve Naroff | a49e1fa | 2008-01-22 00:55:40 +0000 | [diff] [blame] | 627 | return 0; |
| 628 | } |
| 629 | |
Steve Naroff | a996033 | 2008-01-25 00:51:06 +0000 | [diff] [blame] | 630 | bool Sema::CheckInitializerTypes(Expr *&Init, QualType &DeclType) { |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 631 | // C++ [dcl.init.ref]p1: |
| 632 | // A variable declared to be a T&, that is “reference to type T” |
| 633 | // (8.3.2), shall be initialized by an object, or function, of |
| 634 | // type T or by an object that can be converted into a T. |
| 635 | if (DeclType->isReferenceType()) |
| 636 | return CheckReferenceInit(Init, DeclType); |
| 637 | |
Steve Naroff | ca10730 | 2008-01-21 23:53:58 +0000 | [diff] [blame] | 638 | // C99 6.7.8p3: The type of the entity to be initialized shall be an array |
| 639 | // of unknown size ("[]") or an object type that is not a variable array type. |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 640 | if (const VariableArrayType *VAT = Context.getAsVariableArrayType(DeclType)) |
Steve Naroff | ca10730 | 2008-01-21 23:53:58 +0000 | [diff] [blame] | 641 | return Diag(VAT->getSizeExpr()->getLocStart(), |
| 642 | diag::err_variable_object_no_init, |
| 643 | VAT->getSizeExpr()->getSourceRange()); |
| 644 | |
Steve Naroff | 2fdc374 | 2007-12-10 22:44:33 +0000 | [diff] [blame] | 645 | InitListExpr *InitList = dyn_cast<InitListExpr>(Init); |
| 646 | if (!InitList) { |
Steve Naroff | a49e1fa | 2008-01-22 00:55:40 +0000 | [diff] [blame] | 647 | // FIXME: Handle wide strings |
| 648 | if (StringLiteral *strLiteral = IsStringLiteralInit(Init, DeclType)) |
| 649 | return CheckStringLiteralInit(strLiteral, DeclType); |
Eli Friedman | a312ce2 | 2008-02-08 00:48:24 +0000 | [diff] [blame] | 650 | |
Steve Naroff | 1ac6fdd | 2008-09-29 20:07:05 +0000 | [diff] [blame] | 651 | // C99 6.7.8p16. |
Eli Friedman | a312ce2 | 2008-02-08 00:48:24 +0000 | [diff] [blame] | 652 | if (DeclType->isArrayType()) |
| 653 | return Diag(Init->getLocStart(), |
| 654 | diag::err_array_init_list_required, |
| 655 | Init->getSourceRange()); |
| 656 | |
Steve Naroff | d0091aa | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 657 | return CheckSingleInitializer(Init, DeclType); |
Steve Naroff | 2fdc374 | 2007-12-10 22:44:33 +0000 | [diff] [blame] | 658 | } |
Eli Friedman | e6f058f | 2008-06-06 19:40:52 +0000 | [diff] [blame] | 659 | |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 660 | InitListChecker CheckInitList(this, InitList, DeclType); |
| 661 | return CheckInitList.HadError(); |
Steve Naroff | f009063 | 2007-09-02 02:04:30 +0000 | [diff] [blame] | 662 | } |
| 663 | |
Fariborz Jahanian | 306d68f | 2007-11-08 23:49:49 +0000 | [diff] [blame] | 664 | Sema::DeclTy * |
Daniel Dunbar | 914701e | 2008-08-05 16:28:08 +0000 | [diff] [blame] | 665 | Sema::ActOnDeclarator(Scope *S, Declarator &D, DeclTy *lastDecl) { |
Steve Naroff | 9474504 | 2007-09-13 23:52:58 +0000 | [diff] [blame] | 666 | ScopedDecl *LastDeclarator = dyn_cast_or_null<ScopedDecl>((Decl *)lastDecl); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 667 | IdentifierInfo *II = D.getIdentifier(); |
| 668 | |
Chris Lattner | e80a59c | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 669 | // All of these full declarators require an identifier. If it doesn't have |
| 670 | // one, the ParsedFreeStandingDeclSpec action should be used. |
| 671 | if (II == 0) { |
Chris Lattner | 311ff02 | 2007-10-16 22:36:42 +0000 | [diff] [blame] | 672 | Diag(D.getDeclSpec().getSourceRange().getBegin(), |
Chris Lattner | 98e0863 | 2007-08-28 06:17:15 +0000 | [diff] [blame] | 673 | diag::err_declarator_need_ident, |
Chris Lattner | e80a59c | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 674 | D.getDeclSpec().getSourceRange(), D.getSourceRange()); |
| 675 | return 0; |
| 676 | } |
| 677 | |
Chris Lattner | 31e0572 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 678 | // The scope passed in may not be a decl scope. Zip up the scope tree until |
| 679 | // we find one that is. |
| 680 | while ((S->getFlags() & Scope::DeclScope) == 0) |
| 681 | S = S->getParent(); |
| 682 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 683 | // 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] | 684 | Decl *PrevDecl = LookupDecl(II, Decl::IDNS_Ordinary, S); |
Steve Naroff | c752d04 | 2007-09-13 18:10:37 +0000 | [diff] [blame] | 685 | ScopedDecl *New; |
Steve Naroff | 5912a35 | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 686 | bool InvalidDecl = false; |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 687 | |
| 688 | // In C++, the previous declaration we find might be a tag type |
| 689 | // (class or enum). In this case, the new declaration will hide the |
| 690 | // tag type. |
| 691 | if (PrevDecl && PrevDecl->getIdentifierNamespace() == Decl::IDNS_Tag) |
| 692 | PrevDecl = 0; |
| 693 | |
Chris Lattner | 41af093 | 2007-11-14 06:34:38 +0000 | [diff] [blame] | 694 | QualType R = GetTypeForDeclarator(D, S); |
| 695 | assert(!R.isNull() && "GetTypeForDeclarator() returned null type"); |
| 696 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 697 | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) { |
Douglas Gregor | 6d6eb57 | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 698 | // Check that there are no default arguments (C++ only). |
| 699 | if (getLangOptions().CPlusPlus) |
| 700 | CheckExtraCXXDefaultArguments(D); |
| 701 | |
Chris Lattner | 41af093 | 2007-11-14 06:34:38 +0000 | [diff] [blame] | 702 | TypedefDecl *NewTD = ParseTypedefDecl(S, D, R, LastDeclarator); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 703 | if (!NewTD) return 0; |
| 704 | |
| 705 | // Handle attributes prior to checking for duplicates in MergeVarDecl |
Chris Lattner | 3ff30c8 | 2008-06-29 00:02:00 +0000 | [diff] [blame] | 706 | ProcessDeclAttributes(NewTD, D); |
Steve Naroff | ffce4d5 | 2008-01-09 23:34:55 +0000 | [diff] [blame] | 707 | // Merge the decl with the existing one if appropriate. If the decl is |
| 708 | // in an outer scope, it isn't the same thing. |
Argyrios Kyrtzidis | 15a12d0 | 2008-09-09 21:18:04 +0000 | [diff] [blame] | 709 | if (PrevDecl && isDeclInScope(PrevDecl, CurContext, S)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 710 | NewTD = MergeTypeDefDecl(NewTD, PrevDecl); |
| 711 | if (NewTD == 0) return 0; |
| 712 | } |
| 713 | New = NewTD; |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 714 | if (S->getFnParent() == 0) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 715 | // C99 6.7.7p2: If a typedef name specifies a variably modified type |
| 716 | // then it shall have block scope. |
Eli Friedman | 9db1397 | 2008-02-15 12:53:51 +0000 | [diff] [blame] | 717 | if (NewTD->getUnderlyingType()->isVariablyModifiedType()) { |
| 718 | // FIXME: Diagnostic needs to be fixed. |
| 719 | Diag(D.getIdentifierLoc(), diag::err_typecheck_illegal_vla); |
Steve Naroff | d7444aa | 2007-08-31 17:20:07 +0000 | [diff] [blame] | 720 | InvalidDecl = true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 721 | } |
| 722 | } |
Chris Lattner | 41af093 | 2007-11-14 06:34:38 +0000 | [diff] [blame] | 723 | } else if (R.getTypePtr()->isFunctionType()) { |
Chris Lattner | 271f1a6 | 2007-09-27 15:15:46 +0000 | [diff] [blame] | 724 | FunctionDecl::StorageClass SC = FunctionDecl::None; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 725 | switch (D.getDeclSpec().getStorageClassSpec()) { |
| 726 | default: assert(0 && "Unknown storage class!"); |
| 727 | case DeclSpec::SCS_auto: |
| 728 | case DeclSpec::SCS_register: |
| 729 | Diag(D.getIdentifierLoc(), diag::err_typecheck_sclass_func, |
| 730 | R.getAsString()); |
Steve Naroff | 5912a35 | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 731 | InvalidDecl = true; |
| 732 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 733 | case DeclSpec::SCS_unspecified: SC = FunctionDecl::None; break; |
| 734 | case DeclSpec::SCS_extern: SC = FunctionDecl::Extern; break; |
| 735 | case DeclSpec::SCS_static: SC = FunctionDecl::Static; break; |
Steve Naroff | 7dd0bd4 | 2008-01-28 21:57:15 +0000 | [diff] [blame] | 736 | case DeclSpec::SCS_private_extern: SC = FunctionDecl::PrivateExtern;break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 737 | } |
| 738 | |
Chris Lattner | a98e58d | 2008-03-15 21:24:04 +0000 | [diff] [blame] | 739 | bool isInline = D.getDeclSpec().isInlineSpecified(); |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame^] | 740 | bool isVirtual = D.getDeclSpec().isVirtualSpecified(); |
| 741 | bool isExplicit = D.getDeclSpec().isExplicitSpecified(); |
| 742 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 743 | FunctionDecl *NewFD; |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame^] | 744 | if (isCurrentClassName(*II, S)) { |
| 745 | // This is a C++ constructor declaration. |
| 746 | assert(D.getContext() == Declarator::MemberContext && |
| 747 | "Constructors can only be declared in a member context"); |
| 748 | |
| 749 | // C++ [class.ctor]p3: |
| 750 | // A constructor shall not be virtual (10.3) or static (9.4). A |
| 751 | // constructor can be invoked for a const, volatile or const |
| 752 | // volatile object. A constructor shall not be declared const, |
| 753 | // volatile, or const volatile (9.3.2). |
| 754 | if (isVirtual) { |
| 755 | Diag(D.getIdentifierLoc(), |
| 756 | diag::err_constructor_cannot_be, |
| 757 | "virtual", |
| 758 | SourceRange(D.getDeclSpec().getVirtualSpecLoc()), |
| 759 | SourceRange(D.getIdentifierLoc())); |
| 760 | isVirtual = false; |
| 761 | } |
| 762 | if (SC == FunctionDecl::Static) { |
| 763 | Diag(D.getIdentifierLoc(), |
| 764 | diag::err_constructor_cannot_be, |
| 765 | "static", |
| 766 | SourceRange(D.getDeclSpec().getStorageClassSpecLoc()), |
| 767 | SourceRange(D.getIdentifierLoc())); |
| 768 | isVirtual = false; |
| 769 | } |
| 770 | if (D.getDeclSpec().hasTypeSpecifier()) { |
| 771 | // Constructors don't have return types, but the parser will |
| 772 | // happily parse something like: |
| 773 | // |
| 774 | // class X { |
| 775 | // float X(float); |
| 776 | // }; |
| 777 | // |
| 778 | // The return type will be eliminated later. |
| 779 | Diag(D.getIdentifierLoc(), |
| 780 | diag::err_constructor_return_type, |
| 781 | SourceRange(D.getDeclSpec().getTypeSpecTypeLoc()), |
| 782 | SourceRange(D.getIdentifierLoc())); |
| 783 | } |
| 784 | if (R->getAsFunctionTypeProto()->getTypeQuals() != 0) { |
| 785 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun; |
| 786 | if (FTI.TypeQuals & QualType::Const) |
| 787 | Diag(D.getIdentifierLoc(), |
| 788 | diag::err_invalid_qualified_constructor, |
| 789 | "const", |
| 790 | SourceRange(D.getIdentifierLoc())); |
| 791 | if (FTI.TypeQuals & QualType::Volatile) |
| 792 | Diag(D.getIdentifierLoc(), |
| 793 | diag::err_invalid_qualified_constructor, |
| 794 | "volatile", |
| 795 | SourceRange(D.getIdentifierLoc())); |
| 796 | if (FTI.TypeQuals & QualType::Restrict) |
| 797 | Diag(D.getIdentifierLoc(), |
| 798 | diag::err_invalid_qualified_constructor, |
| 799 | "restrict", |
| 800 | SourceRange(D.getIdentifierLoc())); |
| 801 | } |
| 802 | |
| 803 | // Rebuild the function type "R" without any type qualifiers (in |
| 804 | // case any of the errors above fired) and with "void" as the |
| 805 | // return type, since constructors don't have return types. We |
| 806 | // *always* have to do this, because GetTypeForDeclarator will |
| 807 | // put in a result type of "int" when none was specified. |
| 808 | const FunctionTypeProto *Proto = R->getAsFunctionTypeProto(); |
| 809 | R = Context.getFunctionType(Context.VoidTy, Proto->arg_type_begin(), |
| 810 | Proto->getNumArgs(), |
| 811 | Proto->isVariadic(), |
| 812 | 0); |
| 813 | |
| 814 | // Create the new declaration |
| 815 | NewFD = CXXConstructorDecl::Create(Context, |
| 816 | cast<CXXRecordDecl>(CurContext), |
| 817 | D.getIdentifierLoc(), II, R, |
| 818 | isExplicit, isInline, |
| 819 | /*isImplicitlyDeclared=*/false); |
| 820 | |
| 821 | } else if (D.getContext() == Declarator::MemberContext) { |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 822 | // This is a C++ method declaration. |
| 823 | NewFD = CXXMethodDecl::Create(Context, cast<CXXRecordDecl>(CurContext), |
| 824 | D.getIdentifierLoc(), II, R, |
| 825 | (SC == FunctionDecl::Static), isInline, |
| 826 | LastDeclarator); |
| 827 | } else { |
| 828 | NewFD = FunctionDecl::Create(Context, CurContext, |
| 829 | D.getIdentifierLoc(), |
Steve Naroff | 0eb07bf | 2008-10-03 00:02:03 +0000 | [diff] [blame] | 830 | II, R, SC, isInline, LastDeclarator, |
| 831 | // FIXME: Move to DeclGroup... |
| 832 | D.getDeclSpec().getSourceRange().getBegin()); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 833 | } |
Ted Kremenek | f5c93c1 | 2008-02-27 22:18:07 +0000 | [diff] [blame] | 834 | // Handle attributes. |
Chris Lattner | 3ff30c8 | 2008-06-29 00:02:00 +0000 | [diff] [blame] | 835 | ProcessDeclAttributes(NewFD, D); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 836 | |
Daniel Dunbar | a80f874 | 2008-08-05 01:35:17 +0000 | [diff] [blame] | 837 | // Handle GNU asm-label extension (encoded as an attribute). |
Daniel Dunbar | 914701e | 2008-08-05 16:28:08 +0000 | [diff] [blame] | 838 | if (Expr *E = (Expr*) D.getAsmLabel()) { |
Daniel Dunbar | a80f874 | 2008-08-05 01:35:17 +0000 | [diff] [blame] | 839 | // The parser guarantees this is a string. |
| 840 | StringLiteral *SE = cast<StringLiteral>(E); |
| 841 | NewFD->addAttr(new AsmLabelAttr(std::string(SE->getStrData(), |
| 842 | SE->getByteLength()))); |
| 843 | } |
| 844 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 845 | // Copy the parameter declarations from the declarator D to |
| 846 | // the function declaration NewFD, if they are available. |
Eli Friedman | eb4b705 | 2008-08-25 21:31:01 +0000 | [diff] [blame] | 847 | if (D.getNumTypeObjects() > 0) { |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 848 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun; |
| 849 | |
| 850 | // Create Decl objects for each parameter, adding them to the |
| 851 | // FunctionDecl. |
| 852 | llvm::SmallVector<ParmVarDecl*, 16> Params; |
| 853 | |
| 854 | // Check for C99 6.7.5.3p10 - foo(void) is a non-varargs |
| 855 | // function that takes no arguments, not a function that takes a |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 856 | // single void argument. |
Eli Friedman | 6d1e4b5 | 2008-05-22 08:54:03 +0000 | [diff] [blame] | 857 | // We let through "const void" here because Sema::GetTypeForDeclarator |
| 858 | // already checks for that case. |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 859 | if (FTI.NumArgs == 1 && !FTI.isVariadic && FTI.ArgInfo[0].Ident == 0 && |
| 860 | FTI.ArgInfo[0].Param && |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 861 | ((ParmVarDecl*)FTI.ArgInfo[0].Param)->getType()->isVoidType()) { |
| 862 | // empty arg list, don't push any params. |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 863 | ParmVarDecl *Param = (ParmVarDecl*)FTI.ArgInfo[0].Param; |
| 864 | |
Chris Lattner | def026a | 2008-04-10 02:26:16 +0000 | [diff] [blame] | 865 | // In C++, the empty parameter-type-list must be spelled "void"; a |
| 866 | // typedef of void is not permitted. |
| 867 | if (getLangOptions().CPlusPlus && |
Eli Friedman | 6d1e4b5 | 2008-05-22 08:54:03 +0000 | [diff] [blame] | 868 | Param->getType().getUnqualifiedType() != Context.VoidTy) { |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 869 | Diag(Param->getLocation(), diag::ext_param_typedef_of_void); |
| 870 | } |
Eli Friedman | eb4b705 | 2008-08-25 21:31:01 +0000 | [diff] [blame] | 871 | } else if (FTI.NumArgs > 0 && FTI.ArgInfo[0].Param != 0) { |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 872 | for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) |
| 873 | Params.push_back((ParmVarDecl *)FTI.ArgInfo[i].Param); |
| 874 | } |
| 875 | |
| 876 | NewFD->setParams(&Params[0], Params.size()); |
Douglas Gregor | 6cbd3df | 2008-10-24 18:09:54 +0000 | [diff] [blame] | 877 | } else if (R->getAsTypedefType()) { |
| 878 | // When we're declaring a function with a typedef, as in the |
| 879 | // following example, we'll need to synthesize (unnamed) |
| 880 | // parameters for use in the declaration. |
| 881 | // |
| 882 | // @code |
| 883 | // typedef void fn(int); |
| 884 | // fn f; |
| 885 | // @endcode |
| 886 | const FunctionTypeProto *FT = R->getAsFunctionTypeProto(); |
| 887 | if (!FT) { |
| 888 | // This is a typedef of a function with no prototype, so we |
| 889 | // don't need to do anything. |
| 890 | } else if ((FT->getNumArgs() == 0) || |
| 891 | (FT->getNumArgs() == 1 && !FT->isVariadic() && |
| 892 | FT->getArgType(0)->isVoidType())) { |
| 893 | // This is a zero-argument function. We don't need to do anything. |
| 894 | } else { |
| 895 | // Synthesize a parameter for each argument type. |
| 896 | llvm::SmallVector<ParmVarDecl*, 16> Params; |
| 897 | for (FunctionTypeProto::arg_type_iterator ArgType = FT->arg_type_begin(); |
| 898 | ArgType != FT->arg_type_end(); ++ArgType) { |
| 899 | Params.push_back(ParmVarDecl::Create(Context, CurContext, |
| 900 | SourceLocation(), 0, |
| 901 | *ArgType, VarDecl::None, |
| 902 | 0, 0)); |
| 903 | } |
| 904 | |
| 905 | NewFD->setParams(&Params[0], Params.size()); |
| 906 | } |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 907 | } |
| 908 | |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame^] | 909 | // C++ constructors are handled by a separate routine, since they |
| 910 | // don't require any declaration merging (C++ [class.mfct]p2) and |
| 911 | // they aren't ever pushed into scope, because they can't be found |
| 912 | // by name lookup anyway (C++ [class.ctor]p2). |
| 913 | if (CXXConstructorDecl *ConDecl = dyn_cast<CXXConstructorDecl>(NewFD)) |
| 914 | return ActOnConstructorDeclarator(ConDecl); |
| 915 | |
Steve Naroff | ffce4d5 | 2008-01-09 23:34:55 +0000 | [diff] [blame] | 916 | // Merge the decl with the existing one if appropriate. Since C functions |
| 917 | // 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] | 918 | if (PrevDecl && |
Argyrios Kyrtzidis | 15a12d0 | 2008-09-09 21:18:04 +0000 | [diff] [blame] | 919 | (!getLangOptions().CPlusPlus||isDeclInScope(PrevDecl, CurContext, S))) { |
Douglas Gregor | f009795 | 2008-04-21 02:02:58 +0000 | [diff] [blame] | 920 | bool Redeclaration = false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 921 | |
| 922 | // If C++, determine whether NewFD is an overload of PrevDecl or |
| 923 | // a declaration that requires merging. If it's an overload, |
| 924 | // there's no more work to do here; we'll just add the new |
| 925 | // function to the scope. |
| 926 | OverloadedFunctionDecl::function_iterator MatchedDecl; |
| 927 | if (!getLangOptions().CPlusPlus || |
| 928 | !IsOverload(NewFD, PrevDecl, MatchedDecl)) { |
| 929 | Decl *OldDecl = PrevDecl; |
| 930 | |
| 931 | // If PrevDecl was an overloaded function, extract the |
| 932 | // FunctionDecl that matched. |
| 933 | if (isa<OverloadedFunctionDecl>(PrevDecl)) |
| 934 | OldDecl = *MatchedDecl; |
| 935 | |
| 936 | // NewFD and PrevDecl represent declarations that need to be |
| 937 | // merged. |
| 938 | NewFD = MergeFunctionDecl(NewFD, OldDecl, Redeclaration); |
| 939 | |
| 940 | if (NewFD == 0) return 0; |
| 941 | if (Redeclaration) { |
| 942 | NewFD->setPreviousDeclaration(cast<FunctionDecl>(OldDecl)); |
| 943 | |
| 944 | if (OldDecl == PrevDecl) { |
| 945 | // Remove the name binding for the previous |
| 946 | // declaration. We'll add the binding back later, but then |
| 947 | // it will refer to the new declaration (which will |
| 948 | // contain more information). |
| 949 | IdResolver.RemoveDecl(cast<NamedDecl>(PrevDecl)); |
| 950 | } else { |
| 951 | // We need to update the OverloadedFunctionDecl with the |
| 952 | // latest declaration of this function, so that name |
| 953 | // lookup will always refer to the latest declaration of |
| 954 | // this function. |
| 955 | *MatchedDecl = NewFD; |
| 956 | |
| 957 | // Add the redeclaration to the current scope, since we'll |
| 958 | // be skipping PushOnScopeChains. |
| 959 | S->AddDecl(NewFD); |
| 960 | |
| 961 | return NewFD; |
| 962 | } |
| 963 | } |
Douglas Gregor | f009795 | 2008-04-21 02:02:58 +0000 | [diff] [blame] | 964 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 965 | } |
| 966 | New = NewFD; |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 967 | |
| 968 | // In C++, check default arguments now that we have merged decls. |
| 969 | if (getLangOptions().CPlusPlus) |
| 970 | CheckCXXDefaultArguments(NewFD); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 971 | } else { |
Douglas Gregor | 6d6eb57 | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 972 | // Check that there are no default arguments (C++ only). |
| 973 | if (getLangOptions().CPlusPlus) |
| 974 | CheckExtraCXXDefaultArguments(D); |
| 975 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 976 | if (R.getTypePtr()->isObjCInterfaceType()) { |
Fariborz Jahanian | e7f64cc | 2007-10-12 22:10:42 +0000 | [diff] [blame] | 977 | Diag(D.getIdentifierLoc(), diag::err_statically_allocated_object, |
| 978 | D.getIdentifier()->getName()); |
| 979 | InvalidDecl = true; |
| 980 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 981 | |
| 982 | VarDecl *NewVD; |
| 983 | VarDecl::StorageClass SC; |
| 984 | switch (D.getDeclSpec().getStorageClassSpec()) { |
Chris Lattner | 9e151e1 | 2008-03-15 21:10:16 +0000 | [diff] [blame] | 985 | default: assert(0 && "Unknown storage class!"); |
| 986 | case DeclSpec::SCS_unspecified: SC = VarDecl::None; break; |
| 987 | case DeclSpec::SCS_extern: SC = VarDecl::Extern; break; |
| 988 | case DeclSpec::SCS_static: SC = VarDecl::Static; break; |
| 989 | case DeclSpec::SCS_auto: SC = VarDecl::Auto; break; |
| 990 | case DeclSpec::SCS_register: SC = VarDecl::Register; break; |
| 991 | case DeclSpec::SCS_private_extern: SC = VarDecl::PrivateExtern; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 992 | } |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 993 | if (D.getContext() == Declarator::MemberContext) { |
| 994 | assert(SC == VarDecl::Static && "Invalid storage class for member!"); |
| 995 | // This is a static data member for a C++ class. |
| 996 | NewVD = CXXClassVarDecl::Create(Context, cast<CXXRecordDecl>(CurContext), |
| 997 | D.getIdentifierLoc(), II, |
| 998 | R, LastDeclarator); |
Steve Naroff | f009063 | 2007-09-02 02:04:30 +0000 | [diff] [blame] | 999 | } else { |
Daniel Dunbar | 6f0200e | 2008-09-08 20:05:47 +0000 | [diff] [blame] | 1000 | bool ThreadSpecified = D.getDeclSpec().isThreadSpecified(); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1001 | if (S->getFnParent() == 0) { |
| 1002 | // C99 6.9p2: The storage-class specifiers auto and register shall not |
| 1003 | // appear in the declaration specifiers in an external declaration. |
| 1004 | if (SC == VarDecl::Auto || SC == VarDecl::Register) { |
| 1005 | Diag(D.getIdentifierLoc(), diag::err_typecheck_sclass_fscope, |
| 1006 | R.getAsString()); |
| 1007 | InvalidDecl = true; |
| 1008 | } |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1009 | } |
Daniel Dunbar | 6f0200e | 2008-09-08 20:05:47 +0000 | [diff] [blame] | 1010 | NewVD = VarDecl::Create(Context, CurContext, D.getIdentifierLoc(), |
Steve Naroff | 0eb07bf | 2008-10-03 00:02:03 +0000 | [diff] [blame] | 1011 | II, R, SC, LastDeclarator, |
| 1012 | // FIXME: Move to DeclGroup... |
| 1013 | D.getDeclSpec().getSourceRange().getBegin()); |
Daniel Dunbar | 6f0200e | 2008-09-08 20:05:47 +0000 | [diff] [blame] | 1014 | NewVD->setThreadSpecified(ThreadSpecified); |
Steve Naroff | 53a3234 | 2007-08-28 18:45:29 +0000 | [diff] [blame] | 1015 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1016 | // Handle attributes prior to checking for duplicates in MergeVarDecl |
Chris Lattner | 3ff30c8 | 2008-06-29 00:02:00 +0000 | [diff] [blame] | 1017 | ProcessDeclAttributes(NewVD, D); |
Nate Begeman | c8e89a8 | 2008-03-14 18:07:10 +0000 | [diff] [blame] | 1018 | |
Daniel Dunbar | a735ad8 | 2008-08-06 00:03:29 +0000 | [diff] [blame] | 1019 | // Handle GNU asm-label extension (encoded as an attribute). |
| 1020 | if (Expr *E = (Expr*) D.getAsmLabel()) { |
| 1021 | // The parser guarantees this is a string. |
| 1022 | StringLiteral *SE = cast<StringLiteral>(E); |
| 1023 | NewVD->addAttr(new AsmLabelAttr(std::string(SE->getStrData(), |
| 1024 | SE->getByteLength()))); |
| 1025 | } |
| 1026 | |
Nate Begeman | c8e89a8 | 2008-03-14 18:07:10 +0000 | [diff] [blame] | 1027 | // Emit an error if an address space was applied to decl with local storage. |
| 1028 | // This includes arrays of objects with address space qualifiers, but not |
| 1029 | // automatic variables that point to other address spaces. |
| 1030 | // ISO/IEC TR 18037 S5.1.2 |
Nate Begeman | 8e7dafe | 2008-03-25 18:36:32 +0000 | [diff] [blame] | 1031 | if (NewVD->hasLocalStorage() && (NewVD->getType().getAddressSpace() != 0)) { |
| 1032 | Diag(D.getIdentifierLoc(), diag::err_as_qualified_auto_decl); |
| 1033 | InvalidDecl = true; |
Nate Begeman | 5af27e0 | 2008-03-14 00:22:18 +0000 | [diff] [blame] | 1034 | } |
Steve Naroff | ffce4d5 | 2008-01-09 23:34:55 +0000 | [diff] [blame] | 1035 | // Merge the decl with the existing one if appropriate. If the decl is |
| 1036 | // in an outer scope, it isn't the same thing. |
Argyrios Kyrtzidis | 15a12d0 | 2008-09-09 21:18:04 +0000 | [diff] [blame] | 1037 | if (PrevDecl && isDeclInScope(PrevDecl, CurContext, S)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1038 | NewVD = MergeVarDecl(NewVD, PrevDecl); |
| 1039 | if (NewVD == 0) return 0; |
| 1040 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1041 | New = NewVD; |
| 1042 | } |
| 1043 | |
| 1044 | // If this has an identifier, add it to the scope stack. |
Argyrios Kyrtzidis | 87f3ff0 | 2008-04-12 00:47:19 +0000 | [diff] [blame] | 1045 | if (II) |
| 1046 | PushOnScopeChains(New, S); |
Steve Naroff | 5912a35 | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 1047 | // If any semantic error occurred, mark the decl as invalid. |
| 1048 | if (D.getInvalidType() || InvalidDecl) |
| 1049 | New->setInvalidDecl(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1050 | |
| 1051 | return New; |
| 1052 | } |
| 1053 | |
Steve Naroff | 6594a70 | 2008-10-27 11:34:16 +0000 | [diff] [blame] | 1054 | void Sema::InitializerElementNotConstant(const Expr *Init) { |
| 1055 | Diag(Init->getExprLoc(), |
| 1056 | diag::err_init_element_not_constant, Init->getSourceRange()); |
| 1057 | } |
| 1058 | |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1059 | bool Sema::CheckAddressConstantExpressionLValue(const Expr* Init) { |
| 1060 | switch (Init->getStmtClass()) { |
| 1061 | default: |
Steve Naroff | 6594a70 | 2008-10-27 11:34:16 +0000 | [diff] [blame] | 1062 | InitializerElementNotConstant(Init); |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1063 | return true; |
| 1064 | case Expr::ParenExprClass: { |
| 1065 | const ParenExpr* PE = cast<ParenExpr>(Init); |
| 1066 | return CheckAddressConstantExpressionLValue(PE->getSubExpr()); |
| 1067 | } |
| 1068 | case Expr::CompoundLiteralExprClass: |
| 1069 | return cast<CompoundLiteralExpr>(Init)->isFileScope(); |
| 1070 | case Expr::DeclRefExprClass: { |
| 1071 | const Decl *D = cast<DeclRefExpr>(Init)->getDecl(); |
Eli Friedman | 97c0a39 | 2008-05-21 03:39:11 +0000 | [diff] [blame] | 1072 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 1073 | if (VD->hasGlobalStorage()) |
| 1074 | return false; |
Steve Naroff | 6594a70 | 2008-10-27 11:34:16 +0000 | [diff] [blame] | 1075 | InitializerElementNotConstant(Init); |
Eli Friedman | 97c0a39 | 2008-05-21 03:39:11 +0000 | [diff] [blame] | 1076 | return true; |
| 1077 | } |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1078 | if (isa<FunctionDecl>(D)) |
| 1079 | return false; |
Steve Naroff | 6594a70 | 2008-10-27 11:34:16 +0000 | [diff] [blame] | 1080 | InitializerElementNotConstant(Init); |
Steve Naroff | d0091aa | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 1081 | return true; |
| 1082 | } |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1083 | case Expr::MemberExprClass: { |
| 1084 | const MemberExpr *M = cast<MemberExpr>(Init); |
| 1085 | if (M->isArrow()) |
| 1086 | return CheckAddressConstantExpression(M->getBase()); |
| 1087 | return CheckAddressConstantExpressionLValue(M->getBase()); |
| 1088 | } |
| 1089 | case Expr::ArraySubscriptExprClass: { |
| 1090 | // FIXME: Should we pedwarn for "x[0+0]" (where x is a pointer)? |
| 1091 | const ArraySubscriptExpr *ASE = cast<ArraySubscriptExpr>(Init); |
| 1092 | return CheckAddressConstantExpression(ASE->getBase()) || |
| 1093 | CheckArithmeticConstantExpression(ASE->getIdx()); |
| 1094 | } |
| 1095 | case Expr::StringLiteralClass: |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 1096 | case Expr::PredefinedExprClass: |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1097 | return false; |
| 1098 | case Expr::UnaryOperatorClass: { |
| 1099 | const UnaryOperator *Exp = cast<UnaryOperator>(Init); |
| 1100 | |
| 1101 | // C99 6.6p9 |
| 1102 | if (Exp->getOpcode() == UnaryOperator::Deref) |
Eli Friedman | 97c0a39 | 2008-05-21 03:39:11 +0000 | [diff] [blame] | 1103 | return CheckAddressConstantExpression(Exp->getSubExpr()); |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1104 | |
Steve Naroff | 6594a70 | 2008-10-27 11:34:16 +0000 | [diff] [blame] | 1105 | InitializerElementNotConstant(Init); |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1106 | return true; |
| 1107 | } |
| 1108 | } |
| 1109 | } |
| 1110 | |
| 1111 | bool Sema::CheckAddressConstantExpression(const Expr* Init) { |
| 1112 | switch (Init->getStmtClass()) { |
| 1113 | default: |
Steve Naroff | 6594a70 | 2008-10-27 11:34:16 +0000 | [diff] [blame] | 1114 | InitializerElementNotConstant(Init); |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1115 | return true; |
Chris Lattner | 506ff88 | 2008-10-06 07:26:43 +0000 | [diff] [blame] | 1116 | case Expr::ParenExprClass: |
| 1117 | return CheckAddressConstantExpression(cast<ParenExpr>(Init)->getSubExpr()); |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1118 | case Expr::StringLiteralClass: |
| 1119 | case Expr::ObjCStringLiteralClass: |
| 1120 | return false; |
Chris Lattner | 506ff88 | 2008-10-06 07:26:43 +0000 | [diff] [blame] | 1121 | case Expr::CallExprClass: |
| 1122 | // __builtin___CFStringMakeConstantString is a valid constant l-value. |
| 1123 | if (cast<CallExpr>(Init)->isBuiltinCall() == |
| 1124 | Builtin::BI__builtin___CFStringMakeConstantString) |
| 1125 | return false; |
| 1126 | |
Steve Naroff | 6594a70 | 2008-10-27 11:34:16 +0000 | [diff] [blame] | 1127 | InitializerElementNotConstant(Init); |
Chris Lattner | 506ff88 | 2008-10-06 07:26:43 +0000 | [diff] [blame] | 1128 | return true; |
| 1129 | |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1130 | case Expr::UnaryOperatorClass: { |
| 1131 | const UnaryOperator *Exp = cast<UnaryOperator>(Init); |
| 1132 | |
| 1133 | // C99 6.6p9 |
| 1134 | if (Exp->getOpcode() == UnaryOperator::AddrOf) |
| 1135 | return CheckAddressConstantExpressionLValue(Exp->getSubExpr()); |
| 1136 | |
| 1137 | if (Exp->getOpcode() == UnaryOperator::Extension) |
| 1138 | return CheckAddressConstantExpression(Exp->getSubExpr()); |
| 1139 | |
Steve Naroff | 6594a70 | 2008-10-27 11:34:16 +0000 | [diff] [blame] | 1140 | InitializerElementNotConstant(Init); |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1141 | return true; |
| 1142 | } |
| 1143 | case Expr::BinaryOperatorClass: { |
| 1144 | // FIXME: Should we pedwarn for expressions like "a + 1 + 2"? |
| 1145 | const BinaryOperator *Exp = cast<BinaryOperator>(Init); |
| 1146 | |
| 1147 | Expr *PExp = Exp->getLHS(); |
| 1148 | Expr *IExp = Exp->getRHS(); |
| 1149 | if (IExp->getType()->isPointerType()) |
| 1150 | std::swap(PExp, IExp); |
| 1151 | |
| 1152 | // FIXME: Should we pedwarn if IExp isn't an integer constant expression? |
| 1153 | return CheckAddressConstantExpression(PExp) || |
| 1154 | CheckArithmeticConstantExpression(IExp); |
| 1155 | } |
Eli Friedman | c3f0764 | 2008-08-25 20:46:57 +0000 | [diff] [blame] | 1156 | case Expr::ImplicitCastExprClass: |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 1157 | case Expr::CStyleCastExprClass: { |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1158 | const Expr* SubExpr = cast<CastExpr>(Init)->getSubExpr(); |
Eli Friedman | c3f0764 | 2008-08-25 20:46:57 +0000 | [diff] [blame] | 1159 | if (Init->getStmtClass() == Expr::ImplicitCastExprClass) { |
| 1160 | // Check for implicit promotion |
| 1161 | if (SubExpr->getType()->isFunctionType() || |
| 1162 | SubExpr->getType()->isArrayType()) |
| 1163 | return CheckAddressConstantExpressionLValue(SubExpr); |
| 1164 | } |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1165 | |
| 1166 | // Check for pointer->pointer cast |
| 1167 | if (SubExpr->getType()->isPointerType()) |
| 1168 | return CheckAddressConstantExpression(SubExpr); |
| 1169 | |
Eli Friedman | c3f0764 | 2008-08-25 20:46:57 +0000 | [diff] [blame] | 1170 | if (SubExpr->getType()->isIntegralType()) { |
| 1171 | // Check for the special-case of a pointer->int->pointer cast; |
| 1172 | // this isn't standard, but some code requires it. See |
| 1173 | // PR2720 for an example. |
| 1174 | if (const CastExpr* SubCast = dyn_cast<CastExpr>(SubExpr)) { |
| 1175 | if (SubCast->getSubExpr()->getType()->isPointerType()) { |
| 1176 | unsigned IntWidth = Context.getIntWidth(SubCast->getType()); |
| 1177 | unsigned PointerWidth = Context.getTypeSize(Context.VoidPtrTy); |
| 1178 | if (IntWidth >= PointerWidth) { |
| 1179 | return CheckAddressConstantExpression(SubCast->getSubExpr()); |
| 1180 | } |
| 1181 | } |
| 1182 | } |
| 1183 | } |
| 1184 | if (SubExpr->getType()->isArithmeticType()) { |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1185 | return CheckArithmeticConstantExpression(SubExpr); |
Eli Friedman | c3f0764 | 2008-08-25 20:46:57 +0000 | [diff] [blame] | 1186 | } |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1187 | |
Steve Naroff | 6594a70 | 2008-10-27 11:34:16 +0000 | [diff] [blame] | 1188 | InitializerElementNotConstant(Init); |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1189 | return true; |
| 1190 | } |
| 1191 | case Expr::ConditionalOperatorClass: { |
| 1192 | // FIXME: Should we pedwarn here? |
| 1193 | const ConditionalOperator *Exp = cast<ConditionalOperator>(Init); |
| 1194 | if (!Exp->getCond()->getType()->isArithmeticType()) { |
Steve Naroff | 6594a70 | 2008-10-27 11:34:16 +0000 | [diff] [blame] | 1195 | InitializerElementNotConstant(Init); |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1196 | return true; |
| 1197 | } |
| 1198 | if (CheckArithmeticConstantExpression(Exp->getCond())) |
| 1199 | return true; |
| 1200 | if (Exp->getLHS() && |
| 1201 | CheckAddressConstantExpression(Exp->getLHS())) |
| 1202 | return true; |
| 1203 | return CheckAddressConstantExpression(Exp->getRHS()); |
| 1204 | } |
| 1205 | case Expr::AddrLabelExprClass: |
| 1206 | return false; |
| 1207 | } |
| 1208 | } |
| 1209 | |
Eli Friedman | 4caf055 | 2008-06-09 05:05:07 +0000 | [diff] [blame] | 1210 | static const Expr* FindExpressionBaseAddress(const Expr* E); |
| 1211 | |
| 1212 | static const Expr* FindExpressionBaseAddressLValue(const Expr* E) { |
| 1213 | switch (E->getStmtClass()) { |
| 1214 | default: |
| 1215 | return E; |
| 1216 | case Expr::ParenExprClass: { |
| 1217 | const ParenExpr* PE = cast<ParenExpr>(E); |
| 1218 | return FindExpressionBaseAddressLValue(PE->getSubExpr()); |
| 1219 | } |
| 1220 | case Expr::MemberExprClass: { |
| 1221 | const MemberExpr *M = cast<MemberExpr>(E); |
| 1222 | if (M->isArrow()) |
| 1223 | return FindExpressionBaseAddress(M->getBase()); |
| 1224 | return FindExpressionBaseAddressLValue(M->getBase()); |
| 1225 | } |
| 1226 | case Expr::ArraySubscriptExprClass: { |
| 1227 | const ArraySubscriptExpr *ASE = cast<ArraySubscriptExpr>(E); |
| 1228 | return FindExpressionBaseAddress(ASE->getBase()); |
| 1229 | } |
| 1230 | case Expr::UnaryOperatorClass: { |
| 1231 | const UnaryOperator *Exp = cast<UnaryOperator>(E); |
| 1232 | |
| 1233 | if (Exp->getOpcode() == UnaryOperator::Deref) |
| 1234 | return FindExpressionBaseAddress(Exp->getSubExpr()); |
| 1235 | |
| 1236 | return E; |
| 1237 | } |
| 1238 | } |
| 1239 | } |
| 1240 | |
| 1241 | static const Expr* FindExpressionBaseAddress(const Expr* E) { |
| 1242 | switch (E->getStmtClass()) { |
| 1243 | default: |
| 1244 | return E; |
| 1245 | case Expr::ParenExprClass: { |
| 1246 | const ParenExpr* PE = cast<ParenExpr>(E); |
| 1247 | return FindExpressionBaseAddress(PE->getSubExpr()); |
| 1248 | } |
| 1249 | case Expr::UnaryOperatorClass: { |
| 1250 | const UnaryOperator *Exp = cast<UnaryOperator>(E); |
| 1251 | |
| 1252 | // C99 6.6p9 |
| 1253 | if (Exp->getOpcode() == UnaryOperator::AddrOf) |
| 1254 | return FindExpressionBaseAddressLValue(Exp->getSubExpr()); |
| 1255 | |
| 1256 | if (Exp->getOpcode() == UnaryOperator::Extension) |
| 1257 | return FindExpressionBaseAddress(Exp->getSubExpr()); |
| 1258 | |
| 1259 | return E; |
| 1260 | } |
| 1261 | case Expr::BinaryOperatorClass: { |
| 1262 | const BinaryOperator *Exp = cast<BinaryOperator>(E); |
| 1263 | |
| 1264 | Expr *PExp = Exp->getLHS(); |
| 1265 | Expr *IExp = Exp->getRHS(); |
| 1266 | if (IExp->getType()->isPointerType()) |
| 1267 | std::swap(PExp, IExp); |
| 1268 | |
| 1269 | return FindExpressionBaseAddress(PExp); |
| 1270 | } |
| 1271 | case Expr::ImplicitCastExprClass: { |
| 1272 | const Expr* SubExpr = cast<ImplicitCastExpr>(E)->getSubExpr(); |
| 1273 | |
| 1274 | // Check for implicit promotion |
| 1275 | if (SubExpr->getType()->isFunctionType() || |
| 1276 | SubExpr->getType()->isArrayType()) |
| 1277 | return FindExpressionBaseAddressLValue(SubExpr); |
| 1278 | |
| 1279 | // Check for pointer->pointer cast |
| 1280 | if (SubExpr->getType()->isPointerType()) |
| 1281 | return FindExpressionBaseAddress(SubExpr); |
| 1282 | |
| 1283 | // We assume that we have an arithmetic expression here; |
| 1284 | // if we don't, we'll figure it out later |
| 1285 | return 0; |
| 1286 | } |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 1287 | case Expr::CStyleCastExprClass: { |
Eli Friedman | 4caf055 | 2008-06-09 05:05:07 +0000 | [diff] [blame] | 1288 | const Expr* SubExpr = cast<CastExpr>(E)->getSubExpr(); |
| 1289 | |
| 1290 | // Check for pointer->pointer cast |
| 1291 | if (SubExpr->getType()->isPointerType()) |
| 1292 | return FindExpressionBaseAddress(SubExpr); |
| 1293 | |
| 1294 | // We assume that we have an arithmetic expression here; |
| 1295 | // if we don't, we'll figure it out later |
| 1296 | return 0; |
| 1297 | } |
| 1298 | } |
| 1299 | } |
| 1300 | |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1301 | bool Sema::CheckArithmeticConstantExpression(const Expr* Init) { |
| 1302 | switch (Init->getStmtClass()) { |
| 1303 | default: |
Steve Naroff | 6594a70 | 2008-10-27 11:34:16 +0000 | [diff] [blame] | 1304 | InitializerElementNotConstant(Init); |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1305 | return true; |
| 1306 | case Expr::ParenExprClass: { |
| 1307 | const ParenExpr* PE = cast<ParenExpr>(Init); |
| 1308 | return CheckArithmeticConstantExpression(PE->getSubExpr()); |
| 1309 | } |
| 1310 | case Expr::FloatingLiteralClass: |
| 1311 | case Expr::IntegerLiteralClass: |
| 1312 | case Expr::CharacterLiteralClass: |
| 1313 | case Expr::ImaginaryLiteralClass: |
| 1314 | case Expr::TypesCompatibleExprClass: |
| 1315 | case Expr::CXXBoolLiteralExprClass: |
| 1316 | return false; |
| 1317 | case Expr::CallExprClass: { |
| 1318 | const CallExpr *CE = cast<CallExpr>(Init); |
Chris Lattner | 45b6b9d | 2008-10-06 06:49:02 +0000 | [diff] [blame] | 1319 | |
| 1320 | // Allow any constant foldable calls to builtins. |
| 1321 | if (CE->isBuiltinCall() && CE->isEvaluatable(Context)) |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1322 | return false; |
Chris Lattner | 45b6b9d | 2008-10-06 06:49:02 +0000 | [diff] [blame] | 1323 | |
Steve Naroff | 6594a70 | 2008-10-27 11:34:16 +0000 | [diff] [blame] | 1324 | InitializerElementNotConstant(Init); |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1325 | return true; |
| 1326 | } |
| 1327 | case Expr::DeclRefExprClass: { |
| 1328 | const Decl *D = cast<DeclRefExpr>(Init)->getDecl(); |
| 1329 | if (isa<EnumConstantDecl>(D)) |
| 1330 | return false; |
Steve Naroff | 6594a70 | 2008-10-27 11:34:16 +0000 | [diff] [blame] | 1331 | InitializerElementNotConstant(Init); |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1332 | return true; |
| 1333 | } |
| 1334 | case Expr::CompoundLiteralExprClass: |
| 1335 | // Allow "(vector type){2,4}"; normal C constraints don't allow this, |
| 1336 | // but vectors are allowed to be magic. |
| 1337 | if (Init->getType()->isVectorType()) |
| 1338 | return false; |
Steve Naroff | 6594a70 | 2008-10-27 11:34:16 +0000 | [diff] [blame] | 1339 | InitializerElementNotConstant(Init); |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1340 | return true; |
| 1341 | case Expr::UnaryOperatorClass: { |
| 1342 | const UnaryOperator *Exp = cast<UnaryOperator>(Init); |
| 1343 | |
| 1344 | switch (Exp->getOpcode()) { |
| 1345 | // Address, indirect, pre/post inc/dec, etc are not valid constant exprs. |
| 1346 | // See C99 6.6p3. |
| 1347 | default: |
Steve Naroff | 6594a70 | 2008-10-27 11:34:16 +0000 | [diff] [blame] | 1348 | InitializerElementNotConstant(Init); |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1349 | return true; |
| 1350 | case UnaryOperator::SizeOf: |
| 1351 | case UnaryOperator::AlignOf: |
| 1352 | case UnaryOperator::OffsetOf: |
| 1353 | // sizeof(E) is a constantexpr if and only if E is not evaluted. |
| 1354 | // See C99 6.5.3.4p2 and 6.6p3. |
| 1355 | if (Exp->getSubExpr()->getType()->isConstantSizeType()) |
| 1356 | return false; |
Steve Naroff | 6594a70 | 2008-10-27 11:34:16 +0000 | [diff] [blame] | 1357 | InitializerElementNotConstant(Init); |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1358 | return true; |
| 1359 | case UnaryOperator::Extension: |
| 1360 | case UnaryOperator::LNot: |
| 1361 | case UnaryOperator::Plus: |
| 1362 | case UnaryOperator::Minus: |
| 1363 | case UnaryOperator::Not: |
| 1364 | return CheckArithmeticConstantExpression(Exp->getSubExpr()); |
| 1365 | } |
| 1366 | } |
| 1367 | case Expr::SizeOfAlignOfTypeExprClass: { |
| 1368 | const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(Init); |
| 1369 | // Special check for void types, which are allowed as an extension |
| 1370 | if (Exp->getArgumentType()->isVoidType()) |
| 1371 | return false; |
| 1372 | // alignof always evaluates to a constant. |
| 1373 | // FIXME: is sizeof(int[3.0]) a constant expression? |
| 1374 | if (Exp->isSizeOf() && !Exp->getArgumentType()->isConstantSizeType()) { |
Steve Naroff | 6594a70 | 2008-10-27 11:34:16 +0000 | [diff] [blame] | 1375 | InitializerElementNotConstant(Init); |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1376 | return true; |
| 1377 | } |
| 1378 | return false; |
| 1379 | } |
| 1380 | case Expr::BinaryOperatorClass: { |
| 1381 | const BinaryOperator *Exp = cast<BinaryOperator>(Init); |
| 1382 | |
| 1383 | if (Exp->getLHS()->getType()->isArithmeticType() && |
| 1384 | Exp->getRHS()->getType()->isArithmeticType()) { |
| 1385 | return CheckArithmeticConstantExpression(Exp->getLHS()) || |
| 1386 | CheckArithmeticConstantExpression(Exp->getRHS()); |
| 1387 | } |
| 1388 | |
Eli Friedman | 4caf055 | 2008-06-09 05:05:07 +0000 | [diff] [blame] | 1389 | if (Exp->getLHS()->getType()->isPointerType() && |
| 1390 | Exp->getRHS()->getType()->isPointerType()) { |
| 1391 | const Expr* LHSBase = FindExpressionBaseAddress(Exp->getLHS()); |
| 1392 | const Expr* RHSBase = FindExpressionBaseAddress(Exp->getRHS()); |
| 1393 | |
| 1394 | // Only allow a null (constant integer) base; we could |
| 1395 | // allow some additional cases if necessary, but this |
| 1396 | // is sufficient to cover offsetof-like constructs. |
| 1397 | if (!LHSBase && !RHSBase) { |
| 1398 | return CheckAddressConstantExpression(Exp->getLHS()) || |
| 1399 | CheckAddressConstantExpression(Exp->getRHS()); |
| 1400 | } |
| 1401 | } |
| 1402 | |
Steve Naroff | 6594a70 | 2008-10-27 11:34:16 +0000 | [diff] [blame] | 1403 | InitializerElementNotConstant(Init); |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1404 | return true; |
| 1405 | } |
| 1406 | case Expr::ImplicitCastExprClass: |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 1407 | case Expr::CStyleCastExprClass: { |
Argyrios Kyrtzidis | 0835a3c | 2008-08-18 23:01:59 +0000 | [diff] [blame] | 1408 | const Expr *SubExpr = cast<CastExpr>(Init)->getSubExpr(); |
Eli Friedman | 6d4abe1 | 2008-09-01 22:08:17 +0000 | [diff] [blame] | 1409 | if (SubExpr->getType()->isArithmeticType()) |
| 1410 | return CheckArithmeticConstantExpression(SubExpr); |
| 1411 | |
Eli Friedman | b529d83 | 2008-09-02 09:37:00 +0000 | [diff] [blame] | 1412 | if (SubExpr->getType()->isPointerType()) { |
| 1413 | const Expr* Base = FindExpressionBaseAddress(SubExpr); |
| 1414 | // If the pointer has a null base, this is an offsetof-like construct |
| 1415 | if (!Base) |
| 1416 | return CheckAddressConstantExpression(SubExpr); |
| 1417 | } |
| 1418 | |
Steve Naroff | 6594a70 | 2008-10-27 11:34:16 +0000 | [diff] [blame] | 1419 | InitializerElementNotConstant(Init); |
Eli Friedman | 6d4abe1 | 2008-09-01 22:08:17 +0000 | [diff] [blame] | 1420 | return true; |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1421 | } |
| 1422 | case Expr::ConditionalOperatorClass: { |
| 1423 | const ConditionalOperator *Exp = cast<ConditionalOperator>(Init); |
Chris Lattner | 46cfefa | 2008-10-06 05:42:39 +0000 | [diff] [blame] | 1424 | |
| 1425 | // If GNU extensions are disabled, we require all operands to be arithmetic |
| 1426 | // constant expressions. |
| 1427 | if (getLangOptions().NoExtensions) { |
| 1428 | return CheckArithmeticConstantExpression(Exp->getCond()) || |
| 1429 | (Exp->getLHS() && CheckArithmeticConstantExpression(Exp->getLHS())) || |
| 1430 | CheckArithmeticConstantExpression(Exp->getRHS()); |
| 1431 | } |
| 1432 | |
| 1433 | // Otherwise, we have to emulate some of the behavior of fold here. |
| 1434 | // Basically GCC treats things like "4 ? 1 : somefunc()" as a constant |
| 1435 | // because it can constant fold things away. To retain compatibility with |
| 1436 | // GCC code, we see if we can fold the condition to a constant (which we |
| 1437 | // should always be able to do in theory). If so, we only require the |
| 1438 | // specified arm of the conditional to be a constant. This is a horrible |
| 1439 | // hack, but is require by real world code that uses __builtin_constant_p. |
| 1440 | APValue Val; |
| 1441 | if (!Exp->getCond()->tryEvaluate(Val, Context)) { |
| 1442 | // If the tryEvaluate couldn't fold it, CheckArithmeticConstantExpression |
| 1443 | // won't be able to either. Use it to emit the diagnostic though. |
| 1444 | bool Res = CheckArithmeticConstantExpression(Exp->getCond()); |
| 1445 | assert(Res && "tryEvaluate couldn't evaluate this constant?"); |
| 1446 | return Res; |
| 1447 | } |
| 1448 | |
| 1449 | // Verify that the side following the condition is also a constant. |
| 1450 | const Expr *TrueSide = Exp->getLHS(), *FalseSide = Exp->getRHS(); |
| 1451 | if (Val.getInt() == 0) |
| 1452 | std::swap(TrueSide, FalseSide); |
| 1453 | |
| 1454 | if (TrueSide && CheckArithmeticConstantExpression(TrueSide)) |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1455 | return true; |
Chris Lattner | 46cfefa | 2008-10-06 05:42:39 +0000 | [diff] [blame] | 1456 | |
| 1457 | // Okay, the evaluated side evaluates to a constant, so we accept this. |
| 1458 | // Check to see if the other side is obviously not a constant. If so, |
| 1459 | // emit a warning that this is a GNU extension. |
Chris Lattner | 45b6b9d | 2008-10-06 06:49:02 +0000 | [diff] [blame] | 1460 | if (FalseSide && !FalseSide->isEvaluatable(Context)) |
Chris Lattner | 46cfefa | 2008-10-06 05:42:39 +0000 | [diff] [blame] | 1461 | Diag(Init->getExprLoc(), |
| 1462 | diag::ext_typecheck_expression_not_constant_but_accepted, |
| 1463 | FalseSide->getSourceRange()); |
| 1464 | return false; |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1465 | } |
| 1466 | } |
| 1467 | } |
| 1468 | |
| 1469 | bool Sema::CheckForConstantInitializer(Expr *Init, QualType DclT) { |
Nuno Lopes | 9a979c3 | 2008-07-07 16:46:50 +0000 | [diff] [blame] | 1470 | Init = Init->IgnoreParens(); |
| 1471 | |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1472 | // Look through CXXDefaultArgExprs; they have no meaning in this context. |
| 1473 | if (CXXDefaultArgExpr* DAE = dyn_cast<CXXDefaultArgExpr>(Init)) |
| 1474 | return CheckForConstantInitializer(DAE->getExpr(), DclT); |
| 1475 | |
Nuno Lopes | 9a979c3 | 2008-07-07 16:46:50 +0000 | [diff] [blame] | 1476 | if (CompoundLiteralExpr *e = dyn_cast<CompoundLiteralExpr>(Init)) |
| 1477 | return CheckForConstantInitializer(e->getInitializer(), DclT); |
| 1478 | |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1479 | if (InitListExpr *Exp = dyn_cast<InitListExpr>(Init)) { |
| 1480 | unsigned numInits = Exp->getNumInits(); |
| 1481 | for (unsigned i = 0; i < numInits; i++) { |
| 1482 | // FIXME: Need to get the type of the declaration for C++, |
| 1483 | // because it could be a reference? |
| 1484 | if (CheckForConstantInitializer(Exp->getInit(i), |
| 1485 | Exp->getInit(i)->getType())) |
| 1486 | return true; |
| 1487 | } |
| 1488 | return false; |
| 1489 | } |
| 1490 | |
| 1491 | if (Init->isNullPointerConstant(Context)) |
| 1492 | return false; |
| 1493 | if (Init->getType()->isArithmeticType()) { |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 1494 | QualType InitTy = Context.getCanonicalType(Init->getType()) |
| 1495 | .getUnqualifiedType(); |
Eli Friedman | c1cc6dc | 2008-05-30 18:14:48 +0000 | [diff] [blame] | 1496 | if (InitTy == Context.BoolTy) { |
| 1497 | // Special handling for pointers implicitly cast to bool; |
| 1498 | // (e.g. "_Bool rr = &rr;"). This is only legal at the top level. |
| 1499 | if (ImplicitCastExpr* ICE = dyn_cast<ImplicitCastExpr>(Init)) { |
| 1500 | Expr* SubE = ICE->getSubExpr(); |
| 1501 | if (SubE->getType()->isPointerType() || |
| 1502 | SubE->getType()->isArrayType() || |
| 1503 | SubE->getType()->isFunctionType()) { |
| 1504 | return CheckAddressConstantExpression(Init); |
| 1505 | } |
| 1506 | } |
| 1507 | } else if (InitTy->isIntegralType()) { |
| 1508 | Expr* SubE = 0; |
Argyrios Kyrtzidis | 0835a3c | 2008-08-18 23:01:59 +0000 | [diff] [blame] | 1509 | if (CastExpr* CE = dyn_cast<CastExpr>(Init)) |
Eli Friedman | c1cc6dc | 2008-05-30 18:14:48 +0000 | [diff] [blame] | 1510 | SubE = CE->getSubExpr(); |
| 1511 | // Special check for pointer cast to int; we allow as an extension |
| 1512 | // an address constant cast to an integer if the integer |
| 1513 | // is of an appropriate width (this sort of code is apparently used |
| 1514 | // in some places). |
| 1515 | // FIXME: Add pedwarn? |
| 1516 | // FIXME: Don't allow bitfields here! Need the FieldDecl for that. |
| 1517 | if (SubE && (SubE->getType()->isPointerType() || |
| 1518 | SubE->getType()->isArrayType() || |
| 1519 | SubE->getType()->isFunctionType())) { |
| 1520 | unsigned IntWidth = Context.getTypeSize(Init->getType()); |
| 1521 | unsigned PointerWidth = Context.getTypeSize(Context.VoidPtrTy); |
| 1522 | if (IntWidth >= PointerWidth) |
| 1523 | return CheckAddressConstantExpression(Init); |
| 1524 | } |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1525 | } |
| 1526 | |
| 1527 | return CheckArithmeticConstantExpression(Init); |
| 1528 | } |
| 1529 | |
| 1530 | if (Init->getType()->isPointerType()) |
| 1531 | return CheckAddressConstantExpression(Init); |
| 1532 | |
Eli Friedman | c1cc6dc | 2008-05-30 18:14:48 +0000 | [diff] [blame] | 1533 | // An array type at the top level that isn't an init-list must |
| 1534 | // be a string literal |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1535 | if (Init->getType()->isArrayType()) |
| 1536 | return false; |
| 1537 | |
Nuno Lopes | 73419bf | 2008-09-01 18:42:41 +0000 | [diff] [blame] | 1538 | if (Init->getType()->isFunctionType()) |
| 1539 | return false; |
| 1540 | |
Steve Naroff | 8af6a45 | 2008-10-02 17:12:56 +0000 | [diff] [blame] | 1541 | // Allow block exprs at top level. |
| 1542 | if (Init->getType()->isBlockPointerType()) |
| 1543 | return false; |
| 1544 | |
Steve Naroff | 6594a70 | 2008-10-27 11:34:16 +0000 | [diff] [blame] | 1545 | InitializerElementNotConstant(Init); |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1546 | return true; |
Steve Naroff | d0091aa | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 1547 | } |
| 1548 | |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 1549 | void Sema::AddInitializerToDecl(DeclTy *dcl, ExprTy *init) { |
Steve Naroff | 410e3e2 | 2007-09-12 20:13:48 +0000 | [diff] [blame] | 1550 | Decl *RealDecl = static_cast<Decl *>(dcl); |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 1551 | Expr *Init = static_cast<Expr *>(init); |
Chris Lattner | 9a11b9a | 2007-10-19 20:10:30 +0000 | [diff] [blame] | 1552 | assert(Init && "missing initializer"); |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 1553 | |
Chris Lattner | 9a11b9a | 2007-10-19 20:10:30 +0000 | [diff] [blame] | 1554 | // If there is no declaration, there was an error parsing it. Just ignore |
| 1555 | // the initializer. |
| 1556 | if (RealDecl == 0) { |
| 1557 | delete Init; |
| 1558 | return; |
| 1559 | } |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 1560 | |
Steve Naroff | 410e3e2 | 2007-09-12 20:13:48 +0000 | [diff] [blame] | 1561 | VarDecl *VDecl = dyn_cast<VarDecl>(RealDecl); |
| 1562 | if (!VDecl) { |
Steve Naroff | 8e74c93 | 2007-09-13 21:41:19 +0000 | [diff] [blame] | 1563 | Diag(dyn_cast<ScopedDecl>(RealDecl)->getLocation(), |
| 1564 | diag::err_illegal_initializer); |
Steve Naroff | 410e3e2 | 2007-09-12 20:13:48 +0000 | [diff] [blame] | 1565 | RealDecl->setInvalidDecl(); |
| 1566 | return; |
| 1567 | } |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 1568 | // Get the decls type and save a reference for later, since |
Steve Naroff | d0091aa | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 1569 | // CheckInitializerTypes may change it. |
Steve Naroff | 410e3e2 | 2007-09-12 20:13:48 +0000 | [diff] [blame] | 1570 | QualType DclT = VDecl->getType(), SavT = DclT; |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 1571 | if (VDecl->isBlockVarDecl()) { |
| 1572 | VarDecl::StorageClass SC = VDecl->getStorageClass(); |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 1573 | if (SC == VarDecl::Extern) { // C99 6.7.8p5 |
Steve Naroff | 410e3e2 | 2007-09-12 20:13:48 +0000 | [diff] [blame] | 1574 | Diag(VDecl->getLocation(), diag::err_block_extern_cant_init); |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 1575 | VDecl->setInvalidDecl(); |
| 1576 | } else if (!VDecl->isInvalidDecl()) { |
Steve Naroff | a996033 | 2008-01-25 00:51:06 +0000 | [diff] [blame] | 1577 | if (CheckInitializerTypes(Init, DclT)) |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 1578 | VDecl->setInvalidDecl(); |
Anders Carlsson | c5eb731 | 2008-08-22 05:00:02 +0000 | [diff] [blame] | 1579 | |
| 1580 | // C++ 3.6.2p2, allow dynamic initialization of static initializers. |
| 1581 | if (!getLangOptions().CPlusPlus) { |
| 1582 | if (SC == VarDecl::Static) // C99 6.7.8p4. |
| 1583 | CheckForConstantInitializer(Init, DclT); |
| 1584 | } |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 1585 | } |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 1586 | } else if (VDecl->isFileVarDecl()) { |
| 1587 | if (VDecl->getStorageClass() == VarDecl::Extern) |
Steve Naroff | 410e3e2 | 2007-09-12 20:13:48 +0000 | [diff] [blame] | 1588 | Diag(VDecl->getLocation(), diag::warn_extern_init); |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 1589 | if (!VDecl->isInvalidDecl()) |
Steve Naroff | a996033 | 2008-01-25 00:51:06 +0000 | [diff] [blame] | 1590 | if (CheckInitializerTypes(Init, DclT)) |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 1591 | VDecl->setInvalidDecl(); |
Steve Naroff | d0091aa | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 1592 | |
Anders Carlsson | c5eb731 | 2008-08-22 05:00:02 +0000 | [diff] [blame] | 1593 | // C++ 3.6.2p2, allow dynamic initialization of static initializers. |
| 1594 | if (!getLangOptions().CPlusPlus) { |
| 1595 | // C99 6.7.8p4. All file scoped initializers need to be constant. |
| 1596 | CheckForConstantInitializer(Init, DclT); |
| 1597 | } |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 1598 | } |
| 1599 | // If the type changed, it means we had an incomplete type that was |
| 1600 | // completed by the initializer. For example: |
| 1601 | // int ary[] = { 1, 3, 5 }; |
| 1602 | // "ary" transitions from a VariableArrayType to a ConstantArrayType. |
Christopher Lamb | 48b1239 | 2007-11-29 19:09:19 +0000 | [diff] [blame] | 1603 | if (!VDecl->isInvalidDecl() && (DclT != SavT)) { |
Steve Naroff | 410e3e2 | 2007-09-12 20:13:48 +0000 | [diff] [blame] | 1604 | VDecl->setType(DclT); |
Christopher Lamb | 48b1239 | 2007-11-29 19:09:19 +0000 | [diff] [blame] | 1605 | Init->setType(DclT); |
| 1606 | } |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 1607 | |
| 1608 | // Attach the initializer to the decl. |
Steve Naroff | 410e3e2 | 2007-09-12 20:13:48 +0000 | [diff] [blame] | 1609 | VDecl->setInit(Init); |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 1610 | return; |
| 1611 | } |
| 1612 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1613 | void Sema::ActOnUninitializedDecl(DeclTy *dcl) { |
| 1614 | Decl *RealDecl = static_cast<Decl *>(dcl); |
| 1615 | |
| 1616 | if (VarDecl *Var = dyn_cast<VarDecl>(RealDecl)) { |
| 1617 | QualType Type = Var->getType(); |
| 1618 | // C++ [dcl.init.ref]p3: |
| 1619 | // The initializer can be omitted for a reference only in a |
| 1620 | // parameter declaration (8.3.5), in the declaration of a |
| 1621 | // function return type, in the declaration of a class member |
| 1622 | // within its class declaration (9.2), and where the extern |
| 1623 | // specifier is explicitly used. |
| 1624 | if (Type->isReferenceType() && Var->getStorageClass() != VarDecl::Extern) |
| 1625 | Diag(Var->getLocation(), |
| 1626 | diag::err_reference_var_requires_init, |
| 1627 | Var->getName(), |
| 1628 | SourceRange(Var->getLocation(), Var->getLocation())); |
| 1629 | |
Douglas Gregor | 818ce48 | 2008-10-29 13:50:18 +0000 | [diff] [blame] | 1630 | #if 0 |
| 1631 | // FIXME: Temporarily disabled because we are not properly parsing |
| 1632 | // linkage specifications on declarations, e.g., |
| 1633 | // |
| 1634 | // extern "C" const CGPoint CGPointerZero; |
| 1635 | // |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1636 | // C++ [dcl.init]p9: |
| 1637 | // |
| 1638 | // If no initializer is specified for an object, and the |
| 1639 | // object is of (possibly cv-qualified) non-POD class type (or |
| 1640 | // array thereof), the object shall be default-initialized; if |
| 1641 | // the object is of const-qualified type, the underlying class |
| 1642 | // type shall have a user-declared default |
| 1643 | // constructor. Otherwise, if no initializer is specified for |
| 1644 | // an object, the object and its subobjects, if any, have an |
| 1645 | // indeterminate initial value; if the object or any of its |
| 1646 | // subobjects are of const-qualified type, the program is |
| 1647 | // ill-formed. |
| 1648 | // |
| 1649 | // This isn't technically an error in C, so we don't diagnose it. |
| 1650 | // |
| 1651 | // FIXME: Actually perform the POD/user-defined default |
| 1652 | // constructor check. |
| 1653 | if (getLangOptions().CPlusPlus && |
Douglas Gregor | 818ce48 | 2008-10-29 13:50:18 +0000 | [diff] [blame] | 1654 | Context.getCanonicalType(Type).isConstQualified() && |
| 1655 | Var->getStorageClass() != VarDecl::Extern) |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1656 | Diag(Var->getLocation(), |
| 1657 | diag::err_const_var_requires_init, |
| 1658 | Var->getName(), |
| 1659 | SourceRange(Var->getLocation(), Var->getLocation())); |
Douglas Gregor | 818ce48 | 2008-10-29 13:50:18 +0000 | [diff] [blame] | 1660 | #endif |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1661 | } |
| 1662 | } |
| 1663 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1664 | /// The declarators are chained together backwards, reverse the list. |
| 1665 | Sema::DeclTy *Sema::FinalizeDeclaratorGroup(Scope *S, DeclTy *group) { |
| 1666 | // Often we have single declarators, handle them quickly. |
Steve Naroff | 9474504 | 2007-09-13 23:52:58 +0000 | [diff] [blame] | 1667 | Decl *GroupDecl = static_cast<Decl*>(group); |
| 1668 | if (GroupDecl == 0) |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 1669 | return 0; |
Steve Naroff | 9474504 | 2007-09-13 23:52:58 +0000 | [diff] [blame] | 1670 | |
| 1671 | ScopedDecl *Group = dyn_cast<ScopedDecl>(GroupDecl); |
| 1672 | ScopedDecl *NewGroup = 0; |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 1673 | if (Group->getNextDeclarator() == 0) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1674 | NewGroup = Group; |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 1675 | else { // reverse the list. |
| 1676 | while (Group) { |
Steve Naroff | 9474504 | 2007-09-13 23:52:58 +0000 | [diff] [blame] | 1677 | ScopedDecl *Next = Group->getNextDeclarator(); |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 1678 | Group->setNextDeclarator(NewGroup); |
| 1679 | NewGroup = Group; |
| 1680 | Group = Next; |
| 1681 | } |
| 1682 | } |
| 1683 | // Perform semantic analysis that depends on having fully processed both |
| 1684 | // the declarator and initializer. |
Steve Naroff | 9474504 | 2007-09-13 23:52:58 +0000 | [diff] [blame] | 1685 | for (ScopedDecl *ID = NewGroup; ID; ID = ID->getNextDeclarator()) { |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 1686 | VarDecl *IDecl = dyn_cast<VarDecl>(ID); |
| 1687 | if (!IDecl) |
| 1688 | continue; |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 1689 | QualType T = IDecl->getType(); |
| 1690 | |
| 1691 | // C99 6.7.5.2p2: If an identifier is declared to be an object with |
| 1692 | // static storage duration, it shall not have a variable length array. |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 1693 | if ((IDecl->isFileVarDecl() || IDecl->isBlockVarDecl()) && |
| 1694 | IDecl->getStorageClass() == VarDecl::Static) { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 1695 | if (T->isVariableArrayType()) { |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1696 | Diag(IDecl->getLocation(), diag::err_typecheck_illegal_vla); |
| 1697 | IDecl->setInvalidDecl(); |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 1698 | } |
| 1699 | } |
| 1700 | // Block scope. C99 6.7p7: If an identifier for an object is declared with |
| 1701 | // 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] | 1702 | if (IDecl->isBlockVarDecl() && |
| 1703 | IDecl->getStorageClass() != VarDecl::Extern) { |
Chris Lattner | fd89bc8 | 2008-04-02 01:05:10 +0000 | [diff] [blame] | 1704 | if (T->isIncompleteType() && !IDecl->isInvalidDecl()) { |
Chris Lattner | 8b1be77 | 2007-12-02 07:50:03 +0000 | [diff] [blame] | 1705 | Diag(IDecl->getLocation(), diag::err_typecheck_decl_incomplete_type, |
| 1706 | T.getAsString()); |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 1707 | IDecl->setInvalidDecl(); |
| 1708 | } |
| 1709 | } |
| 1710 | // File scope. C99 6.9.2p2: A declaration of an identifier for and |
| 1711 | // object that has file scope without an initializer, and without a |
| 1712 | // storage-class specifier or with the storage-class specifier "static", |
| 1713 | // constitutes a tentative definition. Note: A tentative definition with |
| 1714 | // external linkage is valid (C99 6.2.2p5). |
Steve Naroff | ff9eb1f | 2008-08-08 17:50:35 +0000 | [diff] [blame] | 1715 | if (isTentativeDefinition(IDecl)) { |
Eli Friedman | 9db1397 | 2008-02-15 12:53:51 +0000 | [diff] [blame] | 1716 | if (T->isIncompleteArrayType()) { |
Steve Naroff | 9a75f8a | 2008-01-18 20:40:52 +0000 | [diff] [blame] | 1717 | // C99 6.9.2 (p2, p5): Implicit initialization causes an incomplete |
| 1718 | // array to be completed. Don't issue a diagnostic. |
Chris Lattner | fd89bc8 | 2008-04-02 01:05:10 +0000 | [diff] [blame] | 1719 | } else if (T->isIncompleteType() && !IDecl->isInvalidDecl()) { |
Steve Naroff | 9a75f8a | 2008-01-18 20:40:52 +0000 | [diff] [blame] | 1720 | // C99 6.9.2p3: If the declaration of an identifier for an object is |
| 1721 | // a tentative definition and has internal linkage (C99 6.2.2p3), the |
| 1722 | // declared type shall not be an incomplete type. |
Chris Lattner | 8b1be77 | 2007-12-02 07:50:03 +0000 | [diff] [blame] | 1723 | Diag(IDecl->getLocation(), diag::err_typecheck_decl_incomplete_type, |
| 1724 | T.getAsString()); |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 1725 | IDecl->setInvalidDecl(); |
| 1726 | } |
| 1727 | } |
Steve Naroff | ff9eb1f | 2008-08-08 17:50:35 +0000 | [diff] [blame] | 1728 | if (IDecl->isFileVarDecl()) |
| 1729 | CheckForFileScopedRedefinitions(S, IDecl); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1730 | } |
| 1731 | return NewGroup; |
| 1732 | } |
Steve Naroff | e1223f7 | 2007-08-28 03:03:08 +0000 | [diff] [blame] | 1733 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 1734 | /// ActOnParamDeclarator - Called from Parser::ParseFunctionDeclarator() |
| 1735 | /// to introduce parameters into function prototype scope. |
| 1736 | Sema::DeclTy * |
| 1737 | Sema::ActOnParamDeclarator(Scope *S, Declarator &D) { |
Chris Lattner | 985abd9 | 2008-06-26 06:49:43 +0000 | [diff] [blame] | 1738 | const DeclSpec &DS = D.getDeclSpec(); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 1739 | |
| 1740 | // Verify C99 6.7.5.3p2: The only SCS allowed is 'register'. |
Daniel Dunbar | 33ad012 | 2008-09-03 21:54:21 +0000 | [diff] [blame] | 1741 | VarDecl::StorageClass StorageClass = VarDecl::None; |
| 1742 | if (DS.getStorageClassSpec() == DeclSpec::SCS_register) { |
| 1743 | StorageClass = VarDecl::Register; |
| 1744 | } else if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified) { |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 1745 | Diag(DS.getStorageClassSpecLoc(), |
| 1746 | diag::err_invalid_storage_class_in_func_decl); |
Chris Lattner | 985abd9 | 2008-06-26 06:49:43 +0000 | [diff] [blame] | 1747 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 1748 | } |
| 1749 | if (DS.isThreadSpecified()) { |
| 1750 | Diag(DS.getThreadSpecLoc(), |
| 1751 | diag::err_invalid_storage_class_in_func_decl); |
Chris Lattner | 985abd9 | 2008-06-26 06:49:43 +0000 | [diff] [blame] | 1752 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 1753 | } |
| 1754 | |
Douglas Gregor | 6d6eb57 | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 1755 | // Check that there are no default arguments inside the type of this |
| 1756 | // parameter (C++ only). |
| 1757 | if (getLangOptions().CPlusPlus) |
| 1758 | CheckExtraCXXDefaultArguments(D); |
| 1759 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 1760 | // In this context, we *do not* check D.getInvalidType(). If the declarator |
| 1761 | // type was invalid, GetTypeForDeclarator() still returns a "valid" type, |
| 1762 | // though it will not reflect the user specified type. |
| 1763 | QualType parmDeclType = GetTypeForDeclarator(D, S); |
| 1764 | |
| 1765 | assert(!parmDeclType.isNull() && "GetTypeForDeclarator() returned null type"); |
| 1766 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1767 | // TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope. |
| 1768 | // Can this happen for params? We already checked that they don't conflict |
| 1769 | // among each other. Here they can only shadow globals, which is ok. |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 1770 | IdentifierInfo *II = D.getIdentifier(); |
| 1771 | if (Decl *PrevDecl = LookupDecl(II, Decl::IDNS_Ordinary, S)) { |
| 1772 | if (S->isDeclScope(PrevDecl)) { |
| 1773 | Diag(D.getIdentifierLoc(), diag::err_param_redefinition, |
| 1774 | dyn_cast<NamedDecl>(PrevDecl)->getName()); |
| 1775 | |
| 1776 | // Recover by removing the name |
| 1777 | II = 0; |
| 1778 | D.SetIdentifier(0, D.getIdentifierLoc()); |
| 1779 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1780 | } |
Steve Naroff | 6a9f3e3 | 2007-08-07 22:44:21 +0000 | [diff] [blame] | 1781 | |
| 1782 | // Perform the default function/array conversion (C99 6.7.5.3p[7,8]). |
| 1783 | // Doing the promotion here has a win and a loss. The win is the type for |
| 1784 | // both Decl's and DeclRefExpr's will match (a convenient invariant for the |
| 1785 | // code generator). The loss is the orginal type isn't preserved. For example: |
| 1786 | // |
| 1787 | // void func(int parmvardecl[5]) { // convert "int [5]" to "int *" |
| 1788 | // int blockvardecl[5]; |
| 1789 | // sizeof(parmvardecl); // size == 4 |
| 1790 | // sizeof(blockvardecl); // size == 20 |
| 1791 | // } |
| 1792 | // |
| 1793 | // For expressions, all implicit conversions are captured using the |
| 1794 | // ImplicitCastExpr AST node (we have no such mechanism for Decl's). |
| 1795 | // |
| 1796 | // FIXME: If a source translation tool needs to see the original type, then |
| 1797 | // we need to consider storing both types (in ParmVarDecl)... |
| 1798 | // |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 1799 | if (parmDeclType->isArrayType()) { |
Chris Lattner | 529bd02 | 2008-01-02 22:50:48 +0000 | [diff] [blame] | 1800 | // int x[restrict 4] -> int *restrict |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 1801 | parmDeclType = Context.getArrayDecayedType(parmDeclType); |
Chris Lattner | 529bd02 | 2008-01-02 22:50:48 +0000 | [diff] [blame] | 1802 | } else if (parmDeclType->isFunctionType()) |
Steve Naroff | 6a9f3e3 | 2007-08-07 22:44:21 +0000 | [diff] [blame] | 1803 | parmDeclType = Context.getPointerType(parmDeclType); |
| 1804 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 1805 | ParmVarDecl *New = ParmVarDecl::Create(Context, CurContext, |
| 1806 | D.getIdentifierLoc(), II, |
Daniel Dunbar | 33ad012 | 2008-09-03 21:54:21 +0000 | [diff] [blame] | 1807 | parmDeclType, StorageClass, |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 1808 | 0, 0); |
Anders Carlsson | f78915f | 2008-02-15 07:04:12 +0000 | [diff] [blame] | 1809 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 1810 | if (D.getInvalidType()) |
Steve Naroff | 53a3234 | 2007-08-28 18:45:29 +0000 | [diff] [blame] | 1811 | New->setInvalidDecl(); |
| 1812 | |
Argyrios Kyrtzidis | 87f3ff0 | 2008-04-12 00:47:19 +0000 | [diff] [blame] | 1813 | if (II) |
| 1814 | PushOnScopeChains(New, S); |
Nate Begeman | b7894b5 | 2008-02-17 21:20:31 +0000 | [diff] [blame] | 1815 | |
Chris Lattner | 3ff30c8 | 2008-06-29 00:02:00 +0000 | [diff] [blame] | 1816 | ProcessDeclAttributes(New, D); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1817 | return New; |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 1818 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1819 | } |
Fariborz Jahanian | 306d68f | 2007-11-08 23:49:49 +0000 | [diff] [blame] | 1820 | |
Chris Lattner | b652cea | 2007-10-09 17:14:05 +0000 | [diff] [blame] | 1821 | Sema::DeclTy *Sema::ActOnStartOfFunctionDef(Scope *FnBodyScope, Declarator &D) { |
Argyrios Kyrtzidis | 53d0ea5 | 2008-06-28 06:07:14 +0000 | [diff] [blame] | 1822 | assert(getCurFunctionDecl() == 0 && "Function parsing confused"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1823 | assert(D.getTypeObject(0).Kind == DeclaratorChunk::Function && |
| 1824 | "Not a function declarator!"); |
| 1825 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun; |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 1826 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1827 | // Verify 6.9.1p6: 'every identifier in the identifier list shall be declared' |
| 1828 | // for a K&R function. |
| 1829 | if (!FTI.hasPrototype) { |
| 1830 | for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) { |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 1831 | if (FTI.ArgInfo[i].Param == 0) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1832 | Diag(FTI.ArgInfo[i].IdentLoc, diag::ext_param_not_declared, |
| 1833 | FTI.ArgInfo[i].Ident->getName()); |
| 1834 | // Implicitly declare the argument as type 'int' for lack of a better |
| 1835 | // type. |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 1836 | DeclSpec DS; |
| 1837 | const char* PrevSpec; // unused |
| 1838 | DS.SetTypeSpecType(DeclSpec::TST_int, FTI.ArgInfo[i].IdentLoc, |
| 1839 | PrevSpec); |
| 1840 | Declarator ParamD(DS, Declarator::KNRTypeListContext); |
| 1841 | ParamD.SetIdentifier(FTI.ArgInfo[i].Ident, FTI.ArgInfo[i].IdentLoc); |
| 1842 | FTI.ArgInfo[i].Param = ActOnParamDeclarator(FnBodyScope, ParamD); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1843 | } |
| 1844 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1845 | } else { |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 1846 | // FIXME: Diagnose arguments without names in C. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1847 | } |
| 1848 | |
| 1849 | Scope *GlobalScope = FnBodyScope->getParent(); |
Steve Naroff | adbbd0c | 2008-01-14 20:51:29 +0000 | [diff] [blame] | 1850 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1851 | return ActOnStartOfFunctionDef(FnBodyScope, |
Daniel Dunbar | 914701e | 2008-08-05 16:28:08 +0000 | [diff] [blame] | 1852 | ActOnDeclarator(GlobalScope, D, 0)); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1853 | } |
| 1854 | |
| 1855 | Sema::DeclTy *Sema::ActOnStartOfFunctionDef(Scope *FnBodyScope, DeclTy *D) { |
| 1856 | Decl *decl = static_cast<Decl*>(D); |
Chris Lattner | e9ba323 | 2008-02-16 01:20:36 +0000 | [diff] [blame] | 1857 | FunctionDecl *FD = cast<FunctionDecl>(decl); |
Douglas Gregor | 6fc17ff | 2008-10-29 15:10:40 +0000 | [diff] [blame] | 1858 | |
| 1859 | // See if this is a redefinition. |
| 1860 | const FunctionDecl *Definition; |
| 1861 | if (FD->getBody(Definition)) { |
| 1862 | Diag(FD->getLocation(), diag::err_redefinition, |
| 1863 | FD->getName()); |
| 1864 | Diag(Definition->getLocation(), diag::err_previous_definition); |
| 1865 | } |
| 1866 | |
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 1867 | PushDeclContext(FD); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 1868 | |
| 1869 | // Check the validity of our function parameters |
| 1870 | CheckParmsForFunctionDef(FD); |
| 1871 | |
| 1872 | // Introduce our parameters into the function scope |
| 1873 | for (unsigned p = 0, NumParams = FD->getNumParams(); p < NumParams; ++p) { |
| 1874 | ParmVarDecl *Param = FD->getParamDecl(p); |
| 1875 | // If this has an identifier, add it to the scope stack. |
Argyrios Kyrtzidis | 87f3ff0 | 2008-04-12 00:47:19 +0000 | [diff] [blame] | 1876 | if (Param->getIdentifier()) |
| 1877 | PushOnScopeChains(Param, FnBodyScope); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1878 | } |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 1879 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1880 | return FD; |
| 1881 | } |
| 1882 | |
Steve Naroff | d6d054d | 2007-11-11 23:20:51 +0000 | [diff] [blame] | 1883 | Sema::DeclTy *Sema::ActOnFinishFunctionBody(DeclTy *D, StmtTy *Body) { |
| 1884 | Decl *dcl = static_cast<Decl *>(D); |
Steve Naroff | 394f3f4 | 2008-07-25 17:57:26 +0000 | [diff] [blame] | 1885 | if (FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(dcl)) { |
Steve Naroff | d6d054d | 2007-11-11 23:20:51 +0000 | [diff] [blame] | 1886 | FD->setBody((Stmt*)Body); |
Argyrios Kyrtzidis | 53d0ea5 | 2008-06-28 06:07:14 +0000 | [diff] [blame] | 1887 | assert(FD == getCurFunctionDecl() && "Function parsing confused"); |
Steve Naroff | 394f3f4 | 2008-07-25 17:57:26 +0000 | [diff] [blame] | 1888 | } else if (ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(dcl)) { |
Steve Naroff | d6d054d | 2007-11-11 23:20:51 +0000 | [diff] [blame] | 1889 | MD->setBody((Stmt*)Body); |
Steve Naroff | 394f3f4 | 2008-07-25 17:57:26 +0000 | [diff] [blame] | 1890 | } else |
| 1891 | return 0; |
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 1892 | PopDeclContext(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1893 | // Verify and clean out per-function state. |
| 1894 | |
| 1895 | // Check goto/label use. |
| 1896 | for (llvm::DenseMap<IdentifierInfo*, LabelStmt*>::iterator |
| 1897 | I = LabelMap.begin(), E = LabelMap.end(); I != E; ++I) { |
| 1898 | // Verify that we have no forward references left. If so, there was a goto |
| 1899 | // or address of a label taken, but no definition of it. Label fwd |
| 1900 | // definitions are indicated with a null substmt. |
| 1901 | if (I->second->getSubStmt() == 0) { |
| 1902 | LabelStmt *L = I->second; |
| 1903 | // Emit error. |
| 1904 | Diag(L->getIdentLoc(), diag::err_undeclared_label_use, L->getName()); |
| 1905 | |
| 1906 | // At this point, we have gotos that use the bogus label. Stitch it into |
| 1907 | // the function body so that they aren't leaked and that the AST is well |
| 1908 | // formed. |
Chris Lattner | 0cbc215 | 2008-01-25 00:01:10 +0000 | [diff] [blame] | 1909 | if (Body) { |
| 1910 | L->setSubStmt(new NullStmt(L->getIdentLoc())); |
| 1911 | cast<CompoundStmt>((Stmt*)Body)->push_back(L); |
| 1912 | } else { |
| 1913 | // The whole function wasn't parsed correctly, just delete this. |
| 1914 | delete L; |
| 1915 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1916 | } |
| 1917 | } |
| 1918 | LabelMap.clear(); |
| 1919 | |
Steve Naroff | d6d054d | 2007-11-11 23:20:51 +0000 | [diff] [blame] | 1920 | return D; |
Fariborz Jahanian | 60fbca0 | 2007-11-10 16:31:34 +0000 | [diff] [blame] | 1921 | } |
| 1922 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1923 | /// ImplicitlyDefineFunction - An undeclared identifier was used in a function |
| 1924 | /// 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] | 1925 | ScopedDecl *Sema::ImplicitlyDefineFunction(SourceLocation Loc, |
| 1926 | IdentifierInfo &II, Scope *S) { |
Chris Lattner | 37d1084 | 2008-05-05 21:18:06 +0000 | [diff] [blame] | 1927 | // Extension in C99. Legal in C90, but warn about it. |
| 1928 | if (getLangOptions().C99) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1929 | Diag(Loc, diag::ext_implicit_function_decl, II.getName()); |
Chris Lattner | 37d1084 | 2008-05-05 21:18:06 +0000 | [diff] [blame] | 1930 | else |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1931 | Diag(Loc, diag::warn_implicit_function_decl, II.getName()); |
| 1932 | |
| 1933 | // FIXME: handle stuff like: |
| 1934 | // void foo() { extern float X(); } |
| 1935 | // void bar() { X(); } <-- implicit decl for X in another scope. |
| 1936 | |
| 1937 | // Set a Declarator for the implicit definition: int foo(); |
| 1938 | const char *Dummy; |
| 1939 | DeclSpec DS; |
| 1940 | bool Error = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, Dummy); |
| 1941 | Error = Error; // Silence warning. |
| 1942 | assert(!Error && "Error setting up implicit decl!"); |
| 1943 | Declarator D(DS, Declarator::BlockContext); |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 1944 | D.AddTypeInfo(DeclaratorChunk::getFunction(false, false, 0, 0, 0, Loc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1945 | D.SetIdentifier(&II, Loc); |
| 1946 | |
Argyrios Kyrtzidis | 93213bb | 2008-05-01 21:04:16 +0000 | [diff] [blame] | 1947 | // Insert this function into translation-unit scope. |
| 1948 | |
| 1949 | DeclContext *PrevDC = CurContext; |
| 1950 | CurContext = Context.getTranslationUnitDecl(); |
| 1951 | |
Steve Naroff | e2ef815 | 2008-04-04 14:32:09 +0000 | [diff] [blame] | 1952 | FunctionDecl *FD = |
Daniel Dunbar | 914701e | 2008-08-05 16:28:08 +0000 | [diff] [blame] | 1953 | dyn_cast<FunctionDecl>(static_cast<Decl*>(ActOnDeclarator(TUScope, D, 0))); |
Steve Naroff | e2ef815 | 2008-04-04 14:32:09 +0000 | [diff] [blame] | 1954 | FD->setImplicit(); |
Argyrios Kyrtzidis | 93213bb | 2008-05-01 21:04:16 +0000 | [diff] [blame] | 1955 | |
| 1956 | CurContext = PrevDC; |
| 1957 | |
Steve Naroff | e2ef815 | 2008-04-04 14:32:09 +0000 | [diff] [blame] | 1958 | return FD; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1959 | } |
| 1960 | |
| 1961 | |
Chris Lattner | 41af093 | 2007-11-14 06:34:38 +0000 | [diff] [blame] | 1962 | TypedefDecl *Sema::ParseTypedefDecl(Scope *S, Declarator &D, QualType T, |
Steve Naroff | 9474504 | 2007-09-13 23:52:58 +0000 | [diff] [blame] | 1963 | ScopedDecl *LastDeclarator) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1964 | assert(D.getIdentifier() && "Wrong callback for declspec without declarator"); |
Steve Naroff | 5912a35 | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 1965 | assert(!T.isNull() && "GetTypeForDeclarator() returned null type"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1966 | |
| 1967 | // Scope manipulation handled by caller. |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1968 | TypedefDecl *NewTD = TypedefDecl::Create(Context, CurContext, |
| 1969 | D.getIdentifierLoc(), |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 1970 | D.getIdentifier(), |
Chris Lattner | c63e660 | 2008-03-15 21:32:50 +0000 | [diff] [blame] | 1971 | T, LastDeclarator); |
Steve Naroff | 5912a35 | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 1972 | if (D.getInvalidType()) |
| 1973 | NewTD->setInvalidDecl(); |
| 1974 | return NewTD; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1975 | } |
| 1976 | |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 1977 | /// 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] | 1978 | /// former case, Name will be non-null. In the later case, Name will be null. |
| 1979 | /// TagType indicates what kind of tag this is. TK indicates whether this is a |
| 1980 | /// reference/declaration/definition of a tag. |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 1981 | Sema::DeclTy *Sema::ActOnTag(Scope *S, unsigned TagType, TagKind TK, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1982 | SourceLocation KWLoc, IdentifierInfo *Name, |
| 1983 | SourceLocation NameLoc, AttributeList *Attr) { |
| 1984 | // If this is a use of an existing tag, it must have a name. |
| 1985 | assert((Name != 0 || TK == TK_Definition) && |
| 1986 | "Nameless record must be a definition!"); |
| 1987 | |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 1988 | TagDecl::TagKind Kind; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1989 | switch (TagType) { |
| 1990 | default: assert(0 && "Unknown tag type!"); |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 1991 | case DeclSpec::TST_struct: Kind = TagDecl::TK_struct; break; |
| 1992 | case DeclSpec::TST_union: Kind = TagDecl::TK_union; break; |
| 1993 | case DeclSpec::TST_class: Kind = TagDecl::TK_class; break; |
| 1994 | case DeclSpec::TST_enum: Kind = TagDecl::TK_enum; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1995 | } |
| 1996 | |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 1997 | // Two code paths: a new one for structs/unions/classes where we create |
| 1998 | // separate decls for forward declarations, and an old (eventually to |
| 1999 | // be removed) code path for enums. |
| 2000 | if (Kind != TagDecl::TK_enum) |
| 2001 | return ActOnTagStruct(S, Kind, TK, KWLoc, Name, NameLoc, Attr); |
| 2002 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2003 | // If this is a named struct, check to see if there was a previous forward |
| 2004 | // declaration or definition. |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 2005 | // Use ScopedDecl instead of TagDecl, because a NamespaceDecl may come up. |
Ted Kremenek | 7e8cc57 | 2008-09-02 21:26:19 +0000 | [diff] [blame] | 2006 | ScopedDecl *PrevDecl = |
| 2007 | dyn_cast_or_null<ScopedDecl>(LookupDecl(Name, Decl::IDNS_Tag, S)); |
| 2008 | |
| 2009 | if (PrevDecl) { |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 2010 | assert((isa<TagDecl>(PrevDecl) || isa<NamespaceDecl>(PrevDecl)) && |
| 2011 | "unexpected Decl type"); |
| 2012 | if (TagDecl *PrevTagDecl = dyn_cast<TagDecl>(PrevDecl)) { |
Chris Lattner | 14943b9 | 2008-07-03 03:30:58 +0000 | [diff] [blame] | 2013 | // If this is a use of a previous tag, or if the tag is already declared |
| 2014 | // in the same scope (so that the definition/declaration completes or |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 2015 | // rementions the tag), reuse the decl. |
Argyrios Kyrtzidis | 15a12d0 | 2008-09-09 21:18:04 +0000 | [diff] [blame] | 2016 | if (TK == TK_Reference || isDeclInScope(PrevDecl, CurContext, S)) { |
Chris Lattner | 14943b9 | 2008-07-03 03:30:58 +0000 | [diff] [blame] | 2017 | // Make sure that this wasn't declared as an enum and now used as a |
| 2018 | // struct or something similar. |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 2019 | if (PrevTagDecl->getTagKind() != Kind) { |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 2020 | Diag(KWLoc, diag::err_use_with_wrong_tag, Name->getName()); |
| 2021 | Diag(PrevDecl->getLocation(), diag::err_previous_use); |
Chris Lattner | 14943b9 | 2008-07-03 03:30:58 +0000 | [diff] [blame] | 2022 | // Recover by making this an anonymous redefinition. |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 2023 | Name = 0; |
Chris Lattner | 14943b9 | 2008-07-03 03:30:58 +0000 | [diff] [blame] | 2024 | PrevDecl = 0; |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 2025 | } else { |
Chris Lattner | 14943b9 | 2008-07-03 03:30:58 +0000 | [diff] [blame] | 2026 | // If this is a use or a forward declaration, we're good. |
| 2027 | if (TK != TK_Definition) |
| 2028 | return PrevDecl; |
| 2029 | |
| 2030 | // Diagnose attempts to redefine a tag. |
| 2031 | if (PrevTagDecl->isDefinition()) { |
| 2032 | Diag(NameLoc, diag::err_redefinition, Name->getName()); |
| 2033 | Diag(PrevDecl->getLocation(), diag::err_previous_definition); |
| 2034 | // If this is a redefinition, recover by making this struct be |
| 2035 | // anonymous, which will make any later references get the previous |
| 2036 | // definition. |
| 2037 | Name = 0; |
| 2038 | } else { |
| 2039 | // Okay, this is definition of a previously declared or referenced |
| 2040 | // tag. Move the location of the decl to be the definition site. |
| 2041 | PrevDecl->setLocation(NameLoc); |
| 2042 | return PrevDecl; |
| 2043 | } |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 2044 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2045 | } |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 2046 | // If we get here, this is a definition of a new struct type in a nested |
| 2047 | // scope, e.g. "struct foo; void bar() { struct foo; }", just create a new |
| 2048 | // type. |
| 2049 | } else { |
Argyrios Kyrtzidis | b02ef24 | 2008-07-16 07:45:46 +0000 | [diff] [blame] | 2050 | // PrevDecl is a namespace. |
Argyrios Kyrtzidis | 15a12d0 | 2008-09-09 21:18:04 +0000 | [diff] [blame] | 2051 | if (isDeclInScope(PrevDecl, CurContext, S)) { |
Ted Kremenek | a89d197 | 2008-09-03 18:03:35 +0000 | [diff] [blame] | 2052 | // The tag name clashes with a namespace name, issue an error and |
| 2053 | // recover by making this tag be anonymous. |
Argyrios Kyrtzidis | b02ef24 | 2008-07-16 07:45:46 +0000 | [diff] [blame] | 2054 | Diag(NameLoc, diag::err_redefinition_different_kind, Name->getName()); |
| 2055 | Diag(PrevDecl->getLocation(), diag::err_previous_definition); |
| 2056 | Name = 0; |
| 2057 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2058 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2059 | } |
| 2060 | |
| 2061 | // If there is an identifier, use the location of the identifier as the |
| 2062 | // location of the decl, otherwise use the location of the struct/union |
| 2063 | // keyword. |
| 2064 | SourceLocation Loc = NameLoc.isValid() ? NameLoc : KWLoc; |
| 2065 | |
| 2066 | // Otherwise, if this is the first time we've seen this tag, create the decl. |
| 2067 | TagDecl *New; |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 2068 | if (Kind == TagDecl::TK_enum) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2069 | // FIXME: Tag decls should be chained to any simultaneous vardecls, e.g.: |
| 2070 | // enum X { A, B, C } D; D should chain to X. |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2071 | New = EnumDecl::Create(Context, CurContext, Loc, Name, 0); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2072 | // If this is an undefined enum, warn. |
| 2073 | if (TK != TK_Definition) Diag(Loc, diag::ext_forward_ref_enum); |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 2074 | } else { |
| 2075 | // struct/union/class |
| 2076 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2077 | // FIXME: Tag decls should be chained to any simultaneous vardecls, e.g.: |
| 2078 | // struct X { int A; } D; D should chain to X. |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 2079 | if (getLangOptions().CPlusPlus) |
Ted Kremenek | 2b345eb | 2008-09-05 17:39:33 +0000 | [diff] [blame] | 2080 | // FIXME: Look for a way to use RecordDecl for simple structs. |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2081 | New = CXXRecordDecl::Create(Context, Kind, CurContext, Loc, Name); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 2082 | else |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2083 | New = RecordDecl::Create(Context, Kind, CurContext, Loc, Name); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 2084 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2085 | |
| 2086 | // If this has an identifier, add it to the scope stack. |
| 2087 | if (Name) { |
Chris Lattner | 31e0572 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 2088 | // The scope passed in may not be a decl scope. Zip up the scope tree until |
| 2089 | // we find one that is. |
| 2090 | while ((S->getFlags() & Scope::DeclScope) == 0) |
| 2091 | S = S->getParent(); |
| 2092 | |
| 2093 | // Add it to the decl chain. |
Argyrios Kyrtzidis | 87f3ff0 | 2008-04-12 00:47:19 +0000 | [diff] [blame] | 2094 | PushOnScopeChains(New, S); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2095 | } |
Chris Lattner | e1e7985 | 2008-02-06 00:51:33 +0000 | [diff] [blame] | 2096 | |
Chris Lattner | f2e4bd5 | 2008-06-28 23:58:55 +0000 | [diff] [blame] | 2097 | if (Attr) |
| 2098 | ProcessDeclAttributeList(New, Attr); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2099 | return New; |
| 2100 | } |
| 2101 | |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 2102 | /// ActOnTagStruct - New "ActOnTag" logic for structs/unions/classes. Unlike |
| 2103 | /// the logic for enums, we create separate decls for forward declarations. |
| 2104 | /// This is called by ActOnTag, but eventually will replace its logic. |
| 2105 | Sema::DeclTy *Sema::ActOnTagStruct(Scope *S, TagDecl::TagKind Kind, TagKind TK, |
| 2106 | SourceLocation KWLoc, IdentifierInfo *Name, |
| 2107 | SourceLocation NameLoc, AttributeList *Attr) { |
| 2108 | |
| 2109 | // If this is a named struct, check to see if there was a previous forward |
| 2110 | // declaration or definition. |
| 2111 | // Use ScopedDecl instead of TagDecl, because a NamespaceDecl may come up. |
| 2112 | ScopedDecl *PrevDecl = |
| 2113 | dyn_cast_or_null<ScopedDecl>(LookupDecl(Name, Decl::IDNS_Tag, S)); |
| 2114 | |
| 2115 | if (PrevDecl) { |
| 2116 | assert((isa<TagDecl>(PrevDecl) || isa<NamespaceDecl>(PrevDecl)) && |
| 2117 | "unexpected Decl type"); |
| 2118 | |
| 2119 | if (TagDecl *PrevTagDecl = dyn_cast<TagDecl>(PrevDecl)) { |
| 2120 | // If this is a use of a previous tag, or if the tag is already declared |
| 2121 | // in the same scope (so that the definition/declaration completes or |
| 2122 | // rementions the tag), reuse the decl. |
Argyrios Kyrtzidis | 15a12d0 | 2008-09-09 21:18:04 +0000 | [diff] [blame] | 2123 | if (TK == TK_Reference || isDeclInScope(PrevDecl, CurContext, S)) { |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 2124 | // Make sure that this wasn't declared as an enum and now used as a |
| 2125 | // struct or something similar. |
| 2126 | if (PrevTagDecl->getTagKind() != Kind) { |
| 2127 | Diag(KWLoc, diag::err_use_with_wrong_tag, Name->getName()); |
| 2128 | Diag(PrevDecl->getLocation(), diag::err_previous_use); |
| 2129 | // Recover by making this an anonymous redefinition. |
| 2130 | Name = 0; |
| 2131 | PrevDecl = 0; |
| 2132 | } else { |
| 2133 | // If this is a use, return the original decl. |
| 2134 | |
| 2135 | // FIXME: In the future, return a variant or some other clue |
| 2136 | // for the consumer of this Decl to know it doesn't own it. |
| 2137 | // For our current ASTs this shouldn't be a problem, but will |
| 2138 | // need to be changed with DeclGroups. |
| 2139 | if (TK == TK_Reference) |
| 2140 | return PrevDecl; |
| 2141 | |
| 2142 | // The new decl is a definition? |
| 2143 | if (TK == TK_Definition) { |
| 2144 | // Diagnose attempts to redefine a tag. |
| 2145 | if (RecordDecl* DefRecord = |
| 2146 | cast<RecordDecl>(PrevTagDecl)->getDefinition(Context)) { |
| 2147 | Diag(NameLoc, diag::err_redefinition, Name->getName()); |
| 2148 | Diag(DefRecord->getLocation(), diag::err_previous_definition); |
| 2149 | // If this is a redefinition, recover by making this struct be |
| 2150 | // anonymous, which will make any later references get the previous |
| 2151 | // definition. |
| 2152 | Name = 0; |
| 2153 | PrevDecl = 0; |
| 2154 | } |
| 2155 | // Okay, this is definition of a previously declared or referenced |
| 2156 | // tag. We're going to create a new Decl. |
| 2157 | } |
| 2158 | } |
| 2159 | // If we get here we have (another) forward declaration. Just create |
| 2160 | // a new decl. |
| 2161 | } |
| 2162 | else { |
| 2163 | // If we get here, this is a definition of a new struct type in a nested |
| 2164 | // scope, e.g. "struct foo; void bar() { struct foo; }", just create a |
| 2165 | // new decl/type. We set PrevDecl to NULL so that the Records |
| 2166 | // have distinct types. |
| 2167 | PrevDecl = 0; |
| 2168 | } |
| 2169 | } else { |
| 2170 | // PrevDecl is a namespace. |
Argyrios Kyrtzidis | 15a12d0 | 2008-09-09 21:18:04 +0000 | [diff] [blame] | 2171 | if (isDeclInScope(PrevDecl, CurContext, S)) { |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 2172 | // The tag name clashes with a namespace name, issue an error and |
| 2173 | // recover by making this tag be anonymous. |
| 2174 | Diag(NameLoc, diag::err_redefinition_different_kind, Name->getName()); |
| 2175 | Diag(PrevDecl->getLocation(), diag::err_previous_definition); |
| 2176 | Name = 0; |
| 2177 | } |
| 2178 | } |
| 2179 | } |
| 2180 | |
| 2181 | // If there is an identifier, use the location of the identifier as the |
| 2182 | // location of the decl, otherwise use the location of the struct/union |
| 2183 | // keyword. |
| 2184 | SourceLocation Loc = NameLoc.isValid() ? NameLoc : KWLoc; |
| 2185 | |
| 2186 | // Otherwise, if this is the first time we've seen this tag, create the decl. |
| 2187 | TagDecl *New; |
| 2188 | |
| 2189 | // FIXME: Tag decls should be chained to any simultaneous vardecls, e.g.: |
| 2190 | // struct X { int A; } D; D should chain to X. |
| 2191 | if (getLangOptions().CPlusPlus) |
Ted Kremenek | 2b345eb | 2008-09-05 17:39:33 +0000 | [diff] [blame] | 2192 | // FIXME: Look for a way to use RecordDecl for simple structs. |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 2193 | New = CXXRecordDecl::Create(Context, Kind, CurContext, Loc, Name, |
| 2194 | dyn_cast_or_null<CXXRecordDecl>(PrevDecl)); |
| 2195 | else |
| 2196 | New = RecordDecl::Create(Context, Kind, CurContext, Loc, Name, |
| 2197 | dyn_cast_or_null<RecordDecl>(PrevDecl)); |
| 2198 | |
| 2199 | // If this has an identifier, add it to the scope stack. |
| 2200 | if ((TK == TK_Definition || !PrevDecl) && Name) { |
| 2201 | // The scope passed in may not be a decl scope. Zip up the scope tree until |
| 2202 | // we find one that is. |
| 2203 | while ((S->getFlags() & Scope::DeclScope) == 0) |
| 2204 | S = S->getParent(); |
| 2205 | |
| 2206 | // Add it to the decl chain. |
| 2207 | PushOnScopeChains(New, S); |
| 2208 | } |
Daniel Dunbar | 3b0db90 | 2008-10-16 02:34:03 +0000 | [diff] [blame] | 2209 | |
| 2210 | // Handle #pragma pack: if the #pragma pack stack has non-default |
| 2211 | // alignment, make up a packed attribute for this decl. These |
| 2212 | // attributes are checked when the ASTContext lays out the |
| 2213 | // structure. |
| 2214 | // |
| 2215 | // It is important for implementing the correct semantics that this |
| 2216 | // happen here (in act on tag decl). The #pragma pack stack is |
| 2217 | // maintained as a result of parser callbacks which can occur at |
| 2218 | // many points during the parsing of a struct declaration (because |
| 2219 | // the #pragma tokens are effectively skipped over during the |
| 2220 | // parsing of the struct). |
| 2221 | if (unsigned Alignment = PackContext.getAlignment()) |
| 2222 | New->addAttr(new PackedAttr(Alignment * 8)); |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 2223 | |
| 2224 | if (Attr) |
| 2225 | ProcessDeclAttributeList(New, Attr); |
| 2226 | |
| 2227 | return New; |
| 2228 | } |
| 2229 | |
| 2230 | |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 2231 | /// Collect the instance variables declared in an Objective-C object. Used in |
| 2232 | /// the creation of structures from objects using the @defs directive. |
Ted Kremenek | 01e6779 | 2008-08-20 03:26:33 +0000 | [diff] [blame] | 2233 | static void CollectIvars(ObjCInterfaceDecl *Class, ASTContext& Ctx, |
Chris Lattner | 7caeabd | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 2234 | llvm::SmallVectorImpl<Sema::DeclTy*> &ivars) { |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 2235 | if (Class->getSuperClass()) |
Ted Kremenek | 01e6779 | 2008-08-20 03:26:33 +0000 | [diff] [blame] | 2236 | CollectIvars(Class->getSuperClass(), Ctx, ivars); |
| 2237 | |
| 2238 | // For each ivar, create a fresh ObjCAtDefsFieldDecl. |
Ted Kremenek | a89d197 | 2008-09-03 18:03:35 +0000 | [diff] [blame] | 2239 | for (ObjCInterfaceDecl::ivar_iterator |
| 2240 | I=Class->ivar_begin(), E=Class->ivar_end(); I!=E; ++I) { |
| 2241 | |
Ted Kremenek | 01e6779 | 2008-08-20 03:26:33 +0000 | [diff] [blame] | 2242 | ObjCIvarDecl* ID = *I; |
| 2243 | ivars.push_back(ObjCAtDefsFieldDecl::Create(Ctx, ID->getLocation(), |
| 2244 | ID->getIdentifier(), |
| 2245 | ID->getType(), |
| 2246 | ID->getBitWidth())); |
| 2247 | } |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 2248 | } |
| 2249 | |
| 2250 | /// Called whenever @defs(ClassName) is encountered in the source. Inserts the |
| 2251 | /// instance variables of ClassName into Decls. |
| 2252 | void Sema::ActOnDefs(Scope *S, SourceLocation DeclStart, |
| 2253 | IdentifierInfo *ClassName, |
Chris Lattner | 7caeabd | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 2254 | llvm::SmallVectorImpl<DeclTy*> &Decls) { |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 2255 | // Check that ClassName is a valid class |
| 2256 | ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName); |
| 2257 | if (!Class) { |
| 2258 | Diag(DeclStart, diag::err_undef_interface, ClassName->getName()); |
| 2259 | return; |
| 2260 | } |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 2261 | // Collect the instance variables |
Ted Kremenek | 01e6779 | 2008-08-20 03:26:33 +0000 | [diff] [blame] | 2262 | CollectIvars(Class, Context, Decls); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 2263 | } |
| 2264 | |
Eli Friedman | 1b76ada | 2008-06-03 21:01:11 +0000 | [diff] [blame] | 2265 | QualType Sema::TryFixInvalidVariablyModifiedType(QualType T) { |
| 2266 | // This method tries to turn a variable array into a constant |
| 2267 | // array even when the size isn't an ICE. This is necessary |
| 2268 | // for compatibility with code that depends on gcc's buggy |
| 2269 | // constant expression folding, like struct {char x[(int)(char*)2];} |
| 2270 | if (const VariableArrayType* VLATy = dyn_cast<VariableArrayType>(T)) { |
Anders Carlsson | c44eec6 | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 2271 | APValue Result; |
Eli Friedman | 1b76ada | 2008-06-03 21:01:11 +0000 | [diff] [blame] | 2272 | if (VLATy->getSizeExpr() && |
Chris Lattner | cf0f51d | 2008-07-11 19:19:21 +0000 | [diff] [blame] | 2273 | VLATy->getSizeExpr()->tryEvaluate(Result, Context) && Result.isInt()) { |
| 2274 | llvm::APSInt &Res = Result.getInt(); |
| 2275 | if (Res > llvm::APSInt(Res.getBitWidth(), Res.isUnsigned())) |
| 2276 | return Context.getConstantArrayType(VLATy->getElementType(), |
| 2277 | Res, ArrayType::Normal, 0); |
Eli Friedman | 1b76ada | 2008-06-03 21:01:11 +0000 | [diff] [blame] | 2278 | } |
| 2279 | } |
| 2280 | return QualType(); |
| 2281 | } |
| 2282 | |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 2283 | /// 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] | 2284 | /// to create a FieldDecl object for it. |
Fariborz Jahanian | 1d78cc4 | 2008-04-10 23:32:45 +0000 | [diff] [blame] | 2285 | Sema::DeclTy *Sema::ActOnField(Scope *S, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2286 | SourceLocation DeclStart, |
| 2287 | Declarator &D, ExprTy *BitfieldWidth) { |
| 2288 | IdentifierInfo *II = D.getIdentifier(); |
| 2289 | Expr *BitWidth = (Expr*)BitfieldWidth; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2290 | SourceLocation Loc = DeclStart; |
| 2291 | if (II) Loc = D.getIdentifierLoc(); |
| 2292 | |
| 2293 | // FIXME: Unnamed fields can be handled in various different ways, for |
| 2294 | // example, unnamed unions inject all members into the struct namespace! |
Ted Kremenek | a89d197 | 2008-09-03 18:03:35 +0000 | [diff] [blame] | 2295 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2296 | if (BitWidth) { |
| 2297 | // TODO: Validate. |
| 2298 | //printf("WARNING: BITFIELDS IGNORED!\n"); |
| 2299 | |
| 2300 | // 6.7.2.1p3 |
| 2301 | // 6.7.2.1p4 |
| 2302 | |
| 2303 | } else { |
| 2304 | // Not a bitfield. |
| 2305 | |
| 2306 | // validate II. |
| 2307 | |
| 2308 | } |
| 2309 | |
| 2310 | QualType T = GetTypeForDeclarator(D, S); |
Steve Naroff | 5912a35 | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 2311 | assert(!T.isNull() && "GetTypeForDeclarator() returned null type"); |
| 2312 | bool InvalidDecl = false; |
Steve Naroff | d7444aa | 2007-08-31 17:20:07 +0000 | [diff] [blame] | 2313 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2314 | // C99 6.7.2.1p8: A member of a structure or union may have any type other |
| 2315 | // than a variably modified type. |
Eli Friedman | 9db1397 | 2008-02-15 12:53:51 +0000 | [diff] [blame] | 2316 | if (T->isVariablyModifiedType()) { |
Eli Friedman | 1b76ada | 2008-06-03 21:01:11 +0000 | [diff] [blame] | 2317 | QualType FixedTy = TryFixInvalidVariablyModifiedType(T); |
| 2318 | if (!FixedTy.isNull()) { |
| 2319 | Diag(Loc, diag::warn_illegal_constant_array_size, Loc); |
| 2320 | T = FixedTy; |
| 2321 | } else { |
| 2322 | // FIXME: This diagnostic needs work |
| 2323 | Diag(Loc, diag::err_typecheck_illegal_vla, Loc); |
| 2324 | InvalidDecl = true; |
| 2325 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2326 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2327 | // FIXME: Chain fielddecls together. |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 2328 | FieldDecl *NewFD; |
| 2329 | |
| 2330 | if (getLangOptions().CPlusPlus) { |
| 2331 | // FIXME: Replace CXXFieldDecls with FieldDecls for simple structs. |
| 2332 | NewFD = CXXFieldDecl::Create(Context, cast<CXXRecordDecl>(CurContext), |
| 2333 | Loc, II, T, BitWidth); |
| 2334 | if (II) |
| 2335 | PushOnScopeChains(NewFD, S); |
| 2336 | } |
| 2337 | else |
| 2338 | NewFD = FieldDecl::Create(Context, Loc, II, T, BitWidth); |
Steve Naroff | 4473921 | 2007-09-11 21:17:26 +0000 | [diff] [blame] | 2339 | |
Chris Lattner | 3ff30c8 | 2008-06-29 00:02:00 +0000 | [diff] [blame] | 2340 | ProcessDeclAttributes(NewFD, D); |
Anders Carlsson | ad14806 | 2008-02-16 00:29:18 +0000 | [diff] [blame] | 2341 | |
Steve Naroff | 5912a35 | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 2342 | if (D.getInvalidType() || InvalidDecl) |
| 2343 | NewFD->setInvalidDecl(); |
| 2344 | return NewFD; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2345 | } |
| 2346 | |
Fariborz Jahanian | 89204a1 | 2007-10-01 16:53:59 +0000 | [diff] [blame] | 2347 | /// TranslateIvarVisibility - Translate visibility from a token ID to an |
| 2348 | /// AST enum value. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2349 | static ObjCIvarDecl::AccessControl |
Fariborz Jahanian | 89204a1 | 2007-10-01 16:53:59 +0000 | [diff] [blame] | 2350 | TranslateIvarVisibility(tok::ObjCKeywordKind ivarVisibility) { |
Steve Naroff | f13271f | 2007-09-14 23:09:53 +0000 | [diff] [blame] | 2351 | switch (ivarVisibility) { |
Chris Lattner | 33d34a6 | 2008-10-12 00:28:42 +0000 | [diff] [blame] | 2352 | default: assert(0 && "Unknown visitibility kind"); |
| 2353 | case tok::objc_private: return ObjCIvarDecl::Private; |
| 2354 | case tok::objc_public: return ObjCIvarDecl::Public; |
| 2355 | case tok::objc_protected: return ObjCIvarDecl::Protected; |
| 2356 | case tok::objc_package: return ObjCIvarDecl::Package; |
Steve Naroff | f13271f | 2007-09-14 23:09:53 +0000 | [diff] [blame] | 2357 | } |
| 2358 | } |
| 2359 | |
Fariborz Jahanian | 45bc03f | 2008-04-11 16:55:42 +0000 | [diff] [blame] | 2360 | /// ActOnIvar - Each ivar field of an objective-c class is passed into this |
| 2361 | /// in order to create an IvarDecl object for it. |
Fariborz Jahanian | 1d78cc4 | 2008-04-10 23:32:45 +0000 | [diff] [blame] | 2362 | Sema::DeclTy *Sema::ActOnIvar(Scope *S, |
Fariborz Jahanian | 45bc03f | 2008-04-11 16:55:42 +0000 | [diff] [blame] | 2363 | SourceLocation DeclStart, |
| 2364 | Declarator &D, ExprTy *BitfieldWidth, |
| 2365 | tok::ObjCKeywordKind Visibility) { |
Fariborz Jahanian | 1d78cc4 | 2008-04-10 23:32:45 +0000 | [diff] [blame] | 2366 | IdentifierInfo *II = D.getIdentifier(); |
| 2367 | Expr *BitWidth = (Expr*)BitfieldWidth; |
| 2368 | SourceLocation Loc = DeclStart; |
| 2369 | if (II) Loc = D.getIdentifierLoc(); |
| 2370 | |
| 2371 | // FIXME: Unnamed fields can be handled in various different ways, for |
| 2372 | // example, unnamed unions inject all members into the struct namespace! |
| 2373 | |
| 2374 | |
| 2375 | if (BitWidth) { |
| 2376 | // TODO: Validate. |
| 2377 | //printf("WARNING: BITFIELDS IGNORED!\n"); |
| 2378 | |
| 2379 | // 6.7.2.1p3 |
| 2380 | // 6.7.2.1p4 |
| 2381 | |
| 2382 | } else { |
| 2383 | // Not a bitfield. |
| 2384 | |
| 2385 | // validate II. |
| 2386 | |
| 2387 | } |
| 2388 | |
| 2389 | QualType T = GetTypeForDeclarator(D, S); |
| 2390 | assert(!T.isNull() && "GetTypeForDeclarator() returned null type"); |
| 2391 | bool InvalidDecl = false; |
| 2392 | |
| 2393 | // C99 6.7.2.1p8: A member of a structure or union may have any type other |
| 2394 | // than a variably modified type. |
| 2395 | if (T->isVariablyModifiedType()) { |
| 2396 | // FIXME: This diagnostic needs work |
| 2397 | Diag(Loc, diag::err_typecheck_illegal_vla, Loc); |
| 2398 | InvalidDecl = true; |
| 2399 | } |
| 2400 | |
Ted Kremenek | b8db21d | 2008-07-23 18:04:17 +0000 | [diff] [blame] | 2401 | // Get the visibility (access control) for this ivar. |
| 2402 | ObjCIvarDecl::AccessControl ac = |
| 2403 | Visibility != tok::objc_not_keyword ? TranslateIvarVisibility(Visibility) |
| 2404 | : ObjCIvarDecl::None; |
| 2405 | |
| 2406 | // Construct the decl. |
| 2407 | ObjCIvarDecl *NewID = ObjCIvarDecl::Create(Context, Loc, II, T, ac, |
Steve Naroff | 8f3b265 | 2008-07-16 18:22:22 +0000 | [diff] [blame] | 2408 | (Expr *)BitfieldWidth); |
Fariborz Jahanian | 1d78cc4 | 2008-04-10 23:32:45 +0000 | [diff] [blame] | 2409 | |
Ted Kremenek | b8db21d | 2008-07-23 18:04:17 +0000 | [diff] [blame] | 2410 | // Process attributes attached to the ivar. |
Chris Lattner | 3ff30c8 | 2008-06-29 00:02:00 +0000 | [diff] [blame] | 2411 | ProcessDeclAttributes(NewID, D); |
Fariborz Jahanian | 1d78cc4 | 2008-04-10 23:32:45 +0000 | [diff] [blame] | 2412 | |
| 2413 | if (D.getInvalidType() || InvalidDecl) |
| 2414 | NewID->setInvalidDecl(); |
Ted Kremenek | b8db21d | 2008-07-23 18:04:17 +0000 | [diff] [blame] | 2415 | |
Fariborz Jahanian | 1d78cc4 | 2008-04-10 23:32:45 +0000 | [diff] [blame] | 2416 | return NewID; |
| 2417 | } |
| 2418 | |
Fariborz Jahanian | 9d048ff | 2007-09-29 00:54:24 +0000 | [diff] [blame] | 2419 | void Sema::ActOnFields(Scope* S, |
Fariborz Jahanian | 3f5faf7 | 2007-10-04 00:45:27 +0000 | [diff] [blame] | 2420 | SourceLocation RecLoc, DeclTy *RecDecl, |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 2421 | DeclTy **Fields, unsigned NumFields, |
Daniel Dunbar | 1bfe1c2 | 2008-10-03 02:03:53 +0000 | [diff] [blame] | 2422 | SourceLocation LBrac, SourceLocation RBrac, |
Daniel Dunbar | 7d07664 | 2008-10-03 17:33:35 +0000 | [diff] [blame] | 2423 | AttributeList *Attr) { |
Steve Naroff | 7421664 | 2007-09-14 22:20:54 +0000 | [diff] [blame] | 2424 | Decl *EnclosingDecl = static_cast<Decl*>(RecDecl); |
| 2425 | assert(EnclosingDecl && "missing record or interface decl"); |
| 2426 | RecordDecl *Record = dyn_cast<RecordDecl>(EnclosingDecl); |
| 2427 | |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 2428 | if (Record) |
| 2429 | if (RecordDecl* DefRecord = Record->getDefinition(Context)) { |
| 2430 | // Diagnose code like: |
| 2431 | // struct S { struct S {} X; }; |
| 2432 | // We discover this when we complete the outer S. Reject and ignore the |
| 2433 | // outer S. |
| 2434 | Diag(DefRecord->getLocation(), diag::err_nested_redefinition, |
| 2435 | DefRecord->getKindName()); |
| 2436 | Diag(RecLoc, diag::err_previous_definition); |
| 2437 | Record->setInvalidDecl(); |
| 2438 | return; |
| 2439 | } |
| 2440 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2441 | // Verify that all the fields are okay. |
| 2442 | unsigned NumNamedMembers = 0; |
| 2443 | llvm::SmallVector<FieldDecl*, 32> RecFields; |
| 2444 | llvm::SmallSet<const IdentifierInfo*, 32> FieldIDs; |
Steve Naroff | 7421664 | 2007-09-14 22:20:54 +0000 | [diff] [blame] | 2445 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2446 | for (unsigned i = 0; i != NumFields; ++i) { |
Fariborz Jahanian | e267ab6 | 2007-09-14 16:27:55 +0000 | [diff] [blame] | 2447 | |
Steve Naroff | 7421664 | 2007-09-14 22:20:54 +0000 | [diff] [blame] | 2448 | FieldDecl *FD = cast_or_null<FieldDecl>(static_cast<Decl*>(Fields[i])); |
| 2449 | assert(FD && "missing field decl"); |
| 2450 | |
| 2451 | // Remember all fields. |
| 2452 | RecFields.push_back(FD); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2453 | |
| 2454 | // Get the type for the field. |
Chris Lattner | 02c642e | 2007-07-31 21:33:24 +0000 | [diff] [blame] | 2455 | Type *FDTy = FD->getType().getTypePtr(); |
Steve Naroff | f13271f | 2007-09-14 23:09:53 +0000 | [diff] [blame] | 2456 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2457 | // 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] | 2458 | if (FDTy->isFunctionType()) { |
Steve Naroff | 7421664 | 2007-09-14 22:20:54 +0000 | [diff] [blame] | 2459 | Diag(FD->getLocation(), diag::err_field_declared_as_function, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2460 | FD->getName()); |
Steve Naroff | 7421664 | 2007-09-14 22:20:54 +0000 | [diff] [blame] | 2461 | FD->setInvalidDecl(); |
| 2462 | EnclosingDecl->setInvalidDecl(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2463 | continue; |
| 2464 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2465 | // C99 6.7.2.1p2 - A field may not be an incomplete type except... |
| 2466 | if (FDTy->isIncompleteType()) { |
Fariborz Jahanian | e267ab6 | 2007-09-14 16:27:55 +0000 | [diff] [blame] | 2467 | if (!Record) { // Incomplete ivar type is always an error. |
Fariborz Jahanian | 3f5faf7 | 2007-10-04 00:45:27 +0000 | [diff] [blame] | 2468 | Diag(FD->getLocation(), diag::err_field_incomplete, FD->getName()); |
Steve Naroff | 7421664 | 2007-09-14 22:20:54 +0000 | [diff] [blame] | 2469 | FD->setInvalidDecl(); |
| 2470 | EnclosingDecl->setInvalidDecl(); |
Fariborz Jahanian | 3f5faf7 | 2007-10-04 00:45:27 +0000 | [diff] [blame] | 2471 | continue; |
Fariborz Jahanian | e267ab6 | 2007-09-14 16:27:55 +0000 | [diff] [blame] | 2472 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2473 | if (i != NumFields-1 || // ... that the last member ... |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 2474 | !Record->isStruct() || // ... of a structure ... |
Chris Lattner | 02c642e | 2007-07-31 21:33:24 +0000 | [diff] [blame] | 2475 | !FDTy->isArrayType()) { //... may have incomplete array type. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2476 | Diag(FD->getLocation(), diag::err_field_incomplete, FD->getName()); |
Steve Naroff | 7421664 | 2007-09-14 22:20:54 +0000 | [diff] [blame] | 2477 | FD->setInvalidDecl(); |
| 2478 | EnclosingDecl->setInvalidDecl(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2479 | continue; |
| 2480 | } |
Fariborz Jahanian | e267ab6 | 2007-09-14 16:27:55 +0000 | [diff] [blame] | 2481 | if (NumNamedMembers < 1) { //... must have more than named member ... |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2482 | Diag(FD->getLocation(), diag::err_flexible_array_empty_struct, |
| 2483 | FD->getName()); |
Steve Naroff | 7421664 | 2007-09-14 22:20:54 +0000 | [diff] [blame] | 2484 | FD->setInvalidDecl(); |
| 2485 | EnclosingDecl->setInvalidDecl(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2486 | continue; |
| 2487 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2488 | // 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] | 2489 | if (Record) |
| 2490 | Record->setHasFlexibleArrayMember(true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2491 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2492 | /// C99 6.7.2.1p2 - a struct ending in a flexible array member cannot be the |
| 2493 | /// field of another structure or the element of an array. |
Chris Lattner | 02c642e | 2007-07-31 21:33:24 +0000 | [diff] [blame] | 2494 | if (const RecordType *FDTTy = FDTy->getAsRecordType()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2495 | if (FDTTy->getDecl()->hasFlexibleArrayMember()) { |
| 2496 | // 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] | 2497 | if (Record && Record->isUnion()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2498 | Record->setHasFlexibleArrayMember(true); |
| 2499 | } else { |
| 2500 | // If this is a struct/class and this is not the last element, reject |
| 2501 | // it. Note that GCC supports variable sized arrays in the middle of |
| 2502 | // structures. |
| 2503 | if (i != NumFields-1) { |
| 2504 | Diag(FD->getLocation(), diag::err_variable_sized_type_in_struct, |
| 2505 | FD->getName()); |
Steve Naroff | 7421664 | 2007-09-14 22:20:54 +0000 | [diff] [blame] | 2506 | FD->setInvalidDecl(); |
| 2507 | EnclosingDecl->setInvalidDecl(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2508 | continue; |
| 2509 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2510 | // We support flexible arrays at the end of structs in other structs |
| 2511 | // as an extension. |
| 2512 | Diag(FD->getLocation(), diag::ext_flexible_array_in_struct, |
| 2513 | FD->getName()); |
Fariborz Jahanian | 3f5faf7 | 2007-10-04 00:45:27 +0000 | [diff] [blame] | 2514 | if (Record) |
Fariborz Jahanian | e267ab6 | 2007-09-14 16:27:55 +0000 | [diff] [blame] | 2515 | Record->setHasFlexibleArrayMember(true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2516 | } |
| 2517 | } |
| 2518 | } |
Fariborz Jahanian | e7f64cc | 2007-10-12 22:10:42 +0000 | [diff] [blame] | 2519 | /// A field cannot be an Objective-c object |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2520 | if (FDTy->isObjCInterfaceType()) { |
Fariborz Jahanian | e7f64cc | 2007-10-12 22:10:42 +0000 | [diff] [blame] | 2521 | Diag(FD->getLocation(), diag::err_statically_allocated_object, |
| 2522 | FD->getName()); |
| 2523 | FD->setInvalidDecl(); |
| 2524 | EnclosingDecl->setInvalidDecl(); |
| 2525 | continue; |
| 2526 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2527 | // Keep track of the number of named members. |
| 2528 | if (IdentifierInfo *II = FD->getIdentifier()) { |
| 2529 | // Detect duplicate member names. |
| 2530 | if (!FieldIDs.insert(II)) { |
| 2531 | Diag(FD->getLocation(), diag::err_duplicate_member, II->getName()); |
| 2532 | // Find the previous decl. |
| 2533 | SourceLocation PrevLoc; |
Chris Lattner | 33d34a6 | 2008-10-12 00:28:42 +0000 | [diff] [blame] | 2534 | for (unsigned i = 0; ; ++i) { |
| 2535 | assert(i != RecFields.size() && "Didn't find previous def!"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2536 | if (RecFields[i]->getIdentifier() == II) { |
| 2537 | PrevLoc = RecFields[i]->getLocation(); |
| 2538 | break; |
| 2539 | } |
| 2540 | } |
| 2541 | Diag(PrevLoc, diag::err_previous_definition); |
Steve Naroff | 7421664 | 2007-09-14 22:20:54 +0000 | [diff] [blame] | 2542 | FD->setInvalidDecl(); |
| 2543 | EnclosingDecl->setInvalidDecl(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2544 | continue; |
| 2545 | } |
| 2546 | ++NumNamedMembers; |
| 2547 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2548 | } |
| 2549 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2550 | // Okay, we successfully defined 'Record'. |
Chris Lattner | e1e7985 | 2008-02-06 00:51:33 +0000 | [diff] [blame] | 2551 | if (Record) { |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 2552 | Record->defineBody(Context, &RecFields[0], RecFields.size()); |
Argyrios Kyrtzidis | a4755c6 | 2008-08-09 00:58:37 +0000 | [diff] [blame] | 2553 | // If this is a C++ record, HandleTagDeclDefinition will be invoked in |
| 2554 | // Sema::ActOnFinishCXXClassDef. |
| 2555 | if (!isa<CXXRecordDecl>(Record)) |
| 2556 | Consumer.HandleTagDeclDefinition(Record); |
Chris Lattner | e1e7985 | 2008-02-06 00:51:33 +0000 | [diff] [blame] | 2557 | } else { |
Chris Lattner | a91d381 | 2008-02-05 22:40:55 +0000 | [diff] [blame] | 2558 | ObjCIvarDecl **ClsFields = reinterpret_cast<ObjCIvarDecl**>(&RecFields[0]); |
| 2559 | if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(EnclosingDecl)) |
| 2560 | ID->addInstanceVariablesToClass(ClsFields, RecFields.size(), RBrac); |
| 2561 | else if (ObjCImplementationDecl *IMPDecl = |
| 2562 | dyn_cast<ObjCImplementationDecl>(EnclosingDecl)) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2563 | assert(IMPDecl && "ActOnFields - missing ObjCImplementationDecl"); |
| 2564 | IMPDecl->ObjCAddInstanceVariablesToClassImpl(ClsFields, RecFields.size()); |
Fariborz Jahanian | 3a3ca1b | 2007-10-31 18:48:14 +0000 | [diff] [blame] | 2565 | CheckImplementationIvars(IMPDecl, ClsFields, RecFields.size(), RBrac); |
Fariborz Jahanian | d0b90bf | 2007-09-26 18:27:25 +0000 | [diff] [blame] | 2566 | } |
Fariborz Jahanian | b04a021 | 2007-09-14 21:08:27 +0000 | [diff] [blame] | 2567 | } |
Daniel Dunbar | 7d07664 | 2008-10-03 17:33:35 +0000 | [diff] [blame] | 2568 | |
| 2569 | if (Attr) |
| 2570 | ProcessDeclAttributeList(Record, Attr); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2571 | } |
| 2572 | |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 2573 | Sema::DeclTy *Sema::ActOnEnumConstant(Scope *S, DeclTy *theEnumDecl, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2574 | DeclTy *lastEnumConst, |
| 2575 | SourceLocation IdLoc, IdentifierInfo *Id, |
| 2576 | SourceLocation EqualLoc, ExprTy *val) { |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2577 | EnumDecl *TheEnumDecl = cast<EnumDecl>(static_cast<Decl*>(theEnumDecl)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2578 | EnumConstantDecl *LastEnumConst = |
| 2579 | cast_or_null<EnumConstantDecl>(static_cast<Decl*>(lastEnumConst)); |
| 2580 | Expr *Val = static_cast<Expr*>(val); |
| 2581 | |
Chris Lattner | 31e0572 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 2582 | // The scope passed in may not be a decl scope. Zip up the scope tree until |
| 2583 | // we find one that is. |
| 2584 | while ((S->getFlags() & Scope::DeclScope) == 0) |
| 2585 | S = S->getParent(); |
| 2586 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2587 | // Verify that there isn't already something declared with this name in this |
| 2588 | // scope. |
Steve Naroff | b327ce0 | 2008-04-02 14:35:35 +0000 | [diff] [blame] | 2589 | if (Decl *PrevDecl = LookupDecl(Id, Decl::IDNS_Ordinary, S)) { |
Argyrios Kyrtzidis | 0ff12f0 | 2008-07-16 21:01:53 +0000 | [diff] [blame] | 2590 | // When in C++, we may get a TagDecl with the same name; in this case the |
| 2591 | // enum constant will 'hide' the tag. |
| 2592 | assert((getLangOptions().CPlusPlus || !isa<TagDecl>(PrevDecl)) && |
| 2593 | "Received TagDecl when not in C++!"); |
Argyrios Kyrtzidis | 15a12d0 | 2008-09-09 21:18:04 +0000 | [diff] [blame] | 2594 | if (!isa<TagDecl>(PrevDecl) && isDeclInScope(PrevDecl, CurContext, S)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2595 | if (isa<EnumConstantDecl>(PrevDecl)) |
| 2596 | Diag(IdLoc, diag::err_redefinition_of_enumerator, Id->getName()); |
| 2597 | else |
| 2598 | Diag(IdLoc, diag::err_redefinition, Id->getName()); |
| 2599 | Diag(PrevDecl->getLocation(), diag::err_previous_definition); |
Chris Lattner | a73349d | 2008-02-26 00:33:57 +0000 | [diff] [blame] | 2600 | delete Val; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2601 | return 0; |
| 2602 | } |
| 2603 | } |
| 2604 | |
| 2605 | llvm::APSInt EnumVal(32); |
| 2606 | QualType EltTy; |
| 2607 | if (Val) { |
Chris Lattner | 421a23d | 2007-08-27 21:16:18 +0000 | [diff] [blame] | 2608 | // Make sure to promote the operand type to int. |
| 2609 | UsualUnaryConversions(Val); |
| 2610 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2611 | // C99 6.7.2.2p2: Make sure we have an integer constant expression. |
| 2612 | SourceLocation ExpLoc; |
Chris Lattner | 590b664 | 2007-07-15 23:26:56 +0000 | [diff] [blame] | 2613 | if (!Val->isIntegerConstantExpr(EnumVal, Context, &ExpLoc)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2614 | Diag(ExpLoc, diag::err_enum_value_not_integer_constant_expr, |
| 2615 | Id->getName()); |
Chris Lattner | a73349d | 2008-02-26 00:33:57 +0000 | [diff] [blame] | 2616 | delete Val; |
Chris Lattner | b7416f9 | 2007-08-27 17:37:24 +0000 | [diff] [blame] | 2617 | Val = 0; // Just forget about it. |
Chris Lattner | e9ca851 | 2007-08-29 16:03:41 +0000 | [diff] [blame] | 2618 | } else { |
| 2619 | EltTy = Val->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2620 | } |
Chris Lattner | b7416f9 | 2007-08-27 17:37:24 +0000 | [diff] [blame] | 2621 | } |
| 2622 | |
| 2623 | if (!Val) { |
| 2624 | if (LastEnumConst) { |
| 2625 | // Assign the last value + 1. |
| 2626 | EnumVal = LastEnumConst->getInitVal(); |
| 2627 | ++EnumVal; |
Chris Lattner | 421a23d | 2007-08-27 21:16:18 +0000 | [diff] [blame] | 2628 | |
| 2629 | // Check for overflow on increment. |
| 2630 | if (EnumVal < LastEnumConst->getInitVal()) |
| 2631 | Diag(IdLoc, diag::warn_enum_value_overflow); |
| 2632 | |
Chris Lattner | b7416f9 | 2007-08-27 17:37:24 +0000 | [diff] [blame] | 2633 | EltTy = LastEnumConst->getType(); |
| 2634 | } else { |
| 2635 | // First value, set to zero. |
| 2636 | EltTy = Context.IntTy; |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 2637 | EnumVal.zextOrTrunc(static_cast<uint32_t>(Context.getTypeSize(EltTy))); |
Chris Lattner | b7416f9 | 2007-08-27 17:37:24 +0000 | [diff] [blame] | 2638 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2639 | } |
| 2640 | |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 2641 | EnumConstantDecl *New = |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2642 | EnumConstantDecl::Create(Context, TheEnumDecl, IdLoc, Id, EltTy, |
| 2643 | Val, EnumVal, |
Chris Lattner | c63e660 | 2008-03-15 21:32:50 +0000 | [diff] [blame] | 2644 | LastEnumConst); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2645 | |
| 2646 | // Register this decl in the current scope stack. |
Argyrios Kyrtzidis | 87f3ff0 | 2008-04-12 00:47:19 +0000 | [diff] [blame] | 2647 | PushOnScopeChains(New, S); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2648 | return New; |
| 2649 | } |
| 2650 | |
Steve Naroff | 02408c6 | 2008-08-07 14:08:16 +0000 | [diff] [blame] | 2651 | // FIXME: For consistency with ActOnFields(), we should have the parser |
| 2652 | // pass in the source location for the left/right braces. |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 2653 | void Sema::ActOnEnumBody(SourceLocation EnumLoc, DeclTy *EnumDeclX, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2654 | DeclTy **Elements, unsigned NumElements) { |
| 2655 | EnumDecl *Enum = cast<EnumDecl>(static_cast<Decl*>(EnumDeclX)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2656 | |
Steve Naroff | 02408c6 | 2008-08-07 14:08:16 +0000 | [diff] [blame] | 2657 | if (Enum && Enum->isDefinition()) { |
| 2658 | // Diagnose code like: |
| 2659 | // enum e0 { |
| 2660 | // E0 = sizeof(enum e0 { E1 }) |
| 2661 | // }; |
| 2662 | Diag(Enum->getLocation(), diag::err_nested_redefinition, |
| 2663 | Enum->getName()); |
| 2664 | Diag(EnumLoc, diag::err_previous_definition); |
| 2665 | Enum->setInvalidDecl(); |
| 2666 | return; |
| 2667 | } |
Chris Lattner | e37f0be | 2007-08-28 05:10:31 +0000 | [diff] [blame] | 2668 | // TODO: If the result value doesn't fit in an int, it must be a long or long |
| 2669 | // long value. ISO C does not support this, but GCC does as an extension, |
| 2670 | // emit a warning. |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 2671 | unsigned IntWidth = Context.Target.getIntWidth(); |
Chris Lattner | e37f0be | 2007-08-28 05:10:31 +0000 | [diff] [blame] | 2672 | |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 2673 | // Verify that all the values are okay, compute the size of the values, and |
| 2674 | // reverse the list. |
| 2675 | unsigned NumNegativeBits = 0; |
| 2676 | unsigned NumPositiveBits = 0; |
| 2677 | |
| 2678 | // Keep track of whether all elements have type int. |
| 2679 | bool AllElementsInt = true; |
| 2680 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2681 | EnumConstantDecl *EltList = 0; |
| 2682 | for (unsigned i = 0; i != NumElements; ++i) { |
| 2683 | EnumConstantDecl *ECD = |
| 2684 | cast_or_null<EnumConstantDecl>(static_cast<Decl*>(Elements[i])); |
| 2685 | if (!ECD) continue; // Already issued a diagnostic. |
Chris Lattner | 211a30e | 2007-08-28 05:27:00 +0000 | [diff] [blame] | 2686 | |
| 2687 | // 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] | 2688 | const llvm::APSInt &InitVal = ECD->getInitVal(); |
Chris Lattner | a73349d | 2008-02-26 00:33:57 +0000 | [diff] [blame] | 2689 | assert(InitVal.getBitWidth() >= IntWidth && |
| 2690 | "Should have promoted value to int"); |
Chris Lattner | 211a30e | 2007-08-28 05:27:00 +0000 | [diff] [blame] | 2691 | if (InitVal.getBitWidth() > IntWidth) { |
| 2692 | llvm::APSInt V(InitVal); |
| 2693 | V.trunc(IntWidth); |
| 2694 | V.extend(InitVal.getBitWidth()); |
| 2695 | if (V != InitVal) |
| 2696 | Diag(ECD->getLocation(), diag::ext_enum_value_not_int, |
Chris Lattner | 9aa77f1 | 2008-08-17 07:19:51 +0000 | [diff] [blame] | 2697 | InitVal.toString(10)); |
Chris Lattner | 211a30e | 2007-08-28 05:27:00 +0000 | [diff] [blame] | 2698 | } |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 2699 | |
| 2700 | // Keep track of the size of positive and negative values. |
Chris Lattner | a73349d | 2008-02-26 00:33:57 +0000 | [diff] [blame] | 2701 | if (InitVal.isUnsigned() || InitVal.isNonNegative()) |
Chris Lattner | 21dd821 | 2008-01-14 21:47:29 +0000 | [diff] [blame] | 2702 | NumPositiveBits = std::max(NumPositiveBits, |
| 2703 | (unsigned)InitVal.getActiveBits()); |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 2704 | else |
Chris Lattner | 21dd821 | 2008-01-14 21:47:29 +0000 | [diff] [blame] | 2705 | NumNegativeBits = std::max(NumNegativeBits, |
| 2706 | (unsigned)InitVal.getMinSignedBits()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2707 | |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 2708 | // Keep track of whether every enum element has type int (very commmon). |
| 2709 | if (AllElementsInt) |
| 2710 | AllElementsInt = ECD->getType() == Context.IntTy; |
| 2711 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2712 | ECD->setNextDeclarator(EltList); |
| 2713 | EltList = ECD; |
| 2714 | } |
| 2715 | |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 2716 | // Figure out the type that should be used for this enum. |
| 2717 | // FIXME: Support attribute(packed) on enums and -fshort-enums. |
| 2718 | QualType BestType; |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 2719 | unsigned BestWidth; |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 2720 | |
| 2721 | if (NumNegativeBits) { |
| 2722 | // If there is a negative value, figure out the smallest integer type (of |
| 2723 | // int/long/longlong) that fits. |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 2724 | if (NumNegativeBits <= IntWidth && NumPositiveBits < IntWidth) { |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 2725 | BestType = Context.IntTy; |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 2726 | BestWidth = IntWidth; |
| 2727 | } else { |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 2728 | BestWidth = Context.Target.getLongWidth(); |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 2729 | |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 2730 | if (NumNegativeBits <= BestWidth && NumPositiveBits < BestWidth) |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 2731 | BestType = Context.LongTy; |
| 2732 | else { |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 2733 | BestWidth = Context.Target.getLongLongWidth(); |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 2734 | |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 2735 | if (NumNegativeBits > BestWidth || NumPositiveBits >= BestWidth) |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 2736 | Diag(Enum->getLocation(), diag::warn_enum_too_large); |
| 2737 | BestType = Context.LongLongTy; |
| 2738 | } |
| 2739 | } |
| 2740 | } else { |
| 2741 | // If there is no negative value, figure out which of uint, ulong, ulonglong |
| 2742 | // fits. |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 2743 | if (NumPositiveBits <= IntWidth) { |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 2744 | BestType = Context.UnsignedIntTy; |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 2745 | BestWidth = IntWidth; |
| 2746 | } else if (NumPositiveBits <= |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 2747 | (BestWidth = Context.Target.getLongWidth())) { |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 2748 | BestType = Context.UnsignedLongTy; |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 2749 | } else { |
| 2750 | BestWidth = Context.Target.getLongLongWidth(); |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 2751 | assert(NumPositiveBits <= BestWidth && |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 2752 | "How could an initializer get larger than ULL?"); |
| 2753 | BestType = Context.UnsignedLongLongTy; |
| 2754 | } |
| 2755 | } |
| 2756 | |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 2757 | // Loop over all of the enumerator constants, changing their types to match |
| 2758 | // the type of the enum if needed. |
| 2759 | for (unsigned i = 0; i != NumElements; ++i) { |
| 2760 | EnumConstantDecl *ECD = |
| 2761 | cast_or_null<EnumConstantDecl>(static_cast<Decl*>(Elements[i])); |
| 2762 | if (!ECD) continue; // Already issued a diagnostic. |
| 2763 | |
| 2764 | // Standard C says the enumerators have int type, but we allow, as an |
| 2765 | // extension, the enumerators to be larger than int size. If each |
| 2766 | // enumerator value fits in an int, type it as an int, otherwise type it the |
| 2767 | // same as the enumerator decl itself. This means that in "enum { X = 1U }" |
| 2768 | // that X has type 'int', not 'unsigned'. |
Chris Lattner | a73349d | 2008-02-26 00:33:57 +0000 | [diff] [blame] | 2769 | if (ECD->getType() == Context.IntTy) { |
| 2770 | // Make sure the init value is signed. |
| 2771 | llvm::APSInt IV = ECD->getInitVal(); |
| 2772 | IV.setIsSigned(true); |
| 2773 | ECD->setInitVal(IV); |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 2774 | continue; // Already int type. |
Chris Lattner | a73349d | 2008-02-26 00:33:57 +0000 | [diff] [blame] | 2775 | } |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 2776 | |
| 2777 | // Determine whether the value fits into an int. |
| 2778 | llvm::APSInt InitVal = ECD->getInitVal(); |
| 2779 | bool FitsInInt; |
| 2780 | if (InitVal.isUnsigned() || !InitVal.isNegative()) |
| 2781 | FitsInInt = InitVal.getActiveBits() < IntWidth; |
| 2782 | else |
| 2783 | FitsInInt = InitVal.getMinSignedBits() <= IntWidth; |
| 2784 | |
| 2785 | // If it fits into an integer type, force it. Otherwise force it to match |
| 2786 | // the enum decl type. |
| 2787 | QualType NewTy; |
| 2788 | unsigned NewWidth; |
| 2789 | bool NewSign; |
| 2790 | if (FitsInInt) { |
| 2791 | NewTy = Context.IntTy; |
| 2792 | NewWidth = IntWidth; |
| 2793 | NewSign = true; |
| 2794 | } else if (ECD->getType() == BestType) { |
| 2795 | // Already the right type! |
| 2796 | continue; |
| 2797 | } else { |
| 2798 | NewTy = BestType; |
| 2799 | NewWidth = BestWidth; |
| 2800 | NewSign = BestType->isSignedIntegerType(); |
| 2801 | } |
| 2802 | |
| 2803 | // Adjust the APSInt value. |
| 2804 | InitVal.extOrTrunc(NewWidth); |
| 2805 | InitVal.setIsSigned(NewSign); |
| 2806 | ECD->setInitVal(InitVal); |
| 2807 | |
| 2808 | // Adjust the Expr initializer and type. |
| 2809 | ECD->setInitExpr(new ImplicitCastExpr(NewTy, ECD->getInitExpr())); |
| 2810 | ECD->setType(NewTy); |
| 2811 | } |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 2812 | |
Chris Lattner | e00b18c | 2007-08-28 18:24:31 +0000 | [diff] [blame] | 2813 | Enum->defineElements(EltList, BestType); |
Chris Lattner | e1e7985 | 2008-02-06 00:51:33 +0000 | [diff] [blame] | 2814 | Consumer.HandleTagDeclDefinition(Enum); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2815 | } |
| 2816 | |
Anders Carlsson | dfab6cb | 2008-02-08 00:33:21 +0000 | [diff] [blame] | 2817 | Sema::DeclTy *Sema::ActOnFileScopeAsmDecl(SourceLocation Loc, |
| 2818 | ExprTy *expr) { |
| 2819 | StringLiteral *AsmString = cast<StringLiteral>((Expr*)expr); |
| 2820 | |
Chris Lattner | 8e25d86 | 2008-03-16 00:16:02 +0000 | [diff] [blame] | 2821 | return FileScopeAsmDecl::Create(Context, Loc, AsmString); |
Anders Carlsson | dfab6cb | 2008-02-08 00:33:21 +0000 | [diff] [blame] | 2822 | } |
| 2823 | |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 2824 | Sema::DeclTy* Sema::ActOnLinkageSpec(SourceLocation Loc, |
Chris Lattner | c81c814 | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 2825 | SourceLocation LBrace, |
| 2826 | SourceLocation RBrace, |
| 2827 | const char *Lang, |
| 2828 | unsigned StrSize, |
| 2829 | DeclTy *D) { |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 2830 | LinkageSpecDecl::LanguageIDs Language; |
| 2831 | Decl *dcl = static_cast<Decl *>(D); |
| 2832 | if (strncmp(Lang, "\"C\"", StrSize) == 0) |
| 2833 | Language = LinkageSpecDecl::lang_c; |
| 2834 | else if (strncmp(Lang, "\"C++\"", StrSize) == 0) |
| 2835 | Language = LinkageSpecDecl::lang_cxx; |
| 2836 | else { |
| 2837 | Diag(Loc, diag::err_bad_language); |
| 2838 | return 0; |
| 2839 | } |
| 2840 | |
| 2841 | // FIXME: Add all the various semantics of linkage specifications |
Chris Lattner | 8e25d86 | 2008-03-16 00:16:02 +0000 | [diff] [blame] | 2842 | return LinkageSpecDecl::Create(Context, Loc, Language, dcl); |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 2843 | } |
Daniel Dunbar | 4cde927 | 2008-10-14 05:35:18 +0000 | [diff] [blame] | 2844 | |
| 2845 | void Sema::ActOnPragmaPack(PragmaPackKind Kind, IdentifierInfo *Name, |
| 2846 | ExprTy *alignment, SourceLocation PragmaLoc, |
| 2847 | SourceLocation LParenLoc, SourceLocation RParenLoc) { |
| 2848 | Expr *Alignment = static_cast<Expr *>(alignment); |
| 2849 | |
| 2850 | // If specified then alignment must be a "small" power of two. |
| 2851 | unsigned AlignmentVal = 0; |
| 2852 | if (Alignment) { |
| 2853 | llvm::APSInt Val; |
| 2854 | if (!Alignment->isIntegerConstantExpr(Val, Context) || |
| 2855 | !Val.isPowerOf2() || |
| 2856 | Val.getZExtValue() > 16) { |
| 2857 | Diag(PragmaLoc, diag::warn_pragma_pack_invalid_alignment); |
| 2858 | delete Alignment; |
| 2859 | return; // Ignore |
| 2860 | } |
| 2861 | |
| 2862 | AlignmentVal = (unsigned) Val.getZExtValue(); |
| 2863 | } |
| 2864 | |
| 2865 | switch (Kind) { |
| 2866 | case Action::PPK_Default: // pack([n]) |
| 2867 | PackContext.setAlignment(AlignmentVal); |
| 2868 | break; |
| 2869 | |
| 2870 | case Action::PPK_Show: // pack(show) |
| 2871 | // Show the current alignment, making sure to show the right value |
| 2872 | // for the default. |
| 2873 | AlignmentVal = PackContext.getAlignment(); |
| 2874 | // FIXME: This should come from the target. |
| 2875 | if (AlignmentVal == 0) |
| 2876 | AlignmentVal = 8; |
| 2877 | Diag(PragmaLoc, diag::warn_pragma_pack_show, llvm::utostr(AlignmentVal)); |
| 2878 | break; |
| 2879 | |
| 2880 | case Action::PPK_Push: // pack(push [, id] [, [n]) |
| 2881 | PackContext.push(Name); |
| 2882 | // Set the new alignment if specified. |
| 2883 | if (Alignment) |
| 2884 | PackContext.setAlignment(AlignmentVal); |
| 2885 | break; |
| 2886 | |
| 2887 | case Action::PPK_Pop: // pack(pop [, id] [, n]) |
| 2888 | // MSDN, C/C++ Preprocessor Reference > Pragma Directives > pack: |
| 2889 | // "#pragma pack(pop, identifier, n) is undefined" |
| 2890 | if (Alignment && Name) |
| 2891 | Diag(PragmaLoc, diag::warn_pragma_pack_pop_identifer_and_alignment); |
| 2892 | |
| 2893 | // Do the pop. |
| 2894 | if (!PackContext.pop(Name)) { |
| 2895 | // If a name was specified then failure indicates the name |
| 2896 | // wasn't found. Otherwise failure indicates the stack was |
| 2897 | // empty. |
| 2898 | Diag(PragmaLoc, diag::warn_pragma_pack_pop_failed, |
| 2899 | Name ? "no record matching name" : "stack empty"); |
| 2900 | |
| 2901 | // FIXME: Warn about popping named records as MSVC does. |
| 2902 | } else { |
| 2903 | // Pop succeeded, set the new alignment if specified. |
| 2904 | if (Alignment) |
| 2905 | PackContext.setAlignment(AlignmentVal); |
| 2906 | } |
| 2907 | break; |
| 2908 | |
| 2909 | default: |
| 2910 | assert(0 && "Invalid #pragma pack kind."); |
| 2911 | } |
| 2912 | } |
| 2913 | |
| 2914 | bool PragmaPackStack::pop(IdentifierInfo *Name) { |
| 2915 | if (Stack.empty()) |
| 2916 | return false; |
| 2917 | |
| 2918 | // If name is empty just pop top. |
| 2919 | if (!Name) { |
| 2920 | Alignment = Stack.back().first; |
| 2921 | Stack.pop_back(); |
| 2922 | return true; |
| 2923 | } |
| 2924 | |
| 2925 | // Otherwise, find the named record. |
| 2926 | for (unsigned i = Stack.size(); i != 0; ) { |
| 2927 | --i; |
| 2928 | if (strcmp(Stack[i].second.c_str(), Name->getName()) == 0) { |
| 2929 | // Found it, pop up to and including this record. |
| 2930 | Alignment = Stack[i].first; |
| 2931 | Stack.erase(Stack.begin() + i, Stack.end()); |
| 2932 | return true; |
| 2933 | } |
| 2934 | } |
| 2935 | |
| 2936 | return false; |
| 2937 | } |