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