| 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" |
| Richard Smith | 65ebb4a | 2015-03-26 04:09:53 +0000 | [diff] [blame] | 19 | #include "clang/AST/ASTMutationListener.h" |
| Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 20 | #include "clang/AST/CXXInheritance.h" |
| Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 21 | #include "clang/AST/CharUnits.h" |
| Dmitri Gribenko | f26054f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 22 | #include "clang/AST/CommentDiagnostic.h" |
| John McCall | 28a0cf7 | 2010-08-25 07:42:41 +0000 | [diff] [blame] | 23 | #include "clang/AST/DeclCXX.h" |
| John McCall | de6836a | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 24 | #include "clang/AST/DeclObjC.h" |
| Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 25 | #include "clang/AST/DeclTemplate.h" |
| Chandler Carruth | 33bf3e7 | 2011-03-27 09:46:56 +0000 | [diff] [blame] | 26 | #include "clang/AST/EvaluatedExprVisitor.h" |
| Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 27 | #include "clang/AST/ExprCXX.h" |
| Sebastian Redl | a7b98a7 | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 28 | #include "clang/AST/StmtCXX.h" |
| Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 29 | #include "clang/Basic/Builtins.h" |
| Anders Carlsson | d624e16 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 30 | #include "clang/Basic/PartialDiagnostic.h" |
| Steve Naroff | e101f95 | 2008-01-30 23:46:05 +0000 | [diff] [blame] | 31 | #include "clang/Basic/SourceManager.h" |
| Anders Carlsson | d624e16 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 32 | #include "clang/Basic/TargetInfo.h" |
| Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 33 | #include "clang/Lex/HeaderSearch.h" // TODO: Sema shouldn't depend on Lex |
| 34 | #include "clang/Lex/Lexer.h" // TODO: Extract static functions to fix layering. |
| 35 | #include "clang/Lex/ModuleLoader.h" // TODO: Sema shouldn't depend on Lex |
| 36 | #include "clang/Lex/Preprocessor.h" // Included for isCodeCompletionEnabled() |
| Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 37 | #include "clang/Parse/ParseDiagnostic.h" |
| 38 | #include "clang/Sema/CXXFieldCollector.h" |
| 39 | #include "clang/Sema/DeclSpec.h" |
| 40 | #include "clang/Sema/DelayedDiagnostic.h" |
| 41 | #include "clang/Sema/Initialization.h" |
| 42 | #include "clang/Sema/Lookup.h" |
| 43 | #include "clang/Sema/ParsedTemplate.h" |
| 44 | #include "clang/Sema/Scope.h" |
| 45 | #include "clang/Sema/ScopeInfo.h" |
| Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 46 | #include "clang/Sema/Template.h" |
| Benjamin Kramer | 4903802 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 47 | #include "llvm/ADT/SmallString.h" |
| John McCall | 0e21fcc | 2009-12-24 09:58:38 +0000 | [diff] [blame] | 48 | #include "llvm/ADT/Triple.h" |
| Douglas Gregor | 8b9ccca | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 49 | #include <algorithm> |
| Douglas Gregor | 56fbc37 | 2009-09-28 21:14:19 +0000 | [diff] [blame] | 50 | #include <cstring> |
| Douglas Gregor | 8b9ccca | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 51 | #include <functional> |
| Chris Lattner | 697e5d6 | 2006-11-09 06:32:27 +0000 | [diff] [blame] | 52 | using namespace clang; |
| John McCall | aab3e41 | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 53 | using namespace sema; |
| Chris Lattner | 697e5d6 | 2006-11-09 06:32:27 +0000 | [diff] [blame] | 54 | |
| Richard Smith | cd1c055 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 55 | Sema::DeclGroupPtrTy Sema::ConvertDeclToDeclGroup(Decl *Ptr, Decl *OwnedType) { |
| 56 | if (OwnedType) { |
| 57 | Decl *Group[2] = { OwnedType, Ptr }; |
| 58 | return DeclGroupPtrTy::make(DeclGroupRef::Create(Context, Group, 2)); |
| 59 | } |
| 60 | |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 61 | return DeclGroupPtrTy::make(DeclGroupRef(Ptr)); |
| Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| Kaelyn Uhrain | b137840 | 2012-01-18 21:41:41 +0000 | [diff] [blame] | 64 | namespace { |
| 65 | |
| 66 | class TypeNameValidatorCCC : public CorrectionCandidateCallback { |
| 67 | public: |
| Kaelyn Uhrain | 67b44c9 | 2014-02-13 20:14:07 +0000 | [diff] [blame] | 68 | TypeNameValidatorCCC(bool AllowInvalid, bool WantClass=false, |
| 69 | bool AllowTemplates=false) |
| 70 | : AllowInvalidDecl(AllowInvalid), WantClassName(WantClass), |
| 71 | AllowClassTemplates(AllowTemplates) { |
| Kaelyn Uhrain | b137840 | 2012-01-18 21:41:41 +0000 | [diff] [blame] | 72 | WantExpressionKeywords = false; |
| 73 | WantCXXNamedCasts = false; |
| 74 | WantRemainingKeywords = false; |
| 75 | } |
| 76 | |
| Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 77 | bool ValidateCandidate(const TypoCorrection &candidate) override { |
| Kaelyn Uhrain | 67b44c9 | 2014-02-13 20:14:07 +0000 | [diff] [blame] | 78 | if (NamedDecl *ND = candidate.getCorrectionDecl()) { |
| 79 | bool IsType = isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND); |
| 80 | bool AllowedTemplate = AllowClassTemplates && isa<ClassTemplateDecl>(ND); |
| 81 | return (IsType || AllowedTemplate) && |
| 82 | (AllowInvalidDecl || !ND->isInvalidDecl()); |
| 83 | } |
| 84 | return !WantClassName && candidate.isKeyword(); |
| Kaelyn Uhrain | b137840 | 2012-01-18 21:41:41 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | private: |
| 88 | bool AllowInvalidDecl; |
| Kaelyn Uhrain | 9cb8e9f | 2012-06-22 23:37:05 +0000 | [diff] [blame] | 89 | bool WantClassName; |
| Kaelyn Uhrain | 67b44c9 | 2014-02-13 20:14:07 +0000 | [diff] [blame] | 90 | bool AllowClassTemplates; |
| Kaelyn Uhrain | b137840 | 2012-01-18 21:41:41 +0000 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | } |
| 94 | |
| Kaelyn Uhrain | 237c7d3 | 2012-06-15 23:45:51 +0000 | [diff] [blame] | 95 | /// \brief Determine whether the token kind starts a simple-type-specifier. |
| 96 | bool Sema::isSimpleTypeSpecifier(tok::TokenKind Kind) const { |
| 97 | switch (Kind) { |
| 98 | // FIXME: Take into account the current language when deciding whether a |
| 99 | // token kind is a valid type specifier |
| 100 | case tok::kw_short: |
| 101 | case tok::kw_long: |
| 102 | case tok::kw___int64: |
| 103 | case tok::kw___int128: |
| 104 | case tok::kw_signed: |
| 105 | case tok::kw_unsigned: |
| 106 | case tok::kw_void: |
| 107 | case tok::kw_char: |
| 108 | case tok::kw_int: |
| 109 | case tok::kw_half: |
| 110 | case tok::kw_float: |
| 111 | case tok::kw_double: |
| 112 | case tok::kw_wchar_t: |
| 113 | case tok::kw_bool: |
| 114 | case tok::kw___underlying_type: |
| 115 | return true; |
| 116 | |
| 117 | case tok::annot_typename: |
| 118 | case tok::kw_char16_t: |
| 119 | case tok::kw_char32_t: |
| 120 | case tok::kw_typeof: |
| David Majnemer | a5e9255 | 2013-09-22 01:24:26 +0000 | [diff] [blame] | 121 | case tok::annot_decltype: |
| Kaelyn Uhrain | 237c7d3 | 2012-06-15 23:45:51 +0000 | [diff] [blame] | 122 | case tok::kw_decltype: |
| 123 | return getLangOpts().CPlusPlus; |
| 124 | |
| 125 | default: |
| 126 | break; |
| 127 | } |
| 128 | |
| 129 | return false; |
| 130 | } |
| 131 | |
| Alexey Bataev | 054829b | 2015-02-19 04:28:23 +0000 | [diff] [blame] | 132 | namespace { |
| 133 | enum class UnqualifiedTypeNameLookupResult { |
| 134 | NotFound, |
| 135 | FoundNonType, |
| 136 | FoundType |
| 137 | }; |
| 138 | } // namespace |
| 139 | |
| 140 | /// \brief Tries to perform unqualified lookup of the type decls in bases for |
| 141 | /// dependent class. |
| 142 | /// \return \a NotFound if no any decls is found, \a FoundNotType if found not a |
| 143 | /// type decl, \a FoundType if only type decls are found. |
| 144 | static UnqualifiedTypeNameLookupResult |
| 145 | lookupUnqualifiedTypeNameInBase(Sema &S, const IdentifierInfo &II, |
| 146 | SourceLocation NameLoc, |
| 147 | const CXXRecordDecl *RD) { |
| 148 | if (!RD->hasDefinition()) |
| 149 | return UnqualifiedTypeNameLookupResult::NotFound; |
| 150 | // Look for type decls in base classes. |
| 151 | UnqualifiedTypeNameLookupResult FoundTypeDecl = |
| 152 | UnqualifiedTypeNameLookupResult::NotFound; |
| 153 | for (const auto &Base : RD->bases()) { |
| 154 | const CXXRecordDecl *BaseRD = nullptr; |
| 155 | if (auto *BaseTT = Base.getType()->getAs<TagType>()) |
| 156 | BaseRD = BaseTT->getAsCXXRecordDecl(); |
| 157 | else if (auto *TST = Base.getType()->getAs<TemplateSpecializationType>()) { |
| 158 | // Look for type decls in dependent base classes that have known primary |
| 159 | // templates. |
| 160 | if (!TST || !TST->isDependentType()) |
| 161 | continue; |
| 162 | auto *TD = TST->getTemplateName().getAsTemplateDecl(); |
| 163 | if (!TD) |
| 164 | continue; |
| 165 | auto *BasePrimaryTemplate = |
| 166 | dyn_cast_or_null<CXXRecordDecl>(TD->getTemplatedDecl()); |
| 167 | if (!BasePrimaryTemplate) |
| 168 | continue; |
| 169 | BaseRD = BasePrimaryTemplate; |
| 170 | } |
| 171 | if (BaseRD) { |
| 172 | for (NamedDecl *ND : BaseRD->lookup(&II)) { |
| 173 | if (!isa<TypeDecl>(ND)) |
| 174 | return UnqualifiedTypeNameLookupResult::FoundNonType; |
| 175 | FoundTypeDecl = UnqualifiedTypeNameLookupResult::FoundType; |
| 176 | } |
| 177 | if (FoundTypeDecl == UnqualifiedTypeNameLookupResult::NotFound) { |
| 178 | switch (lookupUnqualifiedTypeNameInBase(S, II, NameLoc, BaseRD)) { |
| 179 | case UnqualifiedTypeNameLookupResult::FoundNonType: |
| 180 | return UnqualifiedTypeNameLookupResult::FoundNonType; |
| 181 | case UnqualifiedTypeNameLookupResult::FoundType: |
| 182 | FoundTypeDecl = UnqualifiedTypeNameLookupResult::FoundType; |
| 183 | break; |
| 184 | case UnqualifiedTypeNameLookupResult::NotFound: |
| 185 | break; |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | return FoundTypeDecl; |
| 192 | } |
| 193 | |
| Reid Kleckner | 1ba38f8 | 2014-07-08 20:05:48 +0000 | [diff] [blame] | 194 | static ParsedType recoverFromTypeInKnownDependentBase(Sema &S, |
| 195 | const IdentifierInfo &II, |
| 196 | SourceLocation NameLoc) { |
| Alexey Bataev | 054829b | 2015-02-19 04:28:23 +0000 | [diff] [blame] | 197 | // Lookup in the parent class template context, if any. |
| Reid Kleckner | fd06827 | 2014-07-08 21:35:03 +0000 | [diff] [blame] | 198 | const CXXRecordDecl *RD = nullptr; |
| Alexey Bataev | 054829b | 2015-02-19 04:28:23 +0000 | [diff] [blame] | 199 | UnqualifiedTypeNameLookupResult FoundTypeDecl = |
| 200 | UnqualifiedTypeNameLookupResult::NotFound; |
| 201 | for (DeclContext *DC = S.CurContext; |
| 202 | DC && FoundTypeDecl == UnqualifiedTypeNameLookupResult::NotFound; |
| 203 | DC = DC->getParent()) { |
| 204 | // Look for type decls in dependent base classes that have known primary |
| 205 | // templates. |
| Reid Kleckner | fd06827 | 2014-07-08 21:35:03 +0000 | [diff] [blame] | 206 | RD = dyn_cast<CXXRecordDecl>(DC); |
| 207 | if (RD && RD->getDescribedClassTemplate()) |
| Alexey Bataev | 054829b | 2015-02-19 04:28:23 +0000 | [diff] [blame] | 208 | FoundTypeDecl = lookupUnqualifiedTypeNameInBase(S, II, NameLoc, RD); |
| Reid Kleckner | fd06827 | 2014-07-08 21:35:03 +0000 | [diff] [blame] | 209 | } |
| Alexey Bataev | 054829b | 2015-02-19 04:28:23 +0000 | [diff] [blame] | 210 | if (FoundTypeDecl != UnqualifiedTypeNameLookupResult::FoundType) |
| Reid Kleckner | 1ba38f8 | 2014-07-08 20:05:48 +0000 | [diff] [blame] | 211 | return ParsedType(); |
| 212 | |
| 213 | // We found some types in dependent base classes. Recover as if the user |
| 214 | // wrote 'typename MyClass::II' instead of 'II'. We'll fully resolve the |
| 215 | // lookup during template instantiation. |
| 216 | S.Diag(NameLoc, diag::ext_found_via_dependent_bases_lookup) << &II; |
| 217 | |
| 218 | ASTContext &Context = S.Context; |
| 219 | auto *NNS = NestedNameSpecifier::Create(Context, nullptr, false, |
| 220 | cast<Type>(Context.getRecordType(RD))); |
| 221 | QualType T = Context.getDependentNameType(ETK_Typename, NNS, &II); |
| 222 | |
| 223 | CXXScopeSpec SS; |
| 224 | SS.MakeTrivial(Context, NNS, SourceRange(NameLoc)); |
| 225 | |
| 226 | TypeLocBuilder Builder; |
| 227 | DependentNameTypeLoc DepTL = Builder.push<DependentNameTypeLoc>(T); |
| 228 | DepTL.setNameLoc(NameLoc); |
| 229 | DepTL.setElaboratedKeywordLoc(SourceLocation()); |
| 230 | DepTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 231 | return S.CreateParsedType(T, Builder.getTypeSourceInfo(Context, T)); |
| 232 | } |
| 233 | |
| Douglas Gregor | ec6e189 | 2009-02-04 19:16:12 +0000 | [diff] [blame] | 234 | /// \brief If the identifier refers to a type name within this scope, |
| 235 | /// return the declaration of that type. |
| 236 | /// |
| 237 | /// This routine performs ordinary name lookup of the identifier II |
| 238 | /// within the given scope, with optional C++ scope specifier SS, to |
| Douglas Gregor | 9817f4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 239 | /// determine whether the name refers to a type. If so, returns an |
| 240 | /// opaque pointer (actually a QualType) corresponding to that |
| 241 | /// type. Otherwise, returns NULL. |
| Dmitri Gribenko | 5267fdf | 2013-05-03 13:12:11 +0000 | [diff] [blame] | 242 | ParsedType Sema::getTypeName(const IdentifierInfo &II, SourceLocation NameLoc, |
| John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 243 | Scope *S, CXXScopeSpec *SS, |
| Fariborz Jahanian | 8796742 | 2011-02-08 18:05:59 +0000 | [diff] [blame] | 244 | bool isClassName, bool HasTrailingDot, |
| Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 245 | ParsedType ObjectTypePtr, |
| Abramo Bagnara | 4244b43 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 246 | bool IsCtorOrDtorName, |
| Kaelyn Uhrain | 85308c6 | 2011-10-11 01:02:41 +0000 | [diff] [blame] | 247 | bool WantNontrivialTypeSourceInfo, |
| 248 | IdentifierInfo **CorrectedII) { |
| Douglas Gregor | a25d65d | 2009-11-20 22:03:38 +0000 | [diff] [blame] | 249 | // Determine where we will perform name lookup. |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 250 | DeclContext *LookupCtx = nullptr; |
| Douglas Gregor | a25d65d | 2009-11-20 22:03:38 +0000 | [diff] [blame] | 251 | if (ObjectTypePtr) { |
| John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 252 | QualType ObjectType = ObjectTypePtr.get(); |
| Douglas Gregor | a25d65d | 2009-11-20 22:03:38 +0000 | [diff] [blame] | 253 | if (ObjectType->isRecordType()) |
| 254 | LookupCtx = computeDeclContext(ObjectType); |
| Jeffrey Yasskin | 4e150f8 | 2010-04-07 23:29:58 +0000 | [diff] [blame] | 255 | } else if (SS && SS->isNotEmpty()) { |
| Douglas Gregor | a25d65d | 2009-11-20 22:03:38 +0000 | [diff] [blame] | 256 | LookupCtx = computeDeclContext(*SS, false); |
| 257 | |
| 258 | if (!LookupCtx) { |
| 259 | if (isDependentScopeSpecifier(*SS)) { |
| 260 | // C++ [temp.res]p3: |
| 261 | // A qualified-id that refers to a type and in which the |
| 262 | // nested-name-specifier depends on a template-parameter (14.6.2) |
| 263 | // shall be prefixed by the keyword typename to indicate that the |
| 264 | // qualified-id denotes a type, forming an |
| 265 | // elaborated-type-specifier (7.1.5.3). |
| 266 | // |
| 267 | // We therefore do not perform any name lookup if the result would |
| 268 | // refer to a member of an unknown specialization. |
| Richard Smith | 23d5587 | 2012-04-02 01:30:27 +0000 | [diff] [blame] | 269 | if (!isClassName && !IsCtorOrDtorName) |
| John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 270 | return ParsedType(); |
| Douglas Gregor | a25d65d | 2009-11-20 22:03:38 +0000 | [diff] [blame] | 271 | |
| John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 272 | // We know from the grammar that this name refers to a type, |
| 273 | // so build a dependent node to describe the type. |
| Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 274 | if (WantNontrivialTypeSourceInfo) |
| 275 | return ActOnTypenameType(S, SourceLocation(), *SS, II, NameLoc).get(); |
| 276 | |
| 277 | NestedNameSpecifierLoc QualifierLoc = SS->getWithLocInContext(Context); |
| Nico Weber | dfc5920 | 2014-05-03 22:07:35 +0000 | [diff] [blame] | 278 | QualType T = CheckTypenameType(ETK_None, SourceLocation(), QualifierLoc, |
| 279 | II, NameLoc); |
| 280 | return ParsedType::make(T); |
| Douglas Gregor | a25d65d | 2009-11-20 22:03:38 +0000 | [diff] [blame] | 281 | } |
| 282 | |
| John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 283 | return ParsedType(); |
| Douglas Gregor | a25d65d | 2009-11-20 22:03:38 +0000 | [diff] [blame] | 284 | } |
| 285 | |
| John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 286 | if (!LookupCtx->isDependentContext() && |
| 287 | RequireCompleteDeclContext(*SS, LookupCtx)) |
| John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 288 | return ParsedType(); |
| Douglas Gregor | 5e0962f | 2009-08-26 18:27:52 +0000 | [diff] [blame] | 289 | } |
| Eli Friedman | 9025ec2 | 2009-12-21 01:42:38 +0000 | [diff] [blame] | 290 | |
| 291 | // FIXME: LookupNestedNameSpecifierName isn't the right kind of |
| 292 | // lookup for class-names. |
| 293 | LookupNameKind Kind = isClassName ? LookupNestedNameSpecifierName : |
| 294 | LookupOrdinaryName; |
| 295 | LookupResult Result(*this, &II, NameLoc, Kind); |
| Douglas Gregor | a25d65d | 2009-11-20 22:03:38 +0000 | [diff] [blame] | 296 | if (LookupCtx) { |
| 297 | // Perform "qualified" name lookup into the declaration context we |
| 298 | // computed, which is either the type of the base of a member access |
| 299 | // expression or the declaration context associated with a prior |
| 300 | // nested-name-specifier. |
| 301 | LookupQualifiedName(Result, LookupCtx); |
| Douglas Gregor | c9f9b86 | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 302 | |
| Douglas Gregor | a25d65d | 2009-11-20 22:03:38 +0000 | [diff] [blame] | 303 | if (ObjectTypePtr && Result.empty()) { |
| 304 | // C++ [basic.lookup.classref]p3: |
| 305 | // If the unqualified-id is ~type-name, the type-name is looked up |
| 306 | // in the context of the entire postfix-expression. If the type T of |
| 307 | // the object expression is of a class type C, the type-name is also |
| 308 | // looked up in the scope of class C. At least one of the lookups shall |
| 309 | // find a name that refers to (possibly cv-qualified) T. |
| 310 | LookupName(Result, S); |
| 311 | } |
| 312 | } else { |
| 313 | // Perform unqualified name lookup. |
| 314 | LookupName(Result, S); |
| Reid Kleckner | 1ba38f8 | 2014-07-08 20:05:48 +0000 | [diff] [blame] | 315 | |
| 316 | // For unqualified lookup in a class template in MSVC mode, look into |
| 317 | // dependent base classes where the primary class template is known. |
| 318 | if (Result.empty() && getLangOpts().MSVCCompat && (!SS || SS->isEmpty())) { |
| 319 | if (ParsedType TypeInBase = |
| 320 | recoverFromTypeInKnownDependentBase(*this, II, NameLoc)) |
| 321 | return TypeInBase; |
| 322 | } |
| Douglas Gregor | a25d65d | 2009-11-20 22:03:38 +0000 | [diff] [blame] | 323 | } |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 324 | |
| 325 | NamedDecl *IIDecl = nullptr; |
| John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 326 | switch (Result.getResultKind()) { |
| Chris Lattner | a377833 | 2009-02-16 22:07:16 +0000 | [diff] [blame] | 327 | case LookupResult::NotFound: |
| Douglas Gregor | d0d2ee0 | 2010-01-15 01:44:47 +0000 | [diff] [blame] | 328 | case LookupResult::NotFoundInCurrentInstantiation: |
| Kaelyn Uhrain | 85308c6 | 2011-10-11 01:02:41 +0000 | [diff] [blame] | 329 | if (CorrectedII) { |
| Kaelyn Takata | 89c881b | 2014-10-27 18:07:29 +0000 | [diff] [blame] | 330 | TypoCorrection Correction = CorrectTypo( |
| 331 | Result.getLookupNameInfo(), Kind, S, SS, |
| 332 | llvm::make_unique<TypeNameValidatorCCC>(true, isClassName), |
| 333 | CTK_ErrorRecovery); |
| Kaelyn Uhrain | 85308c6 | 2011-10-11 01:02:41 +0000 | [diff] [blame] | 334 | IdentifierInfo *NewII = Correction.getCorrectionAsIdentifierInfo(); |
| 335 | TemplateTy Template; |
| 336 | bool MemberOfUnknownSpecialization; |
| 337 | UnqualifiedId TemplateName; |
| 338 | TemplateName.setIdentifier(NewII, NameLoc); |
| 339 | NestedNameSpecifier *NNS = Correction.getCorrectionSpecifier(); |
| 340 | CXXScopeSpec NewSS, *NewSSPtr = SS; |
| 341 | if (SS && NNS) { |
| 342 | NewSS.MakeTrivial(Context, NNS, SourceRange(NameLoc)); |
| 343 | NewSSPtr = &NewSS; |
| 344 | } |
| 345 | if (Correction && (NNS || NewII != &II) && |
| 346 | // Ignore a correction to a template type as the to-be-corrected |
| 347 | // identifier is not a template (typo correction for template names |
| 348 | // is handled elsewhere). |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 349 | !(getLangOpts().CPlusPlus && NewSSPtr && |
| Kaelyn Uhrain | 85308c6 | 2011-10-11 01:02:41 +0000 | [diff] [blame] | 350 | isTemplateName(S, *NewSSPtr, false, TemplateName, ParsedType(), |
| 351 | false, Template, MemberOfUnknownSpecialization))) { |
| 352 | ParsedType Ty = getTypeName(*NewII, NameLoc, S, NewSSPtr, |
| 353 | isClassName, HasTrailingDot, ObjectTypePtr, |
| Abramo Bagnara | 4244b43 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 354 | IsCtorOrDtorName, |
| Kaelyn Uhrain | 85308c6 | 2011-10-11 01:02:41 +0000 | [diff] [blame] | 355 | WantNontrivialTypeSourceInfo); |
| 356 | if (Ty) { |
| Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 357 | diagnoseTypo(Correction, |
| 358 | PDiag(diag::err_unknown_type_or_class_name_suggest) |
| 359 | << Result.getLookupName() << isClassName); |
| Kaelyn Uhrain | 85308c6 | 2011-10-11 01:02:41 +0000 | [diff] [blame] | 360 | if (SS && NNS) |
| 361 | SS->MakeTrivial(Context, NNS, SourceRange(NameLoc)); |
| 362 | *CorrectedII = NewII; |
| 363 | return Ty; |
| 364 | } |
| 365 | } |
| 366 | } |
| 367 | // If typo correction failed or was not performed, fall through |
| Chris Lattner | a377833 | 2009-02-16 22:07:16 +0000 | [diff] [blame] | 368 | case LookupResult::FoundOverloaded: |
| John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 369 | case LookupResult::FoundUnresolvedValue: |
| John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 370 | Result.suppressDiagnostics(); |
| John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 371 | return ParsedType(); |
| Douglas Gregor | 8a6be5e | 2009-02-04 17:00:24 +0000 | [diff] [blame] | 372 | |
| Chris Lattner | e40853a | 2009-10-25 22:09:09 +0000 | [diff] [blame] | 373 | case LookupResult::Ambiguous: |
| John McCall | 6538c93 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 374 | // Recover from type-hiding ambiguities by hiding the type. We'll |
| 375 | // do the lookup again when looking for an object, and we can |
| 376 | // diagnose the error then. If we don't do this, then the error |
| 377 | // about hiding the type will be immediately followed by an error |
| 378 | // that only makes sense if the identifier was treated like a type. |
| John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 379 | if (Result.getAmbiguityKind() == LookupResult::AmbiguousTagHiding) { |
| 380 | Result.suppressDiagnostics(); |
| John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 381 | return ParsedType(); |
| John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 382 | } |
| John McCall | 6538c93 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 383 | |
| Douglas Gregor | fe3d7d0 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 384 | // Look to see if we have a type anywhere in the list of results. |
| 385 | for (LookupResult::iterator Res = Result.begin(), ResEnd = Result.end(); |
| 386 | Res != ResEnd; ++Res) { |
| 387 | if (isa<TypeDecl>(*Res) || isa<ObjCInterfaceDecl>(*Res)) { |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 388 | if (!IIDecl || |
| 389 | (*Res)->getLocation().getRawEncoding() < |
| Douglas Gregor | 712a351 | 2009-04-13 15:14:38 +0000 | [diff] [blame] | 390 | IIDecl->getLocation().getRawEncoding()) |
| 391 | IIDecl = *Res; |
| Douglas Gregor | fe3d7d0 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 392 | } |
| 393 | } |
| 394 | |
| 395 | if (!IIDecl) { |
| 396 | // None of the entities we found is a type, so there is no way |
| 397 | // to even assume that the result is a type. In this case, don't |
| 398 | // complain about the ambiguity. The parser will either try to |
| 399 | // perform this lookup again (e.g., as an object name), which |
| 400 | // will produce the ambiguity, or will complain that it expected |
| 401 | // a type name. |
| John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 402 | Result.suppressDiagnostics(); |
| John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 403 | return ParsedType(); |
| Douglas Gregor | fe3d7d0 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | // We found a type within the ambiguous lookup; diagnose the |
| 407 | // ambiguity and then return that type. This might be the right |
| 408 | // answer, or it might not be, but it suppresses any attempt to |
| 409 | // perform the name lookup again. |
| Douglas Gregor | fe3d7d0 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 410 | break; |
| Douglas Gregor | 8a6be5e | 2009-02-04 17:00:24 +0000 | [diff] [blame] | 411 | |
| Chris Lattner | a377833 | 2009-02-16 22:07:16 +0000 | [diff] [blame] | 412 | case LookupResult::Found: |
| John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 413 | IIDecl = Result.getFoundDecl(); |
| Chris Lattner | a377833 | 2009-02-16 22:07:16 +0000 | [diff] [blame] | 414 | break; |
| Douglas Gregor | 960b5bc | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 415 | } |
| 416 | |
| Chris Lattner | 17e15f1 | 2009-10-25 17:16:46 +0000 | [diff] [blame] | 417 | assert(IIDecl && "Didn't find decl"); |
| John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 418 | |
| Chris Lattner | 17e15f1 | 2009-10-25 17:16:46 +0000 | [diff] [blame] | 419 | QualType T; |
| 420 | if (TypeDecl *TD = dyn_cast<TypeDecl>(IIDecl)) { |
| John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 421 | DiagnoseUseOfDecl(IIDecl, NameLoc); |
| John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 422 | |
| Nico Weber | dfc5920 | 2014-05-03 22:07:35 +0000 | [diff] [blame] | 423 | T = Context.getTypeDeclType(TD); |
| Nico Weber | 7288943 | 2014-09-06 01:25:55 +0000 | [diff] [blame] | 424 | MarkAnyDeclReferenced(TD->getLocation(), TD, /*OdrUse=*/false); |
| Abramo Bagnara | 4244b43 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 425 | |
| 426 | // NOTE: avoid constructing an ElaboratedType(Loc) if this is a |
| 427 | // constructor or destructor name (in such a case, the scope specifier |
| 428 | // will be attached to the enclosing Expr or Decl node). |
| 429 | if (SS && SS->isNotEmpty() && !IsCtorOrDtorName) { |
| Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 430 | if (WantNontrivialTypeSourceInfo) { |
| 431 | // Construct a type with type-source information. |
| 432 | TypeLocBuilder Builder; |
| 433 | Builder.pushTypeSpec(T).setNameLoc(NameLoc); |
| 434 | |
| 435 | T = getElaboratedType(ETK_None, *SS, T); |
| 436 | ElaboratedTypeLoc ElabTL = Builder.push<ElaboratedTypeLoc>(T); |
| Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 437 | ElabTL.setElaboratedKeywordLoc(SourceLocation()); |
| Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 438 | ElabTL.setQualifierLoc(SS->getWithLocInContext(Context)); |
| 439 | return CreateParsedType(T, Builder.getTypeSourceInfo(Context, T)); |
| 440 | } else { |
| 441 | T = getElaboratedType(ETK_None, *SS, T); |
| 442 | } |
| 443 | } |
| Chris Lattner | 17e15f1 | 2009-10-25 17:16:46 +0000 | [diff] [blame] | 444 | } else if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(IIDecl)) { |
| Fariborz Jahanian | 08891f5 | 2011-03-08 19:12:46 +0000 | [diff] [blame] | 445 | (void)DiagnoseUseOfDecl(IDecl, NameLoc); |
| Fariborz Jahanian | 8796742 | 2011-02-08 18:05:59 +0000 | [diff] [blame] | 446 | if (!HasTrailingDot) |
| 447 | T = Context.getObjCInterfaceType(IDecl); |
| 448 | } |
| 449 | |
| 450 | if (T.isNull()) { |
| John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 451 | // If it's not plausibly a type, suppress diagnostics. |
| 452 | Result.suppressDiagnostics(); |
| John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 453 | return ParsedType(); |
| John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 454 | } |
| John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 455 | return ParsedType::make(T); |
| Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 456 | } |
| 457 | |
| Reid Kleckner | df6e4a0 | 2014-06-06 22:36:36 +0000 | [diff] [blame] | 458 | // Builds a fake NNS for the given decl context. |
| 459 | static NestedNameSpecifier * |
| 460 | synthesizeCurrentNestedNameSpecifier(ASTContext &Context, DeclContext *DC) { |
| 461 | for (;; DC = DC->getLookupParent()) { |
| 462 | DC = DC->getPrimaryContext(); |
| 463 | auto *ND = dyn_cast<NamespaceDecl>(DC); |
| 464 | if (ND && !ND->isInline() && !ND->isAnonymousNamespace()) |
| 465 | return NestedNameSpecifier::Create(Context, nullptr, ND); |
| 466 | else if (auto *RD = dyn_cast<CXXRecordDecl>(DC)) |
| 467 | return NestedNameSpecifier::Create(Context, nullptr, RD->isTemplateDecl(), |
| 468 | RD->getTypeForDecl()); |
| 469 | else if (isa<TranslationUnitDecl>(DC)) |
| 470 | return NestedNameSpecifier::GlobalSpecifier(Context); |
| 471 | } |
| 472 | llvm_unreachable("something isn't in TU scope?"); |
| 473 | } |
| 474 | |
| 475 | ParsedType Sema::ActOnDelayedDefaultTemplateArg(const IdentifierInfo &II, |
| 476 | SourceLocation NameLoc) { |
| 477 | // Accepting an undeclared identifier as a default argument for a template |
| 478 | // type parameter is a Microsoft extension. |
| 479 | Diag(NameLoc, diag::ext_ms_delayed_template_argument) << &II; |
| 480 | |
| 481 | // Build a fake DependentNameType that will perform lookup into CurContext at |
| 482 | // instantiation time. The name specifier isn't dependent, so template |
| 483 | // instantiation won't transform it. It will retry the lookup, however. |
| 484 | NestedNameSpecifier *NNS = |
| 485 | synthesizeCurrentNestedNameSpecifier(Context, CurContext); |
| 486 | QualType T = Context.getDependentNameType(ETK_None, NNS, &II); |
| 487 | |
| 488 | // Build type location information. We synthesized the qualifier, so we have |
| 489 | // to build a fake NestedNameSpecifierLoc. |
| 490 | NestedNameSpecifierLocBuilder NNSLocBuilder; |
| 491 | NNSLocBuilder.MakeTrivial(Context, NNS, SourceRange(NameLoc)); |
| 492 | NestedNameSpecifierLoc QualifierLoc = NNSLocBuilder.getWithLocInContext(Context); |
| 493 | |
| 494 | TypeLocBuilder Builder; |
| 495 | DependentNameTypeLoc DepTL = Builder.push<DependentNameTypeLoc>(T); |
| 496 | DepTL.setNameLoc(NameLoc); |
| 497 | DepTL.setElaboratedKeywordLoc(SourceLocation()); |
| 498 | DepTL.setQualifierLoc(QualifierLoc); |
| 499 | return CreateParsedType(T, Builder.getTypeSourceInfo(Context, T)); |
| 500 | } |
| 501 | |
| Chris Lattner | ffaa0e6 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 502 | /// isTagName() - This method is called *for error recovery purposes only* |
| 503 | /// to determine if the specified name is a valid tag name ("struct foo"). If |
| 504 | /// 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] | 505 | /// TST_union, TST_struct, TST_interface, TST_class). This is used to diagnose |
| 506 | /// cases in C where the user forgot to specify the tag. |
| Chris Lattner | ffaa0e6 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 507 | DeclSpec::TST Sema::isTagName(IdentifierInfo &II, Scope *S) { |
| 508 | // Do a tag name lookup in this scope. |
| John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 509 | LookupResult R(*this, &II, SourceLocation(), LookupTagName); |
| 510 | LookupName(R, S, false); |
| 511 | R.suppressDiagnostics(); |
| 512 | if (R.getResultKind() == LookupResult::Found) |
| John McCall | 67c0087 | 2009-12-02 08:25:40 +0000 | [diff] [blame] | 513 | if (const TagDecl *TD = R.getAsSingle<TagDecl>()) { |
| Chris Lattner | ffaa0e6 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 514 | switch (TD->getTagKind()) { |
| Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 515 | case TTK_Struct: return DeclSpec::TST_struct; |
| Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 516 | case TTK_Interface: return DeclSpec::TST_interface; |
| Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 517 | case TTK_Union: return DeclSpec::TST_union; |
| 518 | case TTK_Class: return DeclSpec::TST_class; |
| 519 | case TTK_Enum: return DeclSpec::TST_enum; |
| Chris Lattner | ffaa0e6 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 520 | } |
| 521 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 522 | |
| Chris Lattner | ffaa0e6 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 523 | return DeclSpec::TST_unspecified; |
| 524 | } |
| 525 | |
| Francois Pichet | 48c946e | 2011-04-13 02:38:49 +0000 | [diff] [blame] | 526 | /// isMicrosoftMissingTypename - In Microsoft mode, within class scope, |
| 527 | /// if a CXXScopeSpec's type is equal to the type of one of the base classes |
| 528 | /// then downgrade the missing typename error to a warning. |
| 529 | /// This is needed for MSVC compatibility; Example: |
| 530 | /// @code |
| 531 | /// template<class T> class A { |
| 532 | /// public: |
| 533 | /// typedef int TYPE; |
| 534 | /// }; |
| 535 | /// template<class T> class B : public A<T> { |
| 536 | /// public: |
| 537 | /// A<T>::TYPE a; // no typename required because A<T> is a base class. |
| 538 | /// }; |
| 539 | /// @endcode |
| Francois Pichet | 9a57fb5 | 2011-10-11 01:50:09 +0000 | [diff] [blame] | 540 | bool Sema::isMicrosoftMissingTypename(const CXXScopeSpec *SS, Scope *S) { |
| Francois Pichet | 48c946e | 2011-04-13 02:38:49 +0000 | [diff] [blame] | 541 | if (CurContext->isRecord()) { |
| Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 542 | if (SS->getScopeRep()->getKind() == NestedNameSpecifier::Super) |
| 543 | return true; |
| 544 | |
| Francois Pichet | efc283c | 2011-04-13 02:44:57 +0000 | [diff] [blame] | 545 | const Type *Ty = SS->getScopeRep()->getAsType(); |
| Francois Pichet | 48c946e | 2011-04-13 02:38:49 +0000 | [diff] [blame] | 546 | |
| 547 | CXXRecordDecl *RD = cast<CXXRecordDecl>(CurContext); |
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 548 | for (const auto &Base : RD->bases()) |
| 549 | if (Context.hasSameUnqualifiedType(QualType(Ty, 1), Base.getType())) |
| Francois Pichet | 48c946e | 2011-04-13 02:38:49 +0000 | [diff] [blame] | 550 | return true; |
| Francois Pichet | 9a57fb5 | 2011-10-11 01:50:09 +0000 | [diff] [blame] | 551 | return S->isFunctionPrototypeScope(); |
| Francois Pichet | 48c946e | 2011-04-13 02:38:49 +0000 | [diff] [blame] | 552 | } |
| Francois Pichet | 9a57fb5 | 2011-10-11 01:50:09 +0000 | [diff] [blame] | 553 | return CurContext->isFunctionOrMethod() || S->isFunctionPrototypeScope(); |
| Francois Pichet | 48c946e | 2011-04-13 02:38:49 +0000 | [diff] [blame] | 554 | } |
| 555 | |
| Reid Kleckner | c05ca5e | 2014-06-19 01:23:22 +0000 | [diff] [blame] | 556 | void Sema::DiagnoseUnknownTypeName(IdentifierInfo *&II, |
| Douglas Gregor | 15e5602 | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 557 | SourceLocation IILoc, |
| 558 | Scope *S, |
| Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 559 | CXXScopeSpec *SS, |
| Kaelyn Uhrain | 67b44c9 | 2014-02-13 20:14:07 +0000 | [diff] [blame] | 560 | ParsedType &SuggestedType, |
| 561 | bool AllowClassTemplates) { |
| Douglas Gregor | 15e5602 | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 562 | // We don't have anything to suggest (yet). |
| John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 563 | SuggestedType = ParsedType(); |
| Douglas Gregor | 15e5602 | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 564 | |
| Douglas Gregor | 2d43530 | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 565 | // There may have been a typo in the name of the type. Look up typo |
| 566 | // results, in case we have something that we can suggest. |
| Kaelyn Takata | 89c881b | 2014-10-27 18:07:29 +0000 | [diff] [blame] | 567 | if (TypoCorrection Corrected = |
| 568 | CorrectTypo(DeclarationNameInfo(II, IILoc), LookupOrdinaryName, S, SS, |
| 569 | llvm::make_unique<TypeNameValidatorCCC>( |
| 570 | false, false, AllowClassTemplates), |
| 571 | CTK_ErrorRecovery)) { |
| Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 572 | if (Corrected.isKeyword()) { |
| 573 | // We corrected to a keyword. |
| Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 574 | diagnoseTypo(Corrected, PDiag(diag::err_unknown_typename_suggest) << II); |
| 575 | II = Corrected.getCorrectionAsIdentifierInfo(); |
| Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 576 | } else { |
| Kaelyn Uhrain | b137840 | 2012-01-18 21:41:41 +0000 | [diff] [blame] | 577 | // We found a similarly-named type or interface; suggest that. |
| Kaelyn Uhrain | 10413a4 | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 578 | if (!SS || !SS->isSet()) { |
| Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 579 | diagnoseTypo(Corrected, |
| 580 | PDiag(diag::err_unknown_typename_suggest) << II); |
| Kaelyn Uhrain | 10413a4 | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 581 | } else if (DeclContext *DC = computeDeclContext(*SS, false)) { |
| Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 582 | std::string CorrectedStr(Corrected.getAsString(getLangOpts())); |
| 583 | bool DroppedSpecifier = Corrected.WillReplaceSpecifier() && |
| Kaelyn Uhrain | 10413a4 | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 584 | II->getName().equals(CorrectedStr); |
| Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 585 | diagnoseTypo(Corrected, |
| 586 | PDiag(diag::err_unknown_nested_typename_suggest) |
| 587 | << II << DC << DroppedSpecifier << SS->getRange()); |
| 588 | } else { |
| Kaelyn Uhrain | b137840 | 2012-01-18 21:41:41 +0000 | [diff] [blame] | 589 | llvm_unreachable("could not have corrected a typo here"); |
| Kaelyn Uhrain | 10413a4 | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 590 | } |
| Douglas Gregor | 2d43530 | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 591 | |
| Kaelyn Uhrain | f734301 | 2013-09-26 21:13:05 +0000 | [diff] [blame] | 592 | CXXScopeSpec tmpSS; |
| 593 | if (Corrected.getCorrectionSpecifier()) |
| 594 | tmpSS.MakeTrivial(Context, Corrected.getCorrectionSpecifier(), |
| 595 | SourceRange(IILoc)); |
| Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 596 | SuggestedType = getTypeName(*Corrected.getCorrectionAsIdentifierInfo(), |
| Kaelyn Uhrain | f734301 | 2013-09-26 21:13:05 +0000 | [diff] [blame] | 597 | IILoc, S, tmpSS.isSet() ? &tmpSS : SS, false, |
| 598 | false, ParsedType(), |
| Abramo Bagnara | 4244b43 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 599 | /*IsCtorOrDtorName=*/false, |
| Kaelyn Uhrain | b137840 | 2012-01-18 21:41:41 +0000 | [diff] [blame] | 600 | /*NonTrivialTypeSourceInfo=*/true); |
| Douglas Gregor | 2d43530 | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 601 | } |
| Reid Kleckner | c05ca5e | 2014-06-19 01:23:22 +0000 | [diff] [blame] | 602 | return; |
| Douglas Gregor | 2d43530 | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 603 | } |
| 604 | |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 605 | if (getLangOpts().CPlusPlus) { |
| Jeffrey Yasskin | 54eba42 | 2010-04-08 21:04:54 +0000 | [diff] [blame] | 606 | // See if II is a class template that the user forgot to pass arguments to. |
| 607 | UnqualifiedId Name; |
| Kaelyn Uhrain | b5b17fe | 2012-06-15 23:45:58 +0000 | [diff] [blame] | 608 | Name.setIdentifier(II, IILoc); |
| Jeffrey Yasskin | 54eba42 | 2010-04-08 21:04:54 +0000 | [diff] [blame] | 609 | CXXScopeSpec EmptySS; |
| 610 | TemplateTy TemplateResult; |
| Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 611 | bool MemberOfUnknownSpecialization; |
| Abramo Bagnara | 7c5dee4 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 612 | if (isTemplateName(S, SS ? *SS : EmptySS, /*hasTemplateKeyword=*/false, |
| John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 613 | Name, ParsedType(), true, TemplateResult, |
| Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 614 | MemberOfUnknownSpecialization) == TNK_Type_template) { |
| Serge Pavlov | 9ddb76e | 2013-08-27 13:15:56 +0000 | [diff] [blame] | 615 | TemplateName TplName = TemplateResult.get(); |
| Jeffrey Yasskin | 54eba42 | 2010-04-08 21:04:54 +0000 | [diff] [blame] | 616 | Diag(IILoc, diag::err_template_missing_args) << TplName; |
| 617 | if (TemplateDecl *TplDecl = TplName.getAsTemplateDecl()) { |
| 618 | Diag(TplDecl->getLocation(), diag::note_template_decl_here) |
| 619 | << TplDecl->getTemplateParameters()->getSourceRange(); |
| 620 | } |
| Reid Kleckner | c05ca5e | 2014-06-19 01:23:22 +0000 | [diff] [blame] | 621 | return; |
| Jeffrey Yasskin | 54eba42 | 2010-04-08 21:04:54 +0000 | [diff] [blame] | 622 | } |
| 623 | } |
| 624 | |
| Douglas Gregor | 15e5602 | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 625 | // FIXME: Should we move the logic that tries to recover from a missing tag |
| 626 | // (struct, union, enum) from Parser::ParseImplicitInt here, instead? |
| 627 | |
| Douglas Gregor | 2d43530 | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 628 | if (!SS || (!SS->isSet() && !SS->isInvalid())) |
| Kaelyn Uhrain | b5b17fe | 2012-06-15 23:45:58 +0000 | [diff] [blame] | 629 | Diag(IILoc, diag::err_unknown_typename) << II; |
| Douglas Gregor | 15e5602 | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 630 | else if (DeclContext *DC = computeDeclContext(*SS, false)) |
| 631 | Diag(IILoc, diag::err_typename_nested_not_found) |
| Kaelyn Uhrain | b5b17fe | 2012-06-15 23:45:58 +0000 | [diff] [blame] | 632 | << II << DC << SS->getRange(); |
| Douglas Gregor | 15e5602 | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 633 | else if (isDependentScopeSpecifier(*SS)) { |
| Francois Pichet | 48c946e | 2011-04-13 02:38:49 +0000 | [diff] [blame] | 634 | unsigned DiagID = diag::err_typename_missing; |
| Alp Toker | bfa3934 | 2014-01-14 12:51:41 +0000 | [diff] [blame] | 635 | if (getLangOpts().MSVCCompat && isMicrosoftMissingTypename(SS, S)) |
| Reid Kleckner | 32506ed | 2014-06-12 23:03:48 +0000 | [diff] [blame] | 636 | DiagID = diag::ext_typename_missing; |
| Francois Pichet | 48c946e | 2011-04-13 02:38:49 +0000 | [diff] [blame] | 637 | |
| 638 | Diag(SS->getRange().getBegin(), DiagID) |
| Aaron Ballman | 691e227 | 2014-01-03 14:48:20 +0000 | [diff] [blame] | 639 | << SS->getScopeRep() << II->getName() |
| Douglas Gregor | 15e5602 | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 640 | << SourceRange(SS->getRange().getBegin(), IILoc) |
| Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 641 | << FixItHint::CreateInsertion(SS->getRange().getBegin(), "typename "); |
| Kaelyn Uhrain | b5b17fe | 2012-06-15 23:45:58 +0000 | [diff] [blame] | 642 | SuggestedType = ActOnTypenameType(S, SourceLocation(), |
| 643 | *SS, *II, IILoc).get(); |
| Douglas Gregor | 15e5602 | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 644 | } else { |
| 645 | assert(SS && SS->isInvalid() && |
| 646 | "Invalid scope specifier has already been diagnosed"); |
| 647 | } |
| Douglas Gregor | 15e5602 | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 648 | } |
| Chris Lattner | ffaa0e6 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 649 | |
| Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 650 | /// \brief Determine whether the given result set contains either a type name |
| 651 | /// or |
| 652 | static bool isResultTypeOrTemplate(LookupResult &R, const Token &NextToken) { |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 653 | bool CheckTemplate = R.getSema().getLangOpts().CPlusPlus && |
| Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 654 | NextToken.is(tok::less); |
| 655 | |
| 656 | for (LookupResult::iterator I = R.begin(), IEnd = R.end(); I != IEnd; ++I) { |
| 657 | if (isa<TypeDecl>(*I) || isa<ObjCInterfaceDecl>(*I)) |
| 658 | return true; |
| 659 | |
| 660 | if (CheckTemplate && isa<TemplateDecl>(*I)) |
| 661 | return true; |
| 662 | } |
| 663 | |
| 664 | return false; |
| 665 | } |
| 666 | |
| Kaelyn Uhrain | 7179205 | 2012-05-02 00:11:40 +0000 | [diff] [blame] | 667 | static bool isTagTypeWithMissingTag(Sema &SemaRef, LookupResult &Result, |
| 668 | Scope *S, CXXScopeSpec &SS, |
| 669 | IdentifierInfo *&Name, |
| 670 | SourceLocation NameLoc) { |
| Richard Smith | aa31b4b | 2012-09-06 01:37:56 +0000 | [diff] [blame] | 671 | LookupResult R(SemaRef, Name, NameLoc, Sema::LookupTagName); |
| 672 | SemaRef.LookupParsedName(R, S, &SS); |
| 673 | if (TagDecl *Tag = R.getAsSingle<TagDecl>()) { |
| Alp Toker | 6883743 | 2014-05-20 22:03:47 +0000 | [diff] [blame] | 674 | StringRef FixItTagName; |
| Kaelyn Uhrain | 7179205 | 2012-05-02 00:11:40 +0000 | [diff] [blame] | 675 | switch (Tag->getTagKind()) { |
| 676 | case TTK_Class: |
| Kaelyn Uhrain | 7179205 | 2012-05-02 00:11:40 +0000 | [diff] [blame] | 677 | FixItTagName = "class "; |
| 678 | break; |
| 679 | |
| 680 | case TTK_Enum: |
| Kaelyn Uhrain | 7179205 | 2012-05-02 00:11:40 +0000 | [diff] [blame] | 681 | FixItTagName = "enum "; |
| 682 | break; |
| 683 | |
| 684 | case TTK_Struct: |
| Kaelyn Uhrain | 7179205 | 2012-05-02 00:11:40 +0000 | [diff] [blame] | 685 | FixItTagName = "struct "; |
| 686 | break; |
| 687 | |
| Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 688 | case TTK_Interface: |
| Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 689 | FixItTagName = "__interface "; |
| 690 | break; |
| 691 | |
| Kaelyn Uhrain | 7179205 | 2012-05-02 00:11:40 +0000 | [diff] [blame] | 692 | case TTK_Union: |
| Kaelyn Uhrain | 7179205 | 2012-05-02 00:11:40 +0000 | [diff] [blame] | 693 | FixItTagName = "union "; |
| 694 | break; |
| 695 | } |
| 696 | |
| Alp Toker | 6883743 | 2014-05-20 22:03:47 +0000 | [diff] [blame] | 697 | StringRef TagName = FixItTagName.drop_back(); |
| Kaelyn Uhrain | 7179205 | 2012-05-02 00:11:40 +0000 | [diff] [blame] | 698 | SemaRef.Diag(NameLoc, diag::err_use_of_tag_name_without_tag) |
| 699 | << Name << TagName << SemaRef.getLangOpts().CPlusPlus |
| 700 | << FixItHint::CreateInsertion(NameLoc, FixItTagName); |
| 701 | |
| Richard Smith | aa31b4b | 2012-09-06 01:37:56 +0000 | [diff] [blame] | 702 | for (LookupResult::iterator I = Result.begin(), IEnd = Result.end(); |
| 703 | I != IEnd; ++I) |
| 704 | SemaRef.Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type) |
| 705 | << Name << TagName; |
| 706 | |
| 707 | // Replace lookup results with just the tag decl. |
| 708 | Result.clear(Sema::LookupTagName); |
| 709 | SemaRef.LookupParsedName(Result, S, &SS); |
| Kaelyn Uhrain | 7179205 | 2012-05-02 00:11:40 +0000 | [diff] [blame] | 710 | return true; |
| 711 | } |
| 712 | |
| Kaelyn Uhrain | 7179205 | 2012-05-02 00:11:40 +0000 | [diff] [blame] | 713 | return false; |
| 714 | } |
| 715 | |
| Richard Smith | 4f605af | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 716 | /// Build a ParsedType for a simple-type-specifier with a nested-name-specifier. |
| 717 | static ParsedType buildNestedType(Sema &S, CXXScopeSpec &SS, |
| 718 | QualType T, SourceLocation NameLoc) { |
| 719 | ASTContext &Context = S.Context; |
| 720 | |
| 721 | TypeLocBuilder Builder; |
| 722 | Builder.pushTypeSpec(T).setNameLoc(NameLoc); |
| 723 | |
| 724 | T = S.getElaboratedType(ETK_None, SS, T); |
| 725 | ElaboratedTypeLoc ElabTL = Builder.push<ElaboratedTypeLoc>(T); |
| 726 | ElabTL.setElaboratedKeywordLoc(SourceLocation()); |
| 727 | ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 728 | return S.CreateParsedType(T, Builder.getTypeSourceInfo(Context, T)); |
| 729 | } |
| 730 | |
| Kaelyn Takata | 89c881b | 2014-10-27 18:07:29 +0000 | [diff] [blame] | 731 | Sema::NameClassification |
| 732 | Sema::ClassifyName(Scope *S, CXXScopeSpec &SS, IdentifierInfo *&Name, |
| 733 | SourceLocation NameLoc, const Token &NextToken, |
| 734 | bool IsAddressOfOperand, |
| 735 | std::unique_ptr<CorrectionCandidateCallback> CCC) { |
| Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 736 | DeclarationNameInfo NameInfo(Name, NameLoc); |
| 737 | ObjCMethodDecl *CurMethod = getCurMethodDecl(); |
| Serge Pavlov | 6a7ffbe | 2014-04-13 16:52:03 +0000 | [diff] [blame] | 738 | |
| Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 739 | if (NextToken.is(tok::coloncolon)) { |
| 740 | BuildCXXNestedNameSpecifier(S, *Name, NameLoc, NextToken.getLocation(), |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 741 | QualType(), false, SS, nullptr, false); |
| Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 742 | } |
| Serge Pavlov | 6a7ffbe | 2014-04-13 16:52:03 +0000 | [diff] [blame] | 743 | |
| Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 744 | LookupResult Result(*this, Name, NameLoc, LookupOrdinaryName); |
| Nikola Smiljanic | 905bfda | 2014-10-04 10:17:57 +0000 | [diff] [blame] | 745 | LookupParsedName(Result, S, &SS, !CurMethod); |
| Serge Pavlov | 6a7ffbe | 2014-04-13 16:52:03 +0000 | [diff] [blame] | 746 | |
| Reid Kleckner | 1ba38f8 | 2014-07-08 20:05:48 +0000 | [diff] [blame] | 747 | // For unqualified lookup in a class template in MSVC mode, look into |
| 748 | // dependent base classes where the primary class template is known. |
| 749 | if (Result.empty() && SS.isEmpty() && getLangOpts().MSVCCompat) { |
| 750 | if (ParsedType TypeInBase = |
| 751 | recoverFromTypeInKnownDependentBase(*this, *Name, NameLoc)) |
| 752 | return TypeInBase; |
| 753 | } |
| 754 | |
| Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 755 | // Perform lookup for Objective-C instance variables (including automatically |
| 756 | // synthesized instance variables), if we're in an Objective-C method. |
| 757 | // FIXME: This lookup really, really needs to be folded in to the normal |
| 758 | // unqualified lookup mechanism. |
| 759 | if (!SS.isSet() && CurMethod && !isResultTypeOrTemplate(Result, NextToken)) { |
| 760 | ExprResult E = LookupInObjCMethod(Result, S, Name, true); |
| Douglas Gregor | b90f518 | 2011-04-25 15:05:41 +0000 | [diff] [blame] | 761 | if (E.get() || E.isInvalid()) |
| Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 762 | return E; |
| Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | bool SecondTry = false; |
| 766 | bool IsFilteredTemplateName = false; |
| 767 | |
| 768 | Corrected: |
| 769 | switch (Result.getResultKind()) { |
| 770 | case LookupResult::NotFound: |
| 771 | // If an unqualified-id is followed by a '(', then we have a function |
| 772 | // call. |
| 773 | if (!SS.isSet() && NextToken.is(tok::l_paren)) { |
| 774 | // In C++, this is an ADL-only call. |
| 775 | // FIXME: Reference? |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 776 | if (getLangOpts().CPlusPlus) |
| Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 777 | return BuildDeclarationNameExpr(SS, Result, /*ADL=*/true); |
| 778 | |
| 779 | // C90 6.3.2.2: |
| 780 | // If the expression that precedes the parenthesized argument list in a |
| 781 | // function call consists solely of an identifier, and if no |
| 782 | // declaration is visible for this identifier, the identifier is |
| 783 | // implicitly declared exactly as if, in the innermost block containing |
| 784 | // the function call, the declaration |
| 785 | // |
| 786 | // extern int identifier (); |
| 787 | // |
| 788 | // appeared. |
| 789 | // |
| 790 | // We also allow this in C99 as an extension. |
| 791 | if (NamedDecl *D = ImplicitlyDefineFunction(NameLoc, *Name, S)) { |
| 792 | Result.addDecl(D); |
| 793 | Result.resolveKind(); |
| 794 | return BuildDeclarationNameExpr(SS, Result, /*ADL=*/false); |
| 795 | } |
| 796 | } |
| 797 | |
| 798 | // In C, we first see whether there is a tag type by the same name, in |
| 799 | // which case it's likely that the user just forget to write "enum", |
| 800 | // "struct", or "union". |
| Kaelyn Uhrain | 7179205 | 2012-05-02 00:11:40 +0000 | [diff] [blame] | 801 | if (!getLangOpts().CPlusPlus && !SecondTry && |
| 802 | isTagTypeWithMissingTag(*this, Result, S, SS, Name, NameLoc)) { |
| 803 | break; |
| Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 804 | } |
| 805 | |
| 806 | // Perform typo correction to determine if there is another name that is |
| 807 | // close to this name. |
| Richard Smith | 4f605af | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 808 | if (!SecondTry && CCC) { |
| Douglas Gregor | 5cf0e15 | 2011-07-14 04:54:23 +0000 | [diff] [blame] | 809 | SecondTry = true; |
| Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 810 | if (TypoCorrection Corrected = CorrectTypo(Result.getLookupNameInfo(), |
| David Blaikie | 30d1544 | 2011-10-19 22:56:21 +0000 | [diff] [blame] | 811 | Result.getLookupKind(), S, |
| Kaelyn Takata | 89c881b | 2014-10-27 18:07:29 +0000 | [diff] [blame] | 812 | &SS, std::move(CCC), |
| John Thompson | 2255f2c | 2014-04-23 12:57:01 +0000 | [diff] [blame] | 813 | CTK_ErrorRecovery)) { |
| Douglas Gregor | 5e16c16 | 2011-04-27 03:47:06 +0000 | [diff] [blame] | 814 | unsigned UnqualifiedDiag = diag::err_undeclared_var_use_suggest; |
| 815 | unsigned QualifiedDiag = diag::err_no_member_suggest; |
| Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 816 | |
| Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 817 | NamedDecl *FirstDecl = Corrected.getCorrectionDecl(); |
| Douglas Gregor | 8b02cd0 | 2011-04-27 04:48:22 +0000 | [diff] [blame] | 818 | NamedDecl *UnderlyingFirstDecl |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 819 | = FirstDecl? FirstDecl->getUnderlyingDecl() : nullptr; |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 820 | if (getLangOpts().CPlusPlus && NextToken.is(tok::less) && |
| Douglas Gregor | 8b02cd0 | 2011-04-27 04:48:22 +0000 | [diff] [blame] | 821 | UnderlyingFirstDecl && isa<TemplateDecl>(UnderlyingFirstDecl)) { |
| Douglas Gregor | 5e16c16 | 2011-04-27 03:47:06 +0000 | [diff] [blame] | 822 | UnqualifiedDiag = diag::err_no_template_suggest; |
| 823 | QualifiedDiag = diag::err_no_member_template_suggest; |
| Douglas Gregor | 8b02cd0 | 2011-04-27 04:48:22 +0000 | [diff] [blame] | 824 | } else if (UnderlyingFirstDecl && |
| 825 | (isa<TypeDecl>(UnderlyingFirstDecl) || |
| 826 | isa<ObjCInterfaceDecl>(UnderlyingFirstDecl) || |
| 827 | isa<ObjCCompatibleAliasDecl>(UnderlyingFirstDecl))) { |
| David Blaikie | 9db0604 | 2013-03-21 21:35:15 +0000 | [diff] [blame] | 828 | UnqualifiedDiag = diag::err_unknown_typename_suggest; |
| 829 | QualifiedDiag = diag::err_unknown_nested_typename_suggest; |
| 830 | } |
| Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 831 | |
| Kaelyn Uhrain | 10413a4 | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 832 | if (SS.isEmpty()) { |
| Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 833 | diagnoseTypo(Corrected, PDiag(UnqualifiedDiag) << Name); |
| Kaelyn Uhrain | 10413a4 | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 834 | } else {// FIXME: is this even reachable? Test it. |
| Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 835 | std::string CorrectedStr(Corrected.getAsString(getLangOpts())); |
| 836 | bool DroppedSpecifier = Corrected.WillReplaceSpecifier() && |
| Kaelyn Uhrain | 10413a4 | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 837 | Name->getName().equals(CorrectedStr); |
| Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 838 | diagnoseTypo(Corrected, PDiag(QualifiedDiag) |
| 839 | << Name << computeDeclContext(SS, false) |
| 840 | << DroppedSpecifier << SS.getRange()); |
| Kaelyn Uhrain | 10413a4 | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 841 | } |
| Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 842 | |
| 843 | // Update the name, so that the caller has the new name. |
| Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 844 | Name = Corrected.getCorrectionAsIdentifierInfo(); |
| Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 845 | |
| Kaelyn Uhrain | 9ce58bd | 2012-01-24 19:45:35 +0000 | [diff] [blame] | 846 | // Typo correction corrected to a keyword. |
| 847 | if (Corrected.isKeyword()) |
| Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 848 | return Name; |
| Kaelyn Uhrain | 9ce58bd | 2012-01-24 19:45:35 +0000 | [diff] [blame] | 849 | |
| Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 850 | // Also update the LookupResult... |
| 851 | // FIXME: This should probably go away at some point |
| 852 | Result.clear(); |
| 853 | Result.setLookupName(Corrected.getCorrection()); |
| Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 854 | if (FirstDecl) |
| Kaelyn Uhrain | 9ce58bd | 2012-01-24 19:45:35 +0000 | [diff] [blame] | 855 | Result.addDecl(FirstDecl); |
| Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 856 | |
| 857 | // If we found an Objective-C instance variable, let |
| 858 | // LookupInObjCMethod build the appropriate expression to |
| 859 | // reference the ivar. |
| 860 | // FIXME: This is a gross hack. |
| 861 | if (ObjCIvarDecl *Ivar = Result.getAsSingle<ObjCIvarDecl>()) { |
| 862 | Result.clear(); |
| 863 | ExprResult E(LookupInObjCMethod(Result, S, Ivar->getIdentifier())); |
| Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 864 | return E; |
| Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 865 | } |
| 866 | |
| 867 | goto Corrected; |
| 868 | } |
| 869 | } |
| 870 | |
| 871 | // We failed to correct; just fall through and let the parser deal with it. |
| 872 | Result.suppressDiagnostics(); |
| 873 | return NameClassification::Unknown(); |
| 874 | |
| Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 875 | case LookupResult::NotFoundInCurrentInstantiation: { |
| Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 876 | // We performed name lookup into the current instantiation, and there were |
| 877 | // dependent bases, so we treat this result the same way as any other |
| 878 | // dependent nested-name-specifier. |
| 879 | |
| 880 | // C++ [temp.res]p2: |
| 881 | // A name used in a template declaration or definition and that is |
| 882 | // dependent on a template-parameter is assumed not to name a type |
| 883 | // unless the applicable name lookup finds a type name or the name is |
| 884 | // qualified by the keyword typename. |
| 885 | // |
| 886 | // FIXME: If the next token is '<', we might want to ask the parser to |
| 887 | // perform some heroics to see if we actually have a |
| 888 | // template-argument-list, which would indicate a missing 'template' |
| 889 | // keyword here. |
| Richard Smith | 4f605af | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 890 | return ActOnDependentIdExpression(SS, /*TemplateKWLoc=*/SourceLocation(), |
| 891 | NameInfo, IsAddressOfOperand, |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 892 | /*TemplateArgs=*/nullptr); |
| Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 893 | } |
| Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 894 | |
| 895 | case LookupResult::Found: |
| 896 | case LookupResult::FoundOverloaded: |
| 897 | case LookupResult::FoundUnresolvedValue: |
| 898 | break; |
| 899 | |
| 900 | case LookupResult::Ambiguous: |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 901 | if (getLangOpts().CPlusPlus && NextToken.is(tok::less) && |
| Douglas Gregor | 8b02cd0 | 2011-04-27 04:48:22 +0000 | [diff] [blame] | 902 | hasAnyAcceptableTemplateNames(Result)) { |
| Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 903 | // C++ [temp.local]p3: |
| 904 | // A lookup that finds an injected-class-name (10.2) can result in an |
| 905 | // ambiguity in certain cases (for example, if it is found in more than |
| 906 | // one base class). If all of the injected-class-names that are found |
| 907 | // refer to specializations of the same class template, and if the name |
| 908 | // is followed by a template-argument-list, the reference refers to the |
| 909 | // class template itself and not a specialization thereof, and is not |
| 910 | // ambiguous. |
| 911 | // |
| 912 | // This filtering can make an ambiguous result into an unambiguous one, |
| 913 | // so try again after filtering out template names. |
| 914 | FilterAcceptableTemplateNames(Result); |
| 915 | if (!Result.isAmbiguous()) { |
| 916 | IsFilteredTemplateName = true; |
| 917 | break; |
| 918 | } |
| 919 | } |
| 920 | |
| 921 | // Diagnose the ambiguity and return an error. |
| 922 | return NameClassification::Error(); |
| 923 | } |
| 924 | |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 925 | if (getLangOpts().CPlusPlus && NextToken.is(tok::less) && |
| Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 926 | (IsFilteredTemplateName || hasAnyAcceptableTemplateNames(Result))) { |
| 927 | // C++ [temp.names]p3: |
| 928 | // After name lookup (3.4) finds that a name is a template-name or that |
| 929 | // an operator-function-id or a literal- operator-id refers to a set of |
| 930 | // overloaded functions any member of which is a function template if |
| 931 | // this is followed by a <, the < is always taken as the delimiter of a |
| 932 | // template-argument-list and never as the less-than operator. |
| 933 | if (!IsFilteredTemplateName) |
| 934 | FilterAcceptableTemplateNames(Result); |
| 935 | |
| Douglas Gregor | 8b02cd0 | 2011-04-27 04:48:22 +0000 | [diff] [blame] | 936 | if (!Result.empty()) { |
| 937 | bool IsFunctionTemplate; |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 938 | bool IsVarTemplate; |
| Douglas Gregor | 8b02cd0 | 2011-04-27 04:48:22 +0000 | [diff] [blame] | 939 | TemplateName Template; |
| 940 | if (Result.end() - Result.begin() > 1) { |
| 941 | IsFunctionTemplate = true; |
| 942 | Template = Context.getOverloadedTemplateName(Result.begin(), |
| 943 | Result.end()); |
| 944 | } else { |
| 945 | TemplateDecl *TD |
| 946 | = cast<TemplateDecl>((*Result.begin())->getUnderlyingDecl()); |
| 947 | IsFunctionTemplate = isa<FunctionTemplateDecl>(TD); |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 948 | IsVarTemplate = isa<VarTemplateDecl>(TD); |
| 949 | |
| Douglas Gregor | 8b02cd0 | 2011-04-27 04:48:22 +0000 | [diff] [blame] | 950 | if (SS.isSet() && !SS.isInvalid()) |
| 951 | Template = Context.getQualifiedTemplateName(SS.getScopeRep(), |
| Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 952 | /*TemplateKeyword=*/false, |
| Douglas Gregor | 8b02cd0 | 2011-04-27 04:48:22 +0000 | [diff] [blame] | 953 | TD); |
| 954 | else |
| 955 | Template = TemplateName(TD); |
| 956 | } |
| Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 957 | |
| Douglas Gregor | 8b02cd0 | 2011-04-27 04:48:22 +0000 | [diff] [blame] | 958 | if (IsFunctionTemplate) { |
| 959 | // Function templates always go through overload resolution, at which |
| 960 | // point we'll perform the various checks (e.g., accessibility) we need |
| 961 | // to based on which function we selected. |
| 962 | Result.suppressDiagnostics(); |
| 963 | |
| 964 | return NameClassification::FunctionTemplate(Template); |
| 965 | } |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 966 | |
| 967 | return IsVarTemplate ? NameClassification::VarTemplate(Template) |
| 968 | : NameClassification::TypeTemplate(Template); |
| Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 969 | } |
| Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 970 | } |
| Richard Smith | 4f605af | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 971 | |
| Douglas Gregor | 8b02cd0 | 2011-04-27 04:48:22 +0000 | [diff] [blame] | 972 | NamedDecl *FirstDecl = (*Result.begin())->getUnderlyingDecl(); |
| Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 973 | if (TypeDecl *Type = dyn_cast<TypeDecl>(FirstDecl)) { |
| 974 | DiagnoseUseOfDecl(Type, NameLoc); |
| Nico Weber | 7288943 | 2014-09-06 01:25:55 +0000 | [diff] [blame] | 975 | MarkAnyDeclReferenced(Type->getLocation(), Type, /*OdrUse=*/false); |
| Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 976 | QualType T = Context.getTypeDeclType(Type); |
| Richard Smith | 4f605af | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 977 | if (SS.isNotEmpty()) |
| 978 | return buildNestedType(*this, SS, T, NameLoc); |
| Kaelyn Uhrain | 7179205 | 2012-05-02 00:11:40 +0000 | [diff] [blame] | 979 | return ParsedType::make(T); |
| Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 980 | } |
| Richard Smith | 4f605af | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 981 | |
| Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 982 | ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(FirstDecl); |
| 983 | if (!Class) { |
| 984 | // FIXME: It's unfortunate that we don't have a Type node for handling this. |
| Nico Weber | dfc5920 | 2014-05-03 22:07:35 +0000 | [diff] [blame] | 985 | if (ObjCCompatibleAliasDecl *Alias = |
| 986 | dyn_cast<ObjCCompatibleAliasDecl>(FirstDecl)) |
| Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 987 | Class = Alias->getClassInterface(); |
| 988 | } |
| 989 | |
| 990 | if (Class) { |
| 991 | DiagnoseUseOfDecl(Class, NameLoc); |
| 992 | |
| 993 | if (NextToken.is(tok::period)) { |
| 994 | // Interface. <something> is parsed as a property reference expression. |
| 995 | // Just return "unknown" as a fall-through for now. |
| 996 | Result.suppressDiagnostics(); |
| 997 | return NameClassification::Unknown(); |
| 998 | } |
| 999 | |
| 1000 | QualType T = Context.getObjCInterfaceType(Class); |
| 1001 | return ParsedType::make(T); |
| 1002 | } |
| Kaelyn Uhrain | 7179205 | 2012-05-02 00:11:40 +0000 | [diff] [blame] | 1003 | |
| Richard Smith | 4f605af | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 1004 | // We can have a type template here if we're classifying a template argument. |
| 1005 | if (isa<TemplateDecl>(FirstDecl) && !isa<FunctionTemplateDecl>(FirstDecl)) |
| 1006 | return NameClassification::TypeTemplate( |
| 1007 | TemplateName(cast<TemplateDecl>(FirstDecl))); |
| 1008 | |
| Kaelyn Uhrain | 7179205 | 2012-05-02 00:11:40 +0000 | [diff] [blame] | 1009 | // Check for a tag type hidden by a non-type decl in a few cases where it |
| 1010 | // 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] | 1011 | bool NextIsOp = NextToken.is(tok::amp) || NextToken.is(tok::star); |
| 1012 | if ((NextToken.is(tok::identifier) || |
| Alp Toker | a2794f9 | 2014-01-22 07:29:52 +0000 | [diff] [blame] | 1013 | (NextIsOp && |
| 1014 | FirstDecl->getUnderlyingDecl()->isFunctionOrFunctionTemplate())) && |
| Argyrios Kyrtzidis | c64c029 | 2013-05-07 19:54:28 +0000 | [diff] [blame] | 1015 | isTagTypeWithMissingTag(*this, Result, S, SS, Name, NameLoc)) { |
| 1016 | TypeDecl *Type = Result.getAsSingle<TypeDecl>(); |
| 1017 | DiagnoseUseOfDecl(Type, NameLoc); |
| 1018 | QualType T = Context.getTypeDeclType(Type); |
| 1019 | if (SS.isNotEmpty()) |
| 1020 | return buildNestedType(*this, SS, T, NameLoc); |
| 1021 | return ParsedType::make(T); |
| Kaelyn Uhrain | 7179205 | 2012-05-02 00:11:40 +0000 | [diff] [blame] | 1022 | } |
| Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 1023 | |
| Richard Smith | 4f605af | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 1024 | if (FirstDecl->isCXXClassMember()) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1025 | return BuildPossibleImplicitMemberExpr(SS, SourceLocation(), Result, |
| 1026 | nullptr); |
| Douglas Gregor | 8b02cd0 | 2011-04-27 04:48:22 +0000 | [diff] [blame] | 1027 | |
| Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 1028 | bool ADL = UseArgumentDependentLookup(SS, Result, NextToken.is(tok::l_paren)); |
| 1029 | return BuildDeclarationNameExpr(SS, Result, ADL); |
| 1030 | } |
| 1031 | |
| John McCall | 5ed6e8f | 2009-08-18 00:00:49 +0000 | [diff] [blame] | 1032 | // Determines the context to return to after temporarily entering a |
| 1033 | // context. This depends in an unnecessarily complicated way on the |
| 1034 | // exact ordering of callbacks from the parser. |
| Argyrios Kyrtzidis | 16ac9be | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 1035 | DeclContext *Sema::getContainingDC(DeclContext *DC) { |
| Argyrios Kyrtzidis | 16ac9be | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 1036 | |
| John McCall | 5ed6e8f | 2009-08-18 00:00:49 +0000 | [diff] [blame] | 1037 | // Functions defined inline within classes aren't parsed until we've |
| 1038 | // finished parsing the top-level class, so the top-level class is |
| 1039 | // the context we'll need to return to. |
| Faisal Vali | bb9071e | 2013-12-04 22:43:08 +0000 | [diff] [blame] | 1040 | // A Lambda call operator whose parent is a class must not be treated |
| 1041 | // as an inline member function. A Lambda can be used legally |
| 1042 | // either as an in-class member initializer or a default argument. These |
| 1043 | // are parsed once the class has been marked complete and so the containing |
| 1044 | // context would be the nested class (when the lambda is defined in one); |
| 1045 | // If the class is not complete, then the lambda is being used in an |
| 1046 | // ill-formed fashion (such as to specify the width of a bit-field, or |
| 1047 | // in an array-bound) - in which case we still want to return the |
| 1048 | // lexically containing DC (which could be a nested class). |
| 1049 | if (isa<FunctionDecl>(DC) && !isLambdaCallOperator(DC)) { |
| John McCall | 5ed6e8f | 2009-08-18 00:00:49 +0000 | [diff] [blame] | 1050 | DC = DC->getLexicalParent(); |
| 1051 | |
| 1052 | // A function not defined within a class will always return to its |
| 1053 | // lexical context. |
| 1054 | if (!isa<CXXRecordDecl>(DC)) |
| 1055 | return DC; |
| 1056 | |
| 1057 | // A C++ inline method/friend is parsed *after* the topmost class |
| 1058 | // it was declared in is fully parsed ("complete"); the topmost |
| 1059 | // class is the context we need to return to. |
| Argyrios Kyrtzidis | 0d09c49 | 2008-11-19 18:01:13 +0000 | [diff] [blame] | 1060 | while (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(DC->getLexicalParent())) |
| Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1061 | DC = RD; |
| 1062 | |
| 1063 | // Return the declaration context of the topmost class the inline method is |
| 1064 | // declared in. |
| 1065 | return DC; |
| 1066 | } |
| 1067 | |
| Argyrios Kyrtzidis | 0d09c49 | 2008-11-19 18:01:13 +0000 | [diff] [blame] | 1068 | return DC->getLexicalParent(); |
| Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1069 | } |
| 1070 | |
| Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1071 | void Sema::PushDeclContext(Scope *S, DeclContext *DC) { |
| Argyrios Kyrtzidis | 16ac9be | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 1072 | assert(getContainingDC(DC) == CurContext && |
| Zhongxing Xu | 17a37eb | 2008-12-08 07:14:51 +0000 | [diff] [blame] | 1073 | "The next DeclContext should be lexically contained in the current one."); |
| Chris Lattner | bec4134 | 2008-04-22 18:39:57 +0000 | [diff] [blame] | 1074 | CurContext = DC; |
| Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1075 | S->setEntity(DC); |
| Chris Lattner | c5ffed4 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1076 | } |
| 1077 | |
| Chris Lattner | 0a5ff0d | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 1078 | void Sema::PopDeclContext() { |
| 1079 | assert(CurContext && "DeclContext imbalance!"); |
| Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1080 | |
| Argyrios Kyrtzidis | 16ac9be | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 1081 | CurContext = getContainingDC(CurContext); |
| John McCall | dd762d1 | 2010-07-23 22:45:07 +0000 | [diff] [blame] | 1082 | assert(CurContext && "Popped translation unit!"); |
| Chris Lattner | c5ffed4 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1083 | } |
| 1084 | |
| Argyrios Kyrtzidis | 9941a4d | 2009-06-17 23:19:02 +0000 | [diff] [blame] | 1085 | /// EnterDeclaratorContext - Used when we must lookup names in the context |
| 1086 | /// of a declarator's nested name specifier. |
| John McCall | 6df5fef | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 1087 | /// |
| Argyrios Kyrtzidis | 7bcce49 | 2009-06-17 23:15:40 +0000 | [diff] [blame] | 1088 | void Sema::EnterDeclaratorContext(Scope *S, DeclContext *DC) { |
| John McCall | 6df5fef | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 1089 | // C++0x [basic.lookup.unqual]p13: |
| 1090 | // A name used in the definition of a static data member of class |
| 1091 | // X (after the qualified-id of the static member) is looked up as |
| 1092 | // if the name was used in a member function of X. |
| 1093 | // C++0x [basic.lookup.unqual]p14: |
| 1094 | // If a variable member of a namespace is defined outside of the |
| 1095 | // scope of its namespace then any name used in the definition of |
| 1096 | // the variable member (after the declarator-id) is looked up as |
| 1097 | // if the definition of the variable member occurred in its |
| 1098 | // namespace. |
| 1099 | // Both of these imply that we should push a scope whose context |
| 1100 | // is the semantic context of the declaration. We can't use |
| 1101 | // PushDeclContext here because that context is not necessarily |
| 1102 | // lexically contained in the current context. Fortunately, |
| 1103 | // the containing scope should have the appropriate information. |
| 1104 | |
| 1105 | assert(!S->getEntity() && "scope already has entity"); |
| 1106 | |
| 1107 | #ifndef NDEBUG |
| 1108 | Scope *Ancestor = S->getParent(); |
| 1109 | while (!Ancestor->getEntity()) Ancestor = Ancestor->getParent(); |
| 1110 | assert(Ancestor->getEntity() == CurContext && "ancestor context mismatch"); |
| 1111 | #endif |
| 1112 | |
| Argyrios Kyrtzidis | 7bcce49 | 2009-06-17 23:15:40 +0000 | [diff] [blame] | 1113 | CurContext = DC; |
| John McCall | 6df5fef | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 1114 | S->setEntity(DC); |
| Argyrios Kyrtzidis | 7bcce49 | 2009-06-17 23:15:40 +0000 | [diff] [blame] | 1115 | } |
| 1116 | |
| Argyrios Kyrtzidis | 7bcce49 | 2009-06-17 23:15:40 +0000 | [diff] [blame] | 1117 | void Sema::ExitDeclaratorContext(Scope *S) { |
| John McCall | 6df5fef | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 1118 | assert(S->getEntity() == CurContext && "Context imbalance!"); |
| Argyrios Kyrtzidis | 7bcce49 | 2009-06-17 23:15:40 +0000 | [diff] [blame] | 1119 | |
| John McCall | 6df5fef | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 1120 | // Switch back to the lexical context. The safety of this is |
| 1121 | // enforced by an assert in EnterDeclaratorContext. |
| 1122 | Scope *Ancestor = S->getParent(); |
| 1123 | while (!Ancestor->getEntity()) Ancestor = Ancestor->getParent(); |
| Ted Kremenek | c37877d | 2013-10-08 17:08:03 +0000 | [diff] [blame] | 1124 | CurContext = Ancestor->getEntity(); |
| John McCall | 6df5fef | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 1125 | |
| 1126 | // We don't need to do anything with the scope, which is going to |
| 1127 | // disappear. |
| Argyrios Kyrtzidis | 7bcce49 | 2009-06-17 23:15:40 +0000 | [diff] [blame] | 1128 | } |
| 1129 | |
| Caitlin Sadowski | 990d571 | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 1130 | |
| 1131 | void Sema::ActOnReenterFunctionContext(Scope* S, Decl *D) { |
| Alp Toker | a2794f9 | 2014-01-22 07:29:52 +0000 | [diff] [blame] | 1132 | // We assume that the caller has already called |
| 1133 | // ActOnReenterTemplateScope so getTemplatedDecl() works. |
| 1134 | FunctionDecl *FD = D->getAsFunction(); |
| Caitlin Sadowski | 990d571 | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 1135 | if (!FD) |
| 1136 | return; |
| 1137 | |
| DeLesley Hutchins | 6f86004 | 2012-04-06 15:10:17 +0000 | [diff] [blame] | 1138 | // Same implementation as PushDeclContext, but enters the context |
| 1139 | // from the lexical parent, rather than the top-level class. |
| 1140 | assert(CurContext == FD->getLexicalParent() && |
| 1141 | "The next DeclContext should be lexically contained in the current one."); |
| 1142 | CurContext = FD; |
| 1143 | S->setEntity(CurContext); |
| 1144 | |
| Caitlin Sadowski | 990d571 | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 1145 | for (unsigned P = 0, NumParams = FD->getNumParams(); P < NumParams; ++P) { |
| 1146 | ParmVarDecl *Param = FD->getParamDecl(P); |
| 1147 | // If the parameter has an identifier, then add it to the scope |
| 1148 | if (Param->getIdentifier()) { |
| 1149 | S->AddDecl(Param); |
| 1150 | IdResolver.AddDecl(Param); |
| 1151 | } |
| 1152 | } |
| 1153 | } |
| 1154 | |
| 1155 | |
| DeLesley Hutchins | 6f86004 | 2012-04-06 15:10:17 +0000 | [diff] [blame] | 1156 | void Sema::ActOnExitFunctionContext() { |
| 1157 | // Same implementation as PopDeclContext, but returns to the lexical parent, |
| 1158 | // rather than the top-level class. |
| 1159 | assert(CurContext && "DeclContext imbalance!"); |
| 1160 | CurContext = CurContext->getLexicalParent(); |
| 1161 | assert(CurContext && "Popped translation unit!"); |
| 1162 | } |
| 1163 | |
| 1164 | |
| Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1165 | /// \brief Determine whether we allow overloading of the function |
| 1166 | /// PrevDecl with another declaration. |
| 1167 | /// |
| 1168 | /// This routine determines whether overloading is possible, not |
| 1169 | /// whether some new function is actually an overload. It will return |
| 1170 | /// true in C++ (where we can always provide overloads) or, as an |
| 1171 | /// extension, in C when the previous function is already an |
| 1172 | /// overloaded function declaration or has the "overloadable" |
| 1173 | /// attribute. |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1174 | static bool AllowOverloadingOfFunction(LookupResult &Previous, |
| 1175 | ASTContext &Context) { |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1176 | if (Context.getLangOpts().CPlusPlus) |
| Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1177 | return true; |
| 1178 | |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1179 | if (Previous.getResultKind() == LookupResult::FoundOverloaded) |
| Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1180 | return true; |
| 1181 | |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1182 | return (Previous.getResultKind() == LookupResult::Found |
| 1183 | && Previous.getFoundDecl()->hasAttr<OverloadableAttr>()); |
| Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1184 | } |
| 1185 | |
| Argyrios Kyrtzidis | b8a4920 | 2008-04-12 00:47:19 +0000 | [diff] [blame] | 1186 | /// Add this decl to the scope shadowed decl chains. |
| John McCall | 759e32b | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 1187 | void Sema::PushOnScopeChains(NamedDecl *D, Scope *S, bool AddToContext) { |
| Douglas Gregor | 07665a6 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 1188 | // Move up the scope chain until we find the nearest enclosing |
| 1189 | // non-transparent context. The declaration will be introduced into this |
| 1190 | // scope. |
| Ted Kremenek | c37877d | 2013-10-08 17:08:03 +0000 | [diff] [blame] | 1191 | while (S->getEntity() && S->getEntity()->isTransparentContext()) |
| Douglas Gregor | 07665a6 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 1192 | S = S->getParent(); |
| 1193 | |
| Douglas Gregor | 8b9ccca | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 1194 | // Add scoped declarations into their context, so that they can be |
| 1195 | // found later. Declarations without a context won't be inserted |
| 1196 | // into any context. |
| John McCall | 759e32b | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 1197 | if (AddToContext) |
| 1198 | CurContext->addDecl(D); |
| Douglas Gregor | 8b9ccca | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 1199 | |
| Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 1200 | // Out-of-line definitions shouldn't be pushed into scope in C++, unless they |
| 1201 | // are function-local declarations. |
| 1202 | if (getLangOpts().CPlusPlus && D->isOutOfLine() && |
| Douglas Gregor | f7b9895 | 2011-10-09 22:57:49 +0000 | [diff] [blame] | 1203 | !D->getDeclContext()->getRedeclContext()->Equals( |
| Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 1204 | D->getLexicalDeclContext()->getRedeclContext()) && |
| 1205 | !D->getLexicalDeclContext()->isFunctionOrMethod()) |
| Chandler Carruth | f50ef6e | 2010-02-21 07:08:09 +0000 | [diff] [blame] | 1206 | return; |
| 1207 | |
| 1208 | // Template instantiations should also not be pushed into scope. |
| 1209 | if (isa<FunctionDecl>(D) && |
| 1210 | cast<FunctionDecl>(D)->isFunctionTemplateSpecialization()) |
| Douglas Gregor | 5ad7c54 | 2009-09-28 18:41:37 +0000 | [diff] [blame] | 1211 | return; |
| 1212 | |
| John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1213 | // If this replaces anything in the current scope, |
| 1214 | IdentifierResolver::iterator I = IdResolver.begin(D->getDeclName()), |
| 1215 | IEnd = IdResolver.end(); |
| 1216 | for (; I != IEnd; ++I) { |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1217 | if (S->isDeclScope(*I) && D->declarationReplaces(*I)) { |
| 1218 | S->RemoveDecl(*I); |
| John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1219 | IdResolver.RemoveDecl(*I); |
| Argyrios Kyrtzidis | fa8e15b | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 1220 | |
| John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1221 | // Should only need to replace one decl. |
| 1222 | break; |
| Douglas Gregor | 38feed8 | 2009-04-24 02:57:34 +0000 | [diff] [blame] | 1223 | } |
| Argyrios Kyrtzidis | fa8e15b | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 1224 | } |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1225 | |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1226 | S->AddDecl(D); |
| Douglas Gregor | 88764cf | 2011-03-14 21:19:51 +0000 | [diff] [blame] | 1227 | |
| 1228 | if (isa<LabelDecl>(D) && !cast<LabelDecl>(D)->isGnuLocal()) { |
| 1229 | // Implicitly-generated labels may end up getting generated in an order that |
| 1230 | // isn't strictly lexical, which breaks name lookup. Be careful to insert |
| 1231 | // the label at the appropriate place in the identifier chain. |
| 1232 | for (I = IdResolver.begin(D->getDeclName()); I != IEnd; ++I) { |
| Douglas Gregor | d7d7e0d | 2011-03-24 14:35:16 +0000 | [diff] [blame] | 1233 | DeclContext *IDC = (*I)->getLexicalDeclContext()->getRedeclContext(); |
| Douglas Gregor | 46c04e7 | 2011-03-16 16:39:03 +0000 | [diff] [blame] | 1234 | if (IDC == CurContext) { |
| 1235 | if (!S->isDeclScope(*I)) |
| 1236 | continue; |
| 1237 | } else if (IDC->Encloses(CurContext)) |
| Douglas Gregor | 88764cf | 2011-03-14 21:19:51 +0000 | [diff] [blame] | 1238 | break; |
| 1239 | } |
| 1240 | |
| Douglas Gregor | 46c04e7 | 2011-03-16 16:39:03 +0000 | [diff] [blame] | 1241 | IdResolver.InsertDeclAfter(I, D); |
| Douglas Gregor | 88764cf | 2011-03-14 21:19:51 +0000 | [diff] [blame] | 1242 | } else { |
| 1243 | IdResolver.AddDecl(D); |
| 1244 | } |
| Argyrios Kyrtzidis | b8a4920 | 2008-04-12 00:47:19 +0000 | [diff] [blame] | 1245 | } |
| 1246 | |
| Douglas Gregor | 935bc7a2 | 2011-10-27 09:33:13 +0000 | [diff] [blame] | 1247 | void Sema::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) { |
| 1248 | if (IdResolver.tryAddTopLevelDecl(D, Name) && TUScope) |
| 1249 | TUScope->AddDecl(D); |
| 1250 | } |
| 1251 | |
| Richard Smith | 1c34fb7 | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 1252 | bool Sema::isDeclInScope(NamedDecl *D, DeclContext *Ctx, Scope *S, |
| Richard Smith | 72bcaec | 2013-12-05 04:30:04 +0000 | [diff] [blame] | 1253 | bool AllowInlineNamespace) { |
| 1254 | return IdResolver.isDeclInScope(D, Ctx, S, AllowInlineNamespace); |
| Douglas Gregor | 505ad49 | 2009-09-28 00:47:05 +0000 | [diff] [blame] | 1255 | } |
| 1256 | |
| John McCall | cc14d1f | 2010-08-24 08:50:51 +0000 | [diff] [blame] | 1257 | Scope *Sema::getScopeForDeclContext(Scope *S, DeclContext *DC) { |
| 1258 | DeclContext *TargetDC = DC->getPrimaryContext(); |
| 1259 | do { |
| Ted Kremenek | c37877d | 2013-10-08 17:08:03 +0000 | [diff] [blame] | 1260 | if (DeclContext *ScopeDC = S->getEntity()) |
| John McCall | cc14d1f | 2010-08-24 08:50:51 +0000 | [diff] [blame] | 1261 | if (ScopeDC->getPrimaryContext() == TargetDC) |
| 1262 | return S; |
| 1263 | } while ((S = S->getParent())); |
| 1264 | |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1265 | return nullptr; |
| John McCall | cc14d1f | 2010-08-24 08:50:51 +0000 | [diff] [blame] | 1266 | } |
| 1267 | |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1268 | static bool isOutOfScopePreviousDeclaration(NamedDecl *, |
| 1269 | DeclContext*, |
| 1270 | ASTContext&); |
| 1271 | |
| 1272 | /// Filters out lookup results that don't fall within the given scope |
| 1273 | /// as determined by isDeclInScope. |
| Richard Smith | 72bcaec | 2013-12-05 04:30:04 +0000 | [diff] [blame] | 1274 | void Sema::FilterLookupForScope(LookupResult &R, DeclContext *Ctx, Scope *S, |
| Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1275 | bool ConsiderLinkage, |
| Richard Smith | 72bcaec | 2013-12-05 04:30:04 +0000 | [diff] [blame] | 1276 | bool AllowInlineNamespace) { |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1277 | LookupResult::Filter F = R.makeFilter(); |
| 1278 | while (F.hasNext()) { |
| 1279 | NamedDecl *D = F.next(); |
| 1280 | |
| Richard Smith | 72bcaec | 2013-12-05 04:30:04 +0000 | [diff] [blame] | 1281 | if (isDeclInScope(D, Ctx, S, AllowInlineNamespace)) |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1282 | continue; |
| 1283 | |
| Richard Smith | 72bcaec | 2013-12-05 04:30:04 +0000 | [diff] [blame] | 1284 | if (ConsiderLinkage && isOutOfScopePreviousDeclaration(D, Ctx, Context)) |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1285 | continue; |
| Richard Smith | 72bcaec | 2013-12-05 04:30:04 +0000 | [diff] [blame] | 1286 | |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1287 | F.erase(); |
| 1288 | } |
| 1289 | |
| 1290 | F.done(); |
| 1291 | } |
| 1292 | |
| 1293 | static bool isUsingDecl(NamedDecl *D) { |
| 1294 | return isa<UsingShadowDecl>(D) || |
| 1295 | isa<UnresolvedUsingTypenameDecl>(D) || |
| 1296 | isa<UnresolvedUsingValueDecl>(D); |
| 1297 | } |
| 1298 | |
| 1299 | /// Removes using shadow declarations from the lookup results. |
| 1300 | static void RemoveUsingDecls(LookupResult &R) { |
| 1301 | LookupResult::Filter F = R.makeFilter(); |
| 1302 | while (F.hasNext()) |
| 1303 | if (isUsingDecl(F.next())) |
| 1304 | F.erase(); |
| 1305 | |
| 1306 | F.done(); |
| 1307 | } |
| 1308 | |
| Argyrios Kyrtzidis | 04c7fa0 | 2010-08-15 10:17:33 +0000 | [diff] [blame] | 1309 | /// \brief Check for this common pattern: |
| 1310 | /// @code |
| 1311 | /// class S { |
| 1312 | /// S(const S&); // DO NOT IMPLEMENT |
| 1313 | /// void operator=(const S&); // DO NOT IMPLEMENT |
| 1314 | /// }; |
| 1315 | /// @endcode |
| 1316 | static bool IsDisallowedCopyOrAssign(const CXXMethodDecl *D) { |
| 1317 | // FIXME: Should check for private access too but access is set after we get |
| 1318 | // the decl here. |
| Alexis Hunt | 4a8ea10 | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 1319 | if (D->doesThisDeclarationHaveABody()) |
| Argyrios Kyrtzidis | 04c7fa0 | 2010-08-15 10:17:33 +0000 | [diff] [blame] | 1320 | return false; |
| 1321 | |
| 1322 | if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(D)) |
| 1323 | return CD->isCopyConstructor(); |
| Douglas Gregor | a1ce1f8 | 2010-09-27 22:06:20 +0000 | [diff] [blame] | 1324 | if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) |
| 1325 | return Method->isCopyAssignmentOperator(); |
| 1326 | return false; |
| Argyrios Kyrtzidis | 04c7fa0 | 2010-08-15 10:17:33 +0000 | [diff] [blame] | 1327 | } |
| 1328 | |
| Rafael Espindola | 0e0d009 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 1329 | // We need this to handle |
| 1330 | // |
| 1331 | // typedef struct { |
| 1332 | // void *foo() { return 0; } |
| 1333 | // } A; |
| 1334 | // |
| 1335 | // When we see foo we don't know if after the typedef we will get 'A' or '*A' |
| 1336 | // 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] | 1337 | // 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] | 1338 | // 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] | 1339 | // Callers should verify at the end of the TU if it D has external linkage or |
| 1340 | // not. |
| 1341 | bool Sema::mightHaveNonExternalLinkage(const DeclaratorDecl *D) { |
| 1342 | const DeclContext *DC = D->getDeclContext(); |
| 1343 | while (!DC->isTranslationUnit()) { |
| 1344 | if (const RecordDecl *RD = dyn_cast<RecordDecl>(DC)){ |
| 1345 | if (!RD->hasNameForLinkage()) |
| 1346 | return true; |
| 1347 | } |
| 1348 | DC = DC->getParent(); |
| 1349 | } |
| 1350 | |
| Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 1351 | return !D->isExternallyVisible(); |
| Rafael Espindola | 0e0d009 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 1352 | } |
| 1353 | |
| Eli Friedman | 5ef2175 | 2013-09-10 03:05:56 +0000 | [diff] [blame] | 1354 | // FIXME: This needs to be refactored; some other isInMainFile users want |
| 1355 | // these semantics. |
| 1356 | static bool isMainFileLoc(const Sema &S, SourceLocation Loc) { |
| 1357 | if (S.TUKind != TU_Complete) |
| 1358 | return false; |
| 1359 | return S.SourceMgr.isInMainFile(Loc); |
| 1360 | } |
| 1361 | |
| Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 1362 | bool Sema::ShouldWarnIfUnusedFileScopedDecl(const DeclaratorDecl *D) const { |
| 1363 | assert(D); |
| Argyrios Kyrtzidis | 540bc01 | 2010-08-13 18:42:29 +0000 | [diff] [blame] | 1364 | |
| Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 1365 | if (D->isInvalidDecl() || D->isUsed() || D->hasAttr<UnusedAttr>()) |
| 1366 | return false; |
| 1367 | |
| Richard Smith | c392617 | 2014-04-02 18:28:36 +0000 | [diff] [blame] | 1368 | // Ignore all entities declared within templates, and out-of-line definitions |
| 1369 | // of members of class templates. |
| Chandler Carruth | 8e66651 | 2011-01-03 19:27:19 +0000 | [diff] [blame] | 1370 | if (D->getDeclContext()->isDependentContext() || |
| 1371 | D->getLexicalDeclContext()->isDependentContext()) |
| Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 1372 | return false; |
| 1373 | |
| Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 1374 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| Argyrios Kyrtzidis | 04c7fa0 | 2010-08-15 10:17:33 +0000 | [diff] [blame] | 1375 | if (FD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation) |
| 1376 | return false; |
| Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 1377 | |
| Argyrios Kyrtzidis | 04c7fa0 | 2010-08-15 10:17:33 +0000 | [diff] [blame] | 1378 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) { |
| 1379 | if (MD->isVirtual() || IsDisallowedCopyOrAssign(MD)) |
| 1380 | return false; |
| 1381 | } else { |
| Eli Friedman | 5ef2175 | 2013-09-10 03:05:56 +0000 | [diff] [blame] | 1382 | // 'static inline' functions are defined in headers; don't warn. |
| Richard Smith | a90ee35 | 2014-05-11 21:25:24 +0000 | [diff] [blame] | 1383 | if (FD->isInlined() && !isMainFileLoc(*this, FD->getLocation())) |
| Argyrios Kyrtzidis | 04c7fa0 | 2010-08-15 10:17:33 +0000 | [diff] [blame] | 1384 | return false; |
| 1385 | } |
| Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 1386 | |
| Alexis Hunt | 4a8ea10 | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 1387 | if (FD->doesThisDeclarationHaveABody() && |
| John McCall | d37d35b | 2010-10-27 01:41:35 +0000 | [diff] [blame] | 1388 | Context.DeclMustBeEmitted(FD)) |
| 1389 | return false; |
| John McCall | d37d35b | 2010-10-27 01:41:35 +0000 | [diff] [blame] | 1390 | } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| Eli Friedman | 5ef2175 | 2013-09-10 03:05:56 +0000 | [diff] [blame] | 1391 | // Constants and utility variables are defined in headers with internal |
| 1392 | // linkage; don't warn. (Unlike functions, there isn't a convenient marker |
| 1393 | // like "inline".) |
| 1394 | if (!isMainFileLoc(*this, VD->getLocation())) |
| 1395 | return false; |
| 1396 | |
| Eli Friedman | 5ef2175 | 2013-09-10 03:05:56 +0000 | [diff] [blame] | 1397 | if (Context.DeclMustBeEmitted(VD)) |
| John McCall | d37d35b | 2010-10-27 01:41:35 +0000 | [diff] [blame] | 1398 | return false; |
| 1399 | |
| Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 1400 | if (VD->isStaticDataMember() && |
| 1401 | VD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation) |
| 1402 | return false; |
| John McCall | d37d35b | 2010-10-27 01:41:35 +0000 | [diff] [blame] | 1403 | } else { |
| 1404 | return false; |
| Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 1405 | } |
| 1406 | |
| John McCall | d37d35b | 2010-10-27 01:41:35 +0000 | [diff] [blame] | 1407 | // Only warn for unused decls internal to the translation unit. |
| Richard Smith | a90ee35 | 2014-05-11 21:25:24 +0000 | [diff] [blame] | 1408 | // FIXME: This seems like a bogus check; it suppresses -Wunused-function |
| 1409 | // for inline functions defined in the main source file, for instance. |
| Rafael Espindola | 0e0d009 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 1410 | return mightHaveNonExternalLinkage(D); |
| John McCall | d37d35b | 2010-10-27 01:41:35 +0000 | [diff] [blame] | 1411 | } |
| 1412 | |
| 1413 | void Sema::MarkUnusedFileScopedDecl(const DeclaratorDecl *D) { |
| Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 1414 | if (!D) |
| 1415 | return; |
| 1416 | |
| 1417 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 1418 | const FunctionDecl *First = FD->getFirstDecl(); |
| Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 1419 | if (FD != First && ShouldWarnIfUnusedFileScopedDecl(First)) |
| 1420 | return; // First should already be in the vector. |
| 1421 | } |
| 1422 | |
| 1423 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 1424 | const VarDecl *First = VD->getFirstDecl(); |
| Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 1425 | if (VD != First && ShouldWarnIfUnusedFileScopedDecl(First)) |
| 1426 | return; // First should already be in the vector. |
| 1427 | } |
| 1428 | |
| David Blaikie | 3d8edc2 | 2012-05-26 05:35:39 +0000 | [diff] [blame] | 1429 | if (ShouldWarnIfUnusedFileScopedDecl(D)) |
| 1430 | UnusedFileScopedDecls.push_back(D); |
| 1431 | } |
| Argyrios Kyrtzidis | 35672e7 | 2010-08-13 18:42:17 +0000 | [diff] [blame] | 1432 | |
| Anders Carlsson | 2889e0e | 2009-11-07 07:18:14 +0000 | [diff] [blame] | 1433 | static bool ShouldDiagnoseUnusedDecl(const NamedDecl *D) { |
| John McCall | 67da35c | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 1434 | if (D->isInvalidDecl()) |
| 1435 | return false; |
| 1436 | |
| Ted Kremenek | ce0e3f8 | 2014-01-09 20:19:45 +0000 | [diff] [blame] | 1437 | if (D->isReferenced() || D->isUsed() || D->hasAttr<UnusedAttr>() || |
| 1438 | D->hasAttr<ObjCPreciseLifetimeAttr>()) |
| Anders Carlsson | f5dc6fa | 2009-11-07 07:26:56 +0000 | [diff] [blame] | 1439 | return false; |
| John McCall | 67da35c | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 1440 | |
| Chris Lattner | cab02a6 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 1441 | if (isa<LabelDecl>(D)) |
| 1442 | return true; |
| Nico Weber | 7288943 | 2014-09-06 01:25:55 +0000 | [diff] [blame] | 1443 | |
| 1444 | // Except for labels, we only care about unused decls that are local to |
| 1445 | // functions. |
| 1446 | bool WithinFunction = D->getDeclContext()->isFunctionOrMethod(); |
| 1447 | if (const auto *R = dyn_cast<CXXRecordDecl>(D->getDeclContext())) |
| 1448 | // For dependent types, the diagnostic is deferred. |
| 1449 | WithinFunction = |
| 1450 | WithinFunction || (R->isLocalClass() && !R->isDependentType()); |
| 1451 | if (!WithinFunction) |
| 1452 | return false; |
| 1453 | |
| 1454 | if (isa<TypedefNameDecl>(D)) |
| 1455 | return true; |
| Chris Lattner | cab02a6 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 1456 | |
| John McCall | 67da35c | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 1457 | // White-list anything that isn't a local variable. |
| Nico Weber | 7288943 | 2014-09-06 01:25:55 +0000 | [diff] [blame] | 1458 | if (!isa<VarDecl>(D) || isa<ParmVarDecl>(D) || isa<ImplicitParamDecl>(D)) |
| John McCall | 67da35c | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 1459 | return false; |
| 1460 | |
| 1461 | // Types of valid local variables should be complete, so this should succeed. |
| Rafael Espindola | 7c23b08 | 2012-01-06 04:54:01 +0000 | [diff] [blame] | 1462 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| John McCall | cef1582 | 2010-03-31 02:47:45 +0000 | [diff] [blame] | 1463 | |
| 1464 | // White-list anything with an __attribute__((unused)) type. |
| 1465 | QualType Ty = VD->getType(); |
| 1466 | |
| 1467 | // Only look at the outermost level of typedef. |
| Douglas Gregor | 5c65f62 | 2012-09-14 05:10:40 +0000 | [diff] [blame] | 1468 | if (const TypedefType *TT = Ty->getAs<TypedefType>()) { |
| John McCall | cef1582 | 2010-03-31 02:47:45 +0000 | [diff] [blame] | 1469 | if (TT->getDecl()->hasAttr<UnusedAttr>()) |
| 1470 | return false; |
| 1471 | } |
| 1472 | |
| Douglas Gregor | 14f232e | 2010-05-08 23:05:03 +0000 | [diff] [blame] | 1473 | // If we failed to complete the type for some reason, or if the type is |
| 1474 | // dependent, don't diagnose the variable. |
| 1475 | if (Ty->isIncompleteType() || Ty->isDependentType()) |
| Douglas Gregor | 19defcd | 2010-04-27 16:20:13 +0000 | [diff] [blame] | 1476 | return false; |
| 1477 | |
| John McCall | cef1582 | 2010-03-31 02:47:45 +0000 | [diff] [blame] | 1478 | if (const TagType *TT = Ty->getAs<TagType>()) { |
| 1479 | const TagDecl *Tag = TT->getDecl(); |
| 1480 | if (Tag->hasAttr<UnusedAttr>()) |
| 1481 | return false; |
| 1482 | |
| 1483 | if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Tag)) { |
| Lubos Lunak | edc1388 | 2013-07-20 15:05:36 +0000 | [diff] [blame] | 1484 | if (!RD->hasTrivialDestructor() && !RD->hasAttr<WarnUnusedAttr>()) |
| Anders Carlsson | f5dc6fa | 2009-11-07 07:26:56 +0000 | [diff] [blame] | 1485 | return false; |
| Rafael Espindola | 7c23b08 | 2012-01-06 04:54:01 +0000 | [diff] [blame] | 1486 | |
| 1487 | if (const Expr *Init = VD->getInit()) { |
| Nico Weber | dfc5920 | 2014-05-03 22:07:35 +0000 | [diff] [blame] | 1488 | if (const ExprWithCleanups *Cleanups = |
| 1489 | dyn_cast<ExprWithCleanups>(Init)) |
| David Blaikie | a9d4a93 | 2012-10-24 21:29:06 +0000 | [diff] [blame] | 1490 | Init = Cleanups->getSubExpr(); |
| Rafael Espindola | 7c23b08 | 2012-01-06 04:54:01 +0000 | [diff] [blame] | 1491 | const CXXConstructExpr *Construct = |
| 1492 | dyn_cast<CXXConstructExpr>(Init); |
| 1493 | if (Construct && !Construct->isElidable()) { |
| 1494 | CXXConstructorDecl *CD = Construct->getConstructor(); |
| Lubos Lunak | edc1388 | 2013-07-20 15:05:36 +0000 | [diff] [blame] | 1495 | if (!CD->isTrivial() && !RD->hasAttr<WarnUnusedAttr>()) |
| Rafael Espindola | 7c23b08 | 2012-01-06 04:54:01 +0000 | [diff] [blame] | 1496 | return false; |
| 1497 | } |
| 1498 | } |
| Anders Carlsson | f5dc6fa | 2009-11-07 07:26:56 +0000 | [diff] [blame] | 1499 | } |
| 1500 | } |
| John McCall | cef1582 | 2010-03-31 02:47:45 +0000 | [diff] [blame] | 1501 | |
| 1502 | // TODO: __attribute__((unused)) templates? |
| Anders Carlsson | f5dc6fa | 2009-11-07 07:26:56 +0000 | [diff] [blame] | 1503 | } |
| 1504 | |
| John McCall | 67da35c | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 1505 | return true; |
| Anders Carlsson | 2889e0e | 2009-11-07 07:18:14 +0000 | [diff] [blame] | 1506 | } |
| 1507 | |
| Anna Zaks | 964f4c6 | 2011-07-28 20:52:06 +0000 | [diff] [blame] | 1508 | static void GenerateFixForUnusedDecl(const NamedDecl *D, ASTContext &Ctx, |
| 1509 | FixItHint &Hint) { |
| 1510 | if (isa<LabelDecl>(D)) { |
| 1511 | SourceLocation AfterColon = Lexer::findLocationAfterToken(D->getLocEnd(), |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1512 | tok::colon, Ctx.getSourceManager(), Ctx.getLangOpts(), true); |
| Anna Zaks | 964f4c6 | 2011-07-28 20:52:06 +0000 | [diff] [blame] | 1513 | if (AfterColon.isInvalid()) |
| 1514 | return; |
| 1515 | Hint = FixItHint::CreateRemoval(CharSourceRange:: |
| 1516 | getCharRange(D->getLocStart(), AfterColon)); |
| 1517 | } |
| 1518 | return; |
| 1519 | } |
| 1520 | |
| Nico Weber | 7288943 | 2014-09-06 01:25:55 +0000 | [diff] [blame] | 1521 | void Sema::DiagnoseUnusedNestedTypedefs(const RecordDecl *D) { |
| 1522 | if (D->getTypeForDecl()->isDependentType()) |
| 1523 | return; |
| 1524 | |
| 1525 | for (auto *TmpD : D->decls()) { |
| 1526 | if (const auto *T = dyn_cast<TypedefNameDecl>(TmpD)) |
| 1527 | DiagnoseUnusedDecl(T); |
| 1528 | else if(const auto *R = dyn_cast<RecordDecl>(TmpD)) |
| 1529 | DiagnoseUnusedNestedTypedefs(R); |
| 1530 | } |
| 1531 | } |
| 1532 | |
| Chris Lattner | ebb5c6c | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1533 | /// DiagnoseUnusedDecl - Emit warnings about declarations that are not used |
| 1534 | /// unless they are marked attr(unused). |
| Douglas Gregor | 14f232e | 2010-05-08 23:05:03 +0000 | [diff] [blame] | 1535 | void Sema::DiagnoseUnusedDecl(const NamedDecl *D) { |
| 1536 | if (!ShouldDiagnoseUnusedDecl(D)) |
| 1537 | return; |
| Nico Weber | 7288943 | 2014-09-06 01:25:55 +0000 | [diff] [blame] | 1538 | |
| 1539 | if (auto *TD = dyn_cast<TypedefNameDecl>(D)) { |
| 1540 | // typedefs can be referenced later on, so the diagnostics are emitted |
| 1541 | // at end-of-translation-unit. |
| 1542 | UnusedLocalTypedefNameCandidates.insert(TD); |
| 1543 | return; |
| 1544 | } |
| Douglas Gregor | 14f232e | 2010-05-08 23:05:03 +0000 | [diff] [blame] | 1545 | |
| Nico Weber | dfc5920 | 2014-05-03 22:07:35 +0000 | [diff] [blame] | 1546 | FixItHint Hint; |
| Anna Zaks | 964f4c6 | 2011-07-28 20:52:06 +0000 | [diff] [blame] | 1547 | GenerateFixForUnusedDecl(D, Context, Hint); |
| 1548 | |
| Chris Lattner | cab02a6 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 1549 | unsigned DiagID; |
| Douglas Gregor | 14f232e | 2010-05-08 23:05:03 +0000 | [diff] [blame] | 1550 | if (isa<VarDecl>(D) && cast<VarDecl>(D)->isExceptionVariable()) |
| Chris Lattner | cab02a6 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 1551 | DiagID = diag::warn_unused_exception_param; |
| 1552 | else if (isa<LabelDecl>(D)) |
| 1553 | DiagID = diag::warn_unused_label; |
| Douglas Gregor | 14f232e | 2010-05-08 23:05:03 +0000 | [diff] [blame] | 1554 | else |
| Chris Lattner | cab02a6 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 1555 | DiagID = diag::warn_unused_variable; |
| 1556 | |
| Anna Zaks | 964f4c6 | 2011-07-28 20:52:06 +0000 | [diff] [blame] | 1557 | Diag(D->getLocation(), DiagID) << D->getDeclName() << Hint; |
| Douglas Gregor | 14f232e | 2010-05-08 23:05:03 +0000 | [diff] [blame] | 1558 | } |
| 1559 | |
| Chris Lattner | ebb5c6c | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1560 | static void CheckPoppedLabel(LabelDecl *L, Sema &S) { |
| 1561 | // Verify that we have no forward references left. If so, there was a goto |
| 1562 | // or address of a label taken, but no definition of it. Label fwd |
| Ehsan Akhgari | 3109758 | 2014-09-22 02:21:54 +0000 | [diff] [blame] | 1563 | // definitions are indicated with a null substmt which is also not a resolved |
| 1564 | // MS inline assembly label name. |
| 1565 | bool Diagnose = false; |
| 1566 | if (L->isMSAsmLabel()) |
| 1567 | Diagnose = !L->isResolvedMSAsmLabel(); |
| 1568 | else |
| 1569 | Diagnose = L->getStmt() == nullptr; |
| 1570 | if (Diagnose) |
| Chris Lattner | ebb5c6c | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1571 | S.Diag(L->getLocation(), diag::err_undeclared_label_use) <<L->getDeclName(); |
| 1572 | } |
| 1573 | |
| Steve Naroff | c62adb6 | 2007-10-09 22:01:59 +0000 | [diff] [blame] | 1574 | void Sema::ActOnPopScope(SourceLocation Loc, Scope *S) { |
| Nick Lewycky | d78f92f | 2014-05-03 00:41:18 +0000 | [diff] [blame] | 1575 | S->mergeNRVOIntoParent(); |
| 1576 | |
| Chris Lattner | 1a76a3c | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 1577 | if (S->decl_empty()) return; |
| Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 1578 | assert((S->getFlags() & (Scope::DeclScope | Scope::TemplateParamScope)) && |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1579 | "Scope shouldn't contain decls!"); |
| Argyrios Kyrtzidis | fa8e15b | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 1580 | |
| Aaron Ballman | 35c5495 | 2014-03-17 16:55:25 +0000 | [diff] [blame] | 1581 | for (auto *TmpD : S->decls()) { |
| Steve Naroff | 9324db1 | 2007-09-13 18:10:37 +0000 | [diff] [blame] | 1582 | assert(TmpD && "This decl didn't get pushed??"); |
| Argyrios Kyrtzidis | 406fb23 | 2008-06-10 01:32:09 +0000 | [diff] [blame] | 1583 | |
| Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1584 | assert(isa<NamedDecl>(TmpD) && "Decl isn't NamedDecl?"); |
| 1585 | NamedDecl *D = cast<NamedDecl>(TmpD); |
| Argyrios Kyrtzidis | 406fb23 | 2008-06-10 01:32:09 +0000 | [diff] [blame] | 1586 | |
| Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1587 | if (!D->getDeclName()) continue; |
| Chris Lattner | c5c95b5 | 2008-04-11 07:00:53 +0000 | [diff] [blame] | 1588 | |
| Douglas Gregor | 3beaf9b | 2009-10-08 21:35:42 +0000 | [diff] [blame] | 1589 | // Diagnose unused variables in this scope. |
| Nico Weber | 7288943 | 2014-09-06 01:25:55 +0000 | [diff] [blame] | 1590 | if (!S->hasUnrecoverableErrorOccurred()) { |
| Douglas Gregor | 14f232e | 2010-05-08 23:05:03 +0000 | [diff] [blame] | 1591 | DiagnoseUnusedDecl(D); |
| Nico Weber | 7288943 | 2014-09-06 01:25:55 +0000 | [diff] [blame] | 1592 | if (const auto *RD = dyn_cast<RecordDecl>(D)) |
| 1593 | DiagnoseUnusedNestedTypedefs(RD); |
| 1594 | } |
| Douglas Gregor | 14f232e | 2010-05-08 23:05:03 +0000 | [diff] [blame] | 1595 | |
| Chris Lattner | ebb5c6c | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1596 | // If this was a forward reference to a label, verify it was defined. |
| 1597 | if (LabelDecl *LD = dyn_cast<LabelDecl>(D)) |
| 1598 | CheckPoppedLabel(LD, *this); |
| 1599 | |
| Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1600 | // Remove this name from our lexical scope. |
| 1601 | IdResolver.RemoveDecl(D); |
| Chris Lattner | 302b4be | 2006-11-19 02:31:38 +0000 | [diff] [blame] | 1602 | } |
| 1603 | } |
| 1604 | |
| Douglas Gregor | 1c28331 | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 1605 | /// \brief Look for an Objective-C class in the translation unit. |
| 1606 | /// |
| 1607 | /// \param Id The name of the Objective-C class we're looking for. If |
| 1608 | /// typo-correction fixes this name, the Id will be updated |
| 1609 | /// to the fixed name. |
| 1610 | /// |
| 1611 | /// \param IdLoc The location of the name in the translation unit. |
| 1612 | /// |
| James Dennett | 4172512 | 2012-06-22 10:16:05 +0000 | [diff] [blame] | 1613 | /// \param DoTypoCorrection If true, this routine will attempt typo correction |
| Douglas Gregor | 1c28331 | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 1614 | /// if there is no class with the given name. |
| 1615 | /// |
| 1616 | /// \returns The declaration of the named Objective-C class, or NULL if the |
| 1617 | /// class could not be found. |
| 1618 | ObjCInterfaceDecl *Sema::getObjCInterfaceDecl(IdentifierInfo *&Id, |
| 1619 | SourceLocation IdLoc, |
| Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1620 | bool DoTypoCorrection) { |
| Douglas Gregor | 1c28331 | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 1621 | // The third "scope" argument is 0 since we aren't enabling lazy built-in |
| 1622 | // creation from this context. |
| 1623 | NamedDecl *IDecl = LookupSingleName(TUScope, Id, IdLoc, LookupOrdinaryName); |
| 1624 | |
| Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1625 | if (!IDecl && DoTypoCorrection) { |
| Douglas Gregor | 1c28331 | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 1626 | // Perform typo correction at the given location, but only if we |
| 1627 | // find an Objective-C class name. |
| Kaelyn Takata | 89c881b | 2014-10-27 18:07:29 +0000 | [diff] [blame] | 1628 | if (TypoCorrection C = CorrectTypo( |
| 1629 | DeclarationNameInfo(Id, IdLoc), LookupOrdinaryName, TUScope, nullptr, |
| 1630 | llvm::make_unique<DeclFilterCCC<ObjCInterfaceDecl>>(), |
| 1631 | CTK_ErrorRecovery)) { |
| Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 1632 | diagnoseTypo(C, PDiag(diag::err_undef_interface_suggest) << Id); |
| Kaelyn Uhrain | b137840 | 2012-01-18 21:41:41 +0000 | [diff] [blame] | 1633 | IDecl = C.getCorrectionDeclAs<ObjCInterfaceDecl>(); |
| Douglas Gregor | 1c28331 | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 1634 | Id = IDecl->getIdentifier(); |
| 1635 | } |
| 1636 | } |
| Fariborz Jahanian | 4f8cb1e | 2012-01-12 00:18:35 +0000 | [diff] [blame] | 1637 | ObjCInterfaceDecl *Def = dyn_cast_or_null<ObjCInterfaceDecl>(IDecl); |
| 1638 | // This routine must always return a class definition, if any. |
| 1639 | if (Def && Def->getDefinition()) |
| 1640 | Def = Def->getDefinition(); |
| 1641 | return Def; |
| Douglas Gregor | 1c28331 | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 1642 | } |
| 1643 | |
| Douglas Gregor | 45a33ec | 2009-01-12 18:45:55 +0000 | [diff] [blame] | 1644 | /// getNonFieldDeclScope - Retrieves the innermost scope, starting |
| 1645 | /// from S, where a non-field would be declared. This routine copes |
| 1646 | /// with the difference between C and C++ scoping rules in structs and |
| 1647 | /// unions. For example, the following code is well-formed in C but |
| 1648 | /// ill-formed in C++: |
| 1649 | /// @code |
| 1650 | /// struct S6 { |
| 1651 | /// enum { BAR } e; |
| 1652 | /// }; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1653 | /// |
| Douglas Gregor | 45a33ec | 2009-01-12 18:45:55 +0000 | [diff] [blame] | 1654 | /// void test_S6() { |
| 1655 | /// struct S6 a; |
| 1656 | /// a.e = BAR; |
| 1657 | /// } |
| 1658 | /// @endcode |
| 1659 | /// For the declaration of BAR, this routine will return a different |
| 1660 | /// scope. The scope S will be the scope of the unnamed enumeration |
| 1661 | /// within S6. In C++, this routine will return the scope associated |
| 1662 | /// with S6, because the enumeration's scope is a transparent |
| 1663 | /// context but structures can contain non-field names. In C, this |
| 1664 | /// routine will return the translation unit scope, since the |
| 1665 | /// enumeration's scope is a transparent context and structures cannot |
| 1666 | /// contain non-field names. |
| 1667 | Scope *Sema::getNonFieldDeclScope(Scope *S) { |
| 1668 | while (((S->getFlags() & Scope::DeclScope) == 0) || |
| Ted Kremenek | c37877d | 2013-10-08 17:08:03 +0000 | [diff] [blame] | 1669 | (S->getEntity() && S->getEntity()->isTransparentContext()) || |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1670 | (S->isClassScope() && !getLangOpts().CPlusPlus)) |
| Douglas Gregor | 45a33ec | 2009-01-12 18:45:55 +0000 | [diff] [blame] | 1671 | S = S->getParent(); |
| 1672 | return S; |
| 1673 | } |
| 1674 | |
| Fariborz Jahanian | cb6c867 | 2013-01-04 18:45:40 +0000 | [diff] [blame] | 1675 | /// \brief Looks up the declaration of "struct objc_super" and |
| 1676 | /// saves it for later use in building builtin declaration of |
| 1677 | /// objc_msgSendSuper and objc_msgSendSuper_stret. If no such |
| 1678 | /// pre-existing declaration exists no action takes place. |
| 1679 | static void LookupPredefedObjCSuperType(Sema &ThisSema, Scope *S, |
| 1680 | IdentifierInfo *II) { |
| 1681 | if (!II->isStr("objc_msgSendSuper")) |
| 1682 | return; |
| 1683 | ASTContext &Context = ThisSema.Context; |
| 1684 | |
| 1685 | LookupResult Result(ThisSema, &Context.Idents.get("objc_super"), |
| 1686 | SourceLocation(), Sema::LookupTagName); |
| 1687 | ThisSema.LookupName(Result, S); |
| 1688 | if (Result.getResultKind() == LookupResult::Found) |
| 1689 | if (const TagDecl *TD = Result.getAsSingle<TagDecl>()) |
| 1690 | Context.setObjCSuperType(Context.getTagDeclType(TD)); |
| 1691 | } |
| 1692 | |
| Alp Toker | 5d96e0a | 2014-07-11 20:53:51 +0000 | [diff] [blame] | 1693 | static StringRef getHeaderName(ASTContext::GetBuiltinTypeError Error) { |
| 1694 | switch (Error) { |
| 1695 | case ASTContext::GE_None: |
| 1696 | return ""; |
| 1697 | case ASTContext::GE_Missing_stdio: |
| 1698 | return "stdio.h"; |
| 1699 | case ASTContext::GE_Missing_setjmp: |
| 1700 | return "setjmp.h"; |
| 1701 | case ASTContext::GE_Missing_ucontext: |
| 1702 | return "ucontext.h"; |
| 1703 | } |
| 1704 | llvm_unreachable("unhandled error kind"); |
| 1705 | } |
| 1706 | |
| Douglas Gregor | b9063fc | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 1707 | /// LazilyCreateBuiltin - The specified Builtin-ID was first used at |
| 1708 | /// file scope. lazily create a decl for it. ForRedeclaration is true |
| 1709 | /// if we're creating this built-in in anticipation of redeclaring the |
| 1710 | /// built-in. |
| Alexey Samsonov | 75bb28e | 2014-08-28 23:34:32 +0000 | [diff] [blame] | 1711 | NamedDecl *Sema::LazilyCreateBuiltin(IdentifierInfo *II, unsigned ID, |
| Douglas Gregor | b9063fc | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 1712 | Scope *S, bool ForRedeclaration, |
| 1713 | SourceLocation Loc) { |
| Fariborz Jahanian | cb6c867 | 2013-01-04 18:45:40 +0000 | [diff] [blame] | 1714 | LookupPredefedObjCSuperType(*this, S, II); |
| Chris Lattner | 9561a0b | 2007-01-28 08:20:04 +0000 | [diff] [blame] | 1715 | |
| Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 1716 | ASTContext::GetBuiltinTypeError Error; |
| Alexey Samsonov | 75bb28e | 2014-08-28 23:34:32 +0000 | [diff] [blame] | 1717 | QualType R = Context.GetBuiltinType(ID, Error); |
| Alp Toker | 5d96e0a | 2014-07-11 20:53:51 +0000 | [diff] [blame] | 1718 | if (Error) { |
| Douglas Gregor | 538c3d8 | 2009-02-14 01:52:53 +0000 | [diff] [blame] | 1719 | if (ForRedeclaration) |
| Alp Toker | 5d96e0a | 2014-07-11 20:53:51 +0000 | [diff] [blame] | 1720 | Diag(Loc, diag::warn_implicit_decl_requires_sysheader) |
| 1721 | << getHeaderName(Error) |
| Alexey Samsonov | 75bb28e | 2014-08-28 23:34:32 +0000 | [diff] [blame] | 1722 | << Context.BuiltinInfo.GetName(ID); |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1723 | return nullptr; |
| Douglas Gregor | 538c3d8 | 2009-02-14 01:52:53 +0000 | [diff] [blame] | 1724 | } |
| Douglas Gregor | b9063fc | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 1725 | |
| Alexey Samsonov | 75bb28e | 2014-08-28 23:34:32 +0000 | [diff] [blame] | 1726 | if (!ForRedeclaration && Context.BuiltinInfo.isPredefinedLibFunction(ID)) { |
| Douglas Gregor | b9063fc | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 1727 | Diag(Loc, diag::ext_implicit_lib_function_decl) |
| Alexey Samsonov | 75bb28e | 2014-08-28 23:34:32 +0000 | [diff] [blame] | 1728 | << Context.BuiltinInfo.GetName(ID) |
| Douglas Gregor | b9063fc | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 1729 | << R; |
| Alexey Samsonov | 75bb28e | 2014-08-28 23:34:32 +0000 | [diff] [blame] | 1730 | if (Context.BuiltinInfo.getHeaderName(ID) && |
| Alp Toker | d4a3f0e | 2014-06-15 23:30:39 +0000 | [diff] [blame] | 1731 | !Diags.isIgnored(diag::ext_implicit_lib_function_decl, Loc)) |
| Alp Toker | 5d96e0a | 2014-07-11 20:53:51 +0000 | [diff] [blame] | 1732 | Diag(Loc, diag::note_include_header_or_declare) |
| Alexey Samsonov | 75bb28e | 2014-08-28 23:34:32 +0000 | [diff] [blame] | 1733 | << Context.BuiltinInfo.getHeaderName(ID) |
| 1734 | << Context.BuiltinInfo.GetName(ID); |
| Douglas Gregor | b9063fc | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 1735 | } |
| 1736 | |
| Warren Hunt | 445d83e | 2013-11-01 23:46:51 +0000 | [diff] [blame] | 1737 | DeclContext *Parent = Context.getTranslationUnitDecl(); |
| 1738 | if (getLangOpts().CPlusPlus) { |
| 1739 | LinkageSpecDecl *CLinkageDecl = |
| 1740 | LinkageSpecDecl::Create(Context, Parent, Loc, Loc, |
| 1741 | LinkageSpecDecl::lang_c, false); |
| Enea Zaffanella | d843092 | 2013-11-20 15:41:05 +0000 | [diff] [blame] | 1742 | CLinkageDecl->setImplicit(); |
| Warren Hunt | 445d83e | 2013-11-01 23:46:51 +0000 | [diff] [blame] | 1743 | Parent->addDecl(CLinkageDecl); |
| 1744 | Parent = CLinkageDecl; |
| 1745 | } |
| 1746 | |
| Argyrios Kyrtzidis | 6d05303 | 2008-04-17 14:47:13 +0000 | [diff] [blame] | 1747 | FunctionDecl *New = FunctionDecl::Create(Context, |
| Warren Hunt | 445d83e | 2013-11-01 23:46:51 +0000 | [diff] [blame] | 1748 | Parent, |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1749 | Loc, Loc, II, R, /*TInfo=*/nullptr, |
| John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 1750 | SC_Extern, |
| Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 1751 | false, |
| Douglas Gregor | 739ef0c | 2009-02-25 16:33:18 +0000 | [diff] [blame] | 1752 | /*hasPrototype=*/true); |
| Douglas Gregor | b9063fc | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 1753 | New->setImplicit(); |
| 1754 | |
| Chris Lattner | 4dd2710 | 2008-05-05 22:18:14 +0000 | [diff] [blame] | 1755 | // Create Decl objects for each parameter, adding them to the |
| 1756 | // FunctionDecl. |
| John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1757 | if (const FunctionProtoType *FT = dyn_cast<FunctionProtoType>(R)) { |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1758 | SmallVector<ParmVarDecl*, 16> Params; |
| Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 1759 | for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) { |
| John McCall | 8fb0d9d | 2011-05-01 22:35:37 +0000 | [diff] [blame] | 1760 | ParmVarDecl *parm = |
| Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 1761 | ParmVarDecl::Create(Context, New, SourceLocation(), SourceLocation(), |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1762 | nullptr, FT->getParamType(i), /*TInfo=*/nullptr, |
| 1763 | SC_None, nullptr); |
| John McCall | 8fb0d9d | 2011-05-01 22:35:37 +0000 | [diff] [blame] | 1764 | parm->setScopeInfo(0, i); |
| 1765 | Params.push_back(parm); |
| 1766 | } |
| David Blaikie | 9c70e04 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 1767 | New->setParams(Params); |
| Chris Lattner | 4dd2710 | 2008-05-05 22:18:14 +0000 | [diff] [blame] | 1768 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1769 | |
| 1770 | AddKnownFunctionAttributes(New); |
| Warren Hunt | 445d83e | 2013-11-01 23:46:51 +0000 | [diff] [blame] | 1771 | RegisterLocallyScopedExternCDecl(New, S); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1772 | |
| Chris Lattner | c5c95b5 | 2008-04-11 07:00:53 +0000 | [diff] [blame] | 1773 | // TUScope is the translation-unit scope to insert this function into. |
| Douglas Gregor | c72e645 | 2009-01-09 18:51:29 +0000 | [diff] [blame] | 1774 | // FIXME: This is hideous. We need to teach PushOnScopeChains to |
| 1775 | // relate Scopes to DeclContexts, and probably eliminate CurContext |
| 1776 | // entirely, but we're not there yet. |
| 1777 | DeclContext *SavedContext = CurContext; |
| Warren Hunt | 445d83e | 2013-11-01 23:46:51 +0000 | [diff] [blame] | 1778 | CurContext = Parent; |
| Argyrios Kyrtzidis | fa8e15b | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 1779 | PushOnScopeChains(New, TUScope); |
| Douglas Gregor | c72e645 | 2009-01-09 18:51:29 +0000 | [diff] [blame] | 1780 | CurContext = SavedContext; |
| Chris Lattner | 9561a0b | 2007-01-28 08:20:04 +0000 | [diff] [blame] | 1781 | return New; |
| 1782 | } |
| 1783 | |
| Douglas Gregor | 3552dab | 2013-01-09 00:47:56 +0000 | [diff] [blame] | 1784 | /// \brief Filter out any previous declarations that the given declaration |
| 1785 | /// should not consider because they are not permitted to conflict, e.g., |
| 1786 | /// because they come from hidden sub-modules and do not refer to the same |
| 1787 | /// entity. |
| 1788 | static void filterNonConflictingPreviousDecls(ASTContext &context, |
| 1789 | NamedDecl *decl, |
| 1790 | LookupResult &previous){ |
| 1791 | // This is only interesting when modules are enabled. |
| 1792 | if (!context.getLangOpts().Modules) |
| 1793 | return; |
| 1794 | |
| 1795 | // Empty sets are uninteresting. |
| 1796 | if (previous.empty()) |
| 1797 | return; |
| 1798 | |
| Douglas Gregor | 3552dab | 2013-01-09 00:47:56 +0000 | [diff] [blame] | 1799 | LookupResult::Filter filter = previous.makeFilter(); |
| 1800 | while (filter.hasNext()) { |
| 1801 | NamedDecl *old = filter.next(); |
| 1802 | |
| 1803 | // Non-hidden declarations are never ignored. |
| 1804 | if (!old->isHidden()) |
| 1805 | continue; |
| 1806 | |
| Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 1807 | if (!old->isExternallyVisible()) |
| Douglas Gregor | 3552dab | 2013-01-09 00:47:56 +0000 | [diff] [blame] | 1808 | filter.erase(); |
| 1809 | } |
| 1810 | |
| 1811 | filter.done(); |
| 1812 | } |
| 1813 | |
| Richard Smith | ca40f9b | 2014-08-10 02:20:15 +0000 | [diff] [blame] | 1814 | /// Typedef declarations don't have linkage, but they still denote the same |
| 1815 | /// entity if their types are the same. |
| 1816 | /// FIXME: This is notionally doing the same thing as ASTReaderDecl's |
| 1817 | /// isSameEntity. |
| 1818 | static void filterNonConflictingPreviousTypedefDecls(ASTContext &Context, |
| 1819 | TypedefNameDecl *Decl, |
| 1820 | LookupResult &Previous) { |
| 1821 | // This is only interesting when modules are enabled. |
| 1822 | if (!Context.getLangOpts().Modules) |
| 1823 | return; |
| 1824 | |
| 1825 | // Empty sets are uninteresting. |
| 1826 | if (Previous.empty()) |
| 1827 | return; |
| 1828 | |
| 1829 | LookupResult::Filter Filter = Previous.makeFilter(); |
| 1830 | while (Filter.hasNext()) { |
| 1831 | NamedDecl *Old = Filter.next(); |
| 1832 | |
| 1833 | // Non-hidden declarations are never ignored. |
| 1834 | if (!Old->isHidden()) |
| 1835 | continue; |
| 1836 | |
| 1837 | // Declarations of the same entity are not ignored, even if they have |
| 1838 | // different linkages. |
| Richard Smith | a523022 | 2015-03-27 01:37:43 +0000 | [diff] [blame] | 1839 | if (auto *OldTD = dyn_cast<TypedefNameDecl>(Old)) { |
| Richard Smith | ca40f9b | 2014-08-10 02:20:15 +0000 | [diff] [blame] | 1840 | if (Context.hasSameType(OldTD->getUnderlyingType(), |
| 1841 | Decl->getUnderlyingType())) |
| 1842 | continue; |
| 1843 | |
| Richard Smith | a523022 | 2015-03-27 01:37:43 +0000 | [diff] [blame] | 1844 | // If both declarations give a tag declaration a typedef name for linkage |
| 1845 | // purposes, then they declare the same entity. |
| 1846 | if (OldTD->getAnonDeclWithTypedefName() && |
| 1847 | Decl->getAnonDeclWithTypedefName()) |
| 1848 | continue; |
| 1849 | } |
| 1850 | |
| Richard Smith | ca40f9b | 2014-08-10 02:20:15 +0000 | [diff] [blame] | 1851 | if (!Old->isExternallyVisible()) |
| 1852 | Filter.erase(); |
| 1853 | } |
| 1854 | |
| 1855 | Filter.done(); |
| 1856 | } |
| 1857 | |
| Rafael Espindola | cde2c8f | 2011-12-26 22:42:47 +0000 | [diff] [blame] | 1858 | bool Sema::isIncompatibleTypedef(TypeDecl *Old, TypedefNameDecl *New) { |
| 1859 | QualType OldType; |
| 1860 | if (TypedefNameDecl *OldTypedef = dyn_cast<TypedefNameDecl>(Old)) |
| 1861 | OldType = OldTypedef->getUnderlyingType(); |
| 1862 | else |
| 1863 | OldType = Context.getTypeDeclType(Old); |
| 1864 | QualType NewType = New->getUnderlyingType(); |
| 1865 | |
| Douglas Gregor | aab3698 | 2012-01-11 22:33:48 +0000 | [diff] [blame] | 1866 | if (NewType->isVariablyModifiedType()) { |
| 1867 | // Must not redefine a typedef with a variably-modified type. |
| 1868 | int Kind = isa<TypeAliasDecl>(Old) ? 1 : 0; |
| 1869 | Diag(New->getLocation(), diag::err_redefinition_variably_modified_typedef) |
| 1870 | << Kind << NewType; |
| 1871 | if (Old->getLocation().isValid()) |
| 1872 | Diag(Old->getLocation(), diag::note_previous_definition); |
| 1873 | New->setInvalidDecl(); |
| 1874 | return true; |
| 1875 | } |
| 1876 | |
| Rafael Espindola | cde2c8f | 2011-12-26 22:42:47 +0000 | [diff] [blame] | 1877 | if (OldType != NewType && |
| 1878 | !OldType->isDependentType() && |
| 1879 | !NewType->isDependentType() && |
| Douglas Gregor | aab3698 | 2012-01-11 22:33:48 +0000 | [diff] [blame] | 1880 | !Context.hasSameType(OldType, NewType)) { |
| Rafael Espindola | cde2c8f | 2011-12-26 22:42:47 +0000 | [diff] [blame] | 1881 | int Kind = isa<TypeAliasDecl>(Old) ? 1 : 0; |
| 1882 | Diag(New->getLocation(), diag::err_redefinition_different_typedef) |
| 1883 | << Kind << NewType << OldType; |
| 1884 | if (Old->getLocation().isValid()) |
| 1885 | Diag(Old->getLocation(), diag::note_previous_definition); |
| 1886 | New->setInvalidDecl(); |
| 1887 | return true; |
| 1888 | } |
| 1889 | return false; |
| 1890 | } |
| 1891 | |
| Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1892 | /// MergeTypedefNameDecl - We just parsed a typedef 'New' which has the |
| Douglas Gregor | 75a45ba | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 1893 | /// same name and scope as a previous declaration 'Old'. Figure out |
| 1894 | /// how to resolve this situation, merging decls or emitting |
| Chris Lattner | f6d1c9c | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 1895 | /// 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] | 1896 | /// |
| Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1897 | void Sema::MergeTypedefNameDecl(TypedefNameDecl *New, LookupResult &OldDecls) { |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1898 | // If the new decl is known invalid already, don't bother doing any |
| 1899 | // merging checks. |
| 1900 | if (New->isInvalidDecl()) return; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1901 | |
| Steve Naroff | 44cfcb6 | 2008-09-09 14:32:20 +0000 | [diff] [blame] | 1902 | // Allow multiple definitions for ObjC built-in typedefs. |
| 1903 | // FIXME: Verify the underlying types are equivalent! |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1904 | if (getLangOpts().ObjC1) { |
| Chris Lattner | 66e3281 | 2008-11-20 05:41:43 +0000 | [diff] [blame] | 1905 | const IdentifierInfo *TypeID = New->getIdentifier(); |
| 1906 | switch (TypeID->getLength()) { |
| 1907 | default: break; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1908 | case 2: |
| Fariborz Jahanian | 16d71bb | 2012-05-14 22:48:56 +0000 | [diff] [blame] | 1909 | { |
| 1910 | if (!TypeID->isStr("id")) |
| 1911 | break; |
| 1912 | QualType T = New->getUnderlyingType(); |
| 1913 | if (!T->isPointerType()) |
| 1914 | break; |
| 1915 | if (!T->isVoidPointerType()) { |
| 1916 | QualType PT = T->getAs<PointerType>()->getPointeeType(); |
| 1917 | if (!PT->isStructureType()) |
| 1918 | break; |
| 1919 | } |
| 1920 | Context.setObjCIdRedefinitionType(T); |
| 1921 | // Install the built-in type for 'id', ignoring the current definition. |
| 1922 | New->setTypeForDecl(Context.getObjCIdType().getTypePtr()); |
| 1923 | return; |
| 1924 | } |
| Chris Lattner | 66e3281 | 2008-11-20 05:41:43 +0000 | [diff] [blame] | 1925 | case 5: |
| 1926 | if (!TypeID->isStr("Class")) |
| 1927 | break; |
| Douglas Gregor | 9767347 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 1928 | Context.setObjCClassRedefinitionType(New->getUnderlyingType()); |
| Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1929 | // Install the built-in type for 'Class', ignoring the current definition. |
| 1930 | New->setTypeForDecl(Context.getObjCClassType().getTypePtr()); |
| Chris Lattner | f6d1c9c | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 1931 | return; |
| Chris Lattner | 66e3281 | 2008-11-20 05:41:43 +0000 | [diff] [blame] | 1932 | case 3: |
| 1933 | if (!TypeID->isStr("SEL")) |
| 1934 | break; |
| Douglas Gregor | 9767347 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 1935 | Context.setObjCSelRedefinitionType(New->getUnderlyingType()); |
| Fariborz Jahanian | 252ba5f | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 1936 | // Install the built-in type for 'SEL', ignoring the current definition. |
| 1937 | New->setTypeForDecl(Context.getObjCSelType().getTypePtr()); |
| Chris Lattner | f6d1c9c | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 1938 | return; |
| Steve Naroff | 44cfcb6 | 2008-09-09 14:32:20 +0000 | [diff] [blame] | 1939 | } |
| 1940 | // Fall through - the typedef name was not a builtin type. |
| 1941 | } |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1942 | |
| Douglas Gregor | fb03466 | 2009-01-28 17:15:10 +0000 | [diff] [blame] | 1943 | // Verify the old decl was also a type. |
| John McCall | 91f1a02 | 2009-12-30 00:31:22 +0000 | [diff] [blame] | 1944 | TypeDecl *Old = OldDecls.getAsSingle<TypeDecl>(); |
| 1945 | if (!Old) { |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1946 | Diag(New->getLocation(), diag::err_redefinition_different_kind) |
| Chris Lattner | e3d20d9 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 1947 | << New->getDeclName(); |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1948 | |
| 1949 | NamedDecl *OldD = OldDecls.getRepresentativeDecl(); |
| Chris Lattner | f6d1c9c | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 1950 | if (OldD->getLocation().isValid()) |
| Fariborz Jahanian | 1778f4b | 2009-01-16 19:58:32 +0000 | [diff] [blame] | 1951 | Diag(OldD->getLocation(), diag::note_previous_definition); |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1952 | |
| Chris Lattner | f6d1c9c | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 1953 | return New->setInvalidDecl(); |
| Chris Lattner | c511efb | 2007-01-27 19:32:14 +0000 | [diff] [blame] | 1954 | } |
| Douglas Gregor | fb03466 | 2009-01-28 17:15:10 +0000 | [diff] [blame] | 1955 | |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1956 | // If the old declaration is invalid, just give up here. |
| 1957 | if (Old->isInvalidDecl()) |
| 1958 | return New->setInvalidDecl(); |
| 1959 | |
| Richard Smith | a523022 | 2015-03-27 01:37:43 +0000 | [diff] [blame] | 1960 | if (auto *OldTD = dyn_cast<TypedefNameDecl>(Old)) { |
| 1961 | auto *OldTag = OldTD->getAnonDeclWithTypedefName(); |
| 1962 | auto *NewTag = New->getAnonDeclWithTypedefName(); |
| 1963 | NamedDecl *Hidden = nullptr; |
| 1964 | if (getLangOpts().CPlusPlus && OldTag && NewTag && |
| 1965 | OldTag->getCanonicalDecl() != NewTag->getCanonicalDecl() && |
| 1966 | !hasVisibleDefinition(OldTag, &Hidden)) { |
| 1967 | // There is a definition of this tag, but it is not visible. Use it |
| 1968 | // instead of our tag. |
| 1969 | New->setTypeForDecl(OldTD->getTypeForDecl()); |
| 1970 | if (OldTD->isModed()) |
| 1971 | New->setModedTypeSourceInfo(OldTD->getTypeSourceInfo(), |
| 1972 | OldTD->getUnderlyingType()); |
| 1973 | else |
| 1974 | New->setTypeSourceInfo(OldTD->getTypeSourceInfo()); |
| 1975 | |
| 1976 | // Make the old tag definition visible. |
| 1977 | if (auto *Listener = getASTMutationListener()) |
| 1978 | Listener->RedefinedHiddenDefinition(Hidden, NewTag->getLocation()); |
| 1979 | Hidden->setHidden(false); |
| 1980 | } |
| 1981 | } |
| 1982 | |
| Chris Lattner | f9c49e5 | 2008-07-25 18:44:27 +0000 | [diff] [blame] | 1983 | // If the typedef types are not identical, reject them in all languages and |
| 1984 | // with any extensions enabled. |
| Rafael Espindola | cde2c8f | 2011-12-26 22:42:47 +0000 | [diff] [blame] | 1985 | if (isIncompatibleTypedef(Old, New)) |
| 1986 | return; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1987 | |
| Justin Bogner | 84ff5ee | 2013-10-08 00:19:09 +0000 | [diff] [blame] | 1988 | // The types match. Link up the redeclaration chain and merge attributes if |
| 1989 | // the old declaration was a typedef. |
| 1990 | if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Old)) { |
| Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 1991 | New->setPreviousDecl(Typedef); |
| Justin Bogner | 84ff5ee | 2013-10-08 00:19:09 +0000 | [diff] [blame] | 1992 | mergeDeclAttributes(New, Old); |
| 1993 | } |
| Eli Friedman | e7b8aa9 | 2013-07-16 02:07:49 +0000 | [diff] [blame] | 1994 | |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1995 | if (getLangOpts().MicrosoftExt) |
| Chris Lattner | f6d1c9c | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 1996 | return; |
| Eli Friedman | 61b529f | 2008-06-11 06:20:39 +0000 | [diff] [blame] | 1997 | |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1998 | if (getLangOpts().CPlusPlus) { |
| Douglas Gregor | 9dd13ab | 2010-01-11 21:54:40 +0000 | [diff] [blame] | 1999 | // C++ [dcl.typedef]p2: |
| 2000 | // In a given non-class scope, a typedef specifier can be used to |
| 2001 | // redefine the name of any type declared in that scope to refer |
| 2002 | // to the type to which it already refers. |
| Chris Lattner | 2581fc3 | 2009-04-17 22:04:20 +0000 | [diff] [blame] | 2003 | if (!isa<CXXRecordDecl>(CurContext)) |
| Chris Lattner | f6d1c9c | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 2004 | return; |
| Douglas Gregor | 9dd13ab | 2010-01-11 21:54:40 +0000 | [diff] [blame] | 2005 | |
| 2006 | // C++0x [dcl.typedef]p4: |
| 2007 | // In a given class scope, a typedef specifier can be used to redefine |
| 2008 | // any class-name declared in that scope that is not also a typedef-name |
| 2009 | // to refer to the type to which it already refers. |
| 2010 | // |
| 2011 | // This wording came in via DR424, which was a correction to the |
| 2012 | // wording in DR56, which accidentally banned code like: |
| 2013 | // |
| 2014 | // struct S { |
| 2015 | // typedef struct A { } A; |
| 2016 | // }; |
| 2017 | // |
| 2018 | // in the C++03 standard. We implement the C++0x semantics, which |
| 2019 | // allow the above but disallow |
| 2020 | // |
| 2021 | // struct S { |
| 2022 | // typedef int I; |
| 2023 | // typedef int I; |
| 2024 | // }; |
| 2025 | // |
| 2026 | // since that was the intent of DR56. |
| Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2027 | if (!isa<TypedefNameDecl>(Old)) |
| Douglas Gregor | 9dd13ab | 2010-01-11 21:54:40 +0000 | [diff] [blame] | 2028 | return; |
| 2029 | |
| Chris Lattner | 2581fc3 | 2009-04-17 22:04:20 +0000 | [diff] [blame] | 2030 | Diag(New->getLocation(), diag::err_redefinition) |
| 2031 | << New->getDeclName(); |
| 2032 | Diag(Old->getLocation(), diag::note_previous_definition); |
| Chris Lattner | f6d1c9c | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 2033 | return New->setInvalidDecl(); |
| Daniel Dunbar | 84b70f7 | 2008-09-12 18:10:20 +0000 | [diff] [blame] | 2034 | } |
| Eli Friedman | 61b529f | 2008-06-11 06:20:39 +0000 | [diff] [blame] | 2035 | |
| Douglas Gregor | 7363fb0 | 2012-01-11 04:25:01 +0000 | [diff] [blame] | 2036 | // Modules always permit redefinition of typedefs, as does C11. |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2037 | if (getLangOpts().Modules || getLangOpts().C11) |
| Douglas Gregor | dbd93bf | 2012-01-09 15:36:04 +0000 | [diff] [blame] | 2038 | return; |
| 2039 | |
| Chris Lattner | 2581fc3 | 2009-04-17 22:04:20 +0000 | [diff] [blame] | 2040 | // If we have a redefinition of a typedef in C, emit a warning. This warning |
| 2041 | // is normally mapped to an error, but can be controlled with |
| Eli Friedman | 319ce95 | 2009-06-04 23:03:07 +0000 | [diff] [blame] | 2042 | // -Wtypedef-redefinition. If either the original or the redefinition is |
| 2043 | // in a system header, don't emit this for compatibility with GCC. |
| Chris Lattner | 30d0cfd | 2010-03-01 20:59:53 +0000 | [diff] [blame] | 2044 | if (getDiagnostics().getSuppressSystemWarnings() && |
| Eli Friedman | 319ce95 | 2009-06-04 23:03:07 +0000 | [diff] [blame] | 2045 | (Context.getSourceManager().isInSystemHeader(Old->getLocation()) || |
| 2046 | Context.getSourceManager().isInSystemHeader(New->getLocation()))) |
| Chris Lattner | 9a84589 | 2009-04-27 01:46:12 +0000 | [diff] [blame] | 2047 | return; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2048 | |
| Richard Smith | 1b98ccc | 2014-07-19 01:39:17 +0000 | [diff] [blame] | 2049 | Diag(New->getLocation(), diag::ext_redefinition_of_typedef) |
| Chris Lattner | 2581fc3 | 2009-04-17 22:04:20 +0000 | [diff] [blame] | 2050 | << New->getDeclName(); |
| Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 2051 | Diag(Old->getLocation(), diag::note_previous_definition); |
| Chris Lattner | 01564d9 | 2007-01-27 19:27:06 +0000 | [diff] [blame] | 2052 | } |
| 2053 | |
| Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2054 | /// DeclhasAttr - returns true if decl Declaration already has the target |
| 2055 | /// attribute. |
| Aaron Ballman | b9023ed | 2014-01-20 18:07:09 +0000 | [diff] [blame] | 2056 | static bool DeclHasAttr(const Decl *D, const Attr *A) { |
| Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2057 | const OwnershipAttr *OA = dyn_cast<OwnershipAttr>(A); |
| Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 2058 | const AnnotateAttr *Ann = dyn_cast<AnnotateAttr>(A); |
| Aaron Ballman | b97112e | 2014-03-08 22:19:01 +0000 | [diff] [blame] | 2059 | for (const auto *i : D->attrs()) |
| 2060 | if (i->getKind() == A->getKind()) { |
| Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 2061 | if (Ann) { |
| Aaron Ballman | b97112e | 2014-03-08 22:19:01 +0000 | [diff] [blame] | 2062 | if (Ann->getAnnotation() == cast<AnnotateAttr>(i)->getAnnotation()) |
| Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 2063 | return true; |
| 2064 | continue; |
| 2065 | } |
| Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2066 | // FIXME: Don't hardcode this check |
| Aaron Ballman | b97112e | 2014-03-08 22:19:01 +0000 | [diff] [blame] | 2067 | if (OA && isa<OwnershipAttr>(i)) |
| 2068 | return OA->getOwnKind() == cast<OwnershipAttr>(i)->getOwnKind(); |
| Chris Lattner | 8496639 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 2069 | return true; |
| Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2070 | } |
| Chris Lattner | 8496639 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 2071 | |
| 2072 | return false; |
| 2073 | } |
| 2074 | |
| Richard Smith | bc8caaf | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 2075 | static bool isAttributeTargetADefinition(Decl *D) { |
| 2076 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) |
| 2077 | return VD->isThisDeclarationADefinition(); |
| 2078 | if (TagDecl *TD = dyn_cast<TagDecl>(D)) |
| 2079 | return TD->isCompleteDefinition() || TD->isBeingDefined(); |
| 2080 | return true; |
| 2081 | } |
| 2082 | |
| 2083 | /// Merge alignment attributes from \p Old to \p New, taking into account the |
| 2084 | /// special semantics of C11's _Alignas specifier and C++11's alignas attribute. |
| 2085 | /// |
| 2086 | /// \return \c true if any attributes were added to \p New. |
| 2087 | static bool mergeAlignedAttrs(Sema &S, NamedDecl *New, Decl *Old) { |
| 2088 | // Look for alignas attributes on Old, and pick out whichever attribute |
| 2089 | // specifies the strictest alignment requirement. |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2090 | AlignedAttr *OldAlignasAttr = nullptr; |
| 2091 | AlignedAttr *OldStrictestAlignAttr = nullptr; |
| Richard Smith | bc8caaf | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 2092 | unsigned OldAlign = 0; |
| Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 2093 | for (auto *I : Old->specific_attrs<AlignedAttr>()) { |
| Richard Smith | bc8caaf | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 2094 | // FIXME: We have no way of representing inherited dependent alignments |
| 2095 | // in a case like: |
| 2096 | // template<int A, int B> struct alignas(A) X; |
| 2097 | // template<int A, int B> struct alignas(B) X {}; |
| 2098 | // For now, we just ignore any alignas attributes which are not on the |
| 2099 | // definition in such a case. |
| 2100 | if (I->isAlignmentDependent()) |
| 2101 | return false; |
| 2102 | |
| 2103 | if (I->isAlignas()) |
| Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 2104 | OldAlignasAttr = I; |
| Richard Smith | bc8caaf | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 2105 | |
| 2106 | unsigned Align = I->getAlignment(S.Context); |
| 2107 | if (Align > OldAlign) { |
| 2108 | OldAlign = Align; |
| Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 2109 | OldStrictestAlignAttr = I; |
| Richard Smith | bc8caaf | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 2110 | } |
| 2111 | } |
| 2112 | |
| 2113 | // Look for alignas attributes on New. |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2114 | AlignedAttr *NewAlignasAttr = nullptr; |
| Richard Smith | bc8caaf | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 2115 | unsigned NewAlign = 0; |
| Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 2116 | for (auto *I : New->specific_attrs<AlignedAttr>()) { |
| Richard Smith | bc8caaf | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 2117 | if (I->isAlignmentDependent()) |
| 2118 | return false; |
| 2119 | |
| 2120 | if (I->isAlignas()) |
| Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 2121 | NewAlignasAttr = I; |
| Richard Smith | bc8caaf | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 2122 | |
| 2123 | unsigned Align = I->getAlignment(S.Context); |
| 2124 | if (Align > NewAlign) |
| 2125 | NewAlign = Align; |
| 2126 | } |
| 2127 | |
| 2128 | if (OldAlignasAttr && NewAlignasAttr && OldAlign != NewAlign) { |
| 2129 | // Both declarations have 'alignas' attributes. We require them to match. |
| 2130 | // C++11 [dcl.align]p6 and C11 6.7.5/7 both come close to saying this, but |
| 2131 | // fall short. (If two declarations both have alignas, they must both match |
| 2132 | // every definition, and so must match each other if there is a definition.) |
| 2133 | |
| 2134 | // If either declaration only contains 'alignas(0)' specifiers, then it |
| 2135 | // specifies the natural alignment for the type. |
| 2136 | if (OldAlign == 0 || NewAlign == 0) { |
| 2137 | QualType Ty; |
| 2138 | if (ValueDecl *VD = dyn_cast<ValueDecl>(New)) |
| 2139 | Ty = VD->getType(); |
| 2140 | else |
| 2141 | Ty = S.Context.getTagDeclType(cast<TagDecl>(New)); |
| 2142 | |
| 2143 | if (OldAlign == 0) |
| 2144 | OldAlign = S.Context.getTypeAlign(Ty); |
| 2145 | if (NewAlign == 0) |
| 2146 | NewAlign = S.Context.getTypeAlign(Ty); |
| 2147 | } |
| 2148 | |
| 2149 | if (OldAlign != NewAlign) { |
| 2150 | S.Diag(NewAlignasAttr->getLocation(), diag::err_alignas_mismatch) |
| 2151 | << (unsigned)S.Context.toCharUnitsFromBits(OldAlign).getQuantity() |
| 2152 | << (unsigned)S.Context.toCharUnitsFromBits(NewAlign).getQuantity(); |
| 2153 | S.Diag(OldAlignasAttr->getLocation(), diag::note_previous_declaration); |
| 2154 | } |
| 2155 | } |
| 2156 | |
| 2157 | if (OldAlignasAttr && !NewAlignasAttr && isAttributeTargetADefinition(New)) { |
| 2158 | // C++11 [dcl.align]p6: |
| 2159 | // if any declaration of an entity has an alignment-specifier, |
| 2160 | // every defining declaration of that entity shall specify an |
| 2161 | // equivalent alignment. |
| 2162 | // C11 6.7.5/7: |
| 2163 | // If the definition of an object does not have an alignment |
| 2164 | // specifier, any other declaration of that object shall also |
| 2165 | // have no alignment specifier. |
| 2166 | S.Diag(New->getLocation(), diag::err_alignas_missing_on_definition) |
| Aaron Ballman | 3d216a5 | 2014-01-02 23:39:11 +0000 | [diff] [blame] | 2167 | << OldAlignasAttr; |
| Richard Smith | bc8caaf | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 2168 | S.Diag(OldAlignasAttr->getLocation(), diag::note_alignas_on_declaration) |
| Aaron Ballman | 3d216a5 | 2014-01-02 23:39:11 +0000 | [diff] [blame] | 2169 | << OldAlignasAttr; |
| Richard Smith | bc8caaf | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 2170 | } |
| 2171 | |
| 2172 | bool AnyAdded = false; |
| 2173 | |
| 2174 | // Ensure we have an attribute representing the strictest alignment. |
| 2175 | if (OldAlign > NewAlign) { |
| 2176 | AlignedAttr *Clone = OldStrictestAlignAttr->clone(S.Context); |
| 2177 | Clone->setInherited(true); |
| 2178 | New->addAttr(Clone); |
| 2179 | AnyAdded = true; |
| 2180 | } |
| 2181 | |
| 2182 | // Ensure we have an alignas attribute if the old declaration had one. |
| 2183 | if (OldAlignasAttr && !NewAlignasAttr && |
| 2184 | !(AnyAdded && OldStrictestAlignAttr->isAlignas())) { |
| 2185 | AlignedAttr *Clone = OldAlignasAttr->clone(S.Context); |
| 2186 | Clone->setInherited(true); |
| 2187 | New->addAttr(Clone); |
| 2188 | AnyAdded = true; |
| 2189 | } |
| 2190 | |
| 2191 | return AnyAdded; |
| 2192 | } |
| 2193 | |
| Aaron Ballman | 7a2fb5f | 2014-04-17 20:08:36 +0000 | [diff] [blame] | 2194 | static bool mergeDeclAttribute(Sema &S, NamedDecl *D, |
| 2195 | const InheritableAttr *Attr, bool Override) { |
| 2196 | InheritableAttr *NewAttr = nullptr; |
| Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2197 | unsigned AttrSpellingListIndex = Attr->getSpellingListIndex(); |
| Aaron Ballman | 7a2fb5f | 2014-04-17 20:08:36 +0000 | [diff] [blame] | 2198 | if (const auto *AA = dyn_cast<AvailabilityAttr>(Attr)) |
| Richard Smith | bc8caaf | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 2199 | NewAttr = S.mergeAvailabilityAttr(D, AA->getRange(), AA->getPlatform(), |
| 2200 | AA->getIntroduced(), AA->getDeprecated(), |
| 2201 | AA->getObsoleted(), AA->getUnavailable(), |
| 2202 | AA->getMessage(), Override, |
| John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2203 | AttrSpellingListIndex); |
| Aaron Ballman | 7a2fb5f | 2014-04-17 20:08:36 +0000 | [diff] [blame] | 2204 | else if (const auto *VA = dyn_cast<VisibilityAttr>(Attr)) |
| Richard Smith | bc8caaf | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 2205 | NewAttr = S.mergeVisibilityAttr(D, VA->getRange(), VA->getVisibility(), |
| 2206 | AttrSpellingListIndex); |
| Aaron Ballman | 7a2fb5f | 2014-04-17 20:08:36 +0000 | [diff] [blame] | 2207 | else if (const auto *VA = dyn_cast<TypeVisibilityAttr>(Attr)) |
| Richard Smith | bc8caaf | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 2208 | NewAttr = S.mergeTypeVisibilityAttr(D, VA->getRange(), VA->getVisibility(), |
| 2209 | AttrSpellingListIndex); |
| Aaron Ballman | 7a2fb5f | 2014-04-17 20:08:36 +0000 | [diff] [blame] | 2210 | else if (const auto *ImportA = dyn_cast<DLLImportAttr>(Attr)) |
| Richard Smith | bc8caaf | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 2211 | NewAttr = S.mergeDLLImportAttr(D, ImportA->getRange(), |
| 2212 | AttrSpellingListIndex); |
| Aaron Ballman | 7a2fb5f | 2014-04-17 20:08:36 +0000 | [diff] [blame] | 2213 | else if (const auto *ExportA = dyn_cast<DLLExportAttr>(Attr)) |
| Richard Smith | bc8caaf | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 2214 | NewAttr = S.mergeDLLExportAttr(D, ExportA->getRange(), |
| 2215 | AttrSpellingListIndex); |
| Aaron Ballman | 7a2fb5f | 2014-04-17 20:08:36 +0000 | [diff] [blame] | 2216 | else if (const auto *FA = dyn_cast<FormatAttr>(Attr)) |
| Richard Smith | bc8caaf | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 2217 | NewAttr = S.mergeFormatAttr(D, FA->getRange(), FA->getType(), |
| 2218 | FA->getFormatIdx(), FA->getFirstArg(), |
| 2219 | AttrSpellingListIndex); |
| Aaron Ballman | 7a2fb5f | 2014-04-17 20:08:36 +0000 | [diff] [blame] | 2220 | else if (const auto *SA = dyn_cast<SectionAttr>(Attr)) |
| Richard Smith | bc8caaf | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 2221 | NewAttr = S.mergeSectionAttr(D, SA->getRange(), SA->getName(), |
| 2222 | AttrSpellingListIndex); |
| Aaron Ballman | 7a2fb5f | 2014-04-17 20:08:36 +0000 | [diff] [blame] | 2223 | else if (const auto *IA = dyn_cast<MSInheritanceAttr>(Attr)) |
| David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 2224 | NewAttr = S.mergeMSInheritanceAttr(D, IA->getRange(), IA->getBestCase(), |
| 2225 | AttrSpellingListIndex, |
| David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 2226 | IA->getSemanticSpelling()); |
| Paul Robinson | 30e41fb | 2014-12-15 18:57:28 +0000 | [diff] [blame] | 2227 | else if (const auto *AA = dyn_cast<AlwaysInlineAttr>(Attr)) |
| Paul Robinson | 080b1f3 | 2015-01-13 18:34:56 +0000 | [diff] [blame] | 2228 | NewAttr = S.mergeAlwaysInlineAttr(D, AA->getRange(), |
| 2229 | &S.Context.Idents.get(AA->getSpelling()), |
| 2230 | AttrSpellingListIndex); |
| Paul Robinson | 30e41fb | 2014-12-15 18:57:28 +0000 | [diff] [blame] | 2231 | else if (const auto *MA = dyn_cast<MinSizeAttr>(Attr)) |
| 2232 | NewAttr = S.mergeMinSizeAttr(D, MA->getRange(), AttrSpellingListIndex); |
| 2233 | else if (const auto *OA = dyn_cast<OptimizeNoneAttr>(Attr)) |
| 2234 | NewAttr = S.mergeOptimizeNoneAttr(D, OA->getRange(), AttrSpellingListIndex); |
| Richard Smith | bc8caaf | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 2235 | else if (isa<AlignedAttr>(Attr)) |
| 2236 | // AlignedAttrs are handled separately, because we need to handle all |
| 2237 | // such attributes on a declaration at the same time. |
| Aaron Ballman | 7a2fb5f | 2014-04-17 20:08:36 +0000 | [diff] [blame] | 2238 | NewAttr = nullptr; |
| Fariborz Jahanian | b93355e | 2014-07-17 17:05:04 +0000 | [diff] [blame] | 2239 | else if (isa<DeprecatedAttr>(Attr) && Override) |
| 2240 | NewAttr = nullptr; |
| Aaron Ballman | b9023ed | 2014-01-20 18:07:09 +0000 | [diff] [blame] | 2241 | else if (Attr->duplicatesAllowed() || !DeclHasAttr(D, Attr)) |
| Richard Smith | bc8caaf | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 2242 | NewAttr = cast<InheritableAttr>(Attr->clone(S.Context)); |
| Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2243 | |
| Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2244 | if (NewAttr) { |
| Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2245 | NewAttr->setInherited(true); |
| 2246 | D->addAttr(NewAttr); |
| 2247 | return true; |
| 2248 | } |
| 2249 | |
| 2250 | return false; |
| 2251 | } |
| 2252 | |
| Rafael Espindola | a5bba70 | 2012-07-15 01:05:36 +0000 | [diff] [blame] | 2253 | static const Decl *getDefinition(const Decl *D) { |
| 2254 | if (const TagDecl *TD = dyn_cast<TagDecl>(D)) |
| Rafael Espindola | 3619104 | 2012-05-18 01:47:00 +0000 | [diff] [blame] | 2255 | return TD->getDefinition(); |
| Rafael Espindola | d53ffa0 | 2013-10-22 21:39:03 +0000 | [diff] [blame] | 2256 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 2257 | const VarDecl *Def = VD->getDefinition(); |
| 2258 | if (Def) |
| 2259 | return Def; |
| 2260 | return VD->getActingDefinition(); |
| 2261 | } |
| Rafael Espindola | a5bba70 | 2012-07-15 01:05:36 +0000 | [diff] [blame] | 2262 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| Rafael Espindola | 3619104 | 2012-05-18 01:47:00 +0000 | [diff] [blame] | 2263 | const FunctionDecl* Def; |
| Rafael Espindola | d53ffa0 | 2013-10-22 21:39:03 +0000 | [diff] [blame] | 2264 | if (FD->isDefined(Def)) |
| Rafael Espindola | 3619104 | 2012-05-18 01:47:00 +0000 | [diff] [blame] | 2265 | return Def; |
| 2266 | } |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2267 | return nullptr; |
| Rafael Espindola | 3619104 | 2012-05-18 01:47:00 +0000 | [diff] [blame] | 2268 | } |
| 2269 | |
| Rafael Espindola | faf556b | 2012-07-15 01:33:40 +0000 | [diff] [blame] | 2270 | static bool hasAttribute(const Decl *D, attr::Kind Kind) { |
| Aaron Ballman | b97112e | 2014-03-08 22:19:01 +0000 | [diff] [blame] | 2271 | for (const auto *Attribute : D->attrs()) |
| Rafael Espindola | faf556b | 2012-07-15 01:33:40 +0000 | [diff] [blame] | 2272 | if (Attribute->getKind() == Kind) |
| 2273 | return true; |
| Rafael Espindola | faf556b | 2012-07-15 01:33:40 +0000 | [diff] [blame] | 2274 | return false; |
| 2275 | } |
| 2276 | |
| 2277 | /// checkNewAttributesAfterDef - If we already have a definition, check that |
| 2278 | /// there are no new attributes in this declaration. |
| 2279 | static void checkNewAttributesAfterDef(Sema &S, Decl *New, const Decl *Old) { |
| 2280 | if (!New->hasAttrs()) |
| 2281 | return; |
| 2282 | |
| 2283 | const Decl *Def = getDefinition(Old); |
| 2284 | if (!Def || Def == New) |
| 2285 | return; |
| 2286 | |
| 2287 | AttrVec &NewAttributes = New->getAttrs(); |
| 2288 | for (unsigned I = 0, E = NewAttributes.size(); I != E;) { |
| 2289 | const Attr *NewAttribute = NewAttributes[I]; |
| Rafael Espindola | d53ffa0 | 2013-10-22 21:39:03 +0000 | [diff] [blame] | 2290 | |
| 2291 | if (isa<AliasAttr>(NewAttribute)) { |
| David Majnemer | 6b90356 | 2015-01-19 10:21:22 +0000 | [diff] [blame] | 2292 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(New)) |
| 2293 | S.CheckForFunctionRedefinition(FD, cast<FunctionDecl>(Def)); |
| 2294 | else { |
| 2295 | VarDecl *VD = cast<VarDecl>(New); |
| David Majnemer | 290d347 | 2015-01-21 01:30:40 +0000 | [diff] [blame] | 2296 | unsigned Diag = cast<VarDecl>(Def)->isThisDeclarationADefinition() == |
| 2297 | VarDecl::TentativeDefinition |
| 2298 | ? diag::err_alias_after_tentative |
| 2299 | : diag::err_redefinition; |
| 2300 | S.Diag(VD->getLocation(), Diag) << VD->getDeclName(); |
| David Majnemer | 6b90356 | 2015-01-19 10:21:22 +0000 | [diff] [blame] | 2301 | S.Diag(Def->getLocation(), diag::note_previous_definition); |
| 2302 | VD->setInvalidDecl(); |
| 2303 | } |
| Rafael Espindola | d53ffa0 | 2013-10-22 21:39:03 +0000 | [diff] [blame] | 2304 | ++I; |
| 2305 | continue; |
| 2306 | } |
| 2307 | |
| 2308 | if (const VarDecl *VD = dyn_cast<VarDecl>(Def)) { |
| 2309 | // Tentative definitions are only interesting for the alias check above. |
| 2310 | if (VD->isThisDeclarationADefinition() != VarDecl::Definition) { |
| 2311 | ++I; |
| 2312 | continue; |
| 2313 | } |
| 2314 | } |
| 2315 | |
| Rafael Espindola | faf556b | 2012-07-15 01:33:40 +0000 | [diff] [blame] | 2316 | if (hasAttribute(Def, NewAttribute->getKind())) { |
| 2317 | ++I; |
| 2318 | continue; // regular attr merging will take care of validating this. |
| 2319 | } |
| Richard Smith | bc8caaf | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 2320 | |
| Richard Smith | debc59d | 2013-01-30 05:45:05 +0000 | [diff] [blame] | 2321 | if (isa<C11NoReturnAttr>(NewAttribute)) { |
| Richard Smith | bc8caaf | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 2322 | // 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] | 2323 | ++I; |
| 2324 | continue; |
| Richard Smith | bc8caaf | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 2325 | } else if (const AlignedAttr *AA = dyn_cast<AlignedAttr>(NewAttribute)) { |
| 2326 | if (AA->isAlignas()) { |
| 2327 | // C++11 [dcl.align]p6: |
| 2328 | // if any declaration of an entity has an alignment-specifier, |
| 2329 | // every defining declaration of that entity shall specify an |
| 2330 | // equivalent alignment. |
| 2331 | // C11 6.7.5/7: |
| 2332 | // If the definition of an object does not have an alignment |
| 2333 | // specifier, any other declaration of that object shall also |
| 2334 | // have no alignment specifier. |
| 2335 | S.Diag(Def->getLocation(), diag::err_alignas_missing_on_definition) |
| Aaron Ballman | 3d216a5 | 2014-01-02 23:39:11 +0000 | [diff] [blame] | 2336 | << AA; |
| Richard Smith | bc8caaf | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 2337 | S.Diag(NewAttribute->getLocation(), diag::note_alignas_on_declaration) |
| Aaron Ballman | 3d216a5 | 2014-01-02 23:39:11 +0000 | [diff] [blame] | 2338 | << AA; |
| Richard Smith | bc8caaf | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 2339 | NewAttributes.erase(NewAttributes.begin() + I); |
| 2340 | --E; |
| 2341 | continue; |
| 2342 | } |
| Richard Smith | debc59d | 2013-01-30 05:45:05 +0000 | [diff] [blame] | 2343 | } |
| Richard Smith | bc8caaf | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 2344 | |
| Rafael Espindola | faf556b | 2012-07-15 01:33:40 +0000 | [diff] [blame] | 2345 | S.Diag(NewAttribute->getLocation(), |
| 2346 | diag::warn_attribute_precede_definition); |
| 2347 | S.Diag(Def->getLocation(), diag::note_previous_definition); |
| 2348 | NewAttributes.erase(NewAttributes.begin() + I); |
| 2349 | --E; |
| 2350 | } |
| 2351 | } |
| 2352 | |
| John McCall | f79e87d | 2011-03-02 04:00:57 +0000 | [diff] [blame] | 2353 | /// mergeDeclAttributes - Copy attributes from the Old decl to the New one. |
| Rafael Espindola | a3aea43 | 2013-01-08 22:04:34 +0000 | [diff] [blame] | 2354 | void Sema::mergeDeclAttributes(NamedDecl *New, Decl *Old, |
| Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2355 | AvailabilityMergeKind AMK) { |
| Rafael Espindola | b093885 | 2013-10-25 01:28:12 +0000 | [diff] [blame] | 2356 | if (UsedAttr *OldAttr = Old->getMostRecentDecl()->getAttr<UsedAttr>()) { |
| 2357 | UsedAttr *NewAttr = OldAttr->clone(Context); |
| 2358 | NewAttr->setInherited(true); |
| 2359 | New->addAttr(NewAttr); |
| 2360 | } |
| 2361 | |
| Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 2362 | if (!Old->hasAttrs() && !New->hasAttrs()) |
| 2363 | return; |
| 2364 | |
| Rafael Espindola | 3619104 | 2012-05-18 01:47:00 +0000 | [diff] [blame] | 2365 | // attributes declared post-definition are currently ignored |
| Rafael Espindola | faf556b | 2012-07-15 01:33:40 +0000 | [diff] [blame] | 2366 | checkNewAttributesAfterDef(*this, New, Old); |
| Rafael Espindola | 3619104 | 2012-05-18 01:47:00 +0000 | [diff] [blame] | 2367 | |
| Douglas Gregor | 32c1757 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 2368 | if (!Old->hasAttrs()) |
| Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2369 | return; |
| John McCall | f79e87d | 2011-03-02 04:00:57 +0000 | [diff] [blame] | 2370 | |
| Douglas Gregor | 32c1757 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 2371 | bool foundAny = New->hasAttrs(); |
| John McCall | f79e87d | 2011-03-02 04:00:57 +0000 | [diff] [blame] | 2372 | |
| Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2373 | // Ensure that any moving of objects within the allocated map is done before |
| 2374 | // we process them. |
| Douglas Gregor | 32c1757 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 2375 | if (!foundAny) New->setAttrs(AttrVec()); |
| John McCall | f79e87d | 2011-03-02 04:00:57 +0000 | [diff] [blame] | 2376 | |
| Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 2377 | for (auto *I : Old->specific_attrs<InheritableAttr>()) { |
| Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2378 | bool Override = false; |
| Douglas Gregor | b1fa148 | 2011-09-23 20:23:42 +0000 | [diff] [blame] | 2379 | // Ignore deprecated/unavailable/availability attributes if requested. |
| Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 2380 | if (isa<DeprecatedAttr>(I) || |
| 2381 | isa<UnavailableAttr>(I) || |
| 2382 | isa<AvailabilityAttr>(I)) { |
| Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2383 | switch (AMK) { |
| 2384 | case AMK_None: |
| 2385 | continue; |
| John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 2386 | |
| Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2387 | case AMK_Redeclaration: |
| 2388 | break; |
| 2389 | |
| 2390 | case AMK_Override: |
| 2391 | Override = true; |
| 2392 | break; |
| 2393 | } |
| 2394 | } |
| 2395 | |
| Rafael Espindola | b093885 | 2013-10-25 01:28:12 +0000 | [diff] [blame] | 2396 | // Already handled. |
| Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 2397 | if (isa<UsedAttr>(I)) |
| Rafael Espindola | b093885 | 2013-10-25 01:28:12 +0000 | [diff] [blame] | 2398 | continue; |
| 2399 | |
| Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 2400 | if (mergeDeclAttribute(*this, New, I, Override)) |
| John McCall | f79e87d | 2011-03-02 04:00:57 +0000 | [diff] [blame] | 2401 | foundAny = true; |
| Chris Lattner | 8496639 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 2402 | } |
| John McCall | f79e87d | 2011-03-02 04:00:57 +0000 | [diff] [blame] | 2403 | |
| Richard Smith | bc8caaf | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 2404 | if (mergeAlignedAttrs(*this, New, Old)) |
| 2405 | foundAny = true; |
| 2406 | |
| Douglas Gregor | 32c1757 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 2407 | if (!foundAny) New->dropAttrs(); |
| John McCall | f79e87d | 2011-03-02 04:00:57 +0000 | [diff] [blame] | 2408 | } |
| 2409 | |
| 2410 | /// mergeParamDeclAttributes - Copy attributes from the old parameter |
| 2411 | /// to the new one. |
| 2412 | static void mergeParamDeclAttributes(ParmVarDecl *newDecl, |
| 2413 | const ParmVarDecl *oldDecl, |
| Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 2414 | Sema &S) { |
| 2415 | // C++11 [dcl.attr.depend]p2: |
| 2416 | // The first declaration of a function shall specify the |
| 2417 | // carries_dependency attribute for its declarator-id if any declaration |
| 2418 | // of the function specifies the carries_dependency attribute. |
| Aaron Ballman | cf3b483 | 2013-12-19 13:36:16 +0000 | [diff] [blame] | 2419 | const CarriesDependencyAttr *CDA = newDecl->getAttr<CarriesDependencyAttr>(); |
| 2420 | if (CDA && !oldDecl->hasAttr<CarriesDependencyAttr>()) { |
| 2421 | S.Diag(CDA->getLocation(), |
| Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 2422 | diag::err_carries_dependency_missing_on_first_decl) << 1/*Param*/; |
| 2423 | // Find the first declaration of the parameter. |
| 2424 | // FIXME: Should we build redeclaration chains for function parameters? |
| 2425 | const FunctionDecl *FirstFD = |
| Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 2426 | cast<FunctionDecl>(oldDecl->getDeclContext())->getFirstDecl(); |
| Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 2427 | const ParmVarDecl *FirstVD = |
| 2428 | FirstFD->getParamDecl(oldDecl->getFunctionScopeIndex()); |
| 2429 | S.Diag(FirstVD->getLocation(), |
| 2430 | diag::note_carries_dependency_missing_first_decl) << 1/*Param*/; |
| 2431 | } |
| 2432 | |
| John McCall | f79e87d | 2011-03-02 04:00:57 +0000 | [diff] [blame] | 2433 | if (!oldDecl->hasAttrs()) |
| 2434 | return; |
| 2435 | |
| 2436 | bool foundAny = newDecl->hasAttrs(); |
| 2437 | |
| 2438 | // Ensure that any moving of objects within the allocated map is |
| 2439 | // done before we process them. |
| 2440 | if (!foundAny) newDecl->setAttrs(AttrVec()); |
| 2441 | |
| Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 2442 | for (const auto *I : oldDecl->specific_attrs<InheritableParamAttr>()) { |
| 2443 | if (!DeclHasAttr(newDecl, I)) { |
| Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 2444 | InheritableAttr *newAttr = |
| Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 2445 | cast<InheritableParamAttr>(I->clone(S.Context)); |
| John McCall | f79e87d | 2011-03-02 04:00:57 +0000 | [diff] [blame] | 2446 | newAttr->setInherited(true); |
| 2447 | newDecl->addAttr(newAttr); |
| 2448 | foundAny = true; |
| 2449 | } |
| 2450 | } |
| 2451 | |
| 2452 | if (!foundAny) newDecl->dropAttrs(); |
| Chris Lattner | 8496639 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 2453 | } |
| 2454 | |
| Dan Gohman | 28ade55 | 2010-07-26 21:25:24 +0000 | [diff] [blame] | 2455 | namespace { |
| 2456 | |
| Douglas Gregor | a74a297 | 2009-03-06 22:43:54 +0000 | [diff] [blame] | 2457 | /// Used in MergeFunctionDecl to keep track of function parameters in |
| 2458 | /// C. |
| 2459 | struct GNUCompatibleParamWarning { |
| 2460 | ParmVarDecl *OldParm; |
| 2461 | ParmVarDecl *NewParm; |
| 2462 | QualType PromotedType; |
| 2463 | }; |
| 2464 | |
| Dan Gohman | 28ade55 | 2010-07-26 21:25:24 +0000 | [diff] [blame] | 2465 | } |
| Anders Carlsson | 1f78b2b | 2009-12-04 22:33:25 +0000 | [diff] [blame] | 2466 | |
| 2467 | /// getSpecialMember - get the special member enum for a method. |
| Anders Carlsson | 05bf009 | 2010-04-22 05:40:53 +0000 | [diff] [blame] | 2468 | Sema::CXXSpecialMember Sema::getSpecialMember(const CXXMethodDecl *MD) { |
| Anders Carlsson | 1f78b2b | 2009-12-04 22:33:25 +0000 | [diff] [blame] | 2469 | if (const CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(MD)) { |
| Alexis Hunt | 80f00ff | 2011-05-10 19:08:14 +0000 | [diff] [blame] | 2470 | if (Ctor->isDefaultConstructor()) |
| 2471 | return Sema::CXXDefaultConstructor; |
| Alexis Hunt | d051b87 | 2011-05-26 01:26:05 +0000 | [diff] [blame] | 2472 | |
| 2473 | if (Ctor->isCopyConstructor()) |
| 2474 | return Sema::CXXCopyConstructor; |
| 2475 | |
| 2476 | if (Ctor->isMoveConstructor()) |
| 2477 | return Sema::CXXMoveConstructor; |
| Alexis Hunt | 119c10e | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 2478 | } else if (isa<CXXDestructorDecl>(MD)) { |
| Anders Carlsson | 1f78b2b | 2009-12-04 22:33:25 +0000 | [diff] [blame] | 2479 | return Sema::CXXDestructor; |
| Alexis Hunt | 119c10e | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 2480 | } else if (MD->isCopyAssignmentOperator()) { |
| Alexis Hunt | 80f00ff | 2011-05-10 19:08:14 +0000 | [diff] [blame] | 2481 | return Sema::CXXCopyAssignment; |
| Sebastian Redl | e9c4e84 | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 2482 | } else if (MD->isMoveAssignmentOperator()) { |
| 2483 | return Sema::CXXMoveAssignment; |
| Alexis Hunt | 119c10e | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 2484 | } |
| Alexis Hunt | 80f00ff | 2011-05-10 19:08:14 +0000 | [diff] [blame] | 2485 | |
| Alexis Hunt | 80f00ff | 2011-05-10 19:08:14 +0000 | [diff] [blame] | 2486 | return Sema::CXXInvalid; |
| Anders Carlsson | 1f78b2b | 2009-12-04 22:33:25 +0000 | [diff] [blame] | 2487 | } |
| 2488 | |
| David Majnemer | 5b63fa0 | 2014-06-18 23:26:25 +0000 | [diff] [blame] | 2489 | // Determine whether the previous declaration was a definition, implicit |
| 2490 | // declaration, or a declaration. |
| 2491 | template <typename T> |
| 2492 | static std::pair<diag::kind, SourceLocation> |
| 2493 | getNoteDiagForInvalidRedeclaration(const T *Old, const T *New) { |
| 2494 | diag::kind PrevDiag; |
| 2495 | SourceLocation OldLocation = Old->getLocation(); |
| 2496 | if (Old->isThisDeclarationADefinition()) |
| 2497 | PrevDiag = diag::note_previous_definition; |
| 2498 | else if (Old->isImplicit()) { |
| 2499 | PrevDiag = diag::note_previous_implicit_declaration; |
| 2500 | if (OldLocation.isInvalid()) |
| 2501 | OldLocation = New->getLocation(); |
| 2502 | } else |
| 2503 | PrevDiag = diag::note_previous_declaration; |
| 2504 | return std::make_pair(PrevDiag, OldLocation); |
| 2505 | } |
| 2506 | |
| Sebastian Redl | 243d905 | 2010-06-09 21:17:41 +0000 | [diff] [blame] | 2507 | /// canRedefineFunction - checks if a function can be redefined. Currently, |
| Charles Davis | fea4845 | 2010-02-18 02:00:42 +0000 | [diff] [blame] | 2508 | /// only extern inline functions can be redefined, and even then only in |
| 2509 | /// GNU89 mode. |
| 2510 | static bool canRedefineFunction(const FunctionDecl *FD, |
| 2511 | const LangOptions& LangOpts) { |
| Eli Friedman | 51dd018 | 2011-06-13 23:56:42 +0000 | [diff] [blame] | 2512 | return ((FD->hasAttr<GNUInlineAttr>() || LangOpts.GNUInline) && |
| 2513 | !LangOpts.CPlusPlus && |
| Charles Davis | fea4845 | 2010-02-18 02:00:42 +0000 | [diff] [blame] | 2514 | FD->isInlineSpecified() && |
| John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2515 | FD->getStorageClass() == SC_Extern); |
| Charles Davis | fea4845 | 2010-02-18 02:00:42 +0000 | [diff] [blame] | 2516 | } |
| 2517 | |
| Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 2518 | const AttributedType *Sema::getCallingConvAttributedType(QualType T) const { |
| 2519 | const AttributedType *AT = T->getAs<AttributedType>(); |
| 2520 | while (AT && !AT->isCallingConv()) |
| 2521 | AT = AT->getModifiedType()->getAs<AttributedType>(); |
| 2522 | return AT; |
| John McCall | a5f46fb | 2012-08-25 02:00:03 +0000 | [diff] [blame] | 2523 | } |
| 2524 | |
| Benjamin Kramer | 3e35026 | 2013-02-15 12:30:38 +0000 | [diff] [blame] | 2525 | template <typename T> |
| 2526 | static bool haveIncompatibleLanguageLinkages(const T *Old, const T *New) { |
| Rafael Espindola | f418765 | 2013-02-14 01:18:37 +0000 | [diff] [blame] | 2527 | const DeclContext *DC = Old->getDeclContext(); |
| 2528 | if (DC->isRecord()) |
| 2529 | return false; |
| 2530 | |
| 2531 | LanguageLinkage OldLinkage = Old->getLanguageLinkage(); |
| Rafael Espindola | 593537a | 2013-05-05 20:15:21 +0000 | [diff] [blame] | 2532 | if (OldLinkage == CXXLanguageLinkage && New->isInExternCContext()) |
| Rafael Espindola | f418765 | 2013-02-14 01:18:37 +0000 | [diff] [blame] | 2533 | return true; |
| Rafael Espindola | 593537a | 2013-05-05 20:15:21 +0000 | [diff] [blame] | 2534 | if (OldLinkage == CLanguageLinkage && New->isInExternCXXContext()) |
| Rafael Espindola | f418765 | 2013-02-14 01:18:37 +0000 | [diff] [blame] | 2535 | return true; |
| 2536 | return false; |
| 2537 | } |
| 2538 | |
| Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2539 | /// MergeFunctionDecl - We just parsed a function 'New' from |
| 2540 | /// declarator D which has the same name and scope as a previous |
| 2541 | /// declaration 'Old'. Figure out how to resolve this situation, |
| 2542 | /// merging decls or emitting diagnostics as appropriate. |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2543 | /// |
| 2544 | /// In C++, New and Old must be declarations that are not |
| 2545 | /// overloaded. Use IsOverload to determine whether New and Old are |
| 2546 | /// overloaded, and to select the Old declaration that New should be |
| 2547 | /// merged with. |
| Douglas Gregor | 75a45ba | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 2548 | /// |
| 2549 | /// Returns true if there was an error, false otherwise. |
| Richard Smith | 1881930 | 2014-02-06 01:31:33 +0000 | [diff] [blame] | 2550 | bool Sema::MergeFunctionDecl(FunctionDecl *New, NamedDecl *&OldD, |
| 2551 | Scope *S, bool MergeTypeWithOld) { |
| Chris Lattner | c511efb | 2007-01-27 19:32:14 +0000 | [diff] [blame] | 2552 | // Verify the old decl was also a function. |
| Alp Toker | a2794f9 | 2014-01-22 07:29:52 +0000 | [diff] [blame] | 2553 | FunctionDecl *Old = OldD->getAsFunction(); |
| Chris Lattner | c511efb | 2007-01-27 19:32:14 +0000 | [diff] [blame] | 2554 | if (!Old) { |
| John McCall | e29c5cd | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 2555 | if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(OldD)) { |
| John McCall | c70fca6 | 2013-04-03 21:19:47 +0000 | [diff] [blame] | 2556 | if (New->getFriendObjectKind()) { |
| 2557 | Diag(New->getLocation(), diag::err_using_decl_friend); |
| 2558 | Diag(Shadow->getTargetDecl()->getLocation(), |
| 2559 | diag::note_using_decl_target); |
| 2560 | Diag(Shadow->getUsingDecl()->getLocation(), |
| 2561 | diag::note_using_decl) << 0; |
| 2562 | return true; |
| 2563 | } |
| 2564 | |
| Richard Smith | 1881930 | 2014-02-06 01:31:33 +0000 | [diff] [blame] | 2565 | // C++11 [namespace.udecl]p14: |
| 2566 | // If a function declaration in namespace scope or block scope has the |
| 2567 | // same name and the same parameter-type-list as a function introduced |
| 2568 | // by a using-declaration, and the declarations do not declare the same |
| 2569 | // function, the program is ill-formed. |
| 2570 | |
| 2571 | // Check whether the two declarations might declare the same function. |
| 2572 | Old = dyn_cast<FunctionDecl>(Shadow->getTargetDecl()); |
| 2573 | if (Old && |
| 2574 | !Old->getDeclContext()->getRedeclContext()->Equals( |
| 2575 | New->getDeclContext()->getRedeclContext()) && |
| 2576 | !(Old->isExternC() && New->isExternC())) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2577 | Old = nullptr; |
| Richard Smith | 1881930 | 2014-02-06 01:31:33 +0000 | [diff] [blame] | 2578 | |
| 2579 | if (!Old) { |
| 2580 | Diag(New->getLocation(), diag::err_using_decl_conflict_reverse); |
| 2581 | Diag(Shadow->getTargetDecl()->getLocation(), |
| 2582 | diag::note_using_decl_target); |
| 2583 | Diag(Shadow->getUsingDecl()->getLocation(), diag::note_using_decl) << 0; |
| 2584 | return true; |
| 2585 | } |
| 2586 | OldD = Old; |
| 2587 | } else { |
| 2588 | Diag(New->getLocation(), diag::err_redefinition_different_kind) |
| 2589 | << New->getDeclName(); |
| 2590 | Diag(OldD->getLocation(), diag::note_previous_definition); |
| John McCall | e29c5cd | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 2591 | return true; |
| 2592 | } |
| Chris Lattner | c511efb | 2007-01-27 19:32:14 +0000 | [diff] [blame] | 2593 | } |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2594 | |
| David Majnemer | ea5092a | 2013-07-07 23:49:50 +0000 | [diff] [blame] | 2595 | // If the old declaration is invalid, just give up here. |
| 2596 | if (Old->isInvalidDecl()) |
| 2597 | return true; |
| 2598 | |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2599 | diag::kind PrevDiag; |
| David Majnemer | 5b63fa0 | 2014-06-18 23:26:25 +0000 | [diff] [blame] | 2600 | SourceLocation OldLocation; |
| 2601 | std::tie(PrevDiag, OldLocation) = |
| 2602 | getNoteDiagForInvalidRedeclaration(Old, New); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2603 | |
| Charles Davis | fea4845 | 2010-02-18 02:00:42 +0000 | [diff] [blame] | 2604 | // Don't complain about this if we're in GNU89 mode and the old function |
| 2605 | // is an extern inline function. |
| Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 2606 | // Don't complain about specializations. They are not supposed to have |
| 2607 | // storage classes. |
| Douglas Gregor | e62c0a4 | 2009-02-24 01:23:02 +0000 | [diff] [blame] | 2608 | if (!isa<CXXMethodDecl>(New) && !isa<CXXMethodDecl>(Old) && |
| John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2609 | New->getStorageClass() == SC_Static && |
| Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 2610 | Old->hasExternalFormalLinkage() && |
| Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 2611 | !New->getTemplateSpecializationInfo() && |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2612 | !canRedefineFunction(Old, getLangOpts())) { |
| 2613 | if (getLangOpts().MicrosoftExt) { |
| David Majnemer | 5b63fa0 | 2014-06-18 23:26:25 +0000 | [diff] [blame] | 2614 | Diag(New->getLocation(), diag::ext_static_non_static) << New; |
| Richard Smith | bdd1464 | 2014-02-04 01:14:30 +0000 | [diff] [blame] | 2615 | Diag(OldLocation, PrevDiag); |
| Francois Pichet | 6841a12 | 2011-04-22 19:50:06 +0000 | [diff] [blame] | 2616 | } else { |
| 2617 | Diag(New->getLocation(), diag::err_static_non_static) << New; |
| Richard Smith | bdd1464 | 2014-02-04 01:14:30 +0000 | [diff] [blame] | 2618 | Diag(OldLocation, PrevDiag); |
| Francois Pichet | 6841a12 | 2011-04-22 19:50:06 +0000 | [diff] [blame] | 2619 | return true; |
| 2620 | } |
| Douglas Gregor | e62c0a4 | 2009-02-24 01:23:02 +0000 | [diff] [blame] | 2621 | } |
| 2622 | |
| Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 2623 | |
| 2624 | // If a function is first declared with a calling convention, but is later |
| 2625 | // declared or defined without one, all following decls assume the calling |
| 2626 | // convention of the first. |
| John McCall | cddbad0 | 2010-02-04 05:44:44 +0000 | [diff] [blame] | 2627 | // |
| John McCall | a5f46fb | 2012-08-25 02:00:03 +0000 | [diff] [blame] | 2628 | // It's OK if a function is first declared without a calling convention, |
| 2629 | // but is later declared or defined with the default calling convention. |
| 2630 | // |
| Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 2631 | // To test if either decl has an explicit calling convention, we look for |
| 2632 | // AttributedType sugar nodes on the type as written. If they are missing or |
| 2633 | // were canonicalized away, we assume the calling convention was implicit. |
| John McCall | cddbad0 | 2010-02-04 05:44:44 +0000 | [diff] [blame] | 2634 | // |
| 2635 | // Note also that we DO NOT return at this point, because we still have |
| 2636 | // other tests to run. |
| Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 2637 | QualType OldQType = Context.getCanonicalType(Old->getType()); |
| 2638 | QualType NewQType = Context.getCanonicalType(New->getType()); |
| John McCall | 4f5019e | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 2639 | const FunctionType *OldType = cast<FunctionType>(OldQType); |
| Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 2640 | const FunctionType *NewType = cast<FunctionType>(NewQType); |
| John McCall | 4f5019e | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 2641 | FunctionType::ExtInfo OldTypeInfo = OldType->getExtInfo(); |
| 2642 | FunctionType::ExtInfo NewTypeInfo = NewType->getExtInfo(); |
| 2643 | bool RequiresAdjustment = false; |
| John McCall | a5f46fb | 2012-08-25 02:00:03 +0000 | [diff] [blame] | 2644 | |
| Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 2645 | if (OldTypeInfo.getCC() != NewTypeInfo.getCC()) { |
| Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 2646 | FunctionDecl *First = Old->getFirstDecl(); |
| Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 2647 | const FunctionType *FT = |
| 2648 | First->getType().getCanonicalType()->castAs<FunctionType>(); |
| 2649 | FunctionType::ExtInfo FI = FT->getExtInfo(); |
| 2650 | bool NewCCExplicit = getCallingConvAttributedType(New->getType()); |
| 2651 | if (!NewCCExplicit) { |
| 2652 | // Inherit the CC from the previous declaration if it was specified |
| 2653 | // there but not here. |
| 2654 | NewTypeInfo = NewTypeInfo.withCallingConv(OldTypeInfo.getCC()); |
| 2655 | RequiresAdjustment = true; |
| 2656 | } else { |
| 2657 | // Calling conventions aren't compatible, so complain. |
| 2658 | bool FirstCCExplicit = getCallingConvAttributedType(First->getType()); |
| 2659 | Diag(New->getLocation(), diag::err_cconv_change) |
| 2660 | << FunctionType::getNameForCallConv(NewTypeInfo.getCC()) |
| 2661 | << !FirstCCExplicit |
| 2662 | << (!FirstCCExplicit ? "" : |
| 2663 | FunctionType::getNameForCallConv(FI.getCC())); |
| John McCall | a5f46fb | 2012-08-25 02:00:03 +0000 | [diff] [blame] | 2664 | |
| Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 2665 | // Put the note on the first decl, since it is the one that matters. |
| 2666 | Diag(First->getLocation(), diag::note_previous_declaration); |
| 2667 | return true; |
| 2668 | } |
| John McCall | cddbad0 | 2010-02-04 05:44:44 +0000 | [diff] [blame] | 2669 | } |
| 2670 | |
| John McCall | ab26cfa | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 2671 | // FIXME: diagnose the other way around? |
| John McCall | 4f5019e | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 2672 | if (OldTypeInfo.getNoReturn() && !NewTypeInfo.getNoReturn()) { |
| 2673 | NewTypeInfo = NewTypeInfo.withNoReturn(true); |
| 2674 | RequiresAdjustment = true; |
| John McCall | ab26cfa | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 2675 | } |
| 2676 | |
| Douglas Gregor | 77e274f | 2010-06-18 21:30:25 +0000 | [diff] [blame] | 2677 | // Merge regparm attribute. |
| Eli Friedman | c5b20b5 | 2011-04-09 08:18:08 +0000 | [diff] [blame] | 2678 | if (OldTypeInfo.getHasRegParm() != NewTypeInfo.getHasRegParm() || |
| 2679 | OldTypeInfo.getRegParm() != NewTypeInfo.getRegParm()) { |
| 2680 | if (NewTypeInfo.getHasRegParm()) { |
| Douglas Gregor | 77e274f | 2010-06-18 21:30:25 +0000 | [diff] [blame] | 2681 | Diag(New->getLocation(), diag::err_regparm_mismatch) |
| 2682 | << NewType->getRegParmType() |
| 2683 | << OldType->getRegParmType(); |
| Richard Smith | bdd1464 | 2014-02-04 01:14:30 +0000 | [diff] [blame] | 2684 | Diag(OldLocation, diag::note_previous_declaration); |
| Douglas Gregor | 77e274f | 2010-06-18 21:30:25 +0000 | [diff] [blame] | 2685 | return true; |
| 2686 | } |
| John McCall | 4f5019e | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 2687 | |
| 2688 | NewTypeInfo = NewTypeInfo.withRegParm(OldTypeInfo.getRegParm()); |
| 2689 | RequiresAdjustment = true; |
| 2690 | } |
| 2691 | |
| Douglas Gregor | f1404d7 | 2011-10-14 15:55:40 +0000 | [diff] [blame] | 2692 | // Merge ns_returns_retained attribute. |
| 2693 | if (OldTypeInfo.getProducesResult() != NewTypeInfo.getProducesResult()) { |
| 2694 | if (NewTypeInfo.getProducesResult()) { |
| 2695 | Diag(New->getLocation(), diag::err_returns_retained_mismatch); |
| Richard Smith | bdd1464 | 2014-02-04 01:14:30 +0000 | [diff] [blame] | 2696 | Diag(OldLocation, diag::note_previous_declaration); |
| Douglas Gregor | f1404d7 | 2011-10-14 15:55:40 +0000 | [diff] [blame] | 2697 | return true; |
| 2698 | } |
| 2699 | |
| 2700 | NewTypeInfo = NewTypeInfo.withProducesResult(true); |
| 2701 | RequiresAdjustment = true; |
| 2702 | } |
| 2703 | |
| John McCall | 4f5019e | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 2704 | if (RequiresAdjustment) { |
| Eli Friedman | e934af8 | 2013-09-06 21:09:09 +0000 | [diff] [blame] | 2705 | const FunctionType *AdjustedType = New->getType()->getAs<FunctionType>(); |
| 2706 | AdjustedType = Context.adjustFunctionType(AdjustedType, NewTypeInfo); |
| 2707 | New->setType(QualType(AdjustedType, 0)); |
| John McCall | 4f5019e | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 2708 | NewQType = Context.getCanonicalType(New->getType()); |
| Eli Friedman | e934af8 | 2013-09-06 21:09:09 +0000 | [diff] [blame] | 2709 | NewType = cast<FunctionType>(NewQType); |
| Douglas Gregor | 77e274f | 2010-06-18 21:30:25 +0000 | [diff] [blame] | 2710 | } |
| Nick Lewycky | 9c7eb1d | 2013-02-01 08:13:20 +0000 | [diff] [blame] | 2711 | |
| 2712 | // If this redeclaration makes the function inline, we may need to add it to |
| 2713 | // UndefinedButUsed. |
| 2714 | if (!Old->isInlined() && New->isInlined() && |
| 2715 | !New->hasAttr<GNUInlineAttr>() && |
| 2716 | (getLangOpts().CPlusPlus || !getLangOpts().GNUInline) && |
| 2717 | Old->isUsed(false) && |
| 2718 | !Old->isDefined() && !New->isThisDeclarationADefinition()) |
| 2719 | UndefinedButUsed.insert(std::make_pair(Old->getCanonicalDecl(), |
| 2720 | SourceLocation())); |
| 2721 | |
| 2722 | // If this redeclaration makes it newly gnu_inline, we don't want to warn |
| 2723 | // about it. |
| 2724 | if (New->hasAttr<GNUInlineAttr>() && |
| 2725 | Old->isInlined() && !Old->hasAttr<GNUInlineAttr>()) { |
| 2726 | UndefinedButUsed.erase(Old->getCanonicalDecl()); |
| 2727 | } |
| Douglas Gregor | 77e274f | 2010-06-18 21:30:25 +0000 | [diff] [blame] | 2728 | |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2729 | if (getLangOpts().CPlusPlus) { |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2730 | // (C++98 13.1p2): |
| 2731 | // Certain function declarations cannot be overloaded: |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2732 | // -- Function declarations that differ only in the return type |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2733 | // cannot be overloaded. |
| Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2734 | |
| 2735 | // 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] | 2736 | // per C++1y [dcl.type.auto]p13: |
| Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2737 | // Redeclarations or specializations of a function or function template |
| 2738 | // with a declared return type that uses a placeholder type shall also |
| 2739 | // use that placeholder, not a deduced type. |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2740 | QualType OldDeclaredReturnType = |
| 2741 | (Old->getTypeSourceInfo() |
| 2742 | ? Old->getTypeSourceInfo()->getType()->castAs<FunctionType>() |
| 2743 | : OldType)->getReturnType(); |
| 2744 | QualType NewDeclaredReturnType = |
| 2745 | (New->getTypeSourceInfo() |
| 2746 | ? New->getTypeSourceInfo()->getType()->castAs<FunctionType>() |
| 2747 | : NewType)->getReturnType(); |
| Fariborz Jahanian | f633ebd | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 2748 | QualType ResQT; |
| Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 2749 | if (!Context.hasSameType(OldDeclaredReturnType, NewDeclaredReturnType) && |
| 2750 | !((NewQType->isDependentType() || OldQType->isDependentType()) && |
| 2751 | New->isLocalExternDecl())) { |
| Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2752 | if (NewDeclaredReturnType->isObjCObjectPointerType() && |
| 2753 | OldDeclaredReturnType->isObjCObjectPointerType()) |
| Fariborz Jahanian | f633ebd | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 2754 | ResQT = Context.mergeObjCGCQualifiers(NewQType, OldQType); |
| 2755 | if (ResQT.isNull()) { |
| Argyrios Kyrtzidis | 3d32086 | 2011-02-05 05:54:49 +0000 | [diff] [blame] | 2756 | if (New->isCXXClassMember() && New->isOutOfLine()) |
| Alp Toker | d0787eb | 2014-07-02 01:47:15 +0000 | [diff] [blame] | 2757 | Diag(New->getLocation(), diag::err_member_def_does_not_match_ret_type) |
| 2758 | << New << New->getReturnTypeSourceRange(); |
| Argyrios Kyrtzidis | 3d32086 | 2011-02-05 05:54:49 +0000 | [diff] [blame] | 2759 | else |
| Alp Toker | d0787eb | 2014-07-02 01:47:15 +0000 | [diff] [blame] | 2760 | Diag(New->getLocation(), diag::err_ovl_diff_return_type) |
| 2761 | << New->getReturnTypeSourceRange(); |
| 2762 | Diag(OldLocation, PrevDiag) << Old << Old->getType() |
| 2763 | << Old->getReturnTypeSourceRange(); |
| Fariborz Jahanian | f633ebd | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 2764 | return true; |
| 2765 | } |
| 2766 | else |
| 2767 | NewQType = ResQT; |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2768 | } |
| 2769 | |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2770 | QualType OldReturnType = OldType->getReturnType(); |
| 2771 | QualType NewReturnType = cast<FunctionType>(NewQType)->getReturnType(); |
| Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2772 | if (OldReturnType != NewReturnType) { |
| 2773 | // If this function has a deduced return type and has already been |
| 2774 | // defined, copy the deduced value from the old declaration. |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2775 | AutoType *OldAT = Old->getReturnType()->getContainedAutoType(); |
| Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2776 | if (OldAT && OldAT->isDeduced()) { |
| Richard Smith | c58f38f | 2013-08-14 20:16:31 +0000 | [diff] [blame] | 2777 | New->setType( |
| 2778 | SubstAutoType(New->getType(), |
| 2779 | OldAT->isDependentType() ? Context.DependentTy |
| 2780 | : OldAT->getDeducedType())); |
| Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2781 | NewQType = Context.getCanonicalType( |
| Richard Smith | c58f38f | 2013-08-14 20:16:31 +0000 | [diff] [blame] | 2782 | SubstAutoType(NewQType, |
| 2783 | OldAT->isDependentType() ? Context.DependentTy |
| 2784 | : OldAT->getDeducedType())); |
| Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2785 | } |
| 2786 | } |
| 2787 | |
| 2788 | const CXXMethodDecl *OldMethod = dyn_cast<CXXMethodDecl>(Old); |
| 2789 | CXXMethodDecl *NewMethod = dyn_cast<CXXMethodDecl>(New); |
| Anders Carlsson | 1f78b2b | 2009-12-04 22:33:25 +0000 | [diff] [blame] | 2790 | if (OldMethod && NewMethod) { |
| John McCall | 43314ab | 2010-04-13 07:45:41 +0000 | [diff] [blame] | 2791 | // Preserve triviality. |
| 2792 | NewMethod->setTrivial(OldMethod->isTrivial()); |
| Francois Pichet | c2fac71 | 2011-05-14 19:17:07 +0000 | [diff] [blame] | 2793 | |
| Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 2794 | // MSVC allows explicit template specialization at class scope: |
| Alp Toker | 8db6e7a | 2014-01-05 06:38:57 +0000 | [diff] [blame] | 2795 | // 2 CXXMethodDecls referring to the same function will be injected. |
| 2796 | // We don't want a redeclaration error. |
| Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 2797 | bool IsClassScopeExplicitSpecialization = |
| 2798 | OldMethod->isFunctionTemplateSpecialization() && |
| 2799 | NewMethod->isFunctionTemplateSpecialization(); |
| John McCall | 43314ab | 2010-04-13 07:45:41 +0000 | [diff] [blame] | 2800 | bool isFriend = NewMethod->getFriendObjectKind(); |
| 2801 | |
| Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 2802 | if (!isFriend && NewMethod->getLexicalDeclContext()->isRecord() && |
| 2803 | !IsClassScopeExplicitSpecialization) { |
| Anders Carlsson | 1f78b2b | 2009-12-04 22:33:25 +0000 | [diff] [blame] | 2804 | // -- Member function declarations with the same name and the |
| 2805 | // same parameter types cannot be overloaded if any of them |
| 2806 | // is a static member function declaration. |
| Eli Friedman | f26b81b | 2013-06-19 22:43:55 +0000 | [diff] [blame] | 2807 | if (OldMethod->isStatic() != NewMethod->isStatic()) { |
| Anders Carlsson | 1f78b2b | 2009-12-04 22:33:25 +0000 | [diff] [blame] | 2808 | Diag(New->getLocation(), diag::err_ovl_static_nonstatic_member); |
| Richard Smith | bdd1464 | 2014-02-04 01:14:30 +0000 | [diff] [blame] | 2809 | Diag(OldLocation, PrevDiag) << Old << Old->getType(); |
| Anders Carlsson | 1f78b2b | 2009-12-04 22:33:25 +0000 | [diff] [blame] | 2810 | return true; |
| 2811 | } |
| Richard Smith | 57e7ff9 | 2012-07-13 04:12:04 +0000 | [diff] [blame] | 2812 | |
| Anders Carlsson | 1f78b2b | 2009-12-04 22:33:25 +0000 | [diff] [blame] | 2813 | // C++ [class.mem]p1: |
| 2814 | // [...] A member shall not be declared twice in the |
| 2815 | // member-specification, except that a nested class or member |
| 2816 | // class template can be declared and then later defined. |
| Richard Smith | 57e7ff9 | 2012-07-13 04:12:04 +0000 | [diff] [blame] | 2817 | if (ActiveTemplateInstantiations.empty()) { |
| 2818 | unsigned NewDiag; |
| 2819 | if (isa<CXXConstructorDecl>(OldMethod)) |
| 2820 | NewDiag = diag::err_constructor_redeclared; |
| 2821 | else if (isa<CXXDestructorDecl>(NewMethod)) |
| 2822 | NewDiag = diag::err_destructor_redeclared; |
| 2823 | else if (isa<CXXConversionDecl>(NewMethod)) |
| 2824 | NewDiag = diag::err_conv_function_redeclared; |
| 2825 | else |
| 2826 | NewDiag = diag::err_member_redeclared; |
| Anders Carlsson | 1f78b2b | 2009-12-04 22:33:25 +0000 | [diff] [blame] | 2827 | |
| Richard Smith | 57e7ff9 | 2012-07-13 04:12:04 +0000 | [diff] [blame] | 2828 | Diag(New->getLocation(), NewDiag); |
| 2829 | } else { |
| 2830 | Diag(New->getLocation(), diag::err_member_redeclared_in_instantiation) |
| 2831 | << New << New->getType(); |
| 2832 | } |
| Richard Smith | bdd1464 | 2014-02-04 01:14:30 +0000 | [diff] [blame] | 2833 | Diag(OldLocation, PrevDiag) << Old << Old->getType(); |
| Nico Weber | 71e377d | 2015-01-17 02:33:17 +0000 | [diff] [blame] | 2834 | return true; |
| John McCall | 43314ab | 2010-04-13 07:45:41 +0000 | [diff] [blame] | 2835 | |
| 2836 | // Complain if this is an explicit declaration of a special |
| 2837 | // member that was initially declared implicitly. |
| 2838 | // |
| 2839 | // As an exception, it's okay to befriend such methods in order |
| 2840 | // to permit the implicit constructor/destructor/operator calls. |
| 2841 | } else if (OldMethod->isImplicit()) { |
| 2842 | if (isFriend) { |
| 2843 | NewMethod->setImplicit(); |
| 2844 | } else { |
| Anders Carlsson | 1f78b2b | 2009-12-04 22:33:25 +0000 | [diff] [blame] | 2845 | Diag(NewMethod->getLocation(), |
| 2846 | diag::err_definition_of_implicitly_declared_member) |
| Anders Carlsson | 05bf009 | 2010-04-22 05:40:53 +0000 | [diff] [blame] | 2847 | << New << getSpecialMember(OldMethod); |
| Anders Carlsson | 1f78b2b | 2009-12-04 22:33:25 +0000 | [diff] [blame] | 2848 | return true; |
| 2849 | } |
| Richard Smith | 337a5a1 | 2012-06-08 01:30:54 +0000 | [diff] [blame] | 2850 | } else if (OldMethod->isExplicitlyDefaulted() && !isFriend) { |
| Alexis Hunt | 6d5b96c | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 2851 | Diag(NewMethod->getLocation(), |
| 2852 | diag::err_definition_of_explicitly_defaulted_member) |
| 2853 | << getSpecialMember(OldMethod); |
| 2854 | return true; |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2855 | } |
| 2856 | } |
| 2857 | |
| Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 2858 | // C++11 [dcl.attr.noreturn]p1: |
| 2859 | // The first declaration of a function shall specify the noreturn |
| 2860 | // attribute if any declaration of that function specifies the noreturn |
| 2861 | // attribute. |
| Aaron Ballman | cf3b483 | 2013-12-19 13:36:16 +0000 | [diff] [blame] | 2862 | const CXX11NoReturnAttr *NRA = New->getAttr<CXX11NoReturnAttr>(); |
| 2863 | if (NRA && !Old->hasAttr<CXX11NoReturnAttr>()) { |
| 2864 | Diag(NRA->getLocation(), diag::err_noreturn_missing_on_first_decl); |
| Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 2865 | Diag(Old->getFirstDecl()->getLocation(), |
| Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 2866 | diag::note_noreturn_missing_first_decl); |
| 2867 | } |
| 2868 | |
| Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 2869 | // C++11 [dcl.attr.depend]p2: |
| 2870 | // The first declaration of a function shall specify the |
| 2871 | // carries_dependency attribute for its declarator-id if any declaration |
| 2872 | // of the function specifies the carries_dependency attribute. |
| Aaron Ballman | cf3b483 | 2013-12-19 13:36:16 +0000 | [diff] [blame] | 2873 | const CarriesDependencyAttr *CDA = New->getAttr<CarriesDependencyAttr>(); |
| 2874 | if (CDA && !Old->hasAttr<CarriesDependencyAttr>()) { |
| 2875 | Diag(CDA->getLocation(), |
| Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 2876 | diag::err_carries_dependency_missing_on_first_decl) << 0/*Function*/; |
| Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 2877 | Diag(Old->getFirstDecl()->getLocation(), |
| Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 2878 | diag::note_carries_dependency_missing_first_decl) << 0/*Function*/; |
| 2879 | } |
| 2880 | |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2881 | // (C++98 8.3.5p3): |
| 2882 | // All declarations for a function shall agree exactly in both the |
| 2883 | // return type and the parameter-type-list. |
| John McCall | 4f5019e | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 2884 | // We also want to respect all the extended bits except noreturn. |
| 2885 | |
| 2886 | // noreturn should now match unless the old type info didn't have it. |
| 2887 | QualType OldQTypeForComparison = OldQType; |
| 2888 | if (!OldTypeInfo.getNoReturn() && NewTypeInfo.getNoReturn()) { |
| 2889 | assert(OldQType == QualType(OldType, 0)); |
| 2890 | const FunctionType *OldTypeForComparison |
| 2891 | = Context.adjustFunctionType(OldType, OldTypeInfo.withNoReturn(true)); |
| 2892 | OldQTypeForComparison = QualType(OldTypeForComparison, 0); |
| 2893 | assert(OldQTypeForComparison.isCanonical()); |
| 2894 | } |
| 2895 | |
| Rafael Espindola | f418765 | 2013-02-14 01:18:37 +0000 | [diff] [blame] | 2896 | if (haveIncompatibleLanguageLinkages(Old, New)) { |
| Alp Toker | dd551fc | 2013-10-22 22:53:01 +0000 | [diff] [blame] | 2897 | // As a special case, retain the language linkage from previous |
| 2898 | // declarations of a friend function as an extension. |
| 2899 | // |
| 2900 | // This liberal interpretation of C++ [class.friend]p3 matches GCC/MSVC |
| 2901 | // and is useful because there's otherwise no way to specify language |
| 2902 | // linkage within class scope. |
| 2903 | // |
| 2904 | // Check cautiously as the friend object kind isn't yet complete. |
| 2905 | if (New->getFriendObjectKind() != Decl::FOK_None) { |
| 2906 | Diag(New->getLocation(), diag::ext_retained_language_linkage) << New; |
| Richard Smith | bdd1464 | 2014-02-04 01:14:30 +0000 | [diff] [blame] | 2907 | Diag(OldLocation, PrevDiag); |
| Alp Toker | dd551fc | 2013-10-22 22:53:01 +0000 | [diff] [blame] | 2908 | } else { |
| 2909 | Diag(New->getLocation(), diag::err_different_language_linkage) << New; |
| Richard Smith | bdd1464 | 2014-02-04 01:14:30 +0000 | [diff] [blame] | 2910 | Diag(OldLocation, PrevDiag); |
| Alp Toker | dd551fc | 2013-10-22 22:53:01 +0000 | [diff] [blame] | 2911 | return true; |
| 2912 | } |
| Rafael Espindola | cffa95d | 2012-12-27 03:56:20 +0000 | [diff] [blame] | 2913 | } |
| 2914 | |
| John McCall | 4f5019e | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 2915 | if (OldQTypeForComparison == NewQType) |
| Richard Smith | 1c34fb7 | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 2916 | return MergeCompatibleFunctionDecls(New, Old, S, MergeTypeWithOld); |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2917 | |
| Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 2918 | if ((NewQType->isDependentType() || OldQType->isDependentType()) && |
| 2919 | New->isLocalExternDecl()) { |
| 2920 | // It's OK if we couldn't merge types for a local function declaraton |
| 2921 | // if either the old or new type is dependent. We'll merge the types |
| 2922 | // when we instantiate the function. |
| 2923 | return false; |
| 2924 | } |
| 2925 | |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2926 | // Fall through for conflicting redeclarations and redefinitions. |
| Douglas Gregor | 89f238c | 2008-04-21 02:02:58 +0000 | [diff] [blame] | 2927 | } |
| Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2928 | |
| 2929 | // C: Function types need to be compatible, not identical. This handles |
| Steve Naroff | 012484d | 2008-01-14 20:51:29 +0000 | [diff] [blame] | 2930 | // duplicate function decls like "void f(int); void f(enum X);" properly. |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2931 | if (!getLangOpts().CPlusPlus && |
| Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2932 | Context.typesAreCompatible(OldQType, NewQType)) { |
| John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2933 | const FunctionType *OldFuncType = OldQType->getAs<FunctionType>(); |
| 2934 | const FunctionType *NewFuncType = NewQType->getAs<FunctionType>(); |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2935 | const FunctionProtoType *OldProto = nullptr; |
| Richard Smith | 1c34fb7 | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 2936 | if (MergeTypeWithOld && isa<FunctionNoProtoType>(NewFuncType) && |
| Douglas Gregor | a74a297 | 2009-03-06 22:43:54 +0000 | [diff] [blame] | 2937 | (OldProto = dyn_cast<FunctionProtoType>(OldFuncType))) { |
| Douglas Gregor | bcbf863 | 2009-02-16 18:20:44 +0000 | [diff] [blame] | 2938 | // The old declaration provided a function prototype, but the |
| 2939 | // new declaration does not. Merge in the prototype. |
| Sebastian Redl | 5068f77ac | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2940 | assert(!OldProto->hasExceptionSpec() && "Exception spec in C"); |
| Aaron Ballman | 40bd0aa | 2014-03-17 15:23:01 +0000 | [diff] [blame] | 2941 | SmallVector<QualType, 16> ParamTypes(OldProto->param_types()); |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2942 | NewQType = |
| 2943 | Context.getFunctionType(NewFuncType->getReturnType(), ParamTypes, |
| 2944 | OldProto->getExtProtoInfo()); |
| Douglas Gregor | bcbf863 | 2009-02-16 18:20:44 +0000 | [diff] [blame] | 2945 | New->setType(NewQType); |
| Anders Carlsson | e0dd1d5 | 2009-05-14 21:46:00 +0000 | [diff] [blame] | 2946 | New->setHasInheritedPrototype(); |
| Douglas Gregor | bfdd607 | 2009-02-16 20:58:07 +0000 | [diff] [blame] | 2947 | |
| Alp Toker | 4284c6e | 2014-05-11 16:05:55 +0000 | [diff] [blame] | 2948 | // Synthesize parameters with the same types. |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2949 | SmallVector<ParmVarDecl*, 16> Params; |
| Aaron Ballman | 40bd0aa | 2014-03-17 15:23:01 +0000 | [diff] [blame] | 2950 | for (const auto &ParamType : OldProto->param_types()) { |
| 2951 | ParmVarDecl *Param = ParmVarDecl::Create(Context, New, SourceLocation(), |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2952 | SourceLocation(), nullptr, |
| 2953 | ParamType, /*TInfo=*/nullptr, |
| 2954 | SC_None, nullptr); |
| John McCall | 8fb0d9d | 2011-05-01 22:35:37 +0000 | [diff] [blame] | 2955 | Param->setScopeInfo(0, Params.size()); |
| Douglas Gregor | bfdd607 | 2009-02-16 20:58:07 +0000 | [diff] [blame] | 2956 | Param->setImplicit(); |
| 2957 | Params.push_back(Param); |
| 2958 | } |
| 2959 | |
| David Blaikie | 9c70e04 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 2960 | New->setParams(Params); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2961 | } |
| Douglas Gregor | bcbf863 | 2009-02-16 18:20:44 +0000 | [diff] [blame] | 2962 | |
| Richard Smith | 1c34fb7 | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 2963 | return MergeCompatibleFunctionDecls(New, Old, S, MergeTypeWithOld); |
| Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2964 | } |
| Chris Lattner | 45d561a | 2007-11-06 06:07:26 +0000 | [diff] [blame] | 2965 | |
| Douglas Gregor | a74a297 | 2009-03-06 22:43:54 +0000 | [diff] [blame] | 2966 | // GNU C permits a K&R definition to follow a prototype declaration |
| 2967 | // if the declared types of the parameters in the K&R definition |
| 2968 | // match the types in the prototype declaration, even when the |
| 2969 | // promoted types of the parameters from the K&R definition differ |
| 2970 | // from the types in the prototype. GCC then keeps the types from |
| 2971 | // the prototype. |
| Eli Friedman | fcbf7d2 | 2009-06-01 09:24:59 +0000 | [diff] [blame] | 2972 | // |
| 2973 | // If a variadic prototype is followed by a non-variadic K&R definition, |
| 2974 | // the K&R definition becomes variadic. This is sort of an edge case, but |
| 2975 | // it's legal per the standard depending on how you read C99 6.7.5.3p15 and |
| 2976 | // C99 6.9.1p8. |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2977 | if (!getLangOpts().CPlusPlus && |
| Douglas Gregor | a74a297 | 2009-03-06 22:43:54 +0000 | [diff] [blame] | 2978 | Old->hasPrototype() && !New->hasPrototype() && |
| John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2979 | New->getType()->getAs<FunctionProtoType>() && |
| Douglas Gregor | a74a297 | 2009-03-06 22:43:54 +0000 | [diff] [blame] | 2980 | Old->getNumParams() == New->getNumParams()) { |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2981 | SmallVector<QualType, 16> ArgTypes; |
| 2982 | SmallVector<GNUCompatibleParamWarning, 16> Warnings; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2983 | const FunctionProtoType *OldProto |
| John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2984 | = Old->getType()->getAs<FunctionProtoType>(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2985 | const FunctionProtoType *NewProto |
| John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2986 | = New->getType()->getAs<FunctionProtoType>(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2987 | |
| Douglas Gregor | a74a297 | 2009-03-06 22:43:54 +0000 | [diff] [blame] | 2988 | // Determine whether this is the GNU C extension. |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2989 | QualType MergedReturn = Context.mergeTypes(OldProto->getReturnType(), |
| 2990 | NewProto->getReturnType()); |
| Eli Friedman | fcbf7d2 | 2009-06-01 09:24:59 +0000 | [diff] [blame] | 2991 | bool LooseCompatible = !MergedReturn.isNull(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2992 | for (unsigned Idx = 0, End = Old->getNumParams(); |
| Eli Friedman | fcbf7d2 | 2009-06-01 09:24:59 +0000 | [diff] [blame] | 2993 | LooseCompatible && Idx != End; ++Idx) { |
| Douglas Gregor | a74a297 | 2009-03-06 22:43:54 +0000 | [diff] [blame] | 2994 | ParmVarDecl *OldParm = Old->getParamDecl(Idx); |
| 2995 | ParmVarDecl *NewParm = New->getParamDecl(Idx); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2996 | if (Context.typesAreCompatible(OldParm->getType(), |
| Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 2997 | NewProto->getParamType(Idx))) { |
| Douglas Gregor | a74a297 | 2009-03-06 22:43:54 +0000 | [diff] [blame] | 2998 | ArgTypes.push_back(NewParm->getType()); |
| 2999 | } else if (Context.typesAreCompatible(OldParm->getType(), |
| Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 3000 | NewParm->getType(), |
| 3001 | /*CompareUnqualified=*/true)) { |
| Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 3002 | GNUCompatibleParamWarning Warn = { OldParm, NewParm, |
| 3003 | NewProto->getParamType(Idx) }; |
| Douglas Gregor | a74a297 | 2009-03-06 22:43:54 +0000 | [diff] [blame] | 3004 | Warnings.push_back(Warn); |
| 3005 | ArgTypes.push_back(NewParm->getType()); |
| 3006 | } else |
| Eli Friedman | fcbf7d2 | 2009-06-01 09:24:59 +0000 | [diff] [blame] | 3007 | LooseCompatible = false; |
| Douglas Gregor | a74a297 | 2009-03-06 22:43:54 +0000 | [diff] [blame] | 3008 | } |
| 3009 | |
| Eli Friedman | fcbf7d2 | 2009-06-01 09:24:59 +0000 | [diff] [blame] | 3010 | if (LooseCompatible) { |
| Douglas Gregor | a74a297 | 2009-03-06 22:43:54 +0000 | [diff] [blame] | 3011 | for (unsigned Warn = 0; Warn < Warnings.size(); ++Warn) { |
| 3012 | Diag(Warnings[Warn].NewParm->getLocation(), |
| 3013 | diag::ext_param_promoted_not_compatible_with_prototype) |
| 3014 | << Warnings[Warn].PromotedType |
| 3015 | << Warnings[Warn].OldParm->getType(); |
| Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 3016 | if (Warnings[Warn].OldParm->getLocation().isValid()) |
| 3017 | Diag(Warnings[Warn].OldParm->getLocation(), |
| 3018 | diag::note_previous_declaration); |
| Douglas Gregor | a74a297 | 2009-03-06 22:43:54 +0000 | [diff] [blame] | 3019 | } |
| 3020 | |
| Richard Smith | 1c34fb7 | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 3021 | if (MergeTypeWithOld) |
| 3022 | New->setType(Context.getFunctionType(MergedReturn, ArgTypes, |
| 3023 | OldProto->getExtProtoInfo())); |
| 3024 | return MergeCompatibleFunctionDecls(New, Old, S, MergeTypeWithOld); |
| Douglas Gregor | a74a297 | 2009-03-06 22:43:54 +0000 | [diff] [blame] | 3025 | } |
| 3026 | |
| 3027 | // Fall through to diagnose conflicting types. |
| 3028 | } |
| 3029 | |
| John McCall | ad327cd | 2013-04-14 08:50:55 +0000 | [diff] [blame] | 3030 | // A function that has already been declared has been redeclared or |
| 3031 | // defined with a different type; show an appropriate diagnostic. |
| 3032 | |
| 3033 | // If the previous declaration was an implicitly-generated builtin |
| 3034 | // declaration, then at the very least we should use a specialized note. |
| 3035 | unsigned BuiltinID; |
| 3036 | if (Old->isImplicit() && (BuiltinID = Old->getBuiltinID())) { |
| 3037 | // If it's actually a library-defined builtin function like 'malloc' |
| 3038 | // or 'printf', just warn about the incompatible redeclaration. |
| Douglas Gregor | 75a45ba | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 3039 | if (Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)) { |
| Douglas Gregor | 75a45ba | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 3040 | Diag(New->getLocation(), diag::warn_redecl_library_builtin) << New; |
| Richard Smith | bdd1464 | 2014-02-04 01:14:30 +0000 | [diff] [blame] | 3041 | Diag(OldLocation, diag::note_previous_builtin_declaration) |
| Douglas Gregor | 75a45ba | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 3042 | << Old << Old->getType(); |
| John McCall | ad327cd | 2013-04-14 08:50:55 +0000 | [diff] [blame] | 3043 | |
| 3044 | // If this is a global redeclaration, just forget hereafter |
| 3045 | // about the "builtin-ness" of the function. |
| 3046 | // |
| 3047 | // Doing this for local extern declarations is problematic. If |
| 3048 | // the builtin declaration remains visible, a second invalid |
| 3049 | // local declaration will produce a hard error; if it doesn't |
| 3050 | // remain visible, a single bogus local redeclaration (which is |
| 3051 | // actually only a warning) could break all the downstream code. |
| Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 3052 | if (!New->getLexicalDeclContext()->isFunctionOrMethod()) |
| John McCall | ad327cd | 2013-04-14 08:50:55 +0000 | [diff] [blame] | 3053 | New->getIdentifier()->setBuiltinID(Builtin::NotBuiltin); |
| 3054 | |
| Douglas Gregor | 893c2c9 | 2009-03-23 17:47:24 +0000 | [diff] [blame] | 3055 | return false; |
| Douglas Gregor | 75a45ba | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 3056 | } |
| Steve Naroff | 17832a4 | 2008-01-16 15:01:34 +0000 | [diff] [blame] | 3057 | |
| Douglas Gregor | 75a45ba | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 3058 | PrevDiag = diag::note_previous_builtin_declaration; |
| 3059 | } |
| 3060 | |
| Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3061 | Diag(New->getLocation(), diag::err_conflicting_types) << New->getDeclName(); |
| Richard Smith | bdd1464 | 2014-02-04 01:14:30 +0000 | [diff] [blame] | 3062 | Diag(OldLocation, PrevDiag) << Old << Old->getType(); |
| Douglas Gregor | 75a45ba | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 3063 | return true; |
| Chris Lattner | 01564d9 | 2007-01-27 19:27:06 +0000 | [diff] [blame] | 3064 | } |
| 3065 | |
| Douglas Gregor | e62c0a4 | 2009-02-24 01:23:02 +0000 | [diff] [blame] | 3066 | /// \brief Completes the merge of two function declarations that are |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3067 | /// known to be compatible. |
| Douglas Gregor | e62c0a4 | 2009-02-24 01:23:02 +0000 | [diff] [blame] | 3068 | /// |
| 3069 | /// This routine handles the merging of attributes and other |
| Alp Toker | 5f6b8ac | 2013-10-22 09:00:49 +0000 | [diff] [blame] | 3070 | /// properties of function declarations from the old declaration to |
| Douglas Gregor | e62c0a4 | 2009-02-24 01:23:02 +0000 | [diff] [blame] | 3071 | /// the new declaration, once we know that New is in fact a |
| 3072 | /// redeclaration of Old. |
| 3073 | /// |
| 3074 | /// \returns false |
| James Molloy | e943003 | 2012-03-13 08:55:35 +0000 | [diff] [blame] | 3075 | bool Sema::MergeCompatibleFunctionDecls(FunctionDecl *New, FunctionDecl *Old, |
| Richard Smith | 1c34fb7 | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 3076 | Scope *S, bool MergeTypeWithOld) { |
| Douglas Gregor | e62c0a4 | 2009-02-24 01:23:02 +0000 | [diff] [blame] | 3077 | // Merge the attributes |
| Douglas Gregor | 32c1757 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 3078 | mergeDeclAttributes(New, Old); |
| Douglas Gregor | e62c0a4 | 2009-02-24 01:23:02 +0000 | [diff] [blame] | 3079 | |
| Douglas Gregor | e62c0a4 | 2009-02-24 01:23:02 +0000 | [diff] [blame] | 3080 | // Merge "pure" flag. |
| 3081 | if (Old->isPure()) |
| 3082 | New->setPure(); |
| 3083 | |
| Rafael Espindola | befe130 | 2012-11-25 14:07:59 +0000 | [diff] [blame] | 3084 | // Merge "used" flag. |
| Rafael Espindola | e4865d2 | 2013-10-23 16:46:34 +0000 | [diff] [blame] | 3085 | if (Old->getMostRecentDecl()->isUsed(false)) |
| 3086 | New->setIsUsed(); |
| Rafael Espindola | befe130 | 2012-11-25 14:07:59 +0000 | [diff] [blame] | 3087 | |
| John McCall | f79e87d | 2011-03-02 04:00:57 +0000 | [diff] [blame] | 3088 | // Merge attributes from the parameters. These can mismatch with K&R |
| 3089 | // declarations. |
| 3090 | if (New->getNumParams() == Old->getNumParams()) |
| 3091 | for (unsigned i = 0, e = New->getNumParams(); i != e; ++i) |
| 3092 | mergeParamDeclAttributes(New->getParamDecl(i), Old->getParamDecl(i), |
| Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 3093 | *this); |
| John McCall | f79e87d | 2011-03-02 04:00:57 +0000 | [diff] [blame] | 3094 | |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3095 | if (getLangOpts().CPlusPlus) |
| James Molloy | e943003 | 2012-03-13 08:55:35 +0000 | [diff] [blame] | 3096 | return MergeCXXFunctionDecl(New, Old, S); |
| Douglas Gregor | e62c0a4 | 2009-02-24 01:23:02 +0000 | [diff] [blame] | 3097 | |
| Rafael Espindola | 8778c28 | 2012-11-29 16:09:03 +0000 | [diff] [blame] | 3098 | // 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] | 3099 | // and argument types. Per C11 6.2.7/4, only update the type if the old decl |
| 3100 | // was visible. |
| Rafael Espindola | 8778c28 | 2012-11-29 16:09:03 +0000 | [diff] [blame] | 3101 | QualType Merged = Context.mergeTypes(Old->getType(), New->getType()); |
| Richard Smith | 1c34fb7 | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 3102 | if (!Merged.isNull() && MergeTypeWithOld) |
| Rafael Espindola | 8778c28 | 2012-11-29 16:09:03 +0000 | [diff] [blame] | 3103 | New->setType(Merged); |
| 3104 | |
| Douglas Gregor | e62c0a4 | 2009-02-24 01:23:02 +0000 | [diff] [blame] | 3105 | return false; |
| 3106 | } |
| 3107 | |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3108 | |
| John McCall | f79e87d | 2011-03-02 04:00:57 +0000 | [diff] [blame] | 3109 | void Sema::mergeObjCMethodDecls(ObjCMethodDecl *newMethod, |
| Douglas Gregor | 32c1757 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 3110 | ObjCMethodDecl *oldMethod) { |
| John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 3111 | |
| Fariborz Jahanian | 3da28f8 | 2012-06-05 21:14:46 +0000 | [diff] [blame] | 3112 | // Merge the attributes, including deprecated/unavailable |
| Ted Kremenek | b544572 | 2013-04-06 00:34:27 +0000 | [diff] [blame] | 3113 | AvailabilityMergeKind MergeKind = |
| 3114 | isa<ObjCImplDecl>(newMethod->getDeclContext()) ? AMK_Redeclaration |
| 3115 | : AMK_Override; |
| 3116 | mergeDeclAttributes(newMethod, oldMethod, MergeKind); |
| John McCall | f79e87d | 2011-03-02 04:00:57 +0000 | [diff] [blame] | 3117 | |
| 3118 | // Merge attributes from the parameters. |
| Douglas Gregor | 0bf70f4 | 2012-05-17 23:13:29 +0000 | [diff] [blame] | 3119 | ObjCMethodDecl::param_const_iterator oi = oldMethod->param_begin(), |
| 3120 | oe = oldMethod->param_end(); |
| Argyrios Kyrtzidis | b8c3aaf | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 3121 | for (ObjCMethodDecl::param_iterator |
| John McCall | f79e87d | 2011-03-02 04:00:57 +0000 | [diff] [blame] | 3122 | ni = newMethod->param_begin(), ne = newMethod->param_end(); |
| Douglas Gregor | 0bf70f4 | 2012-05-17 23:13:29 +0000 | [diff] [blame] | 3123 | ni != ne && oi != oe; ++ni, ++oi) |
| Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 3124 | mergeParamDeclAttributes(*ni, *oi, *this); |
| John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 3125 | |
| Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 3126 | CheckObjCMethodOverride(newMethod, oldMethod); |
| John McCall | f79e87d | 2011-03-02 04:00:57 +0000 | [diff] [blame] | 3127 | } |
| 3128 | |
| Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 3129 | /// MergeVarDeclTypes - We parsed a variable 'New' which has the same name and |
| 3130 | /// 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] | 3131 | /// emitting diagnostics as appropriate. |
| 3132 | /// |
| 3133 | /// Declarations using the auto type specifier (C++ [decl.spec.auto]) call back |
| Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 3134 | /// to here in AddInitializerToDecl. We can't check them before the initializer |
| 3135 | /// is attached. |
| Richard Smith | 1c34fb7 | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 3136 | void Sema::MergeVarDeclTypes(VarDecl *New, VarDecl *Old, |
| 3137 | bool MergeTypeWithOld) { |
| Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3138 | if (New->isInvalidDecl() || Old->isInvalidDecl()) |
| 3139 | return; |
| 3140 | |
| 3141 | QualType MergedT; |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3142 | if (getLangOpts().CPlusPlus) { |
| Richard Smith | 27d807c | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 3143 | if (New->getType()->isUndeducedType()) { |
| Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3144 | // We don't know what the new type is until the initializer is attached. |
| 3145 | return; |
| Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 3146 | } else if (Context.hasSameType(New->getType(), Old->getType())) { |
| 3147 | // These could still be something that needs exception specs checked. |
| 3148 | return MergeVarDeclExceptionSpecs(New, Old); |
| 3149 | } |
| Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3150 | // C++ [basic.link]p10: |
| 3151 | // [...] the types specified by all declarations referring to a given |
| 3152 | // object or function shall be identical, except that declarations for an |
| 3153 | // array object can specify array types that differ by the presence or |
| 3154 | // absence of a major array bound (8.3.4). |
| 3155 | else if (Old->getType()->isIncompleteArrayType() && |
| 3156 | New->getType()->isArrayType()) { |
| Eli Friedman | 07bab73 | 2012-12-13 01:43:21 +0000 | [diff] [blame] | 3157 | const ArrayType *OldArray = Context.getAsArrayType(Old->getType()); |
| 3158 | const ArrayType *NewArray = Context.getAsArrayType(New->getType()); |
| 3159 | if (Context.hasSameType(OldArray->getElementType(), |
| 3160 | NewArray->getElementType())) |
| Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3161 | MergedT = New->getType(); |
| 3162 | } else if (Old->getType()->isArrayType() && |
| Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 3163 | New->getType()->isIncompleteArrayType()) { |
| Eli Friedman | 07bab73 | 2012-12-13 01:43:21 +0000 | [diff] [blame] | 3164 | const ArrayType *OldArray = Context.getAsArrayType(Old->getType()); |
| 3165 | const ArrayType *NewArray = Context.getAsArrayType(New->getType()); |
| 3166 | if (Context.hasSameType(OldArray->getElementType(), |
| 3167 | NewArray->getElementType())) |
| Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3168 | MergedT = Old->getType(); |
| Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 3169 | } else if (New->getType()->isObjCObjectPointerType() && |
| 3170 | Old->getType()->isObjCObjectPointerType()) { |
| 3171 | MergedT = Context.mergeObjCGCQualifiers(New->getType(), |
| 3172 | Old->getType()); |
| Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3173 | } |
| 3174 | } else { |
| Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 3175 | // C 6.2.7p2: |
| 3176 | // All declarations that refer to the same object or function shall have |
| 3177 | // compatible type. |
| Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3178 | MergedT = Context.mergeTypes(New->getType(), Old->getType()); |
| 3179 | } |
| 3180 | if (MergedT.isNull()) { |
| Richard Smith | 1c34fb7 | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 3181 | // It's OK if we couldn't merge types if either type is dependent, for a |
| 3182 | // block-scope variable. In other cases (static data members of class |
| 3183 | // templates, variable templates, ...), we require the types to be |
| 3184 | // equivalent. |
| 3185 | // FIXME: The C++ standard doesn't say anything about this. |
| 3186 | if ((New->getType()->isDependentType() || |
| 3187 | Old->getType()->isDependentType()) && New->isLocalVarDecl()) { |
| 3188 | // If the old type was dependent, we can't merge with it, so the new type |
| 3189 | // becomes dependent for now. We'll reproduce the original type when we |
| 3190 | // instantiate the TypeSourceInfo for the variable. |
| 3191 | if (!New->getType()->isDependentType() && MergeTypeWithOld) |
| 3192 | New->setType(Context.DependentTy); |
| 3193 | return; |
| 3194 | } |
| 3195 | |
| 3196 | // FIXME: Even if this merging succeeds, some other non-visible declaration |
| 3197 | // of this variable might have an incompatible type. For instance: |
| 3198 | // |
| 3199 | // extern int arr[]; |
| 3200 | // void f() { extern int arr[2]; } |
| 3201 | // void g() { extern int arr[3]; } |
| 3202 | // |
| 3203 | // Neither C nor C++ requires a diagnostic for this, but we should still try |
| 3204 | // to diagnose it. |
| Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3205 | Diag(New->getLocation(), diag::err_redefinition_different_type) |
| David Blaikie | 6590220 | 2012-09-20 18:38:57 +0000 | [diff] [blame] | 3206 | << New->getDeclName() << New->getType() << Old->getType(); |
| Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3207 | Diag(Old->getLocation(), diag::note_previous_definition); |
| 3208 | return New->setInvalidDecl(); |
| 3209 | } |
| John McCall | b65e8fe | 2013-04-01 18:34:28 +0000 | [diff] [blame] | 3210 | |
| 3211 | // 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] | 3212 | // declaration was an extern declaration in a different scope. |
| Richard Smith | 1c34fb7 | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 3213 | if (MergeTypeWithOld) |
| John McCall | b65e8fe | 2013-04-01 18:34:28 +0000 | [diff] [blame] | 3214 | New->setType(MergedT); |
| Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3215 | } |
| 3216 | |
| Richard Smith | 3c78578 | 2013-09-03 21:00:58 +0000 | [diff] [blame] | 3217 | static bool mergeTypeWithPrevious(Sema &S, VarDecl *NewVD, VarDecl *OldVD, |
| 3218 | LookupResult &Previous) { |
| 3219 | // C11 6.2.7p4: |
| 3220 | // For an identifier with internal or external linkage declared |
| 3221 | // in a scope in which a prior declaration of that identifier is |
| 3222 | // visible, if the prior declaration specifies internal or |
| 3223 | // external linkage, the type of the identifier at the later |
| 3224 | // declaration becomes the composite type. |
| 3225 | // |
| 3226 | // If the variable isn't visible, we do not merge with its type. |
| 3227 | if (Previous.isShadowed()) |
| 3228 | return false; |
| 3229 | |
| 3230 | if (S.getLangOpts().CPlusPlus) { |
| 3231 | // C++11 [dcl.array]p3: |
| 3232 | // If there is a preceding declaration of the entity in the same |
| 3233 | // scope in which the bound was specified, an omitted array bound |
| 3234 | // is taken to be the same as in that earlier declaration. |
| 3235 | return NewVD->isPreviousDeclInSameBlockScope() || |
| 3236 | (!OldVD->getLexicalDeclContext()->isFunctionOrMethod() && |
| 3237 | !NewVD->getLexicalDeclContext()->isFunctionOrMethod()); |
| 3238 | } else { |
| 3239 | // If the old declaration was function-local, don't merge with its |
| 3240 | // type unless we're in the same function. |
| 3241 | return !OldVD->getLexicalDeclContext()->isFunctionOrMethod() || |
| 3242 | OldVD->getLexicalDeclContext() == NewVD->getLexicalDeclContext(); |
| 3243 | } |
| 3244 | } |
| 3245 | |
| Chris Lattner | 01564d9 | 2007-01-27 19:27:06 +0000 | [diff] [blame] | 3246 | /// MergeVarDecl - We just parsed a variable 'New' which has the same name |
| 3247 | /// and scope as a previous declaration 'Old'. Figure out how to resolve this |
| 3248 | /// situation, merging decls or emitting diagnostics as appropriate. |
| 3249 | /// |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3250 | /// Tentative definition rules (C99 6.9.2p2) are checked by |
| 3251 | /// FinalizeDeclaratorGroup. Unfortunately, we can't analyze tentative |
| Steve Naroff | 5bb8f22 | 2008-08-08 17:50:35 +0000 | [diff] [blame] | 3252 | /// definitions here, since the initializer hasn't been attached. |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3253 | /// |
| Richard Smith | 3c78578 | 2013-09-03 21:00:58 +0000 | [diff] [blame] | 3254 | void Sema::MergeVarDecl(VarDecl *New, LookupResult &Previous) { |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 3255 | // If the new decl is already invalid, don't do any other checking. |
| 3256 | if (New->isInvalidDecl()) |
| 3257 | return; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3258 | |
| Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 3259 | VarTemplateDecl *NewTemplate = New->getDescribedVarTemplate(); |
| 3260 | |
| Larisse Voufo | d8dd97c | 2013-08-14 03:09:19 +0000 | [diff] [blame] | 3261 | // Verify the old decl was also a variable or variable template. |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3262 | VarDecl *Old = nullptr; |
| 3263 | VarTemplateDecl *OldTemplate = nullptr; |
| Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 3264 | if (Previous.isSingleResult()) { |
| 3265 | if (NewTemplate) { |
| 3266 | OldTemplate = dyn_cast<VarTemplateDecl>(Previous.getFoundDecl()); |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3267 | Old = OldTemplate ? OldTemplate->getTemplatedDecl() : nullptr; |
| Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 3268 | } else |
| 3269 | Old = dyn_cast<VarDecl>(Previous.getFoundDecl()); |
| Larisse Voufo | d8dd97c | 2013-08-14 03:09:19 +0000 | [diff] [blame] | 3270 | } |
| 3271 | if (!Old) { |
| Chris Lattner | 651d42d | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 3272 | Diag(New->getLocation(), diag::err_redefinition_different_kind) |
| Chris Lattner | e3d20d9 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 3273 | << New->getDeclName(); |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 3274 | Diag(Previous.getRepresentativeDecl()->getLocation(), |
| 3275 | diag::note_previous_definition); |
| Chris Lattner | f6d1c9c | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 3276 | return New->setInvalidDecl(); |
| Chris Lattner | c511efb | 2007-01-27 19:32:14 +0000 | [diff] [blame] | 3277 | } |
| Chris Lattner | 8496639 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 3278 | |
| Rafael Espindola | 5bddd6a | 2013-04-15 12:49:13 +0000 | [diff] [blame] | 3279 | if (!shouldLinkPossiblyHiddenDecl(Old, New)) |
| 3280 | return; |
| 3281 | |
| Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 3282 | // Ensure the template parameters are compatible. |
| 3283 | if (NewTemplate && |
| 3284 | !TemplateParameterListsAreEqual(NewTemplate->getTemplateParameters(), |
| 3285 | OldTemplate->getTemplateParameters(), |
| 3286 | /*Complain=*/true, TPL_TemplateMatch)) |
| 3287 | return; |
| 3288 | |
| Douglas Gregor | 2c7d929 | 2010-08-30 14:32:14 +0000 | [diff] [blame] | 3289 | // C++ [class.mem]p1: |
| 3290 | // A member shall not be declared twice in the member-specification [...] |
| 3291 | // |
| 3292 | // Here, we need only consider static data members. |
| 3293 | if (Old->isStaticDataMember() && !New->isOutOfLine()) { |
| 3294 | Diag(New->getLocation(), diag::err_duplicate_member) |
| 3295 | << New->getIdentifier(); |
| 3296 | Diag(Old->getLocation(), diag::note_previous_declaration); |
| 3297 | New->setInvalidDecl(); |
| 3298 | } |
| 3299 | |
| Douglas Gregor | 32c1757 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 3300 | mergeDeclAttributes(New, Old); |
| David Blaikie | 30d1544 | 2011-10-19 22:56:21 +0000 | [diff] [blame] | 3301 | // Warn if an already-declared variable is made a weak_import in a subsequent |
| 3302 | // declaration |
| Aaron Ballman | 9ead124 | 2013-12-19 02:39:40 +0000 | [diff] [blame] | 3303 | if (New->hasAttr<WeakImportAttr>() && |
| Fariborz Jahanian | ab578bf | 2011-06-20 17:50:03 +0000 | [diff] [blame] | 3304 | Old->getStorageClass() == SC_None && |
| Aaron Ballman | 9ead124 | 2013-12-19 02:39:40 +0000 | [diff] [blame] | 3305 | !Old->hasAttr<WeakImportAttr>()) { |
| Fariborz Jahanian | 33e0226 | 2011-06-22 22:08:50 +0000 | [diff] [blame] | 3306 | Diag(New->getLocation(), diag::warn_weak_import) << New->getDeclName(); |
| 3307 | Diag(Old->getLocation(), diag::note_previous_definition); |
| 3308 | // Remove weak_import attribute on new declaration. |
| Fariborz Jahanian | 0dfc950 | 2011-06-23 17:50:10 +0000 | [diff] [blame] | 3309 | New->dropAttr<WeakImportAttr>(); |
| Fariborz Jahanian | 33e0226 | 2011-06-22 22:08:50 +0000 | [diff] [blame] | 3310 | } |
| Chris Lattner | 8496639 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 3311 | |
| Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3312 | // Merge the types. |
| David Majnemer | ce12ed2 | 2015-01-14 02:27:38 +0000 | [diff] [blame] | 3313 | VarDecl *MostRecent = Old->getMostRecentDecl(); |
| 3314 | if (MostRecent != Old) { |
| 3315 | MergeVarDeclTypes(New, MostRecent, |
| 3316 | mergeTypeWithPrevious(*this, New, MostRecent, Previous)); |
| 3317 | if (New->isInvalidDecl()) |
| 3318 | return; |
| 3319 | } |
| Richard Smith | 3c78578 | 2013-09-03 21:00:58 +0000 | [diff] [blame] | 3320 | |
| David Majnemer | ce12ed2 | 2015-01-14 02:27:38 +0000 | [diff] [blame] | 3321 | MergeVarDeclTypes(New, Old, mergeTypeWithPrevious(*this, New, Old, Previous)); |
| Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3322 | if (New->isInvalidDecl()) |
| 3323 | return; |
| Douglas Gregor | 04e9a03 | 2009-03-11 23:52:16 +0000 | [diff] [blame] | 3324 | |
| David Majnemer | 5b63fa0 | 2014-06-18 23:26:25 +0000 | [diff] [blame] | 3325 | diag::kind PrevDiag; |
| 3326 | SourceLocation OldLocation; |
| 3327 | std::tie(PrevDiag, OldLocation) = |
| 3328 | getNoteDiagForInvalidRedeclaration(Old, New); |
| 3329 | |
| Rafael Espindola | 8ac2f59 | 2013-04-04 21:21:25 +0000 | [diff] [blame] | 3330 | // [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] | 3331 | if (New->getStorageClass() == SC_Static && |
| Rafael Espindola | 8ac2f59 | 2013-04-04 21:21:25 +0000 | [diff] [blame] | 3332 | !New->isStaticDataMember() && |
| Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 3333 | Old->hasExternalFormalLinkage()) { |
| David Majnemer | 5b63fa0 | 2014-06-18 23:26:25 +0000 | [diff] [blame] | 3334 | if (getLangOpts().MicrosoftExt) { |
| 3335 | Diag(New->getLocation(), diag::ext_static_non_static) |
| 3336 | << New->getDeclName(); |
| 3337 | Diag(OldLocation, PrevDiag); |
| 3338 | } else { |
| 3339 | Diag(New->getLocation(), diag::err_static_non_static) |
| 3340 | << New->getDeclName(); |
| 3341 | Diag(OldLocation, PrevDiag); |
| 3342 | return New->setInvalidDecl(); |
| 3343 | } |
| Steve Naroff | 1e78736 | 2008-01-30 00:44:01 +0000 | [diff] [blame] | 3344 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3345 | // C99 6.2.2p4: |
| Douglas Gregor | 3731162 | 2009-03-19 22:01:50 +0000 | [diff] [blame] | 3346 | // For an identifier declared with the storage-class specifier |
| 3347 | // extern in a scope in which a prior declaration of that |
| 3348 | // identifier is visible,23) if the prior declaration specifies |
| 3349 | // internal or external linkage, the linkage of the identifier at |
| 3350 | // the later declaration is the same as the linkage specified at |
| 3351 | // the prior declaration. If no prior declaration is visible, or |
| 3352 | // if the prior declaration specifies no linkage, then the |
| 3353 | // identifier has external linkage. |
| Douglas Gregor | d4eca01 | 2009-03-23 16:17:01 +0000 | [diff] [blame] | 3354 | if (New->hasExternalStorage() && Old->hasLinkage()) |
| Douglas Gregor | 3731162 | 2009-03-19 22:01:50 +0000 | [diff] [blame] | 3355 | /* Okay */; |
| Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 3356 | else if (New->getCanonicalDecl()->getStorageClass() != SC_Static && |
| Rafael Espindola | 8ac2f59 | 2013-04-04 21:21:25 +0000 | [diff] [blame] | 3357 | !New->isStaticDataMember() && |
| Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 3358 | Old->getCanonicalDecl()->getStorageClass() == SC_Static) { |
| Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3359 | Diag(New->getLocation(), diag::err_non_static_static) << New->getDeclName(); |
| David Majnemer | 5b63fa0 | 2014-06-18 23:26:25 +0000 | [diff] [blame] | 3360 | Diag(OldLocation, PrevDiag); |
| Chris Lattner | f6d1c9c | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 3361 | return New->setInvalidDecl(); |
| Steve Naroff | 1e78736 | 2008-01-30 00:44:01 +0000 | [diff] [blame] | 3362 | } |
| Daniel Dunbar | 0ca1660 | 2009-04-14 02:25:56 +0000 | [diff] [blame] | 3363 | |
| Argyrios Kyrtzidis | 819f610 | 2011-01-31 07:04:46 +0000 | [diff] [blame] | 3364 | // Check if extern is followed by non-extern and vice-versa. |
| David Majnemer | cdf8a71 | 2015-01-13 10:14:57 +0000 | [diff] [blame] | 3365 | if (New->hasExternalStorage() && |
| David Majnemer | a3b04ce | 2015-01-14 00:31:13 +0000 | [diff] [blame] | 3366 | !Old->hasLinkage() && Old->isLocalVarDeclOrParm()) { |
| Argyrios Kyrtzidis | 819f610 | 2011-01-31 07:04:46 +0000 | [diff] [blame] | 3367 | Diag(New->getLocation(), diag::err_extern_non_extern) << New->getDeclName(); |
| David Majnemer | 5b63fa0 | 2014-06-18 23:26:25 +0000 | [diff] [blame] | 3368 | Diag(OldLocation, PrevDiag); |
| Argyrios Kyrtzidis | 819f610 | 2011-01-31 07:04:46 +0000 | [diff] [blame] | 3369 | return New->setInvalidDecl(); |
| 3370 | } |
| David Majnemer | a3b04ce | 2015-01-14 00:31:13 +0000 | [diff] [blame] | 3371 | if (Old->hasLinkage() && New->isLocalVarDeclOrParm() && |
| David Majnemer | cdf8a71 | 2015-01-13 10:14:57 +0000 | [diff] [blame] | 3372 | !New->hasExternalStorage()) { |
| Argyrios Kyrtzidis | 819f610 | 2011-01-31 07:04:46 +0000 | [diff] [blame] | 3373 | Diag(New->getLocation(), diag::err_non_extern_extern) << New->getDeclName(); |
| David Majnemer | 5b63fa0 | 2014-06-18 23:26:25 +0000 | [diff] [blame] | 3374 | Diag(OldLocation, PrevDiag); |
| Argyrios Kyrtzidis | 819f610 | 2011-01-31 07:04:46 +0000 | [diff] [blame] | 3375 | return New->setInvalidDecl(); |
| 3376 | } |
| 3377 | |
| Steve Naroff | a562937 | 2008-09-17 14:05:40 +0000 | [diff] [blame] | 3378 | // Variables with external linkage are analyzed in FinalizeDeclaratorGroup. |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3379 | |
| Daniel Dunbar | 0ca1660 | 2009-04-14 02:25:56 +0000 | [diff] [blame] | 3380 | // FIXME: The test for external storage here seems wrong? We still |
| 3381 | // need to check for mismatches. |
| 3382 | if (!New->hasExternalStorage() && !New->isFileVarDecl() && |
| Douglas Gregor | 04e9a03 | 2009-03-11 23:52:16 +0000 | [diff] [blame] | 3383 | // Don't complain about out-of-line definitions of static members. |
| 3384 | !(Old->getLexicalDeclContext()->isRecord() && |
| 3385 | !New->getLexicalDeclContext()->isRecord())) { |
| Chris Lattner | e3d20d9 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 3386 | Diag(New->getLocation(), diag::err_redefinition) << New->getDeclName(); |
| David Majnemer | 5b63fa0 | 2014-06-18 23:26:25 +0000 | [diff] [blame] | 3387 | Diag(OldLocation, PrevDiag); |
| Chris Lattner | f6d1c9c | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 3388 | return New->setInvalidDecl(); |
| Steve Naroff | 6fbf0dc | 2007-03-16 00:33:25 +0000 | [diff] [blame] | 3389 | } |
| Douglas Gregor | 0760fa1 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 3390 | |
| Richard Smith | fd3834f | 2013-04-13 02:43:54 +0000 | [diff] [blame] | 3391 | if (New->getTLSKind() != Old->getTLSKind()) { |
| 3392 | if (!Old->getTLSKind()) { |
| 3393 | Diag(New->getLocation(), diag::err_thread_non_thread) << New->getDeclName(); |
| David Majnemer | 5b63fa0 | 2014-06-18 23:26:25 +0000 | [diff] [blame] | 3394 | Diag(OldLocation, PrevDiag); |
| Richard Smith | fd3834f | 2013-04-13 02:43:54 +0000 | [diff] [blame] | 3395 | } else if (!New->getTLSKind()) { |
| 3396 | Diag(New->getLocation(), diag::err_non_thread_thread) << New->getDeclName(); |
| David Majnemer | 5b63fa0 | 2014-06-18 23:26:25 +0000 | [diff] [blame] | 3397 | Diag(OldLocation, PrevDiag); |
| Richard Smith | fd3834f | 2013-04-13 02:43:54 +0000 | [diff] [blame] | 3398 | } else { |
| 3399 | // Do not allow redeclaration to change the variable between requiring |
| 3400 | // static and dynamic initialization. |
| 3401 | // FIXME: GCC allows this, but uses the TLS keyword on the first |
| 3402 | // declaration to determine the kind. Do we need to be compatible here? |
| 3403 | Diag(New->getLocation(), diag::err_thread_thread_different_kind) |
| 3404 | << New->getDeclName() << (New->getTLSKind() == VarDecl::TLS_Dynamic); |
| David Majnemer | 5b63fa0 | 2014-06-18 23:26:25 +0000 | [diff] [blame] | 3405 | Diag(OldLocation, PrevDiag); |
| Richard Smith | fd3834f | 2013-04-13 02:43:54 +0000 | [diff] [blame] | 3406 | } |
| Eli Friedman | d5c0eed | 2009-04-19 20:27:55 +0000 | [diff] [blame] | 3407 | } |
| 3408 | |
| Sebastian Redl | f184291 | 2010-02-02 18:35:11 +0000 | [diff] [blame] | 3409 | // C++ doesn't have tentative definitions, so go right ahead and check here. |
| 3410 | const VarDecl *Def; |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3411 | if (getLangOpts().CPlusPlus && |
| Sebastian Redl | d85be0c | 2010-02-03 02:08:48 +0000 | [diff] [blame] | 3412 | New->isThisDeclarationADefinition() == VarDecl::Definition && |
| Sebastian Redl | f184291 | 2010-02-02 18:35:11 +0000 | [diff] [blame] | 3413 | (Def = Old->getDefinition())) { |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3414 | Diag(New->getLocation(), diag::err_redefinition) << New; |
| Sebastian Redl | f184291 | 2010-02-02 18:35:11 +0000 | [diff] [blame] | 3415 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 3416 | New->setInvalidDecl(); |
| 3417 | return; |
| 3418 | } |
| Rafael Espindola | cffa95d | 2012-12-27 03:56:20 +0000 | [diff] [blame] | 3419 | |
| Rafael Espindola | f418765 | 2013-02-14 01:18:37 +0000 | [diff] [blame] | 3420 | if (haveIncompatibleLanguageLinkages(Old, New)) { |
| Rafael Espindola | cffa95d | 2012-12-27 03:56:20 +0000 | [diff] [blame] | 3421 | Diag(New->getLocation(), diag::err_different_language_linkage) << New; |
| David Majnemer | 5b63fa0 | 2014-06-18 23:26:25 +0000 | [diff] [blame] | 3422 | Diag(OldLocation, PrevDiag); |
| Rafael Espindola | cffa95d | 2012-12-27 03:56:20 +0000 | [diff] [blame] | 3423 | New->setInvalidDecl(); |
| 3424 | return; |
| 3425 | } |
| 3426 | |
| Rafael Espindola | befe130 | 2012-11-25 14:07:59 +0000 | [diff] [blame] | 3427 | // Merge "used" flag. |
| Rafael Espindola | e4865d2 | 2013-10-23 16:46:34 +0000 | [diff] [blame] | 3428 | if (Old->getMostRecentDecl()->isUsed(false)) |
| 3429 | New->setIsUsed(); |
| Rafael Espindola | befe130 | 2012-11-25 14:07:59 +0000 | [diff] [blame] | 3430 | |
| Douglas Gregor | 0760fa1 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 3431 | // Keep a chain of previous declarations. |
| Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 3432 | New->setPreviousDecl(Old); |
| Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 3433 | if (NewTemplate) |
| 3434 | NewTemplate->setPreviousDecl(OldTemplate); |
| John McCall | 401982f | 2010-01-20 21:53:11 +0000 | [diff] [blame] | 3435 | |
| 3436 | // Inherit access appropriately. |
| 3437 | New->setAccess(Old->getAccess()); |
| Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 3438 | if (NewTemplate) |
| 3439 | NewTemplate->setAccess(New->getAccess()); |
| Chris Lattner | 01564d9 | 2007-01-27 19:27:06 +0000 | [diff] [blame] | 3440 | } |
| 3441 | |
| Chris Lattner | b6738ec | 2007-01-28 00:38:24 +0000 | [diff] [blame] | 3442 | /// ParsedFreeStandingDeclSpec - This method is invoked when a declspec with |
| 3443 | /// no declarator (e.g. "struct foo;") is parsed. |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3444 | Decl *Sema::ParsedFreeStandingDeclSpec(Scope *S, AccessSpecifier AS, |
| John McCall | aa01737 | 2011-03-22 23:00:04 +0000 | [diff] [blame] | 3445 | DeclSpec &DS) { |
| Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 3446 | return ParsedFreeStandingDeclSpec(S, AS, DS, MultiTemplateParamsArg()); |
| Chandler Carruth | 7c9856d | 2011-05-03 18:35:10 +0000 | [diff] [blame] | 3447 | } |
| 3448 | |
| David Majnemer | a7f8c46 | 2015-03-19 21:54:30 +0000 | [diff] [blame] | 3449 | // The MS ABI changed between VS2013 and VS2015 with regard to numbers used to |
| 3450 | // disambiguate entities defined in different scopes. |
| 3451 | // While the VS2015 ABI fixes potential miscompiles, it is also breaks |
| 3452 | // compatibility. |
| 3453 | // We will pick our mangling number depending on which version of MSVC is being |
| 3454 | // targeted. |
| 3455 | static unsigned getMSManglingNumber(const LangOptions &LO, Scope *S) { |
| 3456 | return LO.isCompatibleWithMSVC(19) ? S->getMSCurManglingNumber() |
| 3457 | : S->getMSLastManglingNumber(); |
| 3458 | } |
| 3459 | |
| David Majnemer | f9bde28 | 2015-03-11 06:45:39 +0000 | [diff] [blame] | 3460 | void Sema::handleTagNumbering(const TagDecl *Tag, Scope *TagScope) { |
| 3461 | if (!Context.getLangOpts().CPlusPlus) |
| Reid Kleckner | d8110b6 | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 3462 | return; |
| 3463 | |
| Eli Friedman | 3b7d46c | 2013-07-10 00:30:46 +0000 | [diff] [blame] | 3464 | if (isa<CXXRecordDecl>(Tag->getParent())) { |
| 3465 | // If this tag is the direct child of a class, number it if |
| 3466 | // it is anonymous. |
| 3467 | if (!Tag->getName().empty() || Tag->getTypedefNameForAnonDecl()) |
| 3468 | return; |
| 3469 | MangleNumberingContext &MCtx = |
| David Majnemer | f9bde28 | 2015-03-11 06:45:39 +0000 | [diff] [blame] | 3470 | Context.getManglingNumberContext(Tag->getParent()); |
| 3471 | Context.setManglingNumber( |
| David Majnemer | a7f8c46 | 2015-03-19 21:54:30 +0000 | [diff] [blame] | 3472 | Tag, MCtx.getManglingNumber( |
| 3473 | Tag, getMSManglingNumber(getLangOpts(), TagScope))); |
| Eli Friedman | 3b7d46c | 2013-07-10 00:30:46 +0000 | [diff] [blame] | 3474 | return; |
| 3475 | } |
| 3476 | |
| 3477 | // If this tag isn't a direct child of a class, number it if it is local. |
| 3478 | Decl *ManglingContextDecl; |
| David Majnemer | f9bde28 | 2015-03-11 06:45:39 +0000 | [diff] [blame] | 3479 | if (MangleNumberingContext *MCtx = getCurrentMangleNumberContext( |
| 3480 | Tag->getDeclContext(), ManglingContextDecl)) { |
| 3481 | Context.setManglingNumber( |
| David Majnemer | a7f8c46 | 2015-03-19 21:54:30 +0000 | [diff] [blame] | 3482 | Tag, MCtx->getManglingNumber( |
| 3483 | Tag, getMSManglingNumber(getLangOpts(), TagScope))); |
| Eli Friedman | 3b7d46c | 2013-07-10 00:30:46 +0000 | [diff] [blame] | 3484 | } |
| 3485 | } |
| 3486 | |
| David Majnemer | f9bde28 | 2015-03-11 06:45:39 +0000 | [diff] [blame] | 3487 | void Sema::setTagNameForLinkagePurposes(TagDecl *TagFromDeclSpec, |
| 3488 | TypedefNameDecl *NewTD) { |
| 3489 | // Do nothing if the tag is not anonymous or already has an |
| 3490 | // associated typedef (from an earlier typedef in this decl group). |
| 3491 | if (TagFromDeclSpec->getIdentifier()) |
| 3492 | return; |
| 3493 | if (TagFromDeclSpec->getTypedefNameForAnonDecl()) |
| 3494 | return; |
| 3495 | |
| 3496 | // A well-formed anonymous tag must always be a TUK_Definition. |
| 3497 | assert(TagFromDeclSpec->isThisDeclarationADefinition()); |
| 3498 | |
| 3499 | // The type must match the tag exactly; no qualifiers allowed. |
| 3500 | if (!Context.hasSameType(NewTD->getUnderlyingType(), |
| 3501 | Context.getTagDeclType(TagFromDeclSpec))) |
| 3502 | return; |
| 3503 | |
| 3504 | // If we've already computed linkage for the anonymous tag, then |
| 3505 | // adding a typedef name for the anonymous decl can change that |
| 3506 | // linkage, which might be a serious problem. Diagnose this as |
| 3507 | // unsupported and ignore the typedef name. TODO: we should |
| 3508 | // pursue this as a language defect and establish a formal rule |
| 3509 | // for how to handle it. |
| 3510 | if (TagFromDeclSpec->hasLinkageBeenComputed()) { |
| 3511 | Diag(NewTD->getLocation(), diag::err_typedef_changes_linkage); |
| 3512 | |
| 3513 | SourceLocation tagLoc = TagFromDeclSpec->getInnerLocStart(); |
| 3514 | tagLoc = getLocForEndOfToken(tagLoc); |
| 3515 | |
| 3516 | llvm::SmallString<40> textToInsert; |
| 3517 | textToInsert += ' '; |
| 3518 | textToInsert += NewTD->getIdentifier()->getName(); |
| 3519 | Diag(tagLoc, diag::note_typedef_changes_linkage) |
| 3520 | << FixItHint::CreateInsertion(tagLoc, textToInsert); |
| 3521 | return; |
| 3522 | } |
| 3523 | |
| 3524 | // Otherwise, set this is the anon-decl typedef for the tag. |
| 3525 | TagFromDeclSpec->setTypedefNameForAnonDecl(NewTD); |
| 3526 | } |
| 3527 | |
| Chandler Carruth | 7c9856d | 2011-05-03 18:35:10 +0000 | [diff] [blame] | 3528 | /// ParsedFreeStandingDeclSpec - This method is invoked when a declspec with |
| Richard Smith | b1402ae | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 3529 | /// no declarator (e.g. "struct foo;") is parsed. It also accepts template |
| Chandler Carruth | 7c9856d | 2011-05-03 18:35:10 +0000 | [diff] [blame] | 3530 | /// parameters to cope with template friend declarations. |
| 3531 | Decl *Sema::ParsedFreeStandingDeclSpec(Scope *S, AccessSpecifier AS, |
| 3532 | DeclSpec &DS, |
| Richard Smith | b1402ae | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 3533 | MultiTemplateParamsArg TemplateParams, |
| 3534 | bool IsExplicitInstantiation) { |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3535 | Decl *TagD = nullptr; |
| 3536 | TagDecl *Tag = nullptr; |
| Douglas Gregor | 9817f4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 3537 | if (DS.getTypeSpecType() == DeclSpec::TST_class || |
| 3538 | DS.getTypeSpecType() == DeclSpec::TST_struct || |
| Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 3539 | DS.getTypeSpecType() == DeclSpec::TST_interface || |
| Douglas Gregor | 9817f4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 3540 | DS.getTypeSpecType() == DeclSpec::TST_union || |
| Douglas Gregor | 1b57ff3 | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 3541 | DS.getTypeSpecType() == DeclSpec::TST_enum) { |
| John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3542 | TagD = DS.getRepAsDecl(); |
| John McCall | c398748 | 2009-10-07 23:34:25 +0000 | [diff] [blame] | 3543 | |
| 3544 | if (!TagD) // We probably had an error |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3545 | return nullptr; |
| Douglas Gregor | 1b57ff3 | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 3546 | |
| John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 3547 | // Note that the above type specs guarantee that the |
| 3548 | // type rep is a Decl, whereas in many of the others |
| 3549 | // it's a Type. |
| Peter Collingbourne | e109a2c | 2011-10-23 17:07:16 +0000 | [diff] [blame] | 3550 | if (isa<TagDecl>(TagD)) |
| 3551 | Tag = cast<TagDecl>(TagD); |
| 3552 | else if (ClassTemplateDecl *CTD = dyn_cast<ClassTemplateDecl>(TagD)) |
| 3553 | Tag = CTD->getTemplatedDecl(); |
| Douglas Gregor | 1b57ff3 | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 3554 | } |
| Douglas Gregor | 9817f4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 3555 | |
| Argyrios Kyrtzidis | 2559629 | 2012-03-09 20:10:30 +0000 | [diff] [blame] | 3556 | if (Tag) { |
| David Majnemer | f9bde28 | 2015-03-11 06:45:39 +0000 | [diff] [blame] | 3557 | handleTagNumbering(Tag, S); |
| Argyrios Kyrtzidis | 201d377 | 2011-09-30 22:11:31 +0000 | [diff] [blame] | 3558 | Tag->setFreeStanding(); |
| Argyrios Kyrtzidis | 2559629 | 2012-03-09 20:10:30 +0000 | [diff] [blame] | 3559 | if (Tag->isInvalidDecl()) |
| 3560 | return Tag; |
| 3561 | } |
| Argyrios Kyrtzidis | 201d377 | 2011-09-30 22:11:31 +0000 | [diff] [blame] | 3562 | |
| Nuno Lopes | e9823fa | 2009-12-17 11:35:26 +0000 | [diff] [blame] | 3563 | if (unsigned TypeQuals = DS.getTypeQualifiers()) { |
| 3564 | // Enforce C99 6.7.3p2: "Types other than pointer types derived from object |
| 3565 | // or incomplete types shall not be restrict-qualified." |
| 3566 | if (TypeQuals & DeclSpec::TQ_restrict) |
| 3567 | Diag(DS.getRestrictSpecLoc(), |
| 3568 | diag::err_typecheck_invalid_restrict_not_pointer_noarg) |
| 3569 | << DS.getSourceRange(); |
| 3570 | } |
| 3571 | |
| Richard Smith | a77a0a6 | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 3572 | if (DS.isConstexprSpecified()) { |
| 3573 | // C++0x [dcl.constexpr]p1: constexpr can only be applied to declarations |
| 3574 | // and definitions of functions and variables. |
| 3575 | if (Tag) |
| 3576 | Diag(DS.getConstexprSpecLoc(), diag::err_constexpr_tag) |
| 3577 | << (DS.getTypeSpecType() == DeclSpec::TST_class ? 0 : |
| 3578 | DS.getTypeSpecType() == DeclSpec::TST_struct ? 1 : |
| Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 3579 | DS.getTypeSpecType() == DeclSpec::TST_interface ? 2 : |
| 3580 | DS.getTypeSpecType() == DeclSpec::TST_union ? 3 : 4); |
| Richard Smith | a77a0a6 | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 3581 | else |
| 3582 | Diag(DS.getConstexprSpecLoc(), diag::err_constexpr_no_declarators); |
| 3583 | // Don't emit warnings after this error. |
| 3584 | return TagD; |
| 3585 | } |
| 3586 | |
| Richard Smith | b1402ae | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 3587 | DiagnoseFunctionSpecifiers(DS); |
| 3588 | |
| Douglas Gregor | 3dad842 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 3589 | if (DS.isFriendSpecified()) { |
| John McCall | ace48cd | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 3590 | // If we're dealing with a decl but not a TagDecl, assume that |
| 3591 | // whatever routines created it handled the friendship aspect. |
| 3592 | if (TagD && !Tag) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3593 | return nullptr; |
| Chandler Carruth | 7c9856d | 2011-05-03 18:35:10 +0000 | [diff] [blame] | 3594 | return ActOnFriendTypeDecl(S, DS, TemplateParams); |
| Douglas Gregor | 3dad842 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 3595 | } |
| John McCall | aa01737 | 2011-03-22 23:00:04 +0000 | [diff] [blame] | 3596 | |
| Nico Weber | 4d1bed8 | 2015-02-14 05:47:01 +0000 | [diff] [blame] | 3597 | const CXXScopeSpec &SS = DS.getTypeSpecScope(); |
| Richard Smith | b1402ae | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 3598 | bool IsExplicitSpecialization = |
| 3599 | !TemplateParams.empty() && TemplateParams.back()->size() == 0; |
| 3600 | if (Tag && SS.isNotEmpty() && !Tag->isCompleteDefinition() && |
| 3601 | !IsExplicitInstantiation && !IsExplicitSpecialization) { |
| 3602 | // Per C++ [dcl.type.elab]p1, a class declaration cannot have a |
| 3603 | // nested-name-specifier unless it is an explicit instantiation |
| 3604 | // or an explicit specialization. |
| 3605 | // Per C++ [dcl.enum]p1, an opaque-enum-declaration can't either. |
| 3606 | Diag(SS.getBeginLoc(), diag::err_standalone_class_nested_name_specifier) |
| 3607 | << (DS.getTypeSpecType() == DeclSpec::TST_class ? 0 : |
| 3608 | DS.getTypeSpecType() == DeclSpec::TST_struct ? 1 : |
| 3609 | DS.getTypeSpecType() == DeclSpec::TST_interface ? 2 : |
| 3610 | DS.getTypeSpecType() == DeclSpec::TST_union ? 3 : 4) |
| 3611 | << SS.getRange(); |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3612 | return nullptr; |
| Richard Smith | b1402ae | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 3613 | } |
| 3614 | |
| 3615 | // Track whether this decl-specifier declares anything. |
| 3616 | bool DeclaresAnything = true; |
| 3617 | |
| 3618 | // Handle anonymous struct definitions. |
| Douglas Gregor | c6f58fe | 2009-01-12 22:49:06 +0000 | [diff] [blame] | 3619 | if (RecordDecl *Record = dyn_cast_or_null<RecordDecl>(Tag)) { |
| John McCall | f937c02 | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 3620 | if (!Record->getDeclName() && Record->isCompleteDefinition() && |
| Douglas Gregor | 2e7cba6 | 2009-03-06 23:06:59 +0000 | [diff] [blame] | 3621 | DS.getStorageClassSpec() != DeclSpec::SCS_typedef) { |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3622 | if (getLangOpts().CPlusPlus || |
| Douglas Gregor | 2e7cba6 | 2009-03-06 23:06:59 +0000 | [diff] [blame] | 3623 | Record->getDeclContext()->isRecord()) |
| Nico Weber | 01a46ad | 2015-02-15 06:15:40 +0000 | [diff] [blame] | 3624 | return BuildAnonymousStructOrUnion(S, DS, AS, Record, |
| 3625 | Context.getPrintingPolicy()); |
| Douglas Gregor | 2e7cba6 | 2009-03-06 23:06:59 +0000 | [diff] [blame] | 3626 | |
| Richard Smith | b1402ae | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 3627 | DeclaresAnything = false; |
| Douglas Gregor | 2e7cba6 | 2009-03-06 23:06:59 +0000 | [diff] [blame] | 3628 | } |
| Francois Pichet | 0c71f6c | 2010-11-23 06:07:27 +0000 | [diff] [blame] | 3629 | } |
| Douglas Gregor | c6f58fe | 2009-01-12 22:49:06 +0000 | [diff] [blame] | 3630 | |
| David Majnemer | 8f0ed91 | 2014-08-11 07:29:54 +0000 | [diff] [blame] | 3631 | // C11 6.7.2.1p2: |
| 3632 | // A struct-declaration that does not declare an anonymous structure or |
| 3633 | // anonymous union shall contain a struct-declarator-list. |
| Richard Smith | 04d3b3e | 2014-08-26 21:51:57 +0000 | [diff] [blame] | 3634 | // |
| 3635 | // This rule also existed in C89 and C99; the grammar for struct-declaration |
| 3636 | // did not permit a struct-declaration without a struct-declarator-list. |
| Fariborz Jahanian | ffc120a | 2014-08-26 21:10:47 +0000 | [diff] [blame] | 3637 | if (!getLangOpts().CPlusPlus && CurContext->isRecord() && |
| Francois Pichet | 0c71f6c | 2010-11-23 06:07:27 +0000 | [diff] [blame] | 3638 | DS.getStorageClassSpec() == DeclSpec::SCS_unspecified) { |
| David Majnemer | 8f0ed91 | 2014-08-11 07:29:54 +0000 | [diff] [blame] | 3639 | // Check for Microsoft C extension: anonymous struct/union member. |
| 3640 | // Handle 2 kinds of anonymous struct/union: |
| Francois Pichet | 0c71f6c | 2010-11-23 06:07:27 +0000 | [diff] [blame] | 3641 | // struct STRUCT; |
| David Majnemer | 8f0ed91 | 2014-08-11 07:29:54 +0000 | [diff] [blame] | 3642 | // union UNION; |
| Francois Pichet | 0c71f6c | 2010-11-23 06:07:27 +0000 | [diff] [blame] | 3643 | // and |
| 3644 | // STRUCT_TYPE; <- where STRUCT_TYPE is a typedef struct. |
| David Majnemer | 8f0ed91 | 2014-08-11 07:29:54 +0000 | [diff] [blame] | 3645 | // UNION_TYPE; <- where UNION_TYPE is a typedef union. |
| 3646 | if ((Tag && Tag->getDeclName()) || |
| 3647 | DS.getTypeSpecType() == DeclSpec::TST_typename) { |
| 3648 | RecordDecl *Record = nullptr; |
| 3649 | if (Tag) |
| 3650 | Record = dyn_cast<RecordDecl>(Tag); |
| 3651 | else if (const RecordType *RT = |
| 3652 | DS.getRepAsType().get()->getAsStructureType()) |
| 3653 | Record = RT->getDecl(); |
| 3654 | else if (const RecordType *UT = DS.getRepAsType().get()->getAsUnionType()) |
| 3655 | Record = UT->getDecl(); |
| 3656 | |
| 3657 | if (Record && getLangOpts().MicrosoftExt) { |
| 3658 | Diag(DS.getLocStart(), diag::ext_ms_anonymous_record) |
| 3659 | << Record->isUnion() << DS.getSourceRange(); |
| 3660 | return BuildMicrosoftCAnonymousStruct(S, DS, Record); |
| 3661 | } |
| 3662 | |
| 3663 | DeclaresAnything = false; |
| Francois Pichet | 0c71f6c | 2010-11-23 06:07:27 +0000 | [diff] [blame] | 3664 | } |
| Douglas Gregor | c6f58fe | 2009-01-12 22:49:06 +0000 | [diff] [blame] | 3665 | } |
| Richard Smith | b1402ae | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 3666 | |
| 3667 | // Skip all the checks below if we have a type error. |
| 3668 | if (DS.getTypeSpecType() == DeclSpec::TST_error || |
| 3669 | (TagD && TagD->isInvalidDecl())) |
| 3670 | return TagD; |
| 3671 | |
| 3672 | if (getLangOpts().CPlusPlus && |
| Douglas Gregor | aa8c972 | 2010-07-13 06:24:26 +0000 | [diff] [blame] | 3673 | DS.getStorageClassSpec() != DeclSpec::SCS_typedef) |
| 3674 | if (EnumDecl *Enum = dyn_cast_or_null<EnumDecl>(Tag)) |
| 3675 | if (Enum->enumerator_begin() == Enum->enumerator_end() && |
| Richard Smith | b1402ae | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 3676 | !Enum->getIdentifier() && !Enum->isInvalidDecl()) |
| 3677 | DeclaresAnything = false; |
| John McCall | aa01737 | 2011-03-22 23:00:04 +0000 | [diff] [blame] | 3678 | |
| John McCall | aa01737 | 2011-03-22 23:00:04 +0000 | [diff] [blame] | 3679 | if (!DS.isMissingDeclaratorOk()) { |
| Richard Smith | b1402ae | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 3680 | // Customize diagnostic for a typedef missing a name. |
| 3681 | if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) |
| Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3682 | Diag(DS.getLocStart(), diag::ext_typedef_without_a_name) |
| Douglas Gregor | 3a8e0d7 | 2010-07-16 15:40:40 +0000 | [diff] [blame] | 3683 | << DS.getSourceRange(); |
| Richard Smith | b1402ae | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 3684 | else |
| 3685 | DeclaresAnything = false; |
| Sebastian Redl | a2b5e31 | 2008-12-28 15:28:59 +0000 | [diff] [blame] | 3686 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3687 | |
| Richard Smith | b1402ae | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 3688 | if (DS.isModulePrivateSpecified() && |
| Douglas Gregor | 4186681 | 2011-09-12 18:37:38 +0000 | [diff] [blame] | 3689 | Tag && Tag->getDeclContext()->isFunctionOrMethod()) |
| 3690 | Diag(DS.getModulePrivateSpecLoc(), diag::err_module_private_local_class) |
| 3691 | << Tag->getTagKind() |
| 3692 | << FixItHint::CreateRemoval(DS.getModulePrivateSpecLoc()); |
| 3693 | |
| Richard Smith | b1402ae | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 3694 | ActOnDocumentableDecl(TagD); |
| 3695 | |
| 3696 | // C 6.7/2: |
| 3697 | // A declaration [...] shall declare at least a declarator [...], a tag, |
| 3698 | // or the members of an enumeration. |
| 3699 | // C++ [dcl.dcl]p3: |
| 3700 | // [If there are no declarators], and except for the declaration of an |
| 3701 | // unnamed bit-field, the decl-specifier-seq shall introduce one or more |
| 3702 | // names into the program, or shall redeclare a name introduced by a |
| 3703 | // previous declaration. |
| 3704 | if (!DeclaresAnything) { |
| 3705 | // In C, we allow this as a (popular) extension / bug. Don't bother |
| 3706 | // producing further diagnostics for redundant qualifiers after this. |
| 3707 | Diag(DS.getLocStart(), diag::ext_no_declarators) << DS.getSourceRange(); |
| 3708 | return TagD; |
| 3709 | } |
| 3710 | |
| 3711 | // C++ [dcl.stc]p1: |
| 3712 | // If a storage-class-specifier appears in a decl-specifier-seq, [...] the |
| 3713 | // init-declarator-list of the declaration shall not be empty. |
| 3714 | // C++ [dcl.fct.spec]p1: |
| 3715 | // If a cv-qualifier appears in a decl-specifier-seq, the |
| 3716 | // init-declarator-list of the declaration shall not be empty. |
| 3717 | // |
| 3718 | // Spurious qualifiers here appear to be valid in C. |
| 3719 | unsigned DiagID = diag::warn_standalone_specifier; |
| 3720 | if (getLangOpts().CPlusPlus) |
| 3721 | DiagID = diag::ext_standalone_specifier; |
| 3722 | |
| 3723 | // Note that a linkage-specification sets a storage class, but |
| 3724 | // 'extern "C" struct foo;' is actually valid and not theoretically |
| 3725 | // useless. |
| Aaron Ballman | 5a1ef6b | 2014-05-26 17:03:54 +0000 | [diff] [blame] | 3726 | if (DeclSpec::SCS SCS = DS.getStorageClassSpec()) { |
| 3727 | if (SCS == DeclSpec::SCS_mutable) |
| 3728 | // Since mutable is not a viable storage class specifier in C, there is |
| 3729 | // no reason to treat it as an extension. Instead, diagnose as an error. |
| 3730 | Diag(DS.getStorageClassSpecLoc(), diag::err_mutable_nonmember); |
| 3731 | else if (!DS.isExternInLinkageSpec() && SCS != DeclSpec::SCS_typedef) |
| Richard Smith | b1402ae | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 3732 | Diag(DS.getStorageClassSpecLoc(), DiagID) |
| 3733 | << DeclSpec::getSpecifierName(SCS); |
| Aaron Ballman | 5a1ef6b | 2014-05-26 17:03:54 +0000 | [diff] [blame] | 3734 | } |
| Richard Smith | b1402ae | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 3735 | |
| Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 3736 | if (DeclSpec::TSCS TSCS = DS.getThreadStorageClassSpec()) |
| 3737 | Diag(DS.getThreadStorageClassSpecLoc(), DiagID) |
| 3738 | << DeclSpec::getSpecifierName(TSCS); |
| Richard Smith | b1402ae | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 3739 | if (DS.getTypeQualifiers()) { |
| 3740 | if (DS.getTypeQualifiers() & DeclSpec::TQ_const) |
| 3741 | Diag(DS.getConstSpecLoc(), DiagID) << "const"; |
| 3742 | if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile) |
| 3743 | Diag(DS.getConstSpecLoc(), DiagID) << "volatile"; |
| 3744 | // Restrict is covered above. |
| Richard Smith | 8e1ac33 | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 3745 | if (DS.getTypeQualifiers() & DeclSpec::TQ_atomic) |
| 3746 | Diag(DS.getAtomicSpecLoc(), DiagID) << "_Atomic"; |
| Richard Smith | b1402ae | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 3747 | } |
| 3748 | |
| Eli Friedman | e321795 | 2011-12-17 00:36:09 +0000 | [diff] [blame] | 3749 | // Warn about ignored type attributes, for example: |
| 3750 | // __attribute__((aligned)) struct A; |
| Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3751 | // Attributes should be placed after tag to apply to type declaration. |
| Eli Friedman | e321795 | 2011-12-17 00:36:09 +0000 | [diff] [blame] | 3752 | if (!DS.getAttributes().empty()) { |
| 3753 | DeclSpec::TST TypeSpecType = DS.getTypeSpecType(); |
| 3754 | if (TypeSpecType == DeclSpec::TST_class || |
| 3755 | TypeSpecType == DeclSpec::TST_struct || |
| Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 3756 | TypeSpecType == DeclSpec::TST_interface || |
| Eli Friedman | e321795 | 2011-12-17 00:36:09 +0000 | [diff] [blame] | 3757 | TypeSpecType == DeclSpec::TST_union || |
| 3758 | TypeSpecType == DeclSpec::TST_enum) { |
| 3759 | AttributeList* attrs = DS.getAttributes().getList(); |
| 3760 | while (attrs) { |
| Michael Han | 360d225 | 2012-10-04 16:42:52 +0000 | [diff] [blame] | 3761 | Diag(attrs->getLoc(), diag::warn_declspec_attribute_ignored) |
| Eli Friedman | e321795 | 2011-12-17 00:36:09 +0000 | [diff] [blame] | 3762 | << attrs->getName() |
| 3763 | << (TypeSpecType == DeclSpec::TST_class ? 0 : |
| 3764 | TypeSpecType == DeclSpec::TST_struct ? 1 : |
| Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 3765 | TypeSpecType == DeclSpec::TST_union ? 2 : |
| 3766 | TypeSpecType == DeclSpec::TST_interface ? 3 : 4); |
| Eli Friedman | e321795 | 2011-12-17 00:36:09 +0000 | [diff] [blame] | 3767 | attrs = attrs->getNext(); |
| 3768 | } |
| 3769 | } |
| 3770 | } |
| John McCall | aa01737 | 2011-03-22 23:00:04 +0000 | [diff] [blame] | 3771 | |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3772 | return TagD; |
| Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3773 | } |
| 3774 | |
| John McCall | ea305ed | 2009-12-18 10:40:03 +0000 | [diff] [blame] | 3775 | /// We are trying to inject an anonymous member into the given scope; |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 3776 | /// check if there's an existing declaration that can't be overloaded. |
| 3777 | /// |
| 3778 | /// \return true if this is a forbidden redeclaration |
| John McCall | ea305ed | 2009-12-18 10:40:03 +0000 | [diff] [blame] | 3779 | static bool CheckAnonMemberRedeclaration(Sema &SemaRef, |
| 3780 | Scope *S, |
| Fariborz Jahanian | 53967e2 | 2010-01-22 18:30:17 +0000 | [diff] [blame] | 3781 | DeclContext *Owner, |
| John McCall | ea305ed | 2009-12-18 10:40:03 +0000 | [diff] [blame] | 3782 | DeclarationName Name, |
| 3783 | SourceLocation NameLoc, |
| 3784 | unsigned diagnostic) { |
| 3785 | LookupResult R(SemaRef, Name, NameLoc, Sema::LookupMemberName, |
| 3786 | Sema::ForRedeclaration); |
| 3787 | if (!SemaRef.LookupName(R, S)) return false; |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 3788 | |
| John McCall | ea305ed | 2009-12-18 10:40:03 +0000 | [diff] [blame] | 3789 | if (R.getAsSingle<TagDecl>()) |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 3790 | return false; |
| 3791 | |
| 3792 | // Pick a representative declaration. |
| John McCall | ea305ed | 2009-12-18 10:40:03 +0000 | [diff] [blame] | 3793 | NamedDecl *PrevDecl = R.getRepresentativeDecl()->getUnderlyingDecl(); |
| Argyrios Kyrtzidis | e619e99 | 2010-09-23 14:26:01 +0000 | [diff] [blame] | 3794 | assert(PrevDecl && "Expected a non-null Decl"); |
| 3795 | |
| 3796 | if (!SemaRef.isDeclInScope(PrevDecl, Owner, S)) |
| 3797 | return false; |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 3798 | |
| John McCall | ea305ed | 2009-12-18 10:40:03 +0000 | [diff] [blame] | 3799 | SemaRef.Diag(NameLoc, diagnostic) << Name; |
| 3800 | SemaRef.Diag(PrevDecl->getLocation(), diag::note_previous_declaration); |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 3801 | |
| 3802 | return true; |
| 3803 | } |
| 3804 | |
| Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3805 | /// InjectAnonymousStructOrUnionMembers - Inject the members of the |
| 3806 | /// anonymous struct or union AnonRecord into the owning context Owner |
| 3807 | /// and scope S. This routine will be invoked just after we realize |
| 3808 | /// that an unnamed union or struct is actually an anonymous union or |
| 3809 | /// struct, e.g., |
| 3810 | /// |
| 3811 | /// @code |
| 3812 | /// union { |
| 3813 | /// int i; |
| 3814 | /// float f; |
| 3815 | /// }; // InjectAnonymousStructOrUnionMembers called here to inject i and |
| 3816 | /// // f into the surrounding scope.x |
| 3817 | /// @endcode |
| 3818 | /// |
| 3819 | /// This routine is recursive, injecting the names of nested anonymous |
| 3820 | /// structs/unions into the owning context and scope as well. |
| John McCall | b54367d | 2010-05-21 20:45:30 +0000 | [diff] [blame] | 3821 | static bool InjectAnonymousStructOrUnionMembers(Sema &SemaRef, Scope *S, |
| Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 3822 | DeclContext *Owner, |
| 3823 | RecordDecl *AnonRecord, |
| 3824 | AccessSpecifier AS, |
| 3825 | SmallVectorImpl<NamedDecl *> &Chaining, |
| 3826 | bool MSAnonStruct) { |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 3827 | unsigned diagKind |
| 3828 | = AnonRecord->isUnion() ? diag::err_anonymous_union_member_redecl |
| 3829 | : diag::err_anonymous_struct_member_redecl; |
| 3830 | |
| Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3831 | bool Invalid = false; |
| Francois Pichet | 0c71f6c | 2010-11-23 06:07:27 +0000 | [diff] [blame] | 3832 | |
| 3833 | // Look every FieldDecl and IndirectFieldDecl with a name. |
| Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 3834 | for (auto *D : AnonRecord->decls()) { |
| 3835 | if ((isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D)) && |
| 3836 | cast<NamedDecl>(D)->getDeclName()) { |
| 3837 | ValueDecl *VD = cast<ValueDecl>(D); |
| Francois Pichet | 0c71f6c | 2010-11-23 06:07:27 +0000 | [diff] [blame] | 3838 | if (CheckAnonMemberRedeclaration(SemaRef, S, Owner, VD->getDeclName(), |
| 3839 | VD->getLocation(), diagKind)) { |
| Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3840 | // C++ [class.union]p2: |
| 3841 | // The names of the members of an anonymous union shall be |
| 3842 | // distinct from the names of any other entity in the |
| 3843 | // scope in which the anonymous union is declared. |
| Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3844 | Invalid = true; |
| 3845 | } else { |
| 3846 | // C++ [class.union]p2: |
| 3847 | // For the purpose of name lookup, after the anonymous union |
| 3848 | // definition, the members of the anonymous union are |
| 3849 | // considered to have been defined in the scope in which the |
| 3850 | // anonymous union is declared. |
| Francois Pichet | 0c71f6c | 2010-11-23 06:07:27 +0000 | [diff] [blame] | 3851 | unsigned OldChainingSize = Chaining.size(); |
| 3852 | if (IndirectFieldDecl *IF = dyn_cast<IndirectFieldDecl>(VD)) |
| Benjamin Kramer | f989042 | 2015-02-17 16:48:30 +0000 | [diff] [blame] | 3853 | Chaining.append(IF->chain_begin(), IF->chain_end()); |
| Francois Pichet | 0c71f6c | 2010-11-23 06:07:27 +0000 | [diff] [blame] | 3854 | else |
| 3855 | Chaining.push_back(VD); |
| 3856 | |
| Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 3857 | assert(Chaining.size() >= 2); |
| 3858 | NamedDecl **NamedChain = |
| 3859 | new (SemaRef.Context)NamedDecl*[Chaining.size()]; |
| 3860 | for (unsigned i = 0; i < Chaining.size(); i++) |
| 3861 | NamedChain[i] = Chaining[i]; |
| 3862 | |
| Aaron Ballman | 260995b | 2014-10-15 16:58:18 +0000 | [diff] [blame] | 3863 | IndirectFieldDecl *IndirectField = IndirectFieldDecl::Create( |
| 3864 | SemaRef.Context, Owner, VD->getLocation(), VD->getIdentifier(), |
| 3865 | VD->getType(), NamedChain, Chaining.size()); |
| 3866 | |
| 3867 | for (const auto *Attr : VD->attrs()) |
| 3868 | IndirectField->addAttr(Attr->clone(SemaRef.Context)); |
| Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 3869 | |
| 3870 | IndirectField->setAccess(AS); |
| 3871 | IndirectField->setImplicit(); |
| 3872 | SemaRef.PushOnScopeChains(IndirectField, S); |
| John McCall | b54367d | 2010-05-21 20:45:30 +0000 | [diff] [blame] | 3873 | |
| 3874 | // That includes picking up the appropriate access specifier. |
| Francois Pichet | 0c71f6c | 2010-11-23 06:07:27 +0000 | [diff] [blame] | 3875 | if (AS != AS_none) IndirectField->setAccess(AS); |
| Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 3876 | |
| Francois Pichet | 0c71f6c | 2010-11-23 06:07:27 +0000 | [diff] [blame] | 3877 | Chaining.resize(OldChainingSize); |
| Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3878 | } |
| Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3879 | } |
| 3880 | } |
| 3881 | |
| 3882 | return Invalid; |
| 3883 | } |
| 3884 | |
| Douglas Gregor | c4df407 | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 3885 | /// StorageClassSpecToVarDeclStorageClass - Maps a DeclSpec::SCS to |
| 3886 | /// a VarDecl::StorageClass. Any error reporting is up to the caller: |
| John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 3887 | /// illegal input values are mapped to SC_None. |
| 3888 | static StorageClass |
| Rafael Espindola | bff5956 | 2013-04-25 12:11:36 +0000 | [diff] [blame] | 3889 | StorageClassSpecToVarDeclStorageClass(const DeclSpec &DS) { |
| 3890 | DeclSpec::SCS StorageClassSpec = DS.getStorageClassSpec(); |
| 3891 | assert(StorageClassSpec != DeclSpec::SCS_typedef && |
| 3892 | "Parser allowed 'typedef' as storage class VarDecl."); |
| Douglas Gregor | c4df407 | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 3893 | switch (StorageClassSpec) { |
| John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 3894 | case DeclSpec::SCS_unspecified: return SC_None; |
| Rafael Espindola | bff5956 | 2013-04-25 12:11:36 +0000 | [diff] [blame] | 3895 | case DeclSpec::SCS_extern: |
| 3896 | if (DS.isExternInLinkageSpec()) |
| 3897 | return SC_None; |
| 3898 | return SC_Extern; |
| John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 3899 | case DeclSpec::SCS_static: return SC_Static; |
| 3900 | case DeclSpec::SCS_auto: return SC_Auto; |
| 3901 | case DeclSpec::SCS_register: return SC_Register; |
| 3902 | case DeclSpec::SCS_private_extern: return SC_PrivateExtern; |
| Douglas Gregor | c4df407 | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 3903 | // Illegal SCSs map to None: error reporting is up to the caller. |
| 3904 | case DeclSpec::SCS_mutable: // Fall through. |
| John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 3905 | case DeclSpec::SCS_typedef: return SC_None; |
| Douglas Gregor | c4df407 | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 3906 | } |
| 3907 | llvm_unreachable("unknown storage class specifier"); |
| 3908 | } |
| 3909 | |
| Richard Smith | ab44d5b | 2013-12-10 08:25:00 +0000 | [diff] [blame] | 3910 | static SourceLocation findDefaultInitializer(const CXXRecordDecl *Record) { |
| 3911 | assert(Record->hasInClassInitializer()); |
| 3912 | |
| Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 3913 | for (const auto *I : Record->decls()) { |
| 3914 | const auto *FD = dyn_cast<FieldDecl>(I); |
| 3915 | if (const auto *IFD = dyn_cast<IndirectFieldDecl>(I)) |
| Richard Smith | ab44d5b | 2013-12-10 08:25:00 +0000 | [diff] [blame] | 3916 | FD = IFD->getAnonField(); |
| 3917 | if (FD && FD->hasInClassInitializer()) |
| 3918 | return FD->getLocation(); |
| 3919 | } |
| 3920 | |
| 3921 | llvm_unreachable("couldn't find in-class initializer"); |
| 3922 | } |
| 3923 | |
| 3924 | static void checkDuplicateDefaultInit(Sema &S, CXXRecordDecl *Parent, |
| 3925 | SourceLocation DefaultInitLoc) { |
| 3926 | if (!Parent->isUnion() || !Parent->hasInClassInitializer()) |
| 3927 | return; |
| 3928 | |
| 3929 | S.Diag(DefaultInitLoc, diag::err_multiple_mem_union_initialization); |
| 3930 | S.Diag(findDefaultInitializer(Parent), diag::note_previous_initializer) << 0; |
| 3931 | } |
| 3932 | |
| 3933 | static void checkDuplicateDefaultInit(Sema &S, CXXRecordDecl *Parent, |
| 3934 | CXXRecordDecl *AnonUnion) { |
| 3935 | if (!Parent->isUnion() || !Parent->hasInClassInitializer()) |
| 3936 | return; |
| 3937 | |
| 3938 | checkDuplicateDefaultInit(S, Parent, findDefaultInitializer(AnonUnion)); |
| 3939 | } |
| 3940 | |
| Francois Pichet | 0c71f6c | 2010-11-23 06:07:27 +0000 | [diff] [blame] | 3941 | /// BuildAnonymousStructOrUnion - Handle the declaration of an |
| Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3942 | /// anonymous structure or union. Anonymous unions are a C++ feature |
| Hans Wennborg | b64a1fa | 2012-02-03 15:47:04 +0000 | [diff] [blame] | 3943 | /// (C++ [class.union]) and a C11 feature; anonymous structures |
| 3944 | /// are a C11 feature and GNU C++ extension. |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3945 | Decl *Sema::BuildAnonymousStructOrUnion(Scope *S, DeclSpec &DS, |
| Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3946 | AccessSpecifier AS, |
| 3947 | RecordDecl *Record, |
| 3948 | const PrintingPolicy &Policy) { |
| Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3949 | DeclContext *Owner = Record->getDeclContext(); |
| 3950 | |
| 3951 | // Diagnose whether this anonymous struct/union is an extension. |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3952 | if (Record->isUnion() && !getLangOpts().CPlusPlus && !getLangOpts().C11) |
| Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3953 | Diag(Record->getLocation(), diag::ext_anonymous_union); |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3954 | else if (!Record->isUnion() && getLangOpts().CPlusPlus) |
| Hans Wennborg | b64a1fa | 2012-02-03 15:47:04 +0000 | [diff] [blame] | 3955 | Diag(Record->getLocation(), diag::ext_gnu_anonymous_struct); |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3956 | else if (!Record->isUnion() && !getLangOpts().C11) |
| Hans Wennborg | b64a1fa | 2012-02-03 15:47:04 +0000 | [diff] [blame] | 3957 | Diag(Record->getLocation(), diag::ext_c11_anonymous_struct); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3958 | |
| Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3959 | // C and C++ require different kinds of checks for anonymous |
| 3960 | // structs/unions. |
| 3961 | bool Invalid = false; |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3962 | if (getLangOpts().CPlusPlus) { |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3963 | const char *PrevSpec = nullptr; |
| John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3964 | unsigned DiagID; |
| David Blaikie | 0a8e899 | 2011-10-19 22:43:29 +0000 | [diff] [blame] | 3965 | if (Record->isUnion()) { |
| 3966 | // C++ [class.union]p6: |
| 3967 | // Anonymous unions declared in a named namespace or in the |
| 3968 | // global namespace shall be declared static. |
| 3969 | if (DS.getStorageClassSpec() != DeclSpec::SCS_static && |
| 3970 | (isa<TranslationUnitDecl>(Owner) || |
| 3971 | (isa<NamespaceDecl>(Owner) && |
| 3972 | cast<NamespaceDecl>(Owner)->getDeclName()))) { |
| David Blaikie | 733f7bb | 2011-10-20 02:49:08 +0000 | [diff] [blame] | 3973 | Diag(Record->getLocation(), diag::err_anonymous_union_not_static) |
| 3974 | << FixItHint::CreateInsertion(Record->getLocation(), "static "); |
| David Blaikie | 0a8e899 | 2011-10-19 22:43:29 +0000 | [diff] [blame] | 3975 | |
| 3976 | // Recover by adding 'static'. |
| 3977 | DS.SetStorageClassSpec(*this, DeclSpec::SCS_static, SourceLocation(), |
| Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3978 | PrevSpec, DiagID, Policy); |
| David Blaikie | 0a8e899 | 2011-10-19 22:43:29 +0000 | [diff] [blame] | 3979 | } |
| 3980 | // C++ [class.union]p6: |
| 3981 | // A storage class is not allowed in a declaration of an |
| 3982 | // anonymous union in a class scope. |
| 3983 | else if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified && |
| 3984 | isa<RecordDecl>(Owner)) { |
| 3985 | Diag(DS.getStorageClassSpecLoc(), |
| David Blaikie | 6f686fc | 2011-10-20 02:10:55 +0000 | [diff] [blame] | 3986 | diag::err_anonymous_union_with_storage_spec) |
| 3987 | << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc()); |
| David Blaikie | 0a8e899 | 2011-10-19 22:43:29 +0000 | [diff] [blame] | 3988 | |
| 3989 | // Recover by removing the storage specifier. |
| David Blaikie | 30d1544 | 2011-10-19 22:56:21 +0000 | [diff] [blame] | 3990 | DS.SetStorageClassSpec(*this, DeclSpec::SCS_unspecified, |
| 3991 | SourceLocation(), |
| Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3992 | PrevSpec, DiagID, Context.getPrintingPolicy()); |
| David Blaikie | 0a8e899 | 2011-10-19 22:43:29 +0000 | [diff] [blame] | 3993 | } |
| Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3994 | } |
| Douglas Gregor | f4d3327 | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 3995 | |
| Douglas Gregor | 0f8bc97 | 2011-05-09 23:05:33 +0000 | [diff] [blame] | 3996 | // Ignore const/volatile/restrict qualifiers. |
| 3997 | if (DS.getTypeQualifiers()) { |
| 3998 | if (DS.getTypeQualifiers() & DeclSpec::TQ_const) |
| 3999 | Diag(DS.getConstSpecLoc(), diag::ext_anonymous_struct_union_qualified) |
| Richard Smith | 8e1ac33 | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 4000 | << Record->isUnion() << "const" |
| Douglas Gregor | 0f8bc97 | 2011-05-09 23:05:33 +0000 | [diff] [blame] | 4001 | << FixItHint::CreateRemoval(DS.getConstSpecLoc()); |
| 4002 | if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile) |
| Richard Smith | 8e1ac33 | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 4003 | Diag(DS.getVolatileSpecLoc(), |
| David Blaikie | 30d1544 | 2011-10-19 22:56:21 +0000 | [diff] [blame] | 4004 | diag::ext_anonymous_struct_union_qualified) |
| Richard Smith | 8e1ac33 | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 4005 | << Record->isUnion() << "volatile" |
| Douglas Gregor | 0f8bc97 | 2011-05-09 23:05:33 +0000 | [diff] [blame] | 4006 | << FixItHint::CreateRemoval(DS.getVolatileSpecLoc()); |
| 4007 | if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict) |
| Richard Smith | 8e1ac33 | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 4008 | Diag(DS.getRestrictSpecLoc(), |
| David Blaikie | 30d1544 | 2011-10-19 22:56:21 +0000 | [diff] [blame] | 4009 | diag::ext_anonymous_struct_union_qualified) |
| Richard Smith | 8e1ac33 | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 4010 | << Record->isUnion() << "restrict" |
| Douglas Gregor | 0f8bc97 | 2011-05-09 23:05:33 +0000 | [diff] [blame] | 4011 | << FixItHint::CreateRemoval(DS.getRestrictSpecLoc()); |
| Richard Smith | 8e1ac33 | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 4012 | if (DS.getTypeQualifiers() & DeclSpec::TQ_atomic) |
| 4013 | Diag(DS.getAtomicSpecLoc(), |
| 4014 | diag::ext_anonymous_struct_union_qualified) |
| 4015 | << Record->isUnion() << "_Atomic" |
| 4016 | << FixItHint::CreateRemoval(DS.getAtomicSpecLoc()); |
| Douglas Gregor | 0f8bc97 | 2011-05-09 23:05:33 +0000 | [diff] [blame] | 4017 | |
| 4018 | DS.ClearTypeQualifiers(); |
| 4019 | } |
| 4020 | |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4021 | // C++ [class.union]p2: |
| Douglas Gregor | f4d3327 | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 4022 | // The member-specification of an anonymous union shall only |
| 4023 | // define non-static data members. [Note: nested types and |
| 4024 | // functions cannot be declared within an anonymous union. ] |
| Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 4025 | for (auto *Mem : Record->decls()) { |
| 4026 | if (auto *FD = dyn_cast<FieldDecl>(Mem)) { |
| Douglas Gregor | f4d3327 | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 4027 | // C++ [class.union]p3: |
| 4028 | // An anonymous union shall not have private or protected |
| 4029 | // members (clause 11). |
| John McCall | b54367d | 2010-05-21 20:45:30 +0000 | [diff] [blame] | 4030 | assert(FD->getAccess() != AS_none); |
| 4031 | if (FD->getAccess() != AS_public) { |
| Douglas Gregor | f4d3327 | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 4032 | Diag(FD->getLocation(), diag::err_anonymous_record_nonpublic_member) |
| 4033 | << (int)Record->isUnion() << (int)(FD->getAccess() == AS_protected); |
| 4034 | Invalid = true; |
| 4035 | } |
| Argyrios Kyrtzidis | 33aee39 | 2010-08-16 17:27:08 +0000 | [diff] [blame] | 4036 | |
| Alexis Hunt | 97ab554 | 2011-05-16 22:41:40 +0000 | [diff] [blame] | 4037 | // C++ [class.union]p1 |
| 4038 | // An object of a class with a non-trivial constructor, a non-trivial |
| 4039 | // copy constructor, a non-trivial destructor, or a non-trivial copy |
| 4040 | // assignment operator cannot be a member of a union, nor can an |
| 4041 | // array of such objects. |
| Richard Smith | f720df0 | 2011-10-19 20:41:51 +0000 | [diff] [blame] | 4042 | if (CheckNontrivialField(FD)) |
| Argyrios Kyrtzidis | 33aee39 | 2010-08-16 17:27:08 +0000 | [diff] [blame] | 4043 | Invalid = true; |
| Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 4044 | } else if (Mem->isImplicit()) { |
| Douglas Gregor | f4d3327 | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 4045 | // Any implicit members are fine. |
| Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 4046 | } else if (isa<TagDecl>(Mem) && Mem->getDeclContext() != Record) { |
| Douglas Gregor | 8761da5 | 2009-02-03 00:34:39 +0000 | [diff] [blame] | 4047 | // This is a type that showed up in an |
| 4048 | // elaborated-type-specifier inside the anonymous struct or |
| 4049 | // union, but which actually declares a type outside of the |
| 4050 | // anonymous struct or union. It's okay. |
| Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 4051 | } else if (auto *MemRecord = dyn_cast<RecordDecl>(Mem)) { |
| Douglas Gregor | f4d3327 | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 4052 | if (!MemRecord->isAnonymousStructOrUnion() && |
| 4053 | MemRecord->getDeclName()) { |
| Francois Pichet | 4ad4b58 | 2010-09-08 11:32:25 +0000 | [diff] [blame] | 4054 | // Visual C++ allows type definition in anonymous struct or union. |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4055 | if (getLangOpts().MicrosoftExt) |
| Francois Pichet | 4ad4b58 | 2010-09-08 11:32:25 +0000 | [diff] [blame] | 4056 | Diag(MemRecord->getLocation(), diag::ext_anonymous_record_with_type) |
| 4057 | << (int)Record->isUnion(); |
| 4058 | else { |
| 4059 | // This is a nested type declaration. |
| 4060 | Diag(MemRecord->getLocation(), diag::err_anonymous_record_with_type) |
| 4061 | << (int)Record->isUnion(); |
| 4062 | Invalid = true; |
| 4063 | } |
| Richard Smith | 254d266 | 2013-01-28 00:54:05 +0000 | [diff] [blame] | 4064 | } else { |
| 4065 | // This is an anonymous type definition within another anonymous type. |
| 4066 | // This is a popular extension, provided by Plan9, MSVC and GCC, but |
| 4067 | // not part of standard C++. |
| 4068 | Diag(MemRecord->getLocation(), |
| Richard Smith | d202924 | 2013-01-31 03:11:12 +0000 | [diff] [blame] | 4069 | diag::ext_anonymous_record_with_anonymous_type) |
| 4070 | << (int)Record->isUnion(); |
| Douglas Gregor | f4d3327 | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 4071 | } |
| Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 4072 | } else if (isa<AccessSpecDecl>(Mem)) { |
| Abramo Bagnara | d734058 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 4073 | // Any access specifier is fine. |
| Aaron Ballman | f93ef4e | 2014-06-24 16:22:41 +0000 | [diff] [blame] | 4074 | } else if (isa<StaticAssertDecl>(Mem)) { |
| 4075 | // In C++1z, static_assert declarations are also fine. |
| Douglas Gregor | f4d3327 | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 4076 | } else { |
| 4077 | // We have something that isn't a non-static data |
| 4078 | // member. Complain about it. |
| 4079 | unsigned DK = diag::err_anonymous_record_bad_member; |
| Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 4080 | if (isa<TypeDecl>(Mem)) |
| Douglas Gregor | f4d3327 | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 4081 | DK = diag::err_anonymous_record_with_type; |
| Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 4082 | else if (isa<FunctionDecl>(Mem)) |
| Douglas Gregor | f4d3327 | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 4083 | DK = diag::err_anonymous_record_with_function; |
| Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 4084 | else if (isa<VarDecl>(Mem)) |
| Douglas Gregor | f4d3327 | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 4085 | DK = diag::err_anonymous_record_with_static; |
| Francois Pichet | 4ad4b58 | 2010-09-08 11:32:25 +0000 | [diff] [blame] | 4086 | |
| 4087 | // Visual C++ allows type definition in anonymous struct or union. |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4088 | if (getLangOpts().MicrosoftExt && |
| Francois Pichet | 4ad4b58 | 2010-09-08 11:32:25 +0000 | [diff] [blame] | 4089 | DK == diag::err_anonymous_record_with_type) |
| Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 4090 | Diag(Mem->getLocation(), diag::ext_anonymous_record_with_type) |
| Douglas Gregor | f4d3327 | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 4091 | << (int)Record->isUnion(); |
| Francois Pichet | 4ad4b58 | 2010-09-08 11:32:25 +0000 | [diff] [blame] | 4092 | else { |
| Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 4093 | Diag(Mem->getLocation(), DK) |
| Francois Pichet | 4ad4b58 | 2010-09-08 11:32:25 +0000 | [diff] [blame] | 4094 | << (int)Record->isUnion(); |
| Douglas Gregor | f4d3327 | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 4095 | Invalid = true; |
| Francois Pichet | 4ad4b58 | 2010-09-08 11:32:25 +0000 | [diff] [blame] | 4096 | } |
| Douglas Gregor | f4d3327 | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 4097 | } |
| 4098 | } |
| Richard Smith | ab44d5b | 2013-12-10 08:25:00 +0000 | [diff] [blame] | 4099 | |
| 4100 | // C++11 [class.union]p8 (DR1460): |
| 4101 | // At most one variant member of a union may have a |
| 4102 | // brace-or-equal-initializer. |
| 4103 | if (cast<CXXRecordDecl>(Record)->hasInClassInitializer() && |
| 4104 | Owner->isRecord()) |
| 4105 | checkDuplicateDefaultInit(*this, cast<CXXRecordDecl>(Owner), |
| 4106 | cast<CXXRecordDecl>(Record)); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4107 | } |
| Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 4108 | |
| 4109 | if (!Record->isUnion() && !Owner->isRecord()) { |
| Douglas Gregor | c6f58fe | 2009-01-12 22:49:06 +0000 | [diff] [blame] | 4110 | Diag(Record->getLocation(), diag::err_anonymous_struct_not_member) |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4111 | << (int)getLangOpts().CPlusPlus; |
| Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 4112 | Invalid = true; |
| 4113 | } |
| 4114 | |
| John McCall | fa2d692 | 2009-10-22 23:31:08 +0000 | [diff] [blame] | 4115 | // Mock up a declarator. |
| Argyrios Kyrtzidis | 7baa0af | 2011-06-28 03:01:18 +0000 | [diff] [blame] | 4116 | Declarator Dc(DS, Declarator::MemberContext); |
| John McCall | 8cb7bdf | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 4117 | TypeSourceInfo *TInfo = GetTypeForDeclarator(Dc, S); |
| John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 4118 | assert(TInfo && "couldn't build declarator info for anonymous struct/union"); |
| John McCall | fa2d692 | 2009-10-22 23:31:08 +0000 | [diff] [blame] | 4119 | |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4120 | // Create a declaration for this anonymous struct/union. |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4121 | NamedDecl *Anon = nullptr; |
| Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 4122 | if (RecordDecl *OwningClass = dyn_cast<RecordDecl>(Owner)) { |
| Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4123 | Anon = FieldDecl::Create(Context, OwningClass, |
| Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4124 | DS.getLocStart(), |
| Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4125 | Record->getLocation(), |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4126 | /*IdentifierInfo=*/nullptr, |
| Argyrios Kyrtzidis | 6032ef1 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 4127 | Context.getTypeDeclType(Record), |
| John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 4128 | TInfo, |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4129 | /*BitWidth=*/nullptr, /*Mutable=*/false, |
| Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 4130 | /*InitStyle=*/ICIS_NoInit); |
| John McCall | b54367d | 2010-05-21 20:45:30 +0000 | [diff] [blame] | 4131 | Anon->setAccess(AS); |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4132 | if (getLangOpts().CPlusPlus) |
| Douglas Gregor | f4d3327 | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 4133 | FieldCollector->Add(cast<FieldDecl>(Anon)); |
| Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 4134 | } else { |
| Douglas Gregor | c4df407 | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 4135 | DeclSpec::SCS SCSpec = DS.getStorageClassSpec(); |
| Craig Topper | c79e5e3 | 2014-10-31 06:57:13 +0000 | [diff] [blame] | 4136 | StorageClass SC = StorageClassSpecToVarDeclStorageClass(DS); |
| Douglas Gregor | c4df407 | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 4137 | if (SCSpec == DeclSpec::SCS_mutable) { |
| Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 4138 | // mutable can only appear on non-static class members, so it's always |
| 4139 | // an error here |
| 4140 | Diag(Record->getLocation(), diag::err_mutable_nonmember); |
| 4141 | Invalid = true; |
| John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 4142 | SC = SC_None; |
| Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 4143 | } |
| 4144 | |
| Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4145 | Anon = VarDecl::Create(Context, Owner, |
| Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4146 | DS.getLocStart(), |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4147 | Record->getLocation(), /*IdentifierInfo=*/nullptr, |
| Argyrios Kyrtzidis | 6032ef1 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 4148 | Context.getTypeDeclType(Record), |
| Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 4149 | TInfo, SC); |
| Richard Smith | 4037235 | 2011-09-18 00:06:34 +0000 | [diff] [blame] | 4150 | |
| 4151 | // Default-initialize the implicit variable. This initialization will be |
| 4152 | // trivial in almost all cases, except if a union member has an in-class |
| 4153 | // initializer: |
| 4154 | // union { int n = 0; }; |
| 4155 | ActOnUninitializedDecl(Anon, /*TypeMayContainAuto=*/false); |
| Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 4156 | } |
| Douglas Gregor | f4d3327 | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 4157 | Anon->setImplicit(); |
| Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 4158 | |
| Richard Smith | ab44d5b | 2013-12-10 08:25:00 +0000 | [diff] [blame] | 4159 | // Mark this as an anonymous struct/union type. |
| 4160 | Record->setAnonymousStructOrUnion(true); |
| 4161 | |
| Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 4162 | // Add the anonymous struct/union object to the current |
| 4163 | // context. We'll be referencing this object when we refer to one of |
| 4164 | // its members. |
| Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4165 | Owner->addDecl(Anon); |
| Richard Smith | ab44d5b | 2013-12-10 08:25:00 +0000 | [diff] [blame] | 4166 | |
| Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 4167 | // Inject the members of the anonymous struct/union into the owning |
| 4168 | // context and into the identifier resolver chain for name lookup |
| 4169 | // purposes. |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4170 | SmallVector<NamedDecl*, 2> Chain; |
| Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 4171 | Chain.push_back(Anon); |
| 4172 | |
| Francois Pichet | 0c71f6c | 2010-11-23 06:07:27 +0000 | [diff] [blame] | 4173 | if (InjectAnonymousStructOrUnionMembers(*this, S, Owner, Record, AS, |
| 4174 | Chain, false)) |
| Douglas Gregor | c6f58fe | 2009-01-12 22:49:06 +0000 | [diff] [blame] | 4175 | Invalid = true; |
| Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 4176 | |
| David Majnemer | 2206bf5 | 2014-03-05 08:57:59 +0000 | [diff] [blame] | 4177 | if (VarDecl *NewVD = dyn_cast<VarDecl>(Anon)) { |
| 4178 | if (getLangOpts().CPlusPlus && NewVD->isStaticLocal()) { |
| 4179 | Decl *ManglingContextDecl; |
| David Majnemer | a7f8c46 | 2015-03-19 21:54:30 +0000 | [diff] [blame] | 4180 | if (MangleNumberingContext *MCtx = getCurrentMangleNumberContext( |
| 4181 | NewVD->getDeclContext(), ManglingContextDecl)) { |
| 4182 | Context.setManglingNumber( |
| 4183 | NewVD, MCtx->getManglingNumber( |
| 4184 | NewVD, getMSManglingNumber(getLangOpts(), S))); |
| David Majnemer | 2206bf5 | 2014-03-05 08:57:59 +0000 | [diff] [blame] | 4185 | Context.setStaticLocalNumber(NewVD, MCtx->getStaticLocalNumber(NewVD)); |
| 4186 | } |
| 4187 | } |
| 4188 | } |
| 4189 | |
| Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 4190 | if (Invalid) |
| 4191 | Anon->setInvalidDecl(); |
| 4192 | |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4193 | return Anon; |
| Chris Lattner | b6738ec | 2007-01-28 00:38:24 +0000 | [diff] [blame] | 4194 | } |
| 4195 | |
| Francois Pichet | 0c71f6c | 2010-11-23 06:07:27 +0000 | [diff] [blame] | 4196 | /// BuildMicrosoftCAnonymousStruct - Handle the declaration of an |
| 4197 | /// Microsoft C anonymous structure. |
| 4198 | /// Ref: http://msdn.microsoft.com/en-us/library/z2cx9y4f.aspx |
| 4199 | /// Example: |
| 4200 | /// |
| 4201 | /// struct A { int a; }; |
| 4202 | /// struct B { struct A; int b; }; |
| 4203 | /// |
| 4204 | /// void foo() { |
| 4205 | /// B var; |
| David Majnemer | 8f0ed91 | 2014-08-11 07:29:54 +0000 | [diff] [blame] | 4206 | /// var.a = 3; |
| Francois Pichet | 0c71f6c | 2010-11-23 06:07:27 +0000 | [diff] [blame] | 4207 | /// } |
| 4208 | /// |
| 4209 | Decl *Sema::BuildMicrosoftCAnonymousStruct(Scope *S, DeclSpec &DS, |
| 4210 | RecordDecl *Record) { |
| David Majnemer | 8f0ed91 | 2014-08-11 07:29:54 +0000 | [diff] [blame] | 4211 | assert(Record && "expected a record!"); |
| Francois Pichet | 0c71f6c | 2010-11-23 06:07:27 +0000 | [diff] [blame] | 4212 | |
| 4213 | // Mock up a declarator. |
| 4214 | Declarator Dc(DS, Declarator::TypeNameContext); |
| 4215 | TypeSourceInfo *TInfo = GetTypeForDeclarator(Dc, S); |
| 4216 | assert(TInfo && "couldn't build declarator info for anonymous struct"); |
| 4217 | |
| David Majnemer | d8e366b | 2014-09-18 00:42:05 +0000 | [diff] [blame] | 4218 | auto *ParentDecl = cast<RecordDecl>(CurContext); |
| 4219 | QualType RecTy = Context.getTypeDeclType(Record); |
| 4220 | |
| Francois Pichet | 0c71f6c | 2010-11-23 06:07:27 +0000 | [diff] [blame] | 4221 | // Create a declaration for this anonymous struct. |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4222 | NamedDecl *Anon = FieldDecl::Create(Context, |
| David Majnemer | d8e366b | 2014-09-18 00:42:05 +0000 | [diff] [blame] | 4223 | ParentDecl, |
| Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4224 | DS.getLocStart(), |
| 4225 | DS.getLocStart(), |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4226 | /*IdentifierInfo=*/nullptr, |
| David Majnemer | d8e366b | 2014-09-18 00:42:05 +0000 | [diff] [blame] | 4227 | RecTy, |
| Francois Pichet | 0c71f6c | 2010-11-23 06:07:27 +0000 | [diff] [blame] | 4228 | TInfo, |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4229 | /*BitWidth=*/nullptr, /*Mutable=*/false, |
| Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 4230 | /*InitStyle=*/ICIS_NoInit); |
| Francois Pichet | 0c71f6c | 2010-11-23 06:07:27 +0000 | [diff] [blame] | 4231 | Anon->setImplicit(); |
| 4232 | |
| 4233 | // Add the anonymous struct object to the current context. |
| 4234 | CurContext->addDecl(Anon); |
| 4235 | |
| 4236 | // Inject the members of the anonymous struct into the current |
| 4237 | // context and into the identifier resolver chain for name lookup |
| 4238 | // purposes. |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4239 | SmallVector<NamedDecl*, 2> Chain; |
| Francois Pichet | 0c71f6c | 2010-11-23 06:07:27 +0000 | [diff] [blame] | 4240 | Chain.push_back(Anon); |
| 4241 | |
| Nico Weber | f8bb3de | 2012-02-01 00:41:00 +0000 | [diff] [blame] | 4242 | RecordDecl *RecordDef = Record->getDefinition(); |
| David Majnemer | d8e366b | 2014-09-18 00:42:05 +0000 | [diff] [blame] | 4243 | if (RequireCompleteType(Anon->getLocation(), RecTy, |
| 4244 | diag::err_field_incomplete) || |
| 4245 | InjectAnonymousStructOrUnionMembers(*this, S, CurContext, RecordDef, |
| 4246 | AS_none, Chain, true)) { |
| Francois Pichet | 0c71f6c | 2010-11-23 06:07:27 +0000 | [diff] [blame] | 4247 | Anon->setInvalidDecl(); |
| David Majnemer | d8e366b | 2014-09-18 00:42:05 +0000 | [diff] [blame] | 4248 | ParentDecl->setInvalidDecl(); |
| 4249 | } |
| Francois Pichet | 0c71f6c | 2010-11-23 06:07:27 +0000 | [diff] [blame] | 4250 | |
| 4251 | return Anon; |
| 4252 | } |
| Steve Naroff | 2fea139 | 2007-09-02 02:04:30 +0000 | [diff] [blame] | 4253 | |
| Douglas Gregor | 92751d4 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 4254 | /// GetNameForDeclarator - Determine the full declaration name for the |
| 4255 | /// given Declarator. |
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4256 | DeclarationNameInfo Sema::GetNameForDeclarator(Declarator &D) { |
| Douglas Gregor | a121b75 | 2009-11-03 16:56:39 +0000 | [diff] [blame] | 4257 | return GetNameFromUnqualifiedId(D.getName()); |
| 4258 | } |
| 4259 | |
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4260 | /// \brief Retrieves the declaration name from a parsed unqualified-id. |
| 4261 | DeclarationNameInfo |
| 4262 | Sema::GetNameFromUnqualifiedId(const UnqualifiedId &Name) { |
| 4263 | DeclarationNameInfo NameInfo; |
| 4264 | NameInfo.setLoc(Name.StartLocation); |
| 4265 | |
| Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4266 | switch (Name.getKind()) { |
| Alexis Hunt | 3445850 | 2009-11-28 04:44:28 +0000 | [diff] [blame] | 4267 | |
| Fariborz Jahanian | 7f4427f | 2011-07-12 17:16:56 +0000 | [diff] [blame] | 4268 | case UnqualifiedId::IK_ImplicitSelfParam: |
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4269 | case UnqualifiedId::IK_Identifier: |
| 4270 | NameInfo.setName(Name.Identifier); |
| 4271 | NameInfo.setLoc(Name.StartLocation); |
| 4272 | return NameInfo; |
| Alexis Hunt | 3445850 | 2009-11-28 04:44:28 +0000 | [diff] [blame] | 4273 | |
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4274 | case UnqualifiedId::IK_OperatorFunctionId: |
| 4275 | NameInfo.setName(Context.DeclarationNames.getCXXOperatorName( |
| 4276 | Name.OperatorFunctionId.Operator)); |
| 4277 | NameInfo.setLoc(Name.StartLocation); |
| 4278 | NameInfo.getInfo().CXXOperatorName.BeginOpNameLoc |
| 4279 | = Name.OperatorFunctionId.SymbolLocations[0]; |
| 4280 | NameInfo.getInfo().CXXOperatorName.EndOpNameLoc |
| 4281 | = Name.EndLocation.getRawEncoding(); |
| 4282 | return NameInfo; |
| Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4283 | |
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4284 | case UnqualifiedId::IK_LiteralOperatorId: |
| 4285 | NameInfo.setName(Context.DeclarationNames.getCXXLiteralOperatorName( |
| 4286 | Name.Identifier)); |
| 4287 | NameInfo.setLoc(Name.StartLocation); |
| 4288 | NameInfo.setCXXLiteralOperatorNameLoc(Name.EndLocation); |
| 4289 | return NameInfo; |
| Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4290 | |
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4291 | case UnqualifiedId::IK_ConversionFunctionId: { |
| 4292 | TypeSourceInfo *TInfo; |
| 4293 | QualType Ty = GetTypeFromParser(Name.ConversionFunctionId, &TInfo); |
| 4294 | if (Ty.isNull()) |
| 4295 | return DeclarationNameInfo(); |
| 4296 | NameInfo.setName(Context.DeclarationNames.getCXXConversionFunctionName( |
| 4297 | Context.getCanonicalType(Ty))); |
| 4298 | NameInfo.setLoc(Name.StartLocation); |
| 4299 | NameInfo.setNamedTypeInfo(TInfo); |
| 4300 | return NameInfo; |
| Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 4301 | } |
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4302 | |
| 4303 | case UnqualifiedId::IK_ConstructorName: { |
| 4304 | TypeSourceInfo *TInfo; |
| 4305 | QualType Ty = GetTypeFromParser(Name.ConstructorName, &TInfo); |
| 4306 | if (Ty.isNull()) |
| 4307 | return DeclarationNameInfo(); |
| 4308 | NameInfo.setName(Context.DeclarationNames.getCXXConstructorName( |
| 4309 | Context.getCanonicalType(Ty))); |
| 4310 | NameInfo.setLoc(Name.StartLocation); |
| 4311 | NameInfo.setNamedTypeInfo(TInfo); |
| 4312 | return NameInfo; |
| 4313 | } |
| 4314 | |
| 4315 | case UnqualifiedId::IK_ConstructorTemplateId: { |
| 4316 | // In well-formed code, we can only have a constructor |
| 4317 | // template-id that refers to the current context, so go there |
| 4318 | // to find the actual type being constructed. |
| 4319 | CXXRecordDecl *CurClass = dyn_cast<CXXRecordDecl>(CurContext); |
| 4320 | if (!CurClass || CurClass->getIdentifier() != Name.TemplateId->Name) |
| 4321 | return DeclarationNameInfo(); |
| 4322 | |
| 4323 | // Determine the type of the class being constructed. |
| 4324 | QualType CurClassType = Context.getTypeDeclType(CurClass); |
| 4325 | |
| 4326 | // FIXME: Check two things: that the template-id names the same type as |
| 4327 | // CurClassType, and that the template-id does not occur when the name |
| 4328 | // was qualified. |
| 4329 | |
| 4330 | NameInfo.setName(Context.DeclarationNames.getCXXConstructorName( |
| 4331 | Context.getCanonicalType(CurClassType))); |
| 4332 | NameInfo.setLoc(Name.StartLocation); |
| 4333 | // FIXME: should we retrieve TypeSourceInfo? |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4334 | NameInfo.setNamedTypeInfo(nullptr); |
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4335 | return NameInfo; |
| 4336 | } |
| 4337 | |
| 4338 | case UnqualifiedId::IK_DestructorName: { |
| 4339 | TypeSourceInfo *TInfo; |
| 4340 | QualType Ty = GetTypeFromParser(Name.DestructorName, &TInfo); |
| 4341 | if (Ty.isNull()) |
| 4342 | return DeclarationNameInfo(); |
| 4343 | NameInfo.setName(Context.DeclarationNames.getCXXDestructorName( |
| 4344 | Context.getCanonicalType(Ty))); |
| 4345 | NameInfo.setLoc(Name.StartLocation); |
| 4346 | NameInfo.setNamedTypeInfo(TInfo); |
| 4347 | return NameInfo; |
| 4348 | } |
| 4349 | |
| 4350 | case UnqualifiedId::IK_TemplateId: { |
| John McCall | 3e56fd4 | 2010-08-23 07:28:44 +0000 | [diff] [blame] | 4351 | TemplateName TName = Name.TemplateId->Template.get(); |
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4352 | SourceLocation TNameLoc = Name.TemplateId->TemplateNameLoc; |
| 4353 | return Context.getNameForTemplate(TName, TNameLoc); |
| 4354 | } |
| 4355 | |
| 4356 | } // switch (Name.getKind()) |
| 4357 | |
| David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4358 | llvm_unreachable("Unknown name kind"); |
| Douglas Gregor | 92751d4 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 4359 | } |
| 4360 | |
| Kaelyn Uhrain | 7d9bc63 | 2011-08-04 17:40:00 +0000 | [diff] [blame] | 4361 | static QualType getCoreType(QualType Ty) { |
| 4362 | do { |
| 4363 | if (Ty->isPointerType() || Ty->isReferenceType()) |
| 4364 | Ty = Ty->getPointeeType(); |
| 4365 | else if (Ty->isArrayType()) |
| 4366 | Ty = Ty->castAsArrayTypeUnsafe()->getElementType(); |
| 4367 | else |
| 4368 | return Ty.withoutLocalFastQualifiers(); |
| 4369 | } while (true); |
| 4370 | } |
| 4371 | |
| Kaelyn Uhrain | 0a32fca | 2011-10-11 00:28:39 +0000 | [diff] [blame] | 4372 | /// hasSimilarParameters - Determine whether the C++ functions Declaration |
| 4373 | /// and Definition have "nearly" matching parameters. This heuristic is |
| 4374 | /// used to improve diagnostics in the case where an out-of-line function |
| 4375 | /// definition doesn't match any declaration within the class or namespace. |
| 4376 | /// Also sets Params to the list of indices to the parameters that differ |
| 4377 | /// between the declaration and the definition. If hasSimilarParameters |
| 4378 | /// returns true and Params is empty, then all of the parameters match. |
| 4379 | static bool hasSimilarParameters(ASTContext &Context, |
| Douglas Gregor | 8af63e4 | 2009-02-06 17:46:57 +0000 | [diff] [blame] | 4380 | FunctionDecl *Declaration, |
| Kaelyn Uhrain | 7d9bc63 | 2011-08-04 17:40:00 +0000 | [diff] [blame] | 4381 | FunctionDecl *Definition, |
| Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 4382 | SmallVectorImpl<unsigned> &Params) { |
| Kaelyn Uhrain | 7d9bc63 | 2011-08-04 17:40:00 +0000 | [diff] [blame] | 4383 | Params.clear(); |
| Douglas Gregor | ad59050 | 2008-12-15 23:53:10 +0000 | [diff] [blame] | 4384 | if (Declaration->param_size() != Definition->param_size()) |
| 4385 | return false; |
| 4386 | for (unsigned Idx = 0; Idx < Declaration->param_size(); ++Idx) { |
| 4387 | QualType DeclParamTy = Declaration->getParamDecl(Idx)->getType(); |
| 4388 | QualType DefParamTy = Definition->getParamDecl(Idx)->getType(); |
| 4389 | |
| Kaelyn Uhrain | 7d9bc63 | 2011-08-04 17:40:00 +0000 | [diff] [blame] | 4390 | // The parameter types are identical |
| Matt Beaumont-Gay | 56381b8 | 2011-08-23 01:35:51 +0000 | [diff] [blame] | 4391 | if (Context.hasSameType(DefParamTy, DeclParamTy)) |
| Kaelyn Uhrain | 7d9bc63 | 2011-08-04 17:40:00 +0000 | [diff] [blame] | 4392 | continue; |
| 4393 | |
| 4394 | QualType DeclParamBaseTy = getCoreType(DeclParamTy); |
| 4395 | QualType DefParamBaseTy = getCoreType(DefParamTy); |
| 4396 | const IdentifierInfo *DeclTyName = DeclParamBaseTy.getBaseTypeIdentifier(); |
| 4397 | const IdentifierInfo *DefTyName = DefParamBaseTy.getBaseTypeIdentifier(); |
| 4398 | |
| 4399 | if (Context.hasSameUnqualifiedType(DeclParamBaseTy, DefParamBaseTy) || |
| 4400 | (DeclTyName && DeclTyName == DefTyName)) |
| 4401 | Params.push_back(Idx); |
| 4402 | else // The two parameters aren't even close |
| Douglas Gregor | ad59050 | 2008-12-15 23:53:10 +0000 | [diff] [blame] | 4403 | return false; |
| 4404 | } |
| 4405 | |
| 4406 | return true; |
| 4407 | } |
| 4408 | |
| John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 4409 | /// NeedsRebuildingInCurrentInstantiation - Checks whether the given |
| 4410 | /// declarator needs to be rebuilt in the current instantiation. |
| 4411 | /// Any bits of declarator which appear before the name are valid for |
| 4412 | /// consideration here. That's specifically the type in the decl spec |
| 4413 | /// and the base type in any member-pointer chunks. |
| 4414 | static bool RebuildDeclaratorInCurrentInstantiation(Sema &S, Declarator &D, |
| 4415 | DeclarationName Name) { |
| 4416 | // The types we specifically need to rebuild are: |
| 4417 | // - typenames, typeofs, and decltypes |
| 4418 | // - types which will become injected class names |
| 4419 | // Of course, we also need to rebuild any type referencing such a |
| 4420 | // type. It's safest to just say "dependent", but we call out a |
| 4421 | // few cases here. |
| 4422 | |
| 4423 | DeclSpec &DS = D.getMutableDeclSpec(); |
| 4424 | switch (DS.getTypeSpecType()) { |
| 4425 | case DeclSpec::TST_typename: |
| 4426 | case DeclSpec::TST_typeofType: |
| Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4427 | case DeclSpec::TST_underlyingType: |
| 4428 | case DeclSpec::TST_atomic: { |
| John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 4429 | // Grab the type from the parser. |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4430 | TypeSourceInfo *TSI = nullptr; |
| John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4431 | QualType T = S.GetTypeFromParser(DS.getRepAsType(), &TSI); |
| John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 4432 | if (T.isNull() || !T->isDependentType()) break; |
| 4433 | |
| 4434 | // Make sure there's a type source info. This isn't really much |
| 4435 | // of a waste; most dependent types should have type source info |
| 4436 | // attached already. |
| 4437 | if (!TSI) |
| 4438 | TSI = S.Context.getTrivialTypeSourceInfo(T, DS.getTypeSpecTypeLoc()); |
| 4439 | |
| 4440 | // Rebuild the type in the current instantiation. |
| 4441 | TSI = S.RebuildTypeInCurrentInstantiation(TSI, D.getIdentifierLoc(), Name); |
| 4442 | if (!TSI) return true; |
| 4443 | |
| 4444 | // Store the new type back in the decl spec. |
| John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4445 | ParsedType LocType = S.CreateParsedType(TSI->getType(), TSI); |
| 4446 | DS.UpdateTypeRep(LocType); |
| 4447 | break; |
| 4448 | } |
| 4449 | |
| Richard Smith | 1620ebd | 2012-10-01 20:35:07 +0000 | [diff] [blame] | 4450 | case DeclSpec::TST_decltype: |
| John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4451 | case DeclSpec::TST_typeofExpr: { |
| 4452 | Expr *E = DS.getRepAsExpr(); |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4453 | ExprResult Result = S.RebuildExprInCurrentInstantiation(E); |
| John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4454 | if (Result.isInvalid()) return true; |
| 4455 | DS.UpdateExprRep(Result.get()); |
| John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 4456 | break; |
| 4457 | } |
| 4458 | |
| 4459 | default: |
| 4460 | // Nothing to do for these decl specs. |
| 4461 | break; |
| 4462 | } |
| 4463 | |
| 4464 | // It doesn't matter what order we do this in. |
| 4465 | for (unsigned I = 0, E = D.getNumTypeObjects(); I != E; ++I) { |
| 4466 | DeclaratorChunk &Chunk = D.getTypeObject(I); |
| 4467 | |
| 4468 | // The only type information in the declarator which can come |
| 4469 | // before the declaration name is the base type of a member |
| 4470 | // pointer. |
| 4471 | if (Chunk.Kind != DeclaratorChunk::MemberPointer) |
| 4472 | continue; |
| 4473 | |
| 4474 | // Rebuild the scope specifier in-place. |
| 4475 | CXXScopeSpec &SS = Chunk.Mem.Scope(); |
| 4476 | if (S.RebuildNestedNameSpecifierInCurrentInstantiation(SS)) |
| 4477 | return true; |
| 4478 | } |
| 4479 | |
| 4480 | return false; |
| 4481 | } |
| 4482 | |
| Anders Carlsson | 1052fd7 | 2011-07-04 16:28:17 +0000 | [diff] [blame] | 4483 | Decl *Sema::ActOnDeclarator(Scope *S, Declarator &D) { |
| Douglas Gregor | 5d1b4e3 | 2011-11-07 20:56:01 +0000 | [diff] [blame] | 4484 | D.setFunctionDefinitionKind(FDK_Declaration); |
| Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 4485 | Decl *Dcl = HandleDeclarator(S, D, MultiTemplateParamsArg()); |
| Argyrios Kyrtzidis | 8ad3bab | 2011-11-23 20:27:36 +0000 | [diff] [blame] | 4486 | |
| 4487 | if (OriginalLexicalContext && OriginalLexicalContext->isObjCContainer() && |
| Douglas Gregor | 205b068 | 2012-04-30 18:13:01 +0000 | [diff] [blame] | 4488 | Dcl && Dcl->getDeclContext()->isFileContext()) |
| Argyrios Kyrtzidis | 8ad3bab | 2011-11-23 20:27:36 +0000 | [diff] [blame] | 4489 | Dcl->setTopLevelDeclInObjCContainer(); |
| 4490 | |
| 4491 | return Dcl; |
| John McCall | de6836a | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 4492 | } |
| 4493 | |
| Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4494 | /// DiagnoseClassNameShadow - Implement C++ [class.mem]p13: |
| 4495 | /// If T is the name of a class, then each of the following shall have a |
| 4496 | /// name different from T: |
| 4497 | /// - every static data member of class T; |
| 4498 | /// - every member function of class T |
| 4499 | /// - every member of class T that is itself a type; |
| 4500 | /// \returns true if the declaration name violates these rules. |
| 4501 | bool Sema::DiagnoseClassNameShadow(DeclContext *DC, |
| 4502 | DeclarationNameInfo NameInfo) { |
| 4503 | DeclarationName Name = NameInfo.getName(); |
| 4504 | |
| 4505 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(DC)) |
| 4506 | if (Record->getIdentifier() && Record->getDeclName() == Name) { |
| 4507 | Diag(NameInfo.getLoc(), diag::err_member_name_of_class) << Name; |
| 4508 | return true; |
| 4509 | } |
| 4510 | |
| 4511 | return false; |
| 4512 | } |
| Douglas Gregor | 31feb33 | 2012-03-17 23:06:31 +0000 | [diff] [blame] | 4513 | |
| Douglas Gregor | b7d17dd | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 4514 | /// \brief Diagnose a declaration whose declarator-id has the given |
| 4515 | /// nested-name-specifier. |
| 4516 | /// |
| 4517 | /// \param SS The nested-name-specifier of the declarator-id. |
| 4518 | /// |
| 4519 | /// \param DC The declaration context to which the nested-name-specifier |
| 4520 | /// resolves. |
| 4521 | /// |
| 4522 | /// \param Name The name of the entity being declared. |
| 4523 | /// |
| 4524 | /// \param Loc The location of the name of the entity being declared. |
| Douglas Gregor | 31feb33 | 2012-03-17 23:06:31 +0000 | [diff] [blame] | 4525 | /// |
| 4526 | /// \returns true if we cannot safely recover from this error, false otherwise. |
| Douglas Gregor | b7d17dd | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 4527 | bool Sema::diagnoseQualifiedDeclaration(CXXScopeSpec &SS, DeclContext *DC, |
| Douglas Gregor | 31feb33 | 2012-03-17 23:06:31 +0000 | [diff] [blame] | 4528 | DeclarationName Name, |
| Richard Smith | a230224 | 2013-12-05 07:51:02 +0000 | [diff] [blame] | 4529 | SourceLocation Loc) { |
| Douglas Gregor | b7d17dd | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 4530 | DeclContext *Cur = CurContext; |
| Eli Friedman | e2358c1 | 2013-08-12 21:54:01 +0000 | [diff] [blame] | 4531 | while (isa<LinkageSpecDecl>(Cur) || isa<CapturedDecl>(Cur)) |
| Douglas Gregor | b7d17dd | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 4532 | Cur = Cur->getParent(); |
| Richard Smith | a230224 | 2013-12-05 07:51:02 +0000 | [diff] [blame] | 4533 | |
| 4534 | // If the user provided a superfluous scope specifier that refers back to the |
| 4535 | // class in which the entity is already declared, diagnose and ignore it. |
| Douglas Gregor | 31feb33 | 2012-03-17 23:06:31 +0000 | [diff] [blame] | 4536 | // |
| 4537 | // class X { |
| 4538 | // void X::f(); |
| 4539 | // }; |
| Richard Smith | a230224 | 2013-12-05 07:51:02 +0000 | [diff] [blame] | 4540 | // |
| 4541 | // Note, it was once ill-formed to give redundant qualification in all |
| 4542 | // contexts, but that rule was removed by DR482. |
| Douglas Gregor | b7d17dd | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 4543 | if (Cur->Equals(DC)) { |
| Richard Smith | a230224 | 2013-12-05 07:51:02 +0000 | [diff] [blame] | 4544 | if (Cur->isRecord()) { |
| 4545 | Diag(Loc, LangOpts.MicrosoftExt ? diag::warn_member_extra_qualification |
| 4546 | : diag::err_member_extra_qualification) |
| 4547 | << Name << FixItHint::CreateRemoval(SS.getRange()); |
| 4548 | SS.clear(); |
| 4549 | } else { |
| 4550 | Diag(Loc, diag::warn_namespace_member_extra_qualification) << Name; |
| 4551 | } |
| Douglas Gregor | 31feb33 | 2012-03-17 23:06:31 +0000 | [diff] [blame] | 4552 | return false; |
| Richard Smith | a230224 | 2013-12-05 07:51:02 +0000 | [diff] [blame] | 4553 | } |
| Douglas Gregor | b7d17dd | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 4554 | |
| 4555 | // Check whether the qualifying scope encloses the scope of the original |
| 4556 | // declaration. |
| 4557 | if (!Cur->Encloses(DC)) { |
| 4558 | if (Cur->isRecord()) |
| 4559 | Diag(Loc, diag::err_member_qualification) |
| 4560 | << Name << SS.getRange(); |
| 4561 | else if (isa<TranslationUnitDecl>(DC)) |
| 4562 | Diag(Loc, diag::err_invalid_declarator_global_scope) |
| 4563 | << Name << SS.getRange(); |
| 4564 | else if (isa<FunctionDecl>(Cur)) |
| 4565 | Diag(Loc, diag::err_invalid_declarator_in_function) |
| 4566 | << Name << SS.getRange(); |
| Eli Friedman | e2358c1 | 2013-08-12 21:54:01 +0000 | [diff] [blame] | 4567 | else if (isa<BlockDecl>(Cur)) |
| 4568 | Diag(Loc, diag::err_invalid_declarator_in_block) |
| 4569 | << Name << SS.getRange(); |
| Douglas Gregor | b7d17dd | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 4570 | else |
| 4571 | Diag(Loc, diag::err_invalid_declarator_scope) |
| Richard Smith | 8226984 | 2012-04-13 04:07:40 +0000 | [diff] [blame] | 4572 | << Name << cast<NamedDecl>(Cur) << cast<NamedDecl>(DC) << SS.getRange(); |
| Douglas Gregor | b7d17dd | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 4573 | |
| Douglas Gregor | 31feb33 | 2012-03-17 23:06:31 +0000 | [diff] [blame] | 4574 | return true; |
| Douglas Gregor | b7d17dd | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 4575 | } |
| 4576 | |
| 4577 | if (Cur->isRecord()) { |
| 4578 | // Cannot qualify members within a class. |
| 4579 | Diag(Loc, diag::err_member_qualification) |
| 4580 | << Name << SS.getRange(); |
| 4581 | SS.clear(); |
| 4582 | |
| 4583 | // C++ constructors and destructors with incorrect scopes can break |
| 4584 | // our AST invariants by having the wrong underlying types. If |
| 4585 | // that's the case, then drop this declaration entirely. |
| 4586 | if ((Name.getNameKind() == DeclarationName::CXXConstructorName || |
| 4587 | Name.getNameKind() == DeclarationName::CXXDestructorName) && |
| 4588 | !Context.hasSameType(Name.getCXXNameType(), |
| 4589 | Context.getTypeDeclType(cast<CXXRecordDecl>(Cur)))) |
| 4590 | return true; |
| 4591 | |
| 4592 | return false; |
| 4593 | } |
| Douglas Gregor | 31feb33 | 2012-03-17 23:06:31 +0000 | [diff] [blame] | 4594 | |
| Douglas Gregor | b7d17dd | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 4595 | // C++11 [dcl.meaning]p1: |
| 4596 | // [...] "The nested-name-specifier of the qualified declarator-id shall |
| 4597 | // not begin with a decltype-specifer" |
| 4598 | NestedNameSpecifierLoc SpecLoc(SS.getScopeRep(), SS.location_data()); |
| 4599 | while (SpecLoc.getPrefix()) |
| 4600 | SpecLoc = SpecLoc.getPrefix(); |
| 4601 | if (dyn_cast_or_null<DecltypeType>( |
| 4602 | SpecLoc.getNestedNameSpecifier()->getAsType())) |
| 4603 | Diag(Loc, diag::err_decltype_in_declarator) |
| 4604 | << SpecLoc.getTypeLoc().getSourceRange(); |
| 4605 | |
| Douglas Gregor | 31feb33 | 2012-03-17 23:06:31 +0000 | [diff] [blame] | 4606 | return false; |
| 4607 | } |
| 4608 | |
| Rafael Espindola | 0a67e2f | 2013-01-08 20:44:06 +0000 | [diff] [blame] | 4609 | NamedDecl *Sema::HandleDeclarator(Scope *S, Declarator &D, |
| 4610 | MultiTemplateParamsArg TemplateParamLists) { |
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4611 | // TODO: consider using NameInfo for diagnostic. |
| 4612 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); |
| 4613 | DeclarationName Name = NameInfo.getName(); |
| Douglas Gregor | 92751d4 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 4614 | |
| Chris Lattner | 02c0439 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4615 | // All of these full declarators require an identifier. If it doesn't have |
| 4616 | // one, the ParsedFreeStandingDeclSpec action should be used. |
| Douglas Gregor | 92751d4 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 4617 | if (!Name) { |
| Chris Lattner | f6d1c9c | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 4618 | if (!D.isInvalidType()) // Reject this if we think it is valid. |
| Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4619 | Diag(D.getDeclSpec().getLocStart(), |
| Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 4620 | diag::err_declarator_need_ident) |
| 4621 | << D.getDeclSpec().getSourceRange() << D.getSourceRange(); |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4622 | return nullptr; |
| Douglas Gregor | c435653 | 2010-12-16 00:46:58 +0000 | [diff] [blame] | 4623 | } else if (DiagnoseUnexpandedParameterPack(NameInfo, UPPC_DeclarationType)) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4624 | return nullptr; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4625 | |
| Chris Lattner | 1a76a3c | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 4626 | // The scope passed in may not be a decl scope. Zip up the scope tree until |
| 4627 | // we find one that is. |
| Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 4628 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 4629 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| Chris Lattner | 1a76a3c | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 4630 | S = S->getParent(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4631 | |
| John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 4632 | DeclContext *DC = CurContext; |
| 4633 | if (D.getCXXScopeSpec().isInvalid()) |
| 4634 | D.setInvalidType(); |
| 4635 | else if (D.getCXXScopeSpec().isSet()) { |
| Douglas Gregor | 6c110f3 | 2010-12-16 01:14:37 +0000 | [diff] [blame] | 4636 | if (DiagnoseUnexpandedParameterPack(D.getCXXScopeSpec(), |
| 4637 | UPPC_DeclarationQualifier)) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4638 | return nullptr; |
| Douglas Gregor | 6c110f3 | 2010-12-16 01:14:37 +0000 | [diff] [blame] | 4639 | |
| John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 4640 | bool EnteringContext = !D.getDeclSpec().isFriendSpecified(); |
| 4641 | DC = computeDeclContext(D.getCXXScopeSpec(), EnteringContext); |
| Richard Smith | 8ac1c92 | 2013-11-25 21:30:29 +0000 | [diff] [blame] | 4642 | if (!DC || isa<EnumDecl>(DC)) { |
| John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 4643 | // If we could not compute the declaration context, it's because the |
| 4644 | // declaration context is dependent but does not refer to a class, |
| 4645 | // class template, or class template partial specialization. Complain |
| 4646 | // and return early, to avoid the coming semantic disaster. |
| 4647 | Diag(D.getIdentifierLoc(), |
| 4648 | diag::err_template_qualified_declarator_no_match) |
| Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 4649 | << D.getCXXScopeSpec().getScopeRep() |
| John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 4650 | << D.getCXXScopeSpec().getRange(); |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4651 | return nullptr; |
| John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 4652 | } |
| John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 4653 | bool IsDependentContext = DC->isDependentContext(); |
| John McCall | 4d6d613 | 2009-12-19 09:35:56 +0000 | [diff] [blame] | 4654 | |
| John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 4655 | if (!IsDependentContext && |
| John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 4656 | RequireCompleteDeclContext(D.getCXXScopeSpec(), DC)) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4657 | return nullptr; |
| John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 4658 | |
| Douglas Gregor | b7d17dd | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 4659 | if (isa<CXXRecordDecl>(DC) && !cast<CXXRecordDecl>(DC)->hasDefinition()) { |
| 4660 | Diag(D.getIdentifierLoc(), |
| 4661 | diag::err_member_def_undefined_record) |
| 4662 | << Name << DC << D.getCXXScopeSpec().getRange(); |
| 4663 | D.setInvalidType(); |
| 4664 | } else if (!D.getDeclSpec().isFriendSpecified()) { |
| 4665 | if (diagnoseQualifiedDeclaration(D.getCXXScopeSpec(), DC, |
| 4666 | Name, D.getIdentifierLoc())) { |
| 4667 | if (DC->isRecord()) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4668 | return nullptr; |
| 4669 | |
| Douglas Gregor | b7d17dd | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 4670 | D.setInvalidType(); |
| Douglas Gregor | a007d36 | 2010-10-13 22:19:53 +0000 | [diff] [blame] | 4671 | } |
| John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 4672 | } |
| 4673 | |
| 4674 | // Check whether we need to rebuild the type of the given |
| 4675 | // declaration in the current instantiation. |
| 4676 | if (EnteringContext && IsDependentContext && |
| 4677 | TemplateParamLists.size() != 0) { |
| 4678 | ContextRAII SavedContext(*this, DC); |
| 4679 | if (RebuildDeclaratorInCurrentInstantiation(*this, D, Name)) |
| 4680 | D.setInvalidType(); |
| Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 4681 | } |
| 4682 | } |
| Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4683 | |
| 4684 | if (DiagnoseClassNameShadow(DC, NameInfo)) |
| 4685 | // If this is a typedef, we'll end up spewing multiple diagnostics. |
| 4686 | // Just return early; it's safer. |
| 4687 | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4688 | return nullptr; |
| 4689 | |
| John McCall | 8cb7bdf | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 4690 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
| 4691 | QualType R = TInfo->getType(); |
| Douglas Gregor | eddf433 | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 4692 | |
| Douglas Gregor | 506bd56 | 2010-12-13 22:49:22 +0000 | [diff] [blame] | 4693 | if (DiagnoseUnexpandedParameterPack(D.getIdentifierLoc(), TInfo, |
| 4694 | UPPC_DeclarationType)) |
| 4695 | D.setInvalidType(); |
| 4696 | |
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4697 | LookupResult Previous(*this, NameInfo, LookupOrdinaryName, |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4698 | ForRedeclaration); |
| 4699 | |
| Argyrios Kyrtzidis | 16ac9be | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 4700 | // 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] | 4701 | if (!D.getCXXScopeSpec().isSet()) { |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4702 | bool IsLinkageLookup = false; |
| Richard Smith | 1c34fb7 | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 4703 | bool CreateBuiltins = false; |
| Douglas Gregor | eddf433 | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 4704 | |
| 4705 | // If the declaration we're planning to build will be a function |
| 4706 | // or object with linkage, then look for another declaration with |
| 4707 | // linkage (C99 6.2.2p4-5 and C++ [basic.link]p6). |
| Richard Smith | 1c34fb7 | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 4708 | // |
| 4709 | // If the declaration we're planning to build will be declared with |
| 4710 | // external linkage in the translation unit, create any builtin with |
| 4711 | // the same name. |
| Douglas Gregor | eddf433 | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 4712 | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) |
| 4713 | /* Do nothing*/; |
| Richard Smith | 1c34fb7 | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 4714 | else if (CurContext->isFunctionOrMethod() && |
| 4715 | (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_extern || |
| 4716 | R->isFunctionType())) { |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4717 | IsLinkageLookup = true; |
| Richard Smith | 1c34fb7 | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 4718 | CreateBuiltins = |
| 4719 | CurContext->getEnclosingNamespaceContext()->isTranslationUnit(); |
| 4720 | } else if (CurContext->getRedeclContext()->isTranslationUnit() && |
| 4721 | D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_static) |
| 4722 | CreateBuiltins = true; |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4723 | |
| 4724 | if (IsLinkageLookup) |
| 4725 | Previous.clear(LookupRedeclarationWithLinkage); |
| Douglas Gregor | eddf433 | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 4726 | |
| Richard Smith | 1c34fb7 | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 4727 | LookupName(Previous, S, CreateBuiltins); |
| Argyrios Kyrtzidis | 16ac9be | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 4728 | } else { // Something like "int foo::x;" |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4729 | LookupQualifiedName(Previous, DC); |
| 4730 | |
| Douglas Gregor | b7d17dd | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 4731 | // C++ [dcl.meaning]p1: |
| 4732 | // When the declarator-id is qualified, the declaration shall refer to a |
| 4733 | // previously declared member of the class or namespace to which the |
| 4734 | // qualifier refers (or, in the case of a namespace, of an element of the |
| 4735 | // inline namespace set of that namespace (7.3.1)) or to a specialization |
| 4736 | // thereof; [...] |
| Argyrios Kyrtzidis | 16ac9be | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 4737 | // |
| Douglas Gregor | b7d17dd | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 4738 | // Note that we already checked the context above, and that we do not have |
| 4739 | // enough information to make sure that Previous contains the declaration |
| 4740 | // we want to match. For example, given: |
| Douglas Gregor | ad59050 | 2008-12-15 23:53:10 +0000 | [diff] [blame] | 4741 | // |
| Douglas Gregor | 4287b37 | 2008-12-12 08:25:50 +0000 | [diff] [blame] | 4742 | // class X { |
| 4743 | // void f(); |
| Douglas Gregor | ad59050 | 2008-12-15 23:53:10 +0000 | [diff] [blame] | 4744 | // void f(float); |
| Douglas Gregor | 4287b37 | 2008-12-12 08:25:50 +0000 | [diff] [blame] | 4745 | // }; |
| 4746 | // |
| Douglas Gregor | ad59050 | 2008-12-15 23:53:10 +0000 | [diff] [blame] | 4747 | // void X::f(int) { } // ill-formed |
| 4748 | // |
| Douglas Gregor | b7d17dd | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 4749 | // In this case, Previous will point to the overload set |
| Douglas Gregor | ad59050 | 2008-12-15 23:53:10 +0000 | [diff] [blame] | 4750 | // containing the two f's declared in X, but neither of them |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4751 | // matches. |
| Douglas Gregor | b7d17dd | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 4752 | |
| 4753 | // C++ [dcl.meaning]p1: |
| 4754 | // [...] the member shall not merely have been introduced by a |
| 4755 | // using-declaration in the scope of the class or namespace nominated by |
| 4756 | // the nested-name-specifier of the declarator-id. |
| 4757 | RemoveUsingDecls(Previous); |
| Argyrios Kyrtzidis | 16ac9be | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 4758 | } |
| 4759 | |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4760 | if (Previous.isSingleResult() && |
| 4761 | Previous.getFoundDecl()->isTemplateParameter()) { |
| Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 4762 | // Maybe we will complain about the shadowed template parameter. |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4763 | if (!D.isInvalidType()) |
| Douglas Gregor | f4ef4d2 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 4764 | DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), |
| 4765 | Previous.getFoundDecl()); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4766 | |
| Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 4767 | // Just pretend that we didn't see the previous declaration. |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4768 | Previous.clear(); |
| Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 4769 | } |
| 4770 | |
| Douglas Gregor | 83a586e | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 4771 | // In C++, the previous declaration we find might be a tag type |
| 4772 | // (class or enum). In this case, the new declaration will hide the |
| Douglas Gregor | fb03466 | 2009-01-28 17:15:10 +0000 | [diff] [blame] | 4773 | // tag type. Note that this does does not apply if we're declaring a |
| 4774 | // typedef (C++ [dcl.typedef]p4). |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4775 | if (Previous.isSingleTagDecl() && |
| Kaelyn Uhrain | 5dfc94b | 2013-12-16 19:25:47 +0000 | [diff] [blame] | 4776 | D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef) |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4777 | Previous.clear(); |
| Douglas Gregor | 83a586e | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 4778 | |
| Richard Smith | 5afcdf3f | 2013-03-06 01:37:38 +0000 | [diff] [blame] | 4779 | // Check that there are no default arguments other than in the parameters |
| 4780 | // of a function declaration (C++ only). |
| 4781 | if (getLangOpts().CPlusPlus) |
| 4782 | CheckExtraCXXDefaultArguments(D); |
| 4783 | |
| Nico Weber | cb4c7f4 | 2012-12-23 00:40:46 +0000 | [diff] [blame] | 4784 | NamedDecl *New; |
| 4785 | |
| Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 4786 | bool AddToScope = true; |
| Chris Lattner | 01a7c53 | 2007-01-25 23:09:03 +0000 | [diff] [blame] | 4787 | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| Douglas Gregor | b52fabb | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 4788 | if (TemplateParamLists.size()) { |
| 4789 | Diag(D.getIdentifierLoc(), diag::err_template_typedef); |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4790 | return nullptr; |
| Douglas Gregor | b52fabb | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 4791 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4792 | |
| Kaelyn Uhrain | 4dc695d | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 4793 | New = ActOnTypedefDeclarator(S, D, DC, TInfo, Previous); |
| Douglas Gregor | eddf433 | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 4794 | } else if (R->isFunctionType()) { |
| Kaelyn Uhrain | 4dc695d | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 4795 | New = ActOnFunctionDeclarator(S, D, DC, TInfo, Previous, |
| Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 4796 | TemplateParamLists, |
| Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 4797 | AddToScope); |
| Chris Lattner | 01a7c53 | 2007-01-25 23:09:03 +0000 | [diff] [blame] | 4798 | } else { |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4799 | New = ActOnVariableDeclarator(S, D, DC, TInfo, Previous, TemplateParamLists, |
| 4800 | AddToScope); |
| Chris Lattner | 01a7c53 | 2007-01-25 23:09:03 +0000 | [diff] [blame] | 4801 | } |
| Zhongxing Xu | bece5d6 | 2009-01-16 01:13:29 +0000 | [diff] [blame] | 4802 | |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4803 | if (!New) |
| 4804 | return nullptr; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4805 | |
| Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4806 | // If this has an identifier and is not an invalid redeclaration or |
| 4807 | // function template specialization, add it to the scope stack. |
| Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 4808 | if (New->getDeclName() && AddToScope && |
| Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 4809 | !(D.isRedeclaration() && New->isInvalidDecl())) { |
| 4810 | // Only make a locally-scoped extern declaration visible if it is the first |
| 4811 | // declaration of this entity. Qualified lookup for such an entity should |
| 4812 | // only find this declaration if there is no visible declaration of it. |
| 4813 | bool AddToContext = !D.isRedeclaration() || !New->isLocalExternDecl(); |
| 4814 | PushOnScopeChains(New, S, AddToContext); |
| 4815 | if (!AddToContext) |
| 4816 | CurContext->addHiddenDecl(New); |
| 4817 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4818 | |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4819 | return New; |
| Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 4820 | } |
| 4821 | |
| Abramo Bagnara | ad9f2e2 | 2012-11-08 16:27:30 +0000 | [diff] [blame] | 4822 | /// Helper method to turn variable array types into constant array |
| 4823 | /// types in certain situations which would otherwise be errors (for |
| 4824 | /// GCC compatibility). |
| Eli Friedman | a3b1d03 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 4825 | static QualType TryToFixInvalidVariablyModifiedType(QualType T, |
| 4826 | ASTContext &Context, |
| Douglas Gregor | caa1bf4 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 4827 | bool &SizeIsNegative, |
| 4828 | llvm::APSInt &Oversized) { |
| Eli Friedman | a3b1d03 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 4829 | // This method tries to turn a variable array into a constant |
| 4830 | // array even when the size isn't an ICE. This is necessary |
| 4831 | // for compatibility with code that depends on gcc's buggy |
| 4832 | // constant expression folding, like struct {char x[(int)(char*)2];} |
| 4833 | SizeIsNegative = false; |
| Douglas Gregor | caa1bf4 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 4834 | Oversized = 0; |
| 4835 | |
| 4836 | if (T->isDependentType()) |
| 4837 | return QualType(); |
| 4838 | |
| John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4839 | QualifierCollector Qs; |
| 4840 | const Type *Ty = Qs.strip(T); |
| 4841 | |
| 4842 | if (const PointerType* PTy = dyn_cast<PointerType>(Ty)) { |
| Eli Friedman | a3b1d03 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 4843 | QualType Pointee = PTy->getPointeeType(); |
| 4844 | QualType FixedType = |
| Douglas Gregor | caa1bf4 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 4845 | TryToFixInvalidVariablyModifiedType(Pointee, Context, SizeIsNegative, |
| 4846 | Oversized); |
| Eli Friedman | a3b1d03 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 4847 | if (FixedType.isNull()) return FixedType; |
| Eli Friedman | 44c0f2a | 2009-02-21 00:58:02 +0000 | [diff] [blame] | 4848 | FixedType = Context.getPointerType(FixedType); |
| John McCall | 717d9b0 | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 4849 | return Qs.apply(Context, FixedType); |
| Eli Friedman | a3b1d03 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 4850 | } |
| Abramo Bagnara | 924a8f3 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 4851 | if (const ParenType* PTy = dyn_cast<ParenType>(Ty)) { |
| 4852 | QualType Inner = PTy->getInnerType(); |
| 4853 | QualType FixedType = |
| 4854 | TryToFixInvalidVariablyModifiedType(Inner, Context, SizeIsNegative, |
| 4855 | Oversized); |
| 4856 | if (FixedType.isNull()) return FixedType; |
| 4857 | FixedType = Context.getParenType(FixedType); |
| 4858 | return Qs.apply(Context, FixedType); |
| 4859 | } |
| Eli Friedman | a3b1d03 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 4860 | |
| 4861 | const VariableArrayType* VLATy = dyn_cast<VariableArrayType>(T); |
| Eli Friedman | adf40d4 | 2009-02-26 03:58:54 +0000 | [diff] [blame] | 4862 | if (!VLATy) |
| 4863 | return QualType(); |
| 4864 | // FIXME: We should probably handle this case |
| 4865 | if (VLATy->getElementType()->isVariablyModifiedType()) |
| 4866 | return QualType(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4867 | |
| Richard Smith | 42d3af9 | 2011-12-07 00:43:50 +0000 | [diff] [blame] | 4868 | llvm::APSInt Res; |
| Eli Friedman | a3b1d03 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 4869 | if (!VLATy->getSizeExpr() || |
| Richard Smith | 42d3af9 | 2011-12-07 00:43:50 +0000 | [diff] [blame] | 4870 | !VLATy->getSizeExpr()->EvaluateAsInt(Res, Context)) |
| Eli Friedman | a3b1d03 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 4871 | return QualType(); |
| Eli Friedman | adf40d4 | 2009-02-26 03:58:54 +0000 | [diff] [blame] | 4872 | |
| Douglas Gregor | caa1bf4 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 4873 | // Check whether the array size is negative. |
| Douglas Gregor | caa1bf4 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 4874 | if (Res.isSigned() && Res.isNegative()) { |
| 4875 | SizeIsNegative = true; |
| 4876 | return QualType(); |
| Douglas Gregor | 0431825 | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 4877 | } |
| Eli Friedman | a3b1d03 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 4878 | |
| Douglas Gregor | caa1bf4 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 4879 | // Check whether the array is too large to be addressed. |
| 4880 | unsigned ActiveSizeBits |
| 4881 | = ConstantArrayType::getNumAddressingBits(Context, VLATy->getElementType(), |
| 4882 | Res); |
| 4883 | if (ActiveSizeBits > ConstantArrayType::getMaxSizeBits(Context)) { |
| 4884 | Oversized = Res; |
| 4885 | return QualType(); |
| 4886 | } |
| 4887 | |
| 4888 | return Context.getConstantArrayType(VLATy->getElementType(), |
| 4889 | Res, ArrayType::Normal, 0); |
| Eli Friedman | a3b1d03 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 4890 | } |
| 4891 | |
| Abramo Bagnara | 341ab73 | 2012-11-08 14:44:42 +0000 | [diff] [blame] | 4892 | static void |
| 4893 | FixInvalidVariablyModifiedTypeLoc(TypeLoc SrcTL, TypeLoc DstTL) { |
| David Majnemer | bc4cc14 | 2015-04-18 04:55:51 +0000 | [diff] [blame] | 4894 | SrcTL = SrcTL.getUnqualifiedLoc(); |
| 4895 | DstTL = DstTL.getUnqualifiedLoc(); |
| David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 4896 | if (PointerTypeLoc SrcPTL = SrcTL.getAs<PointerTypeLoc>()) { |
| 4897 | PointerTypeLoc DstPTL = DstTL.castAs<PointerTypeLoc>(); |
| 4898 | FixInvalidVariablyModifiedTypeLoc(SrcPTL.getPointeeLoc(), |
| 4899 | DstPTL.getPointeeLoc()); |
| 4900 | DstPTL.setStarLoc(SrcPTL.getStarLoc()); |
| Abramo Bagnara | 341ab73 | 2012-11-08 14:44:42 +0000 | [diff] [blame] | 4901 | return; |
| 4902 | } |
| David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 4903 | if (ParenTypeLoc SrcPTL = SrcTL.getAs<ParenTypeLoc>()) { |
| 4904 | ParenTypeLoc DstPTL = DstTL.castAs<ParenTypeLoc>(); |
| 4905 | FixInvalidVariablyModifiedTypeLoc(SrcPTL.getInnerLoc(), |
| 4906 | DstPTL.getInnerLoc()); |
| 4907 | DstPTL.setLParenLoc(SrcPTL.getLParenLoc()); |
| 4908 | DstPTL.setRParenLoc(SrcPTL.getRParenLoc()); |
| Abramo Bagnara | 341ab73 | 2012-11-08 14:44:42 +0000 | [diff] [blame] | 4909 | return; |
| 4910 | } |
| David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 4911 | ArrayTypeLoc SrcATL = SrcTL.castAs<ArrayTypeLoc>(); |
| 4912 | ArrayTypeLoc DstATL = DstTL.castAs<ArrayTypeLoc>(); |
| 4913 | TypeLoc SrcElemTL = SrcATL.getElementLoc(); |
| 4914 | TypeLoc DstElemTL = DstATL.getElementLoc(); |
| Abramo Bagnara | 341ab73 | 2012-11-08 14:44:42 +0000 | [diff] [blame] | 4915 | DstElemTL.initializeFullCopy(SrcElemTL); |
| David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 4916 | DstATL.setLBracketLoc(SrcATL.getLBracketLoc()); |
| 4917 | DstATL.setSizeExpr(SrcATL.getSizeExpr()); |
| 4918 | DstATL.setRBracketLoc(SrcATL.getRBracketLoc()); |
| Abramo Bagnara | 341ab73 | 2012-11-08 14:44:42 +0000 | [diff] [blame] | 4919 | } |
| 4920 | |
| Abramo Bagnara | ad9f2e2 | 2012-11-08 16:27:30 +0000 | [diff] [blame] | 4921 | /// Helper method to turn variable array types into constant array |
| 4922 | /// types in certain situations which would otherwise be errors (for |
| 4923 | /// GCC compatibility). |
| Abramo Bagnara | 341ab73 | 2012-11-08 14:44:42 +0000 | [diff] [blame] | 4924 | static TypeSourceInfo* |
| 4925 | TryToFixInvalidVariablyModifiedTypeSourceInfo(TypeSourceInfo *TInfo, |
| 4926 | ASTContext &Context, |
| 4927 | bool &SizeIsNegative, |
| 4928 | llvm::APSInt &Oversized) { |
| 4929 | QualType FixedTy |
| 4930 | = TryToFixInvalidVariablyModifiedType(TInfo->getType(), Context, |
| 4931 | SizeIsNegative, Oversized); |
| 4932 | if (FixedTy.isNull()) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4933 | return nullptr; |
| Abramo Bagnara | 341ab73 | 2012-11-08 14:44:42 +0000 | [diff] [blame] | 4934 | TypeSourceInfo *FixedTInfo = Context.getTrivialTypeSourceInfo(FixedTy); |
| 4935 | FixInvalidVariablyModifiedTypeLoc(TInfo->getTypeLoc(), |
| 4936 | FixedTInfo->getTypeLoc()); |
| 4937 | return FixedTInfo; |
| 4938 | } |
| 4939 | |
| Richard Smith | 78165b5 | 2013-01-10 23:43:47 +0000 | [diff] [blame] | 4940 | /// \brief Register the given locally-scoped extern "C" declaration so |
| Richard Smith | 39b7968 | 2013-06-18 20:15:12 +0000 | [diff] [blame] | 4941 | /// that it can be found later for redeclarations. We include any extern "C" |
| 4942 | /// declaration that is not visible in the translation unit here, not just |
| 4943 | /// function-scope declarations. |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4944 | void |
| Richard Smith | 39b7968 | 2013-06-18 20:15:12 +0000 | [diff] [blame] | 4945 | Sema::RegisterLocallyScopedExternCDecl(NamedDecl *ND, Scope *S) { |
| Richard Smith | ac974a3 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 4946 | if (!getLangOpts().CPlusPlus && |
| 4947 | ND->getLexicalDeclContext()->getRedeclContext()->isTranslationUnit()) |
| 4948 | // Don't need to track declarations in the TU in C. |
| 4949 | return; |
| 4950 | |
| Douglas Gregor | 5a80bd1 | 2009-03-02 00:19:53 +0000 | [diff] [blame] | 4951 | // Note that we have a locally-scoped external with this name. |
| Richard Smith | f19e127 | 2015-03-07 00:04:49 +0000 | [diff] [blame] | 4952 | Context.getExternCContextDecl()->makeDeclVisibleInContext(ND); |
| Douglas Gregor | 5a80bd1 | 2009-03-02 00:19:53 +0000 | [diff] [blame] | 4953 | } |
| 4954 | |
| Richard Smith | 39b7968 | 2013-06-18 20:15:12 +0000 | [diff] [blame] | 4955 | NamedDecl *Sema::findLocallyScopedExternCDecl(DeclarationName Name) { |
| Richard Smith | f19e127 | 2015-03-07 00:04:49 +0000 | [diff] [blame] | 4956 | // FIXME: We can have multiple results via __attribute__((overloadable)). |
| 4957 | auto Result = Context.getExternCContextDecl()->lookup(Name); |
| 4958 | return Result.empty() ? nullptr : *Result.begin(); |
| Douglas Gregor | dc5c958 | 2011-07-28 14:20:37 +0000 | [diff] [blame] | 4959 | } |
| 4960 | |
| Eli Friedman | 574c745 | 2009-04-07 19:37:57 +0000 | [diff] [blame] | 4961 | /// \brief Diagnose function specifiers on a declaration of an identifier that |
| 4962 | /// does not identify a function. |
| Richard Smith | b1402ae | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 4963 | void Sema::DiagnoseFunctionSpecifiers(const DeclSpec &DS) { |
| Eli Friedman | 574c745 | 2009-04-07 19:37:57 +0000 | [diff] [blame] | 4964 | // FIXME: We should probably indicate the identifier in question to avoid |
| 4965 | // confusion for constructs like "inline int a(), b;" |
| Richard Smith | b1402ae | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 4966 | if (DS.isInlineSpecified()) |
| 4967 | Diag(DS.getInlineSpecLoc(), |
| Eli Friedman | 574c745 | 2009-04-07 19:37:57 +0000 | [diff] [blame] | 4968 | diag::err_inline_non_function); |
| 4969 | |
| Richard Smith | b1402ae | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 4970 | if (DS.isVirtualSpecified()) |
| 4971 | Diag(DS.getVirtualSpecLoc(), |
| Eli Friedman | 574c745 | 2009-04-07 19:37:57 +0000 | [diff] [blame] | 4972 | diag::err_virtual_non_function); |
| 4973 | |
| Richard Smith | b1402ae | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 4974 | if (DS.isExplicitSpecified()) |
| 4975 | Diag(DS.getExplicitSpecLoc(), |
| Eli Friedman | 574c745 | 2009-04-07 19:37:57 +0000 | [diff] [blame] | 4976 | diag::err_explicit_non_function); |
| Richard Smith | 0015f09 | 2013-01-17 22:16:11 +0000 | [diff] [blame] | 4977 | |
| Richard Smith | b1402ae | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 4978 | if (DS.isNoreturnSpecified()) |
| 4979 | Diag(DS.getNoreturnSpecLoc(), |
| Richard Smith | 0015f09 | 2013-01-17 22:16:11 +0000 | [diff] [blame] | 4980 | diag::err_noreturn_non_function); |
| Eli Friedman | 574c745 | 2009-04-07 19:37:57 +0000 | [diff] [blame] | 4981 | } |
| 4982 | |
| Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 4983 | NamedDecl* |
| Zhongxing Xu | ac8ef9e | 2009-01-16 03:34:13 +0000 | [diff] [blame] | 4984 | Sema::ActOnTypedefDeclarator(Scope* S, Declarator& D, DeclContext* DC, |
| Kaelyn Uhrain | 4dc695d | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 4985 | TypeSourceInfo *TInfo, LookupResult &Previous) { |
| Zhongxing Xu | ac8ef9e | 2009-01-16 03:34:13 +0000 | [diff] [blame] | 4986 | // Typedef declarators cannot be qualified (C++ [dcl.meaning]p1). |
| 4987 | if (D.getCXXScopeSpec().isSet()) { |
| 4988 | Diag(D.getIdentifierLoc(), diag::err_qualified_typedef_declarator) |
| 4989 | << D.getCXXScopeSpec().getRange(); |
| Chris Lattner | f6d1c9c | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 4990 | D.setInvalidType(); |
| Zhongxing Xu | ac8ef9e | 2009-01-16 03:34:13 +0000 | [diff] [blame] | 4991 | // Pretend we didn't see the scope specifier. |
| Douglas Gregor | b525ef8 | 2010-03-23 15:26:55 +0000 | [diff] [blame] | 4992 | DC = CurContext; |
| 4993 | Previous.clear(); |
| Zhongxing Xu | ac8ef9e | 2009-01-16 03:34:13 +0000 | [diff] [blame] | 4994 | } |
| 4995 | |
| Richard Smith | b1402ae | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 4996 | DiagnoseFunctionSpecifiers(D.getDeclSpec()); |
| Eli Friedman | 574c745 | 2009-04-07 19:37:57 +0000 | [diff] [blame] | 4997 | |
| Richard Smith | a77a0a6 | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 4998 | if (D.getDeclSpec().isConstexprSpecified()) |
| 4999 | Diag(D.getDeclSpec().getConstexprSpecLoc(), diag::err_invalid_constexpr) |
| 5000 | << 1; |
| Eli Friedman | d5c0eed | 2009-04-19 20:27:55 +0000 | [diff] [blame] | 5001 | |
| Douglas Gregor | d8f446f | 2010-07-13 06:37:01 +0000 | [diff] [blame] | 5002 | if (D.getName().Kind != UnqualifiedId::IK_Identifier) { |
| 5003 | Diag(D.getName().StartLocation, diag::err_typedef_not_identifier) |
| 5004 | << D.getName().getSourceRange(); |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5005 | return nullptr; |
| Douglas Gregor | d8f446f | 2010-07-13 06:37:01 +0000 | [diff] [blame] | 5006 | } |
| 5007 | |
| Kaelyn Uhrain | 4dc695d | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 5008 | TypedefDecl *NewTD = ParseTypedefDecl(S, D, TInfo->getType(), TInfo); |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5009 | if (!NewTD) return nullptr; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5010 | |
| Zhongxing Xu | ac8ef9e | 2009-01-16 03:34:13 +0000 | [diff] [blame] | 5011 | // Handle attributes prior to checking for duplicates in MergeVarDecl |
| Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 5012 | ProcessDeclAttributes(S, NewTD, D); |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5013 | |
| Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 5014 | CheckTypedefForVariablyModifiedType(S, NewTD); |
| 5015 | |
| Kaelyn Uhrain | 4dc695d | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 5016 | bool Redeclaration = D.isRedeclaration(); |
| 5017 | NamedDecl *ND = ActOnTypedefNameDecl(S, DC, NewTD, Previous, Redeclaration); |
| 5018 | D.setRedeclaration(Redeclaration); |
| 5019 | return ND; |
| Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 5020 | } |
| 5021 | |
| Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 5022 | void |
| 5023 | Sema::CheckTypedefForVariablyModifiedType(Scope *S, TypedefNameDecl *NewTD) { |
| Chris Lattner | 9fecd74 | 2009-04-19 05:21:20 +0000 | [diff] [blame] | 5024 | // C99 6.7.7p2: If a typedef name specifies a variably modified type |
| 5025 | // then it shall have block scope. |
| Eli Friedman | 88f4ed9 | 2010-08-10 03:13:15 +0000 | [diff] [blame] | 5026 | // Note that variably modified types must be fixed before merging the decl so |
| 5027 | // that redeclarations will match. |
| Abramo Bagnara | 341ab73 | 2012-11-08 14:44:42 +0000 | [diff] [blame] | 5028 | TypeSourceInfo *TInfo = NewTD->getTypeSourceInfo(); |
| 5029 | QualType T = TInfo->getType(); |
| Chris Lattner | 9fecd74 | 2009-04-19 05:21:20 +0000 | [diff] [blame] | 5030 | if (T->isVariablyModifiedType()) { |
| John McCall | aab3e41 | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 5031 | getCurFunction()->setHasBranchProtectedScope(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5032 | |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5033 | if (S->getFnParent() == nullptr) { |
| Eli Friedman | a3b1d03 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 5034 | bool SizeIsNegative; |
| Douglas Gregor | caa1bf4 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 5035 | llvm::APSInt Oversized; |
| Abramo Bagnara | 341ab73 | 2012-11-08 14:44:42 +0000 | [diff] [blame] | 5036 | TypeSourceInfo *FixedTInfo = |
| 5037 | TryToFixInvalidVariablyModifiedTypeSourceInfo(TInfo, Context, |
| 5038 | SizeIsNegative, |
| 5039 | Oversized); |
| 5040 | if (FixedTInfo) { |
| Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 5041 | Diag(NewTD->getLocation(), diag::warn_illegal_constant_array_size); |
| Abramo Bagnara | 341ab73 | 2012-11-08 14:44:42 +0000 | [diff] [blame] | 5042 | NewTD->setTypeSourceInfo(FixedTInfo); |
| Eli Friedman | a3b1d03 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 5043 | } else { |
| 5044 | if (SizeIsNegative) |
| Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 5045 | Diag(NewTD->getLocation(), diag::err_typecheck_negative_array_size); |
| Eli Friedman | a3b1d03 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 5046 | else if (T->isVariableArrayType()) |
| Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 5047 | Diag(NewTD->getLocation(), diag::err_vla_decl_in_file_scope); |
| Douglas Gregor | caa1bf4 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 5048 | else if (Oversized.getBoolValue()) |
| David Blaikie | 30d1544 | 2011-10-19 22:56:21 +0000 | [diff] [blame] | 5049 | Diag(NewTD->getLocation(), diag::err_array_too_large) |
| 5050 | << Oversized.toString(10); |
| Eli Friedman | a3b1d03 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 5051 | else |
| Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 5052 | Diag(NewTD->getLocation(), diag::err_vm_decl_in_file_scope); |
| Chris Lattner | f6d1c9c | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 5053 | NewTD->setInvalidDecl(); |
| Eli Friedman | a3b1d03 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 5054 | } |
| Zhongxing Xu | ac8ef9e | 2009-01-16 03:34:13 +0000 | [diff] [blame] | 5055 | } |
| 5056 | } |
| Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 5057 | } |
| Douglas Gregor | 27821ce | 2009-07-07 16:35:42 +0000 | [diff] [blame] | 5058 | |
| Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 5059 | |
| 5060 | /// ActOnTypedefNameDecl - Perform semantic checking for a declaration which |
| 5061 | /// declares a typedef-name, either using the 'typedef' type specifier or via |
| 5062 | /// a C++0x [dcl.typedef]p2 alias-declaration: 'using T = A;'. |
| 5063 | NamedDecl* |
| 5064 | Sema::ActOnTypedefNameDecl(Scope *S, DeclContext *DC, TypedefNameDecl *NewTD, |
| 5065 | LookupResult &Previous, bool &Redeclaration) { |
| Eli Friedman | 88f4ed9 | 2010-08-10 03:13:15 +0000 | [diff] [blame] | 5066 | // Merge the decl with the existing one if appropriate. If the decl is |
| 5067 | // in an outer scope, it isn't the same thing. |
| Richard Smith | 72bcaec | 2013-12-05 04:30:04 +0000 | [diff] [blame] | 5068 | FilterLookupForScope(Previous, DC, S, /*ConsiderLinkage*/false, |
| 5069 | /*AllowInlineNamespace*/false); |
| Richard Smith | ca40f9b | 2014-08-10 02:20:15 +0000 | [diff] [blame] | 5070 | filterNonConflictingPreviousTypedefDecls(Context, NewTD, Previous); |
| Eli Friedman | 88f4ed9 | 2010-08-10 03:13:15 +0000 | [diff] [blame] | 5071 | if (!Previous.empty()) { |
| 5072 | Redeclaration = true; |
| Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 5073 | MergeTypedefNameDecl(NewTD, Previous); |
| Eli Friedman | 88f4ed9 | 2010-08-10 03:13:15 +0000 | [diff] [blame] | 5074 | } |
| 5075 | |
| Douglas Gregor | 27821ce | 2009-07-07 16:35:42 +0000 | [diff] [blame] | 5076 | // If this is the C FILE type, notify the AST context. |
| 5077 | if (IdentifierInfo *II = NewTD->getIdentifier()) |
| 5078 | if (!NewTD->isInvalidDecl() && |
| Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5079 | NewTD->getDeclContext()->getRedeclContext()->isTranslationUnit()) { |
| Mike Stump | a4de80b | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 5080 | if (II->isStr("FILE")) |
| 5081 | Context.setFILEDecl(NewTD); |
| 5082 | else if (II->isStr("jmp_buf")) |
| 5083 | Context.setjmp_bufDecl(NewTD); |
| 5084 | else if (II->isStr("sigjmp_buf")) |
| 5085 | Context.setsigjmp_bufDecl(NewTD); |
| Rafael Espindola | 6cfa82b | 2011-11-13 21:51:09 +0000 | [diff] [blame] | 5086 | else if (II->isStr("ucontext_t")) |
| 5087 | Context.setucontext_tDecl(NewTD); |
| Mike Stump | a4de80b | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 5088 | } |
| 5089 | |
| Zhongxing Xu | ac8ef9e | 2009-01-16 03:34:13 +0000 | [diff] [blame] | 5090 | return NewTD; |
| 5091 | } |
| 5092 | |
| Douglas Gregor | 5d68a20 | 2009-02-24 19:23:27 +0000 | [diff] [blame] | 5093 | /// \brief Determines whether the given declaration is an out-of-scope |
| 5094 | /// previous declaration. |
| 5095 | /// |
| 5096 | /// This routine should be invoked when name lookup has found a |
| 5097 | /// previous declaration (PrevDecl) that is not in the scope where a |
| 5098 | /// new declaration by the same name is being introduced. If the new |
| 5099 | /// declaration occurs in a local scope, previous declarations with |
| 5100 | /// linkage may still be considered previous declarations (C99 |
| 5101 | /// 6.2.2p4-5, C++ [basic.link]p6). |
| 5102 | /// |
| 5103 | /// \param PrevDecl the previous declaration found by name |
| 5104 | /// lookup |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5105 | /// |
| Douglas Gregor | 5d68a20 | 2009-02-24 19:23:27 +0000 | [diff] [blame] | 5106 | /// \param DC the context in which the new declaration is being |
| 5107 | /// declared. |
| 5108 | /// |
| 5109 | /// \returns true if PrevDecl is an out-of-scope previous declaration |
| 5110 | /// for a new delcaration with the same name. |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5111 | static bool |
| Douglas Gregor | 5d68a20 | 2009-02-24 19:23:27 +0000 | [diff] [blame] | 5112 | isOutOfScopePreviousDeclaration(NamedDecl *PrevDecl, DeclContext *DC, |
| 5113 | ASTContext &Context) { |
| 5114 | if (!PrevDecl) |
| Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5115 | return false; |
| Douglas Gregor | 5d68a20 | 2009-02-24 19:23:27 +0000 | [diff] [blame] | 5116 | |
| Douglas Gregor | eddf433 | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 5117 | if (!PrevDecl->hasLinkage()) |
| 5118 | return false; |
| Douglas Gregor | 5d68a20 | 2009-02-24 19:23:27 +0000 | [diff] [blame] | 5119 | |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5120 | if (Context.getLangOpts().CPlusPlus) { |
| Douglas Gregor | 5d68a20 | 2009-02-24 19:23:27 +0000 | [diff] [blame] | 5121 | // C++ [basic.link]p6: |
| 5122 | // If there is a visible declaration of an entity with linkage |
| 5123 | // having the same name and type, ignoring entities declared |
| 5124 | // outside the innermost enclosing namespace scope, the block |
| 5125 | // scope declaration declares that same entity and receives the |
| 5126 | // linkage of the previous declaration. |
| Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5127 | DeclContext *OuterContext = DC->getRedeclContext(); |
| Douglas Gregor | 5d68a20 | 2009-02-24 19:23:27 +0000 | [diff] [blame] | 5128 | if (!OuterContext->isFunctionOrMethod()) |
| 5129 | // This rule only applies to block-scope declarations. |
| 5130 | return false; |
| Douglas Gregor | fcee946 | 2010-08-27 22:55:10 +0000 | [diff] [blame] | 5131 | |
| 5132 | DeclContext *PrevOuterContext = PrevDecl->getDeclContext(); |
| 5133 | if (PrevOuterContext->isRecord()) |
| 5134 | // We found a member function: ignore it. |
| 5135 | return false; |
| 5136 | |
| 5137 | // Find the innermost enclosing namespace for the new and |
| 5138 | // previous declarations. |
| Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5139 | OuterContext = OuterContext->getEnclosingNamespaceContext(); |
| 5140 | PrevOuterContext = PrevOuterContext->getEnclosingNamespaceContext(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5141 | |
| Douglas Gregor | fcee946 | 2010-08-27 22:55:10 +0000 | [diff] [blame] | 5142 | // The previous declaration is in a different namespace, so it |
| 5143 | // isn't the same function. |
| 5144 | if (!OuterContext->Equals(PrevOuterContext)) |
| 5145 | return false; |
| Douglas Gregor | 5d68a20 | 2009-02-24 19:23:27 +0000 | [diff] [blame] | 5146 | } |
| 5147 | |
| Douglas Gregor | 5d68a20 | 2009-02-24 19:23:27 +0000 | [diff] [blame] | 5148 | return true; |
| 5149 | } |
| 5150 | |
| John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 5151 | static void SetNestedNameSpecifier(DeclaratorDecl *DD, Declarator &D) { |
| 5152 | CXXScopeSpec &SS = D.getCXXScopeSpec(); |
| 5153 | if (!SS.isSet()) return; |
| Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 5154 | DD->setQualifierInfo(SS.getWithLocInContext(DD->getASTContext())); |
| John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 5155 | } |
| 5156 | |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5157 | bool Sema::inferObjCARCLifetime(ValueDecl *decl) { |
| 5158 | QualType type = decl->getType(); |
| 5159 | Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime(); |
| 5160 | if (lifetime == Qualifiers::OCL_Autoreleasing) { |
| 5161 | // Various kinds of declaration aren't allowed to be __autoreleasing. |
| 5162 | unsigned kind = -1U; |
| 5163 | if (VarDecl *var = dyn_cast<VarDecl>(decl)) { |
| 5164 | if (var->hasAttr<BlocksAttr>()) |
| 5165 | kind = 0; // __block |
| 5166 | else if (!var->hasLocalStorage()) |
| 5167 | kind = 1; // global |
| 5168 | } else if (isa<ObjCIvarDecl>(decl)) { |
| 5169 | kind = 3; // ivar |
| 5170 | } else if (isa<FieldDecl>(decl)) { |
| 5171 | kind = 2; // field |
| 5172 | } |
| 5173 | |
| 5174 | if (kind != -1U) { |
| 5175 | Diag(decl->getLocation(), diag::err_arc_autoreleasing_var) |
| 5176 | << kind; |
| 5177 | } |
| 5178 | } else if (lifetime == Qualifiers::OCL_None) { |
| 5179 | // Try to infer lifetime. |
| 5180 | if (!type->isObjCLifetimeType()) |
| 5181 | return false; |
| 5182 | |
| 5183 | lifetime = type->getObjCARCImplicitLifetime(); |
| 5184 | type = Context.getLifetimeQualifiedType(type, lifetime); |
| 5185 | decl->setType(type); |
| 5186 | } |
| 5187 | |
| 5188 | if (VarDecl *var = dyn_cast<VarDecl>(decl)) { |
| 5189 | // Thread-local variables cannot have lifetime. |
| 5190 | if (lifetime && lifetime != Qualifiers::OCL_ExplicitNone && |
| Richard Smith | fd3834f | 2013-04-13 02:43:54 +0000 | [diff] [blame] | 5191 | var->getTLSKind()) { |
| Argyrios Kyrtzidis | cff00d9 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 5192 | Diag(var->getLocation(), diag::err_arc_thread_ownership) |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5193 | << var->getType(); |
| 5194 | return true; |
| 5195 | } |
| 5196 | } |
| 5197 | |
| 5198 | return false; |
| 5199 | } |
| 5200 | |
| Rafael Espindola | f1d2f0e | 2013-01-16 23:11:15 +0000 | [diff] [blame] | 5201 | static void checkAttributesAfterMerging(Sema &S, NamedDecl &ND) { |
| Nico Rieck | f8e8b5f | 2014-01-21 23:54:36 +0000 | [diff] [blame] | 5202 | // Ensure that an auto decl is deduced otherwise the checks below might cache |
| 5203 | // the wrong linkage. |
| 5204 | assert(S.ParsingInitForAutoVars.count(&ND) == 0); |
| 5205 | |
| Rafael Espindola | f1d2f0e | 2013-01-16 23:11:15 +0000 | [diff] [blame] | 5206 | // 'weak' only applies to declarations with external linkage. |
| Rafael Espindola | b306900 | 2013-01-16 23:49:06 +0000 | [diff] [blame] | 5207 | if (WeakAttr *Attr = ND.getAttr<WeakAttr>()) { |
| Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 5208 | if (!ND.isExternallyVisible()) { |
| Rafael Espindola | b306900 | 2013-01-16 23:49:06 +0000 | [diff] [blame] | 5209 | S.Diag(Attr->getLocation(), diag::err_attribute_weak_static); |
| 5210 | ND.dropAttr<WeakAttr>(); |
| 5211 | } |
| 5212 | } |
| 5213 | if (WeakRefAttr *Attr = ND.getAttr<WeakRefAttr>()) { |
| Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 5214 | if (ND.isExternallyVisible()) { |
| Rafael Espindola | b306900 | 2013-01-16 23:49:06 +0000 | [diff] [blame] | 5215 | S.Diag(Attr->getLocation(), diag::err_attribute_weakref_not_static); |
| 5216 | ND.dropAttr<WeakRefAttr>(); |
| David Majnemer | 2dc8146 | 2015-01-19 09:00:28 +0000 | [diff] [blame] | 5217 | ND.dropAttr<AliasAttr>(); |
| 5218 | } |
| 5219 | } |
| 5220 | |
| 5221 | if (auto *VD = dyn_cast<VarDecl>(&ND)) { |
| 5222 | if (VD->hasInit()) { |
| 5223 | if (const auto *Attr = VD->getAttr<AliasAttr>()) { |
| 5224 | assert(VD->isThisDeclarationADefinition() && |
| 5225 | !VD->isExternallyVisible() && "Broken AliasAttr handled late!"); |
| 5226 | S.Diag(Attr->getLocation(), diag::err_alias_is_definition) << VD; |
| 5227 | VD->dropAttr<AliasAttr>(); |
| 5228 | } |
| Rafael Espindola | b306900 | 2013-01-16 23:49:06 +0000 | [diff] [blame] | 5229 | } |
| Rafael Espindola | f1d2f0e | 2013-01-16 23:11:15 +0000 | [diff] [blame] | 5230 | } |
| Reid Kleckner | b144d36 | 2013-05-20 14:02:37 +0000 | [diff] [blame] | 5231 | |
| Nico Weber | 3826734 | 2015-04-22 03:44:51 +0000 | [diff] [blame] | 5232 | // 'selectany' only applies to externally visible variable declarations. |
| Reid Kleckner | b144d36 | 2013-05-20 14:02:37 +0000 | [diff] [blame] | 5233 | // It does not apply to functions. |
| 5234 | if (SelectAnyAttr *Attr = ND.getAttr<SelectAnyAttr>()) { |
| 5235 | if (isa<FunctionDecl>(ND) || !ND.isExternallyVisible()) { |
| Nico Weber | 3826734 | 2015-04-22 03:44:51 +0000 | [diff] [blame] | 5236 | S.Diag(Attr->getLocation(), |
| 5237 | diag::err_attribute_selectany_non_extern_data); |
| Reid Kleckner | b144d36 | 2013-05-20 14:02:37 +0000 | [diff] [blame] | 5238 | ND.dropAttr<SelectAnyAttr>(); |
| 5239 | } |
| 5240 | } |
| Nico Rieck | 8ca0bfc | 2014-03-31 14:56:58 +0000 | [diff] [blame] | 5241 | |
| 5242 | // dll attributes require external linkage. |
| David Majnemer | 7656f41 | 2014-10-04 06:16:45 +0000 | [diff] [blame] | 5243 | if (const InheritableAttr *Attr = getDLLAttr(&ND)) { |
| Nico Rieck | 8ca0bfc | 2014-03-31 14:56:58 +0000 | [diff] [blame] | 5244 | if (!ND.isExternallyVisible()) { |
| 5245 | S.Diag(ND.getLocation(), diag::err_attribute_dll_not_extern) |
| 5246 | << &ND << Attr; |
| 5247 | ND.setInvalidDecl(); |
| 5248 | } |
| 5249 | } |
| Rafael Espindola | f1d2f0e | 2013-01-16 23:11:15 +0000 | [diff] [blame] | 5250 | } |
| 5251 | |
| Nico Rieck | 82f0b06 | 2014-03-31 14:56:15 +0000 | [diff] [blame] | 5252 | static void checkDLLAttributeRedeclaration(Sema &S, NamedDecl *OldDecl, |
| 5253 | NamedDecl *NewDecl, |
| 5254 | bool IsSpecialization) { |
| 5255 | if (TemplateDecl *OldTD = dyn_cast<TemplateDecl>(OldDecl)) |
| 5256 | OldDecl = OldTD->getTemplatedDecl(); |
| 5257 | if (TemplateDecl *NewTD = dyn_cast<TemplateDecl>(NewDecl)) |
| 5258 | NewDecl = NewTD->getTemplatedDecl(); |
| 5259 | |
| 5260 | if (!OldDecl || !NewDecl) |
| Hans Wennborg | dd96db2 | 2014-08-27 21:27:40 +0000 | [diff] [blame] | 5261 | return; |
| Nico Rieck | 82f0b06 | 2014-03-31 14:56:15 +0000 | [diff] [blame] | 5262 | |
| 5263 | const DLLImportAttr *OldImportAttr = OldDecl->getAttr<DLLImportAttr>(); |
| 5264 | const DLLExportAttr *OldExportAttr = OldDecl->getAttr<DLLExportAttr>(); |
| 5265 | const DLLImportAttr *NewImportAttr = NewDecl->getAttr<DLLImportAttr>(); |
| 5266 | const DLLExportAttr *NewExportAttr = NewDecl->getAttr<DLLExportAttr>(); |
| 5267 | |
| 5268 | // dllimport and dllexport are inheritable attributes so we have to exclude |
| 5269 | // inherited attribute instances. |
| 5270 | bool HasNewAttr = (NewImportAttr && !NewImportAttr->isInherited()) || |
| 5271 | (NewExportAttr && !NewExportAttr->isInherited()); |
| 5272 | |
| 5273 | // A redeclaration is not allowed to add a dllimport or dllexport attribute, |
| 5274 | // the only exception being explicit specializations. |
| 5275 | // Implicitly generated declarations are also excluded for now because there |
| 5276 | // is no other way to switch these to use dllimport or dllexport. |
| 5277 | bool AddsAttr = !(OldImportAttr || OldExportAttr) && HasNewAttr; |
| Hans Wennborg | dd96db2 | 2014-08-27 21:27:40 +0000 | [diff] [blame] | 5278 | |
| Nico Rieck | 82f0b06 | 2014-03-31 14:56:15 +0000 | [diff] [blame] | 5279 | if (AddsAttr && !IsSpecialization && !OldDecl->isImplicit()) { |
| Hans Wennborg | dd96db2 | 2014-08-27 21:27:40 +0000 | [diff] [blame] | 5280 | // If the declaration hasn't been used yet, allow with a warning for |
| 5281 | // free functions and global variables. |
| 5282 | bool JustWarn = false; |
| Hans Wennborg | ac6073b | 2014-10-29 21:20:57 +0000 | [diff] [blame] | 5283 | if (!OldDecl->isUsed() && !OldDecl->isCXXClassMember()) { |
| Hans Wennborg | dd96db2 | 2014-08-27 21:27:40 +0000 | [diff] [blame] | 5284 | auto *VD = dyn_cast<VarDecl>(OldDecl); |
| 5285 | if (VD && !VD->getDescribedVarTemplate()) |
| 5286 | JustWarn = true; |
| 5287 | auto *FD = dyn_cast<FunctionDecl>(OldDecl); |
| 5288 | if (FD && FD->getTemplatedKind() == FunctionDecl::TK_NonTemplate) |
| 5289 | JustWarn = true; |
| 5290 | } |
| 5291 | |
| 5292 | unsigned DiagID = JustWarn ? diag::warn_attribute_dll_redeclaration |
| 5293 | : diag::err_attribute_dll_redeclaration; |
| 5294 | S.Diag(NewDecl->getLocation(), DiagID) |
| 5295 | << NewDecl |
| 5296 | << (NewImportAttr ? (const Attr *)NewImportAttr : NewExportAttr); |
| Nico Rieck | 82f0b06 | 2014-03-31 14:56:15 +0000 | [diff] [blame] | 5297 | S.Diag(OldDecl->getLocation(), diag::note_previous_declaration); |
| Hans Wennborg | dd96db2 | 2014-08-27 21:27:40 +0000 | [diff] [blame] | 5298 | if (!JustWarn) { |
| 5299 | NewDecl->setInvalidDecl(); |
| 5300 | return; |
| 5301 | } |
| Nico Rieck | 82f0b06 | 2014-03-31 14:56:15 +0000 | [diff] [blame] | 5302 | } |
| 5303 | |
| 5304 | // A redeclaration is not allowed to drop a dllimport attribute, the only |
| Hans Wennborg | 7c4851e | 2014-08-04 20:54:39 +0000 | [diff] [blame] | 5305 | // exceptions being inline function definitions, local extern declarations, |
| 5306 | // and qualified friend declarations. |
| Nico Rieck | 82f0b06 | 2014-03-31 14:56:15 +0000 | [diff] [blame] | 5307 | // NB: MSVC converts such a declaration to dllexport. |
| Hans Wennborg | 7c4851e | 2014-08-04 20:54:39 +0000 | [diff] [blame] | 5308 | bool IsInline = false, IsStaticDataMember = false, IsQualifiedFriend = false; |
| Nico Rieck | 078d2f8 | 2014-05-29 16:50:20 +0000 | [diff] [blame] | 5309 | if (const auto *VD = dyn_cast<VarDecl>(NewDecl)) |
| 5310 | // Ignore static data because out-of-line definitions are diagnosed |
| 5311 | // separately. |
| 5312 | IsStaticDataMember = VD->isStaticDataMember(); |
| Hans Wennborg | 7c4851e | 2014-08-04 20:54:39 +0000 | [diff] [blame] | 5313 | else if (const auto *FD = dyn_cast<FunctionDecl>(NewDecl)) { |
| Nico Rieck | 078d2f8 | 2014-05-29 16:50:20 +0000 | [diff] [blame] | 5314 | IsInline = FD->isInlined(); |
| Hans Wennborg | 7c4851e | 2014-08-04 20:54:39 +0000 | [diff] [blame] | 5315 | IsQualifiedFriend = FD->getQualifier() && |
| 5316 | FD->getFriendObjectKind() == Decl::FOK_Declared; |
| 5317 | } |
| Hans Wennborg | f436b28 | 2014-05-22 15:46:15 +0000 | [diff] [blame] | 5318 | |
| Hans Wennborg | f51dc3b | 2014-07-31 19:29:39 +0000 | [diff] [blame] | 5319 | if (OldImportAttr && !HasNewAttr && !IsInline && !IsStaticDataMember && |
| Hans Wennborg | 7c4851e | 2014-08-04 20:54:39 +0000 | [diff] [blame] | 5320 | !NewDecl->isLocalExternDecl() && !IsQualifiedFriend) { |
| Nico Rieck | 82f0b06 | 2014-03-31 14:56:15 +0000 | [diff] [blame] | 5321 | S.Diag(NewDecl->getLocation(), |
| 5322 | diag::warn_redeclaration_without_attribute_prev_attribute_ignored) |
| 5323 | << NewDecl << OldImportAttr; |
| 5324 | S.Diag(OldDecl->getLocation(), diag::note_previous_declaration); |
| 5325 | S.Diag(OldImportAttr->getLocation(), diag::note_previous_attribute); |
| 5326 | OldDecl->dropAttr<DLLImportAttr>(); |
| 5327 | NewDecl->dropAttr<DLLImportAttr>(); |
| Hans Wennborg | 606bd6d | 2014-11-03 14:24:45 +0000 | [diff] [blame] | 5328 | } else if (IsInline && OldImportAttr && |
| 5329 | !S.Context.getTargetInfo().getCXXABI().isMicrosoft()) { |
| 5330 | // In MinGW, seeing a function declared inline drops the dllimport attribute. |
| 5331 | OldDecl->dropAttr<DLLImportAttr>(); |
| 5332 | NewDecl->dropAttr<DLLImportAttr>(); |
| 5333 | S.Diag(NewDecl->getLocation(), |
| 5334 | diag::warn_dllimport_dropped_from_inline_function) |
| 5335 | << NewDecl << OldImportAttr; |
| Nico Rieck | 82f0b06 | 2014-03-31 14:56:15 +0000 | [diff] [blame] | 5336 | } |
| 5337 | } |
| 5338 | |
| John McCall | c87d972 | 2013-04-02 02:48:58 +0000 | [diff] [blame] | 5339 | /// Given that we are within the definition of the given function, |
| 5340 | /// will that definition behave like C99's 'inline', where the |
| 5341 | /// definition is discarded except for optimization purposes? |
| 5342 | static bool isFunctionDefinitionDiscarded(Sema &S, FunctionDecl *FD) { |
| 5343 | // Try to avoid calling GetGVALinkageForFunction. |
| 5344 | |
| 5345 | // All cases of this require the 'inline' keyword. |
| 5346 | if (!FD->isInlined()) return false; |
| 5347 | |
| 5348 | // This is only possible in C++ with the gnu_inline attribute. |
| 5349 | if (S.getLangOpts().CPlusPlus && !FD->hasAttr<GNUInlineAttr>()) |
| 5350 | return false; |
| 5351 | |
| 5352 | // Okay, go ahead and call the relatively-more-expensive function. |
| 5353 | |
| 5354 | #ifndef NDEBUG |
| 5355 | // AST quite reasonably asserts that it's working on a function |
| 5356 | // definition. We don't really have a way to tell it that we're |
| 5357 | // currently defining the function, so just lie to it in +Asserts |
| 5358 | // builds. This is an awful hack. |
| 5359 | FD->setLazyBody(1); |
| 5360 | #endif |
| 5361 | |
| David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 5362 | bool isC99Inline = |
| 5363 | S.Context.GetGVALinkageForFunction(FD) == GVA_AvailableExternally; |
| John McCall | c87d972 | 2013-04-02 02:48:58 +0000 | [diff] [blame] | 5364 | |
| 5365 | #ifndef NDEBUG |
| 5366 | FD->setLazyBody(0); |
| 5367 | #endif |
| 5368 | |
| 5369 | return isC99Inline; |
| 5370 | } |
| 5371 | |
| Richard Smith | ac974a3 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 5372 | /// Determine whether a variable is extern "C" prior to attaching |
| 5373 | /// an initializer. We can't just call isExternC() here, because that |
| 5374 | /// will also compute and cache whether the declaration is externally |
| 5375 | /// visible, which might change when we attach the initializer. |
| 5376 | /// |
| 5377 | /// This can only be used if the declaration is known to not be a |
| 5378 | /// redeclaration of an internal linkage declaration. |
| 5379 | /// |
| 5380 | /// For instance: |
| 5381 | /// |
| 5382 | /// auto x = []{}; |
| 5383 | /// |
| 5384 | /// Attaching the initializer here makes this declaration not externally |
| 5385 | /// visible, because its type has internal linkage. |
| 5386 | /// |
| 5387 | /// FIXME: This is a hack. |
| 5388 | template<typename T> |
| 5389 | static bool isIncompleteDeclExternC(Sema &S, const T *D) { |
| 5390 | if (S.getLangOpts().CPlusPlus) { |
| 5391 | // In C++, the overloadable attribute negates the effects of extern "C". |
| 5392 | if (!D->isInExternCContext() || D->template hasAttr<OverloadableAttr>()) |
| 5393 | return false; |
| 5394 | } |
| 5395 | return D->isExternC(); |
| 5396 | } |
| 5397 | |
| Rafael Espindola | 0e0d009 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 5398 | static bool shouldConsiderLinkage(const VarDecl *VD) { |
| 5399 | const DeclContext *DC = VD->getDeclContext()->getRedeclContext(); |
| 5400 | if (DC->isFunctionOrMethod()) |
| Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 5401 | return VD->hasExternalStorage(); |
| Rafael Espindola | 0e0d009 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 5402 | if (DC->isFileContext()) |
| 5403 | return true; |
| 5404 | if (DC->isRecord()) |
| 5405 | return false; |
| 5406 | llvm_unreachable("Unexpected context"); |
| 5407 | } |
| 5408 | |
| 5409 | static bool shouldConsiderLinkage(const FunctionDecl *FD) { |
| 5410 | const DeclContext *DC = FD->getDeclContext()->getRedeclContext(); |
| 5411 | if (DC->isFileContext() || DC->isFunctionOrMethod()) |
| 5412 | return true; |
| 5413 | if (DC->isRecord()) |
| 5414 | return false; |
| 5415 | llvm_unreachable("Unexpected context"); |
| 5416 | } |
| 5417 | |
| Nico Rieck | e84f8db | 2014-03-23 21:24:01 +0000 | [diff] [blame] | 5418 | static bool hasParsedAttr(Scope *S, const AttributeList *AttrList, |
| 5419 | AttributeList::Kind Kind) { |
| 5420 | for (const AttributeList *L = AttrList; L; L = L->getNext()) |
| 5421 | if (L->getKind() == Kind) |
| 5422 | return true; |
| 5423 | return false; |
| 5424 | } |
| 5425 | |
| 5426 | static bool hasParsedAttr(Scope *S, const Declarator &PD, |
| 5427 | AttributeList::Kind Kind) { |
| 5428 | // Check decl attributes on the DeclSpec. |
| 5429 | if (hasParsedAttr(S, PD.getDeclSpec().getAttributes().getList(), Kind)) |
| 5430 | return true; |
| 5431 | |
| 5432 | // Walk the declarator structure, checking decl attributes that were in a type |
| 5433 | // position to the decl itself. |
| 5434 | for (unsigned I = 0, E = PD.getNumTypeObjects(); I != E; ++I) { |
| 5435 | if (hasParsedAttr(S, PD.getTypeObject(I).getAttrs(), Kind)) |
| 5436 | return true; |
| 5437 | } |
| 5438 | |
| 5439 | // Finally, check attributes on the decl itself. |
| 5440 | return hasParsedAttr(S, PD.getAttributes(), Kind); |
| 5441 | } |
| 5442 | |
| Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 5443 | /// Adjust the \c DeclContext for a function or variable that might be a |
| 5444 | /// function-local external declaration. |
| 5445 | bool Sema::adjustContextForLocalExternDecl(DeclContext *&DC) { |
| 5446 | if (!DC->isFunctionOrMethod()) |
| 5447 | return false; |
| 5448 | |
| 5449 | // If this is a local extern function or variable declared within a function |
| 5450 | // template, don't add it into the enclosing namespace scope until it is |
| 5451 | // instantiated; it might have a dependent type right now. |
| 5452 | if (DC->isDependentContext()) |
| 5453 | return true; |
| 5454 | |
| 5455 | // C++11 [basic.link]p7: |
| 5456 | // When a block scope declaration of an entity with linkage is not found to |
| 5457 | // refer to some other declaration, then that entity is a member of the |
| 5458 | // innermost enclosing namespace. |
| 5459 | // |
| 5460 | // Per C++11 [namespace.def]p6, the innermost enclosing namespace is a |
| 5461 | // semantically-enclosing namespace, not a lexically-enclosing one. |
| 5462 | while (!DC->isFileContext() && !isa<LinkageSpecDecl>(DC)) |
| 5463 | DC = DC->getParent(); |
| 5464 | return true; |
| 5465 | } |
| 5466 | |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5467 | NamedDecl * |
| Chris Lattner | 88fdea8 | 2010-10-10 18:16:20 +0000 | [diff] [blame] | 5468 | Sema::ActOnVariableDeclarator(Scope *S, Declarator &D, DeclContext *DC, |
| Kaelyn Uhrain | 4dc695d | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 5469 | TypeSourceInfo *TInfo, LookupResult &Previous, |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5470 | MultiTemplateParamsArg TemplateParamLists, |
| 5471 | bool &AddToScope) { |
| Kaelyn Uhrain | 4dc695d | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 5472 | QualType R = TInfo->getType(); |
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 5473 | DeclarationName Name = GetNameForDeclarator(D).getName(); |
| Zhongxing Xu | 9b7714d | 2009-01-16 02:36:34 +0000 | [diff] [blame] | 5474 | |
| Douglas Gregor | c4df407 | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 5475 | DeclSpec::SCS SCSpec = D.getDeclSpec().getStorageClassSpec(); |
| Craig Topper | c79e5e3 | 2014-10-31 06:57:13 +0000 | [diff] [blame] | 5476 | StorageClass SC = StorageClassSpecToVarDeclStorageClass(D.getDeclSpec()); |
| Joey Gouly | dd7f456 | 2013-01-23 11:56:20 +0000 | [diff] [blame] | 5477 | |
| Nico Rieck | e84f8db | 2014-03-23 21:24:01 +0000 | [diff] [blame] | 5478 | // dllimport globals without explicit storage class are treated as extern. We |
| 5479 | // have to change the storage class this early to get the right DeclContext. |
| 5480 | if (SC == SC_None && !DC->isRecord() && |
| Nico Rieck | 755a36f | 2014-05-25 10:34:16 +0000 | [diff] [blame] | 5481 | hasParsedAttr(S, D, AttributeList::AT_DLLImport) && |
| 5482 | !hasParsedAttr(S, D, AttributeList::AT_DLLExport)) |
| Nico Rieck | e84f8db | 2014-03-23 21:24:01 +0000 | [diff] [blame] | 5483 | SC = SC_Extern; |
| 5484 | |
| Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 5485 | DeclContext *OriginalDC = DC; |
| 5486 | bool IsLocalExternDecl = SC == SC_Extern && |
| 5487 | adjustContextForLocalExternDecl(DC); |
| 5488 | |
| Pekka Jaaskelainen | 8690a68 | 2014-02-20 13:52:08 +0000 | [diff] [blame] | 5489 | if (getLangOpts().OpenCL) { |
| 5490 | // OpenCL v1.0 s6.8.a.3: Pointers to functions are not allowed. |
| 5491 | QualType NR = R; |
| 5492 | while (NR->isPointerType()) { |
| 5493 | if (NR->isFunctionPointerType()) { |
| 5494 | Diag(D.getIdentifierLoc(), diag::err_opencl_function_pointer_variable); |
| 5495 | D.setInvalidType(); |
| 5496 | break; |
| 5497 | } |
| 5498 | NR = NR->getPointeeType(); |
| 5499 | } |
| 5500 | |
| 5501 | if (!getOpenCLOptions().cl_khr_fp16) { |
| 5502 | // OpenCL v1.2 s6.1.1.1: reject declaring variables of the half and |
| 5503 | // half array type (unless the cl_khr_fp16 extension is enabled). |
| 5504 | if (Context.getBaseElementType(R)->isHalfType()) { |
| 5505 | Diag(D.getIdentifierLoc(), diag::err_opencl_half_declaration) << R; |
| 5506 | D.setInvalidType(); |
| 5507 | } |
| Joey Gouly | dd7f456 | 2013-01-23 11:56:20 +0000 | [diff] [blame] | 5508 | } |
| 5509 | } |
| 5510 | |
| Douglas Gregor | c4df407 | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 5511 | if (SCSpec == DeclSpec::SCS_mutable) { |
| Zhongxing Xu | 9b7714d | 2009-01-16 02:36:34 +0000 | [diff] [blame] | 5512 | // mutable can only appear on non-static class members, so it's always |
| 5513 | // an error here |
| 5514 | Diag(D.getIdentifierLoc(), diag::err_mutable_nonmember); |
| Chris Lattner | f6d1c9c | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 5515 | D.setInvalidType(); |
| John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 5516 | SC = SC_None; |
| Zhongxing Xu | 9b7714d | 2009-01-16 02:36:34 +0000 | [diff] [blame] | 5517 | } |
| John McCall | c87d972 | 2013-04-02 02:48:58 +0000 | [diff] [blame] | 5518 | |
| Richard Smith | f2c9afc | 2013-06-17 01:34:01 +0000 | [diff] [blame] | 5519 | if (getLangOpts().CPlusPlus11 && SCSpec == DeclSpec::SCS_register && |
| 5520 | !D.getAsmLabel() && !getSourceManager().isInSystemMacro( |
| 5521 | D.getDeclSpec().getStorageClassSpecLoc())) { |
| 5522 | // In C++11, the 'register' storage class specifier is deprecated. |
| 5523 | // Suppress the warning in system macros, it's used in macros in some |
| 5524 | // popular C system headers, such as in glibc's htonl() macro. |
| 5525 | Diag(D.getDeclSpec().getStorageClassSpecLoc(), |
| 5526 | diag::warn_deprecated_register) |
| 5527 | << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc()); |
| 5528 | } |
| 5529 | |
| Zhongxing Xu | 9b7714d | 2009-01-16 02:36:34 +0000 | [diff] [blame] | 5530 | IdentifierInfo *II = Name.getAsIdentifierInfo(); |
| 5531 | if (!II) { |
| 5532 | Diag(D.getIdentifierLoc(), diag::err_bad_variable_name) |
| Douglas Gregor | bb64afc | 2011-10-09 18:55:59 +0000 | [diff] [blame] | 5533 | << Name; |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5534 | return nullptr; |
| Zhongxing Xu | 9b7714d | 2009-01-16 02:36:34 +0000 | [diff] [blame] | 5535 | } |
| 5536 | |
| Richard Smith | b1402ae | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 5537 | DiagnoseFunctionSpecifiers(D.getDeclSpec()); |
| Douglas Gregor | 0c88030 | 2009-03-11 23:00:04 +0000 | [diff] [blame] | 5538 | |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5539 | if (!DC->isRecord() && S->getFnParent() == nullptr) { |
| Douglas Gregor | 212cab3 | 2009-03-11 20:22:50 +0000 | [diff] [blame] | 5540 | // C99 6.9p2: The storage-class specifiers auto and register shall not |
| 5541 | // appear in the declaration specifiers in an external declaration. |
| Renato Golin | 230c5eb | 2014-05-19 18:15:42 +0000 | [diff] [blame] | 5542 | // Global Register+Asm is a GNU extension we support. |
| 5543 | if (SC == SC_Auto || (SC == SC_Register && !D.getAsmLabel())) { |
| 5544 | Diag(D.getIdentifierLoc(), diag::err_typecheck_sclass_fscope); |
| Chris Lattner | f6d1c9c | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 5545 | D.setInvalidType(); |
| Zhongxing Xu | 9b7714d | 2009-01-16 02:36:34 +0000 | [diff] [blame] | 5546 | } |
| Zhongxing Xu | 9b7714d | 2009-01-16 02:36:34 +0000 | [diff] [blame] | 5547 | } |
| Richard Smith | f2c9afc | 2013-06-17 01:34:01 +0000 | [diff] [blame] | 5548 | |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5549 | if (getLangOpts().OpenCL) { |
| Peter Collingbourne | 2dbb708 | 2011-09-19 21:14:35 +0000 | [diff] [blame] | 5550 | // Set up the special work-group-local storage class for variables in the |
| 5551 | // OpenCL __local address space. |
| Rafael Espindola | 2be6b72 | 2012-12-21 01:21:33 +0000 | [diff] [blame] | 5552 | if (R.getAddressSpace() == LangAS::opencl_local) { |
| Peter Collingbourne | 2dbb708 | 2011-09-19 21:14:35 +0000 | [diff] [blame] | 5553 | SC = SC_OpenCLWorkGroupLocal; |
| Rafael Espindola | 2be6b72 | 2012-12-21 01:21:33 +0000 | [diff] [blame] | 5554 | } |
| Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 5555 | |
| Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 5556 | // OpenCL v1.2 s6.9.b p4: |
| 5557 | // The sampler type cannot be used with the __local and __global address |
| 5558 | // space qualifiers. |
| 5559 | if (R->isSamplerT() && (R.getAddressSpace() == LangAS::opencl_local || |
| 5560 | R.getAddressSpace() == LangAS::opencl_global)) { |
| 5561 | Diag(D.getIdentifierLoc(), diag::err_wrong_sampler_addressspace); |
| 5562 | } |
| 5563 | |
| Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 5564 | // OpenCL 1.2 spec, p6.9 r: |
| 5565 | // The event type cannot be used to declare a program scope variable. |
| 5566 | // The event type cannot be used with the __local, __constant and __global |
| 5567 | // address space qualifiers. |
| 5568 | if (R->isEventT()) { |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5569 | if (S->getParent() == nullptr) { |
| Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 5570 | Diag(D.getLocStart(), diag::err_event_t_global_var); |
| 5571 | D.setInvalidType(); |
| 5572 | } |
| 5573 | |
| 5574 | if (R.getAddressSpace()) { |
| 5575 | Diag(D.getLocStart(), diag::err_event_t_addr_space_qual); |
| 5576 | D.setInvalidType(); |
| 5577 | } |
| 5578 | } |
| Peter Collingbourne | 2dbb708 | 2011-09-19 21:14:35 +0000 | [diff] [blame] | 5579 | } |
| 5580 | |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5581 | bool IsExplicitSpecialization = false; |
| 5582 | bool IsVariableTemplateSpecialization = false; |
| 5583 | bool IsPartialSpecialization = false; |
| Larisse Voufo | d8dd97c | 2013-08-14 03:09:19 +0000 | [diff] [blame] | 5584 | bool IsVariableTemplate = false; |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5585 | VarDecl *NewVD = nullptr; |
| 5586 | VarTemplateDecl *NewTemplate = nullptr; |
| 5587 | TemplateParameterList *TemplateParams = nullptr; |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5588 | if (!getLangOpts().CPlusPlus) { |
| Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5589 | NewVD = VarDecl::Create(Context, DC, D.getLocStart(), |
| Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 5590 | D.getIdentifierLoc(), II, |
| Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 5591 | R, TInfo, SC); |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 5592 | |
| 5593 | if (D.isInvalidType()) |
| 5594 | NewVD->setInvalidDecl(); |
| 5595 | } else { |
| Larisse Voufo | 72caf2b | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 5596 | bool Invalid = false; |
| 5597 | |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 5598 | if (DC->isRecord() && !CurContext->isRecord()) { |
| 5599 | // This is an out-of-line definition of a static data member. |
| Rafael Espindola | 45f96f8 | 2013-06-19 13:41:54 +0000 | [diff] [blame] | 5600 | switch (SC) { |
| 5601 | case SC_None: |
| 5602 | break; |
| 5603 | case SC_Static: |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 5604 | Diag(D.getDeclSpec().getStorageClassSpecLoc(), |
| 5605 | diag::err_static_out_of_line) |
| 5606 | << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc()); |
| Rafael Espindola | 45f96f8 | 2013-06-19 13:41:54 +0000 | [diff] [blame] | 5607 | break; |
| 5608 | case SC_Auto: |
| 5609 | case SC_Register: |
| 5610 | case SC_Extern: |
| 5611 | // [dcl.stc] p2: The auto or register specifiers shall be applied only |
| 5612 | // to names of variables declared in a block or to function parameters. |
| 5613 | // [dcl.stc] p6: The extern specifier cannot be used in the declaration |
| 5614 | // of class members |
| 5615 | |
| 5616 | Diag(D.getDeclSpec().getStorageClassSpecLoc(), |
| 5617 | diag::err_storage_class_for_static_member) |
| 5618 | << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc()); |
| 5619 | break; |
| 5620 | case SC_PrivateExtern: |
| 5621 | llvm_unreachable("C storage class in c++!"); |
| 5622 | case SC_OpenCLWorkGroupLocal: |
| 5623 | llvm_unreachable("OpenCL storage class in c++!"); |
| Rafael Espindola | 8ac2f59 | 2013-04-04 21:21:25 +0000 | [diff] [blame] | 5624 | } |
| Larisse Voufo | 21de36b | 2013-08-06 03:43:07 +0000 | [diff] [blame] | 5625 | } |
| 5626 | |
| Richard Smith | 4297375 | 2012-02-16 20:41:22 +0000 | [diff] [blame] | 5627 | if (SC == SC_Static && CurContext->isRecord()) { |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 5628 | if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(DC)) { |
| 5629 | if (RD->isLocalClass()) |
| 5630 | Diag(D.getIdentifierLoc(), |
| 5631 | diag::err_static_data_member_not_allowed_in_local_class) |
| 5632 | << Name << RD->getDeclName(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5633 | |
| Richard Smith | 4297375 | 2012-02-16 20:41:22 +0000 | [diff] [blame] | 5634 | // C++98 [class.union]p1: If a union contains a static data member, |
| 5635 | // the program is ill-formed. C++11 drops this restriction. |
| 5636 | if (RD->isUnion()) |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 5637 | Diag(D.getIdentifierLoc(), |
| Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5638 | getLangOpts().CPlusPlus11 |
| Richard Smith | 4297375 | 2012-02-16 20:41:22 +0000 | [diff] [blame] | 5639 | ? diag::warn_cxx98_compat_static_data_member_in_union |
| 5640 | : diag::ext_static_data_member_in_union) << Name; |
| 5641 | // We conservatively disallow static data members in anonymous structs. |
| 5642 | else if (!RD->getDeclName()) |
| 5643 | Diag(D.getIdentifierLoc(), |
| 5644 | diag::err_static_data_member_not_allowed_in_anon_struct) |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 5645 | << Name << RD->isUnion(); |
| 5646 | } |
| 5647 | } |
| 5648 | |
| 5649 | // Match up the template parameter lists with the scope specifier, then |
| 5650 | // determine whether we have a template or a template specialization. |
| Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 5651 | TemplateParams = MatchTemplateParametersToScopeSpecifier( |
| 5652 | D.getDeclSpec().getLocStart(), D.getIdentifierLoc(), |
| Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 5653 | D.getCXXScopeSpec(), |
| 5654 | D.getName().getKind() == UnqualifiedId::IK_TemplateId |
| 5655 | ? D.getName().TemplateId |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5656 | : nullptr, |
| Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 5657 | TemplateParamLists, |
| Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 5658 | /*never a friend*/ false, IsExplicitSpecialization, Invalid); |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5659 | |
| Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 5660 | if (TemplateParams) { |
| 5661 | if (!TemplateParams->size() && |
| 5662 | D.getName().getKind() != UnqualifiedId::IK_TemplateId) { |
| 5663 | // There is an extraneous 'template<>' for this variable. Complain |
| 5664 | // about it, but allow the declaration of the variable. |
| 5665 | Diag(TemplateParams->getTemplateLoc(), |
| 5666 | diag::err_template_variable_noparams) |
| 5667 | << II |
| 5668 | << SourceRange(TemplateParams->getTemplateLoc(), |
| 5669 | TemplateParams->getRAngleLoc()); |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5670 | TemplateParams = nullptr; |
| Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 5671 | } else { |
| Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 5672 | if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) { |
| 5673 | // This is an explicit specialization or a partial specialization. |
| 5674 | // FIXME: Check that we can declare a specialization here. |
| 5675 | IsVariableTemplateSpecialization = true; |
| 5676 | IsPartialSpecialization = TemplateParams->size() > 0; |
| 5677 | } else { // if (TemplateParams->size() > 0) |
| 5678 | // This is a template declaration. |
| 5679 | IsVariableTemplate = true; |
| 5680 | |
| 5681 | // Check that we can declare a template here. |
| 5682 | if (CheckTemplateDeclScope(S, TemplateParams)) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5683 | return nullptr; |
| Richard Smith | 0d963d6 | 2014-04-17 02:56:49 +0000 | [diff] [blame] | 5684 | |
| 5685 | // Only C++1y supports variable templates (N3651). |
| 5686 | Diag(D.getIdentifierLoc(), |
| Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 5687 | getLangOpts().CPlusPlus14 |
| Richard Smith | 0d963d6 | 2014-04-17 02:56:49 +0000 | [diff] [blame] | 5688 | ? diag::warn_cxx11_compat_variable_template |
| 5689 | : diag::ext_variable_template); |
| Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 5690 | } |
| 5691 | } |
| Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 5692 | } else { |
| Nico Weber | 9cc795c | 2015-01-30 01:48:49 +0000 | [diff] [blame] | 5693 | assert( |
| 5694 | (Invalid || D.getName().getKind() != UnqualifiedId::IK_TemplateId) && |
| 5695 | "should have a 'template<>' for this decl"); |
| Douglas Gregor | b09f3d8 | 2009-07-22 17:18:37 +0000 | [diff] [blame] | 5696 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5697 | |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5698 | if (IsVariableTemplateSpecialization) { |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5699 | SourceLocation TemplateKWLoc = |
| 5700 | TemplateParamLists.size() > 0 |
| 5701 | ? TemplateParamLists[0]->getTemplateLoc() |
| 5702 | : SourceLocation(); |
| 5703 | DeclResult Res = ActOnVarTemplateSpecialization( |
| Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 5704 | S, D, TInfo, TemplateKWLoc, TemplateParams, SC, |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5705 | IsPartialSpecialization); |
| 5706 | if (Res.isInvalid()) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5707 | return nullptr; |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5708 | NewVD = cast<VarDecl>(Res.get()); |
| 5709 | AddToScope = false; |
| 5710 | } else |
| 5711 | NewVD = VarDecl::Create(Context, DC, D.getLocStart(), |
| 5712 | D.getIdentifierLoc(), II, R, TInfo, SC); |
| Eli Friedman | d5c0eed | 2009-04-19 20:27:55 +0000 | [diff] [blame] | 5713 | |
| Larisse Voufo | 72caf2b | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 5714 | // If this is supposed to be a variable template, create it as such. |
| 5715 | if (IsVariableTemplate) { |
| 5716 | NewTemplate = |
| 5717 | VarTemplateDecl::Create(Context, DC, D.getIdentifierLoc(), Name, |
| Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 5718 | TemplateParams, NewVD); |
| Larisse Voufo | 72caf2b | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 5719 | NewVD->setDescribedVarTemplate(NewTemplate); |
| 5720 | } |
| 5721 | |
| Richard Smith | b2bc2e6 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 5722 | // If this decl has an auto type in need of deduction, make a note of the |
| 5723 | // Decl so we can diagnose uses of it in its own initializer. |
| Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 5724 | if (D.getDeclSpec().containsPlaceholderType() && R->getContainedAutoType()) |
| Richard Smith | b2bc2e6 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 5725 | ParsingInitForAutoVars.insert(NewVD); |
| Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 5726 | |
| Larisse Voufo | 72caf2b | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 5727 | if (D.isInvalidType() || Invalid) { |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 5728 | NewVD->setInvalidDecl(); |
| Larisse Voufo | 72caf2b | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 5729 | if (NewTemplate) |
| 5730 | NewTemplate->setInvalidDecl(); |
| 5731 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5732 | |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 5733 | SetNestedNameSpecifier(NewVD, D); |
| John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 5734 | |
| Richard Smith | 72db563 | 2014-01-25 21:32:06 +0000 | [diff] [blame] | 5735 | // If we have any template parameter lists that don't directly belong to |
| 5736 | // the variable (matching the scope specifier), store them. |
| 5737 | unsigned VDTemplateParamLists = TemplateParams ? 1 : 0; |
| 5738 | if (TemplateParamLists.size() > VDTemplateParamLists) |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5739 | NewVD->setTemplateParameterListsInfo( |
| Richard Smith | 72db563 | 2014-01-25 21:32:06 +0000 | [diff] [blame] | 5740 | Context, TemplateParamLists.size() - VDTemplateParamLists, |
| 5741 | TemplateParamLists.data()); |
| Richard Smith | a77a0a6 | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 5742 | |
| Richard Smith | 6331c40 | 2012-02-13 22:16:19 +0000 | [diff] [blame] | 5743 | if (D.getDeclSpec().isConstexprSpecified()) |
| Richard Smith | f0215fe | 2011-12-25 21:17:58 +0000 | [diff] [blame] | 5744 | NewVD->setConstexpr(true); |
| Abramo Bagnara | da41d0c | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 5745 | } |
| 5746 | |
| Douglas Gregor | 4186681 | 2011-09-12 18:37:38 +0000 | [diff] [blame] | 5747 | // Set the lexical context. If the declarator has a C++ scope specifier, the |
| 5748 | // lexical context will be different from the semantic context. |
| 5749 | NewVD->setLexicalDeclContext(CurContext); |
| Larisse Voufo | 72caf2b | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 5750 | if (NewTemplate) |
| 5751 | NewTemplate->setLexicalDeclContext(CurContext); |
| Douglas Gregor | 4186681 | 2011-09-12 18:37:38 +0000 | [diff] [blame] | 5752 | |
| Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 5753 | if (IsLocalExternDecl) |
| 5754 | NewVD->setLocalExternDecl(); |
| 5755 | |
| Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 5756 | if (DeclSpec::TSCS TSCS = D.getDeclSpec().getThreadStorageClassSpec()) { |
| Bob Wilson | d8f4165 | 2014-11-21 06:52:52 +0000 | [diff] [blame] | 5757 | // C++11 [dcl.stc]p4: |
| 5758 | // When thread_local is applied to a variable of block scope the |
| 5759 | // storage-class-specifier static is implied if it does not appear |
| 5760 | // explicitly. |
| 5761 | // Core issue: 'static' is not implied if the variable is declared |
| 5762 | // 'extern'. |
| 5763 | if (NewVD->hasLocalStorage() && |
| 5764 | (SCSpec != DeclSpec::SCS_unspecified || |
| 5765 | TSCS != DeclSpec::TSCS_thread_local || |
| 5766 | !DC->isFunctionOrMethod())) |
| 5767 | Diag(D.getDeclSpec().getThreadStorageClassSpecLoc(), |
| 5768 | diag::err_thread_non_global) |
| 5769 | << DeclSpec::getSpecifierName(TSCS); |
| 5770 | else if (!Context.getTargetInfo().isTLSSupported()) |
| Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 5771 | Diag(D.getDeclSpec().getThreadStorageClassSpecLoc(), |
| 5772 | diag::err_thread_unsupported); |
| Eli Friedman | d5c0eed | 2009-04-19 20:27:55 +0000 | [diff] [blame] | 5773 | else |
| Enea Zaffanella | acb8ecd | 2013-05-04 08:27:07 +0000 | [diff] [blame] | 5774 | NewVD->setTSCSpec(TSCS); |
| Eli Friedman | d5c0eed | 2009-04-19 20:27:55 +0000 | [diff] [blame] | 5775 | } |
| Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 5776 | |
| John McCall | c87d972 | 2013-04-02 02:48:58 +0000 | [diff] [blame] | 5777 | // C99 6.7.4p3 |
| 5778 | // An inline definition of a function with external linkage shall |
| 5779 | // not contain a definition of a modifiable object with static or |
| 5780 | // thread storage duration... |
| 5781 | // We only apply this when the function is required to be defined |
| 5782 | // elsewhere, i.e. when the function is not 'extern inline'. Note |
| 5783 | // that a local variable with thread storage duration still has to |
| 5784 | // be marked 'static'. Also note that it's possible to get these |
| 5785 | // semantics in C++ using __attribute__((gnu_inline)). |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5786 | if (SC == SC_Static && S->getFnParent() != nullptr && |
| John McCall | c87d972 | 2013-04-02 02:48:58 +0000 | [diff] [blame] | 5787 | !NewVD->getType().isConstQualified()) { |
| 5788 | FunctionDecl *CurFD = getCurFunctionDecl(); |
| 5789 | if (CurFD && isFunctionDefinitionDiscarded(*this, CurFD)) { |
| 5790 | Diag(D.getDeclSpec().getStorageClassSpecLoc(), |
| 5791 | diag::warn_static_local_in_extern_inline); |
| 5792 | MaybeSuggestAddingStaticToDecl(CurFD); |
| 5793 | } |
| 5794 | } |
| 5795 | |
| Douglas Gregor | 3c7cd6a | 2011-09-09 20:53:38 +0000 | [diff] [blame] | 5796 | if (D.getDeclSpec().isModulePrivateSpecified()) { |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5797 | if (IsVariableTemplateSpecialization) |
| 5798 | Diag(NewVD->getLocation(), diag::err_module_private_specialization) |
| 5799 | << (IsPartialSpecialization ? 1 : 0) |
| 5800 | << FixItHint::CreateRemoval( |
| 5801 | D.getDeclSpec().getModulePrivateSpecLoc()); |
| 5802 | else if (IsExplicitSpecialization) |
| Douglas Gregor | 3c7cd6a | 2011-09-09 20:53:38 +0000 | [diff] [blame] | 5803 | Diag(NewVD->getLocation(), diag::err_module_private_specialization) |
| 5804 | << 2 |
| 5805 | << FixItHint::CreateRemoval(D.getDeclSpec().getModulePrivateSpecLoc()); |
| Douglas Gregor | 4186681 | 2011-09-12 18:37:38 +0000 | [diff] [blame] | 5806 | else if (NewVD->hasLocalStorage()) |
| 5807 | Diag(NewVD->getLocation(), diag::err_module_private_local) |
| 5808 | << 0 << NewVD->getDeclName() |
| 5809 | << SourceRange(D.getDeclSpec().getModulePrivateSpecLoc()) |
| 5810 | << FixItHint::CreateRemoval(D.getDeclSpec().getModulePrivateSpecLoc()); |
| Larisse Voufo | 72caf2b | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 5811 | else { |
| Douglas Gregor | 3c7cd6a | 2011-09-09 20:53:38 +0000 | [diff] [blame] | 5812 | NewVD->setModulePrivate(); |
| Larisse Voufo | 72caf2b | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 5813 | if (NewTemplate) |
| 5814 | NewTemplate->setModulePrivate(); |
| 5815 | } |
| Douglas Gregor | 3c7cd6a | 2011-09-09 20:53:38 +0000 | [diff] [blame] | 5816 | } |
| Douglas Gregor | 26701a4 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 5817 | |
| Zhongxing Xu | 9b7714d | 2009-01-16 02:36:34 +0000 | [diff] [blame] | 5818 | // Handle attributes prior to checking for duplicates in MergeVarDecl |
| Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 5819 | ProcessDeclAttributes(S, NewVD, D); |
| Zhongxing Xu | 9b7714d | 2009-01-16 02:36:34 +0000 | [diff] [blame] | 5820 | |
| Peter Collingbourne | c6b0857 | 2012-08-28 20:37:50 +0000 | [diff] [blame] | 5821 | if (getLangOpts().CUDA) { |
| 5822 | // CUDA B.2.5: "__shared__ and __constant__ variables have implied static |
| 5823 | // storage [duration]." |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5824 | if (SC == SC_None && S->getFnParent() != nullptr && |
| Rafael Espindola | 2be6b72 | 2012-12-21 01:21:33 +0000 | [diff] [blame] | 5825 | (NewVD->hasAttr<CUDASharedAttr>() || |
| 5826 | NewVD->hasAttr<CUDAConstantAttr>())) { |
| Peter Collingbourne | c6b0857 | 2012-08-28 20:37:50 +0000 | [diff] [blame] | 5827 | NewVD->setStorageClass(SC_Static); |
| Rafael Espindola | 2be6b72 | 2012-12-21 01:21:33 +0000 | [diff] [blame] | 5828 | } |
| Peter Collingbourne | c6b0857 | 2012-08-28 20:37:50 +0000 | [diff] [blame] | 5829 | } |
| 5830 | |
| Nico Rieck | e84f8db | 2014-03-23 21:24:01 +0000 | [diff] [blame] | 5831 | // Ensure that dllimport globals without explicit storage class are treated as |
| 5832 | // extern. The storage class is set above using parsed attributes. Now we can |
| 5833 | // check the VarDecl itself. |
| 5834 | assert(!NewVD->hasAttr<DLLImportAttr>() || |
| 5835 | NewVD->getAttr<DLLImportAttr>()->isInherited() || |
| 5836 | NewVD->isStaticDataMember() || NewVD->getStorageClass() != SC_None); |
| 5837 | |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5838 | // In auto-retain/release, infer strong retension for variables of |
| 5839 | // retainable type. |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5840 | if (getLangOpts().ObjCAutoRefCount && inferObjCARCLifetime(NewVD)) |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5841 | NewVD->setInvalidDecl(); |
| 5842 | |
| Zhongxing Xu | 9b7714d | 2009-01-16 02:36:34 +0000 | [diff] [blame] | 5843 | // Handle GNU asm-label extension (encoded as an attribute). |
| Chris Lattner | 88fdea8 | 2010-10-10 18:16:20 +0000 | [diff] [blame] | 5844 | if (Expr *E = (Expr*)D.getAsmLabel()) { |
| Zhongxing Xu | 9b7714d | 2009-01-16 02:36:34 +0000 | [diff] [blame] | 5845 | // The parser guarantees this is a string. |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5846 | StringLiteral *SE = cast<StringLiteral>(E); |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5847 | StringRef Label = SE->getString(); |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5848 | if (S->getFnParent() != nullptr) { |
| Abramo Bagnara | 1339223 | 2011-01-11 15:16:52 +0000 | [diff] [blame] | 5849 | switch (SC) { |
| 5850 | case SC_None: |
| 5851 | case SC_Auto: |
| 5852 | Diag(E->getExprLoc(), diag::warn_asm_label_on_auto_decl) << Label; |
| 5853 | break; |
| 5854 | case SC_Register: |
| Renato Golin | 230c5eb | 2014-05-19 18:15:42 +0000 | [diff] [blame] | 5855 | // Local Named register |
| Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 5856 | if (!Context.getTargetInfo().isValidGCCRegisterName(Label)) |
| Abramo Bagnara | 1339223 | 2011-01-11 15:16:52 +0000 | [diff] [blame] | 5857 | Diag(E->getExprLoc(), diag::err_asm_unknown_register_name) << Label; |
| 5858 | break; |
| 5859 | case SC_Static: |
| 5860 | case SC_Extern: |
| 5861 | case SC_PrivateExtern: |
| Peter Collingbourne | 2dbb708 | 2011-09-19 21:14:35 +0000 | [diff] [blame] | 5862 | case SC_OpenCLWorkGroupLocal: |
| Abramo Bagnara | 1339223 | 2011-01-11 15:16:52 +0000 | [diff] [blame] | 5863 | break; |
| 5864 | } |
| Renato Golin | 230c5eb | 2014-05-19 18:15:42 +0000 | [diff] [blame] | 5865 | } else if (SC == SC_Register) { |
| 5866 | // Global Named register |
| 5867 | if (!Context.getTargetInfo().isValidGCCRegisterName(Label)) |
| 5868 | Diag(E->getExprLoc(), diag::err_asm_unknown_register_name) << Label; |
| Renato Golin | 2e31e4e | 2014-06-05 16:45:22 +0000 | [diff] [blame] | 5869 | if (!R->isIntegralType(Context) && !R->isPointerType()) { |
| 5870 | Diag(D.getLocStart(), diag::err_asm_bad_register_type); |
| 5871 | NewVD->setInvalidDecl(true); |
| 5872 | } |
| Abramo Bagnara | 1339223 | 2011-01-11 15:16:52 +0000 | [diff] [blame] | 5873 | } |
| 5874 | |
| 5875 | NewVD->addAttr(::new (Context) AsmLabelAttr(SE->getStrTokenLoc(0), |
| Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 5876 | Context, Label, 0)); |
| David Chisnall | 0867d9c | 2012-02-18 16:12:34 +0000 | [diff] [blame] | 5877 | } else if (!ExtnameUndeclaredIdentifiers.empty()) { |
| 5878 | llvm::DenseMap<IdentifierInfo*,AsmLabelAttr*>::iterator I = |
| 5879 | ExtnameUndeclaredIdentifiers.find(NewVD->getIdentifier()); |
| 5880 | if (I != ExtnameUndeclaredIdentifiers.end()) { |
| 5881 | NewVD->addAttr(I->second); |
| 5882 | ExtnameUndeclaredIdentifiers.erase(I); |
| 5883 | } |
| Zhongxing Xu | 9b7714d | 2009-01-16 02:36:34 +0000 | [diff] [blame] | 5884 | } |
| 5885 | |
| John McCall | a2a3f7d | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 5886 | // Diagnose shadowed variables before filtering for scope. |
| Richard Smith | 72bcaec | 2013-12-05 04:30:04 +0000 | [diff] [blame] | 5887 | if (D.getCXXScopeSpec().isEmpty()) |
| John McCall | df8b37c | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 5888 | CheckShadow(S, NewVD, Previous); |
| John McCall | a2a3f7d | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 5889 | |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5890 | // Don't consider existing declarations that are in a different |
| 5891 | // scope and are out-of-semantic-context declarations (if the new |
| 5892 | // declaration has linkage). |
| Richard Smith | 72bcaec | 2013-12-05 04:30:04 +0000 | [diff] [blame] | 5893 | FilterLookupForScope(Previous, OriginalDC, S, shouldConsiderLinkage(NewVD), |
| 5894 | D.getCXXScopeSpec().isNotEmpty() || |
| 5895 | IsExplicitSpecialization || |
| 5896 | IsVariableTemplateSpecialization); |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5897 | |
| Richard Smith | 1c34fb7 | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 5898 | // Check whether the previous declaration is in the same block scope. This |
| 5899 | // affects whether we merge types with it, per C++11 [dcl.array]p3. |
| 5900 | if (getLangOpts().CPlusPlus && |
| 5901 | NewVD->isLocalVarDecl() && NewVD->hasExternalStorage()) |
| 5902 | NewVD->setPreviousDeclInSameBlockScope( |
| 5903 | Previous.isSingleResult() && !Previous.isShadowed() && |
| Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 5904 | isDeclInScope(Previous.getFoundDecl(), OriginalDC, S, false)); |
| Richard Smith | 1c34fb7 | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 5905 | |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5906 | if (!getLangOpts().CPlusPlus) { |
| Kaelyn Uhrain | 4dc695d | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 5907 | D.setRedeclaration(CheckVariableDeclaration(NewVD, Previous)); |
| 5908 | } else { |
| Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 5909 | // If this is an explicit specialization of a static data member, check it. |
| 5910 | if (IsExplicitSpecialization && !NewVD->isInvalidDecl() && |
| 5911 | CheckMemberSpecialization(NewVD, Previous)) |
| 5912 | NewVD->setInvalidDecl(); |
| 5913 | |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 5914 | // Merge the decl with the existing one if appropriate. |
| 5915 | if (!Previous.empty()) { |
| 5916 | if (Previous.isSingleResult() && |
| 5917 | isa<FieldDecl>(Previous.getFoundDecl()) && |
| 5918 | D.getCXXScopeSpec().isSet()) { |
| 5919 | // The user tried to define a non-static data member |
| 5920 | // out-of-line (C++ [dcl.meaning]p1). |
| 5921 | Diag(NewVD->getLocation(), diag::err_nonstatic_member_out_of_line) |
| 5922 | << D.getCXXScopeSpec().getRange(); |
| 5923 | Previous.clear(); |
| 5924 | NewVD->setInvalidDecl(); |
| 5925 | } |
| 5926 | } else if (D.getCXXScopeSpec().isSet()) { |
| 5927 | // No previous declaration in the qualifying scope. |
| 5928 | Diag(D.getIdentifierLoc(), diag::err_no_member) |
| 5929 | << Name << computeDeclContext(D.getCXXScopeSpec(), true) |
| Douglas Gregor | ef1a09a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 5930 | << D.getCXXScopeSpec().getRange(); |
| Chris Lattner | f6d1c9c | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 5931 | NewVD->setInvalidDecl(); |
| Douglas Gregor | ef1a09a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 5932 | } |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 5933 | |
| Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 5934 | if (!IsVariableTemplateSpecialization) |
| 5935 | D.setRedeclaration(CheckVariableDeclaration(NewVD, Previous)); |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 5936 | |
| Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 5937 | if (NewTemplate) { |
| 5938 | VarTemplateDecl *PrevVarTemplate = |
| 5939 | NewVD->getPreviousDecl() |
| 5940 | ? NewVD->getPreviousDecl()->getDescribedVarTemplate() |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5941 | : nullptr; |
| Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 5942 | |
| 5943 | // Check the template parameter list of this declaration, possibly |
| 5944 | // merging in the template parameter list from the previous variable |
| 5945 | // template declaration. |
| 5946 | if (CheckTemplateParameterList( |
| 5947 | TemplateParams, |
| 5948 | PrevVarTemplate ? PrevVarTemplate->getTemplateParameters() |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5949 | : nullptr, |
| Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 5950 | (D.getCXXScopeSpec().isSet() && DC && DC->isRecord() && |
| 5951 | DC->isDependentContext()) |
| 5952 | ? TPC_ClassTemplateMember |
| 5953 | : TPC_VarTemplate)) |
| 5954 | NewVD->setInvalidDecl(); |
| 5955 | |
| 5956 | // If we are providing an explicit specialization of a static variable |
| 5957 | // template, make a note of that. |
| 5958 | if (PrevVarTemplate && |
| 5959 | PrevVarTemplate->getInstantiatedFromMemberTemplate()) |
| 5960 | PrevVarTemplate->setMemberSpecialization(); |
| 5961 | } |
| Douglas Gregor | ef1a09a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 5962 | } |
| Ryan Flynn | e5dc859 | 2009-07-25 22:29:44 +0000 | [diff] [blame] | 5963 | |
| Rafael Espindola | de6a39f | 2013-03-02 21:41:48 +0000 | [diff] [blame] | 5964 | ProcessPragmaWeak(S, NewVD); |
| Rafael Espindola | f1d2f0e | 2013-01-16 23:11:15 +0000 | [diff] [blame] | 5965 | |
| Richard Smith | ac974a3 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 5966 | // If this is the first declaration of an extern C variable, update |
| 5967 | // the map of such variables. |
| Rafael Espindola | 3f9e444 | 2013-10-19 02:13:21 +0000 | [diff] [blame] | 5968 | if (NewVD->isFirstDecl() && !NewVD->isInvalidDecl() && |
| Richard Smith | ac974a3 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 5969 | isIncompleteDeclExternC(*this, NewVD)) |
| Richard Smith | 39b7968 | 2013-06-18 20:15:12 +0000 | [diff] [blame] | 5970 | RegisterLocallyScopedExternCDecl(NewVD, S); |
| Douglas Gregor | ef1a09a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 5971 | |
| Reid Kleckner | d8110b6 | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 5972 | if (getLangOpts().CPlusPlus && NewVD->isStaticLocal()) { |
| Eli Friedman | 3b7d46c | 2013-07-10 00:30:46 +0000 | [diff] [blame] | 5973 | Decl *ManglingContextDecl; |
| David Majnemer | a7f8c46 | 2015-03-19 21:54:30 +0000 | [diff] [blame] | 5974 | if (MangleNumberingContext *MCtx = getCurrentMangleNumberContext( |
| 5975 | NewVD->getDeclContext(), ManglingContextDecl)) { |
| David Majnemer | f27217f | 2014-03-05 18:55:38 +0000 | [diff] [blame] | 5976 | Context.setManglingNumber( |
| David Majnemer | a7f8c46 | 2015-03-19 21:54:30 +0000 | [diff] [blame] | 5977 | NewVD, MCtx->getManglingNumber( |
| 5978 | NewVD, getMSManglingNumber(getLangOpts(), S))); |
| David Majnemer | 2206bf5 | 2014-03-05 08:57:59 +0000 | [diff] [blame] | 5979 | Context.setStaticLocalNumber(NewVD, MCtx->getStaticLocalNumber(NewVD)); |
| Eli Friedman | 3b7d46c | 2013-07-10 00:30:46 +0000 | [diff] [blame] | 5980 | } |
| 5981 | } |
| 5982 | |
| Nico Rieck | 82f0b06 | 2014-03-31 14:56:15 +0000 | [diff] [blame] | 5983 | if (D.isRedeclaration() && !Previous.empty()) { |
| 5984 | checkDLLAttributeRedeclaration( |
| 5985 | *this, dyn_cast<NamedDecl>(Previous.getRepresentativeDecl()), NewVD, |
| 5986 | IsExplicitSpecialization); |
| 5987 | } |
| 5988 | |
| Larisse Voufo | 72caf2b | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 5989 | if (NewTemplate) { |
| Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 5990 | if (NewVD->isInvalidDecl()) |
| 5991 | NewTemplate->setInvalidDecl(); |
| Larisse Voufo | 72caf2b | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 5992 | ActOnDocumentableDecl(NewTemplate); |
| 5993 | return NewTemplate; |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5994 | } |
| 5995 | |
| Larisse Voufo | 72caf2b | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 5996 | return NewVD; |
| Douglas Gregor | ef1a09a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 5997 | } |
| 5998 | |
| John McCall | df8b37c | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 5999 | /// \brief Diagnose variable or built-in function shadowing. Implements |
| 6000 | /// -Wshadow. |
| John McCall | a2a3f7d | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 6001 | /// |
| John McCall | df8b37c | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 6002 | /// This method is called whenever a VarDecl is added to a "useful" |
| 6003 | /// scope. |
| John McCall | a2a3f7d | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 6004 | /// |
| John McCall | 2d8c760 | 2010-03-20 04:12:52 +0000 | [diff] [blame] | 6005 | /// \param S the scope in which the shadowing name is being declared |
| 6006 | /// \param R the lookup of the name |
| John McCall | a2a3f7d | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 6007 | /// |
| John McCall | df8b37c | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 6008 | void Sema::CheckShadow(Scope *S, VarDecl *D, const LookupResult& R) { |
| John McCall | a2a3f7d | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 6009 | // Return if warning is ignored. |
| Alp Toker | d4a3f0e | 2014-06-15 23:30:39 +0000 | [diff] [blame] | 6010 | if (Diags.isIgnored(diag::warn_decl_shadow, R.getNameLoc())) |
| John McCall | a2a3f7d | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 6011 | return; |
| 6012 | |
| Argyrios Kyrtzidis | 898fdbf | 2011-02-08 18:21:25 +0000 | [diff] [blame] | 6013 | // Don't diagnose declarations at file scope. |
| Argyrios Kyrtzidis | bd0a3fe | 2011-04-25 21:39:50 +0000 | [diff] [blame] | 6014 | if (D->hasGlobalStorage()) |
| John McCall | a2a3f7d | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 6015 | return; |
| Argyrios Kyrtzidis | bd0a3fe | 2011-04-25 21:39:50 +0000 | [diff] [blame] | 6016 | |
| 6017 | DeclContext *NewDC = D->getDeclContext(); |
| 6018 | |
| John McCall | 2d8c760 | 2010-03-20 04:12:52 +0000 | [diff] [blame] | 6019 | // Only diagnose if we're shadowing an unambiguous field or variable. |
| Douglas Gregor | 319aa6c | 2010-03-17 16:03:44 +0000 | [diff] [blame] | 6020 | if (R.getResultKind() != LookupResult::Found) |
| John McCall | a2a3f7d | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 6021 | return; |
| John McCall | a2a3f7d | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 6022 | |
| John McCall | a2a3f7d | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 6023 | NamedDecl* ShadowedDecl = R.getFoundDecl(); |
| 6024 | if (!isa<VarDecl>(ShadowedDecl) && !isa<FieldDecl>(ShadowedDecl)) |
| 6025 | return; |
| 6026 | |
| Argyrios Kyrtzidis | f46cc65 | 2011-01-31 07:04:54 +0000 | [diff] [blame] | 6027 | // Fields are not shadowed by variables in C++ static methods. |
| 6028 | if (isa<FieldDecl>(ShadowedDecl)) |
| 6029 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(NewDC)) |
| 6030 | if (MD->isStatic()) |
| 6031 | return; |
| 6032 | |
| Argyrios Kyrtzidis | 857dd06 | 2011-01-31 07:04:50 +0000 | [diff] [blame] | 6033 | if (VarDecl *shadowedVar = dyn_cast<VarDecl>(ShadowedDecl)) |
| 6034 | if (shadowedVar->isExternC()) { |
| Argyrios Kyrtzidis | 857dd06 | 2011-01-31 07:04:50 +0000 | [diff] [blame] | 6035 | // For shadowing external vars, make sure that we point to the global |
| 6036 | // declaration, not a locally scoped extern declaration. |
| Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 6037 | for (auto I : shadowedVar->redecls()) |
| Argyrios Kyrtzidis | 857dd06 | 2011-01-31 07:04:50 +0000 | [diff] [blame] | 6038 | if (I->isFileVarDecl()) { |
| Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 6039 | ShadowedDecl = I; |
| Argyrios Kyrtzidis | 857dd06 | 2011-01-31 07:04:50 +0000 | [diff] [blame] | 6040 | break; |
| 6041 | } |
| 6042 | } |
| 6043 | |
| 6044 | DeclContext *OldDC = ShadowedDecl->getDeclContext(); |
| 6045 | |
| John McCall | 2d8c760 | 2010-03-20 04:12:52 +0000 | [diff] [blame] | 6046 | // Only warn about certain kinds of shadowing for class members. |
| 6047 | if (NewDC && NewDC->isRecord()) { |
| 6048 | // In particular, don't warn about shadowing non-class members. |
| 6049 | if (!OldDC->isRecord()) |
| 6050 | return; |
| 6051 | |
| 6052 | // TODO: should we warn about static data members shadowing |
| 6053 | // static data members from base classes? |
| 6054 | |
| 6055 | // TODO: don't diagnose for inaccessible shadowed members. |
| 6056 | // This is hard to do perfectly because we might friend the |
| 6057 | // shadowing context, but that's just a false negative. |
| 6058 | } |
| 6059 | |
| 6060 | // Determine what kind of declaration we're shadowing. |
| John McCall | a2a3f7d | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 6061 | unsigned Kind; |
| John McCall | 2d8c760 | 2010-03-20 04:12:52 +0000 | [diff] [blame] | 6062 | if (isa<RecordDecl>(OldDC)) { |
| John McCall | a2a3f7d | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 6063 | if (isa<FieldDecl>(ShadowedDecl)) |
| 6064 | Kind = 3; // field |
| 6065 | else |
| 6066 | Kind = 2; // static data member |
| John McCall | 2d8c760 | 2010-03-20 04:12:52 +0000 | [diff] [blame] | 6067 | } else if (OldDC->isFileContext()) |
| John McCall | a2a3f7d | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 6068 | Kind = 1; // global |
| 6069 | else |
| 6070 | Kind = 0; // local |
| 6071 | |
| John McCall | 2d8c760 | 2010-03-20 04:12:52 +0000 | [diff] [blame] | 6072 | DeclarationName Name = R.getLookupName(); |
| 6073 | |
| John McCall | a2a3f7d | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 6074 | // Emit warning and note. |
| Alp Toker | 15ab373 | 2013-12-12 12:47:48 +0000 | [diff] [blame] | 6075 | if (getSourceManager().isInSystemMacro(R.getNameLoc())) |
| 6076 | return; |
| John McCall | 2d8c760 | 2010-03-20 04:12:52 +0000 | [diff] [blame] | 6077 | Diag(R.getNameLoc(), diag::warn_decl_shadow) << Name << Kind << OldDC; |
| John McCall | a2a3f7d | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 6078 | Diag(ShadowedDecl->getLocation(), diag::note_previous_declaration); |
| 6079 | } |
| 6080 | |
| John McCall | df8b37c | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 6081 | /// \brief Check -Wshadow without the advantage of a previous lookup. |
| 6082 | void Sema::CheckShadow(Scope *S, VarDecl *D) { |
| Alp Toker | d4a3f0e | 2014-06-15 23:30:39 +0000 | [diff] [blame] | 6083 | if (Diags.isIgnored(diag::warn_decl_shadow, D->getLocation())) |
| Argyrios Kyrtzidis | 1cb0de1 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 6084 | return; |
| 6085 | |
| John McCall | df8b37c | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 6086 | LookupResult R(*this, D->getDeclName(), D->getLocation(), |
| 6087 | Sema::LookupOrdinaryName, Sema::ForRedeclaration); |
| 6088 | LookupName(R, S); |
| 6089 | CheckShadow(S, D, R); |
| 6090 | } |
| 6091 | |
| Richard Smith | ac974a3 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 6092 | /// Check for conflict between this global or extern "C" declaration and |
| 6093 | /// previous global or extern "C" declarations. This is only used in C++. |
| Rafael Espindola | 46afb35 | 2013-01-11 19:34:23 +0000 | [diff] [blame] | 6094 | template<typename T> |
| Richard Smith | ac974a3 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 6095 | static bool checkGlobalOrExternCConflict( |
| 6096 | Sema &S, const T *ND, bool IsGlobal, LookupResult &Previous) { |
| 6097 | assert(S.getLangOpts().CPlusPlus && "only C++ has extern \"C\""); |
| 6098 | NamedDecl *Prev = S.findLocallyScopedExternCDecl(ND->getDeclName()); |
| Rafael Espindola | 0e0d009 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 6099 | |
| Richard Smith | ac974a3 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 6100 | if (!Prev && IsGlobal && !isIncompleteDeclExternC(S, ND)) { |
| 6101 | // The common case: this global doesn't conflict with any extern "C" |
| 6102 | // declaration. |
| 6103 | return false; |
| 6104 | } |
| 6105 | |
| 6106 | if (Prev) { |
| 6107 | if (!IsGlobal || isIncompleteDeclExternC(S, ND)) { |
| 6108 | // Both the old and new declarations have C language linkage. This is a |
| 6109 | // redeclaration. |
| 6110 | Previous.clear(); |
| 6111 | Previous.addDecl(Prev); |
| 6112 | return true; |
| 6113 | } |
| 6114 | |
| 6115 | // This is a global, non-extern "C" declaration, and there is a previous |
| 6116 | // non-global extern "C" declaration. Diagnose if this is a variable |
| 6117 | // declaration. |
| 6118 | if (!isa<VarDecl>(ND)) |
| 6119 | return false; |
| 6120 | } else { |
| 6121 | // The declaration is extern "C". Check for any declaration in the |
| 6122 | // translation unit which might conflict. |
| 6123 | if (IsGlobal) { |
| 6124 | // We have already performed the lookup into the translation unit. |
| 6125 | IsGlobal = false; |
| 6126 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 6127 | I != E; ++I) { |
| 6128 | if (isa<VarDecl>(*I)) { |
| 6129 | Prev = *I; |
| 6130 | break; |
| 6131 | } |
| 6132 | } |
| 6133 | } else { |
| 6134 | DeclContext::lookup_result R = |
| 6135 | S.Context.getTranslationUnitDecl()->lookup(ND->getDeclName()); |
| 6136 | for (DeclContext::lookup_result::iterator I = R.begin(), E = R.end(); |
| 6137 | I != E; ++I) { |
| 6138 | if (isa<VarDecl>(*I)) { |
| 6139 | Prev = *I; |
| 6140 | break; |
| 6141 | } |
| 6142 | // FIXME: If we have any other entity with this name in global scope, |
| 6143 | // the declaration is ill-formed, but that is a defect: it breaks the |
| 6144 | // 'stat' hack, for instance. Only variables can have mangled name |
| 6145 | // clashes with extern "C" declarations, so only they deserve a |
| 6146 | // diagnostic. |
| 6147 | } |
| 6148 | } |
| 6149 | |
| 6150 | if (!Prev) |
| Rafael Espindola | 0e0d009 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 6151 | return false; |
| 6152 | } |
| 6153 | |
| Richard Smith | ac974a3 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 6154 | // Use the first declaration's location to ensure we point at something which |
| 6155 | // is lexically inside an extern "C" linkage-spec. |
| 6156 | assert(Prev && "should have found a previous declaration to diagnose"); |
| 6157 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(Prev)) |
| Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 6158 | Prev = FD->getFirstDecl(); |
| Richard Smith | ac974a3 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 6159 | else |
| Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 6160 | Prev = cast<VarDecl>(Prev)->getFirstDecl(); |
| Richard Smith | ac974a3 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 6161 | |
| 6162 | S.Diag(ND->getLocation(), diag::err_extern_c_global_conflict) |
| 6163 | << IsGlobal << ND; |
| 6164 | S.Diag(Prev->getLocation(), diag::note_extern_c_global_conflict) |
| 6165 | << IsGlobal; |
| 6166 | return false; |
| 6167 | } |
| 6168 | |
| 6169 | /// Apply special rules for handling extern "C" declarations. Returns \c true |
| 6170 | /// if we have found that this is a redeclaration of some prior entity. |
| 6171 | /// |
| 6172 | /// Per C++ [dcl.link]p6: |
| 6173 | /// Two declarations [for a function or variable] with C language linkage |
| 6174 | /// with the same name that appear in different scopes refer to the same |
| 6175 | /// [entity]. An entity with C language linkage shall not be declared with |
| 6176 | /// the same name as an entity in global scope. |
| 6177 | template<typename T> |
| 6178 | static bool checkForConflictWithNonVisibleExternC(Sema &S, const T *ND, |
| 6179 | LookupResult &Previous) { |
| 6180 | if (!S.getLangOpts().CPlusPlus) { |
| 6181 | // In C, when declaring a global variable, look for a corresponding 'extern' |
| Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 6182 | // variable declared in function scope. We don't need this in C++, because |
| 6183 | // we find local extern decls in the surrounding file-scope DeclContext. |
| Richard Smith | ac974a3 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 6184 | if (ND->getDeclContext()->getRedeclContext()->isTranslationUnit()) { |
| 6185 | if (NamedDecl *Prev = S.findLocallyScopedExternCDecl(ND->getDeclName())) { |
| 6186 | Previous.clear(); |
| 6187 | Previous.addDecl(Prev); |
| 6188 | return true; |
| 6189 | } |
| 6190 | } |
| 6191 | return false; |
| 6192 | } |
| 6193 | |
| 6194 | // A declaration in the translation unit can conflict with an extern "C" |
| 6195 | // declaration. |
| 6196 | if (ND->getDeclContext()->getRedeclContext()->isTranslationUnit()) |
| 6197 | return checkGlobalOrExternCConflict(S, ND, /*IsGlobal*/true, Previous); |
| 6198 | |
| 6199 | // An extern "C" declaration can conflict with a declaration in the |
| 6200 | // translation unit or can be a redeclaration of an extern "C" declaration |
| 6201 | // in another scope. |
| 6202 | if (isIncompleteDeclExternC(S,ND)) |
| 6203 | return checkGlobalOrExternCConflict(S, ND, /*IsGlobal*/false, Previous); |
| 6204 | |
| 6205 | // Neither global nor extern "C": nothing to do. |
| 6206 | return false; |
| Rafael Espindola | 46afb35 | 2013-01-11 19:34:23 +0000 | [diff] [blame] | 6207 | } |
| 6208 | |
| Richard Smith | 27d807c | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 6209 | void Sema::CheckVariableDeclarationType(VarDecl *NewVD) { |
| Chris Lattner | f6d1c9c | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 6210 | // If the decl is already known invalid, don't check it. |
| 6211 | if (NewVD->isInvalidDecl()) |
| Richard Smith | 27d807c | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 6212 | return; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6213 | |
| Abramo Bagnara | 341ab73 | 2012-11-08 14:44:42 +0000 | [diff] [blame] | 6214 | TypeSourceInfo *TInfo = NewVD->getTypeSourceInfo(); |
| 6215 | QualType T = TInfo->getType(); |
| Douglas Gregor | ef1a09a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 6216 | |
| Richard Smith | 27d807c | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 6217 | // Defer checking an 'auto' type until its initializer is attached. |
| 6218 | if (T->isUndeducedType()) |
| 6219 | return; |
| 6220 | |
| Richard Smith | dc4ccaa | 2014-03-27 01:22:48 +0000 | [diff] [blame] | 6221 | if (NewVD->hasAttrs()) |
| 6222 | CheckAlignasUnderalignment(NewVD); |
| 6223 | |
| John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6224 | if (T->isObjCObjectType()) { |
| Fariborz Jahanian | 9a14c21 | 2011-07-25 21:12:27 +0000 | [diff] [blame] | 6225 | Diag(NewVD->getLocation(), diag::err_statically_allocated_object) |
| 6226 | << FixItHint::CreateInsertion(NewVD->getLocation(), "*"); |
| Fariborz Jahanian | 6507135e | 2011-07-26 17:58:54 +0000 | [diff] [blame] | 6227 | T = Context.getObjCObjectPointerType(T); |
| 6228 | NewVD->setType(T); |
| Douglas Gregor | ef1a09a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 6229 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6230 | |
| Zhongxing Xu | 9b7714d | 2009-01-16 02:36:34 +0000 | [diff] [blame] | 6231 | // Emit an error if an address space was applied to decl with local storage. |
| 6232 | // This includes arrays of objects with address space qualifiers, but not |
| 6233 | // automatic variables that point to other address spaces. |
| 6234 | // ISO/IEC TR 18037 S5.1.2 |
| Chris Lattner | 88fdea8 | 2010-10-10 18:16:20 +0000 | [diff] [blame] | 6235 | if (NewVD->hasLocalStorage() && T.getAddressSpace() != 0) { |
| Douglas Gregor | ef1a09a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 6236 | Diag(NewVD->getLocation(), diag::err_as_qualified_auto_decl); |
| Kaelyn Uhrain | 4dc695d | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 6237 | NewVD->setInvalidDecl(); |
| Richard Smith | 27d807c | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 6238 | return; |
| Zhongxing Xu | 9b7714d | 2009-01-16 02:36:34 +0000 | [diff] [blame] | 6239 | } |
| Fariborz Jahanian | 0c9404e | 2009-02-21 19:44:02 +0000 | [diff] [blame] | 6240 | |
| Tanya Lattner | 713eef4 | 2013-04-05 20:14:50 +0000 | [diff] [blame] | 6241 | // OpenCL v1.2 s6.5 - All program scope variables must be declared in the |
| 6242 | // __constant address space. |
| 6243 | if (getLangOpts().OpenCL && NewVD->isFileVarDecl() |
| 6244 | && T.getAddressSpace() != LangAS::opencl_constant |
| 6245 | && !T->isSamplerT()){ |
| 6246 | Diag(NewVD->getLocation(), diag::err_opencl_global_invalid_addr_space); |
| 6247 | NewVD->setInvalidDecl(); |
| Richard Smith | 27d807c | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 6248 | return; |
| Tanya Lattner | 713eef4 | 2013-04-05 20:14:50 +0000 | [diff] [blame] | 6249 | } |
| 6250 | |
| Tanya Lattner | 4fdce3f | 2012-06-19 23:09:52 +0000 | [diff] [blame] | 6251 | // OpenCL v1.2 s6.8 -- The static qualifier is valid only in program |
| 6252 | // scope. |
| 6253 | if ((getLangOpts().OpenCLVersion >= 120) |
| 6254 | && NewVD->isStaticLocal()) { |
| 6255 | Diag(NewVD->getLocation(), diag::err_static_function_scope); |
| 6256 | NewVD->setInvalidDecl(); |
| Richard Smith | 27d807c | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 6257 | return; |
| Tanya Lattner | 4fdce3f | 2012-06-19 23:09:52 +0000 | [diff] [blame] | 6258 | } |
| 6259 | |
| Mike Stump | ca5ae66 | 2009-04-14 00:57:29 +0000 | [diff] [blame] | 6260 | if (NewVD->hasLocalStorage() && T.isObjCGCWeak() |
| Fariborz Jahanian | c32830c | 2011-06-07 20:15:46 +0000 | [diff] [blame] | 6261 | && !NewVD->hasAttr<BlocksAttr>()) { |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6262 | if (getLangOpts().getGC() != LangOptions::NonGC) |
| Fariborz Jahanian | c32830c | 2011-06-07 20:15:46 +0000 | [diff] [blame] | 6263 | Diag(NewVD->getLocation(), diag::warn_gc_attribute_weak_on_local); |
| Ted Kremenek | 2f88c40 | 2012-10-02 05:36:02 +0000 | [diff] [blame] | 6264 | else { |
| 6265 | assert(!getLangOpts().ObjCAutoRefCount); |
| Fariborz Jahanian | c32830c | 2011-06-07 20:15:46 +0000 | [diff] [blame] | 6266 | Diag(NewVD->getLocation(), diag::warn_attribute_weak_on_local); |
| Ted Kremenek | 2f88c40 | 2012-10-02 05:36:02 +0000 | [diff] [blame] | 6267 | } |
| Fariborz Jahanian | c32830c | 2011-06-07 20:15:46 +0000 | [diff] [blame] | 6268 | } |
| Chris Lattner | 88fdea8 | 2010-10-10 18:16:20 +0000 | [diff] [blame] | 6269 | |
| Chris Lattner | 9fecd74 | 2009-04-19 05:21:20 +0000 | [diff] [blame] | 6270 | bool isVM = T->isVariablyModifiedType(); |
| Chris Lattner | 9662cd3 | 2009-07-19 20:17:11 +0000 | [diff] [blame] | 6271 | if (isVM || NewVD->hasAttr<CleanupAttr>() || |
| John McCall | d4e1b76 | 2010-08-01 01:24:59 +0000 | [diff] [blame] | 6272 | NewVD->hasAttr<BlocksAttr>()) |
| John McCall | aab3e41 | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 6273 | getCurFunction()->setHasBranchProtectedScope(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6274 | |
| Chris Lattner | 9fecd74 | 2009-04-19 05:21:20 +0000 | [diff] [blame] | 6275 | if ((isVM && NewVD->hasLinkage()) || |
| 6276 | (T->isVariableArrayType() && NewVD->hasGlobalStorage())) { |
| Anders Carlsson | 6c88580 | 2009-02-28 21:56:50 +0000 | [diff] [blame] | 6277 | bool SizeIsNegative; |
| Douglas Gregor | caa1bf4 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 6278 | llvm::APSInt Oversized; |
| Abramo Bagnara | 341ab73 | 2012-11-08 14:44:42 +0000 | [diff] [blame] | 6279 | TypeSourceInfo *FixedTInfo = |
| 6280 | TryToFixInvalidVariablyModifiedTypeSourceInfo(TInfo, Context, |
| 6281 | SizeIsNegative, Oversized); |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6282 | if (!FixedTInfo && T->isVariableArrayType()) { |
| Douglas Gregor | ef1a09a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 6283 | const VariableArrayType *VAT = Context.getAsVariableArrayType(T); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6284 | // FIXME: This won't give the correct result for |
| 6285 | // int a[10][n]; |
| Anders Carlsson | 6c88580 | 2009-02-28 21:56:50 +0000 | [diff] [blame] | 6286 | SourceRange SizeRange = VAT->getSizeExpr()->getSourceRange(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6287 | |
| Anders Carlsson | 6c88580 | 2009-02-28 21:56:50 +0000 | [diff] [blame] | 6288 | if (NewVD->isFileVarDecl()) |
| 6289 | Diag(NewVD->getLocation(), diag::err_vla_decl_in_file_scope) |
| Chris Lattner | f6d1c9c | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 6290 | << SizeRange; |
| Enea Zaffanella | 28f36ba | 2013-05-10 20:34:44 +0000 | [diff] [blame] | 6291 | else if (NewVD->isStaticLocal()) |
| Anders Carlsson | 6c88580 | 2009-02-28 21:56:50 +0000 | [diff] [blame] | 6292 | Diag(NewVD->getLocation(), diag::err_vla_decl_has_static_storage) |
| Chris Lattner | f6d1c9c | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 6293 | << SizeRange; |
| Anders Carlsson | 6c88580 | 2009-02-28 21:56:50 +0000 | [diff] [blame] | 6294 | else |
| 6295 | Diag(NewVD->getLocation(), diag::err_vla_decl_has_extern_linkage) |
| Chris Lattner | f6d1c9c | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 6296 | << SizeRange; |
| Kaelyn Uhrain | 4dc695d | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 6297 | NewVD->setInvalidDecl(); |
| Richard Smith | 27d807c | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 6298 | return; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6299 | } |
| 6300 | |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6301 | if (!FixedTInfo) { |
| Anders Carlsson | 6c88580 | 2009-02-28 21:56:50 +0000 | [diff] [blame] | 6302 | if (NewVD->isFileVarDecl()) |
| 6303 | Diag(NewVD->getLocation(), diag::err_vm_decl_in_file_scope); |
| 6304 | else |
| 6305 | Diag(NewVD->getLocation(), diag::err_vm_decl_has_extern_linkage); |
| Kaelyn Uhrain | 4dc695d | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 6306 | NewVD->setInvalidDecl(); |
| Richard Smith | 27d807c | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 6307 | return; |
| Anders Carlsson | 6c88580 | 2009-02-28 21:56:50 +0000 | [diff] [blame] | 6308 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6309 | |
| Chris Lattner | f6d1c9c | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 6310 | Diag(NewVD->getLocation(), diag::warn_illegal_constant_array_size); |
| Abramo Bagnara | 3b8a9e5 | 2012-11-08 16:01:51 +0000 | [diff] [blame] | 6311 | NewVD->setType(FixedTInfo->getType()); |
| Abramo Bagnara | 341ab73 | 2012-11-08 14:44:42 +0000 | [diff] [blame] | 6312 | NewVD->setTypeSourceInfo(FixedTInfo); |
| Anders Carlsson | 6c88580 | 2009-02-28 21:56:50 +0000 | [diff] [blame] | 6313 | } |
| 6314 | |
| David Majnemer | 0ffa331 | 2013-05-29 00:56:45 +0000 | [diff] [blame] | 6315 | if (T->isVoidType()) { |
| 6316 | // C++98 [dcl.stc]p5: The extern specifier can be applied only to the names |
| 6317 | // of objects and functions. |
| 6318 | if (NewVD->isThisDeclarationADefinition() || getLangOpts().CPlusPlus) { |
| 6319 | Diag(NewVD->getLocation(), diag::err_typecheck_decl_incomplete_type) |
| 6320 | << T; |
| 6321 | NewVD->setInvalidDecl(); |
| 6322 | return; |
| 6323 | } |
| Richard Smith | 27d807c | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 6324 | } |
| 6325 | |
| 6326 | if (!NewVD->hasLocalStorage() && NewVD->hasAttr<BlocksAttr>()) { |
| 6327 | Diag(NewVD->getLocation(), diag::err_block_on_nonlocal); |
| 6328 | NewVD->setInvalidDecl(); |
| 6329 | return; |
| 6330 | } |
| 6331 | |
| 6332 | if (isVM && NewVD->hasAttr<BlocksAttr>()) { |
| 6333 | Diag(NewVD->getLocation(), diag::err_block_on_vm); |
| 6334 | NewVD->setInvalidDecl(); |
| 6335 | return; |
| 6336 | } |
| 6337 | |
| 6338 | if (NewVD->isConstexpr() && !T->isDependentType() && |
| 6339 | RequireLiteralType(NewVD->getLocation(), T, |
| 6340 | diag::err_constexpr_var_non_literal)) { |
| Richard Smith | 27d807c | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 6341 | NewVD->setInvalidDecl(); |
| 6342 | return; |
| 6343 | } |
| 6344 | } |
| 6345 | |
| 6346 | /// \brief Perform semantic checking on a newly-created variable |
| 6347 | /// declaration. |
| 6348 | /// |
| 6349 | /// This routine performs all of the type-checking required for a |
| 6350 | /// variable declaration once it has been built. It is used both to |
| 6351 | /// check variables after they have been parsed and their declarators |
| 6352 | /// have been translated into a declaration, and to check variables |
| 6353 | /// that have been instantiated from a template. |
| 6354 | /// |
| 6355 | /// Sets NewVD->isInvalidDecl() if an error was encountered. |
| 6356 | /// |
| 6357 | /// Returns true if the variable declaration is a redeclaration. |
| Larisse Voufo | 72caf2b | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 6358 | bool Sema::CheckVariableDeclaration(VarDecl *NewVD, LookupResult &Previous) { |
| Richard Smith | 27d807c | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 6359 | CheckVariableDeclarationType(NewVD); |
| 6360 | |
| 6361 | // If the decl is already known invalid, don't check it. |
| 6362 | if (NewVD->isInvalidDecl()) |
| 6363 | return false; |
| 6364 | |
| John McCall | b65e8fe | 2013-04-01 18:34:28 +0000 | [diff] [blame] | 6365 | // If we did not find anything by this name, look for a non-visible |
| 6366 | // extern "C" declaration with the same name. |
| Richard Smith | 1c34fb7 | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 6367 | if (Previous.empty() && |
| 6368 | checkForConflictWithNonVisibleExternC(*this, NewVD, Previous)) |
| Richard Smith | 3c78578 | 2013-09-03 21:00:58 +0000 | [diff] [blame] | 6369 | Previous.setShadowed(); |
| Douglas Gregor | 5a80bd1 | 2009-03-02 00:19:53 +0000 | [diff] [blame] | 6370 | |
| Douglas Gregor | 3552dab | 2013-01-09 00:47:56 +0000 | [diff] [blame] | 6371 | // Filter out any non-conflicting previous declarations. |
| 6372 | filterNonConflictingPreviousDecls(Context, NewVD, Previous); |
| 6373 | |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 6374 | if (!Previous.empty()) { |
| Richard Smith | 3c78578 | 2013-09-03 21:00:58 +0000 | [diff] [blame] | 6375 | MergeVarDecl(NewVD, Previous); |
| Kaelyn Uhrain | 4dc695d | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 6376 | return true; |
| Zhongxing Xu | 9b7714d | 2009-01-16 02:36:34 +0000 | [diff] [blame] | 6377 | } |
| Kaelyn Uhrain | 4dc695d | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 6378 | return false; |
| Zhongxing Xu | 9b7714d | 2009-01-16 02:36:34 +0000 | [diff] [blame] | 6379 | } |
| 6380 | |
| Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 6381 | /// \brief Data used with FindOverriddenMethod |
| 6382 | struct FindOverriddenMethodData { |
| 6383 | Sema *S; |
| 6384 | CXXMethodDecl *Method; |
| 6385 | }; |
| 6386 | |
| 6387 | /// \brief Member lookup function that determines whether a given C++ |
| 6388 | /// method overrides a method in a base class, to be used with |
| 6389 | /// CXXRecordDecl::lookupInBases(). |
| John McCall | 84c16cf | 2009-11-12 03:15:40 +0000 | [diff] [blame] | 6390 | static bool FindOverriddenMethod(const CXXBaseSpecifier *Specifier, |
| Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 6391 | CXXBasePath &Path, |
| 6392 | void *UserData) { |
| 6393 | RecordDecl *BaseRecord = Specifier->getType()->getAs<RecordType>()->getDecl(); |
| Anders Carlsson | e985fae | 2009-11-26 20:50:40 +0000 | [diff] [blame] | 6394 | |
| Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 6395 | FindOverriddenMethodData *Data |
| 6396 | = reinterpret_cast<FindOverriddenMethodData*>(UserData); |
| Anders Carlsson | e985fae | 2009-11-26 20:50:40 +0000 | [diff] [blame] | 6397 | |
| 6398 | DeclarationName Name = Data->Method->getDeclName(); |
| 6399 | |
| 6400 | // FIXME: Do we care about other names here too? |
| 6401 | if (Name.getNameKind() == DeclarationName::CXXDestructorName) { |
| John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 6402 | // We really want to find the base class destructor here. |
| Anders Carlsson | e985fae | 2009-11-26 20:50:40 +0000 | [diff] [blame] | 6403 | QualType T = Data->S->Context.getTypeDeclType(BaseRecord); |
| 6404 | CanQualType CT = Data->S->Context.getCanonicalType(T); |
| 6405 | |
| Anders Carlsson | 5a4f772 | 2009-11-27 01:26:58 +0000 | [diff] [blame] | 6406 | Name = Data->S->Context.DeclarationNames.getCXXDestructorName(CT); |
| Anders Carlsson | e985fae | 2009-11-26 20:50:40 +0000 | [diff] [blame] | 6407 | } |
| 6408 | |
| 6409 | for (Path.Decls = BaseRecord->lookup(Name); |
| David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 6410 | !Path.Decls.empty(); |
| 6411 | Path.Decls = Path.Decls.slice(1)) { |
| 6412 | NamedDecl *D = Path.Decls.front(); |
| John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 6413 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) { |
| 6414 | if (MD->isVirtual() && !Data->S->IsOverload(Data->Method, MD, false)) |
| Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 6415 | return true; |
| 6416 | } |
| 6417 | } |
| 6418 | |
| 6419 | return false; |
| 6420 | } |
| 6421 | |
| David Blaikie | 7e41426 | 2012-10-17 00:47:58 +0000 | [diff] [blame] | 6422 | namespace { |
| 6423 | enum OverrideErrorKind { OEK_All, OEK_NonDeleted, OEK_Deleted }; |
| 6424 | } |
| 6425 | /// \brief Report an error regarding overriding, along with any relevant |
| 6426 | /// overriden methods. |
| 6427 | /// |
| 6428 | /// \param DiagID the primary error to report. |
| 6429 | /// \param MD the overriding method. |
| 6430 | /// \param OEK which overrides to include as notes. |
| 6431 | static void ReportOverrides(Sema& S, unsigned DiagID, const CXXMethodDecl *MD, |
| 6432 | OverrideErrorKind OEK = OEK_All) { |
| 6433 | S.Diag(MD->getLocation(), DiagID) << MD->getDeclName(); |
| 6434 | for (CXXMethodDecl::method_iterator I = MD->begin_overridden_methods(), |
| 6435 | E = MD->end_overridden_methods(); |
| 6436 | I != E; ++I) { |
| 6437 | // This check (& the OEK parameter) could be replaced by a predicate, but |
| 6438 | // without lambdas that would be overkill. This is still nicer than writing |
| 6439 | // out the diag loop 3 times. |
| 6440 | if ((OEK == OEK_All) || |
| 6441 | (OEK == OEK_NonDeleted && !(*I)->isDeleted()) || |
| 6442 | (OEK == OEK_Deleted && (*I)->isDeleted())) |
| 6443 | S.Diag((*I)->getLocation(), diag::note_overridden_virtual_function); |
| 6444 | } |
| 6445 | } |
| 6446 | |
| Sebastian Redl | d5b2453 | 2009-11-18 21:51:29 +0000 | [diff] [blame] | 6447 | /// AddOverriddenMethods - See if a method overrides any in the base classes, |
| 6448 | /// 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] | 6449 | bool Sema::AddOverriddenMethods(CXXRecordDecl *DC, CXXMethodDecl *MD) { |
| David Blaikie | 1cbb971 | 2014-11-14 19:09:44 +0000 | [diff] [blame] | 6450 | // Look for methods in base classes that this method might override. |
| Sebastian Redl | d5b2453 | 2009-11-18 21:51:29 +0000 | [diff] [blame] | 6451 | CXXBasePaths Paths; |
| 6452 | FindOverriddenMethodData Data; |
| 6453 | Data.Method = MD; |
| 6454 | Data.S = this; |
| David Blaikie | 7e41426 | 2012-10-17 00:47:58 +0000 | [diff] [blame] | 6455 | bool hasDeletedOverridenMethods = false; |
| 6456 | bool hasNonDeletedOverridenMethods = false; |
| Douglas Gregor | 5a2bb5b | 2010-10-13 22:55:32 +0000 | [diff] [blame] | 6457 | bool AddedAny = false; |
| Sebastian Redl | d5b2453 | 2009-11-18 21:51:29 +0000 | [diff] [blame] | 6458 | if (DC->lookupInBases(&FindOverriddenMethod, &Data, Paths)) { |
| Aaron Ballman | e6f465e | 2014-03-14 21:38:48 +0000 | [diff] [blame] | 6459 | for (auto *I : Paths.found_decls()) { |
| 6460 | if (CXXMethodDecl *OldMD = dyn_cast<CXXMethodDecl>(I)) { |
| Richard Trieu | 95d8809 | 2011-07-01 20:02:53 +0000 | [diff] [blame] | 6461 | MD->addOverriddenMethod(OldMD->getCanonicalDecl()); |
| Sebastian Redl | d5b2453 | 2009-11-18 21:51:29 +0000 | [diff] [blame] | 6462 | if (!CheckOverridingFunctionReturnType(MD, OldMD) && |
| Aaron Ballman | 02df2e0 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 6463 | !CheckOverridingFunctionAttributes(MD, OldMD) && |
| Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 6464 | !CheckOverridingFunctionExceptionSpec(MD, OldMD) && |
| Anders Carlsson | 3f610c7 | 2011-01-20 16:25:36 +0000 | [diff] [blame] | 6465 | !CheckIfOverriddenFunctionIsMarkedFinal(MD, OldMD)) { |
| David Blaikie | 7e41426 | 2012-10-17 00:47:58 +0000 | [diff] [blame] | 6466 | hasDeletedOverridenMethods |= OldMD->isDeleted(); |
| 6467 | hasNonDeletedOverridenMethods |= !OldMD->isDeleted(); |
| Douglas Gregor | 5a2bb5b | 2010-10-13 22:55:32 +0000 | [diff] [blame] | 6468 | AddedAny = true; |
| 6469 | } |
| Sebastian Redl | d5b2453 | 2009-11-18 21:51:29 +0000 | [diff] [blame] | 6470 | } |
| 6471 | } |
| 6472 | } |
| David Blaikie | 7e41426 | 2012-10-17 00:47:58 +0000 | [diff] [blame] | 6473 | |
| 6474 | if (hasDeletedOverridenMethods && !MD->isDeleted()) { |
| 6475 | ReportOverrides(*this, diag::err_non_deleted_override, MD, OEK_Deleted); |
| 6476 | } |
| 6477 | if (hasNonDeletedOverridenMethods && MD->isDeleted()) { |
| 6478 | ReportOverrides(*this, diag::err_deleted_override, MD, OEK_NonDeleted); |
| 6479 | } |
| 6480 | |
| Douglas Gregor | 5a2bb5b | 2010-10-13 22:55:32 +0000 | [diff] [blame] | 6481 | return AddedAny; |
| Sebastian Redl | d5b2453 | 2009-11-18 21:51:29 +0000 | [diff] [blame] | 6482 | } |
| 6483 | |
| Kaelyn Uhrain | 0a32fca | 2011-10-11 00:28:39 +0000 | [diff] [blame] | 6484 | namespace { |
| 6485 | // Struct for holding all of the extra arguments needed by |
| 6486 | // DiagnoseInvalidRedeclaration to call Sema::ActOnFunctionDeclarator. |
| 6487 | struct ActOnFDArgs { |
| 6488 | Scope *S; |
| 6489 | Declarator &D; |
| Kaelyn Uhrain | 0a32fca | 2011-10-11 00:28:39 +0000 | [diff] [blame] | 6490 | MultiTemplateParamsArg TemplateParamLists; |
| Kaelyn Uhrain | 0a32fca | 2011-10-11 00:28:39 +0000 | [diff] [blame] | 6491 | bool AddToScope; |
| 6492 | }; |
| 6493 | } |
| 6494 | |
| Kaelyn Uhrain | b137840 | 2012-01-18 21:41:41 +0000 | [diff] [blame] | 6495 | namespace { |
| 6496 | |
| 6497 | // 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] | 6498 | // Also only accept corrections that have the same parent decl. |
| Kaelyn Uhrain | b137840 | 2012-01-18 21:41:41 +0000 | [diff] [blame] | 6499 | class DifferentNameValidatorCCC : public CorrectionCandidateCallback { |
| 6500 | public: |
| Kaelyn Uhrain | 389e9c2 | 2012-06-07 23:57:08 +0000 | [diff] [blame] | 6501 | DifferentNameValidatorCCC(ASTContext &Context, FunctionDecl *TypoFD, |
| 6502 | CXXRecordDecl *Parent) |
| 6503 | : Context(Context), OriginalFD(TypoFD), |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6504 | ExpectedParent(Parent ? Parent->getCanonicalDecl() : nullptr) {} |
| Kaelyn Uhrain | 7c01917 | 2012-02-16 22:40:59 +0000 | [diff] [blame] | 6505 | |
| Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 6506 | bool ValidateCandidate(const TypoCorrection &candidate) override { |
| Kaelyn Uhrain | 7c01917 | 2012-02-16 22:40:59 +0000 | [diff] [blame] | 6507 | if (candidate.getEditDistance() == 0) |
| 6508 | return false; |
| 6509 | |
| Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 6510 | SmallVector<unsigned, 1> MismatchedParams; |
| Kaelyn Uhrain | 389e9c2 | 2012-06-07 23:57:08 +0000 | [diff] [blame] | 6511 | for (TypoCorrection::const_decl_iterator CDecl = candidate.begin(), |
| 6512 | CDeclEnd = candidate.end(); |
| 6513 | CDecl != CDeclEnd; ++CDecl) { |
| 6514 | FunctionDecl *FD = dyn_cast<FunctionDecl>(*CDecl); |
| 6515 | |
| 6516 | if (FD && !FD->hasBody() && |
| 6517 | hasSimilarParameters(Context, FD, OriginalFD, MismatchedParams)) { |
| 6518 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) { |
| 6519 | CXXRecordDecl *Parent = MD->getParent(); |
| 6520 | if (Parent && Parent->getCanonicalDecl() == ExpectedParent) |
| 6521 | return true; |
| 6522 | } else if (!ExpectedParent) { |
| 6523 | return true; |
| 6524 | } |
| 6525 | } |
| Kaelyn Uhrain | 7c01917 | 2012-02-16 22:40:59 +0000 | [diff] [blame] | 6526 | } |
| 6527 | |
| Kaelyn Uhrain | 389e9c2 | 2012-06-07 23:57:08 +0000 | [diff] [blame] | 6528 | return false; |
| Kaelyn Uhrain | b137840 | 2012-01-18 21:41:41 +0000 | [diff] [blame] | 6529 | } |
| Kaelyn Uhrain | 7c01917 | 2012-02-16 22:40:59 +0000 | [diff] [blame] | 6530 | |
| 6531 | private: |
| Kaelyn Uhrain | 389e9c2 | 2012-06-07 23:57:08 +0000 | [diff] [blame] | 6532 | ASTContext &Context; |
| 6533 | FunctionDecl *OriginalFD; |
| Kaelyn Uhrain | 7c01917 | 2012-02-16 22:40:59 +0000 | [diff] [blame] | 6534 | CXXRecordDecl *ExpectedParent; |
| Kaelyn Uhrain | b137840 | 2012-01-18 21:41:41 +0000 | [diff] [blame] | 6535 | }; |
| 6536 | |
| 6537 | } |
| 6538 | |
| Kaelyn Uhrain | 0a32fca | 2011-10-11 00:28:39 +0000 | [diff] [blame] | 6539 | /// \brief Generate diagnostics for an invalid function redeclaration. |
| 6540 | /// |
| 6541 | /// This routine handles generating the diagnostic messages for an invalid |
| 6542 | /// function redeclaration, including finding possible similar declarations |
| 6543 | /// or performing typo correction if there are no previous declarations with |
| 6544 | /// the same name. |
| 6545 | /// |
| Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 6546 | /// Returns a NamedDecl iff typo correction was performed and substituting in |
| Kaelyn Uhrain | 0a32fca | 2011-10-11 00:28:39 +0000 | [diff] [blame] | 6547 | /// the new declaration name does not cause new errors. |
| Richard Smith | 114394f | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 6548 | static NamedDecl *DiagnoseInvalidRedeclaration( |
| Kaelyn Uhrain | 8af2c9f | 2011-10-11 00:28:52 +0000 | [diff] [blame] | 6549 | Sema &SemaRef, LookupResult &Previous, FunctionDecl *NewFD, |
| Richard Smith | 114394f | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 6550 | ActOnFDArgs &ExtraArgs, bool IsLocalFriend, Scope *S) { |
| Kaelyn Uhrain | fd81a35 | 2011-08-18 18:19:12 +0000 | [diff] [blame] | 6551 | DeclarationName Name = NewFD->getDeclName(); |
| Kaelyn Uhrain | 0a32fca | 2011-10-11 00:28:39 +0000 | [diff] [blame] | 6552 | DeclContext *NewDC = NewFD->getDeclContext(); |
| Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 6553 | SmallVector<unsigned, 1> MismatchedParams; |
| 6554 | SmallVector<std::pair<FunctionDecl *, unsigned>, 1> NearMatches; |
| Kaelyn Uhrain | fd81a35 | 2011-08-18 18:19:12 +0000 | [diff] [blame] | 6555 | TypoCorrection Correction; |
| Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 6556 | bool IsDefinition = ExtraArgs.D.isFunctionDefinition(); |
| Richard Smith | 114394f | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 6557 | unsigned DiagMsg = IsLocalFriend ? diag::err_no_matching_local_friend |
| 6558 | : diag::err_member_decl_does_not_match; |
| 6559 | LookupResult Prev(SemaRef, Name, NewFD->getLocation(), |
| 6560 | IsLocalFriend ? Sema::LookupLocalFriendName |
| 6561 | : Sema::LookupOrdinaryName, |
| 6562 | Sema::ForRedeclaration); |
| Kaelyn Uhrain | fd81a35 | 2011-08-18 18:19:12 +0000 | [diff] [blame] | 6563 | |
| 6564 | NewFD->setInvalidDecl(); |
| Richard Smith | 114394f | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 6565 | if (IsLocalFriend) |
| 6566 | SemaRef.LookupName(Prev, S); |
| 6567 | else |
| 6568 | SemaRef.LookupQualifiedName(Prev, NewDC); |
| John McCall | f7cfb22 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 6569 | assert(!Prev.isAmbiguous() && |
| 6570 | "Cannot have an ambiguity in previous-declaration lookup"); |
| Kaelyn Uhrain | 7c01917 | 2012-02-16 22:40:59 +0000 | [diff] [blame] | 6571 | CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(NewFD); |
| Kaelyn Uhrain | fd81a35 | 2011-08-18 18:19:12 +0000 | [diff] [blame] | 6572 | if (!Prev.empty()) { |
| 6573 | for (LookupResult::iterator Func = Prev.begin(), FuncEnd = Prev.end(); |
| 6574 | Func != FuncEnd; ++Func) { |
| 6575 | FunctionDecl *FD = dyn_cast<FunctionDecl>(*Func); |
| Kaelyn Uhrain | 8af2c9f | 2011-10-11 00:28:52 +0000 | [diff] [blame] | 6576 | if (FD && |
| 6577 | hasSimilarParameters(SemaRef.Context, FD, NewFD, MismatchedParams)) { |
| Kaelyn Uhrain | fd81a35 | 2011-08-18 18:19:12 +0000 | [diff] [blame] | 6578 | // Add 1 to the index so that 0 can mean the mismatch didn't |
| 6579 | // involve a parameter |
| 6580 | unsigned ParamNum = |
| 6581 | MismatchedParams.empty() ? 0 : MismatchedParams.front() + 1; |
| 6582 | NearMatches.push_back(std::make_pair(FD, ParamNum)); |
| 6583 | } |
| Kaelyn Uhrain | 7d9bc63 | 2011-08-04 17:40:00 +0000 | [diff] [blame] | 6584 | } |
| Kaelyn Uhrain | fd81a35 | 2011-08-18 18:19:12 +0000 | [diff] [blame] | 6585 | // If the qualified name lookup yielded nothing, try typo correction |
| Richard Smith | 114394f | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 6586 | } else if ((Correction = SemaRef.CorrectTypo( |
| Kaelyn Takata | 89c881b | 2014-10-27 18:07:29 +0000 | [diff] [blame] | 6587 | Prev.getLookupNameInfo(), Prev.getLookupKind(), S, |
| 6588 | &ExtraArgs.D.getCXXScopeSpec(), |
| 6589 | llvm::make_unique<DifferentNameValidatorCCC>( |
| 6590 | SemaRef.Context, NewFD, MD ? MD->getParent() : nullptr), |
| 6591 | Sema::CTK_ErrorRecovery, IsLocalFriend ? nullptr : NewDC))) { |
| Kaelyn Uhrain | 0a32fca | 2011-10-11 00:28:39 +0000 | [diff] [blame] | 6592 | // Set up everything for the call to ActOnFunctionDeclarator |
| 6593 | ExtraArgs.D.SetIdentifier(Correction.getCorrectionAsIdentifierInfo(), |
| 6594 | ExtraArgs.D.getIdentifierLoc()); |
| 6595 | Previous.clear(); |
| 6596 | Previous.setLookupName(Correction.getCorrection()); |
| Kaelyn Uhrain | fd81a35 | 2011-08-18 18:19:12 +0000 | [diff] [blame] | 6597 | for (TypoCorrection::decl_iterator CDecl = Correction.begin(), |
| 6598 | CDeclEnd = Correction.end(); |
| 6599 | CDecl != CDeclEnd; ++CDecl) { |
| 6600 | FunctionDecl *FD = dyn_cast<FunctionDecl>(*CDecl); |
| Kaelyn Uhrain | 389e9c2 | 2012-06-07 23:57:08 +0000 | [diff] [blame] | 6601 | if (FD && !FD->hasBody() && |
| 6602 | hasSimilarParameters(SemaRef.Context, FD, NewFD, MismatchedParams)) { |
| Kaelyn Uhrain | 0a32fca | 2011-10-11 00:28:39 +0000 | [diff] [blame] | 6603 | Previous.addDecl(FD); |
| Kaelyn Uhrain | fd81a35 | 2011-08-18 18:19:12 +0000 | [diff] [blame] | 6604 | } |
| 6605 | } |
| Kaelyn Uhrain | 4dc695d | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 6606 | bool wasRedeclaration = ExtraArgs.D.isRedeclaration(); |
| Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 6607 | |
| 6608 | NamedDecl *Result; |
| 6609 | // Retry building the function declaration with the new previous |
| 6610 | // declarations, and with errors suppressed. |
| 6611 | { |
| 6612 | // Trap errors. |
| 6613 | Sema::SFINAETrap Trap(SemaRef); |
| 6614 | |
| 6615 | // TODO: Refactor ActOnFunctionDeclarator so that we can call only the |
| 6616 | // pieces need to verify the typo-corrected C++ declaration and hopefully |
| 6617 | // eliminate the need for the parameter pack ExtraArgs. |
| 6618 | Result = SemaRef.ActOnFunctionDeclarator( |
| 6619 | ExtraArgs.S, ExtraArgs.D, |
| 6620 | Correction.getCorrectionDecl()->getDeclContext(), |
| 6621 | NewFD->getTypeSourceInfo(), Previous, ExtraArgs.TemplateParamLists, |
| 6622 | ExtraArgs.AddToScope); |
| 6623 | |
| 6624 | if (Trap.hasErrorOccurred()) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6625 | Result = nullptr; |
| Kaelyn Uhrain | 0a32fca | 2011-10-11 00:28:39 +0000 | [diff] [blame] | 6626 | } |
| Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 6627 | |
| 6628 | if (Result) { |
| 6629 | // Determine which correction we picked. |
| 6630 | Decl *Canonical = Result->getCanonicalDecl(); |
| 6631 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 6632 | I != E; ++I) |
| 6633 | if ((*I)->getCanonicalDecl() == Canonical) |
| 6634 | Correction.setCorrectionDecl(*I); |
| 6635 | |
| 6636 | SemaRef.diagnoseTypo( |
| 6637 | Correction, |
| 6638 | SemaRef.PDiag(IsLocalFriend |
| 6639 | ? diag::err_no_matching_local_friend_suggest |
| 6640 | : diag::err_member_decl_does_not_match_suggest) |
| 6641 | << Name << NewDC << IsDefinition); |
| 6642 | return Result; |
| Kaelyn Uhrain | 0a32fca | 2011-10-11 00:28:39 +0000 | [diff] [blame] | 6643 | } |
| Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 6644 | |
| 6645 | // Pretend the typo correction never occurred |
| 6646 | ExtraArgs.D.SetIdentifier(Name.getAsIdentifierInfo(), |
| 6647 | ExtraArgs.D.getIdentifierLoc()); |
| 6648 | ExtraArgs.D.setRedeclaration(wasRedeclaration); |
| 6649 | Previous.clear(); |
| 6650 | Previous.setLookupName(Name); |
| Kaelyn Uhrain | fd81a35 | 2011-08-18 18:19:12 +0000 | [diff] [blame] | 6651 | } |
| 6652 | |
| Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 6653 | SemaRef.Diag(NewFD->getLocation(), DiagMsg) |
| 6654 | << Name << NewDC << IsDefinition << NewFD->getLocation(); |
| Kaelyn Uhrain | fd81a35 | 2011-08-18 18:19:12 +0000 | [diff] [blame] | 6655 | |
| Kaelyn Uhrain | 1a6eb99 | 2011-10-10 18:01:37 +0000 | [diff] [blame] | 6656 | bool NewFDisConst = false; |
| 6657 | if (CXXMethodDecl *NewMD = dyn_cast<CXXMethodDecl>(NewFD)) |
| David Blaikie | f5697e5 | 2012-08-10 00:55:35 +0000 | [diff] [blame] | 6658 | NewFDisConst = NewMD->isConst(); |
| Kaelyn Uhrain | 1a6eb99 | 2011-10-10 18:01:37 +0000 | [diff] [blame] | 6659 | |
| Craig Topper | af6bd1b | 2013-07-04 03:15:42 +0000 | [diff] [blame] | 6660 | for (SmallVectorImpl<std::pair<FunctionDecl *, unsigned> >::iterator |
| Kaelyn Uhrain | fd81a35 | 2011-08-18 18:19:12 +0000 | [diff] [blame] | 6661 | NearMatch = NearMatches.begin(), NearMatchEnd = NearMatches.end(); |
| 6662 | NearMatch != NearMatchEnd; ++NearMatch) { |
| 6663 | FunctionDecl *FD = NearMatch->first; |
| Richard Smith | 114394f | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 6664 | CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD); |
| 6665 | bool FDisConst = MD && MD->isConst(); |
| 6666 | bool IsMember = MD || !IsLocalFriend; |
| Kaelyn Uhrain | fd81a35 | 2011-08-18 18:19:12 +0000 | [diff] [blame] | 6667 | |
| Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 6668 | // FIXME: These notes are poorly worded for the local friend case. |
| Kaelyn Uhrain | fd81a35 | 2011-08-18 18:19:12 +0000 | [diff] [blame] | 6669 | if (unsigned Idx = NearMatch->second) { |
| 6670 | ParmVarDecl *FDParam = FD->getParamDecl(Idx-1); |
| Richard Smith | cf8ec8d | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 6671 | SourceLocation Loc = FDParam->getTypeSpecStartLoc(); |
| 6672 | if (Loc.isInvalid()) Loc = FD->getLocation(); |
| Richard Smith | 114394f | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 6673 | SemaRef.Diag(Loc, IsMember ? diag::note_member_def_close_param_match |
| 6674 | : diag::note_local_decl_close_param_match) |
| 6675 | << Idx << FDParam->getType() |
| 6676 | << NewFD->getParamDecl(Idx - 1)->getType(); |
| Kaelyn Uhrain | 1a6eb99 | 2011-10-10 18:01:37 +0000 | [diff] [blame] | 6677 | } else if (FDisConst != NewFDisConst) { |
| Kaelyn Uhrain | 8af2c9f | 2011-10-11 00:28:52 +0000 | [diff] [blame] | 6678 | SemaRef.Diag(FD->getLocation(), diag::note_member_def_close_const_match) |
| Kaelyn Uhrain | 1a6eb99 | 2011-10-10 18:01:37 +0000 | [diff] [blame] | 6679 | << NewFDisConst << FD->getSourceRange().getEnd(); |
| Kaelyn Uhrain | fd81a35 | 2011-08-18 18:19:12 +0000 | [diff] [blame] | 6680 | } else |
| Richard Smith | 114394f | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 6681 | SemaRef.Diag(FD->getLocation(), |
| 6682 | IsMember ? diag::note_member_def_close_match |
| 6683 | : diag::note_local_decl_close_match); |
| John McCall | f7cfb22 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 6684 | } |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6685 | return nullptr; |
| John McCall | f7cfb22 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 6686 | } |
| 6687 | |
| Craig Topper | c79e5e3 | 2014-10-31 06:57:13 +0000 | [diff] [blame] | 6688 | static StorageClass getFunctionStorageClass(Sema &SemaRef, Declarator &D) { |
| Kaelyn Uhrain | e1a9ff7 | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6689 | switch (D.getDeclSpec().getStorageClassSpec()) { |
| 6690 | default: llvm_unreachable("Unknown storage class!"); |
| 6691 | case DeclSpec::SCS_auto: |
| 6692 | case DeclSpec::SCS_register: |
| 6693 | case DeclSpec::SCS_mutable: |
| 6694 | SemaRef.Diag(D.getDeclSpec().getStorageClassSpecLoc(), |
| 6695 | diag::err_typecheck_sclass_func); |
| 6696 | D.setInvalidType(); |
| 6697 | break; |
| 6698 | case DeclSpec::SCS_unspecified: break; |
| Rafael Espindola | bff5956 | 2013-04-25 12:11:36 +0000 | [diff] [blame] | 6699 | case DeclSpec::SCS_extern: |
| 6700 | if (D.getDeclSpec().isExternInLinkageSpec()) |
| 6701 | return SC_None; |
| 6702 | return SC_Extern; |
| Kaelyn Uhrain | e1a9ff7 | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6703 | case DeclSpec::SCS_static: { |
| 6704 | if (SemaRef.CurContext->getRedeclContext()->isFunctionOrMethod()) { |
| 6705 | // C99 6.7.1p5: |
| 6706 | // The declaration of an identifier for a function that has |
| 6707 | // block scope shall have no explicit storage-class specifier |
| 6708 | // other than extern |
| 6709 | // See also (C++ [dcl.stc]p4). |
| 6710 | SemaRef.Diag(D.getDeclSpec().getStorageClassSpecLoc(), |
| 6711 | diag::err_static_block_func); |
| 6712 | break; |
| 6713 | } else |
| 6714 | return SC_Static; |
| 6715 | } |
| 6716 | case DeclSpec::SCS_private_extern: return SC_PrivateExtern; |
| 6717 | } |
| 6718 | |
| 6719 | // No explicit storage class has already been returned |
| 6720 | return SC_None; |
| 6721 | } |
| 6722 | |
| 6723 | static FunctionDecl* CreateNewFunctionDecl(Sema &SemaRef, Declarator &D, |
| 6724 | DeclContext *DC, QualType &R, |
| 6725 | TypeSourceInfo *TInfo, |
| Craig Topper | c79e5e3 | 2014-10-31 06:57:13 +0000 | [diff] [blame] | 6726 | StorageClass SC, |
| Kaelyn Uhrain | e1a9ff7 | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6727 | bool &IsVirtualOkay) { |
| 6728 | DeclarationNameInfo NameInfo = SemaRef.GetNameForDeclarator(D); |
| 6729 | DeclarationName Name = NameInfo.getName(); |
| 6730 | |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6731 | FunctionDecl *NewFD = nullptr; |
| Kaelyn Uhrain | e1a9ff7 | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6732 | bool isInline = D.getDeclSpec().isInlineSpecified(); |
| Kaelyn Uhrain | e1a9ff7 | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6733 | |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6734 | if (!SemaRef.getLangOpts().CPlusPlus) { |
| Kaelyn Uhrain | e1a9ff7 | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6735 | // Determine whether the function was written with a |
| 6736 | // prototype. This true when: |
| 6737 | // - there is a prototype in the declarator, or |
| 6738 | // - the type R of the function is some kind of typedef or other reference |
| 6739 | // to a type name (which eventually refers to a function type). |
| 6740 | bool HasPrototype = |
| 6741 | (D.isFunctionDeclarator() && D.getFunctionTypeInfo().hasPrototype) || |
| 6742 | (!isa<FunctionType>(R.getTypePtr()) && R->isFunctionProtoType()); |
| 6743 | |
| David Blaikie | 30d1544 | 2011-10-19 22:56:21 +0000 | [diff] [blame] | 6744 | NewFD = FunctionDecl::Create(SemaRef.Context, DC, |
| Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 6745 | D.getLocStart(), NameInfo, R, |
| Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 6746 | TInfo, SC, isInline, |
| 6747 | HasPrototype, false); |
| Kaelyn Uhrain | e1a9ff7 | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6748 | if (D.isInvalidType()) |
| 6749 | NewFD->setInvalidDecl(); |
| 6750 | |
| Kaelyn Uhrain | e1a9ff7 | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6751 | return NewFD; |
| 6752 | } |
| 6753 | |
| 6754 | bool isExplicit = D.getDeclSpec().isExplicitSpecified(); |
| 6755 | bool isConstexpr = D.getDeclSpec().isConstexprSpecified(); |
| 6756 | |
| 6757 | // Check that the return type is not an abstract class type. |
| 6758 | // For record types, this is done by the AbstractClassUsageDiagnoser once |
| 6759 | // the class has been completely parsed. |
| 6760 | if (!DC->isRecord() && |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 6761 | SemaRef.RequireNonAbstractType( |
| 6762 | D.getIdentifierLoc(), R->getAs<FunctionType>()->getReturnType(), |
| 6763 | diag::err_abstract_type_in_decl, SemaRef.AbstractReturnType)) |
| Kaelyn Uhrain | e1a9ff7 | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6764 | D.setInvalidType(); |
| 6765 | |
| 6766 | if (Name.getNameKind() == DeclarationName::CXXConstructorName) { |
| 6767 | // This is a C++ constructor declaration. |
| 6768 | assert(DC->isRecord() && |
| 6769 | "Constructors can only be declared in a member context"); |
| 6770 | |
| 6771 | R = SemaRef.CheckConstructorDeclarator(D, R, SC); |
| 6772 | return CXXConstructorDecl::Create(SemaRef.Context, cast<CXXRecordDecl>(DC), |
| Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 6773 | D.getLocStart(), NameInfo, |
| Kaelyn Uhrain | e1a9ff7 | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6774 | R, TInfo, isExplicit, isInline, |
| 6775 | /*isImplicitlyDeclared=*/false, |
| 6776 | isConstexpr); |
| 6777 | |
| 6778 | } else if (Name.getNameKind() == DeclarationName::CXXDestructorName) { |
| 6779 | // This is a C++ destructor declaration. |
| 6780 | if (DC->isRecord()) { |
| 6781 | R = SemaRef.CheckDestructorDeclarator(D, R, SC); |
| 6782 | CXXRecordDecl *Record = cast<CXXRecordDecl>(DC); |
| 6783 | CXXDestructorDecl *NewDD = CXXDestructorDecl::Create( |
| 6784 | SemaRef.Context, Record, |
| Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 6785 | D.getLocStart(), |
| Kaelyn Uhrain | e1a9ff7 | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6786 | NameInfo, R, TInfo, isInline, |
| 6787 | /*isImplicitlyDeclared=*/false); |
| 6788 | |
| 6789 | // If the class is complete, then we now create the implicit exception |
| 6790 | // specification. If the class is incomplete or dependent, we can't do |
| 6791 | // it yet. |
| Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6792 | if (SemaRef.getLangOpts().CPlusPlus11 && !Record->isDependentType() && |
| Kaelyn Uhrain | e1a9ff7 | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6793 | Record->getDefinition() && !Record->isBeingDefined() && |
| 6794 | R->getAs<FunctionProtoType>()->getExceptionSpecType() == EST_None) { |
| 6795 | SemaRef.AdjustDestructorExceptionSpec(Record, NewDD); |
| 6796 | } |
| 6797 | |
| 6798 | IsVirtualOkay = true; |
| 6799 | return NewDD; |
| 6800 | |
| 6801 | } else { |
| 6802 | SemaRef.Diag(D.getIdentifierLoc(), diag::err_destructor_not_member); |
| 6803 | D.setInvalidType(); |
| 6804 | |
| 6805 | // Create a FunctionDecl to satisfy the function definition parsing |
| 6806 | // code path. |
| 6807 | return FunctionDecl::Create(SemaRef.Context, DC, |
| Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 6808 | D.getLocStart(), |
| Kaelyn Uhrain | e1a9ff7 | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6809 | D.getIdentifierLoc(), Name, R, TInfo, |
| Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 6810 | SC, isInline, |
| Kaelyn Uhrain | e1a9ff7 | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6811 | /*hasPrototype=*/true, isConstexpr); |
| 6812 | } |
| 6813 | |
| 6814 | } else if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName) { |
| 6815 | if (!DC->isRecord()) { |
| 6816 | SemaRef.Diag(D.getIdentifierLoc(), |
| 6817 | diag::err_conv_function_not_member); |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6818 | return nullptr; |
| Kaelyn Uhrain | e1a9ff7 | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6819 | } |
| 6820 | |
| 6821 | SemaRef.CheckConversionDeclarator(D, R, SC); |
| 6822 | IsVirtualOkay = true; |
| 6823 | return CXXConversionDecl::Create(SemaRef.Context, cast<CXXRecordDecl>(DC), |
| Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 6824 | D.getLocStart(), NameInfo, |
| Kaelyn Uhrain | e1a9ff7 | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6825 | R, TInfo, isInline, isExplicit, |
| 6826 | isConstexpr, SourceLocation()); |
| 6827 | |
| 6828 | } else if (DC->isRecord()) { |
| 6829 | // If the name of the function is the same as the name of the record, |
| 6830 | // then this must be an invalid constructor that has a return type. |
| 6831 | // (The parser checks for a return type and makes the declarator a |
| 6832 | // constructor if it has no return type). |
| 6833 | if (Name.getAsIdentifierInfo() && |
| 6834 | Name.getAsIdentifierInfo() == cast<CXXRecordDecl>(DC)->getIdentifier()){ |
| 6835 | SemaRef.Diag(D.getIdentifierLoc(), diag::err_constructor_return_type) |
| 6836 | << SourceRange(D.getDeclSpec().getTypeSpecTypeLoc()) |
| 6837 | << SourceRange(D.getIdentifierLoc()); |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6838 | return nullptr; |
| Kaelyn Uhrain | e1a9ff7 | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6839 | } |
| 6840 | |
| Kaelyn Uhrain | e1a9ff7 | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6841 | // This is a C++ method declaration. |
| Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 6842 | CXXMethodDecl *Ret = CXXMethodDecl::Create(SemaRef.Context, |
| 6843 | cast<CXXRecordDecl>(DC), |
| 6844 | D.getLocStart(), NameInfo, R, |
| 6845 | TInfo, SC, isInline, |
| 6846 | isConstexpr, SourceLocation()); |
| 6847 | IsVirtualOkay = !Ret->isStatic(); |
| 6848 | return Ret; |
| Kaelyn Uhrain | e1a9ff7 | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6849 | } else { |
| David Majnemer | aa30dd7 | 2015-01-09 07:36:13 +0000 | [diff] [blame] | 6850 | bool isFriend = |
| 6851 | SemaRef.getLangOpts().CPlusPlus && D.getDeclSpec().isFriendSpecified(); |
| 6852 | if (!isFriend && SemaRef.CurContext->isRecord()) |
| 6853 | return nullptr; |
| 6854 | |
| Kaelyn Uhrain | e1a9ff7 | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6855 | // Determine whether the function was written with a |
| 6856 | // prototype. This true when: |
| 6857 | // - we're in C++ (where every function has a prototype), |
| 6858 | return FunctionDecl::Create(SemaRef.Context, DC, |
| Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 6859 | D.getLocStart(), |
| Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 6860 | NameInfo, R, TInfo, SC, isInline, |
| Kaelyn Uhrain | e1a9ff7 | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6861 | true/*HasPrototype*/, isConstexpr); |
| 6862 | } |
| 6863 | } |
| 6864 | |
| Matt Arsenault | efb3819 | 2013-07-23 01:23:36 +0000 | [diff] [blame] | 6865 | enum OpenCLParamType { |
| 6866 | ValidKernelParam, |
| 6867 | PtrPtrKernelParam, |
| 6868 | PtrKernelParam, |
| David Tweed | ababa8f | 2014-03-27 16:34:11 +0000 | [diff] [blame] | 6869 | PrivatePtrKernelParam, |
| Matt Arsenault | efb3819 | 2013-07-23 01:23:36 +0000 | [diff] [blame] | 6870 | InvalidKernelParam, |
| 6871 | RecordKernelParam |
| 6872 | }; |
| 6873 | |
| 6874 | static OpenCLParamType getOpenCLKernelParameterType(QualType PT) { |
| 6875 | if (PT->isPointerType()) { |
| 6876 | QualType PointeeType = PT->getPointeeType(); |
| David Tweed | ababa8f | 2014-03-27 16:34:11 +0000 | [diff] [blame] | 6877 | if (PointeeType->isPointerType()) |
| 6878 | return PtrPtrKernelParam; |
| 6879 | return PointeeType.getAddressSpace() == 0 ? PrivatePtrKernelParam |
| 6880 | : PtrKernelParam; |
| Matt Arsenault | efb3819 | 2013-07-23 01:23:36 +0000 | [diff] [blame] | 6881 | } |
| 6882 | |
| 6883 | // TODO: Forbid the other integer types (size_t, ptrdiff_t...) when they can |
| 6884 | // be used as builtin types. |
| 6885 | |
| 6886 | if (PT->isImageType()) |
| 6887 | return PtrKernelParam; |
| 6888 | |
| 6889 | if (PT->isBooleanType()) |
| 6890 | return InvalidKernelParam; |
| 6891 | |
| 6892 | if (PT->isEventT()) |
| 6893 | return InvalidKernelParam; |
| 6894 | |
| 6895 | if (PT->isHalfType()) |
| 6896 | return InvalidKernelParam; |
| 6897 | |
| 6898 | if (PT->isRecordType()) |
| 6899 | return RecordKernelParam; |
| 6900 | |
| 6901 | return ValidKernelParam; |
| 6902 | } |
| 6903 | |
| 6904 | static void checkIsValidOpenCLKernelParameter( |
| 6905 | Sema &S, |
| 6906 | Declarator &D, |
| 6907 | ParmVarDecl *Param, |
| Craig Topper | 4dd9b43 | 2014-08-17 23:49:53 +0000 | [diff] [blame] | 6908 | llvm::SmallPtrSetImpl<const Type *> &ValidTypes) { |
| Matt Arsenault | efb3819 | 2013-07-23 01:23:36 +0000 | [diff] [blame] | 6909 | QualType PT = Param->getType(); |
| 6910 | |
| 6911 | // Cache the valid types we encounter to avoid rechecking structs that are |
| 6912 | // used again |
| 6913 | if (ValidTypes.count(PT.getTypePtr())) |
| 6914 | return; |
| 6915 | |
| 6916 | switch (getOpenCLKernelParameterType(PT)) { |
| 6917 | case PtrPtrKernelParam: |
| 6918 | // OpenCL v1.2 s6.9.a: |
| 6919 | // A kernel function argument cannot be declared as a |
| 6920 | // pointer to a pointer type. |
| 6921 | S.Diag(Param->getLocation(), diag::err_opencl_ptrptr_kernel_param); |
| 6922 | D.setInvalidType(); |
| 6923 | return; |
| 6924 | |
| David Tweed | ababa8f | 2014-03-27 16:34:11 +0000 | [diff] [blame] | 6925 | case PrivatePtrKernelParam: |
| 6926 | // OpenCL v1.2 s6.9.a: |
| 6927 | // A kernel function argument cannot be declared as a |
| 6928 | // pointer to the private address space. |
| 6929 | S.Diag(Param->getLocation(), diag::err_opencl_private_ptr_kernel_param); |
| 6930 | D.setInvalidType(); |
| 6931 | return; |
| 6932 | |
| Matt Arsenault | efb3819 | 2013-07-23 01:23:36 +0000 | [diff] [blame] | 6933 | // OpenCL v1.2 s6.9.k: |
| 6934 | // Arguments to kernel functions in a program cannot be declared with the |
| 6935 | // built-in scalar types bool, half, size_t, ptrdiff_t, intptr_t, and |
| 6936 | // uintptr_t or a struct and/or union that contain fields declared to be |
| 6937 | // one of these built-in scalar types. |
| 6938 | |
| 6939 | case InvalidKernelParam: |
| 6940 | // OpenCL v1.2 s6.8 n: |
| 6941 | // A kernel function argument cannot be declared |
| 6942 | // of event_t type. |
| 6943 | S.Diag(Param->getLocation(), diag::err_bad_kernel_param_type) << PT; |
| 6944 | D.setInvalidType(); |
| 6945 | return; |
| 6946 | |
| 6947 | case PtrKernelParam: |
| 6948 | case ValidKernelParam: |
| 6949 | ValidTypes.insert(PT.getTypePtr()); |
| 6950 | return; |
| 6951 | |
| 6952 | case RecordKernelParam: |
| 6953 | break; |
| 6954 | } |
| 6955 | |
| 6956 | // Track nested structs we will inspect |
| 6957 | SmallVector<const Decl *, 4> VisitStack; |
| 6958 | |
| 6959 | // Track where we are in the nested structs. Items will migrate from |
| 6960 | // VisitStack to HistoryStack as we do the DFS for bad field. |
| 6961 | SmallVector<const FieldDecl *, 4> HistoryStack; |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6962 | HistoryStack.push_back(nullptr); |
| Matt Arsenault | efb3819 | 2013-07-23 01:23:36 +0000 | [diff] [blame] | 6963 | |
| 6964 | const RecordDecl *PD = PT->castAs<RecordType>()->getDecl(); |
| 6965 | VisitStack.push_back(PD); |
| 6966 | |
| 6967 | assert(VisitStack.back() && "First decl null?"); |
| 6968 | |
| 6969 | do { |
| 6970 | const Decl *Next = VisitStack.pop_back_val(); |
| 6971 | if (!Next) { |
| 6972 | assert(!HistoryStack.empty()); |
| 6973 | // Found a marker, we have gone up a level |
| 6974 | if (const FieldDecl *Hist = HistoryStack.pop_back_val()) |
| 6975 | ValidTypes.insert(Hist->getType().getTypePtr()); |
| 6976 | |
| 6977 | continue; |
| 6978 | } |
| 6979 | |
| 6980 | // Adds everything except the original parameter declaration (which is not a |
| 6981 | // field itself) to the history stack. |
| 6982 | const RecordDecl *RD; |
| 6983 | if (const FieldDecl *Field = dyn_cast<FieldDecl>(Next)) { |
| 6984 | HistoryStack.push_back(Field); |
| 6985 | RD = Field->getType()->castAs<RecordType>()->getDecl(); |
| 6986 | } else { |
| 6987 | RD = cast<RecordDecl>(Next); |
| 6988 | } |
| 6989 | |
| 6990 | // Add a null marker so we know when we've gone back up a level |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6991 | VisitStack.push_back(nullptr); |
| Matt Arsenault | efb3819 | 2013-07-23 01:23:36 +0000 | [diff] [blame] | 6992 | |
| Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 6993 | for (const auto *FD : RD->fields()) { |
| Matt Arsenault | efb3819 | 2013-07-23 01:23:36 +0000 | [diff] [blame] | 6994 | QualType QT = FD->getType(); |
| 6995 | |
| 6996 | if (ValidTypes.count(QT.getTypePtr())) |
| 6997 | continue; |
| 6998 | |
| 6999 | OpenCLParamType ParamType = getOpenCLKernelParameterType(QT); |
| 7000 | if (ParamType == ValidKernelParam) |
| 7001 | continue; |
| 7002 | |
| 7003 | if (ParamType == RecordKernelParam) { |
| 7004 | VisitStack.push_back(FD); |
| 7005 | continue; |
| 7006 | } |
| 7007 | |
| 7008 | // OpenCL v1.2 s6.9.p: |
| 7009 | // Arguments to kernel functions that are declared to be a struct or union |
| 7010 | // do not allow OpenCL objects to be passed as elements of the struct or |
| 7011 | // union. |
| David Tweed | ababa8f | 2014-03-27 16:34:11 +0000 | [diff] [blame] | 7012 | if (ParamType == PtrKernelParam || ParamType == PtrPtrKernelParam || |
| 7013 | ParamType == PrivatePtrKernelParam) { |
| Matt Arsenault | efb3819 | 2013-07-23 01:23:36 +0000 | [diff] [blame] | 7014 | S.Diag(Param->getLocation(), |
| 7015 | diag::err_record_with_pointers_kernel_param) |
| 7016 | << PT->isUnionType() |
| 7017 | << PT; |
| 7018 | } else { |
| 7019 | S.Diag(Param->getLocation(), diag::err_bad_kernel_param_type) << PT; |
| 7020 | } |
| 7021 | |
| 7022 | S.Diag(PD->getLocation(), diag::note_within_field_of_type) |
| 7023 | << PD->getDeclName(); |
| 7024 | |
| 7025 | // We have an error, now let's go back up through history and show where |
| 7026 | // the offending field came from |
| Nico Weber | 6b05f38 | 2015-02-18 04:53:03 +0000 | [diff] [blame] | 7027 | for (ArrayRef<const FieldDecl *>::const_iterator |
| 7028 | I = HistoryStack.begin() + 1, |
| 7029 | E = HistoryStack.end(); |
| 7030 | I != E; ++I) { |
| Matt Arsenault | efb3819 | 2013-07-23 01:23:36 +0000 | [diff] [blame] | 7031 | const FieldDecl *OuterField = *I; |
| 7032 | S.Diag(OuterField->getLocation(), diag::note_within_field_of_type) |
| 7033 | << OuterField->getType(); |
| 7034 | } |
| 7035 | |
| 7036 | S.Diag(FD->getLocation(), diag::note_illegal_field_declared_here) |
| 7037 | << QT->isPointerType() |
| 7038 | << QT; |
| 7039 | D.setInvalidType(); |
| 7040 | return; |
| 7041 | } |
| 7042 | } while (!VisitStack.empty()); |
| 7043 | } |
| 7044 | |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7045 | NamedDecl* |
| Nick Lewycky | 0bdf13e | 2011-07-02 02:05:12 +0000 | [diff] [blame] | 7046 | Sema::ActOnFunctionDeclarator(Scope *S, Declarator &D, DeclContext *DC, |
| Kaelyn Uhrain | 4dc695d | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 7047 | TypeSourceInfo *TInfo, LookupResult &Previous, |
| Douglas Gregor | b52fabb | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 7048 | MultiTemplateParamsArg TemplateParamLists, |
| Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 7049 | bool &AddToScope) { |
| Kaelyn Uhrain | 4dc695d | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 7050 | QualType R = TInfo->getType(); |
| 7051 | |
| Zhongxing Xu | bece5d6 | 2009-01-16 01:13:29 +0000 | [diff] [blame] | 7052 | assert(R.getTypePtr()->isFunctionType()); |
| 7053 | |
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 7054 | // TODO: consider using NameInfo for diagnostic. |
| 7055 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); |
| 7056 | DeclarationName Name = NameInfo.getName(); |
| Craig Topper | c79e5e3 | 2014-10-31 06:57:13 +0000 | [diff] [blame] | 7057 | StorageClass SC = getFunctionStorageClass(*this, D); |
| Zhongxing Xu | bece5d6 | 2009-01-16 01:13:29 +0000 | [diff] [blame] | 7058 | |
| Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 7059 | if (DeclSpec::TSCS TSCS = D.getDeclSpec().getThreadStorageClassSpec()) |
| 7060 | Diag(D.getDeclSpec().getThreadStorageClassSpecLoc(), |
| 7061 | diag::err_invalid_thread) |
| 7062 | << DeclSpec::getSpecifierName(TSCS); |
| Eli Friedman | d5c0eed | 2009-04-19 20:27:55 +0000 | [diff] [blame] | 7063 | |
| Reid Kleckner | 9a7f3e6 | 2013-10-08 00:58:57 +0000 | [diff] [blame] | 7064 | if (D.isFirstDeclarationOfMember()) |
| 7065 | adjustMemberFunctionCC(R, D.isStaticMember()); |
| Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 7066 | |
| Douglas Gregor | 513e63c | 2010-12-10 19:28:19 +0000 | [diff] [blame] | 7067 | bool isFriend = false; |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7068 | FunctionTemplateDecl *FunctionTemplate = nullptr; |
| Douglas Gregor | 513e63c | 2010-12-10 19:28:19 +0000 | [diff] [blame] | 7069 | bool isExplicitSpecialization = false; |
| 7070 | bool isFunctionTemplateSpecialization = false; |
| Nico Weber | 7b5a716 | 2012-06-25 17:21:05 +0000 | [diff] [blame] | 7071 | |
| Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 7072 | bool isDependentClassScopeExplicitSpecialization = false; |
| Nico Weber | 7b5a716 | 2012-06-25 17:21:05 +0000 | [diff] [blame] | 7073 | bool HasExplicitTemplateArgs = false; |
| 7074 | TemplateArgumentListInfo TemplateArgs; |
| 7075 | |
| Kaelyn Uhrain | e1a9ff7 | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 7076 | bool isVirtualOkay = false; |
| Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 7077 | |
| Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 7078 | DeclContext *OriginalDC = DC; |
| 7079 | bool IsLocalExternDecl = adjustContextForLocalExternDecl(DC); |
| 7080 | |
| Kaelyn Uhrain | e1a9ff7 | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 7081 | FunctionDecl *NewFD = CreateNewFunctionDecl(*this, D, DC, R, TInfo, SC, |
| 7082 | isVirtualOkay); |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7083 | if (!NewFD) return nullptr; |
| Kaelyn Uhrain | e1a9ff7 | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 7084 | |
| Argyrios Kyrtzidis | 8ad3bab | 2011-11-23 20:27:36 +0000 | [diff] [blame] | 7085 | if (OriginalLexicalContext && OriginalLexicalContext->isObjCContainer()) |
| 7086 | NewFD->setTopLevelDeclInObjCContainer(); |
| 7087 | |
| Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 7088 | // Set the lexical context. If this is a function-scope declaration, or has a |
| 7089 | // C++ scope specifier, or is the object of a friend declaration, the lexical |
| 7090 | // context will be different from the semantic context. |
| 7091 | NewFD->setLexicalDeclContext(CurContext); |
| 7092 | |
| 7093 | if (IsLocalExternDecl) |
| 7094 | NewFD->setLocalExternDecl(); |
| 7095 | |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7096 | if (getLangOpts().CPlusPlus) { |
| Kaelyn Uhrain | e1a9ff7 | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 7097 | bool isInline = D.getDeclSpec().isInlineSpecified(); |
| Douglas Gregor | 513e63c | 2010-12-10 19:28:19 +0000 | [diff] [blame] | 7098 | bool isVirtual = D.getDeclSpec().isVirtualSpecified(); |
| 7099 | bool isExplicit = D.getDeclSpec().isExplicitSpecified(); |
| Richard Smith | a77a0a6 | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 7100 | bool isConstexpr = D.getDeclSpec().isConstexprSpecified(); |
| Kaelyn Uhrain | e1a9ff7 | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 7101 | isFriend = D.getDeclSpec().isFriendSpecified(); |
| Kaelyn Uhrain | 4dc695d | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 7102 | if (isFriend && !isInline && D.isFunctionDefinition()) { |
| Abramo Bagnara | a3088e6 | 2011-03-18 15:21:59 +0000 | [diff] [blame] | 7103 | // C++ [class.friend]p5 |
| 7104 | // A function can be defined in a friend declaration of a |
| 7105 | // class . . . . Such a function is implicitly inline. |
| 7106 | NewFD->setImplicitlyInline(); |
| 7107 | } |
| 7108 | |
| John McCall | db632ac | 2012-09-25 07:32:39 +0000 | [diff] [blame] | 7109 | // If this is a method defined in an __interface, and is not a constructor |
| 7110 | // or an overloaded operator, then set the pure flag (isVirtual will already |
| 7111 | // return true). |
| 7112 | if (const CXXRecordDecl *Parent = |
| 7113 | dyn_cast<CXXRecordDecl>(NewFD->getDeclContext())) { |
| 7114 | if (Parent->isInterface() && cast<CXXMethodDecl>(NewFD)->isUserProvided()) |
| Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 7115 | NewFD->setPure(true); |
| 7116 | } |
| 7117 | |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7118 | SetNestedNameSpecifier(NewFD, D); |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7119 | isExplicitSpecialization = false; |
| 7120 | isFunctionTemplateSpecialization = false; |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7121 | if (D.isInvalidType()) |
| 7122 | NewFD->setInvalidDecl(); |
| Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 7123 | |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7124 | // Match up the template parameter lists with the scope specifier, then |
| 7125 | // determine whether we have a template or a template specialization. |
| 7126 | bool Invalid = false; |
| Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 7127 | if (TemplateParameterList *TemplateParams = |
| 7128 | MatchTemplateParametersToScopeSpecifier( |
| 7129 | D.getDeclSpec().getLocStart(), D.getIdentifierLoc(), |
| Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 7130 | D.getCXXScopeSpec(), |
| 7131 | D.getName().getKind() == UnqualifiedId::IK_TemplateId |
| 7132 | ? D.getName().TemplateId |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7133 | : nullptr, |
| Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 7134 | TemplateParamLists, isFriend, isExplicitSpecialization, |
| 7135 | Invalid)) { |
| Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 7136 | if (TemplateParams->size() > 0) { |
| 7137 | // This is a function template |
| Abramo Bagnara | da41d0c | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 7138 | |
| Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 7139 | // Check that we can declare a template here. |
| 7140 | if (CheckTemplateDeclScope(S, TemplateParams)) |
| David Majnemer | 8e1a913 | 2015-01-15 07:04:38 +0000 | [diff] [blame] | 7141 | NewFD->setInvalidDecl(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7142 | |
| Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 7143 | // A destructor cannot be a template. |
| 7144 | if (Name.getNameKind() == DeclarationName::CXXDestructorName) { |
| 7145 | Diag(NewFD->getLocation(), diag::err_destructor_template); |
| David Majnemer | 8e1a913 | 2015-01-15 07:04:38 +0000 | [diff] [blame] | 7146 | NewFD->setInvalidDecl(); |
| John McCall | 1f0479e | 2010-03-24 08:27:58 +0000 | [diff] [blame] | 7147 | } |
| Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 7148 | |
| 7149 | // 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] | 7150 | // rebuilding some of the types used within the template parameter list, |
| Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 7151 | // now that we know what the current instantiation is. |
| 7152 | if (DC->isDependentContext()) { |
| 7153 | ContextRAII SavedContext(*this, DC); |
| 7154 | if (RebuildTemplateParamsInCurrentInstantiation(TemplateParams)) |
| 7155 | Invalid = true; |
| 7156 | } |
| 7157 | |
| John McCall | 1f0479e | 2010-03-24 08:27:58 +0000 | [diff] [blame] | 7158 | |
| Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 7159 | FunctionTemplate = FunctionTemplateDecl::Create(Context, DC, |
| 7160 | NewFD->getLocation(), |
| 7161 | Name, TemplateParams, |
| 7162 | NewFD); |
| 7163 | FunctionTemplate->setLexicalDeclContext(CurContext); |
| 7164 | NewFD->setDescribedFunctionTemplate(FunctionTemplate); |
| 7165 | |
| 7166 | // For source fidelity, store the other template param lists. |
| 7167 | if (TemplateParamLists.size() > 1) { |
| 7168 | NewFD->setTemplateParameterListsInfo(Context, |
| 7169 | TemplateParamLists.size() - 1, |
| Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 7170 | TemplateParamLists.data()); |
| Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 7171 | } |
| 7172 | } else { |
| 7173 | // This is a function template specialization. |
| 7174 | isFunctionTemplateSpecialization = true; |
| 7175 | // For source fidelity, store all the template param lists. |
| Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 7176 | if (TemplateParamLists.size() > 0) |
| 7177 | NewFD->setTemplateParameterListsInfo(Context, |
| 7178 | TemplateParamLists.size(), |
| 7179 | TemplateParamLists.data()); |
| Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 7180 | |
| 7181 | // C++0x [temp.expl.spec]p20 forbids "template<> friend void foo(int);". |
| 7182 | if (isFriend) { |
| 7183 | // We want to remove the "template<>", found here. |
| 7184 | SourceRange RemoveRange = TemplateParams->getSourceRange(); |
| 7185 | |
| 7186 | // If we remove the template<> and the name is not a |
| 7187 | // template-id, we're actually silently creating a problem: |
| 7188 | // the friend declaration will refer to an untemplated decl, |
| 7189 | // and clearly the user wants a template specialization. So |
| 7190 | // we need to insert '<>' after the name. |
| 7191 | SourceLocation InsertLoc; |
| 7192 | if (D.getName().getKind() != UnqualifiedId::IK_TemplateId) { |
| 7193 | InsertLoc = D.getName().getSourceRange().getEnd(); |
| Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 7194 | InsertLoc = getLocForEndOfToken(InsertLoc); |
| Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 7195 | } |
| 7196 | |
| 7197 | Diag(D.getIdentifierLoc(), diag::err_template_spec_decl_friend) |
| 7198 | << Name << RemoveRange |
| 7199 | << FixItHint::CreateRemoval(RemoveRange) |
| 7200 | << FixItHint::CreateInsertion(InsertLoc, "<>"); |
| 7201 | } |
| 7202 | } |
| 7203 | } |
| 7204 | else { |
| 7205 | // All template param lists were matched against the scope specifier: |
| 7206 | // this is NOT (an explicit specialization of) a template. |
| 7207 | if (TemplateParamLists.size() > 0) |
| 7208 | // For source fidelity, store all the template param lists. |
| 7209 | NewFD->setTemplateParameterListsInfo(Context, |
| 7210 | TemplateParamLists.size(), |
| Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 7211 | TemplateParamLists.data()); |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7212 | } |
| 7213 | |
| 7214 | if (Invalid) { |
| 7215 | NewFD->setInvalidDecl(); |
| 7216 | if (FunctionTemplate) |
| 7217 | FunctionTemplate->setInvalidDecl(); |
| 7218 | } |
| Kaelyn Uhrain | e1a9ff7 | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 7219 | |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7220 | // C++ [dcl.fct.spec]p5: |
| 7221 | // The virtual specifier shall only be used in declarations of |
| 7222 | // nonstatic class member functions that appear within a |
| 7223 | // member-specification of a class declaration; see 10.3. |
| 7224 | // |
| 7225 | if (isVirtual && !NewFD->isInvalidDecl()) { |
| 7226 | if (!isVirtualOkay) { |
| 7227 | Diag(D.getDeclSpec().getVirtualSpecLoc(), |
| 7228 | diag::err_virtual_non_function); |
| 7229 | } else if (!CurContext->isRecord()) { |
| 7230 | // 'virtual' was specified outside of the class. |
| Anders Carlsson | e973899 | 2011-01-22 14:43:56 +0000 | [diff] [blame] | 7231 | Diag(D.getDeclSpec().getVirtualSpecLoc(), |
| 7232 | diag::err_virtual_out_of_class) |
| 7233 | << FixItHint::CreateRemoval(D.getDeclSpec().getVirtualSpecLoc()); |
| 7234 | } else if (NewFD->getDescribedFunctionTemplate()) { |
| 7235 | // C++ [temp.mem]p3: |
| 7236 | // A member function template shall not be virtual. |
| 7237 | Diag(D.getDeclSpec().getVirtualSpecLoc(), |
| 7238 | diag::err_virtual_member_function_template) |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7239 | << FixItHint::CreateRemoval(D.getDeclSpec().getVirtualSpecLoc()); |
| 7240 | } else { |
| 7241 | // Okay: Add virtual to the method. |
| 7242 | NewFD->setVirtualAsWritten(true); |
| John McCall | 816d75b | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 7243 | } |
| Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 7244 | |
| Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 7245 | if (getLangOpts().CPlusPlus14 && |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 7246 | NewFD->getReturnType()->isUndeducedType()) |
| Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 7247 | Diag(D.getDeclSpec().getVirtualSpecLoc(), diag::err_auto_fn_virtual); |
| Douglas Gregor | c1da0f0 | 2009-06-24 00:23:40 +0000 | [diff] [blame] | 7248 | } |
| Abramo Bagnara | da41d0c | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 7249 | |
| Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 7250 | if (getLangOpts().CPlusPlus14 && |
| Richard Smith | c156470 | 2013-11-15 02:58:23 +0000 | [diff] [blame] | 7251 | (NewFD->isDependentContext() || |
| 7252 | (isFriend && CurContext->isDependentContext())) && |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 7253 | NewFD->getReturnType()->isUndeducedType()) { |
| Richard Smith | c58f38f | 2013-08-14 20:16:31 +0000 | [diff] [blame] | 7254 | // If the function template is referenced directly (for instance, as a |
| 7255 | // member of the current instantiation), pretend it has a dependent type. |
| 7256 | // This is not really justified by the standard, but is the only sane |
| 7257 | // thing to do. |
| Richard Smith | c156470 | 2013-11-15 02:58:23 +0000 | [diff] [blame] | 7258 | // FIXME: For a friend function, we have not marked the function as being |
| 7259 | // a friend yet, so 'isDependentContext' on the FD doesn't work. |
| Richard Smith | c58f38f | 2013-08-14 20:16:31 +0000 | [diff] [blame] | 7260 | const FunctionProtoType *FPT = |
| 7261 | NewFD->getType()->castAs<FunctionProtoType>(); |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 7262 | QualType Result = |
| 7263 | SubstAutoType(FPT->getReturnType(), Context.DependentTy); |
| Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 7264 | NewFD->setType(Context.getFunctionType(Result, FPT->getParamTypes(), |
| Richard Smith | c58f38f | 2013-08-14 20:16:31 +0000 | [diff] [blame] | 7265 | FPT->getExtProtoInfo())); |
| 7266 | } |
| 7267 | |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7268 | // C++ [dcl.fct.spec]p3: |
| David Blaikie | 30d1544 | 2011-10-19 22:56:21 +0000 | [diff] [blame] | 7269 | // The inline specifier shall not appear on a block scope function |
| 7270 | // declaration. |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7271 | if (isInline && !NewFD->isInvalidDecl()) { |
| 7272 | if (CurContext->isFunctionOrMethod()) { |
| 7273 | // 'inline' is not allowed on block scope function declaration. |
| 7274 | Diag(D.getDeclSpec().getInlineSpecLoc(), |
| 7275 | diag::err_inline_declaration_block_scope) << Name |
| 7276 | << FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc()); |
| 7277 | } |
| 7278 | } |
| Kaelyn Uhrain | e1a9ff7 | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 7279 | |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7280 | // C++ [dcl.fct.spec]p6: |
| 7281 | // The explicit specifier shall be used only in the declaration of a |
| David Blaikie | 30d1544 | 2011-10-19 22:56:21 +0000 | [diff] [blame] | 7282 | // constructor or conversion function within its class definition; |
| 7283 | // see 12.3.1 and 12.3.2. |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7284 | if (isExplicit && !NewFD->isInvalidDecl()) { |
| 7285 | if (!CurContext->isRecord()) { |
| 7286 | // 'explicit' was specified outside of the class. |
| 7287 | Diag(D.getDeclSpec().getExplicitSpecLoc(), |
| 7288 | diag::err_explicit_out_of_class) |
| 7289 | << FixItHint::CreateRemoval(D.getDeclSpec().getExplicitSpecLoc()); |
| 7290 | } else if (!isa<CXXConstructorDecl>(NewFD) && |
| 7291 | !isa<CXXConversionDecl>(NewFD)) { |
| 7292 | // 'explicit' was specified on a function that wasn't a constructor |
| 7293 | // or conversion function. |
| 7294 | Diag(D.getDeclSpec().getExplicitSpecLoc(), |
| 7295 | diag::err_explicit_non_ctor_or_conv_function) |
| 7296 | << FixItHint::CreateRemoval(D.getDeclSpec().getExplicitSpecLoc()); |
| 7297 | } |
| 7298 | } |
| Abramo Bagnara | da41d0c | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 7299 | |
| Richard Smith | a77a0a6 | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 7300 | if (isConstexpr) { |
| Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 7301 | // C++11 [dcl.constexpr]p2: constexpr functions and constexpr constructors |
| Richard Smith | a77a0a6 | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 7302 | // are implicitly inline. |
| 7303 | NewFD->setImplicitlyInline(); |
| 7304 | |
| Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 7305 | // C++11 [dcl.constexpr]p3: functions declared constexpr are required to |
| Richard Smith | a77a0a6 | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 7306 | // be either constructors or to return a literal type. Therefore, |
| 7307 | // destructors cannot be declared constexpr. |
| 7308 | if (isa<CXXDestructorDecl>(NewFD)) |
| Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 7309 | Diag(D.getDeclSpec().getConstexprSpecLoc(), diag::err_constexpr_dtor); |
| Richard Smith | a77a0a6 | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 7310 | } |
| 7311 | |
| Douglas Gregor | 26701a4 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 7312 | // If __module_private__ was specified, mark the function accordingly. |
| 7313 | if (D.getDeclSpec().isModulePrivateSpecified()) { |
| Douglas Gregor | 3c7cd6a | 2011-09-09 20:53:38 +0000 | [diff] [blame] | 7314 | if (isFunctionTemplateSpecialization) { |
| 7315 | SourceLocation ModulePrivateLoc |
| 7316 | = D.getDeclSpec().getModulePrivateSpecLoc(); |
| 7317 | Diag(ModulePrivateLoc, diag::err_module_private_specialization) |
| 7318 | << 0 |
| 7319 | << FixItHint::CreateRemoval(ModulePrivateLoc); |
| 7320 | } else { |
| 7321 | NewFD->setModulePrivate(); |
| 7322 | if (FunctionTemplate) |
| 7323 | FunctionTemplate->setModulePrivate(); |
| 7324 | } |
| Douglas Gregor | 26701a4 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 7325 | } |
| Richard Smith | a77a0a6 | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 7326 | |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7327 | if (isFriend) { |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7328 | if (FunctionTemplate) { |
| Richard Smith | 6401768 | 2013-07-17 23:53:16 +0000 | [diff] [blame] | 7329 | FunctionTemplate->setObjectOfFriendDecl(); |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7330 | FunctionTemplate->setAccess(AS_public); |
| 7331 | } |
| Richard Smith | 6401768 | 2013-07-17 23:53:16 +0000 | [diff] [blame] | 7332 | NewFD->setObjectOfFriendDecl(); |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7333 | NewFD->setAccess(AS_public); |
| 7334 | } |
| 7335 | |
| Douglas Gregor | 5d1b4e3 | 2011-11-07 20:56:01 +0000 | [diff] [blame] | 7336 | // 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] | 7337 | // FIXME: Does this ever happen? ActOnStartOfFunctionDef forces the function |
| 7338 | // definition kind to FDK_Definition. |
| Douglas Gregor | 5d1b4e3 | 2011-11-07 20:56:01 +0000 | [diff] [blame] | 7339 | switch (D.getFunctionDefinitionKind()) { |
| 7340 | case FDK_Declaration: |
| 7341 | case FDK_Definition: |
| 7342 | break; |
| 7343 | |
| 7344 | case FDK_Defaulted: |
| 7345 | NewFD->setDefaulted(); |
| 7346 | break; |
| 7347 | |
| 7348 | case FDK_Deleted: |
| 7349 | NewFD->setDeletedAsWritten(); |
| 7350 | break; |
| 7351 | } |
| 7352 | |
| Kaelyn Uhrain | 4dc695d | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 7353 | if (isa<CXXMethodDecl>(NewFD) && DC == CurContext && |
| 7354 | D.isFunctionDefinition()) { |
| Douglas Gregor | 5d1b4e3 | 2011-11-07 20:56:01 +0000 | [diff] [blame] | 7355 | // C++ [class.mfct]p2: |
| 7356 | // A member function may be defined (8.4) in its class definition, in |
| 7357 | // which case it is an inline member function (7.1.2) |
| John McCall | 357d0f3 | 2010-12-15 04:00:32 +0000 | [diff] [blame] | 7358 | NewFD->setImplicitlyInline(); |
| 7359 | } |
| 7360 | |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7361 | if (SC == SC_Static && isa<CXXMethodDecl>(NewFD) && |
| 7362 | !CurContext->isRecord()) { |
| 7363 | // C++ [class.static]p1: |
| 7364 | // A data or function member of a class may be declared static |
| 7365 | // in a class definition, in which case it is a static member of |
| 7366 | // the class. |
| 7367 | |
| 7368 | // Complain about the 'static' specifier if it's on an out-of-line |
| 7369 | // member function definition. |
| 7370 | Diag(D.getDeclSpec().getStorageClassSpecLoc(), |
| 7371 | diag::err_static_out_of_line) |
| 7372 | << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc()); |
| 7373 | } |
| Richard Smith | 66f3ac9 | 2012-10-20 08:26:51 +0000 | [diff] [blame] | 7374 | |
| 7375 | // C++11 [except.spec]p15: |
| 7376 | // A deallocation function with no exception-specification is treated |
| 7377 | // as if it were specified with noexcept(true). |
| 7378 | const FunctionProtoType *FPT = R->getAs<FunctionProtoType>(); |
| 7379 | if ((Name.getCXXOverloadedOperator() == OO_Delete || |
| 7380 | Name.getCXXOverloadedOperator() == OO_Array_Delete) && |
| Richard Smith | 8acb428 | 2014-07-31 21:57:55 +0000 | [diff] [blame] | 7381 | getLangOpts().CPlusPlus11 && FPT && !FPT->hasExceptionSpec()) |
| 7382 | NewFD->setType(Context.getFunctionType( |
| 7383 | FPT->getReturnType(), FPT->getParamTypes(), |
| 7384 | FPT->getExtProtoInfo().withExceptionSpec(EST_BasicNoexcept))); |
| Douglas Gregor | 5f0e252 | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 7385 | } |
| Kaelyn Uhrain | e1a9ff7 | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 7386 | |
| 7387 | // Filter out previous declarations that don't match the scope. |
| Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 7388 | FilterLookupForScope(Previous, OriginalDC, S, shouldConsiderLinkage(NewFD), |
| Richard Smith | 72bcaec | 2013-12-05 04:30:04 +0000 | [diff] [blame] | 7389 | D.getCXXScopeSpec().isNotEmpty() || |
| Kaelyn Uhrain | e1a9ff7 | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 7390 | isExplicitSpecialization || |
| 7391 | isFunctionTemplateSpecialization); |
| Richard Smith | 1c34fb7 | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 7392 | |
| Zhongxing Xu | bece5d6 | 2009-01-16 01:13:29 +0000 | [diff] [blame] | 7393 | // Handle GNU asm-label extension (encoded as an attribute). |
| 7394 | if (Expr *E = (Expr*) D.getAsmLabel()) { |
| 7395 | // The parser guarantees this is a string. |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7396 | StringLiteral *SE = cast<StringLiteral>(E); |
| Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 7397 | NewFD->addAttr(::new (Context) AsmLabelAttr(SE->getStrTokenLoc(0), Context, |
| Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 7398 | SE->getString(), 0)); |
| David Chisnall | 0867d9c | 2012-02-18 16:12:34 +0000 | [diff] [blame] | 7399 | } else if (!ExtnameUndeclaredIdentifiers.empty()) { |
| 7400 | llvm::DenseMap<IdentifierInfo*,AsmLabelAttr*>::iterator I = |
| 7401 | ExtnameUndeclaredIdentifiers.find(NewFD->getIdentifier()); |
| 7402 | if (I != ExtnameUndeclaredIdentifiers.end()) { |
| 7403 | NewFD->addAttr(I->second); |
| 7404 | ExtnameUndeclaredIdentifiers.erase(I); |
| 7405 | } |
| Zhongxing Xu | bece5d6 | 2009-01-16 01:13:29 +0000 | [diff] [blame] | 7406 | } |
| 7407 | |
| Chris Lattner | 9af40c1 | 2009-04-25 06:12:16 +0000 | [diff] [blame] | 7408 | // Copy the parameter declarations from the declarator D to the function |
| 7409 | // declaration NewFD, if they are available. First scavenge them into Params. |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7410 | SmallVector<ParmVarDecl*, 16> Params; |
| Abramo Bagnara | 6d81063 | 2010-12-14 22:11:44 +0000 | [diff] [blame] | 7411 | if (D.isFunctionDeclarator()) { |
| Abramo Bagnara | 924a8f3 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 7412 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo(); |
| Zhongxing Xu | bece5d6 | 2009-01-16 01:13:29 +0000 | [diff] [blame] | 7413 | |
| Zhongxing Xu | bece5d6 | 2009-01-16 01:13:29 +0000 | [diff] [blame] | 7414 | // Check for C99 6.7.5.3p10 - foo(void) is a non-varargs |
| 7415 | // function that takes no arguments, not a function that takes a |
| 7416 | // single void argument. |
| 7417 | // We let through "const void" here because Sema::GetTypeForDeclarator |
| 7418 | // already checks for that case. |
| Alp Toker | 4284c6e | 2014-05-11 16:05:55 +0000 | [diff] [blame] | 7419 | if (FTIHasNonVoidParameters(FTI) && FTI.Params[0].Param) { |
| Alp Toker | c535072 | 2014-02-26 22:27:52 +0000 | [diff] [blame] | 7420 | for (unsigned i = 0, e = FTI.NumParams; i != e; ++i) { |
| 7421 | ParmVarDecl *Param = cast<ParmVarDecl>(FTI.Params[i].Param); |
| Argyrios Kyrtzidis | 11a846a | 2009-07-14 03:18:53 +0000 | [diff] [blame] | 7422 | assert(Param->getDeclContext() != NewFD && "Was set before ?"); |
| 7423 | Param->setDeclContext(NewFD); |
| 7424 | Params.push_back(Param); |
| John McCall | b723860 | 2010-04-14 01:27:20 +0000 | [diff] [blame] | 7425 | |
| 7426 | if (Param->isInvalidDecl()) |
| 7427 | NewFD->setInvalidDecl(); |
| Argyrios Kyrtzidis | 11a846a | 2009-07-14 03:18:53 +0000 | [diff] [blame] | 7428 | } |
| Zhongxing Xu | bece5d6 | 2009-01-16 01:13:29 +0000 | [diff] [blame] | 7429 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7430 | |
| John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 7431 | } else if (const FunctionProtoType *FT = R->getAs<FunctionProtoType>()) { |
| Chris Lattner | 47c0d00 | 2009-04-25 06:03:53 +0000 | [diff] [blame] | 7432 | // 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] | 7433 | // following example, we'll need to synthesize (unnamed) |
| 7434 | // parameters for use in the declaration. |
| 7435 | // |
| 7436 | // @code |
| 7437 | // typedef void fn(int); |
| 7438 | // fn f; |
| 7439 | // @endcode |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7440 | |
| Chris Lattner | 47c0d00 | 2009-04-25 06:03:53 +0000 | [diff] [blame] | 7441 | // Synthesize a parameter for each argument type. |
| Aaron Ballman | 40bd0aa | 2014-03-17 15:23:01 +0000 | [diff] [blame] | 7442 | for (const auto &AI : FT->param_types()) { |
| John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 7443 | ParmVarDecl *Param = |
| Aaron Ballman | 40bd0aa | 2014-03-17 15:23:01 +0000 | [diff] [blame] | 7444 | BuildParmVarDeclForTypedef(NewFD, D.getIdentifierLoc(), AI); |
| John McCall | 8fb0d9d | 2011-05-01 22:35:37 +0000 | [diff] [blame] | 7445 | Param->setScopeInfo(0, Params.size()); |
| Chris Lattner | 47c0d00 | 2009-04-25 06:03:53 +0000 | [diff] [blame] | 7446 | Params.push_back(Param); |
| Zhongxing Xu | bece5d6 | 2009-01-16 01:13:29 +0000 | [diff] [blame] | 7447 | } |
| Chris Lattner | 49303b2 | 2009-04-25 18:38:18 +0000 | [diff] [blame] | 7448 | } else { |
| 7449 | assert(R->isFunctionNoProtoType() && NewFD->getNumParams() == 0 && |
| 7450 | "Should not need args for typedef of non-prototype fn"); |
| Zhongxing Xu | bece5d6 | 2009-01-16 01:13:29 +0000 | [diff] [blame] | 7451 | } |
| Anton Korobeynikov | f0c267e | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 7452 | |
| Chris Lattner | 9af40c1 | 2009-04-25 06:12:16 +0000 | [diff] [blame] | 7453 | // Finally, we know we have the right number of parameters, install them. |
| David Blaikie | 9c70e04 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 7454 | NewFD->setParams(Params); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7455 | |
| James Molloy | 6f8780b | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 7456 | // Find all anonymous symbols defined during the declaration of this function |
| 7457 | // and add to NewFD. This lets us track decls such 'enum Y' in: |
| 7458 | // |
| 7459 | // void f(enum Y {AA} x) {} |
| 7460 | // |
| 7461 | // which would otherwise incorrectly end up in the translation unit scope. |
| 7462 | NewFD->setDeclsInPrototypeScope(DeclsInPrototypeScope); |
| 7463 | DeclsInPrototypeScope.clear(); |
| 7464 | |
| Richard Smith | debc59d | 2013-01-30 05:45:05 +0000 | [diff] [blame] | 7465 | if (D.getDeclSpec().isNoreturnSpecified()) |
| 7466 | NewFD->addAttr( |
| 7467 | ::new(Context) C11NoReturnAttr(D.getDeclSpec().getNoreturnSpecLoc(), |
| Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 7468 | Context, 0)); |
| Richard Smith | debc59d | 2013-01-30 05:45:05 +0000 | [diff] [blame] | 7469 | |
| Richard Smith | 84208dc | 2012-03-13 05:56:40 +0000 | [diff] [blame] | 7470 | // Functions returning a variably modified type violate C99 6.7.5.2p2 |
| 7471 | // because all functions have linkage. |
| 7472 | if (!NewFD->isInvalidDecl() && |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 7473 | NewFD->getReturnType()->isVariablyModifiedType()) { |
| Richard Smith | 84208dc | 2012-03-13 05:56:40 +0000 | [diff] [blame] | 7474 | Diag(NewFD->getLocation(), diag::err_vm_func_decl); |
| 7475 | NewFD->setInvalidDecl(); |
| 7476 | } |
| 7477 | |
| Reid Kleckner | 2a13322 | 2015-03-04 23:39:17 +0000 | [diff] [blame] | 7478 | // Apply an implicit SectionAttr if #pragma code_seg is active. |
| 7479 | if (CodeSegStack.CurrentValue && D.isFunctionDefinition() && |
| Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 7480 | !NewFD->hasAttr<SectionAttr>()) { |
| 7481 | NewFD->addAttr( |
| 7482 | SectionAttr::CreateImplicit(Context, SectionAttr::Declspec_allocate, |
| 7483 | CodeSegStack.CurrentValue->getString(), |
| 7484 | CodeSegStack.CurrentPragmaLocation)); |
| 7485 | if (UnifySection(CodeSegStack.CurrentValue->getString(), |
| Hans Wennborg | 899ded9 | 2014-10-16 20:52:46 +0000 | [diff] [blame] | 7486 | ASTContext::PSF_Implicit | ASTContext::PSF_Execute | |
| 7487 | ASTContext::PSF_Read, |
| 7488 | NewFD)) |
| Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 7489 | NewFD->dropAttr<SectionAttr>(); |
| 7490 | } |
| 7491 | |
| Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 7492 | // Handle attributes. |
| Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 7493 | ProcessDeclAttributes(S, NewFD, D); |
| Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 7494 | |
| Joey Gouly | 16cb99d | 2014-01-06 11:26:18 +0000 | [diff] [blame] | 7495 | if (getLangOpts().OpenCL) { |
| 7496 | // OpenCL v1.1 s6.5: Using an address space qualifier in a function return |
| 7497 | // type declaration will generate a compilation error. |
| Kaelyn Takata | 0a2e84c | 2015-04-09 19:43:04 +0000 | [diff] [blame] | 7498 | unsigned AddressSpace = NewFD->getReturnType().getAddressSpace(); |
| Joey Gouly | 16cb99d | 2014-01-06 11:26:18 +0000 | [diff] [blame] | 7499 | if (AddressSpace == LangAS::opencl_local || |
| 7500 | AddressSpace == LangAS::opencl_global || |
| 7501 | AddressSpace == LangAS::opencl_constant) { |
| 7502 | Diag(NewFD->getLocation(), |
| 7503 | diag::err_opencl_return_value_with_address_space); |
| 7504 | NewFD->setInvalidDecl(); |
| 7505 | } |
| 7506 | } |
| 7507 | |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7508 | if (!getLangOpts().CPlusPlus) { |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7509 | // Perform semantic checking on the function declaration. |
| Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 7510 | bool isExplicitSpecialization=false; |
| David Majnemer | 027f9c4 | 2013-07-06 02:13:46 +0000 | [diff] [blame] | 7511 | if (!NewFD->isInvalidDecl() && NewFD->isMain()) |
| 7512 | CheckMain(NewFD, D.getDeclSpec()); |
| 7513 | |
| David Majnemer | c729b0b | 2013-09-16 22:44:20 +0000 | [diff] [blame] | 7514 | if (!NewFD->isInvalidDecl() && NewFD->isMSVCRTEntryPoint()) |
| 7515 | CheckMSVCRTEntryPoint(NewFD); |
| 7516 | |
| Reid Kleckner | 899baf3 | 2014-11-03 21:56:03 +0000 | [diff] [blame] | 7517 | if (!NewFD->isInvalidDecl()) |
| 7518 | D.setRedeclaration(CheckFunctionDeclaration(S, NewFD, Previous, |
| 7519 | isExplicitSpecialization)); |
| 7520 | else if (!Previous.empty()) |
| Nico Weber | 4486d61 | 2015-02-18 05:19:40 +0000 | [diff] [blame] | 7521 | // Recover gracefully from an invalid redeclaration. |
| Reid Kleckner | 899baf3 | 2014-11-03 21:56:03 +0000 | [diff] [blame] | 7522 | D.setRedeclaration(true); |
| 7523 | assert((NewFD->isInvalidDecl() || !D.isRedeclaration() || |
| 7524 | Previous.getResultKind() != LookupResult::FoundOverloaded) && |
| 7525 | "previous declaration set still overloaded"); |
| 7526 | |
| Reid Kleckner | 1eaa844 | 2014-11-03 21:24:50 +0000 | [diff] [blame] | 7527 | // Diagnose no-prototype function declarations with calling conventions that |
| Reid Kleckner | 899baf3 | 2014-11-03 21:56:03 +0000 | [diff] [blame] | 7528 | // don't support variadic calls. Only do this in C and do it after merging |
| 7529 | // possibly prototyped redeclarations. |
| 7530 | const FunctionType *FT = NewFD->getType()->castAs<FunctionType>(); |
| 7531 | if (isa<FunctionNoProtoType>(FT) && !D.isFunctionDefinition()) { |
| Reid Kleckner | 1eaa844 | 2014-11-03 21:24:50 +0000 | [diff] [blame] | 7532 | CallingConv CC = FT->getExtInfo().getCC(); |
| 7533 | if (!supportsVariadicCall(CC)) { |
| 7534 | // Windows system headers sometimes accidentally use stdcall without |
| 7535 | // (void) parameters, so we relax this to a warning. |
| 7536 | int DiagID = |
| 7537 | CC == CC_X86StdCall ? diag::warn_cconv_knr : diag::err_cconv_knr; |
| 7538 | Diag(NewFD->getLocation(), DiagID) |
| 7539 | << FunctionType::getNameForCallConv(CC); |
| 7540 | } |
| 7541 | } |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7542 | } else { |
| Richard Smith | fa27bc4 | 2013-11-16 01:57:09 +0000 | [diff] [blame] | 7543 | // C++11 [replacement.functions]p3: |
| 7544 | // The program's definitions shall not be specified as inline. |
| 7545 | // |
| 7546 | // N.B. We diagnose declarations instead of definitions per LWG issue 2340. |
| 7547 | // |
| 7548 | // Suppress the diagnostic if the function is __attribute__((used)), since |
| 7549 | // that forces an external definition to be emitted. |
| 7550 | if (D.getDeclSpec().isInlineSpecified() && |
| 7551 | NewFD->isReplaceableGlobalAllocationFunction() && |
| 7552 | !NewFD->hasAttr<UsedAttr>()) |
| 7553 | Diag(D.getDeclSpec().getInlineSpecLoc(), |
| 7554 | diag::ext_operator_new_delete_declared_inline) |
| 7555 | << NewFD->getDeclName(); |
| 7556 | |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7557 | // If the declarator is a template-id, translate the parser's template |
| 7558 | // argument list into our AST format. |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7559 | if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) { |
| 7560 | TemplateIdAnnotation *TemplateId = D.getName().TemplateId; |
| 7561 | TemplateArgs.setLAngleLoc(TemplateId->LAngleLoc); |
| 7562 | TemplateArgs.setRAngleLoc(TemplateId->RAngleLoc); |
| Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 7563 | ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(), |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7564 | TemplateId->NumArgs); |
| 7565 | translateTemplateArguments(TemplateArgsPtr, |
| 7566 | TemplateArgs); |
| Douglas Gregor | 0e876e0 | 2009-09-25 23:53:26 +0000 | [diff] [blame] | 7567 | |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7568 | HasExplicitTemplateArgs = true; |
| Douglas Gregor | 0e876e0 | 2009-09-25 23:53:26 +0000 | [diff] [blame] | 7569 | |
| Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 7570 | if (NewFD->isInvalidDecl()) { |
| 7571 | HasExplicitTemplateArgs = false; |
| 7572 | } else if (FunctionTemplate) { |
| Douglas Gregor | a5f6f9c | 2011-01-24 18:54:39 +0000 | [diff] [blame] | 7573 | // Function template with explicit template arguments. |
| 7574 | Diag(D.getIdentifierLoc(), diag::err_function_template_partial_spec) |
| 7575 | << SourceRange(TemplateId->LAngleLoc, TemplateId->RAngleLoc); |
| 7576 | |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7577 | HasExplicitTemplateArgs = false; |
| John McCall | f7cfb22 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 7578 | } else { |
| Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 7579 | assert((isFunctionTemplateSpecialization || |
| 7580 | D.getDeclSpec().isFriendSpecified()) && |
| 7581 | "should have a 'template<>' for this decl"); |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7582 | // "friend void foo<>(int);" is an implicit specialization decl. |
| 7583 | isFunctionTemplateSpecialization = true; |
| Francois Pichet | 6d76e6c | 2010-10-01 21:19:28 +0000 | [diff] [blame] | 7584 | } |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7585 | } else if (isFriend && isFunctionTemplateSpecialization) { |
| 7586 | // This combination is only possible in a recovery case; the user |
| 7587 | // wrote something like: |
| 7588 | // template <> friend void foo(int); |
| 7589 | // which we're recovering from as if the user had written: |
| 7590 | // friend void foo<>(int); |
| 7591 | // Go ahead and fake up a template id. |
| 7592 | HasExplicitTemplateArgs = true; |
| Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 7593 | TemplateArgs.setLAngleLoc(D.getIdentifierLoc()); |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7594 | TemplateArgs.setRAngleLoc(D.getIdentifierLoc()); |
| Douglas Gregor | f4f296d | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 7595 | } |
| John McCall | f7cfb22 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 7596 | |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7597 | // If it's a friend (and only if it's a friend), it's possible |
| 7598 | // that either the specialized function type or the specialized |
| 7599 | // template is dependent, and therefore matching will fail. In |
| 7600 | // this case, don't check the specialization yet. |
| Douglas Gregor | e9d075e | 2011-10-09 20:59:17 +0000 | [diff] [blame] | 7601 | bool InstantiationDependent = false; |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7602 | if (isFunctionTemplateSpecialization && isFriend && |
| Douglas Gregor | e9d075e | 2011-10-09 20:59:17 +0000 | [diff] [blame] | 7603 | (NewFD->getType()->isDependentType() || DC->isDependentContext() || |
| 7604 | TemplateSpecializationType::anyDependentTemplateArguments( |
| 7605 | TemplateArgs.getArgumentArray(), TemplateArgs.size(), |
| 7606 | InstantiationDependent))) { |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7607 | assert(HasExplicitTemplateArgs && |
| 7608 | "friend function specialization without template args"); |
| 7609 | if (CheckDependentFunctionTemplateSpecialization(NewFD, TemplateArgs, |
| 7610 | Previous)) |
| 7611 | NewFD->setInvalidDecl(); |
| 7612 | } else if (isFunctionTemplateSpecialization) { |
| Douglas Gregor | 63fab34 | 2011-03-16 19:27:09 +0000 | [diff] [blame] | 7613 | if (CurContext->isDependentContext() && CurContext->isRecord() |
| Francois Pichet | b503705 | 2011-06-03 13:59:45 +0000 | [diff] [blame] | 7614 | && !isFriend) { |
| David Majnemer | aa30dd7 | 2015-01-09 07:36:13 +0000 | [diff] [blame] | 7615 | isDependentClassScopeExplicitSpecialization = true; |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7616 | Diag(NewFD->getLocation(), getLangOpts().MicrosoftExt ? |
| Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 7617 | diag::ext_function_specialization_in_class : |
| 7618 | diag::err_function_specialization_in_class) |
| Douglas Gregor | 63fab34 | 2011-03-16 19:27:09 +0000 | [diff] [blame] | 7619 | << NewFD->getDeclName(); |
| Douglas Gregor | 63fab34 | 2011-03-16 19:27:09 +0000 | [diff] [blame] | 7620 | } else if (CheckFunctionTemplateSpecialization(NewFD, |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7621 | (HasExplicitTemplateArgs ? &TemplateArgs |
| 7622 | : nullptr), |
| Douglas Gregor | 63fab34 | 2011-03-16 19:27:09 +0000 | [diff] [blame] | 7623 | Previous)) |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7624 | NewFD->setInvalidDecl(); |
| Douglas Gregor | 781ba6e | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 7625 | |
| 7626 | // C++ [dcl.stc]p1: |
| 7627 | // A storage-class-specifier shall not be specified in an explicit |
| 7628 | // specialization (14.7.3) |
| Richard Trieu | b48e62f | 2013-05-16 02:14:08 +0000 | [diff] [blame] | 7629 | FunctionTemplateSpecializationInfo *Info = |
| 7630 | NewFD->getTemplateSpecializationInfo(); |
| 7631 | if (Info && SC != SC_None) { |
| 7632 | if (SC != Info->getTemplate()->getTemplatedDecl()->getStorageClass()) |
| Douglas Gregor | 84265a0 | 2011-06-17 05:09:08 +0000 | [diff] [blame] | 7633 | Diag(NewFD->getLocation(), |
| 7634 | diag::err_explicit_specialization_inconsistent_storage_class) |
| 7635 | << SC |
| 7636 | << FixItHint::CreateRemoval( |
| 7637 | D.getDeclSpec().getStorageClassSpecLoc()); |
| 7638 | |
| 7639 | else |
| 7640 | Diag(NewFD->getLocation(), |
| 7641 | diag::ext_explicit_specialization_storage_class) |
| 7642 | << FixItHint::CreateRemoval( |
| 7643 | D.getDeclSpec().getStorageClassSpecLoc()); |
| Douglas Gregor | 781ba6e | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 7644 | } |
| 7645 | |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7646 | } else if (isExplicitSpecialization && isa<CXXMethodDecl>(NewFD)) { |
| 7647 | if (CheckMemberSpecialization(NewFD, Previous)) |
| 7648 | NewFD->setInvalidDecl(); |
| 7649 | } |
| Douglas Gregor | f4f296d | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 7650 | |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7651 | // Perform semantic checking on the function declaration. |
| David Blaikie | d937bf1 | 2011-09-08 06:33:04 +0000 | [diff] [blame] | 7652 | if (!isDependentClassScopeExplicitSpecialization) { |
| David Majnemer | 027f9c4 | 2013-07-06 02:13:46 +0000 | [diff] [blame] | 7653 | if (!NewFD->isInvalidDecl() && NewFD->isMain()) |
| 7654 | CheckMain(NewFD, D.getDeclSpec()); |
| 7655 | |
| David Majnemer | c729b0b | 2013-09-16 22:44:20 +0000 | [diff] [blame] | 7656 | if (!NewFD->isInvalidDecl() && NewFD->isMSVCRTEntryPoint()) |
| 7657 | CheckMSVCRTEntryPoint(NewFD); |
| 7658 | |
| Nico Weber | 7607fce | 2013-12-21 00:49:51 +0000 | [diff] [blame] | 7659 | if (!NewFD->isInvalidDecl()) |
| Kaelyn Uhrain | 4dc695d | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 7660 | D.setRedeclaration(CheckFunctionDeclaration(S, NewFD, Previous, |
| 7661 | isExplicitSpecialization)); |
| Nico Weber | 4486d61 | 2015-02-18 05:19:40 +0000 | [diff] [blame] | 7662 | else if (!Previous.empty()) |
| 7663 | // Recover gracefully from an invalid redeclaration. |
| 7664 | D.setRedeclaration(true); |
| David Blaikie | d937bf1 | 2011-09-08 06:33:04 +0000 | [diff] [blame] | 7665 | } |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7666 | |
| Kaelyn Uhrain | 4dc695d | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 7667 | assert((NewFD->isInvalidDecl() || !D.isRedeclaration() || |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7668 | Previous.getResultKind() != LookupResult::FoundOverloaded) && |
| 7669 | "previous declaration set still overloaded"); |
| 7670 | |
| 7671 | NamedDecl *PrincipalDecl = (FunctionTemplate |
| 7672 | ? cast<NamedDecl>(FunctionTemplate) |
| 7673 | : NewFD); |
| 7674 | |
| Kaelyn Uhrain | 4dc695d | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 7675 | if (isFriend && D.isRedeclaration()) { |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7676 | AccessSpecifier Access = AS_public; |
| 7677 | if (!NewFD->isInvalidDecl()) |
| Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 7678 | Access = NewFD->getPreviousDecl()->getAccess(); |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7679 | |
| 7680 | NewFD->setAccess(Access); |
| 7681 | if (FunctionTemplate) FunctionTemplate->setAccess(Access); |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7682 | } |
| 7683 | |
| 7684 | if (NewFD->isOverloadedOperator() && !DC->isRecord() && |
| 7685 | PrincipalDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary)) |
| 7686 | PrincipalDecl->setNonMemberOperator(); |
| 7687 | |
| 7688 | // If we have a function template, check the template parameter |
| 7689 | // list. This will check and merge default template arguments. |
| 7690 | if (FunctionTemplate) { |
| David Blaikie | 30d1544 | 2011-10-19 22:56:21 +0000 | [diff] [blame] | 7691 | FunctionTemplateDecl *PrevTemplate = |
| Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 7692 | FunctionTemplate->getPreviousDecl(); |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7693 | CheckTemplateParameterList(FunctionTemplate->getTemplateParameters(), |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7694 | PrevTemplate ? PrevTemplate->getTemplateParameters() |
| 7695 | : nullptr, |
| Douglas Gregor | a99fb4c | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 7696 | D.getDeclSpec().isFriendSpecified() |
| Kaelyn Uhrain | 4dc695d | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 7697 | ? (D.isFunctionDefinition() |
| Douglas Gregor | a99fb4c | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 7698 | ? TPC_FriendFunctionTemplateDefinition |
| 7699 | : TPC_FriendFunctionTemplate) |
| 7700 | : (D.getCXXScopeSpec().isSet() && |
| Douglas Gregor | 4d5c297 | 2011-02-04 12:22:53 +0000 | [diff] [blame] | 7701 | DC && DC->isRecord() && |
| 7702 | DC->isDependentContext()) |
| Douglas Gregor | a99fb4c | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 7703 | ? TPC_ClassTemplateMember |
| 7704 | : TPC_FunctionTemplate); |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7705 | } |
| 7706 | |
| 7707 | if (NewFD->isInvalidDecl()) { |
| 7708 | // Ignore all the rest of this. |
| Kaelyn Uhrain | 4dc695d | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 7709 | } else if (!D.isRedeclaration()) { |
| Kaelyn Uhrain | 8af2c9f | 2011-10-11 00:28:52 +0000 | [diff] [blame] | 7710 | struct ActOnFDArgs ExtraArgs = { S, D, TemplateParamLists, |
| Kaelyn Uhrain | 0a32fca | 2011-10-11 00:28:39 +0000 | [diff] [blame] | 7711 | AddToScope }; |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7712 | // Fake up an access specifier if it's supposed to be a class member. |
| 7713 | if (isa<CXXRecordDecl>(NewFD->getDeclContext())) |
| 7714 | NewFD->setAccess(AS_public); |
| 7715 | |
| 7716 | // Qualified decls generally require a previous declaration. |
| 7717 | if (D.getCXXScopeSpec().isSet()) { |
| 7718 | // ...with the major exception of templated-scope or |
| 7719 | // dependent-scope friend declarations. |
| 7720 | |
| 7721 | // TODO: we currently also suppress this check in dependent |
| 7722 | // contexts because (1) the parameter depth will be off when |
| 7723 | // matching friend templates and (2) we might actually be |
| 7724 | // selecting a friend based on a dependent factor. But there |
| 7725 | // are situations where these conditions don't apply and we |
| 7726 | // can actually do this check immediately. |
| 7727 | if (isFriend && |
| Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 7728 | (TemplateParamLists.size() || |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7729 | D.getCXXScopeSpec().getScopeRep()->isDependent() || |
| 7730 | CurContext->isDependentContext())) { |
| Chandler Carruth | f8b554f | 2011-08-19 01:38:33 +0000 | [diff] [blame] | 7731 | // ignore these |
| 7732 | } else { |
| 7733 | // The user tried to provide an out-of-line definition for a |
| 7734 | // function that is a member of a class or namespace, but there |
| 7735 | // was no such member function declared (C++ [class.mfct]p2, |
| 7736 | // C++ [namespace.memdef]p2). For example: |
| 7737 | // |
| 7738 | // class X { |
| 7739 | // void f() const; |
| 7740 | // }; |
| 7741 | // |
| 7742 | // void X::f() { } // ill-formed |
| 7743 | // |
| 7744 | // Complain about this problem, and attempt to suggest close |
| 7745 | // matches (e.g., those that differ only in cv-qualifiers and |
| 7746 | // whether the parameter types are references). |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7747 | |
| Richard Smith | 114394f | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 7748 | if (NamedDecl *Result = DiagnoseInvalidRedeclaration( |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7749 | *this, Previous, NewFD, ExtraArgs, false, nullptr)) { |
| Kaelyn Uhrain | 0a32fca | 2011-10-11 00:28:39 +0000 | [diff] [blame] | 7750 | AddToScope = ExtraArgs.AddToScope; |
| 7751 | return Result; |
| 7752 | } |
| Chandler Carruth | f8b554f | 2011-08-19 01:38:33 +0000 | [diff] [blame] | 7753 | } |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7754 | |
| 7755 | // Unqualified local friend declarations are required to resolve |
| 7756 | // to something. |
| Chandler Carruth | e92e42f | 2011-08-19 01:40:11 +0000 | [diff] [blame] | 7757 | } else if (isFriend && cast<CXXRecordDecl>(CurContext)->isLocalClass()) { |
| Richard Smith | 114394f | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 7758 | if (NamedDecl *Result = DiagnoseInvalidRedeclaration( |
| 7759 | *this, Previous, NewFD, ExtraArgs, true, S)) { |
| Kaelyn Uhrain | 0a32fca | 2011-10-11 00:28:39 +0000 | [diff] [blame] | 7760 | AddToScope = ExtraArgs.AddToScope; |
| 7761 | return Result; |
| 7762 | } |
| Chandler Carruth | e92e42f | 2011-08-19 01:40:11 +0000 | [diff] [blame] | 7763 | } |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7764 | |
| Richard Smith | a230224 | 2013-12-05 07:51:02 +0000 | [diff] [blame] | 7765 | } else if (!D.isFunctionDefinition() && |
| 7766 | isa<CXXMethodDecl>(NewFD) && NewFD->isOutOfLine() && |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7767 | !isFriend && !isFunctionTemplateSpecialization && |
| Alexis Hunt | 5a7fa25 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 7768 | !isExplicitSpecialization) { |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7769 | // An out-of-line member function declaration must also be a |
| Richard Smith | a230224 | 2013-12-05 07:51:02 +0000 | [diff] [blame] | 7770 | // definition (C++ [class.mfct]p2). |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7771 | // Note that this is not the case for explicit specializations of |
| 7772 | // function templates or member functions of class templates, per |
| David Blaikie | 30d1544 | 2011-10-19 22:56:21 +0000 | [diff] [blame] | 7773 | // C++ [temp.expl.spec]p2. We also allow these declarations as an |
| 7774 | // extension for compatibility with old SWIG code which likes to |
| 7775 | // generate them. |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7776 | Diag(NewFD->getLocation(), diag::ext_out_of_line_declaration) |
| 7777 | << D.getCXXScopeSpec().getRange(); |
| 7778 | } |
| 7779 | } |
| Ryan Flynn | e5dc859 | 2009-07-25 22:29:44 +0000 | [diff] [blame] | 7780 | |
| Rafael Espindola | de6a39f | 2013-03-02 21:41:48 +0000 | [diff] [blame] | 7781 | ProcessPragmaWeak(S, NewFD); |
| Rafael Espindola | f1d2f0e | 2013-01-16 23:11:15 +0000 | [diff] [blame] | 7782 | checkAttributesAfterMerging(*this, *NewFD); |
| 7783 | |
| Douglas Gregor | f4f296d | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 7784 | AddKnownFunctionAttributes(NewFD); |
| 7785 | |
| Douglas Gregor | 7260905 | 2010-08-06 13:50:58 +0000 | [diff] [blame] | 7786 | if (NewFD->hasAttr<OverloadableAttr>() && |
| 7787 | !NewFD->getType()->getAs<FunctionProtoType>()) { |
| 7788 | Diag(NewFD->getLocation(), |
| 7789 | diag::err_attribute_overloadable_no_prototype) |
| 7790 | << NewFD; |
| 7791 | |
| 7792 | // Turn this into a variadic function with no parameters. |
| 7793 | const FunctionType *FT = NewFD->getType()->getAs<FunctionType>(); |
| Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 7794 | FunctionProtoType::ExtProtoInfo EPI( |
| 7795 | Context.getDefaultCallingConvention(true, false)); |
| John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 7796 | EPI.Variadic = true; |
| 7797 | EPI.ExtInfo = FT->getExtInfo(); |
| 7798 | |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 7799 | QualType R = Context.getFunctionType(FT->getReturnType(), None, EPI); |
| Douglas Gregor | 7260905 | 2010-08-06 13:50:58 +0000 | [diff] [blame] | 7800 | NewFD->setType(R); |
| 7801 | } |
| 7802 | |
| Eli Friedman | 570024a | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 7803 | // If there's a #pragma GCC visibility in scope, and this isn't a class |
| 7804 | // member, set the visibility of this function. |
| Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 7805 | if (!DC->isRecord() && NewFD->isExternallyVisible()) |
| Eli Friedman | 570024a | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 7806 | AddPushedVisibilityAttribute(NewFD); |
| 7807 | |
| John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 7808 | // If there's a #pragma clang arc_cf_code_audited in scope, consider |
| 7809 | // marking the function. |
| 7810 | AddCFAuditedAttribute(NewFD); |
| 7811 | |
| Dario Domizioli | 13a0a38 | 2014-05-23 12:13:25 +0000 | [diff] [blame] | 7812 | // If this is a function definition, check if we have to apply optnone due to |
| 7813 | // a pragma. |
| 7814 | if(D.isFunctionDefinition()) |
| 7815 | AddRangeBasedOptnone(NewFD); |
| 7816 | |
| Richard Smith | ac974a3 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 7817 | // If this is the first declaration of an extern C variable, update |
| 7818 | // the map of such variables. |
| Rafael Espindola | 3f9e444 | 2013-10-19 02:13:21 +0000 | [diff] [blame] | 7819 | if (NewFD->isFirstDecl() && !NewFD->isInvalidDecl() && |
| Richard Smith | ac974a3 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 7820 | isIncompleteDeclExternC(*this, NewFD)) |
| Richard Smith | 39b7968 | 2013-06-18 20:15:12 +0000 | [diff] [blame] | 7821 | RegisterLocallyScopedExternCDecl(NewFD, S); |
| Douglas Gregor | f4f296d | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 7822 | |
| Argyrios Kyrtzidis | 16eecc4 | 2009-06-25 18:22:24 +0000 | [diff] [blame] | 7823 | // Set this FunctionDecl's range up to the right paren. |
| Abramo Bagnara | ea94788 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 7824 | NewFD->setRangeEnd(D.getSourceRange().getEnd()); |
| Argyrios Kyrtzidis | 16eecc4 | 2009-06-25 18:22:24 +0000 | [diff] [blame] | 7825 | |
| Nico Rieck | 82f0b06 | 2014-03-31 14:56:15 +0000 | [diff] [blame] | 7826 | if (D.isRedeclaration() && !Previous.empty()) { |
| 7827 | checkDLLAttributeRedeclaration( |
| 7828 | *this, dyn_cast<NamedDecl>(Previous.getRepresentativeDecl()), NewFD, |
| 7829 | isExplicitSpecialization || isFunctionTemplateSpecialization); |
| 7830 | } |
| 7831 | |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7832 | if (getLangOpts().CPlusPlus) { |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7833 | if (FunctionTemplate) { |
| 7834 | if (NewFD->isInvalidDecl()) |
| 7835 | FunctionTemplate->setInvalidDecl(); |
| 7836 | return FunctionTemplate; |
| 7837 | } |
| Fariborz Jahanian | 31d6d84 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7838 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7839 | |
| Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 7840 | if (NewFD->hasAttr<OpenCLKernelAttr>()) { |
| Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 7841 | // OpenCL v1.2 s6.8 static is invalid for kernel functions. |
| 7842 | if ((getLangOpts().OpenCLVersion >= 120) |
| 7843 | && (SC == SC_Static)) { |
| 7844 | Diag(D.getIdentifierLoc(), diag::err_static_kernel); |
| 7845 | D.setInvalidType(); |
| 7846 | } |
| Tanya Lattner | 0f86433 | 2013-01-30 19:48:52 +0000 | [diff] [blame] | 7847 | |
| 7848 | // OpenCL v1.2, s6.9 -- Kernels can only have return type void. |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 7849 | if (!NewFD->getReturnType()->isVoidType()) { |
| Alp Toker | d0787eb | 2014-07-02 01:47:15 +0000 | [diff] [blame] | 7850 | SourceRange RTRange = NewFD->getReturnTypeSourceRange(); |
| 7851 | Diag(D.getIdentifierLoc(), diag::err_expected_kernel_void_return_type) |
| 7852 | << (RTRange.isValid() ? FixItHint::CreateReplacement(RTRange, "void") |
| 7853 | : FixItHint()); |
| Tanya Lattner | 0f86433 | 2013-01-30 19:48:52 +0000 | [diff] [blame] | 7854 | D.setInvalidType(); |
| 7855 | } |
| Matt Arsenault | efb3819 | 2013-07-23 01:23:36 +0000 | [diff] [blame] | 7856 | |
| 7857 | llvm::SmallPtrSet<const Type *, 16> ValidTypes; |
| Aaron Ballman | f6bf62e | 2014-03-07 15:12:56 +0000 | [diff] [blame] | 7858 | for (auto Param : NewFD->params()) |
| Matt Arsenault | efb3819 | 2013-07-23 01:23:36 +0000 | [diff] [blame] | 7859 | checkIsValidOpenCLKernelParameter(*this, D, Param, ValidTypes); |
| Tanya Lattner | 4fdce3f | 2012-06-19 23:09:52 +0000 | [diff] [blame] | 7860 | } |
| 7861 | |
| Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 7862 | MarkUnusedFileScopedDecl(NewFD); |
| Peter Collingbourne | 9e2c81f | 2011-02-09 21:04:32 +0000 | [diff] [blame] | 7863 | |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7864 | if (getLangOpts().CUDA) |
| Peter Collingbourne | 9e2c81f | 2011-02-09 21:04:32 +0000 | [diff] [blame] | 7865 | if (IdentifierInfo *II = NewFD->getIdentifier()) |
| 7866 | if (!NewFD->isInvalidDecl() && |
| 7867 | NewFD->getDeclContext()->getRedeclContext()->isTranslationUnit()) { |
| 7868 | if (II->isStr("cudaConfigureCall")) { |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 7869 | if (!R->getAs<FunctionType>()->getReturnType()->isScalarType()) |
| Peter Collingbourne | 9e2c81f | 2011-02-09 21:04:32 +0000 | [diff] [blame] | 7870 | Diag(NewFD->getLocation(), diag::err_config_scalar_return); |
| 7871 | |
| 7872 | Context.setcudaConfigureCallDecl(NewFD); |
| 7873 | } |
| 7874 | } |
| Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 7875 | |
| 7876 | // Here we have an function template explicit specialization at class scope. |
| 7877 | // The actually specialization will be postponed to template instatiation |
| 7878 | // time via the ClassScopeFunctionSpecializationDecl node. |
| 7879 | if (isDependentClassScopeExplicitSpecialization) { |
| 7880 | ClassScopeFunctionSpecializationDecl *NewSpec = |
| 7881 | ClassScopeFunctionSpecializationDecl::Create( |
| Nico Weber | 7b5a716 | 2012-06-25 17:21:05 +0000 | [diff] [blame] | 7882 | Context, CurContext, SourceLocation(), |
| 7883 | cast<CXXMethodDecl>(NewFD), |
| 7884 | HasExplicitTemplateArgs, TemplateArgs); |
| Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 7885 | CurContext->addDecl(NewSpec); |
| 7886 | AddToScope = false; |
| 7887 | } |
| Peter Collingbourne | 9e2c81f | 2011-02-09 21:04:32 +0000 | [diff] [blame] | 7888 | |
| Douglas Gregor | f4f296d | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 7889 | return NewFD; |
| 7890 | } |
| 7891 | |
| 7892 | /// \brief Perform semantic checking of a new function declaration. |
| 7893 | /// |
| 7894 | /// Performs semantic analysis of the new function declaration |
| 7895 | /// NewFD. This routine performs all semantic checking that does not |
| 7896 | /// require the actual declarator involved in the declaration, and is |
| 7897 | /// used both for the declaration of functions as they are parsed |
| 7898 | /// (called via ActOnDeclarator) and for the declaration of functions |
| 7899 | /// that have been instantiated via C++ template instantiation (called |
| 7900 | /// via InstantiateDecl). |
| 7901 | /// |
| James Dennett | ffad8b7 | 2012-06-22 08:10:18 +0000 | [diff] [blame] | 7902 | /// \param IsExplicitSpecialization whether this new function declaration is |
| Douglas Gregor | cf91555 | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 7903 | /// an explicit specialization of the previous declaration. |
| 7904 | /// |
| Chris Lattner | f6d1c9c | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 7905 | /// This sets NewFD->isInvalidDecl() to true if there was an error. |
| Kaelyn Uhrain | 4dc695d | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 7906 | /// |
| James Dennett | ffad8b7 | 2012-06-22 08:10:18 +0000 | [diff] [blame] | 7907 | /// \returns true if the function declaration is a redeclaration. |
| Kaelyn Uhrain | 4dc695d | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 7908 | bool Sema::CheckFunctionDeclaration(Scope *S, FunctionDecl *NewFD, |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7909 | LookupResult &Previous, |
| Kaelyn Uhrain | 4dc695d | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 7910 | bool IsExplicitSpecialization) { |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 7911 | assert(!NewFD->getReturnType()->isVariablyModifiedType() && |
| 7912 | "Variably modified return types are not handled here"); |
| John McCall | d9baf6a | 2009-07-24 03:03:21 +0000 | [diff] [blame] | 7913 | |
| Richard Smith | 1c34fb7 | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 7914 | // Determine whether the type of this function should be merged with |
| 7915 | // a previous visible declaration. This never happens for functions in C++, |
| 7916 | // and always happens in C if the previous declaration was visible. |
| 7917 | bool MergeTypeWithPrevious = !getLangOpts().CPlusPlus && |
| 7918 | !Previous.isShadowed(); |
| 7919 | |
| Douglas Gregor | 3552dab | 2013-01-09 00:47:56 +0000 | [diff] [blame] | 7920 | // Filter out any non-conflicting previous declarations. |
| 7921 | filterNonConflictingPreviousDecls(Context, NewFD, Previous); |
| 7922 | |
| Kaelyn Uhrain | 4dc695d | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 7923 | bool Redeclaration = false; |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7924 | NamedDecl *OldDecl = nullptr; |
| Kaelyn Uhrain | 4dc695d | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 7925 | |
| Douglas Gregor | e62c0a4 | 2009-02-24 01:23:02 +0000 | [diff] [blame] | 7926 | // Merge or overload the declaration with an existing declaration of |
| 7927 | // the same name, if appropriate. |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7928 | if (!Previous.empty()) { |
| Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 7929 | // Determine whether NewFD is an overload of PrevDecl or |
| Zhongxing Xu | bece5d6 | 2009-01-16 01:13:29 +0000 | [diff] [blame] | 7930 | // a declaration that requires merging. If it's an overload, |
| 7931 | // there's no more work to do here; we'll just add the new |
| 7932 | // function to the scope. |
| John McCall | daa3d6b | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 7933 | if (!AllowOverloadingOfFunction(Previous, Context)) { |
| Rafael Espindola | 5bddd6a | 2013-04-15 12:49:13 +0000 | [diff] [blame] | 7934 | NamedDecl *Candidate = Previous.getFoundDecl(); |
| 7935 | if (shouldLinkPossiblyHiddenDecl(Candidate, NewFD)) { |
| 7936 | Redeclaration = true; |
| 7937 | OldDecl = Candidate; |
| 7938 | } |
| John McCall | daa3d6b | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 7939 | } else { |
| John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 7940 | switch (CheckOverload(S, NewFD, Previous, OldDecl, |
| 7941 | /*NewIsUsingDecl*/ false)) { |
| John McCall | daa3d6b | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 7942 | case Ovl_Match: |
| John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7943 | Redeclaration = true; |
| John McCall | daa3d6b | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 7944 | break; |
| 7945 | |
| 7946 | case Ovl_NonFunction: |
| 7947 | Redeclaration = true; |
| 7948 | break; |
| 7949 | |
| 7950 | case Ovl_Overload: |
| 7951 | Redeclaration = false; |
| 7952 | break; |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7953 | } |
| Peter Collingbourne | 9f2a990 | 2011-01-21 02:08:54 +0000 | [diff] [blame] | 7954 | |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7955 | if (!getLangOpts().CPlusPlus && !NewFD->hasAttr<OverloadableAttr>()) { |
| Peter Collingbourne | 9f2a990 | 2011-01-21 02:08:54 +0000 | [diff] [blame] | 7956 | // If a function name is overloadable in C, then every function |
| 7957 | // with that name must be marked "overloadable". |
| 7958 | Diag(NewFD->getLocation(), diag::err_attribute_overloadable_missing) |
| 7959 | << Redeclaration << NewFD; |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7960 | NamedDecl *OverloadedDecl = nullptr; |
| Peter Collingbourne | 9f2a990 | 2011-01-21 02:08:54 +0000 | [diff] [blame] | 7961 | if (Redeclaration) |
| 7962 | OverloadedDecl = OldDecl; |
| 7963 | else if (!Previous.empty()) |
| 7964 | OverloadedDecl = Previous.getRepresentativeDecl(); |
| 7965 | if (OverloadedDecl) |
| 7966 | Diag(OverloadedDecl->getLocation(), |
| 7967 | diag::note_attribute_overloadable_prev_overload); |
| Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 7968 | NewFD->addAttr(OverloadableAttr::CreateImplicit(Context)); |
| Peter Collingbourne | 9f2a990 | 2011-01-21 02:08:54 +0000 | [diff] [blame] | 7969 | } |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7970 | } |
| Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 7971 | } |
| Zhongxing Xu | bece5d6 | 2009-01-16 01:13:29 +0000 | [diff] [blame] | 7972 | |
| Richard Smith | ac974a3 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 7973 | // Check for a previous extern "C" declaration with this name. |
| 7974 | if (!Redeclaration && |
| 7975 | checkForConflictWithNonVisibleExternC(*this, NewFD, Previous)) { |
| 7976 | filterNonConflictingPreviousDecls(Context, NewFD, Previous); |
| 7977 | if (!Previous.empty()) { |
| 7978 | // This is an extern "C" declaration with the same name as a previous |
| 7979 | // declaration, and thus redeclares that entity... |
| 7980 | Redeclaration = true; |
| 7981 | OldDecl = Previous.getFoundDecl(); |
| Richard Smith | 1c34fb7 | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 7982 | MergeTypeWithPrevious = false; |
| Richard Smith | ac974a3 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 7983 | |
| 7984 | // ... except in the presence of __attribute__((overloadable)). |
| 7985 | if (OldDecl->hasAttr<OverloadableAttr>()) { |
| 7986 | if (!getLangOpts().CPlusPlus && !NewFD->hasAttr<OverloadableAttr>()) { |
| 7987 | Diag(NewFD->getLocation(), diag::err_attribute_overloadable_missing) |
| 7988 | << Redeclaration << NewFD; |
| 7989 | Diag(Previous.getFoundDecl()->getLocation(), |
| 7990 | diag::note_attribute_overloadable_prev_overload); |
| Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 7991 | NewFD->addAttr(OverloadableAttr::CreateImplicit(Context)); |
| Richard Smith | ac974a3 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 7992 | } |
| 7993 | if (IsOverload(NewFD, cast<FunctionDecl>(OldDecl), false)) { |
| 7994 | Redeclaration = false; |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7995 | OldDecl = nullptr; |
| Richard Smith | ac974a3 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 7996 | } |
| 7997 | } |
| 7998 | } |
| 7999 | } |
| 8000 | |
| Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 8001 | // C++11 [dcl.constexpr]p8: |
| 8002 | // A constexpr specifier for a non-static member function that is not |
| 8003 | // a constructor declares that member function to be const. |
| 8004 | // |
| 8005 | // This needs to be delayed until we know whether this is an out-of-line |
| 8006 | // definition of a static member function. |
| Richard Smith | 034185c | 2013-04-21 01:08:50 +0000 | [diff] [blame] | 8007 | // |
| 8008 | // This rule is not present in C++1y, so we produce a backwards |
| 8009 | // compatibility warning whenever it happens in C++11. |
| Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 8010 | CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(NewFD); |
| Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 8011 | if (!getLangOpts().CPlusPlus14 && MD && MD->isConstexpr() && |
| Richard Smith | 034185c | 2013-04-21 01:08:50 +0000 | [diff] [blame] | 8012 | !MD->isStatic() && !isa<CXXConstructorDecl>(MD) && |
| Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 8013 | (MD->getTypeQualifiers() & Qualifiers::Const) == 0) { |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8014 | CXXMethodDecl *OldMD = nullptr; |
| Alp Toker | a2794f9 | 2014-01-22 07:29:52 +0000 | [diff] [blame] | 8015 | if (OldDecl) |
| David Majnemer | 0fe3f4d | 2015-01-09 10:33:23 +0000 | [diff] [blame] | 8016 | OldMD = dyn_cast_or_null<CXXMethodDecl>(OldDecl->getAsFunction()); |
| Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 8017 | if (!OldMD || !OldMD->isStatic()) { |
| 8018 | const FunctionProtoType *FPT = |
| 8019 | MD->getType()->castAs<FunctionProtoType>(); |
| 8020 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 8021 | EPI.TypeQuals |= Qualifiers::Const; |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 8022 | MD->setType(Context.getFunctionType(FPT->getReturnType(), |
| Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 8023 | FPT->getParamTypes(), EPI)); |
| Richard Smith | 034185c | 2013-04-21 01:08:50 +0000 | [diff] [blame] | 8024 | |
| 8025 | // Warn that we did this, if we're not performing template instantiation. |
| 8026 | // In that case, we'll have warned already when the template was defined. |
| 8027 | if (ActiveTemplateInstantiations.empty()) { |
| 8028 | SourceLocation AddConstLoc; |
| 8029 | if (FunctionTypeLoc FTL = MD->getTypeSourceInfo()->getTypeLoc() |
| 8030 | .IgnoreParens().getAs<FunctionTypeLoc>()) |
| Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 8031 | AddConstLoc = getLocForEndOfToken(FTL.getRParenLoc()); |
| Richard Smith | 034185c | 2013-04-21 01:08:50 +0000 | [diff] [blame] | 8032 | |
| Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 8033 | Diag(MD->getLocation(), diag::warn_cxx14_compat_constexpr_not_const) |
| Richard Smith | 034185c | 2013-04-21 01:08:50 +0000 | [diff] [blame] | 8034 | << FixItHint::CreateInsertion(AddConstLoc, " const"); |
| 8035 | } |
| Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 8036 | } |
| 8037 | } |
| 8038 | |
| 8039 | if (Redeclaration) { |
| 8040 | // NewFD and OldDecl represent declarations that need to be |
| 8041 | // merged. |
| Richard Smith | 1c34fb7 | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 8042 | if (MergeFunctionDecl(NewFD, OldDecl, S, MergeTypeWithPrevious)) { |
| Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 8043 | NewFD->setInvalidDecl(); |
| 8044 | return Redeclaration; |
| 8045 | } |
| 8046 | |
| 8047 | Previous.clear(); |
| 8048 | Previous.addDecl(OldDecl); |
| 8049 | |
| 8050 | if (FunctionTemplateDecl *OldTemplateDecl |
| 8051 | = dyn_cast<FunctionTemplateDecl>(OldDecl)) { |
| 8052 | NewFD->setPreviousDeclaration(OldTemplateDecl->getTemplatedDecl()); |
| 8053 | FunctionTemplateDecl *NewTemplateDecl |
| 8054 | = NewFD->getDescribedFunctionTemplate(); |
| 8055 | assert(NewTemplateDecl && "Template/non-template mismatch"); |
| 8056 | if (CXXMethodDecl *Method |
| 8057 | = dyn_cast<CXXMethodDecl>(NewTemplateDecl->getTemplatedDecl())) { |
| 8058 | Method->setAccess(OldTemplateDecl->getAccess()); |
| 8059 | NewTemplateDecl->setAccess(OldTemplateDecl->getAccess()); |
| Kaelyn Uhrain | 4dc695d | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 8060 | } |
| Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 8061 | |
| 8062 | // If this is an explicit specialization of a member that is a function |
| 8063 | // template, mark it as a member specialization. |
| 8064 | if (IsExplicitSpecialization && |
| 8065 | NewTemplateDecl->getInstantiatedFromMemberTemplate()) { |
| 8066 | NewTemplateDecl->setMemberSpecialization(); |
| 8067 | assert(OldTemplateDecl->isMemberSpecialization()); |
| Argyrios Kyrtzidis | 11a846a | 2009-07-14 03:18:53 +0000 | [diff] [blame] | 8068 | } |
| Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 8069 | |
| 8070 | } else { |
| John McCall | 6bd2a89 | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 8071 | // This needs to happen first so that 'inline' propagates. |
| Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 8072 | NewFD->setPreviousDeclaration(cast<FunctionDecl>(OldDecl)); |
| John McCall | 6bd2a89 | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 8073 | |
| Richard Smith | 91c18de | 2015-02-28 01:01:56 +0000 | [diff] [blame] | 8074 | if (isa<CXXMethodDecl>(NewFD)) |
| 8075 | NewFD->setAccess(OldDecl->getAccess()); |
| Zhongxing Xu | bece5d6 | 2009-01-16 01:13:29 +0000 | [diff] [blame] | 8076 | } |
| Douglas Gregor | 8af63e4 | 2009-02-06 17:46:57 +0000 | [diff] [blame] | 8077 | } |
| Zhongxing Xu | bece5d6 | 2009-01-16 01:13:29 +0000 | [diff] [blame] | 8078 | |
| Anders Carlsson | 1b12ed4 | 2009-09-13 21:33:06 +0000 | [diff] [blame] | 8079 | // Semantic checking for this function declaration (in isolation). |
| Nico Weber | be39a87 | 2014-07-31 17:19:18 +0000 | [diff] [blame] | 8080 | |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8081 | if (getLangOpts().CPlusPlus) { |
| Anders Carlsson | 1b12ed4 | 2009-09-13 21:33:06 +0000 | [diff] [blame] | 8082 | // C++-specific checks. |
| 8083 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(NewFD)) { |
| 8084 | CheckConstructor(Constructor); |
| Anders Carlsson | 2a50e95 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 8085 | } else if (CXXDestructorDecl *Destructor = |
| 8086 | dyn_cast<CXXDestructorDecl>(NewFD)) { |
| 8087 | CXXRecordDecl *Record = Destructor->getParent(); |
| Anders Carlsson | 1b12ed4 | 2009-09-13 21:33:06 +0000 | [diff] [blame] | 8088 | QualType ClassType = Context.getTypeDeclType(Record); |
| Anders Carlsson | 2a50e95 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 8089 | |
| Douglas Gregor | 7454c56 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 8090 | // 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] | 8091 | // type is dependent? Both gcc and edg can handle that. |
| Anders Carlsson | 1b12ed4 | 2009-09-13 21:33:06 +0000 | [diff] [blame] | 8092 | if (!ClassType->isDependentType()) { |
| 8093 | DeclarationName Name |
| 8094 | = Context.DeclarationNames.getCXXDestructorName( |
| 8095 | Context.getCanonicalType(ClassType)); |
| 8096 | if (NewFD->getDeclName() != Name) { |
| 8097 | Diag(NewFD->getLocation(), diag::err_destructor_name); |
| Kaelyn Uhrain | 4dc695d | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 8098 | NewFD->setInvalidDecl(); |
| 8099 | return Redeclaration; |
| Anders Carlsson | 1b12ed4 | 2009-09-13 21:33:06 +0000 | [diff] [blame] | 8100 | } |
| 8101 | } |
| Anders Carlsson | 1b12ed4 | 2009-09-13 21:33:06 +0000 | [diff] [blame] | 8102 | } else if (CXXConversionDecl *Conversion |
| Douglas Gregor | 21920e37 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 8103 | = dyn_cast<CXXConversionDecl>(NewFD)) { |
| Anders Carlsson | 1b12ed4 | 2009-09-13 21:33:06 +0000 | [diff] [blame] | 8104 | ActOnConversionDeclarator(Conversion); |
| Douglas Gregor | 21920e37 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 8105 | } |
| 8106 | |
| 8107 | // Find any virtual functions that this function overrides. |
| Douglas Gregor | 6be3de3 | 2009-12-01 17:35:23 +0000 | [diff] [blame] | 8108 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(NewFD)) { |
| 8109 | if (!Method->isFunctionTemplateSpecialization() && |
| Argyrios Kyrtzidis | cc4ca0a | 2012-10-09 01:23:45 +0000 | [diff] [blame] | 8110 | !Method->getDescribedFunctionTemplate() && |
| 8111 | Method->isCanonicalDecl()) { |
| Douglas Gregor | 5a2bb5b | 2010-10-13 22:55:32 +0000 | [diff] [blame] | 8112 | if (AddOverriddenMethods(Method->getParent(), Method)) { |
| 8113 | // If the function was marked as "static", we have a problem. |
| 8114 | if (NewFD->getStorageClass() == SC_Static) { |
| David Blaikie | 7e41426 | 2012-10-17 00:47:58 +0000 | [diff] [blame] | 8115 | ReportOverrides(*this, diag::err_static_overrides_virtual, Method); |
| Douglas Gregor | 5a2bb5b | 2010-10-13 22:55:32 +0000 | [diff] [blame] | 8116 | } |
| Argyrios Kyrtzidis | 7272d9c | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 8117 | } |
| Douglas Gregor | 5a2bb5b | 2010-10-13 22:55:32 +0000 | [diff] [blame] | 8118 | } |
| Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 8119 | |
| 8120 | if (Method->isStatic()) |
| 8121 | checkThisInStaticMemberFunctionType(Method); |
| Douglas Gregor | 6be3de3 | 2009-12-01 17:35:23 +0000 | [diff] [blame] | 8122 | } |
| Anders Carlsson | 1b12ed4 | 2009-09-13 21:33:06 +0000 | [diff] [blame] | 8123 | |
| 8124 | // Extra checking for C++ overloaded operators (C++ [over.oper]). |
| 8125 | if (NewFD->isOverloadedOperator() && |
| Kaelyn Uhrain | 4dc695d | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 8126 | CheckOverloadedOperatorDeclaration(NewFD)) { |
| 8127 | NewFD->setInvalidDecl(); |
| 8128 | return Redeclaration; |
| 8129 | } |
| Alexis Hunt | c88db06 | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 8130 | |
| 8131 | // Extra checking for C++0x literal operators (C++0x [over.literal]). |
| 8132 | if (NewFD->getLiteralIdentifier() && |
| Kaelyn Uhrain | 4dc695d | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 8133 | CheckLiteralOperatorDeclaration(NewFD)) { |
| 8134 | NewFD->setInvalidDecl(); |
| 8135 | return Redeclaration; |
| 8136 | } |
| Alexis Hunt | c88db06 | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 8137 | |
| Anders Carlsson | 1b12ed4 | 2009-09-13 21:33:06 +0000 | [diff] [blame] | 8138 | // In C++, check default arguments now that we have merged decls. Unless |
| 8139 | // the lexical context is the class, because in this case this is done |
| 8140 | // during delayed parsing anyway. |
| 8141 | if (!CurContext->isRecord()) |
| 8142 | CheckCXXDefaultArguments(NewFD); |
| Warren Hunt | 445d83e | 2013-11-01 23:46:51 +0000 | [diff] [blame] | 8143 | |
| Douglas Gregor | 9246b68 | 2010-12-21 19:47:46 +0000 | [diff] [blame] | 8144 | // If this function declares a builtin function, check the type of this |
| 8145 | // declaration against the expected type for the builtin. |
| 8146 | if (unsigned BuiltinID = NewFD->getBuiltinID()) { |
| 8147 | ASTContext::GetBuiltinTypeError Error; |
| Fariborz Jahanian | feb9ae5 | 2013-01-05 21:54:55 +0000 | [diff] [blame] | 8148 | LookupPredefedObjCSuperType(*this, S, NewFD->getIdentifier()); |
| Douglas Gregor | 9246b68 | 2010-12-21 19:47:46 +0000 | [diff] [blame] | 8149 | QualType T = Context.GetBuiltinType(BuiltinID, Error); |
| 8150 | if (!T.isNull() && !Context.hasSameType(T, NewFD->getType())) { |
| 8151 | // The type of this function differs from the type of the builtin, |
| 8152 | // so forget about the builtin entirely. |
| 8153 | Context.BuiltinInfo.ForgetBuiltin(BuiltinID, Context.Idents); |
| 8154 | } |
| 8155 | } |
| Warren Hunt | 445d83e | 2013-11-01 23:46:51 +0000 | [diff] [blame] | 8156 | |
| Aaron Ballman | c2a9493a | 2012-02-09 01:21:34 +0000 | [diff] [blame] | 8157 | // If this function is declared as being extern "C", then check to see if |
| 8158 | // the function returns a UDT (class, struct, or union type) that is not C |
| 8159 | // compatible, and if it does, warn the user. |
| Fariborz Jahanian | 95236b5 | 2013-03-14 23:09:00 +0000 | [diff] [blame] | 8160 | // But, issue any diagnostic on the first declaration only. |
| David Majnemer | d1091e5 | 2015-01-14 08:08:52 +0000 | [diff] [blame] | 8161 | if (Previous.empty() && NewFD->isExternC()) { |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 8162 | QualType R = NewFD->getReturnType(); |
| Hans Wennborg | 84ce606 | 2012-07-24 17:59:41 +0000 | [diff] [blame] | 8163 | if (R->isIncompleteType() && !R->isVoidType()) |
| 8164 | Diag(NewFD->getLocation(), diag::warn_return_value_udt_incomplete) |
| 8165 | << NewFD << R; |
| Douglas Gregor | 847cea7 | 2012-08-07 06:14:34 +0000 | [diff] [blame] | 8166 | else if (!R.isPODType(Context) && !R->isVoidType() && |
| 8167 | !R->isObjCObjectPointerType()) |
| Hans Wennborg | 84ce606 | 2012-07-24 17:59:41 +0000 | [diff] [blame] | 8168 | Diag(NewFD->getLocation(), diag::warn_return_value_udt) << NewFD << R; |
| Aaron Ballman | c2a9493a | 2012-02-09 01:21:34 +0000 | [diff] [blame] | 8169 | } |
| Anders Carlsson | 1b12ed4 | 2009-09-13 21:33:06 +0000 | [diff] [blame] | 8170 | } |
| Kaelyn Uhrain | 4dc695d | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 8171 | return Redeclaration; |
| Zhongxing Xu | bece5d6 | 2009-01-16 01:13:29 +0000 | [diff] [blame] | 8172 | } |
| 8173 | |
| David Blaikie | d937bf1 | 2011-09-08 06:33:04 +0000 | [diff] [blame] | 8174 | void Sema::CheckMain(FunctionDecl* FD, const DeclSpec& DS) { |
| Richard Smith | b63b6ee | 2014-01-22 01:43:19 +0000 | [diff] [blame] | 8175 | // C++11 [basic.start.main]p3: |
| 8176 | // A program that [...] declares main to be inline, static or |
| 8177 | // constexpr is ill-formed. |
| Richard Smith | 0015f09 | 2013-01-17 22:16:11 +0000 | [diff] [blame] | 8178 | // C11 6.7.4p4: In a hosted environment, no function specifier(s) shall |
| 8179 | // appear in a declaration of main. |
| John McCall | 02dee0a | 2009-07-25 04:36:53 +0000 | [diff] [blame] | 8180 | // 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] | 8181 | // We accept _Noreturn main as an extension. |
| David Blaikie | d937bf1 | 2011-09-08 06:33:04 +0000 | [diff] [blame] | 8182 | if (FD->getStorageClass() == SC_Static) |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8183 | Diag(DS.getStorageClassSpecLoc(), getLangOpts().CPlusPlus |
| David Blaikie | d937bf1 | 2011-09-08 06:33:04 +0000 | [diff] [blame] | 8184 | ? diag::err_static_main : diag::warn_static_main) |
| 8185 | << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc()); |
| 8186 | if (FD->isInlineSpecified()) |
| 8187 | Diag(DS.getInlineSpecLoc(), diag::err_inline_main) |
| 8188 | << FixItHint::CreateRemoval(DS.getInlineSpecLoc()); |
| Dmitri Gribenko | 7ec6f3d | 2013-01-21 11:25:03 +0000 | [diff] [blame] | 8189 | if (DS.isNoreturnSpecified()) { |
| 8190 | SourceLocation NoreturnLoc = DS.getNoreturnSpecLoc(); |
| Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 8191 | SourceRange NoreturnRange(NoreturnLoc, getLocForEndOfToken(NoreturnLoc)); |
| Dmitri Gribenko | 7ec6f3d | 2013-01-21 11:25:03 +0000 | [diff] [blame] | 8192 | Diag(NoreturnLoc, diag::ext_noreturn_main); |
| 8193 | Diag(NoreturnLoc, diag::note_main_remove_noreturn) |
| 8194 | << FixItHint::CreateRemoval(NoreturnRange); |
| 8195 | } |
| Richard Smith | 3f333f2 | 2012-02-04 06:10:17 +0000 | [diff] [blame] | 8196 | if (FD->isConstexpr()) { |
| 8197 | Diag(DS.getConstexprSpecLoc(), diag::err_constexpr_main) |
| 8198 | << FixItHint::CreateRemoval(DS.getConstexprSpecLoc()); |
| 8199 | FD->setConstexpr(false); |
| 8200 | } |
| John McCall | 02dee0a | 2009-07-25 04:36:53 +0000 | [diff] [blame] | 8201 | |
| Joey Gouly | a7310a8 | 2013-11-05 12:30:39 +0000 | [diff] [blame] | 8202 | if (getLangOpts().OpenCL) { |
| 8203 | Diag(FD->getLocation(), diag::err_opencl_no_main) |
| 8204 | << FD->hasAttr<OpenCLKernelAttr>(); |
| 8205 | FD->setInvalidDecl(); |
| 8206 | return; |
| 8207 | } |
| 8208 | |
| John McCall | 02dee0a | 2009-07-25 04:36:53 +0000 | [diff] [blame] | 8209 | QualType T = FD->getType(); |
| 8210 | assert(T->isFunctionType() && "function decl is not of function type"); |
| John McCall | 5ed3caf | 2012-02-14 19:50:52 +0000 | [diff] [blame] | 8211 | const FunctionType* FT = T->castAs<FunctionType>(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8212 | |
| Alp Toker | 70fc29c | 2014-07-02 07:07:20 +0000 | [diff] [blame] | 8213 | if (getLangOpts().GNUMode && !getLangOpts().CPlusPlus) { |
| 8214 | // In C with GNU extensions we allow main() to have non-integer return |
| 8215 | // type, but we should warn about the extension, and we disable the |
| 8216 | // implicit-return-zero rule. |
| 8217 | |
| 8218 | // GCC in C mode accepts qualified 'int'. |
| 8219 | if (Context.hasSameUnqualifiedType(FT->getReturnType(), Context.IntTy)) |
| 8220 | FD->setHasImplicitReturnZero(true); |
| 8221 | else { |
| 8222 | Diag(FD->getTypeSpecStartLoc(), diag::ext_main_returns_nonint); |
| 8223 | SourceRange RTRange = FD->getReturnTypeSourceRange(); |
| 8224 | if (RTRange.isValid()) |
| 8225 | Diag(RTRange.getBegin(), diag::note_main_change_return_type) |
| 8226 | << FixItHint::CreateReplacement(RTRange, "int"); |
| 8227 | } |
| 8228 | } else { |
| John McCall | 5ed3caf | 2012-02-14 19:50:52 +0000 | [diff] [blame] | 8229 | // In C and C++, main magically returns 0 if you fall off the end; |
| 8230 | // set the flag which tells us that. |
| 8231 | // This is C++ [basic.start.main]p5 and C99 5.1.2.2.3. |
| John McCall | 5ed3caf | 2012-02-14 19:50:52 +0000 | [diff] [blame] | 8232 | |
| Alp Toker | 70fc29c | 2014-07-02 07:07:20 +0000 | [diff] [blame] | 8233 | // All the standards say that main() should return 'int'. |
| 8234 | if (Context.hasSameType(FT->getReturnType(), Context.IntTy)) |
| 8235 | FD->setHasImplicitReturnZero(true); |
| 8236 | else { |
| 8237 | // Otherwise, this is just a flat-out error. |
| 8238 | SourceRange RTRange = FD->getReturnTypeSourceRange(); |
| 8239 | Diag(FD->getTypeSpecStartLoc(), diag::err_main_returns_nonint) |
| 8240 | << (RTRange.isValid() ? FixItHint::CreateReplacement(RTRange, "int") |
| 8241 | : FixItHint()); |
| 8242 | FD->setInvalidDecl(true); |
| 8243 | } |
| John McCall | 02dee0a | 2009-07-25 04:36:53 +0000 | [diff] [blame] | 8244 | } |
| 8245 | |
| 8246 | // Treat protoless main() as nullary. |
| 8247 | if (isa<FunctionNoProtoType>(FT)) return; |
| 8248 | |
| 8249 | const FunctionProtoType* FTP = cast<const FunctionProtoType>(FT); |
| Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 8250 | unsigned nparams = FTP->getNumParams(); |
| John McCall | 02dee0a | 2009-07-25 04:36:53 +0000 | [diff] [blame] | 8251 | assert(FD->getNumParams() == nparams); |
| 8252 | |
| John McCall | 0e21fcc | 2009-12-24 09:58:38 +0000 | [diff] [blame] | 8253 | bool HasExtraParameters = (nparams > 3); |
| 8254 | |
| Aaron Ballman | 1710cc9 | 2015-04-23 16:12:42 +0000 | [diff] [blame] | 8255 | if (FTP->isVariadic()) { |
| 8256 | Diag(FD->getLocation(), diag::ext_variadic_main); |
| 8257 | // FIXME: if we had information about the location of the ellipsis, we |
| 8258 | // could add a FixIt hint to remove it as a parameter. |
| 8259 | } |
| 8260 | |
| John McCall | 0e21fcc | 2009-12-24 09:58:38 +0000 | [diff] [blame] | 8261 | // Darwin passes an undocumented fourth argument of type char**. If |
| 8262 | // other platforms start sprouting these, the logic below will start |
| 8263 | // getting shifty. |
| Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 8264 | if (nparams == 4 && Context.getTargetInfo().getTriple().isOSDarwin()) |
| John McCall | 0e21fcc | 2009-12-24 09:58:38 +0000 | [diff] [blame] | 8265 | HasExtraParameters = false; |
| 8266 | |
| 8267 | if (HasExtraParameters) { |
| John McCall | 02dee0a | 2009-07-25 04:36:53 +0000 | [diff] [blame] | 8268 | Diag(FD->getLocation(), diag::err_main_surplus_args) << nparams; |
| 8269 | FD->setInvalidDecl(true); |
| 8270 | nparams = 3; |
| 8271 | } |
| 8272 | |
| 8273 | // FIXME: a lot of the following diagnostics would be improved |
| 8274 | // if we had some location information about types. |
| 8275 | |
| 8276 | QualType CharPP = |
| 8277 | Context.getPointerType(Context.getPointerType(Context.CharTy)); |
| John McCall | 0e21fcc | 2009-12-24 09:58:38 +0000 | [diff] [blame] | 8278 | QualType Expected[] = { Context.IntTy, CharPP, CharPP, CharPP }; |
| John McCall | 02dee0a | 2009-07-25 04:36:53 +0000 | [diff] [blame] | 8279 | |
| 8280 | for (unsigned i = 0; i < nparams; ++i) { |
| Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 8281 | QualType AT = FTP->getParamType(i); |
| John McCall | 02dee0a | 2009-07-25 04:36:53 +0000 | [diff] [blame] | 8282 | |
| 8283 | bool mismatch = true; |
| 8284 | |
| 8285 | if (Context.hasSameUnqualifiedType(AT, Expected[i])) |
| 8286 | mismatch = false; |
| 8287 | else if (Expected[i] == CharPP) { |
| 8288 | // As an extension, the following forms are okay: |
| 8289 | // char const ** |
| 8290 | // char const * const * |
| 8291 | // char * const * |
| 8292 | |
| John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 8293 | QualifierCollector qs; |
| John McCall | 02dee0a | 2009-07-25 04:36:53 +0000 | [diff] [blame] | 8294 | const PointerType* PT; |
| Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 8295 | if ((PT = qs.strip(AT)->getAs<PointerType>()) && |
| 8296 | (PT = qs.strip(PT->getPointeeType())->getAs<PointerType>()) && |
| Richard Smith | 685cef6 | 2013-01-29 02:49:47 +0000 | [diff] [blame] | 8297 | Context.hasSameType(QualType(qs.strip(PT->getPointeeType()), 0), |
| 8298 | Context.CharTy)) { |
| John McCall | 02dee0a | 2009-07-25 04:36:53 +0000 | [diff] [blame] | 8299 | qs.removeConst(); |
| 8300 | mismatch = !qs.empty(); |
| 8301 | } |
| 8302 | } |
| 8303 | |
| 8304 | if (mismatch) { |
| 8305 | Diag(FD->getLocation(), diag::err_main_arg_wrong) << i << Expected[i]; |
| 8306 | // TODO: suggest replacing given type with expected type |
| 8307 | FD->setInvalidDecl(true); |
| 8308 | } |
| 8309 | } |
| 8310 | |
| 8311 | if (nparams == 1 && !FD->isInvalidDecl()) { |
| 8312 | Diag(FD->getLocation(), diag::warn_main_one_arg); |
| 8313 | } |
| Douglas Gregor | bff6203 | 2010-10-21 16:57:46 +0000 | [diff] [blame] | 8314 | |
| 8315 | if (!FD->isInvalidDecl() && FD->getDescribedFunctionTemplate()) { |
| Aaron Ballman | 6d086d7 | 2014-01-03 02:20:27 +0000 | [diff] [blame] | 8316 | Diag(FD->getLocation(), diag::err_mainlike_template_decl) << FD; |
| David Majnemer | c729b0b | 2013-09-16 22:44:20 +0000 | [diff] [blame] | 8317 | FD->setInvalidDecl(); |
| 8318 | } |
| 8319 | } |
| 8320 | |
| 8321 | void Sema::CheckMSVCRTEntryPoint(FunctionDecl *FD) { |
| 8322 | QualType T = FD->getType(); |
| 8323 | assert(T->isFunctionType() && "function decl is not of function type"); |
| 8324 | const FunctionType *FT = T->castAs<FunctionType>(); |
| 8325 | |
| 8326 | // Set an implicit return of 'zero' if the function can return some integral, |
| 8327 | // enumeration, pointer or nullptr type. |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 8328 | if (FT->getReturnType()->isIntegralOrEnumerationType() || |
| 8329 | FT->getReturnType()->isAnyPointerType() || |
| 8330 | FT->getReturnType()->isNullPtrType()) |
| David Majnemer | c729b0b | 2013-09-16 22:44:20 +0000 | [diff] [blame] | 8331 | // DllMain is exempt because a return value of zero means it failed. |
| 8332 | if (FD->getName() != "DllMain") |
| 8333 | FD->setHasImplicitReturnZero(true); |
| 8334 | |
| 8335 | if (!FD->isInvalidDecl() && FD->getDescribedFunctionTemplate()) { |
| Aaron Ballman | 6d086d7 | 2014-01-03 02:20:27 +0000 | [diff] [blame] | 8336 | Diag(FD->getLocation(), diag::err_mainlike_template_decl) << FD; |
| Douglas Gregor | bff6203 | 2010-10-21 16:57:46 +0000 | [diff] [blame] | 8337 | FD->setInvalidDecl(); |
| 8338 | } |
| John McCall | d9baf6a | 2009-07-24 03:03:21 +0000 | [diff] [blame] | 8339 | } |
| 8340 | |
| Eli Friedman | d5a55bd | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 8341 | bool Sema::CheckForConstantInitializer(Expr *Init, QualType DclT) { |
| Eli Friedman | 2c7bd6b | 2009-02-27 04:17:12 +0000 | [diff] [blame] | 8342 | // FIXME: Need strict checking. In C89, we need to check for |
| 8343 | // any assignment, increment, decrement, function-calls, or |
| 8344 | // commas outside of a sizeof. In C99, it's the same list, |
| 8345 | // except that the aforementioned are allowed in unevaluated |
| 8346 | // expressions. Everything else falls under the |
| 8347 | // "may accept other forms of constant expressions" exception. |
| 8348 | // (We never end up here for C++, so the constant expression |
| 8349 | // rules there don't matter.) |
| Abramo Bagnara | 847c660 | 2014-05-22 19:20:46 +0000 | [diff] [blame] | 8350 | const Expr *Culprit; |
| 8351 | if (Init->isConstantInitializer(Context, false, &Culprit)) |
| Eli Friedman | 7bfab36 | 2009-02-22 06:45:27 +0000 | [diff] [blame] | 8352 | return false; |
| Abramo Bagnara | 847c660 | 2014-05-22 19:20:46 +0000 | [diff] [blame] | 8353 | Diag(Culprit->getExprLoc(), diag::err_init_element_not_constant) |
| 8354 | << Culprit->getSourceRange(); |
| Eli Friedman | d5a55bd | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 8355 | return true; |
| Steve Naroff | 98f7203 | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 8356 | } |
| 8357 | |
| Chandler Carruth | 33bf3e7 | 2011-03-27 09:46:56 +0000 | [diff] [blame] | 8358 | namespace { |
| 8359 | // Visits an initialization expression to see if OrigDecl is evaluated in |
| 8360 | // its own initialization and throws a warning if it does. |
| 8361 | class SelfReferenceChecker |
| 8362 | : public EvaluatedExprVisitor<SelfReferenceChecker> { |
| 8363 | Sema &S; |
| 8364 | Decl *OrigDecl; |
| Richard Trieu | a04ad1a | 2011-09-01 21:44:13 +0000 | [diff] [blame] | 8365 | bool isRecordType; |
| 8366 | bool isPODType; |
| Hans Wennborg | e1fdb05 | 2012-08-17 10:12:33 +0000 | [diff] [blame] | 8367 | bool isReferenceType; |
| Chandler Carruth | 33bf3e7 | 2011-03-27 09:46:56 +0000 | [diff] [blame] | 8368 | |
| Richard Trieu | e396ba6 | 2014-09-23 22:52:42 +0000 | [diff] [blame] | 8369 | bool isInitList; |
| 8370 | llvm::SmallVector<unsigned, 4> InitFieldIndex; |
| Chandler Carruth | 33bf3e7 | 2011-03-27 09:46:56 +0000 | [diff] [blame] | 8371 | public: |
| 8372 | typedef EvaluatedExprVisitor<SelfReferenceChecker> Inherited; |
| 8373 | |
| 8374 | SelfReferenceChecker(Sema &S, Decl *OrigDecl) : Inherited(S.Context), |
| Richard Trieu | a04ad1a | 2011-09-01 21:44:13 +0000 | [diff] [blame] | 8375 | S(S), OrigDecl(OrigDecl) { |
| 8376 | isPODType = false; |
| 8377 | isRecordType = false; |
| Hans Wennborg | e1fdb05 | 2012-08-17 10:12:33 +0000 | [diff] [blame] | 8378 | isReferenceType = false; |
| Richard Trieu | e396ba6 | 2014-09-23 22:52:42 +0000 | [diff] [blame] | 8379 | isInitList = false; |
| Richard Trieu | a04ad1a | 2011-09-01 21:44:13 +0000 | [diff] [blame] | 8380 | if (ValueDecl *VD = dyn_cast<ValueDecl>(OrigDecl)) { |
| 8381 | isPODType = VD->getType().isPODType(S.Context); |
| 8382 | isRecordType = VD->getType()->isRecordType(); |
| Hans Wennborg | e1fdb05 | 2012-08-17 10:12:33 +0000 | [diff] [blame] | 8383 | isReferenceType = VD->getType()->isReferenceType(); |
| Richard Trieu | a04ad1a | 2011-09-01 21:44:13 +0000 | [diff] [blame] | 8384 | } |
| 8385 | } |
| Chandler Carruth | 33bf3e7 | 2011-03-27 09:46:56 +0000 | [diff] [blame] | 8386 | |
| Richard Trieu | e396ba6 | 2014-09-23 22:52:42 +0000 | [diff] [blame] | 8387 | // For most expressions, just call the visitor. For initializer lists, |
| 8388 | // track the index of the field being initialized since fields are |
| 8389 | // initialized in order allowing use of previously initialized fields. |
| 8390 | void CheckExpr(Expr *E) { |
| 8391 | InitListExpr *InitList = dyn_cast<InitListExpr>(E); |
| 8392 | if (!InitList) { |
| 8393 | Visit(E); |
| 8394 | return; |
| 8395 | } |
| 8396 | |
| 8397 | // Track and increment the index here. |
| 8398 | isInitList = true; |
| 8399 | InitFieldIndex.push_back(0); |
| 8400 | for (auto Child : InitList->children()) { |
| 8401 | CheckExpr(cast<Expr>(Child)); |
| 8402 | ++InitFieldIndex.back(); |
| 8403 | } |
| 8404 | InitFieldIndex.pop_back(); |
| 8405 | } |
| 8406 | |
| 8407 | // Returns true if MemberExpr is checked and no futher checking is needed. |
| 8408 | // Returns false if additional checking is required. |
| 8409 | bool CheckInitListMemberExpr(MemberExpr *E, bool CheckReference) { |
| 8410 | llvm::SmallVector<FieldDecl*, 4> Fields; |
| 8411 | Expr *Base = E; |
| 8412 | bool ReferenceField = false; |
| 8413 | |
| 8414 | // Get the field memebers used. |
| 8415 | while (MemberExpr *ME = dyn_cast<MemberExpr>(Base)) { |
| 8416 | FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl()); |
| 8417 | if (!FD) |
| 8418 | return false; |
| 8419 | Fields.push_back(FD); |
| 8420 | if (FD->getType()->isReferenceType()) |
| 8421 | ReferenceField = true; |
| 8422 | Base = ME->getBase()->IgnoreParenImpCasts(); |
| 8423 | } |
| 8424 | |
| 8425 | // Keep checking only if the base Decl is the same. |
| 8426 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Base); |
| 8427 | if (!DRE || DRE->getDecl() != OrigDecl) |
| 8428 | return false; |
| 8429 | |
| 8430 | // A reference field can be bound to an unininitialized field. |
| 8431 | if (CheckReference && !ReferenceField) |
| 8432 | return true; |
| 8433 | |
| 8434 | // Convert FieldDecls to their index number. |
| 8435 | llvm::SmallVector<unsigned, 4> UsedFieldIndex; |
| 8436 | for (auto I = Fields.rbegin(), E = Fields.rend(); I != E; ++I) { |
| 8437 | UsedFieldIndex.push_back((*I)->getFieldIndex()); |
| 8438 | } |
| 8439 | |
| 8440 | // See if a warning is needed by checking the first difference in index |
| 8441 | // numbers. If field being used has index less than the field being |
| 8442 | // initialized, then the use is safe. |
| 8443 | for (auto UsedIter = UsedFieldIndex.begin(), |
| 8444 | UsedEnd = UsedFieldIndex.end(), |
| 8445 | OrigIter = InitFieldIndex.begin(), |
| 8446 | OrigEnd = InitFieldIndex.end(); |
| 8447 | UsedIter != UsedEnd && OrigIter != OrigEnd; ++UsedIter, ++OrigIter) { |
| 8448 | if (*UsedIter < *OrigIter) |
| 8449 | return true; |
| 8450 | if (*UsedIter > *OrigIter) |
| 8451 | break; |
| 8452 | } |
| 8453 | |
| 8454 | // TODO: Add a different warning which will print the field names. |
| 8455 | HandleDeclRefExpr(DRE); |
| 8456 | return true; |
| 8457 | } |
| 8458 | |
| Richard Trieu | 64c51ab | 2012-05-09 00:21:34 +0000 | [diff] [blame] | 8459 | // For most expressions, the cast is directly above the DeclRefExpr. |
| 8460 | // For conditional operators, the cast can be outside the conditional |
| 8461 | // operator if both expressions are DeclRefExpr's. |
| 8462 | void HandleValue(Expr *E) { |
| Richard Trieu | abf6ec4 | 2014-08-27 22:15:10 +0000 | [diff] [blame] | 8463 | E = E->IgnoreParens(); |
| Richard Trieu | 64c51ab | 2012-05-09 00:21:34 +0000 | [diff] [blame] | 8464 | if (DeclRefExpr* DRE = dyn_cast<DeclRefExpr>(E)) { |
| 8465 | HandleDeclRefExpr(DRE); |
| 8466 | return; |
| 8467 | } |
| 8468 | |
| 8469 | if (ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E)) { |
| Richard Trieu | 779c6f2 | 2014-09-26 23:48:30 +0000 | [diff] [blame] | 8470 | Visit(CO->getCond()); |
| Richard Trieu | 64c51ab | 2012-05-09 00:21:34 +0000 | [diff] [blame] | 8471 | HandleValue(CO->getTrueExpr()); |
| 8472 | HandleValue(CO->getFalseExpr()); |
| Richard Trieu | 742c6ed | 2012-10-03 00:41:36 +0000 | [diff] [blame] | 8473 | return; |
| 8474 | } |
| 8475 | |
| Richard Trieu | abf6ec4 | 2014-08-27 22:15:10 +0000 | [diff] [blame] | 8476 | if (BinaryConditionalOperator *BCO = |
| 8477 | dyn_cast<BinaryConditionalOperator>(E)) { |
| Richard Trieu | 2a07c96 | 2014-09-04 23:19:34 +0000 | [diff] [blame] | 8478 | Visit(BCO->getCond()); |
| Richard Trieu | abf6ec4 | 2014-08-27 22:15:10 +0000 | [diff] [blame] | 8479 | HandleValue(BCO->getFalseExpr()); |
| 8480 | return; |
| 8481 | } |
| 8482 | |
| 8483 | if (OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(E)) { |
| 8484 | HandleValue(OVE->getSourceExpr()); |
| 8485 | return; |
| 8486 | } |
| 8487 | |
| 8488 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) { |
| Richard Trieu | 78dd725 | 2014-09-24 03:53:56 +0000 | [diff] [blame] | 8489 | if (BO->getOpcode() == BO_Comma) { |
| 8490 | Visit(BO->getLHS()); |
| Richard Trieu | abf6ec4 | 2014-08-27 22:15:10 +0000 | [diff] [blame] | 8491 | HandleValue(BO->getRHS()); |
| Richard Trieu | 78dd725 | 2014-09-24 03:53:56 +0000 | [diff] [blame] | 8492 | return; |
| 8493 | } |
| Richard Trieu | abf6ec4 | 2014-08-27 22:15:10 +0000 | [diff] [blame] | 8494 | } |
| 8495 | |
| Richard Trieu | 742c6ed | 2012-10-03 00:41:36 +0000 | [diff] [blame] | 8496 | if (isa<MemberExpr>(E)) { |
| Richard Trieu | e396ba6 | 2014-09-23 22:52:42 +0000 | [diff] [blame] | 8497 | if (isInitList) { |
| 8498 | if (CheckInitListMemberExpr(cast<MemberExpr>(E), |
| 8499 | false /*CheckReference*/)) |
| 8500 | return; |
| 8501 | } |
| 8502 | |
| Richard Trieu | 742c6ed | 2012-10-03 00:41:36 +0000 | [diff] [blame] | 8503 | Expr *Base = E->IgnoreParenImpCasts(); |
| 8504 | while (MemberExpr *ME = dyn_cast<MemberExpr>(Base)) { |
| 8505 | // Check for static member variables and don't warn on them. |
| 8506 | if (!isa<FieldDecl>(ME->getMemberDecl())) |
| 8507 | return; |
| 8508 | Base = ME->getBase()->IgnoreParenImpCasts(); |
| 8509 | } |
| 8510 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Base)) |
| 8511 | HandleDeclRefExpr(DRE); |
| 8512 | return; |
| Richard Trieu | 64c51ab | 2012-05-09 00:21:34 +0000 | [diff] [blame] | 8513 | } |
| Richard Trieu | 2a07c96 | 2014-09-04 23:19:34 +0000 | [diff] [blame] | 8514 | |
| 8515 | Visit(E); |
| Richard Trieu | 64c51ab | 2012-05-09 00:21:34 +0000 | [diff] [blame] | 8516 | } |
| 8517 | |
| Richard Trieu | 2a07c96 | 2014-09-04 23:19:34 +0000 | [diff] [blame] | 8518 | // Reference types not handled in HandleValue are handled here since all |
| 8519 | // uses of references are bad, not just r-value uses. |
| Richard Trieu | 3267347 | 2012-10-01 17:39:51 +0000 | [diff] [blame] | 8520 | void VisitDeclRefExpr(DeclRefExpr *E) { |
| 8521 | if (isReferenceType) |
| 8522 | HandleDeclRefExpr(E); |
| 8523 | } |
| 8524 | |
| Richard Trieu | 64c51ab | 2012-05-09 00:21:34 +0000 | [diff] [blame] | 8525 | void VisitImplicitCastExpr(ImplicitCastExpr *E) { |
| Richard Trieu | 9f8509f | 2014-09-30 23:04:37 +0000 | [diff] [blame] | 8526 | if (E->getCastKind() == CK_LValueToRValue) { |
| Richard Trieu | 64c51ab | 2012-05-09 00:21:34 +0000 | [diff] [blame] | 8527 | HandleValue(E->getSubExpr()); |
| Richard Trieu | 2a07c96 | 2014-09-04 23:19:34 +0000 | [diff] [blame] | 8528 | return; |
| 8529 | } |
| Richard Trieu | 64c51ab | 2012-05-09 00:21:34 +0000 | [diff] [blame] | 8530 | |
| 8531 | Inherited::VisitImplicitCastExpr(E); |
| Chandler Carruth | 33bf3e7 | 2011-03-27 09:46:56 +0000 | [diff] [blame] | 8532 | } |
| 8533 | |
| Richard Trieu | a04ad1a | 2011-09-01 21:44:13 +0000 | [diff] [blame] | 8534 | void VisitMemberExpr(MemberExpr *E) { |
| Richard Trieu | e396ba6 | 2014-09-23 22:52:42 +0000 | [diff] [blame] | 8535 | if (isInitList) { |
| 8536 | if (CheckInitListMemberExpr(E, true /*CheckReference*/)) |
| 8537 | return; |
| 8538 | } |
| 8539 | |
| Richard Trieu | 64c51ab | 2012-05-09 00:21:34 +0000 | [diff] [blame] | 8540 | // Don't warn on arrays since they can be treated as pointers. |
| Richard Trieu | aa5e256 | 2011-09-07 00:58:53 +0000 | [diff] [blame] | 8541 | if (E->getType()->canDecayToPointerType()) return; |
| Richard Trieu | 64c51ab | 2012-05-09 00:21:34 +0000 | [diff] [blame] | 8542 | |
| Richard Trieu | 742c6ed | 2012-10-03 00:41:36 +0000 | [diff] [blame] | 8543 | // Warn when a non-static method call is followed by non-static member |
| 8544 | // field accesses, which is followed by a DeclRefExpr. |
| 8545 | CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(E->getMemberDecl()); |
| 8546 | bool Warn = (MD && !MD->isStatic()); |
| 8547 | Expr *Base = E->getBase()->IgnoreParenImpCasts(); |
| 8548 | while (MemberExpr *ME = dyn_cast<MemberExpr>(Base)) { |
| 8549 | if (!isa<FieldDecl>(ME->getMemberDecl())) |
| 8550 | Warn = false; |
| 8551 | Base = ME->getBase()->IgnoreParenImpCasts(); |
| 8552 | } |
| Richard Trieu | a04ad1a | 2011-09-01 21:44:13 +0000 | [diff] [blame] | 8553 | |
| Richard Trieu | 742c6ed | 2012-10-03 00:41:36 +0000 | [diff] [blame] | 8554 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Base)) { |
| 8555 | if (Warn) |
| 8556 | HandleDeclRefExpr(DRE); |
| 8557 | return; |
| 8558 | } |
| 8559 | |
| 8560 | // The base of a MemberExpr is not a MemberExpr or a DeclRefExpr. |
| 8561 | // Visit that expression. |
| 8562 | Visit(Base); |
| Chandler Carruth | 33bf3e7 | 2011-03-27 09:46:56 +0000 | [diff] [blame] | 8563 | } |
| 8564 | |
| Richard Trieu | 8fbd91d | 2013-03-26 03:41:40 +0000 | [diff] [blame] | 8565 | void VisitCXXOperatorCallExpr(CXXOperatorCallExpr *E) { |
| Richard Trieu | d4a0136 | 2014-10-31 21:10:22 +0000 | [diff] [blame] | 8566 | Expr *Callee = E->getCallee(); |
| Richard Trieu | 8fbd91d | 2013-03-26 03:41:40 +0000 | [diff] [blame] | 8567 | |
| Richard Trieu | d4a0136 | 2014-10-31 21:10:22 +0000 | [diff] [blame] | 8568 | if (isa<UnresolvedLookupExpr>(Callee)) |
| 8569 | return Inherited::VisitCXXOperatorCallExpr(E); |
| 8570 | |
| 8571 | Visit(Callee); |
| 8572 | for (auto Arg: E->arguments()) |
| 8573 | HandleValue(Arg->IgnoreParenImpCasts()); |
| Richard Trieu | 8fbd91d | 2013-03-26 03:41:40 +0000 | [diff] [blame] | 8574 | } |
| 8575 | |
| Richard Trieu | a04ad1a | 2011-09-01 21:44:13 +0000 | [diff] [blame] | 8576 | void VisitUnaryOperator(UnaryOperator *E) { |
| 8577 | // For POD record types, addresses of its own members are well-defined. |
| Richard Trieu | 742c6ed | 2012-10-03 00:41:36 +0000 | [diff] [blame] | 8578 | if (E->getOpcode() == UO_AddrOf && isRecordType && |
| 8579 | isa<MemberExpr>(E->getSubExpr()->IgnoreParens())) { |
| 8580 | if (!isPODType) |
| 8581 | HandleValue(E->getSubExpr()); |
| 8582 | return; |
| 8583 | } |
| Richard Trieu | 52b8b60 | 2014-09-25 01:15:40 +0000 | [diff] [blame] | 8584 | |
| 8585 | if (E->isIncrementDecrementOp()) { |
| 8586 | HandleValue(E->getSubExpr()); |
| 8587 | return; |
| 8588 | } |
| 8589 | |
| Richard Trieu | a04ad1a | 2011-09-01 21:44:13 +0000 | [diff] [blame] | 8590 | Inherited::VisitUnaryOperator(E); |
| Richard Smith | fa11fd6 | 2013-05-03 19:16:22 +0000 | [diff] [blame] | 8591 | } |
| Richard Trieu | 64c51ab | 2012-05-09 00:21:34 +0000 | [diff] [blame] | 8592 | |
| 8593 | void VisitObjCMessageExpr(ObjCMessageExpr *E) { return; } |
| 8594 | |
| Richard Trieu | 4834ad2 | 2014-08-12 21:05:04 +0000 | [diff] [blame] | 8595 | void VisitCXXConstructExpr(CXXConstructExpr *E) { |
| 8596 | if (E->getConstructor()->isCopyConstructor()) { |
| Richard Trieu | 9f8509f | 2014-09-30 23:04:37 +0000 | [diff] [blame] | 8597 | Expr *ArgExpr = E->getArg(0); |
| 8598 | if (InitListExpr *ILE = dyn_cast<InitListExpr>(ArgExpr)) |
| 8599 | if (ILE->getNumInits() == 1) |
| 8600 | ArgExpr = ILE->getInit(0); |
| 8601 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgExpr)) |
| 8602 | if (ICE->getCastKind() == CK_NoOp) |
| 8603 | ArgExpr = ICE->getSubExpr(); |
| 8604 | HandleValue(ArgExpr); |
| 8605 | return; |
| Richard Trieu | 4834ad2 | 2014-08-12 21:05:04 +0000 | [diff] [blame] | 8606 | } |
| 8607 | Inherited::VisitCXXConstructExpr(E); |
| 8608 | } |
| 8609 | |
| Richard Trieu | 11fd079 | 2014-08-26 04:30:55 +0000 | [diff] [blame] | 8610 | void VisitCallExpr(CallExpr *E) { |
| 8611 | // Treat std::move as a use. |
| 8612 | if (E->getNumArgs() == 1) { |
| 8613 | if (FunctionDecl *FD = E->getDirectCallee()) { |
| Richard Trieu | c321b93 | 2014-11-27 01:29:32 +0000 | [diff] [blame] | 8614 | if (FD->isInStdNamespace() && FD->getIdentifier() && |
| 8615 | FD->getIdentifier()->isStr("move")) { |
| Richard Trieu | abf6ec4 | 2014-08-27 22:15:10 +0000 | [diff] [blame] | 8616 | HandleValue(E->getArg(0)); |
| Richard Trieu | 2a07c96 | 2014-09-04 23:19:34 +0000 | [diff] [blame] | 8617 | return; |
| Richard Trieu | 11fd079 | 2014-08-26 04:30:55 +0000 | [diff] [blame] | 8618 | } |
| 8619 | } |
| 8620 | } |
| 8621 | |
| 8622 | Inherited::VisitCallExpr(E); |
| 8623 | } |
| 8624 | |
| Richard Trieu | 52b8b60 | 2014-09-25 01:15:40 +0000 | [diff] [blame] | 8625 | void VisitBinaryOperator(BinaryOperator *E) { |
| 8626 | if (E->isCompoundAssignmentOp()) { |
| 8627 | HandleValue(E->getLHS()); |
| 8628 | Visit(E->getRHS()); |
| 8629 | return; |
| 8630 | } |
| 8631 | |
| 8632 | Inherited::VisitBinaryOperator(E); |
| 8633 | } |
| 8634 | |
| Richard Trieu | 2a07c96 | 2014-09-04 23:19:34 +0000 | [diff] [blame] | 8635 | // A custom visitor for BinaryConditionalOperator is needed because the |
| 8636 | // regular visitor would check the condition and true expression separately |
| 8637 | // but both point to the same place giving duplicate diagnostics. |
| 8638 | void VisitBinaryConditionalOperator(BinaryConditionalOperator *E) { |
| 8639 | Visit(E->getCond()); |
| 8640 | Visit(E->getFalseExpr()); |
| 8641 | } |
| 8642 | |
| Richard Trieu | a04ad1a | 2011-09-01 21:44:13 +0000 | [diff] [blame] | 8643 | void HandleDeclRefExpr(DeclRefExpr *DRE) { |
| NAKAMURA Takumi | 3fef3ef | 2013-01-19 01:54:35 +0000 | [diff] [blame] | 8644 | Decl* ReferenceDecl = DRE->getDecl(); |
| Chandler Carruth | 33bf3e7 | 2011-03-27 09:46:56 +0000 | [diff] [blame] | 8645 | if (OrigDecl != ReferenceDecl) return; |
| Ted Kremenek | a83b407 | 2013-01-19 04:33:14 +0000 | [diff] [blame] | 8646 | unsigned diag; |
| 8647 | if (isReferenceType) { |
| 8648 | diag = diag::warn_uninit_self_reference_in_reference_init; |
| 8649 | } else if (cast<VarDecl>(OrigDecl)->isStaticLocal()) { |
| 8650 | diag = diag::warn_static_self_reference_in_init; |
| Manuel Klimek | 27ee25f | 2015-03-03 14:54:25 +0000 | [diff] [blame] | 8651 | } else if (isa<TranslationUnitDecl>(OrigDecl->getDeclContext()) || |
| 8652 | isa<NamespaceDecl>(OrigDecl->getDeclContext()) || |
| 8653 | DRE->getDecl()->getType()->isRecordType()) { |
| Ted Kremenek | a83b407 | 2013-01-19 04:33:14 +0000 | [diff] [blame] | 8654 | diag = diag::warn_uninit_self_reference_in_init; |
| Manuel Klimek | 27ee25f | 2015-03-03 14:54:25 +0000 | [diff] [blame] | 8655 | } else { |
| 8656 | // Local variables will be handled by the CFG analysis. |
| 8657 | return; |
| Ted Kremenek | a83b407 | 2013-01-19 04:33:14 +0000 | [diff] [blame] | 8658 | } |
| 8659 | |
| Richard Trieu | a04ad1a | 2011-09-01 21:44:13 +0000 | [diff] [blame] | 8660 | S.DiagRuntimeBehavior(DRE->getLocStart(), DRE, |
| Hans Wennborg | d799a2b | 2012-08-20 08:52:22 +0000 | [diff] [blame] | 8661 | S.PDiag(diag) |
| Hans Wennborg | 61b2ffa | 2012-09-21 08:58:33 +0000 | [diff] [blame] | 8662 | << DRE->getNameInfo().getName() |
| Douglas Gregor | fcafc6e | 2011-05-24 16:02:01 +0000 | [diff] [blame] | 8663 | << OrigDecl->getLocation() |
| Richard Trieu | a04ad1a | 2011-09-01 21:44:13 +0000 | [diff] [blame] | 8664 | << DRE->getSourceRange()); |
| Chandler Carruth | 33bf3e7 | 2011-03-27 09:46:56 +0000 | [diff] [blame] | 8665 | } |
| 8666 | }; |
| Chandler Carruth | 33bf3e7 | 2011-03-27 09:46:56 +0000 | [diff] [blame] | 8667 | |
| Richard Trieu | 3267347 | 2012-10-01 17:39:51 +0000 | [diff] [blame] | 8668 | /// CheckSelfReference - Warns if OrigDecl is used in expression E. |
| 8669 | static void CheckSelfReference(Sema &S, Decl* OrigDecl, Expr *E, |
| 8670 | bool DirectInit) { |
| 8671 | // Parameters arguments are occassionially constructed with itself, |
| 8672 | // for instance, in recursive functions. Skip them. |
| 8673 | if (isa<ParmVarDecl>(OrigDecl)) |
| 8674 | return; |
| 8675 | |
| 8676 | E = E->IgnoreParens(); |
| 8677 | |
| 8678 | // Skip checking T a = a where T is not a record or reference type. |
| 8679 | // Doing so is a way to silence uninitialized warnings. |
| 8680 | if (!DirectInit && !cast<VarDecl>(OrigDecl)->getType()->isRecordType()) |
| 8681 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) |
| 8682 | if (ICE->getCastKind() == CK_LValueToRValue) |
| 8683 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr())) |
| 8684 | if (DRE->getDecl() == OrigDecl) |
| 8685 | return; |
| 8686 | |
| Richard Trieu | e396ba6 | 2014-09-23 22:52:42 +0000 | [diff] [blame] | 8687 | SelfReferenceChecker(S, OrigDecl).CheckExpr(E); |
| Richard Trieu | 3267347 | 2012-10-01 17:39:51 +0000 | [diff] [blame] | 8688 | } |
| Richard Trieu | a04ad1a | 2011-09-01 21:44:13 +0000 | [diff] [blame] | 8689 | } |
| 8690 | |
| Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 8691 | /// AddInitializerToDecl - Adds the initializer Init to the |
| 8692 | /// declaration dcl. If DirectInit is true, this is C++ direct |
| 8693 | /// initialization rather than copy initialization. |
| Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 8694 | void Sema::AddInitializerToDecl(Decl *RealDecl, Expr *Init, |
| 8695 | bool DirectInit, bool TypeMayContainAuto) { |
| Chris Lattner | 8beb9de | 2007-10-19 20:10:30 +0000 | [diff] [blame] | 8696 | // If there is no declaration, there was an error parsing it. Just ignore |
| 8697 | // the initializer. |
| Kaelyn Takata | 42118a9 | 2015-01-07 21:16:39 +0000 | [diff] [blame] | 8698 | if (!RealDecl || RealDecl->isInvalidDecl()) { |
| 8699 | CorrectDelayedTyposInExpr(Init); |
| Chris Lattner | 8beb9de | 2007-10-19 20:10:30 +0000 | [diff] [blame] | 8700 | return; |
| Kaelyn Takata | 42118a9 | 2015-01-07 21:16:39 +0000 | [diff] [blame] | 8701 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8702 | |
| Douglas Gregor | 0c88030 | 2009-03-11 23:00:04 +0000 | [diff] [blame] | 8703 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(RealDecl)) { |
| 8704 | // With declarators parsed the way they are, the parser cannot |
| 8705 | // distinguish between a normal initializer and a pure-specifier. |
| 8706 | // Thus this grotesque test. |
| 8707 | IntegerLiteral *IL; |
| Douglas Gregor | 0c88030 | 2009-03-11 23:00:04 +0000 | [diff] [blame] | 8708 | if ((IL = dyn_cast<IntegerLiteral>(Init)) && IL->getValue() == 0 && |
| Douglas Gregor | 21920e37 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 8709 | Context.getCanonicalType(IL->getType()) == Context.IntTy) |
| 8710 | CheckPureMethod(Method, Init->getSourceRange()); |
| 8711 | else { |
| Douglas Gregor | 0c88030 | 2009-03-11 23:00:04 +0000 | [diff] [blame] | 8712 | Diag(Method->getLocation(), diag::err_member_function_initialization) |
| 8713 | << Method->getDeclName() << Init->getSourceRange(); |
| 8714 | Method->setInvalidDecl(); |
| 8715 | } |
| 8716 | return; |
| 8717 | } |
| 8718 | |
| Steve Naroff | 437b4d8 | 2007-09-12 20:13:48 +0000 | [diff] [blame] | 8719 | VarDecl *VDecl = dyn_cast<VarDecl>(RealDecl); |
| 8720 | if (!VDecl) { |
| Richard Smith | 4a4beec | 2011-06-12 11:43:46 +0000 | [diff] [blame] | 8721 | assert(!isa<FieldDecl>(RealDecl) && "field init shouldn't get here"); |
| 8722 | Diag(RealDecl->getLocation(), diag::err_illegal_initializer); |
| Steve Naroff | 437b4d8 | 2007-09-12 20:13:48 +0000 | [diff] [blame] | 8723 | RealDecl->setInvalidDecl(); |
| 8724 | return; |
| Eli Friedman | 2c7bd6b | 2009-02-27 04:17:12 +0000 | [diff] [blame] | 8725 | } |
| Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 8726 | ParenListExpr *CXXDirectInit = dyn_cast<ParenListExpr>(Init); |
| 8727 | |
| Richard Smith | 0cc8578 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8728 | // 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] | 8729 | if (TypeMayContainAuto && VDecl->getType()->isUndeducedType()) { |
| Kaelyn Takata | 3587fff | 2015-03-17 23:50:12 +0000 | [diff] [blame] | 8730 | // Attempt typo correction early so that the type of the init expression can |
| 8731 | // be deduced based on the chosen correction:if the original init contains a |
| 8732 | // TypoExpr. |
| 8733 | ExprResult Res = CorrectDelayedTyposInExpr(Init); |
| 8734 | if (!Res.isUsable()) { |
| 8735 | RealDecl->setInvalidDecl(); |
| 8736 | return; |
| 8737 | } |
| 8738 | if (Res.get() != Init) { |
| 8739 | Init = Res.get(); |
| 8740 | if (CXXDirectInit) |
| 8741 | CXXDirectInit = dyn_cast<ParenListExpr>(Init); |
| 8742 | } |
| 8743 | |
| Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 8744 | Expr *DeduceInit = Init; |
| 8745 | // Initializer could be a C++ direct-initializer. Deduction only works if it |
| 8746 | // contains exactly one expression. |
| 8747 | if (CXXDirectInit) { |
| 8748 | if (CXXDirectInit->getNumExprs() == 0) { |
| 8749 | // It isn't possible to write this directly, but it is possible to |
| 8750 | // end up in this situation with "auto x(some_pack...);" |
| Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 8751 | Diag(CXXDirectInit->getLocStart(), |
| Richard Smith | bb13c9a | 2013-09-28 04:02:39 +0000 | [diff] [blame] | 8752 | VDecl->isInitCapture() ? diag::err_init_capture_no_expression |
| 8753 | : diag::err_auto_var_init_no_expression) |
| Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 8754 | << VDecl->getDeclName() << VDecl->getType() |
| 8755 | << VDecl->getSourceRange(); |
| 8756 | RealDecl->setInvalidDecl(); |
| 8757 | return; |
| 8758 | } else if (CXXDirectInit->getNumExprs() > 1) { |
| Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 8759 | Diag(CXXDirectInit->getExpr(1)->getLocStart(), |
| Richard Smith | bb13c9a | 2013-09-28 04:02:39 +0000 | [diff] [blame] | 8760 | VDecl->isInitCapture() |
| 8761 | ? diag::err_init_capture_multiple_expressions |
| 8762 | : diag::err_auto_var_init_multiple_expressions) |
| Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 8763 | << VDecl->getDeclName() << VDecl->getType() |
| 8764 | << VDecl->getSourceRange(); |
| 8765 | RealDecl->setInvalidDecl(); |
| 8766 | return; |
| 8767 | } else { |
| 8768 | DeduceInit = CXXDirectInit->getExpr(0); |
| Richard Smith | 66204ec | 2014-03-12 17:42:45 +0000 | [diff] [blame] | 8769 | if (isa<InitListExpr>(DeduceInit)) |
| 8770 | Diag(CXXDirectInit->getLocStart(), |
| 8771 | diag::err_auto_var_init_paren_braces) |
| 8772 | << VDecl->getDeclName() << VDecl->getType() |
| 8773 | << VDecl->getSourceRange(); |
| Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 8774 | } |
| 8775 | } |
| Douglas Gregor | b5af2e9 | 2013-03-07 22:57:58 +0000 | [diff] [blame] | 8776 | |
| 8777 | // Expressions default to 'id' when we're in a debugger. |
| 8778 | bool DefaultedToAuto = false; |
| 8779 | if (getLangOpts().DebuggerCastResultToId && |
| 8780 | Init->getType() == Context.UnknownAnyTy) { |
| 8781 | ExprResult Result = forceUnknownAnyToType(Init, Context.getObjCIdType()); |
| 8782 | if (Result.isInvalid()) { |
| 8783 | VDecl->setInvalidDecl(); |
| 8784 | return; |
| 8785 | } |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 8786 | Init = Result.get(); |
| Douglas Gregor | b5af2e9 | 2013-03-07 22:57:58 +0000 | [diff] [blame] | 8787 | DefaultedToAuto = true; |
| 8788 | } |
| Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 8789 | |
| 8790 | QualType DeducedType; |
| Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 8791 | if (DeduceAutoType(VDecl->getTypeSourceInfo(), DeduceInit, DeducedType) == |
| Sebastian Redl | 09edce0 | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 8792 | DAR_Failed) |
| Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 8793 | DiagnoseAutoDeductionFailure(VDecl, DeduceInit); |
| Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 8794 | if (DeducedType.isNull()) { |
| Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 8795 | RealDecl->setInvalidDecl(); |
| 8796 | return; |
| 8797 | } |
| Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 8798 | VDecl->setType(DeducedType); |
| Rafael Espindola | 0e0d009 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 8799 | assert(VDecl->isLinkageValid()); |
| Rafael Espindola | bf5c33b | 2013-03-12 21:06:00 +0000 | [diff] [blame] | 8800 | |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8801 | // In ARC, infer lifetime. |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8802 | if (getLangOpts().ObjCAutoRefCount && inferObjCARCLifetime(VDecl)) |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8803 | VDecl->setInvalidDecl(); |
| 8804 | |
| Jordan Rose | d8d5669 | 2012-06-08 22:46:07 +0000 | [diff] [blame] | 8805 | // Warn if we deduced 'id'. 'auto' usually implies type-safety, but using |
| 8806 | // 'id' instead of a specific object type prevents most of our usual checks. |
| 8807 | // We only want to warn outside of template instantiations, though: |
| 8808 | // inside a template, the 'id' could have come from a parameter. |
| Douglas Gregor | b5af2e9 | 2013-03-07 22:57:58 +0000 | [diff] [blame] | 8809 | if (ActiveTemplateInstantiations.empty() && !DefaultedToAuto && |
| Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 8810 | DeducedType->isObjCIdType()) { |
| 8811 | SourceLocation Loc = |
| 8812 | VDecl->getTypeSourceInfo()->getTypeLoc().getBeginLoc(); |
| Jordan Rose | d8d5669 | 2012-06-08 22:46:07 +0000 | [diff] [blame] | 8813 | Diag(Loc, diag::warn_auto_var_is_id) |
| 8814 | << VDecl->getDeclName() << DeduceInit->getSourceRange(); |
| 8815 | } |
| 8816 | |
| Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 8817 | // If this is a redeclaration, check that the type we just deduced matches |
| 8818 | // the previously declared type. |
| Richard Smith | 1c34fb7 | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 8819 | if (VarDecl *Old = VDecl->getPreviousDecl()) { |
| 8820 | // We never need to merge the type, because we cannot form an incomplete |
| 8821 | // array of auto, nor deduce such a type. |
| 8822 | MergeVarDeclTypes(VDecl, Old, /*MergeTypeWithPrevious*/false); |
| 8823 | } |
| Richard Smith | 27d807c | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 8824 | |
| 8825 | // Check the deduced type is valid for a variable declaration. |
| 8826 | CheckVariableDeclarationType(VDecl); |
| 8827 | if (VDecl->isInvalidDecl()) |
| 8828 | return; |
| Richard Smith | 215f423 | 2015-02-11 02:41:33 +0000 | [diff] [blame] | 8829 | |
| 8830 | // If all looks well, warn if this is a case that will change meaning when |
| 8831 | // we implement N3922. |
| 8832 | if (DirectInit && !CXXDirectInit && isa<InitListExpr>(Init)) { |
| 8833 | Diag(Init->getLocStart(), |
| 8834 | diag::warn_auto_var_direct_list_init) |
| 8835 | << FixItHint::CreateInsertion(Init->getLocStart(), "="); |
| 8836 | } |
| Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 8837 | } |
| Richard Smith | 0cc8578 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8838 | |
| Nico Rieck | 8e9791f | 2014-02-26 21:27:13 +0000 | [diff] [blame] | 8839 | // dllimport cannot be used on variable definitions. |
| 8840 | if (VDecl->hasAttr<DLLImportAttr>() && !VDecl->isStaticDataMember()) { |
| 8841 | Diag(VDecl->getLocation(), diag::err_attribute_dllimport_data_definition); |
| 8842 | VDecl->setInvalidDecl(); |
| 8843 | return; |
| 8844 | } |
| 8845 | |
| Richard Smith | 0cc8578 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8846 | if (VDecl->isLocalVarDecl() && VDecl->hasExternalStorage()) { |
| 8847 | // C99 6.7.8p5. C++ has no such restriction, but that is a defect. |
| 8848 | Diag(VDecl->getLocation(), diag::err_block_extern_cant_init); |
| 8849 | VDecl->setInvalidDecl(); |
| 8850 | return; |
| 8851 | } |
| 8852 | |
| Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 8853 | if (!VDecl->getType()->isDependentType()) { |
| 8854 | // A definition must end up with a complete type, which means it must be |
| 8855 | // complete with the restriction that an array type might be completed by |
| 8856 | // the initializer; note that later code assumes this restriction. |
| 8857 | QualType BaseDeclType = VDecl->getType(); |
| 8858 | if (const ArrayType *Array = Context.getAsIncompleteArrayType(BaseDeclType)) |
| 8859 | BaseDeclType = Array->getElementType(); |
| 8860 | if (RequireCompleteType(VDecl->getLocation(), BaseDeclType, |
| 8861 | diag::err_typecheck_decl_incomplete_type)) { |
| 8862 | RealDecl->setInvalidDecl(); |
| 8863 | return; |
| 8864 | } |
| Douglas Gregor | f0f8369 | 2010-08-24 05:27:49 +0000 | [diff] [blame] | 8865 | |
| Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 8866 | // The variable can not have an abstract class type. |
| 8867 | if (RequireNonAbstractType(VDecl->getLocation(), VDecl->getType(), |
| 8868 | diag::err_abstract_type_in_decl, |
| 8869 | AbstractVariableType)) |
| 8870 | VDecl->setInvalidDecl(); |
| Eli Friedman | 337cd3a | 2009-04-13 21:28:54 +0000 | [diff] [blame] | 8871 | } |
| 8872 | |
| Sebastian Redl | 5ca7984 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 8873 | const VarDecl *Def; |
| 8874 | if ((Def = VDecl->getDefinition()) && Def != VDecl) { |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8875 | Diag(VDecl->getLocation(), diag::err_redefinition) |
| Douglas Gregor | 0760fa1 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 8876 | << VDecl->getDeclName(); |
| 8877 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 8878 | VDecl->setInvalidDecl(); |
| 8879 | return; |
| 8880 | } |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8881 | |
| 8882 | const VarDecl *PrevInit = nullptr; |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8883 | if (getLangOpts().CPlusPlus) { |
| Douglas Gregor | 71f39c9 | 2010-12-16 01:31:22 +0000 | [diff] [blame] | 8884 | // C++ [class.static.data]p4 |
| 8885 | // If a static data member is of const integral or const |
| 8886 | // enumeration type, its declaration in the class definition can |
| 8887 | // specify a constant-initializer which shall be an integral |
| 8888 | // constant expression (5.19). In that case, the member can appear |
| 8889 | // in integral constant expressions. The member shall still be |
| 8890 | // defined in a namespace scope if it is used in the program and the |
| 8891 | // namespace scope definition shall not contain an initializer. |
| 8892 | // |
| 8893 | // We already performed a redefinition check above, but for static |
| 8894 | // data members we also need to check whether there was an in-class |
| 8895 | // declaration with an initializer. |
| 8896 | if (VDecl->isStaticDataMember() && VDecl->getAnyInitializer(PrevInit)) { |
| Hans Wennborg | 84fe12d | 2013-11-21 03:17:44 +0000 | [diff] [blame] | 8897 | Diag(Init->getExprLoc(), diag::err_static_data_member_reinitialization) |
| 8898 | << VDecl->getDeclName(); |
| 8899 | Diag(PrevInit->getInit()->getExprLoc(), diag::note_previous_initializer) << 0; |
| Douglas Gregor | 71f39c9 | 2010-12-16 01:31:22 +0000 | [diff] [blame] | 8900 | return; |
| 8901 | } |
| Douglas Gregor | 0760fa1 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 8902 | |
| Douglas Gregor | 71f39c9 | 2010-12-16 01:31:22 +0000 | [diff] [blame] | 8903 | if (VDecl->hasLocalStorage()) |
| 8904 | getCurFunction()->setHasBranchProtectedScope(); |
| 8905 | |
| 8906 | if (DiagnoseUnexpandedParameterPack(Init, UPPC_Initializer)) { |
| 8907 | VDecl->setInvalidDecl(); |
| 8908 | return; |
| 8909 | } |
| 8910 | } |
| John McCall | d4e1b76 | 2010-08-01 01:24:59 +0000 | [diff] [blame] | 8911 | |
| Peter Collingbourne | 2dbb708 | 2011-09-19 21:14:35 +0000 | [diff] [blame] | 8912 | // OpenCL 1.1 6.5.2: "Variables allocated in the __local address space inside |
| 8913 | // a kernel function cannot be initialized." |
| 8914 | if (VDecl->getStorageClass() == SC_OpenCLWorkGroupLocal) { |
| 8915 | Diag(VDecl->getLocation(), diag::err_local_cant_init); |
| 8916 | VDecl->setInvalidDecl(); |
| 8917 | return; |
| 8918 | } |
| 8919 | |
| Steve Naroff | 6109140 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 8920 | // Get the decls type and save a reference for later, since |
| Steve Naroff | 98f7203 | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 8921 | // CheckInitializerTypes may change it. |
| Steve Naroff | 437b4d8 | 2007-09-12 20:13:48 +0000 | [diff] [blame] | 8922 | QualType DclT = VDecl->getType(), SavT = DclT; |
| Fariborz Jahanian | c8a322a | 2012-03-09 18:47:16 +0000 | [diff] [blame] | 8923 | |
| Douglas Gregor | b5af2e9 | 2013-03-07 22:57:58 +0000 | [diff] [blame] | 8924 | // Expressions default to 'id' when we're in a debugger |
| 8925 | // and we are assigning it to a variable of Objective-C pointer type. |
| 8926 | if (getLangOpts().DebuggerCastResultToId && DclT->isObjCObjectPointerType() && |
| 8927 | Init->getType() == Context.UnknownAnyTy) { |
| 8928 | ExprResult Result = forceUnknownAnyToType(Init, Context.getObjCIdType()); |
| 8929 | if (Result.isInvalid()) { |
| 8930 | VDecl->setInvalidDecl(); |
| 8931 | return; |
| Fariborz Jahanian | c8a322a | 2012-03-09 18:47:16 +0000 | [diff] [blame] | 8932 | } |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 8933 | Init = Result.get(); |
| Douglas Gregor | b5af2e9 | 2013-03-07 22:57:58 +0000 | [diff] [blame] | 8934 | } |
| Richard Smith | 0cc8578 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8935 | |
| 8936 | // Perform the initialization. |
| 8937 | if (!VDecl->isInvalidDecl()) { |
| 8938 | InitializedEntity Entity = InitializedEntity::InitializeVariable(VDecl); |
| 8939 | InitializationKind Kind |
| Sebastian Redl | 5a41f68 | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 8940 | = DirectInit ? |
| 8941 | CXXDirectInit ? InitializationKind::CreateDirect(VDecl->getLocation(), |
| 8942 | Init->getLocStart(), |
| 8943 | Init->getLocEnd()) |
| 8944 | : InitializationKind::CreateDirectList( |
| 8945 | VDecl->getLocation()) |
| Richard Smith | 0cc8578 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8946 | : InitializationKind::CreateCopy(VDecl->getLocation(), |
| 8947 | Init->getLocStart()); |
| 8948 | |
| Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 8949 | MultiExprArg Args = Init; |
| 8950 | if (CXXDirectInit) |
| 8951 | Args = MultiExprArg(CXXDirectInit->getExprs(), |
| 8952 | CXXDirectInit->getNumExprs()); |
| 8953 | |
| Kaelyn Takata | 938204a | 2014-12-16 23:07:00 +0000 | [diff] [blame] | 8954 | // Try to correct any TypoExprs in the initialization arguments. |
| 8955 | for (size_t Idx = 0; Idx < Args.size(); ++Idx) { |
| Kaelyn Takata | 4c3ffc4 | 2014-11-21 18:48:00 +0000 | [diff] [blame] | 8956 | ExprResult Res = |
| Kaelyn Takata | 938204a | 2014-12-16 23:07:00 +0000 | [diff] [blame] | 8957 | CorrectDelayedTyposInExpr(Args[Idx], [this, Entity, Kind](Expr *E) { |
| Kaelyn Takata | 4c3ffc4 | 2014-11-21 18:48:00 +0000 | [diff] [blame] | 8958 | InitializationSequence Init(*this, Entity, Kind, MultiExprArg(E)); |
| 8959 | return Init.Failed() ? ExprError() : E; |
| 8960 | }); |
| 8961 | if (Res.isInvalid()) { |
| 8962 | VDecl->setInvalidDecl(); |
| Kaelyn Takata | 21a8869 | 2015-01-21 00:04:19 +0000 | [diff] [blame] | 8963 | } else if (Res.get() != Args[Idx]) { |
| Kaelyn Takata | 938204a | 2014-12-16 23:07:00 +0000 | [diff] [blame] | 8964 | Args[Idx] = Res.get(); |
| Kaelyn Takata | 21a8869 | 2015-01-21 00:04:19 +0000 | [diff] [blame] | 8965 | } |
| Kaelyn Takata | 4c3ffc4 | 2014-11-21 18:48:00 +0000 | [diff] [blame] | 8966 | } |
| Kaelyn Takata | 21a8869 | 2015-01-21 00:04:19 +0000 | [diff] [blame] | 8967 | if (VDecl->isInvalidDecl()) |
| 8968 | return; |
| Kaelyn Takata | 4c3ffc4 | 2014-11-21 18:48:00 +0000 | [diff] [blame] | 8969 | |
| Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 8970 | InitializationSequence InitSeq(*this, Entity, Kind, Args); |
| 8971 | ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Args, &DclT); |
| Richard Smith | 0cc8578 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8972 | if (Result.isInvalid()) { |
| Steve Naroff | 08899ff | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 8973 | VDecl->setInvalidDecl(); |
| Richard Smith | 0cc8578 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8974 | return; |
| Steve Naroff | 6109140 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 8975 | } |
| Richard Smith | 0cc8578 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8976 | |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 8977 | Init = Result.getAs<Expr>(); |
| Richard Smith | 0cc8578 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8978 | } |
| 8979 | |
| Richard Trieu | 3267347 | 2012-10-01 17:39:51 +0000 | [diff] [blame] | 8980 | // Check for self-references within variable initializers. |
| 8981 | // Variables declared within a function/method body (except for references) |
| 8982 | // are handled by a dataflow analysis. |
| 8983 | if (!VDecl->hasLocalStorage() || VDecl->getType()->isRecordType() || |
| 8984 | VDecl->getType()->isReferenceType()) { |
| 8985 | CheckSelfReference(*this, RealDecl, Init, DirectInit); |
| 8986 | } |
| 8987 | |
| Richard Smith | 0cc8578 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8988 | // If the type changed, it means we had an incomplete type that was |
| 8989 | // completed by the initializer. For example: |
| 8990 | // int ary[] = { 1, 3, 5 }; |
| John McCall | a59dc2f | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 8991 | // "ary" transitions from an IncompleteArrayType to a ConstantArrayType. |
| Eli Friedman | 91f5ae5 | 2012-02-23 02:25:10 +0000 | [diff] [blame] | 8992 | if (!VDecl->isInvalidDecl() && (DclT != SavT)) |
| Richard Smith | 0cc8578 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8993 | VDecl->setType(DclT); |
| Richard Smith | 0cc8578 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8994 | |
| Jordan Rose | fa9e4ba | 2012-09-15 02:48:31 +0000 | [diff] [blame] | 8995 | if (!VDecl->isInvalidDecl()) { |
| Richard Smith | 0cc8578 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8996 | checkUnsafeAssigns(VDecl->getLocation(), VDecl->getType(), Init); |
| 8997 | |
| Jordan Rose | fa9e4ba | 2012-09-15 02:48:31 +0000 | [diff] [blame] | 8998 | if (VDecl->hasAttr<BlocksAttr>()) |
| 8999 | checkRetainCycles(VDecl, Init); |
| Jordan Rose | d393458 | 2012-09-28 22:21:30 +0000 | [diff] [blame] | 9000 | |
| 9001 | // It is safe to assign a weak reference into a strong variable. |
| 9002 | // Although this code can still have problems: |
| 9003 | // id x = self.weakProp; |
| 9004 | // id y = self.weakProp; |
| 9005 | // we do not warn to warn spuriously when 'x' and 'y' are on separate |
| 9006 | // paths through the function. This should be revisited if |
| 9007 | // -Wrepeated-use-of-weak is made flow-sensitive. |
| Alp Toker | d4a3f0e | 2014-06-15 23:30:39 +0000 | [diff] [blame] | 9008 | if (VDecl->getType().getObjCLifetime() == Qualifiers::OCL_Strong && |
| 9009 | !Diags.isIgnored(diag::warn_arc_repeated_use_of_weak, |
| 9010 | Init->getLocStart())) |
| Jordan Rose | d393458 | 2012-09-28 22:21:30 +0000 | [diff] [blame] | 9011 | getCurFunction()->markSafeWeakUse(Init); |
| Jordan Rose | fa9e4ba | 2012-09-15 02:48:31 +0000 | [diff] [blame] | 9012 | } |
| 9013 | |
| Richard Smith | 945f8d3 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 9014 | // The initialization is usually a full-expression. |
| 9015 | // |
| 9016 | // FIXME: If this is a braced initialization of an aggregate, it is not |
| 9017 | // an expression, and each individual field initializer is a separate |
| 9018 | // full-expression. For instance, in: |
| 9019 | // |
| 9020 | // struct Temp { ~Temp(); }; |
| 9021 | // struct S { S(Temp); }; |
| 9022 | // struct T { S a, b; } t = { Temp(), Temp() } |
| 9023 | // |
| 9024 | // we should destroy the first Temp before constructing the second. |
| Fariborz Jahanian | e735ff9 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 9025 | ExprResult Result = ActOnFinishFullExpr(Init, VDecl->getLocation(), |
| 9026 | false, |
| 9027 | VDecl->isConstexpr()); |
| Richard Smith | 945f8d3 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 9028 | if (Result.isInvalid()) { |
| 9029 | VDecl->setInvalidDecl(); |
| 9030 | return; |
| 9031 | } |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 9032 | Init = Result.get(); |
| Richard Smith | 945f8d3 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 9033 | |
| Richard Smith | 0cc8578 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 9034 | // Attach the initializer to the decl. |
| 9035 | VDecl->setInit(Init); |
| 9036 | |
| 9037 | if (VDecl->isLocalVarDecl()) { |
| 9038 | // C99 6.7.8p4: All the expressions in an initializer for an object that has |
| 9039 | // static storage duration shall be constant expressions or string literals. |
| 9040 | // C++ does not have this restriction. |
| Enea Zaffanella | 1aac546 | 2013-07-22 10:58:26 +0000 | [diff] [blame] | 9041 | if (!getLangOpts().CPlusPlus && !VDecl->isInvalidDecl()) { |
| Abramo Bagnara | 847c660 | 2014-05-22 19:20:46 +0000 | [diff] [blame] | 9042 | const Expr *Culprit; |
| Enea Zaffanella | 1aac546 | 2013-07-22 10:58:26 +0000 | [diff] [blame] | 9043 | if (VDecl->getStorageClass() == SC_Static) |
| 9044 | CheckForConstantInitializer(Init, DclT); |
| 9045 | // C89 is stricter than C99 for non-static aggregate types. |
| 9046 | // C89 6.5.7p3: All the expressions [...] in an initializer list |
| 9047 | // for an object that has aggregate or union type shall be |
| 9048 | // constant expressions. |
| 9049 | else if (!getLangOpts().C99 && VDecl->getType()->isAggregateType() && |
| Enea Zaffanella | c7cb48c | 2013-07-22 19:10:20 +0000 | [diff] [blame] | 9050 | isa<InitListExpr>(Init) && |
| Abramo Bagnara | 847c660 | 2014-05-22 19:20:46 +0000 | [diff] [blame] | 9051 | !Init->isConstantInitializer(Context, false, &Culprit)) |
| 9052 | Diag(Culprit->getExprLoc(), |
| Enea Zaffanella | 1aac546 | 2013-07-22 10:58:26 +0000 | [diff] [blame] | 9053 | diag::ext_aggregate_init_not_constant) |
| Abramo Bagnara | 847c660 | 2014-05-22 19:20:46 +0000 | [diff] [blame] | 9054 | << Culprit->getSourceRange(); |
| Enea Zaffanella | 1aac546 | 2013-07-22 10:58:26 +0000 | [diff] [blame] | 9055 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9056 | } else if (VDecl->isStaticDataMember() && |
| Douglas Gregor | 0c88030 | 2009-03-11 23:00:04 +0000 | [diff] [blame] | 9057 | VDecl->getLexicalDeclContext()->isRecord()) { |
| 9058 | // This is an in-class initialization for a static data member, e.g., |
| 9059 | // |
| 9060 | // struct S { |
| 9061 | // static const int value = 17; |
| 9062 | // }; |
| 9063 | |
| Douglas Gregor | 0c88030 | 2009-03-11 23:00:04 +0000 | [diff] [blame] | 9064 | // C++ [class.mem]p4: |
| 9065 | // A member-declarator can contain a constant-initializer only |
| 9066 | // if it declares a static member (9.4) of const integral or |
| 9067 | // const enumeration type, see 9.4.2. |
| Richard Smith | 2316cd8 | 2011-09-29 19:11:37 +0000 | [diff] [blame] | 9068 | // |
| Richard Smith | 0cc8578 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 9069 | // C++11 [class.static.data]p3: |
| Richard Smith | 2316cd8 | 2011-09-29 19:11:37 +0000 | [diff] [blame] | 9070 | // If a non-volatile const static data member is of integral or |
| 9071 | // enumeration type, its declaration in the class definition can |
| 9072 | // specify a brace-or-equal-initializer in which every initalizer-clause |
| 9073 | // that is an assignment-expression is a constant expression. A static |
| 9074 | // data member of literal type can be declared in the class definition |
| 9075 | // with the constexpr specifier; if so, its declaration shall specify a |
| 9076 | // brace-or-equal-initializer in which every initializer-clause that is |
| 9077 | // an assignment-expression is a constant expression. |
| John McCall | db76892 | 2010-09-10 23:21:22 +0000 | [diff] [blame] | 9078 | |
| 9079 | // Do nothing on dependent types. |
| Richard Smith | 0cc8578 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 9080 | if (DclT->isDependentType()) { |
| John McCall | db76892 | 2010-09-10 23:21:22 +0000 | [diff] [blame] | 9081 | |
| Richard Smith | 2316cd8 | 2011-09-29 19:11:37 +0000 | [diff] [blame] | 9082 | // Allow any 'static constexpr' members, whether or not they are of literal |
| Richard Smith | 3607ffe | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 9083 | // type. We separately check that every constexpr variable is of literal |
| 9084 | // type. |
| Richard Smith | 2316cd8 | 2011-09-29 19:11:37 +0000 | [diff] [blame] | 9085 | } else if (VDecl->isConstexpr()) { |
| 9086 | |
| John McCall | db76892 | 2010-09-10 23:21:22 +0000 | [diff] [blame] | 9087 | // Require constness. |
| Richard Smith | 0cc8578 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 9088 | } else if (!DclT.isConstQualified()) { |
| John McCall | db76892 | 2010-09-10 23:21:22 +0000 | [diff] [blame] | 9089 | Diag(VDecl->getLocation(), diag::err_in_class_initializer_non_const) |
| 9090 | << Init->getSourceRange(); |
| Douglas Gregor | 0c88030 | 2009-03-11 23:00:04 +0000 | [diff] [blame] | 9091 | VDecl->setInvalidDecl(); |
| John McCall | db76892 | 2010-09-10 23:21:22 +0000 | [diff] [blame] | 9092 | |
| 9093 | // We allow integer constant expressions in all cases. |
| Richard Smith | 0cc8578 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 9094 | } else if (DclT->isIntegralOrEnumerationType()) { |
| Chris Lattner | 9925ec8 | 2011-06-14 05:46:29 +0000 | [diff] [blame] | 9095 | // Check whether the expression is a constant expression. |
| 9096 | SourceLocation Loc; |
| Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 9097 | if (getLangOpts().CPlusPlus11 && DclT.isVolatileQualified()) |
| Richard Smith | 0cc8578 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 9098 | // In C++11, a non-constexpr const static data member with an |
| Richard Smith | ee6311d | 2011-09-29 21:28:14 +0000 | [diff] [blame] | 9099 | // in-class initializer cannot be volatile. |
| 9100 | Diag(VDecl->getLocation(), diag::err_in_class_initializer_volatile); |
| 9101 | else if (Init->isValueDependent()) |
| Chris Lattner | 9925ec8 | 2011-06-14 05:46:29 +0000 | [diff] [blame] | 9102 | ; // Nothing to check. |
| 9103 | else if (Init->isIntegerConstantExpr(Context, &Loc)) |
| 9104 | ; // Ok, it's an ICE! |
| 9105 | else if (Init->isEvaluatable(Context)) { |
| 9106 | // If we can constant fold the initializer through heroics, accept it, |
| 9107 | // but report this as a use of an extension for -pedantic. |
| 9108 | Diag(Loc, diag::ext_in_class_initializer_non_constant) |
| 9109 | << Init->getSourceRange(); |
| 9110 | } else { |
| 9111 | // Otherwise, this is some crazy unknown case. Report the issue at the |
| 9112 | // location provided by the isIntegerConstantExpr failed check. |
| 9113 | Diag(Loc, diag::err_in_class_initializer_non_constant) |
| 9114 | << Init->getSourceRange(); |
| 9115 | VDecl->setInvalidDecl(); |
| John McCall | db76892 | 2010-09-10 23:21:22 +0000 | [diff] [blame] | 9116 | } |
| 9117 | |
| Richard Smith | 0cc8578 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 9118 | // We allow foldable floating-point constants as an extension. |
| 9119 | } else if (DclT->isFloatingType()) { // also permits complex, which is ok |
| Richard Smith | cf65638 | 2013-01-25 04:22:16 +0000 | [diff] [blame] | 9120 | // In C++98, this is a GNU extension. In C++11, it is not, but we support |
| 9121 | // it anyway and provide a fixit to add the 'constexpr'. |
| 9122 | if (getLangOpts().CPlusPlus11) { |
| David Blaikie | 8505c29 | 2013-01-29 22:26:08 +0000 | [diff] [blame] | 9123 | Diag(VDecl->getLocation(), |
| 9124 | diag::ext_in_class_initializer_float_type_cxx11) |
| 9125 | << DclT << Init->getSourceRange(); |
| 9126 | Diag(VDecl->getLocStart(), |
| 9127 | diag::note_in_class_initializer_float_type_cxx11) |
| 9128 | << FixItHint::CreateInsertion(VDecl->getLocStart(), "constexpr "); |
| Richard Smith | cf65638 | 2013-01-25 04:22:16 +0000 | [diff] [blame] | 9129 | } else { |
| 9130 | Diag(VDecl->getLocation(), diag::ext_in_class_initializer_float_type) |
| 9131 | << DclT << Init->getSourceRange(); |
| John McCall | db76892 | 2010-09-10 23:21:22 +0000 | [diff] [blame] | 9132 | |
| Richard Smith | cf65638 | 2013-01-25 04:22:16 +0000 | [diff] [blame] | 9133 | if (!Init->isValueDependent() && !Init->isEvaluatable(Context)) { |
| 9134 | Diag(Init->getExprLoc(), diag::err_in_class_initializer_non_constant) |
| 9135 | << Init->getSourceRange(); |
| 9136 | VDecl->setInvalidDecl(); |
| 9137 | } |
| Douglas Gregor | 0c88030 | 2009-03-11 23:00:04 +0000 | [diff] [blame] | 9138 | } |
| Richard Smith | 256336d | 2011-09-29 23:18:34 +0000 | [diff] [blame] | 9139 | |
| Richard Smith | 0cc8578 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 9140 | // Suggest adding 'constexpr' in C++11 for literal types. |
| Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 9141 | } else if (getLangOpts().CPlusPlus11 && DclT->isLiteralType(Context)) { |
| Richard Smith | 256336d | 2011-09-29 23:18:34 +0000 | [diff] [blame] | 9142 | Diag(VDecl->getLocation(), diag::err_in_class_initializer_literal_type) |
| Richard Smith | 0cc8578 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 9143 | << DclT << Init->getSourceRange() |
| Richard Smith | 256336d | 2011-09-29 23:18:34 +0000 | [diff] [blame] | 9144 | << FixItHint::CreateInsertion(VDecl->getLocStart(), "constexpr "); |
| 9145 | VDecl->setConstexpr(true); |
| 9146 | |
| Richard Smith | 2316cd8 | 2011-09-29 19:11:37 +0000 | [diff] [blame] | 9147 | } else { |
| 9148 | Diag(VDecl->getLocation(), diag::err_in_class_initializer_bad_type) |
| Richard Smith | 0cc8578 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 9149 | << DclT << Init->getSourceRange(); |
| Richard Smith | 2316cd8 | 2011-09-29 19:11:37 +0000 | [diff] [blame] | 9150 | VDecl->setInvalidDecl(); |
| Douglas Gregor | 0c88030 | 2009-03-11 23:00:04 +0000 | [diff] [blame] | 9151 | } |
| Steve Naroff | 08899ff | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 9152 | } else if (VDecl->isFileVarDecl()) { |
| Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 9153 | if (VDecl->getStorageClass() == SC_Extern && |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9154 | (!getLangOpts().CPlusPlus || |
| Rafael Espindola | 069ab03 | 2013-03-29 07:56:05 +0000 | [diff] [blame] | 9155 | !(Context.getBaseElementType(VDecl->getType()).isConstQualified() || |
| Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 9156 | VDecl->isExternC())) && |
| 9157 | !isTemplateInstantiation(VDecl->getTemplateSpecializationKind())) |
| Steve Naroff | 437b4d8 | 2007-09-12 20:13:48 +0000 | [diff] [blame] | 9158 | Diag(VDecl->getLocation(), diag::warn_extern_init); |
| Eli Friedman | 463e523 | 2009-12-22 02:10:53 +0000 | [diff] [blame] | 9159 | |
| Richard Smith | 0cc8578 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 9160 | // C99 6.7.8p4. All file scoped initializers need to be constant. |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9161 | if (!getLangOpts().CPlusPlus && !VDecl->isInvalidDecl()) |
| Anders Carlsson | 41e0881 | 2008-08-22 05:00:02 +0000 | [diff] [blame] | 9162 | CheckForConstantInitializer(Init, DclT); |
| Steve Naroff | 6109140 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 9163 | } |
| Douglas Gregor | beecd58 | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 9164 | |
| Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 9165 | // We will represent direct-initialization similarly to copy-initialization: |
| 9166 | // int x(1); -as-> int x = 1; |
| 9167 | // ClassType x(a,b,c); -as-> ClassType x = ClassType(a,b,c); |
| 9168 | // |
| 9169 | // Clients that want to distinguish between the two forms, can check for |
| 9170 | // direct initializer using VarDecl::getInitStyle(). |
| 9171 | // A major benefit is that clients that don't particularly care about which |
| 9172 | // exactly form was it (like the CodeGen) can handle both cases without |
| 9173 | // special case code. |
| 9174 | |
| 9175 | // C++ 8.5p11: |
| 9176 | // The form of initialization (using parentheses or '=') is generally |
| 9177 | // insignificant, but does matter when the entity being initialized has a |
| 9178 | // class type. |
| 9179 | if (CXXDirectInit) { |
| 9180 | assert(DirectInit && "Call-style initializer must be direct init."); |
| 9181 | VDecl->setInitStyle(VarDecl::CallInit); |
| 9182 | } else if (DirectInit) { |
| 9183 | // This must be list-initialization. No other way is direct-initialization. |
| 9184 | VDecl->setInitStyle(VarDecl::ListInit); |
| 9185 | } |
| 9186 | |
| John McCall | 8b7fd8f1 | 2011-01-19 11:48:09 +0000 | [diff] [blame] | 9187 | CheckCompleteVariableDeclaration(VDecl); |
| Steve Naroff | 6109140 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 9188 | } |
| 9189 | |
| John McCall | eae5acb | 2010-03-31 02:13:20 +0000 | [diff] [blame] | 9190 | /// ActOnInitializerError - Given that there was an error parsing an |
| 9191 | /// initializer for the given declaration, try to return to some form |
| 9192 | /// of sanity. |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 9193 | void Sema::ActOnInitializerError(Decl *D) { |
| John McCall | eae5acb | 2010-03-31 02:13:20 +0000 | [diff] [blame] | 9194 | // Our main concern here is re-establishing invariants like "a |
| 9195 | // variable's type is either dependent or complete". |
| John McCall | eae5acb | 2010-03-31 02:13:20 +0000 | [diff] [blame] | 9196 | if (!D || D->isInvalidDecl()) return; |
| 9197 | |
| 9198 | VarDecl *VD = dyn_cast<VarDecl>(D); |
| 9199 | if (!VD) return; |
| 9200 | |
| Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 9201 | // 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] | 9202 | if (ParsingInitForAutoVars.count(D)) { |
| 9203 | D->setInvalidDecl(); |
| Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 9204 | return; |
| 9205 | } |
| 9206 | |
| John McCall | eae5acb | 2010-03-31 02:13:20 +0000 | [diff] [blame] | 9207 | QualType Ty = VD->getType(); |
| 9208 | if (Ty->isDependentType()) return; |
| 9209 | |
| 9210 | // Require a complete type. |
| 9211 | if (RequireCompleteType(VD->getLocation(), |
| 9212 | Context.getBaseElementType(Ty), |
| 9213 | diag::err_typecheck_decl_incomplete_type)) { |
| 9214 | VD->setInvalidDecl(); |
| 9215 | return; |
| 9216 | } |
| 9217 | |
| Alp Toker | 48c7e17 | 2014-04-15 16:24:50 +0000 | [diff] [blame] | 9218 | // Require a non-abstract type. |
| John McCall | eae5acb | 2010-03-31 02:13:20 +0000 | [diff] [blame] | 9219 | if (RequireNonAbstractType(VD->getLocation(), Ty, |
| 9220 | diag::err_abstract_type_in_decl, |
| 9221 | AbstractVariableType)) { |
| 9222 | VD->setInvalidDecl(); |
| 9223 | return; |
| 9224 | } |
| 9225 | |
| 9226 | // Don't bother complaining about constructors or destructors, |
| 9227 | // though. |
| 9228 | } |
| 9229 | |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 9230 | void Sema::ActOnUninitializedDecl(Decl *RealDecl, |
| Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 9231 | bool TypeMayContainAuto) { |
| Argyrios Kyrtzidis | 6709e7d | 2008-11-07 13:01:22 +0000 | [diff] [blame] | 9232 | // If there is no declaration, there was an error parsing it. Just ignore it. |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 9233 | if (!RealDecl) |
| Argyrios Kyrtzidis | 6709e7d | 2008-11-07 13:01:22 +0000 | [diff] [blame] | 9234 | return; |
| 9235 | |
| Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 9236 | if (VarDecl *Var = dyn_cast<VarDecl>(RealDecl)) { |
| 9237 | QualType Type = Var->getType(); |
| Douglas Gregor | beecd58 | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 9238 | |
| Richard Smith | f0215fe | 2011-12-25 21:17:58 +0000 | [diff] [blame] | 9239 | // C++11 [dcl.spec.auto]p3 |
| Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 9240 | if (TypeMayContainAuto && Type->getContainedAutoType()) { |
| Anders Carlsson | ae01993 | 2009-07-11 00:34:39 +0000 | [diff] [blame] | 9241 | Diag(Var->getLocation(), diag::err_auto_var_requires_init) |
| 9242 | << Var->getDeclName() << Type; |
| 9243 | Var->setInvalidDecl(); |
| 9244 | return; |
| 9245 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9246 | |
| Richard Smith | f0215fe | 2011-12-25 21:17:58 +0000 | [diff] [blame] | 9247 | // 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] | 9248 | // the constexpr specifier; if so, its declaration shall specify |
| 9249 | // a brace-or-equal-initializer. |
| Richard Smith | f0215fe | 2011-12-25 21:17:58 +0000 | [diff] [blame] | 9250 | // C++11 [dcl.constexpr]p1: The constexpr specifier shall be applied only to |
| 9251 | // the definition of a variable [...] or the declaration of a static data |
| 9252 | // member. |
| 9253 | if (Var->isConstexpr() && !Var->isThisDeclarationADefinition()) { |
| 9254 | if (Var->isStaticDataMember()) |
| 9255 | Diag(Var->getLocation(), |
| 9256 | diag::err_constexpr_static_mem_var_requires_init) |
| 9257 | << Var->getDeclName(); |
| 9258 | else |
| 9259 | Diag(Var->getLocation(), diag::err_invalid_constexpr_var_decl); |
| Richard Smith | 2316cd8 | 2011-09-29 19:11:37 +0000 | [diff] [blame] | 9260 | Var->setInvalidDecl(); |
| 9261 | return; |
| 9262 | } |
| 9263 | |
| Joey Gouly | 96b94e6 | 2014-01-03 14:16:55 +0000 | [diff] [blame] | 9264 | // OpenCL v1.1 s6.5.3: variables declared in the constant address space must |
| 9265 | // be initialized. |
| 9266 | if (!Var->isInvalidDecl() && |
| 9267 | Var->getType().getAddressSpace() == LangAS::opencl_constant && |
| Pekka Jaaskelainen | b3cdee0 | 2014-01-23 16:21:02 +0000 | [diff] [blame] | 9268 | Var->getStorageClass() != SC_Extern && !Var->getInit()) { |
| Joey Gouly | 96b94e6 | 2014-01-03 14:16:55 +0000 | [diff] [blame] | 9269 | Diag(Var->getLocation(), diag::err_opencl_constant_no_init); |
| 9270 | Var->setInvalidDecl(); |
| 9271 | return; |
| 9272 | } |
| 9273 | |
| Douglas Gregor | e656562 | 2010-02-09 07:26:29 +0000 | [diff] [blame] | 9274 | switch (Var->isThisDeclarationADefinition()) { |
| 9275 | case VarDecl::Definition: |
| 9276 | if (!Var->isStaticDataMember() || !Var->getAnyInitializer()) |
| 9277 | break; |
| 9278 | |
| 9279 | // We have an out-of-line definition of a static data member |
| 9280 | // that has an in-class initializer, so we type-check this like |
| 9281 | // a declaration. |
| 9282 | // |
| 9283 | // Fall through |
| 9284 | |
| 9285 | case VarDecl::DeclarationOnly: |
| 9286 | // It's only a declaration. |
| 9287 | |
| 9288 | // Block scope. C99 6.7p7: If an identifier for an object is |
| 9289 | // declared with no linkage (C99 6.2.2p6), the type for the |
| 9290 | // object shall be complete. |
| John McCall | 1c9c3fd | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 9291 | if (!Type->isDependentType() && Var->isLocalVarDecl() && |
| Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 9292 | !Var->hasLinkage() && !Var->isInvalidDecl() && |
| Douglas Gregor | e656562 | 2010-02-09 07:26:29 +0000 | [diff] [blame] | 9293 | RequireCompleteType(Var->getLocation(), Type, |
| 9294 | diag::err_typecheck_decl_incomplete_type)) |
| 9295 | Var->setInvalidDecl(); |
| 9296 | |
| 9297 | // Make sure that the type is not abstract. |
| 9298 | if (!Type->isDependentType() && !Var->isInvalidDecl() && |
| 9299 | RequireNonAbstractType(Var->getLocation(), Type, |
| 9300 | diag::err_abstract_type_in_decl, |
| 9301 | AbstractVariableType)) |
| 9302 | Var->setInvalidDecl(); |
| Fariborz Jahanian | 05f4e71 | 2012-08-15 18:42:26 +0000 | [diff] [blame] | 9303 | if (!Type->isDependentType() && !Var->isInvalidDecl() && |
| Fariborz Jahanian | f85f338 | 2012-08-17 21:44:55 +0000 | [diff] [blame] | 9304 | Var->getStorageClass() == SC_PrivateExtern) { |
| Fariborz Jahanian | 05f4e71 | 2012-08-15 18:42:26 +0000 | [diff] [blame] | 9305 | Diag(Var->getLocation(), diag::warn_private_extern); |
| Fariborz Jahanian | f85f338 | 2012-08-17 21:44:55 +0000 | [diff] [blame] | 9306 | Diag(Var->getLocation(), diag::note_private_extern); |
| 9307 | } |
| Fariborz Jahanian | 05f4e71 | 2012-08-15 18:42:26 +0000 | [diff] [blame] | 9308 | |
| Douglas Gregor | e656562 | 2010-02-09 07:26:29 +0000 | [diff] [blame] | 9309 | return; |
| 9310 | |
| 9311 | case VarDecl::TentativeDefinition: |
| 9312 | // File scope. C99 6.9.2p2: A declaration of an identifier for an |
| 9313 | // object that has file scope without an initializer, and without a |
| 9314 | // storage-class specifier or with the storage-class specifier "static", |
| 9315 | // constitutes a tentative definition. Note: A tentative definition with |
| 9316 | // external linkage is valid (C99 6.2.2p5). |
| 9317 | if (!Var->isInvalidDecl()) { |
| 9318 | if (const IncompleteArrayType *ArrayT |
| 9319 | = Context.getAsIncompleteArrayType(Type)) { |
| 9320 | if (RequireCompleteType(Var->getLocation(), |
| 9321 | ArrayT->getElementType(), |
| 9322 | diag::err_illegal_decl_array_incomplete_type)) |
| 9323 | Var->setInvalidDecl(); |
| John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 9324 | } else if (Var->getStorageClass() == SC_Static) { |
| Douglas Gregor | e656562 | 2010-02-09 07:26:29 +0000 | [diff] [blame] | 9325 | // C99 6.9.2p3: If the declaration of an identifier for an object is |
| 9326 | // a tentative definition and has internal linkage (C99 6.2.2p3), the |
| 9327 | // declared type shall not be an incomplete type. |
| 9328 | // NOTE: code such as the following |
| 9329 | // static struct s; |
| 9330 | // struct s { int a; }; |
| 9331 | // is accepted by gcc. Hence here we issue a warning instead of |
| 9332 | // an error and we do not invalidate the static declaration. |
| 9333 | // NOTE: to avoid multiple warnings, only check the first declaration. |
| Rafael Espindola | 3f9e444 | 2013-10-19 02:13:21 +0000 | [diff] [blame] | 9334 | if (Var->isFirstDecl()) |
| Douglas Gregor | e656562 | 2010-02-09 07:26:29 +0000 | [diff] [blame] | 9335 | RequireCompleteType(Var->getLocation(), Type, |
| 9336 | diag::ext_typecheck_decl_incomplete_type); |
| 9337 | } |
| 9338 | } |
| 9339 | |
| 9340 | // Record the tentative definition; we're done. |
| 9341 | if (!Var->isInvalidDecl()) |
| 9342 | TentativeDefinitions.push_back(Var); |
| 9343 | return; |
| 9344 | } |
| 9345 | |
| 9346 | // Provide a specific diagnostic for uninitialized variable |
| 9347 | // definitions with incomplete array type. |
| 9348 | if (Type->isIncompleteArrayType()) { |
| Sebastian Redl | 1060067 | 2009-11-05 19:47:47 +0000 | [diff] [blame] | 9349 | Diag(Var->getLocation(), |
| 9350 | diag::err_typecheck_incomplete_array_needs_initializer); |
| 9351 | Var->setInvalidDecl(); |
| 9352 | return; |
| 9353 | } |
| 9354 | |
| John McCall | a755f0f | 2010-08-01 01:25:24 +0000 | [diff] [blame] | 9355 | // Provide a specific diagnostic for uninitialized variable |
| 9356 | // definitions with reference type. |
| 9357 | if (Type->isReferenceType()) { |
| 9358 | Diag(Var->getLocation(), diag::err_reference_var_requires_init) |
| 9359 | << Var->getDeclName() |
| 9360 | << SourceRange(Var->getLocation(), Var->getLocation()); |
| 9361 | Var->setInvalidDecl(); |
| 9362 | return; |
| 9363 | } |
| Douglas Gregor | e656562 | 2010-02-09 07:26:29 +0000 | [diff] [blame] | 9364 | |
| 9365 | // Do not attempt to type-check the default initializer for a |
| 9366 | // variable with dependent type. |
| 9367 | if (Type->isDependentType()) |
| Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 9368 | return; |
| Douglas Gregor | 5d3507d | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 9369 | |
| Douglas Gregor | e656562 | 2010-02-09 07:26:29 +0000 | [diff] [blame] | 9370 | if (Var->isInvalidDecl()) |
| 9371 | return; |
| Douglas Gregor | c99f155 | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 9372 | |
| David Majnemer | 837d5de | 2014-07-09 17:15:52 +0000 | [diff] [blame] | 9373 | if (!Var->hasAttr<AliasAttr>()) { |
| 9374 | if (RequireCompleteType(Var->getLocation(), |
| 9375 | Context.getBaseElementType(Type), |
| 9376 | diag::err_typecheck_decl_incomplete_type)) { |
| 9377 | Var->setInvalidDecl(); |
| 9378 | return; |
| 9379 | } |
| David Majnemer | e9624ed | 2015-02-08 10:55:14 +0000 | [diff] [blame] | 9380 | } else { |
| 9381 | return; |
| Douglas Gregor | c28b57d | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 9382 | } |
| Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 9383 | |
| Douglas Gregor | e656562 | 2010-02-09 07:26:29 +0000 | [diff] [blame] | 9384 | // The variable can not have an abstract class type. |
| 9385 | if (RequireNonAbstractType(Var->getLocation(), Type, |
| 9386 | diag::err_abstract_type_in_decl, |
| 9387 | AbstractVariableType)) { |
| 9388 | Var->setInvalidDecl(); |
| 9389 | return; |
| 9390 | } |
| 9391 | |
| Douglas Gregor | 9574af6 | 2011-05-21 17:52:48 +0000 | [diff] [blame] | 9392 | // Check for jumps past the implicit initializer. C++0x |
| 9393 | // clarifies that this applies to a "variable with automatic |
| 9394 | // storage duration", not a "local variable". |
| Richard Smith | fe2750d | 2011-10-20 21:42:12 +0000 | [diff] [blame] | 9395 | // C++11 [stmt.dcl]p3 |
| Douglas Gregor | 9574af6 | 2011-05-21 17:52:48 +0000 | [diff] [blame] | 9396 | // A program that jumps from a point where a variable with automatic |
| 9397 | // storage duration is not in scope to a point where it is in scope is |
| 9398 | // ill-formed unless the variable has scalar type, class type with a |
| 9399 | // trivial default constructor and a trivial destructor, a cv-qualified |
| 9400 | // version of one of these types, or an array of one of the preceding |
| 9401 | // types and is declared without an initializer. |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9402 | if (getLangOpts().CPlusPlus && Var->hasLocalStorage()) { |
| Douglas Gregor | 9574af6 | 2011-05-21 17:52:48 +0000 | [diff] [blame] | 9403 | if (const RecordType *Record |
| 9404 | = Context.getBaseElementType(Type)->getAs<RecordType>()) { |
| Alexis Hunt | 466627c | 2011-05-11 22:50:12 +0000 | [diff] [blame] | 9405 | CXXRecordDecl *CXXRecord = cast<CXXRecordDecl>(Record->getDecl()); |
| Richard Smith | fe2750d | 2011-10-20 21:42:12 +0000 | [diff] [blame] | 9406 | // Mark the function for further checking even if the looser rules of |
| 9407 | // C++11 do not require such checks, so that we can diagnose |
| 9408 | // incompatibilities with C++98. |
| 9409 | if (!CXXRecord->isPOD()) |
| Alexis Hunt | 466627c | 2011-05-11 22:50:12 +0000 | [diff] [blame] | 9410 | getCurFunction()->setHasBranchProtectedScope(); |
| 9411 | } |
| Douglas Gregor | e656562 | 2010-02-09 07:26:29 +0000 | [diff] [blame] | 9412 | } |
| Douglas Gregor | 9574af6 | 2011-05-21 17:52:48 +0000 | [diff] [blame] | 9413 | |
| 9414 | // C++03 [dcl.init]p9: |
| 9415 | // If no initializer is specified for an object, and the |
| 9416 | // object is of (possibly cv-qualified) non-POD class type (or |
| 9417 | // array thereof), the object shall be default-initialized; if |
| 9418 | // the object is of const-qualified type, the underlying class |
| 9419 | // type shall have a user-declared default |
| 9420 | // constructor. Otherwise, if no initializer is specified for |
| 9421 | // a non- static object, the object and its subobjects, if |
| 9422 | // any, have an indeterminate initial value); if the object |
| 9423 | // or any of its subobjects are of const-qualified type, the |
| 9424 | // program is ill-formed. |
| 9425 | // C++0x [dcl.init]p11: |
| 9426 | // If no initializer is specified for an object, the object is |
| 9427 | // default-initialized; [...]. |
| 9428 | InitializedEntity Entity = InitializedEntity::InitializeVariable(Var); |
| 9429 | InitializationKind Kind |
| 9430 | = InitializationKind::CreateDefault(Var->getLocation()); |
| Dmitri Gribenko | 78852e9 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 9431 | |
| 9432 | InitializationSequence InitSeq(*this, Entity, Kind, None); |
| 9433 | ExprResult Init = InitSeq.Perform(*this, Entity, Kind, None); |
| Douglas Gregor | 9574af6 | 2011-05-21 17:52:48 +0000 | [diff] [blame] | 9434 | if (Init.isInvalid()) |
| 9435 | Var->setInvalidDecl(); |
| Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 9436 | else if (Init.get()) { |
| Douglas Gregor | 9574af6 | 2011-05-21 17:52:48 +0000 | [diff] [blame] | 9437 | Var->setInit(MaybeCreateExprWithCleanups(Init.get())); |
| Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 9438 | // This is important for template substitution. |
| 9439 | Var->setInitStyle(VarDecl::CallInit); |
| 9440 | } |
| Douglas Gregor | 589973b | 2010-03-08 02:45:10 +0000 | [diff] [blame] | 9441 | |
| John McCall | 8b7fd8f1 | 2011-01-19 11:48:09 +0000 | [diff] [blame] | 9442 | CheckCompleteVariableDeclaration(Var); |
| Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 9443 | } |
| 9444 | } |
| 9445 | |
| Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 9446 | void Sema::ActOnCXXForRangeDecl(Decl *D) { |
| 9447 | VarDecl *VD = dyn_cast<VarDecl>(D); |
| 9448 | if (!VD) { |
| 9449 | Diag(D->getLocation(), diag::err_for_range_decl_must_be_var); |
| 9450 | D->setInvalidDecl(); |
| 9451 | return; |
| 9452 | } |
| 9453 | |
| 9454 | VD->setCXXForRangeDecl(true); |
| 9455 | |
| 9456 | // for-range-declaration cannot be given a storage class specifier. |
| 9457 | int Error = -1; |
| Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 9458 | switch (VD->getStorageClass()) { |
| Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 9459 | case SC_None: |
| 9460 | break; |
| 9461 | case SC_Extern: |
| 9462 | Error = 0; |
| 9463 | break; |
| 9464 | case SC_Static: |
| 9465 | Error = 1; |
| 9466 | break; |
| 9467 | case SC_PrivateExtern: |
| 9468 | Error = 2; |
| 9469 | break; |
| 9470 | case SC_Auto: |
| 9471 | Error = 3; |
| 9472 | break; |
| 9473 | case SC_Register: |
| 9474 | Error = 4; |
| 9475 | break; |
| Peter Collingbourne | 2dbb708 | 2011-09-19 21:14:35 +0000 | [diff] [blame] | 9476 | case SC_OpenCLWorkGroupLocal: |
| Peter Collingbourne | 9a8f153 | 2011-09-20 12:40:26 +0000 | [diff] [blame] | 9477 | llvm_unreachable("Unexpected storage class"); |
| Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 9478 | } |
| Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 9479 | if (Error != -1) { |
| 9480 | Diag(VD->getOuterLocStart(), diag::err_for_range_storage_class) |
| 9481 | << VD->getDeclName() << Error; |
| 9482 | D->setInvalidDecl(); |
| 9483 | } |
| 9484 | } |
| 9485 | |
| Richard Smith | 955bf01 | 2014-06-19 11:42:00 +0000 | [diff] [blame] | 9486 | StmtResult |
| 9487 | Sema::ActOnCXXForRangeIdentifier(Scope *S, SourceLocation IdentLoc, |
| 9488 | IdentifierInfo *Ident, |
| 9489 | ParsedAttributes &Attrs, |
| 9490 | SourceLocation AttrEnd) { |
| 9491 | // C++1y [stmt.iter]p1: |
| 9492 | // A range-based for statement of the form |
| 9493 | // for ( for-range-identifier : for-range-initializer ) statement |
| 9494 | // is equivalent to |
| 9495 | // for ( auto&& for-range-identifier : for-range-initializer ) statement |
| 9496 | DeclSpec DS(Attrs.getPool().getFactory()); |
| 9497 | |
| 9498 | const char *PrevSpec; |
| 9499 | unsigned DiagID; |
| 9500 | DS.SetTypeSpecType(DeclSpec::TST_auto, IdentLoc, PrevSpec, DiagID, |
| 9501 | getPrintingPolicy()); |
| 9502 | |
| 9503 | Declarator D(DS, Declarator::ForContext); |
| 9504 | D.SetIdentifier(Ident, IdentLoc); |
| 9505 | D.takeAttributes(Attrs, AttrEnd); |
| 9506 | |
| 9507 | ParsedAttributes EmptyAttrs(Attrs.getPool().getFactory()); |
| 9508 | D.AddTypeInfo(DeclaratorChunk::getReference(0, IdentLoc, /*lvalue*/false), |
| 9509 | EmptyAttrs, IdentLoc); |
| 9510 | Decl *Var = ActOnDeclarator(S, D); |
| 9511 | cast<VarDecl>(Var)->setCXXForRangeDecl(true); |
| 9512 | FinalizeDeclaration(Var); |
| 9513 | return ActOnDeclStmt(FinalizeDeclaratorGroup(S, DS, Var), IdentLoc, |
| 9514 | AttrEnd.isValid() ? AttrEnd : IdentLoc); |
| 9515 | } |
| 9516 | |
| John McCall | 8b7fd8f1 | 2011-01-19 11:48:09 +0000 | [diff] [blame] | 9517 | void Sema::CheckCompleteVariableDeclaration(VarDecl *var) { |
| 9518 | if (var->isInvalidDecl()) return; |
| 9519 | |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9520 | // In ARC, don't allow jumps past the implicit initialization of a |
| 9521 | // local retaining variable. |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9522 | if (getLangOpts().ObjCAutoRefCount && |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9523 | var->hasLocalStorage()) { |
| 9524 | switch (var->getType().getObjCLifetime()) { |
| 9525 | case Qualifiers::OCL_None: |
| 9526 | case Qualifiers::OCL_ExplicitNone: |
| 9527 | case Qualifiers::OCL_Autoreleasing: |
| 9528 | break; |
| 9529 | |
| 9530 | case Qualifiers::OCL_Weak: |
| 9531 | case Qualifiers::OCL_Strong: |
| 9532 | getCurFunction()->setHasBranchProtectedScope(); |
| 9533 | break; |
| 9534 | } |
| 9535 | } |
| 9536 | |
| John McCall | 8a4e2e4 | 2014-01-29 08:33:09 +0000 | [diff] [blame] | 9537 | // Warn about externally-visible variables being defined without a |
| 9538 | // prior declaration. We only want to do this for global |
| 9539 | // declarations, but we also specifically need to avoid doing it for |
| 9540 | // class members because the linkage of an anonymous class can |
| 9541 | // change if it's later given a typedef name. |
| Eli Friedman | 7d14b3c | 2012-10-23 20:19:32 +0000 | [diff] [blame] | 9542 | if (var->isThisDeclarationADefinition() && |
| John McCall | 8a4e2e4 | 2014-01-29 08:33:09 +0000 | [diff] [blame] | 9543 | var->getDeclContext()->getRedeclContext()->isFileContext() && |
| Eli Friedman | 1f5d888 | 2013-09-24 23:10:08 +0000 | [diff] [blame] | 9544 | var->isExternallyVisible() && var->hasLinkage() && |
| Alp Toker | d4a3f0e | 2014-06-15 23:30:39 +0000 | [diff] [blame] | 9545 | !getDiagnostics().isIgnored(diag::warn_missing_variable_declarations, |
| 9546 | var->getLocation())) { |
| Eli Friedman | 7d14b3c | 2012-10-23 20:19:32 +0000 | [diff] [blame] | 9547 | // Find a previous declaration that's not a definition. |
| 9548 | VarDecl *prev = var->getPreviousDecl(); |
| 9549 | while (prev && prev->isThisDeclarationADefinition()) |
| 9550 | prev = prev->getPreviousDecl(); |
| 9551 | |
| 9552 | if (!prev) |
| 9553 | Diag(var->getLocation(), diag::warn_missing_variable_declarations) << var; |
| 9554 | } |
| 9555 | |
| Reid Kleckner | 92fc017 | 2014-04-30 17:10:18 +0000 | [diff] [blame] | 9556 | if (var->getTLSKind() == VarDecl::TLS_Static) { |
| Abramo Bagnara | 847c660 | 2014-05-22 19:20:46 +0000 | [diff] [blame] | 9557 | const Expr *Culprit; |
| Reid Kleckner | 92fc017 | 2014-04-30 17:10:18 +0000 | [diff] [blame] | 9558 | if (var->getType().isDestructedType()) { |
| 9559 | // GNU C++98 edits for __thread, [basic.start.term]p3: |
| 9560 | // The type of an object with thread storage duration shall not |
| 9561 | // have a non-trivial destructor. |
| 9562 | Diag(var->getLocation(), diag::err_thread_nontrivial_dtor); |
| 9563 | if (getLangOpts().CPlusPlus11) |
| 9564 | Diag(var->getLocation(), diag::note_use_thread_local); |
| 9565 | } else if (getLangOpts().CPlusPlus && var->hasInit() && |
| 9566 | !var->getInit()->isConstantInitializer( |
| Abramo Bagnara | 847c660 | 2014-05-22 19:20:46 +0000 | [diff] [blame] | 9567 | Context, var->getType()->isReferenceType(), &Culprit)) { |
| Reid Kleckner | 92fc017 | 2014-04-30 17:10:18 +0000 | [diff] [blame] | 9568 | // GNU C++98 edits for __thread, [basic.start.init]p4: |
| 9569 | // An object of thread storage duration shall not require dynamic |
| 9570 | // initialization. |
| 9571 | // FIXME: Need strict checking here. |
| Abramo Bagnara | 847c660 | 2014-05-22 19:20:46 +0000 | [diff] [blame] | 9572 | Diag(Culprit->getExprLoc(), diag::err_thread_dynamic_init) |
| 9573 | << Culprit->getSourceRange(); |
| Reid Kleckner | 92fc017 | 2014-04-30 17:10:18 +0000 | [diff] [blame] | 9574 | if (getLangOpts().CPlusPlus11) |
| 9575 | Diag(var->getLocation(), diag::note_use_thread_local); |
| 9576 | } |
| 9577 | |
| Richard Smith | 6ea1a4d | 2013-04-14 20:11:31 +0000 | [diff] [blame] | 9578 | } |
| 9579 | |
| Reid Kleckner | 2a13322 | 2015-03-04 23:39:17 +0000 | [diff] [blame] | 9580 | // Apply section attributes and pragmas to global variables. |
| 9581 | bool GlobalStorage = var->hasGlobalStorage(); |
| 9582 | if (GlobalStorage && var->isThisDeclarationADefinition() && |
| Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 9583 | ActiveTemplateInstantiations.empty()) { |
| 9584 | PragmaStack<StringLiteral *> *Stack = nullptr; |
| Hans Wennborg | 899ded9 | 2014-10-16 20:52:46 +0000 | [diff] [blame] | 9585 | int SectionFlags = ASTContext::PSF_Implicit | ASTContext::PSF_Read; |
| Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 9586 | if (var->getType().isConstQualified()) |
| 9587 | Stack = &ConstSegStack; |
| 9588 | else if (!var->getInit()) { |
| 9589 | Stack = &BSSSegStack; |
| Hans Wennborg | 899ded9 | 2014-10-16 20:52:46 +0000 | [diff] [blame] | 9590 | SectionFlags |= ASTContext::PSF_Write; |
| Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 9591 | } else { |
| 9592 | Stack = &DataSegStack; |
| Hans Wennborg | 899ded9 | 2014-10-16 20:52:46 +0000 | [diff] [blame] | 9593 | SectionFlags |= ASTContext::PSF_Write; |
| Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 9594 | } |
| Reid Kleckner | 2a13322 | 2015-03-04 23:39:17 +0000 | [diff] [blame] | 9595 | if (Stack->CurrentValue && !var->hasAttr<SectionAttr>()) { |
| 9596 | var->addAttr(SectionAttr::CreateImplicit( |
| 9597 | Context, SectionAttr::Declspec_allocate, |
| 9598 | Stack->CurrentValue->getString(), Stack->CurrentPragmaLocation)); |
| 9599 | } |
| Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 9600 | if (const SectionAttr *SA = var->getAttr<SectionAttr>()) |
| 9601 | if (UnifySection(SA->getName(), SectionFlags, var)) |
| 9602 | var->dropAttr<SectionAttr>(); |
| Reid Kleckner | 1a711b1 | 2014-07-22 00:53:05 +0000 | [diff] [blame] | 9603 | |
| 9604 | // Apply the init_seg attribute if this has an initializer. If the |
| 9605 | // initializer turns out to not be dynamic, we'll end up ignoring this |
| 9606 | // attribute. |
| 9607 | if (CurInitSeg && var->getInit()) |
| 9608 | var->addAttr(InitSegAttr::CreateImplicit(Context, CurInitSeg->getString(), |
| 9609 | CurInitSegLoc)); |
| Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 9610 | } |
| 9611 | |
| John McCall | 8b7fd8f1 | 2011-01-19 11:48:09 +0000 | [diff] [blame] | 9612 | // All the following checks are C++ only. |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9613 | if (!getLangOpts().CPlusPlus) return; |
| John McCall | 8b7fd8f1 | 2011-01-19 11:48:09 +0000 | [diff] [blame] | 9614 | |
| Richard Smith | de63d36 | 2012-11-09 23:03:14 +0000 | [diff] [blame] | 9615 | QualType type = var->getType(); |
| 9616 | if (type->isDependentType()) return; |
| John McCall | 8b7fd8f1 | 2011-01-19 11:48:09 +0000 | [diff] [blame] | 9617 | |
| 9618 | // __block variables might require us to capture a copy-initializer. |
| 9619 | if (var->hasAttr<BlocksAttr>()) { |
| 9620 | // It's currently invalid to ever have a __block variable with an |
| 9621 | // array type; should we diagnose that here? |
| 9622 | |
| 9623 | // Regardless, we don't want to ignore array nesting when |
| 9624 | // constructing this copy. |
| John McCall | 8b7fd8f1 | 2011-01-19 11:48:09 +0000 | [diff] [blame] | 9625 | if (type->isStructureOrClassType()) { |
| John McCall | eaef89b | 2013-03-22 02:10:40 +0000 | [diff] [blame] | 9626 | EnterExpressionEvaluationContext scope(*this, PotentiallyEvaluated); |
| John McCall | 8b7fd8f1 | 2011-01-19 11:48:09 +0000 | [diff] [blame] | 9627 | SourceLocation poi = var->getLocation(); |
| John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 9628 | Expr *varRef =new (Context) DeclRefExpr(var, false, type, VK_LValue, poi); |
| Douglas Gregor | ca00645 | 2013-03-07 22:38:24 +0000 | [diff] [blame] | 9629 | ExprResult result |
| 9630 | = PerformMoveOrCopyInitialization( |
| 9631 | InitializedEntity::InitializeBlock(poi, type, false), |
| 9632 | var, var->getType(), varRef, /*AllowNRVO=*/true); |
| John McCall | 8b7fd8f1 | 2011-01-19 11:48:09 +0000 | [diff] [blame] | 9633 | if (!result.isInvalid()) { |
| 9634 | result = MaybeCreateExprWithCleanups(result); |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 9635 | Expr *init = result.getAs<Expr>(); |
| John McCall | 8b7fd8f1 | 2011-01-19 11:48:09 +0000 | [diff] [blame] | 9636 | Context.setBlockVarCopyInits(var, init); |
| 9637 | } |
| 9638 | } |
| 9639 | } |
| 9640 | |
| Richard Smith | eda3c84 | 2011-11-07 22:16:17 +0000 | [diff] [blame] | 9641 | Expr *Init = var->getInit(); |
| Reid Kleckner | 2a13322 | 2015-03-04 23:39:17 +0000 | [diff] [blame] | 9642 | bool IsGlobal = GlobalStorage && !var->isStaticLocal(); |
| Richard Smith | de63d36 | 2012-11-09 23:03:14 +0000 | [diff] [blame] | 9643 | QualType baseType = Context.getBaseElementType(type); |
| Richard Smith | eda3c84 | 2011-11-07 22:16:17 +0000 | [diff] [blame] | 9644 | |
| Richard Smith | bf83009 | 2012-10-29 18:26:47 +0000 | [diff] [blame] | 9645 | if (!var->getDeclContext()->isDependentContext() && |
| 9646 | Init && !Init->isValueDependent()) { |
| Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 9647 | if (IsGlobal && !var->isConstexpr() && |
| Alp Toker | d4a3f0e | 2014-06-15 23:30:39 +0000 | [diff] [blame] | 9648 | !getDiagnostics().isIgnored(diag::warn_global_constructor, |
| 9649 | var->getLocation())) { |
| Eli Friedman | 4c27ac2 | 2013-07-16 22:40:53 +0000 | [diff] [blame] | 9650 | // Warn about globals which don't have a constant initializer. Don't |
| 9651 | // warn about globals with a non-trivial destructor because we already |
| 9652 | // warned about them. |
| 9653 | CXXRecordDecl *RD = baseType->getAsCXXRecordDecl(); |
| 9654 | if (!(RD && !RD->hasTrivialDestructor()) && |
| 9655 | !Init->isConstantInitializer(Context, baseType->isReferenceType())) |
| 9656 | Diag(var->getLocation(), diag::warn_global_constructor) |
| 9657 | << Init->getSourceRange(); |
| 9658 | } |
| Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 9659 | |
| Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 9660 | if (var->isConstexpr()) { |
| Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 9661 | SmallVector<PartialDiagnosticAt, 8> Notes; |
| Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 9662 | if (!var->evaluateValue(Notes) || !var->isInitICE()) { |
| 9663 | SourceLocation DiagLoc = var->getLocation(); |
| 9664 | // If the note doesn't add any useful information other than a source |
| 9665 | // location, fold it into the primary diagnostic. |
| 9666 | if (Notes.size() == 1 && Notes[0].second.getDiagID() == |
| 9667 | diag::note_invalid_subexpr_in_const_expr) { |
| 9668 | DiagLoc = Notes[0].first; |
| 9669 | Notes.clear(); |
| 9670 | } |
| 9671 | Diag(DiagLoc, diag::err_constexpr_var_requires_const_init) |
| 9672 | << var << Init->getSourceRange(); |
| 9673 | for (unsigned I = 0, N = Notes.size(); I != N; ++I) |
| 9674 | Diag(Notes[I].first, Notes[I].second); |
| 9675 | } |
| Daniel Dunbar | 9d35581 | 2012-03-09 01:51:51 +0000 | [diff] [blame] | 9676 | } else if (var->isUsableInConstantExpressions(Context)) { |
| Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 9677 | // Check whether the initializer of a const variable of integral or |
| 9678 | // enumeration type is an ICE now, since we can't tell whether it was |
| 9679 | // initialized by a constant expression if we check later. |
| 9680 | var->checkInitIsICE(); |
| 9681 | } |
| Richard Smith | eda3c84 | 2011-11-07 22:16:17 +0000 | [diff] [blame] | 9682 | } |
| John McCall | 8b7fd8f1 | 2011-01-19 11:48:09 +0000 | [diff] [blame] | 9683 | |
| 9684 | // Require the destructor. |
| 9685 | if (const RecordType *recordType = baseType->getAs<RecordType>()) |
| 9686 | FinalizeVarWithDestructor(var, recordType); |
| 9687 | } |
| 9688 | |
| Richard Smith | b2bc2e6 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 9689 | /// FinalizeDeclaration - called by ParseDeclarationAfterDeclarator to perform |
| 9690 | /// any semantic actions necessary after any initializer has been attached. |
| 9691 | void |
| 9692 | Sema::FinalizeDeclaration(Decl *ThisDecl) { |
| 9693 | // Note that we are no longer parsing the initializer for this declaration. |
| 9694 | ParsingInitForAutoVars.erase(ThisDecl); |
| Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 9695 | |
| Rafael Espindola | db1a477 | 2013-02-22 17:59:16 +0000 | [diff] [blame] | 9696 | VarDecl *VD = dyn_cast_or_null<VarDecl>(ThisDecl); |
| Rafael Espindola | 60470f1 | 2013-01-03 04:05:19 +0000 | [diff] [blame] | 9697 | if (!VD) |
| 9698 | return; |
| 9699 | |
| Nico Rieck | f8e8b5f | 2014-01-21 23:54:36 +0000 | [diff] [blame] | 9700 | checkAttributesAfterMerging(*this, *VD); |
| 9701 | |
| Hans Wennborg | ef2272c | 2014-06-18 15:55:13 +0000 | [diff] [blame] | 9702 | // Static locals inherit dll attributes from their function. |
| 9703 | if (VD->isStaticLocal()) { |
| 9704 | if (FunctionDecl *FD = |
| Richard Trieu | f98341e | 2014-08-22 01:16:44 +0000 | [diff] [blame] | 9705 | dyn_cast_or_null<FunctionDecl>(VD->getParentFunctionOrMethod())) { |
| Hans Wennborg | ef2272c | 2014-06-18 15:55:13 +0000 | [diff] [blame] | 9706 | if (Attr *A = getDLLAttr(FD)) { |
| 9707 | auto *NewAttr = cast<InheritableAttr>(A->clone(getASTContext())); |
| 9708 | NewAttr->setInherited(true); |
| 9709 | VD->addAttr(NewAttr); |
| 9710 | } |
| 9711 | } |
| 9712 | } |
| 9713 | |
| David Majnemer | 5da21da | 2014-10-04 06:51:54 +0000 | [diff] [blame] | 9714 | // Grab the dllimport or dllexport attribute off of the VarDecl. |
| 9715 | const InheritableAttr *DLLAttr = getDLLAttr(VD); |
| 9716 | |
| Nico Rieck | 078d2f8 | 2014-05-29 16:50:20 +0000 | [diff] [blame] | 9717 | // Imported static data members cannot be defined out-of-line. |
| David Majnemer | 5da21da | 2014-10-04 06:51:54 +0000 | [diff] [blame] | 9718 | if (const auto *IA = dyn_cast_or_null<DLLImportAttr>(DLLAttr)) { |
| Nico Rieck | 078d2f8 | 2014-05-29 16:50:20 +0000 | [diff] [blame] | 9719 | if (VD->isStaticDataMember() && VD->isOutOfLine() && |
| 9720 | VD->isThisDeclarationADefinition()) { |
| Hans Wennborg | e9af316 | 2014-06-04 00:18:41 +0000 | [diff] [blame] | 9721 | // We allow definitions of dllimport class template static data members |
| 9722 | // with a warning. |
| Hans Wennborg | cd95922 | 2014-06-09 18:30:28 +0000 | [diff] [blame] | 9723 | CXXRecordDecl *Context = |
| 9724 | cast<CXXRecordDecl>(VD->getFirstDecl()->getDeclContext()); |
| Hans Wennborg | e9af316 | 2014-06-04 00:18:41 +0000 | [diff] [blame] | 9725 | bool IsClassTemplateMember = |
| Hans Wennborg | cd95922 | 2014-06-09 18:30:28 +0000 | [diff] [blame] | 9726 | isa<ClassTemplatePartialSpecializationDecl>(Context) || |
| 9727 | Context->getDescribedClassTemplate(); |
| Hans Wennborg | e9af316 | 2014-06-04 00:18:41 +0000 | [diff] [blame] | 9728 | |
| Nico Rieck | 078d2f8 | 2014-05-29 16:50:20 +0000 | [diff] [blame] | 9729 | Diag(VD->getLocation(), |
| Hans Wennborg | e9af316 | 2014-06-04 00:18:41 +0000 | [diff] [blame] | 9730 | IsClassTemplateMember |
| 9731 | ? diag::warn_attribute_dllimport_static_field_definition |
| 9732 | : diag::err_attribute_dllimport_static_field_definition); |
| Nico Rieck | 078d2f8 | 2014-05-29 16:50:20 +0000 | [diff] [blame] | 9733 | Diag(IA->getLocation(), diag::note_attribute); |
| Hans Wennborg | e9af316 | 2014-06-04 00:18:41 +0000 | [diff] [blame] | 9734 | if (!IsClassTemplateMember) |
| 9735 | VD->setInvalidDecl(); |
| Nico Rieck | 078d2f8 | 2014-05-29 16:50:20 +0000 | [diff] [blame] | 9736 | } |
| 9737 | } |
| 9738 | |
| David Majnemer | 5da21da | 2014-10-04 06:51:54 +0000 | [diff] [blame] | 9739 | // dllimport/dllexport variables cannot be thread local, their TLS index |
| 9740 | // isn't exported with the variable. |
| 9741 | if (DLLAttr && VD->getTLSKind()) { |
| 9742 | Diag(VD->getLocation(), diag::err_attribute_dll_thread_local) << VD |
| 9743 | << DLLAttr; |
| 9744 | VD->setInvalidDecl(); |
| 9745 | } |
| 9746 | |
| Rafael Espindola | 87198cd | 2013-08-16 23:18:50 +0000 | [diff] [blame] | 9747 | if (UsedAttr *Attr = VD->getAttr<UsedAttr>()) { |
| 9748 | if (!Attr->isInherited() && !VD->isThisDeclarationADefinition()) { |
| Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 9749 | Diag(Attr->getLocation(), diag::warn_attribute_ignored) << Attr; |
| Rafael Espindola | 87198cd | 2013-08-16 23:18:50 +0000 | [diff] [blame] | 9750 | VD->dropAttr<UsedAttr>(); |
| 9751 | } |
| 9752 | } |
| 9753 | |
| Rafael Espindola | db1a477 | 2013-02-22 17:59:16 +0000 | [diff] [blame] | 9754 | const DeclContext *DC = VD->getDeclContext(); |
| 9755 | // If there's a #pragma GCC visibility in scope, and this isn't a class |
| 9756 | // member, set the visibility of this variable. |
| John McCall | 8a4e2e4 | 2014-01-29 08:33:09 +0000 | [diff] [blame] | 9757 | if (DC->getRedeclContext()->isFileContext() && VD->isExternallyVisible()) |
| Rafael Espindola | db1a477 | 2013-02-22 17:59:16 +0000 | [diff] [blame] | 9758 | AddPushedVisibilityAttribute(VD); |
| 9759 | |
| Richard Smith | c392617 | 2014-04-02 18:28:36 +0000 | [diff] [blame] | 9760 | // FIXME: Warn on unused templates. |
| Richard Smith | 6c6ef82 | 2014-04-25 19:21:40 +0000 | [diff] [blame] | 9761 | if (VD->isFileVarDecl() && !VD->getDescribedVarTemplate() && |
| 9762 | !isa<VarTemplatePartialSpecializationDecl>(VD)) |
| Rafael Espindola | d2ecc13 | 2013-01-03 04:29:20 +0000 | [diff] [blame] | 9763 | MarkUnusedFileScopedDecl(VD); |
| 9764 | |
| Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 9765 | // Now we have parsed the initializer and can update the table of magic |
| 9766 | // tag values. |
| Rafael Espindola | 60470f1 | 2013-01-03 04:05:19 +0000 | [diff] [blame] | 9767 | if (!VD->hasAttr<TypeTagForDatatypeAttr>() || |
| 9768 | !VD->getType()->isIntegralOrEnumerationType()) |
| 9769 | return; |
| 9770 | |
| Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 9771 | for (const auto *I : ThisDecl->specific_attrs<TypeTagForDatatypeAttr>()) { |
| Rafael Espindola | 60470f1 | 2013-01-03 04:05:19 +0000 | [diff] [blame] | 9772 | const Expr *MagicValueExpr = VD->getInit(); |
| 9773 | if (!MagicValueExpr) { |
| 9774 | continue; |
| Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 9775 | } |
| Rafael Espindola | 60470f1 | 2013-01-03 04:05:19 +0000 | [diff] [blame] | 9776 | llvm::APSInt MagicValueInt; |
| 9777 | if (!MagicValueExpr->isIntegerConstantExpr(MagicValueInt, Context)) { |
| 9778 | Diag(I->getRange().getBegin(), |
| 9779 | diag::err_type_tag_for_datatype_not_ice) |
| 9780 | << LangOpts.CPlusPlus << MagicValueExpr->getSourceRange(); |
| 9781 | continue; |
| 9782 | } |
| 9783 | if (MagicValueInt.getActiveBits() > 64) { |
| 9784 | Diag(I->getRange().getBegin(), |
| 9785 | diag::err_type_tag_for_datatype_too_large) |
| 9786 | << LangOpts.CPlusPlus << MagicValueExpr->getSourceRange(); |
| 9787 | continue; |
| 9788 | } |
| 9789 | uint64_t MagicValue = MagicValueInt.getZExtValue(); |
| 9790 | RegisterTypeTagForDatatype(I->getArgumentKind(), |
| 9791 | MagicValue, |
| 9792 | I->getMatchingCType(), |
| 9793 | I->getLayoutCompatible(), |
| 9794 | I->getMustBeNull()); |
| Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 9795 | } |
| Richard Smith | b2bc2e6 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 9796 | } |
| 9797 | |
| Rafael Espindola | ab41769 | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 9798 | Sema::DeclGroupPtrTy Sema::FinalizeDeclaratorGroup(Scope *S, const DeclSpec &DS, |
| 9799 | ArrayRef<Decl *> Group) { |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 9800 | SmallVector<Decl*, 8> Decls; |
| Eli Friedman | 55b9ecb | 2009-05-29 01:49:24 +0000 | [diff] [blame] | 9801 | |
| 9802 | if (DS.isTypeSpecOwned()) |
| John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 9803 | Decls.push_back(DS.getRepAsDecl()); |
| Eli Friedman | 55b9ecb | 2009-05-29 01:49:24 +0000 | [diff] [blame] | 9804 | |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 9805 | DeclaratorDecl *FirstDeclaratorInGroup = nullptr; |
| Rafael Espindola | ab41769 | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 9806 | for (unsigned i = 0, e = Group.size(); i != e; ++i) |
| David Majnemer | 50ce835 | 2013-09-17 23:57:10 +0000 | [diff] [blame] | 9807 | if (Decl *D = Group[i]) { |
| 9808 | if (DeclaratorDecl *DD = dyn_cast<DeclaratorDecl>(D)) |
| 9809 | if (!FirstDeclaratorInGroup) |
| 9810 | FirstDeclaratorInGroup = DD; |
| Richard Smith | 2abf676 | 2011-02-23 00:37:57 +0000 | [diff] [blame] | 9811 | Decls.push_back(D); |
| David Majnemer | 50ce835 | 2013-09-17 23:57:10 +0000 | [diff] [blame] | 9812 | } |
| Richard Smith | 2abf676 | 2011-02-23 00:37:57 +0000 | [diff] [blame] | 9813 | |
| Eli Friedman | 3b7d46c | 2013-07-10 00:30:46 +0000 | [diff] [blame] | 9814 | if (DeclSpec::isDeclRep(DS.getTypeSpecType())) { |
| David Majnemer | 50ce835 | 2013-09-17 23:57:10 +0000 | [diff] [blame] | 9815 | if (TagDecl *Tag = dyn_cast_or_null<TagDecl>(DS.getRepAsDecl())) { |
| David Majnemer | f9bde28 | 2015-03-11 06:45:39 +0000 | [diff] [blame] | 9816 | handleTagNumbering(Tag, S); |
| David Majnemer | 50ce835 | 2013-09-17 23:57:10 +0000 | [diff] [blame] | 9817 | if (!Tag->hasNameForLinkage() && !Tag->hasDeclaratorForAnonDecl()) |
| 9818 | Tag->setDeclaratorForAnonDecl(FirstDeclaratorInGroup); |
| 9819 | } |
| Eli Friedman | 3b7d46c | 2013-07-10 00:30:46 +0000 | [diff] [blame] | 9820 | } |
| David Blaikie | 095deba | 2012-11-14 01:52:05 +0000 | [diff] [blame] | 9821 | |
| Rafael Espindola | ab41769 | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 9822 | return BuildDeclaratorGroup(Decls, DS.containsPlaceholderType()); |
| Richard Smith | 2abf676 | 2011-02-23 00:37:57 +0000 | [diff] [blame] | 9823 | } |
| 9824 | |
| 9825 | /// BuildDeclaratorGroup - convert a list of declarations into a declaration |
| 9826 | /// group, performing any necessary semantic checking. |
| 9827 | Sema::DeclGroupPtrTy |
| Craig Topper | e3d2ecbe | 2014-06-28 23:22:33 +0000 | [diff] [blame] | 9828 | Sema::BuildDeclaratorGroup(MutableArrayRef<Decl *> Group, |
| Richard Smith | 2abf676 | 2011-02-23 00:37:57 +0000 | [diff] [blame] | 9829 | bool TypeMayContainAuto) { |
| Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 9830 | // C++0x [dcl.spec.auto]p7: |
| 9831 | // If the type deduced for the template parameter U is not the same in each |
| 9832 | // deduction, the program is ill-formed. |
| 9833 | // FIXME: When initializer-list support is added, a distinction is needed |
| 9834 | // between the deduced type U and the deduced type which 'auto' stands for. |
| 9835 | // auto a = 0, b = { 1, 2, 3 }; |
| 9836 | // is legal because the deduced type U is 'int' in both cases. |
| Rafael Espindola | ab41769 | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 9837 | if (TypeMayContainAuto && Group.size() > 1) { |
| Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 9838 | QualType Deduced; |
| 9839 | CanQualType DeducedCanon; |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 9840 | VarDecl *DeducedDecl = nullptr; |
| Rafael Espindola | ab41769 | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 9841 | for (unsigned i = 0, e = Group.size(); i != e; ++i) { |
| Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 9842 | if (VarDecl *D = dyn_cast<VarDecl>(Group[i])) { |
| 9843 | AutoType *AT = D->getType()->getContainedAutoType(); |
| Richard Smith | 2abf676 | 2011-02-23 00:37:57 +0000 | [diff] [blame] | 9844 | // Don't reissue diagnostics when instantiating a template. |
| 9845 | if (AT && D->isInvalidDecl()) |
| 9846 | break; |
| Richard Smith | 27d807c | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 9847 | QualType U = AT ? AT->getDeducedType() : QualType(); |
| 9848 | if (!U.isNull()) { |
| Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 9849 | CanQualType UCanon = Context.getCanonicalType(U); |
| 9850 | if (Deduced.isNull()) { |
| 9851 | Deduced = U; |
| 9852 | DeducedCanon = UCanon; |
| 9853 | DeducedDecl = D; |
| 9854 | } else if (DeducedCanon != UCanon) { |
| Richard Smith | 2abf676 | 2011-02-23 00:37:57 +0000 | [diff] [blame] | 9855 | Diag(D->getTypeSourceInfo()->getTypeLoc().getBeginLoc(), |
| 9856 | diag::err_auto_different_deductions) |
| Richard Smith | 489e4e0 | 2013-05-04 04:19:27 +0000 | [diff] [blame] | 9857 | << (AT->isDecltypeAuto() ? 1 : 0) |
| Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 9858 | << Deduced << DeducedDecl->getDeclName() |
| 9859 | << U << D->getDeclName() |
| 9860 | << DeducedDecl->getInit()->getSourceRange() |
| 9861 | << D->getInit()->getSourceRange(); |
| Richard Smith | 2abf676 | 2011-02-23 00:37:57 +0000 | [diff] [blame] | 9862 | D->setInvalidDecl(); |
| Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 9863 | break; |
| 9864 | } |
| 9865 | } |
| 9866 | } |
| 9867 | } |
| 9868 | } |
| 9869 | |
| Rafael Espindola | ab41769 | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 9870 | ActOnDocumentableDecls(Group); |
| Dmitri Gribenko | f26054f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 9871 | |
| Rafael Espindola | ab41769 | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 9872 | return DeclGroupPtrTy::make( |
| 9873 | DeclGroupRef::Create(Context, Group.data(), Group.size())); |
| Chris Lattner | 776fac8 | 2007-06-09 00:53:06 +0000 | [diff] [blame] | 9874 | } |
| Steve Naroff | 7e6f7c2 | 2007-08-28 03:03:08 +0000 | [diff] [blame] | 9875 | |
| Dmitri Gribenko | f26054f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 9876 | void Sema::ActOnDocumentableDecl(Decl *D) { |
| Rafael Espindola | ab41769 | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 9877 | ActOnDocumentableDecls(D); |
| Dmitri Gribenko | f26054f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 9878 | } |
| 9879 | |
| Rafael Espindola | ab41769 | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 9880 | void Sema::ActOnDocumentableDecls(ArrayRef<Decl *> Group) { |
| Dmitri Gribenko | f26054f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 9881 | // Don't parse the comment if Doxygen diagnostics are ignored. |
| Rafael Espindola | ab41769 | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 9882 | if (Group.empty() || !Group[0]) |
| Dmitri Gribenko | 7fe22f6 | 2015-03-17 18:03:42 +0000 | [diff] [blame] | 9883 | return; |
| Dmitri Gribenko | f26054f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 9884 | |
| Dmitri Gribenko | 7fe22f6 | 2015-03-17 18:03:42 +0000 | [diff] [blame] | 9885 | if (Diags.isIgnored(diag::warn_doc_param_not_found, |
| 9886 | Group[0]->getLocation()) && |
| 9887 | Diags.isIgnored(diag::warn_unknown_comment_command_name, |
| 9888 | Group[0]->getLocation())) |
| Dmitri Gribenko | f26054f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 9889 | return; |
| 9890 | |
| Rafael Espindola | ab41769 | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 9891 | if (Group.size() >= 2) { |
| Dmitri Gribenko | f26054f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 9892 | // This is a decl group. Normally it will contain only declarations |
| Rafael Espindola | ab41769 | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 9893 | // produced from declarator list. But in case we have any definitions or |
| Dmitri Gribenko | f26054f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 9894 | // additional declaration references: |
| 9895 | // 'typedef struct S {} S;' |
| 9896 | // 'typedef struct S *S;' |
| 9897 | // 'struct S *pS;' |
| 9898 | // FinalizeDeclaratorGroup adds these as separate declarations. |
| 9899 | Decl *MaybeTagDecl = Group[0]; |
| 9900 | if (MaybeTagDecl && isa<TagDecl>(MaybeTagDecl)) { |
| Rafael Espindola | ab41769 | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 9901 | Group = Group.slice(1); |
| Dmitri Gribenko | f26054f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 9902 | } |
| 9903 | } |
| 9904 | |
| 9905 | // See if there are any new comments that are not attached to a decl. |
| 9906 | ArrayRef<RawComment *> Comments = Context.getRawCommentList().getComments(); |
| 9907 | if (!Comments.empty() && |
| 9908 | !Comments.back()->isAttached()) { |
| 9909 | // There is at least one comment that not attached to a decl. |
| 9910 | // Maybe it should be attached to one of these decls? |
| 9911 | // |
| 9912 | // Note that this way we pick up not only comments that precede the |
| 9913 | // declaration, but also comments that *follow* the declaration -- thanks to |
| 9914 | // the lookahead in the lexer: we've consumed the semicolon and looked |
| 9915 | // ahead through comments. |
| Rafael Espindola | ab41769 | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 9916 | for (unsigned i = 0, e = Group.size(); i != e; ++i) |
| Dmitri Gribenko | 6743e04 | 2012-09-29 11:40:46 +0000 | [diff] [blame] | 9917 | Context.getCommentForDecl(Group[i], &PP); |
| Dmitri Gribenko | f26054f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 9918 | } |
| 9919 | } |
| Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 9920 | |
| Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 9921 | /// ActOnParamDeclarator - Called from Parser::ParseFunctionDeclarator() |
| 9922 | /// to introduce parameters into function prototype scope. |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 9923 | Decl *Sema::ActOnParamDeclarator(Scope *S, Declarator &D) { |
| Chris Lattner | cf31de3 | 2008-06-26 06:49:43 +0000 | [diff] [blame] | 9924 | const DeclSpec &DS = D.getDeclSpec(); |
| Douglas Gregor | ad59050 | 2008-12-15 23:53:10 +0000 | [diff] [blame] | 9925 | |
| Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 9926 | // Verify C99 6.7.5.3p2: The only SCS allowed is 'register'. |
| Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 9927 | |
| Peter Collingbourne | 99eddc3 | 2011-10-21 11:55:09 +0000 | [diff] [blame] | 9928 | // C++03 [dcl.stc]p2 also permits 'auto'. |
| Craig Topper | c79e5e3 | 2014-10-31 06:57:13 +0000 | [diff] [blame] | 9929 | StorageClass SC = SC_None; |
| Daniel Dunbar | 0ff4192 | 2008-09-03 21:54:21 +0000 | [diff] [blame] | 9930 | if (DS.getStorageClassSpec() == DeclSpec::SCS_register) { |
| Craig Topper | c79e5e3 | 2014-10-31 06:57:13 +0000 | [diff] [blame] | 9931 | SC = SC_Register; |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9932 | } else if (getLangOpts().CPlusPlus && |
| Peter Collingbourne | 99eddc3 | 2011-10-21 11:55:09 +0000 | [diff] [blame] | 9933 | DS.getStorageClassSpec() == DeclSpec::SCS_auto) { |
| Craig Topper | c79e5e3 | 2014-10-31 06:57:13 +0000 | [diff] [blame] | 9934 | SC = SC_Auto; |
| Daniel Dunbar | 0ff4192 | 2008-09-03 21:54:21 +0000 | [diff] [blame] | 9935 | } else if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified) { |
| Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 9936 | Diag(DS.getStorageClassSpecLoc(), |
| 9937 | diag::err_invalid_storage_class_in_func_decl); |
| Chris Lattner | cf31de3 | 2008-06-26 06:49:43 +0000 | [diff] [blame] | 9938 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
| Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 9939 | } |
| Eli Friedman | d5c0eed | 2009-04-19 20:27:55 +0000 | [diff] [blame] | 9940 | |
| Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 9941 | if (DeclSpec::TSCS TSCS = DS.getThreadStorageClassSpec()) |
| 9942 | Diag(DS.getThreadStorageClassSpecLoc(), diag::err_invalid_thread) |
| 9943 | << DeclSpec::getSpecifierName(TSCS); |
| 9944 | if (DS.isConstexprSpecified()) |
| 9945 | Diag(DS.getConstexprSpecLoc(), diag::err_invalid_constexpr) |
| Richard Smith | a77a0a6 | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 9946 | << 0; |
| Eli Friedman | d5c0eed | 2009-04-19 20:27:55 +0000 | [diff] [blame] | 9947 | |
| Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 9948 | DiagnoseFunctionSpecifiers(DS); |
| Eli Friedman | 574c745 | 2009-04-07 19:37:57 +0000 | [diff] [blame] | 9949 | |
| Argyrios Kyrtzidis | ef7022f | 2011-06-28 03:01:15 +0000 | [diff] [blame] | 9950 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
| John McCall | 8cb7bdf | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 9951 | QualType parmDeclType = TInfo->getType(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9952 | |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9953 | if (getLangOpts().CPlusPlus) { |
| Douglas Gregor | 27b4c16 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 9954 | // Check that there are no default arguments inside the type of this |
| 9955 | // parameter. |
| 9956 | CheckExtraCXXDefaultArguments(D); |
| Douglas Gregor | 27b4c16 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 9957 | |
| 9958 | // Parameter declarators cannot be qualified (C++ [dcl.meaning]p1). |
| 9959 | if (D.getCXXScopeSpec().isSet()) { |
| 9960 | Diag(D.getIdentifierLoc(), diag::err_qualified_param_declarator) |
| 9961 | << D.getCXXScopeSpec().getRange(); |
| 9962 | D.getCXXScopeSpec().clear(); |
| 9963 | } |
| Douglas Gregor | d6ab874 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 9964 | } |
| 9965 | |
| Alexis Hunt | a56cbcc | 2010-11-03 01:07:06 +0000 | [diff] [blame] | 9966 | // Ensure we have a valid name |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 9967 | IdentifierInfo *II = nullptr; |
| Alexis Hunt | a56cbcc | 2010-11-03 01:07:06 +0000 | [diff] [blame] | 9968 | if (D.hasName()) { |
| 9969 | II = D.getIdentifier(); |
| 9970 | if (!II) { |
| 9971 | Diag(D.getIdentifierLoc(), diag::err_bad_parameter_name) |
| Aaron Ballman | fee0cd4 | 2014-01-03 13:34:55 +0000 | [diff] [blame] | 9972 | << GetNameForDeclarator(D).getName(); |
| Alexis Hunt | a56cbcc | 2010-11-03 01:07:06 +0000 | [diff] [blame] | 9973 | D.setInvalidType(true); |
| 9974 | } |
| 9975 | } |
| 9976 | |
| Chris Lattner | dd1cb5b | 2010-02-22 00:40:25 +0000 | [diff] [blame] | 9977 | // 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] | 9978 | if (II) { |
| John McCall | 84f0267 | 2010-03-18 06:42:38 +0000 | [diff] [blame] | 9979 | LookupResult R(*this, II, D.getIdentifierLoc(), LookupOrdinaryName, |
| 9980 | ForRedeclaration); |
| 9981 | LookupName(R, S); |
| 9982 | if (R.isSingleResult()) { |
| 9983 | NamedDecl *PrevDecl = R.getFoundDecl(); |
| Chris Lattner | d977351 | 2009-01-21 02:38:50 +0000 | [diff] [blame] | 9984 | if (PrevDecl->isTemplateParameter()) { |
| 9985 | // Maybe we will complain about the shadowed template parameter. |
| 9986 | DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), PrevDecl); |
| 9987 | // Just pretend that we didn't see the previous declaration. |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 9988 | PrevDecl = nullptr; |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 9989 | } else if (S->isDeclScope(PrevDecl)) { |
| Chris Lattner | d977351 | 2009-01-21 02:38:50 +0000 | [diff] [blame] | 9990 | Diag(D.getIdentifierLoc(), diag::err_param_redefinition) << II; |
| Chris Lattner | dd1cb5b | 2010-02-22 00:40:25 +0000 | [diff] [blame] | 9991 | Diag(PrevDecl->getLocation(), diag::note_previous_declaration); |
| Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 9992 | |
| Chris Lattner | d977351 | 2009-01-21 02:38:50 +0000 | [diff] [blame] | 9993 | // Recover by removing the name |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 9994 | II = nullptr; |
| 9995 | D.SetIdentifier(nullptr, D.getIdentifierLoc()); |
| Fariborz Jahanian | 5ec502e | 2010-02-12 21:53:14 +0000 | [diff] [blame] | 9996 | D.setInvalidType(true); |
| Chris Lattner | d977351 | 2009-01-21 02:38:50 +0000 | [diff] [blame] | 9997 | } |
| Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 9998 | } |
| Chris Lattner | c5cdf4d | 2007-01-21 07:42:07 +0000 | [diff] [blame] | 9999 | } |
| Steve Naroff | 773df5c | 2007-08-07 22:44:21 +0000 | [diff] [blame] | 10000 | |
| John McCall | f7b2fb5 | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 10001 | // Temporarily put parameter variables in the translation unit, not |
| 10002 | // the enclosing context. This prevents them from accidentally |
| 10003 | // looking like class members in C++. |
| Douglas Gregor | 940bca7 | 2010-04-12 07:48:19 +0000 | [diff] [blame] | 10004 | ParmVarDecl *New = CheckParameter(Context.getTranslationUnitDecl(), |
| Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 10005 | D.getLocStart(), |
| Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 10006 | D.getIdentifierLoc(), II, |
| 10007 | parmDeclType, TInfo, |
| Craig Topper | c79e5e3 | 2014-10-31 06:57:13 +0000 | [diff] [blame] | 10008 | SC); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10009 | |
| Chris Lattner | f6d1c9c | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 10010 | if (D.isInvalidType()) |
| John McCall | 8fb0d9d | 2011-05-01 22:35:37 +0000 | [diff] [blame] | 10011 | New->setInvalidDecl(); |
| 10012 | |
| 10013 | assert(S->isFunctionPrototypeScope()); |
| 10014 | assert(S->getFunctionPrototypeDepth() >= 1); |
| 10015 | New->setScopeInfo(S->getFunctionPrototypeDepth() - 1, |
| 10016 | S->getNextFunctionPrototypeIndex()); |
| Douglas Gregor | 940bca7 | 2010-04-12 07:48:19 +0000 | [diff] [blame] | 10017 | |
| Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 10018 | // Add the parameter declaration into this scope. |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10019 | S->AddDecl(New); |
| Argyrios Kyrtzidis | b8a4920 | 2008-04-12 00:47:19 +0000 | [diff] [blame] | 10020 | if (II) |
| Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 10021 | IdResolver.AddDecl(New); |
| Nate Begeman | d8c4156 | 2008-02-17 21:20:31 +0000 | [diff] [blame] | 10022 | |
| Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 10023 | ProcessDeclAttributes(S, New, D); |
| Mike Stump | e9efa80 | 2009-04-30 00:19:40 +0000 | [diff] [blame] | 10024 | |
| Douglas Gregor | 4186681 | 2011-09-12 18:37:38 +0000 | [diff] [blame] | 10025 | if (D.getDeclSpec().isModulePrivateSpecified()) |
| 10026 | Diag(New->getLocation(), diag::err_module_private_local) |
| 10027 | << 1 << New->getDeclName() |
| 10028 | << SourceRange(D.getDeclSpec().getModulePrivateSpecLoc()) |
| 10029 | << FixItHint::CreateRemoval(D.getDeclSpec().getModulePrivateSpecLoc()); |
| 10030 | |
| Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 10031 | if (New->hasAttr<BlocksAttr>()) { |
| Mike Stump | e9efa80 | 2009-04-30 00:19:40 +0000 | [diff] [blame] | 10032 | Diag(New->getLocation(), diag::err_block_on_nonlocal); |
| 10033 | } |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10034 | return New; |
| Chris Lattner | c5cdf4d | 2007-01-21 07:42:07 +0000 | [diff] [blame] | 10035 | } |
| Fariborz Jahanian | 56ff146 | 2007-11-08 23:49:49 +0000 | [diff] [blame] | 10036 | |
| John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 10037 | /// \brief Synthesizes a variable for a parameter arising from a |
| 10038 | /// typedef. |
| 10039 | ParmVarDecl *Sema::BuildParmVarDeclForTypedef(DeclContext *DC, |
| 10040 | SourceLocation Loc, |
| 10041 | QualType T) { |
| Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 10042 | /* FIXME: setting StartLoc == Loc. |
| 10043 | Would it be worth to modify callers so as to provide proper source |
| 10044 | location for the unnamed parameters, embedding the parameter's type? */ |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 10045 | ParmVarDecl *Param = ParmVarDecl::Create(Context, DC, Loc, Loc, nullptr, |
| John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 10046 | T, Context.getTrivialTypeSourceInfo(T, Loc), |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 10047 | SC_None, nullptr); |
| John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 10048 | Param->setImplicit(); |
| 10049 | return Param; |
| 10050 | } |
| 10051 | |
| John McCall | c599064 | 2010-08-24 09:05:15 +0000 | [diff] [blame] | 10052 | void Sema::DiagnoseUnusedParameters(ParmVarDecl * const *Param, |
| 10053 | ParmVarDecl * const *ParamEnd) { |
| John McCall | c599064 | 2010-08-24 09:05:15 +0000 | [diff] [blame] | 10054 | // Don't diagnose unused-parameter errors in template instantiations; we |
| 10055 | // will already have done so in the template itself. |
| 10056 | if (!ActiveTemplateInstantiations.empty()) |
| 10057 | return; |
| 10058 | |
| 10059 | for (; Param != ParamEnd; ++Param) { |
| Eli Friedman | c09e055 | 2012-01-13 23:41:25 +0000 | [diff] [blame] | 10060 | if (!(*Param)->isReferenced() && (*Param)->getDeclName() && |
| John McCall | c599064 | 2010-08-24 09:05:15 +0000 | [diff] [blame] | 10061 | !(*Param)->hasAttr<UnusedAttr>()) { |
| 10062 | Diag((*Param)->getLocation(), diag::warn_unused_parameter) |
| 10063 | << (*Param)->getDeclName(); |
| 10064 | } |
| 10065 | } |
| 10066 | } |
| 10067 | |
| Argyrios Kyrtzidis | af84ec0 | 2010-11-17 23:11:54 +0000 | [diff] [blame] | 10068 | void Sema::DiagnoseSizeOfParametersAndReturnValue(ParmVarDecl * const *Param, |
| 10069 | ParmVarDecl * const *ParamEnd, |
| 10070 | QualType ReturnTy, |
| 10071 | NamedDecl *D) { |
| Argyrios Kyrtzidis | ef6c8da | 2010-11-18 00:20:36 +0000 | [diff] [blame] | 10072 | if (LangOpts.NumLargeByValueCopy == 0) // No check. |
| Argyrios Kyrtzidis | af84ec0 | 2010-11-17 23:11:54 +0000 | [diff] [blame] | 10073 | return; |
| 10074 | |
| Argyrios Kyrtzidis | ef6c8da | 2010-11-18 00:20:36 +0000 | [diff] [blame] | 10075 | // Warn if the return value is pass-by-value and larger than the specified |
| 10076 | // threshold. |
| Eli Friedman | 7f21bd7 | 2012-01-09 23:46:59 +0000 | [diff] [blame] | 10077 | if (!ReturnTy->isDependentType() && ReturnTy.isPODType(Context)) { |
| Argyrios Kyrtzidis | af84ec0 | 2010-11-17 23:11:54 +0000 | [diff] [blame] | 10078 | unsigned Size = Context.getTypeSizeInChars(ReturnTy).getQuantity(); |
| Argyrios Kyrtzidis | ef6c8da | 2010-11-18 00:20:36 +0000 | [diff] [blame] | 10079 | if (Size > LangOpts.NumLargeByValueCopy) |
| Argyrios Kyrtzidis | af84ec0 | 2010-11-17 23:11:54 +0000 | [diff] [blame] | 10080 | Diag(D->getLocation(), diag::warn_return_value_size) |
| 10081 | << D->getDeclName() << Size; |
| 10082 | } |
| 10083 | |
| Argyrios Kyrtzidis | ef6c8da | 2010-11-18 00:20:36 +0000 | [diff] [blame] | 10084 | // Warn if any parameter is pass-by-value and larger than the specified |
| 10085 | // threshold. |
| Argyrios Kyrtzidis | af84ec0 | 2010-11-17 23:11:54 +0000 | [diff] [blame] | 10086 | for (; Param != ParamEnd; ++Param) { |
| 10087 | QualType T = (*Param)->getType(); |
| Eli Friedman | 7f21bd7 | 2012-01-09 23:46:59 +0000 | [diff] [blame] | 10088 | if (T->isDependentType() || !T.isPODType(Context)) |
| Argyrios Kyrtzidis | af84ec0 | 2010-11-17 23:11:54 +0000 | [diff] [blame] | 10089 | continue; |
| 10090 | unsigned Size = Context.getTypeSizeInChars(T).getQuantity(); |
| Argyrios Kyrtzidis | ef6c8da | 2010-11-18 00:20:36 +0000 | [diff] [blame] | 10091 | if (Size > LangOpts.NumLargeByValueCopy) |
| Argyrios Kyrtzidis | af84ec0 | 2010-11-17 23:11:54 +0000 | [diff] [blame] | 10092 | Diag((*Param)->getLocation(), diag::warn_parameter_size) |
| 10093 | << (*Param)->getDeclName() << Size; |
| 10094 | } |
| 10095 | } |
| 10096 | |
| Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 10097 | ParmVarDecl *Sema::CheckParameter(DeclContext *DC, SourceLocation StartLoc, |
| 10098 | SourceLocation NameLoc, IdentifierInfo *Name, |
| 10099 | QualType T, TypeSourceInfo *TSInfo, |
| Craig Topper | c79e5e3 | 2014-10-31 06:57:13 +0000 | [diff] [blame] | 10100 | StorageClass SC) { |
| Reid Kleckner | 17aeeeb | 2013-06-08 18:19:52 +0000 | [diff] [blame] | 10101 | // In ARC, infer a lifetime qualifier for appropriate parameter types. |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10102 | if (getLangOpts().ObjCAutoRefCount && |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 10103 | T.getObjCLifetime() == Qualifiers::OCL_None && |
| Reid Kleckner | 17aeeeb | 2013-06-08 18:19:52 +0000 | [diff] [blame] | 10104 | T->isObjCLifetimeType()) { |
| 10105 | |
| 10106 | Qualifiers::ObjCLifetime lifetime; |
| 10107 | |
| 10108 | // Special cases for arrays: |
| 10109 | // - if it's const, use __unsafe_unretained |
| 10110 | // - otherwise, it's an error |
| 10111 | if (T->isArrayType()) { |
| 10112 | if (!T.isConstQualified()) { |
| 10113 | DelayedDiagnostics.add( |
| 10114 | sema::DelayedDiagnostic::makeForbiddenType( |
| Fariborz Jahanian | ed1933b | 2011-10-03 22:11:57 +0000 | [diff] [blame] | 10115 | NameLoc, diag::err_arc_array_param_no_ownership, T, false)); |
| Reid Kleckner | 17aeeeb | 2013-06-08 18:19:52 +0000 | [diff] [blame] | 10116 | } |
| 10117 | lifetime = Qualifiers::OCL_ExplicitNone; |
| 10118 | } else { |
| 10119 | lifetime = T->getObjCARCImplicitLifetime(); |
| 10120 | } |
| 10121 | T = Context.getLifetimeQualifiedType(T, lifetime); |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 10122 | } |
| 10123 | |
| Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 10124 | ParmVarDecl *New = ParmVarDecl::Create(Context, DC, StartLoc, NameLoc, Name, |
| Douglas Gregor | 8428064 | 2011-07-12 04:42:08 +0000 | [diff] [blame] | 10125 | Context.getAdjustedParameterType(T), |
| Craig Topper | c79e5e3 | 2014-10-31 06:57:13 +0000 | [diff] [blame] | 10126 | TSInfo, SC, nullptr); |
| Douglas Gregor | 940bca7 | 2010-04-12 07:48:19 +0000 | [diff] [blame] | 10127 | |
| 10128 | // Parameters can not be abstract class types. |
| 10129 | // For record types, this is done by the AbstractClassUsageDiagnoser once |
| 10130 | // the class has been completely parsed. |
| 10131 | if (!CurContext->isRecord() && |
| 10132 | RequireNonAbstractType(NameLoc, T, diag::err_abstract_type_in_decl, |
| 10133 | AbstractParamType)) |
| 10134 | New->setInvalidDecl(); |
| 10135 | |
| 10136 | // Parameter declarators cannot be interface types. All ObjC objects are |
| 10137 | // passed by reference. |
| John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 10138 | if (T->isObjCObjectType()) { |
| Fariborz Jahanian | 7a05536 | 2012-05-09 21:49:29 +0000 | [diff] [blame] | 10139 | SourceLocation TypeEndLoc = TSInfo->getTypeLoc().getLocEnd(); |
| Douglas Gregor | 940bca7 | 2010-04-12 07:48:19 +0000 | [diff] [blame] | 10140 | Diag(NameLoc, |
| Fariborz Jahanian | 6507135e | 2011-07-26 17:58:54 +0000 | [diff] [blame] | 10141 | diag::err_object_cannot_be_passed_returned_by_value) << 1 << T |
| Fariborz Jahanian | 7a05536 | 2012-05-09 21:49:29 +0000 | [diff] [blame] | 10142 | << FixItHint::CreateInsertion(TypeEndLoc, "*"); |
| Fariborz Jahanian | 6507135e | 2011-07-26 17:58:54 +0000 | [diff] [blame] | 10143 | T = Context.getObjCObjectPointerType(T); |
| 10144 | New->setType(T); |
| Douglas Gregor | 940bca7 | 2010-04-12 07:48:19 +0000 | [diff] [blame] | 10145 | } |
| 10146 | |
| 10147 | // ISO/IEC TR 18037 S6.7.3: "The type of an object with automatic storage |
| 10148 | // duration shall not be qualified by an address-space qualifier." |
| 10149 | // Since all parameters have automatic store duration, they can not have |
| 10150 | // an address space. |
| 10151 | if (T.getAddressSpace() != 0) { |
| Fraser Cormack | 01648e0 | 2014-04-15 11:38:29 +0000 | [diff] [blame] | 10152 | // OpenCL allows function arguments declared to be an array of a type |
| 10153 | // to be qualified with an address space. |
| 10154 | if (!(getLangOpts().OpenCL && T->isArrayType())) { |
| 10155 | Diag(NameLoc, diag::err_arg_with_address_space); |
| 10156 | New->setInvalidDecl(); |
| 10157 | } |
| Douglas Gregor | 940bca7 | 2010-04-12 07:48:19 +0000 | [diff] [blame] | 10158 | } |
| 10159 | |
| 10160 | return New; |
| 10161 | } |
| 10162 | |
| Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 10163 | void Sema::ActOnFinishKNRParamDeclarations(Scope *S, Declarator &D, |
| 10164 | SourceLocation LocAfterDecls) { |
| Abramo Bagnara | 924a8f3 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 10165 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo(); |
| Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 10166 | |
| Chris Lattner | 5c5fbcc | 2006-12-03 08:41:30 +0000 | [diff] [blame] | 10167 | // Verify 6.9.1p6: 'every identifier in the identifier list shall be declared' |
| 10168 | // for a K&R function. |
| 10169 | if (!FTI.hasPrototype) { |
| Alp Toker | c535072 | 2014-02-26 22:27:52 +0000 | [diff] [blame] | 10170 | for (int i = FTI.NumParams; i != 0; /* decrement in loop */) { |
| Douglas Gregor | 862ffb1 | 2009-04-02 03:14:12 +0000 | [diff] [blame] | 10171 | --i; |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 10172 | if (FTI.Params[i].Param == nullptr) { |
| Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 10173 | SmallString<256> Code; |
| Alp Toker | c535072 | 2014-02-26 22:27:52 +0000 | [diff] [blame] | 10174 | llvm::raw_svector_ostream(Code) |
| 10175 | << " int " << FTI.Params[i].Ident->getName() << ";\n"; |
| 10176 | Diag(FTI.Params[i].IdentLoc, diag::ext_param_not_declared) |
| 10177 | << FTI.Params[i].Ident |
| Yaron Keren | 92e1b62 | 2015-03-18 10:17:07 +0000 | [diff] [blame] | 10178 | << FixItHint::CreateInsertion(LocAfterDecls, Code); |
| Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 10179 | |
| Chris Lattner | 5c5fbcc | 2006-12-03 08:41:30 +0000 | [diff] [blame] | 10180 | // Implicitly declare the argument as type 'int' for lack of a better |
| 10181 | // type. |
| John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 10182 | AttributeFactory attrs; |
| 10183 | DeclSpec DS(attrs); |
| Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 10184 | const char* PrevSpec; // unused |
| John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 10185 | unsigned DiagID; // unused |
| Alp Toker | c535072 | 2014-02-26 22:27:52 +0000 | [diff] [blame] | 10186 | DS.SetTypeSpecType(DeclSpec::TST_int, FTI.Params[i].IdentLoc, PrevSpec, |
| 10187 | DiagID, Context.getPrintingPolicy()); |
| Abramo Bagnara | 71f32c1 | 2012-10-04 21:38:29 +0000 | [diff] [blame] | 10188 | // Use the identifier location for the type source range. |
| Alp Toker | c535072 | 2014-02-26 22:27:52 +0000 | [diff] [blame] | 10189 | DS.SetRangeStart(FTI.Params[i].IdentLoc); |
| 10190 | DS.SetRangeEnd(FTI.Params[i].IdentLoc); |
| Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 10191 | Declarator ParamD(DS, Declarator::KNRTypeListContext); |
| Alp Toker | c535072 | 2014-02-26 22:27:52 +0000 | [diff] [blame] | 10192 | ParamD.SetIdentifier(FTI.Params[i].Ident, FTI.Params[i].IdentLoc); |
| 10193 | FTI.Params[i].Param = ActOnParamDeclarator(S, ParamD); |
| Chris Lattner | 5c5fbcc | 2006-12-03 08:41:30 +0000 | [diff] [blame] | 10194 | } |
| 10195 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10196 | } |
| Douglas Gregor | 9aa8904 | 2009-01-23 16:23:13 +0000 | [diff] [blame] | 10197 | } |
| 10198 | |
| Richard Smith | 79a52e5 | 2012-04-17 22:30:01 +0000 | [diff] [blame] | 10199 | Decl *Sema::ActOnStartOfFunctionDef(Scope *FnBodyScope, Declarator &D) { |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 10200 | assert(getCurFunctionDecl() == nullptr && "Function parsing confused"); |
| Abramo Bagnara | 924a8f3 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 10201 | assert(D.isFunctionDeclarator() && "Not a function declarator!"); |
| Douglas Gregor | ad59050 | 2008-12-15 23:53:10 +0000 | [diff] [blame] | 10202 | Scope *ParentScope = FnBodyScope->getParent(); |
| Steve Naroff | 012484d | 2008-01-14 20:51:29 +0000 | [diff] [blame] | 10203 | |
| Douglas Gregor | 5d1b4e3 | 2011-11-07 20:56:01 +0000 | [diff] [blame] | 10204 | D.setFunctionDefinitionKind(FDK_Definition); |
| Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 10205 | Decl *DP = HandleDeclarator(ParentScope, D, MultiTemplateParamsArg()); |
| Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 10206 | return ActOnStartOfFunctionDef(FnBodyScope, DP); |
| Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 10207 | } |
| 10208 | |
| Hans Wennborg | a926d84 | 2014-05-23 20:37:38 +0000 | [diff] [blame] | 10209 | void Sema::ActOnFinishInlineMethodDef(CXXMethodDecl *D) { |
| 10210 | Consumer.HandleInlineMethodDefinition(D); |
| 10211 | } |
| 10212 | |
| Anders Carlsson | 2a45e40 | 2012-12-18 01:29:20 +0000 | [diff] [blame] | 10213 | static bool ShouldWarnAboutMissingPrototype(const FunctionDecl *FD, |
| 10214 | const FunctionDecl*& PossibleZeroParamPrototype) { |
| Anders Carlsson | 31c7e88 | 2009-12-09 03:30:09 +0000 | [diff] [blame] | 10215 | // Don't warn about invalid declarations. |
| 10216 | if (FD->isInvalidDecl()) |
| 10217 | return false; |
| Anders Carlsson | a038825 | 2009-12-09 03:44:46 +0000 | [diff] [blame] | 10218 | |
| Anders Carlsson | 31c7e88 | 2009-12-09 03:30:09 +0000 | [diff] [blame] | 10219 | // Or declarations that aren't global. |
| 10220 | if (!FD->isGlobal()) |
| 10221 | return false; |
| Anders Carlsson | a038825 | 2009-12-09 03:44:46 +0000 | [diff] [blame] | 10222 | |
| Anders Carlsson | 31c7e88 | 2009-12-09 03:30:09 +0000 | [diff] [blame] | 10223 | // Don't warn about C++ member functions. |
| 10224 | if (isa<CXXMethodDecl>(FD)) |
| 10225 | return false; |
| Anders Carlsson | a038825 | 2009-12-09 03:44:46 +0000 | [diff] [blame] | 10226 | |
| Anders Carlsson | 31c7e88 | 2009-12-09 03:30:09 +0000 | [diff] [blame] | 10227 | // Don't warn about 'main'. |
| 10228 | if (FD->isMain()) |
| 10229 | return false; |
| Anders Carlsson | a038825 | 2009-12-09 03:44:46 +0000 | [diff] [blame] | 10230 | |
| Anders Carlsson | 31c7e88 | 2009-12-09 03:30:09 +0000 | [diff] [blame] | 10231 | // Don't warn about inline functions. |
| John McCall | 30cd20a | 2011-03-22 07:16:37 +0000 | [diff] [blame] | 10232 | if (FD->isInlined()) |
| Anders Carlsson | 31c7e88 | 2009-12-09 03:30:09 +0000 | [diff] [blame] | 10233 | return false; |
| Anders Carlsson | a038825 | 2009-12-09 03:44:46 +0000 | [diff] [blame] | 10234 | |
| 10235 | // Don't warn about function templates. |
| 10236 | if (FD->getDescribedFunctionTemplate()) |
| 10237 | return false; |
| 10238 | |
| 10239 | // Don't warn about function template specializations. |
| 10240 | if (FD->isFunctionTemplateSpecialization()) |
| 10241 | return false; |
| 10242 | |
| Tanya Lattner | 4bfc355 | 2012-07-26 00:08:28 +0000 | [diff] [blame] | 10243 | // Don't warn for OpenCL kernels. |
| 10244 | if (FD->hasAttr<OpenCLKernelAttr>()) |
| 10245 | return false; |
| Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 10246 | |
| Benjamin Kramer | 8610cae | 2015-03-12 14:28:47 +0000 | [diff] [blame] | 10247 | // Don't warn on explicitly deleted functions. |
| 10248 | if (FD->isDeleted()) |
| 10249 | return false; |
| 10250 | |
| Anders Carlsson | 31c7e88 | 2009-12-09 03:30:09 +0000 | [diff] [blame] | 10251 | bool MissingPrototype = true; |
| Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 10252 | for (const FunctionDecl *Prev = FD->getPreviousDecl(); |
| 10253 | Prev; Prev = Prev->getPreviousDecl()) { |
| Anders Carlsson | 31c7e88 | 2009-12-09 03:30:09 +0000 | [diff] [blame] | 10254 | // Ignore any declarations that occur in function or method |
| 10255 | // scope, because they aren't visible from the header. |
| Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 10256 | if (Prev->getLexicalDeclContext()->isFunctionOrMethod()) |
| Anders Carlsson | 31c7e88 | 2009-12-09 03:30:09 +0000 | [diff] [blame] | 10257 | continue; |
| Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 10258 | |
| Anders Carlsson | 31c7e88 | 2009-12-09 03:30:09 +0000 | [diff] [blame] | 10259 | MissingPrototype = !Prev->getType()->isFunctionProtoType(); |
| Anders Carlsson | 2a45e40 | 2012-12-18 01:29:20 +0000 | [diff] [blame] | 10260 | if (FD->getNumParams() == 0) |
| 10261 | PossibleZeroParamPrototype = Prev; |
| Anders Carlsson | 31c7e88 | 2009-12-09 03:30:09 +0000 | [diff] [blame] | 10262 | break; |
| 10263 | } |
| Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 10264 | |
| Anders Carlsson | 31c7e88 | 2009-12-09 03:30:09 +0000 | [diff] [blame] | 10265 | return MissingPrototype; |
| 10266 | } |
| 10267 | |
| Rafael Espindola | d53ffa0 | 2013-10-22 21:39:03 +0000 | [diff] [blame] | 10268 | void |
| 10269 | Sema::CheckForFunctionRedefinition(FunctionDecl *FD, |
| 10270 | const FunctionDecl *EffectiveDefinition) { |
| Francois Pichet | dcb3ebe | 2011-04-22 23:20:44 +0000 | [diff] [blame] | 10271 | // Don't complain if we're in GNU89 mode and the previous definition |
| 10272 | // was an extern inline function. |
| Rafael Espindola | d53ffa0 | 2013-10-22 21:39:03 +0000 | [diff] [blame] | 10273 | const FunctionDecl *Definition = EffectiveDefinition; |
| 10274 | if (!Definition) |
| 10275 | if (!FD->isDefined(Definition)) |
| 10276 | return; |
| 10277 | |
| 10278 | if (canRedefineFunction(Definition, getLangOpts())) |
| Rafael Espindola | 69f5310 | 2013-10-22 15:18:22 +0000 | [diff] [blame] | 10279 | return; |
| 10280 | |
| Richard Smith | d6aab59 | 2015-03-27 21:57:41 +0000 | [diff] [blame] | 10281 | // If we don't have a visible definition of the function, and it's inline or |
| 10282 | // a template, it's OK to form another definition of it. |
| Richard Smith | 9a71c99 | 2015-03-27 20:16:58 +0000 | [diff] [blame] | 10283 | // |
| 10284 | // FIXME: Should we skip the body of the function and use the old definition |
| 10285 | // in this case? That may be necessary for functions that return local types |
| 10286 | // through a deduced return type, or instantiate templates with local types. |
| Richard Smith | d6aab59 | 2015-03-27 21:57:41 +0000 | [diff] [blame] | 10287 | if (!hasVisibleDefinition(Definition) && |
| 10288 | (Definition->isInlineSpecified() || |
| 10289 | Definition->getDescribedFunctionTemplate() || |
| 10290 | Definition->getNumTemplateParameterLists())) |
| Richard Smith | 9a71c99 | 2015-03-27 20:16:58 +0000 | [diff] [blame] | 10291 | return; |
| 10292 | |
| Rafael Espindola | 69f5310 | 2013-10-22 15:18:22 +0000 | [diff] [blame] | 10293 | if (getLangOpts().GNUMode && Definition->isInlineSpecified() && |
| 10294 | Definition->getStorageClass() == SC_Extern) |
| 10295 | Diag(FD->getLocation(), diag::err_redefinition_extern_inline) |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10296 | << FD->getDeclName() << getLangOpts().CPlusPlus; |
| Rafael Espindola | 69f5310 | 2013-10-22 15:18:22 +0000 | [diff] [blame] | 10297 | else |
| 10298 | Diag(FD->getLocation(), diag::err_redefinition) << FD->getDeclName(); |
| 10299 | |
| 10300 | Diag(Definition->getLocation(), diag::note_previous_definition); |
| 10301 | FD->setInvalidDecl(); |
| Francois Pichet | dcb3ebe | 2011-04-22 23:20:44 +0000 | [diff] [blame] | 10302 | } |
| Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 10303 | |
| 10304 | |
| Faisal Vali | c1a6dc4 | 2013-10-23 16:10:50 +0000 | [diff] [blame] | 10305 | static void RebuildLambdaScopeInfo(CXXMethodDecl *CallOperator, |
| 10306 | Sema &S) { |
| 10307 | CXXRecordDecl *const LambdaClass = CallOperator->getParent(); |
| Faisal Vali | 524ca28 | 2013-11-12 01:40:44 +0000 | [diff] [blame] | 10308 | |
| 10309 | LambdaScopeInfo *LSI = S.PushLambdaScope(); |
| Faisal Vali | c1a6dc4 | 2013-10-23 16:10:50 +0000 | [diff] [blame] | 10310 | LSI->CallOperator = CallOperator; |
| 10311 | LSI->Lambda = LambdaClass; |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 10312 | LSI->ReturnType = CallOperator->getReturnType(); |
| Faisal Vali | c1a6dc4 | 2013-10-23 16:10:50 +0000 | [diff] [blame] | 10313 | const LambdaCaptureDefault LCD = LambdaClass->getLambdaCaptureDefault(); |
| 10314 | |
| 10315 | if (LCD == LCD_None) |
| 10316 | LSI->ImpCaptureStyle = CapturingScopeInfo::ImpCap_None; |
| 10317 | else if (LCD == LCD_ByCopy) |
| 10318 | LSI->ImpCaptureStyle = CapturingScopeInfo::ImpCap_LambdaByval; |
| 10319 | else if (LCD == LCD_ByRef) |
| 10320 | LSI->ImpCaptureStyle = CapturingScopeInfo::ImpCap_LambdaByref; |
| 10321 | DeclarationNameInfo DNI = CallOperator->getNameInfo(); |
| 10322 | |
| 10323 | LSI->IntroducerRange = DNI.getCXXOperatorNameRange(); |
| 10324 | LSI->Mutable = !CallOperator->isConst(); |
| 10325 | |
| Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 10326 | // Add the captures to the LSI so they can be noted as already |
| 10327 | // captured within tryCaptureVar. |
| Alexey Bataev | 39c81e2 | 2014-08-28 04:28:19 +0000 | [diff] [blame] | 10328 | auto I = LambdaClass->field_begin(); |
| Aaron Ballman | 6def98a | 2014-03-13 17:08:33 +0000 | [diff] [blame] | 10329 | for (const auto &C : LambdaClass->captures()) { |
| 10330 | if (C.capturesVariable()) { |
| 10331 | VarDecl *VD = C.getCapturedVar(); |
| Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 10332 | if (VD->isInitCapture()) |
| 10333 | S.CurrentInstantiationScope->InstantiatedLocal(VD, VD); |
| 10334 | QualType CaptureType = VD->getType(); |
| Aaron Ballman | 6def98a | 2014-03-13 17:08:33 +0000 | [diff] [blame] | 10335 | const bool ByRef = C.getCaptureKind() == LCK_ByRef; |
| Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 10336 | LSI->addCapture(VD, /*IsBlock*/false, ByRef, |
| Alexey Bataev | 19acc3d | 2015-01-12 10:17:46 +0000 | [diff] [blame] | 10337 | /*RefersToEnclosingVariableOrCapture*/true, C.getLocation(), |
| Aaron Ballman | 6def98a | 2014-03-13 17:08:33 +0000 | [diff] [blame] | 10338 | /*EllipsisLoc*/C.isPackExpansion() |
| 10339 | ? C.getEllipsisLoc() : SourceLocation(), |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 10340 | CaptureType, /*Expr*/ nullptr); |
| 10341 | |
| Aaron Ballman | 6def98a | 2014-03-13 17:08:33 +0000 | [diff] [blame] | 10342 | } else if (C.capturesThis()) { |
| 10343 | LSI->addThisCapture(/*Nested*/ false, C.getLocation(), |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 10344 | S.getCurrentThisType(), /*Expr*/ nullptr); |
| Alexey Bataev | 39c81e2 | 2014-08-28 04:28:19 +0000 | [diff] [blame] | 10345 | } else { |
| 10346 | LSI->addVLATypeCapture(C.getLocation(), I->getType()); |
| Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 10347 | } |
| Alexey Bataev | 39c81e2 | 2014-08-28 04:28:19 +0000 | [diff] [blame] | 10348 | ++I; |
| Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 10349 | } |
| Faisal Vali | c1a6dc4 | 2013-10-23 16:10:50 +0000 | [diff] [blame] | 10350 | } |
| Francois Pichet | dcb3ebe | 2011-04-22 23:20:44 +0000 | [diff] [blame] | 10351 | |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10352 | Decl *Sema::ActOnStartOfFunctionDef(Scope *FnBodyScope, Decl *D) { |
| Anders Carlsson | 561f793 | 2009-10-29 15:46:07 +0000 | [diff] [blame] | 10353 | // Clear the last template instantiation error context. |
| 10354 | LastTemplateInstantiationErrorContext = ActiveTemplateInstantiation(); |
| 10355 | |
| Douglas Gregor | 17a7c12 | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 10356 | if (!D) |
| 10357 | return D; |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 10358 | FunctionDecl *FD = nullptr; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10359 | |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10360 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D)) |
| Douglas Gregor | c45a40a | 2009-08-22 00:34:47 +0000 | [diff] [blame] | 10361 | FD = FunTmpl->getTemplatedDecl(); |
| 10362 | else |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10363 | FD = cast<FunctionDecl>(D); |
| Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 10364 | // If we are instantiating a generic lambda call operator, push |
| 10365 | // a LambdaScopeInfo onto the function stack. But use the information |
| Faisal Vali | c1a6dc4 | 2013-10-23 16:10:50 +0000 | [diff] [blame] | 10366 | // that's already been calculated (ActOnLambdaExpr) to prime the current |
| 10367 | // LambdaScopeInfo. |
| 10368 | // When the template operator is being specialized, the LambdaScopeInfo, |
| 10369 | // has to be properly restored so that tryCaptureVariable doesn't try |
| 10370 | // and capture any new variables. In addition when calculating potential |
| 10371 | // captures during transformation of nested lambdas, it is necessary to |
| 10372 | // have the LSI properly restored. |
| Faisal Vali | f9a9af3 | 2013-09-29 20:15:45 +0000 | [diff] [blame] | 10373 | if (isGenericLambdaCallOperatorSpecialization(FD)) { |
| Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 10374 | assert(ActiveTemplateInstantiations.size() && |
| 10375 | "There should be an active template instantiation on the stack " |
| 10376 | "when instantiating a generic lambda!"); |
| Faisal Vali | c1a6dc4 | 2013-10-23 16:10:50 +0000 | [diff] [blame] | 10377 | RebuildLambdaScopeInfo(cast<CXXMethodDecl>(D), *this); |
| Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 10378 | } |
| 10379 | else |
| 10380 | // Enter a new function scope |
| 10381 | PushFunctionScope(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10382 | |
| Douglas Gregor | cad304ba | 2008-10-29 15:10:40 +0000 | [diff] [blame] | 10383 | // See if this is a redefinition. |
| Francois Pichet | dcb3ebe | 2011-04-22 23:20:44 +0000 | [diff] [blame] | 10384 | if (!FD->isLateTemplateParsed()) |
| 10385 | CheckForFunctionRedefinition(FD); |
| Douglas Gregor | cad304ba | 2008-10-29 15:10:40 +0000 | [diff] [blame] | 10386 | |
| Douglas Gregor | 75a45ba | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 10387 | // Builtin functions cannot be defined. |
| Douglas Gregor | 15fc956 | 2009-09-12 00:22:50 +0000 | [diff] [blame] | 10388 | if (unsigned BuiltinID = FD->getBuiltinID()) { |
| Rafael Espindola | 3b3a166 | 2013-06-13 18:34:17 +0000 | [diff] [blame] | 10389 | if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID) && |
| 10390 | !Context.BuiltinInfo.isPredefinedRuntimeFunction(BuiltinID)) { |
| Douglas Gregor | 75a45ba | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 10391 | Diag(FD->getLocation(), diag::err_builtin_definition) << FD; |
| Douglas Gregor | 7a0febe | 2009-02-17 16:03:01 +0000 | [diff] [blame] | 10392 | FD->setInvalidDecl(); |
| 10393 | } |
| Douglas Gregor | 75a45ba | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 10394 | } |
| 10395 | |
| Eli Friedman | 9ad7244 | 2009-03-04 07:30:59 +0000 | [diff] [blame] | 10396 | // The return type of a function definition must be complete |
| Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 10397 | // (C99 6.9.1p3, C++ [dcl.fct]p6). |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 10398 | QualType ResultType = FD->getReturnType(); |
| Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 10399 | if (!ResultType->isDependentType() && !ResultType->isVoidType() && |
| Chris Lattner | 0f94c5a | 2009-04-29 05:12:23 +0000 | [diff] [blame] | 10400 | !FD->isInvalidDecl() && |
| Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 10401 | RequireCompleteType(FD->getLocation(), ResultType, |
| 10402 | diag::err_func_def_incomplete_result)) |
| Eli Friedman | 9ad7244 | 2009-03-04 07:30:59 +0000 | [diff] [blame] | 10403 | FD->setInvalidDecl(); |
| Eli Friedman | 9ad7244 | 2009-03-04 07:30:59 +0000 | [diff] [blame] | 10404 | |
| Douglas Gregor | 67da0d9 | 2009-05-15 17:59:04 +0000 | [diff] [blame] | 10405 | if (FnBodyScope) |
| 10406 | PushDeclContext(FnBodyScope, FD); |
| Anton Korobeynikov | d72f47a | 2008-12-26 00:52:02 +0000 | [diff] [blame] | 10407 | |
| Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 10408 | // Check the validity of our function parameters |
| Douglas Gregor | b524d90 | 2010-11-01 18:37:59 +0000 | [diff] [blame] | 10409 | CheckParmsForFunctionDef(FD->param_begin(), FD->param_end(), |
| 10410 | /*CheckParameterNames=*/true); |
| Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 10411 | |
| 10412 | // Introduce our parameters into the function scope |
| Aaron Ballman | f6bf62e | 2014-03-07 15:12:56 +0000 | [diff] [blame] | 10413 | for (auto Param : FD->params()) { |
| Douglas Gregor | c72e645 | 2009-01-09 18:51:29 +0000 | [diff] [blame] | 10414 | Param->setOwningFunction(FD); |
| 10415 | |
| Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 10416 | // If this has an identifier, add it to the scope stack. |
| John McCall | df8b37c | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 10417 | if (Param->getIdentifier() && FnBodyScope) { |
| Argyrios Kyrtzidis | 1cb0de1 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 10418 | CheckShadow(FnBodyScope, Param); |
| John McCall | df8b37c | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 10419 | |
| Argyrios Kyrtzidis | b8a4920 | 2008-04-12 00:47:19 +0000 | [diff] [blame] | 10420 | PushOnScopeChains(Param, FnBodyScope); |
| John McCall | df8b37c | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 10421 | } |
| Chris Lattner | f61c8a8 | 2007-01-21 19:04:43 +0000 | [diff] [blame] | 10422 | } |
| Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 10423 | |
| James Molloy | 6f8780b | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 10424 | // If we had any tags defined in the function prototype, |
| 10425 | // introduce them into the function scope. |
| 10426 | if (FnBodyScope) { |
| Robert Wilhelm | 16e94b9 | 2013-08-09 18:02:13 +0000 | [diff] [blame] | 10427 | for (ArrayRef<NamedDecl *>::iterator |
| 10428 | I = FD->getDeclsInPrototypeScope().begin(), |
| 10429 | E = FD->getDeclsInPrototypeScope().end(); |
| 10430 | I != E; ++I) { |
| James Molloy | 6f8780b | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 10431 | NamedDecl *D = *I; |
| 10432 | |
| Nico Weber | 620660c | 2015-03-09 02:33:32 +0000 | [diff] [blame] | 10433 | // Some of these decls (like enums) may have been pinned to the |
| 10434 | // translation unit for lack of a real context earlier. If so, remove |
| 10435 | // from the translation unit and reattach to the current context. |
| James Molloy | 6f8780b | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 10436 | if (D->getLexicalDeclContext() == Context.getTranslationUnitDecl()) { |
| 10437 | // Is the decl actually in the context? |
| Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 10438 | for (const auto *DI : Context.getTranslationUnitDecl()->decls()) { |
| 10439 | if (DI == D) { |
| James Molloy | 6f8780b | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 10440 | Context.getTranslationUnitDecl()->removeDecl(D); |
| 10441 | break; |
| 10442 | } |
| 10443 | } |
| 10444 | // Either way, reassign the lexical decl context to our FunctionDecl. |
| 10445 | D->setLexicalDeclContext(CurContext); |
| 10446 | } |
| 10447 | |
| 10448 | // If the decl has a non-null name, make accessible in the current scope. |
| 10449 | if (!D->getName().empty()) |
| 10450 | PushOnScopeChains(D, FnBodyScope, /*AddToContext=*/false); |
| 10451 | |
| 10452 | // Similarly, dive into enums and fish their constants out, making them |
| 10453 | // accessible in this scope. |
| Aaron Ballman | 23a6dcb | 2014-03-08 18:45:14 +0000 | [diff] [blame] | 10454 | if (auto *ED = dyn_cast<EnumDecl>(D)) { |
| 10455 | for (auto *EI : ED->enumerators()) |
| 10456 | PushOnScopeChains(EI, FnBodyScope, /*AddToContext=*/false); |
| James Molloy | 6f8780b | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 10457 | } |
| 10458 | } |
| 10459 | } |
| 10460 | |
| Richard Smith | 79a52e5 | 2012-04-17 22:30:01 +0000 | [diff] [blame] | 10461 | // Ensure that the function's exception specification is instantiated. |
| 10462 | if (const FunctionProtoType *FPT = FD->getType()->getAs<FunctionProtoType>()) |
| 10463 | ResolveExceptionSpec(D->getLocation(), FPT); |
| 10464 | |
| Hans Wennborg | b0f2f14 | 2014-05-15 22:07:49 +0000 | [diff] [blame] | 10465 | // dllimport cannot be applied to non-inline function definitions. |
| Hans Wennborg | 7f26fa6 | 2014-05-19 20:14:13 +0000 | [diff] [blame] | 10466 | if (FD->hasAttr<DLLImportAttr>() && !FD->isInlined() && |
| 10467 | !FD->isTemplateInstantiation()) { |
| Hans Wennborg | b0f2f14 | 2014-05-15 22:07:49 +0000 | [diff] [blame] | 10468 | assert(!FD->hasAttr<DLLExportAttr>()); |
| 10469 | Diag(FD->getLocation(), diag::err_attribute_dllimport_function_definition); |
| 10470 | FD->setInvalidDecl(); |
| 10471 | return D; |
| Anton Korobeynikov | d72f47a | 2008-12-26 00:52:02 +0000 | [diff] [blame] | 10472 | } |
| Dmitri Gribenko | b261088 | 2012-08-14 17:17:18 +0000 | [diff] [blame] | 10473 | // We want to attach documentation to original Decl (which might be |
| 10474 | // a function template). |
| 10475 | ActOnDocumentableDecl(D); |
| Fariborz Jahanian | 3451df8 | 2014-05-28 17:02:35 +0000 | [diff] [blame] | 10476 | if (getCurLexicalContext()->isObjCContainer() && |
| 10477 | getCurLexicalContext()->getDeclKind() != Decl::ObjCCategoryImpl && |
| 10478 | getCurLexicalContext()->getDeclKind() != Decl::ObjCImplementation) |
| 10479 | Diag(FD->getLocation(), diag::warn_function_def_in_objc_container); |
| 10480 | |
| Argyrios Kyrtzidis | 26444c5 | 2012-12-14 06:54:03 +0000 | [diff] [blame] | 10481 | return D; |
| Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 10482 | } |
| 10483 | |
| Douglas Gregor | 6fd1b18 | 2010-05-15 06:01:05 +0000 | [diff] [blame] | 10484 | /// \brief Given the set of return statements within a function body, |
| 10485 | /// compute the variables that are subject to the named return value |
| 10486 | /// optimization. |
| 10487 | /// |
| 10488 | /// Each of the variables that is subject to the named return value |
| 10489 | /// optimization will be marked as NRVO variables in the AST, and any |
| 10490 | /// return statement that has a marked NRVO variable as its NRVO candidate can |
| 10491 | /// use the named return value optimization. |
| 10492 | /// |
| 10493 | /// This function applies a very simplistic algorithm for NRVO: if every return |
| Nick Lewycky | d78f92f | 2014-05-03 00:41:18 +0000 | [diff] [blame] | 10494 | /// statement in the scope of a variable has the same NRVO candidate, that |
| 10495 | /// candidate is an NRVO variable. |
| Douglas Gregor | 49695f0 | 2011-09-06 20:46:03 +0000 | [diff] [blame] | 10496 | void Sema::computeNRVO(Stmt *Body, FunctionScopeInfo *Scope) { |
| John McCall | aab3e41 | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 10497 | ReturnStmt **Returns = Scope->Returns.data(); |
| 10498 | |
| John McCall | aab3e41 | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 10499 | for (unsigned I = 0, E = Scope->Returns.size(); I != E; ++I) { |
| Nick Lewycky | d78f92f | 2014-05-03 00:41:18 +0000 | [diff] [blame] | 10500 | if (const VarDecl *NRVOCandidate = Returns[I]->getNRVOCandidate()) { |
| 10501 | if (!NRVOCandidate->isNRVOVariable()) |
| 10502 | Returns[I]->setNRVOCandidate(nullptr); |
| 10503 | } |
| Douglas Gregor | 6fd1b18 | 2010-05-15 06:01:05 +0000 | [diff] [blame] | 10504 | } |
| Douglas Gregor | 6fd1b18 | 2010-05-15 06:01:05 +0000 | [diff] [blame] | 10505 | } |
| 10506 | |
| Richard Smith | 8e6002f | 2014-03-12 23:14:33 +0000 | [diff] [blame] | 10507 | bool Sema::canDelayFunctionBody(const Declarator &D) { |
| 10508 | // We can't delay parsing the body of a constexpr function template (yet). |
| 10509 | if (D.getDeclSpec().isConstexprSpecified()) |
| 10510 | return false; |
| 10511 | |
| 10512 | // We can't delay parsing the body of a function template with a deduced |
| 10513 | // return type (yet). |
| 10514 | if (D.getDeclSpec().containsPlaceholderType()) { |
| 10515 | // If the placeholder introduces a non-deduced trailing return type, |
| 10516 | // we can still delay parsing it. |
| 10517 | if (D.getNumTypeObjects()) { |
| 10518 | const auto &Outer = D.getTypeObject(D.getNumTypeObjects() - 1); |
| 10519 | if (Outer.Kind == DeclaratorChunk::Function && |
| 10520 | Outer.Fun.hasTrailingReturnType()) { |
| 10521 | QualType Ty = GetTypeFromParser(Outer.Fun.getTrailingReturnType()); |
| 10522 | return Ty.isNull() || !Ty->isUndeducedType(); |
| 10523 | } |
| 10524 | } |
| 10525 | return false; |
| 10526 | } |
| 10527 | |
| 10528 | return true; |
| 10529 | } |
| 10530 | |
| Richard Smith | 1ab34b3 | 2012-11-19 21:13:18 +0000 | [diff] [blame] | 10531 | bool Sema::canSkipFunctionBody(Decl *D) { |
| Richard Smith | 1ab34b3 | 2012-11-19 21:13:18 +0000 | [diff] [blame] | 10532 | // We cannot skip the body of a function (or function template) which is |
| 10533 | // constexpr, since we may need to evaluate its body in order to parse the |
| 10534 | // rest of the file. |
| Richard Smith | 7500ab2 | 2013-05-10 04:31:10 +0000 | [diff] [blame] | 10535 | // We cannot skip the body of a function with an undeduced return type, |
| 10536 | // because any callers of that function need to know the type. |
| Alp Toker | a2794f9 | 2014-01-22 07:29:52 +0000 | [diff] [blame] | 10537 | if (const FunctionDecl *FD = D->getAsFunction()) |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 10538 | if (FD->isConstexpr() || FD->getReturnType()->isUndeducedType()) |
| Alp Toker | a2794f9 | 2014-01-22 07:29:52 +0000 | [diff] [blame] | 10539 | return false; |
| 10540 | return Consumer.shouldSkipFunctionBody(D); |
| Richard Smith | 1ab34b3 | 2012-11-19 21:13:18 +0000 | [diff] [blame] | 10541 | } |
| 10542 | |
| Argyrios Kyrtzidis | 1eb71a1 | 2012-12-06 18:59:10 +0000 | [diff] [blame] | 10543 | Decl *Sema::ActOnSkippedFunctionBody(Decl *Decl) { |
| Argyrios Kyrtzidis | 4431918 | 2013-02-22 04:11:06 +0000 | [diff] [blame] | 10544 | if (FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(Decl)) |
| Argyrios Kyrtzidis | 1eb71a1 | 2012-12-06 18:59:10 +0000 | [diff] [blame] | 10545 | FD->setHasSkippedBody(); |
| Argyrios Kyrtzidis | 4431918 | 2013-02-22 04:11:06 +0000 | [diff] [blame] | 10546 | else if (ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(Decl)) |
| Argyrios Kyrtzidis | 1eb71a1 | 2012-12-06 18:59:10 +0000 | [diff] [blame] | 10547 | MD->setHasSkippedBody(); |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 10548 | return ActOnFinishFunctionBody(Decl, nullptr); |
| Argyrios Kyrtzidis | 1eb71a1 | 2012-12-06 18:59:10 +0000 | [diff] [blame] | 10549 | } |
| 10550 | |
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 10551 | Decl *Sema::ActOnFinishFunctionBody(Decl *D, Stmt *BodyArg) { |
| Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 10552 | return ActOnFinishFunctionBody(D, BodyArg, false); |
| Douglas Gregor | 67da0d9 | 2009-05-15 17:59:04 +0000 | [diff] [blame] | 10553 | } |
| 10554 | |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10555 | Decl *Sema::ActOnFinishFunctionBody(Decl *dcl, Stmt *Body, |
| 10556 | bool IsInstantiation) { |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 10557 | FunctionDecl *FD = dcl ? dcl->getAsFunction() : nullptr; |
| Douglas Gregor | c45a40a | 2009-08-22 00:34:47 +0000 | [diff] [blame] | 10558 | |
| Ted Kremenek | 0b40532 | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 10559 | sema::AnalysisBasedWarnings::Policy WP = AnalysisWarnings.getDefaultPolicy(); |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 10560 | sema::AnalysisBasedWarnings::Policy *ActivePolicy = nullptr; |
| Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 10561 | |
| Douglas Gregor | c45a40a | 2009-08-22 00:34:47 +0000 | [diff] [blame] | 10562 | if (FD) { |
| Chris Lattner | 960cc52 | 2009-04-18 09:36:27 +0000 | [diff] [blame] | 10563 | FD->setBody(Body); |
| John McCall | 5ed3caf | 2012-02-14 19:50:52 +0000 | [diff] [blame] | 10564 | |
| Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 10565 | if (getLangOpts().CPlusPlus14 && !FD->isInvalidDecl() && Body && |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 10566 | !FD->isDependentContext() && FD->getReturnType()->isUndeducedType()) { |
| Richard Smith | 7500ab2 | 2013-05-10 04:31:10 +0000 | [diff] [blame] | 10567 | // If the function has a deduced result type but contains no 'return' |
| 10568 | // statements, the result type as written must be exactly 'auto', and |
| 10569 | // the deduced result type is 'void'. |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 10570 | if (!FD->getReturnType()->getAs<AutoType>()) { |
| Richard Smith | 7500ab2 | 2013-05-10 04:31:10 +0000 | [diff] [blame] | 10571 | Diag(dcl->getLocation(), diag::err_auto_fn_no_return_but_not_auto) |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 10572 | << FD->getReturnType(); |
| Richard Smith | 7500ab2 | 2013-05-10 04:31:10 +0000 | [diff] [blame] | 10573 | FD->setInvalidDecl(); |
| 10574 | } else { |
| 10575 | // Substitute 'void' for the 'auto' in the type. |
| Saleem Abdulrasool | 374b5aa | 2014-10-16 22:42:53 +0000 | [diff] [blame] | 10576 | TypeLoc ResultType = getReturnTypeLoc(FD); |
| Richard Smith | 7500ab2 | 2013-05-10 04:31:10 +0000 | [diff] [blame] | 10577 | Context.adjustDeducedFunctionResultType( |
| 10578 | FD, SubstAutoType(ResultType.getType(), Context.VoidTy)); |
| Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 10579 | } |
| 10580 | } |
| 10581 | |
| Nick Lewycky | 9c7eb1d | 2013-02-01 08:13:20 +0000 | [diff] [blame] | 10582 | // The only way to be included in UndefinedButUsed is if there is an |
| 10583 | // ODR use before the definition. Avoid the expensive map lookup if this |
| Nick Lewycky | f0f5616 | 2013-01-31 03:23:57 +0000 | [diff] [blame] | 10584 | // is the first declaration. |
| Rafael Espindola | 3f9e444 | 2013-10-19 02:13:21 +0000 | [diff] [blame] | 10585 | if (!FD->isFirstDecl() && FD->getPreviousDecl()->isUsed()) { |
| Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 10586 | if (!FD->isExternallyVisible()) |
| Nick Lewycky | 9c7eb1d | 2013-02-01 08:13:20 +0000 | [diff] [blame] | 10587 | UndefinedButUsed.erase(FD); |
| 10588 | else if (FD->isInlined() && |
| 10589 | (LangOpts.CPlusPlus || !LangOpts.GNUInline) && |
| 10590 | (!FD->getPreviousDecl()->hasAttr<GNUInlineAttr>())) |
| 10591 | UndefinedButUsed.erase(FD); |
| 10592 | } |
| Nick Lewycky | f0f5616 | 2013-01-31 03:23:57 +0000 | [diff] [blame] | 10593 | |
| John McCall | 5ed3caf | 2012-02-14 19:50:52 +0000 | [diff] [blame] | 10594 | // If the function implicitly returns zero (like 'main') or is naked, |
| 10595 | // don't complain about missing return statements. |
| 10596 | if (FD->hasImplicitReturnZero() || FD->hasAttr<NakedAttr>()) |
| Ted Kremenek | 0b40532 | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 10597 | WP.disableCheckFallThrough(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10598 | |
| Francois Pichet | 3abc9b8 | 2011-05-11 02:14:46 +0000 | [diff] [blame] | 10599 | // MSVC permits the use of pure specifier (=0) on function definition, |
| Alp Toker | d473363 | 2013-12-05 04:47:09 +0000 | [diff] [blame] | 10600 | // defined at class scope, warn about this non-standard construct. |
| Reid Kleckner | be7a446 | 2013-10-08 22:45:29 +0000 | [diff] [blame] | 10601 | if (getLangOpts().MicrosoftExt && FD->isPure() && FD->isCanonicalDecl()) |
| Richard Smith | 1b98ccc | 2014-07-19 01:39:17 +0000 | [diff] [blame] | 10602 | Diag(FD->getLocation(), diag::ext_pure_function_definition); |
| Francois Pichet | 3abc9b8 | 2011-05-11 02:14:46 +0000 | [diff] [blame] | 10603 | |
| Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 10604 | if (!FD->isInvalidDecl()) { |
| Reid Kleckner | 121b1a1 | 2014-04-30 16:31:28 +0000 | [diff] [blame] | 10605 | // Don't diagnose unused parameters of defaulted or deleted functions. |
| Benjamin Kramer | 8610cae | 2015-03-12 14:28:47 +0000 | [diff] [blame] | 10606 | if (!FD->isDeleted() && !FD->isDefaulted()) |
| Reid Kleckner | 121b1a1 | 2014-04-30 16:31:28 +0000 | [diff] [blame] | 10607 | DiagnoseUnusedParameters(FD->param_begin(), FD->param_end()); |
| Argyrios Kyrtzidis | af84ec0 | 2010-11-17 23:11:54 +0000 | [diff] [blame] | 10608 | DiagnoseSizeOfParametersAndReturnValue(FD->param_begin(), FD->param_end(), |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 10609 | FD->getReturnType(), FD); |
| 10610 | |
| Nico Weber | 8b51ae9 | 2015-01-13 03:52:11 +0000 | [diff] [blame] | 10611 | // If this is a structor, we need a vtable. |
| Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 10612 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(FD)) |
| 10613 | MarkVTableUsed(FD->getLocation(), Constructor->getParent()); |
| Nico Weber | 8b51ae9 | 2015-01-13 03:52:11 +0000 | [diff] [blame] | 10614 | else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(FD)) |
| 10615 | MarkVTableUsed(FD->getLocation(), Destructor->getParent()); |
| Douglas Gregor | 6fd1b18 | 2010-05-15 06:01:05 +0000 | [diff] [blame] | 10616 | |
| Jordan Rose | d39e5f1 | 2012-07-02 21:19:23 +0000 | [diff] [blame] | 10617 | // Try to apply the named return value optimization. We have to check |
| 10618 | // if we can do this here because lambdas keep return statements around |
| 10619 | // to deduce an implicit return type. |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 10620 | if (getLangOpts().CPlusPlus && FD->getReturnType()->isRecordType() && |
| Jordan Rose | d39e5f1 | 2012-07-02 21:19:23 +0000 | [diff] [blame] | 10621 | !FD->isDependentContext()) |
| 10622 | computeNRVO(Body, getCurFunction()); |
| Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 10623 | } |
| Richard Smith | 91c18de | 2015-02-28 01:01:56 +0000 | [diff] [blame] | 10624 | |
| Benjamin Kramer | 8610cae | 2015-03-12 14:28:47 +0000 | [diff] [blame] | 10625 | // GNU warning -Wmissing-prototypes: |
| 10626 | // Warn if a global function is defined without a previous |
| 10627 | // prototype declaration. This warning is issued even if the |
| 10628 | // definition itself provides a prototype. The aim is to detect |
| 10629 | // global functions that fail to be declared in header files. |
| 10630 | const FunctionDecl *PossibleZeroParamPrototype = nullptr; |
| 10631 | if (ShouldWarnAboutMissingPrototype(FD, PossibleZeroParamPrototype)) { |
| 10632 | Diag(FD->getLocation(), diag::warn_missing_prototype) << FD; |
| 10633 | |
| 10634 | if (PossibleZeroParamPrototype) { |
| 10635 | // We found a declaration that is not a prototype, |
| 10636 | // but that could be a zero-parameter prototype |
| 10637 | if (TypeSourceInfo *TI = |
| 10638 | PossibleZeroParamPrototype->getTypeSourceInfo()) { |
| 10639 | TypeLoc TL = TI->getTypeLoc(); |
| 10640 | if (FunctionNoProtoTypeLoc FTL = TL.getAs<FunctionNoProtoTypeLoc>()) |
| 10641 | Diag(PossibleZeroParamPrototype->getLocation(), |
| 10642 | diag::note_declaration_not_a_prototype) |
| 10643 | << PossibleZeroParamPrototype |
| 10644 | << FixItHint::CreateInsertion(FTL.getRParenLoc(), "void"); |
| 10645 | } |
| 10646 | } |
| 10647 | } |
| 10648 | |
| Richard Smith | 91c18de | 2015-02-28 01:01:56 +0000 | [diff] [blame] | 10649 | if (auto *MD = dyn_cast<CXXMethodDecl>(FD)) { |
| 10650 | const CXXMethodDecl *KeyFunction; |
| 10651 | if (MD->isOutOfLine() && (MD = MD->getCanonicalDecl()) && |
| 10652 | MD->isVirtual() && |
| 10653 | (KeyFunction = Context.getCurrentKeyFunction(MD->getParent())) && |
| 10654 | MD == KeyFunction->getCanonicalDecl()) { |
| 10655 | // Update the key-function state if necessary for this ABI. |
| 10656 | if (FD->isInlined() && |
| 10657 | !Context.getTargetInfo().getCXXABI().canKeyFunctionBeInline()) { |
| 10658 | Context.setNonKeyFunction(MD); |
| 10659 | |
| 10660 | // If the newly-chosen key function is already defined, then we |
| 10661 | // need to mark the vtable as used retroactively. |
| 10662 | KeyFunction = Context.getCurrentKeyFunction(MD->getParent()); |
| 10663 | const FunctionDecl *Definition; |
| 10664 | if (KeyFunction && KeyFunction->isDefined(Definition)) |
| 10665 | MarkVTableUsed(Definition->getLocation(), MD->getParent(), true); |
| 10666 | } else { |
| 10667 | // We just defined they key function; mark the vtable as used. |
| 10668 | MarkVTableUsed(FD->getLocation(), MD->getParent(), true); |
| 10669 | } |
| 10670 | } |
| 10671 | } |
| 10672 | |
| Douglas Gregor | 21f4692 | 2012-02-08 20:17:14 +0000 | [diff] [blame] | 10673 | assert((FD == getCurFunctionDecl() || getCurLambda()->CallOperator == FD) && |
| 10674 | "Function parsing confused"); |
| Steve Naroff | 542cd5d | 2008-07-25 17:57:26 +0000 | [diff] [blame] | 10675 | } else if (ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(dcl)) { |
| Chris Lattner | 1598a3a | 2009-02-16 19:27:54 +0000 | [diff] [blame] | 10676 | assert(MD == getCurMethodDecl() && "Method parsing confused"); |
| Chris Lattner | 960cc52 | 2009-04-18 09:36:27 +0000 | [diff] [blame] | 10677 | MD->setBody(Body); |
| Argyrios Kyrtzidis | af84ec0 | 2010-11-17 23:11:54 +0000 | [diff] [blame] | 10678 | if (!MD->isInvalidDecl()) { |
| Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 10679 | DiagnoseUnusedParameters(MD->param_begin(), MD->param_end()); |
| Argyrios Kyrtzidis | af84ec0 | 2010-11-17 23:11:54 +0000 | [diff] [blame] | 10680 | DiagnoseSizeOfParametersAndReturnValue(MD->param_begin(), MD->param_end(), |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 10681 | MD->getReturnType(), MD); |
| 10682 | |
| Douglas Gregor | e3f3ea0 | 2011-09-06 20:33:37 +0000 | [diff] [blame] | 10683 | if (Body) |
| Douglas Gregor | 49695f0 | 2011-09-06 20:46:03 +0000 | [diff] [blame] | 10684 | computeNRVO(Body, getCurFunction()); |
| Argyrios Kyrtzidis | af84ec0 | 2010-11-17 23:11:54 +0000 | [diff] [blame] | 10685 | } |
| Jordan Rose | 2afd661 | 2012-10-19 16:05:26 +0000 | [diff] [blame] | 10686 | if (getCurFunction()->ObjCShouldCallSuper) { |
| Fariborz Jahanian | b05417e | 2012-09-10 16:51:09 +0000 | [diff] [blame] | 10687 | Diag(MD->getLocEnd(), diag::warn_objc_missing_super_call) |
| 10688 | << MD->getSelector().getAsString(); |
| Jordan Rose | 2afd661 | 2012-10-19 16:05:26 +0000 | [diff] [blame] | 10689 | getCurFunction()->ObjCShouldCallSuper = false; |
| Nico Weber | 1fb8266 | 2011-08-28 22:35:17 +0000 | [diff] [blame] | 10690 | } |
| Argyrios Kyrtzidis | 22bfa2c | 2013-12-03 21:11:36 +0000 | [diff] [blame] | 10691 | if (getCurFunction()->ObjCWarnForNoDesignatedInitChain) { |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 10692 | const ObjCMethodDecl *InitMethod = nullptr; |
| Argyrios Kyrtzidis | fcded9b | 2013-12-03 21:11:43 +0000 | [diff] [blame] | 10693 | bool isDesignated = |
| 10694 | MD->isDesignatedInitializerForTheInterface(&InitMethod); |
| Argyrios Kyrtzidis | 22bfa2c | 2013-12-03 21:11:36 +0000 | [diff] [blame] | 10695 | assert(isDesignated && InitMethod); |
| 10696 | (void)isDesignated; |
| Argyrios Kyrtzidis | de10366 | 2014-04-16 18:32:51 +0000 | [diff] [blame] | 10697 | |
| 10698 | auto superIsNSObject = [&](const ObjCMethodDecl *MD) { |
| 10699 | auto IFace = MD->getClassInterface(); |
| 10700 | if (!IFace) |
| 10701 | return false; |
| 10702 | auto SuperD = IFace->getSuperClass(); |
| 10703 | if (!SuperD) |
| 10704 | return false; |
| 10705 | return SuperD->getIdentifier() == |
| 10706 | NSAPIObj->getNSClassId(NSAPI::ClassId_NSObject); |
| 10707 | }; |
| 10708 | // Don't issue this warning for unavailable inits or direct subclasses |
| 10709 | // of NSObject. |
| 10710 | if (!MD->isUnavailable() && !superIsNSObject(MD)) { |
| Fariborz Jahanian | e3b5c99 | 2014-03-14 23:30:18 +0000 | [diff] [blame] | 10711 | Diag(MD->getLocation(), |
| 10712 | diag::warn_objc_designated_init_missing_super_call); |
| 10713 | Diag(InitMethod->getLocation(), |
| 10714 | diag::note_objc_designated_init_marked_here); |
| 10715 | } |
| Argyrios Kyrtzidis | 22bfa2c | 2013-12-03 21:11:36 +0000 | [diff] [blame] | 10716 | getCurFunction()->ObjCWarnForNoDesignatedInitChain = false; |
| 10717 | } |
| Argyrios Kyrtzidis | b66d3cf | 2013-12-03 21:11:49 +0000 | [diff] [blame] | 10718 | if (getCurFunction()->ObjCWarnForNoInitDelegation) { |
| Fariborz Jahanian | e3b5c99 | 2014-03-14 23:30:18 +0000 | [diff] [blame] | 10719 | // Don't issue this warning for unavaialable inits. |
| 10720 | if (!MD->isUnavailable()) |
| Nico Weber | 3b00fdc | 2015-03-07 19:52:39 +0000 | [diff] [blame] | 10721 | Diag(MD->getLocation(), |
| 10722 | diag::warn_objc_secondary_init_missing_init_call); |
| Argyrios Kyrtzidis | b66d3cf | 2013-12-03 21:11:49 +0000 | [diff] [blame] | 10723 | getCurFunction()->ObjCWarnForNoInitDelegation = false; |
| 10724 | } |
| Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 10725 | } else { |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 10726 | return nullptr; |
| Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 10727 | } |
| Douglas Gregor | 67da0d9 | 2009-05-15 17:59:04 +0000 | [diff] [blame] | 10728 | |
| Jordan Rose | 2afd661 | 2012-10-19 16:05:26 +0000 | [diff] [blame] | 10729 | assert(!getCurFunction()->ObjCShouldCallSuper && |
| Eli Friedman | 22be06a | 2012-08-01 21:02:59 +0000 | [diff] [blame] | 10730 | "This should only be set for ObjC methods, which should have been " |
| 10731 | "handled in the block above."); |
| Nico Weber | 715abaf | 2011-08-22 17:25:57 +0000 | [diff] [blame] | 10732 | |
| Chris Lattner | e247306 | 2007-05-28 06:28:18 +0000 | [diff] [blame] | 10733 | // Verify and clean out per-function state. |
| Benjamin Kramer | 8610cae | 2015-03-12 14:28:47 +0000 | [diff] [blame] | 10734 | if (Body && (!FD || !FD->isDefaulted())) { |
| Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 10735 | // C++ constructors that have function-try-blocks can't have return |
| 10736 | // statements in the handlers of that block. (C++ [except.handle]p14) |
| 10737 | // Verify this. |
| 10738 | if (FD && isa<CXXConstructorDecl>(FD) && isa<CXXTryStmt>(Body)) |
| 10739 | DiagnoseReturnInConstructorExceptionHandler(cast<CXXTryStmt>(Body)); |
| 10740 | |
| Richard Smith | def8bdb | 2011-08-12 18:44:32 +0000 | [diff] [blame] | 10741 | // Verify that gotos and switch cases don't jump into scopes illegally. |
| John McCall | aab3e41 | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 10742 | if (getCurFunction()->NeedsScopeChecking() && |
| Douglas Gregor | 5d944db | 2012-08-17 05:12:08 +0000 | [diff] [blame] | 10743 | !PP.isCodeCompletionEnabled()) |
| Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 10744 | DiagnoseInvalidJumps(Body); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10745 | |
| John McCall | deb646e | 2010-08-04 01:04:25 +0000 | [diff] [blame] | 10746 | if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(dcl)) { |
| 10747 | if (!Destructor->getParent()->isDependentType()) |
| 10748 | CheckDestructor(Destructor); |
| 10749 | |
| John McCall | a630995 | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 10750 | MarkBaseAndMemberDestructorsReferenced(Destructor->getLocation(), |
| 10751 | Destructor->getParent()); |
| John McCall | deb646e | 2010-08-04 01:04:25 +0000 | [diff] [blame] | 10752 | } |
| Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 10753 | |
| 10754 | // If any errors have occurred, clear out any temporaries that may have |
| 10755 | // been leftover. This ensures that these temporaries won't be picked up for |
| 10756 | // deletion in some later function. |
| Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 10757 | if (getDiagnostics().hasErrorOccurred() || |
| 10758 | getDiagnostics().getSuppressAllDiagnostics()) { |
| John McCall | 28fc709 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 10759 | DiscardCleanupsInEvaluationContext(); |
| DeLesley Hutchins | 8ecd491 | 2012-12-07 22:53:48 +0000 | [diff] [blame] | 10760 | } |
| Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 10761 | if (!getDiagnostics().hasUncompilableErrorOccurred() && |
| DeLesley Hutchins | 8ecd491 | 2012-12-07 22:53:48 +0000 | [diff] [blame] | 10762 | !isa<FunctionTemplateDecl>(dcl)) { |
| Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 10763 | // Since the body is valid, issue any analysis-based warnings that are |
| 10764 | // enabled. |
| Ted Kremenek | 1767a27 | 2011-02-23 01:51:48 +0000 | [diff] [blame] | 10765 | ActivePolicy = &WP; |
| Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 10766 | } |
| 10767 | |
| Richard Smith | 3607ffe | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 10768 | if (!IsInstantiation && FD && FD->isConstexpr() && !FD->isInvalidDecl() && |
| 10769 | (!CheckConstexprFunctionDecl(FD) || |
| 10770 | !CheckConstexprFunctionBody(FD, Body))) |
| Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 10771 | FD->setInvalidDecl(); |
| 10772 | |
| Hans Wennborg | d62cdd2c | 2014-09-04 22:16:40 +0000 | [diff] [blame] | 10773 | if (FD && FD->hasAttr<NakedAttr>()) { |
| 10774 | for (const Stmt *S : Body->children()) { |
| Ehsan Akhgari | 5c00c31 | 2014-09-09 02:49:40 +0000 | [diff] [blame] | 10775 | if (!isa<AsmStmt>(S) && !isa<NullStmt>(S)) { |
| Hans Wennborg | d62cdd2c | 2014-09-04 22:16:40 +0000 | [diff] [blame] | 10776 | Diag(S->getLocStart(), diag::err_non_asm_stmt_in_naked_function); |
| 10777 | Diag(FD->getAttr<NakedAttr>()->getLocation(), diag::note_attribute); |
| 10778 | FD->setInvalidDecl(); |
| 10779 | break; |
| 10780 | } |
| 10781 | } |
| 10782 | } |
| 10783 | |
| Nico Weber | 3b00fdc | 2015-03-07 19:52:39 +0000 | [diff] [blame] | 10784 | assert(ExprCleanupObjects.size() == |
| 10785 | ExprEvalContexts.back().NumCleanupObjects && |
| 10786 | "Leftover temporaries in function"); |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 10787 | assert(!ExprNeedsCleanups && "Unaccounted cleanups in function"); |
| Eli Friedman | 3bda6b1 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 10788 | assert(MaybeODRUseExprs.empty() && |
| 10789 | "Leftover expressions for odr-use checking"); |
| Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 10790 | } |
| 10791 | |
| John McCall | e99d5f3 | 2010-03-25 22:08:03 +0000 | [diff] [blame] | 10792 | if (!IsInstantiation) |
| 10793 | PopDeclContext(); |
| 10794 | |
| Eli Friedman | 71c8055 | 2012-01-05 03:35:19 +0000 | [diff] [blame] | 10795 | PopFunctionScopeInfo(ActivePolicy, dcl); |
| Douglas Gregor | a7e3ea3 | 2009-11-15 07:07:58 +0000 | [diff] [blame] | 10796 | // If any errors have occurred, clear out any temporaries that may have |
| 10797 | // been leftover. This ensures that these temporaries won't be picked up for |
| 10798 | // deletion in some later function. |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 10799 | if (getDiagnostics().hasErrorOccurred()) { |
| John McCall | 28fc709 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 10800 | DiscardCleanupsInEvaluationContext(); |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 10801 | } |
| Argyrios Kyrtzidis | 35672e7 | 2010-08-13 18:42:17 +0000 | [diff] [blame] | 10802 | |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10803 | return dcl; |
| Fariborz Jahanian | 85e1d0d | 2007-11-10 16:31:34 +0000 | [diff] [blame] | 10804 | } |
| 10805 | |
| Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 10806 | |
| 10807 | /// When we finish delayed parsing of an attribute, we must attach it to the |
| 10808 | /// relevant Decl. |
| 10809 | void Sema::ActOnFinishDelayedAttribute(Scope *S, Decl *D, |
| 10810 | ParsedAttributes &Attrs) { |
| DeLesley Hutchins | ceec306 | 2012-01-20 22:37:06 +0000 | [diff] [blame] | 10811 | // Always attach attributes to the underlying decl. |
| 10812 | if (TemplateDecl *TD = dyn_cast<TemplateDecl>(D)) |
| 10813 | D = TD->getTemplatedDecl(); |
| Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 10814 | ProcessDeclAttributeList(S, D, Attrs.getList()); |
| 10815 | |
| 10816 | if (CXXMethodDecl *Method = dyn_cast_or_null<CXXMethodDecl>(D)) |
| 10817 | if (Method->isStatic()) |
| 10818 | checkThisInStaticMemberFunctionAttributes(Method); |
| Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 10819 | } |
| 10820 | |
| 10821 | |
| Chris Lattner | ac18be9 | 2006-11-20 06:49:47 +0000 | [diff] [blame] | 10822 | /// ImplicitlyDefineFunction - An undeclared identifier was used in a function |
| 10823 | /// 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] | 10824 | NamedDecl *Sema::ImplicitlyDefineFunction(SourceLocation Loc, |
| Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 10825 | IdentifierInfo &II, Scope *S) { |
| Douglas Gregor | 5a80bd1 | 2009-03-02 00:19:53 +0000 | [diff] [blame] | 10826 | // Before we produce a declaration for an implicitly defined |
| 10827 | // function, see whether there was a locally-scoped declaration of |
| 10828 | // this name as a function or variable. If so, use that |
| 10829 | // (non-visible) declaration, and complain about it. |
| Richard Smith | 39b7968 | 2013-06-18 20:15:12 +0000 | [diff] [blame] | 10830 | if (NamedDecl *ExternCPrev = findLocallyScopedExternCDecl(&II)) { |
| 10831 | Diag(Loc, diag::warn_use_out_of_scope_declaration) << ExternCPrev; |
| 10832 | Diag(ExternCPrev->getLocation(), diag::note_previous_declaration); |
| 10833 | return ExternCPrev; |
| Douglas Gregor | 5a80bd1 | 2009-03-02 00:19:53 +0000 | [diff] [blame] | 10834 | } |
| 10835 | |
| Chris Lattner | 00e2607 | 2008-05-05 21:18:06 +0000 | [diff] [blame] | 10836 | // Extension in C99. Legal in C90, but warn about it. |
| Hans Wennborg | 70a1324 | 2011-12-08 15:56:07 +0000 | [diff] [blame] | 10837 | unsigned diag_id; |
| Daniel Dunbar | 07d0785 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 10838 | if (II.getName().startswith("__builtin_")) |
| Abramo Bagnara | 3732fa5 | 2012-01-09 10:05:48 +0000 | [diff] [blame] | 10839 | diag_id = diag::warn_builtin_unknown; |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10840 | else if (getLangOpts().C99) |
| Hans Wennborg | 70a1324 | 2011-12-08 15:56:07 +0000 | [diff] [blame] | 10841 | diag_id = diag::ext_implicit_function_decl; |
| Chris Lattner | 00e2607 | 2008-05-05 21:18:06 +0000 | [diff] [blame] | 10842 | else |
| Hans Wennborg | 70a1324 | 2011-12-08 15:56:07 +0000 | [diff] [blame] | 10843 | diag_id = diag::warn_implicit_function_decl; |
| 10844 | Diag(Loc, diag_id) << &II; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10845 | |
| Hans Wennborg | 70a1324 | 2011-12-08 15:56:07 +0000 | [diff] [blame] | 10846 | // Because typo correction is expensive, only do it if the implicit |
| 10847 | // function declaration is going to be treated as an error. |
| 10848 | if (Diags.getDiagnosticLevel(diag_id, Loc) >= DiagnosticsEngine::Error) { |
| 10849 | TypoCorrection Corrected; |
| Kaelyn Takata | 89c881b | 2014-10-27 18:07:29 +0000 | [diff] [blame] | 10850 | if (S && |
| 10851 | (Corrected = CorrectTypo( |
| 10852 | DeclarationNameInfo(&II, Loc), LookupOrdinaryName, S, nullptr, |
| 10853 | llvm::make_unique<DeclFilterCCC<FunctionDecl>>(), CTK_NonError))) |
| Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 10854 | diagnoseTypo(Corrected, PDiag(diag::note_function_suggestion), |
| 10855 | /*ErrorRecovery*/false); |
| Hans Wennborg | 2fb8b91 | 2011-12-06 09:46:12 +0000 | [diff] [blame] | 10856 | } |
| 10857 | |
| Chris Lattner | ac18be9 | 2006-11-20 06:49:47 +0000 | [diff] [blame] | 10858 | // Set a Declarator for the implicit definition: int foo(); |
| Chris Lattner | 353f574 | 2006-11-28 04:50:12 +0000 | [diff] [blame] | 10859 | const char *Dummy; |
| John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 10860 | AttributeFactory attrFactory; |
| 10861 | DeclSpec DS(attrFactory); |
| John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 10862 | unsigned DiagID; |
| Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 10863 | bool Error = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, Dummy, DiagID, |
| 10864 | Context.getPrintingPolicy()); |
| Jeffrey Yasskin | b332153 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 10865 | (void)Error; // Silence warning. |
| Chris Lattner | 353f574 | 2006-11-28 04:50:12 +0000 | [diff] [blame] | 10866 | assert(!Error && "Error setting up implicit decl!"); |
| Abramo Bagnara | aeeb989 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 10867 | SourceLocation NoLoc; |
| Chris Lattner | ac18be9 | 2006-11-20 06:49:47 +0000 | [diff] [blame] | 10868 | Declarator D(DS, Declarator::BlockContext); |
| Abramo Bagnara | aeeb989 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 10869 | D.AddTypeInfo(DeclaratorChunk::getFunction(/*HasProto=*/false, |
| 10870 | /*IsAmbiguous=*/false, |
| Richard Smith | 151b8a3 | 2014-04-07 15:16:58 +0000 | [diff] [blame] | 10871 | /*LParenLoc=*/NoLoc, |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 10872 | /*Params=*/nullptr, |
| Richard Smith | 151b8a3 | 2014-04-07 15:16:58 +0000 | [diff] [blame] | 10873 | /*NumParams=*/0, |
| Abramo Bagnara | aeeb989 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 10874 | /*EllipsisLoc=*/NoLoc, |
| 10875 | /*RParenLoc=*/NoLoc, |
| 10876 | /*TypeQuals=*/0, |
| 10877 | /*RefQualifierIsLvalueRef=*/true, |
| 10878 | /*RefQualifierLoc=*/NoLoc, |
| 10879 | /*ConstQualifierLoc=*/NoLoc, |
| 10880 | /*VolatileQualifierLoc=*/NoLoc, |
| Hal Finkel | 23a0739 | 2014-10-20 17:32:04 +0000 | [diff] [blame] | 10881 | /*RestrictQualifierLoc=*/NoLoc, |
| Abramo Bagnara | aeeb989 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 10882 | /*MutableLoc=*/NoLoc, |
| 10883 | EST_None, |
| 10884 | /*ESpecLoc=*/NoLoc, |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 10885 | /*Exceptions=*/nullptr, |
| 10886 | /*ExceptionRanges=*/nullptr, |
| Abramo Bagnara | aeeb989 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 10887 | /*NumExceptions=*/0, |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 10888 | /*NoexceptExpr=*/nullptr, |
| Richard Smith | 0b3a462 | 2014-11-13 20:01:57 +0000 | [diff] [blame] | 10889 | /*ExceptionSpecTokens=*/nullptr, |
| Abramo Bagnara | aeeb989 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 10890 | Loc, Loc, D), |
| John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 10891 | DS.getAttributes(), |
| Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 10892 | SourceLocation()); |
| Chris Lattner | ac18be9 | 2006-11-20 06:49:47 +0000 | [diff] [blame] | 10893 | D.SetIdentifier(&II, Loc); |
| Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 10894 | |
| Argyrios Kyrtzidis | 694dda1 | 2008-05-01 21:04:16 +0000 | [diff] [blame] | 10895 | // Insert this function into translation-unit scope. |
| 10896 | |
| 10897 | DeclContext *PrevDC = CurContext; |
| 10898 | CurContext = Context.getTranslationUnitDecl(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10899 | |
| Jordan Rose | d03d99d | 2013-03-05 01:27:54 +0000 | [diff] [blame] | 10900 | FunctionDecl *FD = cast<FunctionDecl>(ActOnDeclarator(TUScope, D)); |
| Steve Naroff | 3913ea4 | 2008-04-04 14:32:09 +0000 | [diff] [blame] | 10901 | FD->setImplicit(); |
| Argyrios Kyrtzidis | 694dda1 | 2008-05-01 21:04:16 +0000 | [diff] [blame] | 10902 | |
| 10903 | CurContext = PrevDC; |
| 10904 | |
| Douglas Gregor | e711f70 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 10905 | AddKnownFunctionAttributes(FD); |
| 10906 | |
| Steve Naroff | 3913ea4 | 2008-04-04 14:32:09 +0000 | [diff] [blame] | 10907 | return FD; |
| Chris Lattner | ac18be9 | 2006-11-20 06:49:47 +0000 | [diff] [blame] | 10908 | } |
| 10909 | |
| Douglas Gregor | e711f70 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 10910 | /// \brief Adds any function attributes that we know a priori based on |
| 10911 | /// the declaration of this function. |
| 10912 | /// |
| 10913 | /// These attributes can apply both to implicitly-declared builtins |
| 10914 | /// (like __builtin___printf_chk) or to library-declared functions |
| 10915 | /// like NSLog or printf. |
| Douglas Gregor | 8833683 | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 10916 | /// |
| 10917 | /// We need to check for duplicate attributes both here and where user-written |
| 10918 | /// attributes are applied to declarations. |
| Douglas Gregor | e711f70 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 10919 | void Sema::AddKnownFunctionAttributes(FunctionDecl *FD) { |
| 10920 | if (FD->isInvalidDecl()) |
| 10921 | return; |
| 10922 | |
| 10923 | // If this is a built-in function, map its builtin attributes to |
| 10924 | // actual attributes. |
| Douglas Gregor | 15fc956 | 2009-09-12 00:22:50 +0000 | [diff] [blame] | 10925 | if (unsigned BuiltinID = FD->getBuiltinID()) { |
| Douglas Gregor | e711f70 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 10926 | // Handle printf-formatting attributes. |
| 10927 | unsigned FormatIdx; |
| 10928 | bool HasVAListArg; |
| 10929 | if (Context.BuiltinInfo.isPrintfLike(BuiltinID, FormatIdx, HasVAListArg)) { |
| Aaron Ballman | 9ead124 | 2013-12-19 02:39:40 +0000 | [diff] [blame] | 10930 | if (!FD->hasAttr<FormatAttr>()) { |
| Jean-Daniel Dupas | 78536ae | 2012-01-24 22:32:46 +0000 | [diff] [blame] | 10931 | const char *fmt = "printf"; |
| 10932 | unsigned int NumParams = FD->getNumParams(); |
| 10933 | if (FormatIdx < NumParams && // NumParams may be 0 (e.g. vfprintf) |
| 10934 | FD->getParamDecl(FormatIdx)->getType()->isObjCObjectPointerType()) |
| 10935 | fmt = "NSString"; |
| Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 10936 | FD->addAttr(FormatAttr::CreateImplicit(Context, |
| Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 10937 | &Context.Idents.get(fmt), |
| 10938 | FormatIdx+1, |
| Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 10939 | HasVAListArg ? 0 : FormatIdx+2, |
| 10940 | FD->getLocation())); |
| Jean-Daniel Dupas | 78536ae | 2012-01-24 22:32:46 +0000 | [diff] [blame] | 10941 | } |
| Douglas Gregor | e711f70 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 10942 | } |
| Ted Kremenek | 5932c35 | 2010-07-16 02:11:15 +0000 | [diff] [blame] | 10943 | if (Context.BuiltinInfo.isScanfLike(BuiltinID, FormatIdx, |
| 10944 | HasVAListArg)) { |
| Aaron Ballman | 9ead124 | 2013-12-19 02:39:40 +0000 | [diff] [blame] | 10945 | if (!FD->hasAttr<FormatAttr>()) |
| Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 10946 | FD->addAttr(FormatAttr::CreateImplicit(Context, |
| Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 10947 | &Context.Idents.get("scanf"), |
| 10948 | FormatIdx+1, |
| Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 10949 | HasVAListArg ? 0 : FormatIdx+2, |
| 10950 | FD->getLocation())); |
| Ted Kremenek | 5932c35 | 2010-07-16 02:11:15 +0000 | [diff] [blame] | 10951 | } |
| Daniel Dunbar | 8eb018a | 2009-02-16 22:43:43 +0000 | [diff] [blame] | 10952 | |
| 10953 | // Mark const if we don't care about errno and that is the only |
| 10954 | // thing preventing the function from being const. This allows |
| 10955 | // IRgen to use LLVM intrinsics for such functions. |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10956 | if (!getLangOpts().MathErrno && |
| Daniel Dunbar | 8eb018a | 2009-02-16 22:43:43 +0000 | [diff] [blame] | 10957 | Context.BuiltinInfo.isConstWithoutErrno(BuiltinID)) { |
| Aaron Ballman | 9ead124 | 2013-12-19 02:39:40 +0000 | [diff] [blame] | 10958 | if (!FD->hasAttr<ConstAttr>()) |
| Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 10959 | FD->addAttr(ConstAttr::CreateImplicit(Context, FD->getLocation())); |
| Daniel Dunbar | 8eb018a | 2009-02-16 22:43:43 +0000 | [diff] [blame] | 10960 | } |
| Mike Stump | ca6c875 | 2009-07-27 19:14:18 +0000 | [diff] [blame] | 10961 | |
| Rafael Espindola | 2d21ab0 | 2011-10-12 19:51:18 +0000 | [diff] [blame] | 10962 | if (Context.BuiltinInfo.isReturnsTwice(BuiltinID) && |
| Aaron Ballman | 9ead124 | 2013-12-19 02:39:40 +0000 | [diff] [blame] | 10963 | !FD->hasAttr<ReturnsTwiceAttr>()) |
| Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 10964 | FD->addAttr(ReturnsTwiceAttr::CreateImplicit(Context, |
| 10965 | FD->getLocation())); |
| Aaron Ballman | 9ead124 | 2013-12-19 02:39:40 +0000 | [diff] [blame] | 10966 | if (Context.BuiltinInfo.isNoThrow(BuiltinID) && !FD->hasAttr<NoThrowAttr>()) |
| Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 10967 | FD->addAttr(NoThrowAttr::CreateImplicit(Context, FD->getLocation())); |
| Aaron Ballman | 9ead124 | 2013-12-19 02:39:40 +0000 | [diff] [blame] | 10968 | if (Context.BuiltinInfo.isConst(BuiltinID) && !FD->hasAttr<ConstAttr>()) |
| Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 10969 | FD->addAttr(ConstAttr::CreateImplicit(Context, FD->getLocation())); |
| Douglas Gregor | e711f70 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 10970 | } |
| 10971 | |
| 10972 | IdentifierInfo *Name = FD->getIdentifier(); |
| 10973 | if (!Name) |
| 10974 | return; |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10975 | if ((!getLangOpts().CPlusPlus && |
| Douglas Gregor | e711f70 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 10976 | FD->getDeclContext()->isTranslationUnit()) || |
| 10977 | (isa<LinkageSpecDecl>(FD->getDeclContext()) && |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10978 | cast<LinkageSpecDecl>(FD->getDeclContext())->getLanguage() == |
| Douglas Gregor | e711f70 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 10979 | LinkageSpecDecl::lang_c)) { |
| 10980 | // Okay: this could be a libc/libm/Objective-C function we know |
| 10981 | // about. |
| 10982 | } else |
| 10983 | return; |
| 10984 | |
| Jean-Daniel Dupas | 78536ae | 2012-01-24 22:32:46 +0000 | [diff] [blame] | 10985 | if (Name->isStr("asprintf") || Name->isStr("vasprintf")) { |
| Mike Stump | 82a9e44 | 2009-07-28 00:07:08 +0000 | [diff] [blame] | 10986 | // FIXME: asprintf and vasprintf aren't C99 functions. Should they be |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10987 | // target-specific builtins, perhaps? |
| Aaron Ballman | 9ead124 | 2013-12-19 02:39:40 +0000 | [diff] [blame] | 10988 | if (!FD->hasAttr<FormatAttr>()) |
| Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 10989 | FD->addAttr(FormatAttr::CreateImplicit(Context, |
| Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 10990 | &Context.Idents.get("printf"), 2, |
| Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 10991 | Name->isStr("vasprintf") ? 0 : 3, |
| 10992 | FD->getLocation())); |
| Mike Stump | a4de80b | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 10993 | } |
| Jordan Rose | 742c607 | 2012-08-08 21:17:31 +0000 | [diff] [blame] | 10994 | |
| 10995 | if (Name->isStr("__CFStringMakeConstantString")) { |
| 10996 | // We already have a __builtin___CFStringMakeConstantString, |
| 10997 | // but builds that use -fno-constant-cfstrings don't go through that. |
| Aaron Ballman | 9ead124 | 2013-12-19 02:39:40 +0000 | [diff] [blame] | 10998 | if (!FD->hasAttr<FormatArgAttr>()) |
| Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 10999 | FD->addAttr(FormatArgAttr::CreateImplicit(Context, 1, |
| 11000 | FD->getLocation())); |
| Jordan Rose | 742c607 | 2012-08-08 21:17:31 +0000 | [diff] [blame] | 11001 | } |
| Douglas Gregor | e711f70 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 11002 | } |
| Chris Lattner | 302b4be | 2006-11-19 02:31:38 +0000 | [diff] [blame] | 11003 | |
| John McCall | 703a3f8 | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 11004 | TypedefDecl *Sema::ParseTypedefDecl(Scope *S, Declarator &D, QualType T, |
| John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 11005 | TypeSourceInfo *TInfo) { |
| Chris Lattner | 776fac8 | 2007-06-09 00:53:06 +0000 | [diff] [blame] | 11006 | assert(D.getIdentifier() && "Wrong callback for declspec without declarator"); |
| Steve Naroff | f93b672 | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 11007 | assert(!T.isNull() && "GetTypeForDeclarator() returned null type"); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11008 | |
| John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 11009 | if (!TInfo) { |
| John McCall | 703a3f8 | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 11010 | assert(D.isInvalidType() && "no declarator info for valid type"); |
| John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 11011 | TInfo = Context.getTrivialTypeSourceInfo(T); |
| John McCall | 703a3f8 | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 11012 | } |
| 11013 | |
| Chris Lattner | 18b1962 | 2007-01-22 07:39:13 +0000 | [diff] [blame] | 11014 | // Scope manipulation handled by caller. |
| Chris Lattner | c5ffed4 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 11015 | TypedefDecl *NewTD = TypedefDecl::Create(Context, CurContext, |
| Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 11016 | D.getLocStart(), |
| Chris Lattner | c5ffed4 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 11017 | D.getIdentifierLoc(), |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11018 | D.getIdentifier(), |
| John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 11019 | TInfo); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11020 | |
| John McCall | 04fcd0d | 2011-02-01 08:20:08 +0000 | [diff] [blame] | 11021 | // Bail out immediately if we have an invalid declaration. |
| 11022 | if (D.isInvalidType()) { |
| 11023 | NewTD->setInvalidDecl(); |
| 11024 | return NewTD; |
| Anders Carlsson | 0275115 | 2009-03-10 17:07:44 +0000 | [diff] [blame] | 11025 | } |
| Fariborz Jahanian | db3d855 | 2013-11-19 00:09:48 +0000 | [diff] [blame] | 11026 | |
| Douglas Gregor | 4186681 | 2011-09-12 18:37:38 +0000 | [diff] [blame] | 11027 | if (D.getDeclSpec().isModulePrivateSpecified()) { |
| 11028 | if (CurContext->isFunctionOrMethod()) |
| 11029 | Diag(NewTD->getLocation(), diag::err_module_private_local) |
| 11030 | << 2 << NewTD->getDeclName() |
| 11031 | << SourceRange(D.getDeclSpec().getModulePrivateSpecLoc()) |
| 11032 | << FixItHint::CreateRemoval(D.getDeclSpec().getModulePrivateSpecLoc()); |
| 11033 | else |
| 11034 | NewTD->setModulePrivate(); |
| 11035 | } |
| Douglas Gregor | 26701a4 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 11036 | |
| John McCall | 04fcd0d | 2011-02-01 08:20:08 +0000 | [diff] [blame] | 11037 | // C++ [dcl.typedef]p8: |
| 11038 | // If the typedef declaration defines an unnamed class (or |
| 11039 | // enum), the first typedef-name declared by the declaration |
| 11040 | // to be that class type (or enum type) is used to denote the |
| 11041 | // class type (or enum type) for linkage purposes only. |
| 11042 | // We need to check whether the type was declared in the declaration. |
| 11043 | switch (D.getDeclSpec().getTypeSpecType()) { |
| 11044 | case TST_enum: |
| 11045 | case TST_struct: |
| Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 11046 | case TST_interface: |
| John McCall | 04fcd0d | 2011-02-01 08:20:08 +0000 | [diff] [blame] | 11047 | case TST_union: |
| 11048 | case TST_class: { |
| 11049 | TagDecl *tagFromDeclSpec = cast<TagDecl>(D.getDeclSpec().getRepAsDecl()); |
| David Majnemer | f9bde28 | 2015-03-11 06:45:39 +0000 | [diff] [blame] | 11050 | setTagNameForLinkagePurposes(tagFromDeclSpec, NewTD); |
| John McCall | 04fcd0d | 2011-02-01 08:20:08 +0000 | [diff] [blame] | 11051 | break; |
| 11052 | } |
| David Majnemer | f9bde28 | 2015-03-11 06:45:39 +0000 | [diff] [blame] | 11053 | |
| John McCall | 04fcd0d | 2011-02-01 08:20:08 +0000 | [diff] [blame] | 11054 | default: |
| 11055 | break; |
| 11056 | } |
| 11057 | |
| Steve Naroff | f93b672 | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 11058 | return NewTD; |
| Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 11059 | } |
| 11060 | |
| Douglas Gregor | d9034f0 | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 11061 | |
| Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 11062 | /// \brief Check that this is a valid underlying type for an enum declaration. |
| 11063 | bool Sema::CheckEnumUnderlyingType(TypeSourceInfo *TI) { |
| 11064 | SourceLocation UnderlyingLoc = TI->getTypeLoc().getBeginLoc(); |
| 11065 | QualType T = TI->getType(); |
| 11066 | |
| Eli Friedman | 52f32b9 | 2012-12-18 02:37:32 +0000 | [diff] [blame] | 11067 | if (T->isDependentType()) |
| Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 11068 | return false; |
| 11069 | |
| Eli Friedman | 52f32b9 | 2012-12-18 02:37:32 +0000 | [diff] [blame] | 11070 | if (const BuiltinType *BT = T->getAs<BuiltinType>()) |
| 11071 | if (BT->isInteger()) |
| 11072 | return false; |
| 11073 | |
| Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 11074 | Diag(UnderlyingLoc, diag::err_enum_invalid_underlying) << T; |
| 11075 | return true; |
| 11076 | } |
| 11077 | |
| 11078 | /// Check whether this is a valid redeclaration of a previous enumeration. |
| 11079 | /// \return true if the redeclaration was invalid. |
| 11080 | bool Sema::CheckEnumRedeclaration(SourceLocation EnumLoc, bool IsScoped, |
| 11081 | QualType EnumUnderlyingTy, |
| 11082 | const EnumDecl *Prev) { |
| 11083 | bool IsFixed = !EnumUnderlyingTy.isNull(); |
| 11084 | |
| 11085 | if (IsScoped != Prev->isScoped()) { |
| 11086 | Diag(EnumLoc, diag::err_enum_redeclare_scoped_mismatch) |
| 11087 | << Prev->isScoped(); |
| Alp Toker | 8c44db5 | 2014-01-06 11:31:06 +0000 | [diff] [blame] | 11088 | Diag(Prev->getLocation(), diag::note_previous_declaration); |
| Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 11089 | return true; |
| 11090 | } |
| 11091 | |
| 11092 | if (IsFixed && Prev->isFixed()) { |
| Richard Smith | 258a744 | 2012-03-26 04:08:46 +0000 | [diff] [blame] | 11093 | if (!EnumUnderlyingTy->isDependentType() && |
| 11094 | !Prev->getIntegerType()->isDependentType() && |
| 11095 | !Context.hasSameUnqualifiedType(EnumUnderlyingTy, |
| Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 11096 | Prev->getIntegerType())) { |
| Alp Toker | b9fa512 | 2014-01-06 11:31:18 +0000 | [diff] [blame] | 11097 | // TODO: Highlight the underlying type of the redeclaration. |
| Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 11098 | Diag(EnumLoc, diag::err_enum_redeclare_type_mismatch) |
| 11099 | << EnumUnderlyingTy << Prev->getIntegerType(); |
| Alp Toker | b9fa512 | 2014-01-06 11:31:18 +0000 | [diff] [blame] | 11100 | Diag(Prev->getLocation(), diag::note_previous_declaration) |
| 11101 | << Prev->getIntegerTypeRange(); |
| Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 11102 | return true; |
| 11103 | } |
| 11104 | } else if (IsFixed != Prev->isFixed()) { |
| 11105 | Diag(EnumLoc, diag::err_enum_redeclare_fixed_mismatch) |
| 11106 | << Prev->isFixed(); |
| Alp Toker | 8c44db5 | 2014-01-06 11:31:06 +0000 | [diff] [blame] | 11107 | Diag(Prev->getLocation(), diag::note_previous_declaration); |
| Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 11108 | return true; |
| 11109 | } |
| 11110 | |
| 11111 | return false; |
| 11112 | } |
| 11113 | |
| Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 11114 | /// \brief Get diagnostic %select index for tag kind for |
| 11115 | /// redeclaration diagnostic message. |
| 11116 | /// WARNING: Indexes apply to particular diagnostics only! |
| 11117 | /// |
| 11118 | /// \returns diagnostic %select index. |
| Joao Matos | a5c42e9 | 2012-09-01 00:13:24 +0000 | [diff] [blame] | 11119 | static unsigned getRedeclDiagFromTagKind(TagTypeKind Tag) { |
| Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 11120 | switch (Tag) { |
| Joao Matos | a5c42e9 | 2012-09-01 00:13:24 +0000 | [diff] [blame] | 11121 | case TTK_Struct: return 0; |
| 11122 | case TTK_Interface: return 1; |
| 11123 | case TTK_Class: return 2; |
| 11124 | default: llvm_unreachable("Invalid tag kind for redecl diagnostic!"); |
| Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 11125 | } |
| Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 11126 | } |
| 11127 | |
| 11128 | /// \brief Determine if tag kind is a class-key compatible with |
| 11129 | /// class for redeclaration (class, struct, or __interface). |
| 11130 | /// |
| Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 11131 | /// \returns true iff the tag kind is compatible. |
| Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 11132 | static bool isClassCompatTagKind(TagTypeKind Tag) |
| 11133 | { |
| 11134 | return Tag == TTK_Struct || Tag == TTK_Class || Tag == TTK_Interface; |
| 11135 | } |
| 11136 | |
| Douglas Gregor | d9034f0 | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 11137 | /// \brief Determine whether a tag with a given kind is acceptable |
| 11138 | /// as a redeclaration of the given tag declaration. |
| 11139 | /// |
| 11140 | /// \returns true if the new tag kind is acceptable, false otherwise. |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11141 | bool Sema::isAcceptableTagRedeclaration(const TagDecl *Previous, |
| Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 11142 | TagTypeKind NewTag, bool isDefinition, |
| Douglas Gregor | d9034f0 | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 11143 | SourceLocation NewTagLoc, |
| 11144 | const IdentifierInfo &Name) { |
| 11145 | // C++ [dcl.type.elab]p3: |
| 11146 | // The class-key or enum keyword present in the |
| 11147 | // elaborated-type-specifier shall agree in kind with the |
| Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 11148 | // declaration to which the name in the elaborated-type-specifier |
| Douglas Gregor | d9034f0 | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 11149 | // refers. This rule also applies to the form of |
| 11150 | // elaborated-type-specifier that declares a class-name or |
| 11151 | // friend class since it can be construed as referring to the |
| 11152 | // definition of the class. Thus, in any |
| 11153 | // elaborated-type-specifier, the enum keyword shall be used to |
| Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 11154 | // refer to an enumeration (7.2), the union class-key shall be |
| Douglas Gregor | d9034f0 | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 11155 | // used to refer to a union (clause 9), and either the class or |
| 11156 | // struct class-key shall be used to refer to a class (clause 9) |
| 11157 | // declared using the class or struct class-key. |
| Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 11158 | TagTypeKind OldTag = Previous->getTagKind(); |
| Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 11159 | if (!isDefinition || !isClassCompatTagKind(NewTag)) |
| Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 11160 | if (OldTag == NewTag) |
| 11161 | return true; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11162 | |
| Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 11163 | if (isClassCompatTagKind(OldTag) && isClassCompatTagKind(NewTag)) { |
| Douglas Gregor | d9034f0 | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 11164 | // Warn about the struct/class tag mismatch. |
| 11165 | bool isTemplate = false; |
| 11166 | if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Previous)) |
| 11167 | isTemplate = Record->getDescribedClassTemplate(); |
| 11168 | |
| Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 11169 | if (!ActiveTemplateInstantiations.empty()) { |
| 11170 | // In a template instantiation, do not offer fix-its for tag mismatches |
| 11171 | // since they usually mess up the template instead of fixing the problem. |
| 11172 | Diag(NewTagLoc, diag::warn_struct_class_tag_mismatch) |
| Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 11173 | << getRedeclDiagFromTagKind(NewTag) << isTemplate << &Name |
| 11174 | << getRedeclDiagFromTagKind(OldTag); |
| Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 11175 | return true; |
| 11176 | } |
| 11177 | |
| 11178 | if (isDefinition) { |
| 11179 | // On definitions, check previous tags and issue a fix-it for each |
| 11180 | // one that doesn't match the current tag. |
| 11181 | if (Previous->getDefinition()) { |
| 11182 | // Don't suggest fix-its for redefinitions. |
| 11183 | return true; |
| 11184 | } |
| 11185 | |
| 11186 | bool previousMismatch = false; |
| Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 11187 | for (auto I : Previous->redecls()) { |
| Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 11188 | if (I->getTagKind() != NewTag) { |
| 11189 | if (!previousMismatch) { |
| 11190 | previousMismatch = true; |
| 11191 | Diag(NewTagLoc, diag::warn_struct_class_previous_tag_mismatch) |
| Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 11192 | << getRedeclDiagFromTagKind(NewTag) << isTemplate << &Name |
| 11193 | << getRedeclDiagFromTagKind(I->getTagKind()); |
| Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 11194 | } |
| 11195 | Diag(I->getInnerLocStart(), diag::note_struct_class_suggestion) |
| Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 11196 | << getRedeclDiagFromTagKind(NewTag) |
| Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 11197 | << FixItHint::CreateReplacement(I->getInnerLocStart(), |
| Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 11198 | TypeWithKeyword::getTagTypeKindName(NewTag)); |
| Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 11199 | } |
| 11200 | } |
| 11201 | return true; |
| 11202 | } |
| 11203 | |
| 11204 | // Check for a previous definition. If current tag and definition |
| 11205 | // are same type, do nothing. If no definition, but disagree with |
| 11206 | // with previous tag type, give a warning, but no fix-it. |
| 11207 | const TagDecl *Redecl = Previous->getDefinition() ? |
| 11208 | Previous->getDefinition() : Previous; |
| 11209 | if (Redecl->getTagKind() == NewTag) { |
| 11210 | return true; |
| 11211 | } |
| 11212 | |
| Douglas Gregor | d9034f0 | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 11213 | Diag(NewTagLoc, diag::warn_struct_class_tag_mismatch) |
| Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 11214 | << getRedeclDiagFromTagKind(NewTag) << isTemplate << &Name |
| 11215 | << getRedeclDiagFromTagKind(OldTag); |
| Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 11216 | Diag(Redecl->getLocation(), diag::note_previous_use); |
| 11217 | |
| Alp Toker | f6a24ce | 2013-12-05 16:25:25 +0000 | [diff] [blame] | 11218 | // If there is a previous definition, suggest a fix-it. |
| Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 11219 | if (Previous->getDefinition()) { |
| 11220 | Diag(NewTagLoc, diag::note_struct_class_suggestion) |
| Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 11221 | << getRedeclDiagFromTagKind(Redecl->getTagKind()) |
| Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 11222 | << FixItHint::CreateReplacement(SourceRange(NewTagLoc), |
| Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 11223 | TypeWithKeyword::getTagTypeKindName(Redecl->getTagKind())); |
| Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 11224 | } |
| 11225 | |
| Douglas Gregor | d9034f0 | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 11226 | return true; |
| 11227 | } |
| 11228 | return false; |
| 11229 | } |
| 11230 | |
| Reid Kleckner | 0902a51 | 2014-07-10 23:44:52 +0000 | [diff] [blame] | 11231 | /// Add a minimal nested name specifier fixit hint to allow lookup of a tag name |
| 11232 | /// from an outer enclosing namespace or file scope inside a friend declaration. |
| 11233 | /// This should provide the commented out code in the following snippet: |
| 11234 | /// namespace N { |
| 11235 | /// struct X; |
| 11236 | /// namespace M { |
| 11237 | /// struct Y { friend struct /*N::*/ X; }; |
| 11238 | /// } |
| 11239 | /// } |
| Reid Kleckner | 675d438 | 2014-07-11 00:16:51 +0000 | [diff] [blame] | 11240 | static FixItHint createFriendTagNNSFixIt(Sema &SemaRef, NamedDecl *ND, Scope *S, |
| 11241 | SourceLocation NameLoc) { |
| Reid Kleckner | 0902a51 | 2014-07-10 23:44:52 +0000 | [diff] [blame] | 11242 | // While the decl is in a namespace, do repeated lookup of that name and see |
| 11243 | // if we get the same namespace back. If we do not, continue until |
| 11244 | // translation unit scope, at which point we have a fully qualified NNS. |
| 11245 | SmallVector<IdentifierInfo *, 4> Namespaces; |
| 11246 | DeclContext *DC = ND->getDeclContext()->getRedeclContext(); |
| 11247 | for (; !DC->isTranslationUnit(); DC = DC->getParent()) { |
| 11248 | // This tag should be declared in a namespace, which can only be enclosed by |
| 11249 | // other namespaces. Bail if there's an anonymous namespace in the chain. |
| 11250 | NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(DC); |
| 11251 | if (!Namespace || Namespace->isAnonymousNamespace()) |
| Reid Kleckner | 675d438 | 2014-07-11 00:16:51 +0000 | [diff] [blame] | 11252 | return FixItHint(); |
| Reid Kleckner | 0902a51 | 2014-07-10 23:44:52 +0000 | [diff] [blame] | 11253 | IdentifierInfo *II = Namespace->getIdentifier(); |
| 11254 | Namespaces.push_back(II); |
| 11255 | NamedDecl *Lookup = SemaRef.LookupSingleName( |
| 11256 | S, II, NameLoc, Sema::LookupNestedNameSpecifierName); |
| 11257 | if (Lookup == Namespace) |
| 11258 | break; |
| 11259 | } |
| 11260 | |
| 11261 | // Once we have all the namespaces, reverse them to go outermost first, and |
| 11262 | // build an NNS. |
| 11263 | SmallString<64> Insertion; |
| 11264 | llvm::raw_svector_ostream OS(Insertion); |
| 11265 | if (DC->isTranslationUnit()) |
| 11266 | OS << "::"; |
| 11267 | std::reverse(Namespaces.begin(), Namespaces.end()); |
| 11268 | for (auto *II : Namespaces) |
| 11269 | OS << II->getName() << "::"; |
| 11270 | OS.flush(); |
| Reid Kleckner | 675d438 | 2014-07-11 00:16:51 +0000 | [diff] [blame] | 11271 | return FixItHint::CreateInsertion(NameLoc, Insertion); |
| Reid Kleckner | 0902a51 | 2014-07-10 23:44:52 +0000 | [diff] [blame] | 11272 | } |
| 11273 | |
| Richard Smith | 65ebb4a | 2015-03-26 04:09:53 +0000 | [diff] [blame] | 11274 | /// \brief This is invoked when we see 'struct foo' or 'struct {'. In the |
| Chris Lattner | 1300fb9 | 2007-01-23 23:42:53 +0000 | [diff] [blame] | 11275 | /// 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] | 11276 | /// 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] | 11277 | /// reference/declaration/definition of a tag. |
| Richard Smith | 649c7b06 | 2014-01-08 00:56:48 +0000 | [diff] [blame] | 11278 | /// |
| Richard Smith | 65ebb4a | 2015-03-26 04:09:53 +0000 | [diff] [blame] | 11279 | /// \param IsTypeSpecifier \c true if this is a type-specifier (or |
| Richard Smith | 649c7b06 | 2014-01-08 00:56:48 +0000 | [diff] [blame] | 11280 | /// trailing-type-specifier) other than one in an alias-declaration. |
| Richard Smith | 65ebb4a | 2015-03-26 04:09:53 +0000 | [diff] [blame] | 11281 | /// |
| 11282 | /// \param SkipBody If non-null, will be set to true if the caller should skip |
| 11283 | /// the definition of this tag, and treat it as if it were a declaration. |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11284 | Decl *Sema::ActOnTag(Scope *S, unsigned TagSpec, TagUseKind TUK, |
| Douglas Gregor | 009f699 | 2010-09-16 23:58:57 +0000 | [diff] [blame] | 11285 | SourceLocation KWLoc, CXXScopeSpec &SS, |
| 11286 | IdentifierInfo *Name, SourceLocation NameLoc, |
| 11287 | AttributeList *Attr, AccessSpecifier AS, |
| Douglas Gregor | 2820e69 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 11288 | SourceLocation ModulePrivateLoc, |
| Douglas Gregor | 009f699 | 2010-09-16 23:58:57 +0000 | [diff] [blame] | 11289 | MultiTemplateParamsArg TemplateParameterLists, |
| Abramo Bagnara | 0e05e24 | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 11290 | bool &OwnedDecl, bool &IsDependent, |
| Richard Smith | 0f8ee22 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 11291 | SourceLocation ScopedEnumKWLoc, |
| 11292 | bool ScopedEnumUsesClassTag, |
| Richard Smith | 649c7b06 | 2014-01-08 00:56:48 +0000 | [diff] [blame] | 11293 | TypeResult UnderlyingType, |
| Richard Smith | 65ebb4a | 2015-03-26 04:09:53 +0000 | [diff] [blame] | 11294 | bool IsTypeSpecifier, bool *SkipBody) { |
| Douglas Gregor | c811d8f | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 11295 | // If this is not a definition, it must have a name. |
| Douglas Gregor | b7d17dd | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 11296 | IdentifierInfo *OrigName = Name; |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 11297 | assert((Name != nullptr || TUK == TUK_Definition) && |
| Chris Lattner | 7b9ace6 | 2007-01-23 20:11:08 +0000 | [diff] [blame] | 11298 | "Nameless record must be a definition!"); |
| John McCall | ace48cd | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 11299 | assert(TemplateParameterLists.size() == 0 || TUK != TUK_Reference); |
| Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 11300 | |
| Douglas Gregor | d6ab874 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 11301 | OwnedDecl = false; |
| Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 11302 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| Richard Smith | 0f8ee22 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 11303 | bool ScopedEnum = ScopedEnumKWLoc.isValid(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11304 | |
| Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 11305 | // FIXME: Check explicit specializations more carefully. |
| 11306 | bool isExplicitSpecialization = false; |
| Douglas Gregor | 5f0e252 | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 11307 | bool Invalid = false; |
| John McCall | ace48cd | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 11308 | |
| 11309 | // We only need to do this matching if we have template parameters |
| 11310 | // or a scope specifier, which also conveniently avoids this work |
| 11311 | // for non-C++ cases. |
| Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 11312 | if (TemplateParameterLists.size() > 0 || |
| John McCall | ace48cd | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 11313 | (SS.isNotEmpty() && TUK != TUK_Reference)) { |
| Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 11314 | if (TemplateParameterList *TemplateParams = |
| 11315 | MatchTemplateParametersToScopeSpecifier( |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 11316 | KWLoc, NameLoc, SS, nullptr, TemplateParameterLists, |
| Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 11317 | TUK == TUK_Friend, isExplicitSpecialization, Invalid)) { |
| Richard Smith | 1d4b2e1 | 2013-04-01 21:43:41 +0000 | [diff] [blame] | 11318 | if (Kind == TTK_Enum) { |
| 11319 | Diag(KWLoc, diag::err_enum_template); |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 11320 | return nullptr; |
| Richard Smith | 1d4b2e1 | 2013-04-01 21:43:41 +0000 | [diff] [blame] | 11321 | } |
| 11322 | |
| Douglas Gregor | 3dad842 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 11323 | if (TemplateParams->size() > 0) { |
| Douglas Gregor | e93e46c | 2009-07-22 23:48:44 +0000 | [diff] [blame] | 11324 | // This is a declaration or definition of a class template (which may |
| 11325 | // be a member of another template). |
| Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 11326 | |
| Douglas Gregor | 5f0e252 | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 11327 | if (Invalid) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 11328 | return nullptr; |
| Abramo Bagnara | 0adf29a | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 11329 | |
| Douglas Gregor | e93e46c | 2009-07-22 23:48:44 +0000 | [diff] [blame] | 11330 | OwnedDecl = false; |
| John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 11331 | DeclResult Result = CheckClassTemplate(S, TagSpec, TUK, KWLoc, |
| Douglas Gregor | e93e46c | 2009-07-22 23:48:44 +0000 | [diff] [blame] | 11332 | SS, Name, NameLoc, Attr, |
| Abramo Bagnara | 0adf29a | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 11333 | TemplateParams, AS, |
| Douglas Gregor | 2820e69 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 11334 | ModulePrivateLoc, |
| Nikola Smiljanic | 4fc9153 | 2014-07-17 01:59:34 +0000 | [diff] [blame] | 11335 | /*FriendLoc*/SourceLocation(), |
| Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 11336 | TemplateParameterLists.size()-1, |
| Richard Smith | be3980b | 2015-03-27 00:41:57 +0000 | [diff] [blame] | 11337 | TemplateParameterLists.data(), |
| 11338 | SkipBody); |
| Douglas Gregor | e93e46c | 2009-07-22 23:48:44 +0000 | [diff] [blame] | 11339 | return Result.get(); |
| 11340 | } else { |
| Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 11341 | // The "template<>" header is extraneous. |
| 11342 | Diag(TemplateParams->getTemplateLoc(), diag::err_template_tag_noparams) |
| Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 11343 | << TypeWithKeyword::getTagTypeKindName(Kind) << Name; |
| Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 11344 | isExplicitSpecialization = true; |
| Douglas Gregor | e93e46c | 2009-07-22 23:48:44 +0000 | [diff] [blame] | 11345 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11346 | } |
| 11347 | } |
| 11348 | |
| Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 11349 | // Figure out the underlying type if this a enum declaration. We need to do |
| 11350 | // this early, because it's needed to detect if this is an incompatible |
| 11351 | // redeclaration. |
| 11352 | llvm::PointerUnion<const Type*, TypeSourceInfo*> EnumUnderlying; |
| 11353 | |
| 11354 | if (Kind == TTK_Enum) { |
| 11355 | if (UnderlyingType.isInvalid() || (!UnderlyingType.get() && ScopedEnum)) |
| 11356 | // No underlying type explicitly specified, or we failed to parse the |
| 11357 | // type, default to int. |
| 11358 | EnumUnderlying = Context.IntTy.getTypePtr(); |
| 11359 | else if (UnderlyingType.get()) { |
| 11360 | // C++0x 7.2p2: The type-specifier-seq of an enum-base shall name an |
| 11361 | // integral type; any cv-qualification is ignored. |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 11362 | TypeSourceInfo *TI = nullptr; |
| Richard Smith | eece8c3 | 2012-03-15 00:22:18 +0000 | [diff] [blame] | 11363 | GetTypeFromParser(UnderlyingType.get(), &TI); |
| Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 11364 | EnumUnderlying = TI; |
| 11365 | |
| Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 11366 | if (CheckEnumUnderlyingType(TI)) |
| Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 11367 | // Recover by falling back to int. |
| 11368 | EnumUnderlying = Context.IntTy.getTypePtr(); |
| Douglas Gregor | 2b988fd | 2010-12-16 00:24:44 +0000 | [diff] [blame] | 11369 | |
| Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 11370 | if (DiagnoseUnexpandedParameterPack(TI->getTypeLoc().getBeginLoc(), TI, |
| Douglas Gregor | 2b988fd | 2010-12-16 00:24:44 +0000 | [diff] [blame] | 11371 | UPPC_FixedUnderlyingType)) |
| 11372 | EnumUnderlying = Context.IntTy.getTypePtr(); |
| 11373 | |
| Alp Toker | bfa3934 | 2014-01-14 12:51:41 +0000 | [diff] [blame] | 11374 | } else if (getLangOpts().MSVCCompat) |
| Francois Pichet | a310806 | 2010-10-18 15:01:13 +0000 | [diff] [blame] | 11375 | // Microsoft enums are always of int type. |
| 11376 | EnumUnderlying = Context.IntTy.getTypePtr(); |
| Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 11377 | } |
| 11378 | |
| Douglas Gregor | c6f58fe | 2009-01-12 22:49:06 +0000 | [diff] [blame] | 11379 | DeclContext *SearchDC = CurContext; |
| Argyrios Kyrtzidis | 8ad00b2 | 2008-11-09 22:53:32 +0000 | [diff] [blame] | 11380 | DeclContext *DC = CurContext; |
| Douglas Gregor | 87f5406 | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 11381 | bool isStdBadAlloc = false; |
| Douglas Gregor | dee1be8 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 11382 | |
| Chandler Carruth | a419dbb | 2010-03-01 21:17:36 +0000 | [diff] [blame] | 11383 | RedeclarationKind Redecl = ForRedeclaration; |
| 11384 | if (TUK == TUK_Friend || TUK == TUK_Reference) |
| 11385 | Redecl = NotForRedeclaration; |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 11386 | |
| 11387 | LookupResult Previous(*this, Name, NameLoc, LookupTagName, Redecl); |
| Argyrios Kyrtzidis | e02eb2b | 2008-11-09 22:09:58 +0000 | [diff] [blame] | 11388 | if (Name && SS.isNotEmpty()) { |
| 11389 | // We have a nested-name tag ('struct foo::bar'). |
| 11390 | |
| 11391 | // Check for invalid 'foo::'. |
| Argyrios Kyrtzidis | 8ad00b2 | 2008-11-09 22:53:32 +0000 | [diff] [blame] | 11392 | if (SS.isInvalid()) { |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 11393 | Name = nullptr; |
| Argyrios Kyrtzidis | e02eb2b | 2008-11-09 22:09:58 +0000 | [diff] [blame] | 11394 | goto CreateNewDecl; |
| 11395 | } |
| 11396 | |
| John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 11397 | // If this is a friend or a reference to a class in a dependent |
| 11398 | // context, don't try to make a decl for it. |
| 11399 | if (TUK == TUK_Friend || TUK == TUK_Reference) { |
| 11400 | DC = computeDeclContext(SS, false); |
| 11401 | if (!DC) { |
| 11402 | IsDependent = true; |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 11403 | return nullptr; |
| John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 11404 | } |
| John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 11405 | } else { |
| 11406 | DC = computeDeclContext(SS, true); |
| 11407 | if (!DC) { |
| 11408 | Diag(SS.getRange().getBegin(), diag::err_dependent_nested_name_spec) |
| 11409 | << SS.getRange(); |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 11410 | return nullptr; |
| John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 11411 | } |
| John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 11412 | } |
| 11413 | |
| John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 11414 | if (RequireCompleteDeclContext(SS, DC)) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 11415 | return nullptr; |
| Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 11416 | |
| Douglas Gregor | 8761da5 | 2009-02-03 00:34:39 +0000 | [diff] [blame] | 11417 | SearchDC = DC; |
| Argyrios Kyrtzidis | 8ad00b2 | 2008-11-09 22:53:32 +0000 | [diff] [blame] | 11418 | // Look-up name inside 'foo::'. |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 11419 | LookupQualifiedName(Previous, DC); |
| John McCall | 6538c93 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 11420 | |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 11421 | if (Previous.isAmbiguous()) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 11422 | return nullptr; |
| John McCall | 6538c93 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 11423 | |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 11424 | if (Previous.empty()) { |
| Douglas Gregor | d2e6a45 | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 11425 | // Name lookup did not find anything. However, if the |
| 11426 | // nested-name-specifier refers to the current instantiation, |
| 11427 | // and that current instantiation has any dependent base |
| 11428 | // classes, we might find something at instantiation time: treat |
| 11429 | // this as a dependent elaborated-type-specifier. |
| John McCall | ace48cd | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 11430 | // But this only makes any sense for reference-like lookups. |
| 11431 | if (Previous.wasNotFoundInCurrentInstantiation() && |
| 11432 | (TUK == TUK_Reference || TUK == TUK_Friend)) { |
| Douglas Gregor | d2e6a45 | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 11433 | IsDependent = true; |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 11434 | return nullptr; |
| Douglas Gregor | d2e6a45 | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 11435 | } |
| 11436 | |
| 11437 | // A tag 'foo::bar' must already exist. |
| Douglas Gregor | f5af358 | 2010-03-31 23:17:41 +0000 | [diff] [blame] | 11438 | Diag(NameLoc, diag::err_not_tag_in_scope) |
| 11439 | << Kind << Name << DC << SS.getRange(); |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 11440 | Name = nullptr; |
| Douglas Gregor | b8006faf | 2009-05-27 17:30:49 +0000 | [diff] [blame] | 11441 | Invalid = true; |
| Argyrios Kyrtzidis | e02eb2b | 2008-11-09 22:09:58 +0000 | [diff] [blame] | 11442 | goto CreateNewDecl; |
| 11443 | } |
| Chris Lattner | d977351 | 2009-01-21 02:38:50 +0000 | [diff] [blame] | 11444 | } else if (Name) { |
| Argyrios Kyrtzidis | e02eb2b | 2008-11-09 22:09:58 +0000 | [diff] [blame] | 11445 | // If this is a named struct, check to see if there was a previous forward |
| 11446 | // declaration or definition. |
| Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 11447 | // FIXME: We're looking into outer scopes here, even when we |
| 11448 | // shouldn't be. Doing so can result in ambiguities that we |
| 11449 | // shouldn't be diagnosing. |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 11450 | LookupName(Previous, S); |
| 11451 | |
| John McCall | 3c581bf | 2013-03-20 01:53:00 +0000 | [diff] [blame] | 11452 | // When declaring or defining a tag, ignore ambiguities introduced |
| 11453 | // by types using'ed into this scope. |
| Douglas Gregor | 5d1d9e3 | 2011-05-09 21:46:33 +0000 | [diff] [blame] | 11454 | if (Previous.isAmbiguous() && |
| 11455 | (TUK == TUK_Definition || TUK == TUK_Declaration)) { |
| Douglas Gregor | ed8a29b | 2011-05-04 00:25:33 +0000 | [diff] [blame] | 11456 | LookupResult::Filter F = Previous.makeFilter(); |
| 11457 | while (F.hasNext()) { |
| 11458 | NamedDecl *ND = F.next(); |
| 11459 | if (ND->getDeclContext()->getRedeclContext() != SearchDC) |
| 11460 | F.erase(); |
| 11461 | } |
| 11462 | F.done(); |
| Douglas Gregor | ed8a29b | 2011-05-04 00:25:33 +0000 | [diff] [blame] | 11463 | } |
| John McCall | 3c581bf | 2013-03-20 01:53:00 +0000 | [diff] [blame] | 11464 | |
| 11465 | // C++11 [namespace.memdef]p3: |
| 11466 | // If the name in a friend declaration is neither qualified nor |
| 11467 | // a template-id and the declaration is a function or an |
| 11468 | // elaborated-type-specifier, the lookup to determine whether |
| 11469 | // the entity has been previously declared shall not consider |
| 11470 | // any scopes outside the innermost enclosing namespace. |
| 11471 | // |
| Reid Kleckner | 0902a51 | 2014-07-10 23:44:52 +0000 | [diff] [blame] | 11472 | // MSVC doesn't implement the above rule for types, so a friend tag |
| 11473 | // declaration may be a redeclaration of a type declared in an enclosing |
| 11474 | // scope. They do implement this rule for friend functions. |
| 11475 | // |
| John McCall | 3c581bf | 2013-03-20 01:53:00 +0000 | [diff] [blame] | 11476 | // Does it matter that this should be by scope instead of by |
| 11477 | // semantic context? |
| 11478 | if (!Previous.empty() && TUK == TUK_Friend) { |
| 11479 | DeclContext *EnclosingNS = SearchDC->getEnclosingNamespaceContext(); |
| 11480 | LookupResult::Filter F = Previous.makeFilter(); |
| Reid Kleckner | 0902a51 | 2014-07-10 23:44:52 +0000 | [diff] [blame] | 11481 | bool FriendSawTagOutsideEnclosingNamespace = false; |
| John McCall | 3c581bf | 2013-03-20 01:53:00 +0000 | [diff] [blame] | 11482 | while (F.hasNext()) { |
| 11483 | NamedDecl *ND = F.next(); |
| 11484 | DeclContext *DC = ND->getDeclContext()->getRedeclContext(); |
| Douglas Gregor | f83b4ea | 2013-06-27 20:42:30 +0000 | [diff] [blame] | 11485 | if (DC->isFileContext() && |
| 11486 | !EnclosingNS->Encloses(ND->getDeclContext())) { |
| Reid Kleckner | 0902a51 | 2014-07-10 23:44:52 +0000 | [diff] [blame] | 11487 | if (getLangOpts().MSVCCompat) |
| 11488 | FriendSawTagOutsideEnclosingNamespace = true; |
| 11489 | else |
| 11490 | F.erase(); |
| Douglas Gregor | f83b4ea | 2013-06-27 20:42:30 +0000 | [diff] [blame] | 11491 | } |
| John McCall | 3c581bf | 2013-03-20 01:53:00 +0000 | [diff] [blame] | 11492 | } |
| 11493 | F.done(); |
| Reid Kleckner | 0902a51 | 2014-07-10 23:44:52 +0000 | [diff] [blame] | 11494 | |
| 11495 | // Diagnose this MSVC extension in the easy case where lookup would have |
| 11496 | // unambiguously found something outside the enclosing namespace. |
| 11497 | if (Previous.isSingleResult() && FriendSawTagOutsideEnclosingNamespace) { |
| 11498 | NamedDecl *ND = Previous.getFoundDecl(); |
| Reid Kleckner | 675d438 | 2014-07-11 00:16:51 +0000 | [diff] [blame] | 11499 | Diag(NameLoc, diag::ext_friend_tag_redecl_outside_namespace) |
| 11500 | << createFriendTagNNSFixIt(*this, ND, S, NameLoc); |
| Reid Kleckner | 0902a51 | 2014-07-10 23:44:52 +0000 | [diff] [blame] | 11501 | } |
| John McCall | 3c581bf | 2013-03-20 01:53:00 +0000 | [diff] [blame] | 11502 | } |
| Reid Kleckner | 0902a51 | 2014-07-10 23:44:52 +0000 | [diff] [blame] | 11503 | |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 11504 | // Note: there used to be some attempt at recovery here. |
| 11505 | if (Previous.isAmbiguous()) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 11506 | return nullptr; |
| Douglas Gregor | 82ac25e | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 11507 | |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 11508 | if (!getLangOpts().CPlusPlus && TUK != TUK_Reference) { |
| Douglas Gregor | 82ac25e | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 11509 | // FIXME: This makes sure that we ignore the contexts associated |
| 11510 | // with C structs, unions, and enums when looking for a matching |
| 11511 | // tag declaration or definition. See the similar lookup tweak |
| Douglas Gregor | ed8f288 | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 11512 | // in Sema::LookupName; is there a better way to deal with this? |
| Douglas Gregor | c6f58fe | 2009-01-12 22:49:06 +0000 | [diff] [blame] | 11513 | while (isa<RecordDecl>(SearchDC) || isa<EnumDecl>(SearchDC)) |
| 11514 | SearchDC = SearchDC->getParent(); |
| Douglas Gregor | 82ac25e | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 11515 | } |
| Argyrios Kyrtzidis | e02eb2b | 2008-11-09 22:09:58 +0000 | [diff] [blame] | 11516 | } |
| 11517 | |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 11518 | if (Previous.isSingleResult() && |
| 11519 | Previous.getFoundDecl()->isTemplateParameter()) { |
| Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 11520 | // Maybe we will complain about the shadowed template parameter. |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 11521 | DiagnoseTemplateParameterShadow(NameLoc, Previous.getFoundDecl()); |
| Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 11522 | // Just pretend that we didn't see the previous declaration. |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 11523 | Previous.clear(); |
| Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 11524 | } |
| 11525 | |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 11526 | if (getLangOpts().CPlusPlus && Name && DC && StdNamespace && |
| Argyrios Kyrtzidis | 2d68810 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 11527 | DC->Equals(getStdNamespace()) && Name->isStr("bad_alloc")) { |
| Douglas Gregor | 87f5406 | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 11528 | // This is a declaration of or a reference to "std::bad_alloc". |
| 11529 | isStdBadAlloc = true; |
| 11530 | |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 11531 | if (Previous.empty() && StdBadAlloc) { |
| Douglas Gregor | 87f5406 | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 11532 | // std::bad_alloc has been implicitly declared (but made invisible to |
| 11533 | // name lookup). Fill in this implicit declaration as the previous |
| 11534 | // declaration, so that the declarations get chained appropriately. |
| Argyrios Kyrtzidis | 2d68810 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 11535 | Previous.addDecl(getStdBadAlloc()); |
| Douglas Gregor | 87f5406 | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 11536 | } |
| 11537 | } |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 11538 | |
| John McCall | e9eaf8e | 2010-03-25 21:28:06 +0000 | [diff] [blame] | 11539 | // If we didn't find a previous declaration, and this is a reference |
| 11540 | // (or friend reference), move to the correct scope. In C++, we |
| 11541 | // also need to do a redeclaration lookup there, just in case |
| 11542 | // there's a shadow friend decl. |
| 11543 | if (Name && Previous.empty() && |
| 11544 | (TUK == TUK_Reference || TUK == TUK_Friend)) { |
| 11545 | if (Invalid) goto CreateNewDecl; |
| 11546 | assert(SS.isEmpty()); |
| 11547 | |
| 11548 | if (TUK == TUK_Reference) { |
| 11549 | // C++ [basic.scope.pdecl]p5: |
| 11550 | // -- for an elaborated-type-specifier of the form |
| 11551 | // |
| 11552 | // class-key identifier |
| 11553 | // |
| 11554 | // if the elaborated-type-specifier is used in the |
| 11555 | // decl-specifier-seq or parameter-declaration-clause of a |
| 11556 | // function defined in namespace scope, the identifier is |
| 11557 | // declared as a class-name in the namespace that contains |
| 11558 | // the declaration; otherwise, except as a friend |
| 11559 | // declaration, the identifier is declared in the smallest |
| 11560 | // non-class, non-function-prototype scope that contains the |
| 11561 | // declaration. |
| 11562 | // |
| 11563 | // C99 6.7.2.3p8 has a similar (but not identical!) provision for |
| 11564 | // C structs and unions. |
| 11565 | // |
| 11566 | // It is an error in C++ to declare (rather than define) an enum |
| 11567 | // type, including via an elaborated type specifier. We'll |
| 11568 | // diagnose that later; for now, declare the enum in the same |
| 11569 | // scope as we would have picked for any other tag type. |
| 11570 | // |
| 11571 | // GNU C also supports this behavior as part of its incomplete |
| 11572 | // enum types extension, while GNU C++ does not. |
| 11573 | // |
| 11574 | // Find the context where we'll be declaring the tag. |
| 11575 | // FIXME: We would like to maintain the current DeclContext as the |
| 11576 | // lexical context, |
| Nick Lewycky | f604212 | 2012-03-10 07:47:07 +0000 | [diff] [blame] | 11577 | while (!SearchDC->isFileContext() && !SearchDC->isFunctionOrMethod()) |
| John McCall | e9eaf8e | 2010-03-25 21:28:06 +0000 | [diff] [blame] | 11578 | SearchDC = SearchDC->getParent(); |
| 11579 | |
| 11580 | // Find the scope where we'll be declaring the tag. |
| 11581 | while (S->isClassScope() || |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 11582 | (getLangOpts().CPlusPlus && |
| John McCall | e9eaf8e | 2010-03-25 21:28:06 +0000 | [diff] [blame] | 11583 | S->isFunctionPrototypeScope()) || |
| 11584 | ((S->getFlags() & Scope::DeclScope) == 0) || |
| Ted Kremenek | c37877d | 2013-10-08 17:08:03 +0000 | [diff] [blame] | 11585 | (S->getEntity() && S->getEntity()->isTransparentContext())) |
| John McCall | e9eaf8e | 2010-03-25 21:28:06 +0000 | [diff] [blame] | 11586 | S = S->getParent(); |
| 11587 | } else { |
| 11588 | assert(TUK == TUK_Friend); |
| 11589 | // C++ [namespace.memdef]p3: |
| 11590 | // If a friend declaration in a non-local class first declares a |
| 11591 | // class or function, the friend class or function is a member of |
| 11592 | // the innermost enclosing namespace. |
| 11593 | SearchDC = SearchDC->getEnclosingNamespaceContext(); |
| John McCall | e9eaf8e | 2010-03-25 21:28:06 +0000 | [diff] [blame] | 11594 | } |
| 11595 | |
| John McCall | e87beb2 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 11596 | // In C++, we need to do a redeclaration lookup to properly |
| 11597 | // diagnose some problems. |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 11598 | if (getLangOpts().CPlusPlus) { |
| John McCall | e9eaf8e | 2010-03-25 21:28:06 +0000 | [diff] [blame] | 11599 | Previous.setRedeclarationKind(ForRedeclaration); |
| 11600 | LookupQualifiedName(Previous, SearchDC); |
| 11601 | } |
| 11602 | } |
| 11603 | |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 11604 | if (!Previous.empty()) { |
| Alp Toker | 0abb057 | 2014-01-18 00:59:32 +0000 | [diff] [blame] | 11605 | NamedDecl *PrevDecl = Previous.getFoundDecl(); |
| 11606 | NamedDecl *DirectPrevDecl = |
| 11607 | getLangOpts().MSVCCompat ? *Previous.begin() : PrevDecl; |
| John McCall | e87beb2 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 11608 | |
| 11609 | // It's okay to have a tag decl in the same scope as a typedef |
| 11610 | // which hides a tag decl in the same scope. Finding this |
| 11611 | // insanity with a redeclaration lookup can only actually happen |
| 11612 | // in C++. |
| 11613 | // |
| 11614 | // This is also okay for elaborated-type-specifiers, which is |
| 11615 | // technically forbidden by the current standard but which is |
| 11616 | // okay according to the likely resolution of an open issue; |
| 11617 | // 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] | 11618 | if (getLangOpts().CPlusPlus) { |
| Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 11619 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(PrevDecl)) { |
| John McCall | e87beb2 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 11620 | if (const TagType *TT = TD->getUnderlyingType()->getAs<TagType>()) { |
| 11621 | TagDecl *Tag = TT->getDecl(); |
| 11622 | if (Tag->getDeclName() == Name && |
| Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 11623 | Tag->getDeclContext()->getRedeclContext() |
| 11624 | ->Equals(TD->getDeclContext()->getRedeclContext())) { |
| John McCall | e87beb2 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 11625 | PrevDecl = Tag; |
| 11626 | Previous.clear(); |
| 11627 | Previous.addDecl(Tag); |
| Douglas Gregor | fcee946 | 2010-08-27 22:55:10 +0000 | [diff] [blame] | 11628 | Previous.resolveKind(); |
| John McCall | e87beb2 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 11629 | } |
| 11630 | } |
| 11631 | } |
| 11632 | } |
| 11633 | |
| Argyrios Kyrtzidis | 0811489 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 11634 | if (TagDecl *PrevTagDecl = dyn_cast<TagDecl>(PrevDecl)) { |
| Chris Lattner | 9ff58d7 | 2008-07-03 03:30:58 +0000 | [diff] [blame] | 11635 | // If this is a use of a previous tag, or if the tag is already declared |
| 11636 | // in the same scope (so that the definition/declaration completes or |
| Argyrios Kyrtzidis | 0811489 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 11637 | // rementions the tag), reuse the decl. |
| John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 11638 | if (TUK == TUK_Reference || TUK == TUK_Friend || |
| Alp Toker | 320374c | 2014-01-17 12:57:21 +0000 | [diff] [blame] | 11639 | isDeclInScope(DirectPrevDecl, SearchDC, S, |
| Richard Smith | 72bcaec | 2013-12-05 04:30:04 +0000 | [diff] [blame] | 11640 | SS.isNotEmpty() || isExplicitSpecialization)) { |
| Chris Lattner | 9ff58d7 | 2008-07-03 03:30:58 +0000 | [diff] [blame] | 11641 | // Make sure that this wasn't declared as an enum and now used as a |
| 11642 | // struct or something similar. |
| Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 11643 | if (!isAcceptableTagRedeclaration(PrevTagDecl, Kind, |
| 11644 | TUK == TUK_Definition, KWLoc, |
| 11645 | *Name)) { |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11646 | bool SafeToContinue |
| Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 11647 | = (PrevTagDecl->getTagKind() != TTK_Enum && |
| 11648 | Kind != TTK_Enum); |
| Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 11649 | if (SafeToContinue) |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11650 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
| Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 11651 | << Name |
| Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 11652 | << FixItHint::CreateReplacement(SourceRange(KWLoc), |
| 11653 | PrevTagDecl->getKindName()); |
| Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 11654 | else |
| 11655 | Diag(KWLoc, diag::err_use_with_wrong_tag) << Name; |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 11656 | Diag(PrevTagDecl->getLocation(), diag::note_previous_use); |
| Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 11657 | |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11658 | if (SafeToContinue) |
| Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 11659 | Kind = PrevTagDecl->getTagKind(); |
| 11660 | else { |
| 11661 | // Recover by making this an anonymous redefinition. |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 11662 | Name = nullptr; |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 11663 | Previous.clear(); |
| Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 11664 | Invalid = true; |
| 11665 | } |
| 11666 | } |
| 11667 | |
| Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 11668 | if (Kind == TTK_Enum && PrevTagDecl->getTagKind() == TTK_Enum) { |
| 11669 | const EnumDecl *PrevEnum = cast<EnumDecl>(PrevTagDecl); |
| 11670 | |
| Richard Smith | 0f8ee22 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 11671 | // If this is an elaborated-type-specifier for a scoped enumeration, |
| 11672 | // the 'class' keyword is not necessary and not permitted. |
| 11673 | if (TUK == TUK_Reference || TUK == TUK_Friend) { |
| 11674 | if (ScopedEnum) |
| 11675 | Diag(ScopedEnumKWLoc, diag::err_enum_class_reference) |
| 11676 | << PrevEnum->isScoped() |
| 11677 | << FixItHint::CreateRemoval(ScopedEnumKWLoc); |
| 11678 | return PrevTagDecl; |
| 11679 | } |
| 11680 | |
| Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 11681 | QualType EnumUnderlyingTy; |
| 11682 | if (TypeSourceInfo *TI = EnumUnderlying.dyn_cast<TypeSourceInfo*>()) |
| Richard Smith | 8bcc086 | 2014-01-08 01:16:19 +0000 | [diff] [blame] | 11683 | EnumUnderlyingTy = TI->getType().getUnqualifiedType(); |
| Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 11684 | else if (const Type *T = EnumUnderlying.dyn_cast<const Type*>()) |
| 11685 | EnumUnderlyingTy = QualType(T, 0); |
| 11686 | |
| Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 11687 | // All conflicts with previous declarations are recovered by |
| Richard Smith | b66d777 | 2012-03-23 23:09:08 +0000 | [diff] [blame] | 11688 | // returning the previous declaration, unless this is a definition, |
| 11689 | // in which case we want the caller to bail out. |
| Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 11690 | if (CheckEnumRedeclaration(NameLoc.isValid() ? NameLoc : KWLoc, |
| 11691 | ScopedEnum, EnumUnderlyingTy, PrevEnum)) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 11692 | return TUK == TUK_Declaration ? PrevTagDecl : nullptr; |
| Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 11693 | } |
| 11694 | |
| David Majnemer | 55890bf | 2013-06-11 03:51:23 +0000 | [diff] [blame] | 11695 | // C++11 [class.mem]p1: |
| David Majnemer | bfce664 | 2013-06-11 06:19:45 +0000 | [diff] [blame] | 11696 | // A member shall not be declared twice in the member-specification, |
| David Majnemer | 55890bf | 2013-06-11 03:51:23 +0000 | [diff] [blame] | 11697 | // except that a nested class or member class template can be declared |
| 11698 | // and then later defined. |
| 11699 | if (TUK == TUK_Declaration && PrevDecl->isCXXClassMember() && |
| 11700 | S->isDeclScope(PrevDecl)) { |
| 11701 | Diag(NameLoc, diag::ext_member_redeclared); |
| 11702 | Diag(PrevTagDecl->getLocation(), diag::note_previous_declaration); |
| 11703 | } |
| 11704 | |
| Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 11705 | if (!Invalid) { |
| John McCall | 2976f8b | 2014-05-14 07:54:17 +0000 | [diff] [blame] | 11706 | // If this is a use, just return the declaration we found, unless |
| 11707 | // we have attributes. |
| Chris Lattner | 9ff58d7 | 2008-07-03 03:30:58 +0000 | [diff] [blame] | 11708 | |
| Douglas Gregor | c811d8f | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 11709 | // FIXME: In the future, return a variant or some other clue |
| 11710 | // for the consumer of this Decl to know it doesn't own it. |
| 11711 | // For our current ASTs this shouldn't be a problem, but will |
| 11712 | // need to be changed with DeclGroups. |
| John McCall | 2976f8b | 2014-05-14 07:54:17 +0000 | [diff] [blame] | 11713 | if (!Attr && |
| 11714 | ((TUK == TUK_Reference && |
| 11715 | (!PrevTagDecl->getFriendObjectKind() || getLangOpts().MicrosoftExt)) |
| 11716 | || TUK == TUK_Friend)) |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11717 | return PrevTagDecl; |
| Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 11718 | |
| Douglas Gregor | c811d8f | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 11719 | // Diagnose attempts to redefine a tag. |
| John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 11720 | if (TUK == TUK_Definition) { |
| Richard Smith | 65ebb4a | 2015-03-26 04:09:53 +0000 | [diff] [blame] | 11721 | if (NamedDecl *Def = PrevTagDecl->getDefinition()) { |
| Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 11722 | // If we're defining a specialization and the previous definition |
| 11723 | // is from an implicit instantiation, don't emit an error |
| 11724 | // here; we'll catch this in the general case below. |
| Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 11725 | bool IsExplicitSpecializationAfterInstantiation = false; |
| 11726 | if (isExplicitSpecialization) { |
| 11727 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Def)) |
| 11728 | IsExplicitSpecializationAfterInstantiation = |
| 11729 | RD->getTemplateSpecializationKind() != |
| 11730 | TSK_ExplicitSpecialization; |
| 11731 | else if (EnumDecl *ED = dyn_cast<EnumDecl>(Def)) |
| 11732 | IsExplicitSpecializationAfterInstantiation = |
| 11733 | ED->getTemplateSpecializationKind() != |
| 11734 | TSK_ExplicitSpecialization; |
| 11735 | } |
| 11736 | |
| Richard Smith | be3980b | 2015-03-27 00:41:57 +0000 | [diff] [blame] | 11737 | NamedDecl *Hidden = nullptr; |
| Richard Smith | 65ebb4a | 2015-03-26 04:09:53 +0000 | [diff] [blame] | 11738 | if (SkipBody && getLangOpts().CPlusPlus && |
| Richard Smith | be3980b | 2015-03-27 00:41:57 +0000 | [diff] [blame] | 11739 | !hasVisibleDefinition(Def, &Hidden)) { |
| Richard Smith | 65ebb4a | 2015-03-26 04:09:53 +0000 | [diff] [blame] | 11740 | // There is a definition of this tag, but it is not visible. We |
| 11741 | // explicitly make use of C++'s one definition rule here, and |
| 11742 | // assume that this definition is identical to the hidden one |
| 11743 | // we already have. Make the existing definition visible and |
| 11744 | // use it in place of this one. |
| 11745 | *SkipBody = true; |
| 11746 | if (auto *Listener = getASTMutationListener()) |
| Richard Smith | be3980b | 2015-03-27 00:41:57 +0000 | [diff] [blame] | 11747 | Listener->RedefinedHiddenDefinition(Hidden, KWLoc); |
| 11748 | Hidden->setHidden(false); |
| Richard Smith | 65ebb4a | 2015-03-26 04:09:53 +0000 | [diff] [blame] | 11749 | return Def; |
| 11750 | } else if (!IsExplicitSpecializationAfterInstantiation) { |
| James Molloy | 6f8780b | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 11751 | // A redeclaration in function prototype scope in C isn't |
| 11752 | // visible elsewhere, so merely issue a warning. |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 11753 | if (!getLangOpts().CPlusPlus && S->containedInPrototypeScope()) |
| James Molloy | 6f8780b | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 11754 | Diag(NameLoc, diag::warn_redefinition_in_param_list) << Name; |
| 11755 | else |
| 11756 | Diag(NameLoc, diag::err_redefinition) << Name; |
| Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 11757 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 11758 | // If this is a redefinition, recover by making this |
| 11759 | // struct be anonymous, which will make any later |
| 11760 | // references get the previous definition. |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 11761 | Name = nullptr; |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 11762 | Previous.clear(); |
| Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 11763 | Invalid = true; |
| 11764 | } |
| Douglas Gregor | dee1be8 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 11765 | } else { |
| 11766 | // If the type is currently being defined, complain |
| 11767 | // about a nested redefinition. |
| David Majnemer | 738e587 | 2014-12-28 09:18:54 +0000 | [diff] [blame] | 11768 | auto *TD = Context.getTagDeclType(PrevTagDecl)->getAsTagDecl(); |
| 11769 | if (TD->isBeingDefined()) { |
| Douglas Gregor | dee1be8 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 11770 | Diag(NameLoc, diag::err_nested_redefinition) << Name; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11771 | Diag(PrevTagDecl->getLocation(), |
| Douglas Gregor | dee1be8 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 11772 | diag::note_previous_definition); |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 11773 | Name = nullptr; |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 11774 | Previous.clear(); |
| Douglas Gregor | dee1be8 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 11775 | Invalid = true; |
| 11776 | } |
| Douglas Gregor | c811d8f | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 11777 | } |
| Douglas Gregor | dee1be8 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 11778 | |
| Douglas Gregor | c811d8f | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 11779 | // Okay, this is definition of a previously declared or referenced |
| John McCall | a16fc89 | 2014-05-14 18:31:48 +0000 | [diff] [blame] | 11780 | // tag. We're going to create a new Decl for it. |
| 11781 | } |
| 11782 | |
| 11783 | // Okay, we're going to make a redeclaration. If this is some kind |
| 11784 | // of reference, make sure we build the redeclaration in the same DC |
| 11785 | // as the original, and ignore the current access specifier. |
| 11786 | if (TUK == TUK_Friend || TUK == TUK_Reference) { |
| 11787 | SearchDC = PrevTagDecl->getDeclContext(); |
| 11788 | AS = AS_none; |
| Douglas Gregor | dee1be8 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 11789 | } |
| Argyrios Kyrtzidis | 0811489 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 11790 | } |
| Douglas Gregor | c811d8f | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 11791 | // If we get here we have (another) forward declaration or we |
| John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 11792 | // have a definition. Just create a new decl. |
| 11793 | |
| Douglas Gregor | c811d8f | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 11794 | } else { |
| 11795 | // 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] | 11796 | // scope, e.g. "struct foo; void bar() { struct foo; }", just create a |
| Douglas Gregor | c811d8f | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 11797 | // new decl/type. We set PrevDecl to NULL so that the entities |
| 11798 | // have distinct types. |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 11799 | Previous.clear(); |
| Chris Lattner | 7b9ace6 | 2007-01-23 20:11:08 +0000 | [diff] [blame] | 11800 | } |
| Douglas Gregor | c811d8f | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 11801 | // If we get here, we're going to create a new Decl. If PrevDecl |
| 11802 | // is non-NULL, it's a definition of the tag declared by |
| 11803 | // PrevDecl. If it's NULL, we have a new definition. |
| John McCall | e87beb2 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 11804 | |
| 11805 | |
| 11806 | // Otherwise, PrevDecl is not a tag, but was found with tag |
| 11807 | // lookup. This is only actually possible in C++, where a few |
| 11808 | // things like templates still live in the tag namespace. |
| Argyrios Kyrtzidis | 0811489 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 11809 | } else { |
| John McCall | e87beb2 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 11810 | // Use a better diagnostic if an elaborated-type-specifier |
| 11811 | // found the wrong kind of type on the first |
| 11812 | // (non-redeclaration) lookup. |
| 11813 | if ((TUK == TUK_Reference || TUK == TUK_Friend) && |
| 11814 | !Previous.isForRedeclaration()) { |
| 11815 | unsigned Kind = 0; |
| 11816 | if (isa<TypedefDecl>(PrevDecl)) Kind = 1; |
| Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 11817 | else if (isa<TypeAliasDecl>(PrevDecl)) Kind = 2; |
| 11818 | else if (isa<ClassTemplateDecl>(PrevDecl)) Kind = 3; |
| John McCall | e87beb2 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 11819 | Diag(NameLoc, diag::err_tag_reference_non_tag) << Kind; |
| 11820 | Diag(PrevDecl->getLocation(), diag::note_declared_at); |
| 11821 | Invalid = true; |
| 11822 | |
| 11823 | // Otherwise, only diagnose if the declaration is in scope. |
| Richard Smith | 72bcaec | 2013-12-05 04:30:04 +0000 | [diff] [blame] | 11824 | } else if (!isDeclInScope(PrevDecl, SearchDC, S, |
| 11825 | SS.isNotEmpty() || isExplicitSpecialization)) { |
| John McCall | e87beb2 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 11826 | // do nothing |
| 11827 | |
| 11828 | // Diagnose implicit declarations introduced by elaborated types. |
| 11829 | } else if (TUK == TUK_Reference || TUK == TUK_Friend) { |
| 11830 | unsigned Kind = 0; |
| 11831 | if (isa<TypedefDecl>(PrevDecl)) Kind = 1; |
| Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 11832 | else if (isa<TypeAliasDecl>(PrevDecl)) Kind = 2; |
| 11833 | else if (isa<ClassTemplateDecl>(PrevDecl)) Kind = 3; |
| John McCall | e87beb2 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 11834 | Diag(NameLoc, diag::err_tag_reference_conflict) << Kind; |
| 11835 | Diag(PrevDecl->getLocation(), diag::note_previous_decl) << PrevDecl; |
| 11836 | Invalid = true; |
| 11837 | |
| 11838 | // Otherwise it's a declaration. Call out a particularly common |
| 11839 | // case here. |
| Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 11840 | } else if (TypedefNameDecl *TND = dyn_cast<TypedefNameDecl>(PrevDecl)) { |
| 11841 | unsigned Kind = 0; |
| 11842 | if (isa<TypeAliasDecl>(PrevDecl)) Kind = 1; |
| John McCall | e87beb2 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 11843 | Diag(NameLoc, diag::err_tag_definition_of_typedef) |
| Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 11844 | << Name << Kind << TND->getUnderlyingType(); |
| John McCall | e87beb2 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 11845 | Diag(PrevDecl->getLocation(), diag::note_previous_decl) << PrevDecl; |
| 11846 | Invalid = true; |
| 11847 | |
| 11848 | // Otherwise, diagnose. |
| 11849 | } else { |
| 11850 | // The tag name clashes with something else in the target scope, |
| 11851 | // issue an error and recover by making this tag be anonymous. |
| Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 11852 | Diag(NameLoc, diag::err_redefinition_different_kind) << Name; |
| Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 11853 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 11854 | Name = nullptr; |
| Douglas Gregor | dee1be8 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 11855 | Invalid = true; |
| Argyrios Kyrtzidis | 7da34d0 | 2008-07-16 07:45:46 +0000 | [diff] [blame] | 11856 | } |
| John McCall | e87beb2 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 11857 | |
| 11858 | // The existing declaration isn't relevant to us; we're in a |
| 11859 | // new scope, so clear out the previous declaration. |
| 11860 | Previous.clear(); |
| Chris Lattner | 8799cf2 | 2007-01-23 01:57:16 +0000 | [diff] [blame] | 11861 | } |
| Chris Lattner | 18b1962 | 2007-01-22 07:39:13 +0000 | [diff] [blame] | 11862 | } |
| Argyrios Kyrtzidis | 16ac9be | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 11863 | |
| Chris Lattner | 438e501 | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 11864 | CreateNewDecl: |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11865 | |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 11866 | TagDecl *PrevDecl = nullptr; |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 11867 | if (Previous.isSingleResult()) |
| 11868 | PrevDecl = cast<TagDecl>(Previous.getFoundDecl()); |
| 11869 | |
| Chris Lattner | bf0b798 | 2007-01-23 04:27:41 +0000 | [diff] [blame] | 11870 | // If there is an identifier, use the location of the identifier as the |
| 11871 | // location of the decl, otherwise use the location of the struct/union |
| 11872 | // keyword. |
| 11873 | SourceLocation Loc = NameLoc.isValid() ? NameLoc : KWLoc; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11874 | |
| Douglas Gregor | c811d8f | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 11875 | // Otherwise, create a new declaration. If there is a previous |
| 11876 | // declaration of the same entity, the two will be linked via |
| 11877 | // PrevDecl. |
| Chris Lattner | 7b9ace6 | 2007-01-23 20:11:08 +0000 | [diff] [blame] | 11878 | TagDecl *New; |
| Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 11879 | |
| Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 11880 | bool IsForwardReference = false; |
| Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 11881 | if (Kind == TTK_Enum) { |
| Chris Lattner | 776fac8 | 2007-06-09 00:53:06 +0000 | [diff] [blame] | 11882 | // FIXME: Tag decls should be chained to any simultaneous vardecls, e.g.: |
| 11883 | // enum X { A, B, C } D; D should chain to X. |
| Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 11884 | New = EnumDecl::Create(Context, SearchDC, KWLoc, Loc, Name, |
| Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 11885 | cast_or_null<EnumDecl>(PrevDecl), ScopedEnum, |
| Abramo Bagnara | 0e05e24 | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 11886 | ScopedEnumUsesClassTag, !EnumUnderlying.isNull()); |
| Chris Lattner | 5f52150 | 2007-01-25 06:27:24 +0000 | [diff] [blame] | 11887 | // If this is an undefined enum, warn. |
| Douglas Gregor | c9ea2d5 | 2010-06-22 14:26:35 +0000 | [diff] [blame] | 11888 | if (TUK != TUK_Definition && !Invalid) { |
| 11889 | TagDecl *Def; |
| Douglas Gregor | 780420e | 2013-03-25 22:22:35 +0000 | [diff] [blame] | 11890 | if ((getLangOpts().CPlusPlus11 || getLangOpts().ObjC2) && |
| 11891 | cast<EnumDecl>(New)->isFixed()) { |
| Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 11892 | // C++0x: 7.2p2: opaque-enum-declaration. |
| 11893 | // Conflicts are diagnosed above. Do nothing. |
| 11894 | } |
| 11895 | else if (PrevDecl && (Def = cast<EnumDecl>(PrevDecl)->getDefinition())) { |
| Douglas Gregor | c9ea2d5 | 2010-06-22 14:26:35 +0000 | [diff] [blame] | 11896 | Diag(Loc, diag::ext_forward_ref_enum_def) |
| 11897 | << New; |
| 11898 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 11899 | } else { |
| Francois Pichet | 488b4a7 | 2010-09-12 05:06:55 +0000 | [diff] [blame] | 11900 | unsigned DiagID = diag::ext_forward_ref_enum; |
| Alp Toker | bfa3934 | 2014-01-14 12:51:41 +0000 | [diff] [blame] | 11901 | if (getLangOpts().MSVCCompat) |
| Francois Pichet | 488b4a7 | 2010-09-12 05:06:55 +0000 | [diff] [blame] | 11902 | DiagID = diag::ext_ms_forward_ref_enum; |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 11903 | else if (getLangOpts().CPlusPlus) |
| Francois Pichet | 488b4a7 | 2010-09-12 05:06:55 +0000 | [diff] [blame] | 11904 | DiagID = diag::err_forward_ref_enum; |
| 11905 | Diag(Loc, DiagID); |
| Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 11906 | |
| 11907 | // If this is a forward-declared reference to an enumeration, make a |
| 11908 | // note of it; we won't actually be introducing the declaration into |
| 11909 | // the declaration context. |
| 11910 | if (TUK == TUK_Reference) |
| 11911 | IsForwardReference = true; |
| Douglas Gregor | c9ea2d5 | 2010-06-22 14:26:35 +0000 | [diff] [blame] | 11912 | } |
| Douglas Gregor | d45b93b | 2009-03-06 18:34:03 +0000 | [diff] [blame] | 11913 | } |
| Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 11914 | |
| 11915 | if (EnumUnderlying) { |
| 11916 | EnumDecl *ED = cast<EnumDecl>(New); |
| 11917 | if (TypeSourceInfo *TI = EnumUnderlying.dyn_cast<TypeSourceInfo*>()) |
| 11918 | ED->setIntegerTypeSourceInfo(TI); |
| 11919 | else |
| 11920 | ED->setIntegerType(QualType(EnumUnderlying.get<const Type*>(), 0)); |
| 11921 | ED->setPromotionType(ED->getIntegerType()); |
| 11922 | } |
| 11923 | |
| Argyrios Kyrtzidis | 554a07b | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 11924 | } else { |
| 11925 | // struct/union/class |
| 11926 | |
| Chris Lattner | 776fac8 | 2007-06-09 00:53:06 +0000 | [diff] [blame] | 11927 | // FIXME: Tag decls should be chained to any simultaneous vardecls, e.g.: |
| 11928 | // struct X { int A; } D; D should chain to X. |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 11929 | if (getLangOpts().CPlusPlus) { |
| Ted Kremenek | 6ddf53e | 2008-09-05 17:39:33 +0000 | [diff] [blame] | 11930 | // FIXME: Look for a way to use RecordDecl for simple structs. |
| Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 11931 | New = CXXRecordDecl::Create(Context, Kind, SearchDC, KWLoc, Loc, Name, |
| Douglas Gregor | c811d8f | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 11932 | cast_or_null<CXXRecordDecl>(PrevDecl)); |
| Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 11933 | |
| Argyrios Kyrtzidis | 2d68810 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 11934 | if (isStdBadAlloc && (!StdBadAlloc || getStdBadAlloc()->isImplicit())) |
| Douglas Gregor | 87f5406 | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 11935 | StdBadAlloc = cast<CXXRecordDecl>(New); |
| 11936 | } else |
| Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 11937 | New = RecordDecl::Create(Context, Kind, SearchDC, KWLoc, Loc, Name, |
| Douglas Gregor | c811d8f | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 11938 | cast_or_null<RecordDecl>(PrevDecl)); |
| Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 11939 | } |
| Douglas Gregor | c811d8f | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 11940 | |
| Richard Smith | 649c7b06 | 2014-01-08 00:56:48 +0000 | [diff] [blame] | 11941 | // C++11 [dcl.type]p3: |
| 11942 | // A type-specifier-seq shall not define a class or enumeration [...]. |
| 11943 | if (getLangOpts().CPlusPlus && IsTypeSpecifier && TUK == TUK_Definition) { |
| 11944 | Diag(New->getLocation(), diag::err_type_defined_in_type_specifier) |
| 11945 | << Context.getTagDeclType(New); |
| 11946 | Invalid = true; |
| 11947 | } |
| 11948 | |
| John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 11949 | // Maybe add qualifier info. |
| 11950 | if (SS.isNotEmpty()) { |
| Fariborz Jahanian | 862fac9 | 2010-05-14 21:35:02 +0000 | [diff] [blame] | 11951 | if (SS.isSet()) { |
| Douglas Gregor | b7d17dd | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 11952 | // If this is either a declaration or a definition, check the |
| 11953 | // nested-name-specifier against the current context. We don't do this |
| 11954 | // for explicit specializations, because they have similar checking |
| 11955 | // (with more specific diagnostics) in the call to |
| 11956 | // CheckMemberSpecialization, below. |
| 11957 | if (!isExplicitSpecialization && |
| 11958 | (TUK == TUK_Definition || TUK == TUK_Declaration) && |
| David Majnemer | be4c437 | 2014-12-29 05:17:46 +0000 | [diff] [blame] | 11959 | diagnoseQualifiedDeclaration(SS, DC, OrigName, Loc)) |
| Douglas Gregor | b7d17dd | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 11960 | Invalid = true; |
| 11961 | |
| Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 11962 | New->setQualifierInfo(SS.getWithLocInContext(Context)); |
| Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 11963 | if (TemplateParameterLists.size() > 0) { |
| Douglas Gregor | 20527e2 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 11964 | New->setTemplateParameterListsInfo(Context, |
| Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 11965 | TemplateParameterLists.size(), |
| Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 11966 | TemplateParameterLists.data()); |
| Abramo Bagnara | da41d0c | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 11967 | } |
| Fariborz Jahanian | 862fac9 | 2010-05-14 21:35:02 +0000 | [diff] [blame] | 11968 | } |
| 11969 | else |
| 11970 | Invalid = true; |
| John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 11971 | } |
| 11972 | |
| Daniel Dunbar | 8804f2e | 2010-05-27 01:53:40 +0000 | [diff] [blame] | 11973 | if (RecordDecl *RD = dyn_cast<RecordDecl>(New)) { |
| 11974 | // Add alignment attributes if necessary; these attributes are checked when |
| 11975 | // the ASTContext lays out the structure. |
| Douglas Gregor | c811d8f | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 11976 | // |
| 11977 | // It is important for implementing the correct semantics that this |
| 11978 | // happen here (in act on tag decl). The #pragma pack stack is |
| 11979 | // maintained as a result of parser callbacks which can occur at |
| 11980 | // many points during the parsing of a struct declaration (because |
| 11981 | // the #pragma tokens are effectively skipped over during the |
| 11982 | // parsing of the struct). |
| Eli Friedman | 0415f3e1 | 2012-08-08 21:08:34 +0000 | [diff] [blame] | 11983 | if (TUK == TUK_Definition) { |
| 11984 | AddAlignmentAttributesForRecord(RD); |
| 11985 | AddMsStructLayoutForRecord(RD); |
| 11986 | } |
| Douglas Gregor | c811d8f | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 11987 | } |
| 11988 | |
| Douglas Gregor | 21823bf | 2011-12-20 18:11:52 +0000 | [diff] [blame] | 11989 | if (ModulePrivateLoc.isValid()) { |
| Douglas Gregor | 3c7cd6a | 2011-09-09 20:53:38 +0000 | [diff] [blame] | 11990 | if (isExplicitSpecialization) |
| 11991 | Diag(New->getLocation(), diag::err_module_private_specialization) |
| 11992 | << 2 |
| 11993 | << FixItHint::CreateRemoval(ModulePrivateLoc); |
| Douglas Gregor | 4186681 | 2011-09-12 18:37:38 +0000 | [diff] [blame] | 11994 | // __module_private__ does not apply to local classes. However, we only |
| 11995 | // diagnose this as an error when the declaration specifiers are |
| 11996 | // freestanding. Here, we just ignore the __module_private__. |
| Douglas Gregor | 4186681 | 2011-09-12 18:37:38 +0000 | [diff] [blame] | 11997 | else if (!SearchDC->isFunctionOrMethod()) |
| Douglas Gregor | 2820e69 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 11998 | New->setModulePrivate(); |
| 11999 | } |
| Serge Pavlov | a826147 | 2014-06-25 17:09:41 +0000 | [diff] [blame] | 12000 | |
| Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 12001 | // If this is a specialization of a member class (of a class template), |
| 12002 | // check the specialization. |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 12003 | if (isExplicitSpecialization && CheckMemberSpecialization(New, Previous)) |
| Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 12004 | Invalid = true; |
| Serge Pavlov | a826147 | 2014-06-25 17:09:41 +0000 | [diff] [blame] | 12005 | |
| 12006 | // If we're declaring or defining a tag in function prototype scope in C, |
| 12007 | // note that this type can only be used within the function and add it to |
| 12008 | // the list of decls to inject into the function definition scope. |
| 12009 | if ((Name || Kind == TTK_Enum) && |
| 12010 | getNonFieldDeclScope(S)->isFunctionPrototypeScope()) { |
| 12011 | if (getLangOpts().CPlusPlus) { |
| 12012 | // C++ [dcl.fct]p6: |
| 12013 | // Types shall not be defined in return or parameter types. |
| 12014 | if (TUK == TUK_Definition && !IsTypeSpecifier) { |
| 12015 | Diag(Loc, diag::err_type_defined_in_param_type) |
| 12016 | << Name; |
| 12017 | Invalid = true; |
| 12018 | } |
| 12019 | } else { |
| 12020 | Diag(Loc, diag::warn_decl_in_param_list) << Context.getTagDeclType(New); |
| 12021 | } |
| 12022 | DeclsInPrototypeScope.push_back(New); |
| 12023 | } |
| 12024 | |
| Douglas Gregor | dee1be8 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 12025 | if (Invalid) |
| 12026 | New->setInvalidDecl(); |
| 12027 | |
| Douglas Gregor | c811d8f | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 12028 | if (Attr) |
| Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 12029 | ProcessDeclAttributeList(S, New, Attr); |
| Douglas Gregor | c811d8f | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 12030 | |
| 12031 | // Set the lexical context. If the tag has a C++ scope specifier, the |
| 12032 | // lexical context will be different from the semantic context. |
| Douglas Gregor | 8761da5 | 2009-02-03 00:34:39 +0000 | [diff] [blame] | 12033 | New->setLexicalDeclContext(CurContext); |
| Douglas Gregor | dee1be8 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 12034 | |
| John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 12035 | // Mark this as a friend decl if applicable. |
| Francois Pichet | e37eeba | 2011-06-01 04:14:20 +0000 | [diff] [blame] | 12036 | // In Microsoft mode, a friend declaration also acts as a forward |
| 12037 | // declaration so we always pass true to setObjectOfFriendDecl to make |
| 12038 | // the tag name visible. |
| John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 12039 | if (TUK == TUK_Friend) |
| Reid Kleckner | 0902a51 | 2014-07-10 23:44:52 +0000 | [diff] [blame] | 12040 | New->setObjectOfFriendDecl(getLangOpts().MSVCCompat); |
| John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 12041 | |
| Anders Carlsson | 5558ca1 | 2009-03-26 01:19:02 +0000 | [diff] [blame] | 12042 | // Set the access specifier. |
| John McCall | e9eaf8e | 2010-03-25 21:28:06 +0000 | [diff] [blame] | 12043 | if (!Invalid && SearchDC->isRecord()) |
| Douglas Gregor | b8006faf | 2009-05-27 17:30:49 +0000 | [diff] [blame] | 12044 | SetMemberAccessSpecifier(New, PrevDecl, AS); |
| Douglas Gregor | 6c2adff | 2009-03-25 22:00:53 +0000 | [diff] [blame] | 12045 | |
| John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 12046 | if (TUK == TUK_Definition) |
| Douglas Gregor | dee1be8 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 12047 | New->startDefinition(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12048 | |
| Chris Lattner | 18b1962 | 2007-01-22 07:39:13 +0000 | [diff] [blame] | 12049 | // If this has an identifier, add it to the scope stack. |
| John McCall | 2dc078f | 2009-09-02 00:55:30 +0000 | [diff] [blame] | 12050 | if (TUK == TUK_Friend) { |
| John McCall | f8bd861 | 2009-09-02 19:32:14 +0000 | [diff] [blame] | 12051 | // We might be replacing an existing declaration in the lookup tables; |
| 12052 | // if so, borrow its access specifier. |
| 12053 | if (PrevDecl) |
| 12054 | New->setAccess(PrevDecl->getAccess()); |
| 12055 | |
| Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 12056 | DeclContext *DC = New->getDeclContext()->getRedeclContext(); |
| Richard Smith | 05afe5e | 2012-03-13 03:12:56 +0000 | [diff] [blame] | 12057 | DC->makeDeclVisibleInContext(New); |
| John McCall | e9eaf8e | 2010-03-25 21:28:06 +0000 | [diff] [blame] | 12058 | if (Name) // can be null along some error paths |
| John McCall | 2dc078f | 2009-09-02 00:55:30 +0000 | [diff] [blame] | 12059 | if (Scope *EnclosingScope = getScopeForDeclContext(S, DC)) |
| 12060 | PushOnScopeChains(New, EnclosingScope, /* AddToContext = */ false); |
| John McCall | 2dc078f | 2009-09-02 00:55:30 +0000 | [diff] [blame] | 12061 | } else if (Name) { |
| Douglas Gregor | 45a33ec | 2009-01-12 18:45:55 +0000 | [diff] [blame] | 12062 | S = getNonFieldDeclScope(S); |
| Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 12063 | PushOnScopeChains(New, S, !IsForwardReference); |
| 12064 | if (IsForwardReference) |
| Richard Smith | 05afe5e | 2012-03-13 03:12:56 +0000 | [diff] [blame] | 12065 | SearchDC->makeDeclVisibleInContext(New); |
| Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 12066 | |
| Douglas Gregor | c6f58fe | 2009-01-12 22:49:06 +0000 | [diff] [blame] | 12067 | } else { |
| Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 12068 | CurContext->addDecl(New); |
| Chris Lattner | 18b1962 | 2007-01-22 07:39:13 +0000 | [diff] [blame] | 12069 | } |
| Argyrios Kyrtzidis | 9e59b57 | 2008-11-09 23:41:00 +0000 | [diff] [blame] | 12070 | |
| Douglas Gregor | 27821ce | 2009-07-07 16:35:42 +0000 | [diff] [blame] | 12071 | // If this is the C FILE type, notify the AST context. |
| 12072 | if (IdentifierInfo *II = New->getIdentifier()) |
| 12073 | if (!New->isInvalidDecl() && |
| Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 12074 | New->getDeclContext()->getRedeclContext()->isTranslationUnit() && |
| Douglas Gregor | 27821ce | 2009-07-07 16:35:42 +0000 | [diff] [blame] | 12075 | II->isStr("FILE")) |
| 12076 | Context.setFILEDecl(New); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12077 | |
| Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 12078 | if (PrevDecl) |
| 12079 | mergeDeclAttributes(New, PrevDecl); |
| 12080 | |
| Rafael Espindola | e3a14bb | 2012-07-17 15:14:47 +0000 | [diff] [blame] | 12081 | // If there's a #pragma GCC visibility in scope, set the visibility of this |
| 12082 | // record. |
| 12083 | AddPushedVisibilityAttribute(New); |
| 12084 | |
| Douglas Gregor | d6ab874 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 12085 | OwnedDecl = true; |
| Richard Smith | 3e28469 | 2012-12-05 11:34:06 +0000 | [diff] [blame] | 12086 | // In C++, don't return an invalid declaration. We can't recover well from |
| 12087 | // the cases where we make the type anonymous. |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 12088 | return (Invalid && getLangOpts().CPlusPlus) ? nullptr : New; |
| Chris Lattner | 18b1962 | 2007-01-22 07:39:13 +0000 | [diff] [blame] | 12089 | } |
| Chris Lattner | 1300fb9 | 2007-01-23 23:42:53 +0000 | [diff] [blame] | 12090 | |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 12091 | void Sema::ActOnTagStartDefinition(Scope *S, Decl *TagD) { |
| Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 12092 | AdjustDeclIfTemplate(TagD); |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 12093 | TagDecl *Tag = cast<TagDecl>(TagD); |
| Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 12094 | |
| Douglas Gregor | 82ac25e | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 12095 | // Enter the tag context. |
| 12096 | PushDeclContext(S, Tag); |
| Dmitri Gribenko | f26054f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 12097 | |
| 12098 | ActOnDocumentableDecl(TagD); |
| Rafael Espindola | 4dedd0c | 2012-07-12 04:47:34 +0000 | [diff] [blame] | 12099 | |
| 12100 | // If there's a #pragma GCC visibility in scope, set the visibility of this |
| 12101 | // record. |
| 12102 | AddPushedVisibilityAttribute(Tag); |
| John McCall | 1c7e6ec | 2009-12-20 07:58:13 +0000 | [diff] [blame] | 12103 | } |
| Douglas Gregor | 82ac25e | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 12104 | |
| Argyrios Kyrtzidis | 9321ad3 | 2011-10-06 23:23:20 +0000 | [diff] [blame] | 12105 | Decl *Sema::ActOnObjCContainerStartDefinition(Decl *IDecl) { |
| Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 12106 | assert(isa<ObjCContainerDecl>(IDecl) && |
| 12107 | "ActOnObjCContainerStartDefinition - Not ObjCContainerDecl"); |
| 12108 | DeclContext *OCD = cast<DeclContext>(IDecl); |
| 12109 | assert(getContainingDC(OCD) == CurContext && |
| 12110 | "The next DeclContext should be lexically contained in the current one."); |
| 12111 | CurContext = OCD; |
| Argyrios Kyrtzidis | 9321ad3 | 2011-10-06 23:23:20 +0000 | [diff] [blame] | 12112 | return IDecl; |
| Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 12113 | } |
| 12114 | |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 12115 | void Sema::ActOnStartCXXMemberDeclarations(Scope *S, Decl *TagD, |
| Anders Carlsson | 30f2944 | 2011-03-25 14:31:08 +0000 | [diff] [blame] | 12116 | SourceLocation FinalLoc, |
| David Majnemer | a543308 | 2013-10-18 00:33:31 +0000 | [diff] [blame] | 12117 | bool IsFinalSpelledSealed, |
| John McCall | 1c7e6ec | 2009-12-20 07:58:13 +0000 | [diff] [blame] | 12118 | SourceLocation LBraceLoc) { |
| 12119 | AdjustDeclIfTemplate(TagD); |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 12120 | CXXRecordDecl *Record = cast<CXXRecordDecl>(TagD); |
| Douglas Gregor | 82ac25e | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 12121 | |
| John McCall | 1c7e6ec | 2009-12-20 07:58:13 +0000 | [diff] [blame] | 12122 | FieldCollector->StartClass(); |
| 12123 | |
| 12124 | if (!Record->getIdentifier()) |
| 12125 | return; |
| 12126 | |
| Anders Carlsson | 30f2944 | 2011-03-25 14:31:08 +0000 | [diff] [blame] | 12127 | if (FinalLoc.isValid()) |
| David Majnemer | a543308 | 2013-10-18 00:33:31 +0000 | [diff] [blame] | 12128 | Record->addAttr(new (Context) |
| 12129 | FinalAttr(FinalLoc, Context, IsFinalSpelledSealed)); |
| 12130 | |
| John McCall | 1c7e6ec | 2009-12-20 07:58:13 +0000 | [diff] [blame] | 12131 | // C++ [class]p2: |
| 12132 | // [...] The class-name is also inserted into the scope of the |
| 12133 | // class itself; this is known as the injected-class-name. For |
| 12134 | // purposes of access checking, the injected-class-name is treated |
| 12135 | // as if it were a public member name. |
| 12136 | CXXRecordDecl *InjectedClassName |
| Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 12137 | = CXXRecordDecl::Create(Context, Record->getTagKind(), CurContext, |
| 12138 | Record->getLocStart(), Record->getLocation(), |
| John McCall | 1c7e6ec | 2009-12-20 07:58:13 +0000 | [diff] [blame] | 12139 | Record->getIdentifier(), |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 12140 | /*PrevDecl=*/nullptr, |
| Argyrios Kyrtzidis | 470c454 | 2010-10-14 20:14:21 +0000 | [diff] [blame] | 12141 | /*DelayTypeCreation=*/true); |
| 12142 | Context.getTypeDeclType(InjectedClassName, Record); |
| John McCall | 1c7e6ec | 2009-12-20 07:58:13 +0000 | [diff] [blame] | 12143 | InjectedClassName->setImplicit(); |
| 12144 | InjectedClassName->setAccess(AS_public); |
| 12145 | if (ClassTemplateDecl *Template = Record->getDescribedClassTemplate()) |
| 12146 | InjectedClassName->setDescribedClassTemplate(Template); |
| 12147 | PushOnScopeChains(InjectedClassName, S); |
| 12148 | assert(InjectedClassName->isInjectedClassName() && |
| 12149 | "Broken injected-class-name"); |
| Douglas Gregor | 82ac25e | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 12150 | } |
| 12151 | |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 12152 | void Sema::ActOnTagFinishDefinition(Scope *S, Decl *TagD, |
| Argyrios Kyrtzidis | 23e1f1d | 2009-07-14 03:17:52 +0000 | [diff] [blame] | 12153 | SourceLocation RBraceLoc) { |
| Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 12154 | AdjustDeclIfTemplate(TagD); |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 12155 | TagDecl *Tag = cast<TagDecl>(TagD); |
| Argyrios Kyrtzidis | 23e1f1d | 2009-07-14 03:17:52 +0000 | [diff] [blame] | 12156 | Tag->setRBraceLoc(RBraceLoc); |
| Douglas Gregor | 82ac25e | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 12157 | |
| Argyrios Kyrtzidis | 2559629 | 2012-03-09 20:10:30 +0000 | [diff] [blame] | 12158 | // Make sure we "complete" the definition even it is invalid. |
| 12159 | if (Tag->isBeingDefined()) { |
| 12160 | assert(Tag->isInvalidDecl() && "We should already have completed it"); |
| 12161 | if (RecordDecl *RD = dyn_cast<RecordDecl>(Tag)) |
| 12162 | RD->completeDefinition(); |
| 12163 | } |
| 12164 | |
| Reid Kleckner | bba3cb9 | 2015-03-17 19:00:50 +0000 | [diff] [blame] | 12165 | if (isa<CXXRecordDecl>(Tag)) |
| Douglas Gregor | 82ac25e | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 12166 | FieldCollector->FinishClass(); |
| 12167 | |
| 12168 | // Exit this scope of this tag's definition. |
| 12169 | PopDeclContext(); |
| Argyrios Kyrtzidis | c821f73 | 2013-01-29 18:00:54 +0000 | [diff] [blame] | 12170 | |
| 12171 | if (getCurLexicalContext()->isObjCContainer() && |
| 12172 | Tag->getDeclContext()->isFileContext()) |
| 12173 | Tag->setTopLevelDeclInObjCContainer(); |
| 12174 | |
| Douglas Gregor | 82ac25e | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 12175 | // Notify the consumer that we've defined a tag. |
| Serge Pavlov | fb73ec8 | 2013-07-02 17:31:56 +0000 | [diff] [blame] | 12176 | if (!Tag->isInvalidDecl()) |
| 12177 | Consumer.HandleTagDeclDefinition(Tag); |
| Douglas Gregor | 82ac25e | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 12178 | } |
| Chris Lattner | 535b830 | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 12179 | |
| Fariborz Jahanian | 4327b32 | 2011-08-29 17:33:12 +0000 | [diff] [blame] | 12180 | void Sema::ActOnObjCContainerFinishDefinition() { |
| Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 12181 | // Exit this scope of this interface definition. |
| 12182 | PopDeclContext(); |
| 12183 | } |
| Argyrios Kyrtzidis | 9321ad3 | 2011-10-06 23:23:20 +0000 | [diff] [blame] | 12184 | |
| Argyrios Kyrtzidis | a9aabf7 | 2011-10-27 00:09:34 +0000 | [diff] [blame] | 12185 | void Sema::ActOnObjCTemporaryExitContainerContext(DeclContext *DC) { |
| Argyrios Kyrtzidis | 67f914d | 2011-10-27 00:53:06 +0000 | [diff] [blame] | 12186 | assert(DC == CurContext && "Mismatch of container contexts"); |
| 12187 | OriginalLexicalContext = DC; |
| Argyrios Kyrtzidis | 9321ad3 | 2011-10-06 23:23:20 +0000 | [diff] [blame] | 12188 | ActOnObjCContainerFinishDefinition(); |
| 12189 | } |
| 12190 | |
| Argyrios Kyrtzidis | a9aabf7 | 2011-10-27 00:09:34 +0000 | [diff] [blame] | 12191 | void Sema::ActOnObjCReenterContainerContext(DeclContext *DC) { |
| 12192 | ActOnObjCContainerStartDefinition(cast<Decl>(DC)); |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 12193 | OriginalLexicalContext = nullptr; |
| Argyrios Kyrtzidis | 9321ad3 | 2011-10-06 23:23:20 +0000 | [diff] [blame] | 12194 | } |
| 12195 | |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 12196 | void Sema::ActOnTagDefinitionError(Scope *S, Decl *TagD) { |
| John McCall | 2ff380a | 2010-03-17 00:38:33 +0000 | [diff] [blame] | 12197 | AdjustDeclIfTemplate(TagD); |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 12198 | TagDecl *Tag = cast<TagDecl>(TagD); |
| John McCall | 2ff380a | 2010-03-17 00:38:33 +0000 | [diff] [blame] | 12199 | Tag->setInvalidDecl(); |
| 12200 | |
| Argyrios Kyrtzidis | 2559629 | 2012-03-09 20:10:30 +0000 | [diff] [blame] | 12201 | // Make sure we "complete" the definition even it is invalid. |
| 12202 | if (Tag->isBeingDefined()) { |
| 12203 | if (RecordDecl *RD = dyn_cast<RecordDecl>(Tag)) |
| 12204 | RD->completeDefinition(); |
| 12205 | } |
| 12206 | |
| John McCall | 71ba5f2 | 2010-03-17 19:25:57 +0000 | [diff] [blame] | 12207 | // We're undoing ActOnTagStartDefinition here, not |
| 12208 | // ActOnStartCXXMemberDeclarations, so we don't have to mess with |
| 12209 | // the FieldCollector. |
| John McCall | 2ff380a | 2010-03-17 00:38:33 +0000 | [diff] [blame] | 12210 | |
| 12211 | PopDeclContext(); |
| 12212 | } |
| 12213 | |
| Chris Lattner | f9b00eb | 2009-04-20 17:29:38 +0000 | [diff] [blame] | 12214 | // Note that FieldName may be null for anonymous bitfields. |
| Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 12215 | ExprResult Sema::VerifyBitField(SourceLocation FieldLoc, |
| 12216 | IdentifierInfo *FieldName, |
| Reid Kleckner | 736bc98 | 2013-07-17 20:46:03 +0000 | [diff] [blame] | 12217 | QualType FieldTy, bool IsMsStruct, |
| 12218 | Expr *BitWidth, bool *ZeroWidth) { |
| Eli Friedman | c96d496 | 2009-08-15 21:55:26 +0000 | [diff] [blame] | 12219 | // Default to true; that shouldn't confuse checks for emptiness |
| 12220 | if (ZeroWidth) |
| 12221 | *ZeroWidth = true; |
| 12222 | |
| Chris Lattner | 73bf7b4 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 12223 | // C99 6.7.2.1p4 - verify the field type. |
| Chris Lattner | d26760a | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 12224 | // C++ 9.6p3: A bit-field shall have integral or enumeration type. |
| Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 12225 | if (!FieldTy->isDependentType() && !FieldTy->isIntegralOrEnumerationType()) { |
| Chris Lattner | 73bf7b4 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 12226 | // Handle incomplete types with specific error. |
| Douglas Gregor | 8145742 | 2009-03-10 21:58:27 +0000 | [diff] [blame] | 12227 | if (RequireCompleteType(FieldLoc, FieldTy, diag::err_field_incomplete)) |
| Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 12228 | return ExprError(); |
| Chris Lattner | f9b00eb | 2009-04-20 17:29:38 +0000 | [diff] [blame] | 12229 | if (FieldName) |
| 12230 | return Diag(FieldLoc, diag::err_not_integral_type_bitfield) |
| 12231 | << FieldName << FieldTy << BitWidth->getSourceRange(); |
| 12232 | return Diag(FieldLoc, diag::err_not_integral_type_anon_bitfield) |
| 12233 | << FieldTy << BitWidth->getSourceRange(); |
| Douglas Gregor | a02a72a | 2010-12-15 23:18:36 +0000 | [diff] [blame] | 12234 | } else if (DiagnoseUnexpandedParameterPack(const_cast<Expr *>(BitWidth), |
| 12235 | UPPC_BitFieldWidth)) |
| Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 12236 | return ExprError(); |
| Douglas Gregor | 1efa437 | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 12237 | |
| 12238 | // If the bit-width is type- or value-dependent, don't try to check |
| 12239 | // it now. |
| 12240 | if (BitWidth->isValueDependent() || BitWidth->isTypeDependent()) |
| Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 12241 | return BitWidth; |
| Douglas Gregor | 1efa437 | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 12242 | |
| Anders Carlsson | 5df391e | 2008-12-06 20:33:04 +0000 | [diff] [blame] | 12243 | llvm::APSInt Value; |
| Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 12244 | ExprResult ICE = VerifyIntegerConstantExpression(BitWidth, &Value); |
| 12245 | if (ICE.isInvalid()) |
| 12246 | return ICE; |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 12247 | BitWidth = ICE.get(); |
| Anders Carlsson | 5df391e | 2008-12-06 20:33:04 +0000 | [diff] [blame] | 12248 | |
| Eli Friedman | c96d496 | 2009-08-15 21:55:26 +0000 | [diff] [blame] | 12249 | if (Value != 0 && ZeroWidth) |
| 12250 | *ZeroWidth = false; |
| 12251 | |
| Chris Lattner | 81ed680 | 2008-12-12 04:56:04 +0000 | [diff] [blame] | 12252 | // Zero-width bitfield is ok for anonymous field. |
| 12253 | if (Value == 0 && FieldName) |
| 12254 | return Diag(FieldLoc, diag::err_bitfield_has_zero_width) << FieldName; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12255 | |
| Chris Lattner | f9b00eb | 2009-04-20 17:29:38 +0000 | [diff] [blame] | 12256 | if (Value.isSigned() && Value.isNegative()) { |
| 12257 | if (FieldName) |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12258 | return Diag(FieldLoc, diag::err_bitfield_has_negative_width) |
| Chris Lattner | f9b00eb | 2009-04-20 17:29:38 +0000 | [diff] [blame] | 12259 | << FieldName << Value.toString(10); |
| 12260 | return Diag(FieldLoc, diag::err_anon_bitfield_has_negative_width) |
| 12261 | << Value.toString(10); |
| 12262 | } |
| Anders Carlsson | 5df391e | 2008-12-06 20:33:04 +0000 | [diff] [blame] | 12263 | |
| Douglas Gregor | 1efa437 | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 12264 | if (!FieldTy->isDependentType()) { |
| 12265 | uint64_t TypeSize = Context.getTypeSize(FieldTy); |
| Chris Lattner | f9b00eb | 2009-04-20 17:29:38 +0000 | [diff] [blame] | 12266 | if (Value.getZExtValue() > TypeSize) { |
| Warren Hunt | 96afec1 | 2013-12-12 23:23:28 +0000 | [diff] [blame] | 12267 | if (!getLangOpts().CPlusPlus || IsMsStruct || |
| 12268 | Context.getTargetInfo().getCXXABI().isMicrosoft()) { |
| Anders Carlsson | d5635fe | 2010-04-16 15:16:32 +0000 | [diff] [blame] | 12269 | if (FieldName) |
| 12270 | return Diag(FieldLoc, diag::err_bitfield_width_exceeds_type_size) |
| 12271 | << FieldName << (unsigned)Value.getZExtValue() |
| 12272 | << (unsigned)TypeSize; |
| 12273 | |
| 12274 | return Diag(FieldLoc, diag::err_anon_bitfield_width_exceeds_type_size) |
| 12275 | << (unsigned)Value.getZExtValue() << (unsigned)TypeSize; |
| 12276 | } |
| 12277 | |
| Chris Lattner | f9b00eb | 2009-04-20 17:29:38 +0000 | [diff] [blame] | 12278 | if (FieldName) |
| Anders Carlsson | d5635fe | 2010-04-16 15:16:32 +0000 | [diff] [blame] | 12279 | Diag(FieldLoc, diag::warn_bitfield_width_exceeds_type_size) |
| 12280 | << FieldName << (unsigned)Value.getZExtValue() |
| 12281 | << (unsigned)TypeSize; |
| 12282 | else |
| 12283 | Diag(FieldLoc, diag::warn_anon_bitfield_width_exceeds_type_size) |
| 12284 | << (unsigned)Value.getZExtValue() << (unsigned)TypeSize; |
| Chris Lattner | f9b00eb | 2009-04-20 17:29:38 +0000 | [diff] [blame] | 12285 | } |
| Douglas Gregor | 1efa437 | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 12286 | } |
| Anders Carlsson | 5df391e | 2008-12-06 20:33:04 +0000 | [diff] [blame] | 12287 | |
| Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 12288 | return BitWidth; |
| Anders Carlsson | 5df391e | 2008-12-06 20:33:04 +0000 | [diff] [blame] | 12289 | } |
| 12290 | |
| Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 12291 | /// 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] | 12292 | /// to create a FieldDecl object for it. |
| Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 12293 | Decl *Sema::ActOnField(Scope *S, Decl *TagD, SourceLocation DeclStart, |
| Richard Trieu | 2bd0401 | 2011-09-09 02:00:50 +0000 | [diff] [blame] | 12294 | Declarator &D, Expr *BitfieldWidth) { |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 12295 | FieldDecl *Res = HandleField(S, cast_or_null<RecordDecl>(TagD), |
| Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 12296 | DeclStart, D, static_cast<Expr*>(BitfieldWidth), |
| Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 12297 | /*InitStyle=*/ICIS_NoInit, AS_public); |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 12298 | return Res; |
| Chris Lattner | 73bf7b4 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 12299 | } |
| 12300 | |
| 12301 | /// HandleField - Analyze a field of a C struct or a C++ data member. |
| 12302 | /// |
| 12303 | FieldDecl *Sema::HandleField(Scope *S, RecordDecl *Record, |
| 12304 | SourceLocation DeclStart, |
| Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 12305 | Declarator &D, Expr *BitWidth, |
| 12306 | InClassInitStyle InitStyle, |
| Douglas Gregor | 4261e4c | 2009-03-11 20:50:30 +0000 | [diff] [blame] | 12307 | AccessSpecifier AS) { |
| Chris Lattner | 1300fb9 | 2007-01-23 23:42:53 +0000 | [diff] [blame] | 12308 | IdentifierInfo *II = D.getIdentifier(); |
| Chris Lattner | 1300fb9 | 2007-01-23 23:42:53 +0000 | [diff] [blame] | 12309 | SourceLocation Loc = DeclStart; |
| 12310 | if (II) Loc = D.getIdentifierLoc(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12311 | |
| John McCall | 8cb7bdf | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 12312 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
| 12313 | QualType T = TInfo->getType(); |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 12314 | if (getLangOpts().CPlusPlus) { |
| Douglas Gregor | 1efa437 | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 12315 | CheckExtraCXXDefaultArguments(D); |
| Douglas Gregor | 0c88030 | 2009-03-11 23:00:04 +0000 | [diff] [blame] | 12316 | |
| Douglas Gregor | a02a72a | 2010-12-15 23:18:36 +0000 | [diff] [blame] | 12317 | if (DiagnoseUnexpandedParameterPack(D.getIdentifierLoc(), TInfo, |
| 12318 | UPPC_DataMemberType)) { |
| 12319 | D.setInvalidType(); |
| 12320 | T = Context.IntTy; |
| 12321 | TInfo = Context.getTrivialTypeSourceInfo(T, Loc); |
| 12322 | } |
| 12323 | } |
| 12324 | |
| Matt Arsenault | 376f720 | 2013-02-26 21:16:00 +0000 | [diff] [blame] | 12325 | // TR 18037 does not allow fields to be declared with address spaces. |
| 12326 | if (T.getQualifiers().hasAddressSpace()) { |
| 12327 | Diag(Loc, diag::err_field_with_address_space); |
| 12328 | D.setInvalidType(); |
| 12329 | } |
| 12330 | |
| Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 12331 | // OpenCL 1.2 spec, s6.9 r: |
| 12332 | // The event type cannot be used to declare a structure or union field. |
| 12333 | if (LangOpts.OpenCL && T->isEventT()) { |
| 12334 | Diag(Loc, diag::err_event_t_struct_field); |
| 12335 | D.setInvalidType(); |
| 12336 | } |
| 12337 | |
| Richard Smith | b1402ae | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 12338 | DiagnoseFunctionSpecifiers(D.getDeclSpec()); |
| Eli Friedman | 574c745 | 2009-04-07 19:37:57 +0000 | [diff] [blame] | 12339 | |
| Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 12340 | if (DeclSpec::TSCS TSCS = D.getDeclSpec().getThreadStorageClassSpec()) |
| 12341 | Diag(D.getDeclSpec().getThreadStorageClassSpecLoc(), |
| 12342 | diag::err_invalid_thread) |
| 12343 | << DeclSpec::getSpecifierName(TSCS); |
| Matt Arsenault | 376f720 | 2013-02-26 21:16:00 +0000 | [diff] [blame] | 12344 | |
| Douglas Gregor | 2c7d929 | 2010-08-30 14:32:14 +0000 | [diff] [blame] | 12345 | // Check to see if this name was declared as a member previously |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 12346 | NamedDecl *PrevDecl = nullptr; |
| Douglas Gregor | 2c7d929 | 2010-08-30 14:32:14 +0000 | [diff] [blame] | 12347 | LookupResult Previous(*this, II, Loc, LookupMemberName, ForRedeclaration); |
| 12348 | LookupName(Previous, S); |
| Douglas Gregor | fbf8752 | 2011-10-21 15:47:52 +0000 | [diff] [blame] | 12349 | switch (Previous.getResultKind()) { |
| 12350 | case LookupResult::Found: |
| 12351 | case LookupResult::FoundUnresolvedValue: |
| 12352 | PrevDecl = Previous.getAsSingle<NamedDecl>(); |
| 12353 | break; |
| 12354 | |
| 12355 | case LookupResult::FoundOverloaded: |
| 12356 | PrevDecl = Previous.getRepresentativeDecl(); |
| 12357 | break; |
| 12358 | |
| 12359 | case LookupResult::NotFound: |
| 12360 | case LookupResult::NotFoundInCurrentInstantiation: |
| 12361 | case LookupResult::Ambiguous: |
| 12362 | break; |
| 12363 | } |
| 12364 | Previous.suppressDiagnostics(); |
| Douglas Gregor | f187420f | 2009-06-17 23:37:01 +0000 | [diff] [blame] | 12365 | |
| 12366 | if (PrevDecl && PrevDecl->isTemplateParameter()) { |
| 12367 | // Maybe we will complain about the shadowed template parameter. |
| 12368 | DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), PrevDecl); |
| 12369 | // Just pretend that we didn't see the previous declaration. |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 12370 | PrevDecl = nullptr; |
| Douglas Gregor | f187420f | 2009-06-17 23:37:01 +0000 | [diff] [blame] | 12371 | } |
| 12372 | |
| Douglas Gregor | 1efa437 | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 12373 | if (PrevDecl && !isDeclInScope(PrevDecl, Record, S)) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 12374 | PrevDecl = nullptr; |
| Douglas Gregor | 1efa437 | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 12375 | |
| Steve Naroff | 5ec6ff7 | 2009-07-14 14:58:18 +0000 | [diff] [blame] | 12376 | bool Mutable |
| 12377 | = (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_mutable); |
| Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 12378 | SourceLocation TSSL = D.getLocStart(); |
| Steve Naroff | 5ec6ff7 | 2009-07-14 14:58:18 +0000 | [diff] [blame] | 12379 | FieldDecl *NewFD |
| Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 12380 | = CheckFieldDecl(II, T, TInfo, Record, Loc, Mutable, BitWidth, InitStyle, |
| Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 12381 | TSSL, AS, PrevDecl, &D); |
| Rafael Espindola | 568586f | 2010-03-21 22:56:43 +0000 | [diff] [blame] | 12382 | |
| 12383 | if (NewFD->isInvalidDecl()) |
| 12384 | Record->setInvalidDecl(); |
| 12385 | |
| Douglas Gregor | 3baa670 | 2011-09-12 16:11:24 +0000 | [diff] [blame] | 12386 | if (D.getDeclSpec().isModulePrivateSpecified()) |
| 12387 | NewFD->setModulePrivate(); |
| 12388 | |
| Douglas Gregor | 1efa437 | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 12389 | if (NewFD->isInvalidDecl() && PrevDecl) { |
| 12390 | // Don't introduce NewFD into scope; there's already something |
| 12391 | // with the same name in the same scope. |
| 12392 | } else if (II) { |
| 12393 | PushOnScopeChains(NewFD, S); |
| 12394 | } else |
| Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 12395 | Record->addDecl(NewFD); |
| Douglas Gregor | 1efa437 | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 12396 | |
| 12397 | return NewFD; |
| 12398 | } |
| 12399 | |
| 12400 | /// \brief Build a new FieldDecl and check its well-formedness. |
| 12401 | /// |
| 12402 | /// This routine builds a new FieldDecl given the fields name, type, |
| 12403 | /// record, etc. \p PrevDecl should refer to any previous declaration |
| 12404 | /// with the same name and in the same scope as the field to be |
| 12405 | /// created. |
| 12406 | /// |
| 12407 | /// \returns a new FieldDecl. |
| 12408 | /// |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12409 | /// \todo The Declarator argument is a hack. It will be removed once |
| Argyrios Kyrtzidis | 60ed560 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 12410 | FieldDecl *Sema::CheckFieldDecl(DeclarationName Name, QualType T, |
| John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 12411 | TypeSourceInfo *TInfo, |
| Douglas Gregor | 1efa437 | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 12412 | RecordDecl *Record, SourceLocation Loc, |
| Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 12413 | bool Mutable, Expr *BitWidth, |
| 12414 | InClassInitStyle InitStyle, |
| Steve Naroff | 5ec6ff7 | 2009-07-14 14:58:18 +0000 | [diff] [blame] | 12415 | SourceLocation TSSL, |
| Douglas Gregor | 4261e4c | 2009-03-11 20:50:30 +0000 | [diff] [blame] | 12416 | AccessSpecifier AS, NamedDecl *PrevDecl, |
| Douglas Gregor | 1efa437 | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 12417 | Declarator *D) { |
| 12418 | IdentifierInfo *II = Name.getAsIdentifierInfo(); |
| Steve Naroff | f93b672 | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 12419 | bool InvalidDecl = false; |
| Chris Lattner | f6d1c9c | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 12420 | if (D) InvalidDecl = D->isInvalidType(); |
| Sebastian Redl | baad4e7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 12421 | |
| Douglas Gregor | 1efa437 | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 12422 | // If we receive a broken type, recover by assuming 'int' and |
| 12423 | // marking this declaration as invalid. |
| 12424 | if (T.isNull()) { |
| 12425 | InvalidDecl = true; |
| 12426 | T = Context.IntTy; |
| 12427 | } |
| 12428 | |
| Eli Friedman | d0e8de2 | 2009-12-07 00:22:08 +0000 | [diff] [blame] | 12429 | QualType EltTy = Context.getBaseElementType(T); |
| Argyrios Kyrtzidis | 2559629 | 2012-03-09 20:10:30 +0000 | [diff] [blame] | 12430 | if (!EltTy->isDependentType()) { |
| 12431 | if (RequireCompleteType(Loc, EltTy, diag::err_field_incomplete)) { |
| 12432 | // Fields of incomplete type force their record to be invalid. |
| 12433 | Record->setInvalidDecl(); |
| 12434 | InvalidDecl = true; |
| 12435 | } else { |
| 12436 | NamedDecl *Def; |
| 12437 | EltTy->isIncompleteType(&Def); |
| 12438 | if (Def && Def->isInvalidDecl()) { |
| 12439 | Record->setInvalidDecl(); |
| 12440 | InvalidDecl = true; |
| 12441 | } |
| 12442 | } |
| John McCall | 2677e10 | 2010-08-16 23:42:35 +0000 | [diff] [blame] | 12443 | } |
| Eli Friedman | d0e8de2 | 2009-12-07 00:22:08 +0000 | [diff] [blame] | 12444 | |
| Joey Gouly | 1d58cdb | 2013-01-17 17:35:00 +0000 | [diff] [blame] | 12445 | // OpenCL v1.2 s6.9.c: bitfields are not supported. |
| 12446 | if (BitWidth && getLangOpts().OpenCL) { |
| 12447 | Diag(Loc, diag::err_opencl_bitfields); |
| 12448 | InvalidDecl = true; |
| 12449 | } |
| 12450 | |
| Steve Naroff | 8eeeb13 | 2007-05-08 21:09:37 +0000 | [diff] [blame] | 12451 | // C99 6.7.2.1p8: A member of a structure or union may have any type other |
| 12452 | // than a variably modified type. |
| Eli Friedman | d0e8de2 | 2009-12-07 00:22:08 +0000 | [diff] [blame] | 12453 | if (!InvalidDecl && T->isVariablyModifiedType()) { |
| Eli Friedman | a3b1d03 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 12454 | bool SizeIsNegative; |
| Douglas Gregor | caa1bf4 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 12455 | llvm::APSInt Oversized; |
| Abramo Bagnara | 341ab73 | 2012-11-08 14:44:42 +0000 | [diff] [blame] | 12456 | |
| 12457 | TypeSourceInfo *FixedTInfo = |
| 12458 | TryToFixInvalidVariablyModifiedTypeSourceInfo(TInfo, Context, |
| 12459 | SizeIsNegative, |
| 12460 | Oversized); |
| 12461 | if (FixedTInfo) { |
| Eli Friedman | a3b1d03 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 12462 | Diag(Loc, diag::warn_illegal_constant_array_size); |
| Abramo Bagnara | 341ab73 | 2012-11-08 14:44:42 +0000 | [diff] [blame] | 12463 | TInfo = FixedTInfo; |
| 12464 | T = FixedTInfo->getType(); |
| Eli Friedman | a3b1d03 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 12465 | } else { |
| 12466 | if (SizeIsNegative) |
| 12467 | Diag(Loc, diag::err_typecheck_negative_array_size); |
| Douglas Gregor | caa1bf4 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 12468 | else if (Oversized.getBoolValue()) |
| 12469 | Diag(Loc, diag::err_array_too_large) |
| 12470 | << Oversized.toString(10); |
| Eli Friedman | a3b1d03 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 12471 | else |
| 12472 | Diag(Loc, diag::err_typecheck_field_variable_size); |
| Eli Friedman | a3b1d03 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 12473 | InvalidDecl = true; |
| 12474 | } |
| Steve Naroff | 8eeeb13 | 2007-05-08 21:09:37 +0000 | [diff] [blame] | 12475 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12476 | |
| Anders Carlsson | 576cc6f | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 12477 | // Fields can not have abstract class types |
| Eli Friedman | d0e8de2 | 2009-12-07 00:22:08 +0000 | [diff] [blame] | 12478 | if (!InvalidDecl && RequireNonAbstractType(Loc, T, |
| 12479 | diag::err_abstract_type_in_decl, |
| 12480 | AbstractFieldType)) |
| Anders Carlsson | 576cc6f | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 12481 | InvalidDecl = true; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12482 | |
| Eli Friedman | c96d496 | 2009-08-15 21:55:26 +0000 | [diff] [blame] | 12483 | bool ZeroWidth = false; |
| Douglas Gregor | 1efa437 | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 12484 | // If this is declared as a bit-field, check the bit-field. |
| Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 12485 | if (!InvalidDecl && BitWidth) { |
| Reid Kleckner | 736bc98 | 2013-07-17 20:46:03 +0000 | [diff] [blame] | 12486 | BitWidth = VerifyBitField(Loc, II, T, Record->isMsStruct(Context), BitWidth, |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 12487 | &ZeroWidth).get(); |
| Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 12488 | if (!BitWidth) { |
| 12489 | InvalidDecl = true; |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 12490 | BitWidth = nullptr; |
| Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 12491 | ZeroWidth = false; |
| 12492 | } |
| Anders Carlsson | 5df391e | 2008-12-06 20:33:04 +0000 | [diff] [blame] | 12493 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12494 | |
| John McCall | b1cd7da | 2010-06-04 08:34:12 +0000 | [diff] [blame] | 12495 | // Check that 'mutable' is consistent with the type of the declaration. |
| 12496 | if (!InvalidDecl && Mutable) { |
| 12497 | unsigned DiagID = 0; |
| 12498 | if (T->isReferenceType()) |
| Alexey Bataev | 8f01bb9 | 2015-02-04 04:45:32 +0000 | [diff] [blame] | 12499 | DiagID = getLangOpts().MSVCCompat ? diag::ext_mutable_reference |
| 12500 | : diag::err_mutable_reference; |
| John McCall | b1cd7da | 2010-06-04 08:34:12 +0000 | [diff] [blame] | 12501 | else if (T.isConstQualified()) |
| 12502 | DiagID = diag::err_mutable_const; |
| 12503 | |
| 12504 | if (DiagID) { |
| 12505 | SourceLocation ErrLoc = Loc; |
| 12506 | if (D && D->getDeclSpec().getStorageClassSpecLoc().isValid()) |
| 12507 | ErrLoc = D->getDeclSpec().getStorageClassSpecLoc(); |
| 12508 | Diag(ErrLoc, DiagID); |
| Alexey Bataev | 8f01bb9 | 2015-02-04 04:45:32 +0000 | [diff] [blame] | 12509 | if (DiagID != diag::ext_mutable_reference) { |
| 12510 | Mutable = false; |
| 12511 | InvalidDecl = true; |
| 12512 | } |
| John McCall | b1cd7da | 2010-06-04 08:34:12 +0000 | [diff] [blame] | 12513 | } |
| 12514 | } |
| 12515 | |
| Richard Smith | ab44d5b | 2013-12-10 08:25:00 +0000 | [diff] [blame] | 12516 | // C++11 [class.union]p8 (DR1460): |
| 12517 | // At most one variant member of a union may have a |
| 12518 | // brace-or-equal-initializer. |
| 12519 | if (InitStyle != ICIS_NoInit) |
| 12520 | checkDuplicateDefaultInit(*this, cast<CXXRecordDecl>(Record), Loc); |
| 12521 | |
| Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 12522 | FieldDecl *NewFD = FieldDecl::Create(Context, Record, TSSL, Loc, II, T, TInfo, |
| Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 12523 | BitWidth, Mutable, InitStyle); |
| Chris Lattner | f6d1c9c | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 12524 | if (InvalidDecl) |
| 12525 | NewFD->setInvalidDecl(); |
| Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 12526 | |
| Douglas Gregor | 1efa437 | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 12527 | if (PrevDecl && !isa<TagDecl>(PrevDecl)) { |
| 12528 | Diag(Loc, diag::err_duplicate_member) << II; |
| 12529 | Diag(PrevDecl->getLocation(), diag::note_previous_declaration); |
| 12530 | NewFD->setInvalidDecl(); |
| Douglas Gregor | 82ac25e | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 12531 | } |
| 12532 | |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 12533 | if (!InvalidDecl && getLangOpts().CPlusPlus) { |
| Anders Carlsson | 2ceb347 | 2010-11-07 19:13:55 +0000 | [diff] [blame] | 12534 | if (Record->isUnion()) { |
| 12535 | if (const RecordType *RT = EltTy->getAs<RecordType>()) { |
| 12536 | CXXRecordDecl* RDecl = cast<CXXRecordDecl>(RT->getDecl()); |
| 12537 | if (RDecl->getDefinition()) { |
| 12538 | // C++ [class.union]p1: An object of a class with a non-trivial |
| 12539 | // constructor, a non-trivial copy constructor, a non-trivial |
| 12540 | // destructor, or a non-trivial copy assignment operator |
| 12541 | // cannot be a member of a union, nor can an array of such |
| 12542 | // objects. |
| Richard Smith | f720df0 | 2011-10-19 20:41:51 +0000 | [diff] [blame] | 12543 | if (CheckNontrivialField(NewFD)) |
| Anders Carlsson | 2ceb347 | 2010-11-07 19:13:55 +0000 | [diff] [blame] | 12544 | NewFD->setInvalidDecl(); |
| 12545 | } |
| 12546 | } |
| 12547 | |
| 12548 | // 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] | 12549 | // the program is ill-formed, except when compiling with MSVC extensions |
| 12550 | // enabled. |
| Anders Carlsson | 2ceb347 | 2010-11-07 19:13:55 +0000 | [diff] [blame] | 12551 | if (EltTy->isReferenceType()) { |
| Aaron Ballman | ed0ae1d | 2013-05-30 16:20:00 +0000 | [diff] [blame] | 12552 | Diag(NewFD->getLocation(), getLangOpts().MicrosoftExt ? |
| 12553 | diag::ext_union_member_of_reference_type : |
| 12554 | diag::err_union_member_of_reference_type) |
| Anders Carlsson | 2ceb347 | 2010-11-07 19:13:55 +0000 | [diff] [blame] | 12555 | << NewFD->getDeclName() << EltTy; |
| Aaron Ballman | ed0ae1d | 2013-05-30 16:20:00 +0000 | [diff] [blame] | 12556 | if (!getLangOpts().MicrosoftExt) |
| 12557 | NewFD->setInvalidDecl(); |
| Douglas Gregor | 8a27391 | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 12558 | } |
| 12559 | } |
| 12560 | } |
| 12561 | |
| Douglas Gregor | 1efa437 | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 12562 | // FIXME: We need to pass in the attributes given an AST |
| 12563 | // representation, not a parser representation. |
| Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 12564 | if (D) { |
| Douglas Gregor | d2472d4 | 2013-05-02 23:25:32 +0000 | [diff] [blame] | 12565 | // FIXME: The current scope is almost... but not entirely... correct here. |
| 12566 | ProcessDeclAttributes(getCurScope(), NewFD, *D); |
| Douglas Gregor | 1efa437 | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 12567 | |
| Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 12568 | if (NewFD->hasAttrs()) |
| 12569 | CheckAlignasUnderalignment(NewFD); |
| 12570 | } |
| 12571 | |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 12572 | // In auto-retain/release, infer strong retension for fields of |
| 12573 | // retainable type. |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 12574 | if (getLangOpts().ObjCAutoRefCount && inferObjCARCLifetime(NewFD)) |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 12575 | NewFD->setInvalidDecl(); |
| 12576 | |
| Fariborz Jahanian | d29fecd | 2009-02-19 00:22:47 +0000 | [diff] [blame] | 12577 | if (T.isObjCGCWeak()) |
| Fariborz Jahanian | d35c792 | 2009-02-18 18:14:41 +0000 | [diff] [blame] | 12578 | Diag(Loc, diag::warn_attribute_weak_on_field); |
| Anders Carlsson | 28e7108 | 2008-02-16 00:29:18 +0000 | [diff] [blame] | 12579 | |
| Douglas Gregor | 4261e4c | 2009-03-11 20:50:30 +0000 | [diff] [blame] | 12580 | NewFD->setAccess(AS); |
| Steve Naroff | f93b672 | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 12581 | return NewFD; |
| Chris Lattner | 1300fb9 | 2007-01-23 23:42:53 +0000 | [diff] [blame] | 12582 | } |
| 12583 | |
| Argyrios Kyrtzidis | 33aee39 | 2010-08-16 17:27:08 +0000 | [diff] [blame] | 12584 | bool Sema::CheckNontrivialField(FieldDecl *FD) { |
| 12585 | assert(FD); |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 12586 | assert(getLangOpts().CPlusPlus && "valid check only for C++"); |
| Argyrios Kyrtzidis | 33aee39 | 2010-08-16 17:27:08 +0000 | [diff] [blame] | 12587 | |
| Nick Lewycky | 7a2a479 | 2013-06-25 23:22:23 +0000 | [diff] [blame] | 12588 | if (FD->isInvalidDecl() || FD->getType()->isDependentType()) |
| 12589 | return false; |
| Argyrios Kyrtzidis | 33aee39 | 2010-08-16 17:27:08 +0000 | [diff] [blame] | 12590 | |
| 12591 | QualType EltTy = Context.getBaseElementType(FD->getType()); |
| 12592 | if (const RecordType *RT = EltTy->getAs<RecordType>()) { |
| Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 12593 | CXXRecordDecl *RDecl = cast<CXXRecordDecl>(RT->getDecl()); |
| Argyrios Kyrtzidis | 33aee39 | 2010-08-16 17:27:08 +0000 | [diff] [blame] | 12594 | if (RDecl->getDefinition()) { |
| 12595 | // We check for copy constructors before constructors |
| 12596 | // because otherwise we'll never get complaints about |
| 12597 | // copy constructors. |
| 12598 | |
| 12599 | CXXSpecialMember member = CXXInvalid; |
| Richard Smith | 1648847 | 2012-11-16 00:53:38 +0000 | [diff] [blame] | 12600 | // We're required to check for any non-trivial constructors. Since the |
| 12601 | // implicit default constructor is suppressed if there are any |
| 12602 | // user-declared constructors, we just need to check that there is a |
| 12603 | // trivial default constructor and a trivial copy constructor. (We don't |
| 12604 | // worry about move constructors here, since this is a C++98 check.) |
| 12605 | if (RDecl->hasNonTrivialCopyConstructor()) |
| Argyrios Kyrtzidis | 33aee39 | 2010-08-16 17:27:08 +0000 | [diff] [blame] | 12606 | member = CXXCopyConstructor; |
| Alexis Hunt | f479f1b | 2011-05-09 18:22:59 +0000 | [diff] [blame] | 12607 | else if (!RDecl->hasTrivialDefaultConstructor()) |
| Alexis Hunt | 80f00ff | 2011-05-10 19:08:14 +0000 | [diff] [blame] | 12608 | member = CXXDefaultConstructor; |
| Richard Smith | 1648847 | 2012-11-16 00:53:38 +0000 | [diff] [blame] | 12609 | else if (RDecl->hasNonTrivialCopyAssignment()) |
| Argyrios Kyrtzidis | 33aee39 | 2010-08-16 17:27:08 +0000 | [diff] [blame] | 12610 | member = CXXCopyAssignment; |
| Richard Smith | 1648847 | 2012-11-16 00:53:38 +0000 | [diff] [blame] | 12611 | else if (RDecl->hasNonTrivialDestructor()) |
| Argyrios Kyrtzidis | 33aee39 | 2010-08-16 17:27:08 +0000 | [diff] [blame] | 12612 | member = CXXDestructor; |
| 12613 | |
| 12614 | if (member != CXXInvalid) { |
| Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 12615 | if (!getLangOpts().CPlusPlus11 && |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 12616 | getLangOpts().ObjCAutoRefCount && RDecl->hasObjectMember()) { |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 12617 | // Objective-C++ ARC: it is an error to have a non-trivial field of |
| 12618 | // a union. However, system headers in Objective-C programs |
| 12619 | // occasionally have Objective-C lifetime objects within unions, |
| 12620 | // and rather than cause the program to fail, we make those |
| 12621 | // members unavailable. |
| 12622 | SourceLocation Loc = FD->getLocation(); |
| 12623 | if (getSourceManager().isInSystemHeader(Loc)) { |
| 12624 | if (!FD->hasAttr<UnavailableAttr>()) |
| Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 12625 | FD->addAttr(UnavailableAttr::CreateImplicit(Context, |
| 12626 | "this system field has retaining ownership", |
| 12627 | Loc)); |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 12628 | return false; |
| 12629 | } |
| 12630 | } |
| Richard Smith | f720df0 | 2011-10-19 20:41:51 +0000 | [diff] [blame] | 12631 | |
| Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 12632 | Diag(FD->getLocation(), getLangOpts().CPlusPlus11 ? |
| Richard Smith | f720df0 | 2011-10-19 20:41:51 +0000 | [diff] [blame] | 12633 | diag::warn_cxx98_compat_nontrivial_union_or_anon_struct_member : |
| 12634 | diag::err_illegal_union_or_anon_struct_member) |
| 12635 | << (int)FD->getParent()->isUnion() << FD->getDeclName() << member; |
| Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 12636 | DiagnoseNontrivial(RDecl, member); |
| Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 12637 | return !getLangOpts().CPlusPlus11; |
| Argyrios Kyrtzidis | 33aee39 | 2010-08-16 17:27:08 +0000 | [diff] [blame] | 12638 | } |
| 12639 | } |
| 12640 | } |
| Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 12641 | |
| Argyrios Kyrtzidis | 33aee39 | 2010-08-16 17:27:08 +0000 | [diff] [blame] | 12642 | return false; |
| 12643 | } |
| 12644 | |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12645 | /// TranslateIvarVisibility - Translate visibility from a token ID to an |
| Fariborz Jahanian | f26702eb | 2007-10-01 16:53:59 +0000 | [diff] [blame] | 12646 | /// AST enum value. |
| Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 12647 | static ObjCIvarDecl::AccessControl |
| Fariborz Jahanian | f26702eb | 2007-10-01 16:53:59 +0000 | [diff] [blame] | 12648 | TranslateIvarVisibility(tok::ObjCKeywordKind ivarVisibility) { |
| Steve Naroff | 2e688fd | 2007-09-14 23:09:53 +0000 | [diff] [blame] | 12649 | switch (ivarVisibility) { |
| David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 12650 | default: llvm_unreachable("Unknown visitibility kind"); |
| Chris Lattner | 79ef843 | 2008-10-12 00:28:42 +0000 | [diff] [blame] | 12651 | case tok::objc_private: return ObjCIvarDecl::Private; |
| 12652 | case tok::objc_public: return ObjCIvarDecl::Public; |
| 12653 | case tok::objc_protected: return ObjCIvarDecl::Protected; |
| 12654 | case tok::objc_package: return ObjCIvarDecl::Package; |
| Steve Naroff | 2e688fd | 2007-09-14 23:09:53 +0000 | [diff] [blame] | 12655 | } |
| 12656 | } |
| 12657 | |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12658 | /// 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] | 12659 | /// in order to create an IvarDecl object for it. |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 12660 | Decl *Sema::ActOnIvar(Scope *S, |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12661 | SourceLocation DeclStart, |
| Richard Trieu | 2bd0401 | 2011-09-09 02:00:50 +0000 | [diff] [blame] | 12662 | Declarator &D, Expr *BitfieldWidth, |
| Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 12663 | tok::ObjCKeywordKind Visibility) { |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12664 | |
| Fariborz Jahanian | de61583 | 2008-04-10 23:32:45 +0000 | [diff] [blame] | 12665 | IdentifierInfo *II = D.getIdentifier(); |
| 12666 | Expr *BitWidth = (Expr*)BitfieldWidth; |
| 12667 | SourceLocation Loc = DeclStart; |
| 12668 | if (II) Loc = D.getIdentifierLoc(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12669 | |
| Fariborz Jahanian | de61583 | 2008-04-10 23:32:45 +0000 | [diff] [blame] | 12670 | // FIXME: Unnamed fields can be handled in various different ways, for |
| 12671 | // example, unnamed unions inject all members into the struct namespace! |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12672 | |
| John McCall | 8cb7bdf | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 12673 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
| 12674 | QualType T = TInfo->getType(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12675 | |
| Fariborz Jahanian | de61583 | 2008-04-10 23:32:45 +0000 | [diff] [blame] | 12676 | if (BitWidth) { |
| Steve Naroff | 17b2f5d | 2009-02-20 17:57:11 +0000 | [diff] [blame] | 12677 | // 6.7.2.1p3, 6.7.2.1p4 |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 12678 | BitWidth = VerifyBitField(Loc, II, T, /*IsMsStruct*/false, BitWidth).get(); |
| Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 12679 | if (!BitWidth) |
| Chris Lattner | f6d1c9c | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 12680 | D.setInvalidType(); |
| Fariborz Jahanian | de61583 | 2008-04-10 23:32:45 +0000 | [diff] [blame] | 12681 | } else { |
| 12682 | // Not a bitfield. |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12683 | |
| Fariborz Jahanian | de61583 | 2008-04-10 23:32:45 +0000 | [diff] [blame] | 12684 | // validate II. |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12685 | |
| Fariborz Jahanian | de61583 | 2008-04-10 23:32:45 +0000 | [diff] [blame] | 12686 | } |
| Fariborz Jahanian | 0103d67 | 2010-04-26 22:07:03 +0000 | [diff] [blame] | 12687 | if (T->isReferenceType()) { |
| 12688 | Diag(Loc, diag::err_ivar_reference_type); |
| 12689 | D.setInvalidType(); |
| 12690 | } |
| Fariborz Jahanian | de61583 | 2008-04-10 23:32:45 +0000 | [diff] [blame] | 12691 | // C99 6.7.2.1p8: A member of a structure or union may have any type other |
| 12692 | // than a variably modified type. |
| Fariborz Jahanian | 0103d67 | 2010-04-26 22:07:03 +0000 | [diff] [blame] | 12693 | else if (T->isVariablyModifiedType()) { |
| Anders Carlsson | 0d8f0ba | 2008-12-07 00:20:55 +0000 | [diff] [blame] | 12694 | Diag(Loc, diag::err_typecheck_ivar_variable_size); |
| Chris Lattner | f6d1c9c | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 12695 | D.setInvalidType(); |
| Fariborz Jahanian | de61583 | 2008-04-10 23:32:45 +0000 | [diff] [blame] | 12696 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12697 | |
| Ted Kremenek | 73295fa | 2008-07-23 18:04:17 +0000 | [diff] [blame] | 12698 | // Get the visibility (access control) for this ivar. |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12699 | ObjCIvarDecl::AccessControl ac = |
| Ted Kremenek | 73295fa | 2008-07-23 18:04:17 +0000 | [diff] [blame] | 12700 | Visibility != tok::objc_not_keyword ? TranslateIvarVisibility(Visibility) |
| 12701 | : ObjCIvarDecl::None; |
| Fariborz Jahanian | 6845383 | 2009-06-05 18:16:35 +0000 | [diff] [blame] | 12702 | // Must set ivar's DeclContext to its enclosing interface. |
| Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 12703 | ObjCContainerDecl *EnclosingDecl = cast<ObjCContainerDecl>(CurContext); |
| Fariborz Jahanian | 17612b1 | 2012-02-02 00:49:12 +0000 | [diff] [blame] | 12704 | if (!EnclosingDecl || EnclosingDecl->isInvalidDecl()) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 12705 | return nullptr; |
| Daniel Dunbar | 229385c | 2010-04-02 18:29:09 +0000 | [diff] [blame] | 12706 | ObjCContainerDecl *EnclosingContext; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12707 | if (ObjCImplementationDecl *IMPDecl = |
| Fariborz Jahanian | 6845383 | 2009-06-05 18:16:35 +0000 | [diff] [blame] | 12708 | dyn_cast<ObjCImplementationDecl>(EnclosingDecl)) { |
| John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 12709 | if (LangOpts.ObjCRuntime.isFragile()) { |
| Fariborz Jahanian | 6845383 | 2009-06-05 18:16:35 +0000 | [diff] [blame] | 12710 | // 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] | 12711 | EnclosingContext = IMPDecl->getClassInterface(); |
| 12712 | assert(EnclosingContext && "Implementation has no class interface!"); |
| 12713 | } |
| 12714 | else |
| 12715 | EnclosingContext = EnclosingDecl; |
| Fariborz Jahanian | 6a0a2e0 | 2010-04-06 22:43:48 +0000 | [diff] [blame] | 12716 | } else { |
| 12717 | if (ObjCCategoryDecl *CDecl = |
| 12718 | dyn_cast<ObjCCategoryDecl>(EnclosingDecl)) { |
| John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 12719 | if (LangOpts.ObjCRuntime.isFragile() || !CDecl->IsClassExtension()) { |
| Fariborz Jahanian | 6a0a2e0 | 2010-04-06 22:43:48 +0000 | [diff] [blame] | 12720 | Diag(Loc, diag::err_misplaced_ivar) << CDecl->IsClassExtension(); |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 12721 | return nullptr; |
| Fariborz Jahanian | 6a0a2e0 | 2010-04-06 22:43:48 +0000 | [diff] [blame] | 12722 | } |
| 12723 | } |
| Daniel Dunbar | 229385c | 2010-04-02 18:29:09 +0000 | [diff] [blame] | 12724 | EnclosingContext = EnclosingDecl; |
| Fariborz Jahanian | 6a0a2e0 | 2010-04-06 22:43:48 +0000 | [diff] [blame] | 12725 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12726 | |
| Ted Kremenek | 73295fa | 2008-07-23 18:04:17 +0000 | [diff] [blame] | 12727 | // Construct the decl. |
| Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 12728 | ObjCIvarDecl *NewID = ObjCIvarDecl::Create(Context, EnclosingContext, |
| 12729 | DeclStart, Loc, II, T, |
| John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 12730 | TInfo, ac, (Expr *)BitfieldWidth); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12731 | |
| Douglas Gregor | 82ac25e | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 12732 | if (II) { |
| Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 12733 | NamedDecl *PrevDecl = LookupSingleName(S, II, Loc, LookupMemberName, |
| John McCall | 5cebab1 | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 12734 | ForRedeclaration); |
| Fariborz Jahanian | 6845383 | 2009-06-05 18:16:35 +0000 | [diff] [blame] | 12735 | if (PrevDecl && isDeclInScope(PrevDecl, EnclosingContext, S) |
| Douglas Gregor | 82ac25e | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 12736 | && !isa<TagDecl>(PrevDecl)) { |
| 12737 | Diag(Loc, diag::err_duplicate_member) << II; |
| 12738 | Diag(PrevDecl->getLocation(), diag::note_previous_declaration); |
| 12739 | NewID->setInvalidDecl(); |
| 12740 | } |
| 12741 | } |
| 12742 | |
| Ted Kremenek | 73295fa | 2008-07-23 18:04:17 +0000 | [diff] [blame] | 12743 | // Process attributes attached to the ivar. |
| Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 12744 | ProcessDeclAttributes(S, NewID, D); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12745 | |
| Chris Lattner | f6d1c9c | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 12746 | if (D.isInvalidType()) |
| Fariborz Jahanian | de61583 | 2008-04-10 23:32:45 +0000 | [diff] [blame] | 12747 | NewID->setInvalidDecl(); |
| Ted Kremenek | 73295fa | 2008-07-23 18:04:17 +0000 | [diff] [blame] | 12748 | |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 12749 | // In ARC, infer 'retaining' for ivars of retainable type. |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 12750 | if (getLangOpts().ObjCAutoRefCount && inferObjCARCLifetime(NewID)) |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 12751 | NewID->setInvalidDecl(); |
| 12752 | |
| Douglas Gregor | 3baa670 | 2011-09-12 16:11:24 +0000 | [diff] [blame] | 12753 | if (D.getDeclSpec().isModulePrivateSpecified()) |
| 12754 | NewID->setModulePrivate(); |
| 12755 | |
| Douglas Gregor | 82ac25e | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 12756 | if (II) { |
| 12757 | // FIXME: When interfaces are DeclContexts, we'll need to add |
| 12758 | // these to the interface. |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 12759 | S->AddDecl(NewID); |
| Douglas Gregor | 82ac25e | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 12760 | IdResolver.AddDecl(NewID); |
| 12761 | } |
| Fariborz Jahanian | 80297b1 | 2012-05-15 16:33:04 +0000 | [diff] [blame] | 12762 | |
| John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 12763 | if (LangOpts.ObjCRuntime.isNonFragile() && |
| Fariborz Jahanian | 80297b1 | 2012-05-15 16:33:04 +0000 | [diff] [blame] | 12764 | !NewID->isInvalidDecl() && isa<ObjCInterfaceDecl>(EnclosingDecl)) |
| Fariborz Jahanian | e1ada58 | 2012-05-15 17:43:16 +0000 | [diff] [blame] | 12765 | Diag(Loc, diag::warn_ivars_in_interface); |
| Fariborz Jahanian | 80297b1 | 2012-05-15 16:33:04 +0000 | [diff] [blame] | 12766 | |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 12767 | return NewID; |
| Fariborz Jahanian | de61583 | 2008-04-10 23:32:45 +0000 | [diff] [blame] | 12768 | } |
| 12769 | |
| Fariborz Jahanian | 616d3e7 | 2010-08-23 22:46:52 +0000 | [diff] [blame] | 12770 | /// ActOnLastBitfield - This routine handles synthesized bitfields rules for |
| Jordan Rose | a0e9d39 | 2013-04-03 01:39:23 +0000 | [diff] [blame] | 12771 | /// class and class extensions. For every class \@interface and class |
| 12772 | /// extension \@interface, if the last ivar is a bitfield of any type, |
| Fariborz Jahanian | 616d3e7 | 2010-08-23 22:46:52 +0000 | [diff] [blame] | 12773 | /// 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] | 12774 | void Sema::ActOnLastBitfield(SourceLocation DeclLoc, |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 12775 | SmallVectorImpl<Decl *> &AllIvarDecls) { |
| John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 12776 | if (LangOpts.ObjCRuntime.isFragile() || AllIvarDecls.empty()) |
| Fariborz Jahanian | 616d3e7 | 2010-08-23 22:46:52 +0000 | [diff] [blame] | 12777 | return; |
| 12778 | |
| 12779 | Decl *ivarDecl = AllIvarDecls[AllIvarDecls.size()-1]; |
| 12780 | ObjCIvarDecl *Ivar = cast<ObjCIvarDecl>(ivarDecl); |
| 12781 | |
| Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 12782 | if (!Ivar->isBitField() || Ivar->getBitWidthValue(Context) == 0) |
| Fariborz Jahanian | 616d3e7 | 2010-08-23 22:46:52 +0000 | [diff] [blame] | 12783 | return; |
| Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 12784 | ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CurContext); |
| Fariborz Jahanian | 616d3e7 | 2010-08-23 22:46:52 +0000 | [diff] [blame] | 12785 | if (!ID) { |
| Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 12786 | if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(CurContext)) { |
| Fariborz Jahanian | 616d3e7 | 2010-08-23 22:46:52 +0000 | [diff] [blame] | 12787 | if (!CD->IsClassExtension()) |
| 12788 | return; |
| 12789 | } |
| 12790 | // No need to add this to end of @implementation. |
| 12791 | else |
| 12792 | return; |
| 12793 | } |
| 12794 | // 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] | 12795 | llvm::APInt Zero(Context.getTypeSize(Context.IntTy), 0); |
| 12796 | Expr * BW = IntegerLiteral::Create(Context, Zero, Context.IntTy, DeclLoc); |
| Fariborz Jahanian | 616d3e7 | 2010-08-23 22:46:52 +0000 | [diff] [blame] | 12797 | |
| Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 12798 | Ivar = ObjCIvarDecl::Create(Context, cast<ObjCContainerDecl>(CurContext), |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 12799 | DeclLoc, DeclLoc, nullptr, |
| Fariborz Jahanian | 616d3e7 | 2010-08-23 22:46:52 +0000 | [diff] [blame] | 12800 | Context.CharTy, |
| Douglas Gregor | 1d39480 | 2011-08-03 16:26:46 +0000 | [diff] [blame] | 12801 | Context.getTrivialTypeSourceInfo(Context.CharTy, |
| 12802 | DeclLoc), |
| Fariborz Jahanian | 616d3e7 | 2010-08-23 22:46:52 +0000 | [diff] [blame] | 12803 | ObjCIvarDecl::Private, BW, |
| 12804 | true); |
| 12805 | AllIvarDecls.push_back(Ivar); |
| 12806 | } |
| 12807 | |
| Robert Wilhelm | 16e94b9 | 2013-08-09 18:02:13 +0000 | [diff] [blame] | 12808 | void Sema::ActOnFields(Scope *S, SourceLocation RecLoc, Decl *EnclosingDecl, |
| 12809 | ArrayRef<Decl *> Fields, SourceLocation LBrac, |
| 12810 | SourceLocation RBrac, AttributeList *Attr) { |
| Steve Naroff | db47ee2 | 2007-09-14 22:20:54 +0000 | [diff] [blame] | 12811 | assert(EnclosingDecl && "missing record or interface decl"); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12812 | |
| Eric Christopher | 7457aaf | 2012-07-19 22:22:51 +0000 | [diff] [blame] | 12813 | // If this is an Objective-C @implementation or category and we have |
| 12814 | // new fields here we should reset the layout of the interface since |
| 12815 | // it will now change. |
| 12816 | if (!Fields.empty() && isa<ObjCContainerDecl>(EnclosingDecl)) { |
| 12817 | ObjCContainerDecl *DC = cast<ObjCContainerDecl>(EnclosingDecl); |
| 12818 | switch (DC->getKind()) { |
| 12819 | default: break; |
| 12820 | case Decl::ObjCCategory: |
| 12821 | Context.ResetObjCLayout(cast<ObjCCategoryDecl>(DC)->getClassInterface()); |
| 12822 | break; |
| 12823 | case Decl::ObjCImplementation: |
| 12824 | Context. |
| 12825 | ResetObjCLayout(cast<ObjCImplementationDecl>(DC)->getClassInterface()); |
| 12826 | break; |
| 12827 | } |
| 12828 | } |
| 12829 | |
| Eli Friedman | a767941 | 2012-02-07 05:00:47 +0000 | [diff] [blame] | 12830 | RecordDecl *Record = dyn_cast<RecordDecl>(EnclosingDecl); |
| 12831 | |
| 12832 | // Start counting up the number of named members; make sure to include |
| 12833 | // members of anonymous structs and unions in the total. |
| Chris Lattner | 8262560 | 2007-01-24 02:26:21 +0000 | [diff] [blame] | 12834 | unsigned NumNamedMembers = 0; |
| Eli Friedman | a767941 | 2012-02-07 05:00:47 +0000 | [diff] [blame] | 12835 | if (Record) { |
| Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 12836 | for (const auto *I : Record->decls()) { |
| 12837 | if (const auto *IFD = dyn_cast<IndirectFieldDecl>(I)) |
| Eli Friedman | a767941 | 2012-02-07 05:00:47 +0000 | [diff] [blame] | 12838 | if (IFD->getDeclName()) |
| 12839 | ++NumNamedMembers; |
| 12840 | } |
| 12841 | } |
| 12842 | |
| 12843 | // Verify that all the fields are okay. |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 12844 | SmallVector<FieldDecl*, 32> RecFields; |
| Douglas Gregor | f4d3327 | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 12845 | |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 12846 | bool ARCErrReported = false; |
| Robert Wilhelm | 16e94b9 | 2013-08-09 18:02:13 +0000 | [diff] [blame] | 12847 | for (ArrayRef<Decl *>::iterator i = Fields.begin(), end = Fields.end(); |
| David Blaikie | 751c558 | 2011-09-22 02:58:26 +0000 | [diff] [blame] | 12848 | i != end; ++i) { |
| 12849 | FieldDecl *FD = cast<FieldDecl>(*i); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12850 | |
| Chris Lattner | 720a054 | 2007-01-25 00:44:24 +0000 | [diff] [blame] | 12851 | // Get the type for the field. |
| John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 12852 | const Type *FDTy = FD->getType().getTypePtr(); |
| Douglas Gregor | f4d3327 | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 12853 | |
| Douglas Gregor | 82ac25e | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 12854 | if (!FD->isAnonymousStructOrUnion()) { |
| Douglas Gregor | f4d3327 | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 12855 | // Remember all fields written by the user. |
| 12856 | RecFields.push_back(FD); |
| 12857 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12858 | |
| Chris Lattner | 73bf7b4 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 12859 | // If the field is already invalid for some reason, don't emit more |
| 12860 | // diagnostics about it. |
| Eli Friedman | d0e8de2 | 2009-12-07 00:22:08 +0000 | [diff] [blame] | 12861 | if (FD->isInvalidDecl()) { |
| 12862 | EnclosingDecl->setInvalidDecl(); |
| Chris Lattner | 73bf7b4 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 12863 | continue; |
| Eli Friedman | d0e8de2 | 2009-12-07 00:22:08 +0000 | [diff] [blame] | 12864 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12865 | |
| Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 12866 | // C99 6.7.2.1p2: |
| 12867 | // A structure or union shall not contain a member with |
| 12868 | // incomplete or function type (hence, a structure shall not |
| 12869 | // contain an instance of itself, but may contain a pointer to |
| 12870 | // an instance of itself), except that the last member of a |
| 12871 | // structure with more than one named member may have incomplete |
| 12872 | // array type; such a structure (and any union containing, |
| 12873 | // possibly recursively, a member that is such a structure) |
| 12874 | // shall not be a member of a structure or an element of an |
| 12875 | // array. |
| Chris Lattner | 0fd893e | 2007-07-31 21:33:24 +0000 | [diff] [blame] | 12876 | if (FDTy->isFunctionType()) { |
| Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 12877 | // Field declared as a function. |
| Chris Lattner | 651d42d | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 12878 | Diag(FD->getLocation(), diag::err_field_declared_as_function) |
| Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 12879 | << FD->getDeclName(); |
| Steve Naroff | db47ee2 | 2007-09-14 22:20:54 +0000 | [diff] [blame] | 12880 | FD->setInvalidDecl(); |
| 12881 | EnclosingDecl->setInvalidDecl(); |
| Chris Lattner | bdf8b8d | 2007-01-24 02:11:17 +0000 | [diff] [blame] | 12882 | continue; |
| Francois Pichet | f657b63 | 2010-09-15 00:14:08 +0000 | [diff] [blame] | 12883 | } else if (FDTy->isIncompleteArrayType() && Record && |
| David Blaikie | 751c558 | 2011-09-22 02:58:26 +0000 | [diff] [blame] | 12884 | ((i + 1 == Fields.end() && !Record->isUnion()) || |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 12885 | ((getLangOpts().MicrosoftExt || |
| 12886 | getLangOpts().CPlusPlus) && |
| David Blaikie | 751c558 | 2011-09-22 02:58:26 +0000 | [diff] [blame] | 12887 | (i + 1 == Fields.end() || Record->isUnion())))) { |
| Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 12888 | // Flexible array member. |
| Argyrios Kyrtzidis | 7e25a95 | 2011-03-07 20:04:04 +0000 | [diff] [blame] | 12889 | // Microsoft and g++ is more permissive regarding flexible array. |
| Francois Pichet | f657b63 | 2010-09-15 00:14:08 +0000 | [diff] [blame] | 12890 | // It will accept flexible array in union and also |
| Anders Carlsson | 0ea1047 | 2010-10-17 23:36:12 +0000 | [diff] [blame] | 12891 | // as the sole element of a struct/class. |
| David Majnemer | 4101621 | 2013-11-02 10:38:05 +0000 | [diff] [blame] | 12892 | unsigned DiagID = 0; |
| 12893 | if (Record->isUnion()) |
| 12894 | DiagID = getLangOpts().MicrosoftExt |
| 12895 | ? diag::ext_flexible_array_union_ms |
| 12896 | : getLangOpts().CPlusPlus |
| 12897 | ? diag::ext_flexible_array_union_gnu |
| 12898 | : diag::err_flexible_array_union; |
| 12899 | else if (Fields.size() == 1) |
| 12900 | DiagID = getLangOpts().MicrosoftExt |
| 12901 | ? diag::ext_flexible_array_empty_aggregate_ms |
| 12902 | : getLangOpts().CPlusPlus |
| 12903 | ? diag::ext_flexible_array_empty_aggregate_gnu |
| 12904 | : NumNamedMembers < 1 |
| 12905 | ? diag::err_flexible_array_empty_aggregate |
| 12906 | : 0; |
| 12907 | |
| 12908 | if (DiagID) |
| 12909 | Diag(FD->getLocation(), DiagID) << FD->getDeclName() |
| 12910 | << Record->getTagKind(); |
| David Majnemer | 08cd760 | 2013-11-02 11:19:13 +0000 | [diff] [blame] | 12911 | // While the layout of types that contain virtual bases is not specified |
| 12912 | // by the C++ standard, both the Itanium and Microsoft C++ ABIs place |
| 12913 | // virtual bases after the derived members. This would make a flexible |
| 12914 | // array member declared at the end of an object not adjacent to the end |
| 12915 | // of the type. |
| 12916 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Record)) |
| 12917 | if (RD->getNumVBases() != 0) |
| 12918 | Diag(FD->getLocation(), diag::err_flexible_array_virtual_base) |
| 12919 | << FD->getDeclName() << Record->getTagKind(); |
| David Majnemer | 4101621 | 2013-11-02 10:38:05 +0000 | [diff] [blame] | 12920 | if (!getLangOpts().C99) |
| 12921 | Diag(FD->getLocation(), diag::ext_c99_flexible_array_member) |
| 12922 | << FD->getDeclName() << Record->getTagKind(); |
| 12923 | |
| Richard Smith | 6fa28ff | 2014-01-11 00:53:35 +0000 | [diff] [blame] | 12924 | // If the element type has a non-trivial destructor, we would not |
| 12925 | // implicitly destroy the elements, so disallow it for now. |
| 12926 | // |
| 12927 | // FIXME: GCC allows this. We should probably either implicitly delete |
| 12928 | // the destructor of the containing class, or just allow this. |
| 12929 | QualType BaseElem = Context.getBaseElementType(FD->getType()); |
| 12930 | if (!BaseElem->isDependentType() && BaseElem.isDestructedType()) { |
| 12931 | Diag(FD->getLocation(), diag::err_flexible_array_has_nontrivial_dtor) |
| Fariborz Jahanian | b0e2847 | 2010-05-26 20:46:24 +0000 | [diff] [blame] | 12932 | << FD->getDeclName() << FD->getType(); |
| Fariborz Jahanian | 1138ba6 | 2010-05-26 20:19:07 +0000 | [diff] [blame] | 12933 | FD->setInvalidDecl(); |
| 12934 | EnclosingDecl->setInvalidDecl(); |
| 12935 | continue; |
| 12936 | } |
| Chris Lattner | 720a054 | 2007-01-25 00:44:24 +0000 | [diff] [blame] | 12937 | // Okay, we have a legal flexible array member at the end of the struct. |
| David Majnemer | ac0b30e | 2014-09-24 11:04:09 +0000 | [diff] [blame] | 12938 | Record->setHasFlexibleArrayMember(true); |
| Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 12939 | } else if (!FDTy->isDependentType() && |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12940 | RequireCompleteType(FD->getLocation(), FD->getType(), |
| Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 12941 | diag::err_field_incomplete)) { |
| 12942 | // Incomplete type |
| 12943 | FD->setInvalidDecl(); |
| 12944 | EnclosingDecl->setInvalidDecl(); |
| 12945 | continue; |
| Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 12946 | } else if (const RecordType *FDTTy = FDTy->getAs<RecordType>()) { |
| David Majnemer | ac0b30e | 2014-09-24 11:04:09 +0000 | [diff] [blame] | 12947 | if (Record && FDTTy->getDecl()->hasFlexibleArrayMember()) { |
| 12948 | // A type which contains a flexible array member is considered to be a |
| 12949 | // flexible array member. |
| 12950 | Record->setHasFlexibleArrayMember(true); |
| 12951 | if (!Record->isUnion()) { |
| Chris Lattner | 720a054 | 2007-01-25 00:44:24 +0000 | [diff] [blame] | 12952 | // If this is a struct/class and this is not the last element, reject |
| 12953 | // it. Note that GCC supports variable sized arrays in the middle of |
| 12954 | // structures. |
| David Blaikie | 751c558 | 2011-09-22 02:58:26 +0000 | [diff] [blame] | 12955 | if (i + 1 != Fields.end()) |
| Douglas Gregor | 3e06dbf | 2009-03-06 23:41:27 +0000 | [diff] [blame] | 12956 | Diag(FD->getLocation(), diag::ext_variable_sized_type_in_struct) |
| Chris Lattner | b28fe9e | 2009-04-25 18:52:45 +0000 | [diff] [blame] | 12957 | << FD->getDeclName() << FD->getType(); |
| Douglas Gregor | 3e06dbf | 2009-03-06 23:41:27 +0000 | [diff] [blame] | 12958 | else { |
| 12959 | // We support flexible arrays at the end of structs in |
| 12960 | // other structs as an extension. |
| 12961 | Diag(FD->getLocation(), diag::ext_flexible_array_in_struct) |
| 12962 | << FD->getDeclName(); |
| Chris Lattner | 720a054 | 2007-01-25 00:44:24 +0000 | [diff] [blame] | 12963 | } |
| Chris Lattner | 720a054 | 2007-01-25 00:44:24 +0000 | [diff] [blame] | 12964 | } |
| 12965 | } |
| Fariborz Jahanian | 78f565b | 2012-08-16 22:38:41 +0000 | [diff] [blame] | 12966 | if (isa<ObjCContainerDecl>(EnclosingDecl) && |
| 12967 | RequireNonAbstractType(FD->getLocation(), FD->getType(), |
| 12968 | diag::err_abstract_type_in_decl, |
| 12969 | AbstractIvarType)) { |
| 12970 | // Ivars can not have abstract class types |
| 12971 | FD->setInvalidDecl(); |
| 12972 | } |
| Fariborz Jahanian | 5f21d2f | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 12973 | if (Record && FDTTy->getDecl()->hasObjectMember()) |
| 12974 | Record->setHasObjectMember(true); |
| Fariborz Jahanian | 7865220 | 2013-01-25 23:57:05 +0000 | [diff] [blame] | 12975 | if (Record && FDTTy->getDecl()->hasVolatileMember()) |
| 12976 | Record->setHasVolatileMember(true); |
| John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 12977 | } else if (FDTy->isObjCObjectType()) { |
| Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 12978 | /// A field cannot be an Objective-c object |
| Fariborz Jahanian | 6507135e | 2011-07-26 17:58:54 +0000 | [diff] [blame] | 12979 | Diag(FD->getLocation(), diag::err_statically_allocated_object) |
| 12980 | << FixItHint::CreateInsertion(FD->getLocation(), "*"); |
| 12981 | QualType T = Context.getObjCObjectPointerType(FD->getType()); |
| 12982 | FD->setType(T); |
| Douglas Gregor | e6c3fa0 | 2013-01-28 19:08:09 +0000 | [diff] [blame] | 12983 | } else if (getLangOpts().ObjCAutoRefCount && Record && !ARCErrReported && |
| 12984 | (!getLangOpts().CPlusPlus || Record->isUnion())) { |
| 12985 | // It's an error in ARC if a field has lifetime. |
| 12986 | // We don't want to report this in a system header, though, |
| 12987 | // so we just make the field unavailable. |
| 12988 | // FIXME: that's really not sufficient; we need to make the type |
| 12989 | // itself invalid to, say, initialize or copy. |
| 12990 | QualType T = FD->getType(); |
| 12991 | Qualifiers::ObjCLifetime lifetime = T.getObjCLifetime(); |
| 12992 | if (lifetime && lifetime != Qualifiers::OCL_ExplicitNone) { |
| 12993 | SourceLocation loc = FD->getLocation(); |
| 12994 | if (getSourceManager().isInSystemHeader(loc)) { |
| 12995 | if (!FD->hasAttr<UnavailableAttr>()) { |
| Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 12996 | FD->addAttr(UnavailableAttr::CreateImplicit(Context, |
| 12997 | "this system field has retaining ownership", |
| 12998 | loc)); |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 12999 | } |
| Douglas Gregor | e6c3fa0 | 2013-01-28 19:08:09 +0000 | [diff] [blame] | 13000 | } else { |
| 13001 | Diag(FD->getLocation(), diag::err_arc_objc_object_in_tag) |
| Douglas Gregor | 0ad84b4 | 2013-01-28 20:13:44 +0000 | [diff] [blame] | 13002 | << T->isBlockPointerType() << Record->getTagKind(); |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 13003 | } |
| Douglas Gregor | e6c3fa0 | 2013-01-28 19:08:09 +0000 | [diff] [blame] | 13004 | ARCErrReported = true; |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 13005 | } |
| Douglas Gregor | e6c3fa0 | 2013-01-28 19:08:09 +0000 | [diff] [blame] | 13006 | } else if (getLangOpts().ObjC1 && |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 13007 | getLangOpts().getGC() != LangOptions::NonGC && |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 13008 | Record && !Record->hasObjectMember()) { |
| Douglas Gregor | e6c3fa0 | 2013-01-28 19:08:09 +0000 | [diff] [blame] | 13009 | if (FD->getType()->isObjCObjectPointerType() || |
| 13010 | FD->getType().isObjCGCStrong()) |
| 13011 | Record->setHasObjectMember(true); |
| 13012 | else if (Context.getAsArrayType(FD->getType())) { |
| 13013 | QualType BaseType = Context.getBaseElementType(FD->getType()); |
| 13014 | if (BaseType->isRecordType() && |
| 13015 | BaseType->getAs<RecordType>()->getDecl()->hasObjectMember()) |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 13016 | Record->setHasObjectMember(true); |
| Douglas Gregor | e6c3fa0 | 2013-01-28 19:08:09 +0000 | [diff] [blame] | 13017 | else if (BaseType->isObjCObjectPointerType() || |
| 13018 | BaseType.isObjCGCStrong()) |
| 13019 | Record->setHasObjectMember(true); |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 13020 | } |
| Fariborz Jahanian | 021510e | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 13021 | } |
| Fariborz Jahanian | 7865220 | 2013-01-25 23:57:05 +0000 | [diff] [blame] | 13022 | if (Record && FD->getType().isVolatileQualified()) |
| 13023 | Record->setHasVolatileMember(true); |
| Chris Lattner | 8262560 | 2007-01-24 02:26:21 +0000 | [diff] [blame] | 13024 | // Keep track of the number of named members. |
| Douglas Gregor | 82ac25e | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 13025 | if (FD->getIdentifier()) |
| Chris Lattner | 8262560 | 2007-01-24 02:26:21 +0000 | [diff] [blame] | 13026 | ++NumNamedMembers; |
| Chris Lattner | bdf8b8d | 2007-01-24 02:11:17 +0000 | [diff] [blame] | 13027 | } |
| Sebastian Redl | baad4e7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 13028 | |
| Chris Lattner | 8262560 | 2007-01-24 02:26:21 +0000 | [diff] [blame] | 13029 | // Okay, we successfully defined 'Record'. |
| Chris Lattner | 622c193 | 2008-02-06 00:51:33 +0000 | [diff] [blame] | 13030 | if (Record) { |
| Douglas Gregor | 8fb9512 | 2010-09-29 00:15:42 +0000 | [diff] [blame] | 13031 | bool Completed = false; |
| 13032 | if (CXXRecordDecl *CXXRecord = dyn_cast<CXXRecordDecl>(Record)) { |
| 13033 | if (!CXXRecord->isInvalidDecl()) { |
| 13034 | // Set access bits correctly on the directly-declared conversions. |
| Argyrios Kyrtzidis | a6567c4 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 13035 | for (CXXRecordDecl::conversion_iterator |
| 13036 | I = CXXRecord->conversion_begin(), |
| 13037 | E = CXXRecord->conversion_end(); I != E; ++I) |
| 13038 | I.setAccess((*I)->getAccess()); |
| Douglas Gregor | 8fb9512 | 2010-09-29 00:15:42 +0000 | [diff] [blame] | 13039 | |
| 13040 | if (!CXXRecord->isDependentType()) { |
| Peter Collingbourne | b289fe6 | 2013-05-20 14:12:25 +0000 | [diff] [blame] | 13041 | if (CXXRecord->hasUserDeclaredDestructor()) { |
| 13042 | // Adjust user-defined destructor exception spec. |
| 13043 | if (getLangOpts().CPlusPlus11) |
| 13044 | AdjustDestructorExceptionSpec(CXXRecord, |
| 13045 | CXXRecord->getDestructor()); |
| Peter Collingbourne | b289fe6 | 2013-05-20 14:12:25 +0000 | [diff] [blame] | 13046 | } |
| Sebastian Redl | 623ea82 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 13047 | |
| Douglas Gregor | 8fb9512 | 2010-09-29 00:15:42 +0000 | [diff] [blame] | 13048 | // Add any implicitly-declared members to this class. |
| 13049 | AddImplicitlyDeclaredMembersToClass(CXXRecord); |
| 13050 | |
| 13051 | // If we have virtual base classes, we may end up finding multiple |
| 13052 | // final overriders for a given virtual function. Check for this |
| 13053 | // problem now. |
| 13054 | if (CXXRecord->getNumVBases()) { |
| 13055 | CXXFinalOverriderMap FinalOverriders; |
| 13056 | CXXRecord->getFinalOverriders(FinalOverriders); |
| 13057 | |
| 13058 | for (CXXFinalOverriderMap::iterator M = FinalOverriders.begin(), |
| 13059 | MEnd = FinalOverriders.end(); |
| 13060 | M != MEnd; ++M) { |
| 13061 | for (OverridingMethods::iterator SO = M->second.begin(), |
| 13062 | SOEnd = M->second.end(); |
| 13063 | SO != SOEnd; ++SO) { |
| 13064 | assert(SO->second.size() > 0 && |
| 13065 | "Virtual function without overridding functions?"); |
| 13066 | if (SO->second.size() == 1) |
| 13067 | continue; |
| 13068 | |
| 13069 | // C++ [class.virtual]p2: |
| 13070 | // In a derived class, if a virtual member function of a base |
| 13071 | // class subobject has more than one final overrider the |
| 13072 | // program is ill-formed. |
| 13073 | Diag(Record->getLocation(), diag::err_multiple_final_overriders) |
| Roman Divacky | e637711 | 2012-09-06 15:59:27 +0000 | [diff] [blame] | 13074 | << (const NamedDecl *)M->first << Record; |
| Douglas Gregor | 8fb9512 | 2010-09-29 00:15:42 +0000 | [diff] [blame] | 13075 | Diag(M->first->getLocation(), |
| 13076 | diag::note_overridden_virtual_function); |
| 13077 | for (OverridingMethods::overriding_iterator |
| 13078 | OM = SO->second.begin(), |
| 13079 | OMEnd = SO->second.end(); |
| 13080 | OM != OMEnd; ++OM) |
| 13081 | Diag(OM->Method->getLocation(), diag::note_final_overrider) |
| Roman Divacky | e637711 | 2012-09-06 15:59:27 +0000 | [diff] [blame] | 13082 | << (const NamedDecl *)M->first << OM->Method->getParent(); |
| Douglas Gregor | 8fb9512 | 2010-09-29 00:15:42 +0000 | [diff] [blame] | 13083 | |
| 13084 | Record->setInvalidDecl(); |
| 13085 | } |
| 13086 | } |
| 13087 | CXXRecord->completeDefinition(&FinalOverriders); |
| 13088 | Completed = true; |
| 13089 | } |
| 13090 | } |
| 13091 | } |
| 13092 | } |
| 13093 | |
| 13094 | if (!Completed) |
| 13095 | Record->completeDefinition(); |
| Sebastian Redl | 623ea82 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 13096 | |
| David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 13097 | if (Record->hasAttrs()) { |
| Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 13098 | CheckAlignasUnderalignment(Record); |
| Serge Pavlov | 89578fd | 2013-06-08 13:29:58 +0000 | [diff] [blame] | 13099 | |
| David Majnemer | 98c9ee2 | 2014-02-07 00:43:07 +0000 | [diff] [blame] | 13100 | if (const MSInheritanceAttr *IA = Record->getAttr<MSInheritanceAttr>()) |
| David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 13101 | checkMSInheritanceAttrOnDefinition(cast<CXXRecordDecl>(Record), |
| David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 13102 | IA->getRange(), IA->getBestCase(), |
| David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 13103 | IA->getSemanticSpelling()); |
| 13104 | } |
| 13105 | |
| Serge Pavlov | 3cb8022 | 2013-11-14 02:13:03 +0000 | [diff] [blame] | 13106 | // Check if the structure/union declaration is a type that can have zero |
| 13107 | // size in C. For C this is a language extension, for C++ it may cause |
| 13108 | // compatibility problems. |
| 13109 | bool CheckForZeroSize; |
| Serge Pavlov | 89578fd | 2013-06-08 13:29:58 +0000 | [diff] [blame] | 13110 | if (!getLangOpts().CPlusPlus) { |
| Serge Pavlov | 3cb8022 | 2013-11-14 02:13:03 +0000 | [diff] [blame] | 13111 | CheckForZeroSize = true; |
| 13112 | } else { |
| 13113 | // For C++ filter out types that cannot be referenced in C code. |
| 13114 | CXXRecordDecl *CXXRecord = cast<CXXRecordDecl>(Record); |
| 13115 | CheckForZeroSize = |
| 13116 | CXXRecord->getLexicalDeclContext()->isExternCContext() && |
| 13117 | !CXXRecord->isDependentType() && |
| 13118 | CXXRecord->isCLike(); |
| 13119 | } |
| 13120 | if (CheckForZeroSize) { |
| Serge Pavlov | 89578fd | 2013-06-08 13:29:58 +0000 | [diff] [blame] | 13121 | bool ZeroSize = true; |
| Serge Pavlov | f7c1a21 | 2013-06-17 17:18:51 +0000 | [diff] [blame] | 13122 | bool IsEmpty = true; |
| 13123 | unsigned NonBitFields = 0; |
| Serge Pavlov | 89578fd | 2013-06-08 13:29:58 +0000 | [diff] [blame] | 13124 | for (RecordDecl::field_iterator I = Record->field_begin(), |
| Serge Pavlov | f7c1a21 | 2013-06-17 17:18:51 +0000 | [diff] [blame] | 13125 | E = Record->field_end(); |
| 13126 | (NonBitFields == 0 || ZeroSize) && I != E; ++I) { |
| 13127 | IsEmpty = false; |
| Serge Pavlov | 89578fd | 2013-06-08 13:29:58 +0000 | [diff] [blame] | 13128 | if (I->isUnnamedBitfield()) { |
| Serge Pavlov | 89578fd | 2013-06-08 13:29:58 +0000 | [diff] [blame] | 13129 | if (I->getBitWidthValue(Context) > 0) |
| 13130 | ZeroSize = false; |
| 13131 | } else { |
| Serge Pavlov | f7c1a21 | 2013-06-17 17:18:51 +0000 | [diff] [blame] | 13132 | ++NonBitFields; |
| 13133 | QualType FieldType = I->getType(); |
| 13134 | if (FieldType->isIncompleteType() || |
| 13135 | !Context.getTypeSizeInChars(FieldType).isZero()) |
| 13136 | ZeroSize = false; |
| Serge Pavlov | 89578fd | 2013-06-08 13:29:58 +0000 | [diff] [blame] | 13137 | } |
| 13138 | } |
| 13139 | |
| Serge Pavlov | 3cb8022 | 2013-11-14 02:13:03 +0000 | [diff] [blame] | 13140 | // Empty structs are an extension in C (C99 6.7.2.1p7). They are |
| 13141 | // allowed in C++, but warn if its declaration is inside |
| 13142 | // extern "C" block. |
| 13143 | if (ZeroSize) { |
| 13144 | Diag(RecLoc, getLangOpts().CPlusPlus ? |
| 13145 | diag::warn_zero_size_struct_union_in_extern_c : |
| 13146 | diag::warn_zero_size_struct_union_compat) |
| 13147 | << IsEmpty << Record->isUnion() << (NonBitFields > 1); |
| 13148 | } |
| Serge Pavlov | 89578fd | 2013-06-08 13:29:58 +0000 | [diff] [blame] | 13149 | |
| Serge Pavlov | 3cb8022 | 2013-11-14 02:13:03 +0000 | [diff] [blame] | 13150 | // Structs without named members are extension in C (C99 6.7.2.1p7), |
| 13151 | // but are accepted by GCC. |
| 13152 | if (NonBitFields == 0 && !getLangOpts().CPlusPlus) { |
| 13153 | Diag(RecLoc, IsEmpty ? diag::ext_empty_struct_union : |
| 13154 | diag::ext_no_named_members_in_struct_union) |
| 13155 | << Record->isUnion(); |
| Serge Pavlov | 89578fd | 2013-06-08 13:29:58 +0000 | [diff] [blame] | 13156 | } |
| 13157 | } |
| Chris Lattner | 622c193 | 2008-02-06 00:51:33 +0000 | [diff] [blame] | 13158 | } else { |
| Jay Foad | 7d0479f | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 13159 | ObjCIvarDecl **ClsFields = |
| 13160 | reinterpret_cast<ObjCIvarDecl**>(RecFields.data()); |
| Fariborz Jahanian | 0222553 | 2008-12-13 20:28:25 +0000 | [diff] [blame] | 13161 | if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(EnclosingDecl)) { |
| Douglas Gregor | 1640832 | 2011-12-15 22:34:59 +0000 | [diff] [blame] | 13162 | ID->setEndOfDefinitionLoc(RBrac); |
| Fariborz Jahanian | 6845383 | 2009-06-05 18:16:35 +0000 | [diff] [blame] | 13163 | // Add ivar's to class's DeclContext. |
| 13164 | for (unsigned i = 0, e = RecFields.size(); i != e; ++i) { |
| 13165 | ClsFields[i]->setLexicalDeclContext(ID); |
| Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 13166 | ID->addDecl(ClsFields[i]); |
| Fariborz Jahanian | 6845383 | 2009-06-05 18:16:35 +0000 | [diff] [blame] | 13167 | } |
| Fariborz Jahanian | a599c13 | 2008-12-16 01:08:35 +0000 | [diff] [blame] | 13168 | // Must enforce the rule that ivars in the base classes may not be |
| 13169 | // duplicates. |
| Fariborz Jahanian | 545643c | 2010-02-23 23:41:11 +0000 | [diff] [blame] | 13170 | if (ID->getSuperClass()) |
| 13171 | DiagnoseDuplicateIvars(ID, ID->getSuperClass()); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 13172 | } else if (ObjCImplementationDecl *IMPDecl = |
| Chris Lattner | d13b8b5 | 2009-02-23 22:00:08 +0000 | [diff] [blame] | 13173 | dyn_cast<ObjCImplementationDecl>(EnclosingDecl)) { |
| Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 13174 | assert(IMPDecl && "ActOnFields - missing ObjCImplementationDecl"); |
| Fariborz Jahanian | 6845383 | 2009-06-05 18:16:35 +0000 | [diff] [blame] | 13175 | for (unsigned I = 0, N = RecFields.size(); I != N; ++I) |
| 13176 | // Ivar declared in @implementation never belongs to the implementation. |
| 13177 | // Only it is in implementation's lexical context. |
| Douglas Gregor | 5f66205 | 2009-04-23 03:23:08 +0000 | [diff] [blame] | 13178 | ClsFields[I]->setLexicalDeclContext(IMPDecl); |
| Fariborz Jahanian | 95b6076 | 2007-10-31 18:48:14 +0000 | [diff] [blame] | 13179 | CheckImplementationIvars(IMPDecl, ClsFields, RecFields.size(), RBrac); |
| Fariborz Jahanian | a7765fe | 2012-02-20 20:09:20 +0000 | [diff] [blame] | 13180 | IMPDecl->setIvarLBraceLoc(LBrac); |
| 13181 | IMPDecl->setIvarRBraceLoc(RBrac); |
| Fariborz Jahanian | 4c172c6 | 2010-02-22 23:04:20 +0000 | [diff] [blame] | 13182 | } else if (ObjCCategoryDecl *CDecl = |
| 13183 | dyn_cast<ObjCCategoryDecl>(EnclosingDecl)) { |
| Fariborz Jahanian | 6a0a2e0 | 2010-04-06 22:43:48 +0000 | [diff] [blame] | 13184 | // case of ivars in class extension; all other cases have been |
| 13185 | // reported as errors elsewhere. |
| 13186 | // FIXME. Class extension does not have a LocEnd field. |
| 13187 | // CDecl->setLocEnd(RBrac); |
| 13188 | // Add ivar's to class extension's DeclContext. |
| Fariborz Jahanian | 2512747 | 2011-10-21 18:03:52 +0000 | [diff] [blame] | 13189 | // Diagnose redeclaration of private ivars. |
| 13190 | ObjCInterfaceDecl *IDecl = CDecl->getClassInterface(); |
| Fariborz Jahanian | 6a0a2e0 | 2010-04-06 22:43:48 +0000 | [diff] [blame] | 13191 | for (unsigned i = 0, e = RecFields.size(); i != e; ++i) { |
| Fariborz Jahanian | 2512747 | 2011-10-21 18:03:52 +0000 | [diff] [blame] | 13192 | if (IDecl) { |
| 13193 | if (const ObjCIvarDecl *ClsIvar = |
| 13194 | IDecl->getIvarDecl(ClsFields[i]->getIdentifier())) { |
| 13195 | Diag(ClsFields[i]->getLocation(), |
| 13196 | diag::err_duplicate_ivar_declaration); |
| 13197 | Diag(ClsIvar->getLocation(), diag::note_previous_definition); |
| 13198 | continue; |
| 13199 | } |
| Aaron Ballman | b4a5345 | 2014-03-13 21:57:01 +0000 | [diff] [blame] | 13200 | for (const auto *Ext : IDecl->known_extensions()) { |
| Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 13201 | if (const ObjCIvarDecl *ClsExtIvar |
| 13202 | = Ext->getIvarDecl(ClsFields[i]->getIdentifier())) { |
| Fariborz Jahanian | 2512747 | 2011-10-21 18:03:52 +0000 | [diff] [blame] | 13203 | Diag(ClsFields[i]->getLocation(), |
| 13204 | diag::err_duplicate_ivar_declaration); |
| 13205 | Diag(ClsExtIvar->getLocation(), diag::note_previous_definition); |
| 13206 | continue; |
| 13207 | } |
| 13208 | } |
| 13209 | } |
| Fariborz Jahanian | 6a0a2e0 | 2010-04-06 22:43:48 +0000 | [diff] [blame] | 13210 | ClsFields[i]->setLexicalDeclContext(CDecl); |
| 13211 | CDecl->addDecl(ClsFields[i]); |
| Fariborz Jahanian | 4c172c6 | 2010-02-22 23:04:20 +0000 | [diff] [blame] | 13212 | } |
| Fariborz Jahanian | a7765fe | 2012-02-20 20:09:20 +0000 | [diff] [blame] | 13213 | CDecl->setIvarLBraceLoc(LBrac); |
| 13214 | CDecl->setIvarRBraceLoc(RBrac); |
| Fariborz Jahanian | 2a4dd31 | 2007-09-26 18:27:25 +0000 | [diff] [blame] | 13215 | } |
| Fariborz Jahanian | f3287bf | 2007-09-14 21:08:27 +0000 | [diff] [blame] | 13216 | } |
| Daniel Dunbar | 325601a | 2008-10-03 17:33:35 +0000 | [diff] [blame] | 13217 | |
| 13218 | if (Attr) |
| Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 13219 | ProcessDeclAttributeList(S, Record, Attr); |
| Chris Lattner | 1300fb9 | 2007-01-23 23:42:53 +0000 | [diff] [blame] | 13220 | } |
| 13221 | |
| Douglas Gregor | 6791a0d | 2010-02-01 23:36:03 +0000 | [diff] [blame] | 13222 | /// \brief Determine whether the given integral value is representable within |
| 13223 | /// the given type T. |
| 13224 | static bool isRepresentableIntegerValue(ASTContext &Context, |
| 13225 | llvm::APSInt &Value, |
| 13226 | QualType T) { |
| Douglas Gregor | 6972a62 | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 13227 | assert(T->isIntegralType(Context) && "Integral type required!"); |
| Douglas Gregor | c1cf814 | 2010-04-15 15:53:31 +0000 | [diff] [blame] | 13228 | unsigned BitWidth = Context.getIntWidth(T); |
| Douglas Gregor | 6791a0d | 2010-02-01 23:36:03 +0000 | [diff] [blame] | 13229 | |
| Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 13230 | if (Value.isUnsigned() || Value.isNonNegative()) { |
| Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 13231 | if (T->isSignedIntegerOrEnumerationType()) |
| Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 13232 | --BitWidth; |
| 13233 | return Value.getActiveBits() <= BitWidth; |
| 13234 | } |
| Douglas Gregor | 6791a0d | 2010-02-01 23:36:03 +0000 | [diff] [blame] | 13235 | return Value.getMinSignedBits() <= BitWidth; |
| 13236 | } |
| 13237 | |
| 13238 | // \brief Given an integral type, return the next larger integral type |
| 13239 | // (or a NULL type of no such type exists). |
| 13240 | static QualType getNextLargerIntegralType(ASTContext &Context, QualType T) { |
| 13241 | // FIXME: Int128/UInt128 support, which also needs to be introduced into |
| 13242 | // enum checking below. |
| Douglas Gregor | 6972a62 | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 13243 | assert(T->isIntegralType(Context) && "Integral type required!"); |
| Douglas Gregor | 6791a0d | 2010-02-01 23:36:03 +0000 | [diff] [blame] | 13244 | const unsigned NumTypes = 4; |
| 13245 | QualType SignedIntegralTypes[NumTypes] = { |
| 13246 | Context.ShortTy, Context.IntTy, Context.LongTy, Context.LongLongTy |
| 13247 | }; |
| 13248 | QualType UnsignedIntegralTypes[NumTypes] = { |
| 13249 | Context.UnsignedShortTy, Context.UnsignedIntTy, Context.UnsignedLongTy, |
| 13250 | Context.UnsignedLongLongTy |
| 13251 | }; |
| 13252 | |
| 13253 | unsigned BitWidth = Context.getTypeSize(T); |
| Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 13254 | QualType *Types = T->isSignedIntegerOrEnumerationType()? SignedIntegralTypes |
| 13255 | : UnsignedIntegralTypes; |
| Douglas Gregor | 6791a0d | 2010-02-01 23:36:03 +0000 | [diff] [blame] | 13256 | for (unsigned I = 0; I != NumTypes; ++I) |
| 13257 | if (Context.getTypeSize(Types[I]) > BitWidth) |
| 13258 | return Types[I]; |
| 13259 | |
| 13260 | return QualType(); |
| 13261 | } |
| 13262 | |
| Douglas Gregor | 954f6b27 | 2009-03-17 19:05:46 +0000 | [diff] [blame] | 13263 | EnumConstantDecl *Sema::CheckEnumConstant(EnumDecl *Enum, |
| 13264 | EnumConstantDecl *LastEnumConst, |
| 13265 | SourceLocation IdLoc, |
| 13266 | IdentifierInfo *Id, |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 13267 | Expr *Val) { |
| Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 13268 | unsigned IntWidth = Context.getTargetInfo().getIntWidth(); |
| Douglas Gregor | 6791a0d | 2010-02-01 23:36:03 +0000 | [diff] [blame] | 13269 | llvm::APSInt EnumVal(IntWidth); |
| Douglas Gregor | 954f6b27 | 2009-03-17 19:05:46 +0000 | [diff] [blame] | 13270 | QualType EltTy; |
| Douglas Gregor | 2b988fd | 2010-12-16 00:24:44 +0000 | [diff] [blame] | 13271 | |
| 13272 | if (Val && DiagnoseUnexpandedParameterPack(Val, UPPC_EnumeratorValue)) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 13273 | Val = nullptr; |
| Douglas Gregor | 2b988fd | 2010-12-16 00:24:44 +0000 | [diff] [blame] | 13274 | |
| Eli Friedman | 7c6515a | 2011-12-06 00:10:34 +0000 | [diff] [blame] | 13275 | if (Val) |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 13276 | Val = DefaultLvalueConversion(Val).get(); |
| Eli Friedman | 7c6515a | 2011-12-06 00:10:34 +0000 | [diff] [blame] | 13277 | |
| Douglas Gregor | b2186fe | 2009-11-06 00:03:12 +0000 | [diff] [blame] | 13278 | if (Val) { |
| Douglas Gregor | dc70c3a | 2010-03-02 17:53:14 +0000 | [diff] [blame] | 13279 | if (Enum->isDependentType() || Val->isTypeDependent()) |
| Douglas Gregor | b2186fe | 2009-11-06 00:03:12 +0000 | [diff] [blame] | 13280 | EltTy = Context.DependentTy; |
| 13281 | else { |
| Douglas Gregor | b2186fe | 2009-11-06 00:03:12 +0000 | [diff] [blame] | 13282 | SourceLocation ExpLoc; |
| Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 13283 | if (getLangOpts().CPlusPlus11 && Enum->isFixed() && |
| Alp Toker | bfa3934 | 2014-01-14 12:51:41 +0000 | [diff] [blame] | 13284 | !getLangOpts().MSVCCompat) { |
| Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 13285 | // C++11 [dcl.enum]p5: If the underlying type is fixed, [...] the |
| 13286 | // constant-expression in the enumerator-definition shall be a converted |
| 13287 | // constant expression of the underlying type. |
| 13288 | EltTy = Enum->getIntegerType(); |
| 13289 | ExprResult Converted = |
| 13290 | CheckConvertedConstantExpression(Val, EltTy, EnumVal, |
| 13291 | CCEK_Enumerator); |
| 13292 | if (Converted.isInvalid()) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 13293 | Val = nullptr; |
| Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 13294 | else |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 13295 | Val = Converted.get(); |
| Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 13296 | } else if (!Val->isValueDependent() && |
| Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 13297 | !(Val = VerifyIntegerConstantExpression(Val, |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 13298 | &EnumVal).get())) { |
| Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 13299 | // 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] | 13300 | } else { |
| Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 13301 | if (Enum->isFixed()) { |
| 13302 | EltTy = Enum->getIntegerType(); |
| 13303 | |
| Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 13304 | // In Obj-C and Microsoft mode, require the enumeration value to be |
| 13305 | // representable in the underlying type of the enumeration. In C++11, |
| 13306 | // we perform a non-narrowing conversion as part of converted constant |
| 13307 | // expression checking. |
| Francois Pichet | a310806 | 2010-10-18 15:01:13 +0000 | [diff] [blame] | 13308 | if (!isRepresentableIntegerValue(Context, EnumVal, EltTy)) { |
| Alp Toker | bfa3934 | 2014-01-14 12:51:41 +0000 | [diff] [blame] | 13309 | if (getLangOpts().MSVCCompat) { |
| Francois Pichet | a310806 | 2010-10-18 15:01:13 +0000 | [diff] [blame] | 13310 | Diag(IdLoc, diag::ext_enumerator_too_large) << EltTy; |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 13311 | Val = ImpCastExprToType(Val, EltTy, CK_IntegralCast).get(); |
| Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 13312 | } else |
| 13313 | Diag(IdLoc, diag::err_enumerator_too_large) << EltTy; |
| Francois Pichet | a310806 | 2010-10-18 15:01:13 +0000 | [diff] [blame] | 13314 | } else |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 13315 | Val = ImpCastExprToType(Val, EltTy, CK_IntegralCast).get(); |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 13316 | } else if (getLangOpts().CPlusPlus) { |
| Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 13317 | // C++11 [dcl.enum]p5: |
| Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 13318 | // If the underlying type is not fixed, the type of each enumerator |
| 13319 | // is the type of its initializing value: |
| 13320 | // - If an initializer is specified for an enumerator, the |
| 13321 | // initializing value has the same type as the expression. |
| 13322 | EltTy = Val->getType(); |
| Eli Friedman | 2beed11 | 2012-02-07 04:34:38 +0000 | [diff] [blame] | 13323 | } else { |
| 13324 | // C99 6.7.2.2p2: |
| 13325 | // The expression that defines the value of an enumeration constant |
| 13326 | // shall be an integer constant expression that has a value |
| 13327 | // representable as an int. |
| 13328 | |
| 13329 | // Complain if the value is not representable in an int. |
| 13330 | if (!isRepresentableIntegerValue(Context, EnumVal, Context.IntTy)) |
| 13331 | Diag(IdLoc, diag::ext_enum_value_not_int) |
| 13332 | << EnumVal.toString(10) << Val->getSourceRange() |
| 13333 | << (EnumVal.isUnsigned() || EnumVal.isNonNegative()); |
| 13334 | else if (!Context.hasSameType(Val->getType(), Context.IntTy)) { |
| 13335 | // Force the type of the expression to 'int'. |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 13336 | Val = ImpCastExprToType(Val, Context.IntTy, CK_IntegralCast).get(); |
| Eli Friedman | 2beed11 | 2012-02-07 04:34:38 +0000 | [diff] [blame] | 13337 | } |
| 13338 | EltTy = Val->getType(); |
| Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 13339 | } |
| Douglas Gregor | b2186fe | 2009-11-06 00:03:12 +0000 | [diff] [blame] | 13340 | } |
| Douglas Gregor | 954f6b27 | 2009-03-17 19:05:46 +0000 | [diff] [blame] | 13341 | } |
| 13342 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 13343 | |
| Douglas Gregor | 954f6b27 | 2009-03-17 19:05:46 +0000 | [diff] [blame] | 13344 | if (!Val) { |
| Eli Friedman | d0e6097 | 2009-12-11 01:34:50 +0000 | [diff] [blame] | 13345 | if (Enum->isDependentType()) |
| 13346 | EltTy = Context.DependentTy; |
| Douglas Gregor | 6791a0d | 2010-02-01 23:36:03 +0000 | [diff] [blame] | 13347 | else if (!LastEnumConst) { |
| 13348 | // C++0x [dcl.enum]p5: |
| 13349 | // If the underlying type is not fixed, the type of each enumerator |
| 13350 | // is the type of its initializing value: |
| 13351 | // - If no initializer is specified for the first enumerator, the |
| 13352 | // initializing value has an unspecified integral type. |
| 13353 | // |
| 13354 | // GCC uses 'int' for its unspecified integral type, as does |
| 13355 | // C99 6.7.2.2p3. |
| Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 13356 | if (Enum->isFixed()) { |
| 13357 | EltTy = Enum->getIntegerType(); |
| 13358 | } |
| 13359 | else { |
| 13360 | EltTy = Context.IntTy; |
| 13361 | } |
| Douglas Gregor | 6791a0d | 2010-02-01 23:36:03 +0000 | [diff] [blame] | 13362 | } else { |
| Douglas Gregor | 954f6b27 | 2009-03-17 19:05:46 +0000 | [diff] [blame] | 13363 | // Assign the last value + 1. |
| 13364 | EnumVal = LastEnumConst->getInitVal(); |
| 13365 | ++EnumVal; |
| Douglas Gregor | 6791a0d | 2010-02-01 23:36:03 +0000 | [diff] [blame] | 13366 | EltTy = LastEnumConst->getType(); |
| Douglas Gregor | 954f6b27 | 2009-03-17 19:05:46 +0000 | [diff] [blame] | 13367 | |
| 13368 | // Check for overflow on increment. |
| Douglas Gregor | 6791a0d | 2010-02-01 23:36:03 +0000 | [diff] [blame] | 13369 | if (EnumVal < LastEnumConst->getInitVal()) { |
| 13370 | // C++0x [dcl.enum]p5: |
| 13371 | // If the underlying type is not fixed, the type of each enumerator |
| 13372 | // is the type of its initializing value: |
| 13373 | // |
| 13374 | // - Otherwise the type of the initializing value is the same as |
| 13375 | // the type of the initializing value of the preceding enumerator |
| 13376 | // unless the incremented value is not representable in that type, |
| 13377 | // in which case the type is an unspecified integral type |
| 13378 | // sufficient to contain the incremented value. If no such type |
| 13379 | // exists, the program is ill-formed. |
| 13380 | QualType T = getNextLargerIntegralType(Context, EltTy); |
| Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 13381 | if (T.isNull() || Enum->isFixed()) { |
| Douglas Gregor | 6791a0d | 2010-02-01 23:36:03 +0000 | [diff] [blame] | 13382 | // There is no integral type larger enough to represent this |
| 13383 | // value. Complain, then allow the value to wrap around. |
| 13384 | EnumVal = LastEnumConst->getInitVal(); |
| Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 13385 | EnumVal = EnumVal.zext(EnumVal.getBitWidth() * 2); |
| Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 13386 | ++EnumVal; |
| 13387 | if (Enum->isFixed()) |
| 13388 | // When the underlying type is fixed, this is ill-formed. |
| 13389 | Diag(IdLoc, diag::err_enumerator_wrapped) |
| 13390 | << EnumVal.toString(10) |
| 13391 | << EltTy; |
| 13392 | else |
| Richard Smith | faf156a | 2014-03-05 22:54:58 +0000 | [diff] [blame] | 13393 | Diag(IdLoc, diag::ext_enumerator_increment_too_large) |
| Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 13394 | << EnumVal.toString(10); |
| Douglas Gregor | 6791a0d | 2010-02-01 23:36:03 +0000 | [diff] [blame] | 13395 | } else { |
| 13396 | EltTy = T; |
| 13397 | } |
| 13398 | |
| 13399 | // Retrieve the last enumerator's value, extent that type to the |
| 13400 | // type that is supposed to be large enough to represent the incremented |
| 13401 | // value, then increment. |
| 13402 | EnumVal = LastEnumConst->getInitVal(); |
| Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 13403 | EnumVal.setIsSigned(EltTy->isSignedIntegerOrEnumerationType()); |
| Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 13404 | EnumVal = EnumVal.zextOrTrunc(Context.getIntWidth(EltTy)); |
| Douglas Gregor | 6791a0d | 2010-02-01 23:36:03 +0000 | [diff] [blame] | 13405 | ++EnumVal; |
| 13406 | |
| 13407 | // If we're not in C++, diagnose the overflow of enumerator values, |
| 13408 | // which in C99 means that the enumerator value is not representable in |
| 13409 | // an int (C99 6.7.2.2p2). However, we support GCC's extension that |
| 13410 | // permits enumerator values that are representable in some larger |
| 13411 | // integral type. |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 13412 | if (!getLangOpts().CPlusPlus && !T.isNull()) |
| Douglas Gregor | 6791a0d | 2010-02-01 23:36:03 +0000 | [diff] [blame] | 13413 | Diag(IdLoc, diag::warn_enum_value_overflow); |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 13414 | } else if (!getLangOpts().CPlusPlus && |
| Douglas Gregor | 6791a0d | 2010-02-01 23:36:03 +0000 | [diff] [blame] | 13415 | !isRepresentableIntegerValue(Context, EnumVal, EltTy)) { |
| 13416 | // Enforce C99 6.7.2.2p2 even when we compute the next value. |
| 13417 | Diag(IdLoc, diag::ext_enum_value_not_int) |
| 13418 | << EnumVal.toString(10) << 1; |
| 13419 | } |
| Douglas Gregor | 954f6b27 | 2009-03-17 19:05:46 +0000 | [diff] [blame] | 13420 | } |
| 13421 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 13422 | |
| Douglas Gregor | dc70c3a | 2010-03-02 17:53:14 +0000 | [diff] [blame] | 13423 | if (!EltTy->isDependentType()) { |
| Douglas Gregor | 6791a0d | 2010-02-01 23:36:03 +0000 | [diff] [blame] | 13424 | // Make the enumerator value match the signedness and size of the |
| 13425 | // enumerator's type. |
| Eli Friedman | 2beed11 | 2012-02-07 04:34:38 +0000 | [diff] [blame] | 13426 | EnumVal = EnumVal.extOrTrunc(Context.getIntWidth(EltTy)); |
| Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 13427 | EnumVal.setIsSigned(EltTy->isSignedIntegerOrEnumerationType()); |
| Douglas Gregor | 6791a0d | 2010-02-01 23:36:03 +0000 | [diff] [blame] | 13428 | } |
| Douglas Gregor | b2186fe | 2009-11-06 00:03:12 +0000 | [diff] [blame] | 13429 | |
| Douglas Gregor | 954f6b27 | 2009-03-17 19:05:46 +0000 | [diff] [blame] | 13430 | return EnumConstantDecl::Create(Context, Enum, IdLoc, Id, EltTy, |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 13431 | Val, EnumVal); |
| Douglas Gregor | 954f6b27 | 2009-03-17 19:05:46 +0000 | [diff] [blame] | 13432 | } |
| 13433 | |
| 13434 | |
| John McCall | 811a0f5 | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 13435 | Decl *Sema::ActOnEnumConstant(Scope *S, Decl *theEnumDecl, Decl *lastEnumConst, |
| 13436 | SourceLocation IdLoc, IdentifierInfo *Id, |
| 13437 | AttributeList *Attr, |
| Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 13438 | SourceLocation EqualLoc, Expr *Val) { |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 13439 | EnumDecl *TheEnumDecl = cast<EnumDecl>(theEnumDecl); |
| Chris Lattner | 4ef4001 | 2007-06-11 01:28:17 +0000 | [diff] [blame] | 13440 | EnumConstantDecl *LastEnumConst = |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 13441 | cast_or_null<EnumConstantDecl>(lastEnumConst); |
| Chris Lattner | 8116d1b | 2007-01-25 22:38:29 +0000 | [diff] [blame] | 13442 | |
| Chris Lattner | 1a76a3c | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 13443 | // The scope passed in may not be a decl scope. Zip up the scope tree until |
| 13444 | // we find one that is. |
| Douglas Gregor | 45a33ec | 2009-01-12 18:45:55 +0000 | [diff] [blame] | 13445 | S = getNonFieldDeclScope(S); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 13446 | |
| Chris Lattner | 8116d1b | 2007-01-25 22:38:29 +0000 | [diff] [blame] | 13447 | // Verify that there isn't already something declared with this name in this |
| 13448 | // scope. |
| Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 13449 | NamedDecl *PrevDecl = LookupSingleName(S, Id, IdLoc, LookupOrdinaryName, |
| Douglas Gregor | 5204248f | 2010-01-19 06:06:57 +0000 | [diff] [blame] | 13450 | ForRedeclaration); |
| Douglas Gregor | 5daeee2 | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 13451 | if (PrevDecl && PrevDecl->isTemplateParameter()) { |
| Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 13452 | // Maybe we will complain about the shadowed template parameter. |
| 13453 | DiagnoseTemplateParameterShadow(IdLoc, PrevDecl); |
| 13454 | // Just pretend that we didn't see the previous declaration. |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 13455 | PrevDecl = nullptr; |
| Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 13456 | } |
| 13457 | |
| 13458 | if (PrevDecl) { |
| Argyrios Kyrtzidis | bd25998 | 2008-07-16 21:01:53 +0000 | [diff] [blame] | 13459 | // When in C++, we may get a TagDecl with the same name; in this case the |
| 13460 | // enum constant will 'hide' the tag. |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 13461 | assert((getLangOpts().CPlusPlus || !isa<TagDecl>(PrevDecl)) && |
| Argyrios Kyrtzidis | bd25998 | 2008-07-16 21:01:53 +0000 | [diff] [blame] | 13462 | "Received TagDecl when not in C++!"); |
| Argyrios Kyrtzidis | 5b144d5 | 2008-09-09 21:18:04 +0000 | [diff] [blame] | 13463 | if (!isa<TagDecl>(PrevDecl) && isDeclInScope(PrevDecl, CurContext, S)) { |
| Chris Lattner | 8116d1b | 2007-01-25 22:38:29 +0000 | [diff] [blame] | 13464 | if (isa<EnumConstantDecl>(PrevDecl)) |
| Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 13465 | Diag(IdLoc, diag::err_redefinition_of_enumerator) << Id; |
| Chris Lattner | 8116d1b | 2007-01-25 22:38:29 +0000 | [diff] [blame] | 13466 | else |
| Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 13467 | Diag(IdLoc, diag::err_redefinition) << Id; |
| Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 13468 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 13469 | return nullptr; |
| Chris Lattner | 8116d1b | 2007-01-25 22:38:29 +0000 | [diff] [blame] | 13470 | } |
| 13471 | } |
| Chris Lattner | 4ef4001 | 2007-06-11 01:28:17 +0000 | [diff] [blame] | 13472 | |
| Aaron Ballman | 24a1047 | 2012-07-19 03:12:23 +0000 | [diff] [blame] | 13473 | // C++ [class.mem]p15: |
| 13474 | // If T is the name of a class, then each of the following shall have a name |
| 13475 | // different from T: |
| 13476 | // - every enumerator of every member of class T that is an unscoped |
| 13477 | // enumerated type |
| Douglas Gregor | 36c22a2 | 2010-10-15 13:21:21 +0000 | [diff] [blame] | 13478 | if (CXXRecordDecl *Record |
| 13479 | = dyn_cast<CXXRecordDecl>( |
| 13480 | TheEnumDecl->getDeclContext()->getRedeclContext())) |
| Aaron Ballman | 24a1047 | 2012-07-19 03:12:23 +0000 | [diff] [blame] | 13481 | if (!TheEnumDecl->isScoped() && |
| 13482 | Record->getIdentifier() && Record->getIdentifier() == Id) |
| Douglas Gregor | 36c22a2 | 2010-10-15 13:21:21 +0000 | [diff] [blame] | 13483 | Diag(IdLoc, diag::err_member_name_of_class) << Id; |
| 13484 | |
| John McCall | 811a0f5 | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 13485 | EnumConstantDecl *New = |
| 13486 | CheckEnumConstant(TheEnumDecl, LastEnumConst, IdLoc, Id, Val); |
| Chris Lattner | 0515e4b | 2007-08-27 21:16:18 +0000 | [diff] [blame] | 13487 | |
| John McCall | 553c079 | 2010-01-23 00:46:32 +0000 | [diff] [blame] | 13488 | if (New) { |
| John McCall | 811a0f5 | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 13489 | // Process attributes. |
| 13490 | if (Attr) ProcessDeclAttributeList(S, New, Attr); |
| 13491 | |
| 13492 | // Register this decl in the current scope stack. |
| John McCall | 553c079 | 2010-01-23 00:46:32 +0000 | [diff] [blame] | 13493 | New->setAccess(TheEnumDecl->getAccess()); |
| Douglas Gregor | 954f6b27 | 2009-03-17 19:05:46 +0000 | [diff] [blame] | 13494 | PushOnScopeChains(New, S); |
| John McCall | 553c079 | 2010-01-23 00:46:32 +0000 | [diff] [blame] | 13495 | } |
| Douglas Gregor | 2f52119 | 2008-12-17 02:04:30 +0000 | [diff] [blame] | 13496 | |
| Dmitri Gribenko | f26054f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 13497 | ActOnDocumentableDecl(New); |
| 13498 | |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 13499 | return New; |
| Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 13500 | } |
| 13501 | |
| Ted Kremenek | 6cae9ec | 2012-12-22 01:34:09 +0000 | [diff] [blame] | 13502 | // Returns true when the enum initial expression does not trigger the |
| 13503 | // duplicate enum warning. A few common cases are exempted as follows: |
| 13504 | // Element2 = Element1 |
| 13505 | // Element2 = Element1 + 1 |
| 13506 | // Element2 = Element1 - 1 |
| 13507 | // Where Element2 and Element1 are from the same enum. |
| 13508 | static bool ValidDuplicateEnum(EnumConstantDecl *ECD, EnumDecl *Enum) { |
| 13509 | Expr *InitExpr = ECD->getInitExpr(); |
| 13510 | if (!InitExpr) |
| 13511 | return true; |
| 13512 | InitExpr = InitExpr->IgnoreImpCasts(); |
| 13513 | |
| 13514 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(InitExpr)) { |
| 13515 | if (!BO->isAdditiveOp()) |
| 13516 | return true; |
| 13517 | IntegerLiteral *IL = dyn_cast<IntegerLiteral>(BO->getRHS()); |
| 13518 | if (!IL) |
| 13519 | return true; |
| 13520 | if (IL->getValue() != 1) |
| 13521 | return true; |
| 13522 | |
| 13523 | InitExpr = BO->getLHS(); |
| 13524 | } |
| 13525 | |
| 13526 | // This checks if the elements are from the same enum. |
| 13527 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(InitExpr); |
| 13528 | if (!DRE) |
| 13529 | return true; |
| 13530 | |
| 13531 | EnumConstantDecl *EnumConstant = dyn_cast<EnumConstantDecl>(DRE->getDecl()); |
| 13532 | if (!EnumConstant) |
| 13533 | return true; |
| 13534 | |
| 13535 | if (cast<EnumDecl>(TagDecl::castFromDeclContext(ECD->getDeclContext())) != |
| 13536 | Enum) |
| 13537 | return true; |
| 13538 | |
| 13539 | return false; |
| 13540 | } |
| 13541 | |
| 13542 | struct DupKey { |
| 13543 | int64_t val; |
| 13544 | bool isTombstoneOrEmptyKey; |
| 13545 | DupKey(int64_t val, bool isTombstoneOrEmptyKey) |
| 13546 | : val(val), isTombstoneOrEmptyKey(isTombstoneOrEmptyKey) {} |
| 13547 | }; |
| 13548 | |
| 13549 | static DupKey GetDupKey(const llvm::APSInt& Val) { |
| 13550 | return DupKey(Val.isSigned() ? Val.getSExtValue() : Val.getZExtValue(), |
| 13551 | false); |
| 13552 | } |
| 13553 | |
| 13554 | struct DenseMapInfoDupKey { |
| 13555 | static DupKey getEmptyKey() { return DupKey(0, true); } |
| 13556 | static DupKey getTombstoneKey() { return DupKey(1, true); } |
| 13557 | static unsigned getHashValue(const DupKey Key) { |
| 13558 | return (unsigned)(Key.val * 37); |
| 13559 | } |
| 13560 | static bool isEqual(const DupKey& LHS, const DupKey& RHS) { |
| 13561 | return LHS.isTombstoneOrEmptyKey == RHS.isTombstoneOrEmptyKey && |
| 13562 | LHS.val == RHS.val; |
| 13563 | } |
| 13564 | }; |
| 13565 | |
| 13566 | // Emits a warning when an element is implicitly set a value that |
| 13567 | // a previous element has already been set to. |
| Dmitri Gribenko | e5fde99 | 2013-04-27 20:23:52 +0000 | [diff] [blame] | 13568 | static void CheckForDuplicateEnumValues(Sema &S, ArrayRef<Decl *> Elements, |
| 13569 | EnumDecl *Enum, |
| Ted Kremenek | 6cae9ec | 2012-12-22 01:34:09 +0000 | [diff] [blame] | 13570 | QualType EnumType) { |
| Alp Toker | d4a3f0e | 2014-06-15 23:30:39 +0000 | [diff] [blame] | 13571 | if (S.Diags.isIgnored(diag::warn_duplicate_enum_values, Enum->getLocation())) |
| Ted Kremenek | 6cae9ec | 2012-12-22 01:34:09 +0000 | [diff] [blame] | 13572 | return; |
| 13573 | // Avoid anonymous enums |
| 13574 | if (!Enum->getIdentifier()) |
| 13575 | return; |
| 13576 | |
| 13577 | // Only check for small enums. |
| 13578 | if (Enum->getNumPositiveBits() > 63 || Enum->getNumNegativeBits() > 64) |
| 13579 | return; |
| 13580 | |
| Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 13581 | typedef SmallVector<EnumConstantDecl *, 3> ECDVector; |
| 13582 | typedef SmallVector<ECDVector *, 3> DuplicatesVector; |
| Ted Kremenek | 6cae9ec | 2012-12-22 01:34:09 +0000 | [diff] [blame] | 13583 | |
| 13584 | typedef llvm::PointerUnion<EnumConstantDecl*, ECDVector*> DeclOrVector; |
| 13585 | typedef llvm::DenseMap<DupKey, DeclOrVector, DenseMapInfoDupKey> |
| 13586 | ValueToVectorMap; |
| 13587 | |
| 13588 | DuplicatesVector DupVector; |
| 13589 | ValueToVectorMap EnumMap; |
| 13590 | |
| 13591 | // Populate the EnumMap with all values represented by enum constants without |
| 13592 | // an initialier. |
| Dmitri Gribenko | e5fde99 | 2013-04-27 20:23:52 +0000 | [diff] [blame] | 13593 | for (unsigned i = 0, e = Elements.size(); i != e; ++i) { |
| Benjamin Kramer | 5c488ef | 2013-04-07 14:10:40 +0000 | [diff] [blame] | 13594 | EnumConstantDecl *ECD = cast_or_null<EnumConstantDecl>(Elements[i]); |
| Ted Kremenek | 6cae9ec | 2012-12-22 01:34:09 +0000 | [diff] [blame] | 13595 | |
| 13596 | // Null EnumConstantDecl means a previous diagnostic has been emitted for |
| 13597 | // this constant. Skip this enum since it may be ill-formed. |
| 13598 | if (!ECD) { |
| 13599 | return; |
| 13600 | } |
| 13601 | |
| 13602 | if (ECD->getInitExpr()) |
| 13603 | continue; |
| 13604 | |
| 13605 | DupKey Key = GetDupKey(ECD->getInitVal()); |
| 13606 | DeclOrVector &Entry = EnumMap[Key]; |
| 13607 | |
| 13608 | // First time encountering this value. |
| 13609 | if (Entry.isNull()) |
| 13610 | Entry = ECD; |
| 13611 | } |
| 13612 | |
| 13613 | // Create vectors for any values that has duplicates. |
| Dmitri Gribenko | e5fde99 | 2013-04-27 20:23:52 +0000 | [diff] [blame] | 13614 | for (unsigned i = 0, e = Elements.size(); i != e; ++i) { |
| Ted Kremenek | 6cae9ec | 2012-12-22 01:34:09 +0000 | [diff] [blame] | 13615 | EnumConstantDecl *ECD = cast<EnumConstantDecl>(Elements[i]); |
| 13616 | if (!ValidDuplicateEnum(ECD, Enum)) |
| 13617 | continue; |
| 13618 | |
| 13619 | DupKey Key = GetDupKey(ECD->getInitVal()); |
| 13620 | |
| 13621 | DeclOrVector& Entry = EnumMap[Key]; |
| 13622 | if (Entry.isNull()) |
| 13623 | continue; |
| 13624 | |
| 13625 | if (EnumConstantDecl *D = Entry.dyn_cast<EnumConstantDecl*>()) { |
| 13626 | // Ensure constants are different. |
| 13627 | if (D == ECD) |
| 13628 | continue; |
| 13629 | |
| 13630 | // Create new vector and push values onto it. |
| 13631 | ECDVector *Vec = new ECDVector(); |
| 13632 | Vec->push_back(D); |
| 13633 | Vec->push_back(ECD); |
| 13634 | |
| 13635 | // Update entry to point to the duplicates vector. |
| 13636 | Entry = Vec; |
| 13637 | |
| 13638 | // Store the vector somewhere we can consult later for quick emission of |
| 13639 | // diagnostics. |
| 13640 | DupVector.push_back(Vec); |
| 13641 | continue; |
| 13642 | } |
| 13643 | |
| 13644 | ECDVector *Vec = Entry.get<ECDVector*>(); |
| 13645 | // Make sure constants are not added more than once. |
| 13646 | if (*Vec->begin() == ECD) |
| 13647 | continue; |
| 13648 | |
| 13649 | Vec->push_back(ECD); |
| 13650 | } |
| 13651 | |
| 13652 | // Emit diagnostics. |
| 13653 | for (DuplicatesVector::iterator DupVectorIter = DupVector.begin(), |
| 13654 | DupVectorEnd = DupVector.end(); |
| 13655 | DupVectorIter != DupVectorEnd; ++DupVectorIter) { |
| 13656 | ECDVector *Vec = *DupVectorIter; |
| 13657 | assert(Vec->size() > 1 && "ECDVector should have at least 2 elements."); |
| 13658 | |
| 13659 | // Emit warning for one enum constant. |
| 13660 | ECDVector::iterator I = Vec->begin(); |
| 13661 | S.Diag((*I)->getLocation(), diag::warn_duplicate_enum_values) |
| 13662 | << (*I)->getName() << (*I)->getInitVal().toString(10) |
| 13663 | << (*I)->getSourceRange(); |
| 13664 | ++I; |
| 13665 | |
| 13666 | // Emit one note for each of the remaining enum constants with |
| 13667 | // the same value. |
| 13668 | for (ECDVector::iterator E = Vec->end(); I != E; ++I) |
| 13669 | S.Diag((*I)->getLocation(), diag::note_duplicate_element) |
| 13670 | << (*I)->getName() << (*I)->getInitVal().toString(10) |
| 13671 | << (*I)->getSourceRange(); |
| 13672 | delete Vec; |
| 13673 | } |
| 13674 | } |
| 13675 | |
| Alexis Hunt | 724f14e | 2014-11-28 00:53:20 +0000 | [diff] [blame] | 13676 | bool |
| 13677 | Sema::IsValueInFlagEnum(const EnumDecl *ED, const llvm::APInt &Val, |
| 13678 | bool AllowMask) const { |
| 13679 | FlagEnumAttr *FEAttr = ED->getAttr<FlagEnumAttr>(); |
| 13680 | assert(FEAttr && "looking for value in non-flag enum"); |
| 13681 | |
| 13682 | llvm::APInt FlagMask = ~FEAttr->getFlagBits(); |
| 13683 | unsigned Width = FlagMask.getBitWidth(); |
| 13684 | |
| 13685 | // We will try a zero-extended value for the regular check first. |
| 13686 | llvm::APInt ExtVal = Val.zextOrSelf(Width); |
| 13687 | |
| 13688 | // A value is in a flag enum if either its bits are a subset of the enum's |
| 13689 | // flag bits (the first condition) or we are allowing masks and the same is |
| 13690 | // true of its complement (the second condition). When masks are allowed, we |
| 13691 | // allow the common idiom of ~(enum1 | enum2) to be a valid enum value. |
| 13692 | // |
| 13693 | // While it's true that any value could be used as a mask, the assumption is |
| 13694 | // that a mask will have all of the insignificant bits set. Anything else is |
| 13695 | // likely a logic error. |
| 13696 | if (!(FlagMask & ExtVal)) |
| 13697 | return true; |
| 13698 | |
| 13699 | if (AllowMask) { |
| 13700 | // Try a one-extended value instead. This can happen if the enum is wider |
| 13701 | // than the constant used, in C with extensions to allow for wider enums. |
| 13702 | // The mask will still have the correct behaviour, so we give the user the |
| 13703 | // benefit of the doubt. |
| 13704 | // |
| 13705 | // FIXME: This heuristic can cause weird results if the enum was extended |
| 13706 | // to a larger type and is signed, because then bit-masks of smaller types |
| 13707 | // that get extended will fall out of range (e.g. ~0x1u). We currently don't |
| 13708 | // detect that case and will get a false positive for it. In most cases, |
| 13709 | // though, it can be fixed by making it a signed type (e.g. ~0x1), so it may |
| 13710 | // be fine just to accept this as a warning. |
| 13711 | ExtVal |= llvm::APInt::getHighBitsSet(Width, Width - Val.getBitWidth()); |
| 13712 | if (!(FlagMask & ~ExtVal)) |
| 13713 | return true; |
| 13714 | } |
| 13715 | |
| 13716 | return false; |
| 13717 | } |
| 13718 | |
| Mike Stump | 6814d1c | 2009-05-16 07:06:02 +0000 | [diff] [blame] | 13719 | void Sema::ActOnEnumBody(SourceLocation EnumLoc, SourceLocation LBraceLoc, |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 13720 | SourceLocation RBraceLoc, Decl *EnumDeclX, |
| Dmitri Gribenko | e5fde99 | 2013-04-27 20:23:52 +0000 | [diff] [blame] | 13721 | ArrayRef<Decl *> Elements, |
| Edward O'Callaghan | c69169d | 2009-08-08 14:36:57 +0000 | [diff] [blame] | 13722 | Scope *S, AttributeList *Attr) { |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 13723 | EnumDecl *Enum = cast<EnumDecl>(EnumDeclX); |
| Douglas Gregor | 07665a6 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 13724 | QualType EnumType = Context.getTypeDeclType(Enum); |
| Edward O'Callaghan | c69169d | 2009-08-08 14:36:57 +0000 | [diff] [blame] | 13725 | |
| 13726 | if (Attr) |
| 13727 | ProcessDeclAttributeList(S, Enum, Attr); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 13728 | |
| Eli Friedman | d0e6097 | 2009-12-11 01:34:50 +0000 | [diff] [blame] | 13729 | if (Enum->isDependentType()) { |
| Dmitri Gribenko | e5fde99 | 2013-04-27 20:23:52 +0000 | [diff] [blame] | 13730 | for (unsigned i = 0, e = Elements.size(); i != e; ++i) { |
| Eli Friedman | d0e6097 | 2009-12-11 01:34:50 +0000 | [diff] [blame] | 13731 | EnumConstantDecl *ECD = |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 13732 | cast_or_null<EnumConstantDecl>(Elements[i]); |
| Eli Friedman | d0e6097 | 2009-12-11 01:34:50 +0000 | [diff] [blame] | 13733 | if (!ECD) continue; |
| 13734 | |
| 13735 | ECD->setType(EnumType); |
| 13736 | } |
| 13737 | |
| John McCall | 9aa35be | 2010-05-06 08:49:23 +0000 | [diff] [blame] | 13738 | Enum->completeDefinition(Context.DependentTy, Context.DependentTy, 0, 0); |
| Eli Friedman | d0e6097 | 2009-12-11 01:34:50 +0000 | [diff] [blame] | 13739 | return; |
| 13740 | } |
| 13741 | |
| Chris Lattner | 67933c0 | 2007-08-28 05:10:31 +0000 | [diff] [blame] | 13742 | // TODO: If the result value doesn't fit in an int, it must be a long or long |
| 13743 | // long value. ISO C does not support this, but GCC does as an extension, |
| 13744 | // emit a warning. |
| Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 13745 | unsigned IntWidth = Context.getTargetInfo().getIntWidth(); |
| 13746 | unsigned CharWidth = Context.getTargetInfo().getCharWidth(); |
| 13747 | unsigned ShortWidth = Context.getTargetInfo().getShortWidth(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 13748 | |
| Chris Lattner | b8a501c | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 13749 | // Verify that all the values are okay, compute the size of the values, and |
| 13750 | // reverse the list. |
| 13751 | unsigned NumNegativeBits = 0; |
| 13752 | unsigned NumPositiveBits = 0; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 13753 | |
| Chris Lattner | b8a501c | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 13754 | // Keep track of whether all elements have type int. |
| 13755 | bool AllElementsInt = true; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 13756 | |
| Dmitri Gribenko | e5fde99 | 2013-04-27 20:23:52 +0000 | [diff] [blame] | 13757 | for (unsigned i = 0, e = Elements.size(); i != e; ++i) { |
| Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 13758 | EnumConstantDecl *ECD = |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 13759 | cast_or_null<EnumConstantDecl>(Elements[i]); |
| Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 13760 | if (!ECD) continue; // Already issued a diagnostic. |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 13761 | |
| Chris Lattner | bf478cb | 2007-08-28 05:27:00 +0000 | [diff] [blame] | 13762 | const llvm::APSInt &InitVal = ECD->getInitVal(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 13763 | |
| Chris Lattner | b8a501c | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 13764 | // Keep track of the size of positive and negative values. |
| Chris Lattner | 7768313 | 2008-02-26 00:33:57 +0000 | [diff] [blame] | 13765 | if (InitVal.isUnsigned() || InitVal.isNonNegative()) |
| Chris Lattner | 49f980c | 2008-01-14 21:47:29 +0000 | [diff] [blame] | 13766 | NumPositiveBits = std::max(NumPositiveBits, |
| 13767 | (unsigned)InitVal.getActiveBits()); |
| Chris Lattner | b8a501c | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 13768 | else |
| Chris Lattner | 49f980c | 2008-01-14 21:47:29 +0000 | [diff] [blame] | 13769 | NumNegativeBits = std::max(NumNegativeBits, |
| 13770 | (unsigned)InitVal.getMinSignedBits()); |
| Chris Lattner | 4ef4001 | 2007-06-11 01:28:17 +0000 | [diff] [blame] | 13771 | |
| Chris Lattner | b8a501c | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 13772 | // Keep track of whether every enum element has type int (very commmon). |
| 13773 | if (AllElementsInt) |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 13774 | AllElementsInt = ECD->getType() == Context.IntTy; |
| Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 13775 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 13776 | |
| Chris Lattner | b8a501c | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 13777 | // Figure out the type that should be used for this enum. |
| Chris Lattner | b8a501c | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 13778 | QualType BestType; |
| Chris Lattner | 3a370bf | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 13779 | unsigned BestWidth; |
| Edward O'Callaghan | c69169d | 2009-08-08 14:36:57 +0000 | [diff] [blame] | 13780 | |
| John McCall | 5677499 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 13781 | // C++0x N3000 [conv.prom]p3: |
| 13782 | // An rvalue of an unscoped enumeration type whose underlying |
| 13783 | // type is not fixed can be converted to an rvalue of the first |
| 13784 | // of the following types that can represent all the values of |
| 13785 | // the enumeration: int, unsigned int, long int, unsigned long |
| 13786 | // int, long long int, or unsigned long long int. |
| 13787 | // C99 6.4.4.3p2: |
| 13788 | // An identifier declared as an enumeration constant has type int. |
| 13789 | // The C99 rule is modified by a gcc extension |
| 13790 | QualType BestPromotionType; |
| 13791 | |
| Aaron Ballman | 9ead124 | 2013-12-19 02:39:40 +0000 | [diff] [blame] | 13792 | bool Packed = Enum->hasAttr<PackedAttr>(); |
| Argyrios Kyrtzidis | 74825bc | 2010-10-08 00:25:19 +0000 | [diff] [blame] | 13793 | // -fshort-enums is the equivalent to specifying the packed attribute on all |
| 13794 | // enum definitions. |
| 13795 | if (LangOpts.ShortEnums) |
| 13796 | Packed = true; |
| Edward O'Callaghan | c69169d | 2009-08-08 14:36:57 +0000 | [diff] [blame] | 13797 | |
| Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 13798 | if (Enum->isFixed()) { |
| Eli Friedman | 64d9504 | 2011-10-26 07:38:19 +0000 | [diff] [blame] | 13799 | BestType = Enum->getIntegerType(); |
| 13800 | if (BestType->isPromotableIntegerType()) |
| 13801 | BestPromotionType = Context.getPromotedIntegerType(BestType); |
| 13802 | else |
| 13803 | BestPromotionType = BestType; |
| Alexis Hunt | 724f14e | 2014-11-28 00:53:20 +0000 | [diff] [blame] | 13804 | |
| 13805 | BestWidth = Context.getIntWidth(BestType); |
| Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 13806 | } |
| 13807 | else if (NumNegativeBits) { |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 13808 | // 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] | 13809 | // int/long/longlong) that fits. |
| Edward O'Callaghan | c69169d | 2009-08-08 14:36:57 +0000 | [diff] [blame] | 13810 | // If it's packed, check also if it fits a char or a short. |
| 13811 | if (Packed && NumNegativeBits <= CharWidth && NumPositiveBits < CharWidth) { |
| John McCall | 5677499 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 13812 | BestType = Context.SignedCharTy; |
| 13813 | BestWidth = CharWidth; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 13814 | } else if (Packed && NumNegativeBits <= ShortWidth && |
| Edward O'Callaghan | c69169d | 2009-08-08 14:36:57 +0000 | [diff] [blame] | 13815 | NumPositiveBits < ShortWidth) { |
| John McCall | 5677499 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 13816 | BestType = Context.ShortTy; |
| 13817 | BestWidth = ShortWidth; |
| 13818 | } else if (NumNegativeBits <= IntWidth && NumPositiveBits < IntWidth) { |
| Chris Lattner | b8a501c | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 13819 | BestType = Context.IntTy; |
| Chris Lattner | 3a370bf | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 13820 | BestWidth = IntWidth; |
| 13821 | } else { |
| Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 13822 | BestWidth = Context.getTargetInfo().getLongWidth(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 13823 | |
| John McCall | 5677499 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 13824 | if (NumNegativeBits <= BestWidth && NumPositiveBits < BestWidth) { |
| Chris Lattner | b8a501c | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 13825 | BestType = Context.LongTy; |
| John McCall | 5677499 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 13826 | } else { |
| Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 13827 | BestWidth = Context.getTargetInfo().getLongLongWidth(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 13828 | |
| Chris Lattner | 3a370bf | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 13829 | if (NumNegativeBits > BestWidth || NumPositiveBits >= BestWidth) |
| Richard Smith | faf156a | 2014-03-05 22:54:58 +0000 | [diff] [blame] | 13830 | Diag(Enum->getLocation(), diag::ext_enum_too_large); |
| Chris Lattner | b8a501c | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 13831 | BestType = Context.LongLongTy; |
| 13832 | } |
| 13833 | } |
| John McCall | 5677499 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 13834 | BestPromotionType = (BestWidth <= IntWidth ? Context.IntTy : BestType); |
| Chris Lattner | b8a501c | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 13835 | } else { |
| Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 13836 | // If there is no negative value, figure out the smallest type that fits |
| 13837 | // all of the enumerator values. |
| Edward O'Callaghan | c69169d | 2009-08-08 14:36:57 +0000 | [diff] [blame] | 13838 | // If it's packed, check also if it fits a char or a short. |
| 13839 | if (Packed && NumPositiveBits <= CharWidth) { |
| John McCall | 5677499 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 13840 | BestType = Context.UnsignedCharTy; |
| 13841 | BestPromotionType = Context.IntTy; |
| 13842 | BestWidth = CharWidth; |
| Edward O'Callaghan | c69169d | 2009-08-08 14:36:57 +0000 | [diff] [blame] | 13843 | } else if (Packed && NumPositiveBits <= ShortWidth) { |
| John McCall | 5677499 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 13844 | BestType = Context.UnsignedShortTy; |
| 13845 | BestPromotionType = Context.IntTy; |
| 13846 | BestWidth = ShortWidth; |
| 13847 | } else if (NumPositiveBits <= IntWidth) { |
| Chris Lattner | b8a501c | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 13848 | BestType = Context.UnsignedIntTy; |
| Chris Lattner | 3a370bf | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 13849 | BestWidth = IntWidth; |
| Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 13850 | BestPromotionType |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 13851 | = (NumPositiveBits == BestWidth || !getLangOpts().CPlusPlus) |
| Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 13852 | ? Context.UnsignedIntTy : Context.IntTy; |
| Chris Lattner | 3a370bf | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 13853 | } else if (NumPositiveBits <= |
| Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 13854 | (BestWidth = Context.getTargetInfo().getLongWidth())) { |
| Chris Lattner | b8a501c | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 13855 | BestType = Context.UnsignedLongTy; |
| Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 13856 | BestPromotionType |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 13857 | = (NumPositiveBits == BestWidth || !getLangOpts().CPlusPlus) |
| Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 13858 | ? Context.UnsignedLongTy : Context.LongTy; |
| Chris Lattner | 37e0587 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 13859 | } else { |
| Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 13860 | BestWidth = Context.getTargetInfo().getLongLongWidth(); |
| Chris Lattner | 3a370bf | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 13861 | assert(NumPositiveBits <= BestWidth && |
| Chris Lattner | b8a501c | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 13862 | "How could an initializer get larger than ULL?"); |
| 13863 | BestType = Context.UnsignedLongLongTy; |
| Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 13864 | BestPromotionType |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 13865 | = (NumPositiveBits == BestWidth || !getLangOpts().CPlusPlus) |
| Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 13866 | ? Context.UnsignedLongLongTy : Context.LongLongTy; |
| Chris Lattner | b8a501c | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 13867 | } |
| 13868 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 13869 | |
| Alexis Hunt | 724f14e | 2014-11-28 00:53:20 +0000 | [diff] [blame] | 13870 | FlagEnumAttr *FEAttr = Enum->getAttr<FlagEnumAttr>(); |
| 13871 | if (FEAttr) |
| 13872 | FEAttr->getFlagBits() = llvm::APInt(BestWidth, 0); |
| 13873 | |
| Chris Lattner | 3a370bf | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 13874 | // Loop over all of the enumerator constants, changing their types to match |
| Alexis Hunt | 724f14e | 2014-11-28 00:53:20 +0000 | [diff] [blame] | 13875 | // the type of the enum if needed. If we have a flag type, we also prepare the |
| 13876 | // FlagBits cache. |
| 13877 | for (auto *D : Elements) { |
| 13878 | auto *ECD = cast_or_null<EnumConstantDecl>(D); |
| Chris Lattner | 3a370bf | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 13879 | if (!ECD) continue; // Already issued a diagnostic. |
| 13880 | |
| 13881 | // Standard C says the enumerators have int type, but we allow, as an |
| 13882 | // extension, the enumerators to be larger than int size. If each |
| 13883 | // enumerator value fits in an int, type it as an int, otherwise type it the |
| 13884 | // same as the enumerator decl itself. This means that in "enum { X = 1U }" |
| 13885 | // that X has type 'int', not 'unsigned'. |
| Chris Lattner | 3a370bf | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 13886 | |
| 13887 | // Determine whether the value fits into an int. |
| 13888 | llvm::APSInt InitVal = ECD->getInitVal(); |
| Chris Lattner | 3a370bf | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 13889 | |
| 13890 | // If it fits into an integer type, force it. Otherwise force it to match |
| 13891 | // the enum decl type. |
| 13892 | QualType NewTy; |
| 13893 | unsigned NewWidth; |
| 13894 | bool NewSign; |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 13895 | if (!getLangOpts().CPlusPlus && |
| Fariborz Jahanian | 37c6417 | 2011-11-04 18:51:24 +0000 | [diff] [blame] | 13896 | !Enum->isFixed() && |
| Douglas Gregor | 6791a0d | 2010-02-01 23:36:03 +0000 | [diff] [blame] | 13897 | isRepresentableIntegerValue(Context, InitVal, Context.IntTy)) { |
| Chris Lattner | 3a370bf | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 13898 | NewTy = Context.IntTy; |
| 13899 | NewWidth = IntWidth; |
| 13900 | NewSign = true; |
| 13901 | } else if (ECD->getType() == BestType) { |
| 13902 | // Already the right type! |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 13903 | if (getLangOpts().CPlusPlus) |
| Douglas Gregor | 1d248c5 | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 13904 | // C++ [dcl.enum]p4: Following the closing brace of an |
| 13905 | // enum-specifier, each enumerator has the type of its |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 13906 | // enumeration. |
| Douglas Gregor | 1d248c5 | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 13907 | ECD->setType(EnumType); |
| Alexis Hunt | 724f14e | 2014-11-28 00:53:20 +0000 | [diff] [blame] | 13908 | goto flagbits; |
| Chris Lattner | 3a370bf | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 13909 | } else { |
| 13910 | NewTy = BestType; |
| 13911 | NewWidth = BestWidth; |
| Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 13912 | NewSign = BestType->isSignedIntegerOrEnumerationType(); |
| Chris Lattner | 3a370bf | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 13913 | } |
| 13914 | |
| 13915 | // Adjust the APSInt value. |
| Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 13916 | InitVal = InitVal.extOrTrunc(NewWidth); |
| Chris Lattner | 3a370bf | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 13917 | InitVal.setIsSigned(NewSign); |
| 13918 | ECD->setInitVal(InitVal); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 13919 | |
| Chris Lattner | 3a370bf | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 13920 | // Adjust the Expr initializer and type. |
| Abramo Bagnara | 7781543 | 2010-12-17 15:49:53 +0000 | [diff] [blame] | 13921 | if (ECD->getInitExpr() && |
| Nick Lewycky | 0bdf13e | 2011-07-02 02:05:12 +0000 | [diff] [blame] | 13922 | !Context.hasSameType(NewTy, ECD->getInitExpr()->getType())) |
| John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 13923 | ECD->setInitExpr(ImplicitCastExpr::Create(Context, NewTy, |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 13924 | CK_IntegralCast, |
| John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 13925 | ECD->getInitExpr(), |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 13926 | /*base paths*/ nullptr, |
| John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 13927 | VK_RValue)); |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 13928 | if (getLangOpts().CPlusPlus) |
| Douglas Gregor | 1d248c5 | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 13929 | // C++ [dcl.enum]p4: Following the closing brace of an |
| 13930 | // enum-specifier, each enumerator has the type of its |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 13931 | // enumeration. |
| Douglas Gregor | 1d248c5 | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 13932 | ECD->setType(EnumType); |
| 13933 | else |
| 13934 | ECD->setType(NewTy); |
| Alexis Hunt | 724f14e | 2014-11-28 00:53:20 +0000 | [diff] [blame] | 13935 | |
| 13936 | flagbits: |
| 13937 | // Check to see if we have a constant with exactly one bit set. Note that x |
| 13938 | // & (x - 1) will be nonzero if and only if x has more than one bit set. |
| 13939 | if (FEAttr) { |
| 13940 | llvm::APInt ExtVal = InitVal.zextOrSelf(BestWidth); |
| 13941 | if (ExtVal != 0 && !(ExtVal & (ExtVal - 1))) { |
| 13942 | FEAttr->getFlagBits() |= ExtVal; |
| 13943 | } |
| 13944 | } |
| Chris Lattner | 3a370bf | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 13945 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 13946 | |
| Alexis Hunt | 724f14e | 2014-11-28 00:53:20 +0000 | [diff] [blame] | 13947 | if (FEAttr) { |
| 13948 | for (Decl *D : Elements) { |
| 13949 | EnumConstantDecl *ECD = cast_or_null<EnumConstantDecl>(D); |
| 13950 | if (!ECD) continue; // Already issued a diagnostic. |
| 13951 | |
| 13952 | llvm::APSInt InitVal = ECD->getInitVal(); |
| 13953 | if (InitVal != 0 && !IsValueInFlagEnum(Enum, InitVal, true)) |
| 13954 | Diag(ECD->getLocation(), diag::warn_flag_enum_constant_out_of_range) |
| 13955 | << ECD << Enum; |
| 13956 | } |
| 13957 | } |
| 13958 | |
| 13959 | |
| 13960 | |
| John McCall | 9aa35be | 2010-05-06 08:49:23 +0000 | [diff] [blame] | 13961 | Enum->completeDefinition(BestType, BestPromotionType, |
| 13962 | NumPositiveBits, NumNegativeBits); |
| James Molloy | 6f8780b | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 13963 | |
| Dmitri Gribenko | e5fde99 | 2013-04-27 20:23:52 +0000 | [diff] [blame] | 13964 | CheckForDuplicateEnumValues(*this, Elements, Enum, EnumType); |
| Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 13965 | |
| 13966 | // Now that the enum type is defined, ensure it's not been underaligned. |
| 13967 | if (Enum->hasAttrs()) |
| 13968 | CheckAlignasUnderalignment(Enum); |
| Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 13969 | } |
| Chris Lattner | 1300fb9 | 2007-01-23 23:42:53 +0000 | [diff] [blame] | 13970 | |
| Abramo Bagnara | 348823a | 2011-03-03 14:20:18 +0000 | [diff] [blame] | 13971 | Decl *Sema::ActOnFileScopeAsmDecl(Expr *expr, |
| 13972 | SourceLocation StartLoc, |
| 13973 | SourceLocation EndLoc) { |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 13974 | StringLiteral *AsmString = cast<StringLiteral>(expr); |
| Sebastian Redl | c675bab | 2008-12-13 16:23:55 +0000 | [diff] [blame] | 13975 | |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 13976 | FileScopeAsmDecl *New = FileScopeAsmDecl::Create(Context, CurContext, |
| Abramo Bagnara | 348823a | 2011-03-03 14:20:18 +0000 | [diff] [blame] | 13977 | AsmString, StartLoc, |
| 13978 | EndLoc); |
| Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 13979 | CurContext->addDecl(New); |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 13980 | return New; |
| Anders Carlsson | 5c6c059 | 2008-02-08 00:33:21 +0000 | [diff] [blame] | 13981 | } |
| Eli Friedman | 5ed5198 | 2009-06-05 02:44:36 +0000 | [diff] [blame] | 13982 | |
| Richard Smith | 7794486 | 2014-03-02 05:58:18 +0000 | [diff] [blame] | 13983 | static void checkModuleImportContext(Sema &S, Module *M, |
| 13984 | SourceLocation ImportLoc, |
| 13985 | DeclContext *DC) { |
| 13986 | if (auto *LSD = dyn_cast<LinkageSpecDecl>(DC)) { |
| 13987 | switch (LSD->getLanguage()) { |
| 13988 | case LinkageSpecDecl::lang_c: |
| 13989 | if (!M->IsExternC) { |
| 13990 | S.Diag(ImportLoc, diag::err_module_import_in_extern_c) |
| 13991 | << M->getFullModuleName(); |
| 13992 | S.Diag(LSD->getLocStart(), diag::note_module_import_in_extern_c); |
| 13993 | return; |
| 13994 | } |
| 13995 | break; |
| 13996 | case LinkageSpecDecl::lang_cxx: |
| 13997 | break; |
| 13998 | } |
| 13999 | DC = LSD->getParent(); |
| 14000 | } |
| 14001 | |
| 14002 | while (isa<LinkageSpecDecl>(DC)) |
| 14003 | DC = DC->getParent(); |
| 14004 | if (!isa<TranslationUnitDecl>(DC)) { |
| 14005 | S.Diag(ImportLoc, diag::err_module_import_not_at_top_level) |
| 14006 | << M->getFullModuleName() << DC; |
| 14007 | S.Diag(cast<Decl>(DC)->getLocStart(), |
| 14008 | diag::note_module_import_not_at_top_level) |
| 14009 | << DC; |
| 14010 | } |
| 14011 | } |
| 14012 | |
| Douglas Gregor | 22d0974 | 2012-01-03 18:04:46 +0000 | [diff] [blame] | 14013 | DeclResult Sema::ActOnModuleImport(SourceLocation AtLoc, |
| 14014 | SourceLocation ImportLoc, |
| 14015 | ModuleIdPath Path) { |
| Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 14016 | Module *Mod = |
| 14017 | getModuleLoader().loadModule(ImportLoc, Path, Module::AllVisible, |
| 14018 | /*IsIncludeDirective=*/false); |
| Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 14019 | if (!Mod) |
| Douglas Gregor | 0814253 | 2011-08-26 23:56:07 +0000 | [diff] [blame] | 14020 | return true; |
| Richard Smith | 7794486 | 2014-03-02 05:58:18 +0000 | [diff] [blame] | 14021 | |
| 14022 | checkModuleImportContext(*this, Mod, ImportLoc, CurContext); |
| 14023 | |
| Ben Langmuir | 527040e | 2014-05-05 05:31:33 +0000 | [diff] [blame] | 14024 | // FIXME: we should support importing a submodule within a different submodule |
| 14025 | // of the same top-level module. Until we do, make it an error rather than |
| 14026 | // silently ignoring the import. |
| 14027 | if (Mod->getTopLevelModuleName() == getLangOpts().CurrentModule) |
| 14028 | Diag(ImportLoc, diag::err_module_self_import) |
| 14029 | << Mod->getFullModuleName() << getLangOpts().CurrentModule; |
| Ben Langmuir | b537a3a | 2014-07-23 15:30:23 +0000 | [diff] [blame] | 14030 | else if (Mod->getTopLevelModuleName() == getLangOpts().ImplementationOfModule) |
| 14031 | Diag(ImportLoc, diag::err_module_import_in_implementation) |
| 14032 | << Mod->getFullModuleName() << getLangOpts().ImplementationOfModule; |
| Ben Langmuir | 527040e | 2014-05-05 05:31:33 +0000 | [diff] [blame] | 14033 | |
| Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 14034 | SmallVector<SourceLocation, 2> IdentifierLocs; |
| Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 14035 | Module *ModCheck = Mod; |
| 14036 | for (unsigned I = 0, N = Path.size(); I != N; ++I) { |
| 14037 | // If we've run out of module parents, just drop the remaining identifiers. |
| 14038 | // We need the length to be consistent. |
| 14039 | if (!ModCheck) |
| 14040 | break; |
| 14041 | ModCheck = ModCheck->Parent; |
| 14042 | |
| 14043 | IdentifierLocs.push_back(Path[I].second); |
| 14044 | } |
| 14045 | |
| 14046 | ImportDecl *Import = ImportDecl::Create(Context, |
| 14047 | Context.getTranslationUnitDecl(), |
| Douglas Gregor | 22d0974 | 2012-01-03 18:04:46 +0000 | [diff] [blame] | 14048 | AtLoc.isValid()? AtLoc : ImportLoc, |
| 14049 | Mod, IdentifierLocs); |
| Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 14050 | Context.getTranslationUnitDecl()->addDecl(Import); |
| 14051 | return Import; |
| Douglas Gregor | 0814253 | 2011-08-26 23:56:07 +0000 | [diff] [blame] | 14052 | } |
| 14053 | |
| Richard Smith | ce587f5 | 2013-11-15 04:24:58 +0000 | [diff] [blame] | 14054 | void Sema::ActOnModuleInclude(SourceLocation DirectiveLoc, Module *Mod) { |
| Richard Smith | 7794486 | 2014-03-02 05:58:18 +0000 | [diff] [blame] | 14055 | checkModuleImportContext(*this, Mod, DirectiveLoc, CurContext); |
| 14056 | |
| Richard Smith | ce587f5 | 2013-11-15 04:24:58 +0000 | [diff] [blame] | 14057 | // FIXME: Should we synthesize an ImportDecl here? |
| Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 14058 | getModuleLoader().makeModuleVisible(Mod, Module::AllVisible, DirectiveLoc, |
| 14059 | /*Complain=*/true); |
| Richard Smith | ce587f5 | 2013-11-15 04:24:58 +0000 | [diff] [blame] | 14060 | } |
| 14061 | |
| Richard Smith | 3d23c42 | 2014-05-07 02:25:43 +0000 | [diff] [blame] | 14062 | void Sema::createImplicitModuleImportForErrorRecovery(SourceLocation Loc, |
| 14063 | Module *Mod) { |
| 14064 | // Bail if we're not allowed to implicitly import a module here. |
| 14065 | if (isSFINAEContext() || !getLangOpts().ModulesErrorRecovery) |
| 14066 | return; |
| 14067 | |
| Douglas Gregor | c147b0b | 2013-01-12 01:29:50 +0000 | [diff] [blame] | 14068 | // Create the implicit import declaration. |
| 14069 | TranslationUnitDecl *TU = getASTContext().getTranslationUnitDecl(); |
| 14070 | ImportDecl *ImportD = ImportDecl::CreateImplicit(getASTContext(), TU, |
| 14071 | Loc, Mod, Loc); |
| 14072 | TU->addDecl(ImportD); |
| 14073 | Consumer.HandleImplicitImportDecl(ImportD); |
| 14074 | |
| 14075 | // Make the module visible. |
| Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 14076 | getModuleLoader().makeModuleVisible(Mod, Module::AllVisible, Loc, |
| 14077 | /*Complain=*/false); |
| Douglas Gregor | c147b0b | 2013-01-12 01:29:50 +0000 | [diff] [blame] | 14078 | } |
| 14079 | |
| David Chisnall | 0867d9c | 2012-02-18 16:12:34 +0000 | [diff] [blame] | 14080 | void Sema::ActOnPragmaRedefineExtname(IdentifierInfo* Name, |
| 14081 | IdentifierInfo* AliasName, |
| 14082 | SourceLocation PragmaLoc, |
| 14083 | SourceLocation NameLoc, |
| 14084 | SourceLocation AliasNameLoc) { |
| 14085 | Decl *PrevDecl = LookupSingleName(TUScope, Name, NameLoc, |
| 14086 | LookupOrdinaryName); |
| Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 14087 | AsmLabelAttr *Attr = ::new (Context) AsmLabelAttr(AliasNameLoc, Context, |
| 14088 | AliasName->getName(), 0); |
| David Chisnall | 0867d9c | 2012-02-18 16:12:34 +0000 | [diff] [blame] | 14089 | |
| 14090 | if (PrevDecl) |
| 14091 | PrevDecl->addAttr(Attr); |
| 14092 | else |
| 14093 | (void)ExtnameUndeclaredIdentifiers.insert( |
| 14094 | std::pair<IdentifierInfo*,AsmLabelAttr*>(Name, Attr)); |
| 14095 | } |
| 14096 | |
| Eli Friedman | 5ed5198 | 2009-06-05 02:44:36 +0000 | [diff] [blame] | 14097 | void Sema::ActOnPragmaWeakID(IdentifierInfo* Name, |
| 14098 | SourceLocation PragmaLoc, |
| 14099 | SourceLocation NameLoc) { |
| Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 14100 | Decl *PrevDecl = LookupSingleName(TUScope, Name, NameLoc, LookupOrdinaryName); |
| Eli Friedman | 5ed5198 | 2009-06-05 02:44:36 +0000 | [diff] [blame] | 14101 | |
| Eli Friedman | 5ed5198 | 2009-06-05 02:44:36 +0000 | [diff] [blame] | 14102 | if (PrevDecl) { |
| Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 14103 | PrevDecl->addAttr(WeakAttr::CreateImplicit(Context, PragmaLoc)); |
| Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 14104 | } else { |
| 14105 | (void)WeakUndeclaredIdentifiers.insert( |
| 14106 | std::pair<IdentifierInfo*,WeakInfo> |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 14107 | (Name, WeakInfo((IdentifierInfo*)nullptr, NameLoc))); |
| Eli Friedman | 5ed5198 | 2009-06-05 02:44:36 +0000 | [diff] [blame] | 14108 | } |
| Eli Friedman | 5ed5198 | 2009-06-05 02:44:36 +0000 | [diff] [blame] | 14109 | } |
| 14110 | |
| 14111 | void Sema::ActOnPragmaWeakAlias(IdentifierInfo* Name, |
| 14112 | IdentifierInfo* AliasName, |
| 14113 | SourceLocation PragmaLoc, |
| 14114 | SourceLocation NameLoc, |
| 14115 | SourceLocation AliasNameLoc) { |
| Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 14116 | Decl *PrevDecl = LookupSingleName(TUScope, AliasName, AliasNameLoc, |
| 14117 | LookupOrdinaryName); |
| Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 14118 | WeakInfo W = WeakInfo(Name, NameLoc); |
| Eli Friedman | 5ed5198 | 2009-06-05 02:44:36 +0000 | [diff] [blame] | 14119 | |
| Eli Friedman | 5ed5198 | 2009-06-05 02:44:36 +0000 | [diff] [blame] | 14120 | if (PrevDecl) { |
| Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 14121 | if (!PrevDecl->hasAttr<AliasAttr>()) |
| 14122 | if (NamedDecl *ND = dyn_cast<NamedDecl>(PrevDecl)) |
| Ryan Flynn | d963a49 | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 14123 | DeclApplyPragmaWeak(TUScope, ND, W); |
| Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 14124 | } else { |
| 14125 | (void)WeakUndeclaredIdentifiers.insert( |
| 14126 | std::pair<IdentifierInfo*,WeakInfo>(AliasName, W)); |
| Eli Friedman | 5ed5198 | 2009-06-05 02:44:36 +0000 | [diff] [blame] | 14127 | } |
| Eli Friedman | 5ed5198 | 2009-06-05 02:44:36 +0000 | [diff] [blame] | 14128 | } |
| Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 14129 | |
| 14130 | Decl *Sema::getObjCDeclContext() const { |
| 14131 | return (dyn_cast_or_null<ObjCContainerDecl>(CurContext)); |
| 14132 | } |
| Argyrios Kyrtzidis | c281c96 | 2011-10-06 23:23:27 +0000 | [diff] [blame] | 14133 | |
| 14134 | AvailabilityResult Sema::getCurContextAvailability() const { |
| Ben Langmuir | c91ac9e | 2015-01-20 20:41:36 +0000 | [diff] [blame] | 14135 | const Decl *D = cast_or_null<Decl>(getCurObjCLexicalContext()); |
| 14136 | if (!D) |
| 14137 | return AR_Available; |
| 14138 | |
| Ted Kremenek | cb42dbe | 2013-11-20 17:24:03 +0000 | [diff] [blame] | 14139 | // If we are within an Objective-C method, we should consult |
| 14140 | // both the availability of the method as well as the |
| 14141 | // enclosing class. If the class is (say) deprecated, |
| 14142 | // the entire method is considered deprecated from the |
| 14143 | // purpose of checking if the current context is deprecated. |
| 14144 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
| 14145 | AvailabilityResult R = MD->getAvailability(); |
| 14146 | if (R != AR_Available) |
| 14147 | return R; |
| 14148 | D = MD->getClassInterface(); |
| 14149 | } |
| 14150 | // If we are within an Objective-c @implementation, it |
| 14151 | // gets the same availability context as the @interface. |
| 14152 | else if (const ObjCImplementationDecl *ID = |
| 14153 | dyn_cast<ObjCImplementationDecl>(D)) { |
| 14154 | D = ID->getClassInterface(); |
| 14155 | } |
| Fariborz Jahanian | 38c53fb | 2014-08-21 17:06:57 +0000 | [diff] [blame] | 14156 | // Recover from user error. |
| 14157 | return D ? D->getAvailability() : AR_Available; |
| Argyrios Kyrtzidis | c281c96 | 2011-10-06 23:23:27 +0000 | [diff] [blame] | 14158 | } |