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" |
Chris Lattner | 20c6b3b | 2009-01-27 18:30:58 +0000 | [diff] [blame] | 21 | #include "clang/Basic/DiagnosticSema.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" |
Douglas Gregor | e267ff3 | 2008-12-11 20:41:00 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/STLExtras.h" |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 29 | #include <algorithm> |
| 30 | #include <functional> |
Douglas Gregor | e267ff3 | 2008-12-11 20:41:00 +0000 | [diff] [blame] | 31 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 32 | using namespace clang; |
| 33 | |
Steve Naroff | b43a50f | 2009-01-28 19:39:02 +0000 | [diff] [blame] | 34 | Sema::TypeTy *Sema::getTypeName(IdentifierInfo &II, Scope *S, |
| 35 | const CXXScopeSpec *SS) { |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 36 | DeclContext *DC = 0; |
| 37 | if (SS) { |
| 38 | if (SS->isInvalid()) |
| 39 | return 0; |
| 40 | DC = static_cast<DeclContext*>(SS->getScopeRep()); |
| 41 | } |
Steve Naroff | 939837f | 2009-01-28 15:51:12 +0000 | [diff] [blame] | 42 | LookupResult Result = LookupDecl(&II, Decl::IDNS_Ordinary, S, DC); |
Steve Naroff | b327ce0 | 2008-04-02 14:35:35 +0000 | [diff] [blame] | 43 | |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 44 | Decl *IIDecl = 0; |
| 45 | switch (Result.getKind()) { |
| 46 | case LookupResult::NotFound: |
| 47 | case LookupResult::FoundOverloaded: |
| 48 | case LookupResult::AmbiguousBaseSubobjectTypes: |
| 49 | case LookupResult::AmbiguousBaseSubobjects: |
| 50 | // FIXME: In the event of an ambiguous lookup, we could visit all of |
| 51 | // the entities found to determine whether they are all types. This |
| 52 | // might provide better diagnostics. |
| 53 | return 0; |
| 54 | |
| 55 | case LookupResult::Found: |
| 56 | IIDecl = Result.getAsDecl(); |
| 57 | break; |
| 58 | } |
| 59 | |
| 60 | if (isa<TypedefDecl>(IIDecl) || |
| 61 | isa<ObjCInterfaceDecl>(IIDecl) || |
| 62 | isa<TagDecl>(IIDecl) || |
| 63 | isa<TemplateTypeParmDecl>(IIDecl)) |
Fariborz Jahanian | bece4ac | 2007-10-12 16:34:10 +0000 | [diff] [blame] | 64 | return IIDecl; |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 65 | return 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 68 | DeclContext *Sema::getContainingDC(DeclContext *DC) { |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 69 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(DC)) { |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 70 | // 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] | 71 | if (MD->isOutOfLineDefinition()) |
| 72 | return MD->getLexicalDeclContext(); |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 73 | |
| 74 | // A C++ inline method is parsed *after* the topmost class it was declared in |
| 75 | // is fully parsed (it's "complete"). |
| 76 | // 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] | 77 | // the topmost (non-nested) class it is lexically declared in. |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 78 | assert(isa<CXXRecordDecl>(MD->getParent()) && "C++ method not in Record."); |
| 79 | DC = MD->getParent(); |
Argyrios Kyrtzidis | 77407b8 | 2008-11-19 18:01:13 +0000 | [diff] [blame] | 80 | while (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(DC->getLexicalParent())) |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 81 | DC = RD; |
| 82 | |
| 83 | // Return the declaration context of the topmost class the inline method is |
| 84 | // declared in. |
| 85 | return DC; |
| 86 | } |
| 87 | |
Argyrios Kyrtzidis | 5239304 | 2008-11-09 23:41:00 +0000 | [diff] [blame] | 88 | if (isa<ObjCMethodDecl>(DC)) |
| 89 | return Context.getTranslationUnitDecl(); |
| 90 | |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 91 | if (Decl *D = dyn_cast<Decl>(DC)) |
| 92 | return D->getLexicalDeclContext(); |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 93 | |
Argyrios Kyrtzidis | 77407b8 | 2008-11-19 18:01:13 +0000 | [diff] [blame] | 94 | return DC->getLexicalParent(); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 97 | void Sema::PushDeclContext(Scope *S, DeclContext *DC) { |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 98 | assert(getContainingDC(DC) == CurContext && |
Zhongxing Xu | e50897a | 2008-12-08 07:14:51 +0000 | [diff] [blame] | 99 | "The next DeclContext should be lexically contained in the current one."); |
Chris Lattner | 9fdf9c6 | 2008-04-22 18:39:57 +0000 | [diff] [blame] | 100 | CurContext = DC; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 101 | S->setEntity(DC); |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 104 | void Sema::PopDeclContext() { |
| 105 | assert(CurContext && "DeclContext imbalance!"); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 106 | |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 107 | CurContext = getContainingDC(CurContext); |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 108 | } |
| 109 | |
Argyrios Kyrtzidis | 87f3ff0 | 2008-04-12 00:47:19 +0000 | [diff] [blame] | 110 | /// Add this decl to the scope shadowed decl chains. |
| 111 | void Sema::PushOnScopeChains(NamedDecl *D, Scope *S) { |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 112 | // Move up the scope chain until we find the nearest enclosing |
| 113 | // non-transparent context. The declaration will be introduced into this |
| 114 | // scope. |
| 115 | while (S->getEntity() && |
| 116 | ((DeclContext *)S->getEntity())->isTransparentContext()) |
| 117 | S = S->getParent(); |
| 118 | |
Argyrios Kyrtzidis | 87f3ff0 | 2008-04-12 00:47:19 +0000 | [diff] [blame] | 119 | S->AddDecl(D); |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 120 | |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 121 | // Add scoped declarations into their context, so that they can be |
| 122 | // found later. Declarations without a context won't be inserted |
| 123 | // into any context. |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 124 | CurContext->addDecl(D); |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 125 | |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 126 | // C++ [basic.scope]p4: |
| 127 | // -- exactly one declaration shall declare a class name or |
| 128 | // enumeration name that is not a typedef name and the other |
| 129 | // declarations shall all refer to the same object or |
| 130 | // enumerator, or all refer to functions and function templates; |
| 131 | // in this case the class name or enumeration name is hidden. |
| 132 | if (TagDecl *TD = dyn_cast<TagDecl>(D)) { |
| 133 | // We are pushing the name of a tag (enum or class). |
Douglas Gregor | e21b994 | 2009-01-07 16:34:42 +0000 | [diff] [blame] | 134 | if (CurContext->getLookupContext() |
| 135 | == TD->getDeclContext()->getLookupContext()) { |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 136 | // We're pushing the tag into the current context, which might |
| 137 | // require some reshuffling in the identifier resolver. |
| 138 | IdentifierResolver::iterator |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 139 | I = IdResolver.begin(TD->getDeclName(), CurContext, |
| 140 | false/*LookInParentCtx*/), |
| 141 | IEnd = IdResolver.end(); |
| 142 | if (I != IEnd && isDeclInScope(*I, CurContext, S)) { |
| 143 | NamedDecl *PrevDecl = *I; |
| 144 | for (; I != IEnd && isDeclInScope(*I, CurContext, S); |
| 145 | PrevDecl = *I, ++I) { |
| 146 | if (TD->declarationReplaces(*I)) { |
| 147 | // This is a redeclaration. Remove it from the chain and |
| 148 | // break out, so that we'll add in the shadowed |
| 149 | // declaration. |
| 150 | S->RemoveDecl(*I); |
| 151 | if (PrevDecl == *I) { |
| 152 | IdResolver.RemoveDecl(*I); |
| 153 | IdResolver.AddDecl(TD); |
| 154 | return; |
| 155 | } else { |
| 156 | IdResolver.RemoveDecl(*I); |
| 157 | break; |
| 158 | } |
| 159 | } |
| 160 | } |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 161 | |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 162 | // There is already a declaration with the same name in the same |
| 163 | // scope, which is not a tag declaration. It must be found |
| 164 | // before we find the new declaration, so insert the new |
| 165 | // declaration at the end of the chain. |
| 166 | IdResolver.AddShadowedDecl(TD, PrevDecl); |
| 167 | |
| 168 | return; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 169 | } |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 170 | } |
Argyrios Kyrtzidis | f1af6a7 | 2008-10-22 23:08:24 +0000 | [diff] [blame] | 171 | } else if (getLangOptions().CPlusPlus && isa<FunctionDecl>(D)) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 172 | // We are pushing the name of a function, which might be an |
| 173 | // overloaded name. |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 174 | FunctionDecl *FD = cast<FunctionDecl>(D); |
Douglas Gregor | ce35607 | 2009-01-06 23:51:29 +0000 | [diff] [blame] | 175 | DeclContext *DC = FD->getDeclContext()->getLookupContext(); |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 176 | IdentifierResolver::iterator Redecl |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 177 | = std::find_if(IdResolver.begin(FD->getDeclName(), DC, |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 178 | false/*LookInParentCtx*/), |
| 179 | IdResolver.end(), |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 180 | std::bind1st(std::mem_fun(&NamedDecl::declarationReplaces), |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 181 | FD)); |
| 182 | if (Redecl != IdResolver.end()) { |
| 183 | // There is already a declaration of a function on our |
| 184 | // IdResolver chain. Replace it with this declaration. |
| 185 | S->RemoveDecl(*Redecl); |
| 186 | IdResolver.RemoveDecl(*Redecl); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 187 | } |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 188 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 189 | |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 190 | IdResolver.AddDecl(D); |
Argyrios Kyrtzidis | 87f3ff0 | 2008-04-12 00:47:19 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Steve Naroff | b216c88 | 2007-10-09 22:01:59 +0000 | [diff] [blame] | 193 | void Sema::ActOnPopScope(SourceLocation Loc, Scope *S) { |
Chris Lattner | 31e0572 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 194 | if (S->decl_empty()) return; |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 195 | assert((S->getFlags() & (Scope::DeclScope | Scope::TemplateParamScope)) && |
| 196 | "Scope shouldn't contain decls!"); |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 197 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 198 | for (Scope::decl_iterator I = S->decl_begin(), E = S->decl_end(); |
| 199 | I != E; ++I) { |
Steve Naroff | c752d04 | 2007-09-13 18:10:37 +0000 | [diff] [blame] | 200 | Decl *TmpD = static_cast<Decl*>(*I); |
| 201 | assert(TmpD && "This decl didn't get pushed??"); |
Argyrios Kyrtzidis | 7643536 | 2008-06-10 01:32:09 +0000 | [diff] [blame] | 202 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 203 | assert(isa<NamedDecl>(TmpD) && "Decl isn't NamedDecl?"); |
| 204 | NamedDecl *D = cast<NamedDecl>(TmpD); |
Argyrios Kyrtzidis | 7643536 | 2008-06-10 01:32:09 +0000 | [diff] [blame] | 205 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 206 | if (!D->getDeclName()) continue; |
Chris Lattner | 7f925cc | 2008-04-11 07:00:53 +0000 | [diff] [blame] | 207 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 208 | // Remove this name from our lexical scope. |
| 209 | IdResolver.RemoveDecl(D); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 210 | } |
| 211 | } |
| 212 | |
Steve Naroff | e8043c3 | 2008-04-01 23:04:06 +0000 | [diff] [blame] | 213 | /// getObjCInterfaceDecl - Look up a for a class declaration in the scope. |
| 214 | /// return 0 if one not found. |
Steve Naroff | e8043c3 | 2008-04-01 23:04:06 +0000 | [diff] [blame] | 215 | ObjCInterfaceDecl *Sema::getObjCInterfaceDecl(IdentifierInfo *Id) { |
Steve Naroff | 3110251 | 2008-04-02 18:30:49 +0000 | [diff] [blame] | 216 | // The third "scope" argument is 0 since we aren't enabling lazy built-in |
| 217 | // creation from this context. |
Steve Naroff | 133147d | 2009-01-28 16:09:22 +0000 | [diff] [blame] | 218 | Decl *IDecl = LookupDecl(Id, Decl::IDNS_Ordinary, 0); |
Fariborz Jahanian | 4cabdfc | 2007-10-12 19:38:20 +0000 | [diff] [blame] | 219 | |
Steve Naroff | b327ce0 | 2008-04-02 14:35:35 +0000 | [diff] [blame] | 220 | return dyn_cast_or_null<ObjCInterfaceDecl>(IDecl); |
Fariborz Jahanian | 4cabdfc | 2007-10-12 19:38:20 +0000 | [diff] [blame] | 221 | } |
| 222 | |
Douglas Gregor | 1a0d31a | 2009-01-12 18:45:55 +0000 | [diff] [blame] | 223 | /// getNonFieldDeclScope - Retrieves the innermost scope, starting |
| 224 | /// from S, where a non-field would be declared. This routine copes |
| 225 | /// with the difference between C and C++ scoping rules in structs and |
| 226 | /// unions. For example, the following code is well-formed in C but |
| 227 | /// ill-formed in C++: |
| 228 | /// @code |
| 229 | /// struct S6 { |
| 230 | /// enum { BAR } e; |
| 231 | /// }; |
| 232 | /// |
| 233 | /// void test_S6() { |
| 234 | /// struct S6 a; |
| 235 | /// a.e = BAR; |
| 236 | /// } |
| 237 | /// @endcode |
| 238 | /// For the declaration of BAR, this routine will return a different |
| 239 | /// scope. The scope S will be the scope of the unnamed enumeration |
| 240 | /// within S6. In C++, this routine will return the scope associated |
| 241 | /// with S6, because the enumeration's scope is a transparent |
| 242 | /// context but structures can contain non-field names. In C, this |
| 243 | /// routine will return the translation unit scope, since the |
| 244 | /// enumeration's scope is a transparent context and structures cannot |
| 245 | /// contain non-field names. |
| 246 | Scope *Sema::getNonFieldDeclScope(Scope *S) { |
| 247 | while (((S->getFlags() & Scope::DeclScope) == 0) || |
| 248 | (S->getEntity() && |
| 249 | ((DeclContext *)S->getEntity())->isTransparentContext()) || |
| 250 | (S->isClassScope() && !getLangOptions().CPlusPlus)) |
| 251 | S = S->getParent(); |
| 252 | return S; |
| 253 | } |
| 254 | |
Steve Naroff | e8043c3 | 2008-04-01 23:04:06 +0000 | [diff] [blame] | 255 | /// LookupDecl - Look up the inner-most declaration in the specified |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 256 | /// namespace. NamespaceNameOnly - during lookup only namespace names |
| 257 | /// are considered as required in C++ [basic.lookup.udir] 3.4.6.p1 |
| 258 | /// 'When looking up a namespace-name in a using-directive or |
| 259 | /// namespace-alias-definition, only namespace names are considered.' |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 260 | /// |
| 261 | /// Note: The use of this routine is deprecated. Please use |
| 262 | /// LookupName, LookupQualifiedName, or LookupParsedName instead. |
| 263 | Sema::LookupResult |
| 264 | Sema::LookupDecl(DeclarationName Name, unsigned NSI, Scope *S, |
| 265 | const DeclContext *LookupCtx, |
Steve Naroff | 133147d | 2009-01-28 16:09:22 +0000 | [diff] [blame] | 266 | bool LookInParent) { |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 267 | LookupCriteria::NameKind Kind; |
| 268 | if (NSI == Decl::IDNS_Ordinary) { |
Steve Naroff | 133147d | 2009-01-28 16:09:22 +0000 | [diff] [blame] | 269 | Kind = LookupCriteria::Ordinary; |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 270 | } else if (NSI == Decl::IDNS_Tag) |
| 271 | Kind = LookupCriteria::Tag; |
Chris Lattner | 95d58f3 | 2009-01-16 19:44:00 +0000 | [diff] [blame] | 272 | else { |
| 273 | assert(NSI == Decl::IDNS_Member &&"Unable to grok LookupDecl NSI argument"); |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 274 | Kind = LookupCriteria::Member; |
Chris Lattner | 95d58f3 | 2009-01-16 19:44:00 +0000 | [diff] [blame] | 275 | } |
| 276 | |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 277 | if (LookupCtx) |
| 278 | return LookupQualifiedName(const_cast<DeclContext *>(LookupCtx), Name, |
| 279 | LookupCriteria(Kind, !LookInParent, |
| 280 | getLangOptions().CPlusPlus)); |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 281 | |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 282 | // Unqualified lookup |
| 283 | return LookupName(S, Name, |
| 284 | LookupCriteria(Kind, !LookInParent, |
| 285 | getLangOptions().CPlusPlus)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 286 | } |
| 287 | |
Chris Lattner | 95e2c71 | 2008-05-05 22:18:14 +0000 | [diff] [blame] | 288 | void Sema::InitBuiltinVaListType() { |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 289 | if (!Context.getBuiltinVaListType().isNull()) |
| 290 | return; |
| 291 | |
| 292 | IdentifierInfo *VaIdent = &Context.Idents.get("__builtin_va_list"); |
Steve Naroff | b327ce0 | 2008-04-02 14:35:35 +0000 | [diff] [blame] | 293 | Decl *VaDecl = LookupDecl(VaIdent, Decl::IDNS_Ordinary, TUScope); |
Steve Naroff | 733002f | 2007-10-18 22:17:45 +0000 | [diff] [blame] | 294 | TypedefDecl *VaTypedef = cast<TypedefDecl>(VaDecl); |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 295 | Context.setBuiltinVaListType(Context.getTypedefType(VaTypedef)); |
| 296 | } |
| 297 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 298 | /// LazilyCreateBuiltin - The specified Builtin-ID was first used at file scope. |
| 299 | /// lazily create a decl for it. |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 300 | NamedDecl *Sema::LazilyCreateBuiltin(IdentifierInfo *II, unsigned bid, |
| 301 | Scope *S) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 302 | Builtin::ID BID = (Builtin::ID)bid; |
| 303 | |
Chris Lattner | bd7eb1c | 2008-09-28 05:54:29 +0000 | [diff] [blame] | 304 | if (Context.BuiltinInfo.hasVAListUse(BID)) |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 305 | InitBuiltinVaListType(); |
| 306 | |
Anders Carlsson | b2cf357 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 307 | QualType R = Context.BuiltinInfo.GetBuiltinType(BID, Context); |
Argyrios Kyrtzidis | ff898cd | 2008-04-17 14:47:13 +0000 | [diff] [blame] | 308 | FunctionDecl *New = FunctionDecl::Create(Context, |
| 309 | Context.getTranslationUnitDecl(), |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 310 | SourceLocation(), II, R, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 311 | FunctionDecl::Extern, false); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 312 | |
Chris Lattner | 95e2c71 | 2008-05-05 22:18:14 +0000 | [diff] [blame] | 313 | // Create Decl objects for each parameter, adding them to the |
| 314 | // FunctionDecl. |
| 315 | if (FunctionTypeProto *FT = dyn_cast<FunctionTypeProto>(R)) { |
| 316 | llvm::SmallVector<ParmVarDecl*, 16> Params; |
| 317 | for (unsigned i = 0, e = FT->getNumArgs(); i != e; ++i) |
| 318 | Params.push_back(ParmVarDecl::Create(Context, New, SourceLocation(), 0, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 319 | FT->getArgType(i), VarDecl::None, 0)); |
Ted Kremenek | fc76761 | 2009-01-14 00:42:25 +0000 | [diff] [blame] | 320 | New->setParams(Context, &Params[0], Params.size()); |
Chris Lattner | 95e2c71 | 2008-05-05 22:18:14 +0000 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | |
| 324 | |
Chris Lattner | 7f925cc | 2008-04-11 07:00:53 +0000 | [diff] [blame] | 325 | // TUScope is the translation-unit scope to insert this function into. |
Douglas Gregor | a8cc8ce | 2009-01-09 18:51:29 +0000 | [diff] [blame] | 326 | // FIXME: This is hideous. We need to teach PushOnScopeChains to |
| 327 | // relate Scopes to DeclContexts, and probably eliminate CurContext |
| 328 | // entirely, but we're not there yet. |
| 329 | DeclContext *SavedContext = CurContext; |
| 330 | CurContext = Context.getTranslationUnitDecl(); |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 331 | PushOnScopeChains(New, TUScope); |
Douglas Gregor | a8cc8ce | 2009-01-09 18:51:29 +0000 | [diff] [blame] | 332 | CurContext = SavedContext; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 333 | return New; |
| 334 | } |
| 335 | |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 336 | /// GetStdNamespace - This method gets the C++ "std" namespace. This is where |
| 337 | /// everything from the standard library is defined. |
| 338 | NamespaceDecl *Sema::GetStdNamespace() { |
| 339 | if (!StdNamespace) { |
Chris Lattner | 8edea83 | 2008-11-20 05:45:14 +0000 | [diff] [blame] | 340 | IdentifierInfo *StdIdent = &PP.getIdentifierTable().get("std"); |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 341 | DeclContext *Global = Context.getTranslationUnitDecl(); |
Steve Naroff | 939837f | 2009-01-28 15:51:12 +0000 | [diff] [blame] | 342 | Decl *Std = LookupDecl(StdIdent, Decl::IDNS_Ordinary, 0, Global); |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 343 | StdNamespace = dyn_cast_or_null<NamespaceDecl>(Std); |
| 344 | } |
| 345 | return StdNamespace; |
| 346 | } |
| 347 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 348 | /// MergeTypeDefDecl - We just parsed a typedef 'New' which has the same name |
| 349 | /// and scope as a previous declaration 'Old'. Figure out how to resolve this |
| 350 | /// situation, merging decls or emitting diagnostics as appropriate. |
| 351 | /// |
Steve Naroff | e8043c3 | 2008-04-01 23:04:06 +0000 | [diff] [blame] | 352 | TypedefDecl *Sema::MergeTypeDefDecl(TypedefDecl *New, Decl *OldD) { |
Fariborz Jahanian | c55a240 | 2009-01-16 19:58:32 +0000 | [diff] [blame] | 353 | bool objc_types = false; |
Steve Naroff | 2b255c4 | 2008-09-09 14:32:20 +0000 | [diff] [blame] | 354 | // Allow multiple definitions for ObjC built-in typedefs. |
| 355 | // FIXME: Verify the underlying types are equivalent! |
| 356 | if (getLangOptions().ObjC1) { |
Chris Lattner | 2bac0f6 | 2008-11-20 05:41:43 +0000 | [diff] [blame] | 357 | const IdentifierInfo *TypeID = New->getIdentifier(); |
| 358 | switch (TypeID->getLength()) { |
| 359 | default: break; |
| 360 | case 2: |
| 361 | if (!TypeID->isStr("id")) |
| 362 | break; |
Steve Naroff | 2b255c4 | 2008-09-09 14:32:20 +0000 | [diff] [blame] | 363 | Context.setObjCIdType(New); |
Fariborz Jahanian | c55a240 | 2009-01-16 19:58:32 +0000 | [diff] [blame] | 364 | objc_types = true; |
| 365 | break; |
Chris Lattner | 2bac0f6 | 2008-11-20 05:41:43 +0000 | [diff] [blame] | 366 | case 5: |
| 367 | if (!TypeID->isStr("Class")) |
| 368 | break; |
Steve Naroff | 2b255c4 | 2008-09-09 14:32:20 +0000 | [diff] [blame] | 369 | Context.setObjCClassType(New); |
Fariborz Jahanian | c55a240 | 2009-01-16 19:58:32 +0000 | [diff] [blame] | 370 | objc_types = true; |
Steve Naroff | 2b255c4 | 2008-09-09 14:32:20 +0000 | [diff] [blame] | 371 | return New; |
Chris Lattner | 2bac0f6 | 2008-11-20 05:41:43 +0000 | [diff] [blame] | 372 | case 3: |
| 373 | if (!TypeID->isStr("SEL")) |
| 374 | break; |
Steve Naroff | 2b255c4 | 2008-09-09 14:32:20 +0000 | [diff] [blame] | 375 | Context.setObjCSelType(New); |
Fariborz Jahanian | c55a240 | 2009-01-16 19:58:32 +0000 | [diff] [blame] | 376 | objc_types = true; |
Steve Naroff | 2b255c4 | 2008-09-09 14:32:20 +0000 | [diff] [blame] | 377 | return New; |
Chris Lattner | 2bac0f6 | 2008-11-20 05:41:43 +0000 | [diff] [blame] | 378 | case 8: |
| 379 | if (!TypeID->isStr("Protocol")) |
| 380 | break; |
Steve Naroff | 2b255c4 | 2008-09-09 14:32:20 +0000 | [diff] [blame] | 381 | Context.setObjCProtoType(New->getUnderlyingType()); |
Fariborz Jahanian | c55a240 | 2009-01-16 19:58:32 +0000 | [diff] [blame] | 382 | objc_types = true; |
Steve Naroff | 2b255c4 | 2008-09-09 14:32:20 +0000 | [diff] [blame] | 383 | return New; |
| 384 | } |
| 385 | // Fall through - the typedef name was not a builtin type. |
| 386 | } |
Douglas Gregor | 6697312 | 2009-01-28 17:15:10 +0000 | [diff] [blame] | 387 | // Verify the old decl was also a type. |
| 388 | TypeDecl *Old = dyn_cast<TypeDecl>(OldD); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 389 | if (!Old) { |
Douglas Gregor | 6697312 | 2009-01-28 17:15:10 +0000 | [diff] [blame] | 390 | Diag(New->getLocation(), diag::err_redefinition_different_kind) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 391 | << New->getDeclName(); |
Fariborz Jahanian | c55a240 | 2009-01-16 19:58:32 +0000 | [diff] [blame] | 392 | if (!objc_types) |
| 393 | Diag(OldD->getLocation(), diag::note_previous_definition); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 394 | return New; |
| 395 | } |
Douglas Gregor | 6697312 | 2009-01-28 17:15:10 +0000 | [diff] [blame] | 396 | |
| 397 | // Determine the "old" type we'll use for checking and diagnostics. |
| 398 | QualType OldType; |
| 399 | if (TypedefDecl *OldTypedef = dyn_cast<TypedefDecl>(Old)) |
| 400 | OldType = OldTypedef->getUnderlyingType(); |
| 401 | else |
| 402 | OldType = Context.getTypeDeclType(Old); |
| 403 | |
Chris Lattner | 99cb997 | 2008-07-25 18:44:27 +0000 | [diff] [blame] | 404 | // If the typedef types are not identical, reject them in all languages and |
| 405 | // with any extensions enabled. |
Douglas Gregor | 6697312 | 2009-01-28 17:15:10 +0000 | [diff] [blame] | 406 | |
| 407 | if (OldType != New->getUnderlyingType() && |
| 408 | Context.getCanonicalType(OldType) != |
Chris Lattner | 99cb997 | 2008-07-25 18:44:27 +0000 | [diff] [blame] | 409 | Context.getCanonicalType(New->getUnderlyingType())) { |
Chris Lattner | 5dc266a | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 410 | Diag(New->getLocation(), diag::err_redefinition_different_typedef) |
Douglas Gregor | 6697312 | 2009-01-28 17:15:10 +0000 | [diff] [blame] | 411 | << New->getUnderlyingType() << OldType; |
Fariborz Jahanian | c55a240 | 2009-01-16 19:58:32 +0000 | [diff] [blame] | 412 | if (!objc_types) |
| 413 | Diag(Old->getLocation(), diag::note_previous_definition); |
Douglas Gregor | 6037fcb | 2009-01-09 19:42:16 +0000 | [diff] [blame] | 414 | return New; |
Chris Lattner | 99cb997 | 2008-07-25 18:44:27 +0000 | [diff] [blame] | 415 | } |
Fariborz Jahanian | c55a240 | 2009-01-16 19:58:32 +0000 | [diff] [blame] | 416 | if (objc_types) return New; |
Eli Friedman | 54ecfce | 2008-06-11 06:20:39 +0000 | [diff] [blame] | 417 | if (getLangOptions().Microsoft) return New; |
| 418 | |
Douglas Gregor | bbe2743 | 2008-11-21 16:29:06 +0000 | [diff] [blame] | 419 | // C++ [dcl.typedef]p2: |
| 420 | // In a given non-class scope, a typedef specifier can be used to |
| 421 | // redefine the name of any type declared in that scope to refer |
| 422 | // to the type to which it already refers. |
| 423 | if (getLangOptions().CPlusPlus && !isa<CXXRecordDecl>(CurContext)) |
| 424 | return New; |
| 425 | |
| 426 | // 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] | 427 | // Apparently GCC, Intel, and Sun all silently ignore the redeclaration if |
| 428 | // *either* declaration is in a system header. The code below implements |
| 429 | // this adhoc compatibility rule. FIXME: The following code will not |
| 430 | // work properly when compiling ".i" files (containing preprocessed output). |
Daniel Dunbar | 2fe0997 | 2008-09-12 18:10:20 +0000 | [diff] [blame] | 431 | if (PP.getDiagnostics().getSuppressSystemWarnings()) { |
| 432 | SourceManager &SrcMgr = Context.getSourceManager(); |
| 433 | if (SrcMgr.isInSystemHeader(Old->getLocation())) |
| 434 | return New; |
| 435 | if (SrcMgr.isInSystemHeader(New->getLocation())) |
| 436 | return New; |
| 437 | } |
Eli Friedman | 54ecfce | 2008-06-11 06:20:39 +0000 | [diff] [blame] | 438 | |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 439 | Diag(New->getLocation(), diag::err_redefinition) << New->getDeclName(); |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 440 | Diag(Old->getLocation(), diag::note_previous_definition); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 441 | return New; |
| 442 | } |
| 443 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 444 | /// DeclhasAttr - returns true if decl Declaration already has the target |
| 445 | /// attribute. |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 446 | static bool DeclHasAttr(const Decl *decl, const Attr *target) { |
| 447 | for (const Attr *attr = decl->getAttrs(); attr; attr = attr->getNext()) |
| 448 | if (attr->getKind() == target->getKind()) |
| 449 | return true; |
| 450 | |
| 451 | return false; |
| 452 | } |
| 453 | |
| 454 | /// MergeAttributes - append attributes from the Old decl to the New one. |
| 455 | static void MergeAttributes(Decl *New, Decl *Old) { |
| 456 | Attr *attr = const_cast<Attr*>(Old->getAttrs()), *tmp; |
| 457 | |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 458 | while (attr) { |
| 459 | tmp = attr; |
| 460 | attr = attr->getNext(); |
| 461 | |
| 462 | if (!DeclHasAttr(New, tmp)) { |
Anton Korobeynikov | 2f40270 | 2008-12-26 00:52:02 +0000 | [diff] [blame] | 463 | tmp->setInherited(true); |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 464 | New->addAttr(tmp); |
| 465 | } else { |
| 466 | tmp->setNext(0); |
| 467 | delete(tmp); |
| 468 | } |
| 469 | } |
Nuno Lopes | 9141bee | 2008-06-01 22:53:53 +0000 | [diff] [blame] | 470 | |
| 471 | Old->invalidateAttrs(); |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 472 | } |
| 473 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 474 | /// MergeFunctionDecl - We just parsed a function 'New' from |
| 475 | /// declarator D which has the same name and scope as a previous |
| 476 | /// declaration 'Old'. Figure out how to resolve this situation, |
| 477 | /// merging decls or emitting diagnostics as appropriate. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 478 | /// Redeclaration will be set true if this New is a redeclaration OldD. |
| 479 | /// |
| 480 | /// In C++, New and Old must be declarations that are not |
| 481 | /// overloaded. Use IsOverload to determine whether New and Old are |
| 482 | /// overloaded, and to select the Old declaration that New should be |
| 483 | /// merged with. |
Douglas Gregor | f009795 | 2008-04-21 02:02:58 +0000 | [diff] [blame] | 484 | FunctionDecl * |
| 485 | Sema::MergeFunctionDecl(FunctionDecl *New, Decl *OldD, bool &Redeclaration) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 486 | assert(!isa<OverloadedFunctionDecl>(OldD) && |
| 487 | "Cannot merge with an overloaded function declaration"); |
| 488 | |
Douglas Gregor | f009795 | 2008-04-21 02:02:58 +0000 | [diff] [blame] | 489 | Redeclaration = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 490 | // Verify the old decl was also a function. |
| 491 | FunctionDecl *Old = dyn_cast<FunctionDecl>(OldD); |
| 492 | if (!Old) { |
Chris Lattner | 5dc266a | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 493 | Diag(New->getLocation(), diag::err_redefinition_different_kind) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 494 | << New->getDeclName(); |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 495 | Diag(OldD->getLocation(), diag::note_previous_definition); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 496 | return New; |
| 497 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 498 | |
| 499 | // Determine whether the previous declaration was a definition, |
| 500 | // implicit declaration, or a declaration. |
| 501 | diag::kind PrevDiag; |
| 502 | if (Old->isThisDeclarationADefinition()) |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 503 | PrevDiag = diag::note_previous_definition; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 504 | else if (Old->isImplicit()) |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 505 | PrevDiag = diag::note_previous_implicit_declaration; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 506 | else |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 507 | PrevDiag = diag::note_previous_declaration; |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 508 | |
Chris Lattner | 8bcfc5b | 2008-04-06 23:10:54 +0000 | [diff] [blame] | 509 | QualType OldQType = Context.getCanonicalType(Old->getType()); |
| 510 | QualType NewQType = Context.getCanonicalType(New->getType()); |
Chris Lattner | 5519644 | 2007-11-20 19:04:50 +0000 | [diff] [blame] | 511 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 512 | if (getLangOptions().CPlusPlus) { |
| 513 | // (C++98 13.1p2): |
| 514 | // Certain function declarations cannot be overloaded: |
| 515 | // -- Function declarations that differ only in the return type |
| 516 | // cannot be overloaded. |
| 517 | QualType OldReturnType |
| 518 | = cast<FunctionType>(OldQType.getTypePtr())->getResultType(); |
| 519 | QualType NewReturnType |
| 520 | = cast<FunctionType>(NewQType.getTypePtr())->getResultType(); |
| 521 | if (OldReturnType != NewReturnType) { |
| 522 | Diag(New->getLocation(), diag::err_ovl_diff_return_type); |
| 523 | Diag(Old->getLocation(), PrevDiag); |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 524 | Redeclaration = true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 525 | return New; |
| 526 | } |
| 527 | |
| 528 | const CXXMethodDecl* OldMethod = dyn_cast<CXXMethodDecl>(Old); |
| 529 | const CXXMethodDecl* NewMethod = dyn_cast<CXXMethodDecl>(New); |
| 530 | if (OldMethod && NewMethod) { |
| 531 | // -- Member function declarations with the same name and the |
| 532 | // same parameter types cannot be overloaded if any of them |
| 533 | // is a static member function declaration. |
| 534 | if (OldMethod->isStatic() || NewMethod->isStatic()) { |
| 535 | Diag(New->getLocation(), diag::err_ovl_static_nonstatic_member); |
| 536 | Diag(Old->getLocation(), PrevDiag); |
| 537 | return New; |
| 538 | } |
Douglas Gregor | 9e7d9de | 2008-12-15 21:24:18 +0000 | [diff] [blame] | 539 | |
| 540 | // C++ [class.mem]p1: |
| 541 | // [...] A member shall not be declared twice in the |
| 542 | // member-specification, except that a nested class or member |
| 543 | // class template can be declared and then later defined. |
| 544 | if (OldMethod->getLexicalDeclContext() == |
| 545 | NewMethod->getLexicalDeclContext()) { |
| 546 | unsigned NewDiag; |
| 547 | if (isa<CXXConstructorDecl>(OldMethod)) |
| 548 | NewDiag = diag::err_constructor_redeclared; |
| 549 | else if (isa<CXXDestructorDecl>(NewMethod)) |
| 550 | NewDiag = diag::err_destructor_redeclared; |
| 551 | else if (isa<CXXConversionDecl>(NewMethod)) |
| 552 | NewDiag = diag::err_conv_function_redeclared; |
| 553 | else |
| 554 | NewDiag = diag::err_member_redeclared; |
| 555 | |
| 556 | Diag(New->getLocation(), NewDiag); |
| 557 | Diag(Old->getLocation(), PrevDiag); |
| 558 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | // (C++98 8.3.5p3): |
| 562 | // All declarations for a function shall agree exactly in both the |
| 563 | // return type and the parameter-type-list. |
| 564 | if (OldQType == NewQType) { |
| 565 | // We have a redeclaration. |
| 566 | MergeAttributes(New, Old); |
| 567 | Redeclaration = true; |
| 568 | return MergeCXXFunctionDecl(New, Old); |
| 569 | } |
| 570 | |
| 571 | // Fall through for conflicting redeclarations and redefinitions. |
Douglas Gregor | f009795 | 2008-04-21 02:02:58 +0000 | [diff] [blame] | 572 | } |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 573 | |
| 574 | // C: Function types need to be compatible, not identical. This handles |
Steve Naroff | adbbd0c | 2008-01-14 20:51:29 +0000 | [diff] [blame] | 575 | // duplicate function decls like "void f(int); void f(enum X);" properly. |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 576 | if (!getLangOptions().CPlusPlus && |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 577 | Context.typesAreCompatible(OldQType, NewQType)) { |
Douglas Gregor | f009795 | 2008-04-21 02:02:58 +0000 | [diff] [blame] | 578 | MergeAttributes(New, Old); |
| 579 | Redeclaration = true; |
Steve Naroff | adbbd0c | 2008-01-14 20:51:29 +0000 | [diff] [blame] | 580 | return New; |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 581 | } |
Chris Lattner | e3995fe | 2007-11-06 06:07:26 +0000 | [diff] [blame] | 582 | |
Steve Naroff | 837618c | 2008-01-16 15:01:34 +0000 | [diff] [blame] | 583 | // A function that has already been declared has been redeclared or defined |
| 584 | // with a different type- show appropriate diagnostic |
Steve Naroff | 837618c | 2008-01-16 15:01:34 +0000 | [diff] [blame] | 585 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 586 | // TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope. |
| 587 | // TODO: This is totally simplistic. It should handle merging functions |
| 588 | // together etc, merging extern int X; int X; ... |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 589 | Diag(New->getLocation(), diag::err_conflicting_types) << New->getDeclName(); |
Steve Naroff | 837618c | 2008-01-16 15:01:34 +0000 | [diff] [blame] | 590 | Diag(Old->getLocation(), PrevDiag); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 591 | return New; |
| 592 | } |
| 593 | |
Steve Naroff | ff9eb1f | 2008-08-08 17:50:35 +0000 | [diff] [blame] | 594 | /// Predicate for C "tentative" external object definitions (C99 6.9.2). |
Steve Naroff | d4d46cd | 2008-08-10 15:28:06 +0000 | [diff] [blame] | 595 | static bool isTentativeDefinition(VarDecl *VD) { |
Steve Naroff | ff9eb1f | 2008-08-08 17:50:35 +0000 | [diff] [blame] | 596 | if (VD->isFileVarDecl()) |
| 597 | return (!VD->getInit() && |
| 598 | (VD->getStorageClass() == VarDecl::None || |
| 599 | VD->getStorageClass() == VarDecl::Static)); |
| 600 | return false; |
| 601 | } |
| 602 | |
| 603 | /// CheckForFileScopedRedefinitions - Make sure we forgo redefinition errors |
| 604 | /// when dealing with C "tentative" external object definitions (C99 6.9.2). |
| 605 | void Sema::CheckForFileScopedRedefinitions(Scope *S, VarDecl *VD) { |
| 606 | bool VDIsTentative = isTentativeDefinition(VD); |
Steve Naroff | f855e6f | 2008-08-10 15:20:13 +0000 | [diff] [blame] | 607 | bool VDIsIncompleteArray = VD->getType()->isIncompleteArrayType(); |
Steve Naroff | ff9eb1f | 2008-08-08 17:50:35 +0000 | [diff] [blame] | 608 | |
Douglas Gregor | e21b994 | 2009-01-07 16:34:42 +0000 | [diff] [blame] | 609 | // FIXME: I don't think this will actually see all of the |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 610 | // redefinitions. Can't we check this property on-the-fly? |
Steve Naroff | ff9eb1f | 2008-08-08 17:50:35 +0000 | [diff] [blame] | 611 | for (IdentifierResolver::iterator |
| 612 | I = IdResolver.begin(VD->getIdentifier(), |
| 613 | VD->getDeclContext(), false/*LookInParentCtx*/), |
| 614 | E = IdResolver.end(); I != E; ++I) { |
Argyrios Kyrtzidis | 15a12d0 | 2008-09-09 21:18:04 +0000 | [diff] [blame] | 615 | if (*I != VD && isDeclInScope(*I, VD->getDeclContext(), S)) { |
Steve Naroff | ff9eb1f | 2008-08-08 17:50:35 +0000 | [diff] [blame] | 616 | VarDecl *OldDecl = dyn_cast<VarDecl>(*I); |
| 617 | |
Steve Naroff | f855e6f | 2008-08-10 15:20:13 +0000 | [diff] [blame] | 618 | // Handle the following case: |
| 619 | // int a[10]; |
| 620 | // int a[]; - the code below makes sure we set the correct type. |
| 621 | // int a[11]; - this is an error, size isn't 10. |
| 622 | if (OldDecl && VDIsTentative && VDIsIncompleteArray && |
| 623 | OldDecl->getType()->isConstantArrayType()) |
| 624 | VD->setType(OldDecl->getType()); |
| 625 | |
Steve Naroff | ff9eb1f | 2008-08-08 17:50:35 +0000 | [diff] [blame] | 626 | // Check for "tentative" definitions. We can't accomplish this in |
| 627 | // MergeVarDecl since the initializer hasn't been attached. |
| 628 | if (!OldDecl || isTentativeDefinition(OldDecl) || VDIsTentative) |
| 629 | continue; |
| 630 | |
| 631 | // Handle __private_extern__ just like extern. |
| 632 | if (OldDecl->getStorageClass() != VarDecl::Extern && |
| 633 | OldDecl->getStorageClass() != VarDecl::PrivateExtern && |
| 634 | VD->getStorageClass() != VarDecl::Extern && |
| 635 | VD->getStorageClass() != VarDecl::PrivateExtern) { |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 636 | Diag(VD->getLocation(), diag::err_redefinition) << VD->getDeclName(); |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 637 | Diag(OldDecl->getLocation(), diag::note_previous_definition); |
Steve Naroff | ff9eb1f | 2008-08-08 17:50:35 +0000 | [diff] [blame] | 638 | } |
| 639 | } |
| 640 | } |
| 641 | } |
| 642 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 643 | /// MergeVarDecl - We just parsed a variable 'New' which has the same name |
| 644 | /// and scope as a previous declaration 'Old'. Figure out how to resolve this |
| 645 | /// situation, merging decls or emitting diagnostics as appropriate. |
| 646 | /// |
Steve Naroff | ff9eb1f | 2008-08-08 17:50:35 +0000 | [diff] [blame] | 647 | /// Tentative definition rules (C99 6.9.2p2) are checked by |
| 648 | /// FinalizeDeclaratorGroup. Unfortunately, we can't analyze tentative |
| 649 | /// definitions here, since the initializer hasn't been attached. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 650 | /// |
Steve Naroff | e8043c3 | 2008-04-01 23:04:06 +0000 | [diff] [blame] | 651 | VarDecl *Sema::MergeVarDecl(VarDecl *New, Decl *OldD) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 652 | // Verify the old decl was also a variable. |
| 653 | VarDecl *Old = dyn_cast<VarDecl>(OldD); |
| 654 | if (!Old) { |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 655 | Diag(New->getLocation(), diag::err_redefinition_different_kind) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 656 | << New->getDeclName(); |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 657 | Diag(OldD->getLocation(), diag::note_previous_definition); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 658 | return New; |
| 659 | } |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 660 | |
| 661 | MergeAttributes(New, Old); |
| 662 | |
Eli Friedman | 13ca96a | 2009-01-24 23:49:55 +0000 | [diff] [blame] | 663 | // Merge the types |
| 664 | QualType MergedT = Context.mergeTypes(New->getType(), Old->getType()); |
| 665 | if (MergedT.isNull()) { |
Douglas Gregor | 6037fcb | 2009-01-09 19:42:16 +0000 | [diff] [blame] | 666 | Diag(New->getLocation(), diag::err_redefinition_different_type) |
| 667 | << New->getDeclName(); |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 668 | Diag(Old->getLocation(), diag::note_previous_definition); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 669 | return New; |
| 670 | } |
Eli Friedman | 13ca96a | 2009-01-24 23:49:55 +0000 | [diff] [blame] | 671 | New->setType(MergedT); |
Steve Naroff | b7b032e | 2008-01-30 00:44:01 +0000 | [diff] [blame] | 672 | // C99 6.2.2p4: Check if we have a static decl followed by a non-static. |
| 673 | if (New->getStorageClass() == VarDecl::Static && |
| 674 | (Old->getStorageClass() == VarDecl::None || |
| 675 | Old->getStorageClass() == VarDecl::Extern)) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 676 | Diag(New->getLocation(), diag::err_static_non_static) << New->getDeclName(); |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 677 | Diag(Old->getLocation(), diag::note_previous_definition); |
Steve Naroff | b7b032e | 2008-01-30 00:44:01 +0000 | [diff] [blame] | 678 | return New; |
| 679 | } |
| 680 | // C99 6.2.2p4: Check if we have a non-static decl followed by a static. |
| 681 | if (New->getStorageClass() != VarDecl::Static && |
| 682 | Old->getStorageClass() == VarDecl::Static) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 683 | Diag(New->getLocation(), diag::err_non_static_static) << New->getDeclName(); |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 684 | Diag(Old->getLocation(), diag::note_previous_definition); |
Steve Naroff | b7b032e | 2008-01-30 00:44:01 +0000 | [diff] [blame] | 685 | return New; |
| 686 | } |
Steve Naroff | 094cefb | 2008-09-17 14:05:40 +0000 | [diff] [blame] | 687 | // Variables with external linkage are analyzed in FinalizeDeclaratorGroup. |
| 688 | if (New->getStorageClass() != VarDecl::Extern && !New->isFileVarDecl()) { |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 689 | Diag(New->getLocation(), diag::err_redefinition) << New->getDeclName(); |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 690 | Diag(Old->getLocation(), diag::note_previous_definition); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 691 | } |
| 692 | return New; |
| 693 | } |
| 694 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 695 | /// CheckParmsForFunctionDef - Check that the parameters of the given |
| 696 | /// function are appropriate for the definition of a function. This |
| 697 | /// takes care of any checks that cannot be performed on the |
| 698 | /// declaration itself, e.g., that the types of each of the function |
| 699 | /// parameters are complete. |
| 700 | bool Sema::CheckParmsForFunctionDef(FunctionDecl *FD) { |
| 701 | bool HasInvalidParm = false; |
| 702 | for (unsigned p = 0, NumParams = FD->getNumParams(); p < NumParams; ++p) { |
| 703 | ParmVarDecl *Param = FD->getParamDecl(p); |
| 704 | |
| 705 | // C99 6.7.5.3p4: the parameters in a parameter type list in a |
| 706 | // function declarator that is part of a function definition of |
| 707 | // that function shall not have incomplete type. |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 708 | if (!Param->isInvalidDecl() && |
| 709 | DiagnoseIncompleteType(Param->getLocation(), Param->getType(), |
| 710 | diag::err_typecheck_decl_incomplete_type)) { |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 711 | Param->setInvalidDecl(); |
| 712 | HasInvalidParm = true; |
| 713 | } |
Chris Lattner | 777f07b | 2008-12-17 07:32:46 +0000 | [diff] [blame] | 714 | |
| 715 | // C99 6.9.1p5: If the declarator includes a parameter type list, the |
| 716 | // declaration of each parameter shall include an identifier. |
| 717 | if (Param->getIdentifier() == 0 && !getLangOptions().CPlusPlus) |
| 718 | Diag(Param->getLocation(), diag::err_parameter_name_omitted); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | return HasInvalidParm; |
| 722 | } |
| 723 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 724 | /// ParsedFreeStandingDeclSpec - This method is invoked when a declspec with |
| 725 | /// no declarator (e.g. "struct foo;") is parsed. |
| 726 | Sema::DeclTy *Sema::ParsedFreeStandingDeclSpec(Scope *S, DeclSpec &DS) { |
Douglas Gregor | 4920f1f | 2009-01-12 22:49:06 +0000 | [diff] [blame] | 727 | TagDecl *Tag |
| 728 | = dyn_cast_or_null<TagDecl>(static_cast<Decl *>(DS.getTypeRep())); |
| 729 | if (RecordDecl *Record = dyn_cast_or_null<RecordDecl>(Tag)) { |
| 730 | if (!Record->getDeclName() && Record->isDefinition() && |
| 731 | DS.getStorageClassSpec() != DeclSpec::SCS_typedef) |
| 732 | return BuildAnonymousStructOrUnion(S, DS, Record); |
| 733 | |
| 734 | // Microsoft allows unnamed struct/union fields. Don't complain |
| 735 | // about them. |
| 736 | // FIXME: Should we support Microsoft's extensions in this area? |
| 737 | if (Record->getDeclName() && getLangOptions().Microsoft) |
| 738 | return Tag; |
| 739 | } |
| 740 | |
Sebastian Redl | a4ed0d8 | 2008-12-28 15:28:59 +0000 | [diff] [blame] | 741 | if (!DS.isMissingDeclaratorOk()) { |
Douglas Gregor | 21282df | 2009-01-22 16:23:54 +0000 | [diff] [blame] | 742 | // Warn about typedefs of enums without names, since this is an |
| 743 | // extension in both Microsoft an GNU. |
Douglas Gregor | 8158f69 | 2009-01-17 02:55:50 +0000 | [diff] [blame] | 744 | if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef && |
| 745 | Tag && isa<EnumDecl>(Tag)) { |
Douglas Gregor | 21282df | 2009-01-22 16:23:54 +0000 | [diff] [blame] | 746 | Diag(DS.getSourceRange().getBegin(), diag::ext_typedef_without_a_name) |
Douglas Gregor | ee159c1 | 2009-01-13 23:10:51 +0000 | [diff] [blame] | 747 | << DS.getSourceRange(); |
| 748 | return Tag; |
| 749 | } |
| 750 | |
Sebastian Redl | a4ed0d8 | 2008-12-28 15:28:59 +0000 | [diff] [blame] | 751 | // FIXME: This diagnostic is emitted even when various previous |
| 752 | // errors occurred (see e.g. test/Sema/decl-invalid.c). However, |
| 753 | // DeclSpec has no means of communicating this information, and the |
| 754 | // responsible parser functions are quite far apart. |
| 755 | Diag(DS.getSourceRange().getBegin(), diag::err_no_declarators) |
| 756 | << DS.getSourceRange(); |
| 757 | return 0; |
| 758 | } |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 759 | |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 760 | return Tag; |
| 761 | } |
| 762 | |
| 763 | /// InjectAnonymousStructOrUnionMembers - Inject the members of the |
| 764 | /// anonymous struct or union AnonRecord into the owning context Owner |
| 765 | /// and scope S. This routine will be invoked just after we realize |
| 766 | /// that an unnamed union or struct is actually an anonymous union or |
| 767 | /// struct, e.g., |
| 768 | /// |
| 769 | /// @code |
| 770 | /// union { |
| 771 | /// int i; |
| 772 | /// float f; |
| 773 | /// }; // InjectAnonymousStructOrUnionMembers called here to inject i and |
| 774 | /// // f into the surrounding scope.x |
| 775 | /// @endcode |
| 776 | /// |
| 777 | /// This routine is recursive, injecting the names of nested anonymous |
| 778 | /// structs/unions into the owning context and scope as well. |
| 779 | bool Sema::InjectAnonymousStructOrUnionMembers(Scope *S, DeclContext *Owner, |
| 780 | RecordDecl *AnonRecord) { |
| 781 | bool Invalid = false; |
| 782 | for (RecordDecl::field_iterator F = AnonRecord->field_begin(), |
| 783 | FEnd = AnonRecord->field_end(); |
| 784 | F != FEnd; ++F) { |
| 785 | if ((*F)->getDeclName()) { |
| 786 | Decl *PrevDecl = LookupDecl((*F)->getDeclName(), Decl::IDNS_Ordinary, |
Steve Naroff | 133147d | 2009-01-28 16:09:22 +0000 | [diff] [blame] | 787 | S, Owner, false); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 788 | if (PrevDecl && !isa<TagDecl>(PrevDecl)) { |
| 789 | // C++ [class.union]p2: |
| 790 | // The names of the members of an anonymous union shall be |
| 791 | // distinct from the names of any other entity in the |
| 792 | // scope in which the anonymous union is declared. |
| 793 | unsigned diagKind |
| 794 | = AnonRecord->isUnion()? diag::err_anonymous_union_member_redecl |
| 795 | : diag::err_anonymous_struct_member_redecl; |
| 796 | Diag((*F)->getLocation(), diagKind) |
| 797 | << (*F)->getDeclName(); |
| 798 | Diag(PrevDecl->getLocation(), diag::note_previous_declaration); |
| 799 | Invalid = true; |
| 800 | } else { |
| 801 | // C++ [class.union]p2: |
| 802 | // For the purpose of name lookup, after the anonymous union |
| 803 | // definition, the members of the anonymous union are |
| 804 | // considered to have been defined in the scope in which the |
| 805 | // anonymous union is declared. |
Douglas Gregor | 40f4e69 | 2009-01-20 16:54:50 +0000 | [diff] [blame] | 806 | Owner->makeDeclVisibleInContext(*F); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 807 | S->AddDecl(*F); |
| 808 | IdResolver.AddDecl(*F); |
| 809 | } |
| 810 | } else if (const RecordType *InnerRecordType |
| 811 | = (*F)->getType()->getAsRecordType()) { |
| 812 | RecordDecl *InnerRecord = InnerRecordType->getDecl(); |
| 813 | if (InnerRecord->isAnonymousStructOrUnion()) |
| 814 | Invalid = Invalid || |
| 815 | InjectAnonymousStructOrUnionMembers(S, Owner, InnerRecord); |
| 816 | } |
| 817 | } |
| 818 | |
| 819 | return Invalid; |
| 820 | } |
| 821 | |
| 822 | /// ActOnAnonymousStructOrUnion - Handle the declaration of an |
| 823 | /// anonymous structure or union. Anonymous unions are a C++ feature |
| 824 | /// (C++ [class.union]) and a GNU C extension; anonymous structures |
| 825 | /// are a GNU C and GNU C++ extension. |
| 826 | Sema::DeclTy *Sema::BuildAnonymousStructOrUnion(Scope *S, DeclSpec &DS, |
| 827 | RecordDecl *Record) { |
| 828 | DeclContext *Owner = Record->getDeclContext(); |
| 829 | |
| 830 | // Diagnose whether this anonymous struct/union is an extension. |
| 831 | if (Record->isUnion() && !getLangOptions().CPlusPlus) |
| 832 | Diag(Record->getLocation(), diag::ext_anonymous_union); |
| 833 | else if (!Record->isUnion()) |
| 834 | Diag(Record->getLocation(), diag::ext_anonymous_struct); |
| 835 | |
| 836 | // C and C++ require different kinds of checks for anonymous |
| 837 | // structs/unions. |
| 838 | bool Invalid = false; |
| 839 | if (getLangOptions().CPlusPlus) { |
| 840 | const char* PrevSpec = 0; |
| 841 | // C++ [class.union]p3: |
| 842 | // Anonymous unions declared in a named namespace or in the |
| 843 | // global namespace shall be declared static. |
| 844 | if (DS.getStorageClassSpec() != DeclSpec::SCS_static && |
| 845 | (isa<TranslationUnitDecl>(Owner) || |
| 846 | (isa<NamespaceDecl>(Owner) && |
| 847 | cast<NamespaceDecl>(Owner)->getDeclName()))) { |
| 848 | Diag(Record->getLocation(), diag::err_anonymous_union_not_static); |
| 849 | Invalid = true; |
| 850 | |
| 851 | // Recover by adding 'static'. |
| 852 | DS.SetStorageClassSpec(DeclSpec::SCS_static, SourceLocation(), PrevSpec); |
| 853 | } |
| 854 | // C++ [class.union]p3: |
| 855 | // A storage class is not allowed in a declaration of an |
| 856 | // anonymous union in a class scope. |
| 857 | else if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified && |
| 858 | isa<RecordDecl>(Owner)) { |
| 859 | Diag(DS.getStorageClassSpecLoc(), |
| 860 | diag::err_anonymous_union_with_storage_spec); |
| 861 | Invalid = true; |
| 862 | |
| 863 | // Recover by removing the storage specifier. |
| 864 | DS.SetStorageClassSpec(DeclSpec::SCS_unspecified, SourceLocation(), |
| 865 | PrevSpec); |
| 866 | } |
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 867 | |
| 868 | // C++ [class.union]p2: |
| 869 | // The member-specification of an anonymous union shall only |
| 870 | // define non-static data members. [Note: nested types and |
| 871 | // functions cannot be declared within an anonymous union. ] |
| 872 | for (DeclContext::decl_iterator Mem = Record->decls_begin(), |
| 873 | MemEnd = Record->decls_end(); |
| 874 | Mem != MemEnd; ++Mem) { |
| 875 | if (FieldDecl *FD = dyn_cast<FieldDecl>(*Mem)) { |
| 876 | // C++ [class.union]p3: |
| 877 | // An anonymous union shall not have private or protected |
| 878 | // members (clause 11). |
| 879 | if (FD->getAccess() == AS_protected || FD->getAccess() == AS_private) { |
| 880 | Diag(FD->getLocation(), diag::err_anonymous_record_nonpublic_member) |
| 881 | << (int)Record->isUnion() << (int)(FD->getAccess() == AS_protected); |
| 882 | Invalid = true; |
| 883 | } |
| 884 | } else if ((*Mem)->isImplicit()) { |
| 885 | // Any implicit members are fine. |
| 886 | } else if (RecordDecl *MemRecord = dyn_cast<RecordDecl>(*Mem)) { |
| 887 | if (!MemRecord->isAnonymousStructOrUnion() && |
| 888 | MemRecord->getDeclName()) { |
| 889 | // This is a nested type declaration. |
| 890 | Diag(MemRecord->getLocation(), diag::err_anonymous_record_with_type) |
| 891 | << (int)Record->isUnion(); |
| 892 | Invalid = true; |
| 893 | } |
| 894 | } else { |
| 895 | // We have something that isn't a non-static data |
| 896 | // member. Complain about it. |
| 897 | unsigned DK = diag::err_anonymous_record_bad_member; |
| 898 | if (isa<TypeDecl>(*Mem)) |
| 899 | DK = diag::err_anonymous_record_with_type; |
| 900 | else if (isa<FunctionDecl>(*Mem)) |
| 901 | DK = diag::err_anonymous_record_with_function; |
| 902 | else if (isa<VarDecl>(*Mem)) |
| 903 | DK = diag::err_anonymous_record_with_static; |
| 904 | Diag((*Mem)->getLocation(), DK) |
| 905 | << (int)Record->isUnion(); |
| 906 | Invalid = true; |
| 907 | } |
| 908 | } |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 909 | } else { |
| 910 | // FIXME: Check GNU C semantics |
Douglas Gregor | 4920f1f | 2009-01-12 22:49:06 +0000 | [diff] [blame] | 911 | if (Record->isUnion() && !Owner->isRecord()) { |
| 912 | Diag(Record->getLocation(), diag::err_anonymous_union_not_member) |
| 913 | << (int)getLangOptions().CPlusPlus; |
| 914 | Invalid = true; |
| 915 | } |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 916 | } |
| 917 | |
| 918 | if (!Record->isUnion() && !Owner->isRecord()) { |
Douglas Gregor | 4920f1f | 2009-01-12 22:49:06 +0000 | [diff] [blame] | 919 | Diag(Record->getLocation(), diag::err_anonymous_struct_not_member) |
| 920 | << (int)getLangOptions().CPlusPlus; |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 921 | Invalid = true; |
| 922 | } |
| 923 | |
| 924 | // Create a declaration for this anonymous struct/union. |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 925 | NamedDecl *Anon = 0; |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 926 | if (RecordDecl *OwningClass = dyn_cast<RecordDecl>(Owner)) { |
| 927 | Anon = FieldDecl::Create(Context, OwningClass, Record->getLocation(), |
| 928 | /*IdentifierInfo=*/0, |
| 929 | Context.getTypeDeclType(Record), |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 930 | /*BitWidth=*/0, /*Mutable=*/false); |
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 931 | Anon->setAccess(AS_public); |
| 932 | if (getLangOptions().CPlusPlus) |
| 933 | FieldCollector->Add(cast<FieldDecl>(Anon)); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 934 | } else { |
| 935 | VarDecl::StorageClass SC; |
| 936 | switch (DS.getStorageClassSpec()) { |
| 937 | default: assert(0 && "Unknown storage class!"); |
| 938 | case DeclSpec::SCS_unspecified: SC = VarDecl::None; break; |
| 939 | case DeclSpec::SCS_extern: SC = VarDecl::Extern; break; |
| 940 | case DeclSpec::SCS_static: SC = VarDecl::Static; break; |
| 941 | case DeclSpec::SCS_auto: SC = VarDecl::Auto; break; |
| 942 | case DeclSpec::SCS_register: SC = VarDecl::Register; break; |
| 943 | case DeclSpec::SCS_private_extern: SC = VarDecl::PrivateExtern; break; |
| 944 | case DeclSpec::SCS_mutable: |
| 945 | // mutable can only appear on non-static class members, so it's always |
| 946 | // an error here |
| 947 | Diag(Record->getLocation(), diag::err_mutable_nonmember); |
| 948 | Invalid = true; |
| 949 | SC = VarDecl::None; |
| 950 | break; |
| 951 | } |
| 952 | |
| 953 | Anon = VarDecl::Create(Context, Owner, Record->getLocation(), |
| 954 | /*IdentifierInfo=*/0, |
| 955 | Context.getTypeDeclType(Record), |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 956 | SC, DS.getSourceRange().getBegin()); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 957 | } |
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 958 | Anon->setImplicit(); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 959 | |
| 960 | // Add the anonymous struct/union object to the current |
| 961 | // context. We'll be referencing this object when we refer to one of |
| 962 | // its members. |
Douglas Gregor | 482b77d | 2009-01-12 23:27:07 +0000 | [diff] [blame] | 963 | Owner->addDecl(Anon); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 964 | |
| 965 | // Inject the members of the anonymous struct/union into the owning |
| 966 | // context and into the identifier resolver chain for name lookup |
| 967 | // purposes. |
Douglas Gregor | 4920f1f | 2009-01-12 22:49:06 +0000 | [diff] [blame] | 968 | if (InjectAnonymousStructOrUnionMembers(S, Owner, Record)) |
| 969 | Invalid = true; |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 970 | |
| 971 | // Mark this as an anonymous struct/union type. Note that we do not |
| 972 | // do this until after we have already checked and injected the |
| 973 | // members of this anonymous struct/union type, because otherwise |
| 974 | // the members could be injected twice: once by DeclContext when it |
| 975 | // builds its lookup table, and once by |
| 976 | // InjectAnonymousStructOrUnionMembers. |
| 977 | Record->setAnonymousStructOrUnion(true); |
| 978 | |
| 979 | if (Invalid) |
| 980 | Anon->setInvalidDecl(); |
| 981 | |
| 982 | return Anon; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 983 | } |
| 984 | |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 985 | bool Sema::CheckSingleInitializer(Expr *&Init, QualType DeclType, |
| 986 | bool DirectInit) { |
Steve Naroff | f009063 | 2007-09-02 02:04:30 +0000 | [diff] [blame] | 987 | // Get the type before calling CheckSingleAssignmentConstraints(), since |
| 988 | // it can promote the expression. |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 989 | QualType InitType = Init->getType(); |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 990 | |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 991 | if (getLangOptions().CPlusPlus) { |
| 992 | // FIXME: I dislike this error message. A lot. |
| 993 | if (PerformImplicitConversion(Init, DeclType, "initializing", DirectInit)) |
| 994 | return Diag(Init->getSourceRange().getBegin(), |
| 995 | diag::err_typecheck_convert_incompatible) |
| 996 | << DeclType << Init->getType() << "initializing" |
| 997 | << Init->getSourceRange(); |
| 998 | |
| 999 | return false; |
| 1000 | } |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1001 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 1002 | AssignConvertType ConvTy = CheckSingleAssignmentConstraints(DeclType, Init); |
| 1003 | return DiagnoseAssignmentResult(ConvTy, Init->getLocStart(), DeclType, |
| 1004 | InitType, Init, "initializing"); |
Steve Naroff | f009063 | 2007-09-02 02:04:30 +0000 | [diff] [blame] | 1005 | } |
| 1006 | |
Steve Naroff | a49e1fa | 2008-01-22 00:55:40 +0000 | [diff] [blame] | 1007 | bool Sema::CheckStringLiteralInit(StringLiteral *strLiteral, QualType &DeclT) { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 1008 | const ArrayType *AT = Context.getAsArrayType(DeclT); |
| 1009 | |
| 1010 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) { |
Steve Naroff | a49e1fa | 2008-01-22 00:55:40 +0000 | [diff] [blame] | 1011 | // C99 6.7.8p14. We have an array of character type with unknown size |
| 1012 | // being initialized to a string literal. |
| 1013 | llvm::APSInt ConstVal(32); |
| 1014 | ConstVal = strLiteral->getByteLength() + 1; |
| 1015 | // Return a new array type (C99 6.7.8p22). |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1016 | DeclT = Context.getConstantArrayType(IAT->getElementType(), ConstVal, |
Steve Naroff | a49e1fa | 2008-01-22 00:55:40 +0000 | [diff] [blame] | 1017 | ArrayType::Normal, 0); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 1018 | } else { |
| 1019 | const ConstantArrayType *CAT = cast<ConstantArrayType>(AT); |
Steve Naroff | a49e1fa | 2008-01-22 00:55:40 +0000 | [diff] [blame] | 1020 | // 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] | 1021 | // FIXME: Avoid truncation for 64-bit length strings. |
| 1022 | if (strLiteral->getByteLength() > (unsigned)CAT->getSize().getZExtValue()) |
Steve Naroff | a49e1fa | 2008-01-22 00:55:40 +0000 | [diff] [blame] | 1023 | Diag(strLiteral->getSourceRange().getBegin(), |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 1024 | diag::warn_initializer_string_for_char_array_too_long) |
| 1025 | << strLiteral->getSourceRange(); |
Steve Naroff | a49e1fa | 2008-01-22 00:55:40 +0000 | [diff] [blame] | 1026 | } |
| 1027 | // Set type from "char *" to "constant array of char". |
| 1028 | strLiteral->setType(DeclT); |
| 1029 | // For now, we always return false (meaning success). |
| 1030 | return false; |
| 1031 | } |
| 1032 | |
| 1033 | StringLiteral *Sema::IsStringLiteralInit(Expr *Init, QualType DeclType) { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 1034 | const ArrayType *AT = Context.getAsArrayType(DeclType); |
Steve Naroff | a996033 | 2008-01-25 00:51:06 +0000 | [diff] [blame] | 1035 | if (AT && AT->getElementType()->isCharType()) { |
Anders Carlsson | 91b9f20 | 2009-01-24 17:47:50 +0000 | [diff] [blame] | 1036 | return dyn_cast<StringLiteral>(Init->IgnoreParens()); |
Steve Naroff | a996033 | 2008-01-25 00:51:06 +0000 | [diff] [blame] | 1037 | } |
Steve Naroff | a49e1fa | 2008-01-22 00:55:40 +0000 | [diff] [blame] | 1038 | return 0; |
| 1039 | } |
| 1040 | |
Douglas Gregor | f03d7c7 | 2008-11-05 15:29:30 +0000 | [diff] [blame] | 1041 | bool Sema::CheckInitializerTypes(Expr *&Init, QualType &DeclType, |
| 1042 | SourceLocation InitLoc, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 1043 | DeclarationName InitEntity, |
| 1044 | bool DirectInit) { |
Douglas Gregor | 264c8ed | 2008-12-18 21:49:58 +0000 | [diff] [blame] | 1045 | if (DeclType->isDependentType() || Init->isTypeDependent()) |
| 1046 | return false; |
| 1047 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1048 | // C++ [dcl.init.ref]p1: |
Sebastian Redl | d14094d | 2008-11-24 20:06:50 +0000 | [diff] [blame] | 1049 | // 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] | 1050 | // (8.3.2), shall be initialized by an object, or function, of |
| 1051 | // type T or by an object that can be converted into a T. |
| 1052 | if (DeclType->isReferenceType()) |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 1053 | return CheckReferenceInit(Init, DeclType, 0, false, DirectInit); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1054 | |
Steve Naroff | ca10730 | 2008-01-21 23:53:58 +0000 | [diff] [blame] | 1055 | // C99 6.7.8p3: The type of the entity to be initialized shall be an array |
| 1056 | // 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] | 1057 | if (const VariableArrayType *VAT = Context.getAsVariableArrayType(DeclType)) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 1058 | return Diag(InitLoc, diag::err_variable_object_no_init) |
| 1059 | << VAT->getSizeExpr()->getSourceRange(); |
Steve Naroff | ca10730 | 2008-01-21 23:53:58 +0000 | [diff] [blame] | 1060 | |
Steve Naroff | 2fdc374 | 2007-12-10 22:44:33 +0000 | [diff] [blame] | 1061 | InitListExpr *InitList = dyn_cast<InitListExpr>(Init); |
| 1062 | if (!InitList) { |
Steve Naroff | a49e1fa | 2008-01-22 00:55:40 +0000 | [diff] [blame] | 1063 | // FIXME: Handle wide strings |
| 1064 | if (StringLiteral *strLiteral = IsStringLiteralInit(Init, DeclType)) |
| 1065 | return CheckStringLiteralInit(strLiteral, DeclType); |
Eli Friedman | a312ce2 | 2008-02-08 00:48:24 +0000 | [diff] [blame] | 1066 | |
Douglas Gregor | f03d7c7 | 2008-11-05 15:29:30 +0000 | [diff] [blame] | 1067 | // C++ [dcl.init]p14: |
| 1068 | // -- If the destination type is a (possibly cv-qualified) class |
| 1069 | // type: |
| 1070 | if (getLangOptions().CPlusPlus && DeclType->isRecordType()) { |
| 1071 | QualType DeclTypeC = Context.getCanonicalType(DeclType); |
| 1072 | QualType InitTypeC = Context.getCanonicalType(Init->getType()); |
| 1073 | |
| 1074 | // -- If the initialization is direct-initialization, or if it is |
| 1075 | // copy-initialization where the cv-unqualified version of the |
| 1076 | // source type is the same class as, or a derived class of, the |
| 1077 | // class of the destination, constructors are considered. |
| 1078 | if ((DeclTypeC.getUnqualifiedType() == InitTypeC.getUnqualifiedType()) || |
| 1079 | IsDerivedFrom(InitTypeC, DeclTypeC)) { |
| 1080 | CXXConstructorDecl *Constructor |
| 1081 | = PerformInitializationByConstructor(DeclType, &Init, 1, |
| 1082 | InitLoc, Init->getSourceRange(), |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 1083 | InitEntity, |
| 1084 | DirectInit? IK_Direct : IK_Copy); |
Douglas Gregor | f03d7c7 | 2008-11-05 15:29:30 +0000 | [diff] [blame] | 1085 | return Constructor == 0; |
| 1086 | } |
| 1087 | |
| 1088 | // -- Otherwise (i.e., for the remaining copy-initialization |
| 1089 | // cases), user-defined conversion sequences that can |
| 1090 | // convert from the source type to the destination type or |
| 1091 | // (when a conversion function is used) to a derived class |
| 1092 | // thereof are enumerated as described in 13.3.1.4, and the |
| 1093 | // best one is chosen through overload resolution |
| 1094 | // (13.3). If the conversion cannot be done or is |
| 1095 | // ambiguous, the initialization is ill-formed. The |
| 1096 | // function selected is called with the initializer |
| 1097 | // expression as its argument; if the function is a |
| 1098 | // constructor, the call initializes a temporary of the |
| 1099 | // destination type. |
| 1100 | // FIXME: We're pretending to do copy elision here; return to |
| 1101 | // this when we have ASTs for such things. |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1102 | if (!PerformImplicitConversion(Init, DeclType, "initializing")) |
Douglas Gregor | f03d7c7 | 2008-11-05 15:29:30 +0000 | [diff] [blame] | 1103 | return false; |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 1104 | |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 1105 | if (InitEntity) |
| 1106 | return Diag(InitLoc, diag::err_cannot_initialize_decl) |
| 1107 | << InitEntity << (int)(Init->isLvalue(Context) == Expr::LV_Valid) |
| 1108 | << Init->getType() << Init->getSourceRange(); |
| 1109 | else |
| 1110 | return Diag(InitLoc, diag::err_cannot_initialize_decl_noname) |
| 1111 | << DeclType << (int)(Init->isLvalue(Context) == Expr::LV_Valid) |
| 1112 | << Init->getType() << Init->getSourceRange(); |
Douglas Gregor | f03d7c7 | 2008-11-05 15:29:30 +0000 | [diff] [blame] | 1113 | } |
| 1114 | |
Steve Naroff | 1ac6fdd | 2008-09-29 20:07:05 +0000 | [diff] [blame] | 1115 | // C99 6.7.8p16. |
Eli Friedman | a312ce2 | 2008-02-08 00:48:24 +0000 | [diff] [blame] | 1116 | if (DeclType->isArrayType()) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 1117 | return Diag(Init->getLocStart(), diag::err_array_init_list_required) |
| 1118 | << Init->getSourceRange(); |
Eli Friedman | a312ce2 | 2008-02-08 00:48:24 +0000 | [diff] [blame] | 1119 | |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 1120 | return CheckSingleInitializer(Init, DeclType, DirectInit); |
Douglas Gregor | 64bffa9 | 2008-11-05 16:20:31 +0000 | [diff] [blame] | 1121 | } else if (getLangOptions().CPlusPlus) { |
| 1122 | // C++ [dcl.init]p14: |
| 1123 | // [...] If the class is an aggregate (8.5.1), and the initializer |
| 1124 | // is a brace-enclosed list, see 8.5.1. |
| 1125 | // |
| 1126 | // Note: 8.5.1 is handled below; here, we diagnose the case where |
| 1127 | // we have an initializer list and a destination type that is not |
| 1128 | // an aggregate. |
| 1129 | // FIXME: In C++0x, this is yet another form of initialization. |
| 1130 | if (const RecordType *ClassRec = DeclType->getAsRecordType()) { |
| 1131 | const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(ClassRec->getDecl()); |
| 1132 | if (!ClassDecl->isAggregate()) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 1133 | return Diag(InitLoc, diag::err_init_non_aggr_init_list) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 1134 | << DeclType << Init->getSourceRange(); |
Douglas Gregor | 64bffa9 | 2008-11-05 16:20:31 +0000 | [diff] [blame] | 1135 | } |
Steve Naroff | 2fdc374 | 2007-12-10 22:44:33 +0000 | [diff] [blame] | 1136 | } |
Eli Friedman | e6f058f | 2008-06-06 19:40:52 +0000 | [diff] [blame] | 1137 | |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1138 | InitListChecker CheckInitList(this, InitList, DeclType); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame^] | 1139 | if (!CheckInitList.HadError()) |
| 1140 | Init = CheckInitList.getFullyStructuredList(); |
| 1141 | |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1142 | return CheckInitList.HadError(); |
Steve Naroff | f009063 | 2007-09-02 02:04:30 +0000 | [diff] [blame] | 1143 | } |
| 1144 | |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 1145 | /// GetNameForDeclarator - Determine the full declaration name for the |
| 1146 | /// given Declarator. |
| 1147 | DeclarationName Sema::GetNameForDeclarator(Declarator &D) { |
| 1148 | switch (D.getKind()) { |
| 1149 | case Declarator::DK_Abstract: |
| 1150 | assert(D.getIdentifier() == 0 && "abstract declarators have no name"); |
| 1151 | return DeclarationName(); |
| 1152 | |
| 1153 | case Declarator::DK_Normal: |
| 1154 | assert (D.getIdentifier() != 0 && "normal declarators have an identifier"); |
| 1155 | return DeclarationName(D.getIdentifier()); |
| 1156 | |
| 1157 | case Declarator::DK_Constructor: { |
| 1158 | QualType Ty = Context.getTypeDeclType((TypeDecl *)D.getDeclaratorIdType()); |
| 1159 | Ty = Context.getCanonicalType(Ty); |
| 1160 | return Context.DeclarationNames.getCXXConstructorName(Ty); |
| 1161 | } |
| 1162 | |
| 1163 | case Declarator::DK_Destructor: { |
| 1164 | QualType Ty = Context.getTypeDeclType((TypeDecl *)D.getDeclaratorIdType()); |
| 1165 | Ty = Context.getCanonicalType(Ty); |
| 1166 | return Context.DeclarationNames.getCXXDestructorName(Ty); |
| 1167 | } |
| 1168 | |
| 1169 | case Declarator::DK_Conversion: { |
| 1170 | QualType Ty = QualType::getFromOpaquePtr(D.getDeclaratorIdType()); |
| 1171 | Ty = Context.getCanonicalType(Ty); |
| 1172 | return Context.DeclarationNames.getCXXConversionFunctionName(Ty); |
| 1173 | } |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 1174 | |
| 1175 | case Declarator::DK_Operator: |
| 1176 | assert(D.getIdentifier() == 0 && "operator names have no identifier"); |
| 1177 | return Context.DeclarationNames.getCXXOperatorName( |
| 1178 | D.getOverloadedOperator()); |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 1179 | } |
| 1180 | |
| 1181 | assert(false && "Unknown name kind"); |
| 1182 | return DeclarationName(); |
| 1183 | } |
| 1184 | |
Douglas Gregor | 584049d | 2008-12-15 23:53:10 +0000 | [diff] [blame] | 1185 | /// isNearlyMatchingMemberFunction - Determine whether the C++ member |
| 1186 | /// functions Declaration and Definition are "nearly" matching. This |
| 1187 | /// heuristic is used to improve diagnostics in the case where an |
| 1188 | /// out-of-line member function definition doesn't match any |
| 1189 | /// declaration within the class. |
| 1190 | static bool isNearlyMatchingMemberFunction(ASTContext &Context, |
| 1191 | FunctionDecl *Declaration, |
| 1192 | FunctionDecl *Definition) { |
| 1193 | if (Declaration->param_size() != Definition->param_size()) |
| 1194 | return false; |
| 1195 | for (unsigned Idx = 0; Idx < Declaration->param_size(); ++Idx) { |
| 1196 | QualType DeclParamTy = Declaration->getParamDecl(Idx)->getType(); |
| 1197 | QualType DefParamTy = Definition->getParamDecl(Idx)->getType(); |
| 1198 | |
| 1199 | DeclParamTy = Context.getCanonicalType(DeclParamTy.getNonReferenceType()); |
| 1200 | DefParamTy = Context.getCanonicalType(DefParamTy.getNonReferenceType()); |
| 1201 | if (DeclParamTy.getUnqualifiedType() != DefParamTy.getUnqualifiedType()) |
| 1202 | return false; |
| 1203 | } |
| 1204 | |
| 1205 | return true; |
| 1206 | } |
| 1207 | |
Fariborz Jahanian | 306d68f | 2007-11-08 23:49:49 +0000 | [diff] [blame] | 1208 | Sema::DeclTy * |
Douglas Gregor | 584049d | 2008-12-15 23:53:10 +0000 | [diff] [blame] | 1209 | Sema::ActOnDeclarator(Scope *S, Declarator &D, DeclTy *lastDecl, |
| 1210 | bool IsFunctionDefinition) { |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1211 | NamedDecl *LastDeclarator = dyn_cast_or_null<NamedDecl>((Decl *)lastDecl); |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 1212 | DeclarationName Name = GetNameForDeclarator(D); |
| 1213 | |
Chris Lattner | e80a59c | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1214 | // All of these full declarators require an identifier. If it doesn't have |
| 1215 | // one, the ParsedFreeStandingDeclSpec action should be used. |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 1216 | if (!Name) { |
Chris Lattner | 1f6f54b | 2008-11-11 06:13:16 +0000 | [diff] [blame] | 1217 | if (!D.getInvalidType()) // Reject this if we think it is valid. |
| 1218 | Diag(D.getDeclSpec().getSourceRange().getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1219 | diag::err_declarator_need_ident) |
| 1220 | << D.getDeclSpec().getSourceRange() << D.getSourceRange(); |
Chris Lattner | e80a59c | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1221 | return 0; |
| 1222 | } |
| 1223 | |
Chris Lattner | 31e0572 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 1224 | // The scope passed in may not be a decl scope. Zip up the scope tree until |
| 1225 | // we find one that is. |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1226 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 1227 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
Chris Lattner | 31e0572 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 1228 | S = S->getParent(); |
| 1229 | |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 1230 | DeclContext *DC; |
| 1231 | Decl *PrevDecl; |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1232 | NamedDecl *New; |
Steve Naroff | 5912a35 | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 1233 | bool InvalidDecl = false; |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1234 | |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 1235 | // See if this is a redefinition of a variable in the same scope. |
| 1236 | if (!D.getCXXScopeSpec().isSet()) { |
| 1237 | DC = CurContext; |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 1238 | PrevDecl = LookupDecl(Name, Decl::IDNS_Ordinary, S); |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 1239 | } else { // Something like "int foo::x;" |
| 1240 | DC = static_cast<DeclContext*>(D.getCXXScopeSpec().getScopeRep()); |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 1241 | PrevDecl = LookupDecl(Name, Decl::IDNS_Ordinary, S, DC); |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 1242 | |
| 1243 | // C++ 7.3.1.2p2: |
| 1244 | // Members (including explicit specializations of templates) of a named |
| 1245 | // namespace can also be defined outside that namespace by explicit |
| 1246 | // qualification of the name being defined, provided that the entity being |
| 1247 | // defined was already declared in the namespace and the definition appears |
| 1248 | // after the point of declaration in a namespace that encloses the |
| 1249 | // declarations namespace. |
| 1250 | // |
Douglas Gregor | 584049d | 2008-12-15 23:53:10 +0000 | [diff] [blame] | 1251 | // Note that we only check the context at this point. We don't yet |
| 1252 | // have enough information to make sure that PrevDecl is actually |
| 1253 | // the declaration we want to match. For example, given: |
| 1254 | // |
Douglas Gregor | 9d35097 | 2008-12-12 08:25:50 +0000 | [diff] [blame] | 1255 | // class X { |
| 1256 | // void f(); |
Douglas Gregor | 584049d | 2008-12-15 23:53:10 +0000 | [diff] [blame] | 1257 | // void f(float); |
Douglas Gregor | 9d35097 | 2008-12-12 08:25:50 +0000 | [diff] [blame] | 1258 | // }; |
| 1259 | // |
Douglas Gregor | 584049d | 2008-12-15 23:53:10 +0000 | [diff] [blame] | 1260 | // void X::f(int) { } // ill-formed |
| 1261 | // |
| 1262 | // In this case, PrevDecl will point to the overload set |
| 1263 | // containing the two f's declared in X, but neither of them |
| 1264 | // matches. |
| 1265 | if (!CurContext->Encloses(DC)) { |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 1266 | // The qualifying scope doesn't enclose the original declaration. |
| 1267 | // Emit diagnostic based on current scope. |
| 1268 | SourceLocation L = D.getIdentifierLoc(); |
| 1269 | SourceRange R = D.getCXXScopeSpec().getRange(); |
| 1270 | if (isa<FunctionDecl>(CurContext)) { |
Chris Lattner | 011bb4e | 2008-11-23 20:28:15 +0000 | [diff] [blame] | 1271 | Diag(L, diag::err_invalid_declarator_in_function) << Name << R; |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 1272 | } else { |
Chris Lattner | 011bb4e | 2008-11-23 20:28:15 +0000 | [diff] [blame] | 1273 | Diag(L, diag::err_invalid_declarator_scope) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1274 | << Name << cast<NamedDecl>(DC)->getDeclName() << R; |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 1275 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1276 | InvalidDecl = true; |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 1277 | } |
| 1278 | } |
| 1279 | |
Douglas Gregor | f57172b | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 1280 | if (PrevDecl && PrevDecl->isTemplateParameter()) { |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 1281 | // Maybe we will complain about the shadowed template parameter. |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1282 | InvalidDecl = InvalidDecl |
| 1283 | || DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), PrevDecl); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 1284 | // Just pretend that we didn't see the previous declaration. |
| 1285 | PrevDecl = 0; |
| 1286 | } |
| 1287 | |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1288 | // In C++, the previous declaration we find might be a tag type |
| 1289 | // (class or enum). In this case, the new declaration will hide the |
Douglas Gregor | 6697312 | 2009-01-28 17:15:10 +0000 | [diff] [blame] | 1290 | // tag type. Note that this does does not apply if we're declaring a |
| 1291 | // typedef (C++ [dcl.typedef]p4). |
| 1292 | if (PrevDecl && PrevDecl->getIdentifierNamespace() == Decl::IDNS_Tag && |
| 1293 | D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef) |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1294 | PrevDecl = 0; |
| 1295 | |
Chris Lattner | 41af093 | 2007-11-14 06:34:38 +0000 | [diff] [blame] | 1296 | QualType R = GetTypeForDeclarator(D, S); |
| 1297 | assert(!R.isNull() && "GetTypeForDeclarator() returned null type"); |
| 1298 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1299 | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) { |
Zhongxing Xu | d5ed8c3 | 2009-01-16 03:34:13 +0000 | [diff] [blame] | 1300 | New = ActOnTypedefDeclarator(S, D, DC, R, LastDeclarator, PrevDecl, |
| 1301 | InvalidDecl); |
Chris Lattner | 41af093 | 2007-11-14 06:34:38 +0000 | [diff] [blame] | 1302 | } else if (R.getTypePtr()->isFunctionType()) { |
Zhongxing Xu | 416fcaf | 2009-01-16 01:13:29 +0000 | [diff] [blame] | 1303 | New = ActOnFunctionDeclarator(S, D, DC, R, LastDeclarator, PrevDecl, |
| 1304 | IsFunctionDefinition, InvalidDecl); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1305 | } else { |
Zhongxing Xu | cb8f4f1 | 2009-01-16 02:36:34 +0000 | [diff] [blame] | 1306 | New = ActOnVariableDeclarator(S, D, DC, R, LastDeclarator, PrevDecl, |
| 1307 | InvalidDecl); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1308 | } |
Zhongxing Xu | 416fcaf | 2009-01-16 01:13:29 +0000 | [diff] [blame] | 1309 | |
| 1310 | if (New == 0) |
| 1311 | return 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1312 | |
Argyrios Kyrtzidis | 5239304 | 2008-11-09 23:41:00 +0000 | [diff] [blame] | 1313 | // Set the lexical context. If the declarator has a C++ scope specifier, the |
| 1314 | // lexical context will be different from the semantic context. |
| 1315 | New->setLexicalDeclContext(CurContext); |
| 1316 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1317 | // If this has an identifier, add it to the scope stack. |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 1318 | if (Name) |
Argyrios Kyrtzidis | 87f3ff0 | 2008-04-12 00:47:19 +0000 | [diff] [blame] | 1319 | PushOnScopeChains(New, S); |
Steve Naroff | 5912a35 | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 1320 | // If any semantic error occurred, mark the decl as invalid. |
| 1321 | if (D.getInvalidType() || InvalidDecl) |
| 1322 | New->setInvalidDecl(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1323 | |
| 1324 | return New; |
| 1325 | } |
| 1326 | |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1327 | NamedDecl* |
Zhongxing Xu | d5ed8c3 | 2009-01-16 03:34:13 +0000 | [diff] [blame] | 1328 | Sema::ActOnTypedefDeclarator(Scope* S, Declarator& D, DeclContext* DC, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1329 | QualType R, Decl* LastDeclarator, |
Zhongxing Xu | d5ed8c3 | 2009-01-16 03:34:13 +0000 | [diff] [blame] | 1330 | Decl* PrevDecl, bool& InvalidDecl) { |
| 1331 | // Typedef declarators cannot be qualified (C++ [dcl.meaning]p1). |
| 1332 | if (D.getCXXScopeSpec().isSet()) { |
| 1333 | Diag(D.getIdentifierLoc(), diag::err_qualified_typedef_declarator) |
| 1334 | << D.getCXXScopeSpec().getRange(); |
| 1335 | InvalidDecl = true; |
| 1336 | // Pretend we didn't see the scope specifier. |
| 1337 | DC = 0; |
| 1338 | } |
| 1339 | |
| 1340 | // Check that there are no default arguments (C++ only). |
| 1341 | if (getLangOptions().CPlusPlus) |
| 1342 | CheckExtraCXXDefaultArguments(D); |
| 1343 | |
| 1344 | TypedefDecl *NewTD = ParseTypedefDecl(S, D, R, LastDeclarator); |
| 1345 | if (!NewTD) return 0; |
| 1346 | |
| 1347 | // Handle attributes prior to checking for duplicates in MergeVarDecl |
| 1348 | ProcessDeclAttributes(NewTD, D); |
| 1349 | // Merge the decl with the existing one if appropriate. If the decl is |
| 1350 | // in an outer scope, it isn't the same thing. |
| 1351 | if (PrevDecl && isDeclInScope(PrevDecl, DC, S)) { |
| 1352 | NewTD = MergeTypeDefDecl(NewTD, PrevDecl); |
| 1353 | if (NewTD == 0) return 0; |
| 1354 | } |
| 1355 | |
| 1356 | if (S->getFnParent() == 0) { |
| 1357 | // C99 6.7.7p2: If a typedef name specifies a variably modified type |
| 1358 | // then it shall have block scope. |
| 1359 | if (NewTD->getUnderlyingType()->isVariablyModifiedType()) { |
| 1360 | if (NewTD->getUnderlyingType()->isVariableArrayType()) |
| 1361 | Diag(D.getIdentifierLoc(), diag::err_vla_decl_in_file_scope); |
| 1362 | else |
| 1363 | Diag(D.getIdentifierLoc(), diag::err_vm_decl_in_file_scope); |
| 1364 | |
| 1365 | InvalidDecl = true; |
| 1366 | } |
| 1367 | } |
| 1368 | return NewTD; |
| 1369 | } |
| 1370 | |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1371 | NamedDecl* |
Zhongxing Xu | cb8f4f1 | 2009-01-16 02:36:34 +0000 | [diff] [blame] | 1372 | Sema::ActOnVariableDeclarator(Scope* S, Declarator& D, DeclContext* DC, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1373 | QualType R, Decl* LastDeclarator, |
Zhongxing Xu | cb8f4f1 | 2009-01-16 02:36:34 +0000 | [diff] [blame] | 1374 | Decl* PrevDecl, bool& InvalidDecl) { |
| 1375 | DeclarationName Name = GetNameForDeclarator(D); |
| 1376 | |
| 1377 | // Check that there are no default arguments (C++ only). |
| 1378 | if (getLangOptions().CPlusPlus) |
| 1379 | CheckExtraCXXDefaultArguments(D); |
| 1380 | |
| 1381 | if (R.getTypePtr()->isObjCInterfaceType()) { |
| 1382 | Diag(D.getIdentifierLoc(), diag::err_statically_allocated_object) |
| 1383 | << D.getIdentifier(); |
| 1384 | InvalidDecl = true; |
| 1385 | } |
| 1386 | |
| 1387 | VarDecl *NewVD; |
| 1388 | VarDecl::StorageClass SC; |
| 1389 | switch (D.getDeclSpec().getStorageClassSpec()) { |
| 1390 | default: assert(0 && "Unknown storage class!"); |
| 1391 | case DeclSpec::SCS_unspecified: SC = VarDecl::None; break; |
| 1392 | case DeclSpec::SCS_extern: SC = VarDecl::Extern; break; |
| 1393 | case DeclSpec::SCS_static: SC = VarDecl::Static; break; |
| 1394 | case DeclSpec::SCS_auto: SC = VarDecl::Auto; break; |
| 1395 | case DeclSpec::SCS_register: SC = VarDecl::Register; break; |
| 1396 | case DeclSpec::SCS_private_extern: SC = VarDecl::PrivateExtern; break; |
| 1397 | case DeclSpec::SCS_mutable: |
| 1398 | // mutable can only appear on non-static class members, so it's always |
| 1399 | // an error here |
| 1400 | Diag(D.getIdentifierLoc(), diag::err_mutable_nonmember); |
| 1401 | InvalidDecl = true; |
| 1402 | SC = VarDecl::None; |
| 1403 | break; |
| 1404 | } |
| 1405 | |
| 1406 | IdentifierInfo *II = Name.getAsIdentifierInfo(); |
| 1407 | if (!II) { |
| 1408 | Diag(D.getIdentifierLoc(), diag::err_bad_variable_name) |
| 1409 | << Name.getAsString(); |
| 1410 | return 0; |
| 1411 | } |
| 1412 | |
| 1413 | if (DC->isRecord()) { |
| 1414 | // This is a static data member for a C++ class. |
| 1415 | NewVD = CXXClassVarDecl::Create(Context, cast<CXXRecordDecl>(DC), |
| 1416 | D.getIdentifierLoc(), II, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1417 | R); |
Zhongxing Xu | cb8f4f1 | 2009-01-16 02:36:34 +0000 | [diff] [blame] | 1418 | } else { |
| 1419 | bool ThreadSpecified = D.getDeclSpec().isThreadSpecified(); |
| 1420 | if (S->getFnParent() == 0) { |
| 1421 | // C99 6.9p2: The storage-class specifiers auto and register shall not |
| 1422 | // appear in the declaration specifiers in an external declaration. |
| 1423 | if (SC == VarDecl::Auto || SC == VarDecl::Register) { |
| 1424 | Diag(D.getIdentifierLoc(), diag::err_typecheck_sclass_fscope); |
| 1425 | InvalidDecl = true; |
| 1426 | } |
| 1427 | } |
| 1428 | NewVD = VarDecl::Create(Context, DC, D.getIdentifierLoc(), |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1429 | II, R, SC, |
Zhongxing Xu | cb8f4f1 | 2009-01-16 02:36:34 +0000 | [diff] [blame] | 1430 | // FIXME: Move to DeclGroup... |
| 1431 | D.getDeclSpec().getSourceRange().getBegin()); |
| 1432 | NewVD->setThreadSpecified(ThreadSpecified); |
| 1433 | } |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1434 | NewVD->setNextDeclarator(LastDeclarator); |
| 1435 | |
Zhongxing Xu | cb8f4f1 | 2009-01-16 02:36:34 +0000 | [diff] [blame] | 1436 | // Handle attributes prior to checking for duplicates in MergeVarDecl |
| 1437 | ProcessDeclAttributes(NewVD, D); |
| 1438 | |
| 1439 | // Handle GNU asm-label extension (encoded as an attribute). |
| 1440 | if (Expr *E = (Expr*) D.getAsmLabel()) { |
| 1441 | // The parser guarantees this is a string. |
| 1442 | StringLiteral *SE = cast<StringLiteral>(E); |
| 1443 | NewVD->addAttr(new AsmLabelAttr(std::string(SE->getStrData(), |
| 1444 | SE->getByteLength()))); |
| 1445 | } |
| 1446 | |
| 1447 | // Emit an error if an address space was applied to decl with local storage. |
| 1448 | // This includes arrays of objects with address space qualifiers, but not |
| 1449 | // automatic variables that point to other address spaces. |
| 1450 | // ISO/IEC TR 18037 S5.1.2 |
| 1451 | if (NewVD->hasLocalStorage() && (NewVD->getType().getAddressSpace() != 0)) { |
| 1452 | Diag(D.getIdentifierLoc(), diag::err_as_qualified_auto_decl); |
| 1453 | InvalidDecl = true; |
| 1454 | } |
| 1455 | // Merge the decl with the existing one if appropriate. If the decl is |
| 1456 | // in an outer scope, it isn't the same thing. |
| 1457 | if (PrevDecl && isDeclInScope(PrevDecl, DC, S)) { |
| 1458 | if (isa<FieldDecl>(PrevDecl) && D.getCXXScopeSpec().isSet()) { |
| 1459 | // The user tried to define a non-static data member |
| 1460 | // out-of-line (C++ [dcl.meaning]p1). |
| 1461 | Diag(NewVD->getLocation(), diag::err_nonstatic_member_out_of_line) |
| 1462 | << D.getCXXScopeSpec().getRange(); |
| 1463 | NewVD->Destroy(Context); |
| 1464 | return 0; |
| 1465 | } |
| 1466 | |
| 1467 | NewVD = MergeVarDecl(NewVD, PrevDecl); |
| 1468 | if (NewVD == 0) return 0; |
| 1469 | |
| 1470 | if (D.getCXXScopeSpec().isSet()) { |
| 1471 | // No previous declaration in the qualifying scope. |
| 1472 | Diag(D.getIdentifierLoc(), diag::err_typecheck_no_member) |
| 1473 | << Name << D.getCXXScopeSpec().getRange(); |
| 1474 | InvalidDecl = true; |
| 1475 | } |
| 1476 | } |
| 1477 | return NewVD; |
| 1478 | } |
| 1479 | |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1480 | NamedDecl* |
Zhongxing Xu | 416fcaf | 2009-01-16 01:13:29 +0000 | [diff] [blame] | 1481 | Sema::ActOnFunctionDeclarator(Scope* S, Declarator& D, DeclContext* DC, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1482 | QualType R, Decl *LastDeclarator, |
Zhongxing Xu | 416fcaf | 2009-01-16 01:13:29 +0000 | [diff] [blame] | 1483 | Decl* PrevDecl, bool IsFunctionDefinition, |
| 1484 | bool& InvalidDecl) { |
| 1485 | assert(R.getTypePtr()->isFunctionType()); |
| 1486 | |
| 1487 | DeclarationName Name = GetNameForDeclarator(D); |
| 1488 | FunctionDecl::StorageClass SC = FunctionDecl::None; |
| 1489 | switch (D.getDeclSpec().getStorageClassSpec()) { |
| 1490 | default: assert(0 && "Unknown storage class!"); |
| 1491 | case DeclSpec::SCS_auto: |
| 1492 | case DeclSpec::SCS_register: |
| 1493 | case DeclSpec::SCS_mutable: |
| 1494 | Diag(D.getIdentifierLoc(), diag::err_typecheck_sclass_func); |
| 1495 | InvalidDecl = true; |
| 1496 | break; |
| 1497 | case DeclSpec::SCS_unspecified: SC = FunctionDecl::None; break; |
| 1498 | case DeclSpec::SCS_extern: SC = FunctionDecl::Extern; break; |
| 1499 | case DeclSpec::SCS_static: SC = FunctionDecl::Static; break; |
| 1500 | case DeclSpec::SCS_private_extern: SC = FunctionDecl::PrivateExtern;break; |
| 1501 | } |
| 1502 | |
| 1503 | bool isInline = D.getDeclSpec().isInlineSpecified(); |
| 1504 | // bool isVirtual = D.getDeclSpec().isVirtualSpecified(); |
| 1505 | bool isExplicit = D.getDeclSpec().isExplicitSpecified(); |
| 1506 | |
| 1507 | FunctionDecl *NewFD; |
| 1508 | if (D.getKind() == Declarator::DK_Constructor) { |
| 1509 | // This is a C++ constructor declaration. |
| 1510 | assert(DC->isRecord() && |
| 1511 | "Constructors can only be declared in a member context"); |
| 1512 | |
| 1513 | InvalidDecl = InvalidDecl || CheckConstructorDeclarator(D, R, SC); |
| 1514 | |
| 1515 | // Create the new declaration |
| 1516 | NewFD = CXXConstructorDecl::Create(Context, |
| 1517 | cast<CXXRecordDecl>(DC), |
| 1518 | D.getIdentifierLoc(), Name, R, |
| 1519 | isExplicit, isInline, |
| 1520 | /*isImplicitlyDeclared=*/false); |
| 1521 | |
| 1522 | if (InvalidDecl) |
| 1523 | NewFD->setInvalidDecl(); |
| 1524 | } else if (D.getKind() == Declarator::DK_Destructor) { |
| 1525 | // This is a C++ destructor declaration. |
| 1526 | if (DC->isRecord()) { |
| 1527 | InvalidDecl = InvalidDecl || CheckDestructorDeclarator(D, R, SC); |
| 1528 | |
| 1529 | NewFD = CXXDestructorDecl::Create(Context, |
| 1530 | cast<CXXRecordDecl>(DC), |
| 1531 | D.getIdentifierLoc(), Name, R, |
| 1532 | isInline, |
| 1533 | /*isImplicitlyDeclared=*/false); |
| 1534 | |
| 1535 | if (InvalidDecl) |
| 1536 | NewFD->setInvalidDecl(); |
| 1537 | } else { |
| 1538 | Diag(D.getIdentifierLoc(), diag::err_destructor_not_member); |
| 1539 | |
| 1540 | // Create a FunctionDecl to satisfy the function definition parsing |
| 1541 | // code path. |
| 1542 | NewFD = FunctionDecl::Create(Context, DC, D.getIdentifierLoc(), |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1543 | Name, R, SC, isInline, |
Zhongxing Xu | 416fcaf | 2009-01-16 01:13:29 +0000 | [diff] [blame] | 1544 | // FIXME: Move to DeclGroup... |
| 1545 | D.getDeclSpec().getSourceRange().getBegin()); |
| 1546 | InvalidDecl = true; |
| 1547 | NewFD->setInvalidDecl(); |
| 1548 | } |
| 1549 | } else if (D.getKind() == Declarator::DK_Conversion) { |
| 1550 | if (!DC->isRecord()) { |
| 1551 | Diag(D.getIdentifierLoc(), |
| 1552 | diag::err_conv_function_not_member); |
| 1553 | return 0; |
| 1554 | } else { |
| 1555 | InvalidDecl = InvalidDecl || CheckConversionDeclarator(D, R, SC); |
| 1556 | |
| 1557 | NewFD = CXXConversionDecl::Create(Context, cast<CXXRecordDecl>(DC), |
| 1558 | D.getIdentifierLoc(), Name, R, |
| 1559 | isInline, isExplicit); |
| 1560 | |
| 1561 | if (InvalidDecl) |
| 1562 | NewFD->setInvalidDecl(); |
| 1563 | } |
| 1564 | } else if (DC->isRecord()) { |
| 1565 | // This is a C++ method declaration. |
| 1566 | NewFD = CXXMethodDecl::Create(Context, cast<CXXRecordDecl>(DC), |
| 1567 | D.getIdentifierLoc(), Name, R, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1568 | (SC == FunctionDecl::Static), isInline); |
Zhongxing Xu | 416fcaf | 2009-01-16 01:13:29 +0000 | [diff] [blame] | 1569 | } else { |
| 1570 | NewFD = FunctionDecl::Create(Context, DC, |
| 1571 | D.getIdentifierLoc(), |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1572 | Name, R, SC, isInline, |
Zhongxing Xu | 416fcaf | 2009-01-16 01:13:29 +0000 | [diff] [blame] | 1573 | // FIXME: Move to DeclGroup... |
| 1574 | D.getDeclSpec().getSourceRange().getBegin()); |
| 1575 | } |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1576 | NewFD->setNextDeclarator(LastDeclarator); |
Zhongxing Xu | 416fcaf | 2009-01-16 01:13:29 +0000 | [diff] [blame] | 1577 | |
| 1578 | // Set the lexical context. If the declarator has a C++ |
| 1579 | // scope specifier, the lexical context will be different |
| 1580 | // from the semantic context. |
| 1581 | NewFD->setLexicalDeclContext(CurContext); |
| 1582 | |
| 1583 | // Handle GNU asm-label extension (encoded as an attribute). |
| 1584 | if (Expr *E = (Expr*) D.getAsmLabel()) { |
| 1585 | // The parser guarantees this is a string. |
| 1586 | StringLiteral *SE = cast<StringLiteral>(E); |
| 1587 | NewFD->addAttr(new AsmLabelAttr(std::string(SE->getStrData(), |
| 1588 | SE->getByteLength()))); |
| 1589 | } |
| 1590 | |
| 1591 | // Copy the parameter declarations from the declarator D to |
| 1592 | // the function declaration NewFD, if they are available. |
| 1593 | if (D.getNumTypeObjects() > 0) { |
| 1594 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun; |
| 1595 | |
| 1596 | // Create Decl objects for each parameter, adding them to the |
| 1597 | // FunctionDecl. |
| 1598 | llvm::SmallVector<ParmVarDecl*, 16> Params; |
| 1599 | |
| 1600 | // Check for C99 6.7.5.3p10 - foo(void) is a non-varargs |
| 1601 | // function that takes no arguments, not a function that takes a |
| 1602 | // single void argument. |
| 1603 | // We let through "const void" here because Sema::GetTypeForDeclarator |
| 1604 | // already checks for that case. |
| 1605 | if (FTI.NumArgs == 1 && !FTI.isVariadic && FTI.ArgInfo[0].Ident == 0 && |
| 1606 | FTI.ArgInfo[0].Param && |
| 1607 | ((ParmVarDecl*)FTI.ArgInfo[0].Param)->getType()->isVoidType()) { |
| 1608 | // empty arg list, don't push any params. |
| 1609 | ParmVarDecl *Param = (ParmVarDecl*)FTI.ArgInfo[0].Param; |
| 1610 | |
| 1611 | // In C++, the empty parameter-type-list must be spelled "void"; a |
| 1612 | // typedef of void is not permitted. |
| 1613 | if (getLangOptions().CPlusPlus && |
| 1614 | Param->getType().getUnqualifiedType() != Context.VoidTy) { |
| 1615 | Diag(Param->getLocation(), diag::ext_param_typedef_of_void); |
| 1616 | } |
| 1617 | } else if (FTI.NumArgs > 0 && FTI.ArgInfo[0].Param != 0) { |
| 1618 | for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) |
| 1619 | Params.push_back((ParmVarDecl *)FTI.ArgInfo[i].Param); |
| 1620 | } |
| 1621 | |
| 1622 | NewFD->setParams(Context, &Params[0], Params.size()); |
| 1623 | } else if (R->getAsTypedefType()) { |
| 1624 | // When we're declaring a function with a typedef, as in the |
| 1625 | // following example, we'll need to synthesize (unnamed) |
| 1626 | // parameters for use in the declaration. |
| 1627 | // |
| 1628 | // @code |
| 1629 | // typedef void fn(int); |
| 1630 | // fn f; |
| 1631 | // @endcode |
| 1632 | const FunctionTypeProto *FT = R->getAsFunctionTypeProto(); |
| 1633 | if (!FT) { |
| 1634 | // This is a typedef of a function with no prototype, so we |
| 1635 | // don't need to do anything. |
| 1636 | } else if ((FT->getNumArgs() == 0) || |
| 1637 | (FT->getNumArgs() == 1 && !FT->isVariadic() && |
| 1638 | FT->getArgType(0)->isVoidType())) { |
| 1639 | // This is a zero-argument function. We don't need to do anything. |
| 1640 | } else { |
| 1641 | // Synthesize a parameter for each argument type. |
| 1642 | llvm::SmallVector<ParmVarDecl*, 16> Params; |
| 1643 | for (FunctionTypeProto::arg_type_iterator ArgType = FT->arg_type_begin(); |
| 1644 | ArgType != FT->arg_type_end(); ++ArgType) { |
| 1645 | Params.push_back(ParmVarDecl::Create(Context, DC, |
| 1646 | SourceLocation(), 0, |
| 1647 | *ArgType, VarDecl::None, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1648 | 0)); |
Zhongxing Xu | 416fcaf | 2009-01-16 01:13:29 +0000 | [diff] [blame] | 1649 | } |
| 1650 | |
| 1651 | NewFD->setParams(Context, &Params[0], Params.size()); |
| 1652 | } |
| 1653 | } |
| 1654 | |
| 1655 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(NewFD)) |
| 1656 | InvalidDecl = InvalidDecl || CheckConstructor(Constructor); |
| 1657 | else if (isa<CXXDestructorDecl>(NewFD)) { |
| 1658 | CXXRecordDecl *Record = cast<CXXRecordDecl>(NewFD->getParent()); |
| 1659 | Record->setUserDeclaredDestructor(true); |
| 1660 | // C++ [class]p4: A POD-struct is an aggregate class that has [...] no |
| 1661 | // user-defined destructor. |
| 1662 | Record->setPOD(false); |
| 1663 | } else if (CXXConversionDecl *Conversion = |
| 1664 | dyn_cast<CXXConversionDecl>(NewFD)) |
| 1665 | ActOnConversionDeclarator(Conversion); |
| 1666 | |
| 1667 | // Extra checking for C++ overloaded operators (C++ [over.oper]). |
| 1668 | if (NewFD->isOverloadedOperator() && |
| 1669 | CheckOverloadedOperatorDeclaration(NewFD)) |
| 1670 | NewFD->setInvalidDecl(); |
| 1671 | |
| 1672 | // Merge the decl with the existing one if appropriate. Since C functions |
| 1673 | // are in a flat namespace, make sure we consider decls in outer scopes. |
| 1674 | if (PrevDecl && |
| 1675 | (!getLangOptions().CPlusPlus||isDeclInScope(PrevDecl, DC, S))) { |
| 1676 | bool Redeclaration = false; |
| 1677 | |
| 1678 | // If C++, determine whether NewFD is an overload of PrevDecl or |
| 1679 | // a declaration that requires merging. If it's an overload, |
| 1680 | // there's no more work to do here; we'll just add the new |
| 1681 | // function to the scope. |
| 1682 | OverloadedFunctionDecl::function_iterator MatchedDecl; |
| 1683 | if (!getLangOptions().CPlusPlus || |
| 1684 | !IsOverload(NewFD, PrevDecl, MatchedDecl)) { |
| 1685 | Decl *OldDecl = PrevDecl; |
| 1686 | |
| 1687 | // If PrevDecl was an overloaded function, extract the |
| 1688 | // FunctionDecl that matched. |
| 1689 | if (isa<OverloadedFunctionDecl>(PrevDecl)) |
| 1690 | OldDecl = *MatchedDecl; |
| 1691 | |
| 1692 | // NewFD and PrevDecl represent declarations that need to be |
| 1693 | // merged. |
| 1694 | NewFD = MergeFunctionDecl(NewFD, OldDecl, Redeclaration); |
| 1695 | |
| 1696 | if (NewFD == 0) return 0; |
| 1697 | if (Redeclaration) { |
| 1698 | NewFD->setPreviousDeclaration(cast<FunctionDecl>(OldDecl)); |
| 1699 | |
| 1700 | // An out-of-line member function declaration must also be a |
| 1701 | // definition (C++ [dcl.meaning]p1). |
| 1702 | if (!IsFunctionDefinition && D.getCXXScopeSpec().isSet() && |
| 1703 | !InvalidDecl) { |
| 1704 | Diag(NewFD->getLocation(), diag::err_out_of_line_declaration) |
| 1705 | << D.getCXXScopeSpec().getRange(); |
| 1706 | NewFD->setInvalidDecl(); |
| 1707 | } |
| 1708 | } |
| 1709 | } |
| 1710 | |
| 1711 | if (!Redeclaration && D.getCXXScopeSpec().isSet()) { |
| 1712 | // The user tried to provide an out-of-line definition for a |
| 1713 | // member function, but there was no such member function |
| 1714 | // declared (C++ [class.mfct]p2). For example: |
| 1715 | // |
| 1716 | // class X { |
| 1717 | // void f() const; |
| 1718 | // }; |
| 1719 | // |
| 1720 | // void X::f() { } // ill-formed |
| 1721 | // |
| 1722 | // Complain about this problem, and attempt to suggest close |
| 1723 | // matches (e.g., those that differ only in cv-qualifiers and |
| 1724 | // whether the parameter types are references). |
| 1725 | Diag(D.getIdentifierLoc(), diag::err_member_def_does_not_match) |
| 1726 | << cast<CXXRecordDecl>(DC)->getDeclName() |
| 1727 | << D.getCXXScopeSpec().getRange(); |
| 1728 | InvalidDecl = true; |
| 1729 | |
| 1730 | PrevDecl = LookupDecl(Name, Decl::IDNS_Ordinary, S, DC); |
| 1731 | if (!PrevDecl) { |
| 1732 | // Nothing to suggest. |
| 1733 | } else if (OverloadedFunctionDecl *Ovl |
| 1734 | = dyn_cast<OverloadedFunctionDecl>(PrevDecl)) { |
| 1735 | for (OverloadedFunctionDecl::function_iterator |
| 1736 | Func = Ovl->function_begin(), |
| 1737 | FuncEnd = Ovl->function_end(); |
| 1738 | Func != FuncEnd; ++Func) { |
| 1739 | if (isNearlyMatchingMemberFunction(Context, *Func, NewFD)) |
| 1740 | Diag((*Func)->getLocation(), diag::note_member_def_close_match); |
| 1741 | |
| 1742 | } |
| 1743 | } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(PrevDecl)) { |
| 1744 | // Suggest this no matter how mismatched it is; it's the only |
| 1745 | // thing we have. |
| 1746 | unsigned diag; |
| 1747 | if (isNearlyMatchingMemberFunction(Context, Method, NewFD)) |
| 1748 | diag = diag::note_member_def_close_match; |
| 1749 | else if (Method->getBody()) |
| 1750 | diag = diag::note_previous_definition; |
| 1751 | else |
| 1752 | diag = diag::note_previous_declaration; |
| 1753 | Diag(Method->getLocation(), diag); |
| 1754 | } |
| 1755 | |
| 1756 | PrevDecl = 0; |
| 1757 | } |
| 1758 | } |
| 1759 | // Handle attributes. We need to have merged decls when handling attributes |
| 1760 | // (for example to check for conflicts, etc). |
| 1761 | ProcessDeclAttributes(NewFD, D); |
| 1762 | |
| 1763 | if (getLangOptions().CPlusPlus) { |
| 1764 | // In C++, check default arguments now that we have merged decls. |
| 1765 | CheckCXXDefaultArguments(NewFD); |
| 1766 | |
| 1767 | // An out-of-line member function declaration must also be a |
| 1768 | // definition (C++ [dcl.meaning]p1). |
| 1769 | if (!IsFunctionDefinition && D.getCXXScopeSpec().isSet() && !InvalidDecl) { |
| 1770 | Diag(NewFD->getLocation(), diag::err_out_of_line_declaration) |
| 1771 | << D.getCXXScopeSpec().getRange(); |
| 1772 | InvalidDecl = true; |
| 1773 | } |
| 1774 | } |
| 1775 | return NewFD; |
| 1776 | } |
| 1777 | |
Steve Naroff | 6594a70 | 2008-10-27 11:34:16 +0000 | [diff] [blame] | 1778 | void Sema::InitializerElementNotConstant(const Expr *Init) { |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 1779 | Diag(Init->getExprLoc(), diag::err_init_element_not_constant) |
| 1780 | << Init->getSourceRange(); |
Steve Naroff | 6594a70 | 2008-10-27 11:34:16 +0000 | [diff] [blame] | 1781 | } |
| 1782 | |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1783 | bool Sema::CheckAddressConstantExpressionLValue(const Expr* Init) { |
| 1784 | switch (Init->getStmtClass()) { |
| 1785 | default: |
Steve Naroff | 6594a70 | 2008-10-27 11:34:16 +0000 | [diff] [blame] | 1786 | InitializerElementNotConstant(Init); |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1787 | return true; |
| 1788 | case Expr::ParenExprClass: { |
| 1789 | const ParenExpr* PE = cast<ParenExpr>(Init); |
| 1790 | return CheckAddressConstantExpressionLValue(PE->getSubExpr()); |
| 1791 | } |
| 1792 | case Expr::CompoundLiteralExprClass: |
| 1793 | return cast<CompoundLiteralExpr>(Init)->isFileScope(); |
Douglas Gregor | 1a49af9 | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 1794 | case Expr::DeclRefExprClass: |
| 1795 | case Expr::QualifiedDeclRefExprClass: { |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1796 | const Decl *D = cast<DeclRefExpr>(Init)->getDecl(); |
Eli Friedman | 97c0a39 | 2008-05-21 03:39:11 +0000 | [diff] [blame] | 1797 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 1798 | if (VD->hasGlobalStorage()) |
| 1799 | return false; |
Steve Naroff | 6594a70 | 2008-10-27 11:34:16 +0000 | [diff] [blame] | 1800 | InitializerElementNotConstant(Init); |
Eli Friedman | 97c0a39 | 2008-05-21 03:39:11 +0000 | [diff] [blame] | 1801 | return true; |
| 1802 | } |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1803 | if (isa<FunctionDecl>(D)) |
| 1804 | return false; |
Steve Naroff | 6594a70 | 2008-10-27 11:34:16 +0000 | [diff] [blame] | 1805 | InitializerElementNotConstant(Init); |
Steve Naroff | d0091aa | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 1806 | return true; |
| 1807 | } |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1808 | case Expr::MemberExprClass: { |
| 1809 | const MemberExpr *M = cast<MemberExpr>(Init); |
| 1810 | if (M->isArrow()) |
| 1811 | return CheckAddressConstantExpression(M->getBase()); |
| 1812 | return CheckAddressConstantExpressionLValue(M->getBase()); |
| 1813 | } |
| 1814 | case Expr::ArraySubscriptExprClass: { |
| 1815 | // FIXME: Should we pedwarn for "x[0+0]" (where x is a pointer)? |
| 1816 | const ArraySubscriptExpr *ASE = cast<ArraySubscriptExpr>(Init); |
| 1817 | return CheckAddressConstantExpression(ASE->getBase()) || |
| 1818 | CheckArithmeticConstantExpression(ASE->getIdx()); |
| 1819 | } |
| 1820 | case Expr::StringLiteralClass: |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 1821 | case Expr::PredefinedExprClass: |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1822 | return false; |
| 1823 | case Expr::UnaryOperatorClass: { |
| 1824 | const UnaryOperator *Exp = cast<UnaryOperator>(Init); |
| 1825 | |
| 1826 | // C99 6.6p9 |
| 1827 | if (Exp->getOpcode() == UnaryOperator::Deref) |
Eli Friedman | 97c0a39 | 2008-05-21 03:39:11 +0000 | [diff] [blame] | 1828 | return CheckAddressConstantExpression(Exp->getSubExpr()); |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1829 | |
Steve Naroff | 6594a70 | 2008-10-27 11:34:16 +0000 | [diff] [blame] | 1830 | InitializerElementNotConstant(Init); |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1831 | return true; |
| 1832 | } |
| 1833 | } |
| 1834 | } |
| 1835 | |
| 1836 | bool Sema::CheckAddressConstantExpression(const Expr* Init) { |
| 1837 | switch (Init->getStmtClass()) { |
| 1838 | default: |
Steve Naroff | 6594a70 | 2008-10-27 11:34:16 +0000 | [diff] [blame] | 1839 | InitializerElementNotConstant(Init); |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1840 | return true; |
Chris Lattner | 506ff88 | 2008-10-06 07:26:43 +0000 | [diff] [blame] | 1841 | case Expr::ParenExprClass: |
| 1842 | return CheckAddressConstantExpression(cast<ParenExpr>(Init)->getSubExpr()); |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1843 | case Expr::StringLiteralClass: |
| 1844 | case Expr::ObjCStringLiteralClass: |
| 1845 | return false; |
Chris Lattner | 506ff88 | 2008-10-06 07:26:43 +0000 | [diff] [blame] | 1846 | case Expr::CallExprClass: |
Douglas Gregor | b460980 | 2008-11-14 16:09:21 +0000 | [diff] [blame] | 1847 | case Expr::CXXOperatorCallExprClass: |
Chris Lattner | 506ff88 | 2008-10-06 07:26:43 +0000 | [diff] [blame] | 1848 | // __builtin___CFStringMakeConstantString is a valid constant l-value. |
| 1849 | if (cast<CallExpr>(Init)->isBuiltinCall() == |
| 1850 | Builtin::BI__builtin___CFStringMakeConstantString) |
| 1851 | return false; |
| 1852 | |
Steve Naroff | 6594a70 | 2008-10-27 11:34:16 +0000 | [diff] [blame] | 1853 | InitializerElementNotConstant(Init); |
Chris Lattner | 506ff88 | 2008-10-06 07:26:43 +0000 | [diff] [blame] | 1854 | return true; |
| 1855 | |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1856 | case Expr::UnaryOperatorClass: { |
| 1857 | const UnaryOperator *Exp = cast<UnaryOperator>(Init); |
| 1858 | |
| 1859 | // C99 6.6p9 |
| 1860 | if (Exp->getOpcode() == UnaryOperator::AddrOf) |
| 1861 | return CheckAddressConstantExpressionLValue(Exp->getSubExpr()); |
| 1862 | |
| 1863 | if (Exp->getOpcode() == UnaryOperator::Extension) |
| 1864 | return CheckAddressConstantExpression(Exp->getSubExpr()); |
| 1865 | |
Steve Naroff | 6594a70 | 2008-10-27 11:34:16 +0000 | [diff] [blame] | 1866 | InitializerElementNotConstant(Init); |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1867 | return true; |
| 1868 | } |
| 1869 | case Expr::BinaryOperatorClass: { |
| 1870 | // FIXME: Should we pedwarn for expressions like "a + 1 + 2"? |
| 1871 | const BinaryOperator *Exp = cast<BinaryOperator>(Init); |
| 1872 | |
| 1873 | Expr *PExp = Exp->getLHS(); |
| 1874 | Expr *IExp = Exp->getRHS(); |
| 1875 | if (IExp->getType()->isPointerType()) |
| 1876 | std::swap(PExp, IExp); |
| 1877 | |
| 1878 | // FIXME: Should we pedwarn if IExp isn't an integer constant expression? |
| 1879 | return CheckAddressConstantExpression(PExp) || |
| 1880 | CheckArithmeticConstantExpression(IExp); |
| 1881 | } |
Eli Friedman | c3f0764 | 2008-08-25 20:46:57 +0000 | [diff] [blame] | 1882 | case Expr::ImplicitCastExprClass: |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 1883 | case Expr::CStyleCastExprClass: { |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1884 | const Expr* SubExpr = cast<CastExpr>(Init)->getSubExpr(); |
Eli Friedman | c3f0764 | 2008-08-25 20:46:57 +0000 | [diff] [blame] | 1885 | if (Init->getStmtClass() == Expr::ImplicitCastExprClass) { |
| 1886 | // Check for implicit promotion |
| 1887 | if (SubExpr->getType()->isFunctionType() || |
| 1888 | SubExpr->getType()->isArrayType()) |
| 1889 | return CheckAddressConstantExpressionLValue(SubExpr); |
| 1890 | } |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1891 | |
| 1892 | // Check for pointer->pointer cast |
| 1893 | if (SubExpr->getType()->isPointerType()) |
| 1894 | return CheckAddressConstantExpression(SubExpr); |
| 1895 | |
Eli Friedman | c3f0764 | 2008-08-25 20:46:57 +0000 | [diff] [blame] | 1896 | if (SubExpr->getType()->isIntegralType()) { |
| 1897 | // Check for the special-case of a pointer->int->pointer cast; |
| 1898 | // this isn't standard, but some code requires it. See |
| 1899 | // PR2720 for an example. |
| 1900 | if (const CastExpr* SubCast = dyn_cast<CastExpr>(SubExpr)) { |
| 1901 | if (SubCast->getSubExpr()->getType()->isPointerType()) { |
| 1902 | unsigned IntWidth = Context.getIntWidth(SubCast->getType()); |
| 1903 | unsigned PointerWidth = Context.getTypeSize(Context.VoidPtrTy); |
| 1904 | if (IntWidth >= PointerWidth) { |
| 1905 | return CheckAddressConstantExpression(SubCast->getSubExpr()); |
| 1906 | } |
| 1907 | } |
| 1908 | } |
| 1909 | } |
| 1910 | if (SubExpr->getType()->isArithmeticType()) { |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1911 | return CheckArithmeticConstantExpression(SubExpr); |
Eli Friedman | c3f0764 | 2008-08-25 20:46:57 +0000 | [diff] [blame] | 1912 | } |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1913 | |
Steve Naroff | 6594a70 | 2008-10-27 11:34:16 +0000 | [diff] [blame] | 1914 | InitializerElementNotConstant(Init); |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1915 | return true; |
| 1916 | } |
| 1917 | case Expr::ConditionalOperatorClass: { |
| 1918 | // FIXME: Should we pedwarn here? |
| 1919 | const ConditionalOperator *Exp = cast<ConditionalOperator>(Init); |
| 1920 | if (!Exp->getCond()->getType()->isArithmeticType()) { |
Steve Naroff | 6594a70 | 2008-10-27 11:34:16 +0000 | [diff] [blame] | 1921 | InitializerElementNotConstant(Init); |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1922 | return true; |
| 1923 | } |
| 1924 | if (CheckArithmeticConstantExpression(Exp->getCond())) |
| 1925 | return true; |
| 1926 | if (Exp->getLHS() && |
| 1927 | CheckAddressConstantExpression(Exp->getLHS())) |
| 1928 | return true; |
| 1929 | return CheckAddressConstantExpression(Exp->getRHS()); |
| 1930 | } |
| 1931 | case Expr::AddrLabelExprClass: |
| 1932 | return false; |
| 1933 | } |
| 1934 | } |
| 1935 | |
Eli Friedman | 4caf055 | 2008-06-09 05:05:07 +0000 | [diff] [blame] | 1936 | static const Expr* FindExpressionBaseAddress(const Expr* E); |
| 1937 | |
| 1938 | static const Expr* FindExpressionBaseAddressLValue(const Expr* E) { |
| 1939 | switch (E->getStmtClass()) { |
| 1940 | default: |
| 1941 | return E; |
| 1942 | case Expr::ParenExprClass: { |
| 1943 | const ParenExpr* PE = cast<ParenExpr>(E); |
| 1944 | return FindExpressionBaseAddressLValue(PE->getSubExpr()); |
| 1945 | } |
| 1946 | case Expr::MemberExprClass: { |
| 1947 | const MemberExpr *M = cast<MemberExpr>(E); |
| 1948 | if (M->isArrow()) |
| 1949 | return FindExpressionBaseAddress(M->getBase()); |
| 1950 | return FindExpressionBaseAddressLValue(M->getBase()); |
| 1951 | } |
| 1952 | case Expr::ArraySubscriptExprClass: { |
| 1953 | const ArraySubscriptExpr *ASE = cast<ArraySubscriptExpr>(E); |
| 1954 | return FindExpressionBaseAddress(ASE->getBase()); |
| 1955 | } |
| 1956 | case Expr::UnaryOperatorClass: { |
| 1957 | const UnaryOperator *Exp = cast<UnaryOperator>(E); |
| 1958 | |
| 1959 | if (Exp->getOpcode() == UnaryOperator::Deref) |
| 1960 | return FindExpressionBaseAddress(Exp->getSubExpr()); |
| 1961 | |
| 1962 | return E; |
| 1963 | } |
| 1964 | } |
| 1965 | } |
| 1966 | |
| 1967 | static const Expr* FindExpressionBaseAddress(const Expr* E) { |
| 1968 | switch (E->getStmtClass()) { |
| 1969 | default: |
| 1970 | return E; |
| 1971 | case Expr::ParenExprClass: { |
| 1972 | const ParenExpr* PE = cast<ParenExpr>(E); |
| 1973 | return FindExpressionBaseAddress(PE->getSubExpr()); |
| 1974 | } |
| 1975 | case Expr::UnaryOperatorClass: { |
| 1976 | const UnaryOperator *Exp = cast<UnaryOperator>(E); |
| 1977 | |
| 1978 | // C99 6.6p9 |
| 1979 | if (Exp->getOpcode() == UnaryOperator::AddrOf) |
| 1980 | return FindExpressionBaseAddressLValue(Exp->getSubExpr()); |
| 1981 | |
| 1982 | if (Exp->getOpcode() == UnaryOperator::Extension) |
| 1983 | return FindExpressionBaseAddress(Exp->getSubExpr()); |
| 1984 | |
| 1985 | return E; |
| 1986 | } |
| 1987 | case Expr::BinaryOperatorClass: { |
| 1988 | const BinaryOperator *Exp = cast<BinaryOperator>(E); |
| 1989 | |
| 1990 | Expr *PExp = Exp->getLHS(); |
| 1991 | Expr *IExp = Exp->getRHS(); |
| 1992 | if (IExp->getType()->isPointerType()) |
| 1993 | std::swap(PExp, IExp); |
| 1994 | |
| 1995 | return FindExpressionBaseAddress(PExp); |
| 1996 | } |
| 1997 | case Expr::ImplicitCastExprClass: { |
| 1998 | const Expr* SubExpr = cast<ImplicitCastExpr>(E)->getSubExpr(); |
| 1999 | |
| 2000 | // Check for implicit promotion |
| 2001 | if (SubExpr->getType()->isFunctionType() || |
| 2002 | SubExpr->getType()->isArrayType()) |
| 2003 | return FindExpressionBaseAddressLValue(SubExpr); |
| 2004 | |
| 2005 | // Check for pointer->pointer cast |
| 2006 | if (SubExpr->getType()->isPointerType()) |
| 2007 | return FindExpressionBaseAddress(SubExpr); |
| 2008 | |
| 2009 | // We assume that we have an arithmetic expression here; |
| 2010 | // if we don't, we'll figure it out later |
| 2011 | return 0; |
| 2012 | } |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 2013 | case Expr::CStyleCastExprClass: { |
Eli Friedman | 4caf055 | 2008-06-09 05:05:07 +0000 | [diff] [blame] | 2014 | const Expr* SubExpr = cast<CastExpr>(E)->getSubExpr(); |
| 2015 | |
| 2016 | // Check for pointer->pointer cast |
| 2017 | if (SubExpr->getType()->isPointerType()) |
| 2018 | return FindExpressionBaseAddress(SubExpr); |
| 2019 | |
| 2020 | // We assume that we have an arithmetic expression here; |
| 2021 | // if we don't, we'll figure it out later |
| 2022 | return 0; |
| 2023 | } |
| 2024 | } |
| 2025 | } |
| 2026 | |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 2027 | bool Sema::CheckArithmeticConstantExpression(const Expr* Init) { |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 2028 | switch (Init->getStmtClass()) { |
| 2029 | default: |
Steve Naroff | 6594a70 | 2008-10-27 11:34:16 +0000 | [diff] [blame] | 2030 | InitializerElementNotConstant(Init); |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 2031 | return true; |
| 2032 | case Expr::ParenExprClass: { |
| 2033 | const ParenExpr* PE = cast<ParenExpr>(Init); |
| 2034 | return CheckArithmeticConstantExpression(PE->getSubExpr()); |
| 2035 | } |
| 2036 | case Expr::FloatingLiteralClass: |
| 2037 | case Expr::IntegerLiteralClass: |
| 2038 | case Expr::CharacterLiteralClass: |
| 2039 | case Expr::ImaginaryLiteralClass: |
| 2040 | case Expr::TypesCompatibleExprClass: |
| 2041 | case Expr::CXXBoolLiteralExprClass: |
| 2042 | return false; |
Douglas Gregor | b460980 | 2008-11-14 16:09:21 +0000 | [diff] [blame] | 2043 | case Expr::CallExprClass: |
| 2044 | case Expr::CXXOperatorCallExprClass: { |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 2045 | const CallExpr *CE = cast<CallExpr>(Init); |
Chris Lattner | 45b6b9d | 2008-10-06 06:49:02 +0000 | [diff] [blame] | 2046 | |
| 2047 | // Allow any constant foldable calls to builtins. |
| 2048 | if (CE->isBuiltinCall() && CE->isEvaluatable(Context)) |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 2049 | return false; |
Chris Lattner | 45b6b9d | 2008-10-06 06:49:02 +0000 | [diff] [blame] | 2050 | |
Steve Naroff | 6594a70 | 2008-10-27 11:34:16 +0000 | [diff] [blame] | 2051 | InitializerElementNotConstant(Init); |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 2052 | return true; |
| 2053 | } |
Douglas Gregor | 1a49af9 | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 2054 | case Expr::DeclRefExprClass: |
| 2055 | case Expr::QualifiedDeclRefExprClass: { |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 2056 | const Decl *D = cast<DeclRefExpr>(Init)->getDecl(); |
| 2057 | if (isa<EnumConstantDecl>(D)) |
| 2058 | return false; |
Steve Naroff | 6594a70 | 2008-10-27 11:34:16 +0000 | [diff] [blame] | 2059 | InitializerElementNotConstant(Init); |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 2060 | return true; |
| 2061 | } |
| 2062 | case Expr::CompoundLiteralExprClass: |
| 2063 | // Allow "(vector type){2,4}"; normal C constraints don't allow this, |
| 2064 | // but vectors are allowed to be magic. |
| 2065 | if (Init->getType()->isVectorType()) |
| 2066 | return false; |
Steve Naroff | 6594a70 | 2008-10-27 11:34:16 +0000 | [diff] [blame] | 2067 | InitializerElementNotConstant(Init); |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 2068 | return true; |
| 2069 | case Expr::UnaryOperatorClass: { |
| 2070 | const UnaryOperator *Exp = cast<UnaryOperator>(Init); |
| 2071 | |
| 2072 | switch (Exp->getOpcode()) { |
| 2073 | // Address, indirect, pre/post inc/dec, etc are not valid constant exprs. |
| 2074 | // See C99 6.6p3. |
| 2075 | default: |
Steve Naroff | 6594a70 | 2008-10-27 11:34:16 +0000 | [diff] [blame] | 2076 | InitializerElementNotConstant(Init); |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 2077 | return true; |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 2078 | case UnaryOperator::OffsetOf: |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 2079 | if (Exp->getSubExpr()->getType()->isConstantSizeType()) |
| 2080 | return false; |
Steve Naroff | 6594a70 | 2008-10-27 11:34:16 +0000 | [diff] [blame] | 2081 | InitializerElementNotConstant(Init); |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 2082 | return true; |
| 2083 | case UnaryOperator::Extension: |
| 2084 | case UnaryOperator::LNot: |
| 2085 | case UnaryOperator::Plus: |
| 2086 | case UnaryOperator::Minus: |
| 2087 | case UnaryOperator::Not: |
| 2088 | return CheckArithmeticConstantExpression(Exp->getSubExpr()); |
| 2089 | } |
| 2090 | } |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 2091 | case Expr::SizeOfAlignOfExprClass: { |
| 2092 | const SizeOfAlignOfExpr *Exp = cast<SizeOfAlignOfExpr>(Init); |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 2093 | // Special check for void types, which are allowed as an extension |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 2094 | if (Exp->getTypeOfArgument()->isVoidType()) |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 2095 | return false; |
| 2096 | // alignof always evaluates to a constant. |
| 2097 | // FIXME: is sizeof(int[3.0]) a constant expression? |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 2098 | if (Exp->isSizeOf() && !Exp->getTypeOfArgument()->isConstantSizeType()) { |
Steve Naroff | 6594a70 | 2008-10-27 11:34:16 +0000 | [diff] [blame] | 2099 | InitializerElementNotConstant(Init); |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 2100 | return true; |
| 2101 | } |
| 2102 | return false; |
| 2103 | } |
| 2104 | case Expr::BinaryOperatorClass: { |
| 2105 | const BinaryOperator *Exp = cast<BinaryOperator>(Init); |
| 2106 | |
| 2107 | if (Exp->getLHS()->getType()->isArithmeticType() && |
| 2108 | Exp->getRHS()->getType()->isArithmeticType()) { |
| 2109 | return CheckArithmeticConstantExpression(Exp->getLHS()) || |
| 2110 | CheckArithmeticConstantExpression(Exp->getRHS()); |
| 2111 | } |
| 2112 | |
Eli Friedman | 4caf055 | 2008-06-09 05:05:07 +0000 | [diff] [blame] | 2113 | if (Exp->getLHS()->getType()->isPointerType() && |
| 2114 | Exp->getRHS()->getType()->isPointerType()) { |
| 2115 | const Expr* LHSBase = FindExpressionBaseAddress(Exp->getLHS()); |
| 2116 | const Expr* RHSBase = FindExpressionBaseAddress(Exp->getRHS()); |
| 2117 | |
| 2118 | // Only allow a null (constant integer) base; we could |
| 2119 | // allow some additional cases if necessary, but this |
| 2120 | // is sufficient to cover offsetof-like constructs. |
| 2121 | if (!LHSBase && !RHSBase) { |
| 2122 | return CheckAddressConstantExpression(Exp->getLHS()) || |
| 2123 | CheckAddressConstantExpression(Exp->getRHS()); |
| 2124 | } |
| 2125 | } |
| 2126 | |
Steve Naroff | 6594a70 | 2008-10-27 11:34:16 +0000 | [diff] [blame] | 2127 | InitializerElementNotConstant(Init); |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 2128 | return true; |
| 2129 | } |
| 2130 | case Expr::ImplicitCastExprClass: |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 2131 | case Expr::CStyleCastExprClass: { |
Argyrios Kyrtzidis | 0835a3c | 2008-08-18 23:01:59 +0000 | [diff] [blame] | 2132 | const Expr *SubExpr = cast<CastExpr>(Init)->getSubExpr(); |
Eli Friedman | 6d4abe1 | 2008-09-01 22:08:17 +0000 | [diff] [blame] | 2133 | if (SubExpr->getType()->isArithmeticType()) |
| 2134 | return CheckArithmeticConstantExpression(SubExpr); |
| 2135 | |
Eli Friedman | b529d83 | 2008-09-02 09:37:00 +0000 | [diff] [blame] | 2136 | if (SubExpr->getType()->isPointerType()) { |
| 2137 | const Expr* Base = FindExpressionBaseAddress(SubExpr); |
| 2138 | // If the pointer has a null base, this is an offsetof-like construct |
| 2139 | if (!Base) |
| 2140 | return CheckAddressConstantExpression(SubExpr); |
| 2141 | } |
| 2142 | |
Steve Naroff | 6594a70 | 2008-10-27 11:34:16 +0000 | [diff] [blame] | 2143 | InitializerElementNotConstant(Init); |
Eli Friedman | 6d4abe1 | 2008-09-01 22:08:17 +0000 | [diff] [blame] | 2144 | return true; |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 2145 | } |
| 2146 | case Expr::ConditionalOperatorClass: { |
| 2147 | const ConditionalOperator *Exp = cast<ConditionalOperator>(Init); |
Chris Lattner | 46cfefa | 2008-10-06 05:42:39 +0000 | [diff] [blame] | 2148 | |
| 2149 | // If GNU extensions are disabled, we require all operands to be arithmetic |
| 2150 | // constant expressions. |
| 2151 | if (getLangOptions().NoExtensions) { |
| 2152 | return CheckArithmeticConstantExpression(Exp->getCond()) || |
| 2153 | (Exp->getLHS() && CheckArithmeticConstantExpression(Exp->getLHS())) || |
| 2154 | CheckArithmeticConstantExpression(Exp->getRHS()); |
| 2155 | } |
| 2156 | |
| 2157 | // Otherwise, we have to emulate some of the behavior of fold here. |
| 2158 | // Basically GCC treats things like "4 ? 1 : somefunc()" as a constant |
| 2159 | // because it can constant fold things away. To retain compatibility with |
| 2160 | // GCC code, we see if we can fold the condition to a constant (which we |
| 2161 | // should always be able to do in theory). If so, we only require the |
| 2162 | // specified arm of the conditional to be a constant. This is a horrible |
| 2163 | // hack, but is require by real world code that uses __builtin_constant_p. |
Anders Carlsson | 1c0cfd4 | 2008-12-19 20:58:05 +0000 | [diff] [blame] | 2164 | Expr::EvalResult EvalResult; |
| 2165 | if (!Exp->getCond()->Evaluate(EvalResult, Context) || |
| 2166 | EvalResult.HasSideEffects) { |
Chris Lattner | 6ee7aa1 | 2008-11-16 21:24:15 +0000 | [diff] [blame] | 2167 | // If Evaluate couldn't fold it, CheckArithmeticConstantExpression |
Chris Lattner | 46cfefa | 2008-10-06 05:42:39 +0000 | [diff] [blame] | 2168 | // won't be able to either. Use it to emit the diagnostic though. |
| 2169 | bool Res = CheckArithmeticConstantExpression(Exp->getCond()); |
Chris Lattner | 6ee7aa1 | 2008-11-16 21:24:15 +0000 | [diff] [blame] | 2170 | assert(Res && "Evaluate couldn't evaluate this constant?"); |
Chris Lattner | 46cfefa | 2008-10-06 05:42:39 +0000 | [diff] [blame] | 2171 | return Res; |
| 2172 | } |
| 2173 | |
| 2174 | // Verify that the side following the condition is also a constant. |
| 2175 | const Expr *TrueSide = Exp->getLHS(), *FalseSide = Exp->getRHS(); |
Anders Carlsson | 1c0cfd4 | 2008-12-19 20:58:05 +0000 | [diff] [blame] | 2176 | if (EvalResult.Val.getInt() == 0) |
Chris Lattner | 46cfefa | 2008-10-06 05:42:39 +0000 | [diff] [blame] | 2177 | std::swap(TrueSide, FalseSide); |
| 2178 | |
| 2179 | if (TrueSide && CheckArithmeticConstantExpression(TrueSide)) |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 2180 | return true; |
Chris Lattner | 46cfefa | 2008-10-06 05:42:39 +0000 | [diff] [blame] | 2181 | |
| 2182 | // Okay, the evaluated side evaluates to a constant, so we accept this. |
| 2183 | // Check to see if the other side is obviously not a constant. If so, |
| 2184 | // emit a warning that this is a GNU extension. |
Chris Lattner | 45b6b9d | 2008-10-06 06:49:02 +0000 | [diff] [blame] | 2185 | if (FalseSide && !FalseSide->isEvaluatable(Context)) |
Chris Lattner | 46cfefa | 2008-10-06 05:42:39 +0000 | [diff] [blame] | 2186 | Diag(Init->getExprLoc(), |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 2187 | diag::ext_typecheck_expression_not_constant_but_accepted) |
| 2188 | << FalseSide->getSourceRange(); |
Chris Lattner | 46cfefa | 2008-10-06 05:42:39 +0000 | [diff] [blame] | 2189 | return false; |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 2190 | } |
| 2191 | } |
| 2192 | } |
| 2193 | |
| 2194 | bool Sema::CheckForConstantInitializer(Expr *Init, QualType DclT) { |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2195 | if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) |
| 2196 | Init = DIE->getInit(); |
| 2197 | |
Nuno Lopes | 9a979c3 | 2008-07-07 16:46:50 +0000 | [diff] [blame] | 2198 | Init = Init->IgnoreParens(); |
| 2199 | |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 2200 | if (Init->isEvaluatable(Context)) |
Anders Carlsson | 9e09f5d | 2008-12-05 05:09:56 +0000 | [diff] [blame] | 2201 | return false; |
| 2202 | |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 2203 | // Look through CXXDefaultArgExprs; they have no meaning in this context. |
| 2204 | if (CXXDefaultArgExpr* DAE = dyn_cast<CXXDefaultArgExpr>(Init)) |
| 2205 | return CheckForConstantInitializer(DAE->getExpr(), DclT); |
| 2206 | |
Nuno Lopes | 9a979c3 | 2008-07-07 16:46:50 +0000 | [diff] [blame] | 2207 | if (CompoundLiteralExpr *e = dyn_cast<CompoundLiteralExpr>(Init)) |
| 2208 | return CheckForConstantInitializer(e->getInitializer(), DclT); |
| 2209 | |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 2210 | if (InitListExpr *Exp = dyn_cast<InitListExpr>(Init)) { |
| 2211 | unsigned numInits = Exp->getNumInits(); |
| 2212 | for (unsigned i = 0; i < numInits; i++) { |
| 2213 | // FIXME: Need to get the type of the declaration for C++, |
| 2214 | // because it could be a reference? |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame^] | 2215 | |
| 2216 | // Implicitly-generated value initializations are okay. |
| 2217 | if (isa<CXXZeroInitValueExpr>(Exp->getInit(i)) && |
| 2218 | cast<CXXZeroInitValueExpr>(Exp->getInit(i))->isImplicit()) |
| 2219 | continue; |
| 2220 | |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 2221 | if (CheckForConstantInitializer(Exp->getInit(i), |
| 2222 | Exp->getInit(i)->getType())) |
| 2223 | return true; |
| 2224 | } |
| 2225 | return false; |
| 2226 | } |
| 2227 | |
Anders Carlsson | 9e09f5d | 2008-12-05 05:09:56 +0000 | [diff] [blame] | 2228 | // FIXME: We can probably remove some of this code below, now that |
| 2229 | // Expr::Evaluate is doing the heavy lifting for scalars. |
| 2230 | |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 2231 | if (Init->isNullPointerConstant(Context)) |
| 2232 | return false; |
| 2233 | if (Init->getType()->isArithmeticType()) { |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 2234 | QualType InitTy = Context.getCanonicalType(Init->getType()) |
| 2235 | .getUnqualifiedType(); |
Eli Friedman | c1cc6dc | 2008-05-30 18:14:48 +0000 | [diff] [blame] | 2236 | if (InitTy == Context.BoolTy) { |
| 2237 | // Special handling for pointers implicitly cast to bool; |
| 2238 | // (e.g. "_Bool rr = &rr;"). This is only legal at the top level. |
| 2239 | if (ImplicitCastExpr* ICE = dyn_cast<ImplicitCastExpr>(Init)) { |
| 2240 | Expr* SubE = ICE->getSubExpr(); |
| 2241 | if (SubE->getType()->isPointerType() || |
| 2242 | SubE->getType()->isArrayType() || |
| 2243 | SubE->getType()->isFunctionType()) { |
| 2244 | return CheckAddressConstantExpression(Init); |
| 2245 | } |
| 2246 | } |
| 2247 | } else if (InitTy->isIntegralType()) { |
| 2248 | Expr* SubE = 0; |
Argyrios Kyrtzidis | 0835a3c | 2008-08-18 23:01:59 +0000 | [diff] [blame] | 2249 | if (CastExpr* CE = dyn_cast<CastExpr>(Init)) |
Eli Friedman | c1cc6dc | 2008-05-30 18:14:48 +0000 | [diff] [blame] | 2250 | SubE = CE->getSubExpr(); |
| 2251 | // Special check for pointer cast to int; we allow as an extension |
| 2252 | // an address constant cast to an integer if the integer |
| 2253 | // is of an appropriate width (this sort of code is apparently used |
| 2254 | // in some places). |
| 2255 | // FIXME: Add pedwarn? |
| 2256 | // FIXME: Don't allow bitfields here! Need the FieldDecl for that. |
| 2257 | if (SubE && (SubE->getType()->isPointerType() || |
| 2258 | SubE->getType()->isArrayType() || |
| 2259 | SubE->getType()->isFunctionType())) { |
| 2260 | unsigned IntWidth = Context.getTypeSize(Init->getType()); |
| 2261 | unsigned PointerWidth = Context.getTypeSize(Context.VoidPtrTy); |
| 2262 | if (IntWidth >= PointerWidth) |
| 2263 | return CheckAddressConstantExpression(Init); |
| 2264 | } |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 2265 | } |
| 2266 | |
| 2267 | return CheckArithmeticConstantExpression(Init); |
| 2268 | } |
| 2269 | |
| 2270 | if (Init->getType()->isPointerType()) |
| 2271 | return CheckAddressConstantExpression(Init); |
| 2272 | |
Eli Friedman | c1cc6dc | 2008-05-30 18:14:48 +0000 | [diff] [blame] | 2273 | // An array type at the top level that isn't an init-list must |
| 2274 | // be a string literal |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 2275 | if (Init->getType()->isArrayType()) |
| 2276 | return false; |
| 2277 | |
Nuno Lopes | 73419bf | 2008-09-01 18:42:41 +0000 | [diff] [blame] | 2278 | if (Init->getType()->isFunctionType()) |
| 2279 | return false; |
| 2280 | |
Steve Naroff | 8af6a45 | 2008-10-02 17:12:56 +0000 | [diff] [blame] | 2281 | // Allow block exprs at top level. |
| 2282 | if (Init->getType()->isBlockPointerType()) |
| 2283 | return false; |
Nuno Lopes | 6ed2ef8 | 2009-01-15 16:44:45 +0000 | [diff] [blame] | 2284 | |
| 2285 | // GCC cast to union extension |
| 2286 | // note: the validity of the cast expr is checked by CheckCastTypes() |
| 2287 | if (CastExpr *C = dyn_cast<CastExpr>(Init)) { |
| 2288 | QualType T = C->getType(); |
| 2289 | return T->isUnionType() && CheckForConstantInitializer(C->getSubExpr(), T); |
| 2290 | } |
| 2291 | |
Steve Naroff | 6594a70 | 2008-10-27 11:34:16 +0000 | [diff] [blame] | 2292 | InitializerElementNotConstant(Init); |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 2293 | return true; |
Steve Naroff | d0091aa | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 2294 | } |
| 2295 | |
Sebastian Redl | 798d119 | 2008-12-13 16:23:55 +0000 | [diff] [blame] | 2296 | void Sema::AddInitializerToDecl(DeclTy *dcl, ExprArg init) { |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2297 | AddInitializerToDecl(dcl, move(init), /*DirectInit=*/false); |
| 2298 | } |
| 2299 | |
| 2300 | /// AddInitializerToDecl - Adds the initializer Init to the |
| 2301 | /// declaration dcl. If DirectInit is true, this is C++ direct |
| 2302 | /// initialization rather than copy initialization. |
| 2303 | void Sema::AddInitializerToDecl(DeclTy *dcl, ExprArg init, bool DirectInit) { |
Steve Naroff | 410e3e2 | 2007-09-12 20:13:48 +0000 | [diff] [blame] | 2304 | Decl *RealDecl = static_cast<Decl *>(dcl); |
Sebastian Redl | 798d119 | 2008-12-13 16:23:55 +0000 | [diff] [blame] | 2305 | Expr *Init = static_cast<Expr *>(init.release()); |
Chris Lattner | 9a11b9a | 2007-10-19 20:10:30 +0000 | [diff] [blame] | 2306 | assert(Init && "missing initializer"); |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 2307 | |
Chris Lattner | 9a11b9a | 2007-10-19 20:10:30 +0000 | [diff] [blame] | 2308 | // If there is no declaration, there was an error parsing it. Just ignore |
| 2309 | // the initializer. |
| 2310 | if (RealDecl == 0) { |
| 2311 | delete Init; |
| 2312 | return; |
| 2313 | } |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 2314 | |
Steve Naroff | 410e3e2 | 2007-09-12 20:13:48 +0000 | [diff] [blame] | 2315 | VarDecl *VDecl = dyn_cast<VarDecl>(RealDecl); |
| 2316 | if (!VDecl) { |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2317 | Diag(RealDecl->getLocation(), diag::err_illegal_initializer); |
Steve Naroff | 410e3e2 | 2007-09-12 20:13:48 +0000 | [diff] [blame] | 2318 | RealDecl->setInvalidDecl(); |
| 2319 | return; |
| 2320 | } |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 2321 | // Get the decls type and save a reference for later, since |
Steve Naroff | d0091aa | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 2322 | // CheckInitializerTypes may change it. |
Steve Naroff | 410e3e2 | 2007-09-12 20:13:48 +0000 | [diff] [blame] | 2323 | QualType DclT = VDecl->getType(), SavT = DclT; |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 2324 | if (VDecl->isBlockVarDecl()) { |
| 2325 | VarDecl::StorageClass SC = VDecl->getStorageClass(); |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 2326 | if (SC == VarDecl::Extern) { // C99 6.7.8p5 |
Steve Naroff | 410e3e2 | 2007-09-12 20:13:48 +0000 | [diff] [blame] | 2327 | Diag(VDecl->getLocation(), diag::err_block_extern_cant_init); |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 2328 | VDecl->setInvalidDecl(); |
| 2329 | } else if (!VDecl->isInvalidDecl()) { |
Douglas Gregor | f03d7c7 | 2008-11-05 15:29:30 +0000 | [diff] [blame] | 2330 | if (CheckInitializerTypes(Init, DclT, VDecl->getLocation(), |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2331 | VDecl->getDeclName(), DirectInit)) |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 2332 | VDecl->setInvalidDecl(); |
Anders Carlsson | c5eb731 | 2008-08-22 05:00:02 +0000 | [diff] [blame] | 2333 | |
| 2334 | // C++ 3.6.2p2, allow dynamic initialization of static initializers. |
| 2335 | if (!getLangOptions().CPlusPlus) { |
| 2336 | if (SC == VarDecl::Static) // C99 6.7.8p4. |
| 2337 | CheckForConstantInitializer(Init, DclT); |
| 2338 | } |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 2339 | } |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 2340 | } else if (VDecl->isFileVarDecl()) { |
| 2341 | if (VDecl->getStorageClass() == VarDecl::Extern) |
Steve Naroff | 410e3e2 | 2007-09-12 20:13:48 +0000 | [diff] [blame] | 2342 | Diag(VDecl->getLocation(), diag::warn_extern_init); |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 2343 | if (!VDecl->isInvalidDecl()) |
Douglas Gregor | f03d7c7 | 2008-11-05 15:29:30 +0000 | [diff] [blame] | 2344 | if (CheckInitializerTypes(Init, DclT, VDecl->getLocation(), |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2345 | VDecl->getDeclName(), DirectInit)) |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 2346 | VDecl->setInvalidDecl(); |
Steve Naroff | d0091aa | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 2347 | |
Anders Carlsson | c5eb731 | 2008-08-22 05:00:02 +0000 | [diff] [blame] | 2348 | // C++ 3.6.2p2, allow dynamic initialization of static initializers. |
| 2349 | if (!getLangOptions().CPlusPlus) { |
| 2350 | // C99 6.7.8p4. All file scoped initializers need to be constant. |
| 2351 | CheckForConstantInitializer(Init, DclT); |
| 2352 | } |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 2353 | } |
| 2354 | // If the type changed, it means we had an incomplete type that was |
| 2355 | // completed by the initializer. For example: |
| 2356 | // int ary[] = { 1, 3, 5 }; |
| 2357 | // "ary" transitions from a VariableArrayType to a ConstantArrayType. |
Christopher Lamb | 48b1239 | 2007-11-29 19:09:19 +0000 | [diff] [blame] | 2358 | if (!VDecl->isInvalidDecl() && (DclT != SavT)) { |
Steve Naroff | 410e3e2 | 2007-09-12 20:13:48 +0000 | [diff] [blame] | 2359 | VDecl->setType(DclT); |
Christopher Lamb | 48b1239 | 2007-11-29 19:09:19 +0000 | [diff] [blame] | 2360 | Init->setType(DclT); |
| 2361 | } |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 2362 | |
| 2363 | // Attach the initializer to the decl. |
Steve Naroff | 410e3e2 | 2007-09-12 20:13:48 +0000 | [diff] [blame] | 2364 | VDecl->setInit(Init); |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 2365 | return; |
| 2366 | } |
| 2367 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2368 | void Sema::ActOnUninitializedDecl(DeclTy *dcl) { |
| 2369 | Decl *RealDecl = static_cast<Decl *>(dcl); |
| 2370 | |
Argyrios Kyrtzidis | 48c2e90 | 2008-11-07 13:01:22 +0000 | [diff] [blame] | 2371 | // If there is no declaration, there was an error parsing it. Just ignore it. |
| 2372 | if (RealDecl == 0) |
| 2373 | return; |
| 2374 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2375 | if (VarDecl *Var = dyn_cast<VarDecl>(RealDecl)) { |
| 2376 | QualType Type = Var->getType(); |
| 2377 | // C++ [dcl.init.ref]p3: |
| 2378 | // The initializer can be omitted for a reference only in a |
| 2379 | // parameter declaration (8.3.5), in the declaration of a |
| 2380 | // function return type, in the declaration of a class member |
| 2381 | // within its class declaration (9.2), and where the extern |
| 2382 | // specifier is explicitly used. |
Douglas Gregor | 9e7d9de | 2008-12-15 21:24:18 +0000 | [diff] [blame] | 2383 | if (Type->isReferenceType() && |
| 2384 | Var->getStorageClass() != VarDecl::Extern && |
| 2385 | Var->getStorageClass() != VarDecl::PrivateExtern) { |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 2386 | Diag(Var->getLocation(), diag::err_reference_var_requires_init) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2387 | << Var->getDeclName() |
| 2388 | << SourceRange(Var->getLocation(), Var->getLocation()); |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 2389 | Var->setInvalidDecl(); |
| 2390 | return; |
| 2391 | } |
| 2392 | |
| 2393 | // C++ [dcl.init]p9: |
| 2394 | // |
| 2395 | // If no initializer is specified for an object, and the object |
| 2396 | // is of (possibly cv-qualified) non-POD class type (or array |
| 2397 | // thereof), the object shall be default-initialized; if the |
| 2398 | // object is of const-qualified type, the underlying class type |
| 2399 | // shall have a user-declared default constructor. |
| 2400 | if (getLangOptions().CPlusPlus) { |
| 2401 | QualType InitType = Type; |
| 2402 | if (const ArrayType *Array = Context.getAsArrayType(Type)) |
| 2403 | InitType = Array->getElementType(); |
Douglas Gregor | 9e7d9de | 2008-12-15 21:24:18 +0000 | [diff] [blame] | 2404 | if (Var->getStorageClass() != VarDecl::Extern && |
| 2405 | Var->getStorageClass() != VarDecl::PrivateExtern && |
| 2406 | InitType->isRecordType()) { |
Douglas Gregor | f03d7c7 | 2008-11-05 15:29:30 +0000 | [diff] [blame] | 2407 | const CXXConstructorDecl *Constructor |
| 2408 | = PerformInitializationByConstructor(InitType, 0, 0, |
| 2409 | Var->getLocation(), |
| 2410 | SourceRange(Var->getLocation(), |
| 2411 | Var->getLocation()), |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2412 | Var->getDeclName(), |
Douglas Gregor | f03d7c7 | 2008-11-05 15:29:30 +0000 | [diff] [blame] | 2413 | IK_Default); |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 2414 | if (!Constructor) |
| 2415 | Var->setInvalidDecl(); |
| 2416 | } |
| 2417 | } |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2418 | |
Douglas Gregor | 818ce48 | 2008-10-29 13:50:18 +0000 | [diff] [blame] | 2419 | #if 0 |
| 2420 | // FIXME: Temporarily disabled because we are not properly parsing |
| 2421 | // linkage specifications on declarations, e.g., |
| 2422 | // |
| 2423 | // extern "C" const CGPoint CGPointerZero; |
| 2424 | // |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2425 | // C++ [dcl.init]p9: |
| 2426 | // |
| 2427 | // If no initializer is specified for an object, and the |
| 2428 | // object is of (possibly cv-qualified) non-POD class type (or |
| 2429 | // array thereof), the object shall be default-initialized; if |
| 2430 | // the object is of const-qualified type, the underlying class |
| 2431 | // type shall have a user-declared default |
| 2432 | // constructor. Otherwise, if no initializer is specified for |
| 2433 | // an object, the object and its subobjects, if any, have an |
| 2434 | // indeterminate initial value; if the object or any of its |
| 2435 | // subobjects are of const-qualified type, the program is |
| 2436 | // ill-formed. |
| 2437 | // |
| 2438 | // This isn't technically an error in C, so we don't diagnose it. |
| 2439 | // |
| 2440 | // FIXME: Actually perform the POD/user-defined default |
| 2441 | // constructor check. |
| 2442 | if (getLangOptions().CPlusPlus && |
Douglas Gregor | 818ce48 | 2008-10-29 13:50:18 +0000 | [diff] [blame] | 2443 | Context.getCanonicalType(Type).isConstQualified() && |
| 2444 | Var->getStorageClass() != VarDecl::Extern) |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 2445 | Diag(Var->getLocation(), diag::err_const_var_requires_init) |
| 2446 | << Var->getName() |
| 2447 | << SourceRange(Var->getLocation(), Var->getLocation()); |
Douglas Gregor | 818ce48 | 2008-10-29 13:50:18 +0000 | [diff] [blame] | 2448 | #endif |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2449 | } |
| 2450 | } |
| 2451 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2452 | /// The declarators are chained together backwards, reverse the list. |
| 2453 | Sema::DeclTy *Sema::FinalizeDeclaratorGroup(Scope *S, DeclTy *group) { |
| 2454 | // Often we have single declarators, handle them quickly. |
Steve Naroff | 9474504 | 2007-09-13 23:52:58 +0000 | [diff] [blame] | 2455 | Decl *GroupDecl = static_cast<Decl*>(group); |
| 2456 | if (GroupDecl == 0) |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 2457 | return 0; |
Steve Naroff | 9474504 | 2007-09-13 23:52:58 +0000 | [diff] [blame] | 2458 | |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2459 | Decl *Group = dyn_cast<Decl>(GroupDecl); |
| 2460 | Decl *NewGroup = 0; |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 2461 | if (Group->getNextDeclarator() == 0) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2462 | NewGroup = Group; |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 2463 | else { // reverse the list. |
| 2464 | while (Group) { |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2465 | Decl *Next = Group->getNextDeclarator(); |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 2466 | Group->setNextDeclarator(NewGroup); |
| 2467 | NewGroup = Group; |
| 2468 | Group = Next; |
| 2469 | } |
| 2470 | } |
| 2471 | // Perform semantic analysis that depends on having fully processed both |
| 2472 | // the declarator and initializer. |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2473 | for (Decl *ID = NewGroup; ID; ID = ID->getNextDeclarator()) { |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 2474 | VarDecl *IDecl = dyn_cast<VarDecl>(ID); |
| 2475 | if (!IDecl) |
| 2476 | continue; |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 2477 | QualType T = IDecl->getType(); |
| 2478 | |
Anders Carlsson | 96e05bc | 2008-12-07 00:20:55 +0000 | [diff] [blame] | 2479 | if (T->isVariableArrayType()) { |
Anders Carlsson | fcdbb93 | 2008-12-20 21:51:53 +0000 | [diff] [blame] | 2480 | const VariableArrayType *VAT = Context.getAsVariableArrayType(T); |
Anders Carlsson | 7fd1df2 | 2008-12-07 00:49:48 +0000 | [diff] [blame] | 2481 | |
| 2482 | // FIXME: This won't give the correct result for |
| 2483 | // int a[10][n]; |
| 2484 | SourceRange SizeRange = VAT->getSizeExpr()->getSourceRange(); |
Anders Carlsson | 96e05bc | 2008-12-07 00:20:55 +0000 | [diff] [blame] | 2485 | if (IDecl->isFileVarDecl()) { |
Anders Carlsson | 7fd1df2 | 2008-12-07 00:49:48 +0000 | [diff] [blame] | 2486 | Diag(IDecl->getLocation(), diag::err_vla_decl_in_file_scope) << |
| 2487 | SizeRange; |
| 2488 | |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2489 | IDecl->setInvalidDecl(); |
Anders Carlsson | 96e05bc | 2008-12-07 00:20:55 +0000 | [diff] [blame] | 2490 | } else { |
| 2491 | // C99 6.7.5.2p2: If an identifier is declared to be an object with |
| 2492 | // static storage duration, it shall not have a variable length array. |
| 2493 | if (IDecl->getStorageClass() == VarDecl::Static) { |
Anders Carlsson | 7fd1df2 | 2008-12-07 00:49:48 +0000 | [diff] [blame] | 2494 | Diag(IDecl->getLocation(), diag::err_vla_decl_has_static_storage) |
| 2495 | << SizeRange; |
Anders Carlsson | 96e05bc | 2008-12-07 00:20:55 +0000 | [diff] [blame] | 2496 | IDecl->setInvalidDecl(); |
| 2497 | } else if (IDecl->getStorageClass() == VarDecl::Extern) { |
Anders Carlsson | 7fd1df2 | 2008-12-07 00:49:48 +0000 | [diff] [blame] | 2498 | Diag(IDecl->getLocation(), diag::err_vla_decl_has_extern_linkage) |
| 2499 | << SizeRange; |
Anders Carlsson | 96e05bc | 2008-12-07 00:20:55 +0000 | [diff] [blame] | 2500 | IDecl->setInvalidDecl(); |
| 2501 | } |
| 2502 | } |
| 2503 | } else if (T->isVariablyModifiedType()) { |
| 2504 | if (IDecl->isFileVarDecl()) { |
| 2505 | Diag(IDecl->getLocation(), diag::err_vm_decl_in_file_scope); |
| 2506 | IDecl->setInvalidDecl(); |
| 2507 | } else { |
| 2508 | if (IDecl->getStorageClass() == VarDecl::Extern) { |
| 2509 | Diag(IDecl->getLocation(), diag::err_vm_decl_has_extern_linkage); |
| 2510 | IDecl->setInvalidDecl(); |
| 2511 | } |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 2512 | } |
| 2513 | } |
Anders Carlsson | 96e05bc | 2008-12-07 00:20:55 +0000 | [diff] [blame] | 2514 | |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 2515 | // Block scope. C99 6.7p7: If an identifier for an object is declared with |
| 2516 | // 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] | 2517 | if (IDecl->isBlockVarDecl() && |
| 2518 | IDecl->getStorageClass() != VarDecl::Extern) { |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 2519 | if (!IDecl->isInvalidDecl() && |
| 2520 | DiagnoseIncompleteType(IDecl->getLocation(), T, |
| 2521 | diag::err_typecheck_decl_incomplete_type)) |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 2522 | IDecl->setInvalidDecl(); |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 2523 | } |
| 2524 | // File scope. C99 6.9.2p2: A declaration of an identifier for and |
| 2525 | // object that has file scope without an initializer, and without a |
| 2526 | // storage-class specifier or with the storage-class specifier "static", |
| 2527 | // constitutes a tentative definition. Note: A tentative definition with |
| 2528 | // external linkage is valid (C99 6.2.2p5). |
Steve Naroff | ff9eb1f | 2008-08-08 17:50:35 +0000 | [diff] [blame] | 2529 | if (isTentativeDefinition(IDecl)) { |
Eli Friedman | 9db1397 | 2008-02-15 12:53:51 +0000 | [diff] [blame] | 2530 | if (T->isIncompleteArrayType()) { |
Steve Naroff | 9a75f8a | 2008-01-18 20:40:52 +0000 | [diff] [blame] | 2531 | // C99 6.9.2 (p2, p5): Implicit initialization causes an incomplete |
| 2532 | // array to be completed. Don't issue a diagnostic. |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 2533 | } else if (!IDecl->isInvalidDecl() && |
| 2534 | DiagnoseIncompleteType(IDecl->getLocation(), T, |
| 2535 | diag::err_typecheck_decl_incomplete_type)) |
Steve Naroff | 9a75f8a | 2008-01-18 20:40:52 +0000 | [diff] [blame] | 2536 | // C99 6.9.2p3: If the declaration of an identifier for an object is |
| 2537 | // a tentative definition and has internal linkage (C99 6.2.2p3), the |
| 2538 | // declared type shall not be an incomplete type. |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 2539 | IDecl->setInvalidDecl(); |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 2540 | } |
Steve Naroff | ff9eb1f | 2008-08-08 17:50:35 +0000 | [diff] [blame] | 2541 | if (IDecl->isFileVarDecl()) |
| 2542 | CheckForFileScopedRedefinitions(S, IDecl); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2543 | } |
| 2544 | return NewGroup; |
| 2545 | } |
Steve Naroff | e1223f7 | 2007-08-28 03:03:08 +0000 | [diff] [blame] | 2546 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2547 | /// ActOnParamDeclarator - Called from Parser::ParseFunctionDeclarator() |
| 2548 | /// to introduce parameters into function prototype scope. |
| 2549 | Sema::DeclTy * |
| 2550 | Sema::ActOnParamDeclarator(Scope *S, Declarator &D) { |
Chris Lattner | 985abd9 | 2008-06-26 06:49:43 +0000 | [diff] [blame] | 2551 | const DeclSpec &DS = D.getDeclSpec(); |
Douglas Gregor | 584049d | 2008-12-15 23:53:10 +0000 | [diff] [blame] | 2552 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2553 | // Verify C99 6.7.5.3p2: The only SCS allowed is 'register'. |
Daniel Dunbar | 33ad012 | 2008-09-03 21:54:21 +0000 | [diff] [blame] | 2554 | VarDecl::StorageClass StorageClass = VarDecl::None; |
| 2555 | if (DS.getStorageClassSpec() == DeclSpec::SCS_register) { |
| 2556 | StorageClass = VarDecl::Register; |
| 2557 | } else if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified) { |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2558 | Diag(DS.getStorageClassSpecLoc(), |
| 2559 | diag::err_invalid_storage_class_in_func_decl); |
Chris Lattner | 985abd9 | 2008-06-26 06:49:43 +0000 | [diff] [blame] | 2560 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2561 | } |
| 2562 | if (DS.isThreadSpecified()) { |
| 2563 | Diag(DS.getThreadSpecLoc(), |
| 2564 | diag::err_invalid_storage_class_in_func_decl); |
Chris Lattner | 985abd9 | 2008-06-26 06:49:43 +0000 | [diff] [blame] | 2565 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2566 | } |
| 2567 | |
Douglas Gregor | 6d6eb57 | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 2568 | // Check that there are no default arguments inside the type of this |
| 2569 | // parameter (C++ only). |
| 2570 | if (getLangOptions().CPlusPlus) |
| 2571 | CheckExtraCXXDefaultArguments(D); |
| 2572 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2573 | // In this context, we *do not* check D.getInvalidType(). If the declarator |
| 2574 | // type was invalid, GetTypeForDeclarator() still returns a "valid" type, |
| 2575 | // though it will not reflect the user specified type. |
| 2576 | QualType parmDeclType = GetTypeForDeclarator(D, S); |
| 2577 | |
| 2578 | assert(!parmDeclType.isNull() && "GetTypeForDeclarator() returned null type"); |
| 2579 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2580 | // TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope. |
| 2581 | // Can this happen for params? We already checked that they don't conflict |
| 2582 | // among each other. Here they can only shadow globals, which is ok. |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2583 | IdentifierInfo *II = D.getIdentifier(); |
Chris Lattner | cf79b01 | 2009-01-21 02:38:50 +0000 | [diff] [blame] | 2584 | if (II) { |
| 2585 | if (Decl *PrevDecl = LookupDecl(II, Decl::IDNS_Ordinary, S)) { |
| 2586 | if (PrevDecl->isTemplateParameter()) { |
| 2587 | // Maybe we will complain about the shadowed template parameter. |
| 2588 | DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), PrevDecl); |
| 2589 | // Just pretend that we didn't see the previous declaration. |
| 2590 | PrevDecl = 0; |
| 2591 | } else if (S->isDeclScope(PrevDecl)) { |
| 2592 | Diag(D.getIdentifierLoc(), diag::err_param_redefinition) << II; |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2593 | |
Chris Lattner | cf79b01 | 2009-01-21 02:38:50 +0000 | [diff] [blame] | 2594 | // Recover by removing the name |
| 2595 | II = 0; |
| 2596 | D.SetIdentifier(0, D.getIdentifierLoc()); |
| 2597 | } |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2598 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2599 | } |
Steve Naroff | 6a9f3e3 | 2007-08-07 22:44:21 +0000 | [diff] [blame] | 2600 | |
| 2601 | // Perform the default function/array conversion (C99 6.7.5.3p[7,8]). |
| 2602 | // Doing the promotion here has a win and a loss. The win is the type for |
| 2603 | // both Decl's and DeclRefExpr's will match (a convenient invariant for the |
| 2604 | // code generator). The loss is the orginal type isn't preserved. For example: |
| 2605 | // |
| 2606 | // void func(int parmvardecl[5]) { // convert "int [5]" to "int *" |
| 2607 | // int blockvardecl[5]; |
| 2608 | // sizeof(parmvardecl); // size == 4 |
| 2609 | // sizeof(blockvardecl); // size == 20 |
| 2610 | // } |
| 2611 | // |
| 2612 | // For expressions, all implicit conversions are captured using the |
| 2613 | // ImplicitCastExpr AST node (we have no such mechanism for Decl's). |
| 2614 | // |
| 2615 | // FIXME: If a source translation tool needs to see the original type, then |
| 2616 | // we need to consider storing both types (in ParmVarDecl)... |
| 2617 | // |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 2618 | if (parmDeclType->isArrayType()) { |
Chris Lattner | 529bd02 | 2008-01-02 22:50:48 +0000 | [diff] [blame] | 2619 | // int x[restrict 4] -> int *restrict |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 2620 | parmDeclType = Context.getArrayDecayedType(parmDeclType); |
Chris Lattner | 529bd02 | 2008-01-02 22:50:48 +0000 | [diff] [blame] | 2621 | } else if (parmDeclType->isFunctionType()) |
Steve Naroff | 6a9f3e3 | 2007-08-07 22:44:21 +0000 | [diff] [blame] | 2622 | parmDeclType = Context.getPointerType(parmDeclType); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2623 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2624 | ParmVarDecl *New = ParmVarDecl::Create(Context, CurContext, |
| 2625 | D.getIdentifierLoc(), II, |
Daniel Dunbar | 33ad012 | 2008-09-03 21:54:21 +0000 | [diff] [blame] | 2626 | parmDeclType, StorageClass, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2627 | 0); |
Anders Carlsson | f78915f | 2008-02-15 07:04:12 +0000 | [diff] [blame] | 2628 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2629 | if (D.getInvalidType()) |
Steve Naroff | 53a3234 | 2007-08-28 18:45:29 +0000 | [diff] [blame] | 2630 | New->setInvalidDecl(); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2631 | |
Douglas Gregor | 584049d | 2008-12-15 23:53:10 +0000 | [diff] [blame] | 2632 | // Parameter declarators cannot be qualified (C++ [dcl.meaning]p1). |
| 2633 | if (D.getCXXScopeSpec().isSet()) { |
| 2634 | Diag(D.getIdentifierLoc(), diag::err_qualified_param_declarator) |
| 2635 | << D.getCXXScopeSpec().getRange(); |
| 2636 | New->setInvalidDecl(); |
| 2637 | } |
| 2638 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2639 | // Add the parameter declaration into this scope. |
| 2640 | S->AddDecl(New); |
Argyrios Kyrtzidis | 87f3ff0 | 2008-04-12 00:47:19 +0000 | [diff] [blame] | 2641 | if (II) |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2642 | IdResolver.AddDecl(New); |
Nate Begeman | b7894b5 | 2008-02-17 21:20:31 +0000 | [diff] [blame] | 2643 | |
Chris Lattner | 3ff30c8 | 2008-06-29 00:02:00 +0000 | [diff] [blame] | 2644 | ProcessDeclAttributes(New, D); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2645 | return New; |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2646 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2647 | } |
Fariborz Jahanian | 306d68f | 2007-11-08 23:49:49 +0000 | [diff] [blame] | 2648 | |
Douglas Gregor | be109b3 | 2009-01-23 16:23:13 +0000 | [diff] [blame] | 2649 | void Sema::ActOnFinishKNRParamDeclarations(Scope *S, Declarator &D) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2650 | assert(D.getTypeObject(0).Kind == DeclaratorChunk::Function && |
| 2651 | "Not a function declarator!"); |
| 2652 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun; |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2653 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2654 | // Verify 6.9.1p6: 'every identifier in the identifier list shall be declared' |
| 2655 | // for a K&R function. |
| 2656 | if (!FTI.hasPrototype) { |
| 2657 | for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) { |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2658 | if (FTI.ArgInfo[i].Param == 0) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2659 | Diag(FTI.ArgInfo[i].IdentLoc, diag::ext_param_not_declared) |
| 2660 | << FTI.ArgInfo[i].Ident; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2661 | // Implicitly declare the argument as type 'int' for lack of a better |
| 2662 | // type. |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2663 | DeclSpec DS; |
| 2664 | const char* PrevSpec; // unused |
| 2665 | DS.SetTypeSpecType(DeclSpec::TST_int, FTI.ArgInfo[i].IdentLoc, |
| 2666 | PrevSpec); |
| 2667 | Declarator ParamD(DS, Declarator::KNRTypeListContext); |
| 2668 | ParamD.SetIdentifier(FTI.ArgInfo[i].Ident, FTI.ArgInfo[i].IdentLoc); |
Douglas Gregor | be109b3 | 2009-01-23 16:23:13 +0000 | [diff] [blame] | 2669 | FTI.ArgInfo[i].Param = ActOnParamDeclarator(S, ParamD); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2670 | } |
| 2671 | } |
Douglas Gregor | be109b3 | 2009-01-23 16:23:13 +0000 | [diff] [blame] | 2672 | } |
| 2673 | } |
| 2674 | |
| 2675 | Sema::DeclTy *Sema::ActOnStartOfFunctionDef(Scope *FnBodyScope, Declarator &D) { |
| 2676 | assert(getCurFunctionDecl() == 0 && "Function parsing confused"); |
| 2677 | assert(D.getTypeObject(0).Kind == DeclaratorChunk::Function && |
| 2678 | "Not a function declarator!"); |
| 2679 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun; |
| 2680 | |
| 2681 | if (FTI.hasPrototype) { |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2682 | // FIXME: Diagnose arguments without names in C. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2683 | } |
| 2684 | |
Douglas Gregor | 584049d | 2008-12-15 23:53:10 +0000 | [diff] [blame] | 2685 | Scope *ParentScope = FnBodyScope->getParent(); |
Steve Naroff | adbbd0c | 2008-01-14 20:51:29 +0000 | [diff] [blame] | 2686 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 2687 | return ActOnStartOfFunctionDef(FnBodyScope, |
Douglas Gregor | 584049d | 2008-12-15 23:53:10 +0000 | [diff] [blame] | 2688 | ActOnDeclarator(ParentScope, D, 0, |
| 2689 | /*IsFunctionDefinition=*/true)); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 2690 | } |
| 2691 | |
| 2692 | Sema::DeclTy *Sema::ActOnStartOfFunctionDef(Scope *FnBodyScope, DeclTy *D) { |
| 2693 | Decl *decl = static_cast<Decl*>(D); |
Chris Lattner | e9ba323 | 2008-02-16 01:20:36 +0000 | [diff] [blame] | 2694 | FunctionDecl *FD = cast<FunctionDecl>(decl); |
Douglas Gregor | 6fc17ff | 2008-10-29 15:10:40 +0000 | [diff] [blame] | 2695 | |
| 2696 | // See if this is a redefinition. |
| 2697 | const FunctionDecl *Definition; |
| 2698 | if (FD->getBody(Definition)) { |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 2699 | Diag(FD->getLocation(), diag::err_redefinition) << FD->getDeclName(); |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 2700 | Diag(Definition->getLocation(), diag::note_previous_definition); |
Douglas Gregor | 6fc17ff | 2008-10-29 15:10:40 +0000 | [diff] [blame] | 2701 | } |
| 2702 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2703 | PushDeclContext(FnBodyScope, FD); |
Anton Korobeynikov | 2f40270 | 2008-12-26 00:52:02 +0000 | [diff] [blame] | 2704 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2705 | // Check the validity of our function parameters |
| 2706 | CheckParmsForFunctionDef(FD); |
| 2707 | |
| 2708 | // Introduce our parameters into the function scope |
| 2709 | for (unsigned p = 0, NumParams = FD->getNumParams(); p < NumParams; ++p) { |
| 2710 | ParmVarDecl *Param = FD->getParamDecl(p); |
Douglas Gregor | a8cc8ce | 2009-01-09 18:51:29 +0000 | [diff] [blame] | 2711 | Param->setOwningFunction(FD); |
| 2712 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2713 | // If this has an identifier, add it to the scope stack. |
Argyrios Kyrtzidis | 87f3ff0 | 2008-04-12 00:47:19 +0000 | [diff] [blame] | 2714 | if (Param->getIdentifier()) |
| 2715 | PushOnScopeChains(Param, FnBodyScope); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2716 | } |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2717 | |
Anton Korobeynikov | 2f40270 | 2008-12-26 00:52:02 +0000 | [diff] [blame] | 2718 | // Checking attributes of current function definition |
| 2719 | // dllimport attribute. |
| 2720 | if (FD->getAttr<DLLImportAttr>() && (!FD->getAttr<DLLExportAttr>())) { |
| 2721 | // dllimport attribute cannot be applied to definition. |
| 2722 | if (!(FD->getAttr<DLLImportAttr>())->isInherited()) { |
| 2723 | Diag(FD->getLocation(), |
| 2724 | diag::err_attribute_can_be_applied_only_to_symbol_declaration) |
| 2725 | << "dllimport"; |
| 2726 | FD->setInvalidDecl(); |
| 2727 | return FD; |
| 2728 | } else { |
| 2729 | // If a symbol previously declared dllimport is later defined, the |
| 2730 | // attribute is ignored in subsequent references, and a warning is |
| 2731 | // emitted. |
| 2732 | Diag(FD->getLocation(), |
| 2733 | diag::warn_redeclaration_without_attribute_prev_attribute_ignored) |
| 2734 | << FD->getNameAsCString() << "dllimport"; |
| 2735 | } |
| 2736 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2737 | return FD; |
| 2738 | } |
| 2739 | |
Sebastian Redl | 798d119 | 2008-12-13 16:23:55 +0000 | [diff] [blame] | 2740 | Sema::DeclTy *Sema::ActOnFinishFunctionBody(DeclTy *D, StmtArg BodyArg) { |
Steve Naroff | d6d054d | 2007-11-11 23:20:51 +0000 | [diff] [blame] | 2741 | Decl *dcl = static_cast<Decl *>(D); |
Sebastian Redl | 798d119 | 2008-12-13 16:23:55 +0000 | [diff] [blame] | 2742 | Stmt *Body = static_cast<Stmt*>(BodyArg.release()); |
Steve Naroff | 394f3f4 | 2008-07-25 17:57:26 +0000 | [diff] [blame] | 2743 | if (FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(dcl)) { |
Sebastian Redl | 798d119 | 2008-12-13 16:23:55 +0000 | [diff] [blame] | 2744 | FD->setBody(Body); |
Argyrios Kyrtzidis | 53d0ea5 | 2008-06-28 06:07:14 +0000 | [diff] [blame] | 2745 | assert(FD == getCurFunctionDecl() && "Function parsing confused"); |
Steve Naroff | 394f3f4 | 2008-07-25 17:57:26 +0000 | [diff] [blame] | 2746 | } else if (ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(dcl)) { |
Steve Naroff | d6d054d | 2007-11-11 23:20:51 +0000 | [diff] [blame] | 2747 | MD->setBody((Stmt*)Body); |
Steve Naroff | 394f3f4 | 2008-07-25 17:57:26 +0000 | [diff] [blame] | 2748 | } else |
| 2749 | return 0; |
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 2750 | PopDeclContext(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2751 | // Verify and clean out per-function state. |
| 2752 | |
| 2753 | // Check goto/label use. |
| 2754 | for (llvm::DenseMap<IdentifierInfo*, LabelStmt*>::iterator |
| 2755 | I = LabelMap.begin(), E = LabelMap.end(); I != E; ++I) { |
| 2756 | // Verify that we have no forward references left. If so, there was a goto |
| 2757 | // or address of a label taken, but no definition of it. Label fwd |
| 2758 | // definitions are indicated with a null substmt. |
| 2759 | if (I->second->getSubStmt() == 0) { |
| 2760 | LabelStmt *L = I->second; |
| 2761 | // Emit error. |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 2762 | Diag(L->getIdentLoc(), diag::err_undeclared_label_use) << L->getName(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2763 | |
| 2764 | // At this point, we have gotos that use the bogus label. Stitch it into |
| 2765 | // the function body so that they aren't leaked and that the AST is well |
| 2766 | // formed. |
Chris Lattner | 0cbc215 | 2008-01-25 00:01:10 +0000 | [diff] [blame] | 2767 | if (Body) { |
| 2768 | L->setSubStmt(new NullStmt(L->getIdentLoc())); |
Sebastian Redl | 798d119 | 2008-12-13 16:23:55 +0000 | [diff] [blame] | 2769 | cast<CompoundStmt>(Body)->push_back(L); |
Chris Lattner | 0cbc215 | 2008-01-25 00:01:10 +0000 | [diff] [blame] | 2770 | } else { |
| 2771 | // The whole function wasn't parsed correctly, just delete this. |
| 2772 | delete L; |
| 2773 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2774 | } |
| 2775 | } |
| 2776 | LabelMap.clear(); |
| 2777 | |
Steve Naroff | d6d054d | 2007-11-11 23:20:51 +0000 | [diff] [blame] | 2778 | return D; |
Fariborz Jahanian | 60fbca0 | 2007-11-10 16:31:34 +0000 | [diff] [blame] | 2779 | } |
| 2780 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2781 | /// ImplicitlyDefineFunction - An undeclared identifier was used in a function |
| 2782 | /// call, forming a call to an implicitly defined function (per C99 6.5.1p2). |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2783 | NamedDecl *Sema::ImplicitlyDefineFunction(SourceLocation Loc, |
| 2784 | IdentifierInfo &II, Scope *S) { |
Chris Lattner | 37d1084 | 2008-05-05 21:18:06 +0000 | [diff] [blame] | 2785 | // Extension in C99. Legal in C90, but warn about it. |
| 2786 | if (getLangOptions().C99) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2787 | Diag(Loc, diag::ext_implicit_function_decl) << &II; |
Chris Lattner | 37d1084 | 2008-05-05 21:18:06 +0000 | [diff] [blame] | 2788 | else |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2789 | Diag(Loc, diag::warn_implicit_function_decl) << &II; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2790 | |
| 2791 | // FIXME: handle stuff like: |
| 2792 | // void foo() { extern float X(); } |
| 2793 | // void bar() { X(); } <-- implicit decl for X in another scope. |
| 2794 | |
| 2795 | // Set a Declarator for the implicit definition: int foo(); |
| 2796 | const char *Dummy; |
| 2797 | DeclSpec DS; |
| 2798 | bool Error = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, Dummy); |
| 2799 | Error = Error; // Silence warning. |
| 2800 | assert(!Error && "Error setting up implicit decl!"); |
| 2801 | Declarator D(DS, Declarator::BlockContext); |
Chris Lattner | 5af2f35 | 2009-01-20 19:11:22 +0000 | [diff] [blame] | 2802 | D.AddTypeInfo(DeclaratorChunk::getFunction(false, false, 0, 0, 0, Loc, D)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2803 | D.SetIdentifier(&II, Loc); |
| 2804 | |
Argyrios Kyrtzidis | 93213bb | 2008-05-01 21:04:16 +0000 | [diff] [blame] | 2805 | // Insert this function into translation-unit scope. |
| 2806 | |
| 2807 | DeclContext *PrevDC = CurContext; |
| 2808 | CurContext = Context.getTranslationUnitDecl(); |
| 2809 | |
Steve Naroff | e2ef815 | 2008-04-04 14:32:09 +0000 | [diff] [blame] | 2810 | FunctionDecl *FD = |
Daniel Dunbar | 914701e | 2008-08-05 16:28:08 +0000 | [diff] [blame] | 2811 | dyn_cast<FunctionDecl>(static_cast<Decl*>(ActOnDeclarator(TUScope, D, 0))); |
Steve Naroff | e2ef815 | 2008-04-04 14:32:09 +0000 | [diff] [blame] | 2812 | FD->setImplicit(); |
Argyrios Kyrtzidis | 93213bb | 2008-05-01 21:04:16 +0000 | [diff] [blame] | 2813 | |
| 2814 | CurContext = PrevDC; |
| 2815 | |
Steve Naroff | e2ef815 | 2008-04-04 14:32:09 +0000 | [diff] [blame] | 2816 | return FD; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2817 | } |
| 2818 | |
| 2819 | |
Chris Lattner | 41af093 | 2007-11-14 06:34:38 +0000 | [diff] [blame] | 2820 | TypedefDecl *Sema::ParseTypedefDecl(Scope *S, Declarator &D, QualType T, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2821 | Decl *LastDeclarator) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2822 | assert(D.getIdentifier() && "Wrong callback for declspec without declarator"); |
Steve Naroff | 5912a35 | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 2823 | assert(!T.isNull() && "GetTypeForDeclarator() returned null type"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2824 | |
| 2825 | // Scope manipulation handled by caller. |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2826 | TypedefDecl *NewTD = TypedefDecl::Create(Context, CurContext, |
| 2827 | D.getIdentifierLoc(), |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 2828 | D.getIdentifier(), |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2829 | T); |
| 2830 | NewTD->setNextDeclarator(LastDeclarator); |
Steve Naroff | 5912a35 | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 2831 | if (D.getInvalidType()) |
| 2832 | NewTD->setInvalidDecl(); |
| 2833 | return NewTD; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2834 | } |
| 2835 | |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 2836 | /// 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] | 2837 | /// former case, Name will be non-null. In the later case, Name will be null. |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 2838 | /// TagSpec indicates what kind of tag this is. TK indicates whether this is a |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2839 | /// reference/declaration/definition of a tag. |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 2840 | Sema::DeclTy *Sema::ActOnTag(Scope *S, unsigned TagSpec, TagKind TK, |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2841 | SourceLocation KWLoc, const CXXScopeSpec &SS, |
| 2842 | IdentifierInfo *Name, SourceLocation NameLoc, |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 2843 | AttributeList *Attr, |
| 2844 | MultiTemplateParamsArg TemplateParameterLists) { |
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 2845 | // If this is not a definition, it must have a name. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2846 | assert((Name != 0 || TK == TK_Definition) && |
| 2847 | "Nameless record must be a definition!"); |
| 2848 | |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 2849 | TagDecl::TagKind Kind; |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 2850 | switch (TagSpec) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2851 | default: assert(0 && "Unknown tag type!"); |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 2852 | case DeclSpec::TST_struct: Kind = TagDecl::TK_struct; break; |
| 2853 | case DeclSpec::TST_union: Kind = TagDecl::TK_union; break; |
| 2854 | case DeclSpec::TST_class: Kind = TagDecl::TK_class; break; |
| 2855 | case DeclSpec::TST_enum: Kind = TagDecl::TK_enum; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2856 | } |
| 2857 | |
Douglas Gregor | 4920f1f | 2009-01-12 22:49:06 +0000 | [diff] [blame] | 2858 | DeclContext *SearchDC = CurContext; |
Argyrios Kyrtzidis | 0f84a23 | 2008-11-09 22:53:32 +0000 | [diff] [blame] | 2859 | DeclContext *DC = CurContext; |
Douglas Gregor | 3218c4b | 2009-01-09 22:42:13 +0000 | [diff] [blame] | 2860 | DeclContext *LexicalContext = CurContext; |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2861 | Decl *PrevDecl = 0; |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 2862 | |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 2863 | bool Invalid = false; |
| 2864 | |
Argyrios Kyrtzidis | 630c81b | 2008-11-09 22:09:58 +0000 | [diff] [blame] | 2865 | if (Name && SS.isNotEmpty()) { |
| 2866 | // We have a nested-name tag ('struct foo::bar'). |
| 2867 | |
| 2868 | // Check for invalid 'foo::'. |
Argyrios Kyrtzidis | 0f84a23 | 2008-11-09 22:53:32 +0000 | [diff] [blame] | 2869 | if (SS.isInvalid()) { |
Argyrios Kyrtzidis | 630c81b | 2008-11-09 22:09:58 +0000 | [diff] [blame] | 2870 | Name = 0; |
| 2871 | goto CreateNewDecl; |
| 2872 | } |
| 2873 | |
Argyrios Kyrtzidis | 0f84a23 | 2008-11-09 22:53:32 +0000 | [diff] [blame] | 2874 | DC = static_cast<DeclContext*>(SS.getScopeRep()); |
| 2875 | // Look-up name inside 'foo::'. |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 2876 | PrevDecl = dyn_cast_or_null<TagDecl>(LookupDecl(Name, Decl::IDNS_Tag,S,DC) |
| 2877 | .getAsDecl()); |
Argyrios Kyrtzidis | 630c81b | 2008-11-09 22:09:58 +0000 | [diff] [blame] | 2878 | |
| 2879 | // A tag 'foo::bar' must already exist. |
| 2880 | if (PrevDecl == 0) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2881 | Diag(NameLoc, diag::err_not_tag_in_scope) << Name << SS.getRange(); |
Argyrios Kyrtzidis | 630c81b | 2008-11-09 22:09:58 +0000 | [diff] [blame] | 2882 | Name = 0; |
| 2883 | goto CreateNewDecl; |
| 2884 | } |
Chris Lattner | cf79b01 | 2009-01-21 02:38:50 +0000 | [diff] [blame] | 2885 | } else if (Name) { |
Argyrios Kyrtzidis | 630c81b | 2008-11-09 22:09:58 +0000 | [diff] [blame] | 2886 | // If this is a named struct, check to see if there was a previous forward |
| 2887 | // declaration or definition. |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2888 | PrevDecl = dyn_cast_or_null<NamedDecl>(LookupDecl(Name, Decl::IDNS_Tag,S) |
Chris Lattner | cf79b01 | 2009-01-21 02:38:50 +0000 | [diff] [blame] | 2889 | .getAsDecl()); |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 2890 | |
| 2891 | if (!getLangOptions().CPlusPlus && TK != TK_Reference) { |
| 2892 | // FIXME: This makes sure that we ignore the contexts associated |
| 2893 | // with C structs, unions, and enums when looking for a matching |
| 2894 | // tag declaration or definition. See the similar lookup tweak |
| 2895 | // in Sema::LookupDecl; is there a better way to deal with this? |
Douglas Gregor | 4920f1f | 2009-01-12 22:49:06 +0000 | [diff] [blame] | 2896 | while (isa<RecordDecl>(SearchDC) || isa<EnumDecl>(SearchDC)) |
| 2897 | SearchDC = SearchDC->getParent(); |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 2898 | } |
Argyrios Kyrtzidis | 630c81b | 2008-11-09 22:09:58 +0000 | [diff] [blame] | 2899 | } |
| 2900 | |
Douglas Gregor | f57172b | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 2901 | if (PrevDecl && PrevDecl->isTemplateParameter()) { |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 2902 | // Maybe we will complain about the shadowed template parameter. |
| 2903 | DiagnoseTemplateParameterShadow(NameLoc, PrevDecl); |
| 2904 | // Just pretend that we didn't see the previous declaration. |
| 2905 | PrevDecl = 0; |
| 2906 | } |
| 2907 | |
Ted Kremenek | 7e8cc57 | 2008-09-02 21:26:19 +0000 | [diff] [blame] | 2908 | if (PrevDecl) { |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 2909 | if (TagDecl *PrevTagDecl = dyn_cast<TagDecl>(PrevDecl)) { |
Chris Lattner | 14943b9 | 2008-07-03 03:30:58 +0000 | [diff] [blame] | 2910 | // If this is a use of a previous tag, or if the tag is already declared |
| 2911 | // in the same scope (so that the definition/declaration completes or |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 2912 | // rementions the tag), reuse the decl. |
Douglas Gregor | 4920f1f | 2009-01-12 22:49:06 +0000 | [diff] [blame] | 2913 | if (TK == TK_Reference || isDeclInScope(PrevDecl, SearchDC, S)) { |
Chris Lattner | 14943b9 | 2008-07-03 03:30:58 +0000 | [diff] [blame] | 2914 | // Make sure that this wasn't declared as an enum and now used as a |
| 2915 | // struct or something similar. |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 2916 | if (PrevTagDecl->getTagKind() != Kind) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2917 | Diag(KWLoc, diag::err_use_with_wrong_tag) << Name; |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 2918 | Diag(PrevDecl->getLocation(), diag::note_previous_use); |
Chris Lattner | 14943b9 | 2008-07-03 03:30:58 +0000 | [diff] [blame] | 2919 | // Recover by making this an anonymous redefinition. |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 2920 | Name = 0; |
Chris Lattner | 14943b9 | 2008-07-03 03:30:58 +0000 | [diff] [blame] | 2921 | PrevDecl = 0; |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 2922 | Invalid = true; |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 2923 | } else { |
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 2924 | // If this is a use, just return the declaration we found. |
Chris Lattner | 14943b9 | 2008-07-03 03:30:58 +0000 | [diff] [blame] | 2925 | |
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 2926 | // FIXME: In the future, return a variant or some other clue |
| 2927 | // for the consumer of this Decl to know it doesn't own it. |
| 2928 | // For our current ASTs this shouldn't be a problem, but will |
| 2929 | // need to be changed with DeclGroups. |
| 2930 | if (TK == TK_Reference) |
Chris Lattner | 14943b9 | 2008-07-03 03:30:58 +0000 | [diff] [blame] | 2931 | return PrevDecl; |
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 2932 | |
| 2933 | // Diagnose attempts to redefine a tag. |
| 2934 | if (TK == TK_Definition) { |
| 2935 | if (TagDecl *Def = PrevTagDecl->getDefinition(Context)) { |
| 2936 | Diag(NameLoc, diag::err_redefinition) << Name; |
| 2937 | Diag(Def->getLocation(), diag::note_previous_definition); |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 2938 | // If this is a redefinition, recover by making this |
| 2939 | // struct be anonymous, which will make any later |
| 2940 | // references get the previous definition. |
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 2941 | Name = 0; |
| 2942 | PrevDecl = 0; |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 2943 | Invalid = true; |
| 2944 | } else { |
| 2945 | // If the type is currently being defined, complain |
| 2946 | // about a nested redefinition. |
| 2947 | TagType *Tag = cast<TagType>(Context.getTagDeclType(PrevTagDecl)); |
| 2948 | if (Tag->isBeingDefined()) { |
| 2949 | Diag(NameLoc, diag::err_nested_redefinition) << Name; |
| 2950 | Diag(PrevTagDecl->getLocation(), |
| 2951 | diag::note_previous_definition); |
| 2952 | Name = 0; |
| 2953 | PrevDecl = 0; |
| 2954 | Invalid = true; |
| 2955 | } |
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 2956 | } |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 2957 | |
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 2958 | // Okay, this is definition of a previously declared or referenced |
| 2959 | // tag PrevDecl. We're going to create a new Decl for it. |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 2960 | } |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 2961 | } |
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 2962 | // If we get here we have (another) forward declaration or we |
| 2963 | // have a definition. Just create a new decl. |
| 2964 | } else { |
| 2965 | // If we get here, this is a definition of a new tag type in a nested |
| 2966 | // scope, e.g. "struct foo; void bar() { struct foo; }", just create a |
| 2967 | // new decl/type. We set PrevDecl to NULL so that the entities |
| 2968 | // have distinct types. |
| 2969 | PrevDecl = 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2970 | } |
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 2971 | // If we get here, we're going to create a new Decl. If PrevDecl |
| 2972 | // is non-NULL, it's a definition of the tag declared by |
| 2973 | // PrevDecl. If it's NULL, we have a new definition. |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 2974 | } else { |
Douglas Gregor | 6697312 | 2009-01-28 17:15:10 +0000 | [diff] [blame] | 2975 | // PrevDecl is a namespace, template, or anything else |
| 2976 | // that lives in the IDNS_Tag identifier namespace. |
Douglas Gregor | 4920f1f | 2009-01-12 22:49:06 +0000 | [diff] [blame] | 2977 | if (isDeclInScope(PrevDecl, SearchDC, S)) { |
Ted Kremenek | a89d197 | 2008-09-03 18:03:35 +0000 | [diff] [blame] | 2978 | // The tag name clashes with a namespace name, issue an error and |
| 2979 | // recover by making this tag be anonymous. |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2980 | Diag(NameLoc, diag::err_redefinition_different_kind) << Name; |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 2981 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Argyrios Kyrtzidis | b02ef24 | 2008-07-16 07:45:46 +0000 | [diff] [blame] | 2982 | Name = 0; |
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 2983 | PrevDecl = 0; |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 2984 | Invalid = true; |
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 2985 | } else { |
| 2986 | // The existing declaration isn't relevant to us; we're in a |
| 2987 | // new scope, so clear out the previous declaration. |
| 2988 | PrevDecl = 0; |
Argyrios Kyrtzidis | b02ef24 | 2008-07-16 07:45:46 +0000 | [diff] [blame] | 2989 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2990 | } |
Douglas Gregor | 3218c4b | 2009-01-09 22:42:13 +0000 | [diff] [blame] | 2991 | } else if (TK == TK_Reference && SS.isEmpty() && Name && |
| 2992 | (Kind != TagDecl::TK_enum)) { |
| 2993 | // C++ [basic.scope.pdecl]p5: |
| 2994 | // -- for an elaborated-type-specifier of the form |
| 2995 | // |
| 2996 | // class-key identifier |
| 2997 | // |
| 2998 | // if the elaborated-type-specifier is used in the |
| 2999 | // decl-specifier-seq or parameter-declaration-clause of a |
| 3000 | // function defined in namespace scope, the identifier is |
| 3001 | // declared as a class-name in the namespace that contains |
| 3002 | // the declaration; otherwise, except as a friend |
| 3003 | // declaration, the identifier is declared in the smallest |
| 3004 | // non-class, non-function-prototype scope that contains the |
| 3005 | // declaration. |
| 3006 | // |
| 3007 | // C99 6.7.2.3p8 has a similar (but not identical!) provision for |
| 3008 | // C structs and unions. |
| 3009 | |
| 3010 | // Find the context where we'll be declaring the tag. |
Douglas Gregor | 4920f1f | 2009-01-12 22:49:06 +0000 | [diff] [blame] | 3011 | // FIXME: We would like to maintain the current DeclContext as the |
| 3012 | // lexical context, |
Douglas Gregor | 3218c4b | 2009-01-09 22:42:13 +0000 | [diff] [blame] | 3013 | while (DC->isRecord()) |
| 3014 | DC = DC->getParent(); |
| 3015 | LexicalContext = DC; |
| 3016 | |
| 3017 | // Find the scope where we'll be declaring the tag. |
| 3018 | while (S->isClassScope() || |
| 3019 | (getLangOptions().CPlusPlus && S->isFunctionPrototypeScope()) || |
Douglas Gregor | 1a0d31a | 2009-01-12 18:45:55 +0000 | [diff] [blame] | 3020 | ((S->getFlags() & Scope::DeclScope) == 0) || |
| 3021 | (S->getEntity() && |
| 3022 | ((DeclContext *)S->getEntity())->isTransparentContext())) |
Douglas Gregor | 3218c4b | 2009-01-09 22:42:13 +0000 | [diff] [blame] | 3023 | S = S->getParent(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3024 | } |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 3025 | |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 3026 | CreateNewDecl: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3027 | |
| 3028 | // If there is an identifier, use the location of the identifier as the |
| 3029 | // location of the decl, otherwise use the location of the struct/union |
| 3030 | // keyword. |
| 3031 | SourceLocation Loc = NameLoc.isValid() ? NameLoc : KWLoc; |
| 3032 | |
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 3033 | // Otherwise, create a new declaration. If there is a previous |
| 3034 | // declaration of the same entity, the two will be linked via |
| 3035 | // PrevDecl. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3036 | TagDecl *New; |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3037 | |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 3038 | if (Kind == TagDecl::TK_enum) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3039 | // FIXME: Tag decls should be chained to any simultaneous vardecls, e.g.: |
| 3040 | // enum X { A, B, C } D; D should chain to X. |
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 3041 | New = EnumDecl::Create(Context, DC, Loc, Name, |
| 3042 | cast_or_null<EnumDecl>(PrevDecl)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3043 | // If this is an undefined enum, warn. |
| 3044 | if (TK != TK_Definition) Diag(Loc, diag::ext_forward_ref_enum); |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 3045 | } else { |
| 3046 | // struct/union/class |
| 3047 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3048 | // FIXME: Tag decls should be chained to any simultaneous vardecls, e.g.: |
| 3049 | // struct X { int A; } D; D should chain to X. |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 3050 | if (getLangOptions().CPlusPlus) |
Ted Kremenek | 2b345eb | 2008-09-05 17:39:33 +0000 | [diff] [blame] | 3051 | // FIXME: Look for a way to use RecordDecl for simple structs. |
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 3052 | New = CXXRecordDecl::Create(Context, Kind, DC, Loc, Name, |
| 3053 | cast_or_null<CXXRecordDecl>(PrevDecl)); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 3054 | else |
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 3055 | New = RecordDecl::Create(Context, Kind, DC, Loc, Name, |
| 3056 | cast_or_null<RecordDecl>(PrevDecl)); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 3057 | } |
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 3058 | |
| 3059 | if (Kind != TagDecl::TK_enum) { |
| 3060 | // Handle #pragma pack: if the #pragma pack stack has non-default |
| 3061 | // alignment, make up a packed attribute for this decl. These |
| 3062 | // attributes are checked when the ASTContext lays out the |
| 3063 | // structure. |
| 3064 | // |
| 3065 | // It is important for implementing the correct semantics that this |
| 3066 | // happen here (in act on tag decl). The #pragma pack stack is |
| 3067 | // maintained as a result of parser callbacks which can occur at |
| 3068 | // many points during the parsing of a struct declaration (because |
| 3069 | // the #pragma tokens are effectively skipped over during the |
| 3070 | // parsing of the struct). |
| 3071 | if (unsigned Alignment = PackContext.getAlignment()) |
| 3072 | New->addAttr(new PackedAttr(Alignment * 8)); |
| 3073 | } |
| 3074 | |
Douglas Gregor | 6697312 | 2009-01-28 17:15:10 +0000 | [diff] [blame] | 3075 | if (getLangOptions().CPlusPlus && SS.isEmpty() && Name && !Invalid) { |
| 3076 | // C++ [dcl.typedef]p3: |
| 3077 | // [...] Similarly, in a given scope, a class or enumeration |
| 3078 | // shall not be declared with the same name as a typedef-name |
| 3079 | // that is declared in that scope and refers to a type other |
| 3080 | // than the class or enumeration itself. |
| 3081 | LookupResult Lookup = LookupName(S, Name, |
| 3082 | LookupCriteria(LookupCriteria::Ordinary, |
| 3083 | true, true)); |
| 3084 | TypedefDecl *PrevTypedef = 0; |
| 3085 | if (Lookup.getKind() == LookupResult::Found) |
| 3086 | PrevTypedef = dyn_cast<TypedefDecl>(Lookup.getAsDecl()); |
| 3087 | |
| 3088 | if (PrevTypedef && isDeclInScope(PrevTypedef, SearchDC, S) && |
| 3089 | Context.getCanonicalType(Context.getTypeDeclType(PrevTypedef)) != |
| 3090 | Context.getCanonicalType(Context.getTypeDeclType(New))) { |
| 3091 | Diag(Loc, diag::err_tag_definition_of_typedef) |
| 3092 | << Context.getTypeDeclType(New) |
| 3093 | << PrevTypedef->getUnderlyingType(); |
| 3094 | Diag(PrevTypedef->getLocation(), diag::note_previous_definition); |
| 3095 | Invalid = true; |
| 3096 | } |
| 3097 | } |
| 3098 | |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 3099 | if (Invalid) |
| 3100 | New->setInvalidDecl(); |
| 3101 | |
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 3102 | if (Attr) |
| 3103 | ProcessDeclAttributeList(New, Attr); |
| 3104 | |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 3105 | // If we're declaring or defining a tag in function prototype scope |
| 3106 | // in C, note that this type can only be used within the function. |
Douglas Gregor | 3218c4b | 2009-01-09 22:42:13 +0000 | [diff] [blame] | 3107 | if (Name && S->isFunctionPrototypeScope() && !getLangOptions().CPlusPlus) |
| 3108 | Diag(Loc, diag::warn_decl_in_param_list) << Context.getTagDeclType(New); |
| 3109 | |
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 3110 | // Set the lexical context. If the tag has a C++ scope specifier, the |
| 3111 | // lexical context will be different from the semantic context. |
Douglas Gregor | 3218c4b | 2009-01-09 22:42:13 +0000 | [diff] [blame] | 3112 | New->setLexicalDeclContext(LexicalContext); |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 3113 | |
| 3114 | if (TK == TK_Definition) |
| 3115 | New->startDefinition(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3116 | |
| 3117 | // If this has an identifier, add it to the scope stack. |
| 3118 | if (Name) { |
Douglas Gregor | 1a0d31a | 2009-01-12 18:45:55 +0000 | [diff] [blame] | 3119 | S = getNonFieldDeclScope(S); |
Chris Lattner | 31e0572 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 3120 | |
| 3121 | // Add it to the decl chain. |
Douglas Gregor | 3218c4b | 2009-01-09 22:42:13 +0000 | [diff] [blame] | 3122 | if (LexicalContext != CurContext) { |
| 3123 | // FIXME: PushOnScopeChains should not rely on CurContext! |
| 3124 | DeclContext *OldContext = CurContext; |
| 3125 | CurContext = LexicalContext; |
| 3126 | PushOnScopeChains(New, S); |
| 3127 | CurContext = OldContext; |
| 3128 | } else |
| 3129 | PushOnScopeChains(New, S); |
Douglas Gregor | 4920f1f | 2009-01-12 22:49:06 +0000 | [diff] [blame] | 3130 | } else { |
Douglas Gregor | 482b77d | 2009-01-12 23:27:07 +0000 | [diff] [blame] | 3131 | LexicalContext->addDecl(New); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3132 | } |
Argyrios Kyrtzidis | 5239304 | 2008-11-09 23:41:00 +0000 | [diff] [blame] | 3133 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3134 | return New; |
| 3135 | } |
| 3136 | |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 3137 | void Sema::ActOnTagStartDefinition(Scope *S, DeclTy *TagD) { |
| 3138 | TagDecl *Tag = cast<TagDecl>((Decl *)TagD); |
| 3139 | |
| 3140 | // Enter the tag context. |
| 3141 | PushDeclContext(S, Tag); |
| 3142 | |
| 3143 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Tag)) { |
| 3144 | FieldCollector->StartClass(); |
| 3145 | |
| 3146 | if (Record->getIdentifier()) { |
| 3147 | // C++ [class]p2: |
| 3148 | // [...] The class-name is also inserted into the scope of the |
| 3149 | // class itself; this is known as the injected-class-name. For |
| 3150 | // purposes of access checking, the injected-class-name is treated |
| 3151 | // as if it were a public member name. |
| 3152 | RecordDecl *InjectedClassName |
| 3153 | = CXXRecordDecl::Create(Context, Record->getTagKind(), |
| 3154 | CurContext, Record->getLocation(), |
| 3155 | Record->getIdentifier(), Record); |
| 3156 | InjectedClassName->setImplicit(); |
| 3157 | PushOnScopeChains(InjectedClassName, S); |
| 3158 | } |
| 3159 | } |
| 3160 | } |
| 3161 | |
| 3162 | void Sema::ActOnTagFinishDefinition(Scope *S, DeclTy *TagD) { |
| 3163 | TagDecl *Tag = cast<TagDecl>((Decl *)TagD); |
| 3164 | |
| 3165 | if (isa<CXXRecordDecl>(Tag)) |
| 3166 | FieldCollector->FinishClass(); |
| 3167 | |
| 3168 | // Exit this scope of this tag's definition. |
| 3169 | PopDeclContext(); |
| 3170 | |
| 3171 | // Notify the consumer that we've defined a tag. |
| 3172 | Consumer.HandleTagDeclDefinition(Tag); |
| 3173 | } |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 3174 | |
Chris Lattner | 1d353ba | 2008-11-12 21:17:48 +0000 | [diff] [blame] | 3175 | /// TryToFixInvalidVariablyModifiedType - Helper method to turn variable array |
| 3176 | /// types into constant array types in certain situations which would otherwise |
| 3177 | /// be errors (for GCC compatibility). |
| 3178 | static QualType TryToFixInvalidVariablyModifiedType(QualType T, |
| 3179 | ASTContext &Context) { |
Eli Friedman | 1b76ada | 2008-06-03 21:01:11 +0000 | [diff] [blame] | 3180 | // This method tries to turn a variable array into a constant |
| 3181 | // array even when the size isn't an ICE. This is necessary |
| 3182 | // for compatibility with code that depends on gcc's buggy |
| 3183 | // constant expression folding, like struct {char x[(int)(char*)2];} |
Chris Lattner | 57d5788 | 2008-11-12 19:48:13 +0000 | [diff] [blame] | 3184 | const VariableArrayType* VLATy = dyn_cast<VariableArrayType>(T); |
| 3185 | if (!VLATy) return QualType(); |
| 3186 | |
Anders Carlsson | 1c0cfd4 | 2008-12-19 20:58:05 +0000 | [diff] [blame] | 3187 | Expr::EvalResult EvalResult; |
Chris Lattner | 57d5788 | 2008-11-12 19:48:13 +0000 | [diff] [blame] | 3188 | if (!VLATy->getSizeExpr() || |
Anders Carlsson | 1c0cfd4 | 2008-12-19 20:58:05 +0000 | [diff] [blame] | 3189 | !VLATy->getSizeExpr()->Evaluate(EvalResult, Context)) |
Chris Lattner | 57d5788 | 2008-11-12 19:48:13 +0000 | [diff] [blame] | 3190 | return QualType(); |
| 3191 | |
Anders Carlsson | 1c0cfd4 | 2008-12-19 20:58:05 +0000 | [diff] [blame] | 3192 | assert(EvalResult.Val.isInt() && "Size expressions must be integers!"); |
| 3193 | llvm::APSInt &Res = EvalResult.Val.getInt(); |
Chris Lattner | 57d5788 | 2008-11-12 19:48:13 +0000 | [diff] [blame] | 3194 | if (Res > llvm::APSInt(Res.getBitWidth(), Res.isUnsigned())) |
| 3195 | return Context.getConstantArrayType(VLATy->getElementType(), |
| 3196 | Res, ArrayType::Normal, 0); |
Eli Friedman | 1b76ada | 2008-06-03 21:01:11 +0000 | [diff] [blame] | 3197 | return QualType(); |
| 3198 | } |
| 3199 | |
Anders Carlsson | 9f1e572 | 2008-12-06 20:33:04 +0000 | [diff] [blame] | 3200 | bool Sema::VerifyBitField(SourceLocation FieldLoc, IdentifierInfo *FieldName, |
Chris Lattner | cd08707 | 2008-12-12 04:56:04 +0000 | [diff] [blame] | 3201 | QualType FieldTy, const Expr *BitWidth) { |
Anders Carlsson | 9f1e572 | 2008-12-06 20:33:04 +0000 | [diff] [blame] | 3202 | // FIXME: 6.7.2.1p4 - verify the field type. |
| 3203 | |
| 3204 | llvm::APSInt Value; |
| 3205 | if (VerifyIntegerConstantExpression(BitWidth, &Value)) |
| 3206 | return true; |
| 3207 | |
Chris Lattner | cd08707 | 2008-12-12 04:56:04 +0000 | [diff] [blame] | 3208 | // Zero-width bitfield is ok for anonymous field. |
| 3209 | if (Value == 0 && FieldName) |
| 3210 | return Diag(FieldLoc, diag::err_bitfield_has_zero_width) << FieldName; |
| 3211 | |
| 3212 | if (Value.isNegative()) |
| 3213 | return Diag(FieldLoc, diag::err_bitfield_has_negative_width) << FieldName; |
Anders Carlsson | 9f1e572 | 2008-12-06 20:33:04 +0000 | [diff] [blame] | 3214 | |
| 3215 | uint64_t TypeSize = Context.getTypeSize(FieldTy); |
| 3216 | // FIXME: We won't need the 0 size once we check that the field type is valid. |
Chris Lattner | cd08707 | 2008-12-12 04:56:04 +0000 | [diff] [blame] | 3217 | if (TypeSize && Value.getZExtValue() > TypeSize) |
| 3218 | return Diag(FieldLoc, diag::err_bitfield_width_exceeds_type_size) |
| 3219 | << FieldName << (unsigned)TypeSize; |
Anders Carlsson | 9f1e572 | 2008-12-06 20:33:04 +0000 | [diff] [blame] | 3220 | |
| 3221 | return false; |
| 3222 | } |
| 3223 | |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 3224 | /// 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] | 3225 | /// to create a FieldDecl object for it. |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3226 | Sema::DeclTy *Sema::ActOnField(Scope *S, DeclTy *TagD, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3227 | SourceLocation DeclStart, |
| 3228 | Declarator &D, ExprTy *BitfieldWidth) { |
| 3229 | IdentifierInfo *II = D.getIdentifier(); |
| 3230 | Expr *BitWidth = (Expr*)BitfieldWidth; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3231 | SourceLocation Loc = DeclStart; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3232 | RecordDecl *Record = (RecordDecl *)TagD; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3233 | if (II) Loc = D.getIdentifierLoc(); |
| 3234 | |
| 3235 | // FIXME: Unnamed fields can be handled in various different ways, for |
| 3236 | // example, unnamed unions inject all members into the struct namespace! |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3237 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3238 | QualType T = GetTypeForDeclarator(D, S); |
Steve Naroff | 5912a35 | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 3239 | assert(!T.isNull() && "GetTypeForDeclarator() returned null type"); |
| 3240 | bool InvalidDecl = false; |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 3241 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3242 | // C99 6.7.2.1p8: A member of a structure or union may have any type other |
| 3243 | // than a variably modified type. |
Eli Friedman | 9db1397 | 2008-02-15 12:53:51 +0000 | [diff] [blame] | 3244 | if (T->isVariablyModifiedType()) { |
Chris Lattner | 1d353ba | 2008-11-12 21:17:48 +0000 | [diff] [blame] | 3245 | QualType FixedTy = TryToFixInvalidVariablyModifiedType(T, Context); |
Eli Friedman | 1b76ada | 2008-06-03 21:01:11 +0000 | [diff] [blame] | 3246 | if (!FixedTy.isNull()) { |
Chris Lattner | 23cd0d9 | 2008-11-13 18:49:38 +0000 | [diff] [blame] | 3247 | Diag(Loc, diag::warn_illegal_constant_array_size); |
Eli Friedman | 1b76ada | 2008-06-03 21:01:11 +0000 | [diff] [blame] | 3248 | T = FixedTy; |
| 3249 | } else { |
Chris Lattner | 23cd0d9 | 2008-11-13 18:49:38 +0000 | [diff] [blame] | 3250 | Diag(Loc, diag::err_typecheck_field_variable_size); |
Chris Lattner | 3ab5543 | 2008-11-12 19:45:49 +0000 | [diff] [blame] | 3251 | T = Context.IntTy; |
Eli Friedman | 1b76ada | 2008-06-03 21:01:11 +0000 | [diff] [blame] | 3252 | InvalidDecl = true; |
| 3253 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3254 | } |
Anders Carlsson | 9f1e572 | 2008-12-06 20:33:04 +0000 | [diff] [blame] | 3255 | |
| 3256 | if (BitWidth) { |
| 3257 | if (VerifyBitField(Loc, II, T, BitWidth)) |
| 3258 | InvalidDecl = true; |
| 3259 | } else { |
| 3260 | // Not a bitfield. |
| 3261 | |
| 3262 | // validate II. |
| 3263 | |
| 3264 | } |
| 3265 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3266 | // FIXME: Chain fielddecls together. |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 3267 | FieldDecl *NewFD; |
| 3268 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3269 | NewFD = FieldDecl::Create(Context, Record, |
| 3270 | Loc, II, T, BitWidth, |
| 3271 | D.getDeclSpec().getStorageClassSpec() == |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 3272 | DeclSpec::SCS_mutable); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3273 | |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 3274 | if (II) { |
| 3275 | Decl *PrevDecl |
Steve Naroff | 133147d | 2009-01-28 16:09:22 +0000 | [diff] [blame] | 3276 | = LookupDecl(II, Decl::IDNS_Member, S, 0, false); |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 3277 | if (PrevDecl && isDeclInScope(PrevDecl, CurContext, S) |
| 3278 | && !isa<TagDecl>(PrevDecl)) { |
| 3279 | Diag(Loc, diag::err_duplicate_member) << II; |
| 3280 | Diag(PrevDecl->getLocation(), diag::note_previous_declaration); |
| 3281 | NewFD->setInvalidDecl(); |
| 3282 | Record->setInvalidDecl(); |
| 3283 | } |
| 3284 | } |
| 3285 | |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 3286 | if (getLangOptions().CPlusPlus) { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 3287 | CheckExtraCXXDefaultArguments(D); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 3288 | if (!T->isPODType()) |
| 3289 | cast<CXXRecordDecl>(Record)->setPOD(false); |
| 3290 | } |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 3291 | |
Chris Lattner | 3ff30c8 | 2008-06-29 00:02:00 +0000 | [diff] [blame] | 3292 | ProcessDeclAttributes(NewFD, D); |
Anders Carlsson | ad14806 | 2008-02-16 00:29:18 +0000 | [diff] [blame] | 3293 | |
Steve Naroff | 5912a35 | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 3294 | if (D.getInvalidType() || InvalidDecl) |
| 3295 | NewFD->setInvalidDecl(); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3296 | |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 3297 | if (II) { |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3298 | PushOnScopeChains(NewFD, S); |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 3299 | } else |
Douglas Gregor | 482b77d | 2009-01-12 23:27:07 +0000 | [diff] [blame] | 3300 | Record->addDecl(NewFD); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3301 | |
Steve Naroff | 5912a35 | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 3302 | return NewFD; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3303 | } |
| 3304 | |
Fariborz Jahanian | 89204a1 | 2007-10-01 16:53:59 +0000 | [diff] [blame] | 3305 | /// TranslateIvarVisibility - Translate visibility from a token ID to an |
| 3306 | /// AST enum value. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3307 | static ObjCIvarDecl::AccessControl |
Fariborz Jahanian | 89204a1 | 2007-10-01 16:53:59 +0000 | [diff] [blame] | 3308 | TranslateIvarVisibility(tok::ObjCKeywordKind ivarVisibility) { |
Steve Naroff | f13271f | 2007-09-14 23:09:53 +0000 | [diff] [blame] | 3309 | switch (ivarVisibility) { |
Chris Lattner | 33d34a6 | 2008-10-12 00:28:42 +0000 | [diff] [blame] | 3310 | default: assert(0 && "Unknown visitibility kind"); |
| 3311 | case tok::objc_private: return ObjCIvarDecl::Private; |
| 3312 | case tok::objc_public: return ObjCIvarDecl::Public; |
| 3313 | case tok::objc_protected: return ObjCIvarDecl::Protected; |
| 3314 | case tok::objc_package: return ObjCIvarDecl::Package; |
Steve Naroff | f13271f | 2007-09-14 23:09:53 +0000 | [diff] [blame] | 3315 | } |
| 3316 | } |
| 3317 | |
Fariborz Jahanian | 45bc03f | 2008-04-11 16:55:42 +0000 | [diff] [blame] | 3318 | /// ActOnIvar - Each ivar field of an objective-c class is passed into this |
| 3319 | /// in order to create an IvarDecl object for it. |
Fariborz Jahanian | 1d78cc4 | 2008-04-10 23:32:45 +0000 | [diff] [blame] | 3320 | Sema::DeclTy *Sema::ActOnIvar(Scope *S, |
Fariborz Jahanian | 45bc03f | 2008-04-11 16:55:42 +0000 | [diff] [blame] | 3321 | SourceLocation DeclStart, |
| 3322 | Declarator &D, ExprTy *BitfieldWidth, |
| 3323 | tok::ObjCKeywordKind Visibility) { |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 3324 | |
Fariborz Jahanian | 1d78cc4 | 2008-04-10 23:32:45 +0000 | [diff] [blame] | 3325 | IdentifierInfo *II = D.getIdentifier(); |
| 3326 | Expr *BitWidth = (Expr*)BitfieldWidth; |
| 3327 | SourceLocation Loc = DeclStart; |
| 3328 | if (II) Loc = D.getIdentifierLoc(); |
| 3329 | |
| 3330 | // FIXME: Unnamed fields can be handled in various different ways, for |
| 3331 | // example, unnamed unions inject all members into the struct namespace! |
| 3332 | |
Anders Carlsson | 9f1e572 | 2008-12-06 20:33:04 +0000 | [diff] [blame] | 3333 | QualType T = GetTypeForDeclarator(D, S); |
| 3334 | assert(!T.isNull() && "GetTypeForDeclarator() returned null type"); |
| 3335 | bool InvalidDecl = false; |
Fariborz Jahanian | 1d78cc4 | 2008-04-10 23:32:45 +0000 | [diff] [blame] | 3336 | |
| 3337 | if (BitWidth) { |
| 3338 | // TODO: Validate. |
| 3339 | //printf("WARNING: BITFIELDS IGNORED!\n"); |
| 3340 | |
| 3341 | // 6.7.2.1p3 |
| 3342 | // 6.7.2.1p4 |
| 3343 | |
| 3344 | } else { |
| 3345 | // Not a bitfield. |
| 3346 | |
| 3347 | // validate II. |
| 3348 | |
| 3349 | } |
| 3350 | |
Fariborz Jahanian | 1d78cc4 | 2008-04-10 23:32:45 +0000 | [diff] [blame] | 3351 | // C99 6.7.2.1p8: A member of a structure or union may have any type other |
| 3352 | // than a variably modified type. |
| 3353 | if (T->isVariablyModifiedType()) { |
Anders Carlsson | 96e05bc | 2008-12-07 00:20:55 +0000 | [diff] [blame] | 3354 | Diag(Loc, diag::err_typecheck_ivar_variable_size); |
Fariborz Jahanian | 1d78cc4 | 2008-04-10 23:32:45 +0000 | [diff] [blame] | 3355 | InvalidDecl = true; |
| 3356 | } |
| 3357 | |
Ted Kremenek | b8db21d | 2008-07-23 18:04:17 +0000 | [diff] [blame] | 3358 | // Get the visibility (access control) for this ivar. |
| 3359 | ObjCIvarDecl::AccessControl ac = |
| 3360 | Visibility != tok::objc_not_keyword ? TranslateIvarVisibility(Visibility) |
| 3361 | : ObjCIvarDecl::None; |
| 3362 | |
| 3363 | // Construct the decl. |
| 3364 | ObjCIvarDecl *NewID = ObjCIvarDecl::Create(Context, Loc, II, T, ac, |
Steve Naroff | 8f3b265 | 2008-07-16 18:22:22 +0000 | [diff] [blame] | 3365 | (Expr *)BitfieldWidth); |
Fariborz Jahanian | 1d78cc4 | 2008-04-10 23:32:45 +0000 | [diff] [blame] | 3366 | |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 3367 | if (II) { |
| 3368 | Decl *PrevDecl |
Steve Naroff | 133147d | 2009-01-28 16:09:22 +0000 | [diff] [blame] | 3369 | = LookupDecl(II, Decl::IDNS_Member, S, 0, false); |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 3370 | if (PrevDecl && isDeclInScope(PrevDecl, CurContext, S) |
| 3371 | && !isa<TagDecl>(PrevDecl)) { |
| 3372 | Diag(Loc, diag::err_duplicate_member) << II; |
| 3373 | Diag(PrevDecl->getLocation(), diag::note_previous_declaration); |
| 3374 | NewID->setInvalidDecl(); |
| 3375 | } |
| 3376 | } |
| 3377 | |
Ted Kremenek | b8db21d | 2008-07-23 18:04:17 +0000 | [diff] [blame] | 3378 | // Process attributes attached to the ivar. |
Chris Lattner | 3ff30c8 | 2008-06-29 00:02:00 +0000 | [diff] [blame] | 3379 | ProcessDeclAttributes(NewID, D); |
Fariborz Jahanian | 1d78cc4 | 2008-04-10 23:32:45 +0000 | [diff] [blame] | 3380 | |
| 3381 | if (D.getInvalidType() || InvalidDecl) |
| 3382 | NewID->setInvalidDecl(); |
Ted Kremenek | b8db21d | 2008-07-23 18:04:17 +0000 | [diff] [blame] | 3383 | |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 3384 | if (II) { |
| 3385 | // FIXME: When interfaces are DeclContexts, we'll need to add |
| 3386 | // these to the interface. |
| 3387 | S->AddDecl(NewID); |
| 3388 | IdResolver.AddDecl(NewID); |
| 3389 | } |
| 3390 | |
Fariborz Jahanian | 1d78cc4 | 2008-04-10 23:32:45 +0000 | [diff] [blame] | 3391 | return NewID; |
| 3392 | } |
| 3393 | |
Fariborz Jahanian | 9d048ff | 2007-09-29 00:54:24 +0000 | [diff] [blame] | 3394 | void Sema::ActOnFields(Scope* S, |
Fariborz Jahanian | 3f5faf7 | 2007-10-04 00:45:27 +0000 | [diff] [blame] | 3395 | SourceLocation RecLoc, DeclTy *RecDecl, |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 3396 | DeclTy **Fields, unsigned NumFields, |
Daniel Dunbar | 1bfe1c2 | 2008-10-03 02:03:53 +0000 | [diff] [blame] | 3397 | SourceLocation LBrac, SourceLocation RBrac, |
Daniel Dunbar | 7d07664 | 2008-10-03 17:33:35 +0000 | [diff] [blame] | 3398 | AttributeList *Attr) { |
Steve Naroff | 7421664 | 2007-09-14 22:20:54 +0000 | [diff] [blame] | 3399 | Decl *EnclosingDecl = static_cast<Decl*>(RecDecl); |
| 3400 | assert(EnclosingDecl && "missing record or interface decl"); |
| 3401 | RecordDecl *Record = dyn_cast<RecordDecl>(EnclosingDecl); |
| 3402 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3403 | // Verify that all the fields are okay. |
| 3404 | unsigned NumNamedMembers = 0; |
| 3405 | llvm::SmallVector<FieldDecl*, 32> RecFields; |
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 3406 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3407 | for (unsigned i = 0; i != NumFields; ++i) { |
Steve Naroff | 7421664 | 2007-09-14 22:20:54 +0000 | [diff] [blame] | 3408 | FieldDecl *FD = cast_or_null<FieldDecl>(static_cast<Decl*>(Fields[i])); |
| 3409 | assert(FD && "missing field decl"); |
| 3410 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3411 | // Get the type for the field. |
Chris Lattner | 02c642e | 2007-07-31 21:33:24 +0000 | [diff] [blame] | 3412 | Type *FDTy = FD->getType().getTypePtr(); |
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 3413 | |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 3414 | if (!FD->isAnonymousStructOrUnion()) { |
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 3415 | // Remember all fields written by the user. |
| 3416 | RecFields.push_back(FD); |
| 3417 | } |
Steve Naroff | f13271f | 2007-09-14 23:09:53 +0000 | [diff] [blame] | 3418 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3419 | // 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] | 3420 | if (FDTy->isFunctionType()) { |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 3421 | Diag(FD->getLocation(), diag::err_field_declared_as_function) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3422 | << FD->getDeclName(); |
Steve Naroff | 7421664 | 2007-09-14 22:20:54 +0000 | [diff] [blame] | 3423 | FD->setInvalidDecl(); |
| 3424 | EnclosingDecl->setInvalidDecl(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3425 | continue; |
| 3426 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3427 | // C99 6.7.2.1p2 - A field may not be an incomplete type except... |
| 3428 | if (FDTy->isIncompleteType()) { |
Fariborz Jahanian | e267ab6 | 2007-09-14 16:27:55 +0000 | [diff] [blame] | 3429 | if (!Record) { // Incomplete ivar type is always an error. |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 3430 | DiagnoseIncompleteType(FD->getLocation(), FD->getType(), |
| 3431 | diag::err_field_incomplete); |
Steve Naroff | 7421664 | 2007-09-14 22:20:54 +0000 | [diff] [blame] | 3432 | FD->setInvalidDecl(); |
| 3433 | EnclosingDecl->setInvalidDecl(); |
Fariborz Jahanian | 3f5faf7 | 2007-10-04 00:45:27 +0000 | [diff] [blame] | 3434 | continue; |
Fariborz Jahanian | e267ab6 | 2007-09-14 16:27:55 +0000 | [diff] [blame] | 3435 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3436 | if (i != NumFields-1 || // ... that the last member ... |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 3437 | !Record->isStruct() || // ... of a structure ... |
Chris Lattner | 02c642e | 2007-07-31 21:33:24 +0000 | [diff] [blame] | 3438 | !FDTy->isArrayType()) { //... may have incomplete array type. |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 3439 | DiagnoseIncompleteType(FD->getLocation(), FD->getType(), |
| 3440 | diag::err_field_incomplete); |
Steve Naroff | 7421664 | 2007-09-14 22:20:54 +0000 | [diff] [blame] | 3441 | FD->setInvalidDecl(); |
| 3442 | EnclosingDecl->setInvalidDecl(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3443 | continue; |
| 3444 | } |
Fariborz Jahanian | e267ab6 | 2007-09-14 16:27:55 +0000 | [diff] [blame] | 3445 | if (NumNamedMembers < 1) { //... must have more than named member ... |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 3446 | Diag(FD->getLocation(), diag::err_flexible_array_empty_struct) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3447 | << FD->getDeclName(); |
Steve Naroff | 7421664 | 2007-09-14 22:20:54 +0000 | [diff] [blame] | 3448 | FD->setInvalidDecl(); |
| 3449 | EnclosingDecl->setInvalidDecl(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3450 | continue; |
| 3451 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3452 | // 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] | 3453 | if (Record) |
| 3454 | Record->setHasFlexibleArrayMember(true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3455 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3456 | /// C99 6.7.2.1p2 - a struct ending in a flexible array member cannot be the |
| 3457 | /// field of another structure or the element of an array. |
Chris Lattner | 02c642e | 2007-07-31 21:33:24 +0000 | [diff] [blame] | 3458 | if (const RecordType *FDTTy = FDTy->getAsRecordType()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3459 | if (FDTTy->getDecl()->hasFlexibleArrayMember()) { |
| 3460 | // 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] | 3461 | if (Record && Record->isUnion()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3462 | Record->setHasFlexibleArrayMember(true); |
| 3463 | } else { |
| 3464 | // If this is a struct/class and this is not the last element, reject |
| 3465 | // it. Note that GCC supports variable sized arrays in the middle of |
| 3466 | // structures. |
| 3467 | if (i != NumFields-1) { |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 3468 | Diag(FD->getLocation(), diag::err_variable_sized_type_in_struct) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3469 | << FD->getDeclName(); |
Steve Naroff | 7421664 | 2007-09-14 22:20:54 +0000 | [diff] [blame] | 3470 | FD->setInvalidDecl(); |
| 3471 | EnclosingDecl->setInvalidDecl(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3472 | continue; |
| 3473 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3474 | // We support flexible arrays at the end of structs in other structs |
| 3475 | // as an extension. |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 3476 | Diag(FD->getLocation(), diag::ext_flexible_array_in_struct) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3477 | << FD->getDeclName(); |
Fariborz Jahanian | 3f5faf7 | 2007-10-04 00:45:27 +0000 | [diff] [blame] | 3478 | if (Record) |
Fariborz Jahanian | e267ab6 | 2007-09-14 16:27:55 +0000 | [diff] [blame] | 3479 | Record->setHasFlexibleArrayMember(true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3480 | } |
| 3481 | } |
| 3482 | } |
Fariborz Jahanian | e7f64cc | 2007-10-12 22:10:42 +0000 | [diff] [blame] | 3483 | /// A field cannot be an Objective-c object |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3484 | if (FDTy->isObjCInterfaceType()) { |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 3485 | Diag(FD->getLocation(), diag::err_statically_allocated_object) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 3486 | << FD->getDeclName(); |
Fariborz Jahanian | e7f64cc | 2007-10-12 22:10:42 +0000 | [diff] [blame] | 3487 | FD->setInvalidDecl(); |
| 3488 | EnclosingDecl->setInvalidDecl(); |
| 3489 | continue; |
| 3490 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3491 | // Keep track of the number of named members. |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 3492 | if (FD->getIdentifier()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3493 | ++NumNamedMembers; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3494 | } |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 3495 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3496 | // Okay, we successfully defined 'Record'. |
Chris Lattner | e1e7985 | 2008-02-06 00:51:33 +0000 | [diff] [blame] | 3497 | if (Record) { |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3498 | Record->completeDefinition(Context); |
Chris Lattner | e1e7985 | 2008-02-06 00:51:33 +0000 | [diff] [blame] | 3499 | } else { |
Chris Lattner | a91d381 | 2008-02-05 22:40:55 +0000 | [diff] [blame] | 3500 | ObjCIvarDecl **ClsFields = reinterpret_cast<ObjCIvarDecl**>(&RecFields[0]); |
Fariborz Jahanian | 60f8c86 | 2008-12-13 20:28:25 +0000 | [diff] [blame] | 3501 | if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(EnclosingDecl)) { |
Chris Lattner | a91d381 | 2008-02-05 22:40:55 +0000 | [diff] [blame] | 3502 | ID->addInstanceVariablesToClass(ClsFields, RecFields.size(), RBrac); |
Fariborz Jahanian | 3281eff | 2008-12-16 01:08:35 +0000 | [diff] [blame] | 3503 | // Must enforce the rule that ivars in the base classes may not be |
| 3504 | // duplicates. |
Fariborz Jahanian | 375d37c | 2008-12-17 22:21:44 +0000 | [diff] [blame] | 3505 | if (ID->getSuperClass()) { |
| 3506 | for (ObjCInterfaceDecl::ivar_iterator IVI = ID->ivar_begin(), |
| 3507 | IVE = ID->ivar_end(); IVI != IVE; ++IVI) { |
| 3508 | ObjCIvarDecl* Ivar = (*IVI); |
| 3509 | IdentifierInfo *II = Ivar->getIdentifier(); |
| 3510 | ObjCIvarDecl* prevIvar = ID->getSuperClass()->FindIvarDeclaration(II); |
| 3511 | if (prevIvar) { |
| 3512 | Diag(Ivar->getLocation(), diag::err_duplicate_member) << II; |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 3513 | Diag(prevIvar->getLocation(), diag::note_previous_declaration); |
Fariborz Jahanian | 3281eff | 2008-12-16 01:08:35 +0000 | [diff] [blame] | 3514 | } |
Fariborz Jahanian | 375d37c | 2008-12-17 22:21:44 +0000 | [diff] [blame] | 3515 | } |
Fariborz Jahanian | 3281eff | 2008-12-16 01:08:35 +0000 | [diff] [blame] | 3516 | } |
Fariborz Jahanian | 60f8c86 | 2008-12-13 20:28:25 +0000 | [diff] [blame] | 3517 | } |
Chris Lattner | a91d381 | 2008-02-05 22:40:55 +0000 | [diff] [blame] | 3518 | else if (ObjCImplementationDecl *IMPDecl = |
| 3519 | dyn_cast<ObjCImplementationDecl>(EnclosingDecl)) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3520 | assert(IMPDecl && "ActOnFields - missing ObjCImplementationDecl"); |
| 3521 | IMPDecl->ObjCAddInstanceVariablesToClassImpl(ClsFields, RecFields.size()); |
Fariborz Jahanian | 3a3ca1b | 2007-10-31 18:48:14 +0000 | [diff] [blame] | 3522 | CheckImplementationIvars(IMPDecl, ClsFields, RecFields.size(), RBrac); |
Fariborz Jahanian | d0b90bf | 2007-09-26 18:27:25 +0000 | [diff] [blame] | 3523 | } |
Fariborz Jahanian | b04a021 | 2007-09-14 21:08:27 +0000 | [diff] [blame] | 3524 | } |
Daniel Dunbar | 7d07664 | 2008-10-03 17:33:35 +0000 | [diff] [blame] | 3525 | |
| 3526 | if (Attr) |
| 3527 | ProcessDeclAttributeList(Record, Attr); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3528 | } |
| 3529 | |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 3530 | Sema::DeclTy *Sema::ActOnEnumConstant(Scope *S, DeclTy *theEnumDecl, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3531 | DeclTy *lastEnumConst, |
| 3532 | SourceLocation IdLoc, IdentifierInfo *Id, |
| 3533 | SourceLocation EqualLoc, ExprTy *val) { |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 3534 | EnumDecl *TheEnumDecl = cast<EnumDecl>(static_cast<Decl*>(theEnumDecl)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3535 | EnumConstantDecl *LastEnumConst = |
| 3536 | cast_or_null<EnumConstantDecl>(static_cast<Decl*>(lastEnumConst)); |
| 3537 | Expr *Val = static_cast<Expr*>(val); |
| 3538 | |
Chris Lattner | 31e0572 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 3539 | // The scope passed in may not be a decl scope. Zip up the scope tree until |
| 3540 | // we find one that is. |
Douglas Gregor | 1a0d31a | 2009-01-12 18:45:55 +0000 | [diff] [blame] | 3541 | S = getNonFieldDeclScope(S); |
Chris Lattner | 31e0572 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 3542 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3543 | // Verify that there isn't already something declared with this name in this |
| 3544 | // scope. |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 3545 | Decl *PrevDecl = LookupDecl(Id, Decl::IDNS_Ordinary, S); |
Douglas Gregor | f57172b | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 3546 | if (PrevDecl && PrevDecl->isTemplateParameter()) { |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 3547 | // Maybe we will complain about the shadowed template parameter. |
| 3548 | DiagnoseTemplateParameterShadow(IdLoc, PrevDecl); |
| 3549 | // Just pretend that we didn't see the previous declaration. |
| 3550 | PrevDecl = 0; |
| 3551 | } |
| 3552 | |
| 3553 | if (PrevDecl) { |
Argyrios Kyrtzidis | 0ff12f0 | 2008-07-16 21:01:53 +0000 | [diff] [blame] | 3554 | // When in C++, we may get a TagDecl with the same name; in this case the |
| 3555 | // enum constant will 'hide' the tag. |
| 3556 | assert((getLangOptions().CPlusPlus || !isa<TagDecl>(PrevDecl)) && |
| 3557 | "Received TagDecl when not in C++!"); |
Argyrios Kyrtzidis | 15a12d0 | 2008-09-09 21:18:04 +0000 | [diff] [blame] | 3558 | if (!isa<TagDecl>(PrevDecl) && isDeclInScope(PrevDecl, CurContext, S)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3559 | if (isa<EnumConstantDecl>(PrevDecl)) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3560 | Diag(IdLoc, diag::err_redefinition_of_enumerator) << Id; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3561 | else |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3562 | Diag(IdLoc, diag::err_redefinition) << Id; |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 3563 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Chris Lattner | a73349d | 2008-02-26 00:33:57 +0000 | [diff] [blame] | 3564 | delete Val; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3565 | return 0; |
| 3566 | } |
| 3567 | } |
| 3568 | |
| 3569 | llvm::APSInt EnumVal(32); |
| 3570 | QualType EltTy; |
| 3571 | if (Val) { |
Chris Lattner | 421a23d | 2007-08-27 21:16:18 +0000 | [diff] [blame] | 3572 | // Make sure to promote the operand type to int. |
| 3573 | UsualUnaryConversions(Val); |
| 3574 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3575 | // C99 6.7.2.2p2: Make sure we have an integer constant expression. |
| 3576 | SourceLocation ExpLoc; |
Anders Carlsson | 49184b2 | 2008-12-05 16:33:57 +0000 | [diff] [blame] | 3577 | if (VerifyIntegerConstantExpression(Val, &EnumVal)) { |
Chris Lattner | a73349d | 2008-02-26 00:33:57 +0000 | [diff] [blame] | 3578 | delete Val; |
Chris Lattner | b7416f9 | 2007-08-27 17:37:24 +0000 | [diff] [blame] | 3579 | Val = 0; // Just forget about it. |
Chris Lattner | e9ca851 | 2007-08-29 16:03:41 +0000 | [diff] [blame] | 3580 | } else { |
| 3581 | EltTy = Val->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3582 | } |
Chris Lattner | b7416f9 | 2007-08-27 17:37:24 +0000 | [diff] [blame] | 3583 | } |
| 3584 | |
| 3585 | if (!Val) { |
| 3586 | if (LastEnumConst) { |
| 3587 | // Assign the last value + 1. |
| 3588 | EnumVal = LastEnumConst->getInitVal(); |
| 3589 | ++EnumVal; |
Chris Lattner | 421a23d | 2007-08-27 21:16:18 +0000 | [diff] [blame] | 3590 | |
| 3591 | // Check for overflow on increment. |
| 3592 | if (EnumVal < LastEnumConst->getInitVal()) |
| 3593 | Diag(IdLoc, diag::warn_enum_value_overflow); |
| 3594 | |
Chris Lattner | b7416f9 | 2007-08-27 17:37:24 +0000 | [diff] [blame] | 3595 | EltTy = LastEnumConst->getType(); |
| 3596 | } else { |
| 3597 | // First value, set to zero. |
| 3598 | EltTy = Context.IntTy; |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 3599 | EnumVal.zextOrTrunc(static_cast<uint32_t>(Context.getTypeSize(EltTy))); |
Chris Lattner | b7416f9 | 2007-08-27 17:37:24 +0000 | [diff] [blame] | 3600 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3601 | } |
| 3602 | |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 3603 | EnumConstantDecl *New = |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 3604 | EnumConstantDecl::Create(Context, TheEnumDecl, IdLoc, Id, EltTy, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 3605 | Val, EnumVal); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3606 | |
| 3607 | // Register this decl in the current scope stack. |
Argyrios Kyrtzidis | 87f3ff0 | 2008-04-12 00:47:19 +0000 | [diff] [blame] | 3608 | PushOnScopeChains(New, S); |
Douglas Gregor | 45579f5 | 2008-12-17 02:04:30 +0000 | [diff] [blame] | 3609 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3610 | return New; |
| 3611 | } |
| 3612 | |
Steve Naroff | 02408c6 | 2008-08-07 14:08:16 +0000 | [diff] [blame] | 3613 | // FIXME: For consistency with ActOnFields(), we should have the parser |
| 3614 | // pass in the source location for the left/right braces. |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 3615 | void Sema::ActOnEnumBody(SourceLocation EnumLoc, DeclTy *EnumDeclX, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3616 | DeclTy **Elements, unsigned NumElements) { |
| 3617 | EnumDecl *Enum = cast<EnumDecl>(static_cast<Decl*>(EnumDeclX)); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 3618 | QualType EnumType = Context.getTypeDeclType(Enum); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3619 | |
Chris Lattner | e37f0be | 2007-08-28 05:10:31 +0000 | [diff] [blame] | 3620 | // TODO: If the result value doesn't fit in an int, it must be a long or long |
| 3621 | // long value. ISO C does not support this, but GCC does as an extension, |
| 3622 | // emit a warning. |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 3623 | unsigned IntWidth = Context.Target.getIntWidth(); |
Chris Lattner | e37f0be | 2007-08-28 05:10:31 +0000 | [diff] [blame] | 3624 | |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 3625 | // Verify that all the values are okay, compute the size of the values, and |
| 3626 | // reverse the list. |
| 3627 | unsigned NumNegativeBits = 0; |
| 3628 | unsigned NumPositiveBits = 0; |
| 3629 | |
| 3630 | // Keep track of whether all elements have type int. |
| 3631 | bool AllElementsInt = true; |
| 3632 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3633 | for (unsigned i = 0; i != NumElements; ++i) { |
| 3634 | EnumConstantDecl *ECD = |
| 3635 | cast_or_null<EnumConstantDecl>(static_cast<Decl*>(Elements[i])); |
| 3636 | if (!ECD) continue; // Already issued a diagnostic. |
Chris Lattner | 211a30e | 2007-08-28 05:27:00 +0000 | [diff] [blame] | 3637 | |
| 3638 | // 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] | 3639 | const llvm::APSInt &InitVal = ECD->getInitVal(); |
Chris Lattner | a73349d | 2008-02-26 00:33:57 +0000 | [diff] [blame] | 3640 | assert(InitVal.getBitWidth() >= IntWidth && |
| 3641 | "Should have promoted value to int"); |
Chris Lattner | 211a30e | 2007-08-28 05:27:00 +0000 | [diff] [blame] | 3642 | if (InitVal.getBitWidth() > IntWidth) { |
| 3643 | llvm::APSInt V(InitVal); |
| 3644 | V.trunc(IntWidth); |
| 3645 | V.extend(InitVal.getBitWidth()); |
| 3646 | if (V != InitVal) |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 3647 | Diag(ECD->getLocation(), diag::ext_enum_value_not_int) |
| 3648 | << InitVal.toString(10); |
Chris Lattner | 211a30e | 2007-08-28 05:27:00 +0000 | [diff] [blame] | 3649 | } |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 3650 | |
| 3651 | // Keep track of the size of positive and negative values. |
Chris Lattner | a73349d | 2008-02-26 00:33:57 +0000 | [diff] [blame] | 3652 | if (InitVal.isUnsigned() || InitVal.isNonNegative()) |
Chris Lattner | 21dd821 | 2008-01-14 21:47:29 +0000 | [diff] [blame] | 3653 | NumPositiveBits = std::max(NumPositiveBits, |
| 3654 | (unsigned)InitVal.getActiveBits()); |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 3655 | else |
Chris Lattner | 21dd821 | 2008-01-14 21:47:29 +0000 | [diff] [blame] | 3656 | NumNegativeBits = std::max(NumNegativeBits, |
| 3657 | (unsigned)InitVal.getMinSignedBits()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3658 | |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 3659 | // Keep track of whether every enum element has type int (very commmon). |
| 3660 | if (AllElementsInt) |
| 3661 | AllElementsInt = ECD->getType() == Context.IntTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3662 | } |
| 3663 | |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 3664 | // Figure out the type that should be used for this enum. |
| 3665 | // FIXME: Support attribute(packed) on enums and -fshort-enums. |
| 3666 | QualType BestType; |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 3667 | unsigned BestWidth; |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 3668 | |
| 3669 | if (NumNegativeBits) { |
| 3670 | // If there is a negative value, figure out the smallest integer type (of |
| 3671 | // int/long/longlong) that fits. |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 3672 | if (NumNegativeBits <= IntWidth && NumPositiveBits < IntWidth) { |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 3673 | BestType = Context.IntTy; |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 3674 | BestWidth = IntWidth; |
| 3675 | } else { |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 3676 | BestWidth = Context.Target.getLongWidth(); |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 3677 | |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 3678 | if (NumNegativeBits <= BestWidth && NumPositiveBits < BestWidth) |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 3679 | BestType = Context.LongTy; |
| 3680 | else { |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 3681 | BestWidth = Context.Target.getLongLongWidth(); |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 3682 | |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 3683 | if (NumNegativeBits > BestWidth || NumPositiveBits >= BestWidth) |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 3684 | Diag(Enum->getLocation(), diag::warn_enum_too_large); |
| 3685 | BestType = Context.LongLongTy; |
| 3686 | } |
| 3687 | } |
| 3688 | } else { |
| 3689 | // If there is no negative value, figure out which of uint, ulong, ulonglong |
| 3690 | // fits. |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 3691 | if (NumPositiveBits <= IntWidth) { |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 3692 | BestType = Context.UnsignedIntTy; |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 3693 | BestWidth = IntWidth; |
| 3694 | } else if (NumPositiveBits <= |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 3695 | (BestWidth = Context.Target.getLongWidth())) { |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 3696 | BestType = Context.UnsignedLongTy; |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 3697 | } else { |
| 3698 | BestWidth = Context.Target.getLongLongWidth(); |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 3699 | assert(NumPositiveBits <= BestWidth && |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 3700 | "How could an initializer get larger than ULL?"); |
| 3701 | BestType = Context.UnsignedLongLongTy; |
| 3702 | } |
| 3703 | } |
| 3704 | |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 3705 | // Loop over all of the enumerator constants, changing their types to match |
| 3706 | // the type of the enum if needed. |
| 3707 | for (unsigned i = 0; i != NumElements; ++i) { |
| 3708 | EnumConstantDecl *ECD = |
| 3709 | cast_or_null<EnumConstantDecl>(static_cast<Decl*>(Elements[i])); |
| 3710 | if (!ECD) continue; // Already issued a diagnostic. |
| 3711 | |
| 3712 | // Standard C says the enumerators have int type, but we allow, as an |
| 3713 | // extension, the enumerators to be larger than int size. If each |
| 3714 | // enumerator value fits in an int, type it as an int, otherwise type it the |
| 3715 | // same as the enumerator decl itself. This means that in "enum { X = 1U }" |
| 3716 | // that X has type 'int', not 'unsigned'. |
Chris Lattner | a73349d | 2008-02-26 00:33:57 +0000 | [diff] [blame] | 3717 | if (ECD->getType() == Context.IntTy) { |
| 3718 | // Make sure the init value is signed. |
| 3719 | llvm::APSInt IV = ECD->getInitVal(); |
| 3720 | IV.setIsSigned(true); |
| 3721 | ECD->setInitVal(IV); |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 3722 | |
| 3723 | if (getLangOptions().CPlusPlus) |
| 3724 | // C++ [dcl.enum]p4: Following the closing brace of an |
| 3725 | // enum-specifier, each enumerator has the type of its |
| 3726 | // enumeration. |
| 3727 | ECD->setType(EnumType); |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 3728 | continue; // Already int type. |
Chris Lattner | a73349d | 2008-02-26 00:33:57 +0000 | [diff] [blame] | 3729 | } |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 3730 | |
| 3731 | // Determine whether the value fits into an int. |
| 3732 | llvm::APSInt InitVal = ECD->getInitVal(); |
| 3733 | bool FitsInInt; |
| 3734 | if (InitVal.isUnsigned() || !InitVal.isNegative()) |
| 3735 | FitsInInt = InitVal.getActiveBits() < IntWidth; |
| 3736 | else |
| 3737 | FitsInInt = InitVal.getMinSignedBits() <= IntWidth; |
| 3738 | |
| 3739 | // If it fits into an integer type, force it. Otherwise force it to match |
| 3740 | // the enum decl type. |
| 3741 | QualType NewTy; |
| 3742 | unsigned NewWidth; |
| 3743 | bool NewSign; |
| 3744 | if (FitsInInt) { |
| 3745 | NewTy = Context.IntTy; |
| 3746 | NewWidth = IntWidth; |
| 3747 | NewSign = true; |
| 3748 | } else if (ECD->getType() == BestType) { |
| 3749 | // Already the right type! |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 3750 | if (getLangOptions().CPlusPlus) |
| 3751 | // C++ [dcl.enum]p4: Following the closing brace of an |
| 3752 | // enum-specifier, each enumerator has the type of its |
| 3753 | // enumeration. |
| 3754 | ECD->setType(EnumType); |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 3755 | continue; |
| 3756 | } else { |
| 3757 | NewTy = BestType; |
| 3758 | NewWidth = BestWidth; |
| 3759 | NewSign = BestType->isSignedIntegerType(); |
| 3760 | } |
| 3761 | |
| 3762 | // Adjust the APSInt value. |
| 3763 | InitVal.extOrTrunc(NewWidth); |
| 3764 | InitVal.setIsSigned(NewSign); |
| 3765 | ECD->setInitVal(InitVal); |
| 3766 | |
| 3767 | // Adjust the Expr initializer and type. |
Chris Lattner | 13fd416 | 2009-01-15 19:19:42 +0000 | [diff] [blame] | 3768 | if (ECD->getInitExpr()) |
| 3769 | ECD->setInitExpr(new ImplicitCastExpr(NewTy, ECD->getInitExpr(), |
| 3770 | /*isLvalue=*/false)); |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 3771 | if (getLangOptions().CPlusPlus) |
| 3772 | // C++ [dcl.enum]p4: Following the closing brace of an |
| 3773 | // enum-specifier, each enumerator has the type of its |
| 3774 | // enumeration. |
| 3775 | ECD->setType(EnumType); |
| 3776 | else |
| 3777 | ECD->setType(NewTy); |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 3778 | } |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 3779 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3780 | Enum->completeDefinition(Context, BestType); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3781 | } |
| 3782 | |
Anders Carlsson | dfab6cb | 2008-02-08 00:33:21 +0000 | [diff] [blame] | 3783 | Sema::DeclTy *Sema::ActOnFileScopeAsmDecl(SourceLocation Loc, |
Sebastian Redl | 798d119 | 2008-12-13 16:23:55 +0000 | [diff] [blame] | 3784 | ExprArg expr) { |
| 3785 | StringLiteral *AsmString = cast<StringLiteral>((Expr*)expr.release()); |
| 3786 | |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 3787 | return FileScopeAsmDecl::Create(Context, CurContext, Loc, AsmString); |
Anders Carlsson | dfab6cb | 2008-02-08 00:33:21 +0000 | [diff] [blame] | 3788 | } |
| 3789 | |
Douglas Gregor | f44515a | 2008-12-16 22:23:02 +0000 | [diff] [blame] | 3790 | |
Daniel Dunbar | 4cde927 | 2008-10-14 05:35:18 +0000 | [diff] [blame] | 3791 | void Sema::ActOnPragmaPack(PragmaPackKind Kind, IdentifierInfo *Name, |
| 3792 | ExprTy *alignment, SourceLocation PragmaLoc, |
| 3793 | SourceLocation LParenLoc, SourceLocation RParenLoc) { |
| 3794 | Expr *Alignment = static_cast<Expr *>(alignment); |
| 3795 | |
| 3796 | // If specified then alignment must be a "small" power of two. |
| 3797 | unsigned AlignmentVal = 0; |
| 3798 | if (Alignment) { |
| 3799 | llvm::APSInt Val; |
| 3800 | if (!Alignment->isIntegerConstantExpr(Val, Context) || |
| 3801 | !Val.isPowerOf2() || |
| 3802 | Val.getZExtValue() > 16) { |
| 3803 | Diag(PragmaLoc, diag::warn_pragma_pack_invalid_alignment); |
| 3804 | delete Alignment; |
| 3805 | return; // Ignore |
| 3806 | } |
| 3807 | |
| 3808 | AlignmentVal = (unsigned) Val.getZExtValue(); |
| 3809 | } |
| 3810 | |
| 3811 | switch (Kind) { |
| 3812 | case Action::PPK_Default: // pack([n]) |
| 3813 | PackContext.setAlignment(AlignmentVal); |
| 3814 | break; |
| 3815 | |
| 3816 | case Action::PPK_Show: // pack(show) |
| 3817 | // Show the current alignment, making sure to show the right value |
| 3818 | // for the default. |
| 3819 | AlignmentVal = PackContext.getAlignment(); |
| 3820 | // FIXME: This should come from the target. |
| 3821 | if (AlignmentVal == 0) |
| 3822 | AlignmentVal = 8; |
Chris Lattner | 8365223 | 2008-11-19 07:25:44 +0000 | [diff] [blame] | 3823 | Diag(PragmaLoc, diag::warn_pragma_pack_show) << AlignmentVal; |
Daniel Dunbar | 4cde927 | 2008-10-14 05:35:18 +0000 | [diff] [blame] | 3824 | break; |
| 3825 | |
| 3826 | case Action::PPK_Push: // pack(push [, id] [, [n]) |
| 3827 | PackContext.push(Name); |
| 3828 | // Set the new alignment if specified. |
| 3829 | if (Alignment) |
| 3830 | PackContext.setAlignment(AlignmentVal); |
| 3831 | break; |
| 3832 | |
| 3833 | case Action::PPK_Pop: // pack(pop [, id] [, n]) |
| 3834 | // MSDN, C/C++ Preprocessor Reference > Pragma Directives > pack: |
| 3835 | // "#pragma pack(pop, identifier, n) is undefined" |
| 3836 | if (Alignment && Name) |
| 3837 | Diag(PragmaLoc, diag::warn_pragma_pack_pop_identifer_and_alignment); |
| 3838 | |
| 3839 | // Do the pop. |
| 3840 | if (!PackContext.pop(Name)) { |
| 3841 | // If a name was specified then failure indicates the name |
| 3842 | // wasn't found. Otherwise failure indicates the stack was |
| 3843 | // empty. |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 3844 | Diag(PragmaLoc, diag::warn_pragma_pack_pop_failed) |
| 3845 | << (Name ? "no record matching name" : "stack empty"); |
Daniel Dunbar | 4cde927 | 2008-10-14 05:35:18 +0000 | [diff] [blame] | 3846 | |
| 3847 | // FIXME: Warn about popping named records as MSVC does. |
| 3848 | } else { |
| 3849 | // Pop succeeded, set the new alignment if specified. |
| 3850 | if (Alignment) |
| 3851 | PackContext.setAlignment(AlignmentVal); |
| 3852 | } |
| 3853 | break; |
| 3854 | |
| 3855 | default: |
| 3856 | assert(0 && "Invalid #pragma pack kind."); |
| 3857 | } |
| 3858 | } |
| 3859 | |
| 3860 | bool PragmaPackStack::pop(IdentifierInfo *Name) { |
| 3861 | if (Stack.empty()) |
| 3862 | return false; |
| 3863 | |
| 3864 | // If name is empty just pop top. |
| 3865 | if (!Name) { |
| 3866 | Alignment = Stack.back().first; |
| 3867 | Stack.pop_back(); |
| 3868 | return true; |
| 3869 | } |
| 3870 | |
| 3871 | // Otherwise, find the named record. |
| 3872 | for (unsigned i = Stack.size(); i != 0; ) { |
| 3873 | --i; |
Daniel Dunbar | 0655039 | 2008-11-19 10:32:38 +0000 | [diff] [blame] | 3874 | if (Stack[i].second == Name) { |
Daniel Dunbar | 4cde927 | 2008-10-14 05:35:18 +0000 | [diff] [blame] | 3875 | // Found it, pop up to and including this record. |
| 3876 | Alignment = Stack[i].first; |
| 3877 | Stack.erase(Stack.begin() + i, Stack.end()); |
| 3878 | return true; |
| 3879 | } |
| 3880 | } |
| 3881 | |
| 3882 | return false; |
| 3883 | } |