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