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