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