Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1 | //===--- SemaDecl.cpp - Semantic Analysis for Declarations ----------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 959e5be | 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. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements semantic analysis for declarations. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "Sema.h" |
Anders Carlsson | c7436af | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 15 | #include "clang/AST/APValue.h" |
Chris Lattner | 33aad6e | 2008-02-06 00:51:33 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTConsumer.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | 64789f8 | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclObjC.h" |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 19 | #include "clang/AST/ExprCXX.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 20 | #include "clang/Parse/DeclSpec.h" |
Daniel Dunbar | cc7b160 | 2008-08-11 03:45:03 +0000 | [diff] [blame] | 21 | #include "clang/Basic/Diagnostic.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 22 | #include "clang/Basic/TargetInfo.h" |
Steve Naroff | a9eae58 | 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 | 33aad6e | 2008-02-06 00:51:33 +0000 | [diff] [blame] | 25 | #include "clang/Lex/Preprocessor.h" |
Steve Naroff | a9eae58 | 2008-01-30 23:46:05 +0000 | [diff] [blame] | 26 | #include "clang/Lex/HeaderSearch.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/SmallSet.h" |
Daniel Dunbar | 81c7d47 | 2008-10-14 05:35:18 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/StringExtras.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 29 | using namespace clang; |
| 30 | |
Argiris Kirtzidis | 311db8c | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 31 | Sema::TypeTy *Sema::isTypeName(const IdentifierInfo &II, Scope *S, |
| 32 | const CXXScopeSpec *SS) { |
Argiris Kirtzidis | 054a263 | 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 | 6384a01 | 2008-04-02 14:35:35 +0000 | [diff] [blame] | 40 | |
Douglas Gregor | 1d66155 | 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 | 23f968b | 2007-10-12 16:34:10 +0000 | [diff] [blame] | 44 | return IIDecl; |
Steve Naroff | 81f1bba | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 45 | return 0; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Douglas Gregor | 3ef6c97 | 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 | |
Argiris Kirtzidis | 054a263 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 53 | DeclContext *Sema::getContainingDC(DeclContext *DC) { |
Argiris Kirtzidis | 38f1671 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 54 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(DC)) { |
Argiris Kirtzidis | 054a263 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 55 | // A C++ out-of-line method will return to the file declaration context. |
Argiris Kirtzidis | 881964b | 2008-11-09 23:41:00 +0000 | [diff] [blame^] | 56 | if (MD->isOutOfLineDefinition()) |
| 57 | return MD->getLexicalDeclContext(); |
Argiris Kirtzidis | 054a263 | 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. |
Argiris Kirtzidis | 38f1671 | 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 | |
Argiris Kirtzidis | 881964b | 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(); |
Argiris Kirtzidis | 054a263 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 78 | |
Argiris Kirtzidis | 38f1671 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 79 | return DC->getParent(); |
| 80 | } |
| 81 | |
Chris Lattner | ef87a20 | 2008-04-22 18:39:57 +0000 | [diff] [blame] | 82 | void Sema::PushDeclContext(DeclContext *DC) { |
Argiris Kirtzidis | 054a263 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 83 | assert(getContainingDC(DC) == CurContext && |
Argiris Kirtzidis | 881964b | 2008-11-09 23:41:00 +0000 | [diff] [blame^] | 84 | "The next DeclContext should be lexically contained in the current one."); |
Chris Lattner | ef87a20 | 2008-04-22 18:39:57 +0000 | [diff] [blame] | 85 | CurContext = DC; |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Chris Lattner | f3874bc | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 88 | void Sema::PopDeclContext() { |
| 89 | assert(CurContext && "DeclContext imbalance!"); |
Argiris Kirtzidis | 054a263 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 90 | CurContext = getContainingDC(CurContext); |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Argiris Kirtzidis | 951f25b | 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) { |
Argiris Kirtzidis | 951f25b | 2008-04-12 00:47:19 +0000 | [diff] [blame] | 95 | S->AddDecl(D); |
Argiris Kirtzidis | 59a9afb | 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). |
Argiris Kirtzidis | 9480523 | 2008-07-17 17:49:50 +0000 | [diff] [blame] | 105 | IdentifierResolver::iterator |
| 106 | I = IdResolver.begin(TD->getIdentifier(), |
| 107 | TD->getDeclContext(), false/*LookInParentCtx*/); |
Argiris Kirtzidis | 90842b6 | 2008-09-09 21:18:04 +0000 | [diff] [blame] | 108 | if (I != IdResolver.end() && isDeclInScope(*I, TD->getDeclContext(), S)) { |
Argiris Kirtzidis | 59a9afb | 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 | |
Argiris Kirtzidis | 9480523 | 2008-07-17 17:49:50 +0000 | [diff] [blame] | 113 | IdResolver.AddShadowedDecl(TD, *I); |
Argiris Kirtzidis | 59a9afb | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 114 | return; |
| 115 | } |
Argiris Kirtzidis | 81a5feb | 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 | d2baafd | 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 | } |
Argiris Kirtzidis | 59a9afb | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 150 | } |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 151 | |
Argiris Kirtzidis | 59a9afb | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 152 | IdResolver.AddDecl(D); |
Argiris Kirtzidis | 951f25b | 2008-04-12 00:47:19 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Steve Naroff | 9637a9b | 2007-10-09 22:01:59 +0000 | [diff] [blame] | 155 | void Sema::ActOnPopScope(SourceLocation Loc, Scope *S) { |
Chris Lattner | a754990 | 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!"); |
Argiris Kirtzidis | 59a9afb | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 158 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 159 | for (Scope::decl_iterator I = S->decl_begin(), E = S->decl_end(); |
| 160 | I != E; ++I) { |
Steve Naroff | d21bc0d | 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??"); |
Argiris Kirtzidis | a5c14b2 | 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 | d21bc0d | 2007-09-13 18:10:37 +0000 | [diff] [blame] | 168 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 169 | IdentifierInfo *II = D->getIdentifier(); |
| 170 | if (!II) continue; |
| 171 | |
Ted Kremenek | 40e70e7 | 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. |
Argiris Kirtzidis | a5c14b2 | 2008-06-10 01:32:09 +0000 | [diff] [blame] | 174 | if (S->getFnParent() != 0) |
| 175 | IdResolver.RemoveDecl(D); |
Chris Lattner | 2a1e2ed | 2008-04-11 07:00:53 +0000 | [diff] [blame] | 176 | |
Argiris Kirtzidis | a5c14b2 | 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); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | |
Steve Naroff | e57c21a | 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 | e57c21a | 2008-04-01 23:04:06 +0000 | [diff] [blame] | 185 | ObjCInterfaceDecl *Sema::getObjCInterfaceDecl(IdentifierInfo *Id) { |
Steve Naroff | 1520816 | 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 | dc36dc1 | 2007-10-12 19:38:20 +0000 | [diff] [blame] | 189 | |
Steve Naroff | 6384a01 | 2008-04-02 14:35:35 +0000 | [diff] [blame] | 190 | return dyn_cast_or_null<ObjCInterfaceDecl>(IDecl); |
Fariborz Jahanian | dc36dc1 | 2007-10-12 19:38:20 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Steve Naroff | e57c21a | 2008-04-01 23:04:06 +0000 | [diff] [blame] | 193 | /// LookupDecl - Look up the inner-most declaration in the specified |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 194 | /// namespace. |
Argiris Kirtzidis | 054a263 | 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) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 197 | if (II == 0) return 0; |
Douglas Gregor | 1d66155 | 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 | 2a1e2ed | 2008-04-11 07:00:53 +0000 | [diff] [blame] | 201 | |
Argiris Kirtzidis | 054a263 | 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*/); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +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. |
Argiris Kirtzidis | 054a263 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 208 | for (; I != IdResolver.end(); ++I) |
Argiris Kirtzidis | 59a9afb | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 209 | if ((*I)->getIdentifierNamespace() & NS) |
| 210 | return *I; |
Chris Lattner | 2a1e2ed | 2008-04-11 07:00:53 +0000 | [diff] [blame] | 211 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +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 | 1d66155 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 215 | if (NS & Decl::IDNS_Ordinary) { |
Argiris Kirtzidis | 054a263 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 216 | if (enableLazyBuiltinCreation && |
| 217 | (LookupCtx == 0 || isa<TranslationUnitDecl>(LookupCtx))) { |
Steve Naroff | 6384a01 | 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 | e57c21a | 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 | 64334ea | 2008-04-02 00:39:51 +0000 | [diff] [blame] | 225 | // therefore not scoped decls). They can, however, be shadowed by |
Steve Naroff | e57c21a | 2008-04-01 23:04:06 +0000 | [diff] [blame] | 226 | // other names in IDNS_Ordinary. |
Steve Naroff | 1520816 | 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 | e57c21a | 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 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 234 | } |
| 235 | return 0; |
| 236 | } |
| 237 | |
Chris Lattner | a9c87f2 | 2008-05-05 22:18:14 +0000 | [diff] [blame] | 238 | void Sema::InitBuiltinVaListType() { |
Anders Carlsson | 3676033 | 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 | 6384a01 | 2008-04-02 14:35:35 +0000 | [diff] [blame] | 243 | Decl *VaDecl = LookupDecl(VaIdent, Decl::IDNS_Ordinary, TUScope); |
Steve Naroff | bc8c52e | 2007-10-18 22:17:45 +0000 | [diff] [blame] | 244 | TypedefDecl *VaTypedef = cast<TypedefDecl>(VaDecl); |
Anders Carlsson | 3676033 | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 245 | Context.setBuiltinVaListType(Context.getTypedefType(VaTypedef)); |
| 246 | } |
| 247 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 248 | /// LazilyCreateBuiltin - The specified Builtin-ID was first used at file scope. |
| 249 | /// lazily create a decl for it. |
Chris Lattner | 71c0111 | 2007-10-10 23:42:28 +0000 | [diff] [blame] | 250 | ScopedDecl *Sema::LazilyCreateBuiltin(IdentifierInfo *II, unsigned bid, |
| 251 | Scope *S) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 252 | Builtin::ID BID = (Builtin::ID)bid; |
| 253 | |
Chris Lattner | b23469f | 2008-09-28 05:54:29 +0000 | [diff] [blame] | 254 | if (Context.BuiltinInfo.hasVAListUse(BID)) |
Anders Carlsson | 3676033 | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 255 | InitBuiltinVaListType(); |
| 256 | |
Anders Carlsson | fb5b1e8 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 257 | QualType R = Context.BuiltinInfo.GetBuiltinType(BID, Context); |
Argiris Kirtzidis | 9d0d8bf | 2008-04-17 14:47:13 +0000 | [diff] [blame] | 258 | FunctionDecl *New = FunctionDecl::Create(Context, |
| 259 | Context.getTranslationUnitDecl(), |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 260 | SourceLocation(), II, R, |
Chris Lattner | 4c7802b | 2008-03-15 21:24:04 +0000 | [diff] [blame] | 261 | FunctionDecl::Extern, false, 0); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 262 | |
Chris Lattner | a9c87f2 | 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 | 2a1e2ed | 2008-04-11 07:00:53 +0000 | [diff] [blame] | 276 | // TUScope is the translation-unit scope to insert this function into. |
Argiris Kirtzidis | 59a9afb | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 277 | PushOnScopeChains(New, TUScope); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +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 | e57c21a | 2008-04-01 23:04:06 +0000 | [diff] [blame] | 285 | TypedefDecl *Sema::MergeTypeDefDecl(TypedefDecl *New, Decl *OldD) { |
Steve Naroff | 453a878 | 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 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +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 | bef8d62 | 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 | 324d503 | 2008-06-11 06:20:39 +0000 | [diff] [blame] | 326 | if (getLangOptions().Microsoft) return New; |
| 327 | |
Steve Naroff | a9eae58 | 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 | 4dbd857 | 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 | 324d503 | 2008-06-11 06:20:39 +0000 | [diff] [blame] | 340 | |
Ted Kremenek | 64845ce | 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); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 343 | return New; |
| 344 | } |
| 345 | |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 346 | /// DeclhasAttr - returns true if decl Declaration already has the target |
| 347 | /// attribute. |
Chris Lattner | 402b337 | 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 | 402b337 | 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 | 7765434 | 2008-06-01 22:53:53 +0000 | [diff] [blame] | 371 | |
| 372 | Old->invalidateAttrs(); |
Chris Lattner | 402b337 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 373 | } |
| 374 | |
Chris Lattner | 3e254fb | 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 | d2baafd | 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 | 42214c5 | 2008-04-21 02:02:58 +0000 | [diff] [blame] | 385 | FunctionDecl * |
| 386 | Sema::MergeFunctionDecl(FunctionDecl *New, Decl *OldD, bool &Redeclaration) { |
Douglas Gregor | d2baafd | 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 | 42214c5 | 2008-04-21 02:02:58 +0000 | [diff] [blame] | 390 | Redeclaration = false; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +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 | d2baafd | 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 | 3e254fb | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 409 | |
Chris Lattner | 42a2174 | 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 | 60476ff | 2007-11-20 19:04:50 +0000 | [diff] [blame] | 412 | |
Douglas Gregor | d2baafd | 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 | 42214c5 | 2008-04-21 02:02:58 +0000 | [diff] [blame] | 452 | } |
Chris Lattner | 3e254fb | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 453 | |
| 454 | // C: Function types need to be compatible, not identical. This handles |
Steve Naroff | 1d5bd64 | 2008-01-14 20:51:29 +0000 | [diff] [blame] | 455 | // duplicate function decls like "void f(int); void f(enum X);" properly. |
Chris Lattner | 3e254fb | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 456 | if (!getLangOptions().CPlusPlus && |
Eli Friedman | 0d9549b | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 457 | Context.typesAreCompatible(OldQType, NewQType)) { |
Douglas Gregor | 42214c5 | 2008-04-21 02:02:58 +0000 | [diff] [blame] | 458 | MergeAttributes(New, Old); |
| 459 | Redeclaration = true; |
Steve Naroff | 1d5bd64 | 2008-01-14 20:51:29 +0000 | [diff] [blame] | 460 | return New; |
Chris Lattner | 3e254fb | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 461 | } |
Chris Lattner | 1470b07 | 2007-11-06 06:07:26 +0000 | [diff] [blame] | 462 | |
Steve Naroff | 6c9e792 | 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 | 6c9e792 | 2008-01-16 15:01:34 +0000 | [diff] [blame] | 465 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +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 | 6c9e792 | 2008-01-16 15:01:34 +0000 | [diff] [blame] | 469 | Diag(New->getLocation(), diag::err_conflicting_types, New->getName()); |
| 470 | Diag(Old->getLocation(), PrevDiag); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 471 | return New; |
| 472 | } |
| 473 | |
Steve Naroff | b5e7815 | 2008-08-08 17:50:35 +0000 | [diff] [blame] | 474 | /// Predicate for C "tentative" external object definitions (C99 6.9.2). |
Steve Naroff | d580209 | 2008-08-10 15:28:06 +0000 | [diff] [blame] | 475 | static bool isTentativeDefinition(VarDecl *VD) { |
Steve Naroff | b5e7815 | 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 | 4b6bd3c | 2008-08-10 15:20:13 +0000 | [diff] [blame] | 487 | bool VDIsIncompleteArray = VD->getType()->isIncompleteArrayType(); |
Steve Naroff | b5e7815 | 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) { |
Argiris Kirtzidis | 90842b6 | 2008-09-09 21:18:04 +0000 | [diff] [blame] | 493 | if (*I != VD && isDeclInScope(*I, VD->getDeclContext(), S)) { |
Steve Naroff | b5e7815 | 2008-08-08 17:50:35 +0000 | [diff] [blame] | 494 | VarDecl *OldDecl = dyn_cast<VarDecl>(*I); |
| 495 | |
Steve Naroff | 4b6bd3c | 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 | b5e7815 | 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 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +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 | b5e7815 | 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. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 528 | /// |
Steve Naroff | e57c21a | 2008-04-01 23:04:06 +0000 | [diff] [blame] | 529 | VarDecl *Sema::MergeVarDecl(VarDecl *New, Decl *OldD) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +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 | 402b337 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 538 | |
| 539 | MergeAttributes(New, Old); |
| 540 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 541 | // Verify the types match. |
Chris Lattner | 42a2174 | 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 | 1250817 | 2008-08-09 16:04:40 +0000 | [diff] [blame] | 544 | if (OldCType != NewCType && !Context.typesAreCompatible(OldCType, NewCType)) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +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 | b00247f | 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 | 2f3c443 | 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()) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +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 | 3e254fb | 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 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +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 | edafc0b | 2007-11-17 21:37:36 +0000 | [diff] [blame] | 604 | return dyn_cast_or_null<TagDecl>(static_cast<Decl *>(DS.getTypeRep())); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 605 | } |
| 606 | |
Steve Naroff | f0b2354 | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 607 | bool Sema::CheckSingleInitializer(Expr *&Init, QualType DeclType) { |
Steve Naroff | e14e554 | 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 | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 610 | QualType InitType = Init->getType(); |
Steve Naroff | e14e554 | 2007-09-02 02:04:30 +0000 | [diff] [blame] | 611 | |
Chris Lattner | 005ed75 | 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 | e14e554 | 2007-09-02 02:04:30 +0000 | [diff] [blame] | 615 | } |
| 616 | |
Steve Naroff | 0d4e6ad | 2008-01-22 00:55:40 +0000 | [diff] [blame] | 617 | bool Sema::CheckStringLiteralInit(StringLiteral *strLiteral, QualType &DeclT) { |
Chris Lattner | a1923f6 | 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 | 0d4e6ad | 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 | 8ff0778 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 626 | DeclT = Context.getConstantArrayType(IAT->getElementType(), ConstVal, |
Steve Naroff | 0d4e6ad | 2008-01-22 00:55:40 +0000 | [diff] [blame] | 627 | ArrayType::Normal, 0); |
Chris Lattner | a1923f6 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 628 | } else { |
| 629 | const ConstantArrayType *CAT = cast<ConstantArrayType>(AT); |
Steve Naroff | 0d4e6ad | 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 | a1923f6 | 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 | 0d4e6ad | 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 | 0d4e6ad | 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 | a1923f6 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 644 | const ArrayType *AT = Context.getAsArrayType(DeclType); |
Steve Naroff | f3cb514 | 2008-01-25 00:51:06 +0000 | [diff] [blame] | 645 | if (AT && AT->getElementType()->isCharType()) { |
| 646 | return dyn_cast<StringLiteral>(Init); |
| 647 | } |
Steve Naroff | 0d4e6ad | 2008-01-22 00:55:40 +0000 | [diff] [blame] | 648 | return 0; |
| 649 | } |
| 650 | |
Douglas Gregor | 6428e76 | 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 | 81c2915 | 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 | 8e9337f | 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 | a1923f6 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 663 | if (const VariableArrayType *VAT = Context.getAsVariableArrayType(DeclType)) |
Douglas Gregor | 6428e76 | 2008-11-05 15:29:30 +0000 | [diff] [blame] | 664 | return Diag(InitLoc, |
Steve Naroff | 8e9337f | 2008-01-21 23:53:58 +0000 | [diff] [blame] | 665 | diag::err_variable_object_no_init, |
| 666 | VAT->getSizeExpr()->getSourceRange()); |
| 667 | |
Steve Naroff | cb69fb7 | 2007-12-10 22:44:33 +0000 | [diff] [blame] | 668 | InitListExpr *InitList = dyn_cast<InitListExpr>(Init); |
| 669 | if (!InitList) { |
Steve Naroff | 0d4e6ad | 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 | 6528099 | 2008-02-08 00:48:24 +0000 | [diff] [blame] | 673 | |
Douglas Gregor | 6428e76 | 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 | b2f7241 | 2008-09-29 20:07:05 +0000 | [diff] [blame] | 718 | // C99 6.7.8p16. |
Eli Friedman | 6528099 | 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 | f0b2354 | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 724 | return CheckSingleInitializer(Init, DeclType); |
Douglas Gregor | 15e0462 | 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 | cb69fb7 | 2007-12-10 22:44:33 +0000 | [diff] [blame] | 742 | } |
Eli Friedman | 38b7a91 | 2008-06-06 19:40:52 +0000 | [diff] [blame] | 743 | |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 744 | InitListChecker CheckInitList(this, InitList, DeclType); |
| 745 | return CheckInitList.HadError(); |
Steve Naroff | e14e554 | 2007-09-02 02:04:30 +0000 | [diff] [blame] | 746 | } |
| 747 | |
Fariborz Jahanian | dfb1c37 | 2007-11-08 23:49:49 +0000 | [diff] [blame] | 748 | Sema::DeclTy * |
Daniel Dunbar | 72eaf8a | 2008-08-05 16:28:08 +0000 | [diff] [blame] | 749 | Sema::ActOnDeclarator(Scope *S, Declarator &D, DeclTy *lastDecl) { |
Steve Naroff | 2591e1b | 2007-09-13 23:52:58 +0000 | [diff] [blame] | 750 | ScopedDecl *LastDeclarator = dyn_cast_or_null<ScopedDecl>((Decl *)lastDecl); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 751 | IdentifierInfo *II = D.getIdentifier(); |
| 752 | |
| 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 | 6fe8b27 | 2007-10-16 22:36:42 +0000 | [diff] [blame] | 756 | Diag(D.getDeclSpec().getSourceRange().getBegin(), |
Chris Lattner | 87492f4 | 2007-08-28 06:17:15 +0000 | [diff] [blame] | 757 | diag::err_declarator_need_ident, |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 758 | D.getDeclSpec().getSourceRange(), D.getSourceRange()); |
| 759 | return 0; |
| 760 | } |
| 761 | |
Chris Lattner | a754990 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 762 | // The scope passed in may not be a decl scope. Zip up the scope tree until |
| 763 | // we find one that is. |
| 764 | while ((S->getFlags() & Scope::DeclScope) == 0) |
| 765 | S = S->getParent(); |
| 766 | |
Argiris Kirtzidis | 054a263 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 767 | DeclContext *DC; |
| 768 | Decl *PrevDecl; |
Steve Naroff | d21bc0d | 2007-09-13 18:10:37 +0000 | [diff] [blame] | 769 | ScopedDecl *New; |
Steve Naroff | d1ad6ae | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 770 | bool InvalidDecl = false; |
Douglas Gregor | 1d66155 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 771 | |
Argiris Kirtzidis | 054a263 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 772 | // See if this is a redefinition of a variable in the same scope. |
| 773 | if (!D.getCXXScopeSpec().isSet()) { |
| 774 | DC = CurContext; |
| 775 | PrevDecl = LookupDecl(II, Decl::IDNS_Ordinary, S); |
| 776 | } else { // Something like "int foo::x;" |
| 777 | DC = static_cast<DeclContext*>(D.getCXXScopeSpec().getScopeRep()); |
| 778 | PrevDecl = LookupDecl(II, Decl::IDNS_Ordinary, S, DC); |
| 779 | |
| 780 | // C++ 7.3.1.2p2: |
| 781 | // Members (including explicit specializations of templates) of a named |
| 782 | // namespace can also be defined outside that namespace by explicit |
| 783 | // qualification of the name being defined, provided that the entity being |
| 784 | // defined was already declared in the namespace and the definition appears |
| 785 | // after the point of declaration in a namespace that encloses the |
| 786 | // declarations namespace. |
| 787 | // |
| 788 | if (PrevDecl == 0) { |
| 789 | // No previous declaration in the qualifying scope. |
| 790 | Diag(D.getIdentifierLoc(), diag::err_typecheck_no_member, |
| 791 | II->getName(), D.getCXXScopeSpec().getRange()); |
| 792 | } else if (!CurContext->Encloses(DC)) { |
| 793 | // The qualifying scope doesn't enclose the original declaration. |
| 794 | // Emit diagnostic based on current scope. |
| 795 | SourceLocation L = D.getIdentifierLoc(); |
| 796 | SourceRange R = D.getCXXScopeSpec().getRange(); |
| 797 | if (isa<FunctionDecl>(CurContext)) { |
| 798 | Diag(L, diag::err_invalid_declarator_in_function, II->getName(), R); |
| 799 | } else { |
| 800 | Diag(L, diag::err_invalid_declarator_scope, II->getName(), |
| 801 | cast<NamedDecl>(DC)->getName(), R); |
| 802 | } |
| 803 | } |
| 804 | } |
| 805 | |
Douglas Gregor | 1d66155 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 806 | // In C++, the previous declaration we find might be a tag type |
| 807 | // (class or enum). In this case, the new declaration will hide the |
| 808 | // tag type. |
| 809 | if (PrevDecl && PrevDecl->getIdentifierNamespace() == Decl::IDNS_Tag) |
| 810 | PrevDecl = 0; |
| 811 | |
Chris Lattner | 82bb479 | 2007-11-14 06:34:38 +0000 | [diff] [blame] | 812 | QualType R = GetTypeForDeclarator(D, S); |
| 813 | assert(!R.isNull() && "GetTypeForDeclarator() returned null type"); |
| 814 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 815 | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) { |
Douglas Gregor | 2b9422f | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 816 | // Check that there are no default arguments (C++ only). |
| 817 | if (getLangOptions().CPlusPlus) |
| 818 | CheckExtraCXXDefaultArguments(D); |
| 819 | |
Chris Lattner | 82bb479 | 2007-11-14 06:34:38 +0000 | [diff] [blame] | 820 | TypedefDecl *NewTD = ParseTypedefDecl(S, D, R, LastDeclarator); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 821 | if (!NewTD) return 0; |
| 822 | |
| 823 | // Handle attributes prior to checking for duplicates in MergeVarDecl |
Chris Lattner | 9b384ca | 2008-06-29 00:02:00 +0000 | [diff] [blame] | 824 | ProcessDeclAttributes(NewTD, D); |
Steve Naroff | f8a0943 | 2008-01-09 23:34:55 +0000 | [diff] [blame] | 825 | // Merge the decl with the existing one if appropriate. If the decl is |
| 826 | // in an outer scope, it isn't the same thing. |
Argiris Kirtzidis | 054a263 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 827 | if (PrevDecl && isDeclInScope(PrevDecl, DC, S)) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 828 | NewTD = MergeTypeDefDecl(NewTD, PrevDecl); |
| 829 | if (NewTD == 0) return 0; |
| 830 | } |
| 831 | New = NewTD; |
Argiris Kirtzidis | 59a9afb | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 832 | if (S->getFnParent() == 0) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 833 | // C99 6.7.7p2: If a typedef name specifies a variably modified type |
| 834 | // then it shall have block scope. |
Eli Friedman | e007979 | 2008-02-15 12:53:51 +0000 | [diff] [blame] | 835 | if (NewTD->getUnderlyingType()->isVariablyModifiedType()) { |
| 836 | // FIXME: Diagnostic needs to be fixed. |
| 837 | Diag(D.getIdentifierLoc(), diag::err_typecheck_illegal_vla); |
Steve Naroff | 5eb879b | 2007-08-31 17:20:07 +0000 | [diff] [blame] | 838 | InvalidDecl = true; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 839 | } |
| 840 | } |
Chris Lattner | 82bb479 | 2007-11-14 06:34:38 +0000 | [diff] [blame] | 841 | } else if (R.getTypePtr()->isFunctionType()) { |
Chris Lattner | 265c817 | 2007-09-27 15:15:46 +0000 | [diff] [blame] | 842 | FunctionDecl::StorageClass SC = FunctionDecl::None; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 843 | switch (D.getDeclSpec().getStorageClassSpec()) { |
| 844 | default: assert(0 && "Unknown storage class!"); |
| 845 | case DeclSpec::SCS_auto: |
| 846 | case DeclSpec::SCS_register: |
| 847 | Diag(D.getIdentifierLoc(), diag::err_typecheck_sclass_func, |
| 848 | R.getAsString()); |
Steve Naroff | d1ad6ae | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 849 | InvalidDecl = true; |
| 850 | break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 851 | case DeclSpec::SCS_unspecified: SC = FunctionDecl::None; break; |
| 852 | case DeclSpec::SCS_extern: SC = FunctionDecl::Extern; break; |
| 853 | case DeclSpec::SCS_static: SC = FunctionDecl::Static; break; |
Steve Naroff | d404c35 | 2008-01-28 21:57:15 +0000 | [diff] [blame] | 854 | case DeclSpec::SCS_private_extern: SC = FunctionDecl::PrivateExtern;break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 855 | } |
| 856 | |
Chris Lattner | 4c7802b | 2008-03-15 21:24:04 +0000 | [diff] [blame] | 857 | bool isInline = D.getDeclSpec().isInlineSpecified(); |
Douglas Gregor | 8210a8e | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 858 | // bool isVirtual = D.getDeclSpec().isVirtualSpecified(); |
Douglas Gregor | f15ac4b | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 859 | bool isExplicit = D.getDeclSpec().isExplicitSpecified(); |
| 860 | |
Argiris Kirtzidis | 38f1671 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 861 | FunctionDecl *NewFD; |
Douglas Gregor | 8210a8e | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 862 | if (D.getKind() == Declarator::DK_Constructor) { |
Douglas Gregor | f15ac4b | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 863 | // This is a C++ constructor declaration. |
Argiris Kirtzidis | 054a263 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 864 | assert(DC->isCXXRecord() && |
Douglas Gregor | f15ac4b | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 865 | "Constructors can only be declared in a member context"); |
| 866 | |
Douglas Gregor | 8210a8e | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 867 | bool isInvalidDecl = CheckConstructorDeclarator(D, R, SC); |
Douglas Gregor | f15ac4b | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 868 | |
| 869 | // Create the new declaration |
| 870 | NewFD = CXXConstructorDecl::Create(Context, |
Argiris Kirtzidis | 054a263 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 871 | cast<CXXRecordDecl>(DC), |
Douglas Gregor | f15ac4b | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 872 | D.getIdentifierLoc(), II, R, |
| 873 | isExplicit, isInline, |
| 874 | /*isImplicitlyDeclared=*/false); |
| 875 | |
Douglas Gregor | 8210a8e | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 876 | if (isInvalidDecl) |
| 877 | NewFD->setInvalidDecl(); |
| 878 | } else if (D.getKind() == Declarator::DK_Destructor) { |
| 879 | // This is a C++ destructor declaration. |
Argiris Kirtzidis | 054a263 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 880 | if (DC->isCXXRecord()) { |
Argiris Kirtzidis | c9e909c | 2008-11-07 22:02:30 +0000 | [diff] [blame] | 881 | bool isInvalidDecl = CheckDestructorDeclarator(D, R, SC); |
Douglas Gregor | 8210a8e | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 882 | |
Argiris Kirtzidis | c9e909c | 2008-11-07 22:02:30 +0000 | [diff] [blame] | 883 | NewFD = CXXDestructorDecl::Create(Context, |
Argiris Kirtzidis | 054a263 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 884 | cast<CXXRecordDecl>(DC), |
Argiris Kirtzidis | c9e909c | 2008-11-07 22:02:30 +0000 | [diff] [blame] | 885 | D.getIdentifierLoc(), II, R, |
| 886 | isInline, |
| 887 | /*isImplicitlyDeclared=*/false); |
Douglas Gregor | 8210a8e | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 888 | |
Argiris Kirtzidis | c9e909c | 2008-11-07 22:02:30 +0000 | [diff] [blame] | 889 | if (isInvalidDecl) |
| 890 | NewFD->setInvalidDecl(); |
| 891 | } else { |
| 892 | Diag(D.getIdentifierLoc(), diag::err_destructor_not_member); |
| 893 | // Create a FunctionDecl to satisfy the function definition parsing |
| 894 | // code path. |
Argiris Kirtzidis | 054a263 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 895 | NewFD = FunctionDecl::Create(Context, DC, D.getIdentifierLoc(), |
Argiris Kirtzidis | c9e909c | 2008-11-07 22:02:30 +0000 | [diff] [blame] | 896 | II, R, SC, isInline, LastDeclarator, |
| 897 | // FIXME: Move to DeclGroup... |
| 898 | D.getDeclSpec().getSourceRange().getBegin()); |
Douglas Gregor | 8210a8e | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 899 | NewFD->setInvalidDecl(); |
Argiris Kirtzidis | c9e909c | 2008-11-07 22:02:30 +0000 | [diff] [blame] | 900 | } |
Douglas Gregor | 3ef6c97 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 901 | } else if (D.getKind() == Declarator::DK_Conversion) { |
Argiris Kirtzidis | 054a263 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 902 | if (!DC->isCXXRecord()) { |
Douglas Gregor | 3ef6c97 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 903 | Diag(D.getIdentifierLoc(), |
| 904 | diag::err_conv_function_not_member); |
| 905 | return 0; |
| 906 | } else { |
| 907 | bool isInvalidDecl = CheckConversionDeclarator(D, R, SC); |
| 908 | |
| 909 | NewFD = CXXConversionDecl::Create(Context, |
Argiris Kirtzidis | 054a263 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 910 | cast<CXXRecordDecl>(DC), |
Douglas Gregor | 3ef6c97 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 911 | D.getIdentifierLoc(), II, R, |
| 912 | isInline, isExplicit); |
| 913 | |
| 914 | if (isInvalidDecl) |
| 915 | NewFD->setInvalidDecl(); |
| 916 | } |
Argiris Kirtzidis | 054a263 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 917 | } else if (DC->isCXXRecord()) { |
Argiris Kirtzidis | 38f1671 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 918 | // This is a C++ method declaration. |
Argiris Kirtzidis | 054a263 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 919 | NewFD = CXXMethodDecl::Create(Context, cast<CXXRecordDecl>(DC), |
Argiris Kirtzidis | 38f1671 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 920 | D.getIdentifierLoc(), II, R, |
| 921 | (SC == FunctionDecl::Static), isInline, |
| 922 | LastDeclarator); |
| 923 | } else { |
Argiris Kirtzidis | 054a263 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 924 | NewFD = FunctionDecl::Create(Context, DC, |
Argiris Kirtzidis | 38f1671 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 925 | D.getIdentifierLoc(), |
Steve Naroff | 71cd776 | 2008-10-03 00:02:03 +0000 | [diff] [blame] | 926 | II, R, SC, isInline, LastDeclarator, |
| 927 | // FIXME: Move to DeclGroup... |
| 928 | D.getDeclSpec().getSourceRange().getBegin()); |
Argiris Kirtzidis | 38f1671 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 929 | } |
Ted Kremenek | 117f186 | 2008-02-27 22:18:07 +0000 | [diff] [blame] | 930 | // Handle attributes. |
Chris Lattner | 9b384ca | 2008-06-29 00:02:00 +0000 | [diff] [blame] | 931 | ProcessDeclAttributes(NewFD, D); |
Chris Lattner | 3e254fb | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 932 | |
Daniel Dunbar | c3540ff | 2008-08-05 01:35:17 +0000 | [diff] [blame] | 933 | // Handle GNU asm-label extension (encoded as an attribute). |
Daniel Dunbar | 72eaf8a | 2008-08-05 16:28:08 +0000 | [diff] [blame] | 934 | if (Expr *E = (Expr*) D.getAsmLabel()) { |
Daniel Dunbar | c3540ff | 2008-08-05 01:35:17 +0000 | [diff] [blame] | 935 | // The parser guarantees this is a string. |
| 936 | StringLiteral *SE = cast<StringLiteral>(E); |
| 937 | NewFD->addAttr(new AsmLabelAttr(std::string(SE->getStrData(), |
| 938 | SE->getByteLength()))); |
| 939 | } |
| 940 | |
Chris Lattner | 3e254fb | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 941 | // Copy the parameter declarations from the declarator D to |
| 942 | // the function declaration NewFD, if they are available. |
Eli Friedman | 769e730 | 2008-08-25 21:31:01 +0000 | [diff] [blame] | 943 | if (D.getNumTypeObjects() > 0) { |
Chris Lattner | 3e254fb | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 944 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun; |
| 945 | |
| 946 | // Create Decl objects for each parameter, adding them to the |
| 947 | // FunctionDecl. |
| 948 | llvm::SmallVector<ParmVarDecl*, 16> Params; |
| 949 | |
| 950 | // Check for C99 6.7.5.3p10 - foo(void) is a non-varargs |
| 951 | // function that takes no arguments, not a function that takes a |
Chris Lattner | 97316c0 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 952 | // single void argument. |
Eli Friedman | 910758e | 2008-05-22 08:54:03 +0000 | [diff] [blame] | 953 | // We let through "const void" here because Sema::GetTypeForDeclarator |
| 954 | // already checks for that case. |
Chris Lattner | 3e254fb | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 955 | if (FTI.NumArgs == 1 && !FTI.isVariadic && FTI.ArgInfo[0].Ident == 0 && |
| 956 | FTI.ArgInfo[0].Param && |
Chris Lattner | 3e254fb | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 957 | ((ParmVarDecl*)FTI.ArgInfo[0].Param)->getType()->isVoidType()) { |
| 958 | // empty arg list, don't push any params. |
Chris Lattner | 97316c0 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 959 | ParmVarDecl *Param = (ParmVarDecl*)FTI.ArgInfo[0].Param; |
| 960 | |
Chris Lattner | da7b5f0 | 2008-04-10 02:26:16 +0000 | [diff] [blame] | 961 | // In C++, the empty parameter-type-list must be spelled "void"; a |
| 962 | // typedef of void is not permitted. |
| 963 | if (getLangOptions().CPlusPlus && |
Eli Friedman | 910758e | 2008-05-22 08:54:03 +0000 | [diff] [blame] | 964 | Param->getType().getUnqualifiedType() != Context.VoidTy) { |
Chris Lattner | 97316c0 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 965 | Diag(Param->getLocation(), diag::ext_param_typedef_of_void); |
| 966 | } |
Eli Friedman | 769e730 | 2008-08-25 21:31:01 +0000 | [diff] [blame] | 967 | } else if (FTI.NumArgs > 0 && FTI.ArgInfo[0].Param != 0) { |
Chris Lattner | 3e254fb | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 968 | for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) |
| 969 | Params.push_back((ParmVarDecl *)FTI.ArgInfo[i].Param); |
| 970 | } |
| 971 | |
| 972 | NewFD->setParams(&Params[0], Params.size()); |
Douglas Gregor | ba3e8b7 | 2008-10-24 18:09:54 +0000 | [diff] [blame] | 973 | } else if (R->getAsTypedefType()) { |
| 974 | // When we're declaring a function with a typedef, as in the |
| 975 | // following example, we'll need to synthesize (unnamed) |
| 976 | // parameters for use in the declaration. |
| 977 | // |
| 978 | // @code |
| 979 | // typedef void fn(int); |
| 980 | // fn f; |
| 981 | // @endcode |
| 982 | const FunctionTypeProto *FT = R->getAsFunctionTypeProto(); |
| 983 | if (!FT) { |
| 984 | // This is a typedef of a function with no prototype, so we |
| 985 | // don't need to do anything. |
| 986 | } else if ((FT->getNumArgs() == 0) || |
| 987 | (FT->getNumArgs() == 1 && !FT->isVariadic() && |
| 988 | FT->getArgType(0)->isVoidType())) { |
| 989 | // This is a zero-argument function. We don't need to do anything. |
| 990 | } else { |
| 991 | // Synthesize a parameter for each argument type. |
| 992 | llvm::SmallVector<ParmVarDecl*, 16> Params; |
| 993 | for (FunctionTypeProto::arg_type_iterator ArgType = FT->arg_type_begin(); |
| 994 | ArgType != FT->arg_type_end(); ++ArgType) { |
Argiris Kirtzidis | 054a263 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 995 | Params.push_back(ParmVarDecl::Create(Context, DC, |
Douglas Gregor | ba3e8b7 | 2008-10-24 18:09:54 +0000 | [diff] [blame] | 996 | SourceLocation(), 0, |
| 997 | *ArgType, VarDecl::None, |
| 998 | 0, 0)); |
| 999 | } |
| 1000 | |
| 1001 | NewFD->setParams(&Params[0], Params.size()); |
| 1002 | } |
Chris Lattner | 3e254fb | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 1003 | } |
| 1004 | |
Douglas Gregor | 8210a8e | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1005 | // C++ constructors and destructors are handled by separate |
| 1006 | // routines, since they don't require any declaration merging (C++ |
| 1007 | // [class.mfct]p2) and they aren't ever pushed into scope, because |
| 1008 | // they can't be found by name lookup anyway (C++ [class.ctor]p2). |
| 1009 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(NewFD)) |
| 1010 | return ActOnConstructorDeclarator(Constructor); |
| 1011 | else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(NewFD)) |
| 1012 | return ActOnDestructorDeclarator(Destructor); |
Douglas Gregor | 3ef6c97 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1013 | else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(NewFD)) |
| 1014 | return ActOnConversionDeclarator(Conversion); |
Douglas Gregor | f15ac4b | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1015 | |
Douglas Gregor | e60e5d3 | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 1016 | // Extra checking for C++ overloaded operators (C++ [over.oper]). |
| 1017 | if (NewFD->isOverloadedOperator() && |
| 1018 | CheckOverloadedOperatorDeclaration(NewFD)) |
| 1019 | NewFD->setInvalidDecl(); |
| 1020 | |
Steve Naroff | f8a0943 | 2008-01-09 23:34:55 +0000 | [diff] [blame] | 1021 | // Merge the decl with the existing one if appropriate. Since C functions |
| 1022 | // are in a flat namespace, make sure we consider decls in outer scopes. |
Argiris Kirtzidis | 59a9afb | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 1023 | if (PrevDecl && |
Argiris Kirtzidis | 054a263 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 1024 | (!getLangOptions().CPlusPlus||isDeclInScope(PrevDecl, DC, S))) { |
Douglas Gregor | 42214c5 | 2008-04-21 02:02:58 +0000 | [diff] [blame] | 1025 | bool Redeclaration = false; |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1026 | |
| 1027 | // If C++, determine whether NewFD is an overload of PrevDecl or |
| 1028 | // a declaration that requires merging. If it's an overload, |
| 1029 | // there's no more work to do here; we'll just add the new |
| 1030 | // function to the scope. |
| 1031 | OverloadedFunctionDecl::function_iterator MatchedDecl; |
| 1032 | if (!getLangOptions().CPlusPlus || |
| 1033 | !IsOverload(NewFD, PrevDecl, MatchedDecl)) { |
| 1034 | Decl *OldDecl = PrevDecl; |
| 1035 | |
| 1036 | // If PrevDecl was an overloaded function, extract the |
| 1037 | // FunctionDecl that matched. |
| 1038 | if (isa<OverloadedFunctionDecl>(PrevDecl)) |
| 1039 | OldDecl = *MatchedDecl; |
| 1040 | |
| 1041 | // NewFD and PrevDecl represent declarations that need to be |
| 1042 | // merged. |
| 1043 | NewFD = MergeFunctionDecl(NewFD, OldDecl, Redeclaration); |
| 1044 | |
| 1045 | if (NewFD == 0) return 0; |
| 1046 | if (Redeclaration) { |
| 1047 | NewFD->setPreviousDeclaration(cast<FunctionDecl>(OldDecl)); |
| 1048 | |
| 1049 | if (OldDecl == PrevDecl) { |
| 1050 | // Remove the name binding for the previous |
| 1051 | // declaration. We'll add the binding back later, but then |
| 1052 | // it will refer to the new declaration (which will |
| 1053 | // contain more information). |
| 1054 | IdResolver.RemoveDecl(cast<NamedDecl>(PrevDecl)); |
| 1055 | } else { |
| 1056 | // We need to update the OverloadedFunctionDecl with the |
| 1057 | // latest declaration of this function, so that name |
| 1058 | // lookup will always refer to the latest declaration of |
| 1059 | // this function. |
| 1060 | *MatchedDecl = NewFD; |
| 1061 | |
| 1062 | // Add the redeclaration to the current scope, since we'll |
| 1063 | // be skipping PushOnScopeChains. |
| 1064 | S->AddDecl(NewFD); |
| 1065 | |
| 1066 | return NewFD; |
| 1067 | } |
| 1068 | } |
Douglas Gregor | 42214c5 | 2008-04-21 02:02:58 +0000 | [diff] [blame] | 1069 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1070 | } |
| 1071 | New = NewFD; |
Chris Lattner | 3e254fb | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 1072 | |
| 1073 | // In C++, check default arguments now that we have merged decls. |
| 1074 | if (getLangOptions().CPlusPlus) |
| 1075 | CheckCXXDefaultArguments(NewFD); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1076 | } else { |
Douglas Gregor | 2b9422f | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 1077 | // Check that there are no default arguments (C++ only). |
| 1078 | if (getLangOptions().CPlusPlus) |
| 1079 | CheckExtraCXXDefaultArguments(D); |
| 1080 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1081 | if (R.getTypePtr()->isObjCInterfaceType()) { |
Fariborz Jahanian | 550e050 | 2007-10-12 22:10:42 +0000 | [diff] [blame] | 1082 | Diag(D.getIdentifierLoc(), diag::err_statically_allocated_object, |
| 1083 | D.getIdentifier()->getName()); |
| 1084 | InvalidDecl = true; |
| 1085 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1086 | |
| 1087 | VarDecl *NewVD; |
| 1088 | VarDecl::StorageClass SC; |
| 1089 | switch (D.getDeclSpec().getStorageClassSpec()) { |
Chris Lattner | 48d225c | 2008-03-15 21:10:16 +0000 | [diff] [blame] | 1090 | default: assert(0 && "Unknown storage class!"); |
| 1091 | case DeclSpec::SCS_unspecified: SC = VarDecl::None; break; |
| 1092 | case DeclSpec::SCS_extern: SC = VarDecl::Extern; break; |
| 1093 | case DeclSpec::SCS_static: SC = VarDecl::Static; break; |
| 1094 | case DeclSpec::SCS_auto: SC = VarDecl::Auto; break; |
| 1095 | case DeclSpec::SCS_register: SC = VarDecl::Register; break; |
| 1096 | case DeclSpec::SCS_private_extern: SC = VarDecl::PrivateExtern; break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1097 | } |
Argiris Kirtzidis | 054a263 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 1098 | if (DC->isCXXRecord()) { |
Argiris Kirtzidis | 38f1671 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1099 | assert(SC == VarDecl::Static && "Invalid storage class for member!"); |
| 1100 | // This is a static data member for a C++ class. |
Argiris Kirtzidis | 054a263 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 1101 | NewVD = CXXClassVarDecl::Create(Context, cast<CXXRecordDecl>(DC), |
Argiris Kirtzidis | 38f1671 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1102 | D.getIdentifierLoc(), II, |
| 1103 | R, LastDeclarator); |
Steve Naroff | e14e554 | 2007-09-02 02:04:30 +0000 | [diff] [blame] | 1104 | } else { |
Daniel Dunbar | 5eea562 | 2008-09-08 20:05:47 +0000 | [diff] [blame] | 1105 | bool ThreadSpecified = D.getDeclSpec().isThreadSpecified(); |
Argiris Kirtzidis | 38f1671 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1106 | if (S->getFnParent() == 0) { |
| 1107 | // C99 6.9p2: The storage-class specifiers auto and register shall not |
| 1108 | // appear in the declaration specifiers in an external declaration. |
| 1109 | if (SC == VarDecl::Auto || SC == VarDecl::Register) { |
| 1110 | Diag(D.getIdentifierLoc(), diag::err_typecheck_sclass_fscope, |
| 1111 | R.getAsString()); |
| 1112 | InvalidDecl = true; |
| 1113 | } |
Argiris Kirtzidis | 38f1671 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1114 | } |
Argiris Kirtzidis | 054a263 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 1115 | NewVD = VarDecl::Create(Context, DC, D.getIdentifierLoc(), |
Steve Naroff | 71cd776 | 2008-10-03 00:02:03 +0000 | [diff] [blame] | 1116 | II, R, SC, LastDeclarator, |
| 1117 | // FIXME: Move to DeclGroup... |
| 1118 | D.getDeclSpec().getSourceRange().getBegin()); |
Daniel Dunbar | 5eea562 | 2008-09-08 20:05:47 +0000 | [diff] [blame] | 1119 | NewVD->setThreadSpecified(ThreadSpecified); |
Steve Naroff | cae537d | 2007-08-28 18:45:29 +0000 | [diff] [blame] | 1120 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1121 | // Handle attributes prior to checking for duplicates in MergeVarDecl |
Chris Lattner | 9b384ca | 2008-06-29 00:02:00 +0000 | [diff] [blame] | 1122 | ProcessDeclAttributes(NewVD, D); |
Nate Begeman | ea58326 | 2008-03-14 18:07:10 +0000 | [diff] [blame] | 1123 | |
Daniel Dunbar | ced8914 | 2008-08-06 00:03:29 +0000 | [diff] [blame] | 1124 | // Handle GNU asm-label extension (encoded as an attribute). |
| 1125 | if (Expr *E = (Expr*) D.getAsmLabel()) { |
| 1126 | // The parser guarantees this is a string. |
| 1127 | StringLiteral *SE = cast<StringLiteral>(E); |
| 1128 | NewVD->addAttr(new AsmLabelAttr(std::string(SE->getStrData(), |
| 1129 | SE->getByteLength()))); |
| 1130 | } |
| 1131 | |
Nate Begeman | ea58326 | 2008-03-14 18:07:10 +0000 | [diff] [blame] | 1132 | // Emit an error if an address space was applied to decl with local storage. |
| 1133 | // This includes arrays of objects with address space qualifiers, but not |
| 1134 | // automatic variables that point to other address spaces. |
| 1135 | // ISO/IEC TR 18037 S5.1.2 |
Nate Begeman | efc1121 | 2008-03-25 18:36:32 +0000 | [diff] [blame] | 1136 | if (NewVD->hasLocalStorage() && (NewVD->getType().getAddressSpace() != 0)) { |
| 1137 | Diag(D.getIdentifierLoc(), diag::err_as_qualified_auto_decl); |
| 1138 | InvalidDecl = true; |
Nate Begeman | 0606819 | 2008-03-14 00:22:18 +0000 | [diff] [blame] | 1139 | } |
Steve Naroff | f8a0943 | 2008-01-09 23:34:55 +0000 | [diff] [blame] | 1140 | // Merge the decl with the existing one if appropriate. If the decl is |
| 1141 | // in an outer scope, it isn't the same thing. |
Argiris Kirtzidis | 054a263 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 1142 | if (PrevDecl && isDeclInScope(PrevDecl, DC, S)) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1143 | NewVD = MergeVarDecl(NewVD, PrevDecl); |
| 1144 | if (NewVD == 0) return 0; |
| 1145 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1146 | New = NewVD; |
| 1147 | } |
| 1148 | |
Argiris Kirtzidis | 881964b | 2008-11-09 23:41:00 +0000 | [diff] [blame^] | 1149 | // Set the lexical context. If the declarator has a C++ scope specifier, the |
| 1150 | // lexical context will be different from the semantic context. |
| 1151 | New->setLexicalDeclContext(CurContext); |
| 1152 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1153 | // If this has an identifier, add it to the scope stack. |
Argiris Kirtzidis | 951f25b | 2008-04-12 00:47:19 +0000 | [diff] [blame] | 1154 | if (II) |
| 1155 | PushOnScopeChains(New, S); |
Steve Naroff | d1ad6ae | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 1156 | // If any semantic error occurred, mark the decl as invalid. |
| 1157 | if (D.getInvalidType() || InvalidDecl) |
| 1158 | New->setInvalidDecl(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1159 | |
| 1160 | return New; |
| 1161 | } |
| 1162 | |
Steve Naroff | fc08f5e | 2008-10-27 11:34:16 +0000 | [diff] [blame] | 1163 | void Sema::InitializerElementNotConstant(const Expr *Init) { |
| 1164 | Diag(Init->getExprLoc(), |
| 1165 | diag::err_init_element_not_constant, Init->getSourceRange()); |
| 1166 | } |
| 1167 | |
Eli Friedman | 02c22ce | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1168 | bool Sema::CheckAddressConstantExpressionLValue(const Expr* Init) { |
| 1169 | switch (Init->getStmtClass()) { |
| 1170 | default: |
Steve Naroff | fc08f5e | 2008-10-27 11:34:16 +0000 | [diff] [blame] | 1171 | InitializerElementNotConstant(Init); |
Eli Friedman | 02c22ce | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1172 | return true; |
| 1173 | case Expr::ParenExprClass: { |
| 1174 | const ParenExpr* PE = cast<ParenExpr>(Init); |
| 1175 | return CheckAddressConstantExpressionLValue(PE->getSubExpr()); |
| 1176 | } |
| 1177 | case Expr::CompoundLiteralExprClass: |
| 1178 | return cast<CompoundLiteralExpr>(Init)->isFileScope(); |
| 1179 | case Expr::DeclRefExprClass: { |
| 1180 | const Decl *D = cast<DeclRefExpr>(Init)->getDecl(); |
Eli Friedman | 8cb86e3 | 2008-05-21 03:39:11 +0000 | [diff] [blame] | 1181 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 1182 | if (VD->hasGlobalStorage()) |
| 1183 | return false; |
Steve Naroff | fc08f5e | 2008-10-27 11:34:16 +0000 | [diff] [blame] | 1184 | InitializerElementNotConstant(Init); |
Eli Friedman | 8cb86e3 | 2008-05-21 03:39:11 +0000 | [diff] [blame] | 1185 | return true; |
| 1186 | } |
Eli Friedman | 02c22ce | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1187 | if (isa<FunctionDecl>(D)) |
| 1188 | return false; |
Steve Naroff | fc08f5e | 2008-10-27 11:34:16 +0000 | [diff] [blame] | 1189 | InitializerElementNotConstant(Init); |
Steve Naroff | f0b2354 | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 1190 | return true; |
| 1191 | } |
Eli Friedman | 02c22ce | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1192 | case Expr::MemberExprClass: { |
| 1193 | const MemberExpr *M = cast<MemberExpr>(Init); |
| 1194 | if (M->isArrow()) |
| 1195 | return CheckAddressConstantExpression(M->getBase()); |
| 1196 | return CheckAddressConstantExpressionLValue(M->getBase()); |
| 1197 | } |
| 1198 | case Expr::ArraySubscriptExprClass: { |
| 1199 | // FIXME: Should we pedwarn for "x[0+0]" (where x is a pointer)? |
| 1200 | const ArraySubscriptExpr *ASE = cast<ArraySubscriptExpr>(Init); |
| 1201 | return CheckAddressConstantExpression(ASE->getBase()) || |
| 1202 | CheckArithmeticConstantExpression(ASE->getIdx()); |
| 1203 | } |
| 1204 | case Expr::StringLiteralClass: |
Chris Lattner | 6990929 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 1205 | case Expr::PredefinedExprClass: |
Eli Friedman | 02c22ce | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1206 | return false; |
| 1207 | case Expr::UnaryOperatorClass: { |
| 1208 | const UnaryOperator *Exp = cast<UnaryOperator>(Init); |
| 1209 | |
| 1210 | // C99 6.6p9 |
| 1211 | if (Exp->getOpcode() == UnaryOperator::Deref) |
Eli Friedman | 8cb86e3 | 2008-05-21 03:39:11 +0000 | [diff] [blame] | 1212 | return CheckAddressConstantExpression(Exp->getSubExpr()); |
Eli Friedman | 02c22ce | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1213 | |
Steve Naroff | fc08f5e | 2008-10-27 11:34:16 +0000 | [diff] [blame] | 1214 | InitializerElementNotConstant(Init); |
Eli Friedman | 02c22ce | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1215 | return true; |
| 1216 | } |
| 1217 | } |
| 1218 | } |
| 1219 | |
| 1220 | bool Sema::CheckAddressConstantExpression(const Expr* Init) { |
| 1221 | switch (Init->getStmtClass()) { |
| 1222 | default: |
Steve Naroff | fc08f5e | 2008-10-27 11:34:16 +0000 | [diff] [blame] | 1223 | InitializerElementNotConstant(Init); |
Eli Friedman | 02c22ce | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1224 | return true; |
Chris Lattner | 0903cba | 2008-10-06 07:26:43 +0000 | [diff] [blame] | 1225 | case Expr::ParenExprClass: |
| 1226 | return CheckAddressConstantExpression(cast<ParenExpr>(Init)->getSubExpr()); |
Eli Friedman | 02c22ce | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1227 | case Expr::StringLiteralClass: |
| 1228 | case Expr::ObjCStringLiteralClass: |
| 1229 | return false; |
Chris Lattner | 0903cba | 2008-10-06 07:26:43 +0000 | [diff] [blame] | 1230 | case Expr::CallExprClass: |
| 1231 | // __builtin___CFStringMakeConstantString is a valid constant l-value. |
| 1232 | if (cast<CallExpr>(Init)->isBuiltinCall() == |
| 1233 | Builtin::BI__builtin___CFStringMakeConstantString) |
| 1234 | return false; |
| 1235 | |
Steve Naroff | fc08f5e | 2008-10-27 11:34:16 +0000 | [diff] [blame] | 1236 | InitializerElementNotConstant(Init); |
Chris Lattner | 0903cba | 2008-10-06 07:26:43 +0000 | [diff] [blame] | 1237 | return true; |
| 1238 | |
Eli Friedman | 02c22ce | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1239 | case Expr::UnaryOperatorClass: { |
| 1240 | const UnaryOperator *Exp = cast<UnaryOperator>(Init); |
| 1241 | |
| 1242 | // C99 6.6p9 |
| 1243 | if (Exp->getOpcode() == UnaryOperator::AddrOf) |
| 1244 | return CheckAddressConstantExpressionLValue(Exp->getSubExpr()); |
| 1245 | |
| 1246 | if (Exp->getOpcode() == UnaryOperator::Extension) |
| 1247 | return CheckAddressConstantExpression(Exp->getSubExpr()); |
| 1248 | |
Steve Naroff | fc08f5e | 2008-10-27 11:34:16 +0000 | [diff] [blame] | 1249 | InitializerElementNotConstant(Init); |
Eli Friedman | 02c22ce | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1250 | return true; |
| 1251 | } |
| 1252 | case Expr::BinaryOperatorClass: { |
| 1253 | // FIXME: Should we pedwarn for expressions like "a + 1 + 2"? |
| 1254 | const BinaryOperator *Exp = cast<BinaryOperator>(Init); |
| 1255 | |
| 1256 | Expr *PExp = Exp->getLHS(); |
| 1257 | Expr *IExp = Exp->getRHS(); |
| 1258 | if (IExp->getType()->isPointerType()) |
| 1259 | std::swap(PExp, IExp); |
| 1260 | |
| 1261 | // FIXME: Should we pedwarn if IExp isn't an integer constant expression? |
| 1262 | return CheckAddressConstantExpression(PExp) || |
| 1263 | CheckArithmeticConstantExpression(IExp); |
| 1264 | } |
Eli Friedman | 1fad3c6 | 2008-08-25 20:46:57 +0000 | [diff] [blame] | 1265 | case Expr::ImplicitCastExprClass: |
Douglas Gregor | 035d088 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 1266 | case Expr::CStyleCastExprClass: { |
Eli Friedman | 02c22ce | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1267 | const Expr* SubExpr = cast<CastExpr>(Init)->getSubExpr(); |
Eli Friedman | 1fad3c6 | 2008-08-25 20:46:57 +0000 | [diff] [blame] | 1268 | if (Init->getStmtClass() == Expr::ImplicitCastExprClass) { |
| 1269 | // Check for implicit promotion |
| 1270 | if (SubExpr->getType()->isFunctionType() || |
| 1271 | SubExpr->getType()->isArrayType()) |
| 1272 | return CheckAddressConstantExpressionLValue(SubExpr); |
| 1273 | } |
Eli Friedman | 02c22ce | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1274 | |
| 1275 | // Check for pointer->pointer cast |
| 1276 | if (SubExpr->getType()->isPointerType()) |
| 1277 | return CheckAddressConstantExpression(SubExpr); |
| 1278 | |
Eli Friedman | 1fad3c6 | 2008-08-25 20:46:57 +0000 | [diff] [blame] | 1279 | if (SubExpr->getType()->isIntegralType()) { |
| 1280 | // Check for the special-case of a pointer->int->pointer cast; |
| 1281 | // this isn't standard, but some code requires it. See |
| 1282 | // PR2720 for an example. |
| 1283 | if (const CastExpr* SubCast = dyn_cast<CastExpr>(SubExpr)) { |
| 1284 | if (SubCast->getSubExpr()->getType()->isPointerType()) { |
| 1285 | unsigned IntWidth = Context.getIntWidth(SubCast->getType()); |
| 1286 | unsigned PointerWidth = Context.getTypeSize(Context.VoidPtrTy); |
| 1287 | if (IntWidth >= PointerWidth) { |
| 1288 | return CheckAddressConstantExpression(SubCast->getSubExpr()); |
| 1289 | } |
| 1290 | } |
| 1291 | } |
| 1292 | } |
| 1293 | if (SubExpr->getType()->isArithmeticType()) { |
Eli Friedman | 02c22ce | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1294 | return CheckArithmeticConstantExpression(SubExpr); |
Eli Friedman | 1fad3c6 | 2008-08-25 20:46:57 +0000 | [diff] [blame] | 1295 | } |
Eli Friedman | 02c22ce | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1296 | |
Steve Naroff | fc08f5e | 2008-10-27 11:34:16 +0000 | [diff] [blame] | 1297 | InitializerElementNotConstant(Init); |
Eli Friedman | 02c22ce | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1298 | return true; |
| 1299 | } |
| 1300 | case Expr::ConditionalOperatorClass: { |
| 1301 | // FIXME: Should we pedwarn here? |
| 1302 | const ConditionalOperator *Exp = cast<ConditionalOperator>(Init); |
| 1303 | if (!Exp->getCond()->getType()->isArithmeticType()) { |
Steve Naroff | fc08f5e | 2008-10-27 11:34:16 +0000 | [diff] [blame] | 1304 | InitializerElementNotConstant(Init); |
Eli Friedman | 02c22ce | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1305 | return true; |
| 1306 | } |
| 1307 | if (CheckArithmeticConstantExpression(Exp->getCond())) |
| 1308 | return true; |
| 1309 | if (Exp->getLHS() && |
| 1310 | CheckAddressConstantExpression(Exp->getLHS())) |
| 1311 | return true; |
| 1312 | return CheckAddressConstantExpression(Exp->getRHS()); |
| 1313 | } |
| 1314 | case Expr::AddrLabelExprClass: |
| 1315 | return false; |
| 1316 | } |
| 1317 | } |
| 1318 | |
Eli Friedman | 998dffb | 2008-06-09 05:05:07 +0000 | [diff] [blame] | 1319 | static const Expr* FindExpressionBaseAddress(const Expr* E); |
| 1320 | |
| 1321 | static const Expr* FindExpressionBaseAddressLValue(const Expr* E) { |
| 1322 | switch (E->getStmtClass()) { |
| 1323 | default: |
| 1324 | return E; |
| 1325 | case Expr::ParenExprClass: { |
| 1326 | const ParenExpr* PE = cast<ParenExpr>(E); |
| 1327 | return FindExpressionBaseAddressLValue(PE->getSubExpr()); |
| 1328 | } |
| 1329 | case Expr::MemberExprClass: { |
| 1330 | const MemberExpr *M = cast<MemberExpr>(E); |
| 1331 | if (M->isArrow()) |
| 1332 | return FindExpressionBaseAddress(M->getBase()); |
| 1333 | return FindExpressionBaseAddressLValue(M->getBase()); |
| 1334 | } |
| 1335 | case Expr::ArraySubscriptExprClass: { |
| 1336 | const ArraySubscriptExpr *ASE = cast<ArraySubscriptExpr>(E); |
| 1337 | return FindExpressionBaseAddress(ASE->getBase()); |
| 1338 | } |
| 1339 | case Expr::UnaryOperatorClass: { |
| 1340 | const UnaryOperator *Exp = cast<UnaryOperator>(E); |
| 1341 | |
| 1342 | if (Exp->getOpcode() == UnaryOperator::Deref) |
| 1343 | return FindExpressionBaseAddress(Exp->getSubExpr()); |
| 1344 | |
| 1345 | return E; |
| 1346 | } |
| 1347 | } |
| 1348 | } |
| 1349 | |
| 1350 | static const Expr* FindExpressionBaseAddress(const Expr* E) { |
| 1351 | switch (E->getStmtClass()) { |
| 1352 | default: |
| 1353 | return E; |
| 1354 | case Expr::ParenExprClass: { |
| 1355 | const ParenExpr* PE = cast<ParenExpr>(E); |
| 1356 | return FindExpressionBaseAddress(PE->getSubExpr()); |
| 1357 | } |
| 1358 | case Expr::UnaryOperatorClass: { |
| 1359 | const UnaryOperator *Exp = cast<UnaryOperator>(E); |
| 1360 | |
| 1361 | // C99 6.6p9 |
| 1362 | if (Exp->getOpcode() == UnaryOperator::AddrOf) |
| 1363 | return FindExpressionBaseAddressLValue(Exp->getSubExpr()); |
| 1364 | |
| 1365 | if (Exp->getOpcode() == UnaryOperator::Extension) |
| 1366 | return FindExpressionBaseAddress(Exp->getSubExpr()); |
| 1367 | |
| 1368 | return E; |
| 1369 | } |
| 1370 | case Expr::BinaryOperatorClass: { |
| 1371 | const BinaryOperator *Exp = cast<BinaryOperator>(E); |
| 1372 | |
| 1373 | Expr *PExp = Exp->getLHS(); |
| 1374 | Expr *IExp = Exp->getRHS(); |
| 1375 | if (IExp->getType()->isPointerType()) |
| 1376 | std::swap(PExp, IExp); |
| 1377 | |
| 1378 | return FindExpressionBaseAddress(PExp); |
| 1379 | } |
| 1380 | case Expr::ImplicitCastExprClass: { |
| 1381 | const Expr* SubExpr = cast<ImplicitCastExpr>(E)->getSubExpr(); |
| 1382 | |
| 1383 | // Check for implicit promotion |
| 1384 | if (SubExpr->getType()->isFunctionType() || |
| 1385 | SubExpr->getType()->isArrayType()) |
| 1386 | return FindExpressionBaseAddressLValue(SubExpr); |
| 1387 | |
| 1388 | // Check for pointer->pointer cast |
| 1389 | if (SubExpr->getType()->isPointerType()) |
| 1390 | return FindExpressionBaseAddress(SubExpr); |
| 1391 | |
| 1392 | // We assume that we have an arithmetic expression here; |
| 1393 | // if we don't, we'll figure it out later |
| 1394 | return 0; |
| 1395 | } |
Douglas Gregor | 035d088 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 1396 | case Expr::CStyleCastExprClass: { |
Eli Friedman | 998dffb | 2008-06-09 05:05:07 +0000 | [diff] [blame] | 1397 | const Expr* SubExpr = cast<CastExpr>(E)->getSubExpr(); |
| 1398 | |
| 1399 | // Check for pointer->pointer cast |
| 1400 | if (SubExpr->getType()->isPointerType()) |
| 1401 | return FindExpressionBaseAddress(SubExpr); |
| 1402 | |
| 1403 | // We assume that we have an arithmetic expression here; |
| 1404 | // if we don't, we'll figure it out later |
| 1405 | return 0; |
| 1406 | } |
| 1407 | } |
| 1408 | } |
| 1409 | |
Eli Friedman | 02c22ce | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1410 | bool Sema::CheckArithmeticConstantExpression(const Expr* Init) { |
| 1411 | switch (Init->getStmtClass()) { |
| 1412 | default: |
Steve Naroff | fc08f5e | 2008-10-27 11:34:16 +0000 | [diff] [blame] | 1413 | InitializerElementNotConstant(Init); |
Eli Friedman | 02c22ce | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1414 | return true; |
| 1415 | case Expr::ParenExprClass: { |
| 1416 | const ParenExpr* PE = cast<ParenExpr>(Init); |
| 1417 | return CheckArithmeticConstantExpression(PE->getSubExpr()); |
| 1418 | } |
| 1419 | case Expr::FloatingLiteralClass: |
| 1420 | case Expr::IntegerLiteralClass: |
| 1421 | case Expr::CharacterLiteralClass: |
| 1422 | case Expr::ImaginaryLiteralClass: |
| 1423 | case Expr::TypesCompatibleExprClass: |
| 1424 | case Expr::CXXBoolLiteralExprClass: |
| 1425 | return false; |
| 1426 | case Expr::CallExprClass: { |
| 1427 | const CallExpr *CE = cast<CallExpr>(Init); |
Chris Lattner | 2d9a3f6 | 2008-10-06 06:49:02 +0000 | [diff] [blame] | 1428 | |
| 1429 | // Allow any constant foldable calls to builtins. |
| 1430 | if (CE->isBuiltinCall() && CE->isEvaluatable(Context)) |
Eli Friedman | 02c22ce | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1431 | return false; |
Chris Lattner | 2d9a3f6 | 2008-10-06 06:49:02 +0000 | [diff] [blame] | 1432 | |
Steve Naroff | fc08f5e | 2008-10-27 11:34:16 +0000 | [diff] [blame] | 1433 | InitializerElementNotConstant(Init); |
Eli Friedman | 02c22ce | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1434 | return true; |
| 1435 | } |
| 1436 | case Expr::DeclRefExprClass: { |
| 1437 | const Decl *D = cast<DeclRefExpr>(Init)->getDecl(); |
| 1438 | if (isa<EnumConstantDecl>(D)) |
| 1439 | return false; |
Steve Naroff | fc08f5e | 2008-10-27 11:34:16 +0000 | [diff] [blame] | 1440 | InitializerElementNotConstant(Init); |
Eli Friedman | 02c22ce | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1441 | return true; |
| 1442 | } |
| 1443 | case Expr::CompoundLiteralExprClass: |
| 1444 | // Allow "(vector type){2,4}"; normal C constraints don't allow this, |
| 1445 | // but vectors are allowed to be magic. |
| 1446 | if (Init->getType()->isVectorType()) |
| 1447 | return false; |
Steve Naroff | fc08f5e | 2008-10-27 11:34:16 +0000 | [diff] [blame] | 1448 | InitializerElementNotConstant(Init); |
Eli Friedman | 02c22ce | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1449 | return true; |
| 1450 | case Expr::UnaryOperatorClass: { |
| 1451 | const UnaryOperator *Exp = cast<UnaryOperator>(Init); |
| 1452 | |
| 1453 | switch (Exp->getOpcode()) { |
| 1454 | // Address, indirect, pre/post inc/dec, etc are not valid constant exprs. |
| 1455 | // See C99 6.6p3. |
| 1456 | default: |
Steve Naroff | fc08f5e | 2008-10-27 11:34:16 +0000 | [diff] [blame] | 1457 | InitializerElementNotConstant(Init); |
Eli Friedman | 02c22ce | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1458 | return true; |
| 1459 | case UnaryOperator::SizeOf: |
| 1460 | case UnaryOperator::AlignOf: |
| 1461 | case UnaryOperator::OffsetOf: |
| 1462 | // sizeof(E) is a constantexpr if and only if E is not evaluted. |
| 1463 | // See C99 6.5.3.4p2 and 6.6p3. |
| 1464 | if (Exp->getSubExpr()->getType()->isConstantSizeType()) |
| 1465 | return false; |
Steve Naroff | fc08f5e | 2008-10-27 11:34:16 +0000 | [diff] [blame] | 1466 | InitializerElementNotConstant(Init); |
Eli Friedman | 02c22ce | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1467 | return true; |
| 1468 | case UnaryOperator::Extension: |
| 1469 | case UnaryOperator::LNot: |
| 1470 | case UnaryOperator::Plus: |
| 1471 | case UnaryOperator::Minus: |
| 1472 | case UnaryOperator::Not: |
| 1473 | return CheckArithmeticConstantExpression(Exp->getSubExpr()); |
| 1474 | } |
| 1475 | } |
| 1476 | case Expr::SizeOfAlignOfTypeExprClass: { |
| 1477 | const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(Init); |
| 1478 | // Special check for void types, which are allowed as an extension |
| 1479 | if (Exp->getArgumentType()->isVoidType()) |
| 1480 | return false; |
| 1481 | // alignof always evaluates to a constant. |
| 1482 | // FIXME: is sizeof(int[3.0]) a constant expression? |
| 1483 | if (Exp->isSizeOf() && !Exp->getArgumentType()->isConstantSizeType()) { |
Steve Naroff | fc08f5e | 2008-10-27 11:34:16 +0000 | [diff] [blame] | 1484 | InitializerElementNotConstant(Init); |
Eli Friedman | 02c22ce | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1485 | return true; |
| 1486 | } |
| 1487 | return false; |
| 1488 | } |
| 1489 | case Expr::BinaryOperatorClass: { |
| 1490 | const BinaryOperator *Exp = cast<BinaryOperator>(Init); |
| 1491 | |
| 1492 | if (Exp->getLHS()->getType()->isArithmeticType() && |
| 1493 | Exp->getRHS()->getType()->isArithmeticType()) { |
| 1494 | return CheckArithmeticConstantExpression(Exp->getLHS()) || |
| 1495 | CheckArithmeticConstantExpression(Exp->getRHS()); |
| 1496 | } |
| 1497 | |
Eli Friedman | 998dffb | 2008-06-09 05:05:07 +0000 | [diff] [blame] | 1498 | if (Exp->getLHS()->getType()->isPointerType() && |
| 1499 | Exp->getRHS()->getType()->isPointerType()) { |
| 1500 | const Expr* LHSBase = FindExpressionBaseAddress(Exp->getLHS()); |
| 1501 | const Expr* RHSBase = FindExpressionBaseAddress(Exp->getRHS()); |
| 1502 | |
| 1503 | // Only allow a null (constant integer) base; we could |
| 1504 | // allow some additional cases if necessary, but this |
| 1505 | // is sufficient to cover offsetof-like constructs. |
| 1506 | if (!LHSBase && !RHSBase) { |
| 1507 | return CheckAddressConstantExpression(Exp->getLHS()) || |
| 1508 | CheckAddressConstantExpression(Exp->getRHS()); |
| 1509 | } |
| 1510 | } |
| 1511 | |
Steve Naroff | fc08f5e | 2008-10-27 11:34:16 +0000 | [diff] [blame] | 1512 | InitializerElementNotConstant(Init); |
Eli Friedman | 02c22ce | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1513 | return true; |
| 1514 | } |
| 1515 | case Expr::ImplicitCastExprClass: |
Douglas Gregor | 035d088 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 1516 | case Expr::CStyleCastExprClass: { |
Argiris Kirtzidis | c45e2fb | 2008-08-18 23:01:59 +0000 | [diff] [blame] | 1517 | const Expr *SubExpr = cast<CastExpr>(Init)->getSubExpr(); |
Eli Friedman | d662caa | 2008-09-01 22:08:17 +0000 | [diff] [blame] | 1518 | if (SubExpr->getType()->isArithmeticType()) |
| 1519 | return CheckArithmeticConstantExpression(SubExpr); |
| 1520 | |
Eli Friedman | 266df14 | 2008-09-02 09:37:00 +0000 | [diff] [blame] | 1521 | if (SubExpr->getType()->isPointerType()) { |
| 1522 | const Expr* Base = FindExpressionBaseAddress(SubExpr); |
| 1523 | // If the pointer has a null base, this is an offsetof-like construct |
| 1524 | if (!Base) |
| 1525 | return CheckAddressConstantExpression(SubExpr); |
| 1526 | } |
| 1527 | |
Steve Naroff | fc08f5e | 2008-10-27 11:34:16 +0000 | [diff] [blame] | 1528 | InitializerElementNotConstant(Init); |
Eli Friedman | d662caa | 2008-09-01 22:08:17 +0000 | [diff] [blame] | 1529 | return true; |
Eli Friedman | 02c22ce | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1530 | } |
| 1531 | case Expr::ConditionalOperatorClass: { |
| 1532 | const ConditionalOperator *Exp = cast<ConditionalOperator>(Init); |
Chris Lattner | 94d4541 | 2008-10-06 05:42:39 +0000 | [diff] [blame] | 1533 | |
| 1534 | // If GNU extensions are disabled, we require all operands to be arithmetic |
| 1535 | // constant expressions. |
| 1536 | if (getLangOptions().NoExtensions) { |
| 1537 | return CheckArithmeticConstantExpression(Exp->getCond()) || |
| 1538 | (Exp->getLHS() && CheckArithmeticConstantExpression(Exp->getLHS())) || |
| 1539 | CheckArithmeticConstantExpression(Exp->getRHS()); |
| 1540 | } |
| 1541 | |
| 1542 | // Otherwise, we have to emulate some of the behavior of fold here. |
| 1543 | // Basically GCC treats things like "4 ? 1 : somefunc()" as a constant |
| 1544 | // because it can constant fold things away. To retain compatibility with |
| 1545 | // GCC code, we see if we can fold the condition to a constant (which we |
| 1546 | // should always be able to do in theory). If so, we only require the |
| 1547 | // specified arm of the conditional to be a constant. This is a horrible |
| 1548 | // hack, but is require by real world code that uses __builtin_constant_p. |
| 1549 | APValue Val; |
| 1550 | if (!Exp->getCond()->tryEvaluate(Val, Context)) { |
| 1551 | // If the tryEvaluate couldn't fold it, CheckArithmeticConstantExpression |
| 1552 | // won't be able to either. Use it to emit the diagnostic though. |
| 1553 | bool Res = CheckArithmeticConstantExpression(Exp->getCond()); |
| 1554 | assert(Res && "tryEvaluate couldn't evaluate this constant?"); |
| 1555 | return Res; |
| 1556 | } |
| 1557 | |
| 1558 | // Verify that the side following the condition is also a constant. |
| 1559 | const Expr *TrueSide = Exp->getLHS(), *FalseSide = Exp->getRHS(); |
| 1560 | if (Val.getInt() == 0) |
| 1561 | std::swap(TrueSide, FalseSide); |
| 1562 | |
| 1563 | if (TrueSide && CheckArithmeticConstantExpression(TrueSide)) |
Eli Friedman | 02c22ce | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1564 | return true; |
Chris Lattner | 94d4541 | 2008-10-06 05:42:39 +0000 | [diff] [blame] | 1565 | |
| 1566 | // Okay, the evaluated side evaluates to a constant, so we accept this. |
| 1567 | // Check to see if the other side is obviously not a constant. If so, |
| 1568 | // emit a warning that this is a GNU extension. |
Chris Lattner | 2d9a3f6 | 2008-10-06 06:49:02 +0000 | [diff] [blame] | 1569 | if (FalseSide && !FalseSide->isEvaluatable(Context)) |
Chris Lattner | 94d4541 | 2008-10-06 05:42:39 +0000 | [diff] [blame] | 1570 | Diag(Init->getExprLoc(), |
| 1571 | diag::ext_typecheck_expression_not_constant_but_accepted, |
| 1572 | FalseSide->getSourceRange()); |
| 1573 | return false; |
Eli Friedman | 02c22ce | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1574 | } |
| 1575 | } |
| 1576 | } |
| 1577 | |
| 1578 | bool Sema::CheckForConstantInitializer(Expr *Init, QualType DclT) { |
Nuno Lopes | e728045 | 2008-07-07 16:46:50 +0000 | [diff] [blame] | 1579 | Init = Init->IgnoreParens(); |
| 1580 | |
Eli Friedman | 02c22ce | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1581 | // Look through CXXDefaultArgExprs; they have no meaning in this context. |
| 1582 | if (CXXDefaultArgExpr* DAE = dyn_cast<CXXDefaultArgExpr>(Init)) |
| 1583 | return CheckForConstantInitializer(DAE->getExpr(), DclT); |
| 1584 | |
Nuno Lopes | e728045 | 2008-07-07 16:46:50 +0000 | [diff] [blame] | 1585 | if (CompoundLiteralExpr *e = dyn_cast<CompoundLiteralExpr>(Init)) |
| 1586 | return CheckForConstantInitializer(e->getInitializer(), DclT); |
| 1587 | |
Eli Friedman | 02c22ce | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1588 | if (InitListExpr *Exp = dyn_cast<InitListExpr>(Init)) { |
| 1589 | unsigned numInits = Exp->getNumInits(); |
| 1590 | for (unsigned i = 0; i < numInits; i++) { |
| 1591 | // FIXME: Need to get the type of the declaration for C++, |
| 1592 | // because it could be a reference? |
| 1593 | if (CheckForConstantInitializer(Exp->getInit(i), |
| 1594 | Exp->getInit(i)->getType())) |
| 1595 | return true; |
| 1596 | } |
| 1597 | return false; |
| 1598 | } |
| 1599 | |
| 1600 | if (Init->isNullPointerConstant(Context)) |
| 1601 | return false; |
| 1602 | if (Init->getType()->isArithmeticType()) { |
Chris Lattner | d5a56aa | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 1603 | QualType InitTy = Context.getCanonicalType(Init->getType()) |
| 1604 | .getUnqualifiedType(); |
Eli Friedman | 25086f0 | 2008-05-30 18:14:48 +0000 | [diff] [blame] | 1605 | if (InitTy == Context.BoolTy) { |
| 1606 | // Special handling for pointers implicitly cast to bool; |
| 1607 | // (e.g. "_Bool rr = &rr;"). This is only legal at the top level. |
| 1608 | if (ImplicitCastExpr* ICE = dyn_cast<ImplicitCastExpr>(Init)) { |
| 1609 | Expr* SubE = ICE->getSubExpr(); |
| 1610 | if (SubE->getType()->isPointerType() || |
| 1611 | SubE->getType()->isArrayType() || |
| 1612 | SubE->getType()->isFunctionType()) { |
| 1613 | return CheckAddressConstantExpression(Init); |
| 1614 | } |
| 1615 | } |
| 1616 | } else if (InitTy->isIntegralType()) { |
| 1617 | Expr* SubE = 0; |
Argiris Kirtzidis | c45e2fb | 2008-08-18 23:01:59 +0000 | [diff] [blame] | 1618 | if (CastExpr* CE = dyn_cast<CastExpr>(Init)) |
Eli Friedman | 25086f0 | 2008-05-30 18:14:48 +0000 | [diff] [blame] | 1619 | SubE = CE->getSubExpr(); |
| 1620 | // Special check for pointer cast to int; we allow as an extension |
| 1621 | // an address constant cast to an integer if the integer |
| 1622 | // is of an appropriate width (this sort of code is apparently used |
| 1623 | // in some places). |
| 1624 | // FIXME: Add pedwarn? |
| 1625 | // FIXME: Don't allow bitfields here! Need the FieldDecl for that. |
| 1626 | if (SubE && (SubE->getType()->isPointerType() || |
| 1627 | SubE->getType()->isArrayType() || |
| 1628 | SubE->getType()->isFunctionType())) { |
| 1629 | unsigned IntWidth = Context.getTypeSize(Init->getType()); |
| 1630 | unsigned PointerWidth = Context.getTypeSize(Context.VoidPtrTy); |
| 1631 | if (IntWidth >= PointerWidth) |
| 1632 | return CheckAddressConstantExpression(Init); |
| 1633 | } |
Eli Friedman | 02c22ce | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1634 | } |
| 1635 | |
| 1636 | return CheckArithmeticConstantExpression(Init); |
| 1637 | } |
| 1638 | |
| 1639 | if (Init->getType()->isPointerType()) |
| 1640 | return CheckAddressConstantExpression(Init); |
| 1641 | |
Eli Friedman | 25086f0 | 2008-05-30 18:14:48 +0000 | [diff] [blame] | 1642 | // An array type at the top level that isn't an init-list must |
| 1643 | // be a string literal |
Eli Friedman | 02c22ce | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1644 | if (Init->getType()->isArrayType()) |
| 1645 | return false; |
| 1646 | |
Nuno Lopes | 1dc2676 | 2008-09-01 18:42:41 +0000 | [diff] [blame] | 1647 | if (Init->getType()->isFunctionType()) |
| 1648 | return false; |
| 1649 | |
Steve Naroff | dff3fb2 | 2008-10-02 17:12:56 +0000 | [diff] [blame] | 1650 | // Allow block exprs at top level. |
| 1651 | if (Init->getType()->isBlockPointerType()) |
| 1652 | return false; |
| 1653 | |
Steve Naroff | fc08f5e | 2008-10-27 11:34:16 +0000 | [diff] [blame] | 1654 | InitializerElementNotConstant(Init); |
Eli Friedman | 02c22ce | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1655 | return true; |
Steve Naroff | f0b2354 | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 1656 | } |
| 1657 | |
Steve Naroff | 6a0e209 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 1658 | void Sema::AddInitializerToDecl(DeclTy *dcl, ExprTy *init) { |
Steve Naroff | 420d0f5 | 2007-09-12 20:13:48 +0000 | [diff] [blame] | 1659 | Decl *RealDecl = static_cast<Decl *>(dcl); |
Steve Naroff | 6a0e209 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 1660 | Expr *Init = static_cast<Expr *>(init); |
Chris Lattner | f31a2fb | 2007-10-19 20:10:30 +0000 | [diff] [blame] | 1661 | assert(Init && "missing initializer"); |
Steve Naroff | 6a0e209 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 1662 | |
Chris Lattner | f31a2fb | 2007-10-19 20:10:30 +0000 | [diff] [blame] | 1663 | // If there is no declaration, there was an error parsing it. Just ignore |
| 1664 | // the initializer. |
| 1665 | if (RealDecl == 0) { |
| 1666 | delete Init; |
| 1667 | return; |
| 1668 | } |
Steve Naroff | 6a0e209 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 1669 | |
Steve Naroff | 420d0f5 | 2007-09-12 20:13:48 +0000 | [diff] [blame] | 1670 | VarDecl *VDecl = dyn_cast<VarDecl>(RealDecl); |
| 1671 | if (!VDecl) { |
Steve Naroff | cb59747 | 2007-09-13 21:41:19 +0000 | [diff] [blame] | 1672 | Diag(dyn_cast<ScopedDecl>(RealDecl)->getLocation(), |
| 1673 | diag::err_illegal_initializer); |
Steve Naroff | 420d0f5 | 2007-09-12 20:13:48 +0000 | [diff] [blame] | 1674 | RealDecl->setInvalidDecl(); |
| 1675 | return; |
| 1676 | } |
Steve Naroff | 6a0e209 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 1677 | // Get the decls type and save a reference for later, since |
Steve Naroff | f0b2354 | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 1678 | // CheckInitializerTypes may change it. |
Steve Naroff | 420d0f5 | 2007-09-12 20:13:48 +0000 | [diff] [blame] | 1679 | QualType DclT = VDecl->getType(), SavT = DclT; |
Steve Naroff | 72a6ebc | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 1680 | if (VDecl->isBlockVarDecl()) { |
| 1681 | VarDecl::StorageClass SC = VDecl->getStorageClass(); |
Steve Naroff | 6a0e209 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 1682 | if (SC == VarDecl::Extern) { // C99 6.7.8p5 |
Steve Naroff | 420d0f5 | 2007-09-12 20:13:48 +0000 | [diff] [blame] | 1683 | Diag(VDecl->getLocation(), diag::err_block_extern_cant_init); |
Steve Naroff | 72a6ebc | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 1684 | VDecl->setInvalidDecl(); |
| 1685 | } else if (!VDecl->isInvalidDecl()) { |
Douglas Gregor | 6428e76 | 2008-11-05 15:29:30 +0000 | [diff] [blame] | 1686 | if (CheckInitializerTypes(Init, DclT, VDecl->getLocation(), |
| 1687 | VDecl->getName())) |
Steve Naroff | 72a6ebc | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 1688 | VDecl->setInvalidDecl(); |
Anders Carlsson | ea7140a | 2008-08-22 05:00:02 +0000 | [diff] [blame] | 1689 | |
| 1690 | // C++ 3.6.2p2, allow dynamic initialization of static initializers. |
| 1691 | if (!getLangOptions().CPlusPlus) { |
| 1692 | if (SC == VarDecl::Static) // C99 6.7.8p4. |
| 1693 | CheckForConstantInitializer(Init, DclT); |
| 1694 | } |
Steve Naroff | 6a0e209 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 1695 | } |
Steve Naroff | 72a6ebc | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 1696 | } else if (VDecl->isFileVarDecl()) { |
| 1697 | if (VDecl->getStorageClass() == VarDecl::Extern) |
Steve Naroff | 420d0f5 | 2007-09-12 20:13:48 +0000 | [diff] [blame] | 1698 | Diag(VDecl->getLocation(), diag::warn_extern_init); |
Steve Naroff | 72a6ebc | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 1699 | if (!VDecl->isInvalidDecl()) |
Douglas Gregor | 6428e76 | 2008-11-05 15:29:30 +0000 | [diff] [blame] | 1700 | if (CheckInitializerTypes(Init, DclT, VDecl->getLocation(), |
| 1701 | VDecl->getName())) |
Steve Naroff | 72a6ebc | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 1702 | VDecl->setInvalidDecl(); |
Steve Naroff | f0b2354 | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 1703 | |
Anders Carlsson | ea7140a | 2008-08-22 05:00:02 +0000 | [diff] [blame] | 1704 | // C++ 3.6.2p2, allow dynamic initialization of static initializers. |
| 1705 | if (!getLangOptions().CPlusPlus) { |
| 1706 | // C99 6.7.8p4. All file scoped initializers need to be constant. |
| 1707 | CheckForConstantInitializer(Init, DclT); |
| 1708 | } |
Steve Naroff | 6a0e209 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 1709 | } |
| 1710 | // If the type changed, it means we had an incomplete type that was |
| 1711 | // completed by the initializer. For example: |
| 1712 | // int ary[] = { 1, 3, 5 }; |
| 1713 | // "ary" transitions from a VariableArrayType to a ConstantArrayType. |
Christopher Lamb | 62f06b6 | 2007-11-29 19:09:19 +0000 | [diff] [blame] | 1714 | if (!VDecl->isInvalidDecl() && (DclT != SavT)) { |
Steve Naroff | 420d0f5 | 2007-09-12 20:13:48 +0000 | [diff] [blame] | 1715 | VDecl->setType(DclT); |
Christopher Lamb | 62f06b6 | 2007-11-29 19:09:19 +0000 | [diff] [blame] | 1716 | Init->setType(DclT); |
| 1717 | } |
Steve Naroff | 6a0e209 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 1718 | |
| 1719 | // Attach the initializer to the decl. |
Steve Naroff | 420d0f5 | 2007-09-12 20:13:48 +0000 | [diff] [blame] | 1720 | VDecl->setInit(Init); |
Steve Naroff | 6a0e209 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 1721 | return; |
| 1722 | } |
| 1723 | |
Douglas Gregor | 81c2915 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1724 | void Sema::ActOnUninitializedDecl(DeclTy *dcl) { |
| 1725 | Decl *RealDecl = static_cast<Decl *>(dcl); |
| 1726 | |
Argiris Kirtzidis | 9c0e994 | 2008-11-07 13:01:22 +0000 | [diff] [blame] | 1727 | // If there is no declaration, there was an error parsing it. Just ignore it. |
| 1728 | if (RealDecl == 0) |
| 1729 | return; |
| 1730 | |
Douglas Gregor | 81c2915 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1731 | if (VarDecl *Var = dyn_cast<VarDecl>(RealDecl)) { |
| 1732 | QualType Type = Var->getType(); |
| 1733 | // C++ [dcl.init.ref]p3: |
| 1734 | // The initializer can be omitted for a reference only in a |
| 1735 | // parameter declaration (8.3.5), in the declaration of a |
| 1736 | // function return type, in the declaration of a class member |
| 1737 | // within its class declaration (9.2), and where the extern |
| 1738 | // specifier is explicitly used. |
Douglas Gregor | 5870a95 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 1739 | if (Type->isReferenceType() && Var->getStorageClass() != VarDecl::Extern) { |
Douglas Gregor | 81c2915 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1740 | Diag(Var->getLocation(), |
| 1741 | diag::err_reference_var_requires_init, |
| 1742 | Var->getName(), |
| 1743 | SourceRange(Var->getLocation(), Var->getLocation())); |
Douglas Gregor | 5870a95 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 1744 | Var->setInvalidDecl(); |
| 1745 | return; |
| 1746 | } |
| 1747 | |
| 1748 | // C++ [dcl.init]p9: |
| 1749 | // |
| 1750 | // If no initializer is specified for an object, and the object |
| 1751 | // is of (possibly cv-qualified) non-POD class type (or array |
| 1752 | // thereof), the object shall be default-initialized; if the |
| 1753 | // object is of const-qualified type, the underlying class type |
| 1754 | // shall have a user-declared default constructor. |
| 1755 | if (getLangOptions().CPlusPlus) { |
| 1756 | QualType InitType = Type; |
| 1757 | if (const ArrayType *Array = Context.getAsArrayType(Type)) |
| 1758 | InitType = Array->getElementType(); |
| 1759 | if (InitType->isRecordType()) { |
Douglas Gregor | 6428e76 | 2008-11-05 15:29:30 +0000 | [diff] [blame] | 1760 | const CXXConstructorDecl *Constructor |
| 1761 | = PerformInitializationByConstructor(InitType, 0, 0, |
| 1762 | Var->getLocation(), |
| 1763 | SourceRange(Var->getLocation(), |
| 1764 | Var->getLocation()), |
| 1765 | Var->getName(), |
| 1766 | IK_Default); |
Douglas Gregor | 5870a95 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 1767 | if (!Constructor) |
| 1768 | Var->setInvalidDecl(); |
| 1769 | } |
| 1770 | } |
Douglas Gregor | 81c2915 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1771 | |
Douglas Gregor | c0d11a8 | 2008-10-29 13:50:18 +0000 | [diff] [blame] | 1772 | #if 0 |
| 1773 | // FIXME: Temporarily disabled because we are not properly parsing |
| 1774 | // linkage specifications on declarations, e.g., |
| 1775 | // |
| 1776 | // extern "C" const CGPoint CGPointerZero; |
| 1777 | // |
Douglas Gregor | 81c2915 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1778 | // C++ [dcl.init]p9: |
| 1779 | // |
| 1780 | // If no initializer is specified for an object, and the |
| 1781 | // object is of (possibly cv-qualified) non-POD class type (or |
| 1782 | // array thereof), the object shall be default-initialized; if |
| 1783 | // the object is of const-qualified type, the underlying class |
| 1784 | // type shall have a user-declared default |
| 1785 | // constructor. Otherwise, if no initializer is specified for |
| 1786 | // an object, the object and its subobjects, if any, have an |
| 1787 | // indeterminate initial value; if the object or any of its |
| 1788 | // subobjects are of const-qualified type, the program is |
| 1789 | // ill-formed. |
| 1790 | // |
| 1791 | // This isn't technically an error in C, so we don't diagnose it. |
| 1792 | // |
| 1793 | // FIXME: Actually perform the POD/user-defined default |
| 1794 | // constructor check. |
| 1795 | if (getLangOptions().CPlusPlus && |
Douglas Gregor | c0d11a8 | 2008-10-29 13:50:18 +0000 | [diff] [blame] | 1796 | Context.getCanonicalType(Type).isConstQualified() && |
| 1797 | Var->getStorageClass() != VarDecl::Extern) |
Douglas Gregor | 81c2915 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1798 | Diag(Var->getLocation(), |
| 1799 | diag::err_const_var_requires_init, |
| 1800 | Var->getName(), |
| 1801 | SourceRange(Var->getLocation(), Var->getLocation())); |
Douglas Gregor | c0d11a8 | 2008-10-29 13:50:18 +0000 | [diff] [blame] | 1802 | #endif |
Douglas Gregor | 81c2915 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1803 | } |
| 1804 | } |
| 1805 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1806 | /// The declarators are chained together backwards, reverse the list. |
| 1807 | Sema::DeclTy *Sema::FinalizeDeclaratorGroup(Scope *S, DeclTy *group) { |
| 1808 | // Often we have single declarators, handle them quickly. |
Steve Naroff | 2591e1b | 2007-09-13 23:52:58 +0000 | [diff] [blame] | 1809 | Decl *GroupDecl = static_cast<Decl*>(group); |
| 1810 | if (GroupDecl == 0) |
Steve Naroff | 6a0e209 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 1811 | return 0; |
Steve Naroff | 2591e1b | 2007-09-13 23:52:58 +0000 | [diff] [blame] | 1812 | |
| 1813 | ScopedDecl *Group = dyn_cast<ScopedDecl>(GroupDecl); |
| 1814 | ScopedDecl *NewGroup = 0; |
Steve Naroff | 6a0e209 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 1815 | if (Group->getNextDeclarator() == 0) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1816 | NewGroup = Group; |
Steve Naroff | 6a0e209 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 1817 | else { // reverse the list. |
| 1818 | while (Group) { |
Steve Naroff | 2591e1b | 2007-09-13 23:52:58 +0000 | [diff] [blame] | 1819 | ScopedDecl *Next = Group->getNextDeclarator(); |
Steve Naroff | 6a0e209 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 1820 | Group->setNextDeclarator(NewGroup); |
| 1821 | NewGroup = Group; |
| 1822 | Group = Next; |
| 1823 | } |
| 1824 | } |
| 1825 | // Perform semantic analysis that depends on having fully processed both |
| 1826 | // the declarator and initializer. |
Steve Naroff | 2591e1b | 2007-09-13 23:52:58 +0000 | [diff] [blame] | 1827 | for (ScopedDecl *ID = NewGroup; ID; ID = ID->getNextDeclarator()) { |
Steve Naroff | 6a0e209 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 1828 | VarDecl *IDecl = dyn_cast<VarDecl>(ID); |
| 1829 | if (!IDecl) |
| 1830 | continue; |
Steve Naroff | 6a0e209 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 1831 | QualType T = IDecl->getType(); |
| 1832 | |
| 1833 | // C99 6.7.5.2p2: If an identifier is declared to be an object with |
| 1834 | // static storage duration, it shall not have a variable length array. |
Steve Naroff | 72a6ebc | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 1835 | if ((IDecl->isFileVarDecl() || IDecl->isBlockVarDecl()) && |
| 1836 | IDecl->getStorageClass() == VarDecl::Static) { |
Chris Lattner | a1923f6 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 1837 | if (T->isVariableArrayType()) { |
Eli Friedman | 8ff0778 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1838 | Diag(IDecl->getLocation(), diag::err_typecheck_illegal_vla); |
| 1839 | IDecl->setInvalidDecl(); |
Steve Naroff | 6a0e209 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 1840 | } |
| 1841 | } |
| 1842 | // Block scope. C99 6.7p7: If an identifier for an object is declared with |
| 1843 | // no linkage (C99 6.2.2p6), the type for the object shall be complete... |
Steve Naroff | 72a6ebc | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 1844 | if (IDecl->isBlockVarDecl() && |
| 1845 | IDecl->getStorageClass() != VarDecl::Extern) { |
Chris Lattner | 67d3c8d | 2008-04-02 01:05:10 +0000 | [diff] [blame] | 1846 | if (T->isIncompleteType() && !IDecl->isInvalidDecl()) { |
Chris Lattner | 2f72aa0 | 2007-12-02 07:50:03 +0000 | [diff] [blame] | 1847 | Diag(IDecl->getLocation(), diag::err_typecheck_decl_incomplete_type, |
| 1848 | T.getAsString()); |
Steve Naroff | 6a0e209 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 1849 | IDecl->setInvalidDecl(); |
| 1850 | } |
| 1851 | } |
| 1852 | // File scope. C99 6.9.2p2: A declaration of an identifier for and |
| 1853 | // object that has file scope without an initializer, and without a |
| 1854 | // storage-class specifier or with the storage-class specifier "static", |
| 1855 | // constitutes a tentative definition. Note: A tentative definition with |
| 1856 | // external linkage is valid (C99 6.2.2p5). |
Steve Naroff | b5e7815 | 2008-08-08 17:50:35 +0000 | [diff] [blame] | 1857 | if (isTentativeDefinition(IDecl)) { |
Eli Friedman | e007979 | 2008-02-15 12:53:51 +0000 | [diff] [blame] | 1858 | if (T->isIncompleteArrayType()) { |
Steve Naroff | 6068546 | 2008-01-18 20:40:52 +0000 | [diff] [blame] | 1859 | // C99 6.9.2 (p2, p5): Implicit initialization causes an incomplete |
| 1860 | // array to be completed. Don't issue a diagnostic. |
Chris Lattner | 67d3c8d | 2008-04-02 01:05:10 +0000 | [diff] [blame] | 1861 | } else if (T->isIncompleteType() && !IDecl->isInvalidDecl()) { |
Steve Naroff | 6068546 | 2008-01-18 20:40:52 +0000 | [diff] [blame] | 1862 | // C99 6.9.2p3: If the declaration of an identifier for an object is |
| 1863 | // a tentative definition and has internal linkage (C99 6.2.2p3), the |
| 1864 | // declared type shall not be an incomplete type. |
Chris Lattner | 2f72aa0 | 2007-12-02 07:50:03 +0000 | [diff] [blame] | 1865 | Diag(IDecl->getLocation(), diag::err_typecheck_decl_incomplete_type, |
| 1866 | T.getAsString()); |
Steve Naroff | 6a0e209 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 1867 | IDecl->setInvalidDecl(); |
| 1868 | } |
| 1869 | } |
Steve Naroff | b5e7815 | 2008-08-08 17:50:35 +0000 | [diff] [blame] | 1870 | if (IDecl->isFileVarDecl()) |
| 1871 | CheckForFileScopedRedefinitions(S, IDecl); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1872 | } |
| 1873 | return NewGroup; |
| 1874 | } |
Steve Naroff | 91b03f7 | 2007-08-28 03:03:08 +0000 | [diff] [blame] | 1875 | |
Chris Lattner | 3e254fb | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 1876 | /// ActOnParamDeclarator - Called from Parser::ParseFunctionDeclarator() |
| 1877 | /// to introduce parameters into function prototype scope. |
| 1878 | Sema::DeclTy * |
| 1879 | Sema::ActOnParamDeclarator(Scope *S, Declarator &D) { |
Argiris Kirtzidis | 054a263 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 1880 | // FIXME: disallow CXXScopeSpec for param declarators. |
Chris Lattner | 5e77ade | 2008-06-26 06:49:43 +0000 | [diff] [blame] | 1881 | const DeclSpec &DS = D.getDeclSpec(); |
Chris Lattner | 3e254fb | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 1882 | |
| 1883 | // Verify C99 6.7.5.3p2: The only SCS allowed is 'register'. |
Daniel Dunbar | b648e8c | 2008-09-03 21:54:21 +0000 | [diff] [blame] | 1884 | VarDecl::StorageClass StorageClass = VarDecl::None; |
| 1885 | if (DS.getStorageClassSpec() == DeclSpec::SCS_register) { |
| 1886 | StorageClass = VarDecl::Register; |
| 1887 | } else if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified) { |
Chris Lattner | 3e254fb | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 1888 | Diag(DS.getStorageClassSpecLoc(), |
| 1889 | diag::err_invalid_storage_class_in_func_decl); |
Chris Lattner | 5e77ade | 2008-06-26 06:49:43 +0000 | [diff] [blame] | 1890 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
Chris Lattner | 3e254fb | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 1891 | } |
| 1892 | if (DS.isThreadSpecified()) { |
| 1893 | Diag(DS.getThreadSpecLoc(), |
| 1894 | diag::err_invalid_storage_class_in_func_decl); |
Chris Lattner | 5e77ade | 2008-06-26 06:49:43 +0000 | [diff] [blame] | 1895 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
Chris Lattner | 3e254fb | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 1896 | } |
| 1897 | |
Douglas Gregor | 2b9422f | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 1898 | // Check that there are no default arguments inside the type of this |
| 1899 | // parameter (C++ only). |
| 1900 | if (getLangOptions().CPlusPlus) |
| 1901 | CheckExtraCXXDefaultArguments(D); |
| 1902 | |
Chris Lattner | 3e254fb | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 1903 | // In this context, we *do not* check D.getInvalidType(). If the declarator |
| 1904 | // type was invalid, GetTypeForDeclarator() still returns a "valid" type, |
| 1905 | // though it will not reflect the user specified type. |
| 1906 | QualType parmDeclType = GetTypeForDeclarator(D, S); |
| 1907 | |
| 1908 | assert(!parmDeclType.isNull() && "GetTypeForDeclarator() returned null type"); |
| 1909 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1910 | // TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope. |
| 1911 | // Can this happen for params? We already checked that they don't conflict |
| 1912 | // among each other. Here they can only shadow globals, which is ok. |
Chris Lattner | 3e254fb | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 1913 | IdentifierInfo *II = D.getIdentifier(); |
| 1914 | if (Decl *PrevDecl = LookupDecl(II, Decl::IDNS_Ordinary, S)) { |
| 1915 | if (S->isDeclScope(PrevDecl)) { |
| 1916 | Diag(D.getIdentifierLoc(), diag::err_param_redefinition, |
| 1917 | dyn_cast<NamedDecl>(PrevDecl)->getName()); |
| 1918 | |
| 1919 | // Recover by removing the name |
| 1920 | II = 0; |
| 1921 | D.SetIdentifier(0, D.getIdentifierLoc()); |
| 1922 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1923 | } |
Steve Naroff | 94cd93f | 2007-08-07 22:44:21 +0000 | [diff] [blame] | 1924 | |
| 1925 | // Perform the default function/array conversion (C99 6.7.5.3p[7,8]). |
| 1926 | // Doing the promotion here has a win and a loss. The win is the type for |
| 1927 | // both Decl's and DeclRefExpr's will match (a convenient invariant for the |
| 1928 | // code generator). The loss is the orginal type isn't preserved. For example: |
| 1929 | // |
| 1930 | // void func(int parmvardecl[5]) { // convert "int [5]" to "int *" |
| 1931 | // int blockvardecl[5]; |
| 1932 | // sizeof(parmvardecl); // size == 4 |
| 1933 | // sizeof(blockvardecl); // size == 20 |
| 1934 | // } |
| 1935 | // |
| 1936 | // For expressions, all implicit conversions are captured using the |
| 1937 | // ImplicitCastExpr AST node (we have no such mechanism for Decl's). |
| 1938 | // |
| 1939 | // FIXME: If a source translation tool needs to see the original type, then |
| 1940 | // we need to consider storing both types (in ParmVarDecl)... |
| 1941 | // |
Chris Lattner | 19eb97e | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 1942 | if (parmDeclType->isArrayType()) { |
Chris Lattner | c08564a | 2008-01-02 22:50:48 +0000 | [diff] [blame] | 1943 | // int x[restrict 4] -> int *restrict |
Chris Lattner | 19eb97e | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 1944 | parmDeclType = Context.getArrayDecayedType(parmDeclType); |
Chris Lattner | c08564a | 2008-01-02 22:50:48 +0000 | [diff] [blame] | 1945 | } else if (parmDeclType->isFunctionType()) |
Steve Naroff | 94cd93f | 2007-08-07 22:44:21 +0000 | [diff] [blame] | 1946 | parmDeclType = Context.getPointerType(parmDeclType); |
| 1947 | |
Chris Lattner | 3e254fb | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 1948 | ParmVarDecl *New = ParmVarDecl::Create(Context, CurContext, |
| 1949 | D.getIdentifierLoc(), II, |
Daniel Dunbar | b648e8c | 2008-09-03 21:54:21 +0000 | [diff] [blame] | 1950 | parmDeclType, StorageClass, |
Chris Lattner | 3e254fb | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 1951 | 0, 0); |
Anders Carlsson | 3f70c54 | 2008-02-15 07:04:12 +0000 | [diff] [blame] | 1952 | |
Chris Lattner | 3e254fb | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 1953 | if (D.getInvalidType()) |
Steve Naroff | cae537d | 2007-08-28 18:45:29 +0000 | [diff] [blame] | 1954 | New->setInvalidDecl(); |
| 1955 | |
Argiris Kirtzidis | 951f25b | 2008-04-12 00:47:19 +0000 | [diff] [blame] | 1956 | if (II) |
| 1957 | PushOnScopeChains(New, S); |
Nate Begeman | 9f3c4bb | 2008-02-17 21:20:31 +0000 | [diff] [blame] | 1958 | |
Chris Lattner | 9b384ca | 2008-06-29 00:02:00 +0000 | [diff] [blame] | 1959 | ProcessDeclAttributes(New, D); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1960 | return New; |
Chris Lattner | 3e254fb | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 1961 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1962 | } |
Fariborz Jahanian | dfb1c37 | 2007-11-08 23:49:49 +0000 | [diff] [blame] | 1963 | |
Chris Lattner | ea14870 | 2007-10-09 17:14:05 +0000 | [diff] [blame] | 1964 | Sema::DeclTy *Sema::ActOnStartOfFunctionDef(Scope *FnBodyScope, Declarator &D) { |
Argiris Kirtzidis | 95256e6 | 2008-06-28 06:07:14 +0000 | [diff] [blame] | 1965 | assert(getCurFunctionDecl() == 0 && "Function parsing confused"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1966 | assert(D.getTypeObject(0).Kind == DeclaratorChunk::Function && |
| 1967 | "Not a function declarator!"); |
| 1968 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun; |
Chris Lattner | 3e254fb | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 1969 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1970 | // Verify 6.9.1p6: 'every identifier in the identifier list shall be declared' |
| 1971 | // for a K&R function. |
| 1972 | if (!FTI.hasPrototype) { |
| 1973 | for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) { |
Chris Lattner | 3e254fb | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 1974 | if (FTI.ArgInfo[i].Param == 0) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1975 | Diag(FTI.ArgInfo[i].IdentLoc, diag::ext_param_not_declared, |
| 1976 | FTI.ArgInfo[i].Ident->getName()); |
| 1977 | // Implicitly declare the argument as type 'int' for lack of a better |
| 1978 | // type. |
Chris Lattner | 3e254fb | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 1979 | DeclSpec DS; |
| 1980 | const char* PrevSpec; // unused |
| 1981 | DS.SetTypeSpecType(DeclSpec::TST_int, FTI.ArgInfo[i].IdentLoc, |
| 1982 | PrevSpec); |
| 1983 | Declarator ParamD(DS, Declarator::KNRTypeListContext); |
| 1984 | ParamD.SetIdentifier(FTI.ArgInfo[i].Ident, FTI.ArgInfo[i].IdentLoc); |
| 1985 | FTI.ArgInfo[i].Param = ActOnParamDeclarator(FnBodyScope, ParamD); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1986 | } |
| 1987 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1988 | } else { |
Chris Lattner | 3e254fb | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 1989 | // FIXME: Diagnose arguments without names in C. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1990 | } |
| 1991 | |
| 1992 | Scope *GlobalScope = FnBodyScope->getParent(); |
Steve Naroff | 1d5bd64 | 2008-01-14 20:51:29 +0000 | [diff] [blame] | 1993 | |
Argiris Kirtzidis | 38f1671 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1994 | return ActOnStartOfFunctionDef(FnBodyScope, |
Daniel Dunbar | 72eaf8a | 2008-08-05 16:28:08 +0000 | [diff] [blame] | 1995 | ActOnDeclarator(GlobalScope, D, 0)); |
Argiris Kirtzidis | 38f1671 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1996 | } |
| 1997 | |
| 1998 | Sema::DeclTy *Sema::ActOnStartOfFunctionDef(Scope *FnBodyScope, DeclTy *D) { |
| 1999 | Decl *decl = static_cast<Decl*>(D); |
Chris Lattner | 2d2216b | 2008-02-16 01:20:36 +0000 | [diff] [blame] | 2000 | FunctionDecl *FD = cast<FunctionDecl>(decl); |
Douglas Gregor | 56da786 | 2008-10-29 15:10:40 +0000 | [diff] [blame] | 2001 | |
| 2002 | // See if this is a redefinition. |
| 2003 | const FunctionDecl *Definition; |
| 2004 | if (FD->getBody(Definition)) { |
| 2005 | Diag(FD->getLocation(), diag::err_redefinition, |
| 2006 | FD->getName()); |
| 2007 | Diag(Definition->getLocation(), diag::err_previous_definition); |
| 2008 | } |
| 2009 | |
Chris Lattner | f3874bc | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 2010 | PushDeclContext(FD); |
Chris Lattner | 3e254fb | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2011 | |
| 2012 | // Check the validity of our function parameters |
| 2013 | CheckParmsForFunctionDef(FD); |
| 2014 | |
| 2015 | // Introduce our parameters into the function scope |
| 2016 | for (unsigned p = 0, NumParams = FD->getNumParams(); p < NumParams; ++p) { |
| 2017 | ParmVarDecl *Param = FD->getParamDecl(p); |
| 2018 | // If this has an identifier, add it to the scope stack. |
Argiris Kirtzidis | 951f25b | 2008-04-12 00:47:19 +0000 | [diff] [blame] | 2019 | if (Param->getIdentifier()) |
| 2020 | PushOnScopeChains(Param, FnBodyScope); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2021 | } |
Chris Lattner | 3e254fb | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2022 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2023 | return FD; |
| 2024 | } |
| 2025 | |
Steve Naroff | 99ee430 | 2007-11-11 23:20:51 +0000 | [diff] [blame] | 2026 | Sema::DeclTy *Sema::ActOnFinishFunctionBody(DeclTy *D, StmtTy *Body) { |
| 2027 | Decl *dcl = static_cast<Decl *>(D); |
Steve Naroff | 3ac43f9 | 2008-07-25 17:57:26 +0000 | [diff] [blame] | 2028 | if (FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(dcl)) { |
Steve Naroff | 99ee430 | 2007-11-11 23:20:51 +0000 | [diff] [blame] | 2029 | FD->setBody((Stmt*)Body); |
Argiris Kirtzidis | 95256e6 | 2008-06-28 06:07:14 +0000 | [diff] [blame] | 2030 | assert(FD == getCurFunctionDecl() && "Function parsing confused"); |
Steve Naroff | 3ac43f9 | 2008-07-25 17:57:26 +0000 | [diff] [blame] | 2031 | } else if (ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(dcl)) { |
Steve Naroff | 99ee430 | 2007-11-11 23:20:51 +0000 | [diff] [blame] | 2032 | MD->setBody((Stmt*)Body); |
Steve Naroff | 3ac43f9 | 2008-07-25 17:57:26 +0000 | [diff] [blame] | 2033 | } else |
| 2034 | return 0; |
Chris Lattner | f3874bc | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 2035 | PopDeclContext(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2036 | // Verify and clean out per-function state. |
| 2037 | |
| 2038 | // Check goto/label use. |
| 2039 | for (llvm::DenseMap<IdentifierInfo*, LabelStmt*>::iterator |
| 2040 | I = LabelMap.begin(), E = LabelMap.end(); I != E; ++I) { |
| 2041 | // Verify that we have no forward references left. If so, there was a goto |
| 2042 | // or address of a label taken, but no definition of it. Label fwd |
| 2043 | // definitions are indicated with a null substmt. |
| 2044 | if (I->second->getSubStmt() == 0) { |
| 2045 | LabelStmt *L = I->second; |
| 2046 | // Emit error. |
| 2047 | Diag(L->getIdentLoc(), diag::err_undeclared_label_use, L->getName()); |
| 2048 | |
| 2049 | // At this point, we have gotos that use the bogus label. Stitch it into |
| 2050 | // the function body so that they aren't leaked and that the AST is well |
| 2051 | // formed. |
Chris Lattner | 8334334 | 2008-01-25 00:01:10 +0000 | [diff] [blame] | 2052 | if (Body) { |
| 2053 | L->setSubStmt(new NullStmt(L->getIdentLoc())); |
| 2054 | cast<CompoundStmt>((Stmt*)Body)->push_back(L); |
| 2055 | } else { |
| 2056 | // The whole function wasn't parsed correctly, just delete this. |
| 2057 | delete L; |
| 2058 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2059 | } |
| 2060 | } |
| 2061 | LabelMap.clear(); |
| 2062 | |
Steve Naroff | 99ee430 | 2007-11-11 23:20:51 +0000 | [diff] [blame] | 2063 | return D; |
Fariborz Jahanian | e6f59f1 | 2007-11-10 16:31:34 +0000 | [diff] [blame] | 2064 | } |
| 2065 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2066 | /// ImplicitlyDefineFunction - An undeclared identifier was used in a function |
| 2067 | /// call, forming a call to an implicitly defined function (per C99 6.5.1p2). |
Steve Naroff | f0c31dd | 2007-09-16 16:16:00 +0000 | [diff] [blame] | 2068 | ScopedDecl *Sema::ImplicitlyDefineFunction(SourceLocation Loc, |
| 2069 | IdentifierInfo &II, Scope *S) { |
Chris Lattner | dea31bf | 2008-05-05 21:18:06 +0000 | [diff] [blame] | 2070 | // Extension in C99. Legal in C90, but warn about it. |
| 2071 | if (getLangOptions().C99) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2072 | Diag(Loc, diag::ext_implicit_function_decl, II.getName()); |
Chris Lattner | dea31bf | 2008-05-05 21:18:06 +0000 | [diff] [blame] | 2073 | else |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2074 | Diag(Loc, diag::warn_implicit_function_decl, II.getName()); |
| 2075 | |
| 2076 | // FIXME: handle stuff like: |
| 2077 | // void foo() { extern float X(); } |
| 2078 | // void bar() { X(); } <-- implicit decl for X in another scope. |
| 2079 | |
| 2080 | // Set a Declarator for the implicit definition: int foo(); |
| 2081 | const char *Dummy; |
| 2082 | DeclSpec DS; |
| 2083 | bool Error = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, Dummy); |
| 2084 | Error = Error; // Silence warning. |
| 2085 | assert(!Error && "Error setting up implicit decl!"); |
| 2086 | Declarator D(DS, Declarator::BlockContext); |
Argiris Kirtzidis | 4b269b4 | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 2087 | D.AddTypeInfo(DeclaratorChunk::getFunction(false, false, 0, 0, 0, Loc)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2088 | D.SetIdentifier(&II, Loc); |
| 2089 | |
Argiris Kirtzidis | bb4f7b4 | 2008-05-01 21:04:16 +0000 | [diff] [blame] | 2090 | // Insert this function into translation-unit scope. |
| 2091 | |
| 2092 | DeclContext *PrevDC = CurContext; |
| 2093 | CurContext = Context.getTranslationUnitDecl(); |
| 2094 | |
Steve Naroff | 9104f3c | 2008-04-04 14:32:09 +0000 | [diff] [blame] | 2095 | FunctionDecl *FD = |
Daniel Dunbar | 72eaf8a | 2008-08-05 16:28:08 +0000 | [diff] [blame] | 2096 | dyn_cast<FunctionDecl>(static_cast<Decl*>(ActOnDeclarator(TUScope, D, 0))); |
Steve Naroff | 9104f3c | 2008-04-04 14:32:09 +0000 | [diff] [blame] | 2097 | FD->setImplicit(); |
Argiris Kirtzidis | bb4f7b4 | 2008-05-01 21:04:16 +0000 | [diff] [blame] | 2098 | |
| 2099 | CurContext = PrevDC; |
| 2100 | |
Steve Naroff | 9104f3c | 2008-04-04 14:32:09 +0000 | [diff] [blame] | 2101 | return FD; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2102 | } |
| 2103 | |
| 2104 | |
Chris Lattner | 82bb479 | 2007-11-14 06:34:38 +0000 | [diff] [blame] | 2105 | TypedefDecl *Sema::ParseTypedefDecl(Scope *S, Declarator &D, QualType T, |
Steve Naroff | 2591e1b | 2007-09-13 23:52:58 +0000 | [diff] [blame] | 2106 | ScopedDecl *LastDeclarator) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2107 | assert(D.getIdentifier() && "Wrong callback for declspec without declarator"); |
Steve Naroff | d1ad6ae | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 2108 | assert(!T.isNull() && "GetTypeForDeclarator() returned null type"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2109 | |
| 2110 | // Scope manipulation handled by caller. |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2111 | TypedefDecl *NewTD = TypedefDecl::Create(Context, CurContext, |
| 2112 | D.getIdentifierLoc(), |
Chris Lattner | e465048 | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 2113 | D.getIdentifier(), |
Chris Lattner | 58114f0 | 2008-03-15 21:32:50 +0000 | [diff] [blame] | 2114 | T, LastDeclarator); |
Steve Naroff | d1ad6ae | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 2115 | if (D.getInvalidType()) |
| 2116 | NewTD->setInvalidDecl(); |
| 2117 | return NewTD; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2118 | } |
| 2119 | |
Steve Naroff | 0acc9c9 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 2120 | /// ActOnTag - This is invoked when we see 'struct foo' or 'struct {'. In the |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2121 | /// former case, Name will be non-null. In the later case, Name will be null. |
| 2122 | /// TagType indicates what kind of tag this is. TK indicates whether this is a |
| 2123 | /// reference/declaration/definition of a tag. |
Steve Naroff | 0acc9c9 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 2124 | Sema::DeclTy *Sema::ActOnTag(Scope *S, unsigned TagType, TagKind TK, |
Argiris Kirtzidis | 311db8c | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2125 | SourceLocation KWLoc, const CXXScopeSpec &SS, |
| 2126 | IdentifierInfo *Name, SourceLocation NameLoc, |
| 2127 | AttributeList *Attr) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2128 | // If this is a use of an existing tag, it must have a name. |
| 2129 | assert((Name != 0 || TK == TK_Definition) && |
| 2130 | "Nameless record must be a definition!"); |
| 2131 | |
Argiris Kirtzidis | c6cc7d5 | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 2132 | TagDecl::TagKind Kind; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2133 | switch (TagType) { |
| 2134 | default: assert(0 && "Unknown tag type!"); |
Argiris Kirtzidis | c6cc7d5 | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 2135 | case DeclSpec::TST_struct: Kind = TagDecl::TK_struct; break; |
| 2136 | case DeclSpec::TST_union: Kind = TagDecl::TK_union; break; |
| 2137 | case DeclSpec::TST_class: Kind = TagDecl::TK_class; break; |
| 2138 | case DeclSpec::TST_enum: Kind = TagDecl::TK_enum; break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2139 | } |
| 2140 | |
Ted Kremenek | 46a837c | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 2141 | // Two code paths: a new one for structs/unions/classes where we create |
| 2142 | // separate decls for forward declarations, and an old (eventually to |
| 2143 | // be removed) code path for enums. |
| 2144 | if (Kind != TagDecl::TK_enum) |
Argiris Kirtzidis | 70b5413 | 2008-11-09 22:09:58 +0000 | [diff] [blame] | 2145 | return ActOnTagStruct(S, Kind, TK, KWLoc, SS, Name, NameLoc, Attr); |
Ted Kremenek | 46a837c | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 2146 | |
Argiris Kirtzidis | 5ce4db8 | 2008-11-09 22:53:32 +0000 | [diff] [blame] | 2147 | DeclContext *DC = CurContext; |
| 2148 | ScopedDecl *PrevDecl = 0; |
Argiris Kirtzidis | 054a263 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 2149 | |
Argiris Kirtzidis | 70b5413 | 2008-11-09 22:09:58 +0000 | [diff] [blame] | 2150 | if (Name && SS.isNotEmpty()) { |
| 2151 | // We have a nested-name tag ('struct foo::bar'). |
| 2152 | |
| 2153 | // Check for invalid 'foo::'. |
Argiris Kirtzidis | 5ce4db8 | 2008-11-09 22:53:32 +0000 | [diff] [blame] | 2154 | if (SS.isInvalid()) { |
Argiris Kirtzidis | 70b5413 | 2008-11-09 22:09:58 +0000 | [diff] [blame] | 2155 | Name = 0; |
| 2156 | goto CreateNewDecl; |
| 2157 | } |
| 2158 | |
Argiris Kirtzidis | 5ce4db8 | 2008-11-09 22:53:32 +0000 | [diff] [blame] | 2159 | DC = static_cast<DeclContext*>(SS.getScopeRep()); |
| 2160 | // Look-up name inside 'foo::'. |
Argiris Kirtzidis | 70b5413 | 2008-11-09 22:09:58 +0000 | [diff] [blame] | 2161 | PrevDecl = dyn_cast_or_null<TagDecl>(LookupDecl(Name, Decl::IDNS_Tag,S,DC)); |
| 2162 | |
| 2163 | // A tag 'foo::bar' must already exist. |
| 2164 | if (PrevDecl == 0) { |
| 2165 | Diag(NameLoc, diag::err_not_tag_in_scope, Name->getName(), |
| 2166 | SS.getRange()); |
| 2167 | Name = 0; |
| 2168 | goto CreateNewDecl; |
| 2169 | } |
| 2170 | } else { |
| 2171 | // If this is a named struct, check to see if there was a previous forward |
| 2172 | // declaration or definition. |
| 2173 | // Use ScopedDecl instead of TagDecl, because a NamespaceDecl may come up. |
| 2174 | PrevDecl = dyn_cast_or_null<ScopedDecl>(LookupDecl(Name, Decl::IDNS_Tag,S)); |
| 2175 | } |
| 2176 | |
Ted Kremenek | d443415 | 2008-09-02 21:26:19 +0000 | [diff] [blame] | 2177 | if (PrevDecl) { |
Argiris Kirtzidis | 03e6aaf | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 2178 | assert((isa<TagDecl>(PrevDecl) || isa<NamespaceDecl>(PrevDecl)) && |
| 2179 | "unexpected Decl type"); |
| 2180 | if (TagDecl *PrevTagDecl = dyn_cast<TagDecl>(PrevDecl)) { |
Chris Lattner | 5bf0ad5 | 2008-07-03 03:30:58 +0000 | [diff] [blame] | 2181 | // If this is a use of a previous tag, or if the tag is already declared |
| 2182 | // in the same scope (so that the definition/declaration completes or |
Argiris Kirtzidis | 03e6aaf | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 2183 | // rementions the tag), reuse the decl. |
Argiris Kirtzidis | 5ce4db8 | 2008-11-09 22:53:32 +0000 | [diff] [blame] | 2184 | if (TK == TK_Reference || isDeclInScope(PrevDecl, DC, S)) { |
Chris Lattner | 5bf0ad5 | 2008-07-03 03:30:58 +0000 | [diff] [blame] | 2185 | // Make sure that this wasn't declared as an enum and now used as a |
| 2186 | // struct or something similar. |
Argiris Kirtzidis | c6cc7d5 | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 2187 | if (PrevTagDecl->getTagKind() != Kind) { |
Argiris Kirtzidis | 03e6aaf | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 2188 | Diag(KWLoc, diag::err_use_with_wrong_tag, Name->getName()); |
| 2189 | Diag(PrevDecl->getLocation(), diag::err_previous_use); |
Chris Lattner | 5bf0ad5 | 2008-07-03 03:30:58 +0000 | [diff] [blame] | 2190 | // Recover by making this an anonymous redefinition. |
Argiris Kirtzidis | 03e6aaf | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 2191 | Name = 0; |
Chris Lattner | 5bf0ad5 | 2008-07-03 03:30:58 +0000 | [diff] [blame] | 2192 | PrevDecl = 0; |
Argiris Kirtzidis | 03e6aaf | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 2193 | } else { |
Chris Lattner | 5bf0ad5 | 2008-07-03 03:30:58 +0000 | [diff] [blame] | 2194 | // If this is a use or a forward declaration, we're good. |
| 2195 | if (TK != TK_Definition) |
| 2196 | return PrevDecl; |
| 2197 | |
| 2198 | // Diagnose attempts to redefine a tag. |
| 2199 | if (PrevTagDecl->isDefinition()) { |
| 2200 | Diag(NameLoc, diag::err_redefinition, Name->getName()); |
| 2201 | Diag(PrevDecl->getLocation(), diag::err_previous_definition); |
| 2202 | // If this is a redefinition, recover by making this struct be |
| 2203 | // anonymous, which will make any later references get the previous |
| 2204 | // definition. |
| 2205 | Name = 0; |
| 2206 | } else { |
| 2207 | // Okay, this is definition of a previously declared or referenced |
| 2208 | // tag. Move the location of the decl to be the definition site. |
| 2209 | PrevDecl->setLocation(NameLoc); |
| 2210 | return PrevDecl; |
| 2211 | } |
Argiris Kirtzidis | 03e6aaf | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 2212 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2213 | } |
Argiris Kirtzidis | 03e6aaf | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 2214 | // If we get here, this is a definition of a new struct type in a nested |
| 2215 | // scope, e.g. "struct foo; void bar() { struct foo; }", just create a new |
| 2216 | // type. |
| 2217 | } else { |
Argiris Kirtzidis | 5beb45f | 2008-07-16 07:45:46 +0000 | [diff] [blame] | 2218 | // PrevDecl is a namespace. |
Argiris Kirtzidis | 5ce4db8 | 2008-11-09 22:53:32 +0000 | [diff] [blame] | 2219 | if (isDeclInScope(PrevDecl, DC, S)) { |
Ted Kremenek | 40e70e7 | 2008-09-03 18:03:35 +0000 | [diff] [blame] | 2220 | // The tag name clashes with a namespace name, issue an error and |
| 2221 | // recover by making this tag be anonymous. |
Argiris Kirtzidis | 5beb45f | 2008-07-16 07:45:46 +0000 | [diff] [blame] | 2222 | Diag(NameLoc, diag::err_redefinition_different_kind, Name->getName()); |
| 2223 | Diag(PrevDecl->getLocation(), diag::err_previous_definition); |
| 2224 | Name = 0; |
| 2225 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2226 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2227 | } |
Argiris Kirtzidis | 054a263 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 2228 | |
Argiris Kirtzidis | 054a263 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 2229 | CreateNewDecl: |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2230 | |
| 2231 | // If there is an identifier, use the location of the identifier as the |
| 2232 | // location of the decl, otherwise use the location of the struct/union |
| 2233 | // keyword. |
| 2234 | SourceLocation Loc = NameLoc.isValid() ? NameLoc : KWLoc; |
| 2235 | |
| 2236 | // Otherwise, if this is the first time we've seen this tag, create the decl. |
| 2237 | TagDecl *New; |
Argiris Kirtzidis | c6cc7d5 | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 2238 | if (Kind == TagDecl::TK_enum) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2239 | // FIXME: Tag decls should be chained to any simultaneous vardecls, e.g.: |
| 2240 | // enum X { A, B, C } D; D should chain to X. |
Argiris Kirtzidis | 5ce4db8 | 2008-11-09 22:53:32 +0000 | [diff] [blame] | 2241 | New = EnumDecl::Create(Context, DC, Loc, Name, 0); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2242 | // If this is an undefined enum, warn. |
| 2243 | if (TK != TK_Definition) Diag(Loc, diag::ext_forward_ref_enum); |
Argiris Kirtzidis | c6cc7d5 | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 2244 | } else { |
| 2245 | // struct/union/class |
| 2246 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2247 | // FIXME: Tag decls should be chained to any simultaneous vardecls, e.g.: |
| 2248 | // struct X { int A; } D; D should chain to X. |
Argiris Kirtzidis | 38f1671 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 2249 | if (getLangOptions().CPlusPlus) |
Ted Kremenek | 770b11d | 2008-09-05 17:39:33 +0000 | [diff] [blame] | 2250 | // FIXME: Look for a way to use RecordDecl for simple structs. |
Argiris Kirtzidis | 5ce4db8 | 2008-11-09 22:53:32 +0000 | [diff] [blame] | 2251 | New = CXXRecordDecl::Create(Context, Kind, DC, Loc, Name); |
Argiris Kirtzidis | 38f1671 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 2252 | else |
Argiris Kirtzidis | 5ce4db8 | 2008-11-09 22:53:32 +0000 | [diff] [blame] | 2253 | New = RecordDecl::Create(Context, Kind, DC, Loc, Name); |
Argiris Kirtzidis | 38f1671 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 2254 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2255 | |
| 2256 | // If this has an identifier, add it to the scope stack. |
| 2257 | if (Name) { |
Chris Lattner | a754990 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 2258 | // The scope passed in may not be a decl scope. Zip up the scope tree until |
| 2259 | // we find one that is. |
| 2260 | while ((S->getFlags() & Scope::DeclScope) == 0) |
| 2261 | S = S->getParent(); |
| 2262 | |
| 2263 | // Add it to the decl chain. |
Argiris Kirtzidis | 951f25b | 2008-04-12 00:47:19 +0000 | [diff] [blame] | 2264 | PushOnScopeChains(New, S); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2265 | } |
Chris Lattner | 33aad6e | 2008-02-06 00:51:33 +0000 | [diff] [blame] | 2266 | |
Chris Lattner | d7e83d8 | 2008-06-28 23:58:55 +0000 | [diff] [blame] | 2267 | if (Attr) |
| 2268 | ProcessDeclAttributeList(New, Attr); |
Argiris Kirtzidis | 881964b | 2008-11-09 23:41:00 +0000 | [diff] [blame^] | 2269 | |
| 2270 | // Set the lexical context. If the tag has a C++ scope specifier, the |
| 2271 | // lexical context will be different from the semantic context. |
| 2272 | New->setLexicalDeclContext(CurContext); |
| 2273 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2274 | return New; |
| 2275 | } |
| 2276 | |
Ted Kremenek | 46a837c | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 2277 | /// ActOnTagStruct - New "ActOnTag" logic for structs/unions/classes. Unlike |
| 2278 | /// the logic for enums, we create separate decls for forward declarations. |
| 2279 | /// This is called by ActOnTag, but eventually will replace its logic. |
| 2280 | Sema::DeclTy *Sema::ActOnTagStruct(Scope *S, TagDecl::TagKind Kind, TagKind TK, |
Argiris Kirtzidis | 70b5413 | 2008-11-09 22:09:58 +0000 | [diff] [blame] | 2281 | SourceLocation KWLoc, const CXXScopeSpec &SS, |
| 2282 | IdentifierInfo *Name, SourceLocation NameLoc, |
| 2283 | AttributeList *Attr) { |
Argiris Kirtzidis | 5ce4db8 | 2008-11-09 22:53:32 +0000 | [diff] [blame] | 2284 | DeclContext *DC = CurContext; |
| 2285 | ScopedDecl *PrevDecl = 0; |
Argiris Kirtzidis | 70b5413 | 2008-11-09 22:09:58 +0000 | [diff] [blame] | 2286 | |
| 2287 | if (Name && SS.isNotEmpty()) { |
| 2288 | // We have a nested-name tag ('struct foo::bar'). |
| 2289 | |
| 2290 | // Check for invalid 'foo::'. |
Argiris Kirtzidis | 5ce4db8 | 2008-11-09 22:53:32 +0000 | [diff] [blame] | 2291 | if (SS.isInvalid()) { |
Argiris Kirtzidis | 70b5413 | 2008-11-09 22:09:58 +0000 | [diff] [blame] | 2292 | Name = 0; |
| 2293 | goto CreateNewDecl; |
| 2294 | } |
| 2295 | |
Argiris Kirtzidis | 5ce4db8 | 2008-11-09 22:53:32 +0000 | [diff] [blame] | 2296 | DC = static_cast<DeclContext*>(SS.getScopeRep()); |
| 2297 | // Look-up name inside 'foo::'. |
Argiris Kirtzidis | 70b5413 | 2008-11-09 22:09:58 +0000 | [diff] [blame] | 2298 | PrevDecl = dyn_cast_or_null<TagDecl>(LookupDecl(Name, Decl::IDNS_Tag,S,DC)); |
| 2299 | |
| 2300 | // A tag 'foo::bar' must already exist. |
| 2301 | if (PrevDecl == 0) { |
| 2302 | Diag(NameLoc, diag::err_not_tag_in_scope, Name->getName(), |
| 2303 | SS.getRange()); |
| 2304 | Name = 0; |
| 2305 | goto CreateNewDecl; |
| 2306 | } |
| 2307 | } else { |
| 2308 | // If this is a named struct, check to see if there was a previous forward |
| 2309 | // declaration or definition. |
| 2310 | // Use ScopedDecl instead of TagDecl, because a NamespaceDecl may come up. |
| 2311 | PrevDecl = dyn_cast_or_null<ScopedDecl>(LookupDecl(Name, Decl::IDNS_Tag,S)); |
| 2312 | } |
Ted Kremenek | 46a837c | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 2313 | |
| 2314 | if (PrevDecl) { |
| 2315 | assert((isa<TagDecl>(PrevDecl) || isa<NamespaceDecl>(PrevDecl)) && |
| 2316 | "unexpected Decl type"); |
| 2317 | |
| 2318 | if (TagDecl *PrevTagDecl = dyn_cast<TagDecl>(PrevDecl)) { |
| 2319 | // If this is a use of a previous tag, or if the tag is already declared |
| 2320 | // in the same scope (so that the definition/declaration completes or |
| 2321 | // rementions the tag), reuse the decl. |
Argiris Kirtzidis | 5ce4db8 | 2008-11-09 22:53:32 +0000 | [diff] [blame] | 2322 | if (TK == TK_Reference || isDeclInScope(PrevDecl, DC, S)) { |
Ted Kremenek | 46a837c | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 2323 | // Make sure that this wasn't declared as an enum and now used as a |
| 2324 | // struct or something similar. |
| 2325 | if (PrevTagDecl->getTagKind() != Kind) { |
| 2326 | Diag(KWLoc, diag::err_use_with_wrong_tag, Name->getName()); |
| 2327 | Diag(PrevDecl->getLocation(), diag::err_previous_use); |
| 2328 | // Recover by making this an anonymous redefinition. |
| 2329 | Name = 0; |
| 2330 | PrevDecl = 0; |
| 2331 | } else { |
| 2332 | // If this is a use, return the original decl. |
| 2333 | |
| 2334 | // FIXME: In the future, return a variant or some other clue |
| 2335 | // for the consumer of this Decl to know it doesn't own it. |
| 2336 | // For our current ASTs this shouldn't be a problem, but will |
| 2337 | // need to be changed with DeclGroups. |
| 2338 | if (TK == TK_Reference) |
| 2339 | return PrevDecl; |
| 2340 | |
| 2341 | // The new decl is a definition? |
| 2342 | if (TK == TK_Definition) { |
| 2343 | // Diagnose attempts to redefine a tag. |
| 2344 | if (RecordDecl* DefRecord = |
| 2345 | cast<RecordDecl>(PrevTagDecl)->getDefinition(Context)) { |
| 2346 | Diag(NameLoc, diag::err_redefinition, Name->getName()); |
| 2347 | Diag(DefRecord->getLocation(), diag::err_previous_definition); |
| 2348 | // If this is a redefinition, recover by making this struct be |
| 2349 | // anonymous, which will make any later references get the previous |
| 2350 | // definition. |
| 2351 | Name = 0; |
| 2352 | PrevDecl = 0; |
| 2353 | } |
| 2354 | // Okay, this is definition of a previously declared or referenced |
| 2355 | // tag. We're going to create a new Decl. |
| 2356 | } |
| 2357 | } |
| 2358 | // If we get here we have (another) forward declaration. Just create |
| 2359 | // a new decl. |
| 2360 | } |
| 2361 | else { |
| 2362 | // If we get here, this is a definition of a new struct type in a nested |
| 2363 | // scope, e.g. "struct foo; void bar() { struct foo; }", just create a |
| 2364 | // new decl/type. We set PrevDecl to NULL so that the Records |
| 2365 | // have distinct types. |
| 2366 | PrevDecl = 0; |
| 2367 | } |
| 2368 | } else { |
| 2369 | // PrevDecl is a namespace. |
Argiris Kirtzidis | 5ce4db8 | 2008-11-09 22:53:32 +0000 | [diff] [blame] | 2370 | if (isDeclInScope(PrevDecl, DC, S)) { |
Ted Kremenek | 46a837c | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 2371 | // The tag name clashes with a namespace name, issue an error and |
| 2372 | // recover by making this tag be anonymous. |
| 2373 | Diag(NameLoc, diag::err_redefinition_different_kind, Name->getName()); |
| 2374 | Diag(PrevDecl->getLocation(), diag::err_previous_definition); |
| 2375 | Name = 0; |
| 2376 | } |
| 2377 | } |
| 2378 | } |
Argiris Kirtzidis | 70b5413 | 2008-11-09 22:09:58 +0000 | [diff] [blame] | 2379 | |
| 2380 | CreateNewDecl: |
| 2381 | |
Ted Kremenek | 46a837c | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 2382 | // If there is an identifier, use the location of the identifier as the |
| 2383 | // location of the decl, otherwise use the location of the struct/union |
| 2384 | // keyword. |
| 2385 | SourceLocation Loc = NameLoc.isValid() ? NameLoc : KWLoc; |
| 2386 | |
| 2387 | // Otherwise, if this is the first time we've seen this tag, create the decl. |
| 2388 | TagDecl *New; |
| 2389 | |
| 2390 | // FIXME: Tag decls should be chained to any simultaneous vardecls, e.g.: |
| 2391 | // struct X { int A; } D; D should chain to X. |
| 2392 | if (getLangOptions().CPlusPlus) |
Ted Kremenek | 770b11d | 2008-09-05 17:39:33 +0000 | [diff] [blame] | 2393 | // FIXME: Look for a way to use RecordDecl for simple structs. |
Argiris Kirtzidis | 5ce4db8 | 2008-11-09 22:53:32 +0000 | [diff] [blame] | 2394 | New = CXXRecordDecl::Create(Context, Kind, DC, Loc, Name, |
Ted Kremenek | 46a837c | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 2395 | dyn_cast_or_null<CXXRecordDecl>(PrevDecl)); |
| 2396 | else |
Argiris Kirtzidis | 5ce4db8 | 2008-11-09 22:53:32 +0000 | [diff] [blame] | 2397 | New = RecordDecl::Create(Context, Kind, DC, Loc, Name, |
Ted Kremenek | 46a837c | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 2398 | dyn_cast_or_null<RecordDecl>(PrevDecl)); |
| 2399 | |
| 2400 | // If this has an identifier, add it to the scope stack. |
| 2401 | if ((TK == TK_Definition || !PrevDecl) && Name) { |
| 2402 | // The scope passed in may not be a decl scope. Zip up the scope tree until |
| 2403 | // we find one that is. |
| 2404 | while ((S->getFlags() & Scope::DeclScope) == 0) |
| 2405 | S = S->getParent(); |
| 2406 | |
| 2407 | // Add it to the decl chain. |
| 2408 | PushOnScopeChains(New, S); |
| 2409 | } |
Daniel Dunbar | 2cb762f | 2008-10-16 02:34:03 +0000 | [diff] [blame] | 2410 | |
| 2411 | // Handle #pragma pack: if the #pragma pack stack has non-default |
| 2412 | // alignment, make up a packed attribute for this decl. These |
| 2413 | // attributes are checked when the ASTContext lays out the |
| 2414 | // structure. |
| 2415 | // |
| 2416 | // It is important for implementing the correct semantics that this |
| 2417 | // happen here (in act on tag decl). The #pragma pack stack is |
| 2418 | // maintained as a result of parser callbacks which can occur at |
| 2419 | // many points during the parsing of a struct declaration (because |
| 2420 | // the #pragma tokens are effectively skipped over during the |
| 2421 | // parsing of the struct). |
| 2422 | if (unsigned Alignment = PackContext.getAlignment()) |
| 2423 | New->addAttr(new PackedAttr(Alignment * 8)); |
Ted Kremenek | 46a837c | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 2424 | |
| 2425 | if (Attr) |
| 2426 | ProcessDeclAttributeList(New, Attr); |
| 2427 | |
Argiris Kirtzidis | 881964b | 2008-11-09 23:41:00 +0000 | [diff] [blame^] | 2428 | // Set the lexical context. If the tag has a C++ scope specifier, the |
| 2429 | // lexical context will be different from the semantic context. |
| 2430 | New->setLexicalDeclContext(CurContext); |
| 2431 | |
Ted Kremenek | 46a837c | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 2432 | return New; |
| 2433 | } |
| 2434 | |
| 2435 | |
Chris Lattner | 1bf58f6 | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 2436 | /// Collect the instance variables declared in an Objective-C object. Used in |
| 2437 | /// the creation of structures from objects using the @defs directive. |
Ted Kremenek | e5bedfe | 2008-08-20 03:26:33 +0000 | [diff] [blame] | 2438 | static void CollectIvars(ObjCInterfaceDecl *Class, ASTContext& Ctx, |
Chris Lattner | e705e5e | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 2439 | llvm::SmallVectorImpl<Sema::DeclTy*> &ivars) { |
Chris Lattner | 1bf58f6 | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 2440 | if (Class->getSuperClass()) |
Ted Kremenek | e5bedfe | 2008-08-20 03:26:33 +0000 | [diff] [blame] | 2441 | CollectIvars(Class->getSuperClass(), Ctx, ivars); |
| 2442 | |
| 2443 | // For each ivar, create a fresh ObjCAtDefsFieldDecl. |
Ted Kremenek | 40e70e7 | 2008-09-03 18:03:35 +0000 | [diff] [blame] | 2444 | for (ObjCInterfaceDecl::ivar_iterator |
| 2445 | I=Class->ivar_begin(), E=Class->ivar_end(); I!=E; ++I) { |
| 2446 | |
Ted Kremenek | e5bedfe | 2008-08-20 03:26:33 +0000 | [diff] [blame] | 2447 | ObjCIvarDecl* ID = *I; |
| 2448 | ivars.push_back(ObjCAtDefsFieldDecl::Create(Ctx, ID->getLocation(), |
| 2449 | ID->getIdentifier(), |
| 2450 | ID->getType(), |
| 2451 | ID->getBitWidth())); |
| 2452 | } |
Chris Lattner | 1bf58f6 | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 2453 | } |
| 2454 | |
| 2455 | /// Called whenever @defs(ClassName) is encountered in the source. Inserts the |
| 2456 | /// instance variables of ClassName into Decls. |
| 2457 | void Sema::ActOnDefs(Scope *S, SourceLocation DeclStart, |
| 2458 | IdentifierInfo *ClassName, |
Chris Lattner | e705e5e | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 2459 | llvm::SmallVectorImpl<DeclTy*> &Decls) { |
Chris Lattner | 1bf58f6 | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 2460 | // Check that ClassName is a valid class |
| 2461 | ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName); |
| 2462 | if (!Class) { |
| 2463 | Diag(DeclStart, diag::err_undef_interface, ClassName->getName()); |
| 2464 | return; |
| 2465 | } |
Chris Lattner | 1bf58f6 | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 2466 | // Collect the instance variables |
Ted Kremenek | e5bedfe | 2008-08-20 03:26:33 +0000 | [diff] [blame] | 2467 | CollectIvars(Class, Context, Decls); |
Chris Lattner | 1bf58f6 | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 2468 | } |
| 2469 | |
Eli Friedman | 48fb3ee | 2008-06-03 21:01:11 +0000 | [diff] [blame] | 2470 | QualType Sema::TryFixInvalidVariablyModifiedType(QualType T) { |
| 2471 | // This method tries to turn a variable array into a constant |
| 2472 | // array even when the size isn't an ICE. This is necessary |
| 2473 | // for compatibility with code that depends on gcc's buggy |
| 2474 | // constant expression folding, like struct {char x[(int)(char*)2];} |
| 2475 | if (const VariableArrayType* VLATy = dyn_cast<VariableArrayType>(T)) { |
Anders Carlsson | c7436af | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 2476 | APValue Result; |
Eli Friedman | 48fb3ee | 2008-06-03 21:01:11 +0000 | [diff] [blame] | 2477 | if (VLATy->getSizeExpr() && |
Chris Lattner | 334b194 | 2008-07-11 19:19:21 +0000 | [diff] [blame] | 2478 | VLATy->getSizeExpr()->tryEvaluate(Result, Context) && Result.isInt()) { |
| 2479 | llvm::APSInt &Res = Result.getInt(); |
| 2480 | if (Res > llvm::APSInt(Res.getBitWidth(), Res.isUnsigned())) |
| 2481 | return Context.getConstantArrayType(VLATy->getElementType(), |
| 2482 | Res, ArrayType::Normal, 0); |
Eli Friedman | 48fb3ee | 2008-06-03 21:01:11 +0000 | [diff] [blame] | 2483 | } |
| 2484 | } |
| 2485 | return QualType(); |
| 2486 | } |
| 2487 | |
Steve Naroff | 0acc9c9 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 2488 | /// ActOnField - Each field of a struct/union/class is passed into this in order |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2489 | /// to create a FieldDecl object for it. |
Fariborz Jahanian | 751c617 | 2008-04-10 23:32:45 +0000 | [diff] [blame] | 2490 | Sema::DeclTy *Sema::ActOnField(Scope *S, |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2491 | SourceLocation DeclStart, |
| 2492 | Declarator &D, ExprTy *BitfieldWidth) { |
| 2493 | IdentifierInfo *II = D.getIdentifier(); |
| 2494 | Expr *BitWidth = (Expr*)BitfieldWidth; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2495 | SourceLocation Loc = DeclStart; |
| 2496 | if (II) Loc = D.getIdentifierLoc(); |
| 2497 | |
| 2498 | // FIXME: Unnamed fields can be handled in various different ways, for |
| 2499 | // example, unnamed unions inject all members into the struct namespace! |
Ted Kremenek | 40e70e7 | 2008-09-03 18:03:35 +0000 | [diff] [blame] | 2500 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2501 | if (BitWidth) { |
| 2502 | // TODO: Validate. |
| 2503 | //printf("WARNING: BITFIELDS IGNORED!\n"); |
| 2504 | |
| 2505 | // 6.7.2.1p3 |
| 2506 | // 6.7.2.1p4 |
| 2507 | |
| 2508 | } else { |
| 2509 | // Not a bitfield. |
| 2510 | |
| 2511 | // validate II. |
| 2512 | |
| 2513 | } |
| 2514 | |
| 2515 | QualType T = GetTypeForDeclarator(D, S); |
Steve Naroff | d1ad6ae | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 2516 | assert(!T.isNull() && "GetTypeForDeclarator() returned null type"); |
| 2517 | bool InvalidDecl = false; |
Steve Naroff | 5eb879b | 2007-08-31 17:20:07 +0000 | [diff] [blame] | 2518 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2519 | // C99 6.7.2.1p8: A member of a structure or union may have any type other |
| 2520 | // than a variably modified type. |
Eli Friedman | e007979 | 2008-02-15 12:53:51 +0000 | [diff] [blame] | 2521 | if (T->isVariablyModifiedType()) { |
Eli Friedman | 48fb3ee | 2008-06-03 21:01:11 +0000 | [diff] [blame] | 2522 | QualType FixedTy = TryFixInvalidVariablyModifiedType(T); |
| 2523 | if (!FixedTy.isNull()) { |
| 2524 | Diag(Loc, diag::warn_illegal_constant_array_size, Loc); |
| 2525 | T = FixedTy; |
| 2526 | } else { |
| 2527 | // FIXME: This diagnostic needs work |
| 2528 | Diag(Loc, diag::err_typecheck_illegal_vla, Loc); |
| 2529 | InvalidDecl = true; |
| 2530 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2531 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2532 | // FIXME: Chain fielddecls together. |
Argiris Kirtzidis | 38f1671 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 2533 | FieldDecl *NewFD; |
| 2534 | |
| 2535 | if (getLangOptions().CPlusPlus) { |
| 2536 | // FIXME: Replace CXXFieldDecls with FieldDecls for simple structs. |
| 2537 | NewFD = CXXFieldDecl::Create(Context, cast<CXXRecordDecl>(CurContext), |
| 2538 | Loc, II, T, BitWidth); |
| 2539 | if (II) |
| 2540 | PushOnScopeChains(NewFD, S); |
| 2541 | } |
| 2542 | else |
| 2543 | NewFD = FieldDecl::Create(Context, Loc, II, T, BitWidth); |
Steve Naroff | 7549489 | 2007-09-11 21:17:26 +0000 | [diff] [blame] | 2544 | |
Chris Lattner | 9b384ca | 2008-06-29 00:02:00 +0000 | [diff] [blame] | 2545 | ProcessDeclAttributes(NewFD, D); |
Anders Carlsson | 136cdc3 | 2008-02-16 00:29:18 +0000 | [diff] [blame] | 2546 | |
Steve Naroff | d1ad6ae | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 2547 | if (D.getInvalidType() || InvalidDecl) |
| 2548 | NewFD->setInvalidDecl(); |
| 2549 | return NewFD; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2550 | } |
| 2551 | |
Fariborz Jahanian | bec0d56 | 2007-10-01 16:53:59 +0000 | [diff] [blame] | 2552 | /// TranslateIvarVisibility - Translate visibility from a token ID to an |
| 2553 | /// AST enum value. |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2554 | static ObjCIvarDecl::AccessControl |
Fariborz Jahanian | bec0d56 | 2007-10-01 16:53:59 +0000 | [diff] [blame] | 2555 | TranslateIvarVisibility(tok::ObjCKeywordKind ivarVisibility) { |
Steve Naroff | ffeaa55 | 2007-09-14 23:09:53 +0000 | [diff] [blame] | 2556 | switch (ivarVisibility) { |
Chris Lattner | 504c543 | 2008-10-12 00:28:42 +0000 | [diff] [blame] | 2557 | default: assert(0 && "Unknown visitibility kind"); |
| 2558 | case tok::objc_private: return ObjCIvarDecl::Private; |
| 2559 | case tok::objc_public: return ObjCIvarDecl::Public; |
| 2560 | case tok::objc_protected: return ObjCIvarDecl::Protected; |
| 2561 | case tok::objc_package: return ObjCIvarDecl::Package; |
Steve Naroff | ffeaa55 | 2007-09-14 23:09:53 +0000 | [diff] [blame] | 2562 | } |
| 2563 | } |
| 2564 | |
Fariborz Jahanian | 4e0bb98 | 2008-04-11 16:55:42 +0000 | [diff] [blame] | 2565 | /// ActOnIvar - Each ivar field of an objective-c class is passed into this |
| 2566 | /// in order to create an IvarDecl object for it. |
Fariborz Jahanian | 751c617 | 2008-04-10 23:32:45 +0000 | [diff] [blame] | 2567 | Sema::DeclTy *Sema::ActOnIvar(Scope *S, |
Fariborz Jahanian | 4e0bb98 | 2008-04-11 16:55:42 +0000 | [diff] [blame] | 2568 | SourceLocation DeclStart, |
| 2569 | Declarator &D, ExprTy *BitfieldWidth, |
| 2570 | tok::ObjCKeywordKind Visibility) { |
Fariborz Jahanian | 751c617 | 2008-04-10 23:32:45 +0000 | [diff] [blame] | 2571 | IdentifierInfo *II = D.getIdentifier(); |
| 2572 | Expr *BitWidth = (Expr*)BitfieldWidth; |
| 2573 | SourceLocation Loc = DeclStart; |
| 2574 | if (II) Loc = D.getIdentifierLoc(); |
| 2575 | |
| 2576 | // FIXME: Unnamed fields can be handled in various different ways, for |
| 2577 | // example, unnamed unions inject all members into the struct namespace! |
| 2578 | |
| 2579 | |
| 2580 | if (BitWidth) { |
| 2581 | // TODO: Validate. |
| 2582 | //printf("WARNING: BITFIELDS IGNORED!\n"); |
| 2583 | |
| 2584 | // 6.7.2.1p3 |
| 2585 | // 6.7.2.1p4 |
| 2586 | |
| 2587 | } else { |
| 2588 | // Not a bitfield. |
| 2589 | |
| 2590 | // validate II. |
| 2591 | |
| 2592 | } |
| 2593 | |
| 2594 | QualType T = GetTypeForDeclarator(D, S); |
| 2595 | assert(!T.isNull() && "GetTypeForDeclarator() returned null type"); |
| 2596 | bool InvalidDecl = false; |
| 2597 | |
| 2598 | // C99 6.7.2.1p8: A member of a structure or union may have any type other |
| 2599 | // than a variably modified type. |
| 2600 | if (T->isVariablyModifiedType()) { |
| 2601 | // FIXME: This diagnostic needs work |
| 2602 | Diag(Loc, diag::err_typecheck_illegal_vla, Loc); |
| 2603 | InvalidDecl = true; |
| 2604 | } |
| 2605 | |
Ted Kremenek | 173dd31 | 2008-07-23 18:04:17 +0000 | [diff] [blame] | 2606 | // Get the visibility (access control) for this ivar. |
| 2607 | ObjCIvarDecl::AccessControl ac = |
| 2608 | Visibility != tok::objc_not_keyword ? TranslateIvarVisibility(Visibility) |
| 2609 | : ObjCIvarDecl::None; |
| 2610 | |
| 2611 | // Construct the decl. |
| 2612 | ObjCIvarDecl *NewID = ObjCIvarDecl::Create(Context, Loc, II, T, ac, |
Steve Naroff | d335422 | 2008-07-16 18:22:22 +0000 | [diff] [blame] | 2613 | (Expr *)BitfieldWidth); |
Fariborz Jahanian | 751c617 | 2008-04-10 23:32:45 +0000 | [diff] [blame] | 2614 | |
Ted Kremenek | 173dd31 | 2008-07-23 18:04:17 +0000 | [diff] [blame] | 2615 | // Process attributes attached to the ivar. |
Chris Lattner | 9b384ca | 2008-06-29 00:02:00 +0000 | [diff] [blame] | 2616 | ProcessDeclAttributes(NewID, D); |
Fariborz Jahanian | 751c617 | 2008-04-10 23:32:45 +0000 | [diff] [blame] | 2617 | |
| 2618 | if (D.getInvalidType() || InvalidDecl) |
| 2619 | NewID->setInvalidDecl(); |
Ted Kremenek | 173dd31 | 2008-07-23 18:04:17 +0000 | [diff] [blame] | 2620 | |
Fariborz Jahanian | 751c617 | 2008-04-10 23:32:45 +0000 | [diff] [blame] | 2621 | return NewID; |
| 2622 | } |
| 2623 | |
Fariborz Jahanian | 0c5affb | 2007-09-29 00:54:24 +0000 | [diff] [blame] | 2624 | void Sema::ActOnFields(Scope* S, |
Fariborz Jahanian | cbc36d4 | 2007-10-04 00:45:27 +0000 | [diff] [blame] | 2625 | SourceLocation RecLoc, DeclTy *RecDecl, |
Steve Naroff | 0acc9c9 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 2626 | DeclTy **Fields, unsigned NumFields, |
Daniel Dunbar | f394444 | 2008-10-03 02:03:53 +0000 | [diff] [blame] | 2627 | SourceLocation LBrac, SourceLocation RBrac, |
Daniel Dunbar | 175e639 | 2008-10-03 17:33:35 +0000 | [diff] [blame] | 2628 | AttributeList *Attr) { |
Steve Naroff | 9bb759f | 2007-09-14 22:20:54 +0000 | [diff] [blame] | 2629 | Decl *EnclosingDecl = static_cast<Decl*>(RecDecl); |
| 2630 | assert(EnclosingDecl && "missing record or interface decl"); |
| 2631 | RecordDecl *Record = dyn_cast<RecordDecl>(EnclosingDecl); |
| 2632 | |
Ted Kremenek | 46a837c | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 2633 | if (Record) |
| 2634 | if (RecordDecl* DefRecord = Record->getDefinition(Context)) { |
| 2635 | // Diagnose code like: |
| 2636 | // struct S { struct S {} X; }; |
| 2637 | // We discover this when we complete the outer S. Reject and ignore the |
| 2638 | // outer S. |
| 2639 | Diag(DefRecord->getLocation(), diag::err_nested_redefinition, |
| 2640 | DefRecord->getKindName()); |
| 2641 | Diag(RecLoc, diag::err_previous_definition); |
| 2642 | Record->setInvalidDecl(); |
| 2643 | return; |
| 2644 | } |
| 2645 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2646 | // Verify that all the fields are okay. |
| 2647 | unsigned NumNamedMembers = 0; |
| 2648 | llvm::SmallVector<FieldDecl*, 32> RecFields; |
| 2649 | llvm::SmallSet<const IdentifierInfo*, 32> FieldIDs; |
Steve Naroff | 9bb759f | 2007-09-14 22:20:54 +0000 | [diff] [blame] | 2650 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2651 | for (unsigned i = 0; i != NumFields; ++i) { |
Fariborz Jahanian | 023a439 | 2007-09-14 16:27:55 +0000 | [diff] [blame] | 2652 | |
Steve Naroff | 9bb759f | 2007-09-14 22:20:54 +0000 | [diff] [blame] | 2653 | FieldDecl *FD = cast_or_null<FieldDecl>(static_cast<Decl*>(Fields[i])); |
| 2654 | assert(FD && "missing field decl"); |
| 2655 | |
| 2656 | // Remember all fields. |
| 2657 | RecFields.push_back(FD); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2658 | |
| 2659 | // Get the type for the field. |
Chris Lattner | 36be3d8 | 2007-07-31 21:33:24 +0000 | [diff] [blame] | 2660 | Type *FDTy = FD->getType().getTypePtr(); |
Steve Naroff | ffeaa55 | 2007-09-14 23:09:53 +0000 | [diff] [blame] | 2661 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2662 | // C99 6.7.2.1p2 - A field may not be a function type. |
Chris Lattner | 36be3d8 | 2007-07-31 21:33:24 +0000 | [diff] [blame] | 2663 | if (FDTy->isFunctionType()) { |
Steve Naroff | 9bb759f | 2007-09-14 22:20:54 +0000 | [diff] [blame] | 2664 | Diag(FD->getLocation(), diag::err_field_declared_as_function, |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2665 | FD->getName()); |
Steve Naroff | 9bb759f | 2007-09-14 22:20:54 +0000 | [diff] [blame] | 2666 | FD->setInvalidDecl(); |
| 2667 | EnclosingDecl->setInvalidDecl(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2668 | continue; |
| 2669 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2670 | // C99 6.7.2.1p2 - A field may not be an incomplete type except... |
| 2671 | if (FDTy->isIncompleteType()) { |
Fariborz Jahanian | 023a439 | 2007-09-14 16:27:55 +0000 | [diff] [blame] | 2672 | if (!Record) { // Incomplete ivar type is always an error. |
Fariborz Jahanian | cbc36d4 | 2007-10-04 00:45:27 +0000 | [diff] [blame] | 2673 | Diag(FD->getLocation(), diag::err_field_incomplete, FD->getName()); |
Steve Naroff | 9bb759f | 2007-09-14 22:20:54 +0000 | [diff] [blame] | 2674 | FD->setInvalidDecl(); |
| 2675 | EnclosingDecl->setInvalidDecl(); |
Fariborz Jahanian | cbc36d4 | 2007-10-04 00:45:27 +0000 | [diff] [blame] | 2676 | continue; |
Fariborz Jahanian | 023a439 | 2007-09-14 16:27:55 +0000 | [diff] [blame] | 2677 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2678 | if (i != NumFields-1 || // ... that the last member ... |
Argiris Kirtzidis | c6cc7d5 | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 2679 | !Record->isStruct() || // ... of a structure ... |
Chris Lattner | 36be3d8 | 2007-07-31 21:33:24 +0000 | [diff] [blame] | 2680 | !FDTy->isArrayType()) { //... may have incomplete array type. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2681 | Diag(FD->getLocation(), diag::err_field_incomplete, FD->getName()); |
Steve Naroff | 9bb759f | 2007-09-14 22:20:54 +0000 | [diff] [blame] | 2682 | FD->setInvalidDecl(); |
| 2683 | EnclosingDecl->setInvalidDecl(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2684 | continue; |
| 2685 | } |
Fariborz Jahanian | 023a439 | 2007-09-14 16:27:55 +0000 | [diff] [blame] | 2686 | if (NumNamedMembers < 1) { //... must have more than named member ... |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2687 | Diag(FD->getLocation(), diag::err_flexible_array_empty_struct, |
| 2688 | FD->getName()); |
Steve Naroff | 9bb759f | 2007-09-14 22:20:54 +0000 | [diff] [blame] | 2689 | FD->setInvalidDecl(); |
| 2690 | EnclosingDecl->setInvalidDecl(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2691 | continue; |
| 2692 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2693 | // Okay, we have a legal flexible array member at the end of the struct. |
Fariborz Jahanian | 023a439 | 2007-09-14 16:27:55 +0000 | [diff] [blame] | 2694 | if (Record) |
| 2695 | Record->setHasFlexibleArrayMember(true); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2696 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2697 | /// C99 6.7.2.1p2 - a struct ending in a flexible array member cannot be the |
| 2698 | /// field of another structure or the element of an array. |
Chris Lattner | 36be3d8 | 2007-07-31 21:33:24 +0000 | [diff] [blame] | 2699 | if (const RecordType *FDTTy = FDTy->getAsRecordType()) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2700 | if (FDTTy->getDecl()->hasFlexibleArrayMember()) { |
| 2701 | // If this is a member of a union, then entire union becomes "flexible". |
Argiris Kirtzidis | c6cc7d5 | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 2702 | if (Record && Record->isUnion()) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2703 | Record->setHasFlexibleArrayMember(true); |
| 2704 | } else { |
| 2705 | // If this is a struct/class and this is not the last element, reject |
| 2706 | // it. Note that GCC supports variable sized arrays in the middle of |
| 2707 | // structures. |
| 2708 | if (i != NumFields-1) { |
| 2709 | Diag(FD->getLocation(), diag::err_variable_sized_type_in_struct, |
| 2710 | FD->getName()); |
Steve Naroff | 9bb759f | 2007-09-14 22:20:54 +0000 | [diff] [blame] | 2711 | FD->setInvalidDecl(); |
| 2712 | EnclosingDecl->setInvalidDecl(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2713 | continue; |
| 2714 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2715 | // We support flexible arrays at the end of structs in other structs |
| 2716 | // as an extension. |
| 2717 | Diag(FD->getLocation(), diag::ext_flexible_array_in_struct, |
| 2718 | FD->getName()); |
Fariborz Jahanian | cbc36d4 | 2007-10-04 00:45:27 +0000 | [diff] [blame] | 2719 | if (Record) |
Fariborz Jahanian | 023a439 | 2007-09-14 16:27:55 +0000 | [diff] [blame] | 2720 | Record->setHasFlexibleArrayMember(true); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2721 | } |
| 2722 | } |
| 2723 | } |
Fariborz Jahanian | 550e050 | 2007-10-12 22:10:42 +0000 | [diff] [blame] | 2724 | /// A field cannot be an Objective-c object |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2725 | if (FDTy->isObjCInterfaceType()) { |
Fariborz Jahanian | 550e050 | 2007-10-12 22:10:42 +0000 | [diff] [blame] | 2726 | Diag(FD->getLocation(), diag::err_statically_allocated_object, |
| 2727 | FD->getName()); |
| 2728 | FD->setInvalidDecl(); |
| 2729 | EnclosingDecl->setInvalidDecl(); |
| 2730 | continue; |
| 2731 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2732 | // Keep track of the number of named members. |
| 2733 | if (IdentifierInfo *II = FD->getIdentifier()) { |
| 2734 | // Detect duplicate member names. |
| 2735 | if (!FieldIDs.insert(II)) { |
| 2736 | Diag(FD->getLocation(), diag::err_duplicate_member, II->getName()); |
| 2737 | // Find the previous decl. |
| 2738 | SourceLocation PrevLoc; |
Chris Lattner | 504c543 | 2008-10-12 00:28:42 +0000 | [diff] [blame] | 2739 | for (unsigned i = 0; ; ++i) { |
| 2740 | assert(i != RecFields.size() && "Didn't find previous def!"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2741 | if (RecFields[i]->getIdentifier() == II) { |
| 2742 | PrevLoc = RecFields[i]->getLocation(); |
| 2743 | break; |
| 2744 | } |
| 2745 | } |
| 2746 | Diag(PrevLoc, diag::err_previous_definition); |
Steve Naroff | 9bb759f | 2007-09-14 22:20:54 +0000 | [diff] [blame] | 2747 | FD->setInvalidDecl(); |
| 2748 | EnclosingDecl->setInvalidDecl(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2749 | continue; |
| 2750 | } |
| 2751 | ++NumNamedMembers; |
| 2752 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2753 | } |
| 2754 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2755 | // Okay, we successfully defined 'Record'. |
Chris Lattner | 33aad6e | 2008-02-06 00:51:33 +0000 | [diff] [blame] | 2756 | if (Record) { |
Ted Kremenek | 46a837c | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 2757 | Record->defineBody(Context, &RecFields[0], RecFields.size()); |
Argiris Kirtzidis | 7c210ea | 2008-08-09 00:58:37 +0000 | [diff] [blame] | 2758 | // If this is a C++ record, HandleTagDeclDefinition will be invoked in |
| 2759 | // Sema::ActOnFinishCXXClassDef. |
| 2760 | if (!isa<CXXRecordDecl>(Record)) |
| 2761 | Consumer.HandleTagDeclDefinition(Record); |
Chris Lattner | 33aad6e | 2008-02-06 00:51:33 +0000 | [diff] [blame] | 2762 | } else { |
Chris Lattner | 1100cfb | 2008-02-05 22:40:55 +0000 | [diff] [blame] | 2763 | ObjCIvarDecl **ClsFields = reinterpret_cast<ObjCIvarDecl**>(&RecFields[0]); |
| 2764 | if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(EnclosingDecl)) |
| 2765 | ID->addInstanceVariablesToClass(ClsFields, RecFields.size(), RBrac); |
| 2766 | else if (ObjCImplementationDecl *IMPDecl = |
| 2767 | dyn_cast<ObjCImplementationDecl>(EnclosingDecl)) { |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2768 | assert(IMPDecl && "ActOnFields - missing ObjCImplementationDecl"); |
| 2769 | IMPDecl->ObjCAddInstanceVariablesToClassImpl(ClsFields, RecFields.size()); |
Fariborz Jahanian | 8709373 | 2007-10-31 18:48:14 +0000 | [diff] [blame] | 2770 | CheckImplementationIvars(IMPDecl, ClsFields, RecFields.size(), RBrac); |
Fariborz Jahanian | d34caf9 | 2007-09-26 18:27:25 +0000 | [diff] [blame] | 2771 | } |
Fariborz Jahanian | ebcc9b6 | 2007-09-14 21:08:27 +0000 | [diff] [blame] | 2772 | } |
Daniel Dunbar | 175e639 | 2008-10-03 17:33:35 +0000 | [diff] [blame] | 2773 | |
| 2774 | if (Attr) |
| 2775 | ProcessDeclAttributeList(Record, Attr); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2776 | } |
| 2777 | |
Steve Naroff | 0acc9c9 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 2778 | Sema::DeclTy *Sema::ActOnEnumConstant(Scope *S, DeclTy *theEnumDecl, |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2779 | DeclTy *lastEnumConst, |
| 2780 | SourceLocation IdLoc, IdentifierInfo *Id, |
| 2781 | SourceLocation EqualLoc, ExprTy *val) { |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2782 | EnumDecl *TheEnumDecl = cast<EnumDecl>(static_cast<Decl*>(theEnumDecl)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2783 | EnumConstantDecl *LastEnumConst = |
| 2784 | cast_or_null<EnumConstantDecl>(static_cast<Decl*>(lastEnumConst)); |
| 2785 | Expr *Val = static_cast<Expr*>(val); |
| 2786 | |
Chris Lattner | a754990 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 2787 | // The scope passed in may not be a decl scope. Zip up the scope tree until |
| 2788 | // we find one that is. |
| 2789 | while ((S->getFlags() & Scope::DeclScope) == 0) |
| 2790 | S = S->getParent(); |
| 2791 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2792 | // Verify that there isn't already something declared with this name in this |
| 2793 | // scope. |
Steve Naroff | 6384a01 | 2008-04-02 14:35:35 +0000 | [diff] [blame] | 2794 | if (Decl *PrevDecl = LookupDecl(Id, Decl::IDNS_Ordinary, S)) { |
Argiris Kirtzidis | 4f071ec | 2008-07-16 21:01:53 +0000 | [diff] [blame] | 2795 | // When in C++, we may get a TagDecl with the same name; in this case the |
| 2796 | // enum constant will 'hide' the tag. |
| 2797 | assert((getLangOptions().CPlusPlus || !isa<TagDecl>(PrevDecl)) && |
| 2798 | "Received TagDecl when not in C++!"); |
Argiris Kirtzidis | 90842b6 | 2008-09-09 21:18:04 +0000 | [diff] [blame] | 2799 | if (!isa<TagDecl>(PrevDecl) && isDeclInScope(PrevDecl, CurContext, S)) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2800 | if (isa<EnumConstantDecl>(PrevDecl)) |
| 2801 | Diag(IdLoc, diag::err_redefinition_of_enumerator, Id->getName()); |
| 2802 | else |
| 2803 | Diag(IdLoc, diag::err_redefinition, Id->getName()); |
| 2804 | Diag(PrevDecl->getLocation(), diag::err_previous_definition); |
Chris Lattner | 6ea9bd4 | 2008-02-26 00:33:57 +0000 | [diff] [blame] | 2805 | delete Val; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2806 | return 0; |
| 2807 | } |
| 2808 | } |
| 2809 | |
| 2810 | llvm::APSInt EnumVal(32); |
| 2811 | QualType EltTy; |
| 2812 | if (Val) { |
Chris Lattner | 2cda879 | 2007-08-27 21:16:18 +0000 | [diff] [blame] | 2813 | // Make sure to promote the operand type to int. |
| 2814 | UsualUnaryConversions(Val); |
| 2815 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2816 | // C99 6.7.2.2p2: Make sure we have an integer constant expression. |
| 2817 | SourceLocation ExpLoc; |
| 2818 | if (!Val->isIntegerConstantExpr(EnumVal, Context, &ExpLoc)) { |
| 2819 | Diag(ExpLoc, diag::err_enum_value_not_integer_constant_expr, |
| 2820 | Id->getName()); |
Chris Lattner | 6ea9bd4 | 2008-02-26 00:33:57 +0000 | [diff] [blame] | 2821 | delete Val; |
Chris Lattner | e7f53a4 | 2007-08-27 17:37:24 +0000 | [diff] [blame] | 2822 | Val = 0; // Just forget about it. |
Chris Lattner | 7cea055 | 2007-08-29 16:03:41 +0000 | [diff] [blame] | 2823 | } else { |
| 2824 | EltTy = Val->getType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2825 | } |
Chris Lattner | e7f53a4 | 2007-08-27 17:37:24 +0000 | [diff] [blame] | 2826 | } |
| 2827 | |
| 2828 | if (!Val) { |
| 2829 | if (LastEnumConst) { |
| 2830 | // Assign the last value + 1. |
| 2831 | EnumVal = LastEnumConst->getInitVal(); |
| 2832 | ++EnumVal; |
Chris Lattner | 2cda879 | 2007-08-27 21:16:18 +0000 | [diff] [blame] | 2833 | |
| 2834 | // Check for overflow on increment. |
| 2835 | if (EnumVal < LastEnumConst->getInitVal()) |
| 2836 | Diag(IdLoc, diag::warn_enum_value_overflow); |
| 2837 | |
Chris Lattner | e7f53a4 | 2007-08-27 17:37:24 +0000 | [diff] [blame] | 2838 | EltTy = LastEnumConst->getType(); |
| 2839 | } else { |
| 2840 | // First value, set to zero. |
| 2841 | EltTy = Context.IntTy; |
Chris Lattner | 8cd0e93 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 2842 | EnumVal.zextOrTrunc(static_cast<uint32_t>(Context.getTypeSize(EltTy))); |
Chris Lattner | e7f53a4 | 2007-08-27 17:37:24 +0000 | [diff] [blame] | 2843 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2844 | } |
| 2845 | |
Chris Lattner | e465048 | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 2846 | EnumConstantDecl *New = |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2847 | EnumConstantDecl::Create(Context, TheEnumDecl, IdLoc, Id, EltTy, |
| 2848 | Val, EnumVal, |
Chris Lattner | 58114f0 | 2008-03-15 21:32:50 +0000 | [diff] [blame] | 2849 | LastEnumConst); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2850 | |
| 2851 | // Register this decl in the current scope stack. |
Argiris Kirtzidis | 951f25b | 2008-04-12 00:47:19 +0000 | [diff] [blame] | 2852 | PushOnScopeChains(New, S); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2853 | return New; |
| 2854 | } |
| 2855 | |
Steve Naroff | b0726b8 | 2008-08-07 14:08:16 +0000 | [diff] [blame] | 2856 | // FIXME: For consistency with ActOnFields(), we should have the parser |
| 2857 | // pass in the source location for the left/right braces. |
Steve Naroff | 0acc9c9 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 2858 | void Sema::ActOnEnumBody(SourceLocation EnumLoc, DeclTy *EnumDeclX, |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2859 | DeclTy **Elements, unsigned NumElements) { |
| 2860 | EnumDecl *Enum = cast<EnumDecl>(static_cast<Decl*>(EnumDeclX)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2861 | |
Steve Naroff | b0726b8 | 2008-08-07 14:08:16 +0000 | [diff] [blame] | 2862 | if (Enum && Enum->isDefinition()) { |
| 2863 | // Diagnose code like: |
| 2864 | // enum e0 { |
| 2865 | // E0 = sizeof(enum e0 { E1 }) |
| 2866 | // }; |
| 2867 | Diag(Enum->getLocation(), diag::err_nested_redefinition, |
| 2868 | Enum->getName()); |
| 2869 | Diag(EnumLoc, diag::err_previous_definition); |
| 2870 | Enum->setInvalidDecl(); |
| 2871 | return; |
| 2872 | } |
Chris Lattner | 435c3fd | 2007-08-28 05:10:31 +0000 | [diff] [blame] | 2873 | // TODO: If the result value doesn't fit in an int, it must be a long or long |
| 2874 | // long value. ISO C does not support this, but GCC does as an extension, |
| 2875 | // emit a warning. |
Chris Lattner | 8cd0e93 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 2876 | unsigned IntWidth = Context.Target.getIntWidth(); |
Chris Lattner | 435c3fd | 2007-08-28 05:10:31 +0000 | [diff] [blame] | 2877 | |
Chris Lattner | 206754a | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 2878 | // Verify that all the values are okay, compute the size of the values, and |
| 2879 | // reverse the list. |
| 2880 | unsigned NumNegativeBits = 0; |
| 2881 | unsigned NumPositiveBits = 0; |
| 2882 | |
| 2883 | // Keep track of whether all elements have type int. |
| 2884 | bool AllElementsInt = true; |
| 2885 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2886 | EnumConstantDecl *EltList = 0; |
| 2887 | for (unsigned i = 0; i != NumElements; ++i) { |
| 2888 | EnumConstantDecl *ECD = |
| 2889 | cast_or_null<EnumConstantDecl>(static_cast<Decl*>(Elements[i])); |
| 2890 | if (!ECD) continue; // Already issued a diagnostic. |
Chris Lattner | cb33ae1 | 2007-08-28 05:27:00 +0000 | [diff] [blame] | 2891 | |
| 2892 | // If the enum value doesn't fit in an int, emit an extension warning. |
Chris Lattner | cb33ae1 | 2007-08-28 05:27:00 +0000 | [diff] [blame] | 2893 | const llvm::APSInt &InitVal = ECD->getInitVal(); |
Chris Lattner | 6ea9bd4 | 2008-02-26 00:33:57 +0000 | [diff] [blame] | 2894 | assert(InitVal.getBitWidth() >= IntWidth && |
| 2895 | "Should have promoted value to int"); |
Chris Lattner | cb33ae1 | 2007-08-28 05:27:00 +0000 | [diff] [blame] | 2896 | if (InitVal.getBitWidth() > IntWidth) { |
| 2897 | llvm::APSInt V(InitVal); |
| 2898 | V.trunc(IntWidth); |
| 2899 | V.extend(InitVal.getBitWidth()); |
| 2900 | if (V != InitVal) |
| 2901 | Diag(ECD->getLocation(), diag::ext_enum_value_not_int, |
Chris Lattner | ead053a | 2008-08-17 07:19:51 +0000 | [diff] [blame] | 2902 | InitVal.toString(10)); |
Chris Lattner | cb33ae1 | 2007-08-28 05:27:00 +0000 | [diff] [blame] | 2903 | } |
Chris Lattner | 206754a | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 2904 | |
| 2905 | // Keep track of the size of positive and negative values. |
Chris Lattner | 6ea9bd4 | 2008-02-26 00:33:57 +0000 | [diff] [blame] | 2906 | if (InitVal.isUnsigned() || InitVal.isNonNegative()) |
Chris Lattner | aff63f0 | 2008-01-14 21:47:29 +0000 | [diff] [blame] | 2907 | NumPositiveBits = std::max(NumPositiveBits, |
| 2908 | (unsigned)InitVal.getActiveBits()); |
Chris Lattner | 206754a | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 2909 | else |
Chris Lattner | aff63f0 | 2008-01-14 21:47:29 +0000 | [diff] [blame] | 2910 | NumNegativeBits = std::max(NumNegativeBits, |
| 2911 | (unsigned)InitVal.getMinSignedBits()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2912 | |
Chris Lattner | 206754a | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 2913 | // Keep track of whether every enum element has type int (very commmon). |
| 2914 | if (AllElementsInt) |
| 2915 | AllElementsInt = ECD->getType() == Context.IntTy; |
| 2916 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2917 | ECD->setNextDeclarator(EltList); |
| 2918 | EltList = ECD; |
| 2919 | } |
| 2920 | |
Chris Lattner | 206754a | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 2921 | // Figure out the type that should be used for this enum. |
| 2922 | // FIXME: Support attribute(packed) on enums and -fshort-enums. |
| 2923 | QualType BestType; |
Chris Lattner | ca01d0a | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 2924 | unsigned BestWidth; |
Chris Lattner | 206754a | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 2925 | |
| 2926 | if (NumNegativeBits) { |
| 2927 | // If there is a negative value, figure out the smallest integer type (of |
| 2928 | // int/long/longlong) that fits. |
Chris Lattner | ca01d0a | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 2929 | if (NumNegativeBits <= IntWidth && NumPositiveBits < IntWidth) { |
Chris Lattner | 206754a | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 2930 | BestType = Context.IntTy; |
Chris Lattner | ca01d0a | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 2931 | BestWidth = IntWidth; |
| 2932 | } else { |
Chris Lattner | 8cd0e93 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 2933 | BestWidth = Context.Target.getLongWidth(); |
Ted Kremenek | d7f64cd | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 2934 | |
Chris Lattner | ca01d0a | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 2935 | if (NumNegativeBits <= BestWidth && NumPositiveBits < BestWidth) |
Chris Lattner | 206754a | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 2936 | BestType = Context.LongTy; |
| 2937 | else { |
Chris Lattner | 8cd0e93 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 2938 | BestWidth = Context.Target.getLongLongWidth(); |
Ted Kremenek | d7f64cd | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 2939 | |
Chris Lattner | ca01d0a | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 2940 | if (NumNegativeBits > BestWidth || NumPositiveBits >= BestWidth) |
Chris Lattner | 206754a | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 2941 | Diag(Enum->getLocation(), diag::warn_enum_too_large); |
| 2942 | BestType = Context.LongLongTy; |
| 2943 | } |
| 2944 | } |
| 2945 | } else { |
| 2946 | // If there is no negative value, figure out which of uint, ulong, ulonglong |
| 2947 | // fits. |
Chris Lattner | ca01d0a | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 2948 | if (NumPositiveBits <= IntWidth) { |
Chris Lattner | 206754a | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 2949 | BestType = Context.UnsignedIntTy; |
Chris Lattner | ca01d0a | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 2950 | BestWidth = IntWidth; |
| 2951 | } else if (NumPositiveBits <= |
Chris Lattner | 8cd0e93 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 2952 | (BestWidth = Context.Target.getLongWidth())) { |
Chris Lattner | 206754a | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 2953 | BestType = Context.UnsignedLongTy; |
Chris Lattner | 8cd0e93 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 2954 | } else { |
| 2955 | BestWidth = Context.Target.getLongLongWidth(); |
Chris Lattner | ca01d0a | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 2956 | assert(NumPositiveBits <= BestWidth && |
Chris Lattner | 206754a | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 2957 | "How could an initializer get larger than ULL?"); |
| 2958 | BestType = Context.UnsignedLongLongTy; |
| 2959 | } |
| 2960 | } |
| 2961 | |
Chris Lattner | ca01d0a | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 2962 | // Loop over all of the enumerator constants, changing their types to match |
| 2963 | // the type of the enum if needed. |
| 2964 | for (unsigned i = 0; i != NumElements; ++i) { |
| 2965 | EnumConstantDecl *ECD = |
| 2966 | cast_or_null<EnumConstantDecl>(static_cast<Decl*>(Elements[i])); |
| 2967 | if (!ECD) continue; // Already issued a diagnostic. |
| 2968 | |
| 2969 | // Standard C says the enumerators have int type, but we allow, as an |
| 2970 | // extension, the enumerators to be larger than int size. If each |
| 2971 | // enumerator value fits in an int, type it as an int, otherwise type it the |
| 2972 | // same as the enumerator decl itself. This means that in "enum { X = 1U }" |
| 2973 | // that X has type 'int', not 'unsigned'. |
Chris Lattner | 6ea9bd4 | 2008-02-26 00:33:57 +0000 | [diff] [blame] | 2974 | if (ECD->getType() == Context.IntTy) { |
| 2975 | // Make sure the init value is signed. |
| 2976 | llvm::APSInt IV = ECD->getInitVal(); |
| 2977 | IV.setIsSigned(true); |
| 2978 | ECD->setInitVal(IV); |
Chris Lattner | ca01d0a | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 2979 | continue; // Already int type. |
Chris Lattner | 6ea9bd4 | 2008-02-26 00:33:57 +0000 | [diff] [blame] | 2980 | } |
Chris Lattner | ca01d0a | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 2981 | |
| 2982 | // Determine whether the value fits into an int. |
| 2983 | llvm::APSInt InitVal = ECD->getInitVal(); |
| 2984 | bool FitsInInt; |
| 2985 | if (InitVal.isUnsigned() || !InitVal.isNegative()) |
| 2986 | FitsInInt = InitVal.getActiveBits() < IntWidth; |
| 2987 | else |
| 2988 | FitsInInt = InitVal.getMinSignedBits() <= IntWidth; |
| 2989 | |
| 2990 | // If it fits into an integer type, force it. Otherwise force it to match |
| 2991 | // the enum decl type. |
| 2992 | QualType NewTy; |
| 2993 | unsigned NewWidth; |
| 2994 | bool NewSign; |
| 2995 | if (FitsInInt) { |
| 2996 | NewTy = Context.IntTy; |
| 2997 | NewWidth = IntWidth; |
| 2998 | NewSign = true; |
| 2999 | } else if (ECD->getType() == BestType) { |
| 3000 | // Already the right type! |
| 3001 | continue; |
| 3002 | } else { |
| 3003 | NewTy = BestType; |
| 3004 | NewWidth = BestWidth; |
| 3005 | NewSign = BestType->isSignedIntegerType(); |
| 3006 | } |
| 3007 | |
| 3008 | // Adjust the APSInt value. |
| 3009 | InitVal.extOrTrunc(NewWidth); |
| 3010 | InitVal.setIsSigned(NewSign); |
| 3011 | ECD->setInitVal(InitVal); |
| 3012 | |
| 3013 | // Adjust the Expr initializer and type. |
| 3014 | ECD->setInitExpr(new ImplicitCastExpr(NewTy, ECD->getInitExpr())); |
| 3015 | ECD->setType(NewTy); |
| 3016 | } |
Chris Lattner | 206754a | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 3017 | |
Chris Lattner | 90a018d | 2007-08-28 18:24:31 +0000 | [diff] [blame] | 3018 | Enum->defineElements(EltList, BestType); |
Chris Lattner | 33aad6e | 2008-02-06 00:51:33 +0000 | [diff] [blame] | 3019 | Consumer.HandleTagDeclDefinition(Enum); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3020 | } |
| 3021 | |
Anders Carlsson | 4f7f441 | 2008-02-08 00:33:21 +0000 | [diff] [blame] | 3022 | Sema::DeclTy *Sema::ActOnFileScopeAsmDecl(SourceLocation Loc, |
| 3023 | ExprTy *expr) { |
| 3024 | StringLiteral *AsmString = cast<StringLiteral>((Expr*)expr); |
| 3025 | |
Chris Lattner | 81db64a | 2008-03-16 00:16:02 +0000 | [diff] [blame] | 3026 | return FileScopeAsmDecl::Create(Context, Loc, AsmString); |
Anders Carlsson | 4f7f441 | 2008-02-08 00:33:21 +0000 | [diff] [blame] | 3027 | } |
| 3028 | |
Chris Lattner | 806a5f5 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 3029 | Sema::DeclTy* Sema::ActOnLinkageSpec(SourceLocation Loc, |
Chris Lattner | 43b885f | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 3030 | SourceLocation LBrace, |
| 3031 | SourceLocation RBrace, |
| 3032 | const char *Lang, |
| 3033 | unsigned StrSize, |
| 3034 | DeclTy *D) { |
Chris Lattner | 806a5f5 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 3035 | LinkageSpecDecl::LanguageIDs Language; |
| 3036 | Decl *dcl = static_cast<Decl *>(D); |
| 3037 | if (strncmp(Lang, "\"C\"", StrSize) == 0) |
| 3038 | Language = LinkageSpecDecl::lang_c; |
| 3039 | else if (strncmp(Lang, "\"C++\"", StrSize) == 0) |
| 3040 | Language = LinkageSpecDecl::lang_cxx; |
| 3041 | else { |
| 3042 | Diag(Loc, diag::err_bad_language); |
| 3043 | return 0; |
| 3044 | } |
| 3045 | |
| 3046 | // FIXME: Add all the various semantics of linkage specifications |
Chris Lattner | 81db64a | 2008-03-16 00:16:02 +0000 | [diff] [blame] | 3047 | return LinkageSpecDecl::Create(Context, Loc, Language, dcl); |
Chris Lattner | 806a5f5 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 3048 | } |
Daniel Dunbar | 81c7d47 | 2008-10-14 05:35:18 +0000 | [diff] [blame] | 3049 | |
| 3050 | void Sema::ActOnPragmaPack(PragmaPackKind Kind, IdentifierInfo *Name, |
| 3051 | ExprTy *alignment, SourceLocation PragmaLoc, |
| 3052 | SourceLocation LParenLoc, SourceLocation RParenLoc) { |
| 3053 | Expr *Alignment = static_cast<Expr *>(alignment); |
| 3054 | |
| 3055 | // If specified then alignment must be a "small" power of two. |
| 3056 | unsigned AlignmentVal = 0; |
| 3057 | if (Alignment) { |
| 3058 | llvm::APSInt Val; |
| 3059 | if (!Alignment->isIntegerConstantExpr(Val, Context) || |
| 3060 | !Val.isPowerOf2() || |
| 3061 | Val.getZExtValue() > 16) { |
| 3062 | Diag(PragmaLoc, diag::warn_pragma_pack_invalid_alignment); |
| 3063 | delete Alignment; |
| 3064 | return; // Ignore |
| 3065 | } |
| 3066 | |
| 3067 | AlignmentVal = (unsigned) Val.getZExtValue(); |
| 3068 | } |
| 3069 | |
| 3070 | switch (Kind) { |
| 3071 | case Action::PPK_Default: // pack([n]) |
| 3072 | PackContext.setAlignment(AlignmentVal); |
| 3073 | break; |
| 3074 | |
| 3075 | case Action::PPK_Show: // pack(show) |
| 3076 | // Show the current alignment, making sure to show the right value |
| 3077 | // for the default. |
| 3078 | AlignmentVal = PackContext.getAlignment(); |
| 3079 | // FIXME: This should come from the target. |
| 3080 | if (AlignmentVal == 0) |
| 3081 | AlignmentVal = 8; |
| 3082 | Diag(PragmaLoc, diag::warn_pragma_pack_show, llvm::utostr(AlignmentVal)); |
| 3083 | break; |
| 3084 | |
| 3085 | case Action::PPK_Push: // pack(push [, id] [, [n]) |
| 3086 | PackContext.push(Name); |
| 3087 | // Set the new alignment if specified. |
| 3088 | if (Alignment) |
| 3089 | PackContext.setAlignment(AlignmentVal); |
| 3090 | break; |
| 3091 | |
| 3092 | case Action::PPK_Pop: // pack(pop [, id] [, n]) |
| 3093 | // MSDN, C/C++ Preprocessor Reference > Pragma Directives > pack: |
| 3094 | // "#pragma pack(pop, identifier, n) is undefined" |
| 3095 | if (Alignment && Name) |
| 3096 | Diag(PragmaLoc, diag::warn_pragma_pack_pop_identifer_and_alignment); |
| 3097 | |
| 3098 | // Do the pop. |
| 3099 | if (!PackContext.pop(Name)) { |
| 3100 | // If a name was specified then failure indicates the name |
| 3101 | // wasn't found. Otherwise failure indicates the stack was |
| 3102 | // empty. |
| 3103 | Diag(PragmaLoc, diag::warn_pragma_pack_pop_failed, |
| 3104 | Name ? "no record matching name" : "stack empty"); |
| 3105 | |
| 3106 | // FIXME: Warn about popping named records as MSVC does. |
| 3107 | } else { |
| 3108 | // Pop succeeded, set the new alignment if specified. |
| 3109 | if (Alignment) |
| 3110 | PackContext.setAlignment(AlignmentVal); |
| 3111 | } |
| 3112 | break; |
| 3113 | |
| 3114 | default: |
| 3115 | assert(0 && "Invalid #pragma pack kind."); |
| 3116 | } |
| 3117 | } |
| 3118 | |
| 3119 | bool PragmaPackStack::pop(IdentifierInfo *Name) { |
| 3120 | if (Stack.empty()) |
| 3121 | return false; |
| 3122 | |
| 3123 | // If name is empty just pop top. |
| 3124 | if (!Name) { |
| 3125 | Alignment = Stack.back().first; |
| 3126 | Stack.pop_back(); |
| 3127 | return true; |
| 3128 | } |
| 3129 | |
| 3130 | // Otherwise, find the named record. |
| 3131 | for (unsigned i = Stack.size(); i != 0; ) { |
| 3132 | --i; |
| 3133 | if (strcmp(Stack[i].second.c_str(), Name->getName()) == 0) { |
| 3134 | // Found it, pop up to and including this record. |
| 3135 | Alignment = Stack[i].first; |
| 3136 | Stack.erase(Stack.begin() + i, Stack.end()); |
| 3137 | return true; |
| 3138 | } |
| 3139 | } |
| 3140 | |
| 3141 | return false; |
| 3142 | } |