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