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