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