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