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