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