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