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