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