| Chris Lattner | 697e5d6 | 2006-11-09 06:32:27 +0000 | [diff] [blame] | 1 | //===--- SemaDecl.cpp - Semantic Analysis for Declarations ----------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| Chris Lattner | 5b12ab8 | 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. |
| Chris Lattner | 697e5d6 | 2006-11-09 06:32:27 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements semantic analysis for declarations. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| John McCall | 8302463 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 14 | #include "clang/Sema/SemaInternal.h" |
| Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 15 | #include "TypeLocBuilder.h" |
| Chris Lattner | 622c193 | 2008-02-06 00:51:33 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTConsumer.h" |
| Chris Lattner | 5c5fbcc | 2006-12-03 08:41:30 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTContext.h" |
| Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTLambda.h" |
| Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 19 | #include "clang/AST/CXXInheritance.h" |
| Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 20 | #include "clang/AST/CharUnits.h" |
| Dmitri Gribenko | f26054f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 21 | #include "clang/AST/CommentDiagnostic.h" |
| John McCall | 28a0cf7 | 2010-08-25 07:42:41 +0000 | [diff] [blame] | 22 | #include "clang/AST/DeclCXX.h" |
| John McCall | de6836a | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 23 | #include "clang/AST/DeclObjC.h" |
| Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 24 | #include "clang/AST/DeclTemplate.h" |
| Chandler Carruth | 33bf3e7 | 2011-03-27 09:46:56 +0000 | [diff] [blame] | 25 | #include "clang/AST/EvaluatedExprVisitor.h" |
| Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 26 | #include "clang/AST/ExprCXX.h" |
| Sebastian Redl | a7b98a7 | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 27 | #include "clang/AST/StmtCXX.h" |
| Anders Carlsson | d624e16 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 28 | #include "clang/Basic/PartialDiagnostic.h" |
| Steve Naroff | e101f95 | 2008-01-30 23:46:05 +0000 | [diff] [blame] | 29 | #include "clang/Basic/SourceManager.h" |
| Anders Carlsson | d624e16 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 30 | #include "clang/Basic/TargetInfo.h" |
| Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 31 | #include "clang/Lex/HeaderSearch.h" // FIXME: Sema shouldn't depend on Lex |
| 32 | #include "clang/Lex/ModuleLoader.h" // FIXME: Sema shouldn't depend on Lex |
| 33 | #include "clang/Lex/Preprocessor.h" // FIXME: Sema shouldn't depend on Lex |
| 34 | #include "clang/Parse/ParseDiagnostic.h" |
| 35 | #include "clang/Sema/CXXFieldCollector.h" |
| 36 | #include "clang/Sema/DeclSpec.h" |
| 37 | #include "clang/Sema/DelayedDiagnostic.h" |
| 38 | #include "clang/Sema/Initialization.h" |
| 39 | #include "clang/Sema/Lookup.h" |
| 40 | #include "clang/Sema/ParsedTemplate.h" |
| 41 | #include "clang/Sema/Scope.h" |
| 42 | #include "clang/Sema/ScopeInfo.h" |
| Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 43 | #include "clang/Sema/Template.h" |
| Benjamin Kramer | 4903802 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 44 | #include "llvm/ADT/SmallString.h" |
| John McCall | 0e21fcc | 2009-12-24 09:58:38 +0000 | [diff] [blame] | 45 | #include "llvm/ADT/Triple.h" |
| Douglas Gregor | 8b9ccca | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 46 | #include <algorithm> |
| Douglas Gregor | 56fbc37 | 2009-09-28 21:14:19 +0000 | [diff] [blame] | 47 | #include <cstring> |
| Douglas Gregor | 8b9ccca | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 48 | #include <functional> |
| Chris Lattner | 697e5d6 | 2006-11-09 06:32:27 +0000 | [diff] [blame] | 49 | using namespace clang; |
| John McCall | aab3e41 | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 50 | using namespace sema; |
| Chris Lattner | 697e5d6 | 2006-11-09 06:32:27 +0000 | [diff] [blame] | 51 | |
| Richard Smith | cd1c055 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 52 | Sema::DeclGroupPtrTy Sema::ConvertDeclToDeclGroup(Decl *Ptr, Decl *OwnedType) { |
| 53 | if (OwnedType) { |
| 54 | Decl *Group[2] = { OwnedType, Ptr }; |
| 55 | return DeclGroupPtrTy::make(DeclGroupRef::Create(Context, Group, 2)); |
| 56 | } |
| 57 | |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 58 | return DeclGroupPtrTy::make(DeclGroupRef(Ptr)); |
| Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| Kaelyn Uhrain | b137840 | 2012-01-18 21:41:41 +0000 | [diff] [blame] | 61 | namespace { |
| 62 | |
| 63 | class TypeNameValidatorCCC : public CorrectionCandidateCallback { |
| 64 | public: |
| Kaelyn Uhrain | 67b44c9 | 2014-02-13 20:14:07 +0000 | [diff] [blame] | 65 | TypeNameValidatorCCC(bool AllowInvalid, bool WantClass=false, |
| 66 | bool AllowTemplates=false) |
| 67 | : AllowInvalidDecl(AllowInvalid), WantClassName(WantClass), |
| 68 | AllowClassTemplates(AllowTemplates) { |
| Kaelyn Uhrain | b137840 | 2012-01-18 21:41:41 +0000 | [diff] [blame] | 69 | WantExpressionKeywords = false; |
| 70 | WantCXXNamedCasts = false; |
| 71 | WantRemainingKeywords = false; |
| 72 | } |
| 73 | |
| 74 | virtual bool ValidateCandidate(const TypoCorrection &candidate) { |
| Kaelyn Uhrain | 67b44c9 | 2014-02-13 20:14:07 +0000 | [diff] [blame] | 75 | if (NamedDecl *ND = candidate.getCorrectionDecl()) { |
| 76 | bool IsType = isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND); |
| 77 | bool AllowedTemplate = AllowClassTemplates && isa<ClassTemplateDecl>(ND); |
| 78 | return (IsType || AllowedTemplate) && |
| 79 | (AllowInvalidDecl || !ND->isInvalidDecl()); |
| 80 | } |
| 81 | return !WantClassName && candidate.isKeyword(); |
| Kaelyn Uhrain | b137840 | 2012-01-18 21:41:41 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | private: |
| 85 | bool AllowInvalidDecl; |
| Kaelyn Uhrain | 9cb8e9f | 2012-06-22 23:37:05 +0000 | [diff] [blame] | 86 | bool WantClassName; |
| Kaelyn Uhrain | 67b44c9 | 2014-02-13 20:14:07 +0000 | [diff] [blame] | 87 | bool AllowClassTemplates; |
| Kaelyn Uhrain | b137840 | 2012-01-18 21:41:41 +0000 | [diff] [blame] | 88 | }; |
| 89 | |
| 90 | } |
| 91 | |
| Kaelyn Uhrain | 237c7d3 | 2012-06-15 23:45:51 +0000 | [diff] [blame] | 92 | /// \brief Determine whether the token kind starts a simple-type-specifier. |
| 93 | bool Sema::isSimpleTypeSpecifier(tok::TokenKind Kind) const { |
| 94 | switch (Kind) { |
| 95 | // FIXME: Take into account the current language when deciding whether a |
| 96 | // token kind is a valid type specifier |
| 97 | case tok::kw_short: |
| 98 | case tok::kw_long: |
| 99 | case tok::kw___int64: |
| 100 | case tok::kw___int128: |
| 101 | case tok::kw_signed: |
| 102 | case tok::kw_unsigned: |
| 103 | case tok::kw_void: |
| 104 | case tok::kw_char: |
| 105 | case tok::kw_int: |
| 106 | case tok::kw_half: |
| 107 | case tok::kw_float: |
| 108 | case tok::kw_double: |
| 109 | case tok::kw_wchar_t: |
| 110 | case tok::kw_bool: |
| 111 | case tok::kw___underlying_type: |
| 112 | return true; |
| 113 | |
| 114 | case tok::annot_typename: |
| 115 | case tok::kw_char16_t: |
| 116 | case tok::kw_char32_t: |
| 117 | case tok::kw_typeof: |
| David Majnemer | a5e9255 | 2013-09-22 01:24:26 +0000 | [diff] [blame] | 118 | case tok::annot_decltype: |
| Kaelyn Uhrain | 237c7d3 | 2012-06-15 23:45:51 +0000 | [diff] [blame] | 119 | case tok::kw_decltype: |
| 120 | return getLangOpts().CPlusPlus; |
| 121 | |
| 122 | default: |
| 123 | break; |
| 124 | } |
| 125 | |
| 126 | return false; |
| 127 | } |
| 128 | |
| Douglas Gregor | ec6e189 | 2009-02-04 19:16:12 +0000 | [diff] [blame] | 129 | /// \brief If the identifier refers to a type name within this scope, |
| 130 | /// return the declaration of that type. |
| 131 | /// |
| 132 | /// This routine performs ordinary name lookup of the identifier II |
| 133 | /// within the given scope, with optional C++ scope specifier SS, to |
| Douglas Gregor | 9817f4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 134 | /// determine whether the name refers to a type. If so, returns an |
| 135 | /// opaque pointer (actually a QualType) corresponding to that |
| 136 | /// type. Otherwise, returns NULL. |
| Dmitri Gribenko | 5267fdf | 2013-05-03 13:12:11 +0000 | [diff] [blame] | 137 | ParsedType Sema::getTypeName(const IdentifierInfo &II, SourceLocation NameLoc, |
| John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 138 | Scope *S, CXXScopeSpec *SS, |
| Fariborz Jahanian | 8796742 | 2011-02-08 18:05:59 +0000 | [diff] [blame] | 139 | bool isClassName, bool HasTrailingDot, |
| Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 140 | ParsedType ObjectTypePtr, |
| Abramo Bagnara | 4244b43 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 141 | bool IsCtorOrDtorName, |
| Kaelyn Uhrain | 85308c6 | 2011-10-11 01:02:41 +0000 | [diff] [blame] | 142 | bool WantNontrivialTypeSourceInfo, |
| 143 | IdentifierInfo **CorrectedII) { |
| Douglas Gregor | a25d65d | 2009-11-20 22:03:38 +0000 | [diff] [blame] | 144 | // Determine where we will perform name lookup. |
| 145 | DeclContext *LookupCtx = 0; |
| 146 | if (ObjectTypePtr) { |
| John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 147 | QualType ObjectType = ObjectTypePtr.get(); |
| Douglas Gregor | a25d65d | 2009-11-20 22:03:38 +0000 | [diff] [blame] | 148 | if (ObjectType->isRecordType()) |
| 149 | LookupCtx = computeDeclContext(ObjectType); |
| Jeffrey Yasskin | 4e150f8 | 2010-04-07 23:29:58 +0000 | [diff] [blame] | 150 | } else if (SS && SS->isNotEmpty()) { |
| Douglas Gregor | a25d65d | 2009-11-20 22:03:38 +0000 | [diff] [blame] | 151 | LookupCtx = computeDeclContext(*SS, false); |
| 152 | |
| 153 | if (!LookupCtx) { |
| 154 | if (isDependentScopeSpecifier(*SS)) { |
| 155 | // C++ [temp.res]p3: |
| 156 | // A qualified-id that refers to a type and in which the |
| 157 | // nested-name-specifier depends on a template-parameter (14.6.2) |
| 158 | // shall be prefixed by the keyword typename to indicate that the |
| 159 | // qualified-id denotes a type, forming an |
| 160 | // elaborated-type-specifier (7.1.5.3). |
| 161 | // |
| 162 | // We therefore do not perform any name lookup if the result would |
| 163 | // refer to a member of an unknown specialization. |
| Richard Smith | 23d5587 | 2012-04-02 01:30:27 +0000 | [diff] [blame] | 164 | if (!isClassName && !IsCtorOrDtorName) |
| John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 165 | return ParsedType(); |
| Douglas Gregor | a25d65d | 2009-11-20 22:03:38 +0000 | [diff] [blame] | 166 | |
| John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 167 | // We know from the grammar that this name refers to a type, |
| 168 | // so build a dependent node to describe the type. |
| Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 169 | if (WantNontrivialTypeSourceInfo) |
| 170 | return ActOnTypenameType(S, SourceLocation(), *SS, II, NameLoc).get(); |
| 171 | |
| 172 | NestedNameSpecifierLoc QualifierLoc = SS->getWithLocInContext(Context); |
| John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 173 | QualType T = |
| Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 174 | CheckTypenameType(ETK_None, SourceLocation(), QualifierLoc, |
| Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 175 | II, NameLoc); |
| Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 176 | |
| 177 | return ParsedType::make(T); |
| Douglas Gregor | a25d65d | 2009-11-20 22:03:38 +0000 | [diff] [blame] | 178 | } |
| 179 | |
| John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 180 | return ParsedType(); |
| Douglas Gregor | a25d65d | 2009-11-20 22:03:38 +0000 | [diff] [blame] | 181 | } |
| 182 | |
| John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 183 | if (!LookupCtx->isDependentContext() && |
| 184 | RequireCompleteDeclContext(*SS, LookupCtx)) |
| John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 185 | return ParsedType(); |
| Douglas Gregor | 5e0962f | 2009-08-26 18:27:52 +0000 | [diff] [blame] | 186 | } |
| Eli Friedman | 9025ec2 | 2009-12-21 01:42:38 +0000 | [diff] [blame] | 187 | |
| 188 | // FIXME: LookupNestedNameSpecifierName isn't the right kind of |
| 189 | // lookup for class-names. |
| 190 | LookupNameKind Kind = isClassName ? LookupNestedNameSpecifierName : |
| 191 | LookupOrdinaryName; |
| 192 | LookupResult Result(*this, &II, NameLoc, Kind); |
| Douglas Gregor | a25d65d | 2009-11-20 22:03:38 +0000 | [diff] [blame] | 193 | if (LookupCtx) { |
| 194 | // Perform "qualified" name lookup into the declaration context we |
| 195 | // computed, which is either the type of the base of a member access |
| 196 | // expression or the declaration context associated with a prior |
| 197 | // nested-name-specifier. |
| 198 | LookupQualifiedName(Result, LookupCtx); |
| Douglas Gregor | c9f9b86 | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 199 | |
| Douglas Gregor | a25d65d | 2009-11-20 22:03:38 +0000 | [diff] [blame] | 200 | if (ObjectTypePtr && Result.empty()) { |
| 201 | // C++ [basic.lookup.classref]p3: |
| 202 | // If the unqualified-id is ~type-name, the type-name is looked up |
| 203 | // in the context of the entire postfix-expression. If the type T of |
| 204 | // the object expression is of a class type C, the type-name is also |
| 205 | // looked up in the scope of class C. At least one of the lookups shall |
| 206 | // find a name that refers to (possibly cv-qualified) T. |
| 207 | LookupName(Result, S); |
| 208 | } |
| 209 | } else { |
| 210 | // Perform unqualified name lookup. |
| 211 | LookupName(Result, S); |
| 212 | } |
| 213 | |
| Chris Lattner | a377833 | 2009-02-16 22:07:16 +0000 | [diff] [blame] | 214 | NamedDecl *IIDecl = 0; |
| John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 215 | switch (Result.getResultKind()) { |
| Chris Lattner | a377833 | 2009-02-16 22:07:16 +0000 | [diff] [blame] | 216 | case LookupResult::NotFound: |
| Douglas Gregor | d0d2ee0 | 2010-01-15 01:44:47 +0000 | [diff] [blame] | 217 | case LookupResult::NotFoundInCurrentInstantiation: |
| Kaelyn Uhrain | 85308c6 | 2011-10-11 01:02:41 +0000 | [diff] [blame] | 218 | if (CorrectedII) { |
| Kaelyn Uhrain | 9cb8e9f | 2012-06-22 23:37:05 +0000 | [diff] [blame] | 219 | TypeNameValidatorCCC Validator(true, isClassName); |
| Kaelyn Uhrain | 85308c6 | 2011-10-11 01:02:41 +0000 | [diff] [blame] | 220 | TypoCorrection Correction = CorrectTypo(Result.getLookupNameInfo(), |
| Kaelyn Uhrain | 4e8942c | 2012-01-31 23:49:25 +0000 | [diff] [blame] | 221 | Kind, S, SS, Validator); |
| Kaelyn Uhrain | 85308c6 | 2011-10-11 01:02:41 +0000 | [diff] [blame] | 222 | IdentifierInfo *NewII = Correction.getCorrectionAsIdentifierInfo(); |
| 223 | TemplateTy Template; |
| 224 | bool MemberOfUnknownSpecialization; |
| 225 | UnqualifiedId TemplateName; |
| 226 | TemplateName.setIdentifier(NewII, NameLoc); |
| 227 | NestedNameSpecifier *NNS = Correction.getCorrectionSpecifier(); |
| 228 | CXXScopeSpec NewSS, *NewSSPtr = SS; |
| 229 | if (SS && NNS) { |
| 230 | NewSS.MakeTrivial(Context, NNS, SourceRange(NameLoc)); |
| 231 | NewSSPtr = &NewSS; |
| 232 | } |
| 233 | if (Correction && (NNS || NewII != &II) && |
| 234 | // Ignore a correction to a template type as the to-be-corrected |
| 235 | // identifier is not a template (typo correction for template names |
| 236 | // is handled elsewhere). |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 237 | !(getLangOpts().CPlusPlus && NewSSPtr && |
| Kaelyn Uhrain | 85308c6 | 2011-10-11 01:02:41 +0000 | [diff] [blame] | 238 | isTemplateName(S, *NewSSPtr, false, TemplateName, ParsedType(), |
| 239 | false, Template, MemberOfUnknownSpecialization))) { |
| 240 | ParsedType Ty = getTypeName(*NewII, NameLoc, S, NewSSPtr, |
| 241 | isClassName, HasTrailingDot, ObjectTypePtr, |
| Abramo Bagnara | 4244b43 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 242 | IsCtorOrDtorName, |
| Kaelyn Uhrain | 85308c6 | 2011-10-11 01:02:41 +0000 | [diff] [blame] | 243 | WantNontrivialTypeSourceInfo); |
| 244 | if (Ty) { |
| Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 245 | diagnoseTypo(Correction, |
| 246 | PDiag(diag::err_unknown_type_or_class_name_suggest) |
| 247 | << Result.getLookupName() << isClassName); |
| Kaelyn Uhrain | 85308c6 | 2011-10-11 01:02:41 +0000 | [diff] [blame] | 248 | if (SS && NNS) |
| 249 | SS->MakeTrivial(Context, NNS, SourceRange(NameLoc)); |
| 250 | *CorrectedII = NewII; |
| 251 | return Ty; |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 | // If typo correction failed or was not performed, fall through |
| Chris Lattner | a377833 | 2009-02-16 22:07:16 +0000 | [diff] [blame] | 256 | case LookupResult::FoundOverloaded: |
| John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 257 | case LookupResult::FoundUnresolvedValue: |
| John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 258 | Result.suppressDiagnostics(); |
| John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 259 | return ParsedType(); |
| Douglas Gregor | 8a6be5e | 2009-02-04 17:00:24 +0000 | [diff] [blame] | 260 | |
| Chris Lattner | e40853a | 2009-10-25 22:09:09 +0000 | [diff] [blame] | 261 | case LookupResult::Ambiguous: |
| John McCall | 6538c93 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 262 | // Recover from type-hiding ambiguities by hiding the type. We'll |
| 263 | // do the lookup again when looking for an object, and we can |
| 264 | // diagnose the error then. If we don't do this, then the error |
| 265 | // about hiding the type will be immediately followed by an error |
| 266 | // that only makes sense if the identifier was treated like a type. |
| John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 267 | if (Result.getAmbiguityKind() == LookupResult::AmbiguousTagHiding) { |
| 268 | Result.suppressDiagnostics(); |
| John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 269 | return ParsedType(); |
| John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 270 | } |
| John McCall | 6538c93 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 271 | |
| Douglas Gregor | fe3d7d0 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 272 | // Look to see if we have a type anywhere in the list of results. |
| 273 | for (LookupResult::iterator Res = Result.begin(), ResEnd = Result.end(); |
| 274 | Res != ResEnd; ++Res) { |
| 275 | if (isa<TypeDecl>(*Res) || isa<ObjCInterfaceDecl>(*Res)) { |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 276 | if (!IIDecl || |
| 277 | (*Res)->getLocation().getRawEncoding() < |
| Douglas Gregor | 712a351 | 2009-04-13 15:14:38 +0000 | [diff] [blame] | 278 | IIDecl->getLocation().getRawEncoding()) |
| 279 | IIDecl = *Res; |
| Douglas Gregor | fe3d7d0 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 280 | } |
| 281 | } |
| 282 | |
| 283 | if (!IIDecl) { |
| 284 | // None of the entities we found is a type, so there is no way |
| 285 | // to even assume that the result is a type. In this case, don't |
| 286 | // complain about the ambiguity. The parser will either try to |
| 287 | // perform this lookup again (e.g., as an object name), which |
| 288 | // will produce the ambiguity, or will complain that it expected |
| 289 | // a type name. |
| John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 290 | Result.suppressDiagnostics(); |
| John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 291 | return ParsedType(); |
| Douglas Gregor | fe3d7d0 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | // We found a type within the ambiguous lookup; diagnose the |
| 295 | // ambiguity and then return that type. This might be the right |
| 296 | // answer, or it might not be, but it suppresses any attempt to |
| 297 | // perform the name lookup again. |
| Douglas Gregor | fe3d7d0 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 298 | break; |
| Douglas Gregor | 8a6be5e | 2009-02-04 17:00:24 +0000 | [diff] [blame] | 299 | |
| Chris Lattner | a377833 | 2009-02-16 22:07:16 +0000 | [diff] [blame] | 300 | case LookupResult::Found: |
| John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 301 | IIDecl = Result.getFoundDecl(); |
| Chris Lattner | a377833 | 2009-02-16 22:07:16 +0000 | [diff] [blame] | 302 | break; |
| Douglas Gregor | 960b5bc | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 303 | } |
| 304 | |
| Chris Lattner | 17e15f1 | 2009-10-25 17:16:46 +0000 | [diff] [blame] | 305 | assert(IIDecl && "Didn't find decl"); |
| John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 306 | |
| Chris Lattner | 17e15f1 | 2009-10-25 17:16:46 +0000 | [diff] [blame] | 307 | QualType T; |
| 308 | if (TypeDecl *TD = dyn_cast<TypeDecl>(IIDecl)) { |
| John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 309 | DiagnoseUseOfDecl(IIDecl, NameLoc); |
| John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 310 | |
| Chris Lattner | 17e15f1 | 2009-10-25 17:16:46 +0000 | [diff] [blame] | 311 | if (T.isNull()) |
| 312 | T = Context.getTypeDeclType(TD); |
| Abramo Bagnara | 4244b43 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 313 | |
| 314 | // NOTE: avoid constructing an ElaboratedType(Loc) if this is a |
| 315 | // constructor or destructor name (in such a case, the scope specifier |
| 316 | // will be attached to the enclosing Expr or Decl node). |
| 317 | if (SS && SS->isNotEmpty() && !IsCtorOrDtorName) { |
| Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 318 | if (WantNontrivialTypeSourceInfo) { |
| 319 | // Construct a type with type-source information. |
| 320 | TypeLocBuilder Builder; |
| 321 | Builder.pushTypeSpec(T).setNameLoc(NameLoc); |
| 322 | |
| 323 | T = getElaboratedType(ETK_None, *SS, T); |
| 324 | ElaboratedTypeLoc ElabTL = Builder.push<ElaboratedTypeLoc>(T); |
| Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 325 | ElabTL.setElaboratedKeywordLoc(SourceLocation()); |
| Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 326 | ElabTL.setQualifierLoc(SS->getWithLocInContext(Context)); |
| 327 | return CreateParsedType(T, Builder.getTypeSourceInfo(Context, T)); |
| 328 | } else { |
| 329 | T = getElaboratedType(ETK_None, *SS, T); |
| 330 | } |
| 331 | } |
| Chris Lattner | 17e15f1 | 2009-10-25 17:16:46 +0000 | [diff] [blame] | 332 | } else if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(IIDecl)) { |
| Fariborz Jahanian | 08891f5 | 2011-03-08 19:12:46 +0000 | [diff] [blame] | 333 | (void)DiagnoseUseOfDecl(IDecl, NameLoc); |
| Fariborz Jahanian | 8796742 | 2011-02-08 18:05:59 +0000 | [diff] [blame] | 334 | if (!HasTrailingDot) |
| 335 | T = Context.getObjCInterfaceType(IDecl); |
| 336 | } |
| 337 | |
| 338 | if (T.isNull()) { |
| John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 339 | // If it's not plausibly a type, suppress diagnostics. |
| 340 | Result.suppressDiagnostics(); |
| John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 341 | return ParsedType(); |
| John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 342 | } |
| John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 343 | return ParsedType::make(T); |
| Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 344 | } |
| 345 | |
| Chris Lattner | ffaa0e6 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 346 | /// isTagName() - This method is called *for error recovery purposes only* |
| 347 | /// to determine if the specified name is a valid tag name ("struct foo"). If |
| 348 | /// so, this returns the TST for the tag corresponding to it (TST_enum, |
| Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 349 | /// TST_union, TST_struct, TST_interface, TST_class). This is used to diagnose |
| 350 | /// cases in C where the user forgot to specify the tag. |
| Chris Lattner | ffaa0e6 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 351 | DeclSpec::TST Sema::isTagName(IdentifierInfo &II, Scope *S) { |
| 352 | // Do a tag name lookup in this scope. |
| John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 353 | LookupResult R(*this, &II, SourceLocation(), LookupTagName); |
| 354 | LookupName(R, S, false); |
| 355 | R.suppressDiagnostics(); |
| 356 | if (R.getResultKind() == LookupResult::Found) |
| John McCall | 67c0087 | 2009-12-02 08:25:40 +0000 | [diff] [blame] | 357 | if (const TagDecl *TD = R.getAsSingle<TagDecl>()) { |
| Chris Lattner | ffaa0e6 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 358 | switch (TD->getTagKind()) { |
| Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 359 | case TTK_Struct: return DeclSpec::TST_struct; |
| Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 360 | case TTK_Interface: return DeclSpec::TST_interface; |
| Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 361 | case TTK_Union: return DeclSpec::TST_union; |
| 362 | case TTK_Class: return DeclSpec::TST_class; |
| 363 | case TTK_Enum: return DeclSpec::TST_enum; |
| Chris Lattner | ffaa0e6 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 364 | } |
| 365 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 366 | |
| Chris Lattner | ffaa0e6 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 367 | return DeclSpec::TST_unspecified; |
| 368 | } |
| 369 | |
| Francois Pichet | 48c946e | 2011-04-13 02:38:49 +0000 | [diff] [blame] | 370 | /// isMicrosoftMissingTypename - In Microsoft mode, within class scope, |
| 371 | /// if a CXXScopeSpec's type is equal to the type of one of the base classes |
| 372 | /// then downgrade the missing typename error to a warning. |
| 373 | /// This is needed for MSVC compatibility; Example: |
| 374 | /// @code |
| 375 | /// template<class T> class A { |
| 376 | /// public: |
| 377 | /// typedef int TYPE; |
| 378 | /// }; |
| 379 | /// template<class T> class B : public A<T> { |
| 380 | /// public: |
| 381 | /// A<T>::TYPE a; // no typename required because A<T> is a base class. |
| 382 | /// }; |
| 383 | /// @endcode |
| Francois Pichet | 9a57fb5 | 2011-10-11 01:50:09 +0000 | [diff] [blame] | 384 | bool Sema::isMicrosoftMissingTypename(const CXXScopeSpec *SS, Scope *S) { |
| Francois Pichet | 48c946e | 2011-04-13 02:38:49 +0000 | [diff] [blame] | 385 | if (CurContext->isRecord()) { |
| Francois Pichet | efc283c | 2011-04-13 02:44:57 +0000 | [diff] [blame] | 386 | const Type *Ty = SS->getScopeRep()->getAsType(); |
| Francois Pichet | 48c946e | 2011-04-13 02:38:49 +0000 | [diff] [blame] | 387 | |
| 388 | CXXRecordDecl *RD = cast<CXXRecordDecl>(CurContext); |
| 389 | for (CXXRecordDecl::base_class_const_iterator Base = RD->bases_begin(), |
| 390 | BaseEnd = RD->bases_end(); Base != BaseEnd; ++Base) |
| 391 | if (Context.hasSameUnqualifiedType(QualType(Ty, 1), Base->getType())) |
| 392 | return true; |
| Francois Pichet | 9a57fb5 | 2011-10-11 01:50:09 +0000 | [diff] [blame] | 393 | return S->isFunctionPrototypeScope(); |
| Francois Pichet | 48c946e | 2011-04-13 02:38:49 +0000 | [diff] [blame] | 394 | } |
| Francois Pichet | 9a57fb5 | 2011-10-11 01:50:09 +0000 | [diff] [blame] | 395 | return CurContext->isFunctionOrMethod() || S->isFunctionPrototypeScope(); |
| Francois Pichet | 48c946e | 2011-04-13 02:38:49 +0000 | [diff] [blame] | 396 | } |
| 397 | |
| Kaelyn Uhrain | b5b17fe | 2012-06-15 23:45:58 +0000 | [diff] [blame] | 398 | bool Sema::DiagnoseUnknownTypeName(IdentifierInfo *&II, |
| Douglas Gregor | 15e5602 | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 399 | SourceLocation IILoc, |
| 400 | Scope *S, |
| Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 401 | CXXScopeSpec *SS, |
| Kaelyn Uhrain | 67b44c9 | 2014-02-13 20:14:07 +0000 | [diff] [blame] | 402 | ParsedType &SuggestedType, |
| 403 | bool AllowClassTemplates) { |
| Douglas Gregor | 15e5602 | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 404 | // We don't have anything to suggest (yet). |
| John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 405 | SuggestedType = ParsedType(); |
| Douglas Gregor | 15e5602 | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 406 | |
| Douglas Gregor | 2d43530 | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 407 | // There may have been a typo in the name of the type. Look up typo |
| 408 | // results, in case we have something that we can suggest. |
| Kaelyn Uhrain | 67b44c9 | 2014-02-13 20:14:07 +0000 | [diff] [blame] | 409 | TypeNameValidatorCCC Validator(false, false, AllowClassTemplates); |
| Kaelyn Uhrain | b5b17fe | 2012-06-15 23:45:58 +0000 | [diff] [blame] | 410 | if (TypoCorrection Corrected = CorrectTypo(DeclarationNameInfo(II, IILoc), |
| Kaelyn Uhrain | b137840 | 2012-01-18 21:41:41 +0000 | [diff] [blame] | 411 | LookupOrdinaryName, S, SS, |
| Kaelyn Uhrain | 4e8942c | 2012-01-31 23:49:25 +0000 | [diff] [blame] | 412 | Validator)) { |
| Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 413 | if (Corrected.isKeyword()) { |
| 414 | // We corrected to a keyword. |
| Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 415 | diagnoseTypo(Corrected, PDiag(diag::err_unknown_typename_suggest) << II); |
| 416 | II = Corrected.getCorrectionAsIdentifierInfo(); |
| Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 417 | } else { |
| Kaelyn Uhrain | b137840 | 2012-01-18 21:41:41 +0000 | [diff] [blame] | 418 | // We found a similarly-named type or interface; suggest that. |
| Kaelyn Uhrain | 10413a4 | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 419 | if (!SS || !SS->isSet()) { |
| Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 420 | diagnoseTypo(Corrected, |
| 421 | PDiag(diag::err_unknown_typename_suggest) << II); |
| Kaelyn Uhrain | 10413a4 | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 422 | } else if (DeclContext *DC = computeDeclContext(*SS, false)) { |
| Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 423 | std::string CorrectedStr(Corrected.getAsString(getLangOpts())); |
| 424 | bool DroppedSpecifier = Corrected.WillReplaceSpecifier() && |
| Kaelyn Uhrain | 10413a4 | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 425 | II->getName().equals(CorrectedStr); |
| Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 426 | diagnoseTypo(Corrected, |
| 427 | PDiag(diag::err_unknown_nested_typename_suggest) |
| 428 | << II << DC << DroppedSpecifier << SS->getRange()); |
| 429 | } else { |
| Kaelyn Uhrain | b137840 | 2012-01-18 21:41:41 +0000 | [diff] [blame] | 430 | llvm_unreachable("could not have corrected a typo here"); |
| Kaelyn Uhrain | 10413a4 | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 431 | } |
| Douglas Gregor | 2d43530 | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 432 | |
| Kaelyn Uhrain | f734301 | 2013-09-26 21:13:05 +0000 | [diff] [blame] | 433 | CXXScopeSpec tmpSS; |
| 434 | if (Corrected.getCorrectionSpecifier()) |
| 435 | tmpSS.MakeTrivial(Context, Corrected.getCorrectionSpecifier(), |
| 436 | SourceRange(IILoc)); |
| Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 437 | SuggestedType = getTypeName(*Corrected.getCorrectionAsIdentifierInfo(), |
| Kaelyn Uhrain | f734301 | 2013-09-26 21:13:05 +0000 | [diff] [blame] | 438 | IILoc, S, tmpSS.isSet() ? &tmpSS : SS, false, |
| 439 | false, ParsedType(), |
| Abramo Bagnara | 4244b43 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 440 | /*IsCtorOrDtorName=*/false, |
| Kaelyn Uhrain | b137840 | 2012-01-18 21:41:41 +0000 | [diff] [blame] | 441 | /*NonTrivialTypeSourceInfo=*/true); |
| Douglas Gregor | 2d43530 | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 442 | } |
| Kaelyn Uhrain | b137840 | 2012-01-18 21:41:41 +0000 | [diff] [blame] | 443 | return true; |
| Douglas Gregor | 2d43530 | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 444 | } |
| 445 | |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 446 | if (getLangOpts().CPlusPlus) { |
| Jeffrey Yasskin | 54eba42 | 2010-04-08 21:04:54 +0000 | [diff] [blame] | 447 | // See if II is a class template that the user forgot to pass arguments to. |
| 448 | UnqualifiedId Name; |
| Kaelyn Uhrain | b5b17fe | 2012-06-15 23:45:58 +0000 | [diff] [blame] | 449 | Name.setIdentifier(II, IILoc); |
| Jeffrey Yasskin | 54eba42 | 2010-04-08 21:04:54 +0000 | [diff] [blame] | 450 | CXXScopeSpec EmptySS; |
| 451 | TemplateTy TemplateResult; |
| Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 452 | bool MemberOfUnknownSpecialization; |
| Abramo Bagnara | 7c5dee4 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 453 | if (isTemplateName(S, SS ? *SS : EmptySS, /*hasTemplateKeyword=*/false, |
| John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 454 | Name, ParsedType(), true, TemplateResult, |
| Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 455 | MemberOfUnknownSpecialization) == TNK_Type_template) { |
| Serge Pavlov | 9ddb76e | 2013-08-27 13:15:56 +0000 | [diff] [blame] | 456 | TemplateName TplName = TemplateResult.get(); |
| Jeffrey Yasskin | 54eba42 | 2010-04-08 21:04:54 +0000 | [diff] [blame] | 457 | Diag(IILoc, diag::err_template_missing_args) << TplName; |
| 458 | if (TemplateDecl *TplDecl = TplName.getAsTemplateDecl()) { |
| 459 | Diag(TplDecl->getLocation(), diag::note_template_decl_here) |
| 460 | << TplDecl->getTemplateParameters()->getSourceRange(); |
| 461 | } |
| 462 | return true; |
| 463 | } |
| 464 | } |
| 465 | |
| Douglas Gregor | 15e5602 | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 466 | // FIXME: Should we move the logic that tries to recover from a missing tag |
| 467 | // (struct, union, enum) from Parser::ParseImplicitInt here, instead? |
| 468 | |
| Douglas Gregor | 2d43530 | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 469 | if (!SS || (!SS->isSet() && !SS->isInvalid())) |
| Kaelyn Uhrain | b5b17fe | 2012-06-15 23:45:58 +0000 | [diff] [blame] | 470 | Diag(IILoc, diag::err_unknown_typename) << II; |
| Douglas Gregor | 15e5602 | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 471 | else if (DeclContext *DC = computeDeclContext(*SS, false)) |
| 472 | Diag(IILoc, diag::err_typename_nested_not_found) |
| Kaelyn Uhrain | b5b17fe | 2012-06-15 23:45:58 +0000 | [diff] [blame] | 473 | << II << DC << SS->getRange(); |
| Douglas Gregor | 15e5602 | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 474 | else if (isDependentScopeSpecifier(*SS)) { |
| Francois Pichet | 48c946e | 2011-04-13 02:38:49 +0000 | [diff] [blame] | 475 | unsigned DiagID = diag::err_typename_missing; |
| Alp Toker | bfa3934 | 2014-01-14 12:51:41 +0000 | [diff] [blame] | 476 | if (getLangOpts().MSVCCompat && isMicrosoftMissingTypename(SS, S)) |
| Francois Pichet | 9392165 | 2011-04-22 08:25:24 +0000 | [diff] [blame] | 477 | DiagID = diag::warn_typename_missing; |
| Francois Pichet | 48c946e | 2011-04-13 02:38:49 +0000 | [diff] [blame] | 478 | |
| 479 | Diag(SS->getRange().getBegin(), DiagID) |
| Aaron Ballman | 691e227 | 2014-01-03 14:48:20 +0000 | [diff] [blame] | 480 | << SS->getScopeRep() << II->getName() |
| Douglas Gregor | 15e5602 | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 481 | << SourceRange(SS->getRange().getBegin(), IILoc) |
| Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 482 | << FixItHint::CreateInsertion(SS->getRange().getBegin(), "typename "); |
| Kaelyn Uhrain | b5b17fe | 2012-06-15 23:45:58 +0000 | [diff] [blame] | 483 | SuggestedType = ActOnTypenameType(S, SourceLocation(), |
| 484 | *SS, *II, IILoc).get(); |
| Douglas Gregor | 15e5602 | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 485 | } else { |
| 486 | assert(SS && SS->isInvalid() && |
| 487 | "Invalid scope specifier has already been diagnosed"); |
| 488 | } |
| 489 | |
| 490 | return true; |
| 491 | } |
| Chris Lattner | ffaa0e6 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 492 | |
| Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 493 | /// \brief Determine whether the given result set contains either a type name |
| 494 | /// or |
| 495 | static bool isResultTypeOrTemplate(LookupResult &R, const Token &NextToken) { |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 496 | bool CheckTemplate = R.getSema().getLangOpts().CPlusPlus && |
| Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 497 | NextToken.is(tok::less); |
| 498 | |
| 499 | for (LookupResult::iterator I = R.begin(), IEnd = R.end(); I != IEnd; ++I) { |
| 500 | if (isa<TypeDecl>(*I) || isa<ObjCInterfaceDecl>(*I)) |
| 501 | return true; |
| 502 | |
| 503 | if (CheckTemplate && isa<TemplateDecl>(*I)) |
| 504 | return true; |
| 505 | } |
| 506 | |
| 507 | return false; |
| 508 | } |
| 509 | |
| Kaelyn Uhrain | 7179205 | 2012-05-02 00:11:40 +0000 | [diff] [blame] | 510 | static bool isTagTypeWithMissingTag(Sema &SemaRef, LookupResult &Result, |
| 511 | Scope *S, CXXScopeSpec &SS, |
| 512 | IdentifierInfo *&Name, |
| 513 | SourceLocation NameLoc) { |
| Richard Smith | aa31b4b | 2012-09-06 01:37:56 +0000 | [diff] [blame] | 514 | LookupResult R(SemaRef, Name, NameLoc, Sema::LookupTagName); |
| 515 | SemaRef.LookupParsedName(R, S, &SS); |
| 516 | if (TagDecl *Tag = R.getAsSingle<TagDecl>()) { |
| Kaelyn Uhrain | 7179205 | 2012-05-02 00:11:40 +0000 | [diff] [blame] | 517 | const char *TagName = 0; |
| 518 | const char *FixItTagName = 0; |
| 519 | switch (Tag->getTagKind()) { |
| 520 | case TTK_Class: |
| 521 | TagName = "class"; |
| 522 | FixItTagName = "class "; |
| 523 | break; |
| 524 | |
| 525 | case TTK_Enum: |
| 526 | TagName = "enum"; |
| 527 | FixItTagName = "enum "; |
| 528 | break; |
| 529 | |
| 530 | case TTK_Struct: |
| 531 | TagName = "struct"; |
| 532 | FixItTagName = "struct "; |
| 533 | break; |
| 534 | |
| Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 535 | case TTK_Interface: |
| 536 | TagName = "__interface"; |
| 537 | FixItTagName = "__interface "; |
| 538 | break; |
| 539 | |
| Kaelyn Uhrain | 7179205 | 2012-05-02 00:11:40 +0000 | [diff] [blame] | 540 | case TTK_Union: |
| 541 | TagName = "union"; |
| 542 | FixItTagName = "union "; |
| 543 | break; |
| 544 | } |
| 545 | |
| 546 | SemaRef.Diag(NameLoc, diag::err_use_of_tag_name_without_tag) |
| 547 | << Name << TagName << SemaRef.getLangOpts().CPlusPlus |
| 548 | << FixItHint::CreateInsertion(NameLoc, FixItTagName); |
| 549 | |
| Richard Smith | aa31b4b | 2012-09-06 01:37:56 +0000 | [diff] [blame] | 550 | for (LookupResult::iterator I = Result.begin(), IEnd = Result.end(); |
| 551 | I != IEnd; ++I) |
| 552 | SemaRef.Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type) |
| 553 | << Name << TagName; |
| 554 | |
| 555 | // Replace lookup results with just the tag decl. |
| 556 | Result.clear(Sema::LookupTagName); |
| 557 | SemaRef.LookupParsedName(Result, S, &SS); |
| Kaelyn Uhrain | 7179205 | 2012-05-02 00:11:40 +0000 | [diff] [blame] | 558 | return true; |
| 559 | } |
| 560 | |
| Kaelyn Uhrain | 7179205 | 2012-05-02 00:11:40 +0000 | [diff] [blame] | 561 | return false; |
| 562 | } |
| 563 | |
| Richard Smith | 4f605af | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 564 | /// Build a ParsedType for a simple-type-specifier with a nested-name-specifier. |
| 565 | static ParsedType buildNestedType(Sema &S, CXXScopeSpec &SS, |
| 566 | QualType T, SourceLocation NameLoc) { |
| 567 | ASTContext &Context = S.Context; |
| 568 | |
| 569 | TypeLocBuilder Builder; |
| 570 | Builder.pushTypeSpec(T).setNameLoc(NameLoc); |
| 571 | |
| 572 | T = S.getElaboratedType(ETK_None, SS, T); |
| 573 | ElaboratedTypeLoc ElabTL = Builder.push<ElaboratedTypeLoc>(T); |
| 574 | ElabTL.setElaboratedKeywordLoc(SourceLocation()); |
| 575 | ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 576 | return S.CreateParsedType(T, Builder.getTypeSourceInfo(Context, T)); |
| 577 | } |
| 578 | |
| Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 579 | Sema::NameClassification Sema::ClassifyName(Scope *S, |
| 580 | CXXScopeSpec &SS, |
| 581 | IdentifierInfo *&Name, |
| 582 | SourceLocation NameLoc, |
| Richard Smith | 4f605af | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 583 | const Token &NextToken, |
| 584 | bool IsAddressOfOperand, |
| 585 | CorrectionCandidateCallback *CCC) { |
| Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 586 | DeclarationNameInfo NameInfo(Name, NameLoc); |
| 587 | ObjCMethodDecl *CurMethod = getCurMethodDecl(); |
| 588 | |
| 589 | if (NextToken.is(tok::coloncolon)) { |
| 590 | BuildCXXNestedNameSpecifier(S, *Name, NameLoc, NextToken.getLocation(), |
| 591 | QualType(), false, SS, 0, false); |
| 592 | |
| 593 | } |
| 594 | |
| 595 | LookupResult Result(*this, Name, NameLoc, LookupOrdinaryName); |
| 596 | LookupParsedName(Result, S, &SS, !CurMethod); |
| 597 | |
| 598 | // Perform lookup for Objective-C instance variables (including automatically |
| 599 | // synthesized instance variables), if we're in an Objective-C method. |
| 600 | // FIXME: This lookup really, really needs to be folded in to the normal |
| 601 | // unqualified lookup mechanism. |
| 602 | if (!SS.isSet() && CurMethod && !isResultTypeOrTemplate(Result, NextToken)) { |
| 603 | ExprResult E = LookupInObjCMethod(Result, S, Name, true); |
| Douglas Gregor | b90f518 | 2011-04-25 15:05:41 +0000 | [diff] [blame] | 604 | if (E.get() || E.isInvalid()) |
| Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 605 | return E; |
| Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | bool SecondTry = false; |
| 609 | bool IsFilteredTemplateName = false; |
| 610 | |
| 611 | Corrected: |
| 612 | switch (Result.getResultKind()) { |
| 613 | case LookupResult::NotFound: |
| 614 | // If an unqualified-id is followed by a '(', then we have a function |
| 615 | // call. |
| 616 | if (!SS.isSet() && NextToken.is(tok::l_paren)) { |
| 617 | // In C++, this is an ADL-only call. |
| 618 | // FIXME: Reference? |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 619 | if (getLangOpts().CPlusPlus) |
| Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 620 | return BuildDeclarationNameExpr(SS, Result, /*ADL=*/true); |
| 621 | |
| 622 | // C90 6.3.2.2: |
| 623 | // If the expression that precedes the parenthesized argument list in a |
| 624 | // function call consists solely of an identifier, and if no |
| 625 | // declaration is visible for this identifier, the identifier is |
| 626 | // implicitly declared exactly as if, in the innermost block containing |
| 627 | // the function call, the declaration |
| 628 | // |
| 629 | // extern int identifier (); |
| 630 | // |
| 631 | // appeared. |
| 632 | // |
| 633 | // We also allow this in C99 as an extension. |
| 634 | if (NamedDecl *D = ImplicitlyDefineFunction(NameLoc, *Name, S)) { |
| 635 | Result.addDecl(D); |
| 636 | Result.resolveKind(); |
| 637 | return BuildDeclarationNameExpr(SS, Result, /*ADL=*/false); |
| 638 | } |
| 639 | } |
| 640 | |
| 641 | // In C, we first see whether there is a tag type by the same name, in |
| 642 | // which case it's likely that the user just forget to write "enum", |
| 643 | // "struct", or "union". |
| Kaelyn Uhrain | 7179205 | 2012-05-02 00:11:40 +0000 | [diff] [blame] | 644 | if (!getLangOpts().CPlusPlus && !SecondTry && |
| 645 | isTagTypeWithMissingTag(*this, Result, S, SS, Name, NameLoc)) { |
| 646 | break; |
| Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | // Perform typo correction to determine if there is another name that is |
| 650 | // close to this name. |
| Richard Smith | 4f605af | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 651 | if (!SecondTry && CCC) { |
| Douglas Gregor | 5cf0e15 | 2011-07-14 04:54:23 +0000 | [diff] [blame] | 652 | SecondTry = true; |
| Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 653 | if (TypoCorrection Corrected = CorrectTypo(Result.getLookupNameInfo(), |
| David Blaikie | 30d1544 | 2011-10-19 22:56:21 +0000 | [diff] [blame] | 654 | Result.getLookupKind(), S, |
| Richard Smith | 4f605af | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 655 | &SS, *CCC)) { |
| Douglas Gregor | 5e16c16 | 2011-04-27 03:47:06 +0000 | [diff] [blame] | 656 | unsigned UnqualifiedDiag = diag::err_undeclared_var_use_suggest; |
| 657 | unsigned QualifiedDiag = diag::err_no_member_suggest; |
| Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 658 | |
| Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 659 | NamedDecl *FirstDecl = Corrected.getCorrectionDecl(); |
| Douglas Gregor | 8b02cd0 | 2011-04-27 04:48:22 +0000 | [diff] [blame] | 660 | NamedDecl *UnderlyingFirstDecl |
| 661 | = FirstDecl? FirstDecl->getUnderlyingDecl() : 0; |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 662 | if (getLangOpts().CPlusPlus && NextToken.is(tok::less) && |
| Douglas Gregor | 8b02cd0 | 2011-04-27 04:48:22 +0000 | [diff] [blame] | 663 | UnderlyingFirstDecl && isa<TemplateDecl>(UnderlyingFirstDecl)) { |
| Douglas Gregor | 5e16c16 | 2011-04-27 03:47:06 +0000 | [diff] [blame] | 664 | UnqualifiedDiag = diag::err_no_template_suggest; |
| 665 | QualifiedDiag = diag::err_no_member_template_suggest; |
| Douglas Gregor | 8b02cd0 | 2011-04-27 04:48:22 +0000 | [diff] [blame] | 666 | } else if (UnderlyingFirstDecl && |
| 667 | (isa<TypeDecl>(UnderlyingFirstDecl) || |
| 668 | isa<ObjCInterfaceDecl>(UnderlyingFirstDecl) || |
| 669 | isa<ObjCCompatibleAliasDecl>(UnderlyingFirstDecl))) { |
| David Blaikie | 9db0604 | 2013-03-21 21:35:15 +0000 | [diff] [blame] | 670 | UnqualifiedDiag = diag::err_unknown_typename_suggest; |
| 671 | QualifiedDiag = diag::err_unknown_nested_typename_suggest; |
| 672 | } |
| Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 673 | |
| Kaelyn Uhrain | 10413a4 | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 674 | if (SS.isEmpty()) { |
| Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 675 | diagnoseTypo(Corrected, PDiag(UnqualifiedDiag) << Name); |
| Kaelyn Uhrain | 10413a4 | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 676 | } else {// FIXME: is this even reachable? Test it. |
| Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 677 | std::string CorrectedStr(Corrected.getAsString(getLangOpts())); |
| 678 | bool DroppedSpecifier = Corrected.WillReplaceSpecifier() && |
| Kaelyn Uhrain | 10413a4 | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 679 | Name->getName().equals(CorrectedStr); |
| Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 680 | diagnoseTypo(Corrected, PDiag(QualifiedDiag) |
| 681 | << Name << computeDeclContext(SS, false) |
| 682 | << DroppedSpecifier << SS.getRange()); |
| Kaelyn Uhrain | 10413a4 | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 683 | } |
| Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 684 | |
| 685 | // Update the name, so that the caller has the new name. |
| Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 686 | Name = Corrected.getCorrectionAsIdentifierInfo(); |
| Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 687 | |
| Kaelyn Uhrain | 9ce58bd | 2012-01-24 19:45:35 +0000 | [diff] [blame] | 688 | // Typo correction corrected to a keyword. |
| 689 | if (Corrected.isKeyword()) |
| Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 690 | return Name; |
| Kaelyn Uhrain | 9ce58bd | 2012-01-24 19:45:35 +0000 | [diff] [blame] | 691 | |
| Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 692 | // Also update the LookupResult... |
| 693 | // FIXME: This should probably go away at some point |
| 694 | Result.clear(); |
| 695 | Result.setLookupName(Corrected.getCorrection()); |
| Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 696 | if (FirstDecl) |
| Kaelyn Uhrain | 9ce58bd | 2012-01-24 19:45:35 +0000 | [diff] [blame] | 697 | Result.addDecl(FirstDecl); |
| Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 698 | |
| 699 | // If we found an Objective-C instance variable, let |
| 700 | // LookupInObjCMethod build the appropriate expression to |
| 701 | // reference the ivar. |
| 702 | // FIXME: This is a gross hack. |
| 703 | if (ObjCIvarDecl *Ivar = Result.getAsSingle<ObjCIvarDecl>()) { |
| 704 | Result.clear(); |
| 705 | ExprResult E(LookupInObjCMethod(Result, S, Ivar->getIdentifier())); |
| Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 706 | return E; |
| Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | goto Corrected; |
| 710 | } |
| 711 | } |
| 712 | |
| 713 | // We failed to correct; just fall through and let the parser deal with it. |
| 714 | Result.suppressDiagnostics(); |
| 715 | return NameClassification::Unknown(); |
| 716 | |
| Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 717 | case LookupResult::NotFoundInCurrentInstantiation: { |
| Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 718 | // We performed name lookup into the current instantiation, and there were |
| 719 | // dependent bases, so we treat this result the same way as any other |
| 720 | // dependent nested-name-specifier. |
| 721 | |
| 722 | // C++ [temp.res]p2: |
| 723 | // A name used in a template declaration or definition and that is |
| 724 | // dependent on a template-parameter is assumed not to name a type |
| 725 | // unless the applicable name lookup finds a type name or the name is |
| 726 | // qualified by the keyword typename. |
| 727 | // |
| 728 | // FIXME: If the next token is '<', we might want to ask the parser to |
| 729 | // perform some heroics to see if we actually have a |
| 730 | // template-argument-list, which would indicate a missing 'template' |
| 731 | // keyword here. |
| Richard Smith | 4f605af | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 732 | return ActOnDependentIdExpression(SS, /*TemplateKWLoc=*/SourceLocation(), |
| 733 | NameInfo, IsAddressOfOperand, |
| 734 | /*TemplateArgs=*/0); |
| Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 735 | } |
| Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 736 | |
| 737 | case LookupResult::Found: |
| 738 | case LookupResult::FoundOverloaded: |
| 739 | case LookupResult::FoundUnresolvedValue: |
| 740 | break; |
| 741 | |
| 742 | case LookupResult::Ambiguous: |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 743 | if (getLangOpts().CPlusPlus && NextToken.is(tok::less) && |
| Douglas Gregor | 8b02cd0 | 2011-04-27 04:48:22 +0000 | [diff] [blame] | 744 | hasAnyAcceptableTemplateNames(Result)) { |
| Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 745 | // C++ [temp.local]p3: |
| 746 | // A lookup that finds an injected-class-name (10.2) can result in an |
| 747 | // ambiguity in certain cases (for example, if it is found in more than |
| 748 | // one base class). If all of the injected-class-names that are found |
| 749 | // refer to specializations of the same class template, and if the name |
| 750 | // is followed by a template-argument-list, the reference refers to the |
| 751 | // class template itself and not a specialization thereof, and is not |
| 752 | // ambiguous. |
| 753 | // |
| 754 | // This filtering can make an ambiguous result into an unambiguous one, |
| 755 | // so try again after filtering out template names. |
| 756 | FilterAcceptableTemplateNames(Result); |
| 757 | if (!Result.isAmbiguous()) { |
| 758 | IsFilteredTemplateName = true; |
| 759 | break; |
| 760 | } |
| 761 | } |
| 762 | |
| 763 | // Diagnose the ambiguity and return an error. |
| 764 | return NameClassification::Error(); |
| 765 | } |
| 766 | |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 767 | if (getLangOpts().CPlusPlus && NextToken.is(tok::less) && |
| Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 768 | (IsFilteredTemplateName || hasAnyAcceptableTemplateNames(Result))) { |
| 769 | // C++ [temp.names]p3: |
| 770 | // After name lookup (3.4) finds that a name is a template-name or that |
| 771 | // an operator-function-id or a literal- operator-id refers to a set of |
| 772 | // overloaded functions any member of which is a function template if |
| 773 | // this is followed by a <, the < is always taken as the delimiter of a |
| 774 | // template-argument-list and never as the less-than operator. |
| 775 | if (!IsFilteredTemplateName) |
| 776 | FilterAcceptableTemplateNames(Result); |
| 777 | |
| Douglas Gregor | 8b02cd0 | 2011-04-27 04:48:22 +0000 | [diff] [blame] | 778 | if (!Result.empty()) { |
| 779 | bool IsFunctionTemplate; |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 780 | bool IsVarTemplate; |
| Douglas Gregor | 8b02cd0 | 2011-04-27 04:48:22 +0000 | [diff] [blame] | 781 | TemplateName Template; |
| 782 | if (Result.end() - Result.begin() > 1) { |
| 783 | IsFunctionTemplate = true; |
| 784 | Template = Context.getOverloadedTemplateName(Result.begin(), |
| 785 | Result.end()); |
| 786 | } else { |
| 787 | TemplateDecl *TD |
| 788 | = cast<TemplateDecl>((*Result.begin())->getUnderlyingDecl()); |
| 789 | IsFunctionTemplate = isa<FunctionTemplateDecl>(TD); |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 790 | IsVarTemplate = isa<VarTemplateDecl>(TD); |
| 791 | |
| Douglas Gregor | 8b02cd0 | 2011-04-27 04:48:22 +0000 | [diff] [blame] | 792 | if (SS.isSet() && !SS.isInvalid()) |
| 793 | Template = Context.getQualifiedTemplateName(SS.getScopeRep(), |
| Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 794 | /*TemplateKeyword=*/false, |
| Douglas Gregor | 8b02cd0 | 2011-04-27 04:48:22 +0000 | [diff] [blame] | 795 | TD); |
| 796 | else |
| 797 | Template = TemplateName(TD); |
| 798 | } |
| Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 799 | |
| Douglas Gregor | 8b02cd0 | 2011-04-27 04:48:22 +0000 | [diff] [blame] | 800 | if (IsFunctionTemplate) { |
| 801 | // Function templates always go through overload resolution, at which |
| 802 | // point we'll perform the various checks (e.g., accessibility) we need |
| 803 | // to based on which function we selected. |
| 804 | Result.suppressDiagnostics(); |
| 805 | |
| 806 | return NameClassification::FunctionTemplate(Template); |
| 807 | } |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 808 | |
| 809 | return IsVarTemplate ? NameClassification::VarTemplate(Template) |
| 810 | : NameClassification::TypeTemplate(Template); |
| Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 811 | } |
| Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 812 | } |
| Richard Smith | 4f605af | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 813 | |
| Douglas Gregor | 8b02cd0 | 2011-04-27 04:48:22 +0000 | [diff] [blame] | 814 | NamedDecl *FirstDecl = (*Result.begin())->getUnderlyingDecl(); |
| Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 815 | if (TypeDecl *Type = dyn_cast<TypeDecl>(FirstDecl)) { |
| 816 | DiagnoseUseOfDecl(Type, NameLoc); |
| 817 | QualType T = Context.getTypeDeclType(Type); |
| Richard Smith | 4f605af | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 818 | if (SS.isNotEmpty()) |
| 819 | return buildNestedType(*this, SS, T, NameLoc); |
| Kaelyn Uhrain | 7179205 | 2012-05-02 00:11:40 +0000 | [diff] [blame] | 820 | return ParsedType::make(T); |
| Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 821 | } |
| Richard Smith | 4f605af | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 822 | |
| Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 823 | ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(FirstDecl); |
| 824 | if (!Class) { |
| 825 | // FIXME: It's unfortunate that we don't have a Type node for handling this. |
| 826 | if (ObjCCompatibleAliasDecl *Alias |
| 827 | = dyn_cast<ObjCCompatibleAliasDecl>(FirstDecl)) |
| 828 | Class = Alias->getClassInterface(); |
| 829 | } |
| 830 | |
| 831 | if (Class) { |
| 832 | DiagnoseUseOfDecl(Class, NameLoc); |
| 833 | |
| 834 | if (NextToken.is(tok::period)) { |
| 835 | // Interface. <something> is parsed as a property reference expression. |
| 836 | // Just return "unknown" as a fall-through for now. |
| 837 | Result.suppressDiagnostics(); |
| 838 | return NameClassification::Unknown(); |
| 839 | } |
| 840 | |
| 841 | QualType T = Context.getObjCInterfaceType(Class); |
| 842 | return ParsedType::make(T); |
| 843 | } |
| Kaelyn Uhrain | 7179205 | 2012-05-02 00:11:40 +0000 | [diff] [blame] | 844 | |
| Richard Smith | 4f605af | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 845 | // We can have a type template here if we're classifying a template argument. |
| 846 | if (isa<TemplateDecl>(FirstDecl) && !isa<FunctionTemplateDecl>(FirstDecl)) |
| 847 | return NameClassification::TypeTemplate( |
| 848 | TemplateName(cast<TemplateDecl>(FirstDecl))); |
| 849 | |
| Kaelyn Uhrain | 7179205 | 2012-05-02 00:11:40 +0000 | [diff] [blame] | 850 | // Check for a tag type hidden by a non-type decl in a few cases where it |
| 851 | // seems likely a type is wanted instead of the non-type that was found. |
| Argyrios Kyrtzidis | c64c029 | 2013-05-07 19:54:28 +0000 | [diff] [blame] | 852 | bool NextIsOp = NextToken.is(tok::amp) || NextToken.is(tok::star); |
| 853 | if ((NextToken.is(tok::identifier) || |
| Alp Toker | a2794f9 | 2014-01-22 07:29:52 +0000 | [diff] [blame] | 854 | (NextIsOp && |
| 855 | FirstDecl->getUnderlyingDecl()->isFunctionOrFunctionTemplate())) && |
| Argyrios Kyrtzidis | c64c029 | 2013-05-07 19:54:28 +0000 | [diff] [blame] | 856 | isTagTypeWithMissingTag(*this, Result, S, SS, Name, NameLoc)) { |
| 857 | TypeDecl *Type = Result.getAsSingle<TypeDecl>(); |
| 858 | DiagnoseUseOfDecl(Type, NameLoc); |
| 859 | QualType T = Context.getTypeDeclType(Type); |
| 860 | if (SS.isNotEmpty()) |
| 861 | return buildNestedType(*this, SS, T, NameLoc); |
| 862 | return ParsedType::make(T); |
| Kaelyn Uhrain | 7179205 | 2012-05-02 00:11:40 +0000 | [diff] [blame] | 863 | } |
| Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 864 | |
| Richard Smith | 4f605af | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 865 | if (FirstDecl->isCXXClassMember()) |
| Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 866 | return BuildPossibleImplicitMemberExpr(SS, SourceLocation(), Result, 0); |
| Douglas Gregor | 8b02cd0 | 2011-04-27 04:48:22 +0000 | [diff] [blame] | 867 | |
| Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 868 | bool ADL = UseArgumentDependentLookup(SS, Result, NextToken.is(tok::l_paren)); |
| 869 | return BuildDeclarationNameExpr(SS, Result, ADL); |
| 870 | } |
| 871 | |
| John McCall | 5ed6e8f | 2009-08-18 00:00:49 +0000 | [diff] [blame] | 872 | // Determines the context to return to after temporarily entering a |
| 873 | // context. This depends in an unnecessarily complicated way on the |
| 874 | // exact ordering of callbacks from the parser. |
| Argyrios Kyrtzidis | 16ac9be | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 875 | DeclContext *Sema::getContainingDC(DeclContext *DC) { |
| Argyrios Kyrtzidis | 16ac9be | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 876 | |
| John McCall | 5ed6e8f | 2009-08-18 00:00:49 +0000 | [diff] [blame] | 877 | // Functions defined inline within classes aren't parsed until we've |
| 878 | // finished parsing the top-level class, so the top-level class is |
| 879 | // the context we'll need to return to. |
| Faisal Vali | bb9071e | 2013-12-04 22:43:08 +0000 | [diff] [blame] | 880 | // A Lambda call operator whose parent is a class must not be treated |
| 881 | // as an inline member function. A Lambda can be used legally |
| 882 | // either as an in-class member initializer or a default argument. These |
| 883 | // are parsed once the class has been marked complete and so the containing |
| 884 | // context would be the nested class (when the lambda is defined in one); |
| 885 | // If the class is not complete, then the lambda is being used in an |
| 886 | // ill-formed fashion (such as to specify the width of a bit-field, or |
| 887 | // in an array-bound) - in which case we still want to return the |
| 888 | // lexically containing DC (which could be a nested class). |
| 889 | if (isa<FunctionDecl>(DC) && !isLambdaCallOperator(DC)) { |
| John McCall | 5ed6e8f | 2009-08-18 00:00:49 +0000 | [diff] [blame] | 890 | DC = DC->getLexicalParent(); |
| 891 | |
| 892 | // A function not defined within a class will always return to its |
| 893 | // lexical context. |
| 894 | if (!isa<CXXRecordDecl>(DC)) |
| 895 | return DC; |
| 896 | |
| 897 | // A C++ inline method/friend is parsed *after* the topmost class |
| 898 | // it was declared in is fully parsed ("complete"); the topmost |
| 899 | // class is the context we need to return to. |
| Argyrios Kyrtzidis | 0d09c49 | 2008-11-19 18:01:13 +0000 | [diff] [blame] | 900 | while (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(DC->getLexicalParent())) |
| Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 901 | DC = RD; |
| 902 | |
| 903 | // Return the declaration context of the topmost class the inline method is |
| 904 | // declared in. |
| 905 | return DC; |
| 906 | } |
| 907 | |
| Argyrios Kyrtzidis | 0d09c49 | 2008-11-19 18:01:13 +0000 | [diff] [blame] | 908 | return DC->getLexicalParent(); |
| Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 909 | } |
| 910 | |
| Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 911 | void Sema::PushDeclContext(Scope *S, DeclContext *DC) { |
| Argyrios Kyrtzidis | 16ac9be | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 912 | assert(getContainingDC(DC) == CurContext && |
| Zhongxing Xu | 17a37eb | 2008-12-08 07:14:51 +0000 | [diff] [blame] | 913 | "The next DeclContext should be lexically contained in the current one."); |
| Chris Lattner | bec4134 | 2008-04-22 18:39:57 +0000 | [diff] [blame] | 914 | CurContext = DC; |
| Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 915 | S->setEntity(DC); |
| Chris Lattner | c5ffed4 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 916 | } |
| 917 | |
| Chris Lattner | 0a5ff0d | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 918 | void Sema::PopDeclContext() { |
| 919 | assert(CurContext && "DeclContext imbalance!"); |
| Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 920 | |
| Argyrios Kyrtzidis | 16ac9be | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 921 | CurContext = getContainingDC(CurContext); |
| John McCall | dd762d1 | 2010-07-23 22:45:07 +0000 | [diff] [blame] | 922 | assert(CurContext && "Popped translation unit!"); |
| Chris Lattner | c5ffed4 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 923 | } |
| 924 | |
| Argyrios Kyrtzidis | 9941a4d | 2009-06-17 23:19:02 +0000 | [diff] [blame] | 925 | /// EnterDeclaratorContext - Used when we must lookup names in the context |
| 926 | /// of a declarator's nested name specifier. |
| John McCall | 6df5fef | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 927 | /// |
| Argyrios Kyrtzidis | 7bcce49 | 2009-06-17 23:15:40 +0000 | [diff] [blame] | 928 | void Sema::EnterDeclaratorContext(Scope *S, DeclContext *DC) { |
| John McCall | 6df5fef | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 929 | // C++0x [basic.lookup.unqual]p13: |
| 930 | // A name used in the definition of a static data member of class |
| 931 | // X (after the qualified-id of the static member) is looked up as |
| 932 | // if the name was used in a member function of X. |
| 933 | // C++0x [basic.lookup.unqual]p14: |
| 934 | // If a variable member of a namespace is defined outside of the |
| 935 | // scope of its namespace then any name used in the definition of |
| 936 | // the variable member (after the declarator-id) is looked up as |
| 937 | // if the definition of the variable member occurred in its |
| 938 | // namespace. |
| 939 | // Both of these imply that we should push a scope whose context |
| 940 | // is the semantic context of the declaration. We can't use |
| 941 | // PushDeclContext here because that context is not necessarily |
| 942 | // lexically contained in the current context. Fortunately, |
| 943 | // the containing scope should have the appropriate information. |
| 944 | |
| 945 | assert(!S->getEntity() && "scope already has entity"); |
| 946 | |
| 947 | #ifndef NDEBUG |
| 948 | Scope *Ancestor = S->getParent(); |
| 949 | while (!Ancestor->getEntity()) Ancestor = Ancestor->getParent(); |
| 950 | assert(Ancestor->getEntity() == CurContext && "ancestor context mismatch"); |
| 951 | #endif |
| 952 | |
| Argyrios Kyrtzidis | 7bcce49 | 2009-06-17 23:15:40 +0000 | [diff] [blame] | 953 | CurContext = DC; |
| John McCall | 6df5fef | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 954 | S->setEntity(DC); |
| Argyrios Kyrtzidis | 7bcce49 | 2009-06-17 23:15:40 +0000 | [diff] [blame] | 955 | } |
| 956 | |
| Argyrios Kyrtzidis | 7bcce49 | 2009-06-17 23:15:40 +0000 | [diff] [blame] | 957 | void Sema::ExitDeclaratorContext(Scope *S) { |
| John McCall | 6df5fef | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 958 | assert(S->getEntity() == CurContext && "Context imbalance!"); |
| Argyrios Kyrtzidis | 7bcce49 | 2009-06-17 23:15:40 +0000 | [diff] [blame] | 959 | |
| John McCall | 6df5fef | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 960 | // Switch back to the lexical context. The safety of this is |
| 961 | // enforced by an assert in EnterDeclaratorContext. |
| 962 | Scope *Ancestor = S->getParent(); |
| 963 | while (!Ancestor->getEntity()) Ancestor = Ancestor->getParent(); |
| Ted Kremenek | c37877d | 2013-10-08 17:08:03 +0000 | [diff] [blame] | 964 | CurContext = Ancestor->getEntity(); |
| John McCall | 6df5fef | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 965 | |
| 966 | // We don't need to do anything with the scope, which is going to |
| 967 | // disappear. |
| Argyrios Kyrtzidis | 7bcce49 | 2009-06-17 23:15:40 +0000 | [diff] [blame] | 968 | } |
| 969 | |
| Caitlin Sadowski | 990d571 | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 970 | |
| 971 | void Sema::ActOnReenterFunctionContext(Scope* S, Decl *D) { |
| Alp Toker | a2794f9 | 2014-01-22 07:29:52 +0000 | [diff] [blame] | 972 | // We assume that the caller has already called |
| 973 | // ActOnReenterTemplateScope so getTemplatedDecl() works. |
| 974 | FunctionDecl *FD = D->getAsFunction(); |
| Caitlin Sadowski | 990d571 | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 975 | if (!FD) |
| 976 | return; |
| 977 | |
| DeLesley Hutchins | 6f86004 | 2012-04-06 15:10:17 +0000 | [diff] [blame] | 978 | // Same implementation as PushDeclContext, but enters the context |
| 979 | // from the lexical parent, rather than the top-level class. |
| 980 | assert(CurContext == FD->getLexicalParent() && |
| 981 | "The next DeclContext should be lexically contained in the current one."); |
| 982 | CurContext = FD; |
| 983 | S->setEntity(CurContext); |
| 984 | |
| Caitlin Sadowski | 990d571 | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 985 | for (unsigned P = 0, NumParams = FD->getNumParams(); P < NumParams; ++P) { |
| 986 | ParmVarDecl *Param = FD->getParamDecl(P); |
| 987 | // If the parameter has an identifier, then add it to the scope |
| 988 | if (Param->getIdentifier()) { |
| 989 | S->AddDecl(Param); |
| 990 | IdResolver.AddDecl(Param); |
| 991 | } |
| 992 | } |
| 993 | } |
| 994 | |
| 995 | |
| DeLesley Hutchins | 6f86004 | 2012-04-06 15:10:17 +0000 | [diff] [blame] | 996 | void Sema::ActOnExitFunctionContext() { |
| 997 | // Same implementation as PopDeclContext, but returns to the lexical parent, |
| 998 | // rather than the top-level class. |
| 999 | assert(CurContext && "DeclContext imbalance!"); |
| 1000 | CurContext = CurContext->getLexicalParent(); |
| 1001 | assert(CurContext && "Popped translation unit!"); |
| 1002 | } |
| 1003 | |
| 1004 | |
| Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1005 | /// \brief Determine whether we allow overloading of the function |
| 1006 | /// PrevDecl with another declaration. |
| 1007 | /// |
| 1008 | /// This routine determines whether overloading is possible, not |
| 1009 | /// whether some new function is actually an overload. It will return |
| 1010 | /// true in C++ (where we can always provide overloads) or, as an |
| 1011 | /// extension, in C when the previous function is already an |
| 1012 | /// overloaded function declaration or has the "overloadable" |
| 1013 | /// attribute. |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1014 | static bool AllowOverloadingOfFunction(LookupResult &Previous, |
| 1015 | ASTContext &Context) { |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1016 | if (Context.getLangOpts().CPlusPlus) |
| Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1017 | return true; |
| 1018 | |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1019 | if (Previous.getResultKind() == LookupResult::FoundOverloaded) |
| Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1020 | return true; |
| 1021 | |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1022 | return (Previous.getResultKind() == LookupResult::Found |
| 1023 | && Previous.getFoundDecl()->hasAttr<OverloadableAttr>()); |
| Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1024 | } |
| 1025 | |
| Argyrios Kyrtzidis | b8a4920 | 2008-04-12 00:47:19 +0000 | [diff] [blame] | 1026 | /// Add this decl to the scope shadowed decl chains. |
| John McCall | 759e32b | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 1027 | void Sema::PushOnScopeChains(NamedDecl *D, Scope *S, bool AddToContext) { |
| Douglas Gregor | 07665a6 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 1028 | // Move up the scope chain until we find the nearest enclosing |
| 1029 | // non-transparent context. The declaration will be introduced into this |
| 1030 | // scope. |
| Ted Kremenek | c37877d | 2013-10-08 17:08:03 +0000 | [diff] [blame] | 1031 | while (S->getEntity() && S->getEntity()->isTransparentContext()) |
| Douglas Gregor | 07665a6 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 1032 | S = S->getParent(); |
| 1033 | |
| Douglas Gregor | 8b9ccca | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 1034 | // Add scoped declarations into their context, so that they can be |
| 1035 | // found later. Declarations without a context won't be inserted |
| 1036 | // into any context. |
| John McCall | 759e32b | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 1037 | if (AddToContext) |
| 1038 | CurContext->addDecl(D); |
| Douglas Gregor | 8b9ccca | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 1039 | |
| Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 1040 | // Out-of-line definitions shouldn't be pushed into scope in C++, unless they |
| 1041 | // are function-local declarations. |
| 1042 | if (getLangOpts().CPlusPlus && D->isOutOfLine() && |
| Douglas Gregor | f7b9895 | 2011-10-09 22:57:49 +0000 | [diff] [blame] | 1043 | !D->getDeclContext()->getRedeclContext()->Equals( |
| Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 1044 | D->getLexicalDeclContext()->getRedeclContext()) && |
| 1045 | !D->getLexicalDeclContext()->isFunctionOrMethod()) |
| Chandler Carruth | f50ef6e | 2010-02-21 07:08:09 +0000 | [diff] [blame] | 1046 | return; |
| 1047 | |
| 1048 | // Template instantiations should also not be pushed into scope. |
| 1049 | if (isa<FunctionDecl>(D) && |
| 1050 | cast<FunctionDecl>(D)->isFunctionTemplateSpecialization()) |
| Douglas Gregor | 5ad7c54 | 2009-09-28 18:41:37 +0000 | [diff] [blame] | 1051 | return; |
| 1052 | |
| John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1053 | // If this replaces anything in the current scope, |
| 1054 | IdentifierResolver::iterator I = IdResolver.begin(D->getDeclName()), |
| 1055 | IEnd = IdResolver.end(); |
| 1056 | for (; I != IEnd; ++I) { |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1057 | if (S->isDeclScope(*I) && D->declarationReplaces(*I)) { |
| 1058 | S->RemoveDecl(*I); |
| John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1059 | IdResolver.RemoveDecl(*I); |
| Argyrios Kyrtzidis | fa8e15b | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 1060 | |
| John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1061 | // Should only need to replace one decl. |
| 1062 | break; |
| Douglas Gregor | 38feed8 | 2009-04-24 02:57:34 +0000 | [diff] [blame] | 1063 | } |
| Argyrios Kyrtzidis | fa8e15b | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 1064 | } |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1065 | |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1066 | S->AddDecl(D); |
| Douglas Gregor | 88764cf | 2011-03-14 21:19:51 +0000 | [diff] [blame] | 1067 | |
| 1068 | if (isa<LabelDecl>(D) && !cast<LabelDecl>(D)->isGnuLocal()) { |
| 1069 | // Implicitly-generated labels may end up getting generated in an order that |
| 1070 | // isn't strictly lexical, which breaks name lookup. Be careful to insert |
| 1071 | // the label at the appropriate place in the identifier chain. |
| 1072 | for (I = IdResolver.begin(D->getDeclName()); I != IEnd; ++I) { |
| Douglas Gregor | d7d7e0d | 2011-03-24 14:35:16 +0000 | [diff] [blame] | 1073 | DeclContext *IDC = (*I)->getLexicalDeclContext()->getRedeclContext(); |
| Douglas Gregor | 46c04e7 | 2011-03-16 16:39:03 +0000 | [diff] [blame] | 1074 | if (IDC == CurContext) { |
| 1075 | if (!S->isDeclScope(*I)) |
| 1076 | continue; |
| 1077 | } else if (IDC->Encloses(CurContext)) |
| Douglas Gregor | 88764cf | 2011-03-14 21:19:51 +0000 | [diff] [blame] | 1078 | break; |
| 1079 | } |
| 1080 | |
| Douglas Gregor | 46c04e7 | 2011-03-16 16:39:03 +0000 | [diff] [blame] | 1081 | IdResolver.InsertDeclAfter(I, D); |
| Douglas Gregor | 88764cf | 2011-03-14 21:19:51 +0000 | [diff] [blame] | 1082 | } else { |
| 1083 | IdResolver.AddDecl(D); |
| 1084 | } |
| Argyrios Kyrtzidis | b8a4920 | 2008-04-12 00:47:19 +0000 | [diff] [blame] | 1085 | } |
| 1086 | |
| Douglas Gregor | 935bc7a2 | 2011-10-27 09:33:13 +0000 | [diff] [blame] | 1087 | void Sema::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) { |
| 1088 | if (IdResolver.tryAddTopLevelDecl(D, Name) && TUScope) |
| 1089 | TUScope->AddDecl(D); |
| 1090 | } |
| 1091 | |
| Richard Smith | 1c34fb7 | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 1092 | bool Sema::isDeclInScope(NamedDecl *D, DeclContext *Ctx, Scope *S, |
| Richard Smith | 72bcaec | 2013-12-05 04:30:04 +0000 | [diff] [blame] | 1093 | bool AllowInlineNamespace) { |
| 1094 | return IdResolver.isDeclInScope(D, Ctx, S, AllowInlineNamespace); |
| Douglas Gregor | 505ad49 | 2009-09-28 00:47:05 +0000 | [diff] [blame] | 1095 | } |
| 1096 | |
| John McCall | cc14d1f | 2010-08-24 08:50:51 +0000 | [diff] [blame] | 1097 | Scope *Sema::getScopeForDeclContext(Scope *S, DeclContext *DC) { |
| 1098 | DeclContext *TargetDC = DC->getPrimaryContext(); |
| 1099 | do { |
| Ted Kremenek | c37877d | 2013-10-08 17:08:03 +0000 | [diff] [blame] | 1100 | if (DeclContext *ScopeDC = S->getEntity()) |
| John McCall | cc14d1f | 2010-08-24 08:50:51 +0000 | [diff] [blame] | 1101 | if (ScopeDC->getPrimaryContext() == TargetDC) |
| 1102 | return S; |
| 1103 | } while ((S = S->getParent())); |
| 1104 | |
| 1105 | return 0; |
| 1106 | } |
| 1107 | |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1108 | static bool isOutOfScopePreviousDeclaration(NamedDecl *, |
| 1109 | DeclContext*, |
| 1110 | ASTContext&); |
| 1111 | |
| 1112 | /// Filters out lookup results that don't fall within the given scope |
| 1113 | /// as determined by isDeclInScope. |
| Richard Smith | 72bcaec | 2013-12-05 04:30:04 +0000 | [diff] [blame] | 1114 | void Sema::FilterLookupForScope(LookupResult &R, DeclContext *Ctx, Scope *S, |
| Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1115 | bool ConsiderLinkage, |
| Richard Smith | 72bcaec | 2013-12-05 04:30:04 +0000 | [diff] [blame] | 1116 | bool AllowInlineNamespace) { |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1117 | LookupResult::Filter F = R.makeFilter(); |
| 1118 | while (F.hasNext()) { |
| 1119 | NamedDecl *D = F.next(); |
| 1120 | |
| Richard Smith | 72bcaec | 2013-12-05 04:30:04 +0000 | [diff] [blame] | 1121 | if (isDeclInScope(D, Ctx, S, AllowInlineNamespace)) |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1122 | continue; |
| 1123 | |
| Richard Smith | 72bcaec | 2013-12-05 04:30:04 +0000 | [diff] [blame] | 1124 | if (ConsiderLinkage && isOutOfScopePreviousDeclaration(D, Ctx, Context)) |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1125 | continue; |
| Richard Smith | 72bcaec | 2013-12-05 04:30:04 +0000 | [diff] [blame] | 1126 | |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1127 | F.erase(); |
| 1128 | } |
| 1129 | |
| 1130 | F.done(); |
| 1131 | } |
| 1132 | |
| 1133 | static bool isUsingDecl(NamedDecl *D) { |
| 1134 | return isa<UsingShadowDecl>(D) || |
| 1135 | isa<UnresolvedUsingTypenameDecl>(D) || |
| 1136 | isa<UnresolvedUsingValueDecl>(D); |
| 1137 | } |
| 1138 | |
| 1139 | /// Removes using shadow declarations from the lookup results. |
| 1140 | static void RemoveUsingDecls(LookupResult &R) { |
| 1141 | LookupResult::Filter F = R.makeFilter(); |
| 1142 | while (F.hasNext()) |
| 1143 | if (isUsingDecl(F.next())) |
| 1144 | F.erase(); |
| 1145 | |
| 1146 | F.done(); |
| 1147 | } |
| 1148 | |
| Argyrios Kyrtzidis | 04c7fa0 | 2010-08-15 10:17:33 +0000 | [diff] [blame] | 1149 | /// \brief Check for this common pattern: |
| 1150 | /// @code |
| 1151 | /// class S { |
| 1152 | /// S(const S&); // DO NOT IMPLEMENT |
| 1153 | /// void operator=(const S&); // DO NOT IMPLEMENT |
| 1154 | /// }; |
| 1155 | /// @endcode |
| 1156 | static bool IsDisallowedCopyOrAssign(const CXXMethodDecl *D) { |
| 1157 | // FIXME: Should check for private access too but access is set after we get |
| 1158 | // the decl here. |
| Alexis Hunt | 4a8ea10 | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 1159 | if (D->doesThisDeclarationHaveABody()) |
| Argyrios Kyrtzidis | 04c7fa0 | 2010-08-15 10:17:33 +0000 | [diff] [blame] | 1160 | return false; |
| 1161 | |
| 1162 | if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(D)) |
| 1163 | return CD->isCopyConstructor(); |
| Douglas Gregor | a1ce1f8 | 2010-09-27 22:06:20 +0000 | [diff] [blame] | 1164 | if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) |
| 1165 | return Method->isCopyAssignmentOperator(); |
| 1166 | return false; |
| Argyrios Kyrtzidis | 04c7fa0 | 2010-08-15 10:17:33 +0000 | [diff] [blame] | 1167 | } |
| 1168 | |
| Rafael Espindola | 0e0d009 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 1169 | // We need this to handle |
| 1170 | // |
| 1171 | // typedef struct { |
| 1172 | // void *foo() { return 0; } |
| 1173 | // } A; |
| 1174 | // |
| 1175 | // When we see foo we don't know if after the typedef we will get 'A' or '*A' |
| 1176 | // for example. If 'A', foo will have external linkage. If we have '*A', |
| Alp Toker | f6a24ce | 2013-12-05 16:25:25 +0000 | [diff] [blame] | 1177 | // foo will have no linkage. Since we can't know until we get to the end |
| Alp Toker | d473363 | 2013-12-05 04:47:09 +0000 | [diff] [blame] | 1178 | // of the typedef, this function finds out if D might have non-external linkage. |
| Rafael Espindola | 0e0d009 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 1179 | // Callers should verify at the end of the TU if it D has external linkage or |
| 1180 | // not. |
| 1181 | bool Sema::mightHaveNonExternalLinkage(const DeclaratorDecl *D) { |
| 1182 | const DeclContext *DC = D->getDeclContext(); |
| 1183 | while (!DC->isTranslationUnit()) { |
| 1184 | if (const RecordDecl *RD = dyn_cast<RecordDecl>(DC)){ |
| 1185 | if (!RD->hasNameForLinkage()) |
| 1186 | return true; |
| 1187 | } |
| 1188 | DC = DC->getParent(); |
| 1189 | } |
| 1190 | |
| Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 1191 | return !D->isExternallyVisible(); |
| Rafael Espindola | 0e0d009 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 1192 | } |
| 1193 | |
| Eli Friedman | 5ef2175 | 2013-09-10 03:05:56 +0000 | [diff] [blame] | 1194 | // FIXME: This needs to be refactored; some other isInMainFile users want |
| 1195 | // these semantics. |
| 1196 | static bool isMainFileLoc(const Sema &S, SourceLocation Loc) { |
| 1197 | if (S.TUKind != TU_Complete) |
| 1198 | return false; |
| 1199 | return S.SourceMgr.isInMainFile(Loc); |
| 1200 | } |
| 1201 | |
| Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 1202 | bool Sema::ShouldWarnIfUnusedFileScopedDecl(const DeclaratorDecl *D) const { |
| 1203 | assert(D); |
| Argyrios Kyrtzidis | 540bc01 | 2010-08-13 18:42:29 +0000 | [diff] [blame] | 1204 | |
| Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 1205 | if (D->isInvalidDecl() || D->isUsed() || D->hasAttr<UnusedAttr>()) |
| 1206 | return false; |
| 1207 | |
| 1208 | // Ignore class templates. |
| Chandler Carruth | 8e66651 | 2011-01-03 19:27:19 +0000 | [diff] [blame] | 1209 | if (D->getDeclContext()->isDependentContext() || |
| 1210 | D->getLexicalDeclContext()->isDependentContext()) |
| Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 1211 | return false; |
| 1212 | |
| Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 1213 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| Argyrios Kyrtzidis | 04c7fa0 | 2010-08-15 10:17:33 +0000 | [diff] [blame] | 1214 | if (FD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation) |
| 1215 | return false; |
| Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 1216 | |
| Argyrios Kyrtzidis | 04c7fa0 | 2010-08-15 10:17:33 +0000 | [diff] [blame] | 1217 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) { |
| 1218 | if (MD->isVirtual() || IsDisallowedCopyOrAssign(MD)) |
| 1219 | return false; |
| 1220 | } else { |
| Eli Friedman | 5ef2175 | 2013-09-10 03:05:56 +0000 | [diff] [blame] | 1221 | // 'static inline' functions are defined in headers; don't warn. |
| 1222 | if (FD->isInlineSpecified() && |
| 1223 | !isMainFileLoc(*this, FD->getLocation())) |
| Argyrios Kyrtzidis | 04c7fa0 | 2010-08-15 10:17:33 +0000 | [diff] [blame] | 1224 | return false; |
| 1225 | } |
| Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 1226 | |
| Alexis Hunt | 4a8ea10 | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 1227 | if (FD->doesThisDeclarationHaveABody() && |
| John McCall | d37d35b | 2010-10-27 01:41:35 +0000 | [diff] [blame] | 1228 | Context.DeclMustBeEmitted(FD)) |
| 1229 | return false; |
| John McCall | d37d35b | 2010-10-27 01:41:35 +0000 | [diff] [blame] | 1230 | } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| Eli Friedman | 5ef2175 | 2013-09-10 03:05:56 +0000 | [diff] [blame] | 1231 | // Constants and utility variables are defined in headers with internal |
| 1232 | // linkage; don't warn. (Unlike functions, there isn't a convenient marker |
| 1233 | // like "inline".) |
| 1234 | if (!isMainFileLoc(*this, VD->getLocation())) |
| 1235 | return false; |
| 1236 | |
| Eli Friedman | 5ef2175 | 2013-09-10 03:05:56 +0000 | [diff] [blame] | 1237 | if (Context.DeclMustBeEmitted(VD)) |
| John McCall | d37d35b | 2010-10-27 01:41:35 +0000 | [diff] [blame] | 1238 | return false; |
| 1239 | |
| Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 1240 | if (VD->isStaticDataMember() && |
| 1241 | VD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation) |
| 1242 | return false; |
| John McCall | d37d35b | 2010-10-27 01:41:35 +0000 | [diff] [blame] | 1243 | } else { |
| 1244 | return false; |
| Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 1245 | } |
| 1246 | |
| John McCall | d37d35b | 2010-10-27 01:41:35 +0000 | [diff] [blame] | 1247 | // Only warn for unused decls internal to the translation unit. |
| Rafael Espindola | 0e0d009 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 1248 | return mightHaveNonExternalLinkage(D); |
| John McCall | d37d35b | 2010-10-27 01:41:35 +0000 | [diff] [blame] | 1249 | } |
| 1250 | |
| 1251 | void Sema::MarkUnusedFileScopedDecl(const DeclaratorDecl *D) { |
| Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 1252 | if (!D) |
| 1253 | return; |
| 1254 | |
| 1255 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 1256 | const FunctionDecl *First = FD->getFirstDecl(); |
| Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 1257 | if (FD != First && ShouldWarnIfUnusedFileScopedDecl(First)) |
| 1258 | return; // First should already be in the vector. |
| 1259 | } |
| 1260 | |
| 1261 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 1262 | const VarDecl *First = VD->getFirstDecl(); |
| Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 1263 | if (VD != First && ShouldWarnIfUnusedFileScopedDecl(First)) |
| 1264 | return; // First should already be in the vector. |
| 1265 | } |
| 1266 | |
| David Blaikie | 3d8edc2 | 2012-05-26 05:35:39 +0000 | [diff] [blame] | 1267 | if (ShouldWarnIfUnusedFileScopedDecl(D)) |
| 1268 | UnusedFileScopedDecls.push_back(D); |
| 1269 | } |
| Argyrios Kyrtzidis | 35672e7 | 2010-08-13 18:42:17 +0000 | [diff] [blame] | 1270 | |
| Anders Carlsson | 2889e0e | 2009-11-07 07:18:14 +0000 | [diff] [blame] | 1271 | static bool ShouldDiagnoseUnusedDecl(const NamedDecl *D) { |
| John McCall | 67da35c | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 1272 | if (D->isInvalidDecl()) |
| 1273 | return false; |
| 1274 | |
| Ted Kremenek | ce0e3f8 | 2014-01-09 20:19:45 +0000 | [diff] [blame] | 1275 | if (D->isReferenced() || D->isUsed() || D->hasAttr<UnusedAttr>() || |
| 1276 | D->hasAttr<ObjCPreciseLifetimeAttr>()) |
| Anders Carlsson | f5dc6fa | 2009-11-07 07:26:56 +0000 | [diff] [blame] | 1277 | return false; |
| John McCall | 67da35c | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 1278 | |
| Chris Lattner | cab02a6 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 1279 | if (isa<LabelDecl>(D)) |
| 1280 | return true; |
| 1281 | |
| John McCall | 67da35c | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 1282 | // White-list anything that isn't a local variable. |
| 1283 | if (!isa<VarDecl>(D) || isa<ParmVarDecl>(D) || isa<ImplicitParamDecl>(D) || |
| 1284 | !D->getDeclContext()->isFunctionOrMethod()) |
| 1285 | return false; |
| 1286 | |
| 1287 | // Types of valid local variables should be complete, so this should succeed. |
| Rafael Espindola | 7c23b08 | 2012-01-06 04:54:01 +0000 | [diff] [blame] | 1288 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| John McCall | cef1582 | 2010-03-31 02:47:45 +0000 | [diff] [blame] | 1289 | |
| 1290 | // White-list anything with an __attribute__((unused)) type. |
| 1291 | QualType Ty = VD->getType(); |
| 1292 | |
| 1293 | // Only look at the outermost level of typedef. |
| Douglas Gregor | 5c65f62 | 2012-09-14 05:10:40 +0000 | [diff] [blame] | 1294 | if (const TypedefType *TT = Ty->getAs<TypedefType>()) { |
| John McCall | cef1582 | 2010-03-31 02:47:45 +0000 | [diff] [blame] | 1295 | if (TT->getDecl()->hasAttr<UnusedAttr>()) |
| 1296 | return false; |
| 1297 | } |
| 1298 | |
| Douglas Gregor | 14f232e | 2010-05-08 23:05:03 +0000 | [diff] [blame] | 1299 | // If we failed to complete the type for some reason, or if the type is |
| 1300 | // dependent, don't diagnose the variable. |
| 1301 | if (Ty->isIncompleteType() || Ty->isDependentType()) |
| Douglas Gregor | 19defcd | 2010-04-27 16:20:13 +0000 | [diff] [blame] | 1302 | return false; |
| 1303 | |
| John McCall | cef1582 | 2010-03-31 02:47:45 +0000 | [diff] [blame] | 1304 | if (const TagType *TT = Ty->getAs<TagType>()) { |
| 1305 | const TagDecl *Tag = TT->getDecl(); |
| 1306 | if (Tag->hasAttr<UnusedAttr>()) |
| 1307 | return false; |
| 1308 | |
| 1309 | if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Tag)) { |
| Lubos Lunak | edc1388 | 2013-07-20 15:05:36 +0000 | [diff] [blame] | 1310 | if (!RD->hasTrivialDestructor() && !RD->hasAttr<WarnUnusedAttr>()) |
| Anders Carlsson | f5dc6fa | 2009-11-07 07:26:56 +0000 | [diff] [blame] | 1311 | return false; |
| Rafael Espindola | 7c23b08 | 2012-01-06 04:54:01 +0000 | [diff] [blame] | 1312 | |
| 1313 | if (const Expr *Init = VD->getInit()) { |
| David Blaikie | a9d4a93 | 2012-10-24 21:29:06 +0000 | [diff] [blame] | 1314 | if (const ExprWithCleanups *Cleanups = dyn_cast<ExprWithCleanups>(Init)) |
| 1315 | Init = Cleanups->getSubExpr(); |
| Rafael Espindola | 7c23b08 | 2012-01-06 04:54:01 +0000 | [diff] [blame] | 1316 | const CXXConstructExpr *Construct = |
| 1317 | dyn_cast<CXXConstructExpr>(Init); |
| 1318 | if (Construct && !Construct->isElidable()) { |
| 1319 | CXXConstructorDecl *CD = Construct->getConstructor(); |
| Lubos Lunak | edc1388 | 2013-07-20 15:05:36 +0000 | [diff] [blame] | 1320 | if (!CD->isTrivial() && !RD->hasAttr<WarnUnusedAttr>()) |
| Rafael Espindola | 7c23b08 | 2012-01-06 04:54:01 +0000 | [diff] [blame] | 1321 | return false; |
| 1322 | } |
| 1323 | } |
| Anders Carlsson | f5dc6fa | 2009-11-07 07:26:56 +0000 | [diff] [blame] | 1324 | } |
| 1325 | } |
| John McCall | cef1582 | 2010-03-31 02:47:45 +0000 | [diff] [blame] | 1326 | |
| 1327 | // TODO: __attribute__((unused)) templates? |
| Anders Carlsson | f5dc6fa | 2009-11-07 07:26:56 +0000 | [diff] [blame] | 1328 | } |
| 1329 | |
| John McCall | 67da35c | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 1330 | return true; |
| Anders Carlsson | 2889e0e | 2009-11-07 07:18:14 +0000 | [diff] [blame] | 1331 | } |
| 1332 | |
| Anna Zaks | 964f4c6 | 2011-07-28 20:52:06 +0000 | [diff] [blame] | 1333 | static void GenerateFixForUnusedDecl(const NamedDecl *D, ASTContext &Ctx, |
| 1334 | FixItHint &Hint) { |
| 1335 | if (isa<LabelDecl>(D)) { |
| 1336 | SourceLocation AfterColon = Lexer::findLocationAfterToken(D->getLocEnd(), |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1337 | tok::colon, Ctx.getSourceManager(), Ctx.getLangOpts(), true); |
| Anna Zaks | 964f4c6 | 2011-07-28 20:52:06 +0000 | [diff] [blame] | 1338 | if (AfterColon.isInvalid()) |
| 1339 | return; |
| 1340 | Hint = FixItHint::CreateRemoval(CharSourceRange:: |
| 1341 | getCharRange(D->getLocStart(), AfterColon)); |
| 1342 | } |
| 1343 | return; |
| 1344 | } |
| 1345 | |
| Chris Lattner | ebb5c6c | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1346 | /// DiagnoseUnusedDecl - Emit warnings about declarations that are not used |
| 1347 | /// unless they are marked attr(unused). |
| Douglas Gregor | 14f232e | 2010-05-08 23:05:03 +0000 | [diff] [blame] | 1348 | void Sema::DiagnoseUnusedDecl(const NamedDecl *D) { |
| Anna Zaks | 964f4c6 | 2011-07-28 20:52:06 +0000 | [diff] [blame] | 1349 | FixItHint Hint; |
| Douglas Gregor | 14f232e | 2010-05-08 23:05:03 +0000 | [diff] [blame] | 1350 | if (!ShouldDiagnoseUnusedDecl(D)) |
| 1351 | return; |
| 1352 | |
| Anna Zaks | 964f4c6 | 2011-07-28 20:52:06 +0000 | [diff] [blame] | 1353 | GenerateFixForUnusedDecl(D, Context, Hint); |
| 1354 | |
| Chris Lattner | cab02a6 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 1355 | unsigned DiagID; |
| Douglas Gregor | 14f232e | 2010-05-08 23:05:03 +0000 | [diff] [blame] | 1356 | if (isa<VarDecl>(D) && cast<VarDecl>(D)->isExceptionVariable()) |
| Chris Lattner | cab02a6 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 1357 | DiagID = diag::warn_unused_exception_param; |
| 1358 | else if (isa<LabelDecl>(D)) |
| 1359 | DiagID = diag::warn_unused_label; |
| Douglas Gregor | 14f232e | 2010-05-08 23:05:03 +0000 | [diff] [blame] | 1360 | else |
| Chris Lattner | cab02a6 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 1361 | DiagID = diag::warn_unused_variable; |
| 1362 | |
| Anna Zaks | 964f4c6 | 2011-07-28 20:52:06 +0000 | [diff] [blame] | 1363 | Diag(D->getLocation(), DiagID) << D->getDeclName() << Hint; |
| Douglas Gregor | 14f232e | 2010-05-08 23:05:03 +0000 | [diff] [blame] | 1364 | } |
| 1365 | |
| Chris Lattner | ebb5c6c | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1366 | static void CheckPoppedLabel(LabelDecl *L, Sema &S) { |
| 1367 | // Verify that we have no forward references left. If so, there was a goto |
| 1368 | // or address of a label taken, but no definition of it. Label fwd |
| 1369 | // definitions are indicated with a null substmt. |
| 1370 | if (L->getStmt() == 0) |
| 1371 | S.Diag(L->getLocation(), diag::err_undeclared_label_use) <<L->getDeclName(); |
| 1372 | } |
| 1373 | |
| Steve Naroff | c62adb6 | 2007-10-09 22:01:59 +0000 | [diff] [blame] | 1374 | void Sema::ActOnPopScope(SourceLocation Loc, Scope *S) { |
| Chris Lattner | 1a76a3c | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 1375 | if (S->decl_empty()) return; |
| Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 1376 | assert((S->getFlags() & (Scope::DeclScope | Scope::TemplateParamScope)) && |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1377 | "Scope shouldn't contain decls!"); |
| Argyrios Kyrtzidis | fa8e15b | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 1378 | |
| Chris Lattner | 302b4be | 2006-11-19 02:31:38 +0000 | [diff] [blame] | 1379 | for (Scope::decl_iterator I = S->decl_begin(), E = S->decl_end(); |
| 1380 | I != E; ++I) { |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1381 | Decl *TmpD = (*I); |
| Steve Naroff | 9324db1 | 2007-09-13 18:10:37 +0000 | [diff] [blame] | 1382 | assert(TmpD && "This decl didn't get pushed??"); |
| Argyrios Kyrtzidis | 406fb23 | 2008-06-10 01:32:09 +0000 | [diff] [blame] | 1383 | |
| Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1384 | assert(isa<NamedDecl>(TmpD) && "Decl isn't NamedDecl?"); |
| 1385 | NamedDecl *D = cast<NamedDecl>(TmpD); |
| Argyrios Kyrtzidis | 406fb23 | 2008-06-10 01:32:09 +0000 | [diff] [blame] | 1386 | |
| Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1387 | if (!D->getDeclName()) continue; |
| Chris Lattner | c5c95b5 | 2008-04-11 07:00:53 +0000 | [diff] [blame] | 1388 | |
| Douglas Gregor | 3beaf9b | 2009-10-08 21:35:42 +0000 | [diff] [blame] | 1389 | // Diagnose unused variables in this scope. |
| Matt Beaumont-Gay | 8f51121 | 2013-03-28 21:46:45 +0000 | [diff] [blame] | 1390 | if (!S->hasUnrecoverableErrorOccurred()) |
| Douglas Gregor | 14f232e | 2010-05-08 23:05:03 +0000 | [diff] [blame] | 1391 | DiagnoseUnusedDecl(D); |
| 1392 | |
| Chris Lattner | ebb5c6c | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1393 | // If this was a forward reference to a label, verify it was defined. |
| 1394 | if (LabelDecl *LD = dyn_cast<LabelDecl>(D)) |
| 1395 | CheckPoppedLabel(LD, *this); |
| 1396 | |
| Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1397 | // Remove this name from our lexical scope. |
| 1398 | IdResolver.RemoveDecl(D); |
| Chris Lattner | 302b4be | 2006-11-19 02:31:38 +0000 | [diff] [blame] | 1399 | } |
| 1400 | } |
| 1401 | |
| Douglas Gregor | 1c28331 | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 1402 | /// \brief Look for an Objective-C class in the translation unit. |
| 1403 | /// |
| 1404 | /// \param Id The name of the Objective-C class we're looking for. If |
| 1405 | /// typo-correction fixes this name, the Id will be updated |
| 1406 | /// to the fixed name. |
| 1407 | /// |
| 1408 | /// \param IdLoc The location of the name in the translation unit. |
| 1409 | /// |
| James Dennett | 4172512 | 2012-06-22 10:16:05 +0000 | [diff] [blame] | 1410 | /// \param DoTypoCorrection If true, this routine will attempt typo correction |
| Douglas Gregor | 1c28331 | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 1411 | /// if there is no class with the given name. |
| 1412 | /// |
| 1413 | /// \returns The declaration of the named Objective-C class, or NULL if the |
| 1414 | /// class could not be found. |
| 1415 | ObjCInterfaceDecl *Sema::getObjCInterfaceDecl(IdentifierInfo *&Id, |
| 1416 | SourceLocation IdLoc, |
| Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1417 | bool DoTypoCorrection) { |
| Douglas Gregor | 1c28331 | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 1418 | // The third "scope" argument is 0 since we aren't enabling lazy built-in |
| 1419 | // creation from this context. |
| 1420 | NamedDecl *IDecl = LookupSingleName(TUScope, Id, IdLoc, LookupOrdinaryName); |
| 1421 | |
| Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1422 | if (!IDecl && DoTypoCorrection) { |
| Douglas Gregor | 1c28331 | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 1423 | // Perform typo correction at the given location, but only if we |
| 1424 | // find an Objective-C class name. |
| Kaelyn Uhrain | b137840 | 2012-01-18 21:41:41 +0000 | [diff] [blame] | 1425 | DeclFilterCCC<ObjCInterfaceDecl> Validator; |
| 1426 | if (TypoCorrection C = CorrectTypo(DeclarationNameInfo(Id, IdLoc), |
| 1427 | LookupOrdinaryName, TUScope, NULL, |
| Kaelyn Uhrain | 4e8942c | 2012-01-31 23:49:25 +0000 | [diff] [blame] | 1428 | Validator)) { |
| Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 1429 | diagnoseTypo(C, PDiag(diag::err_undef_interface_suggest) << Id); |
| Kaelyn Uhrain | b137840 | 2012-01-18 21:41:41 +0000 | [diff] [blame] | 1430 | IDecl = C.getCorrectionDeclAs<ObjCInterfaceDecl>(); |
| Douglas Gregor | 1c28331 | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 1431 | Id = IDecl->getIdentifier(); |
| 1432 | } |
| 1433 | } |
| Fariborz Jahanian | 4f8cb1e | 2012-01-12 00:18:35 +0000 | [diff] [blame] | 1434 | ObjCInterfaceDecl *Def = dyn_cast_or_null<ObjCInterfaceDecl>(IDecl); |
| 1435 | // This routine must always return a class definition, if any. |
| 1436 | if (Def && Def->getDefinition()) |
| 1437 | Def = Def->getDefinition(); |
| 1438 | return Def; |
| Douglas Gregor | 1c28331 | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 1439 | } |
| 1440 | |
| Douglas Gregor | 45a33ec | 2009-01-12 18:45:55 +0000 | [diff] [blame] | 1441 | /// getNonFieldDeclScope - Retrieves the innermost scope, starting |
| 1442 | /// from S, where a non-field would be declared. This routine copes |
| 1443 | /// with the difference between C and C++ scoping rules in structs and |
| 1444 | /// unions. For example, the following code is well-formed in C but |
| 1445 | /// ill-formed in C++: |
| 1446 | /// @code |
| 1447 | /// struct S6 { |
| 1448 | /// enum { BAR } e; |
| 1449 | /// }; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1450 | /// |
| Douglas Gregor | 45a33ec | 2009-01-12 18:45:55 +0000 | [diff] [blame] | 1451 | /// void test_S6() { |
| 1452 | /// struct S6 a; |
| 1453 | /// a.e = BAR; |
| 1454 | /// } |
| 1455 | /// @endcode |
| 1456 | /// For the declaration of BAR, this routine will return a different |
| 1457 | /// scope. The scope S will be the scope of the unnamed enumeration |
| 1458 | /// within S6. In C++, this routine will return the scope associated |
| 1459 | /// with S6, because the enumeration's scope is a transparent |
| 1460 | /// context but structures can contain non-field names. In C, this |
| 1461 | /// routine will return the translation unit scope, since the |
| 1462 | /// enumeration's scope is a transparent context and structures cannot |
| 1463 | /// contain non-field names. |
| 1464 | Scope *Sema::getNonFieldDeclScope(Scope *S) { |
| 1465 | while (((S->getFlags() & Scope::DeclScope) == 0) || |
| Ted Kremenek | c37877d | 2013-10-08 17:08:03 +0000 | [diff] [blame] | 1466 | (S->getEntity() && S->getEntity()->isTransparentContext()) || |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1467 | (S->isClassScope() && !getLangOpts().CPlusPlus)) |
| Douglas Gregor | 45a33ec | 2009-01-12 18:45:55 +0000 | [diff] [blame] | 1468 | S = S->getParent(); |
| 1469 | return S; |
| 1470 | } |
| 1471 | |
| Fariborz Jahanian | cb6c867 | 2013-01-04 18:45:40 +0000 | [diff] [blame] | 1472 | /// \brief Looks up the declaration of "struct objc_super" and |
| 1473 | /// saves it for later use in building builtin declaration of |
| 1474 | /// objc_msgSendSuper and objc_msgSendSuper_stret. If no such |
| 1475 | /// pre-existing declaration exists no action takes place. |
| 1476 | static void LookupPredefedObjCSuperType(Sema &ThisSema, Scope *S, |
| 1477 | IdentifierInfo *II) { |
| 1478 | if (!II->isStr("objc_msgSendSuper")) |
| 1479 | return; |
| 1480 | ASTContext &Context = ThisSema.Context; |
| 1481 | |
| 1482 | LookupResult Result(ThisSema, &Context.Idents.get("objc_super"), |
| 1483 | SourceLocation(), Sema::LookupTagName); |
| 1484 | ThisSema.LookupName(Result, S); |
| 1485 | if (Result.getResultKind() == LookupResult::Found) |
| 1486 | if (const TagDecl *TD = Result.getAsSingle<TagDecl>()) |
| 1487 | Context.setObjCSuperType(Context.getTagDeclType(TD)); |
| 1488 | } |
| 1489 | |
| Douglas Gregor | b9063fc | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 1490 | /// LazilyCreateBuiltin - The specified Builtin-ID was first used at |
| 1491 | /// file scope. lazily create a decl for it. ForRedeclaration is true |
| 1492 | /// if we're creating this built-in in anticipation of redeclaring the |
| 1493 | /// built-in. |
| Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1494 | NamedDecl *Sema::LazilyCreateBuiltin(IdentifierInfo *II, unsigned bid, |
| Douglas Gregor | b9063fc | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 1495 | Scope *S, bool ForRedeclaration, |
| 1496 | SourceLocation Loc) { |
| Fariborz Jahanian | cb6c867 | 2013-01-04 18:45:40 +0000 | [diff] [blame] | 1497 | LookupPredefedObjCSuperType(*this, S, II); |
| 1498 | |
| Chris Lattner | 9561a0b | 2007-01-28 08:20:04 +0000 | [diff] [blame] | 1499 | Builtin::ID BID = (Builtin::ID)bid; |
| 1500 | |
| Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 1501 | ASTContext::GetBuiltinTypeError Error; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1502 | QualType R = Context.GetBuiltinType(BID, Error); |
| Douglas Gregor | 538c3d8 | 2009-02-14 01:52:53 +0000 | [diff] [blame] | 1503 | switch (Error) { |
| Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 1504 | case ASTContext::GE_None: |
| Douglas Gregor | 538c3d8 | 2009-02-14 01:52:53 +0000 | [diff] [blame] | 1505 | // Okay |
| 1506 | break; |
| 1507 | |
| Mike Stump | 93246cc | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 1508 | case ASTContext::GE_Missing_stdio: |
| Douglas Gregor | 538c3d8 | 2009-02-14 01:52:53 +0000 | [diff] [blame] | 1509 | if (ForRedeclaration) |
| Douglas Gregor | bfe022c | 2011-01-03 09:37:44 +0000 | [diff] [blame] | 1510 | Diag(Loc, diag::warn_implicit_decl_requires_stdio) |
| Douglas Gregor | 538c3d8 | 2009-02-14 01:52:53 +0000 | [diff] [blame] | 1511 | << Context.BuiltinInfo.GetName(BID); |
| 1512 | return 0; |
| Mike Stump | a4de80b | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 1513 | |
| Mike Stump | 93246cc | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 1514 | case ASTContext::GE_Missing_setjmp: |
| Mike Stump | a4de80b | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 1515 | if (ForRedeclaration) |
| Douglas Gregor | bfe022c | 2011-01-03 09:37:44 +0000 | [diff] [blame] | 1516 | Diag(Loc, diag::warn_implicit_decl_requires_setjmp) |
| Mike Stump | a4de80b | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 1517 | << Context.BuiltinInfo.GetName(BID); |
| 1518 | return 0; |
| Rafael Espindola | 6cfa82b | 2011-11-13 21:51:09 +0000 | [diff] [blame] | 1519 | |
| 1520 | case ASTContext::GE_Missing_ucontext: |
| 1521 | if (ForRedeclaration) |
| 1522 | Diag(Loc, diag::warn_implicit_decl_requires_ucontext) |
| 1523 | << Context.BuiltinInfo.GetName(BID); |
| 1524 | return 0; |
| Douglas Gregor | 538c3d8 | 2009-02-14 01:52:53 +0000 | [diff] [blame] | 1525 | } |
| Douglas Gregor | b9063fc | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 1526 | |
| 1527 | if (!ForRedeclaration && Context.BuiltinInfo.isPredefinedLibFunction(BID)) { |
| 1528 | Diag(Loc, diag::ext_implicit_lib_function_decl) |
| 1529 | << Context.BuiltinInfo.GetName(BID) |
| 1530 | << R; |
| Douglas Gregor | 9eebd97 | 2009-02-16 21:58:21 +0000 | [diff] [blame] | 1531 | if (Context.BuiltinInfo.getHeaderName(BID) && |
| Argyrios Kyrtzidis | 1cb0de1 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 1532 | Diags.getDiagnosticLevel(diag::ext_implicit_lib_function_decl, Loc) |
| David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 1533 | != DiagnosticsEngine::Ignored) |
| Douglas Gregor | b9063fc | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 1534 | Diag(Loc, diag::note_please_include_header) |
| 1535 | << Context.BuiltinInfo.getHeaderName(BID) |
| 1536 | << Context.BuiltinInfo.GetName(BID); |
| 1537 | } |
| 1538 | |
| Warren Hunt | 445d83e | 2013-11-01 23:46:51 +0000 | [diff] [blame] | 1539 | DeclContext *Parent = Context.getTranslationUnitDecl(); |
| 1540 | if (getLangOpts().CPlusPlus) { |
| 1541 | LinkageSpecDecl *CLinkageDecl = |
| 1542 | LinkageSpecDecl::Create(Context, Parent, Loc, Loc, |
| 1543 | LinkageSpecDecl::lang_c, false); |
| Enea Zaffanella | d843092 | 2013-11-20 15:41:05 +0000 | [diff] [blame] | 1544 | CLinkageDecl->setImplicit(); |
| Warren Hunt | 445d83e | 2013-11-01 23:46:51 +0000 | [diff] [blame] | 1545 | Parent->addDecl(CLinkageDecl); |
| 1546 | Parent = CLinkageDecl; |
| 1547 | } |
| 1548 | |
| Argyrios Kyrtzidis | 6d05303 | 2008-04-17 14:47:13 +0000 | [diff] [blame] | 1549 | FunctionDecl *New = FunctionDecl::Create(Context, |
| Warren Hunt | 445d83e | 2013-11-01 23:46:51 +0000 | [diff] [blame] | 1550 | Parent, |
| Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1551 | Loc, Loc, II, R, /*TInfo=*/0, |
| John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 1552 | SC_Extern, |
| Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 1553 | false, |
| Douglas Gregor | 739ef0c | 2009-02-25 16:33:18 +0000 | [diff] [blame] | 1554 | /*hasPrototype=*/true); |
| Douglas Gregor | b9063fc | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 1555 | New->setImplicit(); |
| 1556 | |
| Chris Lattner | 4dd2710 | 2008-05-05 22:18:14 +0000 | [diff] [blame] | 1557 | // Create Decl objects for each parameter, adding them to the |
| 1558 | // FunctionDecl. |
| John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1559 | if (const FunctionProtoType *FT = dyn_cast<FunctionProtoType>(R)) { |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1560 | SmallVector<ParmVarDecl*, 16> Params; |
| Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 1561 | for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) { |
| John McCall | 8fb0d9d | 2011-05-01 22:35:37 +0000 | [diff] [blame] | 1562 | ParmVarDecl *parm = |
| Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 1563 | ParmVarDecl::Create(Context, New, SourceLocation(), SourceLocation(), |
| 1564 | 0, FT->getParamType(i), /*TInfo=*/0, SC_None, 0); |
| John McCall | 8fb0d9d | 2011-05-01 22:35:37 +0000 | [diff] [blame] | 1565 | parm->setScopeInfo(0, i); |
| 1566 | Params.push_back(parm); |
| 1567 | } |
| David Blaikie | 9c70e04 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 1568 | New->setParams(Params); |
| Chris Lattner | 4dd2710 | 2008-05-05 22:18:14 +0000 | [diff] [blame] | 1569 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1570 | |
| 1571 | AddKnownFunctionAttributes(New); |
| Warren Hunt | 445d83e | 2013-11-01 23:46:51 +0000 | [diff] [blame] | 1572 | RegisterLocallyScopedExternCDecl(New, S); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1573 | |
| Chris Lattner | c5c95b5 | 2008-04-11 07:00:53 +0000 | [diff] [blame] | 1574 | // TUScope is the translation-unit scope to insert this function into. |
| Douglas Gregor | c72e645 | 2009-01-09 18:51:29 +0000 | [diff] [blame] | 1575 | // FIXME: This is hideous. We need to teach PushOnScopeChains to |
| 1576 | // relate Scopes to DeclContexts, and probably eliminate CurContext |
| 1577 | // entirely, but we're not there yet. |
| 1578 | DeclContext *SavedContext = CurContext; |
| Warren Hunt | 445d83e | 2013-11-01 23:46:51 +0000 | [diff] [blame] | 1579 | CurContext = Parent; |
| Argyrios Kyrtzidis | fa8e15b | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 1580 | PushOnScopeChains(New, TUScope); |
| Douglas Gregor | c72e645 | 2009-01-09 18:51:29 +0000 | [diff] [blame] | 1581 | CurContext = SavedContext; |
| Chris Lattner | 9561a0b | 2007-01-28 08:20:04 +0000 | [diff] [blame] | 1582 | return New; |
| 1583 | } |
| 1584 | |
| Douglas Gregor | 3552dab | 2013-01-09 00:47:56 +0000 | [diff] [blame] | 1585 | /// \brief Filter out any previous declarations that the given declaration |
| 1586 | /// should not consider because they are not permitted to conflict, e.g., |
| 1587 | /// because they come from hidden sub-modules and do not refer to the same |
| 1588 | /// entity. |
| 1589 | static void filterNonConflictingPreviousDecls(ASTContext &context, |
| 1590 | NamedDecl *decl, |
| 1591 | LookupResult &previous){ |
| 1592 | // This is only interesting when modules are enabled. |
| 1593 | if (!context.getLangOpts().Modules) |
| 1594 | return; |
| 1595 | |
| 1596 | // Empty sets are uninteresting. |
| 1597 | if (previous.empty()) |
| 1598 | return; |
| 1599 | |
| Douglas Gregor | 3552dab | 2013-01-09 00:47:56 +0000 | [diff] [blame] | 1600 | LookupResult::Filter filter = previous.makeFilter(); |
| 1601 | while (filter.hasNext()) { |
| 1602 | NamedDecl *old = filter.next(); |
| 1603 | |
| 1604 | // Non-hidden declarations are never ignored. |
| 1605 | if (!old->isHidden()) |
| 1606 | continue; |
| 1607 | |
| Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 1608 | if (!old->isExternallyVisible()) |
| Douglas Gregor | 3552dab | 2013-01-09 00:47:56 +0000 | [diff] [blame] | 1609 | filter.erase(); |
| 1610 | } |
| 1611 | |
| 1612 | filter.done(); |
| 1613 | } |
| 1614 | |
| Rafael Espindola | cde2c8f | 2011-12-26 22:42:47 +0000 | [diff] [blame] | 1615 | bool Sema::isIncompatibleTypedef(TypeDecl *Old, TypedefNameDecl *New) { |
| 1616 | QualType OldType; |
| 1617 | if (TypedefNameDecl *OldTypedef = dyn_cast<TypedefNameDecl>(Old)) |
| 1618 | OldType = OldTypedef->getUnderlyingType(); |
| 1619 | else |
| 1620 | OldType = Context.getTypeDeclType(Old); |
| 1621 | QualType NewType = New->getUnderlyingType(); |
| 1622 | |
| Douglas Gregor | aab3698 | 2012-01-11 22:33:48 +0000 | [diff] [blame] | 1623 | if (NewType->isVariablyModifiedType()) { |
| 1624 | // Must not redefine a typedef with a variably-modified type. |
| 1625 | int Kind = isa<TypeAliasDecl>(Old) ? 1 : 0; |
| 1626 | Diag(New->getLocation(), diag::err_redefinition_variably_modified_typedef) |
| 1627 | << Kind << NewType; |
| 1628 | if (Old->getLocation().isValid()) |
| 1629 | Diag(Old->getLocation(), diag::note_previous_definition); |
| 1630 | New->setInvalidDecl(); |
| 1631 | return true; |
| 1632 | } |
| 1633 | |
| Rafael Espindola | cde2c8f | 2011-12-26 22:42:47 +0000 | [diff] [blame] | 1634 | if (OldType != NewType && |
| 1635 | !OldType->isDependentType() && |
| 1636 | !NewType->isDependentType() && |
| Douglas Gregor | aab3698 | 2012-01-11 22:33:48 +0000 | [diff] [blame] | 1637 | !Context.hasSameType(OldType, NewType)) { |
| Rafael Espindola | cde2c8f | 2011-12-26 22:42:47 +0000 | [diff] [blame] | 1638 | int Kind = isa<TypeAliasDecl>(Old) ? 1 : 0; |
| 1639 | Diag(New->getLocation(), diag::err_redefinition_different_typedef) |
| 1640 | << Kind << NewType << OldType; |
| 1641 | if (Old->getLocation().isValid()) |
| 1642 | Diag(Old->getLocation(), diag::note_previous_definition); |
| 1643 | New->setInvalidDecl(); |
| 1644 | return true; |
| 1645 | } |
| 1646 | return false; |
| 1647 | } |
| 1648 | |
| Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1649 | /// MergeTypedefNameDecl - We just parsed a typedef 'New' which has the |
| Douglas Gregor | 75a45ba | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 1650 | /// same name and scope as a previous declaration 'Old'. Figure out |
| 1651 | /// how to resolve this situation, merging decls or emitting |
| Chris Lattner | f6d1c9c | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 1652 | /// diagnostics as appropriate. If there was an error, set New to be invalid. |
| Chris Lattner | 01564d9 | 2007-01-27 19:27:06 +0000 | [diff] [blame] | 1653 | /// |
| Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1654 | void Sema::MergeTypedefNameDecl(TypedefNameDecl *New, LookupResult &OldDecls) { |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1655 | // If the new decl is known invalid already, don't bother doing any |
| 1656 | // merging checks. |
| 1657 | if (New->isInvalidDecl()) return; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1658 | |
| Steve Naroff | 44cfcb6 | 2008-09-09 14:32:20 +0000 | [diff] [blame] | 1659 | // Allow multiple definitions for ObjC built-in typedefs. |
| 1660 | // FIXME: Verify the underlying types are equivalent! |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1661 | if (getLangOpts().ObjC1) { |
| Chris Lattner | 66e3281 | 2008-11-20 05:41:43 +0000 | [diff] [blame] | 1662 | const IdentifierInfo *TypeID = New->getIdentifier(); |
| 1663 | switch (TypeID->getLength()) { |
| 1664 | default: break; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1665 | case 2: |
| Fariborz Jahanian | 16d71bb | 2012-05-14 22:48:56 +0000 | [diff] [blame] | 1666 | { |
| 1667 | if (!TypeID->isStr("id")) |
| 1668 | break; |
| 1669 | QualType T = New->getUnderlyingType(); |
| 1670 | if (!T->isPointerType()) |
| 1671 | break; |
| 1672 | if (!T->isVoidPointerType()) { |
| 1673 | QualType PT = T->getAs<PointerType>()->getPointeeType(); |
| 1674 | if (!PT->isStructureType()) |
| 1675 | break; |
| 1676 | } |
| 1677 | Context.setObjCIdRedefinitionType(T); |
| 1678 | // Install the built-in type for 'id', ignoring the current definition. |
| 1679 | New->setTypeForDecl(Context.getObjCIdType().getTypePtr()); |
| 1680 | return; |
| 1681 | } |
| Chris Lattner | 66e3281 | 2008-11-20 05:41:43 +0000 | [diff] [blame] | 1682 | case 5: |
| 1683 | if (!TypeID->isStr("Class")) |
| 1684 | break; |
| Douglas Gregor | 9767347 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 1685 | Context.setObjCClassRedefinitionType(New->getUnderlyingType()); |
| Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1686 | // Install the built-in type for 'Class', ignoring the current definition. |
| 1687 | New->setTypeForDecl(Context.getObjCClassType().getTypePtr()); |
| Chris Lattner | f6d1c9c | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 1688 | return; |
| Chris Lattner | 66e3281 | 2008-11-20 05:41:43 +0000 | [diff] [blame] | 1689 | case 3: |
| 1690 | if (!TypeID->isStr("SEL")) |
| 1691 | break; |
| Douglas Gregor | 9767347 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 1692 | Context.setObjCSelRedefinitionType(New->getUnderlyingType()); |
| Fariborz Jahanian | 252ba5f | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 1693 | // Install the built-in type for 'SEL', ignoring the current definition. |
| 1694 | New->setTypeForDecl(Context.getObjCSelType().getTypePtr()); |
| Chris Lattner | f6d1c9c | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 1695 | return; |
| Steve Naroff | 44cfcb6 | 2008-09-09 14:32:20 +0000 | [diff] [blame] | 1696 | } |
| 1697 | // Fall through - the typedef name was not a builtin type. |
| 1698 | } |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1699 | |
| Douglas Gregor | fb03466 | 2009-01-28 17:15:10 +0000 | [diff] [blame] | 1700 | // Verify the old decl was also a type. |
| John McCall | 91f1a02 | 2009-12-30 00:31:22 +0000 | [diff] [blame] | 1701 | TypeDecl *Old = OldDecls.getAsSingle<TypeDecl>(); |
| 1702 | if (!Old) { |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1703 | Diag(New->getLocation(), diag::err_redefinition_different_kind) |
| Chris Lattner | e3d20d9 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 1704 | << New->getDeclName(); |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1705 | |
| 1706 | NamedDecl *OldD = OldDecls.getRepresentativeDecl(); |
| Chris Lattner | f6d1c9c | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 1707 | if (OldD->getLocation().isValid()) |
| Fariborz Jahanian | 1778f4b | 2009-01-16 19:58:32 +0000 | [diff] [blame] | 1708 | Diag(OldD->getLocation(), diag::note_previous_definition); |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1709 | |
| Chris Lattner | f6d1c9c | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 1710 | return New->setInvalidDecl(); |
| Chris Lattner | c511efb | 2007-01-27 19:32:14 +0000 | [diff] [blame] | 1711 | } |
| Douglas Gregor | fb03466 | 2009-01-28 17:15:10 +0000 | [diff] [blame] | 1712 | |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1713 | // If the old declaration is invalid, just give up here. |
| 1714 | if (Old->isInvalidDecl()) |
| 1715 | return New->setInvalidDecl(); |
| 1716 | |
| Chris Lattner | f9c49e5 | 2008-07-25 18:44:27 +0000 | [diff] [blame] | 1717 | // If the typedef types are not identical, reject them in all languages and |
| 1718 | // with any extensions enabled. |
| Rafael Espindola | cde2c8f | 2011-12-26 22:42:47 +0000 | [diff] [blame] | 1719 | if (isIncompatibleTypedef(Old, New)) |
| 1720 | return; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1721 | |
| Justin Bogner | 84ff5ee | 2013-10-08 00:19:09 +0000 | [diff] [blame] | 1722 | // The types match. Link up the redeclaration chain and merge attributes if |
| 1723 | // the old declaration was a typedef. |
| 1724 | if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Old)) { |
| Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 1725 | New->setPreviousDecl(Typedef); |
| Justin Bogner | 84ff5ee | 2013-10-08 00:19:09 +0000 | [diff] [blame] | 1726 | mergeDeclAttributes(New, Old); |
| 1727 | } |
| Eli Friedman | e7b8aa9 | 2013-07-16 02:07:49 +0000 | [diff] [blame] | 1728 | |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1729 | if (getLangOpts().MicrosoftExt) |
| Chris Lattner | f6d1c9c | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 1730 | return; |
| Eli Friedman | 61b529f | 2008-06-11 06:20:39 +0000 | [diff] [blame] | 1731 | |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1732 | if (getLangOpts().CPlusPlus) { |
| Douglas Gregor | 9dd13ab | 2010-01-11 21:54:40 +0000 | [diff] [blame] | 1733 | // C++ [dcl.typedef]p2: |
| 1734 | // In a given non-class scope, a typedef specifier can be used to |
| 1735 | // redefine the name of any type declared in that scope to refer |
| 1736 | // to the type to which it already refers. |
| Chris Lattner | 2581fc3 | 2009-04-17 22:04:20 +0000 | [diff] [blame] | 1737 | if (!isa<CXXRecordDecl>(CurContext)) |
| Chris Lattner | f6d1c9c | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 1738 | return; |
| Douglas Gregor | 9dd13ab | 2010-01-11 21:54:40 +0000 | [diff] [blame] | 1739 | |
| 1740 | // C++0x [dcl.typedef]p4: |
| 1741 | // In a given class scope, a typedef specifier can be used to redefine |
| 1742 | // any class-name declared in that scope that is not also a typedef-name |
| 1743 | // to refer to the type to which it already refers. |
| 1744 | // |
| 1745 | // This wording came in via DR424, which was a correction to the |
| 1746 | // wording in DR56, which accidentally banned code like: |
| 1747 | // |
| 1748 | // struct S { |
| 1749 | // typedef struct A { } A; |
| 1750 | // }; |
| 1751 | // |
| 1752 | // in the C++03 standard. We implement the C++0x semantics, which |
| 1753 | // allow the above but disallow |
| 1754 | // |
| 1755 | // struct S { |
| 1756 | // typedef int I; |
| 1757 | // typedef int I; |
| 1758 | // }; |
| 1759 | // |
| 1760 | // since that was the intent of DR56. |
| Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1761 | if (!isa<TypedefNameDecl>(Old)) |
| Douglas Gregor | 9dd13ab | 2010-01-11 21:54:40 +0000 | [diff] [blame] | 1762 | return; |
| 1763 | |
| Chris Lattner | 2581fc3 | 2009-04-17 22:04:20 +0000 | [diff] [blame] | 1764 | Diag(New->getLocation(), diag::err_redefinition) |
| 1765 | << New->getDeclName(); |
| 1766 | Diag(Old->getLocation(), diag::note_previous_definition); |
| Chris Lattner | f6d1c9c | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 1767 | return New->setInvalidDecl(); |
| Daniel Dunbar | 84b70f7 | 2008-09-12 18:10:20 +0000 | [diff] [blame] | 1768 | } |
| Eli Friedman | 61b529f | 2008-06-11 06:20:39 +0000 | [diff] [blame] | 1769 | |
| Douglas Gregor | 7363fb0 | 2012-01-11 04:25:01 +0000 | [diff] [blame] | 1770 | // Modules always permit redefinition of typedefs, as does C11. |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1771 | if (getLangOpts().Modules || getLangOpts().C11) |
| Douglas Gregor | dbd93bf | 2012-01-09 15:36:04 +0000 | [diff] [blame] | 1772 | return; |
| 1773 | |
| Chris Lattner | 2581fc3 | 2009-04-17 22:04:20 +0000 | [diff] [blame] | 1774 | // If we have a redefinition of a typedef in C, emit a warning. This warning |
| 1775 | // is normally mapped to an error, but can be controlled with |
| Eli Friedman | 319ce95 | 2009-06-04 23:03:07 +0000 | [diff] [blame] | 1776 | // -Wtypedef-redefinition. If either the original or the redefinition is |
| 1777 | // in a system header, don't emit this for compatibility with GCC. |
| Chris Lattner | 30d0cfd | 2010-03-01 20:59:53 +0000 | [diff] [blame] | 1778 | if (getDiagnostics().getSuppressSystemWarnings() && |
| Eli Friedman | 319ce95 | 2009-06-04 23:03:07 +0000 | [diff] [blame] | 1779 | (Context.getSourceManager().isInSystemHeader(Old->getLocation()) || |
| 1780 | Context.getSourceManager().isInSystemHeader(New->getLocation()))) |
| Chris Lattner | 9a84589 | 2009-04-27 01:46:12 +0000 | [diff] [blame] | 1781 | return; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1782 | |
| Chris Lattner | 2581fc3 | 2009-04-17 22:04:20 +0000 | [diff] [blame] | 1783 | Diag(New->getLocation(), diag::warn_redefinition_of_typedef) |
| 1784 | << New->getDeclName(); |
| Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 1785 | Diag(Old->getLocation(), diag::note_previous_definition); |
| Chris Lattner | f6d1c9c | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 1786 | return; |
| Chris Lattner | 01564d9 | 2007-01-27 19:27:06 +0000 | [diff] [blame] | 1787 | } |
| 1788 | |
| Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1789 | /// DeclhasAttr - returns true if decl Declaration already has the target |
| 1790 | /// attribute. |
| Aaron Ballman | b9023ed | 2014-01-20 18:07:09 +0000 | [diff] [blame] | 1791 | static bool DeclHasAttr(const Decl *D, const Attr *A) { |
| Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1792 | const OwnershipAttr *OA = dyn_cast<OwnershipAttr>(A); |
| Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 1793 | const AnnotateAttr *Ann = dyn_cast<AnnotateAttr>(A); |
| Aaron Ballman | 7dce1a8 | 2014-03-07 13:13:38 +0000 | [diff] [blame] | 1794 | for (Decl::attr_iterator i = D->attr_begin(), e = D->attr_end(); i != e; ++i) |
| 1795 | if ((*i)->getKind() == A->getKind()) { |
| Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 1796 | if (Ann) { |
| Aaron Ballman | 7dce1a8 | 2014-03-07 13:13:38 +0000 | [diff] [blame] | 1797 | if (Ann->getAnnotation() == cast<AnnotateAttr>(*i)->getAnnotation()) |
| Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 1798 | return true; |
| 1799 | continue; |
| 1800 | } |
| Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1801 | // FIXME: Don't hardcode this check |
| Aaron Ballman | 7dce1a8 | 2014-03-07 13:13:38 +0000 | [diff] [blame] | 1802 | if (OA && isa<OwnershipAttr>(*i)) |
| 1803 | return OA->getOwnKind() == cast<OwnershipAttr>(*i)->getOwnKind(); |
| Chris Lattner | 8496639 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 1804 | return true; |
| Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1805 | } |
| Chris Lattner | 8496639 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 1806 | |
| 1807 | return false; |
| 1808 | } |
| 1809 | |
| Richard Smith | bc8caaf | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 1810 | static bool isAttributeTargetADefinition(Decl *D) { |
| 1811 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) |
| 1812 | return VD->isThisDeclarationADefinition(); |
| 1813 | if (TagDecl *TD = dyn_cast<TagDecl>(D)) |
| 1814 | return TD->isCompleteDefinition() || TD->isBeingDefined(); |
| 1815 | return true; |
| 1816 | } |
| 1817 | |
| 1818 | /// Merge alignment attributes from \p Old to \p New, taking into account the |
| 1819 | /// special semantics of C11's _Alignas specifier and C++11's alignas attribute. |
| 1820 | /// |
| 1821 | /// \return \c true if any attributes were added to \p New. |
| 1822 | static bool mergeAlignedAttrs(Sema &S, NamedDecl *New, Decl *Old) { |
| 1823 | // Look for alignas attributes on Old, and pick out whichever attribute |
| 1824 | // specifies the strictest alignment requirement. |
| 1825 | AlignedAttr *OldAlignasAttr = 0; |
| 1826 | AlignedAttr *OldStrictestAlignAttr = 0; |
| 1827 | unsigned OldAlign = 0; |
| 1828 | for (specific_attr_iterator<AlignedAttr> |
| 1829 | I = Old->specific_attr_begin<AlignedAttr>(), |
| 1830 | E = Old->specific_attr_end<AlignedAttr>(); I != E; ++I) { |
| 1831 | // FIXME: We have no way of representing inherited dependent alignments |
| 1832 | // in a case like: |
| 1833 | // template<int A, int B> struct alignas(A) X; |
| 1834 | // template<int A, int B> struct alignas(B) X {}; |
| 1835 | // For now, we just ignore any alignas attributes which are not on the |
| 1836 | // definition in such a case. |
| 1837 | if (I->isAlignmentDependent()) |
| 1838 | return false; |
| 1839 | |
| 1840 | if (I->isAlignas()) |
| 1841 | OldAlignasAttr = *I; |
| 1842 | |
| 1843 | unsigned Align = I->getAlignment(S.Context); |
| 1844 | if (Align > OldAlign) { |
| 1845 | OldAlign = Align; |
| 1846 | OldStrictestAlignAttr = *I; |
| 1847 | } |
| 1848 | } |
| 1849 | |
| 1850 | // Look for alignas attributes on New. |
| 1851 | AlignedAttr *NewAlignasAttr = 0; |
| 1852 | unsigned NewAlign = 0; |
| 1853 | for (specific_attr_iterator<AlignedAttr> |
| 1854 | I = New->specific_attr_begin<AlignedAttr>(), |
| 1855 | E = New->specific_attr_end<AlignedAttr>(); I != E; ++I) { |
| 1856 | if (I->isAlignmentDependent()) |
| 1857 | return false; |
| 1858 | |
| 1859 | if (I->isAlignas()) |
| 1860 | NewAlignasAttr = *I; |
| 1861 | |
| 1862 | unsigned Align = I->getAlignment(S.Context); |
| 1863 | if (Align > NewAlign) |
| 1864 | NewAlign = Align; |
| 1865 | } |
| 1866 | |
| 1867 | if (OldAlignasAttr && NewAlignasAttr && OldAlign != NewAlign) { |
| 1868 | // Both declarations have 'alignas' attributes. We require them to match. |
| 1869 | // C++11 [dcl.align]p6 and C11 6.7.5/7 both come close to saying this, but |
| 1870 | // fall short. (If two declarations both have alignas, they must both match |
| 1871 | // every definition, and so must match each other if there is a definition.) |
| 1872 | |
| 1873 | // If either declaration only contains 'alignas(0)' specifiers, then it |
| 1874 | // specifies the natural alignment for the type. |
| 1875 | if (OldAlign == 0 || NewAlign == 0) { |
| 1876 | QualType Ty; |
| 1877 | if (ValueDecl *VD = dyn_cast<ValueDecl>(New)) |
| 1878 | Ty = VD->getType(); |
| 1879 | else |
| 1880 | Ty = S.Context.getTagDeclType(cast<TagDecl>(New)); |
| 1881 | |
| 1882 | if (OldAlign == 0) |
| 1883 | OldAlign = S.Context.getTypeAlign(Ty); |
| 1884 | if (NewAlign == 0) |
| 1885 | NewAlign = S.Context.getTypeAlign(Ty); |
| 1886 | } |
| 1887 | |
| 1888 | if (OldAlign != NewAlign) { |
| 1889 | S.Diag(NewAlignasAttr->getLocation(), diag::err_alignas_mismatch) |
| 1890 | << (unsigned)S.Context.toCharUnitsFromBits(OldAlign).getQuantity() |
| 1891 | << (unsigned)S.Context.toCharUnitsFromBits(NewAlign).getQuantity(); |
| 1892 | S.Diag(OldAlignasAttr->getLocation(), diag::note_previous_declaration); |
| 1893 | } |
| 1894 | } |
| 1895 | |
| 1896 | if (OldAlignasAttr && !NewAlignasAttr && isAttributeTargetADefinition(New)) { |
| 1897 | // C++11 [dcl.align]p6: |
| 1898 | // if any declaration of an entity has an alignment-specifier, |
| 1899 | // every defining declaration of that entity shall specify an |
| 1900 | // equivalent alignment. |
| 1901 | // C11 6.7.5/7: |
| 1902 | // If the definition of an object does not have an alignment |
| 1903 | // specifier, any other declaration of that object shall also |
| 1904 | // have no alignment specifier. |
| 1905 | S.Diag(New->getLocation(), diag::err_alignas_missing_on_definition) |
| Aaron Ballman | 3d216a5 | 2014-01-02 23:39:11 +0000 | [diff] [blame] | 1906 | << OldAlignasAttr; |
| Richard Smith | bc8caaf | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 1907 | S.Diag(OldAlignasAttr->getLocation(), diag::note_alignas_on_declaration) |
| Aaron Ballman | 3d216a5 | 2014-01-02 23:39:11 +0000 | [diff] [blame] | 1908 | << OldAlignasAttr; |
| Richard Smith | bc8caaf | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 1909 | } |
| 1910 | |
| 1911 | bool AnyAdded = false; |
| 1912 | |
| 1913 | // Ensure we have an attribute representing the strictest alignment. |
| 1914 | if (OldAlign > NewAlign) { |
| 1915 | AlignedAttr *Clone = OldStrictestAlignAttr->clone(S.Context); |
| 1916 | Clone->setInherited(true); |
| 1917 | New->addAttr(Clone); |
| 1918 | AnyAdded = true; |
| 1919 | } |
| 1920 | |
| 1921 | // Ensure we have an alignas attribute if the old declaration had one. |
| 1922 | if (OldAlignasAttr && !NewAlignasAttr && |
| 1923 | !(AnyAdded && OldStrictestAlignAttr->isAlignas())) { |
| 1924 | AlignedAttr *Clone = OldAlignasAttr->clone(S.Context); |
| 1925 | Clone->setInherited(true); |
| 1926 | New->addAttr(Clone); |
| 1927 | AnyAdded = true; |
| 1928 | } |
| 1929 | |
| 1930 | return AnyAdded; |
| 1931 | } |
| 1932 | |
| 1933 | static bool mergeDeclAttribute(Sema &S, NamedDecl *D, InheritableAttr *Attr, |
| 1934 | bool Override) { |
| Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1935 | InheritableAttr *NewAttr = NULL; |
| Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1936 | unsigned AttrSpellingListIndex = Attr->getSpellingListIndex(); |
| Rafael Espindola | 19de561 | 2013-01-12 06:42:30 +0000 | [diff] [blame] | 1937 | if (AvailabilityAttr *AA = dyn_cast<AvailabilityAttr>(Attr)) |
| Richard Smith | bc8caaf | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 1938 | NewAttr = S.mergeAvailabilityAttr(D, AA->getRange(), AA->getPlatform(), |
| 1939 | AA->getIntroduced(), AA->getDeprecated(), |
| 1940 | AA->getObsoleted(), AA->getUnavailable(), |
| 1941 | AA->getMessage(), Override, |
| John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 1942 | AttrSpellingListIndex); |
| Richard Smith | bc8caaf | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 1943 | else if (VisibilityAttr *VA = dyn_cast<VisibilityAttr>(Attr)) |
| 1944 | NewAttr = S.mergeVisibilityAttr(D, VA->getRange(), VA->getVisibility(), |
| 1945 | AttrSpellingListIndex); |
| 1946 | else if (TypeVisibilityAttr *VA = dyn_cast<TypeVisibilityAttr>(Attr)) |
| 1947 | NewAttr = S.mergeTypeVisibilityAttr(D, VA->getRange(), VA->getVisibility(), |
| 1948 | AttrSpellingListIndex); |
| Rafael Espindola | 19de561 | 2013-01-12 06:42:30 +0000 | [diff] [blame] | 1949 | else if (DLLImportAttr *ImportA = dyn_cast<DLLImportAttr>(Attr)) |
| Richard Smith | bc8caaf | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 1950 | NewAttr = S.mergeDLLImportAttr(D, ImportA->getRange(), |
| 1951 | AttrSpellingListIndex); |
| Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1952 | else if (DLLExportAttr *ExportA = dyn_cast<DLLExportAttr>(Attr)) |
| Richard Smith | bc8caaf | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 1953 | NewAttr = S.mergeDLLExportAttr(D, ExportA->getRange(), |
| 1954 | AttrSpellingListIndex); |
| Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1955 | else if (FormatAttr *FA = dyn_cast<FormatAttr>(Attr)) |
| Richard Smith | bc8caaf | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 1956 | NewAttr = S.mergeFormatAttr(D, FA->getRange(), FA->getType(), |
| 1957 | FA->getFormatIdx(), FA->getFirstArg(), |
| 1958 | AttrSpellingListIndex); |
| Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1959 | else if (SectionAttr *SA = dyn_cast<SectionAttr>(Attr)) |
| Richard Smith | bc8caaf | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 1960 | NewAttr = S.mergeSectionAttr(D, SA->getRange(), SA->getName(), |
| 1961 | AttrSpellingListIndex); |
| David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 1962 | else if (MSInheritanceAttr *IA = dyn_cast<MSInheritanceAttr>(Attr)) |
| David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 1963 | NewAttr = S.mergeMSInheritanceAttr(D, IA->getRange(), IA->getBestCase(), |
| 1964 | AttrSpellingListIndex, |
| David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 1965 | IA->getSemanticSpelling()); |
| Richard Smith | bc8caaf | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 1966 | else if (isa<AlignedAttr>(Attr)) |
| 1967 | // AlignedAttrs are handled separately, because we need to handle all |
| 1968 | // such attributes on a declaration at the same time. |
| 1969 | NewAttr = 0; |
| Aaron Ballman | b9023ed | 2014-01-20 18:07:09 +0000 | [diff] [blame] | 1970 | else if (Attr->duplicatesAllowed() || !DeclHasAttr(D, Attr)) |
| Richard Smith | bc8caaf | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 1971 | NewAttr = cast<InheritableAttr>(Attr->clone(S.Context)); |
| Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1972 | |
| Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1973 | if (NewAttr) { |
| Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1974 | NewAttr->setInherited(true); |
| 1975 | D->addAttr(NewAttr); |
| 1976 | return true; |
| 1977 | } |
| 1978 | |
| 1979 | return false; |
| 1980 | } |
| 1981 | |
| Rafael Espindola | a5bba70 | 2012-07-15 01:05:36 +0000 | [diff] [blame] | 1982 | static const Decl *getDefinition(const Decl *D) { |
| 1983 | if (const TagDecl *TD = dyn_cast<TagDecl>(D)) |
| Rafael Espindola | 3619104 | 2012-05-18 01:47:00 +0000 | [diff] [blame] | 1984 | return TD->getDefinition(); |
| Rafael Espindola | d53ffa0 | 2013-10-22 21:39:03 +0000 | [diff] [blame] | 1985 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 1986 | const VarDecl *Def = VD->getDefinition(); |
| 1987 | if (Def) |
| 1988 | return Def; |
| 1989 | return VD->getActingDefinition(); |
| 1990 | } |
| Rafael Espindola | a5bba70 | 2012-07-15 01:05:36 +0000 | [diff] [blame] | 1991 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| Rafael Espindola | 3619104 | 2012-05-18 01:47:00 +0000 | [diff] [blame] | 1992 | const FunctionDecl* Def; |
| Rafael Espindola | d53ffa0 | 2013-10-22 21:39:03 +0000 | [diff] [blame] | 1993 | if (FD->isDefined(Def)) |
| Rafael Espindola | 3619104 | 2012-05-18 01:47:00 +0000 | [diff] [blame] | 1994 | return Def; |
| 1995 | } |
| 1996 | return NULL; |
| 1997 | } |
| 1998 | |
| Rafael Espindola | faf556b | 2012-07-15 01:33:40 +0000 | [diff] [blame] | 1999 | static bool hasAttribute(const Decl *D, attr::Kind Kind) { |
| Aaron Ballman | 7dce1a8 | 2014-03-07 13:13:38 +0000 | [diff] [blame] | 2000 | for (Decl::attr_iterator I = D->attr_begin(), E = D->attr_end(); |
| 2001 | I != E; ++I) { |
| 2002 | Attr *Attribute = *I; |
| Rafael Espindola | faf556b | 2012-07-15 01:33:40 +0000 | [diff] [blame] | 2003 | if (Attribute->getKind() == Kind) |
| 2004 | return true; |
| Aaron Ballman | 7dce1a8 | 2014-03-07 13:13:38 +0000 | [diff] [blame] | 2005 | } |
| Rafael Espindola | faf556b | 2012-07-15 01:33:40 +0000 | [diff] [blame] | 2006 | return false; |
| 2007 | } |
| 2008 | |
| 2009 | /// checkNewAttributesAfterDef - If we already have a definition, check that |
| 2010 | /// there are no new attributes in this declaration. |
| 2011 | static void checkNewAttributesAfterDef(Sema &S, Decl *New, const Decl *Old) { |
| 2012 | if (!New->hasAttrs()) |
| 2013 | return; |
| 2014 | |
| 2015 | const Decl *Def = getDefinition(Old); |
| 2016 | if (!Def || Def == New) |
| 2017 | return; |
| 2018 | |
| 2019 | AttrVec &NewAttributes = New->getAttrs(); |
| 2020 | for (unsigned I = 0, E = NewAttributes.size(); I != E;) { |
| 2021 | const Attr *NewAttribute = NewAttributes[I]; |
| Rafael Espindola | d53ffa0 | 2013-10-22 21:39:03 +0000 | [diff] [blame] | 2022 | |
| 2023 | if (isa<AliasAttr>(NewAttribute)) { |
| 2024 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(New)) |
| 2025 | S.CheckForFunctionRedefinition(FD, cast<FunctionDecl>(Def)); |
| 2026 | else { |
| 2027 | VarDecl *VD = cast<VarDecl>(New); |
| 2028 | unsigned Diag = cast<VarDecl>(Def)->isThisDeclarationADefinition() == |
| 2029 | VarDecl::TentativeDefinition |
| 2030 | ? diag::err_alias_after_tentative |
| 2031 | : diag::err_redefinition; |
| 2032 | S.Diag(VD->getLocation(), Diag) << VD->getDeclName(); |
| 2033 | S.Diag(Def->getLocation(), diag::note_previous_definition); |
| 2034 | VD->setInvalidDecl(); |
| 2035 | } |
| 2036 | ++I; |
| 2037 | continue; |
| 2038 | } |
| 2039 | |
| 2040 | if (const VarDecl *VD = dyn_cast<VarDecl>(Def)) { |
| 2041 | // Tentative definitions are only interesting for the alias check above. |
| 2042 | if (VD->isThisDeclarationADefinition() != VarDecl::Definition) { |
| 2043 | ++I; |
| 2044 | continue; |
| 2045 | } |
| 2046 | } |
| 2047 | |
| Rafael Espindola | faf556b | 2012-07-15 01:33:40 +0000 | [diff] [blame] | 2048 | if (hasAttribute(Def, NewAttribute->getKind())) { |
| 2049 | ++I; |
| 2050 | continue; // regular attr merging will take care of validating this. |
| 2051 | } |
| Richard Smith | bc8caaf | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 2052 | |
| Richard Smith | debc59d | 2013-01-30 05:45:05 +0000 | [diff] [blame] | 2053 | if (isa<C11NoReturnAttr>(NewAttribute)) { |
| Richard Smith | bc8caaf | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 2054 | // C's _Noreturn is allowed to be added to a function after it is defined. |
| Richard Smith | debc59d | 2013-01-30 05:45:05 +0000 | [diff] [blame] | 2055 | ++I; |
| 2056 | continue; |
| Richard Smith | bc8caaf | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 2057 | } else if (const AlignedAttr *AA = dyn_cast<AlignedAttr>(NewAttribute)) { |
| 2058 | if (AA->isAlignas()) { |
| 2059 | // C++11 [dcl.align]p6: |
| 2060 | // if any declaration of an entity has an alignment-specifier, |
| 2061 | // every defining declaration of that entity shall specify an |
| 2062 | // equivalent alignment. |
| 2063 | // C11 6.7.5/7: |
| 2064 | // If the definition of an object does not have an alignment |
| 2065 | // specifier, any other declaration of that object shall also |
| 2066 | // have no alignment specifier. |
| 2067 | S.Diag(Def->getLocation(), diag::err_alignas_missing_on_definition) |
| Aaron Ballman | 3d216a5 | 2014-01-02 23:39:11 +0000 | [diff] [blame] | 2068 | << AA; |
| Richard Smith | bc8caaf | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 2069 | S.Diag(NewAttribute->getLocation(), diag::note_alignas_on_declaration) |
| Aaron Ballman | 3d216a5 | 2014-01-02 23:39:11 +0000 | [diff] [blame] | 2070 | << AA; |
| Richard Smith | bc8caaf | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 2071 | NewAttributes.erase(NewAttributes.begin() + I); |
| 2072 | --E; |
| 2073 | continue; |
| 2074 | } |
| Richard Smith | debc59d | 2013-01-30 05:45:05 +0000 | [diff] [blame] | 2075 | } |
| Richard Smith | bc8caaf | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 2076 | |
| Rafael Espindola | faf556b | 2012-07-15 01:33:40 +0000 | [diff] [blame] | 2077 | S.Diag(NewAttribute->getLocation(), |
| 2078 | diag::warn_attribute_precede_definition); |
| 2079 | S.Diag(Def->getLocation(), diag::note_previous_definition); |
| 2080 | NewAttributes.erase(NewAttributes.begin() + I); |
| 2081 | --E; |
| 2082 | } |
| 2083 | } |
| 2084 | |
| John McCall | f79e87d | 2011-03-02 04:00:57 +0000 | [diff] [blame] | 2085 | /// mergeDeclAttributes - Copy attributes from the Old decl to the New one. |
| Rafael Espindola | a3aea43 | 2013-01-08 22:04:34 +0000 | [diff] [blame] | 2086 | void Sema::mergeDeclAttributes(NamedDecl *New, Decl *Old, |
| Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2087 | AvailabilityMergeKind AMK) { |
| Rafael Espindola | b093885 | 2013-10-25 01:28:12 +0000 | [diff] [blame] | 2088 | if (UsedAttr *OldAttr = Old->getMostRecentDecl()->getAttr<UsedAttr>()) { |
| 2089 | UsedAttr *NewAttr = OldAttr->clone(Context); |
| 2090 | NewAttr->setInherited(true); |
| 2091 | New->addAttr(NewAttr); |
| 2092 | } |
| 2093 | |
| Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 2094 | if (!Old->hasAttrs() && !New->hasAttrs()) |
| 2095 | return; |
| 2096 | |
| Rafael Espindola | 3619104 | 2012-05-18 01:47:00 +0000 | [diff] [blame] | 2097 | // attributes declared post-definition are currently ignored |
| Rafael Espindola | faf556b | 2012-07-15 01:33:40 +0000 | [diff] [blame] | 2098 | checkNewAttributesAfterDef(*this, New, Old); |
| Rafael Espindola | 3619104 | 2012-05-18 01:47:00 +0000 | [diff] [blame] | 2099 | |
| Douglas Gregor | 32c1757 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 2100 | if (!Old->hasAttrs()) |
| Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2101 | return; |
| John McCall | f79e87d | 2011-03-02 04:00:57 +0000 | [diff] [blame] | 2102 | |
| Douglas Gregor | 32c1757 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 2103 | bool foundAny = New->hasAttrs(); |
| John McCall | f79e87d | 2011-03-02 04:00:57 +0000 | [diff] [blame] | 2104 | |
| Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2105 | // Ensure that any moving of objects within the allocated map is done before |
| 2106 | // we process them. |
| Douglas Gregor | 32c1757 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 2107 | if (!foundAny) New->setAttrs(AttrVec()); |
| John McCall | f79e87d | 2011-03-02 04:00:57 +0000 | [diff] [blame] | 2108 | |
| Peter Collingbourne | ab8bc06 | 2011-01-21 02:08:36 +0000 | [diff] [blame] | 2109 | for (specific_attr_iterator<InheritableAttr> |
| Douglas Gregor | 32c1757 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 2110 | i = Old->specific_attr_begin<InheritableAttr>(), |
| 2111 | e = Old->specific_attr_end<InheritableAttr>(); |
| 2112 | i != e; ++i) { |
| Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2113 | bool Override = false; |
| Douglas Gregor | b1fa148 | 2011-09-23 20:23:42 +0000 | [diff] [blame] | 2114 | // Ignore deprecated/unavailable/availability attributes if requested. |
| Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2115 | if (isa<DeprecatedAttr>(*i) || |
| 2116 | isa<UnavailableAttr>(*i) || |
| 2117 | isa<AvailabilityAttr>(*i)) { |
| 2118 | switch (AMK) { |
| 2119 | case AMK_None: |
| 2120 | continue; |
| John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 2121 | |
| Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2122 | case AMK_Redeclaration: |
| 2123 | break; |
| 2124 | |
| 2125 | case AMK_Override: |
| 2126 | Override = true; |
| 2127 | break; |
| 2128 | } |
| 2129 | } |
| 2130 | |
| Rafael Espindola | b093885 | 2013-10-25 01:28:12 +0000 | [diff] [blame] | 2131 | // Already handled. |
| 2132 | if (isa<UsedAttr>(*i)) |
| 2133 | continue; |
| 2134 | |
| Richard Smith | bc8caaf | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 2135 | if (mergeDeclAttribute(*this, New, *i, Override)) |
| John McCall | f79e87d | 2011-03-02 04:00:57 +0000 | [diff] [blame] | 2136 | foundAny = true; |
| Chris Lattner | 8496639 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 2137 | } |
| John McCall | f79e87d | 2011-03-02 04:00:57 +0000 | [diff] [blame] | 2138 | |
| Richard Smith | bc8caaf | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 2139 | if (mergeAlignedAttrs(*this, New, Old)) |
| 2140 | foundAny = true; |
| 2141 | |
| Douglas Gregor | 32c1757 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 2142 | if (!foundAny) New->dropAttrs(); |
| John McCall | f79e87d | 2011-03-02 04:00:57 +0000 | [diff] [blame] | 2143 | } |
| 2144 | |
| 2145 | /// mergeParamDeclAttributes - Copy attributes from the old parameter |
| 2146 | /// to the new one. |
| 2147 | static void mergeParamDeclAttributes(ParmVarDecl *newDecl, |
| 2148 | const ParmVarDecl *oldDecl, |
| Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 2149 | Sema &S) { |
| 2150 | // C++11 [dcl.attr.depend]p2: |
| 2151 | // The first declaration of a function shall specify the |
| 2152 | // carries_dependency attribute for its declarator-id if any declaration |
| 2153 | // of the function specifies the carries_dependency attribute. |
| Aaron Ballman | cf3b483 | 2013-12-19 13:36:16 +0000 | [diff] [blame] | 2154 | const CarriesDependencyAttr *CDA = newDecl->getAttr<CarriesDependencyAttr>(); |
| 2155 | if (CDA && !oldDecl->hasAttr<CarriesDependencyAttr>()) { |
| 2156 | S.Diag(CDA->getLocation(), |
| Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 2157 | diag::err_carries_dependency_missing_on_first_decl) << 1/*Param*/; |
| 2158 | // Find the first declaration of the parameter. |
| 2159 | // FIXME: Should we build redeclaration chains for function parameters? |
| 2160 | const FunctionDecl *FirstFD = |
| Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 2161 | cast<FunctionDecl>(oldDecl->getDeclContext())->getFirstDecl(); |
| Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 2162 | const ParmVarDecl *FirstVD = |
| 2163 | FirstFD->getParamDecl(oldDecl->getFunctionScopeIndex()); |
| 2164 | S.Diag(FirstVD->getLocation(), |
| 2165 | diag::note_carries_dependency_missing_first_decl) << 1/*Param*/; |
| 2166 | } |
| 2167 | |
| John McCall | f79e87d | 2011-03-02 04:00:57 +0000 | [diff] [blame] | 2168 | if (!oldDecl->hasAttrs()) |
| 2169 | return; |
| 2170 | |
| 2171 | bool foundAny = newDecl->hasAttrs(); |
| 2172 | |
| 2173 | // Ensure that any moving of objects within the allocated map is |
| 2174 | // done before we process them. |
| 2175 | if (!foundAny) newDecl->setAttrs(AttrVec()); |
| 2176 | |
| 2177 | for (specific_attr_iterator<InheritableParamAttr> |
| 2178 | i = oldDecl->specific_attr_begin<InheritableParamAttr>(), |
| 2179 | e = oldDecl->specific_attr_end<InheritableParamAttr>(); i != e; ++i) { |
| 2180 | if (!DeclHasAttr(newDecl, *i)) { |
| Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 2181 | InheritableAttr *newAttr = |
| 2182 | cast<InheritableParamAttr>((*i)->clone(S.Context)); |
| John McCall | f79e87d | 2011-03-02 04:00:57 +0000 | [diff] [blame] | 2183 | newAttr->setInherited(true); |
| 2184 | newDecl->addAttr(newAttr); |
| 2185 | foundAny = true; |
| 2186 | } |
| 2187 | } |
| 2188 | |
| 2189 | if (!foundAny) newDecl->dropAttrs(); |
| Chris Lattner | 8496639 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 2190 | } |
| 2191 | |
| Dan Gohman | 28ade55 | 2010-07-26 21:25:24 +0000 | [diff] [blame] | 2192 | namespace { |
| 2193 | |
| Douglas Gregor | a74a297 | 2009-03-06 22:43:54 +0000 | [diff] [blame] | 2194 | /// Used in MergeFunctionDecl to keep track of function parameters in |
| 2195 | /// C. |
| 2196 | struct GNUCompatibleParamWarning { |
| 2197 | ParmVarDecl *OldParm; |
| 2198 | ParmVarDecl *NewParm; |
| 2199 | QualType PromotedType; |
| 2200 | }; |
| 2201 | |
| Dan Gohman | 28ade55 | 2010-07-26 21:25:24 +0000 | [diff] [blame] | 2202 | } |
| Anders Carlsson | 1f78b2b | 2009-12-04 22:33:25 +0000 | [diff] [blame] | 2203 | |
| 2204 | /// getSpecialMember - get the special member enum for a method. |
| Anders Carlsson | 05bf009 | 2010-04-22 05:40:53 +0000 | [diff] [blame] | 2205 | Sema::CXXSpecialMember Sema::getSpecialMember(const CXXMethodDecl *MD) { |
| Anders Carlsson | 1f78b2b | 2009-12-04 22:33:25 +0000 | [diff] [blame] | 2206 | if (const CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(MD)) { |
| Alexis Hunt | 80f00ff | 2011-05-10 19:08:14 +0000 | [diff] [blame] | 2207 | if (Ctor->isDefaultConstructor()) |
| 2208 | return Sema::CXXDefaultConstructor; |
| Alexis Hunt | d051b87 | 2011-05-26 01:26:05 +0000 | [diff] [blame] | 2209 | |
| 2210 | if (Ctor->isCopyConstructor()) |
| 2211 | return Sema::CXXCopyConstructor; |
| 2212 | |
| 2213 | if (Ctor->isMoveConstructor()) |
| 2214 | return Sema::CXXMoveConstructor; |
| Alexis Hunt | 119c10e | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 2215 | } else if (isa<CXXDestructorDecl>(MD)) { |
| Anders Carlsson | 1f78b2b | 2009-12-04 22:33:25 +0000 | [diff] [blame] | 2216 | return Sema::CXXDestructor; |
| Alexis Hunt | 119c10e | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 2217 | } else if (MD->isCopyAssignmentOperator()) { |
| Alexis Hunt | 80f00ff | 2011-05-10 19:08:14 +0000 | [diff] [blame] | 2218 | return Sema::CXXCopyAssignment; |
| Sebastian Redl | e9c4e84 | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 2219 | } else if (MD->isMoveAssignmentOperator()) { |
| 2220 | return Sema::CXXMoveAssignment; |
| Alexis Hunt | 119c10e | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 2221 | } |
| Alexis Hunt | 80f00ff | 2011-05-10 19:08:14 +0000 | [diff] [blame] | 2222 | |
| Alexis Hunt | 80f00ff | 2011-05-10 19:08:14 +0000 | [diff] [blame] | 2223 | return Sema::CXXInvalid; |
| Anders Carlsson | 1f78b2b | 2009-12-04 22:33:25 +0000 | [diff] [blame] | 2224 | } |
| 2225 | |
| Sebastian Redl | 243d905 | 2010-06-09 21:17:41 +0000 | [diff] [blame] | 2226 | /// canRedefineFunction - checks if a function can be redefined. Currently, |
| Charles Davis | fea4845 | 2010-02-18 02:00:42 +0000 | [diff] [blame] | 2227 | /// only extern inline functions can be redefined, and even then only in |
| 2228 | /// GNU89 mode. |
| 2229 | static bool canRedefineFunction(const FunctionDecl *FD, |
| 2230 | const LangOptions& LangOpts) { |
| Eli Friedman | 51dd018 | 2011-06-13 23:56:42 +0000 | [diff] [blame] | 2231 | return ((FD->hasAttr<GNUInlineAttr>() || LangOpts.GNUInline) && |
| 2232 | !LangOpts.CPlusPlus && |
| Charles Davis | fea4845 | 2010-02-18 02:00:42 +0000 | [diff] [blame] | 2233 | FD->isInlineSpecified() && |
| John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2234 | FD->getStorageClass() == SC_Extern); |
| Charles Davis | fea4845 | 2010-02-18 02:00:42 +0000 | [diff] [blame] | 2235 | } |
| 2236 | |
| Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 2237 | const AttributedType *Sema::getCallingConvAttributedType(QualType T) const { |
| 2238 | const AttributedType *AT = T->getAs<AttributedType>(); |
| 2239 | while (AT && !AT->isCallingConv()) |
| 2240 | AT = AT->getModifiedType()->getAs<AttributedType>(); |
| 2241 | return AT; |
| John McCall | a5f46fb | 2012-08-25 02:00:03 +0000 | [diff] [blame] | 2242 | } |
| 2243 | |
| Benjamin Kramer | 3e35026 | 2013-02-15 12:30:38 +0000 | [diff] [blame] | 2244 | template <typename T> |
| 2245 | static bool haveIncompatibleLanguageLinkages(const T *Old, const T *New) { |
| Rafael Espindola | f418765 | 2013-02-14 01:18:37 +0000 | [diff] [blame] | 2246 | const DeclContext *DC = Old->getDeclContext(); |
| 2247 | if (DC->isRecord()) |
| 2248 | return false; |
| 2249 | |
| 2250 | LanguageLinkage OldLinkage = Old->getLanguageLinkage(); |
| Rafael Espindola | 593537a | 2013-05-05 20:15:21 +0000 | [diff] [blame] | 2251 | if (OldLinkage == CXXLanguageLinkage && New->isInExternCContext()) |
| Rafael Espindola | f418765 | 2013-02-14 01:18:37 +0000 | [diff] [blame] | 2252 | return true; |
| Rafael Espindola | 593537a | 2013-05-05 20:15:21 +0000 | [diff] [blame] | 2253 | if (OldLinkage == CLanguageLinkage && New->isInExternCXXContext()) |
| Rafael Espindola | f418765 | 2013-02-14 01:18:37 +0000 | [diff] [blame] | 2254 | return true; |
| 2255 | return false; |
| 2256 | } |
| 2257 | |
| Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2258 | /// MergeFunctionDecl - We just parsed a function 'New' from |
| 2259 | /// declarator D which has the same name and scope as a previous |
| 2260 | /// declaration 'Old'. Figure out how to resolve this situation, |
| 2261 | /// merging decls or emitting diagnostics as appropriate. |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2262 | /// |
| 2263 | /// In C++, New and Old must be declarations that are not |
| 2264 | /// overloaded. Use IsOverload to determine whether New and Old are |
| 2265 | /// overloaded, and to select the Old declaration that New should be |
| 2266 | /// merged with. |
| Douglas Gregor | 75a45ba | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 2267 | /// |
| 2268 | /// Returns true if there was an error, false otherwise. |
| Richard Smith | 1881930 | 2014-02-06 01:31:33 +0000 | [diff] [blame] | 2269 | bool Sema::MergeFunctionDecl(FunctionDecl *New, NamedDecl *&OldD, |
| 2270 | Scope *S, bool MergeTypeWithOld) { |
| Chris Lattner | c511efb | 2007-01-27 19:32:14 +0000 | [diff] [blame] | 2271 | // Verify the old decl was also a function. |
| Alp Toker | a2794f9 | 2014-01-22 07:29:52 +0000 | [diff] [blame] | 2272 | FunctionDecl *Old = OldD->getAsFunction(); |
| Chris Lattner | c511efb | 2007-01-27 19:32:14 +0000 | [diff] [blame] | 2273 | if (!Old) { |
| John McCall | e29c5cd | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 2274 | if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(OldD)) { |
| John McCall | c70fca6 | 2013-04-03 21:19:47 +0000 | [diff] [blame] | 2275 | if (New->getFriendObjectKind()) { |
| 2276 | Diag(New->getLocation(), diag::err_using_decl_friend); |
| 2277 | Diag(Shadow->getTargetDecl()->getLocation(), |
| 2278 | diag::note_using_decl_target); |
| 2279 | Diag(Shadow->getUsingDecl()->getLocation(), |
| 2280 | diag::note_using_decl) << 0; |
| 2281 | return true; |
| 2282 | } |
| 2283 | |
| Richard Smith | 1881930 | 2014-02-06 01:31:33 +0000 | [diff] [blame] | 2284 | // C++11 [namespace.udecl]p14: |
| 2285 | // If a function declaration in namespace scope or block scope has the |
| 2286 | // same name and the same parameter-type-list as a function introduced |
| 2287 | // by a using-declaration, and the declarations do not declare the same |
| 2288 | // function, the program is ill-formed. |
| 2289 | |
| 2290 | // Check whether the two declarations might declare the same function. |
| 2291 | Old = dyn_cast<FunctionDecl>(Shadow->getTargetDecl()); |
| 2292 | if (Old && |
| 2293 | !Old->getDeclContext()->getRedeclContext()->Equals( |
| 2294 | New->getDeclContext()->getRedeclContext()) && |
| 2295 | !(Old->isExternC() && New->isExternC())) |
| 2296 | Old = 0; |
| 2297 | |
| 2298 | if (!Old) { |
| 2299 | Diag(New->getLocation(), diag::err_using_decl_conflict_reverse); |
| 2300 | Diag(Shadow->getTargetDecl()->getLocation(), |
| 2301 | diag::note_using_decl_target); |
| 2302 | Diag(Shadow->getUsingDecl()->getLocation(), diag::note_using_decl) << 0; |
| 2303 | return true; |
| 2304 | } |
| 2305 | OldD = Old; |
| 2306 | } else { |
| 2307 | Diag(New->getLocation(), diag::err_redefinition_different_kind) |
| 2308 | << New->getDeclName(); |
| 2309 | Diag(OldD->getLocation(), diag::note_previous_definition); |
| John McCall | e29c5cd | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 2310 | return true; |
| 2311 | } |
| Chris Lattner | c511efb | 2007-01-27 19:32:14 +0000 | [diff] [blame] | 2312 | } |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2313 | |
| David Majnemer | ea5092a | 2013-07-07 23:49:50 +0000 | [diff] [blame] | 2314 | // If the old declaration is invalid, just give up here. |
| 2315 | if (Old->isInvalidDecl()) |
| 2316 | return true; |
| 2317 | |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2318 | // Determine whether the previous declaration was a definition, |
| 2319 | // implicit declaration, or a declaration. |
| 2320 | diag::kind PrevDiag; |
| Richard Smith | bdd1464 | 2014-02-04 01:14:30 +0000 | [diff] [blame] | 2321 | SourceLocation OldLocation = Old->getLocation(); |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2322 | if (Old->isThisDeclarationADefinition()) |
| Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 2323 | PrevDiag = diag::note_previous_definition; |
| Richard Smith | bdd1464 | 2014-02-04 01:14:30 +0000 | [diff] [blame] | 2324 | else if (Old->isImplicit()) { |
| Douglas Gregor | 75a45ba | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 2325 | PrevDiag = diag::note_previous_implicit_declaration; |
| Richard Smith | bdd1464 | 2014-02-04 01:14:30 +0000 | [diff] [blame] | 2326 | if (OldLocation.isInvalid()) |
| 2327 | OldLocation = New->getLocation(); |
| 2328 | } else |
| Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 2329 | PrevDiag = diag::note_previous_declaration; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2330 | |
| Charles Davis | fea4845 | 2010-02-18 02:00:42 +0000 | [diff] [blame] | 2331 | // Don't complain about this if we're in GNU89 mode and the old function |
| 2332 | // is an extern inline function. |
| Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 2333 | // Don't complain about specializations. They are not supposed to have |
| 2334 | // storage classes. |
| Douglas Gregor | e62c0a4 | 2009-02-24 01:23:02 +0000 | [diff] [blame] | 2335 | if (!isa<CXXMethodDecl>(New) && !isa<CXXMethodDecl>(Old) && |
| John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2336 | New->getStorageClass() == SC_Static && |
| Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 2337 | Old->hasExternalFormalLinkage() && |
| Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 2338 | !New->getTemplateSpecializationInfo() && |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2339 | !canRedefineFunction(Old, getLangOpts())) { |
| 2340 | if (getLangOpts().MicrosoftExt) { |
| Francois Pichet | 6841a12 | 2011-04-22 19:50:06 +0000 | [diff] [blame] | 2341 | Diag(New->getLocation(), diag::warn_static_non_static) << New; |
| Richard Smith | bdd1464 | 2014-02-04 01:14:30 +0000 | [diff] [blame] | 2342 | Diag(OldLocation, PrevDiag); |
| Francois Pichet | 6841a12 | 2011-04-22 19:50:06 +0000 | [diff] [blame] | 2343 | } else { |
| 2344 | Diag(New->getLocation(), diag::err_static_non_static) << New; |
| Richard Smith | bdd1464 | 2014-02-04 01:14:30 +0000 | [diff] [blame] | 2345 | Diag(OldLocation, PrevDiag); |
| Francois Pichet | 6841a12 | 2011-04-22 19:50:06 +0000 | [diff] [blame] | 2346 | return true; |
| 2347 | } |
| Douglas Gregor | e62c0a4 | 2009-02-24 01:23:02 +0000 | [diff] [blame] | 2348 | } |
| 2349 | |
| Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 2350 | |
| 2351 | // If a function is first declared with a calling convention, but is later |
| 2352 | // declared or defined without one, all following decls assume the calling |
| 2353 | // convention of the first. |
| John McCall | cddbad0 | 2010-02-04 05:44:44 +0000 | [diff] [blame] | 2354 | // |
| John McCall | a5f46fb | 2012-08-25 02:00:03 +0000 | [diff] [blame] | 2355 | // It's OK if a function is first declared without a calling convention, |
| 2356 | // but is later declared or defined with the default calling convention. |
| 2357 | // |
| Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 2358 | // To test if either decl has an explicit calling convention, we look for |
| 2359 | // AttributedType sugar nodes on the type as written. If they are missing or |
| 2360 | // were canonicalized away, we assume the calling convention was implicit. |
| John McCall | cddbad0 | 2010-02-04 05:44:44 +0000 | [diff] [blame] | 2361 | // |
| 2362 | // Note also that we DO NOT return at this point, because we still have |
| 2363 | // other tests to run. |
| Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 2364 | QualType OldQType = Context.getCanonicalType(Old->getType()); |
| 2365 | QualType NewQType = Context.getCanonicalType(New->getType()); |
| John McCall | 4f5019e | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 2366 | const FunctionType *OldType = cast<FunctionType>(OldQType); |
| Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 2367 | const FunctionType *NewType = cast<FunctionType>(NewQType); |
| John McCall | 4f5019e | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 2368 | FunctionType::ExtInfo OldTypeInfo = OldType->getExtInfo(); |
| 2369 | FunctionType::ExtInfo NewTypeInfo = NewType->getExtInfo(); |
| 2370 | bool RequiresAdjustment = false; |
| John McCall | a5f46fb | 2012-08-25 02:00:03 +0000 | [diff] [blame] | 2371 | |
| Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 2372 | if (OldTypeInfo.getCC() != NewTypeInfo.getCC()) { |
| Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 2373 | FunctionDecl *First = Old->getFirstDecl(); |
| Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 2374 | const FunctionType *FT = |
| 2375 | First->getType().getCanonicalType()->castAs<FunctionType>(); |
| 2376 | FunctionType::ExtInfo FI = FT->getExtInfo(); |
| 2377 | bool NewCCExplicit = getCallingConvAttributedType(New->getType()); |
| 2378 | if (!NewCCExplicit) { |
| 2379 | // Inherit the CC from the previous declaration if it was specified |
| 2380 | // there but not here. |
| 2381 | NewTypeInfo = NewTypeInfo.withCallingConv(OldTypeInfo.getCC()); |
| 2382 | RequiresAdjustment = true; |
| 2383 | } else { |
| 2384 | // Calling conventions aren't compatible, so complain. |
| 2385 | bool FirstCCExplicit = getCallingConvAttributedType(First->getType()); |
| 2386 | Diag(New->getLocation(), diag::err_cconv_change) |
| 2387 | << FunctionType::getNameForCallConv(NewTypeInfo.getCC()) |
| 2388 | << !FirstCCExplicit |
| 2389 | << (!FirstCCExplicit ? "" : |
| 2390 | FunctionType::getNameForCallConv(FI.getCC())); |
| John McCall | a5f46fb | 2012-08-25 02:00:03 +0000 | [diff] [blame] | 2391 | |
| Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 2392 | // Put the note on the first decl, since it is the one that matters. |
| 2393 | Diag(First->getLocation(), diag::note_previous_declaration); |
| 2394 | return true; |
| 2395 | } |
| John McCall | cddbad0 | 2010-02-04 05:44:44 +0000 | [diff] [blame] | 2396 | } |
| 2397 | |
| John McCall | ab26cfa | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 2398 | // FIXME: diagnose the other way around? |
| John McCall | 4f5019e | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 2399 | if (OldTypeInfo.getNoReturn() && !NewTypeInfo.getNoReturn()) { |
| 2400 | NewTypeInfo = NewTypeInfo.withNoReturn(true); |
| 2401 | RequiresAdjustment = true; |
| John McCall | ab26cfa | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 2402 | } |
| 2403 | |
| Douglas Gregor | 77e274f | 2010-06-18 21:30:25 +0000 | [diff] [blame] | 2404 | // Merge regparm attribute. |
| Eli Friedman | c5b20b5 | 2011-04-09 08:18:08 +0000 | [diff] [blame] | 2405 | if (OldTypeInfo.getHasRegParm() != NewTypeInfo.getHasRegParm() || |
| 2406 | OldTypeInfo.getRegParm() != NewTypeInfo.getRegParm()) { |
| 2407 | if (NewTypeInfo.getHasRegParm()) { |
| Douglas Gregor | 77e274f | 2010-06-18 21:30:25 +0000 | [diff] [blame] | 2408 | Diag(New->getLocation(), diag::err_regparm_mismatch) |
| 2409 | << NewType->getRegParmType() |
| 2410 | << OldType->getRegParmType(); |
| Richard Smith | bdd1464 | 2014-02-04 01:14:30 +0000 | [diff] [blame] | 2411 | Diag(OldLocation, diag::note_previous_declaration); |
| Douglas Gregor | 77e274f | 2010-06-18 21:30:25 +0000 | [diff] [blame] | 2412 | return true; |
| 2413 | } |
| John McCall | 4f5019e | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 2414 | |
| 2415 | NewTypeInfo = NewTypeInfo.withRegParm(OldTypeInfo.getRegParm()); |
| 2416 | RequiresAdjustment = true; |
| 2417 | } |
| 2418 | |
| Douglas Gregor | f1404d7 | 2011-10-14 15:55:40 +0000 | [diff] [blame] | 2419 | // Merge ns_returns_retained attribute. |
| 2420 | if (OldTypeInfo.getProducesResult() != NewTypeInfo.getProducesResult()) { |
| 2421 | if (NewTypeInfo.getProducesResult()) { |
| 2422 | Diag(New->getLocation(), diag::err_returns_retained_mismatch); |
| Richard Smith | bdd1464 | 2014-02-04 01:14:30 +0000 | [diff] [blame] | 2423 | Diag(OldLocation, diag::note_previous_declaration); |
| Douglas Gregor | f1404d7 | 2011-10-14 15:55:40 +0000 | [diff] [blame] | 2424 | return true; |
| 2425 | } |
| 2426 | |
| 2427 | NewTypeInfo = NewTypeInfo.withProducesResult(true); |
| 2428 | RequiresAdjustment = true; |
| 2429 | } |
| 2430 | |
| John McCall | 4f5019e | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 2431 | if (RequiresAdjustment) { |
| Eli Friedman | e934af8 | 2013-09-06 21:09:09 +0000 | [diff] [blame] | 2432 | const FunctionType *AdjustedType = New->getType()->getAs<FunctionType>(); |
| 2433 | AdjustedType = Context.adjustFunctionType(AdjustedType, NewTypeInfo); |
| 2434 | New->setType(QualType(AdjustedType, 0)); |
| John McCall | 4f5019e | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 2435 | NewQType = Context.getCanonicalType(New->getType()); |
| Eli Friedman | e934af8 | 2013-09-06 21:09:09 +0000 | [diff] [blame] | 2436 | NewType = cast<FunctionType>(NewQType); |
| Douglas Gregor | 77e274f | 2010-06-18 21:30:25 +0000 | [diff] [blame] | 2437 | } |
| Nick Lewycky | 9c7eb1d | 2013-02-01 08:13:20 +0000 | [diff] [blame] | 2438 | |
| 2439 | // If this redeclaration makes the function inline, we may need to add it to |
| 2440 | // UndefinedButUsed. |
| 2441 | if (!Old->isInlined() && New->isInlined() && |
| 2442 | !New->hasAttr<GNUInlineAttr>() && |
| 2443 | (getLangOpts().CPlusPlus || !getLangOpts().GNUInline) && |
| 2444 | Old->isUsed(false) && |
| 2445 | !Old->isDefined() && !New->isThisDeclarationADefinition()) |
| 2446 | UndefinedButUsed.insert(std::make_pair(Old->getCanonicalDecl(), |
| 2447 | SourceLocation())); |
| 2448 | |
| 2449 | // If this redeclaration makes it newly gnu_inline, we don't want to warn |
| 2450 | // about it. |
| 2451 | if (New->hasAttr<GNUInlineAttr>() && |
| 2452 | Old->isInlined() && !Old->hasAttr<GNUInlineAttr>()) { |
| 2453 | UndefinedButUsed.erase(Old->getCanonicalDecl()); |
| 2454 | } |
| Douglas Gregor | 77e274f | 2010-06-18 21:30:25 +0000 | [diff] [blame] | 2455 | |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2456 | if (getLangOpts().CPlusPlus) { |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2457 | // (C++98 13.1p2): |
| 2458 | // Certain function declarations cannot be overloaded: |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2459 | // -- Function declarations that differ only in the return type |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2460 | // cannot be overloaded. |
| Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2461 | |
| 2462 | // Go back to the type source info to compare the declared return types, |
| Richard Smith | c58f38f | 2013-08-14 20:16:31 +0000 | [diff] [blame] | 2463 | // per C++1y [dcl.type.auto]p13: |
| Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2464 | // Redeclarations or specializations of a function or function template |
| 2465 | // with a declared return type that uses a placeholder type shall also |
| 2466 | // use that placeholder, not a deduced type. |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2467 | QualType OldDeclaredReturnType = |
| 2468 | (Old->getTypeSourceInfo() |
| 2469 | ? Old->getTypeSourceInfo()->getType()->castAs<FunctionType>() |
| 2470 | : OldType)->getReturnType(); |
| 2471 | QualType NewDeclaredReturnType = |
| 2472 | (New->getTypeSourceInfo() |
| 2473 | ? New->getTypeSourceInfo()->getType()->castAs<FunctionType>() |
| 2474 | : NewType)->getReturnType(); |
| Fariborz Jahanian | f633ebd | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 2475 | QualType ResQT; |
| Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 2476 | if (!Context.hasSameType(OldDeclaredReturnType, NewDeclaredReturnType) && |
| 2477 | !((NewQType->isDependentType() || OldQType->isDependentType()) && |
| 2478 | New->isLocalExternDecl())) { |
| Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2479 | if (NewDeclaredReturnType->isObjCObjectPointerType() && |
| 2480 | OldDeclaredReturnType->isObjCObjectPointerType()) |
| Fariborz Jahanian | f633ebd | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 2481 | ResQT = Context.mergeObjCGCQualifiers(NewQType, OldQType); |
| 2482 | if (ResQT.isNull()) { |
| Argyrios Kyrtzidis | 3d32086 | 2011-02-05 05:54:49 +0000 | [diff] [blame] | 2483 | if (New->isCXXClassMember() && New->isOutOfLine()) |
| 2484 | Diag(New->getLocation(), |
| 2485 | diag::err_member_def_does_not_match_ret_type) << New; |
| 2486 | else |
| 2487 | Diag(New->getLocation(), diag::err_ovl_diff_return_type); |
| Richard Smith | bdd1464 | 2014-02-04 01:14:30 +0000 | [diff] [blame] | 2488 | Diag(OldLocation, PrevDiag) << Old << Old->getType(); |
| Fariborz Jahanian | f633ebd | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 2489 | return true; |
| 2490 | } |
| 2491 | else |
| 2492 | NewQType = ResQT; |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2493 | } |
| 2494 | |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2495 | QualType OldReturnType = OldType->getReturnType(); |
| 2496 | QualType NewReturnType = cast<FunctionType>(NewQType)->getReturnType(); |
| Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2497 | if (OldReturnType != NewReturnType) { |
| 2498 | // If this function has a deduced return type and has already been |
| 2499 | // defined, copy the deduced value from the old declaration. |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2500 | AutoType *OldAT = Old->getReturnType()->getContainedAutoType(); |
| Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2501 | if (OldAT && OldAT->isDeduced()) { |
| Richard Smith | c58f38f | 2013-08-14 20:16:31 +0000 | [diff] [blame] | 2502 | New->setType( |
| 2503 | SubstAutoType(New->getType(), |
| 2504 | OldAT->isDependentType() ? Context.DependentTy |
| 2505 | : OldAT->getDeducedType())); |
| Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2506 | NewQType = Context.getCanonicalType( |
| Richard Smith | c58f38f | 2013-08-14 20:16:31 +0000 | [diff] [blame] | 2507 | SubstAutoType(NewQType, |
| 2508 | OldAT->isDependentType() ? Context.DependentTy |
| 2509 | : OldAT->getDeducedType())); |
| Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2510 | } |
| 2511 | } |
| 2512 | |
| 2513 | const CXXMethodDecl *OldMethod = dyn_cast<CXXMethodDecl>(Old); |
| 2514 | CXXMethodDecl *NewMethod = dyn_cast<CXXMethodDecl>(New); |
| Anders Carlsson | 1f78b2b | 2009-12-04 22:33:25 +0000 | [diff] [blame] | 2515 | if (OldMethod && NewMethod) { |
| John McCall | 43314ab | 2010-04-13 07:45:41 +0000 | [diff] [blame] | 2516 | // Preserve triviality. |
| 2517 | NewMethod->setTrivial(OldMethod->isTrivial()); |
| Francois Pichet | c2fac71 | 2011-05-14 19:17:07 +0000 | [diff] [blame] | 2518 | |
| Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 2519 | // MSVC allows explicit template specialization at class scope: |
| Alp Toker | 8db6e7a | 2014-01-05 06:38:57 +0000 | [diff] [blame] | 2520 | // 2 CXXMethodDecls referring to the same function will be injected. |
| 2521 | // We don't want a redeclaration error. |
| Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 2522 | bool IsClassScopeExplicitSpecialization = |
| 2523 | OldMethod->isFunctionTemplateSpecialization() && |
| 2524 | NewMethod->isFunctionTemplateSpecialization(); |
| John McCall | 43314ab | 2010-04-13 07:45:41 +0000 | [diff] [blame] | 2525 | bool isFriend = NewMethod->getFriendObjectKind(); |
| 2526 | |
| Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 2527 | if (!isFriend && NewMethod->getLexicalDeclContext()->isRecord() && |
| 2528 | !IsClassScopeExplicitSpecialization) { |
| Anders Carlsson | 1f78b2b | 2009-12-04 22:33:25 +0000 | [diff] [blame] | 2529 | // -- Member function declarations with the same name and the |
| 2530 | // same parameter types cannot be overloaded if any of them |
| 2531 | // is a static member function declaration. |
| Eli Friedman | f26b81b | 2013-06-19 22:43:55 +0000 | [diff] [blame] | 2532 | if (OldMethod->isStatic() != NewMethod->isStatic()) { |
| Anders Carlsson | 1f78b2b | 2009-12-04 22:33:25 +0000 | [diff] [blame] | 2533 | Diag(New->getLocation(), diag::err_ovl_static_nonstatic_member); |
| Richard Smith | bdd1464 | 2014-02-04 01:14:30 +0000 | [diff] [blame] | 2534 | Diag(OldLocation, PrevDiag) << Old << Old->getType(); |
| Anders Carlsson | 1f78b2b | 2009-12-04 22:33:25 +0000 | [diff] [blame] | 2535 | return true; |
| 2536 | } |
| Richard Smith | 57e7ff9 | 2012-07-13 04:12:04 +0000 | [diff] [blame] | 2537 | |
| Anders Carlsson | 1f78b2b | 2009-12-04 22:33:25 +0000 | [diff] [blame] | 2538 | // C++ [class.mem]p1: |
| 2539 | // [...] A member shall not be declared twice in the |
| 2540 | // member-specification, except that a nested class or member |
| 2541 | // class template can be declared and then later defined. |
| Richard Smith | 57e7ff9 | 2012-07-13 04:12:04 +0000 | [diff] [blame] | 2542 | if (ActiveTemplateInstantiations.empty()) { |
| 2543 | unsigned NewDiag; |
| 2544 | if (isa<CXXConstructorDecl>(OldMethod)) |
| 2545 | NewDiag = diag::err_constructor_redeclared; |
| 2546 | else if (isa<CXXDestructorDecl>(NewMethod)) |
| 2547 | NewDiag = diag::err_destructor_redeclared; |
| 2548 | else if (isa<CXXConversionDecl>(NewMethod)) |
| 2549 | NewDiag = diag::err_conv_function_redeclared; |
| 2550 | else |
| 2551 | NewDiag = diag::err_member_redeclared; |
| Anders Carlsson | 1f78b2b | 2009-12-04 22:33:25 +0000 | [diff] [blame] | 2552 | |
| Richard Smith | 57e7ff9 | 2012-07-13 04:12:04 +0000 | [diff] [blame] | 2553 | Diag(New->getLocation(), NewDiag); |
| 2554 | } else { |
| 2555 | Diag(New->getLocation(), diag::err_member_redeclared_in_instantiation) |
| 2556 | << New << New->getType(); |
| 2557 | } |
| Richard Smith | bdd1464 | 2014-02-04 01:14:30 +0000 | [diff] [blame] | 2558 | Diag(OldLocation, PrevDiag) << Old << Old->getType(); |
| John McCall | 43314ab | 2010-04-13 07:45:41 +0000 | [diff] [blame] | 2559 | |
| 2560 | // Complain if this is an explicit declaration of a special |
| 2561 | // member that was initially declared implicitly. |
| 2562 | // |
| 2563 | // As an exception, it's okay to befriend such methods in order |
| 2564 | // to permit the implicit constructor/destructor/operator calls. |
| 2565 | } else if (OldMethod->isImplicit()) { |
| 2566 | if (isFriend) { |
| 2567 | NewMethod->setImplicit(); |
| 2568 | } else { |
| Anders Carlsson | 1f78b2b | 2009-12-04 22:33:25 +0000 | [diff] [blame] | 2569 | Diag(NewMethod->getLocation(), |
| 2570 | diag::err_definition_of_implicitly_declared_member) |
| Anders Carlsson | 05bf009 | 2010-04-22 05:40:53 +0000 | [diff] [blame] | 2571 | << New << getSpecialMember(OldMethod); |
| Anders Carlsson | 1f78b2b | 2009-12-04 22:33:25 +0000 | [diff] [blame] | 2572 | return true; |
| 2573 | } |
| Richard Smith | 337a5a1 | 2012-06-08 01:30:54 +0000 | [diff] [blame] | 2574 | } else if (OldMethod->isExplicitlyDefaulted() && !isFriend) { |
| Alexis Hunt | 6d5b96c | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 2575 | Diag(NewMethod->getLocation(), |
| 2576 | diag::err_definition_of_explicitly_defaulted_member) |
| 2577 | << getSpecialMember(OldMethod); |
| 2578 | return true; |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2579 | } |
| 2580 | } |
| 2581 | |
| Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 2582 | // C++11 [dcl.attr.noreturn]p1: |
| 2583 | // The first declaration of a function shall specify the noreturn |
| 2584 | // attribute if any declaration of that function specifies the noreturn |
| 2585 | // attribute. |
| Aaron Ballman | cf3b483 | 2013-12-19 13:36:16 +0000 | [diff] [blame] | 2586 | const CXX11NoReturnAttr *NRA = New->getAttr<CXX11NoReturnAttr>(); |
| 2587 | if (NRA && !Old->hasAttr<CXX11NoReturnAttr>()) { |
| 2588 | Diag(NRA->getLocation(), diag::err_noreturn_missing_on_first_decl); |
| Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 2589 | Diag(Old->getFirstDecl()->getLocation(), |
| Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 2590 | diag::note_noreturn_missing_first_decl); |
| 2591 | } |
| 2592 | |
| Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 2593 | // C++11 [dcl.attr.depend]p2: |
| 2594 | // The first declaration of a function shall specify the |
| 2595 | // carries_dependency attribute for its declarator-id if any declaration |
| 2596 | // of the function specifies the carries_dependency attribute. |
| Aaron Ballman | cf3b483 | 2013-12-19 13:36:16 +0000 | [diff] [blame] | 2597 | const CarriesDependencyAttr *CDA = New->getAttr<CarriesDependencyAttr>(); |
| 2598 | if (CDA && !Old->hasAttr<CarriesDependencyAttr>()) { |
| 2599 | Diag(CDA->getLocation(), |
| Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 2600 | diag::err_carries_dependency_missing_on_first_decl) << 0/*Function*/; |
| Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 2601 | Diag(Old->getFirstDecl()->getLocation(), |
| Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 2602 | diag::note_carries_dependency_missing_first_decl) << 0/*Function*/; |
| 2603 | } |
| 2604 | |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2605 | // (C++98 8.3.5p3): |
| 2606 | // All declarations for a function shall agree exactly in both the |
| 2607 | // return type and the parameter-type-list. |
| John McCall | 4f5019e | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 2608 | // We also want to respect all the extended bits except noreturn. |
| 2609 | |
| 2610 | // noreturn should now match unless the old type info didn't have it. |
| 2611 | QualType OldQTypeForComparison = OldQType; |
| 2612 | if (!OldTypeInfo.getNoReturn() && NewTypeInfo.getNoReturn()) { |
| 2613 | assert(OldQType == QualType(OldType, 0)); |
| 2614 | const FunctionType *OldTypeForComparison |
| 2615 | = Context.adjustFunctionType(OldType, OldTypeInfo.withNoReturn(true)); |
| 2616 | OldQTypeForComparison = QualType(OldTypeForComparison, 0); |
| 2617 | assert(OldQTypeForComparison.isCanonical()); |
| 2618 | } |
| 2619 | |
| Rafael Espindola | f418765 | 2013-02-14 01:18:37 +0000 | [diff] [blame] | 2620 | if (haveIncompatibleLanguageLinkages(Old, New)) { |
| Alp Toker | dd551fc | 2013-10-22 22:53:01 +0000 | [diff] [blame] | 2621 | // As a special case, retain the language linkage from previous |
| 2622 | // declarations of a friend function as an extension. |
| 2623 | // |
| 2624 | // This liberal interpretation of C++ [class.friend]p3 matches GCC/MSVC |
| 2625 | // and is useful because there's otherwise no way to specify language |
| 2626 | // linkage within class scope. |
| 2627 | // |
| 2628 | // Check cautiously as the friend object kind isn't yet complete. |
| 2629 | if (New->getFriendObjectKind() != Decl::FOK_None) { |
| 2630 | Diag(New->getLocation(), diag::ext_retained_language_linkage) << New; |
| Richard Smith | bdd1464 | 2014-02-04 01:14:30 +0000 | [diff] [blame] | 2631 | Diag(OldLocation, PrevDiag); |
| Alp Toker | dd551fc | 2013-10-22 22:53:01 +0000 | [diff] [blame] | 2632 | } else { |
| 2633 | Diag(New->getLocation(), diag::err_different_language_linkage) << New; |
| Richard Smith | bdd1464 | 2014-02-04 01:14:30 +0000 | [diff] [blame] | 2634 | Diag(OldLocation, PrevDiag); |
| Alp Toker | dd551fc | 2013-10-22 22:53:01 +0000 | [diff] [blame] | 2635 | return true; |
| 2636 | } |
| Rafael Espindola | cffa95d | 2012-12-27 03:56:20 +0000 | [diff] [blame] | 2637 | } |
| 2638 | |
| John McCall | 4f5019e | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 2639 | if (OldQTypeForComparison == NewQType) |
| Richard Smith | 1c34fb7 | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 2640 | return MergeCompatibleFunctionDecls(New, Old, S, MergeTypeWithOld); |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2641 | |
| Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 2642 | if ((NewQType->isDependentType() || OldQType->isDependentType()) && |
| 2643 | New->isLocalExternDecl()) { |
| 2644 | // It's OK if we couldn't merge types for a local function declaraton |
| 2645 | // if either the old or new type is dependent. We'll merge the types |
| 2646 | // when we instantiate the function. |
| 2647 | return false; |
| 2648 | } |
| 2649 | |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2650 | // Fall through for conflicting redeclarations and redefinitions. |
| Douglas Gregor | 89f238c | 2008-04-21 02:02:58 +0000 | [diff] [blame] | 2651 | } |
| Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2652 | |
| 2653 | // C: Function types need to be compatible, not identical. This handles |
| Steve Naroff | 012484d | 2008-01-14 20:51:29 +0000 | [diff] [blame] | 2654 | // duplicate function decls like "void f(int); void f(enum X);" properly. |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2655 | if (!getLangOpts().CPlusPlus && |
| Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2656 | Context.typesAreCompatible(OldQType, NewQType)) { |
| John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2657 | const FunctionType *OldFuncType = OldQType->getAs<FunctionType>(); |
| 2658 | const FunctionType *NewFuncType = NewQType->getAs<FunctionType>(); |
| Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2659 | const FunctionProtoType *OldProto = 0; |
| Richard Smith | 1c34fb7 | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 2660 | if (MergeTypeWithOld && isa<FunctionNoProtoType>(NewFuncType) && |
| Douglas Gregor | a74a297 | 2009-03-06 22:43:54 +0000 | [diff] [blame] | 2661 | (OldProto = dyn_cast<FunctionProtoType>(OldFuncType))) { |
| Douglas Gregor | bcbf863 | 2009-02-16 18:20:44 +0000 | [diff] [blame] | 2662 | // The old declaration provided a function prototype, but the |
| 2663 | // new declaration does not. Merge in the prototype. |
| Sebastian Redl | 5068f77ac | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2664 | assert(!OldProto->hasExceptionSpec() && "Exception spec in C"); |
| Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 2665 | SmallVector<QualType, 16> ParamTypes(OldProto->param_type_begin(), |
| 2666 | OldProto->param_type_end()); |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2667 | NewQType = |
| 2668 | Context.getFunctionType(NewFuncType->getReturnType(), ParamTypes, |
| 2669 | OldProto->getExtProtoInfo()); |
| Douglas Gregor | bcbf863 | 2009-02-16 18:20:44 +0000 | [diff] [blame] | 2670 | New->setType(NewQType); |
| Anders Carlsson | e0dd1d5 | 2009-05-14 21:46:00 +0000 | [diff] [blame] | 2671 | New->setHasInheritedPrototype(); |
| Douglas Gregor | bfdd607 | 2009-02-16 20:58:07 +0000 | [diff] [blame] | 2672 | |
| 2673 | // Synthesize a parameter for each argument type. |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2674 | SmallVector<ParmVarDecl*, 16> Params; |
| Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 2675 | for (FunctionProtoType::param_type_iterator |
| 2676 | ParamType = OldProto->param_type_begin(), |
| 2677 | ParamEnd = OldProto->param_type_end(); |
| Douglas Gregor | bfdd607 | 2009-02-16 20:58:07 +0000 | [diff] [blame] | 2678 | ParamType != ParamEnd; ++ParamType) { |
| 2679 | ParmVarDecl *Param = ParmVarDecl::Create(Context, New, |
| Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2680 | SourceLocation(), |
| Douglas Gregor | bfdd607 | 2009-02-16 20:58:07 +0000 | [diff] [blame] | 2681 | SourceLocation(), 0, |
| John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2682 | *ParamType, /*TInfo=*/0, |
| Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 2683 | SC_None, |
| Douglas Gregor | c4df407 | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 2684 | 0); |
| John McCall | 8fb0d9d | 2011-05-01 22:35:37 +0000 | [diff] [blame] | 2685 | Param->setScopeInfo(0, Params.size()); |
| Douglas Gregor | bfdd607 | 2009-02-16 20:58:07 +0000 | [diff] [blame] | 2686 | Param->setImplicit(); |
| 2687 | Params.push_back(Param); |
| 2688 | } |
| 2689 | |
| David Blaikie | 9c70e04 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 2690 | New->setParams(Params); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2691 | } |
| Douglas Gregor | bcbf863 | 2009-02-16 18:20:44 +0000 | [diff] [blame] | 2692 | |
| Richard Smith | 1c34fb7 | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 2693 | return MergeCompatibleFunctionDecls(New, Old, S, MergeTypeWithOld); |
| Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2694 | } |
| Chris Lattner | 45d561a | 2007-11-06 06:07:26 +0000 | [diff] [blame] | 2695 | |
| Douglas Gregor | a74a297 | 2009-03-06 22:43:54 +0000 | [diff] [blame] | 2696 | // GNU C permits a K&R definition to follow a prototype declaration |
| 2697 | // if the declared types of the parameters in the K&R definition |
| 2698 | // match the types in the prototype declaration, even when the |
| 2699 | // promoted types of the parameters from the K&R definition differ |
| 2700 | // from the types in the prototype. GCC then keeps the types from |
| 2701 | // the prototype. |
| Eli Friedman | fcbf7d2 | 2009-06-01 09:24:59 +0000 | [diff] [blame] | 2702 | // |
| 2703 | // If a variadic prototype is followed by a non-variadic K&R definition, |
| 2704 | // the K&R definition becomes variadic. This is sort of an edge case, but |
| 2705 | // it's legal per the standard depending on how you read C99 6.7.5.3p15 and |
| 2706 | // C99 6.9.1p8. |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2707 | if (!getLangOpts().CPlusPlus && |
| Douglas Gregor | a74a297 | 2009-03-06 22:43:54 +0000 | [diff] [blame] | 2708 | Old->hasPrototype() && !New->hasPrototype() && |
| John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2709 | New->getType()->getAs<FunctionProtoType>() && |
| Douglas Gregor | a74a297 | 2009-03-06 22:43:54 +0000 | [diff] [blame] | 2710 | Old->getNumParams() == New->getNumParams()) { |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2711 | SmallVector<QualType, 16> ArgTypes; |
| 2712 | SmallVector<GNUCompatibleParamWarning, 16> Warnings; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2713 | const FunctionProtoType *OldProto |
| John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2714 | = Old->getType()->getAs<FunctionProtoType>(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2715 | const FunctionProtoType *NewProto |
| John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2716 | = New->getType()->getAs<FunctionProtoType>(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2717 | |
| Douglas Gregor | a74a297 | 2009-03-06 22:43:54 +0000 | [diff] [blame] | 2718 | // Determine whether this is the GNU C extension. |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2719 | QualType MergedReturn = Context.mergeTypes(OldProto->getReturnType(), |
| 2720 | NewProto->getReturnType()); |
| Eli Friedman | fcbf7d2 | 2009-06-01 09:24:59 +0000 | [diff] [blame] | 2721 | bool LooseCompatible = !MergedReturn.isNull(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2722 | for (unsigned Idx = 0, End = Old->getNumParams(); |
| Eli Friedman | fcbf7d2 | 2009-06-01 09:24:59 +0000 | [diff] [blame] | 2723 | LooseCompatible && Idx != End; ++Idx) { |
| Douglas Gregor | a74a297 | 2009-03-06 22:43:54 +0000 | [diff] [blame] | 2724 | ParmVarDecl *OldParm = Old->getParamDecl(Idx); |
| 2725 | ParmVarDecl *NewParm = New->getParamDecl(Idx); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2726 | if (Context.typesAreCompatible(OldParm->getType(), |
| Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 2727 | NewProto->getParamType(Idx))) { |
| Douglas Gregor | a74a297 | 2009-03-06 22:43:54 +0000 | [diff] [blame] | 2728 | ArgTypes.push_back(NewParm->getType()); |
| 2729 | } else if (Context.typesAreCompatible(OldParm->getType(), |
| Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 2730 | NewParm->getType(), |
| 2731 | /*CompareUnqualified=*/true)) { |
| Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 2732 | GNUCompatibleParamWarning Warn = { OldParm, NewParm, |
| 2733 | NewProto->getParamType(Idx) }; |
| Douglas Gregor | a74a297 | 2009-03-06 22:43:54 +0000 | [diff] [blame] | 2734 | Warnings.push_back(Warn); |
| 2735 | ArgTypes.push_back(NewParm->getType()); |
| 2736 | } else |
| Eli Friedman | fcbf7d2 | 2009-06-01 09:24:59 +0000 | [diff] [blame] | 2737 | LooseCompatible = false; |
| Douglas Gregor | a74a297 | 2009-03-06 22:43:54 +0000 | [diff] [blame] | 2738 | } |
| 2739 | |
| Eli Friedman | fcbf7d2 | 2009-06-01 09:24:59 +0000 | [diff] [blame] | 2740 | if (LooseCompatible) { |
| Douglas Gregor | a74a297 | 2009-03-06 22:43:54 +0000 | [diff] [blame] | 2741 | for (unsigned Warn = 0; Warn < Warnings.size(); ++Warn) { |
| 2742 | Diag(Warnings[Warn].NewParm->getLocation(), |
| 2743 | diag::ext_param_promoted_not_compatible_with_prototype) |
| 2744 | << Warnings[Warn].PromotedType |
| 2745 | << Warnings[Warn].OldParm->getType(); |
| Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 2746 | if (Warnings[Warn].OldParm->getLocation().isValid()) |
| 2747 | Diag(Warnings[Warn].OldParm->getLocation(), |
| 2748 | diag::note_previous_declaration); |
| Douglas Gregor | a74a297 | 2009-03-06 22:43:54 +0000 | [diff] [blame] | 2749 | } |
| 2750 | |
| Richard Smith | 1c34fb7 | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 2751 | if (MergeTypeWithOld) |
| 2752 | New->setType(Context.getFunctionType(MergedReturn, ArgTypes, |
| 2753 | OldProto->getExtProtoInfo())); |
| 2754 | return MergeCompatibleFunctionDecls(New, Old, S, MergeTypeWithOld); |
| Douglas Gregor | a74a297 | 2009-03-06 22:43:54 +0000 | [diff] [blame] | 2755 | } |
| 2756 | |
| 2757 | // Fall through to diagnose conflicting types. |
| 2758 | } |
| 2759 | |
| John McCall | ad327cd | 2013-04-14 08:50:55 +0000 | [diff] [blame] | 2760 | // A function that has already been declared has been redeclared or |
| 2761 | // defined with a different type; show an appropriate diagnostic. |
| 2762 | |
| 2763 | // If the previous declaration was an implicitly-generated builtin |
| 2764 | // declaration, then at the very least we should use a specialized note. |
| 2765 | unsigned BuiltinID; |
| 2766 | if (Old->isImplicit() && (BuiltinID = Old->getBuiltinID())) { |
| 2767 | // If it's actually a library-defined builtin function like 'malloc' |
| 2768 | // or 'printf', just warn about the incompatible redeclaration. |
| Douglas Gregor | 75a45ba | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 2769 | if (Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)) { |
| Douglas Gregor | 75a45ba | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 2770 | Diag(New->getLocation(), diag::warn_redecl_library_builtin) << New; |
| Richard Smith | bdd1464 | 2014-02-04 01:14:30 +0000 | [diff] [blame] | 2771 | Diag(OldLocation, diag::note_previous_builtin_declaration) |
| Douglas Gregor | 75a45ba | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 2772 | << Old << Old->getType(); |
| John McCall | ad327cd | 2013-04-14 08:50:55 +0000 | [diff] [blame] | 2773 | |
| 2774 | // If this is a global redeclaration, just forget hereafter |
| 2775 | // about the "builtin-ness" of the function. |
| 2776 | // |
| 2777 | // Doing this for local extern declarations is problematic. If |
| 2778 | // the builtin declaration remains visible, a second invalid |
| 2779 | // local declaration will produce a hard error; if it doesn't |
| 2780 | // remain visible, a single bogus local redeclaration (which is |
| 2781 | // actually only a warning) could break all the downstream code. |
| Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 2782 | if (!New->getLexicalDeclContext()->isFunctionOrMethod()) |
| John McCall | ad327cd | 2013-04-14 08:50:55 +0000 | [diff] [blame] | 2783 | New->getIdentifier()->setBuiltinID(Builtin::NotBuiltin); |
| 2784 | |
| Douglas Gregor | 893c2c9 | 2009-03-23 17:47:24 +0000 | [diff] [blame] | 2785 | return false; |
| Douglas Gregor | 75a45ba | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 2786 | } |
| Steve Naroff | 17832a4 | 2008-01-16 15:01:34 +0000 | [diff] [blame] | 2787 | |
| Douglas Gregor | 75a45ba | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 2788 | PrevDiag = diag::note_previous_builtin_declaration; |
| 2789 | } |
| 2790 | |
| Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2791 | Diag(New->getLocation(), diag::err_conflicting_types) << New->getDeclName(); |
| Richard Smith | bdd1464 | 2014-02-04 01:14:30 +0000 | [diff] [blame] | 2792 | Diag(OldLocation, PrevDiag) << Old << Old->getType(); |
| Douglas Gregor | 75a45ba | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 2793 | return true; |
| Chris Lattner | 01564d9 | 2007-01-27 19:27:06 +0000 | [diff] [blame] | 2794 | } |
| 2795 | |
| Douglas Gregor | e62c0a4 | 2009-02-24 01:23:02 +0000 | [diff] [blame] | 2796 | /// \brief Completes the merge of two function declarations that are |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2797 | /// known to be compatible. |
| Douglas Gregor | e62c0a4 | 2009-02-24 01:23:02 +0000 | [diff] [blame] | 2798 | /// |
| 2799 | /// This routine handles the merging of attributes and other |
| Alp Toker | 5f6b8ac | 2013-10-22 09:00:49 +0000 | [diff] [blame] | 2800 | /// properties of function declarations from the old declaration to |
| Douglas Gregor | e62c0a4 | 2009-02-24 01:23:02 +0000 | [diff] [blame] | 2801 | /// the new declaration, once we know that New is in fact a |
| 2802 | /// redeclaration of Old. |
| 2803 | /// |
| 2804 | /// \returns false |
| James Molloy | e943003 | 2012-03-13 08:55:35 +0000 | [diff] [blame] | 2805 | bool Sema::MergeCompatibleFunctionDecls(FunctionDecl *New, FunctionDecl *Old, |
| Richard Smith | 1c34fb7 | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 2806 | Scope *S, bool MergeTypeWithOld) { |
| Douglas Gregor | e62c0a4 | 2009-02-24 01:23:02 +0000 | [diff] [blame] | 2807 | // Merge the attributes |
| Douglas Gregor | 32c1757 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 2808 | mergeDeclAttributes(New, Old); |
| Douglas Gregor | e62c0a4 | 2009-02-24 01:23:02 +0000 | [diff] [blame] | 2809 | |
| Douglas Gregor | e62c0a4 | 2009-02-24 01:23:02 +0000 | [diff] [blame] | 2810 | // Merge "pure" flag. |
| 2811 | if (Old->isPure()) |
| 2812 | New->setPure(); |
| 2813 | |
| Rafael Espindola | befe130 | 2012-11-25 14:07:59 +0000 | [diff] [blame] | 2814 | // Merge "used" flag. |
| Rafael Espindola | e4865d2 | 2013-10-23 16:46:34 +0000 | [diff] [blame] | 2815 | if (Old->getMostRecentDecl()->isUsed(false)) |
| 2816 | New->setIsUsed(); |
| Rafael Espindola | befe130 | 2012-11-25 14:07:59 +0000 | [diff] [blame] | 2817 | |
| John McCall | f79e87d | 2011-03-02 04:00:57 +0000 | [diff] [blame] | 2818 | // Merge attributes from the parameters. These can mismatch with K&R |
| 2819 | // declarations. |
| 2820 | if (New->getNumParams() == Old->getNumParams()) |
| 2821 | for (unsigned i = 0, e = New->getNumParams(); i != e; ++i) |
| 2822 | mergeParamDeclAttributes(New->getParamDecl(i), Old->getParamDecl(i), |
| Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 2823 | *this); |
| John McCall | f79e87d | 2011-03-02 04:00:57 +0000 | [diff] [blame] | 2824 | |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2825 | if (getLangOpts().CPlusPlus) |
| James Molloy | e943003 | 2012-03-13 08:55:35 +0000 | [diff] [blame] | 2826 | return MergeCXXFunctionDecl(New, Old, S); |
| Douglas Gregor | e62c0a4 | 2009-02-24 01:23:02 +0000 | [diff] [blame] | 2827 | |
| Rafael Espindola | 8778c28 | 2012-11-29 16:09:03 +0000 | [diff] [blame] | 2828 | // Merge the function types so the we get the composite types for the return |
| Richard Smith | 1c34fb7 | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 2829 | // and argument types. Per C11 6.2.7/4, only update the type if the old decl |
| 2830 | // was visible. |
| Rafael Espindola | 8778c28 | 2012-11-29 16:09:03 +0000 | [diff] [blame] | 2831 | QualType Merged = Context.mergeTypes(Old->getType(), New->getType()); |
| Richard Smith | 1c34fb7 | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 2832 | if (!Merged.isNull() && MergeTypeWithOld) |
| Rafael Espindola | 8778c28 | 2012-11-29 16:09:03 +0000 | [diff] [blame] | 2833 | New->setType(Merged); |
| 2834 | |
| Douglas Gregor | e62c0a4 | 2009-02-24 01:23:02 +0000 | [diff] [blame] | 2835 | return false; |
| 2836 | } |
| 2837 | |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2838 | |
| John McCall | f79e87d | 2011-03-02 04:00:57 +0000 | [diff] [blame] | 2839 | void Sema::mergeObjCMethodDecls(ObjCMethodDecl *newMethod, |
| Douglas Gregor | 32c1757 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 2840 | ObjCMethodDecl *oldMethod) { |
| John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 2841 | |
| Fariborz Jahanian | 3da28f8 | 2012-06-05 21:14:46 +0000 | [diff] [blame] | 2842 | // Merge the attributes, including deprecated/unavailable |
| Ted Kremenek | b544572 | 2013-04-06 00:34:27 +0000 | [diff] [blame] | 2843 | AvailabilityMergeKind MergeKind = |
| 2844 | isa<ObjCImplDecl>(newMethod->getDeclContext()) ? AMK_Redeclaration |
| 2845 | : AMK_Override; |
| 2846 | mergeDeclAttributes(newMethod, oldMethod, MergeKind); |
| John McCall | f79e87d | 2011-03-02 04:00:57 +0000 | [diff] [blame] | 2847 | |
| 2848 | // Merge attributes from the parameters. |
| Douglas Gregor | 0bf70f4 | 2012-05-17 23:13:29 +0000 | [diff] [blame] | 2849 | ObjCMethodDecl::param_const_iterator oi = oldMethod->param_begin(), |
| 2850 | oe = oldMethod->param_end(); |
| Argyrios Kyrtzidis | b8c3aaf | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 2851 | for (ObjCMethodDecl::param_iterator |
| John McCall | f79e87d | 2011-03-02 04:00:57 +0000 | [diff] [blame] | 2852 | ni = newMethod->param_begin(), ne = newMethod->param_end(); |
| Douglas Gregor | 0bf70f4 | 2012-05-17 23:13:29 +0000 | [diff] [blame] | 2853 | ni != ne && oi != oe; ++ni, ++oi) |
| Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 2854 | mergeParamDeclAttributes(*ni, *oi, *this); |
| John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 2855 | |
| Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2856 | CheckObjCMethodOverride(newMethod, oldMethod); |
| John McCall | f79e87d | 2011-03-02 04:00:57 +0000 | [diff] [blame] | 2857 | } |
| 2858 | |
| Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 2859 | /// MergeVarDeclTypes - We parsed a variable 'New' which has the same name and |
| 2860 | /// scope as a previous declaration 'Old'. Figure out how to merge their types, |
| Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 2861 | /// emitting diagnostics as appropriate. |
| 2862 | /// |
| 2863 | /// Declarations using the auto type specifier (C++ [decl.spec.auto]) call back |
| Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2864 | /// to here in AddInitializerToDecl. We can't check them before the initializer |
| 2865 | /// is attached. |
| Richard Smith | 1c34fb7 | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 2866 | void Sema::MergeVarDeclTypes(VarDecl *New, VarDecl *Old, |
| 2867 | bool MergeTypeWithOld) { |
| Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 2868 | if (New->isInvalidDecl() || Old->isInvalidDecl()) |
| 2869 | return; |
| 2870 | |
| 2871 | QualType MergedT; |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2872 | if (getLangOpts().CPlusPlus) { |
| Richard Smith | 27d807c | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 2873 | if (New->getType()->isUndeducedType()) { |
| Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 2874 | // We don't know what the new type is until the initializer is attached. |
| 2875 | return; |
| Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 2876 | } else if (Context.hasSameType(New->getType(), Old->getType())) { |
| 2877 | // These could still be something that needs exception specs checked. |
| 2878 | return MergeVarDeclExceptionSpecs(New, Old); |
| 2879 | } |
| Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 2880 | // C++ [basic.link]p10: |
| 2881 | // [...] the types specified by all declarations referring to a given |
| 2882 | // object or function shall be identical, except that declarations for an |
| 2883 | // array object can specify array types that differ by the presence or |
| 2884 | // absence of a major array bound (8.3.4). |
| 2885 | else if (Old->getType()->isIncompleteArrayType() && |
| 2886 | New->getType()->isArrayType()) { |
| Eli Friedman | 07bab73 | 2012-12-13 01:43:21 +0000 | [diff] [blame] | 2887 | const ArrayType *OldArray = Context.getAsArrayType(Old->getType()); |
| 2888 | const ArrayType *NewArray = Context.getAsArrayType(New->getType()); |
| 2889 | if (Context.hasSameType(OldArray->getElementType(), |
| 2890 | NewArray->getElementType())) |
| Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 2891 | MergedT = New->getType(); |
| 2892 | } else if (Old->getType()->isArrayType() && |
| Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 2893 | New->getType()->isIncompleteArrayType()) { |
| Eli Friedman | 07bab73 | 2012-12-13 01:43:21 +0000 | [diff] [blame] | 2894 | const ArrayType *OldArray = Context.getAsArrayType(Old->getType()); |
| 2895 | const ArrayType *NewArray = Context.getAsArrayType(New->getType()); |
| 2896 | if (Context.hasSameType(OldArray->getElementType(), |
| 2897 | NewArray->getElementType())) |
| Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 2898 | MergedT = Old->getType(); |
| Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 2899 | } else if (New->getType()->isObjCObjectPointerType() && |
| 2900 | Old->getType()->isObjCObjectPointerType()) { |
| 2901 | MergedT = Context.mergeObjCGCQualifiers(New->getType(), |
| 2902 | Old->getType()); |
| Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 2903 | } |
| 2904 | } else { |
| Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 2905 | // C 6.2.7p2: |
| 2906 | // All declarations that refer to the same object or function shall have |
| 2907 | // compatible type. |
| Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 2908 | MergedT = Context.mergeTypes(New->getType(), Old->getType()); |
| 2909 | } |
| 2910 | if (MergedT.isNull()) { |
| Richard Smith | 1c34fb7 | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 2911 | // It's OK if we couldn't merge types if either type is dependent, for a |
| 2912 | // block-scope variable. In other cases (static data members of class |
| 2913 | // templates, variable templates, ...), we require the types to be |
| 2914 | // equivalent. |
| 2915 | // FIXME: The C++ standard doesn't say anything about this. |
| 2916 | if ((New->getType()->isDependentType() || |
| 2917 | Old->getType()->isDependentType()) && New->isLocalVarDecl()) { |
| 2918 | // If the old type was dependent, we can't merge with it, so the new type |
| 2919 | // becomes dependent for now. We'll reproduce the original type when we |
| 2920 | // instantiate the TypeSourceInfo for the variable. |
| 2921 | if (!New->getType()->isDependentType() && MergeTypeWithOld) |
| 2922 | New->setType(Context.DependentTy); |
| 2923 | return; |
| 2924 | } |
| 2925 | |
| 2926 | // FIXME: Even if this merging succeeds, some other non-visible declaration |
| 2927 | // of this variable might have an incompatible type. For instance: |
| 2928 | // |
| 2929 | // extern int arr[]; |
| 2930 | // void f() { extern int arr[2]; } |
| 2931 | // void g() { extern int arr[3]; } |
| 2932 | // |
| 2933 | // Neither C nor C++ requires a diagnostic for this, but we should still try |
| 2934 | // to diagnose it. |
| Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 2935 | Diag(New->getLocation(), diag::err_redefinition_different_type) |
| David Blaikie | 6590220 | 2012-09-20 18:38:57 +0000 | [diff] [blame] | 2936 | << New->getDeclName() << New->getType() << Old->getType(); |
| Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 2937 | Diag(Old->getLocation(), diag::note_previous_definition); |
| 2938 | return New->setInvalidDecl(); |
| 2939 | } |
| John McCall | b65e8fe | 2013-04-01 18:34:28 +0000 | [diff] [blame] | 2940 | |
| 2941 | // Don't actually update the type on the new declaration if the old |
| Richard Smith | 3c78578 | 2013-09-03 21:00:58 +0000 | [diff] [blame] | 2942 | // declaration was an extern declaration in a different scope. |
| Richard Smith | 1c34fb7 | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 2943 | if (MergeTypeWithOld) |
| John McCall | b65e8fe | 2013-04-01 18:34:28 +0000 | [diff] [blame] | 2944 | New->setType(MergedT); |
| Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 2945 | } |
| 2946 | |
| Richard Smith | 3c78578 | 2013-09-03 21:00:58 +0000 | [diff] [blame] | 2947 | static bool mergeTypeWithPrevious(Sema &S, VarDecl *NewVD, VarDecl *OldVD, |
| 2948 | LookupResult &Previous) { |
| 2949 | // C11 6.2.7p4: |
| 2950 | // For an identifier with internal or external linkage declared |
| 2951 | // in a scope in which a prior declaration of that identifier is |
| 2952 | // visible, if the prior declaration specifies internal or |
| 2953 | // external linkage, the type of the identifier at the later |
| 2954 | // declaration becomes the composite type. |
| 2955 | // |
| 2956 | // If the variable isn't visible, we do not merge with its type. |
| 2957 | if (Previous.isShadowed()) |
| 2958 | return false; |
| 2959 | |
| 2960 | if (S.getLangOpts().CPlusPlus) { |
| 2961 | // C++11 [dcl.array]p3: |
| 2962 | // If there is a preceding declaration of the entity in the same |
| 2963 | // scope in which the bound was specified, an omitted array bound |
| 2964 | // is taken to be the same as in that earlier declaration. |
| 2965 | return NewVD->isPreviousDeclInSameBlockScope() || |
| 2966 | (!OldVD->getLexicalDeclContext()->isFunctionOrMethod() && |
| 2967 | !NewVD->getLexicalDeclContext()->isFunctionOrMethod()); |
| 2968 | } else { |
| 2969 | // If the old declaration was function-local, don't merge with its |
| 2970 | // type unless we're in the same function. |
| 2971 | return !OldVD->getLexicalDeclContext()->isFunctionOrMethod() || |
| 2972 | OldVD->getLexicalDeclContext() == NewVD->getLexicalDeclContext(); |
| 2973 | } |
| 2974 | } |
| 2975 | |
| Chris Lattner | 01564d9 | 2007-01-27 19:27:06 +0000 | [diff] [blame] | 2976 | /// MergeVarDecl - We just parsed a variable 'New' which has the same name |
| 2977 | /// and scope as a previous declaration 'Old'. Figure out how to resolve this |
| 2978 | /// situation, merging decls or emitting diagnostics as appropriate. |
| 2979 | /// |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2980 | /// Tentative definition rules (C99 6.9.2p2) are checked by |
| 2981 | /// FinalizeDeclaratorGroup. Unfortunately, we can't analyze tentative |
| Steve Naroff | 5bb8f22 | 2008-08-08 17:50:35 +0000 | [diff] [blame] | 2982 | /// definitions here, since the initializer hasn't been attached. |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2983 | /// |
| Richard Smith | 3c78578 | 2013-09-03 21:00:58 +0000 | [diff] [blame] | 2984 | void Sema::MergeVarDecl(VarDecl *New, LookupResult &Previous) { |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 2985 | // If the new decl is already invalid, don't do any other checking. |
| 2986 | if (New->isInvalidDecl()) |
| 2987 | return; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2988 | |
| Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 2989 | VarTemplateDecl *NewTemplate = New->getDescribedVarTemplate(); |
| 2990 | |
| Larisse Voufo | d8dd97c | 2013-08-14 03:09:19 +0000 | [diff] [blame] | 2991 | // Verify the old decl was also a variable or variable template. |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 2992 | VarDecl *Old = 0; |
| Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 2993 | VarTemplateDecl *OldTemplate = 0; |
| 2994 | if (Previous.isSingleResult()) { |
| 2995 | if (NewTemplate) { |
| 2996 | OldTemplate = dyn_cast<VarTemplateDecl>(Previous.getFoundDecl()); |
| 2997 | Old = OldTemplate ? OldTemplate->getTemplatedDecl() : 0; |
| 2998 | } else |
| 2999 | Old = dyn_cast<VarDecl>(Previous.getFoundDecl()); |
| Larisse Voufo | d8dd97c | 2013-08-14 03:09:19 +0000 | [diff] [blame] | 3000 | } |
| 3001 | if (!Old) { |
| Chris Lattner | 651d42d | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 3002 | Diag(New->getLocation(), diag::err_redefinition_different_kind) |
| Chris Lattner | e3d20d9 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 3003 | << New->getDeclName(); |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 3004 | Diag(Previous.getRepresentativeDecl()->getLocation(), |
| 3005 | diag::note_previous_definition); |
| Chris Lattner | f6d1c9c | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 3006 | return New->setInvalidDecl(); |
| Chris Lattner | c511efb | 2007-01-27 19:32:14 +0000 | [diff] [blame] | 3007 | } |
| Chris Lattner | 8496639 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 3008 | |
| Rafael Espindola | 5bddd6a | 2013-04-15 12:49:13 +0000 | [diff] [blame] | 3009 | if (!shouldLinkPossiblyHiddenDecl(Old, New)) |
| 3010 | return; |
| 3011 | |
| Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 3012 | // Ensure the template parameters are compatible. |
| 3013 | if (NewTemplate && |
| 3014 | !TemplateParameterListsAreEqual(NewTemplate->getTemplateParameters(), |
| 3015 | OldTemplate->getTemplateParameters(), |
| 3016 | /*Complain=*/true, TPL_TemplateMatch)) |
| 3017 | return; |
| 3018 | |
| Douglas Gregor | 2c7d929 | 2010-08-30 14:32:14 +0000 | [diff] [blame] | 3019 | // C++ [class.mem]p1: |
| 3020 | // A member shall not be declared twice in the member-specification [...] |
| 3021 | // |
| 3022 | // Here, we need only consider static data members. |
| 3023 | if (Old->isStaticDataMember() && !New->isOutOfLine()) { |
| 3024 | Diag(New->getLocation(), diag::err_duplicate_member) |
| 3025 | << New->getIdentifier(); |
| 3026 | Diag(Old->getLocation(), diag::note_previous_declaration); |
| 3027 | New->setInvalidDecl(); |
| 3028 | } |
| 3029 | |
| Douglas Gregor | 32c1757 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 3030 | mergeDeclAttributes(New, Old); |
| David Blaikie | 30d1544 | 2011-10-19 22:56:21 +0000 | [diff] [blame] | 3031 | // Warn if an already-declared variable is made a weak_import in a subsequent |
| 3032 | // declaration |
| Aaron Ballman | 9ead124 | 2013-12-19 02:39:40 +0000 | [diff] [blame] | 3033 | if (New->hasAttr<WeakImportAttr>() && |
| Fariborz Jahanian | ab578bf | 2011-06-20 17:50:03 +0000 | [diff] [blame] | 3034 | Old->getStorageClass() == SC_None && |
| Aaron Ballman | 9ead124 | 2013-12-19 02:39:40 +0000 | [diff] [blame] | 3035 | !Old->hasAttr<WeakImportAttr>()) { |
| Fariborz Jahanian | 33e0226 | 2011-06-22 22:08:50 +0000 | [diff] [blame] | 3036 | Diag(New->getLocation(), diag::warn_weak_import) << New->getDeclName(); |
| 3037 | Diag(Old->getLocation(), diag::note_previous_definition); |
| 3038 | // Remove weak_import attribute on new declaration. |
| Fariborz Jahanian | 0dfc950 | 2011-06-23 17:50:10 +0000 | [diff] [blame] | 3039 | New->dropAttr<WeakImportAttr>(); |
| Fariborz Jahanian | 33e0226 | 2011-06-22 22:08:50 +0000 | [diff] [blame] | 3040 | } |
| Chris Lattner | 8496639 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 3041 | |
| Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3042 | // Merge the types. |
| Richard Smith | 3c78578 | 2013-09-03 21:00:58 +0000 | [diff] [blame] | 3043 | MergeVarDeclTypes(New, Old, mergeTypeWithPrevious(*this, New, Old, Previous)); |
| 3044 | |
| Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3045 | if (New->isInvalidDecl()) |
| 3046 | return; |
| Douglas Gregor | 04e9a03 | 2009-03-11 23:52:16 +0000 | [diff] [blame] | 3047 | |
| Rafael Espindola | 8ac2f59 | 2013-04-04 21:21:25 +0000 | [diff] [blame] | 3048 | // [dcl.stc]p8: Check if we have a non-static decl followed by a static. |
| John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 3049 | if (New->getStorageClass() == SC_Static && |
| Rafael Espindola | 8ac2f59 | 2013-04-04 21:21:25 +0000 | [diff] [blame] | 3050 | !New->isStaticDataMember() && |
| Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 3051 | Old->hasExternalFormalLinkage()) { |
| Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3052 | Diag(New->getLocation(), diag::err_static_non_static) << New->getDeclName(); |
| Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 3053 | Diag(Old->getLocation(), diag::note_previous_definition); |
| Chris Lattner | f6d1c9c | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 3054 | return New->setInvalidDecl(); |
| Steve Naroff | 1e78736 | 2008-01-30 00:44:01 +0000 | [diff] [blame] | 3055 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3056 | // C99 6.2.2p4: |
| Douglas Gregor | 3731162 | 2009-03-19 22:01:50 +0000 | [diff] [blame] | 3057 | // For an identifier declared with the storage-class specifier |
| 3058 | // extern in a scope in which a prior declaration of that |
| 3059 | // identifier is visible,23) if the prior declaration specifies |
| 3060 | // internal or external linkage, the linkage of the identifier at |
| 3061 | // the later declaration is the same as the linkage specified at |
| 3062 | // the prior declaration. If no prior declaration is visible, or |
| 3063 | // if the prior declaration specifies no linkage, then the |
| 3064 | // identifier has external linkage. |
| Douglas Gregor | d4eca01 | 2009-03-23 16:17:01 +0000 | [diff] [blame] | 3065 | if (New->hasExternalStorage() && Old->hasLinkage()) |
| Douglas Gregor | 3731162 | 2009-03-19 22:01:50 +0000 | [diff] [blame] | 3066 | /* Okay */; |
| Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 3067 | else if (New->getCanonicalDecl()->getStorageClass() != SC_Static && |
| Rafael Espindola | 8ac2f59 | 2013-04-04 21:21:25 +0000 | [diff] [blame] | 3068 | !New->isStaticDataMember() && |
| Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 3069 | Old->getCanonicalDecl()->getStorageClass() == SC_Static) { |
| Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3070 | Diag(New->getLocation(), diag::err_non_static_static) << New->getDeclName(); |
| Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 3071 | Diag(Old->getLocation(), diag::note_previous_definition); |
| Chris Lattner | f6d1c9c | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 3072 | return New->setInvalidDecl(); |
| Steve Naroff | 1e78736 | 2008-01-30 00:44:01 +0000 | [diff] [blame] | 3073 | } |
| Daniel Dunbar | 0ca1660 | 2009-04-14 02:25:56 +0000 | [diff] [blame] | 3074 | |
| Argyrios Kyrtzidis | 819f610 | 2011-01-31 07:04:46 +0000 | [diff] [blame] | 3075 | // Check if extern is followed by non-extern and vice-versa. |
| 3076 | if (New->hasExternalStorage() && |
| 3077 | !Old->hasLinkage() && Old->isLocalVarDecl()) { |
| 3078 | Diag(New->getLocation(), diag::err_extern_non_extern) << New->getDeclName(); |
| 3079 | Diag(Old->getLocation(), diag::note_previous_definition); |
| 3080 | return New->setInvalidDecl(); |
| 3081 | } |
| Rafael Espindola | 869fe04 | 2013-04-04 02:47:57 +0000 | [diff] [blame] | 3082 | if (Old->hasLinkage() && New->isLocalVarDecl() && |
| 3083 | !New->hasExternalStorage()) { |
| Argyrios Kyrtzidis | 819f610 | 2011-01-31 07:04:46 +0000 | [diff] [blame] | 3084 | Diag(New->getLocation(), diag::err_non_extern_extern) << New->getDeclName(); |
| 3085 | Diag(Old->getLocation(), diag::note_previous_definition); |
| 3086 | return New->setInvalidDecl(); |
| 3087 | } |
| 3088 | |
| Steve Naroff | a562937 | 2008-09-17 14:05:40 +0000 | [diff] [blame] | 3089 | // Variables with external linkage are analyzed in FinalizeDeclaratorGroup. |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3090 | |
| Daniel Dunbar | 0ca1660 | 2009-04-14 02:25:56 +0000 | [diff] [blame] | 3091 | // FIXME: The test for external storage here seems wrong? We still |
| 3092 | // need to check for mismatches. |
| 3093 | if (!New->hasExternalStorage() && !New->isFileVarDecl() && |
| Douglas Gregor | 04e9a03 | 2009-03-11 23:52:16 +0000 | [diff] [blame] | 3094 | // Don't complain about out-of-line definitions of static members. |
| 3095 | !(Old->getLexicalDeclContext()->isRecord() && |
| 3096 | !New->getLexicalDeclContext()->isRecord())) { |
| Chris Lattner | e3d20d9 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 3097 | Diag(New->getLocation(), diag::err_redefinition) << New->getDeclName(); |
| Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 3098 | Diag(Old->getLocation(), diag::note_previous_definition); |
| Chris Lattner | f6d1c9c | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 3099 | return New->setInvalidDecl(); |
| Steve Naroff | 6fbf0dc | 2007-03-16 00:33:25 +0000 | [diff] [blame] | 3100 | } |
| Douglas Gregor | 0760fa1 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 3101 | |
| Richard Smith | fd3834f | 2013-04-13 02:43:54 +0000 | [diff] [blame] | 3102 | if (New->getTLSKind() != Old->getTLSKind()) { |
| 3103 | if (!Old->getTLSKind()) { |
| 3104 | Diag(New->getLocation(), diag::err_thread_non_thread) << New->getDeclName(); |
| 3105 | Diag(Old->getLocation(), diag::note_previous_declaration); |
| 3106 | } else if (!New->getTLSKind()) { |
| 3107 | Diag(New->getLocation(), diag::err_non_thread_thread) << New->getDeclName(); |
| 3108 | Diag(Old->getLocation(), diag::note_previous_declaration); |
| 3109 | } else { |
| 3110 | // Do not allow redeclaration to change the variable between requiring |
| 3111 | // static and dynamic initialization. |
| 3112 | // FIXME: GCC allows this, but uses the TLS keyword on the first |
| 3113 | // declaration to determine the kind. Do we need to be compatible here? |
| 3114 | Diag(New->getLocation(), diag::err_thread_thread_different_kind) |
| 3115 | << New->getDeclName() << (New->getTLSKind() == VarDecl::TLS_Dynamic); |
| 3116 | Diag(Old->getLocation(), diag::note_previous_declaration); |
| 3117 | } |
| Eli Friedman | d5c0eed | 2009-04-19 20:27:55 +0000 | [diff] [blame] | 3118 | } |
| 3119 | |
| Sebastian Redl | f184291 | 2010-02-02 18:35:11 +0000 | [diff] [blame] | 3120 | // C++ doesn't have tentative definitions, so go right ahead and check here. |
| 3121 | const VarDecl *Def; |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3122 | if (getLangOpts().CPlusPlus && |
| Sebastian Redl | d85be0c | 2010-02-03 02:08:48 +0000 | [diff] [blame] | 3123 | New->isThisDeclarationADefinition() == VarDecl::Definition && |
| Sebastian Redl | f184291 | 2010-02-02 18:35:11 +0000 | [diff] [blame] | 3124 | (Def = Old->getDefinition())) { |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3125 | Diag(New->getLocation(), diag::err_redefinition) << New; |
| Sebastian Redl | f184291 | 2010-02-02 18:35:11 +0000 | [diff] [blame] | 3126 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 3127 | New->setInvalidDecl(); |
| 3128 | return; |
| 3129 | } |
| Rafael Espindola | cffa95d | 2012-12-27 03:56:20 +0000 | [diff] [blame] | 3130 | |
| Rafael Espindola | f418765 | 2013-02-14 01:18:37 +0000 | [diff] [blame] | 3131 | if (haveIncompatibleLanguageLinkages(Old, New)) { |
| Rafael Espindola | cffa95d | 2012-12-27 03:56:20 +0000 | [diff] [blame] | 3132 | Diag(New->getLocation(), diag::err_different_language_linkage) << New; |
| 3133 | Diag(Old->getLocation(), diag::note_previous_definition); |
| 3134 | New->setInvalidDecl(); |
| 3135 | return; |
| 3136 | } |
| 3137 | |
| Rafael Espindola | befe130 | 2012-11-25 14:07:59 +0000 | [diff] [blame] | 3138 | // Merge "used" flag. |
| Rafael Espindola | e4865d2 | 2013-10-23 16:46:34 +0000 | [diff] [blame] | 3139 | if (Old->getMostRecentDecl()->isUsed(false)) |
| 3140 | New->setIsUsed(); |
| Rafael Espindola | befe130 | 2012-11-25 14:07:59 +0000 | [diff] [blame] | 3141 | |
| Douglas Gregor | 0760fa1 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 3142 | // Keep a chain of previous declarations. |
| Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 3143 | New->setPreviousDecl(Old); |
| Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 3144 | if (NewTemplate) |
| 3145 | NewTemplate->setPreviousDecl(OldTemplate); |
| John McCall | 401982f | 2010-01-20 21:53:11 +0000 | [diff] [blame] | 3146 | |
| 3147 | // Inherit access appropriately. |
| 3148 | New->setAccess(Old->getAccess()); |
| Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 3149 | if (NewTemplate) |
| 3150 | NewTemplate->setAccess(New->getAccess()); |
| Chris Lattner | 01564d9 | 2007-01-27 19:27:06 +0000 | [diff] [blame] | 3151 | } |
| 3152 | |
| Chris Lattner | b6738ec | 2007-01-28 00:38:24 +0000 | [diff] [blame] | 3153 | /// ParsedFreeStandingDeclSpec - This method is invoked when a declspec with |
| 3154 | /// no declarator (e.g. "struct foo;") is parsed. |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3155 | Decl *Sema::ParsedFreeStandingDeclSpec(Scope *S, AccessSpecifier AS, |
| John McCall | aa01737 | 2011-03-22 23:00:04 +0000 | [diff] [blame] | 3156 | DeclSpec &DS) { |
| Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 3157 | return ParsedFreeStandingDeclSpec(S, AS, DS, MultiTemplateParamsArg()); |
| Chandler Carruth | 7c9856d | 2011-05-03 18:35:10 +0000 | [diff] [blame] | 3158 | } |
| 3159 | |
| David Majnemer | 2206bf5 | 2014-03-05 08:57:59 +0000 | [diff] [blame] | 3160 | static void HandleTagNumbering(Sema &S, const TagDecl *Tag, Scope *TagScope) { |
| Reid Kleckner | d8110b6 | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 3161 | if (!S.Context.getLangOpts().CPlusPlus) |
| 3162 | return; |
| 3163 | |
| Eli Friedman | 3b7d46c | 2013-07-10 00:30:46 +0000 | [diff] [blame] | 3164 | if (isa<CXXRecordDecl>(Tag->getParent())) { |
| 3165 | // If this tag is the direct child of a class, number it if |
| 3166 | // it is anonymous. |
| 3167 | if (!Tag->getName().empty() || Tag->getTypedefNameForAnonDecl()) |
| 3168 | return; |
| 3169 | MangleNumberingContext &MCtx = |
| 3170 | S.Context.getManglingNumberContext(Tag->getParent()); |
| David Majnemer | f27217f | 2014-03-05 18:55:38 +0000 | [diff] [blame] | 3171 | S.Context.setManglingNumber( |
| 3172 | Tag, MCtx.getManglingNumber(Tag, TagScope->getMSLocalManglingNumber())); |
| Eli Friedman | 3b7d46c | 2013-07-10 00:30:46 +0000 | [diff] [blame] | 3173 | return; |
| 3174 | } |
| 3175 | |
| 3176 | // If this tag isn't a direct child of a class, number it if it is local. |
| 3177 | Decl *ManglingContextDecl; |
| 3178 | if (MangleNumberingContext *MCtx = |
| 3179 | S.getCurrentMangleNumberContext(Tag->getDeclContext(), |
| 3180 | ManglingContextDecl)) { |
| David Majnemer | f27217f | 2014-03-05 18:55:38 +0000 | [diff] [blame] | 3181 | S.Context.setManglingNumber( |
| 3182 | Tag, |
| 3183 | MCtx->getManglingNumber(Tag, TagScope->getMSLocalManglingNumber())); |
| Eli Friedman | 3b7d46c | 2013-07-10 00:30:46 +0000 | [diff] [blame] | 3184 | } |
| 3185 | } |
| 3186 | |
| Chandler Carruth | 7c9856d | 2011-05-03 18:35:10 +0000 | [diff] [blame] | 3187 | /// ParsedFreeStandingDeclSpec - This method is invoked when a declspec with |
| Richard Smith | b1402ae | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 3188 | /// no declarator (e.g. "struct foo;") is parsed. It also accepts template |
| Chandler Carruth | 7c9856d | 2011-05-03 18:35:10 +0000 | [diff] [blame] | 3189 | /// parameters to cope with template friend declarations. |
| 3190 | Decl *Sema::ParsedFreeStandingDeclSpec(Scope *S, AccessSpecifier AS, |
| 3191 | DeclSpec &DS, |
| Richard Smith | b1402ae | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 3192 | MultiTemplateParamsArg TemplateParams, |
| 3193 | bool IsExplicitInstantiation) { |
| John McCall | c398748 | 2009-10-07 23:34:25 +0000 | [diff] [blame] | 3194 | Decl *TagD = 0; |
| Douglas Gregor | 9817f4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 3195 | TagDecl *Tag = 0; |
| 3196 | if (DS.getTypeSpecType() == DeclSpec::TST_class || |
| 3197 | DS.getTypeSpecType() == DeclSpec::TST_struct || |
| Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 3198 | DS.getTypeSpecType() == DeclSpec::TST_interface || |
| Douglas Gregor | 9817f4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 3199 | DS.getTypeSpecType() == DeclSpec::TST_union || |
| Douglas Gregor | 1b57ff3 | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 3200 | DS.getTypeSpecType() == DeclSpec::TST_enum) { |
| John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3201 | TagD = DS.getRepAsDecl(); |
| John McCall | c398748 | 2009-10-07 23:34:25 +0000 | [diff] [blame] | 3202 | |
| 3203 | if (!TagD) // We probably had an error |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3204 | return 0; |
| Douglas Gregor | 1b57ff3 | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 3205 | |
| John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 3206 | // Note that the above type specs guarantee that the |
| 3207 | // type rep is a Decl, whereas in many of the others |
| 3208 | // it's a Type. |
| Peter Collingbourne | e109a2c | 2011-10-23 17:07:16 +0000 | [diff] [blame] | 3209 | if (isa<TagDecl>(TagD)) |
| 3210 | Tag = cast<TagDecl>(TagD); |
| 3211 | else if (ClassTemplateDecl *CTD = dyn_cast<ClassTemplateDecl>(TagD)) |
| 3212 | Tag = CTD->getTemplatedDecl(); |
| Douglas Gregor | 1b57ff3 | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 3213 | } |
| Douglas Gregor | 9817f4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 3214 | |
| Argyrios Kyrtzidis | 2559629 | 2012-03-09 20:10:30 +0000 | [diff] [blame] | 3215 | if (Tag) { |
| David Majnemer | 2206bf5 | 2014-03-05 08:57:59 +0000 | [diff] [blame] | 3216 | HandleTagNumbering(*this, Tag, S); |
| Argyrios Kyrtzidis | 201d377 | 2011-09-30 22:11:31 +0000 | [diff] [blame] | 3217 | Tag->setFreeStanding(); |
| Argyrios Kyrtzidis | 2559629 | 2012-03-09 20:10:30 +0000 | [diff] [blame] | 3218 | if (Tag->isInvalidDecl()) |
| 3219 | return Tag; |
| 3220 | } |
| Argyrios Kyrtzidis | 201d377 | 2011-09-30 22:11:31 +0000 | [diff] [blame] | 3221 | |
| Nuno Lopes | e9823fa | 2009-12-17 11:35:26 +0000 | [diff] [blame] | 3222 | if (unsigned TypeQuals = DS.getTypeQualifiers()) { |
| 3223 | // Enforce C99 6.7.3p2: "Types other than pointer types derived from object |
| 3224 | // or incomplete types shall not be restrict-qualified." |
| 3225 | if (TypeQuals & DeclSpec::TQ_restrict) |
| 3226 | Diag(DS.getRestrictSpecLoc(), |
| 3227 | diag::err_typecheck_invalid_restrict_not_pointer_noarg) |
| 3228 | << DS.getSourceRange(); |
| 3229 | } |
| 3230 | |
| Richard Smith | a77a0a6 | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 3231 | if (DS.isConstexprSpecified()) { |
| 3232 | // C++0x [dcl.constexpr]p1: constexpr can only be applied to declarations |
| 3233 | // and definitions of functions and variables. |
| 3234 | if (Tag) |
| 3235 | Diag(DS.getConstexprSpecLoc(), diag::err_constexpr_tag) |
| 3236 | << (DS.getTypeSpecType() == DeclSpec::TST_class ? 0 : |
| 3237 | DS.getTypeSpecType() == DeclSpec::TST_struct ? 1 : |
| Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 3238 | DS.getTypeSpecType() == DeclSpec::TST_interface ? 2 : |
| 3239 | DS.getTypeSpecType() == DeclSpec::TST_union ? 3 : 4); |
| Richard Smith | a77a0a6 | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 3240 | else |
| 3241 | Diag(DS.getConstexprSpecLoc(), diag::err_constexpr_no_declarators); |
| 3242 | // Don't emit warnings after this error. |
| 3243 | return TagD; |
| 3244 | } |
| 3245 | |
| Richard Smith | b1402ae | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 3246 | DiagnoseFunctionSpecifiers(DS); |
| 3247 | |
| Douglas Gregor | 3dad842 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 3248 | if (DS.isFriendSpecified()) { |
| John McCall | ace48cd | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 3249 | // If we're dealing with a decl but not a TagDecl, assume that |
| 3250 | // whatever routines created it handled the friendship aspect. |
| 3251 | if (TagD && !Tag) |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3252 | return 0; |
| Chandler Carruth | 7c9856d | 2011-05-03 18:35:10 +0000 | [diff] [blame] | 3253 | return ActOnFriendTypeDecl(S, DS, TemplateParams); |
| Douglas Gregor | 3dad842 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 3254 | } |
| John McCall | aa01737 | 2011-03-22 23:00:04 +0000 | [diff] [blame] | 3255 | |
| Richard Smith | b1402ae | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 3256 | CXXScopeSpec &SS = DS.getTypeSpecScope(); |
| 3257 | bool IsExplicitSpecialization = |
| 3258 | !TemplateParams.empty() && TemplateParams.back()->size() == 0; |
| 3259 | if (Tag && SS.isNotEmpty() && !Tag->isCompleteDefinition() && |
| 3260 | !IsExplicitInstantiation && !IsExplicitSpecialization) { |
| 3261 | // Per C++ [dcl.type.elab]p1, a class declaration cannot have a |
| 3262 | // nested-name-specifier unless it is an explicit instantiation |
| 3263 | // or an explicit specialization. |
| 3264 | // Per C++ [dcl.enum]p1, an opaque-enum-declaration can't either. |
| 3265 | Diag(SS.getBeginLoc(), diag::err_standalone_class_nested_name_specifier) |
| 3266 | << (DS.getTypeSpecType() == DeclSpec::TST_class ? 0 : |
| 3267 | DS.getTypeSpecType() == DeclSpec::TST_struct ? 1 : |
| 3268 | DS.getTypeSpecType() == DeclSpec::TST_interface ? 2 : |
| 3269 | DS.getTypeSpecType() == DeclSpec::TST_union ? 3 : 4) |
| 3270 | << SS.getRange(); |
| 3271 | return 0; |
| 3272 | } |
| 3273 | |
| 3274 | // Track whether this decl-specifier declares anything. |
| 3275 | bool DeclaresAnything = true; |
| 3276 | |
| 3277 | // Handle anonymous struct definitions. |
| Douglas Gregor | c6f58fe | 2009-01-12 22:49:06 +0000 | [diff] [blame] | 3278 | if (RecordDecl *Record = dyn_cast_or_null<RecordDecl>(Tag)) { |
| John McCall | f937c02 | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 3279 | if (!Record->getDeclName() && Record->isCompleteDefinition() && |
| Douglas Gregor | 2e7cba6 | 2009-03-06 23:06:59 +0000 | [diff] [blame] | 3280 | DS.getStorageClassSpec() != DeclSpec::SCS_typedef) { |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3281 | if (getLangOpts().CPlusPlus || |
| Douglas Gregor | 2e7cba6 | 2009-03-06 23:06:59 +0000 | [diff] [blame] | 3282 | Record->getDeclContext()->isRecord()) |
| Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3283 | return BuildAnonymousStructOrUnion(S, DS, AS, Record, Context.getPrintingPolicy()); |
| Douglas Gregor | 2e7cba6 | 2009-03-06 23:06:59 +0000 | [diff] [blame] | 3284 | |
| Richard Smith | b1402ae | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 3285 | DeclaresAnything = false; |
| Douglas Gregor | 2e7cba6 | 2009-03-06 23:06:59 +0000 | [diff] [blame] | 3286 | } |
| Francois Pichet | 0c71f6c | 2010-11-23 06:07:27 +0000 | [diff] [blame] | 3287 | } |
| Douglas Gregor | c6f58fe | 2009-01-12 22:49:06 +0000 | [diff] [blame] | 3288 | |
| Richard Smith | b1402ae | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 3289 | // Check for Microsoft C extension: anonymous struct member. |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3290 | if (getLangOpts().MicrosoftExt && !getLangOpts().CPlusPlus && |
| Francois Pichet | 0c71f6c | 2010-11-23 06:07:27 +0000 | [diff] [blame] | 3291 | CurContext->isRecord() && |
| 3292 | DS.getStorageClassSpec() == DeclSpec::SCS_unspecified) { |
| 3293 | // Handle 2 kinds of anonymous struct: |
| 3294 | // struct STRUCT; |
| 3295 | // and |
| 3296 | // STRUCT_TYPE; <- where STRUCT_TYPE is a typedef struct. |
| 3297 | RecordDecl *Record = dyn_cast_or_null<RecordDecl>(Tag); |
| John McCall | f937c02 | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 3298 | if ((Record && Record->getDeclName() && !Record->isCompleteDefinition()) || |
| Francois Pichet | 0c71f6c | 2010-11-23 06:07:27 +0000 | [diff] [blame] | 3299 | (DS.getTypeSpecType() == DeclSpec::TST_typename && |
| 3300 | DS.getRepAsType().get()->isStructureType())) { |
| Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3301 | Diag(DS.getLocStart(), diag::ext_ms_anonymous_struct) |
| Francois Pichet | 0c71f6c | 2010-11-23 06:07:27 +0000 | [diff] [blame] | 3302 | << DS.getSourceRange(); |
| 3303 | return BuildMicrosoftCAnonymousStruct(S, DS, Record); |
| 3304 | } |
| Douglas Gregor | c6f58fe | 2009-01-12 22:49:06 +0000 | [diff] [blame] | 3305 | } |
| Richard Smith | b1402ae | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 3306 | |
| 3307 | // Skip all the checks below if we have a type error. |
| 3308 | if (DS.getTypeSpecType() == DeclSpec::TST_error || |
| 3309 | (TagD && TagD->isInvalidDecl())) |
| 3310 | return TagD; |
| 3311 | |
| 3312 | if (getLangOpts().CPlusPlus && |
| Douglas Gregor | aa8c972 | 2010-07-13 06:24:26 +0000 | [diff] [blame] | 3313 | DS.getStorageClassSpec() != DeclSpec::SCS_typedef) |
| 3314 | if (EnumDecl *Enum = dyn_cast_or_null<EnumDecl>(Tag)) |
| 3315 | if (Enum->enumerator_begin() == Enum->enumerator_end() && |
| Richard Smith | b1402ae | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 3316 | !Enum->getIdentifier() && !Enum->isInvalidDecl()) |
| 3317 | DeclaresAnything = false; |
| John McCall | aa01737 | 2011-03-22 23:00:04 +0000 | [diff] [blame] | 3318 | |
| John McCall | aa01737 | 2011-03-22 23:00:04 +0000 | [diff] [blame] | 3319 | if (!DS.isMissingDeclaratorOk()) { |
| Richard Smith | b1402ae | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 3320 | // Customize diagnostic for a typedef missing a name. |
| 3321 | if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) |
| Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3322 | Diag(DS.getLocStart(), diag::ext_typedef_without_a_name) |
| Douglas Gregor | 3a8e0d7 | 2010-07-16 15:40:40 +0000 | [diff] [blame] | 3323 | << DS.getSourceRange(); |
| Richard Smith | b1402ae | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 3324 | else |
| 3325 | DeclaresAnything = false; |
| Sebastian Redl | a2b5e31 | 2008-12-28 15:28:59 +0000 | [diff] [blame] | 3326 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3327 | |
| Richard Smith | b1402ae | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 3328 | if (DS.isModulePrivateSpecified() && |
| Douglas Gregor | 4186681 | 2011-09-12 18:37:38 +0000 | [diff] [blame] | 3329 | Tag && Tag->getDeclContext()->isFunctionOrMethod()) |
| 3330 | Diag(DS.getModulePrivateSpecLoc(), diag::err_module_private_local_class) |
| 3331 | << Tag->getTagKind() |
| 3332 | << FixItHint::CreateRemoval(DS.getModulePrivateSpecLoc()); |
| 3333 | |
| Richard Smith | b1402ae | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 3334 | ActOnDocumentableDecl(TagD); |
| 3335 | |
| 3336 | // C 6.7/2: |
| 3337 | // A declaration [...] shall declare at least a declarator [...], a tag, |
| 3338 | // or the members of an enumeration. |
| 3339 | // C++ [dcl.dcl]p3: |
| 3340 | // [If there are no declarators], and except for the declaration of an |
| 3341 | // unnamed bit-field, the decl-specifier-seq shall introduce one or more |
| 3342 | // names into the program, or shall redeclare a name introduced by a |
| 3343 | // previous declaration. |
| 3344 | if (!DeclaresAnything) { |
| 3345 | // In C, we allow this as a (popular) extension / bug. Don't bother |
| 3346 | // producing further diagnostics for redundant qualifiers after this. |
| 3347 | Diag(DS.getLocStart(), diag::ext_no_declarators) << DS.getSourceRange(); |
| 3348 | return TagD; |
| 3349 | } |
| 3350 | |
| 3351 | // C++ [dcl.stc]p1: |
| 3352 | // If a storage-class-specifier appears in a decl-specifier-seq, [...] the |
| 3353 | // init-declarator-list of the declaration shall not be empty. |
| 3354 | // C++ [dcl.fct.spec]p1: |
| 3355 | // If a cv-qualifier appears in a decl-specifier-seq, the |
| 3356 | // init-declarator-list of the declaration shall not be empty. |
| 3357 | // |
| 3358 | // Spurious qualifiers here appear to be valid in C. |
| 3359 | unsigned DiagID = diag::warn_standalone_specifier; |
| 3360 | if (getLangOpts().CPlusPlus) |
| 3361 | DiagID = diag::ext_standalone_specifier; |
| 3362 | |
| 3363 | // Note that a linkage-specification sets a storage class, but |
| 3364 | // 'extern "C" struct foo;' is actually valid and not theoretically |
| 3365 | // useless. |
| 3366 | if (DeclSpec::SCS SCS = DS.getStorageClassSpec()) |
| 3367 | if (!DS.isExternInLinkageSpec() && SCS != DeclSpec::SCS_typedef) |
| 3368 | Diag(DS.getStorageClassSpecLoc(), DiagID) |
| 3369 | << DeclSpec::getSpecifierName(SCS); |
| 3370 | |
| Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 3371 | if (DeclSpec::TSCS TSCS = DS.getThreadStorageClassSpec()) |
| 3372 | Diag(DS.getThreadStorageClassSpecLoc(), DiagID) |
| 3373 | << DeclSpec::getSpecifierName(TSCS); |
| Richard Smith | b1402ae | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 3374 | if (DS.getTypeQualifiers()) { |
| 3375 | if (DS.getTypeQualifiers() & DeclSpec::TQ_const) |
| 3376 | Diag(DS.getConstSpecLoc(), DiagID) << "const"; |
| 3377 | if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile) |
| 3378 | Diag(DS.getConstSpecLoc(), DiagID) << "volatile"; |
| 3379 | // Restrict is covered above. |
| Richard Smith | 8e1ac33 | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 3380 | if (DS.getTypeQualifiers() & DeclSpec::TQ_atomic) |
| 3381 | Diag(DS.getAtomicSpecLoc(), DiagID) << "_Atomic"; |
| Richard Smith | b1402ae | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 3382 | } |
| 3383 | |
| Eli Friedman | e321795 | 2011-12-17 00:36:09 +0000 | [diff] [blame] | 3384 | // Warn about ignored type attributes, for example: |
| 3385 | // __attribute__((aligned)) struct A; |
| Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3386 | // Attributes should be placed after tag to apply to type declaration. |
| Eli Friedman | e321795 | 2011-12-17 00:36:09 +0000 | [diff] [blame] | 3387 | if (!DS.getAttributes().empty()) { |
| 3388 | DeclSpec::TST TypeSpecType = DS.getTypeSpecType(); |
| 3389 | if (TypeSpecType == DeclSpec::TST_class || |
| 3390 | TypeSpecType == DeclSpec::TST_struct || |
| Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 3391 | TypeSpecType == DeclSpec::TST_interface || |
| Eli Friedman | e321795 | 2011-12-17 00:36:09 +0000 | [diff] [blame] | 3392 | TypeSpecType == DeclSpec::TST_union || |
| 3393 | TypeSpecType == DeclSpec::TST_enum) { |
| 3394 | AttributeList* attrs = DS.getAttributes().getList(); |
| 3395 | while (attrs) { |
| Michael Han | 360d225 | 2012-10-04 16:42:52 +0000 | [diff] [blame] | 3396 | Diag(attrs->getLoc(), diag::warn_declspec_attribute_ignored) |
| Eli Friedman | e321795 | 2011-12-17 00:36:09 +0000 | [diff] [blame] | 3397 | << attrs->getName() |
| 3398 | << (TypeSpecType == DeclSpec::TST_class ? 0 : |
| 3399 | TypeSpecType == DeclSpec::TST_struct ? 1 : |
| Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 3400 | TypeSpecType == DeclSpec::TST_union ? 2 : |
| 3401 | TypeSpecType == DeclSpec::TST_interface ? 3 : 4); |
| Eli Friedman | e321795 | 2011-12-17 00:36:09 +0000 | [diff] [blame] | 3402 | attrs = attrs->getNext(); |
| 3403 | } |
| 3404 | } |
| 3405 | } |
| John McCall | aa01737 | 2011-03-22 23:00:04 +0000 | [diff] [blame] | 3406 | |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3407 | return TagD; |
| Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3408 | } |
| 3409 | |
| John McCall | ea305ed | 2009-12-18 10:40:03 +0000 | [diff] [blame] | 3410 | /// We are trying to inject an anonymous member into the given scope; |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 3411 | /// check if there's an existing declaration that can't be overloaded. |
| 3412 | /// |
| 3413 | /// \return true if this is a forbidden redeclaration |
| John McCall | ea305ed | 2009-12-18 10:40:03 +0000 | [diff] [blame] | 3414 | static bool CheckAnonMemberRedeclaration(Sema &SemaRef, |
| 3415 | Scope *S, |
| Fariborz Jahanian | 53967e2 | 2010-01-22 18:30:17 +0000 | [diff] [blame] | 3416 | DeclContext *Owner, |
| John McCall | ea305ed | 2009-12-18 10:40:03 +0000 | [diff] [blame] | 3417 | DeclarationName Name, |
| 3418 | SourceLocation NameLoc, |
| 3419 | unsigned diagnostic) { |
| 3420 | LookupResult R(SemaRef, Name, NameLoc, Sema::LookupMemberName, |
| 3421 | Sema::ForRedeclaration); |
| 3422 | if (!SemaRef.LookupName(R, S)) return false; |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 3423 | |
| John McCall | ea305ed | 2009-12-18 10:40:03 +0000 | [diff] [blame] | 3424 | if (R.getAsSingle<TagDecl>()) |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 3425 | return false; |
| 3426 | |
| 3427 | // Pick a representative declaration. |
| John McCall | ea305ed | 2009-12-18 10:40:03 +0000 | [diff] [blame] | 3428 | NamedDecl *PrevDecl = R.getRepresentativeDecl()->getUnderlyingDecl(); |
| Argyrios Kyrtzidis | e619e99 | 2010-09-23 14:26:01 +0000 | [diff] [blame] | 3429 | assert(PrevDecl && "Expected a non-null Decl"); |
| 3430 | |
| 3431 | if (!SemaRef.isDeclInScope(PrevDecl, Owner, S)) |
| 3432 | return false; |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 3433 | |
| John McCall | ea305ed | 2009-12-18 10:40:03 +0000 | [diff] [blame] | 3434 | SemaRef.Diag(NameLoc, diagnostic) << Name; |
| 3435 | SemaRef.Diag(PrevDecl->getLocation(), diag::note_previous_declaration); |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 3436 | |
| 3437 | return true; |
| 3438 | } |
| 3439 | |
| Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3440 | /// InjectAnonymousStructOrUnionMembers - Inject the members of the |
| 3441 | /// anonymous struct or union AnonRecord into the owning context Owner |
| 3442 | /// and scope S. This routine will be invoked just after we realize |
| 3443 | /// that an unnamed union or struct is actually an anonymous union or |
| 3444 | /// struct, e.g., |
| 3445 | /// |
| 3446 | /// @code |
| 3447 | /// union { |
| 3448 | /// int i; |
| 3449 | /// float f; |
| 3450 | /// }; // InjectAnonymousStructOrUnionMembers called here to inject i and |
| 3451 | /// // f into the surrounding scope.x |
| 3452 | /// @endcode |
| 3453 | /// |
| 3454 | /// This routine is recursive, injecting the names of nested anonymous |
| 3455 | /// structs/unions into the owning context and scope as well. |
| John McCall | b54367d | 2010-05-21 20:45:30 +0000 | [diff] [blame] | 3456 | static bool InjectAnonymousStructOrUnionMembers(Sema &SemaRef, Scope *S, |
| Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 3457 | DeclContext *Owner, |
| 3458 | RecordDecl *AnonRecord, |
| 3459 | AccessSpecifier AS, |
| 3460 | SmallVectorImpl<NamedDecl *> &Chaining, |
| 3461 | bool MSAnonStruct) { |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 3462 | unsigned diagKind |
| 3463 | = AnonRecord->isUnion() ? diag::err_anonymous_union_member_redecl |
| 3464 | : diag::err_anonymous_struct_member_redecl; |
| 3465 | |
| Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3466 | bool Invalid = false; |
| Francois Pichet | 0c71f6c | 2010-11-23 06:07:27 +0000 | [diff] [blame] | 3467 | |
| 3468 | // Look every FieldDecl and IndirectFieldDecl with a name. |
| 3469 | for (RecordDecl::decl_iterator D = AnonRecord->decls_begin(), |
| 3470 | DEnd = AnonRecord->decls_end(); |
| 3471 | D != DEnd; ++D) { |
| 3472 | if ((isa<FieldDecl>(*D) || isa<IndirectFieldDecl>(*D)) && |
| 3473 | cast<NamedDecl>(*D)->getDeclName()) { |
| 3474 | ValueDecl *VD = cast<ValueDecl>(*D); |
| 3475 | if (CheckAnonMemberRedeclaration(SemaRef, S, Owner, VD->getDeclName(), |
| 3476 | VD->getLocation(), diagKind)) { |
| Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3477 | // C++ [class.union]p2: |
| 3478 | // The names of the members of an anonymous union shall be |
| 3479 | // distinct from the names of any other entity in the |
| 3480 | // scope in which the anonymous union is declared. |
| Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3481 | Invalid = true; |
| 3482 | } else { |
| 3483 | // C++ [class.union]p2: |
| 3484 | // For the purpose of name lookup, after the anonymous union |
| 3485 | // definition, the members of the anonymous union are |
| 3486 | // considered to have been defined in the scope in which the |
| 3487 | // anonymous union is declared. |
| Francois Pichet | 0c71f6c | 2010-11-23 06:07:27 +0000 | [diff] [blame] | 3488 | unsigned OldChainingSize = Chaining.size(); |
| 3489 | if (IndirectFieldDecl *IF = dyn_cast<IndirectFieldDecl>(VD)) |
| Aaron Ballman | 1391608 | 2014-03-07 18:11:58 +0000 | [diff] [blame^] | 3490 | for (auto *PI : IF->chains()) |
| 3491 | Chaining.push_back(PI); |
| Francois Pichet | 0c71f6c | 2010-11-23 06:07:27 +0000 | [diff] [blame] | 3492 | else |
| 3493 | Chaining.push_back(VD); |
| 3494 | |
| Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 3495 | assert(Chaining.size() >= 2); |
| 3496 | NamedDecl **NamedChain = |
| 3497 | new (SemaRef.Context)NamedDecl*[Chaining.size()]; |
| 3498 | for (unsigned i = 0; i < Chaining.size(); i++) |
| 3499 | NamedChain[i] = Chaining[i]; |
| 3500 | |
| 3501 | IndirectFieldDecl* IndirectField = |
| Francois Pichet | 0c71f6c | 2010-11-23 06:07:27 +0000 | [diff] [blame] | 3502 | IndirectFieldDecl::Create(SemaRef.Context, Owner, VD->getLocation(), |
| 3503 | VD->getIdentifier(), VD->getType(), |
| Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 3504 | NamedChain, Chaining.size()); |
| 3505 | |
| 3506 | IndirectField->setAccess(AS); |
| 3507 | IndirectField->setImplicit(); |
| 3508 | SemaRef.PushOnScopeChains(IndirectField, S); |
| John McCall | b54367d | 2010-05-21 20:45:30 +0000 | [diff] [blame] | 3509 | |
| 3510 | // That includes picking up the appropriate access specifier. |
| Francois Pichet | 0c71f6c | 2010-11-23 06:07:27 +0000 | [diff] [blame] | 3511 | if (AS != AS_none) IndirectField->setAccess(AS); |
| Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 3512 | |
| Francois Pichet | 0c71f6c | 2010-11-23 06:07:27 +0000 | [diff] [blame] | 3513 | Chaining.resize(OldChainingSize); |
| Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3514 | } |
| Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3515 | } |
| 3516 | } |
| 3517 | |
| 3518 | return Invalid; |
| 3519 | } |
| 3520 | |
| Douglas Gregor | c4df407 | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 3521 | /// StorageClassSpecToVarDeclStorageClass - Maps a DeclSpec::SCS to |
| 3522 | /// a VarDecl::StorageClass. Any error reporting is up to the caller: |
| John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 3523 | /// illegal input values are mapped to SC_None. |
| 3524 | static StorageClass |
| Rafael Espindola | bff5956 | 2013-04-25 12:11:36 +0000 | [diff] [blame] | 3525 | StorageClassSpecToVarDeclStorageClass(const DeclSpec &DS) { |
| 3526 | DeclSpec::SCS StorageClassSpec = DS.getStorageClassSpec(); |
| 3527 | assert(StorageClassSpec != DeclSpec::SCS_typedef && |
| 3528 | "Parser allowed 'typedef' as storage class VarDecl."); |
| Douglas Gregor | c4df407 | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 3529 | switch (StorageClassSpec) { |
| John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 3530 | case DeclSpec::SCS_unspecified: return SC_None; |
| Rafael Espindola | bff5956 | 2013-04-25 12:11:36 +0000 | [diff] [blame] | 3531 | case DeclSpec::SCS_extern: |
| 3532 | if (DS.isExternInLinkageSpec()) |
| 3533 | return SC_None; |
| 3534 | return SC_Extern; |
| John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 3535 | case DeclSpec::SCS_static: return SC_Static; |
| 3536 | case DeclSpec::SCS_auto: return SC_Auto; |
| 3537 | case DeclSpec::SCS_register: return SC_Register; |
| 3538 | case DeclSpec::SCS_private_extern: return SC_PrivateExtern; |
| Douglas Gregor | c4df407 | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 3539 | // Illegal SCSs map to None: error reporting is up to the caller. |
| 3540 | case DeclSpec::SCS_mutable: // Fall through. |
| John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 3541 | case DeclSpec::SCS_typedef: return SC_None; |
| Douglas Gregor | c4df407 | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 3542 | } |
| 3543 | llvm_unreachable("unknown storage class specifier"); |
| 3544 | } |
| 3545 | |
| Richard Smith | ab44d5b | 2013-12-10 08:25:00 +0000 | [diff] [blame] | 3546 | static SourceLocation findDefaultInitializer(const CXXRecordDecl *Record) { |
| 3547 | assert(Record->hasInClassInitializer()); |
| 3548 | |
| 3549 | for (DeclContext::decl_iterator I = Record->decls_begin(), |
| 3550 | E = Record->decls_end(); |
| 3551 | I != E; ++I) { |
| 3552 | FieldDecl *FD = dyn_cast<FieldDecl>(*I); |
| 3553 | if (IndirectFieldDecl *IFD = dyn_cast<IndirectFieldDecl>(*I)) |
| 3554 | FD = IFD->getAnonField(); |
| 3555 | if (FD && FD->hasInClassInitializer()) |
| 3556 | return FD->getLocation(); |
| 3557 | } |
| 3558 | |
| 3559 | llvm_unreachable("couldn't find in-class initializer"); |
| 3560 | } |
| 3561 | |
| 3562 | static void checkDuplicateDefaultInit(Sema &S, CXXRecordDecl *Parent, |
| 3563 | SourceLocation DefaultInitLoc) { |
| 3564 | if (!Parent->isUnion() || !Parent->hasInClassInitializer()) |
| 3565 | return; |
| 3566 | |
| 3567 | S.Diag(DefaultInitLoc, diag::err_multiple_mem_union_initialization); |
| 3568 | S.Diag(findDefaultInitializer(Parent), diag::note_previous_initializer) << 0; |
| 3569 | } |
| 3570 | |
| 3571 | static void checkDuplicateDefaultInit(Sema &S, CXXRecordDecl *Parent, |
| 3572 | CXXRecordDecl *AnonUnion) { |
| 3573 | if (!Parent->isUnion() || !Parent->hasInClassInitializer()) |
| 3574 | return; |
| 3575 | |
| 3576 | checkDuplicateDefaultInit(S, Parent, findDefaultInitializer(AnonUnion)); |
| 3577 | } |
| 3578 | |
| Francois Pichet | 0c71f6c | 2010-11-23 06:07:27 +0000 | [diff] [blame] | 3579 | /// BuildAnonymousStructOrUnion - Handle the declaration of an |
| Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3580 | /// anonymous structure or union. Anonymous unions are a C++ feature |
| Hans Wennborg | b64a1fa | 2012-02-03 15:47:04 +0000 | [diff] [blame] | 3581 | /// (C++ [class.union]) and a C11 feature; anonymous structures |
| 3582 | /// are a C11 feature and GNU C++ extension. |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3583 | Decl *Sema::BuildAnonymousStructOrUnion(Scope *S, DeclSpec &DS, |
| Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3584 | AccessSpecifier AS, |
| 3585 | RecordDecl *Record, |
| 3586 | const PrintingPolicy &Policy) { |
| Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3587 | DeclContext *Owner = Record->getDeclContext(); |
| 3588 | |
| 3589 | // Diagnose whether this anonymous struct/union is an extension. |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3590 | if (Record->isUnion() && !getLangOpts().CPlusPlus && !getLangOpts().C11) |
| Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3591 | Diag(Record->getLocation(), diag::ext_anonymous_union); |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3592 | else if (!Record->isUnion() && getLangOpts().CPlusPlus) |
| Hans Wennborg | b64a1fa | 2012-02-03 15:47:04 +0000 | [diff] [blame] | 3593 | Diag(Record->getLocation(), diag::ext_gnu_anonymous_struct); |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3594 | else if (!Record->isUnion() && !getLangOpts().C11) |
| Hans Wennborg | b64a1fa | 2012-02-03 15:47:04 +0000 | [diff] [blame] | 3595 | Diag(Record->getLocation(), diag::ext_c11_anonymous_struct); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3596 | |
| Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3597 | // C and C++ require different kinds of checks for anonymous |
| 3598 | // structs/unions. |
| 3599 | bool Invalid = false; |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3600 | if (getLangOpts().CPlusPlus) { |
| Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3601 | const char* PrevSpec = 0; |
| John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3602 | unsigned DiagID; |
| David Blaikie | 0a8e899 | 2011-10-19 22:43:29 +0000 | [diff] [blame] | 3603 | if (Record->isUnion()) { |
| 3604 | // C++ [class.union]p6: |
| 3605 | // Anonymous unions declared in a named namespace or in the |
| 3606 | // global namespace shall be declared static. |
| 3607 | if (DS.getStorageClassSpec() != DeclSpec::SCS_static && |
| 3608 | (isa<TranslationUnitDecl>(Owner) || |
| 3609 | (isa<NamespaceDecl>(Owner) && |
| 3610 | cast<NamespaceDecl>(Owner)->getDeclName()))) { |
| David Blaikie | 733f7bb | 2011-10-20 02:49:08 +0000 | [diff] [blame] | 3611 | Diag(Record->getLocation(), diag::err_anonymous_union_not_static) |
| 3612 | << FixItHint::CreateInsertion(Record->getLocation(), "static "); |
| David Blaikie | 0a8e899 | 2011-10-19 22:43:29 +0000 | [diff] [blame] | 3613 | |
| 3614 | // Recover by adding 'static'. |
| 3615 | DS.SetStorageClassSpec(*this, DeclSpec::SCS_static, SourceLocation(), |
| Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3616 | PrevSpec, DiagID, Policy); |
| David Blaikie | 0a8e899 | 2011-10-19 22:43:29 +0000 | [diff] [blame] | 3617 | } |
| 3618 | // C++ [class.union]p6: |
| 3619 | // A storage class is not allowed in a declaration of an |
| 3620 | // anonymous union in a class scope. |
| 3621 | else if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified && |
| 3622 | isa<RecordDecl>(Owner)) { |
| 3623 | Diag(DS.getStorageClassSpecLoc(), |
| David Blaikie | 6f686fc | 2011-10-20 02:10:55 +0000 | [diff] [blame] | 3624 | diag::err_anonymous_union_with_storage_spec) |
| 3625 | << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc()); |
| David Blaikie | 0a8e899 | 2011-10-19 22:43:29 +0000 | [diff] [blame] | 3626 | |
| 3627 | // Recover by removing the storage specifier. |
| David Blaikie | 30d1544 | 2011-10-19 22:56:21 +0000 | [diff] [blame] | 3628 | DS.SetStorageClassSpec(*this, DeclSpec::SCS_unspecified, |
| 3629 | SourceLocation(), |
| Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3630 | PrevSpec, DiagID, Context.getPrintingPolicy()); |
| David Blaikie | 0a8e899 | 2011-10-19 22:43:29 +0000 | [diff] [blame] | 3631 | } |
| Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3632 | } |
| Douglas Gregor | f4d3327 | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 3633 | |
| Douglas Gregor | 0f8bc97 | 2011-05-09 23:05:33 +0000 | [diff] [blame] | 3634 | // Ignore const/volatile/restrict qualifiers. |
| 3635 | if (DS.getTypeQualifiers()) { |
| 3636 | if (DS.getTypeQualifiers() & DeclSpec::TQ_const) |
| 3637 | Diag(DS.getConstSpecLoc(), diag::ext_anonymous_struct_union_qualified) |
| Richard Smith | 8e1ac33 | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 3638 | << Record->isUnion() << "const" |
| Douglas Gregor | 0f8bc97 | 2011-05-09 23:05:33 +0000 | [diff] [blame] | 3639 | << FixItHint::CreateRemoval(DS.getConstSpecLoc()); |
| 3640 | if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile) |
| Richard Smith | 8e1ac33 | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 3641 | Diag(DS.getVolatileSpecLoc(), |
| David Blaikie | 30d1544 | 2011-10-19 22:56:21 +0000 | [diff] [blame] | 3642 | diag::ext_anonymous_struct_union_qualified) |
| Richard Smith | 8e1ac33 | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 3643 | << Record->isUnion() << "volatile" |
| Douglas Gregor | 0f8bc97 | 2011-05-09 23:05:33 +0000 | [diff] [blame] | 3644 | << FixItHint::CreateRemoval(DS.getVolatileSpecLoc()); |
| 3645 | if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict) |
| Richard Smith | 8e1ac33 | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 3646 | Diag(DS.getRestrictSpecLoc(), |
| David Blaikie | 30d1544 | 2011-10-19 22:56:21 +0000 | [diff] [blame] | 3647 | diag::ext_anonymous_struct_union_qualified) |
| Richard Smith | 8e1ac33 | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 3648 | << Record->isUnion() << "restrict" |
| Douglas Gregor | 0f8bc97 | 2011-05-09 23:05:33 +0000 | [diff] [blame] | 3649 | << FixItHint::CreateRemoval(DS.getRestrictSpecLoc()); |
| Richard Smith | 8e1ac33 | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 3650 | if (DS.getTypeQualifiers() & DeclSpec::TQ_atomic) |
| 3651 | Diag(DS.getAtomicSpecLoc(), |
| 3652 | diag::ext_anonymous_struct_union_qualified) |
| 3653 | << Record->isUnion() << "_Atomic" |
| 3654 | << FixItHint::CreateRemoval(DS.getAtomicSpecLoc()); |
| Douglas Gregor | 0f8bc97 | 2011-05-09 23:05:33 +0000 | [diff] [blame] | 3655 | |
| 3656 | DS.ClearTypeQualifiers(); |
| 3657 | } |
| 3658 | |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3659 | // C++ [class.union]p2: |
| Douglas Gregor | f4d3327 | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 3660 | // The member-specification of an anonymous union shall only |
| 3661 | // define non-static data members. [Note: nested types and |
| 3662 | // functions cannot be declared within an anonymous union. ] |
| Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3663 | for (DeclContext::decl_iterator Mem = Record->decls_begin(), |
| 3664 | MemEnd = Record->decls_end(); |
| Douglas Gregor | f4d3327 | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 3665 | Mem != MemEnd; ++Mem) { |
| 3666 | if (FieldDecl *FD = dyn_cast<FieldDecl>(*Mem)) { |
| 3667 | // C++ [class.union]p3: |
| 3668 | // An anonymous union shall not have private or protected |
| 3669 | // members (clause 11). |
| John McCall | b54367d | 2010-05-21 20:45:30 +0000 | [diff] [blame] | 3670 | assert(FD->getAccess() != AS_none); |
| 3671 | if (FD->getAccess() != AS_public) { |
| Douglas Gregor | f4d3327 | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 3672 | Diag(FD->getLocation(), diag::err_anonymous_record_nonpublic_member) |
| 3673 | << (int)Record->isUnion() << (int)(FD->getAccess() == AS_protected); |
| 3674 | Invalid = true; |
| 3675 | } |
| Argyrios Kyrtzidis | 33aee39 | 2010-08-16 17:27:08 +0000 | [diff] [blame] | 3676 | |
| Alexis Hunt | 97ab554 | 2011-05-16 22:41:40 +0000 | [diff] [blame] | 3677 | // C++ [class.union]p1 |
| 3678 | // An object of a class with a non-trivial constructor, a non-trivial |
| 3679 | // copy constructor, a non-trivial destructor, or a non-trivial copy |
| 3680 | // assignment operator cannot be a member of a union, nor can an |
| 3681 | // array of such objects. |
| Richard Smith | f720df0 | 2011-10-19 20:41:51 +0000 | [diff] [blame] | 3682 | if (CheckNontrivialField(FD)) |
| Argyrios Kyrtzidis | 33aee39 | 2010-08-16 17:27:08 +0000 | [diff] [blame] | 3683 | Invalid = true; |
| Douglas Gregor | f4d3327 | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 3684 | } else if ((*Mem)->isImplicit()) { |
| 3685 | // Any implicit members are fine. |
| Douglas Gregor | 8761da5 | 2009-02-03 00:34:39 +0000 | [diff] [blame] | 3686 | } else if (isa<TagDecl>(*Mem) && (*Mem)->getDeclContext() != Record) { |
| 3687 | // This is a type that showed up in an |
| 3688 | // elaborated-type-specifier inside the anonymous struct or |
| 3689 | // union, but which actually declares a type outside of the |
| 3690 | // anonymous struct or union. It's okay. |
| Douglas Gregor | f4d3327 | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 3691 | } else if (RecordDecl *MemRecord = dyn_cast<RecordDecl>(*Mem)) { |
| 3692 | if (!MemRecord->isAnonymousStructOrUnion() && |
| 3693 | MemRecord->getDeclName()) { |
| Francois Pichet | 4ad4b58 | 2010-09-08 11:32:25 +0000 | [diff] [blame] | 3694 | // Visual C++ allows type definition in anonymous struct or union. |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3695 | if (getLangOpts().MicrosoftExt) |
| Francois Pichet | 4ad4b58 | 2010-09-08 11:32:25 +0000 | [diff] [blame] | 3696 | Diag(MemRecord->getLocation(), diag::ext_anonymous_record_with_type) |
| 3697 | << (int)Record->isUnion(); |
| 3698 | else { |
| 3699 | // This is a nested type declaration. |
| 3700 | Diag(MemRecord->getLocation(), diag::err_anonymous_record_with_type) |
| 3701 | << (int)Record->isUnion(); |
| 3702 | Invalid = true; |
| 3703 | } |
| Richard Smith | 254d266 | 2013-01-28 00:54:05 +0000 | [diff] [blame] | 3704 | } else { |
| 3705 | // This is an anonymous type definition within another anonymous type. |
| 3706 | // This is a popular extension, provided by Plan9, MSVC and GCC, but |
| 3707 | // not part of standard C++. |
| 3708 | Diag(MemRecord->getLocation(), |
| Richard Smith | d202924 | 2013-01-31 03:11:12 +0000 | [diff] [blame] | 3709 | diag::ext_anonymous_record_with_anonymous_type) |
| 3710 | << (int)Record->isUnion(); |
| Douglas Gregor | f4d3327 | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 3711 | } |
| Abramo Bagnara | d734058 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 3712 | } else if (isa<AccessSpecDecl>(*Mem)) { |
| 3713 | // Any access specifier is fine. |
| Douglas Gregor | f4d3327 | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 3714 | } else { |
| 3715 | // We have something that isn't a non-static data |
| 3716 | // member. Complain about it. |
| 3717 | unsigned DK = diag::err_anonymous_record_bad_member; |
| 3718 | if (isa<TypeDecl>(*Mem)) |
| 3719 | DK = diag::err_anonymous_record_with_type; |
| 3720 | else if (isa<FunctionDecl>(*Mem)) |
| 3721 | DK = diag::err_anonymous_record_with_function; |
| 3722 | else if (isa<VarDecl>(*Mem)) |
| 3723 | DK = diag::err_anonymous_record_with_static; |
| Francois Pichet | 4ad4b58 | 2010-09-08 11:32:25 +0000 | [diff] [blame] | 3724 | |
| 3725 | // Visual C++ allows type definition in anonymous struct or union. |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3726 | if (getLangOpts().MicrosoftExt && |
| Francois Pichet | 4ad4b58 | 2010-09-08 11:32:25 +0000 | [diff] [blame] | 3727 | DK == diag::err_anonymous_record_with_type) |
| 3728 | Diag((*Mem)->getLocation(), diag::ext_anonymous_record_with_type) |
| Douglas Gregor | f4d3327 | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 3729 | << (int)Record->isUnion(); |
| Francois Pichet | 4ad4b58 | 2010-09-08 11:32:25 +0000 | [diff] [blame] | 3730 | else { |
| 3731 | Diag((*Mem)->getLocation(), DK) |
| 3732 | << (int)Record->isUnion(); |
| Douglas Gregor | f4d3327 | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 3733 | Invalid = true; |
| Francois Pichet | 4ad4b58 | 2010-09-08 11:32:25 +0000 | [diff] [blame] | 3734 | } |
| Douglas Gregor | f4d3327 | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 3735 | } |
| 3736 | } |
| Richard Smith | ab44d5b | 2013-12-10 08:25:00 +0000 | [diff] [blame] | 3737 | |
| 3738 | // C++11 [class.union]p8 (DR1460): |
| 3739 | // At most one variant member of a union may have a |
| 3740 | // brace-or-equal-initializer. |
| 3741 | if (cast<CXXRecordDecl>(Record)->hasInClassInitializer() && |
| 3742 | Owner->isRecord()) |
| 3743 | checkDuplicateDefaultInit(*this, cast<CXXRecordDecl>(Owner), |
| 3744 | cast<CXXRecordDecl>(Record)); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3745 | } |
| Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3746 | |
| 3747 | if (!Record->isUnion() && !Owner->isRecord()) { |
| Douglas Gregor | c6f58fe | 2009-01-12 22:49:06 +0000 | [diff] [blame] | 3748 | Diag(Record->getLocation(), diag::err_anonymous_struct_not_member) |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3749 | << (int)getLangOpts().CPlusPlus; |
| Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3750 | Invalid = true; |
| 3751 | } |
| 3752 | |
| John McCall | fa2d692 | 2009-10-22 23:31:08 +0000 | [diff] [blame] | 3753 | // Mock up a declarator. |
| Argyrios Kyrtzidis | 7baa0af | 2011-06-28 03:01:18 +0000 | [diff] [blame] | 3754 | Declarator Dc(DS, Declarator::MemberContext); |
| John McCall | 8cb7bdf | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 3755 | TypeSourceInfo *TInfo = GetTypeForDeclarator(Dc, S); |
| John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3756 | assert(TInfo && "couldn't build declarator info for anonymous struct/union"); |
| John McCall | fa2d692 | 2009-10-22 23:31:08 +0000 | [diff] [blame] | 3757 | |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3758 | // Create a declaration for this anonymous struct/union. |
| Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 3759 | NamedDecl *Anon = 0; |
| Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3760 | if (RecordDecl *OwningClass = dyn_cast<RecordDecl>(Owner)) { |
| Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3761 | Anon = FieldDecl::Create(Context, OwningClass, |
| Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3762 | DS.getLocStart(), |
| Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3763 | Record->getLocation(), |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3764 | /*IdentifierInfo=*/0, |
| Argyrios Kyrtzidis | 6032ef1 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 3765 | Context.getTypeDeclType(Record), |
| John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3766 | TInfo, |
| Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3767 | /*BitWidth=*/0, /*Mutable=*/false, |
| Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 3768 | /*InitStyle=*/ICIS_NoInit); |
| John McCall | b54367d | 2010-05-21 20:45:30 +0000 | [diff] [blame] | 3769 | Anon->setAccess(AS); |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3770 | if (getLangOpts().CPlusPlus) |
| Douglas Gregor | f4d3327 | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 3771 | FieldCollector->Add(cast<FieldDecl>(Anon)); |
| Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3772 | } else { |
| Douglas Gregor | c4df407 | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 3773 | DeclSpec::SCS SCSpec = DS.getStorageClassSpec(); |
| Rafael Espindola | bff5956 | 2013-04-25 12:11:36 +0000 | [diff] [blame] | 3774 | VarDecl::StorageClass SC = StorageClassSpecToVarDeclStorageClass(DS); |
| Douglas Gregor | c4df407 | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 3775 | if (SCSpec == DeclSpec::SCS_mutable) { |
| Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3776 | // mutable can only appear on non-static class members, so it's always |
| 3777 | // an error here |
| 3778 | Diag(Record->getLocation(), diag::err_mutable_nonmember); |
| 3779 | Invalid = true; |
| John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 3780 | SC = SC_None; |
| Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3781 | } |
| 3782 | |
| Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3783 | Anon = VarDecl::Create(Context, Owner, |
| Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3784 | DS.getLocStart(), |
| Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3785 | Record->getLocation(), /*IdentifierInfo=*/0, |
| Argyrios Kyrtzidis | 6032ef1 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 3786 | Context.getTypeDeclType(Record), |
| Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 3787 | TInfo, SC); |
| Richard Smith | 4037235 | 2011-09-18 00:06:34 +0000 | [diff] [blame] | 3788 | |
| 3789 | // Default-initialize the implicit variable. This initialization will be |
| 3790 | // trivial in almost all cases, except if a union member has an in-class |
| 3791 | // initializer: |
| 3792 | // union { int n = 0; }; |
| 3793 | ActOnUninitializedDecl(Anon, /*TypeMayContainAuto=*/false); |
| Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3794 | } |
| Douglas Gregor | f4d3327 | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 3795 | Anon->setImplicit(); |
| Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3796 | |
| Richard Smith | ab44d5b | 2013-12-10 08:25:00 +0000 | [diff] [blame] | 3797 | // Mark this as an anonymous struct/union type. |
| 3798 | Record->setAnonymousStructOrUnion(true); |
| 3799 | |
| Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3800 | // Add the anonymous struct/union object to the current |
| 3801 | // context. We'll be referencing this object when we refer to one of |
| 3802 | // its members. |
| Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3803 | Owner->addDecl(Anon); |
| Richard Smith | ab44d5b | 2013-12-10 08:25:00 +0000 | [diff] [blame] | 3804 | |
| Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3805 | // Inject the members of the anonymous struct/union into the owning |
| 3806 | // context and into the identifier resolver chain for name lookup |
| 3807 | // purposes. |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3808 | SmallVector<NamedDecl*, 2> Chain; |
| Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 3809 | Chain.push_back(Anon); |
| 3810 | |
| Francois Pichet | 0c71f6c | 2010-11-23 06:07:27 +0000 | [diff] [blame] | 3811 | if (InjectAnonymousStructOrUnionMembers(*this, S, Owner, Record, AS, |
| 3812 | Chain, false)) |
| Douglas Gregor | c6f58fe | 2009-01-12 22:49:06 +0000 | [diff] [blame] | 3813 | Invalid = true; |
| Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3814 | |
| David Majnemer | 2206bf5 | 2014-03-05 08:57:59 +0000 | [diff] [blame] | 3815 | if (VarDecl *NewVD = dyn_cast<VarDecl>(Anon)) { |
| 3816 | if (getLangOpts().CPlusPlus && NewVD->isStaticLocal()) { |
| 3817 | Decl *ManglingContextDecl; |
| 3818 | if (MangleNumberingContext *MCtx = |
| 3819 | getCurrentMangleNumberContext(NewVD->getDeclContext(), |
| 3820 | ManglingContextDecl)) { |
| David Majnemer | f27217f | 2014-03-05 18:55:38 +0000 | [diff] [blame] | 3821 | Context.setManglingNumber(NewVD, MCtx->getManglingNumber(NewVD, S->getMSLocalManglingNumber())); |
| David Majnemer | 2206bf5 | 2014-03-05 08:57:59 +0000 | [diff] [blame] | 3822 | Context.setStaticLocalNumber(NewVD, MCtx->getStaticLocalNumber(NewVD)); |
| 3823 | } |
| 3824 | } |
| 3825 | } |
| 3826 | |
| Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3827 | if (Invalid) |
| 3828 | Anon->setInvalidDecl(); |
| 3829 | |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3830 | return Anon; |
| Chris Lattner | b6738ec | 2007-01-28 00:38:24 +0000 | [diff] [blame] | 3831 | } |
| 3832 | |
| Francois Pichet | 0c71f6c | 2010-11-23 06:07:27 +0000 | [diff] [blame] | 3833 | /// BuildMicrosoftCAnonymousStruct - Handle the declaration of an |
| 3834 | /// Microsoft C anonymous structure. |
| 3835 | /// Ref: http://msdn.microsoft.com/en-us/library/z2cx9y4f.aspx |
| 3836 | /// Example: |
| 3837 | /// |
| 3838 | /// struct A { int a; }; |
| 3839 | /// struct B { struct A; int b; }; |
| 3840 | /// |
| 3841 | /// void foo() { |
| 3842 | /// B var; |
| 3843 | /// var.a = 3; |
| 3844 | /// } |
| 3845 | /// |
| 3846 | Decl *Sema::BuildMicrosoftCAnonymousStruct(Scope *S, DeclSpec &DS, |
| 3847 | RecordDecl *Record) { |
| 3848 | |
| 3849 | // If there is no Record, get the record via the typedef. |
| 3850 | if (!Record) |
| 3851 | Record = DS.getRepAsType().get()->getAsStructureType()->getDecl(); |
| 3852 | |
| 3853 | // Mock up a declarator. |
| 3854 | Declarator Dc(DS, Declarator::TypeNameContext); |
| 3855 | TypeSourceInfo *TInfo = GetTypeForDeclarator(Dc, S); |
| 3856 | assert(TInfo && "couldn't build declarator info for anonymous struct"); |
| 3857 | |
| 3858 | // Create a declaration for this anonymous struct. |
| 3859 | NamedDecl* Anon = FieldDecl::Create(Context, |
| 3860 | cast<RecordDecl>(CurContext), |
| Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3861 | DS.getLocStart(), |
| 3862 | DS.getLocStart(), |
| Francois Pichet | 0c71f6c | 2010-11-23 06:07:27 +0000 | [diff] [blame] | 3863 | /*IdentifierInfo=*/0, |
| 3864 | Context.getTypeDeclType(Record), |
| 3865 | TInfo, |
| Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3866 | /*BitWidth=*/0, /*Mutable=*/false, |
| Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 3867 | /*InitStyle=*/ICIS_NoInit); |
| Francois Pichet | 0c71f6c | 2010-11-23 06:07:27 +0000 | [diff] [blame] | 3868 | Anon->setImplicit(); |
| 3869 | |
| 3870 | // Add the anonymous struct object to the current context. |
| 3871 | CurContext->addDecl(Anon); |
| 3872 | |
| 3873 | // Inject the members of the anonymous struct into the current |
| 3874 | // context and into the identifier resolver chain for name lookup |
| 3875 | // purposes. |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3876 | SmallVector<NamedDecl*, 2> Chain; |
| Francois Pichet | 0c71f6c | 2010-11-23 06:07:27 +0000 | [diff] [blame] | 3877 | Chain.push_back(Anon); |
| 3878 | |
| Nico Weber | f8bb3de | 2012-02-01 00:41:00 +0000 | [diff] [blame] | 3879 | RecordDecl *RecordDef = Record->getDefinition(); |
| 3880 | if (!RecordDef || InjectAnonymousStructOrUnionMembers(*this, S, CurContext, |
| 3881 | RecordDef, AS_none, |
| 3882 | Chain, true)) |
| Francois Pichet | 0c71f6c | 2010-11-23 06:07:27 +0000 | [diff] [blame] | 3883 | Anon->setInvalidDecl(); |
| 3884 | |
| 3885 | return Anon; |
| 3886 | } |
| Steve Naroff | 2fea139 | 2007-09-02 02:04:30 +0000 | [diff] [blame] | 3887 | |
| Douglas Gregor | 92751d4 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 3888 | /// GetNameForDeclarator - Determine the full declaration name for the |
| 3889 | /// given Declarator. |
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3890 | DeclarationNameInfo Sema::GetNameForDeclarator(Declarator &D) { |
| Douglas Gregor | a121b75 | 2009-11-03 16:56:39 +0000 | [diff] [blame] | 3891 | return GetNameFromUnqualifiedId(D.getName()); |
| 3892 | } |
| 3893 | |
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3894 | /// \brief Retrieves the declaration name from a parsed unqualified-id. |
| 3895 | DeclarationNameInfo |
| 3896 | Sema::GetNameFromUnqualifiedId(const UnqualifiedId &Name) { |
| 3897 | DeclarationNameInfo NameInfo; |
| 3898 | NameInfo.setLoc(Name.StartLocation); |
| 3899 | |
| Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 3900 | switch (Name.getKind()) { |
| Alexis Hunt | 3445850 | 2009-11-28 04:44:28 +0000 | [diff] [blame] | 3901 | |
| Fariborz Jahanian | 7f4427f | 2011-07-12 17:16:56 +0000 | [diff] [blame] | 3902 | case UnqualifiedId::IK_ImplicitSelfParam: |
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3903 | case UnqualifiedId::IK_Identifier: |
| 3904 | NameInfo.setName(Name.Identifier); |
| 3905 | NameInfo.setLoc(Name.StartLocation); |
| 3906 | return NameInfo; |
| Alexis Hunt | 3445850 | 2009-11-28 04:44:28 +0000 | [diff] [blame] | 3907 | |
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3908 | case UnqualifiedId::IK_OperatorFunctionId: |
| 3909 | NameInfo.setName(Context.DeclarationNames.getCXXOperatorName( |
| 3910 | Name.OperatorFunctionId.Operator)); |
| 3911 | NameInfo.setLoc(Name.StartLocation); |
| 3912 | NameInfo.getInfo().CXXOperatorName.BeginOpNameLoc |
| 3913 | = Name.OperatorFunctionId.SymbolLocations[0]; |
| 3914 | NameInfo.getInfo().CXXOperatorName.EndOpNameLoc |
| 3915 | = Name.EndLocation.getRawEncoding(); |
| 3916 | return NameInfo; |
| Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3917 | |
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3918 | case UnqualifiedId::IK_LiteralOperatorId: |
| 3919 | NameInfo.setName(Context.DeclarationNames.getCXXLiteralOperatorName( |
| 3920 | Name.Identifier)); |
| 3921 | NameInfo.setLoc(Name.StartLocation); |
| 3922 | NameInfo.setCXXLiteralOperatorNameLoc(Name.EndLocation); |
| 3923 | return NameInfo; |
| Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3924 | |
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3925 | case UnqualifiedId::IK_ConversionFunctionId: { |
| 3926 | TypeSourceInfo *TInfo; |
| 3927 | QualType Ty = GetTypeFromParser(Name.ConversionFunctionId, &TInfo); |
| 3928 | if (Ty.isNull()) |
| 3929 | return DeclarationNameInfo(); |
| 3930 | NameInfo.setName(Context.DeclarationNames.getCXXConversionFunctionName( |
| 3931 | Context.getCanonicalType(Ty))); |
| 3932 | NameInfo.setLoc(Name.StartLocation); |
| 3933 | NameInfo.setNamedTypeInfo(TInfo); |
| 3934 | return NameInfo; |
| Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 3935 | } |
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3936 | |
| 3937 | case UnqualifiedId::IK_ConstructorName: { |
| 3938 | TypeSourceInfo *TInfo; |
| 3939 | QualType Ty = GetTypeFromParser(Name.ConstructorName, &TInfo); |
| 3940 | if (Ty.isNull()) |
| 3941 | return DeclarationNameInfo(); |
| 3942 | NameInfo.setName(Context.DeclarationNames.getCXXConstructorName( |
| 3943 | Context.getCanonicalType(Ty))); |
| 3944 | NameInfo.setLoc(Name.StartLocation); |
| 3945 | NameInfo.setNamedTypeInfo(TInfo); |
| 3946 | return NameInfo; |
| 3947 | } |
| 3948 | |
| 3949 | case UnqualifiedId::IK_ConstructorTemplateId: { |
| 3950 | // In well-formed code, we can only have a constructor |
| 3951 | // template-id that refers to the current context, so go there |
| 3952 | // to find the actual type being constructed. |
| 3953 | CXXRecordDecl *CurClass = dyn_cast<CXXRecordDecl>(CurContext); |
| 3954 | if (!CurClass || CurClass->getIdentifier() != Name.TemplateId->Name) |
| 3955 | return DeclarationNameInfo(); |
| 3956 | |
| 3957 | // Determine the type of the class being constructed. |
| 3958 | QualType CurClassType = Context.getTypeDeclType(CurClass); |
| 3959 | |
| 3960 | // FIXME: Check two things: that the template-id names the same type as |
| 3961 | // CurClassType, and that the template-id does not occur when the name |
| 3962 | // was qualified. |
| 3963 | |
| 3964 | NameInfo.setName(Context.DeclarationNames.getCXXConstructorName( |
| 3965 | Context.getCanonicalType(CurClassType))); |
| 3966 | NameInfo.setLoc(Name.StartLocation); |
| 3967 | // FIXME: should we retrieve TypeSourceInfo? |
| 3968 | NameInfo.setNamedTypeInfo(0); |
| 3969 | return NameInfo; |
| 3970 | } |
| 3971 | |
| 3972 | case UnqualifiedId::IK_DestructorName: { |
| 3973 | TypeSourceInfo *TInfo; |
| 3974 | QualType Ty = GetTypeFromParser(Name.DestructorName, &TInfo); |
| 3975 | if (Ty.isNull()) |
| 3976 | return DeclarationNameInfo(); |
| 3977 | NameInfo.setName(Context.DeclarationNames.getCXXDestructorName( |
| 3978 | Context.getCanonicalType(Ty))); |
| 3979 | NameInfo.setLoc(Name.StartLocation); |
| 3980 | NameInfo.setNamedTypeInfo(TInfo); |
| 3981 | return NameInfo; |
| 3982 | } |
| 3983 | |
| 3984 | case UnqualifiedId::IK_TemplateId: { |
| John McCall | 3e56fd4 | 2010-08-23 07:28:44 +0000 | [diff] [blame] | 3985 | TemplateName TName = Name.TemplateId->Template.get(); |
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3986 | SourceLocation TNameLoc = Name.TemplateId->TemplateNameLoc; |
| 3987 | return Context.getNameForTemplate(TName, TNameLoc); |
| 3988 | } |
| 3989 | |
| 3990 | } // switch (Name.getKind()) |
| 3991 | |
| David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3992 | llvm_unreachable("Unknown name kind"); |
| Douglas Gregor | 92751d4 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 3993 | } |
| 3994 | |
| Kaelyn Uhrain | 7d9bc63 | 2011-08-04 17:40:00 +0000 | [diff] [blame] | 3995 | static QualType getCoreType(QualType Ty) { |
| 3996 | do { |
| 3997 | if (Ty->isPointerType() || Ty->isReferenceType()) |
| 3998 | Ty = Ty->getPointeeType(); |
| 3999 | else if (Ty->isArrayType()) |
| 4000 | Ty = Ty->castAsArrayTypeUnsafe()->getElementType(); |
| 4001 | else |
| 4002 | return Ty.withoutLocalFastQualifiers(); |
| 4003 | } while (true); |
| 4004 | } |
| 4005 | |
| Kaelyn Uhrain | 0a32fca | 2011-10-11 00:28:39 +0000 | [diff] [blame] | 4006 | /// hasSimilarParameters - Determine whether the C++ functions Declaration |
| 4007 | /// and Definition have "nearly" matching parameters. This heuristic is |
| 4008 | /// used to improve diagnostics in the case where an out-of-line function |
| 4009 | /// definition doesn't match any declaration within the class or namespace. |
| 4010 | /// Also sets Params to the list of indices to the parameters that differ |
| 4011 | /// between the declaration and the definition. If hasSimilarParameters |
| 4012 | /// returns true and Params is empty, then all of the parameters match. |
| 4013 | static bool hasSimilarParameters(ASTContext &Context, |
| Douglas Gregor | 8af63e4 | 2009-02-06 17:46:57 +0000 | [diff] [blame] | 4014 | FunctionDecl *Declaration, |
| Kaelyn Uhrain | 7d9bc63 | 2011-08-04 17:40:00 +0000 | [diff] [blame] | 4015 | FunctionDecl *Definition, |
| Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 4016 | SmallVectorImpl<unsigned> &Params) { |
| Kaelyn Uhrain | 7d9bc63 | 2011-08-04 17:40:00 +0000 | [diff] [blame] | 4017 | Params.clear(); |
| Douglas Gregor | ad59050 | 2008-12-15 23:53:10 +0000 | [diff] [blame] | 4018 | if (Declaration->param_size() != Definition->param_size()) |
| 4019 | return false; |
| 4020 | for (unsigned Idx = 0; Idx < Declaration->param_size(); ++Idx) { |
| 4021 | QualType DeclParamTy = Declaration->getParamDecl(Idx)->getType(); |
| 4022 | QualType DefParamTy = Definition->getParamDecl(Idx)->getType(); |
| 4023 | |
| Kaelyn Uhrain | 7d9bc63 | 2011-08-04 17:40:00 +0000 | [diff] [blame] | 4024 | // The parameter types are identical |
| Matt Beaumont-Gay | 56381b8 | 2011-08-23 01:35:51 +0000 | [diff] [blame] | 4025 | if (Context.hasSameType(DefParamTy, DeclParamTy)) |
| Kaelyn Uhrain | 7d9bc63 | 2011-08-04 17:40:00 +0000 | [diff] [blame] | 4026 | continue; |
| 4027 | |
| 4028 | QualType DeclParamBaseTy = getCoreType(DeclParamTy); |
| 4029 | QualType DefParamBaseTy = getCoreType(DefParamTy); |
| 4030 | const IdentifierInfo *DeclTyName = DeclParamBaseTy.getBaseTypeIdentifier(); |
| 4031 | const IdentifierInfo *DefTyName = DefParamBaseTy.getBaseTypeIdentifier(); |
| 4032 | |
| 4033 | if (Context.hasSameUnqualifiedType(DeclParamBaseTy, DefParamBaseTy) || |
| 4034 | (DeclTyName && DeclTyName == DefTyName)) |
| 4035 | Params.push_back(Idx); |
| 4036 | else // The two parameters aren't even close |
| Douglas Gregor | ad59050 | 2008-12-15 23:53:10 +0000 | [diff] [blame] | 4037 | return false; |
| 4038 | } |
| 4039 | |
| 4040 | return true; |
| 4041 | } |
| 4042 | |
| John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 4043 | /// NeedsRebuildingInCurrentInstantiation - Checks whether the given |
| 4044 | /// declarator needs to be rebuilt in the current instantiation. |
| 4045 | /// Any bits of declarator which appear before the name are valid for |
| 4046 | /// consideration here. That's specifically the type in the decl spec |
| 4047 | /// and the base type in any member-pointer chunks. |
| 4048 | static bool RebuildDeclaratorInCurrentInstantiation(Sema &S, Declarator &D, |
| 4049 | DeclarationName Name) { |
| 4050 | // The types we specifically need to rebuild are: |
| 4051 | // - typenames, typeofs, and decltypes |
| 4052 | // - types which will become injected class names |
| 4053 | // Of course, we also need to rebuild any type referencing such a |
| 4054 | // type. It's safest to just say "dependent", but we call out a |
| 4055 | // few cases here. |
| 4056 | |
| 4057 | DeclSpec &DS = D.getMutableDeclSpec(); |
| 4058 | switch (DS.getTypeSpecType()) { |
| 4059 | case DeclSpec::TST_typename: |
| 4060 | case DeclSpec::TST_typeofType: |
| Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4061 | case DeclSpec::TST_underlyingType: |
| 4062 | case DeclSpec::TST_atomic: { |
| John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 4063 | // Grab the type from the parser. |
| 4064 | TypeSourceInfo *TSI = 0; |
| John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4065 | QualType T = S.GetTypeFromParser(DS.getRepAsType(), &TSI); |
| John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 4066 | if (T.isNull() || !T->isDependentType()) break; |
| 4067 | |
| 4068 | // Make sure there's a type source info. This isn't really much |
| 4069 | // of a waste; most dependent types should have type source info |
| 4070 | // attached already. |
| 4071 | if (!TSI) |
| 4072 | TSI = S.Context.getTrivialTypeSourceInfo(T, DS.getTypeSpecTypeLoc()); |
| 4073 | |
| 4074 | // Rebuild the type in the current instantiation. |
| 4075 | TSI = S.RebuildTypeInCurrentInstantiation(TSI, D.getIdentifierLoc(), Name); |
| 4076 | if (!TSI) return true; |
| 4077 | |
| 4078 | // Store the new type back in the decl spec. |
| John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4079 | ParsedType LocType = S.CreateParsedType(TSI->getType(), TSI); |
| 4080 | DS.UpdateTypeRep(LocType); |
| 4081 | break; |
| 4082 | } |
| 4083 | |
| Richard Smith | 1620ebd | 2012-10-01 20:35:07 +0000 | [diff] [blame] | 4084 | case DeclSpec::TST_decltype: |
| John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4085 | case DeclSpec::TST_typeofExpr: { |
| 4086 | Expr *E = DS.getRepAsExpr(); |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4087 | ExprResult Result = S.RebuildExprInCurrentInstantiation(E); |
| John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4088 | if (Result.isInvalid()) return true; |
| 4089 | DS.UpdateExprRep(Result.get()); |
| John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 4090 | break; |
| 4091 | } |
| 4092 | |
| 4093 | default: |
| 4094 | // Nothing to do for these decl specs. |
| 4095 | break; |
| 4096 | } |
| 4097 | |
| 4098 | // It doesn't matter what order we do this in. |
| 4099 | for (unsigned I = 0, E = D.getNumTypeObjects(); I != E; ++I) { |
| 4100 | DeclaratorChunk &Chunk = D.getTypeObject(I); |
| 4101 | |
| 4102 | // The only type information in the declarator which can come |
| 4103 | // before the declaration name is the base type of a member |
| 4104 | // pointer. |
| 4105 | if (Chunk.Kind != DeclaratorChunk::MemberPointer) |
| 4106 | continue; |
| 4107 | |
| 4108 | // Rebuild the scope specifier in-place. |
| 4109 | CXXScopeSpec &SS = Chunk.Mem.Scope(); |
| 4110 | if (S.RebuildNestedNameSpecifierInCurrentInstantiation(SS)) |
| 4111 | return true; |
| 4112 | } |
| 4113 | |
| 4114 | return false; |
| 4115 | } |
| 4116 | |
| Anders Carlsson | 1052fd7 | 2011-07-04 16:28:17 +0000 | [diff] [blame] | 4117 | Decl *Sema::ActOnDeclarator(Scope *S, Declarator &D) { |
| Douglas Gregor | 5d1b4e3 | 2011-11-07 20:56:01 +0000 | [diff] [blame] | 4118 | D.setFunctionDefinitionKind(FDK_Declaration); |
| Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 4119 | Decl *Dcl = HandleDeclarator(S, D, MultiTemplateParamsArg()); |
| Argyrios Kyrtzidis | 8ad3bab | 2011-11-23 20:27:36 +0000 | [diff] [blame] | 4120 | |
| 4121 | if (OriginalLexicalContext && OriginalLexicalContext->isObjCContainer() && |
| Douglas Gregor | 205b068 | 2012-04-30 18:13:01 +0000 | [diff] [blame] | 4122 | Dcl && Dcl->getDeclContext()->isFileContext()) |
| Argyrios Kyrtzidis | 8ad3bab | 2011-11-23 20:27:36 +0000 | [diff] [blame] | 4123 | Dcl->setTopLevelDeclInObjCContainer(); |
| 4124 | |
| 4125 | return Dcl; |
| John McCall | de6836a | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 4126 | } |
| 4127 | |
| Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4128 | /// DiagnoseClassNameShadow - Implement C++ [class.mem]p13: |
| 4129 | /// If T is the name of a class, then each of the following shall have a |
| 4130 | /// name different from T: |
| 4131 | /// - every static data member of class T; |
| 4132 | /// - every member function of class T |
| 4133 | /// - every member of class T that is itself a type; |
| 4134 | /// \returns true if the declaration name violates these rules. |
| 4135 | bool Sema::DiagnoseClassNameShadow(DeclContext *DC, |
| 4136 | DeclarationNameInfo NameInfo) { |
| 4137 | DeclarationName Name = NameInfo.getName(); |
| 4138 | |
| 4139 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(DC)) |
| 4140 | if (Record->getIdentifier() && Record->getDeclName() == Name) { |
| 4141 | Diag(NameInfo.getLoc(), diag::err_member_name_of_class) << Name; |
| 4142 | return true; |
| 4143 | } |
| 4144 | |
| 4145 | return false; |
| 4146 | } |
| Douglas Gregor | 31feb33 | 2012-03-17 23:06:31 +0000 | [diff] [blame] | 4147 | |
| Douglas Gregor | b7d17dd | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 4148 | /// \brief Diagnose a declaration whose declarator-id has the given |
| 4149 | /// nested-name-specifier. |
| 4150 | /// |
| 4151 | /// \param SS The nested-name-specifier of the declarator-id. |
| 4152 | /// |
| 4153 | /// \param DC The declaration context to which the nested-name-specifier |
| 4154 | /// resolves. |
| 4155 | /// |
| 4156 | /// \param Name The name of the entity being declared. |
| 4157 | /// |
| 4158 | /// \param Loc The location of the name of the entity being declared. |
| Douglas Gregor | 31feb33 | 2012-03-17 23:06:31 +0000 | [diff] [blame] | 4159 | /// |
| 4160 | /// \returns true if we cannot safely recover from this error, false otherwise. |
| Douglas Gregor | b7d17dd | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 4161 | bool Sema::diagnoseQualifiedDeclaration(CXXScopeSpec &SS, DeclContext *DC, |
| Douglas Gregor | 31feb33 | 2012-03-17 23:06:31 +0000 | [diff] [blame] | 4162 | DeclarationName Name, |
| Richard Smith | a230224 | 2013-12-05 07:51:02 +0000 | [diff] [blame] | 4163 | SourceLocation Loc) { |
| Douglas Gregor | b7d17dd | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 4164 | DeclContext *Cur = CurContext; |
| Eli Friedman | e2358c1 | 2013-08-12 21:54:01 +0000 | [diff] [blame] | 4165 | while (isa<LinkageSpecDecl>(Cur) || isa<CapturedDecl>(Cur)) |
| Douglas Gregor | b7d17dd | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 4166 | Cur = Cur->getParent(); |
| Richard Smith | a230224 | 2013-12-05 07:51:02 +0000 | [diff] [blame] | 4167 | |
| 4168 | // If the user provided a superfluous scope specifier that refers back to the |
| 4169 | // class in which the entity is already declared, diagnose and ignore it. |
| Douglas Gregor | 31feb33 | 2012-03-17 23:06:31 +0000 | [diff] [blame] | 4170 | // |
| 4171 | // class X { |
| 4172 | // void X::f(); |
| 4173 | // }; |
| Richard Smith | a230224 | 2013-12-05 07:51:02 +0000 | [diff] [blame] | 4174 | // |
| 4175 | // Note, it was once ill-formed to give redundant qualification in all |
| 4176 | // contexts, but that rule was removed by DR482. |
| Douglas Gregor | b7d17dd | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 4177 | if (Cur->Equals(DC)) { |
| Richard Smith | a230224 | 2013-12-05 07:51:02 +0000 | [diff] [blame] | 4178 | if (Cur->isRecord()) { |
| 4179 | Diag(Loc, LangOpts.MicrosoftExt ? diag::warn_member_extra_qualification |
| 4180 | : diag::err_member_extra_qualification) |
| 4181 | << Name << FixItHint::CreateRemoval(SS.getRange()); |
| 4182 | SS.clear(); |
| 4183 | } else { |
| 4184 | Diag(Loc, diag::warn_namespace_member_extra_qualification) << Name; |
| 4185 | } |
| Douglas Gregor | 31feb33 | 2012-03-17 23:06:31 +0000 | [diff] [blame] | 4186 | return false; |
| Richard Smith | a230224 | 2013-12-05 07:51:02 +0000 | [diff] [blame] | 4187 | } |
| Douglas Gregor | b7d17dd | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 4188 | |
| 4189 | // Check whether the qualifying scope encloses the scope of the original |
| 4190 | // declaration. |
| 4191 | if (!Cur->Encloses(DC)) { |
| 4192 | if (Cur->isRecord()) |
| 4193 | Diag(Loc, diag::err_member_qualification) |
| 4194 | << Name << SS.getRange(); |
| 4195 | else if (isa<TranslationUnitDecl>(DC)) |
| 4196 | Diag(Loc, diag::err_invalid_declarator_global_scope) |
| 4197 | << Name << SS.getRange(); |
| 4198 | else if (isa<FunctionDecl>(Cur)) |
| 4199 | Diag(Loc, diag::err_invalid_declarator_in_function) |
| 4200 | << Name << SS.getRange(); |
| Eli Friedman | e2358c1 | 2013-08-12 21:54:01 +0000 | [diff] [blame] | 4201 | else if (isa<BlockDecl>(Cur)) |
| 4202 | Diag(Loc, diag::err_invalid_declarator_in_block) |
| 4203 | << Name << SS.getRange(); |
| Douglas Gregor | b7d17dd | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 4204 | else |
| 4205 | Diag(Loc, diag::err_invalid_declarator_scope) |
| Richard Smith | 8226984 | 2012-04-13 04:07:40 +0000 | [diff] [blame] | 4206 | << Name << cast<NamedDecl>(Cur) << cast<NamedDecl>(DC) << SS.getRange(); |
| Douglas Gregor | b7d17dd | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 4207 | |
| Douglas Gregor | 31feb33 | 2012-03-17 23:06:31 +0000 | [diff] [blame] | 4208 | return true; |
| Douglas Gregor | b7d17dd | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 4209 | } |
| 4210 | |
| 4211 | if (Cur->isRecord()) { |
| 4212 | // Cannot qualify members within a class. |
| 4213 | Diag(Loc, diag::err_member_qualification) |
| 4214 | << Name << SS.getRange(); |
| 4215 | SS.clear(); |
| 4216 | |
| 4217 | // C++ constructors and destructors with incorrect scopes can break |
| 4218 | // our AST invariants by having the wrong underlying types. If |
| 4219 | // that's the case, then drop this declaration entirely. |
| 4220 | if ((Name.getNameKind() == DeclarationName::CXXConstructorName || |
| 4221 | Name.getNameKind() == DeclarationName::CXXDestructorName) && |
| 4222 | !Context.hasSameType(Name.getCXXNameType(), |
| 4223 | Context.getTypeDeclType(cast<CXXRecordDecl>(Cur)))) |
| 4224 | return true; |
| 4225 | |
| 4226 | return false; |
| 4227 | } |
| Douglas Gregor | 31feb33 | 2012-03-17 23:06:31 +0000 | [diff] [blame] | 4228 | |
| Douglas Gregor | b7d17dd | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 4229 | // C++11 [dcl.meaning]p1: |
| 4230 | // [...] "The nested-name-specifier of the qualified declarator-id shall |
| 4231 | // not begin with a decltype-specifer" |
| 4232 | NestedNameSpecifierLoc SpecLoc(SS.getScopeRep(), SS.location_data()); |
| 4233 | while (SpecLoc.getPrefix()) |
| 4234 | SpecLoc = SpecLoc.getPrefix(); |
| 4235 | if (dyn_cast_or_null<DecltypeType>( |
| 4236 | SpecLoc.getNestedNameSpecifier()->getAsType())) |
| 4237 | Diag(Loc, diag::err_decltype_in_declarator) |
| 4238 | << SpecLoc.getTypeLoc().getSourceRange(); |
| 4239 | |
| Douglas Gregor | 31feb33 | 2012-03-17 23:06:31 +0000 | [diff] [blame] | 4240 | return false; |
| 4241 | } |
| 4242 | |
| Rafael Espindola | 0a67e2f | 2013-01-08 20:44:06 +0000 | [diff] [blame] | 4243 | NamedDecl *Sema::HandleDeclarator(Scope *S, Declarator &D, |
| 4244 | MultiTemplateParamsArg TemplateParamLists) { |
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4245 | // TODO: consider using NameInfo for diagnostic. |
| 4246 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); |
| 4247 | DeclarationName Name = NameInfo.getName(); |
| Douglas Gregor | 92751d4 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 4248 | |
| Chris Lattner | 02c0439 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4249 | // All of these full declarators require an identifier. If it doesn't have |
| 4250 | // one, the ParsedFreeStandingDeclSpec action should be used. |
| Douglas Gregor | 92751d4 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 4251 | if (!Name) { |
| Chris Lattner | f6d1c9c | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 4252 | if (!D.isInvalidType()) // Reject this if we think it is valid. |
| Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4253 | Diag(D.getDeclSpec().getLocStart(), |
| Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 4254 | diag::err_declarator_need_ident) |
| 4255 | << D.getDeclSpec().getSourceRange() << D.getSourceRange(); |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4256 | return 0; |
| Douglas Gregor | c435653 | 2010-12-16 00:46:58 +0000 | [diff] [blame] | 4257 | } else if (DiagnoseUnexpandedParameterPack(NameInfo, UPPC_DeclarationType)) |
| 4258 | return 0; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4259 | |
| Chris Lattner | 1a76a3c | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 4260 | // The scope passed in may not be a decl scope. Zip up the scope tree until |
| 4261 | // we find one that is. |
| Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 4262 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 4263 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| Chris Lattner | 1a76a3c | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 4264 | S = S->getParent(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4265 | |
| John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 4266 | DeclContext *DC = CurContext; |
| 4267 | if (D.getCXXScopeSpec().isInvalid()) |
| 4268 | D.setInvalidType(); |
| 4269 | else if (D.getCXXScopeSpec().isSet()) { |
| Douglas Gregor | 6c110f3 | 2010-12-16 01:14:37 +0000 | [diff] [blame] | 4270 | if (DiagnoseUnexpandedParameterPack(D.getCXXScopeSpec(), |
| 4271 | UPPC_DeclarationQualifier)) |
| 4272 | return 0; |
| 4273 | |
| John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 4274 | bool EnteringContext = !D.getDeclSpec().isFriendSpecified(); |
| 4275 | DC = computeDeclContext(D.getCXXScopeSpec(), EnteringContext); |
| Richard Smith | 8ac1c92 | 2013-11-25 21:30:29 +0000 | [diff] [blame] | 4276 | if (!DC || isa<EnumDecl>(DC)) { |
| John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 4277 | // If we could not compute the declaration context, it's because the |
| 4278 | // declaration context is dependent but does not refer to a class, |
| 4279 | // class template, or class template partial specialization. Complain |
| 4280 | // and return early, to avoid the coming semantic disaster. |
| 4281 | Diag(D.getIdentifierLoc(), |
| 4282 | diag::err_template_qualified_declarator_no_match) |
| Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 4283 | << D.getCXXScopeSpec().getScopeRep() |
| John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 4284 | << D.getCXXScopeSpec().getRange(); |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4285 | return 0; |
| John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 4286 | } |
| John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 4287 | bool IsDependentContext = DC->isDependentContext(); |
| John McCall | 4d6d613 | 2009-12-19 09:35:56 +0000 | [diff] [blame] | 4288 | |
| John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 4289 | if (!IsDependentContext && |
| John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 4290 | RequireCompleteDeclContext(D.getCXXScopeSpec(), DC)) |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4291 | return 0; |
| John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 4292 | |
| Douglas Gregor | b7d17dd | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 4293 | if (isa<CXXRecordDecl>(DC) && !cast<CXXRecordDecl>(DC)->hasDefinition()) { |
| 4294 | Diag(D.getIdentifierLoc(), |
| 4295 | diag::err_member_def_undefined_record) |
| 4296 | << Name << DC << D.getCXXScopeSpec().getRange(); |
| 4297 | D.setInvalidType(); |
| 4298 | } else if (!D.getDeclSpec().isFriendSpecified()) { |
| 4299 | if (diagnoseQualifiedDeclaration(D.getCXXScopeSpec(), DC, |
| 4300 | Name, D.getIdentifierLoc())) { |
| 4301 | if (DC->isRecord()) |
| Douglas Gregor | 31feb33 | 2012-03-17 23:06:31 +0000 | [diff] [blame] | 4302 | return 0; |
| Douglas Gregor | b7d17dd | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 4303 | |
| 4304 | D.setInvalidType(); |
| Douglas Gregor | a007d36 | 2010-10-13 22:19:53 +0000 | [diff] [blame] | 4305 | } |
| John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 4306 | } |
| 4307 | |
| 4308 | // Check whether we need to rebuild the type of the given |
| 4309 | // declaration in the current instantiation. |
| 4310 | if (EnteringContext && IsDependentContext && |
| 4311 | TemplateParamLists.size() != 0) { |
| 4312 | ContextRAII SavedContext(*this, DC); |
| 4313 | if (RebuildDeclaratorInCurrentInstantiation(*this, D, Name)) |
| 4314 | D.setInvalidType(); |
| Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 4315 | } |
| 4316 | } |
| Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4317 | |
| 4318 | if (DiagnoseClassNameShadow(DC, NameInfo)) |
| 4319 | // If this is a typedef, we'll end up spewing multiple diagnostics. |
| 4320 | // Just return early; it's safer. |
| 4321 | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) |
| 4322 | return 0; |
| Douglas Gregor | 36c22a2 | 2010-10-15 13:21:21 +0000 | [diff] [blame] | 4323 | |
| John McCall | 8cb7bdf | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 4324 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
| 4325 | QualType R = TInfo->getType(); |
| Douglas Gregor | eddf433 | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 4326 | |
| Douglas Gregor | 506bd56 | 2010-12-13 22:49:22 +0000 | [diff] [blame] | 4327 | if (DiagnoseUnexpandedParameterPack(D.getIdentifierLoc(), TInfo, |
| 4328 | UPPC_DeclarationType)) |
| 4329 | D.setInvalidType(); |
| 4330 | |
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4331 | LookupResult Previous(*this, NameInfo, LookupOrdinaryName, |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4332 | ForRedeclaration); |
| 4333 | |
| Argyrios Kyrtzidis | 16ac9be | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 4334 | // See if this is a redefinition of a variable in the same scope. |
| John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 4335 | if (!D.getCXXScopeSpec().isSet()) { |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4336 | bool IsLinkageLookup = false; |
| Richard Smith | 1c34fb7 | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 4337 | bool CreateBuiltins = false; |
| Douglas Gregor | eddf433 | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 4338 | |
| 4339 | // If the declaration we're planning to build will be a function |
| 4340 | // or object with linkage, then look for another declaration with |
| 4341 | // linkage (C99 6.2.2p4-5 and C++ [basic.link]p6). |
| Richard Smith | 1c34fb7 | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 4342 | // |
| 4343 | // If the declaration we're planning to build will be declared with |
| 4344 | // external linkage in the translation unit, create any builtin with |
| 4345 | // the same name. |
| Douglas Gregor | eddf433 | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 4346 | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) |
| 4347 | /* Do nothing*/; |
| Richard Smith | 1c34fb7 | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 4348 | else if (CurContext->isFunctionOrMethod() && |
| 4349 | (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_extern || |
| 4350 | R->isFunctionType())) { |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4351 | IsLinkageLookup = true; |
| Richard Smith | 1c34fb7 | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 4352 | CreateBuiltins = |
| 4353 | CurContext->getEnclosingNamespaceContext()->isTranslationUnit(); |
| 4354 | } else if (CurContext->getRedeclContext()->isTranslationUnit() && |
| 4355 | D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_static) |
| 4356 | CreateBuiltins = true; |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4357 | |
| 4358 | if (IsLinkageLookup) |
| 4359 | Previous.clear(LookupRedeclarationWithLinkage); |
| Douglas Gregor | eddf433 | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 4360 | |
| Richard Smith | 1c34fb7 | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 4361 | LookupName(Previous, S, CreateBuiltins); |
| Argyrios Kyrtzidis | 16ac9be | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 4362 | } else { // Something like "int foo::x;" |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4363 | LookupQualifiedName(Previous, DC); |
| 4364 | |
| Douglas Gregor | b7d17dd | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 4365 | // C++ [dcl.meaning]p1: |
| 4366 | // When the declarator-id is qualified, the declaration shall refer to a |
| 4367 | // previously declared member of the class or namespace to which the |
| 4368 | // qualifier refers (or, in the case of a namespace, of an element of the |
| 4369 | // inline namespace set of that namespace (7.3.1)) or to a specialization |
| 4370 | // thereof; [...] |
| Argyrios Kyrtzidis | 16ac9be | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 4371 | // |
| Douglas Gregor | b7d17dd | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 4372 | // Note that we already checked the context above, and that we do not have |
| 4373 | // enough information to make sure that Previous contains the declaration |
| 4374 | // we want to match. For example, given: |
| Douglas Gregor | ad59050 | 2008-12-15 23:53:10 +0000 | [diff] [blame] | 4375 | // |
| Douglas Gregor | 4287b37 | 2008-12-12 08:25:50 +0000 | [diff] [blame] | 4376 | // class X { |
| 4377 | // void f(); |
| Douglas Gregor | ad59050 | 2008-12-15 23:53:10 +0000 | [diff] [blame] | 4378 | // void f(float); |
| Douglas Gregor | 4287b37 | 2008-12-12 08:25:50 +0000 | [diff] [blame] | 4379 | // }; |
| 4380 | // |
| Douglas Gregor | ad59050 | 2008-12-15 23:53:10 +0000 | [diff] [blame] | 4381 | // void X::f(int) { } // ill-formed |
| 4382 | // |
| Douglas Gregor | b7d17dd | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 4383 | // In this case, Previous will point to the overload set |
| Douglas Gregor | ad59050 | 2008-12-15 23:53:10 +0000 | [diff] [blame] | 4384 | // containing the two f's declared in X, but neither of them |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4385 | // matches. |
| Douglas Gregor | b7d17dd | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 4386 | |
| 4387 | // C++ [dcl.meaning]p1: |
| 4388 | // [...] the member shall not merely have been introduced by a |
| 4389 | // using-declaration in the scope of the class or namespace nominated by |
| 4390 | // the nested-name-specifier of the declarator-id. |
| 4391 | RemoveUsingDecls(Previous); |
| Argyrios Kyrtzidis | 16ac9be | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 4392 | } |
| 4393 | |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4394 | if (Previous.isSingleResult() && |
| 4395 | Previous.getFoundDecl()->isTemplateParameter()) { |
| Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 4396 | // Maybe we will complain about the shadowed template parameter. |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4397 | if (!D.isInvalidType()) |
| Douglas Gregor | f4ef4d2 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 4398 | DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), |
| 4399 | Previous.getFoundDecl()); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4400 | |
| Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 4401 | // Just pretend that we didn't see the previous declaration. |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4402 | Previous.clear(); |
| Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 4403 | } |
| 4404 | |
| Douglas Gregor | 83a586e | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 4405 | // In C++, the previous declaration we find might be a tag type |
| 4406 | // (class or enum). In this case, the new declaration will hide the |
| Douglas Gregor | fb03466 | 2009-01-28 17:15:10 +0000 | [diff] [blame] | 4407 | // tag type. Note that this does does not apply if we're declaring a |
| 4408 | // typedef (C++ [dcl.typedef]p4). |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4409 | if (Previous.isSingleTagDecl() && |
| Kaelyn Uhrain | 5dfc94b | 2013-12-16 19:25:47 +0000 | [diff] [blame] | 4410 | D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef) |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4411 | Previous.clear(); |
| Douglas Gregor | 83a586e | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 4412 | |
| Richard Smith | 5afcdf3f | 2013-03-06 01:37:38 +0000 | [diff] [blame] | 4413 | // Check that there are no default arguments other than in the parameters |
| 4414 | // of a function declaration (C++ only). |
| 4415 | if (getLangOpts().CPlusPlus) |
| 4416 | CheckExtraCXXDefaultArguments(D); |
| 4417 | |
| Nico Weber | cb4c7f4 | 2012-12-23 00:40:46 +0000 | [diff] [blame] | 4418 | NamedDecl *New; |
| 4419 | |
| Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 4420 | bool AddToScope = true; |
| Chris Lattner | 01a7c53 | 2007-01-25 23:09:03 +0000 | [diff] [blame] | 4421 | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| Douglas Gregor | b52fabb | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 4422 | if (TemplateParamLists.size()) { |
| 4423 | Diag(D.getIdentifierLoc(), diag::err_template_typedef); |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4424 | return 0; |
| Douglas Gregor | b52fabb | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 4425 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4426 | |
| Kaelyn Uhrain | 4dc695d | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 4427 | New = ActOnTypedefDeclarator(S, D, DC, TInfo, Previous); |
| Douglas Gregor | eddf433 | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 4428 | } else if (R->isFunctionType()) { |
| Kaelyn Uhrain | 4dc695d | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 4429 | New = ActOnFunctionDeclarator(S, D, DC, TInfo, Previous, |
| Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 4430 | TemplateParamLists, |
| Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 4431 | AddToScope); |
| Chris Lattner | 01a7c53 | 2007-01-25 23:09:03 +0000 | [diff] [blame] | 4432 | } else { |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4433 | New = ActOnVariableDeclarator(S, D, DC, TInfo, Previous, TemplateParamLists, |
| 4434 | AddToScope); |
| Chris Lattner | 01a7c53 | 2007-01-25 23:09:03 +0000 | [diff] [blame] | 4435 | } |
| Zhongxing Xu | bece5d6 | 2009-01-16 01:13:29 +0000 | [diff] [blame] | 4436 | |
| 4437 | if (New == 0) |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4438 | return 0; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4439 | |
| Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4440 | // If this has an identifier and is not an invalid redeclaration or |
| 4441 | // function template specialization, add it to the scope stack. |
| Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 4442 | if (New->getDeclName() && AddToScope && |
| Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 4443 | !(D.isRedeclaration() && New->isInvalidDecl())) { |
| 4444 | // Only make a locally-scoped extern declaration visible if it is the first |
| 4445 | // declaration of this entity. Qualified lookup for such an entity should |
| 4446 | // only find this declaration if there is no visible declaration of it. |
| 4447 | bool AddToContext = !D.isRedeclaration() || !New->isLocalExternDecl(); |
| 4448 | PushOnScopeChains(New, S, AddToContext); |
| 4449 | if (!AddToContext) |
| 4450 | CurContext->addHiddenDecl(New); |
| 4451 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4452 | |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4453 | return New; |
| Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 4454 | } |
| 4455 | |
| Abramo Bagnara | ad9f2e2 | 2012-11-08 16:27:30 +0000 | [diff] [blame] | 4456 | /// Helper method to turn variable array types into constant array |
| 4457 | /// types in certain situations which would otherwise be errors (for |
| 4458 | /// GCC compatibility). |
| Eli Friedman | a3b1d03 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 4459 | static QualType TryToFixInvalidVariablyModifiedType(QualType T, |
| 4460 | ASTContext &Context, |
| Douglas Gregor | caa1bf4 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 4461 | bool &SizeIsNegative, |
| 4462 | llvm::APSInt &Oversized) { |
| Eli Friedman | a3b1d03 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 4463 | // This method tries to turn a variable array into a constant |
| 4464 | // array even when the size isn't an ICE. This is necessary |
| 4465 | // for compatibility with code that depends on gcc's buggy |
| 4466 | // constant expression folding, like struct {char x[(int)(char*)2];} |
| 4467 | SizeIsNegative = false; |
| Douglas Gregor | caa1bf4 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 4468 | Oversized = 0; |
| 4469 | |
| 4470 | if (T->isDependentType()) |
| 4471 | return QualType(); |
| 4472 | |
| John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4473 | QualifierCollector Qs; |
| 4474 | const Type *Ty = Qs.strip(T); |
| 4475 | |
| 4476 | if (const PointerType* PTy = dyn_cast<PointerType>(Ty)) { |
| Eli Friedman | a3b1d03 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 4477 | QualType Pointee = PTy->getPointeeType(); |
| 4478 | QualType FixedType = |
| Douglas Gregor | caa1bf4 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 4479 | TryToFixInvalidVariablyModifiedType(Pointee, Context, SizeIsNegative, |
| 4480 | Oversized); |
| Eli Friedman | a3b1d03 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 4481 | if (FixedType.isNull()) return FixedType; |
| Eli Friedman | 44c0f2a | 2009-02-21 00:58:02 +0000 | [diff] [blame] | 4482 | FixedType = Context.getPointerType(FixedType); |
| John McCall | 717d9b0 | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 4483 | return Qs.apply(Context, FixedType); |
| Eli Friedman | a3b1d03 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 4484 | } |
| Abramo Bagnara | 924a8f3 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 4485 | if (const ParenType* PTy = dyn_cast<ParenType>(Ty)) { |
| 4486 | QualType Inner = PTy->getInnerType(); |
| 4487 | QualType FixedType = |
| 4488 | TryToFixInvalidVariablyModifiedType(Inner, Context, SizeIsNegative, |
| 4489 | Oversized); |
| 4490 | if (FixedType.isNull()) return FixedType; |
| 4491 | FixedType = Context.getParenType(FixedType); |
| 4492 | return Qs.apply(Context, FixedType); |
| 4493 | } |
| Eli Friedman | a3b1d03 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 4494 | |
| 4495 | const VariableArrayType* VLATy = dyn_cast<VariableArrayType>(T); |
| Eli Friedman | adf40d4 | 2009-02-26 03:58:54 +0000 | [diff] [blame] | 4496 | if (!VLATy) |
| 4497 | return QualType(); |
| 4498 | // FIXME: We should probably handle this case |
| 4499 | if (VLATy->getElementType()->isVariablyModifiedType()) |
| 4500 | return QualType(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4501 | |
| Richard Smith | 42d3af9 | 2011-12-07 00:43:50 +0000 | [diff] [blame] | 4502 | llvm::APSInt Res; |
| Eli Friedman | a3b1d03 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 4503 | if (!VLATy->getSizeExpr() || |
| Richard Smith | 42d3af9 | 2011-12-07 00:43:50 +0000 | [diff] [blame] | 4504 | !VLATy->getSizeExpr()->EvaluateAsInt(Res, Context)) |
| Eli Friedman | a3b1d03 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 4505 | return QualType(); |
| Eli Friedman | adf40d4 | 2009-02-26 03:58:54 +0000 | [diff] [blame] | 4506 | |
| Douglas Gregor | caa1bf4 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 4507 | // Check whether the array size is negative. |
| Douglas Gregor | caa1bf4 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 4508 | if (Res.isSigned() && Res.isNegative()) { |
| 4509 | SizeIsNegative = true; |
| 4510 | return QualType(); |
| Douglas Gregor | 0431825 | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 4511 | } |
| Eli Friedman | a3b1d03 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 4512 | |
| Douglas Gregor | caa1bf4 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 4513 | // Check whether the array is too large to be addressed. |
| 4514 | unsigned ActiveSizeBits |
| 4515 | = ConstantArrayType::getNumAddressingBits(Context, VLATy->getElementType(), |
| 4516 | Res); |
| 4517 | if (ActiveSizeBits > ConstantArrayType::getMaxSizeBits(Context)) { |
| 4518 | Oversized = Res; |
| 4519 | return QualType(); |
| 4520 | } |
| 4521 | |
| 4522 | return Context.getConstantArrayType(VLATy->getElementType(), |
| 4523 | Res, ArrayType::Normal, 0); |
| Eli Friedman | a3b1d03 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 4524 | } |
| 4525 | |
| Abramo Bagnara | 341ab73 | 2012-11-08 14:44:42 +0000 | [diff] [blame] | 4526 | static void |
| 4527 | FixInvalidVariablyModifiedTypeLoc(TypeLoc SrcTL, TypeLoc DstTL) { |
| David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 4528 | if (PointerTypeLoc SrcPTL = SrcTL.getAs<PointerTypeLoc>()) { |
| 4529 | PointerTypeLoc DstPTL = DstTL.castAs<PointerTypeLoc>(); |
| 4530 | FixInvalidVariablyModifiedTypeLoc(SrcPTL.getPointeeLoc(), |
| 4531 | DstPTL.getPointeeLoc()); |
| 4532 | DstPTL.setStarLoc(SrcPTL.getStarLoc()); |
| Abramo Bagnara | 341ab73 | 2012-11-08 14:44:42 +0000 | [diff] [blame] | 4533 | return; |
| 4534 | } |
| David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 4535 | if (ParenTypeLoc SrcPTL = SrcTL.getAs<ParenTypeLoc>()) { |
| 4536 | ParenTypeLoc DstPTL = DstTL.castAs<ParenTypeLoc>(); |
| 4537 | FixInvalidVariablyModifiedTypeLoc(SrcPTL.getInnerLoc(), |
| 4538 | DstPTL.getInnerLoc()); |
| 4539 | DstPTL.setLParenLoc(SrcPTL.getLParenLoc()); |
| 4540 | DstPTL.setRParenLoc(SrcPTL.getRParenLoc()); |
| Abramo Bagnara | 341ab73 | 2012-11-08 14:44:42 +0000 | [diff] [blame] | 4541 | return; |
| 4542 | } |
| David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 4543 | ArrayTypeLoc SrcATL = SrcTL.castAs<ArrayTypeLoc>(); |
| 4544 | ArrayTypeLoc DstATL = DstTL.castAs<ArrayTypeLoc>(); |
| 4545 | TypeLoc SrcElemTL = SrcATL.getElementLoc(); |
| 4546 | TypeLoc DstElemTL = DstATL.getElementLoc(); |
| Abramo Bagnara | 341ab73 | 2012-11-08 14:44:42 +0000 | [diff] [blame] | 4547 | DstElemTL.initializeFullCopy(SrcElemTL); |
| David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 4548 | DstATL.setLBracketLoc(SrcATL.getLBracketLoc()); |
| 4549 | DstATL.setSizeExpr(SrcATL.getSizeExpr()); |
| 4550 | DstATL.setRBracketLoc(SrcATL.getRBracketLoc()); |
| Abramo Bagnara | 341ab73 | 2012-11-08 14:44:42 +0000 | [diff] [blame] | 4551 | } |
| 4552 | |
| Abramo Bagnara | ad9f2e2 | 2012-11-08 16:27:30 +0000 | [diff] [blame] | 4553 | /// Helper method to turn variable array types into constant array |
| 4554 | /// types in certain situations which would otherwise be errors (for |
| 4555 | /// GCC compatibility). |
| Abramo Bagnara | 341ab73 | 2012-11-08 14:44:42 +0000 | [diff] [blame] | 4556 | static TypeSourceInfo* |
| 4557 | TryToFixInvalidVariablyModifiedTypeSourceInfo(TypeSourceInfo *TInfo, |
| 4558 | ASTContext &Context, |
| 4559 | bool &SizeIsNegative, |
| 4560 | llvm::APSInt &Oversized) { |
| 4561 | QualType FixedTy |
| 4562 | = TryToFixInvalidVariablyModifiedType(TInfo->getType(), Context, |
| 4563 | SizeIsNegative, Oversized); |
| 4564 | if (FixedTy.isNull()) |
| 4565 | return 0; |
| 4566 | TypeSourceInfo *FixedTInfo = Context.getTrivialTypeSourceInfo(FixedTy); |
| 4567 | FixInvalidVariablyModifiedTypeLoc(TInfo->getTypeLoc(), |
| 4568 | FixedTInfo->getTypeLoc()); |
| 4569 | return FixedTInfo; |
| 4570 | } |
| 4571 | |
| Richard Smith | 78165b5 | 2013-01-10 23:43:47 +0000 | [diff] [blame] | 4572 | /// \brief Register the given locally-scoped extern "C" declaration so |
| Richard Smith | 39b7968 | 2013-06-18 20:15:12 +0000 | [diff] [blame] | 4573 | /// that it can be found later for redeclarations. We include any extern "C" |
| 4574 | /// declaration that is not visible in the translation unit here, not just |
| 4575 | /// function-scope declarations. |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4576 | void |
| Richard Smith | 39b7968 | 2013-06-18 20:15:12 +0000 | [diff] [blame] | 4577 | Sema::RegisterLocallyScopedExternCDecl(NamedDecl *ND, Scope *S) { |
| Richard Smith | ac974a3 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 4578 | if (!getLangOpts().CPlusPlus && |
| 4579 | ND->getLexicalDeclContext()->getRedeclContext()->isTranslationUnit()) |
| 4580 | // Don't need to track declarations in the TU in C. |
| 4581 | return; |
| 4582 | |
| Douglas Gregor | 5a80bd1 | 2009-03-02 00:19:53 +0000 | [diff] [blame] | 4583 | // Note that we have a locally-scoped external with this name. |
| Richard Smith | ac974a3 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 4584 | // FIXME: There can be multiple such declarations if they are functions marked |
| 4585 | // __attribute__((overloadable)) declared in function scope in C. |
| Richard Smith | 78165b5 | 2013-01-10 23:43:47 +0000 | [diff] [blame] | 4586 | LocallyScopedExternCDecls[ND->getDeclName()] = ND; |
| Douglas Gregor | 5a80bd1 | 2009-03-02 00:19:53 +0000 | [diff] [blame] | 4587 | } |
| 4588 | |
| Richard Smith | 39b7968 | 2013-06-18 20:15:12 +0000 | [diff] [blame] | 4589 | NamedDecl *Sema::findLocallyScopedExternCDecl(DeclarationName Name) { |
| Douglas Gregor | dc5c958 | 2011-07-28 14:20:37 +0000 | [diff] [blame] | 4590 | if (ExternalSource) { |
| 4591 | // Load locally-scoped external decls from the external source. |
| Richard Smith | 39b7968 | 2013-06-18 20:15:12 +0000 | [diff] [blame] | 4592 | // FIXME: This is inefficient. Maybe add a DeclContext for extern "C" decls? |
| Douglas Gregor | dc5c958 | 2011-07-28 14:20:37 +0000 | [diff] [blame] | 4593 | SmallVector<NamedDecl *, 4> Decls; |
| Richard Smith | 78165b5 | 2013-01-10 23:43:47 +0000 | [diff] [blame] | 4594 | ExternalSource->ReadLocallyScopedExternCDecls(Decls); |
| Douglas Gregor | dc5c958 | 2011-07-28 14:20:37 +0000 | [diff] [blame] | 4595 | for (unsigned I = 0, N = Decls.size(); I != N; ++I) { |
| 4596 | llvm::DenseMap<DeclarationName, NamedDecl *>::iterator Pos |
| Richard Smith | 78165b5 | 2013-01-10 23:43:47 +0000 | [diff] [blame] | 4597 | = LocallyScopedExternCDecls.find(Decls[I]->getDeclName()); |
| 4598 | if (Pos == LocallyScopedExternCDecls.end()) |
| 4599 | LocallyScopedExternCDecls[Decls[I]->getDeclName()] = Decls[I]; |
| Douglas Gregor | dc5c958 | 2011-07-28 14:20:37 +0000 | [diff] [blame] | 4600 | } |
| 4601 | } |
| Richard Smith | 39b7968 | 2013-06-18 20:15:12 +0000 | [diff] [blame] | 4602 | |
| 4603 | NamedDecl *D = LocallyScopedExternCDecls.lookup(Name); |
| Rafael Espindola | 7b56f6c | 2013-10-19 16:55:03 +0000 | [diff] [blame] | 4604 | return D ? D->getMostRecentDecl() : 0; |
| Douglas Gregor | dc5c958 | 2011-07-28 14:20:37 +0000 | [diff] [blame] | 4605 | } |
| 4606 | |
| Eli Friedman | 574c745 | 2009-04-07 19:37:57 +0000 | [diff] [blame] | 4607 | /// \brief Diagnose function specifiers on a declaration of an identifier that |
| 4608 | /// does not identify a function. |
| Richard Smith | b1402ae | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 4609 | void Sema::DiagnoseFunctionSpecifiers(const DeclSpec &DS) { |
| Eli Friedman | 574c745 | 2009-04-07 19:37:57 +0000 | [diff] [blame] | 4610 | // FIXME: We should probably indicate the identifier in question to avoid |
| 4611 | // confusion for constructs like "inline int a(), b;" |
| Richard Smith | b1402ae | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 4612 | if (DS.isInlineSpecified()) |
| 4613 | Diag(DS.getInlineSpecLoc(), |
| Eli Friedman | 574c745 | 2009-04-07 19:37:57 +0000 | [diff] [blame] | 4614 | diag::err_inline_non_function); |
| 4615 | |
| Richard Smith | b1402ae | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 4616 | if (DS.isVirtualSpecified()) |
| 4617 | Diag(DS.getVirtualSpecLoc(), |
| Eli Friedman | 574c745 | 2009-04-07 19:37:57 +0000 | [diff] [blame] | 4618 | diag::err_virtual_non_function); |
| 4619 | |
| Richard Smith | b1402ae | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 4620 | if (DS.isExplicitSpecified()) |
| 4621 | Diag(DS.getExplicitSpecLoc(), |
| Eli Friedman | 574c745 | 2009-04-07 19:37:57 +0000 | [diff] [blame] | 4622 | diag::err_explicit_non_function); |
| Richard Smith | 0015f09 | 2013-01-17 22:16:11 +0000 | [diff] [blame] | 4623 | |
| Richard Smith | b1402ae | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 4624 | if (DS.isNoreturnSpecified()) |
| 4625 | Diag(DS.getNoreturnSpecLoc(), |
| Richard Smith | 0015f09 | 2013-01-17 22:16:11 +0000 | [diff] [blame] | 4626 | diag::err_noreturn_non_function); |
| Eli Friedman | 574c745 | 2009-04-07 19:37:57 +0000 | [diff] [blame] | 4627 | } |
| 4628 | |
| Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 4629 | NamedDecl* |
| Zhongxing Xu | ac8ef9e | 2009-01-16 03:34:13 +0000 | [diff] [blame] | 4630 | Sema::ActOnTypedefDeclarator(Scope* S, Declarator& D, DeclContext* DC, |
| Kaelyn Uhrain | 4dc695d | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 4631 | TypeSourceInfo *TInfo, LookupResult &Previous) { |
| Zhongxing Xu | ac8ef9e | 2009-01-16 03:34:13 +0000 | [diff] [blame] | 4632 | // Typedef declarators cannot be qualified (C++ [dcl.meaning]p1). |
| 4633 | if (D.getCXXScopeSpec().isSet()) { |
| 4634 | Diag(D.getIdentifierLoc(), diag::err_qualified_typedef_declarator) |
| 4635 | << D.getCXXScopeSpec().getRange(); |
| Chris Lattner | f6d1c9c | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 4636 | D.setInvalidType(); |
| Zhongxing Xu | ac8ef9e | 2009-01-16 03:34:13 +0000 | [diff] [blame] | 4637 | // Pretend we didn't see the scope specifier. |
| Douglas Gregor | b525ef8 | 2010-03-23 15:26:55 +0000 | [diff] [blame] | 4638 | DC = CurContext; |
| 4639 | Previous.clear(); |
| Zhongxing Xu | ac8ef9e | 2009-01-16 03:34:13 +0000 | [diff] [blame] | 4640 | } |
| 4641 | |
| Richard Smith | b1402ae | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 4642 | DiagnoseFunctionSpecifiers(D.getDeclSpec()); |
| Eli Friedman | 574c745 | 2009-04-07 19:37:57 +0000 | [diff] [blame] | 4643 | |
| Richard Smith | a77a0a6 | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 4644 | if (D.getDeclSpec().isConstexprSpecified()) |
| 4645 | Diag(D.getDeclSpec().getConstexprSpecLoc(), diag::err_invalid_constexpr) |
| 4646 | << 1; |
| Eli Friedman | d5c0eed | 2009-04-19 20:27:55 +0000 | [diff] [blame] | 4647 | |
| Douglas Gregor | d8f446f | 2010-07-13 06:37:01 +0000 | [diff] [blame] | 4648 | if (D.getName().Kind != UnqualifiedId::IK_Identifier) { |
| 4649 | Diag(D.getName().StartLocation, diag::err_typedef_not_identifier) |
| 4650 | << D.getName().getSourceRange(); |
| 4651 | return 0; |
| 4652 | } |
| 4653 | |
| Kaelyn Uhrain | 4dc695d | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 4654 | TypedefDecl *NewTD = ParseTypedefDecl(S, D, TInfo->getType(), TInfo); |
| Zhongxing Xu | ac8ef9e | 2009-01-16 03:34:13 +0000 | [diff] [blame] | 4655 | if (!NewTD) return 0; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4656 | |
| Zhongxing Xu | ac8ef9e | 2009-01-16 03:34:13 +0000 | [diff] [blame] | 4657 | // Handle attributes prior to checking for duplicates in MergeVarDecl |
| Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 4658 | ProcessDeclAttributes(S, NewTD, D); |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4659 | |
| Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4660 | CheckTypedefForVariablyModifiedType(S, NewTD); |
| 4661 | |
| Kaelyn Uhrain | 4dc695d | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 4662 | bool Redeclaration = D.isRedeclaration(); |
| 4663 | NamedDecl *ND = ActOnTypedefNameDecl(S, DC, NewTD, Previous, Redeclaration); |
| 4664 | D.setRedeclaration(Redeclaration); |
| 4665 | return ND; |
| Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4666 | } |
| 4667 | |
| Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4668 | void |
| 4669 | Sema::CheckTypedefForVariablyModifiedType(Scope *S, TypedefNameDecl *NewTD) { |
| Chris Lattner | 9fecd74 | 2009-04-19 05:21:20 +0000 | [diff] [blame] | 4670 | // C99 6.7.7p2: If a typedef name specifies a variably modified type |
| 4671 | // then it shall have block scope. |
| Eli Friedman | 88f4ed9 | 2010-08-10 03:13:15 +0000 | [diff] [blame] | 4672 | // Note that variably modified types must be fixed before merging the decl so |
| 4673 | // that redeclarations will match. |
| Abramo Bagnara | 341ab73 | 2012-11-08 14:44:42 +0000 | [diff] [blame] | 4674 | TypeSourceInfo *TInfo = NewTD->getTypeSourceInfo(); |
| 4675 | QualType T = TInfo->getType(); |
| Chris Lattner | 9fecd74 | 2009-04-19 05:21:20 +0000 | [diff] [blame] | 4676 | if (T->isVariablyModifiedType()) { |
| John McCall | aab3e41 | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 4677 | getCurFunction()->setHasBranchProtectedScope(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4678 | |
| Chris Lattner | 9fecd74 | 2009-04-19 05:21:20 +0000 | [diff] [blame] | 4679 | if (S->getFnParent() == 0) { |
| Eli Friedman | a3b1d03 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 4680 | bool SizeIsNegative; |
| Douglas Gregor | caa1bf4 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 4681 | llvm::APSInt Oversized; |
| Abramo Bagnara | 341ab73 | 2012-11-08 14:44:42 +0000 | [diff] [blame] | 4682 | TypeSourceInfo *FixedTInfo = |
| 4683 | TryToFixInvalidVariablyModifiedTypeSourceInfo(TInfo, Context, |
| 4684 | SizeIsNegative, |
| 4685 | Oversized); |
| 4686 | if (FixedTInfo) { |
| Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4687 | Diag(NewTD->getLocation(), diag::warn_illegal_constant_array_size); |
| Abramo Bagnara | 341ab73 | 2012-11-08 14:44:42 +0000 | [diff] [blame] | 4688 | NewTD->setTypeSourceInfo(FixedTInfo); |
| Eli Friedman | a3b1d03 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 4689 | } else { |
| 4690 | if (SizeIsNegative) |
| Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4691 | Diag(NewTD->getLocation(), diag::err_typecheck_negative_array_size); |
| Eli Friedman | a3b1d03 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 4692 | else if (T->isVariableArrayType()) |
| Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4693 | Diag(NewTD->getLocation(), diag::err_vla_decl_in_file_scope); |
| Douglas Gregor | caa1bf4 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 4694 | else if (Oversized.getBoolValue()) |
| David Blaikie | 30d1544 | 2011-10-19 22:56:21 +0000 | [diff] [blame] | 4695 | Diag(NewTD->getLocation(), diag::err_array_too_large) |
| 4696 | << Oversized.toString(10); |
| Eli Friedman | a3b1d03 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 4697 | else |
| Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4698 | Diag(NewTD->getLocation(), diag::err_vm_decl_in_file_scope); |
| Chris Lattner | f6d1c9c | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 4699 | NewTD->setInvalidDecl(); |
| Eli Friedman | a3b1d03 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 4700 | } |
| Zhongxing Xu | ac8ef9e | 2009-01-16 03:34:13 +0000 | [diff] [blame] | 4701 | } |
| 4702 | } |
| Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4703 | } |
| Douglas Gregor | 27821ce | 2009-07-07 16:35:42 +0000 | [diff] [blame] | 4704 | |
| Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4705 | |
| 4706 | /// ActOnTypedefNameDecl - Perform semantic checking for a declaration which |
| 4707 | /// declares a typedef-name, either using the 'typedef' type specifier or via |
| 4708 | /// a C++0x [dcl.typedef]p2 alias-declaration: 'using T = A;'. |
| 4709 | NamedDecl* |
| 4710 | Sema::ActOnTypedefNameDecl(Scope *S, DeclContext *DC, TypedefNameDecl *NewTD, |
| 4711 | LookupResult &Previous, bool &Redeclaration) { |
| Eli Friedman | 88f4ed9 | 2010-08-10 03:13:15 +0000 | [diff] [blame] | 4712 | // Merge the decl with the existing one if appropriate. If the decl is |
| 4713 | // in an outer scope, it isn't the same thing. |
| Richard Smith | 72bcaec | 2013-12-05 04:30:04 +0000 | [diff] [blame] | 4714 | FilterLookupForScope(Previous, DC, S, /*ConsiderLinkage*/false, |
| 4715 | /*AllowInlineNamespace*/false); |
| Douglas Gregor | 3552dab | 2013-01-09 00:47:56 +0000 | [diff] [blame] | 4716 | filterNonConflictingPreviousDecls(Context, NewTD, Previous); |
| Eli Friedman | 88f4ed9 | 2010-08-10 03:13:15 +0000 | [diff] [blame] | 4717 | if (!Previous.empty()) { |
| 4718 | Redeclaration = true; |
| Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4719 | MergeTypedefNameDecl(NewTD, Previous); |
| Eli Friedman | 88f4ed9 | 2010-08-10 03:13:15 +0000 | [diff] [blame] | 4720 | } |
| 4721 | |
| Douglas Gregor | 27821ce | 2009-07-07 16:35:42 +0000 | [diff] [blame] | 4722 | // If this is the C FILE type, notify the AST context. |
| 4723 | if (IdentifierInfo *II = NewTD->getIdentifier()) |
| 4724 | if (!NewTD->isInvalidDecl() && |
| Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 4725 | NewTD->getDeclContext()->getRedeclContext()->isTranslationUnit()) { |
| Mike Stump | a4de80b | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 4726 | if (II->isStr("FILE")) |
| 4727 | Context.setFILEDecl(NewTD); |
| 4728 | else if (II->isStr("jmp_buf")) |
| 4729 | Context.setjmp_bufDecl(NewTD); |
| 4730 | else if (II->isStr("sigjmp_buf")) |
| 4731 | Context.setsigjmp_bufDecl(NewTD); |
| Rafael Espindola | 6cfa82b | 2011-11-13 21:51:09 +0000 | [diff] [blame] | 4732 | else if (II->isStr("ucontext_t")) |
| 4733 | Context.setucontext_tDecl(NewTD); |
| Mike Stump | a4de80b | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 4734 | } |
| 4735 | |
| Zhongxing Xu | ac8ef9e | 2009-01-16 03:34:13 +0000 | [diff] [blame] | 4736 | return NewTD; |
| 4737 | } |
| 4738 | |
| Douglas Gregor | 5d68a20 | 2009-02-24 19:23:27 +0000 | [diff] [blame] | 4739 | /// \brief Determines whether the given declaration is an out-of-scope |
| 4740 | /// previous declaration. |
| 4741 | /// |
| 4742 | /// This routine should be invoked when name lookup has found a |
| 4743 | /// previous declaration (PrevDecl) that is not in the scope where a |
| 4744 | /// new declaration by the same name is being introduced. If the new |
| 4745 | /// declaration occurs in a local scope, previous declarations with |
| 4746 | /// linkage may still be considered previous declarations (C99 |
| 4747 | /// 6.2.2p4-5, C++ [basic.link]p6). |
| 4748 | /// |
| 4749 | /// \param PrevDecl the previous declaration found by name |
| 4750 | /// lookup |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4751 | /// |
| Douglas Gregor | 5d68a20 | 2009-02-24 19:23:27 +0000 | [diff] [blame] | 4752 | /// \param DC the context in which the new declaration is being |
| 4753 | /// declared. |
| 4754 | /// |
| 4755 | /// \returns true if PrevDecl is an out-of-scope previous declaration |
| 4756 | /// for a new delcaration with the same name. |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4757 | static bool |
| Douglas Gregor | 5d68a20 | 2009-02-24 19:23:27 +0000 | [diff] [blame] | 4758 | isOutOfScopePreviousDeclaration(NamedDecl *PrevDecl, DeclContext *DC, |
| 4759 | ASTContext &Context) { |
| 4760 | if (!PrevDecl) |
| Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 4761 | return false; |
| Douglas Gregor | 5d68a20 | 2009-02-24 19:23:27 +0000 | [diff] [blame] | 4762 | |
| Douglas Gregor | eddf433 | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 4763 | if (!PrevDecl->hasLinkage()) |
| 4764 | return false; |
| Douglas Gregor | 5d68a20 | 2009-02-24 19:23:27 +0000 | [diff] [blame] | 4765 | |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4766 | if (Context.getLangOpts().CPlusPlus) { |
| Douglas Gregor | 5d68a20 | 2009-02-24 19:23:27 +0000 | [diff] [blame] | 4767 | // C++ [basic.link]p6: |
| 4768 | // If there is a visible declaration of an entity with linkage |
| 4769 | // having the same name and type, ignoring entities declared |
| 4770 | // outside the innermost enclosing namespace scope, the block |
| 4771 | // scope declaration declares that same entity and receives the |
| 4772 | // linkage of the previous declaration. |
| Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 4773 | DeclContext *OuterContext = DC->getRedeclContext(); |
| Douglas Gregor | 5d68a20 | 2009-02-24 19:23:27 +0000 | [diff] [blame] | 4774 | if (!OuterContext->isFunctionOrMethod()) |
| 4775 | // This rule only applies to block-scope declarations. |
| 4776 | return false; |
| Douglas Gregor | fcee946 | 2010-08-27 22:55:10 +0000 | [diff] [blame] | 4777 | |
| 4778 | DeclContext *PrevOuterContext = PrevDecl->getDeclContext(); |
| 4779 | if (PrevOuterContext->isRecord()) |
| 4780 | // We found a member function: ignore it. |
| 4781 | return false; |
| 4782 | |
| 4783 | // Find the innermost enclosing namespace for the new and |
| 4784 | // previous declarations. |
| Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 4785 | OuterContext = OuterContext->getEnclosingNamespaceContext(); |
| 4786 | PrevOuterContext = PrevOuterContext->getEnclosingNamespaceContext(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4787 | |
| Douglas Gregor | fcee946 | 2010-08-27 22:55:10 +0000 | [diff] [blame] | 4788 | // The previous declaration is in a different namespace, so it |
| 4789 | // isn't the same function. |
| 4790 | if (!OuterContext->Equals(PrevOuterContext)) |
| 4791 | return false; |
| Douglas Gregor | 5d68a20 | 2009-02-24 19:23:27 +0000 | [diff] [blame] | 4792 | } |
| 4793 | |
| Douglas Gregor | 5d68a20 | 2009-02-24 19:23:27 +0000 | [diff] [blame] | 4794 | return true; |
| 4795 | } |
| 4796 | |
| John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4797 | static void SetNestedNameSpecifier(DeclaratorDecl *DD, Declarator &D) { |
| 4798 | CXXScopeSpec &SS = D.getCXXScopeSpec(); |
| 4799 | if (!SS.isSet()) return; |
| Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 4800 | DD->setQualifierInfo(SS.getWithLocInContext(DD->getASTContext())); |
| John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4801 | } |
| 4802 | |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4803 | bool Sema::inferObjCARCLifetime(ValueDecl *decl) { |
| 4804 | QualType type = decl->getType(); |
| 4805 | Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime(); |
| 4806 | if (lifetime == Qualifiers::OCL_Autoreleasing) { |
| 4807 | // Various kinds of declaration aren't allowed to be __autoreleasing. |
| 4808 | unsigned kind = -1U; |
| 4809 | if (VarDecl *var = dyn_cast<VarDecl>(decl)) { |
| 4810 | if (var->hasAttr<BlocksAttr>()) |
| 4811 | kind = 0; // __block |
| 4812 | else if (!var->hasLocalStorage()) |
| 4813 | kind = 1; // global |
| 4814 | } else if (isa<ObjCIvarDecl>(decl)) { |
| 4815 | kind = 3; // ivar |
| 4816 | } else if (isa<FieldDecl>(decl)) { |
| 4817 | kind = 2; // field |
| 4818 | } |
| 4819 | |
| 4820 | if (kind != -1U) { |
| 4821 | Diag(decl->getLocation(), diag::err_arc_autoreleasing_var) |
| 4822 | << kind; |
| 4823 | } |
| 4824 | } else if (lifetime == Qualifiers::OCL_None) { |
| 4825 | // Try to infer lifetime. |
| 4826 | if (!type->isObjCLifetimeType()) |
| 4827 | return false; |
| 4828 | |
| 4829 | lifetime = type->getObjCARCImplicitLifetime(); |
| 4830 | type = Context.getLifetimeQualifiedType(type, lifetime); |
| 4831 | decl->setType(type); |
| 4832 | } |
| 4833 | |
| 4834 | if (VarDecl *var = dyn_cast<VarDecl>(decl)) { |
| 4835 | // Thread-local variables cannot have lifetime. |
| 4836 | if (lifetime && lifetime != Qualifiers::OCL_ExplicitNone && |
| Richard Smith | fd3834f | 2013-04-13 02:43:54 +0000 | [diff] [blame] | 4837 | var->getTLSKind()) { |
| Argyrios Kyrtzidis | cff00d9 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 4838 | Diag(var->getLocation(), diag::err_arc_thread_ownership) |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4839 | << var->getType(); |
| 4840 | return true; |
| 4841 | } |
| 4842 | } |
| 4843 | |
| 4844 | return false; |
| 4845 | } |
| 4846 | |
| Rafael Espindola | f1d2f0e | 2013-01-16 23:11:15 +0000 | [diff] [blame] | 4847 | static void checkAttributesAfterMerging(Sema &S, NamedDecl &ND) { |
| Nico Rieck | f8e8b5f | 2014-01-21 23:54:36 +0000 | [diff] [blame] | 4848 | // Ensure that an auto decl is deduced otherwise the checks below might cache |
| 4849 | // the wrong linkage. |
| 4850 | assert(S.ParsingInitForAutoVars.count(&ND) == 0); |
| 4851 | |
| Rafael Espindola | f1d2f0e | 2013-01-16 23:11:15 +0000 | [diff] [blame] | 4852 | // 'weak' only applies to declarations with external linkage. |
| Rafael Espindola | b306900 | 2013-01-16 23:49:06 +0000 | [diff] [blame] | 4853 | if (WeakAttr *Attr = ND.getAttr<WeakAttr>()) { |
| Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 4854 | if (!ND.isExternallyVisible()) { |
| Rafael Espindola | b306900 | 2013-01-16 23:49:06 +0000 | [diff] [blame] | 4855 | S.Diag(Attr->getLocation(), diag::err_attribute_weak_static); |
| 4856 | ND.dropAttr<WeakAttr>(); |
| 4857 | } |
| 4858 | } |
| 4859 | if (WeakRefAttr *Attr = ND.getAttr<WeakRefAttr>()) { |
| Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 4860 | if (ND.isExternallyVisible()) { |
| Rafael Espindola | b306900 | 2013-01-16 23:49:06 +0000 | [diff] [blame] | 4861 | S.Diag(Attr->getLocation(), diag::err_attribute_weakref_not_static); |
| 4862 | ND.dropAttr<WeakRefAttr>(); |
| 4863 | } |
| Rafael Espindola | f1d2f0e | 2013-01-16 23:11:15 +0000 | [diff] [blame] | 4864 | } |
| Reid Kleckner | b144d36 | 2013-05-20 14:02:37 +0000 | [diff] [blame] | 4865 | |
| 4866 | // 'selectany' only applies to externally visible varable declarations. |
| 4867 | // It does not apply to functions. |
| 4868 | if (SelectAnyAttr *Attr = ND.getAttr<SelectAnyAttr>()) { |
| 4869 | if (isa<FunctionDecl>(ND) || !ND.isExternallyVisible()) { |
| 4870 | S.Diag(Attr->getLocation(), diag::err_attribute_selectany_non_extern_data); |
| 4871 | ND.dropAttr<SelectAnyAttr>(); |
| 4872 | } |
| 4873 | } |
| Rafael Espindola | f1d2f0e | 2013-01-16 23:11:15 +0000 | [diff] [blame] | 4874 | } |
| 4875 | |
| John McCall | c87d972 | 2013-04-02 02:48:58 +0000 | [diff] [blame] | 4876 | /// Given that we are within the definition of the given function, |
| 4877 | /// will that definition behave like C99's 'inline', where the |
| 4878 | /// definition is discarded except for optimization purposes? |
| 4879 | static bool isFunctionDefinitionDiscarded(Sema &S, FunctionDecl *FD) { |
| 4880 | // Try to avoid calling GetGVALinkageForFunction. |
| 4881 | |
| 4882 | // All cases of this require the 'inline' keyword. |
| 4883 | if (!FD->isInlined()) return false; |
| 4884 | |
| 4885 | // This is only possible in C++ with the gnu_inline attribute. |
| 4886 | if (S.getLangOpts().CPlusPlus && !FD->hasAttr<GNUInlineAttr>()) |
| 4887 | return false; |
| 4888 | |
| 4889 | // Okay, go ahead and call the relatively-more-expensive function. |
| 4890 | |
| 4891 | #ifndef NDEBUG |
| 4892 | // AST quite reasonably asserts that it's working on a function |
| 4893 | // definition. We don't really have a way to tell it that we're |
| 4894 | // currently defining the function, so just lie to it in +Asserts |
| 4895 | // builds. This is an awful hack. |
| 4896 | FD->setLazyBody(1); |
| 4897 | #endif |
| 4898 | |
| 4899 | bool isC99Inline = (S.Context.GetGVALinkageForFunction(FD) == GVA_C99Inline); |
| 4900 | |
| 4901 | #ifndef NDEBUG |
| 4902 | FD->setLazyBody(0); |
| 4903 | #endif |
| 4904 | |
| 4905 | return isC99Inline; |
| 4906 | } |
| 4907 | |
| Richard Smith | ac974a3 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 4908 | /// Determine whether a variable is extern "C" prior to attaching |
| 4909 | /// an initializer. We can't just call isExternC() here, because that |
| 4910 | /// will also compute and cache whether the declaration is externally |
| 4911 | /// visible, which might change when we attach the initializer. |
| 4912 | /// |
| 4913 | /// This can only be used if the declaration is known to not be a |
| 4914 | /// redeclaration of an internal linkage declaration. |
| 4915 | /// |
| 4916 | /// For instance: |
| 4917 | /// |
| 4918 | /// auto x = []{}; |
| 4919 | /// |
| 4920 | /// Attaching the initializer here makes this declaration not externally |
| 4921 | /// visible, because its type has internal linkage. |
| 4922 | /// |
| 4923 | /// FIXME: This is a hack. |
| 4924 | template<typename T> |
| 4925 | static bool isIncompleteDeclExternC(Sema &S, const T *D) { |
| 4926 | if (S.getLangOpts().CPlusPlus) { |
| 4927 | // In C++, the overloadable attribute negates the effects of extern "C". |
| 4928 | if (!D->isInExternCContext() || D->template hasAttr<OverloadableAttr>()) |
| 4929 | return false; |
| 4930 | } |
| 4931 | return D->isExternC(); |
| 4932 | } |
| 4933 | |
| Rafael Espindola | 0e0d009 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 4934 | static bool shouldConsiderLinkage(const VarDecl *VD) { |
| 4935 | const DeclContext *DC = VD->getDeclContext()->getRedeclContext(); |
| 4936 | if (DC->isFunctionOrMethod()) |
| Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 4937 | return VD->hasExternalStorage(); |
| Rafael Espindola | 0e0d009 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 4938 | if (DC->isFileContext()) |
| 4939 | return true; |
| 4940 | if (DC->isRecord()) |
| 4941 | return false; |
| 4942 | llvm_unreachable("Unexpected context"); |
| 4943 | } |
| 4944 | |
| 4945 | static bool shouldConsiderLinkage(const FunctionDecl *FD) { |
| 4946 | const DeclContext *DC = FD->getDeclContext()->getRedeclContext(); |
| 4947 | if (DC->isFileContext() || DC->isFunctionOrMethod()) |
| 4948 | return true; |
| 4949 | if (DC->isRecord()) |
| 4950 | return false; |
| 4951 | llvm_unreachable("Unexpected context"); |
| 4952 | } |
| 4953 | |
| Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 4954 | /// Adjust the \c DeclContext for a function or variable that might be a |
| 4955 | /// function-local external declaration. |
| 4956 | bool Sema::adjustContextForLocalExternDecl(DeclContext *&DC) { |
| 4957 | if (!DC->isFunctionOrMethod()) |
| 4958 | return false; |
| 4959 | |
| 4960 | // If this is a local extern function or variable declared within a function |
| 4961 | // template, don't add it into the enclosing namespace scope until it is |
| 4962 | // instantiated; it might have a dependent type right now. |
| 4963 | if (DC->isDependentContext()) |
| 4964 | return true; |
| 4965 | |
| 4966 | // C++11 [basic.link]p7: |
| 4967 | // When a block scope declaration of an entity with linkage is not found to |
| 4968 | // refer to some other declaration, then that entity is a member of the |
| 4969 | // innermost enclosing namespace. |
| 4970 | // |
| 4971 | // Per C++11 [namespace.def]p6, the innermost enclosing namespace is a |
| 4972 | // semantically-enclosing namespace, not a lexically-enclosing one. |
| 4973 | while (!DC->isFileContext() && !isa<LinkageSpecDecl>(DC)) |
| 4974 | DC = DC->getParent(); |
| 4975 | return true; |
| 4976 | } |
| 4977 | |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4978 | NamedDecl * |
| Chris Lattner | 88fdea8 | 2010-10-10 18:16:20 +0000 | [diff] [blame] | 4979 | Sema::ActOnVariableDeclarator(Scope *S, Declarator &D, DeclContext *DC, |
| Kaelyn Uhrain | 4dc695d | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 4980 | TypeSourceInfo *TInfo, LookupResult &Previous, |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4981 | MultiTemplateParamsArg TemplateParamLists, |
| 4982 | bool &AddToScope) { |
| Kaelyn Uhrain | 4dc695d | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 4983 | QualType R = TInfo->getType(); |
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4984 | DeclarationName Name = GetNameForDeclarator(D).getName(); |
| Zhongxing Xu | 9b7714d | 2009-01-16 02:36:34 +0000 | [diff] [blame] | 4985 | |
| Douglas Gregor | c4df407 | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 4986 | DeclSpec::SCS SCSpec = D.getDeclSpec().getStorageClassSpec(); |
| Rafael Espindola | bff5956 | 2013-04-25 12:11:36 +0000 | [diff] [blame] | 4987 | VarDecl::StorageClass SC = |
| 4988 | StorageClassSpecToVarDeclStorageClass(D.getDeclSpec()); |
| Joey Gouly | dd7f456 | 2013-01-23 11:56:20 +0000 | [diff] [blame] | 4989 | |
| Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 4990 | DeclContext *OriginalDC = DC; |
| 4991 | bool IsLocalExternDecl = SC == SC_Extern && |
| 4992 | adjustContextForLocalExternDecl(DC); |
| 4993 | |
| Pekka Jaaskelainen | 8690a68 | 2014-02-20 13:52:08 +0000 | [diff] [blame] | 4994 | if (getLangOpts().OpenCL) { |
| 4995 | // OpenCL v1.0 s6.8.a.3: Pointers to functions are not allowed. |
| 4996 | QualType NR = R; |
| 4997 | while (NR->isPointerType()) { |
| 4998 | if (NR->isFunctionPointerType()) { |
| 4999 | Diag(D.getIdentifierLoc(), diag::err_opencl_function_pointer_variable); |
| 5000 | D.setInvalidType(); |
| 5001 | break; |
| 5002 | } |
| 5003 | NR = NR->getPointeeType(); |
| 5004 | } |
| 5005 | |
| 5006 | if (!getOpenCLOptions().cl_khr_fp16) { |
| 5007 | // OpenCL v1.2 s6.1.1.1: reject declaring variables of the half and |
| 5008 | // half array type (unless the cl_khr_fp16 extension is enabled). |
| 5009 | if (Context.getBaseElementType(R)->isHalfType()) { |
| 5010 | Diag(D.getIdentifierLoc(), diag::err_opencl_half_declaration) << R; |
| 5011 | D.setInvalidType(); |
| 5012 | } |
| Joey Gouly | dd7f456 | 2013-01-23 11:56:20 +0000 | [diff] [blame] | 5013 | } |
| 5014 | } |
| 5015 | |
| Douglas Gregor | c4df407 | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 5016 | if (SCSpec == DeclSpec::SCS_mutable) { |
| Zhongxing Xu | 9b7714d | 2009-01-16 02:36:34 +0000 | [diff] [blame] | 5017 | // mutable can only appear on non-static class members, so it's always |
| 5018 | // an error here |
| 5019 | Diag(D.getIdentifierLoc(), diag::err_mutable_nonmember); |
| Chris Lattner | f6d1c9c | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 5020 | D.setInvalidType(); |
| John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 5021 | SC = SC_None; |
| Zhongxing Xu | 9b7714d | 2009-01-16 02:36:34 +0000 | [diff] [blame] | 5022 | } |
| John McCall | c87d972 | 2013-04-02 02:48:58 +0000 | [diff] [blame] | 5023 | |
| Richard Smith | f2c9afc | 2013-06-17 01:34:01 +0000 | [diff] [blame] | 5024 | if (getLangOpts().CPlusPlus11 && SCSpec == DeclSpec::SCS_register && |
| 5025 | !D.getAsmLabel() && !getSourceManager().isInSystemMacro( |
| 5026 | D.getDeclSpec().getStorageClassSpecLoc())) { |
| 5027 | // In C++11, the 'register' storage class specifier is deprecated. |
| 5028 | // Suppress the warning in system macros, it's used in macros in some |
| 5029 | // popular C system headers, such as in glibc's htonl() macro. |
| 5030 | Diag(D.getDeclSpec().getStorageClassSpecLoc(), |
| 5031 | diag::warn_deprecated_register) |
| 5032 | << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc()); |
| 5033 | } |
| 5034 | |
| Zhongxing Xu | 9b7714d | 2009-01-16 02:36:34 +0000 | [diff] [blame] | 5035 | IdentifierInfo *II = Name.getAsIdentifierInfo(); |
| 5036 | if (!II) { |
| 5037 | Diag(D.getIdentifierLoc(), diag::err_bad_variable_name) |
| Douglas Gregor | bb64afc | 2011-10-09 18:55:59 +0000 | [diff] [blame] | 5038 | << Name; |
| Zhongxing Xu | 9b7714d | 2009-01-16 02:36:34 +0000 | [diff] [blame] | 5039 | return 0; |
| 5040 | } |
| 5041 | |
| Richard Smith | b1402ae | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 5042 | DiagnoseFunctionSpecifiers(D.getDeclSpec()); |
| Douglas Gregor | 0c88030 | 2009-03-11 23:00:04 +0000 | [diff] [blame] | 5043 | |
| Douglas Gregor | 212cab3 | 2009-03-11 20:22:50 +0000 | [diff] [blame] | 5044 | if (!DC->isRecord() && S->getFnParent() == 0) { |
| 5045 | // C99 6.9p2: The storage-class specifiers auto and register shall not |
| 5046 | // appear in the declaration specifiers in an external declaration. |
| John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 5047 | if (SC == SC_Auto || SC == SC_Register) { |
| Chris Lattner | d98e7cf7 | 2009-05-12 21:44:00 +0000 | [diff] [blame] | 5048 | // If this is a register variable with an asm label specified, then this |
| 5049 | // is a GNU extension. |
| John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 5050 | if (SC == SC_Register && D.getAsmLabel()) |
| Chris Lattner | d98e7cf7 | 2009-05-12 21:44:00 +0000 | [diff] [blame] | 5051 | Diag(D.getIdentifierLoc(), diag::err_unsupported_global_register); |
| 5052 | else |
| 5053 | Diag(D.getIdentifierLoc(), diag::err_typecheck_sclass_fscope); |
| Chris Lattner | f6d1c9c | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 5054 | D.setInvalidType(); |
| Zhongxing Xu | 9b7714d | 2009-01-16 02:36:34 +0000 | [diff] [blame] | 5055 | } |
| Zhongxing Xu | 9b7714d | 2009-01-16 02:36:34 +0000 | [diff] [blame] | 5056 | } |
| Richard Smith | f2c9afc | 2013-06-17 01:34:01 +0000 | [diff] [blame] | 5057 | |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5058 | if (getLangOpts().OpenCL) { |
| Peter Collingbourne | 2dbb708 | 2011-09-19 21:14:35 +0000 | [diff] [blame] | 5059 | // Set up the special work-group-local storage class for variables in the |
| 5060 | // OpenCL __local address space. |
| Rafael Espindola | 2be6b72 | 2012-12-21 01:21:33 +0000 | [diff] [blame] | 5061 | if (R.getAddressSpace() == LangAS::opencl_local) { |
| Peter Collingbourne | 2dbb708 | 2011-09-19 21:14:35 +0000 | [diff] [blame] | 5062 | SC = SC_OpenCLWorkGroupLocal; |
| Rafael Espindola | 2be6b72 | 2012-12-21 01:21:33 +0000 | [diff] [blame] | 5063 | } |
| Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 5064 | |
| Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 5065 | // OpenCL v1.2 s6.9.b p4: |
| 5066 | // The sampler type cannot be used with the __local and __global address |
| 5067 | // space qualifiers. |
| 5068 | if (R->isSamplerT() && (R.getAddressSpace() == LangAS::opencl_local || |
| 5069 | R.getAddressSpace() == LangAS::opencl_global)) { |
| 5070 | Diag(D.getIdentifierLoc(), diag::err_wrong_sampler_addressspace); |
| 5071 | } |
| 5072 | |
| Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 5073 | // OpenCL 1.2 spec, p6.9 r: |
| 5074 | // The event type cannot be used to declare a program scope variable. |
| 5075 | // The event type cannot be used with the __local, __constant and __global |
| 5076 | // address space qualifiers. |
| 5077 | if (R->isEventT()) { |
| 5078 | if (S->getParent() == 0) { |
| 5079 | Diag(D.getLocStart(), diag::err_event_t_global_var); |
| 5080 | D.setInvalidType(); |
| 5081 | } |
| 5082 | |
| 5083 | if (R.getAddressSpace()) { |
| 5084 | Diag(D.getLocStart(), diag::err_event_t_addr_space_qual); |
| 5085 | D.setInvalidType(); |
| 5086 | } |
| 5087 | } |
| Peter Collingbourne | 2dbb708 | 2011-09-19 21:14:35 +0000 | [diff] [blame] | 5088 | } |
| 5089 | |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5090 | bool IsExplicitSpecialization = false; |
| 5091 | bool IsVariableTemplateSpecialization = false; |
| 5092 | bool IsPartialSpecialization = false; |
| Larisse Voufo | d8dd97c | 2013-08-14 03:09:19 +0000 | [diff] [blame] | 5093 | bool IsVariableTemplate = false; |
| Larisse Voufo | 72caf2b | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 5094 | VarDecl *NewVD = 0; |
| 5095 | VarTemplateDecl *NewTemplate = 0; |
| Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 5096 | TemplateParameterList *TemplateParams = 0; |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5097 | if (!getLangOpts().CPlusPlus) { |
| Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5098 | NewVD = VarDecl::Create(Context, DC, D.getLocStart(), |
| Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 5099 | D.getIdentifierLoc(), II, |
| Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 5100 | R, TInfo, SC); |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 5101 | |
| 5102 | if (D.isInvalidType()) |
| 5103 | NewVD->setInvalidDecl(); |
| 5104 | } else { |
| Larisse Voufo | 72caf2b | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 5105 | bool Invalid = false; |
| 5106 | |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 5107 | if (DC->isRecord() && !CurContext->isRecord()) { |
| 5108 | // This is an out-of-line definition of a static data member. |
| Rafael Espindola | 45f96f8 | 2013-06-19 13:41:54 +0000 | [diff] [blame] | 5109 | switch (SC) { |
| 5110 | case SC_None: |
| 5111 | break; |
| 5112 | case SC_Static: |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 5113 | Diag(D.getDeclSpec().getStorageClassSpecLoc(), |
| 5114 | diag::err_static_out_of_line) |
| 5115 | << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc()); |
| Rafael Espindola | 45f96f8 | 2013-06-19 13:41:54 +0000 | [diff] [blame] | 5116 | break; |
| 5117 | case SC_Auto: |
| 5118 | case SC_Register: |
| 5119 | case SC_Extern: |
| 5120 | // [dcl.stc] p2: The auto or register specifiers shall be applied only |
| 5121 | // to names of variables declared in a block or to function parameters. |
| 5122 | // [dcl.stc] p6: The extern specifier cannot be used in the declaration |
| 5123 | // of class members |
| 5124 | |
| 5125 | Diag(D.getDeclSpec().getStorageClassSpecLoc(), |
| 5126 | diag::err_storage_class_for_static_member) |
| 5127 | << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc()); |
| 5128 | break; |
| 5129 | case SC_PrivateExtern: |
| 5130 | llvm_unreachable("C storage class in c++!"); |
| 5131 | case SC_OpenCLWorkGroupLocal: |
| 5132 | llvm_unreachable("OpenCL storage class in c++!"); |
| Rafael Espindola | 8ac2f59 | 2013-04-04 21:21:25 +0000 | [diff] [blame] | 5133 | } |
| Larisse Voufo | 21de36b | 2013-08-06 03:43:07 +0000 | [diff] [blame] | 5134 | } |
| 5135 | |
| Richard Smith | 4297375 | 2012-02-16 20:41:22 +0000 | [diff] [blame] | 5136 | if (SC == SC_Static && CurContext->isRecord()) { |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 5137 | if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(DC)) { |
| 5138 | if (RD->isLocalClass()) |
| 5139 | Diag(D.getIdentifierLoc(), |
| 5140 | diag::err_static_data_member_not_allowed_in_local_class) |
| 5141 | << Name << RD->getDeclName(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5142 | |
| Richard Smith | 4297375 | 2012-02-16 20:41:22 +0000 | [diff] [blame] | 5143 | // C++98 [class.union]p1: If a union contains a static data member, |
| 5144 | // the program is ill-formed. C++11 drops this restriction. |
| 5145 | if (RD->isUnion()) |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 5146 | Diag(D.getIdentifierLoc(), |
| Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5147 | getLangOpts().CPlusPlus11 |
| Richard Smith | 4297375 | 2012-02-16 20:41:22 +0000 | [diff] [blame] | 5148 | ? diag::warn_cxx98_compat_static_data_member_in_union |
| 5149 | : diag::ext_static_data_member_in_union) << Name; |
| 5150 | // We conservatively disallow static data members in anonymous structs. |
| 5151 | else if (!RD->getDeclName()) |
| 5152 | Diag(D.getIdentifierLoc(), |
| 5153 | diag::err_static_data_member_not_allowed_in_anon_struct) |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 5154 | << Name << RD->isUnion(); |
| 5155 | } |
| 5156 | } |
| 5157 | |
| 5158 | // Match up the template parameter lists with the scope specifier, then |
| 5159 | // determine whether we have a template or a template specialization. |
| Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 5160 | TemplateParams = MatchTemplateParametersToScopeSpecifier( |
| 5161 | D.getDeclSpec().getLocStart(), D.getIdentifierLoc(), |
| 5162 | D.getCXXScopeSpec(), TemplateParamLists, |
| 5163 | /*never a friend*/ false, IsExplicitSpecialization, Invalid); |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5164 | |
| Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 5165 | if (D.getName().getKind() == UnqualifiedId::IK_TemplateId && |
| 5166 | !TemplateParams) { |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5167 | TemplateIdAnnotation *TemplateId = D.getName().TemplateId; |
| 5168 | |
| 5169 | // We have encountered something that the user meant to be a |
| 5170 | // specialization (because it has explicitly-specified template |
| 5171 | // arguments) but that was not introduced with a "template<>" (or had |
| 5172 | // too few of them). |
| 5173 | // FIXME: Differentiate between attempts for explicit instantiations |
| 5174 | // (starting with "template") and the rest. |
| 5175 | Diag(D.getIdentifierLoc(), diag::err_template_spec_needs_header) |
| 5176 | << SourceRange(TemplateId->LAngleLoc, TemplateId->RAngleLoc) |
| 5177 | << FixItHint::CreateInsertion(D.getDeclSpec().getLocStart(), |
| 5178 | "template<> "); |
| Richard Smith | 72db563 | 2014-01-25 21:32:06 +0000 | [diff] [blame] | 5179 | IsExplicitSpecialization = true; |
| Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 5180 | TemplateParams = TemplateParameterList::Create(Context, SourceLocation(), |
| 5181 | SourceLocation(), 0, 0, |
| 5182 | SourceLocation()); |
| 5183 | } |
| 5184 | |
| 5185 | if (TemplateParams) { |
| 5186 | if (!TemplateParams->size() && |
| 5187 | D.getName().getKind() != UnqualifiedId::IK_TemplateId) { |
| 5188 | // There is an extraneous 'template<>' for this variable. Complain |
| 5189 | // about it, but allow the declaration of the variable. |
| 5190 | Diag(TemplateParams->getTemplateLoc(), |
| 5191 | diag::err_template_variable_noparams) |
| 5192 | << II |
| 5193 | << SourceRange(TemplateParams->getTemplateLoc(), |
| 5194 | TemplateParams->getRAngleLoc()); |
| 5195 | TemplateParams = 0; |
| 5196 | } else { |
| 5197 | // Only C++1y supports variable templates (N3651). |
| 5198 | Diag(D.getIdentifierLoc(), |
| 5199 | getLangOpts().CPlusPlus1y |
| 5200 | ? diag::warn_cxx11_compat_variable_template |
| 5201 | : diag::ext_variable_template); |
| 5202 | |
| 5203 | if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) { |
| 5204 | // This is an explicit specialization or a partial specialization. |
| 5205 | // FIXME: Check that we can declare a specialization here. |
| 5206 | IsVariableTemplateSpecialization = true; |
| 5207 | IsPartialSpecialization = TemplateParams->size() > 0; |
| 5208 | } else { // if (TemplateParams->size() > 0) |
| 5209 | // This is a template declaration. |
| 5210 | IsVariableTemplate = true; |
| 5211 | |
| 5212 | // Check that we can declare a template here. |
| 5213 | if (CheckTemplateDeclScope(S, TemplateParams)) |
| 5214 | return 0; |
| 5215 | } |
| 5216 | } |
| Douglas Gregor | b09f3d8 | 2009-07-22 17:18:37 +0000 | [diff] [blame] | 5217 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5218 | |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5219 | if (IsVariableTemplateSpecialization) { |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5220 | SourceLocation TemplateKWLoc = |
| 5221 | TemplateParamLists.size() > 0 |
| 5222 | ? TemplateParamLists[0]->getTemplateLoc() |
| 5223 | : SourceLocation(); |
| 5224 | DeclResult Res = ActOnVarTemplateSpecialization( |
| Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 5225 | S, D, TInfo, TemplateKWLoc, TemplateParams, SC, |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5226 | IsPartialSpecialization); |
| 5227 | if (Res.isInvalid()) |
| 5228 | return 0; |
| 5229 | NewVD = cast<VarDecl>(Res.get()); |
| 5230 | AddToScope = false; |
| 5231 | } else |
| 5232 | NewVD = VarDecl::Create(Context, DC, D.getLocStart(), |
| 5233 | D.getIdentifierLoc(), II, R, TInfo, SC); |
| Eli Friedman | d5c0eed | 2009-04-19 20:27:55 +0000 | [diff] [blame] | 5234 | |
| Larisse Voufo | 72caf2b | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 5235 | // If this is supposed to be a variable template, create it as such. |
| 5236 | if (IsVariableTemplate) { |
| 5237 | NewTemplate = |
| 5238 | VarTemplateDecl::Create(Context, DC, D.getIdentifierLoc(), Name, |
| Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 5239 | TemplateParams, NewVD); |
| Larisse Voufo | 72caf2b | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 5240 | NewVD->setDescribedVarTemplate(NewTemplate); |
| 5241 | } |
| 5242 | |
| Richard Smith | b2bc2e6 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 5243 | // If this decl has an auto type in need of deduction, make a note of the |
| 5244 | // Decl so we can diagnose uses of it in its own initializer. |
| Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 5245 | if (D.getDeclSpec().containsPlaceholderType() && R->getContainedAutoType()) |
| Richard Smith | b2bc2e6 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 5246 | ParsingInitForAutoVars.insert(NewVD); |
| Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 5247 | |
| Larisse Voufo | 72caf2b | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 5248 | if (D.isInvalidType() || Invalid) { |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 5249 | NewVD->setInvalidDecl(); |
| Larisse Voufo | 72caf2b | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 5250 | if (NewTemplate) |
| 5251 | NewTemplate->setInvalidDecl(); |
| 5252 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5253 | |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 5254 | SetNestedNameSpecifier(NewVD, D); |
| John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 5255 | |
| Richard Smith | 72db563 | 2014-01-25 21:32:06 +0000 | [diff] [blame] | 5256 | // If we have any template parameter lists that don't directly belong to |
| 5257 | // the variable (matching the scope specifier), store them. |
| 5258 | unsigned VDTemplateParamLists = TemplateParams ? 1 : 0; |
| 5259 | if (TemplateParamLists.size() > VDTemplateParamLists) |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5260 | NewVD->setTemplateParameterListsInfo( |
| Richard Smith | 72db563 | 2014-01-25 21:32:06 +0000 | [diff] [blame] | 5261 | Context, TemplateParamLists.size() - VDTemplateParamLists, |
| 5262 | TemplateParamLists.data()); |
| Richard Smith | a77a0a6 | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 5263 | |
| Richard Smith | 6331c40 | 2012-02-13 22:16:19 +0000 | [diff] [blame] | 5264 | if (D.getDeclSpec().isConstexprSpecified()) |
| Richard Smith | f0215fe | 2011-12-25 21:17:58 +0000 | [diff] [blame] | 5265 | NewVD->setConstexpr(true); |
| Abramo Bagnara | da41d0c | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 5266 | } |
| 5267 | |
| Douglas Gregor | 4186681 | 2011-09-12 18:37:38 +0000 | [diff] [blame] | 5268 | // Set the lexical context. If the declarator has a C++ scope specifier, the |
| 5269 | // lexical context will be different from the semantic context. |
| 5270 | NewVD->setLexicalDeclContext(CurContext); |
| Larisse Voufo | 72caf2b | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 5271 | if (NewTemplate) |
| 5272 | NewTemplate->setLexicalDeclContext(CurContext); |
| Douglas Gregor | 4186681 | 2011-09-12 18:37:38 +0000 | [diff] [blame] | 5273 | |
| Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 5274 | if (IsLocalExternDecl) |
| 5275 | NewVD->setLocalExternDecl(); |
| 5276 | |
| Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 5277 | if (DeclSpec::TSCS TSCS = D.getDeclSpec().getThreadStorageClassSpec()) { |
| Enea Zaffanella | 28f36ba | 2013-05-10 20:34:44 +0000 | [diff] [blame] | 5278 | if (NewVD->hasLocalStorage()) { |
| 5279 | // C++11 [dcl.stc]p4: |
| 5280 | // When thread_local is applied to a variable of block scope the |
| 5281 | // storage-class-specifier static is implied if it does not appear |
| 5282 | // explicitly. |
| 5283 | // Core issue: 'static' is not implied if the variable is declared |
| 5284 | // 'extern'. |
| 5285 | if (SCSpec == DeclSpec::SCS_unspecified && |
| 5286 | TSCS == DeclSpec::TSCS_thread_local && |
| 5287 | DC->isFunctionOrMethod()) |
| 5288 | NewVD->setTSCSpec(TSCS); |
| 5289 | else |
| 5290 | Diag(D.getDeclSpec().getThreadStorageClassSpecLoc(), |
| 5291 | diag::err_thread_non_global) |
| 5292 | << DeclSpec::getSpecifierName(TSCS); |
| 5293 | } else if (!Context.getTargetInfo().isTLSSupported()) |
| Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 5294 | Diag(D.getDeclSpec().getThreadStorageClassSpecLoc(), |
| 5295 | diag::err_thread_unsupported); |
| Eli Friedman | d5c0eed | 2009-04-19 20:27:55 +0000 | [diff] [blame] | 5296 | else |
| Enea Zaffanella | acb8ecd | 2013-05-04 08:27:07 +0000 | [diff] [blame] | 5297 | NewVD->setTSCSpec(TSCS); |
| Eli Friedman | d5c0eed | 2009-04-19 20:27:55 +0000 | [diff] [blame] | 5298 | } |
| Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 5299 | |
| John McCall | c87d972 | 2013-04-02 02:48:58 +0000 | [diff] [blame] | 5300 | // C99 6.7.4p3 |
| 5301 | // An inline definition of a function with external linkage shall |
| 5302 | // not contain a definition of a modifiable object with static or |
| 5303 | // thread storage duration... |
| 5304 | // We only apply this when the function is required to be defined |
| 5305 | // elsewhere, i.e. when the function is not 'extern inline'. Note |
| 5306 | // that a local variable with thread storage duration still has to |
| 5307 | // be marked 'static'. Also note that it's possible to get these |
| 5308 | // semantics in C++ using __attribute__((gnu_inline)). |
| 5309 | if (SC == SC_Static && S->getFnParent() != 0 && |
| 5310 | !NewVD->getType().isConstQualified()) { |
| 5311 | FunctionDecl *CurFD = getCurFunctionDecl(); |
| 5312 | if (CurFD && isFunctionDefinitionDiscarded(*this, CurFD)) { |
| 5313 | Diag(D.getDeclSpec().getStorageClassSpecLoc(), |
| 5314 | diag::warn_static_local_in_extern_inline); |
| 5315 | MaybeSuggestAddingStaticToDecl(CurFD); |
| 5316 | } |
| 5317 | } |
| 5318 | |
| Douglas Gregor | 3c7cd6a | 2011-09-09 20:53:38 +0000 | [diff] [blame] | 5319 | if (D.getDeclSpec().isModulePrivateSpecified()) { |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5320 | if (IsVariableTemplateSpecialization) |
| 5321 | Diag(NewVD->getLocation(), diag::err_module_private_specialization) |
| 5322 | << (IsPartialSpecialization ? 1 : 0) |
| 5323 | << FixItHint::CreateRemoval( |
| 5324 | D.getDeclSpec().getModulePrivateSpecLoc()); |
| 5325 | else if (IsExplicitSpecialization) |
| Douglas Gregor | 3c7cd6a | 2011-09-09 20:53:38 +0000 | [diff] [blame] | 5326 | Diag(NewVD->getLocation(), diag::err_module_private_specialization) |
| 5327 | << 2 |
| 5328 | << FixItHint::CreateRemoval(D.getDeclSpec().getModulePrivateSpecLoc()); |
| Douglas Gregor | 4186681 | 2011-09-12 18:37:38 +0000 | [diff] [blame] | 5329 | else if (NewVD->hasLocalStorage()) |
| 5330 | Diag(NewVD->getLocation(), diag::err_module_private_local) |
| 5331 | << 0 << NewVD->getDeclName() |
| 5332 | << SourceRange(D.getDeclSpec().getModulePrivateSpecLoc()) |
| 5333 | << FixItHint::CreateRemoval(D.getDeclSpec().getModulePrivateSpecLoc()); |
| Larisse Voufo | 72caf2b | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 5334 | else { |
| Douglas Gregor | 3c7cd6a | 2011-09-09 20:53:38 +0000 | [diff] [blame] | 5335 | NewVD->setModulePrivate(); |
| Larisse Voufo | 72caf2b | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 5336 | if (NewTemplate) |
| 5337 | NewTemplate->setModulePrivate(); |
| 5338 | } |
| Douglas Gregor | 3c7cd6a | 2011-09-09 20:53:38 +0000 | [diff] [blame] | 5339 | } |
| Douglas Gregor | 26701a4 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 5340 | |
| Zhongxing Xu | 9b7714d | 2009-01-16 02:36:34 +0000 | [diff] [blame] | 5341 | // Handle attributes prior to checking for duplicates in MergeVarDecl |
| Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 5342 | ProcessDeclAttributes(S, NewVD, D); |
| Zhongxing Xu | 9b7714d | 2009-01-16 02:36:34 +0000 | [diff] [blame] | 5343 | |
| Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 5344 | if (NewVD->hasAttrs()) |
| 5345 | CheckAlignasUnderalignment(NewVD); |
| 5346 | |
| Peter Collingbourne | c6b0857 | 2012-08-28 20:37:50 +0000 | [diff] [blame] | 5347 | if (getLangOpts().CUDA) { |
| 5348 | // CUDA B.2.5: "__shared__ and __constant__ variables have implied static |
| 5349 | // storage [duration]." |
| 5350 | if (SC == SC_None && S->getFnParent() != 0 && |
| Rafael Espindola | 2be6b72 | 2012-12-21 01:21:33 +0000 | [diff] [blame] | 5351 | (NewVD->hasAttr<CUDASharedAttr>() || |
| 5352 | NewVD->hasAttr<CUDAConstantAttr>())) { |
| Peter Collingbourne | c6b0857 | 2012-08-28 20:37:50 +0000 | [diff] [blame] | 5353 | NewVD->setStorageClass(SC_Static); |
| Rafael Espindola | 2be6b72 | 2012-12-21 01:21:33 +0000 | [diff] [blame] | 5354 | } |
| Peter Collingbourne | c6b0857 | 2012-08-28 20:37:50 +0000 | [diff] [blame] | 5355 | } |
| 5356 | |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5357 | // In auto-retain/release, infer strong retension for variables of |
| 5358 | // retainable type. |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5359 | if (getLangOpts().ObjCAutoRefCount && inferObjCARCLifetime(NewVD)) |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5360 | NewVD->setInvalidDecl(); |
| 5361 | |
| Zhongxing Xu | 9b7714d | 2009-01-16 02:36:34 +0000 | [diff] [blame] | 5362 | // Handle GNU asm-label extension (encoded as an attribute). |
| Chris Lattner | 88fdea8 | 2010-10-10 18:16:20 +0000 | [diff] [blame] | 5363 | if (Expr *E = (Expr*)D.getAsmLabel()) { |
| Zhongxing Xu | 9b7714d | 2009-01-16 02:36:34 +0000 | [diff] [blame] | 5364 | // The parser guarantees this is a string. |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5365 | StringLiteral *SE = cast<StringLiteral>(E); |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5366 | StringRef Label = SE->getString(); |
| Abramo Bagnara | 1339223 | 2011-01-11 15:16:52 +0000 | [diff] [blame] | 5367 | if (S->getFnParent() != 0) { |
| 5368 | switch (SC) { |
| 5369 | case SC_None: |
| 5370 | case SC_Auto: |
| 5371 | Diag(E->getExprLoc(), diag::warn_asm_label_on_auto_decl) << Label; |
| 5372 | break; |
| 5373 | case SC_Register: |
| Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 5374 | if (!Context.getTargetInfo().isValidGCCRegisterName(Label)) |
| Abramo Bagnara | 1339223 | 2011-01-11 15:16:52 +0000 | [diff] [blame] | 5375 | Diag(E->getExprLoc(), diag::err_asm_unknown_register_name) << Label; |
| 5376 | break; |
| 5377 | case SC_Static: |
| 5378 | case SC_Extern: |
| 5379 | case SC_PrivateExtern: |
| Peter Collingbourne | 2dbb708 | 2011-09-19 21:14:35 +0000 | [diff] [blame] | 5380 | case SC_OpenCLWorkGroupLocal: |
| Abramo Bagnara | 1339223 | 2011-01-11 15:16:52 +0000 | [diff] [blame] | 5381 | break; |
| 5382 | } |
| 5383 | } |
| 5384 | |
| 5385 | NewVD->addAttr(::new (Context) AsmLabelAttr(SE->getStrTokenLoc(0), |
| Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 5386 | Context, Label, 0)); |
| David Chisnall | 0867d9c | 2012-02-18 16:12:34 +0000 | [diff] [blame] | 5387 | } else if (!ExtnameUndeclaredIdentifiers.empty()) { |
| 5388 | llvm::DenseMap<IdentifierInfo*,AsmLabelAttr*>::iterator I = |
| 5389 | ExtnameUndeclaredIdentifiers.find(NewVD->getIdentifier()); |
| 5390 | if (I != ExtnameUndeclaredIdentifiers.end()) { |
| 5391 | NewVD->addAttr(I->second); |
| 5392 | ExtnameUndeclaredIdentifiers.erase(I); |
| 5393 | } |
| Zhongxing Xu | 9b7714d | 2009-01-16 02:36:34 +0000 | [diff] [blame] | 5394 | } |
| 5395 | |
| John McCall | a2a3f7d | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 5396 | // Diagnose shadowed variables before filtering for scope. |
| Richard Smith | 72bcaec | 2013-12-05 04:30:04 +0000 | [diff] [blame] | 5397 | if (D.getCXXScopeSpec().isEmpty()) |
| John McCall | df8b37c | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 5398 | CheckShadow(S, NewVD, Previous); |
| John McCall | a2a3f7d | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 5399 | |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5400 | // Don't consider existing declarations that are in a different |
| 5401 | // scope and are out-of-semantic-context declarations (if the new |
| 5402 | // declaration has linkage). |
| Richard Smith | 72bcaec | 2013-12-05 04:30:04 +0000 | [diff] [blame] | 5403 | FilterLookupForScope(Previous, OriginalDC, S, shouldConsiderLinkage(NewVD), |
| 5404 | D.getCXXScopeSpec().isNotEmpty() || |
| 5405 | IsExplicitSpecialization || |
| 5406 | IsVariableTemplateSpecialization); |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5407 | |
| Richard Smith | 1c34fb7 | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 5408 | // Check whether the previous declaration is in the same block scope. This |
| 5409 | // affects whether we merge types with it, per C++11 [dcl.array]p3. |
| 5410 | if (getLangOpts().CPlusPlus && |
| 5411 | NewVD->isLocalVarDecl() && NewVD->hasExternalStorage()) |
| 5412 | NewVD->setPreviousDeclInSameBlockScope( |
| 5413 | Previous.isSingleResult() && !Previous.isShadowed() && |
| Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 5414 | isDeclInScope(Previous.getFoundDecl(), OriginalDC, S, false)); |
| Richard Smith | 1c34fb7 | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 5415 | |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5416 | if (!getLangOpts().CPlusPlus) { |
| Kaelyn Uhrain | 4dc695d | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 5417 | D.setRedeclaration(CheckVariableDeclaration(NewVD, Previous)); |
| 5418 | } else { |
| Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 5419 | // If this is an explicit specialization of a static data member, check it. |
| 5420 | if (IsExplicitSpecialization && !NewVD->isInvalidDecl() && |
| 5421 | CheckMemberSpecialization(NewVD, Previous)) |
| 5422 | NewVD->setInvalidDecl(); |
| 5423 | |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 5424 | // Merge the decl with the existing one if appropriate. |
| 5425 | if (!Previous.empty()) { |
| 5426 | if (Previous.isSingleResult() && |
| 5427 | isa<FieldDecl>(Previous.getFoundDecl()) && |
| 5428 | D.getCXXScopeSpec().isSet()) { |
| 5429 | // The user tried to define a non-static data member |
| 5430 | // out-of-line (C++ [dcl.meaning]p1). |
| 5431 | Diag(NewVD->getLocation(), diag::err_nonstatic_member_out_of_line) |
| 5432 | << D.getCXXScopeSpec().getRange(); |
| 5433 | Previous.clear(); |
| 5434 | NewVD->setInvalidDecl(); |
| 5435 | } |
| 5436 | } else if (D.getCXXScopeSpec().isSet()) { |
| 5437 | // No previous declaration in the qualifying scope. |
| 5438 | Diag(D.getIdentifierLoc(), diag::err_no_member) |
| 5439 | << Name << computeDeclContext(D.getCXXScopeSpec(), true) |
| Douglas Gregor | ef1a09a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 5440 | << D.getCXXScopeSpec().getRange(); |
| Chris Lattner | f6d1c9c | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 5441 | NewVD->setInvalidDecl(); |
| Douglas Gregor | ef1a09a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 5442 | } |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 5443 | |
| Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 5444 | if (!IsVariableTemplateSpecialization) |
| 5445 | D.setRedeclaration(CheckVariableDeclaration(NewVD, Previous)); |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 5446 | |
| Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 5447 | if (NewTemplate) { |
| 5448 | VarTemplateDecl *PrevVarTemplate = |
| 5449 | NewVD->getPreviousDecl() |
| 5450 | ? NewVD->getPreviousDecl()->getDescribedVarTemplate() |
| 5451 | : 0; |
| 5452 | |
| 5453 | // Check the template parameter list of this declaration, possibly |
| 5454 | // merging in the template parameter list from the previous variable |
| 5455 | // template declaration. |
| 5456 | if (CheckTemplateParameterList( |
| 5457 | TemplateParams, |
| 5458 | PrevVarTemplate ? PrevVarTemplate->getTemplateParameters() |
| 5459 | : 0, |
| 5460 | (D.getCXXScopeSpec().isSet() && DC && DC->isRecord() && |
| 5461 | DC->isDependentContext()) |
| 5462 | ? TPC_ClassTemplateMember |
| 5463 | : TPC_VarTemplate)) |
| 5464 | NewVD->setInvalidDecl(); |
| 5465 | |
| 5466 | // If we are providing an explicit specialization of a static variable |
| 5467 | // template, make a note of that. |
| 5468 | if (PrevVarTemplate && |
| 5469 | PrevVarTemplate->getInstantiatedFromMemberTemplate()) |
| 5470 | PrevVarTemplate->setMemberSpecialization(); |
| 5471 | } |
| Douglas Gregor | ef1a09a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 5472 | } |
| Ryan Flynn | e5dc859 | 2009-07-25 22:29:44 +0000 | [diff] [blame] | 5473 | |
| Rafael Espindola | de6a39f | 2013-03-02 21:41:48 +0000 | [diff] [blame] | 5474 | ProcessPragmaWeak(S, NewVD); |
| Rafael Espindola | f1d2f0e | 2013-01-16 23:11:15 +0000 | [diff] [blame] | 5475 | |
| Richard Smith | ac974a3 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 5476 | // If this is the first declaration of an extern C variable, update |
| 5477 | // the map of such variables. |
| Rafael Espindola | 3f9e444 | 2013-10-19 02:13:21 +0000 | [diff] [blame] | 5478 | if (NewVD->isFirstDecl() && !NewVD->isInvalidDecl() && |
| Richard Smith | ac974a3 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 5479 | isIncompleteDeclExternC(*this, NewVD)) |
| Richard Smith | 39b7968 | 2013-06-18 20:15:12 +0000 | [diff] [blame] | 5480 | RegisterLocallyScopedExternCDecl(NewVD, S); |
| Douglas Gregor | ef1a09a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 5481 | |
| Reid Kleckner | d8110b6 | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 5482 | if (getLangOpts().CPlusPlus && NewVD->isStaticLocal()) { |
| Eli Friedman | 3b7d46c | 2013-07-10 00:30:46 +0000 | [diff] [blame] | 5483 | Decl *ManglingContextDecl; |
| 5484 | if (MangleNumberingContext *MCtx = |
| 5485 | getCurrentMangleNumberContext(NewVD->getDeclContext(), |
| 5486 | ManglingContextDecl)) { |
| David Majnemer | f27217f | 2014-03-05 18:55:38 +0000 | [diff] [blame] | 5487 | Context.setManglingNumber( |
| 5488 | NewVD, MCtx->getManglingNumber(NewVD, S->getMSLocalManglingNumber())); |
| David Majnemer | 2206bf5 | 2014-03-05 08:57:59 +0000 | [diff] [blame] | 5489 | Context.setStaticLocalNumber(NewVD, MCtx->getStaticLocalNumber(NewVD)); |
| Eli Friedman | 3b7d46c | 2013-07-10 00:30:46 +0000 | [diff] [blame] | 5490 | } |
| 5491 | } |
| 5492 | |
| Larisse Voufo | 72caf2b | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 5493 | if (NewTemplate) { |
| Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 5494 | if (NewVD->isInvalidDecl()) |
| 5495 | NewTemplate->setInvalidDecl(); |
| Larisse Voufo | 72caf2b | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 5496 | ActOnDocumentableDecl(NewTemplate); |
| 5497 | return NewTemplate; |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5498 | } |
| 5499 | |
| Larisse Voufo | 72caf2b | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 5500 | return NewVD; |
| Douglas Gregor | ef1a09a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 5501 | } |
| 5502 | |
| John McCall | df8b37c | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 5503 | /// \brief Diagnose variable or built-in function shadowing. Implements |
| 5504 | /// -Wshadow. |
| John McCall | a2a3f7d | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 5505 | /// |
| John McCall | df8b37c | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 5506 | /// This method is called whenever a VarDecl is added to a "useful" |
| 5507 | /// scope. |
| John McCall | a2a3f7d | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 5508 | /// |
| John McCall | 2d8c760 | 2010-03-20 04:12:52 +0000 | [diff] [blame] | 5509 | /// \param S the scope in which the shadowing name is being declared |
| 5510 | /// \param R the lookup of the name |
| John McCall | a2a3f7d | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 5511 | /// |
| John McCall | df8b37c | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 5512 | void Sema::CheckShadow(Scope *S, VarDecl *D, const LookupResult& R) { |
| John McCall | a2a3f7d | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 5513 | // Return if warning is ignored. |
| Argyrios Kyrtzidis | 1cb0de1 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 5514 | if (Diags.getDiagnosticLevel(diag::warn_decl_shadow, R.getNameLoc()) == |
| David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 5515 | DiagnosticsEngine::Ignored) |
| John McCall | a2a3f7d | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 5516 | return; |
| 5517 | |
| Argyrios Kyrtzidis | 898fdbf | 2011-02-08 18:21:25 +0000 | [diff] [blame] | 5518 | // Don't diagnose declarations at file scope. |
| Argyrios Kyrtzidis | bd0a3fe | 2011-04-25 21:39:50 +0000 | [diff] [blame] | 5519 | if (D->hasGlobalStorage()) |
| John McCall | a2a3f7d | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 5520 | return; |
| Argyrios Kyrtzidis | bd0a3fe | 2011-04-25 21:39:50 +0000 | [diff] [blame] | 5521 | |
| 5522 | DeclContext *NewDC = D->getDeclContext(); |
| 5523 | |
| John McCall | 2d8c760 | 2010-03-20 04:12:52 +0000 | [diff] [blame] | 5524 | // Only diagnose if we're shadowing an unambiguous field or variable. |
| Douglas Gregor | 319aa6c | 2010-03-17 16:03:44 +0000 | [diff] [blame] | 5525 | if (R.getResultKind() != LookupResult::Found) |
| John McCall | a2a3f7d | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 5526 | return; |
| John McCall | a2a3f7d | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 5527 | |
| John McCall | a2a3f7d | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 5528 | NamedDecl* ShadowedDecl = R.getFoundDecl(); |
| 5529 | if (!isa<VarDecl>(ShadowedDecl) && !isa<FieldDecl>(ShadowedDecl)) |
| 5530 | return; |
| 5531 | |
| Argyrios Kyrtzidis | f46cc65 | 2011-01-31 07:04:54 +0000 | [diff] [blame] | 5532 | // Fields are not shadowed by variables in C++ static methods. |
| 5533 | if (isa<FieldDecl>(ShadowedDecl)) |
| 5534 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(NewDC)) |
| 5535 | if (MD->isStatic()) |
| 5536 | return; |
| 5537 | |
| Argyrios Kyrtzidis | 857dd06 | 2011-01-31 07:04:50 +0000 | [diff] [blame] | 5538 | if (VarDecl *shadowedVar = dyn_cast<VarDecl>(ShadowedDecl)) |
| 5539 | if (shadowedVar->isExternC()) { |
| Argyrios Kyrtzidis | 857dd06 | 2011-01-31 07:04:50 +0000 | [diff] [blame] | 5540 | // For shadowing external vars, make sure that we point to the global |
| 5541 | // declaration, not a locally scoped extern declaration. |
| Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 5542 | for (auto I : shadowedVar->redecls()) |
| Argyrios Kyrtzidis | 857dd06 | 2011-01-31 07:04:50 +0000 | [diff] [blame] | 5543 | if (I->isFileVarDecl()) { |
| Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 5544 | ShadowedDecl = I; |
| Argyrios Kyrtzidis | 857dd06 | 2011-01-31 07:04:50 +0000 | [diff] [blame] | 5545 | break; |
| 5546 | } |
| 5547 | } |
| 5548 | |
| 5549 | DeclContext *OldDC = ShadowedDecl->getDeclContext(); |
| 5550 | |
| John McCall | 2d8c760 | 2010-03-20 04:12:52 +0000 | [diff] [blame] | 5551 | // Only warn about certain kinds of shadowing for class members. |
| 5552 | if (NewDC && NewDC->isRecord()) { |
| 5553 | // In particular, don't warn about shadowing non-class members. |
| 5554 | if (!OldDC->isRecord()) |
| 5555 | return; |
| 5556 | |
| 5557 | // TODO: should we warn about static data members shadowing |
| 5558 | // static data members from base classes? |
| 5559 | |
| 5560 | // TODO: don't diagnose for inaccessible shadowed members. |
| 5561 | // This is hard to do perfectly because we might friend the |
| 5562 | // shadowing context, but that's just a false negative. |
| 5563 | } |
| 5564 | |
| 5565 | // Determine what kind of declaration we're shadowing. |
| John McCall | a2a3f7d | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 5566 | unsigned Kind; |
| John McCall | 2d8c760 | 2010-03-20 04:12:52 +0000 | [diff] [blame] | 5567 | if (isa<RecordDecl>(OldDC)) { |
| John McCall | a2a3f7d | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 5568 | if (isa<FieldDecl>(ShadowedDecl)) |
| 5569 | Kind = 3; // field |
| 5570 | else |
| 5571 | Kind = 2; // static data member |
| John McCall | 2d8c760 | 2010-03-20 04:12:52 +0000 | [diff] [blame] | 5572 | } else if (OldDC->isFileContext()) |
| John McCall | a2a3f7d | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 5573 | Kind = 1; // global |
| 5574 | else |
| 5575 | Kind = 0; // local |
| 5576 | |
| John McCall | 2d8c760 | 2010-03-20 04:12:52 +0000 | [diff] [blame] | 5577 | DeclarationName Name = R.getLookupName(); |
| 5578 | |
| John McCall | a2a3f7d | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 5579 | // Emit warning and note. |
| Alp Toker | 15ab373 | 2013-12-12 12:47:48 +0000 | [diff] [blame] | 5580 | if (getSourceManager().isInSystemMacro(R.getNameLoc())) |
| 5581 | return; |
| John McCall | 2d8c760 | 2010-03-20 04:12:52 +0000 | [diff] [blame] | 5582 | Diag(R.getNameLoc(), diag::warn_decl_shadow) << Name << Kind << OldDC; |
| John McCall | a2a3f7d | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 5583 | Diag(ShadowedDecl->getLocation(), diag::note_previous_declaration); |
| 5584 | } |
| 5585 | |
| John McCall | df8b37c | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 5586 | /// \brief Check -Wshadow without the advantage of a previous lookup. |
| 5587 | void Sema::CheckShadow(Scope *S, VarDecl *D) { |
| Argyrios Kyrtzidis | 1cb0de1 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 5588 | if (Diags.getDiagnosticLevel(diag::warn_decl_shadow, D->getLocation()) == |
| David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 5589 | DiagnosticsEngine::Ignored) |
| Argyrios Kyrtzidis | 1cb0de1 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 5590 | return; |
| 5591 | |
| John McCall | df8b37c | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 5592 | LookupResult R(*this, D->getDeclName(), D->getLocation(), |
| 5593 | Sema::LookupOrdinaryName, Sema::ForRedeclaration); |
| 5594 | LookupName(R, S); |
| 5595 | CheckShadow(S, D, R); |
| 5596 | } |
| 5597 | |
| Richard Smith | ac974a3 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 5598 | /// Check for conflict between this global or extern "C" declaration and |
| 5599 | /// previous global or extern "C" declarations. This is only used in C++. |
| Rafael Espindola | 46afb35 | 2013-01-11 19:34:23 +0000 | [diff] [blame] | 5600 | template<typename T> |
| Richard Smith | ac974a3 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 5601 | static bool checkGlobalOrExternCConflict( |
| 5602 | Sema &S, const T *ND, bool IsGlobal, LookupResult &Previous) { |
| 5603 | assert(S.getLangOpts().CPlusPlus && "only C++ has extern \"C\""); |
| 5604 | NamedDecl *Prev = S.findLocallyScopedExternCDecl(ND->getDeclName()); |
| Rafael Espindola | 0e0d009 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 5605 | |
| Richard Smith | ac974a3 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 5606 | if (!Prev && IsGlobal && !isIncompleteDeclExternC(S, ND)) { |
| 5607 | // The common case: this global doesn't conflict with any extern "C" |
| 5608 | // declaration. |
| 5609 | return false; |
| 5610 | } |
| 5611 | |
| 5612 | if (Prev) { |
| 5613 | if (!IsGlobal || isIncompleteDeclExternC(S, ND)) { |
| 5614 | // Both the old and new declarations have C language linkage. This is a |
| 5615 | // redeclaration. |
| 5616 | Previous.clear(); |
| 5617 | Previous.addDecl(Prev); |
| 5618 | return true; |
| 5619 | } |
| 5620 | |
| 5621 | // This is a global, non-extern "C" declaration, and there is a previous |
| 5622 | // non-global extern "C" declaration. Diagnose if this is a variable |
| 5623 | // declaration. |
| 5624 | if (!isa<VarDecl>(ND)) |
| 5625 | return false; |
| 5626 | } else { |
| 5627 | // The declaration is extern "C". Check for any declaration in the |
| 5628 | // translation unit which might conflict. |
| 5629 | if (IsGlobal) { |
| 5630 | // We have already performed the lookup into the translation unit. |
| 5631 | IsGlobal = false; |
| 5632 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 5633 | I != E; ++I) { |
| 5634 | if (isa<VarDecl>(*I)) { |
| 5635 | Prev = *I; |
| 5636 | break; |
| 5637 | } |
| 5638 | } |
| 5639 | } else { |
| 5640 | DeclContext::lookup_result R = |
| 5641 | S.Context.getTranslationUnitDecl()->lookup(ND->getDeclName()); |
| 5642 | for (DeclContext::lookup_result::iterator I = R.begin(), E = R.end(); |
| 5643 | I != E; ++I) { |
| 5644 | if (isa<VarDecl>(*I)) { |
| 5645 | Prev = *I; |
| 5646 | break; |
| 5647 | } |
| 5648 | // FIXME: If we have any other entity with this name in global scope, |
| 5649 | // the declaration is ill-formed, but that is a defect: it breaks the |
| 5650 | // 'stat' hack, for instance. Only variables can have mangled name |
| 5651 | // clashes with extern "C" declarations, so only they deserve a |
| 5652 | // diagnostic. |
| 5653 | } |
| 5654 | } |
| 5655 | |
| 5656 | if (!Prev) |
| Rafael Espindola | 0e0d009 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 5657 | return false; |
| 5658 | } |
| 5659 | |
| Richard Smith | ac974a3 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 5660 | // Use the first declaration's location to ensure we point at something which |
| 5661 | // is lexically inside an extern "C" linkage-spec. |
| 5662 | assert(Prev && "should have found a previous declaration to diagnose"); |
| 5663 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(Prev)) |
| Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 5664 | Prev = FD->getFirstDecl(); |
| Richard Smith | ac974a3 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 5665 | else |
| Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 5666 | Prev = cast<VarDecl>(Prev)->getFirstDecl(); |
| Richard Smith | ac974a3 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 5667 | |
| 5668 | S.Diag(ND->getLocation(), diag::err_extern_c_global_conflict) |
| 5669 | << IsGlobal << ND; |
| 5670 | S.Diag(Prev->getLocation(), diag::note_extern_c_global_conflict) |
| 5671 | << IsGlobal; |
| 5672 | return false; |
| 5673 | } |
| 5674 | |
| 5675 | /// Apply special rules for handling extern "C" declarations. Returns \c true |
| 5676 | /// if we have found that this is a redeclaration of some prior entity. |
| 5677 | /// |
| 5678 | /// Per C++ [dcl.link]p6: |
| 5679 | /// Two declarations [for a function or variable] with C language linkage |
| 5680 | /// with the same name that appear in different scopes refer to the same |
| 5681 | /// [entity]. An entity with C language linkage shall not be declared with |
| 5682 | /// the same name as an entity in global scope. |
| 5683 | template<typename T> |
| 5684 | static bool checkForConflictWithNonVisibleExternC(Sema &S, const T *ND, |
| 5685 | LookupResult &Previous) { |
| 5686 | if (!S.getLangOpts().CPlusPlus) { |
| 5687 | // In C, when declaring a global variable, look for a corresponding 'extern' |
| Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 5688 | // variable declared in function scope. We don't need this in C++, because |
| 5689 | // we find local extern decls in the surrounding file-scope DeclContext. |
| Richard Smith | ac974a3 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 5690 | if (ND->getDeclContext()->getRedeclContext()->isTranslationUnit()) { |
| 5691 | if (NamedDecl *Prev = S.findLocallyScopedExternCDecl(ND->getDeclName())) { |
| 5692 | Previous.clear(); |
| 5693 | Previous.addDecl(Prev); |
| 5694 | return true; |
| 5695 | } |
| 5696 | } |
| 5697 | return false; |
| 5698 | } |
| 5699 | |
| 5700 | // A declaration in the translation unit can conflict with an extern "C" |
| 5701 | // declaration. |
| 5702 | if (ND->getDeclContext()->getRedeclContext()->isTranslationUnit()) |
| 5703 | return checkGlobalOrExternCConflict(S, ND, /*IsGlobal*/true, Previous); |
| 5704 | |
| 5705 | // An extern "C" declaration can conflict with a declaration in the |
| 5706 | // translation unit or can be a redeclaration of an extern "C" declaration |
| 5707 | // in another scope. |
| 5708 | if (isIncompleteDeclExternC(S,ND)) |
| 5709 | return checkGlobalOrExternCConflict(S, ND, /*IsGlobal*/false, Previous); |
| 5710 | |
| 5711 | // Neither global nor extern "C": nothing to do. |
| 5712 | return false; |
| Rafael Espindola | 46afb35 | 2013-01-11 19:34:23 +0000 | [diff] [blame] | 5713 | } |
| 5714 | |
| Richard Smith | 27d807c | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 5715 | void Sema::CheckVariableDeclarationType(VarDecl *NewVD) { |
| Chris Lattner | f6d1c9c | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 5716 | // If the decl is already known invalid, don't check it. |
| 5717 | if (NewVD->isInvalidDecl()) |
| Richard Smith | 27d807c | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 5718 | return; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5719 | |
| Abramo Bagnara | 341ab73 | 2012-11-08 14:44:42 +0000 | [diff] [blame] | 5720 | TypeSourceInfo *TInfo = NewVD->getTypeSourceInfo(); |
| 5721 | QualType T = TInfo->getType(); |
| Douglas Gregor | ef1a09a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 5722 | |
| Richard Smith | 27d807c | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 5723 | // Defer checking an 'auto' type until its initializer is attached. |
| 5724 | if (T->isUndeducedType()) |
| 5725 | return; |
| 5726 | |
| John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5727 | if (T->isObjCObjectType()) { |
| Fariborz Jahanian | 9a14c21 | 2011-07-25 21:12:27 +0000 | [diff] [blame] | 5728 | Diag(NewVD->getLocation(), diag::err_statically_allocated_object) |
| 5729 | << FixItHint::CreateInsertion(NewVD->getLocation(), "*"); |
| Fariborz Jahanian | 6507135e | 2011-07-26 17:58:54 +0000 | [diff] [blame] | 5730 | T = Context.getObjCObjectPointerType(T); |
| 5731 | NewVD->setType(T); |
| Douglas Gregor | ef1a09a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 5732 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5733 | |
| Zhongxing Xu | 9b7714d | 2009-01-16 02:36:34 +0000 | [diff] [blame] | 5734 | // Emit an error if an address space was applied to decl with local storage. |
| 5735 | // This includes arrays of objects with address space qualifiers, but not |
| 5736 | // automatic variables that point to other address spaces. |
| 5737 | // ISO/IEC TR 18037 S5.1.2 |
| Chris Lattner | 88fdea8 | 2010-10-10 18:16:20 +0000 | [diff] [blame] | 5738 | if (NewVD->hasLocalStorage() && T.getAddressSpace() != 0) { |
| Douglas Gregor | ef1a09a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 5739 | Diag(NewVD->getLocation(), diag::err_as_qualified_auto_decl); |
| Kaelyn Uhrain | 4dc695d | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 5740 | NewVD->setInvalidDecl(); |
| Richard Smith | 27d807c | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 5741 | return; |
| Zhongxing Xu | 9b7714d | 2009-01-16 02:36:34 +0000 | [diff] [blame] | 5742 | } |
| Fariborz Jahanian | 0c9404e | 2009-02-21 19:44:02 +0000 | [diff] [blame] | 5743 | |
| Tanya Lattner | 713eef4 | 2013-04-05 20:14:50 +0000 | [diff] [blame] | 5744 | // OpenCL v1.2 s6.5 - All program scope variables must be declared in the |
| 5745 | // __constant address space. |
| 5746 | if (getLangOpts().OpenCL && NewVD->isFileVarDecl() |
| 5747 | && T.getAddressSpace() != LangAS::opencl_constant |
| 5748 | && !T->isSamplerT()){ |
| 5749 | Diag(NewVD->getLocation(), diag::err_opencl_global_invalid_addr_space); |
| 5750 | NewVD->setInvalidDecl(); |
| Richard Smith | 27d807c | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 5751 | return; |
| Tanya Lattner | 713eef4 | 2013-04-05 20:14:50 +0000 | [diff] [blame] | 5752 | } |
| 5753 | |
| Tanya Lattner | 4fdce3f | 2012-06-19 23:09:52 +0000 | [diff] [blame] | 5754 | // OpenCL v1.2 s6.8 -- The static qualifier is valid only in program |
| 5755 | // scope. |
| 5756 | if ((getLangOpts().OpenCLVersion >= 120) |
| 5757 | && NewVD->isStaticLocal()) { |
| 5758 | Diag(NewVD->getLocation(), diag::err_static_function_scope); |
| 5759 | NewVD->setInvalidDecl(); |
| Richard Smith | 27d807c | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 5760 | return; |
| Tanya Lattner | 4fdce3f | 2012-06-19 23:09:52 +0000 | [diff] [blame] | 5761 | } |
| 5762 | |
| Mike Stump | ca5ae66 | 2009-04-14 00:57:29 +0000 | [diff] [blame] | 5763 | if (NewVD->hasLocalStorage() && T.isObjCGCWeak() |
| Fariborz Jahanian | c32830c | 2011-06-07 20:15:46 +0000 | [diff] [blame] | 5764 | && !NewVD->hasAttr<BlocksAttr>()) { |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5765 | if (getLangOpts().getGC() != LangOptions::NonGC) |
| Fariborz Jahanian | c32830c | 2011-06-07 20:15:46 +0000 | [diff] [blame] | 5766 | Diag(NewVD->getLocation(), diag::warn_gc_attribute_weak_on_local); |
| Ted Kremenek | 2f88c40 | 2012-10-02 05:36:02 +0000 | [diff] [blame] | 5767 | else { |
| 5768 | assert(!getLangOpts().ObjCAutoRefCount); |
| Fariborz Jahanian | c32830c | 2011-06-07 20:15:46 +0000 | [diff] [blame] | 5769 | Diag(NewVD->getLocation(), diag::warn_attribute_weak_on_local); |
| Ted Kremenek | 2f88c40 | 2012-10-02 05:36:02 +0000 | [diff] [blame] | 5770 | } |
| Fariborz Jahanian | c32830c | 2011-06-07 20:15:46 +0000 | [diff] [blame] | 5771 | } |
| Chris Lattner | 88fdea8 | 2010-10-10 18:16:20 +0000 | [diff] [blame] | 5772 | |
| Chris Lattner | 9fecd74 | 2009-04-19 05:21:20 +0000 | [diff] [blame] | 5773 | bool isVM = T->isVariablyModifiedType(); |
| Chris Lattner | 9662cd3 | 2009-07-19 20:17:11 +0000 | [diff] [blame] | 5774 | if (isVM || NewVD->hasAttr<CleanupAttr>() || |
| John McCall | d4e1b76 | 2010-08-01 01:24:59 +0000 | [diff] [blame] | 5775 | NewVD->hasAttr<BlocksAttr>()) |
| John McCall | aab3e41 | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 5776 | getCurFunction()->setHasBranchProtectedScope(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5777 | |
| Chris Lattner | 9fecd74 | 2009-04-19 05:21:20 +0000 | [diff] [blame] | 5778 | if ((isVM && NewVD->hasLinkage()) || |
| 5779 | (T->isVariableArrayType() && NewVD->hasGlobalStorage())) { |
| Anders Carlsson | 6c88580 | 2009-02-28 21:56:50 +0000 | [diff] [blame] | 5780 | bool SizeIsNegative; |
| Douglas Gregor | caa1bf4 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 5781 | llvm::APSInt Oversized; |
| Abramo Bagnara | 341ab73 | 2012-11-08 14:44:42 +0000 | [diff] [blame] | 5782 | TypeSourceInfo *FixedTInfo = |
| 5783 | TryToFixInvalidVariablyModifiedTypeSourceInfo(TInfo, Context, |
| 5784 | SizeIsNegative, Oversized); |
| 5785 | if (FixedTInfo == 0 && T->isVariableArrayType()) { |
| Douglas Gregor | ef1a09a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 5786 | const VariableArrayType *VAT = Context.getAsVariableArrayType(T); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5787 | // FIXME: This won't give the correct result for |
| 5788 | // int a[10][n]; |
| Anders Carlsson | 6c88580 | 2009-02-28 21:56:50 +0000 | [diff] [blame] | 5789 | SourceRange SizeRange = VAT->getSizeExpr()->getSourceRange(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5790 | |
| Anders Carlsson | 6c88580 | 2009-02-28 21:56:50 +0000 | [diff] [blame] | 5791 | if (NewVD->isFileVarDecl()) |
| 5792 | Diag(NewVD->getLocation(), diag::err_vla_decl_in_file_scope) |
| Chris Lattner | f6d1c9c | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 5793 | << SizeRange; |
| Enea Zaffanella | 28f36ba | 2013-05-10 20:34:44 +0000 | [diff] [blame] | 5794 | else if (NewVD->isStaticLocal()) |
| Anders Carlsson | 6c88580 | 2009-02-28 21:56:50 +0000 | [diff] [blame] | 5795 | Diag(NewVD->getLocation(), diag::err_vla_decl_has_static_storage) |
| Chris Lattner | f6d1c9c | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 5796 | << SizeRange; |
| Anders Carlsson | 6c88580 | 2009-02-28 21:56:50 +0000 | [diff] [blame] | 5797 | else |
| 5798 | Diag(NewVD->getLocation(), diag::err_vla_decl_has_extern_linkage) |
| Chris Lattner | f6d1c9c | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 5799 | << SizeRange; |
| Kaelyn Uhrain | 4dc695d | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 5800 | NewVD->setInvalidDecl(); |
| Richard Smith | 27d807c | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 5801 | return; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5802 | } |
| 5803 | |
| Abramo Bagnara | 341ab73 | 2012-11-08 14:44:42 +0000 | [diff] [blame] | 5804 | if (FixedTInfo == 0) { |
| Anders Carlsson | 6c88580 | 2009-02-28 21:56:50 +0000 | [diff] [blame] | 5805 | if (NewVD->isFileVarDecl()) |
| 5806 | Diag(NewVD->getLocation(), diag::err_vm_decl_in_file_scope); |
| 5807 | else |
| 5808 | Diag(NewVD->getLocation(), diag::err_vm_decl_has_extern_linkage); |
| Kaelyn Uhrain | 4dc695d | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 5809 | NewVD->setInvalidDecl(); |
| Richard Smith | 27d807c | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 5810 | return; |
| Anders Carlsson | 6c88580 | 2009-02-28 21:56:50 +0000 | [diff] [blame] | 5811 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5812 | |
| Chris Lattner | f6d1c9c | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 5813 | Diag(NewVD->getLocation(), diag::warn_illegal_constant_array_size); |
| Abramo Bagnara | 3b8a9e5 | 2012-11-08 16:01:51 +0000 | [diff] [blame] | 5814 | NewVD->setType(FixedTInfo->getType()); |
| Abramo Bagnara | 341ab73 | 2012-11-08 14:44:42 +0000 | [diff] [blame] | 5815 | NewVD->setTypeSourceInfo(FixedTInfo); |
| Anders Carlsson | 6c88580 | 2009-02-28 21:56:50 +0000 | [diff] [blame] | 5816 | } |
| 5817 | |
| David Majnemer | 0ffa331 | 2013-05-29 00:56:45 +0000 | [diff] [blame] | 5818 | if (T->isVoidType()) { |
| 5819 | // C++98 [dcl.stc]p5: The extern specifier can be applied only to the names |
| 5820 | // of objects and functions. |
| 5821 | if (NewVD->isThisDeclarationADefinition() || getLangOpts().CPlusPlus) { |
| 5822 | Diag(NewVD->getLocation(), diag::err_typecheck_decl_incomplete_type) |
| 5823 | << T; |
| 5824 | NewVD->setInvalidDecl(); |
| 5825 | return; |
| 5826 | } |
| Richard Smith | 27d807c | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 5827 | } |
| 5828 | |
| 5829 | if (!NewVD->hasLocalStorage() && NewVD->hasAttr<BlocksAttr>()) { |
| 5830 | Diag(NewVD->getLocation(), diag::err_block_on_nonlocal); |
| 5831 | NewVD->setInvalidDecl(); |
| 5832 | return; |
| 5833 | } |
| 5834 | |
| 5835 | if (isVM && NewVD->hasAttr<BlocksAttr>()) { |
| 5836 | Diag(NewVD->getLocation(), diag::err_block_on_vm); |
| 5837 | NewVD->setInvalidDecl(); |
| 5838 | return; |
| 5839 | } |
| 5840 | |
| 5841 | if (NewVD->isConstexpr() && !T->isDependentType() && |
| 5842 | RequireLiteralType(NewVD->getLocation(), T, |
| 5843 | diag::err_constexpr_var_non_literal)) { |
| 5844 | // Can't perform this check until the type is deduced. |
| 5845 | NewVD->setInvalidDecl(); |
| 5846 | return; |
| 5847 | } |
| 5848 | } |
| 5849 | |
| 5850 | /// \brief Perform semantic checking on a newly-created variable |
| 5851 | /// declaration. |
| 5852 | /// |
| 5853 | /// This routine performs all of the type-checking required for a |
| 5854 | /// variable declaration once it has been built. It is used both to |
| 5855 | /// check variables after they have been parsed and their declarators |
| 5856 | /// have been translated into a declaration, and to check variables |
| 5857 | /// that have been instantiated from a template. |
| 5858 | /// |
| 5859 | /// Sets NewVD->isInvalidDecl() if an error was encountered. |
| 5860 | /// |
| 5861 | /// Returns true if the variable declaration is a redeclaration. |
| Larisse Voufo | 72caf2b | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 5862 | bool Sema::CheckVariableDeclaration(VarDecl *NewVD, LookupResult &Previous) { |
| Richard Smith | 27d807c | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 5863 | CheckVariableDeclarationType(NewVD); |
| 5864 | |
| 5865 | // If the decl is already known invalid, don't check it. |
| 5866 | if (NewVD->isInvalidDecl()) |
| 5867 | return false; |
| 5868 | |
| John McCall | b65e8fe | 2013-04-01 18:34:28 +0000 | [diff] [blame] | 5869 | // If we did not find anything by this name, look for a non-visible |
| 5870 | // extern "C" declaration with the same name. |
| Richard Smith | 1c34fb7 | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 5871 | if (Previous.empty() && |
| 5872 | checkForConflictWithNonVisibleExternC(*this, NewVD, Previous)) |
| Richard Smith | 3c78578 | 2013-09-03 21:00:58 +0000 | [diff] [blame] | 5873 | Previous.setShadowed(); |
| Douglas Gregor | 5a80bd1 | 2009-03-02 00:19:53 +0000 | [diff] [blame] | 5874 | |
| Douglas Gregor | 3552dab | 2013-01-09 00:47:56 +0000 | [diff] [blame] | 5875 | // Filter out any non-conflicting previous declarations. |
| 5876 | filterNonConflictingPreviousDecls(Context, NewVD, Previous); |
| 5877 | |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5878 | if (!Previous.empty()) { |
| Richard Smith | 3c78578 | 2013-09-03 21:00:58 +0000 | [diff] [blame] | 5879 | MergeVarDecl(NewVD, Previous); |
| Kaelyn Uhrain | 4dc695d | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 5880 | return true; |
| Zhongxing Xu | 9b7714d | 2009-01-16 02:36:34 +0000 | [diff] [blame] | 5881 | } |
| Kaelyn Uhrain | 4dc695d | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 5882 | return false; |
| Zhongxing Xu | 9b7714d | 2009-01-16 02:36:34 +0000 | [diff] [blame] | 5883 | } |
| 5884 | |
| Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 5885 | /// \brief Data used with FindOverriddenMethod |
| 5886 | struct FindOverriddenMethodData { |
| 5887 | Sema *S; |
| 5888 | CXXMethodDecl *Method; |
| 5889 | }; |
| 5890 | |
| 5891 | /// \brief Member lookup function that determines whether a given C++ |
| 5892 | /// method overrides a method in a base class, to be used with |
| 5893 | /// CXXRecordDecl::lookupInBases(). |
| John McCall | 84c16cf | 2009-11-12 03:15:40 +0000 | [diff] [blame] | 5894 | static bool FindOverriddenMethod(const CXXBaseSpecifier *Specifier, |
| Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 5895 | CXXBasePath &Path, |
| 5896 | void *UserData) { |
| 5897 | RecordDecl *BaseRecord = Specifier->getType()->getAs<RecordType>()->getDecl(); |
| Anders Carlsson | e985fae | 2009-11-26 20:50:40 +0000 | [diff] [blame] | 5898 | |
| Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 5899 | FindOverriddenMethodData *Data |
| 5900 | = reinterpret_cast<FindOverriddenMethodData*>(UserData); |
| Anders Carlsson | e985fae | 2009-11-26 20:50:40 +0000 | [diff] [blame] | 5901 | |
| 5902 | DeclarationName Name = Data->Method->getDeclName(); |
| 5903 | |
| 5904 | // FIXME: Do we care about other names here too? |
| 5905 | if (Name.getNameKind() == DeclarationName::CXXDestructorName) { |
| John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 5906 | // We really want to find the base class destructor here. |
| Anders Carlsson | e985fae | 2009-11-26 20:50:40 +0000 | [diff] [blame] | 5907 | QualType T = Data->S->Context.getTypeDeclType(BaseRecord); |
| 5908 | CanQualType CT = Data->S->Context.getCanonicalType(T); |
| 5909 | |
| Anders Carlsson | 5a4f772 | 2009-11-27 01:26:58 +0000 | [diff] [blame] | 5910 | Name = Data->S->Context.DeclarationNames.getCXXDestructorName(CT); |
| Anders Carlsson | e985fae | 2009-11-26 20:50:40 +0000 | [diff] [blame] | 5911 | } |
| 5912 | |
| 5913 | for (Path.Decls = BaseRecord->lookup(Name); |
| David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 5914 | !Path.Decls.empty(); |
| 5915 | Path.Decls = Path.Decls.slice(1)) { |
| 5916 | NamedDecl *D = Path.Decls.front(); |
| John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 5917 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) { |
| 5918 | if (MD->isVirtual() && !Data->S->IsOverload(Data->Method, MD, false)) |
| Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 5919 | return true; |
| 5920 | } |
| 5921 | } |
| 5922 | |
| 5923 | return false; |
| 5924 | } |
| 5925 | |
| David Blaikie | 7e41426 | 2012-10-17 00:47:58 +0000 | [diff] [blame] | 5926 | namespace { |
| 5927 | enum OverrideErrorKind { OEK_All, OEK_NonDeleted, OEK_Deleted }; |
| 5928 | } |
| 5929 | /// \brief Report an error regarding overriding, along with any relevant |
| 5930 | /// overriden methods. |
| 5931 | /// |
| 5932 | /// \param DiagID the primary error to report. |
| 5933 | /// \param MD the overriding method. |
| 5934 | /// \param OEK which overrides to include as notes. |
| 5935 | static void ReportOverrides(Sema& S, unsigned DiagID, const CXXMethodDecl *MD, |
| 5936 | OverrideErrorKind OEK = OEK_All) { |
| 5937 | S.Diag(MD->getLocation(), DiagID) << MD->getDeclName(); |
| 5938 | for (CXXMethodDecl::method_iterator I = MD->begin_overridden_methods(), |
| 5939 | E = MD->end_overridden_methods(); |
| 5940 | I != E; ++I) { |
| 5941 | // This check (& the OEK parameter) could be replaced by a predicate, but |
| 5942 | // without lambdas that would be overkill. This is still nicer than writing |
| 5943 | // out the diag loop 3 times. |
| 5944 | if ((OEK == OEK_All) || |
| 5945 | (OEK == OEK_NonDeleted && !(*I)->isDeleted()) || |
| 5946 | (OEK == OEK_Deleted && (*I)->isDeleted())) |
| 5947 | S.Diag((*I)->getLocation(), diag::note_overridden_virtual_function); |
| 5948 | } |
| 5949 | } |
| 5950 | |
| Sebastian Redl | d5b2453 | 2009-11-18 21:51:29 +0000 | [diff] [blame] | 5951 | /// AddOverriddenMethods - See if a method overrides any in the base classes, |
| 5952 | /// and if so, check that it's a valid override and remember it. |
| Douglas Gregor | 5a2bb5b | 2010-10-13 22:55:32 +0000 | [diff] [blame] | 5953 | bool Sema::AddOverriddenMethods(CXXRecordDecl *DC, CXXMethodDecl *MD) { |
| Sebastian Redl | d5b2453 | 2009-11-18 21:51:29 +0000 | [diff] [blame] | 5954 | // Look for virtual methods in base classes that this method might override. |
| 5955 | CXXBasePaths Paths; |
| 5956 | FindOverriddenMethodData Data; |
| 5957 | Data.Method = MD; |
| 5958 | Data.S = this; |
| David Blaikie | 7e41426 | 2012-10-17 00:47:58 +0000 | [diff] [blame] | 5959 | bool hasDeletedOverridenMethods = false; |
| 5960 | bool hasNonDeletedOverridenMethods = false; |
| Douglas Gregor | 5a2bb5b | 2010-10-13 22:55:32 +0000 | [diff] [blame] | 5961 | bool AddedAny = false; |
| Sebastian Redl | d5b2453 | 2009-11-18 21:51:29 +0000 | [diff] [blame] | 5962 | if (DC->lookupInBases(&FindOverriddenMethod, &Data, Paths)) { |
| 5963 | for (CXXBasePaths::decl_iterator I = Paths.found_decls_begin(), |
| 5964 | E = Paths.found_decls_end(); I != E; ++I) { |
| 5965 | if (CXXMethodDecl *OldMD = dyn_cast<CXXMethodDecl>(*I)) { |
| Richard Trieu | 95d8809 | 2011-07-01 20:02:53 +0000 | [diff] [blame] | 5966 | MD->addOverriddenMethod(OldMD->getCanonicalDecl()); |
| Sebastian Redl | d5b2453 | 2009-11-18 21:51:29 +0000 | [diff] [blame] | 5967 | if (!CheckOverridingFunctionReturnType(MD, OldMD) && |
| Aaron Ballman | 02df2e0 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 5968 | !CheckOverridingFunctionAttributes(MD, OldMD) && |
| Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 5969 | !CheckOverridingFunctionExceptionSpec(MD, OldMD) && |
| Anders Carlsson | 3f610c7 | 2011-01-20 16:25:36 +0000 | [diff] [blame] | 5970 | !CheckIfOverriddenFunctionIsMarkedFinal(MD, OldMD)) { |
| David Blaikie | 7e41426 | 2012-10-17 00:47:58 +0000 | [diff] [blame] | 5971 | hasDeletedOverridenMethods |= OldMD->isDeleted(); |
| 5972 | hasNonDeletedOverridenMethods |= !OldMD->isDeleted(); |
| Douglas Gregor | 5a2bb5b | 2010-10-13 22:55:32 +0000 | [diff] [blame] | 5973 | AddedAny = true; |
| 5974 | } |
| Sebastian Redl | d5b2453 | 2009-11-18 21:51:29 +0000 | [diff] [blame] | 5975 | } |
| 5976 | } |
| 5977 | } |
| David Blaikie | 7e41426 | 2012-10-17 00:47:58 +0000 | [diff] [blame] | 5978 | |
| 5979 | if (hasDeletedOverridenMethods && !MD->isDeleted()) { |
| 5980 | ReportOverrides(*this, diag::err_non_deleted_override, MD, OEK_Deleted); |
| 5981 | } |
| 5982 | if (hasNonDeletedOverridenMethods && MD->isDeleted()) { |
| 5983 | ReportOverrides(*this, diag::err_deleted_override, MD, OEK_NonDeleted); |
| 5984 | } |
| 5985 | |
| Douglas Gregor | 5a2bb5b | 2010-10-13 22:55:32 +0000 | [diff] [blame] | 5986 | return AddedAny; |
| Sebastian Redl | d5b2453 | 2009-11-18 21:51:29 +0000 | [diff] [blame] | 5987 | } |
| 5988 | |
| Kaelyn Uhrain | 0a32fca | 2011-10-11 00:28:39 +0000 | [diff] [blame] | 5989 | namespace { |
| 5990 | // Struct for holding all of the extra arguments needed by |
| 5991 | // DiagnoseInvalidRedeclaration to call Sema::ActOnFunctionDeclarator. |
| 5992 | struct ActOnFDArgs { |
| 5993 | Scope *S; |
| 5994 | Declarator &D; |
| Kaelyn Uhrain | 0a32fca | 2011-10-11 00:28:39 +0000 | [diff] [blame] | 5995 | MultiTemplateParamsArg TemplateParamLists; |
| Kaelyn Uhrain | 0a32fca | 2011-10-11 00:28:39 +0000 | [diff] [blame] | 5996 | bool AddToScope; |
| 5997 | }; |
| 5998 | } |
| 5999 | |
| Kaelyn Uhrain | b137840 | 2012-01-18 21:41:41 +0000 | [diff] [blame] | 6000 | namespace { |
| 6001 | |
| 6002 | // Callback to only accept typo corrections that have a non-zero edit distance. |
| Kaelyn Uhrain | 7c01917 | 2012-02-16 22:40:59 +0000 | [diff] [blame] | 6003 | // Also only accept corrections that have the same parent decl. |
| Kaelyn Uhrain | b137840 | 2012-01-18 21:41:41 +0000 | [diff] [blame] | 6004 | class DifferentNameValidatorCCC : public CorrectionCandidateCallback { |
| 6005 | public: |
| Kaelyn Uhrain | 389e9c2 | 2012-06-07 23:57:08 +0000 | [diff] [blame] | 6006 | DifferentNameValidatorCCC(ASTContext &Context, FunctionDecl *TypoFD, |
| 6007 | CXXRecordDecl *Parent) |
| 6008 | : Context(Context), OriginalFD(TypoFD), |
| 6009 | ExpectedParent(Parent ? Parent->getCanonicalDecl() : 0) {} |
| Kaelyn Uhrain | 7c01917 | 2012-02-16 22:40:59 +0000 | [diff] [blame] | 6010 | |
| Kaelyn Uhrain | b137840 | 2012-01-18 21:41:41 +0000 | [diff] [blame] | 6011 | virtual bool ValidateCandidate(const TypoCorrection &candidate) { |
| Kaelyn Uhrain | 7c01917 | 2012-02-16 22:40:59 +0000 | [diff] [blame] | 6012 | if (candidate.getEditDistance() == 0) |
| 6013 | return false; |
| 6014 | |
| Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 6015 | SmallVector<unsigned, 1> MismatchedParams; |
| Kaelyn Uhrain | 389e9c2 | 2012-06-07 23:57:08 +0000 | [diff] [blame] | 6016 | for (TypoCorrection::const_decl_iterator CDecl = candidate.begin(), |
| 6017 | CDeclEnd = candidate.end(); |
| 6018 | CDecl != CDeclEnd; ++CDecl) { |
| 6019 | FunctionDecl *FD = dyn_cast<FunctionDecl>(*CDecl); |
| 6020 | |
| 6021 | if (FD && !FD->hasBody() && |
| 6022 | hasSimilarParameters(Context, FD, OriginalFD, MismatchedParams)) { |
| 6023 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) { |
| 6024 | CXXRecordDecl *Parent = MD->getParent(); |
| 6025 | if (Parent && Parent->getCanonicalDecl() == ExpectedParent) |
| 6026 | return true; |
| 6027 | } else if (!ExpectedParent) { |
| 6028 | return true; |
| 6029 | } |
| 6030 | } |
| Kaelyn Uhrain | 7c01917 | 2012-02-16 22:40:59 +0000 | [diff] [blame] | 6031 | } |
| 6032 | |
| Kaelyn Uhrain | 389e9c2 | 2012-06-07 23:57:08 +0000 | [diff] [blame] | 6033 | return false; |
| Kaelyn Uhrain | b137840 | 2012-01-18 21:41:41 +0000 | [diff] [blame] | 6034 | } |
| Kaelyn Uhrain | 7c01917 | 2012-02-16 22:40:59 +0000 | [diff] [blame] | 6035 | |
| 6036 | private: |
| Kaelyn Uhrain | 389e9c2 | 2012-06-07 23:57:08 +0000 | [diff] [blame] | 6037 | ASTContext &Context; |
| 6038 | FunctionDecl *OriginalFD; |
| Kaelyn Uhrain | 7c01917 | 2012-02-16 22:40:59 +0000 | [diff] [blame] | 6039 | CXXRecordDecl *ExpectedParent; |
| Kaelyn Uhrain | b137840 | 2012-01-18 21:41:41 +0000 | [diff] [blame] | 6040 | }; |
| 6041 | |
| 6042 | } |
| 6043 | |
| Kaelyn Uhrain | 0a32fca | 2011-10-11 00:28:39 +0000 | [diff] [blame] | 6044 | /// \brief Generate diagnostics for an invalid function redeclaration. |
| 6045 | /// |
| 6046 | /// This routine handles generating the diagnostic messages for an invalid |
| 6047 | /// function redeclaration, including finding possible similar declarations |
| 6048 | /// or performing typo correction if there are no previous declarations with |
| 6049 | /// the same name. |
| 6050 | /// |
| Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 6051 | /// Returns a NamedDecl iff typo correction was performed and substituting in |
| Kaelyn Uhrain | 0a32fca | 2011-10-11 00:28:39 +0000 | [diff] [blame] | 6052 | /// the new declaration name does not cause new errors. |
| Richard Smith | 114394f | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 6053 | static NamedDecl *DiagnoseInvalidRedeclaration( |
| Kaelyn Uhrain | 8af2c9f | 2011-10-11 00:28:52 +0000 | [diff] [blame] | 6054 | Sema &SemaRef, LookupResult &Previous, FunctionDecl *NewFD, |
| Richard Smith | 114394f | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 6055 | ActOnFDArgs &ExtraArgs, bool IsLocalFriend, Scope *S) { |
| Kaelyn Uhrain | fd81a35 | 2011-08-18 18:19:12 +0000 | [diff] [blame] | 6056 | DeclarationName Name = NewFD->getDeclName(); |
| Kaelyn Uhrain | 0a32fca | 2011-10-11 00:28:39 +0000 | [diff] [blame] | 6057 | DeclContext *NewDC = NewFD->getDeclContext(); |
| Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 6058 | SmallVector<unsigned, 1> MismatchedParams; |
| 6059 | SmallVector<std::pair<FunctionDecl *, unsigned>, 1> NearMatches; |
| Kaelyn Uhrain | fd81a35 | 2011-08-18 18:19:12 +0000 | [diff] [blame] | 6060 | TypoCorrection Correction; |
| Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 6061 | bool IsDefinition = ExtraArgs.D.isFunctionDefinition(); |
| Richard Smith | 114394f | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 6062 | unsigned DiagMsg = IsLocalFriend ? diag::err_no_matching_local_friend |
| 6063 | : diag::err_member_decl_does_not_match; |
| 6064 | LookupResult Prev(SemaRef, Name, NewFD->getLocation(), |
| 6065 | IsLocalFriend ? Sema::LookupLocalFriendName |
| 6066 | : Sema::LookupOrdinaryName, |
| 6067 | Sema::ForRedeclaration); |
| Kaelyn Uhrain | fd81a35 | 2011-08-18 18:19:12 +0000 | [diff] [blame] | 6068 | |
| 6069 | NewFD->setInvalidDecl(); |
| Richard Smith | 114394f | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 6070 | if (IsLocalFriend) |
| 6071 | SemaRef.LookupName(Prev, S); |
| 6072 | else |
| 6073 | SemaRef.LookupQualifiedName(Prev, NewDC); |
| John McCall | f7cfb22 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 6074 | assert(!Prev.isAmbiguous() && |
| 6075 | "Cannot have an ambiguity in previous-declaration lookup"); |
| Kaelyn Uhrain | 7c01917 | 2012-02-16 22:40:59 +0000 | [diff] [blame] | 6076 | CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(NewFD); |
| Kaelyn Uhrain | 389e9c2 | 2012-06-07 23:57:08 +0000 | [diff] [blame] | 6077 | DifferentNameValidatorCCC Validator(SemaRef.Context, NewFD, |
| 6078 | MD ? MD->getParent() : 0); |
| Kaelyn Uhrain | fd81a35 | 2011-08-18 18:19:12 +0000 | [diff] [blame] | 6079 | if (!Prev.empty()) { |
| 6080 | for (LookupResult::iterator Func = Prev.begin(), FuncEnd = Prev.end(); |
| 6081 | Func != FuncEnd; ++Func) { |
| 6082 | FunctionDecl *FD = dyn_cast<FunctionDecl>(*Func); |
| Kaelyn Uhrain | 8af2c9f | 2011-10-11 00:28:52 +0000 | [diff] [blame] | 6083 | if (FD && |
| 6084 | hasSimilarParameters(SemaRef.Context, FD, NewFD, MismatchedParams)) { |
| Kaelyn Uhrain | fd81a35 | 2011-08-18 18:19:12 +0000 | [diff] [blame] | 6085 | // Add 1 to the index so that 0 can mean the mismatch didn't |
| 6086 | // involve a parameter |
| 6087 | unsigned ParamNum = |
| 6088 | MismatchedParams.empty() ? 0 : MismatchedParams.front() + 1; |
| 6089 | NearMatches.push_back(std::make_pair(FD, ParamNum)); |
| 6090 | } |
| Kaelyn Uhrain | 7d9bc63 | 2011-08-04 17:40:00 +0000 | [diff] [blame] | 6091 | } |
| Kaelyn Uhrain | fd81a35 | 2011-08-18 18:19:12 +0000 | [diff] [blame] | 6092 | // If the qualified name lookup yielded nothing, try typo correction |
| Richard Smith | 114394f | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 6093 | } else if ((Correction = SemaRef.CorrectTypo( |
| Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 6094 | Prev.getLookupNameInfo(), Prev.getLookupKind(), S, |
| 6095 | &ExtraArgs.D.getCXXScopeSpec(), Validator, |
| 6096 | IsLocalFriend ? 0 : NewDC))) { |
| Kaelyn Uhrain | 0a32fca | 2011-10-11 00:28:39 +0000 | [diff] [blame] | 6097 | // Set up everything for the call to ActOnFunctionDeclarator |
| 6098 | ExtraArgs.D.SetIdentifier(Correction.getCorrectionAsIdentifierInfo(), |
| 6099 | ExtraArgs.D.getIdentifierLoc()); |
| 6100 | Previous.clear(); |
| 6101 | Previous.setLookupName(Correction.getCorrection()); |
| Kaelyn Uhrain | fd81a35 | 2011-08-18 18:19:12 +0000 | [diff] [blame] | 6102 | for (TypoCorrection::decl_iterator CDecl = Correction.begin(), |
| 6103 | CDeclEnd = Correction.end(); |
| 6104 | CDecl != CDeclEnd; ++CDecl) { |
| 6105 | FunctionDecl *FD = dyn_cast<FunctionDecl>(*CDecl); |
| Kaelyn Uhrain | 389e9c2 | 2012-06-07 23:57:08 +0000 | [diff] [blame] | 6106 | if (FD && !FD->hasBody() && |
| 6107 | hasSimilarParameters(SemaRef.Context, FD, NewFD, MismatchedParams)) { |
| Kaelyn Uhrain | 0a32fca | 2011-10-11 00:28:39 +0000 | [diff] [blame] | 6108 | Previous.addDecl(FD); |
| Kaelyn Uhrain | fd81a35 | 2011-08-18 18:19:12 +0000 | [diff] [blame] | 6109 | } |
| 6110 | } |
| Kaelyn Uhrain | 4dc695d | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 6111 | bool wasRedeclaration = ExtraArgs.D.isRedeclaration(); |
| Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 6112 | |
| 6113 | NamedDecl *Result; |
| 6114 | // Retry building the function declaration with the new previous |
| 6115 | // declarations, and with errors suppressed. |
| 6116 | { |
| 6117 | // Trap errors. |
| 6118 | Sema::SFINAETrap Trap(SemaRef); |
| 6119 | |
| 6120 | // TODO: Refactor ActOnFunctionDeclarator so that we can call only the |
| 6121 | // pieces need to verify the typo-corrected C++ declaration and hopefully |
| 6122 | // eliminate the need for the parameter pack ExtraArgs. |
| 6123 | Result = SemaRef.ActOnFunctionDeclarator( |
| 6124 | ExtraArgs.S, ExtraArgs.D, |
| 6125 | Correction.getCorrectionDecl()->getDeclContext(), |
| 6126 | NewFD->getTypeSourceInfo(), Previous, ExtraArgs.TemplateParamLists, |
| 6127 | ExtraArgs.AddToScope); |
| 6128 | |
| 6129 | if (Trap.hasErrorOccurred()) |
| 6130 | Result = 0; |
| Kaelyn Uhrain | 0a32fca | 2011-10-11 00:28:39 +0000 | [diff] [blame] | 6131 | } |
| Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 6132 | |
| 6133 | if (Result) { |
| 6134 | // Determine which correction we picked. |
| 6135 | Decl *Canonical = Result->getCanonicalDecl(); |
| 6136 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 6137 | I != E; ++I) |
| 6138 | if ((*I)->getCanonicalDecl() == Canonical) |
| 6139 | Correction.setCorrectionDecl(*I); |
| 6140 | |
| 6141 | SemaRef.diagnoseTypo( |
| 6142 | Correction, |
| 6143 | SemaRef.PDiag(IsLocalFriend |
| 6144 | ? diag::err_no_matching_local_friend_suggest |
| 6145 | : diag::err_member_decl_does_not_match_suggest) |
| 6146 | << Name << NewDC << IsDefinition); |
| 6147 | return Result; |
| Kaelyn Uhrain | 0a32fca | 2011-10-11 00:28:39 +0000 | [diff] [blame] | 6148 | } |
| Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 6149 | |
| 6150 | // Pretend the typo correction never occurred |
| 6151 | ExtraArgs.D.SetIdentifier(Name.getAsIdentifierInfo(), |
| 6152 | ExtraArgs.D.getIdentifierLoc()); |
| 6153 | ExtraArgs.D.setRedeclaration(wasRedeclaration); |
| 6154 | Previous.clear(); |
| 6155 | Previous.setLookupName(Name); |
| Kaelyn Uhrain | fd81a35 | 2011-08-18 18:19:12 +0000 | [diff] [blame] | 6156 | } |
| 6157 | |
| Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 6158 | SemaRef.Diag(NewFD->getLocation(), DiagMsg) |
| 6159 | << Name << NewDC << IsDefinition << NewFD->getLocation(); |
| Kaelyn Uhrain | fd81a35 | 2011-08-18 18:19:12 +0000 | [diff] [blame] | 6160 | |
| Kaelyn Uhrain | 1a6eb99 | 2011-10-10 18:01:37 +0000 | [diff] [blame] | 6161 | bool NewFDisConst = false; |
| 6162 | if (CXXMethodDecl *NewMD = dyn_cast<CXXMethodDecl>(NewFD)) |
| David Blaikie | f5697e5 | 2012-08-10 00:55:35 +0000 | [diff] [blame] | 6163 | NewFDisConst = NewMD->isConst(); |
| Kaelyn Uhrain | 1a6eb99 | 2011-10-10 18:01:37 +0000 | [diff] [blame] | 6164 | |
| Craig Topper | af6bd1b | 2013-07-04 03:15:42 +0000 | [diff] [blame] | 6165 | for (SmallVectorImpl<std::pair<FunctionDecl *, unsigned> >::iterator |
| Kaelyn Uhrain | fd81a35 | 2011-08-18 18:19:12 +0000 | [diff] [blame] | 6166 | NearMatch = NearMatches.begin(), NearMatchEnd = NearMatches.end(); |
| 6167 | NearMatch != NearMatchEnd; ++NearMatch) { |
| 6168 | FunctionDecl *FD = NearMatch->first; |
| Richard Smith | 114394f | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 6169 | CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD); |
| 6170 | bool FDisConst = MD && MD->isConst(); |
| 6171 | bool IsMember = MD || !IsLocalFriend; |
| Kaelyn Uhrain | fd81a35 | 2011-08-18 18:19:12 +0000 | [diff] [blame] | 6172 | |
| Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 6173 | // FIXME: These notes are poorly worded for the local friend case. |
| Kaelyn Uhrain | fd81a35 | 2011-08-18 18:19:12 +0000 | [diff] [blame] | 6174 | if (unsigned Idx = NearMatch->second) { |
| 6175 | ParmVarDecl *FDParam = FD->getParamDecl(Idx-1); |
| Richard Smith | cf8ec8d | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 6176 | SourceLocation Loc = FDParam->getTypeSpecStartLoc(); |
| 6177 | if (Loc.isInvalid()) Loc = FD->getLocation(); |
| Richard Smith | 114394f | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 6178 | SemaRef.Diag(Loc, IsMember ? diag::note_member_def_close_param_match |
| 6179 | : diag::note_local_decl_close_param_match) |
| 6180 | << Idx << FDParam->getType() |
| 6181 | << NewFD->getParamDecl(Idx - 1)->getType(); |
| Kaelyn Uhrain | 1a6eb99 | 2011-10-10 18:01:37 +0000 | [diff] [blame] | 6182 | } else if (FDisConst != NewFDisConst) { |
| Kaelyn Uhrain | 8af2c9f | 2011-10-11 00:28:52 +0000 | [diff] [blame] | 6183 | SemaRef.Diag(FD->getLocation(), diag::note_member_def_close_const_match) |
| Kaelyn Uhrain | 1a6eb99 | 2011-10-10 18:01:37 +0000 | [diff] [blame] | 6184 | << NewFDisConst << FD->getSourceRange().getEnd(); |
| Kaelyn Uhrain | fd81a35 | 2011-08-18 18:19:12 +0000 | [diff] [blame] | 6185 | } else |
| Richard Smith | 114394f | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 6186 | SemaRef.Diag(FD->getLocation(), |
| 6187 | IsMember ? diag::note_member_def_close_match |
| 6188 | : diag::note_local_decl_close_match); |
| John McCall | f7cfb22 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 6189 | } |
| Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 6190 | return 0; |
| John McCall | f7cfb22 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 6191 | } |
| 6192 | |
| David Blaikie | 30d1544 | 2011-10-19 22:56:21 +0000 | [diff] [blame] | 6193 | static FunctionDecl::StorageClass getFunctionStorageClass(Sema &SemaRef, |
| 6194 | Declarator &D) { |
| Kaelyn Uhrain | e1a9ff7 | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6195 | switch (D.getDeclSpec().getStorageClassSpec()) { |
| 6196 | default: llvm_unreachable("Unknown storage class!"); |
| 6197 | case DeclSpec::SCS_auto: |
| 6198 | case DeclSpec::SCS_register: |
| 6199 | case DeclSpec::SCS_mutable: |
| 6200 | SemaRef.Diag(D.getDeclSpec().getStorageClassSpecLoc(), |
| 6201 | diag::err_typecheck_sclass_func); |
| 6202 | D.setInvalidType(); |
| 6203 | break; |
| 6204 | case DeclSpec::SCS_unspecified: break; |
| Rafael Espindola | bff5956 | 2013-04-25 12:11:36 +0000 | [diff] [blame] | 6205 | case DeclSpec::SCS_extern: |
| 6206 | if (D.getDeclSpec().isExternInLinkageSpec()) |
| 6207 | return SC_None; |
| 6208 | return SC_Extern; |
| Kaelyn Uhrain | e1a9ff7 | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6209 | case DeclSpec::SCS_static: { |
| 6210 | if (SemaRef.CurContext->getRedeclContext()->isFunctionOrMethod()) { |
| 6211 | // C99 6.7.1p5: |
| 6212 | // The declaration of an identifier for a function that has |
| 6213 | // block scope shall have no explicit storage-class specifier |
| 6214 | // other than extern |
| 6215 | // See also (C++ [dcl.stc]p4). |
| 6216 | SemaRef.Diag(D.getDeclSpec().getStorageClassSpecLoc(), |
| 6217 | diag::err_static_block_func); |
| 6218 | break; |
| 6219 | } else |
| 6220 | return SC_Static; |
| 6221 | } |
| 6222 | case DeclSpec::SCS_private_extern: return SC_PrivateExtern; |
| 6223 | } |
| 6224 | |
| 6225 | // No explicit storage class has already been returned |
| 6226 | return SC_None; |
| 6227 | } |
| 6228 | |
| 6229 | static FunctionDecl* CreateNewFunctionDecl(Sema &SemaRef, Declarator &D, |
| 6230 | DeclContext *DC, QualType &R, |
| 6231 | TypeSourceInfo *TInfo, |
| 6232 | FunctionDecl::StorageClass SC, |
| 6233 | bool &IsVirtualOkay) { |
| 6234 | DeclarationNameInfo NameInfo = SemaRef.GetNameForDeclarator(D); |
| 6235 | DeclarationName Name = NameInfo.getName(); |
| 6236 | |
| 6237 | FunctionDecl *NewFD = 0; |
| 6238 | bool isInline = D.getDeclSpec().isInlineSpecified(); |
| Kaelyn Uhrain | e1a9ff7 | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6239 | |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6240 | if (!SemaRef.getLangOpts().CPlusPlus) { |
| Kaelyn Uhrain | e1a9ff7 | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6241 | // Determine whether the function was written with a |
| 6242 | // prototype. This true when: |
| 6243 | // - there is a prototype in the declarator, or |
| 6244 | // - the type R of the function is some kind of typedef or other reference |
| 6245 | // to a type name (which eventually refers to a function type). |
| 6246 | bool HasPrototype = |
| 6247 | (D.isFunctionDeclarator() && D.getFunctionTypeInfo().hasPrototype) || |
| 6248 | (!isa<FunctionType>(R.getTypePtr()) && R->isFunctionProtoType()); |
| 6249 | |
| David Blaikie | 30d1544 | 2011-10-19 22:56:21 +0000 | [diff] [blame] | 6250 | NewFD = FunctionDecl::Create(SemaRef.Context, DC, |
| Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 6251 | D.getLocStart(), NameInfo, R, |
| Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 6252 | TInfo, SC, isInline, |
| 6253 | HasPrototype, false); |
| Kaelyn Uhrain | e1a9ff7 | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6254 | if (D.isInvalidType()) |
| 6255 | NewFD->setInvalidDecl(); |
| 6256 | |
| 6257 | // Set the lexical context. |
| 6258 | NewFD->setLexicalDeclContext(SemaRef.CurContext); |
| 6259 | |
| 6260 | return NewFD; |
| 6261 | } |
| 6262 | |
| 6263 | bool isExplicit = D.getDeclSpec().isExplicitSpecified(); |
| 6264 | bool isConstexpr = D.getDeclSpec().isConstexprSpecified(); |
| 6265 | |
| 6266 | // Check that the return type is not an abstract class type. |
| 6267 | // For record types, this is done by the AbstractClassUsageDiagnoser once |
| 6268 | // the class has been completely parsed. |
| 6269 | if (!DC->isRecord() && |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 6270 | SemaRef.RequireNonAbstractType( |
| 6271 | D.getIdentifierLoc(), R->getAs<FunctionType>()->getReturnType(), |
| 6272 | diag::err_abstract_type_in_decl, SemaRef.AbstractReturnType)) |
| Kaelyn Uhrain | e1a9ff7 | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6273 | D.setInvalidType(); |
| 6274 | |
| 6275 | if (Name.getNameKind() == DeclarationName::CXXConstructorName) { |
| 6276 | // This is a C++ constructor declaration. |
| 6277 | assert(DC->isRecord() && |
| 6278 | "Constructors can only be declared in a member context"); |
| 6279 | |
| 6280 | R = SemaRef.CheckConstructorDeclarator(D, R, SC); |
| 6281 | return CXXConstructorDecl::Create(SemaRef.Context, cast<CXXRecordDecl>(DC), |
| Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 6282 | D.getLocStart(), NameInfo, |
| Kaelyn Uhrain | e1a9ff7 | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6283 | R, TInfo, isExplicit, isInline, |
| 6284 | /*isImplicitlyDeclared=*/false, |
| 6285 | isConstexpr); |
| 6286 | |
| 6287 | } else if (Name.getNameKind() == DeclarationName::CXXDestructorName) { |
| 6288 | // This is a C++ destructor declaration. |
| 6289 | if (DC->isRecord()) { |
| 6290 | R = SemaRef.CheckDestructorDeclarator(D, R, SC); |
| 6291 | CXXRecordDecl *Record = cast<CXXRecordDecl>(DC); |
| 6292 | CXXDestructorDecl *NewDD = CXXDestructorDecl::Create( |
| 6293 | SemaRef.Context, Record, |
| Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 6294 | D.getLocStart(), |
| Kaelyn Uhrain | e1a9ff7 | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6295 | NameInfo, R, TInfo, isInline, |
| 6296 | /*isImplicitlyDeclared=*/false); |
| 6297 | |
| 6298 | // If the class is complete, then we now create the implicit exception |
| 6299 | // specification. If the class is incomplete or dependent, we can't do |
| 6300 | // it yet. |
| Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6301 | if (SemaRef.getLangOpts().CPlusPlus11 && !Record->isDependentType() && |
| Kaelyn Uhrain | e1a9ff7 | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6302 | Record->getDefinition() && !Record->isBeingDefined() && |
| 6303 | R->getAs<FunctionProtoType>()->getExceptionSpecType() == EST_None) { |
| 6304 | SemaRef.AdjustDestructorExceptionSpec(Record, NewDD); |
| 6305 | } |
| 6306 | |
| 6307 | IsVirtualOkay = true; |
| 6308 | return NewDD; |
| 6309 | |
| 6310 | } else { |
| 6311 | SemaRef.Diag(D.getIdentifierLoc(), diag::err_destructor_not_member); |
| 6312 | D.setInvalidType(); |
| 6313 | |
| 6314 | // Create a FunctionDecl to satisfy the function definition parsing |
| 6315 | // code path. |
| 6316 | return FunctionDecl::Create(SemaRef.Context, DC, |
| Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 6317 | D.getLocStart(), |
| Kaelyn Uhrain | e1a9ff7 | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6318 | D.getIdentifierLoc(), Name, R, TInfo, |
| Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 6319 | SC, isInline, |
| Kaelyn Uhrain | e1a9ff7 | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6320 | /*hasPrototype=*/true, isConstexpr); |
| 6321 | } |
| 6322 | |
| 6323 | } else if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName) { |
| 6324 | if (!DC->isRecord()) { |
| 6325 | SemaRef.Diag(D.getIdentifierLoc(), |
| 6326 | diag::err_conv_function_not_member); |
| 6327 | return 0; |
| 6328 | } |
| 6329 | |
| 6330 | SemaRef.CheckConversionDeclarator(D, R, SC); |
| 6331 | IsVirtualOkay = true; |
| 6332 | return CXXConversionDecl::Create(SemaRef.Context, cast<CXXRecordDecl>(DC), |
| Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 6333 | D.getLocStart(), NameInfo, |
| Kaelyn Uhrain | e1a9ff7 | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6334 | R, TInfo, isInline, isExplicit, |
| 6335 | isConstexpr, SourceLocation()); |
| 6336 | |
| 6337 | } else if (DC->isRecord()) { |
| 6338 | // If the name of the function is the same as the name of the record, |
| 6339 | // then this must be an invalid constructor that has a return type. |
| 6340 | // (The parser checks for a return type and makes the declarator a |
| 6341 | // constructor if it has no return type). |
| 6342 | if (Name.getAsIdentifierInfo() && |
| 6343 | Name.getAsIdentifierInfo() == cast<CXXRecordDecl>(DC)->getIdentifier()){ |
| 6344 | SemaRef.Diag(D.getIdentifierLoc(), diag::err_constructor_return_type) |
| 6345 | << SourceRange(D.getDeclSpec().getTypeSpecTypeLoc()) |
| 6346 | << SourceRange(D.getIdentifierLoc()); |
| 6347 | return 0; |
| 6348 | } |
| 6349 | |
| Kaelyn Uhrain | e1a9ff7 | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6350 | // This is a C++ method declaration. |
| Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 6351 | CXXMethodDecl *Ret = CXXMethodDecl::Create(SemaRef.Context, |
| 6352 | cast<CXXRecordDecl>(DC), |
| 6353 | D.getLocStart(), NameInfo, R, |
| 6354 | TInfo, SC, isInline, |
| 6355 | isConstexpr, SourceLocation()); |
| 6356 | IsVirtualOkay = !Ret->isStatic(); |
| 6357 | return Ret; |
| Kaelyn Uhrain | e1a9ff7 | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6358 | } else { |
| 6359 | // Determine whether the function was written with a |
| 6360 | // prototype. This true when: |
| 6361 | // - we're in C++ (where every function has a prototype), |
| 6362 | return FunctionDecl::Create(SemaRef.Context, DC, |
| Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 6363 | D.getLocStart(), |
| Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 6364 | NameInfo, R, TInfo, SC, isInline, |
| Kaelyn Uhrain | e1a9ff7 | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6365 | true/*HasPrototype*/, isConstexpr); |
| 6366 | } |
| 6367 | } |
| 6368 | |
| Matt Arsenault | efb3819 | 2013-07-23 01:23:36 +0000 | [diff] [blame] | 6369 | enum OpenCLParamType { |
| 6370 | ValidKernelParam, |
| 6371 | PtrPtrKernelParam, |
| 6372 | PtrKernelParam, |
| 6373 | InvalidKernelParam, |
| 6374 | RecordKernelParam |
| 6375 | }; |
| 6376 | |
| 6377 | static OpenCLParamType getOpenCLKernelParameterType(QualType PT) { |
| 6378 | if (PT->isPointerType()) { |
| 6379 | QualType PointeeType = PT->getPointeeType(); |
| 6380 | return PointeeType->isPointerType() ? PtrPtrKernelParam : PtrKernelParam; |
| 6381 | } |
| 6382 | |
| 6383 | // TODO: Forbid the other integer types (size_t, ptrdiff_t...) when they can |
| 6384 | // be used as builtin types. |
| 6385 | |
| 6386 | if (PT->isImageType()) |
| 6387 | return PtrKernelParam; |
| 6388 | |
| 6389 | if (PT->isBooleanType()) |
| 6390 | return InvalidKernelParam; |
| 6391 | |
| 6392 | if (PT->isEventT()) |
| 6393 | return InvalidKernelParam; |
| 6394 | |
| 6395 | if (PT->isHalfType()) |
| 6396 | return InvalidKernelParam; |
| 6397 | |
| 6398 | if (PT->isRecordType()) |
| 6399 | return RecordKernelParam; |
| 6400 | |
| 6401 | return ValidKernelParam; |
| 6402 | } |
| 6403 | |
| 6404 | static void checkIsValidOpenCLKernelParameter( |
| 6405 | Sema &S, |
| 6406 | Declarator &D, |
| 6407 | ParmVarDecl *Param, |
| 6408 | llvm::SmallPtrSet<const Type *, 16> &ValidTypes) { |
| 6409 | QualType PT = Param->getType(); |
| 6410 | |
| 6411 | // Cache the valid types we encounter to avoid rechecking structs that are |
| 6412 | // used again |
| 6413 | if (ValidTypes.count(PT.getTypePtr())) |
| 6414 | return; |
| 6415 | |
| 6416 | switch (getOpenCLKernelParameterType(PT)) { |
| 6417 | case PtrPtrKernelParam: |
| 6418 | // OpenCL v1.2 s6.9.a: |
| 6419 | // A kernel function argument cannot be declared as a |
| 6420 | // pointer to a pointer type. |
| 6421 | S.Diag(Param->getLocation(), diag::err_opencl_ptrptr_kernel_param); |
| 6422 | D.setInvalidType(); |
| 6423 | return; |
| 6424 | |
| 6425 | // OpenCL v1.2 s6.9.k: |
| 6426 | // Arguments to kernel functions in a program cannot be declared with the |
| 6427 | // built-in scalar types bool, half, size_t, ptrdiff_t, intptr_t, and |
| 6428 | // uintptr_t or a struct and/or union that contain fields declared to be |
| 6429 | // one of these built-in scalar types. |
| 6430 | |
| 6431 | case InvalidKernelParam: |
| 6432 | // OpenCL v1.2 s6.8 n: |
| 6433 | // A kernel function argument cannot be declared |
| 6434 | // of event_t type. |
| 6435 | S.Diag(Param->getLocation(), diag::err_bad_kernel_param_type) << PT; |
| 6436 | D.setInvalidType(); |
| 6437 | return; |
| 6438 | |
| 6439 | case PtrKernelParam: |
| 6440 | case ValidKernelParam: |
| 6441 | ValidTypes.insert(PT.getTypePtr()); |
| 6442 | return; |
| 6443 | |
| 6444 | case RecordKernelParam: |
| 6445 | break; |
| 6446 | } |
| 6447 | |
| 6448 | // Track nested structs we will inspect |
| 6449 | SmallVector<const Decl *, 4> VisitStack; |
| 6450 | |
| 6451 | // Track where we are in the nested structs. Items will migrate from |
| 6452 | // VisitStack to HistoryStack as we do the DFS for bad field. |
| 6453 | SmallVector<const FieldDecl *, 4> HistoryStack; |
| 6454 | HistoryStack.push_back((const FieldDecl *) 0); |
| 6455 | |
| 6456 | const RecordDecl *PD = PT->castAs<RecordType>()->getDecl(); |
| 6457 | VisitStack.push_back(PD); |
| 6458 | |
| 6459 | assert(VisitStack.back() && "First decl null?"); |
| 6460 | |
| 6461 | do { |
| 6462 | const Decl *Next = VisitStack.pop_back_val(); |
| 6463 | if (!Next) { |
| 6464 | assert(!HistoryStack.empty()); |
| 6465 | // Found a marker, we have gone up a level |
| 6466 | if (const FieldDecl *Hist = HistoryStack.pop_back_val()) |
| 6467 | ValidTypes.insert(Hist->getType().getTypePtr()); |
| 6468 | |
| 6469 | continue; |
| 6470 | } |
| 6471 | |
| 6472 | // Adds everything except the original parameter declaration (which is not a |
| 6473 | // field itself) to the history stack. |
| 6474 | const RecordDecl *RD; |
| 6475 | if (const FieldDecl *Field = dyn_cast<FieldDecl>(Next)) { |
| 6476 | HistoryStack.push_back(Field); |
| 6477 | RD = Field->getType()->castAs<RecordType>()->getDecl(); |
| 6478 | } else { |
| 6479 | RD = cast<RecordDecl>(Next); |
| 6480 | } |
| 6481 | |
| 6482 | // Add a null marker so we know when we've gone back up a level |
| 6483 | VisitStack.push_back((const Decl *) 0); |
| 6484 | |
| 6485 | for (RecordDecl::field_iterator I = RD->field_begin(), |
| 6486 | E = RD->field_end(); I != E; ++I) { |
| 6487 | const FieldDecl *FD = *I; |
| 6488 | QualType QT = FD->getType(); |
| 6489 | |
| 6490 | if (ValidTypes.count(QT.getTypePtr())) |
| 6491 | continue; |
| 6492 | |
| 6493 | OpenCLParamType ParamType = getOpenCLKernelParameterType(QT); |
| 6494 | if (ParamType == ValidKernelParam) |
| 6495 | continue; |
| 6496 | |
| 6497 | if (ParamType == RecordKernelParam) { |
| 6498 | VisitStack.push_back(FD); |
| 6499 | continue; |
| 6500 | } |
| 6501 | |
| 6502 | // OpenCL v1.2 s6.9.p: |
| 6503 | // Arguments to kernel functions that are declared to be a struct or union |
| 6504 | // do not allow OpenCL objects to be passed as elements of the struct or |
| 6505 | // union. |
| 6506 | if (ParamType == PtrKernelParam || ParamType == PtrPtrKernelParam) { |
| 6507 | S.Diag(Param->getLocation(), |
| 6508 | diag::err_record_with_pointers_kernel_param) |
| 6509 | << PT->isUnionType() |
| 6510 | << PT; |
| 6511 | } else { |
| 6512 | S.Diag(Param->getLocation(), diag::err_bad_kernel_param_type) << PT; |
| 6513 | } |
| 6514 | |
| 6515 | S.Diag(PD->getLocation(), diag::note_within_field_of_type) |
| 6516 | << PD->getDeclName(); |
| 6517 | |
| 6518 | // We have an error, now let's go back up through history and show where |
| 6519 | // the offending field came from |
| 6520 | for (ArrayRef<const FieldDecl *>::const_iterator I = HistoryStack.begin() + 1, |
| 6521 | E = HistoryStack.end(); I != E; ++I) { |
| 6522 | const FieldDecl *OuterField = *I; |
| 6523 | S.Diag(OuterField->getLocation(), diag::note_within_field_of_type) |
| 6524 | << OuterField->getType(); |
| 6525 | } |
| 6526 | |
| 6527 | S.Diag(FD->getLocation(), diag::note_illegal_field_declared_here) |
| 6528 | << QT->isPointerType() |
| 6529 | << QT; |
| 6530 | D.setInvalidType(); |
| 6531 | return; |
| 6532 | } |
| 6533 | } while (!VisitStack.empty()); |
| 6534 | } |
| 6535 | |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6536 | NamedDecl* |
| Nick Lewycky | 0bdf13e | 2011-07-02 02:05:12 +0000 | [diff] [blame] | 6537 | Sema::ActOnFunctionDeclarator(Scope *S, Declarator &D, DeclContext *DC, |
| Kaelyn Uhrain | 4dc695d | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 6538 | TypeSourceInfo *TInfo, LookupResult &Previous, |
| Douglas Gregor | b52fabb | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 6539 | MultiTemplateParamsArg TemplateParamLists, |
| Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 6540 | bool &AddToScope) { |
| Kaelyn Uhrain | 4dc695d | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 6541 | QualType R = TInfo->getType(); |
| 6542 | |
| Zhongxing Xu | bece5d6 | 2009-01-16 01:13:29 +0000 | [diff] [blame] | 6543 | assert(R.getTypePtr()->isFunctionType()); |
| 6544 | |
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 6545 | // TODO: consider using NameInfo for diagnostic. |
| 6546 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); |
| 6547 | DeclarationName Name = NameInfo.getName(); |
| Kaelyn Uhrain | e1a9ff7 | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6548 | FunctionDecl::StorageClass SC = getFunctionStorageClass(*this, D); |
| Zhongxing Xu | bece5d6 | 2009-01-16 01:13:29 +0000 | [diff] [blame] | 6549 | |
| Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 6550 | if (DeclSpec::TSCS TSCS = D.getDeclSpec().getThreadStorageClassSpec()) |
| 6551 | Diag(D.getDeclSpec().getThreadStorageClassSpecLoc(), |
| 6552 | diag::err_invalid_thread) |
| 6553 | << DeclSpec::getSpecifierName(TSCS); |
| Eli Friedman | d5c0eed | 2009-04-19 20:27:55 +0000 | [diff] [blame] | 6554 | |
| Reid Kleckner | 9a7f3e6 | 2013-10-08 00:58:57 +0000 | [diff] [blame] | 6555 | if (D.isFirstDeclarationOfMember()) |
| 6556 | adjustMemberFunctionCC(R, D.isStaticMember()); |
| Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 6557 | |
| Douglas Gregor | 513e63c | 2010-12-10 19:28:19 +0000 | [diff] [blame] | 6558 | bool isFriend = false; |
| Douglas Gregor | 513e63c | 2010-12-10 19:28:19 +0000 | [diff] [blame] | 6559 | FunctionTemplateDecl *FunctionTemplate = 0; |
| 6560 | bool isExplicitSpecialization = false; |
| 6561 | bool isFunctionTemplateSpecialization = false; |
| Nico Weber | 7b5a716 | 2012-06-25 17:21:05 +0000 | [diff] [blame] | 6562 | |
| Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 6563 | bool isDependentClassScopeExplicitSpecialization = false; |
| Nico Weber | 7b5a716 | 2012-06-25 17:21:05 +0000 | [diff] [blame] | 6564 | bool HasExplicitTemplateArgs = false; |
| 6565 | TemplateArgumentListInfo TemplateArgs; |
| 6566 | |
| Kaelyn Uhrain | e1a9ff7 | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6567 | bool isVirtualOkay = false; |
| Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6568 | |
| Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 6569 | DeclContext *OriginalDC = DC; |
| 6570 | bool IsLocalExternDecl = adjustContextForLocalExternDecl(DC); |
| 6571 | |
| Kaelyn Uhrain | e1a9ff7 | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6572 | FunctionDecl *NewFD = CreateNewFunctionDecl(*this, D, DC, R, TInfo, SC, |
| 6573 | isVirtualOkay); |
| 6574 | if (!NewFD) return 0; |
| 6575 | |
| Argyrios Kyrtzidis | 8ad3bab | 2011-11-23 20:27:36 +0000 | [diff] [blame] | 6576 | if (OriginalLexicalContext && OriginalLexicalContext->isObjCContainer()) |
| 6577 | NewFD->setTopLevelDeclInObjCContainer(); |
| 6578 | |
| Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 6579 | // Set the lexical context. If this is a function-scope declaration, or has a |
| 6580 | // C++ scope specifier, or is the object of a friend declaration, the lexical |
| 6581 | // context will be different from the semantic context. |
| 6582 | NewFD->setLexicalDeclContext(CurContext); |
| 6583 | |
| 6584 | if (IsLocalExternDecl) |
| 6585 | NewFD->setLocalExternDecl(); |
| 6586 | |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6587 | if (getLangOpts().CPlusPlus) { |
| Kaelyn Uhrain | e1a9ff7 | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6588 | bool isInline = D.getDeclSpec().isInlineSpecified(); |
| Douglas Gregor | 513e63c | 2010-12-10 19:28:19 +0000 | [diff] [blame] | 6589 | bool isVirtual = D.getDeclSpec().isVirtualSpecified(); |
| 6590 | bool isExplicit = D.getDeclSpec().isExplicitSpecified(); |
| Richard Smith | a77a0a6 | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 6591 | bool isConstexpr = D.getDeclSpec().isConstexprSpecified(); |
| Kaelyn Uhrain | e1a9ff7 | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6592 | isFriend = D.getDeclSpec().isFriendSpecified(); |
| Kaelyn Uhrain | 4dc695d | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 6593 | if (isFriend && !isInline && D.isFunctionDefinition()) { |
| Abramo Bagnara | a3088e6 | 2011-03-18 15:21:59 +0000 | [diff] [blame] | 6594 | // C++ [class.friend]p5 |
| 6595 | // A function can be defined in a friend declaration of a |
| 6596 | // class . . . . Such a function is implicitly inline. |
| 6597 | NewFD->setImplicitlyInline(); |
| 6598 | } |
| 6599 | |
| John McCall | db632ac | 2012-09-25 07:32:39 +0000 | [diff] [blame] | 6600 | // If this is a method defined in an __interface, and is not a constructor |
| 6601 | // or an overloaded operator, then set the pure flag (isVirtual will already |
| 6602 | // return true). |
| 6603 | if (const CXXRecordDecl *Parent = |
| 6604 | dyn_cast<CXXRecordDecl>(NewFD->getDeclContext())) { |
| 6605 | if (Parent->isInterface() && cast<CXXMethodDecl>(NewFD)->isUserProvided()) |
| Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 6606 | NewFD->setPure(true); |
| 6607 | } |
| 6608 | |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 6609 | SetNestedNameSpecifier(NewFD, D); |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 6610 | isExplicitSpecialization = false; |
| 6611 | isFunctionTemplateSpecialization = false; |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 6612 | if (D.isInvalidType()) |
| 6613 | NewFD->setInvalidDecl(); |
| Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 6614 | |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 6615 | // Match up the template parameter lists with the scope specifier, then |
| 6616 | // determine whether we have a template or a template specialization. |
| 6617 | bool Invalid = false; |
| Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 6618 | if (TemplateParameterList *TemplateParams = |
| 6619 | MatchTemplateParametersToScopeSpecifier( |
| 6620 | D.getDeclSpec().getLocStart(), D.getIdentifierLoc(), |
| 6621 | D.getCXXScopeSpec(), TemplateParamLists, isFriend, |
| 6622 | isExplicitSpecialization, Invalid)) { |
| Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6623 | if (TemplateParams->size() > 0) { |
| 6624 | // This is a function template |
| Abramo Bagnara | da41d0c | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 6625 | |
| Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6626 | // Check that we can declare a template here. |
| 6627 | if (CheckTemplateDeclScope(S, TemplateParams)) |
| 6628 | return 0; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6629 | |
| Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6630 | // A destructor cannot be a template. |
| 6631 | if (Name.getNameKind() == DeclarationName::CXXDestructorName) { |
| 6632 | Diag(NewFD->getLocation(), diag::err_destructor_template); |
| 6633 | return 0; |
| John McCall | 1f0479e | 2010-03-24 08:27:58 +0000 | [diff] [blame] | 6634 | } |
| Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 6635 | |
| 6636 | // If we're adding a template to a dependent context, we may need to |
| David Blaikie | 30d1544 | 2011-10-19 22:56:21 +0000 | [diff] [blame] | 6637 | // rebuilding some of the types used within the template parameter list, |
| Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 6638 | // now that we know what the current instantiation is. |
| 6639 | if (DC->isDependentContext()) { |
| 6640 | ContextRAII SavedContext(*this, DC); |
| 6641 | if (RebuildTemplateParamsInCurrentInstantiation(TemplateParams)) |
| 6642 | Invalid = true; |
| 6643 | } |
| 6644 | |
| John McCall | 1f0479e | 2010-03-24 08:27:58 +0000 | [diff] [blame] | 6645 | |
| Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6646 | FunctionTemplate = FunctionTemplateDecl::Create(Context, DC, |
| 6647 | NewFD->getLocation(), |
| 6648 | Name, TemplateParams, |
| 6649 | NewFD); |
| 6650 | FunctionTemplate->setLexicalDeclContext(CurContext); |
| 6651 | NewFD->setDescribedFunctionTemplate(FunctionTemplate); |
| 6652 | |
| 6653 | // For source fidelity, store the other template param lists. |
| 6654 | if (TemplateParamLists.size() > 1) { |
| 6655 | NewFD->setTemplateParameterListsInfo(Context, |
| 6656 | TemplateParamLists.size() - 1, |
| Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 6657 | TemplateParamLists.data()); |
| Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6658 | } |
| 6659 | } else { |
| 6660 | // This is a function template specialization. |
| 6661 | isFunctionTemplateSpecialization = true; |
| 6662 | // For source fidelity, store all the template param lists. |
| 6663 | NewFD->setTemplateParameterListsInfo(Context, |
| 6664 | TemplateParamLists.size(), |
| Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 6665 | TemplateParamLists.data()); |
| Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6666 | |
| 6667 | // C++0x [temp.expl.spec]p20 forbids "template<> friend void foo(int);". |
| 6668 | if (isFriend) { |
| 6669 | // We want to remove the "template<>", found here. |
| 6670 | SourceRange RemoveRange = TemplateParams->getSourceRange(); |
| 6671 | |
| 6672 | // If we remove the template<> and the name is not a |
| 6673 | // template-id, we're actually silently creating a problem: |
| 6674 | // the friend declaration will refer to an untemplated decl, |
| 6675 | // and clearly the user wants a template specialization. So |
| 6676 | // we need to insert '<>' after the name. |
| 6677 | SourceLocation InsertLoc; |
| 6678 | if (D.getName().getKind() != UnqualifiedId::IK_TemplateId) { |
| 6679 | InsertLoc = D.getName().getSourceRange().getEnd(); |
| 6680 | InsertLoc = PP.getLocForEndOfToken(InsertLoc); |
| 6681 | } |
| 6682 | |
| 6683 | Diag(D.getIdentifierLoc(), diag::err_template_spec_decl_friend) |
| 6684 | << Name << RemoveRange |
| 6685 | << FixItHint::CreateRemoval(RemoveRange) |
| 6686 | << FixItHint::CreateInsertion(InsertLoc, "<>"); |
| 6687 | } |
| 6688 | } |
| 6689 | } |
| 6690 | else { |
| 6691 | // All template param lists were matched against the scope specifier: |
| 6692 | // this is NOT (an explicit specialization of) a template. |
| 6693 | if (TemplateParamLists.size() > 0) |
| 6694 | // For source fidelity, store all the template param lists. |
| 6695 | NewFD->setTemplateParameterListsInfo(Context, |
| 6696 | TemplateParamLists.size(), |
| Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 6697 | TemplateParamLists.data()); |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 6698 | } |
| 6699 | |
| 6700 | if (Invalid) { |
| 6701 | NewFD->setInvalidDecl(); |
| 6702 | if (FunctionTemplate) |
| 6703 | FunctionTemplate->setInvalidDecl(); |
| 6704 | } |
| Kaelyn Uhrain | e1a9ff7 | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6705 | |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 6706 | // C++ [dcl.fct.spec]p5: |
| 6707 | // The virtual specifier shall only be used in declarations of |
| 6708 | // nonstatic class member functions that appear within a |
| 6709 | // member-specification of a class declaration; see 10.3. |
| 6710 | // |
| 6711 | if (isVirtual && !NewFD->isInvalidDecl()) { |
| 6712 | if (!isVirtualOkay) { |
| 6713 | Diag(D.getDeclSpec().getVirtualSpecLoc(), |
| 6714 | diag::err_virtual_non_function); |
| 6715 | } else if (!CurContext->isRecord()) { |
| 6716 | // 'virtual' was specified outside of the class. |
| Anders Carlsson | e973899 | 2011-01-22 14:43:56 +0000 | [diff] [blame] | 6717 | Diag(D.getDeclSpec().getVirtualSpecLoc(), |
| 6718 | diag::err_virtual_out_of_class) |
| 6719 | << FixItHint::CreateRemoval(D.getDeclSpec().getVirtualSpecLoc()); |
| 6720 | } else if (NewFD->getDescribedFunctionTemplate()) { |
| 6721 | // C++ [temp.mem]p3: |
| 6722 | // A member function template shall not be virtual. |
| 6723 | Diag(D.getDeclSpec().getVirtualSpecLoc(), |
| 6724 | diag::err_virtual_member_function_template) |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 6725 | << FixItHint::CreateRemoval(D.getDeclSpec().getVirtualSpecLoc()); |
| 6726 | } else { |
| 6727 | // Okay: Add virtual to the method. |
| 6728 | NewFD->setVirtualAsWritten(true); |
| John McCall | 816d75b | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 6729 | } |
| Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 6730 | |
| 6731 | if (getLangOpts().CPlusPlus1y && |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 6732 | NewFD->getReturnType()->isUndeducedType()) |
| Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 6733 | Diag(D.getDeclSpec().getVirtualSpecLoc(), diag::err_auto_fn_virtual); |
| Douglas Gregor | c1da0f0 | 2009-06-24 00:23:40 +0000 | [diff] [blame] | 6734 | } |
| Abramo Bagnara | da41d0c | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 6735 | |
| Richard Smith | c156470 | 2013-11-15 02:58:23 +0000 | [diff] [blame] | 6736 | if (getLangOpts().CPlusPlus1y && |
| 6737 | (NewFD->isDependentContext() || |
| 6738 | (isFriend && CurContext->isDependentContext())) && |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 6739 | NewFD->getReturnType()->isUndeducedType()) { |
| Richard Smith | c58f38f | 2013-08-14 20:16:31 +0000 | [diff] [blame] | 6740 | // If the function template is referenced directly (for instance, as a |
| 6741 | // member of the current instantiation), pretend it has a dependent type. |
| 6742 | // This is not really justified by the standard, but is the only sane |
| 6743 | // thing to do. |
| Richard Smith | c156470 | 2013-11-15 02:58:23 +0000 | [diff] [blame] | 6744 | // FIXME: For a friend function, we have not marked the function as being |
| 6745 | // a friend yet, so 'isDependentContext' on the FD doesn't work. |
| Richard Smith | c58f38f | 2013-08-14 20:16:31 +0000 | [diff] [blame] | 6746 | const FunctionProtoType *FPT = |
| 6747 | NewFD->getType()->castAs<FunctionProtoType>(); |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 6748 | QualType Result = |
| 6749 | SubstAutoType(FPT->getReturnType(), Context.DependentTy); |
| Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 6750 | NewFD->setType(Context.getFunctionType(Result, FPT->getParamTypes(), |
| Richard Smith | c58f38f | 2013-08-14 20:16:31 +0000 | [diff] [blame] | 6751 | FPT->getExtProtoInfo())); |
| 6752 | } |
| 6753 | |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 6754 | // C++ [dcl.fct.spec]p3: |
| David Blaikie | 30d1544 | 2011-10-19 22:56:21 +0000 | [diff] [blame] | 6755 | // The inline specifier shall not appear on a block scope function |
| 6756 | // declaration. |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 6757 | if (isInline && !NewFD->isInvalidDecl()) { |
| 6758 | if (CurContext->isFunctionOrMethod()) { |
| 6759 | // 'inline' is not allowed on block scope function declaration. |
| 6760 | Diag(D.getDeclSpec().getInlineSpecLoc(), |
| 6761 | diag::err_inline_declaration_block_scope) << Name |
| 6762 | << FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc()); |
| 6763 | } |
| 6764 | } |
| Kaelyn Uhrain | e1a9ff7 | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6765 | |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 6766 | // C++ [dcl.fct.spec]p6: |
| 6767 | // The explicit specifier shall be used only in the declaration of a |
| David Blaikie | 30d1544 | 2011-10-19 22:56:21 +0000 | [diff] [blame] | 6768 | // constructor or conversion function within its class definition; |
| 6769 | // see 12.3.1 and 12.3.2. |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 6770 | if (isExplicit && !NewFD->isInvalidDecl()) { |
| 6771 | if (!CurContext->isRecord()) { |
| 6772 | // 'explicit' was specified outside of the class. |
| 6773 | Diag(D.getDeclSpec().getExplicitSpecLoc(), |
| 6774 | diag::err_explicit_out_of_class) |
| 6775 | << FixItHint::CreateRemoval(D.getDeclSpec().getExplicitSpecLoc()); |
| 6776 | } else if (!isa<CXXConstructorDecl>(NewFD) && |
| 6777 | !isa<CXXConversionDecl>(NewFD)) { |
| 6778 | // 'explicit' was specified on a function that wasn't a constructor |
| 6779 | // or conversion function. |
| 6780 | Diag(D.getDeclSpec().getExplicitSpecLoc(), |
| 6781 | diag::err_explicit_non_ctor_or_conv_function) |
| 6782 | << FixItHint::CreateRemoval(D.getDeclSpec().getExplicitSpecLoc()); |
| 6783 | } |
| 6784 | } |
| Abramo Bagnara | da41d0c | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 6785 | |
| Richard Smith | a77a0a6 | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 6786 | if (isConstexpr) { |
| Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 6787 | // C++11 [dcl.constexpr]p2: constexpr functions and constexpr constructors |
| Richard Smith | a77a0a6 | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 6788 | // are implicitly inline. |
| 6789 | NewFD->setImplicitlyInline(); |
| 6790 | |
| Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 6791 | // C++11 [dcl.constexpr]p3: functions declared constexpr are required to |
| Richard Smith | a77a0a6 | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 6792 | // be either constructors or to return a literal type. Therefore, |
| 6793 | // destructors cannot be declared constexpr. |
| 6794 | if (isa<CXXDestructorDecl>(NewFD)) |
| Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 6795 | Diag(D.getDeclSpec().getConstexprSpecLoc(), diag::err_constexpr_dtor); |
| Richard Smith | a77a0a6 | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 6796 | } |
| 6797 | |
| Douglas Gregor | 26701a4 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 6798 | // If __module_private__ was specified, mark the function accordingly. |
| 6799 | if (D.getDeclSpec().isModulePrivateSpecified()) { |
| Douglas Gregor | 3c7cd6a | 2011-09-09 20:53:38 +0000 | [diff] [blame] | 6800 | if (isFunctionTemplateSpecialization) { |
| 6801 | SourceLocation ModulePrivateLoc |
| 6802 | = D.getDeclSpec().getModulePrivateSpecLoc(); |
| 6803 | Diag(ModulePrivateLoc, diag::err_module_private_specialization) |
| 6804 | << 0 |
| 6805 | << FixItHint::CreateRemoval(ModulePrivateLoc); |
| 6806 | } else { |
| 6807 | NewFD->setModulePrivate(); |
| 6808 | if (FunctionTemplate) |
| 6809 | FunctionTemplate->setModulePrivate(); |
| 6810 | } |
| Douglas Gregor | 26701a4 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 6811 | } |
| Richard Smith | a77a0a6 | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 6812 | |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 6813 | if (isFriend) { |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 6814 | if (FunctionTemplate) { |
| Richard Smith | 6401768 | 2013-07-17 23:53:16 +0000 | [diff] [blame] | 6815 | FunctionTemplate->setObjectOfFriendDecl(); |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 6816 | FunctionTemplate->setAccess(AS_public); |
| 6817 | } |
| Richard Smith | 6401768 | 2013-07-17 23:53:16 +0000 | [diff] [blame] | 6818 | NewFD->setObjectOfFriendDecl(); |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 6819 | NewFD->setAccess(AS_public); |
| 6820 | } |
| 6821 | |
| Douglas Gregor | 5d1b4e3 | 2011-11-07 20:56:01 +0000 | [diff] [blame] | 6822 | // If a function is defined as defaulted or deleted, mark it as such now. |
| Richard Smith | b63b6ee | 2014-01-22 01:43:19 +0000 | [diff] [blame] | 6823 | // FIXME: Does this ever happen? ActOnStartOfFunctionDef forces the function |
| 6824 | // definition kind to FDK_Definition. |
| Douglas Gregor | 5d1b4e3 | 2011-11-07 20:56:01 +0000 | [diff] [blame] | 6825 | switch (D.getFunctionDefinitionKind()) { |
| 6826 | case FDK_Declaration: |
| 6827 | case FDK_Definition: |
| 6828 | break; |
| 6829 | |
| 6830 | case FDK_Defaulted: |
| 6831 | NewFD->setDefaulted(); |
| 6832 | break; |
| 6833 | |
| 6834 | case FDK_Deleted: |
| 6835 | NewFD->setDeletedAsWritten(); |
| 6836 | break; |
| 6837 | } |
| 6838 | |
| Kaelyn Uhrain | 4dc695d | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 6839 | if (isa<CXXMethodDecl>(NewFD) && DC == CurContext && |
| 6840 | D.isFunctionDefinition()) { |
| Douglas Gregor | 5d1b4e3 | 2011-11-07 20:56:01 +0000 | [diff] [blame] | 6841 | // C++ [class.mfct]p2: |
| 6842 | // A member function may be defined (8.4) in its class definition, in |
| 6843 | // which case it is an inline member function (7.1.2) |
| John McCall | 357d0f3 | 2010-12-15 04:00:32 +0000 | [diff] [blame] | 6844 | NewFD->setImplicitlyInline(); |
| 6845 | } |
| 6846 | |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 6847 | if (SC == SC_Static && isa<CXXMethodDecl>(NewFD) && |
| 6848 | !CurContext->isRecord()) { |
| 6849 | // C++ [class.static]p1: |
| 6850 | // A data or function member of a class may be declared static |
| 6851 | // in a class definition, in which case it is a static member of |
| 6852 | // the class. |
| 6853 | |
| 6854 | // Complain about the 'static' specifier if it's on an out-of-line |
| 6855 | // member function definition. |
| 6856 | Diag(D.getDeclSpec().getStorageClassSpecLoc(), |
| 6857 | diag::err_static_out_of_line) |
| 6858 | << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc()); |
| 6859 | } |
| Richard Smith | 66f3ac9 | 2012-10-20 08:26:51 +0000 | [diff] [blame] | 6860 | |
| 6861 | // C++11 [except.spec]p15: |
| 6862 | // A deallocation function with no exception-specification is treated |
| 6863 | // as if it were specified with noexcept(true). |
| 6864 | const FunctionProtoType *FPT = R->getAs<FunctionProtoType>(); |
| 6865 | if ((Name.getCXXOverloadedOperator() == OO_Delete || |
| 6866 | Name.getCXXOverloadedOperator() == OO_Array_Delete) && |
| Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6867 | getLangOpts().CPlusPlus11 && FPT && !FPT->hasExceptionSpec()) { |
| Richard Smith | 66f3ac9 | 2012-10-20 08:26:51 +0000 | [diff] [blame] | 6868 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 6869 | EPI.ExceptionSpecType = EST_BasicNoexcept; |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 6870 | NewFD->setType(Context.getFunctionType(FPT->getReturnType(), |
| Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 6871 | FPT->getParamTypes(), EPI)); |
| Richard Smith | 66f3ac9 | 2012-10-20 08:26:51 +0000 | [diff] [blame] | 6872 | } |
| Douglas Gregor | 5f0e252 | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 6873 | } |
| Kaelyn Uhrain | e1a9ff7 | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6874 | |
| 6875 | // Filter out previous declarations that don't match the scope. |
| Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 6876 | FilterLookupForScope(Previous, OriginalDC, S, shouldConsiderLinkage(NewFD), |
| Richard Smith | 72bcaec | 2013-12-05 04:30:04 +0000 | [diff] [blame] | 6877 | D.getCXXScopeSpec().isNotEmpty() || |
| Kaelyn Uhrain | e1a9ff7 | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6878 | isExplicitSpecialization || |
| 6879 | isFunctionTemplateSpecialization); |
| Richard Smith | 1c34fb7 | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 6880 | |
| Zhongxing Xu | bece5d6 | 2009-01-16 01:13:29 +0000 | [diff] [blame] | 6881 | // Handle GNU asm-label extension (encoded as an attribute). |
| 6882 | if (Expr *E = (Expr*) D.getAsmLabel()) { |
| 6883 | // The parser guarantees this is a string. |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6884 | StringLiteral *SE = cast<StringLiteral>(E); |
| Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 6885 | NewFD->addAttr(::new (Context) AsmLabelAttr(SE->getStrTokenLoc(0), Context, |
| Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 6886 | SE->getString(), 0)); |
| David Chisnall | 0867d9c | 2012-02-18 16:12:34 +0000 | [diff] [blame] | 6887 | } else if (!ExtnameUndeclaredIdentifiers.empty()) { |
| 6888 | llvm::DenseMap<IdentifierInfo*,AsmLabelAttr*>::iterator I = |
| 6889 | ExtnameUndeclaredIdentifiers.find(NewFD->getIdentifier()); |
| 6890 | if (I != ExtnameUndeclaredIdentifiers.end()) { |
| 6891 | NewFD->addAttr(I->second); |
| 6892 | ExtnameUndeclaredIdentifiers.erase(I); |
| 6893 | } |
| Zhongxing Xu | bece5d6 | 2009-01-16 01:13:29 +0000 | [diff] [blame] | 6894 | } |
| 6895 | |
| Chris Lattner | 9af40c1 | 2009-04-25 06:12:16 +0000 | [diff] [blame] | 6896 | // Copy the parameter declarations from the declarator D to the function |
| 6897 | // declaration NewFD, if they are available. First scavenge them into Params. |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6898 | SmallVector<ParmVarDecl*, 16> Params; |
| Abramo Bagnara | 6d81063 | 2010-12-14 22:11:44 +0000 | [diff] [blame] | 6899 | if (D.isFunctionDeclarator()) { |
| Abramo Bagnara | 924a8f3 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 6900 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo(); |
| Zhongxing Xu | bece5d6 | 2009-01-16 01:13:29 +0000 | [diff] [blame] | 6901 | |
| Zhongxing Xu | bece5d6 | 2009-01-16 01:13:29 +0000 | [diff] [blame] | 6902 | // Check for C99 6.7.5.3p10 - foo(void) is a non-varargs |
| 6903 | // function that takes no arguments, not a function that takes a |
| 6904 | // single void argument. |
| 6905 | // We let through "const void" here because Sema::GetTypeForDeclarator |
| 6906 | // already checks for that case. |
| Alp Toker | c535072 | 2014-02-26 22:27:52 +0000 | [diff] [blame] | 6907 | if (FTI.NumParams == 1 && !FTI.isVariadic && FTI.Params[0].Ident == 0 && |
| 6908 | FTI.Params[0].Param && |
| 6909 | cast<ParmVarDecl>(FTI.Params[0].Param)->getType()->isVoidType()) { |
| Chris Lattner | 9af40c1 | 2009-04-25 06:12:16 +0000 | [diff] [blame] | 6910 | // Empty arg list, don't push any params. |
| Alp Toker | c535072 | 2014-02-26 22:27:52 +0000 | [diff] [blame] | 6911 | } else if (FTI.NumParams > 0 && FTI.Params[0].Param != 0) { |
| 6912 | for (unsigned i = 0, e = FTI.NumParams; i != e; ++i) { |
| 6913 | ParmVarDecl *Param = cast<ParmVarDecl>(FTI.Params[i].Param); |
| Argyrios Kyrtzidis | 11a846a | 2009-07-14 03:18:53 +0000 | [diff] [blame] | 6914 | assert(Param->getDeclContext() != NewFD && "Was set before ?"); |
| 6915 | Param->setDeclContext(NewFD); |
| 6916 | Params.push_back(Param); |
| John McCall | b723860 | 2010-04-14 01:27:20 +0000 | [diff] [blame] | 6917 | |
| 6918 | if (Param->isInvalidDecl()) |
| 6919 | NewFD->setInvalidDecl(); |
| Argyrios Kyrtzidis | 11a846a | 2009-07-14 03:18:53 +0000 | [diff] [blame] | 6920 | } |
| Zhongxing Xu | bece5d6 | 2009-01-16 01:13:29 +0000 | [diff] [blame] | 6921 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6922 | |
| John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6923 | } else if (const FunctionProtoType *FT = R->getAs<FunctionProtoType>()) { |
| Chris Lattner | 47c0d00 | 2009-04-25 06:03:53 +0000 | [diff] [blame] | 6924 | // When we're declaring a function with a typedef, typeof, etc as in the |
| Zhongxing Xu | bece5d6 | 2009-01-16 01:13:29 +0000 | [diff] [blame] | 6925 | // following example, we'll need to synthesize (unnamed) |
| 6926 | // parameters for use in the declaration. |
| 6927 | // |
| 6928 | // @code |
| 6929 | // typedef void fn(int); |
| 6930 | // fn f; |
| 6931 | // @endcode |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6932 | |
| Chris Lattner | 47c0d00 | 2009-04-25 06:03:53 +0000 | [diff] [blame] | 6933 | // Synthesize a parameter for each argument type. |
| Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 6934 | for (FunctionProtoType::param_type_iterator AI = FT->param_type_begin(), |
| 6935 | AE = FT->param_type_end(); |
| 6936 | AI != AE; ++AI) { |
| John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 6937 | ParmVarDecl *Param = |
| 6938 | BuildParmVarDeclForTypedef(NewFD, D.getIdentifierLoc(), *AI); |
| John McCall | 8fb0d9d | 2011-05-01 22:35:37 +0000 | [diff] [blame] | 6939 | Param->setScopeInfo(0, Params.size()); |
| Chris Lattner | 47c0d00 | 2009-04-25 06:03:53 +0000 | [diff] [blame] | 6940 | Params.push_back(Param); |
| Zhongxing Xu | bece5d6 | 2009-01-16 01:13:29 +0000 | [diff] [blame] | 6941 | } |
| Chris Lattner | 49303b2 | 2009-04-25 18:38:18 +0000 | [diff] [blame] | 6942 | } else { |
| 6943 | assert(R->isFunctionNoProtoType() && NewFD->getNumParams() == 0 && |
| 6944 | "Should not need args for typedef of non-prototype fn"); |
| Zhongxing Xu | bece5d6 | 2009-01-16 01:13:29 +0000 | [diff] [blame] | 6945 | } |
| Anton Korobeynikov | f0c267e | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 6946 | |
| Chris Lattner | 9af40c1 | 2009-04-25 06:12:16 +0000 | [diff] [blame] | 6947 | // Finally, we know we have the right number of parameters, install them. |
| David Blaikie | 9c70e04 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 6948 | NewFD->setParams(Params); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6949 | |
| James Molloy | 6f8780b | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 6950 | // Find all anonymous symbols defined during the declaration of this function |
| 6951 | // and add to NewFD. This lets us track decls such 'enum Y' in: |
| 6952 | // |
| 6953 | // void f(enum Y {AA} x) {} |
| 6954 | // |
| 6955 | // which would otherwise incorrectly end up in the translation unit scope. |
| 6956 | NewFD->setDeclsInPrototypeScope(DeclsInPrototypeScope); |
| 6957 | DeclsInPrototypeScope.clear(); |
| 6958 | |
| Richard Smith | debc59d | 2013-01-30 05:45:05 +0000 | [diff] [blame] | 6959 | if (D.getDeclSpec().isNoreturnSpecified()) |
| 6960 | NewFD->addAttr( |
| 6961 | ::new(Context) C11NoReturnAttr(D.getDeclSpec().getNoreturnSpecLoc(), |
| Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 6962 | Context, 0)); |
| Richard Smith | debc59d | 2013-01-30 05:45:05 +0000 | [diff] [blame] | 6963 | |
| Richard Smith | 84208dc | 2012-03-13 05:56:40 +0000 | [diff] [blame] | 6964 | // Functions returning a variably modified type violate C99 6.7.5.2p2 |
| 6965 | // because all functions have linkage. |
| 6966 | if (!NewFD->isInvalidDecl() && |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 6967 | NewFD->getReturnType()->isVariablyModifiedType()) { |
| Richard Smith | 84208dc | 2012-03-13 05:56:40 +0000 | [diff] [blame] | 6968 | Diag(NewFD->getLocation(), diag::err_vm_func_decl); |
| 6969 | NewFD->setInvalidDecl(); |
| 6970 | } |
| 6971 | |
| Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 6972 | // Handle attributes. |
| Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 6973 | ProcessDeclAttributes(S, NewFD, D); |
| Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 6974 | |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 6975 | QualType RetType = NewFD->getReturnType(); |
| Kaelyn Uhrain | 8681f9d | 2012-11-12 23:48:05 +0000 | [diff] [blame] | 6976 | const CXXRecordDecl *Ret = RetType->isRecordType() ? |
| 6977 | RetType->getAsCXXRecordDecl() : RetType->getPointeeCXXRecordDecl(); |
| 6978 | if (!NewFD->isInvalidDecl() && !NewFD->hasAttr<WarnUnusedResultAttr>() && |
| 6979 | Ret && Ret->hasAttr<WarnUnusedResultAttr>()) { |
| Kaelyn Uhrain | 1137001 | 2012-11-13 21:23:31 +0000 | [diff] [blame] | 6980 | const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(NewFD); |
| David Blaikie | 080a61c | 2014-02-09 07:24:41 +0000 | [diff] [blame] | 6981 | // Attach WarnUnusedResult to functions returning types with that attribute. |
| 6982 | // Don't apply the attribute to that type's own non-static member functions |
| 6983 | // (to avoid warning on things like assignment operators) |
| 6984 | if (!MD || MD->getParent() != Ret) |
| Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 6985 | NewFD->addAttr(WarnUnusedResultAttr::CreateImplicit(Context)); |
| Kaelyn Uhrain | 8681f9d | 2012-11-12 23:48:05 +0000 | [diff] [blame] | 6986 | } |
| 6987 | |
| Joey Gouly | 16cb99d | 2014-01-06 11:26:18 +0000 | [diff] [blame] | 6988 | if (getLangOpts().OpenCL) { |
| 6989 | // OpenCL v1.1 s6.5: Using an address space qualifier in a function return |
| 6990 | // type declaration will generate a compilation error. |
| 6991 | unsigned AddressSpace = RetType.getAddressSpace(); |
| 6992 | if (AddressSpace == LangAS::opencl_local || |
| 6993 | AddressSpace == LangAS::opencl_global || |
| 6994 | AddressSpace == LangAS::opencl_constant) { |
| 6995 | Diag(NewFD->getLocation(), |
| 6996 | diag::err_opencl_return_value_with_address_space); |
| 6997 | NewFD->setInvalidDecl(); |
| 6998 | } |
| 6999 | } |
| 7000 | |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7001 | if (!getLangOpts().CPlusPlus) { |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7002 | // Perform semantic checking on the function declaration. |
| Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 7003 | bool isExplicitSpecialization=false; |
| David Majnemer | 027f9c4 | 2013-07-06 02:13:46 +0000 | [diff] [blame] | 7004 | if (!NewFD->isInvalidDecl() && NewFD->isMain()) |
| 7005 | CheckMain(NewFD, D.getDeclSpec()); |
| 7006 | |
| David Majnemer | c729b0b | 2013-09-16 22:44:20 +0000 | [diff] [blame] | 7007 | if (!NewFD->isInvalidDecl() && NewFD->isMSVCRTEntryPoint()) |
| 7008 | CheckMSVCRTEntryPoint(NewFD); |
| 7009 | |
| David Majnemer | 027f9c4 | 2013-07-06 02:13:46 +0000 | [diff] [blame] | 7010 | if (!NewFD->isInvalidDecl()) |
| Richard Smith | 84208dc | 2012-03-13 05:56:40 +0000 | [diff] [blame] | 7011 | D.setRedeclaration(CheckFunctionDeclaration(S, NewFD, Previous, |
| 7012 | isExplicitSpecialization)); |
| Fariborz Jahanian | aaf376b | 2012-09-05 17:52:12 +0000 | [diff] [blame] | 7013 | else if (!Previous.empty()) |
| Richard Smith | 1c34fb7 | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 7014 | // Make graceful recovery from an invalid redeclaration. |
| 7015 | D.setRedeclaration(true); |
| Kaelyn Uhrain | 4dc695d | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 7016 | assert((NewFD->isInvalidDecl() || !D.isRedeclaration() || |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7017 | Previous.getResultKind() != LookupResult::FoundOverloaded) && |
| 7018 | "previous declaration set still overloaded"); |
| 7019 | } else { |
| Richard Smith | fa27bc4 | 2013-11-16 01:57:09 +0000 | [diff] [blame] | 7020 | // C++11 [replacement.functions]p3: |
| 7021 | // The program's definitions shall not be specified as inline. |
| 7022 | // |
| 7023 | // N.B. We diagnose declarations instead of definitions per LWG issue 2340. |
| 7024 | // |
| 7025 | // Suppress the diagnostic if the function is __attribute__((used)), since |
| 7026 | // that forces an external definition to be emitted. |
| 7027 | if (D.getDeclSpec().isInlineSpecified() && |
| 7028 | NewFD->isReplaceableGlobalAllocationFunction() && |
| 7029 | !NewFD->hasAttr<UsedAttr>()) |
| 7030 | Diag(D.getDeclSpec().getInlineSpecLoc(), |
| 7031 | diag::ext_operator_new_delete_declared_inline) |
| 7032 | << NewFD->getDeclName(); |
| 7033 | |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7034 | // If the declarator is a template-id, translate the parser's template |
| 7035 | // argument list into our AST format. |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7036 | if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) { |
| 7037 | TemplateIdAnnotation *TemplateId = D.getName().TemplateId; |
| 7038 | TemplateArgs.setLAngleLoc(TemplateId->LAngleLoc); |
| 7039 | TemplateArgs.setRAngleLoc(TemplateId->RAngleLoc); |
| Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 7040 | ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(), |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7041 | TemplateId->NumArgs); |
| 7042 | translateTemplateArguments(TemplateArgsPtr, |
| 7043 | TemplateArgs); |
| Douglas Gregor | 0e876e0 | 2009-09-25 23:53:26 +0000 | [diff] [blame] | 7044 | |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7045 | HasExplicitTemplateArgs = true; |
| Douglas Gregor | 0e876e0 | 2009-09-25 23:53:26 +0000 | [diff] [blame] | 7046 | |
| Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 7047 | if (NewFD->isInvalidDecl()) { |
| 7048 | HasExplicitTemplateArgs = false; |
| 7049 | } else if (FunctionTemplate) { |
| Douglas Gregor | a5f6f9c | 2011-01-24 18:54:39 +0000 | [diff] [blame] | 7050 | // Function template with explicit template arguments. |
| 7051 | Diag(D.getIdentifierLoc(), diag::err_function_template_partial_spec) |
| 7052 | << SourceRange(TemplateId->LAngleLoc, TemplateId->RAngleLoc); |
| 7053 | |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7054 | HasExplicitTemplateArgs = false; |
| 7055 | } else if (!isFunctionTemplateSpecialization && |
| 7056 | !D.getDeclSpec().isFriendSpecified()) { |
| 7057 | // We have encountered something that the user meant to be a |
| 7058 | // specialization (because it has explicitly-specified template |
| 7059 | // arguments) but that was not introduced with a "template<>" (or had |
| 7060 | // too few of them). |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7061 | // FIXME: Differentiate between attempts for explicit instantiations |
| 7062 | // (starting with "template") and the rest. |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7063 | Diag(D.getIdentifierLoc(), diag::err_template_spec_needs_header) |
| 7064 | << SourceRange(TemplateId->LAngleLoc, TemplateId->RAngleLoc) |
| 7065 | << FixItHint::CreateInsertion( |
| Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 7066 | D.getDeclSpec().getLocStart(), |
| David Blaikie | 30d1544 | 2011-10-19 22:56:21 +0000 | [diff] [blame] | 7067 | "template<> "); |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7068 | isFunctionTemplateSpecialization = true; |
| John McCall | f7cfb22 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 7069 | } else { |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7070 | // "friend void foo<>(int);" is an implicit specialization decl. |
| 7071 | isFunctionTemplateSpecialization = true; |
| Francois Pichet | 6d76e6c | 2010-10-01 21:19:28 +0000 | [diff] [blame] | 7072 | } |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7073 | } else if (isFriend && isFunctionTemplateSpecialization) { |
| 7074 | // This combination is only possible in a recovery case; the user |
| 7075 | // wrote something like: |
| 7076 | // template <> friend void foo(int); |
| 7077 | // which we're recovering from as if the user had written: |
| 7078 | // friend void foo<>(int); |
| 7079 | // Go ahead and fake up a template id. |
| 7080 | HasExplicitTemplateArgs = true; |
| 7081 | TemplateArgs.setLAngleLoc(D.getIdentifierLoc()); |
| 7082 | TemplateArgs.setRAngleLoc(D.getIdentifierLoc()); |
| Douglas Gregor | f4f296d | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 7083 | } |
| John McCall | f7cfb22 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 7084 | |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7085 | // If it's a friend (and only if it's a friend), it's possible |
| 7086 | // that either the specialized function type or the specialized |
| 7087 | // template is dependent, and therefore matching will fail. In |
| 7088 | // this case, don't check the specialization yet. |
| Douglas Gregor | e9d075e | 2011-10-09 20:59:17 +0000 | [diff] [blame] | 7089 | bool InstantiationDependent = false; |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7090 | if (isFunctionTemplateSpecialization && isFriend && |
| Douglas Gregor | e9d075e | 2011-10-09 20:59:17 +0000 | [diff] [blame] | 7091 | (NewFD->getType()->isDependentType() || DC->isDependentContext() || |
| 7092 | TemplateSpecializationType::anyDependentTemplateArguments( |
| 7093 | TemplateArgs.getArgumentArray(), TemplateArgs.size(), |
| 7094 | InstantiationDependent))) { |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7095 | assert(HasExplicitTemplateArgs && |
| 7096 | "friend function specialization without template args"); |
| 7097 | if (CheckDependentFunctionTemplateSpecialization(NewFD, TemplateArgs, |
| 7098 | Previous)) |
| 7099 | NewFD->setInvalidDecl(); |
| 7100 | } else if (isFunctionTemplateSpecialization) { |
| Douglas Gregor | 63fab34 | 2011-03-16 19:27:09 +0000 | [diff] [blame] | 7101 | if (CurContext->isDependentContext() && CurContext->isRecord() |
| Francois Pichet | b503705 | 2011-06-03 13:59:45 +0000 | [diff] [blame] | 7102 | && !isFriend) { |
| Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 7103 | isDependentClassScopeExplicitSpecialization = true; |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7104 | Diag(NewFD->getLocation(), getLangOpts().MicrosoftExt ? |
| Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 7105 | diag::ext_function_specialization_in_class : |
| 7106 | diag::err_function_specialization_in_class) |
| Douglas Gregor | 63fab34 | 2011-03-16 19:27:09 +0000 | [diff] [blame] | 7107 | << NewFD->getDeclName(); |
| Douglas Gregor | 63fab34 | 2011-03-16 19:27:09 +0000 | [diff] [blame] | 7108 | } else if (CheckFunctionTemplateSpecialization(NewFD, |
| 7109 | (HasExplicitTemplateArgs ? &TemplateArgs : 0), |
| 7110 | Previous)) |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7111 | NewFD->setInvalidDecl(); |
| Douglas Gregor | 781ba6e | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 7112 | |
| 7113 | // C++ [dcl.stc]p1: |
| 7114 | // A storage-class-specifier shall not be specified in an explicit |
| 7115 | // specialization (14.7.3) |
| Richard Trieu | b48e62f | 2013-05-16 02:14:08 +0000 | [diff] [blame] | 7116 | FunctionTemplateSpecializationInfo *Info = |
| 7117 | NewFD->getTemplateSpecializationInfo(); |
| 7118 | if (Info && SC != SC_None) { |
| 7119 | if (SC != Info->getTemplate()->getTemplatedDecl()->getStorageClass()) |
| Douglas Gregor | 84265a0 | 2011-06-17 05:09:08 +0000 | [diff] [blame] | 7120 | Diag(NewFD->getLocation(), |
| 7121 | diag::err_explicit_specialization_inconsistent_storage_class) |
| 7122 | << SC |
| 7123 | << FixItHint::CreateRemoval( |
| 7124 | D.getDeclSpec().getStorageClassSpecLoc()); |
| 7125 | |
| 7126 | else |
| 7127 | Diag(NewFD->getLocation(), |
| 7128 | diag::ext_explicit_specialization_storage_class) |
| 7129 | << FixItHint::CreateRemoval( |
| 7130 | D.getDeclSpec().getStorageClassSpecLoc()); |
| Douglas Gregor | 781ba6e | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 7131 | } |
| 7132 | |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7133 | } else if (isExplicitSpecialization && isa<CXXMethodDecl>(NewFD)) { |
| 7134 | if (CheckMemberSpecialization(NewFD, Previous)) |
| 7135 | NewFD->setInvalidDecl(); |
| 7136 | } |
| Douglas Gregor | f4f296d | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 7137 | |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7138 | // Perform semantic checking on the function declaration. |
| David Blaikie | d937bf1 | 2011-09-08 06:33:04 +0000 | [diff] [blame] | 7139 | if (!isDependentClassScopeExplicitSpecialization) { |
| David Majnemer | 027f9c4 | 2013-07-06 02:13:46 +0000 | [diff] [blame] | 7140 | if (!NewFD->isInvalidDecl() && NewFD->isMain()) |
| 7141 | CheckMain(NewFD, D.getDeclSpec()); |
| 7142 | |
| David Majnemer | c729b0b | 2013-09-16 22:44:20 +0000 | [diff] [blame] | 7143 | if (!NewFD->isInvalidDecl() && NewFD->isMSVCRTEntryPoint()) |
| 7144 | CheckMSVCRTEntryPoint(NewFD); |
| 7145 | |
| Nico Weber | 7607fce | 2013-12-21 00:49:51 +0000 | [diff] [blame] | 7146 | if (!NewFD->isInvalidDecl()) |
| Kaelyn Uhrain | 4dc695d | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 7147 | D.setRedeclaration(CheckFunctionDeclaration(S, NewFD, Previous, |
| 7148 | isExplicitSpecialization)); |
| David Blaikie | d937bf1 | 2011-09-08 06:33:04 +0000 | [diff] [blame] | 7149 | } |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7150 | |
| Kaelyn Uhrain | 4dc695d | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 7151 | assert((NewFD->isInvalidDecl() || !D.isRedeclaration() || |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7152 | Previous.getResultKind() != LookupResult::FoundOverloaded) && |
| 7153 | "previous declaration set still overloaded"); |
| 7154 | |
| 7155 | NamedDecl *PrincipalDecl = (FunctionTemplate |
| 7156 | ? cast<NamedDecl>(FunctionTemplate) |
| 7157 | : NewFD); |
| 7158 | |
| Kaelyn Uhrain | 4dc695d | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 7159 | if (isFriend && D.isRedeclaration()) { |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7160 | AccessSpecifier Access = AS_public; |
| 7161 | if (!NewFD->isInvalidDecl()) |
| Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 7162 | Access = NewFD->getPreviousDecl()->getAccess(); |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7163 | |
| 7164 | NewFD->setAccess(Access); |
| 7165 | if (FunctionTemplate) FunctionTemplate->setAccess(Access); |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7166 | } |
| 7167 | |
| 7168 | if (NewFD->isOverloadedOperator() && !DC->isRecord() && |
| 7169 | PrincipalDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary)) |
| 7170 | PrincipalDecl->setNonMemberOperator(); |
| 7171 | |
| 7172 | // If we have a function template, check the template parameter |
| 7173 | // list. This will check and merge default template arguments. |
| 7174 | if (FunctionTemplate) { |
| David Blaikie | 30d1544 | 2011-10-19 22:56:21 +0000 | [diff] [blame] | 7175 | FunctionTemplateDecl *PrevTemplate = |
| Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 7176 | FunctionTemplate->getPreviousDecl(); |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7177 | CheckTemplateParameterList(FunctionTemplate->getTemplateParameters(), |
| David Blaikie | 30d1544 | 2011-10-19 22:56:21 +0000 | [diff] [blame] | 7178 | PrevTemplate ? PrevTemplate->getTemplateParameters() : 0, |
| Douglas Gregor | a99fb4c | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 7179 | D.getDeclSpec().isFriendSpecified() |
| Kaelyn Uhrain | 4dc695d | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 7180 | ? (D.isFunctionDefinition() |
| Douglas Gregor | a99fb4c | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 7181 | ? TPC_FriendFunctionTemplateDefinition |
| 7182 | : TPC_FriendFunctionTemplate) |
| 7183 | : (D.getCXXScopeSpec().isSet() && |
| Douglas Gregor | 4d5c297 | 2011-02-04 12:22:53 +0000 | [diff] [blame] | 7184 | DC && DC->isRecord() && |
| 7185 | DC->isDependentContext()) |
| Douglas Gregor | a99fb4c | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 7186 | ? TPC_ClassTemplateMember |
| 7187 | : TPC_FunctionTemplate); |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7188 | } |
| 7189 | |
| 7190 | if (NewFD->isInvalidDecl()) { |
| 7191 | // Ignore all the rest of this. |
| Kaelyn Uhrain | 4dc695d | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 7192 | } else if (!D.isRedeclaration()) { |
| Kaelyn Uhrain | 8af2c9f | 2011-10-11 00:28:52 +0000 | [diff] [blame] | 7193 | struct ActOnFDArgs ExtraArgs = { S, D, TemplateParamLists, |
| Kaelyn Uhrain | 0a32fca | 2011-10-11 00:28:39 +0000 | [diff] [blame] | 7194 | AddToScope }; |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7195 | // Fake up an access specifier if it's supposed to be a class member. |
| 7196 | if (isa<CXXRecordDecl>(NewFD->getDeclContext())) |
| 7197 | NewFD->setAccess(AS_public); |
| 7198 | |
| 7199 | // Qualified decls generally require a previous declaration. |
| 7200 | if (D.getCXXScopeSpec().isSet()) { |
| 7201 | // ...with the major exception of templated-scope or |
| 7202 | // dependent-scope friend declarations. |
| 7203 | |
| 7204 | // TODO: we currently also suppress this check in dependent |
| 7205 | // contexts because (1) the parameter depth will be off when |
| 7206 | // matching friend templates and (2) we might actually be |
| 7207 | // selecting a friend based on a dependent factor. But there |
| 7208 | // are situations where these conditions don't apply and we |
| 7209 | // can actually do this check immediately. |
| 7210 | if (isFriend && |
| Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 7211 | (TemplateParamLists.size() || |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7212 | D.getCXXScopeSpec().getScopeRep()->isDependent() || |
| 7213 | CurContext->isDependentContext())) { |
| Chandler Carruth | f8b554f | 2011-08-19 01:38:33 +0000 | [diff] [blame] | 7214 | // ignore these |
| 7215 | } else { |
| 7216 | // The user tried to provide an out-of-line definition for a |
| 7217 | // function that is a member of a class or namespace, but there |
| 7218 | // was no such member function declared (C++ [class.mfct]p2, |
| 7219 | // C++ [namespace.memdef]p2). For example: |
| 7220 | // |
| 7221 | // class X { |
| 7222 | // void f() const; |
| 7223 | // }; |
| 7224 | // |
| 7225 | // void X::f() { } // ill-formed |
| 7226 | // |
| 7227 | // Complain about this problem, and attempt to suggest close |
| 7228 | // matches (e.g., those that differ only in cv-qualifiers and |
| 7229 | // whether the parameter types are references). |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7230 | |
| Richard Smith | 114394f | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 7231 | if (NamedDecl *Result = DiagnoseInvalidRedeclaration( |
| 7232 | *this, Previous, NewFD, ExtraArgs, false, 0)) { |
| Kaelyn Uhrain | 0a32fca | 2011-10-11 00:28:39 +0000 | [diff] [blame] | 7233 | AddToScope = ExtraArgs.AddToScope; |
| 7234 | return Result; |
| 7235 | } |
| Chandler Carruth | f8b554f | 2011-08-19 01:38:33 +0000 | [diff] [blame] | 7236 | } |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7237 | |
| 7238 | // Unqualified local friend declarations are required to resolve |
| 7239 | // to something. |
| Chandler Carruth | e92e42f | 2011-08-19 01:40:11 +0000 | [diff] [blame] | 7240 | } else if (isFriend && cast<CXXRecordDecl>(CurContext)->isLocalClass()) { |
| Richard Smith | 114394f | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 7241 | if (NamedDecl *Result = DiagnoseInvalidRedeclaration( |
| 7242 | *this, Previous, NewFD, ExtraArgs, true, S)) { |
| Kaelyn Uhrain | 0a32fca | 2011-10-11 00:28:39 +0000 | [diff] [blame] | 7243 | AddToScope = ExtraArgs.AddToScope; |
| 7244 | return Result; |
| 7245 | } |
| Chandler Carruth | e92e42f | 2011-08-19 01:40:11 +0000 | [diff] [blame] | 7246 | } |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7247 | |
| Richard Smith | a230224 | 2013-12-05 07:51:02 +0000 | [diff] [blame] | 7248 | } else if (!D.isFunctionDefinition() && |
| 7249 | isa<CXXMethodDecl>(NewFD) && NewFD->isOutOfLine() && |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7250 | !isFriend && !isFunctionTemplateSpecialization && |
| Alexis Hunt | 5a7fa25 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 7251 | !isExplicitSpecialization) { |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7252 | // An out-of-line member function declaration must also be a |
| Richard Smith | a230224 | 2013-12-05 07:51:02 +0000 | [diff] [blame] | 7253 | // definition (C++ [class.mfct]p2). |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7254 | // Note that this is not the case for explicit specializations of |
| 7255 | // function templates or member functions of class templates, per |
| David Blaikie | 30d1544 | 2011-10-19 22:56:21 +0000 | [diff] [blame] | 7256 | // C++ [temp.expl.spec]p2. We also allow these declarations as an |
| 7257 | // extension for compatibility with old SWIG code which likes to |
| 7258 | // generate them. |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7259 | Diag(NewFD->getLocation(), diag::ext_out_of_line_declaration) |
| 7260 | << D.getCXXScopeSpec().getRange(); |
| 7261 | } |
| 7262 | } |
| Ryan Flynn | e5dc859 | 2009-07-25 22:29:44 +0000 | [diff] [blame] | 7263 | |
| Rafael Espindola | de6a39f | 2013-03-02 21:41:48 +0000 | [diff] [blame] | 7264 | ProcessPragmaWeak(S, NewFD); |
| Rafael Espindola | f1d2f0e | 2013-01-16 23:11:15 +0000 | [diff] [blame] | 7265 | checkAttributesAfterMerging(*this, *NewFD); |
| 7266 | |
| Douglas Gregor | f4f296d | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 7267 | AddKnownFunctionAttributes(NewFD); |
| 7268 | |
| Douglas Gregor | 7260905 | 2010-08-06 13:50:58 +0000 | [diff] [blame] | 7269 | if (NewFD->hasAttr<OverloadableAttr>() && |
| 7270 | !NewFD->getType()->getAs<FunctionProtoType>()) { |
| 7271 | Diag(NewFD->getLocation(), |
| 7272 | diag::err_attribute_overloadable_no_prototype) |
| 7273 | << NewFD; |
| 7274 | |
| 7275 | // Turn this into a variadic function with no parameters. |
| 7276 | const FunctionType *FT = NewFD->getType()->getAs<FunctionType>(); |
| Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 7277 | FunctionProtoType::ExtProtoInfo EPI( |
| 7278 | Context.getDefaultCallingConvention(true, false)); |
| John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 7279 | EPI.Variadic = true; |
| 7280 | EPI.ExtInfo = FT->getExtInfo(); |
| 7281 | |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 7282 | QualType R = Context.getFunctionType(FT->getReturnType(), None, EPI); |
| Douglas Gregor | 7260905 | 2010-08-06 13:50:58 +0000 | [diff] [blame] | 7283 | NewFD->setType(R); |
| 7284 | } |
| 7285 | |
| Eli Friedman | 570024a | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 7286 | // If there's a #pragma GCC visibility in scope, and this isn't a class |
| 7287 | // member, set the visibility of this function. |
| Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 7288 | if (!DC->isRecord() && NewFD->isExternallyVisible()) |
| Eli Friedman | 570024a | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 7289 | AddPushedVisibilityAttribute(NewFD); |
| 7290 | |
| John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 7291 | // If there's a #pragma clang arc_cf_code_audited in scope, consider |
| 7292 | // marking the function. |
| 7293 | AddCFAuditedAttribute(NewFD); |
| 7294 | |
| Richard Smith | ac974a3 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 7295 | // If this is the first declaration of an extern C variable, update |
| 7296 | // the map of such variables. |
| Rafael Espindola | 3f9e444 | 2013-10-19 02:13:21 +0000 | [diff] [blame] | 7297 | if (NewFD->isFirstDecl() && !NewFD->isInvalidDecl() && |
| Richard Smith | ac974a3 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 7298 | isIncompleteDeclExternC(*this, NewFD)) |
| Richard Smith | 39b7968 | 2013-06-18 20:15:12 +0000 | [diff] [blame] | 7299 | RegisterLocallyScopedExternCDecl(NewFD, S); |
| Douglas Gregor | f4f296d | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 7300 | |
| Argyrios Kyrtzidis | 16eecc4 | 2009-06-25 18:22:24 +0000 | [diff] [blame] | 7301 | // Set this FunctionDecl's range up to the right paren. |
| Abramo Bagnara | ea94788 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 7302 | NewFD->setRangeEnd(D.getSourceRange().getEnd()); |
| Argyrios Kyrtzidis | 16eecc4 | 2009-06-25 18:22:24 +0000 | [diff] [blame] | 7303 | |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7304 | if (getLangOpts().CPlusPlus) { |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7305 | if (FunctionTemplate) { |
| 7306 | if (NewFD->isInvalidDecl()) |
| 7307 | FunctionTemplate->setInvalidDecl(); |
| 7308 | return FunctionTemplate; |
| 7309 | } |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7310 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7311 | |
| Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 7312 | if (NewFD->hasAttr<OpenCLKernelAttr>()) { |
| Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 7313 | // OpenCL v1.2 s6.8 static is invalid for kernel functions. |
| 7314 | if ((getLangOpts().OpenCLVersion >= 120) |
| 7315 | && (SC == SC_Static)) { |
| 7316 | Diag(D.getIdentifierLoc(), diag::err_static_kernel); |
| 7317 | D.setInvalidType(); |
| 7318 | } |
| Tanya Lattner | 0f86433 | 2013-01-30 19:48:52 +0000 | [diff] [blame] | 7319 | |
| 7320 | // OpenCL v1.2, s6.9 -- Kernels can only have return type void. |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 7321 | if (!NewFD->getReturnType()->isVoidType()) { |
| Tanya Lattner | 0f86433 | 2013-01-30 19:48:52 +0000 | [diff] [blame] | 7322 | Diag(D.getIdentifierLoc(), |
| 7323 | diag::err_expected_kernel_void_return_type); |
| 7324 | D.setInvalidType(); |
| 7325 | } |
| Matt Arsenault | efb3819 | 2013-07-23 01:23:36 +0000 | [diff] [blame] | 7326 | |
| 7327 | llvm::SmallPtrSet<const Type *, 16> ValidTypes; |
| Aaron Ballman | f6bf62e | 2014-03-07 15:12:56 +0000 | [diff] [blame] | 7328 | for (auto Param : NewFD->params()) |
| Matt Arsenault | efb3819 | 2013-07-23 01:23:36 +0000 | [diff] [blame] | 7329 | checkIsValidOpenCLKernelParameter(*this, D, Param, ValidTypes); |
| Tanya Lattner | 4fdce3f | 2012-06-19 23:09:52 +0000 | [diff] [blame] | 7330 | } |
| 7331 | |
| Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 7332 | MarkUnusedFileScopedDecl(NewFD); |
| Peter Collingbourne | 9e2c81f | 2011-02-09 21:04:32 +0000 | [diff] [blame] | 7333 | |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7334 | if (getLangOpts().CUDA) |
| Peter Collingbourne | 9e2c81f | 2011-02-09 21:04:32 +0000 | [diff] [blame] | 7335 | if (IdentifierInfo *II = NewFD->getIdentifier()) |
| 7336 | if (!NewFD->isInvalidDecl() && |
| 7337 | NewFD->getDeclContext()->getRedeclContext()->isTranslationUnit()) { |
| 7338 | if (II->isStr("cudaConfigureCall")) { |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 7339 | if (!R->getAs<FunctionType>()->getReturnType()->isScalarType()) |
| Peter Collingbourne | 9e2c81f | 2011-02-09 21:04:32 +0000 | [diff] [blame] | 7340 | Diag(NewFD->getLocation(), diag::err_config_scalar_return); |
| 7341 | |
| 7342 | Context.setcudaConfigureCallDecl(NewFD); |
| 7343 | } |
| 7344 | } |
| Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 7345 | |
| 7346 | // Here we have an function template explicit specialization at class scope. |
| 7347 | // The actually specialization will be postponed to template instatiation |
| 7348 | // time via the ClassScopeFunctionSpecializationDecl node. |
| 7349 | if (isDependentClassScopeExplicitSpecialization) { |
| 7350 | ClassScopeFunctionSpecializationDecl *NewSpec = |
| 7351 | ClassScopeFunctionSpecializationDecl::Create( |
| Nico Weber | 7b5a716 | 2012-06-25 17:21:05 +0000 | [diff] [blame] | 7352 | Context, CurContext, SourceLocation(), |
| 7353 | cast<CXXMethodDecl>(NewFD), |
| 7354 | HasExplicitTemplateArgs, TemplateArgs); |
| Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 7355 | CurContext->addDecl(NewSpec); |
| 7356 | AddToScope = false; |
| 7357 | } |
| Peter Collingbourne | 9e2c81f | 2011-02-09 21:04:32 +0000 | [diff] [blame] | 7358 | |
| Douglas Gregor | f4f296d | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 7359 | return NewFD; |
| 7360 | } |
| 7361 | |
| 7362 | /// \brief Perform semantic checking of a new function declaration. |
| 7363 | /// |
| 7364 | /// Performs semantic analysis of the new function declaration |
| 7365 | /// NewFD. This routine performs all semantic checking that does not |
| 7366 | /// require the actual declarator involved in the declaration, and is |
| 7367 | /// used both for the declaration of functions as they are parsed |
| 7368 | /// (called via ActOnDeclarator) and for the declaration of functions |
| 7369 | /// that have been instantiated via C++ template instantiation (called |
| 7370 | /// via InstantiateDecl). |
| 7371 | /// |
| James Dennett | ffad8b7 | 2012-06-22 08:10:18 +0000 | [diff] [blame] | 7372 | /// \param IsExplicitSpecialization whether this new function declaration is |
| Douglas Gregor | cf91555 | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 7373 | /// an explicit specialization of the previous declaration. |
| 7374 | /// |
| Chris Lattner | f6d1c9c | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 7375 | /// This sets NewFD->isInvalidDecl() to true if there was an error. |
| Kaelyn Uhrain | 4dc695d | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 7376 | /// |
| James Dennett | ffad8b7 | 2012-06-22 08:10:18 +0000 | [diff] [blame] | 7377 | /// \returns true if the function declaration is a redeclaration. |
| Kaelyn Uhrain | 4dc695d | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 7378 | bool Sema::CheckFunctionDeclaration(Scope *S, FunctionDecl *NewFD, |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7379 | LookupResult &Previous, |
| Kaelyn Uhrain | 4dc695d | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 7380 | bool IsExplicitSpecialization) { |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 7381 | assert(!NewFD->getReturnType()->isVariablyModifiedType() && |
| 7382 | "Variably modified return types are not handled here"); |
| John McCall | d9baf6a | 2009-07-24 03:03:21 +0000 | [diff] [blame] | 7383 | |
| Richard Smith | 1c34fb7 | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 7384 | // Determine whether the type of this function should be merged with |
| 7385 | // a previous visible declaration. This never happens for functions in C++, |
| 7386 | // and always happens in C if the previous declaration was visible. |
| 7387 | bool MergeTypeWithPrevious = !getLangOpts().CPlusPlus && |
| 7388 | !Previous.isShadowed(); |
| 7389 | |
| Douglas Gregor | 3552dab | 2013-01-09 00:47:56 +0000 | [diff] [blame] | 7390 | // Filter out any non-conflicting previous declarations. |
| 7391 | filterNonConflictingPreviousDecls(Context, NewFD, Previous); |
| 7392 | |
| Kaelyn Uhrain | 4dc695d | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 7393 | bool Redeclaration = false; |
| Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 7394 | NamedDecl *OldDecl = 0; |
| Kaelyn Uhrain | 4dc695d | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 7395 | |
| Douglas Gregor | e62c0a4 | 2009-02-24 01:23:02 +0000 | [diff] [blame] | 7396 | // Merge or overload the declaration with an existing declaration of |
| 7397 | // the same name, if appropriate. |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7398 | if (!Previous.empty()) { |
| Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 7399 | // Determine whether NewFD is an overload of PrevDecl or |
| Zhongxing Xu | bece5d6 | 2009-01-16 01:13:29 +0000 | [diff] [blame] | 7400 | // a declaration that requires merging. If it's an overload, |
| 7401 | // there's no more work to do here; we'll just add the new |
| 7402 | // function to the scope. |
| John McCall | daa3d6b | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 7403 | if (!AllowOverloadingOfFunction(Previous, Context)) { |
| Rafael Espindola | 5bddd6a | 2013-04-15 12:49:13 +0000 | [diff] [blame] | 7404 | NamedDecl *Candidate = Previous.getFoundDecl(); |
| 7405 | if (shouldLinkPossiblyHiddenDecl(Candidate, NewFD)) { |
| 7406 | Redeclaration = true; |
| 7407 | OldDecl = Candidate; |
| 7408 | } |
| John McCall | daa3d6b | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 7409 | } else { |
| John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 7410 | switch (CheckOverload(S, NewFD, Previous, OldDecl, |
| 7411 | /*NewIsUsingDecl*/ false)) { |
| John McCall | daa3d6b | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 7412 | case Ovl_Match: |
| John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7413 | Redeclaration = true; |
| John McCall | daa3d6b | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 7414 | break; |
| 7415 | |
| 7416 | case Ovl_NonFunction: |
| 7417 | Redeclaration = true; |
| 7418 | break; |
| 7419 | |
| 7420 | case Ovl_Overload: |
| 7421 | Redeclaration = false; |
| 7422 | break; |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7423 | } |
| Peter Collingbourne | 9f2a990 | 2011-01-21 02:08:54 +0000 | [diff] [blame] | 7424 | |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7425 | if (!getLangOpts().CPlusPlus && !NewFD->hasAttr<OverloadableAttr>()) { |
| Peter Collingbourne | 9f2a990 | 2011-01-21 02:08:54 +0000 | [diff] [blame] | 7426 | // If a function name is overloadable in C, then every function |
| 7427 | // with that name must be marked "overloadable". |
| 7428 | Diag(NewFD->getLocation(), diag::err_attribute_overloadable_missing) |
| 7429 | << Redeclaration << NewFD; |
| 7430 | NamedDecl *OverloadedDecl = 0; |
| 7431 | if (Redeclaration) |
| 7432 | OverloadedDecl = OldDecl; |
| 7433 | else if (!Previous.empty()) |
| 7434 | OverloadedDecl = Previous.getRepresentativeDecl(); |
| 7435 | if (OverloadedDecl) |
| 7436 | Diag(OverloadedDecl->getLocation(), |
| 7437 | diag::note_attribute_overloadable_prev_overload); |
| Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 7438 | NewFD->addAttr(OverloadableAttr::CreateImplicit(Context)); |
| Peter Collingbourne | 9f2a990 | 2011-01-21 02:08:54 +0000 | [diff] [blame] | 7439 | } |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7440 | } |
| Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 7441 | } |
| Zhongxing Xu | bece5d6 | 2009-01-16 01:13:29 +0000 | [diff] [blame] | 7442 | |
| Richard Smith | ac974a3 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 7443 | // Check for a previous extern "C" declaration with this name. |
| 7444 | if (!Redeclaration && |
| 7445 | checkForConflictWithNonVisibleExternC(*this, NewFD, Previous)) { |
| 7446 | filterNonConflictingPreviousDecls(Context, NewFD, Previous); |
| 7447 | if (!Previous.empty()) { |
| 7448 | // This is an extern "C" declaration with the same name as a previous |
| 7449 | // declaration, and thus redeclares that entity... |
| 7450 | Redeclaration = true; |
| 7451 | OldDecl = Previous.getFoundDecl(); |
| Richard Smith | 1c34fb7 | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 7452 | MergeTypeWithPrevious = false; |
| Richard Smith | ac974a3 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 7453 | |
| 7454 | // ... except in the presence of __attribute__((overloadable)). |
| 7455 | if (OldDecl->hasAttr<OverloadableAttr>()) { |
| 7456 | if (!getLangOpts().CPlusPlus && !NewFD->hasAttr<OverloadableAttr>()) { |
| 7457 | Diag(NewFD->getLocation(), diag::err_attribute_overloadable_missing) |
| 7458 | << Redeclaration << NewFD; |
| 7459 | Diag(Previous.getFoundDecl()->getLocation(), |
| 7460 | diag::note_attribute_overloadable_prev_overload); |
| Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 7461 | NewFD->addAttr(OverloadableAttr::CreateImplicit(Context)); |
| Richard Smith | ac974a3 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 7462 | } |
| 7463 | if (IsOverload(NewFD, cast<FunctionDecl>(OldDecl), false)) { |
| 7464 | Redeclaration = false; |
| 7465 | OldDecl = 0; |
| 7466 | } |
| 7467 | } |
| 7468 | } |
| 7469 | } |
| 7470 | |
| Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 7471 | // C++11 [dcl.constexpr]p8: |
| 7472 | // A constexpr specifier for a non-static member function that is not |
| 7473 | // a constructor declares that member function to be const. |
| 7474 | // |
| 7475 | // This needs to be delayed until we know whether this is an out-of-line |
| 7476 | // definition of a static member function. |
| Richard Smith | 034185c | 2013-04-21 01:08:50 +0000 | [diff] [blame] | 7477 | // |
| 7478 | // This rule is not present in C++1y, so we produce a backwards |
| 7479 | // compatibility warning whenever it happens in C++11. |
| Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 7480 | CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(NewFD); |
| Richard Smith | 034185c | 2013-04-21 01:08:50 +0000 | [diff] [blame] | 7481 | if (!getLangOpts().CPlusPlus1y && MD && MD->isConstexpr() && |
| 7482 | !MD->isStatic() && !isa<CXXConstructorDecl>(MD) && |
| Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 7483 | (MD->getTypeQualifiers() & Qualifiers::Const) == 0) { |
| Alp Toker | a2794f9 | 2014-01-22 07:29:52 +0000 | [diff] [blame] | 7484 | CXXMethodDecl *OldMD = 0; |
| 7485 | if (OldDecl) |
| 7486 | OldMD = dyn_cast<CXXMethodDecl>(OldDecl->getAsFunction()); |
| Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 7487 | if (!OldMD || !OldMD->isStatic()) { |
| 7488 | const FunctionProtoType *FPT = |
| 7489 | MD->getType()->castAs<FunctionProtoType>(); |
| 7490 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 7491 | EPI.TypeQuals |= Qualifiers::Const; |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 7492 | MD->setType(Context.getFunctionType(FPT->getReturnType(), |
| Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 7493 | FPT->getParamTypes(), EPI)); |
| Richard Smith | 034185c | 2013-04-21 01:08:50 +0000 | [diff] [blame] | 7494 | |
| 7495 | // Warn that we did this, if we're not performing template instantiation. |
| 7496 | // In that case, we'll have warned already when the template was defined. |
| 7497 | if (ActiveTemplateInstantiations.empty()) { |
| 7498 | SourceLocation AddConstLoc; |
| 7499 | if (FunctionTypeLoc FTL = MD->getTypeSourceInfo()->getTypeLoc() |
| 7500 | .IgnoreParens().getAs<FunctionTypeLoc>()) |
| 7501 | AddConstLoc = PP.getLocForEndOfToken(FTL.getRParenLoc()); |
| 7502 | |
| 7503 | Diag(MD->getLocation(), diag::warn_cxx1y_compat_constexpr_not_const) |
| 7504 | << FixItHint::CreateInsertion(AddConstLoc, " const"); |
| 7505 | } |
| Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 7506 | } |
| 7507 | } |
| 7508 | |
| 7509 | if (Redeclaration) { |
| 7510 | // NewFD and OldDecl represent declarations that need to be |
| 7511 | // merged. |
| Richard Smith | 1c34fb7 | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 7512 | if (MergeFunctionDecl(NewFD, OldDecl, S, MergeTypeWithPrevious)) { |
| Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 7513 | NewFD->setInvalidDecl(); |
| 7514 | return Redeclaration; |
| 7515 | } |
| 7516 | |
| 7517 | Previous.clear(); |
| 7518 | Previous.addDecl(OldDecl); |
| 7519 | |
| 7520 | if (FunctionTemplateDecl *OldTemplateDecl |
| 7521 | = dyn_cast<FunctionTemplateDecl>(OldDecl)) { |
| 7522 | NewFD->setPreviousDeclaration(OldTemplateDecl->getTemplatedDecl()); |
| 7523 | FunctionTemplateDecl *NewTemplateDecl |
| 7524 | = NewFD->getDescribedFunctionTemplate(); |
| 7525 | assert(NewTemplateDecl && "Template/non-template mismatch"); |
| 7526 | if (CXXMethodDecl *Method |
| 7527 | = dyn_cast<CXXMethodDecl>(NewTemplateDecl->getTemplatedDecl())) { |
| 7528 | Method->setAccess(OldTemplateDecl->getAccess()); |
| 7529 | NewTemplateDecl->setAccess(OldTemplateDecl->getAccess()); |
| Kaelyn Uhrain | 4dc695d | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 7530 | } |
| Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 7531 | |
| 7532 | // If this is an explicit specialization of a member that is a function |
| 7533 | // template, mark it as a member specialization. |
| 7534 | if (IsExplicitSpecialization && |
| 7535 | NewTemplateDecl->getInstantiatedFromMemberTemplate()) { |
| 7536 | NewTemplateDecl->setMemberSpecialization(); |
| 7537 | assert(OldTemplateDecl->isMemberSpecialization()); |
| Argyrios Kyrtzidis | 11a846a | 2009-07-14 03:18:53 +0000 | [diff] [blame] | 7538 | } |
| Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 7539 | |
| 7540 | } else { |
| John McCall | 6bd2a89 | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 7541 | // This needs to happen first so that 'inline' propagates. |
| Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 7542 | NewFD->setPreviousDeclaration(cast<FunctionDecl>(OldDecl)); |
| John McCall | 6bd2a89 | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 7543 | |
| 7544 | if (isa<CXXMethodDecl>(NewFD)) { |
| 7545 | // A valid redeclaration of a C++ method must be out-of-line, |
| 7546 | // but (unfortunately) it's not necessarily a definition |
| 7547 | // because of templates, which means that the previous |
| 7548 | // declaration is not necessarily from the class definition. |
| 7549 | |
| 7550 | // For just setting the access, that doesn't matter. |
| 7551 | CXXMethodDecl *oldMethod = cast<CXXMethodDecl>(OldDecl); |
| 7552 | NewFD->setAccess(oldMethod->getAccess()); |
| 7553 | |
| 7554 | // Update the key-function state if necessary for this ABI. |
| 7555 | if (NewFD->isInlined() && |
| 7556 | !Context.getTargetInfo().getCXXABI().canKeyFunctionBeInline()) { |
| 7557 | // setNonKeyFunction needs to work with the original |
| 7558 | // declaration from the class definition, and isVirtual() is |
| 7559 | // just faster in that case, so map back to that now. |
| Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 7560 | oldMethod = cast<CXXMethodDecl>(oldMethod->getFirstDecl()); |
| John McCall | 6bd2a89 | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 7561 | if (oldMethod->isVirtual()) { |
| 7562 | Context.setNonKeyFunction(oldMethod); |
| 7563 | } |
| 7564 | } |
| 7565 | } |
| Zhongxing Xu | bece5d6 | 2009-01-16 01:13:29 +0000 | [diff] [blame] | 7566 | } |
| Douglas Gregor | 8af63e4 | 2009-02-06 17:46:57 +0000 | [diff] [blame] | 7567 | } |
| Zhongxing Xu | bece5d6 | 2009-01-16 01:13:29 +0000 | [diff] [blame] | 7568 | |
| Anders Carlsson | 1b12ed4 | 2009-09-13 21:33:06 +0000 | [diff] [blame] | 7569 | // Semantic checking for this function declaration (in isolation). |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7570 | if (getLangOpts().CPlusPlus) { |
| Anders Carlsson | 1b12ed4 | 2009-09-13 21:33:06 +0000 | [diff] [blame] | 7571 | // C++-specific checks. |
| 7572 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(NewFD)) { |
| 7573 | CheckConstructor(Constructor); |
| Anders Carlsson | 2a50e95 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 7574 | } else if (CXXDestructorDecl *Destructor = |
| 7575 | dyn_cast<CXXDestructorDecl>(NewFD)) { |
| 7576 | CXXRecordDecl *Record = Destructor->getParent(); |
| Anders Carlsson | 1b12ed4 | 2009-09-13 21:33:06 +0000 | [diff] [blame] | 7577 | QualType ClassType = Context.getTypeDeclType(Record); |
| Anders Carlsson | 2a50e95 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 7578 | |
| Douglas Gregor | 7454c56 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 7579 | // FIXME: Shouldn't we be able to perform this check even when the class |
| Anders Carlsson | 2a50e95 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 7580 | // type is dependent? Both gcc and edg can handle that. |
| Anders Carlsson | 1b12ed4 | 2009-09-13 21:33:06 +0000 | [diff] [blame] | 7581 | if (!ClassType->isDependentType()) { |
| 7582 | DeclarationName Name |
| 7583 | = Context.DeclarationNames.getCXXDestructorName( |
| 7584 | Context.getCanonicalType(ClassType)); |
| 7585 | if (NewFD->getDeclName() != Name) { |
| 7586 | Diag(NewFD->getLocation(), diag::err_destructor_name); |
| Kaelyn Uhrain | 4dc695d | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 7587 | NewFD->setInvalidDecl(); |
| 7588 | return Redeclaration; |
| Anders Carlsson | 1b12ed4 | 2009-09-13 21:33:06 +0000 | [diff] [blame] | 7589 | } |
| 7590 | } |
| Anders Carlsson | 1b12ed4 | 2009-09-13 21:33:06 +0000 | [diff] [blame] | 7591 | } else if (CXXConversionDecl *Conversion |
| Douglas Gregor | 21920e37 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 7592 | = dyn_cast<CXXConversionDecl>(NewFD)) { |
| Anders Carlsson | 1b12ed4 | 2009-09-13 21:33:06 +0000 | [diff] [blame] | 7593 | ActOnConversionDeclarator(Conversion); |
| Douglas Gregor | 21920e37 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 7594 | } |
| 7595 | |
| 7596 | // Find any virtual functions that this function overrides. |
| Douglas Gregor | 6be3de3 | 2009-12-01 17:35:23 +0000 | [diff] [blame] | 7597 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(NewFD)) { |
| 7598 | if (!Method->isFunctionTemplateSpecialization() && |
| Argyrios Kyrtzidis | cc4ca0a | 2012-10-09 01:23:45 +0000 | [diff] [blame] | 7599 | !Method->getDescribedFunctionTemplate() && |
| 7600 | Method->isCanonicalDecl()) { |
| Douglas Gregor | 5a2bb5b | 2010-10-13 22:55:32 +0000 | [diff] [blame] | 7601 | if (AddOverriddenMethods(Method->getParent(), Method)) { |
| 7602 | // If the function was marked as "static", we have a problem. |
| 7603 | if (NewFD->getStorageClass() == SC_Static) { |
| David Blaikie | 7e41426 | 2012-10-17 00:47:58 +0000 | [diff] [blame] | 7604 | ReportOverrides(*this, diag::err_static_overrides_virtual, Method); |
| Douglas Gregor | 5a2bb5b | 2010-10-13 22:55:32 +0000 | [diff] [blame] | 7605 | } |
| Argyrios Kyrtzidis | 7272d9c | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 7606 | } |
| Douglas Gregor | 5a2bb5b | 2010-10-13 22:55:32 +0000 | [diff] [blame] | 7607 | } |
| Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 7608 | |
| 7609 | if (Method->isStatic()) |
| 7610 | checkThisInStaticMemberFunctionType(Method); |
| Douglas Gregor | 6be3de3 | 2009-12-01 17:35:23 +0000 | [diff] [blame] | 7611 | } |
| Anders Carlsson | 1b12ed4 | 2009-09-13 21:33:06 +0000 | [diff] [blame] | 7612 | |
| 7613 | // Extra checking for C++ overloaded operators (C++ [over.oper]). |
| 7614 | if (NewFD->isOverloadedOperator() && |
| Kaelyn Uhrain | 4dc695d | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 7615 | CheckOverloadedOperatorDeclaration(NewFD)) { |
| 7616 | NewFD->setInvalidDecl(); |
| 7617 | return Redeclaration; |
| 7618 | } |
| Alexis Hunt | c88db06 | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 7619 | |
| 7620 | // Extra checking for C++0x literal operators (C++0x [over.literal]). |
| 7621 | if (NewFD->getLiteralIdentifier() && |
| Kaelyn Uhrain | 4dc695d | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 7622 | CheckLiteralOperatorDeclaration(NewFD)) { |
| 7623 | NewFD->setInvalidDecl(); |
| 7624 | return Redeclaration; |
| 7625 | } |
| Alexis Hunt | c88db06 | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 7626 | |
| Anders Carlsson | 1b12ed4 | 2009-09-13 21:33:06 +0000 | [diff] [blame] | 7627 | // In C++, check default arguments now that we have merged decls. Unless |
| 7628 | // the lexical context is the class, because in this case this is done |
| 7629 | // during delayed parsing anyway. |
| 7630 | if (!CurContext->isRecord()) |
| 7631 | CheckCXXDefaultArguments(NewFD); |
| Warren Hunt | 445d83e | 2013-11-01 23:46:51 +0000 | [diff] [blame] | 7632 | |
| Douglas Gregor | 9246b68 | 2010-12-21 19:47:46 +0000 | [diff] [blame] | 7633 | // If this function declares a builtin function, check the type of this |
| 7634 | // declaration against the expected type for the builtin. |
| 7635 | if (unsigned BuiltinID = NewFD->getBuiltinID()) { |
| 7636 | ASTContext::GetBuiltinTypeError Error; |
| Fariborz Jahanian | feb9ae5 | 2013-01-05 21:54:55 +0000 | [diff] [blame] | 7637 | LookupPredefedObjCSuperType(*this, S, NewFD->getIdentifier()); |
| Douglas Gregor | 9246b68 | 2010-12-21 19:47:46 +0000 | [diff] [blame] | 7638 | QualType T = Context.GetBuiltinType(BuiltinID, Error); |
| 7639 | if (!T.isNull() && !Context.hasSameType(T, NewFD->getType())) { |
| 7640 | // The type of this function differs from the type of the builtin, |
| 7641 | // so forget about the builtin entirely. |
| 7642 | Context.BuiltinInfo.ForgetBuiltin(BuiltinID, Context.Idents); |
| 7643 | } |
| 7644 | } |
| Warren Hunt | 445d83e | 2013-11-01 23:46:51 +0000 | [diff] [blame] | 7645 | |
| Aaron Ballman | c2a9493a | 2012-02-09 01:21:34 +0000 | [diff] [blame] | 7646 | // If this function is declared as being extern "C", then check to see if |
| 7647 | // the function returns a UDT (class, struct, or union type) that is not C |
| 7648 | // compatible, and if it does, warn the user. |
| Fariborz Jahanian | 95236b5 | 2013-03-14 23:09:00 +0000 | [diff] [blame] | 7649 | // But, issue any diagnostic on the first declaration only. |
| 7650 | if (NewFD->isExternC() && Previous.empty()) { |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 7651 | QualType R = NewFD->getReturnType(); |
| Hans Wennborg | 84ce606 | 2012-07-24 17:59:41 +0000 | [diff] [blame] | 7652 | if (R->isIncompleteType() && !R->isVoidType()) |
| 7653 | Diag(NewFD->getLocation(), diag::warn_return_value_udt_incomplete) |
| 7654 | << NewFD << R; |
| Douglas Gregor | 847cea7 | 2012-08-07 06:14:34 +0000 | [diff] [blame] | 7655 | else if (!R.isPODType(Context) && !R->isVoidType() && |
| 7656 | !R->isObjCObjectPointerType()) |
| Hans Wennborg | 84ce606 | 2012-07-24 17:59:41 +0000 | [diff] [blame] | 7657 | Diag(NewFD->getLocation(), diag::warn_return_value_udt) << NewFD << R; |
| Aaron Ballman | c2a9493a | 2012-02-09 01:21:34 +0000 | [diff] [blame] | 7658 | } |
| Anders Carlsson | 1b12ed4 | 2009-09-13 21:33:06 +0000 | [diff] [blame] | 7659 | } |
| Kaelyn Uhrain | 4dc695d | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 7660 | return Redeclaration; |
| Zhongxing Xu | bece5d6 | 2009-01-16 01:13:29 +0000 | [diff] [blame] | 7661 | } |
| 7662 | |
| Dmitri Gribenko | ae73417 | 2013-01-17 00:26:13 +0000 | [diff] [blame] | 7663 | static SourceRange getResultSourceRange(const FunctionDecl *FD) { |
| 7664 | const TypeSourceInfo *TSI = FD->getTypeSourceInfo(); |
| 7665 | if (!TSI) |
| 7666 | return SourceRange(); |
| 7667 | |
| 7668 | TypeLoc TL = TSI->getTypeLoc(); |
| David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 7669 | FunctionTypeLoc FunctionTL = TL.getAs<FunctionTypeLoc>(); |
| Dmitri Gribenko | ae73417 | 2013-01-17 00:26:13 +0000 | [diff] [blame] | 7670 | if (!FunctionTL) |
| 7671 | return SourceRange(); |
| 7672 | |
| Alp Toker | 42a16a6 | 2014-01-25 23:51:36 +0000 | [diff] [blame] | 7673 | TypeLoc ResultTL = FunctionTL.getReturnLoc(); |
| David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 7674 | if (ResultTL.getUnqualifiedLoc().getAs<BuiltinTypeLoc>()) |
| Dmitri Gribenko | ae73417 | 2013-01-17 00:26:13 +0000 | [diff] [blame] | 7675 | return ResultTL.getSourceRange(); |
| 7676 | |
| 7677 | return SourceRange(); |
| 7678 | } |
| 7679 | |
| David Blaikie | d937bf1 | 2011-09-08 06:33:04 +0000 | [diff] [blame] | 7680 | void Sema::CheckMain(FunctionDecl* FD, const DeclSpec& DS) { |
| Richard Smith | b63b6ee | 2014-01-22 01:43:19 +0000 | [diff] [blame] | 7681 | // C++11 [basic.start.main]p3: |
| 7682 | // A program that [...] declares main to be inline, static or |
| 7683 | // constexpr is ill-formed. |
| Richard Smith | 0015f09 | 2013-01-17 22:16:11 +0000 | [diff] [blame] | 7684 | // C11 6.7.4p4: In a hosted environment, no function specifier(s) shall |
| 7685 | // appear in a declaration of main. |
| John McCall | 02dee0a | 2009-07-25 04:36:53 +0000 | [diff] [blame] | 7686 | // static main is not an error under C99, but we should warn about it. |
| Richard Smith | 0015f09 | 2013-01-17 22:16:11 +0000 | [diff] [blame] | 7687 | // We accept _Noreturn main as an extension. |
| David Blaikie | d937bf1 | 2011-09-08 06:33:04 +0000 | [diff] [blame] | 7688 | if (FD->getStorageClass() == SC_Static) |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7689 | Diag(DS.getStorageClassSpecLoc(), getLangOpts().CPlusPlus |
| David Blaikie | d937bf1 | 2011-09-08 06:33:04 +0000 | [diff] [blame] | 7690 | ? diag::err_static_main : diag::warn_static_main) |
| 7691 | << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc()); |
| 7692 | if (FD->isInlineSpecified()) |
| 7693 | Diag(DS.getInlineSpecLoc(), diag::err_inline_main) |
| 7694 | << FixItHint::CreateRemoval(DS.getInlineSpecLoc()); |
| Dmitri Gribenko | 7ec6f3d | 2013-01-21 11:25:03 +0000 | [diff] [blame] | 7695 | if (DS.isNoreturnSpecified()) { |
| 7696 | SourceLocation NoreturnLoc = DS.getNoreturnSpecLoc(); |
| 7697 | SourceRange NoreturnRange(NoreturnLoc, |
| 7698 | PP.getLocForEndOfToken(NoreturnLoc)); |
| 7699 | Diag(NoreturnLoc, diag::ext_noreturn_main); |
| 7700 | Diag(NoreturnLoc, diag::note_main_remove_noreturn) |
| 7701 | << FixItHint::CreateRemoval(NoreturnRange); |
| 7702 | } |
| Richard Smith | 3f333f2 | 2012-02-04 06:10:17 +0000 | [diff] [blame] | 7703 | if (FD->isConstexpr()) { |
| 7704 | Diag(DS.getConstexprSpecLoc(), diag::err_constexpr_main) |
| 7705 | << FixItHint::CreateRemoval(DS.getConstexprSpecLoc()); |
| 7706 | FD->setConstexpr(false); |
| 7707 | } |
| John McCall | 02dee0a | 2009-07-25 04:36:53 +0000 | [diff] [blame] | 7708 | |
| Joey Gouly | a7310a8 | 2013-11-05 12:30:39 +0000 | [diff] [blame] | 7709 | if (getLangOpts().OpenCL) { |
| 7710 | Diag(FD->getLocation(), diag::err_opencl_no_main) |
| 7711 | << FD->hasAttr<OpenCLKernelAttr>(); |
| 7712 | FD->setInvalidDecl(); |
| 7713 | return; |
| 7714 | } |
| 7715 | |
| John McCall | 02dee0a | 2009-07-25 04:36:53 +0000 | [diff] [blame] | 7716 | QualType T = FD->getType(); |
| 7717 | assert(T->isFunctionType() && "function decl is not of function type"); |
| John McCall | 5ed3caf | 2012-02-14 19:50:52 +0000 | [diff] [blame] | 7718 | const FunctionType* FT = T->castAs<FunctionType>(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7719 | |
| John McCall | 5ed3caf | 2012-02-14 19:50:52 +0000 | [diff] [blame] | 7720 | // All the standards say that main() should should return 'int'. |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 7721 | if (Context.hasSameUnqualifiedType(FT->getReturnType(), Context.IntTy)) { |
| John McCall | 5ed3caf | 2012-02-14 19:50:52 +0000 | [diff] [blame] | 7722 | // In C and C++, main magically returns 0 if you fall off the end; |
| 7723 | // set the flag which tells us that. |
| 7724 | // This is C++ [basic.start.main]p5 and C99 5.1.2.2.3. |
| 7725 | FD->setHasImplicitReturnZero(true); |
| 7726 | |
| 7727 | // In C with GNU extensions we allow main() to have non-integer return |
| 7728 | // type, but we should warn about the extension, and we disable the |
| 7729 | // implicit-return-zero rule. |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7730 | } else if (getLangOpts().GNUMode && !getLangOpts().CPlusPlus) { |
| John McCall | 5ed3caf | 2012-02-14 19:50:52 +0000 | [diff] [blame] | 7731 | Diag(FD->getTypeSpecStartLoc(), diag::ext_main_returns_nonint); |
| 7732 | |
| Dmitri Gribenko | ae73417 | 2013-01-17 00:26:13 +0000 | [diff] [blame] | 7733 | SourceRange ResultRange = getResultSourceRange(FD); |
| 7734 | if (ResultRange.isValid()) |
| 7735 | Diag(ResultRange.getBegin(), diag::note_main_change_return_type) |
| 7736 | << FixItHint::CreateReplacement(ResultRange, "int"); |
| 7737 | |
| John McCall | 5ed3caf | 2012-02-14 19:50:52 +0000 | [diff] [blame] | 7738 | // Otherwise, this is just a flat-out error. |
| 7739 | } else { |
| Dmitri Gribenko | ae73417 | 2013-01-17 00:26:13 +0000 | [diff] [blame] | 7740 | SourceRange ResultRange = getResultSourceRange(FD); |
| 7741 | if (ResultRange.isValid()) |
| 7742 | Diag(FD->getTypeSpecStartLoc(), diag::err_main_returns_nonint) |
| 7743 | << FixItHint::CreateReplacement(ResultRange, "int"); |
| 7744 | else |
| 7745 | Diag(FD->getTypeSpecStartLoc(), diag::err_main_returns_nonint); |
| 7746 | |
| John McCall | 02dee0a | 2009-07-25 04:36:53 +0000 | [diff] [blame] | 7747 | FD->setInvalidDecl(true); |
| 7748 | } |
| 7749 | |
| 7750 | // Treat protoless main() as nullary. |
| 7751 | if (isa<FunctionNoProtoType>(FT)) return; |
| 7752 | |
| 7753 | const FunctionProtoType* FTP = cast<const FunctionProtoType>(FT); |
| Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 7754 | unsigned nparams = FTP->getNumParams(); |
| John McCall | 02dee0a | 2009-07-25 04:36:53 +0000 | [diff] [blame] | 7755 | assert(FD->getNumParams() == nparams); |
| 7756 | |
| John McCall | 0e21fcc | 2009-12-24 09:58:38 +0000 | [diff] [blame] | 7757 | bool HasExtraParameters = (nparams > 3); |
| 7758 | |
| 7759 | // Darwin passes an undocumented fourth argument of type char**. If |
| 7760 | // other platforms start sprouting these, the logic below will start |
| 7761 | // getting shifty. |
| Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 7762 | if (nparams == 4 && Context.getTargetInfo().getTriple().isOSDarwin()) |
| John McCall | 0e21fcc | 2009-12-24 09:58:38 +0000 | [diff] [blame] | 7763 | HasExtraParameters = false; |
| 7764 | |
| 7765 | if (HasExtraParameters) { |
| John McCall | 02dee0a | 2009-07-25 04:36:53 +0000 | [diff] [blame] | 7766 | Diag(FD->getLocation(), diag::err_main_surplus_args) << nparams; |
| 7767 | FD->setInvalidDecl(true); |
| 7768 | nparams = 3; |
| 7769 | } |
| 7770 | |
| 7771 | // FIXME: a lot of the following diagnostics would be improved |
| 7772 | // if we had some location information about types. |
| 7773 | |
| 7774 | QualType CharPP = |
| 7775 | Context.getPointerType(Context.getPointerType(Context.CharTy)); |
| John McCall | 0e21fcc | 2009-12-24 09:58:38 +0000 | [diff] [blame] | 7776 | QualType Expected[] = { Context.IntTy, CharPP, CharPP, CharPP }; |
| John McCall | 02dee0a | 2009-07-25 04:36:53 +0000 | [diff] [blame] | 7777 | |
| 7778 | for (unsigned i = 0; i < nparams; ++i) { |
| Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 7779 | QualType AT = FTP->getParamType(i); |
| John McCall | 02dee0a | 2009-07-25 04:36:53 +0000 | [diff] [blame] | 7780 | |
| 7781 | bool mismatch = true; |
| 7782 | |
| 7783 | if (Context.hasSameUnqualifiedType(AT, Expected[i])) |
| 7784 | mismatch = false; |
| 7785 | else if (Expected[i] == CharPP) { |
| 7786 | // As an extension, the following forms are okay: |
| 7787 | // char const ** |
| 7788 | // char const * const * |
| 7789 | // char * const * |
| 7790 | |
| John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 7791 | QualifierCollector qs; |
| John McCall | 02dee0a | 2009-07-25 04:36:53 +0000 | [diff] [blame] | 7792 | const PointerType* PT; |
| Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 7793 | if ((PT = qs.strip(AT)->getAs<PointerType>()) && |
| 7794 | (PT = qs.strip(PT->getPointeeType())->getAs<PointerType>()) && |
| Richard Smith | 685cef6 | 2013-01-29 02:49:47 +0000 | [diff] [blame] | 7795 | Context.hasSameType(QualType(qs.strip(PT->getPointeeType()), 0), |
| 7796 | Context.CharTy)) { |
| John McCall | 02dee0a | 2009-07-25 04:36:53 +0000 | [diff] [blame] | 7797 | qs.removeConst(); |
| 7798 | mismatch = !qs.empty(); |
| 7799 | } |
| 7800 | } |
| 7801 | |
| 7802 | if (mismatch) { |
| 7803 | Diag(FD->getLocation(), diag::err_main_arg_wrong) << i << Expected[i]; |
| 7804 | // TODO: suggest replacing given type with expected type |
| 7805 | FD->setInvalidDecl(true); |
| 7806 | } |
| 7807 | } |
| 7808 | |
| 7809 | if (nparams == 1 && !FD->isInvalidDecl()) { |
| 7810 | Diag(FD->getLocation(), diag::warn_main_one_arg); |
| 7811 | } |
| Douglas Gregor | bff6203 | 2010-10-21 16:57:46 +0000 | [diff] [blame] | 7812 | |
| 7813 | if (!FD->isInvalidDecl() && FD->getDescribedFunctionTemplate()) { |
| Aaron Ballman | 6d086d7 | 2014-01-03 02:20:27 +0000 | [diff] [blame] | 7814 | Diag(FD->getLocation(), diag::err_mainlike_template_decl) << FD; |
| David Majnemer | c729b0b | 2013-09-16 22:44:20 +0000 | [diff] [blame] | 7815 | FD->setInvalidDecl(); |
| 7816 | } |
| 7817 | } |
| 7818 | |
| 7819 | void Sema::CheckMSVCRTEntryPoint(FunctionDecl *FD) { |
| 7820 | QualType T = FD->getType(); |
| 7821 | assert(T->isFunctionType() && "function decl is not of function type"); |
| 7822 | const FunctionType *FT = T->castAs<FunctionType>(); |
| 7823 | |
| 7824 | // Set an implicit return of 'zero' if the function can return some integral, |
| 7825 | // enumeration, pointer or nullptr type. |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 7826 | if (FT->getReturnType()->isIntegralOrEnumerationType() || |
| 7827 | FT->getReturnType()->isAnyPointerType() || |
| 7828 | FT->getReturnType()->isNullPtrType()) |
| David Majnemer | c729b0b | 2013-09-16 22:44:20 +0000 | [diff] [blame] | 7829 | // DllMain is exempt because a return value of zero means it failed. |
| 7830 | if (FD->getName() != "DllMain") |
| 7831 | FD->setHasImplicitReturnZero(true); |
| 7832 | |
| 7833 | if (!FD->isInvalidDecl() && FD->getDescribedFunctionTemplate()) { |
| Aaron Ballman | 6d086d7 | 2014-01-03 02:20:27 +0000 | [diff] [blame] | 7834 | Diag(FD->getLocation(), diag::err_mainlike_template_decl) << FD; |
| Douglas Gregor | bff6203 | 2010-10-21 16:57:46 +0000 | [diff] [blame] | 7835 | FD->setInvalidDecl(); |
| 7836 | } |
| John McCall | d9baf6a | 2009-07-24 03:03:21 +0000 | [diff] [blame] | 7837 | } |
| 7838 | |
| Eli Friedman | d5a55bd | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 7839 | bool Sema::CheckForConstantInitializer(Expr *Init, QualType DclT) { |
| Eli Friedman | 2c7bd6b | 2009-02-27 04:17:12 +0000 | [diff] [blame] | 7840 | // FIXME: Need strict checking. In C89, we need to check for |
| 7841 | // any assignment, increment, decrement, function-calls, or |
| 7842 | // commas outside of a sizeof. In C99, it's the same list, |
| 7843 | // except that the aforementioned are allowed in unevaluated |
| 7844 | // expressions. Everything else falls under the |
| 7845 | // "may accept other forms of constant expressions" exception. |
| 7846 | // (We never end up here for C++, so the constant expression |
| 7847 | // rules there don't matter.) |
| John McCall | 8b0f4ff | 2010-08-02 21:13:48 +0000 | [diff] [blame] | 7848 | if (Init->isConstantInitializer(Context, false)) |
| Eli Friedman | 7bfab36 | 2009-02-22 06:45:27 +0000 | [diff] [blame] | 7849 | return false; |
| Eli Friedman | 4f294cf | 2009-02-26 04:47:58 +0000 | [diff] [blame] | 7850 | Diag(Init->getExprLoc(), diag::err_init_element_not_constant) |
| 7851 | << Init->getSourceRange(); |
| Eli Friedman | d5a55bd | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 7852 | return true; |
| Steve Naroff | 98f7203 | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 7853 | } |
| 7854 | |
| Chandler Carruth | 33bf3e7 | 2011-03-27 09:46:56 +0000 | [diff] [blame] | 7855 | namespace { |
| 7856 | // Visits an initialization expression to see if OrigDecl is evaluated in |
| 7857 | // its own initialization and throws a warning if it does. |
| 7858 | class SelfReferenceChecker |
| 7859 | : public EvaluatedExprVisitor<SelfReferenceChecker> { |
| 7860 | Sema &S; |
| 7861 | Decl *OrigDecl; |
| Richard Trieu | a04ad1a | 2011-09-01 21:44:13 +0000 | [diff] [blame] | 7862 | bool isRecordType; |
| 7863 | bool isPODType; |
| Hans Wennborg | e1fdb05 | 2012-08-17 10:12:33 +0000 | [diff] [blame] | 7864 | bool isReferenceType; |
| Chandler Carruth | 33bf3e7 | 2011-03-27 09:46:56 +0000 | [diff] [blame] | 7865 | |
| 7866 | public: |
| 7867 | typedef EvaluatedExprVisitor<SelfReferenceChecker> Inherited; |
| 7868 | |
| 7869 | SelfReferenceChecker(Sema &S, Decl *OrigDecl) : Inherited(S.Context), |
| Richard Trieu | a04ad1a | 2011-09-01 21:44:13 +0000 | [diff] [blame] | 7870 | S(S), OrigDecl(OrigDecl) { |
| 7871 | isPODType = false; |
| 7872 | isRecordType = false; |
| Hans Wennborg | e1fdb05 | 2012-08-17 10:12:33 +0000 | [diff] [blame] | 7873 | isReferenceType = false; |
| Richard Trieu | a04ad1a | 2011-09-01 21:44:13 +0000 | [diff] [blame] | 7874 | if (ValueDecl *VD = dyn_cast<ValueDecl>(OrigDecl)) { |
| 7875 | isPODType = VD->getType().isPODType(S.Context); |
| 7876 | isRecordType = VD->getType()->isRecordType(); |
| Hans Wennborg | e1fdb05 | 2012-08-17 10:12:33 +0000 | [diff] [blame] | 7877 | isReferenceType = VD->getType()->isReferenceType(); |
| Richard Trieu | a04ad1a | 2011-09-01 21:44:13 +0000 | [diff] [blame] | 7878 | } |
| 7879 | } |
| Chandler Carruth | 33bf3e7 | 2011-03-27 09:46:56 +0000 | [diff] [blame] | 7880 | |
| Richard Trieu | 64c51ab | 2012-05-09 00:21:34 +0000 | [diff] [blame] | 7881 | // For most expressions, the cast is directly above the DeclRefExpr. |
| 7882 | // For conditional operators, the cast can be outside the conditional |
| 7883 | // operator if both expressions are DeclRefExpr's. |
| 7884 | void HandleValue(Expr *E) { |
| Richard Trieu | 3267347 | 2012-10-01 17:39:51 +0000 | [diff] [blame] | 7885 | if (isReferenceType) |
| 7886 | return; |
| Richard Trieu | 64c51ab | 2012-05-09 00:21:34 +0000 | [diff] [blame] | 7887 | E = E->IgnoreParenImpCasts(); |
| 7888 | if (DeclRefExpr* DRE = dyn_cast<DeclRefExpr>(E)) { |
| 7889 | HandleDeclRefExpr(DRE); |
| 7890 | return; |
| 7891 | } |
| 7892 | |
| 7893 | if (ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E)) { |
| 7894 | HandleValue(CO->getTrueExpr()); |
| 7895 | HandleValue(CO->getFalseExpr()); |
| Richard Trieu | 742c6ed | 2012-10-03 00:41:36 +0000 | [diff] [blame] | 7896 | return; |
| 7897 | } |
| 7898 | |
| 7899 | if (isa<MemberExpr>(E)) { |
| 7900 | Expr *Base = E->IgnoreParenImpCasts(); |
| 7901 | while (MemberExpr *ME = dyn_cast<MemberExpr>(Base)) { |
| 7902 | // Check for static member variables and don't warn on them. |
| 7903 | if (!isa<FieldDecl>(ME->getMemberDecl())) |
| 7904 | return; |
| 7905 | Base = ME->getBase()->IgnoreParenImpCasts(); |
| 7906 | } |
| 7907 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Base)) |
| 7908 | HandleDeclRefExpr(DRE); |
| 7909 | return; |
| Richard Trieu | 64c51ab | 2012-05-09 00:21:34 +0000 | [diff] [blame] | 7910 | } |
| 7911 | } |
| 7912 | |
| Richard Trieu | 3267347 | 2012-10-01 17:39:51 +0000 | [diff] [blame] | 7913 | // Reference types are handled here since all uses of references are |
| 7914 | // bad, not just r-value uses. |
| 7915 | void VisitDeclRefExpr(DeclRefExpr *E) { |
| 7916 | if (isReferenceType) |
| 7917 | HandleDeclRefExpr(E); |
| 7918 | } |
| 7919 | |
| Richard Trieu | 64c51ab | 2012-05-09 00:21:34 +0000 | [diff] [blame] | 7920 | void VisitImplicitCastExpr(ImplicitCastExpr *E) { |
| Richard Trieu | 742c6ed | 2012-10-03 00:41:36 +0000 | [diff] [blame] | 7921 | if (E->getCastKind() == CK_LValueToRValue || |
| Richard Trieu | 64c51ab | 2012-05-09 00:21:34 +0000 | [diff] [blame] | 7922 | (isRecordType && E->getCastKind() == CK_NoOp)) |
| 7923 | HandleValue(E->getSubExpr()); |
| 7924 | |
| 7925 | Inherited::VisitImplicitCastExpr(E); |
| Chandler Carruth | 33bf3e7 | 2011-03-27 09:46:56 +0000 | [diff] [blame] | 7926 | } |
| 7927 | |
| Richard Trieu | a04ad1a | 2011-09-01 21:44:13 +0000 | [diff] [blame] | 7928 | void VisitMemberExpr(MemberExpr *E) { |
| Richard Trieu | 64c51ab | 2012-05-09 00:21:34 +0000 | [diff] [blame] | 7929 | // Don't warn on arrays since they can be treated as pointers. |
| Richard Trieu | aa5e256 | 2011-09-07 00:58:53 +0000 | [diff] [blame] | 7930 | if (E->getType()->canDecayToPointerType()) return; |
| Richard Trieu | 64c51ab | 2012-05-09 00:21:34 +0000 | [diff] [blame] | 7931 | |
| Richard Trieu | 742c6ed | 2012-10-03 00:41:36 +0000 | [diff] [blame] | 7932 | // Warn when a non-static method call is followed by non-static member |
| 7933 | // field accesses, which is followed by a DeclRefExpr. |
| 7934 | CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(E->getMemberDecl()); |
| 7935 | bool Warn = (MD && !MD->isStatic()); |
| 7936 | Expr *Base = E->getBase()->IgnoreParenImpCasts(); |
| 7937 | while (MemberExpr *ME = dyn_cast<MemberExpr>(Base)) { |
| 7938 | if (!isa<FieldDecl>(ME->getMemberDecl())) |
| 7939 | Warn = false; |
| 7940 | Base = ME->getBase()->IgnoreParenImpCasts(); |
| 7941 | } |
| Richard Trieu | a04ad1a | 2011-09-01 21:44:13 +0000 | [diff] [blame] | 7942 | |
| Richard Trieu | 742c6ed | 2012-10-03 00:41:36 +0000 | [diff] [blame] | 7943 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Base)) { |
| 7944 | if (Warn) |
| 7945 | HandleDeclRefExpr(DRE); |
| 7946 | return; |
| 7947 | } |
| 7948 | |
| 7949 | // The base of a MemberExpr is not a MemberExpr or a DeclRefExpr. |
| 7950 | // Visit that expression. |
| 7951 | Visit(Base); |
| Chandler Carruth | 33bf3e7 | 2011-03-27 09:46:56 +0000 | [diff] [blame] | 7952 | } |
| 7953 | |
| Richard Trieu | 8fbd91d | 2013-03-26 03:41:40 +0000 | [diff] [blame] | 7954 | void VisitCXXOperatorCallExpr(CXXOperatorCallExpr *E) { |
| 7955 | if (E->getNumArgs() > 0) |
| 7956 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E->getArg(0))) |
| 7957 | HandleDeclRefExpr(DRE); |
| 7958 | |
| 7959 | Inherited::VisitCXXOperatorCallExpr(E); |
| 7960 | } |
| 7961 | |
| Richard Trieu | a04ad1a | 2011-09-01 21:44:13 +0000 | [diff] [blame] | 7962 | void VisitUnaryOperator(UnaryOperator *E) { |
| 7963 | // For POD record types, addresses of its own members are well-defined. |
| Richard Trieu | 742c6ed | 2012-10-03 00:41:36 +0000 | [diff] [blame] | 7964 | if (E->getOpcode() == UO_AddrOf && isRecordType && |
| 7965 | isa<MemberExpr>(E->getSubExpr()->IgnoreParens())) { |
| 7966 | if (!isPODType) |
| 7967 | HandleValue(E->getSubExpr()); |
| 7968 | return; |
| 7969 | } |
| Richard Trieu | a04ad1a | 2011-09-01 21:44:13 +0000 | [diff] [blame] | 7970 | Inherited::VisitUnaryOperator(E); |
| Richard Smith | fa11fd6 | 2013-05-03 19:16:22 +0000 | [diff] [blame] | 7971 | } |
| Richard Trieu | 64c51ab | 2012-05-09 00:21:34 +0000 | [diff] [blame] | 7972 | |
| 7973 | void VisitObjCMessageExpr(ObjCMessageExpr *E) { return; } |
| 7974 | |
| Richard Trieu | a04ad1a | 2011-09-01 21:44:13 +0000 | [diff] [blame] | 7975 | void HandleDeclRefExpr(DeclRefExpr *DRE) { |
| NAKAMURA Takumi | 3fef3ef | 2013-01-19 01:54:35 +0000 | [diff] [blame] | 7976 | Decl* ReferenceDecl = DRE->getDecl(); |
| Chandler Carruth | 33bf3e7 | 2011-03-27 09:46:56 +0000 | [diff] [blame] | 7977 | if (OrigDecl != ReferenceDecl) return; |
| Ted Kremenek | a83b407 | 2013-01-19 04:33:14 +0000 | [diff] [blame] | 7978 | unsigned diag; |
| 7979 | if (isReferenceType) { |
| 7980 | diag = diag::warn_uninit_self_reference_in_reference_init; |
| 7981 | } else if (cast<VarDecl>(OrigDecl)->isStaticLocal()) { |
| 7982 | diag = diag::warn_static_self_reference_in_init; |
| 7983 | } else { |
| 7984 | diag = diag::warn_uninit_self_reference_in_init; |
| 7985 | } |
| 7986 | |
| Richard Trieu | a04ad1a | 2011-09-01 21:44:13 +0000 | [diff] [blame] | 7987 | S.DiagRuntimeBehavior(DRE->getLocStart(), DRE, |
| Hans Wennborg | d799a2b | 2012-08-20 08:52:22 +0000 | [diff] [blame] | 7988 | S.PDiag(diag) |
| Hans Wennborg | 61b2ffa | 2012-09-21 08:58:33 +0000 | [diff] [blame] | 7989 | << DRE->getNameInfo().getName() |
| Douglas Gregor | fcafc6e | 2011-05-24 16:02:01 +0000 | [diff] [blame] | 7990 | << OrigDecl->getLocation() |
| Richard Trieu | a04ad1a | 2011-09-01 21:44:13 +0000 | [diff] [blame] | 7991 | << DRE->getSourceRange()); |
| Chandler Carruth | 33bf3e7 | 2011-03-27 09:46:56 +0000 | [diff] [blame] | 7992 | } |
| 7993 | }; |
| Chandler Carruth | 33bf3e7 | 2011-03-27 09:46:56 +0000 | [diff] [blame] | 7994 | |
| Richard Trieu | 3267347 | 2012-10-01 17:39:51 +0000 | [diff] [blame] | 7995 | /// CheckSelfReference - Warns if OrigDecl is used in expression E. |
| 7996 | static void CheckSelfReference(Sema &S, Decl* OrigDecl, Expr *E, |
| 7997 | bool DirectInit) { |
| 7998 | // Parameters arguments are occassionially constructed with itself, |
| 7999 | // for instance, in recursive functions. Skip them. |
| 8000 | if (isa<ParmVarDecl>(OrigDecl)) |
| 8001 | return; |
| 8002 | |
| 8003 | E = E->IgnoreParens(); |
| 8004 | |
| 8005 | // Skip checking T a = a where T is not a record or reference type. |
| 8006 | // Doing so is a way to silence uninitialized warnings. |
| 8007 | if (!DirectInit && !cast<VarDecl>(OrigDecl)->getType()->isRecordType()) |
| 8008 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) |
| 8009 | if (ICE->getCastKind() == CK_LValueToRValue) |
| 8010 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr())) |
| 8011 | if (DRE->getDecl() == OrigDecl) |
| 8012 | return; |
| 8013 | |
| 8014 | SelfReferenceChecker(S, OrigDecl).Visit(E); |
| 8015 | } |
| Richard Trieu | a04ad1a | 2011-09-01 21:44:13 +0000 | [diff] [blame] | 8016 | } |
| 8017 | |
| Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 8018 | /// AddInitializerToDecl - Adds the initializer Init to the |
| 8019 | /// declaration dcl. If DirectInit is true, this is C++ direct |
| 8020 | /// initialization rather than copy initialization. |
| Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 8021 | void Sema::AddInitializerToDecl(Decl *RealDecl, Expr *Init, |
| 8022 | bool DirectInit, bool TypeMayContainAuto) { |
| Chris Lattner | 8beb9de | 2007-10-19 20:10:30 +0000 | [diff] [blame] | 8023 | // If there is no declaration, there was an error parsing it. Just ignore |
| 8024 | // the initializer. |
| Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 8025 | if (RealDecl == 0 || RealDecl->isInvalidDecl()) |
| Chris Lattner | 8beb9de | 2007-10-19 20:10:30 +0000 | [diff] [blame] | 8026 | return; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8027 | |
| Douglas Gregor | 0c88030 | 2009-03-11 23:00:04 +0000 | [diff] [blame] | 8028 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(RealDecl)) { |
| 8029 | // With declarators parsed the way they are, the parser cannot |
| 8030 | // distinguish between a normal initializer and a pure-specifier. |
| 8031 | // Thus this grotesque test. |
| 8032 | IntegerLiteral *IL; |
| Douglas Gregor | 0c88030 | 2009-03-11 23:00:04 +0000 | [diff] [blame] | 8033 | if ((IL = dyn_cast<IntegerLiteral>(Init)) && IL->getValue() == 0 && |
| Douglas Gregor | 21920e37 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 8034 | Context.getCanonicalType(IL->getType()) == Context.IntTy) |
| 8035 | CheckPureMethod(Method, Init->getSourceRange()); |
| 8036 | else { |
| Douglas Gregor | 0c88030 | 2009-03-11 23:00:04 +0000 | [diff] [blame] | 8037 | Diag(Method->getLocation(), diag::err_member_function_initialization) |
| 8038 | << Method->getDeclName() << Init->getSourceRange(); |
| 8039 | Method->setInvalidDecl(); |
| 8040 | } |
| 8041 | return; |
| 8042 | } |
| 8043 | |
| Steve Naroff | 437b4d8 | 2007-09-12 20:13:48 +0000 | [diff] [blame] | 8044 | VarDecl *VDecl = dyn_cast<VarDecl>(RealDecl); |
| 8045 | if (!VDecl) { |
| Richard Smith | 4a4beec | 2011-06-12 11:43:46 +0000 | [diff] [blame] | 8046 | assert(!isa<FieldDecl>(RealDecl) && "field init shouldn't get here"); |
| 8047 | Diag(RealDecl->getLocation(), diag::err_illegal_initializer); |
| Steve Naroff | 437b4d8 | 2007-09-12 20:13:48 +0000 | [diff] [blame] | 8048 | RealDecl->setInvalidDecl(); |
| 8049 | return; |
| Eli Friedman | 2c7bd6b | 2009-02-27 04:17:12 +0000 | [diff] [blame] | 8050 | } |
| Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 8051 | ParenListExpr *CXXDirectInit = dyn_cast<ParenListExpr>(Init); |
| 8052 | |
| Richard Smith | 0cc8578 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8053 | // C++11 [decl.spec.auto]p6. Deduce the type which 'auto' stands in for. |
| Richard Smith | 27d807c | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 8054 | if (TypeMayContainAuto && VDecl->getType()->isUndeducedType()) { |
| Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 8055 | Expr *DeduceInit = Init; |
| 8056 | // Initializer could be a C++ direct-initializer. Deduction only works if it |
| 8057 | // contains exactly one expression. |
| 8058 | if (CXXDirectInit) { |
| 8059 | if (CXXDirectInit->getNumExprs() == 0) { |
| 8060 | // It isn't possible to write this directly, but it is possible to |
| 8061 | // end up in this situation with "auto x(some_pack...);" |
| Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 8062 | Diag(CXXDirectInit->getLocStart(), |
| Richard Smith | bb13c9a | 2013-09-28 04:02:39 +0000 | [diff] [blame] | 8063 | VDecl->isInitCapture() ? diag::err_init_capture_no_expression |
| 8064 | : diag::err_auto_var_init_no_expression) |
| Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 8065 | << VDecl->getDeclName() << VDecl->getType() |
| 8066 | << VDecl->getSourceRange(); |
| 8067 | RealDecl->setInvalidDecl(); |
| 8068 | return; |
| 8069 | } else if (CXXDirectInit->getNumExprs() > 1) { |
| Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 8070 | Diag(CXXDirectInit->getExpr(1)->getLocStart(), |
| Richard Smith | bb13c9a | 2013-09-28 04:02:39 +0000 | [diff] [blame] | 8071 | VDecl->isInitCapture() |
| 8072 | ? diag::err_init_capture_multiple_expressions |
| 8073 | : diag::err_auto_var_init_multiple_expressions) |
| Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 8074 | << VDecl->getDeclName() << VDecl->getType() |
| 8075 | << VDecl->getSourceRange(); |
| 8076 | RealDecl->setInvalidDecl(); |
| 8077 | return; |
| 8078 | } else { |
| 8079 | DeduceInit = CXXDirectInit->getExpr(0); |
| 8080 | } |
| 8081 | } |
| Douglas Gregor | b5af2e9 | 2013-03-07 22:57:58 +0000 | [diff] [blame] | 8082 | |
| 8083 | // Expressions default to 'id' when we're in a debugger. |
| 8084 | bool DefaultedToAuto = false; |
| 8085 | if (getLangOpts().DebuggerCastResultToId && |
| 8086 | Init->getType() == Context.UnknownAnyTy) { |
| 8087 | ExprResult Result = forceUnknownAnyToType(Init, Context.getObjCIdType()); |
| 8088 | if (Result.isInvalid()) { |
| 8089 | VDecl->setInvalidDecl(); |
| 8090 | return; |
| 8091 | } |
| 8092 | Init = Result.take(); |
| 8093 | DefaultedToAuto = true; |
| 8094 | } |
| Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 8095 | |
| 8096 | QualType DeducedType; |
| Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 8097 | if (DeduceAutoType(VDecl->getTypeSourceInfo(), DeduceInit, DeducedType) == |
| Sebastian Redl | 09edce0 | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 8098 | DAR_Failed) |
| Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 8099 | DiagnoseAutoDeductionFailure(VDecl, DeduceInit); |
| Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 8100 | if (DeducedType.isNull()) { |
| Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 8101 | RealDecl->setInvalidDecl(); |
| 8102 | return; |
| 8103 | } |
| Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 8104 | VDecl->setType(DeducedType); |
| Rafael Espindola | 0e0d009 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 8105 | assert(VDecl->isLinkageValid()); |
| Rafael Espindola | bf5c33b | 2013-03-12 21:06:00 +0000 | [diff] [blame] | 8106 | |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8107 | // In ARC, infer lifetime. |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8108 | if (getLangOpts().ObjCAutoRefCount && inferObjCARCLifetime(VDecl)) |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8109 | VDecl->setInvalidDecl(); |
| 8110 | |
| Jordan Rose | d8d5669 | 2012-06-08 22:46:07 +0000 | [diff] [blame] | 8111 | // Warn if we deduced 'id'. 'auto' usually implies type-safety, but using |
| 8112 | // 'id' instead of a specific object type prevents most of our usual checks. |
| 8113 | // We only want to warn outside of template instantiations, though: |
| 8114 | // inside a template, the 'id' could have come from a parameter. |
| Douglas Gregor | b5af2e9 | 2013-03-07 22:57:58 +0000 | [diff] [blame] | 8115 | if (ActiveTemplateInstantiations.empty() && !DefaultedToAuto && |
| Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 8116 | DeducedType->isObjCIdType()) { |
| 8117 | SourceLocation Loc = |
| 8118 | VDecl->getTypeSourceInfo()->getTypeLoc().getBeginLoc(); |
| Jordan Rose | d8d5669 | 2012-06-08 22:46:07 +0000 | [diff] [blame] | 8119 | Diag(Loc, diag::warn_auto_var_is_id) |
| 8120 | << VDecl->getDeclName() << DeduceInit->getSourceRange(); |
| 8121 | } |
| 8122 | |
| Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 8123 | // If this is a redeclaration, check that the type we just deduced matches |
| 8124 | // the previously declared type. |
| Richard Smith | 1c34fb7 | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 8125 | if (VarDecl *Old = VDecl->getPreviousDecl()) { |
| 8126 | // We never need to merge the type, because we cannot form an incomplete |
| 8127 | // array of auto, nor deduce such a type. |
| 8128 | MergeVarDeclTypes(VDecl, Old, /*MergeTypeWithPrevious*/false); |
| 8129 | } |
| Richard Smith | 27d807c | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 8130 | |
| 8131 | // Check the deduced type is valid for a variable declaration. |
| 8132 | CheckVariableDeclarationType(VDecl); |
| 8133 | if (VDecl->isInvalidDecl()) |
| 8134 | return; |
| Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 8135 | } |
| Richard Smith | 0cc8578 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8136 | |
| Nico Rieck | 8e9791f | 2014-02-26 21:27:13 +0000 | [diff] [blame] | 8137 | // dllimport cannot be used on variable definitions. |
| 8138 | if (VDecl->hasAttr<DLLImportAttr>() && !VDecl->isStaticDataMember()) { |
| 8139 | Diag(VDecl->getLocation(), diag::err_attribute_dllimport_data_definition); |
| 8140 | VDecl->setInvalidDecl(); |
| 8141 | return; |
| 8142 | } |
| 8143 | |
| Richard Smith | 0cc8578 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8144 | if (VDecl->isLocalVarDecl() && VDecl->hasExternalStorage()) { |
| 8145 | // C99 6.7.8p5. C++ has no such restriction, but that is a defect. |
| 8146 | Diag(VDecl->getLocation(), diag::err_block_extern_cant_init); |
| 8147 | VDecl->setInvalidDecl(); |
| 8148 | return; |
| 8149 | } |
| 8150 | |
| Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 8151 | if (!VDecl->getType()->isDependentType()) { |
| 8152 | // A definition must end up with a complete type, which means it must be |
| 8153 | // complete with the restriction that an array type might be completed by |
| 8154 | // the initializer; note that later code assumes this restriction. |
| 8155 | QualType BaseDeclType = VDecl->getType(); |
| 8156 | if (const ArrayType *Array = Context.getAsIncompleteArrayType(BaseDeclType)) |
| 8157 | BaseDeclType = Array->getElementType(); |
| 8158 | if (RequireCompleteType(VDecl->getLocation(), BaseDeclType, |
| 8159 | diag::err_typecheck_decl_incomplete_type)) { |
| 8160 | RealDecl->setInvalidDecl(); |
| 8161 | return; |
| 8162 | } |
| Douglas Gregor | f0f8369 | 2010-08-24 05:27:49 +0000 | [diff] [blame] | 8163 | |
| Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 8164 | // The variable can not have an abstract class type. |
| 8165 | if (RequireNonAbstractType(VDecl->getLocation(), VDecl->getType(), |
| 8166 | diag::err_abstract_type_in_decl, |
| 8167 | AbstractVariableType)) |
| 8168 | VDecl->setInvalidDecl(); |
| Eli Friedman | 337cd3a | 2009-04-13 21:28:54 +0000 | [diff] [blame] | 8169 | } |
| 8170 | |
| Sebastian Redl | 5ca7984 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 8171 | const VarDecl *Def; |
| 8172 | if ((Def = VDecl->getDefinition()) && Def != VDecl) { |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8173 | Diag(VDecl->getLocation(), diag::err_redefinition) |
| Douglas Gregor | 0760fa1 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 8174 | << VDecl->getDeclName(); |
| 8175 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 8176 | VDecl->setInvalidDecl(); |
| 8177 | return; |
| 8178 | } |
| Douglas Gregor | f0f8369 | 2010-08-24 05:27:49 +0000 | [diff] [blame] | 8179 | |
| Douglas Gregor | f0f8369 | 2010-08-24 05:27:49 +0000 | [diff] [blame] | 8180 | const VarDecl* PrevInit = 0; |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8181 | if (getLangOpts().CPlusPlus) { |
| Douglas Gregor | 71f39c9 | 2010-12-16 01:31:22 +0000 | [diff] [blame] | 8182 | // C++ [class.static.data]p4 |
| 8183 | // If a static data member is of const integral or const |
| 8184 | // enumeration type, its declaration in the class definition can |
| 8185 | // specify a constant-initializer which shall be an integral |
| 8186 | // constant expression (5.19). In that case, the member can appear |
| 8187 | // in integral constant expressions. The member shall still be |
| 8188 | // defined in a namespace scope if it is used in the program and the |
| 8189 | // namespace scope definition shall not contain an initializer. |
| 8190 | // |
| 8191 | // We already performed a redefinition check above, but for static |
| 8192 | // data members we also need to check whether there was an in-class |
| 8193 | // declaration with an initializer. |
| 8194 | if (VDecl->isStaticDataMember() && VDecl->getAnyInitializer(PrevInit)) { |
| Hans Wennborg | 84fe12d | 2013-11-21 03:17:44 +0000 | [diff] [blame] | 8195 | Diag(Init->getExprLoc(), diag::err_static_data_member_reinitialization) |
| 8196 | << VDecl->getDeclName(); |
| 8197 | Diag(PrevInit->getInit()->getExprLoc(), diag::note_previous_initializer) << 0; |
| Douglas Gregor | 71f39c9 | 2010-12-16 01:31:22 +0000 | [diff] [blame] | 8198 | return; |
| 8199 | } |
| Douglas Gregor | 0760fa1 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 8200 | |
| Douglas Gregor | 71f39c9 | 2010-12-16 01:31:22 +0000 | [diff] [blame] | 8201 | if (VDecl->hasLocalStorage()) |
| 8202 | getCurFunction()->setHasBranchProtectedScope(); |
| 8203 | |
| 8204 | if (DiagnoseUnexpandedParameterPack(Init, UPPC_Initializer)) { |
| 8205 | VDecl->setInvalidDecl(); |
| 8206 | return; |
| 8207 | } |
| 8208 | } |
| John McCall | d4e1b76 | 2010-08-01 01:24:59 +0000 | [diff] [blame] | 8209 | |
| Peter Collingbourne | 2dbb708 | 2011-09-19 21:14:35 +0000 | [diff] [blame] | 8210 | // OpenCL 1.1 6.5.2: "Variables allocated in the __local address space inside |
| 8211 | // a kernel function cannot be initialized." |
| 8212 | if (VDecl->getStorageClass() == SC_OpenCLWorkGroupLocal) { |
| 8213 | Diag(VDecl->getLocation(), diag::err_local_cant_init); |
| 8214 | VDecl->setInvalidDecl(); |
| 8215 | return; |
| 8216 | } |
| 8217 | |
| Steve Naroff | 6109140 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 8218 | // Get the decls type and save a reference for later, since |
| Steve Naroff | 98f7203 | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 8219 | // CheckInitializerTypes may change it. |
| Steve Naroff | 437b4d8 | 2007-09-12 20:13:48 +0000 | [diff] [blame] | 8220 | QualType DclT = VDecl->getType(), SavT = DclT; |
| Fariborz Jahanian | c8a322a | 2012-03-09 18:47:16 +0000 | [diff] [blame] | 8221 | |
| Douglas Gregor | b5af2e9 | 2013-03-07 22:57:58 +0000 | [diff] [blame] | 8222 | // Expressions default to 'id' when we're in a debugger |
| 8223 | // and we are assigning it to a variable of Objective-C pointer type. |
| 8224 | if (getLangOpts().DebuggerCastResultToId && DclT->isObjCObjectPointerType() && |
| 8225 | Init->getType() == Context.UnknownAnyTy) { |
| 8226 | ExprResult Result = forceUnknownAnyToType(Init, Context.getObjCIdType()); |
| 8227 | if (Result.isInvalid()) { |
| 8228 | VDecl->setInvalidDecl(); |
| 8229 | return; |
| Fariborz Jahanian | c8a322a | 2012-03-09 18:47:16 +0000 | [diff] [blame] | 8230 | } |
| Douglas Gregor | b5af2e9 | 2013-03-07 22:57:58 +0000 | [diff] [blame] | 8231 | Init = Result.take(); |
| 8232 | } |
| Richard Smith | 0cc8578 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8233 | |
| 8234 | // Perform the initialization. |
| 8235 | if (!VDecl->isInvalidDecl()) { |
| 8236 | InitializedEntity Entity = InitializedEntity::InitializeVariable(VDecl); |
| 8237 | InitializationKind Kind |
| Sebastian Redl | 5a41f68 | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 8238 | = DirectInit ? |
| 8239 | CXXDirectInit ? InitializationKind::CreateDirect(VDecl->getLocation(), |
| 8240 | Init->getLocStart(), |
| 8241 | Init->getLocEnd()) |
| 8242 | : InitializationKind::CreateDirectList( |
| 8243 | VDecl->getLocation()) |
| Richard Smith | 0cc8578 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8244 | : InitializationKind::CreateCopy(VDecl->getLocation(), |
| 8245 | Init->getLocStart()); |
| 8246 | |
| Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 8247 | MultiExprArg Args = Init; |
| 8248 | if (CXXDirectInit) |
| 8249 | Args = MultiExprArg(CXXDirectInit->getExprs(), |
| 8250 | CXXDirectInit->getNumExprs()); |
| 8251 | |
| 8252 | InitializationSequence InitSeq(*this, Entity, Kind, Args); |
| 8253 | ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Args, &DclT); |
| Richard Smith | 0cc8578 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8254 | if (Result.isInvalid()) { |
| Steve Naroff | 08899ff | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 8255 | VDecl->setInvalidDecl(); |
| Richard Smith | 0cc8578 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8256 | return; |
| Steve Naroff | 6109140 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 8257 | } |
| Richard Smith | 0cc8578 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8258 | |
| 8259 | Init = Result.takeAs<Expr>(); |
| 8260 | } |
| 8261 | |
| Richard Trieu | 3267347 | 2012-10-01 17:39:51 +0000 | [diff] [blame] | 8262 | // Check for self-references within variable initializers. |
| 8263 | // Variables declared within a function/method body (except for references) |
| 8264 | // are handled by a dataflow analysis. |
| 8265 | if (!VDecl->hasLocalStorage() || VDecl->getType()->isRecordType() || |
| 8266 | VDecl->getType()->isReferenceType()) { |
| 8267 | CheckSelfReference(*this, RealDecl, Init, DirectInit); |
| 8268 | } |
| 8269 | |
| Richard Smith | 0cc8578 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8270 | // If the type changed, it means we had an incomplete type that was |
| 8271 | // completed by the initializer. For example: |
| 8272 | // int ary[] = { 1, 3, 5 }; |
| John McCall | a59dc2f | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 8273 | // "ary" transitions from an IncompleteArrayType to a ConstantArrayType. |
| Eli Friedman | 91f5ae5 | 2012-02-23 02:25:10 +0000 | [diff] [blame] | 8274 | if (!VDecl->isInvalidDecl() && (DclT != SavT)) |
| Richard Smith | 0cc8578 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8275 | VDecl->setType(DclT); |
| Richard Smith | 0cc8578 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8276 | |
| Jordan Rose | fa9e4ba | 2012-09-15 02:48:31 +0000 | [diff] [blame] | 8277 | if (!VDecl->isInvalidDecl()) { |
| Richard Smith | 0cc8578 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8278 | checkUnsafeAssigns(VDecl->getLocation(), VDecl->getType(), Init); |
| 8279 | |
| Jordan Rose | fa9e4ba | 2012-09-15 02:48:31 +0000 | [diff] [blame] | 8280 | if (VDecl->hasAttr<BlocksAttr>()) |
| 8281 | checkRetainCycles(VDecl, Init); |
| Jordan Rose | d393458 | 2012-09-28 22:21:30 +0000 | [diff] [blame] | 8282 | |
| 8283 | // It is safe to assign a weak reference into a strong variable. |
| 8284 | // Although this code can still have problems: |
| 8285 | // id x = self.weakProp; |
| 8286 | // id y = self.weakProp; |
| 8287 | // we do not warn to warn spuriously when 'x' and 'y' are on separate |
| 8288 | // paths through the function. This should be revisited if |
| 8289 | // -Wrepeated-use-of-weak is made flow-sensitive. |
| Ted Kremenek | 9453721 | 2012-12-20 22:31:27 +0000 | [diff] [blame] | 8290 | if (VDecl->getType().getObjCLifetime() == Qualifiers::OCL_Strong) { |
| Jordan Rose | d393458 | 2012-09-28 22:21:30 +0000 | [diff] [blame] | 8291 | DiagnosticsEngine::Level Level = |
| 8292 | Diags.getDiagnosticLevel(diag::warn_arc_repeated_use_of_weak, |
| 8293 | Init->getLocStart()); |
| 8294 | if (Level != DiagnosticsEngine::Ignored) |
| 8295 | getCurFunction()->markSafeWeakUse(Init); |
| 8296 | } |
| Jordan Rose | fa9e4ba | 2012-09-15 02:48:31 +0000 | [diff] [blame] | 8297 | } |
| 8298 | |
| Richard Smith | 945f8d3 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 8299 | // The initialization is usually a full-expression. |
| 8300 | // |
| 8301 | // FIXME: If this is a braced initialization of an aggregate, it is not |
| 8302 | // an expression, and each individual field initializer is a separate |
| 8303 | // full-expression. For instance, in: |
| 8304 | // |
| 8305 | // struct Temp { ~Temp(); }; |
| 8306 | // struct S { S(Temp); }; |
| 8307 | // struct T { S a, b; } t = { Temp(), Temp() } |
| 8308 | // |
| 8309 | // we should destroy the first Temp before constructing the second. |
| Fariborz Jahanian | e735ff9 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 8310 | ExprResult Result = ActOnFinishFullExpr(Init, VDecl->getLocation(), |
| 8311 | false, |
| 8312 | VDecl->isConstexpr()); |
| Richard Smith | 945f8d3 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 8313 | if (Result.isInvalid()) { |
| 8314 | VDecl->setInvalidDecl(); |
| 8315 | return; |
| 8316 | } |
| 8317 | Init = Result.take(); |
| 8318 | |
| Richard Smith | 0cc8578 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8319 | // Attach the initializer to the decl. |
| 8320 | VDecl->setInit(Init); |
| 8321 | |
| 8322 | if (VDecl->isLocalVarDecl()) { |
| 8323 | // C99 6.7.8p4: All the expressions in an initializer for an object that has |
| 8324 | // static storage duration shall be constant expressions or string literals. |
| 8325 | // C++ does not have this restriction. |
| Enea Zaffanella | 1aac546 | 2013-07-22 10:58:26 +0000 | [diff] [blame] | 8326 | if (!getLangOpts().CPlusPlus && !VDecl->isInvalidDecl()) { |
| 8327 | if (VDecl->getStorageClass() == SC_Static) |
| 8328 | CheckForConstantInitializer(Init, DclT); |
| 8329 | // C89 is stricter than C99 for non-static aggregate types. |
| 8330 | // C89 6.5.7p3: All the expressions [...] in an initializer list |
| 8331 | // for an object that has aggregate or union type shall be |
| 8332 | // constant expressions. |
| 8333 | else if (!getLangOpts().C99 && VDecl->getType()->isAggregateType() && |
| Enea Zaffanella | c7cb48c | 2013-07-22 19:10:20 +0000 | [diff] [blame] | 8334 | isa<InitListExpr>(Init) && |
| Enea Zaffanella | 1aac546 | 2013-07-22 10:58:26 +0000 | [diff] [blame] | 8335 | !Init->isConstantInitializer(Context, false)) |
| 8336 | Diag(Init->getExprLoc(), |
| 8337 | diag::ext_aggregate_init_not_constant) |
| 8338 | << Init->getSourceRange(); |
| 8339 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8340 | } else if (VDecl->isStaticDataMember() && |
| Douglas Gregor | 0c88030 | 2009-03-11 23:00:04 +0000 | [diff] [blame] | 8341 | VDecl->getLexicalDeclContext()->isRecord()) { |
| 8342 | // This is an in-class initialization for a static data member, e.g., |
| 8343 | // |
| 8344 | // struct S { |
| 8345 | // static const int value = 17; |
| 8346 | // }; |
| 8347 | |
| Douglas Gregor | 0c88030 | 2009-03-11 23:00:04 +0000 | [diff] [blame] | 8348 | // C++ [class.mem]p4: |
| 8349 | // A member-declarator can contain a constant-initializer only |
| 8350 | // if it declares a static member (9.4) of const integral or |
| 8351 | // const enumeration type, see 9.4.2. |
| Richard Smith | 2316cd8 | 2011-09-29 19:11:37 +0000 | [diff] [blame] | 8352 | // |
| Richard Smith | 0cc8578 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8353 | // C++11 [class.static.data]p3: |
| Richard Smith | 2316cd8 | 2011-09-29 19:11:37 +0000 | [diff] [blame] | 8354 | // If a non-volatile const static data member is of integral or |
| 8355 | // enumeration type, its declaration in the class definition can |
| 8356 | // specify a brace-or-equal-initializer in which every initalizer-clause |
| 8357 | // that is an assignment-expression is a constant expression. A static |
| 8358 | // data member of literal type can be declared in the class definition |
| 8359 | // with the constexpr specifier; if so, its declaration shall specify a |
| 8360 | // brace-or-equal-initializer in which every initializer-clause that is |
| 8361 | // an assignment-expression is a constant expression. |
| John McCall | db76892 | 2010-09-10 23:21:22 +0000 | [diff] [blame] | 8362 | |
| 8363 | // Do nothing on dependent types. |
| Richard Smith | 0cc8578 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8364 | if (DclT->isDependentType()) { |
| John McCall | db76892 | 2010-09-10 23:21:22 +0000 | [diff] [blame] | 8365 | |
| Richard Smith | 2316cd8 | 2011-09-29 19:11:37 +0000 | [diff] [blame] | 8366 | // Allow any 'static constexpr' members, whether or not they are of literal |
| Richard Smith | 3607ffe | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 8367 | // type. We separately check that every constexpr variable is of literal |
| 8368 | // type. |
| Richard Smith | 2316cd8 | 2011-09-29 19:11:37 +0000 | [diff] [blame] | 8369 | } else if (VDecl->isConstexpr()) { |
| 8370 | |
| John McCall | db76892 | 2010-09-10 23:21:22 +0000 | [diff] [blame] | 8371 | // Require constness. |
| Richard Smith | 0cc8578 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8372 | } else if (!DclT.isConstQualified()) { |
| John McCall | db76892 | 2010-09-10 23:21:22 +0000 | [diff] [blame] | 8373 | Diag(VDecl->getLocation(), diag::err_in_class_initializer_non_const) |
| 8374 | << Init->getSourceRange(); |
| Douglas Gregor | 0c88030 | 2009-03-11 23:00:04 +0000 | [diff] [blame] | 8375 | VDecl->setInvalidDecl(); |
| John McCall | db76892 | 2010-09-10 23:21:22 +0000 | [diff] [blame] | 8376 | |
| 8377 | // We allow integer constant expressions in all cases. |
| Richard Smith | 0cc8578 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8378 | } else if (DclT->isIntegralOrEnumerationType()) { |
| Chris Lattner | 9925ec8 | 2011-06-14 05:46:29 +0000 | [diff] [blame] | 8379 | // Check whether the expression is a constant expression. |
| 8380 | SourceLocation Loc; |
| Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 8381 | if (getLangOpts().CPlusPlus11 && DclT.isVolatileQualified()) |
| Richard Smith | 0cc8578 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8382 | // In C++11, a non-constexpr const static data member with an |
| Richard Smith | ee6311d | 2011-09-29 21:28:14 +0000 | [diff] [blame] | 8383 | // in-class initializer cannot be volatile. |
| 8384 | Diag(VDecl->getLocation(), diag::err_in_class_initializer_volatile); |
| 8385 | else if (Init->isValueDependent()) |
| Chris Lattner | 9925ec8 | 2011-06-14 05:46:29 +0000 | [diff] [blame] | 8386 | ; // Nothing to check. |
| 8387 | else if (Init->isIntegerConstantExpr(Context, &Loc)) |
| 8388 | ; // Ok, it's an ICE! |
| 8389 | else if (Init->isEvaluatable(Context)) { |
| 8390 | // If we can constant fold the initializer through heroics, accept it, |
| 8391 | // but report this as a use of an extension for -pedantic. |
| 8392 | Diag(Loc, diag::ext_in_class_initializer_non_constant) |
| 8393 | << Init->getSourceRange(); |
| 8394 | } else { |
| 8395 | // Otherwise, this is some crazy unknown case. Report the issue at the |
| 8396 | // location provided by the isIntegerConstantExpr failed check. |
| 8397 | Diag(Loc, diag::err_in_class_initializer_non_constant) |
| 8398 | << Init->getSourceRange(); |
| 8399 | VDecl->setInvalidDecl(); |
| John McCall | db76892 | 2010-09-10 23:21:22 +0000 | [diff] [blame] | 8400 | } |
| 8401 | |
| Richard Smith | 0cc8578 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8402 | // We allow foldable floating-point constants as an extension. |
| 8403 | } else if (DclT->isFloatingType()) { // also permits complex, which is ok |
| Richard Smith | cf65638 | 2013-01-25 04:22:16 +0000 | [diff] [blame] | 8404 | // In C++98, this is a GNU extension. In C++11, it is not, but we support |
| 8405 | // it anyway and provide a fixit to add the 'constexpr'. |
| 8406 | if (getLangOpts().CPlusPlus11) { |
| David Blaikie | 8505c29 | 2013-01-29 22:26:08 +0000 | [diff] [blame] | 8407 | Diag(VDecl->getLocation(), |
| 8408 | diag::ext_in_class_initializer_float_type_cxx11) |
| 8409 | << DclT << Init->getSourceRange(); |
| 8410 | Diag(VDecl->getLocStart(), |
| 8411 | diag::note_in_class_initializer_float_type_cxx11) |
| 8412 | << FixItHint::CreateInsertion(VDecl->getLocStart(), "constexpr "); |
| Richard Smith | cf65638 | 2013-01-25 04:22:16 +0000 | [diff] [blame] | 8413 | } else { |
| 8414 | Diag(VDecl->getLocation(), diag::ext_in_class_initializer_float_type) |
| 8415 | << DclT << Init->getSourceRange(); |
| John McCall | db76892 | 2010-09-10 23:21:22 +0000 | [diff] [blame] | 8416 | |
| Richard Smith | cf65638 | 2013-01-25 04:22:16 +0000 | [diff] [blame] | 8417 | if (!Init->isValueDependent() && !Init->isEvaluatable(Context)) { |
| 8418 | Diag(Init->getExprLoc(), diag::err_in_class_initializer_non_constant) |
| 8419 | << Init->getSourceRange(); |
| 8420 | VDecl->setInvalidDecl(); |
| 8421 | } |
| Douglas Gregor | 0c88030 | 2009-03-11 23:00:04 +0000 | [diff] [blame] | 8422 | } |
| Richard Smith | 256336d | 2011-09-29 23:18:34 +0000 | [diff] [blame] | 8423 | |
| Richard Smith | 0cc8578 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8424 | // Suggest adding 'constexpr' in C++11 for literal types. |
| Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 8425 | } else if (getLangOpts().CPlusPlus11 && DclT->isLiteralType(Context)) { |
| Richard Smith | 256336d | 2011-09-29 23:18:34 +0000 | [diff] [blame] | 8426 | Diag(VDecl->getLocation(), diag::err_in_class_initializer_literal_type) |
| Richard Smith | 0cc8578 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8427 | << DclT << Init->getSourceRange() |
| Richard Smith | 256336d | 2011-09-29 23:18:34 +0000 | [diff] [blame] | 8428 | << FixItHint::CreateInsertion(VDecl->getLocStart(), "constexpr "); |
| 8429 | VDecl->setConstexpr(true); |
| 8430 | |
| Richard Smith | 2316cd8 | 2011-09-29 19:11:37 +0000 | [diff] [blame] | 8431 | } else { |
| 8432 | Diag(VDecl->getLocation(), diag::err_in_class_initializer_bad_type) |
| Richard Smith | 0cc8578 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8433 | << DclT << Init->getSourceRange(); |
| Richard Smith | 2316cd8 | 2011-09-29 19:11:37 +0000 | [diff] [blame] | 8434 | VDecl->setInvalidDecl(); |
| Douglas Gregor | 0c88030 | 2009-03-11 23:00:04 +0000 | [diff] [blame] | 8435 | } |
| Steve Naroff | 08899ff | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 8436 | } else if (VDecl->isFileVarDecl()) { |
| Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 8437 | if (VDecl->getStorageClass() == SC_Extern && |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8438 | (!getLangOpts().CPlusPlus || |
| Rafael Espindola | 069ab03 | 2013-03-29 07:56:05 +0000 | [diff] [blame] | 8439 | !(Context.getBaseElementType(VDecl->getType()).isConstQualified() || |
| Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 8440 | VDecl->isExternC())) && |
| 8441 | !isTemplateInstantiation(VDecl->getTemplateSpecializationKind())) |
| Steve Naroff | 437b4d8 | 2007-09-12 20:13:48 +0000 | [diff] [blame] | 8442 | Diag(VDecl->getLocation(), diag::warn_extern_init); |
| Eli Friedman | 463e523 | 2009-12-22 02:10:53 +0000 | [diff] [blame] | 8443 | |
| Richard Smith | 0cc8578 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8444 | // C99 6.7.8p4. All file scoped initializers need to be constant. |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8445 | if (!getLangOpts().CPlusPlus && !VDecl->isInvalidDecl()) |
| Anders Carlsson | 41e0881 | 2008-08-22 05:00:02 +0000 | [diff] [blame] | 8446 | CheckForConstantInitializer(Init, DclT); |
| Richard Smith | 6ea1a4d | 2013-04-14 20:11:31 +0000 | [diff] [blame] | 8447 | else if (VDecl->getTLSKind() == VarDecl::TLS_Static && |
| 8448 | !VDecl->isInvalidDecl() && !DclT->isDependentType() && |
| 8449 | !Init->isValueDependent() && !VDecl->isConstexpr() && |
| Richard Smith | 774672e | 2013-04-15 08:07:34 +0000 | [diff] [blame] | 8450 | !Init->isConstantInitializer( |
| 8451 | Context, VDecl->getType()->isReferenceType())) { |
| Richard Smith | 6ea1a4d | 2013-04-14 20:11:31 +0000 | [diff] [blame] | 8452 | // GNU C++98 edits for __thread, [basic.start.init]p4: |
| 8453 | // An object of thread storage duration shall not require dynamic |
| 8454 | // initialization. |
| 8455 | // FIXME: Need strict checking here. |
| 8456 | Diag(VDecl->getLocation(), diag::err_thread_dynamic_init); |
| 8457 | if (getLangOpts().CPlusPlus11) |
| 8458 | Diag(VDecl->getLocation(), diag::note_use_thread_local); |
| 8459 | } |
| Steve Naroff | 6109140 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 8460 | } |
| Douglas Gregor | beecd58 | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 8461 | |
| Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 8462 | // We will represent direct-initialization similarly to copy-initialization: |
| 8463 | // int x(1); -as-> int x = 1; |
| 8464 | // ClassType x(a,b,c); -as-> ClassType x = ClassType(a,b,c); |
| 8465 | // |
| 8466 | // Clients that want to distinguish between the two forms, can check for |
| 8467 | // direct initializer using VarDecl::getInitStyle(). |
| 8468 | // A major benefit is that clients that don't particularly care about which |
| 8469 | // exactly form was it (like the CodeGen) can handle both cases without |
| 8470 | // special case code. |
| 8471 | |
| 8472 | // C++ 8.5p11: |
| 8473 | // The form of initialization (using parentheses or '=') is generally |
| 8474 | // insignificant, but does matter when the entity being initialized has a |
| 8475 | // class type. |
| 8476 | if (CXXDirectInit) { |
| 8477 | assert(DirectInit && "Call-style initializer must be direct init."); |
| 8478 | VDecl->setInitStyle(VarDecl::CallInit); |
| 8479 | } else if (DirectInit) { |
| 8480 | // This must be list-initialization. No other way is direct-initialization. |
| 8481 | VDecl->setInitStyle(VarDecl::ListInit); |
| 8482 | } |
| 8483 | |
| John McCall | 8b7fd8f1 | 2011-01-19 11:48:09 +0000 | [diff] [blame] | 8484 | CheckCompleteVariableDeclaration(VDecl); |
| Steve Naroff | 6109140 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 8485 | } |
| 8486 | |
| John McCall | eae5acb | 2010-03-31 02:13:20 +0000 | [diff] [blame] | 8487 | /// ActOnInitializerError - Given that there was an error parsing an |
| 8488 | /// initializer for the given declaration, try to return to some form |
| 8489 | /// of sanity. |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8490 | void Sema::ActOnInitializerError(Decl *D) { |
| John McCall | eae5acb | 2010-03-31 02:13:20 +0000 | [diff] [blame] | 8491 | // Our main concern here is re-establishing invariants like "a |
| 8492 | // variable's type is either dependent or complete". |
| John McCall | eae5acb | 2010-03-31 02:13:20 +0000 | [diff] [blame] | 8493 | if (!D || D->isInvalidDecl()) return; |
| 8494 | |
| 8495 | VarDecl *VD = dyn_cast<VarDecl>(D); |
| 8496 | if (!VD) return; |
| 8497 | |
| Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 8498 | // Auto types are meaningless if we can't make sense of the initializer. |
| Richard Smith | b2bc2e6 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 8499 | if (ParsingInitForAutoVars.count(D)) { |
| 8500 | D->setInvalidDecl(); |
| Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 8501 | return; |
| 8502 | } |
| 8503 | |
| John McCall | eae5acb | 2010-03-31 02:13:20 +0000 | [diff] [blame] | 8504 | QualType Ty = VD->getType(); |
| 8505 | if (Ty->isDependentType()) return; |
| 8506 | |
| 8507 | // Require a complete type. |
| 8508 | if (RequireCompleteType(VD->getLocation(), |
| 8509 | Context.getBaseElementType(Ty), |
| 8510 | diag::err_typecheck_decl_incomplete_type)) { |
| 8511 | VD->setInvalidDecl(); |
| 8512 | return; |
| 8513 | } |
| 8514 | |
| 8515 | // Require an abstract type. |
| 8516 | if (RequireNonAbstractType(VD->getLocation(), Ty, |
| 8517 | diag::err_abstract_type_in_decl, |
| 8518 | AbstractVariableType)) { |
| 8519 | VD->setInvalidDecl(); |
| 8520 | return; |
| 8521 | } |
| 8522 | |
| 8523 | // Don't bother complaining about constructors or destructors, |
| 8524 | // though. |
| 8525 | } |
| 8526 | |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8527 | void Sema::ActOnUninitializedDecl(Decl *RealDecl, |
| Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 8528 | bool TypeMayContainAuto) { |
| Argyrios Kyrtzidis | 6709e7d | 2008-11-07 13:01:22 +0000 | [diff] [blame] | 8529 | // If there is no declaration, there was an error parsing it. Just ignore it. |
| 8530 | if (RealDecl == 0) |
| 8531 | return; |
| 8532 | |
| Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 8533 | if (VarDecl *Var = dyn_cast<VarDecl>(RealDecl)) { |
| 8534 | QualType Type = Var->getType(); |
| Douglas Gregor | beecd58 | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 8535 | |
| Richard Smith | f0215fe | 2011-12-25 21:17:58 +0000 | [diff] [blame] | 8536 | // C++11 [dcl.spec.auto]p3 |
| Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 8537 | if (TypeMayContainAuto && Type->getContainedAutoType()) { |
| Anders Carlsson | ae01993 | 2009-07-11 00:34:39 +0000 | [diff] [blame] | 8538 | Diag(Var->getLocation(), diag::err_auto_var_requires_init) |
| 8539 | << Var->getDeclName() << Type; |
| 8540 | Var->setInvalidDecl(); |
| 8541 | return; |
| 8542 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8543 | |
| Richard Smith | f0215fe | 2011-12-25 21:17:58 +0000 | [diff] [blame] | 8544 | // C++11 [class.static.data]p3: A static data member can be declared with |
| Richard Smith | 2316cd8 | 2011-09-29 19:11:37 +0000 | [diff] [blame] | 8545 | // the constexpr specifier; if so, its declaration shall specify |
| 8546 | // a brace-or-equal-initializer. |
| Richard Smith | f0215fe | 2011-12-25 21:17:58 +0000 | [diff] [blame] | 8547 | // C++11 [dcl.constexpr]p1: The constexpr specifier shall be applied only to |
| 8548 | // the definition of a variable [...] or the declaration of a static data |
| 8549 | // member. |
| 8550 | if (Var->isConstexpr() && !Var->isThisDeclarationADefinition()) { |
| 8551 | if (Var->isStaticDataMember()) |
| 8552 | Diag(Var->getLocation(), |
| 8553 | diag::err_constexpr_static_mem_var_requires_init) |
| 8554 | << Var->getDeclName(); |
| 8555 | else |
| 8556 | Diag(Var->getLocation(), diag::err_invalid_constexpr_var_decl); |
| Richard Smith | 2316cd8 | 2011-09-29 19:11:37 +0000 | [diff] [blame] | 8557 | Var->setInvalidDecl(); |
| 8558 | return; |
| 8559 | } |
| 8560 | |
| Joey Gouly | 96b94e6 | 2014-01-03 14:16:55 +0000 | [diff] [blame] | 8561 | // OpenCL v1.1 s6.5.3: variables declared in the constant address space must |
| 8562 | // be initialized. |
| 8563 | if (!Var->isInvalidDecl() && |
| 8564 | Var->getType().getAddressSpace() == LangAS::opencl_constant && |
| Pekka Jaaskelainen | b3cdee0 | 2014-01-23 16:21:02 +0000 | [diff] [blame] | 8565 | Var->getStorageClass() != SC_Extern && !Var->getInit()) { |
| Joey Gouly | 96b94e6 | 2014-01-03 14:16:55 +0000 | [diff] [blame] | 8566 | Diag(Var->getLocation(), diag::err_opencl_constant_no_init); |
| 8567 | Var->setInvalidDecl(); |
| 8568 | return; |
| 8569 | } |
| 8570 | |
| Douglas Gregor | e656562 | 2010-02-09 07:26:29 +0000 | [diff] [blame] | 8571 | switch (Var->isThisDeclarationADefinition()) { |
| 8572 | case VarDecl::Definition: |
| 8573 | if (!Var->isStaticDataMember() || !Var->getAnyInitializer()) |
| 8574 | break; |
| 8575 | |
| 8576 | // We have an out-of-line definition of a static data member |
| 8577 | // that has an in-class initializer, so we type-check this like |
| 8578 | // a declaration. |
| 8579 | // |
| 8580 | // Fall through |
| 8581 | |
| 8582 | case VarDecl::DeclarationOnly: |
| 8583 | // It's only a declaration. |
| 8584 | |
| 8585 | // Block scope. C99 6.7p7: If an identifier for an object is |
| 8586 | // declared with no linkage (C99 6.2.2p6), the type for the |
| 8587 | // object shall be complete. |
| John McCall | 1c9c3fd | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 8588 | if (!Type->isDependentType() && Var->isLocalVarDecl() && |
| Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 8589 | !Var->hasLinkage() && !Var->isInvalidDecl() && |
| Douglas Gregor | e656562 | 2010-02-09 07:26:29 +0000 | [diff] [blame] | 8590 | RequireCompleteType(Var->getLocation(), Type, |
| 8591 | diag::err_typecheck_decl_incomplete_type)) |
| 8592 | Var->setInvalidDecl(); |
| 8593 | |
| 8594 | // Make sure that the type is not abstract. |
| 8595 | if (!Type->isDependentType() && !Var->isInvalidDecl() && |
| 8596 | RequireNonAbstractType(Var->getLocation(), Type, |
| 8597 | diag::err_abstract_type_in_decl, |
| 8598 | AbstractVariableType)) |
| 8599 | Var->setInvalidDecl(); |
| Fariborz Jahanian | 05f4e71 | 2012-08-15 18:42:26 +0000 | [diff] [blame] | 8600 | if (!Type->isDependentType() && !Var->isInvalidDecl() && |
| Fariborz Jahanian | f85f338 | 2012-08-17 21:44:55 +0000 | [diff] [blame] | 8601 | Var->getStorageClass() == SC_PrivateExtern) { |
| Fariborz Jahanian | 05f4e71 | 2012-08-15 18:42:26 +0000 | [diff] [blame] | 8602 | Diag(Var->getLocation(), diag::warn_private_extern); |
| Fariborz Jahanian | f85f338 | 2012-08-17 21:44:55 +0000 | [diff] [blame] | 8603 | Diag(Var->getLocation(), diag::note_private_extern); |
| 8604 | } |
| Fariborz Jahanian | 05f4e71 | 2012-08-15 18:42:26 +0000 | [diff] [blame] | 8605 | |
| Douglas Gregor | e656562 | 2010-02-09 07:26:29 +0000 | [diff] [blame] | 8606 | return; |
| 8607 | |
| 8608 | case VarDecl::TentativeDefinition: |
| 8609 | // File scope. C99 6.9.2p2: A declaration of an identifier for an |
| 8610 | // object that has file scope without an initializer, and without a |
| 8611 | // storage-class specifier or with the storage-class specifier "static", |
| 8612 | // constitutes a tentative definition. Note: A tentative definition with |
| 8613 | // external linkage is valid (C99 6.2.2p5). |
| 8614 | if (!Var->isInvalidDecl()) { |
| 8615 | if (const IncompleteArrayType *ArrayT |
| 8616 | = Context.getAsIncompleteArrayType(Type)) { |
| 8617 | if (RequireCompleteType(Var->getLocation(), |
| 8618 | ArrayT->getElementType(), |
| 8619 | diag::err_illegal_decl_array_incomplete_type)) |
| 8620 | Var->setInvalidDecl(); |
| John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 8621 | } else if (Var->getStorageClass() == SC_Static) { |
| Douglas Gregor | e656562 | 2010-02-09 07:26:29 +0000 | [diff] [blame] | 8622 | // C99 6.9.2p3: If the declaration of an identifier for an object is |
| 8623 | // a tentative definition and has internal linkage (C99 6.2.2p3), the |
| 8624 | // declared type shall not be an incomplete type. |
| 8625 | // NOTE: code such as the following |
| 8626 | // static struct s; |
| 8627 | // struct s { int a; }; |
| 8628 | // is accepted by gcc. Hence here we issue a warning instead of |
| 8629 | // an error and we do not invalidate the static declaration. |
| 8630 | // NOTE: to avoid multiple warnings, only check the first declaration. |
| Rafael Espindola | 3f9e444 | 2013-10-19 02:13:21 +0000 | [diff] [blame] | 8631 | if (Var->isFirstDecl()) |
| Douglas Gregor | e656562 | 2010-02-09 07:26:29 +0000 | [diff] [blame] | 8632 | RequireCompleteType(Var->getLocation(), Type, |
| 8633 | diag::ext_typecheck_decl_incomplete_type); |
| 8634 | } |
| 8635 | } |
| 8636 | |
| 8637 | // Record the tentative definition; we're done. |
| 8638 | if (!Var->isInvalidDecl()) |
| 8639 | TentativeDefinitions.push_back(Var); |
| 8640 | return; |
| 8641 | } |
| 8642 | |
| 8643 | // Provide a specific diagnostic for uninitialized variable |
| 8644 | // definitions with incomplete array type. |
| 8645 | if (Type->isIncompleteArrayType()) { |
| Sebastian Redl | 1060067 | 2009-11-05 19:47:47 +0000 | [diff] [blame] | 8646 | Diag(Var->getLocation(), |
| 8647 | diag::err_typecheck_incomplete_array_needs_initializer); |
| 8648 | Var->setInvalidDecl(); |
| 8649 | return; |
| 8650 | } |
| 8651 | |
| John McCall | a755f0f | 2010-08-01 01:25:24 +0000 | [diff] [blame] | 8652 | // Provide a specific diagnostic for uninitialized variable |
| 8653 | // definitions with reference type. |
| 8654 | if (Type->isReferenceType()) { |
| 8655 | Diag(Var->getLocation(), diag::err_reference_var_requires_init) |
| 8656 | << Var->getDeclName() |
| 8657 | << SourceRange(Var->getLocation(), Var->getLocation()); |
| 8658 | Var->setInvalidDecl(); |
| 8659 | return; |
| 8660 | } |
| Douglas Gregor | e656562 | 2010-02-09 07:26:29 +0000 | [diff] [blame] | 8661 | |
| 8662 | // Do not attempt to type-check the default initializer for a |
| 8663 | // variable with dependent type. |
| 8664 | if (Type->isDependentType()) |
| Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8665 | return; |
| Douglas Gregor | 5d3507d | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 8666 | |
| Douglas Gregor | e656562 | 2010-02-09 07:26:29 +0000 | [diff] [blame] | 8667 | if (Var->isInvalidDecl()) |
| 8668 | return; |
| Douglas Gregor | c99f155 | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 8669 | |
| Douglas Gregor | e656562 | 2010-02-09 07:26:29 +0000 | [diff] [blame] | 8670 | if (RequireCompleteType(Var->getLocation(), |
| 8671 | Context.getBaseElementType(Type), |
| 8672 | diag::err_typecheck_decl_incomplete_type)) { |
| 8673 | Var->setInvalidDecl(); |
| 8674 | return; |
| Douglas Gregor | c28b57d | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 8675 | } |
| Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 8676 | |
| Douglas Gregor | e656562 | 2010-02-09 07:26:29 +0000 | [diff] [blame] | 8677 | // The variable can not have an abstract class type. |
| 8678 | if (RequireNonAbstractType(Var->getLocation(), Type, |
| 8679 | diag::err_abstract_type_in_decl, |
| 8680 | AbstractVariableType)) { |
| 8681 | Var->setInvalidDecl(); |
| 8682 | return; |
| 8683 | } |
| 8684 | |
| Douglas Gregor | 9574af6 | 2011-05-21 17:52:48 +0000 | [diff] [blame] | 8685 | // Check for jumps past the implicit initializer. C++0x |
| 8686 | // clarifies that this applies to a "variable with automatic |
| 8687 | // storage duration", not a "local variable". |
| Richard Smith | fe2750d | 2011-10-20 21:42:12 +0000 | [diff] [blame] | 8688 | // C++11 [stmt.dcl]p3 |
| Douglas Gregor | 9574af6 | 2011-05-21 17:52:48 +0000 | [diff] [blame] | 8689 | // A program that jumps from a point where a variable with automatic |
| 8690 | // storage duration is not in scope to a point where it is in scope is |
| 8691 | // ill-formed unless the variable has scalar type, class type with a |
| 8692 | // trivial default constructor and a trivial destructor, a cv-qualified |
| 8693 | // version of one of these types, or an array of one of the preceding |
| 8694 | // types and is declared without an initializer. |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8695 | if (getLangOpts().CPlusPlus && Var->hasLocalStorage()) { |
| Douglas Gregor | 9574af6 | 2011-05-21 17:52:48 +0000 | [diff] [blame] | 8696 | if (const RecordType *Record |
| 8697 | = Context.getBaseElementType(Type)->getAs<RecordType>()) { |
| Alexis Hunt | 466627c | 2011-05-11 22:50:12 +0000 | [diff] [blame] | 8698 | CXXRecordDecl *CXXRecord = cast<CXXRecordDecl>(Record->getDecl()); |
| Richard Smith | fe2750d | 2011-10-20 21:42:12 +0000 | [diff] [blame] | 8699 | // Mark the function for further checking even if the looser rules of |
| 8700 | // C++11 do not require such checks, so that we can diagnose |
| 8701 | // incompatibilities with C++98. |
| 8702 | if (!CXXRecord->isPOD()) |
| Alexis Hunt | 466627c | 2011-05-11 22:50:12 +0000 | [diff] [blame] | 8703 | getCurFunction()->setHasBranchProtectedScope(); |
| 8704 | } |
| Douglas Gregor | e656562 | 2010-02-09 07:26:29 +0000 | [diff] [blame] | 8705 | } |
| Douglas Gregor | 9574af6 | 2011-05-21 17:52:48 +0000 | [diff] [blame] | 8706 | |
| 8707 | // C++03 [dcl.init]p9: |
| 8708 | // If no initializer is specified for an object, and the |
| 8709 | // object is of (possibly cv-qualified) non-POD class type (or |
| 8710 | // array thereof), the object shall be default-initialized; if |
| 8711 | // the object is of const-qualified type, the underlying class |
| 8712 | // type shall have a user-declared default |
| 8713 | // constructor. Otherwise, if no initializer is specified for |
| 8714 | // a non- static object, the object and its subobjects, if |
| 8715 | // any, have an indeterminate initial value); if the object |
| 8716 | // or any of its subobjects are of const-qualified type, the |
| 8717 | // program is ill-formed. |
| 8718 | // C++0x [dcl.init]p11: |
| 8719 | // If no initializer is specified for an object, the object is |
| 8720 | // default-initialized; [...]. |
| 8721 | InitializedEntity Entity = InitializedEntity::InitializeVariable(Var); |
| 8722 | InitializationKind Kind |
| 8723 | = InitializationKind::CreateDefault(Var->getLocation()); |
| Dmitri Gribenko | 78852e9 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 8724 | |
| 8725 | InitializationSequence InitSeq(*this, Entity, Kind, None); |
| 8726 | ExprResult Init = InitSeq.Perform(*this, Entity, Kind, None); |
| Douglas Gregor | 9574af6 | 2011-05-21 17:52:48 +0000 | [diff] [blame] | 8727 | if (Init.isInvalid()) |
| 8728 | Var->setInvalidDecl(); |
| Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 8729 | else if (Init.get()) { |
| Douglas Gregor | 9574af6 | 2011-05-21 17:52:48 +0000 | [diff] [blame] | 8730 | Var->setInit(MaybeCreateExprWithCleanups(Init.get())); |
| Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 8731 | // This is important for template substitution. |
| 8732 | Var->setInitStyle(VarDecl::CallInit); |
| 8733 | } |
| Douglas Gregor | 589973b | 2010-03-08 02:45:10 +0000 | [diff] [blame] | 8734 | |
| John McCall | 8b7fd8f1 | 2011-01-19 11:48:09 +0000 | [diff] [blame] | 8735 | CheckCompleteVariableDeclaration(Var); |
| Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 8736 | } |
| 8737 | } |
| 8738 | |
| Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 8739 | void Sema::ActOnCXXForRangeDecl(Decl *D) { |
| 8740 | VarDecl *VD = dyn_cast<VarDecl>(D); |
| 8741 | if (!VD) { |
| 8742 | Diag(D->getLocation(), diag::err_for_range_decl_must_be_var); |
| 8743 | D->setInvalidDecl(); |
| 8744 | return; |
| 8745 | } |
| 8746 | |
| 8747 | VD->setCXXForRangeDecl(true); |
| 8748 | |
| 8749 | // for-range-declaration cannot be given a storage class specifier. |
| 8750 | int Error = -1; |
| Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 8751 | switch (VD->getStorageClass()) { |
| Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 8752 | case SC_None: |
| 8753 | break; |
| 8754 | case SC_Extern: |
| 8755 | Error = 0; |
| 8756 | break; |
| 8757 | case SC_Static: |
| 8758 | Error = 1; |
| 8759 | break; |
| 8760 | case SC_PrivateExtern: |
| 8761 | Error = 2; |
| 8762 | break; |
| 8763 | case SC_Auto: |
| 8764 | Error = 3; |
| 8765 | break; |
| 8766 | case SC_Register: |
| 8767 | Error = 4; |
| 8768 | break; |
| Peter Collingbourne | 2dbb708 | 2011-09-19 21:14:35 +0000 | [diff] [blame] | 8769 | case SC_OpenCLWorkGroupLocal: |
| Peter Collingbourne | 9a8f153 | 2011-09-20 12:40:26 +0000 | [diff] [blame] | 8770 | llvm_unreachable("Unexpected storage class"); |
| Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 8771 | } |
| Richard Smith | 2316cd8 | 2011-09-29 19:11:37 +0000 | [diff] [blame] | 8772 | if (VD->isConstexpr()) |
| 8773 | Error = 5; |
| Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 8774 | if (Error != -1) { |
| 8775 | Diag(VD->getOuterLocStart(), diag::err_for_range_storage_class) |
| 8776 | << VD->getDeclName() << Error; |
| 8777 | D->setInvalidDecl(); |
| 8778 | } |
| 8779 | } |
| 8780 | |
| John McCall | 8b7fd8f1 | 2011-01-19 11:48:09 +0000 | [diff] [blame] | 8781 | void Sema::CheckCompleteVariableDeclaration(VarDecl *var) { |
| 8782 | if (var->isInvalidDecl()) return; |
| 8783 | |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8784 | // In ARC, don't allow jumps past the implicit initialization of a |
| 8785 | // local retaining variable. |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8786 | if (getLangOpts().ObjCAutoRefCount && |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8787 | var->hasLocalStorage()) { |
| 8788 | switch (var->getType().getObjCLifetime()) { |
| 8789 | case Qualifiers::OCL_None: |
| 8790 | case Qualifiers::OCL_ExplicitNone: |
| 8791 | case Qualifiers::OCL_Autoreleasing: |
| 8792 | break; |
| 8793 | |
| 8794 | case Qualifiers::OCL_Weak: |
| 8795 | case Qualifiers::OCL_Strong: |
| 8796 | getCurFunction()->setHasBranchProtectedScope(); |
| 8797 | break; |
| 8798 | } |
| 8799 | } |
| 8800 | |
| John McCall | 8a4e2e4 | 2014-01-29 08:33:09 +0000 | [diff] [blame] | 8801 | // Warn about externally-visible variables being defined without a |
| 8802 | // prior declaration. We only want to do this for global |
| 8803 | // declarations, but we also specifically need to avoid doing it for |
| 8804 | // class members because the linkage of an anonymous class can |
| 8805 | // change if it's later given a typedef name. |
| Eli Friedman | 7d14b3c | 2012-10-23 20:19:32 +0000 | [diff] [blame] | 8806 | if (var->isThisDeclarationADefinition() && |
| John McCall | 8a4e2e4 | 2014-01-29 08:33:09 +0000 | [diff] [blame] | 8807 | var->getDeclContext()->getRedeclContext()->isFileContext() && |
| Eli Friedman | 1f5d888 | 2013-09-24 23:10:08 +0000 | [diff] [blame] | 8808 | var->isExternallyVisible() && var->hasLinkage() && |
| Manuel Klimek | 5704e4e | 2012-12-12 13:26:54 +0000 | [diff] [blame] | 8809 | getDiagnostics().getDiagnosticLevel( |
| 8810 | diag::warn_missing_variable_declarations, |
| 8811 | var->getLocation())) { |
| Eli Friedman | 7d14b3c | 2012-10-23 20:19:32 +0000 | [diff] [blame] | 8812 | // Find a previous declaration that's not a definition. |
| 8813 | VarDecl *prev = var->getPreviousDecl(); |
| 8814 | while (prev && prev->isThisDeclarationADefinition()) |
| 8815 | prev = prev->getPreviousDecl(); |
| 8816 | |
| 8817 | if (!prev) |
| 8818 | Diag(var->getLocation(), diag::warn_missing_variable_declarations) << var; |
| 8819 | } |
| 8820 | |
| Richard Smith | 6ea1a4d | 2013-04-14 20:11:31 +0000 | [diff] [blame] | 8821 | if (var->getTLSKind() == VarDecl::TLS_Static && |
| 8822 | var->getType().isDestructedType()) { |
| 8823 | // GNU C++98 edits for __thread, [basic.start.term]p3: |
| 8824 | // The type of an object with thread storage duration shall not |
| 8825 | // have a non-trivial destructor. |
| 8826 | Diag(var->getLocation(), diag::err_thread_nontrivial_dtor); |
| 8827 | if (getLangOpts().CPlusPlus11) |
| 8828 | Diag(var->getLocation(), diag::note_use_thread_local); |
| 8829 | } |
| 8830 | |
| John McCall | 8b7fd8f1 | 2011-01-19 11:48:09 +0000 | [diff] [blame] | 8831 | // All the following checks are C++ only. |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8832 | if (!getLangOpts().CPlusPlus) return; |
| John McCall | 8b7fd8f1 | 2011-01-19 11:48:09 +0000 | [diff] [blame] | 8833 | |
| Richard Smith | de63d36 | 2012-11-09 23:03:14 +0000 | [diff] [blame] | 8834 | QualType type = var->getType(); |
| 8835 | if (type->isDependentType()) return; |
| John McCall | 8b7fd8f1 | 2011-01-19 11:48:09 +0000 | [diff] [blame] | 8836 | |
| 8837 | // __block variables might require us to capture a copy-initializer. |
| 8838 | if (var->hasAttr<BlocksAttr>()) { |
| 8839 | // It's currently invalid to ever have a __block variable with an |
| 8840 | // array type; should we diagnose that here? |
| 8841 | |
| 8842 | // Regardless, we don't want to ignore array nesting when |
| 8843 | // constructing this copy. |
| John McCall | 8b7fd8f1 | 2011-01-19 11:48:09 +0000 | [diff] [blame] | 8844 | if (type->isStructureOrClassType()) { |
| John McCall | eaef89b | 2013-03-22 02:10:40 +0000 | [diff] [blame] | 8845 | EnterExpressionEvaluationContext scope(*this, PotentiallyEvaluated); |
| John McCall | 8b7fd8f1 | 2011-01-19 11:48:09 +0000 | [diff] [blame] | 8846 | SourceLocation poi = var->getLocation(); |
| John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 8847 | Expr *varRef =new (Context) DeclRefExpr(var, false, type, VK_LValue, poi); |
| Douglas Gregor | ca00645 | 2013-03-07 22:38:24 +0000 | [diff] [blame] | 8848 | ExprResult result |
| 8849 | = PerformMoveOrCopyInitialization( |
| 8850 | InitializedEntity::InitializeBlock(poi, type, false), |
| 8851 | var, var->getType(), varRef, /*AllowNRVO=*/true); |
| John McCall | 8b7fd8f1 | 2011-01-19 11:48:09 +0000 | [diff] [blame] | 8852 | if (!result.isInvalid()) { |
| 8853 | result = MaybeCreateExprWithCleanups(result); |
| 8854 | Expr *init = result.takeAs<Expr>(); |
| 8855 | Context.setBlockVarCopyInits(var, init); |
| 8856 | } |
| 8857 | } |
| 8858 | } |
| 8859 | |
| Richard Smith | eda3c84 | 2011-11-07 22:16:17 +0000 | [diff] [blame] | 8860 | Expr *Init = var->getInit(); |
| 8861 | bool IsGlobal = var->hasGlobalStorage() && !var->isStaticLocal(); |
| Richard Smith | de63d36 | 2012-11-09 23:03:14 +0000 | [diff] [blame] | 8862 | QualType baseType = Context.getBaseElementType(type); |
| Richard Smith | eda3c84 | 2011-11-07 22:16:17 +0000 | [diff] [blame] | 8863 | |
| Richard Smith | bf83009 | 2012-10-29 18:26:47 +0000 | [diff] [blame] | 8864 | if (!var->getDeclContext()->isDependentContext() && |
| 8865 | Init && !Init->isValueDependent()) { |
| Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 8866 | if (IsGlobal && !var->isConstexpr() && |
| 8867 | getDiagnostics().getDiagnosticLevel(diag::warn_global_constructor, |
| 8868 | var->getLocation()) |
| Eli Friedman | 4c27ac2 | 2013-07-16 22:40:53 +0000 | [diff] [blame] | 8869 | != DiagnosticsEngine::Ignored) { |
| 8870 | // Warn about globals which don't have a constant initializer. Don't |
| 8871 | // warn about globals with a non-trivial destructor because we already |
| 8872 | // warned about them. |
| 8873 | CXXRecordDecl *RD = baseType->getAsCXXRecordDecl(); |
| 8874 | if (!(RD && !RD->hasTrivialDestructor()) && |
| 8875 | !Init->isConstantInitializer(Context, baseType->isReferenceType())) |
| 8876 | Diag(var->getLocation(), diag::warn_global_constructor) |
| 8877 | << Init->getSourceRange(); |
| 8878 | } |
| Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 8879 | |
| Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 8880 | if (var->isConstexpr()) { |
| Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 8881 | SmallVector<PartialDiagnosticAt, 8> Notes; |
| Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 8882 | if (!var->evaluateValue(Notes) || !var->isInitICE()) { |
| 8883 | SourceLocation DiagLoc = var->getLocation(); |
| 8884 | // If the note doesn't add any useful information other than a source |
| 8885 | // location, fold it into the primary diagnostic. |
| 8886 | if (Notes.size() == 1 && Notes[0].second.getDiagID() == |
| 8887 | diag::note_invalid_subexpr_in_const_expr) { |
| 8888 | DiagLoc = Notes[0].first; |
| 8889 | Notes.clear(); |
| 8890 | } |
| 8891 | Diag(DiagLoc, diag::err_constexpr_var_requires_const_init) |
| 8892 | << var << Init->getSourceRange(); |
| 8893 | for (unsigned I = 0, N = Notes.size(); I != N; ++I) |
| 8894 | Diag(Notes[I].first, Notes[I].second); |
| 8895 | } |
| Daniel Dunbar | 9d35581 | 2012-03-09 01:51:51 +0000 | [diff] [blame] | 8896 | } else if (var->isUsableInConstantExpressions(Context)) { |
| Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 8897 | // Check whether the initializer of a const variable of integral or |
| 8898 | // enumeration type is an ICE now, since we can't tell whether it was |
| 8899 | // initialized by a constant expression if we check later. |
| 8900 | var->checkInitIsICE(); |
| 8901 | } |
| Richard Smith | eda3c84 | 2011-11-07 22:16:17 +0000 | [diff] [blame] | 8902 | } |
| John McCall | 8b7fd8f1 | 2011-01-19 11:48:09 +0000 | [diff] [blame] | 8903 | |
| 8904 | // Require the destructor. |
| 8905 | if (const RecordType *recordType = baseType->getAs<RecordType>()) |
| 8906 | FinalizeVarWithDestructor(var, recordType); |
| 8907 | } |
| 8908 | |
| Richard Smith | b2bc2e6 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 8909 | /// FinalizeDeclaration - called by ParseDeclarationAfterDeclarator to perform |
| 8910 | /// any semantic actions necessary after any initializer has been attached. |
| 8911 | void |
| 8912 | Sema::FinalizeDeclaration(Decl *ThisDecl) { |
| 8913 | // Note that we are no longer parsing the initializer for this declaration. |
| 8914 | ParsingInitForAutoVars.erase(ThisDecl); |
| Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 8915 | |
| Rafael Espindola | db1a477 | 2013-02-22 17:59:16 +0000 | [diff] [blame] | 8916 | VarDecl *VD = dyn_cast_or_null<VarDecl>(ThisDecl); |
| Rafael Espindola | 60470f1 | 2013-01-03 04:05:19 +0000 | [diff] [blame] | 8917 | if (!VD) |
| 8918 | return; |
| 8919 | |
| Nico Rieck | f8e8b5f | 2014-01-21 23:54:36 +0000 | [diff] [blame] | 8920 | checkAttributesAfterMerging(*this, *VD); |
| 8921 | |
| Rafael Espindola | 87198cd | 2013-08-16 23:18:50 +0000 | [diff] [blame] | 8922 | if (UsedAttr *Attr = VD->getAttr<UsedAttr>()) { |
| 8923 | if (!Attr->isInherited() && !VD->isThisDeclarationADefinition()) { |
| Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 8924 | Diag(Attr->getLocation(), diag::warn_attribute_ignored) << Attr; |
| Rafael Espindola | 87198cd | 2013-08-16 23:18:50 +0000 | [diff] [blame] | 8925 | VD->dropAttr<UsedAttr>(); |
| 8926 | } |
| 8927 | } |
| 8928 | |
| Rafael Espindola | d53ffa0 | 2013-10-22 21:39:03 +0000 | [diff] [blame] | 8929 | if (!VD->isInvalidDecl() && |
| 8930 | VD->isThisDeclarationADefinition() == VarDecl::TentativeDefinition) { |
| 8931 | if (const VarDecl *Def = VD->getDefinition()) { |
| 8932 | if (Def->hasAttr<AliasAttr>()) { |
| 8933 | Diag(VD->getLocation(), diag::err_tentative_after_alias) |
| 8934 | << VD->getDeclName(); |
| 8935 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 8936 | VD->setInvalidDecl(); |
| 8937 | } |
| 8938 | } |
| 8939 | } |
| 8940 | |
| Rafael Espindola | db1a477 | 2013-02-22 17:59:16 +0000 | [diff] [blame] | 8941 | const DeclContext *DC = VD->getDeclContext(); |
| 8942 | // If there's a #pragma GCC visibility in scope, and this isn't a class |
| 8943 | // member, set the visibility of this variable. |
| John McCall | 8a4e2e4 | 2014-01-29 08:33:09 +0000 | [diff] [blame] | 8944 | if (DC->getRedeclContext()->isFileContext() && VD->isExternallyVisible()) |
| Rafael Espindola | db1a477 | 2013-02-22 17:59:16 +0000 | [diff] [blame] | 8945 | AddPushedVisibilityAttribute(VD); |
| 8946 | |
| Rafael Espindola | d2ecc13 | 2013-01-03 04:29:20 +0000 | [diff] [blame] | 8947 | if (VD->isFileVarDecl()) |
| 8948 | MarkUnusedFileScopedDecl(VD); |
| 8949 | |
| Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 8950 | // Now we have parsed the initializer and can update the table of magic |
| 8951 | // tag values. |
| Rafael Espindola | 60470f1 | 2013-01-03 04:05:19 +0000 | [diff] [blame] | 8952 | if (!VD->hasAttr<TypeTagForDatatypeAttr>() || |
| 8953 | !VD->getType()->isIntegralOrEnumerationType()) |
| 8954 | return; |
| 8955 | |
| 8956 | for (specific_attr_iterator<TypeTagForDatatypeAttr> |
| 8957 | I = ThisDecl->specific_attr_begin<TypeTagForDatatypeAttr>(), |
| 8958 | E = ThisDecl->specific_attr_end<TypeTagForDatatypeAttr>(); |
| 8959 | I != E; ++I) { |
| 8960 | const Expr *MagicValueExpr = VD->getInit(); |
| 8961 | if (!MagicValueExpr) { |
| 8962 | continue; |
| Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 8963 | } |
| Rafael Espindola | 60470f1 | 2013-01-03 04:05:19 +0000 | [diff] [blame] | 8964 | llvm::APSInt MagicValueInt; |
| 8965 | if (!MagicValueExpr->isIntegerConstantExpr(MagicValueInt, Context)) { |
| 8966 | Diag(I->getRange().getBegin(), |
| 8967 | diag::err_type_tag_for_datatype_not_ice) |
| 8968 | << LangOpts.CPlusPlus << MagicValueExpr->getSourceRange(); |
| 8969 | continue; |
| 8970 | } |
| 8971 | if (MagicValueInt.getActiveBits() > 64) { |
| 8972 | Diag(I->getRange().getBegin(), |
| 8973 | diag::err_type_tag_for_datatype_too_large) |
| 8974 | << LangOpts.CPlusPlus << MagicValueExpr->getSourceRange(); |
| 8975 | continue; |
| 8976 | } |
| 8977 | uint64_t MagicValue = MagicValueInt.getZExtValue(); |
| 8978 | RegisterTypeTagForDatatype(I->getArgumentKind(), |
| 8979 | MagicValue, |
| 8980 | I->getMatchingCType(), |
| 8981 | I->getLayoutCompatible(), |
| 8982 | I->getMustBeNull()); |
| Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 8983 | } |
| Richard Smith | b2bc2e6 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 8984 | } |
| 8985 | |
| Rafael Espindola | ab41769 | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 8986 | Sema::DeclGroupPtrTy Sema::FinalizeDeclaratorGroup(Scope *S, const DeclSpec &DS, |
| 8987 | ArrayRef<Decl *> Group) { |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 8988 | SmallVector<Decl*, 8> Decls; |
| Eli Friedman | 55b9ecb | 2009-05-29 01:49:24 +0000 | [diff] [blame] | 8989 | |
| 8990 | if (DS.isTypeSpecOwned()) |
| John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 8991 | Decls.push_back(DS.getRepAsDecl()); |
| Eli Friedman | 55b9ecb | 2009-05-29 01:49:24 +0000 | [diff] [blame] | 8992 | |
| David Majnemer | 50ce835 | 2013-09-17 23:57:10 +0000 | [diff] [blame] | 8993 | DeclaratorDecl *FirstDeclaratorInGroup = 0; |
| Rafael Espindola | ab41769 | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 8994 | for (unsigned i = 0, e = Group.size(); i != e; ++i) |
| David Majnemer | 50ce835 | 2013-09-17 23:57:10 +0000 | [diff] [blame] | 8995 | if (Decl *D = Group[i]) { |
| 8996 | if (DeclaratorDecl *DD = dyn_cast<DeclaratorDecl>(D)) |
| 8997 | if (!FirstDeclaratorInGroup) |
| 8998 | FirstDeclaratorInGroup = DD; |
| Richard Smith | 2abf676 | 2011-02-23 00:37:57 +0000 | [diff] [blame] | 8999 | Decls.push_back(D); |
| David Majnemer | 50ce835 | 2013-09-17 23:57:10 +0000 | [diff] [blame] | 9000 | } |
| Richard Smith | 2abf676 | 2011-02-23 00:37:57 +0000 | [diff] [blame] | 9001 | |
| Eli Friedman | 3b7d46c | 2013-07-10 00:30:46 +0000 | [diff] [blame] | 9002 | if (DeclSpec::isDeclRep(DS.getTypeSpecType())) { |
| David Majnemer | 50ce835 | 2013-09-17 23:57:10 +0000 | [diff] [blame] | 9003 | if (TagDecl *Tag = dyn_cast_or_null<TagDecl>(DS.getRepAsDecl())) { |
| David Majnemer | 2206bf5 | 2014-03-05 08:57:59 +0000 | [diff] [blame] | 9004 | HandleTagNumbering(*this, Tag, S); |
| David Majnemer | 50ce835 | 2013-09-17 23:57:10 +0000 | [diff] [blame] | 9005 | if (!Tag->hasNameForLinkage() && !Tag->hasDeclaratorForAnonDecl()) |
| 9006 | Tag->setDeclaratorForAnonDecl(FirstDeclaratorInGroup); |
| 9007 | } |
| Eli Friedman | 3b7d46c | 2013-07-10 00:30:46 +0000 | [diff] [blame] | 9008 | } |
| David Blaikie | 095deba | 2012-11-14 01:52:05 +0000 | [diff] [blame] | 9009 | |
| Rafael Espindola | ab41769 | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 9010 | return BuildDeclaratorGroup(Decls, DS.containsPlaceholderType()); |
| Richard Smith | 2abf676 | 2011-02-23 00:37:57 +0000 | [diff] [blame] | 9011 | } |
| 9012 | |
| 9013 | /// BuildDeclaratorGroup - convert a list of declarations into a declaration |
| 9014 | /// group, performing any necessary semantic checking. |
| 9015 | Sema::DeclGroupPtrTy |
| Rafael Espindola | ab41769 | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 9016 | Sema::BuildDeclaratorGroup(llvm::MutableArrayRef<Decl *> Group, |
| Richard Smith | 2abf676 | 2011-02-23 00:37:57 +0000 | [diff] [blame] | 9017 | bool TypeMayContainAuto) { |
| Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 9018 | // C++0x [dcl.spec.auto]p7: |
| 9019 | // If the type deduced for the template parameter U is not the same in each |
| 9020 | // deduction, the program is ill-formed. |
| 9021 | // FIXME: When initializer-list support is added, a distinction is needed |
| 9022 | // between the deduced type U and the deduced type which 'auto' stands for. |
| 9023 | // auto a = 0, b = { 1, 2, 3 }; |
| 9024 | // is legal because the deduced type U is 'int' in both cases. |
| Rafael Espindola | ab41769 | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 9025 | if (TypeMayContainAuto && Group.size() > 1) { |
| Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 9026 | QualType Deduced; |
| 9027 | CanQualType DeducedCanon; |
| 9028 | VarDecl *DeducedDecl = 0; |
| Rafael Espindola | ab41769 | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 9029 | for (unsigned i = 0, e = Group.size(); i != e; ++i) { |
| Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 9030 | if (VarDecl *D = dyn_cast<VarDecl>(Group[i])) { |
| 9031 | AutoType *AT = D->getType()->getContainedAutoType(); |
| Richard Smith | 2abf676 | 2011-02-23 00:37:57 +0000 | [diff] [blame] | 9032 | // Don't reissue diagnostics when instantiating a template. |
| 9033 | if (AT && D->isInvalidDecl()) |
| 9034 | break; |
| Richard Smith | 27d807c | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 9035 | QualType U = AT ? AT->getDeducedType() : QualType(); |
| 9036 | if (!U.isNull()) { |
| Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 9037 | CanQualType UCanon = Context.getCanonicalType(U); |
| 9038 | if (Deduced.isNull()) { |
| 9039 | Deduced = U; |
| 9040 | DeducedCanon = UCanon; |
| 9041 | DeducedDecl = D; |
| 9042 | } else if (DeducedCanon != UCanon) { |
| Richard Smith | 2abf676 | 2011-02-23 00:37:57 +0000 | [diff] [blame] | 9043 | Diag(D->getTypeSourceInfo()->getTypeLoc().getBeginLoc(), |
| 9044 | diag::err_auto_different_deductions) |
| Richard Smith | 489e4e0 | 2013-05-04 04:19:27 +0000 | [diff] [blame] | 9045 | << (AT->isDecltypeAuto() ? 1 : 0) |
| Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 9046 | << Deduced << DeducedDecl->getDeclName() |
| 9047 | << U << D->getDeclName() |
| 9048 | << DeducedDecl->getInit()->getSourceRange() |
| 9049 | << D->getInit()->getSourceRange(); |
| Richard Smith | 2abf676 | 2011-02-23 00:37:57 +0000 | [diff] [blame] | 9050 | D->setInvalidDecl(); |
| Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 9051 | break; |
| 9052 | } |
| 9053 | } |
| 9054 | } |
| 9055 | } |
| 9056 | } |
| 9057 | |
| Rafael Espindola | ab41769 | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 9058 | ActOnDocumentableDecls(Group); |
| Dmitri Gribenko | f26054f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 9059 | |
| Rafael Espindola | ab41769 | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 9060 | return DeclGroupPtrTy::make( |
| 9061 | DeclGroupRef::Create(Context, Group.data(), Group.size())); |
| Chris Lattner | 776fac8 | 2007-06-09 00:53:06 +0000 | [diff] [blame] | 9062 | } |
| Steve Naroff | 7e6f7c2 | 2007-08-28 03:03:08 +0000 | [diff] [blame] | 9063 | |
| Dmitri Gribenko | f26054f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 9064 | void Sema::ActOnDocumentableDecl(Decl *D) { |
| Rafael Espindola | ab41769 | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 9065 | ActOnDocumentableDecls(D); |
| Dmitri Gribenko | f26054f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 9066 | } |
| 9067 | |
| Rafael Espindola | ab41769 | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 9068 | void Sema::ActOnDocumentableDecls(ArrayRef<Decl *> Group) { |
| Dmitri Gribenko | f26054f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 9069 | // Don't parse the comment if Doxygen diagnostics are ignored. |
| Rafael Espindola | ab41769 | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 9070 | if (Group.empty() || !Group[0]) |
| Dmitri Gribenko | f26054f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 9071 | return; |
| 9072 | |
| 9073 | if (Diags.getDiagnosticLevel(diag::warn_doc_param_not_found, |
| 9074 | Group[0]->getLocation()) |
| 9075 | == DiagnosticsEngine::Ignored) |
| 9076 | return; |
| 9077 | |
| Rafael Espindola | ab41769 | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 9078 | if (Group.size() >= 2) { |
| Dmitri Gribenko | f26054f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 9079 | // This is a decl group. Normally it will contain only declarations |
| Rafael Espindola | ab41769 | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 9080 | // produced from declarator list. But in case we have any definitions or |
| Dmitri Gribenko | f26054f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 9081 | // additional declaration references: |
| 9082 | // 'typedef struct S {} S;' |
| 9083 | // 'typedef struct S *S;' |
| 9084 | // 'struct S *pS;' |
| 9085 | // FinalizeDeclaratorGroup adds these as separate declarations. |
| 9086 | Decl *MaybeTagDecl = Group[0]; |
| 9087 | if (MaybeTagDecl && isa<TagDecl>(MaybeTagDecl)) { |
| Rafael Espindola | ab41769 | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 9088 | Group = Group.slice(1); |
| Dmitri Gribenko | f26054f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 9089 | } |
| 9090 | } |
| 9091 | |
| 9092 | // See if there are any new comments that are not attached to a decl. |
| 9093 | ArrayRef<RawComment *> Comments = Context.getRawCommentList().getComments(); |
| 9094 | if (!Comments.empty() && |
| 9095 | !Comments.back()->isAttached()) { |
| 9096 | // There is at least one comment that not attached to a decl. |
| 9097 | // Maybe it should be attached to one of these decls? |
| 9098 | // |
| 9099 | // Note that this way we pick up not only comments that precede the |
| 9100 | // declaration, but also comments that *follow* the declaration -- thanks to |
| 9101 | // the lookahead in the lexer: we've consumed the semicolon and looked |
| 9102 | // ahead through comments. |
| Rafael Espindola | ab41769 | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 9103 | for (unsigned i = 0, e = Group.size(); i != e; ++i) |
| Dmitri Gribenko | 6743e04 | 2012-09-29 11:40:46 +0000 | [diff] [blame] | 9104 | Context.getCommentForDecl(Group[i], &PP); |
| Dmitri Gribenko | f26054f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 9105 | } |
| 9106 | } |
| Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 9107 | |
| Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 9108 | /// ActOnParamDeclarator - Called from Parser::ParseFunctionDeclarator() |
| 9109 | /// to introduce parameters into function prototype scope. |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 9110 | Decl *Sema::ActOnParamDeclarator(Scope *S, Declarator &D) { |
| Chris Lattner | cf31de3 | 2008-06-26 06:49:43 +0000 | [diff] [blame] | 9111 | const DeclSpec &DS = D.getDeclSpec(); |
| Douglas Gregor | ad59050 | 2008-12-15 23:53:10 +0000 | [diff] [blame] | 9112 | |
| Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 9113 | // Verify C99 6.7.5.3p2: The only SCS allowed is 'register'. |
| Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 9114 | |
| Peter Collingbourne | 99eddc3 | 2011-10-21 11:55:09 +0000 | [diff] [blame] | 9115 | // C++03 [dcl.stc]p2 also permits 'auto'. |
| John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 9116 | VarDecl::StorageClass StorageClass = SC_None; |
| Daniel Dunbar | 0ff4192 | 2008-09-03 21:54:21 +0000 | [diff] [blame] | 9117 | if (DS.getStorageClassSpec() == DeclSpec::SCS_register) { |
| John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 9118 | StorageClass = SC_Register; |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9119 | } else if (getLangOpts().CPlusPlus && |
| Peter Collingbourne | 99eddc3 | 2011-10-21 11:55:09 +0000 | [diff] [blame] | 9120 | DS.getStorageClassSpec() == DeclSpec::SCS_auto) { |
| 9121 | StorageClass = SC_Auto; |
| Daniel Dunbar | 0ff4192 | 2008-09-03 21:54:21 +0000 | [diff] [blame] | 9122 | } else if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified) { |
| Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 9123 | Diag(DS.getStorageClassSpecLoc(), |
| 9124 | diag::err_invalid_storage_class_in_func_decl); |
| Chris Lattner | cf31de3 | 2008-06-26 06:49:43 +0000 | [diff] [blame] | 9125 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
| Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 9126 | } |
| Eli Friedman | d5c0eed | 2009-04-19 20:27:55 +0000 | [diff] [blame] | 9127 | |
| Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 9128 | if (DeclSpec::TSCS TSCS = DS.getThreadStorageClassSpec()) |
| 9129 | Diag(DS.getThreadStorageClassSpecLoc(), diag::err_invalid_thread) |
| 9130 | << DeclSpec::getSpecifierName(TSCS); |
| 9131 | if (DS.isConstexprSpecified()) |
| 9132 | Diag(DS.getConstexprSpecLoc(), diag::err_invalid_constexpr) |
| Richard Smith | a77a0a6 | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 9133 | << 0; |
| Eli Friedman | d5c0eed | 2009-04-19 20:27:55 +0000 | [diff] [blame] | 9134 | |
| Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 9135 | DiagnoseFunctionSpecifiers(DS); |
| Eli Friedman | 574c745 | 2009-04-07 19:37:57 +0000 | [diff] [blame] | 9136 | |
| Argyrios Kyrtzidis | ef7022f | 2011-06-28 03:01:15 +0000 | [diff] [blame] | 9137 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
| John McCall | 8cb7bdf | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 9138 | QualType parmDeclType = TInfo->getType(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9139 | |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9140 | if (getLangOpts().CPlusPlus) { |
| Douglas Gregor | 27b4c16 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 9141 | // Check that there are no default arguments inside the type of this |
| 9142 | // parameter. |
| 9143 | CheckExtraCXXDefaultArguments(D); |
| Douglas Gregor | 27b4c16 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 9144 | |
| 9145 | // Parameter declarators cannot be qualified (C++ [dcl.meaning]p1). |
| 9146 | if (D.getCXXScopeSpec().isSet()) { |
| 9147 | Diag(D.getIdentifierLoc(), diag::err_qualified_param_declarator) |
| 9148 | << D.getCXXScopeSpec().getRange(); |
| 9149 | D.getCXXScopeSpec().clear(); |
| 9150 | } |
| Douglas Gregor | d6ab874 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 9151 | } |
| 9152 | |
| Alexis Hunt | a56cbcc | 2010-11-03 01:07:06 +0000 | [diff] [blame] | 9153 | // Ensure we have a valid name |
| 9154 | IdentifierInfo *II = 0; |
| 9155 | if (D.hasName()) { |
| 9156 | II = D.getIdentifier(); |
| 9157 | if (!II) { |
| 9158 | Diag(D.getIdentifierLoc(), diag::err_bad_parameter_name) |
| Aaron Ballman | fee0cd4 | 2014-01-03 13:34:55 +0000 | [diff] [blame] | 9159 | << GetNameForDeclarator(D).getName(); |
| Alexis Hunt | a56cbcc | 2010-11-03 01:07:06 +0000 | [diff] [blame] | 9160 | D.setInvalidType(true); |
| 9161 | } |
| 9162 | } |
| 9163 | |
| Chris Lattner | dd1cb5b | 2010-02-22 00:40:25 +0000 | [diff] [blame] | 9164 | // Check for redeclaration of parameters, e.g. int foo(int x, int x); |
| Chris Lattner | d977351 | 2009-01-21 02:38:50 +0000 | [diff] [blame] | 9165 | if (II) { |
| John McCall | 84f0267 | 2010-03-18 06:42:38 +0000 | [diff] [blame] | 9166 | LookupResult R(*this, II, D.getIdentifierLoc(), LookupOrdinaryName, |
| 9167 | ForRedeclaration); |
| 9168 | LookupName(R, S); |
| 9169 | if (R.isSingleResult()) { |
| 9170 | NamedDecl *PrevDecl = R.getFoundDecl(); |
| Chris Lattner | d977351 | 2009-01-21 02:38:50 +0000 | [diff] [blame] | 9171 | if (PrevDecl->isTemplateParameter()) { |
| 9172 | // Maybe we will complain about the shadowed template parameter. |
| 9173 | DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), PrevDecl); |
| 9174 | // Just pretend that we didn't see the previous declaration. |
| 9175 | PrevDecl = 0; |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 9176 | } else if (S->isDeclScope(PrevDecl)) { |
| Chris Lattner | d977351 | 2009-01-21 02:38:50 +0000 | [diff] [blame] | 9177 | Diag(D.getIdentifierLoc(), diag::err_param_redefinition) << II; |
| Chris Lattner | dd1cb5b | 2010-02-22 00:40:25 +0000 | [diff] [blame] | 9178 | Diag(PrevDecl->getLocation(), diag::note_previous_declaration); |
| Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 9179 | |
| Chris Lattner | d977351 | 2009-01-21 02:38:50 +0000 | [diff] [blame] | 9180 | // Recover by removing the name |
| 9181 | II = 0; |
| 9182 | D.SetIdentifier(0, D.getIdentifierLoc()); |
| Fariborz Jahanian | 5ec502e | 2010-02-12 21:53:14 +0000 | [diff] [blame] | 9183 | D.setInvalidType(true); |
| Chris Lattner | d977351 | 2009-01-21 02:38:50 +0000 | [diff] [blame] | 9184 | } |
| Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 9185 | } |
| Chris Lattner | c5cdf4d | 2007-01-21 07:42:07 +0000 | [diff] [blame] | 9186 | } |
| Steve Naroff | 773df5c | 2007-08-07 22:44:21 +0000 | [diff] [blame] | 9187 | |
| John McCall | f7b2fb5 | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 9188 | // Temporarily put parameter variables in the translation unit, not |
| 9189 | // the enclosing context. This prevents them from accidentally |
| 9190 | // looking like class members in C++. |
| Douglas Gregor | 940bca7 | 2010-04-12 07:48:19 +0000 | [diff] [blame] | 9191 | ParmVarDecl *New = CheckParameter(Context.getTranslationUnitDecl(), |
| Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 9192 | D.getLocStart(), |
| Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 9193 | D.getIdentifierLoc(), II, |
| 9194 | parmDeclType, TInfo, |
| Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 9195 | StorageClass); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9196 | |
| Chris Lattner | f6d1c9c | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 9197 | if (D.isInvalidType()) |
| John McCall | 8fb0d9d | 2011-05-01 22:35:37 +0000 | [diff] [blame] | 9198 | New->setInvalidDecl(); |
| 9199 | |
| 9200 | assert(S->isFunctionPrototypeScope()); |
| 9201 | assert(S->getFunctionPrototypeDepth() >= 1); |
| 9202 | New->setScopeInfo(S->getFunctionPrototypeDepth() - 1, |
| 9203 | S->getNextFunctionPrototypeIndex()); |
| Douglas Gregor | 940bca7 | 2010-04-12 07:48:19 +0000 | [diff] [blame] | 9204 | |
| Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 9205 | // Add the parameter declaration into this scope. |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 9206 | S->AddDecl(New); |
| Argyrios Kyrtzidis | b8a4920 | 2008-04-12 00:47:19 +0000 | [diff] [blame] | 9207 | if (II) |
| Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 9208 | IdResolver.AddDecl(New); |
| Nate Begeman | d8c4156 | 2008-02-17 21:20:31 +0000 | [diff] [blame] | 9209 | |
| Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 9210 | ProcessDeclAttributes(S, New, D); |
| Mike Stump | e9efa80 | 2009-04-30 00:19:40 +0000 | [diff] [blame] | 9211 | |
| Douglas Gregor | 4186681 | 2011-09-12 18:37:38 +0000 | [diff] [blame] | 9212 | if (D.getDeclSpec().isModulePrivateSpecified()) |
| 9213 | Diag(New->getLocation(), diag::err_module_private_local) |
| 9214 | << 1 << New->getDeclName() |
| 9215 | << SourceRange(D.getDeclSpec().getModulePrivateSpecLoc()) |
| 9216 | << FixItHint::CreateRemoval(D.getDeclSpec().getModulePrivateSpecLoc()); |
| 9217 | |
| Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 9218 | if (New->hasAttr<BlocksAttr>()) { |
| Mike Stump | e9efa80 | 2009-04-30 00:19:40 +0000 | [diff] [blame] | 9219 | Diag(New->getLocation(), diag::err_block_on_nonlocal); |
| 9220 | } |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 9221 | return New; |
| Chris Lattner | c5cdf4d | 2007-01-21 07:42:07 +0000 | [diff] [blame] | 9222 | } |
| Fariborz Jahanian | 56ff146 | 2007-11-08 23:49:49 +0000 | [diff] [blame] | 9223 | |
| John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9224 | /// \brief Synthesizes a variable for a parameter arising from a |
| 9225 | /// typedef. |
| 9226 | ParmVarDecl *Sema::BuildParmVarDeclForTypedef(DeclContext *DC, |
| 9227 | SourceLocation Loc, |
| 9228 | QualType T) { |
| Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 9229 | /* FIXME: setting StartLoc == Loc. |
| 9230 | Would it be worth to modify callers so as to provide proper source |
| 9231 | location for the unnamed parameters, embedding the parameter's type? */ |
| 9232 | ParmVarDecl *Param = ParmVarDecl::Create(Context, DC, Loc, Loc, 0, |
| John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9233 | T, Context.getTrivialTypeSourceInfo(T, Loc), |
| Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 9234 | SC_None, 0); |
| John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9235 | Param->setImplicit(); |
| 9236 | return Param; |
| 9237 | } |
| 9238 | |
| John McCall | c599064 | 2010-08-24 09:05:15 +0000 | [diff] [blame] | 9239 | void Sema::DiagnoseUnusedParameters(ParmVarDecl * const *Param, |
| 9240 | ParmVarDecl * const *ParamEnd) { |
| John McCall | c599064 | 2010-08-24 09:05:15 +0000 | [diff] [blame] | 9241 | // Don't diagnose unused-parameter errors in template instantiations; we |
| 9242 | // will already have done so in the template itself. |
| 9243 | if (!ActiveTemplateInstantiations.empty()) |
| 9244 | return; |
| 9245 | |
| 9246 | for (; Param != ParamEnd; ++Param) { |
| Eli Friedman | c09e055 | 2012-01-13 23:41:25 +0000 | [diff] [blame] | 9247 | if (!(*Param)->isReferenced() && (*Param)->getDeclName() && |
| John McCall | c599064 | 2010-08-24 09:05:15 +0000 | [diff] [blame] | 9248 | !(*Param)->hasAttr<UnusedAttr>()) { |
| 9249 | Diag((*Param)->getLocation(), diag::warn_unused_parameter) |
| 9250 | << (*Param)->getDeclName(); |
| 9251 | } |
| 9252 | } |
| 9253 | } |
| 9254 | |
| Argyrios Kyrtzidis | af84ec0 | 2010-11-17 23:11:54 +0000 | [diff] [blame] | 9255 | void Sema::DiagnoseSizeOfParametersAndReturnValue(ParmVarDecl * const *Param, |
| 9256 | ParmVarDecl * const *ParamEnd, |
| 9257 | QualType ReturnTy, |
| 9258 | NamedDecl *D) { |
| Argyrios Kyrtzidis | ef6c8da | 2010-11-18 00:20:36 +0000 | [diff] [blame] | 9259 | if (LangOpts.NumLargeByValueCopy == 0) // No check. |
| Argyrios Kyrtzidis | af84ec0 | 2010-11-17 23:11:54 +0000 | [diff] [blame] | 9260 | return; |
| 9261 | |
| Argyrios Kyrtzidis | ef6c8da | 2010-11-18 00:20:36 +0000 | [diff] [blame] | 9262 | // Warn if the return value is pass-by-value and larger than the specified |
| 9263 | // threshold. |
| Eli Friedman | 7f21bd7 | 2012-01-09 23:46:59 +0000 | [diff] [blame] | 9264 | if (!ReturnTy->isDependentType() && ReturnTy.isPODType(Context)) { |
| Argyrios Kyrtzidis | af84ec0 | 2010-11-17 23:11:54 +0000 | [diff] [blame] | 9265 | unsigned Size = Context.getTypeSizeInChars(ReturnTy).getQuantity(); |
| Argyrios Kyrtzidis | ef6c8da | 2010-11-18 00:20:36 +0000 | [diff] [blame] | 9266 | if (Size > LangOpts.NumLargeByValueCopy) |
| Argyrios Kyrtzidis | af84ec0 | 2010-11-17 23:11:54 +0000 | [diff] [blame] | 9267 | Diag(D->getLocation(), diag::warn_return_value_size) |
| 9268 | << D->getDeclName() << Size; |
| 9269 | } |
| 9270 | |
| Argyrios Kyrtzidis | ef6c8da | 2010-11-18 00:20:36 +0000 | [diff] [blame] | 9271 | // Warn if any parameter is pass-by-value and larger than the specified |
| 9272 | // threshold. |
| Argyrios Kyrtzidis | af84ec0 | 2010-11-17 23:11:54 +0000 | [diff] [blame] | 9273 | for (; Param != ParamEnd; ++Param) { |
| 9274 | QualType T = (*Param)->getType(); |
| Eli Friedman | 7f21bd7 | 2012-01-09 23:46:59 +0000 | [diff] [blame] | 9275 | if (T->isDependentType() || !T.isPODType(Context)) |
| Argyrios Kyrtzidis | af84ec0 | 2010-11-17 23:11:54 +0000 | [diff] [blame] | 9276 | continue; |
| 9277 | unsigned Size = Context.getTypeSizeInChars(T).getQuantity(); |
| Argyrios Kyrtzidis | ef6c8da | 2010-11-18 00:20:36 +0000 | [diff] [blame] | 9278 | if (Size > LangOpts.NumLargeByValueCopy) |
| Argyrios Kyrtzidis | af84ec0 | 2010-11-17 23:11:54 +0000 | [diff] [blame] | 9279 | Diag((*Param)->getLocation(), diag::warn_parameter_size) |
| 9280 | << (*Param)->getDeclName() << Size; |
| 9281 | } |
| 9282 | } |
| 9283 | |
| Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 9284 | ParmVarDecl *Sema::CheckParameter(DeclContext *DC, SourceLocation StartLoc, |
| 9285 | SourceLocation NameLoc, IdentifierInfo *Name, |
| 9286 | QualType T, TypeSourceInfo *TSInfo, |
| Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 9287 | VarDecl::StorageClass StorageClass) { |
| Reid Kleckner | 17aeeeb | 2013-06-08 18:19:52 +0000 | [diff] [blame] | 9288 | // In ARC, infer a lifetime qualifier for appropriate parameter types. |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9289 | if (getLangOpts().ObjCAutoRefCount && |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9290 | T.getObjCLifetime() == Qualifiers::OCL_None && |
| Reid Kleckner | 17aeeeb | 2013-06-08 18:19:52 +0000 | [diff] [blame] | 9291 | T->isObjCLifetimeType()) { |
| 9292 | |
| 9293 | Qualifiers::ObjCLifetime lifetime; |
| 9294 | |
| 9295 | // Special cases for arrays: |
| 9296 | // - if it's const, use __unsafe_unretained |
| 9297 | // - otherwise, it's an error |
| 9298 | if (T->isArrayType()) { |
| 9299 | if (!T.isConstQualified()) { |
| 9300 | DelayedDiagnostics.add( |
| 9301 | sema::DelayedDiagnostic::makeForbiddenType( |
| Fariborz Jahanian | ed1933b | 2011-10-03 22:11:57 +0000 | [diff] [blame] | 9302 | NameLoc, diag::err_arc_array_param_no_ownership, T, false)); |
| Reid Kleckner | 17aeeeb | 2013-06-08 18:19:52 +0000 | [diff] [blame] | 9303 | } |
| 9304 | lifetime = Qualifiers::OCL_ExplicitNone; |
| 9305 | } else { |
| 9306 | lifetime = T->getObjCARCImplicitLifetime(); |
| 9307 | } |
| 9308 | T = Context.getLifetimeQualifiedType(T, lifetime); |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9309 | } |
| 9310 | |
| Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 9311 | ParmVarDecl *New = ParmVarDecl::Create(Context, DC, StartLoc, NameLoc, Name, |
| Douglas Gregor | 8428064 | 2011-07-12 04:42:08 +0000 | [diff] [blame] | 9312 | Context.getAdjustedParameterType(T), |
| 9313 | TSInfo, |
| Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 9314 | StorageClass, 0); |
| Douglas Gregor | 940bca7 | 2010-04-12 07:48:19 +0000 | [diff] [blame] | 9315 | |
| 9316 | // Parameters can not be abstract class types. |
| 9317 | // For record types, this is done by the AbstractClassUsageDiagnoser once |
| 9318 | // the class has been completely parsed. |
| 9319 | if (!CurContext->isRecord() && |
| 9320 | RequireNonAbstractType(NameLoc, T, diag::err_abstract_type_in_decl, |
| 9321 | AbstractParamType)) |
| 9322 | New->setInvalidDecl(); |
| 9323 | |
| 9324 | // Parameter declarators cannot be interface types. All ObjC objects are |
| 9325 | // passed by reference. |
| John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 9326 | if (T->isObjCObjectType()) { |
| Fariborz Jahanian | 7a05536 | 2012-05-09 21:49:29 +0000 | [diff] [blame] | 9327 | SourceLocation TypeEndLoc = TSInfo->getTypeLoc().getLocEnd(); |
| Douglas Gregor | 940bca7 | 2010-04-12 07:48:19 +0000 | [diff] [blame] | 9328 | Diag(NameLoc, |
| Fariborz Jahanian | 6507135e | 2011-07-26 17:58:54 +0000 | [diff] [blame] | 9329 | diag::err_object_cannot_be_passed_returned_by_value) << 1 << T |
| Fariborz Jahanian | 7a05536 | 2012-05-09 21:49:29 +0000 | [diff] [blame] | 9330 | << FixItHint::CreateInsertion(TypeEndLoc, "*"); |
| Fariborz Jahanian | 6507135e | 2011-07-26 17:58:54 +0000 | [diff] [blame] | 9331 | T = Context.getObjCObjectPointerType(T); |
| 9332 | New->setType(T); |
| Douglas Gregor | 940bca7 | 2010-04-12 07:48:19 +0000 | [diff] [blame] | 9333 | } |
| 9334 | |
| 9335 | // ISO/IEC TR 18037 S6.7.3: "The type of an object with automatic storage |
| 9336 | // duration shall not be qualified by an address-space qualifier." |
| 9337 | // Since all parameters have automatic store duration, they can not have |
| 9338 | // an address space. |
| 9339 | if (T.getAddressSpace() != 0) { |
| 9340 | Diag(NameLoc, diag::err_arg_with_address_space); |
| 9341 | New->setInvalidDecl(); |
| 9342 | } |
| 9343 | |
| 9344 | return New; |
| 9345 | } |
| 9346 | |
| Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 9347 | void Sema::ActOnFinishKNRParamDeclarations(Scope *S, Declarator &D, |
| 9348 | SourceLocation LocAfterDecls) { |
| Abramo Bagnara | 924a8f3 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 9349 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo(); |
| Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 9350 | |
| Chris Lattner | 5c5fbcc | 2006-12-03 08:41:30 +0000 | [diff] [blame] | 9351 | // Verify 6.9.1p6: 'every identifier in the identifier list shall be declared' |
| 9352 | // for a K&R function. |
| 9353 | if (!FTI.hasPrototype) { |
| Alp Toker | c535072 | 2014-02-26 22:27:52 +0000 | [diff] [blame] | 9354 | for (int i = FTI.NumParams; i != 0; /* decrement in loop */) { |
| Douglas Gregor | 862ffb1 | 2009-04-02 03:14:12 +0000 | [diff] [blame] | 9355 | --i; |
| Alp Toker | c535072 | 2014-02-26 22:27:52 +0000 | [diff] [blame] | 9356 | if (FTI.Params[i].Param == 0) { |
| Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 9357 | SmallString<256> Code; |
| Alp Toker | c535072 | 2014-02-26 22:27:52 +0000 | [diff] [blame] | 9358 | llvm::raw_svector_ostream(Code) |
| 9359 | << " int " << FTI.Params[i].Ident->getName() << ";\n"; |
| 9360 | Diag(FTI.Params[i].IdentLoc, diag::ext_param_not_declared) |
| 9361 | << FTI.Params[i].Ident |
| 9362 | << FixItHint::CreateInsertion(LocAfterDecls, Code.str()); |
| Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 9363 | |
| Chris Lattner | 5c5fbcc | 2006-12-03 08:41:30 +0000 | [diff] [blame] | 9364 | // Implicitly declare the argument as type 'int' for lack of a better |
| 9365 | // type. |
| John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 9366 | AttributeFactory attrs; |
| 9367 | DeclSpec DS(attrs); |
| Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 9368 | const char* PrevSpec; // unused |
| John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 9369 | unsigned DiagID; // unused |
| Alp Toker | c535072 | 2014-02-26 22:27:52 +0000 | [diff] [blame] | 9370 | DS.SetTypeSpecType(DeclSpec::TST_int, FTI.Params[i].IdentLoc, PrevSpec, |
| 9371 | DiagID, Context.getPrintingPolicy()); |
| Abramo Bagnara | 71f32c1 | 2012-10-04 21:38:29 +0000 | [diff] [blame] | 9372 | // Use the identifier location for the type source range. |
| Alp Toker | c535072 | 2014-02-26 22:27:52 +0000 | [diff] [blame] | 9373 | DS.SetRangeStart(FTI.Params[i].IdentLoc); |
| 9374 | DS.SetRangeEnd(FTI.Params[i].IdentLoc); |
| Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 9375 | Declarator ParamD(DS, Declarator::KNRTypeListContext); |
| Alp Toker | c535072 | 2014-02-26 22:27:52 +0000 | [diff] [blame] | 9376 | ParamD.SetIdentifier(FTI.Params[i].Ident, FTI.Params[i].IdentLoc); |
| 9377 | FTI.Params[i].Param = ActOnParamDeclarator(S, ParamD); |
| Chris Lattner | 5c5fbcc | 2006-12-03 08:41:30 +0000 | [diff] [blame] | 9378 | } |
| 9379 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9380 | } |
| Douglas Gregor | 9aa8904 | 2009-01-23 16:23:13 +0000 | [diff] [blame] | 9381 | } |
| 9382 | |
| Richard Smith | 79a52e5 | 2012-04-17 22:30:01 +0000 | [diff] [blame] | 9383 | Decl *Sema::ActOnStartOfFunctionDef(Scope *FnBodyScope, Declarator &D) { |
| Douglas Gregor | 9aa8904 | 2009-01-23 16:23:13 +0000 | [diff] [blame] | 9384 | assert(getCurFunctionDecl() == 0 && "Function parsing confused"); |
| Abramo Bagnara | 924a8f3 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 9385 | assert(D.isFunctionDeclarator() && "Not a function declarator!"); |
| Douglas Gregor | ad59050 | 2008-12-15 23:53:10 +0000 | [diff] [blame] | 9386 | Scope *ParentScope = FnBodyScope->getParent(); |
| Steve Naroff | 012484d | 2008-01-14 20:51:29 +0000 | [diff] [blame] | 9387 | |
| Douglas Gregor | 5d1b4e3 | 2011-11-07 20:56:01 +0000 | [diff] [blame] | 9388 | D.setFunctionDefinitionKind(FDK_Definition); |
| Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 9389 | Decl *DP = HandleDeclarator(ParentScope, D, MultiTemplateParamsArg()); |
| Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 9390 | return ActOnStartOfFunctionDef(FnBodyScope, DP); |
| Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 9391 | } |
| 9392 | |
| Anders Carlsson | 2a45e40 | 2012-12-18 01:29:20 +0000 | [diff] [blame] | 9393 | static bool ShouldWarnAboutMissingPrototype(const FunctionDecl *FD, |
| 9394 | const FunctionDecl*& PossibleZeroParamPrototype) { |
| Anders Carlsson | 31c7e88 | 2009-12-09 03:30:09 +0000 | [diff] [blame] | 9395 | // Don't warn about invalid declarations. |
| 9396 | if (FD->isInvalidDecl()) |
| 9397 | return false; |
| Anders Carlsson | a038825 | 2009-12-09 03:44:46 +0000 | [diff] [blame] | 9398 | |
| Anders Carlsson | 31c7e88 | 2009-12-09 03:30:09 +0000 | [diff] [blame] | 9399 | // Or declarations that aren't global. |
| 9400 | if (!FD->isGlobal()) |
| 9401 | return false; |
| Anders Carlsson | a038825 | 2009-12-09 03:44:46 +0000 | [diff] [blame] | 9402 | |
| Anders Carlsson | 31c7e88 | 2009-12-09 03:30:09 +0000 | [diff] [blame] | 9403 | // Don't warn about C++ member functions. |
| 9404 | if (isa<CXXMethodDecl>(FD)) |
| 9405 | return false; |
| Anders Carlsson | a038825 | 2009-12-09 03:44:46 +0000 | [diff] [blame] | 9406 | |
| Anders Carlsson | 31c7e88 | 2009-12-09 03:30:09 +0000 | [diff] [blame] | 9407 | // Don't warn about 'main'. |
| 9408 | if (FD->isMain()) |
| 9409 | return false; |
| Anders Carlsson | a038825 | 2009-12-09 03:44:46 +0000 | [diff] [blame] | 9410 | |
| Anders Carlsson | 31c7e88 | 2009-12-09 03:30:09 +0000 | [diff] [blame] | 9411 | // Don't warn about inline functions. |
| John McCall | 30cd20a | 2011-03-22 07:16:37 +0000 | [diff] [blame] | 9412 | if (FD->isInlined()) |
| Anders Carlsson | 31c7e88 | 2009-12-09 03:30:09 +0000 | [diff] [blame] | 9413 | return false; |
| Anders Carlsson | a038825 | 2009-12-09 03:44:46 +0000 | [diff] [blame] | 9414 | |
| 9415 | // Don't warn about function templates. |
| 9416 | if (FD->getDescribedFunctionTemplate()) |
| 9417 | return false; |
| 9418 | |
| 9419 | // Don't warn about function template specializations. |
| 9420 | if (FD->isFunctionTemplateSpecialization()) |
| 9421 | return false; |
| 9422 | |
| Tanya Lattner | 4bfc355 | 2012-07-26 00:08:28 +0000 | [diff] [blame] | 9423 | // Don't warn for OpenCL kernels. |
| 9424 | if (FD->hasAttr<OpenCLKernelAttr>()) |
| 9425 | return false; |
| Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 9426 | |
| Anders Carlsson | 31c7e88 | 2009-12-09 03:30:09 +0000 | [diff] [blame] | 9427 | bool MissingPrototype = true; |
| Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 9428 | for (const FunctionDecl *Prev = FD->getPreviousDecl(); |
| 9429 | Prev; Prev = Prev->getPreviousDecl()) { |
| Anders Carlsson | 31c7e88 | 2009-12-09 03:30:09 +0000 | [diff] [blame] | 9430 | // Ignore any declarations that occur in function or method |
| 9431 | // scope, because they aren't visible from the header. |
| Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 9432 | if (Prev->getLexicalDeclContext()->isFunctionOrMethod()) |
| Anders Carlsson | 31c7e88 | 2009-12-09 03:30:09 +0000 | [diff] [blame] | 9433 | continue; |
| Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 9434 | |
| Anders Carlsson | 31c7e88 | 2009-12-09 03:30:09 +0000 | [diff] [blame] | 9435 | MissingPrototype = !Prev->getType()->isFunctionProtoType(); |
| Anders Carlsson | 2a45e40 | 2012-12-18 01:29:20 +0000 | [diff] [blame] | 9436 | if (FD->getNumParams() == 0) |
| 9437 | PossibleZeroParamPrototype = Prev; |
| Anders Carlsson | 31c7e88 | 2009-12-09 03:30:09 +0000 | [diff] [blame] | 9438 | break; |
| 9439 | } |
| Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 9440 | |
| Anders Carlsson | 31c7e88 | 2009-12-09 03:30:09 +0000 | [diff] [blame] | 9441 | return MissingPrototype; |
| 9442 | } |
| 9443 | |
| Rafael Espindola | d53ffa0 | 2013-10-22 21:39:03 +0000 | [diff] [blame] | 9444 | void |
| 9445 | Sema::CheckForFunctionRedefinition(FunctionDecl *FD, |
| 9446 | const FunctionDecl *EffectiveDefinition) { |
| Francois Pichet | dcb3ebe | 2011-04-22 23:20:44 +0000 | [diff] [blame] | 9447 | // Don't complain if we're in GNU89 mode and the previous definition |
| 9448 | // was an extern inline function. |
| Rafael Espindola | d53ffa0 | 2013-10-22 21:39:03 +0000 | [diff] [blame] | 9449 | const FunctionDecl *Definition = EffectiveDefinition; |
| 9450 | if (!Definition) |
| 9451 | if (!FD->isDefined(Definition)) |
| 9452 | return; |
| 9453 | |
| 9454 | if (canRedefineFunction(Definition, getLangOpts())) |
| Rafael Espindola | 69f5310 | 2013-10-22 15:18:22 +0000 | [diff] [blame] | 9455 | return; |
| 9456 | |
| 9457 | if (getLangOpts().GNUMode && Definition->isInlineSpecified() && |
| 9458 | Definition->getStorageClass() == SC_Extern) |
| 9459 | Diag(FD->getLocation(), diag::err_redefinition_extern_inline) |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9460 | << FD->getDeclName() << getLangOpts().CPlusPlus; |
| Rafael Espindola | 69f5310 | 2013-10-22 15:18:22 +0000 | [diff] [blame] | 9461 | else |
| 9462 | Diag(FD->getLocation(), diag::err_redefinition) << FD->getDeclName(); |
| 9463 | |
| 9464 | Diag(Definition->getLocation(), diag::note_previous_definition); |
| 9465 | FD->setInvalidDecl(); |
| Francois Pichet | dcb3ebe | 2011-04-22 23:20:44 +0000 | [diff] [blame] | 9466 | } |
| Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 9467 | |
| 9468 | |
| Faisal Vali | c1a6dc4 | 2013-10-23 16:10:50 +0000 | [diff] [blame] | 9469 | static void RebuildLambdaScopeInfo(CXXMethodDecl *CallOperator, |
| 9470 | Sema &S) { |
| 9471 | CXXRecordDecl *const LambdaClass = CallOperator->getParent(); |
| Faisal Vali | 524ca28 | 2013-11-12 01:40:44 +0000 | [diff] [blame] | 9472 | |
| 9473 | LambdaScopeInfo *LSI = S.PushLambdaScope(); |
| Faisal Vali | c1a6dc4 | 2013-10-23 16:10:50 +0000 | [diff] [blame] | 9474 | LSI->CallOperator = CallOperator; |
| 9475 | LSI->Lambda = LambdaClass; |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 9476 | LSI->ReturnType = CallOperator->getReturnType(); |
| Faisal Vali | c1a6dc4 | 2013-10-23 16:10:50 +0000 | [diff] [blame] | 9477 | const LambdaCaptureDefault LCD = LambdaClass->getLambdaCaptureDefault(); |
| 9478 | |
| 9479 | if (LCD == LCD_None) |
| 9480 | LSI->ImpCaptureStyle = CapturingScopeInfo::ImpCap_None; |
| 9481 | else if (LCD == LCD_ByCopy) |
| 9482 | LSI->ImpCaptureStyle = CapturingScopeInfo::ImpCap_LambdaByval; |
| 9483 | else if (LCD == LCD_ByRef) |
| 9484 | LSI->ImpCaptureStyle = CapturingScopeInfo::ImpCap_LambdaByref; |
| 9485 | DeclarationNameInfo DNI = CallOperator->getNameInfo(); |
| 9486 | |
| 9487 | LSI->IntroducerRange = DNI.getCXXOperatorNameRange(); |
| 9488 | LSI->Mutable = !CallOperator->isConst(); |
| 9489 | |
| Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 9490 | // Add the captures to the LSI so they can be noted as already |
| 9491 | // captured within tryCaptureVar. |
| 9492 | for (LambdaExpr::capture_iterator C = LambdaClass->captures_begin(), |
| 9493 | CEnd = LambdaClass->captures_end(); C != CEnd; ++C) { |
| 9494 | if (C->capturesVariable()) { |
| 9495 | VarDecl *VD = C->getCapturedVar(); |
| 9496 | if (VD->isInitCapture()) |
| 9497 | S.CurrentInstantiationScope->InstantiatedLocal(VD, VD); |
| 9498 | QualType CaptureType = VD->getType(); |
| 9499 | const bool ByRef = C->getCaptureKind() == LCK_ByRef; |
| 9500 | LSI->addCapture(VD, /*IsBlock*/false, ByRef, |
| 9501 | /*RefersToEnclosingLocal*/true, C->getLocation(), |
| 9502 | /*EllipsisLoc*/C->isPackExpansion() |
| 9503 | ? C->getEllipsisLoc() : SourceLocation(), |
| 9504 | CaptureType, /*Expr*/ 0); |
| 9505 | |
| 9506 | } else if (C->capturesThis()) { |
| 9507 | LSI->addThisCapture(/*Nested*/ false, C->getLocation(), |
| 9508 | S.getCurrentThisType(), /*Expr*/ 0); |
| 9509 | } |
| 9510 | } |
| Faisal Vali | c1a6dc4 | 2013-10-23 16:10:50 +0000 | [diff] [blame] | 9511 | } |
| Francois Pichet | dcb3ebe | 2011-04-22 23:20:44 +0000 | [diff] [blame] | 9512 | |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 9513 | Decl *Sema::ActOnStartOfFunctionDef(Scope *FnBodyScope, Decl *D) { |
| Anders Carlsson | 561f793 | 2009-10-29 15:46:07 +0000 | [diff] [blame] | 9514 | // Clear the last template instantiation error context. |
| 9515 | LastTemplateInstantiationErrorContext = ActiveTemplateInstantiation(); |
| 9516 | |
| Douglas Gregor | 17a7c12 | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 9517 | if (!D) |
| 9518 | return D; |
| Douglas Gregor | c45a40a | 2009-08-22 00:34:47 +0000 | [diff] [blame] | 9519 | FunctionDecl *FD = 0; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9520 | |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 9521 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D)) |
| Douglas Gregor | c45a40a | 2009-08-22 00:34:47 +0000 | [diff] [blame] | 9522 | FD = FunTmpl->getTemplatedDecl(); |
| 9523 | else |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 9524 | FD = cast<FunctionDecl>(D); |
| Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 9525 | // If we are instantiating a generic lambda call operator, push |
| 9526 | // a LambdaScopeInfo onto the function stack. But use the information |
| Faisal Vali | c1a6dc4 | 2013-10-23 16:10:50 +0000 | [diff] [blame] | 9527 | // that's already been calculated (ActOnLambdaExpr) to prime the current |
| 9528 | // LambdaScopeInfo. |
| 9529 | // When the template operator is being specialized, the LambdaScopeInfo, |
| 9530 | // has to be properly restored so that tryCaptureVariable doesn't try |
| 9531 | // and capture any new variables. In addition when calculating potential |
| 9532 | // captures during transformation of nested lambdas, it is necessary to |
| 9533 | // have the LSI properly restored. |
| Faisal Vali | f9a9af3 | 2013-09-29 20:15:45 +0000 | [diff] [blame] | 9534 | if (isGenericLambdaCallOperatorSpecialization(FD)) { |
| Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 9535 | assert(ActiveTemplateInstantiations.size() && |
| 9536 | "There should be an active template instantiation on the stack " |
| 9537 | "when instantiating a generic lambda!"); |
| Faisal Vali | c1a6dc4 | 2013-10-23 16:10:50 +0000 | [diff] [blame] | 9538 | RebuildLambdaScopeInfo(cast<CXXMethodDecl>(D), *this); |
| Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 9539 | } |
| 9540 | else |
| 9541 | // Enter a new function scope |
| 9542 | PushFunctionScope(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9543 | |
| Douglas Gregor | cad304ba | 2008-10-29 15:10:40 +0000 | [diff] [blame] | 9544 | // See if this is a redefinition. |
| Francois Pichet | dcb3ebe | 2011-04-22 23:20:44 +0000 | [diff] [blame] | 9545 | if (!FD->isLateTemplateParsed()) |
| 9546 | CheckForFunctionRedefinition(FD); |
| Douglas Gregor | cad304ba | 2008-10-29 15:10:40 +0000 | [diff] [blame] | 9547 | |
| Douglas Gregor | 75a45ba | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 9548 | // Builtin functions cannot be defined. |
| Douglas Gregor | 15fc956 | 2009-09-12 00:22:50 +0000 | [diff] [blame] | 9549 | if (unsigned BuiltinID = FD->getBuiltinID()) { |
| Rafael Espindola | 3b3a166 | 2013-06-13 18:34:17 +0000 | [diff] [blame] | 9550 | if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID) && |
| 9551 | !Context.BuiltinInfo.isPredefinedRuntimeFunction(BuiltinID)) { |
| Douglas Gregor | 75a45ba | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 9552 | Diag(FD->getLocation(), diag::err_builtin_definition) << FD; |
| Douglas Gregor | 7a0febe | 2009-02-17 16:03:01 +0000 | [diff] [blame] | 9553 | FD->setInvalidDecl(); |
| 9554 | } |
| Douglas Gregor | 75a45ba | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 9555 | } |
| 9556 | |
| Eli Friedman | 9ad7244 | 2009-03-04 07:30:59 +0000 | [diff] [blame] | 9557 | // The return type of a function definition must be complete |
| Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 9558 | // (C99 6.9.1p3, C++ [dcl.fct]p6). |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 9559 | QualType ResultType = FD->getReturnType(); |
| Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 9560 | if (!ResultType->isDependentType() && !ResultType->isVoidType() && |
| Chris Lattner | 0f94c5a | 2009-04-29 05:12:23 +0000 | [diff] [blame] | 9561 | !FD->isInvalidDecl() && |
| Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 9562 | RequireCompleteType(FD->getLocation(), ResultType, |
| 9563 | diag::err_func_def_incomplete_result)) |
| Eli Friedman | 9ad7244 | 2009-03-04 07:30:59 +0000 | [diff] [blame] | 9564 | FD->setInvalidDecl(); |
| Eli Friedman | 9ad7244 | 2009-03-04 07:30:59 +0000 | [diff] [blame] | 9565 | |
| Douglas Gregor | f1b876d | 2009-03-31 16:35:03 +0000 | [diff] [blame] | 9566 | // GNU warning -Wmissing-prototypes: |
| 9567 | // Warn if a global function is defined without a previous |
| 9568 | // prototype declaration. This warning is issued even if the |
| 9569 | // definition itself provides a prototype. The aim is to detect |
| 9570 | // global functions that fail to be declared in header files. |
| Anders Carlsson | 2a45e40 | 2012-12-18 01:29:20 +0000 | [diff] [blame] | 9571 | const FunctionDecl *PossibleZeroParamPrototype = 0; |
| 9572 | if (ShouldWarnAboutMissingPrototype(FD, PossibleZeroParamPrototype)) { |
| Anders Carlsson | 31c7e88 | 2009-12-09 03:30:09 +0000 | [diff] [blame] | 9573 | Diag(FD->getLocation(), diag::warn_missing_prototype) << FD; |
| Richard Smith | ef87be3 | 2013-06-25 20:34:17 +0000 | [diff] [blame] | 9574 | |
| Anders Carlsson | 2a45e40 | 2012-12-18 01:29:20 +0000 | [diff] [blame] | 9575 | if (PossibleZeroParamPrototype) { |
| Richard Smith | ef87be3 | 2013-06-25 20:34:17 +0000 | [diff] [blame] | 9576 | // We found a declaration that is not a prototype, |
| Anders Carlsson | 2a45e40 | 2012-12-18 01:29:20 +0000 | [diff] [blame] | 9577 | // but that could be a zero-parameter prototype |
| Richard Smith | ef87be3 | 2013-06-25 20:34:17 +0000 | [diff] [blame] | 9578 | if (TypeSourceInfo *TI = |
| 9579 | PossibleZeroParamPrototype->getTypeSourceInfo()) { |
| 9580 | TypeLoc TL = TI->getTypeLoc(); |
| 9581 | if (FunctionNoProtoTypeLoc FTL = TL.getAs<FunctionNoProtoTypeLoc>()) |
| 9582 | Diag(PossibleZeroParamPrototype->getLocation(), |
| 9583 | diag::note_declaration_not_a_prototype) |
| 9584 | << PossibleZeroParamPrototype |
| 9585 | << FixItHint::CreateInsertion(FTL.getRParenLoc(), "void"); |
| 9586 | } |
| Anders Carlsson | 2a45e40 | 2012-12-18 01:29:20 +0000 | [diff] [blame] | 9587 | } |
| 9588 | } |
| Douglas Gregor | f1b876d | 2009-03-31 16:35:03 +0000 | [diff] [blame] | 9589 | |
| Douglas Gregor | 67da0d9 | 2009-05-15 17:59:04 +0000 | [diff] [blame] | 9590 | if (FnBodyScope) |
| 9591 | PushDeclContext(FnBodyScope, FD); |
| Anton Korobeynikov | d72f47a | 2008-12-26 00:52:02 +0000 | [diff] [blame] | 9592 | |
| Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 9593 | // Check the validity of our function parameters |
| Douglas Gregor | b524d90 | 2010-11-01 18:37:59 +0000 | [diff] [blame] | 9594 | CheckParmsForFunctionDef(FD->param_begin(), FD->param_end(), |
| 9595 | /*CheckParameterNames=*/true); |
| Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 9596 | |
| 9597 | // Introduce our parameters into the function scope |
| Aaron Ballman | f6bf62e | 2014-03-07 15:12:56 +0000 | [diff] [blame] | 9598 | for (auto Param : FD->params()) { |
| Douglas Gregor | c72e645 | 2009-01-09 18:51:29 +0000 | [diff] [blame] | 9599 | Param->setOwningFunction(FD); |
| 9600 | |
| Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 9601 | // If this has an identifier, add it to the scope stack. |
| John McCall | df8b37c | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 9602 | if (Param->getIdentifier() && FnBodyScope) { |
| Argyrios Kyrtzidis | 1cb0de1 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 9603 | CheckShadow(FnBodyScope, Param); |
| John McCall | df8b37c | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 9604 | |
| Argyrios Kyrtzidis | b8a4920 | 2008-04-12 00:47:19 +0000 | [diff] [blame] | 9605 | PushOnScopeChains(Param, FnBodyScope); |
| John McCall | df8b37c | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 9606 | } |
| Chris Lattner | f61c8a8 | 2007-01-21 19:04:43 +0000 | [diff] [blame] | 9607 | } |
| Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 9608 | |
| James Molloy | 6f8780b | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 9609 | // If we had any tags defined in the function prototype, |
| 9610 | // introduce them into the function scope. |
| 9611 | if (FnBodyScope) { |
| Robert Wilhelm | 16e94b9 | 2013-08-09 18:02:13 +0000 | [diff] [blame] | 9612 | for (ArrayRef<NamedDecl *>::iterator |
| 9613 | I = FD->getDeclsInPrototypeScope().begin(), |
| 9614 | E = FD->getDeclsInPrototypeScope().end(); |
| 9615 | I != E; ++I) { |
| James Molloy | 6f8780b | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 9616 | NamedDecl *D = *I; |
| 9617 | |
| 9618 | // Some of these decls (like enums) may have been pinned to the translation unit |
| 9619 | // for lack of a real context earlier. If so, remove from the translation unit |
| 9620 | // and reattach to the current context. |
| 9621 | if (D->getLexicalDeclContext() == Context.getTranslationUnitDecl()) { |
| 9622 | // Is the decl actually in the context? |
| 9623 | for (DeclContext::decl_iterator DI = Context.getTranslationUnitDecl()->decls_begin(), |
| 9624 | DE = Context.getTranslationUnitDecl()->decls_end(); DI != DE; ++DI) { |
| 9625 | if (*DI == D) { |
| 9626 | Context.getTranslationUnitDecl()->removeDecl(D); |
| 9627 | break; |
| 9628 | } |
| 9629 | } |
| 9630 | // Either way, reassign the lexical decl context to our FunctionDecl. |
| 9631 | D->setLexicalDeclContext(CurContext); |
| 9632 | } |
| 9633 | |
| 9634 | // If the decl has a non-null name, make accessible in the current scope. |
| 9635 | if (!D->getName().empty()) |
| 9636 | PushOnScopeChains(D, FnBodyScope, /*AddToContext=*/false); |
| 9637 | |
| 9638 | // Similarly, dive into enums and fish their constants out, making them |
| 9639 | // accessible in this scope. |
| 9640 | if (EnumDecl *ED = dyn_cast<EnumDecl>(D)) { |
| 9641 | for (EnumDecl::enumerator_iterator EI = ED->enumerator_begin(), |
| 9642 | EE = ED->enumerator_end(); EI != EE; ++EI) |
| David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 9643 | PushOnScopeChains(*EI, FnBodyScope, /*AddToContext=*/false); |
| James Molloy | 6f8780b | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 9644 | } |
| 9645 | } |
| 9646 | } |
| 9647 | |
| Richard Smith | 79a52e5 | 2012-04-17 22:30:01 +0000 | [diff] [blame] | 9648 | // Ensure that the function's exception specification is instantiated. |
| 9649 | if (const FunctionProtoType *FPT = FD->getType()->getAs<FunctionProtoType>()) |
| 9650 | ResolveExceptionSpec(D->getLocation(), FPT); |
| 9651 | |
| Anton Korobeynikov | d72f47a | 2008-12-26 00:52:02 +0000 | [diff] [blame] | 9652 | // Checking attributes of current function definition |
| 9653 | // dllimport attribute. |
| Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 9654 | DLLImportAttr *DA = FD->getAttr<DLLImportAttr>(); |
| Aaron Ballman | 9ead124 | 2013-12-19 02:39:40 +0000 | [diff] [blame] | 9655 | if (DA && (!FD->hasAttr<DLLExportAttr>())) { |
| Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 9656 | // dllimport attribute cannot be directly applied to definition. |
| Francois Pichet | 3096d20 | 2011-03-29 10:39:17 +0000 | [diff] [blame] | 9657 | // Microsoft accepts dllimport for functions defined within class scope. |
| 9658 | if (!DA->isInherited() && |
| Francois Pichet | 0706d20 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 9659 | !(LangOpts.MicrosoftExt && FD->getLexicalDeclContext()->isRecord())) { |
| Anton Korobeynikov | d72f47a | 2008-12-26 00:52:02 +0000 | [diff] [blame] | 9660 | Diag(FD->getLocation(), |
| 9661 | diag::err_attribute_can_be_applied_only_to_symbol_declaration) |
| Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 9662 | << DA; |
| Anton Korobeynikov | d72f47a | 2008-12-26 00:52:02 +0000 | [diff] [blame] | 9663 | FD->setInvalidDecl(); |
| Argyrios Kyrtzidis | 26444c5 | 2012-12-14 06:54:03 +0000 | [diff] [blame] | 9664 | return D; |
| Ted Kremenek | a3cfc4d | 2010-02-21 05:12:53 +0000 | [diff] [blame] | 9665 | } |
| 9666 | |
| 9667 | // Visual C++ appears to not think this is an issue, so only issue |
| 9668 | // a warning when Microsoft extensions are disabled. |
| Francois Pichet | 0706d20 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 9669 | if (!LangOpts.MicrosoftExt) { |
| Anton Korobeynikov | d72f47a | 2008-12-26 00:52:02 +0000 | [diff] [blame] | 9670 | // If a symbol previously declared dllimport is later defined, the |
| 9671 | // attribute is ignored in subsequent references, and a warning is |
| 9672 | // emitted. |
| 9673 | Diag(FD->getLocation(), |
| 9674 | diag::warn_redeclaration_without_attribute_prev_attribute_ignored) |
| Aaron Ballman | 44ebc07 | 2014-01-02 22:29:41 +0000 | [diff] [blame] | 9675 | << FD << DA; |
| Anton Korobeynikov | d72f47a | 2008-12-26 00:52:02 +0000 | [diff] [blame] | 9676 | } |
| 9677 | } |
| Dmitri Gribenko | b261088 | 2012-08-14 17:17:18 +0000 | [diff] [blame] | 9678 | // We want to attach documentation to original Decl (which might be |
| 9679 | // a function template). |
| 9680 | ActOnDocumentableDecl(D); |
| Argyrios Kyrtzidis | 26444c5 | 2012-12-14 06:54:03 +0000 | [diff] [blame] | 9681 | return D; |
| Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 9682 | } |
| 9683 | |
| Douglas Gregor | 6fd1b18 | 2010-05-15 06:01:05 +0000 | [diff] [blame] | 9684 | /// \brief Given the set of return statements within a function body, |
| 9685 | /// compute the variables that are subject to the named return value |
| 9686 | /// optimization. |
| 9687 | /// |
| 9688 | /// Each of the variables that is subject to the named return value |
| 9689 | /// optimization will be marked as NRVO variables in the AST, and any |
| 9690 | /// return statement that has a marked NRVO variable as its NRVO candidate can |
| 9691 | /// use the named return value optimization. |
| 9692 | /// |
| 9693 | /// This function applies a very simplistic algorithm for NRVO: if every return |
| 9694 | /// statement in the function has the same NRVO candidate, that candidate is |
| 9695 | /// the NRVO variable. |
| 9696 | /// |
| 9697 | /// FIXME: Employ a smarter algorithm that accounts for multiple return |
| 9698 | /// statements and the lifetimes of the NRVO candidates. We should be able to |
| 9699 | /// find a maximal set of NRVO variables. |
| Douglas Gregor | 49695f0 | 2011-09-06 20:46:03 +0000 | [diff] [blame] | 9700 | void Sema::computeNRVO(Stmt *Body, FunctionScopeInfo *Scope) { |
| John McCall | aab3e41 | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 9701 | ReturnStmt **Returns = Scope->Returns.data(); |
| 9702 | |
| Douglas Gregor | 6fd1b18 | 2010-05-15 06:01:05 +0000 | [diff] [blame] | 9703 | const VarDecl *NRVOCandidate = 0; |
| John McCall | aab3e41 | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 9704 | for (unsigned I = 0, E = Scope->Returns.size(); I != E; ++I) { |
| Douglas Gregor | 6fd1b18 | 2010-05-15 06:01:05 +0000 | [diff] [blame] | 9705 | if (!Returns[I]->getNRVOCandidate()) |
| 9706 | return; |
| 9707 | |
| 9708 | if (!NRVOCandidate) |
| 9709 | NRVOCandidate = Returns[I]->getNRVOCandidate(); |
| 9710 | else if (NRVOCandidate != Returns[I]->getNRVOCandidate()) |
| 9711 | return; |
| 9712 | } |
| 9713 | |
| 9714 | if (NRVOCandidate) |
| 9715 | const_cast<VarDecl*>(NRVOCandidate)->setNRVOVariable(true); |
| 9716 | } |
| 9717 | |
| Richard Smith | 1ab34b3 | 2012-11-19 21:13:18 +0000 | [diff] [blame] | 9718 | bool Sema::canSkipFunctionBody(Decl *D) { |
| Richard Smith | 1ab34b3 | 2012-11-19 21:13:18 +0000 | [diff] [blame] | 9719 | // We cannot skip the body of a function (or function template) which is |
| 9720 | // constexpr, since we may need to evaluate its body in order to parse the |
| 9721 | // rest of the file. |
| Richard Smith | 7500ab2 | 2013-05-10 04:31:10 +0000 | [diff] [blame] | 9722 | // We cannot skip the body of a function with an undeduced return type, |
| 9723 | // because any callers of that function need to know the type. |
| Alp Toker | a2794f9 | 2014-01-22 07:29:52 +0000 | [diff] [blame] | 9724 | if (const FunctionDecl *FD = D->getAsFunction()) |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 9725 | if (FD->isConstexpr() || FD->getReturnType()->isUndeducedType()) |
| Alp Toker | a2794f9 | 2014-01-22 07:29:52 +0000 | [diff] [blame] | 9726 | return false; |
| 9727 | return Consumer.shouldSkipFunctionBody(D); |
| Richard Smith | 1ab34b3 | 2012-11-19 21:13:18 +0000 | [diff] [blame] | 9728 | } |
| 9729 | |
| Argyrios Kyrtzidis | 1eb71a1 | 2012-12-06 18:59:10 +0000 | [diff] [blame] | 9730 | Decl *Sema::ActOnSkippedFunctionBody(Decl *Decl) { |
| Argyrios Kyrtzidis | 4431918 | 2013-02-22 04:11:06 +0000 | [diff] [blame] | 9731 | if (FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(Decl)) |
| Argyrios Kyrtzidis | 1eb71a1 | 2012-12-06 18:59:10 +0000 | [diff] [blame] | 9732 | FD->setHasSkippedBody(); |
| Argyrios Kyrtzidis | 4431918 | 2013-02-22 04:11:06 +0000 | [diff] [blame] | 9733 | else if (ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(Decl)) |
| Argyrios Kyrtzidis | 1eb71a1 | 2012-12-06 18:59:10 +0000 | [diff] [blame] | 9734 | MD->setHasSkippedBody(); |
| 9735 | return ActOnFinishFunctionBody(Decl, 0); |
| 9736 | } |
| 9737 | |
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 9738 | Decl *Sema::ActOnFinishFunctionBody(Decl *D, Stmt *BodyArg) { |
| Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 9739 | return ActOnFinishFunctionBody(D, BodyArg, false); |
| Douglas Gregor | 67da0d9 | 2009-05-15 17:59:04 +0000 | [diff] [blame] | 9740 | } |
| 9741 | |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9742 | Decl *Sema::ActOnFinishFunctionBody(Decl *dcl, Stmt *Body, |
| 9743 | bool IsInstantiation) { |
| Alp Toker | a2794f9 | 2014-01-22 07:29:52 +0000 | [diff] [blame] | 9744 | FunctionDecl *FD = dcl ? dcl->getAsFunction() : 0; |
| Douglas Gregor | c45a40a | 2009-08-22 00:34:47 +0000 | [diff] [blame] | 9745 | |
| Ted Kremenek | 0b40532 | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 9746 | sema::AnalysisBasedWarnings::Policy WP = AnalysisWarnings.getDefaultPolicy(); |
| Ted Kremenek | 1767a27 | 2011-02-23 01:51:48 +0000 | [diff] [blame] | 9747 | sema::AnalysisBasedWarnings::Policy *ActivePolicy = 0; |
| Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 9748 | |
| Douglas Gregor | c45a40a | 2009-08-22 00:34:47 +0000 | [diff] [blame] | 9749 | if (FD) { |
| Chris Lattner | 960cc52 | 2009-04-18 09:36:27 +0000 | [diff] [blame] | 9750 | FD->setBody(Body); |
| John McCall | 5ed3caf | 2012-02-14 19:50:52 +0000 | [diff] [blame] | 9751 | |
| Richard Smith | 7500ab2 | 2013-05-10 04:31:10 +0000 | [diff] [blame] | 9752 | if (getLangOpts().CPlusPlus1y && !FD->isInvalidDecl() && Body && |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 9753 | !FD->isDependentContext() && FD->getReturnType()->isUndeducedType()) { |
| Richard Smith | 7500ab2 | 2013-05-10 04:31:10 +0000 | [diff] [blame] | 9754 | // If the function has a deduced result type but contains no 'return' |
| 9755 | // statements, the result type as written must be exactly 'auto', and |
| 9756 | // the deduced result type is 'void'. |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 9757 | if (!FD->getReturnType()->getAs<AutoType>()) { |
| Richard Smith | 7500ab2 | 2013-05-10 04:31:10 +0000 | [diff] [blame] | 9758 | Diag(dcl->getLocation(), diag::err_auto_fn_no_return_but_not_auto) |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 9759 | << FD->getReturnType(); |
| Richard Smith | 7500ab2 | 2013-05-10 04:31:10 +0000 | [diff] [blame] | 9760 | FD->setInvalidDecl(); |
| 9761 | } else { |
| 9762 | // Substitute 'void' for the 'auto' in the type. |
| 9763 | TypeLoc ResultType = FD->getTypeSourceInfo()->getTypeLoc(). |
| Alp Toker | 42a16a6 | 2014-01-25 23:51:36 +0000 | [diff] [blame] | 9764 | IgnoreParens().castAs<FunctionProtoTypeLoc>().getReturnLoc(); |
| Richard Smith | 7500ab2 | 2013-05-10 04:31:10 +0000 | [diff] [blame] | 9765 | Context.adjustDeducedFunctionResultType( |
| 9766 | FD, SubstAutoType(ResultType.getType(), Context.VoidTy)); |
| Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 9767 | } |
| 9768 | } |
| 9769 | |
| Nick Lewycky | 9c7eb1d | 2013-02-01 08:13:20 +0000 | [diff] [blame] | 9770 | // The only way to be included in UndefinedButUsed is if there is an |
| 9771 | // ODR use before the definition. Avoid the expensive map lookup if this |
| Nick Lewycky | f0f5616 | 2013-01-31 03:23:57 +0000 | [diff] [blame] | 9772 | // is the first declaration. |
| Rafael Espindola | 3f9e444 | 2013-10-19 02:13:21 +0000 | [diff] [blame] | 9773 | if (!FD->isFirstDecl() && FD->getPreviousDecl()->isUsed()) { |
| Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 9774 | if (!FD->isExternallyVisible()) |
| Nick Lewycky | 9c7eb1d | 2013-02-01 08:13:20 +0000 | [diff] [blame] | 9775 | UndefinedButUsed.erase(FD); |
| 9776 | else if (FD->isInlined() && |
| 9777 | (LangOpts.CPlusPlus || !LangOpts.GNUInline) && |
| 9778 | (!FD->getPreviousDecl()->hasAttr<GNUInlineAttr>())) |
| 9779 | UndefinedButUsed.erase(FD); |
| 9780 | } |
| Nick Lewycky | f0f5616 | 2013-01-31 03:23:57 +0000 | [diff] [blame] | 9781 | |
| John McCall | 5ed3caf | 2012-02-14 19:50:52 +0000 | [diff] [blame] | 9782 | // If the function implicitly returns zero (like 'main') or is naked, |
| 9783 | // don't complain about missing return statements. |
| 9784 | if (FD->hasImplicitReturnZero() || FD->hasAttr<NakedAttr>()) |
| Ted Kremenek | 0b40532 | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 9785 | WP.disableCheckFallThrough(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9786 | |
| Francois Pichet | 3abc9b8 | 2011-05-11 02:14:46 +0000 | [diff] [blame] | 9787 | // MSVC permits the use of pure specifier (=0) on function definition, |
| Alp Toker | d473363 | 2013-12-05 04:47:09 +0000 | [diff] [blame] | 9788 | // defined at class scope, warn about this non-standard construct. |
| Reid Kleckner | be7a446 | 2013-10-08 22:45:29 +0000 | [diff] [blame] | 9789 | if (getLangOpts().MicrosoftExt && FD->isPure() && FD->isCanonicalDecl()) |
| Francois Pichet | 3abc9b8 | 2011-05-11 02:14:46 +0000 | [diff] [blame] | 9790 | Diag(FD->getLocation(), diag::warn_pure_function_definition); |
| 9791 | |
| Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 9792 | if (!FD->isInvalidDecl()) { |
| Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9793 | DiagnoseUnusedParameters(FD->param_begin(), FD->param_end()); |
| Argyrios Kyrtzidis | af84ec0 | 2010-11-17 23:11:54 +0000 | [diff] [blame] | 9794 | DiagnoseSizeOfParametersAndReturnValue(FD->param_begin(), FD->param_end(), |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 9795 | FD->getReturnType(), FD); |
| 9796 | |
| Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 9797 | // If this is a constructor, we need a vtable. |
| 9798 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(FD)) |
| 9799 | MarkVTableUsed(FD->getLocation(), Constructor->getParent()); |
| Douglas Gregor | 6fd1b18 | 2010-05-15 06:01:05 +0000 | [diff] [blame] | 9800 | |
| Jordan Rose | d39e5f1 | 2012-07-02 21:19:23 +0000 | [diff] [blame] | 9801 | // Try to apply the named return value optimization. We have to check |
| 9802 | // if we can do this here because lambdas keep return statements around |
| 9803 | // to deduce an implicit return type. |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 9804 | if (getLangOpts().CPlusPlus && FD->getReturnType()->isRecordType() && |
| Jordan Rose | d39e5f1 | 2012-07-02 21:19:23 +0000 | [diff] [blame] | 9805 | !FD->isDependentContext()) |
| 9806 | computeNRVO(Body, getCurFunction()); |
| Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 9807 | } |
| 9808 | |
| Douglas Gregor | 21f4692 | 2012-02-08 20:17:14 +0000 | [diff] [blame] | 9809 | assert((FD == getCurFunctionDecl() || getCurLambda()->CallOperator == FD) && |
| 9810 | "Function parsing confused"); |
| Steve Naroff | 542cd5d | 2008-07-25 17:57:26 +0000 | [diff] [blame] | 9811 | } else if (ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(dcl)) { |
| Chris Lattner | 1598a3a | 2009-02-16 19:27:54 +0000 | [diff] [blame] | 9812 | assert(MD == getCurMethodDecl() && "Method parsing confused"); |
| Chris Lattner | 960cc52 | 2009-04-18 09:36:27 +0000 | [diff] [blame] | 9813 | MD->setBody(Body); |
| Argyrios Kyrtzidis | af84ec0 | 2010-11-17 23:11:54 +0000 | [diff] [blame] | 9814 | if (!MD->isInvalidDecl()) { |
| Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9815 | DiagnoseUnusedParameters(MD->param_begin(), MD->param_end()); |
| Argyrios Kyrtzidis | af84ec0 | 2010-11-17 23:11:54 +0000 | [diff] [blame] | 9816 | DiagnoseSizeOfParametersAndReturnValue(MD->param_begin(), MD->param_end(), |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 9817 | MD->getReturnType(), MD); |
| 9818 | |
| Douglas Gregor | e3f3ea0 | 2011-09-06 20:33:37 +0000 | [diff] [blame] | 9819 | if (Body) |
| Douglas Gregor | 49695f0 | 2011-09-06 20:46:03 +0000 | [diff] [blame] | 9820 | computeNRVO(Body, getCurFunction()); |
| Argyrios Kyrtzidis | af84ec0 | 2010-11-17 23:11:54 +0000 | [diff] [blame] | 9821 | } |
| Jordan Rose | 2afd661 | 2012-10-19 16:05:26 +0000 | [diff] [blame] | 9822 | if (getCurFunction()->ObjCShouldCallSuper) { |
| Fariborz Jahanian | b05417e | 2012-09-10 16:51:09 +0000 | [diff] [blame] | 9823 | Diag(MD->getLocEnd(), diag::warn_objc_missing_super_call) |
| 9824 | << MD->getSelector().getAsString(); |
| Jordan Rose | 2afd661 | 2012-10-19 16:05:26 +0000 | [diff] [blame] | 9825 | getCurFunction()->ObjCShouldCallSuper = false; |
| Nico Weber | 1fb8266 | 2011-08-28 22:35:17 +0000 | [diff] [blame] | 9826 | } |
| Argyrios Kyrtzidis | 22bfa2c | 2013-12-03 21:11:36 +0000 | [diff] [blame] | 9827 | if (getCurFunction()->ObjCWarnForNoDesignatedInitChain) { |
| 9828 | const ObjCMethodDecl *InitMethod = 0; |
| Argyrios Kyrtzidis | fcded9b | 2013-12-03 21:11:43 +0000 | [diff] [blame] | 9829 | bool isDesignated = |
| 9830 | MD->isDesignatedInitializerForTheInterface(&InitMethod); |
| Argyrios Kyrtzidis | 22bfa2c | 2013-12-03 21:11:36 +0000 | [diff] [blame] | 9831 | assert(isDesignated && InitMethod); |
| 9832 | (void)isDesignated; |
| 9833 | Diag(MD->getLocation(), |
| 9834 | diag::warn_objc_designated_init_missing_super_call); |
| 9835 | Diag(InitMethod->getLocation(), |
| 9836 | diag::note_objc_designated_init_marked_here); |
| 9837 | getCurFunction()->ObjCWarnForNoDesignatedInitChain = false; |
| 9838 | } |
| Argyrios Kyrtzidis | b66d3cf | 2013-12-03 21:11:49 +0000 | [diff] [blame] | 9839 | if (getCurFunction()->ObjCWarnForNoInitDelegation) { |
| 9840 | Diag(MD->getLocation(), diag::warn_objc_secondary_init_missing_init_call); |
| 9841 | getCurFunction()->ObjCWarnForNoInitDelegation = false; |
| 9842 | } |
| Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 9843 | } else { |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 9844 | return 0; |
| Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 9845 | } |
| Douglas Gregor | 67da0d9 | 2009-05-15 17:59:04 +0000 | [diff] [blame] | 9846 | |
| Jordan Rose | 2afd661 | 2012-10-19 16:05:26 +0000 | [diff] [blame] | 9847 | assert(!getCurFunction()->ObjCShouldCallSuper && |
| Eli Friedman | 22be06a | 2012-08-01 21:02:59 +0000 | [diff] [blame] | 9848 | "This should only be set for ObjC methods, which should have been " |
| 9849 | "handled in the block above."); |
| Nico Weber | 715abaf | 2011-08-22 17:25:57 +0000 | [diff] [blame] | 9850 | |
| Chris Lattner | e247306 | 2007-05-28 06:28:18 +0000 | [diff] [blame] | 9851 | // Verify and clean out per-function state. |
| Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 9852 | if (Body) { |
| Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 9853 | // C++ constructors that have function-try-blocks can't have return |
| 9854 | // statements in the handlers of that block. (C++ [except.handle]p14) |
| 9855 | // Verify this. |
| 9856 | if (FD && isa<CXXConstructorDecl>(FD) && isa<CXXTryStmt>(Body)) |
| 9857 | DiagnoseReturnInConstructorExceptionHandler(cast<CXXTryStmt>(Body)); |
| 9858 | |
| Richard Smith | def8bdb | 2011-08-12 18:44:32 +0000 | [diff] [blame] | 9859 | // Verify that gotos and switch cases don't jump into scopes illegally. |
| John McCall | aab3e41 | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 9860 | if (getCurFunction()->NeedsScopeChecking() && |
| John McCall | 58efb8e | 2010-05-20 07:13:26 +0000 | [diff] [blame] | 9861 | !dcl->isInvalidDecl() && |
| Douglas Gregor | 5d944db | 2012-08-17 05:12:08 +0000 | [diff] [blame] | 9862 | !hasAnyUnrecoverableErrorsInThisFunction() && |
| 9863 | !PP.isCodeCompletionEnabled()) |
| Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 9864 | DiagnoseInvalidJumps(Body); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9865 | |
| John McCall | deb646e | 2010-08-04 01:04:25 +0000 | [diff] [blame] | 9866 | if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(dcl)) { |
| 9867 | if (!Destructor->getParent()->isDependentType()) |
| 9868 | CheckDestructor(Destructor); |
| 9869 | |
| John McCall | a630995 | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 9870 | MarkBaseAndMemberDestructorsReferenced(Destructor->getLocation(), |
| 9871 | Destructor->getParent()); |
| John McCall | deb646e | 2010-08-04 01:04:25 +0000 | [diff] [blame] | 9872 | } |
| Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 9873 | |
| 9874 | // If any errors have occurred, clear out any temporaries that may have |
| 9875 | // been leftover. This ensures that these temporaries won't be picked up for |
| 9876 | // deletion in some later function. |
| Douglas Gregor | 550b98b | 2011-03-04 23:08:02 +0000 | [diff] [blame] | 9877 | if (PP.getDiagnostics().hasErrorOccurred() || |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9878 | PP.getDiagnostics().getSuppressAllDiagnostics()) { |
| John McCall | 28fc709 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 9879 | DiscardCleanupsInEvaluationContext(); |
| DeLesley Hutchins | 8ecd491 | 2012-12-07 22:53:48 +0000 | [diff] [blame] | 9880 | } |
| 9881 | if (!PP.getDiagnostics().hasUncompilableErrorOccurred() && |
| 9882 | !isa<FunctionTemplateDecl>(dcl)) { |
| Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 9883 | // Since the body is valid, issue any analysis-based warnings that are |
| 9884 | // enabled. |
| Ted Kremenek | 1767a27 | 2011-02-23 01:51:48 +0000 | [diff] [blame] | 9885 | ActivePolicy = &WP; |
| Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 9886 | } |
| 9887 | |
| Richard Smith | 3607ffe | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 9888 | if (!IsInstantiation && FD && FD->isConstexpr() && !FD->isInvalidDecl() && |
| 9889 | (!CheckConstexprFunctionDecl(FD) || |
| 9890 | !CheckConstexprFunctionBody(FD, Body))) |
| Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 9891 | FD->setInvalidDecl(); |
| 9892 | |
| John McCall | 28fc709 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 9893 | assert(ExprCleanupObjects.empty() && "Leftover temporaries in function"); |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9894 | assert(!ExprNeedsCleanups && "Unaccounted cleanups in function"); |
| Eli Friedman | 3bda6b1 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 9895 | assert(MaybeODRUseExprs.empty() && |
| 9896 | "Leftover expressions for odr-use checking"); |
| Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 9897 | } |
| 9898 | |
| John McCall | e99d5f3 | 2010-03-25 22:08:03 +0000 | [diff] [blame] | 9899 | if (!IsInstantiation) |
| 9900 | PopDeclContext(); |
| 9901 | |
| Eli Friedman | 71c8055 | 2012-01-05 03:35:19 +0000 | [diff] [blame] | 9902 | PopFunctionScopeInfo(ActivePolicy, dcl); |
| Douglas Gregor | a7e3ea3 | 2009-11-15 07:07:58 +0000 | [diff] [blame] | 9903 | // If any errors have occurred, clear out any temporaries that may have |
| 9904 | // been leftover. This ensures that these temporaries won't be picked up for |
| 9905 | // deletion in some later function. |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9906 | if (getDiagnostics().hasErrorOccurred()) { |
| John McCall | 28fc709 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 9907 | DiscardCleanupsInEvaluationContext(); |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9908 | } |
| Argyrios Kyrtzidis | 35672e7 | 2010-08-13 18:42:17 +0000 | [diff] [blame] | 9909 | |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 9910 | return dcl; |
| Fariborz Jahanian | 85e1d0d | 2007-11-10 16:31:34 +0000 | [diff] [blame] | 9911 | } |
| 9912 | |
| Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 9913 | |
| 9914 | /// When we finish delayed parsing of an attribute, we must attach it to the |
| 9915 | /// relevant Decl. |
| 9916 | void Sema::ActOnFinishDelayedAttribute(Scope *S, Decl *D, |
| 9917 | ParsedAttributes &Attrs) { |
| DeLesley Hutchins | ceec306 | 2012-01-20 22:37:06 +0000 | [diff] [blame] | 9918 | // Always attach attributes to the underlying decl. |
| 9919 | if (TemplateDecl *TD = dyn_cast<TemplateDecl>(D)) |
| 9920 | D = TD->getTemplatedDecl(); |
| Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 9921 | ProcessDeclAttributeList(S, D, Attrs.getList()); |
| 9922 | |
| 9923 | if (CXXMethodDecl *Method = dyn_cast_or_null<CXXMethodDecl>(D)) |
| 9924 | if (Method->isStatic()) |
| 9925 | checkThisInStaticMemberFunctionAttributes(Method); |
| Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 9926 | } |
| 9927 | |
| 9928 | |
| Chris Lattner | ac18be9 | 2006-11-20 06:49:47 +0000 | [diff] [blame] | 9929 | /// ImplicitlyDefineFunction - An undeclared identifier was used in a function |
| 9930 | /// call, forming a call to an implicitly defined function (per C99 6.5.1p2). |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9931 | NamedDecl *Sema::ImplicitlyDefineFunction(SourceLocation Loc, |
| Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 9932 | IdentifierInfo &II, Scope *S) { |
| Douglas Gregor | 5a80bd1 | 2009-03-02 00:19:53 +0000 | [diff] [blame] | 9933 | // Before we produce a declaration for an implicitly defined |
| 9934 | // function, see whether there was a locally-scoped declaration of |
| 9935 | // this name as a function or variable. If so, use that |
| 9936 | // (non-visible) declaration, and complain about it. |
| Richard Smith | 39b7968 | 2013-06-18 20:15:12 +0000 | [diff] [blame] | 9937 | if (NamedDecl *ExternCPrev = findLocallyScopedExternCDecl(&II)) { |
| 9938 | Diag(Loc, diag::warn_use_out_of_scope_declaration) << ExternCPrev; |
| 9939 | Diag(ExternCPrev->getLocation(), diag::note_previous_declaration); |
| 9940 | return ExternCPrev; |
| Douglas Gregor | 5a80bd1 | 2009-03-02 00:19:53 +0000 | [diff] [blame] | 9941 | } |
| 9942 | |
| Chris Lattner | 00e2607 | 2008-05-05 21:18:06 +0000 | [diff] [blame] | 9943 | // Extension in C99. Legal in C90, but warn about it. |
| Hans Wennborg | 70a1324 | 2011-12-08 15:56:07 +0000 | [diff] [blame] | 9944 | unsigned diag_id; |
| Daniel Dunbar | 07d0785 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 9945 | if (II.getName().startswith("__builtin_")) |
| Abramo Bagnara | 3732fa5 | 2012-01-09 10:05:48 +0000 | [diff] [blame] | 9946 | diag_id = diag::warn_builtin_unknown; |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9947 | else if (getLangOpts().C99) |
| Hans Wennborg | 70a1324 | 2011-12-08 15:56:07 +0000 | [diff] [blame] | 9948 | diag_id = diag::ext_implicit_function_decl; |
| Chris Lattner | 00e2607 | 2008-05-05 21:18:06 +0000 | [diff] [blame] | 9949 | else |
| Hans Wennborg | 70a1324 | 2011-12-08 15:56:07 +0000 | [diff] [blame] | 9950 | diag_id = diag::warn_implicit_function_decl; |
| 9951 | Diag(Loc, diag_id) << &II; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9952 | |
| Hans Wennborg | 70a1324 | 2011-12-08 15:56:07 +0000 | [diff] [blame] | 9953 | // Because typo correction is expensive, only do it if the implicit |
| 9954 | // function declaration is going to be treated as an error. |
| 9955 | if (Diags.getDiagnosticLevel(diag_id, Loc) >= DiagnosticsEngine::Error) { |
| 9956 | TypoCorrection Corrected; |
| Kaelyn Uhrain | b137840 | 2012-01-18 21:41:41 +0000 | [diff] [blame] | 9957 | DeclFilterCCC<FunctionDecl> Validator; |
| Hans Wennborg | 70a1324 | 2011-12-08 15:56:07 +0000 | [diff] [blame] | 9958 | if (S && (Corrected = CorrectTypo(DeclarationNameInfo(&II, Loc), |
| Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 9959 | LookupOrdinaryName, S, 0, Validator))) |
| 9960 | diagnoseTypo(Corrected, PDiag(diag::note_function_suggestion), |
| 9961 | /*ErrorRecovery*/false); |
| Hans Wennborg | 2fb8b91 | 2011-12-06 09:46:12 +0000 | [diff] [blame] | 9962 | } |
| 9963 | |
| Chris Lattner | ac18be9 | 2006-11-20 06:49:47 +0000 | [diff] [blame] | 9964 | // Set a Declarator for the implicit definition: int foo(); |
| Chris Lattner | 353f574 | 2006-11-28 04:50:12 +0000 | [diff] [blame] | 9965 | const char *Dummy; |
| John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 9966 | AttributeFactory attrFactory; |
| 9967 | DeclSpec DS(attrFactory); |
| John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 9968 | unsigned DiagID; |
| Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 9969 | bool Error = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, Dummy, DiagID, |
| 9970 | Context.getPrintingPolicy()); |
| Jeffrey Yasskin | b332153 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 9971 | (void)Error; // Silence warning. |
| Chris Lattner | 353f574 | 2006-11-28 04:50:12 +0000 | [diff] [blame] | 9972 | assert(!Error && "Error setting up implicit decl!"); |
| Abramo Bagnara | aeeb989 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 9973 | SourceLocation NoLoc; |
| Chris Lattner | ac18be9 | 2006-11-20 06:49:47 +0000 | [diff] [blame] | 9974 | Declarator D(DS, Declarator::BlockContext); |
| Abramo Bagnara | aeeb989 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 9975 | D.AddTypeInfo(DeclaratorChunk::getFunction(/*HasProto=*/false, |
| 9976 | /*IsAmbiguous=*/false, |
| 9977 | /*RParenLoc=*/NoLoc, |
| 9978 | /*ArgInfo=*/0, |
| 9979 | /*NumArgs=*/0, |
| 9980 | /*EllipsisLoc=*/NoLoc, |
| 9981 | /*RParenLoc=*/NoLoc, |
| 9982 | /*TypeQuals=*/0, |
| 9983 | /*RefQualifierIsLvalueRef=*/true, |
| 9984 | /*RefQualifierLoc=*/NoLoc, |
| 9985 | /*ConstQualifierLoc=*/NoLoc, |
| 9986 | /*VolatileQualifierLoc=*/NoLoc, |
| 9987 | /*MutableLoc=*/NoLoc, |
| 9988 | EST_None, |
| 9989 | /*ESpecLoc=*/NoLoc, |
| 9990 | /*Exceptions=*/0, |
| 9991 | /*ExceptionRanges=*/0, |
| 9992 | /*NumExceptions=*/0, |
| 9993 | /*NoexceptExpr=*/0, |
| 9994 | Loc, Loc, D), |
| John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 9995 | DS.getAttributes(), |
| Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 9996 | SourceLocation()); |
| Chris Lattner | ac18be9 | 2006-11-20 06:49:47 +0000 | [diff] [blame] | 9997 | D.SetIdentifier(&II, Loc); |
| Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 9998 | |
| Argyrios Kyrtzidis | 694dda1 | 2008-05-01 21:04:16 +0000 | [diff] [blame] | 9999 | // Insert this function into translation-unit scope. |
| 10000 | |
| 10001 | DeclContext *PrevDC = CurContext; |
| 10002 | CurContext = Context.getTranslationUnitDecl(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10003 | |
| Jordan Rose | d03d99d | 2013-03-05 01:27:54 +0000 | [diff] [blame] | 10004 | FunctionDecl *FD = cast<FunctionDecl>(ActOnDeclarator(TUScope, D)); |
| Steve Naroff | 3913ea4 | 2008-04-04 14:32:09 +0000 | [diff] [blame] | 10005 | FD->setImplicit(); |
| Argyrios Kyrtzidis | 694dda1 | 2008-05-01 21:04:16 +0000 | [diff] [blame] | 10006 | |
| 10007 | CurContext = PrevDC; |
| 10008 | |
| Douglas Gregor | e711f70 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 10009 | AddKnownFunctionAttributes(FD); |
| 10010 | |
| Steve Naroff | 3913ea4 | 2008-04-04 14:32:09 +0000 | [diff] [blame] | 10011 | return FD; |
| Chris Lattner | ac18be9 | 2006-11-20 06:49:47 +0000 | [diff] [blame] | 10012 | } |
| 10013 | |
| Douglas Gregor | e711f70 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 10014 | /// \brief Adds any function attributes that we know a priori based on |
| 10015 | /// the declaration of this function. |
| 10016 | /// |
| 10017 | /// These attributes can apply both to implicitly-declared builtins |
| 10018 | /// (like __builtin___printf_chk) or to library-declared functions |
| 10019 | /// like NSLog or printf. |
| Douglas Gregor | 8833683 | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 10020 | /// |
| 10021 | /// We need to check for duplicate attributes both here and where user-written |
| 10022 | /// attributes are applied to declarations. |
| Douglas Gregor | e711f70 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 10023 | void Sema::AddKnownFunctionAttributes(FunctionDecl *FD) { |
| 10024 | if (FD->isInvalidDecl()) |
| 10025 | return; |
| 10026 | |
| 10027 | // If this is a built-in function, map its builtin attributes to |
| 10028 | // actual attributes. |
| Douglas Gregor | 15fc956 | 2009-09-12 00:22:50 +0000 | [diff] [blame] | 10029 | if (unsigned BuiltinID = FD->getBuiltinID()) { |
| Douglas Gregor | e711f70 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 10030 | // Handle printf-formatting attributes. |
| 10031 | unsigned FormatIdx; |
| 10032 | bool HasVAListArg; |
| 10033 | if (Context.BuiltinInfo.isPrintfLike(BuiltinID, FormatIdx, HasVAListArg)) { |
| Aaron Ballman | 9ead124 | 2013-12-19 02:39:40 +0000 | [diff] [blame] | 10034 | if (!FD->hasAttr<FormatAttr>()) { |
| Jean-Daniel Dupas | 78536ae | 2012-01-24 22:32:46 +0000 | [diff] [blame] | 10035 | const char *fmt = "printf"; |
| 10036 | unsigned int NumParams = FD->getNumParams(); |
| 10037 | if (FormatIdx < NumParams && // NumParams may be 0 (e.g. vfprintf) |
| 10038 | FD->getParamDecl(FormatIdx)->getType()->isObjCObjectPointerType()) |
| 10039 | fmt = "NSString"; |
| Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 10040 | FD->addAttr(FormatAttr::CreateImplicit(Context, |
| Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 10041 | &Context.Idents.get(fmt), |
| 10042 | FormatIdx+1, |
| Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 10043 | HasVAListArg ? 0 : FormatIdx+2, |
| 10044 | FD->getLocation())); |
| Jean-Daniel Dupas | 78536ae | 2012-01-24 22:32:46 +0000 | [diff] [blame] | 10045 | } |
| Douglas Gregor | e711f70 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 10046 | } |
| Ted Kremenek | 5932c35 | 2010-07-16 02:11:15 +0000 | [diff] [blame] | 10047 | if (Context.BuiltinInfo.isScanfLike(BuiltinID, FormatIdx, |
| 10048 | HasVAListArg)) { |
| Aaron Ballman | 9ead124 | 2013-12-19 02:39:40 +0000 | [diff] [blame] | 10049 | if (!FD->hasAttr<FormatAttr>()) |
| Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 10050 | FD->addAttr(FormatAttr::CreateImplicit(Context, |
| Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 10051 | &Context.Idents.get("scanf"), |
| 10052 | FormatIdx+1, |
| Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 10053 | HasVAListArg ? 0 : FormatIdx+2, |
| 10054 | FD->getLocation())); |
| Ted Kremenek | 5932c35 | 2010-07-16 02:11:15 +0000 | [diff] [blame] | 10055 | } |
| Daniel Dunbar | 8eb018a | 2009-02-16 22:43:43 +0000 | [diff] [blame] | 10056 | |
| 10057 | // Mark const if we don't care about errno and that is the only |
| 10058 | // thing preventing the function from being const. This allows |
| 10059 | // IRgen to use LLVM intrinsics for such functions. |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10060 | if (!getLangOpts().MathErrno && |
| Daniel Dunbar | 8eb018a | 2009-02-16 22:43:43 +0000 | [diff] [blame] | 10061 | Context.BuiltinInfo.isConstWithoutErrno(BuiltinID)) { |
| Aaron Ballman | 9ead124 | 2013-12-19 02:39:40 +0000 | [diff] [blame] | 10062 | if (!FD->hasAttr<ConstAttr>()) |
| Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 10063 | FD->addAttr(ConstAttr::CreateImplicit(Context, FD->getLocation())); |
| Daniel Dunbar | 8eb018a | 2009-02-16 22:43:43 +0000 | [diff] [blame] | 10064 | } |
| Mike Stump | ca6c875 | 2009-07-27 19:14:18 +0000 | [diff] [blame] | 10065 | |
| Rafael Espindola | 2d21ab0 | 2011-10-12 19:51:18 +0000 | [diff] [blame] | 10066 | if (Context.BuiltinInfo.isReturnsTwice(BuiltinID) && |
| Aaron Ballman | 9ead124 | 2013-12-19 02:39:40 +0000 | [diff] [blame] | 10067 | !FD->hasAttr<ReturnsTwiceAttr>()) |
| Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 10068 | FD->addAttr(ReturnsTwiceAttr::CreateImplicit(Context, |
| 10069 | FD->getLocation())); |
| Aaron Ballman | 9ead124 | 2013-12-19 02:39:40 +0000 | [diff] [blame] | 10070 | if (Context.BuiltinInfo.isNoThrow(BuiltinID) && !FD->hasAttr<NoThrowAttr>()) |
| Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 10071 | FD->addAttr(NoThrowAttr::CreateImplicit(Context, FD->getLocation())); |
| Aaron Ballman | 9ead124 | 2013-12-19 02:39:40 +0000 | [diff] [blame] | 10072 | if (Context.BuiltinInfo.isConst(BuiltinID) && !FD->hasAttr<ConstAttr>()) |
| Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 10073 | FD->addAttr(ConstAttr::CreateImplicit(Context, FD->getLocation())); |
| Douglas Gregor | e711f70 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 10074 | } |
| 10075 | |
| 10076 | IdentifierInfo *Name = FD->getIdentifier(); |
| 10077 | if (!Name) |
| 10078 | return; |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10079 | if ((!getLangOpts().CPlusPlus && |
| Douglas Gregor | e711f70 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 10080 | FD->getDeclContext()->isTranslationUnit()) || |
| 10081 | (isa<LinkageSpecDecl>(FD->getDeclContext()) && |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10082 | cast<LinkageSpecDecl>(FD->getDeclContext())->getLanguage() == |
| Douglas Gregor | e711f70 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 10083 | LinkageSpecDecl::lang_c)) { |
| 10084 | // Okay: this could be a libc/libm/Objective-C function we know |
| 10085 | // about. |
| 10086 | } else |
| 10087 | return; |
| 10088 | |
| Jean-Daniel Dupas | 78536ae | 2012-01-24 22:32:46 +0000 | [diff] [blame] | 10089 | if (Name->isStr("asprintf") || Name->isStr("vasprintf")) { |
| Mike Stump | 82a9e44 | 2009-07-28 00:07:08 +0000 | [diff] [blame] | 10090 | // FIXME: asprintf and vasprintf aren't C99 functions. Should they be |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10091 | // target-specific builtins, perhaps? |
| Aaron Ballman | 9ead124 | 2013-12-19 02:39:40 +0000 | [diff] [blame] | 10092 | if (!FD->hasAttr<FormatAttr>()) |
| Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 10093 | FD->addAttr(FormatAttr::CreateImplicit(Context, |
| Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 10094 | &Context.Idents.get("printf"), 2, |
| Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 10095 | Name->isStr("vasprintf") ? 0 : 3, |
| 10096 | FD->getLocation())); |
| Mike Stump | a4de80b | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 10097 | } |
| Jordan Rose | 742c607 | 2012-08-08 21:17:31 +0000 | [diff] [blame] | 10098 | |
| 10099 | if (Name->isStr("__CFStringMakeConstantString")) { |
| 10100 | // We already have a __builtin___CFStringMakeConstantString, |
| 10101 | // but builds that use -fno-constant-cfstrings don't go through that. |
| Aaron Ballman | 9ead124 | 2013-12-19 02:39:40 +0000 | [diff] [blame] | 10102 | if (!FD->hasAttr<FormatArgAttr>()) |
| Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 10103 | FD->addAttr(FormatArgAttr::CreateImplicit(Context, 1, |
| 10104 | FD->getLocation())); |
| Jordan Rose | 742c607 | 2012-08-08 21:17:31 +0000 | [diff] [blame] | 10105 | } |
| Douglas Gregor | e711f70 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 10106 | } |
| Chris Lattner | 302b4be | 2006-11-19 02:31:38 +0000 | [diff] [blame] | 10107 | |
| John McCall | 703a3f8 | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 10108 | TypedefDecl *Sema::ParseTypedefDecl(Scope *S, Declarator &D, QualType T, |
| John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 10109 | TypeSourceInfo *TInfo) { |
| Chris Lattner | 776fac8 | 2007-06-09 00:53:06 +0000 | [diff] [blame] | 10110 | assert(D.getIdentifier() && "Wrong callback for declspec without declarator"); |
| Steve Naroff | f93b672 | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 10111 | assert(!T.isNull() && "GetTypeForDeclarator() returned null type"); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10112 | |
| John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 10113 | if (!TInfo) { |
| John McCall | 703a3f8 | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 10114 | assert(D.isInvalidType() && "no declarator info for valid type"); |
| John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 10115 | TInfo = Context.getTrivialTypeSourceInfo(T); |
| John McCall | 703a3f8 | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 10116 | } |
| 10117 | |
| Chris Lattner | 18b1962 | 2007-01-22 07:39:13 +0000 | [diff] [blame] | 10118 | // Scope manipulation handled by caller. |
| Chris Lattner | c5ffed4 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 10119 | TypedefDecl *NewTD = TypedefDecl::Create(Context, CurContext, |
| Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 10120 | D.getLocStart(), |
| Chris Lattner | c5ffed4 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 10121 | D.getIdentifierLoc(), |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10122 | D.getIdentifier(), |
| John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 10123 | TInfo); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10124 | |
| John McCall | 04fcd0d | 2011-02-01 08:20:08 +0000 | [diff] [blame] | 10125 | // Bail out immediately if we have an invalid declaration. |
| 10126 | if (D.isInvalidType()) { |
| 10127 | NewTD->setInvalidDecl(); |
| 10128 | return NewTD; |
| Anders Carlsson | 0275115 | 2009-03-10 17:07:44 +0000 | [diff] [blame] | 10129 | } |
| Fariborz Jahanian | db3d855 | 2013-11-19 00:09:48 +0000 | [diff] [blame] | 10130 | |
| Douglas Gregor | 4186681 | 2011-09-12 18:37:38 +0000 | [diff] [blame] | 10131 | if (D.getDeclSpec().isModulePrivateSpecified()) { |
| 10132 | if (CurContext->isFunctionOrMethod()) |
| 10133 | Diag(NewTD->getLocation(), diag::err_module_private_local) |
| 10134 | << 2 << NewTD->getDeclName() |
| 10135 | << SourceRange(D.getDeclSpec().getModulePrivateSpecLoc()) |
| 10136 | << FixItHint::CreateRemoval(D.getDeclSpec().getModulePrivateSpecLoc()); |
| 10137 | else |
| 10138 | NewTD->setModulePrivate(); |
| 10139 | } |
| Douglas Gregor | 26701a4 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 10140 | |
| John McCall | 04fcd0d | 2011-02-01 08:20:08 +0000 | [diff] [blame] | 10141 | // C++ [dcl.typedef]p8: |
| 10142 | // If the typedef declaration defines an unnamed class (or |
| 10143 | // enum), the first typedef-name declared by the declaration |
| 10144 | // to be that class type (or enum type) is used to denote the |
| 10145 | // class type (or enum type) for linkage purposes only. |
| 10146 | // We need to check whether the type was declared in the declaration. |
| 10147 | switch (D.getDeclSpec().getTypeSpecType()) { |
| 10148 | case TST_enum: |
| 10149 | case TST_struct: |
| Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 10150 | case TST_interface: |
| John McCall | 04fcd0d | 2011-02-01 08:20:08 +0000 | [diff] [blame] | 10151 | case TST_union: |
| 10152 | case TST_class: { |
| 10153 | TagDecl *tagFromDeclSpec = cast<TagDecl>(D.getDeclSpec().getRepAsDecl()); |
| 10154 | |
| 10155 | // Do nothing if the tag is not anonymous or already has an |
| 10156 | // associated typedef (from an earlier typedef in this decl group). |
| 10157 | if (tagFromDeclSpec->getIdentifier()) break; |
| Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 10158 | if (tagFromDeclSpec->getTypedefNameForAnonDecl()) break; |
| John McCall | 04fcd0d | 2011-02-01 08:20:08 +0000 | [diff] [blame] | 10159 | |
| 10160 | // A well-formed anonymous tag must always be a TUK_Definition. |
| 10161 | assert(tagFromDeclSpec->isThisDeclarationADefinition()); |
| 10162 | |
| 10163 | // The type must match the tag exactly; no qualifiers allowed. |
| 10164 | if (!Context.hasSameType(T, Context.getTagDeclType(tagFromDeclSpec))) |
| 10165 | break; |
| 10166 | |
| John McCall | 2575d88 | 2014-01-30 01:12:53 +0000 | [diff] [blame] | 10167 | // If we've already computed linkage for the anonymous tag, then |
| 10168 | // adding a typedef name for the anonymous decl can change that |
| 10169 | // linkage, which might be a serious problem. Diagnose this as |
| 10170 | // unsupported and ignore the typedef name. TODO: we should |
| 10171 | // pursue this as a language defect and establish a formal rule |
| 10172 | // for how to handle it. |
| 10173 | if (tagFromDeclSpec->hasLinkageBeenComputed()) { |
| 10174 | Diag(D.getIdentifierLoc(), diag::err_typedef_changes_linkage); |
| 10175 | |
| 10176 | SourceLocation tagLoc = D.getDeclSpec().getTypeSpecTypeLoc(); |
| 10177 | tagLoc = Lexer::getLocForEndOfToken(tagLoc, 0, getSourceManager(), |
| 10178 | getLangOpts()); |
| 10179 | |
| 10180 | llvm::SmallString<40> textToInsert; |
| 10181 | textToInsert += ' '; |
| 10182 | textToInsert += D.getIdentifier()->getName(); |
| 10183 | Diag(tagLoc, diag::note_typedef_changes_linkage) |
| 10184 | << FixItHint::CreateInsertion(tagLoc, textToInsert); |
| 10185 | break; |
| 10186 | } |
| 10187 | |
| John McCall | 04fcd0d | 2011-02-01 08:20:08 +0000 | [diff] [blame] | 10188 | // Otherwise, set this is the anon-decl typedef for the tag. |
| Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 10189 | tagFromDeclSpec->setTypedefNameForAnonDecl(NewTD); |
| John McCall | 04fcd0d | 2011-02-01 08:20:08 +0000 | [diff] [blame] | 10190 | break; |
| 10191 | } |
| 10192 | |
| 10193 | default: |
| 10194 | break; |
| 10195 | } |
| 10196 | |
| Steve Naroff | f93b672 | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 10197 | return NewTD; |
| Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 10198 | } |
| 10199 | |
| Douglas Gregor | d9034f0 | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 10200 | |
| Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 10201 | /// \brief Check that this is a valid underlying type for an enum declaration. |
| 10202 | bool Sema::CheckEnumUnderlyingType(TypeSourceInfo *TI) { |
| 10203 | SourceLocation UnderlyingLoc = TI->getTypeLoc().getBeginLoc(); |
| 10204 | QualType T = TI->getType(); |
| 10205 | |
| Eli Friedman | 52f32b9 | 2012-12-18 02:37:32 +0000 | [diff] [blame] | 10206 | if (T->isDependentType()) |
| Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 10207 | return false; |
| 10208 | |
| Eli Friedman | 52f32b9 | 2012-12-18 02:37:32 +0000 | [diff] [blame] | 10209 | if (const BuiltinType *BT = T->getAs<BuiltinType>()) |
| 10210 | if (BT->isInteger()) |
| 10211 | return false; |
| 10212 | |
| Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 10213 | Diag(UnderlyingLoc, diag::err_enum_invalid_underlying) << T; |
| 10214 | return true; |
| 10215 | } |
| 10216 | |
| 10217 | /// Check whether this is a valid redeclaration of a previous enumeration. |
| 10218 | /// \return true if the redeclaration was invalid. |
| 10219 | bool Sema::CheckEnumRedeclaration(SourceLocation EnumLoc, bool IsScoped, |
| 10220 | QualType EnumUnderlyingTy, |
| 10221 | const EnumDecl *Prev) { |
| 10222 | bool IsFixed = !EnumUnderlyingTy.isNull(); |
| 10223 | |
| 10224 | if (IsScoped != Prev->isScoped()) { |
| 10225 | Diag(EnumLoc, diag::err_enum_redeclare_scoped_mismatch) |
| 10226 | << Prev->isScoped(); |
| Alp Toker | 8c44db5 | 2014-01-06 11:31:06 +0000 | [diff] [blame] | 10227 | Diag(Prev->getLocation(), diag::note_previous_declaration); |
| Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 10228 | return true; |
| 10229 | } |
| 10230 | |
| 10231 | if (IsFixed && Prev->isFixed()) { |
| Richard Smith | 258a744 | 2012-03-26 04:08:46 +0000 | [diff] [blame] | 10232 | if (!EnumUnderlyingTy->isDependentType() && |
| 10233 | !Prev->getIntegerType()->isDependentType() && |
| 10234 | !Context.hasSameUnqualifiedType(EnumUnderlyingTy, |
| Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 10235 | Prev->getIntegerType())) { |
| Alp Toker | b9fa512 | 2014-01-06 11:31:18 +0000 | [diff] [blame] | 10236 | // TODO: Highlight the underlying type of the redeclaration. |
| Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 10237 | Diag(EnumLoc, diag::err_enum_redeclare_type_mismatch) |
| 10238 | << EnumUnderlyingTy << Prev->getIntegerType(); |
| Alp Toker | b9fa512 | 2014-01-06 11:31:18 +0000 | [diff] [blame] | 10239 | Diag(Prev->getLocation(), diag::note_previous_declaration) |
| 10240 | << Prev->getIntegerTypeRange(); |
| Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 10241 | return true; |
| 10242 | } |
| 10243 | } else if (IsFixed != Prev->isFixed()) { |
| 10244 | Diag(EnumLoc, diag::err_enum_redeclare_fixed_mismatch) |
| 10245 | << Prev->isFixed(); |
| Alp Toker | 8c44db5 | 2014-01-06 11:31:06 +0000 | [diff] [blame] | 10246 | Diag(Prev->getLocation(), diag::note_previous_declaration); |
| Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 10247 | return true; |
| 10248 | } |
| 10249 | |
| 10250 | return false; |
| 10251 | } |
| 10252 | |
| Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 10253 | /// \brief Get diagnostic %select index for tag kind for |
| 10254 | /// redeclaration diagnostic message. |
| 10255 | /// WARNING: Indexes apply to particular diagnostics only! |
| 10256 | /// |
| 10257 | /// \returns diagnostic %select index. |
| Joao Matos | a5c42e9 | 2012-09-01 00:13:24 +0000 | [diff] [blame] | 10258 | static unsigned getRedeclDiagFromTagKind(TagTypeKind Tag) { |
| Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 10259 | switch (Tag) { |
| Joao Matos | a5c42e9 | 2012-09-01 00:13:24 +0000 | [diff] [blame] | 10260 | case TTK_Struct: return 0; |
| 10261 | case TTK_Interface: return 1; |
| 10262 | case TTK_Class: return 2; |
| 10263 | default: llvm_unreachable("Invalid tag kind for redecl diagnostic!"); |
| Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 10264 | } |
| Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 10265 | } |
| 10266 | |
| 10267 | /// \brief Determine if tag kind is a class-key compatible with |
| 10268 | /// class for redeclaration (class, struct, or __interface). |
| 10269 | /// |
| Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 10270 | /// \returns true iff the tag kind is compatible. |
| Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 10271 | static bool isClassCompatTagKind(TagTypeKind Tag) |
| 10272 | { |
| 10273 | return Tag == TTK_Struct || Tag == TTK_Class || Tag == TTK_Interface; |
| 10274 | } |
| 10275 | |
| Douglas Gregor | d9034f0 | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 10276 | /// \brief Determine whether a tag with a given kind is acceptable |
| 10277 | /// as a redeclaration of the given tag declaration. |
| 10278 | /// |
| 10279 | /// \returns true if the new tag kind is acceptable, false otherwise. |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10280 | bool Sema::isAcceptableTagRedeclaration(const TagDecl *Previous, |
| Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 10281 | TagTypeKind NewTag, bool isDefinition, |
| Douglas Gregor | d9034f0 | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 10282 | SourceLocation NewTagLoc, |
| 10283 | const IdentifierInfo &Name) { |
| 10284 | // C++ [dcl.type.elab]p3: |
| 10285 | // The class-key or enum keyword present in the |
| 10286 | // elaborated-type-specifier shall agree in kind with the |
| Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 10287 | // declaration to which the name in the elaborated-type-specifier |
| Douglas Gregor | d9034f0 | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 10288 | // refers. This rule also applies to the form of |
| 10289 | // elaborated-type-specifier that declares a class-name or |
| 10290 | // friend class since it can be construed as referring to the |
| 10291 | // definition of the class. Thus, in any |
| 10292 | // elaborated-type-specifier, the enum keyword shall be used to |
| Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 10293 | // refer to an enumeration (7.2), the union class-key shall be |
| Douglas Gregor | d9034f0 | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 10294 | // used to refer to a union (clause 9), and either the class or |
| 10295 | // struct class-key shall be used to refer to a class (clause 9) |
| 10296 | // declared using the class or struct class-key. |
| Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 10297 | TagTypeKind OldTag = Previous->getTagKind(); |
| Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 10298 | if (!isDefinition || !isClassCompatTagKind(NewTag)) |
| Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 10299 | if (OldTag == NewTag) |
| 10300 | return true; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10301 | |
| Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 10302 | if (isClassCompatTagKind(OldTag) && isClassCompatTagKind(NewTag)) { |
| Douglas Gregor | d9034f0 | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 10303 | // Warn about the struct/class tag mismatch. |
| 10304 | bool isTemplate = false; |
| 10305 | if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Previous)) |
| 10306 | isTemplate = Record->getDescribedClassTemplate(); |
| 10307 | |
| Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 10308 | if (!ActiveTemplateInstantiations.empty()) { |
| 10309 | // In a template instantiation, do not offer fix-its for tag mismatches |
| 10310 | // since they usually mess up the template instead of fixing the problem. |
| 10311 | Diag(NewTagLoc, diag::warn_struct_class_tag_mismatch) |
| Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 10312 | << getRedeclDiagFromTagKind(NewTag) << isTemplate << &Name |
| 10313 | << getRedeclDiagFromTagKind(OldTag); |
| Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 10314 | return true; |
| 10315 | } |
| 10316 | |
| 10317 | if (isDefinition) { |
| 10318 | // On definitions, check previous tags and issue a fix-it for each |
| 10319 | // one that doesn't match the current tag. |
| 10320 | if (Previous->getDefinition()) { |
| 10321 | // Don't suggest fix-its for redefinitions. |
| 10322 | return true; |
| 10323 | } |
| 10324 | |
| 10325 | bool previousMismatch = false; |
| Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 10326 | for (auto I : Previous->redecls()) { |
| Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 10327 | if (I->getTagKind() != NewTag) { |
| 10328 | if (!previousMismatch) { |
| 10329 | previousMismatch = true; |
| 10330 | Diag(NewTagLoc, diag::warn_struct_class_previous_tag_mismatch) |
| Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 10331 | << getRedeclDiagFromTagKind(NewTag) << isTemplate << &Name |
| 10332 | << getRedeclDiagFromTagKind(I->getTagKind()); |
| Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 10333 | } |
| 10334 | Diag(I->getInnerLocStart(), diag::note_struct_class_suggestion) |
| Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 10335 | << getRedeclDiagFromTagKind(NewTag) |
| Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 10336 | << FixItHint::CreateReplacement(I->getInnerLocStart(), |
| Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 10337 | TypeWithKeyword::getTagTypeKindName(NewTag)); |
| Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 10338 | } |
| 10339 | } |
| 10340 | return true; |
| 10341 | } |
| 10342 | |
| 10343 | // Check for a previous definition. If current tag and definition |
| 10344 | // are same type, do nothing. If no definition, but disagree with |
| 10345 | // with previous tag type, give a warning, but no fix-it. |
| 10346 | const TagDecl *Redecl = Previous->getDefinition() ? |
| 10347 | Previous->getDefinition() : Previous; |
| 10348 | if (Redecl->getTagKind() == NewTag) { |
| 10349 | return true; |
| 10350 | } |
| 10351 | |
| Douglas Gregor | d9034f0 | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 10352 | Diag(NewTagLoc, diag::warn_struct_class_tag_mismatch) |
| Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 10353 | << getRedeclDiagFromTagKind(NewTag) << isTemplate << &Name |
| 10354 | << getRedeclDiagFromTagKind(OldTag); |
| Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 10355 | Diag(Redecl->getLocation(), diag::note_previous_use); |
| 10356 | |
| Alp Toker | f6a24ce | 2013-12-05 16:25:25 +0000 | [diff] [blame] | 10357 | // If there is a previous definition, suggest a fix-it. |
| Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 10358 | if (Previous->getDefinition()) { |
| 10359 | Diag(NewTagLoc, diag::note_struct_class_suggestion) |
| Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 10360 | << getRedeclDiagFromTagKind(Redecl->getTagKind()) |
| Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 10361 | << FixItHint::CreateReplacement(SourceRange(NewTagLoc), |
| Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 10362 | TypeWithKeyword::getTagTypeKindName(Redecl->getTagKind())); |
| Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 10363 | } |
| 10364 | |
| Douglas Gregor | d9034f0 | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 10365 | return true; |
| 10366 | } |
| 10367 | return false; |
| 10368 | } |
| 10369 | |
| Steve Naroff | 30d242c | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 10370 | /// ActOnTag - This is invoked when we see 'struct foo' or 'struct {'. In the |
| Chris Lattner | 1300fb9 | 2007-01-23 23:42:53 +0000 | [diff] [blame] | 10371 | /// former case, Name will be non-null. In the later case, Name will be null. |
| John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 10372 | /// TagSpec indicates what kind of tag this is. TUK indicates whether this is a |
| Chris Lattner | 1300fb9 | 2007-01-23 23:42:53 +0000 | [diff] [blame] | 10373 | /// reference/declaration/definition of a tag. |
| Richard Smith | 649c7b06 | 2014-01-08 00:56:48 +0000 | [diff] [blame] | 10374 | /// |
| 10375 | /// IsTypeSpecifier is true if this is a type-specifier (or |
| 10376 | /// trailing-type-specifier) other than one in an alias-declaration. |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10377 | Decl *Sema::ActOnTag(Scope *S, unsigned TagSpec, TagUseKind TUK, |
| Douglas Gregor | 009f699 | 2010-09-16 23:58:57 +0000 | [diff] [blame] | 10378 | SourceLocation KWLoc, CXXScopeSpec &SS, |
| 10379 | IdentifierInfo *Name, SourceLocation NameLoc, |
| 10380 | AttributeList *Attr, AccessSpecifier AS, |
| Douglas Gregor | 2820e69 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 10381 | SourceLocation ModulePrivateLoc, |
| Douglas Gregor | 009f699 | 2010-09-16 23:58:57 +0000 | [diff] [blame] | 10382 | MultiTemplateParamsArg TemplateParameterLists, |
| Abramo Bagnara | 0e05e24 | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 10383 | bool &OwnedDecl, bool &IsDependent, |
| Richard Smith | 0f8ee22 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 10384 | SourceLocation ScopedEnumKWLoc, |
| 10385 | bool ScopedEnumUsesClassTag, |
| Richard Smith | 649c7b06 | 2014-01-08 00:56:48 +0000 | [diff] [blame] | 10386 | TypeResult UnderlyingType, |
| 10387 | bool IsTypeSpecifier) { |
| Douglas Gregor | c811d8f | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 10388 | // If this is not a definition, it must have a name. |
| Douglas Gregor | b7d17dd | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 10389 | IdentifierInfo *OrigName = Name; |
| John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 10390 | assert((Name != 0 || TUK == TUK_Definition) && |
| Chris Lattner | 7b9ace6 | 2007-01-23 20:11:08 +0000 | [diff] [blame] | 10391 | "Nameless record must be a definition!"); |
| John McCall | ace48cd | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 10392 | assert(TemplateParameterLists.size() == 0 || TUK != TUK_Reference); |
| Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 10393 | |
| Douglas Gregor | d6ab874 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 10394 | OwnedDecl = false; |
| Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 10395 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| Richard Smith | 0f8ee22 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 10396 | bool ScopedEnum = ScopedEnumKWLoc.isValid(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10397 | |
| Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 10398 | // FIXME: Check explicit specializations more carefully. |
| 10399 | bool isExplicitSpecialization = false; |
| Douglas Gregor | 5f0e252 | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 10400 | bool Invalid = false; |
| John McCall | ace48cd | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 10401 | |
| 10402 | // We only need to do this matching if we have template parameters |
| 10403 | // or a scope specifier, which also conveniently avoids this work |
| 10404 | // for non-C++ cases. |
| Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 10405 | if (TemplateParameterLists.size() > 0 || |
| John McCall | ace48cd | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 10406 | (SS.isNotEmpty() && TUK != TUK_Reference)) { |
| Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 10407 | if (TemplateParameterList *TemplateParams = |
| 10408 | MatchTemplateParametersToScopeSpecifier( |
| 10409 | KWLoc, NameLoc, SS, TemplateParameterLists, TUK == TUK_Friend, |
| 10410 | isExplicitSpecialization, Invalid)) { |
| Richard Smith | 1d4b2e1 | 2013-04-01 21:43:41 +0000 | [diff] [blame] | 10411 | if (Kind == TTK_Enum) { |
| 10412 | Diag(KWLoc, diag::err_enum_template); |
| 10413 | return 0; |
| 10414 | } |
| 10415 | |
| Douglas Gregor | 3dad842 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 10416 | if (TemplateParams->size() > 0) { |
| Douglas Gregor | e93e46c | 2009-07-22 23:48:44 +0000 | [diff] [blame] | 10417 | // This is a declaration or definition of a class template (which may |
| 10418 | // be a member of another template). |
| Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 10419 | |
| Douglas Gregor | 5f0e252 | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 10420 | if (Invalid) |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10421 | return 0; |
| Abramo Bagnara | 0adf29a | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 10422 | |
| Douglas Gregor | e93e46c | 2009-07-22 23:48:44 +0000 | [diff] [blame] | 10423 | OwnedDecl = false; |
| John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 10424 | DeclResult Result = CheckClassTemplate(S, TagSpec, TUK, KWLoc, |
| Douglas Gregor | e93e46c | 2009-07-22 23:48:44 +0000 | [diff] [blame] | 10425 | SS, Name, NameLoc, Attr, |
| Abramo Bagnara | 0adf29a | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 10426 | TemplateParams, AS, |
| Douglas Gregor | 2820e69 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 10427 | ModulePrivateLoc, |
| Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 10428 | TemplateParameterLists.size()-1, |
| Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 10429 | TemplateParameterLists.data()); |
| Douglas Gregor | e93e46c | 2009-07-22 23:48:44 +0000 | [diff] [blame] | 10430 | return Result.get(); |
| 10431 | } else { |
| Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 10432 | // The "template<>" header is extraneous. |
| 10433 | Diag(TemplateParams->getTemplateLoc(), diag::err_template_tag_noparams) |
| Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 10434 | << TypeWithKeyword::getTagTypeKindName(Kind) << Name; |
| Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 10435 | isExplicitSpecialization = true; |
| Douglas Gregor | e93e46c | 2009-07-22 23:48:44 +0000 | [diff] [blame] | 10436 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10437 | } |
| 10438 | } |
| 10439 | |
| Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 10440 | // Figure out the underlying type if this a enum declaration. We need to do |
| 10441 | // this early, because it's needed to detect if this is an incompatible |
| 10442 | // redeclaration. |
| 10443 | llvm::PointerUnion<const Type*, TypeSourceInfo*> EnumUnderlying; |
| 10444 | |
| 10445 | if (Kind == TTK_Enum) { |
| 10446 | if (UnderlyingType.isInvalid() || (!UnderlyingType.get() && ScopedEnum)) |
| 10447 | // No underlying type explicitly specified, or we failed to parse the |
| 10448 | // type, default to int. |
| 10449 | EnumUnderlying = Context.IntTy.getTypePtr(); |
| 10450 | else if (UnderlyingType.get()) { |
| 10451 | // C++0x 7.2p2: The type-specifier-seq of an enum-base shall name an |
| 10452 | // integral type; any cv-qualification is ignored. |
| 10453 | TypeSourceInfo *TI = 0; |
| Richard Smith | eece8c3 | 2012-03-15 00:22:18 +0000 | [diff] [blame] | 10454 | GetTypeFromParser(UnderlyingType.get(), &TI); |
| Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 10455 | EnumUnderlying = TI; |
| 10456 | |
| Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 10457 | if (CheckEnumUnderlyingType(TI)) |
| Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 10458 | // Recover by falling back to int. |
| 10459 | EnumUnderlying = Context.IntTy.getTypePtr(); |
| Douglas Gregor | 2b988fd | 2010-12-16 00:24:44 +0000 | [diff] [blame] | 10460 | |
| Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 10461 | if (DiagnoseUnexpandedParameterPack(TI->getTypeLoc().getBeginLoc(), TI, |
| Douglas Gregor | 2b988fd | 2010-12-16 00:24:44 +0000 | [diff] [blame] | 10462 | UPPC_FixedUnderlyingType)) |
| 10463 | EnumUnderlying = Context.IntTy.getTypePtr(); |
| 10464 | |
| Alp Toker | bfa3934 | 2014-01-14 12:51:41 +0000 | [diff] [blame] | 10465 | } else if (getLangOpts().MSVCCompat) |
| Francois Pichet | a310806 | 2010-10-18 15:01:13 +0000 | [diff] [blame] | 10466 | // Microsoft enums are always of int type. |
| 10467 | EnumUnderlying = Context.IntTy.getTypePtr(); |
| Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 10468 | } |
| 10469 | |
| Douglas Gregor | c6f58fe | 2009-01-12 22:49:06 +0000 | [diff] [blame] | 10470 | DeclContext *SearchDC = CurContext; |
| Argyrios Kyrtzidis | 8ad00b2 | 2008-11-09 22:53:32 +0000 | [diff] [blame] | 10471 | DeclContext *DC = CurContext; |
| Douglas Gregor | 87f5406 | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 10472 | bool isStdBadAlloc = false; |
| Douglas Gregor | dee1be8 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 10473 | |
| Chandler Carruth | a419dbb | 2010-03-01 21:17:36 +0000 | [diff] [blame] | 10474 | RedeclarationKind Redecl = ForRedeclaration; |
| 10475 | if (TUK == TUK_Friend || TUK == TUK_Reference) |
| 10476 | Redecl = NotForRedeclaration; |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 10477 | |
| 10478 | LookupResult Previous(*this, Name, NameLoc, LookupTagName, Redecl); |
| Douglas Gregor | f83b4ea | 2013-06-27 20:42:30 +0000 | [diff] [blame] | 10479 | bool FriendSawTagOutsideEnclosingNamespace = false; |
| Argyrios Kyrtzidis | e02eb2b | 2008-11-09 22:09:58 +0000 | [diff] [blame] | 10480 | if (Name && SS.isNotEmpty()) { |
| 10481 | // We have a nested-name tag ('struct foo::bar'). |
| 10482 | |
| 10483 | // Check for invalid 'foo::'. |
| Argyrios Kyrtzidis | 8ad00b2 | 2008-11-09 22:53:32 +0000 | [diff] [blame] | 10484 | if (SS.isInvalid()) { |
| Argyrios Kyrtzidis | e02eb2b | 2008-11-09 22:09:58 +0000 | [diff] [blame] | 10485 | Name = 0; |
| 10486 | goto CreateNewDecl; |
| 10487 | } |
| 10488 | |
| John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 10489 | // If this is a friend or a reference to a class in a dependent |
| 10490 | // context, don't try to make a decl for it. |
| 10491 | if (TUK == TUK_Friend || TUK == TUK_Reference) { |
| 10492 | DC = computeDeclContext(SS, false); |
| 10493 | if (!DC) { |
| 10494 | IsDependent = true; |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10495 | return 0; |
| John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 10496 | } |
| John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 10497 | } else { |
| 10498 | DC = computeDeclContext(SS, true); |
| 10499 | if (!DC) { |
| 10500 | Diag(SS.getRange().getBegin(), diag::err_dependent_nested_name_spec) |
| 10501 | << SS.getRange(); |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10502 | return 0; |
| John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 10503 | } |
| John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 10504 | } |
| 10505 | |
| John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 10506 | if (RequireCompleteDeclContext(SS, DC)) |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10507 | return 0; |
| Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 10508 | |
| Douglas Gregor | 8761da5 | 2009-02-03 00:34:39 +0000 | [diff] [blame] | 10509 | SearchDC = DC; |
| Argyrios Kyrtzidis | 8ad00b2 | 2008-11-09 22:53:32 +0000 | [diff] [blame] | 10510 | // Look-up name inside 'foo::'. |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 10511 | LookupQualifiedName(Previous, DC); |
| John McCall | 6538c93 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 10512 | |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 10513 | if (Previous.isAmbiguous()) |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10514 | return 0; |
| John McCall | 6538c93 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 10515 | |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 10516 | if (Previous.empty()) { |
| Douglas Gregor | d2e6a45 | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 10517 | // Name lookup did not find anything. However, if the |
| 10518 | // nested-name-specifier refers to the current instantiation, |
| 10519 | // and that current instantiation has any dependent base |
| 10520 | // classes, we might find something at instantiation time: treat |
| 10521 | // this as a dependent elaborated-type-specifier. |
| John McCall | ace48cd | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 10522 | // But this only makes any sense for reference-like lookups. |
| 10523 | if (Previous.wasNotFoundInCurrentInstantiation() && |
| 10524 | (TUK == TUK_Reference || TUK == TUK_Friend)) { |
| Douglas Gregor | d2e6a45 | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 10525 | IsDependent = true; |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10526 | return 0; |
| Douglas Gregor | d2e6a45 | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 10527 | } |
| 10528 | |
| 10529 | // A tag 'foo::bar' must already exist. |
| Douglas Gregor | f5af358 | 2010-03-31 23:17:41 +0000 | [diff] [blame] | 10530 | Diag(NameLoc, diag::err_not_tag_in_scope) |
| 10531 | << Kind << Name << DC << SS.getRange(); |
| Argyrios Kyrtzidis | e02eb2b | 2008-11-09 22:09:58 +0000 | [diff] [blame] | 10532 | Name = 0; |
| Douglas Gregor | b8006faf | 2009-05-27 17:30:49 +0000 | [diff] [blame] | 10533 | Invalid = true; |
| Argyrios Kyrtzidis | e02eb2b | 2008-11-09 22:09:58 +0000 | [diff] [blame] | 10534 | goto CreateNewDecl; |
| 10535 | } |
| Chris Lattner | d977351 | 2009-01-21 02:38:50 +0000 | [diff] [blame] | 10536 | } else if (Name) { |
| Argyrios Kyrtzidis | e02eb2b | 2008-11-09 22:09:58 +0000 | [diff] [blame] | 10537 | // If this is a named struct, check to see if there was a previous forward |
| 10538 | // declaration or definition. |
| Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 10539 | // FIXME: We're looking into outer scopes here, even when we |
| 10540 | // shouldn't be. Doing so can result in ambiguities that we |
| 10541 | // shouldn't be diagnosing. |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 10542 | LookupName(Previous, S); |
| 10543 | |
| John McCall | 3c581bf | 2013-03-20 01:53:00 +0000 | [diff] [blame] | 10544 | // When declaring or defining a tag, ignore ambiguities introduced |
| 10545 | // by types using'ed into this scope. |
| Douglas Gregor | 5d1d9e3 | 2011-05-09 21:46:33 +0000 | [diff] [blame] | 10546 | if (Previous.isAmbiguous() && |
| 10547 | (TUK == TUK_Definition || TUK == TUK_Declaration)) { |
| Douglas Gregor | ed8a29b | 2011-05-04 00:25:33 +0000 | [diff] [blame] | 10548 | LookupResult::Filter F = Previous.makeFilter(); |
| 10549 | while (F.hasNext()) { |
| 10550 | NamedDecl *ND = F.next(); |
| 10551 | if (ND->getDeclContext()->getRedeclContext() != SearchDC) |
| 10552 | F.erase(); |
| 10553 | } |
| 10554 | F.done(); |
| Douglas Gregor | ed8a29b | 2011-05-04 00:25:33 +0000 | [diff] [blame] | 10555 | } |
| John McCall | 3c581bf | 2013-03-20 01:53:00 +0000 | [diff] [blame] | 10556 | |
| 10557 | // C++11 [namespace.memdef]p3: |
| 10558 | // If the name in a friend declaration is neither qualified nor |
| 10559 | // a template-id and the declaration is a function or an |
| 10560 | // elaborated-type-specifier, the lookup to determine whether |
| 10561 | // the entity has been previously declared shall not consider |
| 10562 | // any scopes outside the innermost enclosing namespace. |
| 10563 | // |
| 10564 | // Does it matter that this should be by scope instead of by |
| 10565 | // semantic context? |
| 10566 | if (!Previous.empty() && TUK == TUK_Friend) { |
| 10567 | DeclContext *EnclosingNS = SearchDC->getEnclosingNamespaceContext(); |
| 10568 | LookupResult::Filter F = Previous.makeFilter(); |
| 10569 | while (F.hasNext()) { |
| 10570 | NamedDecl *ND = F.next(); |
| 10571 | DeclContext *DC = ND->getDeclContext()->getRedeclContext(); |
| Douglas Gregor | f83b4ea | 2013-06-27 20:42:30 +0000 | [diff] [blame] | 10572 | if (DC->isFileContext() && |
| 10573 | !EnclosingNS->Encloses(ND->getDeclContext())) { |
| John McCall | 3c581bf | 2013-03-20 01:53:00 +0000 | [diff] [blame] | 10574 | F.erase(); |
| Douglas Gregor | f83b4ea | 2013-06-27 20:42:30 +0000 | [diff] [blame] | 10575 | FriendSawTagOutsideEnclosingNamespace = true; |
| 10576 | } |
| John McCall | 3c581bf | 2013-03-20 01:53:00 +0000 | [diff] [blame] | 10577 | } |
| 10578 | F.done(); |
| 10579 | } |
| Douglas Gregor | ed8a29b | 2011-05-04 00:25:33 +0000 | [diff] [blame] | 10580 | |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 10581 | // Note: there used to be some attempt at recovery here. |
| 10582 | if (Previous.isAmbiguous()) |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10583 | return 0; |
| Douglas Gregor | 82ac25e | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 10584 | |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10585 | if (!getLangOpts().CPlusPlus && TUK != TUK_Reference) { |
| Douglas Gregor | 82ac25e | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 10586 | // FIXME: This makes sure that we ignore the contexts associated |
| 10587 | // with C structs, unions, and enums when looking for a matching |
| 10588 | // tag declaration or definition. See the similar lookup tweak |
| Douglas Gregor | ed8f288 | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 10589 | // in Sema::LookupName; is there a better way to deal with this? |
| Douglas Gregor | c6f58fe | 2009-01-12 22:49:06 +0000 | [diff] [blame] | 10590 | while (isa<RecordDecl>(SearchDC) || isa<EnumDecl>(SearchDC)) |
| 10591 | SearchDC = SearchDC->getParent(); |
| Douglas Gregor | 82ac25e | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 10592 | } |
| Douglas Gregor | 009f699 | 2010-09-16 23:58:57 +0000 | [diff] [blame] | 10593 | } else if (S->isFunctionPrototypeScope()) { |
| 10594 | // If this is an enum declaration in function prototype scope, set its |
| 10595 | // initial context to the translation unit. |
| Nick Lewycky | d9e1e57 | 2012-03-10 07:45:33 +0000 | [diff] [blame] | 10596 | // FIXME: [citation needed] |
| Douglas Gregor | 009f699 | 2010-09-16 23:58:57 +0000 | [diff] [blame] | 10597 | SearchDC = Context.getTranslationUnitDecl(); |
| Argyrios Kyrtzidis | e02eb2b | 2008-11-09 22:09:58 +0000 | [diff] [blame] | 10598 | } |
| 10599 | |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 10600 | if (Previous.isSingleResult() && |
| 10601 | Previous.getFoundDecl()->isTemplateParameter()) { |
| Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 10602 | // Maybe we will complain about the shadowed template parameter. |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 10603 | DiagnoseTemplateParameterShadow(NameLoc, Previous.getFoundDecl()); |
| Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 10604 | // Just pretend that we didn't see the previous declaration. |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 10605 | Previous.clear(); |
| Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 10606 | } |
| 10607 | |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10608 | if (getLangOpts().CPlusPlus && Name && DC && StdNamespace && |
| Argyrios Kyrtzidis | 2d68810 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 10609 | DC->Equals(getStdNamespace()) && Name->isStr("bad_alloc")) { |
| Douglas Gregor | 87f5406 | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 10610 | // This is a declaration of or a reference to "std::bad_alloc". |
| 10611 | isStdBadAlloc = true; |
| 10612 | |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 10613 | if (Previous.empty() && StdBadAlloc) { |
| Douglas Gregor | 87f5406 | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 10614 | // std::bad_alloc has been implicitly declared (but made invisible to |
| 10615 | // name lookup). Fill in this implicit declaration as the previous |
| 10616 | // declaration, so that the declarations get chained appropriately. |
| Argyrios Kyrtzidis | 2d68810 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 10617 | Previous.addDecl(getStdBadAlloc()); |
| Douglas Gregor | 87f5406 | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 10618 | } |
| 10619 | } |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 10620 | |
| John McCall | e9eaf8e | 2010-03-25 21:28:06 +0000 | [diff] [blame] | 10621 | // If we didn't find a previous declaration, and this is a reference |
| 10622 | // (or friend reference), move to the correct scope. In C++, we |
| 10623 | // also need to do a redeclaration lookup there, just in case |
| 10624 | // there's a shadow friend decl. |
| 10625 | if (Name && Previous.empty() && |
| 10626 | (TUK == TUK_Reference || TUK == TUK_Friend)) { |
| 10627 | if (Invalid) goto CreateNewDecl; |
| 10628 | assert(SS.isEmpty()); |
| 10629 | |
| 10630 | if (TUK == TUK_Reference) { |
| 10631 | // C++ [basic.scope.pdecl]p5: |
| 10632 | // -- for an elaborated-type-specifier of the form |
| 10633 | // |
| 10634 | // class-key identifier |
| 10635 | // |
| 10636 | // if the elaborated-type-specifier is used in the |
| 10637 | // decl-specifier-seq or parameter-declaration-clause of a |
| 10638 | // function defined in namespace scope, the identifier is |
| 10639 | // declared as a class-name in the namespace that contains |
| 10640 | // the declaration; otherwise, except as a friend |
| 10641 | // declaration, the identifier is declared in the smallest |
| 10642 | // non-class, non-function-prototype scope that contains the |
| 10643 | // declaration. |
| 10644 | // |
| 10645 | // C99 6.7.2.3p8 has a similar (but not identical!) provision for |
| 10646 | // C structs and unions. |
| 10647 | // |
| 10648 | // It is an error in C++ to declare (rather than define) an enum |
| 10649 | // type, including via an elaborated type specifier. We'll |
| 10650 | // diagnose that later; for now, declare the enum in the same |
| 10651 | // scope as we would have picked for any other tag type. |
| 10652 | // |
| 10653 | // GNU C also supports this behavior as part of its incomplete |
| 10654 | // enum types extension, while GNU C++ does not. |
| 10655 | // |
| 10656 | // Find the context where we'll be declaring the tag. |
| 10657 | // FIXME: We would like to maintain the current DeclContext as the |
| 10658 | // lexical context, |
| Nick Lewycky | f604212 | 2012-03-10 07:47:07 +0000 | [diff] [blame] | 10659 | while (!SearchDC->isFileContext() && !SearchDC->isFunctionOrMethod()) |
| John McCall | e9eaf8e | 2010-03-25 21:28:06 +0000 | [diff] [blame] | 10660 | SearchDC = SearchDC->getParent(); |
| 10661 | |
| 10662 | // Find the scope where we'll be declaring the tag. |
| 10663 | while (S->isClassScope() || |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10664 | (getLangOpts().CPlusPlus && |
| John McCall | e9eaf8e | 2010-03-25 21:28:06 +0000 | [diff] [blame] | 10665 | S->isFunctionPrototypeScope()) || |
| 10666 | ((S->getFlags() & Scope::DeclScope) == 0) || |
| Ted Kremenek | c37877d | 2013-10-08 17:08:03 +0000 | [diff] [blame] | 10667 | (S->getEntity() && S->getEntity()->isTransparentContext())) |
| John McCall | e9eaf8e | 2010-03-25 21:28:06 +0000 | [diff] [blame] | 10668 | S = S->getParent(); |
| 10669 | } else { |
| 10670 | assert(TUK == TUK_Friend); |
| 10671 | // C++ [namespace.memdef]p3: |
| 10672 | // If a friend declaration in a non-local class first declares a |
| 10673 | // class or function, the friend class or function is a member of |
| 10674 | // the innermost enclosing namespace. |
| 10675 | SearchDC = SearchDC->getEnclosingNamespaceContext(); |
| John McCall | e9eaf8e | 2010-03-25 21:28:06 +0000 | [diff] [blame] | 10676 | } |
| 10677 | |
| John McCall | e87beb2 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 10678 | // In C++, we need to do a redeclaration lookup to properly |
| 10679 | // diagnose some problems. |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10680 | if (getLangOpts().CPlusPlus) { |
| John McCall | e9eaf8e | 2010-03-25 21:28:06 +0000 | [diff] [blame] | 10681 | Previous.setRedeclarationKind(ForRedeclaration); |
| 10682 | LookupQualifiedName(Previous, SearchDC); |
| 10683 | } |
| 10684 | } |
| 10685 | |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 10686 | if (!Previous.empty()) { |
| Alp Toker | 0abb057 | 2014-01-18 00:59:32 +0000 | [diff] [blame] | 10687 | NamedDecl *PrevDecl = Previous.getFoundDecl(); |
| 10688 | NamedDecl *DirectPrevDecl = |
| 10689 | getLangOpts().MSVCCompat ? *Previous.begin() : PrevDecl; |
| John McCall | e87beb2 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 10690 | |
| 10691 | // It's okay to have a tag decl in the same scope as a typedef |
| 10692 | // which hides a tag decl in the same scope. Finding this |
| 10693 | // insanity with a redeclaration lookup can only actually happen |
| 10694 | // in C++. |
| 10695 | // |
| 10696 | // This is also okay for elaborated-type-specifiers, which is |
| 10697 | // technically forbidden by the current standard but which is |
| 10698 | // okay according to the likely resolution of an open issue; |
| 10699 | // see http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#407 |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10700 | if (getLangOpts().CPlusPlus) { |
| Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 10701 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(PrevDecl)) { |
| John McCall | e87beb2 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 10702 | if (const TagType *TT = TD->getUnderlyingType()->getAs<TagType>()) { |
| 10703 | TagDecl *Tag = TT->getDecl(); |
| 10704 | if (Tag->getDeclName() == Name && |
| Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 10705 | Tag->getDeclContext()->getRedeclContext() |
| 10706 | ->Equals(TD->getDeclContext()->getRedeclContext())) { |
| John McCall | e87beb2 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 10707 | PrevDecl = Tag; |
| 10708 | Previous.clear(); |
| 10709 | Previous.addDecl(Tag); |
| Douglas Gregor | fcee946 | 2010-08-27 22:55:10 +0000 | [diff] [blame] | 10710 | Previous.resolveKind(); |
| John McCall | e87beb2 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 10711 | } |
| 10712 | } |
| 10713 | } |
| 10714 | } |
| 10715 | |
| Argyrios Kyrtzidis | 0811489 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 10716 | if (TagDecl *PrevTagDecl = dyn_cast<TagDecl>(PrevDecl)) { |
| Chris Lattner | 9ff58d7 | 2008-07-03 03:30:58 +0000 | [diff] [blame] | 10717 | // If this is a use of a previous tag, or if the tag is already declared |
| 10718 | // in the same scope (so that the definition/declaration completes or |
| Argyrios Kyrtzidis | 0811489 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 10719 | // rementions the tag), reuse the decl. |
| John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 10720 | if (TUK == TUK_Reference || TUK == TUK_Friend || |
| Alp Toker | 320374c | 2014-01-17 12:57:21 +0000 | [diff] [blame] | 10721 | isDeclInScope(DirectPrevDecl, SearchDC, S, |
| Richard Smith | 72bcaec | 2013-12-05 04:30:04 +0000 | [diff] [blame] | 10722 | SS.isNotEmpty() || isExplicitSpecialization)) { |
| Chris Lattner | 9ff58d7 | 2008-07-03 03:30:58 +0000 | [diff] [blame] | 10723 | // Make sure that this wasn't declared as an enum and now used as a |
| 10724 | // struct or something similar. |
| Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 10725 | if (!isAcceptableTagRedeclaration(PrevTagDecl, Kind, |
| 10726 | TUK == TUK_Definition, KWLoc, |
| 10727 | *Name)) { |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10728 | bool SafeToContinue |
| Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 10729 | = (PrevTagDecl->getTagKind() != TTK_Enum && |
| 10730 | Kind != TTK_Enum); |
| Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 10731 | if (SafeToContinue) |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10732 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
| Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 10733 | << Name |
| Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 10734 | << FixItHint::CreateReplacement(SourceRange(KWLoc), |
| 10735 | PrevTagDecl->getKindName()); |
| Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 10736 | else |
| 10737 | Diag(KWLoc, diag::err_use_with_wrong_tag) << Name; |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 10738 | Diag(PrevTagDecl->getLocation(), diag::note_previous_use); |
| Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 10739 | |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10740 | if (SafeToContinue) |
| Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 10741 | Kind = PrevTagDecl->getTagKind(); |
| 10742 | else { |
| 10743 | // Recover by making this an anonymous redefinition. |
| 10744 | Name = 0; |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 10745 | Previous.clear(); |
| Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 10746 | Invalid = true; |
| 10747 | } |
| 10748 | } |
| 10749 | |
| Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 10750 | if (Kind == TTK_Enum && PrevTagDecl->getTagKind() == TTK_Enum) { |
| 10751 | const EnumDecl *PrevEnum = cast<EnumDecl>(PrevTagDecl); |
| 10752 | |
| Richard Smith | 0f8ee22 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 10753 | // If this is an elaborated-type-specifier for a scoped enumeration, |
| 10754 | // the 'class' keyword is not necessary and not permitted. |
| 10755 | if (TUK == TUK_Reference || TUK == TUK_Friend) { |
| 10756 | if (ScopedEnum) |
| 10757 | Diag(ScopedEnumKWLoc, diag::err_enum_class_reference) |
| 10758 | << PrevEnum->isScoped() |
| 10759 | << FixItHint::CreateRemoval(ScopedEnumKWLoc); |
| 10760 | return PrevTagDecl; |
| 10761 | } |
| 10762 | |
| Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 10763 | QualType EnumUnderlyingTy; |
| 10764 | if (TypeSourceInfo *TI = EnumUnderlying.dyn_cast<TypeSourceInfo*>()) |
| Richard Smith | 8bcc086 | 2014-01-08 01:16:19 +0000 | [diff] [blame] | 10765 | EnumUnderlyingTy = TI->getType().getUnqualifiedType(); |
| Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 10766 | else if (const Type *T = EnumUnderlying.dyn_cast<const Type*>()) |
| 10767 | EnumUnderlyingTy = QualType(T, 0); |
| 10768 | |
| Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 10769 | // All conflicts with previous declarations are recovered by |
| Richard Smith | b66d777 | 2012-03-23 23:09:08 +0000 | [diff] [blame] | 10770 | // returning the previous declaration, unless this is a definition, |
| 10771 | // in which case we want the caller to bail out. |
| Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 10772 | if (CheckEnumRedeclaration(NameLoc.isValid() ? NameLoc : KWLoc, |
| 10773 | ScopedEnum, EnumUnderlyingTy, PrevEnum)) |
| Richard Smith | b66d777 | 2012-03-23 23:09:08 +0000 | [diff] [blame] | 10774 | return TUK == TUK_Declaration ? PrevTagDecl : 0; |
| Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 10775 | } |
| 10776 | |
| David Majnemer | 55890bf | 2013-06-11 03:51:23 +0000 | [diff] [blame] | 10777 | // C++11 [class.mem]p1: |
| David Majnemer | bfce664 | 2013-06-11 06:19:45 +0000 | [diff] [blame] | 10778 | // A member shall not be declared twice in the member-specification, |
| David Majnemer | 55890bf | 2013-06-11 03:51:23 +0000 | [diff] [blame] | 10779 | // except that a nested class or member class template can be declared |
| 10780 | // and then later defined. |
| 10781 | if (TUK == TUK_Declaration && PrevDecl->isCXXClassMember() && |
| 10782 | S->isDeclScope(PrevDecl)) { |
| 10783 | Diag(NameLoc, diag::ext_member_redeclared); |
| 10784 | Diag(PrevTagDecl->getLocation(), diag::note_previous_declaration); |
| 10785 | } |
| 10786 | |
| Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 10787 | if (!Invalid) { |
| Douglas Gregor | c811d8f | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 10788 | // If this is a use, just return the declaration we found. |
| Chris Lattner | 9ff58d7 | 2008-07-03 03:30:58 +0000 | [diff] [blame] | 10789 | |
| Douglas Gregor | c811d8f | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 10790 | // FIXME: In the future, return a variant or some other clue |
| 10791 | // for the consumer of this Decl to know it doesn't own it. |
| 10792 | // For our current ASTs this shouldn't be a problem, but will |
| 10793 | // need to be changed with DeclGroups. |
| Francois Pichet | e37eeba | 2011-06-01 04:14:20 +0000 | [diff] [blame] | 10794 | if ((TUK == TUK_Reference && (!PrevTagDecl->getFriendObjectKind() || |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10795 | getLangOpts().MicrosoftExt)) || TUK == TUK_Friend) |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10796 | return PrevTagDecl; |
| Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 10797 | |
| Douglas Gregor | c811d8f | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 10798 | // Diagnose attempts to redefine a tag. |
| John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 10799 | if (TUK == TUK_Definition) { |
| Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 10800 | if (TagDecl *Def = PrevTagDecl->getDefinition()) { |
| Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 10801 | // If we're defining a specialization and the previous definition |
| 10802 | // is from an implicit instantiation, don't emit an error |
| 10803 | // here; we'll catch this in the general case below. |
| Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 10804 | bool IsExplicitSpecializationAfterInstantiation = false; |
| 10805 | if (isExplicitSpecialization) { |
| 10806 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Def)) |
| 10807 | IsExplicitSpecializationAfterInstantiation = |
| 10808 | RD->getTemplateSpecializationKind() != |
| 10809 | TSK_ExplicitSpecialization; |
| 10810 | else if (EnumDecl *ED = dyn_cast<EnumDecl>(Def)) |
| 10811 | IsExplicitSpecializationAfterInstantiation = |
| 10812 | ED->getTemplateSpecializationKind() != |
| 10813 | TSK_ExplicitSpecialization; |
| 10814 | } |
| 10815 | |
| 10816 | if (!IsExplicitSpecializationAfterInstantiation) { |
| James Molloy | 6f8780b | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 10817 | // A redeclaration in function prototype scope in C isn't |
| 10818 | // visible elsewhere, so merely issue a warning. |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10819 | if (!getLangOpts().CPlusPlus && S->containedInPrototypeScope()) |
| James Molloy | 6f8780b | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 10820 | Diag(NameLoc, diag::warn_redefinition_in_param_list) << Name; |
| 10821 | else |
| 10822 | Diag(NameLoc, diag::err_redefinition) << Name; |
| Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 10823 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 10824 | // If this is a redefinition, recover by making this |
| 10825 | // struct be anonymous, which will make any later |
| 10826 | // references get the previous definition. |
| 10827 | Name = 0; |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 10828 | Previous.clear(); |
| Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 10829 | Invalid = true; |
| 10830 | } |
| Douglas Gregor | dee1be8 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 10831 | } else { |
| 10832 | // If the type is currently being defined, complain |
| 10833 | // about a nested redefinition. |
| John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 10834 | const TagType *Tag |
| 10835 | = cast<TagType>(Context.getTagDeclType(PrevTagDecl)); |
| Douglas Gregor | dee1be8 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 10836 | if (Tag->isBeingDefined()) { |
| 10837 | Diag(NameLoc, diag::err_nested_redefinition) << Name; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10838 | Diag(PrevTagDecl->getLocation(), |
| Douglas Gregor | dee1be8 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 10839 | diag::note_previous_definition); |
| 10840 | Name = 0; |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 10841 | Previous.clear(); |
| Douglas Gregor | dee1be8 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 10842 | Invalid = true; |
| 10843 | } |
| Douglas Gregor | c811d8f | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 10844 | } |
| Douglas Gregor | dee1be8 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 10845 | |
| Douglas Gregor | c811d8f | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 10846 | // Okay, this is definition of a previously declared or referenced |
| 10847 | // tag PrevDecl. We're going to create a new Decl for it. |
| Douglas Gregor | dee1be8 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 10848 | } |
| Argyrios Kyrtzidis | 0811489 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 10849 | } |
| Douglas Gregor | c811d8f | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 10850 | // If we get here we have (another) forward declaration or we |
| John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 10851 | // have a definition. Just create a new decl. |
| 10852 | |
| Douglas Gregor | c811d8f | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 10853 | } else { |
| 10854 | // If we get here, this is a definition of a new tag type in a nested |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10855 | // scope, e.g. "struct foo; void bar() { struct foo; }", just create a |
| Douglas Gregor | c811d8f | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 10856 | // new decl/type. We set PrevDecl to NULL so that the entities |
| 10857 | // have distinct types. |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 10858 | Previous.clear(); |
| Chris Lattner | 7b9ace6 | 2007-01-23 20:11:08 +0000 | [diff] [blame] | 10859 | } |
| Douglas Gregor | c811d8f | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 10860 | // If we get here, we're going to create a new Decl. If PrevDecl |
| 10861 | // is non-NULL, it's a definition of the tag declared by |
| 10862 | // PrevDecl. If it's NULL, we have a new definition. |
| John McCall | e87beb2 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 10863 | |
| 10864 | |
| 10865 | // Otherwise, PrevDecl is not a tag, but was found with tag |
| 10866 | // lookup. This is only actually possible in C++, where a few |
| 10867 | // things like templates still live in the tag namespace. |
| Argyrios Kyrtzidis | 0811489 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 10868 | } else { |
| John McCall | e87beb2 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 10869 | // Use a better diagnostic if an elaborated-type-specifier |
| 10870 | // found the wrong kind of type on the first |
| 10871 | // (non-redeclaration) lookup. |
| 10872 | if ((TUK == TUK_Reference || TUK == TUK_Friend) && |
| 10873 | !Previous.isForRedeclaration()) { |
| 10874 | unsigned Kind = 0; |
| 10875 | if (isa<TypedefDecl>(PrevDecl)) Kind = 1; |
| Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 10876 | else if (isa<TypeAliasDecl>(PrevDecl)) Kind = 2; |
| 10877 | else if (isa<ClassTemplateDecl>(PrevDecl)) Kind = 3; |
| John McCall | e87beb2 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 10878 | Diag(NameLoc, diag::err_tag_reference_non_tag) << Kind; |
| 10879 | Diag(PrevDecl->getLocation(), diag::note_declared_at); |
| 10880 | Invalid = true; |
| 10881 | |
| 10882 | // Otherwise, only diagnose if the declaration is in scope. |
| Richard Smith | 72bcaec | 2013-12-05 04:30:04 +0000 | [diff] [blame] | 10883 | } else if (!isDeclInScope(PrevDecl, SearchDC, S, |
| 10884 | SS.isNotEmpty() || isExplicitSpecialization)) { |
| John McCall | e87beb2 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 10885 | // do nothing |
| 10886 | |
| 10887 | // Diagnose implicit declarations introduced by elaborated types. |
| 10888 | } else if (TUK == TUK_Reference || TUK == TUK_Friend) { |
| 10889 | unsigned Kind = 0; |
| 10890 | if (isa<TypedefDecl>(PrevDecl)) Kind = 1; |
| Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 10891 | else if (isa<TypeAliasDecl>(PrevDecl)) Kind = 2; |
| 10892 | else if (isa<ClassTemplateDecl>(PrevDecl)) Kind = 3; |
| John McCall | e87beb2 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 10893 | Diag(NameLoc, diag::err_tag_reference_conflict) << Kind; |
| 10894 | Diag(PrevDecl->getLocation(), diag::note_previous_decl) << PrevDecl; |
| 10895 | Invalid = true; |
| 10896 | |
| 10897 | // Otherwise it's a declaration. Call out a particularly common |
| 10898 | // case here. |
| Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 10899 | } else if (TypedefNameDecl *TND = dyn_cast<TypedefNameDecl>(PrevDecl)) { |
| 10900 | unsigned Kind = 0; |
| 10901 | if (isa<TypeAliasDecl>(PrevDecl)) Kind = 1; |
| John McCall | e87beb2 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 10902 | Diag(NameLoc, diag::err_tag_definition_of_typedef) |
| Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 10903 | << Name << Kind << TND->getUnderlyingType(); |
| John McCall | e87beb2 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 10904 | Diag(PrevDecl->getLocation(), diag::note_previous_decl) << PrevDecl; |
| 10905 | Invalid = true; |
| 10906 | |
| 10907 | // Otherwise, diagnose. |
| 10908 | } else { |
| 10909 | // The tag name clashes with something else in the target scope, |
| 10910 | // issue an error and recover by making this tag be anonymous. |
| Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 10911 | Diag(NameLoc, diag::err_redefinition_different_kind) << Name; |
| Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 10912 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
| Argyrios Kyrtzidis | 7da34d0 | 2008-07-16 07:45:46 +0000 | [diff] [blame] | 10913 | Name = 0; |
| Douglas Gregor | dee1be8 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 10914 | Invalid = true; |
| Argyrios Kyrtzidis | 7da34d0 | 2008-07-16 07:45:46 +0000 | [diff] [blame] | 10915 | } |
| John McCall | e87beb2 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 10916 | |
| 10917 | // The existing declaration isn't relevant to us; we're in a |
| 10918 | // new scope, so clear out the previous declaration. |
| 10919 | Previous.clear(); |
| Chris Lattner | 8799cf2 | 2007-01-23 01:57:16 +0000 | [diff] [blame] | 10920 | } |
| Chris Lattner | 18b1962 | 2007-01-22 07:39:13 +0000 | [diff] [blame] | 10921 | } |
| Argyrios Kyrtzidis | 16ac9be | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 10922 | |
| Chris Lattner | 438e501 | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 10923 | CreateNewDecl: |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10924 | |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 10925 | TagDecl *PrevDecl = 0; |
| 10926 | if (Previous.isSingleResult()) |
| 10927 | PrevDecl = cast<TagDecl>(Previous.getFoundDecl()); |
| 10928 | |
| Chris Lattner | bf0b798 | 2007-01-23 04:27:41 +0000 | [diff] [blame] | 10929 | // If there is an identifier, use the location of the identifier as the |
| 10930 | // location of the decl, otherwise use the location of the struct/union |
| 10931 | // keyword. |
| 10932 | SourceLocation Loc = NameLoc.isValid() ? NameLoc : KWLoc; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10933 | |
| Douglas Gregor | c811d8f | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 10934 | // Otherwise, create a new declaration. If there is a previous |
| 10935 | // declaration of the same entity, the two will be linked via |
| 10936 | // PrevDecl. |
| Chris Lattner | 7b9ace6 | 2007-01-23 20:11:08 +0000 | [diff] [blame] | 10937 | TagDecl *New; |
| Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 10938 | |
| Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 10939 | bool IsForwardReference = false; |
| Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 10940 | if (Kind == TTK_Enum) { |
| Chris Lattner | 776fac8 | 2007-06-09 00:53:06 +0000 | [diff] [blame] | 10941 | // FIXME: Tag decls should be chained to any simultaneous vardecls, e.g.: |
| 10942 | // enum X { A, B, C } D; D should chain to X. |
| Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 10943 | New = EnumDecl::Create(Context, SearchDC, KWLoc, Loc, Name, |
| Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 10944 | cast_or_null<EnumDecl>(PrevDecl), ScopedEnum, |
| Abramo Bagnara | 0e05e24 | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 10945 | ScopedEnumUsesClassTag, !EnumUnderlying.isNull()); |
| Chris Lattner | 5f52150 | 2007-01-25 06:27:24 +0000 | [diff] [blame] | 10946 | // If this is an undefined enum, warn. |
| Douglas Gregor | c9ea2d5 | 2010-06-22 14:26:35 +0000 | [diff] [blame] | 10947 | if (TUK != TUK_Definition && !Invalid) { |
| 10948 | TagDecl *Def; |
| Douglas Gregor | 780420e | 2013-03-25 22:22:35 +0000 | [diff] [blame] | 10949 | if ((getLangOpts().CPlusPlus11 || getLangOpts().ObjC2) && |
| 10950 | cast<EnumDecl>(New)->isFixed()) { |
| Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 10951 | // C++0x: 7.2p2: opaque-enum-declaration. |
| 10952 | // Conflicts are diagnosed above. Do nothing. |
| 10953 | } |
| 10954 | else if (PrevDecl && (Def = cast<EnumDecl>(PrevDecl)->getDefinition())) { |
| Douglas Gregor | c9ea2d5 | 2010-06-22 14:26:35 +0000 | [diff] [blame] | 10955 | Diag(Loc, diag::ext_forward_ref_enum_def) |
| 10956 | << New; |
| 10957 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 10958 | } else { |
| Francois Pichet | 488b4a7 | 2010-09-12 05:06:55 +0000 | [diff] [blame] | 10959 | unsigned DiagID = diag::ext_forward_ref_enum; |
| Alp Toker | bfa3934 | 2014-01-14 12:51:41 +0000 | [diff] [blame] | 10960 | if (getLangOpts().MSVCCompat) |
| Francois Pichet | 488b4a7 | 2010-09-12 05:06:55 +0000 | [diff] [blame] | 10961 | DiagID = diag::ext_ms_forward_ref_enum; |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10962 | else if (getLangOpts().CPlusPlus) |
| Francois Pichet | 488b4a7 | 2010-09-12 05:06:55 +0000 | [diff] [blame] | 10963 | DiagID = diag::err_forward_ref_enum; |
| 10964 | Diag(Loc, DiagID); |
| Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 10965 | |
| 10966 | // If this is a forward-declared reference to an enumeration, make a |
| 10967 | // note of it; we won't actually be introducing the declaration into |
| 10968 | // the declaration context. |
| 10969 | if (TUK == TUK_Reference) |
| 10970 | IsForwardReference = true; |
| Douglas Gregor | c9ea2d5 | 2010-06-22 14:26:35 +0000 | [diff] [blame] | 10971 | } |
| Douglas Gregor | d45b93b | 2009-03-06 18:34:03 +0000 | [diff] [blame] | 10972 | } |
| Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 10973 | |
| 10974 | if (EnumUnderlying) { |
| 10975 | EnumDecl *ED = cast<EnumDecl>(New); |
| 10976 | if (TypeSourceInfo *TI = EnumUnderlying.dyn_cast<TypeSourceInfo*>()) |
| 10977 | ED->setIntegerTypeSourceInfo(TI); |
| 10978 | else |
| 10979 | ED->setIntegerType(QualType(EnumUnderlying.get<const Type*>(), 0)); |
| 10980 | ED->setPromotionType(ED->getIntegerType()); |
| 10981 | } |
| 10982 | |
| Argyrios Kyrtzidis | 554a07b | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 10983 | } else { |
| 10984 | // struct/union/class |
| 10985 | |
| Chris Lattner | 776fac8 | 2007-06-09 00:53:06 +0000 | [diff] [blame] | 10986 | // FIXME: Tag decls should be chained to any simultaneous vardecls, e.g.: |
| 10987 | // struct X { int A; } D; D should chain to X. |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10988 | if (getLangOpts().CPlusPlus) { |
| Ted Kremenek | 6ddf53e | 2008-09-05 17:39:33 +0000 | [diff] [blame] | 10989 | // FIXME: Look for a way to use RecordDecl for simple structs. |
| Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 10990 | New = CXXRecordDecl::Create(Context, Kind, SearchDC, KWLoc, Loc, Name, |
| Douglas Gregor | c811d8f | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 10991 | cast_or_null<CXXRecordDecl>(PrevDecl)); |
| Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 10992 | |
| Argyrios Kyrtzidis | 2d68810 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 10993 | if (isStdBadAlloc && (!StdBadAlloc || getStdBadAlloc()->isImplicit())) |
| Douglas Gregor | 87f5406 | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 10994 | StdBadAlloc = cast<CXXRecordDecl>(New); |
| 10995 | } else |
| Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 10996 | New = RecordDecl::Create(Context, Kind, SearchDC, KWLoc, Loc, Name, |
| Douglas Gregor | c811d8f | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 10997 | cast_or_null<RecordDecl>(PrevDecl)); |
| Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 10998 | } |
| Douglas Gregor | c811d8f | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 10999 | |
| Richard Smith | 649c7b06 | 2014-01-08 00:56:48 +0000 | [diff] [blame] | 11000 | // C++11 [dcl.type]p3: |
| 11001 | // A type-specifier-seq shall not define a class or enumeration [...]. |
| 11002 | if (getLangOpts().CPlusPlus && IsTypeSpecifier && TUK == TUK_Definition) { |
| 11003 | Diag(New->getLocation(), diag::err_type_defined_in_type_specifier) |
| 11004 | << Context.getTagDeclType(New); |
| 11005 | Invalid = true; |
| 11006 | } |
| 11007 | |
| John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 11008 | // Maybe add qualifier info. |
| 11009 | if (SS.isNotEmpty()) { |
| Fariborz Jahanian | 862fac9 | 2010-05-14 21:35:02 +0000 | [diff] [blame] | 11010 | if (SS.isSet()) { |
| Douglas Gregor | b7d17dd | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 11011 | // If this is either a declaration or a definition, check the |
| 11012 | // nested-name-specifier against the current context. We don't do this |
| 11013 | // for explicit specializations, because they have similar checking |
| 11014 | // (with more specific diagnostics) in the call to |
| 11015 | // CheckMemberSpecialization, below. |
| 11016 | if (!isExplicitSpecialization && |
| 11017 | (TUK == TUK_Definition || TUK == TUK_Declaration) && |
| 11018 | diagnoseQualifiedDeclaration(SS, DC, OrigName, NameLoc)) |
| 11019 | Invalid = true; |
| 11020 | |
| Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 11021 | New->setQualifierInfo(SS.getWithLocInContext(Context)); |
| Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 11022 | if (TemplateParameterLists.size() > 0) { |
| Douglas Gregor | 20527e2 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 11023 | New->setTemplateParameterListsInfo(Context, |
| Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 11024 | TemplateParameterLists.size(), |
| Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 11025 | TemplateParameterLists.data()); |
| Abramo Bagnara | da41d0c | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 11026 | } |
| Fariborz Jahanian | 862fac9 | 2010-05-14 21:35:02 +0000 | [diff] [blame] | 11027 | } |
| 11028 | else |
| 11029 | Invalid = true; |
| John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 11030 | } |
| 11031 | |
| Daniel Dunbar | 8804f2e | 2010-05-27 01:53:40 +0000 | [diff] [blame] | 11032 | if (RecordDecl *RD = dyn_cast<RecordDecl>(New)) { |
| 11033 | // Add alignment attributes if necessary; these attributes are checked when |
| 11034 | // the ASTContext lays out the structure. |
| Douglas Gregor | c811d8f | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 11035 | // |
| 11036 | // It is important for implementing the correct semantics that this |
| 11037 | // happen here (in act on tag decl). The #pragma pack stack is |
| 11038 | // maintained as a result of parser callbacks which can occur at |
| 11039 | // many points during the parsing of a struct declaration (because |
| 11040 | // the #pragma tokens are effectively skipped over during the |
| 11041 | // parsing of the struct). |
| Eli Friedman | 0415f3e1 | 2012-08-08 21:08:34 +0000 | [diff] [blame] | 11042 | if (TUK == TUK_Definition) { |
| 11043 | AddAlignmentAttributesForRecord(RD); |
| 11044 | AddMsStructLayoutForRecord(RD); |
| 11045 | } |
| Douglas Gregor | c811d8f | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 11046 | } |
| 11047 | |
| Douglas Gregor | 21823bf | 2011-12-20 18:11:52 +0000 | [diff] [blame] | 11048 | if (ModulePrivateLoc.isValid()) { |
| Douglas Gregor | 3c7cd6a | 2011-09-09 20:53:38 +0000 | [diff] [blame] | 11049 | if (isExplicitSpecialization) |
| 11050 | Diag(New->getLocation(), diag::err_module_private_specialization) |
| 11051 | << 2 |
| 11052 | << FixItHint::CreateRemoval(ModulePrivateLoc); |
| Douglas Gregor | 4186681 | 2011-09-12 18:37:38 +0000 | [diff] [blame] | 11053 | // __module_private__ does not apply to local classes. However, we only |
| 11054 | // diagnose this as an error when the declaration specifiers are |
| 11055 | // freestanding. Here, we just ignore the __module_private__. |
| Douglas Gregor | 4186681 | 2011-09-12 18:37:38 +0000 | [diff] [blame] | 11056 | else if (!SearchDC->isFunctionOrMethod()) |
| Douglas Gregor | 2820e69 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 11057 | New->setModulePrivate(); |
| 11058 | } |
| 11059 | |
| Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 11060 | // If this is a specialization of a member class (of a class template), |
| 11061 | // check the specialization. |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 11062 | if (isExplicitSpecialization && CheckMemberSpecialization(New, Previous)) |
| Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 11063 | Invalid = true; |
| Douglas Gregor | b7d17dd | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 11064 | |
| Douglas Gregor | dee1be8 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 11065 | if (Invalid) |
| 11066 | New->setInvalidDecl(); |
| 11067 | |
| Douglas Gregor | c811d8f | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 11068 | if (Attr) |
| Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 11069 | ProcessDeclAttributeList(S, New, Attr); |
| Douglas Gregor | c811d8f | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 11070 | |
| Peter Collingbourne | fa1d4e1 | 2014-02-22 03:05:49 +0000 | [diff] [blame] | 11071 | // If we're declaring or defining a tag in function prototype scope in C, |
| 11072 | // note that this type can only be used within the function and add it to |
| 11073 | // the list of decls to inject into the function definition scope. |
| 11074 | if (!getLangOpts().CPlusPlus && (Name || Kind == TTK_Enum) && |
| 11075 | getNonFieldDeclScope(S)->isFunctionPrototypeScope()) { |
| Douglas Gregor | 658b955 | 2009-01-09 22:42:13 +0000 | [diff] [blame] | 11076 | Diag(Loc, diag::warn_decl_in_param_list) << Context.getTagDeclType(New); |
| Peter Collingbourne | fa1d4e1 | 2014-02-22 03:05:49 +0000 | [diff] [blame] | 11077 | DeclsInPrototypeScope.push_back(New); |
| 11078 | } |
| Douglas Gregor | 658b955 | 2009-01-09 22:42:13 +0000 | [diff] [blame] | 11079 | |
| Douglas Gregor | c811d8f | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 11080 | // Set the lexical context. If the tag has a C++ scope specifier, the |
| 11081 | // lexical context will be different from the semantic context. |
| Douglas Gregor | 8761da5 | 2009-02-03 00:34:39 +0000 | [diff] [blame] | 11082 | New->setLexicalDeclContext(CurContext); |
| Douglas Gregor | dee1be8 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 11083 | |
| John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 11084 | // Mark this as a friend decl if applicable. |
| Francois Pichet | e37eeba | 2011-06-01 04:14:20 +0000 | [diff] [blame] | 11085 | // In Microsoft mode, a friend declaration also acts as a forward |
| 11086 | // declaration so we always pass true to setObjectOfFriendDecl to make |
| 11087 | // the tag name visible. |
| John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 11088 | if (TUK == TUK_Friend) |
| Richard Smith | 6401768 | 2013-07-17 23:53:16 +0000 | [diff] [blame] | 11089 | New->setObjectOfFriendDecl(!FriendSawTagOutsideEnclosingNamespace && |
| 11090 | getLangOpts().MicrosoftExt); |
| John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 11091 | |
| Anders Carlsson | 5558ca1 | 2009-03-26 01:19:02 +0000 | [diff] [blame] | 11092 | // Set the access specifier. |
| John McCall | e9eaf8e | 2010-03-25 21:28:06 +0000 | [diff] [blame] | 11093 | if (!Invalid && SearchDC->isRecord()) |
| Douglas Gregor | b8006faf | 2009-05-27 17:30:49 +0000 | [diff] [blame] | 11094 | SetMemberAccessSpecifier(New, PrevDecl, AS); |
| Douglas Gregor | 6c2adff | 2009-03-25 22:00:53 +0000 | [diff] [blame] | 11095 | |
| John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 11096 | if (TUK == TUK_Definition) |
| Douglas Gregor | dee1be8 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 11097 | New->startDefinition(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11098 | |
| Chris Lattner | 18b1962 | 2007-01-22 07:39:13 +0000 | [diff] [blame] | 11099 | // If this has an identifier, add it to the scope stack. |
| John McCall | 2dc078f | 2009-09-02 00:55:30 +0000 | [diff] [blame] | 11100 | if (TUK == TUK_Friend) { |
| John McCall | f8bd861 | 2009-09-02 19:32:14 +0000 | [diff] [blame] | 11101 | // We might be replacing an existing declaration in the lookup tables; |
| 11102 | // if so, borrow its access specifier. |
| 11103 | if (PrevDecl) |
| 11104 | New->setAccess(PrevDecl->getAccess()); |
| 11105 | |
| Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 11106 | DeclContext *DC = New->getDeclContext()->getRedeclContext(); |
| Richard Smith | 05afe5e | 2012-03-13 03:12:56 +0000 | [diff] [blame] | 11107 | DC->makeDeclVisibleInContext(New); |
| John McCall | e9eaf8e | 2010-03-25 21:28:06 +0000 | [diff] [blame] | 11108 | if (Name) // can be null along some error paths |
| John McCall | 2dc078f | 2009-09-02 00:55:30 +0000 | [diff] [blame] | 11109 | if (Scope *EnclosingScope = getScopeForDeclContext(S, DC)) |
| 11110 | PushOnScopeChains(New, EnclosingScope, /* AddToContext = */ false); |
| John McCall | 2dc078f | 2009-09-02 00:55:30 +0000 | [diff] [blame] | 11111 | } else if (Name) { |
| Douglas Gregor | 45a33ec | 2009-01-12 18:45:55 +0000 | [diff] [blame] | 11112 | S = getNonFieldDeclScope(S); |
| Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 11113 | PushOnScopeChains(New, S, !IsForwardReference); |
| 11114 | if (IsForwardReference) |
| Richard Smith | 05afe5e | 2012-03-13 03:12:56 +0000 | [diff] [blame] | 11115 | SearchDC->makeDeclVisibleInContext(New); |
| Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 11116 | |
| Douglas Gregor | c6f58fe | 2009-01-12 22:49:06 +0000 | [diff] [blame] | 11117 | } else { |
| Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 11118 | CurContext->addDecl(New); |
| Chris Lattner | 18b1962 | 2007-01-22 07:39:13 +0000 | [diff] [blame] | 11119 | } |
| Argyrios Kyrtzidis | 9e59b57 | 2008-11-09 23:41:00 +0000 | [diff] [blame] | 11120 | |
| Douglas Gregor | 27821ce | 2009-07-07 16:35:42 +0000 | [diff] [blame] | 11121 | // If this is the C FILE type, notify the AST context. |
| 11122 | if (IdentifierInfo *II = New->getIdentifier()) |
| 11123 | if (!New->isInvalidDecl() && |
| Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 11124 | New->getDeclContext()->getRedeclContext()->isTranslationUnit() && |
| Douglas Gregor | 27821ce | 2009-07-07 16:35:42 +0000 | [diff] [blame] | 11125 | II->isStr("FILE")) |
| 11126 | Context.setFILEDecl(New); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11127 | |
| Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 11128 | if (PrevDecl) |
| 11129 | mergeDeclAttributes(New, PrevDecl); |
| 11130 | |
| Rafael Espindola | e3a14bb | 2012-07-17 15:14:47 +0000 | [diff] [blame] | 11131 | // If there's a #pragma GCC visibility in scope, set the visibility of this |
| 11132 | // record. |
| 11133 | AddPushedVisibilityAttribute(New); |
| 11134 | |
| Douglas Gregor | d6ab874 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 11135 | OwnedDecl = true; |
| Richard Smith | 3e28469 | 2012-12-05 11:34:06 +0000 | [diff] [blame] | 11136 | // In C++, don't return an invalid declaration. We can't recover well from |
| 11137 | // the cases where we make the type anonymous. |
| 11138 | return (Invalid && getLangOpts().CPlusPlus) ? 0 : New; |
| Chris Lattner | 18b1962 | 2007-01-22 07:39:13 +0000 | [diff] [blame] | 11139 | } |
| Chris Lattner | 1300fb9 | 2007-01-23 23:42:53 +0000 | [diff] [blame] | 11140 | |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11141 | void Sema::ActOnTagStartDefinition(Scope *S, Decl *TagD) { |
| Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 11142 | AdjustDeclIfTemplate(TagD); |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11143 | TagDecl *Tag = cast<TagDecl>(TagD); |
| Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 11144 | |
| Douglas Gregor | 82ac25e | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 11145 | // Enter the tag context. |
| 11146 | PushDeclContext(S, Tag); |
| Dmitri Gribenko | f26054f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 11147 | |
| 11148 | ActOnDocumentableDecl(TagD); |
| Rafael Espindola | 4dedd0c | 2012-07-12 04:47:34 +0000 | [diff] [blame] | 11149 | |
| 11150 | // If there's a #pragma GCC visibility in scope, set the visibility of this |
| 11151 | // record. |
| 11152 | AddPushedVisibilityAttribute(Tag); |
| John McCall | 1c7e6ec | 2009-12-20 07:58:13 +0000 | [diff] [blame] | 11153 | } |
| Douglas Gregor | 82ac25e | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 11154 | |
| Argyrios Kyrtzidis | 9321ad3 | 2011-10-06 23:23:20 +0000 | [diff] [blame] | 11155 | Decl *Sema::ActOnObjCContainerStartDefinition(Decl *IDecl) { |
| Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 11156 | assert(isa<ObjCContainerDecl>(IDecl) && |
| 11157 | "ActOnObjCContainerStartDefinition - Not ObjCContainerDecl"); |
| 11158 | DeclContext *OCD = cast<DeclContext>(IDecl); |
| 11159 | assert(getContainingDC(OCD) == CurContext && |
| 11160 | "The next DeclContext should be lexically contained in the current one."); |
| 11161 | CurContext = OCD; |
| Argyrios Kyrtzidis | 9321ad3 | 2011-10-06 23:23:20 +0000 | [diff] [blame] | 11162 | return IDecl; |
| Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 11163 | } |
| 11164 | |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11165 | void Sema::ActOnStartCXXMemberDeclarations(Scope *S, Decl *TagD, |
| Anders Carlsson | 30f2944 | 2011-03-25 14:31:08 +0000 | [diff] [blame] | 11166 | SourceLocation FinalLoc, |
| David Majnemer | a543308 | 2013-10-18 00:33:31 +0000 | [diff] [blame] | 11167 | bool IsFinalSpelledSealed, |
| John McCall | 1c7e6ec | 2009-12-20 07:58:13 +0000 | [diff] [blame] | 11168 | SourceLocation LBraceLoc) { |
| 11169 | AdjustDeclIfTemplate(TagD); |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11170 | CXXRecordDecl *Record = cast<CXXRecordDecl>(TagD); |
| Douglas Gregor | 82ac25e | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 11171 | |
| John McCall | 1c7e6ec | 2009-12-20 07:58:13 +0000 | [diff] [blame] | 11172 | FieldCollector->StartClass(); |
| 11173 | |
| 11174 | if (!Record->getIdentifier()) |
| 11175 | return; |
| 11176 | |
| Anders Carlsson | 30f2944 | 2011-03-25 14:31:08 +0000 | [diff] [blame] | 11177 | if (FinalLoc.isValid()) |
| David Majnemer | a543308 | 2013-10-18 00:33:31 +0000 | [diff] [blame] | 11178 | Record->addAttr(new (Context) |
| 11179 | FinalAttr(FinalLoc, Context, IsFinalSpelledSealed)); |
| 11180 | |
| John McCall | 1c7e6ec | 2009-12-20 07:58:13 +0000 | [diff] [blame] | 11181 | // C++ [class]p2: |
| 11182 | // [...] The class-name is also inserted into the scope of the |
| 11183 | // class itself; this is known as the injected-class-name. For |
| 11184 | // purposes of access checking, the injected-class-name is treated |
| 11185 | // as if it were a public member name. |
| 11186 | CXXRecordDecl *InjectedClassName |
| Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 11187 | = CXXRecordDecl::Create(Context, Record->getTagKind(), CurContext, |
| 11188 | Record->getLocStart(), Record->getLocation(), |
| John McCall | 1c7e6ec | 2009-12-20 07:58:13 +0000 | [diff] [blame] | 11189 | Record->getIdentifier(), |
| Argyrios Kyrtzidis | 470c454 | 2010-10-14 20:14:21 +0000 | [diff] [blame] | 11190 | /*PrevDecl=*/0, |
| 11191 | /*DelayTypeCreation=*/true); |
| 11192 | Context.getTypeDeclType(InjectedClassName, Record); |
| John McCall | 1c7e6ec | 2009-12-20 07:58:13 +0000 | [diff] [blame] | 11193 | InjectedClassName->setImplicit(); |
| 11194 | InjectedClassName->setAccess(AS_public); |
| 11195 | if (ClassTemplateDecl *Template = Record->getDescribedClassTemplate()) |
| 11196 | InjectedClassName->setDescribedClassTemplate(Template); |
| 11197 | PushOnScopeChains(InjectedClassName, S); |
| 11198 | assert(InjectedClassName->isInjectedClassName() && |
| 11199 | "Broken injected-class-name"); |
| Douglas Gregor | 82ac25e | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 11200 | } |
| 11201 | |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11202 | void Sema::ActOnTagFinishDefinition(Scope *S, Decl *TagD, |
| Argyrios Kyrtzidis | 23e1f1d | 2009-07-14 03:17:52 +0000 | [diff] [blame] | 11203 | SourceLocation RBraceLoc) { |
| Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 11204 | AdjustDeclIfTemplate(TagD); |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11205 | TagDecl *Tag = cast<TagDecl>(TagD); |
| Argyrios Kyrtzidis | 23e1f1d | 2009-07-14 03:17:52 +0000 | [diff] [blame] | 11206 | Tag->setRBraceLoc(RBraceLoc); |
| Douglas Gregor | 82ac25e | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 11207 | |
| Argyrios Kyrtzidis | 2559629 | 2012-03-09 20:10:30 +0000 | [diff] [blame] | 11208 | // Make sure we "complete" the definition even it is invalid. |
| 11209 | if (Tag->isBeingDefined()) { |
| 11210 | assert(Tag->isInvalidDecl() && "We should already have completed it"); |
| 11211 | if (RecordDecl *RD = dyn_cast<RecordDecl>(Tag)) |
| 11212 | RD->completeDefinition(); |
| 11213 | } |
| 11214 | |
| Douglas Gregor | 82ac25e | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 11215 | if (isa<CXXRecordDecl>(Tag)) |
| 11216 | FieldCollector->FinishClass(); |
| 11217 | |
| 11218 | // Exit this scope of this tag's definition. |
| 11219 | PopDeclContext(); |
| Argyrios Kyrtzidis | c821f73 | 2013-01-29 18:00:54 +0000 | [diff] [blame] | 11220 | |
| 11221 | if (getCurLexicalContext()->isObjCContainer() && |
| 11222 | Tag->getDeclContext()->isFileContext()) |
| 11223 | Tag->setTopLevelDeclInObjCContainer(); |
| 11224 | |
| Douglas Gregor | 82ac25e | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 11225 | // Notify the consumer that we've defined a tag. |
| Serge Pavlov | fb73ec8 | 2013-07-02 17:31:56 +0000 | [diff] [blame] | 11226 | if (!Tag->isInvalidDecl()) |
| 11227 | Consumer.HandleTagDeclDefinition(Tag); |
| Douglas Gregor | 82ac25e | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 11228 | } |
| Chris Lattner | 535b830 | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 11229 | |
| Fariborz Jahanian | 4327b32 | 2011-08-29 17:33:12 +0000 | [diff] [blame] | 11230 | void Sema::ActOnObjCContainerFinishDefinition() { |
| Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 11231 | // Exit this scope of this interface definition. |
| 11232 | PopDeclContext(); |
| 11233 | } |
| Argyrios Kyrtzidis | 9321ad3 | 2011-10-06 23:23:20 +0000 | [diff] [blame] | 11234 | |
| Argyrios Kyrtzidis | a9aabf7 | 2011-10-27 00:09:34 +0000 | [diff] [blame] | 11235 | void Sema::ActOnObjCTemporaryExitContainerContext(DeclContext *DC) { |
| Argyrios Kyrtzidis | 67f914d | 2011-10-27 00:53:06 +0000 | [diff] [blame] | 11236 | assert(DC == CurContext && "Mismatch of container contexts"); |
| 11237 | OriginalLexicalContext = DC; |
| Argyrios Kyrtzidis | 9321ad3 | 2011-10-06 23:23:20 +0000 | [diff] [blame] | 11238 | ActOnObjCContainerFinishDefinition(); |
| 11239 | } |
| 11240 | |
| Argyrios Kyrtzidis | a9aabf7 | 2011-10-27 00:09:34 +0000 | [diff] [blame] | 11241 | void Sema::ActOnObjCReenterContainerContext(DeclContext *DC) { |
| 11242 | ActOnObjCContainerStartDefinition(cast<Decl>(DC)); |
| Argyrios Kyrtzidis | 9321ad3 | 2011-10-06 23:23:20 +0000 | [diff] [blame] | 11243 | OriginalLexicalContext = 0; |
| 11244 | } |
| 11245 | |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11246 | void Sema::ActOnTagDefinitionError(Scope *S, Decl *TagD) { |
| John McCall | 2ff380a | 2010-03-17 00:38:33 +0000 | [diff] [blame] | 11247 | AdjustDeclIfTemplate(TagD); |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11248 | TagDecl *Tag = cast<TagDecl>(TagD); |
| John McCall | 2ff380a | 2010-03-17 00:38:33 +0000 | [diff] [blame] | 11249 | Tag->setInvalidDecl(); |
| 11250 | |
| Argyrios Kyrtzidis | 2559629 | 2012-03-09 20:10:30 +0000 | [diff] [blame] | 11251 | // Make sure we "complete" the definition even it is invalid. |
| 11252 | if (Tag->isBeingDefined()) { |
| 11253 | if (RecordDecl *RD = dyn_cast<RecordDecl>(Tag)) |
| 11254 | RD->completeDefinition(); |
| 11255 | } |
| 11256 | |
| John McCall | 71ba5f2 | 2010-03-17 19:25:57 +0000 | [diff] [blame] | 11257 | // We're undoing ActOnTagStartDefinition here, not |
| 11258 | // ActOnStartCXXMemberDeclarations, so we don't have to mess with |
| 11259 | // the FieldCollector. |
| John McCall | 2ff380a | 2010-03-17 00:38:33 +0000 | [diff] [blame] | 11260 | |
| 11261 | PopDeclContext(); |
| 11262 | } |
| 11263 | |
| Chris Lattner | f9b00eb | 2009-04-20 17:29:38 +0000 | [diff] [blame] | 11264 | // Note that FieldName may be null for anonymous bitfields. |
| Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 11265 | ExprResult Sema::VerifyBitField(SourceLocation FieldLoc, |
| 11266 | IdentifierInfo *FieldName, |
| Reid Kleckner | 736bc98 | 2013-07-17 20:46:03 +0000 | [diff] [blame] | 11267 | QualType FieldTy, bool IsMsStruct, |
| 11268 | Expr *BitWidth, bool *ZeroWidth) { |
| Eli Friedman | c96d496 | 2009-08-15 21:55:26 +0000 | [diff] [blame] | 11269 | // Default to true; that shouldn't confuse checks for emptiness |
| 11270 | if (ZeroWidth) |
| 11271 | *ZeroWidth = true; |
| 11272 | |
| Chris Lattner | 73bf7b4 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 11273 | // C99 6.7.2.1p4 - verify the field type. |
| Chris Lattner | d26760a | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 11274 | // C++ 9.6p3: A bit-field shall have integral or enumeration type. |
| Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 11275 | if (!FieldTy->isDependentType() && !FieldTy->isIntegralOrEnumerationType()) { |
| Chris Lattner | 73bf7b4 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 11276 | // Handle incomplete types with specific error. |
| Douglas Gregor | 8145742 | 2009-03-10 21:58:27 +0000 | [diff] [blame] | 11277 | if (RequireCompleteType(FieldLoc, FieldTy, diag::err_field_incomplete)) |
| Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 11278 | return ExprError(); |
| Chris Lattner | f9b00eb | 2009-04-20 17:29:38 +0000 | [diff] [blame] | 11279 | if (FieldName) |
| 11280 | return Diag(FieldLoc, diag::err_not_integral_type_bitfield) |
| 11281 | << FieldName << FieldTy << BitWidth->getSourceRange(); |
| 11282 | return Diag(FieldLoc, diag::err_not_integral_type_anon_bitfield) |
| 11283 | << FieldTy << BitWidth->getSourceRange(); |
| Douglas Gregor | a02a72a | 2010-12-15 23:18:36 +0000 | [diff] [blame] | 11284 | } else if (DiagnoseUnexpandedParameterPack(const_cast<Expr *>(BitWidth), |
| 11285 | UPPC_BitFieldWidth)) |
| Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 11286 | return ExprError(); |
| Douglas Gregor | 1efa437 | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 11287 | |
| 11288 | // If the bit-width is type- or value-dependent, don't try to check |
| 11289 | // it now. |
| 11290 | if (BitWidth->isValueDependent() || BitWidth->isTypeDependent()) |
| Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 11291 | return Owned(BitWidth); |
| Douglas Gregor | 1efa437 | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 11292 | |
| Anders Carlsson | 5df391e | 2008-12-06 20:33:04 +0000 | [diff] [blame] | 11293 | llvm::APSInt Value; |
| Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 11294 | ExprResult ICE = VerifyIntegerConstantExpression(BitWidth, &Value); |
| 11295 | if (ICE.isInvalid()) |
| 11296 | return ICE; |
| 11297 | BitWidth = ICE.take(); |
| Anders Carlsson | 5df391e | 2008-12-06 20:33:04 +0000 | [diff] [blame] | 11298 | |
| Eli Friedman | c96d496 | 2009-08-15 21:55:26 +0000 | [diff] [blame] | 11299 | if (Value != 0 && ZeroWidth) |
| 11300 | *ZeroWidth = false; |
| 11301 | |
| Chris Lattner | 81ed680 | 2008-12-12 04:56:04 +0000 | [diff] [blame] | 11302 | // Zero-width bitfield is ok for anonymous field. |
| 11303 | if (Value == 0 && FieldName) |
| 11304 | return Diag(FieldLoc, diag::err_bitfield_has_zero_width) << FieldName; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11305 | |
| Chris Lattner | f9b00eb | 2009-04-20 17:29:38 +0000 | [diff] [blame] | 11306 | if (Value.isSigned() && Value.isNegative()) { |
| 11307 | if (FieldName) |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11308 | return Diag(FieldLoc, diag::err_bitfield_has_negative_width) |
| Chris Lattner | f9b00eb | 2009-04-20 17:29:38 +0000 | [diff] [blame] | 11309 | << FieldName << Value.toString(10); |
| 11310 | return Diag(FieldLoc, diag::err_anon_bitfield_has_negative_width) |
| 11311 | << Value.toString(10); |
| 11312 | } |
| Anders Carlsson | 5df391e | 2008-12-06 20:33:04 +0000 | [diff] [blame] | 11313 | |
| Douglas Gregor | 1efa437 | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 11314 | if (!FieldTy->isDependentType()) { |
| 11315 | uint64_t TypeSize = Context.getTypeSize(FieldTy); |
| Chris Lattner | f9b00eb | 2009-04-20 17:29:38 +0000 | [diff] [blame] | 11316 | if (Value.getZExtValue() > TypeSize) { |
| Warren Hunt | 96afec1 | 2013-12-12 23:23:28 +0000 | [diff] [blame] | 11317 | if (!getLangOpts().CPlusPlus || IsMsStruct || |
| 11318 | Context.getTargetInfo().getCXXABI().isMicrosoft()) { |
| Anders Carlsson | d5635fe | 2010-04-16 15:16:32 +0000 | [diff] [blame] | 11319 | if (FieldName) |
| 11320 | return Diag(FieldLoc, diag::err_bitfield_width_exceeds_type_size) |
| 11321 | << FieldName << (unsigned)Value.getZExtValue() |
| 11322 | << (unsigned)TypeSize; |
| 11323 | |
| 11324 | return Diag(FieldLoc, diag::err_anon_bitfield_width_exceeds_type_size) |
| 11325 | << (unsigned)Value.getZExtValue() << (unsigned)TypeSize; |
| 11326 | } |
| 11327 | |
| Chris Lattner | f9b00eb | 2009-04-20 17:29:38 +0000 | [diff] [blame] | 11328 | if (FieldName) |
| Anders Carlsson | d5635fe | 2010-04-16 15:16:32 +0000 | [diff] [blame] | 11329 | Diag(FieldLoc, diag::warn_bitfield_width_exceeds_type_size) |
| 11330 | << FieldName << (unsigned)Value.getZExtValue() |
| 11331 | << (unsigned)TypeSize; |
| 11332 | else |
| 11333 | Diag(FieldLoc, diag::warn_anon_bitfield_width_exceeds_type_size) |
| 11334 | << (unsigned)Value.getZExtValue() << (unsigned)TypeSize; |
| Chris Lattner | f9b00eb | 2009-04-20 17:29:38 +0000 | [diff] [blame] | 11335 | } |
| Douglas Gregor | 1efa437 | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 11336 | } |
| Anders Carlsson | 5df391e | 2008-12-06 20:33:04 +0000 | [diff] [blame] | 11337 | |
| Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 11338 | return Owned(BitWidth); |
| Anders Carlsson | 5df391e | 2008-12-06 20:33:04 +0000 | [diff] [blame] | 11339 | } |
| 11340 | |
| Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 11341 | /// ActOnField - Each field of a C struct/union is passed into this in order |
| Chris Lattner | 1300fb9 | 2007-01-23 23:42:53 +0000 | [diff] [blame] | 11342 | /// to create a FieldDecl object for it. |
| Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 11343 | Decl *Sema::ActOnField(Scope *S, Decl *TagD, SourceLocation DeclStart, |
| Richard Trieu | 2bd0401 | 2011-09-09 02:00:50 +0000 | [diff] [blame] | 11344 | Declarator &D, Expr *BitfieldWidth) { |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11345 | FieldDecl *Res = HandleField(S, cast_or_null<RecordDecl>(TagD), |
| Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 11346 | DeclStart, D, static_cast<Expr*>(BitfieldWidth), |
| Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 11347 | /*InitStyle=*/ICIS_NoInit, AS_public); |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11348 | return Res; |
| Chris Lattner | 73bf7b4 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 11349 | } |
| 11350 | |
| 11351 | /// HandleField - Analyze a field of a C struct or a C++ data member. |
| 11352 | /// |
| 11353 | FieldDecl *Sema::HandleField(Scope *S, RecordDecl *Record, |
| 11354 | SourceLocation DeclStart, |
| Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 11355 | Declarator &D, Expr *BitWidth, |
| 11356 | InClassInitStyle InitStyle, |
| Douglas Gregor | 4261e4c | 2009-03-11 20:50:30 +0000 | [diff] [blame] | 11357 | AccessSpecifier AS) { |
| Chris Lattner | 1300fb9 | 2007-01-23 23:42:53 +0000 | [diff] [blame] | 11358 | IdentifierInfo *II = D.getIdentifier(); |
| Chris Lattner | 1300fb9 | 2007-01-23 23:42:53 +0000 | [diff] [blame] | 11359 | SourceLocation Loc = DeclStart; |
| 11360 | if (II) Loc = D.getIdentifierLoc(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11361 | |
| John McCall | 8cb7bdf | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 11362 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
| 11363 | QualType T = TInfo->getType(); |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 11364 | if (getLangOpts().CPlusPlus) { |
| Douglas Gregor | 1efa437 | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 11365 | CheckExtraCXXDefaultArguments(D); |
| Douglas Gregor | 0c88030 | 2009-03-11 23:00:04 +0000 | [diff] [blame] | 11366 | |
| Douglas Gregor | a02a72a | 2010-12-15 23:18:36 +0000 | [diff] [blame] | 11367 | if (DiagnoseUnexpandedParameterPack(D.getIdentifierLoc(), TInfo, |
| 11368 | UPPC_DataMemberType)) { |
| 11369 | D.setInvalidType(); |
| 11370 | T = Context.IntTy; |
| 11371 | TInfo = Context.getTrivialTypeSourceInfo(T, Loc); |
| 11372 | } |
| 11373 | } |
| 11374 | |
| Matt Arsenault | 376f720 | 2013-02-26 21:16:00 +0000 | [diff] [blame] | 11375 | // TR 18037 does not allow fields to be declared with address spaces. |
| 11376 | if (T.getQualifiers().hasAddressSpace()) { |
| 11377 | Diag(Loc, diag::err_field_with_address_space); |
| 11378 | D.setInvalidType(); |
| 11379 | } |
| 11380 | |
| Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 11381 | // OpenCL 1.2 spec, s6.9 r: |
| 11382 | // The event type cannot be used to declare a structure or union field. |
| 11383 | if (LangOpts.OpenCL && T->isEventT()) { |
| 11384 | Diag(Loc, diag::err_event_t_struct_field); |
| 11385 | D.setInvalidType(); |
| 11386 | } |
| 11387 | |
| Richard Smith | b1402ae | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 11388 | DiagnoseFunctionSpecifiers(D.getDeclSpec()); |
| Eli Friedman | 574c745 | 2009-04-07 19:37:57 +0000 | [diff] [blame] | 11389 | |
| Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 11390 | if (DeclSpec::TSCS TSCS = D.getDeclSpec().getThreadStorageClassSpec()) |
| 11391 | Diag(D.getDeclSpec().getThreadStorageClassSpecLoc(), |
| 11392 | diag::err_invalid_thread) |
| 11393 | << DeclSpec::getSpecifierName(TSCS); |
| Matt Arsenault | 376f720 | 2013-02-26 21:16:00 +0000 | [diff] [blame] | 11394 | |
| Douglas Gregor | 2c7d929 | 2010-08-30 14:32:14 +0000 | [diff] [blame] | 11395 | // Check to see if this name was declared as a member previously |
| Douglas Gregor | fbf8752 | 2011-10-21 15:47:52 +0000 | [diff] [blame] | 11396 | NamedDecl *PrevDecl = 0; |
| Douglas Gregor | 2c7d929 | 2010-08-30 14:32:14 +0000 | [diff] [blame] | 11397 | LookupResult Previous(*this, II, Loc, LookupMemberName, ForRedeclaration); |
| 11398 | LookupName(Previous, S); |
| Douglas Gregor | fbf8752 | 2011-10-21 15:47:52 +0000 | [diff] [blame] | 11399 | switch (Previous.getResultKind()) { |
| 11400 | case LookupResult::Found: |
| 11401 | case LookupResult::FoundUnresolvedValue: |
| 11402 | PrevDecl = Previous.getAsSingle<NamedDecl>(); |
| 11403 | break; |
| 11404 | |
| 11405 | case LookupResult::FoundOverloaded: |
| 11406 | PrevDecl = Previous.getRepresentativeDecl(); |
| 11407 | break; |
| 11408 | |
| 11409 | case LookupResult::NotFound: |
| 11410 | case LookupResult::NotFoundInCurrentInstantiation: |
| 11411 | case LookupResult::Ambiguous: |
| 11412 | break; |
| 11413 | } |
| 11414 | Previous.suppressDiagnostics(); |
| Douglas Gregor | f187420f | 2009-06-17 23:37:01 +0000 | [diff] [blame] | 11415 | |
| 11416 | if (PrevDecl && PrevDecl->isTemplateParameter()) { |
| 11417 | // Maybe we will complain about the shadowed template parameter. |
| 11418 | DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), PrevDecl); |
| 11419 | // Just pretend that we didn't see the previous declaration. |
| 11420 | PrevDecl = 0; |
| 11421 | } |
| 11422 | |
| Douglas Gregor | 1efa437 | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 11423 | if (PrevDecl && !isDeclInScope(PrevDecl, Record, S)) |
| 11424 | PrevDecl = 0; |
| 11425 | |
| Steve Naroff | 5ec6ff7 | 2009-07-14 14:58:18 +0000 | [diff] [blame] | 11426 | bool Mutable |
| 11427 | = (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_mutable); |
| Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 11428 | SourceLocation TSSL = D.getLocStart(); |
| Steve Naroff | 5ec6ff7 | 2009-07-14 14:58:18 +0000 | [diff] [blame] | 11429 | FieldDecl *NewFD |
| Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 11430 | = CheckFieldDecl(II, T, TInfo, Record, Loc, Mutable, BitWidth, InitStyle, |
| Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 11431 | TSSL, AS, PrevDecl, &D); |
| Rafael Espindola | 568586f | 2010-03-21 22:56:43 +0000 | [diff] [blame] | 11432 | |
| 11433 | if (NewFD->isInvalidDecl()) |
| 11434 | Record->setInvalidDecl(); |
| 11435 | |
| Douglas Gregor | 3baa670 | 2011-09-12 16:11:24 +0000 | [diff] [blame] | 11436 | if (D.getDeclSpec().isModulePrivateSpecified()) |
| 11437 | NewFD->setModulePrivate(); |
| 11438 | |
| Douglas Gregor | 1efa437 | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 11439 | if (NewFD->isInvalidDecl() && PrevDecl) { |
| 11440 | // Don't introduce NewFD into scope; there's already something |
| 11441 | // with the same name in the same scope. |
| 11442 | } else if (II) { |
| 11443 | PushOnScopeChains(NewFD, S); |
| 11444 | } else |
| Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 11445 | Record->addDecl(NewFD); |
| Douglas Gregor | 1efa437 | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 11446 | |
| 11447 | return NewFD; |
| 11448 | } |
| 11449 | |
| 11450 | /// \brief Build a new FieldDecl and check its well-formedness. |
| 11451 | /// |
| 11452 | /// This routine builds a new FieldDecl given the fields name, type, |
| 11453 | /// record, etc. \p PrevDecl should refer to any previous declaration |
| 11454 | /// with the same name and in the same scope as the field to be |
| 11455 | /// created. |
| 11456 | /// |
| 11457 | /// \returns a new FieldDecl. |
| 11458 | /// |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11459 | /// \todo The Declarator argument is a hack. It will be removed once |
| Argyrios Kyrtzidis | 60ed560 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 11460 | FieldDecl *Sema::CheckFieldDecl(DeclarationName Name, QualType T, |
| John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 11461 | TypeSourceInfo *TInfo, |
| Douglas Gregor | 1efa437 | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 11462 | RecordDecl *Record, SourceLocation Loc, |
| Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 11463 | bool Mutable, Expr *BitWidth, |
| 11464 | InClassInitStyle InitStyle, |
| Steve Naroff | 5ec6ff7 | 2009-07-14 14:58:18 +0000 | [diff] [blame] | 11465 | SourceLocation TSSL, |
| Douglas Gregor | 4261e4c | 2009-03-11 20:50:30 +0000 | [diff] [blame] | 11466 | AccessSpecifier AS, NamedDecl *PrevDecl, |
| Douglas Gregor | 1efa437 | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 11467 | Declarator *D) { |
| 11468 | IdentifierInfo *II = Name.getAsIdentifierInfo(); |
| Steve Naroff | f93b672 | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 11469 | bool InvalidDecl = false; |
| Chris Lattner | f6d1c9c | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 11470 | if (D) InvalidDecl = D->isInvalidType(); |
| Sebastian Redl | baad4e7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 11471 | |
| Douglas Gregor | 1efa437 | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 11472 | // If we receive a broken type, recover by assuming 'int' and |
| 11473 | // marking this declaration as invalid. |
| 11474 | if (T.isNull()) { |
| 11475 | InvalidDecl = true; |
| 11476 | T = Context.IntTy; |
| 11477 | } |
| 11478 | |
| Eli Friedman | d0e8de2 | 2009-12-07 00:22:08 +0000 | [diff] [blame] | 11479 | QualType EltTy = Context.getBaseElementType(T); |
| Argyrios Kyrtzidis | 2559629 | 2012-03-09 20:10:30 +0000 | [diff] [blame] | 11480 | if (!EltTy->isDependentType()) { |
| 11481 | if (RequireCompleteType(Loc, EltTy, diag::err_field_incomplete)) { |
| 11482 | // Fields of incomplete type force their record to be invalid. |
| 11483 | Record->setInvalidDecl(); |
| 11484 | InvalidDecl = true; |
| 11485 | } else { |
| 11486 | NamedDecl *Def; |
| 11487 | EltTy->isIncompleteType(&Def); |
| 11488 | if (Def && Def->isInvalidDecl()) { |
| 11489 | Record->setInvalidDecl(); |
| 11490 | InvalidDecl = true; |
| 11491 | } |
| 11492 | } |
| John McCall | 2677e10 | 2010-08-16 23:42:35 +0000 | [diff] [blame] | 11493 | } |
| Eli Friedman | d0e8de2 | 2009-12-07 00:22:08 +0000 | [diff] [blame] | 11494 | |
| Joey Gouly | 1d58cdb | 2013-01-17 17:35:00 +0000 | [diff] [blame] | 11495 | // OpenCL v1.2 s6.9.c: bitfields are not supported. |
| 11496 | if (BitWidth && getLangOpts().OpenCL) { |
| 11497 | Diag(Loc, diag::err_opencl_bitfields); |
| 11498 | InvalidDecl = true; |
| 11499 | } |
| 11500 | |
| Steve Naroff | 8eeeb13 | 2007-05-08 21:09:37 +0000 | [diff] [blame] | 11501 | // C99 6.7.2.1p8: A member of a structure or union may have any type other |
| 11502 | // than a variably modified type. |
| Eli Friedman | d0e8de2 | 2009-12-07 00:22:08 +0000 | [diff] [blame] | 11503 | if (!InvalidDecl && T->isVariablyModifiedType()) { |
| Eli Friedman | a3b1d03 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 11504 | bool SizeIsNegative; |
| Douglas Gregor | caa1bf4 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 11505 | llvm::APSInt Oversized; |
| Abramo Bagnara | 341ab73 | 2012-11-08 14:44:42 +0000 | [diff] [blame] | 11506 | |
| 11507 | TypeSourceInfo *FixedTInfo = |
| 11508 | TryToFixInvalidVariablyModifiedTypeSourceInfo(TInfo, Context, |
| 11509 | SizeIsNegative, |
| 11510 | Oversized); |
| 11511 | if (FixedTInfo) { |
| Eli Friedman | a3b1d03 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 11512 | Diag(Loc, diag::warn_illegal_constant_array_size); |
| Abramo Bagnara | 341ab73 | 2012-11-08 14:44:42 +0000 | [diff] [blame] | 11513 | TInfo = FixedTInfo; |
| 11514 | T = FixedTInfo->getType(); |
| Eli Friedman | a3b1d03 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 11515 | } else { |
| 11516 | if (SizeIsNegative) |
| 11517 | Diag(Loc, diag::err_typecheck_negative_array_size); |
| Douglas Gregor | caa1bf4 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 11518 | else if (Oversized.getBoolValue()) |
| 11519 | Diag(Loc, diag::err_array_too_large) |
| 11520 | << Oversized.toString(10); |
| Eli Friedman | a3b1d03 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 11521 | else |
| 11522 | Diag(Loc, diag::err_typecheck_field_variable_size); |
| Eli Friedman | a3b1d03 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 11523 | InvalidDecl = true; |
| 11524 | } |
| Steve Naroff | 8eeeb13 | 2007-05-08 21:09:37 +0000 | [diff] [blame] | 11525 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11526 | |
| Anders Carlsson | 576cc6f | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 11527 | // Fields can not have abstract class types |
| Eli Friedman | d0e8de2 | 2009-12-07 00:22:08 +0000 | [diff] [blame] | 11528 | if (!InvalidDecl && RequireNonAbstractType(Loc, T, |
| 11529 | diag::err_abstract_type_in_decl, |
| 11530 | AbstractFieldType)) |
| Anders Carlsson | 576cc6f | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 11531 | InvalidDecl = true; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11532 | |
| Eli Friedman | c96d496 | 2009-08-15 21:55:26 +0000 | [diff] [blame] | 11533 | bool ZeroWidth = false; |
| Douglas Gregor | 1efa437 | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 11534 | // If this is declared as a bit-field, check the bit-field. |
| Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 11535 | if (!InvalidDecl && BitWidth) { |
| Reid Kleckner | 736bc98 | 2013-07-17 20:46:03 +0000 | [diff] [blame] | 11536 | BitWidth = VerifyBitField(Loc, II, T, Record->isMsStruct(Context), BitWidth, |
| 11537 | &ZeroWidth).take(); |
| Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 11538 | if (!BitWidth) { |
| 11539 | InvalidDecl = true; |
| 11540 | BitWidth = 0; |
| 11541 | ZeroWidth = false; |
| 11542 | } |
| Anders Carlsson | 5df391e | 2008-12-06 20:33:04 +0000 | [diff] [blame] | 11543 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11544 | |
| John McCall | b1cd7da | 2010-06-04 08:34:12 +0000 | [diff] [blame] | 11545 | // Check that 'mutable' is consistent with the type of the declaration. |
| 11546 | if (!InvalidDecl && Mutable) { |
| 11547 | unsigned DiagID = 0; |
| 11548 | if (T->isReferenceType()) |
| 11549 | DiagID = diag::err_mutable_reference; |
| 11550 | else if (T.isConstQualified()) |
| 11551 | DiagID = diag::err_mutable_const; |
| 11552 | |
| 11553 | if (DiagID) { |
| 11554 | SourceLocation ErrLoc = Loc; |
| 11555 | if (D && D->getDeclSpec().getStorageClassSpecLoc().isValid()) |
| 11556 | ErrLoc = D->getDeclSpec().getStorageClassSpecLoc(); |
| 11557 | Diag(ErrLoc, DiagID); |
| 11558 | Mutable = false; |
| 11559 | InvalidDecl = true; |
| 11560 | } |
| 11561 | } |
| 11562 | |
| Richard Smith | ab44d5b | 2013-12-10 08:25:00 +0000 | [diff] [blame] | 11563 | // C++11 [class.union]p8 (DR1460): |
| 11564 | // At most one variant member of a union may have a |
| 11565 | // brace-or-equal-initializer. |
| 11566 | if (InitStyle != ICIS_NoInit) |
| 11567 | checkDuplicateDefaultInit(*this, cast<CXXRecordDecl>(Record), Loc); |
| 11568 | |
| Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 11569 | FieldDecl *NewFD = FieldDecl::Create(Context, Record, TSSL, Loc, II, T, TInfo, |
| Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 11570 | BitWidth, Mutable, InitStyle); |
| Chris Lattner | f6d1c9c | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 11571 | if (InvalidDecl) |
| 11572 | NewFD->setInvalidDecl(); |
| Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 11573 | |
| Douglas Gregor | 1efa437 | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 11574 | if (PrevDecl && !isa<TagDecl>(PrevDecl)) { |
| 11575 | Diag(Loc, diag::err_duplicate_member) << II; |
| 11576 | Diag(PrevDecl->getLocation(), diag::note_previous_declaration); |
| 11577 | NewFD->setInvalidDecl(); |
| Douglas Gregor | 82ac25e | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 11578 | } |
| 11579 | |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 11580 | if (!InvalidDecl && getLangOpts().CPlusPlus) { |
| Anders Carlsson | 2ceb347 | 2010-11-07 19:13:55 +0000 | [diff] [blame] | 11581 | if (Record->isUnion()) { |
| 11582 | if (const RecordType *RT = EltTy->getAs<RecordType>()) { |
| 11583 | CXXRecordDecl* RDecl = cast<CXXRecordDecl>(RT->getDecl()); |
| 11584 | if (RDecl->getDefinition()) { |
| 11585 | // C++ [class.union]p1: An object of a class with a non-trivial |
| 11586 | // constructor, a non-trivial copy constructor, a non-trivial |
| 11587 | // destructor, or a non-trivial copy assignment operator |
| 11588 | // cannot be a member of a union, nor can an array of such |
| 11589 | // objects. |
| Richard Smith | f720df0 | 2011-10-19 20:41:51 +0000 | [diff] [blame] | 11590 | if (CheckNontrivialField(NewFD)) |
| Anders Carlsson | 2ceb347 | 2010-11-07 19:13:55 +0000 | [diff] [blame] | 11591 | NewFD->setInvalidDecl(); |
| 11592 | } |
| 11593 | } |
| 11594 | |
| 11595 | // C++ [class.union]p1: If a union contains a member of reference type, |
| Aaron Ballman | ed0ae1d | 2013-05-30 16:20:00 +0000 | [diff] [blame] | 11596 | // the program is ill-formed, except when compiling with MSVC extensions |
| 11597 | // enabled. |
| Anders Carlsson | 2ceb347 | 2010-11-07 19:13:55 +0000 | [diff] [blame] | 11598 | if (EltTy->isReferenceType()) { |
| Aaron Ballman | ed0ae1d | 2013-05-30 16:20:00 +0000 | [diff] [blame] | 11599 | Diag(NewFD->getLocation(), getLangOpts().MicrosoftExt ? |
| 11600 | diag::ext_union_member_of_reference_type : |
| 11601 | diag::err_union_member_of_reference_type) |
| Anders Carlsson | 2ceb347 | 2010-11-07 19:13:55 +0000 | [diff] [blame] | 11602 | << NewFD->getDeclName() << EltTy; |
| Aaron Ballman | ed0ae1d | 2013-05-30 16:20:00 +0000 | [diff] [blame] | 11603 | if (!getLangOpts().MicrosoftExt) |
| 11604 | NewFD->setInvalidDecl(); |
| Douglas Gregor | 8a27391 | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 11605 | } |
| 11606 | } |
| 11607 | } |
| 11608 | |
| Douglas Gregor | 1efa437 | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 11609 | // FIXME: We need to pass in the attributes given an AST |
| 11610 | // representation, not a parser representation. |
| Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 11611 | if (D) { |
| Douglas Gregor | d2472d4 | 2013-05-02 23:25:32 +0000 | [diff] [blame] | 11612 | // FIXME: The current scope is almost... but not entirely... correct here. |
| 11613 | ProcessDeclAttributes(getCurScope(), NewFD, *D); |
| Douglas Gregor | 1efa437 | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 11614 | |
| Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 11615 | if (NewFD->hasAttrs()) |
| 11616 | CheckAlignasUnderalignment(NewFD); |
| 11617 | } |
| 11618 | |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 11619 | // In auto-retain/release, infer strong retension for fields of |
| 11620 | // retainable type. |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 11621 | if (getLangOpts().ObjCAutoRefCount && inferObjCARCLifetime(NewFD)) |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 11622 | NewFD->setInvalidDecl(); |
| 11623 | |
| Fariborz Jahanian | d29fecd | 2009-02-19 00:22:47 +0000 | [diff] [blame] | 11624 | if (T.isObjCGCWeak()) |
| Fariborz Jahanian | d35c792 | 2009-02-18 18:14:41 +0000 | [diff] [blame] | 11625 | Diag(Loc, diag::warn_attribute_weak_on_field); |
| Anders Carlsson | 28e7108 | 2008-02-16 00:29:18 +0000 | [diff] [blame] | 11626 | |
| Douglas Gregor | 4261e4c | 2009-03-11 20:50:30 +0000 | [diff] [blame] | 11627 | NewFD->setAccess(AS); |
| Steve Naroff | f93b672 | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 11628 | return NewFD; |
| Chris Lattner | 1300fb9 | 2007-01-23 23:42:53 +0000 | [diff] [blame] | 11629 | } |
| 11630 | |
| Argyrios Kyrtzidis | 33aee39 | 2010-08-16 17:27:08 +0000 | [diff] [blame] | 11631 | bool Sema::CheckNontrivialField(FieldDecl *FD) { |
| 11632 | assert(FD); |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 11633 | assert(getLangOpts().CPlusPlus && "valid check only for C++"); |
| Argyrios Kyrtzidis | 33aee39 | 2010-08-16 17:27:08 +0000 | [diff] [blame] | 11634 | |
| Nick Lewycky | 7a2a479 | 2013-06-25 23:22:23 +0000 | [diff] [blame] | 11635 | if (FD->isInvalidDecl() || FD->getType()->isDependentType()) |
| 11636 | return false; |
| Argyrios Kyrtzidis | 33aee39 | 2010-08-16 17:27:08 +0000 | [diff] [blame] | 11637 | |
| 11638 | QualType EltTy = Context.getBaseElementType(FD->getType()); |
| 11639 | if (const RecordType *RT = EltTy->getAs<RecordType>()) { |
| Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 11640 | CXXRecordDecl *RDecl = cast<CXXRecordDecl>(RT->getDecl()); |
| Argyrios Kyrtzidis | 33aee39 | 2010-08-16 17:27:08 +0000 | [diff] [blame] | 11641 | if (RDecl->getDefinition()) { |
| 11642 | // We check for copy constructors before constructors |
| 11643 | // because otherwise we'll never get complaints about |
| 11644 | // copy constructors. |
| 11645 | |
| 11646 | CXXSpecialMember member = CXXInvalid; |
| Richard Smith | 1648847 | 2012-11-16 00:53:38 +0000 | [diff] [blame] | 11647 | // We're required to check for any non-trivial constructors. Since the |
| 11648 | // implicit default constructor is suppressed if there are any |
| 11649 | // user-declared constructors, we just need to check that there is a |
| 11650 | // trivial default constructor and a trivial copy constructor. (We don't |
| 11651 | // worry about move constructors here, since this is a C++98 check.) |
| 11652 | if (RDecl->hasNonTrivialCopyConstructor()) |
| Argyrios Kyrtzidis | 33aee39 | 2010-08-16 17:27:08 +0000 | [diff] [blame] | 11653 | member = CXXCopyConstructor; |
| Alexis Hunt | f479f1b | 2011-05-09 18:22:59 +0000 | [diff] [blame] | 11654 | else if (!RDecl->hasTrivialDefaultConstructor()) |
| Alexis Hunt | 80f00ff | 2011-05-10 19:08:14 +0000 | [diff] [blame] | 11655 | member = CXXDefaultConstructor; |
| Richard Smith | 1648847 | 2012-11-16 00:53:38 +0000 | [diff] [blame] | 11656 | else if (RDecl->hasNonTrivialCopyAssignment()) |
| Argyrios Kyrtzidis | 33aee39 | 2010-08-16 17:27:08 +0000 | [diff] [blame] | 11657 | member = CXXCopyAssignment; |
| Richard Smith | 1648847 | 2012-11-16 00:53:38 +0000 | [diff] [blame] | 11658 | else if (RDecl->hasNonTrivialDestructor()) |
| Argyrios Kyrtzidis | 33aee39 | 2010-08-16 17:27:08 +0000 | [diff] [blame] | 11659 | member = CXXDestructor; |
| 11660 | |
| 11661 | if (member != CXXInvalid) { |
| Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 11662 | if (!getLangOpts().CPlusPlus11 && |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 11663 | getLangOpts().ObjCAutoRefCount && RDecl->hasObjectMember()) { |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 11664 | // Objective-C++ ARC: it is an error to have a non-trivial field of |
| 11665 | // a union. However, system headers in Objective-C programs |
| 11666 | // occasionally have Objective-C lifetime objects within unions, |
| 11667 | // and rather than cause the program to fail, we make those |
| 11668 | // members unavailable. |
| 11669 | SourceLocation Loc = FD->getLocation(); |
| 11670 | if (getSourceManager().isInSystemHeader(Loc)) { |
| 11671 | if (!FD->hasAttr<UnavailableAttr>()) |
| Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 11672 | FD->addAttr(UnavailableAttr::CreateImplicit(Context, |
| 11673 | "this system field has retaining ownership", |
| 11674 | Loc)); |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 11675 | return false; |
| 11676 | } |
| 11677 | } |
| Richard Smith | f720df0 | 2011-10-19 20:41:51 +0000 | [diff] [blame] | 11678 | |
| Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 11679 | Diag(FD->getLocation(), getLangOpts().CPlusPlus11 ? |
| Richard Smith | f720df0 | 2011-10-19 20:41:51 +0000 | [diff] [blame] | 11680 | diag::warn_cxx98_compat_nontrivial_union_or_anon_struct_member : |
| 11681 | diag::err_illegal_union_or_anon_struct_member) |
| 11682 | << (int)FD->getParent()->isUnion() << FD->getDeclName() << member; |
| Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 11683 | DiagnoseNontrivial(RDecl, member); |
| Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 11684 | return !getLangOpts().CPlusPlus11; |
| Argyrios Kyrtzidis | 33aee39 | 2010-08-16 17:27:08 +0000 | [diff] [blame] | 11685 | } |
| 11686 | } |
| 11687 | } |
| Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 11688 | |
| Argyrios Kyrtzidis | 33aee39 | 2010-08-16 17:27:08 +0000 | [diff] [blame] | 11689 | return false; |
| 11690 | } |
| 11691 | |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11692 | /// TranslateIvarVisibility - Translate visibility from a token ID to an |
| Fariborz Jahanian | f26702eb | 2007-10-01 16:53:59 +0000 | [diff] [blame] | 11693 | /// AST enum value. |
| Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 11694 | static ObjCIvarDecl::AccessControl |
| Fariborz Jahanian | f26702eb | 2007-10-01 16:53:59 +0000 | [diff] [blame] | 11695 | TranslateIvarVisibility(tok::ObjCKeywordKind ivarVisibility) { |
| Steve Naroff | 2e688fd | 2007-09-14 23:09:53 +0000 | [diff] [blame] | 11696 | switch (ivarVisibility) { |
| David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 11697 | default: llvm_unreachable("Unknown visitibility kind"); |
| Chris Lattner | 79ef843 | 2008-10-12 00:28:42 +0000 | [diff] [blame] | 11698 | case tok::objc_private: return ObjCIvarDecl::Private; |
| 11699 | case tok::objc_public: return ObjCIvarDecl::Public; |
| 11700 | case tok::objc_protected: return ObjCIvarDecl::Protected; |
| 11701 | case tok::objc_package: return ObjCIvarDecl::Package; |
| Steve Naroff | 2e688fd | 2007-09-14 23:09:53 +0000 | [diff] [blame] | 11702 | } |
| 11703 | } |
| 11704 | |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11705 | /// ActOnIvar - Each ivar field of an objective-c class is passed into this |
| Fariborz Jahanian | 9637674 | 2008-04-11 16:55:42 +0000 | [diff] [blame] | 11706 | /// in order to create an IvarDecl object for it. |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11707 | Decl *Sema::ActOnIvar(Scope *S, |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11708 | SourceLocation DeclStart, |
| Richard Trieu | 2bd0401 | 2011-09-09 02:00:50 +0000 | [diff] [blame] | 11709 | Declarator &D, Expr *BitfieldWidth, |
| Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 11710 | tok::ObjCKeywordKind Visibility) { |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11711 | |
| Fariborz Jahanian | de61583 | 2008-04-10 23:32:45 +0000 | [diff] [blame] | 11712 | IdentifierInfo *II = D.getIdentifier(); |
| 11713 | Expr *BitWidth = (Expr*)BitfieldWidth; |
| 11714 | SourceLocation Loc = DeclStart; |
| 11715 | if (II) Loc = D.getIdentifierLoc(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11716 | |
| Fariborz Jahanian | de61583 | 2008-04-10 23:32:45 +0000 | [diff] [blame] | 11717 | // FIXME: Unnamed fields can be handled in various different ways, for |
| 11718 | // example, unnamed unions inject all members into the struct namespace! |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11719 | |
| John McCall | 8cb7bdf | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 11720 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
| 11721 | QualType T = TInfo->getType(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11722 | |
| Fariborz Jahanian | de61583 | 2008-04-10 23:32:45 +0000 | [diff] [blame] | 11723 | if (BitWidth) { |
| Steve Naroff | 17b2f5d | 2009-02-20 17:57:11 +0000 | [diff] [blame] | 11724 | // 6.7.2.1p3, 6.7.2.1p4 |
| Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 11725 | BitWidth = VerifyBitField(Loc, II, T, /*IsMsStruct*/false, BitWidth).take(); |
| Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 11726 | if (!BitWidth) |
| Chris Lattner | f6d1c9c | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 11727 | D.setInvalidType(); |
| Fariborz Jahanian | de61583 | 2008-04-10 23:32:45 +0000 | [diff] [blame] | 11728 | } else { |
| 11729 | // Not a bitfield. |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11730 | |
| Fariborz Jahanian | de61583 | 2008-04-10 23:32:45 +0000 | [diff] [blame] | 11731 | // validate II. |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11732 | |
| Fariborz Jahanian | de61583 | 2008-04-10 23:32:45 +0000 | [diff] [blame] | 11733 | } |
| Fariborz Jahanian | 0103d67 | 2010-04-26 22:07:03 +0000 | [diff] [blame] | 11734 | if (T->isReferenceType()) { |
| 11735 | Diag(Loc, diag::err_ivar_reference_type); |
| 11736 | D.setInvalidType(); |
| 11737 | } |
| Fariborz Jahanian | de61583 | 2008-04-10 23:32:45 +0000 | [diff] [blame] | 11738 | // C99 6.7.2.1p8: A member of a structure or union may have any type other |
| 11739 | // than a variably modified type. |
| Fariborz Jahanian | 0103d67 | 2010-04-26 22:07:03 +0000 | [diff] [blame] | 11740 | else if (T->isVariablyModifiedType()) { |
| Anders Carlsson | 0d8f0ba | 2008-12-07 00:20:55 +0000 | [diff] [blame] | 11741 | Diag(Loc, diag::err_typecheck_ivar_variable_size); |
| Chris Lattner | f6d1c9c | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 11742 | D.setInvalidType(); |
| Fariborz Jahanian | de61583 | 2008-04-10 23:32:45 +0000 | [diff] [blame] | 11743 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11744 | |
| Ted Kremenek | 73295fa | 2008-07-23 18:04:17 +0000 | [diff] [blame] | 11745 | // Get the visibility (access control) for this ivar. |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11746 | ObjCIvarDecl::AccessControl ac = |
| Ted Kremenek | 73295fa | 2008-07-23 18:04:17 +0000 | [diff] [blame] | 11747 | Visibility != tok::objc_not_keyword ? TranslateIvarVisibility(Visibility) |
| 11748 | : ObjCIvarDecl::None; |
| Fariborz Jahanian | 6845383 | 2009-06-05 18:16:35 +0000 | [diff] [blame] | 11749 | // Must set ivar's DeclContext to its enclosing interface. |
| Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 11750 | ObjCContainerDecl *EnclosingDecl = cast<ObjCContainerDecl>(CurContext); |
| Fariborz Jahanian | 17612b1 | 2012-02-02 00:49:12 +0000 | [diff] [blame] | 11751 | if (!EnclosingDecl || EnclosingDecl->isInvalidDecl()) |
| 11752 | return 0; |
| Daniel Dunbar | 229385c | 2010-04-02 18:29:09 +0000 | [diff] [blame] | 11753 | ObjCContainerDecl *EnclosingContext; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11754 | if (ObjCImplementationDecl *IMPDecl = |
| Fariborz Jahanian | 6845383 | 2009-06-05 18:16:35 +0000 | [diff] [blame] | 11755 | dyn_cast<ObjCImplementationDecl>(EnclosingDecl)) { |
| John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 11756 | if (LangOpts.ObjCRuntime.isFragile()) { |
| Fariborz Jahanian | 6845383 | 2009-06-05 18:16:35 +0000 | [diff] [blame] | 11757 | // Case of ivar declared in an implementation. Context is that of its class. |
| Fariborz Jahanian | bf9294f | 2010-08-23 18:51:39 +0000 | [diff] [blame] | 11758 | EnclosingContext = IMPDecl->getClassInterface(); |
| 11759 | assert(EnclosingContext && "Implementation has no class interface!"); |
| 11760 | } |
| 11761 | else |
| 11762 | EnclosingContext = EnclosingDecl; |
| Fariborz Jahanian | 6a0a2e0 | 2010-04-06 22:43:48 +0000 | [diff] [blame] | 11763 | } else { |
| 11764 | if (ObjCCategoryDecl *CDecl = |
| 11765 | dyn_cast<ObjCCategoryDecl>(EnclosingDecl)) { |
| John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 11766 | if (LangOpts.ObjCRuntime.isFragile() || !CDecl->IsClassExtension()) { |
| Fariborz Jahanian | 6a0a2e0 | 2010-04-06 22:43:48 +0000 | [diff] [blame] | 11767 | Diag(Loc, diag::err_misplaced_ivar) << CDecl->IsClassExtension(); |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11768 | return 0; |
| Fariborz Jahanian | 6a0a2e0 | 2010-04-06 22:43:48 +0000 | [diff] [blame] | 11769 | } |
| 11770 | } |
| Daniel Dunbar | 229385c | 2010-04-02 18:29:09 +0000 | [diff] [blame] | 11771 | EnclosingContext = EnclosingDecl; |
| Fariborz Jahanian | 6a0a2e0 | 2010-04-06 22:43:48 +0000 | [diff] [blame] | 11772 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11773 | |
| Ted Kremenek | 73295fa | 2008-07-23 18:04:17 +0000 | [diff] [blame] | 11774 | // Construct the decl. |
| Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 11775 | ObjCIvarDecl *NewID = ObjCIvarDecl::Create(Context, EnclosingContext, |
| 11776 | DeclStart, Loc, II, T, |
| John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 11777 | TInfo, ac, (Expr *)BitfieldWidth); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11778 | |
| Douglas Gregor | 82ac25e | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 11779 | if (II) { |
| Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 11780 | NamedDecl *PrevDecl = LookupSingleName(S, II, Loc, LookupMemberName, |
| John McCall | 5cebab1 | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 11781 | ForRedeclaration); |
| Fariborz Jahanian | 6845383 | 2009-06-05 18:16:35 +0000 | [diff] [blame] | 11782 | if (PrevDecl && isDeclInScope(PrevDecl, EnclosingContext, S) |
| Douglas Gregor | 82ac25e | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 11783 | && !isa<TagDecl>(PrevDecl)) { |
| 11784 | Diag(Loc, diag::err_duplicate_member) << II; |
| 11785 | Diag(PrevDecl->getLocation(), diag::note_previous_declaration); |
| 11786 | NewID->setInvalidDecl(); |
| 11787 | } |
| 11788 | } |
| 11789 | |
| Ted Kremenek | 73295fa | 2008-07-23 18:04:17 +0000 | [diff] [blame] | 11790 | // Process attributes attached to the ivar. |
| Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 11791 | ProcessDeclAttributes(S, NewID, D); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11792 | |
| Chris Lattner | f6d1c9c | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 11793 | if (D.isInvalidType()) |
| Fariborz Jahanian | de61583 | 2008-04-10 23:32:45 +0000 | [diff] [blame] | 11794 | NewID->setInvalidDecl(); |
| Ted Kremenek | 73295fa | 2008-07-23 18:04:17 +0000 | [diff] [blame] | 11795 | |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 11796 | // In ARC, infer 'retaining' for ivars of retainable type. |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 11797 | if (getLangOpts().ObjCAutoRefCount && inferObjCARCLifetime(NewID)) |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 11798 | NewID->setInvalidDecl(); |
| 11799 | |
| Douglas Gregor | 3baa670 | 2011-09-12 16:11:24 +0000 | [diff] [blame] | 11800 | if (D.getDeclSpec().isModulePrivateSpecified()) |
| 11801 | NewID->setModulePrivate(); |
| 11802 | |
| Douglas Gregor | 82ac25e | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 11803 | if (II) { |
| 11804 | // FIXME: When interfaces are DeclContexts, we'll need to add |
| 11805 | // these to the interface. |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11806 | S->AddDecl(NewID); |
| Douglas Gregor | 82ac25e | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 11807 | IdResolver.AddDecl(NewID); |
| 11808 | } |
| Fariborz Jahanian | 80297b1 | 2012-05-15 16:33:04 +0000 | [diff] [blame] | 11809 | |
| John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 11810 | if (LangOpts.ObjCRuntime.isNonFragile() && |
| Fariborz Jahanian | 80297b1 | 2012-05-15 16:33:04 +0000 | [diff] [blame] | 11811 | !NewID->isInvalidDecl() && isa<ObjCInterfaceDecl>(EnclosingDecl)) |
| Fariborz Jahanian | e1ada58 | 2012-05-15 17:43:16 +0000 | [diff] [blame] | 11812 | Diag(Loc, diag::warn_ivars_in_interface); |
| Fariborz Jahanian | 80297b1 | 2012-05-15 16:33:04 +0000 | [diff] [blame] | 11813 | |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11814 | return NewID; |
| Fariborz Jahanian | de61583 | 2008-04-10 23:32:45 +0000 | [diff] [blame] | 11815 | } |
| 11816 | |
| Fariborz Jahanian | 616d3e7 | 2010-08-23 22:46:52 +0000 | [diff] [blame] | 11817 | /// ActOnLastBitfield - This routine handles synthesized bitfields rules for |
| Jordan Rose | a0e9d39 | 2013-04-03 01:39:23 +0000 | [diff] [blame] | 11818 | /// class and class extensions. For every class \@interface and class |
| 11819 | /// extension \@interface, if the last ivar is a bitfield of any type, |
| Fariborz Jahanian | 616d3e7 | 2010-08-23 22:46:52 +0000 | [diff] [blame] | 11820 | /// then add an implicit `char :0` ivar to the end of that interface. |
| Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 11821 | void Sema::ActOnLastBitfield(SourceLocation DeclLoc, |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 11822 | SmallVectorImpl<Decl *> &AllIvarDecls) { |
| John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 11823 | if (LangOpts.ObjCRuntime.isFragile() || AllIvarDecls.empty()) |
| Fariborz Jahanian | 616d3e7 | 2010-08-23 22:46:52 +0000 | [diff] [blame] | 11824 | return; |
| 11825 | |
| 11826 | Decl *ivarDecl = AllIvarDecls[AllIvarDecls.size()-1]; |
| 11827 | ObjCIvarDecl *Ivar = cast<ObjCIvarDecl>(ivarDecl); |
| 11828 | |
| Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 11829 | if (!Ivar->isBitField() || Ivar->getBitWidthValue(Context) == 0) |
| Fariborz Jahanian | 616d3e7 | 2010-08-23 22:46:52 +0000 | [diff] [blame] | 11830 | return; |
| Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 11831 | ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CurContext); |
| Fariborz Jahanian | 616d3e7 | 2010-08-23 22:46:52 +0000 | [diff] [blame] | 11832 | if (!ID) { |
| Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 11833 | if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(CurContext)) { |
| Fariborz Jahanian | 616d3e7 | 2010-08-23 22:46:52 +0000 | [diff] [blame] | 11834 | if (!CD->IsClassExtension()) |
| 11835 | return; |
| 11836 | } |
| 11837 | // No need to add this to end of @implementation. |
| 11838 | else |
| 11839 | return; |
| 11840 | } |
| 11841 | // All conditions are met. Add a new bitfield to the tail end of ivars. |
| Douglas Gregor | 1d39480 | 2011-08-03 16:26:46 +0000 | [diff] [blame] | 11842 | llvm::APInt Zero(Context.getTypeSize(Context.IntTy), 0); |
| 11843 | Expr * BW = IntegerLiteral::Create(Context, Zero, Context.IntTy, DeclLoc); |
| Fariborz Jahanian | 616d3e7 | 2010-08-23 22:46:52 +0000 | [diff] [blame] | 11844 | |
| Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 11845 | Ivar = ObjCIvarDecl::Create(Context, cast<ObjCContainerDecl>(CurContext), |
| Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 11846 | DeclLoc, DeclLoc, 0, |
| Fariborz Jahanian | 616d3e7 | 2010-08-23 22:46:52 +0000 | [diff] [blame] | 11847 | Context.CharTy, |
| Douglas Gregor | 1d39480 | 2011-08-03 16:26:46 +0000 | [diff] [blame] | 11848 | Context.getTrivialTypeSourceInfo(Context.CharTy, |
| 11849 | DeclLoc), |
| Fariborz Jahanian | 616d3e7 | 2010-08-23 22:46:52 +0000 | [diff] [blame] | 11850 | ObjCIvarDecl::Private, BW, |
| 11851 | true); |
| 11852 | AllIvarDecls.push_back(Ivar); |
| 11853 | } |
| 11854 | |
| Robert Wilhelm | 16e94b9 | 2013-08-09 18:02:13 +0000 | [diff] [blame] | 11855 | void Sema::ActOnFields(Scope *S, SourceLocation RecLoc, Decl *EnclosingDecl, |
| 11856 | ArrayRef<Decl *> Fields, SourceLocation LBrac, |
| 11857 | SourceLocation RBrac, AttributeList *Attr) { |
| Steve Naroff | db47ee2 | 2007-09-14 22:20:54 +0000 | [diff] [blame] | 11858 | assert(EnclosingDecl && "missing record or interface decl"); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11859 | |
| Eric Christopher | 7457aaf | 2012-07-19 22:22:51 +0000 | [diff] [blame] | 11860 | // If this is an Objective-C @implementation or category and we have |
| 11861 | // new fields here we should reset the layout of the interface since |
| 11862 | // it will now change. |
| 11863 | if (!Fields.empty() && isa<ObjCContainerDecl>(EnclosingDecl)) { |
| 11864 | ObjCContainerDecl *DC = cast<ObjCContainerDecl>(EnclosingDecl); |
| 11865 | switch (DC->getKind()) { |
| 11866 | default: break; |
| 11867 | case Decl::ObjCCategory: |
| 11868 | Context.ResetObjCLayout(cast<ObjCCategoryDecl>(DC)->getClassInterface()); |
| 11869 | break; |
| 11870 | case Decl::ObjCImplementation: |
| 11871 | Context. |
| 11872 | ResetObjCLayout(cast<ObjCImplementationDecl>(DC)->getClassInterface()); |
| 11873 | break; |
| 11874 | } |
| 11875 | } |
| 11876 | |
| Eli Friedman | a767941 | 2012-02-07 05:00:47 +0000 | [diff] [blame] | 11877 | RecordDecl *Record = dyn_cast<RecordDecl>(EnclosingDecl); |
| 11878 | |
| 11879 | // Start counting up the number of named members; make sure to include |
| 11880 | // members of anonymous structs and unions in the total. |
| Chris Lattner | 8262560 | 2007-01-24 02:26:21 +0000 | [diff] [blame] | 11881 | unsigned NumNamedMembers = 0; |
| Eli Friedman | a767941 | 2012-02-07 05:00:47 +0000 | [diff] [blame] | 11882 | if (Record) { |
| 11883 | for (RecordDecl::decl_iterator i = Record->decls_begin(), |
| 11884 | e = Record->decls_end(); i != e; i++) { |
| 11885 | if (IndirectFieldDecl *IFD = dyn_cast<IndirectFieldDecl>(*i)) |
| 11886 | if (IFD->getDeclName()) |
| 11887 | ++NumNamedMembers; |
| 11888 | } |
| 11889 | } |
| 11890 | |
| 11891 | // Verify that all the fields are okay. |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 11892 | SmallVector<FieldDecl*, 32> RecFields; |
| Douglas Gregor | f4d3327 | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 11893 | |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 11894 | bool ARCErrReported = false; |
| Robert Wilhelm | 16e94b9 | 2013-08-09 18:02:13 +0000 | [diff] [blame] | 11895 | for (ArrayRef<Decl *>::iterator i = Fields.begin(), end = Fields.end(); |
| David Blaikie | 751c558 | 2011-09-22 02:58:26 +0000 | [diff] [blame] | 11896 | i != end; ++i) { |
| 11897 | FieldDecl *FD = cast<FieldDecl>(*i); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11898 | |
| Chris Lattner | 720a054 | 2007-01-25 00:44:24 +0000 | [diff] [blame] | 11899 | // Get the type for the field. |
| John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 11900 | const Type *FDTy = FD->getType().getTypePtr(); |
| Douglas Gregor | f4d3327 | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 11901 | |
| Douglas Gregor | 82ac25e | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 11902 | if (!FD->isAnonymousStructOrUnion()) { |
| Douglas Gregor | f4d3327 | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 11903 | // Remember all fields written by the user. |
| 11904 | RecFields.push_back(FD); |
| 11905 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11906 | |
| Chris Lattner | 73bf7b4 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 11907 | // If the field is already invalid for some reason, don't emit more |
| 11908 | // diagnostics about it. |
| Eli Friedman | d0e8de2 | 2009-12-07 00:22:08 +0000 | [diff] [blame] | 11909 | if (FD->isInvalidDecl()) { |
| 11910 | EnclosingDecl->setInvalidDecl(); |
| Chris Lattner | 73bf7b4 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 11911 | continue; |
| Eli Friedman | d0e8de2 | 2009-12-07 00:22:08 +0000 | [diff] [blame] | 11912 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11913 | |
| Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 11914 | // C99 6.7.2.1p2: |
| 11915 | // A structure or union shall not contain a member with |
| 11916 | // incomplete or function type (hence, a structure shall not |
| 11917 | // contain an instance of itself, but may contain a pointer to |
| 11918 | // an instance of itself), except that the last member of a |
| 11919 | // structure with more than one named member may have incomplete |
| 11920 | // array type; such a structure (and any union containing, |
| 11921 | // possibly recursively, a member that is such a structure) |
| 11922 | // shall not be a member of a structure or an element of an |
| 11923 | // array. |
| Chris Lattner | 0fd893e | 2007-07-31 21:33:24 +0000 | [diff] [blame] | 11924 | if (FDTy->isFunctionType()) { |
| Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 11925 | // Field declared as a function. |
| Chris Lattner | 651d42d | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 11926 | Diag(FD->getLocation(), diag::err_field_declared_as_function) |
| Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 11927 | << FD->getDeclName(); |
| Steve Naroff | db47ee2 | 2007-09-14 22:20:54 +0000 | [diff] [blame] | 11928 | FD->setInvalidDecl(); |
| 11929 | EnclosingDecl->setInvalidDecl(); |
| Chris Lattner | bdf8b8d | 2007-01-24 02:11:17 +0000 | [diff] [blame] | 11930 | continue; |
| Francois Pichet | f657b63 | 2010-09-15 00:14:08 +0000 | [diff] [blame] | 11931 | } else if (FDTy->isIncompleteArrayType() && Record && |
| David Blaikie | 751c558 | 2011-09-22 02:58:26 +0000 | [diff] [blame] | 11932 | ((i + 1 == Fields.end() && !Record->isUnion()) || |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 11933 | ((getLangOpts().MicrosoftExt || |
| 11934 | getLangOpts().CPlusPlus) && |
| David Blaikie | 751c558 | 2011-09-22 02:58:26 +0000 | [diff] [blame] | 11935 | (i + 1 == Fields.end() || Record->isUnion())))) { |
| Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 11936 | // Flexible array member. |
| Argyrios Kyrtzidis | 7e25a95 | 2011-03-07 20:04:04 +0000 | [diff] [blame] | 11937 | // Microsoft and g++ is more permissive regarding flexible array. |
| Francois Pichet | f657b63 | 2010-09-15 00:14:08 +0000 | [diff] [blame] | 11938 | // It will accept flexible array in union and also |
| Anders Carlsson | 0ea1047 | 2010-10-17 23:36:12 +0000 | [diff] [blame] | 11939 | // as the sole element of a struct/class. |
| David Majnemer | 4101621 | 2013-11-02 10:38:05 +0000 | [diff] [blame] | 11940 | unsigned DiagID = 0; |
| 11941 | if (Record->isUnion()) |
| 11942 | DiagID = getLangOpts().MicrosoftExt |
| 11943 | ? diag::ext_flexible_array_union_ms |
| 11944 | : getLangOpts().CPlusPlus |
| 11945 | ? diag::ext_flexible_array_union_gnu |
| 11946 | : diag::err_flexible_array_union; |
| 11947 | else if (Fields.size() == 1) |
| 11948 | DiagID = getLangOpts().MicrosoftExt |
| 11949 | ? diag::ext_flexible_array_empty_aggregate_ms |
| 11950 | : getLangOpts().CPlusPlus |
| 11951 | ? diag::ext_flexible_array_empty_aggregate_gnu |
| 11952 | : NumNamedMembers < 1 |
| 11953 | ? diag::err_flexible_array_empty_aggregate |
| 11954 | : 0; |
| 11955 | |
| 11956 | if (DiagID) |
| 11957 | Diag(FD->getLocation(), DiagID) << FD->getDeclName() |
| 11958 | << Record->getTagKind(); |
| David Majnemer | 08cd760 | 2013-11-02 11:19:13 +0000 | [diff] [blame] | 11959 | // While the layout of types that contain virtual bases is not specified |
| 11960 | // by the C++ standard, both the Itanium and Microsoft C++ ABIs place |
| 11961 | // virtual bases after the derived members. This would make a flexible |
| 11962 | // array member declared at the end of an object not adjacent to the end |
| 11963 | // of the type. |
| 11964 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Record)) |
| 11965 | if (RD->getNumVBases() != 0) |
| 11966 | Diag(FD->getLocation(), diag::err_flexible_array_virtual_base) |
| 11967 | << FD->getDeclName() << Record->getTagKind(); |
| David Majnemer | 4101621 | 2013-11-02 10:38:05 +0000 | [diff] [blame] | 11968 | if (!getLangOpts().C99) |
| 11969 | Diag(FD->getLocation(), diag::ext_c99_flexible_array_member) |
| 11970 | << FD->getDeclName() << Record->getTagKind(); |
| 11971 | |
| Richard Smith | 6fa28ff | 2014-01-11 00:53:35 +0000 | [diff] [blame] | 11972 | // If the element type has a non-trivial destructor, we would not |
| 11973 | // implicitly destroy the elements, so disallow it for now. |
| 11974 | // |
| 11975 | // FIXME: GCC allows this. We should probably either implicitly delete |
| 11976 | // the destructor of the containing class, or just allow this. |
| 11977 | QualType BaseElem = Context.getBaseElementType(FD->getType()); |
| 11978 | if (!BaseElem->isDependentType() && BaseElem.isDestructedType()) { |
| 11979 | Diag(FD->getLocation(), diag::err_flexible_array_has_nontrivial_dtor) |
| Fariborz Jahanian | b0e2847 | 2010-05-26 20:46:24 +0000 | [diff] [blame] | 11980 | << FD->getDeclName() << FD->getType(); |
| Fariborz Jahanian | 1138ba6 | 2010-05-26 20:19:07 +0000 | [diff] [blame] | 11981 | FD->setInvalidDecl(); |
| 11982 | EnclosingDecl->setInvalidDecl(); |
| 11983 | continue; |
| 11984 | } |
| Chris Lattner | 720a054 | 2007-01-25 00:44:24 +0000 | [diff] [blame] | 11985 | // Okay, we have a legal flexible array member at the end of the struct. |
| Fariborz Jahanian | aefb230 | 2007-09-14 16:27:55 +0000 | [diff] [blame] | 11986 | if (Record) |
| 11987 | Record->setHasFlexibleArrayMember(true); |
| Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 11988 | } else if (!FDTy->isDependentType() && |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11989 | RequireCompleteType(FD->getLocation(), FD->getType(), |
| Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 11990 | diag::err_field_incomplete)) { |
| 11991 | // Incomplete type |
| 11992 | FD->setInvalidDecl(); |
| 11993 | EnclosingDecl->setInvalidDecl(); |
| 11994 | continue; |
| Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 11995 | } else if (const RecordType *FDTTy = FDTy->getAs<RecordType>()) { |
| Chris Lattner | 720a054 | 2007-01-25 00:44:24 +0000 | [diff] [blame] | 11996 | if (FDTTy->getDecl()->hasFlexibleArrayMember()) { |
| 11997 | // If this is a member of a union, then entire union becomes "flexible". |
| Argyrios Kyrtzidis | 554a07b | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 11998 | if (Record && Record->isUnion()) { |
| Chris Lattner | 4194315 | 2007-01-25 04:52:46 +0000 | [diff] [blame] | 11999 | Record->setHasFlexibleArrayMember(true); |
| Chris Lattner | 720a054 | 2007-01-25 00:44:24 +0000 | [diff] [blame] | 12000 | } else { |
| 12001 | // If this is a struct/class and this is not the last element, reject |
| 12002 | // it. Note that GCC supports variable sized arrays in the middle of |
| 12003 | // structures. |
| David Blaikie | 751c558 | 2011-09-22 02:58:26 +0000 | [diff] [blame] | 12004 | if (i + 1 != Fields.end()) |
| Douglas Gregor | 3e06dbf | 2009-03-06 23:41:27 +0000 | [diff] [blame] | 12005 | Diag(FD->getLocation(), diag::ext_variable_sized_type_in_struct) |
| Chris Lattner | b28fe9e | 2009-04-25 18:52:45 +0000 | [diff] [blame] | 12006 | << FD->getDeclName() << FD->getType(); |
| Douglas Gregor | 3e06dbf | 2009-03-06 23:41:27 +0000 | [diff] [blame] | 12007 | else { |
| 12008 | // We support flexible arrays at the end of structs in |
| 12009 | // other structs as an extension. |
| 12010 | Diag(FD->getLocation(), diag::ext_flexible_array_in_struct) |
| 12011 | << FD->getDeclName(); |
| 12012 | if (Record) |
| 12013 | Record->setHasFlexibleArrayMember(true); |
| Chris Lattner | 720a054 | 2007-01-25 00:44:24 +0000 | [diff] [blame] | 12014 | } |
| Chris Lattner | 720a054 | 2007-01-25 00:44:24 +0000 | [diff] [blame] | 12015 | } |
| 12016 | } |
| Fariborz Jahanian | 78f565b | 2012-08-16 22:38:41 +0000 | [diff] [blame] | 12017 | if (isa<ObjCContainerDecl>(EnclosingDecl) && |
| 12018 | RequireNonAbstractType(FD->getLocation(), FD->getType(), |
| 12019 | diag::err_abstract_type_in_decl, |
| 12020 | AbstractIvarType)) { |
| 12021 | // Ivars can not have abstract class types |
| 12022 | FD->setInvalidDecl(); |
| 12023 | } |
| Fariborz Jahanian | 5f21d2f | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 12024 | if (Record && FDTTy->getDecl()->hasObjectMember()) |
| 12025 | Record->setHasObjectMember(true); |
| Fariborz Jahanian | 7865220 | 2013-01-25 23:57:05 +0000 | [diff] [blame] | 12026 | if (Record && FDTTy->getDecl()->hasVolatileMember()) |
| 12027 | Record->setHasVolatileMember(true); |
| John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 12028 | } else if (FDTy->isObjCObjectType()) { |
| Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 12029 | /// A field cannot be an Objective-c object |
| Fariborz Jahanian | 6507135e | 2011-07-26 17:58:54 +0000 | [diff] [blame] | 12030 | Diag(FD->getLocation(), diag::err_statically_allocated_object) |
| 12031 | << FixItHint::CreateInsertion(FD->getLocation(), "*"); |
| 12032 | QualType T = Context.getObjCObjectPointerType(FD->getType()); |
| 12033 | FD->setType(T); |
| Douglas Gregor | e6c3fa0 | 2013-01-28 19:08:09 +0000 | [diff] [blame] | 12034 | } else if (getLangOpts().ObjCAutoRefCount && Record && !ARCErrReported && |
| 12035 | (!getLangOpts().CPlusPlus || Record->isUnion())) { |
| 12036 | // It's an error in ARC if a field has lifetime. |
| 12037 | // We don't want to report this in a system header, though, |
| 12038 | // so we just make the field unavailable. |
| 12039 | // FIXME: that's really not sufficient; we need to make the type |
| 12040 | // itself invalid to, say, initialize or copy. |
| 12041 | QualType T = FD->getType(); |
| 12042 | Qualifiers::ObjCLifetime lifetime = T.getObjCLifetime(); |
| 12043 | if (lifetime && lifetime != Qualifiers::OCL_ExplicitNone) { |
| 12044 | SourceLocation loc = FD->getLocation(); |
| 12045 | if (getSourceManager().isInSystemHeader(loc)) { |
| 12046 | if (!FD->hasAttr<UnavailableAttr>()) { |
| Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 12047 | FD->addAttr(UnavailableAttr::CreateImplicit(Context, |
| 12048 | "this system field has retaining ownership", |
| 12049 | loc)); |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 12050 | } |
| Douglas Gregor | e6c3fa0 | 2013-01-28 19:08:09 +0000 | [diff] [blame] | 12051 | } else { |
| 12052 | Diag(FD->getLocation(), diag::err_arc_objc_object_in_tag) |
| Douglas Gregor | 0ad84b4 | 2013-01-28 20:13:44 +0000 | [diff] [blame] | 12053 | << T->isBlockPointerType() << Record->getTagKind(); |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 12054 | } |
| Douglas Gregor | e6c3fa0 | 2013-01-28 19:08:09 +0000 | [diff] [blame] | 12055 | ARCErrReported = true; |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 12056 | } |
| Douglas Gregor | e6c3fa0 | 2013-01-28 19:08:09 +0000 | [diff] [blame] | 12057 | } else if (getLangOpts().ObjC1 && |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 12058 | getLangOpts().getGC() != LangOptions::NonGC && |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 12059 | Record && !Record->hasObjectMember()) { |
| Douglas Gregor | e6c3fa0 | 2013-01-28 19:08:09 +0000 | [diff] [blame] | 12060 | if (FD->getType()->isObjCObjectPointerType() || |
| 12061 | FD->getType().isObjCGCStrong()) |
| 12062 | Record->setHasObjectMember(true); |
| 12063 | else if (Context.getAsArrayType(FD->getType())) { |
| 12064 | QualType BaseType = Context.getBaseElementType(FD->getType()); |
| 12065 | if (BaseType->isRecordType() && |
| 12066 | BaseType->getAs<RecordType>()->getDecl()->hasObjectMember()) |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 12067 | Record->setHasObjectMember(true); |
| Douglas Gregor | e6c3fa0 | 2013-01-28 19:08:09 +0000 | [diff] [blame] | 12068 | else if (BaseType->isObjCObjectPointerType() || |
| 12069 | BaseType.isObjCGCStrong()) |
| 12070 | Record->setHasObjectMember(true); |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 12071 | } |
| Fariborz Jahanian | 021510e | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 12072 | } |
| Fariborz Jahanian | 7865220 | 2013-01-25 23:57:05 +0000 | [diff] [blame] | 12073 | if (Record && FD->getType().isVolatileQualified()) |
| 12074 | Record->setHasVolatileMember(true); |
| Chris Lattner | 8262560 | 2007-01-24 02:26:21 +0000 | [diff] [blame] | 12075 | // Keep track of the number of named members. |
| Douglas Gregor | 82ac25e | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 12076 | if (FD->getIdentifier()) |
| Chris Lattner | 8262560 | 2007-01-24 02:26:21 +0000 | [diff] [blame] | 12077 | ++NumNamedMembers; |
| Chris Lattner | bdf8b8d | 2007-01-24 02:11:17 +0000 | [diff] [blame] | 12078 | } |
| Sebastian Redl | baad4e7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 12079 | |
| Chris Lattner | 8262560 | 2007-01-24 02:26:21 +0000 | [diff] [blame] | 12080 | // Okay, we successfully defined 'Record'. |
| Chris Lattner | 622c193 | 2008-02-06 00:51:33 +0000 | [diff] [blame] | 12081 | if (Record) { |
| Douglas Gregor | 8fb9512 | 2010-09-29 00:15:42 +0000 | [diff] [blame] | 12082 | bool Completed = false; |
| 12083 | if (CXXRecordDecl *CXXRecord = dyn_cast<CXXRecordDecl>(Record)) { |
| 12084 | if (!CXXRecord->isInvalidDecl()) { |
| 12085 | // Set access bits correctly on the directly-declared conversions. |
| Argyrios Kyrtzidis | a6567c4 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 12086 | for (CXXRecordDecl::conversion_iterator |
| 12087 | I = CXXRecord->conversion_begin(), |
| 12088 | E = CXXRecord->conversion_end(); I != E; ++I) |
| 12089 | I.setAccess((*I)->getAccess()); |
| Douglas Gregor | 8fb9512 | 2010-09-29 00:15:42 +0000 | [diff] [blame] | 12090 | |
| 12091 | if (!CXXRecord->isDependentType()) { |
| Peter Collingbourne | b289fe6 | 2013-05-20 14:12:25 +0000 | [diff] [blame] | 12092 | if (CXXRecord->hasUserDeclaredDestructor()) { |
| 12093 | // Adjust user-defined destructor exception spec. |
| 12094 | if (getLangOpts().CPlusPlus11) |
| 12095 | AdjustDestructorExceptionSpec(CXXRecord, |
| 12096 | CXXRecord->getDestructor()); |
| Peter Collingbourne | b289fe6 | 2013-05-20 14:12:25 +0000 | [diff] [blame] | 12097 | } |
| Sebastian Redl | 623ea82 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 12098 | |
| Douglas Gregor | 8fb9512 | 2010-09-29 00:15:42 +0000 | [diff] [blame] | 12099 | // Add any implicitly-declared members to this class. |
| 12100 | AddImplicitlyDeclaredMembersToClass(CXXRecord); |
| 12101 | |
| 12102 | // If we have virtual base classes, we may end up finding multiple |
| 12103 | // final overriders for a given virtual function. Check for this |
| 12104 | // problem now. |
| 12105 | if (CXXRecord->getNumVBases()) { |
| 12106 | CXXFinalOverriderMap FinalOverriders; |
| 12107 | CXXRecord->getFinalOverriders(FinalOverriders); |
| 12108 | |
| 12109 | for (CXXFinalOverriderMap::iterator M = FinalOverriders.begin(), |
| 12110 | MEnd = FinalOverriders.end(); |
| 12111 | M != MEnd; ++M) { |
| 12112 | for (OverridingMethods::iterator SO = M->second.begin(), |
| 12113 | SOEnd = M->second.end(); |
| 12114 | SO != SOEnd; ++SO) { |
| 12115 | assert(SO->second.size() > 0 && |
| 12116 | "Virtual function without overridding functions?"); |
| 12117 | if (SO->second.size() == 1) |
| 12118 | continue; |
| 12119 | |
| 12120 | // C++ [class.virtual]p2: |
| 12121 | // In a derived class, if a virtual member function of a base |
| 12122 | // class subobject has more than one final overrider the |
| 12123 | // program is ill-formed. |
| 12124 | Diag(Record->getLocation(), diag::err_multiple_final_overriders) |
| Roman Divacky | e637711 | 2012-09-06 15:59:27 +0000 | [diff] [blame] | 12125 | << (const NamedDecl *)M->first << Record; |
| Douglas Gregor | 8fb9512 | 2010-09-29 00:15:42 +0000 | [diff] [blame] | 12126 | Diag(M->first->getLocation(), |
| 12127 | diag::note_overridden_virtual_function); |
| 12128 | for (OverridingMethods::overriding_iterator |
| 12129 | OM = SO->second.begin(), |
| 12130 | OMEnd = SO->second.end(); |
| 12131 | OM != OMEnd; ++OM) |
| 12132 | Diag(OM->Method->getLocation(), diag::note_final_overrider) |
| Roman Divacky | e637711 | 2012-09-06 15:59:27 +0000 | [diff] [blame] | 12133 | << (const NamedDecl *)M->first << OM->Method->getParent(); |
| Douglas Gregor | 8fb9512 | 2010-09-29 00:15:42 +0000 | [diff] [blame] | 12134 | |
| 12135 | Record->setInvalidDecl(); |
| 12136 | } |
| 12137 | } |
| 12138 | CXXRecord->completeDefinition(&FinalOverriders); |
| 12139 | Completed = true; |
| 12140 | } |
| 12141 | } |
| 12142 | } |
| 12143 | } |
| 12144 | |
| 12145 | if (!Completed) |
| 12146 | Record->completeDefinition(); |
| Sebastian Redl | 623ea82 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 12147 | |
| David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 12148 | if (Record->hasAttrs()) { |
| Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 12149 | CheckAlignasUnderalignment(Record); |
| Serge Pavlov | 89578fd | 2013-06-08 13:29:58 +0000 | [diff] [blame] | 12150 | |
| David Majnemer | 98c9ee2 | 2014-02-07 00:43:07 +0000 | [diff] [blame] | 12151 | if (const MSInheritanceAttr *IA = Record->getAttr<MSInheritanceAttr>()) |
| David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 12152 | checkMSInheritanceAttrOnDefinition(cast<CXXRecordDecl>(Record), |
| David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 12153 | IA->getRange(), IA->getBestCase(), |
| David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 12154 | IA->getSemanticSpelling()); |
| 12155 | } |
| 12156 | |
| Serge Pavlov | 3cb8022 | 2013-11-14 02:13:03 +0000 | [diff] [blame] | 12157 | // Check if the structure/union declaration is a type that can have zero |
| 12158 | // size in C. For C this is a language extension, for C++ it may cause |
| 12159 | // compatibility problems. |
| 12160 | bool CheckForZeroSize; |
| Serge Pavlov | 89578fd | 2013-06-08 13:29:58 +0000 | [diff] [blame] | 12161 | if (!getLangOpts().CPlusPlus) { |
| Serge Pavlov | 3cb8022 | 2013-11-14 02:13:03 +0000 | [diff] [blame] | 12162 | CheckForZeroSize = true; |
| 12163 | } else { |
| 12164 | // For C++ filter out types that cannot be referenced in C code. |
| 12165 | CXXRecordDecl *CXXRecord = cast<CXXRecordDecl>(Record); |
| 12166 | CheckForZeroSize = |
| 12167 | CXXRecord->getLexicalDeclContext()->isExternCContext() && |
| 12168 | !CXXRecord->isDependentType() && |
| 12169 | CXXRecord->isCLike(); |
| 12170 | } |
| 12171 | if (CheckForZeroSize) { |
| Serge Pavlov | 89578fd | 2013-06-08 13:29:58 +0000 | [diff] [blame] | 12172 | bool ZeroSize = true; |
| Serge Pavlov | f7c1a21 | 2013-06-17 17:18:51 +0000 | [diff] [blame] | 12173 | bool IsEmpty = true; |
| 12174 | unsigned NonBitFields = 0; |
| Serge Pavlov | 89578fd | 2013-06-08 13:29:58 +0000 | [diff] [blame] | 12175 | for (RecordDecl::field_iterator I = Record->field_begin(), |
| Serge Pavlov | f7c1a21 | 2013-06-17 17:18:51 +0000 | [diff] [blame] | 12176 | E = Record->field_end(); |
| 12177 | (NonBitFields == 0 || ZeroSize) && I != E; ++I) { |
| 12178 | IsEmpty = false; |
| Serge Pavlov | 89578fd | 2013-06-08 13:29:58 +0000 | [diff] [blame] | 12179 | if (I->isUnnamedBitfield()) { |
| Serge Pavlov | 89578fd | 2013-06-08 13:29:58 +0000 | [diff] [blame] | 12180 | if (I->getBitWidthValue(Context) > 0) |
| 12181 | ZeroSize = false; |
| 12182 | } else { |
| Serge Pavlov | f7c1a21 | 2013-06-17 17:18:51 +0000 | [diff] [blame] | 12183 | ++NonBitFields; |
| 12184 | QualType FieldType = I->getType(); |
| 12185 | if (FieldType->isIncompleteType() || |
| 12186 | !Context.getTypeSizeInChars(FieldType).isZero()) |
| 12187 | ZeroSize = false; |
| Serge Pavlov | 89578fd | 2013-06-08 13:29:58 +0000 | [diff] [blame] | 12188 | } |
| 12189 | } |
| 12190 | |
| Serge Pavlov | 3cb8022 | 2013-11-14 02:13:03 +0000 | [diff] [blame] | 12191 | // Empty structs are an extension in C (C99 6.7.2.1p7). They are |
| 12192 | // allowed in C++, but warn if its declaration is inside |
| 12193 | // extern "C" block. |
| 12194 | if (ZeroSize) { |
| 12195 | Diag(RecLoc, getLangOpts().CPlusPlus ? |
| 12196 | diag::warn_zero_size_struct_union_in_extern_c : |
| 12197 | diag::warn_zero_size_struct_union_compat) |
| 12198 | << IsEmpty << Record->isUnion() << (NonBitFields > 1); |
| 12199 | } |
| Serge Pavlov | 89578fd | 2013-06-08 13:29:58 +0000 | [diff] [blame] | 12200 | |
| Serge Pavlov | 3cb8022 | 2013-11-14 02:13:03 +0000 | [diff] [blame] | 12201 | // Structs without named members are extension in C (C99 6.7.2.1p7), |
| 12202 | // but are accepted by GCC. |
| 12203 | if (NonBitFields == 0 && !getLangOpts().CPlusPlus) { |
| 12204 | Diag(RecLoc, IsEmpty ? diag::ext_empty_struct_union : |
| 12205 | diag::ext_no_named_members_in_struct_union) |
| 12206 | << Record->isUnion(); |
| Serge Pavlov | 89578fd | 2013-06-08 13:29:58 +0000 | [diff] [blame] | 12207 | } |
| 12208 | } |
| Chris Lattner | 622c193 | 2008-02-06 00:51:33 +0000 | [diff] [blame] | 12209 | } else { |
| Jay Foad | 7d0479f | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 12210 | ObjCIvarDecl **ClsFields = |
| 12211 | reinterpret_cast<ObjCIvarDecl**>(RecFields.data()); |
| Fariborz Jahanian | 0222553 | 2008-12-13 20:28:25 +0000 | [diff] [blame] | 12212 | if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(EnclosingDecl)) { |
| Douglas Gregor | 1640832 | 2011-12-15 22:34:59 +0000 | [diff] [blame] | 12213 | ID->setEndOfDefinitionLoc(RBrac); |
| Fariborz Jahanian | 6845383 | 2009-06-05 18:16:35 +0000 | [diff] [blame] | 12214 | // Add ivar's to class's DeclContext. |
| 12215 | for (unsigned i = 0, e = RecFields.size(); i != e; ++i) { |
| 12216 | ClsFields[i]->setLexicalDeclContext(ID); |
| Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 12217 | ID->addDecl(ClsFields[i]); |
| Fariborz Jahanian | 6845383 | 2009-06-05 18:16:35 +0000 | [diff] [blame] | 12218 | } |
| Fariborz Jahanian | a599c13 | 2008-12-16 01:08:35 +0000 | [diff] [blame] | 12219 | // Must enforce the rule that ivars in the base classes may not be |
| 12220 | // duplicates. |
| Fariborz Jahanian | 545643c | 2010-02-23 23:41:11 +0000 | [diff] [blame] | 12221 | if (ID->getSuperClass()) |
| 12222 | DiagnoseDuplicateIvars(ID, ID->getSuperClass()); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12223 | } else if (ObjCImplementationDecl *IMPDecl = |
| Chris Lattner | d13b8b5 | 2009-02-23 22:00:08 +0000 | [diff] [blame] | 12224 | dyn_cast<ObjCImplementationDecl>(EnclosingDecl)) { |
| Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 12225 | assert(IMPDecl && "ActOnFields - missing ObjCImplementationDecl"); |
| Fariborz Jahanian | 6845383 | 2009-06-05 18:16:35 +0000 | [diff] [blame] | 12226 | for (unsigned I = 0, N = RecFields.size(); I != N; ++I) |
| 12227 | // Ivar declared in @implementation never belongs to the implementation. |
| 12228 | // Only it is in implementation's lexical context. |
| Douglas Gregor | 5f66205 | 2009-04-23 03:23:08 +0000 | [diff] [blame] | 12229 | ClsFields[I]->setLexicalDeclContext(IMPDecl); |
| Fariborz Jahanian | 95b6076 | 2007-10-31 18:48:14 +0000 | [diff] [blame] | 12230 | CheckImplementationIvars(IMPDecl, ClsFields, RecFields.size(), RBrac); |
| Fariborz Jahanian | a7765fe | 2012-02-20 20:09:20 +0000 | [diff] [blame] | 12231 | IMPDecl->setIvarLBraceLoc(LBrac); |
| 12232 | IMPDecl->setIvarRBraceLoc(RBrac); |
| Fariborz Jahanian | 4c172c6 | 2010-02-22 23:04:20 +0000 | [diff] [blame] | 12233 | } else if (ObjCCategoryDecl *CDecl = |
| 12234 | dyn_cast<ObjCCategoryDecl>(EnclosingDecl)) { |
| Fariborz Jahanian | 6a0a2e0 | 2010-04-06 22:43:48 +0000 | [diff] [blame] | 12235 | // case of ivars in class extension; all other cases have been |
| 12236 | // reported as errors elsewhere. |
| 12237 | // FIXME. Class extension does not have a LocEnd field. |
| 12238 | // CDecl->setLocEnd(RBrac); |
| 12239 | // Add ivar's to class extension's DeclContext. |
| Fariborz Jahanian | 2512747 | 2011-10-21 18:03:52 +0000 | [diff] [blame] | 12240 | // Diagnose redeclaration of private ivars. |
| 12241 | ObjCInterfaceDecl *IDecl = CDecl->getClassInterface(); |
| Fariborz Jahanian | 6a0a2e0 | 2010-04-06 22:43:48 +0000 | [diff] [blame] | 12242 | for (unsigned i = 0, e = RecFields.size(); i != e; ++i) { |
| Fariborz Jahanian | 2512747 | 2011-10-21 18:03:52 +0000 | [diff] [blame] | 12243 | if (IDecl) { |
| 12244 | if (const ObjCIvarDecl *ClsIvar = |
| 12245 | IDecl->getIvarDecl(ClsFields[i]->getIdentifier())) { |
| 12246 | Diag(ClsFields[i]->getLocation(), |
| 12247 | diag::err_duplicate_ivar_declaration); |
| 12248 | Diag(ClsIvar->getLocation(), diag::note_previous_definition); |
| 12249 | continue; |
| 12250 | } |
| Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 12251 | for (ObjCInterfaceDecl::known_extensions_iterator |
| 12252 | Ext = IDecl->known_extensions_begin(), |
| 12253 | ExtEnd = IDecl->known_extensions_end(); |
| 12254 | Ext != ExtEnd; ++Ext) { |
| 12255 | if (const ObjCIvarDecl *ClsExtIvar |
| 12256 | = Ext->getIvarDecl(ClsFields[i]->getIdentifier())) { |
| Fariborz Jahanian | 2512747 | 2011-10-21 18:03:52 +0000 | [diff] [blame] | 12257 | Diag(ClsFields[i]->getLocation(), |
| 12258 | diag::err_duplicate_ivar_declaration); |
| 12259 | Diag(ClsExtIvar->getLocation(), diag::note_previous_definition); |
| 12260 | continue; |
| 12261 | } |
| 12262 | } |
| 12263 | } |
| Fariborz Jahanian | 6a0a2e0 | 2010-04-06 22:43:48 +0000 | [diff] [blame] | 12264 | ClsFields[i]->setLexicalDeclContext(CDecl); |
| 12265 | CDecl->addDecl(ClsFields[i]); |
| Fariborz Jahanian | 4c172c6 | 2010-02-22 23:04:20 +0000 | [diff] [blame] | 12266 | } |
| Fariborz Jahanian | a7765fe | 2012-02-20 20:09:20 +0000 | [diff] [blame] | 12267 | CDecl->setIvarLBraceLoc(LBrac); |
| 12268 | CDecl->setIvarRBraceLoc(RBrac); |
| Fariborz Jahanian | 2a4dd31 | 2007-09-26 18:27:25 +0000 | [diff] [blame] | 12269 | } |
| Fariborz Jahanian | f3287bf | 2007-09-14 21:08:27 +0000 | [diff] [blame] | 12270 | } |
| Daniel Dunbar | 325601a | 2008-10-03 17:33:35 +0000 | [diff] [blame] | 12271 | |
| 12272 | if (Attr) |
| Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 12273 | ProcessDeclAttributeList(S, Record, Attr); |
| Chris Lattner | 1300fb9 | 2007-01-23 23:42:53 +0000 | [diff] [blame] | 12274 | } |
| 12275 | |
| Douglas Gregor | 6791a0d | 2010-02-01 23:36:03 +0000 | [diff] [blame] | 12276 | /// \brief Determine whether the given integral value is representable within |
| 12277 | /// the given type T. |
| 12278 | static bool isRepresentableIntegerValue(ASTContext &Context, |
| 12279 | llvm::APSInt &Value, |
| 12280 | QualType T) { |
| Douglas Gregor | 6972a62 | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 12281 | assert(T->isIntegralType(Context) && "Integral type required!"); |
| Douglas Gregor | c1cf814 | 2010-04-15 15:53:31 +0000 | [diff] [blame] | 12282 | unsigned BitWidth = Context.getIntWidth(T); |
| Douglas Gregor | 6791a0d | 2010-02-01 23:36:03 +0000 | [diff] [blame] | 12283 | |
| Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 12284 | if (Value.isUnsigned() || Value.isNonNegative()) { |
| Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 12285 | if (T->isSignedIntegerOrEnumerationType()) |
| Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 12286 | --BitWidth; |
| 12287 | return Value.getActiveBits() <= BitWidth; |
| 12288 | } |
| Douglas Gregor | 6791a0d | 2010-02-01 23:36:03 +0000 | [diff] [blame] | 12289 | return Value.getMinSignedBits() <= BitWidth; |
| 12290 | } |
| 12291 | |
| 12292 | // \brief Given an integral type, return the next larger integral type |
| 12293 | // (or a NULL type of no such type exists). |
| 12294 | static QualType getNextLargerIntegralType(ASTContext &Context, QualType T) { |
| 12295 | // FIXME: Int128/UInt128 support, which also needs to be introduced into |
| 12296 | // enum checking below. |
| Douglas Gregor | 6972a62 | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 12297 | assert(T->isIntegralType(Context) && "Integral type required!"); |
| Douglas Gregor | 6791a0d | 2010-02-01 23:36:03 +0000 | [diff] [blame] | 12298 | const unsigned NumTypes = 4; |
| 12299 | QualType SignedIntegralTypes[NumTypes] = { |
| 12300 | Context.ShortTy, Context.IntTy, Context.LongTy, Context.LongLongTy |
| 12301 | }; |
| 12302 | QualType UnsignedIntegralTypes[NumTypes] = { |
| 12303 | Context.UnsignedShortTy, Context.UnsignedIntTy, Context.UnsignedLongTy, |
| 12304 | Context.UnsignedLongLongTy |
| 12305 | }; |
| 12306 | |
| 12307 | unsigned BitWidth = Context.getTypeSize(T); |
| Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 12308 | QualType *Types = T->isSignedIntegerOrEnumerationType()? SignedIntegralTypes |
| 12309 | : UnsignedIntegralTypes; |
| Douglas Gregor | 6791a0d | 2010-02-01 23:36:03 +0000 | [diff] [blame] | 12310 | for (unsigned I = 0; I != NumTypes; ++I) |
| 12311 | if (Context.getTypeSize(Types[I]) > BitWidth) |
| 12312 | return Types[I]; |
| 12313 | |
| 12314 | return QualType(); |
| 12315 | } |
| 12316 | |
| Douglas Gregor | 954f6b27 | 2009-03-17 19:05:46 +0000 | [diff] [blame] | 12317 | EnumConstantDecl *Sema::CheckEnumConstant(EnumDecl *Enum, |
| 12318 | EnumConstantDecl *LastEnumConst, |
| 12319 | SourceLocation IdLoc, |
| 12320 | IdentifierInfo *Id, |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 12321 | Expr *Val) { |
| Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 12322 | unsigned IntWidth = Context.getTargetInfo().getIntWidth(); |
| Douglas Gregor | 6791a0d | 2010-02-01 23:36:03 +0000 | [diff] [blame] | 12323 | llvm::APSInt EnumVal(IntWidth); |
| Douglas Gregor | 954f6b27 | 2009-03-17 19:05:46 +0000 | [diff] [blame] | 12324 | QualType EltTy; |
| Douglas Gregor | 2b988fd | 2010-12-16 00:24:44 +0000 | [diff] [blame] | 12325 | |
| 12326 | if (Val && DiagnoseUnexpandedParameterPack(Val, UPPC_EnumeratorValue)) |
| 12327 | Val = 0; |
| 12328 | |
| Eli Friedman | 7c6515a | 2011-12-06 00:10:34 +0000 | [diff] [blame] | 12329 | if (Val) |
| 12330 | Val = DefaultLvalueConversion(Val).take(); |
| 12331 | |
| Douglas Gregor | b2186fe | 2009-11-06 00:03:12 +0000 | [diff] [blame] | 12332 | if (Val) { |
| Douglas Gregor | dc70c3a | 2010-03-02 17:53:14 +0000 | [diff] [blame] | 12333 | if (Enum->isDependentType() || Val->isTypeDependent()) |
| Douglas Gregor | b2186fe | 2009-11-06 00:03:12 +0000 | [diff] [blame] | 12334 | EltTy = Context.DependentTy; |
| 12335 | else { |
| Douglas Gregor | b2186fe | 2009-11-06 00:03:12 +0000 | [diff] [blame] | 12336 | SourceLocation ExpLoc; |
| Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 12337 | if (getLangOpts().CPlusPlus11 && Enum->isFixed() && |
| Alp Toker | bfa3934 | 2014-01-14 12:51:41 +0000 | [diff] [blame] | 12338 | !getLangOpts().MSVCCompat) { |
| Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 12339 | // C++11 [dcl.enum]p5: If the underlying type is fixed, [...] the |
| 12340 | // constant-expression in the enumerator-definition shall be a converted |
| 12341 | // constant expression of the underlying type. |
| 12342 | EltTy = Enum->getIntegerType(); |
| 12343 | ExprResult Converted = |
| 12344 | CheckConvertedConstantExpression(Val, EltTy, EnumVal, |
| 12345 | CCEK_Enumerator); |
| 12346 | if (Converted.isInvalid()) |
| 12347 | Val = 0; |
| 12348 | else |
| 12349 | Val = Converted.take(); |
| 12350 | } else if (!Val->isValueDependent() && |
| Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 12351 | !(Val = VerifyIntegerConstantExpression(Val, |
| 12352 | &EnumVal).take())) { |
| Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 12353 | // C99 6.7.2.2p2: Make sure we have an integer constant expression. |
| Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 12354 | } else { |
| Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 12355 | if (Enum->isFixed()) { |
| 12356 | EltTy = Enum->getIntegerType(); |
| 12357 | |
| Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 12358 | // In Obj-C and Microsoft mode, require the enumeration value to be |
| 12359 | // representable in the underlying type of the enumeration. In C++11, |
| 12360 | // we perform a non-narrowing conversion as part of converted constant |
| 12361 | // expression checking. |
| Francois Pichet | a310806 | 2010-10-18 15:01:13 +0000 | [diff] [blame] | 12362 | if (!isRepresentableIntegerValue(Context, EnumVal, EltTy)) { |
| Alp Toker | bfa3934 | 2014-01-14 12:51:41 +0000 | [diff] [blame] | 12363 | if (getLangOpts().MSVCCompat) { |
| Francois Pichet | a310806 | 2010-10-18 15:01:13 +0000 | [diff] [blame] | 12364 | Diag(IdLoc, diag::ext_enumerator_too_large) << EltTy; |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 12365 | Val = ImpCastExprToType(Val, EltTy, CK_IntegralCast).take(); |
| Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 12366 | } else |
| 12367 | Diag(IdLoc, diag::err_enumerator_too_large) << EltTy; |
| Francois Pichet | a310806 | 2010-10-18 15:01:13 +0000 | [diff] [blame] | 12368 | } else |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 12369 | Val = ImpCastExprToType(Val, EltTy, CK_IntegralCast).take(); |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 12370 | } else if (getLangOpts().CPlusPlus) { |
| Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 12371 | // C++11 [dcl.enum]p5: |
| Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 12372 | // If the underlying type is not fixed, the type of each enumerator |
| 12373 | // is the type of its initializing value: |
| 12374 | // - If an initializer is specified for an enumerator, the |
| 12375 | // initializing value has the same type as the expression. |
| 12376 | EltTy = Val->getType(); |
| Eli Friedman | 2beed11 | 2012-02-07 04:34:38 +0000 | [diff] [blame] | 12377 | } else { |
| 12378 | // C99 6.7.2.2p2: |
| 12379 | // The expression that defines the value of an enumeration constant |
| 12380 | // shall be an integer constant expression that has a value |
| 12381 | // representable as an int. |
| 12382 | |
| 12383 | // Complain if the value is not representable in an int. |
| 12384 | if (!isRepresentableIntegerValue(Context, EnumVal, Context.IntTy)) |
| 12385 | Diag(IdLoc, diag::ext_enum_value_not_int) |
| 12386 | << EnumVal.toString(10) << Val->getSourceRange() |
| 12387 | << (EnumVal.isUnsigned() || EnumVal.isNonNegative()); |
| 12388 | else if (!Context.hasSameType(Val->getType(), Context.IntTy)) { |
| 12389 | // Force the type of the expression to 'int'. |
| 12390 | Val = ImpCastExprToType(Val, Context.IntTy, CK_IntegralCast).take(); |
| 12391 | } |
| 12392 | EltTy = Val->getType(); |
| Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 12393 | } |
| Douglas Gregor | b2186fe | 2009-11-06 00:03:12 +0000 | [diff] [blame] | 12394 | } |
| Douglas Gregor | 954f6b27 | 2009-03-17 19:05:46 +0000 | [diff] [blame] | 12395 | } |
| 12396 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12397 | |
| Douglas Gregor | 954f6b27 | 2009-03-17 19:05:46 +0000 | [diff] [blame] | 12398 | if (!Val) { |
| Eli Friedman | d0e6097 | 2009-12-11 01:34:50 +0000 | [diff] [blame] | 12399 | if (Enum->isDependentType()) |
| 12400 | EltTy = Context.DependentTy; |
| Douglas Gregor | 6791a0d | 2010-02-01 23:36:03 +0000 | [diff] [blame] | 12401 | else if (!LastEnumConst) { |
| 12402 | // C++0x [dcl.enum]p5: |
| 12403 | // If the underlying type is not fixed, the type of each enumerator |
| 12404 | // is the type of its initializing value: |
| 12405 | // - If no initializer is specified for the first enumerator, the |
| 12406 | // initializing value has an unspecified integral type. |
| 12407 | // |
| 12408 | // GCC uses 'int' for its unspecified integral type, as does |
| 12409 | // C99 6.7.2.2p3. |
| Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 12410 | if (Enum->isFixed()) { |
| 12411 | EltTy = Enum->getIntegerType(); |
| 12412 | } |
| 12413 | else { |
| 12414 | EltTy = Context.IntTy; |
| 12415 | } |
| Douglas Gregor | 6791a0d | 2010-02-01 23:36:03 +0000 | [diff] [blame] | 12416 | } else { |
| Douglas Gregor | 954f6b27 | 2009-03-17 19:05:46 +0000 | [diff] [blame] | 12417 | // Assign the last value + 1. |
| 12418 | EnumVal = LastEnumConst->getInitVal(); |
| 12419 | ++EnumVal; |
| Douglas Gregor | 6791a0d | 2010-02-01 23:36:03 +0000 | [diff] [blame] | 12420 | EltTy = LastEnumConst->getType(); |
| Douglas Gregor | 954f6b27 | 2009-03-17 19:05:46 +0000 | [diff] [blame] | 12421 | |
| 12422 | // Check for overflow on increment. |
| Douglas Gregor | 6791a0d | 2010-02-01 23:36:03 +0000 | [diff] [blame] | 12423 | if (EnumVal < LastEnumConst->getInitVal()) { |
| 12424 | // C++0x [dcl.enum]p5: |
| 12425 | // If the underlying type is not fixed, the type of each enumerator |
| 12426 | // is the type of its initializing value: |
| 12427 | // |
| 12428 | // - Otherwise the type of the initializing value is the same as |
| 12429 | // the type of the initializing value of the preceding enumerator |
| 12430 | // unless the incremented value is not representable in that type, |
| 12431 | // in which case the type is an unspecified integral type |
| 12432 | // sufficient to contain the incremented value. If no such type |
| 12433 | // exists, the program is ill-formed. |
| 12434 | QualType T = getNextLargerIntegralType(Context, EltTy); |
| Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 12435 | if (T.isNull() || Enum->isFixed()) { |
| Douglas Gregor | 6791a0d | 2010-02-01 23:36:03 +0000 | [diff] [blame] | 12436 | // There is no integral type larger enough to represent this |
| 12437 | // value. Complain, then allow the value to wrap around. |
| 12438 | EnumVal = LastEnumConst->getInitVal(); |
| Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 12439 | EnumVal = EnumVal.zext(EnumVal.getBitWidth() * 2); |
| Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 12440 | ++EnumVal; |
| 12441 | if (Enum->isFixed()) |
| 12442 | // When the underlying type is fixed, this is ill-formed. |
| 12443 | Diag(IdLoc, diag::err_enumerator_wrapped) |
| 12444 | << EnumVal.toString(10) |
| 12445 | << EltTy; |
| 12446 | else |
| Richard Smith | faf156a | 2014-03-05 22:54:58 +0000 | [diff] [blame] | 12447 | Diag(IdLoc, diag::ext_enumerator_increment_too_large) |
| Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 12448 | << EnumVal.toString(10); |
| Douglas Gregor | 6791a0d | 2010-02-01 23:36:03 +0000 | [diff] [blame] | 12449 | } else { |
| 12450 | EltTy = T; |
| 12451 | } |
| 12452 | |
| 12453 | // Retrieve the last enumerator's value, extent that type to the |
| 12454 | // type that is supposed to be large enough to represent the incremented |
| 12455 | // value, then increment. |
| 12456 | EnumVal = LastEnumConst->getInitVal(); |
| Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 12457 | EnumVal.setIsSigned(EltTy->isSignedIntegerOrEnumerationType()); |
| Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 12458 | EnumVal = EnumVal.zextOrTrunc(Context.getIntWidth(EltTy)); |
| Douglas Gregor | 6791a0d | 2010-02-01 23:36:03 +0000 | [diff] [blame] | 12459 | ++EnumVal; |
| 12460 | |
| 12461 | // If we're not in C++, diagnose the overflow of enumerator values, |
| 12462 | // which in C99 means that the enumerator value is not representable in |
| 12463 | // an int (C99 6.7.2.2p2). However, we support GCC's extension that |
| 12464 | // permits enumerator values that are representable in some larger |
| 12465 | // integral type. |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 12466 | if (!getLangOpts().CPlusPlus && !T.isNull()) |
| Douglas Gregor | 6791a0d | 2010-02-01 23:36:03 +0000 | [diff] [blame] | 12467 | Diag(IdLoc, diag::warn_enum_value_overflow); |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 12468 | } else if (!getLangOpts().CPlusPlus && |
| Douglas Gregor | 6791a0d | 2010-02-01 23:36:03 +0000 | [diff] [blame] | 12469 | !isRepresentableIntegerValue(Context, EnumVal, EltTy)) { |
| 12470 | // Enforce C99 6.7.2.2p2 even when we compute the next value. |
| 12471 | Diag(IdLoc, diag::ext_enum_value_not_int) |
| 12472 | << EnumVal.toString(10) << 1; |
| 12473 | } |
| Douglas Gregor | 954f6b27 | 2009-03-17 19:05:46 +0000 | [diff] [blame] | 12474 | } |
| 12475 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12476 | |
| Douglas Gregor | dc70c3a | 2010-03-02 17:53:14 +0000 | [diff] [blame] | 12477 | if (!EltTy->isDependentType()) { |
| Douglas Gregor | 6791a0d | 2010-02-01 23:36:03 +0000 | [diff] [blame] | 12478 | // Make the enumerator value match the signedness and size of the |
| 12479 | // enumerator's type. |
| Eli Friedman | 2beed11 | 2012-02-07 04:34:38 +0000 | [diff] [blame] | 12480 | EnumVal = EnumVal.extOrTrunc(Context.getIntWidth(EltTy)); |
| Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 12481 | EnumVal.setIsSigned(EltTy->isSignedIntegerOrEnumerationType()); |
| Douglas Gregor | 6791a0d | 2010-02-01 23:36:03 +0000 | [diff] [blame] | 12482 | } |
| Douglas Gregor | b2186fe | 2009-11-06 00:03:12 +0000 | [diff] [blame] | 12483 | |
| Douglas Gregor | 954f6b27 | 2009-03-17 19:05:46 +0000 | [diff] [blame] | 12484 | return EnumConstantDecl::Create(Context, Enum, IdLoc, Id, EltTy, |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12485 | Val, EnumVal); |
| Douglas Gregor | 954f6b27 | 2009-03-17 19:05:46 +0000 | [diff] [blame] | 12486 | } |
| 12487 | |
| 12488 | |
| John McCall | 811a0f5 | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 12489 | Decl *Sema::ActOnEnumConstant(Scope *S, Decl *theEnumDecl, Decl *lastEnumConst, |
| 12490 | SourceLocation IdLoc, IdentifierInfo *Id, |
| 12491 | AttributeList *Attr, |
| Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 12492 | SourceLocation EqualLoc, Expr *Val) { |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 12493 | EnumDecl *TheEnumDecl = cast<EnumDecl>(theEnumDecl); |
| Chris Lattner | 4ef4001 | 2007-06-11 01:28:17 +0000 | [diff] [blame] | 12494 | EnumConstantDecl *LastEnumConst = |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 12495 | cast_or_null<EnumConstantDecl>(lastEnumConst); |
| Chris Lattner | 8116d1b | 2007-01-25 22:38:29 +0000 | [diff] [blame] | 12496 | |
| Chris Lattner | 1a76a3c | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 12497 | // The scope passed in may not be a decl scope. Zip up the scope tree until |
| 12498 | // we find one that is. |
| Douglas Gregor | 45a33ec | 2009-01-12 18:45:55 +0000 | [diff] [blame] | 12499 | S = getNonFieldDeclScope(S); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12500 | |
| Chris Lattner | 8116d1b | 2007-01-25 22:38:29 +0000 | [diff] [blame] | 12501 | // Verify that there isn't already something declared with this name in this |
| 12502 | // scope. |
| Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 12503 | NamedDecl *PrevDecl = LookupSingleName(S, Id, IdLoc, LookupOrdinaryName, |
| Douglas Gregor | 5204248f | 2010-01-19 06:06:57 +0000 | [diff] [blame] | 12504 | ForRedeclaration); |
| Douglas Gregor | 5daeee2 | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 12505 | if (PrevDecl && PrevDecl->isTemplateParameter()) { |
| Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 12506 | // Maybe we will complain about the shadowed template parameter. |
| 12507 | DiagnoseTemplateParameterShadow(IdLoc, PrevDecl); |
| 12508 | // Just pretend that we didn't see the previous declaration. |
| 12509 | PrevDecl = 0; |
| 12510 | } |
| 12511 | |
| 12512 | if (PrevDecl) { |
| Argyrios Kyrtzidis | bd25998 | 2008-07-16 21:01:53 +0000 | [diff] [blame] | 12513 | // When in C++, we may get a TagDecl with the same name; in this case the |
| 12514 | // enum constant will 'hide' the tag. |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 12515 | assert((getLangOpts().CPlusPlus || !isa<TagDecl>(PrevDecl)) && |
| Argyrios Kyrtzidis | bd25998 | 2008-07-16 21:01:53 +0000 | [diff] [blame] | 12516 | "Received TagDecl when not in C++!"); |
| Argyrios Kyrtzidis | 5b144d5 | 2008-09-09 21:18:04 +0000 | [diff] [blame] | 12517 | if (!isa<TagDecl>(PrevDecl) && isDeclInScope(PrevDecl, CurContext, S)) { |
| Chris Lattner | 8116d1b | 2007-01-25 22:38:29 +0000 | [diff] [blame] | 12518 | if (isa<EnumConstantDecl>(PrevDecl)) |
| Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 12519 | Diag(IdLoc, diag::err_redefinition_of_enumerator) << Id; |
| Chris Lattner | 8116d1b | 2007-01-25 22:38:29 +0000 | [diff] [blame] | 12520 | else |
| Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 12521 | Diag(IdLoc, diag::err_redefinition) << Id; |
| Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 12522 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 12523 | return 0; |
| Chris Lattner | 8116d1b | 2007-01-25 22:38:29 +0000 | [diff] [blame] | 12524 | } |
| 12525 | } |
| Chris Lattner | 4ef4001 | 2007-06-11 01:28:17 +0000 | [diff] [blame] | 12526 | |
| Aaron Ballman | 24a1047 | 2012-07-19 03:12:23 +0000 | [diff] [blame] | 12527 | // C++ [class.mem]p15: |
| 12528 | // If T is the name of a class, then each of the following shall have a name |
| 12529 | // different from T: |
| 12530 | // - every enumerator of every member of class T that is an unscoped |
| 12531 | // enumerated type |
| Douglas Gregor | 36c22a2 | 2010-10-15 13:21:21 +0000 | [diff] [blame] | 12532 | if (CXXRecordDecl *Record |
| 12533 | = dyn_cast<CXXRecordDecl>( |
| 12534 | TheEnumDecl->getDeclContext()->getRedeclContext())) |
| Aaron Ballman | 24a1047 | 2012-07-19 03:12:23 +0000 | [diff] [blame] | 12535 | if (!TheEnumDecl->isScoped() && |
| 12536 | Record->getIdentifier() && Record->getIdentifier() == Id) |
| Douglas Gregor | 36c22a2 | 2010-10-15 13:21:21 +0000 | [diff] [blame] | 12537 | Diag(IdLoc, diag::err_member_name_of_class) << Id; |
| 12538 | |
| John McCall | 811a0f5 | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 12539 | EnumConstantDecl *New = |
| 12540 | CheckEnumConstant(TheEnumDecl, LastEnumConst, IdLoc, Id, Val); |
| Chris Lattner | 0515e4b | 2007-08-27 21:16:18 +0000 | [diff] [blame] | 12541 | |
| John McCall | 553c079 | 2010-01-23 00:46:32 +0000 | [diff] [blame] | 12542 | if (New) { |
| John McCall | 811a0f5 | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 12543 | // Process attributes. |
| 12544 | if (Attr) ProcessDeclAttributeList(S, New, Attr); |
| 12545 | |
| 12546 | // Register this decl in the current scope stack. |
| John McCall | 553c079 | 2010-01-23 00:46:32 +0000 | [diff] [blame] | 12547 | New->setAccess(TheEnumDecl->getAccess()); |
| Douglas Gregor | 954f6b27 | 2009-03-17 19:05:46 +0000 | [diff] [blame] | 12548 | PushOnScopeChains(New, S); |
| John McCall | 553c079 | 2010-01-23 00:46:32 +0000 | [diff] [blame] | 12549 | } |
| Douglas Gregor | 2f52119 | 2008-12-17 02:04:30 +0000 | [diff] [blame] | 12550 | |
| Dmitri Gribenko | f26054f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 12551 | ActOnDocumentableDecl(New); |
| 12552 | |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 12553 | return New; |
| Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 12554 | } |
| 12555 | |
| Ted Kremenek | 6cae9ec | 2012-12-22 01:34:09 +0000 | [diff] [blame] | 12556 | // Returns true when the enum initial expression does not trigger the |
| 12557 | // duplicate enum warning. A few common cases are exempted as follows: |
| 12558 | // Element2 = Element1 |
| 12559 | // Element2 = Element1 + 1 |
| 12560 | // Element2 = Element1 - 1 |
| 12561 | // Where Element2 and Element1 are from the same enum. |
| 12562 | static bool ValidDuplicateEnum(EnumConstantDecl *ECD, EnumDecl *Enum) { |
| 12563 | Expr *InitExpr = ECD->getInitExpr(); |
| 12564 | if (!InitExpr) |
| 12565 | return true; |
| 12566 | InitExpr = InitExpr->IgnoreImpCasts(); |
| 12567 | |
| 12568 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(InitExpr)) { |
| 12569 | if (!BO->isAdditiveOp()) |
| 12570 | return true; |
| 12571 | IntegerLiteral *IL = dyn_cast<IntegerLiteral>(BO->getRHS()); |
| 12572 | if (!IL) |
| 12573 | return true; |
| 12574 | if (IL->getValue() != 1) |
| 12575 | return true; |
| 12576 | |
| 12577 | InitExpr = BO->getLHS(); |
| 12578 | } |
| 12579 | |
| 12580 | // This checks if the elements are from the same enum. |
| 12581 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(InitExpr); |
| 12582 | if (!DRE) |
| 12583 | return true; |
| 12584 | |
| 12585 | EnumConstantDecl *EnumConstant = dyn_cast<EnumConstantDecl>(DRE->getDecl()); |
| 12586 | if (!EnumConstant) |
| 12587 | return true; |
| 12588 | |
| 12589 | if (cast<EnumDecl>(TagDecl::castFromDeclContext(ECD->getDeclContext())) != |
| 12590 | Enum) |
| 12591 | return true; |
| 12592 | |
| 12593 | return false; |
| 12594 | } |
| 12595 | |
| 12596 | struct DupKey { |
| 12597 | int64_t val; |
| 12598 | bool isTombstoneOrEmptyKey; |
| 12599 | DupKey(int64_t val, bool isTombstoneOrEmptyKey) |
| 12600 | : val(val), isTombstoneOrEmptyKey(isTombstoneOrEmptyKey) {} |
| 12601 | }; |
| 12602 | |
| 12603 | static DupKey GetDupKey(const llvm::APSInt& Val) { |
| 12604 | return DupKey(Val.isSigned() ? Val.getSExtValue() : Val.getZExtValue(), |
| 12605 | false); |
| 12606 | } |
| 12607 | |
| 12608 | struct DenseMapInfoDupKey { |
| 12609 | static DupKey getEmptyKey() { return DupKey(0, true); } |
| 12610 | static DupKey getTombstoneKey() { return DupKey(1, true); } |
| 12611 | static unsigned getHashValue(const DupKey Key) { |
| 12612 | return (unsigned)(Key.val * 37); |
| 12613 | } |
| 12614 | static bool isEqual(const DupKey& LHS, const DupKey& RHS) { |
| 12615 | return LHS.isTombstoneOrEmptyKey == RHS.isTombstoneOrEmptyKey && |
| 12616 | LHS.val == RHS.val; |
| 12617 | } |
| 12618 | }; |
| 12619 | |
| 12620 | // Emits a warning when an element is implicitly set a value that |
| 12621 | // a previous element has already been set to. |
| Dmitri Gribenko | e5fde99 | 2013-04-27 20:23:52 +0000 | [diff] [blame] | 12622 | static void CheckForDuplicateEnumValues(Sema &S, ArrayRef<Decl *> Elements, |
| 12623 | EnumDecl *Enum, |
| Ted Kremenek | 6cae9ec | 2012-12-22 01:34:09 +0000 | [diff] [blame] | 12624 | QualType EnumType) { |
| 12625 | if (S.Diags.getDiagnosticLevel(diag::warn_duplicate_enum_values, |
| 12626 | Enum->getLocation()) == |
| 12627 | DiagnosticsEngine::Ignored) |
| 12628 | return; |
| 12629 | // Avoid anonymous enums |
| 12630 | if (!Enum->getIdentifier()) |
| 12631 | return; |
| 12632 | |
| 12633 | // Only check for small enums. |
| 12634 | if (Enum->getNumPositiveBits() > 63 || Enum->getNumNegativeBits() > 64) |
| 12635 | return; |
| 12636 | |
| Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 12637 | typedef SmallVector<EnumConstantDecl *, 3> ECDVector; |
| 12638 | typedef SmallVector<ECDVector *, 3> DuplicatesVector; |
| Ted Kremenek | 6cae9ec | 2012-12-22 01:34:09 +0000 | [diff] [blame] | 12639 | |
| 12640 | typedef llvm::PointerUnion<EnumConstantDecl*, ECDVector*> DeclOrVector; |
| 12641 | typedef llvm::DenseMap<DupKey, DeclOrVector, DenseMapInfoDupKey> |
| 12642 | ValueToVectorMap; |
| 12643 | |
| 12644 | DuplicatesVector DupVector; |
| 12645 | ValueToVectorMap EnumMap; |
| 12646 | |
| 12647 | // Populate the EnumMap with all values represented by enum constants without |
| 12648 | // an initialier. |
| Dmitri Gribenko | e5fde99 | 2013-04-27 20:23:52 +0000 | [diff] [blame] | 12649 | for (unsigned i = 0, e = Elements.size(); i != e; ++i) { |
| Benjamin Kramer | 5c488ef | 2013-04-07 14:10:40 +0000 | [diff] [blame] | 12650 | EnumConstantDecl *ECD = cast_or_null<EnumConstantDecl>(Elements[i]); |
| Ted Kremenek | 6cae9ec | 2012-12-22 01:34:09 +0000 | [diff] [blame] | 12651 | |
| 12652 | // Null EnumConstantDecl means a previous diagnostic has been emitted for |
| 12653 | // this constant. Skip this enum since it may be ill-formed. |
| 12654 | if (!ECD) { |
| 12655 | return; |
| 12656 | } |
| 12657 | |
| 12658 | if (ECD->getInitExpr()) |
| 12659 | continue; |
| 12660 | |
| 12661 | DupKey Key = GetDupKey(ECD->getInitVal()); |
| 12662 | DeclOrVector &Entry = EnumMap[Key]; |
| 12663 | |
| 12664 | // First time encountering this value. |
| 12665 | if (Entry.isNull()) |
| 12666 | Entry = ECD; |
| 12667 | } |
| 12668 | |
| 12669 | // Create vectors for any values that has duplicates. |
| Dmitri Gribenko | e5fde99 | 2013-04-27 20:23:52 +0000 | [diff] [blame] | 12670 | for (unsigned i = 0, e = Elements.size(); i != e; ++i) { |
| Ted Kremenek | 6cae9ec | 2012-12-22 01:34:09 +0000 | [diff] [blame] | 12671 | EnumConstantDecl *ECD = cast<EnumConstantDecl>(Elements[i]); |
| 12672 | if (!ValidDuplicateEnum(ECD, Enum)) |
| 12673 | continue; |
| 12674 | |
| 12675 | DupKey Key = GetDupKey(ECD->getInitVal()); |
| 12676 | |
| 12677 | DeclOrVector& Entry = EnumMap[Key]; |
| 12678 | if (Entry.isNull()) |
| 12679 | continue; |
| 12680 | |
| 12681 | if (EnumConstantDecl *D = Entry.dyn_cast<EnumConstantDecl*>()) { |
| 12682 | // Ensure constants are different. |
| 12683 | if (D == ECD) |
| 12684 | continue; |
| 12685 | |
| 12686 | // Create new vector and push values onto it. |
| 12687 | ECDVector *Vec = new ECDVector(); |
| 12688 | Vec->push_back(D); |
| 12689 | Vec->push_back(ECD); |
| 12690 | |
| 12691 | // Update entry to point to the duplicates vector. |
| 12692 | Entry = Vec; |
| 12693 | |
| 12694 | // Store the vector somewhere we can consult later for quick emission of |
| 12695 | // diagnostics. |
| 12696 | DupVector.push_back(Vec); |
| 12697 | continue; |
| 12698 | } |
| 12699 | |
| 12700 | ECDVector *Vec = Entry.get<ECDVector*>(); |
| 12701 | // Make sure constants are not added more than once. |
| 12702 | if (*Vec->begin() == ECD) |
| 12703 | continue; |
| 12704 | |
| 12705 | Vec->push_back(ECD); |
| 12706 | } |
| 12707 | |
| 12708 | // Emit diagnostics. |
| 12709 | for (DuplicatesVector::iterator DupVectorIter = DupVector.begin(), |
| 12710 | DupVectorEnd = DupVector.end(); |
| 12711 | DupVectorIter != DupVectorEnd; ++DupVectorIter) { |
| 12712 | ECDVector *Vec = *DupVectorIter; |
| 12713 | assert(Vec->size() > 1 && "ECDVector should have at least 2 elements."); |
| 12714 | |
| 12715 | // Emit warning for one enum constant. |
| 12716 | ECDVector::iterator I = Vec->begin(); |
| 12717 | S.Diag((*I)->getLocation(), diag::warn_duplicate_enum_values) |
| 12718 | << (*I)->getName() << (*I)->getInitVal().toString(10) |
| 12719 | << (*I)->getSourceRange(); |
| 12720 | ++I; |
| 12721 | |
| 12722 | // Emit one note for each of the remaining enum constants with |
| 12723 | // the same value. |
| 12724 | for (ECDVector::iterator E = Vec->end(); I != E; ++I) |
| 12725 | S.Diag((*I)->getLocation(), diag::note_duplicate_element) |
| 12726 | << (*I)->getName() << (*I)->getInitVal().toString(10) |
| 12727 | << (*I)->getSourceRange(); |
| 12728 | delete Vec; |
| 12729 | } |
| 12730 | } |
| 12731 | |
| Mike Stump | 6814d1c | 2009-05-16 07:06:02 +0000 | [diff] [blame] | 12732 | void Sema::ActOnEnumBody(SourceLocation EnumLoc, SourceLocation LBraceLoc, |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 12733 | SourceLocation RBraceLoc, Decl *EnumDeclX, |
| Dmitri Gribenko | e5fde99 | 2013-04-27 20:23:52 +0000 | [diff] [blame] | 12734 | ArrayRef<Decl *> Elements, |
| Edward O'Callaghan | c69169d | 2009-08-08 14:36:57 +0000 | [diff] [blame] | 12735 | Scope *S, AttributeList *Attr) { |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 12736 | EnumDecl *Enum = cast<EnumDecl>(EnumDeclX); |
| Douglas Gregor | 07665a6 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 12737 | QualType EnumType = Context.getTypeDeclType(Enum); |
| Edward O'Callaghan | c69169d | 2009-08-08 14:36:57 +0000 | [diff] [blame] | 12738 | |
| 12739 | if (Attr) |
| 12740 | ProcessDeclAttributeList(S, Enum, Attr); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12741 | |
| Eli Friedman | d0e6097 | 2009-12-11 01:34:50 +0000 | [diff] [blame] | 12742 | if (Enum->isDependentType()) { |
| Dmitri Gribenko | e5fde99 | 2013-04-27 20:23:52 +0000 | [diff] [blame] | 12743 | for (unsigned i = 0, e = Elements.size(); i != e; ++i) { |
| Eli Friedman | d0e6097 | 2009-12-11 01:34:50 +0000 | [diff] [blame] | 12744 | EnumConstantDecl *ECD = |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 12745 | cast_or_null<EnumConstantDecl>(Elements[i]); |
| Eli Friedman | d0e6097 | 2009-12-11 01:34:50 +0000 | [diff] [blame] | 12746 | if (!ECD) continue; |
| 12747 | |
| 12748 | ECD->setType(EnumType); |
| 12749 | } |
| 12750 | |
| John McCall | 9aa35be | 2010-05-06 08:49:23 +0000 | [diff] [blame] | 12751 | Enum->completeDefinition(Context.DependentTy, Context.DependentTy, 0, 0); |
| Eli Friedman | d0e6097 | 2009-12-11 01:34:50 +0000 | [diff] [blame] | 12752 | return; |
| 12753 | } |
| 12754 | |
| Chris Lattner | 67933c0 | 2007-08-28 05:10:31 +0000 | [diff] [blame] | 12755 | // TODO: If the result value doesn't fit in an int, it must be a long or long |
| 12756 | // long value. ISO C does not support this, but GCC does as an extension, |
| 12757 | // emit a warning. |
| Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 12758 | unsigned IntWidth = Context.getTargetInfo().getIntWidth(); |
| 12759 | unsigned CharWidth = Context.getTargetInfo().getCharWidth(); |
| 12760 | unsigned ShortWidth = Context.getTargetInfo().getShortWidth(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12761 | |
| Chris Lattner | b8a501c | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 12762 | // Verify that all the values are okay, compute the size of the values, and |
| 12763 | // reverse the list. |
| 12764 | unsigned NumNegativeBits = 0; |
| 12765 | unsigned NumPositiveBits = 0; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12766 | |
| Chris Lattner | b8a501c | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 12767 | // Keep track of whether all elements have type int. |
| 12768 | bool AllElementsInt = true; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12769 | |
| Dmitri Gribenko | e5fde99 | 2013-04-27 20:23:52 +0000 | [diff] [blame] | 12770 | for (unsigned i = 0, e = Elements.size(); i != e; ++i) { |
| Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 12771 | EnumConstantDecl *ECD = |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 12772 | cast_or_null<EnumConstantDecl>(Elements[i]); |
| Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 12773 | if (!ECD) continue; // Already issued a diagnostic. |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12774 | |
| Chris Lattner | bf478cb | 2007-08-28 05:27:00 +0000 | [diff] [blame] | 12775 | const llvm::APSInt &InitVal = ECD->getInitVal(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12776 | |
| Chris Lattner | b8a501c | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 12777 | // Keep track of the size of positive and negative values. |
| Chris Lattner | 7768313 | 2008-02-26 00:33:57 +0000 | [diff] [blame] | 12778 | if (InitVal.isUnsigned() || InitVal.isNonNegative()) |
| Chris Lattner | 49f980c | 2008-01-14 21:47:29 +0000 | [diff] [blame] | 12779 | NumPositiveBits = std::max(NumPositiveBits, |
| 12780 | (unsigned)InitVal.getActiveBits()); |
| Chris Lattner | b8a501c | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 12781 | else |
| Chris Lattner | 49f980c | 2008-01-14 21:47:29 +0000 | [diff] [blame] | 12782 | NumNegativeBits = std::max(NumNegativeBits, |
| 12783 | (unsigned)InitVal.getMinSignedBits()); |
| Chris Lattner | 4ef4001 | 2007-06-11 01:28:17 +0000 | [diff] [blame] | 12784 | |
| Chris Lattner | b8a501c | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 12785 | // Keep track of whether every enum element has type int (very commmon). |
| 12786 | if (AllElementsInt) |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12787 | AllElementsInt = ECD->getType() == Context.IntTy; |
| Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 12788 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12789 | |
| Chris Lattner | b8a501c | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 12790 | // Figure out the type that should be used for this enum. |
| Chris Lattner | b8a501c | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 12791 | QualType BestType; |
| Chris Lattner | 3a370bf | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 12792 | unsigned BestWidth; |
| Edward O'Callaghan | c69169d | 2009-08-08 14:36:57 +0000 | [diff] [blame] | 12793 | |
| John McCall | 5677499 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 12794 | // C++0x N3000 [conv.prom]p3: |
| 12795 | // An rvalue of an unscoped enumeration type whose underlying |
| 12796 | // type is not fixed can be converted to an rvalue of the first |
| 12797 | // of the following types that can represent all the values of |
| 12798 | // the enumeration: int, unsigned int, long int, unsigned long |
| 12799 | // int, long long int, or unsigned long long int. |
| 12800 | // C99 6.4.4.3p2: |
| 12801 | // An identifier declared as an enumeration constant has type int. |
| 12802 | // The C99 rule is modified by a gcc extension |
| 12803 | QualType BestPromotionType; |
| 12804 | |
| Aaron Ballman | 9ead124 | 2013-12-19 02:39:40 +0000 | [diff] [blame] | 12805 | bool Packed = Enum->hasAttr<PackedAttr>(); |
| Argyrios Kyrtzidis | 74825bc | 2010-10-08 00:25:19 +0000 | [diff] [blame] | 12806 | // -fshort-enums is the equivalent to specifying the packed attribute on all |
| 12807 | // enum definitions. |
| 12808 | if (LangOpts.ShortEnums) |
| 12809 | Packed = true; |
| Edward O'Callaghan | c69169d | 2009-08-08 14:36:57 +0000 | [diff] [blame] | 12810 | |
| Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 12811 | if (Enum->isFixed()) { |
| Eli Friedman | 64d9504 | 2011-10-26 07:38:19 +0000 | [diff] [blame] | 12812 | BestType = Enum->getIntegerType(); |
| 12813 | if (BestType->isPromotableIntegerType()) |
| 12814 | BestPromotionType = Context.getPromotedIntegerType(BestType); |
| 12815 | else |
| 12816 | BestPromotionType = BestType; |
| Duncan Sands | 38b918c | 2010-10-12 14:07:59 +0000 | [diff] [blame] | 12817 | // We don't need to set BestWidth, because BestType is going to be the type |
| 12818 | // of the enumerators, but we do anyway because otherwise some compilers |
| 12819 | // warn that it might be used uninitialized. |
| 12820 | BestWidth = CharWidth; |
| Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 12821 | } |
| 12822 | else if (NumNegativeBits) { |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12823 | // If there is a negative value, figure out the smallest integer type (of |
| Chris Lattner | b8a501c | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 12824 | // int/long/longlong) that fits. |
| Edward O'Callaghan | c69169d | 2009-08-08 14:36:57 +0000 | [diff] [blame] | 12825 | // If it's packed, check also if it fits a char or a short. |
| 12826 | if (Packed && NumNegativeBits <= CharWidth && NumPositiveBits < CharWidth) { |
| John McCall | 5677499 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 12827 | BestType = Context.SignedCharTy; |
| 12828 | BestWidth = CharWidth; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12829 | } else if (Packed && NumNegativeBits <= ShortWidth && |
| Edward O'Callaghan | c69169d | 2009-08-08 14:36:57 +0000 | [diff] [blame] | 12830 | NumPositiveBits < ShortWidth) { |
| John McCall | 5677499 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 12831 | BestType = Context.ShortTy; |
| 12832 | BestWidth = ShortWidth; |
| 12833 | } else if (NumNegativeBits <= IntWidth && NumPositiveBits < IntWidth) { |
| Chris Lattner | b8a501c | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 12834 | BestType = Context.IntTy; |
| Chris Lattner | 3a370bf | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 12835 | BestWidth = IntWidth; |
| 12836 | } else { |
| Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 12837 | BestWidth = Context.getTargetInfo().getLongWidth(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12838 | |
| John McCall | 5677499 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 12839 | if (NumNegativeBits <= BestWidth && NumPositiveBits < BestWidth) { |
| Chris Lattner | b8a501c | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 12840 | BestType = Context.LongTy; |
| John McCall | 5677499 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 12841 | } else { |
| Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 12842 | BestWidth = Context.getTargetInfo().getLongLongWidth(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12843 | |
| Chris Lattner | 3a370bf | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 12844 | if (NumNegativeBits > BestWidth || NumPositiveBits >= BestWidth) |
| Richard Smith | faf156a | 2014-03-05 22:54:58 +0000 | [diff] [blame] | 12845 | Diag(Enum->getLocation(), diag::ext_enum_too_large); |
| Chris Lattner | b8a501c | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 12846 | BestType = Context.LongLongTy; |
| 12847 | } |
| 12848 | } |
| John McCall | 5677499 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 12849 | BestPromotionType = (BestWidth <= IntWidth ? Context.IntTy : BestType); |
| Chris Lattner | b8a501c | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 12850 | } else { |
| Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 12851 | // If there is no negative value, figure out the smallest type that fits |
| 12852 | // all of the enumerator values. |
| Edward O'Callaghan | c69169d | 2009-08-08 14:36:57 +0000 | [diff] [blame] | 12853 | // If it's packed, check also if it fits a char or a short. |
| 12854 | if (Packed && NumPositiveBits <= CharWidth) { |
| John McCall | 5677499 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 12855 | BestType = Context.UnsignedCharTy; |
| 12856 | BestPromotionType = Context.IntTy; |
| 12857 | BestWidth = CharWidth; |
| Edward O'Callaghan | c69169d | 2009-08-08 14:36:57 +0000 | [diff] [blame] | 12858 | } else if (Packed && NumPositiveBits <= ShortWidth) { |
| John McCall | 5677499 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 12859 | BestType = Context.UnsignedShortTy; |
| 12860 | BestPromotionType = Context.IntTy; |
| 12861 | BestWidth = ShortWidth; |
| 12862 | } else if (NumPositiveBits <= IntWidth) { |
| Chris Lattner | b8a501c | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 12863 | BestType = Context.UnsignedIntTy; |
| Chris Lattner | 3a370bf | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 12864 | BestWidth = IntWidth; |
| Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 12865 | BestPromotionType |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 12866 | = (NumPositiveBits == BestWidth || !getLangOpts().CPlusPlus) |
| Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 12867 | ? Context.UnsignedIntTy : Context.IntTy; |
| Chris Lattner | 3a370bf | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 12868 | } else if (NumPositiveBits <= |
| Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 12869 | (BestWidth = Context.getTargetInfo().getLongWidth())) { |
| Chris Lattner | b8a501c | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 12870 | BestType = Context.UnsignedLongTy; |
| Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 12871 | BestPromotionType |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 12872 | = (NumPositiveBits == BestWidth || !getLangOpts().CPlusPlus) |
| Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 12873 | ? Context.UnsignedLongTy : Context.LongTy; |
| Chris Lattner | 37e0587 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 12874 | } else { |
| Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 12875 | BestWidth = Context.getTargetInfo().getLongLongWidth(); |
| Chris Lattner | 3a370bf | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 12876 | assert(NumPositiveBits <= BestWidth && |
| Chris Lattner | b8a501c | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 12877 | "How could an initializer get larger than ULL?"); |
| 12878 | BestType = Context.UnsignedLongLongTy; |
| Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 12879 | BestPromotionType |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 12880 | = (NumPositiveBits == BestWidth || !getLangOpts().CPlusPlus) |
| Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 12881 | ? Context.UnsignedLongLongTy : Context.LongLongTy; |
| Chris Lattner | b8a501c | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 12882 | } |
| 12883 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12884 | |
| Chris Lattner | 3a370bf | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 12885 | // Loop over all of the enumerator constants, changing their types to match |
| 12886 | // the type of the enum if needed. |
| Dmitri Gribenko | e5fde99 | 2013-04-27 20:23:52 +0000 | [diff] [blame] | 12887 | for (unsigned i = 0, e = Elements.size(); i != e; ++i) { |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 12888 | EnumConstantDecl *ECD = cast_or_null<EnumConstantDecl>(Elements[i]); |
| Chris Lattner | 3a370bf | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 12889 | if (!ECD) continue; // Already issued a diagnostic. |
| 12890 | |
| 12891 | // Standard C says the enumerators have int type, but we allow, as an |
| 12892 | // extension, the enumerators to be larger than int size. If each |
| 12893 | // enumerator value fits in an int, type it as an int, otherwise type it the |
| 12894 | // same as the enumerator decl itself. This means that in "enum { X = 1U }" |
| 12895 | // that X has type 'int', not 'unsigned'. |
| Chris Lattner | 3a370bf | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 12896 | |
| 12897 | // Determine whether the value fits into an int. |
| 12898 | llvm::APSInt InitVal = ECD->getInitVal(); |
| Chris Lattner | 3a370bf | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 12899 | |
| 12900 | // If it fits into an integer type, force it. Otherwise force it to match |
| 12901 | // the enum decl type. |
| 12902 | QualType NewTy; |
| 12903 | unsigned NewWidth; |
| 12904 | bool NewSign; |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 12905 | if (!getLangOpts().CPlusPlus && |
| Fariborz Jahanian | 37c6417 | 2011-11-04 18:51:24 +0000 | [diff] [blame] | 12906 | !Enum->isFixed() && |
| Douglas Gregor | 6791a0d | 2010-02-01 23:36:03 +0000 | [diff] [blame] | 12907 | isRepresentableIntegerValue(Context, InitVal, Context.IntTy)) { |
| Chris Lattner | 3a370bf | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 12908 | NewTy = Context.IntTy; |
| 12909 | NewWidth = IntWidth; |
| 12910 | NewSign = true; |
| 12911 | } else if (ECD->getType() == BestType) { |
| 12912 | // Already the right type! |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 12913 | if (getLangOpts().CPlusPlus) |
| Douglas Gregor | 1d248c5 | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 12914 | // C++ [dcl.enum]p4: Following the closing brace of an |
| 12915 | // enum-specifier, each enumerator has the type of its |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12916 | // enumeration. |
| Douglas Gregor | 1d248c5 | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 12917 | ECD->setType(EnumType); |
| Chris Lattner | 3a370bf | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 12918 | continue; |
| 12919 | } else { |
| 12920 | NewTy = BestType; |
| 12921 | NewWidth = BestWidth; |
| Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 12922 | NewSign = BestType->isSignedIntegerOrEnumerationType(); |
| Chris Lattner | 3a370bf | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 12923 | } |
| 12924 | |
| 12925 | // Adjust the APSInt value. |
| Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 12926 | InitVal = InitVal.extOrTrunc(NewWidth); |
| Chris Lattner | 3a370bf | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 12927 | InitVal.setIsSigned(NewSign); |
| 12928 | ECD->setInitVal(InitVal); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12929 | |
| Chris Lattner | 3a370bf | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 12930 | // Adjust the Expr initializer and type. |
| Abramo Bagnara | 7781543 | 2010-12-17 15:49:53 +0000 | [diff] [blame] | 12931 | if (ECD->getInitExpr() && |
| Nick Lewycky | 0bdf13e | 2011-07-02 02:05:12 +0000 | [diff] [blame] | 12932 | !Context.hasSameType(NewTy, ECD->getInitExpr()->getType())) |
| John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 12933 | ECD->setInitExpr(ImplicitCastExpr::Create(Context, NewTy, |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 12934 | CK_IntegralCast, |
| John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 12935 | ECD->getInitExpr(), |
| 12936 | /*base paths*/ 0, |
| John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 12937 | VK_RValue)); |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 12938 | if (getLangOpts().CPlusPlus) |
| Douglas Gregor | 1d248c5 | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 12939 | // C++ [dcl.enum]p4: Following the closing brace of an |
| 12940 | // enum-specifier, each enumerator has the type of its |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12941 | // enumeration. |
| Douglas Gregor | 1d248c5 | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 12942 | ECD->setType(EnumType); |
| 12943 | else |
| 12944 | ECD->setType(NewTy); |
| Chris Lattner | 3a370bf | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 12945 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12946 | |
| John McCall | 9aa35be | 2010-05-06 08:49:23 +0000 | [diff] [blame] | 12947 | Enum->completeDefinition(BestType, BestPromotionType, |
| 12948 | NumPositiveBits, NumNegativeBits); |
| James Molloy | 6f8780b | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 12949 | |
| Dmitri Gribenko | e5fde99 | 2013-04-27 20:23:52 +0000 | [diff] [blame] | 12950 | CheckForDuplicateEnumValues(*this, Elements, Enum, EnumType); |
| Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 12951 | |
| 12952 | // Now that the enum type is defined, ensure it's not been underaligned. |
| 12953 | if (Enum->hasAttrs()) |
| 12954 | CheckAlignasUnderalignment(Enum); |
| Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 12955 | } |
| Chris Lattner | 1300fb9 | 2007-01-23 23:42:53 +0000 | [diff] [blame] | 12956 | |
| Abramo Bagnara | 348823a | 2011-03-03 14:20:18 +0000 | [diff] [blame] | 12957 | Decl *Sema::ActOnFileScopeAsmDecl(Expr *expr, |
| 12958 | SourceLocation StartLoc, |
| 12959 | SourceLocation EndLoc) { |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 12960 | StringLiteral *AsmString = cast<StringLiteral>(expr); |
| Sebastian Redl | c675bab | 2008-12-13 16:23:55 +0000 | [diff] [blame] | 12961 | |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 12962 | FileScopeAsmDecl *New = FileScopeAsmDecl::Create(Context, CurContext, |
| Abramo Bagnara | 348823a | 2011-03-03 14:20:18 +0000 | [diff] [blame] | 12963 | AsmString, StartLoc, |
| 12964 | EndLoc); |
| Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 12965 | CurContext->addDecl(New); |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 12966 | return New; |
| Anders Carlsson | 5c6c059 | 2008-02-08 00:33:21 +0000 | [diff] [blame] | 12967 | } |
| Eli Friedman | 5ed5198 | 2009-06-05 02:44:36 +0000 | [diff] [blame] | 12968 | |
| Richard Smith | 7794486 | 2014-03-02 05:58:18 +0000 | [diff] [blame] | 12969 | static void checkModuleImportContext(Sema &S, Module *M, |
| 12970 | SourceLocation ImportLoc, |
| 12971 | DeclContext *DC) { |
| 12972 | if (auto *LSD = dyn_cast<LinkageSpecDecl>(DC)) { |
| 12973 | switch (LSD->getLanguage()) { |
| 12974 | case LinkageSpecDecl::lang_c: |
| 12975 | if (!M->IsExternC) { |
| 12976 | S.Diag(ImportLoc, diag::err_module_import_in_extern_c) |
| 12977 | << M->getFullModuleName(); |
| 12978 | S.Diag(LSD->getLocStart(), diag::note_module_import_in_extern_c); |
| 12979 | return; |
| 12980 | } |
| 12981 | break; |
| 12982 | case LinkageSpecDecl::lang_cxx: |
| 12983 | break; |
| 12984 | } |
| 12985 | DC = LSD->getParent(); |
| 12986 | } |
| 12987 | |
| 12988 | while (isa<LinkageSpecDecl>(DC)) |
| 12989 | DC = DC->getParent(); |
| 12990 | if (!isa<TranslationUnitDecl>(DC)) { |
| 12991 | S.Diag(ImportLoc, diag::err_module_import_not_at_top_level) |
| 12992 | << M->getFullModuleName() << DC; |
| 12993 | S.Diag(cast<Decl>(DC)->getLocStart(), |
| 12994 | diag::note_module_import_not_at_top_level) |
| 12995 | << DC; |
| 12996 | } |
| 12997 | } |
| 12998 | |
| Douglas Gregor | 22d0974 | 2012-01-03 18:04:46 +0000 | [diff] [blame] | 12999 | DeclResult Sema::ActOnModuleImport(SourceLocation AtLoc, |
| 13000 | SourceLocation ImportLoc, |
| 13001 | ModuleIdPath Path) { |
| Douglas Gregor | ff2be53 | 2011-12-01 17:11:21 +0000 | [diff] [blame] | 13002 | Module *Mod = PP.getModuleLoader().loadModule(ImportLoc, Path, |
| Douglas Gregor | bcfc7d0 | 2011-12-02 23:42:12 +0000 | [diff] [blame] | 13003 | Module::AllVisible, |
| 13004 | /*IsIncludeDirective=*/false); |
| Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 13005 | if (!Mod) |
| Douglas Gregor | 0814253 | 2011-08-26 23:56:07 +0000 | [diff] [blame] | 13006 | return true; |
| Richard Smith | 7794486 | 2014-03-02 05:58:18 +0000 | [diff] [blame] | 13007 | |
| 13008 | checkModuleImportContext(*this, Mod, ImportLoc, CurContext); |
| 13009 | |
| Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 13010 | SmallVector<SourceLocation, 2> IdentifierLocs; |
| Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 13011 | Module *ModCheck = Mod; |
| 13012 | for (unsigned I = 0, N = Path.size(); I != N; ++I) { |
| 13013 | // If we've run out of module parents, just drop the remaining identifiers. |
| 13014 | // We need the length to be consistent. |
| 13015 | if (!ModCheck) |
| 13016 | break; |
| 13017 | ModCheck = ModCheck->Parent; |
| 13018 | |
| 13019 | IdentifierLocs.push_back(Path[I].second); |
| 13020 | } |
| 13021 | |
| 13022 | ImportDecl *Import = ImportDecl::Create(Context, |
| 13023 | Context.getTranslationUnitDecl(), |
| Douglas Gregor | 22d0974 | 2012-01-03 18:04:46 +0000 | [diff] [blame] | 13024 | AtLoc.isValid()? AtLoc : ImportLoc, |
| 13025 | Mod, IdentifierLocs); |
| Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 13026 | Context.getTranslationUnitDecl()->addDecl(Import); |
| 13027 | return Import; |
| Douglas Gregor | 0814253 | 2011-08-26 23:56:07 +0000 | [diff] [blame] | 13028 | } |
| 13029 | |
| Richard Smith | ce587f5 | 2013-11-15 04:24:58 +0000 | [diff] [blame] | 13030 | void Sema::ActOnModuleInclude(SourceLocation DirectiveLoc, Module *Mod) { |
| Richard Smith | 7794486 | 2014-03-02 05:58:18 +0000 | [diff] [blame] | 13031 | checkModuleImportContext(*this, Mod, DirectiveLoc, CurContext); |
| 13032 | |
| Richard Smith | ce587f5 | 2013-11-15 04:24:58 +0000 | [diff] [blame] | 13033 | // FIXME: Should we synthesize an ImportDecl here? |
| 13034 | PP.getModuleLoader().makeModuleVisible(Mod, Module::AllVisible, DirectiveLoc, |
| 13035 | /*Complain=*/true); |
| 13036 | } |
| 13037 | |
| Douglas Gregor | c147b0b | 2013-01-12 01:29:50 +0000 | [diff] [blame] | 13038 | void Sema::createImplicitModuleImport(SourceLocation Loc, Module *Mod) { |
| 13039 | // Create the implicit import declaration. |
| 13040 | TranslationUnitDecl *TU = getASTContext().getTranslationUnitDecl(); |
| 13041 | ImportDecl *ImportD = ImportDecl::CreateImplicit(getASTContext(), TU, |
| 13042 | Loc, Mod, Loc); |
| 13043 | TU->addDecl(ImportD); |
| 13044 | Consumer.HandleImplicitImportDecl(ImportD); |
| 13045 | |
| 13046 | // Make the module visible. |
| Douglas Gregor | fb91265 | 2013-03-20 21:10:35 +0000 | [diff] [blame] | 13047 | PP.getModuleLoader().makeModuleVisible(Mod, Module::AllVisible, Loc, |
| 13048 | /*Complain=*/false); |
| Douglas Gregor | c147b0b | 2013-01-12 01:29:50 +0000 | [diff] [blame] | 13049 | } |
| 13050 | |
| David Chisnall | 0867d9c | 2012-02-18 16:12:34 +0000 | [diff] [blame] | 13051 | void Sema::ActOnPragmaRedefineExtname(IdentifierInfo* Name, |
| 13052 | IdentifierInfo* AliasName, |
| 13053 | SourceLocation PragmaLoc, |
| 13054 | SourceLocation NameLoc, |
| 13055 | SourceLocation AliasNameLoc) { |
| 13056 | Decl *PrevDecl = LookupSingleName(TUScope, Name, NameLoc, |
| 13057 | LookupOrdinaryName); |
| Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 13058 | AsmLabelAttr *Attr = ::new (Context) AsmLabelAttr(AliasNameLoc, Context, |
| 13059 | AliasName->getName(), 0); |
| David Chisnall | 0867d9c | 2012-02-18 16:12:34 +0000 | [diff] [blame] | 13060 | |
| 13061 | if (PrevDecl) |
| 13062 | PrevDecl->addAttr(Attr); |
| 13063 | else |
| 13064 | (void)ExtnameUndeclaredIdentifiers.insert( |
| 13065 | std::pair<IdentifierInfo*,AsmLabelAttr*>(Name, Attr)); |
| 13066 | } |
| 13067 | |
| Eli Friedman | 5ed5198 | 2009-06-05 02:44:36 +0000 | [diff] [blame] | 13068 | void Sema::ActOnPragmaWeakID(IdentifierInfo* Name, |
| 13069 | SourceLocation PragmaLoc, |
| 13070 | SourceLocation NameLoc) { |
| Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 13071 | Decl *PrevDecl = LookupSingleName(TUScope, Name, NameLoc, LookupOrdinaryName); |
| Eli Friedman | 5ed5198 | 2009-06-05 02:44:36 +0000 | [diff] [blame] | 13072 | |
| Eli Friedman | 5ed5198 | 2009-06-05 02:44:36 +0000 | [diff] [blame] | 13073 | if (PrevDecl) { |
| Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 13074 | PrevDecl->addAttr(WeakAttr::CreateImplicit(Context, PragmaLoc)); |
| Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 13075 | } else { |
| 13076 | (void)WeakUndeclaredIdentifiers.insert( |
| 13077 | std::pair<IdentifierInfo*,WeakInfo> |
| 13078 | (Name, WeakInfo((IdentifierInfo*)0, NameLoc))); |
| Eli Friedman | 5ed5198 | 2009-06-05 02:44:36 +0000 | [diff] [blame] | 13079 | } |
| Eli Friedman | 5ed5198 | 2009-06-05 02:44:36 +0000 | [diff] [blame] | 13080 | } |
| 13081 | |
| 13082 | void Sema::ActOnPragmaWeakAlias(IdentifierInfo* Name, |
| 13083 | IdentifierInfo* AliasName, |
| 13084 | SourceLocation PragmaLoc, |
| 13085 | SourceLocation NameLoc, |
| 13086 | SourceLocation AliasNameLoc) { |
| Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 13087 | Decl *PrevDecl = LookupSingleName(TUScope, AliasName, AliasNameLoc, |
| 13088 | LookupOrdinaryName); |
| Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 13089 | WeakInfo W = WeakInfo(Name, NameLoc); |
| Eli Friedman | 5ed5198 | 2009-06-05 02:44:36 +0000 | [diff] [blame] | 13090 | |
| Eli Friedman | 5ed5198 | 2009-06-05 02:44:36 +0000 | [diff] [blame] | 13091 | if (PrevDecl) { |
| Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 13092 | if (!PrevDecl->hasAttr<AliasAttr>()) |
| 13093 | if (NamedDecl *ND = dyn_cast<NamedDecl>(PrevDecl)) |
| Ryan Flynn | d963a49 | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 13094 | DeclApplyPragmaWeak(TUScope, ND, W); |
| Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 13095 | } else { |
| 13096 | (void)WeakUndeclaredIdentifiers.insert( |
| 13097 | std::pair<IdentifierInfo*,WeakInfo>(AliasName, W)); |
| Eli Friedman | 5ed5198 | 2009-06-05 02:44:36 +0000 | [diff] [blame] | 13098 | } |
| Eli Friedman | 5ed5198 | 2009-06-05 02:44:36 +0000 | [diff] [blame] | 13099 | } |
| Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 13100 | |
| 13101 | Decl *Sema::getObjCDeclContext() const { |
| 13102 | return (dyn_cast_or_null<ObjCContainerDecl>(CurContext)); |
| 13103 | } |
| Argyrios Kyrtzidis | c281c96 | 2011-10-06 23:23:27 +0000 | [diff] [blame] | 13104 | |
| 13105 | AvailabilityResult Sema::getCurContextAvailability() const { |
| Fariborz Jahanian | 979780f | 2012-09-06 18:38:58 +0000 | [diff] [blame] | 13106 | const Decl *D = cast<Decl>(getCurObjCLexicalContext()); |
| Ted Kremenek | cb42dbe | 2013-11-20 17:24:03 +0000 | [diff] [blame] | 13107 | // If we are within an Objective-C method, we should consult |
| 13108 | // both the availability of the method as well as the |
| 13109 | // enclosing class. If the class is (say) deprecated, |
| 13110 | // the entire method is considered deprecated from the |
| 13111 | // purpose of checking if the current context is deprecated. |
| 13112 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
| 13113 | AvailabilityResult R = MD->getAvailability(); |
| 13114 | if (R != AR_Available) |
| 13115 | return R; |
| 13116 | D = MD->getClassInterface(); |
| 13117 | } |
| 13118 | // If we are within an Objective-c @implementation, it |
| 13119 | // gets the same availability context as the @interface. |
| 13120 | else if (const ObjCImplementationDecl *ID = |
| 13121 | dyn_cast<ObjCImplementationDecl>(D)) { |
| 13122 | D = ID->getClassInterface(); |
| 13123 | } |
| Argyrios Kyrtzidis | c281c96 | 2011-10-06 23:23:27 +0000 | [diff] [blame] | 13124 | return D->getAvailability(); |
| 13125 | } |