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