Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- SemaDecl.cpp - Semantic Analysis for Declarations ----------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements semantic analysis for declarations. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
John McCall | 2d88708 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 14 | #include "clang/Sema/SemaInternal.h" |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 15 | #include "TypeLocBuilder.h" |
Chris Lattner | e1e7985 | 2008-02-06 00:51:33 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTConsumer.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTContext.h" |
Faisal Vali | fad9e13 | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTLambda.h" |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 19 | #include "clang/AST/CXXInheritance.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 20 | #include "clang/AST/CharUnits.h" |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 21 | #include "clang/AST/CommentDiagnostic.h" |
John McCall | 384aff8 | 2010-08-25 07:42:41 +0000 | [diff] [blame] | 22 | #include "clang/AST/DeclCXX.h" |
John McCall | 7cd088e | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 23 | #include "clang/AST/DeclObjC.h" |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 24 | #include "clang/AST/DeclTemplate.h" |
Chandler Carruth | a7689ef | 2011-03-27 09:46:56 +0000 | [diff] [blame] | 25 | #include "clang/AST/EvaluatedExprVisitor.h" |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 26 | #include "clang/AST/ExprCXX.h" |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 27 | #include "clang/AST/StmtCXX.h" |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 28 | #include "clang/Basic/Builtins.h" |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 29 | #include "clang/Basic/PartialDiagnostic.h" |
Steve Naroff | 4c49a6c | 2008-01-30 23:46:05 +0000 | [diff] [blame] | 30 | #include "clang/Basic/SourceManager.h" |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 31 | #include "clang/Basic/TargetInfo.h" |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 32 | #include "clang/Lex/HeaderSearch.h" // TODO: Sema shouldn't depend on Lex |
| 33 | #include "clang/Lex/Lexer.h" // TODO: Extract static functions to fix layering. |
| 34 | #include "clang/Lex/ModuleLoader.h" // TODO: Sema shouldn't depend on Lex |
| 35 | #include "clang/Lex/Preprocessor.h" // Included for isCodeCompletionEnabled() |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 36 | #include "clang/Parse/ParseDiagnostic.h" |
| 37 | #include "clang/Sema/CXXFieldCollector.h" |
| 38 | #include "clang/Sema/DeclSpec.h" |
| 39 | #include "clang/Sema/DelayedDiagnostic.h" |
| 40 | #include "clang/Sema/Initialization.h" |
| 41 | #include "clang/Sema/Lookup.h" |
| 42 | #include "clang/Sema/ParsedTemplate.h" |
| 43 | #include "clang/Sema/Scope.h" |
| 44 | #include "clang/Sema/ScopeInfo.h" |
Faisal Vali | c00e419 | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 45 | #include "clang/Sema/Template.h" |
Benjamin Kramer | 8fe83e1 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 46 | #include "llvm/ADT/SmallString.h" |
John McCall | 6675586 | 2009-12-24 09:58:38 +0000 | [diff] [blame] | 47 | #include "llvm/ADT/Triple.h" |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 48 | #include <algorithm> |
Douglas Gregor | 9a8c9a2 | 2009-09-28 21:14:19 +0000 | [diff] [blame] | 49 | #include <cstring> |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 50 | #include <functional> |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 51 | using namespace clang; |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 52 | using namespace sema; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 53 | |
Richard Smith | c89edf5 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 54 | Sema::DeclGroupPtrTy Sema::ConvertDeclToDeclGroup(Decl *Ptr, Decl *OwnedType) { |
| 55 | if (OwnedType) { |
| 56 | Decl *Group[2] = { OwnedType, Ptr }; |
| 57 | return DeclGroupPtrTy::make(DeclGroupRef::Create(Context, Group, 2)); |
| 58 | } |
| 59 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 60 | return DeclGroupPtrTy::make(DeclGroupRef(Ptr)); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Kaelyn Uhrain | 43e875d | 2012-01-18 21:41:41 +0000 | [diff] [blame] | 63 | namespace { |
| 64 | |
| 65 | class TypeNameValidatorCCC : public CorrectionCandidateCallback { |
| 66 | public: |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 67 | TypeNameValidatorCCC(bool AllowInvalid, bool WantClass=false, |
| 68 | bool AllowTemplates=false) |
| 69 | : AllowInvalidDecl(AllowInvalid), WantClassName(WantClass), |
| 70 | AllowClassTemplates(AllowTemplates) { |
Kaelyn Uhrain | 43e875d | 2012-01-18 21:41:41 +0000 | [diff] [blame] | 71 | WantExpressionKeywords = false; |
| 72 | WantCXXNamedCasts = false; |
| 73 | WantRemainingKeywords = false; |
| 74 | } |
| 75 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 76 | bool ValidateCandidate(const TypoCorrection &candidate) override { |
| 77 | if (NamedDecl *ND = candidate.getCorrectionDecl()) { |
| 78 | bool IsType = isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND); |
| 79 | bool AllowedTemplate = AllowClassTemplates && isa<ClassTemplateDecl>(ND); |
| 80 | return (IsType || AllowedTemplate) && |
| 81 | (AllowInvalidDecl || !ND->isInvalidDecl()); |
| 82 | } |
| 83 | return !WantClassName && candidate.isKeyword(); |
Kaelyn Uhrain | 43e875d | 2012-01-18 21:41:41 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | private: |
| 87 | bool AllowInvalidDecl; |
Kaelyn Uhrain | c1fb542 | 2012-06-22 23:37:05 +0000 | [diff] [blame] | 88 | bool WantClassName; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 89 | bool AllowClassTemplates; |
Kaelyn Uhrain | 43e875d | 2012-01-18 21:41:41 +0000 | [diff] [blame] | 90 | }; |
| 91 | |
| 92 | } |
| 93 | |
Kaelyn Uhrain | 7bf3340 | 2012-06-15 23:45:51 +0000 | [diff] [blame] | 94 | /// \brief Determine whether the token kind starts a simple-type-specifier. |
| 95 | bool Sema::isSimpleTypeSpecifier(tok::TokenKind Kind) const { |
| 96 | switch (Kind) { |
| 97 | // FIXME: Take into account the current language when deciding whether a |
| 98 | // token kind is a valid type specifier |
| 99 | case tok::kw_short: |
| 100 | case tok::kw_long: |
| 101 | case tok::kw___int64: |
| 102 | case tok::kw___int128: |
| 103 | case tok::kw_signed: |
| 104 | case tok::kw_unsigned: |
| 105 | case tok::kw_void: |
| 106 | case tok::kw_char: |
| 107 | case tok::kw_int: |
| 108 | case tok::kw_half: |
| 109 | case tok::kw_float: |
| 110 | case tok::kw_double: |
| 111 | case tok::kw_wchar_t: |
| 112 | case tok::kw_bool: |
| 113 | case tok::kw___underlying_type: |
| 114 | return true; |
| 115 | |
| 116 | case tok::annot_typename: |
| 117 | case tok::kw_char16_t: |
| 118 | case tok::kw_char32_t: |
| 119 | case tok::kw_typeof: |
David Majnemer | ff989a8 | 2013-09-22 01:24:26 +0000 | [diff] [blame] | 120 | case tok::annot_decltype: |
Kaelyn Uhrain | 7bf3340 | 2012-06-15 23:45:51 +0000 | [diff] [blame] | 121 | case tok::kw_decltype: |
| 122 | return getLangOpts().CPlusPlus; |
| 123 | |
| 124 | default: |
| 125 | break; |
| 126 | } |
| 127 | |
| 128 | return false; |
| 129 | } |
| 130 | |
Douglas Gregor | d6efafa | 2009-02-04 19:16:12 +0000 | [diff] [blame] | 131 | /// \brief If the identifier refers to a type name within this scope, |
| 132 | /// return the declaration of that type. |
| 133 | /// |
| 134 | /// This routine performs ordinary name lookup of the identifier II |
| 135 | /// within the given scope, with optional C++ scope specifier SS, to |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 136 | /// determine whether the name refers to a type. If so, returns an |
| 137 | /// opaque pointer (actually a QualType) corresponding to that |
| 138 | /// type. Otherwise, returns NULL. |
Dmitri Gribenko | 8eead16 | 2013-05-03 13:12:11 +0000 | [diff] [blame] | 139 | ParsedType Sema::getTypeName(const IdentifierInfo &II, SourceLocation NameLoc, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 140 | Scope *S, CXXScopeSpec *SS, |
Fariborz Jahanian | 1e52dfc | 2011-02-08 18:05:59 +0000 | [diff] [blame] | 141 | bool isClassName, bool HasTrailingDot, |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 142 | ParsedType ObjectTypePtr, |
Abramo Bagnara | fad03b7 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 143 | bool IsCtorOrDtorName, |
Kaelyn Uhrain | fac9467 | 2011-10-11 01:02:41 +0000 | [diff] [blame] | 144 | bool WantNontrivialTypeSourceInfo, |
| 145 | IdentifierInfo **CorrectedII) { |
Douglas Gregor | f6e6fc8 | 2009-11-20 22:03:38 +0000 | [diff] [blame] | 146 | // Determine where we will perform name lookup. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 147 | DeclContext *LookupCtx = nullptr; |
Douglas Gregor | f6e6fc8 | 2009-11-20 22:03:38 +0000 | [diff] [blame] | 148 | if (ObjectTypePtr) { |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 149 | QualType ObjectType = ObjectTypePtr.get(); |
Douglas Gregor | f6e6fc8 | 2009-11-20 22:03:38 +0000 | [diff] [blame] | 150 | if (ObjectType->isRecordType()) |
| 151 | LookupCtx = computeDeclContext(ObjectType); |
Jeffrey Yasskin | edc2877 | 2010-04-07 23:29:58 +0000 | [diff] [blame] | 152 | } else if (SS && SS->isNotEmpty()) { |
Douglas Gregor | f6e6fc8 | 2009-11-20 22:03:38 +0000 | [diff] [blame] | 153 | LookupCtx = computeDeclContext(*SS, false); |
| 154 | |
| 155 | if (!LookupCtx) { |
| 156 | if (isDependentScopeSpecifier(*SS)) { |
| 157 | // C++ [temp.res]p3: |
| 158 | // A qualified-id that refers to a type and in which the |
| 159 | // nested-name-specifier depends on a template-parameter (14.6.2) |
| 160 | // shall be prefixed by the keyword typename to indicate that the |
| 161 | // qualified-id denotes a type, forming an |
| 162 | // elaborated-type-specifier (7.1.5.3). |
| 163 | // |
| 164 | // We therefore do not perform any name lookup if the result would |
| 165 | // refer to a member of an unknown specialization. |
Richard Smith | c5a89a1 | 2012-04-02 01:30:27 +0000 | [diff] [blame] | 166 | if (!isClassName && !IsCtorOrDtorName) |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 167 | return ParsedType(); |
Douglas Gregor | f6e6fc8 | 2009-11-20 22:03:38 +0000 | [diff] [blame] | 168 | |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 169 | // We know from the grammar that this name refers to a type, |
| 170 | // so build a dependent node to describe the type. |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 171 | if (WantNontrivialTypeSourceInfo) |
| 172 | return ActOnTypenameType(S, SourceLocation(), *SS, II, NameLoc).get(); |
| 173 | |
| 174 | NestedNameSpecifierLoc QualifierLoc = SS->getWithLocInContext(Context); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 175 | QualType T = CheckTypenameType(ETK_None, SourceLocation(), QualifierLoc, |
| 176 | II, NameLoc); |
| 177 | return ParsedType::make(T); |
Douglas Gregor | f6e6fc8 | 2009-11-20 22:03:38 +0000 | [diff] [blame] | 178 | } |
| 179 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 180 | return ParsedType(); |
Douglas Gregor | f6e6fc8 | 2009-11-20 22:03:38 +0000 | [diff] [blame] | 181 | } |
| 182 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 183 | if (!LookupCtx->isDependentContext() && |
| 184 | RequireCompleteDeclContext(*SS, LookupCtx)) |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 185 | return ParsedType(); |
Douglas Gregor | 42c39f3 | 2009-08-26 18:27:52 +0000 | [diff] [blame] | 186 | } |
Eli Friedman | 0f0615b | 2009-12-21 01:42:38 +0000 | [diff] [blame] | 187 | |
| 188 | // FIXME: LookupNestedNameSpecifierName isn't the right kind of |
| 189 | // lookup for class-names. |
| 190 | LookupNameKind Kind = isClassName ? LookupNestedNameSpecifierName : |
| 191 | LookupOrdinaryName; |
| 192 | LookupResult Result(*this, &II, NameLoc, Kind); |
Douglas Gregor | f6e6fc8 | 2009-11-20 22:03:38 +0000 | [diff] [blame] | 193 | if (LookupCtx) { |
| 194 | // Perform "qualified" name lookup into the declaration context we |
| 195 | // computed, which is either the type of the base of a member access |
| 196 | // expression or the declaration context associated with a prior |
| 197 | // nested-name-specifier. |
| 198 | LookupQualifiedName(Result, LookupCtx); |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 199 | |
Douglas Gregor | f6e6fc8 | 2009-11-20 22:03:38 +0000 | [diff] [blame] | 200 | if (ObjectTypePtr && Result.empty()) { |
| 201 | // C++ [basic.lookup.classref]p3: |
| 202 | // If the unqualified-id is ~type-name, the type-name is looked up |
| 203 | // in the context of the entire postfix-expression. If the type T of |
| 204 | // the object expression is of a class type C, the type-name is also |
| 205 | // looked up in the scope of class C. At least one of the lookups shall |
| 206 | // find a name that refers to (possibly cv-qualified) T. |
| 207 | LookupName(Result, S); |
| 208 | } |
| 209 | } else { |
| 210 | // Perform unqualified name lookup. |
| 211 | LookupName(Result, S); |
| 212 | } |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 213 | |
| 214 | NamedDecl *IIDecl = nullptr; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 215 | switch (Result.getResultKind()) { |
Chris Lattner | 22bd905 | 2009-02-16 22:07:16 +0000 | [diff] [blame] | 216 | case LookupResult::NotFound: |
Douglas Gregor | 7d3f576 | 2010-01-15 01:44:47 +0000 | [diff] [blame] | 217 | case LookupResult::NotFoundInCurrentInstantiation: |
Kaelyn Uhrain | fac9467 | 2011-10-11 01:02:41 +0000 | [diff] [blame] | 218 | if (CorrectedII) { |
Kaelyn Uhrain | c1fb542 | 2012-06-22 23:37:05 +0000 | [diff] [blame] | 219 | TypeNameValidatorCCC Validator(true, isClassName); |
Kaelyn Uhrain | fac9467 | 2011-10-11 01:02:41 +0000 | [diff] [blame] | 220 | TypoCorrection Correction = CorrectTypo(Result.getLookupNameInfo(), |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 221 | Kind, S, SS, Validator, |
| 222 | CTK_ErrorRecovery); |
Kaelyn Uhrain | fac9467 | 2011-10-11 01:02:41 +0000 | [diff] [blame] | 223 | IdentifierInfo *NewII = Correction.getCorrectionAsIdentifierInfo(); |
| 224 | TemplateTy Template; |
| 225 | bool MemberOfUnknownSpecialization; |
| 226 | UnqualifiedId TemplateName; |
| 227 | TemplateName.setIdentifier(NewII, NameLoc); |
| 228 | NestedNameSpecifier *NNS = Correction.getCorrectionSpecifier(); |
| 229 | CXXScopeSpec NewSS, *NewSSPtr = SS; |
| 230 | if (SS && NNS) { |
| 231 | NewSS.MakeTrivial(Context, NNS, SourceRange(NameLoc)); |
| 232 | NewSSPtr = &NewSS; |
| 233 | } |
| 234 | if (Correction && (NNS || NewII != &II) && |
| 235 | // Ignore a correction to a template type as the to-be-corrected |
| 236 | // identifier is not a template (typo correction for template names |
| 237 | // is handled elsewhere). |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 238 | !(getLangOpts().CPlusPlus && NewSSPtr && |
Kaelyn Uhrain | fac9467 | 2011-10-11 01:02:41 +0000 | [diff] [blame] | 239 | isTemplateName(S, *NewSSPtr, false, TemplateName, ParsedType(), |
| 240 | false, Template, MemberOfUnknownSpecialization))) { |
| 241 | ParsedType Ty = getTypeName(*NewII, NameLoc, S, NewSSPtr, |
| 242 | isClassName, HasTrailingDot, ObjectTypePtr, |
Abramo Bagnara | fad03b7 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 243 | IsCtorOrDtorName, |
Kaelyn Uhrain | fac9467 | 2011-10-11 01:02:41 +0000 | [diff] [blame] | 244 | WantNontrivialTypeSourceInfo); |
| 245 | if (Ty) { |
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 246 | diagnoseTypo(Correction, |
| 247 | PDiag(diag::err_unknown_type_or_class_name_suggest) |
| 248 | << Result.getLookupName() << isClassName); |
Kaelyn Uhrain | fac9467 | 2011-10-11 01:02:41 +0000 | [diff] [blame] | 249 | if (SS && NNS) |
| 250 | SS->MakeTrivial(Context, NNS, SourceRange(NameLoc)); |
| 251 | *CorrectedII = NewII; |
| 252 | return Ty; |
| 253 | } |
| 254 | } |
| 255 | } |
| 256 | // If typo correction failed or was not performed, fall through |
Chris Lattner | 22bd905 | 2009-02-16 22:07:16 +0000 | [diff] [blame] | 257 | case LookupResult::FoundOverloaded: |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 258 | case LookupResult::FoundUnresolvedValue: |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 259 | Result.suppressDiagnostics(); |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 260 | return ParsedType(); |
Douglas Gregor | b696ea3 | 2009-02-04 17:00:24 +0000 | [diff] [blame] | 261 | |
Chris Lattner | a64ef0a | 2009-10-25 22:09:09 +0000 | [diff] [blame] | 262 | case LookupResult::Ambiguous: |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 263 | // Recover from type-hiding ambiguities by hiding the type. We'll |
| 264 | // do the lookup again when looking for an object, and we can |
| 265 | // diagnose the error then. If we don't do this, then the error |
| 266 | // about hiding the type will be immediately followed by an error |
| 267 | // that only makes sense if the identifier was treated like a type. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 268 | if (Result.getAmbiguityKind() == LookupResult::AmbiguousTagHiding) { |
| 269 | Result.suppressDiagnostics(); |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 270 | return ParsedType(); |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 271 | } |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 272 | |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 273 | // Look to see if we have a type anywhere in the list of results. |
| 274 | for (LookupResult::iterator Res = Result.begin(), ResEnd = Result.end(); |
| 275 | Res != ResEnd; ++Res) { |
| 276 | if (isa<TypeDecl>(*Res) || isa<ObjCInterfaceDecl>(*Res)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 277 | if (!IIDecl || |
| 278 | (*Res)->getLocation().getRawEncoding() < |
Douglas Gregor | 841b53c | 2009-04-13 15:14:38 +0000 | [diff] [blame] | 279 | IIDecl->getLocation().getRawEncoding()) |
| 280 | IIDecl = *Res; |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 281 | } |
| 282 | } |
| 283 | |
| 284 | if (!IIDecl) { |
| 285 | // None of the entities we found is a type, so there is no way |
| 286 | // to even assume that the result is a type. In this case, don't |
| 287 | // complain about the ambiguity. The parser will either try to |
| 288 | // perform this lookup again (e.g., as an object name), which |
| 289 | // will produce the ambiguity, or will complain that it expected |
| 290 | // a type name. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 291 | Result.suppressDiagnostics(); |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 292 | return ParsedType(); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | // We found a type within the ambiguous lookup; diagnose the |
| 296 | // ambiguity and then return that type. This might be the right |
| 297 | // answer, or it might not be, but it suppresses any attempt to |
| 298 | // perform the name lookup again. |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 299 | break; |
Douglas Gregor | b696ea3 | 2009-02-04 17:00:24 +0000 | [diff] [blame] | 300 | |
Chris Lattner | 22bd905 | 2009-02-16 22:07:16 +0000 | [diff] [blame] | 301 | case LookupResult::Found: |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 302 | IIDecl = Result.getFoundDecl(); |
Chris Lattner | 22bd905 | 2009-02-16 22:07:16 +0000 | [diff] [blame] | 303 | break; |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 304 | } |
| 305 | |
Chris Lattner | 10ca337 | 2009-10-25 17:16:46 +0000 | [diff] [blame] | 306 | assert(IIDecl && "Didn't find decl"); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 307 | |
Chris Lattner | 10ca337 | 2009-10-25 17:16:46 +0000 | [diff] [blame] | 308 | QualType T; |
| 309 | if (TypeDecl *TD = dyn_cast<TypeDecl>(IIDecl)) { |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 310 | DiagnoseUseOfDecl(IIDecl, NameLoc); |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 311 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 312 | T = Context.getTypeDeclType(TD); |
Abramo Bagnara | fad03b7 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 313 | |
| 314 | // NOTE: avoid constructing an ElaboratedType(Loc) if this is a |
| 315 | // constructor or destructor name (in such a case, the scope specifier |
| 316 | // will be attached to the enclosing Expr or Decl node). |
| 317 | if (SS && SS->isNotEmpty() && !IsCtorOrDtorName) { |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 318 | if (WantNontrivialTypeSourceInfo) { |
| 319 | // Construct a type with type-source information. |
| 320 | TypeLocBuilder Builder; |
| 321 | Builder.pushTypeSpec(T).setNameLoc(NameLoc); |
| 322 | |
| 323 | T = getElaboratedType(ETK_None, *SS, T); |
| 324 | ElaboratedTypeLoc ElabTL = Builder.push<ElaboratedTypeLoc>(T); |
Abramo Bagnara | 38a4291 | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 325 | ElabTL.setElaboratedKeywordLoc(SourceLocation()); |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 326 | ElabTL.setQualifierLoc(SS->getWithLocInContext(Context)); |
| 327 | return CreateParsedType(T, Builder.getTypeSourceInfo(Context, T)); |
| 328 | } else { |
| 329 | T = getElaboratedType(ETK_None, *SS, T); |
| 330 | } |
| 331 | } |
Chris Lattner | 10ca337 | 2009-10-25 17:16:46 +0000 | [diff] [blame] | 332 | } else if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(IIDecl)) { |
Fariborz Jahanian | 02b0d65 | 2011-03-08 19:12:46 +0000 | [diff] [blame] | 333 | (void)DiagnoseUseOfDecl(IDecl, NameLoc); |
Fariborz Jahanian | 1e52dfc | 2011-02-08 18:05:59 +0000 | [diff] [blame] | 334 | if (!HasTrailingDot) |
| 335 | T = Context.getObjCInterfaceType(IDecl); |
| 336 | } |
| 337 | |
| 338 | if (T.isNull()) { |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 339 | // If it's not plausibly a type, suppress diagnostics. |
| 340 | Result.suppressDiagnostics(); |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 341 | return ParsedType(); |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 342 | } |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 343 | return ParsedType::make(T); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 344 | } |
| 345 | |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 346 | /// isTagName() - This method is called *for error recovery purposes only* |
| 347 | /// to determine if the specified name is a valid tag name ("struct foo"). If |
| 348 | /// so, this returns the TST for the tag corresponding to it (TST_enum, |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 349 | /// TST_union, TST_struct, TST_interface, TST_class). This is used to diagnose |
| 350 | /// cases in C where the user forgot to specify the tag. |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 351 | DeclSpec::TST Sema::isTagName(IdentifierInfo &II, Scope *S) { |
| 352 | // Do a tag name lookup in this scope. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 353 | LookupResult R(*this, &II, SourceLocation(), LookupTagName); |
| 354 | LookupName(R, S, false); |
| 355 | R.suppressDiagnostics(); |
| 356 | if (R.getResultKind() == LookupResult::Found) |
John McCall | 1bcee0a | 2009-12-02 08:25:40 +0000 | [diff] [blame] | 357 | if (const TagDecl *TD = R.getAsSingle<TagDecl>()) { |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 358 | switch (TD->getTagKind()) { |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 359 | case TTK_Struct: return DeclSpec::TST_struct; |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 360 | case TTK_Interface: return DeclSpec::TST_interface; |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 361 | case TTK_Union: return DeclSpec::TST_union; |
| 362 | case TTK_Class: return DeclSpec::TST_class; |
| 363 | case TTK_Enum: return DeclSpec::TST_enum; |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 364 | } |
| 365 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 366 | |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 367 | return DeclSpec::TST_unspecified; |
| 368 | } |
| 369 | |
Francois Pichet | 6943e9b | 2011-04-13 02:38:49 +0000 | [diff] [blame] | 370 | /// isMicrosoftMissingTypename - In Microsoft mode, within class scope, |
| 371 | /// if a CXXScopeSpec's type is equal to the type of one of the base classes |
| 372 | /// then downgrade the missing typename error to a warning. |
| 373 | /// This is needed for MSVC compatibility; Example: |
| 374 | /// @code |
| 375 | /// template<class T> class A { |
| 376 | /// public: |
| 377 | /// typedef int TYPE; |
| 378 | /// }; |
| 379 | /// template<class T> class B : public A<T> { |
| 380 | /// public: |
| 381 | /// A<T>::TYPE a; // no typename required because A<T> is a base class. |
| 382 | /// }; |
| 383 | /// @endcode |
Francois Pichet | f11dbe9 | 2011-10-11 01:50:09 +0000 | [diff] [blame] | 384 | bool Sema::isMicrosoftMissingTypename(const CXXScopeSpec *SS, Scope *S) { |
Francois Pichet | 6943e9b | 2011-04-13 02:38:49 +0000 | [diff] [blame] | 385 | if (CurContext->isRecord()) { |
Francois Pichet | 3441a52 | 2011-04-13 02:44:57 +0000 | [diff] [blame] | 386 | const Type *Ty = SS->getScopeRep()->getAsType(); |
Francois Pichet | 6943e9b | 2011-04-13 02:38:49 +0000 | [diff] [blame] | 387 | |
| 388 | CXXRecordDecl *RD = cast<CXXRecordDecl>(CurContext); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 389 | for (const auto &Base : RD->bases()) |
| 390 | if (Context.hasSameUnqualifiedType(QualType(Ty, 1), Base.getType())) |
Francois Pichet | 6943e9b | 2011-04-13 02:38:49 +0000 | [diff] [blame] | 391 | return true; |
Francois Pichet | f11dbe9 | 2011-10-11 01:50:09 +0000 | [diff] [blame] | 392 | return S->isFunctionPrototypeScope(); |
Francois Pichet | 6943e9b | 2011-04-13 02:38:49 +0000 | [diff] [blame] | 393 | } |
Francois Pichet | f11dbe9 | 2011-10-11 01:50:09 +0000 | [diff] [blame] | 394 | return CurContext->isFunctionOrMethod() || S->isFunctionPrototypeScope(); |
Francois Pichet | 6943e9b | 2011-04-13 02:38:49 +0000 | [diff] [blame] | 395 | } |
| 396 | |
Kaelyn Uhrain | 50dc12a | 2012-06-15 23:45:58 +0000 | [diff] [blame] | 397 | bool Sema::DiagnoseUnknownTypeName(IdentifierInfo *&II, |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 398 | SourceLocation IILoc, |
| 399 | Scope *S, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 400 | CXXScopeSpec *SS, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 401 | ParsedType &SuggestedType, |
| 402 | bool AllowClassTemplates) { |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 403 | // We don't have anything to suggest (yet). |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 404 | SuggestedType = ParsedType(); |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 405 | |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 406 | // There may have been a typo in the name of the type. Look up typo |
| 407 | // results, in case we have something that we can suggest. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 408 | TypeNameValidatorCCC Validator(false, false, AllowClassTemplates); |
Kaelyn Uhrain | 50dc12a | 2012-06-15 23:45:58 +0000 | [diff] [blame] | 409 | if (TypoCorrection Corrected = CorrectTypo(DeclarationNameInfo(II, IILoc), |
Kaelyn Uhrain | 43e875d | 2012-01-18 21:41:41 +0000 | [diff] [blame] | 410 | LookupOrdinaryName, S, SS, |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 411 | Validator, CTK_ErrorRecovery)) { |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 412 | if (Corrected.isKeyword()) { |
| 413 | // We corrected to a keyword. |
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 414 | diagnoseTypo(Corrected, PDiag(diag::err_unknown_typename_suggest) << II); |
| 415 | II = Corrected.getCorrectionAsIdentifierInfo(); |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 416 | } else { |
Kaelyn Uhrain | 43e875d | 2012-01-18 21:41:41 +0000 | [diff] [blame] | 417 | // We found a similarly-named type or interface; suggest that. |
Kaelyn Uhrain | b2567dd | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 418 | if (!SS || !SS->isSet()) { |
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 419 | diagnoseTypo(Corrected, |
| 420 | PDiag(diag::err_unknown_typename_suggest) << II); |
Kaelyn Uhrain | b2567dd | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 421 | } else if (DeclContext *DC = computeDeclContext(*SS, false)) { |
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 422 | std::string CorrectedStr(Corrected.getAsString(getLangOpts())); |
| 423 | bool DroppedSpecifier = Corrected.WillReplaceSpecifier() && |
Kaelyn Uhrain | b2567dd | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 424 | II->getName().equals(CorrectedStr); |
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 425 | diagnoseTypo(Corrected, |
| 426 | PDiag(diag::err_unknown_nested_typename_suggest) |
| 427 | << II << DC << DroppedSpecifier << SS->getRange()); |
| 428 | } else { |
Kaelyn Uhrain | 43e875d | 2012-01-18 21:41:41 +0000 | [diff] [blame] | 429 | llvm_unreachable("could not have corrected a typo here"); |
Kaelyn Uhrain | b2567dd | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 430 | } |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 431 | |
Kaelyn Uhrain | a934c31 | 2013-09-26 21:13:05 +0000 | [diff] [blame] | 432 | CXXScopeSpec tmpSS; |
| 433 | if (Corrected.getCorrectionSpecifier()) |
| 434 | tmpSS.MakeTrivial(Context, Corrected.getCorrectionSpecifier(), |
| 435 | SourceRange(IILoc)); |
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 436 | SuggestedType = getTypeName(*Corrected.getCorrectionAsIdentifierInfo(), |
Kaelyn Uhrain | a934c31 | 2013-09-26 21:13:05 +0000 | [diff] [blame] | 437 | IILoc, S, tmpSS.isSet() ? &tmpSS : SS, false, |
| 438 | false, ParsedType(), |
Abramo Bagnara | fad03b7 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 439 | /*IsCtorOrDtorName=*/false, |
Kaelyn Uhrain | 43e875d | 2012-01-18 21:41:41 +0000 | [diff] [blame] | 440 | /*NonTrivialTypeSourceInfo=*/true); |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 441 | } |
Kaelyn Uhrain | 43e875d | 2012-01-18 21:41:41 +0000 | [diff] [blame] | 442 | return true; |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 443 | } |
| 444 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 445 | if (getLangOpts().CPlusPlus) { |
Jeffrey Yasskin | c173be2 | 2010-04-08 21:04:54 +0000 | [diff] [blame] | 446 | // See if II is a class template that the user forgot to pass arguments to. |
| 447 | UnqualifiedId Name; |
Kaelyn Uhrain | 50dc12a | 2012-06-15 23:45:58 +0000 | [diff] [blame] | 448 | Name.setIdentifier(II, IILoc); |
Jeffrey Yasskin | c173be2 | 2010-04-08 21:04:54 +0000 | [diff] [blame] | 449 | CXXScopeSpec EmptySS; |
| 450 | TemplateTy TemplateResult; |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 451 | bool MemberOfUnknownSpecialization; |
Abramo Bagnara | 7c15353 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 452 | if (isTemplateName(S, SS ? *SS : EmptySS, /*hasTemplateKeyword=*/false, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 453 | Name, ParsedType(), true, TemplateResult, |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 454 | MemberOfUnknownSpecialization) == TNK_Type_template) { |
Serge Pavlov | 1806239 | 2013-08-27 13:15:56 +0000 | [diff] [blame] | 455 | TemplateName TplName = TemplateResult.get(); |
Jeffrey Yasskin | c173be2 | 2010-04-08 21:04:54 +0000 | [diff] [blame] | 456 | Diag(IILoc, diag::err_template_missing_args) << TplName; |
| 457 | if (TemplateDecl *TplDecl = TplName.getAsTemplateDecl()) { |
| 458 | Diag(TplDecl->getLocation(), diag::note_template_decl_here) |
| 459 | << TplDecl->getTemplateParameters()->getSourceRange(); |
| 460 | } |
| 461 | return true; |
| 462 | } |
| 463 | } |
| 464 | |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 465 | // FIXME: Should we move the logic that tries to recover from a missing tag |
| 466 | // (struct, union, enum) from Parser::ParseImplicitInt here, instead? |
| 467 | |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 468 | if (!SS || (!SS->isSet() && !SS->isInvalid())) |
Kaelyn Uhrain | 50dc12a | 2012-06-15 23:45:58 +0000 | [diff] [blame] | 469 | Diag(IILoc, diag::err_unknown_typename) << II; |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 470 | else if (DeclContext *DC = computeDeclContext(*SS, false)) |
| 471 | Diag(IILoc, diag::err_typename_nested_not_found) |
Kaelyn Uhrain | 50dc12a | 2012-06-15 23:45:58 +0000 | [diff] [blame] | 472 | << II << DC << SS->getRange(); |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 473 | else if (isDependentScopeSpecifier(*SS)) { |
Francois Pichet | 6943e9b | 2011-04-13 02:38:49 +0000 | [diff] [blame] | 474 | unsigned DiagID = diag::err_typename_missing; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 475 | if (getLangOpts().MSVCCompat && isMicrosoftMissingTypename(SS, S)) |
Francois Pichet | cf320c6 | 2011-04-22 08:25:24 +0000 | [diff] [blame] | 476 | DiagID = diag::warn_typename_missing; |
Francois Pichet | 6943e9b | 2011-04-13 02:38:49 +0000 | [diff] [blame] | 477 | |
| 478 | Diag(SS->getRange().getBegin(), DiagID) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 479 | << SS->getScopeRep() << II->getName() |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 480 | << SourceRange(SS->getRange().getBegin(), IILoc) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 481 | << FixItHint::CreateInsertion(SS->getRange().getBegin(), "typename "); |
Kaelyn Uhrain | 50dc12a | 2012-06-15 23:45:58 +0000 | [diff] [blame] | 482 | SuggestedType = ActOnTypenameType(S, SourceLocation(), |
| 483 | *SS, *II, IILoc).get(); |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 484 | } else { |
| 485 | assert(SS && SS->isInvalid() && |
| 486 | "Invalid scope specifier has already been diagnosed"); |
| 487 | } |
| 488 | |
| 489 | return true; |
| 490 | } |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 491 | |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 492 | /// \brief Determine whether the given result set contains either a type name |
| 493 | /// or |
| 494 | static bool isResultTypeOrTemplate(LookupResult &R, const Token &NextToken) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 495 | bool CheckTemplate = R.getSema().getLangOpts().CPlusPlus && |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 496 | NextToken.is(tok::less); |
| 497 | |
| 498 | for (LookupResult::iterator I = R.begin(), IEnd = R.end(); I != IEnd; ++I) { |
| 499 | if (isa<TypeDecl>(*I) || isa<ObjCInterfaceDecl>(*I)) |
| 500 | return true; |
| 501 | |
| 502 | if (CheckTemplate && isa<TemplateDecl>(*I)) |
| 503 | return true; |
| 504 | } |
| 505 | |
| 506 | return false; |
| 507 | } |
| 508 | |
Kaelyn Uhrain | 12f3297 | 2012-05-02 00:11:40 +0000 | [diff] [blame] | 509 | static bool isTagTypeWithMissingTag(Sema &SemaRef, LookupResult &Result, |
| 510 | Scope *S, CXXScopeSpec &SS, |
| 511 | IdentifierInfo *&Name, |
| 512 | SourceLocation NameLoc) { |
Richard Smith | 69e4826 | 2012-09-06 01:37:56 +0000 | [diff] [blame] | 513 | LookupResult R(SemaRef, Name, NameLoc, Sema::LookupTagName); |
| 514 | SemaRef.LookupParsedName(R, S, &SS); |
| 515 | if (TagDecl *Tag = R.getAsSingle<TagDecl>()) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 516 | StringRef FixItTagName; |
Kaelyn Uhrain | 12f3297 | 2012-05-02 00:11:40 +0000 | [diff] [blame] | 517 | switch (Tag->getTagKind()) { |
| 518 | case TTK_Class: |
Kaelyn Uhrain | 12f3297 | 2012-05-02 00:11:40 +0000 | [diff] [blame] | 519 | FixItTagName = "class "; |
| 520 | break; |
| 521 | |
| 522 | case TTK_Enum: |
Kaelyn Uhrain | 12f3297 | 2012-05-02 00:11:40 +0000 | [diff] [blame] | 523 | FixItTagName = "enum "; |
| 524 | break; |
| 525 | |
| 526 | case TTK_Struct: |
Kaelyn Uhrain | 12f3297 | 2012-05-02 00:11:40 +0000 | [diff] [blame] | 527 | FixItTagName = "struct "; |
| 528 | break; |
| 529 | |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 530 | case TTK_Interface: |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 531 | FixItTagName = "__interface "; |
| 532 | break; |
| 533 | |
Kaelyn Uhrain | 12f3297 | 2012-05-02 00:11:40 +0000 | [diff] [blame] | 534 | case TTK_Union: |
Kaelyn Uhrain | 12f3297 | 2012-05-02 00:11:40 +0000 | [diff] [blame] | 535 | FixItTagName = "union "; |
| 536 | break; |
| 537 | } |
| 538 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 539 | StringRef TagName = FixItTagName.drop_back(); |
Kaelyn Uhrain | 12f3297 | 2012-05-02 00:11:40 +0000 | [diff] [blame] | 540 | SemaRef.Diag(NameLoc, diag::err_use_of_tag_name_without_tag) |
| 541 | << Name << TagName << SemaRef.getLangOpts().CPlusPlus |
| 542 | << FixItHint::CreateInsertion(NameLoc, FixItTagName); |
| 543 | |
Richard Smith | 69e4826 | 2012-09-06 01:37:56 +0000 | [diff] [blame] | 544 | for (LookupResult::iterator I = Result.begin(), IEnd = Result.end(); |
| 545 | I != IEnd; ++I) |
| 546 | SemaRef.Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type) |
| 547 | << Name << TagName; |
| 548 | |
| 549 | // Replace lookup results with just the tag decl. |
| 550 | Result.clear(Sema::LookupTagName); |
| 551 | SemaRef.LookupParsedName(Result, S, &SS); |
Kaelyn Uhrain | 12f3297 | 2012-05-02 00:11:40 +0000 | [diff] [blame] | 552 | return true; |
| 553 | } |
| 554 | |
Kaelyn Uhrain | 12f3297 | 2012-05-02 00:11:40 +0000 | [diff] [blame] | 555 | return false; |
| 556 | } |
| 557 | |
Richard Smith | 0576681 | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 558 | /// Build a ParsedType for a simple-type-specifier with a nested-name-specifier. |
| 559 | static ParsedType buildNestedType(Sema &S, CXXScopeSpec &SS, |
| 560 | QualType T, SourceLocation NameLoc) { |
| 561 | ASTContext &Context = S.Context; |
| 562 | |
| 563 | TypeLocBuilder Builder; |
| 564 | Builder.pushTypeSpec(T).setNameLoc(NameLoc); |
| 565 | |
| 566 | T = S.getElaboratedType(ETK_None, SS, T); |
| 567 | ElaboratedTypeLoc ElabTL = Builder.push<ElaboratedTypeLoc>(T); |
| 568 | ElabTL.setElaboratedKeywordLoc(SourceLocation()); |
| 569 | ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 570 | return S.CreateParsedType(T, Builder.getTypeSourceInfo(Context, T)); |
| 571 | } |
| 572 | |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 573 | Sema::NameClassification Sema::ClassifyName(Scope *S, |
| 574 | CXXScopeSpec &SS, |
| 575 | IdentifierInfo *&Name, |
| 576 | SourceLocation NameLoc, |
Richard Smith | 0576681 | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 577 | const Token &NextToken, |
| 578 | bool IsAddressOfOperand, |
| 579 | CorrectionCandidateCallback *CCC) { |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 580 | DeclarationNameInfo NameInfo(Name, NameLoc); |
| 581 | ObjCMethodDecl *CurMethod = getCurMethodDecl(); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 582 | |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 583 | if (NextToken.is(tok::coloncolon)) { |
| 584 | BuildCXXNestedNameSpecifier(S, *Name, NameLoc, NextToken.getLocation(), |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 585 | QualType(), false, SS, nullptr, false); |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 586 | } |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 587 | |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 588 | LookupResult Result(*this, Name, NameLoc, LookupOrdinaryName); |
| 589 | LookupParsedName(Result, S, &SS, !CurMethod); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 590 | |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 591 | // Perform lookup for Objective-C instance variables (including automatically |
| 592 | // synthesized instance variables), if we're in an Objective-C method. |
| 593 | // FIXME: This lookup really, really needs to be folded in to the normal |
| 594 | // unqualified lookup mechanism. |
| 595 | if (!SS.isSet() && CurMethod && !isResultTypeOrTemplate(Result, NextToken)) { |
| 596 | ExprResult E = LookupInObjCMethod(Result, S, Name, true); |
Douglas Gregor | ec385cf | 2011-04-25 15:05:41 +0000 | [diff] [blame] | 597 | if (E.get() || E.isInvalid()) |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 598 | return E; |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | bool SecondTry = false; |
| 602 | bool IsFilteredTemplateName = false; |
| 603 | |
| 604 | Corrected: |
| 605 | switch (Result.getResultKind()) { |
| 606 | case LookupResult::NotFound: |
| 607 | // If an unqualified-id is followed by a '(', then we have a function |
| 608 | // call. |
| 609 | if (!SS.isSet() && NextToken.is(tok::l_paren)) { |
| 610 | // In C++, this is an ADL-only call. |
| 611 | // FIXME: Reference? |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 612 | if (getLangOpts().CPlusPlus) |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 613 | return BuildDeclarationNameExpr(SS, Result, /*ADL=*/true); |
| 614 | |
| 615 | // C90 6.3.2.2: |
| 616 | // If the expression that precedes the parenthesized argument list in a |
| 617 | // function call consists solely of an identifier, and if no |
| 618 | // declaration is visible for this identifier, the identifier is |
| 619 | // implicitly declared exactly as if, in the innermost block containing |
| 620 | // the function call, the declaration |
| 621 | // |
| 622 | // extern int identifier (); |
| 623 | // |
| 624 | // appeared. |
| 625 | // |
| 626 | // We also allow this in C99 as an extension. |
| 627 | if (NamedDecl *D = ImplicitlyDefineFunction(NameLoc, *Name, S)) { |
| 628 | Result.addDecl(D); |
| 629 | Result.resolveKind(); |
| 630 | return BuildDeclarationNameExpr(SS, Result, /*ADL=*/false); |
| 631 | } |
| 632 | } |
| 633 | |
| 634 | // In C, we first see whether there is a tag type by the same name, in |
| 635 | // which case it's likely that the user just forget to write "enum", |
| 636 | // "struct", or "union". |
Kaelyn Uhrain | 12f3297 | 2012-05-02 00:11:40 +0000 | [diff] [blame] | 637 | if (!getLangOpts().CPlusPlus && !SecondTry && |
| 638 | isTagTypeWithMissingTag(*this, Result, S, SS, Name, NameLoc)) { |
| 639 | break; |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | // Perform typo correction to determine if there is another name that is |
| 643 | // close to this name. |
Richard Smith | 0576681 | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 644 | if (!SecondTry && CCC) { |
Douglas Gregor | 3a348c8 | 2011-07-14 04:54:23 +0000 | [diff] [blame] | 645 | SecondTry = true; |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 646 | if (TypoCorrection Corrected = CorrectTypo(Result.getLookupNameInfo(), |
David Blaikie | d662a79 | 2011-10-19 22:56:21 +0000 | [diff] [blame] | 647 | Result.getLookupKind(), S, |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 648 | &SS, *CCC, |
| 649 | CTK_ErrorRecovery)) { |
Douglas Gregor | 27766d2 | 2011-04-27 03:47:06 +0000 | [diff] [blame] | 650 | unsigned UnqualifiedDiag = diag::err_undeclared_var_use_suggest; |
| 651 | unsigned QualifiedDiag = diag::err_no_member_suggest; |
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 652 | |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 653 | NamedDecl *FirstDecl = Corrected.getCorrectionDecl(); |
Douglas Gregor | 3b88735 | 2011-04-27 04:48:22 +0000 | [diff] [blame] | 654 | NamedDecl *UnderlyingFirstDecl |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 655 | = FirstDecl? FirstDecl->getUnderlyingDecl() : nullptr; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 656 | if (getLangOpts().CPlusPlus && NextToken.is(tok::less) && |
Douglas Gregor | 3b88735 | 2011-04-27 04:48:22 +0000 | [diff] [blame] | 657 | UnderlyingFirstDecl && isa<TemplateDecl>(UnderlyingFirstDecl)) { |
Douglas Gregor | 27766d2 | 2011-04-27 03:47:06 +0000 | [diff] [blame] | 658 | UnqualifiedDiag = diag::err_no_template_suggest; |
| 659 | QualifiedDiag = diag::err_no_member_template_suggest; |
Douglas Gregor | 3b88735 | 2011-04-27 04:48:22 +0000 | [diff] [blame] | 660 | } else if (UnderlyingFirstDecl && |
| 661 | (isa<TypeDecl>(UnderlyingFirstDecl) || |
| 662 | isa<ObjCInterfaceDecl>(UnderlyingFirstDecl) || |
| 663 | isa<ObjCCompatibleAliasDecl>(UnderlyingFirstDecl))) { |
David Blaikie | 30262b7 | 2013-03-21 21:35:15 +0000 | [diff] [blame] | 664 | UnqualifiedDiag = diag::err_unknown_typename_suggest; |
| 665 | QualifiedDiag = diag::err_unknown_nested_typename_suggest; |
| 666 | } |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 667 | |
Kaelyn Uhrain | b2567dd | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 668 | if (SS.isEmpty()) { |
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 669 | diagnoseTypo(Corrected, PDiag(UnqualifiedDiag) << Name); |
Kaelyn Uhrain | b2567dd | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 670 | } else {// FIXME: is this even reachable? Test it. |
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 671 | std::string CorrectedStr(Corrected.getAsString(getLangOpts())); |
| 672 | bool DroppedSpecifier = Corrected.WillReplaceSpecifier() && |
Kaelyn Uhrain | b2567dd | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 673 | Name->getName().equals(CorrectedStr); |
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 674 | diagnoseTypo(Corrected, PDiag(QualifiedDiag) |
| 675 | << Name << computeDeclContext(SS, false) |
| 676 | << DroppedSpecifier << SS.getRange()); |
Kaelyn Uhrain | b2567dd | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 677 | } |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 678 | |
| 679 | // Update the name, so that the caller has the new name. |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 680 | Name = Corrected.getCorrectionAsIdentifierInfo(); |
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 681 | |
Kaelyn Uhrain | a5ee634 | 2012-01-24 19:45:35 +0000 | [diff] [blame] | 682 | // Typo correction corrected to a keyword. |
| 683 | if (Corrected.isKeyword()) |
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 684 | return Name; |
Kaelyn Uhrain | a5ee634 | 2012-01-24 19:45:35 +0000 | [diff] [blame] | 685 | |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 686 | // Also update the LookupResult... |
| 687 | // FIXME: This should probably go away at some point |
| 688 | Result.clear(); |
| 689 | Result.setLookupName(Corrected.getCorrection()); |
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 690 | if (FirstDecl) |
Kaelyn Uhrain | a5ee634 | 2012-01-24 19:45:35 +0000 | [diff] [blame] | 691 | Result.addDecl(FirstDecl); |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 692 | |
| 693 | // If we found an Objective-C instance variable, let |
| 694 | // LookupInObjCMethod build the appropriate expression to |
| 695 | // reference the ivar. |
| 696 | // FIXME: This is a gross hack. |
| 697 | if (ObjCIvarDecl *Ivar = Result.getAsSingle<ObjCIvarDecl>()) { |
| 698 | Result.clear(); |
| 699 | ExprResult E(LookupInObjCMethod(Result, S, Ivar->getIdentifier())); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 700 | return E; |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 701 | } |
| 702 | |
| 703 | goto Corrected; |
| 704 | } |
| 705 | } |
| 706 | |
| 707 | // We failed to correct; just fall through and let the parser deal with it. |
| 708 | Result.suppressDiagnostics(); |
| 709 | return NameClassification::Unknown(); |
| 710 | |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 711 | case LookupResult::NotFoundInCurrentInstantiation: { |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 712 | // We performed name lookup into the current instantiation, and there were |
| 713 | // dependent bases, so we treat this result the same way as any other |
| 714 | // dependent nested-name-specifier. |
| 715 | |
| 716 | // C++ [temp.res]p2: |
| 717 | // A name used in a template declaration or definition and that is |
| 718 | // dependent on a template-parameter is assumed not to name a type |
| 719 | // unless the applicable name lookup finds a type name or the name is |
| 720 | // qualified by the keyword typename. |
| 721 | // |
| 722 | // FIXME: If the next token is '<', we might want to ask the parser to |
| 723 | // perform some heroics to see if we actually have a |
| 724 | // template-argument-list, which would indicate a missing 'template' |
| 725 | // keyword here. |
Richard Smith | 0576681 | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 726 | return ActOnDependentIdExpression(SS, /*TemplateKWLoc=*/SourceLocation(), |
| 727 | NameInfo, IsAddressOfOperand, |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 728 | /*TemplateArgs=*/nullptr); |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 729 | } |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 730 | |
| 731 | case LookupResult::Found: |
| 732 | case LookupResult::FoundOverloaded: |
| 733 | case LookupResult::FoundUnresolvedValue: |
| 734 | break; |
| 735 | |
| 736 | case LookupResult::Ambiguous: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 737 | if (getLangOpts().CPlusPlus && NextToken.is(tok::less) && |
Douglas Gregor | 3b88735 | 2011-04-27 04:48:22 +0000 | [diff] [blame] | 738 | hasAnyAcceptableTemplateNames(Result)) { |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 739 | // C++ [temp.local]p3: |
| 740 | // A lookup that finds an injected-class-name (10.2) can result in an |
| 741 | // ambiguity in certain cases (for example, if it is found in more than |
| 742 | // one base class). If all of the injected-class-names that are found |
| 743 | // refer to specializations of the same class template, and if the name |
| 744 | // is followed by a template-argument-list, the reference refers to the |
| 745 | // class template itself and not a specialization thereof, and is not |
| 746 | // ambiguous. |
| 747 | // |
| 748 | // This filtering can make an ambiguous result into an unambiguous one, |
| 749 | // so try again after filtering out template names. |
| 750 | FilterAcceptableTemplateNames(Result); |
| 751 | if (!Result.isAmbiguous()) { |
| 752 | IsFilteredTemplateName = true; |
| 753 | break; |
| 754 | } |
| 755 | } |
| 756 | |
| 757 | // Diagnose the ambiguity and return an error. |
| 758 | return NameClassification::Error(); |
| 759 | } |
| 760 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 761 | if (getLangOpts().CPlusPlus && NextToken.is(tok::less) && |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 762 | (IsFilteredTemplateName || hasAnyAcceptableTemplateNames(Result))) { |
| 763 | // C++ [temp.names]p3: |
| 764 | // After name lookup (3.4) finds that a name is a template-name or that |
| 765 | // an operator-function-id or a literal- operator-id refers to a set of |
| 766 | // overloaded functions any member of which is a function template if |
| 767 | // this is followed by a <, the < is always taken as the delimiter of a |
| 768 | // template-argument-list and never as the less-than operator. |
| 769 | if (!IsFilteredTemplateName) |
| 770 | FilterAcceptableTemplateNames(Result); |
| 771 | |
Douglas Gregor | 3b88735 | 2011-04-27 04:48:22 +0000 | [diff] [blame] | 772 | if (!Result.empty()) { |
| 773 | bool IsFunctionTemplate; |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 774 | bool IsVarTemplate; |
Douglas Gregor | 3b88735 | 2011-04-27 04:48:22 +0000 | [diff] [blame] | 775 | TemplateName Template; |
| 776 | if (Result.end() - Result.begin() > 1) { |
| 777 | IsFunctionTemplate = true; |
| 778 | Template = Context.getOverloadedTemplateName(Result.begin(), |
| 779 | Result.end()); |
| 780 | } else { |
| 781 | TemplateDecl *TD |
| 782 | = cast<TemplateDecl>((*Result.begin())->getUnderlyingDecl()); |
| 783 | IsFunctionTemplate = isa<FunctionTemplateDecl>(TD); |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 784 | IsVarTemplate = isa<VarTemplateDecl>(TD); |
| 785 | |
Douglas Gregor | 3b88735 | 2011-04-27 04:48:22 +0000 | [diff] [blame] | 786 | if (SS.isSet() && !SS.isInvalid()) |
| 787 | Template = Context.getQualifiedTemplateName(SS.getScopeRep(), |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 788 | /*TemplateKeyword=*/false, |
Douglas Gregor | 3b88735 | 2011-04-27 04:48:22 +0000 | [diff] [blame] | 789 | TD); |
| 790 | else |
| 791 | Template = TemplateName(TD); |
| 792 | } |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 793 | |
Douglas Gregor | 3b88735 | 2011-04-27 04:48:22 +0000 | [diff] [blame] | 794 | if (IsFunctionTemplate) { |
| 795 | // Function templates always go through overload resolution, at which |
| 796 | // point we'll perform the various checks (e.g., accessibility) we need |
| 797 | // to based on which function we selected. |
| 798 | Result.suppressDiagnostics(); |
| 799 | |
| 800 | return NameClassification::FunctionTemplate(Template); |
| 801 | } |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 802 | |
| 803 | return IsVarTemplate ? NameClassification::VarTemplate(Template) |
| 804 | : NameClassification::TypeTemplate(Template); |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 805 | } |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 806 | } |
Richard Smith | 0576681 | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 807 | |
Douglas Gregor | 3b88735 | 2011-04-27 04:48:22 +0000 | [diff] [blame] | 808 | NamedDecl *FirstDecl = (*Result.begin())->getUnderlyingDecl(); |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 809 | if (TypeDecl *Type = dyn_cast<TypeDecl>(FirstDecl)) { |
| 810 | DiagnoseUseOfDecl(Type, NameLoc); |
| 811 | QualType T = Context.getTypeDeclType(Type); |
Richard Smith | 0576681 | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 812 | if (SS.isNotEmpty()) |
| 813 | return buildNestedType(*this, SS, T, NameLoc); |
Kaelyn Uhrain | 12f3297 | 2012-05-02 00:11:40 +0000 | [diff] [blame] | 814 | return ParsedType::make(T); |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 815 | } |
Richard Smith | 0576681 | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 816 | |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 817 | ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(FirstDecl); |
| 818 | if (!Class) { |
| 819 | // FIXME: It's unfortunate that we don't have a Type node for handling this. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 820 | if (ObjCCompatibleAliasDecl *Alias = |
| 821 | dyn_cast<ObjCCompatibleAliasDecl>(FirstDecl)) |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 822 | Class = Alias->getClassInterface(); |
| 823 | } |
| 824 | |
| 825 | if (Class) { |
| 826 | DiagnoseUseOfDecl(Class, NameLoc); |
| 827 | |
| 828 | if (NextToken.is(tok::period)) { |
| 829 | // Interface. <something> is parsed as a property reference expression. |
| 830 | // Just return "unknown" as a fall-through for now. |
| 831 | Result.suppressDiagnostics(); |
| 832 | return NameClassification::Unknown(); |
| 833 | } |
| 834 | |
| 835 | QualType T = Context.getObjCInterfaceType(Class); |
| 836 | return ParsedType::make(T); |
| 837 | } |
Kaelyn Uhrain | 12f3297 | 2012-05-02 00:11:40 +0000 | [diff] [blame] | 838 | |
Richard Smith | 0576681 | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 839 | // We can have a type template here if we're classifying a template argument. |
| 840 | if (isa<TemplateDecl>(FirstDecl) && !isa<FunctionTemplateDecl>(FirstDecl)) |
| 841 | return NameClassification::TypeTemplate( |
| 842 | TemplateName(cast<TemplateDecl>(FirstDecl))); |
| 843 | |
Kaelyn Uhrain | 12f3297 | 2012-05-02 00:11:40 +0000 | [diff] [blame] | 844 | // Check for a tag type hidden by a non-type decl in a few cases where it |
| 845 | // seems likely a type is wanted instead of the non-type that was found. |
Argyrios Kyrtzidis | 99e9fe0 | 2013-05-07 19:54:28 +0000 | [diff] [blame] | 846 | bool NextIsOp = NextToken.is(tok::amp) || NextToken.is(tok::star); |
| 847 | if ((NextToken.is(tok::identifier) || |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 848 | (NextIsOp && |
| 849 | FirstDecl->getUnderlyingDecl()->isFunctionOrFunctionTemplate())) && |
Argyrios Kyrtzidis | 99e9fe0 | 2013-05-07 19:54:28 +0000 | [diff] [blame] | 850 | isTagTypeWithMissingTag(*this, Result, S, SS, Name, NameLoc)) { |
| 851 | TypeDecl *Type = Result.getAsSingle<TypeDecl>(); |
| 852 | DiagnoseUseOfDecl(Type, NameLoc); |
| 853 | QualType T = Context.getTypeDeclType(Type); |
| 854 | if (SS.isNotEmpty()) |
| 855 | return buildNestedType(*this, SS, T, NameLoc); |
| 856 | return ParsedType::make(T); |
Kaelyn Uhrain | 12f3297 | 2012-05-02 00:11:40 +0000 | [diff] [blame] | 857 | } |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 858 | |
Richard Smith | 0576681 | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 859 | if (FirstDecl->isCXXClassMember()) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 860 | return BuildPossibleImplicitMemberExpr(SS, SourceLocation(), Result, |
| 861 | nullptr); |
Douglas Gregor | 3b88735 | 2011-04-27 04:48:22 +0000 | [diff] [blame] | 862 | |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 863 | bool ADL = UseArgumentDependentLookup(SS, Result, NextToken.is(tok::l_paren)); |
| 864 | return BuildDeclarationNameExpr(SS, Result, ADL); |
| 865 | } |
| 866 | |
John McCall | 88232aa | 2009-08-18 00:00:49 +0000 | [diff] [blame] | 867 | // Determines the context to return to after temporarily entering a |
| 868 | // context. This depends in an unnecessarily complicated way on the |
| 869 | // exact ordering of callbacks from the parser. |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 870 | DeclContext *Sema::getContainingDC(DeclContext *DC) { |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 871 | |
John McCall | 88232aa | 2009-08-18 00:00:49 +0000 | [diff] [blame] | 872 | // Functions defined inline within classes aren't parsed until we've |
| 873 | // finished parsing the top-level class, so the top-level class is |
| 874 | // the context we'll need to return to. |
Bill Wendling | b7accd0 | 2013-12-05 05:24:30 +0000 | [diff] [blame] | 875 | // A Lambda call operator whose parent is a class must not be treated |
| 876 | // as an inline member function. A Lambda can be used legally |
| 877 | // either as an in-class member initializer or a default argument. These |
| 878 | // are parsed once the class has been marked complete and so the containing |
| 879 | // context would be the nested class (when the lambda is defined in one); |
| 880 | // If the class is not complete, then the lambda is being used in an |
| 881 | // ill-formed fashion (such as to specify the width of a bit-field, or |
| 882 | // in an array-bound) - in which case we still want to return the |
| 883 | // lexically containing DC (which could be a nested class). |
| 884 | if (isa<FunctionDecl>(DC) && !isLambdaCallOperator(DC)) { |
John McCall | 88232aa | 2009-08-18 00:00:49 +0000 | [diff] [blame] | 885 | DC = DC->getLexicalParent(); |
| 886 | |
| 887 | // A function not defined within a class will always return to its |
| 888 | // lexical context. |
| 889 | if (!isa<CXXRecordDecl>(DC)) |
| 890 | return DC; |
| 891 | |
| 892 | // A C++ inline method/friend is parsed *after* the topmost class |
| 893 | // it was declared in is fully parsed ("complete"); the topmost |
| 894 | // class is the context we need to return to. |
Argyrios Kyrtzidis | 77407b8 | 2008-11-19 18:01:13 +0000 | [diff] [blame] | 895 | while (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(DC->getLexicalParent())) |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 896 | DC = RD; |
| 897 | |
| 898 | // Return the declaration context of the topmost class the inline method is |
| 899 | // declared in. |
| 900 | return DC; |
| 901 | } |
| 902 | |
Argyrios Kyrtzidis | 77407b8 | 2008-11-19 18:01:13 +0000 | [diff] [blame] | 903 | return DC->getLexicalParent(); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 904 | } |
| 905 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 906 | void Sema::PushDeclContext(Scope *S, DeclContext *DC) { |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 907 | assert(getContainingDC(DC) == CurContext && |
Zhongxing Xu | e50897a | 2008-12-08 07:14:51 +0000 | [diff] [blame] | 908 | "The next DeclContext should be lexically contained in the current one."); |
Chris Lattner | 9fdf9c6 | 2008-04-22 18:39:57 +0000 | [diff] [blame] | 909 | CurContext = DC; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 910 | S->setEntity(DC); |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 911 | } |
| 912 | |
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 913 | void Sema::PopDeclContext() { |
| 914 | assert(CurContext && "DeclContext imbalance!"); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 915 | |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 916 | CurContext = getContainingDC(CurContext); |
John McCall | acb7039 | 2010-07-23 22:45:07 +0000 | [diff] [blame] | 917 | assert(CurContext && "Popped translation unit!"); |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 918 | } |
| 919 | |
Argyrios Kyrtzidis | 179fe1a | 2009-06-17 23:19:02 +0000 | [diff] [blame] | 920 | /// EnterDeclaratorContext - Used when we must lookup names in the context |
| 921 | /// of a declarator's nested name specifier. |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 922 | /// |
Argyrios Kyrtzidis | 1d17553 | 2009-06-17 23:15:40 +0000 | [diff] [blame] | 923 | void Sema::EnterDeclaratorContext(Scope *S, DeclContext *DC) { |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 924 | // C++0x [basic.lookup.unqual]p13: |
| 925 | // A name used in the definition of a static data member of class |
| 926 | // X (after the qualified-id of the static member) is looked up as |
| 927 | // if the name was used in a member function of X. |
| 928 | // C++0x [basic.lookup.unqual]p14: |
| 929 | // If a variable member of a namespace is defined outside of the |
| 930 | // scope of its namespace then any name used in the definition of |
| 931 | // the variable member (after the declarator-id) is looked up as |
| 932 | // if the definition of the variable member occurred in its |
| 933 | // namespace. |
| 934 | // Both of these imply that we should push a scope whose context |
| 935 | // is the semantic context of the declaration. We can't use |
| 936 | // PushDeclContext here because that context is not necessarily |
| 937 | // lexically contained in the current context. Fortunately, |
| 938 | // the containing scope should have the appropriate information. |
| 939 | |
| 940 | assert(!S->getEntity() && "scope already has entity"); |
| 941 | |
| 942 | #ifndef NDEBUG |
| 943 | Scope *Ancestor = S->getParent(); |
| 944 | while (!Ancestor->getEntity()) Ancestor = Ancestor->getParent(); |
| 945 | assert(Ancestor->getEntity() == CurContext && "ancestor context mismatch"); |
| 946 | #endif |
| 947 | |
Argyrios Kyrtzidis | 1d17553 | 2009-06-17 23:15:40 +0000 | [diff] [blame] | 948 | CurContext = DC; |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 949 | S->setEntity(DC); |
Argyrios Kyrtzidis | 1d17553 | 2009-06-17 23:15:40 +0000 | [diff] [blame] | 950 | } |
| 951 | |
Argyrios Kyrtzidis | 1d17553 | 2009-06-17 23:15:40 +0000 | [diff] [blame] | 952 | void Sema::ExitDeclaratorContext(Scope *S) { |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 953 | assert(S->getEntity() == CurContext && "Context imbalance!"); |
Argyrios Kyrtzidis | 1d17553 | 2009-06-17 23:15:40 +0000 | [diff] [blame] | 954 | |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 955 | // Switch back to the lexical context. The safety of this is |
| 956 | // enforced by an assert in EnterDeclaratorContext. |
| 957 | Scope *Ancestor = S->getParent(); |
| 958 | while (!Ancestor->getEntity()) Ancestor = Ancestor->getParent(); |
Ted Kremenek | f0d5861 | 2013-10-08 17:08:03 +0000 | [diff] [blame] | 959 | CurContext = Ancestor->getEntity(); |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 960 | |
| 961 | // We don't need to do anything with the scope, which is going to |
| 962 | // disappear. |
Argyrios Kyrtzidis | 1d17553 | 2009-06-17 23:15:40 +0000 | [diff] [blame] | 963 | } |
| 964 | |
Caitlin Sadowski | ed9d84a | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 965 | |
| 966 | void Sema::ActOnReenterFunctionContext(Scope* S, Decl *D) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 967 | // We assume that the caller has already called |
| 968 | // ActOnReenterTemplateScope so getTemplatedDecl() works. |
| 969 | FunctionDecl *FD = D->getAsFunction(); |
Caitlin Sadowski | ed9d84a | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 970 | if (!FD) |
| 971 | return; |
| 972 | |
DeLesley Hutchins | cf2fa2f | 2012-04-06 15:10:17 +0000 | [diff] [blame] | 973 | // Same implementation as PushDeclContext, but enters the context |
| 974 | // from the lexical parent, rather than the top-level class. |
| 975 | assert(CurContext == FD->getLexicalParent() && |
| 976 | "The next DeclContext should be lexically contained in the current one."); |
| 977 | CurContext = FD; |
| 978 | S->setEntity(CurContext); |
| 979 | |
Caitlin Sadowski | ed9d84a | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 980 | for (unsigned P = 0, NumParams = FD->getNumParams(); P < NumParams; ++P) { |
| 981 | ParmVarDecl *Param = FD->getParamDecl(P); |
| 982 | // If the parameter has an identifier, then add it to the scope |
| 983 | if (Param->getIdentifier()) { |
| 984 | S->AddDecl(Param); |
| 985 | IdResolver.AddDecl(Param); |
| 986 | } |
| 987 | } |
| 988 | } |
| 989 | |
| 990 | |
DeLesley Hutchins | cf2fa2f | 2012-04-06 15:10:17 +0000 | [diff] [blame] | 991 | void Sema::ActOnExitFunctionContext() { |
| 992 | // Same implementation as PopDeclContext, but returns to the lexical parent, |
| 993 | // rather than the top-level class. |
| 994 | assert(CurContext && "DeclContext imbalance!"); |
| 995 | CurContext = CurContext->getLexicalParent(); |
| 996 | assert(CurContext && "Popped translation unit!"); |
| 997 | } |
| 998 | |
| 999 | |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1000 | /// \brief Determine whether we allow overloading of the function |
| 1001 | /// PrevDecl with another declaration. |
| 1002 | /// |
| 1003 | /// This routine determines whether overloading is possible, not |
| 1004 | /// whether some new function is actually an overload. It will return |
| 1005 | /// true in C++ (where we can always provide overloads) or, as an |
| 1006 | /// extension, in C when the previous function is already an |
| 1007 | /// overloaded function declaration or has the "overloadable" |
| 1008 | /// attribute. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1009 | static bool AllowOverloadingOfFunction(LookupResult &Previous, |
| 1010 | ASTContext &Context) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1011 | if (Context.getLangOpts().CPlusPlus) |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1012 | return true; |
| 1013 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1014 | if (Previous.getResultKind() == LookupResult::FoundOverloaded) |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1015 | return true; |
| 1016 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1017 | return (Previous.getResultKind() == LookupResult::Found |
| 1018 | && Previous.getFoundDecl()->hasAttr<OverloadableAttr>()); |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1019 | } |
| 1020 | |
Argyrios Kyrtzidis | 87f3ff0 | 2008-04-12 00:47:19 +0000 | [diff] [blame] | 1021 | /// Add this decl to the scope shadowed decl chains. |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 1022 | void Sema::PushOnScopeChains(NamedDecl *D, Scope *S, bool AddToContext) { |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 1023 | // Move up the scope chain until we find the nearest enclosing |
| 1024 | // non-transparent context. The declaration will be introduced into this |
| 1025 | // scope. |
Ted Kremenek | f0d5861 | 2013-10-08 17:08:03 +0000 | [diff] [blame] | 1026 | while (S->getEntity() && S->getEntity()->isTransparentContext()) |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 1027 | S = S->getParent(); |
| 1028 | |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 1029 | // Add scoped declarations into their context, so that they can be |
| 1030 | // found later. Declarations without a context won't be inserted |
| 1031 | // into any context. |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 1032 | if (AddToContext) |
| 1033 | CurContext->addDecl(D); |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 1034 | |
Richard Smith | a41c97a | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 1035 | // Out-of-line definitions shouldn't be pushed into scope in C++, unless they |
| 1036 | // are function-local declarations. |
| 1037 | if (getLangOpts().CPlusPlus && D->isOutOfLine() && |
Douglas Gregor | 6d0468b | 2011-10-09 22:57:49 +0000 | [diff] [blame] | 1038 | !D->getDeclContext()->getRedeclContext()->Equals( |
Richard Smith | a41c97a | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 1039 | D->getLexicalDeclContext()->getRedeclContext()) && |
| 1040 | !D->getLexicalDeclContext()->isFunctionOrMethod()) |
Chandler Carruth | 8761d68 | 2010-02-21 07:08:09 +0000 | [diff] [blame] | 1041 | return; |
| 1042 | |
| 1043 | // Template instantiations should also not be pushed into scope. |
| 1044 | if (isa<FunctionDecl>(D) && |
| 1045 | cast<FunctionDecl>(D)->isFunctionTemplateSpecialization()) |
Douglas Gregor | d04b1be | 2009-09-28 18:41:37 +0000 | [diff] [blame] | 1046 | return; |
| 1047 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1048 | // If this replaces anything in the current scope, |
| 1049 | IdentifierResolver::iterator I = IdResolver.begin(D->getDeclName()), |
| 1050 | IEnd = IdResolver.end(); |
| 1051 | for (; I != IEnd; ++I) { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1052 | if (S->isDeclScope(*I) && D->declarationReplaces(*I)) { |
| 1053 | S->RemoveDecl(*I); |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1054 | IdResolver.RemoveDecl(*I); |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 1055 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1056 | // Should only need to replace one decl. |
| 1057 | break; |
Douglas Gregor | 516ff43 | 2009-04-24 02:57:34 +0000 | [diff] [blame] | 1058 | } |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 1059 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1060 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1061 | S->AddDecl(D); |
Douglas Gregor | 7cbc558 | 2011-03-14 21:19:51 +0000 | [diff] [blame] | 1062 | |
| 1063 | if (isa<LabelDecl>(D) && !cast<LabelDecl>(D)->isGnuLocal()) { |
| 1064 | // Implicitly-generated labels may end up getting generated in an order that |
| 1065 | // isn't strictly lexical, which breaks name lookup. Be careful to insert |
| 1066 | // the label at the appropriate place in the identifier chain. |
| 1067 | for (I = IdResolver.begin(D->getDeclName()); I != IEnd; ++I) { |
Douglas Gregor | 1d2de76 | 2011-03-24 14:35:16 +0000 | [diff] [blame] | 1068 | DeclContext *IDC = (*I)->getLexicalDeclContext()->getRedeclContext(); |
Douglas Gregor | 250e7a7 | 2011-03-16 16:39:03 +0000 | [diff] [blame] | 1069 | if (IDC == CurContext) { |
| 1070 | if (!S->isDeclScope(*I)) |
| 1071 | continue; |
| 1072 | } else if (IDC->Encloses(CurContext)) |
Douglas Gregor | 7cbc558 | 2011-03-14 21:19:51 +0000 | [diff] [blame] | 1073 | break; |
| 1074 | } |
| 1075 | |
Douglas Gregor | 250e7a7 | 2011-03-16 16:39:03 +0000 | [diff] [blame] | 1076 | IdResolver.InsertDeclAfter(I, D); |
Douglas Gregor | 7cbc558 | 2011-03-14 21:19:51 +0000 | [diff] [blame] | 1077 | } else { |
| 1078 | IdResolver.AddDecl(D); |
| 1079 | } |
Argyrios Kyrtzidis | 87f3ff0 | 2008-04-12 00:47:19 +0000 | [diff] [blame] | 1080 | } |
| 1081 | |
Douglas Gregor | eee242f | 2011-10-27 09:33:13 +0000 | [diff] [blame] | 1082 | void Sema::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) { |
| 1083 | if (IdResolver.tryAddTopLevelDecl(D, Name) && TUScope) |
| 1084 | TUScope->AddDecl(D); |
| 1085 | } |
| 1086 | |
Richard Smith | dd9459f | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 1087 | bool Sema::isDeclInScope(NamedDecl *D, DeclContext *Ctx, Scope *S, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1088 | bool AllowInlineNamespace) { |
| 1089 | return IdResolver.isDeclInScope(D, Ctx, S, AllowInlineNamespace); |
Douglas Gregor | 2531c2d | 2009-09-28 00:47:05 +0000 | [diff] [blame] | 1090 | } |
| 1091 | |
John McCall | 5f1e094 | 2010-08-24 08:50:51 +0000 | [diff] [blame] | 1092 | Scope *Sema::getScopeForDeclContext(Scope *S, DeclContext *DC) { |
| 1093 | DeclContext *TargetDC = DC->getPrimaryContext(); |
| 1094 | do { |
Ted Kremenek | f0d5861 | 2013-10-08 17:08:03 +0000 | [diff] [blame] | 1095 | if (DeclContext *ScopeDC = S->getEntity()) |
John McCall | 5f1e094 | 2010-08-24 08:50:51 +0000 | [diff] [blame] | 1096 | if (ScopeDC->getPrimaryContext() == TargetDC) |
| 1097 | return S; |
| 1098 | } while ((S = S->getParent())); |
| 1099 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1100 | return nullptr; |
John McCall | 5f1e094 | 2010-08-24 08:50:51 +0000 | [diff] [blame] | 1101 | } |
| 1102 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1103 | static bool isOutOfScopePreviousDeclaration(NamedDecl *, |
| 1104 | DeclContext*, |
| 1105 | ASTContext&); |
| 1106 | |
| 1107 | /// Filters out lookup results that don't fall within the given scope |
| 1108 | /// as determined by isDeclInScope. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1109 | void Sema::FilterLookupForScope(LookupResult &R, DeclContext *Ctx, Scope *S, |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1110 | bool ConsiderLinkage, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1111 | bool AllowInlineNamespace) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1112 | LookupResult::Filter F = R.makeFilter(); |
| 1113 | while (F.hasNext()) { |
| 1114 | NamedDecl *D = F.next(); |
| 1115 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1116 | if (isDeclInScope(D, Ctx, S, AllowInlineNamespace)) |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1117 | continue; |
| 1118 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1119 | if (ConsiderLinkage && isOutOfScopePreviousDeclaration(D, Ctx, Context)) |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1120 | continue; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1121 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1122 | F.erase(); |
| 1123 | } |
| 1124 | |
| 1125 | F.done(); |
| 1126 | } |
| 1127 | |
| 1128 | static bool isUsingDecl(NamedDecl *D) { |
| 1129 | return isa<UsingShadowDecl>(D) || |
| 1130 | isa<UnresolvedUsingTypenameDecl>(D) || |
| 1131 | isa<UnresolvedUsingValueDecl>(D); |
| 1132 | } |
| 1133 | |
| 1134 | /// Removes using shadow declarations from the lookup results. |
| 1135 | static void RemoveUsingDecls(LookupResult &R) { |
| 1136 | LookupResult::Filter F = R.makeFilter(); |
| 1137 | while (F.hasNext()) |
| 1138 | if (isUsingDecl(F.next())) |
| 1139 | F.erase(); |
| 1140 | |
| 1141 | F.done(); |
| 1142 | } |
| 1143 | |
Argyrios Kyrtzidis | 06999f8 | 2010-08-15 10:17:33 +0000 | [diff] [blame] | 1144 | /// \brief Check for this common pattern: |
| 1145 | /// @code |
| 1146 | /// class S { |
| 1147 | /// S(const S&); // DO NOT IMPLEMENT |
| 1148 | /// void operator=(const S&); // DO NOT IMPLEMENT |
| 1149 | /// }; |
| 1150 | /// @endcode |
| 1151 | static bool IsDisallowedCopyOrAssign(const CXXMethodDecl *D) { |
| 1152 | // FIXME: Should check for private access too but access is set after we get |
| 1153 | // the decl here. |
Sean Hunt | 10620eb | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 1154 | if (D->doesThisDeclarationHaveABody()) |
Argyrios Kyrtzidis | 06999f8 | 2010-08-15 10:17:33 +0000 | [diff] [blame] | 1155 | return false; |
| 1156 | |
| 1157 | if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(D)) |
| 1158 | return CD->isCopyConstructor(); |
Douglas Gregor | 27c08ab | 2010-09-27 22:06:20 +0000 | [diff] [blame] | 1159 | if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) |
| 1160 | return Method->isCopyAssignmentOperator(); |
| 1161 | return false; |
Argyrios Kyrtzidis | 06999f8 | 2010-08-15 10:17:33 +0000 | [diff] [blame] | 1162 | } |
| 1163 | |
Rafael Espindola | 2d1b096 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 1164 | // We need this to handle |
| 1165 | // |
| 1166 | // typedef struct { |
| 1167 | // void *foo() { return 0; } |
| 1168 | // } A; |
| 1169 | // |
| 1170 | // When we see foo we don't know if after the typedef we will get 'A' or '*A' |
| 1171 | // for example. If 'A', foo will have external linkage. If we have '*A', |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1172 | // foo will have no linkage. Since we can't know until we get to the end |
| 1173 | // of the typedef, this function finds out if D might have non-external linkage. |
Rafael Espindola | 2d1b096 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 1174 | // Callers should verify at the end of the TU if it D has external linkage or |
| 1175 | // not. |
| 1176 | bool Sema::mightHaveNonExternalLinkage(const DeclaratorDecl *D) { |
| 1177 | const DeclContext *DC = D->getDeclContext(); |
| 1178 | while (!DC->isTranslationUnit()) { |
| 1179 | if (const RecordDecl *RD = dyn_cast<RecordDecl>(DC)){ |
| 1180 | if (!RD->hasNameForLinkage()) |
| 1181 | return true; |
| 1182 | } |
| 1183 | DC = DC->getParent(); |
| 1184 | } |
| 1185 | |
Rafael Espindola | 181e3ec | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 1186 | return !D->isExternallyVisible(); |
Rafael Espindola | 2d1b096 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 1187 | } |
| 1188 | |
Eli Friedman | 39bd371 | 2013-09-10 03:05:56 +0000 | [diff] [blame] | 1189 | // FIXME: This needs to be refactored; some other isInMainFile users want |
| 1190 | // these semantics. |
| 1191 | static bool isMainFileLoc(const Sema &S, SourceLocation Loc) { |
| 1192 | if (S.TUKind != TU_Complete) |
| 1193 | return false; |
| 1194 | return S.SourceMgr.isInMainFile(Loc); |
| 1195 | } |
| 1196 | |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 1197 | bool Sema::ShouldWarnIfUnusedFileScopedDecl(const DeclaratorDecl *D) const { |
| 1198 | assert(D); |
Argyrios Kyrtzidis | f6d1d43 | 2010-08-13 18:42:29 +0000 | [diff] [blame] | 1199 | |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 1200 | if (D->isInvalidDecl() || D->isUsed() || D->hasAttr<UnusedAttr>()) |
| 1201 | return false; |
| 1202 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1203 | // Ignore all entities declared within templates, and out-of-line definitions |
| 1204 | // of members of class templates. |
Chandler Carruth | ef9d09c | 2011-01-03 19:27:19 +0000 | [diff] [blame] | 1205 | if (D->getDeclContext()->isDependentContext() || |
| 1206 | D->getLexicalDeclContext()->isDependentContext()) |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 1207 | return false; |
| 1208 | |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 1209 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Argyrios Kyrtzidis | 06999f8 | 2010-08-15 10:17:33 +0000 | [diff] [blame] | 1210 | if (FD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation) |
| 1211 | return false; |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 1212 | |
Argyrios Kyrtzidis | 06999f8 | 2010-08-15 10:17:33 +0000 | [diff] [blame] | 1213 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) { |
| 1214 | if (MD->isVirtual() || IsDisallowedCopyOrAssign(MD)) |
| 1215 | return false; |
| 1216 | } else { |
Eli Friedman | 39bd371 | 2013-09-10 03:05:56 +0000 | [diff] [blame] | 1217 | // 'static inline' functions are defined in headers; don't warn. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1218 | if (FD->isInlined() && !isMainFileLoc(*this, FD->getLocation())) |
Argyrios Kyrtzidis | 06999f8 | 2010-08-15 10:17:33 +0000 | [diff] [blame] | 1219 | return false; |
| 1220 | } |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 1221 | |
Sean Hunt | 10620eb | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 1222 | if (FD->doesThisDeclarationHaveABody() && |
John McCall | 82b9659 | 2010-10-27 01:41:35 +0000 | [diff] [blame] | 1223 | Context.DeclMustBeEmitted(FD)) |
| 1224 | return false; |
John McCall | 82b9659 | 2010-10-27 01:41:35 +0000 | [diff] [blame] | 1225 | } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
Eli Friedman | 39bd371 | 2013-09-10 03:05:56 +0000 | [diff] [blame] | 1226 | // Constants and utility variables are defined in headers with internal |
| 1227 | // linkage; don't warn. (Unlike functions, there isn't a convenient marker |
| 1228 | // like "inline".) |
| 1229 | if (!isMainFileLoc(*this, VD->getLocation())) |
| 1230 | return false; |
| 1231 | |
Eli Friedman | 39bd371 | 2013-09-10 03:05:56 +0000 | [diff] [blame] | 1232 | if (Context.DeclMustBeEmitted(VD)) |
John McCall | 82b9659 | 2010-10-27 01:41:35 +0000 | [diff] [blame] | 1233 | return false; |
| 1234 | |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 1235 | if (VD->isStaticDataMember() && |
| 1236 | VD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation) |
| 1237 | return false; |
John McCall | 82b9659 | 2010-10-27 01:41:35 +0000 | [diff] [blame] | 1238 | } else { |
| 1239 | return false; |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 1240 | } |
| 1241 | |
John McCall | 82b9659 | 2010-10-27 01:41:35 +0000 | [diff] [blame] | 1242 | // Only warn for unused decls internal to the translation unit. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1243 | // FIXME: This seems like a bogus check; it suppresses -Wunused-function |
| 1244 | // for inline functions defined in the main source file, for instance. |
Rafael Espindola | 2d1b096 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 1245 | return mightHaveNonExternalLinkage(D); |
John McCall | 82b9659 | 2010-10-27 01:41:35 +0000 | [diff] [blame] | 1246 | } |
| 1247 | |
| 1248 | void Sema::MarkUnusedFileScopedDecl(const DeclaratorDecl *D) { |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 1249 | if (!D) |
| 1250 | return; |
| 1251 | |
| 1252 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Rafael Espindola | bc65091 | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 1253 | const FunctionDecl *First = FD->getFirstDecl(); |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 1254 | if (FD != First && ShouldWarnIfUnusedFileScopedDecl(First)) |
| 1255 | return; // First should already be in the vector. |
| 1256 | } |
| 1257 | |
| 1258 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
Rafael Espindola | bc65091 | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 1259 | const VarDecl *First = VD->getFirstDecl(); |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 1260 | if (VD != First && ShouldWarnIfUnusedFileScopedDecl(First)) |
| 1261 | return; // First should already be in the vector. |
| 1262 | } |
| 1263 | |
David Blaikie | 7f7c42b | 2012-05-26 05:35:39 +0000 | [diff] [blame] | 1264 | if (ShouldWarnIfUnusedFileScopedDecl(D)) |
| 1265 | UnusedFileScopedDecls.push_back(D); |
| 1266 | } |
Argyrios Kyrtzidis | 49b96d1 | 2010-08-13 18:42:17 +0000 | [diff] [blame] | 1267 | |
Anders Carlsson | 99a000e | 2009-11-07 07:18:14 +0000 | [diff] [blame] | 1268 | static bool ShouldDiagnoseUnusedDecl(const NamedDecl *D) { |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 1269 | if (D->isInvalidDecl()) |
| 1270 | return false; |
| 1271 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1272 | if (D->isReferenced() || D->isUsed() || D->hasAttr<UnusedAttr>() || |
| 1273 | D->hasAttr<ObjCPreciseLifetimeAttr>()) |
Anders Carlsson | f7613d5 | 2009-11-07 07:26:56 +0000 | [diff] [blame] | 1274 | return false; |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 1275 | |
Chris Lattner | 57ad378 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 1276 | if (isa<LabelDecl>(D)) |
| 1277 | return true; |
| 1278 | |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 1279 | // White-list anything that isn't a local variable. |
| 1280 | if (!isa<VarDecl>(D) || isa<ParmVarDecl>(D) || isa<ImplicitParamDecl>(D) || |
| 1281 | !D->getDeclContext()->isFunctionOrMethod()) |
| 1282 | return false; |
| 1283 | |
| 1284 | // Types of valid local variables should be complete, so this should succeed. |
Rafael Espindola | 1a5d355 | 2012-01-06 04:54:01 +0000 | [diff] [blame] | 1285 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
John McCall | aec5860 | 2010-03-31 02:47:45 +0000 | [diff] [blame] | 1286 | |
| 1287 | // White-list anything with an __attribute__((unused)) type. |
| 1288 | QualType Ty = VD->getType(); |
| 1289 | |
| 1290 | // Only look at the outermost level of typedef. |
Douglas Gregor | 2c8e81e | 2012-09-14 05:10:40 +0000 | [diff] [blame] | 1291 | if (const TypedefType *TT = Ty->getAs<TypedefType>()) { |
John McCall | aec5860 | 2010-03-31 02:47:45 +0000 | [diff] [blame] | 1292 | if (TT->getDecl()->hasAttr<UnusedAttr>()) |
| 1293 | return false; |
| 1294 | } |
| 1295 | |
Douglas Gregor | 5764f61 | 2010-05-08 23:05:03 +0000 | [diff] [blame] | 1296 | // If we failed to complete the type for some reason, or if the type is |
| 1297 | // dependent, don't diagnose the variable. |
| 1298 | if (Ty->isIncompleteType() || Ty->isDependentType()) |
Douglas Gregor | a6a292b | 2010-04-27 16:20:13 +0000 | [diff] [blame] | 1299 | return false; |
| 1300 | |
John McCall | aec5860 | 2010-03-31 02:47:45 +0000 | [diff] [blame] | 1301 | if (const TagType *TT = Ty->getAs<TagType>()) { |
| 1302 | const TagDecl *Tag = TT->getDecl(); |
| 1303 | if (Tag->hasAttr<UnusedAttr>()) |
| 1304 | return false; |
| 1305 | |
| 1306 | if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Tag)) { |
Lubos Lunak | 1d3ce65 | 2013-07-20 15:05:36 +0000 | [diff] [blame] | 1307 | if (!RD->hasTrivialDestructor() && !RD->hasAttr<WarnUnusedAttr>()) |
Anders Carlsson | f7613d5 | 2009-11-07 07:26:56 +0000 | [diff] [blame] | 1308 | return false; |
Rafael Espindola | 1a5d355 | 2012-01-06 04:54:01 +0000 | [diff] [blame] | 1309 | |
| 1310 | if (const Expr *Init = VD->getInit()) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1311 | if (const ExprWithCleanups *Cleanups = |
| 1312 | dyn_cast<ExprWithCleanups>(Init)) |
David Blaikie | 39e1776 | 2012-10-24 21:29:06 +0000 | [diff] [blame] | 1313 | Init = Cleanups->getSubExpr(); |
Rafael Espindola | 1a5d355 | 2012-01-06 04:54:01 +0000 | [diff] [blame] | 1314 | const CXXConstructExpr *Construct = |
| 1315 | dyn_cast<CXXConstructExpr>(Init); |
| 1316 | if (Construct && !Construct->isElidable()) { |
| 1317 | CXXConstructorDecl *CD = Construct->getConstructor(); |
Lubos Lunak | 1d3ce65 | 2013-07-20 15:05:36 +0000 | [diff] [blame] | 1318 | if (!CD->isTrivial() && !RD->hasAttr<WarnUnusedAttr>()) |
Rafael Espindola | 1a5d355 | 2012-01-06 04:54:01 +0000 | [diff] [blame] | 1319 | return false; |
| 1320 | } |
| 1321 | } |
Anders Carlsson | f7613d5 | 2009-11-07 07:26:56 +0000 | [diff] [blame] | 1322 | } |
| 1323 | } |
John McCall | aec5860 | 2010-03-31 02:47:45 +0000 | [diff] [blame] | 1324 | |
| 1325 | // TODO: __attribute__((unused)) templates? |
Anders Carlsson | f7613d5 | 2009-11-07 07:26:56 +0000 | [diff] [blame] | 1326 | } |
| 1327 | |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 1328 | return true; |
Anders Carlsson | 99a000e | 2009-11-07 07:18:14 +0000 | [diff] [blame] | 1329 | } |
| 1330 | |
Anna Zaks | d5612a2 | 2011-07-28 20:52:06 +0000 | [diff] [blame] | 1331 | static void GenerateFixForUnusedDecl(const NamedDecl *D, ASTContext &Ctx, |
| 1332 | FixItHint &Hint) { |
| 1333 | if (isa<LabelDecl>(D)) { |
| 1334 | SourceLocation AfterColon = Lexer::findLocationAfterToken(D->getLocEnd(), |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1335 | tok::colon, Ctx.getSourceManager(), Ctx.getLangOpts(), true); |
Anna Zaks | d5612a2 | 2011-07-28 20:52:06 +0000 | [diff] [blame] | 1336 | if (AfterColon.isInvalid()) |
| 1337 | return; |
| 1338 | Hint = FixItHint::CreateRemoval(CharSourceRange:: |
| 1339 | getCharRange(D->getLocStart(), AfterColon)); |
| 1340 | } |
| 1341 | return; |
| 1342 | } |
| 1343 | |
Chris Lattner | 337e550 | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1344 | /// DiagnoseUnusedDecl - Emit warnings about declarations that are not used |
| 1345 | /// unless they are marked attr(unused). |
Douglas Gregor | 5764f61 | 2010-05-08 23:05:03 +0000 | [diff] [blame] | 1346 | void Sema::DiagnoseUnusedDecl(const NamedDecl *D) { |
| 1347 | if (!ShouldDiagnoseUnusedDecl(D)) |
| 1348 | return; |
| 1349 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1350 | FixItHint Hint; |
Anna Zaks | d5612a2 | 2011-07-28 20:52:06 +0000 | [diff] [blame] | 1351 | GenerateFixForUnusedDecl(D, Context, Hint); |
| 1352 | |
Chris Lattner | 57ad378 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 1353 | unsigned DiagID; |
Douglas Gregor | 5764f61 | 2010-05-08 23:05:03 +0000 | [diff] [blame] | 1354 | if (isa<VarDecl>(D) && cast<VarDecl>(D)->isExceptionVariable()) |
Chris Lattner | 57ad378 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 1355 | DiagID = diag::warn_unused_exception_param; |
| 1356 | else if (isa<LabelDecl>(D)) |
| 1357 | DiagID = diag::warn_unused_label; |
Douglas Gregor | 5764f61 | 2010-05-08 23:05:03 +0000 | [diff] [blame] | 1358 | else |
Chris Lattner | 57ad378 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 1359 | DiagID = diag::warn_unused_variable; |
| 1360 | |
Anna Zaks | d5612a2 | 2011-07-28 20:52:06 +0000 | [diff] [blame] | 1361 | Diag(D->getLocation(), DiagID) << D->getDeclName() << Hint; |
Douglas Gregor | 5764f61 | 2010-05-08 23:05:03 +0000 | [diff] [blame] | 1362 | } |
| 1363 | |
Chris Lattner | 337e550 | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1364 | static void CheckPoppedLabel(LabelDecl *L, Sema &S) { |
| 1365 | // Verify that we have no forward references left. If so, there was a goto |
| 1366 | // or address of a label taken, but no definition of it. Label fwd |
| 1367 | // definitions are indicated with a null substmt. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1368 | if (L->getStmt() == nullptr) |
Chris Lattner | 337e550 | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1369 | S.Diag(L->getLocation(), diag::err_undeclared_label_use) <<L->getDeclName(); |
| 1370 | } |
| 1371 | |
Steve Naroff | b216c88 | 2007-10-09 22:01:59 +0000 | [diff] [blame] | 1372 | void Sema::ActOnPopScope(SourceLocation Loc, Scope *S) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1373 | S->mergeNRVOIntoParent(); |
| 1374 | |
Chris Lattner | 31e0572 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 1375 | if (S->decl_empty()) return; |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 1376 | assert((S->getFlags() & (Scope::DeclScope | Scope::TemplateParamScope)) && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1377 | "Scope shouldn't contain decls!"); |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 1378 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1379 | for (auto *TmpD : S->decls()) { |
Steve Naroff | c752d04 | 2007-09-13 18:10:37 +0000 | [diff] [blame] | 1380 | assert(TmpD && "This decl didn't get pushed??"); |
Argyrios Kyrtzidis | 7643536 | 2008-06-10 01:32:09 +0000 | [diff] [blame] | 1381 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1382 | assert(isa<NamedDecl>(TmpD) && "Decl isn't NamedDecl?"); |
| 1383 | NamedDecl *D = cast<NamedDecl>(TmpD); |
Argyrios Kyrtzidis | 7643536 | 2008-06-10 01:32:09 +0000 | [diff] [blame] | 1384 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1385 | if (!D->getDeclName()) continue; |
Chris Lattner | 7f925cc | 2008-04-11 07:00:53 +0000 | [diff] [blame] | 1386 | |
Douglas Gregor | b5352cf | 2009-10-08 21:35:42 +0000 | [diff] [blame] | 1387 | // Diagnose unused variables in this scope. |
Matt Beaumont-Gay | 59d8ccb | 2013-03-28 21:46:45 +0000 | [diff] [blame] | 1388 | if (!S->hasUnrecoverableErrorOccurred()) |
Douglas Gregor | 5764f61 | 2010-05-08 23:05:03 +0000 | [diff] [blame] | 1389 | DiagnoseUnusedDecl(D); |
| 1390 | |
Chris Lattner | 337e550 | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1391 | // If this was a forward reference to a label, verify it was defined. |
| 1392 | if (LabelDecl *LD = dyn_cast<LabelDecl>(D)) |
| 1393 | CheckPoppedLabel(LD, *this); |
| 1394 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1395 | // Remove this name from our lexical scope. |
| 1396 | IdResolver.RemoveDecl(D); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1397 | } |
James Molloy | 16f1f71 | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 1398 | } |
| 1399 | |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 1400 | /// \brief Look for an Objective-C class in the translation unit. |
| 1401 | /// |
| 1402 | /// \param Id The name of the Objective-C class we're looking for. If |
| 1403 | /// typo-correction fixes this name, the Id will be updated |
| 1404 | /// to the fixed name. |
| 1405 | /// |
| 1406 | /// \param IdLoc The location of the name in the translation unit. |
| 1407 | /// |
James Dennett | 16ae9de | 2012-06-22 10:16:05 +0000 | [diff] [blame] | 1408 | /// \param DoTypoCorrection If true, this routine will attempt typo correction |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 1409 | /// if there is no class with the given name. |
| 1410 | /// |
| 1411 | /// \returns The declaration of the named Objective-C class, or NULL if the |
| 1412 | /// class could not be found. |
| 1413 | ObjCInterfaceDecl *Sema::getObjCInterfaceDecl(IdentifierInfo *&Id, |
| 1414 | SourceLocation IdLoc, |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1415 | bool DoTypoCorrection) { |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 1416 | // The third "scope" argument is 0 since we aren't enabling lazy built-in |
| 1417 | // creation from this context. |
| 1418 | NamedDecl *IDecl = LookupSingleName(TUScope, Id, IdLoc, LookupOrdinaryName); |
| 1419 | |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1420 | if (!IDecl && DoTypoCorrection) { |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 1421 | // Perform typo correction at the given location, but only if we |
| 1422 | // find an Objective-C class name. |
Kaelyn Uhrain | 43e875d | 2012-01-18 21:41:41 +0000 | [diff] [blame] | 1423 | DeclFilterCCC<ObjCInterfaceDecl> Validator; |
| 1424 | if (TypoCorrection C = CorrectTypo(DeclarationNameInfo(Id, IdLoc), |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1425 | LookupOrdinaryName, TUScope, nullptr, |
| 1426 | Validator, CTK_ErrorRecovery)) { |
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 1427 | diagnoseTypo(C, PDiag(diag::err_undef_interface_suggest) << Id); |
Kaelyn Uhrain | 43e875d | 2012-01-18 21:41:41 +0000 | [diff] [blame] | 1428 | IDecl = C.getCorrectionDeclAs<ObjCInterfaceDecl>(); |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 1429 | Id = IDecl->getIdentifier(); |
| 1430 | } |
| 1431 | } |
Fariborz Jahanian | 3306f96 | 2012-01-12 00:18:35 +0000 | [diff] [blame] | 1432 | ObjCInterfaceDecl *Def = dyn_cast_or_null<ObjCInterfaceDecl>(IDecl); |
| 1433 | // This routine must always return a class definition, if any. |
| 1434 | if (Def && Def->getDefinition()) |
| 1435 | Def = Def->getDefinition(); |
| 1436 | return Def; |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 1437 | } |
| 1438 | |
Douglas Gregor | 1a0d31a | 2009-01-12 18:45:55 +0000 | [diff] [blame] | 1439 | /// getNonFieldDeclScope - Retrieves the innermost scope, starting |
| 1440 | /// from S, where a non-field would be declared. This routine copes |
| 1441 | /// with the difference between C and C++ scoping rules in structs and |
| 1442 | /// unions. For example, the following code is well-formed in C but |
| 1443 | /// ill-formed in C++: |
| 1444 | /// @code |
| 1445 | /// struct S6 { |
| 1446 | /// enum { BAR } e; |
| 1447 | /// }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1448 | /// |
Douglas Gregor | 1a0d31a | 2009-01-12 18:45:55 +0000 | [diff] [blame] | 1449 | /// void test_S6() { |
| 1450 | /// struct S6 a; |
| 1451 | /// a.e = BAR; |
| 1452 | /// } |
| 1453 | /// @endcode |
| 1454 | /// For the declaration of BAR, this routine will return a different |
| 1455 | /// scope. The scope S will be the scope of the unnamed enumeration |
| 1456 | /// within S6. In C++, this routine will return the scope associated |
| 1457 | /// with S6, because the enumeration's scope is a transparent |
| 1458 | /// context but structures can contain non-field names. In C, this |
| 1459 | /// routine will return the translation unit scope, since the |
| 1460 | /// enumeration's scope is a transparent context and structures cannot |
| 1461 | /// contain non-field names. |
| 1462 | Scope *Sema::getNonFieldDeclScope(Scope *S) { |
| 1463 | while (((S->getFlags() & Scope::DeclScope) == 0) || |
Ted Kremenek | f0d5861 | 2013-10-08 17:08:03 +0000 | [diff] [blame] | 1464 | (S->getEntity() && S->getEntity()->isTransparentContext()) || |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1465 | (S->isClassScope() && !getLangOpts().CPlusPlus)) |
Douglas Gregor | 1a0d31a | 2009-01-12 18:45:55 +0000 | [diff] [blame] | 1466 | S = S->getParent(); |
| 1467 | return S; |
| 1468 | } |
| 1469 | |
Fariborz Jahanian | f799213 | 2013-01-04 18:45:40 +0000 | [diff] [blame] | 1470 | /// \brief Looks up the declaration of "struct objc_super" and |
| 1471 | /// saves it for later use in building builtin declaration of |
| 1472 | /// objc_msgSendSuper and objc_msgSendSuper_stret. If no such |
| 1473 | /// pre-existing declaration exists no action takes place. |
| 1474 | static void LookupPredefedObjCSuperType(Sema &ThisSema, Scope *S, |
| 1475 | IdentifierInfo *II) { |
| 1476 | if (!II->isStr("objc_msgSendSuper")) |
| 1477 | return; |
| 1478 | ASTContext &Context = ThisSema.Context; |
| 1479 | |
| 1480 | LookupResult Result(ThisSema, &Context.Idents.get("objc_super"), |
| 1481 | SourceLocation(), Sema::LookupTagName); |
| 1482 | ThisSema.LookupName(Result, S); |
| 1483 | if (Result.getResultKind() == LookupResult::Found) |
| 1484 | if (const TagDecl *TD = Result.getAsSingle<TagDecl>()) |
| 1485 | Context.setObjCSuperType(Context.getTagDeclType(TD)); |
| 1486 | } |
| 1487 | |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 1488 | /// LazilyCreateBuiltin - The specified Builtin-ID was first used at |
| 1489 | /// file scope. lazily create a decl for it. ForRedeclaration is true |
| 1490 | /// if we're creating this built-in in anticipation of redeclaring the |
| 1491 | /// built-in. |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1492 | NamedDecl *Sema::LazilyCreateBuiltin(IdentifierInfo *II, unsigned bid, |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 1493 | Scope *S, bool ForRedeclaration, |
| 1494 | SourceLocation Loc) { |
Fariborz Jahanian | f799213 | 2013-01-04 18:45:40 +0000 | [diff] [blame] | 1495 | LookupPredefedObjCSuperType(*this, S, II); |
| 1496 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1497 | Builtin::ID BID = (Builtin::ID)bid; |
| 1498 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 1499 | ASTContext::GetBuiltinTypeError Error; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1500 | QualType R = Context.GetBuiltinType(BID, Error); |
Douglas Gregor | 370ab3f | 2009-02-14 01:52:53 +0000 | [diff] [blame] | 1501 | switch (Error) { |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 1502 | case ASTContext::GE_None: |
Douglas Gregor | 370ab3f | 2009-02-14 01:52:53 +0000 | [diff] [blame] | 1503 | // Okay |
| 1504 | break; |
| 1505 | |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 1506 | case ASTContext::GE_Missing_stdio: |
Douglas Gregor | 370ab3f | 2009-02-14 01:52:53 +0000 | [diff] [blame] | 1507 | if (ForRedeclaration) |
Douglas Gregor | 6b9109e | 2011-01-03 09:37:44 +0000 | [diff] [blame] | 1508 | Diag(Loc, diag::warn_implicit_decl_requires_stdio) |
Douglas Gregor | 370ab3f | 2009-02-14 01:52:53 +0000 | [diff] [blame] | 1509 | << Context.BuiltinInfo.GetName(BID); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1510 | return nullptr; |
Mike Stump | 782fa30 | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 1511 | |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 1512 | case ASTContext::GE_Missing_setjmp: |
Mike Stump | 782fa30 | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 1513 | if (ForRedeclaration) |
Douglas Gregor | 6b9109e | 2011-01-03 09:37:44 +0000 | [diff] [blame] | 1514 | Diag(Loc, diag::warn_implicit_decl_requires_setjmp) |
Mike Stump | 782fa30 | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 1515 | << Context.BuiltinInfo.GetName(BID); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1516 | return nullptr; |
Rafael Espindola | e2d4f4e | 2011-11-13 21:51:09 +0000 | [diff] [blame] | 1517 | |
| 1518 | case ASTContext::GE_Missing_ucontext: |
| 1519 | if (ForRedeclaration) |
| 1520 | Diag(Loc, diag::warn_implicit_decl_requires_ucontext) |
| 1521 | << Context.BuiltinInfo.GetName(BID); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1522 | return nullptr; |
Douglas Gregor | 370ab3f | 2009-02-14 01:52:53 +0000 | [diff] [blame] | 1523 | } |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 1524 | |
| 1525 | if (!ForRedeclaration && Context.BuiltinInfo.isPredefinedLibFunction(BID)) { |
| 1526 | Diag(Loc, diag::ext_implicit_lib_function_decl) |
| 1527 | << Context.BuiltinInfo.GetName(BID) |
| 1528 | << R; |
Douglas Gregor | b1152d8 | 2009-02-16 21:58:21 +0000 | [diff] [blame] | 1529 | if (Context.BuiltinInfo.getHeaderName(BID) && |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 1530 | Diags.getDiagnosticLevel(diag::ext_implicit_lib_function_decl, Loc) |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 1531 | != DiagnosticsEngine::Ignored) |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 1532 | Diag(Loc, diag::note_please_include_header) |
| 1533 | << Context.BuiltinInfo.getHeaderName(BID) |
| 1534 | << Context.BuiltinInfo.GetName(BID); |
| 1535 | } |
| 1536 | |
Warren Hunt | 2d023ec | 2013-11-01 23:46:51 +0000 | [diff] [blame] | 1537 | DeclContext *Parent = Context.getTranslationUnitDecl(); |
| 1538 | if (getLangOpts().CPlusPlus) { |
| 1539 | LinkageSpecDecl *CLinkageDecl = |
| 1540 | LinkageSpecDecl::Create(Context, Parent, Loc, Loc, |
| 1541 | LinkageSpecDecl::lang_c, false); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1542 | CLinkageDecl->setImplicit(); |
Warren Hunt | 2d023ec | 2013-11-01 23:46:51 +0000 | [diff] [blame] | 1543 | Parent->addDecl(CLinkageDecl); |
| 1544 | Parent = CLinkageDecl; |
| 1545 | } |
| 1546 | |
Argyrios Kyrtzidis | ff898cd | 2008-04-17 14:47:13 +0000 | [diff] [blame] | 1547 | FunctionDecl *New = FunctionDecl::Create(Context, |
Warren Hunt | 2d023ec | 2013-11-01 23:46:51 +0000 | [diff] [blame] | 1548 | Parent, |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1549 | Loc, Loc, II, R, /*TInfo=*/nullptr, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 1550 | SC_Extern, |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 1551 | false, |
Douglas Gregor | 2224f84 | 2009-02-25 16:33:18 +0000 | [diff] [blame] | 1552 | /*hasPrototype=*/true); |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 1553 | New->setImplicit(); |
| 1554 | |
Chris Lattner | 95e2c71 | 2008-05-05 22:18:14 +0000 | [diff] [blame] | 1555 | // Create Decl objects for each parameter, adding them to the |
| 1556 | // FunctionDecl. |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1557 | if (const FunctionProtoType *FT = dyn_cast<FunctionProtoType>(R)) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1558 | SmallVector<ParmVarDecl*, 16> Params; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1559 | for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) { |
John McCall | fb44de9 | 2011-05-01 22:35:37 +0000 | [diff] [blame] | 1560 | ParmVarDecl *parm = |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1561 | ParmVarDecl::Create(Context, New, SourceLocation(), SourceLocation(), |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1562 | nullptr, FT->getParamType(i), /*TInfo=*/nullptr, |
| 1563 | SC_None, nullptr); |
John McCall | fb44de9 | 2011-05-01 22:35:37 +0000 | [diff] [blame] | 1564 | parm->setScopeInfo(0, i); |
| 1565 | Params.push_back(parm); |
| 1566 | } |
David Blaikie | 4278c65 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 1567 | New->setParams(Params); |
Chris Lattner | 95e2c71 | 2008-05-05 22:18:14 +0000 | [diff] [blame] | 1568 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1569 | |
| 1570 | AddKnownFunctionAttributes(New); |
Warren Hunt | 2d023ec | 2013-11-01 23:46:51 +0000 | [diff] [blame] | 1571 | RegisterLocallyScopedExternCDecl(New, S); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1572 | |
Chris Lattner | 7f925cc | 2008-04-11 07:00:53 +0000 | [diff] [blame] | 1573 | // TUScope is the translation-unit scope to insert this function into. |
Douglas Gregor | a8cc8ce | 2009-01-09 18:51:29 +0000 | [diff] [blame] | 1574 | // FIXME: This is hideous. We need to teach PushOnScopeChains to |
| 1575 | // relate Scopes to DeclContexts, and probably eliminate CurContext |
| 1576 | // entirely, but we're not there yet. |
| 1577 | DeclContext *SavedContext = CurContext; |
Warren Hunt | 2d023ec | 2013-11-01 23:46:51 +0000 | [diff] [blame] | 1578 | CurContext = Parent; |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 1579 | PushOnScopeChains(New, TUScope); |
Douglas Gregor | a8cc8ce | 2009-01-09 18:51:29 +0000 | [diff] [blame] | 1580 | CurContext = SavedContext; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1581 | return New; |
| 1582 | } |
| 1583 | |
Douglas Gregor | 7dc80e1 | 2013-01-09 00:47:56 +0000 | [diff] [blame] | 1584 | /// \brief Filter out any previous declarations that the given declaration |
| 1585 | /// should not consider because they are not permitted to conflict, e.g., |
| 1586 | /// because they come from hidden sub-modules and do not refer to the same |
| 1587 | /// entity. |
| 1588 | static void filterNonConflictingPreviousDecls(ASTContext &context, |
| 1589 | NamedDecl *decl, |
| 1590 | LookupResult &previous){ |
| 1591 | // This is only interesting when modules are enabled. |
| 1592 | if (!context.getLangOpts().Modules) |
| 1593 | return; |
| 1594 | |
| 1595 | // Empty sets are uninteresting. |
| 1596 | if (previous.empty()) |
| 1597 | return; |
| 1598 | |
Douglas Gregor | 7dc80e1 | 2013-01-09 00:47:56 +0000 | [diff] [blame] | 1599 | LookupResult::Filter filter = previous.makeFilter(); |
| 1600 | while (filter.hasNext()) { |
| 1601 | NamedDecl *old = filter.next(); |
| 1602 | |
| 1603 | // Non-hidden declarations are never ignored. |
| 1604 | if (!old->isHidden()) |
| 1605 | continue; |
| 1606 | |
Rafael Espindola | 181e3ec | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 1607 | if (!old->isExternallyVisible()) |
Douglas Gregor | 7dc80e1 | 2013-01-09 00:47:56 +0000 | [diff] [blame] | 1608 | filter.erase(); |
| 1609 | } |
| 1610 | |
| 1611 | filter.done(); |
| 1612 | } |
| 1613 | |
Rafael Espindola | 5df37bd | 2011-12-26 22:42:47 +0000 | [diff] [blame] | 1614 | bool Sema::isIncompatibleTypedef(TypeDecl *Old, TypedefNameDecl *New) { |
| 1615 | QualType OldType; |
| 1616 | if (TypedefNameDecl *OldTypedef = dyn_cast<TypedefNameDecl>(Old)) |
| 1617 | OldType = OldTypedef->getUnderlyingType(); |
| 1618 | else |
| 1619 | OldType = Context.getTypeDeclType(Old); |
| 1620 | QualType NewType = New->getUnderlyingType(); |
| 1621 | |
Douglas Gregor | ec3bd72 | 2012-01-11 22:33:48 +0000 | [diff] [blame] | 1622 | if (NewType->isVariablyModifiedType()) { |
| 1623 | // Must not redefine a typedef with a variably-modified type. |
| 1624 | int Kind = isa<TypeAliasDecl>(Old) ? 1 : 0; |
| 1625 | Diag(New->getLocation(), diag::err_redefinition_variably_modified_typedef) |
| 1626 | << Kind << NewType; |
| 1627 | if (Old->getLocation().isValid()) |
| 1628 | Diag(Old->getLocation(), diag::note_previous_definition); |
| 1629 | New->setInvalidDecl(); |
| 1630 | return true; |
| 1631 | } |
| 1632 | |
Rafael Espindola | 5df37bd | 2011-12-26 22:42:47 +0000 | [diff] [blame] | 1633 | if (OldType != NewType && |
| 1634 | !OldType->isDependentType() && |
| 1635 | !NewType->isDependentType() && |
Douglas Gregor | ec3bd72 | 2012-01-11 22:33:48 +0000 | [diff] [blame] | 1636 | !Context.hasSameType(OldType, NewType)) { |
Rafael Espindola | 5df37bd | 2011-12-26 22:42:47 +0000 | [diff] [blame] | 1637 | int Kind = isa<TypeAliasDecl>(Old) ? 1 : 0; |
| 1638 | Diag(New->getLocation(), diag::err_redefinition_different_typedef) |
| 1639 | << Kind << NewType << OldType; |
| 1640 | if (Old->getLocation().isValid()) |
| 1641 | Diag(Old->getLocation(), diag::note_previous_definition); |
| 1642 | New->setInvalidDecl(); |
| 1643 | return true; |
| 1644 | } |
| 1645 | return false; |
| 1646 | } |
| 1647 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1648 | /// MergeTypedefNameDecl - We just parsed a typedef 'New' which has the |
Douglas Gregor | cda9c67 | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 1649 | /// same name and scope as a previous declaration 'Old'. Figure out |
| 1650 | /// how to resolve this situation, merging decls or emitting |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 1651 | /// diagnostics as appropriate. If there was an error, set New to be invalid. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1652 | /// |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1653 | void Sema::MergeTypedefNameDecl(TypedefNameDecl *New, LookupResult &OldDecls) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1654 | // If the new decl is known invalid already, don't bother doing any |
| 1655 | // merging checks. |
| 1656 | if (New->isInvalidDecl()) return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1657 | |
Steve Naroff | 2b255c4 | 2008-09-09 14:32:20 +0000 | [diff] [blame] | 1658 | // Allow multiple definitions for ObjC built-in typedefs. |
| 1659 | // FIXME: Verify the underlying types are equivalent! |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1660 | if (getLangOpts().ObjC1) { |
Chris Lattner | 2bac0f6 | 2008-11-20 05:41:43 +0000 | [diff] [blame] | 1661 | const IdentifierInfo *TypeID = New->getIdentifier(); |
| 1662 | switch (TypeID->getLength()) { |
| 1663 | default: break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1664 | case 2: |
Fariborz Jahanian | 0cd00be | 2012-05-14 22:48:56 +0000 | [diff] [blame] | 1665 | { |
| 1666 | if (!TypeID->isStr("id")) |
| 1667 | break; |
| 1668 | QualType T = New->getUnderlyingType(); |
| 1669 | if (!T->isPointerType()) |
| 1670 | break; |
| 1671 | if (!T->isVoidPointerType()) { |
| 1672 | QualType PT = T->getAs<PointerType>()->getPointeeType(); |
| 1673 | if (!PT->isStructureType()) |
| 1674 | break; |
| 1675 | } |
| 1676 | Context.setObjCIdRedefinitionType(T); |
| 1677 | // Install the built-in type for 'id', ignoring the current definition. |
| 1678 | New->setTypeForDecl(Context.getObjCIdType().getTypePtr()); |
| 1679 | return; |
| 1680 | } |
Chris Lattner | 2bac0f6 | 2008-11-20 05:41:43 +0000 | [diff] [blame] | 1681 | case 5: |
| 1682 | if (!TypeID->isStr("Class")) |
| 1683 | break; |
Douglas Gregor | 01a4cf1 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 1684 | Context.setObjCClassRedefinitionType(New->getUnderlyingType()); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1685 | // Install the built-in type for 'Class', ignoring the current definition. |
| 1686 | New->setTypeForDecl(Context.getObjCClassType().getTypePtr()); |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 1687 | return; |
Chris Lattner | 2bac0f6 | 2008-11-20 05:41:43 +0000 | [diff] [blame] | 1688 | case 3: |
| 1689 | if (!TypeID->isStr("SEL")) |
| 1690 | break; |
Douglas Gregor | 01a4cf1 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 1691 | Context.setObjCSelRedefinitionType(New->getUnderlyingType()); |
Fariborz Jahanian | 13dcd00 | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 1692 | // Install the built-in type for 'SEL', ignoring the current definition. |
| 1693 | New->setTypeForDecl(Context.getObjCSelType().getTypePtr()); |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 1694 | return; |
Steve Naroff | 2b255c4 | 2008-09-09 14:32:20 +0000 | [diff] [blame] | 1695 | } |
| 1696 | // Fall through - the typedef name was not a builtin type. |
| 1697 | } |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1698 | |
Douglas Gregor | 6697312 | 2009-01-28 17:15:10 +0000 | [diff] [blame] | 1699 | // Verify the old decl was also a type. |
John McCall | 5126fd0 | 2009-12-30 00:31:22 +0000 | [diff] [blame] | 1700 | TypeDecl *Old = OldDecls.getAsSingle<TypeDecl>(); |
| 1701 | if (!Old) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1702 | Diag(New->getLocation(), diag::err_redefinition_different_kind) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 1703 | << New->getDeclName(); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1704 | |
| 1705 | NamedDecl *OldD = OldDecls.getRepresentativeDecl(); |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 1706 | if (OldD->getLocation().isValid()) |
Fariborz Jahanian | c55a240 | 2009-01-16 19:58:32 +0000 | [diff] [blame] | 1707 | Diag(OldD->getLocation(), diag::note_previous_definition); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1708 | |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 1709 | return New->setInvalidDecl(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1710 | } |
Douglas Gregor | 6697312 | 2009-01-28 17:15:10 +0000 | [diff] [blame] | 1711 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1712 | // If the old declaration is invalid, just give up here. |
| 1713 | if (Old->isInvalidDecl()) |
| 1714 | return New->setInvalidDecl(); |
| 1715 | |
Chris Lattner | 99cb997 | 2008-07-25 18:44:27 +0000 | [diff] [blame] | 1716 | // If the typedef types are not identical, reject them in all languages and |
| 1717 | // with any extensions enabled. |
Rafael Espindola | 5df37bd | 2011-12-26 22:42:47 +0000 | [diff] [blame] | 1718 | if (isIncompatibleTypedef(Old, New)) |
| 1719 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1720 | |
Justin Bogner | 2dd68de | 2013-10-08 00:19:09 +0000 | [diff] [blame] | 1721 | // The types match. Link up the redeclaration chain and merge attributes if |
| 1722 | // the old declaration was a typedef. |
| 1723 | if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Old)) { |
Rafael Espindola | bc65091 | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 1724 | New->setPreviousDecl(Typedef); |
Justin Bogner | 2dd68de | 2013-10-08 00:19:09 +0000 | [diff] [blame] | 1725 | mergeDeclAttributes(New, Old); |
| 1726 | } |
Eli Friedman | 9ec4099 | 2013-07-16 02:07:49 +0000 | [diff] [blame] | 1727 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1728 | if (getLangOpts().MicrosoftExt) |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 1729 | return; |
Eli Friedman | 54ecfce | 2008-06-11 06:20:39 +0000 | [diff] [blame] | 1730 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1731 | if (getLangOpts().CPlusPlus) { |
Douglas Gregor | 93dda72 | 2010-01-11 21:54:40 +0000 | [diff] [blame] | 1732 | // C++ [dcl.typedef]p2: |
| 1733 | // In a given non-class scope, a typedef specifier can be used to |
| 1734 | // redefine the name of any type declared in that scope to refer |
| 1735 | // to the type to which it already refers. |
Chris Lattner | 32b0675 | 2009-04-17 22:04:20 +0000 | [diff] [blame] | 1736 | if (!isa<CXXRecordDecl>(CurContext)) |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 1737 | return; |
Douglas Gregor | 93dda72 | 2010-01-11 21:54:40 +0000 | [diff] [blame] | 1738 | |
| 1739 | // C++0x [dcl.typedef]p4: |
| 1740 | // In a given class scope, a typedef specifier can be used to redefine |
| 1741 | // any class-name declared in that scope that is not also a typedef-name |
| 1742 | // to refer to the type to which it already refers. |
| 1743 | // |
| 1744 | // This wording came in via DR424, which was a correction to the |
| 1745 | // wording in DR56, which accidentally banned code like: |
| 1746 | // |
| 1747 | // struct S { |
| 1748 | // typedef struct A { } A; |
| 1749 | // }; |
| 1750 | // |
| 1751 | // in the C++03 standard. We implement the C++0x semantics, which |
| 1752 | // allow the above but disallow |
| 1753 | // |
| 1754 | // struct S { |
| 1755 | // typedef int I; |
| 1756 | // typedef int I; |
| 1757 | // }; |
| 1758 | // |
| 1759 | // since that was the intent of DR56. |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1760 | if (!isa<TypedefNameDecl>(Old)) |
Douglas Gregor | 93dda72 | 2010-01-11 21:54:40 +0000 | [diff] [blame] | 1761 | return; |
| 1762 | |
Chris Lattner | 32b0675 | 2009-04-17 22:04:20 +0000 | [diff] [blame] | 1763 | Diag(New->getLocation(), diag::err_redefinition) |
| 1764 | << New->getDeclName(); |
| 1765 | Diag(Old->getLocation(), diag::note_previous_definition); |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 1766 | return New->setInvalidDecl(); |
Daniel Dunbar | 2fe0997 | 2008-09-12 18:10:20 +0000 | [diff] [blame] | 1767 | } |
Eli Friedman | 54ecfce | 2008-06-11 06:20:39 +0000 | [diff] [blame] | 1768 | |
Douglas Gregor | c0004df | 2012-01-11 04:25:01 +0000 | [diff] [blame] | 1769 | // Modules always permit redefinition of typedefs, as does C11. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1770 | if (getLangOpts().Modules || getLangOpts().C11) |
Douglas Gregor | c02d62f | 2012-01-09 15:36:04 +0000 | [diff] [blame] | 1771 | return; |
| 1772 | |
Chris Lattner | 32b0675 | 2009-04-17 22:04:20 +0000 | [diff] [blame] | 1773 | // If we have a redefinition of a typedef in C, emit a warning. This warning |
| 1774 | // is normally mapped to an error, but can be controlled with |
Eli Friedman | 340a4e5 | 2009-06-04 23:03:07 +0000 | [diff] [blame] | 1775 | // -Wtypedef-redefinition. If either the original or the redefinition is |
| 1776 | // in a system header, don't emit this for compatibility with GCC. |
Chris Lattner | 6d97e5e | 2010-03-01 20:59:53 +0000 | [diff] [blame] | 1777 | if (getDiagnostics().getSuppressSystemWarnings() && |
Eli Friedman | 340a4e5 | 2009-06-04 23:03:07 +0000 | [diff] [blame] | 1778 | (Context.getSourceManager().isInSystemHeader(Old->getLocation()) || |
| 1779 | Context.getSourceManager().isInSystemHeader(New->getLocation()))) |
Chris Lattner | d0359af | 2009-04-27 01:46:12 +0000 | [diff] [blame] | 1780 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1781 | |
Chris Lattner | 32b0675 | 2009-04-17 22:04:20 +0000 | [diff] [blame] | 1782 | Diag(New->getLocation(), diag::warn_redefinition_of_typedef) |
| 1783 | << New->getDeclName(); |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 1784 | Diag(Old->getLocation(), diag::note_previous_definition); |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 1785 | return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1786 | } |
| 1787 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1788 | /// DeclhasAttr - returns true if decl Declaration already has the target |
| 1789 | /// attribute. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1790 | static bool DeclHasAttr(const Decl *D, const Attr *A) { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1791 | const OwnershipAttr *OA = dyn_cast<OwnershipAttr>(A); |
Julien Lerouge | 77f68bb | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 1792 | const AnnotateAttr *Ann = dyn_cast<AnnotateAttr>(A); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1793 | for (const auto *i : D->attrs()) |
| 1794 | if (i->getKind() == A->getKind()) { |
Julien Lerouge | 77f68bb | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 1795 | if (Ann) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1796 | if (Ann->getAnnotation() == cast<AnnotateAttr>(i)->getAnnotation()) |
Julien Lerouge | 77f68bb | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 1797 | return true; |
| 1798 | continue; |
| 1799 | } |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1800 | // FIXME: Don't hardcode this check |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1801 | if (OA && isa<OwnershipAttr>(i)) |
| 1802 | return OA->getOwnKind() == cast<OwnershipAttr>(i)->getOwnKind(); |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 1803 | return true; |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1804 | } |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 1805 | |
| 1806 | return false; |
| 1807 | } |
| 1808 | |
Richard Smith | 671b321 | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 1809 | static bool isAttributeTargetADefinition(Decl *D) { |
| 1810 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) |
| 1811 | return VD->isThisDeclarationADefinition(); |
| 1812 | if (TagDecl *TD = dyn_cast<TagDecl>(D)) |
| 1813 | return TD->isCompleteDefinition() || TD->isBeingDefined(); |
| 1814 | return true; |
| 1815 | } |
| 1816 | |
| 1817 | /// Merge alignment attributes from \p Old to \p New, taking into account the |
| 1818 | /// special semantics of C11's _Alignas specifier and C++11's alignas attribute. |
| 1819 | /// |
| 1820 | /// \return \c true if any attributes were added to \p New. |
| 1821 | static bool mergeAlignedAttrs(Sema &S, NamedDecl *New, Decl *Old) { |
| 1822 | // Look for alignas attributes on Old, and pick out whichever attribute |
| 1823 | // specifies the strictest alignment requirement. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1824 | AlignedAttr *OldAlignasAttr = nullptr; |
| 1825 | AlignedAttr *OldStrictestAlignAttr = nullptr; |
Richard Smith | 671b321 | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 1826 | unsigned OldAlign = 0; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1827 | for (auto *I : Old->specific_attrs<AlignedAttr>()) { |
Richard Smith | 671b321 | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 1828 | // FIXME: We have no way of representing inherited dependent alignments |
| 1829 | // in a case like: |
| 1830 | // template<int A, int B> struct alignas(A) X; |
| 1831 | // template<int A, int B> struct alignas(B) X {}; |
| 1832 | // For now, we just ignore any alignas attributes which are not on the |
| 1833 | // definition in such a case. |
| 1834 | if (I->isAlignmentDependent()) |
| 1835 | return false; |
| 1836 | |
| 1837 | if (I->isAlignas()) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1838 | OldAlignasAttr = I; |
Richard Smith | 671b321 | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 1839 | |
| 1840 | unsigned Align = I->getAlignment(S.Context); |
| 1841 | if (Align > OldAlign) { |
| 1842 | OldAlign = Align; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1843 | OldStrictestAlignAttr = I; |
Richard Smith | 671b321 | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 1844 | } |
| 1845 | } |
| 1846 | |
| 1847 | // Look for alignas attributes on New. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1848 | AlignedAttr *NewAlignasAttr = nullptr; |
Richard Smith | 671b321 | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 1849 | unsigned NewAlign = 0; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1850 | for (auto *I : New->specific_attrs<AlignedAttr>()) { |
Richard Smith | 671b321 | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 1851 | if (I->isAlignmentDependent()) |
| 1852 | return false; |
| 1853 | |
| 1854 | if (I->isAlignas()) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1855 | NewAlignasAttr = I; |
Richard Smith | 671b321 | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 1856 | |
| 1857 | unsigned Align = I->getAlignment(S.Context); |
| 1858 | if (Align > NewAlign) |
| 1859 | NewAlign = Align; |
| 1860 | } |
| 1861 | |
| 1862 | if (OldAlignasAttr && NewAlignasAttr && OldAlign != NewAlign) { |
| 1863 | // Both declarations have 'alignas' attributes. We require them to match. |
| 1864 | // C++11 [dcl.align]p6 and C11 6.7.5/7 both come close to saying this, but |
| 1865 | // fall short. (If two declarations both have alignas, they must both match |
| 1866 | // every definition, and so must match each other if there is a definition.) |
| 1867 | |
| 1868 | // If either declaration only contains 'alignas(0)' specifiers, then it |
| 1869 | // specifies the natural alignment for the type. |
| 1870 | if (OldAlign == 0 || NewAlign == 0) { |
| 1871 | QualType Ty; |
| 1872 | if (ValueDecl *VD = dyn_cast<ValueDecl>(New)) |
| 1873 | Ty = VD->getType(); |
| 1874 | else |
| 1875 | Ty = S.Context.getTagDeclType(cast<TagDecl>(New)); |
| 1876 | |
| 1877 | if (OldAlign == 0) |
| 1878 | OldAlign = S.Context.getTypeAlign(Ty); |
| 1879 | if (NewAlign == 0) |
| 1880 | NewAlign = S.Context.getTypeAlign(Ty); |
| 1881 | } |
| 1882 | |
| 1883 | if (OldAlign != NewAlign) { |
| 1884 | S.Diag(NewAlignasAttr->getLocation(), diag::err_alignas_mismatch) |
| 1885 | << (unsigned)S.Context.toCharUnitsFromBits(OldAlign).getQuantity() |
| 1886 | << (unsigned)S.Context.toCharUnitsFromBits(NewAlign).getQuantity(); |
| 1887 | S.Diag(OldAlignasAttr->getLocation(), diag::note_previous_declaration); |
| 1888 | } |
| 1889 | } |
| 1890 | |
| 1891 | if (OldAlignasAttr && !NewAlignasAttr && isAttributeTargetADefinition(New)) { |
| 1892 | // C++11 [dcl.align]p6: |
| 1893 | // if any declaration of an entity has an alignment-specifier, |
| 1894 | // every defining declaration of that entity shall specify an |
| 1895 | // equivalent alignment. |
| 1896 | // C11 6.7.5/7: |
| 1897 | // If the definition of an object does not have an alignment |
| 1898 | // specifier, any other declaration of that object shall also |
| 1899 | // have no alignment specifier. |
| 1900 | S.Diag(New->getLocation(), diag::err_alignas_missing_on_definition) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1901 | << OldAlignasAttr; |
Richard Smith | 671b321 | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 1902 | S.Diag(OldAlignasAttr->getLocation(), diag::note_alignas_on_declaration) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1903 | << OldAlignasAttr; |
Richard Smith | 671b321 | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 1904 | } |
| 1905 | |
| 1906 | bool AnyAdded = false; |
| 1907 | |
| 1908 | // Ensure we have an attribute representing the strictest alignment. |
| 1909 | if (OldAlign > NewAlign) { |
| 1910 | AlignedAttr *Clone = OldStrictestAlignAttr->clone(S.Context); |
| 1911 | Clone->setInherited(true); |
| 1912 | New->addAttr(Clone); |
| 1913 | AnyAdded = true; |
| 1914 | } |
| 1915 | |
| 1916 | // Ensure we have an alignas attribute if the old declaration had one. |
| 1917 | if (OldAlignasAttr && !NewAlignasAttr && |
| 1918 | !(AnyAdded && OldStrictestAlignAttr->isAlignas())) { |
| 1919 | AlignedAttr *Clone = OldAlignasAttr->clone(S.Context); |
| 1920 | Clone->setInherited(true); |
| 1921 | New->addAttr(Clone); |
| 1922 | AnyAdded = true; |
| 1923 | } |
| 1924 | |
| 1925 | return AnyAdded; |
| 1926 | } |
| 1927 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1928 | static bool mergeDeclAttribute(Sema &S, NamedDecl *D, |
| 1929 | const InheritableAttr *Attr, bool Override) { |
| 1930 | InheritableAttr *NewAttr = nullptr; |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1931 | unsigned AttrSpellingListIndex = Attr->getSpellingListIndex(); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1932 | if (const auto *AA = dyn_cast<AvailabilityAttr>(Attr)) |
Richard Smith | 671b321 | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 1933 | NewAttr = S.mergeAvailabilityAttr(D, AA->getRange(), AA->getPlatform(), |
| 1934 | AA->getIntroduced(), AA->getDeprecated(), |
| 1935 | AA->getObsoleted(), AA->getUnavailable(), |
| 1936 | AA->getMessage(), Override, |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 1937 | AttrSpellingListIndex); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1938 | else if (const auto *VA = dyn_cast<VisibilityAttr>(Attr)) |
Richard Smith | 671b321 | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 1939 | NewAttr = S.mergeVisibilityAttr(D, VA->getRange(), VA->getVisibility(), |
| 1940 | AttrSpellingListIndex); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1941 | else if (const auto *VA = dyn_cast<TypeVisibilityAttr>(Attr)) |
Richard Smith | 671b321 | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 1942 | NewAttr = S.mergeTypeVisibilityAttr(D, VA->getRange(), VA->getVisibility(), |
| 1943 | AttrSpellingListIndex); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1944 | else if (const auto *ImportA = dyn_cast<DLLImportAttr>(Attr)) |
Richard Smith | 671b321 | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 1945 | NewAttr = S.mergeDLLImportAttr(D, ImportA->getRange(), |
| 1946 | AttrSpellingListIndex); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1947 | else if (const auto *ExportA = dyn_cast<DLLExportAttr>(Attr)) |
Richard Smith | 671b321 | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 1948 | NewAttr = S.mergeDLLExportAttr(D, ExportA->getRange(), |
| 1949 | AttrSpellingListIndex); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1950 | else if (const auto *FA = dyn_cast<FormatAttr>(Attr)) |
Richard Smith | 671b321 | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 1951 | NewAttr = S.mergeFormatAttr(D, FA->getRange(), FA->getType(), |
| 1952 | FA->getFormatIdx(), FA->getFirstArg(), |
| 1953 | AttrSpellingListIndex); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1954 | else if (const auto *SA = dyn_cast<SectionAttr>(Attr)) |
Richard Smith | 671b321 | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 1955 | NewAttr = S.mergeSectionAttr(D, SA->getRange(), SA->getName(), |
| 1956 | AttrSpellingListIndex); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1957 | else if (const auto *IA = dyn_cast<MSInheritanceAttr>(Attr)) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1958 | NewAttr = S.mergeMSInheritanceAttr(D, IA->getRange(), IA->getBestCase(), |
| 1959 | AttrSpellingListIndex, |
| 1960 | IA->getSemanticSpelling()); |
Richard Smith | 671b321 | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 1961 | else if (isa<AlignedAttr>(Attr)) |
| 1962 | // AlignedAttrs are handled separately, because we need to handle all |
| 1963 | // such attributes on a declaration at the same time. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1964 | NewAttr = nullptr; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1965 | else if (Attr->duplicatesAllowed() || !DeclHasAttr(D, Attr)) |
Richard Smith | 671b321 | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 1966 | NewAttr = cast<InheritableAttr>(Attr->clone(S.Context)); |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1967 | |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1968 | if (NewAttr) { |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1969 | NewAttr->setInherited(true); |
| 1970 | D->addAttr(NewAttr); |
| 1971 | return true; |
| 1972 | } |
| 1973 | |
| 1974 | return false; |
| 1975 | } |
| 1976 | |
Rafael Espindola | 4b044c6 | 2012-07-15 01:05:36 +0000 | [diff] [blame] | 1977 | static const Decl *getDefinition(const Decl *D) { |
| 1978 | if (const TagDecl *TD = dyn_cast<TagDecl>(D)) |
Rafael Espindola | 3f66406 | 2012-05-18 01:47:00 +0000 | [diff] [blame] | 1979 | return TD->getDefinition(); |
Rafael Espindola | b1c0e20 | 2013-10-22 21:39:03 +0000 | [diff] [blame] | 1980 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 1981 | const VarDecl *Def = VD->getDefinition(); |
| 1982 | if (Def) |
| 1983 | return Def; |
| 1984 | return VD->getActingDefinition(); |
| 1985 | } |
Rafael Espindola | 4b044c6 | 2012-07-15 01:05:36 +0000 | [diff] [blame] | 1986 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Rafael Espindola | 3f66406 | 2012-05-18 01:47:00 +0000 | [diff] [blame] | 1987 | const FunctionDecl* Def; |
Rafael Espindola | b1c0e20 | 2013-10-22 21:39:03 +0000 | [diff] [blame] | 1988 | if (FD->isDefined(Def)) |
Rafael Espindola | 3f66406 | 2012-05-18 01:47:00 +0000 | [diff] [blame] | 1989 | return Def; |
| 1990 | } |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1991 | return nullptr; |
Rafael Espindola | 3f66406 | 2012-05-18 01:47:00 +0000 | [diff] [blame] | 1992 | } |
| 1993 | |
Rafael Espindola | d320ffc | 2012-07-15 01:33:40 +0000 | [diff] [blame] | 1994 | static bool hasAttribute(const Decl *D, attr::Kind Kind) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1995 | for (const auto *Attribute : D->attrs()) |
Rafael Espindola | d320ffc | 2012-07-15 01:33:40 +0000 | [diff] [blame] | 1996 | if (Attribute->getKind() == Kind) |
| 1997 | return true; |
Rafael Espindola | d320ffc | 2012-07-15 01:33:40 +0000 | [diff] [blame] | 1998 | return false; |
| 1999 | } |
| 2000 | |
| 2001 | /// checkNewAttributesAfterDef - If we already have a definition, check that |
| 2002 | /// there are no new attributes in this declaration. |
| 2003 | static void checkNewAttributesAfterDef(Sema &S, Decl *New, const Decl *Old) { |
| 2004 | if (!New->hasAttrs()) |
| 2005 | return; |
| 2006 | |
| 2007 | const Decl *Def = getDefinition(Old); |
| 2008 | if (!Def || Def == New) |
| 2009 | return; |
| 2010 | |
| 2011 | AttrVec &NewAttributes = New->getAttrs(); |
| 2012 | for (unsigned I = 0, E = NewAttributes.size(); I != E;) { |
| 2013 | const Attr *NewAttribute = NewAttributes[I]; |
Rafael Espindola | b1c0e20 | 2013-10-22 21:39:03 +0000 | [diff] [blame] | 2014 | |
| 2015 | if (isa<AliasAttr>(NewAttribute)) { |
| 2016 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(New)) |
| 2017 | S.CheckForFunctionRedefinition(FD, cast<FunctionDecl>(Def)); |
| 2018 | else { |
| 2019 | VarDecl *VD = cast<VarDecl>(New); |
| 2020 | unsigned Diag = cast<VarDecl>(Def)->isThisDeclarationADefinition() == |
| 2021 | VarDecl::TentativeDefinition |
| 2022 | ? diag::err_alias_after_tentative |
| 2023 | : diag::err_redefinition; |
| 2024 | S.Diag(VD->getLocation(), Diag) << VD->getDeclName(); |
| 2025 | S.Diag(Def->getLocation(), diag::note_previous_definition); |
| 2026 | VD->setInvalidDecl(); |
| 2027 | } |
| 2028 | ++I; |
| 2029 | continue; |
| 2030 | } |
| 2031 | |
| 2032 | if (const VarDecl *VD = dyn_cast<VarDecl>(Def)) { |
| 2033 | // Tentative definitions are only interesting for the alias check above. |
| 2034 | if (VD->isThisDeclarationADefinition() != VarDecl::Definition) { |
| 2035 | ++I; |
| 2036 | continue; |
| 2037 | } |
| 2038 | } |
| 2039 | |
Rafael Espindola | d320ffc | 2012-07-15 01:33:40 +0000 | [diff] [blame] | 2040 | if (hasAttribute(Def, NewAttribute->getKind())) { |
| 2041 | ++I; |
| 2042 | continue; // regular attr merging will take care of validating this. |
| 2043 | } |
Richard Smith | 671b321 | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 2044 | |
Richard Smith | 7586a6e | 2013-01-30 05:45:05 +0000 | [diff] [blame] | 2045 | if (isa<C11NoReturnAttr>(NewAttribute)) { |
Richard Smith | 671b321 | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 2046 | // C's _Noreturn is allowed to be added to a function after it is defined. |
Richard Smith | 7586a6e | 2013-01-30 05:45:05 +0000 | [diff] [blame] | 2047 | ++I; |
| 2048 | continue; |
Richard Smith | 671b321 | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 2049 | } else if (const AlignedAttr *AA = dyn_cast<AlignedAttr>(NewAttribute)) { |
| 2050 | if (AA->isAlignas()) { |
| 2051 | // C++11 [dcl.align]p6: |
| 2052 | // if any declaration of an entity has an alignment-specifier, |
| 2053 | // every defining declaration of that entity shall specify an |
| 2054 | // equivalent alignment. |
| 2055 | // C11 6.7.5/7: |
| 2056 | // If the definition of an object does not have an alignment |
| 2057 | // specifier, any other declaration of that object shall also |
| 2058 | // have no alignment specifier. |
| 2059 | S.Diag(Def->getLocation(), diag::err_alignas_missing_on_definition) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2060 | << AA; |
Richard Smith | 671b321 | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 2061 | S.Diag(NewAttribute->getLocation(), diag::note_alignas_on_declaration) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2062 | << AA; |
Richard Smith | 671b321 | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 2063 | NewAttributes.erase(NewAttributes.begin() + I); |
| 2064 | --E; |
| 2065 | continue; |
| 2066 | } |
Richard Smith | 7586a6e | 2013-01-30 05:45:05 +0000 | [diff] [blame] | 2067 | } |
Richard Smith | 671b321 | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 2068 | |
Rafael Espindola | d320ffc | 2012-07-15 01:33:40 +0000 | [diff] [blame] | 2069 | S.Diag(NewAttribute->getLocation(), |
| 2070 | diag::warn_attribute_precede_definition); |
| 2071 | S.Diag(Def->getLocation(), diag::note_previous_definition); |
| 2072 | NewAttributes.erase(NewAttributes.begin() + I); |
| 2073 | --E; |
| 2074 | } |
| 2075 | } |
| 2076 | |
John McCall | eca5d22 | 2011-03-02 04:00:57 +0000 | [diff] [blame] | 2077 | /// mergeDeclAttributes - Copy attributes from the Old decl to the New one. |
Rafael Espindola | 51be6e3 | 2013-01-08 22:04:34 +0000 | [diff] [blame] | 2078 | void Sema::mergeDeclAttributes(NamedDecl *New, Decl *Old, |
Douglas Gregor | f4d918f | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2079 | AvailabilityMergeKind AMK) { |
Rafael Espindola | 101e9dc | 2013-10-25 01:28:12 +0000 | [diff] [blame] | 2080 | if (UsedAttr *OldAttr = Old->getMostRecentDecl()->getAttr<UsedAttr>()) { |
| 2081 | UsedAttr *NewAttr = OldAttr->clone(Context); |
| 2082 | NewAttr->setInherited(true); |
| 2083 | New->addAttr(NewAttr); |
| 2084 | } |
| 2085 | |
Richard Smith | 3a2b7a1 | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 2086 | if (!Old->hasAttrs() && !New->hasAttrs()) |
| 2087 | return; |
| 2088 | |
Rafael Espindola | 3f66406 | 2012-05-18 01:47:00 +0000 | [diff] [blame] | 2089 | // attributes declared post-definition are currently ignored |
Rafael Espindola | d320ffc | 2012-07-15 01:33:40 +0000 | [diff] [blame] | 2090 | checkNewAttributesAfterDef(*this, New, Old); |
Rafael Espindola | 3f66406 | 2012-05-18 01:47:00 +0000 | [diff] [blame] | 2091 | |
Douglas Gregor | 27c6da2 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 2092 | if (!Old->hasAttrs()) |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2093 | return; |
John McCall | eca5d22 | 2011-03-02 04:00:57 +0000 | [diff] [blame] | 2094 | |
Douglas Gregor | 27c6da2 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 2095 | bool foundAny = New->hasAttrs(); |
John McCall | eca5d22 | 2011-03-02 04:00:57 +0000 | [diff] [blame] | 2096 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2097 | // Ensure that any moving of objects within the allocated map is done before |
| 2098 | // we process them. |
Douglas Gregor | 27c6da2 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 2099 | if (!foundAny) New->setAttrs(AttrVec()); |
John McCall | eca5d22 | 2011-03-02 04:00:57 +0000 | [diff] [blame] | 2100 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2101 | for (auto *I : Old->specific_attrs<InheritableAttr>()) { |
Douglas Gregor | f4d918f | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2102 | bool Override = false; |
Douglas Gregor | c193dd8 | 2011-09-23 20:23:42 +0000 | [diff] [blame] | 2103 | // Ignore deprecated/unavailable/availability attributes if requested. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2104 | if (isa<DeprecatedAttr>(I) || |
| 2105 | isa<UnavailableAttr>(I) || |
| 2106 | isa<AvailabilityAttr>(I)) { |
Douglas Gregor | f4d918f | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2107 | switch (AMK) { |
| 2108 | case AMK_None: |
| 2109 | continue; |
John McCall | 6c2c250 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 2110 | |
Douglas Gregor | f4d918f | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2111 | case AMK_Redeclaration: |
| 2112 | break; |
| 2113 | |
| 2114 | case AMK_Override: |
| 2115 | Override = true; |
| 2116 | break; |
| 2117 | } |
| 2118 | } |
| 2119 | |
Rafael Espindola | 101e9dc | 2013-10-25 01:28:12 +0000 | [diff] [blame] | 2120 | // Already handled. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2121 | if (isa<UsedAttr>(I)) |
Rafael Espindola | 101e9dc | 2013-10-25 01:28:12 +0000 | [diff] [blame] | 2122 | continue; |
| 2123 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2124 | if (mergeDeclAttribute(*this, New, I, Override)) |
John McCall | eca5d22 | 2011-03-02 04:00:57 +0000 | [diff] [blame] | 2125 | foundAny = true; |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 2126 | } |
John McCall | eca5d22 | 2011-03-02 04:00:57 +0000 | [diff] [blame] | 2127 | |
Richard Smith | 671b321 | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 2128 | if (mergeAlignedAttrs(*this, New, Old)) |
| 2129 | foundAny = true; |
| 2130 | |
Douglas Gregor | 27c6da2 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 2131 | if (!foundAny) New->dropAttrs(); |
John McCall | eca5d22 | 2011-03-02 04:00:57 +0000 | [diff] [blame] | 2132 | } |
| 2133 | |
| 2134 | /// mergeParamDeclAttributes - Copy attributes from the old parameter |
| 2135 | /// to the new one. |
| 2136 | static void mergeParamDeclAttributes(ParmVarDecl *newDecl, |
| 2137 | const ParmVarDecl *oldDecl, |
Richard Smith | 3a2b7a1 | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 2138 | Sema &S) { |
| 2139 | // C++11 [dcl.attr.depend]p2: |
| 2140 | // The first declaration of a function shall specify the |
| 2141 | // carries_dependency attribute for its declarator-id if any declaration |
| 2142 | // of the function specifies the carries_dependency attribute. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2143 | const CarriesDependencyAttr *CDA = newDecl->getAttr<CarriesDependencyAttr>(); |
| 2144 | if (CDA && !oldDecl->hasAttr<CarriesDependencyAttr>()) { |
| 2145 | S.Diag(CDA->getLocation(), |
Richard Smith | 3a2b7a1 | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 2146 | diag::err_carries_dependency_missing_on_first_decl) << 1/*Param*/; |
| 2147 | // Find the first declaration of the parameter. |
| 2148 | // FIXME: Should we build redeclaration chains for function parameters? |
| 2149 | const FunctionDecl *FirstFD = |
Rafael Espindola | bc65091 | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 2150 | cast<FunctionDecl>(oldDecl->getDeclContext())->getFirstDecl(); |
Richard Smith | 3a2b7a1 | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 2151 | const ParmVarDecl *FirstVD = |
| 2152 | FirstFD->getParamDecl(oldDecl->getFunctionScopeIndex()); |
| 2153 | S.Diag(FirstVD->getLocation(), |
| 2154 | diag::note_carries_dependency_missing_first_decl) << 1/*Param*/; |
| 2155 | } |
| 2156 | |
John McCall | eca5d22 | 2011-03-02 04:00:57 +0000 | [diff] [blame] | 2157 | if (!oldDecl->hasAttrs()) |
| 2158 | return; |
| 2159 | |
| 2160 | bool foundAny = newDecl->hasAttrs(); |
| 2161 | |
| 2162 | // Ensure that any moving of objects within the allocated map is |
| 2163 | // done before we process them. |
| 2164 | if (!foundAny) newDecl->setAttrs(AttrVec()); |
| 2165 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2166 | for (const auto *I : oldDecl->specific_attrs<InheritableParamAttr>()) { |
| 2167 | if (!DeclHasAttr(newDecl, I)) { |
Richard Smith | 3a2b7a1 | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 2168 | InheritableAttr *newAttr = |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2169 | cast<InheritableParamAttr>(I->clone(S.Context)); |
John McCall | eca5d22 | 2011-03-02 04:00:57 +0000 | [diff] [blame] | 2170 | newAttr->setInherited(true); |
| 2171 | newDecl->addAttr(newAttr); |
| 2172 | foundAny = true; |
| 2173 | } |
| 2174 | } |
| 2175 | |
| 2176 | if (!foundAny) newDecl->dropAttrs(); |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 2177 | } |
| 2178 | |
Dan Gohman | 3c46e8d | 2010-07-26 21:25:24 +0000 | [diff] [blame] | 2179 | namespace { |
| 2180 | |
Douglas Gregor | c837656 | 2009-03-06 22:43:54 +0000 | [diff] [blame] | 2181 | /// Used in MergeFunctionDecl to keep track of function parameters in |
| 2182 | /// C. |
| 2183 | struct GNUCompatibleParamWarning { |
| 2184 | ParmVarDecl *OldParm; |
| 2185 | ParmVarDecl *NewParm; |
| 2186 | QualType PromotedType; |
| 2187 | }; |
| 2188 | |
Dan Gohman | 3c46e8d | 2010-07-26 21:25:24 +0000 | [diff] [blame] | 2189 | } |
Anders Carlsson | 5c478cf | 2009-12-04 22:33:25 +0000 | [diff] [blame] | 2190 | |
| 2191 | /// getSpecialMember - get the special member enum for a method. |
Anders Carlsson | 3b8c53b | 2010-04-22 05:40:53 +0000 | [diff] [blame] | 2192 | Sema::CXXSpecialMember Sema::getSpecialMember(const CXXMethodDecl *MD) { |
Anders Carlsson | 5c478cf | 2009-12-04 22:33:25 +0000 | [diff] [blame] | 2193 | if (const CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(MD)) { |
Sean Hunt | f961ea5 | 2011-05-10 19:08:14 +0000 | [diff] [blame] | 2194 | if (Ctor->isDefaultConstructor()) |
| 2195 | return Sema::CXXDefaultConstructor; |
Sean Hunt | 9ae60d5 | 2011-05-26 01:26:05 +0000 | [diff] [blame] | 2196 | |
| 2197 | if (Ctor->isCopyConstructor()) |
| 2198 | return Sema::CXXCopyConstructor; |
| 2199 | |
| 2200 | if (Ctor->isMoveConstructor()) |
| 2201 | return Sema::CXXMoveConstructor; |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 2202 | } else if (isa<CXXDestructorDecl>(MD)) { |
Anders Carlsson | 5c478cf | 2009-12-04 22:33:25 +0000 | [diff] [blame] | 2203 | return Sema::CXXDestructor; |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 2204 | } else if (MD->isCopyAssignmentOperator()) { |
Sean Hunt | f961ea5 | 2011-05-10 19:08:14 +0000 | [diff] [blame] | 2205 | return Sema::CXXCopyAssignment; |
Sebastian Redl | 74e611a | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 2206 | } else if (MD->isMoveAssignmentOperator()) { |
| 2207 | return Sema::CXXMoveAssignment; |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 2208 | } |
Sean Hunt | f961ea5 | 2011-05-10 19:08:14 +0000 | [diff] [blame] | 2209 | |
Sean Hunt | f961ea5 | 2011-05-10 19:08:14 +0000 | [diff] [blame] | 2210 | return Sema::CXXInvalid; |
Anders Carlsson | 5c478cf | 2009-12-04 22:33:25 +0000 | [diff] [blame] | 2211 | } |
| 2212 | |
Sebastian Redl | 515ddd8 | 2010-06-09 21:17:41 +0000 | [diff] [blame] | 2213 | /// canRedefineFunction - checks if a function can be redefined. Currently, |
Charles Davis | f3f8d2a | 2010-02-18 02:00:42 +0000 | [diff] [blame] | 2214 | /// only extern inline functions can be redefined, and even then only in |
| 2215 | /// GNU89 mode. |
| 2216 | static bool canRedefineFunction(const FunctionDecl *FD, |
| 2217 | const LangOptions& LangOpts) { |
Eli Friedman | eca3ed7 | 2011-06-13 23:56:42 +0000 | [diff] [blame] | 2218 | return ((FD->hasAttr<GNUInlineAttr>() || LangOpts.GNUInline) && |
| 2219 | !LangOpts.CPlusPlus && |
Charles Davis | f3f8d2a | 2010-02-18 02:00:42 +0000 | [diff] [blame] | 2220 | FD->isInlineSpecified() && |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2221 | FD->getStorageClass() == SC_Extern); |
Charles Davis | f3f8d2a | 2010-02-18 02:00:42 +0000 | [diff] [blame] | 2222 | } |
| 2223 | |
Reid Kleckner | ef07203 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 2224 | const AttributedType *Sema::getCallingConvAttributedType(QualType T) const { |
| 2225 | const AttributedType *AT = T->getAs<AttributedType>(); |
| 2226 | while (AT && !AT->isCallingConv()) |
| 2227 | AT = AT->getModifiedType()->getAs<AttributedType>(); |
| 2228 | return AT; |
John McCall | fb60914 | 2012-08-25 02:00:03 +0000 | [diff] [blame] | 2229 | } |
| 2230 | |
Benjamin Kramer | a574c89 | 2013-02-15 12:30:38 +0000 | [diff] [blame] | 2231 | template <typename T> |
| 2232 | static bool haveIncompatibleLanguageLinkages(const T *Old, const T *New) { |
Rafael Espindola | 950fee2 | 2013-02-14 01:18:37 +0000 | [diff] [blame] | 2233 | const DeclContext *DC = Old->getDeclContext(); |
| 2234 | if (DC->isRecord()) |
| 2235 | return false; |
| 2236 | |
| 2237 | LanguageLinkage OldLinkage = Old->getLanguageLinkage(); |
Rafael Espindola | d8ffd0b | 2013-05-05 20:15:21 +0000 | [diff] [blame] | 2238 | if (OldLinkage == CXXLanguageLinkage && New->isInExternCContext()) |
Rafael Espindola | 950fee2 | 2013-02-14 01:18:37 +0000 | [diff] [blame] | 2239 | return true; |
Rafael Espindola | d8ffd0b | 2013-05-05 20:15:21 +0000 | [diff] [blame] | 2240 | if (OldLinkage == CLanguageLinkage && New->isInExternCXXContext()) |
Rafael Espindola | 950fee2 | 2013-02-14 01:18:37 +0000 | [diff] [blame] | 2241 | return true; |
| 2242 | return false; |
| 2243 | } |
| 2244 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2245 | /// MergeFunctionDecl - We just parsed a function 'New' from |
| 2246 | /// declarator D which has the same name and scope as a previous |
| 2247 | /// declaration 'Old'. Figure out how to resolve this situation, |
| 2248 | /// merging decls or emitting diagnostics as appropriate. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2249 | /// |
| 2250 | /// In C++, New and Old must be declarations that are not |
| 2251 | /// overloaded. Use IsOverload to determine whether New and Old are |
| 2252 | /// overloaded, and to select the Old declaration that New should be |
| 2253 | /// merged with. |
Douglas Gregor | cda9c67 | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 2254 | /// |
| 2255 | /// Returns true if there was an error, false otherwise. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2256 | bool Sema::MergeFunctionDecl(FunctionDecl *New, NamedDecl *&OldD, |
| 2257 | Scope *S, bool MergeTypeWithOld) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2258 | // Verify the old decl was also a function. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2259 | FunctionDecl *Old = OldD->getAsFunction(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2260 | if (!Old) { |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 2261 | if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(OldD)) { |
John McCall | 78037ac | 2013-04-03 21:19:47 +0000 | [diff] [blame] | 2262 | if (New->getFriendObjectKind()) { |
| 2263 | Diag(New->getLocation(), diag::err_using_decl_friend); |
| 2264 | Diag(Shadow->getTargetDecl()->getLocation(), |
| 2265 | diag::note_using_decl_target); |
| 2266 | Diag(Shadow->getUsingDecl()->getLocation(), |
| 2267 | diag::note_using_decl) << 0; |
| 2268 | return true; |
| 2269 | } |
| 2270 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2271 | // C++11 [namespace.udecl]p14: |
| 2272 | // If a function declaration in namespace scope or block scope has the |
| 2273 | // same name and the same parameter-type-list as a function introduced |
| 2274 | // by a using-declaration, and the declarations do not declare the same |
| 2275 | // function, the program is ill-formed. |
| 2276 | |
| 2277 | // Check whether the two declarations might declare the same function. |
| 2278 | Old = dyn_cast<FunctionDecl>(Shadow->getTargetDecl()); |
| 2279 | if (Old && |
| 2280 | !Old->getDeclContext()->getRedeclContext()->Equals( |
| 2281 | New->getDeclContext()->getRedeclContext()) && |
| 2282 | !(Old->isExternC() && New->isExternC())) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 2283 | Old = nullptr; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2284 | |
| 2285 | if (!Old) { |
| 2286 | Diag(New->getLocation(), diag::err_using_decl_conflict_reverse); |
| 2287 | Diag(Shadow->getTargetDecl()->getLocation(), |
| 2288 | diag::note_using_decl_target); |
| 2289 | Diag(Shadow->getUsingDecl()->getLocation(), diag::note_using_decl) << 0; |
| 2290 | return true; |
| 2291 | } |
| 2292 | OldD = Old; |
| 2293 | } else { |
| 2294 | Diag(New->getLocation(), diag::err_redefinition_different_kind) |
| 2295 | << New->getDeclName(); |
| 2296 | Diag(OldD->getLocation(), diag::note_previous_definition); |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 2297 | return true; |
| 2298 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2299 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2300 | |
David Majnemer | bcd0650 | 2013-07-07 23:49:50 +0000 | [diff] [blame] | 2301 | // If the old declaration is invalid, just give up here. |
| 2302 | if (Old->isInvalidDecl()) |
| 2303 | return true; |
| 2304 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2305 | // Determine whether the previous declaration was a definition, |
| 2306 | // implicit declaration, or a declaration. |
| 2307 | diag::kind PrevDiag; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2308 | SourceLocation OldLocation = Old->getLocation(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2309 | if (Old->isThisDeclarationADefinition()) |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 2310 | PrevDiag = diag::note_previous_definition; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2311 | else if (Old->isImplicit()) { |
Douglas Gregor | cda9c67 | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 2312 | PrevDiag = diag::note_previous_implicit_declaration; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2313 | if (OldLocation.isInvalid()) |
| 2314 | OldLocation = New->getLocation(); |
| 2315 | } else |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 2316 | PrevDiag = diag::note_previous_declaration; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2317 | |
Charles Davis | f3f8d2a | 2010-02-18 02:00:42 +0000 | [diff] [blame] | 2318 | // Don't complain about this if we're in GNU89 mode and the old function |
| 2319 | // is an extern inline function. |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 2320 | // Don't complain about specializations. They are not supposed to have |
| 2321 | // storage classes. |
Douglas Gregor | 04495c8 | 2009-02-24 01:23:02 +0000 | [diff] [blame] | 2322 | if (!isa<CXXMethodDecl>(New) && !isa<CXXMethodDecl>(Old) && |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2323 | New->getStorageClass() == SC_Static && |
Rafael Espindola | 181e3ec | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 2324 | Old->hasExternalFormalLinkage() && |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 2325 | !New->getTemplateSpecializationInfo() && |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2326 | !canRedefineFunction(Old, getLangOpts())) { |
| 2327 | if (getLangOpts().MicrosoftExt) { |
Francois Pichet | 4bada2e | 2011-04-22 19:50:06 +0000 | [diff] [blame] | 2328 | Diag(New->getLocation(), diag::warn_static_non_static) << New; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2329 | Diag(OldLocation, PrevDiag); |
Francois Pichet | 4bada2e | 2011-04-22 19:50:06 +0000 | [diff] [blame] | 2330 | } else { |
| 2331 | Diag(New->getLocation(), diag::err_static_non_static) << New; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2332 | Diag(OldLocation, PrevDiag); |
Francois Pichet | 4bada2e | 2011-04-22 19:50:06 +0000 | [diff] [blame] | 2333 | return true; |
| 2334 | } |
Douglas Gregor | 04495c8 | 2009-02-24 01:23:02 +0000 | [diff] [blame] | 2335 | } |
| 2336 | |
Reid Kleckner | ef07203 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 2337 | |
| 2338 | // If a function is first declared with a calling convention, but is later |
| 2339 | // declared or defined without one, all following decls assume the calling |
| 2340 | // convention of the first. |
John McCall | f82b4e8 | 2010-02-04 05:44:44 +0000 | [diff] [blame] | 2341 | // |
John McCall | fb60914 | 2012-08-25 02:00:03 +0000 | [diff] [blame] | 2342 | // It's OK if a function is first declared without a calling convention, |
| 2343 | // but is later declared or defined with the default calling convention. |
| 2344 | // |
Reid Kleckner | ef07203 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 2345 | // To test if either decl has an explicit calling convention, we look for |
| 2346 | // AttributedType sugar nodes on the type as written. If they are missing or |
| 2347 | // were canonicalized away, we assume the calling convention was implicit. |
John McCall | f82b4e8 | 2010-02-04 05:44:44 +0000 | [diff] [blame] | 2348 | // |
| 2349 | // Note also that we DO NOT return at this point, because we still have |
| 2350 | // other tests to run. |
Reid Kleckner | ef07203 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 2351 | QualType OldQType = Context.getCanonicalType(Old->getType()); |
| 2352 | QualType NewQType = Context.getCanonicalType(New->getType()); |
John McCall | e6a365d | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 2353 | const FunctionType *OldType = cast<FunctionType>(OldQType); |
Reid Kleckner | ef07203 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 2354 | const FunctionType *NewType = cast<FunctionType>(NewQType); |
John McCall | e6a365d | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 2355 | FunctionType::ExtInfo OldTypeInfo = OldType->getExtInfo(); |
| 2356 | FunctionType::ExtInfo NewTypeInfo = NewType->getExtInfo(); |
| 2357 | bool RequiresAdjustment = false; |
John McCall | fb60914 | 2012-08-25 02:00:03 +0000 | [diff] [blame] | 2358 | |
Reid Kleckner | ef07203 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 2359 | if (OldTypeInfo.getCC() != NewTypeInfo.getCC()) { |
Rafael Espindola | bc65091 | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 2360 | FunctionDecl *First = Old->getFirstDecl(); |
Reid Kleckner | ef07203 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 2361 | const FunctionType *FT = |
| 2362 | First->getType().getCanonicalType()->castAs<FunctionType>(); |
| 2363 | FunctionType::ExtInfo FI = FT->getExtInfo(); |
| 2364 | bool NewCCExplicit = getCallingConvAttributedType(New->getType()); |
| 2365 | if (!NewCCExplicit) { |
| 2366 | // Inherit the CC from the previous declaration if it was specified |
| 2367 | // there but not here. |
| 2368 | NewTypeInfo = NewTypeInfo.withCallingConv(OldTypeInfo.getCC()); |
| 2369 | RequiresAdjustment = true; |
| 2370 | } else { |
| 2371 | // Calling conventions aren't compatible, so complain. |
| 2372 | bool FirstCCExplicit = getCallingConvAttributedType(First->getType()); |
| 2373 | Diag(New->getLocation(), diag::err_cconv_change) |
| 2374 | << FunctionType::getNameForCallConv(NewTypeInfo.getCC()) |
| 2375 | << !FirstCCExplicit |
| 2376 | << (!FirstCCExplicit ? "" : |
| 2377 | FunctionType::getNameForCallConv(FI.getCC())); |
John McCall | fb60914 | 2012-08-25 02:00:03 +0000 | [diff] [blame] | 2378 | |
Reid Kleckner | ef07203 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 2379 | // Put the note on the first decl, since it is the one that matters. |
| 2380 | Diag(First->getLocation(), diag::note_previous_declaration); |
| 2381 | return true; |
| 2382 | } |
John McCall | f82b4e8 | 2010-02-04 05:44:44 +0000 | [diff] [blame] | 2383 | } |
| 2384 | |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 2385 | // FIXME: diagnose the other way around? |
John McCall | e6a365d | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 2386 | if (OldTypeInfo.getNoReturn() && !NewTypeInfo.getNoReturn()) { |
| 2387 | NewTypeInfo = NewTypeInfo.withNoReturn(true); |
| 2388 | RequiresAdjustment = true; |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 2389 | } |
| 2390 | |
Douglas Gregor | d2c6490 | 2010-06-18 21:30:25 +0000 | [diff] [blame] | 2391 | // Merge regparm attribute. |
Eli Friedman | a49218e | 2011-04-09 08:18:08 +0000 | [diff] [blame] | 2392 | if (OldTypeInfo.getHasRegParm() != NewTypeInfo.getHasRegParm() || |
| 2393 | OldTypeInfo.getRegParm() != NewTypeInfo.getRegParm()) { |
| 2394 | if (NewTypeInfo.getHasRegParm()) { |
Douglas Gregor | d2c6490 | 2010-06-18 21:30:25 +0000 | [diff] [blame] | 2395 | Diag(New->getLocation(), diag::err_regparm_mismatch) |
| 2396 | << NewType->getRegParmType() |
| 2397 | << OldType->getRegParmType(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2398 | Diag(OldLocation, diag::note_previous_declaration); |
Douglas Gregor | d2c6490 | 2010-06-18 21:30:25 +0000 | [diff] [blame] | 2399 | return true; |
| 2400 | } |
John McCall | e6a365d | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 2401 | |
| 2402 | NewTypeInfo = NewTypeInfo.withRegParm(OldTypeInfo.getRegParm()); |
| 2403 | RequiresAdjustment = true; |
| 2404 | } |
| 2405 | |
Douglas Gregor | cb1c9c3 | 2011-10-14 15:55:40 +0000 | [diff] [blame] | 2406 | // Merge ns_returns_retained attribute. |
| 2407 | if (OldTypeInfo.getProducesResult() != NewTypeInfo.getProducesResult()) { |
| 2408 | if (NewTypeInfo.getProducesResult()) { |
| 2409 | Diag(New->getLocation(), diag::err_returns_retained_mismatch); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2410 | Diag(OldLocation, diag::note_previous_declaration); |
Douglas Gregor | cb1c9c3 | 2011-10-14 15:55:40 +0000 | [diff] [blame] | 2411 | return true; |
| 2412 | } |
| 2413 | |
| 2414 | NewTypeInfo = NewTypeInfo.withProducesResult(true); |
| 2415 | RequiresAdjustment = true; |
| 2416 | } |
| 2417 | |
John McCall | e6a365d | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 2418 | if (RequiresAdjustment) { |
Eli Friedman | 130fcc8 | 2013-09-06 21:09:09 +0000 | [diff] [blame] | 2419 | const FunctionType *AdjustedType = New->getType()->getAs<FunctionType>(); |
| 2420 | AdjustedType = Context.adjustFunctionType(AdjustedType, NewTypeInfo); |
| 2421 | New->setType(QualType(AdjustedType, 0)); |
John McCall | e6a365d | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 2422 | NewQType = Context.getCanonicalType(New->getType()); |
Eli Friedman | 130fcc8 | 2013-09-06 21:09:09 +0000 | [diff] [blame] | 2423 | NewType = cast<FunctionType>(NewQType); |
Douglas Gregor | d2c6490 | 2010-06-18 21:30:25 +0000 | [diff] [blame] | 2424 | } |
Nick Lewycky | cd0655b | 2013-02-01 08:13:20 +0000 | [diff] [blame] | 2425 | |
| 2426 | // If this redeclaration makes the function inline, we may need to add it to |
| 2427 | // UndefinedButUsed. |
| 2428 | if (!Old->isInlined() && New->isInlined() && |
| 2429 | !New->hasAttr<GNUInlineAttr>() && |
| 2430 | (getLangOpts().CPlusPlus || !getLangOpts().GNUInline) && |
| 2431 | Old->isUsed(false) && |
| 2432 | !Old->isDefined() && !New->isThisDeclarationADefinition()) |
| 2433 | UndefinedButUsed.insert(std::make_pair(Old->getCanonicalDecl(), |
| 2434 | SourceLocation())); |
| 2435 | |
| 2436 | // If this redeclaration makes it newly gnu_inline, we don't want to warn |
| 2437 | // about it. |
| 2438 | if (New->hasAttr<GNUInlineAttr>() && |
| 2439 | Old->isInlined() && !Old->hasAttr<GNUInlineAttr>()) { |
| 2440 | UndefinedButUsed.erase(Old->getCanonicalDecl()); |
| 2441 | } |
Douglas Gregor | d2c6490 | 2010-06-18 21:30:25 +0000 | [diff] [blame] | 2442 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2443 | if (getLangOpts().CPlusPlus) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2444 | // (C++98 13.1p2): |
| 2445 | // Certain function declarations cannot be overloaded: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2446 | // -- Function declarations that differ only in the return type |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2447 | // cannot be overloaded. |
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2448 | |
| 2449 | // Go back to the type source info to compare the declared return types, |
Richard Smith | 37e849a | 2013-08-14 20:16:31 +0000 | [diff] [blame] | 2450 | // per C++1y [dcl.type.auto]p13: |
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2451 | // Redeclarations or specializations of a function or function template |
| 2452 | // with a declared return type that uses a placeholder type shall also |
| 2453 | // use that placeholder, not a deduced type. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2454 | QualType OldDeclaredReturnType = |
| 2455 | (Old->getTypeSourceInfo() |
| 2456 | ? Old->getTypeSourceInfo()->getType()->castAs<FunctionType>() |
| 2457 | : OldType)->getReturnType(); |
| 2458 | QualType NewDeclaredReturnType = |
| 2459 | (New->getTypeSourceInfo() |
| 2460 | ? New->getTypeSourceInfo()->getType()->castAs<FunctionType>() |
| 2461 | : NewType)->getReturnType(); |
Fariborz Jahanian | 2390a72 | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 2462 | QualType ResQT; |
Richard Smith | a41c97a | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 2463 | if (!Context.hasSameType(OldDeclaredReturnType, NewDeclaredReturnType) && |
| 2464 | !((NewQType->isDependentType() || OldQType->isDependentType()) && |
| 2465 | New->isLocalExternDecl())) { |
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2466 | if (NewDeclaredReturnType->isObjCObjectPointerType() && |
| 2467 | OldDeclaredReturnType->isObjCObjectPointerType()) |
Fariborz Jahanian | 2390a72 | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 2468 | ResQT = Context.mergeObjCGCQualifiers(NewQType, OldQType); |
| 2469 | if (ResQT.isNull()) { |
Argyrios Kyrtzidis | 1de34dd | 2011-02-05 05:54:49 +0000 | [diff] [blame] | 2470 | if (New->isCXXClassMember() && New->isOutOfLine()) |
| 2471 | Diag(New->getLocation(), |
| 2472 | diag::err_member_def_does_not_match_ret_type) << New; |
| 2473 | else |
| 2474 | Diag(New->getLocation(), diag::err_ovl_diff_return_type); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2475 | Diag(OldLocation, PrevDiag) << Old << Old->getType(); |
Fariborz Jahanian | 2390a72 | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 2476 | return true; |
| 2477 | } |
| 2478 | else |
| 2479 | NewQType = ResQT; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2480 | } |
| 2481 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2482 | QualType OldReturnType = OldType->getReturnType(); |
| 2483 | QualType NewReturnType = cast<FunctionType>(NewQType)->getReturnType(); |
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2484 | if (OldReturnType != NewReturnType) { |
| 2485 | // If this function has a deduced return type and has already been |
| 2486 | // defined, copy the deduced value from the old declaration. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2487 | AutoType *OldAT = Old->getReturnType()->getContainedAutoType(); |
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2488 | if (OldAT && OldAT->isDeduced()) { |
Richard Smith | 37e849a | 2013-08-14 20:16:31 +0000 | [diff] [blame] | 2489 | New->setType( |
| 2490 | SubstAutoType(New->getType(), |
| 2491 | OldAT->isDependentType() ? Context.DependentTy |
| 2492 | : OldAT->getDeducedType())); |
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2493 | NewQType = Context.getCanonicalType( |
Richard Smith | 37e849a | 2013-08-14 20:16:31 +0000 | [diff] [blame] | 2494 | SubstAutoType(NewQType, |
| 2495 | OldAT->isDependentType() ? Context.DependentTy |
| 2496 | : OldAT->getDeducedType())); |
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2497 | } |
| 2498 | } |
| 2499 | |
| 2500 | const CXXMethodDecl *OldMethod = dyn_cast<CXXMethodDecl>(Old); |
| 2501 | CXXMethodDecl *NewMethod = dyn_cast<CXXMethodDecl>(New); |
Anders Carlsson | 5c478cf | 2009-12-04 22:33:25 +0000 | [diff] [blame] | 2502 | if (OldMethod && NewMethod) { |
John McCall | 3d04336 | 2010-04-13 07:45:41 +0000 | [diff] [blame] | 2503 | // Preserve triviality. |
| 2504 | NewMethod->setTrivial(OldMethod->isTrivial()); |
Francois Pichet | e1e96a6 | 2011-05-14 19:17:07 +0000 | [diff] [blame] | 2505 | |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 2506 | // MSVC allows explicit template specialization at class scope: |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2507 | // 2 CXXMethodDecls referring to the same function will be injected. |
| 2508 | // We don't want a redeclaration error. |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 2509 | bool IsClassScopeExplicitSpecialization = |
| 2510 | OldMethod->isFunctionTemplateSpecialization() && |
| 2511 | NewMethod->isFunctionTemplateSpecialization(); |
John McCall | 3d04336 | 2010-04-13 07:45:41 +0000 | [diff] [blame] | 2512 | bool isFriend = NewMethod->getFriendObjectKind(); |
| 2513 | |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 2514 | if (!isFriend && NewMethod->getLexicalDeclContext()->isRecord() && |
| 2515 | !IsClassScopeExplicitSpecialization) { |
Anders Carlsson | 5c478cf | 2009-12-04 22:33:25 +0000 | [diff] [blame] | 2516 | // -- Member function declarations with the same name and the |
| 2517 | // same parameter types cannot be overloaded if any of them |
| 2518 | // is a static member function declaration. |
Eli Friedman | fa0d3f8 | 2013-06-19 22:43:55 +0000 | [diff] [blame] | 2519 | if (OldMethod->isStatic() != NewMethod->isStatic()) { |
Anders Carlsson | 5c478cf | 2009-12-04 22:33:25 +0000 | [diff] [blame] | 2520 | Diag(New->getLocation(), diag::err_ovl_static_nonstatic_member); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2521 | Diag(OldLocation, PrevDiag) << Old << Old->getType(); |
Anders Carlsson | 5c478cf | 2009-12-04 22:33:25 +0000 | [diff] [blame] | 2522 | return true; |
| 2523 | } |
Richard Smith | 838925d | 2012-07-13 04:12:04 +0000 | [diff] [blame] | 2524 | |
Anders Carlsson | 5c478cf | 2009-12-04 22:33:25 +0000 | [diff] [blame] | 2525 | // C++ [class.mem]p1: |
| 2526 | // [...] A member shall not be declared twice in the |
| 2527 | // member-specification, except that a nested class or member |
| 2528 | // class template can be declared and then later defined. |
Richard Smith | 838925d | 2012-07-13 04:12:04 +0000 | [diff] [blame] | 2529 | if (ActiveTemplateInstantiations.empty()) { |
| 2530 | unsigned NewDiag; |
| 2531 | if (isa<CXXConstructorDecl>(OldMethod)) |
| 2532 | NewDiag = diag::err_constructor_redeclared; |
| 2533 | else if (isa<CXXDestructorDecl>(NewMethod)) |
| 2534 | NewDiag = diag::err_destructor_redeclared; |
| 2535 | else if (isa<CXXConversionDecl>(NewMethod)) |
| 2536 | NewDiag = diag::err_conv_function_redeclared; |
| 2537 | else |
| 2538 | NewDiag = diag::err_member_redeclared; |
Anders Carlsson | 5c478cf | 2009-12-04 22:33:25 +0000 | [diff] [blame] | 2539 | |
Richard Smith | 838925d | 2012-07-13 04:12:04 +0000 | [diff] [blame] | 2540 | Diag(New->getLocation(), NewDiag); |
| 2541 | } else { |
| 2542 | Diag(New->getLocation(), diag::err_member_redeclared_in_instantiation) |
| 2543 | << New << New->getType(); |
| 2544 | } |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2545 | Diag(OldLocation, PrevDiag) << Old << Old->getType(); |
John McCall | 3d04336 | 2010-04-13 07:45:41 +0000 | [diff] [blame] | 2546 | |
| 2547 | // Complain if this is an explicit declaration of a special |
| 2548 | // member that was initially declared implicitly. |
| 2549 | // |
| 2550 | // As an exception, it's okay to befriend such methods in order |
| 2551 | // to permit the implicit constructor/destructor/operator calls. |
| 2552 | } else if (OldMethod->isImplicit()) { |
| 2553 | if (isFriend) { |
| 2554 | NewMethod->setImplicit(); |
| 2555 | } else { |
Anders Carlsson | 5c478cf | 2009-12-04 22:33:25 +0000 | [diff] [blame] | 2556 | Diag(NewMethod->getLocation(), |
| 2557 | diag::err_definition_of_implicitly_declared_member) |
Anders Carlsson | 3b8c53b | 2010-04-22 05:40:53 +0000 | [diff] [blame] | 2558 | << New << getSpecialMember(OldMethod); |
Anders Carlsson | 5c478cf | 2009-12-04 22:33:25 +0000 | [diff] [blame] | 2559 | return true; |
| 2560 | } |
Richard Smith | f4fe843 | 2012-06-08 01:30:54 +0000 | [diff] [blame] | 2561 | } else if (OldMethod->isExplicitlyDefaulted() && !isFriend) { |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 2562 | Diag(NewMethod->getLocation(), |
| 2563 | diag::err_definition_of_explicitly_defaulted_member) |
| 2564 | << getSpecialMember(OldMethod); |
| 2565 | return true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2566 | } |
| 2567 | } |
| 2568 | |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 2569 | // C++11 [dcl.attr.noreturn]p1: |
| 2570 | // The first declaration of a function shall specify the noreturn |
| 2571 | // attribute if any declaration of that function specifies the noreturn |
| 2572 | // attribute. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2573 | const CXX11NoReturnAttr *NRA = New->getAttr<CXX11NoReturnAttr>(); |
| 2574 | if (NRA && !Old->hasAttr<CXX11NoReturnAttr>()) { |
| 2575 | Diag(NRA->getLocation(), diag::err_noreturn_missing_on_first_decl); |
Rafael Espindola | bc65091 | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 2576 | Diag(Old->getFirstDecl()->getLocation(), |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 2577 | diag::note_noreturn_missing_first_decl); |
| 2578 | } |
| 2579 | |
Richard Smith | 3a2b7a1 | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 2580 | // C++11 [dcl.attr.depend]p2: |
| 2581 | // The first declaration of a function shall specify the |
| 2582 | // carries_dependency attribute for its declarator-id if any declaration |
| 2583 | // of the function specifies the carries_dependency attribute. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2584 | const CarriesDependencyAttr *CDA = New->getAttr<CarriesDependencyAttr>(); |
| 2585 | if (CDA && !Old->hasAttr<CarriesDependencyAttr>()) { |
| 2586 | Diag(CDA->getLocation(), |
Richard Smith | 3a2b7a1 | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 2587 | diag::err_carries_dependency_missing_on_first_decl) << 0/*Function*/; |
Rafael Espindola | bc65091 | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 2588 | Diag(Old->getFirstDecl()->getLocation(), |
Richard Smith | 3a2b7a1 | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 2589 | diag::note_carries_dependency_missing_first_decl) << 0/*Function*/; |
| 2590 | } |
| 2591 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2592 | // (C++98 8.3.5p3): |
| 2593 | // All declarations for a function shall agree exactly in both the |
| 2594 | // return type and the parameter-type-list. |
John McCall | e6a365d | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 2595 | // We also want to respect all the extended bits except noreturn. |
| 2596 | |
| 2597 | // noreturn should now match unless the old type info didn't have it. |
| 2598 | QualType OldQTypeForComparison = OldQType; |
| 2599 | if (!OldTypeInfo.getNoReturn() && NewTypeInfo.getNoReturn()) { |
| 2600 | assert(OldQType == QualType(OldType, 0)); |
| 2601 | const FunctionType *OldTypeForComparison |
| 2602 | = Context.adjustFunctionType(OldType, OldTypeInfo.withNoReturn(true)); |
| 2603 | OldQTypeForComparison = QualType(OldTypeForComparison, 0); |
| 2604 | assert(OldQTypeForComparison.isCanonical()); |
| 2605 | } |
| 2606 | |
Rafael Espindola | 950fee2 | 2013-02-14 01:18:37 +0000 | [diff] [blame] | 2607 | if (haveIncompatibleLanguageLinkages(Old, New)) { |
Alp Toker | a8a2ebe | 2013-10-22 22:53:01 +0000 | [diff] [blame] | 2608 | // As a special case, retain the language linkage from previous |
| 2609 | // declarations of a friend function as an extension. |
| 2610 | // |
| 2611 | // This liberal interpretation of C++ [class.friend]p3 matches GCC/MSVC |
| 2612 | // and is useful because there's otherwise no way to specify language |
| 2613 | // linkage within class scope. |
| 2614 | // |
| 2615 | // Check cautiously as the friend object kind isn't yet complete. |
| 2616 | if (New->getFriendObjectKind() != Decl::FOK_None) { |
| 2617 | Diag(New->getLocation(), diag::ext_retained_language_linkage) << New; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2618 | Diag(OldLocation, PrevDiag); |
Alp Toker | a8a2ebe | 2013-10-22 22:53:01 +0000 | [diff] [blame] | 2619 | } else { |
| 2620 | Diag(New->getLocation(), diag::err_different_language_linkage) << New; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2621 | Diag(OldLocation, PrevDiag); |
Alp Toker | a8a2ebe | 2013-10-22 22:53:01 +0000 | [diff] [blame] | 2622 | return true; |
| 2623 | } |
Rafael Espindola | e57e3d3 | 2012-12-27 03:56:20 +0000 | [diff] [blame] | 2624 | } |
| 2625 | |
John McCall | e6a365d | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 2626 | if (OldQTypeForComparison == NewQType) |
Richard Smith | dd9459f | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 2627 | return MergeCompatibleFunctionDecls(New, Old, S, MergeTypeWithOld); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2628 | |
Richard Smith | a41c97a | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 2629 | if ((NewQType->isDependentType() || OldQType->isDependentType()) && |
| 2630 | New->isLocalExternDecl()) { |
| 2631 | // It's OK if we couldn't merge types for a local function declaraton |
| 2632 | // if either the old or new type is dependent. We'll merge the types |
| 2633 | // when we instantiate the function. |
| 2634 | return false; |
| 2635 | } |
| 2636 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2637 | // Fall through for conflicting redeclarations and redefinitions. |
Douglas Gregor | f009795 | 2008-04-21 02:02:58 +0000 | [diff] [blame] | 2638 | } |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2639 | |
| 2640 | // C: Function types need to be compatible, not identical. This handles |
Steve Naroff | adbbd0c | 2008-01-14 20:51:29 +0000 | [diff] [blame] | 2641 | // duplicate function decls like "void f(int); void f(enum X);" properly. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2642 | if (!getLangOpts().CPlusPlus && |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2643 | Context.typesAreCompatible(OldQType, NewQType)) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2644 | const FunctionType *OldFuncType = OldQType->getAs<FunctionType>(); |
| 2645 | const FunctionType *NewFuncType = NewQType->getAs<FunctionType>(); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 2646 | const FunctionProtoType *OldProto = nullptr; |
Richard Smith | dd9459f | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 2647 | if (MergeTypeWithOld && isa<FunctionNoProtoType>(NewFuncType) && |
Douglas Gregor | c837656 | 2009-03-06 22:43:54 +0000 | [diff] [blame] | 2648 | (OldProto = dyn_cast<FunctionProtoType>(OldFuncType))) { |
Douglas Gregor | 6871981 | 2009-02-16 18:20:44 +0000 | [diff] [blame] | 2649 | // The old declaration provided a function prototype, but the |
| 2650 | // new declaration does not. Merge in the prototype. |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2651 | assert(!OldProto->hasExceptionSpec() && "Exception spec in C"); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2652 | SmallVector<QualType, 16> ParamTypes(OldProto->param_types()); |
| 2653 | NewQType = |
| 2654 | Context.getFunctionType(NewFuncType->getReturnType(), ParamTypes, |
| 2655 | OldProto->getExtProtoInfo()); |
Douglas Gregor | 6871981 | 2009-02-16 18:20:44 +0000 | [diff] [blame] | 2656 | New->setType(NewQType); |
Anders Carlsson | a75e853 | 2009-05-14 21:46:00 +0000 | [diff] [blame] | 2657 | New->setHasInheritedPrototype(); |
Douglas Gregor | 450da98 | 2009-02-16 20:58:07 +0000 | [diff] [blame] | 2658 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 2659 | // Synthesize parameters with the same types. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2660 | SmallVector<ParmVarDecl*, 16> Params; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2661 | for (const auto &ParamType : OldProto->param_types()) { |
| 2662 | ParmVarDecl *Param = ParmVarDecl::Create(Context, New, SourceLocation(), |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 2663 | SourceLocation(), nullptr, |
| 2664 | ParamType, /*TInfo=*/nullptr, |
| 2665 | SC_None, nullptr); |
John McCall | fb44de9 | 2011-05-01 22:35:37 +0000 | [diff] [blame] | 2666 | Param->setScopeInfo(0, Params.size()); |
Douglas Gregor | 450da98 | 2009-02-16 20:58:07 +0000 | [diff] [blame] | 2667 | Param->setImplicit(); |
| 2668 | Params.push_back(Param); |
| 2669 | } |
| 2670 | |
David Blaikie | 4278c65 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 2671 | New->setParams(Params); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2672 | } |
Douglas Gregor | 6871981 | 2009-02-16 18:20:44 +0000 | [diff] [blame] | 2673 | |
Richard Smith | dd9459f | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 2674 | return MergeCompatibleFunctionDecls(New, Old, S, MergeTypeWithOld); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2675 | } |
Chris Lattner | e3995fe | 2007-11-06 06:07:26 +0000 | [diff] [blame] | 2676 | |
Douglas Gregor | c837656 | 2009-03-06 22:43:54 +0000 | [diff] [blame] | 2677 | // GNU C permits a K&R definition to follow a prototype declaration |
| 2678 | // if the declared types of the parameters in the K&R definition |
| 2679 | // match the types in the prototype declaration, even when the |
| 2680 | // promoted types of the parameters from the K&R definition differ |
| 2681 | // from the types in the prototype. GCC then keeps the types from |
| 2682 | // the prototype. |
Eli Friedman | bc4e29f | 2009-06-01 09:24:59 +0000 | [diff] [blame] | 2683 | // |
| 2684 | // If a variadic prototype is followed by a non-variadic K&R definition, |
| 2685 | // the K&R definition becomes variadic. This is sort of an edge case, but |
| 2686 | // it's legal per the standard depending on how you read C99 6.7.5.3p15 and |
| 2687 | // C99 6.9.1p8. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2688 | if (!getLangOpts().CPlusPlus && |
Douglas Gregor | c837656 | 2009-03-06 22:43:54 +0000 | [diff] [blame] | 2689 | Old->hasPrototype() && !New->hasPrototype() && |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2690 | New->getType()->getAs<FunctionProtoType>() && |
Douglas Gregor | c837656 | 2009-03-06 22:43:54 +0000 | [diff] [blame] | 2691 | Old->getNumParams() == New->getNumParams()) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2692 | SmallVector<QualType, 16> ArgTypes; |
| 2693 | SmallVector<GNUCompatibleParamWarning, 16> Warnings; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2694 | const FunctionProtoType *OldProto |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2695 | = Old->getType()->getAs<FunctionProtoType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2696 | const FunctionProtoType *NewProto |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2697 | = New->getType()->getAs<FunctionProtoType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2698 | |
Douglas Gregor | c837656 | 2009-03-06 22:43:54 +0000 | [diff] [blame] | 2699 | // Determine whether this is the GNU C extension. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2700 | QualType MergedReturn = Context.mergeTypes(OldProto->getReturnType(), |
| 2701 | NewProto->getReturnType()); |
Eli Friedman | bc4e29f | 2009-06-01 09:24:59 +0000 | [diff] [blame] | 2702 | bool LooseCompatible = !MergedReturn.isNull(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2703 | for (unsigned Idx = 0, End = Old->getNumParams(); |
Eli Friedman | bc4e29f | 2009-06-01 09:24:59 +0000 | [diff] [blame] | 2704 | LooseCompatible && Idx != End; ++Idx) { |
Douglas Gregor | c837656 | 2009-03-06 22:43:54 +0000 | [diff] [blame] | 2705 | ParmVarDecl *OldParm = Old->getParamDecl(Idx); |
| 2706 | ParmVarDecl *NewParm = New->getParamDecl(Idx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2707 | if (Context.typesAreCompatible(OldParm->getType(), |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2708 | NewProto->getParamType(Idx))) { |
Douglas Gregor | c837656 | 2009-03-06 22:43:54 +0000 | [diff] [blame] | 2709 | ArgTypes.push_back(NewParm->getType()); |
| 2710 | } else if (Context.typesAreCompatible(OldParm->getType(), |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 2711 | NewParm->getType(), |
| 2712 | /*CompareUnqualified=*/true)) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2713 | GNUCompatibleParamWarning Warn = { OldParm, NewParm, |
| 2714 | NewProto->getParamType(Idx) }; |
Douglas Gregor | c837656 | 2009-03-06 22:43:54 +0000 | [diff] [blame] | 2715 | Warnings.push_back(Warn); |
| 2716 | ArgTypes.push_back(NewParm->getType()); |
| 2717 | } else |
Eli Friedman | bc4e29f | 2009-06-01 09:24:59 +0000 | [diff] [blame] | 2718 | LooseCompatible = false; |
Douglas Gregor | c837656 | 2009-03-06 22:43:54 +0000 | [diff] [blame] | 2719 | } |
| 2720 | |
Eli Friedman | bc4e29f | 2009-06-01 09:24:59 +0000 | [diff] [blame] | 2721 | if (LooseCompatible) { |
Douglas Gregor | c837656 | 2009-03-06 22:43:54 +0000 | [diff] [blame] | 2722 | for (unsigned Warn = 0; Warn < Warnings.size(); ++Warn) { |
| 2723 | Diag(Warnings[Warn].NewParm->getLocation(), |
| 2724 | diag::ext_param_promoted_not_compatible_with_prototype) |
| 2725 | << Warnings[Warn].PromotedType |
| 2726 | << Warnings[Warn].OldParm->getType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 2727 | if (Warnings[Warn].OldParm->getLocation().isValid()) |
| 2728 | Diag(Warnings[Warn].OldParm->getLocation(), |
| 2729 | diag::note_previous_declaration); |
Douglas Gregor | c837656 | 2009-03-06 22:43:54 +0000 | [diff] [blame] | 2730 | } |
| 2731 | |
Richard Smith | dd9459f | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 2732 | if (MergeTypeWithOld) |
| 2733 | New->setType(Context.getFunctionType(MergedReturn, ArgTypes, |
| 2734 | OldProto->getExtProtoInfo())); |
| 2735 | return MergeCompatibleFunctionDecls(New, Old, S, MergeTypeWithOld); |
Douglas Gregor | c837656 | 2009-03-06 22:43:54 +0000 | [diff] [blame] | 2736 | } |
| 2737 | |
| 2738 | // Fall through to diagnose conflicting types. |
| 2739 | } |
| 2740 | |
John McCall | 088831d | 2013-04-14 08:50:55 +0000 | [diff] [blame] | 2741 | // A function that has already been declared has been redeclared or |
| 2742 | // defined with a different type; show an appropriate diagnostic. |
| 2743 | |
| 2744 | // If the previous declaration was an implicitly-generated builtin |
| 2745 | // declaration, then at the very least we should use a specialized note. |
| 2746 | unsigned BuiltinID; |
| 2747 | if (Old->isImplicit() && (BuiltinID = Old->getBuiltinID())) { |
| 2748 | // If it's actually a library-defined builtin function like 'malloc' |
| 2749 | // or 'printf', just warn about the incompatible redeclaration. |
Douglas Gregor | cda9c67 | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 2750 | if (Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)) { |
Douglas Gregor | cda9c67 | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 2751 | Diag(New->getLocation(), diag::warn_redecl_library_builtin) << New; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2752 | Diag(OldLocation, diag::note_previous_builtin_declaration) |
Douglas Gregor | cda9c67 | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 2753 | << Old << Old->getType(); |
John McCall | 088831d | 2013-04-14 08:50:55 +0000 | [diff] [blame] | 2754 | |
| 2755 | // If this is a global redeclaration, just forget hereafter |
| 2756 | // about the "builtin-ness" of the function. |
| 2757 | // |
| 2758 | // Doing this for local extern declarations is problematic. If |
| 2759 | // the builtin declaration remains visible, a second invalid |
| 2760 | // local declaration will produce a hard error; if it doesn't |
| 2761 | // remain visible, a single bogus local redeclaration (which is |
| 2762 | // actually only a warning) could break all the downstream code. |
Richard Smith | a41c97a | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 2763 | if (!New->getLexicalDeclContext()->isFunctionOrMethod()) |
John McCall | 088831d | 2013-04-14 08:50:55 +0000 | [diff] [blame] | 2764 | New->getIdentifier()->setBuiltinID(Builtin::NotBuiltin); |
| 2765 | |
Douglas Gregor | 374e156 | 2009-03-23 17:47:24 +0000 | [diff] [blame] | 2766 | return false; |
Douglas Gregor | cda9c67 | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 2767 | } |
Steve Naroff | 837618c | 2008-01-16 15:01:34 +0000 | [diff] [blame] | 2768 | |
Douglas Gregor | cda9c67 | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 2769 | PrevDiag = diag::note_previous_builtin_declaration; |
| 2770 | } |
| 2771 | |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2772 | Diag(New->getLocation(), diag::err_conflicting_types) << New->getDeclName(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2773 | Diag(OldLocation, PrevDiag) << Old << Old->getType(); |
Douglas Gregor | cda9c67 | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 2774 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2775 | } |
| 2776 | |
Douglas Gregor | 04495c8 | 2009-02-24 01:23:02 +0000 | [diff] [blame] | 2777 | /// \brief Completes the merge of two function declarations that are |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2778 | /// known to be compatible. |
Douglas Gregor | 04495c8 | 2009-02-24 01:23:02 +0000 | [diff] [blame] | 2779 | /// |
| 2780 | /// This routine handles the merging of attributes and other |
Alp Toker | 89673e0 | 2013-10-22 09:00:49 +0000 | [diff] [blame] | 2781 | /// properties of function declarations from the old declaration to |
Douglas Gregor | 04495c8 | 2009-02-24 01:23:02 +0000 | [diff] [blame] | 2782 | /// the new declaration, once we know that New is in fact a |
| 2783 | /// redeclaration of Old. |
| 2784 | /// |
| 2785 | /// \returns false |
James Molloy | 9cda03f | 2012-03-13 08:55:35 +0000 | [diff] [blame] | 2786 | bool Sema::MergeCompatibleFunctionDecls(FunctionDecl *New, FunctionDecl *Old, |
Richard Smith | dd9459f | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 2787 | Scope *S, bool MergeTypeWithOld) { |
Douglas Gregor | 04495c8 | 2009-02-24 01:23:02 +0000 | [diff] [blame] | 2788 | // Merge the attributes |
Douglas Gregor | 27c6da2 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 2789 | mergeDeclAttributes(New, Old); |
Douglas Gregor | 04495c8 | 2009-02-24 01:23:02 +0000 | [diff] [blame] | 2790 | |
Douglas Gregor | 04495c8 | 2009-02-24 01:23:02 +0000 | [diff] [blame] | 2791 | // Merge "pure" flag. |
| 2792 | if (Old->isPure()) |
| 2793 | New->setPure(); |
| 2794 | |
Rafael Espindola | 8dbf697 | 2012-11-25 14:07:59 +0000 | [diff] [blame] | 2795 | // Merge "used" flag. |
Rafael Espindola | e7bd89a | 2013-10-23 16:46:34 +0000 | [diff] [blame] | 2796 | if (Old->getMostRecentDecl()->isUsed(false)) |
| 2797 | New->setIsUsed(); |
Rafael Espindola | 8dbf697 | 2012-11-25 14:07:59 +0000 | [diff] [blame] | 2798 | |
John McCall | eca5d22 | 2011-03-02 04:00:57 +0000 | [diff] [blame] | 2799 | // Merge attributes from the parameters. These can mismatch with K&R |
| 2800 | // declarations. |
| 2801 | if (New->getNumParams() == Old->getNumParams()) |
| 2802 | for (unsigned i = 0, e = New->getNumParams(); i != e; ++i) |
| 2803 | mergeParamDeclAttributes(New->getParamDecl(i), Old->getParamDecl(i), |
Richard Smith | 3a2b7a1 | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 2804 | *this); |
John McCall | eca5d22 | 2011-03-02 04:00:57 +0000 | [diff] [blame] | 2805 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2806 | if (getLangOpts().CPlusPlus) |
James Molloy | 9cda03f | 2012-03-13 08:55:35 +0000 | [diff] [blame] | 2807 | return MergeCXXFunctionDecl(New, Old, S); |
Douglas Gregor | 04495c8 | 2009-02-24 01:23:02 +0000 | [diff] [blame] | 2808 | |
Rafael Espindola | 8b8a09e | 2012-11-29 16:09:03 +0000 | [diff] [blame] | 2809 | // Merge the function types so the we get the composite types for the return |
Richard Smith | dd9459f | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 2810 | // and argument types. Per C11 6.2.7/4, only update the type if the old decl |
| 2811 | // was visible. |
Rafael Espindola | 8b8a09e | 2012-11-29 16:09:03 +0000 | [diff] [blame] | 2812 | QualType Merged = Context.mergeTypes(Old->getType(), New->getType()); |
Richard Smith | dd9459f | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 2813 | if (!Merged.isNull() && MergeTypeWithOld) |
Rafael Espindola | 8b8a09e | 2012-11-29 16:09:03 +0000 | [diff] [blame] | 2814 | New->setType(Merged); |
| 2815 | |
Douglas Gregor | 04495c8 | 2009-02-24 01:23:02 +0000 | [diff] [blame] | 2816 | return false; |
| 2817 | } |
| 2818 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2819 | |
John McCall | eca5d22 | 2011-03-02 04:00:57 +0000 | [diff] [blame] | 2820 | void Sema::mergeObjCMethodDecls(ObjCMethodDecl *newMethod, |
Douglas Gregor | 27c6da2 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 2821 | ObjCMethodDecl *oldMethod) { |
John McCall | 6c2c250 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 2822 | |
Fariborz Jahanian | 1ea6744 | 2012-06-05 21:14:46 +0000 | [diff] [blame] | 2823 | // Merge the attributes, including deprecated/unavailable |
Ted Kremenek | cb34439 | 2013-04-06 00:34:27 +0000 | [diff] [blame] | 2824 | AvailabilityMergeKind MergeKind = |
| 2825 | isa<ObjCImplDecl>(newMethod->getDeclContext()) ? AMK_Redeclaration |
| 2826 | : AMK_Override; |
| 2827 | mergeDeclAttributes(newMethod, oldMethod, MergeKind); |
John McCall | eca5d22 | 2011-03-02 04:00:57 +0000 | [diff] [blame] | 2828 | |
| 2829 | // Merge attributes from the parameters. |
Douglas Gregor | 0a4a23a | 2012-05-17 23:13:29 +0000 | [diff] [blame] | 2830 | ObjCMethodDecl::param_const_iterator oi = oldMethod->param_begin(), |
| 2831 | oe = oldMethod->param_end(); |
Argyrios Kyrtzidis | 491306a | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 2832 | for (ObjCMethodDecl::param_iterator |
John McCall | eca5d22 | 2011-03-02 04:00:57 +0000 | [diff] [blame] | 2833 | ni = newMethod->param_begin(), ne = newMethod->param_end(); |
Douglas Gregor | 0a4a23a | 2012-05-17 23:13:29 +0000 | [diff] [blame] | 2834 | ni != ne && oi != oe; ++ni, ++oi) |
Richard Smith | 3a2b7a1 | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 2835 | mergeParamDeclAttributes(*ni, *oi, *this); |
John McCall | 6c2c250 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 2836 | |
Douglas Gregor | f4d918f | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2837 | CheckObjCMethodOverride(newMethod, oldMethod); |
John McCall | eca5d22 | 2011-03-02 04:00:57 +0000 | [diff] [blame] | 2838 | } |
| 2839 | |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 2840 | /// MergeVarDeclTypes - We parsed a variable 'New' which has the same name and |
| 2841 | /// scope as a previous declaration 'Old'. Figure out how to merge their types, |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 2842 | /// emitting diagnostics as appropriate. |
| 2843 | /// |
| 2844 | /// Declarations using the auto type specifier (C++ [decl.spec.auto]) call back |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2845 | /// to here in AddInitializerToDecl. We can't check them before the initializer |
| 2846 | /// is attached. |
Richard Smith | dd9459f | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 2847 | void Sema::MergeVarDeclTypes(VarDecl *New, VarDecl *Old, |
| 2848 | bool MergeTypeWithOld) { |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 2849 | if (New->isInvalidDecl() || Old->isInvalidDecl()) |
| 2850 | return; |
| 2851 | |
| 2852 | QualType MergedT; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2853 | if (getLangOpts().CPlusPlus) { |
Richard Smith | dc7a4f5 | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 2854 | if (New->getType()->isUndeducedType()) { |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 2855 | // We don't know what the new type is until the initializer is attached. |
| 2856 | return; |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 2857 | } else if (Context.hasSameType(New->getType(), Old->getType())) { |
| 2858 | // These could still be something that needs exception specs checked. |
| 2859 | return MergeVarDeclExceptionSpecs(New, Old); |
| 2860 | } |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 2861 | // C++ [basic.link]p10: |
| 2862 | // [...] the types specified by all declarations referring to a given |
| 2863 | // object or function shall be identical, except that declarations for an |
| 2864 | // array object can specify array types that differ by the presence or |
| 2865 | // absence of a major array bound (8.3.4). |
| 2866 | else if (Old->getType()->isIncompleteArrayType() && |
| 2867 | New->getType()->isArrayType()) { |
Eli Friedman | 6febf12 | 2012-12-13 01:43:21 +0000 | [diff] [blame] | 2868 | const ArrayType *OldArray = Context.getAsArrayType(Old->getType()); |
| 2869 | const ArrayType *NewArray = Context.getAsArrayType(New->getType()); |
| 2870 | if (Context.hasSameType(OldArray->getElementType(), |
| 2871 | NewArray->getElementType())) |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 2872 | MergedT = New->getType(); |
| 2873 | } else if (Old->getType()->isArrayType() && |
Richard Smith | a41c97a | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 2874 | New->getType()->isIncompleteArrayType()) { |
Eli Friedman | 6febf12 | 2012-12-13 01:43:21 +0000 | [diff] [blame] | 2875 | const ArrayType *OldArray = Context.getAsArrayType(Old->getType()); |
| 2876 | const ArrayType *NewArray = Context.getAsArrayType(New->getType()); |
| 2877 | if (Context.hasSameType(OldArray->getElementType(), |
| 2878 | NewArray->getElementType())) |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 2879 | MergedT = Old->getType(); |
Richard Smith | a41c97a | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 2880 | } else if (New->getType()->isObjCObjectPointerType() && |
| 2881 | Old->getType()->isObjCObjectPointerType()) { |
| 2882 | MergedT = Context.mergeObjCGCQualifiers(New->getType(), |
| 2883 | Old->getType()); |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 2884 | } |
| 2885 | } else { |
Richard Smith | a41c97a | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 2886 | // C 6.2.7p2: |
| 2887 | // All declarations that refer to the same object or function shall have |
| 2888 | // compatible type. |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 2889 | MergedT = Context.mergeTypes(New->getType(), Old->getType()); |
| 2890 | } |
| 2891 | if (MergedT.isNull()) { |
Richard Smith | dd9459f | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 2892 | // It's OK if we couldn't merge types if either type is dependent, for a |
| 2893 | // block-scope variable. In other cases (static data members of class |
| 2894 | // templates, variable templates, ...), we require the types to be |
| 2895 | // equivalent. |
| 2896 | // FIXME: The C++ standard doesn't say anything about this. |
| 2897 | if ((New->getType()->isDependentType() || |
| 2898 | Old->getType()->isDependentType()) && New->isLocalVarDecl()) { |
| 2899 | // If the old type was dependent, we can't merge with it, so the new type |
| 2900 | // becomes dependent for now. We'll reproduce the original type when we |
| 2901 | // instantiate the TypeSourceInfo for the variable. |
| 2902 | if (!New->getType()->isDependentType() && MergeTypeWithOld) |
| 2903 | New->setType(Context.DependentTy); |
| 2904 | return; |
| 2905 | } |
| 2906 | |
| 2907 | // FIXME: Even if this merging succeeds, some other non-visible declaration |
| 2908 | // of this variable might have an incompatible type. For instance: |
| 2909 | // |
| 2910 | // extern int arr[]; |
| 2911 | // void f() { extern int arr[2]; } |
| 2912 | // void g() { extern int arr[3]; } |
| 2913 | // |
| 2914 | // Neither C nor C++ requires a diagnostic for this, but we should still try |
| 2915 | // to diagnose it. |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 2916 | Diag(New->getLocation(), diag::err_redefinition_different_type) |
David Blaikie | a405b25 | 2012-09-20 18:38:57 +0000 | [diff] [blame] | 2917 | << New->getDeclName() << New->getType() << Old->getType(); |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 2918 | Diag(Old->getLocation(), diag::note_previous_definition); |
| 2919 | return New->setInvalidDecl(); |
| 2920 | } |
John McCall | 5b8740f | 2013-04-01 18:34:28 +0000 | [diff] [blame] | 2921 | |
| 2922 | // Don't actually update the type on the new declaration if the old |
Richard Smith | 99a7238 | 2013-09-03 21:00:58 +0000 | [diff] [blame] | 2923 | // declaration was an extern declaration in a different scope. |
Richard Smith | dd9459f | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 2924 | if (MergeTypeWithOld) |
John McCall | 5b8740f | 2013-04-01 18:34:28 +0000 | [diff] [blame] | 2925 | New->setType(MergedT); |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 2926 | } |
| 2927 | |
Richard Smith | 99a7238 | 2013-09-03 21:00:58 +0000 | [diff] [blame] | 2928 | static bool mergeTypeWithPrevious(Sema &S, VarDecl *NewVD, VarDecl *OldVD, |
| 2929 | LookupResult &Previous) { |
| 2930 | // C11 6.2.7p4: |
| 2931 | // For an identifier with internal or external linkage declared |
| 2932 | // in a scope in which a prior declaration of that identifier is |
| 2933 | // visible, if the prior declaration specifies internal or |
| 2934 | // external linkage, the type of the identifier at the later |
| 2935 | // declaration becomes the composite type. |
| 2936 | // |
| 2937 | // If the variable isn't visible, we do not merge with its type. |
| 2938 | if (Previous.isShadowed()) |
| 2939 | return false; |
| 2940 | |
| 2941 | if (S.getLangOpts().CPlusPlus) { |
| 2942 | // C++11 [dcl.array]p3: |
| 2943 | // If there is a preceding declaration of the entity in the same |
| 2944 | // scope in which the bound was specified, an omitted array bound |
| 2945 | // is taken to be the same as in that earlier declaration. |
| 2946 | return NewVD->isPreviousDeclInSameBlockScope() || |
| 2947 | (!OldVD->getLexicalDeclContext()->isFunctionOrMethod() && |
| 2948 | !NewVD->getLexicalDeclContext()->isFunctionOrMethod()); |
| 2949 | } else { |
| 2950 | // If the old declaration was function-local, don't merge with its |
| 2951 | // type unless we're in the same function. |
| 2952 | return !OldVD->getLexicalDeclContext()->isFunctionOrMethod() || |
| 2953 | OldVD->getLexicalDeclContext() == NewVD->getLexicalDeclContext(); |
| 2954 | } |
| 2955 | } |
| 2956 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2957 | /// MergeVarDecl - We just parsed a variable 'New' which has the same name |
| 2958 | /// and scope as a previous declaration 'Old'. Figure out how to resolve this |
| 2959 | /// situation, merging decls or emitting diagnostics as appropriate. |
| 2960 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2961 | /// Tentative definition rules (C99 6.9.2p2) are checked by |
| 2962 | /// FinalizeDeclaratorGroup. Unfortunately, we can't analyze tentative |
Steve Naroff | ff9eb1f | 2008-08-08 17:50:35 +0000 | [diff] [blame] | 2963 | /// definitions here, since the initializer hasn't been attached. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2964 | /// |
Richard Smith | 99a7238 | 2013-09-03 21:00:58 +0000 | [diff] [blame] | 2965 | void Sema::MergeVarDecl(VarDecl *New, LookupResult &Previous) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 2966 | // If the new decl is already invalid, don't do any other checking. |
| 2967 | if (New->isInvalidDecl()) |
| 2968 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2969 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2970 | VarTemplateDecl *NewTemplate = New->getDescribedVarTemplate(); |
| 2971 | |
Larisse Voufo | 4a91989 | 2013-08-14 03:09:19 +0000 | [diff] [blame] | 2972 | // Verify the old decl was also a variable or variable template. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 2973 | VarDecl *Old = nullptr; |
| 2974 | VarTemplateDecl *OldTemplate = nullptr; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2975 | if (Previous.isSingleResult()) { |
| 2976 | if (NewTemplate) { |
| 2977 | OldTemplate = dyn_cast<VarTemplateDecl>(Previous.getFoundDecl()); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 2978 | Old = OldTemplate ? OldTemplate->getTemplatedDecl() : nullptr; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2979 | } else |
| 2980 | Old = dyn_cast<VarDecl>(Previous.getFoundDecl()); |
Larisse Voufo | 4a91989 | 2013-08-14 03:09:19 +0000 | [diff] [blame] | 2981 | } |
| 2982 | if (!Old) { |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 2983 | Diag(New->getLocation(), diag::err_redefinition_different_kind) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 2984 | << New->getDeclName(); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 2985 | Diag(Previous.getRepresentativeDecl()->getLocation(), |
| 2986 | diag::note_previous_definition); |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 2987 | return New->setInvalidDecl(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2988 | } |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 2989 | |
Rafael Espindola | 90cc390 | 2013-04-15 12:49:13 +0000 | [diff] [blame] | 2990 | if (!shouldLinkPossiblyHiddenDecl(Old, New)) |
| 2991 | return; |
| 2992 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2993 | // Ensure the template parameters are compatible. |
| 2994 | if (NewTemplate && |
| 2995 | !TemplateParameterListsAreEqual(NewTemplate->getTemplateParameters(), |
| 2996 | OldTemplate->getTemplateParameters(), |
| 2997 | /*Complain=*/true, TPL_TemplateMatch)) |
| 2998 | return; |
| 2999 | |
Douglas Gregor | 7f6ff02 | 2010-08-30 14:32:14 +0000 | [diff] [blame] | 3000 | // C++ [class.mem]p1: |
| 3001 | // A member shall not be declared twice in the member-specification [...] |
| 3002 | // |
| 3003 | // Here, we need only consider static data members. |
| 3004 | if (Old->isStaticDataMember() && !New->isOutOfLine()) { |
| 3005 | Diag(New->getLocation(), diag::err_duplicate_member) |
| 3006 | << New->getIdentifier(); |
| 3007 | Diag(Old->getLocation(), diag::note_previous_declaration); |
| 3008 | New->setInvalidDecl(); |
| 3009 | } |
| 3010 | |
Douglas Gregor | 27c6da2 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 3011 | mergeDeclAttributes(New, Old); |
David Blaikie | d662a79 | 2011-10-19 22:56:21 +0000 | [diff] [blame] | 3012 | // Warn if an already-declared variable is made a weak_import in a subsequent |
| 3013 | // declaration |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3014 | if (New->hasAttr<WeakImportAttr>() && |
Fariborz Jahanian | ab27d6e | 2011-06-20 17:50:03 +0000 | [diff] [blame] | 3015 | Old->getStorageClass() == SC_None && |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3016 | !Old->hasAttr<WeakImportAttr>()) { |
Fariborz Jahanian | d543130 | 2011-06-22 22:08:50 +0000 | [diff] [blame] | 3017 | Diag(New->getLocation(), diag::warn_weak_import) << New->getDeclName(); |
| 3018 | Diag(Old->getLocation(), diag::note_previous_definition); |
| 3019 | // Remove weak_import attribute on new declaration. |
Fariborz Jahanian | c3ca14d | 2011-06-23 17:50:10 +0000 | [diff] [blame] | 3020 | New->dropAttr<WeakImportAttr>(); |
Fariborz Jahanian | d543130 | 2011-06-22 22:08:50 +0000 | [diff] [blame] | 3021 | } |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 3022 | |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3023 | // Merge the types. |
Richard Smith | 99a7238 | 2013-09-03 21:00:58 +0000 | [diff] [blame] | 3024 | MergeVarDeclTypes(New, Old, mergeTypeWithPrevious(*this, New, Old, Previous)); |
| 3025 | |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3026 | if (New->isInvalidDecl()) |
| 3027 | return; |
Douglas Gregor | 656de63 | 2009-03-11 23:52:16 +0000 | [diff] [blame] | 3028 | |
Rafael Espindola | ea4b111 | 2013-04-04 21:21:25 +0000 | [diff] [blame] | 3029 | // [dcl.stc]p8: Check if we have a non-static decl followed by a static. |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 3030 | if (New->getStorageClass() == SC_Static && |
Rafael Espindola | ea4b111 | 2013-04-04 21:21:25 +0000 | [diff] [blame] | 3031 | !New->isStaticDataMember() && |
Rafael Espindola | 181e3ec | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 3032 | Old->hasExternalFormalLinkage()) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3033 | Diag(New->getLocation(), diag::err_static_non_static) << New->getDeclName(); |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 3034 | Diag(Old->getLocation(), diag::note_previous_definition); |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 3035 | return New->setInvalidDecl(); |
Steve Naroff | b7b032e | 2008-01-30 00:44:01 +0000 | [diff] [blame] | 3036 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3037 | // C99 6.2.2p4: |
Douglas Gregor | 5ef122e | 2009-03-19 22:01:50 +0000 | [diff] [blame] | 3038 | // For an identifier declared with the storage-class specifier |
| 3039 | // extern in a scope in which a prior declaration of that |
| 3040 | // identifier is visible,23) if the prior declaration specifies |
| 3041 | // internal or external linkage, the linkage of the identifier at |
| 3042 | // the later declaration is the same as the linkage specified at |
| 3043 | // the prior declaration. If no prior declaration is visible, or |
| 3044 | // if the prior declaration specifies no linkage, then the |
| 3045 | // identifier has external linkage. |
Douglas Gregor | 38179b2 | 2009-03-23 16:17:01 +0000 | [diff] [blame] | 3046 | if (New->hasExternalStorage() && Old->hasLinkage()) |
Douglas Gregor | 5ef122e | 2009-03-19 22:01:50 +0000 | [diff] [blame] | 3047 | /* Okay */; |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 3048 | else if (New->getCanonicalDecl()->getStorageClass() != SC_Static && |
Rafael Espindola | ea4b111 | 2013-04-04 21:21:25 +0000 | [diff] [blame] | 3049 | !New->isStaticDataMember() && |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 3050 | Old->getCanonicalDecl()->getStorageClass() == SC_Static) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3051 | Diag(New->getLocation(), diag::err_non_static_static) << New->getDeclName(); |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 3052 | Diag(Old->getLocation(), diag::note_previous_definition); |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 3053 | return New->setInvalidDecl(); |
Steve Naroff | b7b032e | 2008-01-30 00:44:01 +0000 | [diff] [blame] | 3054 | } |
Daniel Dunbar | 5466c7b | 2009-04-14 02:25:56 +0000 | [diff] [blame] | 3055 | |
Argyrios Kyrtzidis | 6684d85 | 2011-01-31 07:04:46 +0000 | [diff] [blame] | 3056 | // Check if extern is followed by non-extern and vice-versa. |
| 3057 | if (New->hasExternalStorage() && |
| 3058 | !Old->hasLinkage() && Old->isLocalVarDecl()) { |
| 3059 | Diag(New->getLocation(), diag::err_extern_non_extern) << New->getDeclName(); |
| 3060 | Diag(Old->getLocation(), diag::note_previous_definition); |
| 3061 | return New->setInvalidDecl(); |
| 3062 | } |
Rafael Espindola | 80a8689 | 2013-04-04 02:47:57 +0000 | [diff] [blame] | 3063 | if (Old->hasLinkage() && New->isLocalVarDecl() && |
| 3064 | !New->hasExternalStorage()) { |
Argyrios Kyrtzidis | 6684d85 | 2011-01-31 07:04:46 +0000 | [diff] [blame] | 3065 | Diag(New->getLocation(), diag::err_non_extern_extern) << New->getDeclName(); |
| 3066 | Diag(Old->getLocation(), diag::note_previous_definition); |
| 3067 | return New->setInvalidDecl(); |
| 3068 | } |
| 3069 | |
Steve Naroff | 094cefb | 2008-09-17 14:05:40 +0000 | [diff] [blame] | 3070 | // Variables with external linkage are analyzed in FinalizeDeclaratorGroup. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3071 | |
Daniel Dunbar | 5466c7b | 2009-04-14 02:25:56 +0000 | [diff] [blame] | 3072 | // FIXME: The test for external storage here seems wrong? We still |
| 3073 | // need to check for mismatches. |
| 3074 | if (!New->hasExternalStorage() && !New->isFileVarDecl() && |
Douglas Gregor | 656de63 | 2009-03-11 23:52:16 +0000 | [diff] [blame] | 3075 | // Don't complain about out-of-line definitions of static members. |
| 3076 | !(Old->getLexicalDeclContext()->isRecord() && |
| 3077 | !New->getLexicalDeclContext()->isRecord())) { |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 3078 | Diag(New->getLocation(), diag::err_redefinition) << New->getDeclName(); |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 3079 | Diag(Old->getLocation(), diag::note_previous_definition); |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 3080 | return New->setInvalidDecl(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3081 | } |
Douglas Gregor | 275a369 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 3082 | |
Richard Smith | 38afbc7 | 2013-04-13 02:43:54 +0000 | [diff] [blame] | 3083 | if (New->getTLSKind() != Old->getTLSKind()) { |
| 3084 | if (!Old->getTLSKind()) { |
| 3085 | Diag(New->getLocation(), diag::err_thread_non_thread) << New->getDeclName(); |
| 3086 | Diag(Old->getLocation(), diag::note_previous_declaration); |
| 3087 | } else if (!New->getTLSKind()) { |
| 3088 | Diag(New->getLocation(), diag::err_non_thread_thread) << New->getDeclName(); |
| 3089 | Diag(Old->getLocation(), diag::note_previous_declaration); |
| 3090 | } else { |
| 3091 | // Do not allow redeclaration to change the variable between requiring |
| 3092 | // static and dynamic initialization. |
| 3093 | // FIXME: GCC allows this, but uses the TLS keyword on the first |
| 3094 | // declaration to determine the kind. Do we need to be compatible here? |
| 3095 | Diag(New->getLocation(), diag::err_thread_thread_different_kind) |
| 3096 | << New->getDeclName() << (New->getTLSKind() == VarDecl::TLS_Dynamic); |
| 3097 | Diag(Old->getLocation(), diag::note_previous_declaration); |
| 3098 | } |
Eli Friedman | 63054b3 | 2009-04-19 20:27:55 +0000 | [diff] [blame] | 3099 | } |
| 3100 | |
Sebastian Redl | 4cae1b3 | 2010-02-02 18:35:11 +0000 | [diff] [blame] | 3101 | // C++ doesn't have tentative definitions, so go right ahead and check here. |
| 3102 | const VarDecl *Def; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3103 | if (getLangOpts().CPlusPlus && |
Sebastian Redl | 6c048a9 | 2010-02-03 02:08:48 +0000 | [diff] [blame] | 3104 | New->isThisDeclarationADefinition() == VarDecl::Definition && |
Sebastian Redl | 4cae1b3 | 2010-02-02 18:35:11 +0000 | [diff] [blame] | 3105 | (Def = Old->getDefinition())) { |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3106 | Diag(New->getLocation(), diag::err_redefinition) << New; |
Sebastian Redl | 4cae1b3 | 2010-02-02 18:35:11 +0000 | [diff] [blame] | 3107 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 3108 | New->setInvalidDecl(); |
| 3109 | return; |
| 3110 | } |
Rafael Espindola | e57e3d3 | 2012-12-27 03:56:20 +0000 | [diff] [blame] | 3111 | |
Rafael Espindola | 950fee2 | 2013-02-14 01:18:37 +0000 | [diff] [blame] | 3112 | if (haveIncompatibleLanguageLinkages(Old, New)) { |
Rafael Espindola | e57e3d3 | 2012-12-27 03:56:20 +0000 | [diff] [blame] | 3113 | Diag(New->getLocation(), diag::err_different_language_linkage) << New; |
| 3114 | Diag(Old->getLocation(), diag::note_previous_definition); |
| 3115 | New->setInvalidDecl(); |
| 3116 | return; |
| 3117 | } |
| 3118 | |
Rafael Espindola | 8dbf697 | 2012-11-25 14:07:59 +0000 | [diff] [blame] | 3119 | // Merge "used" flag. |
Rafael Espindola | e7bd89a | 2013-10-23 16:46:34 +0000 | [diff] [blame] | 3120 | if (Old->getMostRecentDecl()->isUsed(false)) |
| 3121 | New->setIsUsed(); |
Rafael Espindola | 8dbf697 | 2012-11-25 14:07:59 +0000 | [diff] [blame] | 3122 | |
Douglas Gregor | 275a369 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 3123 | // Keep a chain of previous declarations. |
Rafael Espindola | bc65091 | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 3124 | New->setPreviousDecl(Old); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3125 | if (NewTemplate) |
| 3126 | NewTemplate->setPreviousDecl(OldTemplate); |
John McCall | 46460a6 | 2010-01-20 21:53:11 +0000 | [diff] [blame] | 3127 | |
| 3128 | // Inherit access appropriately. |
| 3129 | New->setAccess(Old->getAccess()); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3130 | if (NewTemplate) |
| 3131 | NewTemplate->setAccess(New->getAccess()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3132 | } |
| 3133 | |
| 3134 | /// ParsedFreeStandingDeclSpec - This method is invoked when a declspec with |
| 3135 | /// no declarator (e.g. "struct foo;") is parsed. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3136 | Decl *Sema::ParsedFreeStandingDeclSpec(Scope *S, AccessSpecifier AS, |
John McCall | ac4df24 | 2011-03-22 23:00:04 +0000 | [diff] [blame] | 3137 | DeclSpec &DS) { |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 3138 | return ParsedFreeStandingDeclSpec(S, AS, DS, MultiTemplateParamsArg()); |
Chandler Carruth | 0f4be74 | 2011-05-03 18:35:10 +0000 | [diff] [blame] | 3139 | } |
| 3140 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3141 | static void HandleTagNumbering(Sema &S, const TagDecl *Tag, Scope *TagScope) { |
Reid Kleckner | 942f9fe | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 3142 | if (!S.Context.getLangOpts().CPlusPlus) |
| 3143 | return; |
| 3144 | |
Eli Friedman | 5e867c8 | 2013-07-10 00:30:46 +0000 | [diff] [blame] | 3145 | if (isa<CXXRecordDecl>(Tag->getParent())) { |
| 3146 | // If this tag is the direct child of a class, number it if |
| 3147 | // it is anonymous. |
| 3148 | if (!Tag->getName().empty() || Tag->getTypedefNameForAnonDecl()) |
| 3149 | return; |
| 3150 | MangleNumberingContext &MCtx = |
| 3151 | S.Context.getManglingNumberContext(Tag->getParent()); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3152 | S.Context.setManglingNumber( |
| 3153 | Tag, MCtx.getManglingNumber(Tag, TagScope->getMSLocalManglingNumber())); |
Eli Friedman | 5e867c8 | 2013-07-10 00:30:46 +0000 | [diff] [blame] | 3154 | return; |
| 3155 | } |
| 3156 | |
| 3157 | // If this tag isn't a direct child of a class, number it if it is local. |
| 3158 | Decl *ManglingContextDecl; |
| 3159 | if (MangleNumberingContext *MCtx = |
| 3160 | S.getCurrentMangleNumberContext(Tag->getDeclContext(), |
| 3161 | ManglingContextDecl)) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3162 | S.Context.setManglingNumber( |
| 3163 | Tag, |
| 3164 | MCtx->getManglingNumber(Tag, TagScope->getMSLocalManglingNumber())); |
Eli Friedman | 5e867c8 | 2013-07-10 00:30:46 +0000 | [diff] [blame] | 3165 | } |
| 3166 | } |
| 3167 | |
Chandler Carruth | 0f4be74 | 2011-05-03 18:35:10 +0000 | [diff] [blame] | 3168 | /// ParsedFreeStandingDeclSpec - This method is invoked when a declspec with |
Richard Smith | c7f8116 | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 3169 | /// no declarator (e.g. "struct foo;") is parsed. It also accepts template |
Chandler Carruth | 0f4be74 | 2011-05-03 18:35:10 +0000 | [diff] [blame] | 3170 | /// parameters to cope with template friend declarations. |
| 3171 | Decl *Sema::ParsedFreeStandingDeclSpec(Scope *S, AccessSpecifier AS, |
| 3172 | DeclSpec &DS, |
Richard Smith | c7f8116 | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 3173 | MultiTemplateParamsArg TemplateParams, |
| 3174 | bool IsExplicitInstantiation) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 3175 | Decl *TagD = nullptr; |
| 3176 | TagDecl *Tag = nullptr; |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 3177 | if (DS.getTypeSpecType() == DeclSpec::TST_class || |
| 3178 | DS.getTypeSpecType() == DeclSpec::TST_struct || |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 3179 | DS.getTypeSpecType() == DeclSpec::TST_interface || |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 3180 | DS.getTypeSpecType() == DeclSpec::TST_union || |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 3181 | DS.getTypeSpecType() == DeclSpec::TST_enum) { |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3182 | TagD = DS.getRepAsDecl(); |
John McCall | e3af023 | 2009-10-07 23:34:25 +0000 | [diff] [blame] | 3183 | |
| 3184 | if (!TagD) // We probably had an error |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 3185 | return nullptr; |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 3186 | |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 3187 | // Note that the above type specs guarantee that the |
| 3188 | // type rep is a Decl, whereas in many of the others |
| 3189 | // it's a Type. |
Peter Collingbourne | 0661bd0c | 2011-10-23 17:07:16 +0000 | [diff] [blame] | 3190 | if (isa<TagDecl>(TagD)) |
| 3191 | Tag = cast<TagDecl>(TagD); |
| 3192 | else if (ClassTemplateDecl *CTD = dyn_cast<ClassTemplateDecl>(TagD)) |
| 3193 | Tag = CTD->getTemplatedDecl(); |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 3194 | } |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 3195 | |
Argyrios Kyrtzidis | 216f78b | 2012-03-09 20:10:30 +0000 | [diff] [blame] | 3196 | if (Tag) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3197 | HandleTagNumbering(*this, Tag, S); |
Argyrios Kyrtzidis | 717a20b | 2011-09-30 22:11:31 +0000 | [diff] [blame] | 3198 | Tag->setFreeStanding(); |
Argyrios Kyrtzidis | 216f78b | 2012-03-09 20:10:30 +0000 | [diff] [blame] | 3199 | if (Tag->isInvalidDecl()) |
| 3200 | return Tag; |
| 3201 | } |
Argyrios Kyrtzidis | 717a20b | 2011-09-30 22:11:31 +0000 | [diff] [blame] | 3202 | |
Nuno Lopes | 0a8bab0 | 2009-12-17 11:35:26 +0000 | [diff] [blame] | 3203 | if (unsigned TypeQuals = DS.getTypeQualifiers()) { |
| 3204 | // Enforce C99 6.7.3p2: "Types other than pointer types derived from object |
| 3205 | // or incomplete types shall not be restrict-qualified." |
| 3206 | if (TypeQuals & DeclSpec::TQ_restrict) |
| 3207 | Diag(DS.getRestrictSpecLoc(), |
| 3208 | diag::err_typecheck_invalid_restrict_not_pointer_noarg) |
| 3209 | << DS.getSourceRange(); |
| 3210 | } |
| 3211 | |
Richard Smith | af1fc7a | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 3212 | if (DS.isConstexprSpecified()) { |
| 3213 | // C++0x [dcl.constexpr]p1: constexpr can only be applied to declarations |
| 3214 | // and definitions of functions and variables. |
| 3215 | if (Tag) |
| 3216 | Diag(DS.getConstexprSpecLoc(), diag::err_constexpr_tag) |
| 3217 | << (DS.getTypeSpecType() == DeclSpec::TST_class ? 0 : |
| 3218 | DS.getTypeSpecType() == DeclSpec::TST_struct ? 1 : |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 3219 | DS.getTypeSpecType() == DeclSpec::TST_interface ? 2 : |
| 3220 | DS.getTypeSpecType() == DeclSpec::TST_union ? 3 : 4); |
Richard Smith | af1fc7a | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 3221 | else |
| 3222 | Diag(DS.getConstexprSpecLoc(), diag::err_constexpr_no_declarators); |
| 3223 | // Don't emit warnings after this error. |
| 3224 | return TagD; |
| 3225 | } |
| 3226 | |
Richard Smith | c7f8116 | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 3227 | DiagnoseFunctionSpecifiers(DS); |
| 3228 | |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 3229 | if (DS.isFriendSpecified()) { |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 3230 | // If we're dealing with a decl but not a TagDecl, assume that |
| 3231 | // whatever routines created it handled the friendship aspect. |
| 3232 | if (TagD && !Tag) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 3233 | return nullptr; |
Chandler Carruth | 0f4be74 | 2011-05-03 18:35:10 +0000 | [diff] [blame] | 3234 | return ActOnFriendTypeDecl(S, DS, TemplateParams); |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 3235 | } |
John McCall | ac4df24 | 2011-03-22 23:00:04 +0000 | [diff] [blame] | 3236 | |
Richard Smith | c7f8116 | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 3237 | CXXScopeSpec &SS = DS.getTypeSpecScope(); |
| 3238 | bool IsExplicitSpecialization = |
| 3239 | !TemplateParams.empty() && TemplateParams.back()->size() == 0; |
| 3240 | if (Tag && SS.isNotEmpty() && !Tag->isCompleteDefinition() && |
| 3241 | !IsExplicitInstantiation && !IsExplicitSpecialization) { |
| 3242 | // Per C++ [dcl.type.elab]p1, a class declaration cannot have a |
| 3243 | // nested-name-specifier unless it is an explicit instantiation |
| 3244 | // or an explicit specialization. |
| 3245 | // Per C++ [dcl.enum]p1, an opaque-enum-declaration can't either. |
| 3246 | Diag(SS.getBeginLoc(), diag::err_standalone_class_nested_name_specifier) |
| 3247 | << (DS.getTypeSpecType() == DeclSpec::TST_class ? 0 : |
| 3248 | DS.getTypeSpecType() == DeclSpec::TST_struct ? 1 : |
| 3249 | DS.getTypeSpecType() == DeclSpec::TST_interface ? 2 : |
| 3250 | DS.getTypeSpecType() == DeclSpec::TST_union ? 3 : 4) |
| 3251 | << SS.getRange(); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 3252 | return nullptr; |
Richard Smith | c7f8116 | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 3253 | } |
| 3254 | |
| 3255 | // Track whether this decl-specifier declares anything. |
| 3256 | bool DeclaresAnything = true; |
| 3257 | |
| 3258 | // Handle anonymous struct definitions. |
Douglas Gregor | 4920f1f | 2009-01-12 22:49:06 +0000 | [diff] [blame] | 3259 | if (RecordDecl *Record = dyn_cast_or_null<RecordDecl>(Tag)) { |
John McCall | 5e1cdac | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 3260 | if (!Record->getDeclName() && Record->isCompleteDefinition() && |
Douglas Gregor | a71c129 | 2009-03-06 23:06:59 +0000 | [diff] [blame] | 3261 | DS.getStorageClassSpec() != DeclSpec::SCS_typedef) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3262 | if (getLangOpts().CPlusPlus || |
Douglas Gregor | a71c129 | 2009-03-06 23:06:59 +0000 | [diff] [blame] | 3263 | Record->getDeclContext()->isRecord()) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3264 | return BuildAnonymousStructOrUnion(S, DS, AS, Record, Context.getPrintingPolicy()); |
Douglas Gregor | a71c129 | 2009-03-06 23:06:59 +0000 | [diff] [blame] | 3265 | |
Richard Smith | c7f8116 | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 3266 | DeclaresAnything = false; |
Douglas Gregor | a71c129 | 2009-03-06 23:06:59 +0000 | [diff] [blame] | 3267 | } |
Francois Pichet | 8e161ed | 2010-11-23 06:07:27 +0000 | [diff] [blame] | 3268 | } |
Douglas Gregor | 4920f1f | 2009-01-12 22:49:06 +0000 | [diff] [blame] | 3269 | |
Richard Smith | c7f8116 | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 3270 | // Check for Microsoft C extension: anonymous struct member. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3271 | if (getLangOpts().MicrosoftExt && !getLangOpts().CPlusPlus && |
Francois Pichet | 8e161ed | 2010-11-23 06:07:27 +0000 | [diff] [blame] | 3272 | CurContext->isRecord() && |
| 3273 | DS.getStorageClassSpec() == DeclSpec::SCS_unspecified) { |
| 3274 | // Handle 2 kinds of anonymous struct: |
| 3275 | // struct STRUCT; |
| 3276 | // and |
| 3277 | // STRUCT_TYPE; <- where STRUCT_TYPE is a typedef struct. |
| 3278 | RecordDecl *Record = dyn_cast_or_null<RecordDecl>(Tag); |
John McCall | 5e1cdac | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 3279 | if ((Record && Record->getDeclName() && !Record->isCompleteDefinition()) || |
Francois Pichet | 8e161ed | 2010-11-23 06:07:27 +0000 | [diff] [blame] | 3280 | (DS.getTypeSpecType() == DeclSpec::TST_typename && |
| 3281 | DS.getRepAsType().get()->isStructureType())) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3282 | Diag(DS.getLocStart(), diag::ext_ms_anonymous_struct) |
Francois Pichet | 8e161ed | 2010-11-23 06:07:27 +0000 | [diff] [blame] | 3283 | << DS.getSourceRange(); |
| 3284 | return BuildMicrosoftCAnonymousStruct(S, DS, Record); |
| 3285 | } |
Douglas Gregor | 4920f1f | 2009-01-12 22:49:06 +0000 | [diff] [blame] | 3286 | } |
Richard Smith | c7f8116 | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 3287 | |
| 3288 | // Skip all the checks below if we have a type error. |
| 3289 | if (DS.getTypeSpecType() == DeclSpec::TST_error || |
| 3290 | (TagD && TagD->isInvalidDecl())) |
| 3291 | return TagD; |
| 3292 | |
| 3293 | if (getLangOpts().CPlusPlus && |
Douglas Gregor | a131d0f | 2010-07-13 06:24:26 +0000 | [diff] [blame] | 3294 | DS.getStorageClassSpec() != DeclSpec::SCS_typedef) |
| 3295 | if (EnumDecl *Enum = dyn_cast_or_null<EnumDecl>(Tag)) |
| 3296 | if (Enum->enumerator_begin() == Enum->enumerator_end() && |
Richard Smith | c7f8116 | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 3297 | !Enum->getIdentifier() && !Enum->isInvalidDecl()) |
| 3298 | DeclaresAnything = false; |
John McCall | ac4df24 | 2011-03-22 23:00:04 +0000 | [diff] [blame] | 3299 | |
John McCall | ac4df24 | 2011-03-22 23:00:04 +0000 | [diff] [blame] | 3300 | if (!DS.isMissingDeclaratorOk()) { |
Richard Smith | c7f8116 | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 3301 | // Customize diagnostic for a typedef missing a name. |
| 3302 | if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3303 | Diag(DS.getLocStart(), diag::ext_typedef_without_a_name) |
Douglas Gregor | a0ebd60 | 2010-07-16 15:40:40 +0000 | [diff] [blame] | 3304 | << DS.getSourceRange(); |
Richard Smith | c7f8116 | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 3305 | else |
| 3306 | DeclaresAnything = false; |
Sebastian Redl | a4ed0d8 | 2008-12-28 15:28:59 +0000 | [diff] [blame] | 3307 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3308 | |
Richard Smith | c7f8116 | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 3309 | if (DS.isModulePrivateSpecified() && |
Douglas Gregor | e389585 | 2011-09-12 18:37:38 +0000 | [diff] [blame] | 3310 | Tag && Tag->getDeclContext()->isFunctionOrMethod()) |
| 3311 | Diag(DS.getModulePrivateSpecLoc(), diag::err_module_private_local_class) |
| 3312 | << Tag->getTagKind() |
| 3313 | << FixItHint::CreateRemoval(DS.getModulePrivateSpecLoc()); |
| 3314 | |
Richard Smith | c7f8116 | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 3315 | ActOnDocumentableDecl(TagD); |
| 3316 | |
| 3317 | // C 6.7/2: |
| 3318 | // A declaration [...] shall declare at least a declarator [...], a tag, |
| 3319 | // or the members of an enumeration. |
| 3320 | // C++ [dcl.dcl]p3: |
| 3321 | // [If there are no declarators], and except for the declaration of an |
| 3322 | // unnamed bit-field, the decl-specifier-seq shall introduce one or more |
| 3323 | // names into the program, or shall redeclare a name introduced by a |
| 3324 | // previous declaration. |
| 3325 | if (!DeclaresAnything) { |
| 3326 | // In C, we allow this as a (popular) extension / bug. Don't bother |
| 3327 | // producing further diagnostics for redundant qualifiers after this. |
| 3328 | Diag(DS.getLocStart(), diag::ext_no_declarators) << DS.getSourceRange(); |
| 3329 | return TagD; |
| 3330 | } |
| 3331 | |
| 3332 | // C++ [dcl.stc]p1: |
| 3333 | // If a storage-class-specifier appears in a decl-specifier-seq, [...] the |
| 3334 | // init-declarator-list of the declaration shall not be empty. |
| 3335 | // C++ [dcl.fct.spec]p1: |
| 3336 | // If a cv-qualifier appears in a decl-specifier-seq, the |
| 3337 | // init-declarator-list of the declaration shall not be empty. |
| 3338 | // |
| 3339 | // Spurious qualifiers here appear to be valid in C. |
| 3340 | unsigned DiagID = diag::warn_standalone_specifier; |
| 3341 | if (getLangOpts().CPlusPlus) |
| 3342 | DiagID = diag::ext_standalone_specifier; |
| 3343 | |
| 3344 | // Note that a linkage-specification sets a storage class, but |
| 3345 | // 'extern "C" struct foo;' is actually valid and not theoretically |
| 3346 | // useless. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 3347 | if (DeclSpec::SCS SCS = DS.getStorageClassSpec()) { |
| 3348 | if (SCS == DeclSpec::SCS_mutable) |
| 3349 | // Since mutable is not a viable storage class specifier in C, there is |
| 3350 | // no reason to treat it as an extension. Instead, diagnose as an error. |
| 3351 | Diag(DS.getStorageClassSpecLoc(), diag::err_mutable_nonmember); |
| 3352 | else if (!DS.isExternInLinkageSpec() && SCS != DeclSpec::SCS_typedef) |
Richard Smith | c7f8116 | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 3353 | Diag(DS.getStorageClassSpecLoc(), DiagID) |
| 3354 | << DeclSpec::getSpecifierName(SCS); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 3355 | } |
Richard Smith | c7f8116 | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 3356 | |
Richard Smith | ec64244 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 3357 | if (DeclSpec::TSCS TSCS = DS.getThreadStorageClassSpec()) |
| 3358 | Diag(DS.getThreadStorageClassSpecLoc(), DiagID) |
| 3359 | << DeclSpec::getSpecifierName(TSCS); |
Richard Smith | c7f8116 | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 3360 | if (DS.getTypeQualifiers()) { |
| 3361 | if (DS.getTypeQualifiers() & DeclSpec::TQ_const) |
| 3362 | Diag(DS.getConstSpecLoc(), DiagID) << "const"; |
| 3363 | if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile) |
| 3364 | Diag(DS.getConstSpecLoc(), DiagID) << "volatile"; |
| 3365 | // Restrict is covered above. |
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 3366 | if (DS.getTypeQualifiers() & DeclSpec::TQ_atomic) |
| 3367 | Diag(DS.getAtomicSpecLoc(), DiagID) << "_Atomic"; |
Richard Smith | c7f8116 | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 3368 | } |
| 3369 | |
Eli Friedman | fc038e9 | 2011-12-17 00:36:09 +0000 | [diff] [blame] | 3370 | // Warn about ignored type attributes, for example: |
| 3371 | // __attribute__((aligned)) struct A; |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3372 | // Attributes should be placed after tag to apply to type declaration. |
Eli Friedman | fc038e9 | 2011-12-17 00:36:09 +0000 | [diff] [blame] | 3373 | if (!DS.getAttributes().empty()) { |
| 3374 | DeclSpec::TST TypeSpecType = DS.getTypeSpecType(); |
| 3375 | if (TypeSpecType == DeclSpec::TST_class || |
| 3376 | TypeSpecType == DeclSpec::TST_struct || |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 3377 | TypeSpecType == DeclSpec::TST_interface || |
Eli Friedman | fc038e9 | 2011-12-17 00:36:09 +0000 | [diff] [blame] | 3378 | TypeSpecType == DeclSpec::TST_union || |
| 3379 | TypeSpecType == DeclSpec::TST_enum) { |
| 3380 | AttributeList* attrs = DS.getAttributes().getList(); |
| 3381 | while (attrs) { |
Michael Han | 45bed13 | 2012-10-04 16:42:52 +0000 | [diff] [blame] | 3382 | Diag(attrs->getLoc(), diag::warn_declspec_attribute_ignored) |
Eli Friedman | fc038e9 | 2011-12-17 00:36:09 +0000 | [diff] [blame] | 3383 | << attrs->getName() |
| 3384 | << (TypeSpecType == DeclSpec::TST_class ? 0 : |
| 3385 | TypeSpecType == DeclSpec::TST_struct ? 1 : |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 3386 | TypeSpecType == DeclSpec::TST_union ? 2 : |
| 3387 | TypeSpecType == DeclSpec::TST_interface ? 3 : 4); |
Eli Friedman | fc038e9 | 2011-12-17 00:36:09 +0000 | [diff] [blame] | 3388 | attrs = attrs->getNext(); |
| 3389 | } |
| 3390 | } |
| 3391 | } |
John McCall | ac4df24 | 2011-03-22 23:00:04 +0000 | [diff] [blame] | 3392 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3393 | return TagD; |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3394 | } |
| 3395 | |
John McCall | 1d7c528 | 2009-12-18 10:40:03 +0000 | [diff] [blame] | 3396 | /// We are trying to inject an anonymous member into the given scope; |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 3397 | /// check if there's an existing declaration that can't be overloaded. |
| 3398 | /// |
| 3399 | /// \return true if this is a forbidden redeclaration |
John McCall | 1d7c528 | 2009-12-18 10:40:03 +0000 | [diff] [blame] | 3400 | static bool CheckAnonMemberRedeclaration(Sema &SemaRef, |
| 3401 | Scope *S, |
Fariborz Jahanian | 588a4ad | 2010-01-22 18:30:17 +0000 | [diff] [blame] | 3402 | DeclContext *Owner, |
John McCall | 1d7c528 | 2009-12-18 10:40:03 +0000 | [diff] [blame] | 3403 | DeclarationName Name, |
| 3404 | SourceLocation NameLoc, |
| 3405 | unsigned diagnostic) { |
| 3406 | LookupResult R(SemaRef, Name, NameLoc, Sema::LookupMemberName, |
| 3407 | Sema::ForRedeclaration); |
| 3408 | if (!SemaRef.LookupName(R, S)) return false; |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 3409 | |
John McCall | 1d7c528 | 2009-12-18 10:40:03 +0000 | [diff] [blame] | 3410 | if (R.getAsSingle<TagDecl>()) |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 3411 | return false; |
| 3412 | |
| 3413 | // Pick a representative declaration. |
John McCall | 1d7c528 | 2009-12-18 10:40:03 +0000 | [diff] [blame] | 3414 | NamedDecl *PrevDecl = R.getRepresentativeDecl()->getUnderlyingDecl(); |
Argyrios Kyrtzidis | 2b64239 | 2010-09-23 14:26:01 +0000 | [diff] [blame] | 3415 | assert(PrevDecl && "Expected a non-null Decl"); |
| 3416 | |
| 3417 | if (!SemaRef.isDeclInScope(PrevDecl, Owner, S)) |
| 3418 | return false; |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 3419 | |
John McCall | 1d7c528 | 2009-12-18 10:40:03 +0000 | [diff] [blame] | 3420 | SemaRef.Diag(NameLoc, diagnostic) << Name; |
| 3421 | SemaRef.Diag(PrevDecl->getLocation(), diag::note_previous_declaration); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 3422 | |
| 3423 | return true; |
| 3424 | } |
| 3425 | |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3426 | /// InjectAnonymousStructOrUnionMembers - Inject the members of the |
| 3427 | /// anonymous struct or union AnonRecord into the owning context Owner |
| 3428 | /// and scope S. This routine will be invoked just after we realize |
| 3429 | /// that an unnamed union or struct is actually an anonymous union or |
| 3430 | /// struct, e.g., |
| 3431 | /// |
| 3432 | /// @code |
| 3433 | /// union { |
| 3434 | /// int i; |
| 3435 | /// float f; |
| 3436 | /// }; // InjectAnonymousStructOrUnionMembers called here to inject i and |
| 3437 | /// // f into the surrounding scope.x |
| 3438 | /// @endcode |
| 3439 | /// |
| 3440 | /// This routine is recursive, injecting the names of nested anonymous |
| 3441 | /// structs/unions into the owning context and scope as well. |
John McCall | aec0371 | 2010-05-21 20:45:30 +0000 | [diff] [blame] | 3442 | static bool InjectAnonymousStructOrUnionMembers(Sema &SemaRef, Scope *S, |
Craig Topper | 6b9240e | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 3443 | DeclContext *Owner, |
| 3444 | RecordDecl *AnonRecord, |
| 3445 | AccessSpecifier AS, |
| 3446 | SmallVectorImpl<NamedDecl *> &Chaining, |
| 3447 | bool MSAnonStruct) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 3448 | unsigned diagKind |
| 3449 | = AnonRecord->isUnion() ? diag::err_anonymous_union_member_redecl |
| 3450 | : diag::err_anonymous_struct_member_redecl; |
| 3451 | |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3452 | bool Invalid = false; |
Francois Pichet | 8e161ed | 2010-11-23 06:07:27 +0000 | [diff] [blame] | 3453 | |
| 3454 | // Look every FieldDecl and IndirectFieldDecl with a name. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3455 | for (auto *D : AnonRecord->decls()) { |
| 3456 | if ((isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D)) && |
| 3457 | cast<NamedDecl>(D)->getDeclName()) { |
| 3458 | ValueDecl *VD = cast<ValueDecl>(D); |
Francois Pichet | 8e161ed | 2010-11-23 06:07:27 +0000 | [diff] [blame] | 3459 | if (CheckAnonMemberRedeclaration(SemaRef, S, Owner, VD->getDeclName(), |
| 3460 | VD->getLocation(), diagKind)) { |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3461 | // C++ [class.union]p2: |
| 3462 | // The names of the members of an anonymous union shall be |
| 3463 | // distinct from the names of any other entity in the |
| 3464 | // scope in which the anonymous union is declared. |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3465 | Invalid = true; |
| 3466 | } else { |
| 3467 | // C++ [class.union]p2: |
| 3468 | // For the purpose of name lookup, after the anonymous union |
| 3469 | // definition, the members of the anonymous union are |
| 3470 | // considered to have been defined in the scope in which the |
| 3471 | // anonymous union is declared. |
Francois Pichet | 8e161ed | 2010-11-23 06:07:27 +0000 | [diff] [blame] | 3472 | unsigned OldChainingSize = Chaining.size(); |
| 3473 | if (IndirectFieldDecl *IF = dyn_cast<IndirectFieldDecl>(VD)) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3474 | for (auto *PI : IF->chain()) |
| 3475 | Chaining.push_back(PI); |
Francois Pichet | 8e161ed | 2010-11-23 06:07:27 +0000 | [diff] [blame] | 3476 | else |
| 3477 | Chaining.push_back(VD); |
| 3478 | |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 3479 | assert(Chaining.size() >= 2); |
| 3480 | NamedDecl **NamedChain = |
| 3481 | new (SemaRef.Context)NamedDecl*[Chaining.size()]; |
| 3482 | for (unsigned i = 0; i < Chaining.size(); i++) |
| 3483 | NamedChain[i] = Chaining[i]; |
| 3484 | |
| 3485 | IndirectFieldDecl* IndirectField = |
Francois Pichet | 8e161ed | 2010-11-23 06:07:27 +0000 | [diff] [blame] | 3486 | IndirectFieldDecl::Create(SemaRef.Context, Owner, VD->getLocation(), |
| 3487 | VD->getIdentifier(), VD->getType(), |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 3488 | NamedChain, Chaining.size()); |
| 3489 | |
| 3490 | IndirectField->setAccess(AS); |
| 3491 | IndirectField->setImplicit(); |
| 3492 | SemaRef.PushOnScopeChains(IndirectField, S); |
John McCall | aec0371 | 2010-05-21 20:45:30 +0000 | [diff] [blame] | 3493 | |
| 3494 | // That includes picking up the appropriate access specifier. |
Francois Pichet | 8e161ed | 2010-11-23 06:07:27 +0000 | [diff] [blame] | 3495 | if (AS != AS_none) IndirectField->setAccess(AS); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 3496 | |
Francois Pichet | 8e161ed | 2010-11-23 06:07:27 +0000 | [diff] [blame] | 3497 | Chaining.resize(OldChainingSize); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3498 | } |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3499 | } |
| 3500 | } |
| 3501 | |
| 3502 | return Invalid; |
| 3503 | } |
| 3504 | |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 3505 | /// StorageClassSpecToVarDeclStorageClass - Maps a DeclSpec::SCS to |
| 3506 | /// a VarDecl::StorageClass. Any error reporting is up to the caller: |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 3507 | /// illegal input values are mapped to SC_None. |
| 3508 | static StorageClass |
Rafael Espindola | 65dfa2b | 2013-04-25 12:11:36 +0000 | [diff] [blame] | 3509 | StorageClassSpecToVarDeclStorageClass(const DeclSpec &DS) { |
| 3510 | DeclSpec::SCS StorageClassSpec = DS.getStorageClassSpec(); |
| 3511 | assert(StorageClassSpec != DeclSpec::SCS_typedef && |
| 3512 | "Parser allowed 'typedef' as storage class VarDecl."); |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 3513 | switch (StorageClassSpec) { |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 3514 | case DeclSpec::SCS_unspecified: return SC_None; |
Rafael Espindola | 65dfa2b | 2013-04-25 12:11:36 +0000 | [diff] [blame] | 3515 | case DeclSpec::SCS_extern: |
| 3516 | if (DS.isExternInLinkageSpec()) |
| 3517 | return SC_None; |
| 3518 | return SC_Extern; |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 3519 | case DeclSpec::SCS_static: return SC_Static; |
| 3520 | case DeclSpec::SCS_auto: return SC_Auto; |
| 3521 | case DeclSpec::SCS_register: return SC_Register; |
| 3522 | case DeclSpec::SCS_private_extern: return SC_PrivateExtern; |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 3523 | // Illegal SCSs map to None: error reporting is up to the caller. |
| 3524 | case DeclSpec::SCS_mutable: // Fall through. |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 3525 | case DeclSpec::SCS_typedef: return SC_None; |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 3526 | } |
| 3527 | llvm_unreachable("unknown storage class specifier"); |
| 3528 | } |
| 3529 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3530 | static SourceLocation findDefaultInitializer(const CXXRecordDecl *Record) { |
| 3531 | assert(Record->hasInClassInitializer()); |
| 3532 | |
| 3533 | for (const auto *I : Record->decls()) { |
| 3534 | const auto *FD = dyn_cast<FieldDecl>(I); |
| 3535 | if (const auto *IFD = dyn_cast<IndirectFieldDecl>(I)) |
| 3536 | FD = IFD->getAnonField(); |
| 3537 | if (FD && FD->hasInClassInitializer()) |
| 3538 | return FD->getLocation(); |
| 3539 | } |
| 3540 | |
| 3541 | llvm_unreachable("couldn't find in-class initializer"); |
| 3542 | } |
| 3543 | |
| 3544 | static void checkDuplicateDefaultInit(Sema &S, CXXRecordDecl *Parent, |
| 3545 | SourceLocation DefaultInitLoc) { |
| 3546 | if (!Parent->isUnion() || !Parent->hasInClassInitializer()) |
| 3547 | return; |
| 3548 | |
| 3549 | S.Diag(DefaultInitLoc, diag::err_multiple_mem_union_initialization); |
| 3550 | S.Diag(findDefaultInitializer(Parent), diag::note_previous_initializer) << 0; |
| 3551 | } |
| 3552 | |
| 3553 | static void checkDuplicateDefaultInit(Sema &S, CXXRecordDecl *Parent, |
| 3554 | CXXRecordDecl *AnonUnion) { |
| 3555 | if (!Parent->isUnion() || !Parent->hasInClassInitializer()) |
| 3556 | return; |
| 3557 | |
| 3558 | checkDuplicateDefaultInit(S, Parent, findDefaultInitializer(AnonUnion)); |
| 3559 | } |
| 3560 | |
Francois Pichet | 8e161ed | 2010-11-23 06:07:27 +0000 | [diff] [blame] | 3561 | /// BuildAnonymousStructOrUnion - Handle the declaration of an |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3562 | /// anonymous structure or union. Anonymous unions are a C++ feature |
Hans Wennborg | acbabf1 | 2012-02-03 15:47:04 +0000 | [diff] [blame] | 3563 | /// (C++ [class.union]) and a C11 feature; anonymous structures |
| 3564 | /// are a C11 feature and GNU C++ extension. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3565 | Decl *Sema::BuildAnonymousStructOrUnion(Scope *S, DeclSpec &DS, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3566 | AccessSpecifier AS, |
| 3567 | RecordDecl *Record, |
| 3568 | const PrintingPolicy &Policy) { |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3569 | DeclContext *Owner = Record->getDeclContext(); |
| 3570 | |
| 3571 | // Diagnose whether this anonymous struct/union is an extension. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3572 | if (Record->isUnion() && !getLangOpts().CPlusPlus && !getLangOpts().C11) |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3573 | Diag(Record->getLocation(), diag::ext_anonymous_union); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3574 | else if (!Record->isUnion() && getLangOpts().CPlusPlus) |
Hans Wennborg | acbabf1 | 2012-02-03 15:47:04 +0000 | [diff] [blame] | 3575 | Diag(Record->getLocation(), diag::ext_gnu_anonymous_struct); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3576 | else if (!Record->isUnion() && !getLangOpts().C11) |
Hans Wennborg | acbabf1 | 2012-02-03 15:47:04 +0000 | [diff] [blame] | 3577 | Diag(Record->getLocation(), diag::ext_c11_anonymous_struct); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3578 | |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3579 | // C and C++ require different kinds of checks for anonymous |
| 3580 | // structs/unions. |
| 3581 | bool Invalid = false; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3582 | if (getLangOpts().CPlusPlus) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 3583 | const char *PrevSpec = nullptr; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3584 | unsigned DiagID; |
David Blaikie | 2b79c32 | 2011-10-19 22:43:29 +0000 | [diff] [blame] | 3585 | if (Record->isUnion()) { |
| 3586 | // C++ [class.union]p6: |
| 3587 | // Anonymous unions declared in a named namespace or in the |
| 3588 | // global namespace shall be declared static. |
| 3589 | if (DS.getStorageClassSpec() != DeclSpec::SCS_static && |
| 3590 | (isa<TranslationUnitDecl>(Owner) || |
| 3591 | (isa<NamespaceDecl>(Owner) && |
| 3592 | cast<NamespaceDecl>(Owner)->getDeclName()))) { |
David Blaikie | 82c8ca1 | 2011-10-20 02:49:08 +0000 | [diff] [blame] | 3593 | Diag(Record->getLocation(), diag::err_anonymous_union_not_static) |
| 3594 | << FixItHint::CreateInsertion(Record->getLocation(), "static "); |
David Blaikie | 2b79c32 | 2011-10-19 22:43:29 +0000 | [diff] [blame] | 3595 | |
| 3596 | // Recover by adding 'static'. |
| 3597 | DS.SetStorageClassSpec(*this, DeclSpec::SCS_static, SourceLocation(), |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3598 | PrevSpec, DiagID, Policy); |
David Blaikie | 2b79c32 | 2011-10-19 22:43:29 +0000 | [diff] [blame] | 3599 | } |
| 3600 | // C++ [class.union]p6: |
| 3601 | // A storage class is not allowed in a declaration of an |
| 3602 | // anonymous union in a class scope. |
| 3603 | else if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified && |
| 3604 | isa<RecordDecl>(Owner)) { |
| 3605 | Diag(DS.getStorageClassSpecLoc(), |
David Blaikie | f6f876c | 2011-10-20 02:10:55 +0000 | [diff] [blame] | 3606 | diag::err_anonymous_union_with_storage_spec) |
| 3607 | << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc()); |
David Blaikie | 2b79c32 | 2011-10-19 22:43:29 +0000 | [diff] [blame] | 3608 | |
| 3609 | // Recover by removing the storage specifier. |
David Blaikie | d662a79 | 2011-10-19 22:56:21 +0000 | [diff] [blame] | 3610 | DS.SetStorageClassSpec(*this, DeclSpec::SCS_unspecified, |
| 3611 | SourceLocation(), |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3612 | PrevSpec, DiagID, Context.getPrintingPolicy()); |
David Blaikie | 2b79c32 | 2011-10-19 22:43:29 +0000 | [diff] [blame] | 3613 | } |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3614 | } |
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 3615 | |
Douglas Gregor | 7604f64 | 2011-05-09 23:05:33 +0000 | [diff] [blame] | 3616 | // Ignore const/volatile/restrict qualifiers. |
| 3617 | if (DS.getTypeQualifiers()) { |
| 3618 | if (DS.getTypeQualifiers() & DeclSpec::TQ_const) |
| 3619 | Diag(DS.getConstSpecLoc(), diag::ext_anonymous_struct_union_qualified) |
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 3620 | << Record->isUnion() << "const" |
Douglas Gregor | 7604f64 | 2011-05-09 23:05:33 +0000 | [diff] [blame] | 3621 | << FixItHint::CreateRemoval(DS.getConstSpecLoc()); |
| 3622 | if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile) |
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 3623 | Diag(DS.getVolatileSpecLoc(), |
David Blaikie | d662a79 | 2011-10-19 22:56:21 +0000 | [diff] [blame] | 3624 | diag::ext_anonymous_struct_union_qualified) |
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 3625 | << Record->isUnion() << "volatile" |
Douglas Gregor | 7604f64 | 2011-05-09 23:05:33 +0000 | [diff] [blame] | 3626 | << FixItHint::CreateRemoval(DS.getVolatileSpecLoc()); |
| 3627 | if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict) |
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 3628 | Diag(DS.getRestrictSpecLoc(), |
David Blaikie | d662a79 | 2011-10-19 22:56:21 +0000 | [diff] [blame] | 3629 | diag::ext_anonymous_struct_union_qualified) |
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 3630 | << Record->isUnion() << "restrict" |
Douglas Gregor | 7604f64 | 2011-05-09 23:05:33 +0000 | [diff] [blame] | 3631 | << FixItHint::CreateRemoval(DS.getRestrictSpecLoc()); |
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 3632 | if (DS.getTypeQualifiers() & DeclSpec::TQ_atomic) |
| 3633 | Diag(DS.getAtomicSpecLoc(), |
| 3634 | diag::ext_anonymous_struct_union_qualified) |
| 3635 | << Record->isUnion() << "_Atomic" |
| 3636 | << FixItHint::CreateRemoval(DS.getAtomicSpecLoc()); |
Douglas Gregor | 7604f64 | 2011-05-09 23:05:33 +0000 | [diff] [blame] | 3637 | |
| 3638 | DS.ClearTypeQualifiers(); |
| 3639 | } |
| 3640 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3641 | // C++ [class.union]p2: |
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 3642 | // The member-specification of an anonymous union shall only |
| 3643 | // define non-static data members. [Note: nested types and |
| 3644 | // functions cannot be declared within an anonymous union. ] |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3645 | for (auto *Mem : Record->decls()) { |
| 3646 | if (auto *FD = dyn_cast<FieldDecl>(Mem)) { |
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 3647 | // C++ [class.union]p3: |
| 3648 | // An anonymous union shall not have private or protected |
| 3649 | // members (clause 11). |
John McCall | aec0371 | 2010-05-21 20:45:30 +0000 | [diff] [blame] | 3650 | assert(FD->getAccess() != AS_none); |
| 3651 | if (FD->getAccess() != AS_public) { |
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 3652 | Diag(FD->getLocation(), diag::err_anonymous_record_nonpublic_member) |
| 3653 | << (int)Record->isUnion() << (int)(FD->getAccess() == AS_protected); |
| 3654 | Invalid = true; |
| 3655 | } |
Argyrios Kyrtzidis | dd7744d | 2010-08-16 17:27:08 +0000 | [diff] [blame] | 3656 | |
Sean Hunt | cf34e75 | 2011-05-16 22:41:40 +0000 | [diff] [blame] | 3657 | // C++ [class.union]p1 |
| 3658 | // An object of a class with a non-trivial constructor, a non-trivial |
| 3659 | // copy constructor, a non-trivial destructor, or a non-trivial copy |
| 3660 | // assignment operator cannot be a member of a union, nor can an |
| 3661 | // array of such objects. |
Richard Smith | e7d7c39 | 2011-10-19 20:41:51 +0000 | [diff] [blame] | 3662 | if (CheckNontrivialField(FD)) |
Argyrios Kyrtzidis | dd7744d | 2010-08-16 17:27:08 +0000 | [diff] [blame] | 3663 | Invalid = true; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3664 | } else if (Mem->isImplicit()) { |
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 3665 | // Any implicit members are fine. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3666 | } else if (isa<TagDecl>(Mem) && Mem->getDeclContext() != Record) { |
Douglas Gregor | 1931b44 | 2009-02-03 00:34:39 +0000 | [diff] [blame] | 3667 | // This is a type that showed up in an |
| 3668 | // elaborated-type-specifier inside the anonymous struct or |
| 3669 | // union, but which actually declares a type outside of the |
| 3670 | // anonymous struct or union. It's okay. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3671 | } else if (auto *MemRecord = dyn_cast<RecordDecl>(Mem)) { |
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 3672 | if (!MemRecord->isAnonymousStructOrUnion() && |
| 3673 | MemRecord->getDeclName()) { |
Francois Pichet | 538e0d0 | 2010-09-08 11:32:25 +0000 | [diff] [blame] | 3674 | // Visual C++ allows type definition in anonymous struct or union. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3675 | if (getLangOpts().MicrosoftExt) |
Francois Pichet | 538e0d0 | 2010-09-08 11:32:25 +0000 | [diff] [blame] | 3676 | Diag(MemRecord->getLocation(), diag::ext_anonymous_record_with_type) |
| 3677 | << (int)Record->isUnion(); |
| 3678 | else { |
| 3679 | // This is a nested type declaration. |
| 3680 | Diag(MemRecord->getLocation(), diag::err_anonymous_record_with_type) |
| 3681 | << (int)Record->isUnion(); |
| 3682 | Invalid = true; |
| 3683 | } |
Richard Smith | c5f7d6a | 2013-01-28 00:54:05 +0000 | [diff] [blame] | 3684 | } else { |
| 3685 | // This is an anonymous type definition within another anonymous type. |
| 3686 | // This is a popular extension, provided by Plan9, MSVC and GCC, but |
| 3687 | // not part of standard C++. |
| 3688 | Diag(MemRecord->getLocation(), |
Richard Smith | f270519 | 2013-01-31 03:11:12 +0000 | [diff] [blame] | 3689 | diag::ext_anonymous_record_with_anonymous_type) |
| 3690 | << (int)Record->isUnion(); |
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 3691 | } |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3692 | } else if (isa<AccessSpecDecl>(Mem)) { |
Abramo Bagnara | 6206d53 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 3693 | // Any access specifier is fine. |
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 3694 | } else { |
| 3695 | // We have something that isn't a non-static data |
| 3696 | // member. Complain about it. |
| 3697 | unsigned DK = diag::err_anonymous_record_bad_member; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3698 | if (isa<TypeDecl>(Mem)) |
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 3699 | DK = diag::err_anonymous_record_with_type; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3700 | else if (isa<FunctionDecl>(Mem)) |
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 3701 | DK = diag::err_anonymous_record_with_function; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3702 | else if (isa<VarDecl>(Mem)) |
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 3703 | DK = diag::err_anonymous_record_with_static; |
Francois Pichet | 538e0d0 | 2010-09-08 11:32:25 +0000 | [diff] [blame] | 3704 | |
| 3705 | // Visual C++ allows type definition in anonymous struct or union. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3706 | if (getLangOpts().MicrosoftExt && |
Francois Pichet | 538e0d0 | 2010-09-08 11:32:25 +0000 | [diff] [blame] | 3707 | DK == diag::err_anonymous_record_with_type) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3708 | Diag(Mem->getLocation(), diag::ext_anonymous_record_with_type) |
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 3709 | << (int)Record->isUnion(); |
Francois Pichet | 538e0d0 | 2010-09-08 11:32:25 +0000 | [diff] [blame] | 3710 | else { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3711 | Diag(Mem->getLocation(), DK) |
Francois Pichet | 538e0d0 | 2010-09-08 11:32:25 +0000 | [diff] [blame] | 3712 | << (int)Record->isUnion(); |
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 3713 | Invalid = true; |
Francois Pichet | 538e0d0 | 2010-09-08 11:32:25 +0000 | [diff] [blame] | 3714 | } |
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 3715 | } |
| 3716 | } |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3717 | |
| 3718 | // C++11 [class.union]p8 (DR1460): |
| 3719 | // At most one variant member of a union may have a |
| 3720 | // brace-or-equal-initializer. |
| 3721 | if (cast<CXXRecordDecl>(Record)->hasInClassInitializer() && |
| 3722 | Owner->isRecord()) |
| 3723 | checkDuplicateDefaultInit(*this, cast<CXXRecordDecl>(Owner), |
| 3724 | cast<CXXRecordDecl>(Record)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3725 | } |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3726 | |
| 3727 | if (!Record->isUnion() && !Owner->isRecord()) { |
Douglas Gregor | 4920f1f | 2009-01-12 22:49:06 +0000 | [diff] [blame] | 3728 | Diag(Record->getLocation(), diag::err_anonymous_struct_not_member) |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3729 | << (int)getLangOpts().CPlusPlus; |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3730 | Invalid = true; |
| 3731 | } |
| 3732 | |
John McCall | eb692e0 | 2009-10-22 23:31:08 +0000 | [diff] [blame] | 3733 | // Mock up a declarator. |
Argyrios Kyrtzidis | d3880f8 | 2011-06-28 03:01:18 +0000 | [diff] [blame] | 3734 | Declarator Dc(DS, Declarator::MemberContext); |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 3735 | TypeSourceInfo *TInfo = GetTypeForDeclarator(Dc, S); |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3736 | assert(TInfo && "couldn't build declarator info for anonymous struct/union"); |
John McCall | eb692e0 | 2009-10-22 23:31:08 +0000 | [diff] [blame] | 3737 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3738 | // Create a declaration for this anonymous struct/union. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 3739 | NamedDecl *Anon = nullptr; |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3740 | if (RecordDecl *OwningClass = dyn_cast<RecordDecl>(Owner)) { |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3741 | Anon = FieldDecl::Create(Context, OwningClass, |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3742 | DS.getLocStart(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3743 | Record->getLocation(), |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 3744 | /*IdentifierInfo=*/nullptr, |
Argyrios Kyrtzidis | a5d8200 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 3745 | Context.getTypeDeclType(Record), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3746 | TInfo, |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 3747 | /*BitWidth=*/nullptr, /*Mutable=*/false, |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 3748 | /*InitStyle=*/ICIS_NoInit); |
John McCall | aec0371 | 2010-05-21 20:45:30 +0000 | [diff] [blame] | 3749 | Anon->setAccess(AS); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3750 | if (getLangOpts().CPlusPlus) |
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 3751 | FieldCollector->Add(cast<FieldDecl>(Anon)); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3752 | } else { |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 3753 | DeclSpec::SCS SCSpec = DS.getStorageClassSpec(); |
Rafael Espindola | 65dfa2b | 2013-04-25 12:11:36 +0000 | [diff] [blame] | 3754 | VarDecl::StorageClass SC = StorageClassSpecToVarDeclStorageClass(DS); |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 3755 | if (SCSpec == DeclSpec::SCS_mutable) { |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3756 | // mutable can only appear on non-static class members, so it's always |
| 3757 | // an error here |
| 3758 | Diag(Record->getLocation(), diag::err_mutable_nonmember); |
| 3759 | Invalid = true; |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 3760 | SC = SC_None; |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3761 | } |
| 3762 | |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3763 | Anon = VarDecl::Create(Context, Owner, |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3764 | DS.getLocStart(), |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 3765 | Record->getLocation(), /*IdentifierInfo=*/nullptr, |
Argyrios Kyrtzidis | a5d8200 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 3766 | Context.getTypeDeclType(Record), |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 3767 | TInfo, SC); |
Richard Smith | 16ee819 | 2011-09-18 00:06:34 +0000 | [diff] [blame] | 3768 | |
| 3769 | // Default-initialize the implicit variable. This initialization will be |
| 3770 | // trivial in almost all cases, except if a union member has an in-class |
| 3771 | // initializer: |
| 3772 | // union { int n = 0; }; |
| 3773 | ActOnUninitializedDecl(Anon, /*TypeMayContainAuto=*/false); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3774 | } |
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 3775 | Anon->setImplicit(); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3776 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3777 | // Mark this as an anonymous struct/union type. |
| 3778 | Record->setAnonymousStructOrUnion(true); |
| 3779 | |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3780 | // Add the anonymous struct/union object to the current |
| 3781 | // context. We'll be referencing this object when we refer to one of |
| 3782 | // its members. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3783 | Owner->addDecl(Anon); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3784 | |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3785 | // Inject the members of the anonymous struct/union into the owning |
| 3786 | // context and into the identifier resolver chain for name lookup |
| 3787 | // purposes. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3788 | SmallVector<NamedDecl*, 2> Chain; |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 3789 | Chain.push_back(Anon); |
| 3790 | |
Francois Pichet | 8e161ed | 2010-11-23 06:07:27 +0000 | [diff] [blame] | 3791 | if (InjectAnonymousStructOrUnionMembers(*this, S, Owner, Record, AS, |
| 3792 | Chain, false)) |
Douglas Gregor | 4920f1f | 2009-01-12 22:49:06 +0000 | [diff] [blame] | 3793 | Invalid = true; |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3794 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3795 | if (VarDecl *NewVD = dyn_cast<VarDecl>(Anon)) { |
| 3796 | if (getLangOpts().CPlusPlus && NewVD->isStaticLocal()) { |
| 3797 | Decl *ManglingContextDecl; |
| 3798 | if (MangleNumberingContext *MCtx = |
| 3799 | getCurrentMangleNumberContext(NewVD->getDeclContext(), |
| 3800 | ManglingContextDecl)) { |
| 3801 | Context.setManglingNumber(NewVD, MCtx->getManglingNumber(NewVD, S->getMSLocalManglingNumber())); |
| 3802 | Context.setStaticLocalNumber(NewVD, MCtx->getStaticLocalNumber(NewVD)); |
| 3803 | } |
| 3804 | } |
| 3805 | } |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3806 | |
| 3807 | if (Invalid) |
| 3808 | Anon->setInvalidDecl(); |
| 3809 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3810 | return Anon; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3811 | } |
| 3812 | |
Francois Pichet | 8e161ed | 2010-11-23 06:07:27 +0000 | [diff] [blame] | 3813 | /// BuildMicrosoftCAnonymousStruct - Handle the declaration of an |
| 3814 | /// Microsoft C anonymous structure. |
| 3815 | /// Ref: http://msdn.microsoft.com/en-us/library/z2cx9y4f.aspx |
| 3816 | /// Example: |
| 3817 | /// |
| 3818 | /// struct A { int a; }; |
| 3819 | /// struct B { struct A; int b; }; |
| 3820 | /// |
| 3821 | /// void foo() { |
| 3822 | /// B var; |
| 3823 | /// var.a = 3; |
| 3824 | /// } |
| 3825 | /// |
| 3826 | Decl *Sema::BuildMicrosoftCAnonymousStruct(Scope *S, DeclSpec &DS, |
| 3827 | RecordDecl *Record) { |
| 3828 | |
| 3829 | // If there is no Record, get the record via the typedef. |
| 3830 | if (!Record) |
| 3831 | Record = DS.getRepAsType().get()->getAsStructureType()->getDecl(); |
| 3832 | |
| 3833 | // Mock up a declarator. |
| 3834 | Declarator Dc(DS, Declarator::TypeNameContext); |
| 3835 | TypeSourceInfo *TInfo = GetTypeForDeclarator(Dc, S); |
| 3836 | assert(TInfo && "couldn't build declarator info for anonymous struct"); |
| 3837 | |
| 3838 | // Create a declaration for this anonymous struct. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 3839 | NamedDecl *Anon = FieldDecl::Create(Context, |
Francois Pichet | 8e161ed | 2010-11-23 06:07:27 +0000 | [diff] [blame] | 3840 | cast<RecordDecl>(CurContext), |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3841 | DS.getLocStart(), |
| 3842 | DS.getLocStart(), |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 3843 | /*IdentifierInfo=*/nullptr, |
Francois Pichet | 8e161ed | 2010-11-23 06:07:27 +0000 | [diff] [blame] | 3844 | Context.getTypeDeclType(Record), |
| 3845 | TInfo, |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 3846 | /*BitWidth=*/nullptr, /*Mutable=*/false, |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 3847 | /*InitStyle=*/ICIS_NoInit); |
Francois Pichet | 8e161ed | 2010-11-23 06:07:27 +0000 | [diff] [blame] | 3848 | Anon->setImplicit(); |
| 3849 | |
| 3850 | // Add the anonymous struct object to the current context. |
| 3851 | CurContext->addDecl(Anon); |
| 3852 | |
| 3853 | // Inject the members of the anonymous struct into the current |
| 3854 | // context and into the identifier resolver chain for name lookup |
| 3855 | // purposes. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3856 | SmallVector<NamedDecl*, 2> Chain; |
Francois Pichet | 8e161ed | 2010-11-23 06:07:27 +0000 | [diff] [blame] | 3857 | Chain.push_back(Anon); |
| 3858 | |
Nico Weber | ee625af | 2012-02-01 00:41:00 +0000 | [diff] [blame] | 3859 | RecordDecl *RecordDef = Record->getDefinition(); |
| 3860 | if (!RecordDef || InjectAnonymousStructOrUnionMembers(*this, S, CurContext, |
| 3861 | RecordDef, AS_none, |
| 3862 | Chain, true)) |
Francois Pichet | 8e161ed | 2010-11-23 06:07:27 +0000 | [diff] [blame] | 3863 | Anon->setInvalidDecl(); |
| 3864 | |
| 3865 | return Anon; |
| 3866 | } |
Steve Naroff | f009063 | 2007-09-02 02:04:30 +0000 | [diff] [blame] | 3867 | |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 3868 | /// GetNameForDeclarator - Determine the full declaration name for the |
| 3869 | /// given Declarator. |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3870 | DeclarationNameInfo Sema::GetNameForDeclarator(Declarator &D) { |
Douglas Gregor | 02a24ee | 2009-11-03 16:56:39 +0000 | [diff] [blame] | 3871 | return GetNameFromUnqualifiedId(D.getName()); |
| 3872 | } |
| 3873 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3874 | /// \brief Retrieves the declaration name from a parsed unqualified-id. |
| 3875 | DeclarationNameInfo |
| 3876 | Sema::GetNameFromUnqualifiedId(const UnqualifiedId &Name) { |
| 3877 | DeclarationNameInfo NameInfo; |
| 3878 | NameInfo.setLoc(Name.StartLocation); |
| 3879 | |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 3880 | switch (Name.getKind()) { |
Sean Hunt | 0486d74 | 2009-11-28 04:44:28 +0000 | [diff] [blame] | 3881 | |
Fariborz Jahanian | 98a5403 | 2011-07-12 17:16:56 +0000 | [diff] [blame] | 3882 | case UnqualifiedId::IK_ImplicitSelfParam: |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3883 | case UnqualifiedId::IK_Identifier: |
| 3884 | NameInfo.setName(Name.Identifier); |
| 3885 | NameInfo.setLoc(Name.StartLocation); |
| 3886 | return NameInfo; |
Sean Hunt | 0486d74 | 2009-11-28 04:44:28 +0000 | [diff] [blame] | 3887 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3888 | case UnqualifiedId::IK_OperatorFunctionId: |
| 3889 | NameInfo.setName(Context.DeclarationNames.getCXXOperatorName( |
| 3890 | Name.OperatorFunctionId.Operator)); |
| 3891 | NameInfo.setLoc(Name.StartLocation); |
| 3892 | NameInfo.getInfo().CXXOperatorName.BeginOpNameLoc |
| 3893 | = Name.OperatorFunctionId.SymbolLocations[0]; |
| 3894 | NameInfo.getInfo().CXXOperatorName.EndOpNameLoc |
| 3895 | = Name.EndLocation.getRawEncoding(); |
| 3896 | return NameInfo; |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3897 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3898 | case UnqualifiedId::IK_LiteralOperatorId: |
| 3899 | NameInfo.setName(Context.DeclarationNames.getCXXLiteralOperatorName( |
| 3900 | Name.Identifier)); |
| 3901 | NameInfo.setLoc(Name.StartLocation); |
| 3902 | NameInfo.setCXXLiteralOperatorNameLoc(Name.EndLocation); |
| 3903 | return NameInfo; |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3904 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3905 | case UnqualifiedId::IK_ConversionFunctionId: { |
| 3906 | TypeSourceInfo *TInfo; |
| 3907 | QualType Ty = GetTypeFromParser(Name.ConversionFunctionId, &TInfo); |
| 3908 | if (Ty.isNull()) |
| 3909 | return DeclarationNameInfo(); |
| 3910 | NameInfo.setName(Context.DeclarationNames.getCXXConversionFunctionName( |
| 3911 | Context.getCanonicalType(Ty))); |
| 3912 | NameInfo.setLoc(Name.StartLocation); |
| 3913 | NameInfo.setNamedTypeInfo(TInfo); |
| 3914 | return NameInfo; |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 3915 | } |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3916 | |
| 3917 | case UnqualifiedId::IK_ConstructorName: { |
| 3918 | TypeSourceInfo *TInfo; |
| 3919 | QualType Ty = GetTypeFromParser(Name.ConstructorName, &TInfo); |
| 3920 | if (Ty.isNull()) |
| 3921 | return DeclarationNameInfo(); |
| 3922 | NameInfo.setName(Context.DeclarationNames.getCXXConstructorName( |
| 3923 | Context.getCanonicalType(Ty))); |
| 3924 | NameInfo.setLoc(Name.StartLocation); |
| 3925 | NameInfo.setNamedTypeInfo(TInfo); |
| 3926 | return NameInfo; |
| 3927 | } |
| 3928 | |
| 3929 | case UnqualifiedId::IK_ConstructorTemplateId: { |
| 3930 | // In well-formed code, we can only have a constructor |
| 3931 | // template-id that refers to the current context, so go there |
| 3932 | // to find the actual type being constructed. |
| 3933 | CXXRecordDecl *CurClass = dyn_cast<CXXRecordDecl>(CurContext); |
| 3934 | if (!CurClass || CurClass->getIdentifier() != Name.TemplateId->Name) |
| 3935 | return DeclarationNameInfo(); |
| 3936 | |
| 3937 | // Determine the type of the class being constructed. |
| 3938 | QualType CurClassType = Context.getTypeDeclType(CurClass); |
| 3939 | |
| 3940 | // FIXME: Check two things: that the template-id names the same type as |
| 3941 | // CurClassType, and that the template-id does not occur when the name |
| 3942 | // was qualified. |
| 3943 | |
| 3944 | NameInfo.setName(Context.DeclarationNames.getCXXConstructorName( |
| 3945 | Context.getCanonicalType(CurClassType))); |
| 3946 | NameInfo.setLoc(Name.StartLocation); |
| 3947 | // FIXME: should we retrieve TypeSourceInfo? |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 3948 | NameInfo.setNamedTypeInfo(nullptr); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3949 | return NameInfo; |
| 3950 | } |
| 3951 | |
| 3952 | case UnqualifiedId::IK_DestructorName: { |
| 3953 | TypeSourceInfo *TInfo; |
| 3954 | QualType Ty = GetTypeFromParser(Name.DestructorName, &TInfo); |
| 3955 | if (Ty.isNull()) |
| 3956 | return DeclarationNameInfo(); |
| 3957 | NameInfo.setName(Context.DeclarationNames.getCXXDestructorName( |
| 3958 | Context.getCanonicalType(Ty))); |
| 3959 | NameInfo.setLoc(Name.StartLocation); |
| 3960 | NameInfo.setNamedTypeInfo(TInfo); |
| 3961 | return NameInfo; |
| 3962 | } |
| 3963 | |
| 3964 | case UnqualifiedId::IK_TemplateId: { |
John McCall | 2b5289b | 2010-08-23 07:28:44 +0000 | [diff] [blame] | 3965 | TemplateName TName = Name.TemplateId->Template.get(); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3966 | SourceLocation TNameLoc = Name.TemplateId->TemplateNameLoc; |
| 3967 | return Context.getNameForTemplate(TName, TNameLoc); |
| 3968 | } |
| 3969 | |
| 3970 | } // switch (Name.getKind()) |
| 3971 | |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3972 | llvm_unreachable("Unknown name kind"); |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 3973 | } |
| 3974 | |
Kaelyn Uhrain | 4d9d157 | 2011-08-04 17:40:00 +0000 | [diff] [blame] | 3975 | static QualType getCoreType(QualType Ty) { |
| 3976 | do { |
| 3977 | if (Ty->isPointerType() || Ty->isReferenceType()) |
| 3978 | Ty = Ty->getPointeeType(); |
| 3979 | else if (Ty->isArrayType()) |
| 3980 | Ty = Ty->castAsArrayTypeUnsafe()->getElementType(); |
| 3981 | else |
| 3982 | return Ty.withoutLocalFastQualifiers(); |
| 3983 | } while (true); |
| 3984 | } |
| 3985 | |
Kaelyn Uhrain | 2afd766 | 2011-10-11 00:28:39 +0000 | [diff] [blame] | 3986 | /// hasSimilarParameters - Determine whether the C++ functions Declaration |
| 3987 | /// and Definition have "nearly" matching parameters. This heuristic is |
| 3988 | /// used to improve diagnostics in the case where an out-of-line function |
| 3989 | /// definition doesn't match any declaration within the class or namespace. |
| 3990 | /// Also sets Params to the list of indices to the parameters that differ |
| 3991 | /// between the declaration and the definition. If hasSimilarParameters |
| 3992 | /// returns true and Params is empty, then all of the parameters match. |
| 3993 | static bool hasSimilarParameters(ASTContext &Context, |
Douglas Gregor | 4ce205f | 2009-02-06 17:46:57 +0000 | [diff] [blame] | 3994 | FunctionDecl *Declaration, |
Kaelyn Uhrain | 4d9d157 | 2011-08-04 17:40:00 +0000 | [diff] [blame] | 3995 | FunctionDecl *Definition, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 3996 | SmallVectorImpl<unsigned> &Params) { |
Kaelyn Uhrain | 4d9d157 | 2011-08-04 17:40:00 +0000 | [diff] [blame] | 3997 | Params.clear(); |
Douglas Gregor | 584049d | 2008-12-15 23:53:10 +0000 | [diff] [blame] | 3998 | if (Declaration->param_size() != Definition->param_size()) |
| 3999 | return false; |
| 4000 | for (unsigned Idx = 0; Idx < Declaration->param_size(); ++Idx) { |
| 4001 | QualType DeclParamTy = Declaration->getParamDecl(Idx)->getType(); |
| 4002 | QualType DefParamTy = Definition->getParamDecl(Idx)->getType(); |
| 4003 | |
Kaelyn Uhrain | 4d9d157 | 2011-08-04 17:40:00 +0000 | [diff] [blame] | 4004 | // The parameter types are identical |
Matt Beaumont-Gay | 903d6dc | 2011-08-23 01:35:51 +0000 | [diff] [blame] | 4005 | if (Context.hasSameType(DefParamTy, DeclParamTy)) |
Kaelyn Uhrain | 4d9d157 | 2011-08-04 17:40:00 +0000 | [diff] [blame] | 4006 | continue; |
| 4007 | |
| 4008 | QualType DeclParamBaseTy = getCoreType(DeclParamTy); |
| 4009 | QualType DefParamBaseTy = getCoreType(DefParamTy); |
| 4010 | const IdentifierInfo *DeclTyName = DeclParamBaseTy.getBaseTypeIdentifier(); |
| 4011 | const IdentifierInfo *DefTyName = DefParamBaseTy.getBaseTypeIdentifier(); |
| 4012 | |
| 4013 | if (Context.hasSameUnqualifiedType(DeclParamBaseTy, DefParamBaseTy) || |
| 4014 | (DeclTyName && DeclTyName == DefTyName)) |
| 4015 | Params.push_back(Idx); |
| 4016 | else // The two parameters aren't even close |
Douglas Gregor | 584049d | 2008-12-15 23:53:10 +0000 | [diff] [blame] | 4017 | return false; |
| 4018 | } |
| 4019 | |
| 4020 | return true; |
| 4021 | } |
| 4022 | |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 4023 | /// NeedsRebuildingInCurrentInstantiation - Checks whether the given |
| 4024 | /// declarator needs to be rebuilt in the current instantiation. |
| 4025 | /// Any bits of declarator which appear before the name are valid for |
| 4026 | /// consideration here. That's specifically the type in the decl spec |
| 4027 | /// and the base type in any member-pointer chunks. |
| 4028 | static bool RebuildDeclaratorInCurrentInstantiation(Sema &S, Declarator &D, |
| 4029 | DeclarationName Name) { |
| 4030 | // The types we specifically need to rebuild are: |
| 4031 | // - typenames, typeofs, and decltypes |
| 4032 | // - types which will become injected class names |
| 4033 | // Of course, we also need to rebuild any type referencing such a |
| 4034 | // type. It's safest to just say "dependent", but we call out a |
| 4035 | // few cases here. |
| 4036 | |
| 4037 | DeclSpec &DS = D.getMutableDeclSpec(); |
| 4038 | switch (DS.getTypeSpecType()) { |
| 4039 | case DeclSpec::TST_typename: |
| 4040 | case DeclSpec::TST_typeofType: |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4041 | case DeclSpec::TST_underlyingType: |
| 4042 | case DeclSpec::TST_atomic: { |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 4043 | // Grab the type from the parser. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 4044 | TypeSourceInfo *TSI = nullptr; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4045 | QualType T = S.GetTypeFromParser(DS.getRepAsType(), &TSI); |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 4046 | if (T.isNull() || !T->isDependentType()) break; |
| 4047 | |
| 4048 | // Make sure there's a type source info. This isn't really much |
| 4049 | // of a waste; most dependent types should have type source info |
| 4050 | // attached already. |
| 4051 | if (!TSI) |
| 4052 | TSI = S.Context.getTrivialTypeSourceInfo(T, DS.getTypeSpecTypeLoc()); |
| 4053 | |
| 4054 | // Rebuild the type in the current instantiation. |
| 4055 | TSI = S.RebuildTypeInCurrentInstantiation(TSI, D.getIdentifierLoc(), Name); |
| 4056 | if (!TSI) return true; |
| 4057 | |
| 4058 | // Store the new type back in the decl spec. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4059 | ParsedType LocType = S.CreateParsedType(TSI->getType(), TSI); |
| 4060 | DS.UpdateTypeRep(LocType); |
| 4061 | break; |
| 4062 | } |
| 4063 | |
Richard Smith | c4a8391 | 2012-10-01 20:35:07 +0000 | [diff] [blame] | 4064 | case DeclSpec::TST_decltype: |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4065 | case DeclSpec::TST_typeofExpr: { |
| 4066 | Expr *E = DS.getRepAsExpr(); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4067 | ExprResult Result = S.RebuildExprInCurrentInstantiation(E); |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4068 | if (Result.isInvalid()) return true; |
| 4069 | DS.UpdateExprRep(Result.get()); |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 4070 | break; |
| 4071 | } |
| 4072 | |
| 4073 | default: |
| 4074 | // Nothing to do for these decl specs. |
| 4075 | break; |
| 4076 | } |
| 4077 | |
| 4078 | // It doesn't matter what order we do this in. |
| 4079 | for (unsigned I = 0, E = D.getNumTypeObjects(); I != E; ++I) { |
| 4080 | DeclaratorChunk &Chunk = D.getTypeObject(I); |
| 4081 | |
| 4082 | // The only type information in the declarator which can come |
| 4083 | // before the declaration name is the base type of a member |
| 4084 | // pointer. |
| 4085 | if (Chunk.Kind != DeclaratorChunk::MemberPointer) |
| 4086 | continue; |
| 4087 | |
| 4088 | // Rebuild the scope specifier in-place. |
| 4089 | CXXScopeSpec &SS = Chunk.Mem.Scope(); |
| 4090 | if (S.RebuildNestedNameSpecifierInCurrentInstantiation(SS)) |
| 4091 | return true; |
| 4092 | } |
| 4093 | |
| 4094 | return false; |
| 4095 | } |
| 4096 | |
Anders Carlsson | 3242ee0 | 2011-07-04 16:28:17 +0000 | [diff] [blame] | 4097 | Decl *Sema::ActOnDeclarator(Scope *S, Declarator &D) { |
Douglas Gregor | 45fa560 | 2011-11-07 20:56:01 +0000 | [diff] [blame] | 4098 | D.setFunctionDefinitionKind(FDK_Declaration); |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 4099 | Decl *Dcl = HandleDeclarator(S, D, MultiTemplateParamsArg()); |
Argyrios Kyrtzidis | c14a03d | 2011-11-23 20:27:36 +0000 | [diff] [blame] | 4100 | |
| 4101 | if (OriginalLexicalContext && OriginalLexicalContext->isObjCContainer() && |
Douglas Gregor | e7be109 | 2012-04-30 18:13:01 +0000 | [diff] [blame] | 4102 | Dcl && Dcl->getDeclContext()->isFileContext()) |
Argyrios Kyrtzidis | c14a03d | 2011-11-23 20:27:36 +0000 | [diff] [blame] | 4103 | Dcl->setTopLevelDeclInObjCContainer(); |
| 4104 | |
| 4105 | return Dcl; |
John McCall | 7cd088e | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 4106 | } |
| 4107 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4108 | /// DiagnoseClassNameShadow - Implement C++ [class.mem]p13: |
| 4109 | /// If T is the name of a class, then each of the following shall have a |
| 4110 | /// name different from T: |
| 4111 | /// - every static data member of class T; |
| 4112 | /// - every member function of class T |
| 4113 | /// - every member of class T that is itself a type; |
| 4114 | /// \returns true if the declaration name violates these rules. |
| 4115 | bool Sema::DiagnoseClassNameShadow(DeclContext *DC, |
| 4116 | DeclarationNameInfo NameInfo) { |
| 4117 | DeclarationName Name = NameInfo.getName(); |
| 4118 | |
| 4119 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(DC)) |
| 4120 | if (Record->getIdentifier() && Record->getDeclName() == Name) { |
| 4121 | Diag(NameInfo.getLoc(), diag::err_member_name_of_class) << Name; |
| 4122 | return true; |
| 4123 | } |
| 4124 | |
| 4125 | return false; |
| 4126 | } |
Douglas Gregor | 42acead | 2012-03-17 23:06:31 +0000 | [diff] [blame] | 4127 | |
Douglas Gregor | 6960587 | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 4128 | /// \brief Diagnose a declaration whose declarator-id has the given |
| 4129 | /// nested-name-specifier. |
| 4130 | /// |
| 4131 | /// \param SS The nested-name-specifier of the declarator-id. |
| 4132 | /// |
| 4133 | /// \param DC The declaration context to which the nested-name-specifier |
| 4134 | /// resolves. |
| 4135 | /// |
| 4136 | /// \param Name The name of the entity being declared. |
| 4137 | /// |
| 4138 | /// \param Loc The location of the name of the entity being declared. |
Douglas Gregor | 42acead | 2012-03-17 23:06:31 +0000 | [diff] [blame] | 4139 | /// |
| 4140 | /// \returns true if we cannot safely recover from this error, false otherwise. |
Douglas Gregor | 6960587 | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 4141 | bool Sema::diagnoseQualifiedDeclaration(CXXScopeSpec &SS, DeclContext *DC, |
Douglas Gregor | 42acead | 2012-03-17 23:06:31 +0000 | [diff] [blame] | 4142 | DeclarationName Name, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4143 | SourceLocation Loc) { |
Douglas Gregor | 6960587 | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 4144 | DeclContext *Cur = CurContext; |
Eli Friedman | a03c5ee | 2013-08-12 21:54:01 +0000 | [diff] [blame] | 4145 | while (isa<LinkageSpecDecl>(Cur) || isa<CapturedDecl>(Cur)) |
Douglas Gregor | 6960587 | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 4146 | Cur = Cur->getParent(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4147 | |
| 4148 | // If the user provided a superfluous scope specifier that refers back to the |
| 4149 | // class in which the entity is already declared, diagnose and ignore it. |
Douglas Gregor | 42acead | 2012-03-17 23:06:31 +0000 | [diff] [blame] | 4150 | // |
| 4151 | // class X { |
| 4152 | // void X::f(); |
| 4153 | // }; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4154 | // |
| 4155 | // Note, it was once ill-formed to give redundant qualification in all |
| 4156 | // contexts, but that rule was removed by DR482. |
Douglas Gregor | 6960587 | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 4157 | if (Cur->Equals(DC)) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4158 | if (Cur->isRecord()) { |
| 4159 | Diag(Loc, LangOpts.MicrosoftExt ? diag::warn_member_extra_qualification |
| 4160 | : diag::err_member_extra_qualification) |
| 4161 | << Name << FixItHint::CreateRemoval(SS.getRange()); |
| 4162 | SS.clear(); |
| 4163 | } else { |
| 4164 | Diag(Loc, diag::warn_namespace_member_extra_qualification) << Name; |
| 4165 | } |
Douglas Gregor | 42acead | 2012-03-17 23:06:31 +0000 | [diff] [blame] | 4166 | return false; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4167 | } |
Douglas Gregor | 6960587 | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 4168 | |
| 4169 | // Check whether the qualifying scope encloses the scope of the original |
| 4170 | // declaration. |
| 4171 | if (!Cur->Encloses(DC)) { |
| 4172 | if (Cur->isRecord()) |
| 4173 | Diag(Loc, diag::err_member_qualification) |
| 4174 | << Name << SS.getRange(); |
| 4175 | else if (isa<TranslationUnitDecl>(DC)) |
| 4176 | Diag(Loc, diag::err_invalid_declarator_global_scope) |
| 4177 | << Name << SS.getRange(); |
| 4178 | else if (isa<FunctionDecl>(Cur)) |
| 4179 | Diag(Loc, diag::err_invalid_declarator_in_function) |
| 4180 | << Name << SS.getRange(); |
Eli Friedman | a03c5ee | 2013-08-12 21:54:01 +0000 | [diff] [blame] | 4181 | else if (isa<BlockDecl>(Cur)) |
| 4182 | Diag(Loc, diag::err_invalid_declarator_in_block) |
| 4183 | << Name << SS.getRange(); |
Douglas Gregor | 6960587 | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 4184 | else |
| 4185 | Diag(Loc, diag::err_invalid_declarator_scope) |
Richard Smith | a1c4f7c | 2012-04-13 04:07:40 +0000 | [diff] [blame] | 4186 | << Name << cast<NamedDecl>(Cur) << cast<NamedDecl>(DC) << SS.getRange(); |
Douglas Gregor | 6960587 | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 4187 | |
Douglas Gregor | 42acead | 2012-03-17 23:06:31 +0000 | [diff] [blame] | 4188 | return true; |
Douglas Gregor | 6960587 | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 4189 | } |
| 4190 | |
| 4191 | if (Cur->isRecord()) { |
| 4192 | // Cannot qualify members within a class. |
| 4193 | Diag(Loc, diag::err_member_qualification) |
| 4194 | << Name << SS.getRange(); |
| 4195 | SS.clear(); |
| 4196 | |
| 4197 | // C++ constructors and destructors with incorrect scopes can break |
| 4198 | // our AST invariants by having the wrong underlying types. If |
| 4199 | // that's the case, then drop this declaration entirely. |
| 4200 | if ((Name.getNameKind() == DeclarationName::CXXConstructorName || |
| 4201 | Name.getNameKind() == DeclarationName::CXXDestructorName) && |
| 4202 | !Context.hasSameType(Name.getCXXNameType(), |
| 4203 | Context.getTypeDeclType(cast<CXXRecordDecl>(Cur)))) |
| 4204 | return true; |
| 4205 | |
| 4206 | return false; |
| 4207 | } |
Douglas Gregor | 42acead | 2012-03-17 23:06:31 +0000 | [diff] [blame] | 4208 | |
Douglas Gregor | 6960587 | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 4209 | // C++11 [dcl.meaning]p1: |
| 4210 | // [...] "The nested-name-specifier of the qualified declarator-id shall |
| 4211 | // not begin with a decltype-specifer" |
| 4212 | NestedNameSpecifierLoc SpecLoc(SS.getScopeRep(), SS.location_data()); |
| 4213 | while (SpecLoc.getPrefix()) |
| 4214 | SpecLoc = SpecLoc.getPrefix(); |
| 4215 | if (dyn_cast_or_null<DecltypeType>( |
| 4216 | SpecLoc.getNestedNameSpecifier()->getAsType())) |
| 4217 | Diag(Loc, diag::err_decltype_in_declarator) |
| 4218 | << SpecLoc.getTypeLoc().getSourceRange(); |
| 4219 | |
Douglas Gregor | 42acead | 2012-03-17 23:06:31 +0000 | [diff] [blame] | 4220 | return false; |
| 4221 | } |
| 4222 | |
Rafael Espindola | fc35cbc | 2013-01-08 20:44:06 +0000 | [diff] [blame] | 4223 | NamedDecl *Sema::HandleDeclarator(Scope *S, Declarator &D, |
| 4224 | MultiTemplateParamsArg TemplateParamLists) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4225 | // TODO: consider using NameInfo for diagnostic. |
| 4226 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); |
| 4227 | DeclarationName Name = NameInfo.getName(); |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 4228 | |
Chris Lattner | e80a59c | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4229 | // All of these full declarators require an identifier. If it doesn't have |
| 4230 | // one, the ParsedFreeStandingDeclSpec action should be used. |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 4231 | if (!Name) { |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 4232 | if (!D.isInvalidType()) // Reject this if we think it is valid. |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4233 | Diag(D.getDeclSpec().getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 4234 | diag::err_declarator_need_ident) |
| 4235 | << D.getDeclSpec().getSourceRange() << D.getSourceRange(); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 4236 | return nullptr; |
Douglas Gregor | 56c0458 | 2010-12-16 00:46:58 +0000 | [diff] [blame] | 4237 | } else if (DiagnoseUnexpandedParameterPack(NameInfo, UPPC_DeclarationType)) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 4238 | return nullptr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4239 | |
Chris Lattner | 31e0572 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 4240 | // The scope passed in may not be a decl scope. Zip up the scope tree until |
| 4241 | // we find one that is. |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 4242 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 4243 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
Chris Lattner | 31e0572 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 4244 | S = S->getParent(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4245 | |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 4246 | DeclContext *DC = CurContext; |
| 4247 | if (D.getCXXScopeSpec().isInvalid()) |
| 4248 | D.setInvalidType(); |
| 4249 | else if (D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | 6ccab97 | 2010-12-16 01:14:37 +0000 | [diff] [blame] | 4250 | if (DiagnoseUnexpandedParameterPack(D.getCXXScopeSpec(), |
| 4251 | UPPC_DeclarationQualifier)) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 4252 | return nullptr; |
Douglas Gregor | 6ccab97 | 2010-12-16 01:14:37 +0000 | [diff] [blame] | 4253 | |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 4254 | bool EnteringContext = !D.getDeclSpec().isFriendSpecified(); |
| 4255 | DC = computeDeclContext(D.getCXXScopeSpec(), EnteringContext); |
Bill Wendling | 13303b9 | 2013-11-26 04:09:45 +0000 | [diff] [blame] | 4256 | if (!DC || isa<EnumDecl>(DC)) { |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 4257 | // If we could not compute the declaration context, it's because the |
| 4258 | // declaration context is dependent but does not refer to a class, |
| 4259 | // class template, or class template partial specialization. Complain |
| 4260 | // and return early, to avoid the coming semantic disaster. |
| 4261 | Diag(D.getIdentifierLoc(), |
| 4262 | diag::err_template_qualified_declarator_no_match) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4263 | << D.getCXXScopeSpec().getScopeRep() |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 4264 | << D.getCXXScopeSpec().getRange(); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 4265 | return nullptr; |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 4266 | } |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 4267 | bool IsDependentContext = DC->isDependentContext(); |
John McCall | 0dd7ceb | 2009-12-19 09:35:56 +0000 | [diff] [blame] | 4268 | |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 4269 | if (!IsDependentContext && |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 4270 | RequireCompleteDeclContext(D.getCXXScopeSpec(), DC)) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 4271 | return nullptr; |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 4272 | |
Douglas Gregor | 6960587 | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 4273 | if (isa<CXXRecordDecl>(DC) && !cast<CXXRecordDecl>(DC)->hasDefinition()) { |
| 4274 | Diag(D.getIdentifierLoc(), |
| 4275 | diag::err_member_def_undefined_record) |
| 4276 | << Name << DC << D.getCXXScopeSpec().getRange(); |
| 4277 | D.setInvalidType(); |
| 4278 | } else if (!D.getDeclSpec().isFriendSpecified()) { |
| 4279 | if (diagnoseQualifiedDeclaration(D.getCXXScopeSpec(), DC, |
| 4280 | Name, D.getIdentifierLoc())) { |
| 4281 | if (DC->isRecord()) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 4282 | return nullptr; |
| 4283 | |
Douglas Gregor | 6960587 | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 4284 | D.setInvalidType(); |
Douglas Gregor | 922fff2 | 2010-10-13 22:19:53 +0000 | [diff] [blame] | 4285 | } |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 4286 | } |
| 4287 | |
| 4288 | // Check whether we need to rebuild the type of the given |
| 4289 | // declaration in the current instantiation. |
| 4290 | if (EnteringContext && IsDependentContext && |
| 4291 | TemplateParamLists.size() != 0) { |
| 4292 | ContextRAII SavedContext(*this, DC); |
| 4293 | if (RebuildDeclaratorInCurrentInstantiation(*this, D, Name)) |
| 4294 | D.setInvalidType(); |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 4295 | } |
| 4296 | } |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4297 | |
| 4298 | if (DiagnoseClassNameShadow(DC, NameInfo)) |
| 4299 | // If this is a typedef, we'll end up spewing multiple diagnostics. |
| 4300 | // Just return early; it's safer. |
| 4301 | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 4302 | return nullptr; |
| 4303 | |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 4304 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
| 4305 | QualType R = TInfo->getType(); |
Douglas Gregor | d6f7e9d | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 4306 | |
Douglas Gregor | d093722 | 2010-12-13 22:49:22 +0000 | [diff] [blame] | 4307 | if (DiagnoseUnexpandedParameterPack(D.getIdentifierLoc(), TInfo, |
| 4308 | UPPC_DeclarationType)) |
| 4309 | D.setInvalidType(); |
| 4310 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4311 | LookupResult Previous(*this, NameInfo, LookupOrdinaryName, |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4312 | ForRedeclaration); |
| 4313 | |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 4314 | // See if this is a redefinition of a variable in the same scope. |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 4315 | if (!D.getCXXScopeSpec().isSet()) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4316 | bool IsLinkageLookup = false; |
Richard Smith | dd9459f | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 4317 | bool CreateBuiltins = false; |
Douglas Gregor | d6f7e9d | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 4318 | |
| 4319 | // If the declaration we're planning to build will be a function |
| 4320 | // or object with linkage, then look for another declaration with |
| 4321 | // linkage (C99 6.2.2p4-5 and C++ [basic.link]p6). |
Richard Smith | dd9459f | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 4322 | // |
| 4323 | // If the declaration we're planning to build will be declared with |
| 4324 | // external linkage in the translation unit, create any builtin with |
| 4325 | // the same name. |
Douglas Gregor | d6f7e9d | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 4326 | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) |
| 4327 | /* Do nothing*/; |
Richard Smith | dd9459f | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 4328 | else if (CurContext->isFunctionOrMethod() && |
| 4329 | (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_extern || |
| 4330 | R->isFunctionType())) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4331 | IsLinkageLookup = true; |
Richard Smith | dd9459f | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 4332 | CreateBuiltins = |
| 4333 | CurContext->getEnclosingNamespaceContext()->isTranslationUnit(); |
| 4334 | } else if (CurContext->getRedeclContext()->isTranslationUnit() && |
| 4335 | D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_static) |
| 4336 | CreateBuiltins = true; |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4337 | |
| 4338 | if (IsLinkageLookup) |
| 4339 | Previous.clear(LookupRedeclarationWithLinkage); |
Douglas Gregor | d6f7e9d | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 4340 | |
Richard Smith | dd9459f | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 4341 | LookupName(Previous, S, CreateBuiltins); |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 4342 | } else { // Something like "int foo::x;" |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4343 | LookupQualifiedName(Previous, DC); |
| 4344 | |
Douglas Gregor | 6960587 | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 4345 | // C++ [dcl.meaning]p1: |
| 4346 | // When the declarator-id is qualified, the declaration shall refer to a |
| 4347 | // previously declared member of the class or namespace to which the |
| 4348 | // qualifier refers (or, in the case of a namespace, of an element of the |
| 4349 | // inline namespace set of that namespace (7.3.1)) or to a specialization |
| 4350 | // thereof; [...] |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 4351 | // |
Douglas Gregor | 6960587 | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 4352 | // Note that we already checked the context above, and that we do not have |
| 4353 | // enough information to make sure that Previous contains the declaration |
| 4354 | // we want to match. For example, given: |
Douglas Gregor | 584049d | 2008-12-15 23:53:10 +0000 | [diff] [blame] | 4355 | // |
Douglas Gregor | 9d35097 | 2008-12-12 08:25:50 +0000 | [diff] [blame] | 4356 | // class X { |
| 4357 | // void f(); |
Douglas Gregor | 584049d | 2008-12-15 23:53:10 +0000 | [diff] [blame] | 4358 | // void f(float); |
Douglas Gregor | 9d35097 | 2008-12-12 08:25:50 +0000 | [diff] [blame] | 4359 | // }; |
| 4360 | // |
Douglas Gregor | 584049d | 2008-12-15 23:53:10 +0000 | [diff] [blame] | 4361 | // void X::f(int) { } // ill-formed |
| 4362 | // |
Douglas Gregor | 6960587 | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 4363 | // In this case, Previous will point to the overload set |
Douglas Gregor | 584049d | 2008-12-15 23:53:10 +0000 | [diff] [blame] | 4364 | // containing the two f's declared in X, but neither of them |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4365 | // matches. |
Douglas Gregor | 6960587 | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 4366 | |
| 4367 | // C++ [dcl.meaning]p1: |
| 4368 | // [...] the member shall not merely have been introduced by a |
| 4369 | // using-declaration in the scope of the class or namespace nominated by |
| 4370 | // the nested-name-specifier of the declarator-id. |
| 4371 | RemoveUsingDecls(Previous); |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 4372 | } |
| 4373 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4374 | if (Previous.isSingleResult() && |
| 4375 | Previous.getFoundDecl()->isTemplateParameter()) { |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 4376 | // Maybe we will complain about the shadowed template parameter. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4377 | if (!D.isInvalidType()) |
Douglas Gregor | cb8f951 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 4378 | DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), |
| 4379 | Previous.getFoundDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4380 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 4381 | // Just pretend that we didn't see the previous declaration. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4382 | Previous.clear(); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 4383 | } |
| 4384 | |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 4385 | // In C++, the previous declaration we find might be a tag type |
| 4386 | // (class or enum). In this case, the new declaration will hide the |
Douglas Gregor | 6697312 | 2009-01-28 17:15:10 +0000 | [diff] [blame] | 4387 | // tag type. Note that this does does not apply if we're declaring a |
| 4388 | // typedef (C++ [dcl.typedef]p4). |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4389 | if (Previous.isSingleTagDecl() && |
Douglas Gregor | 6697312 | 2009-01-28 17:15:10 +0000 | [diff] [blame] | 4390 | D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef) |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4391 | Previous.clear(); |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 4392 | |
Richard Smith | 3cdbbdc | 2013-03-06 01:37:38 +0000 | [diff] [blame] | 4393 | // Check that there are no default arguments other than in the parameters |
| 4394 | // of a function declaration (C++ only). |
| 4395 | if (getLangOpts().CPlusPlus) |
| 4396 | CheckExtraCXXDefaultArguments(D); |
| 4397 | |
Nico Weber | e6bb76c | 2012-12-23 00:40:46 +0000 | [diff] [blame] | 4398 | NamedDecl *New; |
| 4399 | |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 4400 | bool AddToScope = true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4401 | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) { |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 4402 | if (TemplateParamLists.size()) { |
| 4403 | Diag(D.getIdentifierLoc(), diag::err_template_typedef); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 4404 | return nullptr; |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 4405 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4406 | |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 4407 | New = ActOnTypedefDeclarator(S, D, DC, TInfo, Previous); |
Douglas Gregor | d6f7e9d | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 4408 | } else if (R->isFunctionType()) { |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 4409 | New = ActOnFunctionDeclarator(S, D, DC, TInfo, Previous, |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 4410 | TemplateParamLists, |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 4411 | AddToScope); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4412 | } else { |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4413 | New = ActOnVariableDeclarator(S, D, DC, TInfo, Previous, TemplateParamLists, |
| 4414 | AddToScope); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4415 | } |
Zhongxing Xu | 416fcaf | 2009-01-16 01:13:29 +0000 | [diff] [blame] | 4416 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 4417 | if (!New) |
| 4418 | return nullptr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4419 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4420 | // If this has an identifier and is not an invalid redeclaration or |
| 4421 | // function template specialization, add it to the scope stack. |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 4422 | if (New->getDeclName() && AddToScope && |
Richard Smith | a41c97a | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 4423 | !(D.isRedeclaration() && New->isInvalidDecl())) { |
| 4424 | // Only make a locally-scoped extern declaration visible if it is the first |
| 4425 | // declaration of this entity. Qualified lookup for such an entity should |
| 4426 | // only find this declaration if there is no visible declaration of it. |
| 4427 | bool AddToContext = !D.isRedeclaration() || !New->isLocalExternDecl(); |
| 4428 | PushOnScopeChains(New, S, AddToContext); |
| 4429 | if (!AddToContext) |
| 4430 | CurContext->addHiddenDecl(New); |
| 4431 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4432 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4433 | return New; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4434 | } |
| 4435 | |
Abramo Bagnara | 88adb98 | 2012-11-08 16:27:30 +0000 | [diff] [blame] | 4436 | /// Helper method to turn variable array types into constant array |
| 4437 | /// types in certain situations which would otherwise be errors (for |
| 4438 | /// GCC compatibility). |
Eli Friedman | 1ca4813 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 4439 | static QualType TryToFixInvalidVariablyModifiedType(QualType T, |
| 4440 | ASTContext &Context, |
Douglas Gregor | 2767ce2 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 4441 | bool &SizeIsNegative, |
| 4442 | llvm::APSInt &Oversized) { |
Eli Friedman | 1ca4813 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 4443 | // This method tries to turn a variable array into a constant |
| 4444 | // array even when the size isn't an ICE. This is necessary |
| 4445 | // for compatibility with code that depends on gcc's buggy |
| 4446 | // constant expression folding, like struct {char x[(int)(char*)2];} |
| 4447 | SizeIsNegative = false; |
Douglas Gregor | 2767ce2 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 4448 | Oversized = 0; |
| 4449 | |
| 4450 | if (T->isDependentType()) |
| 4451 | return QualType(); |
| 4452 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4453 | QualifierCollector Qs; |
| 4454 | const Type *Ty = Qs.strip(T); |
| 4455 | |
| 4456 | if (const PointerType* PTy = dyn_cast<PointerType>(Ty)) { |
Eli Friedman | 1ca4813 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 4457 | QualType Pointee = PTy->getPointeeType(); |
| 4458 | QualType FixedType = |
Douglas Gregor | 2767ce2 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 4459 | TryToFixInvalidVariablyModifiedType(Pointee, Context, SizeIsNegative, |
| 4460 | Oversized); |
Eli Friedman | 1ca4813 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 4461 | if (FixedType.isNull()) return FixedType; |
Eli Friedman | 61125c8 | 2009-02-21 00:58:02 +0000 | [diff] [blame] | 4462 | FixedType = Context.getPointerType(FixedType); |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 4463 | return Qs.apply(Context, FixedType); |
Eli Friedman | 1ca4813 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 4464 | } |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 4465 | if (const ParenType* PTy = dyn_cast<ParenType>(Ty)) { |
| 4466 | QualType Inner = PTy->getInnerType(); |
| 4467 | QualType FixedType = |
| 4468 | TryToFixInvalidVariablyModifiedType(Inner, Context, SizeIsNegative, |
| 4469 | Oversized); |
| 4470 | if (FixedType.isNull()) return FixedType; |
| 4471 | FixedType = Context.getParenType(FixedType); |
| 4472 | return Qs.apply(Context, FixedType); |
| 4473 | } |
Eli Friedman | 1ca4813 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 4474 | |
| 4475 | const VariableArrayType* VLATy = dyn_cast<VariableArrayType>(T); |
Eli Friedman | bc592e6 | 2009-02-26 03:58:54 +0000 | [diff] [blame] | 4476 | if (!VLATy) |
| 4477 | return QualType(); |
| 4478 | // FIXME: We should probably handle this case |
| 4479 | if (VLATy->getElementType()->isVariablyModifiedType()) |
| 4480 | return QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4481 | |
Richard Smith | aa9c350 | 2011-12-07 00:43:50 +0000 | [diff] [blame] | 4482 | llvm::APSInt Res; |
Eli Friedman | 1ca4813 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 4483 | if (!VLATy->getSizeExpr() || |
Richard Smith | aa9c350 | 2011-12-07 00:43:50 +0000 | [diff] [blame] | 4484 | !VLATy->getSizeExpr()->EvaluateAsInt(Res, Context)) |
Eli Friedman | 1ca4813 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 4485 | return QualType(); |
Eli Friedman | bc592e6 | 2009-02-26 03:58:54 +0000 | [diff] [blame] | 4486 | |
Douglas Gregor | 2767ce2 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 4487 | // Check whether the array size is negative. |
Douglas Gregor | 2767ce2 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 4488 | if (Res.isSigned() && Res.isNegative()) { |
| 4489 | SizeIsNegative = true; |
| 4490 | return QualType(); |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 4491 | } |
Eli Friedman | 1ca4813 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 4492 | |
Douglas Gregor | 2767ce2 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 4493 | // Check whether the array is too large to be addressed. |
| 4494 | unsigned ActiveSizeBits |
| 4495 | = ConstantArrayType::getNumAddressingBits(Context, VLATy->getElementType(), |
| 4496 | Res); |
| 4497 | if (ActiveSizeBits > ConstantArrayType::getMaxSizeBits(Context)) { |
| 4498 | Oversized = Res; |
| 4499 | return QualType(); |
| 4500 | } |
| 4501 | |
| 4502 | return Context.getConstantArrayType(VLATy->getElementType(), |
| 4503 | Res, ArrayType::Normal, 0); |
Eli Friedman | 1ca4813 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 4504 | } |
| 4505 | |
Abramo Bagnara | 4c5750e | 2012-11-08 14:44:42 +0000 | [diff] [blame] | 4506 | static void |
| 4507 | FixInvalidVariablyModifiedTypeLoc(TypeLoc SrcTL, TypeLoc DstTL) { |
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 4508 | if (PointerTypeLoc SrcPTL = SrcTL.getAs<PointerTypeLoc>()) { |
| 4509 | PointerTypeLoc DstPTL = DstTL.castAs<PointerTypeLoc>(); |
| 4510 | FixInvalidVariablyModifiedTypeLoc(SrcPTL.getPointeeLoc(), |
| 4511 | DstPTL.getPointeeLoc()); |
| 4512 | DstPTL.setStarLoc(SrcPTL.getStarLoc()); |
Abramo Bagnara | 4c5750e | 2012-11-08 14:44:42 +0000 | [diff] [blame] | 4513 | return; |
| 4514 | } |
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 4515 | if (ParenTypeLoc SrcPTL = SrcTL.getAs<ParenTypeLoc>()) { |
| 4516 | ParenTypeLoc DstPTL = DstTL.castAs<ParenTypeLoc>(); |
| 4517 | FixInvalidVariablyModifiedTypeLoc(SrcPTL.getInnerLoc(), |
| 4518 | DstPTL.getInnerLoc()); |
| 4519 | DstPTL.setLParenLoc(SrcPTL.getLParenLoc()); |
| 4520 | DstPTL.setRParenLoc(SrcPTL.getRParenLoc()); |
Abramo Bagnara | 4c5750e | 2012-11-08 14:44:42 +0000 | [diff] [blame] | 4521 | return; |
| 4522 | } |
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 4523 | ArrayTypeLoc SrcATL = SrcTL.castAs<ArrayTypeLoc>(); |
| 4524 | ArrayTypeLoc DstATL = DstTL.castAs<ArrayTypeLoc>(); |
| 4525 | TypeLoc SrcElemTL = SrcATL.getElementLoc(); |
| 4526 | TypeLoc DstElemTL = DstATL.getElementLoc(); |
Abramo Bagnara | 4c5750e | 2012-11-08 14:44:42 +0000 | [diff] [blame] | 4527 | DstElemTL.initializeFullCopy(SrcElemTL); |
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 4528 | DstATL.setLBracketLoc(SrcATL.getLBracketLoc()); |
| 4529 | DstATL.setSizeExpr(SrcATL.getSizeExpr()); |
| 4530 | DstATL.setRBracketLoc(SrcATL.getRBracketLoc()); |
Abramo Bagnara | 4c5750e | 2012-11-08 14:44:42 +0000 | [diff] [blame] | 4531 | } |
| 4532 | |
Abramo Bagnara | 88adb98 | 2012-11-08 16:27:30 +0000 | [diff] [blame] | 4533 | /// Helper method to turn variable array types into constant array |
| 4534 | /// types in certain situations which would otherwise be errors (for |
| 4535 | /// GCC compatibility). |
Abramo Bagnara | 4c5750e | 2012-11-08 14:44:42 +0000 | [diff] [blame] | 4536 | static TypeSourceInfo* |
| 4537 | TryToFixInvalidVariablyModifiedTypeSourceInfo(TypeSourceInfo *TInfo, |
| 4538 | ASTContext &Context, |
| 4539 | bool &SizeIsNegative, |
| 4540 | llvm::APSInt &Oversized) { |
| 4541 | QualType FixedTy |
| 4542 | = TryToFixInvalidVariablyModifiedType(TInfo->getType(), Context, |
| 4543 | SizeIsNegative, Oversized); |
| 4544 | if (FixedTy.isNull()) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 4545 | return nullptr; |
Abramo Bagnara | 4c5750e | 2012-11-08 14:44:42 +0000 | [diff] [blame] | 4546 | TypeSourceInfo *FixedTInfo = Context.getTrivialTypeSourceInfo(FixedTy); |
| 4547 | FixInvalidVariablyModifiedTypeLoc(TInfo->getTypeLoc(), |
| 4548 | FixedTInfo->getTypeLoc()); |
| 4549 | return FixedTInfo; |
| 4550 | } |
| 4551 | |
Richard Smith | 5ea6ef4 | 2013-01-10 23:43:47 +0000 | [diff] [blame] | 4552 | /// \brief Register the given locally-scoped extern "C" declaration so |
Richard Smith | 662f41b | 2013-06-18 20:15:12 +0000 | [diff] [blame] | 4553 | /// that it can be found later for redeclarations. We include any extern "C" |
| 4554 | /// declaration that is not visible in the translation unit here, not just |
| 4555 | /// function-scope declarations. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4556 | void |
Richard Smith | 662f41b | 2013-06-18 20:15:12 +0000 | [diff] [blame] | 4557 | Sema::RegisterLocallyScopedExternCDecl(NamedDecl *ND, Scope *S) { |
Richard Smith | aa4bc18 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 4558 | if (!getLangOpts().CPlusPlus && |
| 4559 | ND->getLexicalDeclContext()->getRedeclContext()->isTranslationUnit()) |
| 4560 | // Don't need to track declarations in the TU in C. |
| 4561 | return; |
| 4562 | |
Douglas Gregor | 6393519 | 2009-03-02 00:19:53 +0000 | [diff] [blame] | 4563 | // Note that we have a locally-scoped external with this name. |
Richard Smith | aa4bc18 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 4564 | // FIXME: There can be multiple such declarations if they are functions marked |
| 4565 | // __attribute__((overloadable)) declared in function scope in C. |
Richard Smith | 5ea6ef4 | 2013-01-10 23:43:47 +0000 | [diff] [blame] | 4566 | LocallyScopedExternCDecls[ND->getDeclName()] = ND; |
Douglas Gregor | 6393519 | 2009-03-02 00:19:53 +0000 | [diff] [blame] | 4567 | } |
| 4568 | |
Richard Smith | 662f41b | 2013-06-18 20:15:12 +0000 | [diff] [blame] | 4569 | NamedDecl *Sema::findLocallyScopedExternCDecl(DeclarationName Name) { |
Douglas Gregor | ec12ce2 | 2011-07-28 14:20:37 +0000 | [diff] [blame] | 4570 | if (ExternalSource) { |
| 4571 | // Load locally-scoped external decls from the external source. |
Richard Smith | 662f41b | 2013-06-18 20:15:12 +0000 | [diff] [blame] | 4572 | // FIXME: This is inefficient. Maybe add a DeclContext for extern "C" decls? |
Douglas Gregor | ec12ce2 | 2011-07-28 14:20:37 +0000 | [diff] [blame] | 4573 | SmallVector<NamedDecl *, 4> Decls; |
Richard Smith | 5ea6ef4 | 2013-01-10 23:43:47 +0000 | [diff] [blame] | 4574 | ExternalSource->ReadLocallyScopedExternCDecls(Decls); |
Douglas Gregor | ec12ce2 | 2011-07-28 14:20:37 +0000 | [diff] [blame] | 4575 | for (unsigned I = 0, N = Decls.size(); I != N; ++I) { |
| 4576 | llvm::DenseMap<DeclarationName, NamedDecl *>::iterator Pos |
Richard Smith | 5ea6ef4 | 2013-01-10 23:43:47 +0000 | [diff] [blame] | 4577 | = LocallyScopedExternCDecls.find(Decls[I]->getDeclName()); |
| 4578 | if (Pos == LocallyScopedExternCDecls.end()) |
| 4579 | LocallyScopedExternCDecls[Decls[I]->getDeclName()] = Decls[I]; |
Douglas Gregor | ec12ce2 | 2011-07-28 14:20:37 +0000 | [diff] [blame] | 4580 | } |
| 4581 | } |
Richard Smith | 662f41b | 2013-06-18 20:15:12 +0000 | [diff] [blame] | 4582 | |
| 4583 | NamedDecl *D = LocallyScopedExternCDecls.lookup(Name); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 4584 | return D ? D->getMostRecentDecl() : nullptr; |
Douglas Gregor | ec12ce2 | 2011-07-28 14:20:37 +0000 | [diff] [blame] | 4585 | } |
| 4586 | |
Eli Friedman | 85a5319 | 2009-04-07 19:37:57 +0000 | [diff] [blame] | 4587 | /// \brief Diagnose function specifiers on a declaration of an identifier that |
| 4588 | /// does not identify a function. |
Richard Smith | c7f8116 | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 4589 | void Sema::DiagnoseFunctionSpecifiers(const DeclSpec &DS) { |
Eli Friedman | 85a5319 | 2009-04-07 19:37:57 +0000 | [diff] [blame] | 4590 | // FIXME: We should probably indicate the identifier in question to avoid |
| 4591 | // confusion for constructs like "inline int a(), b;" |
Richard Smith | c7f8116 | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 4592 | if (DS.isInlineSpecified()) |
| 4593 | Diag(DS.getInlineSpecLoc(), |
Eli Friedman | 85a5319 | 2009-04-07 19:37:57 +0000 | [diff] [blame] | 4594 | diag::err_inline_non_function); |
| 4595 | |
Richard Smith | c7f8116 | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 4596 | if (DS.isVirtualSpecified()) |
| 4597 | Diag(DS.getVirtualSpecLoc(), |
Eli Friedman | 85a5319 | 2009-04-07 19:37:57 +0000 | [diff] [blame] | 4598 | diag::err_virtual_non_function); |
| 4599 | |
Richard Smith | c7f8116 | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 4600 | if (DS.isExplicitSpecified()) |
| 4601 | Diag(DS.getExplicitSpecLoc(), |
Eli Friedman | 85a5319 | 2009-04-07 19:37:57 +0000 | [diff] [blame] | 4602 | diag::err_explicit_non_function); |
Richard Smith | de03c15 | 2013-01-17 22:16:11 +0000 | [diff] [blame] | 4603 | |
Richard Smith | c7f8116 | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 4604 | if (DS.isNoreturnSpecified()) |
| 4605 | Diag(DS.getNoreturnSpecLoc(), |
Richard Smith | de03c15 | 2013-01-17 22:16:11 +0000 | [diff] [blame] | 4606 | diag::err_noreturn_non_function); |
Eli Friedman | 85a5319 | 2009-04-07 19:37:57 +0000 | [diff] [blame] | 4607 | } |
| 4608 | |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 4609 | NamedDecl* |
Zhongxing Xu | d5ed8c3 | 2009-01-16 03:34:13 +0000 | [diff] [blame] | 4610 | Sema::ActOnTypedefDeclarator(Scope* S, Declarator& D, DeclContext* DC, |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 4611 | TypeSourceInfo *TInfo, LookupResult &Previous) { |
Zhongxing Xu | d5ed8c3 | 2009-01-16 03:34:13 +0000 | [diff] [blame] | 4612 | // Typedef declarators cannot be qualified (C++ [dcl.meaning]p1). |
| 4613 | if (D.getCXXScopeSpec().isSet()) { |
| 4614 | Diag(D.getIdentifierLoc(), diag::err_qualified_typedef_declarator) |
| 4615 | << D.getCXXScopeSpec().getRange(); |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 4616 | D.setInvalidType(); |
Zhongxing Xu | d5ed8c3 | 2009-01-16 03:34:13 +0000 | [diff] [blame] | 4617 | // Pretend we didn't see the scope specifier. |
Douglas Gregor | 9de672f | 2010-03-23 15:26:55 +0000 | [diff] [blame] | 4618 | DC = CurContext; |
| 4619 | Previous.clear(); |
Zhongxing Xu | d5ed8c3 | 2009-01-16 03:34:13 +0000 | [diff] [blame] | 4620 | } |
| 4621 | |
Richard Smith | c7f8116 | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 4622 | DiagnoseFunctionSpecifiers(D.getDeclSpec()); |
Eli Friedman | 85a5319 | 2009-04-07 19:37:57 +0000 | [diff] [blame] | 4623 | |
Richard Smith | af1fc7a | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 4624 | if (D.getDeclSpec().isConstexprSpecified()) |
| 4625 | Diag(D.getDeclSpec().getConstexprSpecLoc(), diag::err_invalid_constexpr) |
| 4626 | << 1; |
Eli Friedman | 63054b3 | 2009-04-19 20:27:55 +0000 | [diff] [blame] | 4627 | |
Douglas Gregor | aef0199 | 2010-07-13 06:37:01 +0000 | [diff] [blame] | 4628 | if (D.getName().Kind != UnqualifiedId::IK_Identifier) { |
| 4629 | Diag(D.getName().StartLocation, diag::err_typedef_not_identifier) |
| 4630 | << D.getName().getSourceRange(); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 4631 | return nullptr; |
Douglas Gregor | aef0199 | 2010-07-13 06:37:01 +0000 | [diff] [blame] | 4632 | } |
| 4633 | |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 4634 | TypedefDecl *NewTD = ParseTypedefDecl(S, D, TInfo->getType(), TInfo); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 4635 | if (!NewTD) return nullptr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4636 | |
Zhongxing Xu | d5ed8c3 | 2009-01-16 03:34:13 +0000 | [diff] [blame] | 4637 | // Handle attributes prior to checking for duplicates in MergeVarDecl |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 4638 | ProcessDeclAttributes(S, NewTD, D); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4639 | |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4640 | CheckTypedefForVariablyModifiedType(S, NewTD); |
| 4641 | |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 4642 | bool Redeclaration = D.isRedeclaration(); |
| 4643 | NamedDecl *ND = ActOnTypedefNameDecl(S, DC, NewTD, Previous, Redeclaration); |
| 4644 | D.setRedeclaration(Redeclaration); |
| 4645 | return ND; |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4646 | } |
| 4647 | |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4648 | void |
| 4649 | Sema::CheckTypedefForVariablyModifiedType(Scope *S, TypedefNameDecl *NewTD) { |
Chris Lattner | 38c5ebd | 2009-04-19 05:21:20 +0000 | [diff] [blame] | 4650 | // C99 6.7.7p2: If a typedef name specifies a variably modified type |
| 4651 | // then it shall have block scope. |
Eli Friedman | bf87f2c | 2010-08-10 03:13:15 +0000 | [diff] [blame] | 4652 | // Note that variably modified types must be fixed before merging the decl so |
| 4653 | // that redeclarations will match. |
Abramo Bagnara | 4c5750e | 2012-11-08 14:44:42 +0000 | [diff] [blame] | 4654 | TypeSourceInfo *TInfo = NewTD->getTypeSourceInfo(); |
| 4655 | QualType T = TInfo->getType(); |
Chris Lattner | 38c5ebd | 2009-04-19 05:21:20 +0000 | [diff] [blame] | 4656 | if (T->isVariablyModifiedType()) { |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 4657 | getCurFunction()->setHasBranchProtectedScope(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4658 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 4659 | if (S->getFnParent() == nullptr) { |
Eli Friedman | 1ca4813 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 4660 | bool SizeIsNegative; |
Douglas Gregor | 2767ce2 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 4661 | llvm::APSInt Oversized; |
Abramo Bagnara | 4c5750e | 2012-11-08 14:44:42 +0000 | [diff] [blame] | 4662 | TypeSourceInfo *FixedTInfo = |
| 4663 | TryToFixInvalidVariablyModifiedTypeSourceInfo(TInfo, Context, |
| 4664 | SizeIsNegative, |
| 4665 | Oversized); |
| 4666 | if (FixedTInfo) { |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4667 | Diag(NewTD->getLocation(), diag::warn_illegal_constant_array_size); |
Abramo Bagnara | 4c5750e | 2012-11-08 14:44:42 +0000 | [diff] [blame] | 4668 | NewTD->setTypeSourceInfo(FixedTInfo); |
Eli Friedman | 1ca4813 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 4669 | } else { |
| 4670 | if (SizeIsNegative) |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4671 | Diag(NewTD->getLocation(), diag::err_typecheck_negative_array_size); |
Eli Friedman | 1ca4813 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 4672 | else if (T->isVariableArrayType()) |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4673 | Diag(NewTD->getLocation(), diag::err_vla_decl_in_file_scope); |
Douglas Gregor | 2767ce2 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 4674 | else if (Oversized.getBoolValue()) |
David Blaikie | d662a79 | 2011-10-19 22:56:21 +0000 | [diff] [blame] | 4675 | Diag(NewTD->getLocation(), diag::err_array_too_large) |
| 4676 | << Oversized.toString(10); |
Eli Friedman | 1ca4813 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 4677 | else |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4678 | Diag(NewTD->getLocation(), diag::err_vm_decl_in_file_scope); |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 4679 | NewTD->setInvalidDecl(); |
Eli Friedman | 1ca4813 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 4680 | } |
Zhongxing Xu | d5ed8c3 | 2009-01-16 03:34:13 +0000 | [diff] [blame] | 4681 | } |
| 4682 | } |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4683 | } |
Douglas Gregor | c29f77b | 2009-07-07 16:35:42 +0000 | [diff] [blame] | 4684 | |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4685 | |
| 4686 | /// ActOnTypedefNameDecl - Perform semantic checking for a declaration which |
| 4687 | /// declares a typedef-name, either using the 'typedef' type specifier or via |
| 4688 | /// a C++0x [dcl.typedef]p2 alias-declaration: 'using T = A;'. |
| 4689 | NamedDecl* |
| 4690 | Sema::ActOnTypedefNameDecl(Scope *S, DeclContext *DC, TypedefNameDecl *NewTD, |
| 4691 | LookupResult &Previous, bool &Redeclaration) { |
Eli Friedman | bf87f2c | 2010-08-10 03:13:15 +0000 | [diff] [blame] | 4692 | // Merge the decl with the existing one if appropriate. If the decl is |
| 4693 | // in an outer scope, it isn't the same thing. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4694 | FilterLookupForScope(Previous, DC, S, /*ConsiderLinkage*/false, |
| 4695 | /*AllowInlineNamespace*/false); |
Douglas Gregor | 7dc80e1 | 2013-01-09 00:47:56 +0000 | [diff] [blame] | 4696 | filterNonConflictingPreviousDecls(Context, NewTD, Previous); |
Eli Friedman | bf87f2c | 2010-08-10 03:13:15 +0000 | [diff] [blame] | 4697 | if (!Previous.empty()) { |
| 4698 | Redeclaration = true; |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4699 | MergeTypedefNameDecl(NewTD, Previous); |
Eli Friedman | bf87f2c | 2010-08-10 03:13:15 +0000 | [diff] [blame] | 4700 | } |
| 4701 | |
Douglas Gregor | c29f77b | 2009-07-07 16:35:42 +0000 | [diff] [blame] | 4702 | // If this is the C FILE type, notify the AST context. |
| 4703 | if (IdentifierInfo *II = NewTD->getIdentifier()) |
| 4704 | if (!NewTD->isInvalidDecl() && |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 4705 | NewTD->getDeclContext()->getRedeclContext()->isTranslationUnit()) { |
Mike Stump | 782fa30 | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 4706 | if (II->isStr("FILE")) |
| 4707 | Context.setFILEDecl(NewTD); |
| 4708 | else if (II->isStr("jmp_buf")) |
| 4709 | Context.setjmp_bufDecl(NewTD); |
| 4710 | else if (II->isStr("sigjmp_buf")) |
| 4711 | Context.setsigjmp_bufDecl(NewTD); |
Rafael Espindola | e2d4f4e | 2011-11-13 21:51:09 +0000 | [diff] [blame] | 4712 | else if (II->isStr("ucontext_t")) |
| 4713 | Context.setucontext_tDecl(NewTD); |
Mike Stump | 782fa30 | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 4714 | } |
| 4715 | |
Zhongxing Xu | d5ed8c3 | 2009-01-16 03:34:13 +0000 | [diff] [blame] | 4716 | return NewTD; |
| 4717 | } |
| 4718 | |
Douglas Gregor | 8f30105 | 2009-02-24 19:23:27 +0000 | [diff] [blame] | 4719 | /// \brief Determines whether the given declaration is an out-of-scope |
| 4720 | /// previous declaration. |
| 4721 | /// |
| 4722 | /// This routine should be invoked when name lookup has found a |
| 4723 | /// previous declaration (PrevDecl) that is not in the scope where a |
| 4724 | /// new declaration by the same name is being introduced. If the new |
| 4725 | /// declaration occurs in a local scope, previous declarations with |
| 4726 | /// linkage may still be considered previous declarations (C99 |
| 4727 | /// 6.2.2p4-5, C++ [basic.link]p6). |
| 4728 | /// |
| 4729 | /// \param PrevDecl the previous declaration found by name |
| 4730 | /// lookup |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4731 | /// |
Douglas Gregor | 8f30105 | 2009-02-24 19:23:27 +0000 | [diff] [blame] | 4732 | /// \param DC the context in which the new declaration is being |
| 4733 | /// declared. |
| 4734 | /// |
| 4735 | /// \returns true if PrevDecl is an out-of-scope previous declaration |
| 4736 | /// for a new delcaration with the same name. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4737 | static bool |
Douglas Gregor | 8f30105 | 2009-02-24 19:23:27 +0000 | [diff] [blame] | 4738 | isOutOfScopePreviousDeclaration(NamedDecl *PrevDecl, DeclContext *DC, |
| 4739 | ASTContext &Context) { |
| 4740 | if (!PrevDecl) |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 4741 | return false; |
Douglas Gregor | 8f30105 | 2009-02-24 19:23:27 +0000 | [diff] [blame] | 4742 | |
Douglas Gregor | d6f7e9d | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 4743 | if (!PrevDecl->hasLinkage()) |
| 4744 | return false; |
Douglas Gregor | 8f30105 | 2009-02-24 19:23:27 +0000 | [diff] [blame] | 4745 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4746 | if (Context.getLangOpts().CPlusPlus) { |
Douglas Gregor | 8f30105 | 2009-02-24 19:23:27 +0000 | [diff] [blame] | 4747 | // C++ [basic.link]p6: |
| 4748 | // If there is a visible declaration of an entity with linkage |
| 4749 | // having the same name and type, ignoring entities declared |
| 4750 | // outside the innermost enclosing namespace scope, the block |
| 4751 | // scope declaration declares that same entity and receives the |
| 4752 | // linkage of the previous declaration. |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 4753 | DeclContext *OuterContext = DC->getRedeclContext(); |
Douglas Gregor | 8f30105 | 2009-02-24 19:23:27 +0000 | [diff] [blame] | 4754 | if (!OuterContext->isFunctionOrMethod()) |
| 4755 | // This rule only applies to block-scope declarations. |
| 4756 | return false; |
Douglas Gregor | 757c600 | 2010-08-27 22:55:10 +0000 | [diff] [blame] | 4757 | |
| 4758 | DeclContext *PrevOuterContext = PrevDecl->getDeclContext(); |
| 4759 | if (PrevOuterContext->isRecord()) |
| 4760 | // We found a member function: ignore it. |
| 4761 | return false; |
| 4762 | |
| 4763 | // Find the innermost enclosing namespace for the new and |
| 4764 | // previous declarations. |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 4765 | OuterContext = OuterContext->getEnclosingNamespaceContext(); |
| 4766 | PrevOuterContext = PrevOuterContext->getEnclosingNamespaceContext(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4767 | |
Douglas Gregor | 757c600 | 2010-08-27 22:55:10 +0000 | [diff] [blame] | 4768 | // The previous declaration is in a different namespace, so it |
| 4769 | // isn't the same function. |
| 4770 | if (!OuterContext->Equals(PrevOuterContext)) |
| 4771 | return false; |
Douglas Gregor | 8f30105 | 2009-02-24 19:23:27 +0000 | [diff] [blame] | 4772 | } |
| 4773 | |
Douglas Gregor | 8f30105 | 2009-02-24 19:23:27 +0000 | [diff] [blame] | 4774 | return true; |
| 4775 | } |
| 4776 | |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4777 | static void SetNestedNameSpecifier(DeclaratorDecl *DD, Declarator &D) { |
| 4778 | CXXScopeSpec &SS = D.getCXXScopeSpec(); |
| 4779 | if (!SS.isSet()) return; |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 4780 | DD->setQualifierInfo(SS.getWithLocInContext(DD->getASTContext())); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4781 | } |
| 4782 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4783 | bool Sema::inferObjCARCLifetime(ValueDecl *decl) { |
| 4784 | QualType type = decl->getType(); |
| 4785 | Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime(); |
| 4786 | if (lifetime == Qualifiers::OCL_Autoreleasing) { |
| 4787 | // Various kinds of declaration aren't allowed to be __autoreleasing. |
| 4788 | unsigned kind = -1U; |
| 4789 | if (VarDecl *var = dyn_cast<VarDecl>(decl)) { |
| 4790 | if (var->hasAttr<BlocksAttr>()) |
| 4791 | kind = 0; // __block |
| 4792 | else if (!var->hasLocalStorage()) |
| 4793 | kind = 1; // global |
| 4794 | } else if (isa<ObjCIvarDecl>(decl)) { |
| 4795 | kind = 3; // ivar |
| 4796 | } else if (isa<FieldDecl>(decl)) { |
| 4797 | kind = 2; // field |
| 4798 | } |
| 4799 | |
| 4800 | if (kind != -1U) { |
| 4801 | Diag(decl->getLocation(), diag::err_arc_autoreleasing_var) |
| 4802 | << kind; |
| 4803 | } |
| 4804 | } else if (lifetime == Qualifiers::OCL_None) { |
| 4805 | // Try to infer lifetime. |
| 4806 | if (!type->isObjCLifetimeType()) |
| 4807 | return false; |
| 4808 | |
| 4809 | lifetime = type->getObjCARCImplicitLifetime(); |
| 4810 | type = Context.getLifetimeQualifiedType(type, lifetime); |
| 4811 | decl->setType(type); |
| 4812 | } |
| 4813 | |
| 4814 | if (VarDecl *var = dyn_cast<VarDecl>(decl)) { |
| 4815 | // Thread-local variables cannot have lifetime. |
| 4816 | if (lifetime && lifetime != Qualifiers::OCL_ExplicitNone && |
Richard Smith | 38afbc7 | 2013-04-13 02:43:54 +0000 | [diff] [blame] | 4817 | var->getTLSKind()) { |
Argyrios Kyrtzidis | b8b0313 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 4818 | Diag(var->getLocation(), diag::err_arc_thread_ownership) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4819 | << var->getType(); |
| 4820 | return true; |
| 4821 | } |
| 4822 | } |
| 4823 | |
| 4824 | return false; |
| 4825 | } |
| 4826 | |
Rafael Espindola | 2a5bb50 | 2013-01-16 23:11:15 +0000 | [diff] [blame] | 4827 | static void checkAttributesAfterMerging(Sema &S, NamedDecl &ND) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4828 | // Ensure that an auto decl is deduced otherwise the checks below might cache |
| 4829 | // the wrong linkage. |
| 4830 | assert(S.ParsingInitForAutoVars.count(&ND) == 0); |
| 4831 | |
Rafael Espindola | 2a5bb50 | 2013-01-16 23:11:15 +0000 | [diff] [blame] | 4832 | // 'weak' only applies to declarations with external linkage. |
Rafael Espindola | 4d8a33b | 2013-01-16 23:49:06 +0000 | [diff] [blame] | 4833 | if (WeakAttr *Attr = ND.getAttr<WeakAttr>()) { |
Rafael Espindola | 181e3ec | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 4834 | if (!ND.isExternallyVisible()) { |
Rafael Espindola | 4d8a33b | 2013-01-16 23:49:06 +0000 | [diff] [blame] | 4835 | S.Diag(Attr->getLocation(), diag::err_attribute_weak_static); |
| 4836 | ND.dropAttr<WeakAttr>(); |
| 4837 | } |
| 4838 | } |
| 4839 | if (WeakRefAttr *Attr = ND.getAttr<WeakRefAttr>()) { |
Rafael Espindola | 181e3ec | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 4840 | if (ND.isExternallyVisible()) { |
Rafael Espindola | 4d8a33b | 2013-01-16 23:49:06 +0000 | [diff] [blame] | 4841 | S.Diag(Attr->getLocation(), diag::err_attribute_weakref_not_static); |
| 4842 | ND.dropAttr<WeakRefAttr>(); |
| 4843 | } |
Rafael Espindola | 2a5bb50 | 2013-01-16 23:11:15 +0000 | [diff] [blame] | 4844 | } |
Reid Kleckner | a722534 | 2013-05-20 14:02:37 +0000 | [diff] [blame] | 4845 | |
| 4846 | // 'selectany' only applies to externally visible varable declarations. |
| 4847 | // It does not apply to functions. |
| 4848 | if (SelectAnyAttr *Attr = ND.getAttr<SelectAnyAttr>()) { |
| 4849 | if (isa<FunctionDecl>(ND) || !ND.isExternallyVisible()) { |
| 4850 | S.Diag(Attr->getLocation(), diag::err_attribute_selectany_non_extern_data); |
| 4851 | ND.dropAttr<SelectAnyAttr>(); |
| 4852 | } |
| 4853 | } |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4854 | |
| 4855 | // dll attributes require external linkage. |
| 4856 | if (const DLLImportAttr *Attr = ND.getAttr<DLLImportAttr>()) { |
| 4857 | if (!ND.isExternallyVisible()) { |
| 4858 | S.Diag(ND.getLocation(), diag::err_attribute_dll_not_extern) |
| 4859 | << &ND << Attr; |
| 4860 | ND.setInvalidDecl(); |
| 4861 | } |
| 4862 | } |
| 4863 | if (const DLLExportAttr *Attr = ND.getAttr<DLLExportAttr>()) { |
| 4864 | if (!ND.isExternallyVisible()) { |
| 4865 | S.Diag(ND.getLocation(), diag::err_attribute_dll_not_extern) |
| 4866 | << &ND << Attr; |
| 4867 | ND.setInvalidDecl(); |
| 4868 | } |
| 4869 | } |
| 4870 | } |
| 4871 | |
| 4872 | static void checkDLLAttributeRedeclaration(Sema &S, NamedDecl *OldDecl, |
| 4873 | NamedDecl *NewDecl, |
| 4874 | bool IsSpecialization) { |
| 4875 | if (TemplateDecl *OldTD = dyn_cast<TemplateDecl>(OldDecl)) |
| 4876 | OldDecl = OldTD->getTemplatedDecl(); |
| 4877 | if (TemplateDecl *NewTD = dyn_cast<TemplateDecl>(NewDecl)) |
| 4878 | NewDecl = NewTD->getTemplatedDecl(); |
| 4879 | |
| 4880 | if (!OldDecl || !NewDecl) |
| 4881 | return; |
| 4882 | |
| 4883 | const DLLImportAttr *OldImportAttr = OldDecl->getAttr<DLLImportAttr>(); |
| 4884 | const DLLExportAttr *OldExportAttr = OldDecl->getAttr<DLLExportAttr>(); |
| 4885 | const DLLImportAttr *NewImportAttr = NewDecl->getAttr<DLLImportAttr>(); |
| 4886 | const DLLExportAttr *NewExportAttr = NewDecl->getAttr<DLLExportAttr>(); |
| 4887 | |
| 4888 | // dllimport and dllexport are inheritable attributes so we have to exclude |
| 4889 | // inherited attribute instances. |
| 4890 | bool HasNewAttr = (NewImportAttr && !NewImportAttr->isInherited()) || |
| 4891 | (NewExportAttr && !NewExportAttr->isInherited()); |
| 4892 | |
| 4893 | // A redeclaration is not allowed to add a dllimport or dllexport attribute, |
| 4894 | // the only exception being explicit specializations. |
| 4895 | // Implicitly generated declarations are also excluded for now because there |
| 4896 | // is no other way to switch these to use dllimport or dllexport. |
| 4897 | bool AddsAttr = !(OldImportAttr || OldExportAttr) && HasNewAttr; |
| 4898 | if (AddsAttr && !IsSpecialization && !OldDecl->isImplicit()) { |
| 4899 | S.Diag(NewDecl->getLocation(), diag::err_attribute_dll_redeclaration) |
| 4900 | << NewDecl |
| 4901 | << (NewImportAttr ? (const Attr *)NewImportAttr : NewExportAttr); |
| 4902 | S.Diag(OldDecl->getLocation(), diag::note_previous_declaration); |
| 4903 | NewDecl->setInvalidDecl(); |
| 4904 | return; |
| 4905 | } |
| 4906 | |
| 4907 | // A redeclaration is not allowed to drop a dllimport attribute, the only |
| 4908 | // exception being inline function definitions. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4909 | // NB: MSVC converts such a declaration to dllexport. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 4910 | bool IsInline = |
| 4911 | isa<FunctionDecl>(NewDecl) && cast<FunctionDecl>(NewDecl)->isInlined(); |
| 4912 | |
| 4913 | if (OldImportAttr && !HasNewAttr && !IsInline) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4914 | S.Diag(NewDecl->getLocation(), |
| 4915 | diag::warn_redeclaration_without_attribute_prev_attribute_ignored) |
| 4916 | << NewDecl << OldImportAttr; |
| 4917 | S.Diag(OldDecl->getLocation(), diag::note_previous_declaration); |
| 4918 | S.Diag(OldImportAttr->getLocation(), diag::note_previous_attribute); |
| 4919 | OldDecl->dropAttr<DLLImportAttr>(); |
| 4920 | NewDecl->dropAttr<DLLImportAttr>(); |
| 4921 | } |
Rafael Espindola | 2a5bb50 | 2013-01-16 23:11:15 +0000 | [diff] [blame] | 4922 | } |
| 4923 | |
John McCall | b421d92 | 2013-04-02 02:48:58 +0000 | [diff] [blame] | 4924 | /// Given that we are within the definition of the given function, |
| 4925 | /// will that definition behave like C99's 'inline', where the |
| 4926 | /// definition is discarded except for optimization purposes? |
| 4927 | static bool isFunctionDefinitionDiscarded(Sema &S, FunctionDecl *FD) { |
| 4928 | // Try to avoid calling GetGVALinkageForFunction. |
| 4929 | |
| 4930 | // All cases of this require the 'inline' keyword. |
| 4931 | if (!FD->isInlined()) return false; |
| 4932 | |
| 4933 | // This is only possible in C++ with the gnu_inline attribute. |
| 4934 | if (S.getLangOpts().CPlusPlus && !FD->hasAttr<GNUInlineAttr>()) |
| 4935 | return false; |
| 4936 | |
| 4937 | // Okay, go ahead and call the relatively-more-expensive function. |
| 4938 | |
| 4939 | #ifndef NDEBUG |
| 4940 | // AST quite reasonably asserts that it's working on a function |
| 4941 | // definition. We don't really have a way to tell it that we're |
| 4942 | // currently defining the function, so just lie to it in +Asserts |
| 4943 | // builds. This is an awful hack. |
| 4944 | FD->setLazyBody(1); |
| 4945 | #endif |
| 4946 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 4947 | bool isC99Inline = |
| 4948 | S.Context.GetGVALinkageForFunction(FD) == GVA_AvailableExternally; |
John McCall | b421d92 | 2013-04-02 02:48:58 +0000 | [diff] [blame] | 4949 | |
| 4950 | #ifndef NDEBUG |
| 4951 | FD->setLazyBody(0); |
| 4952 | #endif |
| 4953 | |
| 4954 | return isC99Inline; |
| 4955 | } |
| 4956 | |
Richard Smith | aa4bc18 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 4957 | /// Determine whether a variable is extern "C" prior to attaching |
| 4958 | /// an initializer. We can't just call isExternC() here, because that |
| 4959 | /// will also compute and cache whether the declaration is externally |
| 4960 | /// visible, which might change when we attach the initializer. |
| 4961 | /// |
| 4962 | /// This can only be used if the declaration is known to not be a |
| 4963 | /// redeclaration of an internal linkage declaration. |
| 4964 | /// |
| 4965 | /// For instance: |
| 4966 | /// |
| 4967 | /// auto x = []{}; |
| 4968 | /// |
| 4969 | /// Attaching the initializer here makes this declaration not externally |
| 4970 | /// visible, because its type has internal linkage. |
| 4971 | /// |
| 4972 | /// FIXME: This is a hack. |
| 4973 | template<typename T> |
| 4974 | static bool isIncompleteDeclExternC(Sema &S, const T *D) { |
| 4975 | if (S.getLangOpts().CPlusPlus) { |
| 4976 | // In C++, the overloadable attribute negates the effects of extern "C". |
| 4977 | if (!D->isInExternCContext() || D->template hasAttr<OverloadableAttr>()) |
| 4978 | return false; |
| 4979 | } |
| 4980 | return D->isExternC(); |
| 4981 | } |
| 4982 | |
Rafael Espindola | 2d1b096 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 4983 | static bool shouldConsiderLinkage(const VarDecl *VD) { |
| 4984 | const DeclContext *DC = VD->getDeclContext()->getRedeclContext(); |
| 4985 | if (DC->isFunctionOrMethod()) |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 4986 | return VD->hasExternalStorage(); |
Rafael Espindola | 2d1b096 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 4987 | if (DC->isFileContext()) |
| 4988 | return true; |
| 4989 | if (DC->isRecord()) |
| 4990 | return false; |
| 4991 | llvm_unreachable("Unexpected context"); |
| 4992 | } |
| 4993 | |
| 4994 | static bool shouldConsiderLinkage(const FunctionDecl *FD) { |
| 4995 | const DeclContext *DC = FD->getDeclContext()->getRedeclContext(); |
| 4996 | if (DC->isFileContext() || DC->isFunctionOrMethod()) |
| 4997 | return true; |
| 4998 | if (DC->isRecord()) |
| 4999 | return false; |
| 5000 | llvm_unreachable("Unexpected context"); |
| 5001 | } |
| 5002 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5003 | static bool hasParsedAttr(Scope *S, const AttributeList *AttrList, |
| 5004 | AttributeList::Kind Kind) { |
| 5005 | for (const AttributeList *L = AttrList; L; L = L->getNext()) |
| 5006 | if (L->getKind() == Kind) |
| 5007 | return true; |
| 5008 | return false; |
| 5009 | } |
| 5010 | |
| 5011 | static bool hasParsedAttr(Scope *S, const Declarator &PD, |
| 5012 | AttributeList::Kind Kind) { |
| 5013 | // Check decl attributes on the DeclSpec. |
| 5014 | if (hasParsedAttr(S, PD.getDeclSpec().getAttributes().getList(), Kind)) |
| 5015 | return true; |
| 5016 | |
| 5017 | // Walk the declarator structure, checking decl attributes that were in a type |
| 5018 | // position to the decl itself. |
| 5019 | for (unsigned I = 0, E = PD.getNumTypeObjects(); I != E; ++I) { |
| 5020 | if (hasParsedAttr(S, PD.getTypeObject(I).getAttrs(), Kind)) |
| 5021 | return true; |
| 5022 | } |
| 5023 | |
| 5024 | // Finally, check attributes on the decl itself. |
| 5025 | return hasParsedAttr(S, PD.getAttributes(), Kind); |
| 5026 | } |
| 5027 | |
Richard Smith | a41c97a | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 5028 | /// Adjust the \c DeclContext for a function or variable that might be a |
| 5029 | /// function-local external declaration. |
| 5030 | bool Sema::adjustContextForLocalExternDecl(DeclContext *&DC) { |
| 5031 | if (!DC->isFunctionOrMethod()) |
| 5032 | return false; |
| 5033 | |
| 5034 | // If this is a local extern function or variable declared within a function |
| 5035 | // template, don't add it into the enclosing namespace scope until it is |
| 5036 | // instantiated; it might have a dependent type right now. |
| 5037 | if (DC->isDependentContext()) |
| 5038 | return true; |
| 5039 | |
| 5040 | // C++11 [basic.link]p7: |
| 5041 | // When a block scope declaration of an entity with linkage is not found to |
| 5042 | // refer to some other declaration, then that entity is a member of the |
| 5043 | // innermost enclosing namespace. |
| 5044 | // |
| 5045 | // Per C++11 [namespace.def]p6, the innermost enclosing namespace is a |
| 5046 | // semantically-enclosing namespace, not a lexically-enclosing one. |
| 5047 | while (!DC->isFileContext() && !isa<LinkageSpecDecl>(DC)) |
| 5048 | DC = DC->getParent(); |
| 5049 | return true; |
| 5050 | } |
| 5051 | |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5052 | NamedDecl * |
Chris Lattner | 16c5dea | 2010-10-10 18:16:20 +0000 | [diff] [blame] | 5053 | Sema::ActOnVariableDeclarator(Scope *S, Declarator &D, DeclContext *DC, |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 5054 | TypeSourceInfo *TInfo, LookupResult &Previous, |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5055 | MultiTemplateParamsArg TemplateParamLists, |
| 5056 | bool &AddToScope) { |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 5057 | QualType R = TInfo->getType(); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 5058 | DeclarationName Name = GetNameForDeclarator(D).getName(); |
Zhongxing Xu | cb8f4f1 | 2009-01-16 02:36:34 +0000 | [diff] [blame] | 5059 | |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 5060 | DeclSpec::SCS SCSpec = D.getDeclSpec().getStorageClassSpec(); |
Rafael Espindola | 65dfa2b | 2013-04-25 12:11:36 +0000 | [diff] [blame] | 5061 | VarDecl::StorageClass SC = |
| 5062 | StorageClassSpecToVarDeclStorageClass(D.getDeclSpec()); |
Joey Gouly | 19dbb20 | 2013-01-23 11:56:20 +0000 | [diff] [blame] | 5063 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5064 | // dllimport globals without explicit storage class are treated as extern. We |
| 5065 | // have to change the storage class this early to get the right DeclContext. |
| 5066 | if (SC == SC_None && !DC->isRecord() && |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 5067 | hasParsedAttr(S, D, AttributeList::AT_DLLImport) && |
| 5068 | !hasParsedAttr(S, D, AttributeList::AT_DLLExport)) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5069 | SC = SC_Extern; |
| 5070 | |
Richard Smith | a41c97a | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 5071 | DeclContext *OriginalDC = DC; |
| 5072 | bool IsLocalExternDecl = SC == SC_Extern && |
| 5073 | adjustContextForLocalExternDecl(DC); |
| 5074 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5075 | if (getLangOpts().OpenCL) { |
| 5076 | // OpenCL v1.0 s6.8.a.3: Pointers to functions are not allowed. |
| 5077 | QualType NR = R; |
| 5078 | while (NR->isPointerType()) { |
| 5079 | if (NR->isFunctionPointerType()) { |
| 5080 | Diag(D.getIdentifierLoc(), diag::err_opencl_function_pointer_variable); |
| 5081 | D.setInvalidType(); |
| 5082 | break; |
| 5083 | } |
| 5084 | NR = NR->getPointeeType(); |
| 5085 | } |
| 5086 | |
| 5087 | if (!getOpenCLOptions().cl_khr_fp16) { |
| 5088 | // OpenCL v1.2 s6.1.1.1: reject declaring variables of the half and |
| 5089 | // half array type (unless the cl_khr_fp16 extension is enabled). |
| 5090 | if (Context.getBaseElementType(R)->isHalfType()) { |
| 5091 | Diag(D.getIdentifierLoc(), diag::err_opencl_half_declaration) << R; |
| 5092 | D.setInvalidType(); |
| 5093 | } |
Joey Gouly | 19dbb20 | 2013-01-23 11:56:20 +0000 | [diff] [blame] | 5094 | } |
| 5095 | } |
| 5096 | |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 5097 | if (SCSpec == DeclSpec::SCS_mutable) { |
Zhongxing Xu | cb8f4f1 | 2009-01-16 02:36:34 +0000 | [diff] [blame] | 5098 | // mutable can only appear on non-static class members, so it's always |
| 5099 | // an error here |
| 5100 | Diag(D.getIdentifierLoc(), diag::err_mutable_nonmember); |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 5101 | D.setInvalidType(); |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 5102 | SC = SC_None; |
Zhongxing Xu | cb8f4f1 | 2009-01-16 02:36:34 +0000 | [diff] [blame] | 5103 | } |
John McCall | b421d92 | 2013-04-02 02:48:58 +0000 | [diff] [blame] | 5104 | |
Richard Smith | 9109bf1 | 2013-06-17 01:34:01 +0000 | [diff] [blame] | 5105 | if (getLangOpts().CPlusPlus11 && SCSpec == DeclSpec::SCS_register && |
| 5106 | !D.getAsmLabel() && !getSourceManager().isInSystemMacro( |
| 5107 | D.getDeclSpec().getStorageClassSpecLoc())) { |
| 5108 | // In C++11, the 'register' storage class specifier is deprecated. |
| 5109 | // Suppress the warning in system macros, it's used in macros in some |
| 5110 | // popular C system headers, such as in glibc's htonl() macro. |
| 5111 | Diag(D.getDeclSpec().getStorageClassSpecLoc(), |
| 5112 | diag::warn_deprecated_register) |
| 5113 | << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc()); |
| 5114 | } |
| 5115 | |
Zhongxing Xu | cb8f4f1 | 2009-01-16 02:36:34 +0000 | [diff] [blame] | 5116 | IdentifierInfo *II = Name.getAsIdentifierInfo(); |
| 5117 | if (!II) { |
| 5118 | Diag(D.getIdentifierLoc(), diag::err_bad_variable_name) |
Douglas Gregor | b5a0187 | 2011-10-09 18:55:59 +0000 | [diff] [blame] | 5119 | << Name; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 5120 | return nullptr; |
Zhongxing Xu | cb8f4f1 | 2009-01-16 02:36:34 +0000 | [diff] [blame] | 5121 | } |
| 5122 | |
Richard Smith | c7f8116 | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 5123 | DiagnoseFunctionSpecifiers(D.getDeclSpec()); |
Douglas Gregor | 021c3b3 | 2009-03-11 23:00:04 +0000 | [diff] [blame] | 5124 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 5125 | if (!DC->isRecord() && S->getFnParent() == nullptr) { |
Douglas Gregor | 2d2e9cf | 2009-03-11 20:22:50 +0000 | [diff] [blame] | 5126 | // C99 6.9p2: The storage-class specifiers auto and register shall not |
| 5127 | // appear in the declaration specifiers in an external declaration. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 5128 | // Global Register+Asm is a GNU extension we support. |
| 5129 | if (SC == SC_Auto || (SC == SC_Register && !D.getAsmLabel())) { |
| 5130 | Diag(D.getIdentifierLoc(), diag::err_typecheck_sclass_fscope); |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 5131 | D.setInvalidType(); |
Zhongxing Xu | cb8f4f1 | 2009-01-16 02:36:34 +0000 | [diff] [blame] | 5132 | } |
Zhongxing Xu | cb8f4f1 | 2009-01-16 02:36:34 +0000 | [diff] [blame] | 5133 | } |
Richard Smith | 9109bf1 | 2013-06-17 01:34:01 +0000 | [diff] [blame] | 5134 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5135 | if (getLangOpts().OpenCL) { |
Peter Collingbourne | 8c25fc5 | 2011-09-19 21:14:35 +0000 | [diff] [blame] | 5136 | // Set up the special work-group-local storage class for variables in the |
| 5137 | // OpenCL __local address space. |
Rafael Espindola | 0db661e | 2012-12-21 01:21:33 +0000 | [diff] [blame] | 5138 | if (R.getAddressSpace() == LangAS::opencl_local) { |
Peter Collingbourne | 8c25fc5 | 2011-09-19 21:14:35 +0000 | [diff] [blame] | 5139 | SC = SC_OpenCLWorkGroupLocal; |
Rafael Espindola | 0db661e | 2012-12-21 01:21:33 +0000 | [diff] [blame] | 5140 | } |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 5141 | |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 5142 | // OpenCL v1.2 s6.9.b p4: |
| 5143 | // The sampler type cannot be used with the __local and __global address |
| 5144 | // space qualifiers. |
| 5145 | if (R->isSamplerT() && (R.getAddressSpace() == LangAS::opencl_local || |
| 5146 | R.getAddressSpace() == LangAS::opencl_global)) { |
| 5147 | Diag(D.getIdentifierLoc(), diag::err_wrong_sampler_addressspace); |
| 5148 | } |
| 5149 | |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 5150 | // OpenCL 1.2 spec, p6.9 r: |
| 5151 | // The event type cannot be used to declare a program scope variable. |
| 5152 | // The event type cannot be used with the __local, __constant and __global |
| 5153 | // address space qualifiers. |
| 5154 | if (R->isEventT()) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 5155 | if (S->getParent() == nullptr) { |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 5156 | Diag(D.getLocStart(), diag::err_event_t_global_var); |
| 5157 | D.setInvalidType(); |
| 5158 | } |
| 5159 | |
| 5160 | if (R.getAddressSpace()) { |
| 5161 | Diag(D.getLocStart(), diag::err_event_t_addr_space_qual); |
| 5162 | D.setInvalidType(); |
| 5163 | } |
| 5164 | } |
Peter Collingbourne | 8c25fc5 | 2011-09-19 21:14:35 +0000 | [diff] [blame] | 5165 | } |
| 5166 | |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5167 | bool IsExplicitSpecialization = false; |
| 5168 | bool IsVariableTemplateSpecialization = false; |
| 5169 | bool IsPartialSpecialization = false; |
Larisse Voufo | 4a91989 | 2013-08-14 03:09:19 +0000 | [diff] [blame] | 5170 | bool IsVariableTemplate = false; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 5171 | VarDecl *NewVD = nullptr; |
| 5172 | VarTemplateDecl *NewTemplate = nullptr; |
| 5173 | TemplateParameterList *TemplateParams = nullptr; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5174 | if (!getLangOpts().CPlusPlus) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5175 | NewVD = VarDecl::Create(Context, DC, D.getLocStart(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 5176 | D.getIdentifierLoc(), II, |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 5177 | R, TInfo, SC); |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 5178 | |
| 5179 | if (D.isInvalidType()) |
| 5180 | NewVD->setInvalidDecl(); |
| 5181 | } else { |
Larisse Voufo | 567f917 | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 5182 | bool Invalid = false; |
| 5183 | |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 5184 | if (DC->isRecord() && !CurContext->isRecord()) { |
| 5185 | // This is an out-of-line definition of a static data member. |
Rafael Espindola | 3882aed | 2013-06-19 13:41:54 +0000 | [diff] [blame] | 5186 | switch (SC) { |
| 5187 | case SC_None: |
| 5188 | break; |
| 5189 | case SC_Static: |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 5190 | Diag(D.getDeclSpec().getStorageClassSpecLoc(), |
| 5191 | diag::err_static_out_of_line) |
| 5192 | << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc()); |
Rafael Espindola | 3882aed | 2013-06-19 13:41:54 +0000 | [diff] [blame] | 5193 | break; |
| 5194 | case SC_Auto: |
| 5195 | case SC_Register: |
| 5196 | case SC_Extern: |
| 5197 | // [dcl.stc] p2: The auto or register specifiers shall be applied only |
| 5198 | // to names of variables declared in a block or to function parameters. |
| 5199 | // [dcl.stc] p6: The extern specifier cannot be used in the declaration |
| 5200 | // of class members |
| 5201 | |
| 5202 | Diag(D.getDeclSpec().getStorageClassSpecLoc(), |
| 5203 | diag::err_storage_class_for_static_member) |
| 5204 | << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc()); |
| 5205 | break; |
| 5206 | case SC_PrivateExtern: |
| 5207 | llvm_unreachable("C storage class in c++!"); |
| 5208 | case SC_OpenCLWorkGroupLocal: |
| 5209 | llvm_unreachable("OpenCL storage class in c++!"); |
Rafael Espindola | ea4b111 | 2013-04-04 21:21:25 +0000 | [diff] [blame] | 5210 | } |
Larisse Voufo | 06935f3 | 2013-08-06 03:43:07 +0000 | [diff] [blame] | 5211 | } |
| 5212 | |
Richard Smith | b9c64d8 | 2012-02-16 20:41:22 +0000 | [diff] [blame] | 5213 | if (SC == SC_Static && CurContext->isRecord()) { |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 5214 | if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(DC)) { |
| 5215 | if (RD->isLocalClass()) |
| 5216 | Diag(D.getIdentifierLoc(), |
| 5217 | diag::err_static_data_member_not_allowed_in_local_class) |
| 5218 | << Name << RD->getDeclName(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5219 | |
Richard Smith | b9c64d8 | 2012-02-16 20:41:22 +0000 | [diff] [blame] | 5220 | // C++98 [class.union]p1: If a union contains a static data member, |
| 5221 | // the program is ill-formed. C++11 drops this restriction. |
| 5222 | if (RD->isUnion()) |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 5223 | Diag(D.getIdentifierLoc(), |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5224 | getLangOpts().CPlusPlus11 |
Richard Smith | b9c64d8 | 2012-02-16 20:41:22 +0000 | [diff] [blame] | 5225 | ? diag::warn_cxx98_compat_static_data_member_in_union |
| 5226 | : diag::ext_static_data_member_in_union) << Name; |
| 5227 | // We conservatively disallow static data members in anonymous structs. |
| 5228 | else if (!RD->getDeclName()) |
| 5229 | Diag(D.getIdentifierLoc(), |
| 5230 | diag::err_static_data_member_not_allowed_in_anon_struct) |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 5231 | << Name << RD->isUnion(); |
| 5232 | } |
| 5233 | } |
| 5234 | |
| 5235 | // Match up the template parameter lists with the scope specifier, then |
| 5236 | // determine whether we have a template or a template specialization. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5237 | TemplateParams = MatchTemplateParametersToScopeSpecifier( |
| 5238 | D.getDeclSpec().getLocStart(), D.getIdentifierLoc(), |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 5239 | D.getCXXScopeSpec(), |
| 5240 | D.getName().getKind() == UnqualifiedId::IK_TemplateId |
| 5241 | ? D.getName().TemplateId |
| 5242 | : nullptr, |
| 5243 | TemplateParamLists, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5244 | /*never a friend*/ false, IsExplicitSpecialization, Invalid); |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5245 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5246 | if (TemplateParams) { |
| 5247 | if (!TemplateParams->size() && |
| 5248 | D.getName().getKind() != UnqualifiedId::IK_TemplateId) { |
| 5249 | // There is an extraneous 'template<>' for this variable. Complain |
| 5250 | // about it, but allow the declaration of the variable. |
| 5251 | Diag(TemplateParams->getTemplateLoc(), |
| 5252 | diag::err_template_variable_noparams) |
| 5253 | << II |
| 5254 | << SourceRange(TemplateParams->getTemplateLoc(), |
| 5255 | TemplateParams->getRAngleLoc()); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 5256 | TemplateParams = nullptr; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5257 | } else { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5258 | if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) { |
| 5259 | // This is an explicit specialization or a partial specialization. |
| 5260 | // FIXME: Check that we can declare a specialization here. |
| 5261 | IsVariableTemplateSpecialization = true; |
| 5262 | IsPartialSpecialization = TemplateParams->size() > 0; |
| 5263 | } else { // if (TemplateParams->size() > 0) |
| 5264 | // This is a template declaration. |
| 5265 | IsVariableTemplate = true; |
| 5266 | |
| 5267 | // Check that we can declare a template here. |
| 5268 | if (CheckTemplateDeclScope(S, TemplateParams)) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 5269 | return nullptr; |
| 5270 | |
| 5271 | // Only C++1y supports variable templates (N3651). |
| 5272 | Diag(D.getIdentifierLoc(), |
| 5273 | getLangOpts().CPlusPlus1y |
| 5274 | ? diag::warn_cxx11_compat_variable_template |
| 5275 | : diag::ext_variable_template); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5276 | } |
| 5277 | } |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 5278 | } else { |
| 5279 | assert(D.getName().getKind() != UnqualifiedId::IK_TemplateId && |
| 5280 | "should have a 'template<>' for this decl"); |
Douglas Gregor | dfe3f2d | 2009-07-22 17:18:37 +0000 | [diff] [blame] | 5281 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5282 | |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5283 | if (IsVariableTemplateSpecialization) { |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5284 | SourceLocation TemplateKWLoc = |
| 5285 | TemplateParamLists.size() > 0 |
| 5286 | ? TemplateParamLists[0]->getTemplateLoc() |
| 5287 | : SourceLocation(); |
| 5288 | DeclResult Res = ActOnVarTemplateSpecialization( |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5289 | S, D, TInfo, TemplateKWLoc, TemplateParams, SC, |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5290 | IsPartialSpecialization); |
| 5291 | if (Res.isInvalid()) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 5292 | return nullptr; |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5293 | NewVD = cast<VarDecl>(Res.get()); |
| 5294 | AddToScope = false; |
| 5295 | } else |
| 5296 | NewVD = VarDecl::Create(Context, DC, D.getLocStart(), |
| 5297 | D.getIdentifierLoc(), II, R, TInfo, SC); |
Eli Friedman | 63054b3 | 2009-04-19 20:27:55 +0000 | [diff] [blame] | 5298 | |
Larisse Voufo | 567f917 | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 5299 | // If this is supposed to be a variable template, create it as such. |
| 5300 | if (IsVariableTemplate) { |
| 5301 | NewTemplate = |
| 5302 | VarTemplateDecl::Create(Context, DC, D.getIdentifierLoc(), Name, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5303 | TemplateParams, NewVD); |
Larisse Voufo | 567f917 | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 5304 | NewVD->setDescribedVarTemplate(NewTemplate); |
| 5305 | } |
| 5306 | |
Richard Smith | 483b9f3 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 5307 | // If this decl has an auto type in need of deduction, make a note of the |
| 5308 | // Decl so we can diagnose uses of it in its own initializer. |
Richard Smith | a2c3646 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 5309 | if (D.getDeclSpec().containsPlaceholderType() && R->getContainedAutoType()) |
Richard Smith | 483b9f3 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 5310 | ParsingInitForAutoVars.insert(NewVD); |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 5311 | |
Larisse Voufo | 567f917 | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 5312 | if (D.isInvalidType() || Invalid) { |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 5313 | NewVD->setInvalidDecl(); |
Larisse Voufo | 567f917 | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 5314 | if (NewTemplate) |
| 5315 | NewTemplate->setInvalidDecl(); |
| 5316 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5317 | |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 5318 | SetNestedNameSpecifier(NewVD, D); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 5319 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5320 | // If we have any template parameter lists that don't directly belong to |
| 5321 | // the variable (matching the scope specifier), store them. |
| 5322 | unsigned VDTemplateParamLists = TemplateParams ? 1 : 0; |
| 5323 | if (TemplateParamLists.size() > VDTemplateParamLists) |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5324 | NewVD->setTemplateParameterListsInfo( |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5325 | Context, TemplateParamLists.size() - VDTemplateParamLists, |
| 5326 | TemplateParamLists.data()); |
Richard Smith | af1fc7a | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 5327 | |
Richard Smith | 7ca4850 | 2012-02-13 22:16:19 +0000 | [diff] [blame] | 5328 | if (D.getDeclSpec().isConstexprSpecified()) |
Richard Smith | dd4b350 | 2011-12-25 21:17:58 +0000 | [diff] [blame] | 5329 | NewVD->setConstexpr(true); |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 5330 | } |
| 5331 | |
Douglas Gregor | e389585 | 2011-09-12 18:37:38 +0000 | [diff] [blame] | 5332 | // Set the lexical context. If the declarator has a C++ scope specifier, the |
| 5333 | // lexical context will be different from the semantic context. |
| 5334 | NewVD->setLexicalDeclContext(CurContext); |
Larisse Voufo | 567f917 | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 5335 | if (NewTemplate) |
| 5336 | NewTemplate->setLexicalDeclContext(CurContext); |
Douglas Gregor | e389585 | 2011-09-12 18:37:38 +0000 | [diff] [blame] | 5337 | |
Richard Smith | a41c97a | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 5338 | if (IsLocalExternDecl) |
| 5339 | NewVD->setLocalExternDecl(); |
| 5340 | |
Richard Smith | ec64244 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 5341 | if (DeclSpec::TSCS TSCS = D.getDeclSpec().getThreadStorageClassSpec()) { |
Enea Zaffanella | 9cbcab8 | 2013-05-10 20:34:44 +0000 | [diff] [blame] | 5342 | if (NewVD->hasLocalStorage()) { |
| 5343 | // C++11 [dcl.stc]p4: |
| 5344 | // When thread_local is applied to a variable of block scope the |
| 5345 | // storage-class-specifier static is implied if it does not appear |
| 5346 | // explicitly. |
| 5347 | // Core issue: 'static' is not implied if the variable is declared |
| 5348 | // 'extern'. |
| 5349 | if (SCSpec == DeclSpec::SCS_unspecified && |
| 5350 | TSCS == DeclSpec::TSCS_thread_local && |
| 5351 | DC->isFunctionOrMethod()) |
| 5352 | NewVD->setTSCSpec(TSCS); |
| 5353 | else |
| 5354 | Diag(D.getDeclSpec().getThreadStorageClassSpecLoc(), |
| 5355 | diag::err_thread_non_global) |
| 5356 | << DeclSpec::getSpecifierName(TSCS); |
| 5357 | } else if (!Context.getTargetInfo().isTLSSupported()) |
Richard Smith | ec64244 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 5358 | Diag(D.getDeclSpec().getThreadStorageClassSpecLoc(), |
| 5359 | diag::err_thread_unsupported); |
Eli Friedman | 63054b3 | 2009-04-19 20:27:55 +0000 | [diff] [blame] | 5360 | else |
Enea Zaffanella | dc17384 | 2013-05-04 08:27:07 +0000 | [diff] [blame] | 5361 | NewVD->setTSCSpec(TSCS); |
Eli Friedman | 63054b3 | 2009-04-19 20:27:55 +0000 | [diff] [blame] | 5362 | } |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 5363 | |
John McCall | b421d92 | 2013-04-02 02:48:58 +0000 | [diff] [blame] | 5364 | // C99 6.7.4p3 |
| 5365 | // An inline definition of a function with external linkage shall |
| 5366 | // not contain a definition of a modifiable object with static or |
| 5367 | // thread storage duration... |
| 5368 | // We only apply this when the function is required to be defined |
| 5369 | // elsewhere, i.e. when the function is not 'extern inline'. Note |
| 5370 | // that a local variable with thread storage duration still has to |
| 5371 | // be marked 'static'. Also note that it's possible to get these |
| 5372 | // semantics in C++ using __attribute__((gnu_inline)). |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 5373 | if (SC == SC_Static && S->getFnParent() != nullptr && |
John McCall | b421d92 | 2013-04-02 02:48:58 +0000 | [diff] [blame] | 5374 | !NewVD->getType().isConstQualified()) { |
| 5375 | FunctionDecl *CurFD = getCurFunctionDecl(); |
| 5376 | if (CurFD && isFunctionDefinitionDiscarded(*this, CurFD)) { |
| 5377 | Diag(D.getDeclSpec().getStorageClassSpecLoc(), |
| 5378 | diag::warn_static_local_in_extern_inline); |
| 5379 | MaybeSuggestAddingStaticToDecl(CurFD); |
| 5380 | } |
| 5381 | } |
| 5382 | |
Douglas Gregor | d023aec | 2011-09-09 20:53:38 +0000 | [diff] [blame] | 5383 | if (D.getDeclSpec().isModulePrivateSpecified()) { |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5384 | if (IsVariableTemplateSpecialization) |
| 5385 | Diag(NewVD->getLocation(), diag::err_module_private_specialization) |
| 5386 | << (IsPartialSpecialization ? 1 : 0) |
| 5387 | << FixItHint::CreateRemoval( |
| 5388 | D.getDeclSpec().getModulePrivateSpecLoc()); |
| 5389 | else if (IsExplicitSpecialization) |
Douglas Gregor | d023aec | 2011-09-09 20:53:38 +0000 | [diff] [blame] | 5390 | Diag(NewVD->getLocation(), diag::err_module_private_specialization) |
| 5391 | << 2 |
| 5392 | << FixItHint::CreateRemoval(D.getDeclSpec().getModulePrivateSpecLoc()); |
Douglas Gregor | e389585 | 2011-09-12 18:37:38 +0000 | [diff] [blame] | 5393 | else if (NewVD->hasLocalStorage()) |
| 5394 | Diag(NewVD->getLocation(), diag::err_module_private_local) |
| 5395 | << 0 << NewVD->getDeclName() |
| 5396 | << SourceRange(D.getDeclSpec().getModulePrivateSpecLoc()) |
| 5397 | << FixItHint::CreateRemoval(D.getDeclSpec().getModulePrivateSpecLoc()); |
Larisse Voufo | 567f917 | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 5398 | else { |
Douglas Gregor | d023aec | 2011-09-09 20:53:38 +0000 | [diff] [blame] | 5399 | NewVD->setModulePrivate(); |
Larisse Voufo | 567f917 | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 5400 | if (NewTemplate) |
| 5401 | NewTemplate->setModulePrivate(); |
| 5402 | } |
Douglas Gregor | d023aec | 2011-09-09 20:53:38 +0000 | [diff] [blame] | 5403 | } |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 5404 | |
Zhongxing Xu | cb8f4f1 | 2009-01-16 02:36:34 +0000 | [diff] [blame] | 5405 | // Handle attributes prior to checking for duplicates in MergeVarDecl |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 5406 | ProcessDeclAttributes(S, NewVD, D); |
Zhongxing Xu | cb8f4f1 | 2009-01-16 02:36:34 +0000 | [diff] [blame] | 5407 | |
Peter Collingbourne | c0c0066 | 2012-08-28 20:37:50 +0000 | [diff] [blame] | 5408 | if (getLangOpts().CUDA) { |
| 5409 | // CUDA B.2.5: "__shared__ and __constant__ variables have implied static |
| 5410 | // storage [duration]." |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 5411 | if (SC == SC_None && S->getFnParent() != nullptr && |
Rafael Espindola | 0db661e | 2012-12-21 01:21:33 +0000 | [diff] [blame] | 5412 | (NewVD->hasAttr<CUDASharedAttr>() || |
| 5413 | NewVD->hasAttr<CUDAConstantAttr>())) { |
Peter Collingbourne | c0c0066 | 2012-08-28 20:37:50 +0000 | [diff] [blame] | 5414 | NewVD->setStorageClass(SC_Static); |
Rafael Espindola | 0db661e | 2012-12-21 01:21:33 +0000 | [diff] [blame] | 5415 | } |
Peter Collingbourne | c0c0066 | 2012-08-28 20:37:50 +0000 | [diff] [blame] | 5416 | } |
| 5417 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5418 | // Ensure that dllimport globals without explicit storage class are treated as |
| 5419 | // extern. The storage class is set above using parsed attributes. Now we can |
| 5420 | // check the VarDecl itself. |
| 5421 | assert(!NewVD->hasAttr<DLLImportAttr>() || |
| 5422 | NewVD->getAttr<DLLImportAttr>()->isInherited() || |
| 5423 | NewVD->isStaticDataMember() || NewVD->getStorageClass() != SC_None); |
| 5424 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5425 | // In auto-retain/release, infer strong retension for variables of |
| 5426 | // retainable type. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5427 | if (getLangOpts().ObjCAutoRefCount && inferObjCARCLifetime(NewVD)) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5428 | NewVD->setInvalidDecl(); |
| 5429 | |
Zhongxing Xu | cb8f4f1 | 2009-01-16 02:36:34 +0000 | [diff] [blame] | 5430 | // Handle GNU asm-label extension (encoded as an attribute). |
Chris Lattner | 16c5dea | 2010-10-10 18:16:20 +0000 | [diff] [blame] | 5431 | if (Expr *E = (Expr*)D.getAsmLabel()) { |
Zhongxing Xu | cb8f4f1 | 2009-01-16 02:36:34 +0000 | [diff] [blame] | 5432 | // The parser guarantees this is a string. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5433 | StringLiteral *SE = cast<StringLiteral>(E); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5434 | StringRef Label = SE->getString(); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 5435 | if (S->getFnParent() != nullptr) { |
Abramo Bagnara | 2b57aef | 2011-01-11 15:16:52 +0000 | [diff] [blame] | 5436 | switch (SC) { |
| 5437 | case SC_None: |
| 5438 | case SC_Auto: |
| 5439 | Diag(E->getExprLoc(), diag::warn_asm_label_on_auto_decl) << Label; |
| 5440 | break; |
| 5441 | case SC_Register: |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 5442 | // Local Named register |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 5443 | if (!Context.getTargetInfo().isValidGCCRegisterName(Label)) |
Abramo Bagnara | 2b57aef | 2011-01-11 15:16:52 +0000 | [diff] [blame] | 5444 | Diag(E->getExprLoc(), diag::err_asm_unknown_register_name) << Label; |
| 5445 | break; |
| 5446 | case SC_Static: |
| 5447 | case SC_Extern: |
| 5448 | case SC_PrivateExtern: |
Peter Collingbourne | 8c25fc5 | 2011-09-19 21:14:35 +0000 | [diff] [blame] | 5449 | case SC_OpenCLWorkGroupLocal: |
Abramo Bagnara | 2b57aef | 2011-01-11 15:16:52 +0000 | [diff] [blame] | 5450 | break; |
| 5451 | } |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 5452 | } else if (SC == SC_Register) { |
| 5453 | // Global Named register |
| 5454 | if (!Context.getTargetInfo().isValidGCCRegisterName(Label)) |
| 5455 | Diag(E->getExprLoc(), diag::err_asm_unknown_register_name) << Label; |
Abramo Bagnara | 2b57aef | 2011-01-11 15:16:52 +0000 | [diff] [blame] | 5456 | } |
| 5457 | |
| 5458 | NewVD->addAttr(::new (Context) AsmLabelAttr(SE->getStrTokenLoc(0), |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5459 | Context, Label, 0)); |
David Chisnall | 5f3c163 | 2012-02-18 16:12:34 +0000 | [diff] [blame] | 5460 | } else if (!ExtnameUndeclaredIdentifiers.empty()) { |
| 5461 | llvm::DenseMap<IdentifierInfo*,AsmLabelAttr*>::iterator I = |
| 5462 | ExtnameUndeclaredIdentifiers.find(NewVD->getIdentifier()); |
| 5463 | if (I != ExtnameUndeclaredIdentifiers.end()) { |
| 5464 | NewVD->addAttr(I->second); |
| 5465 | ExtnameUndeclaredIdentifiers.erase(I); |
| 5466 | } |
Zhongxing Xu | cb8f4f1 | 2009-01-16 02:36:34 +0000 | [diff] [blame] | 5467 | } |
| 5468 | |
John McCall | 8472af4 | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 5469 | // Diagnose shadowed variables before filtering for scope. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5470 | if (D.getCXXScopeSpec().isEmpty()) |
John McCall | 053f4bd | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 5471 | CheckShadow(S, NewVD, Previous); |
John McCall | 8472af4 | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 5472 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5473 | // Don't consider existing declarations that are in a different |
| 5474 | // scope and are out-of-semantic-context declarations (if the new |
| 5475 | // declaration has linkage). |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5476 | FilterLookupForScope(Previous, OriginalDC, S, shouldConsiderLinkage(NewVD), |
| 5477 | D.getCXXScopeSpec().isNotEmpty() || |
| 5478 | IsExplicitSpecialization || |
| 5479 | IsVariableTemplateSpecialization); |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5480 | |
Richard Smith | dd9459f | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 5481 | // Check whether the previous declaration is in the same block scope. This |
| 5482 | // affects whether we merge types with it, per C++11 [dcl.array]p3. |
| 5483 | if (getLangOpts().CPlusPlus && |
| 5484 | NewVD->isLocalVarDecl() && NewVD->hasExternalStorage()) |
| 5485 | NewVD->setPreviousDeclInSameBlockScope( |
| 5486 | Previous.isSingleResult() && !Previous.isShadowed() && |
Richard Smith | a41c97a | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 5487 | isDeclInScope(Previous.getFoundDecl(), OriginalDC, S, false)); |
Richard Smith | dd9459f | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 5488 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5489 | if (!getLangOpts().CPlusPlus) { |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 5490 | D.setRedeclaration(CheckVariableDeclaration(NewVD, Previous)); |
| 5491 | } else { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5492 | // If this is an explicit specialization of a static data member, check it. |
| 5493 | if (IsExplicitSpecialization && !NewVD->isInvalidDecl() && |
| 5494 | CheckMemberSpecialization(NewVD, Previous)) |
| 5495 | NewVD->setInvalidDecl(); |
| 5496 | |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 5497 | // Merge the decl with the existing one if appropriate. |
| 5498 | if (!Previous.empty()) { |
| 5499 | if (Previous.isSingleResult() && |
| 5500 | isa<FieldDecl>(Previous.getFoundDecl()) && |
| 5501 | D.getCXXScopeSpec().isSet()) { |
| 5502 | // The user tried to define a non-static data member |
| 5503 | // out-of-line (C++ [dcl.meaning]p1). |
| 5504 | Diag(NewVD->getLocation(), diag::err_nonstatic_member_out_of_line) |
| 5505 | << D.getCXXScopeSpec().getRange(); |
| 5506 | Previous.clear(); |
| 5507 | NewVD->setInvalidDecl(); |
| 5508 | } |
| 5509 | } else if (D.getCXXScopeSpec().isSet()) { |
| 5510 | // No previous declaration in the qualifying scope. |
| 5511 | Diag(D.getIdentifierLoc(), diag::err_no_member) |
| 5512 | << Name << computeDeclContext(D.getCXXScopeSpec(), true) |
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 5513 | << D.getCXXScopeSpec().getRange(); |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 5514 | NewVD->setInvalidDecl(); |
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 5515 | } |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 5516 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5517 | if (!IsVariableTemplateSpecialization) |
| 5518 | D.setRedeclaration(CheckVariableDeclaration(NewVD, Previous)); |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 5519 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5520 | if (NewTemplate) { |
| 5521 | VarTemplateDecl *PrevVarTemplate = |
| 5522 | NewVD->getPreviousDecl() |
| 5523 | ? NewVD->getPreviousDecl()->getDescribedVarTemplate() |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 5524 | : nullptr; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5525 | |
| 5526 | // Check the template parameter list of this declaration, possibly |
| 5527 | // merging in the template parameter list from the previous variable |
| 5528 | // template declaration. |
| 5529 | if (CheckTemplateParameterList( |
| 5530 | TemplateParams, |
| 5531 | PrevVarTemplate ? PrevVarTemplate->getTemplateParameters() |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 5532 | : nullptr, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5533 | (D.getCXXScopeSpec().isSet() && DC && DC->isRecord() && |
| 5534 | DC->isDependentContext()) |
| 5535 | ? TPC_ClassTemplateMember |
| 5536 | : TPC_VarTemplate)) |
| 5537 | NewVD->setInvalidDecl(); |
| 5538 | |
| 5539 | // If we are providing an explicit specialization of a static variable |
| 5540 | // template, make a note of that. |
| 5541 | if (PrevVarTemplate && |
| 5542 | PrevVarTemplate->getInstantiatedFromMemberTemplate()) |
| 5543 | PrevVarTemplate->setMemberSpecialization(); |
| 5544 | } |
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 5545 | } |
Ryan Flynn | 478fbc6 | 2009-07-25 22:29:44 +0000 | [diff] [blame] | 5546 | |
Rafael Espindola | 65611bf | 2013-03-02 21:41:48 +0000 | [diff] [blame] | 5547 | ProcessPragmaWeak(S, NewVD); |
Rafael Espindola | 2a5bb50 | 2013-01-16 23:11:15 +0000 | [diff] [blame] | 5548 | |
Richard Smith | aa4bc18 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 5549 | // If this is the first declaration of an extern C variable, update |
| 5550 | // the map of such variables. |
Rafael Espindola | 7693b32 | 2013-10-19 02:13:21 +0000 | [diff] [blame] | 5551 | if (NewVD->isFirstDecl() && !NewVD->isInvalidDecl() && |
Richard Smith | aa4bc18 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 5552 | isIncompleteDeclExternC(*this, NewVD)) |
Richard Smith | 662f41b | 2013-06-18 20:15:12 +0000 | [diff] [blame] | 5553 | RegisterLocallyScopedExternCDecl(NewVD, S); |
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 5554 | |
Reid Kleckner | 942f9fe | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 5555 | if (getLangOpts().CPlusPlus && NewVD->isStaticLocal()) { |
Eli Friedman | 5e867c8 | 2013-07-10 00:30:46 +0000 | [diff] [blame] | 5556 | Decl *ManglingContextDecl; |
| 5557 | if (MangleNumberingContext *MCtx = |
| 5558 | getCurrentMangleNumberContext(NewVD->getDeclContext(), |
| 5559 | ManglingContextDecl)) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5560 | Context.setManglingNumber( |
| 5561 | NewVD, MCtx->getManglingNumber(NewVD, S->getMSLocalManglingNumber())); |
| 5562 | Context.setStaticLocalNumber(NewVD, MCtx->getStaticLocalNumber(NewVD)); |
Eli Friedman | 5e867c8 | 2013-07-10 00:30:46 +0000 | [diff] [blame] | 5563 | } |
| 5564 | } |
| 5565 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5566 | if (D.isRedeclaration() && !Previous.empty()) { |
| 5567 | checkDLLAttributeRedeclaration( |
| 5568 | *this, dyn_cast<NamedDecl>(Previous.getRepresentativeDecl()), NewVD, |
| 5569 | IsExplicitSpecialization); |
| 5570 | } |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5571 | |
Larisse Voufo | 567f917 | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 5572 | if (NewTemplate) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5573 | if (NewVD->isInvalidDecl()) |
| 5574 | NewTemplate->setInvalidDecl(); |
Larisse Voufo | 567f917 | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 5575 | ActOnDocumentableDecl(NewTemplate); |
| 5576 | return NewTemplate; |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5577 | } |
| 5578 | |
Larisse Voufo | 567f917 | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 5579 | return NewVD; |
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 5580 | } |
| 5581 | |
John McCall | 053f4bd | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 5582 | /// \brief Diagnose variable or built-in function shadowing. Implements |
| 5583 | /// -Wshadow. |
John McCall | 8472af4 | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 5584 | /// |
John McCall | 053f4bd | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 5585 | /// This method is called whenever a VarDecl is added to a "useful" |
| 5586 | /// scope. |
John McCall | 8472af4 | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 5587 | /// |
John McCall | a369a95 | 2010-03-20 04:12:52 +0000 | [diff] [blame] | 5588 | /// \param S the scope in which the shadowing name is being declared |
| 5589 | /// \param R the lookup of the name |
John McCall | 8472af4 | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 5590 | /// |
John McCall | 053f4bd | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 5591 | void Sema::CheckShadow(Scope *S, VarDecl *D, const LookupResult& R) { |
John McCall | 8472af4 | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 5592 | // Return if warning is ignored. |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 5593 | if (Diags.getDiagnosticLevel(diag::warn_decl_shadow, R.getNameLoc()) == |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 5594 | DiagnosticsEngine::Ignored) |
John McCall | 8472af4 | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 5595 | return; |
| 5596 | |
Argyrios Kyrtzidis | 651f86f | 2011-02-08 18:21:25 +0000 | [diff] [blame] | 5597 | // Don't diagnose declarations at file scope. |
Argyrios Kyrtzidis | 865dd8c | 2011-04-25 21:39:50 +0000 | [diff] [blame] | 5598 | if (D->hasGlobalStorage()) |
John McCall | 8472af4 | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 5599 | return; |
Argyrios Kyrtzidis | 865dd8c | 2011-04-25 21:39:50 +0000 | [diff] [blame] | 5600 | |
| 5601 | DeclContext *NewDC = D->getDeclContext(); |
| 5602 | |
John McCall | a369a95 | 2010-03-20 04:12:52 +0000 | [diff] [blame] | 5603 | // Only diagnose if we're shadowing an unambiguous field or variable. |
Douglas Gregor | c48c916 | 2010-03-17 16:03:44 +0000 | [diff] [blame] | 5604 | if (R.getResultKind() != LookupResult::Found) |
John McCall | 8472af4 | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 5605 | return; |
John McCall | 8472af4 | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 5606 | |
John McCall | 8472af4 | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 5607 | NamedDecl* ShadowedDecl = R.getFoundDecl(); |
| 5608 | if (!isa<VarDecl>(ShadowedDecl) && !isa<FieldDecl>(ShadowedDecl)) |
| 5609 | return; |
| 5610 | |
Argyrios Kyrtzidis | 36eb5e4 | 2011-01-31 07:04:54 +0000 | [diff] [blame] | 5611 | // Fields are not shadowed by variables in C++ static methods. |
| 5612 | if (isa<FieldDecl>(ShadowedDecl)) |
| 5613 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(NewDC)) |
| 5614 | if (MD->isStatic()) |
| 5615 | return; |
| 5616 | |
Argyrios Kyrtzidis | 49a6172 | 2011-01-31 07:04:50 +0000 | [diff] [blame] | 5617 | if (VarDecl *shadowedVar = dyn_cast<VarDecl>(ShadowedDecl)) |
| 5618 | if (shadowedVar->isExternC()) { |
Argyrios Kyrtzidis | 49a6172 | 2011-01-31 07:04:50 +0000 | [diff] [blame] | 5619 | // For shadowing external vars, make sure that we point to the global |
| 5620 | // declaration, not a locally scoped extern declaration. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5621 | for (auto I : shadowedVar->redecls()) |
Argyrios Kyrtzidis | 49a6172 | 2011-01-31 07:04:50 +0000 | [diff] [blame] | 5622 | if (I->isFileVarDecl()) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5623 | ShadowedDecl = I; |
Argyrios Kyrtzidis | 49a6172 | 2011-01-31 07:04:50 +0000 | [diff] [blame] | 5624 | break; |
| 5625 | } |
| 5626 | } |
| 5627 | |
| 5628 | DeclContext *OldDC = ShadowedDecl->getDeclContext(); |
| 5629 | |
John McCall | a369a95 | 2010-03-20 04:12:52 +0000 | [diff] [blame] | 5630 | // Only warn about certain kinds of shadowing for class members. |
| 5631 | if (NewDC && NewDC->isRecord()) { |
| 5632 | // In particular, don't warn about shadowing non-class members. |
| 5633 | if (!OldDC->isRecord()) |
| 5634 | return; |
| 5635 | |
| 5636 | // TODO: should we warn about static data members shadowing |
| 5637 | // static data members from base classes? |
| 5638 | |
| 5639 | // TODO: don't diagnose for inaccessible shadowed members. |
| 5640 | // This is hard to do perfectly because we might friend the |
| 5641 | // shadowing context, but that's just a false negative. |
| 5642 | } |
| 5643 | |
| 5644 | // Determine what kind of declaration we're shadowing. |
John McCall | 8472af4 | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 5645 | unsigned Kind; |
John McCall | a369a95 | 2010-03-20 04:12:52 +0000 | [diff] [blame] | 5646 | if (isa<RecordDecl>(OldDC)) { |
John McCall | 8472af4 | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 5647 | if (isa<FieldDecl>(ShadowedDecl)) |
| 5648 | Kind = 3; // field |
| 5649 | else |
| 5650 | Kind = 2; // static data member |
John McCall | a369a95 | 2010-03-20 04:12:52 +0000 | [diff] [blame] | 5651 | } else if (OldDC->isFileContext()) |
John McCall | 8472af4 | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 5652 | Kind = 1; // global |
| 5653 | else |
| 5654 | Kind = 0; // local |
| 5655 | |
John McCall | a369a95 | 2010-03-20 04:12:52 +0000 | [diff] [blame] | 5656 | DeclarationName Name = R.getLookupName(); |
| 5657 | |
John McCall | 8472af4 | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 5658 | // Emit warning and note. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5659 | if (getSourceManager().isInSystemMacro(R.getNameLoc())) |
| 5660 | return; |
John McCall | a369a95 | 2010-03-20 04:12:52 +0000 | [diff] [blame] | 5661 | Diag(R.getNameLoc(), diag::warn_decl_shadow) << Name << Kind << OldDC; |
John McCall | 8472af4 | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 5662 | Diag(ShadowedDecl->getLocation(), diag::note_previous_declaration); |
| 5663 | } |
| 5664 | |
John McCall | 053f4bd | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 5665 | /// \brief Check -Wshadow without the advantage of a previous lookup. |
| 5666 | void Sema::CheckShadow(Scope *S, VarDecl *D) { |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 5667 | if (Diags.getDiagnosticLevel(diag::warn_decl_shadow, D->getLocation()) == |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 5668 | DiagnosticsEngine::Ignored) |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 5669 | return; |
| 5670 | |
John McCall | 053f4bd | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 5671 | LookupResult R(*this, D->getDeclName(), D->getLocation(), |
| 5672 | Sema::LookupOrdinaryName, Sema::ForRedeclaration); |
| 5673 | LookupName(R, S); |
| 5674 | CheckShadow(S, D, R); |
| 5675 | } |
| 5676 | |
Richard Smith | aa4bc18 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 5677 | /// Check for conflict between this global or extern "C" declaration and |
| 5678 | /// previous global or extern "C" declarations. This is only used in C++. |
Rafael Espindola | 294ddc6 | 2013-01-11 19:34:23 +0000 | [diff] [blame] | 5679 | template<typename T> |
Richard Smith | aa4bc18 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 5680 | static bool checkGlobalOrExternCConflict( |
| 5681 | Sema &S, const T *ND, bool IsGlobal, LookupResult &Previous) { |
| 5682 | assert(S.getLangOpts().CPlusPlus && "only C++ has extern \"C\""); |
| 5683 | NamedDecl *Prev = S.findLocallyScopedExternCDecl(ND->getDeclName()); |
Rafael Espindola | 2d1b096 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 5684 | |
Richard Smith | aa4bc18 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 5685 | if (!Prev && IsGlobal && !isIncompleteDeclExternC(S, ND)) { |
| 5686 | // The common case: this global doesn't conflict with any extern "C" |
| 5687 | // declaration. |
| 5688 | return false; |
| 5689 | } |
| 5690 | |
| 5691 | if (Prev) { |
| 5692 | if (!IsGlobal || isIncompleteDeclExternC(S, ND)) { |
| 5693 | // Both the old and new declarations have C language linkage. This is a |
| 5694 | // redeclaration. |
| 5695 | Previous.clear(); |
| 5696 | Previous.addDecl(Prev); |
| 5697 | return true; |
| 5698 | } |
| 5699 | |
| 5700 | // This is a global, non-extern "C" declaration, and there is a previous |
| 5701 | // non-global extern "C" declaration. Diagnose if this is a variable |
| 5702 | // declaration. |
| 5703 | if (!isa<VarDecl>(ND)) |
| 5704 | return false; |
| 5705 | } else { |
| 5706 | // The declaration is extern "C". Check for any declaration in the |
| 5707 | // translation unit which might conflict. |
| 5708 | if (IsGlobal) { |
| 5709 | // We have already performed the lookup into the translation unit. |
| 5710 | IsGlobal = false; |
| 5711 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 5712 | I != E; ++I) { |
| 5713 | if (isa<VarDecl>(*I)) { |
| 5714 | Prev = *I; |
| 5715 | break; |
| 5716 | } |
| 5717 | } |
| 5718 | } else { |
| 5719 | DeclContext::lookup_result R = |
| 5720 | S.Context.getTranslationUnitDecl()->lookup(ND->getDeclName()); |
| 5721 | for (DeclContext::lookup_result::iterator I = R.begin(), E = R.end(); |
| 5722 | I != E; ++I) { |
| 5723 | if (isa<VarDecl>(*I)) { |
| 5724 | Prev = *I; |
| 5725 | break; |
| 5726 | } |
| 5727 | // FIXME: If we have any other entity with this name in global scope, |
| 5728 | // the declaration is ill-formed, but that is a defect: it breaks the |
| 5729 | // 'stat' hack, for instance. Only variables can have mangled name |
| 5730 | // clashes with extern "C" declarations, so only they deserve a |
| 5731 | // diagnostic. |
| 5732 | } |
| 5733 | } |
| 5734 | |
| 5735 | if (!Prev) |
Rafael Espindola | 2d1b096 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 5736 | return false; |
| 5737 | } |
| 5738 | |
Richard Smith | aa4bc18 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 5739 | // Use the first declaration's location to ensure we point at something which |
| 5740 | // is lexically inside an extern "C" linkage-spec. |
| 5741 | assert(Prev && "should have found a previous declaration to diagnose"); |
| 5742 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(Prev)) |
Rafael Espindola | bc65091 | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 5743 | Prev = FD->getFirstDecl(); |
Richard Smith | aa4bc18 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 5744 | else |
Rafael Espindola | bc65091 | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 5745 | Prev = cast<VarDecl>(Prev)->getFirstDecl(); |
Richard Smith | aa4bc18 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 5746 | |
| 5747 | S.Diag(ND->getLocation(), diag::err_extern_c_global_conflict) |
| 5748 | << IsGlobal << ND; |
| 5749 | S.Diag(Prev->getLocation(), diag::note_extern_c_global_conflict) |
| 5750 | << IsGlobal; |
| 5751 | return false; |
| 5752 | } |
| 5753 | |
| 5754 | /// Apply special rules for handling extern "C" declarations. Returns \c true |
| 5755 | /// if we have found that this is a redeclaration of some prior entity. |
| 5756 | /// |
| 5757 | /// Per C++ [dcl.link]p6: |
| 5758 | /// Two declarations [for a function or variable] with C language linkage |
| 5759 | /// with the same name that appear in different scopes refer to the same |
| 5760 | /// [entity]. An entity with C language linkage shall not be declared with |
| 5761 | /// the same name as an entity in global scope. |
| 5762 | template<typename T> |
| 5763 | static bool checkForConflictWithNonVisibleExternC(Sema &S, const T *ND, |
| 5764 | LookupResult &Previous) { |
| 5765 | if (!S.getLangOpts().CPlusPlus) { |
| 5766 | // In C, when declaring a global variable, look for a corresponding 'extern' |
Richard Smith | a41c97a | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 5767 | // variable declared in function scope. We don't need this in C++, because |
| 5768 | // we find local extern decls in the surrounding file-scope DeclContext. |
Richard Smith | aa4bc18 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 5769 | if (ND->getDeclContext()->getRedeclContext()->isTranslationUnit()) { |
| 5770 | if (NamedDecl *Prev = S.findLocallyScopedExternCDecl(ND->getDeclName())) { |
| 5771 | Previous.clear(); |
| 5772 | Previous.addDecl(Prev); |
| 5773 | return true; |
| 5774 | } |
| 5775 | } |
| 5776 | return false; |
| 5777 | } |
| 5778 | |
| 5779 | // A declaration in the translation unit can conflict with an extern "C" |
| 5780 | // declaration. |
| 5781 | if (ND->getDeclContext()->getRedeclContext()->isTranslationUnit()) |
| 5782 | return checkGlobalOrExternCConflict(S, ND, /*IsGlobal*/true, Previous); |
| 5783 | |
| 5784 | // An extern "C" declaration can conflict with a declaration in the |
| 5785 | // translation unit or can be a redeclaration of an extern "C" declaration |
| 5786 | // in another scope. |
| 5787 | if (isIncompleteDeclExternC(S,ND)) |
| 5788 | return checkGlobalOrExternCConflict(S, ND, /*IsGlobal*/false, Previous); |
| 5789 | |
| 5790 | // Neither global nor extern "C": nothing to do. |
| 5791 | return false; |
Rafael Espindola | 294ddc6 | 2013-01-11 19:34:23 +0000 | [diff] [blame] | 5792 | } |
| 5793 | |
Richard Smith | dc7a4f5 | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 5794 | void Sema::CheckVariableDeclarationType(VarDecl *NewVD) { |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 5795 | // If the decl is already known invalid, don't check it. |
| 5796 | if (NewVD->isInvalidDecl()) |
Richard Smith | dc7a4f5 | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 5797 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5798 | |
Abramo Bagnara | 4c5750e | 2012-11-08 14:44:42 +0000 | [diff] [blame] | 5799 | TypeSourceInfo *TInfo = NewVD->getTypeSourceInfo(); |
| 5800 | QualType T = TInfo->getType(); |
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 5801 | |
Richard Smith | dc7a4f5 | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 5802 | // Defer checking an 'auto' type until its initializer is attached. |
| 5803 | if (T->isUndeducedType()) |
| 5804 | return; |
| 5805 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5806 | if (NewVD->hasAttrs()) |
| 5807 | CheckAlignasUnderalignment(NewVD); |
| 5808 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5809 | if (T->isObjCObjectType()) { |
Fariborz Jahanian | dcf1011 | 2011-07-25 21:12:27 +0000 | [diff] [blame] | 5810 | Diag(NewVD->getLocation(), diag::err_statically_allocated_object) |
| 5811 | << FixItHint::CreateInsertion(NewVD->getLocation(), "*"); |
Fariborz Jahanian | 8eaefdc | 2011-07-26 17:58:54 +0000 | [diff] [blame] | 5812 | T = Context.getObjCObjectPointerType(T); |
| 5813 | NewVD->setType(T); |
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 5814 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5815 | |
Zhongxing Xu | cb8f4f1 | 2009-01-16 02:36:34 +0000 | [diff] [blame] | 5816 | // Emit an error if an address space was applied to decl with local storage. |
| 5817 | // This includes arrays of objects with address space qualifiers, but not |
| 5818 | // automatic variables that point to other address spaces. |
| 5819 | // ISO/IEC TR 18037 S5.1.2 |
Chris Lattner | 16c5dea | 2010-10-10 18:16:20 +0000 | [diff] [blame] | 5820 | if (NewVD->hasLocalStorage() && T.getAddressSpace() != 0) { |
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 5821 | Diag(NewVD->getLocation(), diag::err_as_qualified_auto_decl); |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 5822 | NewVD->setInvalidDecl(); |
Richard Smith | dc7a4f5 | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 5823 | return; |
Zhongxing Xu | cb8f4f1 | 2009-01-16 02:36:34 +0000 | [diff] [blame] | 5824 | } |
Fariborz Jahanian | 7b5b317 | 2009-02-21 19:44:02 +0000 | [diff] [blame] | 5825 | |
Tanya Lattner | 8aa86d1 | 2013-04-05 20:14:50 +0000 | [diff] [blame] | 5826 | // OpenCL v1.2 s6.5 - All program scope variables must be declared in the |
| 5827 | // __constant address space. |
| 5828 | if (getLangOpts().OpenCL && NewVD->isFileVarDecl() |
| 5829 | && T.getAddressSpace() != LangAS::opencl_constant |
| 5830 | && !T->isSamplerT()){ |
| 5831 | Diag(NewVD->getLocation(), diag::err_opencl_global_invalid_addr_space); |
| 5832 | NewVD->setInvalidDecl(); |
Richard Smith | dc7a4f5 | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 5833 | return; |
Tanya Lattner | 8aa86d1 | 2013-04-05 20:14:50 +0000 | [diff] [blame] | 5834 | } |
| 5835 | |
Tanya Lattner | 5e94d6f | 2012-06-19 23:09:52 +0000 | [diff] [blame] | 5836 | // OpenCL v1.2 s6.8 -- The static qualifier is valid only in program |
| 5837 | // scope. |
| 5838 | if ((getLangOpts().OpenCLVersion >= 120) |
| 5839 | && NewVD->isStaticLocal()) { |
| 5840 | Diag(NewVD->getLocation(), diag::err_static_function_scope); |
| 5841 | NewVD->setInvalidDecl(); |
Richard Smith | dc7a4f5 | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 5842 | return; |
Tanya Lattner | 5e94d6f | 2012-06-19 23:09:52 +0000 | [diff] [blame] | 5843 | } |
| 5844 | |
Mike Stump | f33651c | 2009-04-14 00:57:29 +0000 | [diff] [blame] | 5845 | if (NewVD->hasLocalStorage() && T.isObjCGCWeak() |
Fariborz Jahanian | 175df89 | 2011-06-07 20:15:46 +0000 | [diff] [blame] | 5846 | && !NewVD->hasAttr<BlocksAttr>()) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5847 | if (getLangOpts().getGC() != LangOptions::NonGC) |
Fariborz Jahanian | 175df89 | 2011-06-07 20:15:46 +0000 | [diff] [blame] | 5848 | Diag(NewVD->getLocation(), diag::warn_gc_attribute_weak_on_local); |
Ted Kremenek | 3ba17ee | 2012-10-02 05:36:02 +0000 | [diff] [blame] | 5849 | else { |
| 5850 | assert(!getLangOpts().ObjCAutoRefCount); |
Fariborz Jahanian | 175df89 | 2011-06-07 20:15:46 +0000 | [diff] [blame] | 5851 | Diag(NewVD->getLocation(), diag::warn_attribute_weak_on_local); |
Ted Kremenek | 3ba17ee | 2012-10-02 05:36:02 +0000 | [diff] [blame] | 5852 | } |
Fariborz Jahanian | 175df89 | 2011-06-07 20:15:46 +0000 | [diff] [blame] | 5853 | } |
Chris Lattner | 16c5dea | 2010-10-10 18:16:20 +0000 | [diff] [blame] | 5854 | |
Chris Lattner | 38c5ebd | 2009-04-19 05:21:20 +0000 | [diff] [blame] | 5855 | bool isVM = T->isVariablyModifiedType(); |
Chris Lattner | be6d259 | 2009-07-19 20:17:11 +0000 | [diff] [blame] | 5856 | if (isVM || NewVD->hasAttr<CleanupAttr>() || |
John McCall | e46f62c | 2010-08-01 01:24:59 +0000 | [diff] [blame] | 5857 | NewVD->hasAttr<BlocksAttr>()) |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 5858 | getCurFunction()->setHasBranchProtectedScope(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5859 | |
Chris Lattner | 38c5ebd | 2009-04-19 05:21:20 +0000 | [diff] [blame] | 5860 | if ((isVM && NewVD->hasLinkage()) || |
| 5861 | (T->isVariableArrayType() && NewVD->hasGlobalStorage())) { |
Anders Carlsson | 1a7acfa | 2009-02-28 21:56:50 +0000 | [diff] [blame] | 5862 | bool SizeIsNegative; |
Douglas Gregor | 2767ce2 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 5863 | llvm::APSInt Oversized; |
Abramo Bagnara | 4c5750e | 2012-11-08 14:44:42 +0000 | [diff] [blame] | 5864 | TypeSourceInfo *FixedTInfo = |
| 5865 | TryToFixInvalidVariablyModifiedTypeSourceInfo(TInfo, Context, |
| 5866 | SizeIsNegative, Oversized); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 5867 | if (!FixedTInfo && T->isVariableArrayType()) { |
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 5868 | const VariableArrayType *VAT = Context.getAsVariableArrayType(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5869 | // FIXME: This won't give the correct result for |
| 5870 | // int a[10][n]; |
Anders Carlsson | 1a7acfa | 2009-02-28 21:56:50 +0000 | [diff] [blame] | 5871 | SourceRange SizeRange = VAT->getSizeExpr()->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5872 | |
Anders Carlsson | 1a7acfa | 2009-02-28 21:56:50 +0000 | [diff] [blame] | 5873 | if (NewVD->isFileVarDecl()) |
| 5874 | Diag(NewVD->getLocation(), diag::err_vla_decl_in_file_scope) |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 5875 | << SizeRange; |
Enea Zaffanella | 9cbcab8 | 2013-05-10 20:34:44 +0000 | [diff] [blame] | 5876 | else if (NewVD->isStaticLocal()) |
Anders Carlsson | 1a7acfa | 2009-02-28 21:56:50 +0000 | [diff] [blame] | 5877 | Diag(NewVD->getLocation(), diag::err_vla_decl_has_static_storage) |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 5878 | << SizeRange; |
Anders Carlsson | 1a7acfa | 2009-02-28 21:56:50 +0000 | [diff] [blame] | 5879 | else |
| 5880 | Diag(NewVD->getLocation(), diag::err_vla_decl_has_extern_linkage) |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 5881 | << SizeRange; |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 5882 | NewVD->setInvalidDecl(); |
Richard Smith | dc7a4f5 | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 5883 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5884 | } |
| 5885 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 5886 | if (!FixedTInfo) { |
Anders Carlsson | 1a7acfa | 2009-02-28 21:56:50 +0000 | [diff] [blame] | 5887 | if (NewVD->isFileVarDecl()) |
| 5888 | Diag(NewVD->getLocation(), diag::err_vm_decl_in_file_scope); |
| 5889 | else |
| 5890 | Diag(NewVD->getLocation(), diag::err_vm_decl_has_extern_linkage); |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 5891 | NewVD->setInvalidDecl(); |
Richard Smith | dc7a4f5 | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 5892 | return; |
Anders Carlsson | 1a7acfa | 2009-02-28 21:56:50 +0000 | [diff] [blame] | 5893 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5894 | |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 5895 | Diag(NewVD->getLocation(), diag::warn_illegal_constant_array_size); |
Abramo Bagnara | eae859a | 2012-11-08 16:01:51 +0000 | [diff] [blame] | 5896 | NewVD->setType(FixedTInfo->getType()); |
Abramo Bagnara | 4c5750e | 2012-11-08 14:44:42 +0000 | [diff] [blame] | 5897 | NewVD->setTypeSourceInfo(FixedTInfo); |
Anders Carlsson | 1a7acfa | 2009-02-28 21:56:50 +0000 | [diff] [blame] | 5898 | } |
| 5899 | |
David Majnemer | aa71567 | 2013-05-29 00:56:45 +0000 | [diff] [blame] | 5900 | if (T->isVoidType()) { |
| 5901 | // C++98 [dcl.stc]p5: The extern specifier can be applied only to the names |
| 5902 | // of objects and functions. |
| 5903 | if (NewVD->isThisDeclarationADefinition() || getLangOpts().CPlusPlus) { |
| 5904 | Diag(NewVD->getLocation(), diag::err_typecheck_decl_incomplete_type) |
| 5905 | << T; |
| 5906 | NewVD->setInvalidDecl(); |
| 5907 | return; |
| 5908 | } |
Richard Smith | dc7a4f5 | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 5909 | } |
| 5910 | |
| 5911 | if (!NewVD->hasLocalStorage() && NewVD->hasAttr<BlocksAttr>()) { |
| 5912 | Diag(NewVD->getLocation(), diag::err_block_on_nonlocal); |
| 5913 | NewVD->setInvalidDecl(); |
| 5914 | return; |
| 5915 | } |
| 5916 | |
| 5917 | if (isVM && NewVD->hasAttr<BlocksAttr>()) { |
| 5918 | Diag(NewVD->getLocation(), diag::err_block_on_vm); |
| 5919 | NewVD->setInvalidDecl(); |
| 5920 | return; |
| 5921 | } |
| 5922 | |
| 5923 | if (NewVD->isConstexpr() && !T->isDependentType() && |
| 5924 | RequireLiteralType(NewVD->getLocation(), T, |
| 5925 | diag::err_constexpr_var_non_literal)) { |
Richard Smith | dc7a4f5 | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 5926 | NewVD->setInvalidDecl(); |
| 5927 | return; |
| 5928 | } |
| 5929 | } |
| 5930 | |
| 5931 | /// \brief Perform semantic checking on a newly-created variable |
| 5932 | /// declaration. |
| 5933 | /// |
| 5934 | /// This routine performs all of the type-checking required for a |
| 5935 | /// variable declaration once it has been built. It is used both to |
| 5936 | /// check variables after they have been parsed and their declarators |
| 5937 | /// have been translated into a declaration, and to check variables |
| 5938 | /// that have been instantiated from a template. |
| 5939 | /// |
| 5940 | /// Sets NewVD->isInvalidDecl() if an error was encountered. |
| 5941 | /// |
| 5942 | /// Returns true if the variable declaration is a redeclaration. |
Larisse Voufo | 567f917 | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 5943 | bool Sema::CheckVariableDeclaration(VarDecl *NewVD, LookupResult &Previous) { |
Richard Smith | dc7a4f5 | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 5944 | CheckVariableDeclarationType(NewVD); |
| 5945 | |
| 5946 | // If the decl is already known invalid, don't check it. |
| 5947 | if (NewVD->isInvalidDecl()) |
| 5948 | return false; |
| 5949 | |
John McCall | 5b8740f | 2013-04-01 18:34:28 +0000 | [diff] [blame] | 5950 | // If we did not find anything by this name, look for a non-visible |
| 5951 | // extern "C" declaration with the same name. |
Richard Smith | dd9459f | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 5952 | if (Previous.empty() && |
| 5953 | checkForConflictWithNonVisibleExternC(*this, NewVD, Previous)) |
Richard Smith | 99a7238 | 2013-09-03 21:00:58 +0000 | [diff] [blame] | 5954 | Previous.setShadowed(); |
Douglas Gregor | 6393519 | 2009-03-02 00:19:53 +0000 | [diff] [blame] | 5955 | |
Douglas Gregor | 7dc80e1 | 2013-01-09 00:47:56 +0000 | [diff] [blame] | 5956 | // Filter out any non-conflicting previous declarations. |
| 5957 | filterNonConflictingPreviousDecls(Context, NewVD, Previous); |
| 5958 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5959 | if (!Previous.empty()) { |
Richard Smith | 99a7238 | 2013-09-03 21:00:58 +0000 | [diff] [blame] | 5960 | MergeVarDecl(NewVD, Previous); |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 5961 | return true; |
Zhongxing Xu | cb8f4f1 | 2009-01-16 02:36:34 +0000 | [diff] [blame] | 5962 | } |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 5963 | return false; |
Zhongxing Xu | cb8f4f1 | 2009-01-16 02:36:34 +0000 | [diff] [blame] | 5964 | } |
| 5965 | |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 5966 | /// \brief Data used with FindOverriddenMethod |
| 5967 | struct FindOverriddenMethodData { |
| 5968 | Sema *S; |
| 5969 | CXXMethodDecl *Method; |
| 5970 | }; |
| 5971 | |
| 5972 | /// \brief Member lookup function that determines whether a given C++ |
| 5973 | /// method overrides a method in a base class, to be used with |
| 5974 | /// CXXRecordDecl::lookupInBases(). |
John McCall | af8e6ed | 2009-11-12 03:15:40 +0000 | [diff] [blame] | 5975 | static bool FindOverriddenMethod(const CXXBaseSpecifier *Specifier, |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 5976 | CXXBasePath &Path, |
| 5977 | void *UserData) { |
| 5978 | RecordDecl *BaseRecord = Specifier->getType()->getAs<RecordType>()->getDecl(); |
Anders Carlsson | 9549680 | 2009-11-26 20:50:40 +0000 | [diff] [blame] | 5979 | |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 5980 | FindOverriddenMethodData *Data |
| 5981 | = reinterpret_cast<FindOverriddenMethodData*>(UserData); |
Anders Carlsson | 9549680 | 2009-11-26 20:50:40 +0000 | [diff] [blame] | 5982 | |
| 5983 | DeclarationName Name = Data->Method->getDeclName(); |
| 5984 | |
| 5985 | // FIXME: Do we care about other names here too? |
| 5986 | if (Name.getNameKind() == DeclarationName::CXXDestructorName) { |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 5987 | // We really want to find the base class destructor here. |
Anders Carlsson | 9549680 | 2009-11-26 20:50:40 +0000 | [diff] [blame] | 5988 | QualType T = Data->S->Context.getTypeDeclType(BaseRecord); |
| 5989 | CanQualType CT = Data->S->Context.getCanonicalType(T); |
| 5990 | |
Anders Carlsson | 1a68972 | 2009-11-27 01:26:58 +0000 | [diff] [blame] | 5991 | Name = Data->S->Context.DeclarationNames.getCXXDestructorName(CT); |
Anders Carlsson | 9549680 | 2009-11-26 20:50:40 +0000 | [diff] [blame] | 5992 | } |
| 5993 | |
| 5994 | for (Path.Decls = BaseRecord->lookup(Name); |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 5995 | !Path.Decls.empty(); |
| 5996 | Path.Decls = Path.Decls.slice(1)) { |
| 5997 | NamedDecl *D = Path.Decls.front(); |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 5998 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) { |
| 5999 | if (MD->isVirtual() && !Data->S->IsOverload(Data->Method, MD, false)) |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 6000 | return true; |
| 6001 | } |
| 6002 | } |
| 6003 | |
| 6004 | return false; |
| 6005 | } |
| 6006 | |
David Blaikie | 5708c18 | 2012-10-17 00:47:58 +0000 | [diff] [blame] | 6007 | namespace { |
| 6008 | enum OverrideErrorKind { OEK_All, OEK_NonDeleted, OEK_Deleted }; |
| 6009 | } |
| 6010 | /// \brief Report an error regarding overriding, along with any relevant |
| 6011 | /// overriden methods. |
| 6012 | /// |
| 6013 | /// \param DiagID the primary error to report. |
| 6014 | /// \param MD the overriding method. |
| 6015 | /// \param OEK which overrides to include as notes. |
| 6016 | static void ReportOverrides(Sema& S, unsigned DiagID, const CXXMethodDecl *MD, |
| 6017 | OverrideErrorKind OEK = OEK_All) { |
| 6018 | S.Diag(MD->getLocation(), DiagID) << MD->getDeclName(); |
| 6019 | for (CXXMethodDecl::method_iterator I = MD->begin_overridden_methods(), |
| 6020 | E = MD->end_overridden_methods(); |
| 6021 | I != E; ++I) { |
| 6022 | // This check (& the OEK parameter) could be replaced by a predicate, but |
| 6023 | // without lambdas that would be overkill. This is still nicer than writing |
| 6024 | // out the diag loop 3 times. |
| 6025 | if ((OEK == OEK_All) || |
| 6026 | (OEK == OEK_NonDeleted && !(*I)->isDeleted()) || |
| 6027 | (OEK == OEK_Deleted && (*I)->isDeleted())) |
| 6028 | S.Diag((*I)->getLocation(), diag::note_overridden_virtual_function); |
| 6029 | } |
| 6030 | } |
| 6031 | |
Sebastian Redl | a165da0 | 2009-11-18 21:51:29 +0000 | [diff] [blame] | 6032 | /// AddOverriddenMethods - See if a method overrides any in the base classes, |
| 6033 | /// and if so, check that it's a valid override and remember it. |
Douglas Gregor | a6c1e3a | 2010-10-13 22:55:32 +0000 | [diff] [blame] | 6034 | bool Sema::AddOverriddenMethods(CXXRecordDecl *DC, CXXMethodDecl *MD) { |
Sebastian Redl | a165da0 | 2009-11-18 21:51:29 +0000 | [diff] [blame] | 6035 | // Look for virtual methods in base classes that this method might override. |
| 6036 | CXXBasePaths Paths; |
| 6037 | FindOverriddenMethodData Data; |
| 6038 | Data.Method = MD; |
| 6039 | Data.S = this; |
David Blaikie | 5708c18 | 2012-10-17 00:47:58 +0000 | [diff] [blame] | 6040 | bool hasDeletedOverridenMethods = false; |
| 6041 | bool hasNonDeletedOverridenMethods = false; |
Douglas Gregor | a6c1e3a | 2010-10-13 22:55:32 +0000 | [diff] [blame] | 6042 | bool AddedAny = false; |
Sebastian Redl | a165da0 | 2009-11-18 21:51:29 +0000 | [diff] [blame] | 6043 | if (DC->lookupInBases(&FindOverriddenMethod, &Data, Paths)) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 6044 | for (auto *I : Paths.found_decls()) { |
| 6045 | if (CXXMethodDecl *OldMD = dyn_cast<CXXMethodDecl>(I)) { |
Richard Trieu | 304e233 | 2011-07-01 20:02:53 +0000 | [diff] [blame] | 6046 | MD->addOverriddenMethod(OldMD->getCanonicalDecl()); |
Sebastian Redl | a165da0 | 2009-11-18 21:51:29 +0000 | [diff] [blame] | 6047 | if (!CheckOverridingFunctionReturnType(MD, OldMD) && |
Aaron Ballman | fff3248 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 6048 | !CheckOverridingFunctionAttributes(MD, OldMD) && |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 6049 | !CheckOverridingFunctionExceptionSpec(MD, OldMD) && |
Anders Carlsson | 2e1c730 | 2011-01-20 16:25:36 +0000 | [diff] [blame] | 6050 | !CheckIfOverriddenFunctionIsMarkedFinal(MD, OldMD)) { |
David Blaikie | 5708c18 | 2012-10-17 00:47:58 +0000 | [diff] [blame] | 6051 | hasDeletedOverridenMethods |= OldMD->isDeleted(); |
| 6052 | hasNonDeletedOverridenMethods |= !OldMD->isDeleted(); |
Douglas Gregor | a6c1e3a | 2010-10-13 22:55:32 +0000 | [diff] [blame] | 6053 | AddedAny = true; |
| 6054 | } |
Sebastian Redl | a165da0 | 2009-11-18 21:51:29 +0000 | [diff] [blame] | 6055 | } |
| 6056 | } |
| 6057 | } |
David Blaikie | 5708c18 | 2012-10-17 00:47:58 +0000 | [diff] [blame] | 6058 | |
| 6059 | if (hasDeletedOverridenMethods && !MD->isDeleted()) { |
| 6060 | ReportOverrides(*this, diag::err_non_deleted_override, MD, OEK_Deleted); |
| 6061 | } |
| 6062 | if (hasNonDeletedOverridenMethods && MD->isDeleted()) { |
| 6063 | ReportOverrides(*this, diag::err_deleted_override, MD, OEK_NonDeleted); |
| 6064 | } |
| 6065 | |
Douglas Gregor | a6c1e3a | 2010-10-13 22:55:32 +0000 | [diff] [blame] | 6066 | return AddedAny; |
Sebastian Redl | a165da0 | 2009-11-18 21:51:29 +0000 | [diff] [blame] | 6067 | } |
| 6068 | |
Kaelyn Uhrain | 2afd766 | 2011-10-11 00:28:39 +0000 | [diff] [blame] | 6069 | namespace { |
| 6070 | // Struct for holding all of the extra arguments needed by |
| 6071 | // DiagnoseInvalidRedeclaration to call Sema::ActOnFunctionDeclarator. |
| 6072 | struct ActOnFDArgs { |
| 6073 | Scope *S; |
| 6074 | Declarator &D; |
Kaelyn Uhrain | 2afd766 | 2011-10-11 00:28:39 +0000 | [diff] [blame] | 6075 | MultiTemplateParamsArg TemplateParamLists; |
Kaelyn Uhrain | 2afd766 | 2011-10-11 00:28:39 +0000 | [diff] [blame] | 6076 | bool AddToScope; |
| 6077 | }; |
| 6078 | } |
| 6079 | |
Kaelyn Uhrain | 43e875d | 2012-01-18 21:41:41 +0000 | [diff] [blame] | 6080 | namespace { |
| 6081 | |
| 6082 | // Callback to only accept typo corrections that have a non-zero edit distance. |
Kaelyn Uhrain | 3336353 | 2012-02-16 22:40:59 +0000 | [diff] [blame] | 6083 | // Also only accept corrections that have the same parent decl. |
Kaelyn Uhrain | 43e875d | 2012-01-18 21:41:41 +0000 | [diff] [blame] | 6084 | class DifferentNameValidatorCCC : public CorrectionCandidateCallback { |
| 6085 | public: |
Kaelyn Uhrain | ef094a1 | 2012-06-07 23:57:08 +0000 | [diff] [blame] | 6086 | DifferentNameValidatorCCC(ASTContext &Context, FunctionDecl *TypoFD, |
| 6087 | CXXRecordDecl *Parent) |
| 6088 | : Context(Context), OriginalFD(TypoFD), |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 6089 | ExpectedParent(Parent ? Parent->getCanonicalDecl() : nullptr) {} |
Kaelyn Uhrain | 3336353 | 2012-02-16 22:40:59 +0000 | [diff] [blame] | 6090 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 6091 | bool ValidateCandidate(const TypoCorrection &candidate) override { |
Kaelyn Uhrain | 3336353 | 2012-02-16 22:40:59 +0000 | [diff] [blame] | 6092 | if (candidate.getEditDistance() == 0) |
| 6093 | return false; |
| 6094 | |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 6095 | SmallVector<unsigned, 1> MismatchedParams; |
Kaelyn Uhrain | ef094a1 | 2012-06-07 23:57:08 +0000 | [diff] [blame] | 6096 | for (TypoCorrection::const_decl_iterator CDecl = candidate.begin(), |
| 6097 | CDeclEnd = candidate.end(); |
| 6098 | CDecl != CDeclEnd; ++CDecl) { |
| 6099 | FunctionDecl *FD = dyn_cast<FunctionDecl>(*CDecl); |
| 6100 | |
| 6101 | if (FD && !FD->hasBody() && |
| 6102 | hasSimilarParameters(Context, FD, OriginalFD, MismatchedParams)) { |
| 6103 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) { |
| 6104 | CXXRecordDecl *Parent = MD->getParent(); |
| 6105 | if (Parent && Parent->getCanonicalDecl() == ExpectedParent) |
| 6106 | return true; |
| 6107 | } else if (!ExpectedParent) { |
| 6108 | return true; |
| 6109 | } |
| 6110 | } |
Kaelyn Uhrain | 3336353 | 2012-02-16 22:40:59 +0000 | [diff] [blame] | 6111 | } |
| 6112 | |
Kaelyn Uhrain | ef094a1 | 2012-06-07 23:57:08 +0000 | [diff] [blame] | 6113 | return false; |
Kaelyn Uhrain | 43e875d | 2012-01-18 21:41:41 +0000 | [diff] [blame] | 6114 | } |
Kaelyn Uhrain | 3336353 | 2012-02-16 22:40:59 +0000 | [diff] [blame] | 6115 | |
| 6116 | private: |
Kaelyn Uhrain | ef094a1 | 2012-06-07 23:57:08 +0000 | [diff] [blame] | 6117 | ASTContext &Context; |
| 6118 | FunctionDecl *OriginalFD; |
Kaelyn Uhrain | 3336353 | 2012-02-16 22:40:59 +0000 | [diff] [blame] | 6119 | CXXRecordDecl *ExpectedParent; |
Kaelyn Uhrain | 43e875d | 2012-01-18 21:41:41 +0000 | [diff] [blame] | 6120 | }; |
| 6121 | |
| 6122 | } |
| 6123 | |
Kaelyn Uhrain | 2afd766 | 2011-10-11 00:28:39 +0000 | [diff] [blame] | 6124 | /// \brief Generate diagnostics for an invalid function redeclaration. |
| 6125 | /// |
| 6126 | /// This routine handles generating the diagnostic messages for an invalid |
| 6127 | /// function redeclaration, including finding possible similar declarations |
| 6128 | /// or performing typo correction if there are no previous declarations with |
| 6129 | /// the same name. |
| 6130 | /// |
Sylvestre Ledru | f3477c1 | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 6131 | /// Returns a NamedDecl iff typo correction was performed and substituting in |
Kaelyn Uhrain | 2afd766 | 2011-10-11 00:28:39 +0000 | [diff] [blame] | 6132 | /// the new declaration name does not cause new errors. |
Richard Smith | 4e9686b | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 6133 | static NamedDecl *DiagnoseInvalidRedeclaration( |
Kaelyn Uhrain | f09ce39 | 2011-10-11 00:28:52 +0000 | [diff] [blame] | 6134 | Sema &SemaRef, LookupResult &Previous, FunctionDecl *NewFD, |
Richard Smith | 4e9686b | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 6135 | ActOnFDArgs &ExtraArgs, bool IsLocalFriend, Scope *S) { |
Kaelyn Uhrain | 5161163 | 2011-08-18 18:19:12 +0000 | [diff] [blame] | 6136 | DeclarationName Name = NewFD->getDeclName(); |
Kaelyn Uhrain | 2afd766 | 2011-10-11 00:28:39 +0000 | [diff] [blame] | 6137 | DeclContext *NewDC = NewFD->getDeclContext(); |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 6138 | SmallVector<unsigned, 1> MismatchedParams; |
| 6139 | SmallVector<std::pair<FunctionDecl *, unsigned>, 1> NearMatches; |
Kaelyn Uhrain | 5161163 | 2011-08-18 18:19:12 +0000 | [diff] [blame] | 6140 | TypoCorrection Correction; |
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 6141 | bool IsDefinition = ExtraArgs.D.isFunctionDefinition(); |
Richard Smith | 4e9686b | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 6142 | unsigned DiagMsg = IsLocalFriend ? diag::err_no_matching_local_friend |
| 6143 | : diag::err_member_decl_does_not_match; |
| 6144 | LookupResult Prev(SemaRef, Name, NewFD->getLocation(), |
| 6145 | IsLocalFriend ? Sema::LookupLocalFriendName |
| 6146 | : Sema::LookupOrdinaryName, |
| 6147 | Sema::ForRedeclaration); |
Kaelyn Uhrain | 5161163 | 2011-08-18 18:19:12 +0000 | [diff] [blame] | 6148 | |
| 6149 | NewFD->setInvalidDecl(); |
Richard Smith | 4e9686b | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 6150 | if (IsLocalFriend) |
| 6151 | SemaRef.LookupName(Prev, S); |
| 6152 | else |
| 6153 | SemaRef.LookupQualifiedName(Prev, NewDC); |
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 6154 | assert(!Prev.isAmbiguous() && |
| 6155 | "Cannot have an ambiguity in previous-declaration lookup"); |
Kaelyn Uhrain | 3336353 | 2012-02-16 22:40:59 +0000 | [diff] [blame] | 6156 | CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(NewFD); |
Kaelyn Uhrain | ef094a1 | 2012-06-07 23:57:08 +0000 | [diff] [blame] | 6157 | DifferentNameValidatorCCC Validator(SemaRef.Context, NewFD, |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 6158 | MD ? MD->getParent() : nullptr); |
Kaelyn Uhrain | 5161163 | 2011-08-18 18:19:12 +0000 | [diff] [blame] | 6159 | if (!Prev.empty()) { |
| 6160 | for (LookupResult::iterator Func = Prev.begin(), FuncEnd = Prev.end(); |
| 6161 | Func != FuncEnd; ++Func) { |
| 6162 | FunctionDecl *FD = dyn_cast<FunctionDecl>(*Func); |
Kaelyn Uhrain | f09ce39 | 2011-10-11 00:28:52 +0000 | [diff] [blame] | 6163 | if (FD && |
| 6164 | hasSimilarParameters(SemaRef.Context, FD, NewFD, MismatchedParams)) { |
Kaelyn Uhrain | 5161163 | 2011-08-18 18:19:12 +0000 | [diff] [blame] | 6165 | // Add 1 to the index so that 0 can mean the mismatch didn't |
| 6166 | // involve a parameter |
| 6167 | unsigned ParamNum = |
| 6168 | MismatchedParams.empty() ? 0 : MismatchedParams.front() + 1; |
| 6169 | NearMatches.push_back(std::make_pair(FD, ParamNum)); |
| 6170 | } |
Kaelyn Uhrain | 4d9d157 | 2011-08-04 17:40:00 +0000 | [diff] [blame] | 6171 | } |
Kaelyn Uhrain | 5161163 | 2011-08-18 18:19:12 +0000 | [diff] [blame] | 6172 | // If the qualified name lookup yielded nothing, try typo correction |
Richard Smith | 4e9686b | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 6173 | } else if ((Correction = SemaRef.CorrectTypo( |
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 6174 | Prev.getLookupNameInfo(), Prev.getLookupKind(), S, |
| 6175 | &ExtraArgs.D.getCXXScopeSpec(), Validator, |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 6176 | Sema::CTK_ErrorRecovery, IsLocalFriend ? nullptr : NewDC))) { |
Kaelyn Uhrain | 2afd766 | 2011-10-11 00:28:39 +0000 | [diff] [blame] | 6177 | // Set up everything for the call to ActOnFunctionDeclarator |
| 6178 | ExtraArgs.D.SetIdentifier(Correction.getCorrectionAsIdentifierInfo(), |
| 6179 | ExtraArgs.D.getIdentifierLoc()); |
| 6180 | Previous.clear(); |
| 6181 | Previous.setLookupName(Correction.getCorrection()); |
Kaelyn Uhrain | 5161163 | 2011-08-18 18:19:12 +0000 | [diff] [blame] | 6182 | for (TypoCorrection::decl_iterator CDecl = Correction.begin(), |
| 6183 | CDeclEnd = Correction.end(); |
| 6184 | CDecl != CDeclEnd; ++CDecl) { |
| 6185 | FunctionDecl *FD = dyn_cast<FunctionDecl>(*CDecl); |
Kaelyn Uhrain | ef094a1 | 2012-06-07 23:57:08 +0000 | [diff] [blame] | 6186 | if (FD && !FD->hasBody() && |
| 6187 | hasSimilarParameters(SemaRef.Context, FD, NewFD, MismatchedParams)) { |
Kaelyn Uhrain | 2afd766 | 2011-10-11 00:28:39 +0000 | [diff] [blame] | 6188 | Previous.addDecl(FD); |
Kaelyn Uhrain | 5161163 | 2011-08-18 18:19:12 +0000 | [diff] [blame] | 6189 | } |
| 6190 | } |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 6191 | bool wasRedeclaration = ExtraArgs.D.isRedeclaration(); |
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 6192 | |
| 6193 | NamedDecl *Result; |
| 6194 | // Retry building the function declaration with the new previous |
| 6195 | // declarations, and with errors suppressed. |
| 6196 | { |
| 6197 | // Trap errors. |
| 6198 | Sema::SFINAETrap Trap(SemaRef); |
| 6199 | |
| 6200 | // TODO: Refactor ActOnFunctionDeclarator so that we can call only the |
| 6201 | // pieces need to verify the typo-corrected C++ declaration and hopefully |
| 6202 | // eliminate the need for the parameter pack ExtraArgs. |
| 6203 | Result = SemaRef.ActOnFunctionDeclarator( |
| 6204 | ExtraArgs.S, ExtraArgs.D, |
| 6205 | Correction.getCorrectionDecl()->getDeclContext(), |
| 6206 | NewFD->getTypeSourceInfo(), Previous, ExtraArgs.TemplateParamLists, |
| 6207 | ExtraArgs.AddToScope); |
| 6208 | |
| 6209 | if (Trap.hasErrorOccurred()) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 6210 | Result = nullptr; |
Kaelyn Uhrain | 2afd766 | 2011-10-11 00:28:39 +0000 | [diff] [blame] | 6211 | } |
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 6212 | |
| 6213 | if (Result) { |
| 6214 | // Determine which correction we picked. |
| 6215 | Decl *Canonical = Result->getCanonicalDecl(); |
| 6216 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 6217 | I != E; ++I) |
| 6218 | if ((*I)->getCanonicalDecl() == Canonical) |
| 6219 | Correction.setCorrectionDecl(*I); |
| 6220 | |
| 6221 | SemaRef.diagnoseTypo( |
| 6222 | Correction, |
| 6223 | SemaRef.PDiag(IsLocalFriend |
| 6224 | ? diag::err_no_matching_local_friend_suggest |
| 6225 | : diag::err_member_decl_does_not_match_suggest) |
| 6226 | << Name << NewDC << IsDefinition); |
| 6227 | return Result; |
Kaelyn Uhrain | 2afd766 | 2011-10-11 00:28:39 +0000 | [diff] [blame] | 6228 | } |
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 6229 | |
| 6230 | // Pretend the typo correction never occurred |
| 6231 | ExtraArgs.D.SetIdentifier(Name.getAsIdentifierInfo(), |
| 6232 | ExtraArgs.D.getIdentifierLoc()); |
| 6233 | ExtraArgs.D.setRedeclaration(wasRedeclaration); |
| 6234 | Previous.clear(); |
| 6235 | Previous.setLookupName(Name); |
Kaelyn Uhrain | 5161163 | 2011-08-18 18:19:12 +0000 | [diff] [blame] | 6236 | } |
| 6237 | |
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 6238 | SemaRef.Diag(NewFD->getLocation(), DiagMsg) |
| 6239 | << Name << NewDC << IsDefinition << NewFD->getLocation(); |
Kaelyn Uhrain | 5161163 | 2011-08-18 18:19:12 +0000 | [diff] [blame] | 6240 | |
Kaelyn Uhrain | 1055393 | 2011-10-10 18:01:37 +0000 | [diff] [blame] | 6241 | bool NewFDisConst = false; |
| 6242 | if (CXXMethodDecl *NewMD = dyn_cast<CXXMethodDecl>(NewFD)) |
David Blaikie | 4ef832f | 2012-08-10 00:55:35 +0000 | [diff] [blame] | 6243 | NewFDisConst = NewMD->isConst(); |
Kaelyn Uhrain | 1055393 | 2011-10-10 18:01:37 +0000 | [diff] [blame] | 6244 | |
Craig Topper | 8bc99dd | 2013-07-04 03:15:42 +0000 | [diff] [blame] | 6245 | for (SmallVectorImpl<std::pair<FunctionDecl *, unsigned> >::iterator |
Kaelyn Uhrain | 5161163 | 2011-08-18 18:19:12 +0000 | [diff] [blame] | 6246 | NearMatch = NearMatches.begin(), NearMatchEnd = NearMatches.end(); |
| 6247 | NearMatch != NearMatchEnd; ++NearMatch) { |
| 6248 | FunctionDecl *FD = NearMatch->first; |
Richard Smith | 4e9686b | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 6249 | CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD); |
| 6250 | bool FDisConst = MD && MD->isConst(); |
| 6251 | bool IsMember = MD || !IsLocalFriend; |
Kaelyn Uhrain | 5161163 | 2011-08-18 18:19:12 +0000 | [diff] [blame] | 6252 | |
Richard Smith | a41c97a | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 6253 | // FIXME: These notes are poorly worded for the local friend case. |
Kaelyn Uhrain | 5161163 | 2011-08-18 18:19:12 +0000 | [diff] [blame] | 6254 | if (unsigned Idx = NearMatch->second) { |
| 6255 | ParmVarDecl *FDParam = FD->getParamDecl(Idx-1); |
Richard Smith | 1c931be | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 6256 | SourceLocation Loc = FDParam->getTypeSpecStartLoc(); |
| 6257 | if (Loc.isInvalid()) Loc = FD->getLocation(); |
Richard Smith | 4e9686b | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 6258 | SemaRef.Diag(Loc, IsMember ? diag::note_member_def_close_param_match |
| 6259 | : diag::note_local_decl_close_param_match) |
| 6260 | << Idx << FDParam->getType() |
| 6261 | << NewFD->getParamDecl(Idx - 1)->getType(); |
Kaelyn Uhrain | 1055393 | 2011-10-10 18:01:37 +0000 | [diff] [blame] | 6262 | } else if (FDisConst != NewFDisConst) { |
Kaelyn Uhrain | f09ce39 | 2011-10-11 00:28:52 +0000 | [diff] [blame] | 6263 | SemaRef.Diag(FD->getLocation(), diag::note_member_def_close_const_match) |
Kaelyn Uhrain | 1055393 | 2011-10-10 18:01:37 +0000 | [diff] [blame] | 6264 | << NewFDisConst << FD->getSourceRange().getEnd(); |
Kaelyn Uhrain | 5161163 | 2011-08-18 18:19:12 +0000 | [diff] [blame] | 6265 | } else |
Richard Smith | 4e9686b | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 6266 | SemaRef.Diag(FD->getLocation(), |
| 6267 | IsMember ? diag::note_member_def_close_match |
| 6268 | : diag::note_local_decl_close_match); |
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 6269 | } |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 6270 | return nullptr; |
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 6271 | } |
| 6272 | |
David Blaikie | d662a79 | 2011-10-19 22:56:21 +0000 | [diff] [blame] | 6273 | static FunctionDecl::StorageClass getFunctionStorageClass(Sema &SemaRef, |
| 6274 | Declarator &D) { |
Kaelyn Uhrain | d7e19ce | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6275 | switch (D.getDeclSpec().getStorageClassSpec()) { |
| 6276 | default: llvm_unreachable("Unknown storage class!"); |
| 6277 | case DeclSpec::SCS_auto: |
| 6278 | case DeclSpec::SCS_register: |
| 6279 | case DeclSpec::SCS_mutable: |
| 6280 | SemaRef.Diag(D.getDeclSpec().getStorageClassSpecLoc(), |
| 6281 | diag::err_typecheck_sclass_func); |
| 6282 | D.setInvalidType(); |
| 6283 | break; |
| 6284 | case DeclSpec::SCS_unspecified: break; |
Rafael Espindola | 65dfa2b | 2013-04-25 12:11:36 +0000 | [diff] [blame] | 6285 | case DeclSpec::SCS_extern: |
| 6286 | if (D.getDeclSpec().isExternInLinkageSpec()) |
| 6287 | return SC_None; |
| 6288 | return SC_Extern; |
Kaelyn Uhrain | d7e19ce | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6289 | case DeclSpec::SCS_static: { |
| 6290 | if (SemaRef.CurContext->getRedeclContext()->isFunctionOrMethod()) { |
| 6291 | // C99 6.7.1p5: |
| 6292 | // The declaration of an identifier for a function that has |
| 6293 | // block scope shall have no explicit storage-class specifier |
| 6294 | // other than extern |
| 6295 | // See also (C++ [dcl.stc]p4). |
| 6296 | SemaRef.Diag(D.getDeclSpec().getStorageClassSpecLoc(), |
| 6297 | diag::err_static_block_func); |
| 6298 | break; |
| 6299 | } else |
| 6300 | return SC_Static; |
| 6301 | } |
| 6302 | case DeclSpec::SCS_private_extern: return SC_PrivateExtern; |
| 6303 | } |
| 6304 | |
| 6305 | // No explicit storage class has already been returned |
| 6306 | return SC_None; |
| 6307 | } |
| 6308 | |
| 6309 | static FunctionDecl* CreateNewFunctionDecl(Sema &SemaRef, Declarator &D, |
| 6310 | DeclContext *DC, QualType &R, |
| 6311 | TypeSourceInfo *TInfo, |
| 6312 | FunctionDecl::StorageClass SC, |
| 6313 | bool &IsVirtualOkay) { |
| 6314 | DeclarationNameInfo NameInfo = SemaRef.GetNameForDeclarator(D); |
| 6315 | DeclarationName Name = NameInfo.getName(); |
| 6316 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 6317 | FunctionDecl *NewFD = nullptr; |
Kaelyn Uhrain | d7e19ce | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6318 | bool isInline = D.getDeclSpec().isInlineSpecified(); |
Kaelyn Uhrain | d7e19ce | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6319 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6320 | if (!SemaRef.getLangOpts().CPlusPlus) { |
Kaelyn Uhrain | d7e19ce | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6321 | // Determine whether the function was written with a |
| 6322 | // prototype. This true when: |
| 6323 | // - there is a prototype in the declarator, or |
| 6324 | // - the type R of the function is some kind of typedef or other reference |
| 6325 | // to a type name (which eventually refers to a function type). |
| 6326 | bool HasPrototype = |
| 6327 | (D.isFunctionDeclarator() && D.getFunctionTypeInfo().hasPrototype) || |
| 6328 | (!isa<FunctionType>(R.getTypePtr()) && R->isFunctionProtoType()); |
| 6329 | |
David Blaikie | d662a79 | 2011-10-19 22:56:21 +0000 | [diff] [blame] | 6330 | NewFD = FunctionDecl::Create(SemaRef.Context, DC, |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 6331 | D.getLocStart(), NameInfo, R, |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 6332 | TInfo, SC, isInline, |
| 6333 | HasPrototype, false); |
Kaelyn Uhrain | d7e19ce | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6334 | if (D.isInvalidType()) |
| 6335 | NewFD->setInvalidDecl(); |
| 6336 | |
| 6337 | // Set the lexical context. |
| 6338 | NewFD->setLexicalDeclContext(SemaRef.CurContext); |
| 6339 | |
| 6340 | return NewFD; |
| 6341 | } |
| 6342 | |
| 6343 | bool isExplicit = D.getDeclSpec().isExplicitSpecified(); |
| 6344 | bool isConstexpr = D.getDeclSpec().isConstexprSpecified(); |
| 6345 | |
| 6346 | // Check that the return type is not an abstract class type. |
| 6347 | // For record types, this is done by the AbstractClassUsageDiagnoser once |
| 6348 | // the class has been completely parsed. |
| 6349 | if (!DC->isRecord() && |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 6350 | SemaRef.RequireNonAbstractType( |
| 6351 | D.getIdentifierLoc(), R->getAs<FunctionType>()->getReturnType(), |
| 6352 | diag::err_abstract_type_in_decl, SemaRef.AbstractReturnType)) |
Kaelyn Uhrain | d7e19ce | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6353 | D.setInvalidType(); |
| 6354 | |
| 6355 | if (Name.getNameKind() == DeclarationName::CXXConstructorName) { |
| 6356 | // This is a C++ constructor declaration. |
| 6357 | assert(DC->isRecord() && |
| 6358 | "Constructors can only be declared in a member context"); |
| 6359 | |
| 6360 | R = SemaRef.CheckConstructorDeclarator(D, R, SC); |
| 6361 | return CXXConstructorDecl::Create(SemaRef.Context, cast<CXXRecordDecl>(DC), |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 6362 | D.getLocStart(), NameInfo, |
Kaelyn Uhrain | d7e19ce | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6363 | R, TInfo, isExplicit, isInline, |
| 6364 | /*isImplicitlyDeclared=*/false, |
| 6365 | isConstexpr); |
| 6366 | |
| 6367 | } else if (Name.getNameKind() == DeclarationName::CXXDestructorName) { |
| 6368 | // This is a C++ destructor declaration. |
| 6369 | if (DC->isRecord()) { |
| 6370 | R = SemaRef.CheckDestructorDeclarator(D, R, SC); |
| 6371 | CXXRecordDecl *Record = cast<CXXRecordDecl>(DC); |
| 6372 | CXXDestructorDecl *NewDD = CXXDestructorDecl::Create( |
| 6373 | SemaRef.Context, Record, |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 6374 | D.getLocStart(), |
Kaelyn Uhrain | d7e19ce | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6375 | NameInfo, R, TInfo, isInline, |
| 6376 | /*isImplicitlyDeclared=*/false); |
| 6377 | |
| 6378 | // If the class is complete, then we now create the implicit exception |
| 6379 | // specification. If the class is incomplete or dependent, we can't do |
| 6380 | // it yet. |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6381 | if (SemaRef.getLangOpts().CPlusPlus11 && !Record->isDependentType() && |
Kaelyn Uhrain | d7e19ce | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6382 | Record->getDefinition() && !Record->isBeingDefined() && |
| 6383 | R->getAs<FunctionProtoType>()->getExceptionSpecType() == EST_None) { |
| 6384 | SemaRef.AdjustDestructorExceptionSpec(Record, NewDD); |
| 6385 | } |
| 6386 | |
| 6387 | IsVirtualOkay = true; |
| 6388 | return NewDD; |
| 6389 | |
| 6390 | } else { |
| 6391 | SemaRef.Diag(D.getIdentifierLoc(), diag::err_destructor_not_member); |
| 6392 | D.setInvalidType(); |
| 6393 | |
| 6394 | // Create a FunctionDecl to satisfy the function definition parsing |
| 6395 | // code path. |
| 6396 | return FunctionDecl::Create(SemaRef.Context, DC, |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 6397 | D.getLocStart(), |
Kaelyn Uhrain | d7e19ce | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6398 | D.getIdentifierLoc(), Name, R, TInfo, |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 6399 | SC, isInline, |
Kaelyn Uhrain | d7e19ce | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6400 | /*hasPrototype=*/true, isConstexpr); |
| 6401 | } |
| 6402 | |
| 6403 | } else if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName) { |
| 6404 | if (!DC->isRecord()) { |
| 6405 | SemaRef.Diag(D.getIdentifierLoc(), |
| 6406 | diag::err_conv_function_not_member); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 6407 | return nullptr; |
Kaelyn Uhrain | d7e19ce | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6408 | } |
| 6409 | |
| 6410 | SemaRef.CheckConversionDeclarator(D, R, SC); |
| 6411 | IsVirtualOkay = true; |
| 6412 | return CXXConversionDecl::Create(SemaRef.Context, cast<CXXRecordDecl>(DC), |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 6413 | D.getLocStart(), NameInfo, |
Kaelyn Uhrain | d7e19ce | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6414 | R, TInfo, isInline, isExplicit, |
| 6415 | isConstexpr, SourceLocation()); |
| 6416 | |
| 6417 | } else if (DC->isRecord()) { |
| 6418 | // If the name of the function is the same as the name of the record, |
| 6419 | // then this must be an invalid constructor that has a return type. |
| 6420 | // (The parser checks for a return type and makes the declarator a |
| 6421 | // constructor if it has no return type). |
| 6422 | if (Name.getAsIdentifierInfo() && |
| 6423 | Name.getAsIdentifierInfo() == cast<CXXRecordDecl>(DC)->getIdentifier()){ |
| 6424 | SemaRef.Diag(D.getIdentifierLoc(), diag::err_constructor_return_type) |
| 6425 | << SourceRange(D.getDeclSpec().getTypeSpecTypeLoc()) |
| 6426 | << SourceRange(D.getIdentifierLoc()); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 6427 | return nullptr; |
Kaelyn Uhrain | d7e19ce | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6428 | } |
| 6429 | |
Kaelyn Uhrain | d7e19ce | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6430 | // This is a C++ method declaration. |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 6431 | CXXMethodDecl *Ret = CXXMethodDecl::Create(SemaRef.Context, |
| 6432 | cast<CXXRecordDecl>(DC), |
| 6433 | D.getLocStart(), NameInfo, R, |
| 6434 | TInfo, SC, isInline, |
| 6435 | isConstexpr, SourceLocation()); |
| 6436 | IsVirtualOkay = !Ret->isStatic(); |
| 6437 | return Ret; |
Kaelyn Uhrain | d7e19ce | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6438 | } else { |
| 6439 | // Determine whether the function was written with a |
| 6440 | // prototype. This true when: |
| 6441 | // - we're in C++ (where every function has a prototype), |
| 6442 | return FunctionDecl::Create(SemaRef.Context, DC, |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 6443 | D.getLocStart(), |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 6444 | NameInfo, R, TInfo, SC, isInline, |
Kaelyn Uhrain | d7e19ce | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6445 | true/*HasPrototype*/, isConstexpr); |
| 6446 | } |
| 6447 | } |
| 6448 | |
Matt Arsenault | e6c8afc | 2013-07-23 01:23:36 +0000 | [diff] [blame] | 6449 | enum OpenCLParamType { |
| 6450 | ValidKernelParam, |
| 6451 | PtrPtrKernelParam, |
| 6452 | PtrKernelParam, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 6453 | PrivatePtrKernelParam, |
Matt Arsenault | e6c8afc | 2013-07-23 01:23:36 +0000 | [diff] [blame] | 6454 | InvalidKernelParam, |
| 6455 | RecordKernelParam |
| 6456 | }; |
| 6457 | |
| 6458 | static OpenCLParamType getOpenCLKernelParameterType(QualType PT) { |
| 6459 | if (PT->isPointerType()) { |
| 6460 | QualType PointeeType = PT->getPointeeType(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 6461 | if (PointeeType->isPointerType()) |
| 6462 | return PtrPtrKernelParam; |
| 6463 | return PointeeType.getAddressSpace() == 0 ? PrivatePtrKernelParam |
| 6464 | : PtrKernelParam; |
Matt Arsenault | e6c8afc | 2013-07-23 01:23:36 +0000 | [diff] [blame] | 6465 | } |
| 6466 | |
| 6467 | // TODO: Forbid the other integer types (size_t, ptrdiff_t...) when they can |
| 6468 | // be used as builtin types. |
| 6469 | |
| 6470 | if (PT->isImageType()) |
| 6471 | return PtrKernelParam; |
| 6472 | |
| 6473 | if (PT->isBooleanType()) |
| 6474 | return InvalidKernelParam; |
| 6475 | |
| 6476 | if (PT->isEventT()) |
| 6477 | return InvalidKernelParam; |
| 6478 | |
| 6479 | if (PT->isHalfType()) |
| 6480 | return InvalidKernelParam; |
| 6481 | |
| 6482 | if (PT->isRecordType()) |
| 6483 | return RecordKernelParam; |
| 6484 | |
| 6485 | return ValidKernelParam; |
| 6486 | } |
| 6487 | |
| 6488 | static void checkIsValidOpenCLKernelParameter( |
| 6489 | Sema &S, |
| 6490 | Declarator &D, |
| 6491 | ParmVarDecl *Param, |
| 6492 | llvm::SmallPtrSet<const Type *, 16> &ValidTypes) { |
| 6493 | QualType PT = Param->getType(); |
| 6494 | |
| 6495 | // Cache the valid types we encounter to avoid rechecking structs that are |
| 6496 | // used again |
| 6497 | if (ValidTypes.count(PT.getTypePtr())) |
| 6498 | return; |
| 6499 | |
| 6500 | switch (getOpenCLKernelParameterType(PT)) { |
| 6501 | case PtrPtrKernelParam: |
| 6502 | // OpenCL v1.2 s6.9.a: |
| 6503 | // A kernel function argument cannot be declared as a |
| 6504 | // pointer to a pointer type. |
| 6505 | S.Diag(Param->getLocation(), diag::err_opencl_ptrptr_kernel_param); |
| 6506 | D.setInvalidType(); |
| 6507 | return; |
| 6508 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 6509 | case PrivatePtrKernelParam: |
| 6510 | // OpenCL v1.2 s6.9.a: |
| 6511 | // A kernel function argument cannot be declared as a |
| 6512 | // pointer to the private address space. |
| 6513 | S.Diag(Param->getLocation(), diag::err_opencl_private_ptr_kernel_param); |
| 6514 | D.setInvalidType(); |
| 6515 | return; |
| 6516 | |
Matt Arsenault | e6c8afc | 2013-07-23 01:23:36 +0000 | [diff] [blame] | 6517 | // OpenCL v1.2 s6.9.k: |
| 6518 | // Arguments to kernel functions in a program cannot be declared with the |
| 6519 | // built-in scalar types bool, half, size_t, ptrdiff_t, intptr_t, and |
| 6520 | // uintptr_t or a struct and/or union that contain fields declared to be |
| 6521 | // one of these built-in scalar types. |
| 6522 | |
| 6523 | case InvalidKernelParam: |
| 6524 | // OpenCL v1.2 s6.8 n: |
| 6525 | // A kernel function argument cannot be declared |
| 6526 | // of event_t type. |
| 6527 | S.Diag(Param->getLocation(), diag::err_bad_kernel_param_type) << PT; |
| 6528 | D.setInvalidType(); |
| 6529 | return; |
| 6530 | |
| 6531 | case PtrKernelParam: |
| 6532 | case ValidKernelParam: |
| 6533 | ValidTypes.insert(PT.getTypePtr()); |
| 6534 | return; |
| 6535 | |
| 6536 | case RecordKernelParam: |
| 6537 | break; |
| 6538 | } |
| 6539 | |
| 6540 | // Track nested structs we will inspect |
| 6541 | SmallVector<const Decl *, 4> VisitStack; |
| 6542 | |
| 6543 | // Track where we are in the nested structs. Items will migrate from |
| 6544 | // VisitStack to HistoryStack as we do the DFS for bad field. |
| 6545 | SmallVector<const FieldDecl *, 4> HistoryStack; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 6546 | HistoryStack.push_back(nullptr); |
Matt Arsenault | e6c8afc | 2013-07-23 01:23:36 +0000 | [diff] [blame] | 6547 | |
| 6548 | const RecordDecl *PD = PT->castAs<RecordType>()->getDecl(); |
| 6549 | VisitStack.push_back(PD); |
| 6550 | |
| 6551 | assert(VisitStack.back() && "First decl null?"); |
| 6552 | |
| 6553 | do { |
| 6554 | const Decl *Next = VisitStack.pop_back_val(); |
| 6555 | if (!Next) { |
| 6556 | assert(!HistoryStack.empty()); |
| 6557 | // Found a marker, we have gone up a level |
| 6558 | if (const FieldDecl *Hist = HistoryStack.pop_back_val()) |
| 6559 | ValidTypes.insert(Hist->getType().getTypePtr()); |
| 6560 | |
| 6561 | continue; |
| 6562 | } |
| 6563 | |
| 6564 | // Adds everything except the original parameter declaration (which is not a |
| 6565 | // field itself) to the history stack. |
| 6566 | const RecordDecl *RD; |
| 6567 | if (const FieldDecl *Field = dyn_cast<FieldDecl>(Next)) { |
| 6568 | HistoryStack.push_back(Field); |
| 6569 | RD = Field->getType()->castAs<RecordType>()->getDecl(); |
| 6570 | } else { |
| 6571 | RD = cast<RecordDecl>(Next); |
| 6572 | } |
| 6573 | |
| 6574 | // Add a null marker so we know when we've gone back up a level |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 6575 | VisitStack.push_back(nullptr); |
Matt Arsenault | e6c8afc | 2013-07-23 01:23:36 +0000 | [diff] [blame] | 6576 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 6577 | for (const auto *FD : RD->fields()) { |
Matt Arsenault | e6c8afc | 2013-07-23 01:23:36 +0000 | [diff] [blame] | 6578 | QualType QT = FD->getType(); |
| 6579 | |
| 6580 | if (ValidTypes.count(QT.getTypePtr())) |
| 6581 | continue; |
| 6582 | |
| 6583 | OpenCLParamType ParamType = getOpenCLKernelParameterType(QT); |
| 6584 | if (ParamType == ValidKernelParam) |
| 6585 | continue; |
| 6586 | |
| 6587 | if (ParamType == RecordKernelParam) { |
| 6588 | VisitStack.push_back(FD); |
| 6589 | continue; |
| 6590 | } |
| 6591 | |
| 6592 | // OpenCL v1.2 s6.9.p: |
| 6593 | // Arguments to kernel functions that are declared to be a struct or union |
| 6594 | // do not allow OpenCL objects to be passed as elements of the struct or |
| 6595 | // union. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 6596 | if (ParamType == PtrKernelParam || ParamType == PtrPtrKernelParam || |
| 6597 | ParamType == PrivatePtrKernelParam) { |
Matt Arsenault | e6c8afc | 2013-07-23 01:23:36 +0000 | [diff] [blame] | 6598 | S.Diag(Param->getLocation(), |
| 6599 | diag::err_record_with_pointers_kernel_param) |
| 6600 | << PT->isUnionType() |
| 6601 | << PT; |
| 6602 | } else { |
| 6603 | S.Diag(Param->getLocation(), diag::err_bad_kernel_param_type) << PT; |
| 6604 | } |
| 6605 | |
| 6606 | S.Diag(PD->getLocation(), diag::note_within_field_of_type) |
| 6607 | << PD->getDeclName(); |
| 6608 | |
| 6609 | // We have an error, now let's go back up through history and show where |
| 6610 | // the offending field came from |
| 6611 | for (ArrayRef<const FieldDecl *>::const_iterator I = HistoryStack.begin() + 1, |
| 6612 | E = HistoryStack.end(); I != E; ++I) { |
| 6613 | const FieldDecl *OuterField = *I; |
| 6614 | S.Diag(OuterField->getLocation(), diag::note_within_field_of_type) |
| 6615 | << OuterField->getType(); |
| 6616 | } |
| 6617 | |
| 6618 | S.Diag(FD->getLocation(), diag::note_illegal_field_declared_here) |
| 6619 | << QT->isPointerType() |
| 6620 | << QT; |
| 6621 | D.setInvalidType(); |
| 6622 | return; |
| 6623 | } |
| 6624 | } while (!VisitStack.empty()); |
| 6625 | } |
| 6626 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6627 | NamedDecl* |
Nick Lewycky | 25af091 | 2011-07-02 02:05:12 +0000 | [diff] [blame] | 6628 | Sema::ActOnFunctionDeclarator(Scope *S, Declarator &D, DeclContext *DC, |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 6629 | TypeSourceInfo *TInfo, LookupResult &Previous, |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 6630 | MultiTemplateParamsArg TemplateParamLists, |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 6631 | bool &AddToScope) { |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 6632 | QualType R = TInfo->getType(); |
| 6633 | |
Zhongxing Xu | 416fcaf | 2009-01-16 01:13:29 +0000 | [diff] [blame] | 6634 | assert(R.getTypePtr()->isFunctionType()); |
| 6635 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 6636 | // TODO: consider using NameInfo for diagnostic. |
| 6637 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); |
| 6638 | DeclarationName Name = NameInfo.getName(); |
Kaelyn Uhrain | d7e19ce | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6639 | FunctionDecl::StorageClass SC = getFunctionStorageClass(*this, D); |
Zhongxing Xu | 416fcaf | 2009-01-16 01:13:29 +0000 | [diff] [blame] | 6640 | |
Richard Smith | ec64244 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 6641 | if (DeclSpec::TSCS TSCS = D.getDeclSpec().getThreadStorageClassSpec()) |
| 6642 | Diag(D.getDeclSpec().getThreadStorageClassSpecLoc(), |
| 6643 | diag::err_invalid_thread) |
| 6644 | << DeclSpec::getSpecifierName(TSCS); |
Eli Friedman | 63054b3 | 2009-04-19 20:27:55 +0000 | [diff] [blame] | 6645 | |
Reid Kleckner | d1a32c3 | 2013-10-08 00:58:57 +0000 | [diff] [blame] | 6646 | if (D.isFirstDeclarationOfMember()) |
| 6647 | adjustMemberFunctionCC(R, D.isStaticMember()); |
Reid Kleckner | ef07203 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 6648 | |
Douglas Gregor | 3922ed0 | 2010-12-10 19:28:19 +0000 | [diff] [blame] | 6649 | bool isFriend = false; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 6650 | FunctionTemplateDecl *FunctionTemplate = nullptr; |
Douglas Gregor | 3922ed0 | 2010-12-10 19:28:19 +0000 | [diff] [blame] | 6651 | bool isExplicitSpecialization = false; |
| 6652 | bool isFunctionTemplateSpecialization = false; |
Nico Weber | 6b02009 | 2012-06-25 17:21:05 +0000 | [diff] [blame] | 6653 | |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 6654 | bool isDependentClassScopeExplicitSpecialization = false; |
Nico Weber | 6b02009 | 2012-06-25 17:21:05 +0000 | [diff] [blame] | 6655 | bool HasExplicitTemplateArgs = false; |
| 6656 | TemplateArgumentListInfo TemplateArgs; |
| 6657 | |
Kaelyn Uhrain | d7e19ce | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6658 | bool isVirtualOkay = false; |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6659 | |
Richard Smith | a41c97a | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 6660 | DeclContext *OriginalDC = DC; |
| 6661 | bool IsLocalExternDecl = adjustContextForLocalExternDecl(DC); |
| 6662 | |
Kaelyn Uhrain | d7e19ce | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6663 | FunctionDecl *NewFD = CreateNewFunctionDecl(*this, D, DC, R, TInfo, SC, |
| 6664 | isVirtualOkay); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 6665 | if (!NewFD) return nullptr; |
Kaelyn Uhrain | d7e19ce | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6666 | |
Argyrios Kyrtzidis | c14a03d | 2011-11-23 20:27:36 +0000 | [diff] [blame] | 6667 | if (OriginalLexicalContext && OriginalLexicalContext->isObjCContainer()) |
| 6668 | NewFD->setTopLevelDeclInObjCContainer(); |
| 6669 | |
Richard Smith | a41c97a | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 6670 | // Set the lexical context. If this is a function-scope declaration, or has a |
| 6671 | // C++ scope specifier, or is the object of a friend declaration, the lexical |
| 6672 | // context will be different from the semantic context. |
| 6673 | NewFD->setLexicalDeclContext(CurContext); |
| 6674 | |
| 6675 | if (IsLocalExternDecl) |
| 6676 | NewFD->setLocalExternDecl(); |
| 6677 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6678 | if (getLangOpts().CPlusPlus) { |
Kaelyn Uhrain | d7e19ce | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6679 | bool isInline = D.getDeclSpec().isInlineSpecified(); |
Douglas Gregor | 3922ed0 | 2010-12-10 19:28:19 +0000 | [diff] [blame] | 6680 | bool isVirtual = D.getDeclSpec().isVirtualSpecified(); |
| 6681 | bool isExplicit = D.getDeclSpec().isExplicitSpecified(); |
Richard Smith | af1fc7a | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 6682 | bool isConstexpr = D.getDeclSpec().isConstexprSpecified(); |
Kaelyn Uhrain | d7e19ce | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6683 | isFriend = D.getDeclSpec().isFriendSpecified(); |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 6684 | if (isFriend && !isInline && D.isFunctionDefinition()) { |
Abramo Bagnara | b0a2fcc | 2011-03-18 15:21:59 +0000 | [diff] [blame] | 6685 | // C++ [class.friend]p5 |
| 6686 | // A function can be defined in a friend declaration of a |
| 6687 | // class . . . . Such a function is implicitly inline. |
| 6688 | NewFD->setImplicitlyInline(); |
| 6689 | } |
| 6690 | |
John McCall | e402e72 | 2012-09-25 07:32:39 +0000 | [diff] [blame] | 6691 | // If this is a method defined in an __interface, and is not a constructor |
| 6692 | // or an overloaded operator, then set the pure flag (isVirtual will already |
| 6693 | // return true). |
| 6694 | if (const CXXRecordDecl *Parent = |
| 6695 | dyn_cast<CXXRecordDecl>(NewFD->getDeclContext())) { |
| 6696 | if (Parent->isInterface() && cast<CXXMethodDecl>(NewFD)->isUserProvided()) |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 6697 | NewFD->setPure(true); |
| 6698 | } |
| 6699 | |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 6700 | SetNestedNameSpecifier(NewFD, D); |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 6701 | isExplicitSpecialization = false; |
| 6702 | isFunctionTemplateSpecialization = false; |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 6703 | if (D.isInvalidType()) |
| 6704 | NewFD->setInvalidDecl(); |
Richard Smith | a41c97a | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 6705 | |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 6706 | // Match up the template parameter lists with the scope specifier, then |
| 6707 | // determine whether we have a template or a template specialization. |
| 6708 | bool Invalid = false; |
Robert Wilhelm | 1169e2f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 6709 | if (TemplateParameterList *TemplateParams = |
| 6710 | MatchTemplateParametersToScopeSpecifier( |
| 6711 | D.getDeclSpec().getLocStart(), D.getIdentifierLoc(), |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 6712 | D.getCXXScopeSpec(), |
| 6713 | D.getName().getKind() == UnqualifiedId::IK_TemplateId |
| 6714 | ? D.getName().TemplateId |
| 6715 | : nullptr, |
| 6716 | TemplateParamLists, isFriend, isExplicitSpecialization, |
| 6717 | Invalid)) { |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6718 | if (TemplateParams->size() > 0) { |
| 6719 | // This is a function template |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 6720 | |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6721 | // Check that we can declare a template here. |
| 6722 | if (CheckTemplateDeclScope(S, TemplateParams)) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 6723 | return nullptr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6724 | |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6725 | // A destructor cannot be a template. |
| 6726 | if (Name.getNameKind() == DeclarationName::CXXDestructorName) { |
| 6727 | Diag(NewFD->getLocation(), diag::err_destructor_template); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 6728 | return nullptr; |
John McCall | 5fd378b | 2010-03-24 08:27:58 +0000 | [diff] [blame] | 6729 | } |
Douglas Gregor | 2060650 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 6730 | |
| 6731 | // If we're adding a template to a dependent context, we may need to |
David Blaikie | d662a79 | 2011-10-19 22:56:21 +0000 | [diff] [blame] | 6732 | // rebuilding some of the types used within the template parameter list, |
Douglas Gregor | 2060650 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 6733 | // now that we know what the current instantiation is. |
| 6734 | if (DC->isDependentContext()) { |
| 6735 | ContextRAII SavedContext(*this, DC); |
| 6736 | if (RebuildTemplateParamsInCurrentInstantiation(TemplateParams)) |
| 6737 | Invalid = true; |
| 6738 | } |
| 6739 | |
John McCall | 5fd378b | 2010-03-24 08:27:58 +0000 | [diff] [blame] | 6740 | |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6741 | FunctionTemplate = FunctionTemplateDecl::Create(Context, DC, |
| 6742 | NewFD->getLocation(), |
| 6743 | Name, TemplateParams, |
| 6744 | NewFD); |
| 6745 | FunctionTemplate->setLexicalDeclContext(CurContext); |
| 6746 | NewFD->setDescribedFunctionTemplate(FunctionTemplate); |
| 6747 | |
| 6748 | // For source fidelity, store the other template param lists. |
| 6749 | if (TemplateParamLists.size() > 1) { |
| 6750 | NewFD->setTemplateParameterListsInfo(Context, |
| 6751 | TemplateParamLists.size() - 1, |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 6752 | TemplateParamLists.data()); |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6753 | } |
| 6754 | } else { |
| 6755 | // This is a function template specialization. |
| 6756 | isFunctionTemplateSpecialization = true; |
| 6757 | // For source fidelity, store all the template param lists. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 6758 | if (TemplateParamLists.size() > 0) |
| 6759 | NewFD->setTemplateParameterListsInfo(Context, |
| 6760 | TemplateParamLists.size(), |
| 6761 | TemplateParamLists.data()); |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6762 | |
| 6763 | // C++0x [temp.expl.spec]p20 forbids "template<> friend void foo(int);". |
| 6764 | if (isFriend) { |
| 6765 | // We want to remove the "template<>", found here. |
| 6766 | SourceRange RemoveRange = TemplateParams->getSourceRange(); |
| 6767 | |
| 6768 | // If we remove the template<> and the name is not a |
| 6769 | // template-id, we're actually silently creating a problem: |
| 6770 | // the friend declaration will refer to an untemplated decl, |
| 6771 | // and clearly the user wants a template specialization. So |
| 6772 | // we need to insert '<>' after the name. |
| 6773 | SourceLocation InsertLoc; |
| 6774 | if (D.getName().getKind() != UnqualifiedId::IK_TemplateId) { |
| 6775 | InsertLoc = D.getName().getSourceRange().getEnd(); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 6776 | InsertLoc = getLocForEndOfToken(InsertLoc); |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6777 | } |
| 6778 | |
| 6779 | Diag(D.getIdentifierLoc(), diag::err_template_spec_decl_friend) |
| 6780 | << Name << RemoveRange |
| 6781 | << FixItHint::CreateRemoval(RemoveRange) |
| 6782 | << FixItHint::CreateInsertion(InsertLoc, "<>"); |
| 6783 | } |
| 6784 | } |
| 6785 | } |
| 6786 | else { |
| 6787 | // All template param lists were matched against the scope specifier: |
| 6788 | // this is NOT (an explicit specialization of) a template. |
| 6789 | if (TemplateParamLists.size() > 0) |
| 6790 | // For source fidelity, store all the template param lists. |
| 6791 | NewFD->setTemplateParameterListsInfo(Context, |
| 6792 | TemplateParamLists.size(), |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 6793 | TemplateParamLists.data()); |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 6794 | } |
| 6795 | |
| 6796 | if (Invalid) { |
| 6797 | NewFD->setInvalidDecl(); |
| 6798 | if (FunctionTemplate) |
| 6799 | FunctionTemplate->setInvalidDecl(); |
| 6800 | } |
Kaelyn Uhrain | d7e19ce | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6801 | |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 6802 | // C++ [dcl.fct.spec]p5: |
| 6803 | // The virtual specifier shall only be used in declarations of |
| 6804 | // nonstatic class member functions that appear within a |
| 6805 | // member-specification of a class declaration; see 10.3. |
| 6806 | // |
| 6807 | if (isVirtual && !NewFD->isInvalidDecl()) { |
| 6808 | if (!isVirtualOkay) { |
| 6809 | Diag(D.getDeclSpec().getVirtualSpecLoc(), |
| 6810 | diag::err_virtual_non_function); |
| 6811 | } else if (!CurContext->isRecord()) { |
| 6812 | // 'virtual' was specified outside of the class. |
Anders Carlsson | f1602a5 | 2011-01-22 14:43:56 +0000 | [diff] [blame] | 6813 | Diag(D.getDeclSpec().getVirtualSpecLoc(), |
| 6814 | diag::err_virtual_out_of_class) |
| 6815 | << FixItHint::CreateRemoval(D.getDeclSpec().getVirtualSpecLoc()); |
| 6816 | } else if (NewFD->getDescribedFunctionTemplate()) { |
| 6817 | // C++ [temp.mem]p3: |
| 6818 | // A member function template shall not be virtual. |
| 6819 | Diag(D.getDeclSpec().getVirtualSpecLoc(), |
| 6820 | diag::err_virtual_member_function_template) |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 6821 | << FixItHint::CreateRemoval(D.getDeclSpec().getVirtualSpecLoc()); |
| 6822 | } else { |
| 6823 | // Okay: Add virtual to the method. |
| 6824 | NewFD->setVirtualAsWritten(true); |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 6825 | } |
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 6826 | |
| 6827 | if (getLangOpts().CPlusPlus1y && |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 6828 | NewFD->getReturnType()->isUndeducedType()) |
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 6829 | Diag(D.getDeclSpec().getVirtualSpecLoc(), diag::err_auto_fn_virtual); |
Douglas Gregor | c5c903a | 2009-06-24 00:23:40 +0000 | [diff] [blame] | 6830 | } |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 6831 | |
Richard Smith | f93ec76 | 2013-11-15 02:58:23 +0000 | [diff] [blame] | 6832 | if (getLangOpts().CPlusPlus1y && |
| 6833 | (NewFD->isDependentContext() || |
| 6834 | (isFriend && CurContext->isDependentContext())) && |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 6835 | NewFD->getReturnType()->isUndeducedType()) { |
Richard Smith | 37e849a | 2013-08-14 20:16:31 +0000 | [diff] [blame] | 6836 | // If the function template is referenced directly (for instance, as a |
| 6837 | // member of the current instantiation), pretend it has a dependent type. |
| 6838 | // This is not really justified by the standard, but is the only sane |
| 6839 | // thing to do. |
Richard Smith | f93ec76 | 2013-11-15 02:58:23 +0000 | [diff] [blame] | 6840 | // FIXME: For a friend function, we have not marked the function as being |
| 6841 | // a friend yet, so 'isDependentContext' on the FD doesn't work. |
Richard Smith | 37e849a | 2013-08-14 20:16:31 +0000 | [diff] [blame] | 6842 | const FunctionProtoType *FPT = |
| 6843 | NewFD->getType()->castAs<FunctionProtoType>(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 6844 | QualType Result = |
| 6845 | SubstAutoType(FPT->getReturnType(), Context.DependentTy); |
| 6846 | NewFD->setType(Context.getFunctionType(Result, FPT->getParamTypes(), |
Richard Smith | 37e849a | 2013-08-14 20:16:31 +0000 | [diff] [blame] | 6847 | FPT->getExtProtoInfo())); |
| 6848 | } |
| 6849 | |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 6850 | // C++ [dcl.fct.spec]p3: |
David Blaikie | d662a79 | 2011-10-19 22:56:21 +0000 | [diff] [blame] | 6851 | // The inline specifier shall not appear on a block scope function |
| 6852 | // declaration. |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 6853 | if (isInline && !NewFD->isInvalidDecl()) { |
| 6854 | if (CurContext->isFunctionOrMethod()) { |
| 6855 | // 'inline' is not allowed on block scope function declaration. |
| 6856 | Diag(D.getDeclSpec().getInlineSpecLoc(), |
| 6857 | diag::err_inline_declaration_block_scope) << Name |
| 6858 | << FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc()); |
| 6859 | } |
| 6860 | } |
Kaelyn Uhrain | d7e19ce | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6861 | |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 6862 | // C++ [dcl.fct.spec]p6: |
| 6863 | // The explicit specifier shall be used only in the declaration of a |
David Blaikie | d662a79 | 2011-10-19 22:56:21 +0000 | [diff] [blame] | 6864 | // constructor or conversion function within its class definition; |
| 6865 | // see 12.3.1 and 12.3.2. |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 6866 | if (isExplicit && !NewFD->isInvalidDecl()) { |
| 6867 | if (!CurContext->isRecord()) { |
| 6868 | // 'explicit' was specified outside of the class. |
| 6869 | Diag(D.getDeclSpec().getExplicitSpecLoc(), |
| 6870 | diag::err_explicit_out_of_class) |
| 6871 | << FixItHint::CreateRemoval(D.getDeclSpec().getExplicitSpecLoc()); |
| 6872 | } else if (!isa<CXXConstructorDecl>(NewFD) && |
| 6873 | !isa<CXXConversionDecl>(NewFD)) { |
| 6874 | // 'explicit' was specified on a function that wasn't a constructor |
| 6875 | // or conversion function. |
| 6876 | Diag(D.getDeclSpec().getExplicitSpecLoc(), |
| 6877 | diag::err_explicit_non_ctor_or_conv_function) |
| 6878 | << FixItHint::CreateRemoval(D.getDeclSpec().getExplicitSpecLoc()); |
| 6879 | } |
| 6880 | } |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 6881 | |
Richard Smith | af1fc7a | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 6882 | if (isConstexpr) { |
Richard Smith | 21c8fa8 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 6883 | // C++11 [dcl.constexpr]p2: constexpr functions and constexpr constructors |
Richard Smith | af1fc7a | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 6884 | // are implicitly inline. |
| 6885 | NewFD->setImplicitlyInline(); |
| 6886 | |
Richard Smith | 21c8fa8 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 6887 | // C++11 [dcl.constexpr]p3: functions declared constexpr are required to |
Richard Smith | af1fc7a | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 6888 | // be either constructors or to return a literal type. Therefore, |
| 6889 | // destructors cannot be declared constexpr. |
| 6890 | if (isa<CXXDestructorDecl>(NewFD)) |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 6891 | Diag(D.getDeclSpec().getConstexprSpecLoc(), diag::err_constexpr_dtor); |
Richard Smith | af1fc7a | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 6892 | } |
| 6893 | |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 6894 | // If __module_private__ was specified, mark the function accordingly. |
| 6895 | if (D.getDeclSpec().isModulePrivateSpecified()) { |
Douglas Gregor | d023aec | 2011-09-09 20:53:38 +0000 | [diff] [blame] | 6896 | if (isFunctionTemplateSpecialization) { |
| 6897 | SourceLocation ModulePrivateLoc |
| 6898 | = D.getDeclSpec().getModulePrivateSpecLoc(); |
| 6899 | Diag(ModulePrivateLoc, diag::err_module_private_specialization) |
| 6900 | << 0 |
| 6901 | << FixItHint::CreateRemoval(ModulePrivateLoc); |
| 6902 | } else { |
| 6903 | NewFD->setModulePrivate(); |
| 6904 | if (FunctionTemplate) |
| 6905 | FunctionTemplate->setModulePrivate(); |
| 6906 | } |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 6907 | } |
Richard Smith | af1fc7a | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 6908 | |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 6909 | if (isFriend) { |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 6910 | if (FunctionTemplate) { |
Richard Smith | 22050f2 | 2013-07-17 23:53:16 +0000 | [diff] [blame] | 6911 | FunctionTemplate->setObjectOfFriendDecl(); |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 6912 | FunctionTemplate->setAccess(AS_public); |
| 6913 | } |
Richard Smith | 22050f2 | 2013-07-17 23:53:16 +0000 | [diff] [blame] | 6914 | NewFD->setObjectOfFriendDecl(); |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 6915 | NewFD->setAccess(AS_public); |
| 6916 | } |
| 6917 | |
Douglas Gregor | 45fa560 | 2011-11-07 20:56:01 +0000 | [diff] [blame] | 6918 | // If a function is defined as defaulted or deleted, mark it as such now. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 6919 | // FIXME: Does this ever happen? ActOnStartOfFunctionDef forces the function |
| 6920 | // definition kind to FDK_Definition. |
Douglas Gregor | 45fa560 | 2011-11-07 20:56:01 +0000 | [diff] [blame] | 6921 | switch (D.getFunctionDefinitionKind()) { |
| 6922 | case FDK_Declaration: |
| 6923 | case FDK_Definition: |
| 6924 | break; |
| 6925 | |
| 6926 | case FDK_Defaulted: |
| 6927 | NewFD->setDefaulted(); |
| 6928 | break; |
| 6929 | |
| 6930 | case FDK_Deleted: |
| 6931 | NewFD->setDeletedAsWritten(); |
| 6932 | break; |
| 6933 | } |
| 6934 | |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 6935 | if (isa<CXXMethodDecl>(NewFD) && DC == CurContext && |
| 6936 | D.isFunctionDefinition()) { |
Douglas Gregor | 45fa560 | 2011-11-07 20:56:01 +0000 | [diff] [blame] | 6937 | // C++ [class.mfct]p2: |
| 6938 | // A member function may be defined (8.4) in its class definition, in |
| 6939 | // which case it is an inline member function (7.1.2) |
John McCall | bfdcdc8 | 2010-12-15 04:00:32 +0000 | [diff] [blame] | 6940 | NewFD->setImplicitlyInline(); |
| 6941 | } |
| 6942 | |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 6943 | if (SC == SC_Static && isa<CXXMethodDecl>(NewFD) && |
| 6944 | !CurContext->isRecord()) { |
| 6945 | // C++ [class.static]p1: |
| 6946 | // A data or function member of a class may be declared static |
| 6947 | // in a class definition, in which case it is a static member of |
| 6948 | // the class. |
| 6949 | |
| 6950 | // Complain about the 'static' specifier if it's on an out-of-line |
| 6951 | // member function definition. |
| 6952 | Diag(D.getDeclSpec().getStorageClassSpecLoc(), |
| 6953 | diag::err_static_out_of_line) |
| 6954 | << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc()); |
| 6955 | } |
Richard Smith | 444d384 | 2012-10-20 08:26:51 +0000 | [diff] [blame] | 6956 | |
| 6957 | // C++11 [except.spec]p15: |
| 6958 | // A deallocation function with no exception-specification is treated |
| 6959 | // as if it were specified with noexcept(true). |
| 6960 | const FunctionProtoType *FPT = R->getAs<FunctionProtoType>(); |
| 6961 | if ((Name.getCXXOverloadedOperator() == OO_Delete || |
| 6962 | Name.getCXXOverloadedOperator() == OO_Array_Delete) && |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6963 | getLangOpts().CPlusPlus11 && FPT && !FPT->hasExceptionSpec()) { |
Richard Smith | 444d384 | 2012-10-20 08:26:51 +0000 | [diff] [blame] | 6964 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 6965 | EPI.ExceptionSpecType = EST_BasicNoexcept; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 6966 | NewFD->setType(Context.getFunctionType(FPT->getReturnType(), |
| 6967 | FPT->getParamTypes(), EPI)); |
Richard Smith | 444d384 | 2012-10-20 08:26:51 +0000 | [diff] [blame] | 6968 | } |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 6969 | } |
Kaelyn Uhrain | d7e19ce | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6970 | |
| 6971 | // Filter out previous declarations that don't match the scope. |
Richard Smith | a41c97a | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 6972 | FilterLookupForScope(Previous, OriginalDC, S, shouldConsiderLinkage(NewFD), |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 6973 | D.getCXXScopeSpec().isNotEmpty() || |
Kaelyn Uhrain | d7e19ce | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6974 | isExplicitSpecialization || |
| 6975 | isFunctionTemplateSpecialization); |
Richard Smith | dd9459f | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 6976 | |
Zhongxing Xu | 416fcaf | 2009-01-16 01:13:29 +0000 | [diff] [blame] | 6977 | // Handle GNU asm-label extension (encoded as an attribute). |
| 6978 | if (Expr *E = (Expr*) D.getAsmLabel()) { |
| 6979 | // The parser guarantees this is a string. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6980 | StringLiteral *SE = cast<StringLiteral>(E); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 6981 | NewFD->addAttr(::new (Context) AsmLabelAttr(SE->getStrTokenLoc(0), Context, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 6982 | SE->getString(), 0)); |
David Chisnall | 5f3c163 | 2012-02-18 16:12:34 +0000 | [diff] [blame] | 6983 | } else if (!ExtnameUndeclaredIdentifiers.empty()) { |
| 6984 | llvm::DenseMap<IdentifierInfo*,AsmLabelAttr*>::iterator I = |
| 6985 | ExtnameUndeclaredIdentifiers.find(NewFD->getIdentifier()); |
| 6986 | if (I != ExtnameUndeclaredIdentifiers.end()) { |
| 6987 | NewFD->addAttr(I->second); |
| 6988 | ExtnameUndeclaredIdentifiers.erase(I); |
| 6989 | } |
Zhongxing Xu | 416fcaf | 2009-01-16 01:13:29 +0000 | [diff] [blame] | 6990 | } |
| 6991 | |
Chris Lattner | 2dbd285 | 2009-04-25 06:12:16 +0000 | [diff] [blame] | 6992 | // Copy the parameter declarations from the declarator D to the function |
| 6993 | // declaration NewFD, if they are available. First scavenge them into Params. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6994 | SmallVector<ParmVarDecl*, 16> Params; |
Abramo Bagnara | 723df24 | 2010-12-14 22:11:44 +0000 | [diff] [blame] | 6995 | if (D.isFunctionDeclarator()) { |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 6996 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo(); |
Zhongxing Xu | 416fcaf | 2009-01-16 01:13:29 +0000 | [diff] [blame] | 6997 | |
Zhongxing Xu | 416fcaf | 2009-01-16 01:13:29 +0000 | [diff] [blame] | 6998 | // Check for C99 6.7.5.3p10 - foo(void) is a non-varargs |
| 6999 | // function that takes no arguments, not a function that takes a |
| 7000 | // single void argument. |
| 7001 | // We let through "const void" here because Sema::GetTypeForDeclarator |
| 7002 | // already checks for that case. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 7003 | if (FTIHasNonVoidParameters(FTI) && FTI.Params[0].Param) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 7004 | for (unsigned i = 0, e = FTI.NumParams; i != e; ++i) { |
| 7005 | ParmVarDecl *Param = cast<ParmVarDecl>(FTI.Params[i].Param); |
Argyrios Kyrtzidis | 9bedef6 | 2009-07-14 03:18:53 +0000 | [diff] [blame] | 7006 | assert(Param->getDeclContext() != NewFD && "Was set before ?"); |
| 7007 | Param->setDeclContext(NewFD); |
| 7008 | Params.push_back(Param); |
John McCall | f19de1c | 2010-04-14 01:27:20 +0000 | [diff] [blame] | 7009 | |
| 7010 | if (Param->isInvalidDecl()) |
| 7011 | NewFD->setInvalidDecl(); |
Argyrios Kyrtzidis | 9bedef6 | 2009-07-14 03:18:53 +0000 | [diff] [blame] | 7012 | } |
Zhongxing Xu | 416fcaf | 2009-01-16 01:13:29 +0000 | [diff] [blame] | 7013 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7014 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 7015 | } else if (const FunctionProtoType *FT = R->getAs<FunctionProtoType>()) { |
Chris Lattner | 1ad9b28 | 2009-04-25 06:03:53 +0000 | [diff] [blame] | 7016 | // When we're declaring a function with a typedef, typeof, etc as in the |
Zhongxing Xu | 416fcaf | 2009-01-16 01:13:29 +0000 | [diff] [blame] | 7017 | // following example, we'll need to synthesize (unnamed) |
| 7018 | // parameters for use in the declaration. |
| 7019 | // |
| 7020 | // @code |
| 7021 | // typedef void fn(int); |
| 7022 | // fn f; |
| 7023 | // @endcode |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7024 | |
Chris Lattner | 1ad9b28 | 2009-04-25 06:03:53 +0000 | [diff] [blame] | 7025 | // Synthesize a parameter for each argument type. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 7026 | for (const auto &AI : FT->param_types()) { |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 7027 | ParmVarDecl *Param = |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 7028 | BuildParmVarDeclForTypedef(NewFD, D.getIdentifierLoc(), AI); |
John McCall | fb44de9 | 2011-05-01 22:35:37 +0000 | [diff] [blame] | 7029 | Param->setScopeInfo(0, Params.size()); |
Chris Lattner | 1ad9b28 | 2009-04-25 06:03:53 +0000 | [diff] [blame] | 7030 | Params.push_back(Param); |
Zhongxing Xu | 416fcaf | 2009-01-16 01:13:29 +0000 | [diff] [blame] | 7031 | } |
Chris Lattner | 84bb944 | 2009-04-25 18:38:18 +0000 | [diff] [blame] | 7032 | } else { |
| 7033 | assert(R->isFunctionNoProtoType() && NewFD->getNumParams() == 0 && |
| 7034 | "Should not need args for typedef of non-prototype fn"); |
Zhongxing Xu | 416fcaf | 2009-01-16 01:13:29 +0000 | [diff] [blame] | 7035 | } |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 7036 | |
Chris Lattner | 2dbd285 | 2009-04-25 06:12:16 +0000 | [diff] [blame] | 7037 | // Finally, we know we have the right number of parameters, install them. |
David Blaikie | 4278c65 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 7038 | NewFD->setParams(Params); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7039 | |
James Molloy | 16f1f71 | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 7040 | // Find all anonymous symbols defined during the declaration of this function |
| 7041 | // and add to NewFD. This lets us track decls such 'enum Y' in: |
| 7042 | // |
| 7043 | // void f(enum Y {AA} x) {} |
| 7044 | // |
| 7045 | // which would otherwise incorrectly end up in the translation unit scope. |
| 7046 | NewFD->setDeclsInPrototypeScope(DeclsInPrototypeScope); |
| 7047 | DeclsInPrototypeScope.clear(); |
| 7048 | |
Richard Smith | 7586a6e | 2013-01-30 05:45:05 +0000 | [diff] [blame] | 7049 | if (D.getDeclSpec().isNoreturnSpecified()) |
| 7050 | NewFD->addAttr( |
| 7051 | ::new(Context) C11NoReturnAttr(D.getDeclSpec().getNoreturnSpecLoc(), |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 7052 | Context, 0)); |
Richard Smith | 7586a6e | 2013-01-30 05:45:05 +0000 | [diff] [blame] | 7053 | |
Richard Smith | b03a9df | 2012-03-13 05:56:40 +0000 | [diff] [blame] | 7054 | // Functions returning a variably modified type violate C99 6.7.5.2p2 |
| 7055 | // because all functions have linkage. |
| 7056 | if (!NewFD->isInvalidDecl() && |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 7057 | NewFD->getReturnType()->isVariablyModifiedType()) { |
Richard Smith | b03a9df | 2012-03-13 05:56:40 +0000 | [diff] [blame] | 7058 | Diag(NewFD->getLocation(), diag::err_vm_func_decl); |
| 7059 | NewFD->setInvalidDecl(); |
| 7060 | } |
| 7061 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 7062 | if (D.isFunctionDefinition() && CodeSegStack.CurrentValue && |
| 7063 | !NewFD->hasAttr<SectionAttr>()) { |
| 7064 | NewFD->addAttr( |
| 7065 | SectionAttr::CreateImplicit(Context, SectionAttr::Declspec_allocate, |
| 7066 | CodeSegStack.CurrentValue->getString(), |
| 7067 | CodeSegStack.CurrentPragmaLocation)); |
| 7068 | if (UnifySection(CodeSegStack.CurrentValue->getString(), |
| 7069 | PSF_Implicit | PSF_Execute | PSF_Read, NewFD)) |
| 7070 | NewFD->dropAttr<SectionAttr>(); |
| 7071 | } |
| 7072 | |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 7073 | // Handle attributes. |
Richard Smith | 4a97b8e | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 7074 | ProcessDeclAttributes(S, NewFD, D); |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 7075 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 7076 | QualType RetType = NewFD->getReturnType(); |
Kaelyn Uhrain | 51ceb7b | 2012-11-12 23:48:05 +0000 | [diff] [blame] | 7077 | const CXXRecordDecl *Ret = RetType->isRecordType() ? |
| 7078 | RetType->getAsCXXRecordDecl() : RetType->getPointeeCXXRecordDecl(); |
| 7079 | if (!NewFD->isInvalidDecl() && !NewFD->hasAttr<WarnUnusedResultAttr>() && |
| 7080 | Ret && Ret->hasAttr<WarnUnusedResultAttr>()) { |
Kaelyn Uhrain | 97c81bf | 2012-11-13 21:23:31 +0000 | [diff] [blame] | 7081 | const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(NewFD); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 7082 | // Attach WarnUnusedResult to functions returning types with that attribute. |
| 7083 | // Don't apply the attribute to that type's own non-static member functions |
| 7084 | // (to avoid warning on things like assignment operators) |
| 7085 | if (!MD || MD->getParent() != Ret) |
| 7086 | NewFD->addAttr(WarnUnusedResultAttr::CreateImplicit(Context)); |
| 7087 | } |
| 7088 | |
| 7089 | if (getLangOpts().OpenCL) { |
| 7090 | // OpenCL v1.1 s6.5: Using an address space qualifier in a function return |
| 7091 | // type declaration will generate a compilation error. |
| 7092 | unsigned AddressSpace = RetType.getAddressSpace(); |
| 7093 | if (AddressSpace == LangAS::opencl_local || |
| 7094 | AddressSpace == LangAS::opencl_global || |
| 7095 | AddressSpace == LangAS::opencl_constant) { |
| 7096 | Diag(NewFD->getLocation(), |
| 7097 | diag::err_opencl_return_value_with_address_space); |
| 7098 | NewFD->setInvalidDecl(); |
Kaelyn Uhrain | 97c81bf | 2012-11-13 21:23:31 +0000 | [diff] [blame] | 7099 | } |
Kaelyn Uhrain | 51ceb7b | 2012-11-12 23:48:05 +0000 | [diff] [blame] | 7100 | } |
| 7101 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7102 | if (!getLangOpts().CPlusPlus) { |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7103 | // Perform semantic checking on the function declaration. |
Douglas Gregor | 89b9f10 | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 7104 | bool isExplicitSpecialization=false; |
David Majnemer | c371db6 | 2013-07-06 02:13:46 +0000 | [diff] [blame] | 7105 | if (!NewFD->isInvalidDecl() && NewFD->isMain()) |
| 7106 | CheckMain(NewFD, D.getDeclSpec()); |
| 7107 | |
David Majnemer | e9f6f33 | 2013-09-16 22:44:20 +0000 | [diff] [blame] | 7108 | if (!NewFD->isInvalidDecl() && NewFD->isMSVCRTEntryPoint()) |
| 7109 | CheckMSVCRTEntryPoint(NewFD); |
| 7110 | |
David Majnemer | c371db6 | 2013-07-06 02:13:46 +0000 | [diff] [blame] | 7111 | if (!NewFD->isInvalidDecl()) |
Richard Smith | b03a9df | 2012-03-13 05:56:40 +0000 | [diff] [blame] | 7112 | D.setRedeclaration(CheckFunctionDeclaration(S, NewFD, Previous, |
| 7113 | isExplicitSpecialization)); |
Fariborz Jahanian | 37c765a | 2012-09-05 17:52:12 +0000 | [diff] [blame] | 7114 | else if (!Previous.empty()) |
Richard Smith | dd9459f | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 7115 | // Make graceful recovery from an invalid redeclaration. |
| 7116 | D.setRedeclaration(true); |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 7117 | assert((NewFD->isInvalidDecl() || !D.isRedeclaration() || |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7118 | Previous.getResultKind() != LookupResult::FoundOverloaded) && |
| 7119 | "previous declaration set still overloaded"); |
| 7120 | } else { |
Richard Smith | cf19e5b | 2013-11-16 01:57:09 +0000 | [diff] [blame] | 7121 | // C++11 [replacement.functions]p3: |
| 7122 | // The program's definitions shall not be specified as inline. |
| 7123 | // |
| 7124 | // N.B. We diagnose declarations instead of definitions per LWG issue 2340. |
| 7125 | // |
| 7126 | // Suppress the diagnostic if the function is __attribute__((used)), since |
| 7127 | // that forces an external definition to be emitted. |
| 7128 | if (D.getDeclSpec().isInlineSpecified() && |
| 7129 | NewFD->isReplaceableGlobalAllocationFunction() && |
| 7130 | !NewFD->hasAttr<UsedAttr>()) |
| 7131 | Diag(D.getDeclSpec().getInlineSpecLoc(), |
| 7132 | diag::ext_operator_new_delete_declared_inline) |
| 7133 | << NewFD->getDeclName(); |
| 7134 | |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7135 | // If the declarator is a template-id, translate the parser's template |
| 7136 | // argument list into our AST format. |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7137 | if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) { |
| 7138 | TemplateIdAnnotation *TemplateId = D.getName().TemplateId; |
| 7139 | TemplateArgs.setLAngleLoc(TemplateId->LAngleLoc); |
| 7140 | TemplateArgs.setRAngleLoc(TemplateId->RAngleLoc); |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 7141 | ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(), |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7142 | TemplateId->NumArgs); |
| 7143 | translateTemplateArguments(TemplateArgsPtr, |
| 7144 | TemplateArgs); |
Douglas Gregor | 0b60d9e | 2009-09-25 23:53:26 +0000 | [diff] [blame] | 7145 | |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7146 | HasExplicitTemplateArgs = true; |
Douglas Gregor | 0b60d9e | 2009-09-25 23:53:26 +0000 | [diff] [blame] | 7147 | |
Douglas Gregor | 89b9f10 | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 7148 | if (NewFD->isInvalidDecl()) { |
| 7149 | HasExplicitTemplateArgs = false; |
| 7150 | } else if (FunctionTemplate) { |
Douglas Gregor | 5505c72 | 2011-01-24 18:54:39 +0000 | [diff] [blame] | 7151 | // Function template with explicit template arguments. |
| 7152 | Diag(D.getIdentifierLoc(), diag::err_function_template_partial_spec) |
| 7153 | << SourceRange(TemplateId->LAngleLoc, TemplateId->RAngleLoc); |
| 7154 | |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7155 | HasExplicitTemplateArgs = false; |
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 7156 | } else { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 7157 | assert((isFunctionTemplateSpecialization || |
| 7158 | D.getDeclSpec().isFriendSpecified()) && |
| 7159 | "should have a 'template<>' for this decl"); |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7160 | // "friend void foo<>(int);" is an implicit specialization decl. |
| 7161 | isFunctionTemplateSpecialization = true; |
Francois Pichet | c71d8eb | 2010-10-01 21:19:28 +0000 | [diff] [blame] | 7162 | } |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7163 | } else if (isFriend && isFunctionTemplateSpecialization) { |
| 7164 | // This combination is only possible in a recovery case; the user |
| 7165 | // wrote something like: |
| 7166 | // template <> friend void foo(int); |
| 7167 | // which we're recovering from as if the user had written: |
| 7168 | // friend void foo<>(int); |
| 7169 | // Go ahead and fake up a template id. |
| 7170 | HasExplicitTemplateArgs = true; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 7171 | TemplateArgs.setLAngleLoc(D.getIdentifierLoc()); |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7172 | TemplateArgs.setRAngleLoc(D.getIdentifierLoc()); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 7173 | } |
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 7174 | |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7175 | // If it's a friend (and only if it's a friend), it's possible |
| 7176 | // that either the specialized function type or the specialized |
| 7177 | // template is dependent, and therefore matching will fail. In |
| 7178 | // this case, don't check the specialization yet. |
Douglas Gregor | 33ab0da | 2011-10-09 20:59:17 +0000 | [diff] [blame] | 7179 | bool InstantiationDependent = false; |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7180 | if (isFunctionTemplateSpecialization && isFriend && |
Douglas Gregor | 33ab0da | 2011-10-09 20:59:17 +0000 | [diff] [blame] | 7181 | (NewFD->getType()->isDependentType() || DC->isDependentContext() || |
| 7182 | TemplateSpecializationType::anyDependentTemplateArguments( |
| 7183 | TemplateArgs.getArgumentArray(), TemplateArgs.size(), |
| 7184 | InstantiationDependent))) { |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7185 | assert(HasExplicitTemplateArgs && |
| 7186 | "friend function specialization without template args"); |
| 7187 | if (CheckDependentFunctionTemplateSpecialization(NewFD, TemplateArgs, |
| 7188 | Previous)) |
| 7189 | NewFD->setInvalidDecl(); |
| 7190 | } else if (isFunctionTemplateSpecialization) { |
Douglas Gregor | eef7ac5 | 2011-03-16 19:27:09 +0000 | [diff] [blame] | 7191 | if (CurContext->isDependentContext() && CurContext->isRecord() |
Francois Pichet | ab01add | 2011-06-03 13:59:45 +0000 | [diff] [blame] | 7192 | && !isFriend) { |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 7193 | isDependentClassScopeExplicitSpecialization = true; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7194 | Diag(NewFD->getLocation(), getLangOpts().MicrosoftExt ? |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 7195 | diag::ext_function_specialization_in_class : |
| 7196 | diag::err_function_specialization_in_class) |
Douglas Gregor | eef7ac5 | 2011-03-16 19:27:09 +0000 | [diff] [blame] | 7197 | << NewFD->getDeclName(); |
Douglas Gregor | eef7ac5 | 2011-03-16 19:27:09 +0000 | [diff] [blame] | 7198 | } else if (CheckFunctionTemplateSpecialization(NewFD, |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 7199 | (HasExplicitTemplateArgs ? &TemplateArgs |
| 7200 | : nullptr), |
Douglas Gregor | eef7ac5 | 2011-03-16 19:27:09 +0000 | [diff] [blame] | 7201 | Previous)) |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7202 | NewFD->setInvalidDecl(); |
Douglas Gregor | e885e18 | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 7203 | |
| 7204 | // C++ [dcl.stc]p1: |
| 7205 | // A storage-class-specifier shall not be specified in an explicit |
| 7206 | // specialization (14.7.3) |
Richard Trieu | 62ab010 | 2013-05-16 02:14:08 +0000 | [diff] [blame] | 7207 | FunctionTemplateSpecializationInfo *Info = |
| 7208 | NewFD->getTemplateSpecializationInfo(); |
| 7209 | if (Info && SC != SC_None) { |
| 7210 | if (SC != Info->getTemplate()->getTemplatedDecl()->getStorageClass()) |
Douglas Gregor | 0f9dc86 | 2011-06-17 05:09:08 +0000 | [diff] [blame] | 7211 | Diag(NewFD->getLocation(), |
| 7212 | diag::err_explicit_specialization_inconsistent_storage_class) |
| 7213 | << SC |
| 7214 | << FixItHint::CreateRemoval( |
| 7215 | D.getDeclSpec().getStorageClassSpecLoc()); |
| 7216 | |
| 7217 | else |
| 7218 | Diag(NewFD->getLocation(), |
| 7219 | diag::ext_explicit_specialization_storage_class) |
| 7220 | << FixItHint::CreateRemoval( |
| 7221 | D.getDeclSpec().getStorageClassSpecLoc()); |
Douglas Gregor | e885e18 | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 7222 | } |
| 7223 | |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7224 | } else if (isExplicitSpecialization && isa<CXXMethodDecl>(NewFD)) { |
| 7225 | if (CheckMemberSpecialization(NewFD, Previous)) |
| 7226 | NewFD->setInvalidDecl(); |
| 7227 | } |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 7228 | |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7229 | // Perform semantic checking on the function declaration. |
David Blaikie | 14068e8 | 2011-09-08 06:33:04 +0000 | [diff] [blame] | 7230 | if (!isDependentClassScopeExplicitSpecialization) { |
David Majnemer | c371db6 | 2013-07-06 02:13:46 +0000 | [diff] [blame] | 7231 | if (!NewFD->isInvalidDecl() && NewFD->isMain()) |
| 7232 | CheckMain(NewFD, D.getDeclSpec()); |
| 7233 | |
David Majnemer | e9f6f33 | 2013-09-16 22:44:20 +0000 | [diff] [blame] | 7234 | if (!NewFD->isInvalidDecl() && NewFD->isMSVCRTEntryPoint()) |
| 7235 | CheckMSVCRTEntryPoint(NewFD); |
| 7236 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 7237 | if (!NewFD->isInvalidDecl()) |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 7238 | D.setRedeclaration(CheckFunctionDeclaration(S, NewFD, Previous, |
| 7239 | isExplicitSpecialization)); |
David Blaikie | 14068e8 | 2011-09-08 06:33:04 +0000 | [diff] [blame] | 7240 | } |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7241 | |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 7242 | assert((NewFD->isInvalidDecl() || !D.isRedeclaration() || |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7243 | Previous.getResultKind() != LookupResult::FoundOverloaded) && |
| 7244 | "previous declaration set still overloaded"); |
| 7245 | |
| 7246 | NamedDecl *PrincipalDecl = (FunctionTemplate |
| 7247 | ? cast<NamedDecl>(FunctionTemplate) |
| 7248 | : NewFD); |
| 7249 | |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 7250 | if (isFriend && D.isRedeclaration()) { |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7251 | AccessSpecifier Access = AS_public; |
| 7252 | if (!NewFD->isInvalidDecl()) |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 7253 | Access = NewFD->getPreviousDecl()->getAccess(); |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7254 | |
| 7255 | NewFD->setAccess(Access); |
| 7256 | if (FunctionTemplate) FunctionTemplate->setAccess(Access); |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7257 | } |
| 7258 | |
| 7259 | if (NewFD->isOverloadedOperator() && !DC->isRecord() && |
| 7260 | PrincipalDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary)) |
| 7261 | PrincipalDecl->setNonMemberOperator(); |
| 7262 | |
| 7263 | // If we have a function template, check the template parameter |
| 7264 | // list. This will check and merge default template arguments. |
| 7265 | if (FunctionTemplate) { |
David Blaikie | d662a79 | 2011-10-19 22:56:21 +0000 | [diff] [blame] | 7266 | FunctionTemplateDecl *PrevTemplate = |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 7267 | FunctionTemplate->getPreviousDecl(); |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7268 | CheckTemplateParameterList(FunctionTemplate->getTemplateParameters(), |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 7269 | PrevTemplate ? PrevTemplate->getTemplateParameters() |
| 7270 | : nullptr, |
Douglas Gregor | d89d86f | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 7271 | D.getDeclSpec().isFriendSpecified() |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 7272 | ? (D.isFunctionDefinition() |
Douglas Gregor | d89d86f | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 7273 | ? TPC_FriendFunctionTemplateDefinition |
| 7274 | : TPC_FriendFunctionTemplate) |
| 7275 | : (D.getCXXScopeSpec().isSet() && |
Douglas Gregor | 461bf2e | 2011-02-04 12:22:53 +0000 | [diff] [blame] | 7276 | DC && DC->isRecord() && |
| 7277 | DC->isDependentContext()) |
Douglas Gregor | d89d86f | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 7278 | ? TPC_ClassTemplateMember |
| 7279 | : TPC_FunctionTemplate); |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7280 | } |
| 7281 | |
| 7282 | if (NewFD->isInvalidDecl()) { |
| 7283 | // Ignore all the rest of this. |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 7284 | } else if (!D.isRedeclaration()) { |
Kaelyn Uhrain | f09ce39 | 2011-10-11 00:28:52 +0000 | [diff] [blame] | 7285 | struct ActOnFDArgs ExtraArgs = { S, D, TemplateParamLists, |
Kaelyn Uhrain | 2afd766 | 2011-10-11 00:28:39 +0000 | [diff] [blame] | 7286 | AddToScope }; |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7287 | // Fake up an access specifier if it's supposed to be a class member. |
| 7288 | if (isa<CXXRecordDecl>(NewFD->getDeclContext())) |
| 7289 | NewFD->setAccess(AS_public); |
| 7290 | |
| 7291 | // Qualified decls generally require a previous declaration. |
| 7292 | if (D.getCXXScopeSpec().isSet()) { |
| 7293 | // ...with the major exception of templated-scope or |
| 7294 | // dependent-scope friend declarations. |
| 7295 | |
| 7296 | // TODO: we currently also suppress this check in dependent |
| 7297 | // contexts because (1) the parameter depth will be off when |
| 7298 | // matching friend templates and (2) we might actually be |
| 7299 | // selecting a friend based on a dependent factor. But there |
| 7300 | // are situations where these conditions don't apply and we |
| 7301 | // can actually do this check immediately. |
| 7302 | if (isFriend && |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 7303 | (TemplateParamLists.size() || |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7304 | D.getCXXScopeSpec().getScopeRep()->isDependent() || |
| 7305 | CurContext->isDependentContext())) { |
Chandler Carruth | 47eb2b6 | 2011-08-19 01:38:33 +0000 | [diff] [blame] | 7306 | // ignore these |
| 7307 | } else { |
| 7308 | // The user tried to provide an out-of-line definition for a |
| 7309 | // function that is a member of a class or namespace, but there |
| 7310 | // was no such member function declared (C++ [class.mfct]p2, |
| 7311 | // C++ [namespace.memdef]p2). For example: |
| 7312 | // |
| 7313 | // class X { |
| 7314 | // void f() const; |
| 7315 | // }; |
| 7316 | // |
| 7317 | // void X::f() { } // ill-formed |
| 7318 | // |
| 7319 | // Complain about this problem, and attempt to suggest close |
| 7320 | // matches (e.g., those that differ only in cv-qualifiers and |
| 7321 | // whether the parameter types are references). |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7322 | |
Richard Smith | 4e9686b | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 7323 | if (NamedDecl *Result = DiagnoseInvalidRedeclaration( |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 7324 | *this, Previous, NewFD, ExtraArgs, false, nullptr)) { |
Kaelyn Uhrain | 2afd766 | 2011-10-11 00:28:39 +0000 | [diff] [blame] | 7325 | AddToScope = ExtraArgs.AddToScope; |
| 7326 | return Result; |
| 7327 | } |
Chandler Carruth | 47eb2b6 | 2011-08-19 01:38:33 +0000 | [diff] [blame] | 7328 | } |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7329 | |
| 7330 | // Unqualified local friend declarations are required to resolve |
| 7331 | // to something. |
Chandler Carruth | 3d095fe | 2011-08-19 01:40:11 +0000 | [diff] [blame] | 7332 | } else if (isFriend && cast<CXXRecordDecl>(CurContext)->isLocalClass()) { |
Richard Smith | 4e9686b | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 7333 | if (NamedDecl *Result = DiagnoseInvalidRedeclaration( |
| 7334 | *this, Previous, NewFD, ExtraArgs, true, S)) { |
Kaelyn Uhrain | 2afd766 | 2011-10-11 00:28:39 +0000 | [diff] [blame] | 7335 | AddToScope = ExtraArgs.AddToScope; |
| 7336 | return Result; |
| 7337 | } |
Chandler Carruth | 3d095fe | 2011-08-19 01:40:11 +0000 | [diff] [blame] | 7338 | } |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7339 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 7340 | } else if (!D.isFunctionDefinition() && |
| 7341 | isa<CXXMethodDecl>(NewFD) && NewFD->isOutOfLine() && |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7342 | !isFriend && !isFunctionTemplateSpecialization && |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 7343 | !isExplicitSpecialization) { |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7344 | // An out-of-line member function declaration must also be a |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 7345 | // definition (C++ [class.mfct]p2). |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7346 | // Note that this is not the case for explicit specializations of |
| 7347 | // function templates or member functions of class templates, per |
David Blaikie | d662a79 | 2011-10-19 22:56:21 +0000 | [diff] [blame] | 7348 | // C++ [temp.expl.spec]p2. We also allow these declarations as an |
| 7349 | // extension for compatibility with old SWIG code which likes to |
| 7350 | // generate them. |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7351 | Diag(NewFD->getLocation(), diag::ext_out_of_line_declaration) |
| 7352 | << D.getCXXScopeSpec().getRange(); |
| 7353 | } |
| 7354 | } |
Ryan Flynn | 478fbc6 | 2009-07-25 22:29:44 +0000 | [diff] [blame] | 7355 | |
Rafael Espindola | 65611bf | 2013-03-02 21:41:48 +0000 | [diff] [blame] | 7356 | ProcessPragmaWeak(S, NewFD); |
Rafael Espindola | 2a5bb50 | 2013-01-16 23:11:15 +0000 | [diff] [blame] | 7357 | checkAttributesAfterMerging(*this, *NewFD); |
| 7358 | |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 7359 | AddKnownFunctionAttributes(NewFD); |
| 7360 | |
Douglas Gregor | d945538 | 2010-08-06 13:50:58 +0000 | [diff] [blame] | 7361 | if (NewFD->hasAttr<OverloadableAttr>() && |
| 7362 | !NewFD->getType()->getAs<FunctionProtoType>()) { |
| 7363 | Diag(NewFD->getLocation(), |
| 7364 | diag::err_attribute_overloadable_no_prototype) |
| 7365 | << NewFD; |
| 7366 | |
| 7367 | // Turn this into a variadic function with no parameters. |
| 7368 | const FunctionType *FT = NewFD->getType()->getAs<FunctionType>(); |
Reid Kleckner | ef07203 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 7369 | FunctionProtoType::ExtProtoInfo EPI( |
| 7370 | Context.getDefaultCallingConvention(true, false)); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 7371 | EPI.Variadic = true; |
| 7372 | EPI.ExtInfo = FT->getExtInfo(); |
| 7373 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 7374 | QualType R = Context.getFunctionType(FT->getReturnType(), None, EPI); |
Douglas Gregor | d945538 | 2010-08-06 13:50:58 +0000 | [diff] [blame] | 7375 | NewFD->setType(R); |
| 7376 | } |
| 7377 | |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 7378 | // If there's a #pragma GCC visibility in scope, and this isn't a class |
| 7379 | // member, set the visibility of this function. |
Rafael Espindola | 181e3ec | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 7380 | if (!DC->isRecord() && NewFD->isExternallyVisible()) |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 7381 | AddPushedVisibilityAttribute(NewFD); |
| 7382 | |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 7383 | // If there's a #pragma clang arc_cf_code_audited in scope, consider |
| 7384 | // marking the function. |
| 7385 | AddCFAuditedAttribute(NewFD); |
| 7386 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 7387 | // If this is a function definition, check if we have to apply optnone due to |
| 7388 | // a pragma. |
| 7389 | if(D.isFunctionDefinition()) |
| 7390 | AddRangeBasedOptnone(NewFD); |
| 7391 | |
Richard Smith | aa4bc18 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 7392 | // If this is the first declaration of an extern C variable, update |
| 7393 | // the map of such variables. |
Rafael Espindola | 7693b32 | 2013-10-19 02:13:21 +0000 | [diff] [blame] | 7394 | if (NewFD->isFirstDecl() && !NewFD->isInvalidDecl() && |
Richard Smith | aa4bc18 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 7395 | isIncompleteDeclExternC(*this, NewFD)) |
Richard Smith | 662f41b | 2013-06-18 20:15:12 +0000 | [diff] [blame] | 7396 | RegisterLocallyScopedExternCDecl(NewFD, S); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 7397 | |
Argyrios Kyrtzidis | 16f1930 | 2009-06-25 18:22:24 +0000 | [diff] [blame] | 7398 | // Set this FunctionDecl's range up to the right paren. |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 7399 | NewFD->setRangeEnd(D.getSourceRange().getEnd()); |
Argyrios Kyrtzidis | 16f1930 | 2009-06-25 18:22:24 +0000 | [diff] [blame] | 7400 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 7401 | if (D.isRedeclaration() && !Previous.empty()) { |
| 7402 | checkDLLAttributeRedeclaration( |
| 7403 | *this, dyn_cast<NamedDecl>(Previous.getRepresentativeDecl()), NewFD, |
| 7404 | isExplicitSpecialization || isFunctionTemplateSpecialization); |
| 7405 | } |
| 7406 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7407 | if (getLangOpts().CPlusPlus) { |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7408 | if (FunctionTemplate) { |
| 7409 | if (NewFD->isInvalidDecl()) |
| 7410 | FunctionTemplate->setInvalidDecl(); |
| 7411 | return FunctionTemplate; |
| 7412 | } |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7413 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7414 | |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 7415 | if (NewFD->hasAttr<OpenCLKernelAttr>()) { |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 7416 | // OpenCL v1.2 s6.8 static is invalid for kernel functions. |
| 7417 | if ((getLangOpts().OpenCLVersion >= 120) |
| 7418 | && (SC == SC_Static)) { |
| 7419 | Diag(D.getIdentifierLoc(), diag::err_static_kernel); |
| 7420 | D.setInvalidType(); |
| 7421 | } |
Tanya Lattner | 7564bcc | 2013-01-30 19:48:52 +0000 | [diff] [blame] | 7422 | |
| 7423 | // OpenCL v1.2, s6.9 -- Kernels can only have return type void. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 7424 | if (!NewFD->getReturnType()->isVoidType()) { |
Tanya Lattner | 7564bcc | 2013-01-30 19:48:52 +0000 | [diff] [blame] | 7425 | Diag(D.getIdentifierLoc(), |
| 7426 | diag::err_expected_kernel_void_return_type); |
| 7427 | D.setInvalidType(); |
| 7428 | } |
Matt Arsenault | e6c8afc | 2013-07-23 01:23:36 +0000 | [diff] [blame] | 7429 | |
| 7430 | llvm::SmallPtrSet<const Type *, 16> ValidTypes; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 7431 | for (auto Param : NewFD->params()) |
Matt Arsenault | e6c8afc | 2013-07-23 01:23:36 +0000 | [diff] [blame] | 7432 | checkIsValidOpenCLKernelParameter(*this, D, Param, ValidTypes); |
Tanya Lattner | 5e94d6f | 2012-06-19 23:09:52 +0000 | [diff] [blame] | 7433 | } |
| 7434 | |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 7435 | MarkUnusedFileScopedDecl(NewFD); |
Peter Collingbourne | 14b6ba7 | 2011-02-09 21:04:32 +0000 | [diff] [blame] | 7436 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7437 | if (getLangOpts().CUDA) |
Peter Collingbourne | 14b6ba7 | 2011-02-09 21:04:32 +0000 | [diff] [blame] | 7438 | if (IdentifierInfo *II = NewFD->getIdentifier()) |
| 7439 | if (!NewFD->isInvalidDecl() && |
| 7440 | NewFD->getDeclContext()->getRedeclContext()->isTranslationUnit()) { |
| 7441 | if (II->isStr("cudaConfigureCall")) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 7442 | if (!R->getAs<FunctionType>()->getReturnType()->isScalarType()) |
Peter Collingbourne | 14b6ba7 | 2011-02-09 21:04:32 +0000 | [diff] [blame] | 7443 | Diag(NewFD->getLocation(), diag::err_config_scalar_return); |
| 7444 | |
| 7445 | Context.setcudaConfigureCallDecl(NewFD); |
| 7446 | } |
| 7447 | } |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 7448 | |
| 7449 | // Here we have an function template explicit specialization at class scope. |
| 7450 | // The actually specialization will be postponed to template instatiation |
| 7451 | // time via the ClassScopeFunctionSpecializationDecl node. |
| 7452 | if (isDependentClassScopeExplicitSpecialization) { |
| 7453 | ClassScopeFunctionSpecializationDecl *NewSpec = |
| 7454 | ClassScopeFunctionSpecializationDecl::Create( |
Nico Weber | 6b02009 | 2012-06-25 17:21:05 +0000 | [diff] [blame] | 7455 | Context, CurContext, SourceLocation(), |
| 7456 | cast<CXXMethodDecl>(NewFD), |
| 7457 | HasExplicitTemplateArgs, TemplateArgs); |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 7458 | CurContext->addDecl(NewSpec); |
| 7459 | AddToScope = false; |
| 7460 | } |
Peter Collingbourne | 14b6ba7 | 2011-02-09 21:04:32 +0000 | [diff] [blame] | 7461 | |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 7462 | return NewFD; |
| 7463 | } |
| 7464 | |
| 7465 | /// \brief Perform semantic checking of a new function declaration. |
| 7466 | /// |
| 7467 | /// Performs semantic analysis of the new function declaration |
| 7468 | /// NewFD. This routine performs all semantic checking that does not |
| 7469 | /// require the actual declarator involved in the declaration, and is |
| 7470 | /// used both for the declaration of functions as they are parsed |
| 7471 | /// (called via ActOnDeclarator) and for the declaration of functions |
| 7472 | /// that have been instantiated via C++ template instantiation (called |
| 7473 | /// via InstantiateDecl). |
| 7474 | /// |
James Dennett | efce31f | 2012-06-22 08:10:18 +0000 | [diff] [blame] | 7475 | /// \param IsExplicitSpecialization whether this new function declaration is |
Douglas Gregor | fd056bc | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 7476 | /// an explicit specialization of the previous declaration. |
| 7477 | /// |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 7478 | /// This sets NewFD->isInvalidDecl() to true if there was an error. |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 7479 | /// |
James Dennett | efce31f | 2012-06-22 08:10:18 +0000 | [diff] [blame] | 7480 | /// \returns true if the function declaration is a redeclaration. |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 7481 | bool Sema::CheckFunctionDeclaration(Scope *S, FunctionDecl *NewFD, |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7482 | LookupResult &Previous, |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 7483 | bool IsExplicitSpecialization) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 7484 | assert(!NewFD->getReturnType()->isVariablyModifiedType() && |
| 7485 | "Variably modified return types are not handled here"); |
John McCall | 8c4859a | 2009-07-24 03:03:21 +0000 | [diff] [blame] | 7486 | |
Richard Smith | dd9459f | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 7487 | // Determine whether the type of this function should be merged with |
| 7488 | // a previous visible declaration. This never happens for functions in C++, |
| 7489 | // and always happens in C if the previous declaration was visible. |
| 7490 | bool MergeTypeWithPrevious = !getLangOpts().CPlusPlus && |
| 7491 | !Previous.isShadowed(); |
| 7492 | |
Douglas Gregor | 7dc80e1 | 2013-01-09 00:47:56 +0000 | [diff] [blame] | 7493 | // Filter out any non-conflicting previous declarations. |
| 7494 | filterNonConflictingPreviousDecls(Context, NewFD, Previous); |
| 7495 | |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 7496 | bool Redeclaration = false; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 7497 | NamedDecl *OldDecl = nullptr; |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 7498 | |
Douglas Gregor | 04495c8 | 2009-02-24 01:23:02 +0000 | [diff] [blame] | 7499 | // Merge or overload the declaration with an existing declaration of |
| 7500 | // the same name, if appropriate. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7501 | if (!Previous.empty()) { |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 7502 | // Determine whether NewFD is an overload of PrevDecl or |
Zhongxing Xu | 416fcaf | 2009-01-16 01:13:29 +0000 | [diff] [blame] | 7503 | // a declaration that requires merging. If it's an overload, |
| 7504 | // there's no more work to do here; we'll just add the new |
| 7505 | // function to the scope. |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 7506 | if (!AllowOverloadingOfFunction(Previous, Context)) { |
Rafael Espindola | 90cc390 | 2013-04-15 12:49:13 +0000 | [diff] [blame] | 7507 | NamedDecl *Candidate = Previous.getFoundDecl(); |
| 7508 | if (shouldLinkPossiblyHiddenDecl(Candidate, NewFD)) { |
| 7509 | Redeclaration = true; |
| 7510 | OldDecl = Candidate; |
| 7511 | } |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 7512 | } else { |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 7513 | switch (CheckOverload(S, NewFD, Previous, OldDecl, |
| 7514 | /*NewIsUsingDecl*/ false)) { |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 7515 | case Ovl_Match: |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7516 | Redeclaration = true; |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 7517 | break; |
| 7518 | |
| 7519 | case Ovl_NonFunction: |
| 7520 | Redeclaration = true; |
| 7521 | break; |
| 7522 | |
| 7523 | case Ovl_Overload: |
| 7524 | Redeclaration = false; |
| 7525 | break; |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7526 | } |
Peter Collingbourne | c80e811 | 2011-01-21 02:08:54 +0000 | [diff] [blame] | 7527 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7528 | if (!getLangOpts().CPlusPlus && !NewFD->hasAttr<OverloadableAttr>()) { |
Peter Collingbourne | c80e811 | 2011-01-21 02:08:54 +0000 | [diff] [blame] | 7529 | // If a function name is overloadable in C, then every function |
| 7530 | // with that name must be marked "overloadable". |
| 7531 | Diag(NewFD->getLocation(), diag::err_attribute_overloadable_missing) |
| 7532 | << Redeclaration << NewFD; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 7533 | NamedDecl *OverloadedDecl = nullptr; |
Peter Collingbourne | c80e811 | 2011-01-21 02:08:54 +0000 | [diff] [blame] | 7534 | if (Redeclaration) |
| 7535 | OverloadedDecl = OldDecl; |
| 7536 | else if (!Previous.empty()) |
| 7537 | OverloadedDecl = Previous.getRepresentativeDecl(); |
| 7538 | if (OverloadedDecl) |
| 7539 | Diag(OverloadedDecl->getLocation(), |
| 7540 | diag::note_attribute_overloadable_prev_overload); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 7541 | NewFD->addAttr(OverloadableAttr::CreateImplicit(Context)); |
Peter Collingbourne | c80e811 | 2011-01-21 02:08:54 +0000 | [diff] [blame] | 7542 | } |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7543 | } |
Richard Smith | 21c8fa8 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 7544 | } |
Zhongxing Xu | 416fcaf | 2009-01-16 01:13:29 +0000 | [diff] [blame] | 7545 | |
Richard Smith | aa4bc18 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 7546 | // Check for a previous extern "C" declaration with this name. |
| 7547 | if (!Redeclaration && |
| 7548 | checkForConflictWithNonVisibleExternC(*this, NewFD, Previous)) { |
| 7549 | filterNonConflictingPreviousDecls(Context, NewFD, Previous); |
| 7550 | if (!Previous.empty()) { |
| 7551 | // This is an extern "C" declaration with the same name as a previous |
| 7552 | // declaration, and thus redeclares that entity... |
| 7553 | Redeclaration = true; |
| 7554 | OldDecl = Previous.getFoundDecl(); |
Richard Smith | dd9459f | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 7555 | MergeTypeWithPrevious = false; |
Richard Smith | aa4bc18 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 7556 | |
| 7557 | // ... except in the presence of __attribute__((overloadable)). |
| 7558 | if (OldDecl->hasAttr<OverloadableAttr>()) { |
| 7559 | if (!getLangOpts().CPlusPlus && !NewFD->hasAttr<OverloadableAttr>()) { |
| 7560 | Diag(NewFD->getLocation(), diag::err_attribute_overloadable_missing) |
| 7561 | << Redeclaration << NewFD; |
| 7562 | Diag(Previous.getFoundDecl()->getLocation(), |
| 7563 | diag::note_attribute_overloadable_prev_overload); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 7564 | NewFD->addAttr(OverloadableAttr::CreateImplicit(Context)); |
Richard Smith | aa4bc18 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 7565 | } |
| 7566 | if (IsOverload(NewFD, cast<FunctionDecl>(OldDecl), false)) { |
| 7567 | Redeclaration = false; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 7568 | OldDecl = nullptr; |
Richard Smith | aa4bc18 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 7569 | } |
| 7570 | } |
| 7571 | } |
| 7572 | } |
| 7573 | |
Richard Smith | 21c8fa8 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 7574 | // C++11 [dcl.constexpr]p8: |
| 7575 | // A constexpr specifier for a non-static member function that is not |
| 7576 | // a constructor declares that member function to be const. |
| 7577 | // |
| 7578 | // This needs to be delayed until we know whether this is an out-of-line |
| 7579 | // definition of a static member function. |
Richard Smith | 8404626 | 2013-04-21 01:08:50 +0000 | [diff] [blame] | 7580 | // |
| 7581 | // This rule is not present in C++1y, so we produce a backwards |
| 7582 | // compatibility warning whenever it happens in C++11. |
Richard Smith | 21c8fa8 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 7583 | CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(NewFD); |
Richard Smith | 8404626 | 2013-04-21 01:08:50 +0000 | [diff] [blame] | 7584 | if (!getLangOpts().CPlusPlus1y && MD && MD->isConstexpr() && |
| 7585 | !MD->isStatic() && !isa<CXXConstructorDecl>(MD) && |
Richard Smith | 21c8fa8 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 7586 | (MD->getTypeQualifiers() & Qualifiers::Const) == 0) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 7587 | CXXMethodDecl *OldMD = nullptr; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 7588 | if (OldDecl) |
| 7589 | OldMD = dyn_cast<CXXMethodDecl>(OldDecl->getAsFunction()); |
Richard Smith | 21c8fa8 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 7590 | if (!OldMD || !OldMD->isStatic()) { |
| 7591 | const FunctionProtoType *FPT = |
| 7592 | MD->getType()->castAs<FunctionProtoType>(); |
| 7593 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 7594 | EPI.TypeQuals |= Qualifiers::Const; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 7595 | MD->setType(Context.getFunctionType(FPT->getReturnType(), |
| 7596 | FPT->getParamTypes(), EPI)); |
Richard Smith | 8404626 | 2013-04-21 01:08:50 +0000 | [diff] [blame] | 7597 | |
| 7598 | // Warn that we did this, if we're not performing template instantiation. |
| 7599 | // In that case, we'll have warned already when the template was defined. |
| 7600 | if (ActiveTemplateInstantiations.empty()) { |
| 7601 | SourceLocation AddConstLoc; |
| 7602 | if (FunctionTypeLoc FTL = MD->getTypeSourceInfo()->getTypeLoc() |
| 7603 | .IgnoreParens().getAs<FunctionTypeLoc>()) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 7604 | AddConstLoc = getLocForEndOfToken(FTL.getRParenLoc()); |
Richard Smith | 8404626 | 2013-04-21 01:08:50 +0000 | [diff] [blame] | 7605 | |
| 7606 | Diag(MD->getLocation(), diag::warn_cxx1y_compat_constexpr_not_const) |
| 7607 | << FixItHint::CreateInsertion(AddConstLoc, " const"); |
| 7608 | } |
Richard Smith | 21c8fa8 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 7609 | } |
| 7610 | } |
| 7611 | |
| 7612 | if (Redeclaration) { |
| 7613 | // NewFD and OldDecl represent declarations that need to be |
| 7614 | // merged. |
Richard Smith | dd9459f | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 7615 | if (MergeFunctionDecl(NewFD, OldDecl, S, MergeTypeWithPrevious)) { |
Richard Smith | 21c8fa8 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 7616 | NewFD->setInvalidDecl(); |
| 7617 | return Redeclaration; |
| 7618 | } |
| 7619 | |
| 7620 | Previous.clear(); |
| 7621 | Previous.addDecl(OldDecl); |
| 7622 | |
| 7623 | if (FunctionTemplateDecl *OldTemplateDecl |
| 7624 | = dyn_cast<FunctionTemplateDecl>(OldDecl)) { |
| 7625 | NewFD->setPreviousDeclaration(OldTemplateDecl->getTemplatedDecl()); |
| 7626 | FunctionTemplateDecl *NewTemplateDecl |
| 7627 | = NewFD->getDescribedFunctionTemplate(); |
| 7628 | assert(NewTemplateDecl && "Template/non-template mismatch"); |
| 7629 | if (CXXMethodDecl *Method |
| 7630 | = dyn_cast<CXXMethodDecl>(NewTemplateDecl->getTemplatedDecl())) { |
| 7631 | Method->setAccess(OldTemplateDecl->getAccess()); |
| 7632 | NewTemplateDecl->setAccess(OldTemplateDecl->getAccess()); |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 7633 | } |
Richard Smith | 21c8fa8 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 7634 | |
| 7635 | // If this is an explicit specialization of a member that is a function |
| 7636 | // template, mark it as a member specialization. |
| 7637 | if (IsExplicitSpecialization && |
| 7638 | NewTemplateDecl->getInstantiatedFromMemberTemplate()) { |
| 7639 | NewTemplateDecl->setMemberSpecialization(); |
| 7640 | assert(OldTemplateDecl->isMemberSpecialization()); |
Argyrios Kyrtzidis | 9bedef6 | 2009-07-14 03:18:53 +0000 | [diff] [blame] | 7641 | } |
Richard Smith | 21c8fa8 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 7642 | |
| 7643 | } else { |
John McCall | d5617ee | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 7644 | // This needs to happen first so that 'inline' propagates. |
Richard Smith | 21c8fa8 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 7645 | NewFD->setPreviousDeclaration(cast<FunctionDecl>(OldDecl)); |
John McCall | d5617ee | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 7646 | |
| 7647 | if (isa<CXXMethodDecl>(NewFD)) { |
| 7648 | // A valid redeclaration of a C++ method must be out-of-line, |
| 7649 | // but (unfortunately) it's not necessarily a definition |
| 7650 | // because of templates, which means that the previous |
| 7651 | // declaration is not necessarily from the class definition. |
| 7652 | |
| 7653 | // For just setting the access, that doesn't matter. |
| 7654 | CXXMethodDecl *oldMethod = cast<CXXMethodDecl>(OldDecl); |
| 7655 | NewFD->setAccess(oldMethod->getAccess()); |
| 7656 | |
| 7657 | // Update the key-function state if necessary for this ABI. |
| 7658 | if (NewFD->isInlined() && |
| 7659 | !Context.getTargetInfo().getCXXABI().canKeyFunctionBeInline()) { |
| 7660 | // setNonKeyFunction needs to work with the original |
| 7661 | // declaration from the class definition, and isVirtual() is |
| 7662 | // just faster in that case, so map back to that now. |
Rafael Espindola | bc65091 | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 7663 | oldMethod = cast<CXXMethodDecl>(oldMethod->getFirstDecl()); |
John McCall | d5617ee | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 7664 | if (oldMethod->isVirtual()) { |
| 7665 | Context.setNonKeyFunction(oldMethod); |
| 7666 | } |
| 7667 | } |
| 7668 | } |
Zhongxing Xu | 416fcaf | 2009-01-16 01:13:29 +0000 | [diff] [blame] | 7669 | } |
Douglas Gregor | 4ce205f | 2009-02-06 17:46:57 +0000 | [diff] [blame] | 7670 | } |
Zhongxing Xu | 416fcaf | 2009-01-16 01:13:29 +0000 | [diff] [blame] | 7671 | |
Anders Carlsson | 2c59d3c | 2009-09-13 21:33:06 +0000 | [diff] [blame] | 7672 | // Semantic checking for this function declaration (in isolation). |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7673 | if (getLangOpts().CPlusPlus) { |
Anders Carlsson | 2c59d3c | 2009-09-13 21:33:06 +0000 | [diff] [blame] | 7674 | // C++-specific checks. |
| 7675 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(NewFD)) { |
| 7676 | CheckConstructor(Constructor); |
Anders Carlsson | 6d70139 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 7677 | } else if (CXXDestructorDecl *Destructor = |
| 7678 | dyn_cast<CXXDestructorDecl>(NewFD)) { |
| 7679 | CXXRecordDecl *Record = Destructor->getParent(); |
Anders Carlsson | 2c59d3c | 2009-09-13 21:33:06 +0000 | [diff] [blame] | 7680 | QualType ClassType = Context.getTypeDeclType(Record); |
Anders Carlsson | 6d70139 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 7681 | |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 7682 | // FIXME: Shouldn't we be able to perform this check even when the class |
Anders Carlsson | 6d70139 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 7683 | // type is dependent? Both gcc and edg can handle that. |
Anders Carlsson | 2c59d3c | 2009-09-13 21:33:06 +0000 | [diff] [blame] | 7684 | if (!ClassType->isDependentType()) { |
| 7685 | DeclarationName Name |
| 7686 | = Context.DeclarationNames.getCXXDestructorName( |
| 7687 | Context.getCanonicalType(ClassType)); |
| 7688 | if (NewFD->getDeclName() != Name) { |
| 7689 | Diag(NewFD->getLocation(), diag::err_destructor_name); |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 7690 | NewFD->setInvalidDecl(); |
| 7691 | return Redeclaration; |
Anders Carlsson | 2c59d3c | 2009-09-13 21:33:06 +0000 | [diff] [blame] | 7692 | } |
| 7693 | } |
Anders Carlsson | 2c59d3c | 2009-09-13 21:33:06 +0000 | [diff] [blame] | 7694 | } else if (CXXConversionDecl *Conversion |
Douglas Gregor | 4ba3136 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 7695 | = dyn_cast<CXXConversionDecl>(NewFD)) { |
Anders Carlsson | 2c59d3c | 2009-09-13 21:33:06 +0000 | [diff] [blame] | 7696 | ActOnConversionDeclarator(Conversion); |
Douglas Gregor | 4ba3136 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 7697 | } |
| 7698 | |
| 7699 | // Find any virtual functions that this function overrides. |
Douglas Gregor | e6342c0 | 2009-12-01 17:35:23 +0000 | [diff] [blame] | 7700 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(NewFD)) { |
| 7701 | if (!Method->isFunctionTemplateSpecialization() && |
Argyrios Kyrtzidis | 38eb1e1 | 2012-10-09 01:23:45 +0000 | [diff] [blame] | 7702 | !Method->getDescribedFunctionTemplate() && |
| 7703 | Method->isCanonicalDecl()) { |
Douglas Gregor | a6c1e3a | 2010-10-13 22:55:32 +0000 | [diff] [blame] | 7704 | if (AddOverriddenMethods(Method->getParent(), Method)) { |
| 7705 | // If the function was marked as "static", we have a problem. |
| 7706 | if (NewFD->getStorageClass() == SC_Static) { |
David Blaikie | 5708c18 | 2012-10-17 00:47:58 +0000 | [diff] [blame] | 7707 | ReportOverrides(*this, diag::err_static_overrides_virtual, Method); |
Douglas Gregor | a6c1e3a | 2010-10-13 22:55:32 +0000 | [diff] [blame] | 7708 | } |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 7709 | } |
Douglas Gregor | a6c1e3a | 2010-10-13 22:55:32 +0000 | [diff] [blame] | 7710 | } |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 7711 | |
| 7712 | if (Method->isStatic()) |
| 7713 | checkThisInStaticMemberFunctionType(Method); |
Douglas Gregor | e6342c0 | 2009-12-01 17:35:23 +0000 | [diff] [blame] | 7714 | } |
Anders Carlsson | 2c59d3c | 2009-09-13 21:33:06 +0000 | [diff] [blame] | 7715 | |
| 7716 | // Extra checking for C++ overloaded operators (C++ [over.oper]). |
| 7717 | if (NewFD->isOverloadedOperator() && |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 7718 | CheckOverloadedOperatorDeclaration(NewFD)) { |
| 7719 | NewFD->setInvalidDecl(); |
| 7720 | return Redeclaration; |
| 7721 | } |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 7722 | |
| 7723 | // Extra checking for C++0x literal operators (C++0x [over.literal]). |
| 7724 | if (NewFD->getLiteralIdentifier() && |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 7725 | CheckLiteralOperatorDeclaration(NewFD)) { |
| 7726 | NewFD->setInvalidDecl(); |
| 7727 | return Redeclaration; |
| 7728 | } |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 7729 | |
Anders Carlsson | 2c59d3c | 2009-09-13 21:33:06 +0000 | [diff] [blame] | 7730 | // In C++, check default arguments now that we have merged decls. Unless |
| 7731 | // the lexical context is the class, because in this case this is done |
| 7732 | // during delayed parsing anyway. |
| 7733 | if (!CurContext->isRecord()) |
| 7734 | CheckCXXDefaultArguments(NewFD); |
Warren Hunt | 2d023ec | 2013-11-01 23:46:51 +0000 | [diff] [blame] | 7735 | |
Douglas Gregor | b68e399 | 2010-12-21 19:47:46 +0000 | [diff] [blame] | 7736 | // If this function declares a builtin function, check the type of this |
| 7737 | // declaration against the expected type for the builtin. |
| 7738 | if (unsigned BuiltinID = NewFD->getBuiltinID()) { |
| 7739 | ASTContext::GetBuiltinTypeError Error; |
Fariborz Jahanian | 9ef1518 | 2013-01-05 21:54:55 +0000 | [diff] [blame] | 7740 | LookupPredefedObjCSuperType(*this, S, NewFD->getIdentifier()); |
Douglas Gregor | b68e399 | 2010-12-21 19:47:46 +0000 | [diff] [blame] | 7741 | QualType T = Context.GetBuiltinType(BuiltinID, Error); |
| 7742 | if (!T.isNull() && !Context.hasSameType(T, NewFD->getType())) { |
| 7743 | // The type of this function differs from the type of the builtin, |
| 7744 | // so forget about the builtin entirely. |
| 7745 | Context.BuiltinInfo.ForgetBuiltin(BuiltinID, Context.Idents); |
| 7746 | } |
| 7747 | } |
Warren Hunt | 2d023ec | 2013-11-01 23:46:51 +0000 | [diff] [blame] | 7748 | |
Aaron Ballman | 2c0bf24 | 2012-02-09 01:21:34 +0000 | [diff] [blame] | 7749 | // If this function is declared as being extern "C", then check to see if |
| 7750 | // the function returns a UDT (class, struct, or union type) that is not C |
| 7751 | // compatible, and if it does, warn the user. |
Fariborz Jahanian | 96db329 | 2013-03-14 23:09:00 +0000 | [diff] [blame] | 7752 | // But, issue any diagnostic on the first declaration only. |
| 7753 | if (NewFD->isExternC() && Previous.empty()) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 7754 | QualType R = NewFD->getReturnType(); |
Hans Wennborg | 168c07b | 2012-07-24 17:59:41 +0000 | [diff] [blame] | 7755 | if (R->isIncompleteType() && !R->isVoidType()) |
| 7756 | Diag(NewFD->getLocation(), diag::warn_return_value_udt_incomplete) |
| 7757 | << NewFD << R; |
Douglas Gregor | b38b491 | 2012-08-07 06:14:34 +0000 | [diff] [blame] | 7758 | else if (!R.isPODType(Context) && !R->isVoidType() && |
| 7759 | !R->isObjCObjectPointerType()) |
Hans Wennborg | 168c07b | 2012-07-24 17:59:41 +0000 | [diff] [blame] | 7760 | Diag(NewFD->getLocation(), diag::warn_return_value_udt) << NewFD << R; |
Aaron Ballman | 2c0bf24 | 2012-02-09 01:21:34 +0000 | [diff] [blame] | 7761 | } |
Anders Carlsson | 2c59d3c | 2009-09-13 21:33:06 +0000 | [diff] [blame] | 7762 | } |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 7763 | return Redeclaration; |
Zhongxing Xu | 416fcaf | 2009-01-16 01:13:29 +0000 | [diff] [blame] | 7764 | } |
| 7765 | |
Dmitri Gribenko | a6f9707 | 2013-01-17 00:26:13 +0000 | [diff] [blame] | 7766 | static SourceRange getResultSourceRange(const FunctionDecl *FD) { |
| 7767 | const TypeSourceInfo *TSI = FD->getTypeSourceInfo(); |
| 7768 | if (!TSI) |
| 7769 | return SourceRange(); |
| 7770 | |
| 7771 | TypeLoc TL = TSI->getTypeLoc(); |
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 7772 | FunctionTypeLoc FunctionTL = TL.getAs<FunctionTypeLoc>(); |
Dmitri Gribenko | a6f9707 | 2013-01-17 00:26:13 +0000 | [diff] [blame] | 7773 | if (!FunctionTL) |
| 7774 | return SourceRange(); |
| 7775 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 7776 | TypeLoc ResultTL = FunctionTL.getReturnLoc(); |
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 7777 | if (ResultTL.getUnqualifiedLoc().getAs<BuiltinTypeLoc>()) |
Dmitri Gribenko | a6f9707 | 2013-01-17 00:26:13 +0000 | [diff] [blame] | 7778 | return ResultTL.getSourceRange(); |
| 7779 | |
| 7780 | return SourceRange(); |
| 7781 | } |
| 7782 | |
David Blaikie | 14068e8 | 2011-09-08 06:33:04 +0000 | [diff] [blame] | 7783 | void Sema::CheckMain(FunctionDecl* FD, const DeclSpec& DS) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 7784 | // C++11 [basic.start.main]p3: |
| 7785 | // A program that [...] declares main to be inline, static or |
| 7786 | // constexpr is ill-formed. |
Richard Smith | de03c15 | 2013-01-17 22:16:11 +0000 | [diff] [blame] | 7787 | // C11 6.7.4p4: In a hosted environment, no function specifier(s) shall |
| 7788 | // appear in a declaration of main. |
John McCall | 13591ed | 2009-07-25 04:36:53 +0000 | [diff] [blame] | 7789 | // static main is not an error under C99, but we should warn about it. |
Richard Smith | de03c15 | 2013-01-17 22:16:11 +0000 | [diff] [blame] | 7790 | // We accept _Noreturn main as an extension. |
David Blaikie | 14068e8 | 2011-09-08 06:33:04 +0000 | [diff] [blame] | 7791 | if (FD->getStorageClass() == SC_Static) |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7792 | Diag(DS.getStorageClassSpecLoc(), getLangOpts().CPlusPlus |
David Blaikie | 14068e8 | 2011-09-08 06:33:04 +0000 | [diff] [blame] | 7793 | ? diag::err_static_main : diag::warn_static_main) |
| 7794 | << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc()); |
| 7795 | if (FD->isInlineSpecified()) |
| 7796 | Diag(DS.getInlineSpecLoc(), diag::err_inline_main) |
| 7797 | << FixItHint::CreateRemoval(DS.getInlineSpecLoc()); |
Dmitri Gribenko | 445743d | 2013-01-21 11:25:03 +0000 | [diff] [blame] | 7798 | if (DS.isNoreturnSpecified()) { |
| 7799 | SourceLocation NoreturnLoc = DS.getNoreturnSpecLoc(); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 7800 | SourceRange NoreturnRange(NoreturnLoc, getLocForEndOfToken(NoreturnLoc)); |
Dmitri Gribenko | 445743d | 2013-01-21 11:25:03 +0000 | [diff] [blame] | 7801 | Diag(NoreturnLoc, diag::ext_noreturn_main); |
| 7802 | Diag(NoreturnLoc, diag::note_main_remove_noreturn) |
| 7803 | << FixItHint::CreateRemoval(NoreturnRange); |
| 7804 | } |
Richard Smith | a506586 | 2012-02-04 06:10:17 +0000 | [diff] [blame] | 7805 | if (FD->isConstexpr()) { |
| 7806 | Diag(DS.getConstexprSpecLoc(), diag::err_constexpr_main) |
| 7807 | << FixItHint::CreateRemoval(DS.getConstexprSpecLoc()); |
| 7808 | FD->setConstexpr(false); |
| 7809 | } |
John McCall | 13591ed | 2009-07-25 04:36:53 +0000 | [diff] [blame] | 7810 | |
Joey Gouly | c879fe5 | 2013-11-05 12:30:39 +0000 | [diff] [blame] | 7811 | if (getLangOpts().OpenCL) { |
| 7812 | Diag(FD->getLocation(), diag::err_opencl_no_main) |
| 7813 | << FD->hasAttr<OpenCLKernelAttr>(); |
| 7814 | FD->setInvalidDecl(); |
| 7815 | return; |
| 7816 | } |
| 7817 | |
John McCall | 13591ed | 2009-07-25 04:36:53 +0000 | [diff] [blame] | 7818 | QualType T = FD->getType(); |
| 7819 | assert(T->isFunctionType() && "function decl is not of function type"); |
John McCall | 75d8ba3 | 2012-02-14 19:50:52 +0000 | [diff] [blame] | 7820 | const FunctionType* FT = T->castAs<FunctionType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7821 | |
John McCall | 75d8ba3 | 2012-02-14 19:50:52 +0000 | [diff] [blame] | 7822 | // All the standards say that main() should should return 'int'. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 7823 | if (Context.hasSameUnqualifiedType(FT->getReturnType(), Context.IntTy)) { |
John McCall | 75d8ba3 | 2012-02-14 19:50:52 +0000 | [diff] [blame] | 7824 | // In C and C++, main magically returns 0 if you fall off the end; |
| 7825 | // set the flag which tells us that. |
| 7826 | // This is C++ [basic.start.main]p5 and C99 5.1.2.2.3. |
| 7827 | FD->setHasImplicitReturnZero(true); |
| 7828 | |
| 7829 | // In C with GNU extensions we allow main() to have non-integer return |
| 7830 | // type, but we should warn about the extension, and we disable the |
| 7831 | // implicit-return-zero rule. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7832 | } else if (getLangOpts().GNUMode && !getLangOpts().CPlusPlus) { |
John McCall | 75d8ba3 | 2012-02-14 19:50:52 +0000 | [diff] [blame] | 7833 | Diag(FD->getTypeSpecStartLoc(), diag::ext_main_returns_nonint); |
| 7834 | |
Dmitri Gribenko | a6f9707 | 2013-01-17 00:26:13 +0000 | [diff] [blame] | 7835 | SourceRange ResultRange = getResultSourceRange(FD); |
| 7836 | if (ResultRange.isValid()) |
| 7837 | Diag(ResultRange.getBegin(), diag::note_main_change_return_type) |
| 7838 | << FixItHint::CreateReplacement(ResultRange, "int"); |
| 7839 | |
John McCall | 75d8ba3 | 2012-02-14 19:50:52 +0000 | [diff] [blame] | 7840 | // Otherwise, this is just a flat-out error. |
| 7841 | } else { |
Dmitri Gribenko | a6f9707 | 2013-01-17 00:26:13 +0000 | [diff] [blame] | 7842 | SourceRange ResultRange = getResultSourceRange(FD); |
| 7843 | if (ResultRange.isValid()) |
| 7844 | Diag(FD->getTypeSpecStartLoc(), diag::err_main_returns_nonint) |
| 7845 | << FixItHint::CreateReplacement(ResultRange, "int"); |
| 7846 | else |
| 7847 | Diag(FD->getTypeSpecStartLoc(), diag::err_main_returns_nonint); |
| 7848 | |
John McCall | 13591ed | 2009-07-25 04:36:53 +0000 | [diff] [blame] | 7849 | FD->setInvalidDecl(true); |
| 7850 | } |
| 7851 | |
| 7852 | // Treat protoless main() as nullary. |
| 7853 | if (isa<FunctionNoProtoType>(FT)) return; |
| 7854 | |
| 7855 | const FunctionProtoType* FTP = cast<const FunctionProtoType>(FT); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 7856 | unsigned nparams = FTP->getNumParams(); |
John McCall | 13591ed | 2009-07-25 04:36:53 +0000 | [diff] [blame] | 7857 | assert(FD->getNumParams() == nparams); |
| 7858 | |
John McCall | 6675586 | 2009-12-24 09:58:38 +0000 | [diff] [blame] | 7859 | bool HasExtraParameters = (nparams > 3); |
| 7860 | |
| 7861 | // Darwin passes an undocumented fourth argument of type char**. If |
| 7862 | // other platforms start sprouting these, the logic below will start |
| 7863 | // getting shifty. |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 7864 | if (nparams == 4 && Context.getTargetInfo().getTriple().isOSDarwin()) |
John McCall | 6675586 | 2009-12-24 09:58:38 +0000 | [diff] [blame] | 7865 | HasExtraParameters = false; |
| 7866 | |
| 7867 | if (HasExtraParameters) { |
John McCall | 13591ed | 2009-07-25 04:36:53 +0000 | [diff] [blame] | 7868 | Diag(FD->getLocation(), diag::err_main_surplus_args) << nparams; |
| 7869 | FD->setInvalidDecl(true); |
| 7870 | nparams = 3; |
| 7871 | } |
| 7872 | |
| 7873 | // FIXME: a lot of the following diagnostics would be improved |
| 7874 | // if we had some location information about types. |
| 7875 | |
| 7876 | QualType CharPP = |
| 7877 | Context.getPointerType(Context.getPointerType(Context.CharTy)); |
John McCall | 6675586 | 2009-12-24 09:58:38 +0000 | [diff] [blame] | 7878 | QualType Expected[] = { Context.IntTy, CharPP, CharPP, CharPP }; |
John McCall | 13591ed | 2009-07-25 04:36:53 +0000 | [diff] [blame] | 7879 | |
| 7880 | for (unsigned i = 0; i < nparams; ++i) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 7881 | QualType AT = FTP->getParamType(i); |
John McCall | 13591ed | 2009-07-25 04:36:53 +0000 | [diff] [blame] | 7882 | |
| 7883 | bool mismatch = true; |
| 7884 | |
| 7885 | if (Context.hasSameUnqualifiedType(AT, Expected[i])) |
| 7886 | mismatch = false; |
| 7887 | else if (Expected[i] == CharPP) { |
| 7888 | // As an extension, the following forms are okay: |
| 7889 | // char const ** |
| 7890 | // char const * const * |
| 7891 | // char * const * |
| 7892 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 7893 | QualifierCollector qs; |
John McCall | 13591ed | 2009-07-25 04:36:53 +0000 | [diff] [blame] | 7894 | const PointerType* PT; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 7895 | if ((PT = qs.strip(AT)->getAs<PointerType>()) && |
| 7896 | (PT = qs.strip(PT->getPointeeType())->getAs<PointerType>()) && |
Richard Smith | 485b312 | 2013-01-29 02:49:47 +0000 | [diff] [blame] | 7897 | Context.hasSameType(QualType(qs.strip(PT->getPointeeType()), 0), |
| 7898 | Context.CharTy)) { |
John McCall | 13591ed | 2009-07-25 04:36:53 +0000 | [diff] [blame] | 7899 | qs.removeConst(); |
| 7900 | mismatch = !qs.empty(); |
| 7901 | } |
| 7902 | } |
| 7903 | |
| 7904 | if (mismatch) { |
| 7905 | Diag(FD->getLocation(), diag::err_main_arg_wrong) << i << Expected[i]; |
| 7906 | // TODO: suggest replacing given type with expected type |
| 7907 | FD->setInvalidDecl(true); |
| 7908 | } |
| 7909 | } |
| 7910 | |
| 7911 | if (nparams == 1 && !FD->isInvalidDecl()) { |
| 7912 | Diag(FD->getLocation(), diag::warn_main_one_arg); |
| 7913 | } |
Douglas Gregor | 0bab54c | 2010-10-21 16:57:46 +0000 | [diff] [blame] | 7914 | |
| 7915 | if (!FD->isInvalidDecl() && FD->getDescribedFunctionTemplate()) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 7916 | Diag(FD->getLocation(), diag::err_mainlike_template_decl) << FD; |
David Majnemer | e9f6f33 | 2013-09-16 22:44:20 +0000 | [diff] [blame] | 7917 | FD->setInvalidDecl(); |
| 7918 | } |
| 7919 | } |
| 7920 | |
| 7921 | void Sema::CheckMSVCRTEntryPoint(FunctionDecl *FD) { |
| 7922 | QualType T = FD->getType(); |
| 7923 | assert(T->isFunctionType() && "function decl is not of function type"); |
| 7924 | const FunctionType *FT = T->castAs<FunctionType>(); |
| 7925 | |
| 7926 | // Set an implicit return of 'zero' if the function can return some integral, |
| 7927 | // enumeration, pointer or nullptr type. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 7928 | if (FT->getReturnType()->isIntegralOrEnumerationType() || |
| 7929 | FT->getReturnType()->isAnyPointerType() || |
| 7930 | FT->getReturnType()->isNullPtrType()) |
David Majnemer | e9f6f33 | 2013-09-16 22:44:20 +0000 | [diff] [blame] | 7931 | // DllMain is exempt because a return value of zero means it failed. |
| 7932 | if (FD->getName() != "DllMain") |
| 7933 | FD->setHasImplicitReturnZero(true); |
| 7934 | |
| 7935 | if (!FD->isInvalidDecl() && FD->getDescribedFunctionTemplate()) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 7936 | Diag(FD->getLocation(), diag::err_mainlike_template_decl) << FD; |
Douglas Gregor | 0bab54c | 2010-10-21 16:57:46 +0000 | [diff] [blame] | 7937 | FD->setInvalidDecl(); |
| 7938 | } |
John McCall | 8c4859a | 2009-07-24 03:03:21 +0000 | [diff] [blame] | 7939 | } |
| 7940 | |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 7941 | bool Sema::CheckForConstantInitializer(Expr *Init, QualType DclT) { |
Eli Friedman | 3b8a36a | 2009-02-27 04:17:12 +0000 | [diff] [blame] | 7942 | // FIXME: Need strict checking. In C89, we need to check for |
| 7943 | // any assignment, increment, decrement, function-calls, or |
| 7944 | // commas outside of a sizeof. In C99, it's the same list, |
| 7945 | // except that the aforementioned are allowed in unevaluated |
| 7946 | // expressions. Everything else falls under the |
| 7947 | // "may accept other forms of constant expressions" exception. |
| 7948 | // (We never end up here for C++, so the constant expression |
| 7949 | // rules there don't matter.) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 7950 | const Expr *Culprit; |
| 7951 | if (Init->isConstantInitializer(Context, false, &Culprit)) |
Eli Friedman | 578a972 | 2009-02-22 06:45:27 +0000 | [diff] [blame] | 7952 | return false; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 7953 | Diag(Culprit->getExprLoc(), diag::err_init_element_not_constant) |
| 7954 | << Culprit->getSourceRange(); |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 7955 | return true; |
Steve Naroff | d0091aa | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 7956 | } |
| 7957 | |
Chandler Carruth | a7689ef | 2011-03-27 09:46:56 +0000 | [diff] [blame] | 7958 | namespace { |
| 7959 | // Visits an initialization expression to see if OrigDecl is evaluated in |
| 7960 | // its own initialization and throws a warning if it does. |
| 7961 | class SelfReferenceChecker |
| 7962 | : public EvaluatedExprVisitor<SelfReferenceChecker> { |
| 7963 | Sema &S; |
| 7964 | Decl *OrigDecl; |
Richard Trieu | 898267f | 2011-09-01 21:44:13 +0000 | [diff] [blame] | 7965 | bool isRecordType; |
| 7966 | bool isPODType; |
Hans Wennborg | 8be9e77 | 2012-08-17 10:12:33 +0000 | [diff] [blame] | 7967 | bool isReferenceType; |
Chandler Carruth | a7689ef | 2011-03-27 09:46:56 +0000 | [diff] [blame] | 7968 | |
| 7969 | public: |
| 7970 | typedef EvaluatedExprVisitor<SelfReferenceChecker> Inherited; |
| 7971 | |
| 7972 | SelfReferenceChecker(Sema &S, Decl *OrigDecl) : Inherited(S.Context), |
Richard Trieu | 898267f | 2011-09-01 21:44:13 +0000 | [diff] [blame] | 7973 | S(S), OrigDecl(OrigDecl) { |
| 7974 | isPODType = false; |
| 7975 | isRecordType = false; |
Hans Wennborg | 8be9e77 | 2012-08-17 10:12:33 +0000 | [diff] [blame] | 7976 | isReferenceType = false; |
Richard Trieu | 898267f | 2011-09-01 21:44:13 +0000 | [diff] [blame] | 7977 | if (ValueDecl *VD = dyn_cast<ValueDecl>(OrigDecl)) { |
| 7978 | isPODType = VD->getType().isPODType(S.Context); |
| 7979 | isRecordType = VD->getType()->isRecordType(); |
Hans Wennborg | 8be9e77 | 2012-08-17 10:12:33 +0000 | [diff] [blame] | 7980 | isReferenceType = VD->getType()->isReferenceType(); |
Richard Trieu | 898267f | 2011-09-01 21:44:13 +0000 | [diff] [blame] | 7981 | } |
| 7982 | } |
Chandler Carruth | a7689ef | 2011-03-27 09:46:56 +0000 | [diff] [blame] | 7983 | |
Richard Trieu | 7e9f8af | 2012-05-09 00:21:34 +0000 | [diff] [blame] | 7984 | // For most expressions, the cast is directly above the DeclRefExpr. |
| 7985 | // For conditional operators, the cast can be outside the conditional |
| 7986 | // operator if both expressions are DeclRefExpr's. |
| 7987 | void HandleValue(Expr *E) { |
Richard Trieu | 568f785 | 2012-10-01 17:39:51 +0000 | [diff] [blame] | 7988 | if (isReferenceType) |
| 7989 | return; |
Richard Trieu | 7e9f8af | 2012-05-09 00:21:34 +0000 | [diff] [blame] | 7990 | E = E->IgnoreParenImpCasts(); |
| 7991 | if (DeclRefExpr* DRE = dyn_cast<DeclRefExpr>(E)) { |
| 7992 | HandleDeclRefExpr(DRE); |
| 7993 | return; |
| 7994 | } |
| 7995 | |
| 7996 | if (ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E)) { |
| 7997 | HandleValue(CO->getTrueExpr()); |
| 7998 | HandleValue(CO->getFalseExpr()); |
Richard Trieu | 6b2cc42 | 2012-10-03 00:41:36 +0000 | [diff] [blame] | 7999 | return; |
| 8000 | } |
| 8001 | |
| 8002 | if (isa<MemberExpr>(E)) { |
| 8003 | Expr *Base = E->IgnoreParenImpCasts(); |
| 8004 | while (MemberExpr *ME = dyn_cast<MemberExpr>(Base)) { |
| 8005 | // Check for static member variables and don't warn on them. |
| 8006 | if (!isa<FieldDecl>(ME->getMemberDecl())) |
| 8007 | return; |
| 8008 | Base = ME->getBase()->IgnoreParenImpCasts(); |
| 8009 | } |
| 8010 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Base)) |
| 8011 | HandleDeclRefExpr(DRE); |
| 8012 | return; |
Richard Trieu | 7e9f8af | 2012-05-09 00:21:34 +0000 | [diff] [blame] | 8013 | } |
| 8014 | } |
| 8015 | |
Richard Trieu | 568f785 | 2012-10-01 17:39:51 +0000 | [diff] [blame] | 8016 | // Reference types are handled here since all uses of references are |
| 8017 | // bad, not just r-value uses. |
| 8018 | void VisitDeclRefExpr(DeclRefExpr *E) { |
| 8019 | if (isReferenceType) |
| 8020 | HandleDeclRefExpr(E); |
| 8021 | } |
| 8022 | |
Richard Trieu | 7e9f8af | 2012-05-09 00:21:34 +0000 | [diff] [blame] | 8023 | void VisitImplicitCastExpr(ImplicitCastExpr *E) { |
Richard Trieu | 6b2cc42 | 2012-10-03 00:41:36 +0000 | [diff] [blame] | 8024 | if (E->getCastKind() == CK_LValueToRValue || |
Richard Trieu | 7e9f8af | 2012-05-09 00:21:34 +0000 | [diff] [blame] | 8025 | (isRecordType && E->getCastKind() == CK_NoOp)) |
| 8026 | HandleValue(E->getSubExpr()); |
| 8027 | |
| 8028 | Inherited::VisitImplicitCastExpr(E); |
Chandler Carruth | a7689ef | 2011-03-27 09:46:56 +0000 | [diff] [blame] | 8029 | } |
| 8030 | |
Richard Trieu | 898267f | 2011-09-01 21:44:13 +0000 | [diff] [blame] | 8031 | void VisitMemberExpr(MemberExpr *E) { |
Richard Trieu | 7e9f8af | 2012-05-09 00:21:34 +0000 | [diff] [blame] | 8032 | // Don't warn on arrays since they can be treated as pointers. |
Richard Trieu | 47eb898 | 2011-09-07 00:58:53 +0000 | [diff] [blame] | 8033 | if (E->getType()->canDecayToPointerType()) return; |
Richard Trieu | 7e9f8af | 2012-05-09 00:21:34 +0000 | [diff] [blame] | 8034 | |
Richard Trieu | 6b2cc42 | 2012-10-03 00:41:36 +0000 | [diff] [blame] | 8035 | // Warn when a non-static method call is followed by non-static member |
| 8036 | // field accesses, which is followed by a DeclRefExpr. |
| 8037 | CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(E->getMemberDecl()); |
| 8038 | bool Warn = (MD && !MD->isStatic()); |
| 8039 | Expr *Base = E->getBase()->IgnoreParenImpCasts(); |
| 8040 | while (MemberExpr *ME = dyn_cast<MemberExpr>(Base)) { |
| 8041 | if (!isa<FieldDecl>(ME->getMemberDecl())) |
| 8042 | Warn = false; |
| 8043 | Base = ME->getBase()->IgnoreParenImpCasts(); |
| 8044 | } |
Richard Trieu | 898267f | 2011-09-01 21:44:13 +0000 | [diff] [blame] | 8045 | |
Richard Trieu | 6b2cc42 | 2012-10-03 00:41:36 +0000 | [diff] [blame] | 8046 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Base)) { |
| 8047 | if (Warn) |
| 8048 | HandleDeclRefExpr(DRE); |
| 8049 | return; |
| 8050 | } |
| 8051 | |
| 8052 | // The base of a MemberExpr is not a MemberExpr or a DeclRefExpr. |
| 8053 | // Visit that expression. |
| 8054 | Visit(Base); |
Chandler Carruth | a7689ef | 2011-03-27 09:46:56 +0000 | [diff] [blame] | 8055 | } |
| 8056 | |
Richard Trieu | 8af742a | 2013-03-26 03:41:40 +0000 | [diff] [blame] | 8057 | void VisitCXXOperatorCallExpr(CXXOperatorCallExpr *E) { |
| 8058 | if (E->getNumArgs() > 0) |
| 8059 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E->getArg(0))) |
| 8060 | HandleDeclRefExpr(DRE); |
| 8061 | |
| 8062 | Inherited::VisitCXXOperatorCallExpr(E); |
| 8063 | } |
| 8064 | |
Richard Trieu | 898267f | 2011-09-01 21:44:13 +0000 | [diff] [blame] | 8065 | void VisitUnaryOperator(UnaryOperator *E) { |
| 8066 | // For POD record types, addresses of its own members are well-defined. |
Richard Trieu | 6b2cc42 | 2012-10-03 00:41:36 +0000 | [diff] [blame] | 8067 | if (E->getOpcode() == UO_AddrOf && isRecordType && |
| 8068 | isa<MemberExpr>(E->getSubExpr()->IgnoreParens())) { |
| 8069 | if (!isPODType) |
| 8070 | HandleValue(E->getSubExpr()); |
| 8071 | return; |
| 8072 | } |
Richard Trieu | 898267f | 2011-09-01 21:44:13 +0000 | [diff] [blame] | 8073 | Inherited::VisitUnaryOperator(E); |
Richard Smith | 0f2fc5f | 2013-05-03 19:16:22 +0000 | [diff] [blame] | 8074 | } |
Richard Trieu | 7e9f8af | 2012-05-09 00:21:34 +0000 | [diff] [blame] | 8075 | |
| 8076 | void VisitObjCMessageExpr(ObjCMessageExpr *E) { return; } |
| 8077 | |
Richard Trieu | 898267f | 2011-09-01 21:44:13 +0000 | [diff] [blame] | 8078 | void HandleDeclRefExpr(DeclRefExpr *DRE) { |
NAKAMURA Takumi | f305279 | 2013-01-19 01:54:35 +0000 | [diff] [blame] | 8079 | Decl* ReferenceDecl = DRE->getDecl(); |
Chandler Carruth | a7689ef | 2011-03-27 09:46:56 +0000 | [diff] [blame] | 8080 | if (OrigDecl != ReferenceDecl) return; |
Ted Kremenek | 39371b8 | 2013-01-19 04:33:14 +0000 | [diff] [blame] | 8081 | unsigned diag; |
| 8082 | if (isReferenceType) { |
| 8083 | diag = diag::warn_uninit_self_reference_in_reference_init; |
| 8084 | } else if (cast<VarDecl>(OrigDecl)->isStaticLocal()) { |
| 8085 | diag = diag::warn_static_self_reference_in_init; |
| 8086 | } else { |
| 8087 | diag = diag::warn_uninit_self_reference_in_init; |
| 8088 | } |
| 8089 | |
Richard Trieu | 898267f | 2011-09-01 21:44:13 +0000 | [diff] [blame] | 8090 | S.DiagRuntimeBehavior(DRE->getLocStart(), DRE, |
Hans Wennborg | 5965b7c | 2012-08-20 08:52:22 +0000 | [diff] [blame] | 8091 | S.PDiag(diag) |
Hans Wennborg | 7821e07 | 2012-09-21 08:58:33 +0000 | [diff] [blame] | 8092 | << DRE->getNameInfo().getName() |
Douglas Gregor | 63fe681 | 2011-05-24 16:02:01 +0000 | [diff] [blame] | 8093 | << OrigDecl->getLocation() |
Richard Trieu | 898267f | 2011-09-01 21:44:13 +0000 | [diff] [blame] | 8094 | << DRE->getSourceRange()); |
Chandler Carruth | a7689ef | 2011-03-27 09:46:56 +0000 | [diff] [blame] | 8095 | } |
| 8096 | }; |
Chandler Carruth | a7689ef | 2011-03-27 09:46:56 +0000 | [diff] [blame] | 8097 | |
Richard Trieu | 568f785 | 2012-10-01 17:39:51 +0000 | [diff] [blame] | 8098 | /// CheckSelfReference - Warns if OrigDecl is used in expression E. |
| 8099 | static void CheckSelfReference(Sema &S, Decl* OrigDecl, Expr *E, |
| 8100 | bool DirectInit) { |
| 8101 | // Parameters arguments are occassionially constructed with itself, |
| 8102 | // for instance, in recursive functions. Skip them. |
| 8103 | if (isa<ParmVarDecl>(OrigDecl)) |
| 8104 | return; |
| 8105 | |
| 8106 | E = E->IgnoreParens(); |
| 8107 | |
| 8108 | // Skip checking T a = a where T is not a record or reference type. |
| 8109 | // Doing so is a way to silence uninitialized warnings. |
| 8110 | if (!DirectInit && !cast<VarDecl>(OrigDecl)->getType()->isRecordType()) |
| 8111 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) |
| 8112 | if (ICE->getCastKind() == CK_LValueToRValue) |
| 8113 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr())) |
| 8114 | if (DRE->getDecl() == OrigDecl) |
| 8115 | return; |
| 8116 | |
| 8117 | SelfReferenceChecker(S, OrigDecl).Visit(E); |
| 8118 | } |
Richard Trieu | 898267f | 2011-09-01 21:44:13 +0000 | [diff] [blame] | 8119 | } |
| 8120 | |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 8121 | /// AddInitializerToDecl - Adds the initializer Init to the |
| 8122 | /// declaration dcl. If DirectInit is true, this is C++ direct |
| 8123 | /// initialization rather than copy initialization. |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 8124 | void Sema::AddInitializerToDecl(Decl *RealDecl, Expr *Init, |
| 8125 | bool DirectInit, bool TypeMayContainAuto) { |
Chris Lattner | 9a11b9a | 2007-10-19 20:10:30 +0000 | [diff] [blame] | 8126 | // If there is no declaration, there was an error parsing it. Just ignore |
| 8127 | // the initializer. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 8128 | if (!RealDecl || RealDecl->isInvalidDecl()) |
Chris Lattner | 9a11b9a | 2007-10-19 20:10:30 +0000 | [diff] [blame] | 8129 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8130 | |
Douglas Gregor | 021c3b3 | 2009-03-11 23:00:04 +0000 | [diff] [blame] | 8131 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(RealDecl)) { |
| 8132 | // With declarators parsed the way they are, the parser cannot |
| 8133 | // distinguish between a normal initializer and a pure-specifier. |
| 8134 | // Thus this grotesque test. |
| 8135 | IntegerLiteral *IL; |
Douglas Gregor | 021c3b3 | 2009-03-11 23:00:04 +0000 | [diff] [blame] | 8136 | if ((IL = dyn_cast<IntegerLiteral>(Init)) && IL->getValue() == 0 && |
Douglas Gregor | 4ba3136 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 8137 | Context.getCanonicalType(IL->getType()) == Context.IntTy) |
| 8138 | CheckPureMethod(Method, Init->getSourceRange()); |
| 8139 | else { |
Douglas Gregor | 021c3b3 | 2009-03-11 23:00:04 +0000 | [diff] [blame] | 8140 | Diag(Method->getLocation(), diag::err_member_function_initialization) |
| 8141 | << Method->getDeclName() << Init->getSourceRange(); |
| 8142 | Method->setInvalidDecl(); |
| 8143 | } |
| 8144 | return; |
| 8145 | } |
| 8146 | |
Steve Naroff | 410e3e2 | 2007-09-12 20:13:48 +0000 | [diff] [blame] | 8147 | VarDecl *VDecl = dyn_cast<VarDecl>(RealDecl); |
| 8148 | if (!VDecl) { |
Richard Smith | c2cdd53 | 2011-06-12 11:43:46 +0000 | [diff] [blame] | 8149 | assert(!isa<FieldDecl>(RealDecl) && "field init shouldn't get here"); |
| 8150 | Diag(RealDecl->getLocation(), diag::err_illegal_initializer); |
Steve Naroff | 410e3e2 | 2007-09-12 20:13:48 +0000 | [diff] [blame] | 8151 | RealDecl->setInvalidDecl(); |
| 8152 | return; |
Eli Friedman | 3b8a36a | 2009-02-27 04:17:12 +0000 | [diff] [blame] | 8153 | } |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 8154 | ParenListExpr *CXXDirectInit = dyn_cast<ParenListExpr>(Init); |
| 8155 | |
Richard Smith | 0188872 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8156 | // C++11 [decl.spec.auto]p6. Deduce the type which 'auto' stands in for. |
Richard Smith | dc7a4f5 | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 8157 | if (TypeMayContainAuto && VDecl->getType()->isUndeducedType()) { |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 8158 | Expr *DeduceInit = Init; |
| 8159 | // Initializer could be a C++ direct-initializer. Deduction only works if it |
| 8160 | // contains exactly one expression. |
| 8161 | if (CXXDirectInit) { |
| 8162 | if (CXXDirectInit->getNumExprs() == 0) { |
| 8163 | // It isn't possible to write this directly, but it is possible to |
| 8164 | // end up in this situation with "auto x(some_pack...);" |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 8165 | Diag(CXXDirectInit->getLocStart(), |
Richard Smith | 04fa7a3 | 2013-09-28 04:02:39 +0000 | [diff] [blame] | 8166 | VDecl->isInitCapture() ? diag::err_init_capture_no_expression |
| 8167 | : diag::err_auto_var_init_no_expression) |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 8168 | << VDecl->getDeclName() << VDecl->getType() |
| 8169 | << VDecl->getSourceRange(); |
| 8170 | RealDecl->setInvalidDecl(); |
| 8171 | return; |
| 8172 | } else if (CXXDirectInit->getNumExprs() > 1) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 8173 | Diag(CXXDirectInit->getExpr(1)->getLocStart(), |
Richard Smith | 04fa7a3 | 2013-09-28 04:02:39 +0000 | [diff] [blame] | 8174 | VDecl->isInitCapture() |
| 8175 | ? diag::err_init_capture_multiple_expressions |
| 8176 | : diag::err_auto_var_init_multiple_expressions) |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 8177 | << VDecl->getDeclName() << VDecl->getType() |
| 8178 | << VDecl->getSourceRange(); |
| 8179 | RealDecl->setInvalidDecl(); |
| 8180 | return; |
| 8181 | } else { |
| 8182 | DeduceInit = CXXDirectInit->getExpr(0); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 8183 | if (isa<InitListExpr>(DeduceInit)) |
| 8184 | Diag(CXXDirectInit->getLocStart(), |
| 8185 | diag::err_auto_var_init_paren_braces) |
| 8186 | << VDecl->getDeclName() << VDecl->getType() |
| 8187 | << VDecl->getSourceRange(); |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 8188 | } |
| 8189 | } |
Douglas Gregor | 1344e94 | 2013-03-07 22:57:58 +0000 | [diff] [blame] | 8190 | |
| 8191 | // Expressions default to 'id' when we're in a debugger. |
| 8192 | bool DefaultedToAuto = false; |
| 8193 | if (getLangOpts().DebuggerCastResultToId && |
| 8194 | Init->getType() == Context.UnknownAnyTy) { |
| 8195 | ExprResult Result = forceUnknownAnyToType(Init, Context.getObjCIdType()); |
| 8196 | if (Result.isInvalid()) { |
| 8197 | VDecl->setInvalidDecl(); |
| 8198 | return; |
| 8199 | } |
| 8200 | Init = Result.take(); |
| 8201 | DefaultedToAuto = true; |
| 8202 | } |
Richard Smith | 9b13175 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 8203 | |
| 8204 | QualType DeducedType; |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 8205 | if (DeduceAutoType(VDecl->getTypeSourceInfo(), DeduceInit, DeducedType) == |
Sebastian Redl | b832f6d | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 8206 | DAR_Failed) |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 8207 | DiagnoseAutoDeductionFailure(VDecl, DeduceInit); |
Richard Smith | 9b13175 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 8208 | if (DeducedType.isNull()) { |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 8209 | RealDecl->setInvalidDecl(); |
| 8210 | return; |
| 8211 | } |
Richard Smith | 9b13175 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 8212 | VDecl->setType(DeducedType); |
Rafael Espindola | 2d1b096 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 8213 | assert(VDecl->isLinkageValid()); |
Rafael Espindola | 2d9e883 | 2013-03-12 21:06:00 +0000 | [diff] [blame] | 8214 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8215 | // In ARC, infer lifetime. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8216 | if (getLangOpts().ObjCAutoRefCount && inferObjCARCLifetime(VDecl)) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8217 | VDecl->setInvalidDecl(); |
| 8218 | |
Jordan Rose | 0abbdfe | 2012-06-08 22:46:07 +0000 | [diff] [blame] | 8219 | // Warn if we deduced 'id'. 'auto' usually implies type-safety, but using |
| 8220 | // 'id' instead of a specific object type prevents most of our usual checks. |
| 8221 | // We only want to warn outside of template instantiations, though: |
| 8222 | // inside a template, the 'id' could have come from a parameter. |
Douglas Gregor | 1344e94 | 2013-03-07 22:57:58 +0000 | [diff] [blame] | 8223 | if (ActiveTemplateInstantiations.empty() && !DefaultedToAuto && |
Richard Smith | 9b13175 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 8224 | DeducedType->isObjCIdType()) { |
| 8225 | SourceLocation Loc = |
| 8226 | VDecl->getTypeSourceInfo()->getTypeLoc().getBeginLoc(); |
Jordan Rose | 0abbdfe | 2012-06-08 22:46:07 +0000 | [diff] [blame] | 8227 | Diag(Loc, diag::warn_auto_var_is_id) |
| 8228 | << VDecl->getDeclName() << DeduceInit->getSourceRange(); |
| 8229 | } |
| 8230 | |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 8231 | // If this is a redeclaration, check that the type we just deduced matches |
| 8232 | // the previously declared type. |
Richard Smith | dd9459f | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 8233 | if (VarDecl *Old = VDecl->getPreviousDecl()) { |
| 8234 | // We never need to merge the type, because we cannot form an incomplete |
| 8235 | // array of auto, nor deduce such a type. |
| 8236 | MergeVarDeclTypes(VDecl, Old, /*MergeTypeWithPrevious*/false); |
| 8237 | } |
Richard Smith | dc7a4f5 | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 8238 | |
| 8239 | // Check the deduced type is valid for a variable declaration. |
| 8240 | CheckVariableDeclarationType(VDecl); |
| 8241 | if (VDecl->isInvalidDecl()) |
| 8242 | return; |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 8243 | } |
Richard Smith | 0188872 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8244 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 8245 | // dllimport cannot be used on variable definitions. |
| 8246 | if (VDecl->hasAttr<DLLImportAttr>() && !VDecl->isStaticDataMember()) { |
| 8247 | Diag(VDecl->getLocation(), diag::err_attribute_dllimport_data_definition); |
| 8248 | VDecl->setInvalidDecl(); |
| 8249 | return; |
| 8250 | } |
| 8251 | |
Richard Smith | 0188872 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8252 | if (VDecl->isLocalVarDecl() && VDecl->hasExternalStorage()) { |
| 8253 | // C99 6.7.8p5. C++ has no such restriction, but that is a defect. |
| 8254 | Diag(VDecl->getLocation(), diag::err_block_extern_cant_init); |
| 8255 | VDecl->setInvalidDecl(); |
| 8256 | return; |
| 8257 | } |
| 8258 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 8259 | if (!VDecl->getType()->isDependentType()) { |
| 8260 | // A definition must end up with a complete type, which means it must be |
| 8261 | // complete with the restriction that an array type might be completed by |
| 8262 | // the initializer; note that later code assumes this restriction. |
| 8263 | QualType BaseDeclType = VDecl->getType(); |
| 8264 | if (const ArrayType *Array = Context.getAsIncompleteArrayType(BaseDeclType)) |
| 8265 | BaseDeclType = Array->getElementType(); |
| 8266 | if (RequireCompleteType(VDecl->getLocation(), BaseDeclType, |
| 8267 | diag::err_typecheck_decl_incomplete_type)) { |
| 8268 | RealDecl->setInvalidDecl(); |
| 8269 | return; |
| 8270 | } |
Douglas Gregor | 3a91abf | 2010-08-24 05:27:49 +0000 | [diff] [blame] | 8271 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 8272 | // The variable can not have an abstract class type. |
| 8273 | if (RequireNonAbstractType(VDecl->getLocation(), VDecl->getType(), |
| 8274 | diag::err_abstract_type_in_decl, |
| 8275 | AbstractVariableType)) |
| 8276 | VDecl->setInvalidDecl(); |
Eli Friedman | a31feca | 2009-04-13 21:28:54 +0000 | [diff] [blame] | 8277 | } |
| 8278 | |
Sebastian Redl | 31310a2 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 8279 | const VarDecl *Def; |
| 8280 | if ((Def = VDecl->getDefinition()) && Def != VDecl) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8281 | Diag(VDecl->getLocation(), diag::err_redefinition) |
Douglas Gregor | 275a369 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 8282 | << VDecl->getDeclName(); |
| 8283 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 8284 | VDecl->setInvalidDecl(); |
| 8285 | return; |
| 8286 | } |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 8287 | |
| 8288 | const VarDecl *PrevInit = nullptr; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8289 | if (getLangOpts().CPlusPlus) { |
Douglas Gregor | a31040f | 2010-12-16 01:31:22 +0000 | [diff] [blame] | 8290 | // C++ [class.static.data]p4 |
| 8291 | // If a static data member is of const integral or const |
| 8292 | // enumeration type, its declaration in the class definition can |
| 8293 | // specify a constant-initializer which shall be an integral |
| 8294 | // constant expression (5.19). In that case, the member can appear |
| 8295 | // in integral constant expressions. The member shall still be |
| 8296 | // defined in a namespace scope if it is used in the program and the |
| 8297 | // namespace scope definition shall not contain an initializer. |
| 8298 | // |
| 8299 | // We already performed a redefinition check above, but for static |
| 8300 | // data members we also need to check whether there was an in-class |
| 8301 | // declaration with an initializer. |
| 8302 | if (VDecl->isStaticDataMember() && VDecl->getAnyInitializer(PrevInit)) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 8303 | Diag(Init->getExprLoc(), diag::err_static_data_member_reinitialization) |
| 8304 | << VDecl->getDeclName(); |
| 8305 | Diag(PrevInit->getInit()->getExprLoc(), diag::note_previous_initializer) << 0; |
Douglas Gregor | a31040f | 2010-12-16 01:31:22 +0000 | [diff] [blame] | 8306 | return; |
| 8307 | } |
Douglas Gregor | 275a369 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 8308 | |
Douglas Gregor | a31040f | 2010-12-16 01:31:22 +0000 | [diff] [blame] | 8309 | if (VDecl->hasLocalStorage()) |
| 8310 | getCurFunction()->setHasBranchProtectedScope(); |
| 8311 | |
| 8312 | if (DiagnoseUnexpandedParameterPack(Init, UPPC_Initializer)) { |
| 8313 | VDecl->setInvalidDecl(); |
| 8314 | return; |
| 8315 | } |
| 8316 | } |
John McCall | e46f62c | 2010-08-01 01:24:59 +0000 | [diff] [blame] | 8317 | |
Peter Collingbourne | 8c25fc5 | 2011-09-19 21:14:35 +0000 | [diff] [blame] | 8318 | // OpenCL 1.1 6.5.2: "Variables allocated in the __local address space inside |
| 8319 | // a kernel function cannot be initialized." |
| 8320 | if (VDecl->getStorageClass() == SC_OpenCLWorkGroupLocal) { |
| 8321 | Diag(VDecl->getLocation(), diag::err_local_cant_init); |
| 8322 | VDecl->setInvalidDecl(); |
| 8323 | return; |
| 8324 | } |
| 8325 | |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 8326 | // Get the decls type and save a reference for later, since |
Steve Naroff | d0091aa | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 8327 | // CheckInitializerTypes may change it. |
Steve Naroff | 410e3e2 | 2007-09-12 20:13:48 +0000 | [diff] [blame] | 8328 | QualType DclT = VDecl->getType(), SavT = DclT; |
Fariborz Jahanian | 509fb3e | 2012-03-09 18:47:16 +0000 | [diff] [blame] | 8329 | |
Douglas Gregor | 1344e94 | 2013-03-07 22:57:58 +0000 | [diff] [blame] | 8330 | // Expressions default to 'id' when we're in a debugger |
| 8331 | // and we are assigning it to a variable of Objective-C pointer type. |
| 8332 | if (getLangOpts().DebuggerCastResultToId && DclT->isObjCObjectPointerType() && |
| 8333 | Init->getType() == Context.UnknownAnyTy) { |
| 8334 | ExprResult Result = forceUnknownAnyToType(Init, Context.getObjCIdType()); |
| 8335 | if (Result.isInvalid()) { |
| 8336 | VDecl->setInvalidDecl(); |
| 8337 | return; |
Fariborz Jahanian | 509fb3e | 2012-03-09 18:47:16 +0000 | [diff] [blame] | 8338 | } |
Douglas Gregor | 1344e94 | 2013-03-07 22:57:58 +0000 | [diff] [blame] | 8339 | Init = Result.take(); |
| 8340 | } |
Richard Smith | 0188872 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8341 | |
| 8342 | // Perform the initialization. |
| 8343 | if (!VDecl->isInvalidDecl()) { |
| 8344 | InitializedEntity Entity = InitializedEntity::InitializeVariable(VDecl); |
| 8345 | InitializationKind Kind |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 8346 | = DirectInit ? |
| 8347 | CXXDirectInit ? InitializationKind::CreateDirect(VDecl->getLocation(), |
| 8348 | Init->getLocStart(), |
| 8349 | Init->getLocEnd()) |
| 8350 | : InitializationKind::CreateDirectList( |
| 8351 | VDecl->getLocation()) |
Richard Smith | 0188872 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8352 | : InitializationKind::CreateCopy(VDecl->getLocation(), |
| 8353 | Init->getLocStart()); |
| 8354 | |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 8355 | MultiExprArg Args = Init; |
| 8356 | if (CXXDirectInit) |
| 8357 | Args = MultiExprArg(CXXDirectInit->getExprs(), |
| 8358 | CXXDirectInit->getNumExprs()); |
| 8359 | |
| 8360 | InitializationSequence InitSeq(*this, Entity, Kind, Args); |
| 8361 | ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Args, &DclT); |
Richard Smith | 0188872 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8362 | if (Result.isInvalid()) { |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 8363 | VDecl->setInvalidDecl(); |
Richard Smith | 0188872 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8364 | return; |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 8365 | } |
Richard Smith | 0188872 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8366 | |
| 8367 | Init = Result.takeAs<Expr>(); |
| 8368 | } |
| 8369 | |
Richard Trieu | 568f785 | 2012-10-01 17:39:51 +0000 | [diff] [blame] | 8370 | // Check for self-references within variable initializers. |
| 8371 | // Variables declared within a function/method body (except for references) |
| 8372 | // are handled by a dataflow analysis. |
| 8373 | if (!VDecl->hasLocalStorage() || VDecl->getType()->isRecordType() || |
| 8374 | VDecl->getType()->isReferenceType()) { |
| 8375 | CheckSelfReference(*this, RealDecl, Init, DirectInit); |
| 8376 | } |
| 8377 | |
Richard Smith | 0188872 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8378 | // If the type changed, it means we had an incomplete type that was |
| 8379 | // completed by the initializer. For example: |
| 8380 | // int ary[] = { 1, 3, 5 }; |
John McCall | 7307643 | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 8381 | // "ary" transitions from an IncompleteArrayType to a ConstantArrayType. |
Eli Friedman | 5c89c39 | 2012-02-23 02:25:10 +0000 | [diff] [blame] | 8382 | if (!VDecl->isInvalidDecl() && (DclT != SavT)) |
Richard Smith | 0188872 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8383 | VDecl->setType(DclT); |
Richard Smith | 0188872 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8384 | |
Jordan Rose | e10f4d3 | 2012-09-15 02:48:31 +0000 | [diff] [blame] | 8385 | if (!VDecl->isInvalidDecl()) { |
Richard Smith | 0188872 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8386 | checkUnsafeAssigns(VDecl->getLocation(), VDecl->getType(), Init); |
| 8387 | |
Jordan Rose | e10f4d3 | 2012-09-15 02:48:31 +0000 | [diff] [blame] | 8388 | if (VDecl->hasAttr<BlocksAttr>()) |
| 8389 | checkRetainCycles(VDecl, Init); |
Jordan Rose | 58b6bdc | 2012-09-28 22:21:30 +0000 | [diff] [blame] | 8390 | |
| 8391 | // It is safe to assign a weak reference into a strong variable. |
| 8392 | // Although this code can still have problems: |
| 8393 | // id x = self.weakProp; |
| 8394 | // id y = self.weakProp; |
| 8395 | // we do not warn to warn spuriously when 'x' and 'y' are on separate |
| 8396 | // paths through the function. This should be revisited if |
| 8397 | // -Wrepeated-use-of-weak is made flow-sensitive. |
Ted Kremenek | 904a326 | 2012-12-20 22:31:27 +0000 | [diff] [blame] | 8398 | if (VDecl->getType().getObjCLifetime() == Qualifiers::OCL_Strong) { |
Jordan Rose | 58b6bdc | 2012-09-28 22:21:30 +0000 | [diff] [blame] | 8399 | DiagnosticsEngine::Level Level = |
| 8400 | Diags.getDiagnosticLevel(diag::warn_arc_repeated_use_of_weak, |
| 8401 | Init->getLocStart()); |
| 8402 | if (Level != DiagnosticsEngine::Ignored) |
| 8403 | getCurFunction()->markSafeWeakUse(Init); |
| 8404 | } |
Jordan Rose | e10f4d3 | 2012-09-15 02:48:31 +0000 | [diff] [blame] | 8405 | } |
| 8406 | |
Richard Smith | 4195637 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 8407 | // The initialization is usually a full-expression. |
| 8408 | // |
| 8409 | // FIXME: If this is a braced initialization of an aggregate, it is not |
| 8410 | // an expression, and each individual field initializer is a separate |
| 8411 | // full-expression. For instance, in: |
| 8412 | // |
| 8413 | // struct Temp { ~Temp(); }; |
| 8414 | // struct S { S(Temp); }; |
| 8415 | // struct T { S a, b; } t = { Temp(), Temp() } |
| 8416 | // |
| 8417 | // we should destroy the first Temp before constructing the second. |
Fariborz Jahanian | ad48a50 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 8418 | ExprResult Result = ActOnFinishFullExpr(Init, VDecl->getLocation(), |
| 8419 | false, |
| 8420 | VDecl->isConstexpr()); |
Richard Smith | 4195637 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 8421 | if (Result.isInvalid()) { |
| 8422 | VDecl->setInvalidDecl(); |
| 8423 | return; |
| 8424 | } |
| 8425 | Init = Result.take(); |
| 8426 | |
Richard Smith | 0188872 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8427 | // Attach the initializer to the decl. |
| 8428 | VDecl->setInit(Init); |
| 8429 | |
| 8430 | if (VDecl->isLocalVarDecl()) { |
| 8431 | // C99 6.7.8p4: All the expressions in an initializer for an object that has |
| 8432 | // static storage duration shall be constant expressions or string literals. |
| 8433 | // C++ does not have this restriction. |
Enea Zaffanella | b9a5935 | 2013-07-22 10:58:26 +0000 | [diff] [blame] | 8434 | if (!getLangOpts().CPlusPlus && !VDecl->isInvalidDecl()) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 8435 | const Expr *Culprit; |
Enea Zaffanella | b9a5935 | 2013-07-22 10:58:26 +0000 | [diff] [blame] | 8436 | if (VDecl->getStorageClass() == SC_Static) |
| 8437 | CheckForConstantInitializer(Init, DclT); |
| 8438 | // C89 is stricter than C99 for non-static aggregate types. |
| 8439 | // C89 6.5.7p3: All the expressions [...] in an initializer list |
| 8440 | // for an object that has aggregate or union type shall be |
| 8441 | // constant expressions. |
| 8442 | else if (!getLangOpts().C99 && VDecl->getType()->isAggregateType() && |
Enea Zaffanella | 8202630 | 2013-07-22 19:10:20 +0000 | [diff] [blame] | 8443 | isa<InitListExpr>(Init) && |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 8444 | !Init->isConstantInitializer(Context, false, &Culprit)) |
| 8445 | Diag(Culprit->getExprLoc(), |
Enea Zaffanella | b9a5935 | 2013-07-22 10:58:26 +0000 | [diff] [blame] | 8446 | diag::ext_aggregate_init_not_constant) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 8447 | << Culprit->getSourceRange(); |
Enea Zaffanella | b9a5935 | 2013-07-22 10:58:26 +0000 | [diff] [blame] | 8448 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8449 | } else if (VDecl->isStaticDataMember() && |
Douglas Gregor | 021c3b3 | 2009-03-11 23:00:04 +0000 | [diff] [blame] | 8450 | VDecl->getLexicalDeclContext()->isRecord()) { |
| 8451 | // This is an in-class initialization for a static data member, e.g., |
| 8452 | // |
| 8453 | // struct S { |
| 8454 | // static const int value = 17; |
| 8455 | // }; |
| 8456 | |
Douglas Gregor | 021c3b3 | 2009-03-11 23:00:04 +0000 | [diff] [blame] | 8457 | // C++ [class.mem]p4: |
| 8458 | // A member-declarator can contain a constant-initializer only |
| 8459 | // if it declares a static member (9.4) of const integral or |
| 8460 | // const enumeration type, see 9.4.2. |
Richard Smith | c6d990a | 2011-09-29 19:11:37 +0000 | [diff] [blame] | 8461 | // |
Richard Smith | 0188872 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8462 | // C++11 [class.static.data]p3: |
Richard Smith | c6d990a | 2011-09-29 19:11:37 +0000 | [diff] [blame] | 8463 | // If a non-volatile const static data member is of integral or |
| 8464 | // enumeration type, its declaration in the class definition can |
| 8465 | // specify a brace-or-equal-initializer in which every initalizer-clause |
| 8466 | // that is an assignment-expression is a constant expression. A static |
| 8467 | // data member of literal type can be declared in the class definition |
| 8468 | // with the constexpr specifier; if so, its declaration shall specify a |
| 8469 | // brace-or-equal-initializer in which every initializer-clause that is |
| 8470 | // an assignment-expression is a constant expression. |
John McCall | 4e63564 | 2010-09-10 23:21:22 +0000 | [diff] [blame] | 8471 | |
| 8472 | // Do nothing on dependent types. |
Richard Smith | 0188872 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8473 | if (DclT->isDependentType()) { |
John McCall | 4e63564 | 2010-09-10 23:21:22 +0000 | [diff] [blame] | 8474 | |
Richard Smith | c6d990a | 2011-09-29 19:11:37 +0000 | [diff] [blame] | 8475 | // Allow any 'static constexpr' members, whether or not they are of literal |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 8476 | // type. We separately check that every constexpr variable is of literal |
| 8477 | // type. |
Richard Smith | c6d990a | 2011-09-29 19:11:37 +0000 | [diff] [blame] | 8478 | } else if (VDecl->isConstexpr()) { |
| 8479 | |
John McCall | 4e63564 | 2010-09-10 23:21:22 +0000 | [diff] [blame] | 8480 | // Require constness. |
Richard Smith | 0188872 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8481 | } else if (!DclT.isConstQualified()) { |
John McCall | 4e63564 | 2010-09-10 23:21:22 +0000 | [diff] [blame] | 8482 | Diag(VDecl->getLocation(), diag::err_in_class_initializer_non_const) |
| 8483 | << Init->getSourceRange(); |
Douglas Gregor | 021c3b3 | 2009-03-11 23:00:04 +0000 | [diff] [blame] | 8484 | VDecl->setInvalidDecl(); |
John McCall | 4e63564 | 2010-09-10 23:21:22 +0000 | [diff] [blame] | 8485 | |
| 8486 | // We allow integer constant expressions in all cases. |
Richard Smith | 0188872 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8487 | } else if (DclT->isIntegralOrEnumerationType()) { |
Chris Lattner | 24c38e1 | 2011-06-14 05:46:29 +0000 | [diff] [blame] | 8488 | // Check whether the expression is a constant expression. |
| 8489 | SourceLocation Loc; |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 8490 | if (getLangOpts().CPlusPlus11 && DclT.isVolatileQualified()) |
Richard Smith | 0188872 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8491 | // In C++11, a non-constexpr const static data member with an |
Richard Smith | 2da7a51 | 2011-09-29 21:28:14 +0000 | [diff] [blame] | 8492 | // in-class initializer cannot be volatile. |
| 8493 | Diag(VDecl->getLocation(), diag::err_in_class_initializer_volatile); |
| 8494 | else if (Init->isValueDependent()) |
Chris Lattner | 24c38e1 | 2011-06-14 05:46:29 +0000 | [diff] [blame] | 8495 | ; // Nothing to check. |
| 8496 | else if (Init->isIntegerConstantExpr(Context, &Loc)) |
| 8497 | ; // Ok, it's an ICE! |
| 8498 | else if (Init->isEvaluatable(Context)) { |
| 8499 | // If we can constant fold the initializer through heroics, accept it, |
| 8500 | // but report this as a use of an extension for -pedantic. |
| 8501 | Diag(Loc, diag::ext_in_class_initializer_non_constant) |
| 8502 | << Init->getSourceRange(); |
| 8503 | } else { |
| 8504 | // Otherwise, this is some crazy unknown case. Report the issue at the |
| 8505 | // location provided by the isIntegerConstantExpr failed check. |
| 8506 | Diag(Loc, diag::err_in_class_initializer_non_constant) |
| 8507 | << Init->getSourceRange(); |
| 8508 | VDecl->setInvalidDecl(); |
John McCall | 4e63564 | 2010-09-10 23:21:22 +0000 | [diff] [blame] | 8509 | } |
| 8510 | |
Richard Smith | 0188872 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8511 | // We allow foldable floating-point constants as an extension. |
| 8512 | } else if (DclT->isFloatingType()) { // also permits complex, which is ok |
Richard Smith | b4b1d69 | 2013-01-25 04:22:16 +0000 | [diff] [blame] | 8513 | // In C++98, this is a GNU extension. In C++11, it is not, but we support |
| 8514 | // it anyway and provide a fixit to add the 'constexpr'. |
| 8515 | if (getLangOpts().CPlusPlus11) { |
David Blaikie | a367e9d | 2013-01-29 22:26:08 +0000 | [diff] [blame] | 8516 | Diag(VDecl->getLocation(), |
| 8517 | diag::ext_in_class_initializer_float_type_cxx11) |
| 8518 | << DclT << Init->getSourceRange(); |
| 8519 | Diag(VDecl->getLocStart(), |
| 8520 | diag::note_in_class_initializer_float_type_cxx11) |
| 8521 | << FixItHint::CreateInsertion(VDecl->getLocStart(), "constexpr "); |
Richard Smith | b4b1d69 | 2013-01-25 04:22:16 +0000 | [diff] [blame] | 8522 | } else { |
| 8523 | Diag(VDecl->getLocation(), diag::ext_in_class_initializer_float_type) |
| 8524 | << DclT << Init->getSourceRange(); |
John McCall | 4e63564 | 2010-09-10 23:21:22 +0000 | [diff] [blame] | 8525 | |
Richard Smith | b4b1d69 | 2013-01-25 04:22:16 +0000 | [diff] [blame] | 8526 | if (!Init->isValueDependent() && !Init->isEvaluatable(Context)) { |
| 8527 | Diag(Init->getExprLoc(), diag::err_in_class_initializer_non_constant) |
| 8528 | << Init->getSourceRange(); |
| 8529 | VDecl->setInvalidDecl(); |
| 8530 | } |
Douglas Gregor | 021c3b3 | 2009-03-11 23:00:04 +0000 | [diff] [blame] | 8531 | } |
Richard Smith | 947be19 | 2011-09-29 23:18:34 +0000 | [diff] [blame] | 8532 | |
Richard Smith | 0188872 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8533 | // Suggest adding 'constexpr' in C++11 for literal types. |
Richard Smith | a10b978 | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 8534 | } else if (getLangOpts().CPlusPlus11 && DclT->isLiteralType(Context)) { |
Richard Smith | 947be19 | 2011-09-29 23:18:34 +0000 | [diff] [blame] | 8535 | Diag(VDecl->getLocation(), diag::err_in_class_initializer_literal_type) |
Richard Smith | 0188872 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8536 | << DclT << Init->getSourceRange() |
Richard Smith | 947be19 | 2011-09-29 23:18:34 +0000 | [diff] [blame] | 8537 | << FixItHint::CreateInsertion(VDecl->getLocStart(), "constexpr "); |
| 8538 | VDecl->setConstexpr(true); |
| 8539 | |
Richard Smith | c6d990a | 2011-09-29 19:11:37 +0000 | [diff] [blame] | 8540 | } else { |
| 8541 | Diag(VDecl->getLocation(), diag::err_in_class_initializer_bad_type) |
Richard Smith | 0188872 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8542 | << DclT << Init->getSourceRange(); |
Richard Smith | c6d990a | 2011-09-29 19:11:37 +0000 | [diff] [blame] | 8543 | VDecl->setInvalidDecl(); |
Douglas Gregor | 021c3b3 | 2009-03-11 23:00:04 +0000 | [diff] [blame] | 8544 | } |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 8545 | } else if (VDecl->isFileVarDecl()) { |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 8546 | if (VDecl->getStorageClass() == SC_Extern && |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8547 | (!getLangOpts().CPlusPlus || |
Rafael Espindola | 5b34b9c | 2013-03-29 07:56:05 +0000 | [diff] [blame] | 8548 | !(Context.getBaseElementType(VDecl->getType()).isConstQualified() || |
Richard Smith | d0629eb | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 8549 | VDecl->isExternC())) && |
| 8550 | !isTemplateInstantiation(VDecl->getTemplateSpecializationKind())) |
Steve Naroff | 410e3e2 | 2007-09-12 20:13:48 +0000 | [diff] [blame] | 8551 | Diag(VDecl->getLocation(), diag::warn_extern_init); |
Eli Friedman | a91eb54 | 2009-12-22 02:10:53 +0000 | [diff] [blame] | 8552 | |
Richard Smith | 0188872 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8553 | // C99 6.7.8p4. All file scoped initializers need to be constant. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8554 | if (!getLangOpts().CPlusPlus && !VDecl->isInvalidDecl()) |
Anders Carlsson | c5eb731 | 2008-08-22 05:00:02 +0000 | [diff] [blame] | 8555 | CheckForConstantInitializer(Init, DclT); |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 8556 | } |
Douglas Gregor | b6c8c8b | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 8557 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 8558 | // We will represent direct-initialization similarly to copy-initialization: |
| 8559 | // int x(1); -as-> int x = 1; |
| 8560 | // ClassType x(a,b,c); -as-> ClassType x = ClassType(a,b,c); |
| 8561 | // |
| 8562 | // Clients that want to distinguish between the two forms, can check for |
| 8563 | // direct initializer using VarDecl::getInitStyle(). |
| 8564 | // A major benefit is that clients that don't particularly care about which |
| 8565 | // exactly form was it (like the CodeGen) can handle both cases without |
| 8566 | // special case code. |
| 8567 | |
| 8568 | // C++ 8.5p11: |
| 8569 | // The form of initialization (using parentheses or '=') is generally |
| 8570 | // insignificant, but does matter when the entity being initialized has a |
| 8571 | // class type. |
| 8572 | if (CXXDirectInit) { |
| 8573 | assert(DirectInit && "Call-style initializer must be direct init."); |
| 8574 | VDecl->setInitStyle(VarDecl::CallInit); |
| 8575 | } else if (DirectInit) { |
| 8576 | // This must be list-initialization. No other way is direct-initialization. |
| 8577 | VDecl->setInitStyle(VarDecl::ListInit); |
| 8578 | } |
| 8579 | |
John McCall | 2998d6b | 2011-01-19 11:48:09 +0000 | [diff] [blame] | 8580 | CheckCompleteVariableDeclaration(VDecl); |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 8581 | } |
| 8582 | |
John McCall | 7727acf | 2010-03-31 02:13:20 +0000 | [diff] [blame] | 8583 | /// ActOnInitializerError - Given that there was an error parsing an |
| 8584 | /// initializer for the given declaration, try to return to some form |
| 8585 | /// of sanity. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8586 | void Sema::ActOnInitializerError(Decl *D) { |
John McCall | 7727acf | 2010-03-31 02:13:20 +0000 | [diff] [blame] | 8587 | // Our main concern here is re-establishing invariants like "a |
| 8588 | // variable's type is either dependent or complete". |
John McCall | 7727acf | 2010-03-31 02:13:20 +0000 | [diff] [blame] | 8589 | if (!D || D->isInvalidDecl()) return; |
| 8590 | |
| 8591 | VarDecl *VD = dyn_cast<VarDecl>(D); |
| 8592 | if (!VD) return; |
| 8593 | |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 8594 | // Auto types are meaningless if we can't make sense of the initializer. |
Richard Smith | 483b9f3 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 8595 | if (ParsingInitForAutoVars.count(D)) { |
| 8596 | D->setInvalidDecl(); |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 8597 | return; |
| 8598 | } |
| 8599 | |
John McCall | 7727acf | 2010-03-31 02:13:20 +0000 | [diff] [blame] | 8600 | QualType Ty = VD->getType(); |
| 8601 | if (Ty->isDependentType()) return; |
| 8602 | |
| 8603 | // Require a complete type. |
| 8604 | if (RequireCompleteType(VD->getLocation(), |
| 8605 | Context.getBaseElementType(Ty), |
| 8606 | diag::err_typecheck_decl_incomplete_type)) { |
| 8607 | VD->setInvalidDecl(); |
| 8608 | return; |
| 8609 | } |
| 8610 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 8611 | // Require a non-abstract type. |
John McCall | 7727acf | 2010-03-31 02:13:20 +0000 | [diff] [blame] | 8612 | if (RequireNonAbstractType(VD->getLocation(), Ty, |
| 8613 | diag::err_abstract_type_in_decl, |
| 8614 | AbstractVariableType)) { |
| 8615 | VD->setInvalidDecl(); |
| 8616 | return; |
| 8617 | } |
| 8618 | |
| 8619 | // Don't bother complaining about constructors or destructors, |
| 8620 | // though. |
| 8621 | } |
| 8622 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8623 | void Sema::ActOnUninitializedDecl(Decl *RealDecl, |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 8624 | bool TypeMayContainAuto) { |
Argyrios Kyrtzidis | 48c2e90 | 2008-11-07 13:01:22 +0000 | [diff] [blame] | 8625 | // If there is no declaration, there was an error parsing it. Just ignore it. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 8626 | if (!RealDecl) |
Argyrios Kyrtzidis | 48c2e90 | 2008-11-07 13:01:22 +0000 | [diff] [blame] | 8627 | return; |
| 8628 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 8629 | if (VarDecl *Var = dyn_cast<VarDecl>(RealDecl)) { |
| 8630 | QualType Type = Var->getType(); |
Douglas Gregor | b6c8c8b | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 8631 | |
Richard Smith | dd4b350 | 2011-12-25 21:17:58 +0000 | [diff] [blame] | 8632 | // C++11 [dcl.spec.auto]p3 |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 8633 | if (TypeMayContainAuto && Type->getContainedAutoType()) { |
Anders Carlsson | 6a75cd9 | 2009-07-11 00:34:39 +0000 | [diff] [blame] | 8634 | Diag(Var->getLocation(), diag::err_auto_var_requires_init) |
| 8635 | << Var->getDeclName() << Type; |
| 8636 | Var->setInvalidDecl(); |
| 8637 | return; |
| 8638 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8639 | |
Richard Smith | dd4b350 | 2011-12-25 21:17:58 +0000 | [diff] [blame] | 8640 | // C++11 [class.static.data]p3: A static data member can be declared with |
Richard Smith | c6d990a | 2011-09-29 19:11:37 +0000 | [diff] [blame] | 8641 | // the constexpr specifier; if so, its declaration shall specify |
| 8642 | // a brace-or-equal-initializer. |
Richard Smith | dd4b350 | 2011-12-25 21:17:58 +0000 | [diff] [blame] | 8643 | // C++11 [dcl.constexpr]p1: The constexpr specifier shall be applied only to |
| 8644 | // the definition of a variable [...] or the declaration of a static data |
| 8645 | // member. |
| 8646 | if (Var->isConstexpr() && !Var->isThisDeclarationADefinition()) { |
| 8647 | if (Var->isStaticDataMember()) |
| 8648 | Diag(Var->getLocation(), |
| 8649 | diag::err_constexpr_static_mem_var_requires_init) |
| 8650 | << Var->getDeclName(); |
| 8651 | else |
| 8652 | Diag(Var->getLocation(), diag::err_invalid_constexpr_var_decl); |
Richard Smith | c6d990a | 2011-09-29 19:11:37 +0000 | [diff] [blame] | 8653 | Var->setInvalidDecl(); |
| 8654 | return; |
| 8655 | } |
| 8656 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 8657 | // OpenCL v1.1 s6.5.3: variables declared in the constant address space must |
| 8658 | // be initialized. |
| 8659 | if (!Var->isInvalidDecl() && |
| 8660 | Var->getType().getAddressSpace() == LangAS::opencl_constant && |
| 8661 | Var->getStorageClass() != SC_Extern && !Var->getInit()) { |
| 8662 | Diag(Var->getLocation(), diag::err_opencl_constant_no_init); |
| 8663 | Var->setInvalidDecl(); |
| 8664 | return; |
| 8665 | } |
| 8666 | |
Douglas Gregor | 60c93c9 | 2010-02-09 07:26:29 +0000 | [diff] [blame] | 8667 | switch (Var->isThisDeclarationADefinition()) { |
| 8668 | case VarDecl::Definition: |
| 8669 | if (!Var->isStaticDataMember() || !Var->getAnyInitializer()) |
| 8670 | break; |
| 8671 | |
| 8672 | // We have an out-of-line definition of a static data member |
| 8673 | // that has an in-class initializer, so we type-check this like |
| 8674 | // a declaration. |
| 8675 | // |
| 8676 | // Fall through |
| 8677 | |
| 8678 | case VarDecl::DeclarationOnly: |
| 8679 | // It's only a declaration. |
| 8680 | |
| 8681 | // Block scope. C99 6.7p7: If an identifier for an object is |
| 8682 | // declared with no linkage (C99 6.2.2p6), the type for the |
| 8683 | // object shall be complete. |
John McCall | b6bbcc9 | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 8684 | if (!Type->isDependentType() && Var->isLocalVarDecl() && |
Rafael Espindola | 181e3ec | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 8685 | !Var->hasLinkage() && !Var->isInvalidDecl() && |
Douglas Gregor | 60c93c9 | 2010-02-09 07:26:29 +0000 | [diff] [blame] | 8686 | RequireCompleteType(Var->getLocation(), Type, |
| 8687 | diag::err_typecheck_decl_incomplete_type)) |
| 8688 | Var->setInvalidDecl(); |
| 8689 | |
| 8690 | // Make sure that the type is not abstract. |
| 8691 | if (!Type->isDependentType() && !Var->isInvalidDecl() && |
| 8692 | RequireNonAbstractType(Var->getLocation(), Type, |
| 8693 | diag::err_abstract_type_in_decl, |
| 8694 | AbstractVariableType)) |
| 8695 | Var->setInvalidDecl(); |
Fariborz Jahanian | 4cc83c2 | 2012-08-15 18:42:26 +0000 | [diff] [blame] | 8696 | if (!Type->isDependentType() && !Var->isInvalidDecl() && |
Fariborz Jahanian | 767a1a2 | 2012-08-17 21:44:55 +0000 | [diff] [blame] | 8697 | Var->getStorageClass() == SC_PrivateExtern) { |
Fariborz Jahanian | 4cc83c2 | 2012-08-15 18:42:26 +0000 | [diff] [blame] | 8698 | Diag(Var->getLocation(), diag::warn_private_extern); |
Fariborz Jahanian | 767a1a2 | 2012-08-17 21:44:55 +0000 | [diff] [blame] | 8699 | Diag(Var->getLocation(), diag::note_private_extern); |
| 8700 | } |
Fariborz Jahanian | 4cc83c2 | 2012-08-15 18:42:26 +0000 | [diff] [blame] | 8701 | |
Douglas Gregor | 60c93c9 | 2010-02-09 07:26:29 +0000 | [diff] [blame] | 8702 | return; |
| 8703 | |
| 8704 | case VarDecl::TentativeDefinition: |
| 8705 | // File scope. C99 6.9.2p2: A declaration of an identifier for an |
| 8706 | // object that has file scope without an initializer, and without a |
| 8707 | // storage-class specifier or with the storage-class specifier "static", |
| 8708 | // constitutes a tentative definition. Note: A tentative definition with |
| 8709 | // external linkage is valid (C99 6.2.2p5). |
| 8710 | if (!Var->isInvalidDecl()) { |
| 8711 | if (const IncompleteArrayType *ArrayT |
| 8712 | = Context.getAsIncompleteArrayType(Type)) { |
| 8713 | if (RequireCompleteType(Var->getLocation(), |
| 8714 | ArrayT->getElementType(), |
| 8715 | diag::err_illegal_decl_array_incomplete_type)) |
| 8716 | Var->setInvalidDecl(); |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 8717 | } else if (Var->getStorageClass() == SC_Static) { |
Douglas Gregor | 60c93c9 | 2010-02-09 07:26:29 +0000 | [diff] [blame] | 8718 | // C99 6.9.2p3: If the declaration of an identifier for an object is |
| 8719 | // a tentative definition and has internal linkage (C99 6.2.2p3), the |
| 8720 | // declared type shall not be an incomplete type. |
| 8721 | // NOTE: code such as the following |
| 8722 | // static struct s; |
| 8723 | // struct s { int a; }; |
| 8724 | // is accepted by gcc. Hence here we issue a warning instead of |
| 8725 | // an error and we do not invalidate the static declaration. |
| 8726 | // NOTE: to avoid multiple warnings, only check the first declaration. |
Rafael Espindola | 7693b32 | 2013-10-19 02:13:21 +0000 | [diff] [blame] | 8727 | if (Var->isFirstDecl()) |
Douglas Gregor | 60c93c9 | 2010-02-09 07:26:29 +0000 | [diff] [blame] | 8728 | RequireCompleteType(Var->getLocation(), Type, |
| 8729 | diag::ext_typecheck_decl_incomplete_type); |
| 8730 | } |
| 8731 | } |
| 8732 | |
| 8733 | // Record the tentative definition; we're done. |
| 8734 | if (!Var->isInvalidDecl()) |
| 8735 | TentativeDefinitions.push_back(Var); |
| 8736 | return; |
| 8737 | } |
| 8738 | |
| 8739 | // Provide a specific diagnostic for uninitialized variable |
| 8740 | // definitions with incomplete array type. |
| 8741 | if (Type->isIncompleteArrayType()) { |
Sebastian Redl | 6e82475 | 2009-11-05 19:47:47 +0000 | [diff] [blame] | 8742 | Diag(Var->getLocation(), |
| 8743 | diag::err_typecheck_incomplete_array_needs_initializer); |
| 8744 | Var->setInvalidDecl(); |
| 8745 | return; |
| 8746 | } |
| 8747 | |
John McCall | b567a8b | 2010-08-01 01:25:24 +0000 | [diff] [blame] | 8748 | // Provide a specific diagnostic for uninitialized variable |
| 8749 | // definitions with reference type. |
| 8750 | if (Type->isReferenceType()) { |
| 8751 | Diag(Var->getLocation(), diag::err_reference_var_requires_init) |
| 8752 | << Var->getDeclName() |
| 8753 | << SourceRange(Var->getLocation(), Var->getLocation()); |
| 8754 | Var->setInvalidDecl(); |
| 8755 | return; |
| 8756 | } |
Douglas Gregor | 60c93c9 | 2010-02-09 07:26:29 +0000 | [diff] [blame] | 8757 | |
| 8758 | // Do not attempt to type-check the default initializer for a |
| 8759 | // variable with dependent type. |
| 8760 | if (Type->isDependentType()) |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8761 | return; |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 8762 | |
Douglas Gregor | 60c93c9 | 2010-02-09 07:26:29 +0000 | [diff] [blame] | 8763 | if (Var->isInvalidDecl()) |
| 8764 | return; |
Douglas Gregor | 1ab537b | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 8765 | |
Douglas Gregor | 60c93c9 | 2010-02-09 07:26:29 +0000 | [diff] [blame] | 8766 | if (RequireCompleteType(Var->getLocation(), |
| 8767 | Context.getBaseElementType(Type), |
| 8768 | diag::err_typecheck_decl_incomplete_type)) { |
| 8769 | Var->setInvalidDecl(); |
| 8770 | return; |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 8771 | } |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 8772 | |
Douglas Gregor | 60c93c9 | 2010-02-09 07:26:29 +0000 | [diff] [blame] | 8773 | // The variable can not have an abstract class type. |
| 8774 | if (RequireNonAbstractType(Var->getLocation(), Type, |
| 8775 | diag::err_abstract_type_in_decl, |
| 8776 | AbstractVariableType)) { |
| 8777 | Var->setInvalidDecl(); |
| 8778 | return; |
| 8779 | } |
| 8780 | |
Douglas Gregor | 4337dc7 | 2011-05-21 17:52:48 +0000 | [diff] [blame] | 8781 | // Check for jumps past the implicit initializer. C++0x |
| 8782 | // clarifies that this applies to a "variable with automatic |
| 8783 | // storage duration", not a "local variable". |
Richard Smith | 0e9e981 | 2011-10-20 21:42:12 +0000 | [diff] [blame] | 8784 | // C++11 [stmt.dcl]p3 |
Douglas Gregor | 4337dc7 | 2011-05-21 17:52:48 +0000 | [diff] [blame] | 8785 | // A program that jumps from a point where a variable with automatic |
| 8786 | // storage duration is not in scope to a point where it is in scope is |
| 8787 | // ill-formed unless the variable has scalar type, class type with a |
| 8788 | // trivial default constructor and a trivial destructor, a cv-qualified |
| 8789 | // version of one of these types, or an array of one of the preceding |
| 8790 | // types and is declared without an initializer. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8791 | if (getLangOpts().CPlusPlus && Var->hasLocalStorage()) { |
Douglas Gregor | 4337dc7 | 2011-05-21 17:52:48 +0000 | [diff] [blame] | 8792 | if (const RecordType *Record |
| 8793 | = Context.getBaseElementType(Type)->getAs<RecordType>()) { |
Sean Hunt | a6bff2c | 2011-05-11 22:50:12 +0000 | [diff] [blame] | 8794 | CXXRecordDecl *CXXRecord = cast<CXXRecordDecl>(Record->getDecl()); |
Richard Smith | 0e9e981 | 2011-10-20 21:42:12 +0000 | [diff] [blame] | 8795 | // Mark the function for further checking even if the looser rules of |
| 8796 | // C++11 do not require such checks, so that we can diagnose |
| 8797 | // incompatibilities with C++98. |
| 8798 | if (!CXXRecord->isPOD()) |
Sean Hunt | a6bff2c | 2011-05-11 22:50:12 +0000 | [diff] [blame] | 8799 | getCurFunction()->setHasBranchProtectedScope(); |
| 8800 | } |
Douglas Gregor | 60c93c9 | 2010-02-09 07:26:29 +0000 | [diff] [blame] | 8801 | } |
Douglas Gregor | 4337dc7 | 2011-05-21 17:52:48 +0000 | [diff] [blame] | 8802 | |
| 8803 | // C++03 [dcl.init]p9: |
| 8804 | // If no initializer is specified for an object, and the |
| 8805 | // object is of (possibly cv-qualified) non-POD class type (or |
| 8806 | // array thereof), the object shall be default-initialized; if |
| 8807 | // the object is of const-qualified type, the underlying class |
| 8808 | // type shall have a user-declared default |
| 8809 | // constructor. Otherwise, if no initializer is specified for |
| 8810 | // a non- static object, the object and its subobjects, if |
| 8811 | // any, have an indeterminate initial value); if the object |
| 8812 | // or any of its subobjects are of const-qualified type, the |
| 8813 | // program is ill-formed. |
| 8814 | // C++0x [dcl.init]p11: |
| 8815 | // If no initializer is specified for an object, the object is |
| 8816 | // default-initialized; [...]. |
| 8817 | InitializedEntity Entity = InitializedEntity::InitializeVariable(Var); |
| 8818 | InitializationKind Kind |
| 8819 | = InitializationKind::CreateDefault(Var->getLocation()); |
Dmitri Gribenko | 62ed889 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 8820 | |
| 8821 | InitializationSequence InitSeq(*this, Entity, Kind, None); |
| 8822 | ExprResult Init = InitSeq.Perform(*this, Entity, Kind, None); |
Douglas Gregor | 4337dc7 | 2011-05-21 17:52:48 +0000 | [diff] [blame] | 8823 | if (Init.isInvalid()) |
| 8824 | Var->setInvalidDecl(); |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 8825 | else if (Init.get()) { |
Douglas Gregor | 4337dc7 | 2011-05-21 17:52:48 +0000 | [diff] [blame] | 8826 | Var->setInit(MaybeCreateExprWithCleanups(Init.get())); |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 8827 | // This is important for template substitution. |
| 8828 | Var->setInitStyle(VarDecl::CallInit); |
| 8829 | } |
Douglas Gregor | 516a6bc | 2010-03-08 02:45:10 +0000 | [diff] [blame] | 8830 | |
John McCall | 2998d6b | 2011-01-19 11:48:09 +0000 | [diff] [blame] | 8831 | CheckCompleteVariableDeclaration(Var); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 8832 | } |
| 8833 | } |
| 8834 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 8835 | void Sema::ActOnCXXForRangeDecl(Decl *D) { |
| 8836 | VarDecl *VD = dyn_cast<VarDecl>(D); |
| 8837 | if (!VD) { |
| 8838 | Diag(D->getLocation(), diag::err_for_range_decl_must_be_var); |
| 8839 | D->setInvalidDecl(); |
| 8840 | return; |
| 8841 | } |
| 8842 | |
| 8843 | VD->setCXXForRangeDecl(true); |
| 8844 | |
| 8845 | // for-range-declaration cannot be given a storage class specifier. |
| 8846 | int Error = -1; |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 8847 | switch (VD->getStorageClass()) { |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 8848 | case SC_None: |
| 8849 | break; |
| 8850 | case SC_Extern: |
| 8851 | Error = 0; |
| 8852 | break; |
| 8853 | case SC_Static: |
| 8854 | Error = 1; |
| 8855 | break; |
| 8856 | case SC_PrivateExtern: |
| 8857 | Error = 2; |
| 8858 | break; |
| 8859 | case SC_Auto: |
| 8860 | Error = 3; |
| 8861 | break; |
| 8862 | case SC_Register: |
| 8863 | Error = 4; |
| 8864 | break; |
Peter Collingbourne | 8c25fc5 | 2011-09-19 21:14:35 +0000 | [diff] [blame] | 8865 | case SC_OpenCLWorkGroupLocal: |
Peter Collingbourne | 8be0c74 | 2011-09-20 12:40:26 +0000 | [diff] [blame] | 8866 | llvm_unreachable("Unexpected storage class"); |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 8867 | } |
Richard Smith | c6d990a | 2011-09-29 19:11:37 +0000 | [diff] [blame] | 8868 | if (VD->isConstexpr()) |
| 8869 | Error = 5; |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 8870 | if (Error != -1) { |
| 8871 | Diag(VD->getOuterLocStart(), diag::err_for_range_storage_class) |
| 8872 | << VD->getDeclName() << Error; |
| 8873 | D->setInvalidDecl(); |
| 8874 | } |
| 8875 | } |
| 8876 | |
John McCall | 2998d6b | 2011-01-19 11:48:09 +0000 | [diff] [blame] | 8877 | void Sema::CheckCompleteVariableDeclaration(VarDecl *var) { |
| 8878 | if (var->isInvalidDecl()) return; |
| 8879 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8880 | // In ARC, don't allow jumps past the implicit initialization of a |
| 8881 | // local retaining variable. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8882 | if (getLangOpts().ObjCAutoRefCount && |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8883 | var->hasLocalStorage()) { |
| 8884 | switch (var->getType().getObjCLifetime()) { |
| 8885 | case Qualifiers::OCL_None: |
| 8886 | case Qualifiers::OCL_ExplicitNone: |
| 8887 | case Qualifiers::OCL_Autoreleasing: |
| 8888 | break; |
| 8889 | |
| 8890 | case Qualifiers::OCL_Weak: |
| 8891 | case Qualifiers::OCL_Strong: |
| 8892 | getCurFunction()->setHasBranchProtectedScope(); |
| 8893 | break; |
| 8894 | } |
| 8895 | } |
| 8896 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 8897 | // Warn about externally-visible variables being defined without a |
| 8898 | // prior declaration. We only want to do this for global |
| 8899 | // declarations, but we also specifically need to avoid doing it for |
| 8900 | // class members because the linkage of an anonymous class can |
| 8901 | // change if it's later given a typedef name. |
Eli Friedman | e4851f2 | 2012-10-23 20:19:32 +0000 | [diff] [blame] | 8902 | if (var->isThisDeclarationADefinition() && |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 8903 | var->getDeclContext()->getRedeclContext()->isFileContext() && |
Eli Friedman | 2ae28e5 | 2013-09-24 23:10:08 +0000 | [diff] [blame] | 8904 | var->isExternallyVisible() && var->hasLinkage() && |
Manuel Klimek | acaf110 | 2012-12-12 13:26:54 +0000 | [diff] [blame] | 8905 | getDiagnostics().getDiagnosticLevel( |
| 8906 | diag::warn_missing_variable_declarations, |
| 8907 | var->getLocation())) { |
Eli Friedman | e4851f2 | 2012-10-23 20:19:32 +0000 | [diff] [blame] | 8908 | // Find a previous declaration that's not a definition. |
| 8909 | VarDecl *prev = var->getPreviousDecl(); |
| 8910 | while (prev && prev->isThisDeclarationADefinition()) |
| 8911 | prev = prev->getPreviousDecl(); |
| 8912 | |
| 8913 | if (!prev) |
| 8914 | Diag(var->getLocation(), diag::warn_missing_variable_declarations) << var; |
| 8915 | } |
| 8916 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 8917 | if (var->getTLSKind() == VarDecl::TLS_Static) { |
| 8918 | const Expr *Culprit; |
| 8919 | if (var->getType().isDestructedType()) { |
| 8920 | // GNU C++98 edits for __thread, [basic.start.term]p3: |
| 8921 | // The type of an object with thread storage duration shall not |
| 8922 | // have a non-trivial destructor. |
| 8923 | Diag(var->getLocation(), diag::err_thread_nontrivial_dtor); |
| 8924 | if (getLangOpts().CPlusPlus11) |
| 8925 | Diag(var->getLocation(), diag::note_use_thread_local); |
| 8926 | } else if (getLangOpts().CPlusPlus && var->hasInit() && |
| 8927 | !var->getInit()->isConstantInitializer( |
| 8928 | Context, var->getType()->isReferenceType(), &Culprit)) { |
| 8929 | // GNU C++98 edits for __thread, [basic.start.init]p4: |
| 8930 | // An object of thread storage duration shall not require dynamic |
| 8931 | // initialization. |
| 8932 | // FIXME: Need strict checking here. |
| 8933 | Diag(Culprit->getExprLoc(), diag::err_thread_dynamic_init) |
| 8934 | << Culprit->getSourceRange(); |
| 8935 | if (getLangOpts().CPlusPlus11) |
| 8936 | Diag(var->getLocation(), diag::note_use_thread_local); |
| 8937 | } |
| 8938 | |
| 8939 | } |
| 8940 | |
| 8941 | if (var->isThisDeclarationADefinition() && |
| 8942 | ActiveTemplateInstantiations.empty()) { |
| 8943 | PragmaStack<StringLiteral *> *Stack = nullptr; |
| 8944 | int SectionFlags = PSF_Implicit | PSF_Read; |
| 8945 | if (var->getType().isConstQualified()) |
| 8946 | Stack = &ConstSegStack; |
| 8947 | else if (!var->getInit()) { |
| 8948 | Stack = &BSSSegStack; |
| 8949 | SectionFlags |= PSF_Write; |
| 8950 | } else { |
| 8951 | Stack = &DataSegStack; |
| 8952 | SectionFlags |= PSF_Write; |
| 8953 | } |
| 8954 | if (!var->hasAttr<SectionAttr>() && Stack->CurrentValue) |
| 8955 | var->addAttr( |
| 8956 | SectionAttr::CreateImplicit(Context, SectionAttr::Declspec_allocate, |
| 8957 | Stack->CurrentValue->getString(), |
| 8958 | Stack->CurrentPragmaLocation)); |
| 8959 | if (const SectionAttr *SA = var->getAttr<SectionAttr>()) |
| 8960 | if (UnifySection(SA->getName(), SectionFlags, var)) |
| 8961 | var->dropAttr<SectionAttr>(); |
Richard Smith | 6a570f6 | 2013-04-14 20:11:31 +0000 | [diff] [blame] | 8962 | } |
| 8963 | |
John McCall | 2998d6b | 2011-01-19 11:48:09 +0000 | [diff] [blame] | 8964 | // All the following checks are C++ only. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8965 | if (!getLangOpts().CPlusPlus) return; |
John McCall | 2998d6b | 2011-01-19 11:48:09 +0000 | [diff] [blame] | 8966 | |
Richard Smith | a67d503 | 2012-11-09 23:03:14 +0000 | [diff] [blame] | 8967 | QualType type = var->getType(); |
| 8968 | if (type->isDependentType()) return; |
John McCall | 2998d6b | 2011-01-19 11:48:09 +0000 | [diff] [blame] | 8969 | |
| 8970 | // __block variables might require us to capture a copy-initializer. |
| 8971 | if (var->hasAttr<BlocksAttr>()) { |
| 8972 | // It's currently invalid to ever have a __block variable with an |
| 8973 | // array type; should we diagnose that here? |
| 8974 | |
| 8975 | // Regardless, we don't want to ignore array nesting when |
| 8976 | // constructing this copy. |
John McCall | 2998d6b | 2011-01-19 11:48:09 +0000 | [diff] [blame] | 8977 | if (type->isStructureOrClassType()) { |
John McCall | b760f11 | 2013-03-22 02:10:40 +0000 | [diff] [blame] | 8978 | EnterExpressionEvaluationContext scope(*this, PotentiallyEvaluated); |
John McCall | 2998d6b | 2011-01-19 11:48:09 +0000 | [diff] [blame] | 8979 | SourceLocation poi = var->getLocation(); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 8980 | Expr *varRef =new (Context) DeclRefExpr(var, false, type, VK_LValue, poi); |
Douglas Gregor | 6cda3e6 | 2013-03-07 22:38:24 +0000 | [diff] [blame] | 8981 | ExprResult result |
| 8982 | = PerformMoveOrCopyInitialization( |
| 8983 | InitializedEntity::InitializeBlock(poi, type, false), |
| 8984 | var, var->getType(), varRef, /*AllowNRVO=*/true); |
John McCall | 2998d6b | 2011-01-19 11:48:09 +0000 | [diff] [blame] | 8985 | if (!result.isInvalid()) { |
| 8986 | result = MaybeCreateExprWithCleanups(result); |
| 8987 | Expr *init = result.takeAs<Expr>(); |
| 8988 | Context.setBlockVarCopyInits(var, init); |
| 8989 | } |
| 8990 | } |
| 8991 | } |
| 8992 | |
Richard Smith | 66f8571 | 2011-11-07 22:16:17 +0000 | [diff] [blame] | 8993 | Expr *Init = var->getInit(); |
| 8994 | bool IsGlobal = var->hasGlobalStorage() && !var->isStaticLocal(); |
Richard Smith | a67d503 | 2012-11-09 23:03:14 +0000 | [diff] [blame] | 8995 | QualType baseType = Context.getBaseElementType(type); |
Richard Smith | 66f8571 | 2011-11-07 22:16:17 +0000 | [diff] [blame] | 8996 | |
Richard Smith | 9568f0c | 2012-10-29 18:26:47 +0000 | [diff] [blame] | 8997 | if (!var->getDeclContext()->isDependentContext() && |
| 8998 | Init && !Init->isValueDependent()) { |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 8999 | if (IsGlobal && !var->isConstexpr() && |
| 9000 | getDiagnostics().getDiagnosticLevel(diag::warn_global_constructor, |
| 9001 | var->getLocation()) |
Eli Friedman | 21cde05 | 2013-07-16 22:40:53 +0000 | [diff] [blame] | 9002 | != DiagnosticsEngine::Ignored) { |
| 9003 | // Warn about globals which don't have a constant initializer. Don't |
| 9004 | // warn about globals with a non-trivial destructor because we already |
| 9005 | // warned about them. |
| 9006 | CXXRecordDecl *RD = baseType->getAsCXXRecordDecl(); |
| 9007 | if (!(RD && !RD->hasTrivialDestructor()) && |
| 9008 | !Init->isConstantInitializer(Context, baseType->isReferenceType())) |
| 9009 | Diag(var->getLocation(), diag::warn_global_constructor) |
| 9010 | << Init->getSourceRange(); |
| 9011 | } |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 9012 | |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 9013 | if (var->isConstexpr()) { |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 9014 | SmallVector<PartialDiagnosticAt, 8> Notes; |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 9015 | if (!var->evaluateValue(Notes) || !var->isInitICE()) { |
| 9016 | SourceLocation DiagLoc = var->getLocation(); |
| 9017 | // If the note doesn't add any useful information other than a source |
| 9018 | // location, fold it into the primary diagnostic. |
| 9019 | if (Notes.size() == 1 && Notes[0].second.getDiagID() == |
| 9020 | diag::note_invalid_subexpr_in_const_expr) { |
| 9021 | DiagLoc = Notes[0].first; |
| 9022 | Notes.clear(); |
| 9023 | } |
| 9024 | Diag(DiagLoc, diag::err_constexpr_var_requires_const_init) |
| 9025 | << var << Init->getSourceRange(); |
| 9026 | for (unsigned I = 0, N = Notes.size(); I != N; ++I) |
| 9027 | Diag(Notes[I].first, Notes[I].second); |
| 9028 | } |
Daniel Dunbar | 3d13c5a | 2012-03-09 01:51:51 +0000 | [diff] [blame] | 9029 | } else if (var->isUsableInConstantExpressions(Context)) { |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 9030 | // Check whether the initializer of a const variable of integral or |
| 9031 | // enumeration type is an ICE now, since we can't tell whether it was |
| 9032 | // initialized by a constant expression if we check later. |
| 9033 | var->checkInitIsICE(); |
| 9034 | } |
Richard Smith | 66f8571 | 2011-11-07 22:16:17 +0000 | [diff] [blame] | 9035 | } |
John McCall | 2998d6b | 2011-01-19 11:48:09 +0000 | [diff] [blame] | 9036 | |
| 9037 | // Require the destructor. |
| 9038 | if (const RecordType *recordType = baseType->getAs<RecordType>()) |
| 9039 | FinalizeVarWithDestructor(var, recordType); |
| 9040 | } |
| 9041 | |
Richard Smith | 483b9f3 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 9042 | /// FinalizeDeclaration - called by ParseDeclarationAfterDeclarator to perform |
| 9043 | /// any semantic actions necessary after any initializer has been attached. |
| 9044 | void |
| 9045 | Sema::FinalizeDeclaration(Decl *ThisDecl) { |
| 9046 | // Note that we are no longer parsing the initializer for this declaration. |
| 9047 | ParsingInitForAutoVars.erase(ThisDecl); |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 9048 | |
Rafael Espindola | 4c8cba8 | 2013-02-22 17:59:16 +0000 | [diff] [blame] | 9049 | VarDecl *VD = dyn_cast_or_null<VarDecl>(ThisDecl); |
Rafael Espindola | da844b3 | 2013-01-03 04:05:19 +0000 | [diff] [blame] | 9050 | if (!VD) |
| 9051 | return; |
| 9052 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 9053 | checkAttributesAfterMerging(*this, *VD); |
| 9054 | |
Rafael Espindola | 29535ba | 2013-08-16 23:18:50 +0000 | [diff] [blame] | 9055 | if (UsedAttr *Attr = VD->getAttr<UsedAttr>()) { |
| 9056 | if (!Attr->isInherited() && !VD->isThisDeclarationADefinition()) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 9057 | Diag(Attr->getLocation(), diag::warn_attribute_ignored) << Attr; |
Rafael Espindola | 29535ba | 2013-08-16 23:18:50 +0000 | [diff] [blame] | 9058 | VD->dropAttr<UsedAttr>(); |
| 9059 | } |
| 9060 | } |
| 9061 | |
Rafael Espindola | b1c0e20 | 2013-10-22 21:39:03 +0000 | [diff] [blame] | 9062 | if (!VD->isInvalidDecl() && |
| 9063 | VD->isThisDeclarationADefinition() == VarDecl::TentativeDefinition) { |
| 9064 | if (const VarDecl *Def = VD->getDefinition()) { |
| 9065 | if (Def->hasAttr<AliasAttr>()) { |
| 9066 | Diag(VD->getLocation(), diag::err_tentative_after_alias) |
| 9067 | << VD->getDeclName(); |
| 9068 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 9069 | VD->setInvalidDecl(); |
| 9070 | } |
| 9071 | } |
| 9072 | } |
| 9073 | |
Rafael Espindola | 4c8cba8 | 2013-02-22 17:59:16 +0000 | [diff] [blame] | 9074 | const DeclContext *DC = VD->getDeclContext(); |
| 9075 | // If there's a #pragma GCC visibility in scope, and this isn't a class |
| 9076 | // member, set the visibility of this variable. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 9077 | if (DC->getRedeclContext()->isFileContext() && VD->isExternallyVisible()) |
Rafael Espindola | 4c8cba8 | 2013-02-22 17:59:16 +0000 | [diff] [blame] | 9078 | AddPushedVisibilityAttribute(VD); |
| 9079 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 9080 | // FIXME: Warn on unused templates. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 9081 | if (VD->isFileVarDecl() && !VD->getDescribedVarTemplate() && |
| 9082 | !isa<VarTemplatePartialSpecializationDecl>(VD)) |
Rafael Espindola | 6769ccb | 2013-01-03 04:29:20 +0000 | [diff] [blame] | 9083 | MarkUnusedFileScopedDecl(VD); |
| 9084 | |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 9085 | // Now we have parsed the initializer and can update the table of magic |
| 9086 | // tag values. |
Rafael Espindola | da844b3 | 2013-01-03 04:05:19 +0000 | [diff] [blame] | 9087 | if (!VD->hasAttr<TypeTagForDatatypeAttr>() || |
| 9088 | !VD->getType()->isIntegralOrEnumerationType()) |
| 9089 | return; |
| 9090 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 9091 | for (const auto *I : ThisDecl->specific_attrs<TypeTagForDatatypeAttr>()) { |
Rafael Espindola | da844b3 | 2013-01-03 04:05:19 +0000 | [diff] [blame] | 9092 | const Expr *MagicValueExpr = VD->getInit(); |
| 9093 | if (!MagicValueExpr) { |
| 9094 | continue; |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 9095 | } |
Rafael Espindola | da844b3 | 2013-01-03 04:05:19 +0000 | [diff] [blame] | 9096 | llvm::APSInt MagicValueInt; |
| 9097 | if (!MagicValueExpr->isIntegerConstantExpr(MagicValueInt, Context)) { |
| 9098 | Diag(I->getRange().getBegin(), |
| 9099 | diag::err_type_tag_for_datatype_not_ice) |
| 9100 | << LangOpts.CPlusPlus << MagicValueExpr->getSourceRange(); |
| 9101 | continue; |
| 9102 | } |
| 9103 | if (MagicValueInt.getActiveBits() > 64) { |
| 9104 | Diag(I->getRange().getBegin(), |
| 9105 | diag::err_type_tag_for_datatype_too_large) |
| 9106 | << LangOpts.CPlusPlus << MagicValueExpr->getSourceRange(); |
| 9107 | continue; |
| 9108 | } |
| 9109 | uint64_t MagicValue = MagicValueInt.getZExtValue(); |
| 9110 | RegisterTypeTagForDatatype(I->getArgumentKind(), |
| 9111 | MagicValue, |
| 9112 | I->getMatchingCType(), |
| 9113 | I->getLayoutCompatible(), |
| 9114 | I->getMustBeNull()); |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 9115 | } |
Richard Smith | 483b9f3 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 9116 | } |
| 9117 | |
Rafael Espindola | 4549d7f | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 9118 | Sema::DeclGroupPtrTy Sema::FinalizeDeclaratorGroup(Scope *S, const DeclSpec &DS, |
| 9119 | ArrayRef<Decl *> Group) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 9120 | SmallVector<Decl*, 8> Decls; |
Eli Friedman | c1dc653 | 2009-05-29 01:49:24 +0000 | [diff] [blame] | 9121 | |
| 9122 | if (DS.isTypeSpecOwned()) |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 9123 | Decls.push_back(DS.getRepAsDecl()); |
Eli Friedman | c1dc653 | 2009-05-29 01:49:24 +0000 | [diff] [blame] | 9124 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 9125 | DeclaratorDecl *FirstDeclaratorInGroup = nullptr; |
Rafael Espindola | 4549d7f | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 9126 | for (unsigned i = 0, e = Group.size(); i != e; ++i) |
David Majnemer | aa82461 | 2013-09-17 23:57:10 +0000 | [diff] [blame] | 9127 | if (Decl *D = Group[i]) { |
| 9128 | if (DeclaratorDecl *DD = dyn_cast<DeclaratorDecl>(D)) |
| 9129 | if (!FirstDeclaratorInGroup) |
| 9130 | FirstDeclaratorInGroup = DD; |
Richard Smith | 406c38e | 2011-02-23 00:37:57 +0000 | [diff] [blame] | 9131 | Decls.push_back(D); |
David Majnemer | aa82461 | 2013-09-17 23:57:10 +0000 | [diff] [blame] | 9132 | } |
Richard Smith | 406c38e | 2011-02-23 00:37:57 +0000 | [diff] [blame] | 9133 | |
Eli Friedman | 5e867c8 | 2013-07-10 00:30:46 +0000 | [diff] [blame] | 9134 | if (DeclSpec::isDeclRep(DS.getTypeSpecType())) { |
David Majnemer | aa82461 | 2013-09-17 23:57:10 +0000 | [diff] [blame] | 9135 | if (TagDecl *Tag = dyn_cast_or_null<TagDecl>(DS.getRepAsDecl())) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 9136 | HandleTagNumbering(*this, Tag, S); |
David Majnemer | aa82461 | 2013-09-17 23:57:10 +0000 | [diff] [blame] | 9137 | if (!Tag->hasNameForLinkage() && !Tag->hasDeclaratorForAnonDecl()) |
| 9138 | Tag->setDeclaratorForAnonDecl(FirstDeclaratorInGroup); |
| 9139 | } |
Eli Friedman | 5e867c8 | 2013-07-10 00:30:46 +0000 | [diff] [blame] | 9140 | } |
David Blaikie | 66cff72 | 2012-11-14 01:52:05 +0000 | [diff] [blame] | 9141 | |
Rafael Espindola | 4549d7f | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 9142 | return BuildDeclaratorGroup(Decls, DS.containsPlaceholderType()); |
Richard Smith | 406c38e | 2011-02-23 00:37:57 +0000 | [diff] [blame] | 9143 | } |
| 9144 | |
| 9145 | /// BuildDeclaratorGroup - convert a list of declarations into a declaration |
| 9146 | /// group, performing any necessary semantic checking. |
| 9147 | Sema::DeclGroupPtrTy |
Rafael Espindola | 4549d7f | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 9148 | Sema::BuildDeclaratorGroup(llvm::MutableArrayRef<Decl *> Group, |
Richard Smith | 406c38e | 2011-02-23 00:37:57 +0000 | [diff] [blame] | 9149 | bool TypeMayContainAuto) { |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 9150 | // C++0x [dcl.spec.auto]p7: |
| 9151 | // If the type deduced for the template parameter U is not the same in each |
| 9152 | // deduction, the program is ill-formed. |
| 9153 | // FIXME: When initializer-list support is added, a distinction is needed |
| 9154 | // between the deduced type U and the deduced type which 'auto' stands for. |
| 9155 | // auto a = 0, b = { 1, 2, 3 }; |
| 9156 | // is legal because the deduced type U is 'int' in both cases. |
Rafael Espindola | 4549d7f | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 9157 | if (TypeMayContainAuto && Group.size() > 1) { |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 9158 | QualType Deduced; |
| 9159 | CanQualType DeducedCanon; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 9160 | VarDecl *DeducedDecl = nullptr; |
Rafael Espindola | 4549d7f | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 9161 | for (unsigned i = 0, e = Group.size(); i != e; ++i) { |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 9162 | if (VarDecl *D = dyn_cast<VarDecl>(Group[i])) { |
| 9163 | AutoType *AT = D->getType()->getContainedAutoType(); |
Richard Smith | 406c38e | 2011-02-23 00:37:57 +0000 | [diff] [blame] | 9164 | // Don't reissue diagnostics when instantiating a template. |
| 9165 | if (AT && D->isInvalidDecl()) |
| 9166 | break; |
Richard Smith | dc7a4f5 | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 9167 | QualType U = AT ? AT->getDeducedType() : QualType(); |
| 9168 | if (!U.isNull()) { |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 9169 | CanQualType UCanon = Context.getCanonicalType(U); |
| 9170 | if (Deduced.isNull()) { |
| 9171 | Deduced = U; |
| 9172 | DeducedCanon = UCanon; |
| 9173 | DeducedDecl = D; |
| 9174 | } else if (DeducedCanon != UCanon) { |
Richard Smith | 406c38e | 2011-02-23 00:37:57 +0000 | [diff] [blame] | 9175 | Diag(D->getTypeSourceInfo()->getTypeLoc().getBeginLoc(), |
| 9176 | diag::err_auto_different_deductions) |
Richard Smith | ffd015e | 2013-05-04 04:19:27 +0000 | [diff] [blame] | 9177 | << (AT->isDecltypeAuto() ? 1 : 0) |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 9178 | << Deduced << DeducedDecl->getDeclName() |
| 9179 | << U << D->getDeclName() |
| 9180 | << DeducedDecl->getInit()->getSourceRange() |
| 9181 | << D->getInit()->getSourceRange(); |
Richard Smith | 406c38e | 2011-02-23 00:37:57 +0000 | [diff] [blame] | 9182 | D->setInvalidDecl(); |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 9183 | break; |
| 9184 | } |
| 9185 | } |
| 9186 | } |
| 9187 | } |
| 9188 | } |
| 9189 | |
Rafael Espindola | 4549d7f | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 9190 | ActOnDocumentableDecls(Group); |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 9191 | |
Rafael Espindola | 4549d7f | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 9192 | return DeclGroupPtrTy::make( |
| 9193 | DeclGroupRef::Create(Context, Group.data(), Group.size())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 9194 | } |
Steve Naroff | e1223f7 | 2007-08-28 03:03:08 +0000 | [diff] [blame] | 9195 | |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 9196 | void Sema::ActOnDocumentableDecl(Decl *D) { |
Rafael Espindola | 4549d7f | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 9197 | ActOnDocumentableDecls(D); |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 9198 | } |
| 9199 | |
Rafael Espindola | 4549d7f | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 9200 | void Sema::ActOnDocumentableDecls(ArrayRef<Decl *> Group) { |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 9201 | // Don't parse the comment if Doxygen diagnostics are ignored. |
Rafael Espindola | 4549d7f | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 9202 | if (Group.empty() || !Group[0]) |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 9203 | return; |
| 9204 | |
| 9205 | if (Diags.getDiagnosticLevel(diag::warn_doc_param_not_found, |
| 9206 | Group[0]->getLocation()) |
| 9207 | == DiagnosticsEngine::Ignored) |
| 9208 | return; |
| 9209 | |
Rafael Espindola | 4549d7f | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 9210 | if (Group.size() >= 2) { |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 9211 | // This is a decl group. Normally it will contain only declarations |
Rafael Espindola | 4549d7f | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 9212 | // produced from declarator list. But in case we have any definitions or |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 9213 | // additional declaration references: |
| 9214 | // 'typedef struct S {} S;' |
| 9215 | // 'typedef struct S *S;' |
| 9216 | // 'struct S *pS;' |
| 9217 | // FinalizeDeclaratorGroup adds these as separate declarations. |
| 9218 | Decl *MaybeTagDecl = Group[0]; |
| 9219 | if (MaybeTagDecl && isa<TagDecl>(MaybeTagDecl)) { |
Rafael Espindola | 4549d7f | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 9220 | Group = Group.slice(1); |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 9221 | } |
| 9222 | } |
| 9223 | |
| 9224 | // See if there are any new comments that are not attached to a decl. |
| 9225 | ArrayRef<RawComment *> Comments = Context.getRawCommentList().getComments(); |
| 9226 | if (!Comments.empty() && |
| 9227 | !Comments.back()->isAttached()) { |
| 9228 | // There is at least one comment that not attached to a decl. |
| 9229 | // Maybe it should be attached to one of these decls? |
| 9230 | // |
| 9231 | // Note that this way we pick up not only comments that precede the |
| 9232 | // declaration, but also comments that *follow* the declaration -- thanks to |
| 9233 | // the lookahead in the lexer: we've consumed the semicolon and looked |
| 9234 | // ahead through comments. |
Rafael Espindola | 4549d7f | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 9235 | for (unsigned i = 0, e = Group.size(); i != e; ++i) |
Dmitri Gribenko | 1952354 | 2012-09-29 11:40:46 +0000 | [diff] [blame] | 9236 | Context.getCommentForDecl(Group[i], &PP); |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 9237 | } |
| 9238 | } |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 9239 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 9240 | /// ActOnParamDeclarator - Called from Parser::ParseFunctionDeclarator() |
| 9241 | /// to introduce parameters into function prototype scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 9242 | Decl *Sema::ActOnParamDeclarator(Scope *S, Declarator &D) { |
Chris Lattner | 985abd9 | 2008-06-26 06:49:43 +0000 | [diff] [blame] | 9243 | const DeclSpec &DS = D.getDeclSpec(); |
Douglas Gregor | 584049d | 2008-12-15 23:53:10 +0000 | [diff] [blame] | 9244 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 9245 | // Verify C99 6.7.5.3p2: The only SCS allowed is 'register'. |
Faisal Vali | fad9e13 | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 9246 | |
Peter Collingbourne | 7a8a2e3 | 2011-10-21 11:55:09 +0000 | [diff] [blame] | 9247 | // C++03 [dcl.stc]p2 also permits 'auto'. |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 9248 | VarDecl::StorageClass StorageClass = SC_None; |
Daniel Dunbar | 33ad012 | 2008-09-03 21:54:21 +0000 | [diff] [blame] | 9249 | if (DS.getStorageClassSpec() == DeclSpec::SCS_register) { |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 9250 | StorageClass = SC_Register; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9251 | } else if (getLangOpts().CPlusPlus && |
Peter Collingbourne | 7a8a2e3 | 2011-10-21 11:55:09 +0000 | [diff] [blame] | 9252 | DS.getStorageClassSpec() == DeclSpec::SCS_auto) { |
| 9253 | StorageClass = SC_Auto; |
Daniel Dunbar | 33ad012 | 2008-09-03 21:54:21 +0000 | [diff] [blame] | 9254 | } else if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified) { |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 9255 | Diag(DS.getStorageClassSpecLoc(), |
| 9256 | diag::err_invalid_storage_class_in_func_decl); |
Chris Lattner | 985abd9 | 2008-06-26 06:49:43 +0000 | [diff] [blame] | 9257 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 9258 | } |
Eli Friedman | 63054b3 | 2009-04-19 20:27:55 +0000 | [diff] [blame] | 9259 | |
Richard Smith | ec64244 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 9260 | if (DeclSpec::TSCS TSCS = DS.getThreadStorageClassSpec()) |
| 9261 | Diag(DS.getThreadStorageClassSpecLoc(), diag::err_invalid_thread) |
| 9262 | << DeclSpec::getSpecifierName(TSCS); |
| 9263 | if (DS.isConstexprSpecified()) |
| 9264 | Diag(DS.getConstexprSpecLoc(), diag::err_invalid_constexpr) |
Richard Smith | af1fc7a | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 9265 | << 0; |
Eli Friedman | 63054b3 | 2009-04-19 20:27:55 +0000 | [diff] [blame] | 9266 | |
Richard Smith | ec64244 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 9267 | DiagnoseFunctionSpecifiers(DS); |
Eli Friedman | 85a5319 | 2009-04-07 19:37:57 +0000 | [diff] [blame] | 9268 | |
Argyrios Kyrtzidis | 3215398 | 2011-06-28 03:01:15 +0000 | [diff] [blame] | 9269 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 9270 | QualType parmDeclType = TInfo->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9271 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9272 | if (getLangOpts().CPlusPlus) { |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 9273 | // Check that there are no default arguments inside the type of this |
| 9274 | // parameter. |
| 9275 | CheckExtraCXXDefaultArguments(D); |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 9276 | |
| 9277 | // Parameter declarators cannot be qualified (C++ [dcl.meaning]p1). |
| 9278 | if (D.getCXXScopeSpec().isSet()) { |
| 9279 | Diag(D.getIdentifierLoc(), diag::err_qualified_param_declarator) |
| 9280 | << D.getCXXScopeSpec().getRange(); |
| 9281 | D.getCXXScopeSpec().clear(); |
| 9282 | } |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 9283 | } |
| 9284 | |
Sean Hunt | 7533a5b | 2010-11-03 01:07:06 +0000 | [diff] [blame] | 9285 | // Ensure we have a valid name |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 9286 | IdentifierInfo *II = nullptr; |
Sean Hunt | 7533a5b | 2010-11-03 01:07:06 +0000 | [diff] [blame] | 9287 | if (D.hasName()) { |
| 9288 | II = D.getIdentifier(); |
| 9289 | if (!II) { |
| 9290 | Diag(D.getIdentifierLoc(), diag::err_bad_parameter_name) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 9291 | << GetNameForDeclarator(D).getName(); |
Sean Hunt | 7533a5b | 2010-11-03 01:07:06 +0000 | [diff] [blame] | 9292 | D.setInvalidType(true); |
| 9293 | } |
| 9294 | } |
| 9295 | |
Chris Lattner | d84aac1 | 2010-02-22 00:40:25 +0000 | [diff] [blame] | 9296 | // Check for redeclaration of parameters, e.g. int foo(int x, int x); |
Chris Lattner | cf79b01 | 2009-01-21 02:38:50 +0000 | [diff] [blame] | 9297 | if (II) { |
John McCall | 10f2873 | 2010-03-18 06:42:38 +0000 | [diff] [blame] | 9298 | LookupResult R(*this, II, D.getIdentifierLoc(), LookupOrdinaryName, |
| 9299 | ForRedeclaration); |
| 9300 | LookupName(R, S); |
| 9301 | if (R.isSingleResult()) { |
| 9302 | NamedDecl *PrevDecl = R.getFoundDecl(); |
Chris Lattner | cf79b01 | 2009-01-21 02:38:50 +0000 | [diff] [blame] | 9303 | if (PrevDecl->isTemplateParameter()) { |
| 9304 | // Maybe we will complain about the shadowed template parameter. |
| 9305 | DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), PrevDecl); |
| 9306 | // Just pretend that we didn't see the previous declaration. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 9307 | PrevDecl = nullptr; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 9308 | } else if (S->isDeclScope(PrevDecl)) { |
Chris Lattner | cf79b01 | 2009-01-21 02:38:50 +0000 | [diff] [blame] | 9309 | Diag(D.getIdentifierLoc(), diag::err_param_redefinition) << II; |
Chris Lattner | d84aac1 | 2010-02-22 00:40:25 +0000 | [diff] [blame] | 9310 | Diag(PrevDecl->getLocation(), diag::note_previous_declaration); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 9311 | |
Chris Lattner | cf79b01 | 2009-01-21 02:38:50 +0000 | [diff] [blame] | 9312 | // Recover by removing the name |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 9313 | II = nullptr; |
| 9314 | D.SetIdentifier(nullptr, D.getIdentifierLoc()); |
Fariborz Jahanian | 9a66c30 | 2010-02-12 21:53:14 +0000 | [diff] [blame] | 9315 | D.setInvalidType(true); |
Chris Lattner | cf79b01 | 2009-01-21 02:38:50 +0000 | [diff] [blame] | 9316 | } |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 9317 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 9318 | } |
Steve Naroff | 6a9f3e3 | 2007-08-07 22:44:21 +0000 | [diff] [blame] | 9319 | |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 9320 | // Temporarily put parameter variables in the translation unit, not |
| 9321 | // the enclosing context. This prevents them from accidentally |
| 9322 | // looking like class members in C++. |
Douglas Gregor | cb27b0f | 2010-04-12 07:48:19 +0000 | [diff] [blame] | 9323 | ParmVarDecl *New = CheckParameter(Context.getTranslationUnitDecl(), |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 9324 | D.getLocStart(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 9325 | D.getIdentifierLoc(), II, |
| 9326 | parmDeclType, TInfo, |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 9327 | StorageClass); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9328 | |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 9329 | if (D.isInvalidType()) |
John McCall | fb44de9 | 2011-05-01 22:35:37 +0000 | [diff] [blame] | 9330 | New->setInvalidDecl(); |
| 9331 | |
| 9332 | assert(S->isFunctionPrototypeScope()); |
| 9333 | assert(S->getFunctionPrototypeDepth() >= 1); |
| 9334 | New->setScopeInfo(S->getFunctionPrototypeDepth() - 1, |
| 9335 | S->getNextFunctionPrototypeIndex()); |
Douglas Gregor | cb27b0f | 2010-04-12 07:48:19 +0000 | [diff] [blame] | 9336 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 9337 | // Add the parameter declaration into this scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 9338 | S->AddDecl(New); |
Argyrios Kyrtzidis | 87f3ff0 | 2008-04-12 00:47:19 +0000 | [diff] [blame] | 9339 | if (II) |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 9340 | IdResolver.AddDecl(New); |
Nate Begeman | b7894b5 | 2008-02-17 21:20:31 +0000 | [diff] [blame] | 9341 | |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 9342 | ProcessDeclAttributes(S, New, D); |
Mike Stump | ea000bf | 2009-04-30 00:19:40 +0000 | [diff] [blame] | 9343 | |
Douglas Gregor | e389585 | 2011-09-12 18:37:38 +0000 | [diff] [blame] | 9344 | if (D.getDeclSpec().isModulePrivateSpecified()) |
| 9345 | Diag(New->getLocation(), diag::err_module_private_local) |
| 9346 | << 1 << New->getDeclName() |
| 9347 | << SourceRange(D.getDeclSpec().getModulePrivateSpecLoc()) |
| 9348 | << FixItHint::CreateRemoval(D.getDeclSpec().getModulePrivateSpecLoc()); |
| 9349 | |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 9350 | if (New->hasAttr<BlocksAttr>()) { |
Mike Stump | ea000bf | 2009-04-30 00:19:40 +0000 | [diff] [blame] | 9351 | Diag(New->getLocation(), diag::err_block_on_nonlocal); |
| 9352 | } |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 9353 | return New; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 9354 | } |
Fariborz Jahanian | 306d68f | 2007-11-08 23:49:49 +0000 | [diff] [blame] | 9355 | |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9356 | /// \brief Synthesizes a variable for a parameter arising from a |
| 9357 | /// typedef. |
| 9358 | ParmVarDecl *Sema::BuildParmVarDeclForTypedef(DeclContext *DC, |
| 9359 | SourceLocation Loc, |
| 9360 | QualType T) { |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 9361 | /* FIXME: setting StartLoc == Loc. |
| 9362 | Would it be worth to modify callers so as to provide proper source |
| 9363 | location for the unnamed parameters, embedding the parameter's type? */ |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 9364 | ParmVarDecl *Param = ParmVarDecl::Create(Context, DC, Loc, Loc, nullptr, |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9365 | T, Context.getTrivialTypeSourceInfo(T, Loc), |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 9366 | SC_None, nullptr); |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9367 | Param->setImplicit(); |
| 9368 | return Param; |
| 9369 | } |
| 9370 | |
John McCall | fbce0e1 | 2010-08-24 09:05:15 +0000 | [diff] [blame] | 9371 | void Sema::DiagnoseUnusedParameters(ParmVarDecl * const *Param, |
| 9372 | ParmVarDecl * const *ParamEnd) { |
John McCall | fbce0e1 | 2010-08-24 09:05:15 +0000 | [diff] [blame] | 9373 | // Don't diagnose unused-parameter errors in template instantiations; we |
| 9374 | // will already have done so in the template itself. |
| 9375 | if (!ActiveTemplateInstantiations.empty()) |
| 9376 | return; |
| 9377 | |
| 9378 | for (; Param != ParamEnd; ++Param) { |
Eli Friedman | dd9d645 | 2012-01-13 23:41:25 +0000 | [diff] [blame] | 9379 | if (!(*Param)->isReferenced() && (*Param)->getDeclName() && |
John McCall | fbce0e1 | 2010-08-24 09:05:15 +0000 | [diff] [blame] | 9380 | !(*Param)->hasAttr<UnusedAttr>()) { |
| 9381 | Diag((*Param)->getLocation(), diag::warn_unused_parameter) |
| 9382 | << (*Param)->getDeclName(); |
| 9383 | } |
| 9384 | } |
| 9385 | } |
| 9386 | |
Argyrios Kyrtzidis | 3532fdd | 2010-11-17 23:11:54 +0000 | [diff] [blame] | 9387 | void Sema::DiagnoseSizeOfParametersAndReturnValue(ParmVarDecl * const *Param, |
| 9388 | ParmVarDecl * const *ParamEnd, |
| 9389 | QualType ReturnTy, |
| 9390 | NamedDecl *D) { |
Argyrios Kyrtzidis | 1380a14 | 2010-11-18 00:20:36 +0000 | [diff] [blame] | 9391 | if (LangOpts.NumLargeByValueCopy == 0) // No check. |
Argyrios Kyrtzidis | 3532fdd | 2010-11-17 23:11:54 +0000 | [diff] [blame] | 9392 | return; |
| 9393 | |
Argyrios Kyrtzidis | 1380a14 | 2010-11-18 00:20:36 +0000 | [diff] [blame] | 9394 | // Warn if the return value is pass-by-value and larger than the specified |
| 9395 | // threshold. |
Eli Friedman | d18840d | 2012-01-09 23:46:59 +0000 | [diff] [blame] | 9396 | if (!ReturnTy->isDependentType() && ReturnTy.isPODType(Context)) { |
Argyrios Kyrtzidis | 3532fdd | 2010-11-17 23:11:54 +0000 | [diff] [blame] | 9397 | unsigned Size = Context.getTypeSizeInChars(ReturnTy).getQuantity(); |
Argyrios Kyrtzidis | 1380a14 | 2010-11-18 00:20:36 +0000 | [diff] [blame] | 9398 | if (Size > LangOpts.NumLargeByValueCopy) |
Argyrios Kyrtzidis | 3532fdd | 2010-11-17 23:11:54 +0000 | [diff] [blame] | 9399 | Diag(D->getLocation(), diag::warn_return_value_size) |
| 9400 | << D->getDeclName() << Size; |
| 9401 | } |
| 9402 | |
Argyrios Kyrtzidis | 1380a14 | 2010-11-18 00:20:36 +0000 | [diff] [blame] | 9403 | // Warn if any parameter is pass-by-value and larger than the specified |
| 9404 | // threshold. |
Argyrios Kyrtzidis | 3532fdd | 2010-11-17 23:11:54 +0000 | [diff] [blame] | 9405 | for (; Param != ParamEnd; ++Param) { |
| 9406 | QualType T = (*Param)->getType(); |
Eli Friedman | d18840d | 2012-01-09 23:46:59 +0000 | [diff] [blame] | 9407 | if (T->isDependentType() || !T.isPODType(Context)) |
Argyrios Kyrtzidis | 3532fdd | 2010-11-17 23:11:54 +0000 | [diff] [blame] | 9408 | continue; |
| 9409 | unsigned Size = Context.getTypeSizeInChars(T).getQuantity(); |
Argyrios Kyrtzidis | 1380a14 | 2010-11-18 00:20:36 +0000 | [diff] [blame] | 9410 | if (Size > LangOpts.NumLargeByValueCopy) |
Argyrios Kyrtzidis | 3532fdd | 2010-11-17 23:11:54 +0000 | [diff] [blame] | 9411 | Diag((*Param)->getLocation(), diag::warn_parameter_size) |
| 9412 | << (*Param)->getDeclName() << Size; |
| 9413 | } |
| 9414 | } |
| 9415 | |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 9416 | ParmVarDecl *Sema::CheckParameter(DeclContext *DC, SourceLocation StartLoc, |
| 9417 | SourceLocation NameLoc, IdentifierInfo *Name, |
| 9418 | QualType T, TypeSourceInfo *TSInfo, |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 9419 | VarDecl::StorageClass StorageClass) { |
Reid Kleckner | c910d4c | 2013-06-08 18:19:52 +0000 | [diff] [blame] | 9420 | // In ARC, infer a lifetime qualifier for appropriate parameter types. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9421 | if (getLangOpts().ObjCAutoRefCount && |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9422 | T.getObjCLifetime() == Qualifiers::OCL_None && |
Reid Kleckner | c910d4c | 2013-06-08 18:19:52 +0000 | [diff] [blame] | 9423 | T->isObjCLifetimeType()) { |
| 9424 | |
| 9425 | Qualifiers::ObjCLifetime lifetime; |
| 9426 | |
| 9427 | // Special cases for arrays: |
| 9428 | // - if it's const, use __unsafe_unretained |
| 9429 | // - otherwise, it's an error |
| 9430 | if (T->isArrayType()) { |
| 9431 | if (!T.isConstQualified()) { |
| 9432 | DelayedDiagnostics.add( |
| 9433 | sema::DelayedDiagnostic::makeForbiddenType( |
Fariborz Jahanian | 175fb10 | 2011-10-03 22:11:57 +0000 | [diff] [blame] | 9434 | NameLoc, diag::err_arc_array_param_no_ownership, T, false)); |
Reid Kleckner | c910d4c | 2013-06-08 18:19:52 +0000 | [diff] [blame] | 9435 | } |
| 9436 | lifetime = Qualifiers::OCL_ExplicitNone; |
| 9437 | } else { |
| 9438 | lifetime = T->getObjCARCImplicitLifetime(); |
| 9439 | } |
| 9440 | T = Context.getLifetimeQualifiedType(T, lifetime); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9441 | } |
| 9442 | |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 9443 | ParmVarDecl *New = ParmVarDecl::Create(Context, DC, StartLoc, NameLoc, Name, |
Douglas Gregor | 79e6bd3 | 2011-07-12 04:42:08 +0000 | [diff] [blame] | 9444 | Context.getAdjustedParameterType(T), |
| 9445 | TSInfo, |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 9446 | StorageClass, nullptr); |
Douglas Gregor | cb27b0f | 2010-04-12 07:48:19 +0000 | [diff] [blame] | 9447 | |
| 9448 | // Parameters can not be abstract class types. |
| 9449 | // For record types, this is done by the AbstractClassUsageDiagnoser once |
| 9450 | // the class has been completely parsed. |
| 9451 | if (!CurContext->isRecord() && |
| 9452 | RequireNonAbstractType(NameLoc, T, diag::err_abstract_type_in_decl, |
| 9453 | AbstractParamType)) |
| 9454 | New->setInvalidDecl(); |
| 9455 | |
| 9456 | // Parameter declarators cannot be interface types. All ObjC objects are |
| 9457 | // passed by reference. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 9458 | if (T->isObjCObjectType()) { |
Fariborz Jahanian | 1de6a6c | 2012-05-09 21:49:29 +0000 | [diff] [blame] | 9459 | SourceLocation TypeEndLoc = TSInfo->getTypeLoc().getLocEnd(); |
Douglas Gregor | cb27b0f | 2010-04-12 07:48:19 +0000 | [diff] [blame] | 9460 | Diag(NameLoc, |
Fariborz Jahanian | 8eaefdc | 2011-07-26 17:58:54 +0000 | [diff] [blame] | 9461 | diag::err_object_cannot_be_passed_returned_by_value) << 1 << T |
Fariborz Jahanian | 1de6a6c | 2012-05-09 21:49:29 +0000 | [diff] [blame] | 9462 | << FixItHint::CreateInsertion(TypeEndLoc, "*"); |
Fariborz Jahanian | 8eaefdc | 2011-07-26 17:58:54 +0000 | [diff] [blame] | 9463 | T = Context.getObjCObjectPointerType(T); |
| 9464 | New->setType(T); |
Douglas Gregor | cb27b0f | 2010-04-12 07:48:19 +0000 | [diff] [blame] | 9465 | } |
| 9466 | |
| 9467 | // ISO/IEC TR 18037 S6.7.3: "The type of an object with automatic storage |
| 9468 | // duration shall not be qualified by an address-space qualifier." |
| 9469 | // Since all parameters have automatic store duration, they can not have |
| 9470 | // an address space. |
| 9471 | if (T.getAddressSpace() != 0) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 9472 | // OpenCL allows function arguments declared to be an array of a type |
| 9473 | // to be qualified with an address space. |
| 9474 | if (!(getLangOpts().OpenCL && T->isArrayType())) { |
| 9475 | Diag(NameLoc, diag::err_arg_with_address_space); |
| 9476 | New->setInvalidDecl(); |
| 9477 | } |
Douglas Gregor | cb27b0f | 2010-04-12 07:48:19 +0000 | [diff] [blame] | 9478 | } |
| 9479 | |
| 9480 | return New; |
| 9481 | } |
| 9482 | |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 9483 | void Sema::ActOnFinishKNRParamDeclarations(Scope *S, Declarator &D, |
| 9484 | SourceLocation LocAfterDecls) { |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 9485 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo(); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 9486 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 9487 | // Verify 6.9.1p6: 'every identifier in the identifier list shall be declared' |
| 9488 | // for a K&R function. |
| 9489 | if (!FTI.hasPrototype) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 9490 | for (int i = FTI.NumParams; i != 0; /* decrement in loop */) { |
Douglas Gregor | 2610348 | 2009-04-02 03:14:12 +0000 | [diff] [blame] | 9491 | --i; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 9492 | if (FTI.Params[i].Param == nullptr) { |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 9493 | SmallString<256> Code; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 9494 | llvm::raw_svector_ostream(Code) |
| 9495 | << " int " << FTI.Params[i].Ident->getName() << ";\n"; |
| 9496 | Diag(FTI.Params[i].IdentLoc, diag::ext_param_not_declared) |
| 9497 | << FTI.Params[i].Ident |
| 9498 | << FixItHint::CreateInsertion(LocAfterDecls, Code.str()); |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 9499 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 9500 | // Implicitly declare the argument as type 'int' for lack of a better |
| 9501 | // type. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 9502 | AttributeFactory attrs; |
| 9503 | DeclSpec DS(attrs); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 9504 | const char* PrevSpec; // unused |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 9505 | unsigned DiagID; // unused |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 9506 | DS.SetTypeSpecType(DeclSpec::TST_int, FTI.Params[i].IdentLoc, PrevSpec, |
| 9507 | DiagID, Context.getPrintingPolicy()); |
Abramo Bagnara | 16467f2 | 2012-10-04 21:38:29 +0000 | [diff] [blame] | 9508 | // Use the identifier location for the type source range. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 9509 | DS.SetRangeStart(FTI.Params[i].IdentLoc); |
| 9510 | DS.SetRangeEnd(FTI.Params[i].IdentLoc); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 9511 | Declarator ParamD(DS, Declarator::KNRTypeListContext); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 9512 | ParamD.SetIdentifier(FTI.Params[i].Ident, FTI.Params[i].IdentLoc); |
| 9513 | FTI.Params[i].Param = ActOnParamDeclarator(S, ParamD); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 9514 | } |
| 9515 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9516 | } |
Douglas Gregor | be109b3 | 2009-01-23 16:23:13 +0000 | [diff] [blame] | 9517 | } |
| 9518 | |
Richard Smith | 87162c2 | 2012-04-17 22:30:01 +0000 | [diff] [blame] | 9519 | Decl *Sema::ActOnStartOfFunctionDef(Scope *FnBodyScope, Declarator &D) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 9520 | assert(getCurFunctionDecl() == nullptr && "Function parsing confused"); |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 9521 | assert(D.isFunctionDeclarator() && "Not a function declarator!"); |
Douglas Gregor | 584049d | 2008-12-15 23:53:10 +0000 | [diff] [blame] | 9522 | Scope *ParentScope = FnBodyScope->getParent(); |
Steve Naroff | adbbd0c | 2008-01-14 20:51:29 +0000 | [diff] [blame] | 9523 | |
Douglas Gregor | 45fa560 | 2011-11-07 20:56:01 +0000 | [diff] [blame] | 9524 | D.setFunctionDefinitionKind(FDK_Definition); |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 9525 | Decl *DP = HandleDeclarator(ParentScope, D, MultiTemplateParamsArg()); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 9526 | return ActOnStartOfFunctionDef(FnBodyScope, DP); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 9527 | } |
| 9528 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 9529 | void Sema::ActOnFinishInlineMethodDef(CXXMethodDecl *D) { |
| 9530 | Consumer.HandleInlineMethodDefinition(D); |
| 9531 | } |
| 9532 | |
Anders Carlsson | 8a0086c | 2012-12-18 01:29:20 +0000 | [diff] [blame] | 9533 | static bool ShouldWarnAboutMissingPrototype(const FunctionDecl *FD, |
| 9534 | const FunctionDecl*& PossibleZeroParamPrototype) { |
Anders Carlsson | 9f89dd7 | 2009-12-09 03:30:09 +0000 | [diff] [blame] | 9535 | // Don't warn about invalid declarations. |
| 9536 | if (FD->isInvalidDecl()) |
| 9537 | return false; |
Anders Carlsson | 63fb673 | 2009-12-09 03:44:46 +0000 | [diff] [blame] | 9538 | |
Anders Carlsson | 9f89dd7 | 2009-12-09 03:30:09 +0000 | [diff] [blame] | 9539 | // Or declarations that aren't global. |
| 9540 | if (!FD->isGlobal()) |
| 9541 | return false; |
Anders Carlsson | 63fb673 | 2009-12-09 03:44:46 +0000 | [diff] [blame] | 9542 | |
Anders Carlsson | 9f89dd7 | 2009-12-09 03:30:09 +0000 | [diff] [blame] | 9543 | // Don't warn about C++ member functions. |
| 9544 | if (isa<CXXMethodDecl>(FD)) |
| 9545 | return false; |
Anders Carlsson | 63fb673 | 2009-12-09 03:44:46 +0000 | [diff] [blame] | 9546 | |
Anders Carlsson | 9f89dd7 | 2009-12-09 03:30:09 +0000 | [diff] [blame] | 9547 | // Don't warn about 'main'. |
| 9548 | if (FD->isMain()) |
| 9549 | return false; |
Anders Carlsson | 63fb673 | 2009-12-09 03:44:46 +0000 | [diff] [blame] | 9550 | |
Anders Carlsson | 9f89dd7 | 2009-12-09 03:30:09 +0000 | [diff] [blame] | 9551 | // Don't warn about inline functions. |
John McCall | 850d3b3 | 2011-03-22 07:16:37 +0000 | [diff] [blame] | 9552 | if (FD->isInlined()) |
Anders Carlsson | 9f89dd7 | 2009-12-09 03:30:09 +0000 | [diff] [blame] | 9553 | return false; |
Anders Carlsson | 63fb673 | 2009-12-09 03:44:46 +0000 | [diff] [blame] | 9554 | |
| 9555 | // Don't warn about function templates. |
| 9556 | if (FD->getDescribedFunctionTemplate()) |
| 9557 | return false; |
| 9558 | |
| 9559 | // Don't warn about function template specializations. |
| 9560 | if (FD->isFunctionTemplateSpecialization()) |
| 9561 | return false; |
| 9562 | |
Tanya Lattner | a95b4f7 | 2012-07-26 00:08:28 +0000 | [diff] [blame] | 9563 | // Don't warn for OpenCL kernels. |
| 9564 | if (FD->hasAttr<OpenCLKernelAttr>()) |
| 9565 | return false; |
Richard Smith | a41c97a | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 9566 | |
Anders Carlsson | 9f89dd7 | 2009-12-09 03:30:09 +0000 | [diff] [blame] | 9567 | bool MissingPrototype = true; |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 9568 | for (const FunctionDecl *Prev = FD->getPreviousDecl(); |
| 9569 | Prev; Prev = Prev->getPreviousDecl()) { |
Anders Carlsson | 9f89dd7 | 2009-12-09 03:30:09 +0000 | [diff] [blame] | 9570 | // Ignore any declarations that occur in function or method |
| 9571 | // scope, because they aren't visible from the header. |
Richard Smith | a41c97a | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 9572 | if (Prev->getLexicalDeclContext()->isFunctionOrMethod()) |
Anders Carlsson | 9f89dd7 | 2009-12-09 03:30:09 +0000 | [diff] [blame] | 9573 | continue; |
Richard Smith | a41c97a | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 9574 | |
Anders Carlsson | 9f89dd7 | 2009-12-09 03:30:09 +0000 | [diff] [blame] | 9575 | MissingPrototype = !Prev->getType()->isFunctionProtoType(); |
Anders Carlsson | 8a0086c | 2012-12-18 01:29:20 +0000 | [diff] [blame] | 9576 | if (FD->getNumParams() == 0) |
| 9577 | PossibleZeroParamPrototype = Prev; |
Anders Carlsson | 9f89dd7 | 2009-12-09 03:30:09 +0000 | [diff] [blame] | 9578 | break; |
| 9579 | } |
Richard Smith | a41c97a | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 9580 | |
Anders Carlsson | 9f89dd7 | 2009-12-09 03:30:09 +0000 | [diff] [blame] | 9581 | return MissingPrototype; |
| 9582 | } |
| 9583 | |
Rafael Espindola | b1c0e20 | 2013-10-22 21:39:03 +0000 | [diff] [blame] | 9584 | void |
| 9585 | Sema::CheckForFunctionRedefinition(FunctionDecl *FD, |
| 9586 | const FunctionDecl *EffectiveDefinition) { |
Francois Pichet | d4a0caf | 2011-04-22 23:20:44 +0000 | [diff] [blame] | 9587 | // Don't complain if we're in GNU89 mode and the previous definition |
| 9588 | // was an extern inline function. |
Rafael Espindola | b1c0e20 | 2013-10-22 21:39:03 +0000 | [diff] [blame] | 9589 | const FunctionDecl *Definition = EffectiveDefinition; |
| 9590 | if (!Definition) |
| 9591 | if (!FD->isDefined(Definition)) |
| 9592 | return; |
| 9593 | |
| 9594 | if (canRedefineFunction(Definition, getLangOpts())) |
Rafael Espindola | a2770ab | 2013-10-22 15:18:22 +0000 | [diff] [blame] | 9595 | return; |
| 9596 | |
| 9597 | if (getLangOpts().GNUMode && Definition->isInlineSpecified() && |
| 9598 | Definition->getStorageClass() == SC_Extern) |
| 9599 | Diag(FD->getLocation(), diag::err_redefinition_extern_inline) |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9600 | << FD->getDeclName() << getLangOpts().CPlusPlus; |
Rafael Espindola | a2770ab | 2013-10-22 15:18:22 +0000 | [diff] [blame] | 9601 | else |
| 9602 | Diag(FD->getLocation(), diag::err_redefinition) << FD->getDeclName(); |
| 9603 | |
| 9604 | Diag(Definition->getLocation(), diag::note_previous_definition); |
| 9605 | FD->setInvalidDecl(); |
Francois Pichet | d4a0caf | 2011-04-22 23:20:44 +0000 | [diff] [blame] | 9606 | } |
Faisal Vali | c00e419 | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 9607 | |
| 9608 | |
Faisal Vali | bef582b | 2013-10-23 16:10:50 +0000 | [diff] [blame] | 9609 | static void RebuildLambdaScopeInfo(CXXMethodDecl *CallOperator, |
| 9610 | Sema &S) { |
| 9611 | CXXRecordDecl *const LambdaClass = CallOperator->getParent(); |
Faisal Vali | d78d659 | 2013-11-12 01:40:44 +0000 | [diff] [blame] | 9612 | |
| 9613 | LambdaScopeInfo *LSI = S.PushLambdaScope(); |
Faisal Vali | bef582b | 2013-10-23 16:10:50 +0000 | [diff] [blame] | 9614 | LSI->CallOperator = CallOperator; |
| 9615 | LSI->Lambda = LambdaClass; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 9616 | LSI->ReturnType = CallOperator->getReturnType(); |
Faisal Vali | bef582b | 2013-10-23 16:10:50 +0000 | [diff] [blame] | 9617 | const LambdaCaptureDefault LCD = LambdaClass->getLambdaCaptureDefault(); |
| 9618 | |
| 9619 | if (LCD == LCD_None) |
| 9620 | LSI->ImpCaptureStyle = CapturingScopeInfo::ImpCap_None; |
| 9621 | else if (LCD == LCD_ByCopy) |
| 9622 | LSI->ImpCaptureStyle = CapturingScopeInfo::ImpCap_LambdaByval; |
| 9623 | else if (LCD == LCD_ByRef) |
| 9624 | LSI->ImpCaptureStyle = CapturingScopeInfo::ImpCap_LambdaByref; |
| 9625 | DeclarationNameInfo DNI = CallOperator->getNameInfo(); |
| 9626 | |
| 9627 | LSI->IntroducerRange = DNI.getCXXOperatorNameRange(); |
| 9628 | LSI->Mutable = !CallOperator->isConst(); |
| 9629 | |
Faisal Vali | c00e419 | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 9630 | // Add the captures to the LSI so they can be noted as already |
| 9631 | // captured within tryCaptureVar. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 9632 | for (const auto &C : LambdaClass->captures()) { |
| 9633 | if (C.capturesVariable()) { |
| 9634 | VarDecl *VD = C.getCapturedVar(); |
Faisal Vali | c00e419 | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 9635 | if (VD->isInitCapture()) |
| 9636 | S.CurrentInstantiationScope->InstantiatedLocal(VD, VD); |
| 9637 | QualType CaptureType = VD->getType(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 9638 | const bool ByRef = C.getCaptureKind() == LCK_ByRef; |
Faisal Vali | c00e419 | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 9639 | LSI->addCapture(VD, /*IsBlock*/false, ByRef, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 9640 | /*RefersToEnclosingLocal*/true, C.getLocation(), |
| 9641 | /*EllipsisLoc*/C.isPackExpansion() |
| 9642 | ? C.getEllipsisLoc() : SourceLocation(), |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 9643 | CaptureType, /*Expr*/ nullptr); |
| 9644 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 9645 | } else if (C.capturesThis()) { |
| 9646 | LSI->addThisCapture(/*Nested*/ false, C.getLocation(), |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 9647 | S.getCurrentThisType(), /*Expr*/ nullptr); |
Faisal Vali | c00e419 | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 9648 | } |
| 9649 | } |
Faisal Vali | bef582b | 2013-10-23 16:10:50 +0000 | [diff] [blame] | 9650 | } |
Francois Pichet | d4a0caf | 2011-04-22 23:20:44 +0000 | [diff] [blame] | 9651 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 9652 | Decl *Sema::ActOnStartOfFunctionDef(Scope *FnBodyScope, Decl *D) { |
Anders Carlsson | 0ebb6d3 | 2009-10-29 15:46:07 +0000 | [diff] [blame] | 9653 | // Clear the last template instantiation error context. |
| 9654 | LastTemplateInstantiationErrorContext = ActiveTemplateInstantiation(); |
| 9655 | |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 9656 | if (!D) |
| 9657 | return D; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 9658 | FunctionDecl *FD = nullptr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9659 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 9660 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D)) |
Douglas Gregor | d83d040 | 2009-08-22 00:34:47 +0000 | [diff] [blame] | 9661 | FD = FunTmpl->getTemplatedDecl(); |
| 9662 | else |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 9663 | FD = cast<FunctionDecl>(D); |
Faisal Vali | fad9e13 | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 9664 | // If we are instantiating a generic lambda call operator, push |
| 9665 | // a LambdaScopeInfo onto the function stack. But use the information |
Faisal Vali | bef582b | 2013-10-23 16:10:50 +0000 | [diff] [blame] | 9666 | // that's already been calculated (ActOnLambdaExpr) to prime the current |
| 9667 | // LambdaScopeInfo. |
| 9668 | // When the template operator is being specialized, the LambdaScopeInfo, |
| 9669 | // has to be properly restored so that tryCaptureVariable doesn't try |
| 9670 | // and capture any new variables. In addition when calculating potential |
| 9671 | // captures during transformation of nested lambdas, it is necessary to |
| 9672 | // have the LSI properly restored. |
Faisal Vali | 998c518 | 2013-09-29 20:15:45 +0000 | [diff] [blame] | 9673 | if (isGenericLambdaCallOperatorSpecialization(FD)) { |
Faisal Vali | fad9e13 | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 9674 | assert(ActiveTemplateInstantiations.size() && |
| 9675 | "There should be an active template instantiation on the stack " |
| 9676 | "when instantiating a generic lambda!"); |
Faisal Vali | bef582b | 2013-10-23 16:10:50 +0000 | [diff] [blame] | 9677 | RebuildLambdaScopeInfo(cast<CXXMethodDecl>(D), *this); |
Faisal Vali | fad9e13 | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 9678 | } |
| 9679 | else |
| 9680 | // Enter a new function scope |
| 9681 | PushFunctionScope(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9682 | |
Douglas Gregor | 6fc17ff | 2008-10-29 15:10:40 +0000 | [diff] [blame] | 9683 | // See if this is a redefinition. |
Francois Pichet | d4a0caf | 2011-04-22 23:20:44 +0000 | [diff] [blame] | 9684 | if (!FD->isLateTemplateParsed()) |
| 9685 | CheckForFunctionRedefinition(FD); |
Douglas Gregor | 6fc17ff | 2008-10-29 15:10:40 +0000 | [diff] [blame] | 9686 | |
Douglas Gregor | cda9c67 | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 9687 | // Builtin functions cannot be defined. |
Douglas Gregor | 7814e6d | 2009-09-12 00:22:50 +0000 | [diff] [blame] | 9688 | if (unsigned BuiltinID = FD->getBuiltinID()) { |
Rafael Espindola | ad24ad4 | 2013-06-13 18:34:17 +0000 | [diff] [blame] | 9689 | if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID) && |
| 9690 | !Context.BuiltinInfo.isPredefinedRuntimeFunction(BuiltinID)) { |
Douglas Gregor | cda9c67 | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 9691 | Diag(FD->getLocation(), diag::err_builtin_definition) << FD; |
Douglas Gregor | 655753a | 2009-02-17 16:03:01 +0000 | [diff] [blame] | 9692 | FD->setInvalidDecl(); |
| 9693 | } |
Douglas Gregor | cda9c67 | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 9694 | } |
| 9695 | |
Eli Friedman | 7f0f5dc | 2009-03-04 07:30:59 +0000 | [diff] [blame] | 9696 | // The return type of a function definition must be complete |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 9697 | // (C99 6.9.1p3, C++ [dcl.fct]p6). |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 9698 | QualType ResultType = FD->getReturnType(); |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 9699 | if (!ResultType->isDependentType() && !ResultType->isVoidType() && |
Chris Lattner | 65e6a09 | 2009-04-29 05:12:23 +0000 | [diff] [blame] | 9700 | !FD->isInvalidDecl() && |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 9701 | RequireCompleteType(FD->getLocation(), ResultType, |
| 9702 | diag::err_func_def_incomplete_result)) |
Eli Friedman | 7f0f5dc | 2009-03-04 07:30:59 +0000 | [diff] [blame] | 9703 | FD->setInvalidDecl(); |
Eli Friedman | 7f0f5dc | 2009-03-04 07:30:59 +0000 | [diff] [blame] | 9704 | |
Douglas Gregor | 8499f3f | 2009-03-31 16:35:03 +0000 | [diff] [blame] | 9705 | // GNU warning -Wmissing-prototypes: |
| 9706 | // Warn if a global function is defined without a previous |
| 9707 | // prototype declaration. This warning is issued even if the |
| 9708 | // definition itself provides a prototype. The aim is to detect |
| 9709 | // global functions that fail to be declared in header files. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 9710 | const FunctionDecl *PossibleZeroParamPrototype = nullptr; |
Anders Carlsson | 8a0086c | 2012-12-18 01:29:20 +0000 | [diff] [blame] | 9711 | if (ShouldWarnAboutMissingPrototype(FD, PossibleZeroParamPrototype)) { |
Anders Carlsson | 9f89dd7 | 2009-12-09 03:30:09 +0000 | [diff] [blame] | 9712 | Diag(FD->getLocation(), diag::warn_missing_prototype) << FD; |
Richard Smith | ac83a3c | 2013-06-25 20:34:17 +0000 | [diff] [blame] | 9713 | |
Anders Carlsson | 8a0086c | 2012-12-18 01:29:20 +0000 | [diff] [blame] | 9714 | if (PossibleZeroParamPrototype) { |
Richard Smith | ac83a3c | 2013-06-25 20:34:17 +0000 | [diff] [blame] | 9715 | // We found a declaration that is not a prototype, |
Anders Carlsson | 8a0086c | 2012-12-18 01:29:20 +0000 | [diff] [blame] | 9716 | // but that could be a zero-parameter prototype |
Richard Smith | ac83a3c | 2013-06-25 20:34:17 +0000 | [diff] [blame] | 9717 | if (TypeSourceInfo *TI = |
| 9718 | PossibleZeroParamPrototype->getTypeSourceInfo()) { |
| 9719 | TypeLoc TL = TI->getTypeLoc(); |
| 9720 | if (FunctionNoProtoTypeLoc FTL = TL.getAs<FunctionNoProtoTypeLoc>()) |
| 9721 | Diag(PossibleZeroParamPrototype->getLocation(), |
| 9722 | diag::note_declaration_not_a_prototype) |
| 9723 | << PossibleZeroParamPrototype |
| 9724 | << FixItHint::CreateInsertion(FTL.getRParenLoc(), "void"); |
| 9725 | } |
Anders Carlsson | 8a0086c | 2012-12-18 01:29:20 +0000 | [diff] [blame] | 9726 | } |
| 9727 | } |
Douglas Gregor | 8499f3f | 2009-03-31 16:35:03 +0000 | [diff] [blame] | 9728 | |
Douglas Gregor | e2c31ff | 2009-05-15 17:59:04 +0000 | [diff] [blame] | 9729 | if (FnBodyScope) |
| 9730 | PushDeclContext(FnBodyScope, FD); |
Anton Korobeynikov | 2f40270 | 2008-12-26 00:52:02 +0000 | [diff] [blame] | 9731 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 9732 | // Check the validity of our function parameters |
Douglas Gregor | 82aa713 | 2010-11-01 18:37:59 +0000 | [diff] [blame] | 9733 | CheckParmsForFunctionDef(FD->param_begin(), FD->param_end(), |
| 9734 | /*CheckParameterNames=*/true); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 9735 | |
| 9736 | // Introduce our parameters into the function scope |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 9737 | for (auto Param : FD->params()) { |
Douglas Gregor | a8cc8ce | 2009-01-09 18:51:29 +0000 | [diff] [blame] | 9738 | Param->setOwningFunction(FD); |
| 9739 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 9740 | // If this has an identifier, add it to the scope stack. |
John McCall | 053f4bd | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 9741 | if (Param->getIdentifier() && FnBodyScope) { |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 9742 | CheckShadow(FnBodyScope, Param); |
John McCall | 053f4bd | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 9743 | |
Argyrios Kyrtzidis | 87f3ff0 | 2008-04-12 00:47:19 +0000 | [diff] [blame] | 9744 | PushOnScopeChains(Param, FnBodyScope); |
John McCall | 053f4bd | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 9745 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 9746 | } |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 9747 | |
James Molloy | 16f1f71 | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 9748 | // If we had any tags defined in the function prototype, |
| 9749 | // introduce them into the function scope. |
| 9750 | if (FnBodyScope) { |
Robert Wilhelm | 834c058 | 2013-08-09 18:02:13 +0000 | [diff] [blame] | 9751 | for (ArrayRef<NamedDecl *>::iterator |
| 9752 | I = FD->getDeclsInPrototypeScope().begin(), |
| 9753 | E = FD->getDeclsInPrototypeScope().end(); |
| 9754 | I != E; ++I) { |
James Molloy | 16f1f71 | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 9755 | NamedDecl *D = *I; |
| 9756 | |
| 9757 | // Some of these decls (like enums) may have been pinned to the translation unit |
| 9758 | // for lack of a real context earlier. If so, remove from the translation unit |
| 9759 | // and reattach to the current context. |
| 9760 | if (D->getLexicalDeclContext() == Context.getTranslationUnitDecl()) { |
| 9761 | // Is the decl actually in the context? |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 9762 | for (const auto *DI : Context.getTranslationUnitDecl()->decls()) { |
| 9763 | if (DI == D) { |
James Molloy | 16f1f71 | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 9764 | Context.getTranslationUnitDecl()->removeDecl(D); |
| 9765 | break; |
| 9766 | } |
| 9767 | } |
| 9768 | // Either way, reassign the lexical decl context to our FunctionDecl. |
| 9769 | D->setLexicalDeclContext(CurContext); |
| 9770 | } |
| 9771 | |
| 9772 | // If the decl has a non-null name, make accessible in the current scope. |
| 9773 | if (!D->getName().empty()) |
| 9774 | PushOnScopeChains(D, FnBodyScope, /*AddToContext=*/false); |
| 9775 | |
| 9776 | // Similarly, dive into enums and fish their constants out, making them |
| 9777 | // accessible in this scope. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 9778 | if (auto *ED = dyn_cast<EnumDecl>(D)) { |
| 9779 | for (auto *EI : ED->enumerators()) |
| 9780 | PushOnScopeChains(EI, FnBodyScope, /*AddToContext=*/false); |
James Molloy | 16f1f71 | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 9781 | } |
| 9782 | } |
| 9783 | } |
| 9784 | |
Richard Smith | 87162c2 | 2012-04-17 22:30:01 +0000 | [diff] [blame] | 9785 | // Ensure that the function's exception specification is instantiated. |
| 9786 | if (const FunctionProtoType *FPT = FD->getType()->getAs<FunctionProtoType>()) |
| 9787 | ResolveExceptionSpec(D->getLocation(), FPT); |
| 9788 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 9789 | // dllimport cannot be applied to non-inline function definitions. |
| 9790 | if (FD->hasAttr<DLLImportAttr>() && !FD->isInlined() && |
| 9791 | !FD->isTemplateInstantiation()) { |
| 9792 | assert(!FD->hasAttr<DLLExportAttr>()); |
| 9793 | Diag(FD->getLocation(), diag::err_attribute_dllimport_function_definition); |
| 9794 | FD->setInvalidDecl(); |
| 9795 | return D; |
Anton Korobeynikov | 2f40270 | 2008-12-26 00:52:02 +0000 | [diff] [blame] | 9796 | } |
Dmitri Gribenko | c41ace9 | 2012-08-14 17:17:18 +0000 | [diff] [blame] | 9797 | // We want to attach documentation to original Decl (which might be |
| 9798 | // a function template). |
| 9799 | ActOnDocumentableDecl(D); |
Argyrios Kyrtzidis | a9990e8 | 2012-12-14 06:54:03 +0000 | [diff] [blame] | 9800 | return D; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 9801 | } |
| 9802 | |
Douglas Gregor | 5077c38 | 2010-05-15 06:01:05 +0000 | [diff] [blame] | 9803 | /// \brief Given the set of return statements within a function body, |
| 9804 | /// compute the variables that are subject to the named return value |
| 9805 | /// optimization. |
| 9806 | /// |
| 9807 | /// Each of the variables that is subject to the named return value |
| 9808 | /// optimization will be marked as NRVO variables in the AST, and any |
| 9809 | /// return statement that has a marked NRVO variable as its NRVO candidate can |
| 9810 | /// use the named return value optimization. |
| 9811 | /// |
| 9812 | /// This function applies a very simplistic algorithm for NRVO: if every return |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 9813 | /// statement in the scope of a variable has the same NRVO candidate, that |
| 9814 | /// candidate is an NRVO variable. |
Douglas Gregor | f8b7f71 | 2011-09-06 20:46:03 +0000 | [diff] [blame] | 9815 | void Sema::computeNRVO(Stmt *Body, FunctionScopeInfo *Scope) { |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 9816 | ReturnStmt **Returns = Scope->Returns.data(); |
| 9817 | |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 9818 | for (unsigned I = 0, E = Scope->Returns.size(); I != E; ++I) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 9819 | if (const VarDecl *NRVOCandidate = Returns[I]->getNRVOCandidate()) { |
| 9820 | if (!NRVOCandidate->isNRVOVariable()) |
| 9821 | Returns[I]->setNRVOCandidate(nullptr); |
| 9822 | } |
Douglas Gregor | 5077c38 | 2010-05-15 06:01:05 +0000 | [diff] [blame] | 9823 | } |
Douglas Gregor | 5077c38 | 2010-05-15 06:01:05 +0000 | [diff] [blame] | 9824 | } |
| 9825 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 9826 | bool Sema::canDelayFunctionBody(const Declarator &D) { |
| 9827 | // We can't delay parsing the body of a constexpr function template (yet). |
| 9828 | if (D.getDeclSpec().isConstexprSpecified()) |
Richard Smith | d1bac8d | 2012-11-27 21:31:01 +0000 | [diff] [blame] | 9829 | return false; |
| 9830 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 9831 | // We can't delay parsing the body of a function template with a deduced |
| 9832 | // return type (yet). |
| 9833 | if (D.getDeclSpec().containsPlaceholderType()) { |
| 9834 | // If the placeholder introduces a non-deduced trailing return type, |
| 9835 | // we can still delay parsing it. |
| 9836 | if (D.getNumTypeObjects()) { |
| 9837 | const auto &Outer = D.getTypeObject(D.getNumTypeObjects() - 1); |
| 9838 | if (Outer.Kind == DeclaratorChunk::Function && |
| 9839 | Outer.Fun.hasTrailingReturnType()) { |
| 9840 | QualType Ty = GetTypeFromParser(Outer.Fun.getTrailingReturnType()); |
| 9841 | return Ty.isNull() || !Ty->isUndeducedType(); |
| 9842 | } |
| 9843 | } |
| 9844 | return false; |
| 9845 | } |
Richard Smith | 1a5bd5d | 2012-11-19 21:13:18 +0000 | [diff] [blame] | 9846 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 9847 | return true; |
| 9848 | } |
Richard Smith | 1a5bd5d | 2012-11-19 21:13:18 +0000 | [diff] [blame] | 9849 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 9850 | bool Sema::canSkipFunctionBody(Decl *D) { |
Richard Smith | 1a5bd5d | 2012-11-19 21:13:18 +0000 | [diff] [blame] | 9851 | // We cannot skip the body of a function (or function template) which is |
| 9852 | // constexpr, since we may need to evaluate its body in order to parse the |
| 9853 | // rest of the file. |
Richard Smith | 25d8c85 | 2013-05-10 04:31:10 +0000 | [diff] [blame] | 9854 | // We cannot skip the body of a function with an undeduced return type, |
| 9855 | // because any callers of that function need to know the type. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 9856 | if (const FunctionDecl *FD = D->getAsFunction()) |
| 9857 | if (FD->isConstexpr() || FD->getReturnType()->isUndeducedType()) |
| 9858 | return false; |
| 9859 | return Consumer.shouldSkipFunctionBody(D); |
Richard Smith | 1a5bd5d | 2012-11-19 21:13:18 +0000 | [diff] [blame] | 9860 | } |
| 9861 | |
Argyrios Kyrtzidis | 35f3f36 | 2012-12-06 18:59:10 +0000 | [diff] [blame] | 9862 | Decl *Sema::ActOnSkippedFunctionBody(Decl *Decl) { |
Argyrios Kyrtzidis | 1f12c47 | 2013-02-22 04:11:06 +0000 | [diff] [blame] | 9863 | if (FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(Decl)) |
Argyrios Kyrtzidis | 35f3f36 | 2012-12-06 18:59:10 +0000 | [diff] [blame] | 9864 | FD->setHasSkippedBody(); |
Argyrios Kyrtzidis | 1f12c47 | 2013-02-22 04:11:06 +0000 | [diff] [blame] | 9865 | else if (ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(Decl)) |
Argyrios Kyrtzidis | 35f3f36 | 2012-12-06 18:59:10 +0000 | [diff] [blame] | 9866 | MD->setHasSkippedBody(); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 9867 | return ActOnFinishFunctionBody(Decl, nullptr); |
Argyrios Kyrtzidis | 35f3f36 | 2012-12-06 18:59:10 +0000 | [diff] [blame] | 9868 | } |
| 9869 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 9870 | Decl *Sema::ActOnFinishFunctionBody(Decl *D, Stmt *BodyArg) { |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 9871 | return ActOnFinishFunctionBody(D, BodyArg, false); |
Douglas Gregor | e2c31ff | 2009-05-15 17:59:04 +0000 | [diff] [blame] | 9872 | } |
| 9873 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9874 | Decl *Sema::ActOnFinishFunctionBody(Decl *dcl, Stmt *Body, |
| 9875 | bool IsInstantiation) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 9876 | FunctionDecl *FD = dcl ? dcl->getAsFunction() : nullptr; |
Douglas Gregor | d83d040 | 2009-08-22 00:34:47 +0000 | [diff] [blame] | 9877 | |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 9878 | sema::AnalysisBasedWarnings::Policy WP = AnalysisWarnings.getDefaultPolicy(); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 9879 | sema::AnalysisBasedWarnings::Policy *ActivePolicy = nullptr; |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 9880 | |
Douglas Gregor | d83d040 | 2009-08-22 00:34:47 +0000 | [diff] [blame] | 9881 | if (FD) { |
Chris Lattner | a5251fc | 2009-04-18 09:36:27 +0000 | [diff] [blame] | 9882 | FD->setBody(Body); |
John McCall | 75d8ba3 | 2012-02-14 19:50:52 +0000 | [diff] [blame] | 9883 | |
Richard Smith | 25d8c85 | 2013-05-10 04:31:10 +0000 | [diff] [blame] | 9884 | if (getLangOpts().CPlusPlus1y && !FD->isInvalidDecl() && Body && |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 9885 | !FD->isDependentContext() && FD->getReturnType()->isUndeducedType()) { |
Richard Smith | 25d8c85 | 2013-05-10 04:31:10 +0000 | [diff] [blame] | 9886 | // If the function has a deduced result type but contains no 'return' |
| 9887 | // statements, the result type as written must be exactly 'auto', and |
| 9888 | // the deduced result type is 'void'. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 9889 | if (!FD->getReturnType()->getAs<AutoType>()) { |
Richard Smith | 25d8c85 | 2013-05-10 04:31:10 +0000 | [diff] [blame] | 9890 | Diag(dcl->getLocation(), diag::err_auto_fn_no_return_but_not_auto) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 9891 | << FD->getReturnType(); |
Richard Smith | 25d8c85 | 2013-05-10 04:31:10 +0000 | [diff] [blame] | 9892 | FD->setInvalidDecl(); |
| 9893 | } else { |
| 9894 | // Substitute 'void' for the 'auto' in the type. |
| 9895 | TypeLoc ResultType = FD->getTypeSourceInfo()->getTypeLoc(). |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 9896 | IgnoreParens().castAs<FunctionProtoTypeLoc>().getReturnLoc(); |
Richard Smith | 25d8c85 | 2013-05-10 04:31:10 +0000 | [diff] [blame] | 9897 | Context.adjustDeducedFunctionResultType( |
| 9898 | FD, SubstAutoType(ResultType.getType(), Context.VoidTy)); |
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 9899 | } |
| 9900 | } |
| 9901 | |
Nick Lewycky | cd0655b | 2013-02-01 08:13:20 +0000 | [diff] [blame] | 9902 | // The only way to be included in UndefinedButUsed is if there is an |
| 9903 | // ODR use before the definition. Avoid the expensive map lookup if this |
Nick Lewycky | 995e26b | 2013-01-31 03:23:57 +0000 | [diff] [blame] | 9904 | // is the first declaration. |
Rafael Espindola | 7693b32 | 2013-10-19 02:13:21 +0000 | [diff] [blame] | 9905 | if (!FD->isFirstDecl() && FD->getPreviousDecl()->isUsed()) { |
Rafael Espindola | 181e3ec | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 9906 | if (!FD->isExternallyVisible()) |
Nick Lewycky | cd0655b | 2013-02-01 08:13:20 +0000 | [diff] [blame] | 9907 | UndefinedButUsed.erase(FD); |
| 9908 | else if (FD->isInlined() && |
| 9909 | (LangOpts.CPlusPlus || !LangOpts.GNUInline) && |
| 9910 | (!FD->getPreviousDecl()->hasAttr<GNUInlineAttr>())) |
| 9911 | UndefinedButUsed.erase(FD); |
| 9912 | } |
Nick Lewycky | 995e26b | 2013-01-31 03:23:57 +0000 | [diff] [blame] | 9913 | |
John McCall | 75d8ba3 | 2012-02-14 19:50:52 +0000 | [diff] [blame] | 9914 | // If the function implicitly returns zero (like 'main') or is naked, |
| 9915 | // don't complain about missing return statements. |
| 9916 | if (FD->hasImplicitReturnZero() || FD->hasAttr<NakedAttr>()) |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 9917 | WP.disableCheckFallThrough(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9918 | |
Francois Pichet | 6a24747 | 2011-05-11 02:14:46 +0000 | [diff] [blame] | 9919 | // MSVC permits the use of pure specifier (=0) on function definition, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 9920 | // defined at class scope, warn about this non-standard construct. |
Reid Kleckner | 5dbed66 | 2013-10-08 22:45:29 +0000 | [diff] [blame] | 9921 | if (getLangOpts().MicrosoftExt && FD->isPure() && FD->isCanonicalDecl()) |
Francois Pichet | 6a24747 | 2011-05-11 02:14:46 +0000 | [diff] [blame] | 9922 | Diag(FD->getLocation(), diag::warn_pure_function_definition); |
| 9923 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 9924 | if (!FD->isInvalidDecl()) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 9925 | // Don't diagnose unused parameters of defaulted or deleted functions. |
| 9926 | if (Body) |
| 9927 | DiagnoseUnusedParameters(FD->param_begin(), FD->param_end()); |
Argyrios Kyrtzidis | 3532fdd | 2010-11-17 23:11:54 +0000 | [diff] [blame] | 9928 | DiagnoseSizeOfParametersAndReturnValue(FD->param_begin(), FD->param_end(), |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 9929 | FD->getReturnType(), FD); |
| 9930 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 9931 | // If this is a constructor, we need a vtable. |
| 9932 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(FD)) |
| 9933 | MarkVTableUsed(FD->getLocation(), Constructor->getParent()); |
Douglas Gregor | 5077c38 | 2010-05-15 06:01:05 +0000 | [diff] [blame] | 9934 | |
Jordan Rose | 7dd900e | 2012-07-02 21:19:23 +0000 | [diff] [blame] | 9935 | // Try to apply the named return value optimization. We have to check |
| 9936 | // if we can do this here because lambdas keep return statements around |
| 9937 | // to deduce an implicit return type. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 9938 | if (getLangOpts().CPlusPlus && FD->getReturnType()->isRecordType() && |
Jordan Rose | 7dd900e | 2012-07-02 21:19:23 +0000 | [diff] [blame] | 9939 | !FD->isDependentContext()) |
| 9940 | computeNRVO(Body, getCurFunction()); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 9941 | } |
| 9942 | |
Douglas Gregor | 76e3da5 | 2012-02-08 20:17:14 +0000 | [diff] [blame] | 9943 | assert((FD == getCurFunctionDecl() || getCurLambda()->CallOperator == FD) && |
| 9944 | "Function parsing confused"); |
Steve Naroff | 394f3f4 | 2008-07-25 17:57:26 +0000 | [diff] [blame] | 9945 | } else if (ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(dcl)) { |
Chris Lattner | ffed163 | 2009-02-16 19:27:54 +0000 | [diff] [blame] | 9946 | assert(MD == getCurMethodDecl() && "Method parsing confused"); |
Chris Lattner | a5251fc | 2009-04-18 09:36:27 +0000 | [diff] [blame] | 9947 | MD->setBody(Body); |
Argyrios Kyrtzidis | 3532fdd | 2010-11-17 23:11:54 +0000 | [diff] [blame] | 9948 | if (!MD->isInvalidDecl()) { |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9949 | DiagnoseUnusedParameters(MD->param_begin(), MD->param_end()); |
Argyrios Kyrtzidis | 3532fdd | 2010-11-17 23:11:54 +0000 | [diff] [blame] | 9950 | DiagnoseSizeOfParametersAndReturnValue(MD->param_begin(), MD->param_end(), |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 9951 | MD->getReturnType(), MD); |
| 9952 | |
Douglas Gregor | f7603f6 | 2011-09-06 20:33:37 +0000 | [diff] [blame] | 9953 | if (Body) |
Douglas Gregor | f8b7f71 | 2011-09-06 20:46:03 +0000 | [diff] [blame] | 9954 | computeNRVO(Body, getCurFunction()); |
Argyrios Kyrtzidis | 3532fdd | 2010-11-17 23:11:54 +0000 | [diff] [blame] | 9955 | } |
Jordan Rose | 535a5d0 | 2012-10-19 16:05:26 +0000 | [diff] [blame] | 9956 | if (getCurFunction()->ObjCShouldCallSuper) { |
Fariborz Jahanian | 9f55983 | 2012-09-10 16:51:09 +0000 | [diff] [blame] | 9957 | Diag(MD->getLocEnd(), diag::warn_objc_missing_super_call) |
| 9958 | << MD->getSelector().getAsString(); |
Jordan Rose | 535a5d0 | 2012-10-19 16:05:26 +0000 | [diff] [blame] | 9959 | getCurFunction()->ObjCShouldCallSuper = false; |
Nico Weber | 80cb6e6 | 2011-08-28 22:35:17 +0000 | [diff] [blame] | 9960 | } |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 9961 | if (getCurFunction()->ObjCWarnForNoDesignatedInitChain) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 9962 | const ObjCMethodDecl *InitMethod = nullptr; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 9963 | bool isDesignated = |
| 9964 | MD->isDesignatedInitializerForTheInterface(&InitMethod); |
| 9965 | assert(isDesignated && InitMethod); |
| 9966 | (void)isDesignated; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 9967 | |
| 9968 | auto superIsNSObject = [&](const ObjCMethodDecl *MD) { |
| 9969 | auto IFace = MD->getClassInterface(); |
| 9970 | if (!IFace) |
| 9971 | return false; |
| 9972 | auto SuperD = IFace->getSuperClass(); |
| 9973 | if (!SuperD) |
| 9974 | return false; |
| 9975 | return SuperD->getIdentifier() == |
| 9976 | NSAPIObj->getNSClassId(NSAPI::ClassId_NSObject); |
| 9977 | }; |
| 9978 | // Don't issue this warning for unavailable inits or direct subclasses |
| 9979 | // of NSObject. |
| 9980 | if (!MD->isUnavailable() && !superIsNSObject(MD)) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 9981 | Diag(MD->getLocation(), |
| 9982 | diag::warn_objc_designated_init_missing_super_call); |
| 9983 | Diag(InitMethod->getLocation(), |
| 9984 | diag::note_objc_designated_init_marked_here); |
| 9985 | } |
| 9986 | getCurFunction()->ObjCWarnForNoDesignatedInitChain = false; |
| 9987 | } |
| 9988 | if (getCurFunction()->ObjCWarnForNoInitDelegation) { |
| 9989 | // Don't issue this warning for unavaialable inits. |
| 9990 | if (!MD->isUnavailable()) |
| 9991 | Diag(MD->getLocation(), diag::warn_objc_secondary_init_missing_init_call); |
| 9992 | getCurFunction()->ObjCWarnForNoInitDelegation = false; |
| 9993 | } |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 9994 | } else { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 9995 | return nullptr; |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 9996 | } |
Douglas Gregor | e2c31ff | 2009-05-15 17:59:04 +0000 | [diff] [blame] | 9997 | |
Jordan Rose | 535a5d0 | 2012-10-19 16:05:26 +0000 | [diff] [blame] | 9998 | assert(!getCurFunction()->ObjCShouldCallSuper && |
Eli Friedman | 95aac15 | 2012-08-01 21:02:59 +0000 | [diff] [blame] | 9999 | "This should only be set for ObjC methods, which should have been " |
| 10000 | "handled in the block above."); |
Nico Weber | 9a1ecf0 | 2011-08-22 17:25:57 +0000 | [diff] [blame] | 10001 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 10002 | // Verify and clean out per-function state. |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 10003 | if (Body) { |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 10004 | // C++ constructors that have function-try-blocks can't have return |
| 10005 | // statements in the handlers of that block. (C++ [except.handle]p14) |
| 10006 | // Verify this. |
| 10007 | if (FD && isa<CXXConstructorDecl>(FD) && isa<CXXTryStmt>(Body)) |
| 10008 | DiagnoseReturnInConstructorExceptionHandler(cast<CXXTryStmt>(Body)); |
| 10009 | |
Richard Smith | 37bee67 | 2011-08-12 18:44:32 +0000 | [diff] [blame] | 10010 | // Verify that gotos and switch cases don't jump into scopes illegally. |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 10011 | if (getCurFunction()->NeedsScopeChecking() && |
Douglas Gregor | 27bec77 | 2012-08-17 05:12:08 +0000 | [diff] [blame] | 10012 | !PP.isCodeCompletionEnabled()) |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 10013 | DiagnoseInvalidJumps(Body); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10014 | |
John McCall | 1544282 | 2010-08-04 01:04:25 +0000 | [diff] [blame] | 10015 | if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(dcl)) { |
| 10016 | if (!Destructor->getParent()->isDependentType()) |
| 10017 | CheckDestructor(Destructor); |
| 10018 | |
John McCall | ef027fe | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 10019 | MarkBaseAndMemberDestructorsReferenced(Destructor->getLocation(), |
| 10020 | Destructor->getParent()); |
John McCall | 1544282 | 2010-08-04 01:04:25 +0000 | [diff] [blame] | 10021 | } |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 10022 | |
| 10023 | // If any errors have occurred, clear out any temporaries that may have |
| 10024 | // been leftover. This ensures that these temporaries won't be picked up for |
| 10025 | // deletion in some later function. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 10026 | if (getDiagnostics().hasErrorOccurred() || |
| 10027 | getDiagnostics().getSuppressAllDiagnostics()) { |
John McCall | 80ee6e8 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 10028 | DiscardCleanupsInEvaluationContext(); |
DeLesley Hutchins | 12f37e4 | 2012-12-07 22:53:48 +0000 | [diff] [blame] | 10029 | } |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 10030 | if (!getDiagnostics().hasUncompilableErrorOccurred() && |
DeLesley Hutchins | 12f37e4 | 2012-12-07 22:53:48 +0000 | [diff] [blame] | 10031 | !isa<FunctionTemplateDecl>(dcl)) { |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 10032 | // Since the body is valid, issue any analysis-based warnings that are |
| 10033 | // enabled. |
Ted Kremenek | 3ed6fc0 | 2011-02-23 01:51:48 +0000 | [diff] [blame] | 10034 | ActivePolicy = &WP; |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 10035 | } |
| 10036 | |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 10037 | if (!IsInstantiation && FD && FD->isConstexpr() && !FD->isInvalidDecl() && |
| 10038 | (!CheckConstexprFunctionDecl(FD) || |
| 10039 | !CheckConstexprFunctionBody(FD, Body))) |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 10040 | FD->setInvalidDecl(); |
| 10041 | |
John McCall | 80ee6e8 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 10042 | assert(ExprCleanupObjects.empty() && "Leftover temporaries in function"); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 10043 | assert(!ExprNeedsCleanups && "Unaccounted cleanups in function"); |
Eli Friedman | d2cce13 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 10044 | assert(MaybeODRUseExprs.empty() && |
| 10045 | "Leftover expressions for odr-use checking"); |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 10046 | } |
| 10047 | |
John McCall | 90f9789 | 2010-03-25 22:08:03 +0000 | [diff] [blame] | 10048 | if (!IsInstantiation) |
| 10049 | PopDeclContext(); |
| 10050 | |
Eli Friedman | ec9ea72 | 2012-01-05 03:35:19 +0000 | [diff] [blame] | 10051 | PopFunctionScopeInfo(ActivePolicy, dcl); |
Douglas Gregor | d5b5728 | 2009-11-15 07:07:58 +0000 | [diff] [blame] | 10052 | // If any errors have occurred, clear out any temporaries that may have |
| 10053 | // been leftover. This ensures that these temporaries won't be picked up for |
| 10054 | // deletion in some later function. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 10055 | if (getDiagnostics().hasErrorOccurred()) { |
John McCall | 80ee6e8 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 10056 | DiscardCleanupsInEvaluationContext(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 10057 | } |
Argyrios Kyrtzidis | 49b96d1 | 2010-08-13 18:42:17 +0000 | [diff] [blame] | 10058 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10059 | return dcl; |
Fariborz Jahanian | 60fbca0 | 2007-11-10 16:31:34 +0000 | [diff] [blame] | 10060 | } |
| 10061 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 10062 | |
| 10063 | /// When we finish delayed parsing of an attribute, we must attach it to the |
| 10064 | /// relevant Decl. |
| 10065 | void Sema::ActOnFinishDelayedAttribute(Scope *S, Decl *D, |
| 10066 | ParsedAttributes &Attrs) { |
DeLesley Hutchins | 7b9ff0c | 2012-01-20 22:37:06 +0000 | [diff] [blame] | 10067 | // Always attach attributes to the underlying decl. |
| 10068 | if (TemplateDecl *TD = dyn_cast<TemplateDecl>(D)) |
| 10069 | D = TD->getTemplatedDecl(); |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 10070 | ProcessDeclAttributeList(S, D, Attrs.getList()); |
| 10071 | |
| 10072 | if (CXXMethodDecl *Method = dyn_cast_or_null<CXXMethodDecl>(D)) |
| 10073 | if (Method->isStatic()) |
| 10074 | checkThisInStaticMemberFunctionAttributes(Method); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 10075 | } |
| 10076 | |
| 10077 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 10078 | /// ImplicitlyDefineFunction - An undeclared identifier was used in a function |
| 10079 | /// call, forming a call to an implicitly defined function (per C99 6.5.1p2). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10080 | NamedDecl *Sema::ImplicitlyDefineFunction(SourceLocation Loc, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 10081 | IdentifierInfo &II, Scope *S) { |
Douglas Gregor | 6393519 | 2009-03-02 00:19:53 +0000 | [diff] [blame] | 10082 | // Before we produce a declaration for an implicitly defined |
| 10083 | // function, see whether there was a locally-scoped declaration of |
| 10084 | // this name as a function or variable. If so, use that |
| 10085 | // (non-visible) declaration, and complain about it. |
Richard Smith | 662f41b | 2013-06-18 20:15:12 +0000 | [diff] [blame] | 10086 | if (NamedDecl *ExternCPrev = findLocallyScopedExternCDecl(&II)) { |
| 10087 | Diag(Loc, diag::warn_use_out_of_scope_declaration) << ExternCPrev; |
| 10088 | Diag(ExternCPrev->getLocation(), diag::note_previous_declaration); |
| 10089 | return ExternCPrev; |
Douglas Gregor | 6393519 | 2009-03-02 00:19:53 +0000 | [diff] [blame] | 10090 | } |
| 10091 | |
Chris Lattner | 37d1084 | 2008-05-05 21:18:06 +0000 | [diff] [blame] | 10092 | // Extension in C99. Legal in C90, but warn about it. |
Hans Wennborg | e3ca33a | 2011-12-08 15:56:07 +0000 | [diff] [blame] | 10093 | unsigned diag_id; |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 10094 | if (II.getName().startswith("__builtin_")) |
Abramo Bagnara | 753a200 | 2012-01-09 10:05:48 +0000 | [diff] [blame] | 10095 | diag_id = diag::warn_builtin_unknown; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10096 | else if (getLangOpts().C99) |
Hans Wennborg | e3ca33a | 2011-12-08 15:56:07 +0000 | [diff] [blame] | 10097 | diag_id = diag::ext_implicit_function_decl; |
Chris Lattner | 37d1084 | 2008-05-05 21:18:06 +0000 | [diff] [blame] | 10098 | else |
Hans Wennborg | e3ca33a | 2011-12-08 15:56:07 +0000 | [diff] [blame] | 10099 | diag_id = diag::warn_implicit_function_decl; |
| 10100 | Diag(Loc, diag_id) << &II; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10101 | |
Hans Wennborg | e3ca33a | 2011-12-08 15:56:07 +0000 | [diff] [blame] | 10102 | // Because typo correction is expensive, only do it if the implicit |
| 10103 | // function declaration is going to be treated as an error. |
| 10104 | if (Diags.getDiagnosticLevel(diag_id, Loc) >= DiagnosticsEngine::Error) { |
| 10105 | TypoCorrection Corrected; |
Kaelyn Uhrain | 43e875d | 2012-01-18 21:41:41 +0000 | [diff] [blame] | 10106 | DeclFilterCCC<FunctionDecl> Validator; |
Hans Wennborg | e3ca33a | 2011-12-08 15:56:07 +0000 | [diff] [blame] | 10107 | if (S && (Corrected = CorrectTypo(DeclarationNameInfo(&II, Loc), |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 10108 | LookupOrdinaryName, S, nullptr, Validator, |
| 10109 | CTK_NonError))) |
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 10110 | diagnoseTypo(Corrected, PDiag(diag::note_function_suggestion), |
| 10111 | /*ErrorRecovery*/false); |
Hans Wennborg | 122de3e | 2011-12-06 09:46:12 +0000 | [diff] [blame] | 10112 | } |
| 10113 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 10114 | // Set a Declarator for the implicit definition: int foo(); |
| 10115 | const char *Dummy; |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 10116 | AttributeFactory attrFactory; |
| 10117 | DeclSpec DS(attrFactory); |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 10118 | unsigned DiagID; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 10119 | bool Error = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, Dummy, DiagID, |
| 10120 | Context.getPrintingPolicy()); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 10121 | (void)Error; // Silence warning. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 10122 | assert(!Error && "Error setting up implicit decl!"); |
Abramo Bagnara | 59c0a81 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 10123 | SourceLocation NoLoc; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 10124 | Declarator D(DS, Declarator::BlockContext); |
Abramo Bagnara | 59c0a81 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 10125 | D.AddTypeInfo(DeclaratorChunk::getFunction(/*HasProto=*/false, |
| 10126 | /*IsAmbiguous=*/false, |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 10127 | /*LParenLoc=*/NoLoc, |
| 10128 | /*Params=*/nullptr, |
| 10129 | /*NumParams=*/0, |
Abramo Bagnara | 59c0a81 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 10130 | /*EllipsisLoc=*/NoLoc, |
| 10131 | /*RParenLoc=*/NoLoc, |
| 10132 | /*TypeQuals=*/0, |
| 10133 | /*RefQualifierIsLvalueRef=*/true, |
| 10134 | /*RefQualifierLoc=*/NoLoc, |
| 10135 | /*ConstQualifierLoc=*/NoLoc, |
| 10136 | /*VolatileQualifierLoc=*/NoLoc, |
| 10137 | /*MutableLoc=*/NoLoc, |
| 10138 | EST_None, |
| 10139 | /*ESpecLoc=*/NoLoc, |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 10140 | /*Exceptions=*/nullptr, |
| 10141 | /*ExceptionRanges=*/nullptr, |
Abramo Bagnara | 59c0a81 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 10142 | /*NumExceptions=*/0, |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 10143 | /*NoexceptExpr=*/nullptr, |
Abramo Bagnara | 59c0a81 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 10144 | Loc, Loc, D), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 10145 | DS.getAttributes(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 10146 | SourceLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 10147 | D.SetIdentifier(&II, Loc); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 10148 | |
Argyrios Kyrtzidis | 93213bb | 2008-05-01 21:04:16 +0000 | [diff] [blame] | 10149 | // Insert this function into translation-unit scope. |
| 10150 | |
| 10151 | DeclContext *PrevDC = CurContext; |
| 10152 | CurContext = Context.getTranslationUnitDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10153 | |
Jordan Rose | 41f3f3a | 2013-03-05 01:27:54 +0000 | [diff] [blame] | 10154 | FunctionDecl *FD = cast<FunctionDecl>(ActOnDeclarator(TUScope, D)); |
Steve Naroff | e2ef815 | 2008-04-04 14:32:09 +0000 | [diff] [blame] | 10155 | FD->setImplicit(); |
Argyrios Kyrtzidis | 93213bb | 2008-05-01 21:04:16 +0000 | [diff] [blame] | 10156 | |
| 10157 | CurContext = PrevDC; |
| 10158 | |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 10159 | AddKnownFunctionAttributes(FD); |
| 10160 | |
Steve Naroff | e2ef815 | 2008-04-04 14:32:09 +0000 | [diff] [blame] | 10161 | return FD; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 10162 | } |
| 10163 | |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 10164 | /// \brief Adds any function attributes that we know a priori based on |
| 10165 | /// the declaration of this function. |
| 10166 | /// |
| 10167 | /// These attributes can apply both to implicitly-declared builtins |
| 10168 | /// (like __builtin___printf_chk) or to library-declared functions |
| 10169 | /// like NSLog or printf. |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 10170 | /// |
| 10171 | /// We need to check for duplicate attributes both here and where user-written |
| 10172 | /// attributes are applied to declarations. |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 10173 | void Sema::AddKnownFunctionAttributes(FunctionDecl *FD) { |
| 10174 | if (FD->isInvalidDecl()) |
| 10175 | return; |
| 10176 | |
| 10177 | // If this is a built-in function, map its builtin attributes to |
| 10178 | // actual attributes. |
Douglas Gregor | 7814e6d | 2009-09-12 00:22:50 +0000 | [diff] [blame] | 10179 | if (unsigned BuiltinID = FD->getBuiltinID()) { |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 10180 | // Handle printf-formatting attributes. |
| 10181 | unsigned FormatIdx; |
| 10182 | bool HasVAListArg; |
| 10183 | if (Context.BuiltinInfo.isPrintfLike(BuiltinID, FormatIdx, HasVAListArg)) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 10184 | if (!FD->hasAttr<FormatAttr>()) { |
Jean-Daniel Dupas | 1acbe5e | 2012-01-24 22:32:46 +0000 | [diff] [blame] | 10185 | const char *fmt = "printf"; |
| 10186 | unsigned int NumParams = FD->getNumParams(); |
| 10187 | if (FormatIdx < NumParams && // NumParams may be 0 (e.g. vfprintf) |
| 10188 | FD->getParamDecl(FormatIdx)->getType()->isObjCObjectPointerType()) |
| 10189 | fmt = "NSString"; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 10190 | FD->addAttr(FormatAttr::CreateImplicit(Context, |
Aaron Ballman | caa5ab2 | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 10191 | &Context.Idents.get(fmt), |
| 10192 | FormatIdx+1, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 10193 | HasVAListArg ? 0 : FormatIdx+2, |
| 10194 | FD->getLocation())); |
Jean-Daniel Dupas | 1acbe5e | 2012-01-24 22:32:46 +0000 | [diff] [blame] | 10195 | } |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 10196 | } |
Ted Kremenek | bee05c1 | 2010-07-16 02:11:15 +0000 | [diff] [blame] | 10197 | if (Context.BuiltinInfo.isScanfLike(BuiltinID, FormatIdx, |
| 10198 | HasVAListArg)) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 10199 | if (!FD->hasAttr<FormatAttr>()) |
| 10200 | FD->addAttr(FormatAttr::CreateImplicit(Context, |
Aaron Ballman | caa5ab2 | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 10201 | &Context.Idents.get("scanf"), |
| 10202 | FormatIdx+1, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 10203 | HasVAListArg ? 0 : FormatIdx+2, |
| 10204 | FD->getLocation())); |
Ted Kremenek | bee05c1 | 2010-07-16 02:11:15 +0000 | [diff] [blame] | 10205 | } |
Daniel Dunbar | ef2abfe | 2009-02-16 22:43:43 +0000 | [diff] [blame] | 10206 | |
| 10207 | // Mark const if we don't care about errno and that is the only |
| 10208 | // thing preventing the function from being const. This allows |
| 10209 | // IRgen to use LLVM intrinsics for such functions. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10210 | if (!getLangOpts().MathErrno && |
Daniel Dunbar | ef2abfe | 2009-02-16 22:43:43 +0000 | [diff] [blame] | 10211 | Context.BuiltinInfo.isConstWithoutErrno(BuiltinID)) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 10212 | if (!FD->hasAttr<ConstAttr>()) |
| 10213 | FD->addAttr(ConstAttr::CreateImplicit(Context, FD->getLocation())); |
Daniel Dunbar | ef2abfe | 2009-02-16 22:43:43 +0000 | [diff] [blame] | 10214 | } |
Mike Stump | 0feecbb | 2009-07-27 19:14:18 +0000 | [diff] [blame] | 10215 | |
Rafael Espindola | 6700415 | 2011-10-12 19:51:18 +0000 | [diff] [blame] | 10216 | if (Context.BuiltinInfo.isReturnsTwice(BuiltinID) && |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 10217 | !FD->hasAttr<ReturnsTwiceAttr>()) |
| 10218 | FD->addAttr(ReturnsTwiceAttr::CreateImplicit(Context, |
| 10219 | FD->getLocation())); |
| 10220 | if (Context.BuiltinInfo.isNoThrow(BuiltinID) && !FD->hasAttr<NoThrowAttr>()) |
| 10221 | FD->addAttr(NoThrowAttr::CreateImplicit(Context, FD->getLocation())); |
| 10222 | if (Context.BuiltinInfo.isConst(BuiltinID) && !FD->hasAttr<ConstAttr>()) |
| 10223 | FD->addAttr(ConstAttr::CreateImplicit(Context, FD->getLocation())); |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 10224 | } |
| 10225 | |
| 10226 | IdentifierInfo *Name = FD->getIdentifier(); |
| 10227 | if (!Name) |
| 10228 | return; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10229 | if ((!getLangOpts().CPlusPlus && |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 10230 | FD->getDeclContext()->isTranslationUnit()) || |
| 10231 | (isa<LinkageSpecDecl>(FD->getDeclContext()) && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10232 | cast<LinkageSpecDecl>(FD->getDeclContext())->getLanguage() == |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 10233 | LinkageSpecDecl::lang_c)) { |
| 10234 | // Okay: this could be a libc/libm/Objective-C function we know |
| 10235 | // about. |
| 10236 | } else |
| 10237 | return; |
| 10238 | |
Jean-Daniel Dupas | 1acbe5e | 2012-01-24 22:32:46 +0000 | [diff] [blame] | 10239 | if (Name->isStr("asprintf") || Name->isStr("vasprintf")) { |
Mike Stump | 523a8fd | 2009-07-28 00:07:08 +0000 | [diff] [blame] | 10240 | // FIXME: asprintf and vasprintf aren't C99 functions. Should they be |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10241 | // target-specific builtins, perhaps? |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 10242 | if (!FD->hasAttr<FormatAttr>()) |
| 10243 | FD->addAttr(FormatAttr::CreateImplicit(Context, |
Aaron Ballman | caa5ab2 | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 10244 | &Context.Idents.get("printf"), 2, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 10245 | Name->isStr("vasprintf") ? 0 : 3, |
| 10246 | FD->getLocation())); |
Mike Stump | 782fa30 | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 10247 | } |
Jordan Rose | 8a64f88 | 2012-08-08 21:17:31 +0000 | [diff] [blame] | 10248 | |
| 10249 | if (Name->isStr("__CFStringMakeConstantString")) { |
| 10250 | // We already have a __builtin___CFStringMakeConstantString, |
| 10251 | // but builds that use -fno-constant-cfstrings don't go through that. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 10252 | if (!FD->hasAttr<FormatArgAttr>()) |
| 10253 | FD->addAttr(FormatArgAttr::CreateImplicit(Context, 1, |
| 10254 | FD->getLocation())); |
Jordan Rose | 8a64f88 | 2012-08-08 21:17:31 +0000 | [diff] [blame] | 10255 | } |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 10256 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 10257 | |
John McCall | ba6a9bd | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 10258 | TypedefDecl *Sema::ParseTypedefDecl(Scope *S, Declarator &D, QualType T, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 10259 | TypeSourceInfo *TInfo) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 10260 | assert(D.getIdentifier() && "Wrong callback for declspec without declarator"); |
Steve Naroff | 5912a35 | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 10261 | assert(!T.isNull() && "GetTypeForDeclarator() returned null type"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10262 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 10263 | if (!TInfo) { |
John McCall | ba6a9bd | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 10264 | assert(D.isInvalidType() && "no declarator info for valid type"); |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 10265 | TInfo = Context.getTrivialTypeSourceInfo(T); |
John McCall | ba6a9bd | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 10266 | } |
| 10267 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 10268 | // Scope manipulation handled by caller. |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 10269 | TypedefDecl *NewTD = TypedefDecl::Create(Context, CurContext, |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 10270 | D.getLocStart(), |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 10271 | D.getIdentifierLoc(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10272 | D.getIdentifier(), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 10273 | TInfo); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10274 | |
John McCall | cde5a40 | 2011-02-01 08:20:08 +0000 | [diff] [blame] | 10275 | // Bail out immediately if we have an invalid declaration. |
| 10276 | if (D.isInvalidType()) { |
| 10277 | NewTD->setInvalidDecl(); |
| 10278 | return NewTD; |
Anders Carlsson | 4843e58 | 2009-03-10 17:07:44 +0000 | [diff] [blame] | 10279 | } |
Fariborz Jahanian | 0f3bb9e | 2013-11-19 00:09:48 +0000 | [diff] [blame] | 10280 | |
Douglas Gregor | e389585 | 2011-09-12 18:37:38 +0000 | [diff] [blame] | 10281 | if (D.getDeclSpec().isModulePrivateSpecified()) { |
| 10282 | if (CurContext->isFunctionOrMethod()) |
| 10283 | Diag(NewTD->getLocation(), diag::err_module_private_local) |
| 10284 | << 2 << NewTD->getDeclName() |
| 10285 | << SourceRange(D.getDeclSpec().getModulePrivateSpecLoc()) |
| 10286 | << FixItHint::CreateRemoval(D.getDeclSpec().getModulePrivateSpecLoc()); |
| 10287 | else |
| 10288 | NewTD->setModulePrivate(); |
| 10289 | } |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 10290 | |
John McCall | cde5a40 | 2011-02-01 08:20:08 +0000 | [diff] [blame] | 10291 | // C++ [dcl.typedef]p8: |
| 10292 | // If the typedef declaration defines an unnamed class (or |
| 10293 | // enum), the first typedef-name declared by the declaration |
| 10294 | // to be that class type (or enum type) is used to denote the |
| 10295 | // class type (or enum type) for linkage purposes only. |
| 10296 | // We need to check whether the type was declared in the declaration. |
| 10297 | switch (D.getDeclSpec().getTypeSpecType()) { |
| 10298 | case TST_enum: |
| 10299 | case TST_struct: |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 10300 | case TST_interface: |
John McCall | cde5a40 | 2011-02-01 08:20:08 +0000 | [diff] [blame] | 10301 | case TST_union: |
| 10302 | case TST_class: { |
| 10303 | TagDecl *tagFromDeclSpec = cast<TagDecl>(D.getDeclSpec().getRepAsDecl()); |
| 10304 | |
| 10305 | // Do nothing if the tag is not anonymous or already has an |
| 10306 | // associated typedef (from an earlier typedef in this decl group). |
| 10307 | if (tagFromDeclSpec->getIdentifier()) break; |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 10308 | if (tagFromDeclSpec->getTypedefNameForAnonDecl()) break; |
John McCall | cde5a40 | 2011-02-01 08:20:08 +0000 | [diff] [blame] | 10309 | |
| 10310 | // A well-formed anonymous tag must always be a TUK_Definition. |
| 10311 | assert(tagFromDeclSpec->isThisDeclarationADefinition()); |
| 10312 | |
| 10313 | // The type must match the tag exactly; no qualifiers allowed. |
| 10314 | if (!Context.hasSameType(T, Context.getTagDeclType(tagFromDeclSpec))) |
| 10315 | break; |
| 10316 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 10317 | // If we've already computed linkage for the anonymous tag, then |
| 10318 | // adding a typedef name for the anonymous decl can change that |
| 10319 | // linkage, which might be a serious problem. Diagnose this as |
| 10320 | // unsupported and ignore the typedef name. TODO: we should |
| 10321 | // pursue this as a language defect and establish a formal rule |
| 10322 | // for how to handle it. |
| 10323 | if (tagFromDeclSpec->hasLinkageBeenComputed()) { |
| 10324 | Diag(D.getIdentifierLoc(), diag::err_typedef_changes_linkage); |
| 10325 | |
| 10326 | SourceLocation tagLoc = D.getDeclSpec().getTypeSpecTypeLoc(); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 10327 | tagLoc = getLocForEndOfToken(tagLoc); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 10328 | |
| 10329 | llvm::SmallString<40> textToInsert; |
| 10330 | textToInsert += ' '; |
| 10331 | textToInsert += D.getIdentifier()->getName(); |
| 10332 | Diag(tagLoc, diag::note_typedef_changes_linkage) |
| 10333 | << FixItHint::CreateInsertion(tagLoc, textToInsert); |
| 10334 | break; |
| 10335 | } |
| 10336 | |
John McCall | cde5a40 | 2011-02-01 08:20:08 +0000 | [diff] [blame] | 10337 | // Otherwise, set this is the anon-decl typedef for the tag. |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 10338 | tagFromDeclSpec->setTypedefNameForAnonDecl(NewTD); |
John McCall | cde5a40 | 2011-02-01 08:20:08 +0000 | [diff] [blame] | 10339 | break; |
| 10340 | } |
| 10341 | |
| 10342 | default: |
| 10343 | break; |
| 10344 | } |
| 10345 | |
Steve Naroff | 5912a35 | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 10346 | return NewTD; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 10347 | } |
| 10348 | |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 10349 | |
Richard Smith | f1c66b4 | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 10350 | /// \brief Check that this is a valid underlying type for an enum declaration. |
| 10351 | bool Sema::CheckEnumUnderlyingType(TypeSourceInfo *TI) { |
| 10352 | SourceLocation UnderlyingLoc = TI->getTypeLoc().getBeginLoc(); |
| 10353 | QualType T = TI->getType(); |
| 10354 | |
Eli Friedman | 2fcff83 | 2012-12-18 02:37:32 +0000 | [diff] [blame] | 10355 | if (T->isDependentType()) |
Richard Smith | f1c66b4 | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 10356 | return false; |
| 10357 | |
Eli Friedman | 2fcff83 | 2012-12-18 02:37:32 +0000 | [diff] [blame] | 10358 | if (const BuiltinType *BT = T->getAs<BuiltinType>()) |
| 10359 | if (BT->isInteger()) |
| 10360 | return false; |
| 10361 | |
Richard Smith | f1c66b4 | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 10362 | Diag(UnderlyingLoc, diag::err_enum_invalid_underlying) << T; |
| 10363 | return true; |
| 10364 | } |
| 10365 | |
| 10366 | /// Check whether this is a valid redeclaration of a previous enumeration. |
| 10367 | /// \return true if the redeclaration was invalid. |
| 10368 | bool Sema::CheckEnumRedeclaration(SourceLocation EnumLoc, bool IsScoped, |
| 10369 | QualType EnumUnderlyingTy, |
| 10370 | const EnumDecl *Prev) { |
| 10371 | bool IsFixed = !EnumUnderlyingTy.isNull(); |
| 10372 | |
| 10373 | if (IsScoped != Prev->isScoped()) { |
| 10374 | Diag(EnumLoc, diag::err_enum_redeclare_scoped_mismatch) |
| 10375 | << Prev->isScoped(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 10376 | Diag(Prev->getLocation(), diag::note_previous_declaration); |
Richard Smith | f1c66b4 | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 10377 | return true; |
| 10378 | } |
| 10379 | |
| 10380 | if (IsFixed && Prev->isFixed()) { |
Richard Smith | 4ca93d9 | 2012-03-26 04:08:46 +0000 | [diff] [blame] | 10381 | if (!EnumUnderlyingTy->isDependentType() && |
| 10382 | !Prev->getIntegerType()->isDependentType() && |
| 10383 | !Context.hasSameUnqualifiedType(EnumUnderlyingTy, |
Richard Smith | f1c66b4 | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 10384 | Prev->getIntegerType())) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 10385 | // TODO: Highlight the underlying type of the redeclaration. |
Richard Smith | f1c66b4 | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 10386 | Diag(EnumLoc, diag::err_enum_redeclare_type_mismatch) |
| 10387 | << EnumUnderlyingTy << Prev->getIntegerType(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 10388 | Diag(Prev->getLocation(), diag::note_previous_declaration) |
| 10389 | << Prev->getIntegerTypeRange(); |
Richard Smith | f1c66b4 | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 10390 | return true; |
| 10391 | } |
| 10392 | } else if (IsFixed != Prev->isFixed()) { |
| 10393 | Diag(EnumLoc, diag::err_enum_redeclare_fixed_mismatch) |
| 10394 | << Prev->isFixed(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 10395 | Diag(Prev->getLocation(), diag::note_previous_declaration); |
Richard Smith | f1c66b4 | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 10396 | return true; |
| 10397 | } |
| 10398 | |
| 10399 | return false; |
| 10400 | } |
| 10401 | |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 10402 | /// \brief Get diagnostic %select index for tag kind for |
| 10403 | /// redeclaration diagnostic message. |
| 10404 | /// WARNING: Indexes apply to particular diagnostics only! |
| 10405 | /// |
| 10406 | /// \returns diagnostic %select index. |
Joao Matos | f143ae9 | 2012-09-01 00:13:24 +0000 | [diff] [blame] | 10407 | static unsigned getRedeclDiagFromTagKind(TagTypeKind Tag) { |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 10408 | switch (Tag) { |
Joao Matos | f143ae9 | 2012-09-01 00:13:24 +0000 | [diff] [blame] | 10409 | case TTK_Struct: return 0; |
| 10410 | case TTK_Interface: return 1; |
| 10411 | case TTK_Class: return 2; |
| 10412 | default: llvm_unreachable("Invalid tag kind for redecl diagnostic!"); |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 10413 | } |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 10414 | } |
| 10415 | |
| 10416 | /// \brief Determine if tag kind is a class-key compatible with |
| 10417 | /// class for redeclaration (class, struct, or __interface). |
| 10418 | /// |
Sylvestre Ledru | f3477c1 | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 10419 | /// \returns true iff the tag kind is compatible. |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 10420 | static bool isClassCompatTagKind(TagTypeKind Tag) |
| 10421 | { |
| 10422 | return Tag == TTK_Struct || Tag == TTK_Class || Tag == TTK_Interface; |
| 10423 | } |
| 10424 | |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 10425 | /// \brief Determine whether a tag with a given kind is acceptable |
| 10426 | /// as a redeclaration of the given tag declaration. |
| 10427 | /// |
| 10428 | /// \returns true if the new tag kind is acceptable, false otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10429 | bool Sema::isAcceptableTagRedeclaration(const TagDecl *Previous, |
Richard Trieu | bbf34c0 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 10430 | TagTypeKind NewTag, bool isDefinition, |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 10431 | SourceLocation NewTagLoc, |
| 10432 | const IdentifierInfo &Name) { |
| 10433 | // C++ [dcl.type.elab]p3: |
| 10434 | // The class-key or enum keyword present in the |
| 10435 | // elaborated-type-specifier shall agree in kind with the |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 10436 | // declaration to which the name in the elaborated-type-specifier |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 10437 | // refers. This rule also applies to the form of |
| 10438 | // elaborated-type-specifier that declares a class-name or |
| 10439 | // friend class since it can be construed as referring to the |
| 10440 | // definition of the class. Thus, in any |
| 10441 | // elaborated-type-specifier, the enum keyword shall be used to |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 10442 | // refer to an enumeration (7.2), the union class-key shall be |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 10443 | // used to refer to a union (clause 9), and either the class or |
| 10444 | // struct class-key shall be used to refer to a class (clause 9) |
| 10445 | // declared using the class or struct class-key. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 10446 | TagTypeKind OldTag = Previous->getTagKind(); |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 10447 | if (!isDefinition || !isClassCompatTagKind(NewTag)) |
Richard Trieu | bbf34c0 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 10448 | if (OldTag == NewTag) |
| 10449 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10450 | |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 10451 | if (isClassCompatTagKind(OldTag) && isClassCompatTagKind(NewTag)) { |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 10452 | // Warn about the struct/class tag mismatch. |
| 10453 | bool isTemplate = false; |
| 10454 | if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Previous)) |
| 10455 | isTemplate = Record->getDescribedClassTemplate(); |
| 10456 | |
Richard Trieu | bbf34c0 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 10457 | if (!ActiveTemplateInstantiations.empty()) { |
| 10458 | // In a template instantiation, do not offer fix-its for tag mismatches |
| 10459 | // since they usually mess up the template instead of fixing the problem. |
| 10460 | Diag(NewTagLoc, diag::warn_struct_class_tag_mismatch) |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 10461 | << getRedeclDiagFromTagKind(NewTag) << isTemplate << &Name |
| 10462 | << getRedeclDiagFromTagKind(OldTag); |
Richard Trieu | bbf34c0 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 10463 | return true; |
| 10464 | } |
| 10465 | |
| 10466 | if (isDefinition) { |
| 10467 | // On definitions, check previous tags and issue a fix-it for each |
| 10468 | // one that doesn't match the current tag. |
| 10469 | if (Previous->getDefinition()) { |
| 10470 | // Don't suggest fix-its for redefinitions. |
| 10471 | return true; |
| 10472 | } |
| 10473 | |
| 10474 | bool previousMismatch = false; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 10475 | for (auto I : Previous->redecls()) { |
Richard Trieu | bbf34c0 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 10476 | if (I->getTagKind() != NewTag) { |
| 10477 | if (!previousMismatch) { |
| 10478 | previousMismatch = true; |
| 10479 | Diag(NewTagLoc, diag::warn_struct_class_previous_tag_mismatch) |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 10480 | << getRedeclDiagFromTagKind(NewTag) << isTemplate << &Name |
| 10481 | << getRedeclDiagFromTagKind(I->getTagKind()); |
Richard Trieu | bbf34c0 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 10482 | } |
| 10483 | Diag(I->getInnerLocStart(), diag::note_struct_class_suggestion) |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 10484 | << getRedeclDiagFromTagKind(NewTag) |
Richard Trieu | bbf34c0 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 10485 | << FixItHint::CreateReplacement(I->getInnerLocStart(), |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 10486 | TypeWithKeyword::getTagTypeKindName(NewTag)); |
Richard Trieu | bbf34c0 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 10487 | } |
| 10488 | } |
| 10489 | return true; |
| 10490 | } |
| 10491 | |
| 10492 | // Check for a previous definition. If current tag and definition |
| 10493 | // are same type, do nothing. If no definition, but disagree with |
| 10494 | // with previous tag type, give a warning, but no fix-it. |
| 10495 | const TagDecl *Redecl = Previous->getDefinition() ? |
| 10496 | Previous->getDefinition() : Previous; |
| 10497 | if (Redecl->getTagKind() == NewTag) { |
| 10498 | return true; |
| 10499 | } |
| 10500 | |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 10501 | Diag(NewTagLoc, diag::warn_struct_class_tag_mismatch) |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 10502 | << getRedeclDiagFromTagKind(NewTag) << isTemplate << &Name |
| 10503 | << getRedeclDiagFromTagKind(OldTag); |
Richard Trieu | bbf34c0 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 10504 | Diag(Redecl->getLocation(), diag::note_previous_use); |
| 10505 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 10506 | // If there is a previous definition, suggest a fix-it. |
Richard Trieu | bbf34c0 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 10507 | if (Previous->getDefinition()) { |
| 10508 | Diag(NewTagLoc, diag::note_struct_class_suggestion) |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 10509 | << getRedeclDiagFromTagKind(Redecl->getTagKind()) |
Richard Trieu | bbf34c0 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 10510 | << FixItHint::CreateReplacement(SourceRange(NewTagLoc), |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 10511 | TypeWithKeyword::getTagTypeKindName(Redecl->getTagKind())); |
Richard Trieu | bbf34c0 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 10512 | } |
| 10513 | |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 10514 | return true; |
| 10515 | } |
| 10516 | return false; |
| 10517 | } |
| 10518 | |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 10519 | /// ActOnTag - This is invoked when we see 'struct foo' or 'struct {'. In the |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 10520 | /// former case, Name will be non-null. In the later case, Name will be null. |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 10521 | /// TagSpec indicates what kind of tag this is. TUK indicates whether this is a |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 10522 | /// reference/declaration/definition of a tag. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 10523 | /// |
| 10524 | /// IsTypeSpecifier is true if this is a type-specifier (or |
| 10525 | /// trailing-type-specifier) other than one in an alias-declaration. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10526 | Decl *Sema::ActOnTag(Scope *S, unsigned TagSpec, TagUseKind TUK, |
Douglas Gregor | 069ea64 | 2010-09-16 23:58:57 +0000 | [diff] [blame] | 10527 | SourceLocation KWLoc, CXXScopeSpec &SS, |
| 10528 | IdentifierInfo *Name, SourceLocation NameLoc, |
| 10529 | AttributeList *Attr, AccessSpecifier AS, |
Douglas Gregor | e761230 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 10530 | SourceLocation ModulePrivateLoc, |
Douglas Gregor | 069ea64 | 2010-09-16 23:58:57 +0000 | [diff] [blame] | 10531 | MultiTemplateParamsArg TemplateParameterLists, |
Abramo Bagnara | a88cefd | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 10532 | bool &OwnedDecl, bool &IsDependent, |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 10533 | SourceLocation ScopedEnumKWLoc, |
| 10534 | bool ScopedEnumUsesClassTag, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 10535 | TypeResult UnderlyingType, |
| 10536 | bool IsTypeSpecifier) { |
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 10537 | // If this is not a definition, it must have a name. |
Douglas Gregor | 6960587 | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 10538 | IdentifierInfo *OrigName = Name; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 10539 | assert((Name != nullptr || TUK == TUK_Definition) && |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 10540 | "Nameless record must be a definition!"); |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 10541 | assert(TemplateParameterLists.size() == 0 || TUK != TUK_Reference); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 10542 | |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 10543 | OwnedDecl = false; |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 10544 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 10545 | bool ScopedEnum = ScopedEnumKWLoc.isValid(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10546 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 10547 | // FIXME: Check explicit specializations more carefully. |
| 10548 | bool isExplicitSpecialization = false; |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 10549 | bool Invalid = false; |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 10550 | |
| 10551 | // We only need to do this matching if we have template parameters |
| 10552 | // or a scope specifier, which also conveniently avoids this work |
| 10553 | // for non-C++ cases. |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 10554 | if (TemplateParameterLists.size() > 0 || |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 10555 | (SS.isNotEmpty() && TUK != TUK_Reference)) { |
Robert Wilhelm | 1169e2f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 10556 | if (TemplateParameterList *TemplateParams = |
| 10557 | MatchTemplateParametersToScopeSpecifier( |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 10558 | KWLoc, NameLoc, SS, nullptr, TemplateParameterLists, |
| 10559 | TUK == TUK_Friend, isExplicitSpecialization, Invalid)) { |
Richard Smith | 725fe0e | 2013-04-01 21:43:41 +0000 | [diff] [blame] | 10560 | if (Kind == TTK_Enum) { |
| 10561 | Diag(KWLoc, diag::err_enum_template); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 10562 | return nullptr; |
Richard Smith | 725fe0e | 2013-04-01 21:43:41 +0000 | [diff] [blame] | 10563 | } |
| 10564 | |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 10565 | if (TemplateParams->size() > 0) { |
Douglas Gregor | 7cdbc58 | 2009-07-22 23:48:44 +0000 | [diff] [blame] | 10566 | // This is a declaration or definition of a class template (which may |
| 10567 | // be a member of another template). |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 10568 | |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 10569 | if (Invalid) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 10570 | return nullptr; |
Abramo Bagnara | c57c17d | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 10571 | |
Douglas Gregor | 7cdbc58 | 2009-07-22 23:48:44 +0000 | [diff] [blame] | 10572 | OwnedDecl = false; |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 10573 | DeclResult Result = CheckClassTemplate(S, TagSpec, TUK, KWLoc, |
Douglas Gregor | 7cdbc58 | 2009-07-22 23:48:44 +0000 | [diff] [blame] | 10574 | SS, Name, NameLoc, Attr, |
Abramo Bagnara | c57c17d | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 10575 | TemplateParams, AS, |
Douglas Gregor | e761230 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 10576 | ModulePrivateLoc, |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 10577 | TemplateParameterLists.size()-1, |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 10578 | TemplateParameterLists.data()); |
Douglas Gregor | 7cdbc58 | 2009-07-22 23:48:44 +0000 | [diff] [blame] | 10579 | return Result.get(); |
| 10580 | } else { |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 10581 | // The "template<>" header is extraneous. |
| 10582 | Diag(TemplateParams->getTemplateLoc(), diag::err_template_tag_noparams) |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 10583 | << TypeWithKeyword::getTagTypeKindName(Kind) << Name; |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 10584 | isExplicitSpecialization = true; |
Douglas Gregor | 7cdbc58 | 2009-07-22 23:48:44 +0000 | [diff] [blame] | 10585 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10586 | } |
| 10587 | } |
| 10588 | |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 10589 | // Figure out the underlying type if this a enum declaration. We need to do |
| 10590 | // this early, because it's needed to detect if this is an incompatible |
| 10591 | // redeclaration. |
| 10592 | llvm::PointerUnion<const Type*, TypeSourceInfo*> EnumUnderlying; |
| 10593 | |
| 10594 | if (Kind == TTK_Enum) { |
| 10595 | if (UnderlyingType.isInvalid() || (!UnderlyingType.get() && ScopedEnum)) |
| 10596 | // No underlying type explicitly specified, or we failed to parse the |
| 10597 | // type, default to int. |
| 10598 | EnumUnderlying = Context.IntTy.getTypePtr(); |
| 10599 | else if (UnderlyingType.get()) { |
| 10600 | // C++0x 7.2p2: The type-specifier-seq of an enum-base shall name an |
| 10601 | // integral type; any cv-qualification is ignored. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 10602 | TypeSourceInfo *TI = nullptr; |
Richard Smith | 878416d | 2012-03-15 00:22:18 +0000 | [diff] [blame] | 10603 | GetTypeFromParser(UnderlyingType.get(), &TI); |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 10604 | EnumUnderlying = TI; |
| 10605 | |
Richard Smith | f1c66b4 | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 10606 | if (CheckEnumUnderlyingType(TI)) |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 10607 | // Recover by falling back to int. |
| 10608 | EnumUnderlying = Context.IntTy.getTypePtr(); |
Douglas Gregor | 0c9e479 | 2010-12-16 00:24:44 +0000 | [diff] [blame] | 10609 | |
Richard Smith | f1c66b4 | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 10610 | if (DiagnoseUnexpandedParameterPack(TI->getTypeLoc().getBeginLoc(), TI, |
Douglas Gregor | 0c9e479 | 2010-12-16 00:24:44 +0000 | [diff] [blame] | 10611 | UPPC_FixedUnderlyingType)) |
| 10612 | EnumUnderlying = Context.IntTy.getTypePtr(); |
| 10613 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 10614 | } else if (getLangOpts().MSVCCompat) |
Francois Pichet | 842e7a2 | 2010-10-18 15:01:13 +0000 | [diff] [blame] | 10615 | // Microsoft enums are always of int type. |
| 10616 | EnumUnderlying = Context.IntTy.getTypePtr(); |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 10617 | } |
| 10618 | |
Douglas Gregor | 4920f1f | 2009-01-12 22:49:06 +0000 | [diff] [blame] | 10619 | DeclContext *SearchDC = CurContext; |
Argyrios Kyrtzidis | 0f84a23 | 2008-11-09 22:53:32 +0000 | [diff] [blame] | 10620 | DeclContext *DC = CurContext; |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 10621 | bool isStdBadAlloc = false; |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 10622 | |
Chandler Carruth | 7bf3600 | 2010-03-01 21:17:36 +0000 | [diff] [blame] | 10623 | RedeclarationKind Redecl = ForRedeclaration; |
| 10624 | if (TUK == TUK_Friend || TUK == TUK_Reference) |
| 10625 | Redecl = NotForRedeclaration; |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 10626 | |
| 10627 | LookupResult Previous(*this, Name, NameLoc, LookupTagName, Redecl); |
Douglas Gregor | d943352 | 2013-06-27 20:42:30 +0000 | [diff] [blame] | 10628 | bool FriendSawTagOutsideEnclosingNamespace = false; |
Argyrios Kyrtzidis | 630c81b | 2008-11-09 22:09:58 +0000 | [diff] [blame] | 10629 | if (Name && SS.isNotEmpty()) { |
| 10630 | // We have a nested-name tag ('struct foo::bar'). |
| 10631 | |
| 10632 | // Check for invalid 'foo::'. |
Argyrios Kyrtzidis | 0f84a23 | 2008-11-09 22:53:32 +0000 | [diff] [blame] | 10633 | if (SS.isInvalid()) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 10634 | Name = nullptr; |
Argyrios Kyrtzidis | 630c81b | 2008-11-09 22:09:58 +0000 | [diff] [blame] | 10635 | goto CreateNewDecl; |
| 10636 | } |
| 10637 | |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 10638 | // If this is a friend or a reference to a class in a dependent |
| 10639 | // context, don't try to make a decl for it. |
| 10640 | if (TUK == TUK_Friend || TUK == TUK_Reference) { |
| 10641 | DC = computeDeclContext(SS, false); |
| 10642 | if (!DC) { |
| 10643 | IsDependent = true; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 10644 | return nullptr; |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 10645 | } |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 10646 | } else { |
| 10647 | DC = computeDeclContext(SS, true); |
| 10648 | if (!DC) { |
| 10649 | Diag(SS.getRange().getBegin(), diag::err_dependent_nested_name_spec) |
| 10650 | << SS.getRange(); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 10651 | return nullptr; |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 10652 | } |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 10653 | } |
| 10654 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 10655 | if (RequireCompleteDeclContext(SS, DC)) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 10656 | return nullptr; |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 10657 | |
Douglas Gregor | 1931b44 | 2009-02-03 00:34:39 +0000 | [diff] [blame] | 10658 | SearchDC = DC; |
Argyrios Kyrtzidis | 0f84a23 | 2008-11-09 22:53:32 +0000 | [diff] [blame] | 10659 | // Look-up name inside 'foo::'. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 10660 | LookupQualifiedName(Previous, DC); |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 10661 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 10662 | if (Previous.isAmbiguous()) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 10663 | return nullptr; |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 10664 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 10665 | if (Previous.empty()) { |
Douglas Gregor | 9edad9b | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 10666 | // Name lookup did not find anything. However, if the |
| 10667 | // nested-name-specifier refers to the current instantiation, |
| 10668 | // and that current instantiation has any dependent base |
| 10669 | // classes, we might find something at instantiation time: treat |
| 10670 | // this as a dependent elaborated-type-specifier. |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 10671 | // But this only makes any sense for reference-like lookups. |
| 10672 | if (Previous.wasNotFoundInCurrentInstantiation() && |
| 10673 | (TUK == TUK_Reference || TUK == TUK_Friend)) { |
Douglas Gregor | 9edad9b | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 10674 | IsDependent = true; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 10675 | return nullptr; |
Douglas Gregor | 9edad9b | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 10676 | } |
| 10677 | |
| 10678 | // A tag 'foo::bar' must already exist. |
Douglas Gregor | 1eabb7d | 2010-03-31 23:17:41 +0000 | [diff] [blame] | 10679 | Diag(NameLoc, diag::err_not_tag_in_scope) |
| 10680 | << Kind << Name << DC << SS.getRange(); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 10681 | Name = nullptr; |
Douglas Gregor | d0c8737 | 2009-05-27 17:30:49 +0000 | [diff] [blame] | 10682 | Invalid = true; |
Argyrios Kyrtzidis | 630c81b | 2008-11-09 22:09:58 +0000 | [diff] [blame] | 10683 | goto CreateNewDecl; |
| 10684 | } |
Chris Lattner | cf79b01 | 2009-01-21 02:38:50 +0000 | [diff] [blame] | 10685 | } else if (Name) { |
Argyrios Kyrtzidis | 630c81b | 2008-11-09 22:09:58 +0000 | [diff] [blame] | 10686 | // If this is a named struct, check to see if there was a previous forward |
| 10687 | // declaration or definition. |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 10688 | // FIXME: We're looking into outer scopes here, even when we |
| 10689 | // shouldn't be. Doing so can result in ambiguities that we |
| 10690 | // shouldn't be diagnosing. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 10691 | LookupName(Previous, S); |
| 10692 | |
John McCall | c96cd7a | 2013-03-20 01:53:00 +0000 | [diff] [blame] | 10693 | // When declaring or defining a tag, ignore ambiguities introduced |
| 10694 | // by types using'ed into this scope. |
Douglas Gregor | 93b6bce | 2011-05-09 21:46:33 +0000 | [diff] [blame] | 10695 | if (Previous.isAmbiguous() && |
| 10696 | (TUK == TUK_Definition || TUK == TUK_Declaration)) { |
Douglas Gregor | 61c6c44 | 2011-05-04 00:25:33 +0000 | [diff] [blame] | 10697 | LookupResult::Filter F = Previous.makeFilter(); |
| 10698 | while (F.hasNext()) { |
| 10699 | NamedDecl *ND = F.next(); |
| 10700 | if (ND->getDeclContext()->getRedeclContext() != SearchDC) |
| 10701 | F.erase(); |
| 10702 | } |
| 10703 | F.done(); |
Douglas Gregor | 61c6c44 | 2011-05-04 00:25:33 +0000 | [diff] [blame] | 10704 | } |
John McCall | c96cd7a | 2013-03-20 01:53:00 +0000 | [diff] [blame] | 10705 | |
| 10706 | // C++11 [namespace.memdef]p3: |
| 10707 | // If the name in a friend declaration is neither qualified nor |
| 10708 | // a template-id and the declaration is a function or an |
| 10709 | // elaborated-type-specifier, the lookup to determine whether |
| 10710 | // the entity has been previously declared shall not consider |
| 10711 | // any scopes outside the innermost enclosing namespace. |
| 10712 | // |
| 10713 | // Does it matter that this should be by scope instead of by |
| 10714 | // semantic context? |
| 10715 | if (!Previous.empty() && TUK == TUK_Friend) { |
| 10716 | DeclContext *EnclosingNS = SearchDC->getEnclosingNamespaceContext(); |
| 10717 | LookupResult::Filter F = Previous.makeFilter(); |
| 10718 | while (F.hasNext()) { |
| 10719 | NamedDecl *ND = F.next(); |
| 10720 | DeclContext *DC = ND->getDeclContext()->getRedeclContext(); |
Douglas Gregor | d943352 | 2013-06-27 20:42:30 +0000 | [diff] [blame] | 10721 | if (DC->isFileContext() && |
| 10722 | !EnclosingNS->Encloses(ND->getDeclContext())) { |
John McCall | c96cd7a | 2013-03-20 01:53:00 +0000 | [diff] [blame] | 10723 | F.erase(); |
Douglas Gregor | d943352 | 2013-06-27 20:42:30 +0000 | [diff] [blame] | 10724 | FriendSawTagOutsideEnclosingNamespace = true; |
| 10725 | } |
John McCall | c96cd7a | 2013-03-20 01:53:00 +0000 | [diff] [blame] | 10726 | } |
| 10727 | F.done(); |
| 10728 | } |
Douglas Gregor | 61c6c44 | 2011-05-04 00:25:33 +0000 | [diff] [blame] | 10729 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 10730 | // Note: there used to be some attempt at recovery here. |
| 10731 | if (Previous.isAmbiguous()) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 10732 | return nullptr; |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 10733 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10734 | if (!getLangOpts().CPlusPlus && TUK != TUK_Reference) { |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 10735 | // FIXME: This makes sure that we ignore the contexts associated |
| 10736 | // with C structs, unions, and enums when looking for a matching |
| 10737 | // tag declaration or definition. See the similar lookup tweak |
Douglas Gregor | 4c921ae | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 10738 | // in Sema::LookupName; is there a better way to deal with this? |
Douglas Gregor | 4920f1f | 2009-01-12 22:49:06 +0000 | [diff] [blame] | 10739 | while (isa<RecordDecl>(SearchDC) || isa<EnumDecl>(SearchDC)) |
| 10740 | SearchDC = SearchDC->getParent(); |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 10741 | } |
Douglas Gregor | 069ea64 | 2010-09-16 23:58:57 +0000 | [diff] [blame] | 10742 | } else if (S->isFunctionPrototypeScope()) { |
| 10743 | // If this is an enum declaration in function prototype scope, set its |
| 10744 | // initial context to the translation unit. |
Nick Lewycky | 8d17681 | 2012-03-10 07:45:33 +0000 | [diff] [blame] | 10745 | // FIXME: [citation needed] |
Douglas Gregor | 069ea64 | 2010-09-16 23:58:57 +0000 | [diff] [blame] | 10746 | SearchDC = Context.getTranslationUnitDecl(); |
Argyrios Kyrtzidis | 630c81b | 2008-11-09 22:09:58 +0000 | [diff] [blame] | 10747 | } |
| 10748 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 10749 | if (Previous.isSingleResult() && |
| 10750 | Previous.getFoundDecl()->isTemplateParameter()) { |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 10751 | // Maybe we will complain about the shadowed template parameter. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 10752 | DiagnoseTemplateParameterShadow(NameLoc, Previous.getFoundDecl()); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 10753 | // Just pretend that we didn't see the previous declaration. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 10754 | Previous.clear(); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 10755 | } |
| 10756 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10757 | if (getLangOpts().CPlusPlus && Name && DC && StdNamespace && |
Argyrios Kyrtzidis | 76c38d3 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 10758 | DC->Equals(getStdNamespace()) && Name->isStr("bad_alloc")) { |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 10759 | // This is a declaration of or a reference to "std::bad_alloc". |
| 10760 | isStdBadAlloc = true; |
| 10761 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 10762 | if (Previous.empty() && StdBadAlloc) { |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 10763 | // std::bad_alloc has been implicitly declared (but made invisible to |
| 10764 | // name lookup). Fill in this implicit declaration as the previous |
| 10765 | // declaration, so that the declarations get chained appropriately. |
Argyrios Kyrtzidis | 76c38d3 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 10766 | Previous.addDecl(getStdBadAlloc()); |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 10767 | } |
| 10768 | } |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 10769 | |
John McCall | 9c86b51 | 2010-03-25 21:28:06 +0000 | [diff] [blame] | 10770 | // If we didn't find a previous declaration, and this is a reference |
| 10771 | // (or friend reference), move to the correct scope. In C++, we |
| 10772 | // also need to do a redeclaration lookup there, just in case |
| 10773 | // there's a shadow friend decl. |
| 10774 | if (Name && Previous.empty() && |
| 10775 | (TUK == TUK_Reference || TUK == TUK_Friend)) { |
| 10776 | if (Invalid) goto CreateNewDecl; |
| 10777 | assert(SS.isEmpty()); |
| 10778 | |
| 10779 | if (TUK == TUK_Reference) { |
| 10780 | // C++ [basic.scope.pdecl]p5: |
| 10781 | // -- for an elaborated-type-specifier of the form |
| 10782 | // |
| 10783 | // class-key identifier |
| 10784 | // |
| 10785 | // if the elaborated-type-specifier is used in the |
| 10786 | // decl-specifier-seq or parameter-declaration-clause of a |
| 10787 | // function defined in namespace scope, the identifier is |
| 10788 | // declared as a class-name in the namespace that contains |
| 10789 | // the declaration; otherwise, except as a friend |
| 10790 | // declaration, the identifier is declared in the smallest |
| 10791 | // non-class, non-function-prototype scope that contains the |
| 10792 | // declaration. |
| 10793 | // |
| 10794 | // C99 6.7.2.3p8 has a similar (but not identical!) provision for |
| 10795 | // C structs and unions. |
| 10796 | // |
| 10797 | // It is an error in C++ to declare (rather than define) an enum |
| 10798 | // type, including via an elaborated type specifier. We'll |
| 10799 | // diagnose that later; for now, declare the enum in the same |
| 10800 | // scope as we would have picked for any other tag type. |
| 10801 | // |
| 10802 | // GNU C also supports this behavior as part of its incomplete |
| 10803 | // enum types extension, while GNU C++ does not. |
| 10804 | // |
| 10805 | // Find the context where we'll be declaring the tag. |
| 10806 | // FIXME: We would like to maintain the current DeclContext as the |
| 10807 | // lexical context, |
Nick Lewycky | 1659c37 | 2012-03-10 07:47:07 +0000 | [diff] [blame] | 10808 | while (!SearchDC->isFileContext() && !SearchDC->isFunctionOrMethod()) |
John McCall | 9c86b51 | 2010-03-25 21:28:06 +0000 | [diff] [blame] | 10809 | SearchDC = SearchDC->getParent(); |
| 10810 | |
| 10811 | // Find the scope where we'll be declaring the tag. |
| 10812 | while (S->isClassScope() || |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10813 | (getLangOpts().CPlusPlus && |
John McCall | 9c86b51 | 2010-03-25 21:28:06 +0000 | [diff] [blame] | 10814 | S->isFunctionPrototypeScope()) || |
| 10815 | ((S->getFlags() & Scope::DeclScope) == 0) || |
Ted Kremenek | f0d5861 | 2013-10-08 17:08:03 +0000 | [diff] [blame] | 10816 | (S->getEntity() && S->getEntity()->isTransparentContext())) |
John McCall | 9c86b51 | 2010-03-25 21:28:06 +0000 | [diff] [blame] | 10817 | S = S->getParent(); |
| 10818 | } else { |
| 10819 | assert(TUK == TUK_Friend); |
| 10820 | // C++ [namespace.memdef]p3: |
| 10821 | // If a friend declaration in a non-local class first declares a |
| 10822 | // class or function, the friend class or function is a member of |
| 10823 | // the innermost enclosing namespace. |
| 10824 | SearchDC = SearchDC->getEnclosingNamespaceContext(); |
John McCall | 9c86b51 | 2010-03-25 21:28:06 +0000 | [diff] [blame] | 10825 | } |
| 10826 | |
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 10827 | // In C++, we need to do a redeclaration lookup to properly |
| 10828 | // diagnose some problems. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10829 | if (getLangOpts().CPlusPlus) { |
John McCall | 9c86b51 | 2010-03-25 21:28:06 +0000 | [diff] [blame] | 10830 | Previous.setRedeclarationKind(ForRedeclaration); |
| 10831 | LookupQualifiedName(Previous, SearchDC); |
| 10832 | } |
| 10833 | } |
| 10834 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 10835 | if (!Previous.empty()) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 10836 | NamedDecl *PrevDecl = Previous.getFoundDecl(); |
| 10837 | NamedDecl *DirectPrevDecl = |
| 10838 | getLangOpts().MSVCCompat ? *Previous.begin() : PrevDecl; |
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 10839 | |
| 10840 | // It's okay to have a tag decl in the same scope as a typedef |
| 10841 | // which hides a tag decl in the same scope. Finding this |
| 10842 | // insanity with a redeclaration lookup can only actually happen |
| 10843 | // in C++. |
| 10844 | // |
| 10845 | // This is also okay for elaborated-type-specifiers, which is |
| 10846 | // technically forbidden by the current standard but which is |
| 10847 | // okay according to the likely resolution of an open issue; |
| 10848 | // see http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#407 |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10849 | if (getLangOpts().CPlusPlus) { |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 10850 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(PrevDecl)) { |
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 10851 | if (const TagType *TT = TD->getUnderlyingType()->getAs<TagType>()) { |
| 10852 | TagDecl *Tag = TT->getDecl(); |
| 10853 | if (Tag->getDeclName() == Name && |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 10854 | Tag->getDeclContext()->getRedeclContext() |
| 10855 | ->Equals(TD->getDeclContext()->getRedeclContext())) { |
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 10856 | PrevDecl = Tag; |
| 10857 | Previous.clear(); |
| 10858 | Previous.addDecl(Tag); |
Douglas Gregor | 757c600 | 2010-08-27 22:55:10 +0000 | [diff] [blame] | 10859 | Previous.resolveKind(); |
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 10860 | } |
| 10861 | } |
| 10862 | } |
| 10863 | } |
| 10864 | |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 10865 | if (TagDecl *PrevTagDecl = dyn_cast<TagDecl>(PrevDecl)) { |
Chris Lattner | 14943b9 | 2008-07-03 03:30:58 +0000 | [diff] [blame] | 10866 | // If this is a use of a previous tag, or if the tag is already declared |
| 10867 | // in the same scope (so that the definition/declaration completes or |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 10868 | // rementions the tag), reuse the decl. |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 10869 | if (TUK == TUK_Reference || TUK == TUK_Friend || |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 10870 | isDeclInScope(DirectPrevDecl, SearchDC, S, |
| 10871 | SS.isNotEmpty() || isExplicitSpecialization)) { |
Chris Lattner | 14943b9 | 2008-07-03 03:30:58 +0000 | [diff] [blame] | 10872 | // Make sure that this wasn't declared as an enum and now used as a |
| 10873 | // struct or something similar. |
Richard Trieu | bbf34c0 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 10874 | if (!isAcceptableTagRedeclaration(PrevTagDecl, Kind, |
| 10875 | TUK == TUK_Definition, KWLoc, |
| 10876 | *Name)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10877 | bool SafeToContinue |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 10878 | = (PrevTagDecl->getTagKind() != TTK_Enum && |
| 10879 | Kind != TTK_Enum); |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 10880 | if (SafeToContinue) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10881 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 10882 | << Name |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 10883 | << FixItHint::CreateReplacement(SourceRange(KWLoc), |
| 10884 | PrevTagDecl->getKindName()); |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 10885 | else |
| 10886 | Diag(KWLoc, diag::err_use_with_wrong_tag) << Name; |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 10887 | Diag(PrevTagDecl->getLocation(), diag::note_previous_use); |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 10888 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10889 | if (SafeToContinue) |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 10890 | Kind = PrevTagDecl->getTagKind(); |
| 10891 | else { |
| 10892 | // Recover by making this an anonymous redefinition. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 10893 | Name = nullptr; |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 10894 | Previous.clear(); |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 10895 | Invalid = true; |
| 10896 | } |
| 10897 | } |
| 10898 | |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 10899 | if (Kind == TTK_Enum && PrevTagDecl->getTagKind() == TTK_Enum) { |
| 10900 | const EnumDecl *PrevEnum = cast<EnumDecl>(PrevTagDecl); |
| 10901 | |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 10902 | // If this is an elaborated-type-specifier for a scoped enumeration, |
| 10903 | // the 'class' keyword is not necessary and not permitted. |
| 10904 | if (TUK == TUK_Reference || TUK == TUK_Friend) { |
| 10905 | if (ScopedEnum) |
| 10906 | Diag(ScopedEnumKWLoc, diag::err_enum_class_reference) |
| 10907 | << PrevEnum->isScoped() |
| 10908 | << FixItHint::CreateRemoval(ScopedEnumKWLoc); |
| 10909 | return PrevTagDecl; |
| 10910 | } |
| 10911 | |
Richard Smith | f1c66b4 | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 10912 | QualType EnumUnderlyingTy; |
| 10913 | if (TypeSourceInfo *TI = EnumUnderlying.dyn_cast<TypeSourceInfo*>()) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 10914 | EnumUnderlyingTy = TI->getType().getUnqualifiedType(); |
Richard Smith | f1c66b4 | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 10915 | else if (const Type *T = EnumUnderlying.dyn_cast<const Type*>()) |
| 10916 | EnumUnderlyingTy = QualType(T, 0); |
| 10917 | |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 10918 | // All conflicts with previous declarations are recovered by |
Richard Smith | 3343fad | 2012-03-23 23:09:08 +0000 | [diff] [blame] | 10919 | // returning the previous declaration, unless this is a definition, |
| 10920 | // in which case we want the caller to bail out. |
Richard Smith | f1c66b4 | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 10921 | if (CheckEnumRedeclaration(NameLoc.isValid() ? NameLoc : KWLoc, |
| 10922 | ScopedEnum, EnumUnderlyingTy, PrevEnum)) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 10923 | return TUK == TUK_Declaration ? PrevTagDecl : nullptr; |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 10924 | } |
| 10925 | |
David Majnemer | 2ec2b84 | 2013-06-11 03:51:23 +0000 | [diff] [blame] | 10926 | // C++11 [class.mem]p1: |
David Majnemer | 0f9b855 | 2013-06-11 06:19:45 +0000 | [diff] [blame] | 10927 | // A member shall not be declared twice in the member-specification, |
David Majnemer | 2ec2b84 | 2013-06-11 03:51:23 +0000 | [diff] [blame] | 10928 | // except that a nested class or member class template can be declared |
| 10929 | // and then later defined. |
| 10930 | if (TUK == TUK_Declaration && PrevDecl->isCXXClassMember() && |
| 10931 | S->isDeclScope(PrevDecl)) { |
| 10932 | Diag(NameLoc, diag::ext_member_redeclared); |
| 10933 | Diag(PrevTagDecl->getLocation(), diag::note_previous_declaration); |
| 10934 | } |
| 10935 | |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 10936 | if (!Invalid) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 10937 | // If this is a use, just return the declaration we found, unless |
| 10938 | // we have attributes. |
Chris Lattner | 14943b9 | 2008-07-03 03:30:58 +0000 | [diff] [blame] | 10939 | |
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 10940 | // FIXME: In the future, return a variant or some other clue |
| 10941 | // for the consumer of this Decl to know it doesn't own it. |
| 10942 | // For our current ASTs this shouldn't be a problem, but will |
| 10943 | // need to be changed with DeclGroups. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 10944 | if (!Attr && |
| 10945 | ((TUK == TUK_Reference && |
| 10946 | (!PrevTagDecl->getFriendObjectKind() || getLangOpts().MicrosoftExt)) |
| 10947 | || TUK == TUK_Friend)) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10948 | return PrevTagDecl; |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 10949 | |
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 10950 | // Diagnose attempts to redefine a tag. |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 10951 | if (TUK == TUK_Definition) { |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 10952 | if (TagDecl *Def = PrevTagDecl->getDefinition()) { |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 10953 | // If we're defining a specialization and the previous definition |
| 10954 | // is from an implicit instantiation, don't emit an error |
| 10955 | // here; we'll catch this in the general case below. |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 10956 | bool IsExplicitSpecializationAfterInstantiation = false; |
| 10957 | if (isExplicitSpecialization) { |
| 10958 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Def)) |
| 10959 | IsExplicitSpecializationAfterInstantiation = |
| 10960 | RD->getTemplateSpecializationKind() != |
| 10961 | TSK_ExplicitSpecialization; |
| 10962 | else if (EnumDecl *ED = dyn_cast<EnumDecl>(Def)) |
| 10963 | IsExplicitSpecializationAfterInstantiation = |
| 10964 | ED->getTemplateSpecializationKind() != |
| 10965 | TSK_ExplicitSpecialization; |
| 10966 | } |
| 10967 | |
| 10968 | if (!IsExplicitSpecializationAfterInstantiation) { |
James Molloy | 16f1f71 | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 10969 | // A redeclaration in function prototype scope in C isn't |
| 10970 | // visible elsewhere, so merely issue a warning. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10971 | if (!getLangOpts().CPlusPlus && S->containedInPrototypeScope()) |
James Molloy | 16f1f71 | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 10972 | Diag(NameLoc, diag::warn_redefinition_in_param_list) << Name; |
| 10973 | else |
| 10974 | Diag(NameLoc, diag::err_redefinition) << Name; |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 10975 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 10976 | // If this is a redefinition, recover by making this |
| 10977 | // struct be anonymous, which will make any later |
| 10978 | // references get the previous definition. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 10979 | Name = nullptr; |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 10980 | Previous.clear(); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 10981 | Invalid = true; |
| 10982 | } |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 10983 | } else { |
| 10984 | // If the type is currently being defined, complain |
| 10985 | // about a nested redefinition. |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 10986 | const TagType *Tag |
| 10987 | = cast<TagType>(Context.getTagDeclType(PrevTagDecl)); |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 10988 | if (Tag->isBeingDefined()) { |
| 10989 | Diag(NameLoc, diag::err_nested_redefinition) << Name; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10990 | Diag(PrevTagDecl->getLocation(), |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 10991 | diag::note_previous_definition); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 10992 | Name = nullptr; |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 10993 | Previous.clear(); |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 10994 | Invalid = true; |
| 10995 | } |
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 10996 | } |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 10997 | |
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 10998 | // Okay, this is definition of a previously declared or referenced |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 10999 | // tag. We're going to create a new Decl for it. |
| 11000 | } |
| 11001 | |
| 11002 | // Okay, we're going to make a redeclaration. If this is some kind |
| 11003 | // of reference, make sure we build the redeclaration in the same DC |
| 11004 | // as the original, and ignore the current access specifier. |
| 11005 | if (TUK == TUK_Friend || TUK == TUK_Reference) { |
| 11006 | SearchDC = PrevTagDecl->getDeclContext(); |
| 11007 | AS = AS_none; |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 11008 | } |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 11009 | } |
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 11010 | // If we get here we have (another) forward declaration or we |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 11011 | // have a definition. Just create a new decl. |
| 11012 | |
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 11013 | } else { |
| 11014 | // If we get here, this is a definition of a new tag type in a nested |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11015 | // scope, e.g. "struct foo; void bar() { struct foo; }", just create a |
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 11016 | // new decl/type. We set PrevDecl to NULL so that the entities |
| 11017 | // have distinct types. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 11018 | Previous.clear(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 11019 | } |
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 11020 | // If we get here, we're going to create a new Decl. If PrevDecl |
| 11021 | // is non-NULL, it's a definition of the tag declared by |
| 11022 | // PrevDecl. If it's NULL, we have a new definition. |
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 11023 | |
| 11024 | |
| 11025 | // Otherwise, PrevDecl is not a tag, but was found with tag |
| 11026 | // lookup. This is only actually possible in C++, where a few |
| 11027 | // things like templates still live in the tag namespace. |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 11028 | } else { |
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 11029 | // Use a better diagnostic if an elaborated-type-specifier |
| 11030 | // found the wrong kind of type on the first |
| 11031 | // (non-redeclaration) lookup. |
| 11032 | if ((TUK == TUK_Reference || TUK == TUK_Friend) && |
| 11033 | !Previous.isForRedeclaration()) { |
| 11034 | unsigned Kind = 0; |
| 11035 | if (isa<TypedefDecl>(PrevDecl)) Kind = 1; |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 11036 | else if (isa<TypeAliasDecl>(PrevDecl)) Kind = 2; |
| 11037 | else if (isa<ClassTemplateDecl>(PrevDecl)) Kind = 3; |
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 11038 | Diag(NameLoc, diag::err_tag_reference_non_tag) << Kind; |
| 11039 | Diag(PrevDecl->getLocation(), diag::note_declared_at); |
| 11040 | Invalid = true; |
| 11041 | |
| 11042 | // Otherwise, only diagnose if the declaration is in scope. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 11043 | } else if (!isDeclInScope(PrevDecl, SearchDC, S, |
| 11044 | SS.isNotEmpty() || isExplicitSpecialization)) { |
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 11045 | // do nothing |
| 11046 | |
| 11047 | // Diagnose implicit declarations introduced by elaborated types. |
| 11048 | } else if (TUK == TUK_Reference || TUK == TUK_Friend) { |
| 11049 | unsigned Kind = 0; |
| 11050 | if (isa<TypedefDecl>(PrevDecl)) Kind = 1; |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 11051 | else if (isa<TypeAliasDecl>(PrevDecl)) Kind = 2; |
| 11052 | else if (isa<ClassTemplateDecl>(PrevDecl)) Kind = 3; |
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 11053 | Diag(NameLoc, diag::err_tag_reference_conflict) << Kind; |
| 11054 | Diag(PrevDecl->getLocation(), diag::note_previous_decl) << PrevDecl; |
| 11055 | Invalid = true; |
| 11056 | |
| 11057 | // Otherwise it's a declaration. Call out a particularly common |
| 11058 | // case here. |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 11059 | } else if (TypedefNameDecl *TND = dyn_cast<TypedefNameDecl>(PrevDecl)) { |
| 11060 | unsigned Kind = 0; |
| 11061 | if (isa<TypeAliasDecl>(PrevDecl)) Kind = 1; |
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 11062 | Diag(NameLoc, diag::err_tag_definition_of_typedef) |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 11063 | << Name << Kind << TND->getUnderlyingType(); |
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 11064 | Diag(PrevDecl->getLocation(), diag::note_previous_decl) << PrevDecl; |
| 11065 | Invalid = true; |
| 11066 | |
| 11067 | // Otherwise, diagnose. |
| 11068 | } else { |
| 11069 | // The tag name clashes with something else in the target scope, |
| 11070 | // issue an error and recover by making this tag be anonymous. |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 11071 | Diag(NameLoc, diag::err_redefinition_different_kind) << Name; |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 11072 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 11073 | Name = nullptr; |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 11074 | Invalid = true; |
Argyrios Kyrtzidis | b02ef24 | 2008-07-16 07:45:46 +0000 | [diff] [blame] | 11075 | } |
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 11076 | |
| 11077 | // The existing declaration isn't relevant to us; we're in a |
| 11078 | // new scope, so clear out the previous declaration. |
| 11079 | Previous.clear(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 11080 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 11081 | } |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 11082 | |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 11083 | CreateNewDecl: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11084 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 11085 | TagDecl *PrevDecl = nullptr; |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 11086 | if (Previous.isSingleResult()) |
| 11087 | PrevDecl = cast<TagDecl>(Previous.getFoundDecl()); |
| 11088 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 11089 | // If there is an identifier, use the location of the identifier as the |
| 11090 | // location of the decl, otherwise use the location of the struct/union |
| 11091 | // keyword. |
| 11092 | SourceLocation Loc = NameLoc.isValid() ? NameLoc : KWLoc; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11093 | |
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 11094 | // Otherwise, create a new declaration. If there is a previous |
| 11095 | // declaration of the same entity, the two will be linked via |
| 11096 | // PrevDecl. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 11097 | TagDecl *New; |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 11098 | |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 11099 | bool IsForwardReference = false; |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 11100 | if (Kind == TTK_Enum) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 11101 | // FIXME: Tag decls should be chained to any simultaneous vardecls, e.g.: |
| 11102 | // enum X { A, B, C } D; D should chain to X. |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 11103 | New = EnumDecl::Create(Context, SearchDC, KWLoc, Loc, Name, |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 11104 | cast_or_null<EnumDecl>(PrevDecl), ScopedEnum, |
Abramo Bagnara | a88cefd | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 11105 | ScopedEnumUsesClassTag, !EnumUnderlying.isNull()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 11106 | // If this is an undefined enum, warn. |
Douglas Gregor | f3a7b7c | 2010-06-22 14:26:35 +0000 | [diff] [blame] | 11107 | if (TUK != TUK_Definition && !Invalid) { |
| 11108 | TagDecl *Def; |
Douglas Gregor | abde2c7 | 2013-03-25 22:22:35 +0000 | [diff] [blame] | 11109 | if ((getLangOpts().CPlusPlus11 || getLangOpts().ObjC2) && |
| 11110 | cast<EnumDecl>(New)->isFixed()) { |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 11111 | // C++0x: 7.2p2: opaque-enum-declaration. |
| 11112 | // Conflicts are diagnosed above. Do nothing. |
| 11113 | } |
| 11114 | else if (PrevDecl && (Def = cast<EnumDecl>(PrevDecl)->getDefinition())) { |
Douglas Gregor | f3a7b7c | 2010-06-22 14:26:35 +0000 | [diff] [blame] | 11115 | Diag(Loc, diag::ext_forward_ref_enum_def) |
| 11116 | << New; |
| 11117 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 11118 | } else { |
Francois Pichet | 8dc3abc | 2010-09-12 05:06:55 +0000 | [diff] [blame] | 11119 | unsigned DiagID = diag::ext_forward_ref_enum; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 11120 | if (getLangOpts().MSVCCompat) |
Francois Pichet | 8dc3abc | 2010-09-12 05:06:55 +0000 | [diff] [blame] | 11121 | DiagID = diag::ext_ms_forward_ref_enum; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 11122 | else if (getLangOpts().CPlusPlus) |
Francois Pichet | 8dc3abc | 2010-09-12 05:06:55 +0000 | [diff] [blame] | 11123 | DiagID = diag::err_forward_ref_enum; |
| 11124 | Diag(Loc, DiagID); |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 11125 | |
| 11126 | // If this is a forward-declared reference to an enumeration, make a |
| 11127 | // note of it; we won't actually be introducing the declaration into |
| 11128 | // the declaration context. |
| 11129 | if (TUK == TUK_Reference) |
| 11130 | IsForwardReference = true; |
Douglas Gregor | f3a7b7c | 2010-06-22 14:26:35 +0000 | [diff] [blame] | 11131 | } |
Douglas Gregor | 80711a2 | 2009-03-06 18:34:03 +0000 | [diff] [blame] | 11132 | } |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 11133 | |
| 11134 | if (EnumUnderlying) { |
| 11135 | EnumDecl *ED = cast<EnumDecl>(New); |
| 11136 | if (TypeSourceInfo *TI = EnumUnderlying.dyn_cast<TypeSourceInfo*>()) |
| 11137 | ED->setIntegerTypeSourceInfo(TI); |
| 11138 | else |
| 11139 | ED->setIntegerType(QualType(EnumUnderlying.get<const Type*>(), 0)); |
| 11140 | ED->setPromotionType(ED->getIntegerType()); |
| 11141 | } |
| 11142 | |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 11143 | } else { |
| 11144 | // struct/union/class |
| 11145 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 11146 | // FIXME: Tag decls should be chained to any simultaneous vardecls, e.g.: |
| 11147 | // struct X { int A; } D; D should chain to X. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 11148 | if (getLangOpts().CPlusPlus) { |
Ted Kremenek | 2b345eb | 2008-09-05 17:39:33 +0000 | [diff] [blame] | 11149 | // FIXME: Look for a way to use RecordDecl for simple structs. |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 11150 | New = CXXRecordDecl::Create(Context, Kind, SearchDC, KWLoc, Loc, Name, |
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 11151 | cast_or_null<CXXRecordDecl>(PrevDecl)); |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 11152 | |
Argyrios Kyrtzidis | 76c38d3 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 11153 | if (isStdBadAlloc && (!StdBadAlloc || getStdBadAlloc()->isImplicit())) |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 11154 | StdBadAlloc = cast<CXXRecordDecl>(New); |
| 11155 | } else |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 11156 | New = RecordDecl::Create(Context, Kind, SearchDC, KWLoc, Loc, Name, |
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 11157 | cast_or_null<RecordDecl>(PrevDecl)); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 11158 | } |
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 11159 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 11160 | // C++11 [dcl.type]p3: |
| 11161 | // A type-specifier-seq shall not define a class or enumeration [...]. |
| 11162 | if (getLangOpts().CPlusPlus && IsTypeSpecifier && TUK == TUK_Definition) { |
| 11163 | Diag(New->getLocation(), diag::err_type_defined_in_type_specifier) |
| 11164 | << Context.getTagDeclType(New); |
| 11165 | Invalid = true; |
| 11166 | } |
| 11167 | |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 11168 | // Maybe add qualifier info. |
| 11169 | if (SS.isNotEmpty()) { |
Fariborz Jahanian | 4fb2053 | 2010-05-14 21:35:02 +0000 | [diff] [blame] | 11170 | if (SS.isSet()) { |
Douglas Gregor | 6960587 | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 11171 | // If this is either a declaration or a definition, check the |
| 11172 | // nested-name-specifier against the current context. We don't do this |
| 11173 | // for explicit specializations, because they have similar checking |
| 11174 | // (with more specific diagnostics) in the call to |
| 11175 | // CheckMemberSpecialization, below. |
| 11176 | if (!isExplicitSpecialization && |
| 11177 | (TUK == TUK_Definition || TUK == TUK_Declaration) && |
| 11178 | diagnoseQualifiedDeclaration(SS, DC, OrigName, NameLoc)) |
| 11179 | Invalid = true; |
| 11180 | |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 11181 | New->setQualifierInfo(SS.getWithLocInContext(Context)); |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 11182 | if (TemplateParameterLists.size() > 0) { |
Douglas Gregor | c722ea4 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 11183 | New->setTemplateParameterListsInfo(Context, |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 11184 | TemplateParameterLists.size(), |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 11185 | TemplateParameterLists.data()); |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 11186 | } |
Fariborz Jahanian | 4fb2053 | 2010-05-14 21:35:02 +0000 | [diff] [blame] | 11187 | } |
| 11188 | else |
| 11189 | Invalid = true; |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 11190 | } |
| 11191 | |
Daniel Dunbar | 9f21f89 | 2010-05-27 01:53:40 +0000 | [diff] [blame] | 11192 | if (RecordDecl *RD = dyn_cast<RecordDecl>(New)) { |
| 11193 | // Add alignment attributes if necessary; these attributes are checked when |
| 11194 | // the ASTContext lays out the structure. |
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 11195 | // |
| 11196 | // It is important for implementing the correct semantics that this |
| 11197 | // happen here (in act on tag decl). The #pragma pack stack is |
| 11198 | // maintained as a result of parser callbacks which can occur at |
| 11199 | // many points during the parsing of a struct declaration (because |
| 11200 | // the #pragma tokens are effectively skipped over during the |
| 11201 | // parsing of the struct). |
Eli Friedman | 2016c8c | 2012-08-08 21:08:34 +0000 | [diff] [blame] | 11202 | if (TUK == TUK_Definition) { |
| 11203 | AddAlignmentAttributesForRecord(RD); |
| 11204 | AddMsStructLayoutForRecord(RD); |
| 11205 | } |
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 11206 | } |
| 11207 | |
Douglas Gregor | 2ccd89c | 2011-12-20 18:11:52 +0000 | [diff] [blame] | 11208 | if (ModulePrivateLoc.isValid()) { |
Douglas Gregor | d023aec | 2011-09-09 20:53:38 +0000 | [diff] [blame] | 11209 | if (isExplicitSpecialization) |
| 11210 | Diag(New->getLocation(), diag::err_module_private_specialization) |
| 11211 | << 2 |
| 11212 | << FixItHint::CreateRemoval(ModulePrivateLoc); |
Douglas Gregor | e389585 | 2011-09-12 18:37:38 +0000 | [diff] [blame] | 11213 | // __module_private__ does not apply to local classes. However, we only |
| 11214 | // diagnose this as an error when the declaration specifiers are |
| 11215 | // freestanding. Here, we just ignore the __module_private__. |
Douglas Gregor | e389585 | 2011-09-12 18:37:38 +0000 | [diff] [blame] | 11216 | else if (!SearchDC->isFunctionOrMethod()) |
Douglas Gregor | e761230 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 11217 | New->setModulePrivate(); |
| 11218 | } |
| 11219 | |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 11220 | // If this is a specialization of a member class (of a class template), |
| 11221 | // check the specialization. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 11222 | if (isExplicitSpecialization && CheckMemberSpecialization(New, Previous)) |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 11223 | Invalid = true; |
Douglas Gregor | 6960587 | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 11224 | |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 11225 | if (Invalid) |
| 11226 | New->setInvalidDecl(); |
| 11227 | |
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 11228 | if (Attr) |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 11229 | ProcessDeclAttributeList(S, New, Attr); |
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 11230 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 11231 | // If we're declaring or defining a tag in function prototype scope in C, |
| 11232 | // note that this type can only be used within the function and add it to |
| 11233 | // the list of decls to inject into the function definition scope. |
| 11234 | if (!getLangOpts().CPlusPlus && (Name || Kind == TTK_Enum) && |
| 11235 | getNonFieldDeclScope(S)->isFunctionPrototypeScope()) { |
Douglas Gregor | 3218c4b | 2009-01-09 22:42:13 +0000 | [diff] [blame] | 11236 | Diag(Loc, diag::warn_decl_in_param_list) << Context.getTagDeclType(New); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 11237 | DeclsInPrototypeScope.push_back(New); |
| 11238 | } |
Douglas Gregor | 3218c4b | 2009-01-09 22:42:13 +0000 | [diff] [blame] | 11239 | |
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 11240 | // Set the lexical context. If the tag has a C++ scope specifier, the |
| 11241 | // lexical context will be different from the semantic context. |
Douglas Gregor | 1931b44 | 2009-02-03 00:34:39 +0000 | [diff] [blame] | 11242 | New->setLexicalDeclContext(CurContext); |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 11243 | |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 11244 | // Mark this as a friend decl if applicable. |
Francois Pichet | b474603 | 2011-06-01 04:14:20 +0000 | [diff] [blame] | 11245 | // In Microsoft mode, a friend declaration also acts as a forward |
| 11246 | // declaration so we always pass true to setObjectOfFriendDecl to make |
| 11247 | // the tag name visible. |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 11248 | if (TUK == TUK_Friend) |
Richard Smith | 22050f2 | 2013-07-17 23:53:16 +0000 | [diff] [blame] | 11249 | New->setObjectOfFriendDecl(!FriendSawTagOutsideEnclosingNamespace && |
| 11250 | getLangOpts().MicrosoftExt); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 11251 | |
Anders Carlsson | 0cf8830 | 2009-03-26 01:19:02 +0000 | [diff] [blame] | 11252 | // Set the access specifier. |
John McCall | 9c86b51 | 2010-03-25 21:28:06 +0000 | [diff] [blame] | 11253 | if (!Invalid && SearchDC->isRecord()) |
Douglas Gregor | d0c8737 | 2009-05-27 17:30:49 +0000 | [diff] [blame] | 11254 | SetMemberAccessSpecifier(New, PrevDecl, AS); |
Douglas Gregor | 06c0fec | 2009-03-25 22:00:53 +0000 | [diff] [blame] | 11255 | |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 11256 | if (TUK == TUK_Definition) |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 11257 | New->startDefinition(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11258 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 11259 | // If this has an identifier, add it to the scope stack. |
John McCall | d7eff68 | 2009-09-02 00:55:30 +0000 | [diff] [blame] | 11260 | if (TUK == TUK_Friend) { |
John McCall | 82b9fb8 | 2009-09-02 19:32:14 +0000 | [diff] [blame] | 11261 | // We might be replacing an existing declaration in the lookup tables; |
| 11262 | // if so, borrow its access specifier. |
| 11263 | if (PrevDecl) |
| 11264 | New->setAccess(PrevDecl->getAccess()); |
| 11265 | |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 11266 | DeclContext *DC = New->getDeclContext()->getRedeclContext(); |
Richard Smith | 1b7f9cb | 2012-03-13 03:12:56 +0000 | [diff] [blame] | 11267 | DC->makeDeclVisibleInContext(New); |
John McCall | 9c86b51 | 2010-03-25 21:28:06 +0000 | [diff] [blame] | 11268 | if (Name) // can be null along some error paths |
John McCall | d7eff68 | 2009-09-02 00:55:30 +0000 | [diff] [blame] | 11269 | if (Scope *EnclosingScope = getScopeForDeclContext(S, DC)) |
| 11270 | PushOnScopeChains(New, EnclosingScope, /* AddToContext = */ false); |
John McCall | d7eff68 | 2009-09-02 00:55:30 +0000 | [diff] [blame] | 11271 | } else if (Name) { |
Douglas Gregor | 1a0d31a | 2009-01-12 18:45:55 +0000 | [diff] [blame] | 11272 | S = getNonFieldDeclScope(S); |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 11273 | PushOnScopeChains(New, S, !IsForwardReference); |
| 11274 | if (IsForwardReference) |
Richard Smith | 1b7f9cb | 2012-03-13 03:12:56 +0000 | [diff] [blame] | 11275 | SearchDC->makeDeclVisibleInContext(New); |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 11276 | |
Douglas Gregor | 4920f1f | 2009-01-12 22:49:06 +0000 | [diff] [blame] | 11277 | } else { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 11278 | CurContext->addDecl(New); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 11279 | } |
Argyrios Kyrtzidis | 5239304 | 2008-11-09 23:41:00 +0000 | [diff] [blame] | 11280 | |
Douglas Gregor | c29f77b | 2009-07-07 16:35:42 +0000 | [diff] [blame] | 11281 | // If this is the C FILE type, notify the AST context. |
| 11282 | if (IdentifierInfo *II = New->getIdentifier()) |
| 11283 | if (!New->isInvalidDecl() && |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 11284 | New->getDeclContext()->getRedeclContext()->isTranslationUnit() && |
Douglas Gregor | c29f77b | 2009-07-07 16:35:42 +0000 | [diff] [blame] | 11285 | II->isStr("FILE")) |
| 11286 | Context.setFILEDecl(New); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11287 | |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 11288 | if (PrevDecl) |
| 11289 | mergeDeclAttributes(New, PrevDecl); |
| 11290 | |
Rafael Espindola | 71adc5b | 2012-07-17 15:14:47 +0000 | [diff] [blame] | 11291 | // If there's a #pragma GCC visibility in scope, set the visibility of this |
| 11292 | // record. |
| 11293 | AddPushedVisibilityAttribute(New); |
| 11294 | |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 11295 | OwnedDecl = true; |
Richard Smith | 37ec8d5 | 2012-12-05 11:34:06 +0000 | [diff] [blame] | 11296 | // In C++, don't return an invalid declaration. We can't recover well from |
| 11297 | // the cases where we make the type anonymous. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 11298 | return (Invalid && getLangOpts().CPlusPlus) ? nullptr : New; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 11299 | } |
| 11300 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11301 | void Sema::ActOnTagStartDefinition(Scope *S, Decl *TagD) { |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 11302 | AdjustDeclIfTemplate(TagD); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11303 | TagDecl *Tag = cast<TagDecl>(TagD); |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 11304 | |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 11305 | // Enter the tag context. |
| 11306 | PushDeclContext(S, Tag); |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 11307 | |
| 11308 | ActOnDocumentableDecl(TagD); |
Rafael Espindola | 5e06529 | 2012-07-12 04:47:34 +0000 | [diff] [blame] | 11309 | |
| 11310 | // If there's a #pragma GCC visibility in scope, set the visibility of this |
| 11311 | // record. |
| 11312 | AddPushedVisibilityAttribute(Tag); |
John McCall | f936815 | 2009-12-20 07:58:13 +0000 | [diff] [blame] | 11313 | } |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 11314 | |
Argyrios Kyrtzidis | 3a38744 | 2011-10-06 23:23:20 +0000 | [diff] [blame] | 11315 | Decl *Sema::ActOnObjCContainerStartDefinition(Decl *IDecl) { |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 11316 | assert(isa<ObjCContainerDecl>(IDecl) && |
| 11317 | "ActOnObjCContainerStartDefinition - Not ObjCContainerDecl"); |
| 11318 | DeclContext *OCD = cast<DeclContext>(IDecl); |
| 11319 | assert(getContainingDC(OCD) == CurContext && |
| 11320 | "The next DeclContext should be lexically contained in the current one."); |
| 11321 | CurContext = OCD; |
Argyrios Kyrtzidis | 3a38744 | 2011-10-06 23:23:20 +0000 | [diff] [blame] | 11322 | return IDecl; |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 11323 | } |
| 11324 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11325 | void Sema::ActOnStartCXXMemberDeclarations(Scope *S, Decl *TagD, |
Anders Carlsson | 2c3ee54 | 2011-03-25 14:31:08 +0000 | [diff] [blame] | 11326 | SourceLocation FinalLoc, |
David Majnemer | 7121bdb | 2013-10-18 00:33:31 +0000 | [diff] [blame] | 11327 | bool IsFinalSpelledSealed, |
John McCall | f936815 | 2009-12-20 07:58:13 +0000 | [diff] [blame] | 11328 | SourceLocation LBraceLoc) { |
| 11329 | AdjustDeclIfTemplate(TagD); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11330 | CXXRecordDecl *Record = cast<CXXRecordDecl>(TagD); |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 11331 | |
John McCall | f936815 | 2009-12-20 07:58:13 +0000 | [diff] [blame] | 11332 | FieldCollector->StartClass(); |
| 11333 | |
| 11334 | if (!Record->getIdentifier()) |
| 11335 | return; |
| 11336 | |
Anders Carlsson | 2c3ee54 | 2011-03-25 14:31:08 +0000 | [diff] [blame] | 11337 | if (FinalLoc.isValid()) |
David Majnemer | 7121bdb | 2013-10-18 00:33:31 +0000 | [diff] [blame] | 11338 | Record->addAttr(new (Context) |
| 11339 | FinalAttr(FinalLoc, Context, IsFinalSpelledSealed)); |
| 11340 | |
John McCall | f936815 | 2009-12-20 07:58:13 +0000 | [diff] [blame] | 11341 | // C++ [class]p2: |
| 11342 | // [...] The class-name is also inserted into the scope of the |
| 11343 | // class itself; this is known as the injected-class-name. For |
| 11344 | // purposes of access checking, the injected-class-name is treated |
| 11345 | // as if it were a public member name. |
| 11346 | CXXRecordDecl *InjectedClassName |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 11347 | = CXXRecordDecl::Create(Context, Record->getTagKind(), CurContext, |
| 11348 | Record->getLocStart(), Record->getLocation(), |
John McCall | f936815 | 2009-12-20 07:58:13 +0000 | [diff] [blame] | 11349 | Record->getIdentifier(), |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 11350 | /*PrevDecl=*/nullptr, |
Argyrios Kyrtzidis | 3b8f610 | 2010-10-14 20:14:21 +0000 | [diff] [blame] | 11351 | /*DelayTypeCreation=*/true); |
| 11352 | Context.getTypeDeclType(InjectedClassName, Record); |
John McCall | f936815 | 2009-12-20 07:58:13 +0000 | [diff] [blame] | 11353 | InjectedClassName->setImplicit(); |
| 11354 | InjectedClassName->setAccess(AS_public); |
| 11355 | if (ClassTemplateDecl *Template = Record->getDescribedClassTemplate()) |
| 11356 | InjectedClassName->setDescribedClassTemplate(Template); |
| 11357 | PushOnScopeChains(InjectedClassName, S); |
| 11358 | assert(InjectedClassName->isInjectedClassName() && |
| 11359 | "Broken injected-class-name"); |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 11360 | } |
| 11361 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11362 | void Sema::ActOnTagFinishDefinition(Scope *S, Decl *TagD, |
Argyrios Kyrtzidis | 07a5b28 | 2009-07-14 03:17:52 +0000 | [diff] [blame] | 11363 | SourceLocation RBraceLoc) { |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 11364 | AdjustDeclIfTemplate(TagD); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11365 | TagDecl *Tag = cast<TagDecl>(TagD); |
Argyrios Kyrtzidis | 07a5b28 | 2009-07-14 03:17:52 +0000 | [diff] [blame] | 11366 | Tag->setRBraceLoc(RBraceLoc); |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 11367 | |
Argyrios Kyrtzidis | 216f78b | 2012-03-09 20:10:30 +0000 | [diff] [blame] | 11368 | // Make sure we "complete" the definition even it is invalid. |
| 11369 | if (Tag->isBeingDefined()) { |
| 11370 | assert(Tag->isInvalidDecl() && "We should already have completed it"); |
| 11371 | if (RecordDecl *RD = dyn_cast<RecordDecl>(Tag)) |
| 11372 | RD->completeDefinition(); |
| 11373 | } |
| 11374 | |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 11375 | if (isa<CXXRecordDecl>(Tag)) |
| 11376 | FieldCollector->FinishClass(); |
| 11377 | |
| 11378 | // Exit this scope of this tag's definition. |
| 11379 | PopDeclContext(); |
Argyrios Kyrtzidis | 3d207e7 | 2013-01-29 18:00:54 +0000 | [diff] [blame] | 11380 | |
| 11381 | if (getCurLexicalContext()->isObjCContainer() && |
| 11382 | Tag->getDeclContext()->isFileContext()) |
| 11383 | Tag->setTopLevelDeclInObjCContainer(); |
| 11384 | |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 11385 | // Notify the consumer that we've defined a tag. |
Serge Pavlov | 439b701 | 2013-07-02 17:31:56 +0000 | [diff] [blame] | 11386 | if (!Tag->isInvalidDecl()) |
| 11387 | Consumer.HandleTagDeclDefinition(Tag); |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 11388 | } |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 11389 | |
Fariborz Jahanian | 10af879 | 2011-08-29 17:33:12 +0000 | [diff] [blame] | 11390 | void Sema::ActOnObjCContainerFinishDefinition() { |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 11391 | // Exit this scope of this interface definition. |
| 11392 | PopDeclContext(); |
| 11393 | } |
Argyrios Kyrtzidis | 3a38744 | 2011-10-06 23:23:20 +0000 | [diff] [blame] | 11394 | |
Argyrios Kyrtzidis | 458bacf | 2011-10-27 00:09:34 +0000 | [diff] [blame] | 11395 | void Sema::ActOnObjCTemporaryExitContainerContext(DeclContext *DC) { |
Argyrios Kyrtzidis | 4a7dc8a | 2011-10-27 00:53:06 +0000 | [diff] [blame] | 11396 | assert(DC == CurContext && "Mismatch of container contexts"); |
| 11397 | OriginalLexicalContext = DC; |
Argyrios Kyrtzidis | 3a38744 | 2011-10-06 23:23:20 +0000 | [diff] [blame] | 11398 | ActOnObjCContainerFinishDefinition(); |
| 11399 | } |
| 11400 | |
Argyrios Kyrtzidis | 458bacf | 2011-10-27 00:09:34 +0000 | [diff] [blame] | 11401 | void Sema::ActOnObjCReenterContainerContext(DeclContext *DC) { |
| 11402 | ActOnObjCContainerStartDefinition(cast<Decl>(DC)); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 11403 | OriginalLexicalContext = nullptr; |
Argyrios Kyrtzidis | 3a38744 | 2011-10-06 23:23:20 +0000 | [diff] [blame] | 11404 | } |
| 11405 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11406 | void Sema::ActOnTagDefinitionError(Scope *S, Decl *TagD) { |
John McCall | db7bb4a | 2010-03-17 00:38:33 +0000 | [diff] [blame] | 11407 | AdjustDeclIfTemplate(TagD); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11408 | TagDecl *Tag = cast<TagDecl>(TagD); |
John McCall | db7bb4a | 2010-03-17 00:38:33 +0000 | [diff] [blame] | 11409 | Tag->setInvalidDecl(); |
| 11410 | |
Argyrios Kyrtzidis | 216f78b | 2012-03-09 20:10:30 +0000 | [diff] [blame] | 11411 | // Make sure we "complete" the definition even it is invalid. |
| 11412 | if (Tag->isBeingDefined()) { |
| 11413 | if (RecordDecl *RD = dyn_cast<RecordDecl>(Tag)) |
| 11414 | RD->completeDefinition(); |
| 11415 | } |
| 11416 | |
John McCall | a8cab01 | 2010-03-17 19:25:57 +0000 | [diff] [blame] | 11417 | // We're undoing ActOnTagStartDefinition here, not |
| 11418 | // ActOnStartCXXMemberDeclarations, so we don't have to mess with |
| 11419 | // the FieldCollector. |
John McCall | db7bb4a | 2010-03-17 00:38:33 +0000 | [diff] [blame] | 11420 | |
| 11421 | PopDeclContext(); |
| 11422 | } |
| 11423 | |
Chris Lattner | df9bcd5 | 2009-04-20 17:29:38 +0000 | [diff] [blame] | 11424 | // Note that FieldName may be null for anonymous bitfields. |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 11425 | ExprResult Sema::VerifyBitField(SourceLocation FieldLoc, |
| 11426 | IdentifierInfo *FieldName, |
Reid Kleckner | 9a3ecb0 | 2013-07-17 20:46:03 +0000 | [diff] [blame] | 11427 | QualType FieldTy, bool IsMsStruct, |
| 11428 | Expr *BitWidth, bool *ZeroWidth) { |
Eli Friedman | 1d954f6 | 2009-08-15 21:55:26 +0000 | [diff] [blame] | 11429 | // Default to true; that shouldn't confuse checks for emptiness |
| 11430 | if (ZeroWidth) |
| 11431 | *ZeroWidth = true; |
| 11432 | |
Chris Lattner | 2479366 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 11433 | // C99 6.7.2.1p4 - verify the field type. |
Chris Lattner | 8b963ef | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 11434 | // C++ 9.6p3: A bit-field shall have integral or enumeration type. |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 11435 | if (!FieldTy->isDependentType() && !FieldTy->isIntegralOrEnumerationType()) { |
Chris Lattner | 2479366 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 11436 | // Handle incomplete types with specific error. |
Douglas Gregor | a03aca8 | 2009-03-10 21:58:27 +0000 | [diff] [blame] | 11437 | if (RequireCompleteType(FieldLoc, FieldTy, diag::err_field_incomplete)) |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 11438 | return ExprError(); |
Chris Lattner | df9bcd5 | 2009-04-20 17:29:38 +0000 | [diff] [blame] | 11439 | if (FieldName) |
| 11440 | return Diag(FieldLoc, diag::err_not_integral_type_bitfield) |
| 11441 | << FieldName << FieldTy << BitWidth->getSourceRange(); |
| 11442 | return Diag(FieldLoc, diag::err_not_integral_type_anon_bitfield) |
| 11443 | << FieldTy << BitWidth->getSourceRange(); |
Douglas Gregor | e186269 | 2010-12-15 23:18:36 +0000 | [diff] [blame] | 11444 | } else if (DiagnoseUnexpandedParameterPack(const_cast<Expr *>(BitWidth), |
| 11445 | UPPC_BitFieldWidth)) |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 11446 | return ExprError(); |
Douglas Gregor | 3cf538d | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 11447 | |
| 11448 | // If the bit-width is type- or value-dependent, don't try to check |
| 11449 | // it now. |
| 11450 | if (BitWidth->isValueDependent() || BitWidth->isTypeDependent()) |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 11451 | return Owned(BitWidth); |
Douglas Gregor | 3cf538d | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 11452 | |
Anders Carlsson | 9f1e572 | 2008-12-06 20:33:04 +0000 | [diff] [blame] | 11453 | llvm::APSInt Value; |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 11454 | ExprResult ICE = VerifyIntegerConstantExpression(BitWidth, &Value); |
| 11455 | if (ICE.isInvalid()) |
| 11456 | return ICE; |
| 11457 | BitWidth = ICE.take(); |
Anders Carlsson | 9f1e572 | 2008-12-06 20:33:04 +0000 | [diff] [blame] | 11458 | |
Eli Friedman | 1d954f6 | 2009-08-15 21:55:26 +0000 | [diff] [blame] | 11459 | if (Value != 0 && ZeroWidth) |
| 11460 | *ZeroWidth = false; |
| 11461 | |
Chris Lattner | cd08707 | 2008-12-12 04:56:04 +0000 | [diff] [blame] | 11462 | // Zero-width bitfield is ok for anonymous field. |
| 11463 | if (Value == 0 && FieldName) |
| 11464 | return Diag(FieldLoc, diag::err_bitfield_has_zero_width) << FieldName; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11465 | |
Chris Lattner | df9bcd5 | 2009-04-20 17:29:38 +0000 | [diff] [blame] | 11466 | if (Value.isSigned() && Value.isNegative()) { |
| 11467 | if (FieldName) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11468 | return Diag(FieldLoc, diag::err_bitfield_has_negative_width) |
Chris Lattner | df9bcd5 | 2009-04-20 17:29:38 +0000 | [diff] [blame] | 11469 | << FieldName << Value.toString(10); |
| 11470 | return Diag(FieldLoc, diag::err_anon_bitfield_has_negative_width) |
| 11471 | << Value.toString(10); |
| 11472 | } |
Anders Carlsson | 9f1e572 | 2008-12-06 20:33:04 +0000 | [diff] [blame] | 11473 | |
Douglas Gregor | 3cf538d | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 11474 | if (!FieldTy->isDependentType()) { |
| 11475 | uint64_t TypeSize = Context.getTypeSize(FieldTy); |
Chris Lattner | df9bcd5 | 2009-04-20 17:29:38 +0000 | [diff] [blame] | 11476 | if (Value.getZExtValue() > TypeSize) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 11477 | if (!getLangOpts().CPlusPlus || IsMsStruct || |
| 11478 | Context.getTargetInfo().getCXXABI().isMicrosoft()) { |
Anders Carlsson | 72468ec | 2010-04-16 15:16:32 +0000 | [diff] [blame] | 11479 | if (FieldName) |
| 11480 | return Diag(FieldLoc, diag::err_bitfield_width_exceeds_type_size) |
| 11481 | << FieldName << (unsigned)Value.getZExtValue() |
| 11482 | << (unsigned)TypeSize; |
| 11483 | |
| 11484 | return Diag(FieldLoc, diag::err_anon_bitfield_width_exceeds_type_size) |
| 11485 | << (unsigned)Value.getZExtValue() << (unsigned)TypeSize; |
| 11486 | } |
| 11487 | |
Chris Lattner | df9bcd5 | 2009-04-20 17:29:38 +0000 | [diff] [blame] | 11488 | if (FieldName) |
Anders Carlsson | 72468ec | 2010-04-16 15:16:32 +0000 | [diff] [blame] | 11489 | Diag(FieldLoc, diag::warn_bitfield_width_exceeds_type_size) |
| 11490 | << FieldName << (unsigned)Value.getZExtValue() |
| 11491 | << (unsigned)TypeSize; |
| 11492 | else |
| 11493 | Diag(FieldLoc, diag::warn_anon_bitfield_width_exceeds_type_size) |
| 11494 | << (unsigned)Value.getZExtValue() << (unsigned)TypeSize; |
Chris Lattner | df9bcd5 | 2009-04-20 17:29:38 +0000 | [diff] [blame] | 11495 | } |
Douglas Gregor | 3cf538d | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 11496 | } |
Anders Carlsson | 9f1e572 | 2008-12-06 20:33:04 +0000 | [diff] [blame] | 11497 | |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 11498 | return Owned(BitWidth); |
Anders Carlsson | 9f1e572 | 2008-12-06 20:33:04 +0000 | [diff] [blame] | 11499 | } |
| 11500 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 11501 | /// ActOnField - Each field of a C struct/union is passed into this in order |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 11502 | /// to create a FieldDecl object for it. |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 11503 | Decl *Sema::ActOnField(Scope *S, Decl *TagD, SourceLocation DeclStart, |
Richard Trieu | f81e5a9 | 2011-09-09 02:00:50 +0000 | [diff] [blame] | 11504 | Declarator &D, Expr *BitfieldWidth) { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11505 | FieldDecl *Res = HandleField(S, cast_or_null<RecordDecl>(TagD), |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 11506 | DeclStart, D, static_cast<Expr*>(BitfieldWidth), |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 11507 | /*InitStyle=*/ICIS_NoInit, AS_public); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11508 | return Res; |
Chris Lattner | 2479366 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 11509 | } |
| 11510 | |
| 11511 | /// HandleField - Analyze a field of a C struct or a C++ data member. |
| 11512 | /// |
| 11513 | FieldDecl *Sema::HandleField(Scope *S, RecordDecl *Record, |
| 11514 | SourceLocation DeclStart, |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 11515 | Declarator &D, Expr *BitWidth, |
| 11516 | InClassInitStyle InitStyle, |
Douglas Gregor | 4dd55f5 | 2009-03-11 20:50:30 +0000 | [diff] [blame] | 11517 | AccessSpecifier AS) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 11518 | IdentifierInfo *II = D.getIdentifier(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 11519 | SourceLocation Loc = DeclStart; |
| 11520 | if (II) Loc = D.getIdentifierLoc(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11521 | |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 11522 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
| 11523 | QualType T = TInfo->getType(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 11524 | if (getLangOpts().CPlusPlus) { |
Douglas Gregor | 3cf538d | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 11525 | CheckExtraCXXDefaultArguments(D); |
Douglas Gregor | 021c3b3 | 2009-03-11 23:00:04 +0000 | [diff] [blame] | 11526 | |
Douglas Gregor | e186269 | 2010-12-15 23:18:36 +0000 | [diff] [blame] | 11527 | if (DiagnoseUnexpandedParameterPack(D.getIdentifierLoc(), TInfo, |
| 11528 | UPPC_DataMemberType)) { |
| 11529 | D.setInvalidType(); |
| 11530 | T = Context.IntTy; |
| 11531 | TInfo = Context.getTrivialTypeSourceInfo(T, Loc); |
| 11532 | } |
| 11533 | } |
| 11534 | |
Matt Arsenault | 34b0adb | 2013-02-26 21:16:00 +0000 | [diff] [blame] | 11535 | // TR 18037 does not allow fields to be declared with address spaces. |
| 11536 | if (T.getQualifiers().hasAddressSpace()) { |
| 11537 | Diag(Loc, diag::err_field_with_address_space); |
| 11538 | D.setInvalidType(); |
| 11539 | } |
| 11540 | |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 11541 | // OpenCL 1.2 spec, s6.9 r: |
| 11542 | // The event type cannot be used to declare a structure or union field. |
| 11543 | if (LangOpts.OpenCL && T->isEventT()) { |
| 11544 | Diag(Loc, diag::err_event_t_struct_field); |
| 11545 | D.setInvalidType(); |
| 11546 | } |
| 11547 | |
Richard Smith | c7f8116 | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 11548 | DiagnoseFunctionSpecifiers(D.getDeclSpec()); |
Eli Friedman | 85a5319 | 2009-04-07 19:37:57 +0000 | [diff] [blame] | 11549 | |
Richard Smith | ec64244 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 11550 | if (DeclSpec::TSCS TSCS = D.getDeclSpec().getThreadStorageClassSpec()) |
| 11551 | Diag(D.getDeclSpec().getThreadStorageClassSpecLoc(), |
| 11552 | diag::err_invalid_thread) |
| 11553 | << DeclSpec::getSpecifierName(TSCS); |
Matt Arsenault | 34b0adb | 2013-02-26 21:16:00 +0000 | [diff] [blame] | 11554 | |
Douglas Gregor | 7f6ff02 | 2010-08-30 14:32:14 +0000 | [diff] [blame] | 11555 | // Check to see if this name was declared as a member previously |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 11556 | NamedDecl *PrevDecl = nullptr; |
Douglas Gregor | 7f6ff02 | 2010-08-30 14:32:14 +0000 | [diff] [blame] | 11557 | LookupResult Previous(*this, II, Loc, LookupMemberName, ForRedeclaration); |
| 11558 | LookupName(Previous, S); |
Douglas Gregor | 95e5510 | 2011-10-21 15:47:52 +0000 | [diff] [blame] | 11559 | switch (Previous.getResultKind()) { |
| 11560 | case LookupResult::Found: |
| 11561 | case LookupResult::FoundUnresolvedValue: |
| 11562 | PrevDecl = Previous.getAsSingle<NamedDecl>(); |
| 11563 | break; |
| 11564 | |
| 11565 | case LookupResult::FoundOverloaded: |
| 11566 | PrevDecl = Previous.getRepresentativeDecl(); |
| 11567 | break; |
| 11568 | |
| 11569 | case LookupResult::NotFound: |
| 11570 | case LookupResult::NotFoundInCurrentInstantiation: |
| 11571 | case LookupResult::Ambiguous: |
| 11572 | break; |
| 11573 | } |
| 11574 | Previous.suppressDiagnostics(); |
Douglas Gregor | c19ee3e | 2009-06-17 23:37:01 +0000 | [diff] [blame] | 11575 | |
| 11576 | if (PrevDecl && PrevDecl->isTemplateParameter()) { |
| 11577 | // Maybe we will complain about the shadowed template parameter. |
| 11578 | DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), PrevDecl); |
| 11579 | // Just pretend that we didn't see the previous declaration. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 11580 | PrevDecl = nullptr; |
Douglas Gregor | c19ee3e | 2009-06-17 23:37:01 +0000 | [diff] [blame] | 11581 | } |
| 11582 | |
Douglas Gregor | 3cf538d | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 11583 | if (PrevDecl && !isDeclInScope(PrevDecl, Record, S)) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 11584 | PrevDecl = nullptr; |
Douglas Gregor | 3cf538d | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 11585 | |
Steve Naroff | ea218b8 | 2009-07-14 14:58:18 +0000 | [diff] [blame] | 11586 | bool Mutable |
| 11587 | = (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_mutable); |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 11588 | SourceLocation TSSL = D.getLocStart(); |
Steve Naroff | ea218b8 | 2009-07-14 14:58:18 +0000 | [diff] [blame] | 11589 | FieldDecl *NewFD |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 11590 | = CheckFieldDecl(II, T, TInfo, Record, Loc, Mutable, BitWidth, InitStyle, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 11591 | TSSL, AS, PrevDecl, &D); |
Rafael Espindola | 0162070 | 2010-03-21 22:56:43 +0000 | [diff] [blame] | 11592 | |
| 11593 | if (NewFD->isInvalidDecl()) |
| 11594 | Record->setInvalidDecl(); |
| 11595 | |
Douglas Gregor | 591dc84 | 2011-09-12 16:11:24 +0000 | [diff] [blame] | 11596 | if (D.getDeclSpec().isModulePrivateSpecified()) |
| 11597 | NewFD->setModulePrivate(); |
| 11598 | |
Douglas Gregor | 3cf538d | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 11599 | if (NewFD->isInvalidDecl() && PrevDecl) { |
| 11600 | // Don't introduce NewFD into scope; there's already something |
| 11601 | // with the same name in the same scope. |
| 11602 | } else if (II) { |
| 11603 | PushOnScopeChains(NewFD, S); |
| 11604 | } else |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 11605 | Record->addDecl(NewFD); |
Douglas Gregor | 3cf538d | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 11606 | |
| 11607 | return NewFD; |
| 11608 | } |
| 11609 | |
| 11610 | /// \brief Build a new FieldDecl and check its well-formedness. |
| 11611 | /// |
| 11612 | /// This routine builds a new FieldDecl given the fields name, type, |
| 11613 | /// record, etc. \p PrevDecl should refer to any previous declaration |
| 11614 | /// with the same name and in the same scope as the field to be |
| 11615 | /// created. |
| 11616 | /// |
| 11617 | /// \returns a new FieldDecl. |
| 11618 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11619 | /// \todo The Declarator argument is a hack. It will be removed once |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 11620 | FieldDecl *Sema::CheckFieldDecl(DeclarationName Name, QualType T, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 11621 | TypeSourceInfo *TInfo, |
Douglas Gregor | 3cf538d | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 11622 | RecordDecl *Record, SourceLocation Loc, |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 11623 | bool Mutable, Expr *BitWidth, |
| 11624 | InClassInitStyle InitStyle, |
Steve Naroff | ea218b8 | 2009-07-14 14:58:18 +0000 | [diff] [blame] | 11625 | SourceLocation TSSL, |
Douglas Gregor | 4dd55f5 | 2009-03-11 20:50:30 +0000 | [diff] [blame] | 11626 | AccessSpecifier AS, NamedDecl *PrevDecl, |
Douglas Gregor | 3cf538d | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 11627 | Declarator *D) { |
| 11628 | IdentifierInfo *II = Name.getAsIdentifierInfo(); |
Steve Naroff | 5912a35 | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 11629 | bool InvalidDecl = false; |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 11630 | if (D) InvalidDecl = D->isInvalidType(); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 11631 | |
Douglas Gregor | 3cf538d | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 11632 | // If we receive a broken type, recover by assuming 'int' and |
| 11633 | // marking this declaration as invalid. |
| 11634 | if (T.isNull()) { |
| 11635 | InvalidDecl = true; |
| 11636 | T = Context.IntTy; |
| 11637 | } |
| 11638 | |
Eli Friedman | 721e77d | 2009-12-07 00:22:08 +0000 | [diff] [blame] | 11639 | QualType EltTy = Context.getBaseElementType(T); |
Argyrios Kyrtzidis | 216f78b | 2012-03-09 20:10:30 +0000 | [diff] [blame] | 11640 | if (!EltTy->isDependentType()) { |
| 11641 | if (RequireCompleteType(Loc, EltTy, diag::err_field_incomplete)) { |
| 11642 | // Fields of incomplete type force their record to be invalid. |
| 11643 | Record->setInvalidDecl(); |
| 11644 | InvalidDecl = true; |
| 11645 | } else { |
| 11646 | NamedDecl *Def; |
| 11647 | EltTy->isIncompleteType(&Def); |
| 11648 | if (Def && Def->isInvalidDecl()) { |
| 11649 | Record->setInvalidDecl(); |
| 11650 | InvalidDecl = true; |
| 11651 | } |
| 11652 | } |
John McCall | 2d7d2d9 | 2010-08-16 23:42:35 +0000 | [diff] [blame] | 11653 | } |
Eli Friedman | 721e77d | 2009-12-07 00:22:08 +0000 | [diff] [blame] | 11654 | |
Joey Gouly | 617bb31 | 2013-01-17 17:35:00 +0000 | [diff] [blame] | 11655 | // OpenCL v1.2 s6.9.c: bitfields are not supported. |
| 11656 | if (BitWidth && getLangOpts().OpenCL) { |
| 11657 | Diag(Loc, diag::err_opencl_bitfields); |
| 11658 | InvalidDecl = true; |
| 11659 | } |
| 11660 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 11661 | // C99 6.7.2.1p8: A member of a structure or union may have any type other |
| 11662 | // than a variably modified type. |
Eli Friedman | 721e77d | 2009-12-07 00:22:08 +0000 | [diff] [blame] | 11663 | if (!InvalidDecl && T->isVariablyModifiedType()) { |
Eli Friedman | 1ca4813 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 11664 | bool SizeIsNegative; |
Douglas Gregor | 2767ce2 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 11665 | llvm::APSInt Oversized; |
Abramo Bagnara | 4c5750e | 2012-11-08 14:44:42 +0000 | [diff] [blame] | 11666 | |
| 11667 | TypeSourceInfo *FixedTInfo = |
| 11668 | TryToFixInvalidVariablyModifiedTypeSourceInfo(TInfo, Context, |
| 11669 | SizeIsNegative, |
| 11670 | Oversized); |
| 11671 | if (FixedTInfo) { |
Eli Friedman | 1ca4813 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 11672 | Diag(Loc, diag::warn_illegal_constant_array_size); |
Abramo Bagnara | 4c5750e | 2012-11-08 14:44:42 +0000 | [diff] [blame] | 11673 | TInfo = FixedTInfo; |
| 11674 | T = FixedTInfo->getType(); |
Eli Friedman | 1ca4813 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 11675 | } else { |
| 11676 | if (SizeIsNegative) |
| 11677 | Diag(Loc, diag::err_typecheck_negative_array_size); |
Douglas Gregor | 2767ce2 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 11678 | else if (Oversized.getBoolValue()) |
| 11679 | Diag(Loc, diag::err_array_too_large) |
| 11680 | << Oversized.toString(10); |
Eli Friedman | 1ca4813 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 11681 | else |
| 11682 | Diag(Loc, diag::err_typecheck_field_variable_size); |
Eli Friedman | 1ca4813 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 11683 | InvalidDecl = true; |
| 11684 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 11685 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11686 | |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 11687 | // Fields can not have abstract class types |
Eli Friedman | 721e77d | 2009-12-07 00:22:08 +0000 | [diff] [blame] | 11688 | if (!InvalidDecl && RequireNonAbstractType(Loc, T, |
| 11689 | diag::err_abstract_type_in_decl, |
| 11690 | AbstractFieldType)) |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 11691 | InvalidDecl = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11692 | |
Eli Friedman | 1d954f6 | 2009-08-15 21:55:26 +0000 | [diff] [blame] | 11693 | bool ZeroWidth = false; |
Douglas Gregor | 3cf538d | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 11694 | // If this is declared as a bit-field, check the bit-field. |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 11695 | if (!InvalidDecl && BitWidth) { |
Reid Kleckner | 9a3ecb0 | 2013-07-17 20:46:03 +0000 | [diff] [blame] | 11696 | BitWidth = VerifyBitField(Loc, II, T, Record->isMsStruct(Context), BitWidth, |
| 11697 | &ZeroWidth).take(); |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 11698 | if (!BitWidth) { |
| 11699 | InvalidDecl = true; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 11700 | BitWidth = nullptr; |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 11701 | ZeroWidth = false; |
| 11702 | } |
Anders Carlsson | 9f1e572 | 2008-12-06 20:33:04 +0000 | [diff] [blame] | 11703 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11704 | |
John McCall | 4bde1e1 | 2010-06-04 08:34:12 +0000 | [diff] [blame] | 11705 | // Check that 'mutable' is consistent with the type of the declaration. |
| 11706 | if (!InvalidDecl && Mutable) { |
| 11707 | unsigned DiagID = 0; |
| 11708 | if (T->isReferenceType()) |
| 11709 | DiagID = diag::err_mutable_reference; |
| 11710 | else if (T.isConstQualified()) |
| 11711 | DiagID = diag::err_mutable_const; |
| 11712 | |
| 11713 | if (DiagID) { |
| 11714 | SourceLocation ErrLoc = Loc; |
| 11715 | if (D && D->getDeclSpec().getStorageClassSpecLoc().isValid()) |
| 11716 | ErrLoc = D->getDeclSpec().getStorageClassSpecLoc(); |
| 11717 | Diag(ErrLoc, DiagID); |
| 11718 | Mutable = false; |
| 11719 | InvalidDecl = true; |
| 11720 | } |
| 11721 | } |
| 11722 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 11723 | // C++11 [class.union]p8 (DR1460): |
| 11724 | // At most one variant member of a union may have a |
| 11725 | // brace-or-equal-initializer. |
| 11726 | if (InitStyle != ICIS_NoInit) |
| 11727 | checkDuplicateDefaultInit(*this, cast<CXXRecordDecl>(Record), Loc); |
| 11728 | |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 11729 | FieldDecl *NewFD = FieldDecl::Create(Context, Record, TSSL, Loc, II, T, TInfo, |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 11730 | BitWidth, Mutable, InitStyle); |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 11731 | if (InvalidDecl) |
| 11732 | NewFD->setInvalidDecl(); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 11733 | |
Douglas Gregor | 3cf538d | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 11734 | if (PrevDecl && !isa<TagDecl>(PrevDecl)) { |
| 11735 | Diag(Loc, diag::err_duplicate_member) << II; |
| 11736 | Diag(PrevDecl->getLocation(), diag::note_previous_declaration); |
| 11737 | NewFD->setInvalidDecl(); |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 11738 | } |
| 11739 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 11740 | if (!InvalidDecl && getLangOpts().CPlusPlus) { |
Anders Carlsson | dfdfc58 | 2010-11-07 19:13:55 +0000 | [diff] [blame] | 11741 | if (Record->isUnion()) { |
| 11742 | if (const RecordType *RT = EltTy->getAs<RecordType>()) { |
| 11743 | CXXRecordDecl* RDecl = cast<CXXRecordDecl>(RT->getDecl()); |
| 11744 | if (RDecl->getDefinition()) { |
| 11745 | // C++ [class.union]p1: An object of a class with a non-trivial |
| 11746 | // constructor, a non-trivial copy constructor, a non-trivial |
| 11747 | // destructor, or a non-trivial copy assignment operator |
| 11748 | // cannot be a member of a union, nor can an array of such |
| 11749 | // objects. |
Richard Smith | e7d7c39 | 2011-10-19 20:41:51 +0000 | [diff] [blame] | 11750 | if (CheckNontrivialField(NewFD)) |
Anders Carlsson | dfdfc58 | 2010-11-07 19:13:55 +0000 | [diff] [blame] | 11751 | NewFD->setInvalidDecl(); |
| 11752 | } |
| 11753 | } |
| 11754 | |
| 11755 | // C++ [class.union]p1: If a union contains a member of reference type, |
Aaron Ballman | 76eed42 | 2013-05-30 16:20:00 +0000 | [diff] [blame] | 11756 | // the program is ill-formed, except when compiling with MSVC extensions |
| 11757 | // enabled. |
Anders Carlsson | dfdfc58 | 2010-11-07 19:13:55 +0000 | [diff] [blame] | 11758 | if (EltTy->isReferenceType()) { |
Aaron Ballman | 76eed42 | 2013-05-30 16:20:00 +0000 | [diff] [blame] | 11759 | Diag(NewFD->getLocation(), getLangOpts().MicrosoftExt ? |
| 11760 | diag::ext_union_member_of_reference_type : |
| 11761 | diag::err_union_member_of_reference_type) |
Anders Carlsson | dfdfc58 | 2010-11-07 19:13:55 +0000 | [diff] [blame] | 11762 | << NewFD->getDeclName() << EltTy; |
Aaron Ballman | 76eed42 | 2013-05-30 16:20:00 +0000 | [diff] [blame] | 11763 | if (!getLangOpts().MicrosoftExt) |
| 11764 | NewFD->setInvalidDecl(); |
Douglas Gregor | 1f2023a | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 11765 | } |
| 11766 | } |
| 11767 | } |
| 11768 | |
Douglas Gregor | 3cf538d | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 11769 | // FIXME: We need to pass in the attributes given an AST |
| 11770 | // representation, not a parser representation. |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 11771 | if (D) { |
Douglas Gregor | 92eb7d8 | 2013-05-02 23:25:32 +0000 | [diff] [blame] | 11772 | // FIXME: The current scope is almost... but not entirely... correct here. |
| 11773 | ProcessDeclAttributes(getCurScope(), NewFD, *D); |
Douglas Gregor | 3cf538d | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 11774 | |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 11775 | if (NewFD->hasAttrs()) |
| 11776 | CheckAlignasUnderalignment(NewFD); |
| 11777 | } |
| 11778 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 11779 | // In auto-retain/release, infer strong retension for fields of |
| 11780 | // retainable type. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 11781 | if (getLangOpts().ObjCAutoRefCount && inferObjCARCLifetime(NewFD)) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 11782 | NewFD->setInvalidDecl(); |
| 11783 | |
Fariborz Jahanian | f6123ca | 2009-02-19 00:22:47 +0000 | [diff] [blame] | 11784 | if (T.isObjCGCWeak()) |
Fariborz Jahanian | ed7e9ef | 2009-02-18 18:14:41 +0000 | [diff] [blame] | 11785 | Diag(Loc, diag::warn_attribute_weak_on_field); |
Anders Carlsson | ad14806 | 2008-02-16 00:29:18 +0000 | [diff] [blame] | 11786 | |
Douglas Gregor | 4dd55f5 | 2009-03-11 20:50:30 +0000 | [diff] [blame] | 11787 | NewFD->setAccess(AS); |
Steve Naroff | 5912a35 | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 11788 | return NewFD; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 11789 | } |
| 11790 | |
Argyrios Kyrtzidis | dd7744d | 2010-08-16 17:27:08 +0000 | [diff] [blame] | 11791 | bool Sema::CheckNontrivialField(FieldDecl *FD) { |
| 11792 | assert(FD); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 11793 | assert(getLangOpts().CPlusPlus && "valid check only for C++"); |
Argyrios Kyrtzidis | dd7744d | 2010-08-16 17:27:08 +0000 | [diff] [blame] | 11794 | |
Nick Lewycky | dccd04d | 2013-06-25 23:22:23 +0000 | [diff] [blame] | 11795 | if (FD->isInvalidDecl() || FD->getType()->isDependentType()) |
| 11796 | return false; |
Argyrios Kyrtzidis | dd7744d | 2010-08-16 17:27:08 +0000 | [diff] [blame] | 11797 | |
| 11798 | QualType EltTy = Context.getBaseElementType(FD->getType()); |
| 11799 | if (const RecordType *RT = EltTy->getAs<RecordType>()) { |
Richard Smith | ac71351 | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 11800 | CXXRecordDecl *RDecl = cast<CXXRecordDecl>(RT->getDecl()); |
Argyrios Kyrtzidis | dd7744d | 2010-08-16 17:27:08 +0000 | [diff] [blame] | 11801 | if (RDecl->getDefinition()) { |
| 11802 | // We check for copy constructors before constructors |
| 11803 | // because otherwise we'll never get complaints about |
| 11804 | // copy constructors. |
| 11805 | |
| 11806 | CXXSpecialMember member = CXXInvalid; |
Richard Smith | 426391c | 2012-11-16 00:53:38 +0000 | [diff] [blame] | 11807 | // We're required to check for any non-trivial constructors. Since the |
| 11808 | // implicit default constructor is suppressed if there are any |
| 11809 | // user-declared constructors, we just need to check that there is a |
| 11810 | // trivial default constructor and a trivial copy constructor. (We don't |
| 11811 | // worry about move constructors here, since this is a C++98 check.) |
| 11812 | if (RDecl->hasNonTrivialCopyConstructor()) |
Argyrios Kyrtzidis | dd7744d | 2010-08-16 17:27:08 +0000 | [diff] [blame] | 11813 | member = CXXCopyConstructor; |
Sean Hunt | 023df37 | 2011-05-09 18:22:59 +0000 | [diff] [blame] | 11814 | else if (!RDecl->hasTrivialDefaultConstructor()) |
Sean Hunt | f961ea5 | 2011-05-10 19:08:14 +0000 | [diff] [blame] | 11815 | member = CXXDefaultConstructor; |
Richard Smith | 426391c | 2012-11-16 00:53:38 +0000 | [diff] [blame] | 11816 | else if (RDecl->hasNonTrivialCopyAssignment()) |
Argyrios Kyrtzidis | dd7744d | 2010-08-16 17:27:08 +0000 | [diff] [blame] | 11817 | member = CXXCopyAssignment; |
Richard Smith | 426391c | 2012-11-16 00:53:38 +0000 | [diff] [blame] | 11818 | else if (RDecl->hasNonTrivialDestructor()) |
Argyrios Kyrtzidis | dd7744d | 2010-08-16 17:27:08 +0000 | [diff] [blame] | 11819 | member = CXXDestructor; |
| 11820 | |
| 11821 | if (member != CXXInvalid) { |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 11822 | if (!getLangOpts().CPlusPlus11 && |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 11823 | getLangOpts().ObjCAutoRefCount && RDecl->hasObjectMember()) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 11824 | // Objective-C++ ARC: it is an error to have a non-trivial field of |
| 11825 | // a union. However, system headers in Objective-C programs |
| 11826 | // occasionally have Objective-C lifetime objects within unions, |
| 11827 | // and rather than cause the program to fail, we make those |
| 11828 | // members unavailable. |
| 11829 | SourceLocation Loc = FD->getLocation(); |
| 11830 | if (getSourceManager().isInSystemHeader(Loc)) { |
| 11831 | if (!FD->hasAttr<UnavailableAttr>()) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 11832 | FD->addAttr(UnavailableAttr::CreateImplicit(Context, |
| 11833 | "this system field has retaining ownership", |
| 11834 | Loc)); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 11835 | return false; |
| 11836 | } |
| 11837 | } |
Richard Smith | e7d7c39 | 2011-10-19 20:41:51 +0000 | [diff] [blame] | 11838 | |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 11839 | Diag(FD->getLocation(), getLangOpts().CPlusPlus11 ? |
Richard Smith | e7d7c39 | 2011-10-19 20:41:51 +0000 | [diff] [blame] | 11840 | diag::warn_cxx98_compat_nontrivial_union_or_anon_struct_member : |
| 11841 | diag::err_illegal_union_or_anon_struct_member) |
| 11842 | << (int)FD->getParent()->isUnion() << FD->getDeclName() << member; |
Richard Smith | ac71351 | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 11843 | DiagnoseNontrivial(RDecl, member); |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 11844 | return !getLangOpts().CPlusPlus11; |
Argyrios Kyrtzidis | dd7744d | 2010-08-16 17:27:08 +0000 | [diff] [blame] | 11845 | } |
| 11846 | } |
| 11847 | } |
Richard Smith | ac71351 | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 11848 | |
Argyrios Kyrtzidis | dd7744d | 2010-08-16 17:27:08 +0000 | [diff] [blame] | 11849 | return false; |
| 11850 | } |
| 11851 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11852 | /// TranslateIvarVisibility - Translate visibility from a token ID to an |
Fariborz Jahanian | 89204a1 | 2007-10-01 16:53:59 +0000 | [diff] [blame] | 11853 | /// AST enum value. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 11854 | static ObjCIvarDecl::AccessControl |
Fariborz Jahanian | 89204a1 | 2007-10-01 16:53:59 +0000 | [diff] [blame] | 11855 | TranslateIvarVisibility(tok::ObjCKeywordKind ivarVisibility) { |
Steve Naroff | f13271f | 2007-09-14 23:09:53 +0000 | [diff] [blame] | 11856 | switch (ivarVisibility) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 11857 | default: llvm_unreachable("Unknown visitibility kind"); |
Chris Lattner | 33d34a6 | 2008-10-12 00:28:42 +0000 | [diff] [blame] | 11858 | case tok::objc_private: return ObjCIvarDecl::Private; |
| 11859 | case tok::objc_public: return ObjCIvarDecl::Public; |
| 11860 | case tok::objc_protected: return ObjCIvarDecl::Protected; |
| 11861 | case tok::objc_package: return ObjCIvarDecl::Package; |
Steve Naroff | f13271f | 2007-09-14 23:09:53 +0000 | [diff] [blame] | 11862 | } |
| 11863 | } |
| 11864 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11865 | /// ActOnIvar - Each ivar field of an objective-c class is passed into this |
Fariborz Jahanian | 45bc03f | 2008-04-11 16:55:42 +0000 | [diff] [blame] | 11866 | /// in order to create an IvarDecl object for it. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11867 | Decl *Sema::ActOnIvar(Scope *S, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11868 | SourceLocation DeclStart, |
Richard Trieu | f81e5a9 | 2011-09-09 02:00:50 +0000 | [diff] [blame] | 11869 | Declarator &D, Expr *BitfieldWidth, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 11870 | tok::ObjCKeywordKind Visibility) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11871 | |
Fariborz Jahanian | 1d78cc4 | 2008-04-10 23:32:45 +0000 | [diff] [blame] | 11872 | IdentifierInfo *II = D.getIdentifier(); |
| 11873 | Expr *BitWidth = (Expr*)BitfieldWidth; |
| 11874 | SourceLocation Loc = DeclStart; |
| 11875 | if (II) Loc = D.getIdentifierLoc(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11876 | |
Fariborz Jahanian | 1d78cc4 | 2008-04-10 23:32:45 +0000 | [diff] [blame] | 11877 | // FIXME: Unnamed fields can be handled in various different ways, for |
| 11878 | // example, unnamed unions inject all members into the struct namespace! |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11879 | |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 11880 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
| 11881 | QualType T = TInfo->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11882 | |
Fariborz Jahanian | 1d78cc4 | 2008-04-10 23:32:45 +0000 | [diff] [blame] | 11883 | if (BitWidth) { |
Steve Naroff | 63359c8 | 2009-02-20 17:57:11 +0000 | [diff] [blame] | 11884 | // 6.7.2.1p3, 6.7.2.1p4 |
Warren Hunt | b2969b1 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 11885 | BitWidth = VerifyBitField(Loc, II, T, /*IsMsStruct*/false, BitWidth).take(); |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 11886 | if (!BitWidth) |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 11887 | D.setInvalidType(); |
Fariborz Jahanian | 1d78cc4 | 2008-04-10 23:32:45 +0000 | [diff] [blame] | 11888 | } else { |
| 11889 | // Not a bitfield. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11890 | |
Fariborz Jahanian | 1d78cc4 | 2008-04-10 23:32:45 +0000 | [diff] [blame] | 11891 | // validate II. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11892 | |
Fariborz Jahanian | 1d78cc4 | 2008-04-10 23:32:45 +0000 | [diff] [blame] | 11893 | } |
Fariborz Jahanian | 0b7bc8e | 2010-04-26 22:07:03 +0000 | [diff] [blame] | 11894 | if (T->isReferenceType()) { |
| 11895 | Diag(Loc, diag::err_ivar_reference_type); |
| 11896 | D.setInvalidType(); |
| 11897 | } |
Fariborz Jahanian | 1d78cc4 | 2008-04-10 23:32:45 +0000 | [diff] [blame] | 11898 | // C99 6.7.2.1p8: A member of a structure or union may have any type other |
| 11899 | // than a variably modified type. |
Fariborz Jahanian | 0b7bc8e | 2010-04-26 22:07:03 +0000 | [diff] [blame] | 11900 | else if (T->isVariablyModifiedType()) { |
Anders Carlsson | 96e05bc | 2008-12-07 00:20:55 +0000 | [diff] [blame] | 11901 | Diag(Loc, diag::err_typecheck_ivar_variable_size); |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 11902 | D.setInvalidType(); |
Fariborz Jahanian | 1d78cc4 | 2008-04-10 23:32:45 +0000 | [diff] [blame] | 11903 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11904 | |
Ted Kremenek | b8db21d | 2008-07-23 18:04:17 +0000 | [diff] [blame] | 11905 | // Get the visibility (access control) for this ivar. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11906 | ObjCIvarDecl::AccessControl ac = |
Ted Kremenek | b8db21d | 2008-07-23 18:04:17 +0000 | [diff] [blame] | 11907 | Visibility != tok::objc_not_keyword ? TranslateIvarVisibility(Visibility) |
| 11908 | : ObjCIvarDecl::None; |
Fariborz Jahanian | 496b5a8 | 2009-06-05 18:16:35 +0000 | [diff] [blame] | 11909 | // Must set ivar's DeclContext to its enclosing interface. |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 11910 | ObjCContainerDecl *EnclosingDecl = cast<ObjCContainerDecl>(CurContext); |
Fariborz Jahanian | c645ddf | 2012-02-02 00:49:12 +0000 | [diff] [blame] | 11911 | if (!EnclosingDecl || EnclosingDecl->isInvalidDecl()) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 11912 | return nullptr; |
Daniel Dunbar | a19331f | 2010-04-02 18:29:09 +0000 | [diff] [blame] | 11913 | ObjCContainerDecl *EnclosingContext; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11914 | if (ObjCImplementationDecl *IMPDecl = |
Fariborz Jahanian | 496b5a8 | 2009-06-05 18:16:35 +0000 | [diff] [blame] | 11915 | dyn_cast<ObjCImplementationDecl>(EnclosingDecl)) { |
John McCall | 260611a | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 11916 | if (LangOpts.ObjCRuntime.isFragile()) { |
Fariborz Jahanian | 496b5a8 | 2009-06-05 18:16:35 +0000 | [diff] [blame] | 11917 | // Case of ivar declared in an implementation. Context is that of its class. |
Fariborz Jahanian | 000835d | 2010-08-23 18:51:39 +0000 | [diff] [blame] | 11918 | EnclosingContext = IMPDecl->getClassInterface(); |
| 11919 | assert(EnclosingContext && "Implementation has no class interface!"); |
| 11920 | } |
| 11921 | else |
| 11922 | EnclosingContext = EnclosingDecl; |
Fariborz Jahanian | 0bd0459 | 2010-04-06 22:43:48 +0000 | [diff] [blame] | 11923 | } else { |
| 11924 | if (ObjCCategoryDecl *CDecl = |
| 11925 | dyn_cast<ObjCCategoryDecl>(EnclosingDecl)) { |
John McCall | 260611a | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 11926 | if (LangOpts.ObjCRuntime.isFragile() || !CDecl->IsClassExtension()) { |
Fariborz Jahanian | 0bd0459 | 2010-04-06 22:43:48 +0000 | [diff] [blame] | 11927 | Diag(Loc, diag::err_misplaced_ivar) << CDecl->IsClassExtension(); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 11928 | return nullptr; |
Fariborz Jahanian | 0bd0459 | 2010-04-06 22:43:48 +0000 | [diff] [blame] | 11929 | } |
| 11930 | } |
Daniel Dunbar | a19331f | 2010-04-02 18:29:09 +0000 | [diff] [blame] | 11931 | EnclosingContext = EnclosingDecl; |
Fariborz Jahanian | 0bd0459 | 2010-04-06 22:43:48 +0000 | [diff] [blame] | 11932 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11933 | |
Ted Kremenek | b8db21d | 2008-07-23 18:04:17 +0000 | [diff] [blame] | 11934 | // Construct the decl. |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 11935 | ObjCIvarDecl *NewID = ObjCIvarDecl::Create(Context, EnclosingContext, |
| 11936 | DeclStart, Loc, II, T, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 11937 | TInfo, ac, (Expr *)BitfieldWidth); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11938 | |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 11939 | if (II) { |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 11940 | NamedDecl *PrevDecl = LookupSingleName(S, II, Loc, LookupMemberName, |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 11941 | ForRedeclaration); |
Fariborz Jahanian | 496b5a8 | 2009-06-05 18:16:35 +0000 | [diff] [blame] | 11942 | if (PrevDecl && isDeclInScope(PrevDecl, EnclosingContext, S) |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 11943 | && !isa<TagDecl>(PrevDecl)) { |
| 11944 | Diag(Loc, diag::err_duplicate_member) << II; |
| 11945 | Diag(PrevDecl->getLocation(), diag::note_previous_declaration); |
| 11946 | NewID->setInvalidDecl(); |
| 11947 | } |
| 11948 | } |
| 11949 | |
Ted Kremenek | b8db21d | 2008-07-23 18:04:17 +0000 | [diff] [blame] | 11950 | // Process attributes attached to the ivar. |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 11951 | ProcessDeclAttributes(S, NewID, D); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11952 | |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 11953 | if (D.isInvalidType()) |
Fariborz Jahanian | 1d78cc4 | 2008-04-10 23:32:45 +0000 | [diff] [blame] | 11954 | NewID->setInvalidDecl(); |
Ted Kremenek | b8db21d | 2008-07-23 18:04:17 +0000 | [diff] [blame] | 11955 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 11956 | // In ARC, infer 'retaining' for ivars of retainable type. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 11957 | if (getLangOpts().ObjCAutoRefCount && inferObjCARCLifetime(NewID)) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 11958 | NewID->setInvalidDecl(); |
| 11959 | |
Douglas Gregor | 591dc84 | 2011-09-12 16:11:24 +0000 | [diff] [blame] | 11960 | if (D.getDeclSpec().isModulePrivateSpecified()) |
| 11961 | NewID->setModulePrivate(); |
| 11962 | |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 11963 | if (II) { |
| 11964 | // FIXME: When interfaces are DeclContexts, we'll need to add |
| 11965 | // these to the interface. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11966 | S->AddDecl(NewID); |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 11967 | IdResolver.AddDecl(NewID); |
| 11968 | } |
Fariborz Jahanian | 8f674a8 | 2012-05-15 16:33:04 +0000 | [diff] [blame] | 11969 | |
John McCall | 260611a | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 11970 | if (LangOpts.ObjCRuntime.isNonFragile() && |
Fariborz Jahanian | 8f674a8 | 2012-05-15 16:33:04 +0000 | [diff] [blame] | 11971 | !NewID->isInvalidDecl() && isa<ObjCInterfaceDecl>(EnclosingDecl)) |
Fariborz Jahanian | dc3eb6a | 2012-05-15 17:43:16 +0000 | [diff] [blame] | 11972 | Diag(Loc, diag::warn_ivars_in_interface); |
Fariborz Jahanian | 8f674a8 | 2012-05-15 16:33:04 +0000 | [diff] [blame] | 11973 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11974 | return NewID; |
Fariborz Jahanian | 1d78cc4 | 2008-04-10 23:32:45 +0000 | [diff] [blame] | 11975 | } |
| 11976 | |
Fariborz Jahanian | d097be8 | 2010-08-23 22:46:52 +0000 | [diff] [blame] | 11977 | /// ActOnLastBitfield - This routine handles synthesized bitfields rules for |
Jordan Rose | d4582b8 | 2013-04-03 01:39:23 +0000 | [diff] [blame] | 11978 | /// class and class extensions. For every class \@interface and class |
| 11979 | /// extension \@interface, if the last ivar is a bitfield of any type, |
Fariborz Jahanian | d097be8 | 2010-08-23 22:46:52 +0000 | [diff] [blame] | 11980 | /// then add an implicit `char :0` ivar to the end of that interface. |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 11981 | void Sema::ActOnLastBitfield(SourceLocation DeclLoc, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 11982 | SmallVectorImpl<Decl *> &AllIvarDecls) { |
John McCall | 260611a | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 11983 | if (LangOpts.ObjCRuntime.isFragile() || AllIvarDecls.empty()) |
Fariborz Jahanian | d097be8 | 2010-08-23 22:46:52 +0000 | [diff] [blame] | 11984 | return; |
| 11985 | |
| 11986 | Decl *ivarDecl = AllIvarDecls[AllIvarDecls.size()-1]; |
| 11987 | ObjCIvarDecl *Ivar = cast<ObjCIvarDecl>(ivarDecl); |
| 11988 | |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 11989 | if (!Ivar->isBitField() || Ivar->getBitWidthValue(Context) == 0) |
Fariborz Jahanian | d097be8 | 2010-08-23 22:46:52 +0000 | [diff] [blame] | 11990 | return; |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 11991 | ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CurContext); |
Fariborz Jahanian | d097be8 | 2010-08-23 22:46:52 +0000 | [diff] [blame] | 11992 | if (!ID) { |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 11993 | if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(CurContext)) { |
Fariborz Jahanian | d097be8 | 2010-08-23 22:46:52 +0000 | [diff] [blame] | 11994 | if (!CD->IsClassExtension()) |
| 11995 | return; |
| 11996 | } |
| 11997 | // No need to add this to end of @implementation. |
| 11998 | else |
| 11999 | return; |
| 12000 | } |
| 12001 | // All conditions are met. Add a new bitfield to the tail end of ivars. |
Douglas Gregor | 0bbea1b | 2011-08-03 16:26:46 +0000 | [diff] [blame] | 12002 | llvm::APInt Zero(Context.getTypeSize(Context.IntTy), 0); |
| 12003 | Expr * BW = IntegerLiteral::Create(Context, Zero, Context.IntTy, DeclLoc); |
Fariborz Jahanian | d097be8 | 2010-08-23 22:46:52 +0000 | [diff] [blame] | 12004 | |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 12005 | Ivar = ObjCIvarDecl::Create(Context, cast<ObjCContainerDecl>(CurContext), |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 12006 | DeclLoc, DeclLoc, nullptr, |
Fariborz Jahanian | d097be8 | 2010-08-23 22:46:52 +0000 | [diff] [blame] | 12007 | Context.CharTy, |
Douglas Gregor | 0bbea1b | 2011-08-03 16:26:46 +0000 | [diff] [blame] | 12008 | Context.getTrivialTypeSourceInfo(Context.CharTy, |
| 12009 | DeclLoc), |
Fariborz Jahanian | d097be8 | 2010-08-23 22:46:52 +0000 | [diff] [blame] | 12010 | ObjCIvarDecl::Private, BW, |
| 12011 | true); |
| 12012 | AllIvarDecls.push_back(Ivar); |
| 12013 | } |
| 12014 | |
Robert Wilhelm | 834c058 | 2013-08-09 18:02:13 +0000 | [diff] [blame] | 12015 | void Sema::ActOnFields(Scope *S, SourceLocation RecLoc, Decl *EnclosingDecl, |
| 12016 | ArrayRef<Decl *> Fields, SourceLocation LBrac, |
| 12017 | SourceLocation RBrac, AttributeList *Attr) { |
Steve Naroff | 7421664 | 2007-09-14 22:20:54 +0000 | [diff] [blame] | 12018 | assert(EnclosingDecl && "missing record or interface decl"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12019 | |
Eric Christopher | 6dba4a1 | 2012-07-19 22:22:51 +0000 | [diff] [blame] | 12020 | // If this is an Objective-C @implementation or category and we have |
| 12021 | // new fields here we should reset the layout of the interface since |
| 12022 | // it will now change. |
| 12023 | if (!Fields.empty() && isa<ObjCContainerDecl>(EnclosingDecl)) { |
| 12024 | ObjCContainerDecl *DC = cast<ObjCContainerDecl>(EnclosingDecl); |
| 12025 | switch (DC->getKind()) { |
| 12026 | default: break; |
| 12027 | case Decl::ObjCCategory: |
| 12028 | Context.ResetObjCLayout(cast<ObjCCategoryDecl>(DC)->getClassInterface()); |
| 12029 | break; |
| 12030 | case Decl::ObjCImplementation: |
| 12031 | Context. |
| 12032 | ResetObjCLayout(cast<ObjCImplementationDecl>(DC)->getClassInterface()); |
| 12033 | break; |
| 12034 | } |
| 12035 | } |
| 12036 | |
Eli Friedman | 11e70d7 | 2012-02-07 05:00:47 +0000 | [diff] [blame] | 12037 | RecordDecl *Record = dyn_cast<RecordDecl>(EnclosingDecl); |
| 12038 | |
| 12039 | // Start counting up the number of named members; make sure to include |
| 12040 | // members of anonymous structs and unions in the total. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 12041 | unsigned NumNamedMembers = 0; |
Eli Friedman | 11e70d7 | 2012-02-07 05:00:47 +0000 | [diff] [blame] | 12042 | if (Record) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 12043 | for (const auto *I : Record->decls()) { |
| 12044 | if (const auto *IFD = dyn_cast<IndirectFieldDecl>(I)) |
Eli Friedman | 11e70d7 | 2012-02-07 05:00:47 +0000 | [diff] [blame] | 12045 | if (IFD->getDeclName()) |
| 12046 | ++NumNamedMembers; |
| 12047 | } |
| 12048 | } |
| 12049 | |
| 12050 | // Verify that all the fields are okay. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 12051 | SmallVector<FieldDecl*, 32> RecFields; |
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 12052 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 12053 | bool ARCErrReported = false; |
Robert Wilhelm | 834c058 | 2013-08-09 18:02:13 +0000 | [diff] [blame] | 12054 | for (ArrayRef<Decl *>::iterator i = Fields.begin(), end = Fields.end(); |
David Blaikie | 77b6de0 | 2011-09-22 02:58:26 +0000 | [diff] [blame] | 12055 | i != end; ++i) { |
| 12056 | FieldDecl *FD = cast<FieldDecl>(*i); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12057 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 12058 | // Get the type for the field. |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 12059 | const Type *FDTy = FD->getType().getTypePtr(); |
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 12060 | |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 12061 | if (!FD->isAnonymousStructOrUnion()) { |
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 12062 | // Remember all fields written by the user. |
| 12063 | RecFields.push_back(FD); |
| 12064 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12065 | |
Chris Lattner | 2479366 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 12066 | // If the field is already invalid for some reason, don't emit more |
| 12067 | // diagnostics about it. |
Eli Friedman | 721e77d | 2009-12-07 00:22:08 +0000 | [diff] [blame] | 12068 | if (FD->isInvalidDecl()) { |
| 12069 | EnclosingDecl->setInvalidDecl(); |
Chris Lattner | 2479366 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 12070 | continue; |
Eli Friedman | 721e77d | 2009-12-07 00:22:08 +0000 | [diff] [blame] | 12071 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12072 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 12073 | // C99 6.7.2.1p2: |
| 12074 | // A structure or union shall not contain a member with |
| 12075 | // incomplete or function type (hence, a structure shall not |
| 12076 | // contain an instance of itself, but may contain a pointer to |
| 12077 | // an instance of itself), except that the last member of a |
| 12078 | // structure with more than one named member may have incomplete |
| 12079 | // array type; such a structure (and any union containing, |
| 12080 | // possibly recursively, a member that is such a structure) |
| 12081 | // shall not be a member of a structure or an element of an |
| 12082 | // array. |
Chris Lattner | 02c642e | 2007-07-31 21:33:24 +0000 | [diff] [blame] | 12083 | if (FDTy->isFunctionType()) { |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 12084 | // Field declared as a function. |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 12085 | Diag(FD->getLocation(), diag::err_field_declared_as_function) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 12086 | << FD->getDeclName(); |
Steve Naroff | 7421664 | 2007-09-14 22:20:54 +0000 | [diff] [blame] | 12087 | FD->setInvalidDecl(); |
| 12088 | EnclosingDecl->setInvalidDecl(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 12089 | continue; |
Francois Pichet | 0924618 | 2010-09-15 00:14:08 +0000 | [diff] [blame] | 12090 | } else if (FDTy->isIncompleteArrayType() && Record && |
David Blaikie | 77b6de0 | 2011-09-22 02:58:26 +0000 | [diff] [blame] | 12091 | ((i + 1 == Fields.end() && !Record->isUnion()) || |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 12092 | ((getLangOpts().MicrosoftExt || |
| 12093 | getLangOpts().CPlusPlus) && |
David Blaikie | 77b6de0 | 2011-09-22 02:58:26 +0000 | [diff] [blame] | 12094 | (i + 1 == Fields.end() || Record->isUnion())))) { |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 12095 | // Flexible array member. |
Argyrios Kyrtzidis | d97cec3 | 2011-03-07 20:04:04 +0000 | [diff] [blame] | 12096 | // Microsoft and g++ is more permissive regarding flexible array. |
Francois Pichet | 0924618 | 2010-09-15 00:14:08 +0000 | [diff] [blame] | 12097 | // It will accept flexible array in union and also |
Anders Carlsson | 4d09e84 | 2010-10-17 23:36:12 +0000 | [diff] [blame] | 12098 | // as the sole element of a struct/class. |
David Majnemer | 633c0c2 | 2013-11-02 10:38:05 +0000 | [diff] [blame] | 12099 | unsigned DiagID = 0; |
| 12100 | if (Record->isUnion()) |
| 12101 | DiagID = getLangOpts().MicrosoftExt |
| 12102 | ? diag::ext_flexible_array_union_ms |
| 12103 | : getLangOpts().CPlusPlus |
| 12104 | ? diag::ext_flexible_array_union_gnu |
| 12105 | : diag::err_flexible_array_union; |
| 12106 | else if (Fields.size() == 1) |
| 12107 | DiagID = getLangOpts().MicrosoftExt |
| 12108 | ? diag::ext_flexible_array_empty_aggregate_ms |
| 12109 | : getLangOpts().CPlusPlus |
| 12110 | ? diag::ext_flexible_array_empty_aggregate_gnu |
| 12111 | : NumNamedMembers < 1 |
| 12112 | ? diag::err_flexible_array_empty_aggregate |
| 12113 | : 0; |
| 12114 | |
| 12115 | if (DiagID) |
| 12116 | Diag(FD->getLocation(), DiagID) << FD->getDeclName() |
| 12117 | << Record->getTagKind(); |
David Majnemer | 3a66557 | 2013-11-02 11:19:13 +0000 | [diff] [blame] | 12118 | // While the layout of types that contain virtual bases is not specified |
| 12119 | // by the C++ standard, both the Itanium and Microsoft C++ ABIs place |
| 12120 | // virtual bases after the derived members. This would make a flexible |
| 12121 | // array member declared at the end of an object not adjacent to the end |
| 12122 | // of the type. |
| 12123 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Record)) |
| 12124 | if (RD->getNumVBases() != 0) |
| 12125 | Diag(FD->getLocation(), diag::err_flexible_array_virtual_base) |
| 12126 | << FD->getDeclName() << Record->getTagKind(); |
David Majnemer | 633c0c2 | 2013-11-02 10:38:05 +0000 | [diff] [blame] | 12127 | if (!getLangOpts().C99) |
| 12128 | Diag(FD->getLocation(), diag::ext_c99_flexible_array_member) |
| 12129 | << FD->getDeclName() << Record->getTagKind(); |
| 12130 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 12131 | // If the element type has a non-trivial destructor, we would not |
| 12132 | // implicitly destroy the elements, so disallow it for now. |
| 12133 | // |
| 12134 | // FIXME: GCC allows this. We should probably either implicitly delete |
| 12135 | // the destructor of the containing class, or just allow this. |
| 12136 | QualType BaseElem = Context.getBaseElementType(FD->getType()); |
| 12137 | if (!BaseElem->isDependentType() && BaseElem.isDestructedType()) { |
| 12138 | Diag(FD->getLocation(), diag::err_flexible_array_has_nontrivial_dtor) |
Fariborz Jahanian | 2c0a540 | 2010-05-26 20:46:24 +0000 | [diff] [blame] | 12139 | << FD->getDeclName() << FD->getType(); |
Fariborz Jahanian | 4142ceb | 2010-05-26 20:19:07 +0000 | [diff] [blame] | 12140 | FD->setInvalidDecl(); |
| 12141 | EnclosingDecl->setInvalidDecl(); |
| 12142 | continue; |
| 12143 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 12144 | // Okay, we have a legal flexible array member at the end of the struct. |
Fariborz Jahanian | e267ab6 | 2007-09-14 16:27:55 +0000 | [diff] [blame] | 12145 | if (Record) |
| 12146 | Record->setHasFlexibleArrayMember(true); |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 12147 | } else if (!FDTy->isDependentType() && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12148 | RequireCompleteType(FD->getLocation(), FD->getType(), |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 12149 | diag::err_field_incomplete)) { |
| 12150 | // Incomplete type |
| 12151 | FD->setInvalidDecl(); |
| 12152 | EnclosingDecl->setInvalidDecl(); |
| 12153 | continue; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 12154 | } else if (const RecordType *FDTTy = FDTy->getAs<RecordType>()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 12155 | if (FDTTy->getDecl()->hasFlexibleArrayMember()) { |
| 12156 | // If this is a member of a union, then entire union becomes "flexible". |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 12157 | if (Record && Record->isUnion()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 12158 | Record->setHasFlexibleArrayMember(true); |
| 12159 | } else { |
| 12160 | // If this is a struct/class and this is not the last element, reject |
| 12161 | // it. Note that GCC supports variable sized arrays in the middle of |
| 12162 | // structures. |
David Blaikie | 77b6de0 | 2011-09-22 02:58:26 +0000 | [diff] [blame] | 12163 | if (i + 1 != Fields.end()) |
Douglas Gregor | e4f3e06 | 2009-03-06 23:41:27 +0000 | [diff] [blame] | 12164 | Diag(FD->getLocation(), diag::ext_variable_sized_type_in_struct) |
Chris Lattner | 740782a | 2009-04-25 18:52:45 +0000 | [diff] [blame] | 12165 | << FD->getDeclName() << FD->getType(); |
Douglas Gregor | e4f3e06 | 2009-03-06 23:41:27 +0000 | [diff] [blame] | 12166 | else { |
| 12167 | // We support flexible arrays at the end of structs in |
| 12168 | // other structs as an extension. |
| 12169 | Diag(FD->getLocation(), diag::ext_flexible_array_in_struct) |
| 12170 | << FD->getDeclName(); |
| 12171 | if (Record) |
| 12172 | Record->setHasFlexibleArrayMember(true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 12173 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 12174 | } |
| 12175 | } |
Fariborz Jahanian | 7f90b53 | 2012-08-16 22:38:41 +0000 | [diff] [blame] | 12176 | if (isa<ObjCContainerDecl>(EnclosingDecl) && |
| 12177 | RequireNonAbstractType(FD->getLocation(), FD->getType(), |
| 12178 | diag::err_abstract_type_in_decl, |
| 12179 | AbstractIvarType)) { |
| 12180 | // Ivars can not have abstract class types |
| 12181 | FD->setInvalidDecl(); |
| 12182 | } |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 12183 | if (Record && FDTTy->getDecl()->hasObjectMember()) |
| 12184 | Record->setHasObjectMember(true); |
Fariborz Jahanian | 3ac83d6 | 2013-01-25 23:57:05 +0000 | [diff] [blame] | 12185 | if (Record && FDTTy->getDecl()->hasVolatileMember()) |
| 12186 | Record->setHasVolatileMember(true); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 12187 | } else if (FDTy->isObjCObjectType()) { |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 12188 | /// A field cannot be an Objective-c object |
Fariborz Jahanian | 8eaefdc | 2011-07-26 17:58:54 +0000 | [diff] [blame] | 12189 | Diag(FD->getLocation(), diag::err_statically_allocated_object) |
| 12190 | << FixItHint::CreateInsertion(FD->getLocation(), "*"); |
| 12191 | QualType T = Context.getObjCObjectPointerType(FD->getType()); |
| 12192 | FD->setType(T); |
Douglas Gregor | 4581d45 | 2013-01-28 19:08:09 +0000 | [diff] [blame] | 12193 | } else if (getLangOpts().ObjCAutoRefCount && Record && !ARCErrReported && |
| 12194 | (!getLangOpts().CPlusPlus || Record->isUnion())) { |
| 12195 | // It's an error in ARC if a field has lifetime. |
| 12196 | // We don't want to report this in a system header, though, |
| 12197 | // so we just make the field unavailable. |
| 12198 | // FIXME: that's really not sufficient; we need to make the type |
| 12199 | // itself invalid to, say, initialize or copy. |
| 12200 | QualType T = FD->getType(); |
| 12201 | Qualifiers::ObjCLifetime lifetime = T.getObjCLifetime(); |
| 12202 | if (lifetime && lifetime != Qualifiers::OCL_ExplicitNone) { |
| 12203 | SourceLocation loc = FD->getLocation(); |
| 12204 | if (getSourceManager().isInSystemHeader(loc)) { |
| 12205 | if (!FD->hasAttr<UnavailableAttr>()) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 12206 | FD->addAttr(UnavailableAttr::CreateImplicit(Context, |
| 12207 | "this system field has retaining ownership", |
| 12208 | loc)); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 12209 | } |
Douglas Gregor | 4581d45 | 2013-01-28 19:08:09 +0000 | [diff] [blame] | 12210 | } else { |
| 12211 | Diag(FD->getLocation(), diag::err_arc_objc_object_in_tag) |
Douglas Gregor | bde67cf | 2013-01-28 20:13:44 +0000 | [diff] [blame] | 12212 | << T->isBlockPointerType() << Record->getTagKind(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 12213 | } |
Douglas Gregor | 4581d45 | 2013-01-28 19:08:09 +0000 | [diff] [blame] | 12214 | ARCErrReported = true; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 12215 | } |
Douglas Gregor | 4581d45 | 2013-01-28 19:08:09 +0000 | [diff] [blame] | 12216 | } else if (getLangOpts().ObjC1 && |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 12217 | getLangOpts().getGC() != LangOptions::NonGC && |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 12218 | Record && !Record->hasObjectMember()) { |
Douglas Gregor | 4581d45 | 2013-01-28 19:08:09 +0000 | [diff] [blame] | 12219 | if (FD->getType()->isObjCObjectPointerType() || |
| 12220 | FD->getType().isObjCGCStrong()) |
| 12221 | Record->setHasObjectMember(true); |
| 12222 | else if (Context.getAsArrayType(FD->getType())) { |
| 12223 | QualType BaseType = Context.getBaseElementType(FD->getType()); |
| 12224 | if (BaseType->isRecordType() && |
| 12225 | BaseType->getAs<RecordType>()->getDecl()->hasObjectMember()) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 12226 | Record->setHasObjectMember(true); |
Douglas Gregor | 4581d45 | 2013-01-28 19:08:09 +0000 | [diff] [blame] | 12227 | else if (BaseType->isObjCObjectPointerType() || |
| 12228 | BaseType.isObjCGCStrong()) |
| 12229 | Record->setHasObjectMember(true); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 12230 | } |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 12231 | } |
Fariborz Jahanian | 3ac83d6 | 2013-01-25 23:57:05 +0000 | [diff] [blame] | 12232 | if (Record && FD->getType().isVolatileQualified()) |
| 12233 | Record->setHasVolatileMember(true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 12234 | // Keep track of the number of named members. |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 12235 | if (FD->getIdentifier()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 12236 | ++NumNamedMembers; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 12237 | } |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 12238 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 12239 | // Okay, we successfully defined 'Record'. |
Chris Lattner | e1e7985 | 2008-02-06 00:51:33 +0000 | [diff] [blame] | 12240 | if (Record) { |
Douglas Gregor | 7a39dd0 | 2010-09-29 00:15:42 +0000 | [diff] [blame] | 12241 | bool Completed = false; |
| 12242 | if (CXXRecordDecl *CXXRecord = dyn_cast<CXXRecordDecl>(Record)) { |
| 12243 | if (!CXXRecord->isInvalidDecl()) { |
| 12244 | // Set access bits correctly on the directly-declared conversions. |
Argyrios Kyrtzidis | 9d29543 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 12245 | for (CXXRecordDecl::conversion_iterator |
| 12246 | I = CXXRecord->conversion_begin(), |
| 12247 | E = CXXRecord->conversion_end(); I != E; ++I) |
| 12248 | I.setAccess((*I)->getAccess()); |
Douglas Gregor | 7a39dd0 | 2010-09-29 00:15:42 +0000 | [diff] [blame] | 12249 | |
| 12250 | if (!CXXRecord->isDependentType()) { |
Peter Collingbourne | f51cfb8 | 2013-05-20 14:12:25 +0000 | [diff] [blame] | 12251 | if (CXXRecord->hasUserDeclaredDestructor()) { |
| 12252 | // Adjust user-defined destructor exception spec. |
| 12253 | if (getLangOpts().CPlusPlus11) |
| 12254 | AdjustDestructorExceptionSpec(CXXRecord, |
| 12255 | CXXRecord->getDestructor()); |
Peter Collingbourne | f51cfb8 | 2013-05-20 14:12:25 +0000 | [diff] [blame] | 12256 | } |
Sebastian Redl | 0ee3391 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 12257 | |
Douglas Gregor | 7a39dd0 | 2010-09-29 00:15:42 +0000 | [diff] [blame] | 12258 | // Add any implicitly-declared members to this class. |
| 12259 | AddImplicitlyDeclaredMembersToClass(CXXRecord); |
| 12260 | |
| 12261 | // If we have virtual base classes, we may end up finding multiple |
| 12262 | // final overriders for a given virtual function. Check for this |
| 12263 | // problem now. |
| 12264 | if (CXXRecord->getNumVBases()) { |
| 12265 | CXXFinalOverriderMap FinalOverriders; |
| 12266 | CXXRecord->getFinalOverriders(FinalOverriders); |
| 12267 | |
| 12268 | for (CXXFinalOverriderMap::iterator M = FinalOverriders.begin(), |
| 12269 | MEnd = FinalOverriders.end(); |
| 12270 | M != MEnd; ++M) { |
| 12271 | for (OverridingMethods::iterator SO = M->second.begin(), |
| 12272 | SOEnd = M->second.end(); |
| 12273 | SO != SOEnd; ++SO) { |
| 12274 | assert(SO->second.size() > 0 && |
| 12275 | "Virtual function without overridding functions?"); |
| 12276 | if (SO->second.size() == 1) |
| 12277 | continue; |
| 12278 | |
| 12279 | // C++ [class.virtual]p2: |
| 12280 | // In a derived class, if a virtual member function of a base |
| 12281 | // class subobject has more than one final overrider the |
| 12282 | // program is ill-formed. |
| 12283 | Diag(Record->getLocation(), diag::err_multiple_final_overriders) |
Roman Divacky | 31ba613 | 2012-09-06 15:59:27 +0000 | [diff] [blame] | 12284 | << (const NamedDecl *)M->first << Record; |
Douglas Gregor | 7a39dd0 | 2010-09-29 00:15:42 +0000 | [diff] [blame] | 12285 | Diag(M->first->getLocation(), |
| 12286 | diag::note_overridden_virtual_function); |
| 12287 | for (OverridingMethods::overriding_iterator |
| 12288 | OM = SO->second.begin(), |
| 12289 | OMEnd = SO->second.end(); |
| 12290 | OM != OMEnd; ++OM) |
| 12291 | Diag(OM->Method->getLocation(), diag::note_final_overrider) |
Roman Divacky | 31ba613 | 2012-09-06 15:59:27 +0000 | [diff] [blame] | 12292 | << (const NamedDecl *)M->first << OM->Method->getParent(); |
Douglas Gregor | 7a39dd0 | 2010-09-29 00:15:42 +0000 | [diff] [blame] | 12293 | |
| 12294 | Record->setInvalidDecl(); |
| 12295 | } |
| 12296 | } |
| 12297 | CXXRecord->completeDefinition(&FinalOverriders); |
| 12298 | Completed = true; |
| 12299 | } |
| 12300 | } |
| 12301 | } |
| 12302 | } |
| 12303 | |
| 12304 | if (!Completed) |
| 12305 | Record->completeDefinition(); |
Sebastian Redl | 0ee3391 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 12306 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 12307 | if (Record->hasAttrs()) { |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 12308 | CheckAlignasUnderalignment(Record); |
Serge Pavlov | 122e601 | 2013-06-08 13:29:58 +0000 | [diff] [blame] | 12309 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 12310 | if (const MSInheritanceAttr *IA = Record->getAttr<MSInheritanceAttr>()) |
| 12311 | checkMSInheritanceAttrOnDefinition(cast<CXXRecordDecl>(Record), |
| 12312 | IA->getRange(), IA->getBestCase(), |
| 12313 | IA->getSemanticSpelling()); |
| 12314 | } |
| 12315 | |
Serge Pavlov | 142ab06 | 2013-11-14 02:13:03 +0000 | [diff] [blame] | 12316 | // Check if the structure/union declaration is a type that can have zero |
| 12317 | // size in C. For C this is a language extension, for C++ it may cause |
| 12318 | // compatibility problems. |
| 12319 | bool CheckForZeroSize; |
Serge Pavlov | 122e601 | 2013-06-08 13:29:58 +0000 | [diff] [blame] | 12320 | if (!getLangOpts().CPlusPlus) { |
Serge Pavlov | 142ab06 | 2013-11-14 02:13:03 +0000 | [diff] [blame] | 12321 | CheckForZeroSize = true; |
| 12322 | } else { |
| 12323 | // For C++ filter out types that cannot be referenced in C code. |
| 12324 | CXXRecordDecl *CXXRecord = cast<CXXRecordDecl>(Record); |
| 12325 | CheckForZeroSize = |
| 12326 | CXXRecord->getLexicalDeclContext()->isExternCContext() && |
| 12327 | !CXXRecord->isDependentType() && |
| 12328 | CXXRecord->isCLike(); |
| 12329 | } |
| 12330 | if (CheckForZeroSize) { |
Serge Pavlov | 122e601 | 2013-06-08 13:29:58 +0000 | [diff] [blame] | 12331 | bool ZeroSize = true; |
Serge Pavlov | 0dcea35 | 2013-06-17 17:18:51 +0000 | [diff] [blame] | 12332 | bool IsEmpty = true; |
| 12333 | unsigned NonBitFields = 0; |
Serge Pavlov | 122e601 | 2013-06-08 13:29:58 +0000 | [diff] [blame] | 12334 | for (RecordDecl::field_iterator I = Record->field_begin(), |
Serge Pavlov | 0dcea35 | 2013-06-17 17:18:51 +0000 | [diff] [blame] | 12335 | E = Record->field_end(); |
| 12336 | (NonBitFields == 0 || ZeroSize) && I != E; ++I) { |
| 12337 | IsEmpty = false; |
Serge Pavlov | 122e601 | 2013-06-08 13:29:58 +0000 | [diff] [blame] | 12338 | if (I->isUnnamedBitfield()) { |
Serge Pavlov | 122e601 | 2013-06-08 13:29:58 +0000 | [diff] [blame] | 12339 | if (I->getBitWidthValue(Context) > 0) |
| 12340 | ZeroSize = false; |
| 12341 | } else { |
Serge Pavlov | 0dcea35 | 2013-06-17 17:18:51 +0000 | [diff] [blame] | 12342 | ++NonBitFields; |
| 12343 | QualType FieldType = I->getType(); |
| 12344 | if (FieldType->isIncompleteType() || |
| 12345 | !Context.getTypeSizeInChars(FieldType).isZero()) |
| 12346 | ZeroSize = false; |
Serge Pavlov | 122e601 | 2013-06-08 13:29:58 +0000 | [diff] [blame] | 12347 | } |
| 12348 | } |
| 12349 | |
Serge Pavlov | 142ab06 | 2013-11-14 02:13:03 +0000 | [diff] [blame] | 12350 | // Empty structs are an extension in C (C99 6.7.2.1p7). They are |
| 12351 | // allowed in C++, but warn if its declaration is inside |
| 12352 | // extern "C" block. |
| 12353 | if (ZeroSize) { |
| 12354 | Diag(RecLoc, getLangOpts().CPlusPlus ? |
| 12355 | diag::warn_zero_size_struct_union_in_extern_c : |
| 12356 | diag::warn_zero_size_struct_union_compat) |
| 12357 | << IsEmpty << Record->isUnion() << (NonBitFields > 1); |
| 12358 | } |
Serge Pavlov | 122e601 | 2013-06-08 13:29:58 +0000 | [diff] [blame] | 12359 | |
Serge Pavlov | 142ab06 | 2013-11-14 02:13:03 +0000 | [diff] [blame] | 12360 | // Structs without named members are extension in C (C99 6.7.2.1p7), |
| 12361 | // but are accepted by GCC. |
| 12362 | if (NonBitFields == 0 && !getLangOpts().CPlusPlus) { |
| 12363 | Diag(RecLoc, IsEmpty ? diag::ext_empty_struct_union : |
| 12364 | diag::ext_no_named_members_in_struct_union) |
| 12365 | << Record->isUnion(); |
Serge Pavlov | 122e601 | 2013-06-08 13:29:58 +0000 | [diff] [blame] | 12366 | } |
| 12367 | } |
Chris Lattner | e1e7985 | 2008-02-06 00:51:33 +0000 | [diff] [blame] | 12368 | } else { |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 12369 | ObjCIvarDecl **ClsFields = |
| 12370 | reinterpret_cast<ObjCIvarDecl**>(RecFields.data()); |
Fariborz Jahanian | 60f8c86 | 2008-12-13 20:28:25 +0000 | [diff] [blame] | 12371 | if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(EnclosingDecl)) { |
Douglas Gregor | 05c272f | 2011-12-15 22:34:59 +0000 | [diff] [blame] | 12372 | ID->setEndOfDefinitionLoc(RBrac); |
Fariborz Jahanian | 496b5a8 | 2009-06-05 18:16:35 +0000 | [diff] [blame] | 12373 | // Add ivar's to class's DeclContext. |
| 12374 | for (unsigned i = 0, e = RecFields.size(); i != e; ++i) { |
| 12375 | ClsFields[i]->setLexicalDeclContext(ID); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 12376 | ID->addDecl(ClsFields[i]); |
Fariborz Jahanian | 496b5a8 | 2009-06-05 18:16:35 +0000 | [diff] [blame] | 12377 | } |
Fariborz Jahanian | 3281eff | 2008-12-16 01:08:35 +0000 | [diff] [blame] | 12378 | // Must enforce the rule that ivars in the base classes may not be |
| 12379 | // duplicates. |
Fariborz Jahanian | f914b97 | 2010-02-23 23:41:11 +0000 | [diff] [blame] | 12380 | if (ID->getSuperClass()) |
| 12381 | DiagnoseDuplicateIvars(ID, ID->getSuperClass()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12382 | } else if (ObjCImplementationDecl *IMPDecl = |
Chris Lattner | 1829a6d | 2009-02-23 22:00:08 +0000 | [diff] [blame] | 12383 | dyn_cast<ObjCImplementationDecl>(EnclosingDecl)) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 12384 | assert(IMPDecl && "ActOnFields - missing ObjCImplementationDecl"); |
Fariborz Jahanian | 496b5a8 | 2009-06-05 18:16:35 +0000 | [diff] [blame] | 12385 | for (unsigned I = 0, N = RecFields.size(); I != N; ++I) |
| 12386 | // Ivar declared in @implementation never belongs to the implementation. |
| 12387 | // Only it is in implementation's lexical context. |
Douglas Gregor | 8f36aba | 2009-04-23 03:23:08 +0000 | [diff] [blame] | 12388 | ClsFields[I]->setLexicalDeclContext(IMPDecl); |
Fariborz Jahanian | 3a3ca1b | 2007-10-31 18:48:14 +0000 | [diff] [blame] | 12389 | CheckImplementationIvars(IMPDecl, ClsFields, RecFields.size(), RBrac); |
Fariborz Jahanian | af30029 | 2012-02-20 20:09:20 +0000 | [diff] [blame] | 12390 | IMPDecl->setIvarLBraceLoc(LBrac); |
| 12391 | IMPDecl->setIvarRBraceLoc(RBrac); |
Fariborz Jahanian | 83c481a | 2010-02-22 23:04:20 +0000 | [diff] [blame] | 12392 | } else if (ObjCCategoryDecl *CDecl = |
| 12393 | dyn_cast<ObjCCategoryDecl>(EnclosingDecl)) { |
Fariborz Jahanian | 0bd0459 | 2010-04-06 22:43:48 +0000 | [diff] [blame] | 12394 | // case of ivars in class extension; all other cases have been |
| 12395 | // reported as errors elsewhere. |
| 12396 | // FIXME. Class extension does not have a LocEnd field. |
| 12397 | // CDecl->setLocEnd(RBrac); |
| 12398 | // Add ivar's to class extension's DeclContext. |
Fariborz Jahanian | 3ff86f7 | 2011-10-21 18:03:52 +0000 | [diff] [blame] | 12399 | // Diagnose redeclaration of private ivars. |
| 12400 | ObjCInterfaceDecl *IDecl = CDecl->getClassInterface(); |
Fariborz Jahanian | 0bd0459 | 2010-04-06 22:43:48 +0000 | [diff] [blame] | 12401 | for (unsigned i = 0, e = RecFields.size(); i != e; ++i) { |
Fariborz Jahanian | 3ff86f7 | 2011-10-21 18:03:52 +0000 | [diff] [blame] | 12402 | if (IDecl) { |
| 12403 | if (const ObjCIvarDecl *ClsIvar = |
| 12404 | IDecl->getIvarDecl(ClsFields[i]->getIdentifier())) { |
| 12405 | Diag(ClsFields[i]->getLocation(), |
| 12406 | diag::err_duplicate_ivar_declaration); |
| 12407 | Diag(ClsIvar->getLocation(), diag::note_previous_definition); |
| 12408 | continue; |
| 12409 | } |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 12410 | for (const auto *Ext : IDecl->known_extensions()) { |
Douglas Gregor | d329724 | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 12411 | if (const ObjCIvarDecl *ClsExtIvar |
| 12412 | = Ext->getIvarDecl(ClsFields[i]->getIdentifier())) { |
Fariborz Jahanian | 3ff86f7 | 2011-10-21 18:03:52 +0000 | [diff] [blame] | 12413 | Diag(ClsFields[i]->getLocation(), |
| 12414 | diag::err_duplicate_ivar_declaration); |
| 12415 | Diag(ClsExtIvar->getLocation(), diag::note_previous_definition); |
| 12416 | continue; |
| 12417 | } |
| 12418 | } |
| 12419 | } |
Fariborz Jahanian | 0bd0459 | 2010-04-06 22:43:48 +0000 | [diff] [blame] | 12420 | ClsFields[i]->setLexicalDeclContext(CDecl); |
| 12421 | CDecl->addDecl(ClsFields[i]); |
Fariborz Jahanian | 83c481a | 2010-02-22 23:04:20 +0000 | [diff] [blame] | 12422 | } |
Fariborz Jahanian | af30029 | 2012-02-20 20:09:20 +0000 | [diff] [blame] | 12423 | CDecl->setIvarLBraceLoc(LBrac); |
| 12424 | CDecl->setIvarRBraceLoc(RBrac); |
Fariborz Jahanian | d0b90bf | 2007-09-26 18:27:25 +0000 | [diff] [blame] | 12425 | } |
Fariborz Jahanian | b04a021 | 2007-09-14 21:08:27 +0000 | [diff] [blame] | 12426 | } |
Daniel Dunbar | 7d07664 | 2008-10-03 17:33:35 +0000 | [diff] [blame] | 12427 | |
| 12428 | if (Attr) |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 12429 | ProcessDeclAttributeList(S, Record, Attr); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 12430 | } |
| 12431 | |
Douglas Gregor | 677e4fe | 2010-02-01 23:36:03 +0000 | [diff] [blame] | 12432 | /// \brief Determine whether the given integral value is representable within |
| 12433 | /// the given type T. |
| 12434 | static bool isRepresentableIntegerValue(ASTContext &Context, |
| 12435 | llvm::APSInt &Value, |
| 12436 | QualType T) { |
Douglas Gregor | 9d3347a | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 12437 | assert(T->isIntegralType(Context) && "Integral type required!"); |
Douglas Gregor | af68d4e | 2010-04-15 15:53:31 +0000 | [diff] [blame] | 12438 | unsigned BitWidth = Context.getIntWidth(T); |
Douglas Gregor | 677e4fe | 2010-02-01 23:36:03 +0000 | [diff] [blame] | 12439 | |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 12440 | if (Value.isUnsigned() || Value.isNonNegative()) { |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 12441 | if (T->isSignedIntegerOrEnumerationType()) |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 12442 | --BitWidth; |
| 12443 | return Value.getActiveBits() <= BitWidth; |
| 12444 | } |
Douglas Gregor | 677e4fe | 2010-02-01 23:36:03 +0000 | [diff] [blame] | 12445 | return Value.getMinSignedBits() <= BitWidth; |
| 12446 | } |
| 12447 | |
| 12448 | // \brief Given an integral type, return the next larger integral type |
| 12449 | // (or a NULL type of no such type exists). |
| 12450 | static QualType getNextLargerIntegralType(ASTContext &Context, QualType T) { |
| 12451 | // FIXME: Int128/UInt128 support, which also needs to be introduced into |
| 12452 | // enum checking below. |
Douglas Gregor | 9d3347a | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 12453 | assert(T->isIntegralType(Context) && "Integral type required!"); |
Douglas Gregor | 677e4fe | 2010-02-01 23:36:03 +0000 | [diff] [blame] | 12454 | const unsigned NumTypes = 4; |
| 12455 | QualType SignedIntegralTypes[NumTypes] = { |
| 12456 | Context.ShortTy, Context.IntTy, Context.LongTy, Context.LongLongTy |
| 12457 | }; |
| 12458 | QualType UnsignedIntegralTypes[NumTypes] = { |
| 12459 | Context.UnsignedShortTy, Context.UnsignedIntTy, Context.UnsignedLongTy, |
| 12460 | Context.UnsignedLongLongTy |
| 12461 | }; |
| 12462 | |
| 12463 | unsigned BitWidth = Context.getTypeSize(T); |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 12464 | QualType *Types = T->isSignedIntegerOrEnumerationType()? SignedIntegralTypes |
| 12465 | : UnsignedIntegralTypes; |
Douglas Gregor | 677e4fe | 2010-02-01 23:36:03 +0000 | [diff] [blame] | 12466 | for (unsigned I = 0; I != NumTypes; ++I) |
| 12467 | if (Context.getTypeSize(Types[I]) > BitWidth) |
| 12468 | return Types[I]; |
| 12469 | |
| 12470 | return QualType(); |
| 12471 | } |
| 12472 | |
Douglas Gregor | 879fd49 | 2009-03-17 19:05:46 +0000 | [diff] [blame] | 12473 | EnumConstantDecl *Sema::CheckEnumConstant(EnumDecl *Enum, |
| 12474 | EnumConstantDecl *LastEnumConst, |
| 12475 | SourceLocation IdLoc, |
| 12476 | IdentifierInfo *Id, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 12477 | Expr *Val) { |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 12478 | unsigned IntWidth = Context.getTargetInfo().getIntWidth(); |
Douglas Gregor | 677e4fe | 2010-02-01 23:36:03 +0000 | [diff] [blame] | 12479 | llvm::APSInt EnumVal(IntWidth); |
Douglas Gregor | 879fd49 | 2009-03-17 19:05:46 +0000 | [diff] [blame] | 12480 | QualType EltTy; |
Douglas Gregor | 0c9e479 | 2010-12-16 00:24:44 +0000 | [diff] [blame] | 12481 | |
| 12482 | if (Val && DiagnoseUnexpandedParameterPack(Val, UPPC_EnumeratorValue)) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 12483 | Val = nullptr; |
Douglas Gregor | 0c9e479 | 2010-12-16 00:24:44 +0000 | [diff] [blame] | 12484 | |
Eli Friedman | 19efa3e | 2011-12-06 00:10:34 +0000 | [diff] [blame] | 12485 | if (Val) |
| 12486 | Val = DefaultLvalueConversion(Val).take(); |
| 12487 | |
Douglas Gregor | 4912c34 | 2009-11-06 00:03:12 +0000 | [diff] [blame] | 12488 | if (Val) { |
Douglas Gregor | 9b9edd6 | 2010-03-02 17:53:14 +0000 | [diff] [blame] | 12489 | if (Enum->isDependentType() || Val->isTypeDependent()) |
Douglas Gregor | 4912c34 | 2009-11-06 00:03:12 +0000 | [diff] [blame] | 12490 | EltTy = Context.DependentTy; |
| 12491 | else { |
Douglas Gregor | 4912c34 | 2009-11-06 00:03:12 +0000 | [diff] [blame] | 12492 | SourceLocation ExpLoc; |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 12493 | if (getLangOpts().CPlusPlus11 && Enum->isFixed() && |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 12494 | !getLangOpts().MSVCCompat) { |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 12495 | // C++11 [dcl.enum]p5: If the underlying type is fixed, [...] the |
| 12496 | // constant-expression in the enumerator-definition shall be a converted |
| 12497 | // constant expression of the underlying type. |
| 12498 | EltTy = Enum->getIntegerType(); |
| 12499 | ExprResult Converted = |
| 12500 | CheckConvertedConstantExpression(Val, EltTy, EnumVal, |
| 12501 | CCEK_Enumerator); |
| 12502 | if (Converted.isInvalid()) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 12503 | Val = nullptr; |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 12504 | else |
| 12505 | Val = Converted.take(); |
| 12506 | } else if (!Val->isValueDependent() && |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 12507 | !(Val = VerifyIntegerConstantExpression(Val, |
| 12508 | &EnumVal).take())) { |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 12509 | // C99 6.7.2.2p2: Make sure we have an integer constant expression. |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 12510 | } else { |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 12511 | if (Enum->isFixed()) { |
| 12512 | EltTy = Enum->getIntegerType(); |
| 12513 | |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 12514 | // In Obj-C and Microsoft mode, require the enumeration value to be |
| 12515 | // representable in the underlying type of the enumeration. In C++11, |
| 12516 | // we perform a non-narrowing conversion as part of converted constant |
| 12517 | // expression checking. |
Francois Pichet | 842e7a2 | 2010-10-18 15:01:13 +0000 | [diff] [blame] | 12518 | if (!isRepresentableIntegerValue(Context, EnumVal, EltTy)) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 12519 | if (getLangOpts().MSVCCompat) { |
Francois Pichet | 842e7a2 | 2010-10-18 15:01:13 +0000 | [diff] [blame] | 12520 | Diag(IdLoc, diag::ext_enumerator_too_large) << EltTy; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 12521 | Val = ImpCastExprToType(Val, EltTy, CK_IntegralCast).take(); |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 12522 | } else |
| 12523 | Diag(IdLoc, diag::err_enumerator_too_large) << EltTy; |
Francois Pichet | 842e7a2 | 2010-10-18 15:01:13 +0000 | [diff] [blame] | 12524 | } else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 12525 | Val = ImpCastExprToType(Val, EltTy, CK_IntegralCast).take(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 12526 | } else if (getLangOpts().CPlusPlus) { |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 12527 | // C++11 [dcl.enum]p5: |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 12528 | // If the underlying type is not fixed, the type of each enumerator |
| 12529 | // is the type of its initializing value: |
| 12530 | // - If an initializer is specified for an enumerator, the |
| 12531 | // initializing value has the same type as the expression. |
| 12532 | EltTy = Val->getType(); |
Eli Friedman | 04ca252 | 2012-02-07 04:34:38 +0000 | [diff] [blame] | 12533 | } else { |
| 12534 | // C99 6.7.2.2p2: |
| 12535 | // The expression that defines the value of an enumeration constant |
| 12536 | // shall be an integer constant expression that has a value |
| 12537 | // representable as an int. |
| 12538 | |
| 12539 | // Complain if the value is not representable in an int. |
| 12540 | if (!isRepresentableIntegerValue(Context, EnumVal, Context.IntTy)) |
| 12541 | Diag(IdLoc, diag::ext_enum_value_not_int) |
| 12542 | << EnumVal.toString(10) << Val->getSourceRange() |
| 12543 | << (EnumVal.isUnsigned() || EnumVal.isNonNegative()); |
| 12544 | else if (!Context.hasSameType(Val->getType(), Context.IntTy)) { |
| 12545 | // Force the type of the expression to 'int'. |
| 12546 | Val = ImpCastExprToType(Val, Context.IntTy, CK_IntegralCast).take(); |
| 12547 | } |
| 12548 | EltTy = Val->getType(); |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 12549 | } |
Douglas Gregor | 4912c34 | 2009-11-06 00:03:12 +0000 | [diff] [blame] | 12550 | } |
Douglas Gregor | 879fd49 | 2009-03-17 19:05:46 +0000 | [diff] [blame] | 12551 | } |
| 12552 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12553 | |
Douglas Gregor | 879fd49 | 2009-03-17 19:05:46 +0000 | [diff] [blame] | 12554 | if (!Val) { |
Eli Friedman | ed0716b | 2009-12-11 01:34:50 +0000 | [diff] [blame] | 12555 | if (Enum->isDependentType()) |
| 12556 | EltTy = Context.DependentTy; |
Douglas Gregor | 677e4fe | 2010-02-01 23:36:03 +0000 | [diff] [blame] | 12557 | else if (!LastEnumConst) { |
| 12558 | // C++0x [dcl.enum]p5: |
| 12559 | // If the underlying type is not fixed, the type of each enumerator |
| 12560 | // is the type of its initializing value: |
| 12561 | // - If no initializer is specified for the first enumerator, the |
| 12562 | // initializing value has an unspecified integral type. |
| 12563 | // |
| 12564 | // GCC uses 'int' for its unspecified integral type, as does |
| 12565 | // C99 6.7.2.2p3. |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 12566 | if (Enum->isFixed()) { |
| 12567 | EltTy = Enum->getIntegerType(); |
| 12568 | } |
| 12569 | else { |
| 12570 | EltTy = Context.IntTy; |
| 12571 | } |
Douglas Gregor | 677e4fe | 2010-02-01 23:36:03 +0000 | [diff] [blame] | 12572 | } else { |
Douglas Gregor | 879fd49 | 2009-03-17 19:05:46 +0000 | [diff] [blame] | 12573 | // Assign the last value + 1. |
| 12574 | EnumVal = LastEnumConst->getInitVal(); |
| 12575 | ++EnumVal; |
Douglas Gregor | 677e4fe | 2010-02-01 23:36:03 +0000 | [diff] [blame] | 12576 | EltTy = LastEnumConst->getType(); |
Douglas Gregor | 879fd49 | 2009-03-17 19:05:46 +0000 | [diff] [blame] | 12577 | |
| 12578 | // Check for overflow on increment. |
Douglas Gregor | 677e4fe | 2010-02-01 23:36:03 +0000 | [diff] [blame] | 12579 | if (EnumVal < LastEnumConst->getInitVal()) { |
| 12580 | // C++0x [dcl.enum]p5: |
| 12581 | // If the underlying type is not fixed, the type of each enumerator |
| 12582 | // is the type of its initializing value: |
| 12583 | // |
| 12584 | // - Otherwise the type of the initializing value is the same as |
| 12585 | // the type of the initializing value of the preceding enumerator |
| 12586 | // unless the incremented value is not representable in that type, |
| 12587 | // in which case the type is an unspecified integral type |
| 12588 | // sufficient to contain the incremented value. If no such type |
| 12589 | // exists, the program is ill-formed. |
| 12590 | QualType T = getNextLargerIntegralType(Context, EltTy); |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 12591 | if (T.isNull() || Enum->isFixed()) { |
Douglas Gregor | 677e4fe | 2010-02-01 23:36:03 +0000 | [diff] [blame] | 12592 | // There is no integral type larger enough to represent this |
| 12593 | // value. Complain, then allow the value to wrap around. |
| 12594 | EnumVal = LastEnumConst->getInitVal(); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 12595 | EnumVal = EnumVal.zext(EnumVal.getBitWidth() * 2); |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 12596 | ++EnumVal; |
| 12597 | if (Enum->isFixed()) |
| 12598 | // When the underlying type is fixed, this is ill-formed. |
| 12599 | Diag(IdLoc, diag::err_enumerator_wrapped) |
| 12600 | << EnumVal.toString(10) |
| 12601 | << EltTy; |
| 12602 | else |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 12603 | Diag(IdLoc, diag::ext_enumerator_increment_too_large) |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 12604 | << EnumVal.toString(10); |
Douglas Gregor | 677e4fe | 2010-02-01 23:36:03 +0000 | [diff] [blame] | 12605 | } else { |
| 12606 | EltTy = T; |
| 12607 | } |
| 12608 | |
| 12609 | // Retrieve the last enumerator's value, extent that type to the |
| 12610 | // type that is supposed to be large enough to represent the incremented |
| 12611 | // value, then increment. |
| 12612 | EnumVal = LastEnumConst->getInitVal(); |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 12613 | EnumVal.setIsSigned(EltTy->isSignedIntegerOrEnumerationType()); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 12614 | EnumVal = EnumVal.zextOrTrunc(Context.getIntWidth(EltTy)); |
Douglas Gregor | 677e4fe | 2010-02-01 23:36:03 +0000 | [diff] [blame] | 12615 | ++EnumVal; |
| 12616 | |
| 12617 | // If we're not in C++, diagnose the overflow of enumerator values, |
| 12618 | // which in C99 means that the enumerator value is not representable in |
| 12619 | // an int (C99 6.7.2.2p2). However, we support GCC's extension that |
| 12620 | // permits enumerator values that are representable in some larger |
| 12621 | // integral type. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 12622 | if (!getLangOpts().CPlusPlus && !T.isNull()) |
Douglas Gregor | 677e4fe | 2010-02-01 23:36:03 +0000 | [diff] [blame] | 12623 | Diag(IdLoc, diag::warn_enum_value_overflow); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 12624 | } else if (!getLangOpts().CPlusPlus && |
Douglas Gregor | 677e4fe | 2010-02-01 23:36:03 +0000 | [diff] [blame] | 12625 | !isRepresentableIntegerValue(Context, EnumVal, EltTy)) { |
| 12626 | // Enforce C99 6.7.2.2p2 even when we compute the next value. |
| 12627 | Diag(IdLoc, diag::ext_enum_value_not_int) |
| 12628 | << EnumVal.toString(10) << 1; |
| 12629 | } |
Douglas Gregor | 879fd49 | 2009-03-17 19:05:46 +0000 | [diff] [blame] | 12630 | } |
| 12631 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12632 | |
Douglas Gregor | 9b9edd6 | 2010-03-02 17:53:14 +0000 | [diff] [blame] | 12633 | if (!EltTy->isDependentType()) { |
Douglas Gregor | 677e4fe | 2010-02-01 23:36:03 +0000 | [diff] [blame] | 12634 | // Make the enumerator value match the signedness and size of the |
| 12635 | // enumerator's type. |
Eli Friedman | 04ca252 | 2012-02-07 04:34:38 +0000 | [diff] [blame] | 12636 | EnumVal = EnumVal.extOrTrunc(Context.getIntWidth(EltTy)); |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 12637 | EnumVal.setIsSigned(EltTy->isSignedIntegerOrEnumerationType()); |
Douglas Gregor | 677e4fe | 2010-02-01 23:36:03 +0000 | [diff] [blame] | 12638 | } |
Douglas Gregor | 4912c34 | 2009-11-06 00:03:12 +0000 | [diff] [blame] | 12639 | |
Douglas Gregor | 879fd49 | 2009-03-17 19:05:46 +0000 | [diff] [blame] | 12640 | return EnumConstantDecl::Create(Context, Enum, IdLoc, Id, EltTy, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12641 | Val, EnumVal); |
Douglas Gregor | 879fd49 | 2009-03-17 19:05:46 +0000 | [diff] [blame] | 12642 | } |
| 12643 | |
| 12644 | |
John McCall | 5b629aa | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 12645 | Decl *Sema::ActOnEnumConstant(Scope *S, Decl *theEnumDecl, Decl *lastEnumConst, |
| 12646 | SourceLocation IdLoc, IdentifierInfo *Id, |
| 12647 | AttributeList *Attr, |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 12648 | SourceLocation EqualLoc, Expr *Val) { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 12649 | EnumDecl *TheEnumDecl = cast<EnumDecl>(theEnumDecl); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 12650 | EnumConstantDecl *LastEnumConst = |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 12651 | cast_or_null<EnumConstantDecl>(lastEnumConst); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 12652 | |
Chris Lattner | 31e0572 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 12653 | // The scope passed in may not be a decl scope. Zip up the scope tree until |
| 12654 | // we find one that is. |
Douglas Gregor | 1a0d31a | 2009-01-12 18:45:55 +0000 | [diff] [blame] | 12655 | S = getNonFieldDeclScope(S); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12656 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 12657 | // Verify that there isn't already something declared with this name in this |
| 12658 | // scope. |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 12659 | NamedDecl *PrevDecl = LookupSingleName(S, Id, IdLoc, LookupOrdinaryName, |
Douglas Gregor | e39fe72 | 2010-01-19 06:06:57 +0000 | [diff] [blame] | 12660 | ForRedeclaration); |
Douglas Gregor | f57172b | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 12661 | if (PrevDecl && PrevDecl->isTemplateParameter()) { |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 12662 | // Maybe we will complain about the shadowed template parameter. |
| 12663 | DiagnoseTemplateParameterShadow(IdLoc, PrevDecl); |
| 12664 | // Just pretend that we didn't see the previous declaration. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 12665 | PrevDecl = nullptr; |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 12666 | } |
| 12667 | |
| 12668 | if (PrevDecl) { |
Argyrios Kyrtzidis | 0ff12f0 | 2008-07-16 21:01:53 +0000 | [diff] [blame] | 12669 | // When in C++, we may get a TagDecl with the same name; in this case the |
| 12670 | // enum constant will 'hide' the tag. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 12671 | assert((getLangOpts().CPlusPlus || !isa<TagDecl>(PrevDecl)) && |
Argyrios Kyrtzidis | 0ff12f0 | 2008-07-16 21:01:53 +0000 | [diff] [blame] | 12672 | "Received TagDecl when not in C++!"); |
Argyrios Kyrtzidis | 15a12d0 | 2008-09-09 21:18:04 +0000 | [diff] [blame] | 12673 | if (!isa<TagDecl>(PrevDecl) && isDeclInScope(PrevDecl, CurContext, S)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 12674 | if (isa<EnumConstantDecl>(PrevDecl)) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 12675 | Diag(IdLoc, diag::err_redefinition_of_enumerator) << Id; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 12676 | else |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 12677 | Diag(IdLoc, diag::err_redefinition) << Id; |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 12678 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 12679 | return nullptr; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 12680 | } |
| 12681 | } |
| 12682 | |
Aaron Ballman | f816787 | 2012-07-19 03:12:23 +0000 | [diff] [blame] | 12683 | // C++ [class.mem]p15: |
| 12684 | // If T is the name of a class, then each of the following shall have a name |
| 12685 | // different from T: |
| 12686 | // - every enumerator of every member of class T that is an unscoped |
| 12687 | // enumerated type |
Douglas Gregor | a6e937c | 2010-10-15 13:21:21 +0000 | [diff] [blame] | 12688 | if (CXXRecordDecl *Record |
| 12689 | = dyn_cast<CXXRecordDecl>( |
| 12690 | TheEnumDecl->getDeclContext()->getRedeclContext())) |
Aaron Ballman | f816787 | 2012-07-19 03:12:23 +0000 | [diff] [blame] | 12691 | if (!TheEnumDecl->isScoped() && |
| 12692 | Record->getIdentifier() && Record->getIdentifier() == Id) |
Douglas Gregor | a6e937c | 2010-10-15 13:21:21 +0000 | [diff] [blame] | 12693 | Diag(IdLoc, diag::err_member_name_of_class) << Id; |
| 12694 | |
John McCall | 5b629aa | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 12695 | EnumConstantDecl *New = |
| 12696 | CheckEnumConstant(TheEnumDecl, LastEnumConst, IdLoc, Id, Val); |
Chris Lattner | 421a23d | 2007-08-27 21:16:18 +0000 | [diff] [blame] | 12697 | |
John McCall | 92f8831 | 2010-01-23 00:46:32 +0000 | [diff] [blame] | 12698 | if (New) { |
John McCall | 5b629aa | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 12699 | // Process attributes. |
| 12700 | if (Attr) ProcessDeclAttributeList(S, New, Attr); |
| 12701 | |
| 12702 | // Register this decl in the current scope stack. |
John McCall | 92f8831 | 2010-01-23 00:46:32 +0000 | [diff] [blame] | 12703 | New->setAccess(TheEnumDecl->getAccess()); |
Douglas Gregor | 879fd49 | 2009-03-17 19:05:46 +0000 | [diff] [blame] | 12704 | PushOnScopeChains(New, S); |
John McCall | 92f8831 | 2010-01-23 00:46:32 +0000 | [diff] [blame] | 12705 | } |
Douglas Gregor | 45579f5 | 2008-12-17 02:04:30 +0000 | [diff] [blame] | 12706 | |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 12707 | ActOnDocumentableDecl(New); |
| 12708 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 12709 | return New; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 12710 | } |
| 12711 | |
Ted Kremenek | a734a0e | 2012-12-22 01:34:09 +0000 | [diff] [blame] | 12712 | // Returns true when the enum initial expression does not trigger the |
| 12713 | // duplicate enum warning. A few common cases are exempted as follows: |
| 12714 | // Element2 = Element1 |
| 12715 | // Element2 = Element1 + 1 |
| 12716 | // Element2 = Element1 - 1 |
| 12717 | // Where Element2 and Element1 are from the same enum. |
| 12718 | static bool ValidDuplicateEnum(EnumConstantDecl *ECD, EnumDecl *Enum) { |
| 12719 | Expr *InitExpr = ECD->getInitExpr(); |
| 12720 | if (!InitExpr) |
| 12721 | return true; |
| 12722 | InitExpr = InitExpr->IgnoreImpCasts(); |
| 12723 | |
| 12724 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(InitExpr)) { |
| 12725 | if (!BO->isAdditiveOp()) |
| 12726 | return true; |
| 12727 | IntegerLiteral *IL = dyn_cast<IntegerLiteral>(BO->getRHS()); |
| 12728 | if (!IL) |
| 12729 | return true; |
| 12730 | if (IL->getValue() != 1) |
| 12731 | return true; |
| 12732 | |
| 12733 | InitExpr = BO->getLHS(); |
| 12734 | } |
| 12735 | |
| 12736 | // This checks if the elements are from the same enum. |
| 12737 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(InitExpr); |
| 12738 | if (!DRE) |
| 12739 | return true; |
| 12740 | |
| 12741 | EnumConstantDecl *EnumConstant = dyn_cast<EnumConstantDecl>(DRE->getDecl()); |
| 12742 | if (!EnumConstant) |
| 12743 | return true; |
| 12744 | |
| 12745 | if (cast<EnumDecl>(TagDecl::castFromDeclContext(ECD->getDeclContext())) != |
| 12746 | Enum) |
| 12747 | return true; |
| 12748 | |
| 12749 | return false; |
| 12750 | } |
| 12751 | |
| 12752 | struct DupKey { |
| 12753 | int64_t val; |
| 12754 | bool isTombstoneOrEmptyKey; |
| 12755 | DupKey(int64_t val, bool isTombstoneOrEmptyKey) |
| 12756 | : val(val), isTombstoneOrEmptyKey(isTombstoneOrEmptyKey) {} |
| 12757 | }; |
| 12758 | |
| 12759 | static DupKey GetDupKey(const llvm::APSInt& Val) { |
| 12760 | return DupKey(Val.isSigned() ? Val.getSExtValue() : Val.getZExtValue(), |
| 12761 | false); |
| 12762 | } |
| 12763 | |
| 12764 | struct DenseMapInfoDupKey { |
| 12765 | static DupKey getEmptyKey() { return DupKey(0, true); } |
| 12766 | static DupKey getTombstoneKey() { return DupKey(1, true); } |
| 12767 | static unsigned getHashValue(const DupKey Key) { |
| 12768 | return (unsigned)(Key.val * 37); |
| 12769 | } |
| 12770 | static bool isEqual(const DupKey& LHS, const DupKey& RHS) { |
| 12771 | return LHS.isTombstoneOrEmptyKey == RHS.isTombstoneOrEmptyKey && |
| 12772 | LHS.val == RHS.val; |
| 12773 | } |
| 12774 | }; |
| 12775 | |
| 12776 | // Emits a warning when an element is implicitly set a value that |
| 12777 | // a previous element has already been set to. |
Dmitri Gribenko | 9ff2b42 | 2013-04-27 20:23:52 +0000 | [diff] [blame] | 12778 | static void CheckForDuplicateEnumValues(Sema &S, ArrayRef<Decl *> Elements, |
| 12779 | EnumDecl *Enum, |
Ted Kremenek | a734a0e | 2012-12-22 01:34:09 +0000 | [diff] [blame] | 12780 | QualType EnumType) { |
| 12781 | if (S.Diags.getDiagnosticLevel(diag::warn_duplicate_enum_values, |
| 12782 | Enum->getLocation()) == |
| 12783 | DiagnosticsEngine::Ignored) |
| 12784 | return; |
| 12785 | // Avoid anonymous enums |
| 12786 | if (!Enum->getIdentifier()) |
| 12787 | return; |
| 12788 | |
| 12789 | // Only check for small enums. |
| 12790 | if (Enum->getNumPositiveBits() > 63 || Enum->getNumNegativeBits() > 64) |
| 12791 | return; |
| 12792 | |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 12793 | typedef SmallVector<EnumConstantDecl *, 3> ECDVector; |
| 12794 | typedef SmallVector<ECDVector *, 3> DuplicatesVector; |
Ted Kremenek | a734a0e | 2012-12-22 01:34:09 +0000 | [diff] [blame] | 12795 | |
| 12796 | typedef llvm::PointerUnion<EnumConstantDecl*, ECDVector*> DeclOrVector; |
| 12797 | typedef llvm::DenseMap<DupKey, DeclOrVector, DenseMapInfoDupKey> |
| 12798 | ValueToVectorMap; |
| 12799 | |
| 12800 | DuplicatesVector DupVector; |
| 12801 | ValueToVectorMap EnumMap; |
| 12802 | |
| 12803 | // Populate the EnumMap with all values represented by enum constants without |
| 12804 | // an initialier. |
Dmitri Gribenko | 9ff2b42 | 2013-04-27 20:23:52 +0000 | [diff] [blame] | 12805 | for (unsigned i = 0, e = Elements.size(); i != e; ++i) { |
Benjamin Kramer | efac8da | 2013-04-07 14:10:40 +0000 | [diff] [blame] | 12806 | EnumConstantDecl *ECD = cast_or_null<EnumConstantDecl>(Elements[i]); |
Ted Kremenek | a734a0e | 2012-12-22 01:34:09 +0000 | [diff] [blame] | 12807 | |
| 12808 | // Null EnumConstantDecl means a previous diagnostic has been emitted for |
| 12809 | // this constant. Skip this enum since it may be ill-formed. |
| 12810 | if (!ECD) { |
| 12811 | return; |
| 12812 | } |
| 12813 | |
| 12814 | if (ECD->getInitExpr()) |
| 12815 | continue; |
| 12816 | |
| 12817 | DupKey Key = GetDupKey(ECD->getInitVal()); |
| 12818 | DeclOrVector &Entry = EnumMap[Key]; |
| 12819 | |
| 12820 | // First time encountering this value. |
| 12821 | if (Entry.isNull()) |
| 12822 | Entry = ECD; |
| 12823 | } |
| 12824 | |
| 12825 | // Create vectors for any values that has duplicates. |
Dmitri Gribenko | 9ff2b42 | 2013-04-27 20:23:52 +0000 | [diff] [blame] | 12826 | for (unsigned i = 0, e = Elements.size(); i != e; ++i) { |
Ted Kremenek | a734a0e | 2012-12-22 01:34:09 +0000 | [diff] [blame] | 12827 | EnumConstantDecl *ECD = cast<EnumConstantDecl>(Elements[i]); |
| 12828 | if (!ValidDuplicateEnum(ECD, Enum)) |
| 12829 | continue; |
| 12830 | |
| 12831 | DupKey Key = GetDupKey(ECD->getInitVal()); |
| 12832 | |
| 12833 | DeclOrVector& Entry = EnumMap[Key]; |
| 12834 | if (Entry.isNull()) |
| 12835 | continue; |
| 12836 | |
| 12837 | if (EnumConstantDecl *D = Entry.dyn_cast<EnumConstantDecl*>()) { |
| 12838 | // Ensure constants are different. |
| 12839 | if (D == ECD) |
| 12840 | continue; |
| 12841 | |
| 12842 | // Create new vector and push values onto it. |
| 12843 | ECDVector *Vec = new ECDVector(); |
| 12844 | Vec->push_back(D); |
| 12845 | Vec->push_back(ECD); |
| 12846 | |
| 12847 | // Update entry to point to the duplicates vector. |
| 12848 | Entry = Vec; |
| 12849 | |
| 12850 | // Store the vector somewhere we can consult later for quick emission of |
| 12851 | // diagnostics. |
| 12852 | DupVector.push_back(Vec); |
| 12853 | continue; |
| 12854 | } |
| 12855 | |
| 12856 | ECDVector *Vec = Entry.get<ECDVector*>(); |
| 12857 | // Make sure constants are not added more than once. |
| 12858 | if (*Vec->begin() == ECD) |
| 12859 | continue; |
| 12860 | |
| 12861 | Vec->push_back(ECD); |
| 12862 | } |
| 12863 | |
| 12864 | // Emit diagnostics. |
| 12865 | for (DuplicatesVector::iterator DupVectorIter = DupVector.begin(), |
| 12866 | DupVectorEnd = DupVector.end(); |
| 12867 | DupVectorIter != DupVectorEnd; ++DupVectorIter) { |
| 12868 | ECDVector *Vec = *DupVectorIter; |
| 12869 | assert(Vec->size() > 1 && "ECDVector should have at least 2 elements."); |
| 12870 | |
| 12871 | // Emit warning for one enum constant. |
| 12872 | ECDVector::iterator I = Vec->begin(); |
| 12873 | S.Diag((*I)->getLocation(), diag::warn_duplicate_enum_values) |
| 12874 | << (*I)->getName() << (*I)->getInitVal().toString(10) |
| 12875 | << (*I)->getSourceRange(); |
| 12876 | ++I; |
| 12877 | |
| 12878 | // Emit one note for each of the remaining enum constants with |
| 12879 | // the same value. |
| 12880 | for (ECDVector::iterator E = Vec->end(); I != E; ++I) |
| 12881 | S.Diag((*I)->getLocation(), diag::note_duplicate_element) |
| 12882 | << (*I)->getName() << (*I)->getInitVal().toString(10) |
| 12883 | << (*I)->getSourceRange(); |
| 12884 | delete Vec; |
| 12885 | } |
| 12886 | } |
| 12887 | |
Mike Stump | c6e35aa | 2009-05-16 07:06:02 +0000 | [diff] [blame] | 12888 | void Sema::ActOnEnumBody(SourceLocation EnumLoc, SourceLocation LBraceLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 12889 | SourceLocation RBraceLoc, Decl *EnumDeclX, |
Dmitri Gribenko | 9ff2b42 | 2013-04-27 20:23:52 +0000 | [diff] [blame] | 12890 | ArrayRef<Decl *> Elements, |
Edward O'Callaghan | fee1381 | 2009-08-08 14:36:57 +0000 | [diff] [blame] | 12891 | Scope *S, AttributeList *Attr) { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 12892 | EnumDecl *Enum = cast<EnumDecl>(EnumDeclX); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 12893 | QualType EnumType = Context.getTypeDeclType(Enum); |
Edward O'Callaghan | fee1381 | 2009-08-08 14:36:57 +0000 | [diff] [blame] | 12894 | |
| 12895 | if (Attr) |
| 12896 | ProcessDeclAttributeList(S, Enum, Attr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12897 | |
Eli Friedman | ed0716b | 2009-12-11 01:34:50 +0000 | [diff] [blame] | 12898 | if (Enum->isDependentType()) { |
Dmitri Gribenko | 9ff2b42 | 2013-04-27 20:23:52 +0000 | [diff] [blame] | 12899 | for (unsigned i = 0, e = Elements.size(); i != e; ++i) { |
Eli Friedman | ed0716b | 2009-12-11 01:34:50 +0000 | [diff] [blame] | 12900 | EnumConstantDecl *ECD = |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 12901 | cast_or_null<EnumConstantDecl>(Elements[i]); |
Eli Friedman | ed0716b | 2009-12-11 01:34:50 +0000 | [diff] [blame] | 12902 | if (!ECD) continue; |
| 12903 | |
| 12904 | ECD->setType(EnumType); |
| 12905 | } |
| 12906 | |
John McCall | 1b5a618 | 2010-05-06 08:49:23 +0000 | [diff] [blame] | 12907 | Enum->completeDefinition(Context.DependentTy, Context.DependentTy, 0, 0); |
Eli Friedman | ed0716b | 2009-12-11 01:34:50 +0000 | [diff] [blame] | 12908 | return; |
| 12909 | } |
| 12910 | |
Chris Lattner | e37f0be | 2007-08-28 05:10:31 +0000 | [diff] [blame] | 12911 | // TODO: If the result value doesn't fit in an int, it must be a long or long |
| 12912 | // long value. ISO C does not support this, but GCC does as an extension, |
| 12913 | // emit a warning. |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 12914 | unsigned IntWidth = Context.getTargetInfo().getIntWidth(); |
| 12915 | unsigned CharWidth = Context.getTargetInfo().getCharWidth(); |
| 12916 | unsigned ShortWidth = Context.getTargetInfo().getShortWidth(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12917 | |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 12918 | // Verify that all the values are okay, compute the size of the values, and |
| 12919 | // reverse the list. |
| 12920 | unsigned NumNegativeBits = 0; |
| 12921 | unsigned NumPositiveBits = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12922 | |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 12923 | // Keep track of whether all elements have type int. |
| 12924 | bool AllElementsInt = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12925 | |
Dmitri Gribenko | 9ff2b42 | 2013-04-27 20:23:52 +0000 | [diff] [blame] | 12926 | for (unsigned i = 0, e = Elements.size(); i != e; ++i) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 12927 | EnumConstantDecl *ECD = |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 12928 | cast_or_null<EnumConstantDecl>(Elements[i]); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 12929 | if (!ECD) continue; // Already issued a diagnostic. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12930 | |
Chris Lattner | 211a30e | 2007-08-28 05:27:00 +0000 | [diff] [blame] | 12931 | const llvm::APSInt &InitVal = ECD->getInitVal(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12932 | |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 12933 | // Keep track of the size of positive and negative values. |
Chris Lattner | a73349d | 2008-02-26 00:33:57 +0000 | [diff] [blame] | 12934 | if (InitVal.isUnsigned() || InitVal.isNonNegative()) |
Chris Lattner | 21dd821 | 2008-01-14 21:47:29 +0000 | [diff] [blame] | 12935 | NumPositiveBits = std::max(NumPositiveBits, |
| 12936 | (unsigned)InitVal.getActiveBits()); |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 12937 | else |
Chris Lattner | 21dd821 | 2008-01-14 21:47:29 +0000 | [diff] [blame] | 12938 | NumNegativeBits = std::max(NumNegativeBits, |
| 12939 | (unsigned)InitVal.getMinSignedBits()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 12940 | |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 12941 | // Keep track of whether every enum element has type int (very commmon). |
| 12942 | if (AllElementsInt) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12943 | AllElementsInt = ECD->getType() == Context.IntTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 12944 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12945 | |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 12946 | // Figure out the type that should be used for this enum. |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 12947 | QualType BestType; |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 12948 | unsigned BestWidth; |
Edward O'Callaghan | fee1381 | 2009-08-08 14:36:57 +0000 | [diff] [blame] | 12949 | |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 12950 | // C++0x N3000 [conv.prom]p3: |
| 12951 | // An rvalue of an unscoped enumeration type whose underlying |
| 12952 | // type is not fixed can be converted to an rvalue of the first |
| 12953 | // of the following types that can represent all the values of |
| 12954 | // the enumeration: int, unsigned int, long int, unsigned long |
| 12955 | // int, long long int, or unsigned long long int. |
| 12956 | // C99 6.4.4.3p2: |
| 12957 | // An identifier declared as an enumeration constant has type int. |
| 12958 | // The C99 rule is modified by a gcc extension |
| 12959 | QualType BestPromotionType; |
| 12960 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 12961 | bool Packed = Enum->hasAttr<PackedAttr>(); |
Argyrios Kyrtzidis | 9a2b9d7 | 2010-10-08 00:25:19 +0000 | [diff] [blame] | 12962 | // -fshort-enums is the equivalent to specifying the packed attribute on all |
| 12963 | // enum definitions. |
| 12964 | if (LangOpts.ShortEnums) |
| 12965 | Packed = true; |
Edward O'Callaghan | fee1381 | 2009-08-08 14:36:57 +0000 | [diff] [blame] | 12966 | |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 12967 | if (Enum->isFixed()) { |
Eli Friedman | 3bfb571 | 2011-10-26 07:38:19 +0000 | [diff] [blame] | 12968 | BestType = Enum->getIntegerType(); |
| 12969 | if (BestType->isPromotableIntegerType()) |
| 12970 | BestPromotionType = Context.getPromotedIntegerType(BestType); |
| 12971 | else |
| 12972 | BestPromotionType = BestType; |
Duncan Sands | 240a020 | 2010-10-12 14:07:59 +0000 | [diff] [blame] | 12973 | // We don't need to set BestWidth, because BestType is going to be the type |
| 12974 | // of the enumerators, but we do anyway because otherwise some compilers |
| 12975 | // warn that it might be used uninitialized. |
| 12976 | BestWidth = CharWidth; |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 12977 | } |
| 12978 | else if (NumNegativeBits) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12979 | // If there is a negative value, figure out the smallest integer type (of |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 12980 | // int/long/longlong) that fits. |
Edward O'Callaghan | fee1381 | 2009-08-08 14:36:57 +0000 | [diff] [blame] | 12981 | // If it's packed, check also if it fits a char or a short. |
| 12982 | if (Packed && NumNegativeBits <= CharWidth && NumPositiveBits < CharWidth) { |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 12983 | BestType = Context.SignedCharTy; |
| 12984 | BestWidth = CharWidth; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12985 | } else if (Packed && NumNegativeBits <= ShortWidth && |
Edward O'Callaghan | fee1381 | 2009-08-08 14:36:57 +0000 | [diff] [blame] | 12986 | NumPositiveBits < ShortWidth) { |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 12987 | BestType = Context.ShortTy; |
| 12988 | BestWidth = ShortWidth; |
| 12989 | } else if (NumNegativeBits <= IntWidth && NumPositiveBits < IntWidth) { |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 12990 | BestType = Context.IntTy; |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 12991 | BestWidth = IntWidth; |
| 12992 | } else { |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 12993 | BestWidth = Context.getTargetInfo().getLongWidth(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12994 | |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 12995 | if (NumNegativeBits <= BestWidth && NumPositiveBits < BestWidth) { |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 12996 | BestType = Context.LongTy; |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 12997 | } else { |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 12998 | BestWidth = Context.getTargetInfo().getLongLongWidth(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12999 | |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 13000 | if (NumNegativeBits > BestWidth || NumPositiveBits >= BestWidth) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 13001 | Diag(Enum->getLocation(), diag::ext_enum_too_large); |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 13002 | BestType = Context.LongLongTy; |
| 13003 | } |
| 13004 | } |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 13005 | BestPromotionType = (BestWidth <= IntWidth ? Context.IntTy : BestType); |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 13006 | } else { |
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 13007 | // If there is no negative value, figure out the smallest type that fits |
| 13008 | // all of the enumerator values. |
Edward O'Callaghan | fee1381 | 2009-08-08 14:36:57 +0000 | [diff] [blame] | 13009 | // If it's packed, check also if it fits a char or a short. |
| 13010 | if (Packed && NumPositiveBits <= CharWidth) { |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 13011 | BestType = Context.UnsignedCharTy; |
| 13012 | BestPromotionType = Context.IntTy; |
| 13013 | BestWidth = CharWidth; |
Edward O'Callaghan | fee1381 | 2009-08-08 14:36:57 +0000 | [diff] [blame] | 13014 | } else if (Packed && NumPositiveBits <= ShortWidth) { |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 13015 | BestType = Context.UnsignedShortTy; |
| 13016 | BestPromotionType = Context.IntTy; |
| 13017 | BestWidth = ShortWidth; |
| 13018 | } else if (NumPositiveBits <= IntWidth) { |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 13019 | BestType = Context.UnsignedIntTy; |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 13020 | BestWidth = IntWidth; |
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 13021 | BestPromotionType |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 13022 | = (NumPositiveBits == BestWidth || !getLangOpts().CPlusPlus) |
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 13023 | ? Context.UnsignedIntTy : Context.IntTy; |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 13024 | } else if (NumPositiveBits <= |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 13025 | (BestWidth = Context.getTargetInfo().getLongWidth())) { |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 13026 | BestType = Context.UnsignedLongTy; |
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 13027 | BestPromotionType |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 13028 | = (NumPositiveBits == BestWidth || !getLangOpts().CPlusPlus) |
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 13029 | ? Context.UnsignedLongTy : Context.LongTy; |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 13030 | } else { |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 13031 | BestWidth = Context.getTargetInfo().getLongLongWidth(); |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 13032 | assert(NumPositiveBits <= BestWidth && |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 13033 | "How could an initializer get larger than ULL?"); |
| 13034 | BestType = Context.UnsignedLongLongTy; |
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 13035 | BestPromotionType |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 13036 | = (NumPositiveBits == BestWidth || !getLangOpts().CPlusPlus) |
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 13037 | ? Context.UnsignedLongLongTy : Context.LongLongTy; |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 13038 | } |
| 13039 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 13040 | |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 13041 | // Loop over all of the enumerator constants, changing their types to match |
| 13042 | // the type of the enum if needed. |
Dmitri Gribenko | 9ff2b42 | 2013-04-27 20:23:52 +0000 | [diff] [blame] | 13043 | for (unsigned i = 0, e = Elements.size(); i != e; ++i) { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 13044 | EnumConstantDecl *ECD = cast_or_null<EnumConstantDecl>(Elements[i]); |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 13045 | if (!ECD) continue; // Already issued a diagnostic. |
| 13046 | |
| 13047 | // Standard C says the enumerators have int type, but we allow, as an |
| 13048 | // extension, the enumerators to be larger than int size. If each |
| 13049 | // enumerator value fits in an int, type it as an int, otherwise type it the |
| 13050 | // same as the enumerator decl itself. This means that in "enum { X = 1U }" |
| 13051 | // that X has type 'int', not 'unsigned'. |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 13052 | |
| 13053 | // Determine whether the value fits into an int. |
| 13054 | llvm::APSInt InitVal = ECD->getInitVal(); |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 13055 | |
| 13056 | // If it fits into an integer type, force it. Otherwise force it to match |
| 13057 | // the enum decl type. |
| 13058 | QualType NewTy; |
| 13059 | unsigned NewWidth; |
| 13060 | bool NewSign; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 13061 | if (!getLangOpts().CPlusPlus && |
Fariborz Jahanian | 3b25216 | 2011-11-04 18:51:24 +0000 | [diff] [blame] | 13062 | !Enum->isFixed() && |
Douglas Gregor | 677e4fe | 2010-02-01 23:36:03 +0000 | [diff] [blame] | 13063 | isRepresentableIntegerValue(Context, InitVal, Context.IntTy)) { |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 13064 | NewTy = Context.IntTy; |
| 13065 | NewWidth = IntWidth; |
| 13066 | NewSign = true; |
| 13067 | } else if (ECD->getType() == BestType) { |
| 13068 | // Already the right type! |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 13069 | if (getLangOpts().CPlusPlus) |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 13070 | // C++ [dcl.enum]p4: Following the closing brace of an |
| 13071 | // enum-specifier, each enumerator has the type of its |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 13072 | // enumeration. |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 13073 | ECD->setType(EnumType); |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 13074 | continue; |
| 13075 | } else { |
| 13076 | NewTy = BestType; |
| 13077 | NewWidth = BestWidth; |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 13078 | NewSign = BestType->isSignedIntegerOrEnumerationType(); |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 13079 | } |
| 13080 | |
| 13081 | // Adjust the APSInt value. |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 13082 | InitVal = InitVal.extOrTrunc(NewWidth); |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 13083 | InitVal.setIsSigned(NewSign); |
| 13084 | ECD->setInitVal(InitVal); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 13085 | |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 13086 | // Adjust the Expr initializer and type. |
Abramo Bagnara | 320e153 | 2010-12-17 15:49:53 +0000 | [diff] [blame] | 13087 | if (ECD->getInitExpr() && |
Nick Lewycky | 25af091 | 2011-07-02 02:05:12 +0000 | [diff] [blame] | 13088 | !Context.hasSameType(NewTy, ECD->getInitExpr()->getType())) |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 13089 | ECD->setInitExpr(ImplicitCastExpr::Create(Context, NewTy, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 13090 | CK_IntegralCast, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 13091 | ECD->getInitExpr(), |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 13092 | /*base paths*/ nullptr, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 13093 | VK_RValue)); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 13094 | if (getLangOpts().CPlusPlus) |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 13095 | // C++ [dcl.enum]p4: Following the closing brace of an |
| 13096 | // enum-specifier, each enumerator has the type of its |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 13097 | // enumeration. |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 13098 | ECD->setType(EnumType); |
| 13099 | else |
| 13100 | ECD->setType(NewTy); |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 13101 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 13102 | |
John McCall | 1b5a618 | 2010-05-06 08:49:23 +0000 | [diff] [blame] | 13103 | Enum->completeDefinition(BestType, BestPromotionType, |
| 13104 | NumPositiveBits, NumNegativeBits); |
James Molloy | 16f1f71 | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 13105 | |
Dmitri Gribenko | 9ff2b42 | 2013-04-27 20:23:52 +0000 | [diff] [blame] | 13106 | CheckForDuplicateEnumValues(*this, Elements, Enum, EnumType); |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 13107 | |
| 13108 | // Now that the enum type is defined, ensure it's not been underaligned. |
| 13109 | if (Enum->hasAttrs()) |
| 13110 | CheckAlignasUnderalignment(Enum); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 13111 | } |
| 13112 | |
Abramo Bagnara | 21e006e | 2011-03-03 14:20:18 +0000 | [diff] [blame] | 13113 | Decl *Sema::ActOnFileScopeAsmDecl(Expr *expr, |
| 13114 | SourceLocation StartLoc, |
| 13115 | SourceLocation EndLoc) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 13116 | StringLiteral *AsmString = cast<StringLiteral>(expr); |
Sebastian Redl | 798d119 | 2008-12-13 16:23:55 +0000 | [diff] [blame] | 13117 | |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 13118 | FileScopeAsmDecl *New = FileScopeAsmDecl::Create(Context, CurContext, |
Abramo Bagnara | 21e006e | 2011-03-03 14:20:18 +0000 | [diff] [blame] | 13119 | AsmString, StartLoc, |
| 13120 | EndLoc); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 13121 | CurContext->addDecl(New); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 13122 | return New; |
Anders Carlsson | dfab6cb | 2008-02-08 00:33:21 +0000 | [diff] [blame] | 13123 | } |
Eli Friedman | c49f19b | 2009-06-05 02:44:36 +0000 | [diff] [blame] | 13124 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 13125 | static void checkModuleImportContext(Sema &S, Module *M, |
| 13126 | SourceLocation ImportLoc, |
| 13127 | DeclContext *DC) { |
| 13128 | if (auto *LSD = dyn_cast<LinkageSpecDecl>(DC)) { |
| 13129 | switch (LSD->getLanguage()) { |
| 13130 | case LinkageSpecDecl::lang_c: |
| 13131 | if (!M->IsExternC) { |
| 13132 | S.Diag(ImportLoc, diag::err_module_import_in_extern_c) |
| 13133 | << M->getFullModuleName(); |
| 13134 | S.Diag(LSD->getLocStart(), diag::note_module_import_in_extern_c); |
| 13135 | return; |
| 13136 | } |
| 13137 | break; |
| 13138 | case LinkageSpecDecl::lang_cxx: |
| 13139 | break; |
| 13140 | } |
| 13141 | DC = LSD->getParent(); |
| 13142 | } |
| 13143 | |
| 13144 | while (isa<LinkageSpecDecl>(DC)) |
| 13145 | DC = DC->getParent(); |
| 13146 | if (!isa<TranslationUnitDecl>(DC)) { |
| 13147 | S.Diag(ImportLoc, diag::err_module_import_not_at_top_level) |
| 13148 | << M->getFullModuleName() << DC; |
| 13149 | S.Diag(cast<Decl>(DC)->getLocStart(), |
| 13150 | diag::note_module_import_not_at_top_level) |
| 13151 | << DC; |
| 13152 | } |
| 13153 | } |
| 13154 | |
Douglas Gregor | 5948ae1 | 2012-01-03 18:04:46 +0000 | [diff] [blame] | 13155 | DeclResult Sema::ActOnModuleImport(SourceLocation AtLoc, |
| 13156 | SourceLocation ImportLoc, |
| 13157 | ModuleIdPath Path) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 13158 | Module *Mod = |
| 13159 | getModuleLoader().loadModule(ImportLoc, Path, Module::AllVisible, |
| 13160 | /*IsIncludeDirective=*/false); |
Douglas Gregor | 1a4761e | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 13161 | if (!Mod) |
Douglas Gregor | 6aa52ec | 2011-08-26 23:56:07 +0000 | [diff] [blame] | 13162 | return true; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 13163 | |
| 13164 | checkModuleImportContext(*this, Mod, ImportLoc, CurContext); |
| 13165 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 13166 | // FIXME: we should support importing a submodule within a different submodule |
| 13167 | // of the same top-level module. Until we do, make it an error rather than |
| 13168 | // silently ignoring the import. |
| 13169 | if (Mod->getTopLevelModuleName() == getLangOpts().CurrentModule) |
| 13170 | Diag(ImportLoc, diag::err_module_self_import) |
| 13171 | << Mod->getFullModuleName() << getLangOpts().CurrentModule; |
| 13172 | |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 13173 | SmallVector<SourceLocation, 2> IdentifierLocs; |
Douglas Gregor | 15de72c | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 13174 | Module *ModCheck = Mod; |
| 13175 | for (unsigned I = 0, N = Path.size(); I != N; ++I) { |
| 13176 | // If we've run out of module parents, just drop the remaining identifiers. |
| 13177 | // We need the length to be consistent. |
| 13178 | if (!ModCheck) |
| 13179 | break; |
| 13180 | ModCheck = ModCheck->Parent; |
| 13181 | |
| 13182 | IdentifierLocs.push_back(Path[I].second); |
| 13183 | } |
| 13184 | |
| 13185 | ImportDecl *Import = ImportDecl::Create(Context, |
| 13186 | Context.getTranslationUnitDecl(), |
Douglas Gregor | 5948ae1 | 2012-01-03 18:04:46 +0000 | [diff] [blame] | 13187 | AtLoc.isValid()? AtLoc : ImportLoc, |
| 13188 | Mod, IdentifierLocs); |
Douglas Gregor | 15de72c | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 13189 | Context.getTranslationUnitDecl()->addDecl(Import); |
| 13190 | return Import; |
Douglas Gregor | 6aa52ec | 2011-08-26 23:56:07 +0000 | [diff] [blame] | 13191 | } |
| 13192 | |
Richard Smith | 26297f5 | 2013-11-15 04:24:58 +0000 | [diff] [blame] | 13193 | void Sema::ActOnModuleInclude(SourceLocation DirectiveLoc, Module *Mod) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 13194 | checkModuleImportContext(*this, Mod, DirectiveLoc, CurContext); |
| 13195 | |
Richard Smith | 26297f5 | 2013-11-15 04:24:58 +0000 | [diff] [blame] | 13196 | // FIXME: Should we synthesize an ImportDecl here? |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 13197 | getModuleLoader().makeModuleVisible(Mod, Module::AllVisible, DirectiveLoc, |
| 13198 | /*Complain=*/true); |
Richard Smith | 26297f5 | 2013-11-15 04:24:58 +0000 | [diff] [blame] | 13199 | } |
| 13200 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 13201 | void Sema::createImplicitModuleImportForErrorRecovery(SourceLocation Loc, |
| 13202 | Module *Mod) { |
| 13203 | // Bail if we're not allowed to implicitly import a module here. |
| 13204 | if (isSFINAEContext() || !getLangOpts().ModulesErrorRecovery) |
| 13205 | return; |
| 13206 | |
Douglas Gregor | ca2ab45 | 2013-01-12 01:29:50 +0000 | [diff] [blame] | 13207 | // Create the implicit import declaration. |
| 13208 | TranslationUnitDecl *TU = getASTContext().getTranslationUnitDecl(); |
| 13209 | ImportDecl *ImportD = ImportDecl::CreateImplicit(getASTContext(), TU, |
| 13210 | Loc, Mod, Loc); |
| 13211 | TU->addDecl(ImportD); |
| 13212 | Consumer.HandleImplicitImportDecl(ImportD); |
| 13213 | |
| 13214 | // Make the module visible. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 13215 | getModuleLoader().makeModuleVisible(Mod, Module::AllVisible, Loc, |
| 13216 | /*Complain=*/false); |
Douglas Gregor | ca2ab45 | 2013-01-12 01:29:50 +0000 | [diff] [blame] | 13217 | } |
| 13218 | |
David Chisnall | 5f3c163 | 2012-02-18 16:12:34 +0000 | [diff] [blame] | 13219 | void Sema::ActOnPragmaRedefineExtname(IdentifierInfo* Name, |
| 13220 | IdentifierInfo* AliasName, |
| 13221 | SourceLocation PragmaLoc, |
| 13222 | SourceLocation NameLoc, |
| 13223 | SourceLocation AliasNameLoc) { |
| 13224 | Decl *PrevDecl = LookupSingleName(TUScope, Name, NameLoc, |
| 13225 | LookupOrdinaryName); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 13226 | AsmLabelAttr *Attr = ::new (Context) AsmLabelAttr(AliasNameLoc, Context, |
| 13227 | AliasName->getName(), 0); |
David Chisnall | 5f3c163 | 2012-02-18 16:12:34 +0000 | [diff] [blame] | 13228 | |
| 13229 | if (PrevDecl) |
| 13230 | PrevDecl->addAttr(Attr); |
| 13231 | else |
| 13232 | (void)ExtnameUndeclaredIdentifiers.insert( |
| 13233 | std::pair<IdentifierInfo*,AsmLabelAttr*>(Name, Attr)); |
| 13234 | } |
| 13235 | |
Eli Friedman | c49f19b | 2009-06-05 02:44:36 +0000 | [diff] [blame] | 13236 | void Sema::ActOnPragmaWeakID(IdentifierInfo* Name, |
| 13237 | SourceLocation PragmaLoc, |
| 13238 | SourceLocation NameLoc) { |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 13239 | Decl *PrevDecl = LookupSingleName(TUScope, Name, NameLoc, LookupOrdinaryName); |
Eli Friedman | c49f19b | 2009-06-05 02:44:36 +0000 | [diff] [blame] | 13240 | |
Eli Friedman | c49f19b | 2009-06-05 02:44:36 +0000 | [diff] [blame] | 13241 | if (PrevDecl) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 13242 | PrevDecl->addAttr(WeakAttr::CreateImplicit(Context, PragmaLoc)); |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 13243 | } else { |
| 13244 | (void)WeakUndeclaredIdentifiers.insert( |
| 13245 | std::pair<IdentifierInfo*,WeakInfo> |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 13246 | (Name, WeakInfo((IdentifierInfo*)nullptr, NameLoc))); |
Eli Friedman | c49f19b | 2009-06-05 02:44:36 +0000 | [diff] [blame] | 13247 | } |
Eli Friedman | c49f19b | 2009-06-05 02:44:36 +0000 | [diff] [blame] | 13248 | } |
| 13249 | |
| 13250 | void Sema::ActOnPragmaWeakAlias(IdentifierInfo* Name, |
| 13251 | IdentifierInfo* AliasName, |
| 13252 | SourceLocation PragmaLoc, |
| 13253 | SourceLocation NameLoc, |
| 13254 | SourceLocation AliasNameLoc) { |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 13255 | Decl *PrevDecl = LookupSingleName(TUScope, AliasName, AliasNameLoc, |
| 13256 | LookupOrdinaryName); |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 13257 | WeakInfo W = WeakInfo(Name, NameLoc); |
Eli Friedman | c49f19b | 2009-06-05 02:44:36 +0000 | [diff] [blame] | 13258 | |
Eli Friedman | c49f19b | 2009-06-05 02:44:36 +0000 | [diff] [blame] | 13259 | if (PrevDecl) { |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 13260 | if (!PrevDecl->hasAttr<AliasAttr>()) |
| 13261 | if (NamedDecl *ND = dyn_cast<NamedDecl>(PrevDecl)) |
Ryan Flynn | 7b1fdbd | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 13262 | DeclApplyPragmaWeak(TUScope, ND, W); |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 13263 | } else { |
| 13264 | (void)WeakUndeclaredIdentifiers.insert( |
| 13265 | std::pair<IdentifierInfo*,WeakInfo>(AliasName, W)); |
Eli Friedman | c49f19b | 2009-06-05 02:44:36 +0000 | [diff] [blame] | 13266 | } |
Eli Friedman | c49f19b | 2009-06-05 02:44:36 +0000 | [diff] [blame] | 13267 | } |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 13268 | |
| 13269 | Decl *Sema::getObjCDeclContext() const { |
| 13270 | return (dyn_cast_or_null<ObjCContainerDecl>(CurContext)); |
| 13271 | } |
Argyrios Kyrtzidis | c076e37 | 2011-10-06 23:23:27 +0000 | [diff] [blame] | 13272 | |
| 13273 | AvailabilityResult Sema::getCurContextAvailability() const { |
Fariborz Jahanian | 3359fa3 | 2012-09-06 18:38:58 +0000 | [diff] [blame] | 13274 | const Decl *D = cast<Decl>(getCurObjCLexicalContext()); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 13275 | // If we are within an Objective-C method, we should consult |
| 13276 | // both the availability of the method as well as the |
| 13277 | // enclosing class. If the class is (say) deprecated, |
| 13278 | // the entire method is considered deprecated from the |
| 13279 | // purpose of checking if the current context is deprecated. |
| 13280 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
| 13281 | AvailabilityResult R = MD->getAvailability(); |
| 13282 | if (R != AR_Available) |
| 13283 | return R; |
| 13284 | D = MD->getClassInterface(); |
| 13285 | } |
| 13286 | // If we are within an Objective-c @implementation, it |
| 13287 | // gets the same availability context as the @interface. |
| 13288 | else if (const ObjCImplementationDecl *ID = |
| 13289 | dyn_cast<ObjCImplementationDecl>(D)) { |
| 13290 | D = ID->getClassInterface(); |
| 13291 | } |
Argyrios Kyrtzidis | c076e37 | 2011-10-06 23:23:27 +0000 | [diff] [blame] | 13292 | return D->getAvailability(); |
| 13293 | } |