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