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