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" |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 28 | #include "clang/Basic/PartialDiagnostic.h" |
Steve Naroff | 4c49a6c | 2008-01-30 23:46:05 +0000 | [diff] [blame] | 29 | #include "clang/Basic/SourceManager.h" |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 30 | #include "clang/Basic/TargetInfo.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 31 | #include "clang/Lex/HeaderSearch.h" // FIXME: Sema shouldn't depend on Lex |
32 | #include "clang/Lex/ModuleLoader.h" // FIXME: Sema shouldn't depend on Lex | ||||
33 | #include "clang/Lex/Preprocessor.h" // FIXME: Sema shouldn't depend on Lex | ||||
34 | #include "clang/Parse/ParseDiagnostic.h" | ||||
35 | #include "clang/Sema/CXXFieldCollector.h" | ||||
36 | #include "clang/Sema/DeclSpec.h" | ||||
37 | #include "clang/Sema/DelayedDiagnostic.h" | ||||
38 | #include "clang/Sema/Initialization.h" | ||||
39 | #include "clang/Sema/Lookup.h" | ||||
40 | #include "clang/Sema/ParsedTemplate.h" | ||||
41 | #include "clang/Sema/Scope.h" | ||||
42 | #include "clang/Sema/ScopeInfo.h" | ||||
Benjamin Kramer | 8fe83e1 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 43 | #include "llvm/ADT/SmallString.h" |
John McCall | 6675586 | 2009-12-24 09:58:38 +0000 | [diff] [blame] | 44 | #include "llvm/ADT/Triple.h" |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 45 | #include <algorithm> |
Douglas Gregor | 9a8c9a2 | 2009-09-28 21:14:19 +0000 | [diff] [blame] | 46 | #include <cstring> |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 47 | #include <functional> |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 48 | using namespace clang; |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 49 | using namespace sema; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 50 | |
Richard Smith | c89edf5 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 51 | Sema::DeclGroupPtrTy Sema::ConvertDeclToDeclGroup(Decl *Ptr, Decl *OwnedType) { |
52 | if (OwnedType) { | ||||
53 | Decl *Group[2] = { OwnedType, Ptr }; | ||||
54 | return DeclGroupPtrTy::make(DeclGroupRef::Create(Context, Group, 2)); | ||||
55 | } | ||||
56 | |||||
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 57 | return DeclGroupPtrTy::make(DeclGroupRef(Ptr)); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 58 | } |
59 | |||||
Kaelyn Uhrain | 43e875d | 2012-01-18 21:41:41 +0000 | [diff] [blame] | 60 | namespace { |
61 | |||||
62 | class TypeNameValidatorCCC : public CorrectionCandidateCallback { | ||||
63 | public: | ||||
Kaelyn Uhrain | c1fb542 | 2012-06-22 23:37:05 +0000 | [diff] [blame] | 64 | TypeNameValidatorCCC(bool AllowInvalid, bool WantClass=false) |
65 | : AllowInvalidDecl(AllowInvalid), WantClassName(WantClass) { | ||||
Kaelyn Uhrain | 43e875d | 2012-01-18 21:41:41 +0000 | [diff] [blame] | 66 | WantExpressionKeywords = false; |
67 | WantCXXNamedCasts = false; | ||||
68 | WantRemainingKeywords = false; | ||||
69 | } | ||||
70 | |||||
71 | virtual bool ValidateCandidate(const TypoCorrection &candidate) { | ||||
72 | if (NamedDecl *ND = candidate.getCorrectionDecl()) | ||||
73 | return (isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND)) && | ||||
74 | (AllowInvalidDecl || !ND->isInvalidDecl()); | ||||
75 | else | ||||
Kaelyn Uhrain | c1fb542 | 2012-06-22 23:37:05 +0000 | [diff] [blame] | 76 | return !WantClassName && candidate.isKeyword(); |
Kaelyn Uhrain | 43e875d | 2012-01-18 21:41:41 +0000 | [diff] [blame] | 77 | } |
78 | |||||
79 | private: | ||||
80 | bool AllowInvalidDecl; | ||||
Kaelyn Uhrain | c1fb542 | 2012-06-22 23:37:05 +0000 | [diff] [blame] | 81 | bool WantClassName; |
Kaelyn Uhrain | 43e875d | 2012-01-18 21:41:41 +0000 | [diff] [blame] | 82 | }; |
83 | |||||
84 | } | ||||
85 | |||||
Kaelyn Uhrain | 7bf3340 | 2012-06-15 23:45:51 +0000 | [diff] [blame] | 86 | /// \brief Determine whether the token kind starts a simple-type-specifier. |
87 | bool Sema::isSimpleTypeSpecifier(tok::TokenKind Kind) const { | ||||
88 | switch (Kind) { | ||||
89 | // FIXME: Take into account the current language when deciding whether a | ||||
90 | // token kind is a valid type specifier | ||||
91 | case tok::kw_short: | ||||
92 | case tok::kw_long: | ||||
93 | case tok::kw___int64: | ||||
94 | case tok::kw___int128: | ||||
95 | case tok::kw_signed: | ||||
96 | case tok::kw_unsigned: | ||||
97 | case tok::kw_void: | ||||
98 | case tok::kw_char: | ||||
99 | case tok::kw_int: | ||||
100 | case tok::kw_half: | ||||
101 | case tok::kw_float: | ||||
102 | case tok::kw_double: | ||||
103 | case tok::kw_wchar_t: | ||||
104 | case tok::kw_bool: | ||||
105 | case tok::kw___underlying_type: | ||||
106 | return true; | ||||
107 | |||||
108 | case tok::annot_typename: | ||||
109 | case tok::kw_char16_t: | ||||
110 | case tok::kw_char32_t: | ||||
111 | case tok::kw_typeof: | ||||
David Majnemer | ff989a8 | 2013-09-22 01:24:26 +0000 | [diff] [blame] | 112 | case tok::annot_decltype: |
Kaelyn Uhrain | 7bf3340 | 2012-06-15 23:45:51 +0000 | [diff] [blame] | 113 | case tok::kw_decltype: |
114 | return getLangOpts().CPlusPlus; | ||||
115 | |||||
116 | default: | ||||
117 | break; | ||||
118 | } | ||||
119 | |||||
120 | return false; | ||||
121 | } | ||||
122 | |||||
Douglas Gregor | d6efafa | 2009-02-04 19:16:12 +0000 | [diff] [blame] | 123 | /// \brief If the identifier refers to a type name within this scope, |
124 | /// return the declaration of that type. | ||||
125 | /// | ||||
126 | /// This routine performs ordinary name lookup of the identifier II | ||||
127 | /// within the given scope, with optional C++ scope specifier SS, to | ||||
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 128 | /// determine whether the name refers to a type. If so, returns an |
129 | /// opaque pointer (actually a QualType) corresponding to that | ||||
130 | /// type. Otherwise, returns NULL. | ||||
Douglas Gregor | d6efafa | 2009-02-04 19:16:12 +0000 | [diff] [blame] | 131 | /// |
132 | /// If name lookup results in an ambiguity, this routine will complain | ||||
133 | /// and then return NULL. | ||||
Dmitri Gribenko | 8eead16 | 2013-05-03 13:12:11 +0000 | [diff] [blame] | 134 | ParsedType Sema::getTypeName(const IdentifierInfo &II, SourceLocation NameLoc, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 135 | Scope *S, CXXScopeSpec *SS, |
Fariborz Jahanian | 1e52dfc | 2011-02-08 18:05:59 +0000 | [diff] [blame] | 136 | bool isClassName, bool HasTrailingDot, |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 137 | ParsedType ObjectTypePtr, |
Abramo Bagnara | fad03b7 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 138 | bool IsCtorOrDtorName, |
Kaelyn Uhrain | fac9467 | 2011-10-11 01:02:41 +0000 | [diff] [blame] | 139 | bool WantNontrivialTypeSourceInfo, |
140 | IdentifierInfo **CorrectedII) { | ||||
Douglas Gregor | f6e6fc8 | 2009-11-20 22:03:38 +0000 | [diff] [blame] | 141 | // Determine where we will perform name lookup. |
142 | DeclContext *LookupCtx = 0; | ||||
143 | if (ObjectTypePtr) { | ||||
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 144 | QualType ObjectType = ObjectTypePtr.get(); |
Douglas Gregor | f6e6fc8 | 2009-11-20 22:03:38 +0000 | [diff] [blame] | 145 | if (ObjectType->isRecordType()) |
146 | LookupCtx = computeDeclContext(ObjectType); | ||||
Jeffrey Yasskin | edc2877 | 2010-04-07 23:29:58 +0000 | [diff] [blame] | 147 | } else if (SS && SS->isNotEmpty()) { |
Douglas Gregor | f6e6fc8 | 2009-11-20 22:03:38 +0000 | [diff] [blame] | 148 | LookupCtx = computeDeclContext(*SS, false); |
149 | |||||
150 | if (!LookupCtx) { | ||||
151 | if (isDependentScopeSpecifier(*SS)) { | ||||
152 | // C++ [temp.res]p3: | ||||
153 | // A qualified-id that refers to a type and in which the | ||||
154 | // nested-name-specifier depends on a template-parameter (14.6.2) | ||||
155 | // shall be prefixed by the keyword typename to indicate that the | ||||
156 | // qualified-id denotes a type, forming an | ||||
157 | // elaborated-type-specifier (7.1.5.3). | ||||
158 | // | ||||
159 | // We therefore do not perform any name lookup if the result would | ||||
160 | // refer to a member of an unknown specialization. | ||||
Richard Smith | c5a89a1 | 2012-04-02 01:30:27 +0000 | [diff] [blame] | 161 | if (!isClassName && !IsCtorOrDtorName) |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 162 | return ParsedType(); |
Douglas Gregor | f6e6fc8 | 2009-11-20 22:03:38 +0000 | [diff] [blame] | 163 | |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 164 | // We know from the grammar that this name refers to a type, |
165 | // so build a dependent node to describe the type. | ||||
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 166 | if (WantNontrivialTypeSourceInfo) |
167 | return ActOnTypenameType(S, SourceLocation(), *SS, II, NameLoc).get(); | ||||
168 | |||||
169 | NestedNameSpecifierLoc QualifierLoc = SS->getWithLocInContext(Context); | ||||
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 170 | QualType T = |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 171 | CheckTypenameType(ETK_None, SourceLocation(), QualifierLoc, |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 172 | II, NameLoc); |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 173 | |
174 | return ParsedType::make(T); | ||||
Douglas Gregor | f6e6fc8 | 2009-11-20 22:03:38 +0000 | [diff] [blame] | 175 | } |
176 | |||||
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 177 | return ParsedType(); |
Douglas Gregor | f6e6fc8 | 2009-11-20 22:03:38 +0000 | [diff] [blame] | 178 | } |
179 | |||||
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 180 | if (!LookupCtx->isDependentContext() && |
181 | RequireCompleteDeclContext(*SS, LookupCtx)) | ||||
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 182 | return ParsedType(); |
Douglas Gregor | 42c39f3 | 2009-08-26 18:27:52 +0000 | [diff] [blame] | 183 | } |
Eli Friedman | 0f0615b | 2009-12-21 01:42:38 +0000 | [diff] [blame] | 184 | |
185 | // FIXME: LookupNestedNameSpecifierName isn't the right kind of | ||||
186 | // lookup for class-names. | ||||
187 | LookupNameKind Kind = isClassName ? LookupNestedNameSpecifierName : | ||||
188 | LookupOrdinaryName; | ||||
189 | LookupResult Result(*this, &II, NameLoc, Kind); | ||||
Douglas Gregor | f6e6fc8 | 2009-11-20 22:03:38 +0000 | [diff] [blame] | 190 | if (LookupCtx) { |
191 | // Perform "qualified" name lookup into the declaration context we | ||||
192 | // computed, which is either the type of the base of a member access | ||||
193 | // expression or the declaration context associated with a prior | ||||
194 | // nested-name-specifier. | ||||
195 | LookupQualifiedName(Result, LookupCtx); | ||||
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 196 | |
Douglas Gregor | f6e6fc8 | 2009-11-20 22:03:38 +0000 | [diff] [blame] | 197 | if (ObjectTypePtr && Result.empty()) { |
198 | // C++ [basic.lookup.classref]p3: | ||||
199 | // If the unqualified-id is ~type-name, the type-name is looked up | ||||
200 | // in the context of the entire postfix-expression. If the type T of | ||||
201 | // the object expression is of a class type C, the type-name is also | ||||
202 | // looked up in the scope of class C. At least one of the lookups shall | ||||
203 | // find a name that refers to (possibly cv-qualified) T. | ||||
204 | LookupName(Result, S); | ||||
205 | } | ||||
206 | } else { | ||||
207 | // Perform unqualified name lookup. | ||||
208 | LookupName(Result, S); | ||||
209 | } | ||||
210 | |||||
Chris Lattner | 22bd905 | 2009-02-16 22:07:16 +0000 | [diff] [blame] | 211 | NamedDecl *IIDecl = 0; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 212 | switch (Result.getResultKind()) { |
Chris Lattner | 22bd905 | 2009-02-16 22:07:16 +0000 | [diff] [blame] | 213 | case LookupResult::NotFound: |
Douglas Gregor | 7d3f576 | 2010-01-15 01:44:47 +0000 | [diff] [blame] | 214 | case LookupResult::NotFoundInCurrentInstantiation: |
Kaelyn Uhrain | fac9467 | 2011-10-11 01:02:41 +0000 | [diff] [blame] | 215 | if (CorrectedII) { |
Kaelyn Uhrain | c1fb542 | 2012-06-22 23:37:05 +0000 | [diff] [blame] | 216 | TypeNameValidatorCCC Validator(true, isClassName); |
Kaelyn Uhrain | fac9467 | 2011-10-11 01:02:41 +0000 | [diff] [blame] | 217 | TypoCorrection Correction = CorrectTypo(Result.getLookupNameInfo(), |
Kaelyn Uhrain | 16e46dd | 2012-01-31 23:49:25 +0000 | [diff] [blame] | 218 | Kind, S, SS, Validator); |
Kaelyn Uhrain | fac9467 | 2011-10-11 01:02:41 +0000 | [diff] [blame] | 219 | IdentifierInfo *NewII = Correction.getCorrectionAsIdentifierInfo(); |
220 | TemplateTy Template; | ||||
221 | bool MemberOfUnknownSpecialization; | ||||
222 | UnqualifiedId TemplateName; | ||||
223 | TemplateName.setIdentifier(NewII, NameLoc); | ||||
224 | NestedNameSpecifier *NNS = Correction.getCorrectionSpecifier(); | ||||
225 | CXXScopeSpec NewSS, *NewSSPtr = SS; | ||||
226 | if (SS && NNS) { | ||||
227 | NewSS.MakeTrivial(Context, NNS, SourceRange(NameLoc)); | ||||
228 | NewSSPtr = &NewSS; | ||||
229 | } | ||||
230 | if (Correction && (NNS || NewII != &II) && | ||||
231 | // Ignore a correction to a template type as the to-be-corrected | ||||
232 | // identifier is not a template (typo correction for template names | ||||
233 | // is handled elsewhere). | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 234 | !(getLangOpts().CPlusPlus && NewSSPtr && |
Kaelyn Uhrain | fac9467 | 2011-10-11 01:02:41 +0000 | [diff] [blame] | 235 | isTemplateName(S, *NewSSPtr, false, TemplateName, ParsedType(), |
236 | false, Template, MemberOfUnknownSpecialization))) { | ||||
237 | ParsedType Ty = getTypeName(*NewII, NameLoc, S, NewSSPtr, | ||||
238 | isClassName, HasTrailingDot, ObjectTypePtr, | ||||
Abramo Bagnara | fad03b7 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 239 | IsCtorOrDtorName, |
Kaelyn Uhrain | fac9467 | 2011-10-11 01:02:41 +0000 | [diff] [blame] | 240 | WantNontrivialTypeSourceInfo); |
241 | if (Ty) { | ||||
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 242 | diagnoseTypo(Correction, |
243 | PDiag(diag::err_unknown_type_or_class_name_suggest) | ||||
244 | << Result.getLookupName() << isClassName); | ||||
Kaelyn Uhrain | fac9467 | 2011-10-11 01:02:41 +0000 | [diff] [blame] | 245 | if (SS && NNS) |
246 | SS->MakeTrivial(Context, NNS, SourceRange(NameLoc)); | ||||
247 | *CorrectedII = NewII; | ||||
248 | return Ty; | ||||
249 | } | ||||
250 | } | ||||
251 | } | ||||
252 | // If typo correction failed or was not performed, fall through | ||||
Chris Lattner | 22bd905 | 2009-02-16 22:07:16 +0000 | [diff] [blame] | 253 | case LookupResult::FoundOverloaded: |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 254 | case LookupResult::FoundUnresolvedValue: |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 255 | Result.suppressDiagnostics(); |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 256 | return ParsedType(); |
Douglas Gregor | b696ea3 | 2009-02-04 17:00:24 +0000 | [diff] [blame] | 257 | |
Chris Lattner | a64ef0a | 2009-10-25 22:09:09 +0000 | [diff] [blame] | 258 | case LookupResult::Ambiguous: |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 259 | // Recover from type-hiding ambiguities by hiding the type. We'll |
260 | // do the lookup again when looking for an object, and we can | ||||
261 | // diagnose the error then. If we don't do this, then the error | ||||
262 | // about hiding the type will be immediately followed by an error | ||||
263 | // that only makes sense if the identifier was treated like a type. | ||||
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 264 | if (Result.getAmbiguityKind() == LookupResult::AmbiguousTagHiding) { |
265 | Result.suppressDiagnostics(); | ||||
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 266 | return ParsedType(); |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 267 | } |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 268 | |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 269 | // Look to see if we have a type anywhere in the list of results. |
270 | for (LookupResult::iterator Res = Result.begin(), ResEnd = Result.end(); | ||||
271 | Res != ResEnd; ++Res) { | ||||
272 | if (isa<TypeDecl>(*Res) || isa<ObjCInterfaceDecl>(*Res)) { | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 273 | if (!IIDecl || |
274 | (*Res)->getLocation().getRawEncoding() < | ||||
Douglas Gregor | 841b53c | 2009-04-13 15:14:38 +0000 | [diff] [blame] | 275 | IIDecl->getLocation().getRawEncoding()) |
276 | IIDecl = *Res; | ||||
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 277 | } |
278 | } | ||||
279 | |||||
280 | if (!IIDecl) { | ||||
281 | // None of the entities we found is a type, so there is no way | ||||
282 | // to even assume that the result is a type. In this case, don't | ||||
283 | // complain about the ambiguity. The parser will either try to | ||||
284 | // perform this lookup again (e.g., as an object name), which | ||||
285 | // will produce the ambiguity, or will complain that it expected | ||||
286 | // a type name. | ||||
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 287 | Result.suppressDiagnostics(); |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 288 | return ParsedType(); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 289 | } |
290 | |||||
291 | // We found a type within the ambiguous lookup; diagnose the | ||||
292 | // ambiguity and then return that type. This might be the right | ||||
293 | // answer, or it might not be, but it suppresses any attempt to | ||||
294 | // perform the name lookup again. | ||||
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 295 | break; |
Douglas Gregor | b696ea3 | 2009-02-04 17:00:24 +0000 | [diff] [blame] | 296 | |
Chris Lattner | 22bd905 | 2009-02-16 22:07:16 +0000 | [diff] [blame] | 297 | case LookupResult::Found: |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 298 | IIDecl = Result.getFoundDecl(); |
Chris Lattner | 22bd905 | 2009-02-16 22:07:16 +0000 | [diff] [blame] | 299 | break; |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 300 | } |
301 | |||||
Chris Lattner | 10ca337 | 2009-10-25 17:16:46 +0000 | [diff] [blame] | 302 | assert(IIDecl && "Didn't find decl"); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 303 | |
Chris Lattner | 10ca337 | 2009-10-25 17:16:46 +0000 | [diff] [blame] | 304 | QualType T; |
305 | if (TypeDecl *TD = dyn_cast<TypeDecl>(IIDecl)) { | ||||
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 306 | DiagnoseUseOfDecl(IIDecl, NameLoc); |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 307 | |
Chris Lattner | 10ca337 | 2009-10-25 17:16:46 +0000 | [diff] [blame] | 308 | if (T.isNull()) |
309 | T = Context.getTypeDeclType(TD); | ||||
Abramo Bagnara | fad03b7 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 310 | |
311 | // NOTE: avoid constructing an ElaboratedType(Loc) if this is a | ||||
312 | // constructor or destructor name (in such a case, the scope specifier | ||||
313 | // will be attached to the enclosing Expr or Decl node). | ||||
314 | if (SS && SS->isNotEmpty() && !IsCtorOrDtorName) { | ||||
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 315 | if (WantNontrivialTypeSourceInfo) { |
316 | // Construct a type with type-source information. | ||||
317 | TypeLocBuilder Builder; | ||||
318 | Builder.pushTypeSpec(T).setNameLoc(NameLoc); | ||||
319 | |||||
320 | T = getElaboratedType(ETK_None, *SS, T); | ||||
321 | ElaboratedTypeLoc ElabTL = Builder.push<ElaboratedTypeLoc>(T); | ||||
Abramo Bagnara | 38a4291 | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 322 | ElabTL.setElaboratedKeywordLoc(SourceLocation()); |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 323 | ElabTL.setQualifierLoc(SS->getWithLocInContext(Context)); |
324 | return CreateParsedType(T, Builder.getTypeSourceInfo(Context, T)); | ||||
325 | } else { | ||||
326 | T = getElaboratedType(ETK_None, *SS, T); | ||||
327 | } | ||||
328 | } | ||||
Chris Lattner | 10ca337 | 2009-10-25 17:16:46 +0000 | [diff] [blame] | 329 | } else if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(IIDecl)) { |
Fariborz Jahanian | 02b0d65 | 2011-03-08 19:12:46 +0000 | [diff] [blame] | 330 | (void)DiagnoseUseOfDecl(IDecl, NameLoc); |
Fariborz Jahanian | 1e52dfc | 2011-02-08 18:05:59 +0000 | [diff] [blame] | 331 | if (!HasTrailingDot) |
332 | T = Context.getObjCInterfaceType(IDecl); | ||||
333 | } | ||||
334 | |||||
335 | if (T.isNull()) { | ||||
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 336 | // If it's not plausibly a type, suppress diagnostics. |
337 | Result.suppressDiagnostics(); | ||||
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 338 | return ParsedType(); |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 339 | } |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 340 | return ParsedType::make(T); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 341 | } |
342 | |||||
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 343 | /// isTagName() - This method is called *for error recovery purposes only* |
344 | /// to determine if the specified name is a valid tag name ("struct foo"). If | ||||
345 | /// 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] | 346 | /// TST_union, TST_struct, TST_interface, TST_class). This is used to diagnose |
347 | /// cases in C where the user forgot to specify the tag. | ||||
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 348 | DeclSpec::TST Sema::isTagName(IdentifierInfo &II, Scope *S) { |
349 | // Do a tag name lookup in this scope. | ||||
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 350 | LookupResult R(*this, &II, SourceLocation(), LookupTagName); |
351 | LookupName(R, S, false); | ||||
352 | R.suppressDiagnostics(); | ||||
353 | if (R.getResultKind() == LookupResult::Found) | ||||
John McCall | 1bcee0a | 2009-12-02 08:25:40 +0000 | [diff] [blame] | 354 | if (const TagDecl *TD = R.getAsSingle<TagDecl>()) { |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 355 | switch (TD->getTagKind()) { |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 356 | case TTK_Struct: return DeclSpec::TST_struct; |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 357 | case TTK_Interface: return DeclSpec::TST_interface; |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 358 | case TTK_Union: return DeclSpec::TST_union; |
359 | case TTK_Class: return DeclSpec::TST_class; | ||||
360 | case TTK_Enum: return DeclSpec::TST_enum; | ||||
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 361 | } |
362 | } | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 363 | |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 364 | return DeclSpec::TST_unspecified; |
365 | } | ||||
366 | |||||
Francois Pichet | 6943e9b | 2011-04-13 02:38:49 +0000 | [diff] [blame] | 367 | /// isMicrosoftMissingTypename - In Microsoft mode, within class scope, |
368 | /// if a CXXScopeSpec's type is equal to the type of one of the base classes | ||||
369 | /// then downgrade the missing typename error to a warning. | ||||
370 | /// This is needed for MSVC compatibility; Example: | ||||
371 | /// @code | ||||
372 | /// template<class T> class A { | ||||
373 | /// public: | ||||
374 | /// typedef int TYPE; | ||||
375 | /// }; | ||||
376 | /// template<class T> class B : public A<T> { | ||||
377 | /// public: | ||||
378 | /// A<T>::TYPE a; // no typename required because A<T> is a base class. | ||||
379 | /// }; | ||||
380 | /// @endcode | ||||
Francois Pichet | f11dbe9 | 2011-10-11 01:50:09 +0000 | [diff] [blame] | 381 | bool Sema::isMicrosoftMissingTypename(const CXXScopeSpec *SS, Scope *S) { |
Francois Pichet | 6943e9b | 2011-04-13 02:38:49 +0000 | [diff] [blame] | 382 | if (CurContext->isRecord()) { |
Francois Pichet | 3441a52 | 2011-04-13 02:44:57 +0000 | [diff] [blame] | 383 | const Type *Ty = SS->getScopeRep()->getAsType(); |
Francois Pichet | 6943e9b | 2011-04-13 02:38:49 +0000 | [diff] [blame] | 384 | |
385 | CXXRecordDecl *RD = cast<CXXRecordDecl>(CurContext); | ||||
386 | for (CXXRecordDecl::base_class_const_iterator Base = RD->bases_begin(), | ||||
387 | BaseEnd = RD->bases_end(); Base != BaseEnd; ++Base) | ||||
388 | if (Context.hasSameUnqualifiedType(QualType(Ty, 1), Base->getType())) | ||||
389 | return true; | ||||
Francois Pichet | f11dbe9 | 2011-10-11 01:50:09 +0000 | [diff] [blame] | 390 | return S->isFunctionPrototypeScope(); |
Francois Pichet | 6943e9b | 2011-04-13 02:38:49 +0000 | [diff] [blame] | 391 | } |
Francois Pichet | f11dbe9 | 2011-10-11 01:50:09 +0000 | [diff] [blame] | 392 | return CurContext->isFunctionOrMethod() || S->isFunctionPrototypeScope(); |
Francois Pichet | 6943e9b | 2011-04-13 02:38:49 +0000 | [diff] [blame] | 393 | } |
394 | |||||
Kaelyn Uhrain | 50dc12a | 2012-06-15 23:45:58 +0000 | [diff] [blame] | 395 | bool Sema::DiagnoseUnknownTypeName(IdentifierInfo *&II, |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 396 | SourceLocation IILoc, |
397 | Scope *S, | ||||
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 398 | CXXScopeSpec *SS, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 399 | ParsedType &SuggestedType) { |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 400 | // We don't have anything to suggest (yet). |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 401 | SuggestedType = ParsedType(); |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 402 | |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 403 | // There may have been a typo in the name of the type. Look up typo |
404 | // results, in case we have something that we can suggest. | ||||
Kaelyn Uhrain | 43e875d | 2012-01-18 21:41:41 +0000 | [diff] [blame] | 405 | TypeNameValidatorCCC Validator(false); |
Kaelyn Uhrain | 50dc12a | 2012-06-15 23:45:58 +0000 | [diff] [blame] | 406 | if (TypoCorrection Corrected = CorrectTypo(DeclarationNameInfo(II, IILoc), |
Kaelyn Uhrain | 43e875d | 2012-01-18 21:41:41 +0000 | [diff] [blame] | 407 | LookupOrdinaryName, S, SS, |
Kaelyn Uhrain | 16e46dd | 2012-01-31 23:49:25 +0000 | [diff] [blame] | 408 | Validator)) { |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 409 | if (Corrected.isKeyword()) { |
410 | // We corrected to a keyword. | ||||
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 411 | diagnoseTypo(Corrected, PDiag(diag::err_unknown_typename_suggest) << II); |
412 | II = Corrected.getCorrectionAsIdentifierInfo(); | ||||
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 413 | } else { |
Kaelyn Uhrain | 43e875d | 2012-01-18 21:41:41 +0000 | [diff] [blame] | 414 | // We found a similarly-named type or interface; suggest that. |
Kaelyn Uhrain | b2567dd | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 415 | if (!SS || !SS->isSet()) { |
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 416 | diagnoseTypo(Corrected, |
417 | PDiag(diag::err_unknown_typename_suggest) << II); | ||||
Kaelyn Uhrain | b2567dd | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 418 | } else if (DeclContext *DC = computeDeclContext(*SS, false)) { |
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 419 | std::string CorrectedStr(Corrected.getAsString(getLangOpts())); |
420 | bool DroppedSpecifier = Corrected.WillReplaceSpecifier() && | ||||
Kaelyn Uhrain | b2567dd | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 421 | II->getName().equals(CorrectedStr); |
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 422 | diagnoseTypo(Corrected, |
423 | PDiag(diag::err_unknown_nested_typename_suggest) | ||||
424 | << II << DC << DroppedSpecifier << SS->getRange()); | ||||
425 | } else { | ||||
Kaelyn Uhrain | 43e875d | 2012-01-18 21:41:41 +0000 | [diff] [blame] | 426 | llvm_unreachable("could not have corrected a typo here"); |
Kaelyn Uhrain | b2567dd | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 427 | } |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 428 | |
Kaelyn Uhrain | a934c31 | 2013-09-26 21:13:05 +0000 | [diff] [blame] | 429 | CXXScopeSpec tmpSS; |
430 | if (Corrected.getCorrectionSpecifier()) | ||||
431 | tmpSS.MakeTrivial(Context, Corrected.getCorrectionSpecifier(), | ||||
432 | SourceRange(IILoc)); | ||||
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 433 | SuggestedType = getTypeName(*Corrected.getCorrectionAsIdentifierInfo(), |
Kaelyn Uhrain | a934c31 | 2013-09-26 21:13:05 +0000 | [diff] [blame] | 434 | IILoc, S, tmpSS.isSet() ? &tmpSS : SS, false, |
435 | false, ParsedType(), | ||||
Abramo Bagnara | fad03b7 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 436 | /*IsCtorOrDtorName=*/false, |
Kaelyn Uhrain | 43e875d | 2012-01-18 21:41:41 +0000 | [diff] [blame] | 437 | /*NonTrivialTypeSourceInfo=*/true); |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 438 | } |
Kaelyn Uhrain | 43e875d | 2012-01-18 21:41:41 +0000 | [diff] [blame] | 439 | return true; |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 440 | } |
441 | |||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 442 | if (getLangOpts().CPlusPlus) { |
Jeffrey Yasskin | c173be2 | 2010-04-08 21:04:54 +0000 | [diff] [blame] | 443 | // See if II is a class template that the user forgot to pass arguments to. |
444 | UnqualifiedId Name; | ||||
Kaelyn Uhrain | 50dc12a | 2012-06-15 23:45:58 +0000 | [diff] [blame] | 445 | Name.setIdentifier(II, IILoc); |
Jeffrey Yasskin | c173be2 | 2010-04-08 21:04:54 +0000 | [diff] [blame] | 446 | CXXScopeSpec EmptySS; |
447 | TemplateTy TemplateResult; | ||||
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 448 | bool MemberOfUnknownSpecialization; |
Abramo Bagnara | 7c15353 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 449 | if (isTemplateName(S, SS ? *SS : EmptySS, /*hasTemplateKeyword=*/false, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 450 | Name, ParsedType(), true, TemplateResult, |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 451 | MemberOfUnknownSpecialization) == TNK_Type_template) { |
Serge Pavlov | 1806239 | 2013-08-27 13:15:56 +0000 | [diff] [blame] | 452 | TemplateName TplName = TemplateResult.get(); |
Jeffrey Yasskin | c173be2 | 2010-04-08 21:04:54 +0000 | [diff] [blame] | 453 | Diag(IILoc, diag::err_template_missing_args) << TplName; |
454 | if (TemplateDecl *TplDecl = TplName.getAsTemplateDecl()) { | ||||
455 | Diag(TplDecl->getLocation(), diag::note_template_decl_here) | ||||
456 | << TplDecl->getTemplateParameters()->getSourceRange(); | ||||
457 | } | ||||
458 | return true; | ||||
459 | } | ||||
460 | } | ||||
461 | |||||
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 462 | // FIXME: Should we move the logic that tries to recover from a missing tag |
463 | // (struct, union, enum) from Parser::ParseImplicitInt here, instead? | ||||
464 | |||||
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 465 | if (!SS || (!SS->isSet() && !SS->isInvalid())) |
Kaelyn Uhrain | 50dc12a | 2012-06-15 23:45:58 +0000 | [diff] [blame] | 466 | Diag(IILoc, diag::err_unknown_typename) << II; |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 467 | else if (DeclContext *DC = computeDeclContext(*SS, false)) |
468 | Diag(IILoc, diag::err_typename_nested_not_found) | ||||
Kaelyn Uhrain | 50dc12a | 2012-06-15 23:45:58 +0000 | [diff] [blame] | 469 | << II << DC << SS->getRange(); |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 470 | else if (isDependentScopeSpecifier(*SS)) { |
Francois Pichet | 6943e9b | 2011-04-13 02:38:49 +0000 | [diff] [blame] | 471 | unsigned DiagID = diag::err_typename_missing; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 472 | if (getLangOpts().MicrosoftMode && isMicrosoftMissingTypename(SS, S)) |
Francois Pichet | cf320c6 | 2011-04-22 08:25:24 +0000 | [diff] [blame] | 473 | DiagID = diag::warn_typename_missing; |
Francois Pichet | 6943e9b | 2011-04-13 02:38:49 +0000 | [diff] [blame] | 474 | |
475 | Diag(SS->getRange().getBegin(), DiagID) | ||||
Kaelyn Uhrain | 50dc12a | 2012-06-15 23:45:58 +0000 | [diff] [blame] | 476 | << (NestedNameSpecifier *)SS->getScopeRep() << II->getName() |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 477 | << SourceRange(SS->getRange().getBegin(), IILoc) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 478 | << FixItHint::CreateInsertion(SS->getRange().getBegin(), "typename "); |
Kaelyn Uhrain | 50dc12a | 2012-06-15 23:45:58 +0000 | [diff] [blame] | 479 | SuggestedType = ActOnTypenameType(S, SourceLocation(), |
480 | *SS, *II, IILoc).get(); | ||||
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 481 | } else { |
482 | assert(SS && SS->isInvalid() && | ||||
483 | "Invalid scope specifier has already been diagnosed"); | ||||
484 | } | ||||
485 | |||||
486 | return true; | ||||
487 | } | ||||
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 488 | |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 489 | /// \brief Determine whether the given result set contains either a type name |
490 | /// or | ||||
491 | static bool isResultTypeOrTemplate(LookupResult &R, const Token &NextToken) { | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 492 | bool CheckTemplate = R.getSema().getLangOpts().CPlusPlus && |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 493 | NextToken.is(tok::less); |
494 | |||||
495 | for (LookupResult::iterator I = R.begin(), IEnd = R.end(); I != IEnd; ++I) { | ||||
496 | if (isa<TypeDecl>(*I) || isa<ObjCInterfaceDecl>(*I)) | ||||
497 | return true; | ||||
498 | |||||
499 | if (CheckTemplate && isa<TemplateDecl>(*I)) | ||||
500 | return true; | ||||
501 | } | ||||
502 | |||||
503 | return false; | ||||
504 | } | ||||
505 | |||||
Kaelyn Uhrain | 12f3297 | 2012-05-02 00:11:40 +0000 | [diff] [blame] | 506 | static bool isTagTypeWithMissingTag(Sema &SemaRef, LookupResult &Result, |
507 | Scope *S, CXXScopeSpec &SS, | ||||
508 | IdentifierInfo *&Name, | ||||
509 | SourceLocation NameLoc) { | ||||
Richard Smith | 69e4826 | 2012-09-06 01:37:56 +0000 | [diff] [blame] | 510 | LookupResult R(SemaRef, Name, NameLoc, Sema::LookupTagName); |
511 | SemaRef.LookupParsedName(R, S, &SS); | ||||
512 | if (TagDecl *Tag = R.getAsSingle<TagDecl>()) { | ||||
Kaelyn Uhrain | 12f3297 | 2012-05-02 00:11:40 +0000 | [diff] [blame] | 513 | const char *TagName = 0; |
514 | const char *FixItTagName = 0; | ||||
515 | switch (Tag->getTagKind()) { | ||||
516 | case TTK_Class: | ||||
517 | TagName = "class"; | ||||
518 | FixItTagName = "class "; | ||||
519 | break; | ||||
520 | |||||
521 | case TTK_Enum: | ||||
522 | TagName = "enum"; | ||||
523 | FixItTagName = "enum "; | ||||
524 | break; | ||||
525 | |||||
526 | case TTK_Struct: | ||||
527 | TagName = "struct"; | ||||
528 | FixItTagName = "struct "; | ||||
529 | break; | ||||
530 | |||||
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 531 | case TTK_Interface: |
532 | TagName = "__interface"; | ||||
533 | FixItTagName = "__interface "; | ||||
534 | break; | ||||
535 | |||||
Kaelyn Uhrain | 12f3297 | 2012-05-02 00:11:40 +0000 | [diff] [blame] | 536 | case TTK_Union: |
537 | TagName = "union"; | ||||
538 | FixItTagName = "union "; | ||||
539 | break; | ||||
540 | } | ||||
541 | |||||
542 | SemaRef.Diag(NameLoc, diag::err_use_of_tag_name_without_tag) | ||||
543 | << Name << TagName << SemaRef.getLangOpts().CPlusPlus | ||||
544 | << FixItHint::CreateInsertion(NameLoc, FixItTagName); | ||||
545 | |||||
Richard Smith | 69e4826 | 2012-09-06 01:37:56 +0000 | [diff] [blame] | 546 | for (LookupResult::iterator I = Result.begin(), IEnd = Result.end(); |
547 | I != IEnd; ++I) | ||||
548 | SemaRef.Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type) | ||||
549 | << Name << TagName; | ||||
550 | |||||
551 | // Replace lookup results with just the tag decl. | ||||
552 | Result.clear(Sema::LookupTagName); | ||||
553 | SemaRef.LookupParsedName(Result, S, &SS); | ||||
Kaelyn Uhrain | 12f3297 | 2012-05-02 00:11:40 +0000 | [diff] [blame] | 554 | return true; |
555 | } | ||||
556 | |||||
Kaelyn Uhrain | 12f3297 | 2012-05-02 00:11:40 +0000 | [diff] [blame] | 557 | return false; |
558 | } | ||||
559 | |||||
Richard Smith | 0576681 | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 560 | /// Build a ParsedType for a simple-type-specifier with a nested-name-specifier. |
561 | static ParsedType buildNestedType(Sema &S, CXXScopeSpec &SS, | ||||
562 | QualType T, SourceLocation NameLoc) { | ||||
563 | ASTContext &Context = S.Context; | ||||
564 | |||||
565 | TypeLocBuilder Builder; | ||||
566 | Builder.pushTypeSpec(T).setNameLoc(NameLoc); | ||||
567 | |||||
568 | T = S.getElaboratedType(ETK_None, SS, T); | ||||
569 | ElaboratedTypeLoc ElabTL = Builder.push<ElaboratedTypeLoc>(T); | ||||
570 | ElabTL.setElaboratedKeywordLoc(SourceLocation()); | ||||
571 | ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); | ||||
572 | return S.CreateParsedType(T, Builder.getTypeSourceInfo(Context, T)); | ||||
573 | } | ||||
574 | |||||
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 575 | Sema::NameClassification Sema::ClassifyName(Scope *S, |
576 | CXXScopeSpec &SS, | ||||
577 | IdentifierInfo *&Name, | ||||
578 | SourceLocation NameLoc, | ||||
Richard Smith | 0576681 | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 579 | const Token &NextToken, |
580 | bool IsAddressOfOperand, | ||||
581 | CorrectionCandidateCallback *CCC) { | ||||
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 582 | DeclarationNameInfo NameInfo(Name, NameLoc); |
583 | ObjCMethodDecl *CurMethod = getCurMethodDecl(); | ||||
584 | |||||
585 | if (NextToken.is(tok::coloncolon)) { | ||||
586 | BuildCXXNestedNameSpecifier(S, *Name, NameLoc, NextToken.getLocation(), | ||||
587 | QualType(), false, SS, 0, false); | ||||
588 | |||||
589 | } | ||||
590 | |||||
591 | LookupResult Result(*this, Name, NameLoc, LookupOrdinaryName); | ||||
592 | LookupParsedName(Result, S, &SS, !CurMethod); | ||||
593 | |||||
594 | // Perform lookup for Objective-C instance variables (including automatically | ||||
595 | // synthesized instance variables), if we're in an Objective-C method. | ||||
596 | // FIXME: This lookup really, really needs to be folded in to the normal | ||||
597 | // unqualified lookup mechanism. | ||||
598 | if (!SS.isSet() && CurMethod && !isResultTypeOrTemplate(Result, NextToken)) { | ||||
599 | ExprResult E = LookupInObjCMethod(Result, S, Name, true); | ||||
Douglas Gregor | ec385cf | 2011-04-25 15:05:41 +0000 | [diff] [blame] | 600 | if (E.get() || E.isInvalid()) |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 601 | return E; |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 602 | } |
603 | |||||
604 | bool SecondTry = false; | ||||
605 | bool IsFilteredTemplateName = false; | ||||
606 | |||||
607 | Corrected: | ||||
608 | switch (Result.getResultKind()) { | ||||
609 | case LookupResult::NotFound: | ||||
610 | // If an unqualified-id is followed by a '(', then we have a function | ||||
611 | // call. | ||||
612 | if (!SS.isSet() && NextToken.is(tok::l_paren)) { | ||||
613 | // In C++, this is an ADL-only call. | ||||
614 | // FIXME: Reference? | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 615 | if (getLangOpts().CPlusPlus) |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 616 | return BuildDeclarationNameExpr(SS, Result, /*ADL=*/true); |
617 | |||||
618 | // C90 6.3.2.2: | ||||
619 | // If the expression that precedes the parenthesized argument list in a | ||||
620 | // function call consists solely of an identifier, and if no | ||||
621 | // declaration is visible for this identifier, the identifier is | ||||
622 | // implicitly declared exactly as if, in the innermost block containing | ||||
623 | // the function call, the declaration | ||||
624 | // | ||||
625 | // extern int identifier (); | ||||
626 | // | ||||
627 | // appeared. | ||||
628 | // | ||||
629 | // We also allow this in C99 as an extension. | ||||
630 | if (NamedDecl *D = ImplicitlyDefineFunction(NameLoc, *Name, S)) { | ||||
631 | Result.addDecl(D); | ||||
632 | Result.resolveKind(); | ||||
633 | return BuildDeclarationNameExpr(SS, Result, /*ADL=*/false); | ||||
634 | } | ||||
635 | } | ||||
636 | |||||
637 | // In C, we first see whether there is a tag type by the same name, in | ||||
638 | // which case it's likely that the user just forget to write "enum", | ||||
639 | // "struct", or "union". | ||||
Kaelyn Uhrain | 12f3297 | 2012-05-02 00:11:40 +0000 | [diff] [blame] | 640 | if (!getLangOpts().CPlusPlus && !SecondTry && |
641 | isTagTypeWithMissingTag(*this, Result, S, SS, Name, NameLoc)) { | ||||
642 | break; | ||||
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 643 | } |
644 | |||||
645 | // Perform typo correction to determine if there is another name that is | ||||
646 | // close to this name. | ||||
Richard Smith | 0576681 | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 647 | if (!SecondTry && CCC) { |
Douglas Gregor | 3a348c8 | 2011-07-14 04:54:23 +0000 | [diff] [blame] | 648 | SecondTry = true; |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 649 | if (TypoCorrection Corrected = CorrectTypo(Result.getLookupNameInfo(), |
David Blaikie | d662a79 | 2011-10-19 22:56:21 +0000 | [diff] [blame] | 650 | Result.getLookupKind(), S, |
Richard Smith | 0576681 | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 651 | &SS, *CCC)) { |
Douglas Gregor | 27766d2 | 2011-04-27 03:47:06 +0000 | [diff] [blame] | 652 | unsigned UnqualifiedDiag = diag::err_undeclared_var_use_suggest; |
653 | unsigned QualifiedDiag = diag::err_no_member_suggest; | ||||
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 654 | |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 655 | NamedDecl *FirstDecl = Corrected.getCorrectionDecl(); |
Douglas Gregor | 3b88735 | 2011-04-27 04:48:22 +0000 | [diff] [blame] | 656 | NamedDecl *UnderlyingFirstDecl |
657 | = FirstDecl? FirstDecl->getUnderlyingDecl() : 0; | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 658 | if (getLangOpts().CPlusPlus && NextToken.is(tok::less) && |
Douglas Gregor | 3b88735 | 2011-04-27 04:48:22 +0000 | [diff] [blame] | 659 | UnderlyingFirstDecl && isa<TemplateDecl>(UnderlyingFirstDecl)) { |
Douglas Gregor | 27766d2 | 2011-04-27 03:47:06 +0000 | [diff] [blame] | 660 | UnqualifiedDiag = diag::err_no_template_suggest; |
661 | QualifiedDiag = diag::err_no_member_template_suggest; | ||||
Douglas Gregor | 3b88735 | 2011-04-27 04:48:22 +0000 | [diff] [blame] | 662 | } else if (UnderlyingFirstDecl && |
663 | (isa<TypeDecl>(UnderlyingFirstDecl) || | ||||
664 | isa<ObjCInterfaceDecl>(UnderlyingFirstDecl) || | ||||
665 | isa<ObjCCompatibleAliasDecl>(UnderlyingFirstDecl))) { | ||||
David Blaikie | 30262b7 | 2013-03-21 21:35:15 +0000 | [diff] [blame] | 666 | UnqualifiedDiag = diag::err_unknown_typename_suggest; |
667 | QualifiedDiag = diag::err_unknown_nested_typename_suggest; | ||||
668 | } | ||||
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 669 | |
Kaelyn Uhrain | b2567dd | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 670 | if (SS.isEmpty()) { |
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 671 | diagnoseTypo(Corrected, PDiag(UnqualifiedDiag) << Name); |
Kaelyn Uhrain | b2567dd | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 672 | } else {// FIXME: is this even reachable? Test it. |
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 673 | std::string CorrectedStr(Corrected.getAsString(getLangOpts())); |
674 | bool DroppedSpecifier = Corrected.WillReplaceSpecifier() && | ||||
Kaelyn Uhrain | b2567dd | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 675 | Name->getName().equals(CorrectedStr); |
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 676 | diagnoseTypo(Corrected, PDiag(QualifiedDiag) |
677 | << Name << computeDeclContext(SS, false) | ||||
678 | << DroppedSpecifier << SS.getRange()); | ||||
Kaelyn Uhrain | b2567dd | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 679 | } |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 680 | |
681 | // Update the name, so that the caller has the new name. | ||||
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 682 | Name = Corrected.getCorrectionAsIdentifierInfo(); |
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 683 | |
Kaelyn Uhrain | a5ee634 | 2012-01-24 19:45:35 +0000 | [diff] [blame] | 684 | // Typo correction corrected to a keyword. |
685 | if (Corrected.isKeyword()) | ||||
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 686 | return Name; |
Kaelyn Uhrain | a5ee634 | 2012-01-24 19:45:35 +0000 | [diff] [blame] | 687 | |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 688 | // Also update the LookupResult... |
689 | // FIXME: This should probably go away at some point | ||||
690 | Result.clear(); | ||||
691 | Result.setLookupName(Corrected.getCorrection()); | ||||
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 692 | if (FirstDecl) |
Kaelyn Uhrain | a5ee634 | 2012-01-24 19:45:35 +0000 | [diff] [blame] | 693 | Result.addDecl(FirstDecl); |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 694 | |
695 | // If we found an Objective-C instance variable, let | ||||
696 | // LookupInObjCMethod build the appropriate expression to | ||||
697 | // reference the ivar. | ||||
698 | // FIXME: This is a gross hack. | ||||
699 | if (ObjCIvarDecl *Ivar = Result.getAsSingle<ObjCIvarDecl>()) { | ||||
700 | Result.clear(); | ||||
701 | ExprResult E(LookupInObjCMethod(Result, S, Ivar->getIdentifier())); | ||||
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 702 | return E; |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 703 | } |
704 | |||||
705 | goto Corrected; | ||||
706 | } | ||||
707 | } | ||||
708 | |||||
709 | // We failed to correct; just fall through and let the parser deal with it. | ||||
710 | Result.suppressDiagnostics(); | ||||
711 | return NameClassification::Unknown(); | ||||
712 | |||||
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 713 | case LookupResult::NotFoundInCurrentInstantiation: { |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 714 | // We performed name lookup into the current instantiation, and there were |
715 | // dependent bases, so we treat this result the same way as any other | ||||
716 | // dependent nested-name-specifier. | ||||
717 | |||||
718 | // C++ [temp.res]p2: | ||||
719 | // A name used in a template declaration or definition and that is | ||||
720 | // dependent on a template-parameter is assumed not to name a type | ||||
721 | // unless the applicable name lookup finds a type name or the name is | ||||
722 | // qualified by the keyword typename. | ||||
723 | // | ||||
724 | // FIXME: If the next token is '<', we might want to ask the parser to | ||||
725 | // perform some heroics to see if we actually have a | ||||
726 | // template-argument-list, which would indicate a missing 'template' | ||||
727 | // keyword here. | ||||
Richard Smith | 0576681 | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 728 | return ActOnDependentIdExpression(SS, /*TemplateKWLoc=*/SourceLocation(), |
729 | NameInfo, IsAddressOfOperand, | ||||
730 | /*TemplateArgs=*/0); | ||||
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 731 | } |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 732 | |
733 | case LookupResult::Found: | ||||
734 | case LookupResult::FoundOverloaded: | ||||
735 | case LookupResult::FoundUnresolvedValue: | ||||
736 | break; | ||||
737 | |||||
738 | case LookupResult::Ambiguous: | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 739 | if (getLangOpts().CPlusPlus && NextToken.is(tok::less) && |
Douglas Gregor | 3b88735 | 2011-04-27 04:48:22 +0000 | [diff] [blame] | 740 | hasAnyAcceptableTemplateNames(Result)) { |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 741 | // C++ [temp.local]p3: |
742 | // A lookup that finds an injected-class-name (10.2) can result in an | ||||
743 | // ambiguity in certain cases (for example, if it is found in more than | ||||
744 | // one base class). If all of the injected-class-names that are found | ||||
745 | // refer to specializations of the same class template, and if the name | ||||
746 | // is followed by a template-argument-list, the reference refers to the | ||||
747 | // class template itself and not a specialization thereof, and is not | ||||
748 | // ambiguous. | ||||
749 | // | ||||
750 | // This filtering can make an ambiguous result into an unambiguous one, | ||||
751 | // so try again after filtering out template names. | ||||
752 | FilterAcceptableTemplateNames(Result); | ||||
753 | if (!Result.isAmbiguous()) { | ||||
754 | IsFilteredTemplateName = true; | ||||
755 | break; | ||||
756 | } | ||||
757 | } | ||||
758 | |||||
759 | // Diagnose the ambiguity and return an error. | ||||
760 | return NameClassification::Error(); | ||||
761 | } | ||||
762 | |||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 763 | if (getLangOpts().CPlusPlus && NextToken.is(tok::less) && |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 764 | (IsFilteredTemplateName || hasAnyAcceptableTemplateNames(Result))) { |
765 | // C++ [temp.names]p3: | ||||
766 | // After name lookup (3.4) finds that a name is a template-name or that | ||||
767 | // an operator-function-id or a literal- operator-id refers to a set of | ||||
768 | // overloaded functions any member of which is a function template if | ||||
769 | // this is followed by a <, the < is always taken as the delimiter of a | ||||
770 | // template-argument-list and never as the less-than operator. | ||||
771 | if (!IsFilteredTemplateName) | ||||
772 | FilterAcceptableTemplateNames(Result); | ||||
773 | |||||
Douglas Gregor | 3b88735 | 2011-04-27 04:48:22 +0000 | [diff] [blame] | 774 | if (!Result.empty()) { |
775 | bool IsFunctionTemplate; | ||||
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 776 | bool IsVarTemplate; |
Douglas Gregor | 3b88735 | 2011-04-27 04:48:22 +0000 | [diff] [blame] | 777 | TemplateName Template; |
778 | if (Result.end() - Result.begin() > 1) { | ||||
779 | IsFunctionTemplate = true; | ||||
780 | Template = Context.getOverloadedTemplateName(Result.begin(), | ||||
781 | Result.end()); | ||||
782 | } else { | ||||
783 | TemplateDecl *TD | ||||
784 | = cast<TemplateDecl>((*Result.begin())->getUnderlyingDecl()); | ||||
785 | IsFunctionTemplate = isa<FunctionTemplateDecl>(TD); | ||||
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 786 | IsVarTemplate = isa<VarTemplateDecl>(TD); |
787 | |||||
Douglas Gregor | 3b88735 | 2011-04-27 04:48:22 +0000 | [diff] [blame] | 788 | if (SS.isSet() && !SS.isInvalid()) |
789 | Template = Context.getQualifiedTemplateName(SS.getScopeRep(), | ||||
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 790 | /*TemplateKeyword=*/false, |
Douglas Gregor | 3b88735 | 2011-04-27 04:48:22 +0000 | [diff] [blame] | 791 | TD); |
792 | else | ||||
793 | Template = TemplateName(TD); | ||||
794 | } | ||||
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 795 | |
Douglas Gregor | 3b88735 | 2011-04-27 04:48:22 +0000 | [diff] [blame] | 796 | if (IsFunctionTemplate) { |
797 | // Function templates always go through overload resolution, at which | ||||
798 | // point we'll perform the various checks (e.g., accessibility) we need | ||||
799 | // to based on which function we selected. | ||||
800 | Result.suppressDiagnostics(); | ||||
801 | |||||
802 | return NameClassification::FunctionTemplate(Template); | ||||
803 | } | ||||
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 804 | |
805 | return IsVarTemplate ? NameClassification::VarTemplate(Template) | ||||
806 | : NameClassification::TypeTemplate(Template); | ||||
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 807 | } |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 808 | } |
Richard Smith | 0576681 | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 809 | |
Douglas Gregor | 3b88735 | 2011-04-27 04:48:22 +0000 | [diff] [blame] | 810 | NamedDecl *FirstDecl = (*Result.begin())->getUnderlyingDecl(); |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 811 | if (TypeDecl *Type = dyn_cast<TypeDecl>(FirstDecl)) { |
812 | DiagnoseUseOfDecl(Type, NameLoc); | ||||
813 | QualType T = Context.getTypeDeclType(Type); | ||||
Richard Smith | 0576681 | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 814 | if (SS.isNotEmpty()) |
815 | return buildNestedType(*this, SS, T, NameLoc); | ||||
Kaelyn Uhrain | 12f3297 | 2012-05-02 00:11:40 +0000 | [diff] [blame] | 816 | return ParsedType::make(T); |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 817 | } |
Richard Smith | 0576681 | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 818 | |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 819 | ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(FirstDecl); |
820 | if (!Class) { | ||||
821 | // FIXME: It's unfortunate that we don't have a Type node for handling this. | ||||
822 | if (ObjCCompatibleAliasDecl *Alias | ||||
823 | = dyn_cast<ObjCCompatibleAliasDecl>(FirstDecl)) | ||||
824 | Class = Alias->getClassInterface(); | ||||
825 | } | ||||
826 | |||||
827 | if (Class) { | ||||
828 | DiagnoseUseOfDecl(Class, NameLoc); | ||||
829 | |||||
830 | if (NextToken.is(tok::period)) { | ||||
831 | // Interface. <something> is parsed as a property reference expression. | ||||
832 | // Just return "unknown" as a fall-through for now. | ||||
833 | Result.suppressDiagnostics(); | ||||
834 | return NameClassification::Unknown(); | ||||
835 | } | ||||
836 | |||||
837 | QualType T = Context.getObjCInterfaceType(Class); | ||||
838 | return ParsedType::make(T); | ||||
839 | } | ||||
Kaelyn Uhrain | 12f3297 | 2012-05-02 00:11:40 +0000 | [diff] [blame] | 840 | |
Richard Smith | 0576681 | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 841 | // We can have a type template here if we're classifying a template argument. |
842 | if (isa<TemplateDecl>(FirstDecl) && !isa<FunctionTemplateDecl>(FirstDecl)) | ||||
843 | return NameClassification::TypeTemplate( | ||||
844 | TemplateName(cast<TemplateDecl>(FirstDecl))); | ||||
845 | |||||
Kaelyn Uhrain | 12f3297 | 2012-05-02 00:11:40 +0000 | [diff] [blame] | 846 | // Check for a tag type hidden by a non-type decl in a few cases where it |
847 | // 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] | 848 | bool NextIsOp = NextToken.is(tok::amp) || NextToken.is(tok::star); |
849 | if ((NextToken.is(tok::identifier) || | ||||
850 | (NextIsOp && FirstDecl->isFunctionOrFunctionTemplate())) && | ||||
851 | isTagTypeWithMissingTag(*this, Result, S, SS, Name, NameLoc)) { | ||||
852 | TypeDecl *Type = Result.getAsSingle<TypeDecl>(); | ||||
853 | DiagnoseUseOfDecl(Type, NameLoc); | ||||
854 | QualType T = Context.getTypeDeclType(Type); | ||||
855 | if (SS.isNotEmpty()) | ||||
856 | return buildNestedType(*this, SS, T, NameLoc); | ||||
857 | return ParsedType::make(T); | ||||
Kaelyn Uhrain | 12f3297 | 2012-05-02 00:11:40 +0000 | [diff] [blame] | 858 | } |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 859 | |
Richard Smith | 0576681 | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 860 | if (FirstDecl->isCXXClassMember()) |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 861 | return BuildPossibleImplicitMemberExpr(SS, SourceLocation(), Result, 0); |
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. | ||||
875 | if (isa<FunctionDecl>(DC)) { | ||||
876 | DC = DC->getLexicalParent(); | ||||
877 | |||||
878 | // A function not defined within a class will always return to its | ||||
879 | // lexical context. | ||||
880 | if (!isa<CXXRecordDecl>(DC)) | ||||
881 | return DC; | ||||
882 | |||||
883 | // A C++ inline method/friend is parsed *after* the topmost class | ||||
884 | // it was declared in is fully parsed ("complete"); the topmost | ||||
885 | // class is the context we need to return to. | ||||
Argyrios Kyrtzidis | 77407b8 | 2008-11-19 18:01:13 +0000 | [diff] [blame] | 886 | while (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(DC->getLexicalParent())) |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 887 | DC = RD; |
888 | |||||
889 | // Return the declaration context of the topmost class the inline method is | ||||
890 | // declared in. | ||||
891 | return DC; | ||||
892 | } | ||||
893 | |||||
Argyrios Kyrtzidis | 77407b8 | 2008-11-19 18:01:13 +0000 | [diff] [blame] | 894 | return DC->getLexicalParent(); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 895 | } |
896 | |||||
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 897 | void Sema::PushDeclContext(Scope *S, DeclContext *DC) { |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 898 | assert(getContainingDC(DC) == CurContext && |
Zhongxing Xu | e50897a | 2008-12-08 07:14:51 +0000 | [diff] [blame] | 899 | "The next DeclContext should be lexically contained in the current one."); |
Chris Lattner | 9fdf9c6 | 2008-04-22 18:39:57 +0000 | [diff] [blame] | 900 | CurContext = DC; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 901 | S->setEntity(DC); |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 902 | } |
903 | |||||
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 904 | void Sema::PopDeclContext() { |
905 | assert(CurContext && "DeclContext imbalance!"); | ||||
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 906 | |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 907 | CurContext = getContainingDC(CurContext); |
John McCall | acb7039 | 2010-07-23 22:45:07 +0000 | [diff] [blame] | 908 | assert(CurContext && "Popped translation unit!"); |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 909 | } |
910 | |||||
Argyrios Kyrtzidis | 179fe1a | 2009-06-17 23:19:02 +0000 | [diff] [blame] | 911 | /// EnterDeclaratorContext - Used when we must lookup names in the context |
912 | /// of a declarator's nested name specifier. | ||||
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 913 | /// |
Argyrios Kyrtzidis | 1d17553 | 2009-06-17 23:15:40 +0000 | [diff] [blame] | 914 | void Sema::EnterDeclaratorContext(Scope *S, DeclContext *DC) { |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 915 | // C++0x [basic.lookup.unqual]p13: |
916 | // A name used in the definition of a static data member of class | ||||
917 | // X (after the qualified-id of the static member) is looked up as | ||||
918 | // if the name was used in a member function of X. | ||||
919 | // C++0x [basic.lookup.unqual]p14: | ||||
920 | // If a variable member of a namespace is defined outside of the | ||||
921 | // scope of its namespace then any name used in the definition of | ||||
922 | // the variable member (after the declarator-id) is looked up as | ||||
923 | // if the definition of the variable member occurred in its | ||||
924 | // namespace. | ||||
925 | // Both of these imply that we should push a scope whose context | ||||
926 | // is the semantic context of the declaration. We can't use | ||||
927 | // PushDeclContext here because that context is not necessarily | ||||
928 | // lexically contained in the current context. Fortunately, | ||||
929 | // the containing scope should have the appropriate information. | ||||
930 | |||||
931 | assert(!S->getEntity() && "scope already has entity"); | ||||
932 | |||||
933 | #ifndef NDEBUG | ||||
934 | Scope *Ancestor = S->getParent(); | ||||
935 | while (!Ancestor->getEntity()) Ancestor = Ancestor->getParent(); | ||||
936 | assert(Ancestor->getEntity() == CurContext && "ancestor context mismatch"); | ||||
937 | #endif | ||||
938 | |||||
Argyrios Kyrtzidis | 1d17553 | 2009-06-17 23:15:40 +0000 | [diff] [blame] | 939 | CurContext = DC; |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 940 | S->setEntity(DC); |
Argyrios Kyrtzidis | 1d17553 | 2009-06-17 23:15:40 +0000 | [diff] [blame] | 941 | } |
942 | |||||
Argyrios Kyrtzidis | 1d17553 | 2009-06-17 23:15:40 +0000 | [diff] [blame] | 943 | void Sema::ExitDeclaratorContext(Scope *S) { |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 944 | assert(S->getEntity() == CurContext && "Context imbalance!"); |
Argyrios Kyrtzidis | 1d17553 | 2009-06-17 23:15:40 +0000 | [diff] [blame] | 945 | |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 946 | // Switch back to the lexical context. The safety of this is |
947 | // enforced by an assert in EnterDeclaratorContext. | ||||
948 | Scope *Ancestor = S->getParent(); | ||||
949 | while (!Ancestor->getEntity()) Ancestor = Ancestor->getParent(); | ||||
950 | CurContext = (DeclContext*) Ancestor->getEntity(); | ||||
951 | |||||
952 | // We don't need to do anything with the scope, which is going to | ||||
953 | // disappear. | ||||
Argyrios Kyrtzidis | 1d17553 | 2009-06-17 23:15:40 +0000 | [diff] [blame] | 954 | } |
955 | |||||
Caitlin Sadowski | ed9d84a | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 956 | |
957 | void Sema::ActOnReenterFunctionContext(Scope* S, Decl *D) { | ||||
958 | FunctionDecl *FD = dyn_cast<FunctionDecl>(D); | ||||
959 | if (FunctionTemplateDecl *TFD = dyn_cast_or_null<FunctionTemplateDecl>(D)) { | ||||
960 | // We assume that the caller has already called | ||||
961 | // ActOnReenterTemplateScope | ||||
962 | FD = TFD->getTemplatedDecl(); | ||||
963 | } | ||||
964 | if (!FD) | ||||
965 | return; | ||||
966 | |||||
DeLesley Hutchins | cf2fa2f | 2012-04-06 15:10:17 +0000 | [diff] [blame] | 967 | // Same implementation as PushDeclContext, but enters the context |
968 | // from the lexical parent, rather than the top-level class. | ||||
969 | assert(CurContext == FD->getLexicalParent() && | ||||
970 | "The next DeclContext should be lexically contained in the current one."); | ||||
971 | CurContext = FD; | ||||
972 | S->setEntity(CurContext); | ||||
973 | |||||
Caitlin Sadowski | ed9d84a | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 974 | for (unsigned P = 0, NumParams = FD->getNumParams(); P < NumParams; ++P) { |
975 | ParmVarDecl *Param = FD->getParamDecl(P); | ||||
976 | // If the parameter has an identifier, then add it to the scope | ||||
977 | if (Param->getIdentifier()) { | ||||
978 | S->AddDecl(Param); | ||||
979 | IdResolver.AddDecl(Param); | ||||
980 | } | ||||
981 | } | ||||
982 | } | ||||
983 | |||||
984 | |||||
DeLesley Hutchins | cf2fa2f | 2012-04-06 15:10:17 +0000 | [diff] [blame] | 985 | void Sema::ActOnExitFunctionContext() { |
986 | // Same implementation as PopDeclContext, but returns to the lexical parent, | ||||
987 | // rather than the top-level class. | ||||
988 | assert(CurContext && "DeclContext imbalance!"); | ||||
989 | CurContext = CurContext->getLexicalParent(); | ||||
990 | assert(CurContext && "Popped translation unit!"); | ||||
991 | } | ||||
992 | |||||
993 | |||||
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 994 | /// \brief Determine whether we allow overloading of the function |
995 | /// PrevDecl with another declaration. | ||||
996 | /// | ||||
997 | /// This routine determines whether overloading is possible, not | ||||
998 | /// whether some new function is actually an overload. It will return | ||||
999 | /// true in C++ (where we can always provide overloads) or, as an | ||||
1000 | /// extension, in C when the previous function is already an | ||||
1001 | /// overloaded function declaration or has the "overloadable" | ||||
1002 | /// attribute. | ||||
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1003 | static bool AllowOverloadingOfFunction(LookupResult &Previous, |
1004 | ASTContext &Context) { | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1005 | if (Context.getLangOpts().CPlusPlus) |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1006 | return true; |
1007 | |||||
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1008 | if (Previous.getResultKind() == LookupResult::FoundOverloaded) |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1009 | return true; |
1010 | |||||
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1011 | return (Previous.getResultKind() == LookupResult::Found |
1012 | && Previous.getFoundDecl()->hasAttr<OverloadableAttr>()); | ||||
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1013 | } |
1014 | |||||
Argyrios Kyrtzidis | 87f3ff0 | 2008-04-12 00:47:19 +0000 | [diff] [blame] | 1015 | /// Add this decl to the scope shadowed decl chains. |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 1016 | void Sema::PushOnScopeChains(NamedDecl *D, Scope *S, bool AddToContext) { |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 1017 | // Move up the scope chain until we find the nearest enclosing |
1018 | // non-transparent context. The declaration will be introduced into this | ||||
1019 | // scope. | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1020 | while (S->getEntity() && |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 1021 | ((DeclContext *)S->getEntity())->isTransparentContext()) |
1022 | S = S->getParent(); | ||||
1023 | |||||
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 1024 | // Add scoped declarations into their context, so that they can be |
1025 | // found later. Declarations without a context won't be inserted | ||||
1026 | // into any context. | ||||
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 1027 | if (AddToContext) |
1028 | CurContext->addDecl(D); | ||||
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 1029 | |
Richard Smith | a41c97a | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 1030 | // Out-of-line definitions shouldn't be pushed into scope in C++, unless they |
1031 | // are function-local declarations. | ||||
1032 | if (getLangOpts().CPlusPlus && D->isOutOfLine() && | ||||
Douglas Gregor | 6d0468b | 2011-10-09 22:57:49 +0000 | [diff] [blame] | 1033 | !D->getDeclContext()->getRedeclContext()->Equals( |
Richard Smith | a41c97a | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 1034 | D->getLexicalDeclContext()->getRedeclContext()) && |
1035 | !D->getLexicalDeclContext()->isFunctionOrMethod()) | ||||
Chandler Carruth | 8761d68 | 2010-02-21 07:08:09 +0000 | [diff] [blame] | 1036 | return; |
1037 | |||||
1038 | // Template instantiations should also not be pushed into scope. | ||||
1039 | if (isa<FunctionDecl>(D) && | ||||
1040 | cast<FunctionDecl>(D)->isFunctionTemplateSpecialization()) | ||||
Douglas Gregor | d04b1be | 2009-09-28 18:41:37 +0000 | [diff] [blame] | 1041 | return; |
1042 | |||||
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1043 | // If this replaces anything in the current scope, |
1044 | IdentifierResolver::iterator I = IdResolver.begin(D->getDeclName()), | ||||
1045 | IEnd = IdResolver.end(); | ||||
1046 | for (; I != IEnd; ++I) { | ||||
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1047 | if (S->isDeclScope(*I) && D->declarationReplaces(*I)) { |
1048 | S->RemoveDecl(*I); | ||||
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1049 | IdResolver.RemoveDecl(*I); |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 1050 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1051 | // Should only need to replace one decl. |
1052 | break; | ||||
Douglas Gregor | 516ff43 | 2009-04-24 02:57:34 +0000 | [diff] [blame] | 1053 | } |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 1054 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1055 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1056 | S->AddDecl(D); |
Douglas Gregor | 7cbc558 | 2011-03-14 21:19:51 +0000 | [diff] [blame] | 1057 | |
1058 | if (isa<LabelDecl>(D) && !cast<LabelDecl>(D)->isGnuLocal()) { | ||||
1059 | // Implicitly-generated labels may end up getting generated in an order that | ||||
1060 | // isn't strictly lexical, which breaks name lookup. Be careful to insert | ||||
1061 | // the label at the appropriate place in the identifier chain. | ||||
1062 | for (I = IdResolver.begin(D->getDeclName()); I != IEnd; ++I) { | ||||
Douglas Gregor | 1d2de76 | 2011-03-24 14:35:16 +0000 | [diff] [blame] | 1063 | DeclContext *IDC = (*I)->getLexicalDeclContext()->getRedeclContext(); |
Douglas Gregor | 250e7a7 | 2011-03-16 16:39:03 +0000 | [diff] [blame] | 1064 | if (IDC == CurContext) { |
1065 | if (!S->isDeclScope(*I)) | ||||
1066 | continue; | ||||
1067 | } else if (IDC->Encloses(CurContext)) | ||||
Douglas Gregor | 7cbc558 | 2011-03-14 21:19:51 +0000 | [diff] [blame] | 1068 | break; |
1069 | } | ||||
1070 | |||||
Douglas Gregor | 250e7a7 | 2011-03-16 16:39:03 +0000 | [diff] [blame] | 1071 | IdResolver.InsertDeclAfter(I, D); |
Douglas Gregor | 7cbc558 | 2011-03-14 21:19:51 +0000 | [diff] [blame] | 1072 | } else { |
1073 | IdResolver.AddDecl(D); | ||||
1074 | } | ||||
Argyrios Kyrtzidis | 87f3ff0 | 2008-04-12 00:47:19 +0000 | [diff] [blame] | 1075 | } |
1076 | |||||
Douglas Gregor | eee242f | 2011-10-27 09:33:13 +0000 | [diff] [blame] | 1077 | void Sema::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) { |
1078 | if (IdResolver.tryAddTopLevelDecl(D, Name) && TUScope) | ||||
1079 | TUScope->AddDecl(D); | ||||
1080 | } | ||||
1081 | |||||
Richard Smith | dd9459f | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 1082 | bool Sema::isDeclInScope(NamedDecl *D, DeclContext *Ctx, Scope *S, |
Douglas Gregor | cc20945 | 2011-03-07 16:54:27 +0000 | [diff] [blame] | 1083 | bool ExplicitInstantiationOrSpecialization) { |
Nico Weber | 355a166 | 2012-12-17 03:51:09 +0000 | [diff] [blame] | 1084 | return IdResolver.isDeclInScope(D, Ctx, S, |
Douglas Gregor | cc20945 | 2011-03-07 16:54:27 +0000 | [diff] [blame] | 1085 | ExplicitInstantiationOrSpecialization); |
Douglas Gregor | 2531c2d | 2009-09-28 00:47:05 +0000 | [diff] [blame] | 1086 | } |
1087 | |||||
John McCall | 5f1e094 | 2010-08-24 08:50:51 +0000 | [diff] [blame] | 1088 | Scope *Sema::getScopeForDeclContext(Scope *S, DeclContext *DC) { |
1089 | DeclContext *TargetDC = DC->getPrimaryContext(); | ||||
1090 | do { | ||||
1091 | if (DeclContext *ScopeDC = (DeclContext*) S->getEntity()) | ||||
1092 | if (ScopeDC->getPrimaryContext() == TargetDC) | ||||
1093 | return S; | ||||
1094 | } while ((S = S->getParent())); | ||||
1095 | |||||
1096 | return 0; | ||||
1097 | } | ||||
1098 | |||||
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1099 | static bool isOutOfScopePreviousDeclaration(NamedDecl *, |
1100 | DeclContext*, | ||||
1101 | ASTContext&); | ||||
1102 | |||||
1103 | /// Filters out lookup results that don't fall within the given scope | ||||
1104 | /// as determined by isDeclInScope. | ||||
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1105 | void Sema::FilterLookupForScope(LookupResult &R, |
1106 | DeclContext *Ctx, Scope *S, | ||||
1107 | bool ConsiderLinkage, | ||||
1108 | bool ExplicitInstantiationOrSpecialization) { | ||||
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1109 | LookupResult::Filter F = R.makeFilter(); |
1110 | while (F.hasNext()) { | ||||
1111 | NamedDecl *D = F.next(); | ||||
1112 | |||||
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1113 | if (isDeclInScope(D, Ctx, S, ExplicitInstantiationOrSpecialization)) |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1114 | continue; |
1115 | |||||
1116 | if (ConsiderLinkage && | ||||
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1117 | isOutOfScopePreviousDeclaration(D, Ctx, Context)) |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1118 | continue; |
1119 | |||||
1120 | F.erase(); | ||||
1121 | } | ||||
1122 | |||||
1123 | F.done(); | ||||
1124 | } | ||||
1125 | |||||
1126 | static bool isUsingDecl(NamedDecl *D) { | ||||
1127 | return isa<UsingShadowDecl>(D) || | ||||
1128 | isa<UnresolvedUsingTypenameDecl>(D) || | ||||
1129 | isa<UnresolvedUsingValueDecl>(D); | ||||
1130 | } | ||||
1131 | |||||
1132 | /// Removes using shadow declarations from the lookup results. | ||||
1133 | static void RemoveUsingDecls(LookupResult &R) { | ||||
1134 | LookupResult::Filter F = R.makeFilter(); | ||||
1135 | while (F.hasNext()) | ||||
1136 | if (isUsingDecl(F.next())) | ||||
1137 | F.erase(); | ||||
1138 | |||||
1139 | F.done(); | ||||
1140 | } | ||||
1141 | |||||
Argyrios Kyrtzidis | 06999f8 | 2010-08-15 10:17:33 +0000 | [diff] [blame] | 1142 | /// \brief Check for this common pattern: |
1143 | /// @code | ||||
1144 | /// class S { | ||||
1145 | /// S(const S&); // DO NOT IMPLEMENT | ||||
1146 | /// void operator=(const S&); // DO NOT IMPLEMENT | ||||
1147 | /// }; | ||||
1148 | /// @endcode | ||||
1149 | static bool IsDisallowedCopyOrAssign(const CXXMethodDecl *D) { | ||||
1150 | // FIXME: Should check for private access too but access is set after we get | ||||
1151 | // the decl here. | ||||
Sean Hunt | 10620eb | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 1152 | if (D->doesThisDeclarationHaveABody()) |
Argyrios Kyrtzidis | 06999f8 | 2010-08-15 10:17:33 +0000 | [diff] [blame] | 1153 | return false; |
1154 | |||||
1155 | if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(D)) | ||||
1156 | return CD->isCopyConstructor(); | ||||
Douglas Gregor | 27c08ab | 2010-09-27 22:06:20 +0000 | [diff] [blame] | 1157 | if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) |
1158 | return Method->isCopyAssignmentOperator(); | ||||
1159 | return false; | ||||
Argyrios Kyrtzidis | 06999f8 | 2010-08-15 10:17:33 +0000 | [diff] [blame] | 1160 | } |
1161 | |||||
Rafael Espindola | 2d1b096 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 1162 | // We need this to handle |
1163 | // | ||||
1164 | // typedef struct { | ||||
1165 | // void *foo() { return 0; } | ||||
1166 | // } A; | ||||
1167 | // | ||||
1168 | // When we see foo we don't know if after the typedef we will get 'A' or '*A' | ||||
1169 | // for example. If 'A', foo will have external linkage. If we have '*A', | ||||
1170 | // foo will have no linkage. Since we can't know untill we get to the end | ||||
1171 | // of the typedef, this function finds out if D might have non external linkage. | ||||
1172 | // Callers should verify at the end of the TU if it D has external linkage or | ||||
1173 | // not. | ||||
1174 | bool Sema::mightHaveNonExternalLinkage(const DeclaratorDecl *D) { | ||||
1175 | const DeclContext *DC = D->getDeclContext(); | ||||
1176 | while (!DC->isTranslationUnit()) { | ||||
1177 | if (const RecordDecl *RD = dyn_cast<RecordDecl>(DC)){ | ||||
1178 | if (!RD->hasNameForLinkage()) | ||||
1179 | return true; | ||||
1180 | } | ||||
1181 | DC = DC->getParent(); | ||||
1182 | } | ||||
1183 | |||||
Rafael Espindola | 181e3ec | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 1184 | return !D->isExternallyVisible(); |
Rafael Espindola | 2d1b096 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 1185 | } |
1186 | |||||
Eli Friedman | 39bd371 | 2013-09-10 03:05:56 +0000 | [diff] [blame] | 1187 | // FIXME: This needs to be refactored; some other isInMainFile users want |
1188 | // these semantics. | ||||
1189 | static bool isMainFileLoc(const Sema &S, SourceLocation Loc) { | ||||
1190 | if (S.TUKind != TU_Complete) | ||||
1191 | return false; | ||||
1192 | return S.SourceMgr.isInMainFile(Loc); | ||||
1193 | } | ||||
1194 | |||||
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 1195 | bool Sema::ShouldWarnIfUnusedFileScopedDecl(const DeclaratorDecl *D) const { |
1196 | assert(D); | ||||
Argyrios Kyrtzidis | f6d1d43 | 2010-08-13 18:42:29 +0000 | [diff] [blame] | 1197 | |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 1198 | if (D->isInvalidDecl() || D->isUsed() || D->hasAttr<UnusedAttr>()) |
1199 | return false; | ||||
1200 | |||||
1201 | // Ignore class templates. | ||||
Chandler Carruth | ef9d09c | 2011-01-03 19:27:19 +0000 | [diff] [blame] | 1202 | if (D->getDeclContext()->isDependentContext() || |
1203 | D->getLexicalDeclContext()->isDependentContext()) | ||||
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 1204 | return false; |
1205 | |||||
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 1206 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Argyrios Kyrtzidis | 06999f8 | 2010-08-15 10:17:33 +0000 | [diff] [blame] | 1207 | if (FD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation) |
1208 | return false; | ||||
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 1209 | |
Argyrios Kyrtzidis | 06999f8 | 2010-08-15 10:17:33 +0000 | [diff] [blame] | 1210 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) { |
1211 | if (MD->isVirtual() || IsDisallowedCopyOrAssign(MD)) | ||||
1212 | return false; | ||||
1213 | } else { | ||||
Eli Friedman | 39bd371 | 2013-09-10 03:05:56 +0000 | [diff] [blame] | 1214 | // 'static inline' functions are defined in headers; don't warn. |
1215 | if (FD->isInlineSpecified() && | ||||
1216 | !isMainFileLoc(*this, FD->getLocation())) | ||||
Argyrios Kyrtzidis | 06999f8 | 2010-08-15 10:17:33 +0000 | [diff] [blame] | 1217 | return false; |
1218 | } | ||||
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 1219 | |
Sean Hunt | 10620eb | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 1220 | if (FD->doesThisDeclarationHaveABody() && |
John McCall | 82b9659 | 2010-10-27 01:41:35 +0000 | [diff] [blame] | 1221 | Context.DeclMustBeEmitted(FD)) |
1222 | return false; | ||||
John McCall | 82b9659 | 2010-10-27 01:41:35 +0000 | [diff] [blame] | 1223 | } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
Eli Friedman | 39bd371 | 2013-09-10 03:05:56 +0000 | [diff] [blame] | 1224 | // Constants and utility variables are defined in headers with internal |
1225 | // linkage; don't warn. (Unlike functions, there isn't a convenient marker | ||||
1226 | // like "inline".) | ||||
1227 | if (!isMainFileLoc(*this, VD->getLocation())) | ||||
1228 | return false; | ||||
1229 | |||||
Eli Friedman | 39bd371 | 2013-09-10 03:05:56 +0000 | [diff] [blame] | 1230 | if (Context.DeclMustBeEmitted(VD)) |
John McCall | 82b9659 | 2010-10-27 01:41:35 +0000 | [diff] [blame] | 1231 | return false; |
1232 | |||||
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 1233 | if (VD->isStaticDataMember() && |
1234 | VD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation) | ||||
1235 | return false; | ||||
John McCall | 82b9659 | 2010-10-27 01:41:35 +0000 | [diff] [blame] | 1236 | } else { |
1237 | return false; | ||||
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 1238 | } |
1239 | |||||
John McCall | 82b9659 | 2010-10-27 01:41:35 +0000 | [diff] [blame] | 1240 | // Only warn for unused decls internal to the translation unit. |
Rafael Espindola | 2d1b096 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 1241 | return mightHaveNonExternalLinkage(D); |
John McCall | 82b9659 | 2010-10-27 01:41:35 +0000 | [diff] [blame] | 1242 | } |
1243 | |||||
1244 | void Sema::MarkUnusedFileScopedDecl(const DeclaratorDecl *D) { | ||||
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 1245 | if (!D) |
1246 | return; | ||||
1247 | |||||
1248 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { | ||||
1249 | const FunctionDecl *First = FD->getFirstDeclaration(); | ||||
1250 | if (FD != First && ShouldWarnIfUnusedFileScopedDecl(First)) | ||||
1251 | return; // First should already be in the vector. | ||||
1252 | } | ||||
1253 | |||||
1254 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { | ||||
1255 | const VarDecl *First = VD->getFirstDeclaration(); | ||||
1256 | if (VD != First && ShouldWarnIfUnusedFileScopedDecl(First)) | ||||
1257 | return; // First should already be in the vector. | ||||
1258 | } | ||||
1259 | |||||
David Blaikie | 7f7c42b | 2012-05-26 05:35:39 +0000 | [diff] [blame] | 1260 | if (ShouldWarnIfUnusedFileScopedDecl(D)) |
1261 | UnusedFileScopedDecls.push_back(D); | ||||
1262 | } | ||||
Argyrios Kyrtzidis | 49b96d1 | 2010-08-13 18:42:17 +0000 | [diff] [blame] | 1263 | |
Anders Carlsson | 99a000e | 2009-11-07 07:18:14 +0000 | [diff] [blame] | 1264 | static bool ShouldDiagnoseUnusedDecl(const NamedDecl *D) { |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 1265 | if (D->isInvalidDecl()) |
1266 | return false; | ||||
1267 | |||||
Eli Friedman | dd9d645 | 2012-01-13 23:41:25 +0000 | [diff] [blame] | 1268 | if (D->isReferenced() || D->isUsed() || D->hasAttr<UnusedAttr>()) |
Anders Carlsson | f7613d5 | 2009-11-07 07:26:56 +0000 | [diff] [blame] | 1269 | return false; |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 1270 | |
Chris Lattner | 57ad378 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 1271 | if (isa<LabelDecl>(D)) |
1272 | return true; | ||||
1273 | |||||
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 1274 | // White-list anything that isn't a local variable. |
1275 | if (!isa<VarDecl>(D) || isa<ParmVarDecl>(D) || isa<ImplicitParamDecl>(D) || | ||||
1276 | !D->getDeclContext()->isFunctionOrMethod()) | ||||
1277 | return false; | ||||
1278 | |||||
1279 | // Types of valid local variables should be complete, so this should succeed. | ||||
Rafael Espindola | 1a5d355 | 2012-01-06 04:54:01 +0000 | [diff] [blame] | 1280 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
John McCall | aec5860 | 2010-03-31 02:47:45 +0000 | [diff] [blame] | 1281 | |
1282 | // White-list anything with an __attribute__((unused)) type. | ||||
1283 | QualType Ty = VD->getType(); | ||||
1284 | |||||
1285 | // Only look at the outermost level of typedef. | ||||
Douglas Gregor | 2c8e81e | 2012-09-14 05:10:40 +0000 | [diff] [blame] | 1286 | if (const TypedefType *TT = Ty->getAs<TypedefType>()) { |
John McCall | aec5860 | 2010-03-31 02:47:45 +0000 | [diff] [blame] | 1287 | if (TT->getDecl()->hasAttr<UnusedAttr>()) |
1288 | return false; | ||||
1289 | } | ||||
1290 | |||||
Douglas Gregor | 5764f61 | 2010-05-08 23:05:03 +0000 | [diff] [blame] | 1291 | // If we failed to complete the type for some reason, or if the type is |
1292 | // dependent, don't diagnose the variable. | ||||
1293 | if (Ty->isIncompleteType() || Ty->isDependentType()) | ||||
Douglas Gregor | a6a292b | 2010-04-27 16:20:13 +0000 | [diff] [blame] | 1294 | return false; |
1295 | |||||
John McCall | aec5860 | 2010-03-31 02:47:45 +0000 | [diff] [blame] | 1296 | if (const TagType *TT = Ty->getAs<TagType>()) { |
1297 | const TagDecl *Tag = TT->getDecl(); | ||||
1298 | if (Tag->hasAttr<UnusedAttr>()) | ||||
1299 | return false; | ||||
1300 | |||||
1301 | if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Tag)) { | ||||
Lubos Lunak | 1d3ce65 | 2013-07-20 15:05:36 +0000 | [diff] [blame] | 1302 | if (!RD->hasTrivialDestructor() && !RD->hasAttr<WarnUnusedAttr>()) |
Anders Carlsson | f7613d5 | 2009-11-07 07:26:56 +0000 | [diff] [blame] | 1303 | return false; |
Rafael Espindola | 1a5d355 | 2012-01-06 04:54:01 +0000 | [diff] [blame] | 1304 | |
1305 | if (const Expr *Init = VD->getInit()) { | ||||
David Blaikie | 39e1776 | 2012-10-24 21:29:06 +0000 | [diff] [blame] | 1306 | if (const ExprWithCleanups *Cleanups = dyn_cast<ExprWithCleanups>(Init)) |
1307 | Init = Cleanups->getSubExpr(); | ||||
Rafael Espindola | 1a5d355 | 2012-01-06 04:54:01 +0000 | [diff] [blame] | 1308 | const CXXConstructExpr *Construct = |
1309 | dyn_cast<CXXConstructExpr>(Init); | ||||
1310 | if (Construct && !Construct->isElidable()) { | ||||
1311 | CXXConstructorDecl *CD = Construct->getConstructor(); | ||||
Lubos Lunak | 1d3ce65 | 2013-07-20 15:05:36 +0000 | [diff] [blame] | 1312 | if (!CD->isTrivial() && !RD->hasAttr<WarnUnusedAttr>()) |
Rafael Espindola | 1a5d355 | 2012-01-06 04:54:01 +0000 | [diff] [blame] | 1313 | return false; |
1314 | } | ||||
1315 | } | ||||
Anders Carlsson | f7613d5 | 2009-11-07 07:26:56 +0000 | [diff] [blame] | 1316 | } |
1317 | } | ||||
John McCall | aec5860 | 2010-03-31 02:47:45 +0000 | [diff] [blame] | 1318 | |
1319 | // TODO: __attribute__((unused)) templates? | ||||
Anders Carlsson | f7613d5 | 2009-11-07 07:26:56 +0000 | [diff] [blame] | 1320 | } |
1321 | |||||
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 1322 | return true; |
Anders Carlsson | 99a000e | 2009-11-07 07:18:14 +0000 | [diff] [blame] | 1323 | } |
1324 | |||||
Anna Zaks | d5612a2 | 2011-07-28 20:52:06 +0000 | [diff] [blame] | 1325 | static void GenerateFixForUnusedDecl(const NamedDecl *D, ASTContext &Ctx, |
1326 | FixItHint &Hint) { | ||||
1327 | if (isa<LabelDecl>(D)) { | ||||
1328 | SourceLocation AfterColon = Lexer::findLocationAfterToken(D->getLocEnd(), | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1329 | tok::colon, Ctx.getSourceManager(), Ctx.getLangOpts(), true); |
Anna Zaks | d5612a2 | 2011-07-28 20:52:06 +0000 | [diff] [blame] | 1330 | if (AfterColon.isInvalid()) |
1331 | return; | ||||
1332 | Hint = FixItHint::CreateRemoval(CharSourceRange:: | ||||
1333 | getCharRange(D->getLocStart(), AfterColon)); | ||||
1334 | } | ||||
1335 | return; | ||||
1336 | } | ||||
1337 | |||||
Chris Lattner | 337e550 | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1338 | /// DiagnoseUnusedDecl - Emit warnings about declarations that are not used |
1339 | /// unless they are marked attr(unused). | ||||
Douglas Gregor | 5764f61 | 2010-05-08 23:05:03 +0000 | [diff] [blame] | 1340 | void Sema::DiagnoseUnusedDecl(const NamedDecl *D) { |
Anna Zaks | d5612a2 | 2011-07-28 20:52:06 +0000 | [diff] [blame] | 1341 | FixItHint Hint; |
Douglas Gregor | 5764f61 | 2010-05-08 23:05:03 +0000 | [diff] [blame] | 1342 | if (!ShouldDiagnoseUnusedDecl(D)) |
1343 | return; | ||||
1344 | |||||
Anna Zaks | d5612a2 | 2011-07-28 20:52:06 +0000 | [diff] [blame] | 1345 | GenerateFixForUnusedDecl(D, Context, Hint); |
1346 | |||||
Chris Lattner | 57ad378 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 1347 | unsigned DiagID; |
Douglas Gregor | 5764f61 | 2010-05-08 23:05:03 +0000 | [diff] [blame] | 1348 | if (isa<VarDecl>(D) && cast<VarDecl>(D)->isExceptionVariable()) |
Chris Lattner | 57ad378 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 1349 | DiagID = diag::warn_unused_exception_param; |
1350 | else if (isa<LabelDecl>(D)) | ||||
1351 | DiagID = diag::warn_unused_label; | ||||
Douglas Gregor | 5764f61 | 2010-05-08 23:05:03 +0000 | [diff] [blame] | 1352 | else |
Chris Lattner | 57ad378 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 1353 | DiagID = diag::warn_unused_variable; |
1354 | |||||
Anna Zaks | d5612a2 | 2011-07-28 20:52:06 +0000 | [diff] [blame] | 1355 | Diag(D->getLocation(), DiagID) << D->getDeclName() << Hint; |
Douglas Gregor | 5764f61 | 2010-05-08 23:05:03 +0000 | [diff] [blame] | 1356 | } |
1357 | |||||
Chris Lattner | 337e550 | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1358 | static void CheckPoppedLabel(LabelDecl *L, Sema &S) { |
1359 | // Verify that we have no forward references left. If so, there was a goto | ||||
1360 | // or address of a label taken, but no definition of it. Label fwd | ||||
1361 | // definitions are indicated with a null substmt. | ||||
1362 | if (L->getStmt() == 0) | ||||
1363 | S.Diag(L->getLocation(), diag::err_undeclared_label_use) <<L->getDeclName(); | ||||
1364 | } | ||||
1365 | |||||
Steve Naroff | b216c88 | 2007-10-09 22:01:59 +0000 | [diff] [blame] | 1366 | void Sema::ActOnPopScope(SourceLocation Loc, Scope *S) { |
Chris Lattner | 31e0572 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 1367 | if (S->decl_empty()) return; |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 1368 | assert((S->getFlags() & (Scope::DeclScope | Scope::TemplateParamScope)) && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1369 | "Scope shouldn't contain decls!"); |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 1370 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1371 | for (Scope::decl_iterator I = S->decl_begin(), E = S->decl_end(); |
1372 | I != E; ++I) { | ||||
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1373 | Decl *TmpD = (*I); |
Steve Naroff | c752d04 | 2007-09-13 18:10:37 +0000 | [diff] [blame] | 1374 | assert(TmpD && "This decl didn't get pushed??"); |
Argyrios Kyrtzidis | 7643536 | 2008-06-10 01:32:09 +0000 | [diff] [blame] | 1375 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1376 | assert(isa<NamedDecl>(TmpD) && "Decl isn't NamedDecl?"); |
1377 | NamedDecl *D = cast<NamedDecl>(TmpD); | ||||
Argyrios Kyrtzidis | 7643536 | 2008-06-10 01:32:09 +0000 | [diff] [blame] | 1378 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1379 | if (!D->getDeclName()) continue; |
Chris Lattner | 7f925cc | 2008-04-11 07:00:53 +0000 | [diff] [blame] | 1380 | |
Douglas Gregor | b5352cf | 2009-10-08 21:35:42 +0000 | [diff] [blame] | 1381 | // Diagnose unused variables in this scope. |
Matt Beaumont-Gay | 59d8ccb | 2013-03-28 21:46:45 +0000 | [diff] [blame] | 1382 | if (!S->hasUnrecoverableErrorOccurred()) |
Douglas Gregor | 5764f61 | 2010-05-08 23:05:03 +0000 | [diff] [blame] | 1383 | DiagnoseUnusedDecl(D); |
1384 | |||||
Chris Lattner | 337e550 | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1385 | // If this was a forward reference to a label, verify it was defined. |
1386 | if (LabelDecl *LD = dyn_cast<LabelDecl>(D)) | ||||
1387 | CheckPoppedLabel(LD, *this); | ||||
1388 | |||||
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1389 | // Remove this name from our lexical scope. |
1390 | IdResolver.RemoveDecl(D); | ||||
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1391 | } |
1392 | } | ||||
1393 | |||||
James Molloy | 16f1f71 | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 1394 | void Sema::ActOnStartFunctionDeclarator() { |
1395 | ++InFunctionDeclarator; | ||||
1396 | } | ||||
1397 | |||||
1398 | void Sema::ActOnEndFunctionDeclarator() { | ||||
1399 | assert(InFunctionDeclarator); | ||||
1400 | --InFunctionDeclarator; | ||||
1401 | } | ||||
1402 | |||||
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 1403 | /// \brief Look for an Objective-C class in the translation unit. |
1404 | /// | ||||
1405 | /// \param Id The name of the Objective-C class we're looking for. If | ||||
1406 | /// typo-correction fixes this name, the Id will be updated | ||||
1407 | /// to the fixed name. | ||||
1408 | /// | ||||
1409 | /// \param IdLoc The location of the name in the translation unit. | ||||
1410 | /// | ||||
James Dennett | 16ae9de | 2012-06-22 10:16:05 +0000 | [diff] [blame] | 1411 | /// \param DoTypoCorrection If true, this routine will attempt typo correction |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 1412 | /// if there is no class with the given name. |
1413 | /// | ||||
1414 | /// \returns The declaration of the named Objective-C class, or NULL if the | ||||
1415 | /// class could not be found. | ||||
1416 | ObjCInterfaceDecl *Sema::getObjCInterfaceDecl(IdentifierInfo *&Id, | ||||
1417 | SourceLocation IdLoc, | ||||
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1418 | bool DoTypoCorrection) { |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 1419 | // The third "scope" argument is 0 since we aren't enabling lazy built-in |
1420 | // creation from this context. | ||||
1421 | NamedDecl *IDecl = LookupSingleName(TUScope, Id, IdLoc, LookupOrdinaryName); | ||||
1422 | |||||
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1423 | if (!IDecl && DoTypoCorrection) { |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 1424 | // Perform typo correction at the given location, but only if we |
1425 | // find an Objective-C class name. | ||||
Kaelyn Uhrain | 43e875d | 2012-01-18 21:41:41 +0000 | [diff] [blame] | 1426 | DeclFilterCCC<ObjCInterfaceDecl> Validator; |
1427 | if (TypoCorrection C = CorrectTypo(DeclarationNameInfo(Id, IdLoc), | ||||
1428 | LookupOrdinaryName, TUScope, NULL, | ||||
Kaelyn Uhrain | 16e46dd | 2012-01-31 23:49:25 +0000 | [diff] [blame] | 1429 | Validator)) { |
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 1430 | diagnoseTypo(C, PDiag(diag::err_undef_interface_suggest) << Id); |
Kaelyn Uhrain | 43e875d | 2012-01-18 21:41:41 +0000 | [diff] [blame] | 1431 | IDecl = C.getCorrectionDeclAs<ObjCInterfaceDecl>(); |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 1432 | Id = IDecl->getIdentifier(); |
1433 | } | ||||
1434 | } | ||||
Fariborz Jahanian | 3306f96 | 2012-01-12 00:18:35 +0000 | [diff] [blame] | 1435 | ObjCInterfaceDecl *Def = dyn_cast_or_null<ObjCInterfaceDecl>(IDecl); |
1436 | // This routine must always return a class definition, if any. | ||||
1437 | if (Def && Def->getDefinition()) | ||||
1438 | Def = Def->getDefinition(); | ||||
1439 | return Def; | ||||
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 1440 | } |
1441 | |||||
Douglas Gregor | 1a0d31a | 2009-01-12 18:45:55 +0000 | [diff] [blame] | 1442 | /// getNonFieldDeclScope - Retrieves the innermost scope, starting |
1443 | /// from S, where a non-field would be declared. This routine copes | ||||
1444 | /// with the difference between C and C++ scoping rules in structs and | ||||
1445 | /// unions. For example, the following code is well-formed in C but | ||||
1446 | /// ill-formed in C++: | ||||
1447 | /// @code | ||||
1448 | /// struct S6 { | ||||
1449 | /// enum { BAR } e; | ||||
1450 | /// }; | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1451 | /// |
Douglas Gregor | 1a0d31a | 2009-01-12 18:45:55 +0000 | [diff] [blame] | 1452 | /// void test_S6() { |
1453 | /// struct S6 a; | ||||
1454 | /// a.e = BAR; | ||||
1455 | /// } | ||||
1456 | /// @endcode | ||||
1457 | /// For the declaration of BAR, this routine will return a different | ||||
1458 | /// scope. The scope S will be the scope of the unnamed enumeration | ||||
1459 | /// within S6. In C++, this routine will return the scope associated | ||||
1460 | /// with S6, because the enumeration's scope is a transparent | ||||
1461 | /// context but structures can contain non-field names. In C, this | ||||
1462 | /// routine will return the translation unit scope, since the | ||||
1463 | /// enumeration's scope is a transparent context and structures cannot | ||||
1464 | /// contain non-field names. | ||||
1465 | Scope *Sema::getNonFieldDeclScope(Scope *S) { | ||||
1466 | while (((S->getFlags() & Scope::DeclScope) == 0) || | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1467 | (S->getEntity() && |
Douglas Gregor | 1a0d31a | 2009-01-12 18:45:55 +0000 | [diff] [blame] | 1468 | ((DeclContext *)S->getEntity())->isTransparentContext()) || |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1469 | (S->isClassScope() && !getLangOpts().CPlusPlus)) |
Douglas Gregor | 1a0d31a | 2009-01-12 18:45:55 +0000 | [diff] [blame] | 1470 | S = S->getParent(); |
1471 | return S; | ||||
1472 | } | ||||
1473 | |||||
Fariborz Jahanian | f799213 | 2013-01-04 18:45:40 +0000 | [diff] [blame] | 1474 | /// \brief Looks up the declaration of "struct objc_super" and |
1475 | /// saves it for later use in building builtin declaration of | ||||
1476 | /// objc_msgSendSuper and objc_msgSendSuper_stret. If no such | ||||
1477 | /// pre-existing declaration exists no action takes place. | ||||
1478 | static void LookupPredefedObjCSuperType(Sema &ThisSema, Scope *S, | ||||
1479 | IdentifierInfo *II) { | ||||
1480 | if (!II->isStr("objc_msgSendSuper")) | ||||
1481 | return; | ||||
1482 | ASTContext &Context = ThisSema.Context; | ||||
1483 | |||||
1484 | LookupResult Result(ThisSema, &Context.Idents.get("objc_super"), | ||||
1485 | SourceLocation(), Sema::LookupTagName); | ||||
1486 | ThisSema.LookupName(Result, S); | ||||
1487 | if (Result.getResultKind() == LookupResult::Found) | ||||
1488 | if (const TagDecl *TD = Result.getAsSingle<TagDecl>()) | ||||
1489 | Context.setObjCSuperType(Context.getTagDeclType(TD)); | ||||
1490 | } | ||||
1491 | |||||
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 1492 | /// LazilyCreateBuiltin - The specified Builtin-ID was first used at |
1493 | /// file scope. lazily create a decl for it. ForRedeclaration is true | ||||
1494 | /// if we're creating this built-in in anticipation of redeclaring the | ||||
1495 | /// built-in. | ||||
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1496 | NamedDecl *Sema::LazilyCreateBuiltin(IdentifierInfo *II, unsigned bid, |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 1497 | Scope *S, bool ForRedeclaration, |
1498 | SourceLocation Loc) { | ||||
Fariborz Jahanian | f799213 | 2013-01-04 18:45:40 +0000 | [diff] [blame] | 1499 | LookupPredefedObjCSuperType(*this, S, II); |
1500 | |||||
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1501 | Builtin::ID BID = (Builtin::ID)bid; |
1502 | |||||
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 1503 | ASTContext::GetBuiltinTypeError Error; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1504 | QualType R = Context.GetBuiltinType(BID, Error); |
Douglas Gregor | 370ab3f | 2009-02-14 01:52:53 +0000 | [diff] [blame] | 1505 | switch (Error) { |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 1506 | case ASTContext::GE_None: |
Douglas Gregor | 370ab3f | 2009-02-14 01:52:53 +0000 | [diff] [blame] | 1507 | // Okay |
1508 | break; | ||||
1509 | |||||
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 1510 | case ASTContext::GE_Missing_stdio: |
Douglas Gregor | 370ab3f | 2009-02-14 01:52:53 +0000 | [diff] [blame] | 1511 | if (ForRedeclaration) |
Douglas Gregor | 6b9109e | 2011-01-03 09:37:44 +0000 | [diff] [blame] | 1512 | Diag(Loc, diag::warn_implicit_decl_requires_stdio) |
Douglas Gregor | 370ab3f | 2009-02-14 01:52:53 +0000 | [diff] [blame] | 1513 | << Context.BuiltinInfo.GetName(BID); |
1514 | return 0; | ||||
Mike Stump | 782fa30 | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 1515 | |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 1516 | case ASTContext::GE_Missing_setjmp: |
Mike Stump | 782fa30 | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 1517 | if (ForRedeclaration) |
Douglas Gregor | 6b9109e | 2011-01-03 09:37:44 +0000 | [diff] [blame] | 1518 | Diag(Loc, diag::warn_implicit_decl_requires_setjmp) |
Mike Stump | 782fa30 | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 1519 | << Context.BuiltinInfo.GetName(BID); |
1520 | return 0; | ||||
Rafael Espindola | e2d4f4e | 2011-11-13 21:51:09 +0000 | [diff] [blame] | 1521 | |
1522 | case ASTContext::GE_Missing_ucontext: | ||||
1523 | if (ForRedeclaration) | ||||
1524 | Diag(Loc, diag::warn_implicit_decl_requires_ucontext) | ||||
1525 | << Context.BuiltinInfo.GetName(BID); | ||||
1526 | return 0; | ||||
Douglas Gregor | 370ab3f | 2009-02-14 01:52:53 +0000 | [diff] [blame] | 1527 | } |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 1528 | |
1529 | if (!ForRedeclaration && Context.BuiltinInfo.isPredefinedLibFunction(BID)) { | ||||
1530 | Diag(Loc, diag::ext_implicit_lib_function_decl) | ||||
1531 | << Context.BuiltinInfo.GetName(BID) | ||||
1532 | << R; | ||||
Douglas Gregor | b1152d8 | 2009-02-16 21:58:21 +0000 | [diff] [blame] | 1533 | if (Context.BuiltinInfo.getHeaderName(BID) && |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 1534 | Diags.getDiagnosticLevel(diag::ext_implicit_lib_function_decl, Loc) |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 1535 | != DiagnosticsEngine::Ignored) |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 1536 | Diag(Loc, diag::note_please_include_header) |
1537 | << Context.BuiltinInfo.getHeaderName(BID) | ||||
1538 | << Context.BuiltinInfo.GetName(BID); | ||||
1539 | } | ||||
1540 | |||||
Argyrios Kyrtzidis | ff898cd | 2008-04-17 14:47:13 +0000 | [diff] [blame] | 1541 | FunctionDecl *New = FunctionDecl::Create(Context, |
1542 | Context.getTranslationUnitDecl(), | ||||
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1543 | Loc, Loc, II, R, /*TInfo=*/0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 1544 | SC_Extern, |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 1545 | false, |
Douglas Gregor | 2224f84 | 2009-02-25 16:33:18 +0000 | [diff] [blame] | 1546 | /*hasPrototype=*/true); |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 1547 | New->setImplicit(); |
1548 | |||||
Chris Lattner | 95e2c71 | 2008-05-05 22:18:14 +0000 | [diff] [blame] | 1549 | // Create Decl objects for each parameter, adding them to the |
1550 | // FunctionDecl. | ||||
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1551 | if (const FunctionProtoType *FT = dyn_cast<FunctionProtoType>(R)) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1552 | SmallVector<ParmVarDecl*, 16> Params; |
John McCall | fb44de9 | 2011-05-01 22:35:37 +0000 | [diff] [blame] | 1553 | for (unsigned i = 0, e = FT->getNumArgs(); i != e; ++i) { |
1554 | ParmVarDecl *parm = | ||||
1555 | ParmVarDecl::Create(Context, New, SourceLocation(), | ||||
1556 | SourceLocation(), 0, | ||||
1557 | FT->getArgType(i), /*TInfo=*/0, | ||||
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 1558 | SC_None, 0); |
John McCall | fb44de9 | 2011-05-01 22:35:37 +0000 | [diff] [blame] | 1559 | parm->setScopeInfo(0, i); |
1560 | Params.push_back(parm); | ||||
1561 | } | ||||
David Blaikie | 4278c65 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 1562 | New->setParams(Params); |
Chris Lattner | 95e2c71 | 2008-05-05 22:18:14 +0000 | [diff] [blame] | 1563 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1564 | |
1565 | AddKnownFunctionAttributes(New); | ||||
1566 | |||||
Chris Lattner | 7f925cc | 2008-04-11 07:00:53 +0000 | [diff] [blame] | 1567 | // TUScope is the translation-unit scope to insert this function into. |
Douglas Gregor | a8cc8ce | 2009-01-09 18:51:29 +0000 | [diff] [blame] | 1568 | // FIXME: This is hideous. We need to teach PushOnScopeChains to |
1569 | // relate Scopes to DeclContexts, and probably eliminate CurContext | ||||
1570 | // entirely, but we're not there yet. | ||||
1571 | DeclContext *SavedContext = CurContext; | ||||
1572 | CurContext = Context.getTranslationUnitDecl(); | ||||
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 1573 | PushOnScopeChains(New, TUScope); |
Douglas Gregor | a8cc8ce | 2009-01-09 18:51:29 +0000 | [diff] [blame] | 1574 | CurContext = SavedContext; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1575 | return New; |
1576 | } | ||||
1577 | |||||
Douglas Gregor | 7dc80e1 | 2013-01-09 00:47:56 +0000 | [diff] [blame] | 1578 | /// \brief Filter out any previous declarations that the given declaration |
1579 | /// should not consider because they are not permitted to conflict, e.g., | ||||
1580 | /// because they come from hidden sub-modules and do not refer to the same | ||||
1581 | /// entity. | ||||
1582 | static void filterNonConflictingPreviousDecls(ASTContext &context, | ||||
1583 | NamedDecl *decl, | ||||
1584 | LookupResult &previous){ | ||||
1585 | // This is only interesting when modules are enabled. | ||||
1586 | if (!context.getLangOpts().Modules) | ||||
1587 | return; | ||||
1588 | |||||
1589 | // Empty sets are uninteresting. | ||||
1590 | if (previous.empty()) | ||||
1591 | return; | ||||
1592 | |||||
Douglas Gregor | 7dc80e1 | 2013-01-09 00:47:56 +0000 | [diff] [blame] | 1593 | LookupResult::Filter filter = previous.makeFilter(); |
1594 | while (filter.hasNext()) { | ||||
1595 | NamedDecl *old = filter.next(); | ||||
1596 | |||||
1597 | // Non-hidden declarations are never ignored. | ||||
1598 | if (!old->isHidden()) | ||||
1599 | continue; | ||||
1600 | |||||
Rafael Espindola | 181e3ec | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 1601 | if (!old->isExternallyVisible()) |
Douglas Gregor | 7dc80e1 | 2013-01-09 00:47:56 +0000 | [diff] [blame] | 1602 | filter.erase(); |
1603 | } | ||||
1604 | |||||
1605 | filter.done(); | ||||
1606 | } | ||||
1607 | |||||
Rafael Espindola | 5df37bd | 2011-12-26 22:42:47 +0000 | [diff] [blame] | 1608 | bool Sema::isIncompatibleTypedef(TypeDecl *Old, TypedefNameDecl *New) { |
1609 | QualType OldType; | ||||
1610 | if (TypedefNameDecl *OldTypedef = dyn_cast<TypedefNameDecl>(Old)) | ||||
1611 | OldType = OldTypedef->getUnderlyingType(); | ||||
1612 | else | ||||
1613 | OldType = Context.getTypeDeclType(Old); | ||||
1614 | QualType NewType = New->getUnderlyingType(); | ||||
1615 | |||||
Douglas Gregor | ec3bd72 | 2012-01-11 22:33:48 +0000 | [diff] [blame] | 1616 | if (NewType->isVariablyModifiedType()) { |
1617 | // Must not redefine a typedef with a variably-modified type. | ||||
1618 | int Kind = isa<TypeAliasDecl>(Old) ? 1 : 0; | ||||
1619 | Diag(New->getLocation(), diag::err_redefinition_variably_modified_typedef) | ||||
1620 | << Kind << NewType; | ||||
1621 | if (Old->getLocation().isValid()) | ||||
1622 | Diag(Old->getLocation(), diag::note_previous_definition); | ||||
1623 | New->setInvalidDecl(); | ||||
1624 | return true; | ||||
1625 | } | ||||
1626 | |||||
Rafael Espindola | 5df37bd | 2011-12-26 22:42:47 +0000 | [diff] [blame] | 1627 | if (OldType != NewType && |
1628 | !OldType->isDependentType() && | ||||
1629 | !NewType->isDependentType() && | ||||
Douglas Gregor | ec3bd72 | 2012-01-11 22:33:48 +0000 | [diff] [blame] | 1630 | !Context.hasSameType(OldType, NewType)) { |
Rafael Espindola | 5df37bd | 2011-12-26 22:42:47 +0000 | [diff] [blame] | 1631 | int Kind = isa<TypeAliasDecl>(Old) ? 1 : 0; |
1632 | Diag(New->getLocation(), diag::err_redefinition_different_typedef) | ||||
1633 | << Kind << NewType << OldType; | ||||
1634 | if (Old->getLocation().isValid()) | ||||
1635 | Diag(Old->getLocation(), diag::note_previous_definition); | ||||
1636 | New->setInvalidDecl(); | ||||
1637 | return true; | ||||
1638 | } | ||||
1639 | return false; | ||||
1640 | } | ||||
1641 | |||||
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1642 | /// MergeTypedefNameDecl - We just parsed a typedef 'New' which has the |
Douglas Gregor | cda9c67 | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 1643 | /// same name and scope as a previous declaration 'Old'. Figure out |
1644 | /// how to resolve this situation, merging decls or emitting | ||||
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 1645 | /// 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] | 1646 | /// |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1647 | void Sema::MergeTypedefNameDecl(TypedefNameDecl *New, LookupResult &OldDecls) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1648 | // If the new decl is known invalid already, don't bother doing any |
1649 | // merging checks. | ||||
1650 | if (New->isInvalidDecl()) return; | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1651 | |
Steve Naroff | 2b255c4 | 2008-09-09 14:32:20 +0000 | [diff] [blame] | 1652 | // Allow multiple definitions for ObjC built-in typedefs. |
1653 | // FIXME: Verify the underlying types are equivalent! | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1654 | if (getLangOpts().ObjC1) { |
Chris Lattner | 2bac0f6 | 2008-11-20 05:41:43 +0000 | [diff] [blame] | 1655 | const IdentifierInfo *TypeID = New->getIdentifier(); |
1656 | switch (TypeID->getLength()) { | ||||
1657 | default: break; | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1658 | case 2: |
Fariborz Jahanian | 0cd00be | 2012-05-14 22:48:56 +0000 | [diff] [blame] | 1659 | { |
1660 | if (!TypeID->isStr("id")) | ||||
1661 | break; | ||||
1662 | QualType T = New->getUnderlyingType(); | ||||
1663 | if (!T->isPointerType()) | ||||
1664 | break; | ||||
1665 | if (!T->isVoidPointerType()) { | ||||
1666 | QualType PT = T->getAs<PointerType>()->getPointeeType(); | ||||
1667 | if (!PT->isStructureType()) | ||||
1668 | break; | ||||
1669 | } | ||||
1670 | Context.setObjCIdRedefinitionType(T); | ||||
1671 | // Install the built-in type for 'id', ignoring the current definition. | ||||
1672 | New->setTypeForDecl(Context.getObjCIdType().getTypePtr()); | ||||
1673 | return; | ||||
1674 | } | ||||
Chris Lattner | 2bac0f6 | 2008-11-20 05:41:43 +0000 | [diff] [blame] | 1675 | case 5: |
1676 | if (!TypeID->isStr("Class")) | ||||
1677 | break; | ||||
Douglas Gregor | 01a4cf1 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 1678 | Context.setObjCClassRedefinitionType(New->getUnderlyingType()); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1679 | // Install the built-in type for 'Class', ignoring the current definition. |
1680 | New->setTypeForDecl(Context.getObjCClassType().getTypePtr()); | ||||
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 1681 | return; |
Chris Lattner | 2bac0f6 | 2008-11-20 05:41:43 +0000 | [diff] [blame] | 1682 | case 3: |
1683 | if (!TypeID->isStr("SEL")) | ||||
1684 | break; | ||||
Douglas Gregor | 01a4cf1 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 1685 | Context.setObjCSelRedefinitionType(New->getUnderlyingType()); |
Fariborz Jahanian | 13dcd00 | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 1686 | // Install the built-in type for 'SEL', ignoring the current definition. |
1687 | New->setTypeForDecl(Context.getObjCSelType().getTypePtr()); | ||||
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 1688 | return; |
Steve Naroff | 2b255c4 | 2008-09-09 14:32:20 +0000 | [diff] [blame] | 1689 | } |
1690 | // Fall through - the typedef name was not a builtin type. | ||||
1691 | } | ||||
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1692 | |
Douglas Gregor | 6697312 | 2009-01-28 17:15:10 +0000 | [diff] [blame] | 1693 | // Verify the old decl was also a type. |
John McCall | 5126fd0 | 2009-12-30 00:31:22 +0000 | [diff] [blame] | 1694 | TypeDecl *Old = OldDecls.getAsSingle<TypeDecl>(); |
1695 | if (!Old) { | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1696 | Diag(New->getLocation(), diag::err_redefinition_different_kind) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 1697 | << New->getDeclName(); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1698 | |
1699 | NamedDecl *OldD = OldDecls.getRepresentativeDecl(); | ||||
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 1700 | if (OldD->getLocation().isValid()) |
Fariborz Jahanian | c55a240 | 2009-01-16 19:58:32 +0000 | [diff] [blame] | 1701 | Diag(OldD->getLocation(), diag::note_previous_definition); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1702 | |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 1703 | return New->setInvalidDecl(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1704 | } |
Douglas Gregor | 6697312 | 2009-01-28 17:15:10 +0000 | [diff] [blame] | 1705 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1706 | // If the old declaration is invalid, just give up here. |
1707 | if (Old->isInvalidDecl()) | ||||
1708 | return New->setInvalidDecl(); | ||||
1709 | |||||
Chris Lattner | 99cb997 | 2008-07-25 18:44:27 +0000 | [diff] [blame] | 1710 | // If the typedef types are not identical, reject them in all languages and |
1711 | // with any extensions enabled. | ||||
Rafael Espindola | 5df37bd | 2011-12-26 22:42:47 +0000 | [diff] [blame] | 1712 | if (isIncompatibleTypedef(Old, New)) |
1713 | return; | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1714 | |
Justin Bogner | 2dd68de | 2013-10-08 00:19:09 +0000 | [diff] [blame] | 1715 | // The types match. Link up the redeclaration chain and merge attributes if |
1716 | // the old declaration was a typedef. | ||||
1717 | if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Old)) { | ||||
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1718 | New->setPreviousDeclaration(Typedef); |
Justin Bogner | 2dd68de | 2013-10-08 00:19:09 +0000 | [diff] [blame] | 1719 | mergeDeclAttributes(New, Old); |
1720 | } | ||||
Eli Friedman | 9ec4099 | 2013-07-16 02:07:49 +0000 | [diff] [blame] | 1721 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1722 | if (getLangOpts().MicrosoftExt) |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 1723 | return; |
Eli Friedman | 54ecfce | 2008-06-11 06:20:39 +0000 | [diff] [blame] | 1724 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1725 | if (getLangOpts().CPlusPlus) { |
Douglas Gregor | 93dda72 | 2010-01-11 21:54:40 +0000 | [diff] [blame] | 1726 | // C++ [dcl.typedef]p2: |
1727 | // In a given non-class scope, a typedef specifier can be used to | ||||
1728 | // redefine the name of any type declared in that scope to refer | ||||
1729 | // to the type to which it already refers. | ||||
Chris Lattner | 32b0675 | 2009-04-17 22:04:20 +0000 | [diff] [blame] | 1730 | if (!isa<CXXRecordDecl>(CurContext)) |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 1731 | return; |
Douglas Gregor | 93dda72 | 2010-01-11 21:54:40 +0000 | [diff] [blame] | 1732 | |
1733 | // C++0x [dcl.typedef]p4: | ||||
1734 | // In a given class scope, a typedef specifier can be used to redefine | ||||
1735 | // any class-name declared in that scope that is not also a typedef-name | ||||
1736 | // to refer to the type to which it already refers. | ||||
1737 | // | ||||
1738 | // This wording came in via DR424, which was a correction to the | ||||
1739 | // wording in DR56, which accidentally banned code like: | ||||
1740 | // | ||||
1741 | // struct S { | ||||
1742 | // typedef struct A { } A; | ||||
1743 | // }; | ||||
1744 | // | ||||
1745 | // in the C++03 standard. We implement the C++0x semantics, which | ||||
1746 | // allow the above but disallow | ||||
1747 | // | ||||
1748 | // struct S { | ||||
1749 | // typedef int I; | ||||
1750 | // typedef int I; | ||||
1751 | // }; | ||||
1752 | // | ||||
1753 | // since that was the intent of DR56. | ||||
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1754 | if (!isa<TypedefNameDecl>(Old)) |
Douglas Gregor | 93dda72 | 2010-01-11 21:54:40 +0000 | [diff] [blame] | 1755 | return; |
1756 | |||||
Chris Lattner | 32b0675 | 2009-04-17 22:04:20 +0000 | [diff] [blame] | 1757 | Diag(New->getLocation(), diag::err_redefinition) |
1758 | << New->getDeclName(); | ||||
1759 | Diag(Old->getLocation(), diag::note_previous_definition); | ||||
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 1760 | return New->setInvalidDecl(); |
Daniel Dunbar | 2fe0997 | 2008-09-12 18:10:20 +0000 | [diff] [blame] | 1761 | } |
Eli Friedman | 54ecfce | 2008-06-11 06:20:39 +0000 | [diff] [blame] | 1762 | |
Douglas Gregor | c0004df | 2012-01-11 04:25:01 +0000 | [diff] [blame] | 1763 | // Modules always permit redefinition of typedefs, as does C11. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1764 | if (getLangOpts().Modules || getLangOpts().C11) |
Douglas Gregor | c02d62f | 2012-01-09 15:36:04 +0000 | [diff] [blame] | 1765 | return; |
1766 | |||||
Chris Lattner | 32b0675 | 2009-04-17 22:04:20 +0000 | [diff] [blame] | 1767 | // If we have a redefinition of a typedef in C, emit a warning. This warning |
1768 | // is normally mapped to an error, but can be controlled with | ||||
Eli Friedman | 340a4e5 | 2009-06-04 23:03:07 +0000 | [diff] [blame] | 1769 | // -Wtypedef-redefinition. If either the original or the redefinition is |
1770 | // in a system header, don't emit this for compatibility with GCC. | ||||
Chris Lattner | 6d97e5e | 2010-03-01 20:59:53 +0000 | [diff] [blame] | 1771 | if (getDiagnostics().getSuppressSystemWarnings() && |
Eli Friedman | 340a4e5 | 2009-06-04 23:03:07 +0000 | [diff] [blame] | 1772 | (Context.getSourceManager().isInSystemHeader(Old->getLocation()) || |
1773 | Context.getSourceManager().isInSystemHeader(New->getLocation()))) | ||||
Chris Lattner | d0359af | 2009-04-27 01:46:12 +0000 | [diff] [blame] | 1774 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1775 | |
Chris Lattner | 32b0675 | 2009-04-17 22:04:20 +0000 | [diff] [blame] | 1776 | Diag(New->getLocation(), diag::warn_redefinition_of_typedef) |
1777 | << New->getDeclName(); | ||||
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 1778 | Diag(Old->getLocation(), diag::note_previous_definition); |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 1779 | return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1780 | } |
1781 | |||||
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1782 | /// DeclhasAttr - returns true if decl Declaration already has the target |
1783 | /// attribute. | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1784 | static bool |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1785 | DeclHasAttr(const Decl *D, const Attr *A) { |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1786 | // There can be multiple AvailabilityAttr in a Decl. Make sure we copy |
1787 | // all of them. It is mergeAvailabilityAttr in SemaDeclAttr.cpp that is | ||||
1788 | // responsible for making sure they are consistent. | ||||
1789 | const AvailabilityAttr *AA = dyn_cast<AvailabilityAttr>(A); | ||||
1790 | if (AA) | ||||
1791 | return false; | ||||
1792 | |||||
DeLesley Hutchins | 3ce9fae | 2012-10-12 21:38:12 +0000 | [diff] [blame] | 1793 | // The following thread safety attributes can also be duplicated. |
1794 | switch (A->getKind()) { | ||||
1795 | case attr::ExclusiveLocksRequired: | ||||
1796 | case attr::SharedLocksRequired: | ||||
1797 | case attr::LocksExcluded: | ||||
1798 | case attr::ExclusiveLockFunction: | ||||
1799 | case attr::SharedLockFunction: | ||||
1800 | case attr::UnlockFunction: | ||||
1801 | case attr::ExclusiveTrylockFunction: | ||||
1802 | case attr::SharedTrylockFunction: | ||||
1803 | case attr::GuardedBy: | ||||
1804 | case attr::PtGuardedBy: | ||||
1805 | case attr::AcquiredBefore: | ||||
1806 | case attr::AcquiredAfter: | ||||
1807 | return false; | ||||
DeLesley Hutchins | 6c500b1 | 2012-10-12 21:49:04 +0000 | [diff] [blame] | 1808 | default: |
1809 | ; | ||||
DeLesley Hutchins | 3ce9fae | 2012-10-12 21:38:12 +0000 | [diff] [blame] | 1810 | } |
1811 | |||||
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1812 | const OwnershipAttr *OA = dyn_cast<OwnershipAttr>(A); |
Julien Lerouge | 77f68bb | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 1813 | const AnnotateAttr *Ann = dyn_cast<AnnotateAttr>(A); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1814 | for (Decl::attr_iterator i = D->attr_begin(), e = D->attr_end(); i != e; ++i) |
1815 | if ((*i)->getKind() == A->getKind()) { | ||||
Julien Lerouge | 77f68bb | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 1816 | if (Ann) { |
1817 | if (Ann->getAnnotation() == cast<AnnotateAttr>(*i)->getAnnotation()) | ||||
1818 | return true; | ||||
1819 | continue; | ||||
1820 | } | ||||
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1821 | // FIXME: Don't hardcode this check |
1822 | if (OA && isa<OwnershipAttr>(*i)) | ||||
1823 | return OA->getOwnKind() == cast<OwnershipAttr>(*i)->getOwnKind(); | ||||
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 1824 | return true; |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1825 | } |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 1826 | |
1827 | return false; | ||||
1828 | } | ||||
1829 | |||||
Richard Smith | 671b321 | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 1830 | static bool isAttributeTargetADefinition(Decl *D) { |
1831 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) | ||||
1832 | return VD->isThisDeclarationADefinition(); | ||||
1833 | if (TagDecl *TD = dyn_cast<TagDecl>(D)) | ||||
1834 | return TD->isCompleteDefinition() || TD->isBeingDefined(); | ||||
1835 | return true; | ||||
1836 | } | ||||
1837 | |||||
1838 | /// Merge alignment attributes from \p Old to \p New, taking into account the | ||||
1839 | /// special semantics of C11's _Alignas specifier and C++11's alignas attribute. | ||||
1840 | /// | ||||
1841 | /// \return \c true if any attributes were added to \p New. | ||||
1842 | static bool mergeAlignedAttrs(Sema &S, NamedDecl *New, Decl *Old) { | ||||
1843 | // Look for alignas attributes on Old, and pick out whichever attribute | ||||
1844 | // specifies the strictest alignment requirement. | ||||
1845 | AlignedAttr *OldAlignasAttr = 0; | ||||
1846 | AlignedAttr *OldStrictestAlignAttr = 0; | ||||
1847 | unsigned OldAlign = 0; | ||||
1848 | for (specific_attr_iterator<AlignedAttr> | ||||
1849 | I = Old->specific_attr_begin<AlignedAttr>(), | ||||
1850 | E = Old->specific_attr_end<AlignedAttr>(); I != E; ++I) { | ||||
1851 | // FIXME: We have no way of representing inherited dependent alignments | ||||
1852 | // in a case like: | ||||
1853 | // template<int A, int B> struct alignas(A) X; | ||||
1854 | // template<int A, int B> struct alignas(B) X {}; | ||||
1855 | // For now, we just ignore any alignas attributes which are not on the | ||||
1856 | // definition in such a case. | ||||
1857 | if (I->isAlignmentDependent()) | ||||
1858 | return false; | ||||
1859 | |||||
1860 | if (I->isAlignas()) | ||||
1861 | OldAlignasAttr = *I; | ||||
1862 | |||||
1863 | unsigned Align = I->getAlignment(S.Context); | ||||
1864 | if (Align > OldAlign) { | ||||
1865 | OldAlign = Align; | ||||
1866 | OldStrictestAlignAttr = *I; | ||||
1867 | } | ||||
1868 | } | ||||
1869 | |||||
1870 | // Look for alignas attributes on New. | ||||
1871 | AlignedAttr *NewAlignasAttr = 0; | ||||
1872 | unsigned NewAlign = 0; | ||||
1873 | for (specific_attr_iterator<AlignedAttr> | ||||
1874 | I = New->specific_attr_begin<AlignedAttr>(), | ||||
1875 | E = New->specific_attr_end<AlignedAttr>(); I != E; ++I) { | ||||
1876 | if (I->isAlignmentDependent()) | ||||
1877 | return false; | ||||
1878 | |||||
1879 | if (I->isAlignas()) | ||||
1880 | NewAlignasAttr = *I; | ||||
1881 | |||||
1882 | unsigned Align = I->getAlignment(S.Context); | ||||
1883 | if (Align > NewAlign) | ||||
1884 | NewAlign = Align; | ||||
1885 | } | ||||
1886 | |||||
1887 | if (OldAlignasAttr && NewAlignasAttr && OldAlign != NewAlign) { | ||||
1888 | // Both declarations have 'alignas' attributes. We require them to match. | ||||
1889 | // C++11 [dcl.align]p6 and C11 6.7.5/7 both come close to saying this, but | ||||
1890 | // fall short. (If two declarations both have alignas, they must both match | ||||
1891 | // every definition, and so must match each other if there is a definition.) | ||||
1892 | |||||
1893 | // If either declaration only contains 'alignas(0)' specifiers, then it | ||||
1894 | // specifies the natural alignment for the type. | ||||
1895 | if (OldAlign == 0 || NewAlign == 0) { | ||||
1896 | QualType Ty; | ||||
1897 | if (ValueDecl *VD = dyn_cast<ValueDecl>(New)) | ||||
1898 | Ty = VD->getType(); | ||||
1899 | else | ||||
1900 | Ty = S.Context.getTagDeclType(cast<TagDecl>(New)); | ||||
1901 | |||||
1902 | if (OldAlign == 0) | ||||
1903 | OldAlign = S.Context.getTypeAlign(Ty); | ||||
1904 | if (NewAlign == 0) | ||||
1905 | NewAlign = S.Context.getTypeAlign(Ty); | ||||
1906 | } | ||||
1907 | |||||
1908 | if (OldAlign != NewAlign) { | ||||
1909 | S.Diag(NewAlignasAttr->getLocation(), diag::err_alignas_mismatch) | ||||
1910 | << (unsigned)S.Context.toCharUnitsFromBits(OldAlign).getQuantity() | ||||
1911 | << (unsigned)S.Context.toCharUnitsFromBits(NewAlign).getQuantity(); | ||||
1912 | S.Diag(OldAlignasAttr->getLocation(), diag::note_previous_declaration); | ||||
1913 | } | ||||
1914 | } | ||||
1915 | |||||
1916 | if (OldAlignasAttr && !NewAlignasAttr && isAttributeTargetADefinition(New)) { | ||||
1917 | // C++11 [dcl.align]p6: | ||||
1918 | // if any declaration of an entity has an alignment-specifier, | ||||
1919 | // every defining declaration of that entity shall specify an | ||||
1920 | // equivalent alignment. | ||||
1921 | // C11 6.7.5/7: | ||||
1922 | // If the definition of an object does not have an alignment | ||||
1923 | // specifier, any other declaration of that object shall also | ||||
1924 | // have no alignment specifier. | ||||
1925 | S.Diag(New->getLocation(), diag::err_alignas_missing_on_definition) | ||||
1926 | << OldAlignasAttr->isC11(); | ||||
1927 | S.Diag(OldAlignasAttr->getLocation(), diag::note_alignas_on_declaration) | ||||
1928 | << OldAlignasAttr->isC11(); | ||||
1929 | } | ||||
1930 | |||||
1931 | bool AnyAdded = false; | ||||
1932 | |||||
1933 | // Ensure we have an attribute representing the strictest alignment. | ||||
1934 | if (OldAlign > NewAlign) { | ||||
1935 | AlignedAttr *Clone = OldStrictestAlignAttr->clone(S.Context); | ||||
1936 | Clone->setInherited(true); | ||||
1937 | New->addAttr(Clone); | ||||
1938 | AnyAdded = true; | ||||
1939 | } | ||||
1940 | |||||
1941 | // Ensure we have an alignas attribute if the old declaration had one. | ||||
1942 | if (OldAlignasAttr && !NewAlignasAttr && | ||||
1943 | !(AnyAdded && OldStrictestAlignAttr->isAlignas())) { | ||||
1944 | AlignedAttr *Clone = OldAlignasAttr->clone(S.Context); | ||||
1945 | Clone->setInherited(true); | ||||
1946 | New->addAttr(Clone); | ||||
1947 | AnyAdded = true; | ||||
1948 | } | ||||
1949 | |||||
1950 | return AnyAdded; | ||||
1951 | } | ||||
1952 | |||||
1953 | static bool mergeDeclAttribute(Sema &S, NamedDecl *D, InheritableAttr *Attr, | ||||
1954 | bool Override) { | ||||
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1955 | InheritableAttr *NewAttr = NULL; |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1956 | unsigned AttrSpellingListIndex = Attr->getSpellingListIndex(); |
Rafael Espindola | 838dc59 | 2013-01-12 06:42:30 +0000 | [diff] [blame] | 1957 | if (AvailabilityAttr *AA = dyn_cast<AvailabilityAttr>(Attr)) |
Richard Smith | 671b321 | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 1958 | NewAttr = S.mergeAvailabilityAttr(D, AA->getRange(), AA->getPlatform(), |
1959 | AA->getIntroduced(), AA->getDeprecated(), | ||||
1960 | AA->getObsoleted(), AA->getUnavailable(), | ||||
1961 | AA->getMessage(), Override, | ||||
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 1962 | AttrSpellingListIndex); |
Richard Smith | 671b321 | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 1963 | else if (VisibilityAttr *VA = dyn_cast<VisibilityAttr>(Attr)) |
1964 | NewAttr = S.mergeVisibilityAttr(D, VA->getRange(), VA->getVisibility(), | ||||
1965 | AttrSpellingListIndex); | ||||
1966 | else if (TypeVisibilityAttr *VA = dyn_cast<TypeVisibilityAttr>(Attr)) | ||||
1967 | NewAttr = S.mergeTypeVisibilityAttr(D, VA->getRange(), VA->getVisibility(), | ||||
1968 | AttrSpellingListIndex); | ||||
Rafael Espindola | 838dc59 | 2013-01-12 06:42:30 +0000 | [diff] [blame] | 1969 | else if (DLLImportAttr *ImportA = dyn_cast<DLLImportAttr>(Attr)) |
Richard Smith | 671b321 | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 1970 | NewAttr = S.mergeDLLImportAttr(D, ImportA->getRange(), |
1971 | AttrSpellingListIndex); | ||||
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1972 | else if (DLLExportAttr *ExportA = dyn_cast<DLLExportAttr>(Attr)) |
Richard Smith | 671b321 | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 1973 | NewAttr = S.mergeDLLExportAttr(D, ExportA->getRange(), |
1974 | AttrSpellingListIndex); | ||||
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1975 | else if (FormatAttr *FA = dyn_cast<FormatAttr>(Attr)) |
Richard Smith | 671b321 | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 1976 | NewAttr = S.mergeFormatAttr(D, FA->getRange(), FA->getType(), |
1977 | FA->getFormatIdx(), FA->getFirstArg(), | ||||
1978 | AttrSpellingListIndex); | ||||
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1979 | else if (SectionAttr *SA = dyn_cast<SectionAttr>(Attr)) |
Richard Smith | 671b321 | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 1980 | NewAttr = S.mergeSectionAttr(D, SA->getRange(), SA->getName(), |
1981 | AttrSpellingListIndex); | ||||
1982 | else if (isa<AlignedAttr>(Attr)) | ||||
1983 | // AlignedAttrs are handled separately, because we need to handle all | ||||
1984 | // such attributes on a declaration at the same time. | ||||
1985 | NewAttr = 0; | ||||
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1986 | else if (!DeclHasAttr(D, Attr)) |
Richard Smith | 671b321 | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 1987 | NewAttr = cast<InheritableAttr>(Attr->clone(S.Context)); |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1988 | |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1989 | if (NewAttr) { |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1990 | NewAttr->setInherited(true); |
1991 | D->addAttr(NewAttr); | ||||
1992 | return true; | ||||
1993 | } | ||||
1994 | |||||
1995 | return false; | ||||
1996 | } | ||||
1997 | |||||
Rafael Espindola | 4b044c6 | 2012-07-15 01:05:36 +0000 | [diff] [blame] | 1998 | static const Decl *getDefinition(const Decl *D) { |
1999 | if (const TagDecl *TD = dyn_cast<TagDecl>(D)) | ||||
Rafael Espindola | 3f66406 | 2012-05-18 01:47:00 +0000 | [diff] [blame] | 2000 | return TD->getDefinition(); |
Rafael Espindola | 4b044c6 | 2012-07-15 01:05:36 +0000 | [diff] [blame] | 2001 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) |
Rafael Espindola | 3f66406 | 2012-05-18 01:47:00 +0000 | [diff] [blame] | 2002 | return VD->getDefinition(); |
Rafael Espindola | 4b044c6 | 2012-07-15 01:05:36 +0000 | [diff] [blame] | 2003 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Rafael Espindola | 3f66406 | 2012-05-18 01:47:00 +0000 | [diff] [blame] | 2004 | const FunctionDecl* Def; |
2005 | if (FD->hasBody(Def)) | ||||
2006 | return Def; | ||||
2007 | } | ||||
2008 | return NULL; | ||||
2009 | } | ||||
2010 | |||||
Rafael Espindola | d320ffc | 2012-07-15 01:33:40 +0000 | [diff] [blame] | 2011 | static bool hasAttribute(const Decl *D, attr::Kind Kind) { |
2012 | for (Decl::attr_iterator I = D->attr_begin(), E = D->attr_end(); | ||||
2013 | I != E; ++I) { | ||||
2014 | Attr *Attribute = *I; | ||||
2015 | if (Attribute->getKind() == Kind) | ||||
2016 | return true; | ||||
2017 | } | ||||
2018 | return false; | ||||
2019 | } | ||||
2020 | |||||
2021 | /// checkNewAttributesAfterDef - If we already have a definition, check that | ||||
2022 | /// there are no new attributes in this declaration. | ||||
2023 | static void checkNewAttributesAfterDef(Sema &S, Decl *New, const Decl *Old) { | ||||
2024 | if (!New->hasAttrs()) | ||||
2025 | return; | ||||
2026 | |||||
2027 | const Decl *Def = getDefinition(Old); | ||||
2028 | if (!Def || Def == New) | ||||
2029 | return; | ||||
2030 | |||||
2031 | AttrVec &NewAttributes = New->getAttrs(); | ||||
2032 | for (unsigned I = 0, E = NewAttributes.size(); I != E;) { | ||||
2033 | const Attr *NewAttribute = NewAttributes[I]; | ||||
2034 | if (hasAttribute(Def, NewAttribute->getKind())) { | ||||
2035 | ++I; | ||||
2036 | continue; // regular attr merging will take care of validating this. | ||||
2037 | } | ||||
Richard Smith | 671b321 | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 2038 | |
Richard Smith | 7586a6e | 2013-01-30 05:45:05 +0000 | [diff] [blame] | 2039 | if (isa<C11NoReturnAttr>(NewAttribute)) { |
Richard Smith | 671b321 | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 2040 | // 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] | 2041 | ++I; |
2042 | continue; | ||||
Richard Smith | 671b321 | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 2043 | } else if (const AlignedAttr *AA = dyn_cast<AlignedAttr>(NewAttribute)) { |
2044 | if (AA->isAlignas()) { | ||||
2045 | // C++11 [dcl.align]p6: | ||||
2046 | // if any declaration of an entity has an alignment-specifier, | ||||
2047 | // every defining declaration of that entity shall specify an | ||||
2048 | // equivalent alignment. | ||||
2049 | // C11 6.7.5/7: | ||||
2050 | // If the definition of an object does not have an alignment | ||||
2051 | // specifier, any other declaration of that object shall also | ||||
2052 | // have no alignment specifier. | ||||
2053 | S.Diag(Def->getLocation(), diag::err_alignas_missing_on_definition) | ||||
2054 | << AA->isC11(); | ||||
2055 | S.Diag(NewAttribute->getLocation(), diag::note_alignas_on_declaration) | ||||
2056 | << AA->isC11(); | ||||
2057 | NewAttributes.erase(NewAttributes.begin() + I); | ||||
2058 | --E; | ||||
2059 | continue; | ||||
2060 | } | ||||
Richard Smith | 7586a6e | 2013-01-30 05:45:05 +0000 | [diff] [blame] | 2061 | } |
Richard Smith | 671b321 | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 2062 | |
Rafael Espindola | d320ffc | 2012-07-15 01:33:40 +0000 | [diff] [blame] | 2063 | S.Diag(NewAttribute->getLocation(), |
2064 | diag::warn_attribute_precede_definition); | ||||
2065 | S.Diag(Def->getLocation(), diag::note_previous_definition); | ||||
2066 | NewAttributes.erase(NewAttributes.begin() + I); | ||||
2067 | --E; | ||||
2068 | } | ||||
2069 | } | ||||
2070 | |||||
John McCall | eca5d22 | 2011-03-02 04:00:57 +0000 | [diff] [blame] | 2071 | /// mergeDeclAttributes - Copy attributes from the Old decl to the New one. |
Rafael Espindola | 51be6e3 | 2013-01-08 22:04:34 +0000 | [diff] [blame] | 2072 | void Sema::mergeDeclAttributes(NamedDecl *New, Decl *Old, |
Douglas Gregor | f4d918f | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2073 | AvailabilityMergeKind AMK) { |
Richard Smith | 3a2b7a1 | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 2074 | if (!Old->hasAttrs() && !New->hasAttrs()) |
2075 | return; | ||||
2076 | |||||
Rafael Espindola | 3f66406 | 2012-05-18 01:47:00 +0000 | [diff] [blame] | 2077 | // attributes declared post-definition are currently ignored |
Rafael Espindola | d320ffc | 2012-07-15 01:33:40 +0000 | [diff] [blame] | 2078 | checkNewAttributesAfterDef(*this, New, Old); |
Rafael Espindola | 3f66406 | 2012-05-18 01:47:00 +0000 | [diff] [blame] | 2079 | |
Douglas Gregor | 27c6da2 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 2080 | if (!Old->hasAttrs()) |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2081 | return; |
John McCall | eca5d22 | 2011-03-02 04:00:57 +0000 | [diff] [blame] | 2082 | |
Douglas Gregor | 27c6da2 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 2083 | bool foundAny = New->hasAttrs(); |
John McCall | eca5d22 | 2011-03-02 04:00:57 +0000 | [diff] [blame] | 2084 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2085 | // Ensure that any moving of objects within the allocated map is done before |
2086 | // we process them. | ||||
Douglas Gregor | 27c6da2 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 2087 | if (!foundAny) New->setAttrs(AttrVec()); |
John McCall | eca5d22 | 2011-03-02 04:00:57 +0000 | [diff] [blame] | 2088 | |
Peter Collingbourne | a97d70b | 2011-01-21 02:08:36 +0000 | [diff] [blame] | 2089 | for (specific_attr_iterator<InheritableAttr> |
Douglas Gregor | 27c6da2 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 2090 | i = Old->specific_attr_begin<InheritableAttr>(), |
2091 | e = Old->specific_attr_end<InheritableAttr>(); | ||||
2092 | i != e; ++i) { | ||||
Douglas Gregor | f4d918f | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2093 | bool Override = false; |
Douglas Gregor | c193dd8 | 2011-09-23 20:23:42 +0000 | [diff] [blame] | 2094 | // Ignore deprecated/unavailable/availability attributes if requested. |
Douglas Gregor | f4d918f | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2095 | if (isa<DeprecatedAttr>(*i) || |
2096 | isa<UnavailableAttr>(*i) || | ||||
2097 | isa<AvailabilityAttr>(*i)) { | ||||
2098 | switch (AMK) { | ||||
2099 | case AMK_None: | ||||
2100 | continue; | ||||
John McCall | 6c2c250 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 2101 | |
Douglas Gregor | f4d918f | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2102 | case AMK_Redeclaration: |
2103 | break; | ||||
2104 | |||||
2105 | case AMK_Override: | ||||
2106 | Override = true; | ||||
2107 | break; | ||||
2108 | } | ||||
2109 | } | ||||
2110 | |||||
Richard Smith | 671b321 | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 2111 | if (mergeDeclAttribute(*this, New, *i, Override)) |
John McCall | eca5d22 | 2011-03-02 04:00:57 +0000 | [diff] [blame] | 2112 | foundAny = true; |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 2113 | } |
John McCall | eca5d22 | 2011-03-02 04:00:57 +0000 | [diff] [blame] | 2114 | |
Richard Smith | 671b321 | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 2115 | if (mergeAlignedAttrs(*this, New, Old)) |
2116 | foundAny = true; | ||||
2117 | |||||
Douglas Gregor | 27c6da2 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 2118 | if (!foundAny) New->dropAttrs(); |
John McCall | eca5d22 | 2011-03-02 04:00:57 +0000 | [diff] [blame] | 2119 | } |
2120 | |||||
2121 | /// mergeParamDeclAttributes - Copy attributes from the old parameter | ||||
2122 | /// to the new one. | ||||
2123 | static void mergeParamDeclAttributes(ParmVarDecl *newDecl, | ||||
2124 | const ParmVarDecl *oldDecl, | ||||
Richard Smith | 3a2b7a1 | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 2125 | Sema &S) { |
2126 | // C++11 [dcl.attr.depend]p2: | ||||
2127 | // The first declaration of a function shall specify the | ||||
2128 | // carries_dependency attribute for its declarator-id if any declaration | ||||
2129 | // of the function specifies the carries_dependency attribute. | ||||
2130 | if (newDecl->hasAttr<CarriesDependencyAttr>() && | ||||
2131 | !oldDecl->hasAttr<CarriesDependencyAttr>()) { | ||||
2132 | S.Diag(newDecl->getAttr<CarriesDependencyAttr>()->getLocation(), | ||||
2133 | diag::err_carries_dependency_missing_on_first_decl) << 1/*Param*/; | ||||
2134 | // Find the first declaration of the parameter. | ||||
2135 | // FIXME: Should we build redeclaration chains for function parameters? | ||||
2136 | const FunctionDecl *FirstFD = | ||||
2137 | cast<FunctionDecl>(oldDecl->getDeclContext())->getFirstDeclaration(); | ||||
2138 | const ParmVarDecl *FirstVD = | ||||
2139 | FirstFD->getParamDecl(oldDecl->getFunctionScopeIndex()); | ||||
2140 | S.Diag(FirstVD->getLocation(), | ||||
2141 | diag::note_carries_dependency_missing_first_decl) << 1/*Param*/; | ||||
2142 | } | ||||
2143 | |||||
John McCall | eca5d22 | 2011-03-02 04:00:57 +0000 | [diff] [blame] | 2144 | if (!oldDecl->hasAttrs()) |
2145 | return; | ||||
2146 | |||||
2147 | bool foundAny = newDecl->hasAttrs(); | ||||
2148 | |||||
2149 | // Ensure that any moving of objects within the allocated map is | ||||
2150 | // done before we process them. | ||||
2151 | if (!foundAny) newDecl->setAttrs(AttrVec()); | ||||
2152 | |||||
2153 | for (specific_attr_iterator<InheritableParamAttr> | ||||
2154 | i = oldDecl->specific_attr_begin<InheritableParamAttr>(), | ||||
2155 | e = oldDecl->specific_attr_end<InheritableParamAttr>(); i != e; ++i) { | ||||
2156 | if (!DeclHasAttr(newDecl, *i)) { | ||||
Richard Smith | 3a2b7a1 | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 2157 | InheritableAttr *newAttr = |
2158 | cast<InheritableParamAttr>((*i)->clone(S.Context)); | ||||
John McCall | eca5d22 | 2011-03-02 04:00:57 +0000 | [diff] [blame] | 2159 | newAttr->setInherited(true); |
2160 | newDecl->addAttr(newAttr); | ||||
2161 | foundAny = true; | ||||
2162 | } | ||||
2163 | } | ||||
2164 | |||||
2165 | if (!foundAny) newDecl->dropAttrs(); | ||||
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 2166 | } |
2167 | |||||
Dan Gohman | 3c46e8d | 2010-07-26 21:25:24 +0000 | [diff] [blame] | 2168 | namespace { |
2169 | |||||
Douglas Gregor | c837656 | 2009-03-06 22:43:54 +0000 | [diff] [blame] | 2170 | /// Used in MergeFunctionDecl to keep track of function parameters in |
2171 | /// C. | ||||
2172 | struct GNUCompatibleParamWarning { | ||||
2173 | ParmVarDecl *OldParm; | ||||
2174 | ParmVarDecl *NewParm; | ||||
2175 | QualType PromotedType; | ||||
2176 | }; | ||||
2177 | |||||
Dan Gohman | 3c46e8d | 2010-07-26 21:25:24 +0000 | [diff] [blame] | 2178 | } |
Anders Carlsson | 5c478cf | 2009-12-04 22:33:25 +0000 | [diff] [blame] | 2179 | |
2180 | /// getSpecialMember - get the special member enum for a method. | ||||
Anders Carlsson | 3b8c53b | 2010-04-22 05:40:53 +0000 | [diff] [blame] | 2181 | Sema::CXXSpecialMember Sema::getSpecialMember(const CXXMethodDecl *MD) { |
Anders Carlsson | 5c478cf | 2009-12-04 22:33:25 +0000 | [diff] [blame] | 2182 | if (const CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(MD)) { |
Sean Hunt | f961ea5 | 2011-05-10 19:08:14 +0000 | [diff] [blame] | 2183 | if (Ctor->isDefaultConstructor()) |
2184 | return Sema::CXXDefaultConstructor; | ||||
Sean Hunt | 9ae60d5 | 2011-05-26 01:26:05 +0000 | [diff] [blame] | 2185 | |
2186 | if (Ctor->isCopyConstructor()) | ||||
2187 | return Sema::CXXCopyConstructor; | ||||
2188 | |||||
2189 | if (Ctor->isMoveConstructor()) | ||||
2190 | return Sema::CXXMoveConstructor; | ||||
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 2191 | } else if (isa<CXXDestructorDecl>(MD)) { |
Anders Carlsson | 5c478cf | 2009-12-04 22:33:25 +0000 | [diff] [blame] | 2192 | return Sema::CXXDestructor; |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 2193 | } else if (MD->isCopyAssignmentOperator()) { |
Sean Hunt | f961ea5 | 2011-05-10 19:08:14 +0000 | [diff] [blame] | 2194 | return Sema::CXXCopyAssignment; |
Sebastian Redl | 74e611a | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 2195 | } else if (MD->isMoveAssignmentOperator()) { |
2196 | return Sema::CXXMoveAssignment; | ||||
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 2197 | } |
Sean Hunt | f961ea5 | 2011-05-10 19:08:14 +0000 | [diff] [blame] | 2198 | |
Sean Hunt | f961ea5 | 2011-05-10 19:08:14 +0000 | [diff] [blame] | 2199 | return Sema::CXXInvalid; |
Anders Carlsson | 5c478cf | 2009-12-04 22:33:25 +0000 | [diff] [blame] | 2200 | } |
2201 | |||||
Sebastian Redl | 515ddd8 | 2010-06-09 21:17:41 +0000 | [diff] [blame] | 2202 | /// canRedefineFunction - checks if a function can be redefined. Currently, |
Charles Davis | f3f8d2a | 2010-02-18 02:00:42 +0000 | [diff] [blame] | 2203 | /// only extern inline functions can be redefined, and even then only in |
2204 | /// GNU89 mode. | ||||
2205 | static bool canRedefineFunction(const FunctionDecl *FD, | ||||
2206 | const LangOptions& LangOpts) { | ||||
Eli Friedman | eca3ed7 | 2011-06-13 23:56:42 +0000 | [diff] [blame] | 2207 | return ((FD->hasAttr<GNUInlineAttr>() || LangOpts.GNUInline) && |
2208 | !LangOpts.CPlusPlus && | ||||
Charles Davis | f3f8d2a | 2010-02-18 02:00:42 +0000 | [diff] [blame] | 2209 | FD->isInlineSpecified() && |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2210 | FD->getStorageClass() == SC_Extern); |
Charles Davis | f3f8d2a | 2010-02-18 02:00:42 +0000 | [diff] [blame] | 2211 | } |
2212 | |||||
Reid Kleckner | ef07203 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 2213 | const AttributedType *Sema::getCallingConvAttributedType(QualType T) const { |
2214 | const AttributedType *AT = T->getAs<AttributedType>(); | ||||
2215 | while (AT && !AT->isCallingConv()) | ||||
2216 | AT = AT->getModifiedType()->getAs<AttributedType>(); | ||||
2217 | return AT; | ||||
John McCall | fb60914 | 2012-08-25 02:00:03 +0000 | [diff] [blame] | 2218 | } |
2219 | |||||
Benjamin Kramer | a574c89 | 2013-02-15 12:30:38 +0000 | [diff] [blame] | 2220 | template <typename T> |
2221 | static bool haveIncompatibleLanguageLinkages(const T *Old, const T *New) { | ||||
Rafael Espindola | 950fee2 | 2013-02-14 01:18:37 +0000 | [diff] [blame] | 2222 | const DeclContext *DC = Old->getDeclContext(); |
2223 | if (DC->isRecord()) | ||||
2224 | return false; | ||||
2225 | |||||
2226 | LanguageLinkage OldLinkage = Old->getLanguageLinkage(); | ||||
Rafael Espindola | d8ffd0b | 2013-05-05 20:15:21 +0000 | [diff] [blame] | 2227 | if (OldLinkage == CXXLanguageLinkage && New->isInExternCContext()) |
Rafael Espindola | 950fee2 | 2013-02-14 01:18:37 +0000 | [diff] [blame] | 2228 | return true; |
Rafael Espindola | d8ffd0b | 2013-05-05 20:15:21 +0000 | [diff] [blame] | 2229 | if (OldLinkage == CLanguageLinkage && New->isInExternCXXContext()) |
Rafael Espindola | 950fee2 | 2013-02-14 01:18:37 +0000 | [diff] [blame] | 2230 | return true; |
2231 | return false; | ||||
2232 | } | ||||
2233 | |||||
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2234 | /// MergeFunctionDecl - We just parsed a function 'New' from |
2235 | /// declarator D which has the same name and scope as a previous | ||||
2236 | /// declaration 'Old'. Figure out how to resolve this situation, | ||||
2237 | /// merging decls or emitting diagnostics as appropriate. | ||||
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2238 | /// |
2239 | /// In C++, New and Old must be declarations that are not | ||||
2240 | /// overloaded. Use IsOverload to determine whether New and Old are | ||||
2241 | /// overloaded, and to select the Old declaration that New should be | ||||
2242 | /// merged with. | ||||
Douglas Gregor | cda9c67 | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 2243 | /// |
2244 | /// Returns true if there was an error, false otherwise. | ||||
Richard Smith | dd9459f | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 2245 | bool Sema::MergeFunctionDecl(FunctionDecl *New, Decl *OldD, Scope *S, |
2246 | bool MergeTypeWithOld) { | ||||
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2247 | // Verify the old decl was also a function. |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2248 | FunctionDecl *Old = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2249 | if (FunctionTemplateDecl *OldFunctionTemplate |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2250 | = dyn_cast<FunctionTemplateDecl>(OldD)) |
2251 | Old = OldFunctionTemplate->getTemplatedDecl(); | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2252 | else |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2253 | Old = dyn_cast<FunctionDecl>(OldD); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2254 | if (!Old) { |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 2255 | if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(OldD)) { |
John McCall | 78037ac | 2013-04-03 21:19:47 +0000 | [diff] [blame] | 2256 | if (New->getFriendObjectKind()) { |
2257 | Diag(New->getLocation(), diag::err_using_decl_friend); | ||||
2258 | Diag(Shadow->getTargetDecl()->getLocation(), | ||||
2259 | diag::note_using_decl_target); | ||||
2260 | Diag(Shadow->getUsingDecl()->getLocation(), | ||||
2261 | diag::note_using_decl) << 0; | ||||
2262 | return true; | ||||
2263 | } | ||||
2264 | |||||
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 2265 | Diag(New->getLocation(), diag::err_using_decl_conflict_reverse); |
2266 | Diag(Shadow->getTargetDecl()->getLocation(), | ||||
2267 | diag::note_using_decl_target); | ||||
2268 | Diag(Shadow->getUsingDecl()->getLocation(), | ||||
2269 | diag::note_using_decl) << 0; | ||||
2270 | return true; | ||||
2271 | } | ||||
2272 | |||||
Chris Lattner | 5dc266a | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 2273 | Diag(New->getLocation(), diag::err_redefinition_different_kind) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 2274 | << New->getDeclName(); |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 2275 | Diag(OldD->getLocation(), diag::note_previous_definition); |
Douglas Gregor | cda9c67 | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 2276 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2277 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2278 | |
David Majnemer | bcd0650 | 2013-07-07 23:49:50 +0000 | [diff] [blame] | 2279 | // If the old declaration is invalid, just give up here. |
2280 | if (Old->isInvalidDecl()) | ||||
2281 | return true; | ||||
2282 | |||||
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2283 | // Determine whether the previous declaration was a definition, |
2284 | // implicit declaration, or a declaration. | ||||
2285 | diag::kind PrevDiag; | ||||
2286 | if (Old->isThisDeclarationADefinition()) | ||||
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 2287 | PrevDiag = diag::note_previous_definition; |
Douglas Gregor | cda9c67 | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 2288 | else if (Old->isImplicit()) |
2289 | PrevDiag = diag::note_previous_implicit_declaration; | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2290 | else |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 2291 | PrevDiag = diag::note_previous_declaration; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2292 | |
Charles Davis | f3f8d2a | 2010-02-18 02:00:42 +0000 | [diff] [blame] | 2293 | // Don't complain about this if we're in GNU89 mode and the old function |
2294 | // is an extern inline function. | ||||
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 2295 | // Don't complain about specializations. They are not supposed to have |
2296 | // storage classes. | ||||
Douglas Gregor | 04495c8 | 2009-02-24 01:23:02 +0000 | [diff] [blame] | 2297 | if (!isa<CXXMethodDecl>(New) && !isa<CXXMethodDecl>(Old) && |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2298 | New->getStorageClass() == SC_Static && |
Rafael Espindola | 181e3ec | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 2299 | Old->hasExternalFormalLinkage() && |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 2300 | !New->getTemplateSpecializationInfo() && |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2301 | !canRedefineFunction(Old, getLangOpts())) { |
2302 | if (getLangOpts().MicrosoftExt) { | ||||
Francois Pichet | 4bada2e | 2011-04-22 19:50:06 +0000 | [diff] [blame] | 2303 | Diag(New->getLocation(), diag::warn_static_non_static) << New; |
2304 | Diag(Old->getLocation(), PrevDiag); | ||||
2305 | } else { | ||||
2306 | Diag(New->getLocation(), diag::err_static_non_static) << New; | ||||
2307 | Diag(Old->getLocation(), PrevDiag); | ||||
2308 | return true; | ||||
2309 | } | ||||
Douglas Gregor | 04495c8 | 2009-02-24 01:23:02 +0000 | [diff] [blame] | 2310 | } |
2311 | |||||
Reid Kleckner | ef07203 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 2312 | |
2313 | // If a function is first declared with a calling convention, but is later | ||||
2314 | // declared or defined without one, all following decls assume the calling | ||||
2315 | // convention of the first. | ||||
John McCall | f82b4e8 | 2010-02-04 05:44:44 +0000 | [diff] [blame] | 2316 | // |
John McCall | fb60914 | 2012-08-25 02:00:03 +0000 | [diff] [blame] | 2317 | // It's OK if a function is first declared without a calling convention, |
2318 | // but is later declared or defined with the default calling convention. | ||||
2319 | // | ||||
Reid Kleckner | ef07203 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 2320 | // To test if either decl has an explicit calling convention, we look for |
2321 | // AttributedType sugar nodes on the type as written. If they are missing or | ||||
2322 | // were canonicalized away, we assume the calling convention was implicit. | ||||
John McCall | f82b4e8 | 2010-02-04 05:44:44 +0000 | [diff] [blame] | 2323 | // |
2324 | // Note also that we DO NOT return at this point, because we still have | ||||
2325 | // other tests to run. | ||||
Reid Kleckner | ef07203 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 2326 | QualType OldQType = Context.getCanonicalType(Old->getType()); |
2327 | QualType NewQType = Context.getCanonicalType(New->getType()); | ||||
John McCall | e6a365d | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 2328 | const FunctionType *OldType = cast<FunctionType>(OldQType); |
Reid Kleckner | ef07203 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 2329 | const FunctionType *NewType = cast<FunctionType>(NewQType); |
John McCall | e6a365d | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 2330 | FunctionType::ExtInfo OldTypeInfo = OldType->getExtInfo(); |
2331 | FunctionType::ExtInfo NewTypeInfo = NewType->getExtInfo(); | ||||
2332 | bool RequiresAdjustment = false; | ||||
John McCall | fb60914 | 2012-08-25 02:00:03 +0000 | [diff] [blame] | 2333 | |
Reid Kleckner | ef07203 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 2334 | if (OldTypeInfo.getCC() != NewTypeInfo.getCC()) { |
2335 | FunctionDecl *First = Old->getFirstDeclaration(); | ||||
2336 | const FunctionType *FT = | ||||
2337 | First->getType().getCanonicalType()->castAs<FunctionType>(); | ||||
2338 | FunctionType::ExtInfo FI = FT->getExtInfo(); | ||||
2339 | bool NewCCExplicit = getCallingConvAttributedType(New->getType()); | ||||
2340 | if (!NewCCExplicit) { | ||||
2341 | // Inherit the CC from the previous declaration if it was specified | ||||
2342 | // there but not here. | ||||
2343 | NewTypeInfo = NewTypeInfo.withCallingConv(OldTypeInfo.getCC()); | ||||
2344 | RequiresAdjustment = true; | ||||
2345 | } else { | ||||
2346 | // Calling conventions aren't compatible, so complain. | ||||
2347 | bool FirstCCExplicit = getCallingConvAttributedType(First->getType()); | ||||
2348 | Diag(New->getLocation(), diag::err_cconv_change) | ||||
2349 | << FunctionType::getNameForCallConv(NewTypeInfo.getCC()) | ||||
2350 | << !FirstCCExplicit | ||||
2351 | << (!FirstCCExplicit ? "" : | ||||
2352 | FunctionType::getNameForCallConv(FI.getCC())); | ||||
John McCall | fb60914 | 2012-08-25 02:00:03 +0000 | [diff] [blame] | 2353 | |
Reid Kleckner | ef07203 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 2354 | // Put the note on the first decl, since it is the one that matters. |
2355 | Diag(First->getLocation(), diag::note_previous_declaration); | ||||
2356 | return true; | ||||
2357 | } | ||||
John McCall | f82b4e8 | 2010-02-04 05:44:44 +0000 | [diff] [blame] | 2358 | } |
2359 | |||||
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 2360 | // FIXME: diagnose the other way around? |
John McCall | e6a365d | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 2361 | if (OldTypeInfo.getNoReturn() && !NewTypeInfo.getNoReturn()) { |
2362 | NewTypeInfo = NewTypeInfo.withNoReturn(true); | ||||
2363 | RequiresAdjustment = true; | ||||
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 2364 | } |
2365 | |||||
Douglas Gregor | d2c6490 | 2010-06-18 21:30:25 +0000 | [diff] [blame] | 2366 | // Merge regparm attribute. |
Eli Friedman | a49218e | 2011-04-09 08:18:08 +0000 | [diff] [blame] | 2367 | if (OldTypeInfo.getHasRegParm() != NewTypeInfo.getHasRegParm() || |
2368 | OldTypeInfo.getRegParm() != NewTypeInfo.getRegParm()) { | ||||
2369 | if (NewTypeInfo.getHasRegParm()) { | ||||
Douglas Gregor | d2c6490 | 2010-06-18 21:30:25 +0000 | [diff] [blame] | 2370 | Diag(New->getLocation(), diag::err_regparm_mismatch) |
2371 | << NewType->getRegParmType() | ||||
2372 | << OldType->getRegParmType(); | ||||
2373 | Diag(Old->getLocation(), diag::note_previous_declaration); | ||||
2374 | return true; | ||||
2375 | } | ||||
John McCall | e6a365d | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 2376 | |
2377 | NewTypeInfo = NewTypeInfo.withRegParm(OldTypeInfo.getRegParm()); | ||||
2378 | RequiresAdjustment = true; | ||||
2379 | } | ||||
2380 | |||||
Douglas Gregor | cb1c9c3 | 2011-10-14 15:55:40 +0000 | [diff] [blame] | 2381 | // Merge ns_returns_retained attribute. |
2382 | if (OldTypeInfo.getProducesResult() != NewTypeInfo.getProducesResult()) { | ||||
2383 | if (NewTypeInfo.getProducesResult()) { | ||||
2384 | Diag(New->getLocation(), diag::err_returns_retained_mismatch); | ||||
2385 | Diag(Old->getLocation(), diag::note_previous_declaration); | ||||
2386 | return true; | ||||
2387 | } | ||||
2388 | |||||
2389 | NewTypeInfo = NewTypeInfo.withProducesResult(true); | ||||
2390 | RequiresAdjustment = true; | ||||
2391 | } | ||||
2392 | |||||
John McCall | e6a365d | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 2393 | if (RequiresAdjustment) { |
Eli Friedman | 130fcc8 | 2013-09-06 21:09:09 +0000 | [diff] [blame] | 2394 | const FunctionType *AdjustedType = New->getType()->getAs<FunctionType>(); |
2395 | AdjustedType = Context.adjustFunctionType(AdjustedType, NewTypeInfo); | ||||
2396 | New->setType(QualType(AdjustedType, 0)); | ||||
John McCall | e6a365d | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 2397 | NewQType = Context.getCanonicalType(New->getType()); |
Eli Friedman | 130fcc8 | 2013-09-06 21:09:09 +0000 | [diff] [blame] | 2398 | NewType = cast<FunctionType>(NewQType); |
Douglas Gregor | d2c6490 | 2010-06-18 21:30:25 +0000 | [diff] [blame] | 2399 | } |
Nick Lewycky | cd0655b | 2013-02-01 08:13:20 +0000 | [diff] [blame] | 2400 | |
2401 | // If this redeclaration makes the function inline, we may need to add it to | ||||
2402 | // UndefinedButUsed. | ||||
2403 | if (!Old->isInlined() && New->isInlined() && | ||||
2404 | !New->hasAttr<GNUInlineAttr>() && | ||||
2405 | (getLangOpts().CPlusPlus || !getLangOpts().GNUInline) && | ||||
2406 | Old->isUsed(false) && | ||||
2407 | !Old->isDefined() && !New->isThisDeclarationADefinition()) | ||||
2408 | UndefinedButUsed.insert(std::make_pair(Old->getCanonicalDecl(), | ||||
2409 | SourceLocation())); | ||||
2410 | |||||
2411 | // If this redeclaration makes it newly gnu_inline, we don't want to warn | ||||
2412 | // about it. | ||||
2413 | if (New->hasAttr<GNUInlineAttr>() && | ||||
2414 | Old->isInlined() && !Old->hasAttr<GNUInlineAttr>()) { | ||||
2415 | UndefinedButUsed.erase(Old->getCanonicalDecl()); | ||||
2416 | } | ||||
Douglas Gregor | d2c6490 | 2010-06-18 21:30:25 +0000 | [diff] [blame] | 2417 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2418 | if (getLangOpts().CPlusPlus) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2419 | // (C++98 13.1p2): |
2420 | // Certain function declarations cannot be overloaded: | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2421 | // -- Function declarations that differ only in the return type |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2422 | // cannot be overloaded. |
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2423 | |
2424 | // 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] | 2425 | // per C++1y [dcl.type.auto]p13: |
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2426 | // Redeclarations or specializations of a function or function template |
2427 | // with a declared return type that uses a placeholder type shall also | ||||
2428 | // use that placeholder, not a deduced type. | ||||
2429 | QualType OldDeclaredReturnType = (Old->getTypeSourceInfo() | ||||
2430 | ? Old->getTypeSourceInfo()->getType()->castAs<FunctionType>() | ||||
2431 | : OldType)->getResultType(); | ||||
2432 | QualType NewDeclaredReturnType = (New->getTypeSourceInfo() | ||||
2433 | ? New->getTypeSourceInfo()->getType()->castAs<FunctionType>() | ||||
2434 | : NewType)->getResultType(); | ||||
Fariborz Jahanian | 2390a72 | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 2435 | QualType ResQT; |
Richard Smith | a41c97a | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 2436 | if (!Context.hasSameType(OldDeclaredReturnType, NewDeclaredReturnType) && |
2437 | !((NewQType->isDependentType() || OldQType->isDependentType()) && | ||||
2438 | New->isLocalExternDecl())) { | ||||
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2439 | if (NewDeclaredReturnType->isObjCObjectPointerType() && |
2440 | OldDeclaredReturnType->isObjCObjectPointerType()) | ||||
Fariborz Jahanian | 2390a72 | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 2441 | ResQT = Context.mergeObjCGCQualifiers(NewQType, OldQType); |
2442 | if (ResQT.isNull()) { | ||||
Argyrios Kyrtzidis | 1de34dd | 2011-02-05 05:54:49 +0000 | [diff] [blame] | 2443 | if (New->isCXXClassMember() && New->isOutOfLine()) |
2444 | Diag(New->getLocation(), | ||||
2445 | diag::err_member_def_does_not_match_ret_type) << New; | ||||
2446 | else | ||||
2447 | Diag(New->getLocation(), diag::err_ovl_diff_return_type); | ||||
Fariborz Jahanian | 2390a72 | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 2448 | Diag(Old->getLocation(), PrevDiag) << Old << Old->getType(); |
2449 | return true; | ||||
2450 | } | ||||
2451 | else | ||||
2452 | NewQType = ResQT; | ||||
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2453 | } |
2454 | |||||
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2455 | QualType OldReturnType = OldType->getResultType(); |
2456 | QualType NewReturnType = cast<FunctionType>(NewQType)->getResultType(); | ||||
2457 | if (OldReturnType != NewReturnType) { | ||||
2458 | // If this function has a deduced return type and has already been | ||||
2459 | // defined, copy the deduced value from the old declaration. | ||||
2460 | AutoType *OldAT = Old->getResultType()->getContainedAutoType(); | ||||
2461 | if (OldAT && OldAT->isDeduced()) { | ||||
Richard Smith | 37e849a | 2013-08-14 20:16:31 +0000 | [diff] [blame] | 2462 | New->setType( |
2463 | SubstAutoType(New->getType(), | ||||
2464 | OldAT->isDependentType() ? Context.DependentTy | ||||
2465 | : OldAT->getDeducedType())); | ||||
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2466 | NewQType = Context.getCanonicalType( |
Richard Smith | 37e849a | 2013-08-14 20:16:31 +0000 | [diff] [blame] | 2467 | SubstAutoType(NewQType, |
2468 | OldAT->isDependentType() ? Context.DependentTy | ||||
2469 | : OldAT->getDeducedType())); | ||||
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2470 | } |
2471 | } | ||||
2472 | |||||
2473 | const CXXMethodDecl *OldMethod = dyn_cast<CXXMethodDecl>(Old); | ||||
2474 | CXXMethodDecl *NewMethod = dyn_cast<CXXMethodDecl>(New); | ||||
Anders Carlsson | 5c478cf | 2009-12-04 22:33:25 +0000 | [diff] [blame] | 2475 | if (OldMethod && NewMethod) { |
John McCall | 3d04336 | 2010-04-13 07:45:41 +0000 | [diff] [blame] | 2476 | // Preserve triviality. |
2477 | NewMethod->setTrivial(OldMethod->isTrivial()); | ||||
Francois Pichet | e1e96a6 | 2011-05-14 19:17:07 +0000 | [diff] [blame] | 2478 | |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 2479 | // MSVC allows explicit template specialization at class scope: |
2480 | // 2 CXMethodDecls referring to the same function will be injected. | ||||
2481 | // We don't want a redeclartion error. | ||||
2482 | bool IsClassScopeExplicitSpecialization = | ||||
2483 | OldMethod->isFunctionTemplateSpecialization() && | ||||
2484 | NewMethod->isFunctionTemplateSpecialization(); | ||||
John McCall | 3d04336 | 2010-04-13 07:45:41 +0000 | [diff] [blame] | 2485 | bool isFriend = NewMethod->getFriendObjectKind(); |
2486 | |||||
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 2487 | if (!isFriend && NewMethod->getLexicalDeclContext()->isRecord() && |
2488 | !IsClassScopeExplicitSpecialization) { | ||||
Anders Carlsson | 5c478cf | 2009-12-04 22:33:25 +0000 | [diff] [blame] | 2489 | // -- Member function declarations with the same name and the |
2490 | // same parameter types cannot be overloaded if any of them | ||||
2491 | // is a static member function declaration. | ||||
Eli Friedman | fa0d3f8 | 2013-06-19 22:43:55 +0000 | [diff] [blame] | 2492 | if (OldMethod->isStatic() != NewMethod->isStatic()) { |
Anders Carlsson | 5c478cf | 2009-12-04 22:33:25 +0000 | [diff] [blame] | 2493 | Diag(New->getLocation(), diag::err_ovl_static_nonstatic_member); |
2494 | Diag(Old->getLocation(), PrevDiag) << Old << Old->getType(); | ||||
2495 | return true; | ||||
2496 | } | ||||
Richard Smith | 838925d | 2012-07-13 04:12:04 +0000 | [diff] [blame] | 2497 | |
Anders Carlsson | 5c478cf | 2009-12-04 22:33:25 +0000 | [diff] [blame] | 2498 | // C++ [class.mem]p1: |
2499 | // [...] A member shall not be declared twice in the | ||||
2500 | // member-specification, except that a nested class or member | ||||
2501 | // class template can be declared and then later defined. | ||||
Richard Smith | 838925d | 2012-07-13 04:12:04 +0000 | [diff] [blame] | 2502 | if (ActiveTemplateInstantiations.empty()) { |
2503 | unsigned NewDiag; | ||||
2504 | if (isa<CXXConstructorDecl>(OldMethod)) | ||||
2505 | NewDiag = diag::err_constructor_redeclared; | ||||
2506 | else if (isa<CXXDestructorDecl>(NewMethod)) | ||||
2507 | NewDiag = diag::err_destructor_redeclared; | ||||
2508 | else if (isa<CXXConversionDecl>(NewMethod)) | ||||
2509 | NewDiag = diag::err_conv_function_redeclared; | ||||
2510 | else | ||||
2511 | NewDiag = diag::err_member_redeclared; | ||||
Anders Carlsson | 5c478cf | 2009-12-04 22:33:25 +0000 | [diff] [blame] | 2512 | |
Richard Smith | 838925d | 2012-07-13 04:12:04 +0000 | [diff] [blame] | 2513 | Diag(New->getLocation(), NewDiag); |
2514 | } else { | ||||
2515 | Diag(New->getLocation(), diag::err_member_redeclared_in_instantiation) | ||||
2516 | << New << New->getType(); | ||||
2517 | } | ||||
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 2518 | Diag(Old->getLocation(), PrevDiag) << Old << Old->getType(); |
John McCall | 3d04336 | 2010-04-13 07:45:41 +0000 | [diff] [blame] | 2519 | |
2520 | // Complain if this is an explicit declaration of a special | ||||
2521 | // member that was initially declared implicitly. | ||||
2522 | // | ||||
2523 | // As an exception, it's okay to befriend such methods in order | ||||
2524 | // to permit the implicit constructor/destructor/operator calls. | ||||
2525 | } else if (OldMethod->isImplicit()) { | ||||
2526 | if (isFriend) { | ||||
2527 | NewMethod->setImplicit(); | ||||
2528 | } else { | ||||
Anders Carlsson | 5c478cf | 2009-12-04 22:33:25 +0000 | [diff] [blame] | 2529 | Diag(NewMethod->getLocation(), |
2530 | diag::err_definition_of_implicitly_declared_member) | ||||
Anders Carlsson | 3b8c53b | 2010-04-22 05:40:53 +0000 | [diff] [blame] | 2531 | << New << getSpecialMember(OldMethod); |
Anders Carlsson | 5c478cf | 2009-12-04 22:33:25 +0000 | [diff] [blame] | 2532 | return true; |
2533 | } | ||||
Richard Smith | f4fe843 | 2012-06-08 01:30:54 +0000 | [diff] [blame] | 2534 | } else if (OldMethod->isExplicitlyDefaulted() && !isFriend) { |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 2535 | Diag(NewMethod->getLocation(), |
2536 | diag::err_definition_of_explicitly_defaulted_member) | ||||
2537 | << getSpecialMember(OldMethod); | ||||
2538 | return true; | ||||
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2539 | } |
2540 | } | ||||
2541 | |||||
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 2542 | // C++11 [dcl.attr.noreturn]p1: |
2543 | // The first declaration of a function shall specify the noreturn | ||||
2544 | // attribute if any declaration of that function specifies the noreturn | ||||
2545 | // attribute. | ||||
2546 | if (New->hasAttr<CXX11NoReturnAttr>() && | ||||
2547 | !Old->hasAttr<CXX11NoReturnAttr>()) { | ||||
2548 | Diag(New->getAttr<CXX11NoReturnAttr>()->getLocation(), | ||||
2549 | diag::err_noreturn_missing_on_first_decl); | ||||
2550 | Diag(Old->getFirstDeclaration()->getLocation(), | ||||
2551 | diag::note_noreturn_missing_first_decl); | ||||
2552 | } | ||||
2553 | |||||
Richard Smith | 3a2b7a1 | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 2554 | // C++11 [dcl.attr.depend]p2: |
2555 | // The first declaration of a function shall specify the | ||||
2556 | // carries_dependency attribute for its declarator-id if any declaration | ||||
2557 | // of the function specifies the carries_dependency attribute. | ||||
2558 | if (New->hasAttr<CarriesDependencyAttr>() && | ||||
2559 | !Old->hasAttr<CarriesDependencyAttr>()) { | ||||
2560 | Diag(New->getAttr<CarriesDependencyAttr>()->getLocation(), | ||||
2561 | diag::err_carries_dependency_missing_on_first_decl) << 0/*Function*/; | ||||
2562 | Diag(Old->getFirstDeclaration()->getLocation(), | ||||
2563 | diag::note_carries_dependency_missing_first_decl) << 0/*Function*/; | ||||
2564 | } | ||||
2565 | |||||
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2566 | // (C++98 8.3.5p3): |
2567 | // All declarations for a function shall agree exactly in both the | ||||
2568 | // return type and the parameter-type-list. | ||||
John McCall | e6a365d | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 2569 | // We also want to respect all the extended bits except noreturn. |
2570 | |||||
2571 | // noreturn should now match unless the old type info didn't have it. | ||||
2572 | QualType OldQTypeForComparison = OldQType; | ||||
2573 | if (!OldTypeInfo.getNoReturn() && NewTypeInfo.getNoReturn()) { | ||||
2574 | assert(OldQType == QualType(OldType, 0)); | ||||
2575 | const FunctionType *OldTypeForComparison | ||||
2576 | = Context.adjustFunctionType(OldType, OldTypeInfo.withNoReturn(true)); | ||||
2577 | OldQTypeForComparison = QualType(OldTypeForComparison, 0); | ||||
2578 | assert(OldQTypeForComparison.isCanonical()); | ||||
2579 | } | ||||
2580 | |||||
Rafael Espindola | 950fee2 | 2013-02-14 01:18:37 +0000 | [diff] [blame] | 2581 | if (haveIncompatibleLanguageLinkages(Old, New)) { |
Rafael Espindola | e57e3d3 | 2012-12-27 03:56:20 +0000 | [diff] [blame] | 2582 | Diag(New->getLocation(), diag::err_different_language_linkage) << New; |
2583 | Diag(Old->getLocation(), PrevDiag); | ||||
2584 | return true; | ||||
2585 | } | ||||
2586 | |||||
John McCall | e6a365d | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 2587 | if (OldQTypeForComparison == NewQType) |
Richard Smith | dd9459f | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 2588 | return MergeCompatibleFunctionDecls(New, Old, S, MergeTypeWithOld); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2589 | |
Richard Smith | a41c97a | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 2590 | if ((NewQType->isDependentType() || OldQType->isDependentType()) && |
2591 | New->isLocalExternDecl()) { | ||||
2592 | // It's OK if we couldn't merge types for a local function declaraton | ||||
2593 | // if either the old or new type is dependent. We'll merge the types | ||||
2594 | // when we instantiate the function. | ||||
2595 | return false; | ||||
2596 | } | ||||
2597 | |||||
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2598 | // Fall through for conflicting redeclarations and redefinitions. |
Douglas Gregor | f009795 | 2008-04-21 02:02:58 +0000 | [diff] [blame] | 2599 | } |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2600 | |
2601 | // C: Function types need to be compatible, not identical. This handles | ||||
Steve Naroff | adbbd0c | 2008-01-14 20:51:29 +0000 | [diff] [blame] | 2602 | // duplicate function decls like "void f(int); void f(enum X);" properly. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2603 | if (!getLangOpts().CPlusPlus && |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2604 | Context.typesAreCompatible(OldQType, NewQType)) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2605 | const FunctionType *OldFuncType = OldQType->getAs<FunctionType>(); |
2606 | const FunctionType *NewFuncType = NewQType->getAs<FunctionType>(); | ||||
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2607 | const FunctionProtoType *OldProto = 0; |
Richard Smith | dd9459f | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 2608 | if (MergeTypeWithOld && isa<FunctionNoProtoType>(NewFuncType) && |
Douglas Gregor | c837656 | 2009-03-06 22:43:54 +0000 | [diff] [blame] | 2609 | (OldProto = dyn_cast<FunctionProtoType>(OldFuncType))) { |
Douglas Gregor | 6871981 | 2009-02-16 18:20:44 +0000 | [diff] [blame] | 2610 | // The old declaration provided a function prototype, but the |
2611 | // new declaration does not. Merge in the prototype. | ||||
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2612 | assert(!OldProto->hasExceptionSpec() && "Exception spec in C"); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2613 | SmallVector<QualType, 16> ParamTypes(OldProto->arg_type_begin(), |
Douglas Gregor | 6871981 | 2009-02-16 18:20:44 +0000 | [diff] [blame] | 2614 | OldProto->arg_type_end()); |
2615 | NewQType = Context.getFunctionType(NewFuncType->getResultType(), | ||||
Jordan Rose | bea522f | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2616 | ParamTypes, |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2617 | OldProto->getExtProtoInfo()); |
Douglas Gregor | 6871981 | 2009-02-16 18:20:44 +0000 | [diff] [blame] | 2618 | New->setType(NewQType); |
Anders Carlsson | a75e853 | 2009-05-14 21:46:00 +0000 | [diff] [blame] | 2619 | New->setHasInheritedPrototype(); |
Douglas Gregor | 450da98 | 2009-02-16 20:58:07 +0000 | [diff] [blame] | 2620 | |
2621 | // Synthesize a parameter for each argument type. | ||||
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2622 | SmallVector<ParmVarDecl*, 16> Params; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2623 | for (FunctionProtoType::arg_type_iterator |
2624 | ParamType = OldProto->arg_type_begin(), | ||||
Douglas Gregor | 450da98 | 2009-02-16 20:58:07 +0000 | [diff] [blame] | 2625 | ParamEnd = OldProto->arg_type_end(); |
2626 | ParamType != ParamEnd; ++ParamType) { | ||||
2627 | ParmVarDecl *Param = ParmVarDecl::Create(Context, New, | ||||
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2628 | SourceLocation(), |
Douglas Gregor | 450da98 | 2009-02-16 20:58:07 +0000 | [diff] [blame] | 2629 | SourceLocation(), 0, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2630 | *ParamType, /*TInfo=*/0, |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 2631 | SC_None, |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 2632 | 0); |
John McCall | fb44de9 | 2011-05-01 22:35:37 +0000 | [diff] [blame] | 2633 | Param->setScopeInfo(0, Params.size()); |
Douglas Gregor | 450da98 | 2009-02-16 20:58:07 +0000 | [diff] [blame] | 2634 | Param->setImplicit(); |
2635 | Params.push_back(Param); | ||||
2636 | } | ||||
2637 | |||||
David Blaikie | 4278c65 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 2638 | New->setParams(Params); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2639 | } |
Douglas Gregor | 6871981 | 2009-02-16 18:20:44 +0000 | [diff] [blame] | 2640 | |
Richard Smith | dd9459f | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 2641 | return MergeCompatibleFunctionDecls(New, Old, S, MergeTypeWithOld); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2642 | } |
Chris Lattner | e3995fe | 2007-11-06 06:07:26 +0000 | [diff] [blame] | 2643 | |
Douglas Gregor | c837656 | 2009-03-06 22:43:54 +0000 | [diff] [blame] | 2644 | // GNU C permits a K&R definition to follow a prototype declaration |
2645 | // if the declared types of the parameters in the K&R definition | ||||
2646 | // match the types in the prototype declaration, even when the | ||||
2647 | // promoted types of the parameters from the K&R definition differ | ||||
2648 | // from the types in the prototype. GCC then keeps the types from | ||||
2649 | // the prototype. | ||||
Eli Friedman | bc4e29f | 2009-06-01 09:24:59 +0000 | [diff] [blame] | 2650 | // |
2651 | // If a variadic prototype is followed by a non-variadic K&R definition, | ||||
2652 | // the K&R definition becomes variadic. This is sort of an edge case, but | ||||
2653 | // it's legal per the standard depending on how you read C99 6.7.5.3p15 and | ||||
2654 | // C99 6.9.1p8. | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2655 | if (!getLangOpts().CPlusPlus && |
Douglas Gregor | c837656 | 2009-03-06 22:43:54 +0000 | [diff] [blame] | 2656 | Old->hasPrototype() && !New->hasPrototype() && |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2657 | New->getType()->getAs<FunctionProtoType>() && |
Douglas Gregor | c837656 | 2009-03-06 22:43:54 +0000 | [diff] [blame] | 2658 | Old->getNumParams() == New->getNumParams()) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2659 | SmallVector<QualType, 16> ArgTypes; |
2660 | SmallVector<GNUCompatibleParamWarning, 16> Warnings; | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2661 | const FunctionProtoType *OldProto |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2662 | = Old->getType()->getAs<FunctionProtoType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2663 | const FunctionProtoType *NewProto |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2664 | = New->getType()->getAs<FunctionProtoType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2665 | |
Douglas Gregor | c837656 | 2009-03-06 22:43:54 +0000 | [diff] [blame] | 2666 | // Determine whether this is the GNU C extension. |
Eli Friedman | bc4e29f | 2009-06-01 09:24:59 +0000 | [diff] [blame] | 2667 | QualType MergedReturn = Context.mergeTypes(OldProto->getResultType(), |
2668 | NewProto->getResultType()); | ||||
2669 | bool LooseCompatible = !MergedReturn.isNull(); | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2670 | for (unsigned Idx = 0, End = Old->getNumParams(); |
Eli Friedman | bc4e29f | 2009-06-01 09:24:59 +0000 | [diff] [blame] | 2671 | LooseCompatible && Idx != End; ++Idx) { |
Douglas Gregor | c837656 | 2009-03-06 22:43:54 +0000 | [diff] [blame] | 2672 | ParmVarDecl *OldParm = Old->getParamDecl(Idx); |
2673 | ParmVarDecl *NewParm = New->getParamDecl(Idx); | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2674 | if (Context.typesAreCompatible(OldParm->getType(), |
Douglas Gregor | c837656 | 2009-03-06 22:43:54 +0000 | [diff] [blame] | 2675 | NewProto->getArgType(Idx))) { |
2676 | ArgTypes.push_back(NewParm->getType()); | ||||
2677 | } else if (Context.typesAreCompatible(OldParm->getType(), | ||||
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 2678 | NewParm->getType(), |
2679 | /*CompareUnqualified=*/true)) { | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2680 | GNUCompatibleParamWarning Warn |
Douglas Gregor | c837656 | 2009-03-06 22:43:54 +0000 | [diff] [blame] | 2681 | = { OldParm, NewParm, NewProto->getArgType(Idx) }; |
2682 | Warnings.push_back(Warn); | ||||
2683 | ArgTypes.push_back(NewParm->getType()); | ||||
2684 | } else | ||||
Eli Friedman | bc4e29f | 2009-06-01 09:24:59 +0000 | [diff] [blame] | 2685 | LooseCompatible = false; |
Douglas Gregor | c837656 | 2009-03-06 22:43:54 +0000 | [diff] [blame] | 2686 | } |
2687 | |||||
Eli Friedman | bc4e29f | 2009-06-01 09:24:59 +0000 | [diff] [blame] | 2688 | if (LooseCompatible) { |
Douglas Gregor | c837656 | 2009-03-06 22:43:54 +0000 | [diff] [blame] | 2689 | for (unsigned Warn = 0; Warn < Warnings.size(); ++Warn) { |
2690 | Diag(Warnings[Warn].NewParm->getLocation(), | ||||
2691 | diag::ext_param_promoted_not_compatible_with_prototype) | ||||
2692 | << Warnings[Warn].PromotedType | ||||
2693 | << Warnings[Warn].OldParm->getType(); | ||||
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 2694 | if (Warnings[Warn].OldParm->getLocation().isValid()) |
2695 | Diag(Warnings[Warn].OldParm->getLocation(), | ||||
2696 | diag::note_previous_declaration); | ||||
Douglas Gregor | c837656 | 2009-03-06 22:43:54 +0000 | [diff] [blame] | 2697 | } |
2698 | |||||
Richard Smith | dd9459f | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 2699 | if (MergeTypeWithOld) |
2700 | New->setType(Context.getFunctionType(MergedReturn, ArgTypes, | ||||
2701 | OldProto->getExtProtoInfo())); | ||||
2702 | return MergeCompatibleFunctionDecls(New, Old, S, MergeTypeWithOld); | ||||
Douglas Gregor | c837656 | 2009-03-06 22:43:54 +0000 | [diff] [blame] | 2703 | } |
2704 | |||||
2705 | // Fall through to diagnose conflicting types. | ||||
2706 | } | ||||
2707 | |||||
John McCall | 088831d | 2013-04-14 08:50:55 +0000 | [diff] [blame] | 2708 | // A function that has already been declared has been redeclared or |
2709 | // defined with a different type; show an appropriate diagnostic. | ||||
2710 | |||||
2711 | // If the previous declaration was an implicitly-generated builtin | ||||
2712 | // declaration, then at the very least we should use a specialized note. | ||||
2713 | unsigned BuiltinID; | ||||
2714 | if (Old->isImplicit() && (BuiltinID = Old->getBuiltinID())) { | ||||
2715 | // If it's actually a library-defined builtin function like 'malloc' | ||||
2716 | // or 'printf', just warn about the incompatible redeclaration. | ||||
Douglas Gregor | cda9c67 | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 2717 | if (Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)) { |
Douglas Gregor | cda9c67 | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 2718 | Diag(New->getLocation(), diag::warn_redecl_library_builtin) << New; |
2719 | Diag(Old->getLocation(), diag::note_previous_builtin_declaration) | ||||
2720 | << Old << Old->getType(); | ||||
John McCall | 088831d | 2013-04-14 08:50:55 +0000 | [diff] [blame] | 2721 | |
2722 | // If this is a global redeclaration, just forget hereafter | ||||
2723 | // about the "builtin-ness" of the function. | ||||
2724 | // | ||||
2725 | // Doing this for local extern declarations is problematic. If | ||||
2726 | // the builtin declaration remains visible, a second invalid | ||||
2727 | // local declaration will produce a hard error; if it doesn't | ||||
2728 | // remain visible, a single bogus local redeclaration (which is | ||||
2729 | // actually only a warning) could break all the downstream code. | ||||
Richard Smith | a41c97a | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 2730 | if (!New->getLexicalDeclContext()->isFunctionOrMethod()) |
John McCall | 088831d | 2013-04-14 08:50:55 +0000 | [diff] [blame] | 2731 | New->getIdentifier()->setBuiltinID(Builtin::NotBuiltin); |
2732 | |||||
Douglas Gregor | 374e156 | 2009-03-23 17:47:24 +0000 | [diff] [blame] | 2733 | return false; |
Douglas Gregor | cda9c67 | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 2734 | } |
Steve Naroff | 837618c | 2008-01-16 15:01:34 +0000 | [diff] [blame] | 2735 | |
Douglas Gregor | cda9c67 | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 2736 | PrevDiag = diag::note_previous_builtin_declaration; |
2737 | } | ||||
2738 | |||||
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2739 | Diag(New->getLocation(), diag::err_conflicting_types) << New->getDeclName(); |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 2740 | Diag(Old->getLocation(), PrevDiag) << Old << Old->getType(); |
Douglas Gregor | cda9c67 | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 2741 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2742 | } |
2743 | |||||
Douglas Gregor | 04495c8 | 2009-02-24 01:23:02 +0000 | [diff] [blame] | 2744 | /// \brief Completes the merge of two function declarations that are |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2745 | /// known to be compatible. |
Douglas Gregor | 04495c8 | 2009-02-24 01:23:02 +0000 | [diff] [blame] | 2746 | /// |
2747 | /// This routine handles the merging of attributes and other | ||||
2748 | /// properties of function declarations form the old declaration to | ||||
2749 | /// the new declaration, once we know that New is in fact a | ||||
2750 | /// redeclaration of Old. | ||||
2751 | /// | ||||
2752 | /// \returns false | ||||
James Molloy | 9cda03f | 2012-03-13 08:55:35 +0000 | [diff] [blame] | 2753 | bool Sema::MergeCompatibleFunctionDecls(FunctionDecl *New, FunctionDecl *Old, |
Richard Smith | dd9459f | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 2754 | Scope *S, bool MergeTypeWithOld) { |
Douglas Gregor | 04495c8 | 2009-02-24 01:23:02 +0000 | [diff] [blame] | 2755 | // Merge the attributes |
Douglas Gregor | 27c6da2 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 2756 | mergeDeclAttributes(New, Old); |
Douglas Gregor | 04495c8 | 2009-02-24 01:23:02 +0000 | [diff] [blame] | 2757 | |
Douglas Gregor | 04495c8 | 2009-02-24 01:23:02 +0000 | [diff] [blame] | 2758 | // Merge "pure" flag. |
2759 | if (Old->isPure()) | ||||
2760 | New->setPure(); | ||||
2761 | |||||
Rafael Espindola | 8dbf697 | 2012-11-25 14:07:59 +0000 | [diff] [blame] | 2762 | // Merge "used" flag. |
Eli Friedman | 86164e8 | 2013-09-05 00:02:25 +0000 | [diff] [blame] | 2763 | New->setIsUsed(Old->isUsed(false)); |
Rafael Espindola | 8dbf697 | 2012-11-25 14:07:59 +0000 | [diff] [blame] | 2764 | |
John McCall | eca5d22 | 2011-03-02 04:00:57 +0000 | [diff] [blame] | 2765 | // Merge attributes from the parameters. These can mismatch with K&R |
2766 | // declarations. | ||||
2767 | if (New->getNumParams() == Old->getNumParams()) | ||||
2768 | for (unsigned i = 0, e = New->getNumParams(); i != e; ++i) | ||||
2769 | mergeParamDeclAttributes(New->getParamDecl(i), Old->getParamDecl(i), | ||||
Richard Smith | 3a2b7a1 | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 2770 | *this); |
John McCall | eca5d22 | 2011-03-02 04:00:57 +0000 | [diff] [blame] | 2771 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2772 | if (getLangOpts().CPlusPlus) |
James Molloy | 9cda03f | 2012-03-13 08:55:35 +0000 | [diff] [blame] | 2773 | return MergeCXXFunctionDecl(New, Old, S); |
Douglas Gregor | 04495c8 | 2009-02-24 01:23:02 +0000 | [diff] [blame] | 2774 | |
Rafael Espindola | 8b8a09e | 2012-11-29 16:09:03 +0000 | [diff] [blame] | 2775 | // 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] | 2776 | // and argument types. Per C11 6.2.7/4, only update the type if the old decl |
2777 | // was visible. | ||||
Rafael Espindola | 8b8a09e | 2012-11-29 16:09:03 +0000 | [diff] [blame] | 2778 | QualType Merged = Context.mergeTypes(Old->getType(), New->getType()); |
Richard Smith | dd9459f | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 2779 | if (!Merged.isNull() && MergeTypeWithOld) |
Rafael Espindola | 8b8a09e | 2012-11-29 16:09:03 +0000 | [diff] [blame] | 2780 | New->setType(Merged); |
2781 | |||||
Douglas Gregor | 04495c8 | 2009-02-24 01:23:02 +0000 | [diff] [blame] | 2782 | return false; |
2783 | } | ||||
2784 | |||||
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2785 | |
John McCall | eca5d22 | 2011-03-02 04:00:57 +0000 | [diff] [blame] | 2786 | void Sema::mergeObjCMethodDecls(ObjCMethodDecl *newMethod, |
Douglas Gregor | 27c6da2 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 2787 | ObjCMethodDecl *oldMethod) { |
John McCall | 6c2c250 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 2788 | |
Fariborz Jahanian | 1ea6744 | 2012-06-05 21:14:46 +0000 | [diff] [blame] | 2789 | // Merge the attributes, including deprecated/unavailable |
Ted Kremenek | cb34439 | 2013-04-06 00:34:27 +0000 | [diff] [blame] | 2790 | AvailabilityMergeKind MergeKind = |
2791 | isa<ObjCImplDecl>(newMethod->getDeclContext()) ? AMK_Redeclaration | ||||
2792 | : AMK_Override; | ||||
2793 | mergeDeclAttributes(newMethod, oldMethod, MergeKind); | ||||
John McCall | eca5d22 | 2011-03-02 04:00:57 +0000 | [diff] [blame] | 2794 | |
2795 | // Merge attributes from the parameters. | ||||
Douglas Gregor | 0a4a23a | 2012-05-17 23:13:29 +0000 | [diff] [blame] | 2796 | ObjCMethodDecl::param_const_iterator oi = oldMethod->param_begin(), |
2797 | oe = oldMethod->param_end(); | ||||
Argyrios Kyrtzidis | 491306a | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 2798 | for (ObjCMethodDecl::param_iterator |
John McCall | eca5d22 | 2011-03-02 04:00:57 +0000 | [diff] [blame] | 2799 | ni = newMethod->param_begin(), ne = newMethod->param_end(); |
Douglas Gregor | 0a4a23a | 2012-05-17 23:13:29 +0000 | [diff] [blame] | 2800 | ni != ne && oi != oe; ++ni, ++oi) |
Richard Smith | 3a2b7a1 | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 2801 | mergeParamDeclAttributes(*ni, *oi, *this); |
John McCall | 6c2c250 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 2802 | |
Douglas Gregor | f4d918f | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2803 | CheckObjCMethodOverride(newMethod, oldMethod); |
John McCall | eca5d22 | 2011-03-02 04:00:57 +0000 | [diff] [blame] | 2804 | } |
2805 | |||||
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 2806 | /// MergeVarDeclTypes - We parsed a variable 'New' which has the same name and |
2807 | /// 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] | 2808 | /// emitting diagnostics as appropriate. |
2809 | /// | ||||
2810 | /// Declarations using the auto type specifier (C++ [decl.spec.auto]) call back | ||||
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2811 | /// to here in AddInitializerToDecl. We can't check them before the initializer |
2812 | /// is attached. | ||||
Richard Smith | dd9459f | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 2813 | void Sema::MergeVarDeclTypes(VarDecl *New, VarDecl *Old, |
2814 | bool MergeTypeWithOld) { | ||||
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 2815 | if (New->isInvalidDecl() || Old->isInvalidDecl()) |
2816 | return; | ||||
2817 | |||||
2818 | QualType MergedT; | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2819 | if (getLangOpts().CPlusPlus) { |
Richard Smith | dc7a4f5 | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 2820 | if (New->getType()->isUndeducedType()) { |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 2821 | // We don't know what the new type is until the initializer is attached. |
2822 | return; | ||||
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 2823 | } else if (Context.hasSameType(New->getType(), Old->getType())) { |
2824 | // These could still be something that needs exception specs checked. | ||||
2825 | return MergeVarDeclExceptionSpecs(New, Old); | ||||
2826 | } | ||||
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 2827 | // C++ [basic.link]p10: |
2828 | // [...] the types specified by all declarations referring to a given | ||||
2829 | // object or function shall be identical, except that declarations for an | ||||
2830 | // array object can specify array types that differ by the presence or | ||||
2831 | // absence of a major array bound (8.3.4). | ||||
2832 | else if (Old->getType()->isIncompleteArrayType() && | ||||
2833 | New->getType()->isArrayType()) { | ||||
Eli Friedman | 6febf12 | 2012-12-13 01:43:21 +0000 | [diff] [blame] | 2834 | const ArrayType *OldArray = Context.getAsArrayType(Old->getType()); |
2835 | const ArrayType *NewArray = Context.getAsArrayType(New->getType()); | ||||
2836 | if (Context.hasSameType(OldArray->getElementType(), | ||||
2837 | NewArray->getElementType())) | ||||
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 2838 | MergedT = New->getType(); |
2839 | } else if (Old->getType()->isArrayType() && | ||||
Richard Smith | a41c97a | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 2840 | New->getType()->isIncompleteArrayType()) { |
Eli Friedman | 6febf12 | 2012-12-13 01:43:21 +0000 | [diff] [blame] | 2841 | const ArrayType *OldArray = Context.getAsArrayType(Old->getType()); |
2842 | const ArrayType *NewArray = Context.getAsArrayType(New->getType()); | ||||
2843 | if (Context.hasSameType(OldArray->getElementType(), | ||||
2844 | NewArray->getElementType())) | ||||
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 2845 | MergedT = Old->getType(); |
Richard Smith | a41c97a | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 2846 | } else if (New->getType()->isObjCObjectPointerType() && |
2847 | Old->getType()->isObjCObjectPointerType()) { | ||||
2848 | MergedT = Context.mergeObjCGCQualifiers(New->getType(), | ||||
2849 | Old->getType()); | ||||
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 2850 | } |
2851 | } else { | ||||
Richard Smith | a41c97a | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 2852 | // C 6.2.7p2: |
2853 | // All declarations that refer to the same object or function shall have | ||||
2854 | // compatible type. | ||||
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 2855 | MergedT = Context.mergeTypes(New->getType(), Old->getType()); |
2856 | } | ||||
2857 | if (MergedT.isNull()) { | ||||
Richard Smith | dd9459f | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 2858 | // It's OK if we couldn't merge types if either type is dependent, for a |
2859 | // block-scope variable. In other cases (static data members of class | ||||
2860 | // templates, variable templates, ...), we require the types to be | ||||
2861 | // equivalent. | ||||
2862 | // FIXME: The C++ standard doesn't say anything about this. | ||||
2863 | if ((New->getType()->isDependentType() || | ||||
2864 | Old->getType()->isDependentType()) && New->isLocalVarDecl()) { | ||||
2865 | // If the old type was dependent, we can't merge with it, so the new type | ||||
2866 | // becomes dependent for now. We'll reproduce the original type when we | ||||
2867 | // instantiate the TypeSourceInfo for the variable. | ||||
2868 | if (!New->getType()->isDependentType() && MergeTypeWithOld) | ||||
2869 | New->setType(Context.DependentTy); | ||||
2870 | return; | ||||
2871 | } | ||||
2872 | |||||
2873 | // FIXME: Even if this merging succeeds, some other non-visible declaration | ||||
2874 | // of this variable might have an incompatible type. For instance: | ||||
2875 | // | ||||
2876 | // extern int arr[]; | ||||
2877 | // void f() { extern int arr[2]; } | ||||
2878 | // void g() { extern int arr[3]; } | ||||
2879 | // | ||||
2880 | // Neither C nor C++ requires a diagnostic for this, but we should still try | ||||
2881 | // to diagnose it. | ||||
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 2882 | Diag(New->getLocation(), diag::err_redefinition_different_type) |
David Blaikie | a405b25 | 2012-09-20 18:38:57 +0000 | [diff] [blame] | 2883 | << New->getDeclName() << New->getType() << Old->getType(); |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 2884 | Diag(Old->getLocation(), diag::note_previous_definition); |
2885 | return New->setInvalidDecl(); | ||||
2886 | } | ||||
John McCall | 5b8740f | 2013-04-01 18:34:28 +0000 | [diff] [blame] | 2887 | |
2888 | // 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] | 2889 | // declaration was an extern declaration in a different scope. |
Richard Smith | dd9459f | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 2890 | if (MergeTypeWithOld) |
John McCall | 5b8740f | 2013-04-01 18:34:28 +0000 | [diff] [blame] | 2891 | New->setType(MergedT); |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 2892 | } |
2893 | |||||
Richard Smith | 99a7238 | 2013-09-03 21:00:58 +0000 | [diff] [blame] | 2894 | static bool mergeTypeWithPrevious(Sema &S, VarDecl *NewVD, VarDecl *OldVD, |
2895 | LookupResult &Previous) { | ||||
2896 | // C11 6.2.7p4: | ||||
2897 | // For an identifier with internal or external linkage declared | ||||
2898 | // in a scope in which a prior declaration of that identifier is | ||||
2899 | // visible, if the prior declaration specifies internal or | ||||
2900 | // external linkage, the type of the identifier at the later | ||||
2901 | // declaration becomes the composite type. | ||||
2902 | // | ||||
2903 | // If the variable isn't visible, we do not merge with its type. | ||||
2904 | if (Previous.isShadowed()) | ||||
2905 | return false; | ||||
2906 | |||||
2907 | if (S.getLangOpts().CPlusPlus) { | ||||
2908 | // C++11 [dcl.array]p3: | ||||
2909 | // If there is a preceding declaration of the entity in the same | ||||
2910 | // scope in which the bound was specified, an omitted array bound | ||||
2911 | // is taken to be the same as in that earlier declaration. | ||||
2912 | return NewVD->isPreviousDeclInSameBlockScope() || | ||||
2913 | (!OldVD->getLexicalDeclContext()->isFunctionOrMethod() && | ||||
2914 | !NewVD->getLexicalDeclContext()->isFunctionOrMethod()); | ||||
2915 | } else { | ||||
2916 | // If the old declaration was function-local, don't merge with its | ||||
2917 | // type unless we're in the same function. | ||||
2918 | return !OldVD->getLexicalDeclContext()->isFunctionOrMethod() || | ||||
2919 | OldVD->getLexicalDeclContext() == NewVD->getLexicalDeclContext(); | ||||
2920 | } | ||||
2921 | } | ||||
2922 | |||||
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2923 | /// MergeVarDecl - We just parsed a variable 'New' which has the same name |
2924 | /// and scope as a previous declaration 'Old'. Figure out how to resolve this | ||||
2925 | /// situation, merging decls or emitting diagnostics as appropriate. | ||||
2926 | /// | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2927 | /// Tentative definition rules (C99 6.9.2p2) are checked by |
2928 | /// FinalizeDeclaratorGroup. Unfortunately, we can't analyze tentative | ||||
Steve Naroff | ff9eb1f | 2008-08-08 17:50:35 +0000 | [diff] [blame] | 2929 | /// definitions here, since the initializer hasn't been attached. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2930 | /// |
Richard Smith | 99a7238 | 2013-09-03 21:00:58 +0000 | [diff] [blame] | 2931 | void Sema::MergeVarDecl(VarDecl *New, LookupResult &Previous) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 2932 | // If the new decl is already invalid, don't do any other checking. |
2933 | if (New->isInvalidDecl()) | ||||
2934 | return; | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2935 | |
Larisse Voufo | 4a91989 | 2013-08-14 03:09:19 +0000 | [diff] [blame] | 2936 | // Verify the old decl was also a variable or variable template. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 2937 | VarDecl *Old = 0; |
Larisse Voufo | 4a91989 | 2013-08-14 03:09:19 +0000 | [diff] [blame] | 2938 | if (Previous.isSingleResult() && |
2939 | (Old = dyn_cast<VarDecl>(Previous.getFoundDecl()))) { | ||||
Larisse Voufo | 567f917 | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 2940 | if (New->getDescribedVarTemplate()) |
Larisse Voufo | 4a91989 | 2013-08-14 03:09:19 +0000 | [diff] [blame] | 2941 | Old = Old->getDescribedVarTemplate() ? Old : 0; |
2942 | else | ||||
2943 | Old = Old->getDescribedVarTemplate() ? 0 : Old; | ||||
2944 | } | ||||
2945 | if (!Old) { | ||||
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 2946 | Diag(New->getLocation(), diag::err_redefinition_different_kind) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 2947 | << New->getDeclName(); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 2948 | Diag(Previous.getRepresentativeDecl()->getLocation(), |
2949 | diag::note_previous_definition); | ||||
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 2950 | return New->setInvalidDecl(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2951 | } |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 2952 | |
Rafael Espindola | 90cc390 | 2013-04-15 12:49:13 +0000 | [diff] [blame] | 2953 | if (!shouldLinkPossiblyHiddenDecl(Old, New)) |
2954 | return; | ||||
2955 | |||||
Douglas Gregor | 7f6ff02 | 2010-08-30 14:32:14 +0000 | [diff] [blame] | 2956 | // C++ [class.mem]p1: |
2957 | // A member shall not be declared twice in the member-specification [...] | ||||
2958 | // | ||||
2959 | // Here, we need only consider static data members. | ||||
2960 | if (Old->isStaticDataMember() && !New->isOutOfLine()) { | ||||
2961 | Diag(New->getLocation(), diag::err_duplicate_member) | ||||
2962 | << New->getIdentifier(); | ||||
2963 | Diag(Old->getLocation(), diag::note_previous_declaration); | ||||
2964 | New->setInvalidDecl(); | ||||
2965 | } | ||||
2966 | |||||
Douglas Gregor | 27c6da2 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 2967 | mergeDeclAttributes(New, Old); |
David Blaikie | d662a79 | 2011-10-19 22:56:21 +0000 | [diff] [blame] | 2968 | // Warn if an already-declared variable is made a weak_import in a subsequent |
2969 | // declaration | ||||
Fariborz Jahanian | ab27d6e | 2011-06-20 17:50:03 +0000 | [diff] [blame] | 2970 | if (New->getAttr<WeakImportAttr>() && |
2971 | Old->getStorageClass() == SC_None && | ||||
Fariborz Jahanian | d543130 | 2011-06-22 22:08:50 +0000 | [diff] [blame] | 2972 | !Old->getAttr<WeakImportAttr>()) { |
2973 | Diag(New->getLocation(), diag::warn_weak_import) << New->getDeclName(); | ||||
2974 | Diag(Old->getLocation(), diag::note_previous_definition); | ||||
2975 | // Remove weak_import attribute on new declaration. | ||||
Fariborz Jahanian | c3ca14d | 2011-06-23 17:50:10 +0000 | [diff] [blame] | 2976 | New->dropAttr<WeakImportAttr>(); |
Fariborz Jahanian | d543130 | 2011-06-22 22:08:50 +0000 | [diff] [blame] | 2977 | } |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 2978 | |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 2979 | // Merge the types. |
Richard Smith | 99a7238 | 2013-09-03 21:00:58 +0000 | [diff] [blame] | 2980 | MergeVarDeclTypes(New, Old, mergeTypeWithPrevious(*this, New, Old, Previous)); |
2981 | |||||
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 2982 | if (New->isInvalidDecl()) |
2983 | return; | ||||
Douglas Gregor | 656de63 | 2009-03-11 23:52:16 +0000 | [diff] [blame] | 2984 | |
Rafael Espindola | ea4b111 | 2013-04-04 21:21:25 +0000 | [diff] [blame] | 2985 | // [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] | 2986 | if (New->getStorageClass() == SC_Static && |
Rafael Espindola | ea4b111 | 2013-04-04 21:21:25 +0000 | [diff] [blame] | 2987 | !New->isStaticDataMember() && |
Rafael Espindola | 181e3ec | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 2988 | Old->hasExternalFormalLinkage()) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2989 | Diag(New->getLocation(), diag::err_static_non_static) << New->getDeclName(); |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 2990 | Diag(Old->getLocation(), diag::note_previous_definition); |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 2991 | return New->setInvalidDecl(); |
Steve Naroff | b7b032e | 2008-01-30 00:44:01 +0000 | [diff] [blame] | 2992 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2993 | // C99 6.2.2p4: |
Douglas Gregor | 5ef122e | 2009-03-19 22:01:50 +0000 | [diff] [blame] | 2994 | // For an identifier declared with the storage-class specifier |
2995 | // extern in a scope in which a prior declaration of that | ||||
2996 | // identifier is visible,23) if the prior declaration specifies | ||||
2997 | // internal or external linkage, the linkage of the identifier at | ||||
2998 | // the later declaration is the same as the linkage specified at | ||||
2999 | // the prior declaration. If no prior declaration is visible, or | ||||
3000 | // if the prior declaration specifies no linkage, then the | ||||
3001 | // identifier has external linkage. | ||||
Douglas Gregor | 38179b2 | 2009-03-23 16:17:01 +0000 | [diff] [blame] | 3002 | if (New->hasExternalStorage() && Old->hasLinkage()) |
Douglas Gregor | 5ef122e | 2009-03-19 22:01:50 +0000 | [diff] [blame] | 3003 | /* Okay */; |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 3004 | else if (New->getCanonicalDecl()->getStorageClass() != SC_Static && |
Rafael Espindola | ea4b111 | 2013-04-04 21:21:25 +0000 | [diff] [blame] | 3005 | !New->isStaticDataMember() && |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 3006 | Old->getCanonicalDecl()->getStorageClass() == SC_Static) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3007 | Diag(New->getLocation(), diag::err_non_static_static) << New->getDeclName(); |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 3008 | Diag(Old->getLocation(), diag::note_previous_definition); |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 3009 | return New->setInvalidDecl(); |
Steve Naroff | b7b032e | 2008-01-30 00:44:01 +0000 | [diff] [blame] | 3010 | } |
Daniel Dunbar | 5466c7b | 2009-04-14 02:25:56 +0000 | [diff] [blame] | 3011 | |
Argyrios Kyrtzidis | 6684d85 | 2011-01-31 07:04:46 +0000 | [diff] [blame] | 3012 | // Check if extern is followed by non-extern and vice-versa. |
3013 | if (New->hasExternalStorage() && | ||||
3014 | !Old->hasLinkage() && Old->isLocalVarDecl()) { | ||||
3015 | Diag(New->getLocation(), diag::err_extern_non_extern) << New->getDeclName(); | ||||
3016 | Diag(Old->getLocation(), diag::note_previous_definition); | ||||
3017 | return New->setInvalidDecl(); | ||||
3018 | } | ||||
Rafael Espindola | 80a8689 | 2013-04-04 02:47:57 +0000 | [diff] [blame] | 3019 | if (Old->hasLinkage() && New->isLocalVarDecl() && |
3020 | !New->hasExternalStorage()) { | ||||
Argyrios Kyrtzidis | 6684d85 | 2011-01-31 07:04:46 +0000 | [diff] [blame] | 3021 | Diag(New->getLocation(), diag::err_non_extern_extern) << New->getDeclName(); |
3022 | Diag(Old->getLocation(), diag::note_previous_definition); | ||||
3023 | return New->setInvalidDecl(); | ||||
3024 | } | ||||
3025 | |||||
Steve Naroff | 094cefb | 2008-09-17 14:05:40 +0000 | [diff] [blame] | 3026 | // Variables with external linkage are analyzed in FinalizeDeclaratorGroup. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3027 | |
Daniel Dunbar | 5466c7b | 2009-04-14 02:25:56 +0000 | [diff] [blame] | 3028 | // FIXME: The test for external storage here seems wrong? We still |
3029 | // need to check for mismatches. | ||||
3030 | if (!New->hasExternalStorage() && !New->isFileVarDecl() && | ||||
Douglas Gregor | 656de63 | 2009-03-11 23:52:16 +0000 | [diff] [blame] | 3031 | // Don't complain about out-of-line definitions of static members. |
3032 | !(Old->getLexicalDeclContext()->isRecord() && | ||||
3033 | !New->getLexicalDeclContext()->isRecord())) { | ||||
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 3034 | Diag(New->getLocation(), diag::err_redefinition) << New->getDeclName(); |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 3035 | Diag(Old->getLocation(), diag::note_previous_definition); |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 3036 | return New->setInvalidDecl(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3037 | } |
Douglas Gregor | 275a369 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 3038 | |
Richard Smith | 38afbc7 | 2013-04-13 02:43:54 +0000 | [diff] [blame] | 3039 | if (New->getTLSKind() != Old->getTLSKind()) { |
3040 | if (!Old->getTLSKind()) { | ||||
3041 | Diag(New->getLocation(), diag::err_thread_non_thread) << New->getDeclName(); | ||||
3042 | Diag(Old->getLocation(), diag::note_previous_declaration); | ||||
3043 | } else if (!New->getTLSKind()) { | ||||
3044 | Diag(New->getLocation(), diag::err_non_thread_thread) << New->getDeclName(); | ||||
3045 | Diag(Old->getLocation(), diag::note_previous_declaration); | ||||
3046 | } else { | ||||
3047 | // Do not allow redeclaration to change the variable between requiring | ||||
3048 | // static and dynamic initialization. | ||||
3049 | // FIXME: GCC allows this, but uses the TLS keyword on the first | ||||
3050 | // declaration to determine the kind. Do we need to be compatible here? | ||||
3051 | Diag(New->getLocation(), diag::err_thread_thread_different_kind) | ||||
3052 | << New->getDeclName() << (New->getTLSKind() == VarDecl::TLS_Dynamic); | ||||
3053 | Diag(Old->getLocation(), diag::note_previous_declaration); | ||||
3054 | } | ||||
Eli Friedman | 63054b3 | 2009-04-19 20:27:55 +0000 | [diff] [blame] | 3055 | } |
3056 | |||||
Sebastian Redl | 4cae1b3 | 2010-02-02 18:35:11 +0000 | [diff] [blame] | 3057 | // C++ doesn't have tentative definitions, so go right ahead and check here. |
3058 | const VarDecl *Def; | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3059 | if (getLangOpts().CPlusPlus && |
Sebastian Redl | 6c048a9 | 2010-02-03 02:08:48 +0000 | [diff] [blame] | 3060 | New->isThisDeclarationADefinition() == VarDecl::Definition && |
Sebastian Redl | 4cae1b3 | 2010-02-02 18:35:11 +0000 | [diff] [blame] | 3061 | (Def = Old->getDefinition())) { |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3062 | Diag(New->getLocation(), diag::err_redefinition) << New; |
Sebastian Redl | 4cae1b3 | 2010-02-02 18:35:11 +0000 | [diff] [blame] | 3063 | Diag(Def->getLocation(), diag::note_previous_definition); |
3064 | New->setInvalidDecl(); | ||||
3065 | return; | ||||
3066 | } | ||||
Rafael Espindola | e57e3d3 | 2012-12-27 03:56:20 +0000 | [diff] [blame] | 3067 | |
Rafael Espindola | 950fee2 | 2013-02-14 01:18:37 +0000 | [diff] [blame] | 3068 | if (haveIncompatibleLanguageLinkages(Old, New)) { |
Rafael Espindola | e57e3d3 | 2012-12-27 03:56:20 +0000 | [diff] [blame] | 3069 | Diag(New->getLocation(), diag::err_different_language_linkage) << New; |
3070 | Diag(Old->getLocation(), diag::note_previous_definition); | ||||
3071 | New->setInvalidDecl(); | ||||
3072 | return; | ||||
3073 | } | ||||
3074 | |||||
Rafael Espindola | 8dbf697 | 2012-11-25 14:07:59 +0000 | [diff] [blame] | 3075 | // Merge "used" flag. |
Eli Friedman | 86164e8 | 2013-09-05 00:02:25 +0000 | [diff] [blame] | 3076 | New->setIsUsed(Old->isUsed(false)); |
Rafael Espindola | 8dbf697 | 2012-11-25 14:07:59 +0000 | [diff] [blame] | 3077 | |
Douglas Gregor | 275a369 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 3078 | // Keep a chain of previous declarations. |
3079 | New->setPreviousDeclaration(Old); | ||||
John McCall | 46460a6 | 2010-01-20 21:53:11 +0000 | [diff] [blame] | 3080 | |
3081 | // Inherit access appropriately. | ||||
3082 | New->setAccess(Old->getAccess()); | ||||
Larisse Voufo | 567f917 | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 3083 | |
3084 | if (VarTemplateDecl *VTD = New->getDescribedVarTemplate()) { | ||||
3085 | if (New->isStaticDataMember() && New->isOutOfLine()) | ||||
3086 | VTD->setAccess(New->getAccess()); | ||||
3087 | } | ||||
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3088 | } |
3089 | |||||
3090 | /// ParsedFreeStandingDeclSpec - This method is invoked when a declspec with | ||||
3091 | /// no declarator (e.g. "struct foo;") is parsed. | ||||
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3092 | Decl *Sema::ParsedFreeStandingDeclSpec(Scope *S, AccessSpecifier AS, |
John McCall | ac4df24 | 2011-03-22 23:00:04 +0000 | [diff] [blame] | 3093 | DeclSpec &DS) { |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 3094 | return ParsedFreeStandingDeclSpec(S, AS, DS, MultiTemplateParamsArg()); |
Chandler Carruth | 0f4be74 | 2011-05-03 18:35:10 +0000 | [diff] [blame] | 3095 | } |
3096 | |||||
Eli Friedman | 5e867c8 | 2013-07-10 00:30:46 +0000 | [diff] [blame] | 3097 | static void HandleTagNumbering(Sema &S, const TagDecl *Tag) { |
Reid Kleckner | 942f9fe | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 3098 | if (!S.Context.getLangOpts().CPlusPlus) |
3099 | return; | ||||
3100 | |||||
Eli Friedman | 5e867c8 | 2013-07-10 00:30:46 +0000 | [diff] [blame] | 3101 | if (isa<CXXRecordDecl>(Tag->getParent())) { |
3102 | // If this tag is the direct child of a class, number it if | ||||
3103 | // it is anonymous. | ||||
3104 | if (!Tag->getName().empty() || Tag->getTypedefNameForAnonDecl()) | ||||
3105 | return; | ||||
3106 | MangleNumberingContext &MCtx = | ||||
3107 | S.Context.getManglingNumberContext(Tag->getParent()); | ||||
3108 | S.Context.setManglingNumber(Tag, MCtx.getManglingNumber(Tag)); | ||||
3109 | return; | ||||
3110 | } | ||||
3111 | |||||
3112 | // If this tag isn't a direct child of a class, number it if it is local. | ||||
3113 | Decl *ManglingContextDecl; | ||||
3114 | if (MangleNumberingContext *MCtx = | ||||
3115 | S.getCurrentMangleNumberContext(Tag->getDeclContext(), | ||||
3116 | ManglingContextDecl)) { | ||||
3117 | S.Context.setManglingNumber(Tag, MCtx->getManglingNumber(Tag)); | ||||
3118 | } | ||||
3119 | } | ||||
3120 | |||||
Chandler Carruth | 0f4be74 | 2011-05-03 18:35:10 +0000 | [diff] [blame] | 3121 | /// ParsedFreeStandingDeclSpec - This method is invoked when a declspec with |
Richard Smith | c7f8116 | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 3122 | /// no declarator (e.g. "struct foo;") is parsed. It also accepts template |
Chandler Carruth | 0f4be74 | 2011-05-03 18:35:10 +0000 | [diff] [blame] | 3123 | /// parameters to cope with template friend declarations. |
3124 | Decl *Sema::ParsedFreeStandingDeclSpec(Scope *S, AccessSpecifier AS, | ||||
3125 | DeclSpec &DS, | ||||
Richard Smith | c7f8116 | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 3126 | MultiTemplateParamsArg TemplateParams, |
3127 | bool IsExplicitInstantiation) { | ||||
John McCall | e3af023 | 2009-10-07 23:34:25 +0000 | [diff] [blame] | 3128 | Decl *TagD = 0; |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 3129 | TagDecl *Tag = 0; |
3130 | if (DS.getTypeSpecType() == DeclSpec::TST_class || | ||||
3131 | DS.getTypeSpecType() == DeclSpec::TST_struct || | ||||
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 3132 | DS.getTypeSpecType() == DeclSpec::TST_interface || |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 3133 | DS.getTypeSpecType() == DeclSpec::TST_union || |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 3134 | DS.getTypeSpecType() == DeclSpec::TST_enum) { |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3135 | TagD = DS.getRepAsDecl(); |
John McCall | e3af023 | 2009-10-07 23:34:25 +0000 | [diff] [blame] | 3136 | |
3137 | if (!TagD) // We probably had an error | ||||
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3138 | return 0; |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 3139 | |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 3140 | // Note that the above type specs guarantee that the |
3141 | // type rep is a Decl, whereas in many of the others | ||||
3142 | // it's a Type. | ||||
Peter Collingbourne | 0661bd0c | 2011-10-23 17:07:16 +0000 | [diff] [blame] | 3143 | if (isa<TagDecl>(TagD)) |
3144 | Tag = cast<TagDecl>(TagD); | ||||
3145 | else if (ClassTemplateDecl *CTD = dyn_cast<ClassTemplateDecl>(TagD)) | ||||
3146 | Tag = CTD->getTemplatedDecl(); | ||||
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 3147 | } |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 3148 | |
Argyrios Kyrtzidis | 216f78b | 2012-03-09 20:10:30 +0000 | [diff] [blame] | 3149 | if (Tag) { |
Eli Friedman | 5e867c8 | 2013-07-10 00:30:46 +0000 | [diff] [blame] | 3150 | HandleTagNumbering(*this, Tag); |
Argyrios Kyrtzidis | 717a20b | 2011-09-30 22:11:31 +0000 | [diff] [blame] | 3151 | Tag->setFreeStanding(); |
Argyrios Kyrtzidis | 216f78b | 2012-03-09 20:10:30 +0000 | [diff] [blame] | 3152 | if (Tag->isInvalidDecl()) |
3153 | return Tag; | ||||
3154 | } | ||||
Argyrios Kyrtzidis | 717a20b | 2011-09-30 22:11:31 +0000 | [diff] [blame] | 3155 | |
Nuno Lopes | 0a8bab0 | 2009-12-17 11:35:26 +0000 | [diff] [blame] | 3156 | if (unsigned TypeQuals = DS.getTypeQualifiers()) { |
3157 | // Enforce C99 6.7.3p2: "Types other than pointer types derived from object | ||||
3158 | // or incomplete types shall not be restrict-qualified." | ||||
3159 | if (TypeQuals & DeclSpec::TQ_restrict) | ||||
3160 | Diag(DS.getRestrictSpecLoc(), | ||||
3161 | diag::err_typecheck_invalid_restrict_not_pointer_noarg) | ||||
3162 | << DS.getSourceRange(); | ||||
3163 | } | ||||
3164 | |||||
Richard Smith | af1fc7a | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 3165 | if (DS.isConstexprSpecified()) { |
3166 | // C++0x [dcl.constexpr]p1: constexpr can only be applied to declarations | ||||
3167 | // and definitions of functions and variables. | ||||
3168 | if (Tag) | ||||
3169 | Diag(DS.getConstexprSpecLoc(), diag::err_constexpr_tag) | ||||
3170 | << (DS.getTypeSpecType() == DeclSpec::TST_class ? 0 : | ||||
3171 | DS.getTypeSpecType() == DeclSpec::TST_struct ? 1 : | ||||
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 3172 | DS.getTypeSpecType() == DeclSpec::TST_interface ? 2 : |
3173 | DS.getTypeSpecType() == DeclSpec::TST_union ? 3 : 4); | ||||
Richard Smith | af1fc7a | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 3174 | else |
3175 | Diag(DS.getConstexprSpecLoc(), diag::err_constexpr_no_declarators); | ||||
3176 | // Don't emit warnings after this error. | ||||
3177 | return TagD; | ||||
3178 | } | ||||
3179 | |||||
Richard Smith | c7f8116 | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 3180 | DiagnoseFunctionSpecifiers(DS); |
3181 | |||||
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 3182 | if (DS.isFriendSpecified()) { |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 3183 | // If we're dealing with a decl but not a TagDecl, assume that |
3184 | // whatever routines created it handled the friendship aspect. | ||||
3185 | if (TagD && !Tag) | ||||
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3186 | return 0; |
Chandler Carruth | 0f4be74 | 2011-05-03 18:35:10 +0000 | [diff] [blame] | 3187 | return ActOnFriendTypeDecl(S, DS, TemplateParams); |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 3188 | } |
John McCall | ac4df24 | 2011-03-22 23:00:04 +0000 | [diff] [blame] | 3189 | |
Richard Smith | c7f8116 | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 3190 | CXXScopeSpec &SS = DS.getTypeSpecScope(); |
3191 | bool IsExplicitSpecialization = | ||||
3192 | !TemplateParams.empty() && TemplateParams.back()->size() == 0; | ||||
3193 | if (Tag && SS.isNotEmpty() && !Tag->isCompleteDefinition() && | ||||
3194 | !IsExplicitInstantiation && !IsExplicitSpecialization) { | ||||
3195 | // Per C++ [dcl.type.elab]p1, a class declaration cannot have a | ||||
3196 | // nested-name-specifier unless it is an explicit instantiation | ||||
3197 | // or an explicit specialization. | ||||
3198 | // Per C++ [dcl.enum]p1, an opaque-enum-declaration can't either. | ||||
3199 | Diag(SS.getBeginLoc(), diag::err_standalone_class_nested_name_specifier) | ||||
3200 | << (DS.getTypeSpecType() == DeclSpec::TST_class ? 0 : | ||||
3201 | DS.getTypeSpecType() == DeclSpec::TST_struct ? 1 : | ||||
3202 | DS.getTypeSpecType() == DeclSpec::TST_interface ? 2 : | ||||
3203 | DS.getTypeSpecType() == DeclSpec::TST_union ? 3 : 4) | ||||
3204 | << SS.getRange(); | ||||
3205 | return 0; | ||||
3206 | } | ||||
3207 | |||||
3208 | // Track whether this decl-specifier declares anything. | ||||
3209 | bool DeclaresAnything = true; | ||||
3210 | |||||
3211 | // Handle anonymous struct definitions. | ||||
Douglas Gregor | 4920f1f | 2009-01-12 22:49:06 +0000 | [diff] [blame] | 3212 | if (RecordDecl *Record = dyn_cast_or_null<RecordDecl>(Tag)) { |
John McCall | 5e1cdac | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 3213 | if (!Record->getDeclName() && Record->isCompleteDefinition() && |
Douglas Gregor | a71c129 | 2009-03-06 23:06:59 +0000 | [diff] [blame] | 3214 | DS.getStorageClassSpec() != DeclSpec::SCS_typedef) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3215 | if (getLangOpts().CPlusPlus || |
Douglas Gregor | a71c129 | 2009-03-06 23:06:59 +0000 | [diff] [blame] | 3216 | Record->getDeclContext()->isRecord()) |
John McCall | aec0371 | 2010-05-21 20:45:30 +0000 | [diff] [blame] | 3217 | return BuildAnonymousStructOrUnion(S, DS, AS, Record); |
Douglas Gregor | a71c129 | 2009-03-06 23:06:59 +0000 | [diff] [blame] | 3218 | |
Richard Smith | c7f8116 | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 3219 | DeclaresAnything = false; |
Douglas Gregor | a71c129 | 2009-03-06 23:06:59 +0000 | [diff] [blame] | 3220 | } |
Francois Pichet | 8e161ed | 2010-11-23 06:07:27 +0000 | [diff] [blame] | 3221 | } |
Douglas Gregor | 4920f1f | 2009-01-12 22:49:06 +0000 | [diff] [blame] | 3222 | |
Richard Smith | c7f8116 | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 3223 | // Check for Microsoft C extension: anonymous struct member. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3224 | if (getLangOpts().MicrosoftExt && !getLangOpts().CPlusPlus && |
Francois Pichet | 8e161ed | 2010-11-23 06:07:27 +0000 | [diff] [blame] | 3225 | CurContext->isRecord() && |
3226 | DS.getStorageClassSpec() == DeclSpec::SCS_unspecified) { | ||||
3227 | // Handle 2 kinds of anonymous struct: | ||||
3228 | // struct STRUCT; | ||||
3229 | // and | ||||
3230 | // STRUCT_TYPE; <- where STRUCT_TYPE is a typedef struct. | ||||
3231 | RecordDecl *Record = dyn_cast_or_null<RecordDecl>(Tag); | ||||
John McCall | 5e1cdac | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 3232 | if ((Record && Record->getDeclName() && !Record->isCompleteDefinition()) || |
Francois Pichet | 8e161ed | 2010-11-23 06:07:27 +0000 | [diff] [blame] | 3233 | (DS.getTypeSpecType() == DeclSpec::TST_typename && |
3234 | DS.getRepAsType().get()->isStructureType())) { | ||||
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3235 | Diag(DS.getLocStart(), diag::ext_ms_anonymous_struct) |
Francois Pichet | 8e161ed | 2010-11-23 06:07:27 +0000 | [diff] [blame] | 3236 | << DS.getSourceRange(); |
3237 | return BuildMicrosoftCAnonymousStruct(S, DS, Record); | ||||
3238 | } | ||||
Douglas Gregor | 4920f1f | 2009-01-12 22:49:06 +0000 | [diff] [blame] | 3239 | } |
Richard Smith | c7f8116 | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 3240 | |
3241 | // Skip all the checks below if we have a type error. | ||||
3242 | if (DS.getTypeSpecType() == DeclSpec::TST_error || | ||||
3243 | (TagD && TagD->isInvalidDecl())) | ||||
3244 | return TagD; | ||||
3245 | |||||
3246 | if (getLangOpts().CPlusPlus && | ||||
Douglas Gregor | a131d0f | 2010-07-13 06:24:26 +0000 | [diff] [blame] | 3247 | DS.getStorageClassSpec() != DeclSpec::SCS_typedef) |
3248 | if (EnumDecl *Enum = dyn_cast_or_null<EnumDecl>(Tag)) | ||||
3249 | if (Enum->enumerator_begin() == Enum->enumerator_end() && | ||||
Richard Smith | c7f8116 | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 3250 | !Enum->getIdentifier() && !Enum->isInvalidDecl()) |
3251 | DeclaresAnything = false; | ||||
John McCall | ac4df24 | 2011-03-22 23:00:04 +0000 | [diff] [blame] | 3252 | |
John McCall | ac4df24 | 2011-03-22 23:00:04 +0000 | [diff] [blame] | 3253 | if (!DS.isMissingDeclaratorOk()) { |
Richard Smith | c7f8116 | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 3254 | // Customize diagnostic for a typedef missing a name. |
3255 | if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) | ||||
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3256 | Diag(DS.getLocStart(), diag::ext_typedef_without_a_name) |
Douglas Gregor | a0ebd60 | 2010-07-16 15:40:40 +0000 | [diff] [blame] | 3257 | << DS.getSourceRange(); |
Richard Smith | c7f8116 | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 3258 | else |
3259 | DeclaresAnything = false; | ||||
Sebastian Redl | a4ed0d8 | 2008-12-28 15:28:59 +0000 | [diff] [blame] | 3260 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3261 | |
Richard Smith | c7f8116 | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 3262 | if (DS.isModulePrivateSpecified() && |
Douglas Gregor | e389585 | 2011-09-12 18:37:38 +0000 | [diff] [blame] | 3263 | Tag && Tag->getDeclContext()->isFunctionOrMethod()) |
3264 | Diag(DS.getModulePrivateSpecLoc(), diag::err_module_private_local_class) | ||||
3265 | << Tag->getTagKind() | ||||
3266 | << FixItHint::CreateRemoval(DS.getModulePrivateSpecLoc()); | ||||
3267 | |||||
Richard Smith | c7f8116 | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 3268 | ActOnDocumentableDecl(TagD); |
3269 | |||||
3270 | // C 6.7/2: | ||||
3271 | // A declaration [...] shall declare at least a declarator [...], a tag, | ||||
3272 | // or the members of an enumeration. | ||||
3273 | // C++ [dcl.dcl]p3: | ||||
3274 | // [If there are no declarators], and except for the declaration of an | ||||
3275 | // unnamed bit-field, the decl-specifier-seq shall introduce one or more | ||||
3276 | // names into the program, or shall redeclare a name introduced by a | ||||
3277 | // previous declaration. | ||||
3278 | if (!DeclaresAnything) { | ||||
3279 | // In C, we allow this as a (popular) extension / bug. Don't bother | ||||
3280 | // producing further diagnostics for redundant qualifiers after this. | ||||
3281 | Diag(DS.getLocStart(), diag::ext_no_declarators) << DS.getSourceRange(); | ||||
3282 | return TagD; | ||||
3283 | } | ||||
3284 | |||||
3285 | // C++ [dcl.stc]p1: | ||||
3286 | // If a storage-class-specifier appears in a decl-specifier-seq, [...] the | ||||
3287 | // init-declarator-list of the declaration shall not be empty. | ||||
3288 | // C++ [dcl.fct.spec]p1: | ||||
3289 | // If a cv-qualifier appears in a decl-specifier-seq, the | ||||
3290 | // init-declarator-list of the declaration shall not be empty. | ||||
3291 | // | ||||
3292 | // Spurious qualifiers here appear to be valid in C. | ||||
3293 | unsigned DiagID = diag::warn_standalone_specifier; | ||||
3294 | if (getLangOpts().CPlusPlus) | ||||
3295 | DiagID = diag::ext_standalone_specifier; | ||||
3296 | |||||
3297 | // Note that a linkage-specification sets a storage class, but | ||||
3298 | // 'extern "C" struct foo;' is actually valid and not theoretically | ||||
3299 | // useless. | ||||
3300 | if (DeclSpec::SCS SCS = DS.getStorageClassSpec()) | ||||
3301 | if (!DS.isExternInLinkageSpec() && SCS != DeclSpec::SCS_typedef) | ||||
3302 | Diag(DS.getStorageClassSpecLoc(), DiagID) | ||||
3303 | << DeclSpec::getSpecifierName(SCS); | ||||
3304 | |||||
Richard Smith | ec64244 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 3305 | if (DeclSpec::TSCS TSCS = DS.getThreadStorageClassSpec()) |
3306 | Diag(DS.getThreadStorageClassSpecLoc(), DiagID) | ||||
3307 | << DeclSpec::getSpecifierName(TSCS); | ||||
Richard Smith | c7f8116 | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 3308 | if (DS.getTypeQualifiers()) { |
3309 | if (DS.getTypeQualifiers() & DeclSpec::TQ_const) | ||||
3310 | Diag(DS.getConstSpecLoc(), DiagID) << "const"; | ||||
3311 | if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile) | ||||
3312 | Diag(DS.getConstSpecLoc(), DiagID) << "volatile"; | ||||
3313 | // Restrict is covered above. | ||||
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 3314 | if (DS.getTypeQualifiers() & DeclSpec::TQ_atomic) |
3315 | Diag(DS.getAtomicSpecLoc(), DiagID) << "_Atomic"; | ||||
Richard Smith | c7f8116 | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 3316 | } |
3317 | |||||
Eli Friedman | fc038e9 | 2011-12-17 00:36:09 +0000 | [diff] [blame] | 3318 | // Warn about ignored type attributes, for example: |
3319 | // __attribute__((aligned)) struct A; | ||||
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3320 | // Attributes should be placed after tag to apply to type declaration. |
Eli Friedman | fc038e9 | 2011-12-17 00:36:09 +0000 | [diff] [blame] | 3321 | if (!DS.getAttributes().empty()) { |
3322 | DeclSpec::TST TypeSpecType = DS.getTypeSpecType(); | ||||
3323 | if (TypeSpecType == DeclSpec::TST_class || | ||||
3324 | TypeSpecType == DeclSpec::TST_struct || | ||||
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 3325 | TypeSpecType == DeclSpec::TST_interface || |
Eli Friedman | fc038e9 | 2011-12-17 00:36:09 +0000 | [diff] [blame] | 3326 | TypeSpecType == DeclSpec::TST_union || |
3327 | TypeSpecType == DeclSpec::TST_enum) { | ||||
3328 | AttributeList* attrs = DS.getAttributes().getList(); | ||||
3329 | while (attrs) { | ||||
Michael Han | 45bed13 | 2012-10-04 16:42:52 +0000 | [diff] [blame] | 3330 | Diag(attrs->getLoc(), diag::warn_declspec_attribute_ignored) |
Eli Friedman | fc038e9 | 2011-12-17 00:36:09 +0000 | [diff] [blame] | 3331 | << attrs->getName() |
3332 | << (TypeSpecType == DeclSpec::TST_class ? 0 : | ||||
3333 | TypeSpecType == DeclSpec::TST_struct ? 1 : | ||||
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 3334 | TypeSpecType == DeclSpec::TST_union ? 2 : |
3335 | TypeSpecType == DeclSpec::TST_interface ? 3 : 4); | ||||
Eli Friedman | fc038e9 | 2011-12-17 00:36:09 +0000 | [diff] [blame] | 3336 | attrs = attrs->getNext(); |
3337 | } | ||||
3338 | } | ||||
3339 | } | ||||
John McCall | ac4df24 | 2011-03-22 23:00:04 +0000 | [diff] [blame] | 3340 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3341 | return TagD; |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3342 | } |
3343 | |||||
John McCall | 1d7c528 | 2009-12-18 10:40:03 +0000 | [diff] [blame] | 3344 | /// We are trying to inject an anonymous member into the given scope; |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 3345 | /// check if there's an existing declaration that can't be overloaded. |
3346 | /// | ||||
3347 | /// \return true if this is a forbidden redeclaration | ||||
John McCall | 1d7c528 | 2009-12-18 10:40:03 +0000 | [diff] [blame] | 3348 | static bool CheckAnonMemberRedeclaration(Sema &SemaRef, |
3349 | Scope *S, | ||||
Fariborz Jahanian | 588a4ad | 2010-01-22 18:30:17 +0000 | [diff] [blame] | 3350 | DeclContext *Owner, |
John McCall | 1d7c528 | 2009-12-18 10:40:03 +0000 | [diff] [blame] | 3351 | DeclarationName Name, |
3352 | SourceLocation NameLoc, | ||||
3353 | unsigned diagnostic) { | ||||
3354 | LookupResult R(SemaRef, Name, NameLoc, Sema::LookupMemberName, | ||||
3355 | Sema::ForRedeclaration); | ||||
3356 | if (!SemaRef.LookupName(R, S)) return false; | ||||
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 3357 | |
John McCall | 1d7c528 | 2009-12-18 10:40:03 +0000 | [diff] [blame] | 3358 | if (R.getAsSingle<TagDecl>()) |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 3359 | return false; |
3360 | |||||
3361 | // Pick a representative declaration. | ||||
John McCall | 1d7c528 | 2009-12-18 10:40:03 +0000 | [diff] [blame] | 3362 | NamedDecl *PrevDecl = R.getRepresentativeDecl()->getUnderlyingDecl(); |
Argyrios Kyrtzidis | 2b64239 | 2010-09-23 14:26:01 +0000 | [diff] [blame] | 3363 | assert(PrevDecl && "Expected a non-null Decl"); |
3364 | |||||
3365 | if (!SemaRef.isDeclInScope(PrevDecl, Owner, S)) | ||||
3366 | return false; | ||||
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 3367 | |
John McCall | 1d7c528 | 2009-12-18 10:40:03 +0000 | [diff] [blame] | 3368 | SemaRef.Diag(NameLoc, diagnostic) << Name; |
3369 | SemaRef.Diag(PrevDecl->getLocation(), diag::note_previous_declaration); | ||||
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 3370 | |
3371 | return true; | ||||
3372 | } | ||||
3373 | |||||
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3374 | /// InjectAnonymousStructOrUnionMembers - Inject the members of the |
3375 | /// anonymous struct or union AnonRecord into the owning context Owner | ||||
3376 | /// and scope S. This routine will be invoked just after we realize | ||||
3377 | /// that an unnamed union or struct is actually an anonymous union or | ||||
3378 | /// struct, e.g., | ||||
3379 | /// | ||||
3380 | /// @code | ||||
3381 | /// union { | ||||
3382 | /// int i; | ||||
3383 | /// float f; | ||||
3384 | /// }; // InjectAnonymousStructOrUnionMembers called here to inject i and | ||||
3385 | /// // f into the surrounding scope.x | ||||
3386 | /// @endcode | ||||
3387 | /// | ||||
3388 | /// This routine is recursive, injecting the names of nested anonymous | ||||
3389 | /// structs/unions into the owning context and scope as well. | ||||
John McCall | aec0371 | 2010-05-21 20:45:30 +0000 | [diff] [blame] | 3390 | static bool InjectAnonymousStructOrUnionMembers(Sema &SemaRef, Scope *S, |
Craig Topper | 6b9240e | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 3391 | DeclContext *Owner, |
3392 | RecordDecl *AnonRecord, | ||||
3393 | AccessSpecifier AS, | ||||
3394 | SmallVectorImpl<NamedDecl *> &Chaining, | ||||
3395 | bool MSAnonStruct) { | ||||
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 3396 | unsigned diagKind |
3397 | = AnonRecord->isUnion() ? diag::err_anonymous_union_member_redecl | ||||
3398 | : diag::err_anonymous_struct_member_redecl; | ||||
3399 | |||||
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3400 | bool Invalid = false; |
Francois Pichet | 8e161ed | 2010-11-23 06:07:27 +0000 | [diff] [blame] | 3401 | |
3402 | // Look every FieldDecl and IndirectFieldDecl with a name. | ||||
3403 | for (RecordDecl::decl_iterator D = AnonRecord->decls_begin(), | ||||
3404 | DEnd = AnonRecord->decls_end(); | ||||
3405 | D != DEnd; ++D) { | ||||
3406 | if ((isa<FieldDecl>(*D) || isa<IndirectFieldDecl>(*D)) && | ||||
3407 | cast<NamedDecl>(*D)->getDeclName()) { | ||||
3408 | ValueDecl *VD = cast<ValueDecl>(*D); | ||||
3409 | if (CheckAnonMemberRedeclaration(SemaRef, S, Owner, VD->getDeclName(), | ||||
3410 | VD->getLocation(), diagKind)) { | ||||
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3411 | // C++ [class.union]p2: |
3412 | // The names of the members of an anonymous union shall be | ||||
3413 | // distinct from the names of any other entity in the | ||||
3414 | // scope in which the anonymous union is declared. | ||||
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3415 | Invalid = true; |
3416 | } else { | ||||
3417 | // C++ [class.union]p2: | ||||
3418 | // For the purpose of name lookup, after the anonymous union | ||||
3419 | // definition, the members of the anonymous union are | ||||
3420 | // considered to have been defined in the scope in which the | ||||
3421 | // anonymous union is declared. | ||||
Francois Pichet | 8e161ed | 2010-11-23 06:07:27 +0000 | [diff] [blame] | 3422 | unsigned OldChainingSize = Chaining.size(); |
3423 | if (IndirectFieldDecl *IF = dyn_cast<IndirectFieldDecl>(VD)) | ||||
3424 | for (IndirectFieldDecl::chain_iterator PI = IF->chain_begin(), | ||||
3425 | PE = IF->chain_end(); PI != PE; ++PI) | ||||
3426 | Chaining.push_back(*PI); | ||||
3427 | else | ||||
3428 | Chaining.push_back(VD); | ||||
3429 | |||||
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 3430 | assert(Chaining.size() >= 2); |
3431 | NamedDecl **NamedChain = | ||||
3432 | new (SemaRef.Context)NamedDecl*[Chaining.size()]; | ||||
3433 | for (unsigned i = 0; i < Chaining.size(); i++) | ||||
3434 | NamedChain[i] = Chaining[i]; | ||||
3435 | |||||
3436 | IndirectFieldDecl* IndirectField = | ||||
Francois Pichet | 8e161ed | 2010-11-23 06:07:27 +0000 | [diff] [blame] | 3437 | IndirectFieldDecl::Create(SemaRef.Context, Owner, VD->getLocation(), |
3438 | VD->getIdentifier(), VD->getType(), | ||||
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 3439 | NamedChain, Chaining.size()); |
3440 | |||||
3441 | IndirectField->setAccess(AS); | ||||
3442 | IndirectField->setImplicit(); | ||||
3443 | SemaRef.PushOnScopeChains(IndirectField, S); | ||||
John McCall | aec0371 | 2010-05-21 20:45:30 +0000 | [diff] [blame] | 3444 | |
3445 | // That includes picking up the appropriate access specifier. | ||||
Francois Pichet | 8e161ed | 2010-11-23 06:07:27 +0000 | [diff] [blame] | 3446 | if (AS != AS_none) IndirectField->setAccess(AS); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 3447 | |
Francois Pichet | 8e161ed | 2010-11-23 06:07:27 +0000 | [diff] [blame] | 3448 | Chaining.resize(OldChainingSize); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3449 | } |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3450 | } |
3451 | } | ||||
3452 | |||||
3453 | return Invalid; | ||||
3454 | } | ||||
3455 | |||||
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 3456 | /// StorageClassSpecToVarDeclStorageClass - Maps a DeclSpec::SCS to |
3457 | /// a VarDecl::StorageClass. Any error reporting is up to the caller: | ||||
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 3458 | /// illegal input values are mapped to SC_None. |
3459 | static StorageClass | ||||
Rafael Espindola | 65dfa2b | 2013-04-25 12:11:36 +0000 | [diff] [blame] | 3460 | StorageClassSpecToVarDeclStorageClass(const DeclSpec &DS) { |
3461 | DeclSpec::SCS StorageClassSpec = DS.getStorageClassSpec(); | ||||
3462 | assert(StorageClassSpec != DeclSpec::SCS_typedef && | ||||
3463 | "Parser allowed 'typedef' as storage class VarDecl."); | ||||
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 3464 | switch (StorageClassSpec) { |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 3465 | case DeclSpec::SCS_unspecified: return SC_None; |
Rafael Espindola | 65dfa2b | 2013-04-25 12:11:36 +0000 | [diff] [blame] | 3466 | case DeclSpec::SCS_extern: |
3467 | if (DS.isExternInLinkageSpec()) | ||||
3468 | return SC_None; | ||||
3469 | return SC_Extern; | ||||
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 3470 | case DeclSpec::SCS_static: return SC_Static; |
3471 | case DeclSpec::SCS_auto: return SC_Auto; | ||||
3472 | case DeclSpec::SCS_register: return SC_Register; | ||||
3473 | case DeclSpec::SCS_private_extern: return SC_PrivateExtern; | ||||
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 3474 | // Illegal SCSs map to None: error reporting is up to the caller. |
3475 | case DeclSpec::SCS_mutable: // Fall through. | ||||
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 3476 | case DeclSpec::SCS_typedef: return SC_None; |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 3477 | } |
3478 | llvm_unreachable("unknown storage class specifier"); | ||||
3479 | } | ||||
3480 | |||||
Francois Pichet | 8e161ed | 2010-11-23 06:07:27 +0000 | [diff] [blame] | 3481 | /// BuildAnonymousStructOrUnion - Handle the declaration of an |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3482 | /// anonymous structure or union. Anonymous unions are a C++ feature |
Hans Wennborg | acbabf1 | 2012-02-03 15:47:04 +0000 | [diff] [blame] | 3483 | /// (C++ [class.union]) and a C11 feature; anonymous structures |
3484 | /// are a C11 feature and GNU C++ extension. | ||||
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3485 | Decl *Sema::BuildAnonymousStructOrUnion(Scope *S, DeclSpec &DS, |
3486 | AccessSpecifier AS, | ||||
3487 | RecordDecl *Record) { | ||||
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3488 | DeclContext *Owner = Record->getDeclContext(); |
3489 | |||||
3490 | // Diagnose whether this anonymous struct/union is an extension. | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3491 | if (Record->isUnion() && !getLangOpts().CPlusPlus && !getLangOpts().C11) |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3492 | Diag(Record->getLocation(), diag::ext_anonymous_union); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3493 | else if (!Record->isUnion() && getLangOpts().CPlusPlus) |
Hans Wennborg | acbabf1 | 2012-02-03 15:47:04 +0000 | [diff] [blame] | 3494 | Diag(Record->getLocation(), diag::ext_gnu_anonymous_struct); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3495 | else if (!Record->isUnion() && !getLangOpts().C11) |
Hans Wennborg | acbabf1 | 2012-02-03 15:47:04 +0000 | [diff] [blame] | 3496 | Diag(Record->getLocation(), diag::ext_c11_anonymous_struct); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3497 | |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3498 | // C and C++ require different kinds of checks for anonymous |
3499 | // structs/unions. | ||||
3500 | bool Invalid = false; | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3501 | if (getLangOpts().CPlusPlus) { |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3502 | const char* PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3503 | unsigned DiagID; |
David Blaikie | 2b79c32 | 2011-10-19 22:43:29 +0000 | [diff] [blame] | 3504 | if (Record->isUnion()) { |
3505 | // C++ [class.union]p6: | ||||
3506 | // Anonymous unions declared in a named namespace or in the | ||||
3507 | // global namespace shall be declared static. | ||||
3508 | if (DS.getStorageClassSpec() != DeclSpec::SCS_static && | ||||
3509 | (isa<TranslationUnitDecl>(Owner) || | ||||
3510 | (isa<NamespaceDecl>(Owner) && | ||||
3511 | cast<NamespaceDecl>(Owner)->getDeclName()))) { | ||||
David Blaikie | 82c8ca1 | 2011-10-20 02:49:08 +0000 | [diff] [blame] | 3512 | Diag(Record->getLocation(), diag::err_anonymous_union_not_static) |
3513 | << FixItHint::CreateInsertion(Record->getLocation(), "static "); | ||||
David Blaikie | 2b79c32 | 2011-10-19 22:43:29 +0000 | [diff] [blame] | 3514 | |
3515 | // Recover by adding 'static'. | ||||
3516 | DS.SetStorageClassSpec(*this, DeclSpec::SCS_static, SourceLocation(), | ||||
3517 | PrevSpec, DiagID); | ||||
3518 | } | ||||
3519 | // C++ [class.union]p6: | ||||
3520 | // A storage class is not allowed in a declaration of an | ||||
3521 | // anonymous union in a class scope. | ||||
3522 | else if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified && | ||||
3523 | isa<RecordDecl>(Owner)) { | ||||
3524 | Diag(DS.getStorageClassSpecLoc(), | ||||
David Blaikie | f6f876c | 2011-10-20 02:10:55 +0000 | [diff] [blame] | 3525 | diag::err_anonymous_union_with_storage_spec) |
3526 | << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc()); | ||||
David Blaikie | 2b79c32 | 2011-10-19 22:43:29 +0000 | [diff] [blame] | 3527 | |
3528 | // Recover by removing the storage specifier. | ||||
David Blaikie | d662a79 | 2011-10-19 22:56:21 +0000 | [diff] [blame] | 3529 | DS.SetStorageClassSpec(*this, DeclSpec::SCS_unspecified, |
3530 | SourceLocation(), | ||||
David Blaikie | 2b79c32 | 2011-10-19 22:43:29 +0000 | [diff] [blame] | 3531 | PrevSpec, DiagID); |
3532 | } | ||||
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3533 | } |
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 3534 | |
Douglas Gregor | 7604f64 | 2011-05-09 23:05:33 +0000 | [diff] [blame] | 3535 | // Ignore const/volatile/restrict qualifiers. |
3536 | if (DS.getTypeQualifiers()) { | ||||
3537 | if (DS.getTypeQualifiers() & DeclSpec::TQ_const) | ||||
3538 | Diag(DS.getConstSpecLoc(), diag::ext_anonymous_struct_union_qualified) | ||||
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 3539 | << Record->isUnion() << "const" |
Douglas Gregor | 7604f64 | 2011-05-09 23:05:33 +0000 | [diff] [blame] | 3540 | << FixItHint::CreateRemoval(DS.getConstSpecLoc()); |
3541 | if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile) | ||||
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 3542 | Diag(DS.getVolatileSpecLoc(), |
David Blaikie | d662a79 | 2011-10-19 22:56:21 +0000 | [diff] [blame] | 3543 | diag::ext_anonymous_struct_union_qualified) |
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 3544 | << Record->isUnion() << "volatile" |
Douglas Gregor | 7604f64 | 2011-05-09 23:05:33 +0000 | [diff] [blame] | 3545 | << FixItHint::CreateRemoval(DS.getVolatileSpecLoc()); |
3546 | if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict) | ||||
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 3547 | Diag(DS.getRestrictSpecLoc(), |
David Blaikie | d662a79 | 2011-10-19 22:56:21 +0000 | [diff] [blame] | 3548 | diag::ext_anonymous_struct_union_qualified) |
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 3549 | << Record->isUnion() << "restrict" |
Douglas Gregor | 7604f64 | 2011-05-09 23:05:33 +0000 | [diff] [blame] | 3550 | << FixItHint::CreateRemoval(DS.getRestrictSpecLoc()); |
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 3551 | if (DS.getTypeQualifiers() & DeclSpec::TQ_atomic) |
3552 | Diag(DS.getAtomicSpecLoc(), | ||||
3553 | diag::ext_anonymous_struct_union_qualified) | ||||
3554 | << Record->isUnion() << "_Atomic" | ||||
3555 | << FixItHint::CreateRemoval(DS.getAtomicSpecLoc()); | ||||
Douglas Gregor | 7604f64 | 2011-05-09 23:05:33 +0000 | [diff] [blame] | 3556 | |
3557 | DS.ClearTypeQualifiers(); | ||||
3558 | } | ||||
3559 | |||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3560 | // C++ [class.union]p2: |
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 3561 | // The member-specification of an anonymous union shall only |
3562 | // define non-static data members. [Note: nested types and | ||||
3563 | // functions cannot be declared within an anonymous union. ] | ||||
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3564 | for (DeclContext::decl_iterator Mem = Record->decls_begin(), |
3565 | MemEnd = Record->decls_end(); | ||||
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 3566 | Mem != MemEnd; ++Mem) { |
3567 | if (FieldDecl *FD = dyn_cast<FieldDecl>(*Mem)) { | ||||
3568 | // C++ [class.union]p3: | ||||
3569 | // An anonymous union shall not have private or protected | ||||
3570 | // members (clause 11). | ||||
John McCall | aec0371 | 2010-05-21 20:45:30 +0000 | [diff] [blame] | 3571 | assert(FD->getAccess() != AS_none); |
3572 | if (FD->getAccess() != AS_public) { | ||||
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 3573 | Diag(FD->getLocation(), diag::err_anonymous_record_nonpublic_member) |
3574 | << (int)Record->isUnion() << (int)(FD->getAccess() == AS_protected); | ||||
3575 | Invalid = true; | ||||
3576 | } | ||||
Argyrios Kyrtzidis | dd7744d | 2010-08-16 17:27:08 +0000 | [diff] [blame] | 3577 | |
Sean Hunt | cf34e75 | 2011-05-16 22:41:40 +0000 | [diff] [blame] | 3578 | // C++ [class.union]p1 |
3579 | // An object of a class with a non-trivial constructor, a non-trivial | ||||
3580 | // copy constructor, a non-trivial destructor, or a non-trivial copy | ||||
3581 | // assignment operator cannot be a member of a union, nor can an | ||||
3582 | // array of such objects. | ||||
Richard Smith | e7d7c39 | 2011-10-19 20:41:51 +0000 | [diff] [blame] | 3583 | if (CheckNontrivialField(FD)) |
Argyrios Kyrtzidis | dd7744d | 2010-08-16 17:27:08 +0000 | [diff] [blame] | 3584 | Invalid = true; |
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 3585 | } else if ((*Mem)->isImplicit()) { |
3586 | // Any implicit members are fine. | ||||
Douglas Gregor | 1931b44 | 2009-02-03 00:34:39 +0000 | [diff] [blame] | 3587 | } else if (isa<TagDecl>(*Mem) && (*Mem)->getDeclContext() != Record) { |
3588 | // This is a type that showed up in an | ||||
3589 | // elaborated-type-specifier inside the anonymous struct or | ||||
3590 | // union, but which actually declares a type outside of the | ||||
3591 | // anonymous struct or union. It's okay. | ||||
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 3592 | } else if (RecordDecl *MemRecord = dyn_cast<RecordDecl>(*Mem)) { |
3593 | if (!MemRecord->isAnonymousStructOrUnion() && | ||||
3594 | MemRecord->getDeclName()) { | ||||
Francois Pichet | 538e0d0 | 2010-09-08 11:32:25 +0000 | [diff] [blame] | 3595 | // Visual C++ allows type definition in anonymous struct or union. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3596 | if (getLangOpts().MicrosoftExt) |
Francois Pichet | 538e0d0 | 2010-09-08 11:32:25 +0000 | [diff] [blame] | 3597 | Diag(MemRecord->getLocation(), diag::ext_anonymous_record_with_type) |
3598 | << (int)Record->isUnion(); | ||||
3599 | else { | ||||
3600 | // This is a nested type declaration. | ||||
3601 | Diag(MemRecord->getLocation(), diag::err_anonymous_record_with_type) | ||||
3602 | << (int)Record->isUnion(); | ||||
3603 | Invalid = true; | ||||
3604 | } | ||||
Richard Smith | c5f7d6a | 2013-01-28 00:54:05 +0000 | [diff] [blame] | 3605 | } else { |
3606 | // This is an anonymous type definition within another anonymous type. | ||||
3607 | // This is a popular extension, provided by Plan9, MSVC and GCC, but | ||||
3608 | // not part of standard C++. | ||||
3609 | Diag(MemRecord->getLocation(), | ||||
Richard Smith | f270519 | 2013-01-31 03:11:12 +0000 | [diff] [blame] | 3610 | diag::ext_anonymous_record_with_anonymous_type) |
3611 | << (int)Record->isUnion(); | ||||
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 3612 | } |
Abramo Bagnara | 6206d53 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 3613 | } else if (isa<AccessSpecDecl>(*Mem)) { |
3614 | // Any access specifier is fine. | ||||
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 3615 | } else { |
3616 | // We have something that isn't a non-static data | ||||
3617 | // member. Complain about it. | ||||
3618 | unsigned DK = diag::err_anonymous_record_bad_member; | ||||
3619 | if (isa<TypeDecl>(*Mem)) | ||||
3620 | DK = diag::err_anonymous_record_with_type; | ||||
3621 | else if (isa<FunctionDecl>(*Mem)) | ||||
3622 | DK = diag::err_anonymous_record_with_function; | ||||
3623 | else if (isa<VarDecl>(*Mem)) | ||||
3624 | DK = diag::err_anonymous_record_with_static; | ||||
Francois Pichet | 538e0d0 | 2010-09-08 11:32:25 +0000 | [diff] [blame] | 3625 | |
3626 | // Visual C++ allows type definition in anonymous struct or union. | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3627 | if (getLangOpts().MicrosoftExt && |
Francois Pichet | 538e0d0 | 2010-09-08 11:32:25 +0000 | [diff] [blame] | 3628 | DK == diag::err_anonymous_record_with_type) |
3629 | Diag((*Mem)->getLocation(), diag::ext_anonymous_record_with_type) | ||||
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 3630 | << (int)Record->isUnion(); |
Francois Pichet | 538e0d0 | 2010-09-08 11:32:25 +0000 | [diff] [blame] | 3631 | else { |
3632 | Diag((*Mem)->getLocation(), DK) | ||||
3633 | << (int)Record->isUnion(); | ||||
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 3634 | Invalid = true; |
Francois Pichet | 538e0d0 | 2010-09-08 11:32:25 +0000 | [diff] [blame] | 3635 | } |
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 3636 | } |
3637 | } | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3638 | } |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3639 | |
3640 | if (!Record->isUnion() && !Owner->isRecord()) { | ||||
Douglas Gregor | 4920f1f | 2009-01-12 22:49:06 +0000 | [diff] [blame] | 3641 | Diag(Record->getLocation(), diag::err_anonymous_struct_not_member) |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3642 | << (int)getLangOpts().CPlusPlus; |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3643 | Invalid = true; |
3644 | } | ||||
3645 | |||||
John McCall | eb692e0 | 2009-10-22 23:31:08 +0000 | [diff] [blame] | 3646 | // Mock up a declarator. |
Argyrios Kyrtzidis | d3880f8 | 2011-06-28 03:01:18 +0000 | [diff] [blame] | 3647 | Declarator Dc(DS, Declarator::MemberContext); |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 3648 | TypeSourceInfo *TInfo = GetTypeForDeclarator(Dc, S); |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3649 | assert(TInfo && "couldn't build declarator info for anonymous struct/union"); |
John McCall | eb692e0 | 2009-10-22 23:31:08 +0000 | [diff] [blame] | 3650 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3651 | // Create a declaration for this anonymous struct/union. |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 3652 | NamedDecl *Anon = 0; |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3653 | if (RecordDecl *OwningClass = dyn_cast<RecordDecl>(Owner)) { |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3654 | Anon = FieldDecl::Create(Context, OwningClass, |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3655 | DS.getLocStart(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3656 | Record->getLocation(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3657 | /*IdentifierInfo=*/0, |
Argyrios Kyrtzidis | a5d8200 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 3658 | Context.getTypeDeclType(Record), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3659 | TInfo, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3660 | /*BitWidth=*/0, /*Mutable=*/false, |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 3661 | /*InitStyle=*/ICIS_NoInit); |
John McCall | aec0371 | 2010-05-21 20:45:30 +0000 | [diff] [blame] | 3662 | Anon->setAccess(AS); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3663 | if (getLangOpts().CPlusPlus) |
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 3664 | FieldCollector->Add(cast<FieldDecl>(Anon)); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3665 | } else { |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 3666 | DeclSpec::SCS SCSpec = DS.getStorageClassSpec(); |
Rafael Espindola | 65dfa2b | 2013-04-25 12:11:36 +0000 | [diff] [blame] | 3667 | VarDecl::StorageClass SC = StorageClassSpecToVarDeclStorageClass(DS); |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 3668 | if (SCSpec == DeclSpec::SCS_mutable) { |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3669 | // mutable can only appear on non-static class members, so it's always |
3670 | // an error here | ||||
3671 | Diag(Record->getLocation(), diag::err_mutable_nonmember); | ||||
3672 | Invalid = true; | ||||
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 3673 | SC = SC_None; |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3674 | } |
3675 | |||||
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3676 | Anon = VarDecl::Create(Context, Owner, |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3677 | DS.getLocStart(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3678 | Record->getLocation(), /*IdentifierInfo=*/0, |
Argyrios Kyrtzidis | a5d8200 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 3679 | Context.getTypeDeclType(Record), |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 3680 | TInfo, SC); |
Richard Smith | 16ee819 | 2011-09-18 00:06:34 +0000 | [diff] [blame] | 3681 | |
3682 | // Default-initialize the implicit variable. This initialization will be | ||||
3683 | // trivial in almost all cases, except if a union member has an in-class | ||||
3684 | // initializer: | ||||
3685 | // union { int n = 0; }; | ||||
3686 | ActOnUninitializedDecl(Anon, /*TypeMayContainAuto=*/false); | ||||
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3687 | } |
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 3688 | Anon->setImplicit(); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3689 | |
3690 | // Add the anonymous struct/union object to the current | ||||
3691 | // context. We'll be referencing this object when we refer to one of | ||||
3692 | // its members. | ||||
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3693 | Owner->addDecl(Anon); |
Douglas Gregor | fe60f84 | 2010-05-03 15:18:25 +0000 | [diff] [blame] | 3694 | |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3695 | // Inject the members of the anonymous struct/union into the owning |
3696 | // context and into the identifier resolver chain for name lookup | ||||
3697 | // purposes. | ||||
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3698 | SmallVector<NamedDecl*, 2> Chain; |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 3699 | Chain.push_back(Anon); |
3700 | |||||
Francois Pichet | 8e161ed | 2010-11-23 06:07:27 +0000 | [diff] [blame] | 3701 | if (InjectAnonymousStructOrUnionMembers(*this, S, Owner, Record, AS, |
3702 | Chain, false)) | ||||
Douglas Gregor | 4920f1f | 2009-01-12 22:49:06 +0000 | [diff] [blame] | 3703 | Invalid = true; |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3704 | |
3705 | // Mark this as an anonymous struct/union type. Note that we do not | ||||
3706 | // do this until after we have already checked and injected the | ||||
3707 | // members of this anonymous struct/union type, because otherwise | ||||
3708 | // the members could be injected twice: once by DeclContext when it | ||||
3709 | // builds its lookup table, and once by | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3710 | // InjectAnonymousStructOrUnionMembers. |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3711 | Record->setAnonymousStructOrUnion(true); |
3712 | |||||
3713 | if (Invalid) | ||||
3714 | Anon->setInvalidDecl(); | ||||
3715 | |||||
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3716 | return Anon; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3717 | } |
3718 | |||||
Francois Pichet | 8e161ed | 2010-11-23 06:07:27 +0000 | [diff] [blame] | 3719 | /// BuildMicrosoftCAnonymousStruct - Handle the declaration of an |
3720 | /// Microsoft C anonymous structure. | ||||
3721 | /// Ref: http://msdn.microsoft.com/en-us/library/z2cx9y4f.aspx | ||||
3722 | /// Example: | ||||
3723 | /// | ||||
3724 | /// struct A { int a; }; | ||||
3725 | /// struct B { struct A; int b; }; | ||||
3726 | /// | ||||
3727 | /// void foo() { | ||||
3728 | /// B var; | ||||
3729 | /// var.a = 3; | ||||
3730 | /// } | ||||
3731 | /// | ||||
3732 | Decl *Sema::BuildMicrosoftCAnonymousStruct(Scope *S, DeclSpec &DS, | ||||
3733 | RecordDecl *Record) { | ||||
3734 | |||||
3735 | // If there is no Record, get the record via the typedef. | ||||
3736 | if (!Record) | ||||
3737 | Record = DS.getRepAsType().get()->getAsStructureType()->getDecl(); | ||||
3738 | |||||
3739 | // Mock up a declarator. | ||||
3740 | Declarator Dc(DS, Declarator::TypeNameContext); | ||||
3741 | TypeSourceInfo *TInfo = GetTypeForDeclarator(Dc, S); | ||||
3742 | assert(TInfo && "couldn't build declarator info for anonymous struct"); | ||||
3743 | |||||
3744 | // Create a declaration for this anonymous struct. | ||||
3745 | NamedDecl* Anon = FieldDecl::Create(Context, | ||||
3746 | cast<RecordDecl>(CurContext), | ||||
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3747 | DS.getLocStart(), |
3748 | DS.getLocStart(), | ||||
Francois Pichet | 8e161ed | 2010-11-23 06:07:27 +0000 | [diff] [blame] | 3749 | /*IdentifierInfo=*/0, |
3750 | Context.getTypeDeclType(Record), | ||||
3751 | TInfo, | ||||
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3752 | /*BitWidth=*/0, /*Mutable=*/false, |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 3753 | /*InitStyle=*/ICIS_NoInit); |
Francois Pichet | 8e161ed | 2010-11-23 06:07:27 +0000 | [diff] [blame] | 3754 | Anon->setImplicit(); |
3755 | |||||
3756 | // Add the anonymous struct object to the current context. | ||||
3757 | CurContext->addDecl(Anon); | ||||
3758 | |||||
3759 | // Inject the members of the anonymous struct into the current | ||||
3760 | // context and into the identifier resolver chain for name lookup | ||||
3761 | // purposes. | ||||
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3762 | SmallVector<NamedDecl*, 2> Chain; |
Francois Pichet | 8e161ed | 2010-11-23 06:07:27 +0000 | [diff] [blame] | 3763 | Chain.push_back(Anon); |
3764 | |||||
Nico Weber | ee625af | 2012-02-01 00:41:00 +0000 | [diff] [blame] | 3765 | RecordDecl *RecordDef = Record->getDefinition(); |
3766 | if (!RecordDef || InjectAnonymousStructOrUnionMembers(*this, S, CurContext, | ||||
3767 | RecordDef, AS_none, | ||||
3768 | Chain, true)) | ||||
Francois Pichet | 8e161ed | 2010-11-23 06:07:27 +0000 | [diff] [blame] | 3769 | Anon->setInvalidDecl(); |
3770 | |||||
3771 | return Anon; | ||||
3772 | } | ||||
Steve Naroff | f009063 | 2007-09-02 02:04:30 +0000 | [diff] [blame] | 3773 | |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 3774 | /// GetNameForDeclarator - Determine the full declaration name for the |
3775 | /// given Declarator. | ||||
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3776 | DeclarationNameInfo Sema::GetNameForDeclarator(Declarator &D) { |
Douglas Gregor | 02a24ee | 2009-11-03 16:56:39 +0000 | [diff] [blame] | 3777 | return GetNameFromUnqualifiedId(D.getName()); |
3778 | } | ||||
3779 | |||||
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3780 | /// \brief Retrieves the declaration name from a parsed unqualified-id. |
3781 | DeclarationNameInfo | ||||
3782 | Sema::GetNameFromUnqualifiedId(const UnqualifiedId &Name) { | ||||
3783 | DeclarationNameInfo NameInfo; | ||||
3784 | NameInfo.setLoc(Name.StartLocation); | ||||
3785 | |||||
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 3786 | switch (Name.getKind()) { |
Sean Hunt | 0486d74 | 2009-11-28 04:44:28 +0000 | [diff] [blame] | 3787 | |
Fariborz Jahanian | 98a5403 | 2011-07-12 17:16:56 +0000 | [diff] [blame] | 3788 | case UnqualifiedId::IK_ImplicitSelfParam: |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3789 | case UnqualifiedId::IK_Identifier: |
3790 | NameInfo.setName(Name.Identifier); | ||||
3791 | NameInfo.setLoc(Name.StartLocation); | ||||
3792 | return NameInfo; | ||||
Sean Hunt | 0486d74 | 2009-11-28 04:44:28 +0000 | [diff] [blame] | 3793 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3794 | case UnqualifiedId::IK_OperatorFunctionId: |
3795 | NameInfo.setName(Context.DeclarationNames.getCXXOperatorName( | ||||
3796 | Name.OperatorFunctionId.Operator)); | ||||
3797 | NameInfo.setLoc(Name.StartLocation); | ||||
3798 | NameInfo.getInfo().CXXOperatorName.BeginOpNameLoc | ||||
3799 | = Name.OperatorFunctionId.SymbolLocations[0]; | ||||
3800 | NameInfo.getInfo().CXXOperatorName.EndOpNameLoc | ||||
3801 | = Name.EndLocation.getRawEncoding(); | ||||
3802 | return NameInfo; | ||||
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3803 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3804 | case UnqualifiedId::IK_LiteralOperatorId: |
3805 | NameInfo.setName(Context.DeclarationNames.getCXXLiteralOperatorName( | ||||
3806 | Name.Identifier)); | ||||
3807 | NameInfo.setLoc(Name.StartLocation); | ||||
3808 | NameInfo.setCXXLiteralOperatorNameLoc(Name.EndLocation); | ||||
3809 | return NameInfo; | ||||
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3810 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3811 | case UnqualifiedId::IK_ConversionFunctionId: { |
3812 | TypeSourceInfo *TInfo; | ||||
3813 | QualType Ty = GetTypeFromParser(Name.ConversionFunctionId, &TInfo); | ||||
3814 | if (Ty.isNull()) | ||||
3815 | return DeclarationNameInfo(); | ||||
3816 | NameInfo.setName(Context.DeclarationNames.getCXXConversionFunctionName( | ||||
3817 | Context.getCanonicalType(Ty))); | ||||
3818 | NameInfo.setLoc(Name.StartLocation); | ||||
3819 | NameInfo.setNamedTypeInfo(TInfo); | ||||
3820 | return NameInfo; | ||||
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 3821 | } |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3822 | |
3823 | case UnqualifiedId::IK_ConstructorName: { | ||||
3824 | TypeSourceInfo *TInfo; | ||||
3825 | QualType Ty = GetTypeFromParser(Name.ConstructorName, &TInfo); | ||||
3826 | if (Ty.isNull()) | ||||
3827 | return DeclarationNameInfo(); | ||||
3828 | NameInfo.setName(Context.DeclarationNames.getCXXConstructorName( | ||||
3829 | Context.getCanonicalType(Ty))); | ||||
3830 | NameInfo.setLoc(Name.StartLocation); | ||||
3831 | NameInfo.setNamedTypeInfo(TInfo); | ||||
3832 | return NameInfo; | ||||
3833 | } | ||||
3834 | |||||
3835 | case UnqualifiedId::IK_ConstructorTemplateId: { | ||||
3836 | // In well-formed code, we can only have a constructor | ||||
3837 | // template-id that refers to the current context, so go there | ||||
3838 | // to find the actual type being constructed. | ||||
3839 | CXXRecordDecl *CurClass = dyn_cast<CXXRecordDecl>(CurContext); | ||||
3840 | if (!CurClass || CurClass->getIdentifier() != Name.TemplateId->Name) | ||||
3841 | return DeclarationNameInfo(); | ||||
3842 | |||||
3843 | // Determine the type of the class being constructed. | ||||
3844 | QualType CurClassType = Context.getTypeDeclType(CurClass); | ||||
3845 | |||||
3846 | // FIXME: Check two things: that the template-id names the same type as | ||||
3847 | // CurClassType, and that the template-id does not occur when the name | ||||
3848 | // was qualified. | ||||
3849 | |||||
3850 | NameInfo.setName(Context.DeclarationNames.getCXXConstructorName( | ||||
3851 | Context.getCanonicalType(CurClassType))); | ||||
3852 | NameInfo.setLoc(Name.StartLocation); | ||||
3853 | // FIXME: should we retrieve TypeSourceInfo? | ||||
3854 | NameInfo.setNamedTypeInfo(0); | ||||
3855 | return NameInfo; | ||||
3856 | } | ||||
3857 | |||||
3858 | case UnqualifiedId::IK_DestructorName: { | ||||
3859 | TypeSourceInfo *TInfo; | ||||
3860 | QualType Ty = GetTypeFromParser(Name.DestructorName, &TInfo); | ||||
3861 | if (Ty.isNull()) | ||||
3862 | return DeclarationNameInfo(); | ||||
3863 | NameInfo.setName(Context.DeclarationNames.getCXXDestructorName( | ||||
3864 | Context.getCanonicalType(Ty))); | ||||
3865 | NameInfo.setLoc(Name.StartLocation); | ||||
3866 | NameInfo.setNamedTypeInfo(TInfo); | ||||
3867 | return NameInfo; | ||||
3868 | } | ||||
3869 | |||||
3870 | case UnqualifiedId::IK_TemplateId: { | ||||
John McCall | 2b5289b | 2010-08-23 07:28:44 +0000 | [diff] [blame] | 3871 | TemplateName TName = Name.TemplateId->Template.get(); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3872 | SourceLocation TNameLoc = Name.TemplateId->TemplateNameLoc; |
3873 | return Context.getNameForTemplate(TName, TNameLoc); | ||||
3874 | } | ||||
3875 | |||||
3876 | } // switch (Name.getKind()) | ||||
3877 | |||||
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3878 | llvm_unreachable("Unknown name kind"); |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 3879 | } |
3880 | |||||
Kaelyn Uhrain | 4d9d157 | 2011-08-04 17:40:00 +0000 | [diff] [blame] | 3881 | static QualType getCoreType(QualType Ty) { |
3882 | do { | ||||
3883 | if (Ty->isPointerType() || Ty->isReferenceType()) | ||||
3884 | Ty = Ty->getPointeeType(); | ||||
3885 | else if (Ty->isArrayType()) | ||||
3886 | Ty = Ty->castAsArrayTypeUnsafe()->getElementType(); | ||||
3887 | else | ||||
3888 | return Ty.withoutLocalFastQualifiers(); | ||||
3889 | } while (true); | ||||
3890 | } | ||||
3891 | |||||
Kaelyn Uhrain | 2afd766 | 2011-10-11 00:28:39 +0000 | [diff] [blame] | 3892 | /// hasSimilarParameters - Determine whether the C++ functions Declaration |
3893 | /// and Definition have "nearly" matching parameters. This heuristic is | ||||
3894 | /// used to improve diagnostics in the case where an out-of-line function | ||||
3895 | /// definition doesn't match any declaration within the class or namespace. | ||||
3896 | /// Also sets Params to the list of indices to the parameters that differ | ||||
3897 | /// between the declaration and the definition. If hasSimilarParameters | ||||
3898 | /// returns true and Params is empty, then all of the parameters match. | ||||
3899 | static bool hasSimilarParameters(ASTContext &Context, | ||||
Douglas Gregor | 4ce205f | 2009-02-06 17:46:57 +0000 | [diff] [blame] | 3900 | FunctionDecl *Declaration, |
Kaelyn Uhrain | 4d9d157 | 2011-08-04 17:40:00 +0000 | [diff] [blame] | 3901 | FunctionDecl *Definition, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 3902 | SmallVectorImpl<unsigned> &Params) { |
Kaelyn Uhrain | 4d9d157 | 2011-08-04 17:40:00 +0000 | [diff] [blame] | 3903 | Params.clear(); |
Douglas Gregor | 584049d | 2008-12-15 23:53:10 +0000 | [diff] [blame] | 3904 | if (Declaration->param_size() != Definition->param_size()) |
3905 | return false; | ||||
3906 | for (unsigned Idx = 0; Idx < Declaration->param_size(); ++Idx) { | ||||
3907 | QualType DeclParamTy = Declaration->getParamDecl(Idx)->getType(); | ||||
3908 | QualType DefParamTy = Definition->getParamDecl(Idx)->getType(); | ||||
3909 | |||||
Kaelyn Uhrain | 4d9d157 | 2011-08-04 17:40:00 +0000 | [diff] [blame] | 3910 | // The parameter types are identical |
Matt Beaumont-Gay | 903d6dc | 2011-08-23 01:35:51 +0000 | [diff] [blame] | 3911 | if (Context.hasSameType(DefParamTy, DeclParamTy)) |
Kaelyn Uhrain | 4d9d157 | 2011-08-04 17:40:00 +0000 | [diff] [blame] | 3912 | continue; |
3913 | |||||
3914 | QualType DeclParamBaseTy = getCoreType(DeclParamTy); | ||||
3915 | QualType DefParamBaseTy = getCoreType(DefParamTy); | ||||
3916 | const IdentifierInfo *DeclTyName = DeclParamBaseTy.getBaseTypeIdentifier(); | ||||
3917 | const IdentifierInfo *DefTyName = DefParamBaseTy.getBaseTypeIdentifier(); | ||||
3918 | |||||
3919 | if (Context.hasSameUnqualifiedType(DeclParamBaseTy, DefParamBaseTy) || | ||||
3920 | (DeclTyName && DeclTyName == DefTyName)) | ||||
3921 | Params.push_back(Idx); | ||||
3922 | else // The two parameters aren't even close | ||||
Douglas Gregor | 584049d | 2008-12-15 23:53:10 +0000 | [diff] [blame] | 3923 | return false; |
3924 | } | ||||
3925 | |||||
3926 | return true; | ||||
3927 | } | ||||
3928 | |||||
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 3929 | /// NeedsRebuildingInCurrentInstantiation - Checks whether the given |
3930 | /// declarator needs to be rebuilt in the current instantiation. | ||||
3931 | /// Any bits of declarator which appear before the name are valid for | ||||
3932 | /// consideration here. That's specifically the type in the decl spec | ||||
3933 | /// and the base type in any member-pointer chunks. | ||||
3934 | static bool RebuildDeclaratorInCurrentInstantiation(Sema &S, Declarator &D, | ||||
3935 | DeclarationName Name) { | ||||
3936 | // The types we specifically need to rebuild are: | ||||
3937 | // - typenames, typeofs, and decltypes | ||||
3938 | // - types which will become injected class names | ||||
3939 | // Of course, we also need to rebuild any type referencing such a | ||||
3940 | // type. It's safest to just say "dependent", but we call out a | ||||
3941 | // few cases here. | ||||
3942 | |||||
3943 | DeclSpec &DS = D.getMutableDeclSpec(); | ||||
3944 | switch (DS.getTypeSpecType()) { | ||||
3945 | case DeclSpec::TST_typename: | ||||
3946 | case DeclSpec::TST_typeofType: | ||||
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3947 | case DeclSpec::TST_underlyingType: |
3948 | case DeclSpec::TST_atomic: { | ||||
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 3949 | // Grab the type from the parser. |
3950 | TypeSourceInfo *TSI = 0; | ||||
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3951 | QualType T = S.GetTypeFromParser(DS.getRepAsType(), &TSI); |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 3952 | if (T.isNull() || !T->isDependentType()) break; |
3953 | |||||
3954 | // Make sure there's a type source info. This isn't really much | ||||
3955 | // of a waste; most dependent types should have type source info | ||||
3956 | // attached already. | ||||
3957 | if (!TSI) | ||||
3958 | TSI = S.Context.getTrivialTypeSourceInfo(T, DS.getTypeSpecTypeLoc()); | ||||
3959 | |||||
3960 | // Rebuild the type in the current instantiation. | ||||
3961 | TSI = S.RebuildTypeInCurrentInstantiation(TSI, D.getIdentifierLoc(), Name); | ||||
3962 | if (!TSI) return true; | ||||
3963 | |||||
3964 | // Store the new type back in the decl spec. | ||||
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3965 | ParsedType LocType = S.CreateParsedType(TSI->getType(), TSI); |
3966 | DS.UpdateTypeRep(LocType); | ||||
3967 | break; | ||||
3968 | } | ||||
3969 | |||||
Richard Smith | c4a8391 | 2012-10-01 20:35:07 +0000 | [diff] [blame] | 3970 | case DeclSpec::TST_decltype: |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3971 | case DeclSpec::TST_typeofExpr: { |
3972 | Expr *E = DS.getRepAsExpr(); | ||||
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3973 | ExprResult Result = S.RebuildExprInCurrentInstantiation(E); |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3974 | if (Result.isInvalid()) return true; |
3975 | DS.UpdateExprRep(Result.get()); | ||||
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 3976 | break; |
3977 | } | ||||
3978 | |||||
3979 | default: | ||||
3980 | // Nothing to do for these decl specs. | ||||
3981 | break; | ||||
3982 | } | ||||
3983 | |||||
3984 | // It doesn't matter what order we do this in. | ||||
3985 | for (unsigned I = 0, E = D.getNumTypeObjects(); I != E; ++I) { | ||||
3986 | DeclaratorChunk &Chunk = D.getTypeObject(I); | ||||
3987 | |||||
3988 | // The only type information in the declarator which can come | ||||
3989 | // before the declaration name is the base type of a member | ||||
3990 | // pointer. | ||||
3991 | if (Chunk.Kind != DeclaratorChunk::MemberPointer) | ||||
3992 | continue; | ||||
3993 | |||||
3994 | // Rebuild the scope specifier in-place. | ||||
3995 | CXXScopeSpec &SS = Chunk.Mem.Scope(); | ||||
3996 | if (S.RebuildNestedNameSpecifierInCurrentInstantiation(SS)) | ||||
3997 | return true; | ||||
3998 | } | ||||
3999 | |||||
4000 | return false; | ||||
4001 | } | ||||
4002 | |||||
Anders Carlsson | 3242ee0 | 2011-07-04 16:28:17 +0000 | [diff] [blame] | 4003 | Decl *Sema::ActOnDeclarator(Scope *S, Declarator &D) { |
Douglas Gregor | 45fa560 | 2011-11-07 20:56:01 +0000 | [diff] [blame] | 4004 | D.setFunctionDefinitionKind(FDK_Declaration); |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 4005 | Decl *Dcl = HandleDeclarator(S, D, MultiTemplateParamsArg()); |
Argyrios Kyrtzidis | c14a03d | 2011-11-23 20:27:36 +0000 | [diff] [blame] | 4006 | |
4007 | if (OriginalLexicalContext && OriginalLexicalContext->isObjCContainer() && | ||||
Douglas Gregor | e7be109 | 2012-04-30 18:13:01 +0000 | [diff] [blame] | 4008 | Dcl && Dcl->getDeclContext()->isFileContext()) |
Argyrios Kyrtzidis | c14a03d | 2011-11-23 20:27:36 +0000 | [diff] [blame] | 4009 | Dcl->setTopLevelDeclInObjCContainer(); |
4010 | |||||
4011 | return Dcl; | ||||
John McCall | 7cd088e | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 4012 | } |
4013 | |||||
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4014 | /// DiagnoseClassNameShadow - Implement C++ [class.mem]p13: |
4015 | /// If T is the name of a class, then each of the following shall have a | ||||
4016 | /// name different from T: | ||||
4017 | /// - every static data member of class T; | ||||
4018 | /// - every member function of class T | ||||
4019 | /// - every member of class T that is itself a type; | ||||
4020 | /// \returns true if the declaration name violates these rules. | ||||
4021 | bool Sema::DiagnoseClassNameShadow(DeclContext *DC, | ||||
4022 | DeclarationNameInfo NameInfo) { | ||||
4023 | DeclarationName Name = NameInfo.getName(); | ||||
4024 | |||||
4025 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(DC)) | ||||
4026 | if (Record->getIdentifier() && Record->getDeclName() == Name) { | ||||
4027 | Diag(NameInfo.getLoc(), diag::err_member_name_of_class) << Name; | ||||
4028 | return true; | ||||
4029 | } | ||||
4030 | |||||
4031 | return false; | ||||
4032 | } | ||||
Douglas Gregor | 42acead | 2012-03-17 23:06:31 +0000 | [diff] [blame] | 4033 | |
Douglas Gregor | 6960587 | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 4034 | /// \brief Diagnose a declaration whose declarator-id has the given |
4035 | /// nested-name-specifier. | ||||
4036 | /// | ||||
4037 | /// \param SS The nested-name-specifier of the declarator-id. | ||||
4038 | /// | ||||
4039 | /// \param DC The declaration context to which the nested-name-specifier | ||||
4040 | /// resolves. | ||||
4041 | /// | ||||
4042 | /// \param Name The name of the entity being declared. | ||||
4043 | /// | ||||
4044 | /// \param Loc The location of the name of the entity being declared. | ||||
Douglas Gregor | 42acead | 2012-03-17 23:06:31 +0000 | [diff] [blame] | 4045 | /// |
4046 | /// \returns true if we cannot safely recover from this error, false otherwise. | ||||
Douglas Gregor | 6960587 | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 4047 | bool Sema::diagnoseQualifiedDeclaration(CXXScopeSpec &SS, DeclContext *DC, |
Douglas Gregor | 42acead | 2012-03-17 23:06:31 +0000 | [diff] [blame] | 4048 | DeclarationName Name, |
Douglas Gregor | 6960587 | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 4049 | SourceLocation Loc) { |
4050 | DeclContext *Cur = CurContext; | ||||
Eli Friedman | a03c5ee | 2013-08-12 21:54:01 +0000 | [diff] [blame] | 4051 | while (isa<LinkageSpecDecl>(Cur) || isa<CapturedDecl>(Cur)) |
Douglas Gregor | 6960587 | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 4052 | Cur = Cur->getParent(); |
4053 | |||||
4054 | // C++ [dcl.meaning]p1: | ||||
4055 | // A declarator-id shall not be qualified except for the definition | ||||
4056 | // of a member function (9.3) or static data member (9.4) outside of | ||||
4057 | // its class, the definition or explicit instantiation of a function | ||||
4058 | // or variable member of a namespace outside of its namespace, or the | ||||
4059 | // definition of an explicit specialization outside of its namespace, | ||||
4060 | // or the declaration of a friend function that is a member of | ||||
4061 | // another class or namespace (11.3). [...] | ||||
4062 | |||||
4063 | // The user provided a superfluous scope specifier that refers back to the | ||||
4064 | // class or namespaces in which the entity is already declared. | ||||
Douglas Gregor | 42acead | 2012-03-17 23:06:31 +0000 | [diff] [blame] | 4065 | // |
4066 | // class X { | ||||
4067 | // void X::f(); | ||||
4068 | // }; | ||||
Douglas Gregor | 6960587 | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 4069 | if (Cur->Equals(DC)) { |
Douglas Gregor | 7537945 | 2012-09-13 20:16:20 +0000 | [diff] [blame] | 4070 | Diag(Loc, LangOpts.MicrosoftExt? diag::warn_member_extra_qualification |
4071 | : diag::err_member_extra_qualification) | ||||
Douglas Gregor | 42acead | 2012-03-17 23:06:31 +0000 | [diff] [blame] | 4072 | << Name << FixItHint::CreateRemoval(SS.getRange()); |
4073 | SS.clear(); | ||||
4074 | return false; | ||||
4075 | } | ||||
Douglas Gregor | 6960587 | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 4076 | |
4077 | // Check whether the qualifying scope encloses the scope of the original | ||||
4078 | // declaration. | ||||
4079 | if (!Cur->Encloses(DC)) { | ||||
4080 | if (Cur->isRecord()) | ||||
4081 | Diag(Loc, diag::err_member_qualification) | ||||
4082 | << Name << SS.getRange(); | ||||
4083 | else if (isa<TranslationUnitDecl>(DC)) | ||||
4084 | Diag(Loc, diag::err_invalid_declarator_global_scope) | ||||
4085 | << Name << SS.getRange(); | ||||
4086 | else if (isa<FunctionDecl>(Cur)) | ||||
4087 | Diag(Loc, diag::err_invalid_declarator_in_function) | ||||
4088 | << Name << SS.getRange(); | ||||
Eli Friedman | a03c5ee | 2013-08-12 21:54:01 +0000 | [diff] [blame] | 4089 | else if (isa<BlockDecl>(Cur)) |
4090 | Diag(Loc, diag::err_invalid_declarator_in_block) | ||||
4091 | << Name << SS.getRange(); | ||||
Douglas Gregor | 6960587 | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 4092 | else |
4093 | Diag(Loc, diag::err_invalid_declarator_scope) | ||||
Richard Smith | a1c4f7c | 2012-04-13 04:07:40 +0000 | [diff] [blame] | 4094 | << Name << cast<NamedDecl>(Cur) << cast<NamedDecl>(DC) << SS.getRange(); |
Douglas Gregor | 6960587 | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 4095 | |
Douglas Gregor | 42acead | 2012-03-17 23:06:31 +0000 | [diff] [blame] | 4096 | return true; |
Douglas Gregor | 6960587 | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 4097 | } |
4098 | |||||
4099 | if (Cur->isRecord()) { | ||||
4100 | // Cannot qualify members within a class. | ||||
4101 | Diag(Loc, diag::err_member_qualification) | ||||
4102 | << Name << SS.getRange(); | ||||
4103 | SS.clear(); | ||||
4104 | |||||
4105 | // C++ constructors and destructors with incorrect scopes can break | ||||
4106 | // our AST invariants by having the wrong underlying types. If | ||||
4107 | // that's the case, then drop this declaration entirely. | ||||
4108 | if ((Name.getNameKind() == DeclarationName::CXXConstructorName || | ||||
4109 | Name.getNameKind() == DeclarationName::CXXDestructorName) && | ||||
4110 | !Context.hasSameType(Name.getCXXNameType(), | ||||
4111 | Context.getTypeDeclType(cast<CXXRecordDecl>(Cur)))) | ||||
4112 | return true; | ||||
4113 | |||||
4114 | return false; | ||||
4115 | } | ||||
Douglas Gregor | 42acead | 2012-03-17 23:06:31 +0000 | [diff] [blame] | 4116 | |
Douglas Gregor | 6960587 | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 4117 | // C++11 [dcl.meaning]p1: |
4118 | // [...] "The nested-name-specifier of the qualified declarator-id shall | ||||
4119 | // not begin with a decltype-specifer" | ||||
4120 | NestedNameSpecifierLoc SpecLoc(SS.getScopeRep(), SS.location_data()); | ||||
4121 | while (SpecLoc.getPrefix()) | ||||
4122 | SpecLoc = SpecLoc.getPrefix(); | ||||
4123 | if (dyn_cast_or_null<DecltypeType>( | ||||
4124 | SpecLoc.getNestedNameSpecifier()->getAsType())) | ||||
4125 | Diag(Loc, diag::err_decltype_in_declarator) | ||||
4126 | << SpecLoc.getTypeLoc().getSourceRange(); | ||||
4127 | |||||
Douglas Gregor | 42acead | 2012-03-17 23:06:31 +0000 | [diff] [blame] | 4128 | return false; |
4129 | } | ||||
4130 | |||||
Rafael Espindola | fc35cbc | 2013-01-08 20:44:06 +0000 | [diff] [blame] | 4131 | NamedDecl *Sema::HandleDeclarator(Scope *S, Declarator &D, |
4132 | MultiTemplateParamsArg TemplateParamLists) { | ||||
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4133 | // TODO: consider using NameInfo for diagnostic. |
4134 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); | ||||
4135 | DeclarationName Name = NameInfo.getName(); | ||||
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 4136 | |
Chris Lattner | e80a59c | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4137 | // All of these full declarators require an identifier. If it doesn't have |
4138 | // one, the ParsedFreeStandingDeclSpec action should be used. | ||||
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 4139 | if (!Name) { |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 4140 | if (!D.isInvalidType()) // Reject this if we think it is valid. |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4141 | Diag(D.getDeclSpec().getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 4142 | diag::err_declarator_need_ident) |
4143 | << D.getDeclSpec().getSourceRange() << D.getSourceRange(); | ||||
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4144 | return 0; |
Douglas Gregor | 56c0458 | 2010-12-16 00:46:58 +0000 | [diff] [blame] | 4145 | } else if (DiagnoseUnexpandedParameterPack(NameInfo, UPPC_DeclarationType)) |
4146 | return 0; | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4147 | |
Chris Lattner | 31e0572 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 4148 | // The scope passed in may not be a decl scope. Zip up the scope tree until |
4149 | // we find one that is. | ||||
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 4150 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 4151 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
Chris Lattner | 31e0572 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 4152 | S = S->getParent(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4153 | |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 4154 | DeclContext *DC = CurContext; |
4155 | if (D.getCXXScopeSpec().isInvalid()) | ||||
4156 | D.setInvalidType(); | ||||
4157 | else if (D.getCXXScopeSpec().isSet()) { | ||||
Douglas Gregor | 6ccab97 | 2010-12-16 01:14:37 +0000 | [diff] [blame] | 4158 | if (DiagnoseUnexpandedParameterPack(D.getCXXScopeSpec(), |
4159 | UPPC_DeclarationQualifier)) | ||||
4160 | return 0; | ||||
4161 | |||||
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 4162 | bool EnteringContext = !D.getDeclSpec().isFriendSpecified(); |
4163 | DC = computeDeclContext(D.getCXXScopeSpec(), EnteringContext); | ||||
4164 | if (!DC) { | ||||
4165 | // If we could not compute the declaration context, it's because the | ||||
4166 | // declaration context is dependent but does not refer to a class, | ||||
4167 | // class template, or class template partial specialization. Complain | ||||
4168 | // and return early, to avoid the coming semantic disaster. | ||||
4169 | Diag(D.getIdentifierLoc(), | ||||
4170 | diag::err_template_qualified_declarator_no_match) | ||||
4171 | << (NestedNameSpecifier*)D.getCXXScopeSpec().getScopeRep() | ||||
4172 | << D.getCXXScopeSpec().getRange(); | ||||
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4173 | return 0; |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 4174 | } |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 4175 | bool IsDependentContext = DC->isDependentContext(); |
John McCall | 0dd7ceb | 2009-12-19 09:35:56 +0000 | [diff] [blame] | 4176 | |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 4177 | if (!IsDependentContext && |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 4178 | RequireCompleteDeclContext(D.getCXXScopeSpec(), DC)) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4179 | return 0; |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 4180 | |
Douglas Gregor | 6960587 | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 4181 | if (isa<CXXRecordDecl>(DC) && !cast<CXXRecordDecl>(DC)->hasDefinition()) { |
4182 | Diag(D.getIdentifierLoc(), | ||||
4183 | diag::err_member_def_undefined_record) | ||||
4184 | << Name << DC << D.getCXXScopeSpec().getRange(); | ||||
4185 | D.setInvalidType(); | ||||
4186 | } else if (!D.getDeclSpec().isFriendSpecified()) { | ||||
4187 | if (diagnoseQualifiedDeclaration(D.getCXXScopeSpec(), DC, | ||||
4188 | Name, D.getIdentifierLoc())) { | ||||
4189 | if (DC->isRecord()) | ||||
Douglas Gregor | 42acead | 2012-03-17 23:06:31 +0000 | [diff] [blame] | 4190 | return 0; |
Douglas Gregor | 6960587 | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 4191 | |
4192 | D.setInvalidType(); | ||||
Douglas Gregor | 922fff2 | 2010-10-13 22:19:53 +0000 | [diff] [blame] | 4193 | } |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 4194 | } |
4195 | |||||
4196 | // Check whether we need to rebuild the type of the given | ||||
4197 | // declaration in the current instantiation. | ||||
4198 | if (EnteringContext && IsDependentContext && | ||||
4199 | TemplateParamLists.size() != 0) { | ||||
4200 | ContextRAII SavedContext(*this, DC); | ||||
4201 | if (RebuildDeclaratorInCurrentInstantiation(*this, D, Name)) | ||||
4202 | D.setInvalidType(); | ||||
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 4203 | } |
4204 | } | ||||
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4205 | |
4206 | if (DiagnoseClassNameShadow(DC, NameInfo)) | ||||
4207 | // If this is a typedef, we'll end up spewing multiple diagnostics. | ||||
4208 | // Just return early; it's safer. | ||||
4209 | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) | ||||
4210 | return 0; | ||||
Douglas Gregor | a6e937c | 2010-10-15 13:21:21 +0000 | [diff] [blame] | 4211 | |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 4212 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
4213 | QualType R = TInfo->getType(); | ||||
Douglas Gregor | d6f7e9d | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 4214 | |
Douglas Gregor | d093722 | 2010-12-13 22:49:22 +0000 | [diff] [blame] | 4215 | if (DiagnoseUnexpandedParameterPack(D.getIdentifierLoc(), TInfo, |
4216 | UPPC_DeclarationType)) | ||||
4217 | D.setInvalidType(); | ||||
4218 | |||||
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4219 | LookupResult Previous(*this, NameInfo, LookupOrdinaryName, |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4220 | ForRedeclaration); |
4221 | |||||
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 4222 | // 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] | 4223 | if (!D.getCXXScopeSpec().isSet()) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4224 | bool IsLinkageLookup = false; |
Richard Smith | dd9459f | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 4225 | bool CreateBuiltins = false; |
Douglas Gregor | d6f7e9d | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 4226 | |
4227 | // If the declaration we're planning to build will be a function | ||||
4228 | // or object with linkage, then look for another declaration with | ||||
4229 | // linkage (C99 6.2.2p4-5 and C++ [basic.link]p6). | ||||
Richard Smith | dd9459f | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 4230 | // |
4231 | // If the declaration we're planning to build will be declared with | ||||
4232 | // external linkage in the translation unit, create any builtin with | ||||
4233 | // the same name. | ||||
Douglas Gregor | d6f7e9d | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 4234 | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) |
4235 | /* Do nothing*/; | ||||
Richard Smith | dd9459f | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 4236 | else if (CurContext->isFunctionOrMethod() && |
4237 | (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_extern || | ||||
4238 | R->isFunctionType())) { | ||||
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4239 | IsLinkageLookup = true; |
Richard Smith | dd9459f | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 4240 | CreateBuiltins = |
4241 | CurContext->getEnclosingNamespaceContext()->isTranslationUnit(); | ||||
4242 | } else if (CurContext->getRedeclContext()->isTranslationUnit() && | ||||
4243 | D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_static) | ||||
4244 | CreateBuiltins = true; | ||||
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4245 | |
4246 | if (IsLinkageLookup) | ||||
4247 | Previous.clear(LookupRedeclarationWithLinkage); | ||||
Douglas Gregor | d6f7e9d | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 4248 | |
Richard Smith | dd9459f | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 4249 | LookupName(Previous, S, CreateBuiltins); |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 4250 | } else { // Something like "int foo::x;" |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4251 | LookupQualifiedName(Previous, DC); |
4252 | |||||
Douglas Gregor | 6960587 | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 4253 | // C++ [dcl.meaning]p1: |
4254 | // When the declarator-id is qualified, the declaration shall refer to a | ||||
4255 | // previously declared member of the class or namespace to which the | ||||
4256 | // qualifier refers (or, in the case of a namespace, of an element of the | ||||
4257 | // inline namespace set of that namespace (7.3.1)) or to a specialization | ||||
4258 | // thereof; [...] | ||||
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 4259 | // |
Douglas Gregor | 6960587 | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 4260 | // Note that we already checked the context above, and that we do not have |
4261 | // enough information to make sure that Previous contains the declaration | ||||
4262 | // we want to match. For example, given: | ||||
Douglas Gregor | 584049d | 2008-12-15 23:53:10 +0000 | [diff] [blame] | 4263 | // |
Douglas Gregor | 9d35097 | 2008-12-12 08:25:50 +0000 | [diff] [blame] | 4264 | // class X { |
4265 | // void f(); | ||||
Douglas Gregor | 584049d | 2008-12-15 23:53:10 +0000 | [diff] [blame] | 4266 | // void f(float); |
Douglas Gregor | 9d35097 | 2008-12-12 08:25:50 +0000 | [diff] [blame] | 4267 | // }; |
4268 | // | ||||
Douglas Gregor | 584049d | 2008-12-15 23:53:10 +0000 | [diff] [blame] | 4269 | // void X::f(int) { } // ill-formed |
4270 | // | ||||
Douglas Gregor | 6960587 | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 4271 | // In this case, Previous will point to the overload set |
Douglas Gregor | 584049d | 2008-12-15 23:53:10 +0000 | [diff] [blame] | 4272 | // containing the two f's declared in X, but neither of them |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4273 | // matches. |
Douglas Gregor | 6960587 | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 4274 | |
4275 | // C++ [dcl.meaning]p1: | ||||
4276 | // [...] the member shall not merely have been introduced by a | ||||
4277 | // using-declaration in the scope of the class or namespace nominated by | ||||
4278 | // the nested-name-specifier of the declarator-id. | ||||
4279 | RemoveUsingDecls(Previous); | ||||
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 4280 | } |
4281 | |||||
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4282 | if (Previous.isSingleResult() && |
4283 | Previous.getFoundDecl()->isTemplateParameter()) { | ||||
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 4284 | // Maybe we will complain about the shadowed template parameter. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4285 | if (!D.isInvalidType()) |
Douglas Gregor | cb8f951 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 4286 | DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), |
4287 | Previous.getFoundDecl()); | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4288 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 4289 | // Just pretend that we didn't see the previous declaration. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4290 | Previous.clear(); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 4291 | } |
4292 | |||||
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 4293 | // In C++, the previous declaration we find might be a tag type |
4294 | // (class or enum). In this case, the new declaration will hide the | ||||
Douglas Gregor | 6697312 | 2009-01-28 17:15:10 +0000 | [diff] [blame] | 4295 | // tag type. Note that this does does not apply if we're declaring a |
4296 | // typedef (C++ [dcl.typedef]p4). | ||||
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4297 | if (Previous.isSingleTagDecl() && |
Douglas Gregor | 6697312 | 2009-01-28 17:15:10 +0000 | [diff] [blame] | 4298 | D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef) |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4299 | Previous.clear(); |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 4300 | |
Richard Smith | 3cdbbdc | 2013-03-06 01:37:38 +0000 | [diff] [blame] | 4301 | // Check that there are no default arguments other than in the parameters |
4302 | // of a function declaration (C++ only). | ||||
4303 | if (getLangOpts().CPlusPlus) | ||||
4304 | CheckExtraCXXDefaultArguments(D); | ||||
4305 | |||||
Nico Weber | e6bb76c | 2012-12-23 00:40:46 +0000 | [diff] [blame] | 4306 | NamedDecl *New; |
4307 | |||||
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 4308 | bool AddToScope = true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4309 | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) { |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 4310 | if (TemplateParamLists.size()) { |
4311 | Diag(D.getIdentifierLoc(), diag::err_template_typedef); | ||||
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4312 | return 0; |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 4313 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4314 | |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 4315 | New = ActOnTypedefDeclarator(S, D, DC, TInfo, Previous); |
Douglas Gregor | d6f7e9d | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 4316 | } else if (R->isFunctionType()) { |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 4317 | New = ActOnFunctionDeclarator(S, D, DC, TInfo, Previous, |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 4318 | TemplateParamLists, |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 4319 | AddToScope); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4320 | } else { |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4321 | New = ActOnVariableDeclarator(S, D, DC, TInfo, Previous, TemplateParamLists, |
4322 | AddToScope); | ||||
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4323 | } |
Zhongxing Xu | 416fcaf | 2009-01-16 01:13:29 +0000 | [diff] [blame] | 4324 | |
4325 | if (New == 0) | ||||
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4326 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4327 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4328 | // If this has an identifier and is not an invalid redeclaration or |
4329 | // function template specialization, add it to the scope stack. | ||||
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 4330 | if (New->getDeclName() && AddToScope && |
Richard Smith | a41c97a | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 4331 | !(D.isRedeclaration() && New->isInvalidDecl())) { |
4332 | // Only make a locally-scoped extern declaration visible if it is the first | ||||
4333 | // declaration of this entity. Qualified lookup for such an entity should | ||||
4334 | // only find this declaration if there is no visible declaration of it. | ||||
4335 | bool AddToContext = !D.isRedeclaration() || !New->isLocalExternDecl(); | ||||
4336 | PushOnScopeChains(New, S, AddToContext); | ||||
4337 | if (!AddToContext) | ||||
4338 | CurContext->addHiddenDecl(New); | ||||
4339 | } | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4340 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4341 | return New; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4342 | } |
4343 | |||||
Abramo Bagnara | 88adb98 | 2012-11-08 16:27:30 +0000 | [diff] [blame] | 4344 | /// Helper method to turn variable array types into constant array |
4345 | /// types in certain situations which would otherwise be errors (for | ||||
4346 | /// GCC compatibility). | ||||
Eli Friedman | 1ca4813 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 4347 | static QualType TryToFixInvalidVariablyModifiedType(QualType T, |
4348 | ASTContext &Context, | ||||
Douglas Gregor | 2767ce2 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 4349 | bool &SizeIsNegative, |
4350 | llvm::APSInt &Oversized) { | ||||
Eli Friedman | 1ca4813 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 4351 | // This method tries to turn a variable array into a constant |
4352 | // array even when the size isn't an ICE. This is necessary | ||||
4353 | // for compatibility with code that depends on gcc's buggy | ||||
4354 | // constant expression folding, like struct {char x[(int)(char*)2];} | ||||
4355 | SizeIsNegative = false; | ||||
Douglas Gregor | 2767ce2 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 4356 | Oversized = 0; |
4357 | |||||
4358 | if (T->isDependentType()) | ||||
4359 | return QualType(); | ||||
4360 | |||||
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4361 | QualifierCollector Qs; |
4362 | const Type *Ty = Qs.strip(T); | ||||
4363 | |||||
4364 | if (const PointerType* PTy = dyn_cast<PointerType>(Ty)) { | ||||
Eli Friedman | 1ca4813 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 4365 | QualType Pointee = PTy->getPointeeType(); |
4366 | QualType FixedType = | ||||
Douglas Gregor | 2767ce2 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 4367 | TryToFixInvalidVariablyModifiedType(Pointee, Context, SizeIsNegative, |
4368 | Oversized); | ||||
Eli Friedman | 1ca4813 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 4369 | if (FixedType.isNull()) return FixedType; |
Eli Friedman | 61125c8 | 2009-02-21 00:58:02 +0000 | [diff] [blame] | 4370 | FixedType = Context.getPointerType(FixedType); |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 4371 | return Qs.apply(Context, FixedType); |
Eli Friedman | 1ca4813 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 4372 | } |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 4373 | if (const ParenType* PTy = dyn_cast<ParenType>(Ty)) { |
4374 | QualType Inner = PTy->getInnerType(); | ||||
4375 | QualType FixedType = | ||||
4376 | TryToFixInvalidVariablyModifiedType(Inner, Context, SizeIsNegative, | ||||
4377 | Oversized); | ||||
4378 | if (FixedType.isNull()) return FixedType; | ||||
4379 | FixedType = Context.getParenType(FixedType); | ||||
4380 | return Qs.apply(Context, FixedType); | ||||
4381 | } | ||||
Eli Friedman | 1ca4813 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 4382 | |
4383 | const VariableArrayType* VLATy = dyn_cast<VariableArrayType>(T); | ||||
Eli Friedman | bc592e6 | 2009-02-26 03:58:54 +0000 | [diff] [blame] | 4384 | if (!VLATy) |
4385 | return QualType(); | ||||
4386 | // FIXME: We should probably handle this case | ||||
4387 | if (VLATy->getElementType()->isVariablyModifiedType()) | ||||
4388 | return QualType(); | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4389 | |
Richard Smith | aa9c350 | 2011-12-07 00:43:50 +0000 | [diff] [blame] | 4390 | llvm::APSInt Res; |
Eli Friedman | 1ca4813 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 4391 | if (!VLATy->getSizeExpr() || |
Richard Smith | aa9c350 | 2011-12-07 00:43:50 +0000 | [diff] [blame] | 4392 | !VLATy->getSizeExpr()->EvaluateAsInt(Res, Context)) |
Eli Friedman | 1ca4813 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 4393 | return QualType(); |
Eli Friedman | bc592e6 | 2009-02-26 03:58:54 +0000 | [diff] [blame] | 4394 | |
Douglas Gregor | 2767ce2 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 4395 | // Check whether the array size is negative. |
Douglas Gregor | 2767ce2 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 4396 | if (Res.isSigned() && Res.isNegative()) { |
4397 | SizeIsNegative = true; | ||||
4398 | return QualType(); | ||||
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 4399 | } |
Eli Friedman | 1ca4813 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 4400 | |
Douglas Gregor | 2767ce2 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 4401 | // Check whether the array is too large to be addressed. |
4402 | unsigned ActiveSizeBits | ||||
4403 | = ConstantArrayType::getNumAddressingBits(Context, VLATy->getElementType(), | ||||
4404 | Res); | ||||
4405 | if (ActiveSizeBits > ConstantArrayType::getMaxSizeBits(Context)) { | ||||
4406 | Oversized = Res; | ||||
4407 | return QualType(); | ||||
4408 | } | ||||
4409 | |||||
4410 | return Context.getConstantArrayType(VLATy->getElementType(), | ||||
4411 | Res, ArrayType::Normal, 0); | ||||
Eli Friedman | 1ca4813 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 4412 | } |
4413 | |||||
Abramo Bagnara | 4c5750e | 2012-11-08 14:44:42 +0000 | [diff] [blame] | 4414 | static void |
4415 | FixInvalidVariablyModifiedTypeLoc(TypeLoc SrcTL, TypeLoc DstTL) { | ||||
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 4416 | if (PointerTypeLoc SrcPTL = SrcTL.getAs<PointerTypeLoc>()) { |
4417 | PointerTypeLoc DstPTL = DstTL.castAs<PointerTypeLoc>(); | ||||
4418 | FixInvalidVariablyModifiedTypeLoc(SrcPTL.getPointeeLoc(), | ||||
4419 | DstPTL.getPointeeLoc()); | ||||
4420 | DstPTL.setStarLoc(SrcPTL.getStarLoc()); | ||||
Abramo Bagnara | 4c5750e | 2012-11-08 14:44:42 +0000 | [diff] [blame] | 4421 | return; |
4422 | } | ||||
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 4423 | if (ParenTypeLoc SrcPTL = SrcTL.getAs<ParenTypeLoc>()) { |
4424 | ParenTypeLoc DstPTL = DstTL.castAs<ParenTypeLoc>(); | ||||
4425 | FixInvalidVariablyModifiedTypeLoc(SrcPTL.getInnerLoc(), | ||||
4426 | DstPTL.getInnerLoc()); | ||||
4427 | DstPTL.setLParenLoc(SrcPTL.getLParenLoc()); | ||||
4428 | DstPTL.setRParenLoc(SrcPTL.getRParenLoc()); | ||||
Abramo Bagnara | 4c5750e | 2012-11-08 14:44:42 +0000 | [diff] [blame] | 4429 | return; |
4430 | } | ||||
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 4431 | ArrayTypeLoc SrcATL = SrcTL.castAs<ArrayTypeLoc>(); |
4432 | ArrayTypeLoc DstATL = DstTL.castAs<ArrayTypeLoc>(); | ||||
4433 | TypeLoc SrcElemTL = SrcATL.getElementLoc(); | ||||
4434 | TypeLoc DstElemTL = DstATL.getElementLoc(); | ||||
Abramo Bagnara | 4c5750e | 2012-11-08 14:44:42 +0000 | [diff] [blame] | 4435 | DstElemTL.initializeFullCopy(SrcElemTL); |
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 4436 | DstATL.setLBracketLoc(SrcATL.getLBracketLoc()); |
4437 | DstATL.setSizeExpr(SrcATL.getSizeExpr()); | ||||
4438 | DstATL.setRBracketLoc(SrcATL.getRBracketLoc()); | ||||
Abramo Bagnara | 4c5750e | 2012-11-08 14:44:42 +0000 | [diff] [blame] | 4439 | } |
4440 | |||||
Abramo Bagnara | 88adb98 | 2012-11-08 16:27:30 +0000 | [diff] [blame] | 4441 | /// Helper method to turn variable array types into constant array |
4442 | /// types in certain situations which would otherwise be errors (for | ||||
4443 | /// GCC compatibility). | ||||
Abramo Bagnara | 4c5750e | 2012-11-08 14:44:42 +0000 | [diff] [blame] | 4444 | static TypeSourceInfo* |
4445 | TryToFixInvalidVariablyModifiedTypeSourceInfo(TypeSourceInfo *TInfo, | ||||
4446 | ASTContext &Context, | ||||
4447 | bool &SizeIsNegative, | ||||
4448 | llvm::APSInt &Oversized) { | ||||
4449 | QualType FixedTy | ||||
4450 | = TryToFixInvalidVariablyModifiedType(TInfo->getType(), Context, | ||||
4451 | SizeIsNegative, Oversized); | ||||
4452 | if (FixedTy.isNull()) | ||||
4453 | return 0; | ||||
4454 | TypeSourceInfo *FixedTInfo = Context.getTrivialTypeSourceInfo(FixedTy); | ||||
4455 | FixInvalidVariablyModifiedTypeLoc(TInfo->getTypeLoc(), | ||||
4456 | FixedTInfo->getTypeLoc()); | ||||
4457 | return FixedTInfo; | ||||
4458 | } | ||||
4459 | |||||
Richard Smith | 5ea6ef4 | 2013-01-10 23:43:47 +0000 | [diff] [blame] | 4460 | /// \brief Register the given locally-scoped extern "C" declaration so |
Richard Smith | 662f41b | 2013-06-18 20:15:12 +0000 | [diff] [blame] | 4461 | /// that it can be found later for redeclarations. We include any extern "C" |
4462 | /// declaration that is not visible in the translation unit here, not just | ||||
4463 | /// function-scope declarations. | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4464 | void |
Richard Smith | 662f41b | 2013-06-18 20:15:12 +0000 | [diff] [blame] | 4465 | Sema::RegisterLocallyScopedExternCDecl(NamedDecl *ND, Scope *S) { |
Richard Smith | aa4bc18 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 4466 | if (!getLangOpts().CPlusPlus && |
4467 | ND->getLexicalDeclContext()->getRedeclContext()->isTranslationUnit()) | ||||
4468 | // Don't need to track declarations in the TU in C. | ||||
4469 | return; | ||||
4470 | |||||
Douglas Gregor | 6393519 | 2009-03-02 00:19:53 +0000 | [diff] [blame] | 4471 | // Note that we have a locally-scoped external with this name. |
Richard Smith | aa4bc18 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 4472 | // FIXME: There can be multiple such declarations if they are functions marked |
4473 | // __attribute__((overloadable)) declared in function scope in C. | ||||
Richard Smith | 5ea6ef4 | 2013-01-10 23:43:47 +0000 | [diff] [blame] | 4474 | LocallyScopedExternCDecls[ND->getDeclName()] = ND; |
Douglas Gregor | 6393519 | 2009-03-02 00:19:53 +0000 | [diff] [blame] | 4475 | } |
4476 | |||||
Richard Smith | 662f41b | 2013-06-18 20:15:12 +0000 | [diff] [blame] | 4477 | NamedDecl *Sema::findLocallyScopedExternCDecl(DeclarationName Name) { |
Douglas Gregor | ec12ce2 | 2011-07-28 14:20:37 +0000 | [diff] [blame] | 4478 | if (ExternalSource) { |
4479 | // Load locally-scoped external decls from the external source. | ||||
Richard Smith | 662f41b | 2013-06-18 20:15:12 +0000 | [diff] [blame] | 4480 | // FIXME: This is inefficient. Maybe add a DeclContext for extern "C" decls? |
Douglas Gregor | ec12ce2 | 2011-07-28 14:20:37 +0000 | [diff] [blame] | 4481 | SmallVector<NamedDecl *, 4> Decls; |
Richard Smith | 5ea6ef4 | 2013-01-10 23:43:47 +0000 | [diff] [blame] | 4482 | ExternalSource->ReadLocallyScopedExternCDecls(Decls); |
Douglas Gregor | ec12ce2 | 2011-07-28 14:20:37 +0000 | [diff] [blame] | 4483 | for (unsigned I = 0, N = Decls.size(); I != N; ++I) { |
4484 | llvm::DenseMap<DeclarationName, NamedDecl *>::iterator Pos | ||||
Richard Smith | 5ea6ef4 | 2013-01-10 23:43:47 +0000 | [diff] [blame] | 4485 | = LocallyScopedExternCDecls.find(Decls[I]->getDeclName()); |
4486 | if (Pos == LocallyScopedExternCDecls.end()) | ||||
4487 | LocallyScopedExternCDecls[Decls[I]->getDeclName()] = Decls[I]; | ||||
Douglas Gregor | ec12ce2 | 2011-07-28 14:20:37 +0000 | [diff] [blame] | 4488 | } |
4489 | } | ||||
Richard Smith | 662f41b | 2013-06-18 20:15:12 +0000 | [diff] [blame] | 4490 | |
4491 | NamedDecl *D = LocallyScopedExternCDecls.lookup(Name); | ||||
4492 | return D ? cast<NamedDecl>(D->getMostRecentDecl()) : 0; | ||||
Douglas Gregor | ec12ce2 | 2011-07-28 14:20:37 +0000 | [diff] [blame] | 4493 | } |
4494 | |||||
Eli Friedman | 85a5319 | 2009-04-07 19:37:57 +0000 | [diff] [blame] | 4495 | /// \brief Diagnose function specifiers on a declaration of an identifier that |
4496 | /// does not identify a function. | ||||
Richard Smith | c7f8116 | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 4497 | void Sema::DiagnoseFunctionSpecifiers(const DeclSpec &DS) { |
Eli Friedman | 85a5319 | 2009-04-07 19:37:57 +0000 | [diff] [blame] | 4498 | // FIXME: We should probably indicate the identifier in question to avoid |
4499 | // confusion for constructs like "inline int a(), b;" | ||||
Richard Smith | c7f8116 | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 4500 | if (DS.isInlineSpecified()) |
4501 | Diag(DS.getInlineSpecLoc(), | ||||
Eli Friedman | 85a5319 | 2009-04-07 19:37:57 +0000 | [diff] [blame] | 4502 | diag::err_inline_non_function); |
4503 | |||||
Richard Smith | c7f8116 | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 4504 | if (DS.isVirtualSpecified()) |
4505 | Diag(DS.getVirtualSpecLoc(), | ||||
Eli Friedman | 85a5319 | 2009-04-07 19:37:57 +0000 | [diff] [blame] | 4506 | diag::err_virtual_non_function); |
4507 | |||||
Richard Smith | c7f8116 | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 4508 | if (DS.isExplicitSpecified()) |
4509 | Diag(DS.getExplicitSpecLoc(), | ||||
Eli Friedman | 85a5319 | 2009-04-07 19:37:57 +0000 | [diff] [blame] | 4510 | diag::err_explicit_non_function); |
Richard Smith | de03c15 | 2013-01-17 22:16:11 +0000 | [diff] [blame] | 4511 | |
Richard Smith | c7f8116 | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 4512 | if (DS.isNoreturnSpecified()) |
4513 | Diag(DS.getNoreturnSpecLoc(), | ||||
Richard Smith | de03c15 | 2013-01-17 22:16:11 +0000 | [diff] [blame] | 4514 | diag::err_noreturn_non_function); |
Eli Friedman | 85a5319 | 2009-04-07 19:37:57 +0000 | [diff] [blame] | 4515 | } |
4516 | |||||
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 4517 | NamedDecl* |
Zhongxing Xu | d5ed8c3 | 2009-01-16 03:34:13 +0000 | [diff] [blame] | 4518 | Sema::ActOnTypedefDeclarator(Scope* S, Declarator& D, DeclContext* DC, |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 4519 | TypeSourceInfo *TInfo, LookupResult &Previous) { |
Zhongxing Xu | d5ed8c3 | 2009-01-16 03:34:13 +0000 | [diff] [blame] | 4520 | // Typedef declarators cannot be qualified (C++ [dcl.meaning]p1). |
4521 | if (D.getCXXScopeSpec().isSet()) { | ||||
4522 | Diag(D.getIdentifierLoc(), diag::err_qualified_typedef_declarator) | ||||
4523 | << D.getCXXScopeSpec().getRange(); | ||||
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 4524 | D.setInvalidType(); |
Zhongxing Xu | d5ed8c3 | 2009-01-16 03:34:13 +0000 | [diff] [blame] | 4525 | // Pretend we didn't see the scope specifier. |
Douglas Gregor | 9de672f | 2010-03-23 15:26:55 +0000 | [diff] [blame] | 4526 | DC = CurContext; |
4527 | Previous.clear(); | ||||
Zhongxing Xu | d5ed8c3 | 2009-01-16 03:34:13 +0000 | [diff] [blame] | 4528 | } |
4529 | |||||
Richard Smith | c7f8116 | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 4530 | DiagnoseFunctionSpecifiers(D.getDeclSpec()); |
Eli Friedman | 85a5319 | 2009-04-07 19:37:57 +0000 | [diff] [blame] | 4531 | |
Richard Smith | af1fc7a | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 4532 | if (D.getDeclSpec().isConstexprSpecified()) |
4533 | Diag(D.getDeclSpec().getConstexprSpecLoc(), diag::err_invalid_constexpr) | ||||
4534 | << 1; | ||||
Eli Friedman | 63054b3 | 2009-04-19 20:27:55 +0000 | [diff] [blame] | 4535 | |
Douglas Gregor | aef0199 | 2010-07-13 06:37:01 +0000 | [diff] [blame] | 4536 | if (D.getName().Kind != UnqualifiedId::IK_Identifier) { |
4537 | Diag(D.getName().StartLocation, diag::err_typedef_not_identifier) | ||||
4538 | << D.getName().getSourceRange(); | ||||
4539 | return 0; | ||||
4540 | } | ||||
4541 | |||||
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 4542 | TypedefDecl *NewTD = ParseTypedefDecl(S, D, TInfo->getType(), TInfo); |
Zhongxing Xu | d5ed8c3 | 2009-01-16 03:34:13 +0000 | [diff] [blame] | 4543 | if (!NewTD) return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4544 | |
Zhongxing Xu | d5ed8c3 | 2009-01-16 03:34:13 +0000 | [diff] [blame] | 4545 | // Handle attributes prior to checking for duplicates in MergeVarDecl |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 4546 | ProcessDeclAttributes(S, NewTD, D); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4547 | |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4548 | CheckTypedefForVariablyModifiedType(S, NewTD); |
4549 | |||||
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 4550 | bool Redeclaration = D.isRedeclaration(); |
4551 | NamedDecl *ND = ActOnTypedefNameDecl(S, DC, NewTD, Previous, Redeclaration); | ||||
4552 | D.setRedeclaration(Redeclaration); | ||||
4553 | return ND; | ||||
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4554 | } |
4555 | |||||
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4556 | void |
4557 | Sema::CheckTypedefForVariablyModifiedType(Scope *S, TypedefNameDecl *NewTD) { | ||||
Chris Lattner | 38c5ebd | 2009-04-19 05:21:20 +0000 | [diff] [blame] | 4558 | // C99 6.7.7p2: If a typedef name specifies a variably modified type |
4559 | // then it shall have block scope. | ||||
Eli Friedman | bf87f2c | 2010-08-10 03:13:15 +0000 | [diff] [blame] | 4560 | // Note that variably modified types must be fixed before merging the decl so |
4561 | // that redeclarations will match. | ||||
Abramo Bagnara | 4c5750e | 2012-11-08 14:44:42 +0000 | [diff] [blame] | 4562 | TypeSourceInfo *TInfo = NewTD->getTypeSourceInfo(); |
4563 | QualType T = TInfo->getType(); | ||||
Chris Lattner | 38c5ebd | 2009-04-19 05:21:20 +0000 | [diff] [blame] | 4564 | if (T->isVariablyModifiedType()) { |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 4565 | getCurFunction()->setHasBranchProtectedScope(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4566 | |
Chris Lattner | 38c5ebd | 2009-04-19 05:21:20 +0000 | [diff] [blame] | 4567 | if (S->getFnParent() == 0) { |
Eli Friedman | 1ca4813 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 4568 | bool SizeIsNegative; |
Douglas Gregor | 2767ce2 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 4569 | llvm::APSInt Oversized; |
Abramo Bagnara | 4c5750e | 2012-11-08 14:44:42 +0000 | [diff] [blame] | 4570 | TypeSourceInfo *FixedTInfo = |
4571 | TryToFixInvalidVariablyModifiedTypeSourceInfo(TInfo, Context, | ||||
4572 | SizeIsNegative, | ||||
4573 | Oversized); | ||||
4574 | if (FixedTInfo) { | ||||
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4575 | Diag(NewTD->getLocation(), diag::warn_illegal_constant_array_size); |
Abramo Bagnara | 4c5750e | 2012-11-08 14:44:42 +0000 | [diff] [blame] | 4576 | NewTD->setTypeSourceInfo(FixedTInfo); |
Eli Friedman | 1ca4813 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 4577 | } else { |
4578 | if (SizeIsNegative) | ||||
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4579 | Diag(NewTD->getLocation(), diag::err_typecheck_negative_array_size); |
Eli Friedman | 1ca4813 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 4580 | else if (T->isVariableArrayType()) |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4581 | Diag(NewTD->getLocation(), diag::err_vla_decl_in_file_scope); |
Douglas Gregor | 2767ce2 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 4582 | else if (Oversized.getBoolValue()) |
David Blaikie | d662a79 | 2011-10-19 22:56:21 +0000 | [diff] [blame] | 4583 | Diag(NewTD->getLocation(), diag::err_array_too_large) |
4584 | << Oversized.toString(10); | ||||
Eli Friedman | 1ca4813 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 4585 | else |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4586 | Diag(NewTD->getLocation(), diag::err_vm_decl_in_file_scope); |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 4587 | NewTD->setInvalidDecl(); |
Eli Friedman | 1ca4813 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 4588 | } |
Zhongxing Xu | d5ed8c3 | 2009-01-16 03:34:13 +0000 | [diff] [blame] | 4589 | } |
4590 | } | ||||
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4591 | } |
Douglas Gregor | c29f77b | 2009-07-07 16:35:42 +0000 | [diff] [blame] | 4592 | |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4593 | |
4594 | /// ActOnTypedefNameDecl - Perform semantic checking for a declaration which | ||||
4595 | /// declares a typedef-name, either using the 'typedef' type specifier or via | ||||
4596 | /// a C++0x [dcl.typedef]p2 alias-declaration: 'using T = A;'. | ||||
4597 | NamedDecl* | ||||
4598 | Sema::ActOnTypedefNameDecl(Scope *S, DeclContext *DC, TypedefNameDecl *NewTD, | ||||
4599 | LookupResult &Previous, bool &Redeclaration) { | ||||
Eli Friedman | bf87f2c | 2010-08-10 03:13:15 +0000 | [diff] [blame] | 4600 | // Merge the decl with the existing one if appropriate. If the decl is |
4601 | // in an outer scope, it isn't the same thing. | ||||
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4602 | FilterLookupForScope(Previous, DC, S, /*ConsiderLinkage*/ false, |
Douglas Gregor | cc20945 | 2011-03-07 16:54:27 +0000 | [diff] [blame] | 4603 | /*ExplicitInstantiationOrSpecialization=*/false); |
Douglas Gregor | 7dc80e1 | 2013-01-09 00:47:56 +0000 | [diff] [blame] | 4604 | filterNonConflictingPreviousDecls(Context, NewTD, Previous); |
Eli Friedman | bf87f2c | 2010-08-10 03:13:15 +0000 | [diff] [blame] | 4605 | if (!Previous.empty()) { |
4606 | Redeclaration = true; | ||||
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4607 | MergeTypedefNameDecl(NewTD, Previous); |
Eli Friedman | bf87f2c | 2010-08-10 03:13:15 +0000 | [diff] [blame] | 4608 | } |
4609 | |||||
Douglas Gregor | c29f77b | 2009-07-07 16:35:42 +0000 | [diff] [blame] | 4610 | // If this is the C FILE type, notify the AST context. |
4611 | if (IdentifierInfo *II = NewTD->getIdentifier()) | ||||
4612 | if (!NewTD->isInvalidDecl() && | ||||
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 4613 | NewTD->getDeclContext()->getRedeclContext()->isTranslationUnit()) { |
Mike Stump | 782fa30 | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 4614 | if (II->isStr("FILE")) |
4615 | Context.setFILEDecl(NewTD); | ||||
4616 | else if (II->isStr("jmp_buf")) | ||||
4617 | Context.setjmp_bufDecl(NewTD); | ||||
4618 | else if (II->isStr("sigjmp_buf")) | ||||
4619 | Context.setsigjmp_bufDecl(NewTD); | ||||
Rafael Espindola | e2d4f4e | 2011-11-13 21:51:09 +0000 | [diff] [blame] | 4620 | else if (II->isStr("ucontext_t")) |
4621 | Context.setucontext_tDecl(NewTD); | ||||
Mike Stump | 782fa30 | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 4622 | } |
4623 | |||||
Zhongxing Xu | d5ed8c3 | 2009-01-16 03:34:13 +0000 | [diff] [blame] | 4624 | return NewTD; |
4625 | } | ||||
4626 | |||||
Douglas Gregor | 8f30105 | 2009-02-24 19:23:27 +0000 | [diff] [blame] | 4627 | /// \brief Determines whether the given declaration is an out-of-scope |
4628 | /// previous declaration. | ||||
4629 | /// | ||||
4630 | /// This routine should be invoked when name lookup has found a | ||||
4631 | /// previous declaration (PrevDecl) that is not in the scope where a | ||||
4632 | /// new declaration by the same name is being introduced. If the new | ||||
4633 | /// declaration occurs in a local scope, previous declarations with | ||||
4634 | /// linkage may still be considered previous declarations (C99 | ||||
4635 | /// 6.2.2p4-5, C++ [basic.link]p6). | ||||
4636 | /// | ||||
4637 | /// \param PrevDecl the previous declaration found by name | ||||
4638 | /// lookup | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4639 | /// |
Douglas Gregor | 8f30105 | 2009-02-24 19:23:27 +0000 | [diff] [blame] | 4640 | /// \param DC the context in which the new declaration is being |
4641 | /// declared. | ||||
4642 | /// | ||||
4643 | /// \returns true if PrevDecl is an out-of-scope previous declaration | ||||
4644 | /// for a new delcaration with the same name. | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4645 | static bool |
Douglas Gregor | 8f30105 | 2009-02-24 19:23:27 +0000 | [diff] [blame] | 4646 | isOutOfScopePreviousDeclaration(NamedDecl *PrevDecl, DeclContext *DC, |
4647 | ASTContext &Context) { | ||||
4648 | if (!PrevDecl) | ||||
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 4649 | return false; |
Douglas Gregor | 8f30105 | 2009-02-24 19:23:27 +0000 | [diff] [blame] | 4650 | |
Douglas Gregor | d6f7e9d | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 4651 | if (!PrevDecl->hasLinkage()) |
4652 | return false; | ||||
Douglas Gregor | 8f30105 | 2009-02-24 19:23:27 +0000 | [diff] [blame] | 4653 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4654 | if (Context.getLangOpts().CPlusPlus) { |
Douglas Gregor | 8f30105 | 2009-02-24 19:23:27 +0000 | [diff] [blame] | 4655 | // C++ [basic.link]p6: |
4656 | // If there is a visible declaration of an entity with linkage | ||||
4657 | // having the same name and type, ignoring entities declared | ||||
4658 | // outside the innermost enclosing namespace scope, the block | ||||
4659 | // scope declaration declares that same entity and receives the | ||||
4660 | // linkage of the previous declaration. | ||||
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 4661 | DeclContext *OuterContext = DC->getRedeclContext(); |
Douglas Gregor | 8f30105 | 2009-02-24 19:23:27 +0000 | [diff] [blame] | 4662 | if (!OuterContext->isFunctionOrMethod()) |
4663 | // This rule only applies to block-scope declarations. | ||||
4664 | return false; | ||||
Douglas Gregor | 757c600 | 2010-08-27 22:55:10 +0000 | [diff] [blame] | 4665 | |
4666 | DeclContext *PrevOuterContext = PrevDecl->getDeclContext(); | ||||
4667 | if (PrevOuterContext->isRecord()) | ||||
4668 | // We found a member function: ignore it. | ||||
4669 | return false; | ||||
4670 | |||||
4671 | // Find the innermost enclosing namespace for the new and | ||||
4672 | // previous declarations. | ||||
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 4673 | OuterContext = OuterContext->getEnclosingNamespaceContext(); |
4674 | PrevOuterContext = PrevOuterContext->getEnclosingNamespaceContext(); | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4675 | |
Douglas Gregor | 757c600 | 2010-08-27 22:55:10 +0000 | [diff] [blame] | 4676 | // The previous declaration is in a different namespace, so it |
4677 | // isn't the same function. | ||||
4678 | if (!OuterContext->Equals(PrevOuterContext)) | ||||
4679 | return false; | ||||
Douglas Gregor | 8f30105 | 2009-02-24 19:23:27 +0000 | [diff] [blame] | 4680 | } |
4681 | |||||
Douglas Gregor | 8f30105 | 2009-02-24 19:23:27 +0000 | [diff] [blame] | 4682 | return true; |
4683 | } | ||||
4684 | |||||
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4685 | static void SetNestedNameSpecifier(DeclaratorDecl *DD, Declarator &D) { |
4686 | CXXScopeSpec &SS = D.getCXXScopeSpec(); | ||||
4687 | if (!SS.isSet()) return; | ||||
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 4688 | DD->setQualifierInfo(SS.getWithLocInContext(DD->getASTContext())); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4689 | } |
4690 | |||||
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4691 | bool Sema::inferObjCARCLifetime(ValueDecl *decl) { |
4692 | QualType type = decl->getType(); | ||||
4693 | Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime(); | ||||
4694 | if (lifetime == Qualifiers::OCL_Autoreleasing) { | ||||
4695 | // Various kinds of declaration aren't allowed to be __autoreleasing. | ||||
4696 | unsigned kind = -1U; | ||||
4697 | if (VarDecl *var = dyn_cast<VarDecl>(decl)) { | ||||
4698 | if (var->hasAttr<BlocksAttr>()) | ||||
4699 | kind = 0; // __block | ||||
4700 | else if (!var->hasLocalStorage()) | ||||
4701 | kind = 1; // global | ||||
4702 | } else if (isa<ObjCIvarDecl>(decl)) { | ||||
4703 | kind = 3; // ivar | ||||
4704 | } else if (isa<FieldDecl>(decl)) { | ||||
4705 | kind = 2; // field | ||||
4706 | } | ||||
4707 | |||||
4708 | if (kind != -1U) { | ||||
4709 | Diag(decl->getLocation(), diag::err_arc_autoreleasing_var) | ||||
4710 | << kind; | ||||
4711 | } | ||||
4712 | } else if (lifetime == Qualifiers::OCL_None) { | ||||
4713 | // Try to infer lifetime. | ||||
4714 | if (!type->isObjCLifetimeType()) | ||||
4715 | return false; | ||||
4716 | |||||
4717 | lifetime = type->getObjCARCImplicitLifetime(); | ||||
4718 | type = Context.getLifetimeQualifiedType(type, lifetime); | ||||
4719 | decl->setType(type); | ||||
4720 | } | ||||
4721 | |||||
4722 | if (VarDecl *var = dyn_cast<VarDecl>(decl)) { | ||||
4723 | // Thread-local variables cannot have lifetime. | ||||
4724 | if (lifetime && lifetime != Qualifiers::OCL_ExplicitNone && | ||||
Richard Smith | 38afbc7 | 2013-04-13 02:43:54 +0000 | [diff] [blame] | 4725 | var->getTLSKind()) { |
Argyrios Kyrtzidis | b8b0313 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 4726 | Diag(var->getLocation(), diag::err_arc_thread_ownership) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4727 | << var->getType(); |
4728 | return true; | ||||
4729 | } | ||||
4730 | } | ||||
4731 | |||||
4732 | return false; | ||||
4733 | } | ||||
4734 | |||||
Rafael Espindola | 2a5bb50 | 2013-01-16 23:11:15 +0000 | [diff] [blame] | 4735 | static void checkAttributesAfterMerging(Sema &S, NamedDecl &ND) { |
4736 | // 'weak' only applies to declarations with external linkage. | ||||
Rafael Espindola | 4d8a33b | 2013-01-16 23:49:06 +0000 | [diff] [blame] | 4737 | if (WeakAttr *Attr = ND.getAttr<WeakAttr>()) { |
Rafael Espindola | 181e3ec | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 4738 | if (!ND.isExternallyVisible()) { |
Rafael Espindola | 4d8a33b | 2013-01-16 23:49:06 +0000 | [diff] [blame] | 4739 | S.Diag(Attr->getLocation(), diag::err_attribute_weak_static); |
4740 | ND.dropAttr<WeakAttr>(); | ||||
4741 | } | ||||
4742 | } | ||||
4743 | if (WeakRefAttr *Attr = ND.getAttr<WeakRefAttr>()) { | ||||
Rafael Espindola | 181e3ec | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 4744 | if (ND.isExternallyVisible()) { |
Rafael Espindola | 4d8a33b | 2013-01-16 23:49:06 +0000 | [diff] [blame] | 4745 | S.Diag(Attr->getLocation(), diag::err_attribute_weakref_not_static); |
4746 | ND.dropAttr<WeakRefAttr>(); | ||||
4747 | } | ||||
Rafael Espindola | 2a5bb50 | 2013-01-16 23:11:15 +0000 | [diff] [blame] | 4748 | } |
Reid Kleckner | a722534 | 2013-05-20 14:02:37 +0000 | [diff] [blame] | 4749 | |
4750 | // 'selectany' only applies to externally visible varable declarations. | ||||
4751 | // It does not apply to functions. | ||||
4752 | if (SelectAnyAttr *Attr = ND.getAttr<SelectAnyAttr>()) { | ||||
4753 | if (isa<FunctionDecl>(ND) || !ND.isExternallyVisible()) { | ||||
4754 | S.Diag(Attr->getLocation(), diag::err_attribute_selectany_non_extern_data); | ||||
4755 | ND.dropAttr<SelectAnyAttr>(); | ||||
4756 | } | ||||
4757 | } | ||||
Rafael Espindola | 2a5bb50 | 2013-01-16 23:11:15 +0000 | [diff] [blame] | 4758 | } |
4759 | |||||
John McCall | b421d92 | 2013-04-02 02:48:58 +0000 | [diff] [blame] | 4760 | /// Given that we are within the definition of the given function, |
4761 | /// will that definition behave like C99's 'inline', where the | ||||
4762 | /// definition is discarded except for optimization purposes? | ||||
4763 | static bool isFunctionDefinitionDiscarded(Sema &S, FunctionDecl *FD) { | ||||
4764 | // Try to avoid calling GetGVALinkageForFunction. | ||||
4765 | |||||
4766 | // All cases of this require the 'inline' keyword. | ||||
4767 | if (!FD->isInlined()) return false; | ||||
4768 | |||||
4769 | // This is only possible in C++ with the gnu_inline attribute. | ||||
4770 | if (S.getLangOpts().CPlusPlus && !FD->hasAttr<GNUInlineAttr>()) | ||||
4771 | return false; | ||||
4772 | |||||
4773 | // Okay, go ahead and call the relatively-more-expensive function. | ||||
4774 | |||||
4775 | #ifndef NDEBUG | ||||
4776 | // AST quite reasonably asserts that it's working on a function | ||||
4777 | // definition. We don't really have a way to tell it that we're | ||||
4778 | // currently defining the function, so just lie to it in +Asserts | ||||
4779 | // builds. This is an awful hack. | ||||
4780 | FD->setLazyBody(1); | ||||
4781 | #endif | ||||
4782 | |||||
4783 | bool isC99Inline = (S.Context.GetGVALinkageForFunction(FD) == GVA_C99Inline); | ||||
4784 | |||||
4785 | #ifndef NDEBUG | ||||
4786 | FD->setLazyBody(0); | ||||
4787 | #endif | ||||
4788 | |||||
4789 | return isC99Inline; | ||||
4790 | } | ||||
4791 | |||||
Richard Smith | aa4bc18 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 4792 | /// Determine whether a variable is extern "C" prior to attaching |
4793 | /// an initializer. We can't just call isExternC() here, because that | ||||
4794 | /// will also compute and cache whether the declaration is externally | ||||
4795 | /// visible, which might change when we attach the initializer. | ||||
4796 | /// | ||||
4797 | /// This can only be used if the declaration is known to not be a | ||||
4798 | /// redeclaration of an internal linkage declaration. | ||||
4799 | /// | ||||
4800 | /// For instance: | ||||
4801 | /// | ||||
4802 | /// auto x = []{}; | ||||
4803 | /// | ||||
4804 | /// Attaching the initializer here makes this declaration not externally | ||||
4805 | /// visible, because its type has internal linkage. | ||||
4806 | /// | ||||
4807 | /// FIXME: This is a hack. | ||||
4808 | template<typename T> | ||||
4809 | static bool isIncompleteDeclExternC(Sema &S, const T *D) { | ||||
4810 | if (S.getLangOpts().CPlusPlus) { | ||||
4811 | // In C++, the overloadable attribute negates the effects of extern "C". | ||||
4812 | if (!D->isInExternCContext() || D->template hasAttr<OverloadableAttr>()) | ||||
4813 | return false; | ||||
4814 | } | ||||
4815 | return D->isExternC(); | ||||
4816 | } | ||||
4817 | |||||
Rafael Espindola | 2d1b096 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 4818 | static bool shouldConsiderLinkage(const VarDecl *VD) { |
4819 | const DeclContext *DC = VD->getDeclContext()->getRedeclContext(); | ||||
4820 | if (DC->isFunctionOrMethod()) | ||||
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 4821 | return VD->hasExternalStorage(); |
Rafael Espindola | 2d1b096 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 4822 | if (DC->isFileContext()) |
4823 | return true; | ||||
4824 | if (DC->isRecord()) | ||||
4825 | return false; | ||||
4826 | llvm_unreachable("Unexpected context"); | ||||
4827 | } | ||||
4828 | |||||
4829 | static bool shouldConsiderLinkage(const FunctionDecl *FD) { | ||||
4830 | const DeclContext *DC = FD->getDeclContext()->getRedeclContext(); | ||||
4831 | if (DC->isFileContext() || DC->isFunctionOrMethod()) | ||||
4832 | return true; | ||||
4833 | if (DC->isRecord()) | ||||
4834 | return false; | ||||
4835 | llvm_unreachable("Unexpected context"); | ||||
4836 | } | ||||
4837 | |||||
Richard Smith | a41c97a | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 4838 | /// Adjust the \c DeclContext for a function or variable that might be a |
4839 | /// function-local external declaration. | ||||
4840 | bool Sema::adjustContextForLocalExternDecl(DeclContext *&DC) { | ||||
4841 | if (!DC->isFunctionOrMethod()) | ||||
4842 | return false; | ||||
4843 | |||||
4844 | // If this is a local extern function or variable declared within a function | ||||
4845 | // template, don't add it into the enclosing namespace scope until it is | ||||
4846 | // instantiated; it might have a dependent type right now. | ||||
4847 | if (DC->isDependentContext()) | ||||
4848 | return true; | ||||
4849 | |||||
4850 | // C++11 [basic.link]p7: | ||||
4851 | // When a block scope declaration of an entity with linkage is not found to | ||||
4852 | // refer to some other declaration, then that entity is a member of the | ||||
4853 | // innermost enclosing namespace. | ||||
4854 | // | ||||
4855 | // Per C++11 [namespace.def]p6, the innermost enclosing namespace is a | ||||
4856 | // semantically-enclosing namespace, not a lexically-enclosing one. | ||||
4857 | while (!DC->isFileContext() && !isa<LinkageSpecDecl>(DC)) | ||||
4858 | DC = DC->getParent(); | ||||
4859 | return true; | ||||
4860 | } | ||||
4861 | |||||
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4862 | NamedDecl * |
Chris Lattner | 16c5dea | 2010-10-10 18:16:20 +0000 | [diff] [blame] | 4863 | Sema::ActOnVariableDeclarator(Scope *S, Declarator &D, DeclContext *DC, |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 4864 | TypeSourceInfo *TInfo, LookupResult &Previous, |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4865 | MultiTemplateParamsArg TemplateParamLists, |
4866 | bool &AddToScope) { | ||||
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 4867 | QualType R = TInfo->getType(); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4868 | DeclarationName Name = GetNameForDeclarator(D).getName(); |
Zhongxing Xu | cb8f4f1 | 2009-01-16 02:36:34 +0000 | [diff] [blame] | 4869 | |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 4870 | DeclSpec::SCS SCSpec = D.getDeclSpec().getStorageClassSpec(); |
Rafael Espindola | 65dfa2b | 2013-04-25 12:11:36 +0000 | [diff] [blame] | 4871 | VarDecl::StorageClass SC = |
4872 | StorageClassSpecToVarDeclStorageClass(D.getDeclSpec()); | ||||
Joey Gouly | 19dbb20 | 2013-01-23 11:56:20 +0000 | [diff] [blame] | 4873 | |
Richard Smith | a41c97a | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 4874 | DeclContext *OriginalDC = DC; |
4875 | bool IsLocalExternDecl = SC == SC_Extern && | ||||
4876 | adjustContextForLocalExternDecl(DC); | ||||
4877 | |||||
Richard Smith | df4cc0a | 2013-04-15 08:33:22 +0000 | [diff] [blame] | 4878 | if (getLangOpts().OpenCL && !getOpenCLOptions().cl_khr_fp16) { |
Joey Gouly | 19dbb20 | 2013-01-23 11:56:20 +0000 | [diff] [blame] | 4879 | // OpenCL v1.2 s6.1.1.1: reject declaring variables of the half and |
4880 | // half array type (unless the cl_khr_fp16 extension is enabled). | ||||
4881 | if (Context.getBaseElementType(R)->isHalfType()) { | ||||
4882 | Diag(D.getIdentifierLoc(), diag::err_opencl_half_declaration) << R; | ||||
4883 | D.setInvalidType(); | ||||
4884 | } | ||||
4885 | } | ||||
4886 | |||||
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 4887 | if (SCSpec == DeclSpec::SCS_mutable) { |
Zhongxing Xu | cb8f4f1 | 2009-01-16 02:36:34 +0000 | [diff] [blame] | 4888 | // mutable can only appear on non-static class members, so it's always |
4889 | // an error here | ||||
4890 | Diag(D.getIdentifierLoc(), diag::err_mutable_nonmember); | ||||
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 4891 | D.setInvalidType(); |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 4892 | SC = SC_None; |
Zhongxing Xu | cb8f4f1 | 2009-01-16 02:36:34 +0000 | [diff] [blame] | 4893 | } |
John McCall | b421d92 | 2013-04-02 02:48:58 +0000 | [diff] [blame] | 4894 | |
Richard Smith | 9109bf1 | 2013-06-17 01:34:01 +0000 | [diff] [blame] | 4895 | if (getLangOpts().CPlusPlus11 && SCSpec == DeclSpec::SCS_register && |
4896 | !D.getAsmLabel() && !getSourceManager().isInSystemMacro( | ||||
4897 | D.getDeclSpec().getStorageClassSpecLoc())) { | ||||
4898 | // In C++11, the 'register' storage class specifier is deprecated. | ||||
4899 | // Suppress the warning in system macros, it's used in macros in some | ||||
4900 | // popular C system headers, such as in glibc's htonl() macro. | ||||
4901 | Diag(D.getDeclSpec().getStorageClassSpecLoc(), | ||||
4902 | diag::warn_deprecated_register) | ||||
4903 | << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc()); | ||||
4904 | } | ||||
4905 | |||||
Zhongxing Xu | cb8f4f1 | 2009-01-16 02:36:34 +0000 | [diff] [blame] | 4906 | IdentifierInfo *II = Name.getAsIdentifierInfo(); |
4907 | if (!II) { | ||||
4908 | Diag(D.getIdentifierLoc(), diag::err_bad_variable_name) | ||||
Douglas Gregor | b5a0187 | 2011-10-09 18:55:59 +0000 | [diff] [blame] | 4909 | << Name; |
Zhongxing Xu | cb8f4f1 | 2009-01-16 02:36:34 +0000 | [diff] [blame] | 4910 | return 0; |
4911 | } | ||||
4912 | |||||
Richard Smith | c7f8116 | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 4913 | DiagnoseFunctionSpecifiers(D.getDeclSpec()); |
Douglas Gregor | 021c3b3 | 2009-03-11 23:00:04 +0000 | [diff] [blame] | 4914 | |
Douglas Gregor | 2d2e9cf | 2009-03-11 20:22:50 +0000 | [diff] [blame] | 4915 | if (!DC->isRecord() && S->getFnParent() == 0) { |
4916 | // C99 6.9p2: The storage-class specifiers auto and register shall not | ||||
4917 | // appear in the declaration specifiers in an external declaration. | ||||
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 4918 | if (SC == SC_Auto || SC == SC_Register) { |
Chris Lattner | d4b19d5 | 2009-05-12 21:44:00 +0000 | [diff] [blame] | 4919 | // If this is a register variable with an asm label specified, then this |
4920 | // is a GNU extension. | ||||
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 4921 | if (SC == SC_Register && D.getAsmLabel()) |
Chris Lattner | d4b19d5 | 2009-05-12 21:44:00 +0000 | [diff] [blame] | 4922 | Diag(D.getIdentifierLoc(), diag::err_unsupported_global_register); |
4923 | else | ||||
4924 | Diag(D.getIdentifierLoc(), diag::err_typecheck_sclass_fscope); | ||||
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 4925 | D.setInvalidType(); |
Zhongxing Xu | cb8f4f1 | 2009-01-16 02:36:34 +0000 | [diff] [blame] | 4926 | } |
Zhongxing Xu | cb8f4f1 | 2009-01-16 02:36:34 +0000 | [diff] [blame] | 4927 | } |
Richard Smith | 9109bf1 | 2013-06-17 01:34:01 +0000 | [diff] [blame] | 4928 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4929 | if (getLangOpts().OpenCL) { |
Peter Collingbourne | 8c25fc5 | 2011-09-19 21:14:35 +0000 | [diff] [blame] | 4930 | // Set up the special work-group-local storage class for variables in the |
4931 | // OpenCL __local address space. | ||||
Rafael Espindola | 0db661e | 2012-12-21 01:21:33 +0000 | [diff] [blame] | 4932 | if (R.getAddressSpace() == LangAS::opencl_local) { |
Peter Collingbourne | 8c25fc5 | 2011-09-19 21:14:35 +0000 | [diff] [blame] | 4933 | SC = SC_OpenCLWorkGroupLocal; |
Rafael Espindola | 0db661e | 2012-12-21 01:21:33 +0000 | [diff] [blame] | 4934 | } |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 4935 | |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 4936 | // OpenCL v1.2 s6.9.b p4: |
4937 | // The sampler type cannot be used with the __local and __global address | ||||
4938 | // space qualifiers. | ||||
4939 | if (R->isSamplerT() && (R.getAddressSpace() == LangAS::opencl_local || | ||||
4940 | R.getAddressSpace() == LangAS::opencl_global)) { | ||||
4941 | Diag(D.getIdentifierLoc(), diag::err_wrong_sampler_addressspace); | ||||
4942 | } | ||||
4943 | |||||
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 4944 | // OpenCL 1.2 spec, p6.9 r: |
4945 | // The event type cannot be used to declare a program scope variable. | ||||
4946 | // The event type cannot be used with the __local, __constant and __global | ||||
4947 | // address space qualifiers. | ||||
4948 | if (R->isEventT()) { | ||||
4949 | if (S->getParent() == 0) { | ||||
4950 | Diag(D.getLocStart(), diag::err_event_t_global_var); | ||||
4951 | D.setInvalidType(); | ||||
4952 | } | ||||
4953 | |||||
4954 | if (R.getAddressSpace()) { | ||||
4955 | Diag(D.getLocStart(), diag::err_event_t_addr_space_qual); | ||||
4956 | D.setInvalidType(); | ||||
4957 | } | ||||
4958 | } | ||||
Peter Collingbourne | 8c25fc5 | 2011-09-19 21:14:35 +0000 | [diff] [blame] | 4959 | } |
4960 | |||||
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4961 | bool IsExplicitSpecialization = false; |
4962 | bool IsVariableTemplateSpecialization = false; | ||||
4963 | bool IsPartialSpecialization = false; | ||||
Larisse Voufo | 4a91989 | 2013-08-14 03:09:19 +0000 | [diff] [blame] | 4964 | bool IsVariableTemplate = false; |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4965 | VarTemplateDecl *PrevVarTemplate = 0; |
Larisse Voufo | 567f917 | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 4966 | VarDecl *NewVD = 0; |
4967 | VarTemplateDecl *NewTemplate = 0; | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4968 | if (!getLangOpts().CPlusPlus) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4969 | NewVD = VarDecl::Create(Context, DC, D.getLocStart(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4970 | D.getIdentifierLoc(), II, |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 4971 | R, TInfo, SC); |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 4972 | |
4973 | if (D.isInvalidType()) | ||||
4974 | NewVD->setInvalidDecl(); | ||||
4975 | } else { | ||||
Larisse Voufo | 567f917 | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 4976 | bool Invalid = false; |
4977 | |||||
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 4978 | if (DC->isRecord() && !CurContext->isRecord()) { |
4979 | // This is an out-of-line definition of a static data member. | ||||
Rafael Espindola | 3882aed | 2013-06-19 13:41:54 +0000 | [diff] [blame] | 4980 | switch (SC) { |
4981 | case SC_None: | ||||
4982 | break; | ||||
4983 | case SC_Static: | ||||
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 4984 | Diag(D.getDeclSpec().getStorageClassSpecLoc(), |
4985 | diag::err_static_out_of_line) | ||||
4986 | << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc()); | ||||
Rafael Espindola | 3882aed | 2013-06-19 13:41:54 +0000 | [diff] [blame] | 4987 | break; |
4988 | case SC_Auto: | ||||
4989 | case SC_Register: | ||||
4990 | case SC_Extern: | ||||
4991 | // [dcl.stc] p2: The auto or register specifiers shall be applied only | ||||
4992 | // to names of variables declared in a block or to function parameters. | ||||
4993 | // [dcl.stc] p6: The extern specifier cannot be used in the declaration | ||||
4994 | // of class members | ||||
4995 | |||||
4996 | Diag(D.getDeclSpec().getStorageClassSpecLoc(), | ||||
4997 | diag::err_storage_class_for_static_member) | ||||
4998 | << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc()); | ||||
4999 | break; | ||||
5000 | case SC_PrivateExtern: | ||||
5001 | llvm_unreachable("C storage class in c++!"); | ||||
5002 | case SC_OpenCLWorkGroupLocal: | ||||
5003 | llvm_unreachable("OpenCL storage class in c++!"); | ||||
Rafael Espindola | ea4b111 | 2013-04-04 21:21:25 +0000 | [diff] [blame] | 5004 | } |
Larisse Voufo | 06935f3 | 2013-08-06 03:43:07 +0000 | [diff] [blame] | 5005 | } |
5006 | |||||
Richard Smith | b9c64d8 | 2012-02-16 20:41:22 +0000 | [diff] [blame] | 5007 | if (SC == SC_Static && CurContext->isRecord()) { |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 5008 | if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(DC)) { |
5009 | if (RD->isLocalClass()) | ||||
5010 | Diag(D.getIdentifierLoc(), | ||||
5011 | diag::err_static_data_member_not_allowed_in_local_class) | ||||
5012 | << Name << RD->getDeclName(); | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5013 | |
Richard Smith | b9c64d8 | 2012-02-16 20:41:22 +0000 | [diff] [blame] | 5014 | // C++98 [class.union]p1: If a union contains a static data member, |
5015 | // the program is ill-formed. C++11 drops this restriction. | ||||
5016 | if (RD->isUnion()) | ||||
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 5017 | Diag(D.getIdentifierLoc(), |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5018 | getLangOpts().CPlusPlus11 |
Richard Smith | b9c64d8 | 2012-02-16 20:41:22 +0000 | [diff] [blame] | 5019 | ? diag::warn_cxx98_compat_static_data_member_in_union |
5020 | : diag::ext_static_data_member_in_union) << Name; | ||||
5021 | // We conservatively disallow static data members in anonymous structs. | ||||
5022 | else if (!RD->getDeclName()) | ||||
5023 | Diag(D.getIdentifierLoc(), | ||||
5024 | diag::err_static_data_member_not_allowed_in_anon_struct) | ||||
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 5025 | << Name << RD->isUnion(); |
5026 | } | ||||
5027 | } | ||||
5028 | |||||
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5029 | NamedDecl *PrevDecl = 0; |
5030 | if (Previous.begin() != Previous.end()) | ||||
5031 | PrevDecl = (*Previous.begin())->getUnderlyingDecl(); | ||||
5032 | PrevVarTemplate = dyn_cast_or_null<VarTemplateDecl>(PrevDecl); | ||||
5033 | |||||
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 5034 | // Match up the template parameter lists with the scope specifier, then |
5035 | // determine whether we have a template or a template specialization. | ||||
Larisse Voufo | 567f917 | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 5036 | TemplateParameterList *TemplateParams = |
5037 | MatchTemplateParametersToScopeSpecifier( | ||||
5038 | D.getDeclSpec().getLocStart(), D.getIdentifierLoc(), | ||||
5039 | D.getCXXScopeSpec(), TemplateParamLists, | ||||
5040 | /*never a friend*/ false, IsExplicitSpecialization, Invalid); | ||||
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5041 | if (TemplateParams) { |
5042 | if (!TemplateParams->size() && | ||||
5043 | D.getName().getKind() != UnqualifiedId::IK_TemplateId) { | ||||
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 5044 | // There is an extraneous 'template<>' for this variable. Complain |
5045 | // about it, but allow the declaration of the variable. | ||||
5046 | Diag(TemplateParams->getTemplateLoc(), | ||||
5047 | diag::err_template_variable_noparams) | ||||
5048 | << II | ||||
5049 | << SourceRange(TemplateParams->getTemplateLoc(), | ||||
5050 | TemplateParams->getRAngleLoc()); | ||||
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5051 | } else { |
5052 | // Only C++1y supports variable templates (N3651). | ||||
5053 | Diag(D.getIdentifierLoc(), | ||||
5054 | getLangOpts().CPlusPlus1y | ||||
5055 | ? diag::warn_cxx11_compat_variable_template | ||||
5056 | : diag::ext_variable_template); | ||||
5057 | |||||
5058 | if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) { | ||||
5059 | // This is an explicit specialization or a partial specialization. | ||||
5060 | // Check that we can declare a specialization here | ||||
5061 | |||||
5062 | IsVariableTemplateSpecialization = true; | ||||
5063 | IsPartialSpecialization = TemplateParams->size() > 0; | ||||
5064 | |||||
5065 | } else { // if (TemplateParams->size() > 0) | ||||
Larisse Voufo | 06935f3 | 2013-08-06 03:43:07 +0000 | [diff] [blame] | 5066 | // This is a template declaration. |
Larisse Voufo | 4a91989 | 2013-08-14 03:09:19 +0000 | [diff] [blame] | 5067 | IsVariableTemplate = true; |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5068 | |
5069 | // Check that we can declare a template here. | ||||
5070 | if (CheckTemplateDeclScope(S, TemplateParams)) | ||||
5071 | return 0; | ||||
5072 | |||||
5073 | // If there is a previous declaration with the same name, check | ||||
5074 | // whether this is a valid redeclaration. | ||||
5075 | if (PrevDecl && !isDeclInScope(PrevDecl, DC, S)) | ||||
5076 | PrevDecl = PrevVarTemplate = 0; | ||||
5077 | |||||
5078 | if (PrevVarTemplate) { | ||||
5079 | // Ensure that the template parameter lists are compatible. | ||||
5080 | if (!TemplateParameterListsAreEqual( | ||||
5081 | TemplateParams, PrevVarTemplate->getTemplateParameters(), | ||||
5082 | /*Complain=*/true, TPL_TemplateMatch)) | ||||
5083 | return 0; | ||||
5084 | } else if (PrevDecl && PrevDecl->isTemplateParameter()) { | ||||
5085 | // Maybe we will complain about the shadowed template parameter. | ||||
5086 | DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), PrevDecl); | ||||
5087 | |||||
5088 | // Just pretend that we didn't see the previous declaration. | ||||
5089 | PrevDecl = 0; | ||||
5090 | } else if (PrevDecl) { | ||||
5091 | // C++ [temp]p5: | ||||
5092 | // ... a template name declared in namespace scope or in class | ||||
5093 | // scope shall be unique in that scope. | ||||
5094 | Diag(D.getIdentifierLoc(), diag::err_redefinition_different_kind) | ||||
5095 | << Name; | ||||
5096 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); | ||||
5097 | return 0; | ||||
5098 | } | ||||
5099 | |||||
5100 | // Check the template parameter list of this declaration, possibly | ||||
5101 | // merging in the template parameter list from the previous variable | ||||
5102 | // template declaration. | ||||
5103 | if (CheckTemplateParameterList( | ||||
5104 | TemplateParams, | ||||
5105 | PrevVarTemplate ? PrevVarTemplate->getTemplateParameters() | ||||
5106 | : 0, | ||||
5107 | (D.getCXXScopeSpec().isSet() && DC && DC->isRecord() && | ||||
5108 | DC->isDependentContext()) | ||||
5109 | ? TPC_ClassTemplateMember | ||||
5110 | : TPC_VarTemplate)) | ||||
5111 | Invalid = true; | ||||
5112 | |||||
5113 | if (D.getCXXScopeSpec().isSet()) { | ||||
5114 | // If the name of the template was qualified, we must be defining | ||||
5115 | // the template out-of-line. | ||||
5116 | if (!D.getCXXScopeSpec().isInvalid() && !Invalid && | ||||
5117 | !PrevVarTemplate) { | ||||
Richard Smith | 4e9686b | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 5118 | Diag(D.getIdentifierLoc(), diag::err_member_decl_does_not_match) |
5119 | << Name << DC << /*IsDefinition*/true | ||||
5120 | << D.getCXXScopeSpec().getRange(); | ||||
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5121 | Invalid = true; |
5122 | } | ||||
5123 | } | ||||
5124 | } | ||||
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 5125 | } |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5126 | } else if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) { |
5127 | TemplateIdAnnotation *TemplateId = D.getName().TemplateId; | ||||
5128 | |||||
5129 | // We have encountered something that the user meant to be a | ||||
5130 | // specialization (because it has explicitly-specified template | ||||
5131 | // arguments) but that was not introduced with a "template<>" (or had | ||||
5132 | // too few of them). | ||||
5133 | // FIXME: Differentiate between attempts for explicit instantiations | ||||
5134 | // (starting with "template") and the rest. | ||||
5135 | Diag(D.getIdentifierLoc(), diag::err_template_spec_needs_header) | ||||
5136 | << SourceRange(TemplateId->LAngleLoc, TemplateId->RAngleLoc) | ||||
5137 | << FixItHint::CreateInsertion(D.getDeclSpec().getLocStart(), | ||||
5138 | "template<> "); | ||||
5139 | IsVariableTemplateSpecialization = true; | ||||
Douglas Gregor | dfe3f2d | 2009-07-22 17:18:37 +0000 | [diff] [blame] | 5140 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5141 | |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5142 | if (IsVariableTemplateSpecialization) { |
5143 | if (!PrevVarTemplate) { | ||||
5144 | Diag(D.getIdentifierLoc(), diag::err_var_spec_no_template) | ||||
5145 | << IsPartialSpecialization; | ||||
5146 | return 0; | ||||
5147 | } | ||||
5148 | |||||
5149 | SourceLocation TemplateKWLoc = | ||||
5150 | TemplateParamLists.size() > 0 | ||||
5151 | ? TemplateParamLists[0]->getTemplateLoc() | ||||
5152 | : SourceLocation(); | ||||
5153 | DeclResult Res = ActOnVarTemplateSpecialization( | ||||
5154 | S, PrevVarTemplate, D, TInfo, TemplateKWLoc, TemplateParams, SC, | ||||
5155 | IsPartialSpecialization); | ||||
5156 | if (Res.isInvalid()) | ||||
5157 | return 0; | ||||
5158 | NewVD = cast<VarDecl>(Res.get()); | ||||
5159 | AddToScope = false; | ||||
5160 | } else | ||||
5161 | NewVD = VarDecl::Create(Context, DC, D.getLocStart(), | ||||
5162 | D.getIdentifierLoc(), II, R, TInfo, SC); | ||||
Eli Friedman | 63054b3 | 2009-04-19 20:27:55 +0000 | [diff] [blame] | 5163 | |
Larisse Voufo | 567f917 | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 5164 | // If this is supposed to be a variable template, create it as such. |
5165 | if (IsVariableTemplate) { | ||||
5166 | NewTemplate = | ||||
5167 | VarTemplateDecl::Create(Context, DC, D.getIdentifierLoc(), Name, | ||||
5168 | TemplateParams, NewVD, PrevVarTemplate); | ||||
5169 | NewVD->setDescribedVarTemplate(NewTemplate); | ||||
5170 | } | ||||
5171 | |||||
Richard Smith | 483b9f3 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 5172 | // If this decl has an auto type in need of deduction, make a note of the |
5173 | // Decl so we can diagnose uses of it in its own initializer. | ||||
Richard Smith | a2c3646 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 5174 | if (D.getDeclSpec().containsPlaceholderType() && R->getContainedAutoType()) |
Richard Smith | 483b9f3 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 5175 | ParsingInitForAutoVars.insert(NewVD); |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 5176 | |
Larisse Voufo | 567f917 | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 5177 | if (D.isInvalidType() || Invalid) { |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 5178 | NewVD->setInvalidDecl(); |
Larisse Voufo | 567f917 | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 5179 | if (NewTemplate) |
5180 | NewTemplate->setInvalidDecl(); | ||||
5181 | } | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5182 | |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 5183 | SetNestedNameSpecifier(NewVD, D); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 5184 | |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5185 | // FIXME: Do we need D.getCXXScopeSpec().isSet()? |
5186 | if (TemplateParams && TemplateParamLists.size() > 1 && | ||||
5187 | (!IsVariableTemplateSpecialization || D.getCXXScopeSpec().isSet())) { | ||||
5188 | NewVD->setTemplateParameterListsInfo( | ||||
5189 | Context, TemplateParamLists.size() - 1, TemplateParamLists.data()); | ||||
5190 | } else if (IsVariableTemplateSpecialization || | ||||
5191 | (!TemplateParams && TemplateParamLists.size() > 0 && | ||||
5192 | (D.getCXXScopeSpec().isSet()))) { | ||||
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 5193 | NewVD->setTemplateParameterListsInfo(Context, |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5194 | TemplateParamLists.size(), |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 5195 | TemplateParamLists.data()); |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 5196 | } |
Richard Smith | af1fc7a | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 5197 | |
Richard Smith | 7ca4850 | 2012-02-13 22:16:19 +0000 | [diff] [blame] | 5198 | if (D.getDeclSpec().isConstexprSpecified()) |
Richard Smith | dd4b350 | 2011-12-25 21:17:58 +0000 | [diff] [blame] | 5199 | NewVD->setConstexpr(true); |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 5200 | } |
5201 | |||||
Douglas Gregor | e389585 | 2011-09-12 18:37:38 +0000 | [diff] [blame] | 5202 | // Set the lexical context. If the declarator has a C++ scope specifier, the |
5203 | // lexical context will be different from the semantic context. | ||||
5204 | NewVD->setLexicalDeclContext(CurContext); | ||||
Larisse Voufo | 567f917 | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 5205 | if (NewTemplate) |
5206 | NewTemplate->setLexicalDeclContext(CurContext); | ||||
Douglas Gregor | e389585 | 2011-09-12 18:37:38 +0000 | [diff] [blame] | 5207 | |
Richard Smith | a41c97a | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 5208 | if (IsLocalExternDecl) |
5209 | NewVD->setLocalExternDecl(); | ||||
5210 | |||||
Richard Smith | ec64244 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 5211 | if (DeclSpec::TSCS TSCS = D.getDeclSpec().getThreadStorageClassSpec()) { |
Enea Zaffanella | 9cbcab8 | 2013-05-10 20:34:44 +0000 | [diff] [blame] | 5212 | if (NewVD->hasLocalStorage()) { |
5213 | // C++11 [dcl.stc]p4: | ||||
5214 | // When thread_local is applied to a variable of block scope the | ||||
5215 | // storage-class-specifier static is implied if it does not appear | ||||
5216 | // explicitly. | ||||
5217 | // Core issue: 'static' is not implied if the variable is declared | ||||
5218 | // 'extern'. | ||||
5219 | if (SCSpec == DeclSpec::SCS_unspecified && | ||||
5220 | TSCS == DeclSpec::TSCS_thread_local && | ||||
5221 | DC->isFunctionOrMethod()) | ||||
5222 | NewVD->setTSCSpec(TSCS); | ||||
5223 | else | ||||
5224 | Diag(D.getDeclSpec().getThreadStorageClassSpecLoc(), | ||||
5225 | diag::err_thread_non_global) | ||||
5226 | << DeclSpec::getSpecifierName(TSCS); | ||||
5227 | } else if (!Context.getTargetInfo().isTLSSupported()) | ||||
Richard Smith | ec64244 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 5228 | Diag(D.getDeclSpec().getThreadStorageClassSpecLoc(), |
5229 | diag::err_thread_unsupported); | ||||
Eli Friedman | 63054b3 | 2009-04-19 20:27:55 +0000 | [diff] [blame] | 5230 | else |
Enea Zaffanella | dc17384 | 2013-05-04 08:27:07 +0000 | [diff] [blame] | 5231 | NewVD->setTSCSpec(TSCS); |
Eli Friedman | 63054b3 | 2009-04-19 20:27:55 +0000 | [diff] [blame] | 5232 | } |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 5233 | |
John McCall | b421d92 | 2013-04-02 02:48:58 +0000 | [diff] [blame] | 5234 | // C99 6.7.4p3 |
5235 | // An inline definition of a function with external linkage shall | ||||
5236 | // not contain a definition of a modifiable object with static or | ||||
5237 | // thread storage duration... | ||||
5238 | // We only apply this when the function is required to be defined | ||||
5239 | // elsewhere, i.e. when the function is not 'extern inline'. Note | ||||
5240 | // that a local variable with thread storage duration still has to | ||||
5241 | // be marked 'static'. Also note that it's possible to get these | ||||
5242 | // semantics in C++ using __attribute__((gnu_inline)). | ||||
5243 | if (SC == SC_Static && S->getFnParent() != 0 && | ||||
5244 | !NewVD->getType().isConstQualified()) { | ||||
5245 | FunctionDecl *CurFD = getCurFunctionDecl(); | ||||
5246 | if (CurFD && isFunctionDefinitionDiscarded(*this, CurFD)) { | ||||
5247 | Diag(D.getDeclSpec().getStorageClassSpecLoc(), | ||||
5248 | diag::warn_static_local_in_extern_inline); | ||||
5249 | MaybeSuggestAddingStaticToDecl(CurFD); | ||||
5250 | } | ||||
5251 | } | ||||
5252 | |||||
Douglas Gregor | d023aec | 2011-09-09 20:53:38 +0000 | [diff] [blame] | 5253 | if (D.getDeclSpec().isModulePrivateSpecified()) { |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5254 | if (IsVariableTemplateSpecialization) |
5255 | Diag(NewVD->getLocation(), diag::err_module_private_specialization) | ||||
5256 | << (IsPartialSpecialization ? 1 : 0) | ||||
5257 | << FixItHint::CreateRemoval( | ||||
5258 | D.getDeclSpec().getModulePrivateSpecLoc()); | ||||
5259 | else if (IsExplicitSpecialization) | ||||
Douglas Gregor | d023aec | 2011-09-09 20:53:38 +0000 | [diff] [blame] | 5260 | Diag(NewVD->getLocation(), diag::err_module_private_specialization) |
5261 | << 2 | ||||
5262 | << FixItHint::CreateRemoval(D.getDeclSpec().getModulePrivateSpecLoc()); | ||||
Douglas Gregor | e389585 | 2011-09-12 18:37:38 +0000 | [diff] [blame] | 5263 | else if (NewVD->hasLocalStorage()) |
5264 | Diag(NewVD->getLocation(), diag::err_module_private_local) | ||||
5265 | << 0 << NewVD->getDeclName() | ||||
5266 | << SourceRange(D.getDeclSpec().getModulePrivateSpecLoc()) | ||||
5267 | << FixItHint::CreateRemoval(D.getDeclSpec().getModulePrivateSpecLoc()); | ||||
Larisse Voufo | 567f917 | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 5268 | else { |
Douglas Gregor | d023aec | 2011-09-09 20:53:38 +0000 | [diff] [blame] | 5269 | NewVD->setModulePrivate(); |
Larisse Voufo | 567f917 | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 5270 | if (NewTemplate) |
5271 | NewTemplate->setModulePrivate(); | ||||
5272 | } | ||||
Douglas Gregor | d023aec | 2011-09-09 20:53:38 +0000 | [diff] [blame] | 5273 | } |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 5274 | |
Zhongxing Xu | cb8f4f1 | 2009-01-16 02:36:34 +0000 | [diff] [blame] | 5275 | // Handle attributes prior to checking for duplicates in MergeVarDecl |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 5276 | ProcessDeclAttributes(S, NewVD, D); |
Zhongxing Xu | cb8f4f1 | 2009-01-16 02:36:34 +0000 | [diff] [blame] | 5277 | |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 5278 | if (NewVD->hasAttrs()) |
5279 | CheckAlignasUnderalignment(NewVD); | ||||
5280 | |||||
Peter Collingbourne | c0c0066 | 2012-08-28 20:37:50 +0000 | [diff] [blame] | 5281 | if (getLangOpts().CUDA) { |
5282 | // CUDA B.2.5: "__shared__ and __constant__ variables have implied static | ||||
5283 | // storage [duration]." | ||||
5284 | if (SC == SC_None && S->getFnParent() != 0 && | ||||
Rafael Espindola | 0db661e | 2012-12-21 01:21:33 +0000 | [diff] [blame] | 5285 | (NewVD->hasAttr<CUDASharedAttr>() || |
5286 | NewVD->hasAttr<CUDAConstantAttr>())) { | ||||
Peter Collingbourne | c0c0066 | 2012-08-28 20:37:50 +0000 | [diff] [blame] | 5287 | NewVD->setStorageClass(SC_Static); |
Rafael Espindola | 0db661e | 2012-12-21 01:21:33 +0000 | [diff] [blame] | 5288 | } |
Peter Collingbourne | c0c0066 | 2012-08-28 20:37:50 +0000 | [diff] [blame] | 5289 | } |
5290 | |||||
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5291 | // In auto-retain/release, infer strong retension for variables of |
5292 | // retainable type. | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5293 | if (getLangOpts().ObjCAutoRefCount && inferObjCARCLifetime(NewVD)) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5294 | NewVD->setInvalidDecl(); |
5295 | |||||
Zhongxing Xu | cb8f4f1 | 2009-01-16 02:36:34 +0000 | [diff] [blame] | 5296 | // Handle GNU asm-label extension (encoded as an attribute). |
Chris Lattner | 16c5dea | 2010-10-10 18:16:20 +0000 | [diff] [blame] | 5297 | if (Expr *E = (Expr*)D.getAsmLabel()) { |
Zhongxing Xu | cb8f4f1 | 2009-01-16 02:36:34 +0000 | [diff] [blame] | 5298 | // The parser guarantees this is a string. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5299 | StringLiteral *SE = cast<StringLiteral>(E); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5300 | StringRef Label = SE->getString(); |
Abramo Bagnara | 2b57aef | 2011-01-11 15:16:52 +0000 | [diff] [blame] | 5301 | if (S->getFnParent() != 0) { |
5302 | switch (SC) { | ||||
5303 | case SC_None: | ||||
5304 | case SC_Auto: | ||||
5305 | Diag(E->getExprLoc(), diag::warn_asm_label_on_auto_decl) << Label; | ||||
5306 | break; | ||||
5307 | case SC_Register: | ||||
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 5308 | if (!Context.getTargetInfo().isValidGCCRegisterName(Label)) |
Abramo Bagnara | 2b57aef | 2011-01-11 15:16:52 +0000 | [diff] [blame] | 5309 | Diag(E->getExprLoc(), diag::err_asm_unknown_register_name) << Label; |
5310 | break; | ||||
5311 | case SC_Static: | ||||
5312 | case SC_Extern: | ||||
5313 | case SC_PrivateExtern: | ||||
Peter Collingbourne | 8c25fc5 | 2011-09-19 21:14:35 +0000 | [diff] [blame] | 5314 | case SC_OpenCLWorkGroupLocal: |
Abramo Bagnara | 2b57aef | 2011-01-11 15:16:52 +0000 | [diff] [blame] | 5315 | break; |
5316 | } | ||||
5317 | } | ||||
5318 | |||||
5319 | NewVD->addAttr(::new (Context) AsmLabelAttr(SE->getStrTokenLoc(0), | ||||
Rafael Espindola | baf8695 | 2011-01-01 21:47:03 +0000 | [diff] [blame] | 5320 | Context, Label)); |
David Chisnall | 5f3c163 | 2012-02-18 16:12:34 +0000 | [diff] [blame] | 5321 | } else if (!ExtnameUndeclaredIdentifiers.empty()) { |
5322 | llvm::DenseMap<IdentifierInfo*,AsmLabelAttr*>::iterator I = | ||||
5323 | ExtnameUndeclaredIdentifiers.find(NewVD->getIdentifier()); | ||||
5324 | if (I != ExtnameUndeclaredIdentifiers.end()) { | ||||
5325 | NewVD->addAttr(I->second); | ||||
5326 | ExtnameUndeclaredIdentifiers.erase(I); | ||||
5327 | } | ||||
Zhongxing Xu | cb8f4f1 | 2009-01-16 02:36:34 +0000 | [diff] [blame] | 5328 | } |
5329 | |||||
John McCall | 8472af4 | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 5330 | // Diagnose shadowed variables before filtering for scope. |
John McCall | a369a95 | 2010-03-20 04:12:52 +0000 | [diff] [blame] | 5331 | if (!D.getCXXScopeSpec().isSet()) |
John McCall | 053f4bd | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 5332 | CheckShadow(S, NewVD, Previous); |
John McCall | 8472af4 | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 5333 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5334 | // Don't consider existing declarations that are in a different |
5335 | // scope and are out-of-semantic-context declarations (if the new | ||||
5336 | // declaration has linkage). | ||||
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5337 | FilterLookupForScope( |
Richard Smith | a41c97a | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 5338 | Previous, OriginalDC, S, shouldConsiderLinkage(NewVD), |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5339 | IsExplicitSpecialization || IsVariableTemplateSpecialization); |
5340 | |||||
Richard Smith | dd9459f | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 5341 | // Check whether the previous declaration is in the same block scope. This |
5342 | // affects whether we merge types with it, per C++11 [dcl.array]p3. | ||||
5343 | if (getLangOpts().CPlusPlus && | ||||
5344 | NewVD->isLocalVarDecl() && NewVD->hasExternalStorage()) | ||||
5345 | NewVD->setPreviousDeclInSameBlockScope( | ||||
5346 | Previous.isSingleResult() && !Previous.isShadowed() && | ||||
Richard Smith | a41c97a | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 5347 | isDeclInScope(Previous.getFoundDecl(), OriginalDC, S, false)); |
Richard Smith | dd9459f | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 5348 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5349 | if (!getLangOpts().CPlusPlus) { |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 5350 | D.setRedeclaration(CheckVariableDeclaration(NewVD, Previous)); |
5351 | } else { | ||||
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 5352 | // Merge the decl with the existing one if appropriate. |
5353 | if (!Previous.empty()) { | ||||
5354 | if (Previous.isSingleResult() && | ||||
5355 | isa<FieldDecl>(Previous.getFoundDecl()) && | ||||
5356 | D.getCXXScopeSpec().isSet()) { | ||||
5357 | // The user tried to define a non-static data member | ||||
5358 | // out-of-line (C++ [dcl.meaning]p1). | ||||
5359 | Diag(NewVD->getLocation(), diag::err_nonstatic_member_out_of_line) | ||||
5360 | << D.getCXXScopeSpec().getRange(); | ||||
5361 | Previous.clear(); | ||||
5362 | NewVD->setInvalidDecl(); | ||||
5363 | } | ||||
5364 | } else if (D.getCXXScopeSpec().isSet()) { | ||||
5365 | // No previous declaration in the qualifying scope. | ||||
5366 | Diag(D.getIdentifierLoc(), diag::err_no_member) | ||||
5367 | << Name << computeDeclContext(D.getCXXScopeSpec(), true) | ||||
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 5368 | << D.getCXXScopeSpec().getRange(); |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 5369 | NewVD->setInvalidDecl(); |
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 5370 | } |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 5371 | |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5372 | if (!IsVariableTemplateSpecialization) { |
5373 | if (PrevVarTemplate) { | ||||
5374 | LookupResult PrevDecl(*this, GetNameForDeclarator(D), | ||||
5375 | LookupOrdinaryName, ForRedeclaration); | ||||
5376 | PrevDecl.addDecl(PrevVarTemplate->getTemplatedDecl()); | ||||
Larisse Voufo | 567f917 | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 5377 | D.setRedeclaration(CheckVariableDeclaration(NewVD, PrevDecl)); |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5378 | } else |
Larisse Voufo | 567f917 | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 5379 | D.setRedeclaration(CheckVariableDeclaration(NewVD, Previous)); |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5380 | } |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 5381 | |
5382 | // This is an explicit specialization of a static data member. Check it. | ||||
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5383 | if (IsExplicitSpecialization && !NewVD->isInvalidDecl() && |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 5384 | CheckMemberSpecialization(NewVD, Previous)) |
5385 | NewVD->setInvalidDecl(); | ||||
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 5386 | } |
Ryan Flynn | 478fbc6 | 2009-07-25 22:29:44 +0000 | [diff] [blame] | 5387 | |
Rafael Espindola | 65611bf | 2013-03-02 21:41:48 +0000 | [diff] [blame] | 5388 | ProcessPragmaWeak(S, NewVD); |
Rafael Espindola | 2a5bb50 | 2013-01-16 23:11:15 +0000 | [diff] [blame] | 5389 | checkAttributesAfterMerging(*this, *NewVD); |
5390 | |||||
Richard Smith | aa4bc18 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 5391 | // If this is the first declaration of an extern C variable, update |
5392 | // the map of such variables. | ||||
5393 | if (!NewVD->getPreviousDecl() && !NewVD->isInvalidDecl() && | ||||
5394 | isIncompleteDeclExternC(*this, NewVD)) | ||||
Richard Smith | 662f41b | 2013-06-18 20:15:12 +0000 | [diff] [blame] | 5395 | RegisterLocallyScopedExternCDecl(NewVD, S); |
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 5396 | |
Reid Kleckner | 942f9fe | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 5397 | if (getLangOpts().CPlusPlus && NewVD->isStaticLocal()) { |
Eli Friedman | 5e867c8 | 2013-07-10 00:30:46 +0000 | [diff] [blame] | 5398 | Decl *ManglingContextDecl; |
5399 | if (MangleNumberingContext *MCtx = | ||||
5400 | getCurrentMangleNumberContext(NewVD->getDeclContext(), | ||||
5401 | ManglingContextDecl)) { | ||||
5402 | Context.setManglingNumber(NewVD, MCtx->getManglingNumber(NewVD)); | ||||
5403 | } | ||||
5404 | } | ||||
5405 | |||||
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5406 | // If we are providing an explicit specialization of a static variable |
5407 | // template, make a note of that. | ||||
5408 | if (PrevVarTemplate && PrevVarTemplate->getInstantiatedFromMemberTemplate()) | ||||
Larisse Voufo | 04592e7 | 2013-08-22 00:28:27 +0000 | [diff] [blame] | 5409 | PrevVarTemplate->setMemberSpecialization(); |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5410 | |
Larisse Voufo | 567f917 | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 5411 | if (NewTemplate) { |
5412 | ActOnDocumentableDecl(NewTemplate); | ||||
5413 | return NewTemplate; | ||||
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5414 | } |
5415 | |||||
Larisse Voufo | 567f917 | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 5416 | return NewVD; |
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 5417 | } |
5418 | |||||
John McCall | 053f4bd | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 5419 | /// \brief Diagnose variable or built-in function shadowing. Implements |
5420 | /// -Wshadow. | ||||
John McCall | 8472af4 | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 5421 | /// |
John McCall | 053f4bd | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 5422 | /// This method is called whenever a VarDecl is added to a "useful" |
5423 | /// scope. | ||||
John McCall | 8472af4 | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 5424 | /// |
John McCall | a369a95 | 2010-03-20 04:12:52 +0000 | [diff] [blame] | 5425 | /// \param S the scope in which the shadowing name is being declared |
5426 | /// \param R the lookup of the name | ||||
John McCall | 8472af4 | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 5427 | /// |
John McCall | 053f4bd | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 5428 | void Sema::CheckShadow(Scope *S, VarDecl *D, const LookupResult& R) { |
John McCall | 8472af4 | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 5429 | // Return if warning is ignored. |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 5430 | if (Diags.getDiagnosticLevel(diag::warn_decl_shadow, R.getNameLoc()) == |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 5431 | DiagnosticsEngine::Ignored) |
John McCall | 8472af4 | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 5432 | return; |
5433 | |||||
Argyrios Kyrtzidis | 651f86f | 2011-02-08 18:21:25 +0000 | [diff] [blame] | 5434 | // Don't diagnose declarations at file scope. |
Argyrios Kyrtzidis | 865dd8c | 2011-04-25 21:39:50 +0000 | [diff] [blame] | 5435 | if (D->hasGlobalStorage()) |
John McCall | 8472af4 | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 5436 | return; |
Argyrios Kyrtzidis | 865dd8c | 2011-04-25 21:39:50 +0000 | [diff] [blame] | 5437 | |
5438 | DeclContext *NewDC = D->getDeclContext(); | ||||
5439 | |||||
John McCall | a369a95 | 2010-03-20 04:12:52 +0000 | [diff] [blame] | 5440 | // Only diagnose if we're shadowing an unambiguous field or variable. |
Douglas Gregor | c48c916 | 2010-03-17 16:03:44 +0000 | [diff] [blame] | 5441 | if (R.getResultKind() != LookupResult::Found) |
John McCall | 8472af4 | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 5442 | return; |
John McCall | 8472af4 | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 5443 | |
John McCall | 8472af4 | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 5444 | NamedDecl* ShadowedDecl = R.getFoundDecl(); |
5445 | if (!isa<VarDecl>(ShadowedDecl) && !isa<FieldDecl>(ShadowedDecl)) | ||||
5446 | return; | ||||
5447 | |||||
Argyrios Kyrtzidis | 36eb5e4 | 2011-01-31 07:04:54 +0000 | [diff] [blame] | 5448 | // Fields are not shadowed by variables in C++ static methods. |
5449 | if (isa<FieldDecl>(ShadowedDecl)) | ||||
5450 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(NewDC)) | ||||
5451 | if (MD->isStatic()) | ||||
5452 | return; | ||||
5453 | |||||
Argyrios Kyrtzidis | 49a6172 | 2011-01-31 07:04:50 +0000 | [diff] [blame] | 5454 | if (VarDecl *shadowedVar = dyn_cast<VarDecl>(ShadowedDecl)) |
5455 | if (shadowedVar->isExternC()) { | ||||
Argyrios Kyrtzidis | 49a6172 | 2011-01-31 07:04:50 +0000 | [diff] [blame] | 5456 | // For shadowing external vars, make sure that we point to the global |
5457 | // declaration, not a locally scoped extern declaration. | ||||
5458 | for (VarDecl::redecl_iterator | ||||
5459 | I = shadowedVar->redecls_begin(), E = shadowedVar->redecls_end(); | ||||
5460 | I != E; ++I) | ||||
5461 | if (I->isFileVarDecl()) { | ||||
5462 | ShadowedDecl = *I; | ||||
5463 | break; | ||||
5464 | } | ||||
5465 | } | ||||
5466 | |||||
5467 | DeclContext *OldDC = ShadowedDecl->getDeclContext(); | ||||
5468 | |||||
John McCall | a369a95 | 2010-03-20 04:12:52 +0000 | [diff] [blame] | 5469 | // Only warn about certain kinds of shadowing for class members. |
5470 | if (NewDC && NewDC->isRecord()) { | ||||
5471 | // In particular, don't warn about shadowing non-class members. | ||||
5472 | if (!OldDC->isRecord()) | ||||
5473 | return; | ||||
5474 | |||||
5475 | // TODO: should we warn about static data members shadowing | ||||
5476 | // static data members from base classes? | ||||
5477 | |||||
5478 | // TODO: don't diagnose for inaccessible shadowed members. | ||||
5479 | // This is hard to do perfectly because we might friend the | ||||
5480 | // shadowing context, but that's just a false negative. | ||||
5481 | } | ||||
5482 | |||||
5483 | // Determine what kind of declaration we're shadowing. | ||||
John McCall | 8472af4 | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 5484 | unsigned Kind; |
John McCall | a369a95 | 2010-03-20 04:12:52 +0000 | [diff] [blame] | 5485 | if (isa<RecordDecl>(OldDC)) { |
John McCall | 8472af4 | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 5486 | if (isa<FieldDecl>(ShadowedDecl)) |
5487 | Kind = 3; // field | ||||
5488 | else | ||||
5489 | Kind = 2; // static data member | ||||
John McCall | a369a95 | 2010-03-20 04:12:52 +0000 | [diff] [blame] | 5490 | } else if (OldDC->isFileContext()) |
John McCall | 8472af4 | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 5491 | Kind = 1; // global |
5492 | else | ||||
5493 | Kind = 0; // local | ||||
5494 | |||||
John McCall | a369a95 | 2010-03-20 04:12:52 +0000 | [diff] [blame] | 5495 | DeclarationName Name = R.getLookupName(); |
5496 | |||||
John McCall | 8472af4 | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 5497 | // Emit warning and note. |
John McCall | a369a95 | 2010-03-20 04:12:52 +0000 | [diff] [blame] | 5498 | Diag(R.getNameLoc(), diag::warn_decl_shadow) << Name << Kind << OldDC; |
John McCall | 8472af4 | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 5499 | Diag(ShadowedDecl->getLocation(), diag::note_previous_declaration); |
5500 | } | ||||
5501 | |||||
John McCall | 053f4bd | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 5502 | /// \brief Check -Wshadow without the advantage of a previous lookup. |
5503 | void Sema::CheckShadow(Scope *S, VarDecl *D) { | ||||
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 5504 | if (Diags.getDiagnosticLevel(diag::warn_decl_shadow, D->getLocation()) == |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 5505 | DiagnosticsEngine::Ignored) |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 5506 | return; |
5507 | |||||
John McCall | 053f4bd | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 5508 | LookupResult R(*this, D->getDeclName(), D->getLocation(), |
5509 | Sema::LookupOrdinaryName, Sema::ForRedeclaration); | ||||
5510 | LookupName(R, S); | ||||
5511 | CheckShadow(S, D, R); | ||||
5512 | } | ||||
5513 | |||||
Richard Smith | aa4bc18 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 5514 | /// Check for conflict between this global or extern "C" declaration and |
5515 | /// previous global or extern "C" declarations. This is only used in C++. | ||||
Rafael Espindola | 294ddc6 | 2013-01-11 19:34:23 +0000 | [diff] [blame] | 5516 | template<typename T> |
Richard Smith | aa4bc18 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 5517 | static bool checkGlobalOrExternCConflict( |
5518 | Sema &S, const T *ND, bool IsGlobal, LookupResult &Previous) { | ||||
5519 | assert(S.getLangOpts().CPlusPlus && "only C++ has extern \"C\""); | ||||
5520 | NamedDecl *Prev = S.findLocallyScopedExternCDecl(ND->getDeclName()); | ||||
Rafael Espindola | 2d1b096 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 5521 | |
Richard Smith | aa4bc18 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 5522 | if (!Prev && IsGlobal && !isIncompleteDeclExternC(S, ND)) { |
5523 | // The common case: this global doesn't conflict with any extern "C" | ||||
5524 | // declaration. | ||||
5525 | return false; | ||||
5526 | } | ||||
5527 | |||||
5528 | if (Prev) { | ||||
5529 | if (!IsGlobal || isIncompleteDeclExternC(S, ND)) { | ||||
5530 | // Both the old and new declarations have C language linkage. This is a | ||||
5531 | // redeclaration. | ||||
5532 | Previous.clear(); | ||||
5533 | Previous.addDecl(Prev); | ||||
5534 | return true; | ||||
5535 | } | ||||
5536 | |||||
5537 | // This is a global, non-extern "C" declaration, and there is a previous | ||||
5538 | // non-global extern "C" declaration. Diagnose if this is a variable | ||||
5539 | // declaration. | ||||
5540 | if (!isa<VarDecl>(ND)) | ||||
5541 | return false; | ||||
5542 | } else { | ||||
5543 | // The declaration is extern "C". Check for any declaration in the | ||||
5544 | // translation unit which might conflict. | ||||
5545 | if (IsGlobal) { | ||||
5546 | // We have already performed the lookup into the translation unit. | ||||
5547 | IsGlobal = false; | ||||
5548 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); | ||||
5549 | I != E; ++I) { | ||||
5550 | if (isa<VarDecl>(*I)) { | ||||
5551 | Prev = *I; | ||||
5552 | break; | ||||
5553 | } | ||||
5554 | } | ||||
5555 | } else { | ||||
5556 | DeclContext::lookup_result R = | ||||
5557 | S.Context.getTranslationUnitDecl()->lookup(ND->getDeclName()); | ||||
5558 | for (DeclContext::lookup_result::iterator I = R.begin(), E = R.end(); | ||||
5559 | I != E; ++I) { | ||||
5560 | if (isa<VarDecl>(*I)) { | ||||
5561 | Prev = *I; | ||||
5562 | break; | ||||
5563 | } | ||||
5564 | // FIXME: If we have any other entity with this name in global scope, | ||||
5565 | // the declaration is ill-formed, but that is a defect: it breaks the | ||||
5566 | // 'stat' hack, for instance. Only variables can have mangled name | ||||
5567 | // clashes with extern "C" declarations, so only they deserve a | ||||
5568 | // diagnostic. | ||||
5569 | } | ||||
5570 | } | ||||
5571 | |||||
5572 | if (!Prev) | ||||
Rafael Espindola | 2d1b096 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 5573 | return false; |
5574 | } | ||||
5575 | |||||
Richard Smith | aa4bc18 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 5576 | // Use the first declaration's location to ensure we point at something which |
5577 | // is lexically inside an extern "C" linkage-spec. | ||||
5578 | assert(Prev && "should have found a previous declaration to diagnose"); | ||||
5579 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(Prev)) | ||||
5580 | Prev = FD->getFirstDeclaration(); | ||||
5581 | else | ||||
5582 | Prev = cast<VarDecl>(Prev)->getFirstDeclaration(); | ||||
5583 | |||||
5584 | S.Diag(ND->getLocation(), diag::err_extern_c_global_conflict) | ||||
5585 | << IsGlobal << ND; | ||||
5586 | S.Diag(Prev->getLocation(), diag::note_extern_c_global_conflict) | ||||
5587 | << IsGlobal; | ||||
5588 | return false; | ||||
5589 | } | ||||
5590 | |||||
5591 | /// Apply special rules for handling extern "C" declarations. Returns \c true | ||||
5592 | /// if we have found that this is a redeclaration of some prior entity. | ||||
5593 | /// | ||||
5594 | /// Per C++ [dcl.link]p6: | ||||
5595 | /// Two declarations [for a function or variable] with C language linkage | ||||
5596 | /// with the same name that appear in different scopes refer to the same | ||||
5597 | /// [entity]. An entity with C language linkage shall not be declared with | ||||
5598 | /// the same name as an entity in global scope. | ||||
5599 | template<typename T> | ||||
5600 | static bool checkForConflictWithNonVisibleExternC(Sema &S, const T *ND, | ||||
5601 | LookupResult &Previous) { | ||||
5602 | if (!S.getLangOpts().CPlusPlus) { | ||||
5603 | // In C, when declaring a global variable, look for a corresponding 'extern' | ||||
Richard Smith | a41c97a | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 5604 | // variable declared in function scope. We don't need this in C++, because |
5605 | // we find local extern decls in the surrounding file-scope DeclContext. | ||||
Richard Smith | aa4bc18 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 5606 | if (ND->getDeclContext()->getRedeclContext()->isTranslationUnit()) { |
5607 | if (NamedDecl *Prev = S.findLocallyScopedExternCDecl(ND->getDeclName())) { | ||||
5608 | Previous.clear(); | ||||
5609 | Previous.addDecl(Prev); | ||||
5610 | return true; | ||||
5611 | } | ||||
5612 | } | ||||
5613 | return false; | ||||
5614 | } | ||||
5615 | |||||
5616 | // A declaration in the translation unit can conflict with an extern "C" | ||||
5617 | // declaration. | ||||
5618 | if (ND->getDeclContext()->getRedeclContext()->isTranslationUnit()) | ||||
5619 | return checkGlobalOrExternCConflict(S, ND, /*IsGlobal*/true, Previous); | ||||
5620 | |||||
5621 | // An extern "C" declaration can conflict with a declaration in the | ||||
5622 | // translation unit or can be a redeclaration of an extern "C" declaration | ||||
5623 | // in another scope. | ||||
5624 | if (isIncompleteDeclExternC(S,ND)) | ||||
5625 | return checkGlobalOrExternCConflict(S, ND, /*IsGlobal*/false, Previous); | ||||
5626 | |||||
5627 | // Neither global nor extern "C": nothing to do. | ||||
5628 | return false; | ||||
Rafael Espindola | 294ddc6 | 2013-01-11 19:34:23 +0000 | [diff] [blame] | 5629 | } |
5630 | |||||
Richard Smith | dc7a4f5 | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 5631 | void Sema::CheckVariableDeclarationType(VarDecl *NewVD) { |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 5632 | // If the decl is already known invalid, don't check it. |
5633 | if (NewVD->isInvalidDecl()) | ||||
Richard Smith | dc7a4f5 | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 5634 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5635 | |
Abramo Bagnara | 4c5750e | 2012-11-08 14:44:42 +0000 | [diff] [blame] | 5636 | TypeSourceInfo *TInfo = NewVD->getTypeSourceInfo(); |
5637 | QualType T = TInfo->getType(); | ||||
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 5638 | |
Richard Smith | dc7a4f5 | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 5639 | // Defer checking an 'auto' type until its initializer is attached. |
5640 | if (T->isUndeducedType()) | ||||
5641 | return; | ||||
5642 | |||||
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5643 | if (T->isObjCObjectType()) { |
Fariborz Jahanian | dcf1011 | 2011-07-25 21:12:27 +0000 | [diff] [blame] | 5644 | Diag(NewVD->getLocation(), diag::err_statically_allocated_object) |
5645 | << FixItHint::CreateInsertion(NewVD->getLocation(), "*"); | ||||
Fariborz Jahanian | 8eaefdc | 2011-07-26 17:58:54 +0000 | [diff] [blame] | 5646 | T = Context.getObjCObjectPointerType(T); |
5647 | NewVD->setType(T); | ||||
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 5648 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5649 | |
Zhongxing Xu | cb8f4f1 | 2009-01-16 02:36:34 +0000 | [diff] [blame] | 5650 | // Emit an error if an address space was applied to decl with local storage. |
5651 | // This includes arrays of objects with address space qualifiers, but not | ||||
5652 | // automatic variables that point to other address spaces. | ||||
5653 | // ISO/IEC TR 18037 S5.1.2 | ||||
Chris Lattner | 16c5dea | 2010-10-10 18:16:20 +0000 | [diff] [blame] | 5654 | if (NewVD->hasLocalStorage() && T.getAddressSpace() != 0) { |
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 5655 | Diag(NewVD->getLocation(), diag::err_as_qualified_auto_decl); |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 5656 | NewVD->setInvalidDecl(); |
Richard Smith | dc7a4f5 | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 5657 | return; |
Zhongxing Xu | cb8f4f1 | 2009-01-16 02:36:34 +0000 | [diff] [blame] | 5658 | } |
Fariborz Jahanian | 7b5b317 | 2009-02-21 19:44:02 +0000 | [diff] [blame] | 5659 | |
Tanya Lattner | 8aa86d1 | 2013-04-05 20:14:50 +0000 | [diff] [blame] | 5660 | // OpenCL v1.2 s6.5 - All program scope variables must be declared in the |
5661 | // __constant address space. | ||||
5662 | if (getLangOpts().OpenCL && NewVD->isFileVarDecl() | ||||
5663 | && T.getAddressSpace() != LangAS::opencl_constant | ||||
5664 | && !T->isSamplerT()){ | ||||
5665 | Diag(NewVD->getLocation(), diag::err_opencl_global_invalid_addr_space); | ||||
5666 | NewVD->setInvalidDecl(); | ||||
Richard Smith | dc7a4f5 | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 5667 | return; |
Tanya Lattner | 8aa86d1 | 2013-04-05 20:14:50 +0000 | [diff] [blame] | 5668 | } |
5669 | |||||
Tanya Lattner | 5e94d6f | 2012-06-19 23:09:52 +0000 | [diff] [blame] | 5670 | // OpenCL v1.2 s6.8 -- The static qualifier is valid only in program |
5671 | // scope. | ||||
5672 | if ((getLangOpts().OpenCLVersion >= 120) | ||||
5673 | && NewVD->isStaticLocal()) { | ||||
5674 | Diag(NewVD->getLocation(), diag::err_static_function_scope); | ||||
5675 | NewVD->setInvalidDecl(); | ||||
Richard Smith | dc7a4f5 | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 5676 | return; |
Tanya Lattner | 5e94d6f | 2012-06-19 23:09:52 +0000 | [diff] [blame] | 5677 | } |
5678 | |||||
Mike Stump | f33651c | 2009-04-14 00:57:29 +0000 | [diff] [blame] | 5679 | if (NewVD->hasLocalStorage() && T.isObjCGCWeak() |
Fariborz Jahanian | 175df89 | 2011-06-07 20:15:46 +0000 | [diff] [blame] | 5680 | && !NewVD->hasAttr<BlocksAttr>()) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5681 | if (getLangOpts().getGC() != LangOptions::NonGC) |
Fariborz Jahanian | 175df89 | 2011-06-07 20:15:46 +0000 | [diff] [blame] | 5682 | Diag(NewVD->getLocation(), diag::warn_gc_attribute_weak_on_local); |
Ted Kremenek | 3ba17ee | 2012-10-02 05:36:02 +0000 | [diff] [blame] | 5683 | else { |
5684 | assert(!getLangOpts().ObjCAutoRefCount); | ||||
Fariborz Jahanian | 175df89 | 2011-06-07 20:15:46 +0000 | [diff] [blame] | 5685 | Diag(NewVD->getLocation(), diag::warn_attribute_weak_on_local); |
Ted Kremenek | 3ba17ee | 2012-10-02 05:36:02 +0000 | [diff] [blame] | 5686 | } |
Fariborz Jahanian | 175df89 | 2011-06-07 20:15:46 +0000 | [diff] [blame] | 5687 | } |
Chris Lattner | 16c5dea | 2010-10-10 18:16:20 +0000 | [diff] [blame] | 5688 | |
Chris Lattner | 38c5ebd | 2009-04-19 05:21:20 +0000 | [diff] [blame] | 5689 | bool isVM = T->isVariablyModifiedType(); |
Chris Lattner | be6d259 | 2009-07-19 20:17:11 +0000 | [diff] [blame] | 5690 | if (isVM || NewVD->hasAttr<CleanupAttr>() || |
John McCall | e46f62c | 2010-08-01 01:24:59 +0000 | [diff] [blame] | 5691 | NewVD->hasAttr<BlocksAttr>()) |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 5692 | getCurFunction()->setHasBranchProtectedScope(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5693 | |
Chris Lattner | 38c5ebd | 2009-04-19 05:21:20 +0000 | [diff] [blame] | 5694 | if ((isVM && NewVD->hasLinkage()) || |
5695 | (T->isVariableArrayType() && NewVD->hasGlobalStorage())) { | ||||
Anders Carlsson | 1a7acfa | 2009-02-28 21:56:50 +0000 | [diff] [blame] | 5696 | bool SizeIsNegative; |
Douglas Gregor | 2767ce2 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 5697 | llvm::APSInt Oversized; |
Abramo Bagnara | 4c5750e | 2012-11-08 14:44:42 +0000 | [diff] [blame] | 5698 | TypeSourceInfo *FixedTInfo = |
5699 | TryToFixInvalidVariablyModifiedTypeSourceInfo(TInfo, Context, | ||||
5700 | SizeIsNegative, Oversized); | ||||
5701 | if (FixedTInfo == 0 && T->isVariableArrayType()) { | ||||
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 5702 | const VariableArrayType *VAT = Context.getAsVariableArrayType(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5703 | // FIXME: This won't give the correct result for |
5704 | // int a[10][n]; | ||||
Anders Carlsson | 1a7acfa | 2009-02-28 21:56:50 +0000 | [diff] [blame] | 5705 | SourceRange SizeRange = VAT->getSizeExpr()->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5706 | |
Anders Carlsson | 1a7acfa | 2009-02-28 21:56:50 +0000 | [diff] [blame] | 5707 | if (NewVD->isFileVarDecl()) |
5708 | Diag(NewVD->getLocation(), diag::err_vla_decl_in_file_scope) | ||||
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 5709 | << SizeRange; |
Enea Zaffanella | 9cbcab8 | 2013-05-10 20:34:44 +0000 | [diff] [blame] | 5710 | else if (NewVD->isStaticLocal()) |
Anders Carlsson | 1a7acfa | 2009-02-28 21:56:50 +0000 | [diff] [blame] | 5711 | Diag(NewVD->getLocation(), diag::err_vla_decl_has_static_storage) |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 5712 | << SizeRange; |
Anders Carlsson | 1a7acfa | 2009-02-28 21:56:50 +0000 | [diff] [blame] | 5713 | else |
5714 | Diag(NewVD->getLocation(), diag::err_vla_decl_has_extern_linkage) | ||||
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 5715 | << SizeRange; |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 5716 | NewVD->setInvalidDecl(); |
Richard Smith | dc7a4f5 | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 5717 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5718 | } |
5719 | |||||
Abramo Bagnara | 4c5750e | 2012-11-08 14:44:42 +0000 | [diff] [blame] | 5720 | if (FixedTInfo == 0) { |
Anders Carlsson | 1a7acfa | 2009-02-28 21:56:50 +0000 | [diff] [blame] | 5721 | if (NewVD->isFileVarDecl()) |
5722 | Diag(NewVD->getLocation(), diag::err_vm_decl_in_file_scope); | ||||
5723 | else | ||||
5724 | Diag(NewVD->getLocation(), diag::err_vm_decl_has_extern_linkage); | ||||
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 5725 | NewVD->setInvalidDecl(); |
Richard Smith | dc7a4f5 | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 5726 | return; |
Anders Carlsson | 1a7acfa | 2009-02-28 21:56:50 +0000 | [diff] [blame] | 5727 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5728 | |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 5729 | Diag(NewVD->getLocation(), diag::warn_illegal_constant_array_size); |
Abramo Bagnara | eae859a | 2012-11-08 16:01:51 +0000 | [diff] [blame] | 5730 | NewVD->setType(FixedTInfo->getType()); |
Abramo Bagnara | 4c5750e | 2012-11-08 14:44:42 +0000 | [diff] [blame] | 5731 | NewVD->setTypeSourceInfo(FixedTInfo); |
Anders Carlsson | 1a7acfa | 2009-02-28 21:56:50 +0000 | [diff] [blame] | 5732 | } |
5733 | |||||
David Majnemer | aa71567 | 2013-05-29 00:56:45 +0000 | [diff] [blame] | 5734 | if (T->isVoidType()) { |
5735 | // C++98 [dcl.stc]p5: The extern specifier can be applied only to the names | ||||
5736 | // of objects and functions. | ||||
5737 | if (NewVD->isThisDeclarationADefinition() || getLangOpts().CPlusPlus) { | ||||
5738 | Diag(NewVD->getLocation(), diag::err_typecheck_decl_incomplete_type) | ||||
5739 | << T; | ||||
5740 | NewVD->setInvalidDecl(); | ||||
5741 | return; | ||||
5742 | } | ||||
Richard Smith | dc7a4f5 | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 5743 | } |
5744 | |||||
5745 | if (!NewVD->hasLocalStorage() && NewVD->hasAttr<BlocksAttr>()) { | ||||
5746 | Diag(NewVD->getLocation(), diag::err_block_on_nonlocal); | ||||
5747 | NewVD->setInvalidDecl(); | ||||
5748 | return; | ||||
5749 | } | ||||
5750 | |||||
5751 | if (isVM && NewVD->hasAttr<BlocksAttr>()) { | ||||
5752 | Diag(NewVD->getLocation(), diag::err_block_on_vm); | ||||
5753 | NewVD->setInvalidDecl(); | ||||
5754 | return; | ||||
5755 | } | ||||
5756 | |||||
5757 | if (NewVD->isConstexpr() && !T->isDependentType() && | ||||
5758 | RequireLiteralType(NewVD->getLocation(), T, | ||||
5759 | diag::err_constexpr_var_non_literal)) { | ||||
5760 | // Can't perform this check until the type is deduced. | ||||
5761 | NewVD->setInvalidDecl(); | ||||
5762 | return; | ||||
5763 | } | ||||
5764 | } | ||||
5765 | |||||
5766 | /// \brief Perform semantic checking on a newly-created variable | ||||
5767 | /// declaration. | ||||
5768 | /// | ||||
5769 | /// This routine performs all of the type-checking required for a | ||||
5770 | /// variable declaration once it has been built. It is used both to | ||||
5771 | /// check variables after they have been parsed and their declarators | ||||
5772 | /// have been translated into a declaration, and to check variables | ||||
5773 | /// that have been instantiated from a template. | ||||
5774 | /// | ||||
5775 | /// Sets NewVD->isInvalidDecl() if an error was encountered. | ||||
5776 | /// | ||||
5777 | /// Returns true if the variable declaration is a redeclaration. | ||||
Larisse Voufo | 567f917 | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 5778 | bool Sema::CheckVariableDeclaration(VarDecl *NewVD, LookupResult &Previous) { |
Richard Smith | dc7a4f5 | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 5779 | CheckVariableDeclarationType(NewVD); |
5780 | |||||
5781 | // If the decl is already known invalid, don't check it. | ||||
5782 | if (NewVD->isInvalidDecl()) | ||||
5783 | return false; | ||||
5784 | |||||
John McCall | 5b8740f | 2013-04-01 18:34:28 +0000 | [diff] [blame] | 5785 | // If we did not find anything by this name, look for a non-visible |
5786 | // extern "C" declaration with the same name. | ||||
Richard Smith | dd9459f | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 5787 | if (Previous.empty() && |
5788 | checkForConflictWithNonVisibleExternC(*this, NewVD, Previous)) | ||||
Richard Smith | 99a7238 | 2013-09-03 21:00:58 +0000 | [diff] [blame] | 5789 | Previous.setShadowed(); |
Douglas Gregor | 6393519 | 2009-03-02 00:19:53 +0000 | [diff] [blame] | 5790 | |
Douglas Gregor | 7dc80e1 | 2013-01-09 00:47:56 +0000 | [diff] [blame] | 5791 | // Filter out any non-conflicting previous declarations. |
5792 | filterNonConflictingPreviousDecls(Context, NewVD, Previous); | ||||
5793 | |||||
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5794 | if (!Previous.empty()) { |
Richard Smith | 99a7238 | 2013-09-03 21:00:58 +0000 | [diff] [blame] | 5795 | MergeVarDecl(NewVD, Previous); |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 5796 | return true; |
Zhongxing Xu | cb8f4f1 | 2009-01-16 02:36:34 +0000 | [diff] [blame] | 5797 | } |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 5798 | return false; |
Zhongxing Xu | cb8f4f1 | 2009-01-16 02:36:34 +0000 | [diff] [blame] | 5799 | } |
5800 | |||||
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 5801 | /// \brief Data used with FindOverriddenMethod |
5802 | struct FindOverriddenMethodData { | ||||
5803 | Sema *S; | ||||
5804 | CXXMethodDecl *Method; | ||||
5805 | }; | ||||
5806 | |||||
5807 | /// \brief Member lookup function that determines whether a given C++ | ||||
5808 | /// method overrides a method in a base class, to be used with | ||||
5809 | /// CXXRecordDecl::lookupInBases(). | ||||
John McCall | af8e6ed | 2009-11-12 03:15:40 +0000 | [diff] [blame] | 5810 | static bool FindOverriddenMethod(const CXXBaseSpecifier *Specifier, |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 5811 | CXXBasePath &Path, |
5812 | void *UserData) { | ||||
5813 | RecordDecl *BaseRecord = Specifier->getType()->getAs<RecordType>()->getDecl(); | ||||
Anders Carlsson | 9549680 | 2009-11-26 20:50:40 +0000 | [diff] [blame] | 5814 | |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 5815 | FindOverriddenMethodData *Data |
5816 | = reinterpret_cast<FindOverriddenMethodData*>(UserData); | ||||
Anders Carlsson | 9549680 | 2009-11-26 20:50:40 +0000 | [diff] [blame] | 5817 | |
5818 | DeclarationName Name = Data->Method->getDeclName(); | ||||
5819 | |||||
5820 | // FIXME: Do we care about other names here too? | ||||
5821 | if (Name.getNameKind() == DeclarationName::CXXDestructorName) { | ||||
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 5822 | // We really want to find the base class destructor here. |
Anders Carlsson | 9549680 | 2009-11-26 20:50:40 +0000 | [diff] [blame] | 5823 | QualType T = Data->S->Context.getTypeDeclType(BaseRecord); |
5824 | CanQualType CT = Data->S->Context.getCanonicalType(T); | ||||
5825 | |||||
Anders Carlsson | 1a68972 | 2009-11-27 01:26:58 +0000 | [diff] [blame] | 5826 | Name = Data->S->Context.DeclarationNames.getCXXDestructorName(CT); |
Anders Carlsson | 9549680 | 2009-11-26 20:50:40 +0000 | [diff] [blame] | 5827 | } |
5828 | |||||
5829 | for (Path.Decls = BaseRecord->lookup(Name); | ||||
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 5830 | !Path.Decls.empty(); |
5831 | Path.Decls = Path.Decls.slice(1)) { | ||||
5832 | NamedDecl *D = Path.Decls.front(); | ||||
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 5833 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) { |
5834 | if (MD->isVirtual() && !Data->S->IsOverload(Data->Method, MD, false)) | ||||
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 5835 | return true; |
5836 | } | ||||
5837 | } | ||||
5838 | |||||
5839 | return false; | ||||
5840 | } | ||||
5841 | |||||
David Blaikie | 5708c18 | 2012-10-17 00:47:58 +0000 | [diff] [blame] | 5842 | namespace { |
5843 | enum OverrideErrorKind { OEK_All, OEK_NonDeleted, OEK_Deleted }; | ||||
5844 | } | ||||
5845 | /// \brief Report an error regarding overriding, along with any relevant | ||||
5846 | /// overriden methods. | ||||
5847 | /// | ||||
5848 | /// \param DiagID the primary error to report. | ||||
5849 | /// \param MD the overriding method. | ||||
5850 | /// \param OEK which overrides to include as notes. | ||||
5851 | static void ReportOverrides(Sema& S, unsigned DiagID, const CXXMethodDecl *MD, | ||||
5852 | OverrideErrorKind OEK = OEK_All) { | ||||
5853 | S.Diag(MD->getLocation(), DiagID) << MD->getDeclName(); | ||||
5854 | for (CXXMethodDecl::method_iterator I = MD->begin_overridden_methods(), | ||||
5855 | E = MD->end_overridden_methods(); | ||||
5856 | I != E; ++I) { | ||||
5857 | // This check (& the OEK parameter) could be replaced by a predicate, but | ||||
5858 | // without lambdas that would be overkill. This is still nicer than writing | ||||
5859 | // out the diag loop 3 times. | ||||
5860 | if ((OEK == OEK_All) || | ||||
5861 | (OEK == OEK_NonDeleted && !(*I)->isDeleted()) || | ||||
5862 | (OEK == OEK_Deleted && (*I)->isDeleted())) | ||||
5863 | S.Diag((*I)->getLocation(), diag::note_overridden_virtual_function); | ||||
5864 | } | ||||
5865 | } | ||||
5866 | |||||
Sebastian Redl | a165da0 | 2009-11-18 21:51:29 +0000 | [diff] [blame] | 5867 | /// AddOverriddenMethods - See if a method overrides any in the base classes, |
5868 | /// 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] | 5869 | bool Sema::AddOverriddenMethods(CXXRecordDecl *DC, CXXMethodDecl *MD) { |
Sebastian Redl | a165da0 | 2009-11-18 21:51:29 +0000 | [diff] [blame] | 5870 | // Look for virtual methods in base classes that this method might override. |
5871 | CXXBasePaths Paths; | ||||
5872 | FindOverriddenMethodData Data; | ||||
5873 | Data.Method = MD; | ||||
5874 | Data.S = this; | ||||
David Blaikie | 5708c18 | 2012-10-17 00:47:58 +0000 | [diff] [blame] | 5875 | bool hasDeletedOverridenMethods = false; |
5876 | bool hasNonDeletedOverridenMethods = false; | ||||
Douglas Gregor | a6c1e3a | 2010-10-13 22:55:32 +0000 | [diff] [blame] | 5877 | bool AddedAny = false; |
Sebastian Redl | a165da0 | 2009-11-18 21:51:29 +0000 | [diff] [blame] | 5878 | if (DC->lookupInBases(&FindOverriddenMethod, &Data, Paths)) { |
5879 | for (CXXBasePaths::decl_iterator I = Paths.found_decls_begin(), | ||||
5880 | E = Paths.found_decls_end(); I != E; ++I) { | ||||
5881 | if (CXXMethodDecl *OldMD = dyn_cast<CXXMethodDecl>(*I)) { | ||||
Richard Trieu | 304e233 | 2011-07-01 20:02:53 +0000 | [diff] [blame] | 5882 | MD->addOverriddenMethod(OldMD->getCanonicalDecl()); |
Sebastian Redl | a165da0 | 2009-11-18 21:51:29 +0000 | [diff] [blame] | 5883 | if (!CheckOverridingFunctionReturnType(MD, OldMD) && |
Aaron Ballman | fff3248 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 5884 | !CheckOverridingFunctionAttributes(MD, OldMD) && |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 5885 | !CheckOverridingFunctionExceptionSpec(MD, OldMD) && |
Anders Carlsson | 2e1c730 | 2011-01-20 16:25:36 +0000 | [diff] [blame] | 5886 | !CheckIfOverriddenFunctionIsMarkedFinal(MD, OldMD)) { |
David Blaikie | 5708c18 | 2012-10-17 00:47:58 +0000 | [diff] [blame] | 5887 | hasDeletedOverridenMethods |= OldMD->isDeleted(); |
5888 | hasNonDeletedOverridenMethods |= !OldMD->isDeleted(); | ||||
Douglas Gregor | a6c1e3a | 2010-10-13 22:55:32 +0000 | [diff] [blame] | 5889 | AddedAny = true; |
5890 | } | ||||
Sebastian Redl | a165da0 | 2009-11-18 21:51:29 +0000 | [diff] [blame] | 5891 | } |
5892 | } | ||||
5893 | } | ||||
David Blaikie | 5708c18 | 2012-10-17 00:47:58 +0000 | [diff] [blame] | 5894 | |
5895 | if (hasDeletedOverridenMethods && !MD->isDeleted()) { | ||||
5896 | ReportOverrides(*this, diag::err_non_deleted_override, MD, OEK_Deleted); | ||||
5897 | } | ||||
5898 | if (hasNonDeletedOverridenMethods && MD->isDeleted()) { | ||||
5899 | ReportOverrides(*this, diag::err_deleted_override, MD, OEK_NonDeleted); | ||||
5900 | } | ||||
5901 | |||||
Douglas Gregor | a6c1e3a | 2010-10-13 22:55:32 +0000 | [diff] [blame] | 5902 | return AddedAny; |
Sebastian Redl | a165da0 | 2009-11-18 21:51:29 +0000 | [diff] [blame] | 5903 | } |
5904 | |||||
Kaelyn Uhrain | 2afd766 | 2011-10-11 00:28:39 +0000 | [diff] [blame] | 5905 | namespace { |
5906 | // Struct for holding all of the extra arguments needed by | ||||
5907 | // DiagnoseInvalidRedeclaration to call Sema::ActOnFunctionDeclarator. | ||||
5908 | struct ActOnFDArgs { | ||||
5909 | Scope *S; | ||||
5910 | Declarator &D; | ||||
Kaelyn Uhrain | 2afd766 | 2011-10-11 00:28:39 +0000 | [diff] [blame] | 5911 | MultiTemplateParamsArg TemplateParamLists; |
Kaelyn Uhrain | 2afd766 | 2011-10-11 00:28:39 +0000 | [diff] [blame] | 5912 | bool AddToScope; |
5913 | }; | ||||
5914 | } | ||||
5915 | |||||
Kaelyn Uhrain | 43e875d | 2012-01-18 21:41:41 +0000 | [diff] [blame] | 5916 | namespace { |
5917 | |||||
5918 | // 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] | 5919 | // Also only accept corrections that have the same parent decl. |
Kaelyn Uhrain | 43e875d | 2012-01-18 21:41:41 +0000 | [diff] [blame] | 5920 | class DifferentNameValidatorCCC : public CorrectionCandidateCallback { |
5921 | public: | ||||
Kaelyn Uhrain | ef094a1 | 2012-06-07 23:57:08 +0000 | [diff] [blame] | 5922 | DifferentNameValidatorCCC(ASTContext &Context, FunctionDecl *TypoFD, |
5923 | CXXRecordDecl *Parent) | ||||
5924 | : Context(Context), OriginalFD(TypoFD), | ||||
5925 | ExpectedParent(Parent ? Parent->getCanonicalDecl() : 0) {} | ||||
Kaelyn Uhrain | 3336353 | 2012-02-16 22:40:59 +0000 | [diff] [blame] | 5926 | |
Kaelyn Uhrain | 43e875d | 2012-01-18 21:41:41 +0000 | [diff] [blame] | 5927 | virtual bool ValidateCandidate(const TypoCorrection &candidate) { |
Kaelyn Uhrain | 3336353 | 2012-02-16 22:40:59 +0000 | [diff] [blame] | 5928 | if (candidate.getEditDistance() == 0) |
5929 | return false; | ||||
5930 | |||||
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 5931 | SmallVector<unsigned, 1> MismatchedParams; |
Kaelyn Uhrain | ef094a1 | 2012-06-07 23:57:08 +0000 | [diff] [blame] | 5932 | for (TypoCorrection::const_decl_iterator CDecl = candidate.begin(), |
5933 | CDeclEnd = candidate.end(); | ||||
5934 | CDecl != CDeclEnd; ++CDecl) { | ||||
5935 | FunctionDecl *FD = dyn_cast<FunctionDecl>(*CDecl); | ||||
5936 | |||||
5937 | if (FD && !FD->hasBody() && | ||||
5938 | hasSimilarParameters(Context, FD, OriginalFD, MismatchedParams)) { | ||||
5939 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) { | ||||
5940 | CXXRecordDecl *Parent = MD->getParent(); | ||||
5941 | if (Parent && Parent->getCanonicalDecl() == ExpectedParent) | ||||
5942 | return true; | ||||
5943 | } else if (!ExpectedParent) { | ||||
5944 | return true; | ||||
5945 | } | ||||
5946 | } | ||||
Kaelyn Uhrain | 3336353 | 2012-02-16 22:40:59 +0000 | [diff] [blame] | 5947 | } |
5948 | |||||
Kaelyn Uhrain | ef094a1 | 2012-06-07 23:57:08 +0000 | [diff] [blame] | 5949 | return false; |
Kaelyn Uhrain | 43e875d | 2012-01-18 21:41:41 +0000 | [diff] [blame] | 5950 | } |
Kaelyn Uhrain | 3336353 | 2012-02-16 22:40:59 +0000 | [diff] [blame] | 5951 | |
5952 | private: | ||||
Kaelyn Uhrain | ef094a1 | 2012-06-07 23:57:08 +0000 | [diff] [blame] | 5953 | ASTContext &Context; |
5954 | FunctionDecl *OriginalFD; | ||||
Kaelyn Uhrain | 3336353 | 2012-02-16 22:40:59 +0000 | [diff] [blame] | 5955 | CXXRecordDecl *ExpectedParent; |
Kaelyn Uhrain | 43e875d | 2012-01-18 21:41:41 +0000 | [diff] [blame] | 5956 | }; |
5957 | |||||
5958 | } | ||||
5959 | |||||
Kaelyn Uhrain | 2afd766 | 2011-10-11 00:28:39 +0000 | [diff] [blame] | 5960 | /// \brief Generate diagnostics for an invalid function redeclaration. |
5961 | /// | ||||
5962 | /// This routine handles generating the diagnostic messages for an invalid | ||||
5963 | /// function redeclaration, including finding possible similar declarations | ||||
5964 | /// or performing typo correction if there are no previous declarations with | ||||
5965 | /// the same name. | ||||
5966 | /// | ||||
Sylvestre Ledru | f3477c1 | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 5967 | /// Returns a NamedDecl iff typo correction was performed and substituting in |
Kaelyn Uhrain | 2afd766 | 2011-10-11 00:28:39 +0000 | [diff] [blame] | 5968 | /// the new declaration name does not cause new errors. |
Richard Smith | 4e9686b | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 5969 | static NamedDecl *DiagnoseInvalidRedeclaration( |
Kaelyn Uhrain | f09ce39 | 2011-10-11 00:28:52 +0000 | [diff] [blame] | 5970 | Sema &SemaRef, LookupResult &Previous, FunctionDecl *NewFD, |
Richard Smith | 4e9686b | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 5971 | ActOnFDArgs &ExtraArgs, bool IsLocalFriend, Scope *S) { |
Kaelyn Uhrain | 5161163 | 2011-08-18 18:19:12 +0000 | [diff] [blame] | 5972 | DeclarationName Name = NewFD->getDeclName(); |
Kaelyn Uhrain | 2afd766 | 2011-10-11 00:28:39 +0000 | [diff] [blame] | 5973 | DeclContext *NewDC = NewFD->getDeclContext(); |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 5974 | SmallVector<unsigned, 1> MismatchedParams; |
5975 | SmallVector<std::pair<FunctionDecl *, unsigned>, 1> NearMatches; | ||||
Kaelyn Uhrain | 5161163 | 2011-08-18 18:19:12 +0000 | [diff] [blame] | 5976 | TypoCorrection Correction; |
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 5977 | bool IsDefinition = ExtraArgs.D.isFunctionDefinition(); |
Richard Smith | 4e9686b | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 5978 | unsigned DiagMsg = IsLocalFriend ? diag::err_no_matching_local_friend |
5979 | : diag::err_member_decl_does_not_match; | ||||
5980 | LookupResult Prev(SemaRef, Name, NewFD->getLocation(), | ||||
5981 | IsLocalFriend ? Sema::LookupLocalFriendName | ||||
5982 | : Sema::LookupOrdinaryName, | ||||
5983 | Sema::ForRedeclaration); | ||||
Kaelyn Uhrain | 5161163 | 2011-08-18 18:19:12 +0000 | [diff] [blame] | 5984 | |
5985 | NewFD->setInvalidDecl(); | ||||
Richard Smith | 4e9686b | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 5986 | if (IsLocalFriend) |
5987 | SemaRef.LookupName(Prev, S); | ||||
5988 | else | ||||
5989 | SemaRef.LookupQualifiedName(Prev, NewDC); | ||||
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 5990 | assert(!Prev.isAmbiguous() && |
5991 | "Cannot have an ambiguity in previous-declaration lookup"); | ||||
Kaelyn Uhrain | 3336353 | 2012-02-16 22:40:59 +0000 | [diff] [blame] | 5992 | CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(NewFD); |
Kaelyn Uhrain | ef094a1 | 2012-06-07 23:57:08 +0000 | [diff] [blame] | 5993 | DifferentNameValidatorCCC Validator(SemaRef.Context, NewFD, |
5994 | MD ? MD->getParent() : 0); | ||||
Kaelyn Uhrain | 5161163 | 2011-08-18 18:19:12 +0000 | [diff] [blame] | 5995 | if (!Prev.empty()) { |
5996 | for (LookupResult::iterator Func = Prev.begin(), FuncEnd = Prev.end(); | ||||
5997 | Func != FuncEnd; ++Func) { | ||||
5998 | FunctionDecl *FD = dyn_cast<FunctionDecl>(*Func); | ||||
Kaelyn Uhrain | f09ce39 | 2011-10-11 00:28:52 +0000 | [diff] [blame] | 5999 | if (FD && |
6000 | hasSimilarParameters(SemaRef.Context, FD, NewFD, MismatchedParams)) { | ||||
Kaelyn Uhrain | 5161163 | 2011-08-18 18:19:12 +0000 | [diff] [blame] | 6001 | // Add 1 to the index so that 0 can mean the mismatch didn't |
6002 | // involve a parameter | ||||
6003 | unsigned ParamNum = | ||||
6004 | MismatchedParams.empty() ? 0 : MismatchedParams.front() + 1; | ||||
6005 | NearMatches.push_back(std::make_pair(FD, ParamNum)); | ||||
6006 | } | ||||
Kaelyn Uhrain | 4d9d157 | 2011-08-04 17:40:00 +0000 | [diff] [blame] | 6007 | } |
Kaelyn Uhrain | 5161163 | 2011-08-18 18:19:12 +0000 | [diff] [blame] | 6008 | // If the qualified name lookup yielded nothing, try typo correction |
Richard Smith | 4e9686b | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 6009 | } else if ((Correction = SemaRef.CorrectTypo( |
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 6010 | Prev.getLookupNameInfo(), Prev.getLookupKind(), S, |
6011 | &ExtraArgs.D.getCXXScopeSpec(), Validator, | ||||
6012 | IsLocalFriend ? 0 : NewDC))) { | ||||
Kaelyn Uhrain | 2afd766 | 2011-10-11 00:28:39 +0000 | [diff] [blame] | 6013 | // Set up everything for the call to ActOnFunctionDeclarator |
6014 | ExtraArgs.D.SetIdentifier(Correction.getCorrectionAsIdentifierInfo(), | ||||
6015 | ExtraArgs.D.getIdentifierLoc()); | ||||
6016 | Previous.clear(); | ||||
6017 | Previous.setLookupName(Correction.getCorrection()); | ||||
Kaelyn Uhrain | 5161163 | 2011-08-18 18:19:12 +0000 | [diff] [blame] | 6018 | for (TypoCorrection::decl_iterator CDecl = Correction.begin(), |
6019 | CDeclEnd = Correction.end(); | ||||
6020 | CDecl != CDeclEnd; ++CDecl) { | ||||
6021 | FunctionDecl *FD = dyn_cast<FunctionDecl>(*CDecl); | ||||
Kaelyn Uhrain | ef094a1 | 2012-06-07 23:57:08 +0000 | [diff] [blame] | 6022 | if (FD && !FD->hasBody() && |
6023 | hasSimilarParameters(SemaRef.Context, FD, NewFD, MismatchedParams)) { | ||||
Kaelyn Uhrain | 2afd766 | 2011-10-11 00:28:39 +0000 | [diff] [blame] | 6024 | Previous.addDecl(FD); |
Kaelyn Uhrain | 5161163 | 2011-08-18 18:19:12 +0000 | [diff] [blame] | 6025 | } |
6026 | } | ||||
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 6027 | bool wasRedeclaration = ExtraArgs.D.isRedeclaration(); |
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 6028 | |
6029 | NamedDecl *Result; | ||||
6030 | // Retry building the function declaration with the new previous | ||||
6031 | // declarations, and with errors suppressed. | ||||
6032 | { | ||||
6033 | // Trap errors. | ||||
6034 | Sema::SFINAETrap Trap(SemaRef); | ||||
6035 | |||||
6036 | // TODO: Refactor ActOnFunctionDeclarator so that we can call only the | ||||
6037 | // pieces need to verify the typo-corrected C++ declaration and hopefully | ||||
6038 | // eliminate the need for the parameter pack ExtraArgs. | ||||
6039 | Result = SemaRef.ActOnFunctionDeclarator( | ||||
6040 | ExtraArgs.S, ExtraArgs.D, | ||||
6041 | Correction.getCorrectionDecl()->getDeclContext(), | ||||
6042 | NewFD->getTypeSourceInfo(), Previous, ExtraArgs.TemplateParamLists, | ||||
6043 | ExtraArgs.AddToScope); | ||||
6044 | |||||
6045 | if (Trap.hasErrorOccurred()) | ||||
6046 | Result = 0; | ||||
Kaelyn Uhrain | 2afd766 | 2011-10-11 00:28:39 +0000 | [diff] [blame] | 6047 | } |
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 6048 | |
6049 | if (Result) { | ||||
6050 | // Determine which correction we picked. | ||||
6051 | Decl *Canonical = Result->getCanonicalDecl(); | ||||
6052 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); | ||||
6053 | I != E; ++I) | ||||
6054 | if ((*I)->getCanonicalDecl() == Canonical) | ||||
6055 | Correction.setCorrectionDecl(*I); | ||||
6056 | |||||
6057 | SemaRef.diagnoseTypo( | ||||
6058 | Correction, | ||||
6059 | SemaRef.PDiag(IsLocalFriend | ||||
6060 | ? diag::err_no_matching_local_friend_suggest | ||||
6061 | : diag::err_member_decl_does_not_match_suggest) | ||||
6062 | << Name << NewDC << IsDefinition); | ||||
6063 | return Result; | ||||
Kaelyn Uhrain | 2afd766 | 2011-10-11 00:28:39 +0000 | [diff] [blame] | 6064 | } |
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 6065 | |
6066 | // Pretend the typo correction never occurred | ||||
6067 | ExtraArgs.D.SetIdentifier(Name.getAsIdentifierInfo(), | ||||
6068 | ExtraArgs.D.getIdentifierLoc()); | ||||
6069 | ExtraArgs.D.setRedeclaration(wasRedeclaration); | ||||
6070 | Previous.clear(); | ||||
6071 | Previous.setLookupName(Name); | ||||
Kaelyn Uhrain | 5161163 | 2011-08-18 18:19:12 +0000 | [diff] [blame] | 6072 | } |
6073 | |||||
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 6074 | SemaRef.Diag(NewFD->getLocation(), DiagMsg) |
6075 | << Name << NewDC << IsDefinition << NewFD->getLocation(); | ||||
Kaelyn Uhrain | 5161163 | 2011-08-18 18:19:12 +0000 | [diff] [blame] | 6076 | |
Kaelyn Uhrain | 1055393 | 2011-10-10 18:01:37 +0000 | [diff] [blame] | 6077 | bool NewFDisConst = false; |
6078 | if (CXXMethodDecl *NewMD = dyn_cast<CXXMethodDecl>(NewFD)) | ||||
David Blaikie | 4ef832f | 2012-08-10 00:55:35 +0000 | [diff] [blame] | 6079 | NewFDisConst = NewMD->isConst(); |
Kaelyn Uhrain | 1055393 | 2011-10-10 18:01:37 +0000 | [diff] [blame] | 6080 | |
Craig Topper | 8bc99dd | 2013-07-04 03:15:42 +0000 | [diff] [blame] | 6081 | for (SmallVectorImpl<std::pair<FunctionDecl *, unsigned> >::iterator |
Kaelyn Uhrain | 5161163 | 2011-08-18 18:19:12 +0000 | [diff] [blame] | 6082 | NearMatch = NearMatches.begin(), NearMatchEnd = NearMatches.end(); |
6083 | NearMatch != NearMatchEnd; ++NearMatch) { | ||||
6084 | FunctionDecl *FD = NearMatch->first; | ||||
Richard Smith | 4e9686b | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 6085 | CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD); |
6086 | bool FDisConst = MD && MD->isConst(); | ||||
6087 | bool IsMember = MD || !IsLocalFriend; | ||||
Kaelyn Uhrain | 5161163 | 2011-08-18 18:19:12 +0000 | [diff] [blame] | 6088 | |
Richard Smith | a41c97a | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 6089 | // FIXME: These notes are poorly worded for the local friend case. |
Kaelyn Uhrain | 5161163 | 2011-08-18 18:19:12 +0000 | [diff] [blame] | 6090 | if (unsigned Idx = NearMatch->second) { |
6091 | ParmVarDecl *FDParam = FD->getParamDecl(Idx-1); | ||||
Richard Smith | 1c931be | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 6092 | SourceLocation Loc = FDParam->getTypeSpecStartLoc(); |
6093 | if (Loc.isInvalid()) Loc = FD->getLocation(); | ||||
Richard Smith | 4e9686b | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 6094 | SemaRef.Diag(Loc, IsMember ? diag::note_member_def_close_param_match |
6095 | : diag::note_local_decl_close_param_match) | ||||
6096 | << Idx << FDParam->getType() | ||||
6097 | << NewFD->getParamDecl(Idx - 1)->getType(); | ||||
Kaelyn Uhrain | 1055393 | 2011-10-10 18:01:37 +0000 | [diff] [blame] | 6098 | } else if (FDisConst != NewFDisConst) { |
Kaelyn Uhrain | f09ce39 | 2011-10-11 00:28:52 +0000 | [diff] [blame] | 6099 | SemaRef.Diag(FD->getLocation(), diag::note_member_def_close_const_match) |
Kaelyn Uhrain | 1055393 | 2011-10-10 18:01:37 +0000 | [diff] [blame] | 6100 | << NewFDisConst << FD->getSourceRange().getEnd(); |
Kaelyn Uhrain | 5161163 | 2011-08-18 18:19:12 +0000 | [diff] [blame] | 6101 | } else |
Richard Smith | 4e9686b | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 6102 | SemaRef.Diag(FD->getLocation(), |
6103 | IsMember ? diag::note_member_def_close_match | ||||
6104 | : diag::note_local_decl_close_match); | ||||
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 6105 | } |
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 6106 | return 0; |
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 6107 | } |
6108 | |||||
David Blaikie | d662a79 | 2011-10-19 22:56:21 +0000 | [diff] [blame] | 6109 | static FunctionDecl::StorageClass getFunctionStorageClass(Sema &SemaRef, |
6110 | Declarator &D) { | ||||
Kaelyn Uhrain | d7e19ce | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6111 | switch (D.getDeclSpec().getStorageClassSpec()) { |
6112 | default: llvm_unreachable("Unknown storage class!"); | ||||
6113 | case DeclSpec::SCS_auto: | ||||
6114 | case DeclSpec::SCS_register: | ||||
6115 | case DeclSpec::SCS_mutable: | ||||
6116 | SemaRef.Diag(D.getDeclSpec().getStorageClassSpecLoc(), | ||||
6117 | diag::err_typecheck_sclass_func); | ||||
6118 | D.setInvalidType(); | ||||
6119 | break; | ||||
6120 | case DeclSpec::SCS_unspecified: break; | ||||
Rafael Espindola | 65dfa2b | 2013-04-25 12:11:36 +0000 | [diff] [blame] | 6121 | case DeclSpec::SCS_extern: |
6122 | if (D.getDeclSpec().isExternInLinkageSpec()) | ||||
6123 | return SC_None; | ||||
6124 | return SC_Extern; | ||||
Kaelyn Uhrain | d7e19ce | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6125 | case DeclSpec::SCS_static: { |
6126 | if (SemaRef.CurContext->getRedeclContext()->isFunctionOrMethod()) { | ||||
6127 | // C99 6.7.1p5: | ||||
6128 | // The declaration of an identifier for a function that has | ||||
6129 | // block scope shall have no explicit storage-class specifier | ||||
6130 | // other than extern | ||||
6131 | // See also (C++ [dcl.stc]p4). | ||||
6132 | SemaRef.Diag(D.getDeclSpec().getStorageClassSpecLoc(), | ||||
6133 | diag::err_static_block_func); | ||||
6134 | break; | ||||
6135 | } else | ||||
6136 | return SC_Static; | ||||
6137 | } | ||||
6138 | case DeclSpec::SCS_private_extern: return SC_PrivateExtern; | ||||
6139 | } | ||||
6140 | |||||
6141 | // No explicit storage class has already been returned | ||||
6142 | return SC_None; | ||||
6143 | } | ||||
6144 | |||||
6145 | static FunctionDecl* CreateNewFunctionDecl(Sema &SemaRef, Declarator &D, | ||||
6146 | DeclContext *DC, QualType &R, | ||||
6147 | TypeSourceInfo *TInfo, | ||||
6148 | FunctionDecl::StorageClass SC, | ||||
6149 | bool &IsVirtualOkay) { | ||||
6150 | DeclarationNameInfo NameInfo = SemaRef.GetNameForDeclarator(D); | ||||
6151 | DeclarationName Name = NameInfo.getName(); | ||||
6152 | |||||
6153 | FunctionDecl *NewFD = 0; | ||||
6154 | bool isInline = D.getDeclSpec().isInlineSpecified(); | ||||
Kaelyn Uhrain | d7e19ce | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6155 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6156 | if (!SemaRef.getLangOpts().CPlusPlus) { |
Kaelyn Uhrain | d7e19ce | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6157 | // Determine whether the function was written with a |
6158 | // prototype. This true when: | ||||
6159 | // - there is a prototype in the declarator, or | ||||
6160 | // - the type R of the function is some kind of typedef or other reference | ||||
6161 | // to a type name (which eventually refers to a function type). | ||||
6162 | bool HasPrototype = | ||||
6163 | (D.isFunctionDeclarator() && D.getFunctionTypeInfo().hasPrototype) || | ||||
6164 | (!isa<FunctionType>(R.getTypePtr()) && R->isFunctionProtoType()); | ||||
6165 | |||||
David Blaikie | d662a79 | 2011-10-19 22:56:21 +0000 | [diff] [blame] | 6166 | NewFD = FunctionDecl::Create(SemaRef.Context, DC, |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 6167 | D.getLocStart(), NameInfo, R, |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 6168 | TInfo, SC, isInline, |
6169 | HasPrototype, false); | ||||
Kaelyn Uhrain | d7e19ce | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6170 | if (D.isInvalidType()) |
6171 | NewFD->setInvalidDecl(); | ||||
6172 | |||||
6173 | // Set the lexical context. | ||||
6174 | NewFD->setLexicalDeclContext(SemaRef.CurContext); | ||||
6175 | |||||
6176 | return NewFD; | ||||
6177 | } | ||||
6178 | |||||
6179 | bool isExplicit = D.getDeclSpec().isExplicitSpecified(); | ||||
6180 | bool isConstexpr = D.getDeclSpec().isConstexprSpecified(); | ||||
6181 | |||||
6182 | // Check that the return type is not an abstract class type. | ||||
6183 | // For record types, this is done by the AbstractClassUsageDiagnoser once | ||||
6184 | // the class has been completely parsed. | ||||
6185 | if (!DC->isRecord() && | ||||
6186 | SemaRef.RequireNonAbstractType(D.getIdentifierLoc(), | ||||
6187 | R->getAs<FunctionType>()->getResultType(), | ||||
6188 | diag::err_abstract_type_in_decl, | ||||
6189 | SemaRef.AbstractReturnType)) | ||||
6190 | D.setInvalidType(); | ||||
6191 | |||||
6192 | if (Name.getNameKind() == DeclarationName::CXXConstructorName) { | ||||
6193 | // This is a C++ constructor declaration. | ||||
6194 | assert(DC->isRecord() && | ||||
6195 | "Constructors can only be declared in a member context"); | ||||
6196 | |||||
6197 | R = SemaRef.CheckConstructorDeclarator(D, R, SC); | ||||
6198 | return CXXConstructorDecl::Create(SemaRef.Context, cast<CXXRecordDecl>(DC), | ||||
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 6199 | D.getLocStart(), NameInfo, |
Kaelyn Uhrain | d7e19ce | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6200 | R, TInfo, isExplicit, isInline, |
6201 | /*isImplicitlyDeclared=*/false, | ||||
6202 | isConstexpr); | ||||
6203 | |||||
6204 | } else if (Name.getNameKind() == DeclarationName::CXXDestructorName) { | ||||
6205 | // This is a C++ destructor declaration. | ||||
6206 | if (DC->isRecord()) { | ||||
6207 | R = SemaRef.CheckDestructorDeclarator(D, R, SC); | ||||
6208 | CXXRecordDecl *Record = cast<CXXRecordDecl>(DC); | ||||
6209 | CXXDestructorDecl *NewDD = CXXDestructorDecl::Create( | ||||
6210 | SemaRef.Context, Record, | ||||
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 6211 | D.getLocStart(), |
Kaelyn Uhrain | d7e19ce | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6212 | NameInfo, R, TInfo, isInline, |
6213 | /*isImplicitlyDeclared=*/false); | ||||
6214 | |||||
6215 | // If the class is complete, then we now create the implicit exception | ||||
6216 | // specification. If the class is incomplete or dependent, we can't do | ||||
6217 | // it yet. | ||||
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6218 | if (SemaRef.getLangOpts().CPlusPlus11 && !Record->isDependentType() && |
Kaelyn Uhrain | d7e19ce | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6219 | Record->getDefinition() && !Record->isBeingDefined() && |
6220 | R->getAs<FunctionProtoType>()->getExceptionSpecType() == EST_None) { | ||||
6221 | SemaRef.AdjustDestructorExceptionSpec(Record, NewDD); | ||||
6222 | } | ||||
6223 | |||||
Peter Collingbourne | f51cfb8 | 2013-05-20 14:12:25 +0000 | [diff] [blame] | 6224 | // The Microsoft ABI requires that we perform the destructor body |
6225 | // checks (i.e. operator delete() lookup) at every declaration, as | ||||
6226 | // any translation unit may need to emit a deleting destructor. | ||||
6227 | if (SemaRef.Context.getTargetInfo().getCXXABI().isMicrosoft() && | ||||
6228 | !Record->isDependentType() && Record->getDefinition() && | ||||
6229 | !Record->isBeingDefined()) { | ||||
6230 | SemaRef.CheckDestructor(NewDD); | ||||
6231 | } | ||||
6232 | |||||
Kaelyn Uhrain | d7e19ce | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6233 | IsVirtualOkay = true; |
6234 | return NewDD; | ||||
6235 | |||||
6236 | } else { | ||||
6237 | SemaRef.Diag(D.getIdentifierLoc(), diag::err_destructor_not_member); | ||||
6238 | D.setInvalidType(); | ||||
6239 | |||||
6240 | // Create a FunctionDecl to satisfy the function definition parsing | ||||
6241 | // code path. | ||||
6242 | return FunctionDecl::Create(SemaRef.Context, DC, | ||||
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 6243 | D.getLocStart(), |
Kaelyn Uhrain | d7e19ce | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6244 | D.getIdentifierLoc(), Name, R, TInfo, |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 6245 | SC, isInline, |
Kaelyn Uhrain | d7e19ce | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6246 | /*hasPrototype=*/true, isConstexpr); |
6247 | } | ||||
6248 | |||||
6249 | } else if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName) { | ||||
6250 | if (!DC->isRecord()) { | ||||
6251 | SemaRef.Diag(D.getIdentifierLoc(), | ||||
6252 | diag::err_conv_function_not_member); | ||||
6253 | return 0; | ||||
6254 | } | ||||
6255 | |||||
6256 | SemaRef.CheckConversionDeclarator(D, R, SC); | ||||
6257 | IsVirtualOkay = true; | ||||
6258 | return CXXConversionDecl::Create(SemaRef.Context, cast<CXXRecordDecl>(DC), | ||||
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 6259 | D.getLocStart(), NameInfo, |
Kaelyn Uhrain | d7e19ce | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6260 | R, TInfo, isInline, isExplicit, |
6261 | isConstexpr, SourceLocation()); | ||||
6262 | |||||
6263 | } else if (DC->isRecord()) { | ||||
6264 | // If the name of the function is the same as the name of the record, | ||||
6265 | // then this must be an invalid constructor that has a return type. | ||||
6266 | // (The parser checks for a return type and makes the declarator a | ||||
6267 | // constructor if it has no return type). | ||||
6268 | if (Name.getAsIdentifierInfo() && | ||||
6269 | Name.getAsIdentifierInfo() == cast<CXXRecordDecl>(DC)->getIdentifier()){ | ||||
6270 | SemaRef.Diag(D.getIdentifierLoc(), diag::err_constructor_return_type) | ||||
6271 | << SourceRange(D.getDeclSpec().getTypeSpecTypeLoc()) | ||||
6272 | << SourceRange(D.getIdentifierLoc()); | ||||
6273 | return 0; | ||||
6274 | } | ||||
6275 | |||||
Kaelyn Uhrain | d7e19ce | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6276 | // This is a C++ method declaration. |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 6277 | CXXMethodDecl *Ret = CXXMethodDecl::Create(SemaRef.Context, |
6278 | cast<CXXRecordDecl>(DC), | ||||
6279 | D.getLocStart(), NameInfo, R, | ||||
6280 | TInfo, SC, isInline, | ||||
6281 | isConstexpr, SourceLocation()); | ||||
6282 | IsVirtualOkay = !Ret->isStatic(); | ||||
6283 | return Ret; | ||||
Kaelyn Uhrain | d7e19ce | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6284 | } else { |
6285 | // Determine whether the function was written with a | ||||
6286 | // prototype. This true when: | ||||
6287 | // - we're in C++ (where every function has a prototype), | ||||
6288 | return FunctionDecl::Create(SemaRef.Context, DC, | ||||
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 6289 | D.getLocStart(), |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 6290 | NameInfo, R, TInfo, SC, isInline, |
Kaelyn Uhrain | d7e19ce | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6291 | true/*HasPrototype*/, isConstexpr); |
6292 | } | ||||
6293 | } | ||||
6294 | |||||
Eli Friedman | 7c3c6bc | 2012-09-20 01:40:23 +0000 | [diff] [blame] | 6295 | void Sema::checkVoidParamDecl(ParmVarDecl *Param) { |
6296 | // In C++, the empty parameter-type-list must be spelled "void"; a | ||||
6297 | // typedef of void is not permitted. | ||||
6298 | if (getLangOpts().CPlusPlus && | ||||
6299 | Param->getType().getUnqualifiedType() != Context.VoidTy) { | ||||
6300 | bool IsTypeAlias = false; | ||||
6301 | if (const TypedefType *TT = Param->getType()->getAs<TypedefType>()) | ||||
6302 | IsTypeAlias = isa<TypeAliasDecl>(TT->getDecl()); | ||||
6303 | else if (const TemplateSpecializationType *TST = | ||||
6304 | Param->getType()->getAs<TemplateSpecializationType>()) | ||||
6305 | IsTypeAlias = TST->isTypeAlias(); | ||||
6306 | Diag(Param->getLocation(), diag::err_param_typedef_of_void) | ||||
6307 | << IsTypeAlias; | ||||
6308 | } | ||||
6309 | } | ||||
6310 | |||||
Matt Arsenault | e6c8afc | 2013-07-23 01:23:36 +0000 | [diff] [blame] | 6311 | enum OpenCLParamType { |
6312 | ValidKernelParam, | ||||
6313 | PtrPtrKernelParam, | ||||
6314 | PtrKernelParam, | ||||
6315 | InvalidKernelParam, | ||||
6316 | RecordKernelParam | ||||
6317 | }; | ||||
6318 | |||||
6319 | static OpenCLParamType getOpenCLKernelParameterType(QualType PT) { | ||||
6320 | if (PT->isPointerType()) { | ||||
6321 | QualType PointeeType = PT->getPointeeType(); | ||||
6322 | return PointeeType->isPointerType() ? PtrPtrKernelParam : PtrKernelParam; | ||||
6323 | } | ||||
6324 | |||||
6325 | // TODO: Forbid the other integer types (size_t, ptrdiff_t...) when they can | ||||
6326 | // be used as builtin types. | ||||
6327 | |||||
6328 | if (PT->isImageType()) | ||||
6329 | return PtrKernelParam; | ||||
6330 | |||||
6331 | if (PT->isBooleanType()) | ||||
6332 | return InvalidKernelParam; | ||||
6333 | |||||
6334 | if (PT->isEventT()) | ||||
6335 | return InvalidKernelParam; | ||||
6336 | |||||
6337 | if (PT->isHalfType()) | ||||
6338 | return InvalidKernelParam; | ||||
6339 | |||||
6340 | if (PT->isRecordType()) | ||||
6341 | return RecordKernelParam; | ||||
6342 | |||||
6343 | return ValidKernelParam; | ||||
6344 | } | ||||
6345 | |||||
6346 | static void checkIsValidOpenCLKernelParameter( | ||||
6347 | Sema &S, | ||||
6348 | Declarator &D, | ||||
6349 | ParmVarDecl *Param, | ||||
6350 | llvm::SmallPtrSet<const Type *, 16> &ValidTypes) { | ||||
6351 | QualType PT = Param->getType(); | ||||
6352 | |||||
6353 | // Cache the valid types we encounter to avoid rechecking structs that are | ||||
6354 | // used again | ||||
6355 | if (ValidTypes.count(PT.getTypePtr())) | ||||
6356 | return; | ||||
6357 | |||||
6358 | switch (getOpenCLKernelParameterType(PT)) { | ||||
6359 | case PtrPtrKernelParam: | ||||
6360 | // OpenCL v1.2 s6.9.a: | ||||
6361 | // A kernel function argument cannot be declared as a | ||||
6362 | // pointer to a pointer type. | ||||
6363 | S.Diag(Param->getLocation(), diag::err_opencl_ptrptr_kernel_param); | ||||
6364 | D.setInvalidType(); | ||||
6365 | return; | ||||
6366 | |||||
6367 | // OpenCL v1.2 s6.9.k: | ||||
6368 | // Arguments to kernel functions in a program cannot be declared with the | ||||
6369 | // built-in scalar types bool, half, size_t, ptrdiff_t, intptr_t, and | ||||
6370 | // uintptr_t or a struct and/or union that contain fields declared to be | ||||
6371 | // one of these built-in scalar types. | ||||
6372 | |||||
6373 | case InvalidKernelParam: | ||||
6374 | // OpenCL v1.2 s6.8 n: | ||||
6375 | // A kernel function argument cannot be declared | ||||
6376 | // of event_t type. | ||||
6377 | S.Diag(Param->getLocation(), diag::err_bad_kernel_param_type) << PT; | ||||
6378 | D.setInvalidType(); | ||||
6379 | return; | ||||
6380 | |||||
6381 | case PtrKernelParam: | ||||
6382 | case ValidKernelParam: | ||||
6383 | ValidTypes.insert(PT.getTypePtr()); | ||||
6384 | return; | ||||
6385 | |||||
6386 | case RecordKernelParam: | ||||
6387 | break; | ||||
6388 | } | ||||
6389 | |||||
6390 | // Track nested structs we will inspect | ||||
6391 | SmallVector<const Decl *, 4> VisitStack; | ||||
6392 | |||||
6393 | // Track where we are in the nested structs. Items will migrate from | ||||
6394 | // VisitStack to HistoryStack as we do the DFS for bad field. | ||||
6395 | SmallVector<const FieldDecl *, 4> HistoryStack; | ||||
6396 | HistoryStack.push_back((const FieldDecl *) 0); | ||||
6397 | |||||
6398 | const RecordDecl *PD = PT->castAs<RecordType>()->getDecl(); | ||||
6399 | VisitStack.push_back(PD); | ||||
6400 | |||||
6401 | assert(VisitStack.back() && "First decl null?"); | ||||
6402 | |||||
6403 | do { | ||||
6404 | const Decl *Next = VisitStack.pop_back_val(); | ||||
6405 | if (!Next) { | ||||
6406 | assert(!HistoryStack.empty()); | ||||
6407 | // Found a marker, we have gone up a level | ||||
6408 | if (const FieldDecl *Hist = HistoryStack.pop_back_val()) | ||||
6409 | ValidTypes.insert(Hist->getType().getTypePtr()); | ||||
6410 | |||||
6411 | continue; | ||||
6412 | } | ||||
6413 | |||||
6414 | // Adds everything except the original parameter declaration (which is not a | ||||
6415 | // field itself) to the history stack. | ||||
6416 | const RecordDecl *RD; | ||||
6417 | if (const FieldDecl *Field = dyn_cast<FieldDecl>(Next)) { | ||||
6418 | HistoryStack.push_back(Field); | ||||
6419 | RD = Field->getType()->castAs<RecordType>()->getDecl(); | ||||
6420 | } else { | ||||
6421 | RD = cast<RecordDecl>(Next); | ||||
6422 | } | ||||
6423 | |||||
6424 | // Add a null marker so we know when we've gone back up a level | ||||
6425 | VisitStack.push_back((const Decl *) 0); | ||||
6426 | |||||
6427 | for (RecordDecl::field_iterator I = RD->field_begin(), | ||||
6428 | E = RD->field_end(); I != E; ++I) { | ||||
6429 | const FieldDecl *FD = *I; | ||||
6430 | QualType QT = FD->getType(); | ||||
6431 | |||||
6432 | if (ValidTypes.count(QT.getTypePtr())) | ||||
6433 | continue; | ||||
6434 | |||||
6435 | OpenCLParamType ParamType = getOpenCLKernelParameterType(QT); | ||||
6436 | if (ParamType == ValidKernelParam) | ||||
6437 | continue; | ||||
6438 | |||||
6439 | if (ParamType == RecordKernelParam) { | ||||
6440 | VisitStack.push_back(FD); | ||||
6441 | continue; | ||||
6442 | } | ||||
6443 | |||||
6444 | // OpenCL v1.2 s6.9.p: | ||||
6445 | // Arguments to kernel functions that are declared to be a struct or union | ||||
6446 | // do not allow OpenCL objects to be passed as elements of the struct or | ||||
6447 | // union. | ||||
6448 | if (ParamType == PtrKernelParam || ParamType == PtrPtrKernelParam) { | ||||
6449 | S.Diag(Param->getLocation(), | ||||
6450 | diag::err_record_with_pointers_kernel_param) | ||||
6451 | << PT->isUnionType() | ||||
6452 | << PT; | ||||
6453 | } else { | ||||
6454 | S.Diag(Param->getLocation(), diag::err_bad_kernel_param_type) << PT; | ||||
6455 | } | ||||
6456 | |||||
6457 | S.Diag(PD->getLocation(), diag::note_within_field_of_type) | ||||
6458 | << PD->getDeclName(); | ||||
6459 | |||||
6460 | // We have an error, now let's go back up through history and show where | ||||
6461 | // the offending field came from | ||||
6462 | for (ArrayRef<const FieldDecl *>::const_iterator I = HistoryStack.begin() + 1, | ||||
6463 | E = HistoryStack.end(); I != E; ++I) { | ||||
6464 | const FieldDecl *OuterField = *I; | ||||
6465 | S.Diag(OuterField->getLocation(), diag::note_within_field_of_type) | ||||
6466 | << OuterField->getType(); | ||||
6467 | } | ||||
6468 | |||||
6469 | S.Diag(FD->getLocation(), diag::note_illegal_field_declared_here) | ||||
6470 | << QT->isPointerType() | ||||
6471 | << QT; | ||||
6472 | D.setInvalidType(); | ||||
6473 | return; | ||||
6474 | } | ||||
6475 | } while (!VisitStack.empty()); | ||||
6476 | } | ||||
6477 | |||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6478 | NamedDecl* |
Nick Lewycky | 25af091 | 2011-07-02 02:05:12 +0000 | [diff] [blame] | 6479 | Sema::ActOnFunctionDeclarator(Scope *S, Declarator &D, DeclContext *DC, |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 6480 | TypeSourceInfo *TInfo, LookupResult &Previous, |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 6481 | MultiTemplateParamsArg TemplateParamLists, |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 6482 | bool &AddToScope) { |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 6483 | QualType R = TInfo->getType(); |
6484 | |||||
Zhongxing Xu | 416fcaf | 2009-01-16 01:13:29 +0000 | [diff] [blame] | 6485 | assert(R.getTypePtr()->isFunctionType()); |
6486 | |||||
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 6487 | // TODO: consider using NameInfo for diagnostic. |
6488 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); | ||||
6489 | DeclarationName Name = NameInfo.getName(); | ||||
Kaelyn Uhrain | d7e19ce | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6490 | FunctionDecl::StorageClass SC = getFunctionStorageClass(*this, D); |
Zhongxing Xu | 416fcaf | 2009-01-16 01:13:29 +0000 | [diff] [blame] | 6491 | |
Richard Smith | ec64244 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 6492 | if (DeclSpec::TSCS TSCS = D.getDeclSpec().getThreadStorageClassSpec()) |
6493 | Diag(D.getDeclSpec().getThreadStorageClassSpecLoc(), | ||||
6494 | diag::err_invalid_thread) | ||||
6495 | << DeclSpec::getSpecifierName(TSCS); | ||||
Eli Friedman | 63054b3 | 2009-04-19 20:27:55 +0000 | [diff] [blame] | 6496 | |
Reid Kleckner | d1a32c3 | 2013-10-08 00:58:57 +0000 | [diff] [blame] | 6497 | if (D.isFirstDeclarationOfMember()) |
6498 | adjustMemberFunctionCC(R, D.isStaticMember()); | ||||
Reid Kleckner | ef07203 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 6499 | |
Douglas Gregor | 3922ed0 | 2010-12-10 19:28:19 +0000 | [diff] [blame] | 6500 | bool isFriend = false; |
Douglas Gregor | 3922ed0 | 2010-12-10 19:28:19 +0000 | [diff] [blame] | 6501 | FunctionTemplateDecl *FunctionTemplate = 0; |
6502 | bool isExplicitSpecialization = false; | ||||
6503 | bool isFunctionTemplateSpecialization = false; | ||||
Nico Weber | 6b02009 | 2012-06-25 17:21:05 +0000 | [diff] [blame] | 6504 | |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 6505 | bool isDependentClassScopeExplicitSpecialization = false; |
Nico Weber | 6b02009 | 2012-06-25 17:21:05 +0000 | [diff] [blame] | 6506 | bool HasExplicitTemplateArgs = false; |
6507 | TemplateArgumentListInfo TemplateArgs; | ||||
6508 | |||||
Kaelyn Uhrain | d7e19ce | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6509 | bool isVirtualOkay = false; |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6510 | |
Richard Smith | a41c97a | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 6511 | DeclContext *OriginalDC = DC; |
6512 | bool IsLocalExternDecl = adjustContextForLocalExternDecl(DC); | ||||
6513 | |||||
Kaelyn Uhrain | d7e19ce | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6514 | FunctionDecl *NewFD = CreateNewFunctionDecl(*this, D, DC, R, TInfo, SC, |
6515 | isVirtualOkay); | ||||
6516 | if (!NewFD) return 0; | ||||
6517 | |||||
Argyrios Kyrtzidis | c14a03d | 2011-11-23 20:27:36 +0000 | [diff] [blame] | 6518 | if (OriginalLexicalContext && OriginalLexicalContext->isObjCContainer()) |
6519 | NewFD->setTopLevelDeclInObjCContainer(); | ||||
6520 | |||||
Richard Smith | a41c97a | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 6521 | // Set the lexical context. If this is a function-scope declaration, or has a |
6522 | // C++ scope specifier, or is the object of a friend declaration, the lexical | ||||
6523 | // context will be different from the semantic context. | ||||
6524 | NewFD->setLexicalDeclContext(CurContext); | ||||
6525 | |||||
6526 | if (IsLocalExternDecl) | ||||
6527 | NewFD->setLocalExternDecl(); | ||||
6528 | |||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6529 | if (getLangOpts().CPlusPlus) { |
Kaelyn Uhrain | d7e19ce | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6530 | bool isInline = D.getDeclSpec().isInlineSpecified(); |
Douglas Gregor | 3922ed0 | 2010-12-10 19:28:19 +0000 | [diff] [blame] | 6531 | bool isVirtual = D.getDeclSpec().isVirtualSpecified(); |
6532 | bool isExplicit = D.getDeclSpec().isExplicitSpecified(); | ||||
Richard Smith | af1fc7a | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 6533 | bool isConstexpr = D.getDeclSpec().isConstexprSpecified(); |
Kaelyn Uhrain | d7e19ce | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6534 | isFriend = D.getDeclSpec().isFriendSpecified(); |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 6535 | if (isFriend && !isInline && D.isFunctionDefinition()) { |
Abramo Bagnara | b0a2fcc | 2011-03-18 15:21:59 +0000 | [diff] [blame] | 6536 | // C++ [class.friend]p5 |
6537 | // A function can be defined in a friend declaration of a | ||||
6538 | // class . . . . Such a function is implicitly inline. | ||||
6539 | NewFD->setImplicitlyInline(); | ||||
6540 | } | ||||
6541 | |||||
John McCall | e402e72 | 2012-09-25 07:32:39 +0000 | [diff] [blame] | 6542 | // If this is a method defined in an __interface, and is not a constructor |
6543 | // or an overloaded operator, then set the pure flag (isVirtual will already | ||||
6544 | // return true). | ||||
6545 | if (const CXXRecordDecl *Parent = | ||||
6546 | dyn_cast<CXXRecordDecl>(NewFD->getDeclContext())) { | ||||
6547 | if (Parent->isInterface() && cast<CXXMethodDecl>(NewFD)->isUserProvided()) | ||||
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 6548 | NewFD->setPure(true); |
6549 | } | ||||
6550 | |||||
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 6551 | SetNestedNameSpecifier(NewFD, D); |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 6552 | isExplicitSpecialization = false; |
6553 | isFunctionTemplateSpecialization = false; | ||||
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 6554 | if (D.isInvalidType()) |
6555 | NewFD->setInvalidDecl(); | ||||
Richard Smith | a41c97a | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 6556 | |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 6557 | // Match up the template parameter lists with the scope specifier, then |
6558 | // determine whether we have a template or a template specialization. | ||||
6559 | bool Invalid = false; | ||||
Robert Wilhelm | 1169e2f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 6560 | if (TemplateParameterList *TemplateParams = |
6561 | MatchTemplateParametersToScopeSpecifier( | ||||
6562 | D.getDeclSpec().getLocStart(), D.getIdentifierLoc(), | ||||
6563 | D.getCXXScopeSpec(), TemplateParamLists, isFriend, | ||||
6564 | isExplicitSpecialization, Invalid)) { | ||||
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6565 | if (TemplateParams->size() > 0) { |
6566 | // This is a function template | ||||
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 6567 | |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6568 | // Check that we can declare a template here. |
6569 | if (CheckTemplateDeclScope(S, TemplateParams)) | ||||
6570 | return 0; | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6571 | |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6572 | // A destructor cannot be a template. |
6573 | if (Name.getNameKind() == DeclarationName::CXXDestructorName) { | ||||
6574 | Diag(NewFD->getLocation(), diag::err_destructor_template); | ||||
6575 | return 0; | ||||
John McCall | 5fd378b | 2010-03-24 08:27:58 +0000 | [diff] [blame] | 6576 | } |
Douglas Gregor | 2060650 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 6577 | |
6578 | // 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] | 6579 | // rebuilding some of the types used within the template parameter list, |
Douglas Gregor | 2060650 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 6580 | // now that we know what the current instantiation is. |
6581 | if (DC->isDependentContext()) { | ||||
6582 | ContextRAII SavedContext(*this, DC); | ||||
6583 | if (RebuildTemplateParamsInCurrentInstantiation(TemplateParams)) | ||||
6584 | Invalid = true; | ||||
6585 | } | ||||
6586 | |||||
John McCall | 5fd378b | 2010-03-24 08:27:58 +0000 | [diff] [blame] | 6587 | |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6588 | FunctionTemplate = FunctionTemplateDecl::Create(Context, DC, |
6589 | NewFD->getLocation(), | ||||
6590 | Name, TemplateParams, | ||||
6591 | NewFD); | ||||
6592 | FunctionTemplate->setLexicalDeclContext(CurContext); | ||||
6593 | NewFD->setDescribedFunctionTemplate(FunctionTemplate); | ||||
6594 | |||||
6595 | // For source fidelity, store the other template param lists. | ||||
6596 | if (TemplateParamLists.size() > 1) { | ||||
6597 | NewFD->setTemplateParameterListsInfo(Context, | ||||
6598 | TemplateParamLists.size() - 1, | ||||
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 6599 | TemplateParamLists.data()); |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6600 | } |
6601 | } else { | ||||
6602 | // This is a function template specialization. | ||||
6603 | isFunctionTemplateSpecialization = true; | ||||
6604 | // For source fidelity, store all the template param lists. | ||||
6605 | NewFD->setTemplateParameterListsInfo(Context, | ||||
6606 | TemplateParamLists.size(), | ||||
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 6607 | TemplateParamLists.data()); |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6608 | |
6609 | // C++0x [temp.expl.spec]p20 forbids "template<> friend void foo(int);". | ||||
6610 | if (isFriend) { | ||||
6611 | // We want to remove the "template<>", found here. | ||||
6612 | SourceRange RemoveRange = TemplateParams->getSourceRange(); | ||||
6613 | |||||
6614 | // If we remove the template<> and the name is not a | ||||
6615 | // template-id, we're actually silently creating a problem: | ||||
6616 | // the friend declaration will refer to an untemplated decl, | ||||
6617 | // and clearly the user wants a template specialization. So | ||||
6618 | // we need to insert '<>' after the name. | ||||
6619 | SourceLocation InsertLoc; | ||||
6620 | if (D.getName().getKind() != UnqualifiedId::IK_TemplateId) { | ||||
6621 | InsertLoc = D.getName().getSourceRange().getEnd(); | ||||
6622 | InsertLoc = PP.getLocForEndOfToken(InsertLoc); | ||||
6623 | } | ||||
6624 | |||||
6625 | Diag(D.getIdentifierLoc(), diag::err_template_spec_decl_friend) | ||||
6626 | << Name << RemoveRange | ||||
6627 | << FixItHint::CreateRemoval(RemoveRange) | ||||
6628 | << FixItHint::CreateInsertion(InsertLoc, "<>"); | ||||
6629 | } | ||||
6630 | } | ||||
6631 | } | ||||
6632 | else { | ||||
6633 | // All template param lists were matched against the scope specifier: | ||||
6634 | // this is NOT (an explicit specialization of) a template. | ||||
6635 | if (TemplateParamLists.size() > 0) | ||||
6636 | // For source fidelity, store all the template param lists. | ||||
6637 | NewFD->setTemplateParameterListsInfo(Context, | ||||
6638 | TemplateParamLists.size(), | ||||
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 6639 | TemplateParamLists.data()); |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 6640 | } |
6641 | |||||
6642 | if (Invalid) { | ||||
6643 | NewFD->setInvalidDecl(); | ||||
6644 | if (FunctionTemplate) | ||||
6645 | FunctionTemplate->setInvalidDecl(); | ||||
6646 | } | ||||
Kaelyn Uhrain | d7e19ce | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6647 | |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 6648 | // C++ [dcl.fct.spec]p5: |
6649 | // The virtual specifier shall only be used in declarations of | ||||
6650 | // nonstatic class member functions that appear within a | ||||
6651 | // member-specification of a class declaration; see 10.3. | ||||
6652 | // | ||||
6653 | if (isVirtual && !NewFD->isInvalidDecl()) { | ||||
6654 | if (!isVirtualOkay) { | ||||
6655 | Diag(D.getDeclSpec().getVirtualSpecLoc(), | ||||
6656 | diag::err_virtual_non_function); | ||||
6657 | } else if (!CurContext->isRecord()) { | ||||
6658 | // 'virtual' was specified outside of the class. | ||||
Anders Carlsson | f1602a5 | 2011-01-22 14:43:56 +0000 | [diff] [blame] | 6659 | Diag(D.getDeclSpec().getVirtualSpecLoc(), |
6660 | diag::err_virtual_out_of_class) | ||||
6661 | << FixItHint::CreateRemoval(D.getDeclSpec().getVirtualSpecLoc()); | ||||
6662 | } else if (NewFD->getDescribedFunctionTemplate()) { | ||||
6663 | // C++ [temp.mem]p3: | ||||
6664 | // A member function template shall not be virtual. | ||||
6665 | Diag(D.getDeclSpec().getVirtualSpecLoc(), | ||||
6666 | diag::err_virtual_member_function_template) | ||||
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 6667 | << FixItHint::CreateRemoval(D.getDeclSpec().getVirtualSpecLoc()); |
6668 | } else { | ||||
6669 | // Okay: Add virtual to the method. | ||||
6670 | NewFD->setVirtualAsWritten(true); | ||||
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 6671 | } |
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 6672 | |
6673 | if (getLangOpts().CPlusPlus1y && | ||||
6674 | NewFD->getResultType()->isUndeducedType()) | ||||
6675 | Diag(D.getDeclSpec().getVirtualSpecLoc(), diag::err_auto_fn_virtual); | ||||
Douglas Gregor | c5c903a | 2009-06-24 00:23:40 +0000 | [diff] [blame] | 6676 | } |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 6677 | |
Richard Smith | 37e849a | 2013-08-14 20:16:31 +0000 | [diff] [blame] | 6678 | if (getLangOpts().CPlusPlus1y && NewFD->isDependentContext() && |
6679 | NewFD->getResultType()->isUndeducedType()) { | ||||
6680 | // If the function template is referenced directly (for instance, as a | ||||
6681 | // member of the current instantiation), pretend it has a dependent type. | ||||
6682 | // This is not really justified by the standard, but is the only sane | ||||
6683 | // thing to do. | ||||
6684 | const FunctionProtoType *FPT = | ||||
6685 | NewFD->getType()->castAs<FunctionProtoType>(); | ||||
6686 | QualType Result = SubstAutoType(FPT->getResultType(), | ||||
6687 | Context.DependentTy); | ||||
6688 | NewFD->setType(Context.getFunctionType(Result, FPT->getArgTypes(), | ||||
6689 | FPT->getExtProtoInfo())); | ||||
6690 | } | ||||
6691 | |||||
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 6692 | // C++ [dcl.fct.spec]p3: |
David Blaikie | d662a79 | 2011-10-19 22:56:21 +0000 | [diff] [blame] | 6693 | // The inline specifier shall not appear on a block scope function |
6694 | // declaration. | ||||
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 6695 | if (isInline && !NewFD->isInvalidDecl()) { |
6696 | if (CurContext->isFunctionOrMethod()) { | ||||
6697 | // 'inline' is not allowed on block scope function declaration. | ||||
6698 | Diag(D.getDeclSpec().getInlineSpecLoc(), | ||||
6699 | diag::err_inline_declaration_block_scope) << Name | ||||
6700 | << FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc()); | ||||
6701 | } | ||||
6702 | } | ||||
Kaelyn Uhrain | d7e19ce | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6703 | |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 6704 | // C++ [dcl.fct.spec]p6: |
6705 | // The explicit specifier shall be used only in the declaration of a | ||||
David Blaikie | d662a79 | 2011-10-19 22:56:21 +0000 | [diff] [blame] | 6706 | // constructor or conversion function within its class definition; |
6707 | // see 12.3.1 and 12.3.2. | ||||
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 6708 | if (isExplicit && !NewFD->isInvalidDecl()) { |
6709 | if (!CurContext->isRecord()) { | ||||
6710 | // 'explicit' was specified outside of the class. | ||||
6711 | Diag(D.getDeclSpec().getExplicitSpecLoc(), | ||||
6712 | diag::err_explicit_out_of_class) | ||||
6713 | << FixItHint::CreateRemoval(D.getDeclSpec().getExplicitSpecLoc()); | ||||
6714 | } else if (!isa<CXXConstructorDecl>(NewFD) && | ||||
6715 | !isa<CXXConversionDecl>(NewFD)) { | ||||
6716 | // 'explicit' was specified on a function that wasn't a constructor | ||||
6717 | // or conversion function. | ||||
6718 | Diag(D.getDeclSpec().getExplicitSpecLoc(), | ||||
6719 | diag::err_explicit_non_ctor_or_conv_function) | ||||
6720 | << FixItHint::CreateRemoval(D.getDeclSpec().getExplicitSpecLoc()); | ||||
6721 | } | ||||
6722 | } | ||||
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 6723 | |
Richard Smith | af1fc7a | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 6724 | if (isConstexpr) { |
Richard Smith | 21c8fa8 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 6725 | // C++11 [dcl.constexpr]p2: constexpr functions and constexpr constructors |
Richard Smith | af1fc7a | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 6726 | // are implicitly inline. |
6727 | NewFD->setImplicitlyInline(); | ||||
6728 | |||||
Richard Smith | 21c8fa8 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 6729 | // C++11 [dcl.constexpr]p3: functions declared constexpr are required to |
Richard Smith | af1fc7a | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 6730 | // be either constructors or to return a literal type. Therefore, |
6731 | // destructors cannot be declared constexpr. | ||||
6732 | if (isa<CXXDestructorDecl>(NewFD)) | ||||
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 6733 | Diag(D.getDeclSpec().getConstexprSpecLoc(), diag::err_constexpr_dtor); |
Richard Smith | af1fc7a | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 6734 | } |
6735 | |||||
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 6736 | // If __module_private__ was specified, mark the function accordingly. |
6737 | if (D.getDeclSpec().isModulePrivateSpecified()) { | ||||
Douglas Gregor | d023aec | 2011-09-09 20:53:38 +0000 | [diff] [blame] | 6738 | if (isFunctionTemplateSpecialization) { |
6739 | SourceLocation ModulePrivateLoc | ||||
6740 | = D.getDeclSpec().getModulePrivateSpecLoc(); | ||||
6741 | Diag(ModulePrivateLoc, diag::err_module_private_specialization) | ||||
6742 | << 0 | ||||
6743 | << FixItHint::CreateRemoval(ModulePrivateLoc); | ||||
6744 | } else { | ||||
6745 | NewFD->setModulePrivate(); | ||||
6746 | if (FunctionTemplate) | ||||
6747 | FunctionTemplate->setModulePrivate(); | ||||
6748 | } | ||||
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 6749 | } |
Richard Smith | af1fc7a | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 6750 | |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 6751 | if (isFriend) { |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 6752 | if (FunctionTemplate) { |
Richard Smith | 22050f2 | 2013-07-17 23:53:16 +0000 | [diff] [blame] | 6753 | FunctionTemplate->setObjectOfFriendDecl(); |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 6754 | FunctionTemplate->setAccess(AS_public); |
6755 | } | ||||
Richard Smith | 22050f2 | 2013-07-17 23:53:16 +0000 | [diff] [blame] | 6756 | NewFD->setObjectOfFriendDecl(); |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 6757 | NewFD->setAccess(AS_public); |
6758 | } | ||||
6759 | |||||
Douglas Gregor | 45fa560 | 2011-11-07 20:56:01 +0000 | [diff] [blame] | 6760 | // If a function is defined as defaulted or deleted, mark it as such now. |
6761 | switch (D.getFunctionDefinitionKind()) { | ||||
6762 | case FDK_Declaration: | ||||
6763 | case FDK_Definition: | ||||
6764 | break; | ||||
6765 | |||||
6766 | case FDK_Defaulted: | ||||
6767 | NewFD->setDefaulted(); | ||||
6768 | break; | ||||
6769 | |||||
6770 | case FDK_Deleted: | ||||
6771 | NewFD->setDeletedAsWritten(); | ||||
6772 | break; | ||||
6773 | } | ||||
6774 | |||||
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 6775 | if (isa<CXXMethodDecl>(NewFD) && DC == CurContext && |
6776 | D.isFunctionDefinition()) { | ||||
Douglas Gregor | 45fa560 | 2011-11-07 20:56:01 +0000 | [diff] [blame] | 6777 | // C++ [class.mfct]p2: |
6778 | // A member function may be defined (8.4) in its class definition, in | ||||
6779 | // which case it is an inline member function (7.1.2) | ||||
John McCall | bfdcdc8 | 2010-12-15 04:00:32 +0000 | [diff] [blame] | 6780 | NewFD->setImplicitlyInline(); |
6781 | } | ||||
6782 | |||||
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 6783 | if (SC == SC_Static && isa<CXXMethodDecl>(NewFD) && |
6784 | !CurContext->isRecord()) { | ||||
6785 | // C++ [class.static]p1: | ||||
6786 | // A data or function member of a class may be declared static | ||||
6787 | // in a class definition, in which case it is a static member of | ||||
6788 | // the class. | ||||
6789 | |||||
6790 | // Complain about the 'static' specifier if it's on an out-of-line | ||||
6791 | // member function definition. | ||||
6792 | Diag(D.getDeclSpec().getStorageClassSpecLoc(), | ||||
6793 | diag::err_static_out_of_line) | ||||
6794 | << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc()); | ||||
6795 | } | ||||
Richard Smith | 444d384 | 2012-10-20 08:26:51 +0000 | [diff] [blame] | 6796 | |
6797 | // C++11 [except.spec]p15: | ||||
6798 | // A deallocation function with no exception-specification is treated | ||||
6799 | // as if it were specified with noexcept(true). | ||||
6800 | const FunctionProtoType *FPT = R->getAs<FunctionProtoType>(); | ||||
6801 | if ((Name.getCXXOverloadedOperator() == OO_Delete || | ||||
6802 | Name.getCXXOverloadedOperator() == OO_Array_Delete) && | ||||
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6803 | getLangOpts().CPlusPlus11 && FPT && !FPT->hasExceptionSpec()) { |
Richard Smith | 444d384 | 2012-10-20 08:26:51 +0000 | [diff] [blame] | 6804 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
6805 | EPI.ExceptionSpecType = EST_BasicNoexcept; | ||||
6806 | NewFD->setType(Context.getFunctionType(FPT->getResultType(), | ||||
Reid Kleckner | 0567a79 | 2013-06-10 20:51:09 +0000 | [diff] [blame] | 6807 | FPT->getArgTypes(), EPI)); |
Richard Smith | 444d384 | 2012-10-20 08:26:51 +0000 | [diff] [blame] | 6808 | } |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 6809 | } |
Kaelyn Uhrain | d7e19ce | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6810 | |
6811 | // Filter out previous declarations that don't match the scope. | ||||
Richard Smith | a41c97a | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 6812 | FilterLookupForScope(Previous, OriginalDC, S, shouldConsiderLinkage(NewFD), |
Kaelyn Uhrain | d7e19ce | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6813 | isExplicitSpecialization || |
6814 | isFunctionTemplateSpecialization); | ||||
Richard Smith | dd9459f | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 6815 | |
Zhongxing Xu | 416fcaf | 2009-01-16 01:13:29 +0000 | [diff] [blame] | 6816 | // Handle GNU asm-label extension (encoded as an attribute). |
6817 | if (Expr *E = (Expr*) D.getAsmLabel()) { | ||||
6818 | // The parser guarantees this is a string. | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6819 | StringLiteral *SE = cast<StringLiteral>(E); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 6820 | NewFD->addAttr(::new (Context) AsmLabelAttr(SE->getStrTokenLoc(0), Context, |
6821 | SE->getString())); | ||||
David Chisnall | 5f3c163 | 2012-02-18 16:12:34 +0000 | [diff] [blame] | 6822 | } else if (!ExtnameUndeclaredIdentifiers.empty()) { |
6823 | llvm::DenseMap<IdentifierInfo*,AsmLabelAttr*>::iterator I = | ||||
6824 | ExtnameUndeclaredIdentifiers.find(NewFD->getIdentifier()); | ||||
6825 | if (I != ExtnameUndeclaredIdentifiers.end()) { | ||||
6826 | NewFD->addAttr(I->second); | ||||
6827 | ExtnameUndeclaredIdentifiers.erase(I); | ||||
6828 | } | ||||
Zhongxing Xu | 416fcaf | 2009-01-16 01:13:29 +0000 | [diff] [blame] | 6829 | } |
6830 | |||||
Chris Lattner | 2dbd285 | 2009-04-25 06:12:16 +0000 | [diff] [blame] | 6831 | // Copy the parameter declarations from the declarator D to the function |
6832 | // declaration NewFD, if they are available. First scavenge them into Params. | ||||
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6833 | SmallVector<ParmVarDecl*, 16> Params; |
Abramo Bagnara | 723df24 | 2010-12-14 22:11:44 +0000 | [diff] [blame] | 6834 | if (D.isFunctionDeclarator()) { |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 6835 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo(); |
Zhongxing Xu | 416fcaf | 2009-01-16 01:13:29 +0000 | [diff] [blame] | 6836 | |
Zhongxing Xu | 416fcaf | 2009-01-16 01:13:29 +0000 | [diff] [blame] | 6837 | // Check for C99 6.7.5.3p10 - foo(void) is a non-varargs |
6838 | // function that takes no arguments, not a function that takes a | ||||
6839 | // single void argument. | ||||
6840 | // We let through "const void" here because Sema::GetTypeForDeclarator | ||||
6841 | // already checks for that case. | ||||
6842 | if (FTI.NumArgs == 1 && !FTI.isVariadic && FTI.ArgInfo[0].Ident == 0 && | ||||
6843 | FTI.ArgInfo[0].Param && | ||||
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6844 | cast<ParmVarDecl>(FTI.ArgInfo[0].Param)->getType()->isVoidType()) { |
Chris Lattner | 2dbd285 | 2009-04-25 06:12:16 +0000 | [diff] [blame] | 6845 | // Empty arg list, don't push any params. |
Eli Friedman | 7c3c6bc | 2012-09-20 01:40:23 +0000 | [diff] [blame] | 6846 | checkVoidParamDecl(cast<ParmVarDecl>(FTI.ArgInfo[0].Param)); |
Zhongxing Xu | 416fcaf | 2009-01-16 01:13:29 +0000 | [diff] [blame] | 6847 | } else if (FTI.NumArgs > 0 && FTI.ArgInfo[0].Param != 0) { |
Argyrios Kyrtzidis | 9bedef6 | 2009-07-14 03:18:53 +0000 | [diff] [blame] | 6848 | for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6849 | ParmVarDecl *Param = cast<ParmVarDecl>(FTI.ArgInfo[i].Param); |
Argyrios Kyrtzidis | 9bedef6 | 2009-07-14 03:18:53 +0000 | [diff] [blame] | 6850 | assert(Param->getDeclContext() != NewFD && "Was set before ?"); |
6851 | Param->setDeclContext(NewFD); | ||||
6852 | Params.push_back(Param); | ||||
John McCall | f19de1c | 2010-04-14 01:27:20 +0000 | [diff] [blame] | 6853 | |
6854 | if (Param->isInvalidDecl()) | ||||
6855 | NewFD->setInvalidDecl(); | ||||
Argyrios Kyrtzidis | 9bedef6 | 2009-07-14 03:18:53 +0000 | [diff] [blame] | 6856 | } |
Zhongxing Xu | 416fcaf | 2009-01-16 01:13:29 +0000 | [diff] [blame] | 6857 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6858 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6859 | } else if (const FunctionProtoType *FT = R->getAs<FunctionProtoType>()) { |
Chris Lattner | 1ad9b28 | 2009-04-25 06:03:53 +0000 | [diff] [blame] | 6860 | // 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] | 6861 | // following example, we'll need to synthesize (unnamed) |
6862 | // parameters for use in the declaration. | ||||
6863 | // | ||||
6864 | // @code | ||||
6865 | // typedef void fn(int); | ||||
6866 | // fn f; | ||||
6867 | // @endcode | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6868 | |
Chris Lattner | 1ad9b28 | 2009-04-25 06:03:53 +0000 | [diff] [blame] | 6869 | // Synthesize a parameter for each argument type. |
Chris Lattner | 1ad9b28 | 2009-04-25 06:03:53 +0000 | [diff] [blame] | 6870 | for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(), |
6871 | AE = FT->arg_type_end(); AI != AE; ++AI) { | ||||
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 6872 | ParmVarDecl *Param = |
6873 | BuildParmVarDeclForTypedef(NewFD, D.getIdentifierLoc(), *AI); | ||||
John McCall | fb44de9 | 2011-05-01 22:35:37 +0000 | [diff] [blame] | 6874 | Param->setScopeInfo(0, Params.size()); |
Chris Lattner | 1ad9b28 | 2009-04-25 06:03:53 +0000 | [diff] [blame] | 6875 | Params.push_back(Param); |
Zhongxing Xu | 416fcaf | 2009-01-16 01:13:29 +0000 | [diff] [blame] | 6876 | } |
Chris Lattner | 84bb944 | 2009-04-25 18:38:18 +0000 | [diff] [blame] | 6877 | } else { |
6878 | assert(R->isFunctionNoProtoType() && NewFD->getNumParams() == 0 && | ||||
6879 | "Should not need args for typedef of non-prototype fn"); | ||||
Zhongxing Xu | 416fcaf | 2009-01-16 01:13:29 +0000 | [diff] [blame] | 6880 | } |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 6881 | |
Chris Lattner | 2dbd285 | 2009-04-25 06:12:16 +0000 | [diff] [blame] | 6882 | // Finally, we know we have the right number of parameters, install them. |
David Blaikie | 4278c65 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 6883 | NewFD->setParams(Params); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6884 | |
James Molloy | 16f1f71 | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 6885 | // Find all anonymous symbols defined during the declaration of this function |
6886 | // and add to NewFD. This lets us track decls such 'enum Y' in: | ||||
6887 | // | ||||
6888 | // void f(enum Y {AA} x) {} | ||||
6889 | // | ||||
6890 | // which would otherwise incorrectly end up in the translation unit scope. | ||||
6891 | NewFD->setDeclsInPrototypeScope(DeclsInPrototypeScope); | ||||
6892 | DeclsInPrototypeScope.clear(); | ||||
6893 | |||||
Richard Smith | 7586a6e | 2013-01-30 05:45:05 +0000 | [diff] [blame] | 6894 | if (D.getDeclSpec().isNoreturnSpecified()) |
6895 | NewFD->addAttr( | ||||
6896 | ::new(Context) C11NoReturnAttr(D.getDeclSpec().getNoreturnSpecLoc(), | ||||
6897 | Context)); | ||||
6898 | |||||
Richard Smith | b03a9df | 2012-03-13 05:56:40 +0000 | [diff] [blame] | 6899 | // Functions returning a variably modified type violate C99 6.7.5.2p2 |
6900 | // because all functions have linkage. | ||||
6901 | if (!NewFD->isInvalidDecl() && | ||||
6902 | NewFD->getResultType()->isVariablyModifiedType()) { | ||||
6903 | Diag(NewFD->getLocation(), diag::err_vm_func_decl); | ||||
6904 | NewFD->setInvalidDecl(); | ||||
6905 | } | ||||
6906 | |||||
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 6907 | // Handle attributes. |
Richard Smith | 4a97b8e | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 6908 | ProcessDeclAttributes(S, NewFD, D); |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 6909 | |
Kaelyn Uhrain | 51ceb7b | 2012-11-12 23:48:05 +0000 | [diff] [blame] | 6910 | QualType RetType = NewFD->getResultType(); |
6911 | const CXXRecordDecl *Ret = RetType->isRecordType() ? | ||||
6912 | RetType->getAsCXXRecordDecl() : RetType->getPointeeCXXRecordDecl(); | ||||
6913 | if (!NewFD->isInvalidDecl() && !NewFD->hasAttr<WarnUnusedResultAttr>() && | ||||
6914 | Ret && Ret->hasAttr<WarnUnusedResultAttr>()) { | ||||
Kaelyn Uhrain | 97c81bf | 2012-11-13 21:23:31 +0000 | [diff] [blame] | 6915 | const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(NewFD); |
6916 | if (!(MD && MD->getCorrespondingMethodInClass(Ret, true))) { | ||||
6917 | NewFD->addAttr(new (Context) WarnUnusedResultAttr(SourceRange(), | ||||
6918 | Context)); | ||||
6919 | } | ||||
Kaelyn Uhrain | 51ceb7b | 2012-11-12 23:48:05 +0000 | [diff] [blame] | 6920 | } |
6921 | |||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6922 | if (!getLangOpts().CPlusPlus) { |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 6923 | // Perform semantic checking on the function declaration. |
Douglas Gregor | 89b9f10 | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 6924 | bool isExplicitSpecialization=false; |
David Majnemer | c371db6 | 2013-07-06 02:13:46 +0000 | [diff] [blame] | 6925 | if (!NewFD->isInvalidDecl() && NewFD->isMain()) |
6926 | CheckMain(NewFD, D.getDeclSpec()); | ||||
6927 | |||||
David Majnemer | e9f6f33 | 2013-09-16 22:44:20 +0000 | [diff] [blame] | 6928 | if (!NewFD->isInvalidDecl() && NewFD->isMSVCRTEntryPoint()) |
6929 | CheckMSVCRTEntryPoint(NewFD); | ||||
6930 | |||||
David Majnemer | c371db6 | 2013-07-06 02:13:46 +0000 | [diff] [blame] | 6931 | if (!NewFD->isInvalidDecl()) |
Richard Smith | b03a9df | 2012-03-13 05:56:40 +0000 | [diff] [blame] | 6932 | D.setRedeclaration(CheckFunctionDeclaration(S, NewFD, Previous, |
6933 | isExplicitSpecialization)); | ||||
Fariborz Jahanian | 37c765a | 2012-09-05 17:52:12 +0000 | [diff] [blame] | 6934 | else if (!Previous.empty()) |
Richard Smith | dd9459f | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 6935 | // Make graceful recovery from an invalid redeclaration. |
6936 | D.setRedeclaration(true); | ||||
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 6937 | assert((NewFD->isInvalidDecl() || !D.isRedeclaration() || |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 6938 | Previous.getResultKind() != LookupResult::FoundOverloaded) && |
6939 | "previous declaration set still overloaded"); | ||||
6940 | } else { | ||||
6941 | // If the declarator is a template-id, translate the parser's template | ||||
6942 | // argument list into our AST format. | ||||
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 6943 | if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) { |
6944 | TemplateIdAnnotation *TemplateId = D.getName().TemplateId; | ||||
6945 | TemplateArgs.setLAngleLoc(TemplateId->LAngleLoc); | ||||
6946 | TemplateArgs.setRAngleLoc(TemplateId->RAngleLoc); | ||||
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 6947 | ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(), |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 6948 | TemplateId->NumArgs); |
6949 | translateTemplateArguments(TemplateArgsPtr, | ||||
6950 | TemplateArgs); | ||||
Douglas Gregor | 0b60d9e | 2009-09-25 23:53:26 +0000 | [diff] [blame] | 6951 | |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 6952 | HasExplicitTemplateArgs = true; |
Douglas Gregor | 0b60d9e | 2009-09-25 23:53:26 +0000 | [diff] [blame] | 6953 | |
Douglas Gregor | 89b9f10 | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 6954 | if (NewFD->isInvalidDecl()) { |
6955 | HasExplicitTemplateArgs = false; | ||||
6956 | } else if (FunctionTemplate) { | ||||
Douglas Gregor | 5505c72 | 2011-01-24 18:54:39 +0000 | [diff] [blame] | 6957 | // Function template with explicit template arguments. |
6958 | Diag(D.getIdentifierLoc(), diag::err_function_template_partial_spec) | ||||
6959 | << SourceRange(TemplateId->LAngleLoc, TemplateId->RAngleLoc); | ||||
6960 | |||||
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 6961 | HasExplicitTemplateArgs = false; |
6962 | } else if (!isFunctionTemplateSpecialization && | ||||
6963 | !D.getDeclSpec().isFriendSpecified()) { | ||||
6964 | // We have encountered something that the user meant to be a | ||||
6965 | // specialization (because it has explicitly-specified template | ||||
6966 | // arguments) but that was not introduced with a "template<>" (or had | ||||
6967 | // too few of them). | ||||
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 6968 | // FIXME: Differentiate between attempts for explicit instantiations |
6969 | // (starting with "template") and the rest. | ||||
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 6970 | Diag(D.getIdentifierLoc(), diag::err_template_spec_needs_header) |
6971 | << SourceRange(TemplateId->LAngleLoc, TemplateId->RAngleLoc) | ||||
6972 | << FixItHint::CreateInsertion( | ||||
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 6973 | D.getDeclSpec().getLocStart(), |
David Blaikie | d662a79 | 2011-10-19 22:56:21 +0000 | [diff] [blame] | 6974 | "template<> "); |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 6975 | isFunctionTemplateSpecialization = true; |
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 6976 | } else { |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 6977 | // "friend void foo<>(int);" is an implicit specialization decl. |
6978 | isFunctionTemplateSpecialization = true; | ||||
Francois Pichet | c71d8eb | 2010-10-01 21:19:28 +0000 | [diff] [blame] | 6979 | } |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 6980 | } else if (isFriend && isFunctionTemplateSpecialization) { |
6981 | // This combination is only possible in a recovery case; the user | ||||
6982 | // wrote something like: | ||||
6983 | // template <> friend void foo(int); | ||||
6984 | // which we're recovering from as if the user had written: | ||||
6985 | // friend void foo<>(int); | ||||
6986 | // Go ahead and fake up a template id. | ||||
6987 | HasExplicitTemplateArgs = true; | ||||
6988 | TemplateArgs.setLAngleLoc(D.getIdentifierLoc()); | ||||
6989 | TemplateArgs.setRAngleLoc(D.getIdentifierLoc()); | ||||
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 6990 | } |
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 6991 | |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 6992 | // If it's a friend (and only if it's a friend), it's possible |
6993 | // that either the specialized function type or the specialized | ||||
6994 | // template is dependent, and therefore matching will fail. In | ||||
6995 | // this case, don't check the specialization yet. | ||||
Douglas Gregor | 33ab0da | 2011-10-09 20:59:17 +0000 | [diff] [blame] | 6996 | bool InstantiationDependent = false; |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 6997 | if (isFunctionTemplateSpecialization && isFriend && |
Douglas Gregor | 33ab0da | 2011-10-09 20:59:17 +0000 | [diff] [blame] | 6998 | (NewFD->getType()->isDependentType() || DC->isDependentContext() || |
6999 | TemplateSpecializationType::anyDependentTemplateArguments( | ||||
7000 | TemplateArgs.getArgumentArray(), TemplateArgs.size(), | ||||
7001 | InstantiationDependent))) { | ||||
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7002 | assert(HasExplicitTemplateArgs && |
7003 | "friend function specialization without template args"); | ||||
7004 | if (CheckDependentFunctionTemplateSpecialization(NewFD, TemplateArgs, | ||||
7005 | Previous)) | ||||
7006 | NewFD->setInvalidDecl(); | ||||
7007 | } else if (isFunctionTemplateSpecialization) { | ||||
Douglas Gregor | eef7ac5 | 2011-03-16 19:27:09 +0000 | [diff] [blame] | 7008 | if (CurContext->isDependentContext() && CurContext->isRecord() |
Francois Pichet | ab01add | 2011-06-03 13:59:45 +0000 | [diff] [blame] | 7009 | && !isFriend) { |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 7010 | isDependentClassScopeExplicitSpecialization = true; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7011 | Diag(NewFD->getLocation(), getLangOpts().MicrosoftExt ? |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 7012 | diag::ext_function_specialization_in_class : |
7013 | diag::err_function_specialization_in_class) | ||||
Douglas Gregor | eef7ac5 | 2011-03-16 19:27:09 +0000 | [diff] [blame] | 7014 | << NewFD->getDeclName(); |
Douglas Gregor | eef7ac5 | 2011-03-16 19:27:09 +0000 | [diff] [blame] | 7015 | } else if (CheckFunctionTemplateSpecialization(NewFD, |
7016 | (HasExplicitTemplateArgs ? &TemplateArgs : 0), | ||||
7017 | Previous)) | ||||
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7018 | NewFD->setInvalidDecl(); |
Douglas Gregor | e885e18 | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 7019 | |
7020 | // C++ [dcl.stc]p1: | ||||
7021 | // A storage-class-specifier shall not be specified in an explicit | ||||
7022 | // specialization (14.7.3) | ||||
Richard Trieu | 62ab010 | 2013-05-16 02:14:08 +0000 | [diff] [blame] | 7023 | FunctionTemplateSpecializationInfo *Info = |
7024 | NewFD->getTemplateSpecializationInfo(); | ||||
7025 | if (Info && SC != SC_None) { | ||||
7026 | if (SC != Info->getTemplate()->getTemplatedDecl()->getStorageClass()) | ||||
Douglas Gregor | 0f9dc86 | 2011-06-17 05:09:08 +0000 | [diff] [blame] | 7027 | Diag(NewFD->getLocation(), |
7028 | diag::err_explicit_specialization_inconsistent_storage_class) | ||||
7029 | << SC | ||||
7030 | << FixItHint::CreateRemoval( | ||||
7031 | D.getDeclSpec().getStorageClassSpecLoc()); | ||||
7032 | |||||
7033 | else | ||||
7034 | Diag(NewFD->getLocation(), | ||||
7035 | diag::ext_explicit_specialization_storage_class) | ||||
7036 | << FixItHint::CreateRemoval( | ||||
7037 | D.getDeclSpec().getStorageClassSpecLoc()); | ||||
Douglas Gregor | e885e18 | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 7038 | } |
7039 | |||||
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7040 | } else if (isExplicitSpecialization && isa<CXXMethodDecl>(NewFD)) { |
7041 | if (CheckMemberSpecialization(NewFD, Previous)) | ||||
7042 | NewFD->setInvalidDecl(); | ||||
7043 | } | ||||
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 7044 | |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7045 | // Perform semantic checking on the function declaration. |
David Blaikie | 14068e8 | 2011-09-08 06:33:04 +0000 | [diff] [blame] | 7046 | if (!isDependentClassScopeExplicitSpecialization) { |
David Majnemer | c371db6 | 2013-07-06 02:13:46 +0000 | [diff] [blame] | 7047 | if (!NewFD->isInvalidDecl() && NewFD->isMain()) |
7048 | CheckMain(NewFD, D.getDeclSpec()); | ||||
7049 | |||||
David Majnemer | e9f6f33 | 2013-09-16 22:44:20 +0000 | [diff] [blame] | 7050 | if (!NewFD->isInvalidDecl() && NewFD->isMSVCRTEntryPoint()) |
7051 | CheckMSVCRTEntryPoint(NewFD); | ||||
7052 | |||||
David Blaikie | 14068e8 | 2011-09-08 06:33:04 +0000 | [diff] [blame] | 7053 | if (NewFD->isInvalidDecl()) { |
7054 | // If this is a class member, mark the class invalid immediately. | ||||
7055 | // This avoids some consistency errors later. | ||||
7056 | if (CXXMethodDecl* methodDecl = dyn_cast<CXXMethodDecl>(NewFD)) | ||||
7057 | methodDecl->getParent()->setInvalidDecl(); | ||||
David Majnemer | c371db6 | 2013-07-06 02:13:46 +0000 | [diff] [blame] | 7058 | } else |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 7059 | D.setRedeclaration(CheckFunctionDeclaration(S, NewFD, Previous, |
7060 | isExplicitSpecialization)); | ||||
David Blaikie | 14068e8 | 2011-09-08 06:33:04 +0000 | [diff] [blame] | 7061 | } |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7062 | |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 7063 | assert((NewFD->isInvalidDecl() || !D.isRedeclaration() || |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7064 | Previous.getResultKind() != LookupResult::FoundOverloaded) && |
7065 | "previous declaration set still overloaded"); | ||||
7066 | |||||
7067 | NamedDecl *PrincipalDecl = (FunctionTemplate | ||||
7068 | ? cast<NamedDecl>(FunctionTemplate) | ||||
7069 | : NewFD); | ||||
7070 | |||||
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 7071 | if (isFriend && D.isRedeclaration()) { |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7072 | AccessSpecifier Access = AS_public; |
7073 | if (!NewFD->isInvalidDecl()) | ||||
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 7074 | Access = NewFD->getPreviousDecl()->getAccess(); |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7075 | |
7076 | NewFD->setAccess(Access); | ||||
7077 | if (FunctionTemplate) FunctionTemplate->setAccess(Access); | ||||
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7078 | } |
7079 | |||||
7080 | if (NewFD->isOverloadedOperator() && !DC->isRecord() && | ||||
7081 | PrincipalDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary)) | ||||
7082 | PrincipalDecl->setNonMemberOperator(); | ||||
7083 | |||||
7084 | // If we have a function template, check the template parameter | ||||
7085 | // list. This will check and merge default template arguments. | ||||
7086 | if (FunctionTemplate) { | ||||
David Blaikie | d662a79 | 2011-10-19 22:56:21 +0000 | [diff] [blame] | 7087 | FunctionTemplateDecl *PrevTemplate = |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 7088 | FunctionTemplate->getPreviousDecl(); |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7089 | CheckTemplateParameterList(FunctionTemplate->getTemplateParameters(), |
David Blaikie | d662a79 | 2011-10-19 22:56:21 +0000 | [diff] [blame] | 7090 | PrevTemplate ? PrevTemplate->getTemplateParameters() : 0, |
Douglas Gregor | d89d86f | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 7091 | D.getDeclSpec().isFriendSpecified() |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 7092 | ? (D.isFunctionDefinition() |
Douglas Gregor | d89d86f | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 7093 | ? TPC_FriendFunctionTemplateDefinition |
7094 | : TPC_FriendFunctionTemplate) | ||||
7095 | : (D.getCXXScopeSpec().isSet() && | ||||
Douglas Gregor | 461bf2e | 2011-02-04 12:22:53 +0000 | [diff] [blame] | 7096 | DC && DC->isRecord() && |
7097 | DC->isDependentContext()) | ||||
Douglas Gregor | d89d86f | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 7098 | ? TPC_ClassTemplateMember |
7099 | : TPC_FunctionTemplate); | ||||
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7100 | } |
7101 | |||||
7102 | if (NewFD->isInvalidDecl()) { | ||||
7103 | // Ignore all the rest of this. | ||||
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 7104 | } else if (!D.isRedeclaration()) { |
Kaelyn Uhrain | f09ce39 | 2011-10-11 00:28:52 +0000 | [diff] [blame] | 7105 | struct ActOnFDArgs ExtraArgs = { S, D, TemplateParamLists, |
Kaelyn Uhrain | 2afd766 | 2011-10-11 00:28:39 +0000 | [diff] [blame] | 7106 | AddToScope }; |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7107 | // Fake up an access specifier if it's supposed to be a class member. |
7108 | if (isa<CXXRecordDecl>(NewFD->getDeclContext())) | ||||
7109 | NewFD->setAccess(AS_public); | ||||
7110 | |||||
7111 | // Qualified decls generally require a previous declaration. | ||||
7112 | if (D.getCXXScopeSpec().isSet()) { | ||||
7113 | // ...with the major exception of templated-scope or | ||||
7114 | // dependent-scope friend declarations. | ||||
7115 | |||||
7116 | // TODO: we currently also suppress this check in dependent | ||||
7117 | // contexts because (1) the parameter depth will be off when | ||||
7118 | // matching friend templates and (2) we might actually be | ||||
7119 | // selecting a friend based on a dependent factor. But there | ||||
7120 | // are situations where these conditions don't apply and we | ||||
7121 | // can actually do this check immediately. | ||||
7122 | if (isFriend && | ||||
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 7123 | (TemplateParamLists.size() || |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7124 | D.getCXXScopeSpec().getScopeRep()->isDependent() || |
7125 | CurContext->isDependentContext())) { | ||||
Chandler Carruth | 47eb2b6 | 2011-08-19 01:38:33 +0000 | [diff] [blame] | 7126 | // ignore these |
7127 | } else { | ||||
7128 | // The user tried to provide an out-of-line definition for a | ||||
7129 | // function that is a member of a class or namespace, but there | ||||
7130 | // was no such member function declared (C++ [class.mfct]p2, | ||||
7131 | // C++ [namespace.memdef]p2). For example: | ||||
7132 | // | ||||
7133 | // class X { | ||||
7134 | // void f() const; | ||||
7135 | // }; | ||||
7136 | // | ||||
7137 | // void X::f() { } // ill-formed | ||||
7138 | // | ||||
7139 | // Complain about this problem, and attempt to suggest close | ||||
7140 | // matches (e.g., those that differ only in cv-qualifiers and | ||||
7141 | // whether the parameter types are references). | ||||
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7142 | |
Richard Smith | 4e9686b | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 7143 | if (NamedDecl *Result = DiagnoseInvalidRedeclaration( |
7144 | *this, Previous, NewFD, ExtraArgs, false, 0)) { | ||||
Kaelyn Uhrain | 2afd766 | 2011-10-11 00:28:39 +0000 | [diff] [blame] | 7145 | AddToScope = ExtraArgs.AddToScope; |
7146 | return Result; | ||||
7147 | } | ||||
Chandler Carruth | 47eb2b6 | 2011-08-19 01:38:33 +0000 | [diff] [blame] | 7148 | } |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7149 | |
7150 | // Unqualified local friend declarations are required to resolve | ||||
7151 | // to something. | ||||
Chandler Carruth | 3d095fe | 2011-08-19 01:40:11 +0000 | [diff] [blame] | 7152 | } else if (isFriend && cast<CXXRecordDecl>(CurContext)->isLocalClass()) { |
Richard Smith | 4e9686b | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 7153 | if (NamedDecl *Result = DiagnoseInvalidRedeclaration( |
7154 | *this, Previous, NewFD, ExtraArgs, true, S)) { | ||||
Kaelyn Uhrain | 2afd766 | 2011-10-11 00:28:39 +0000 | [diff] [blame] | 7155 | AddToScope = ExtraArgs.AddToScope; |
7156 | return Result; | ||||
7157 | } | ||||
Chandler Carruth | 3d095fe | 2011-08-19 01:40:11 +0000 | [diff] [blame] | 7158 | } |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7159 | |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 7160 | } else if (!D.isFunctionDefinition() && D.getCXXScopeSpec().isSet() && |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7161 | !isFriend && !isFunctionTemplateSpecialization && |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 7162 | !isExplicitSpecialization) { |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7163 | // An out-of-line member function declaration must also be a |
7164 | // definition (C++ [dcl.meaning]p1). | ||||
7165 | // Note that this is not the case for explicit specializations of | ||||
7166 | // function templates or member functions of class templates, per | ||||
David Blaikie | d662a79 | 2011-10-19 22:56:21 +0000 | [diff] [blame] | 7167 | // C++ [temp.expl.spec]p2. We also allow these declarations as an |
7168 | // extension for compatibility with old SWIG code which likes to | ||||
7169 | // generate them. | ||||
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7170 | Diag(NewFD->getLocation(), diag::ext_out_of_line_declaration) |
7171 | << D.getCXXScopeSpec().getRange(); | ||||
7172 | } | ||||
7173 | } | ||||
Ryan Flynn | 478fbc6 | 2009-07-25 22:29:44 +0000 | [diff] [blame] | 7174 | |
Rafael Espindola | 65611bf | 2013-03-02 21:41:48 +0000 | [diff] [blame] | 7175 | ProcessPragmaWeak(S, NewFD); |
Rafael Espindola | 2a5bb50 | 2013-01-16 23:11:15 +0000 | [diff] [blame] | 7176 | checkAttributesAfterMerging(*this, *NewFD); |
7177 | |||||
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 7178 | AddKnownFunctionAttributes(NewFD); |
7179 | |||||
Douglas Gregor | d945538 | 2010-08-06 13:50:58 +0000 | [diff] [blame] | 7180 | if (NewFD->hasAttr<OverloadableAttr>() && |
7181 | !NewFD->getType()->getAs<FunctionProtoType>()) { | ||||
7182 | Diag(NewFD->getLocation(), | ||||
7183 | diag::err_attribute_overloadable_no_prototype) | ||||
7184 | << NewFD; | ||||
7185 | |||||
7186 | // Turn this into a variadic function with no parameters. | ||||
7187 | const FunctionType *FT = NewFD->getType()->getAs<FunctionType>(); | ||||
Reid Kleckner | ef07203 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 7188 | FunctionProtoType::ExtProtoInfo EPI( |
7189 | Context.getDefaultCallingConvention(true, false)); | ||||
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 7190 | EPI.Variadic = true; |
7191 | EPI.ExtInfo = FT->getExtInfo(); | ||||
7192 | |||||
Dmitri Gribenko | 5543169 | 2013-05-05 00:41:58 +0000 | [diff] [blame] | 7193 | QualType R = Context.getFunctionType(FT->getResultType(), None, EPI); |
Douglas Gregor | d945538 | 2010-08-06 13:50:58 +0000 | [diff] [blame] | 7194 | NewFD->setType(R); |
7195 | } | ||||
7196 | |||||
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 7197 | // If there's a #pragma GCC visibility in scope, and this isn't a class |
7198 | // member, set the visibility of this function. | ||||
Rafael Espindola | 181e3ec | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 7199 | if (!DC->isRecord() && NewFD->isExternallyVisible()) |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 7200 | AddPushedVisibilityAttribute(NewFD); |
7201 | |||||
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 7202 | // If there's a #pragma clang arc_cf_code_audited in scope, consider |
7203 | // marking the function. | ||||
7204 | AddCFAuditedAttribute(NewFD); | ||||
7205 | |||||
Richard Smith | aa4bc18 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 7206 | // If this is the first declaration of an extern C variable, update |
7207 | // the map of such variables. | ||||
7208 | if (!NewFD->getPreviousDecl() && !NewFD->isInvalidDecl() && | ||||
7209 | isIncompleteDeclExternC(*this, NewFD)) | ||||
Richard Smith | 662f41b | 2013-06-18 20:15:12 +0000 | [diff] [blame] | 7210 | RegisterLocallyScopedExternCDecl(NewFD, S); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 7211 | |
Argyrios Kyrtzidis | 16f1930 | 2009-06-25 18:22:24 +0000 | [diff] [blame] | 7212 | // Set this FunctionDecl's range up to the right paren. |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 7213 | NewFD->setRangeEnd(D.getSourceRange().getEnd()); |
Argyrios Kyrtzidis | 16f1930 | 2009-06-25 18:22:24 +0000 | [diff] [blame] | 7214 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7215 | if (getLangOpts().CPlusPlus) { |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7216 | if (FunctionTemplate) { |
7217 | if (NewFD->isInvalidDecl()) | ||||
7218 | FunctionTemplate->setInvalidDecl(); | ||||
7219 | return FunctionTemplate; | ||||
7220 | } | ||||
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7221 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7222 | |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 7223 | if (NewFD->hasAttr<OpenCLKernelAttr>()) { |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 7224 | // OpenCL v1.2 s6.8 static is invalid for kernel functions. |
7225 | if ((getLangOpts().OpenCLVersion >= 120) | ||||
7226 | && (SC == SC_Static)) { | ||||
7227 | Diag(D.getIdentifierLoc(), diag::err_static_kernel); | ||||
7228 | D.setInvalidType(); | ||||
7229 | } | ||||
Tanya Lattner | 7564bcc | 2013-01-30 19:48:52 +0000 | [diff] [blame] | 7230 | |
7231 | // OpenCL v1.2, s6.9 -- Kernels can only have return type void. | ||||
7232 | if (!NewFD->getResultType()->isVoidType()) { | ||||
7233 | Diag(D.getIdentifierLoc(), | ||||
7234 | diag::err_expected_kernel_void_return_type); | ||||
7235 | D.setInvalidType(); | ||||
7236 | } | ||||
Matt Arsenault | e6c8afc | 2013-07-23 01:23:36 +0000 | [diff] [blame] | 7237 | |
7238 | llvm::SmallPtrSet<const Type *, 16> ValidTypes; | ||||
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 7239 | for (FunctionDecl::param_iterator PI = NewFD->param_begin(), |
7240 | PE = NewFD->param_end(); PI != PE; ++PI) { | ||||
Joey Gouly | 98f988d | 2013-01-29 10:54:06 +0000 | [diff] [blame] | 7241 | ParmVarDecl *Param = *PI; |
Matt Arsenault | e6c8afc | 2013-07-23 01:23:36 +0000 | [diff] [blame] | 7242 | checkIsValidOpenCLKernelParameter(*this, D, Param, ValidTypes); |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 7243 | } |
Tanya Lattner | 5e94d6f | 2012-06-19 23:09:52 +0000 | [diff] [blame] | 7244 | } |
7245 | |||||
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 7246 | MarkUnusedFileScopedDecl(NewFD); |
Peter Collingbourne | 14b6ba7 | 2011-02-09 21:04:32 +0000 | [diff] [blame] | 7247 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7248 | if (getLangOpts().CUDA) |
Peter Collingbourne | 14b6ba7 | 2011-02-09 21:04:32 +0000 | [diff] [blame] | 7249 | if (IdentifierInfo *II = NewFD->getIdentifier()) |
7250 | if (!NewFD->isInvalidDecl() && | ||||
7251 | NewFD->getDeclContext()->getRedeclContext()->isTranslationUnit()) { | ||||
7252 | if (II->isStr("cudaConfigureCall")) { | ||||
7253 | if (!R->getAs<FunctionType>()->getResultType()->isScalarType()) | ||||
7254 | Diag(NewFD->getLocation(), diag::err_config_scalar_return); | ||||
7255 | |||||
7256 | Context.setcudaConfigureCallDecl(NewFD); | ||||
7257 | } | ||||
7258 | } | ||||
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 7259 | |
7260 | // Here we have an function template explicit specialization at class scope. | ||||
7261 | // The actually specialization will be postponed to template instatiation | ||||
7262 | // time via the ClassScopeFunctionSpecializationDecl node. | ||||
7263 | if (isDependentClassScopeExplicitSpecialization) { | ||||
7264 | ClassScopeFunctionSpecializationDecl *NewSpec = | ||||
7265 | ClassScopeFunctionSpecializationDecl::Create( | ||||
Nico Weber | 6b02009 | 2012-06-25 17:21:05 +0000 | [diff] [blame] | 7266 | Context, CurContext, SourceLocation(), |
7267 | cast<CXXMethodDecl>(NewFD), | ||||
7268 | HasExplicitTemplateArgs, TemplateArgs); | ||||
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 7269 | CurContext->addDecl(NewSpec); |
7270 | AddToScope = false; | ||||
7271 | } | ||||
Peter Collingbourne | 14b6ba7 | 2011-02-09 21:04:32 +0000 | [diff] [blame] | 7272 | |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 7273 | return NewFD; |
7274 | } | ||||
7275 | |||||
7276 | /// \brief Perform semantic checking of a new function declaration. | ||||
7277 | /// | ||||
7278 | /// Performs semantic analysis of the new function declaration | ||||
7279 | /// NewFD. This routine performs all semantic checking that does not | ||||
7280 | /// require the actual declarator involved in the declaration, and is | ||||
7281 | /// used both for the declaration of functions as they are parsed | ||||
7282 | /// (called via ActOnDeclarator) and for the declaration of functions | ||||
7283 | /// that have been instantiated via C++ template instantiation (called | ||||
7284 | /// via InstantiateDecl). | ||||
7285 | /// | ||||
James Dennett | efce31f | 2012-06-22 08:10:18 +0000 | [diff] [blame] | 7286 | /// \param IsExplicitSpecialization whether this new function declaration is |
Douglas Gregor | fd056bc | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 7287 | /// an explicit specialization of the previous declaration. |
7288 | /// | ||||
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 7289 | /// This sets NewFD->isInvalidDecl() to true if there was an error. |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 7290 | /// |
James Dennett | efce31f | 2012-06-22 08:10:18 +0000 | [diff] [blame] | 7291 | /// \returns true if the function declaration is a redeclaration. |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 7292 | bool Sema::CheckFunctionDeclaration(Scope *S, FunctionDecl *NewFD, |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7293 | LookupResult &Previous, |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 7294 | bool IsExplicitSpecialization) { |
David Blaikie | 14068e8 | 2011-09-08 06:33:04 +0000 | [diff] [blame] | 7295 | assert(!NewFD->getResultType()->isVariablyModifiedType() |
7296 | && "Variably modified return types are not handled here"); | ||||
John McCall | 8c4859a | 2009-07-24 03:03:21 +0000 | [diff] [blame] | 7297 | |
Richard Smith | dd9459f | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 7298 | // Determine whether the type of this function should be merged with |
7299 | // a previous visible declaration. This never happens for functions in C++, | ||||
7300 | // and always happens in C if the previous declaration was visible. | ||||
7301 | bool MergeTypeWithPrevious = !getLangOpts().CPlusPlus && | ||||
7302 | !Previous.isShadowed(); | ||||
7303 | |||||
Douglas Gregor | 7dc80e1 | 2013-01-09 00:47:56 +0000 | [diff] [blame] | 7304 | // Filter out any non-conflicting previous declarations. |
7305 | filterNonConflictingPreviousDecls(Context, NewFD, Previous); | ||||
7306 | |||||
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 7307 | bool Redeclaration = false; |
Richard Smith | 21c8fa8 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 7308 | NamedDecl *OldDecl = 0; |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 7309 | |
Douglas Gregor | 04495c8 | 2009-02-24 01:23:02 +0000 | [diff] [blame] | 7310 | // Merge or overload the declaration with an existing declaration of |
7311 | // the same name, if appropriate. | ||||
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7312 | if (!Previous.empty()) { |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 7313 | // Determine whether NewFD is an overload of PrevDecl or |
Zhongxing Xu | 416fcaf | 2009-01-16 01:13:29 +0000 | [diff] [blame] | 7314 | // a declaration that requires merging. If it's an overload, |
7315 | // there's no more work to do here; we'll just add the new | ||||
7316 | // function to the scope. | ||||
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 7317 | if (!AllowOverloadingOfFunction(Previous, Context)) { |
Rafael Espindola | 90cc390 | 2013-04-15 12:49:13 +0000 | [diff] [blame] | 7318 | NamedDecl *Candidate = Previous.getFoundDecl(); |
7319 | if (shouldLinkPossiblyHiddenDecl(Candidate, NewFD)) { | ||||
7320 | Redeclaration = true; | ||||
7321 | OldDecl = Candidate; | ||||
7322 | } | ||||
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 7323 | } else { |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 7324 | switch (CheckOverload(S, NewFD, Previous, OldDecl, |
7325 | /*NewIsUsingDecl*/ false)) { | ||||
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 7326 | case Ovl_Match: |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7327 | Redeclaration = true; |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 7328 | break; |
7329 | |||||
7330 | case Ovl_NonFunction: | ||||
7331 | Redeclaration = true; | ||||
7332 | break; | ||||
7333 | |||||
7334 | case Ovl_Overload: | ||||
7335 | Redeclaration = false; | ||||
7336 | break; | ||||
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7337 | } |
Peter Collingbourne | c80e811 | 2011-01-21 02:08:54 +0000 | [diff] [blame] | 7338 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7339 | if (!getLangOpts().CPlusPlus && !NewFD->hasAttr<OverloadableAttr>()) { |
Peter Collingbourne | c80e811 | 2011-01-21 02:08:54 +0000 | [diff] [blame] | 7340 | // If a function name is overloadable in C, then every function |
7341 | // with that name must be marked "overloadable". | ||||
7342 | Diag(NewFD->getLocation(), diag::err_attribute_overloadable_missing) | ||||
7343 | << Redeclaration << NewFD; | ||||
7344 | NamedDecl *OverloadedDecl = 0; | ||||
7345 | if (Redeclaration) | ||||
7346 | OverloadedDecl = OldDecl; | ||||
7347 | else if (!Previous.empty()) | ||||
7348 | OverloadedDecl = Previous.getRepresentativeDecl(); | ||||
7349 | if (OverloadedDecl) | ||||
7350 | Diag(OverloadedDecl->getLocation(), | ||||
7351 | diag::note_attribute_overloadable_prev_overload); | ||||
7352 | NewFD->addAttr(::new (Context) OverloadableAttr(SourceLocation(), | ||||
7353 | Context)); | ||||
7354 | } | ||||
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7355 | } |
Richard Smith | 21c8fa8 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 7356 | } |
Zhongxing Xu | 416fcaf | 2009-01-16 01:13:29 +0000 | [diff] [blame] | 7357 | |
Richard Smith | aa4bc18 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 7358 | // Check for a previous extern "C" declaration with this name. |
7359 | if (!Redeclaration && | ||||
7360 | checkForConflictWithNonVisibleExternC(*this, NewFD, Previous)) { | ||||
7361 | filterNonConflictingPreviousDecls(Context, NewFD, Previous); | ||||
7362 | if (!Previous.empty()) { | ||||
7363 | // This is an extern "C" declaration with the same name as a previous | ||||
7364 | // declaration, and thus redeclares that entity... | ||||
7365 | Redeclaration = true; | ||||
7366 | OldDecl = Previous.getFoundDecl(); | ||||
Richard Smith | dd9459f | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 7367 | MergeTypeWithPrevious = false; |
Richard Smith | aa4bc18 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 7368 | |
7369 | // ... except in the presence of __attribute__((overloadable)). | ||||
7370 | if (OldDecl->hasAttr<OverloadableAttr>()) { | ||||
7371 | if (!getLangOpts().CPlusPlus && !NewFD->hasAttr<OverloadableAttr>()) { | ||||
7372 | Diag(NewFD->getLocation(), diag::err_attribute_overloadable_missing) | ||||
7373 | << Redeclaration << NewFD; | ||||
7374 | Diag(Previous.getFoundDecl()->getLocation(), | ||||
7375 | diag::note_attribute_overloadable_prev_overload); | ||||
7376 | NewFD->addAttr(::new (Context) OverloadableAttr(SourceLocation(), | ||||
7377 | Context)); | ||||
7378 | } | ||||
7379 | if (IsOverload(NewFD, cast<FunctionDecl>(OldDecl), false)) { | ||||
7380 | Redeclaration = false; | ||||
7381 | OldDecl = 0; | ||||
7382 | } | ||||
7383 | } | ||||
7384 | } | ||||
7385 | } | ||||
7386 | |||||
Richard Smith | 21c8fa8 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 7387 | // C++11 [dcl.constexpr]p8: |
7388 | // A constexpr specifier for a non-static member function that is not | ||||
7389 | // a constructor declares that member function to be const. | ||||
7390 | // | ||||
7391 | // This needs to be delayed until we know whether this is an out-of-line | ||||
7392 | // definition of a static member function. | ||||
Richard Smith | 8404626 | 2013-04-21 01:08:50 +0000 | [diff] [blame] | 7393 | // |
7394 | // This rule is not present in C++1y, so we produce a backwards | ||||
7395 | // compatibility warning whenever it happens in C++11. | ||||
Richard Smith | 21c8fa8 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 7396 | CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(NewFD); |
Richard Smith | 8404626 | 2013-04-21 01:08:50 +0000 | [diff] [blame] | 7397 | if (!getLangOpts().CPlusPlus1y && MD && MD->isConstexpr() && |
7398 | !MD->isStatic() && !isa<CXXConstructorDecl>(MD) && | ||||
Richard Smith | 21c8fa8 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 7399 | (MD->getTypeQualifiers() & Qualifiers::Const) == 0) { |
7400 | CXXMethodDecl *OldMD = dyn_cast_or_null<CXXMethodDecl>(OldDecl); | ||||
7401 | if (FunctionTemplateDecl *OldTD = | ||||
7402 | dyn_cast_or_null<FunctionTemplateDecl>(OldDecl)) | ||||
7403 | OldMD = dyn_cast<CXXMethodDecl>(OldTD->getTemplatedDecl()); | ||||
7404 | if (!OldMD || !OldMD->isStatic()) { | ||||
7405 | const FunctionProtoType *FPT = | ||||
7406 | MD->getType()->castAs<FunctionProtoType>(); | ||||
7407 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); | ||||
7408 | EPI.TypeQuals |= Qualifiers::Const; | ||||
7409 | MD->setType(Context.getFunctionType(FPT->getResultType(), | ||||
Reid Kleckner | 0567a79 | 2013-06-10 20:51:09 +0000 | [diff] [blame] | 7410 | FPT->getArgTypes(), EPI)); |
Richard Smith | 8404626 | 2013-04-21 01:08:50 +0000 | [diff] [blame] | 7411 | |
7412 | // Warn that we did this, if we're not performing template instantiation. | ||||
7413 | // In that case, we'll have warned already when the template was defined. | ||||
7414 | if (ActiveTemplateInstantiations.empty()) { | ||||
7415 | SourceLocation AddConstLoc; | ||||
7416 | if (FunctionTypeLoc FTL = MD->getTypeSourceInfo()->getTypeLoc() | ||||
7417 | .IgnoreParens().getAs<FunctionTypeLoc>()) | ||||
7418 | AddConstLoc = PP.getLocForEndOfToken(FTL.getRParenLoc()); | ||||
7419 | |||||
7420 | Diag(MD->getLocation(), diag::warn_cxx1y_compat_constexpr_not_const) | ||||
7421 | << FixItHint::CreateInsertion(AddConstLoc, " const"); | ||||
7422 | } | ||||
Richard Smith | 21c8fa8 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 7423 | } |
7424 | } | ||||
7425 | |||||
7426 | if (Redeclaration) { | ||||
7427 | // NewFD and OldDecl represent declarations that need to be | ||||
7428 | // merged. | ||||
Richard Smith | dd9459f | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 7429 | if (MergeFunctionDecl(NewFD, OldDecl, S, MergeTypeWithPrevious)) { |
Richard Smith | 21c8fa8 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 7430 | NewFD->setInvalidDecl(); |
7431 | return Redeclaration; | ||||
7432 | } | ||||
7433 | |||||
7434 | Previous.clear(); | ||||
7435 | Previous.addDecl(OldDecl); | ||||
7436 | |||||
7437 | if (FunctionTemplateDecl *OldTemplateDecl | ||||
7438 | = dyn_cast<FunctionTemplateDecl>(OldDecl)) { | ||||
7439 | NewFD->setPreviousDeclaration(OldTemplateDecl->getTemplatedDecl()); | ||||
7440 | FunctionTemplateDecl *NewTemplateDecl | ||||
7441 | = NewFD->getDescribedFunctionTemplate(); | ||||
7442 | assert(NewTemplateDecl && "Template/non-template mismatch"); | ||||
7443 | if (CXXMethodDecl *Method | ||||
7444 | = dyn_cast<CXXMethodDecl>(NewTemplateDecl->getTemplatedDecl())) { | ||||
7445 | Method->setAccess(OldTemplateDecl->getAccess()); | ||||
7446 | NewTemplateDecl->setAccess(OldTemplateDecl->getAccess()); | ||||
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 7447 | } |
Richard Smith | 21c8fa8 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 7448 | |
7449 | // If this is an explicit specialization of a member that is a function | ||||
7450 | // template, mark it as a member specialization. | ||||
7451 | if (IsExplicitSpecialization && | ||||
7452 | NewTemplateDecl->getInstantiatedFromMemberTemplate()) { | ||||
7453 | NewTemplateDecl->setMemberSpecialization(); | ||||
7454 | assert(OldTemplateDecl->isMemberSpecialization()); | ||||
Argyrios Kyrtzidis | 9bedef6 | 2009-07-14 03:18:53 +0000 | [diff] [blame] | 7455 | } |
Richard Smith | 21c8fa8 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 7456 | |
7457 | } else { | ||||
John McCall | d5617ee | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 7458 | // This needs to happen first so that 'inline' propagates. |
Richard Smith | 21c8fa8 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 7459 | NewFD->setPreviousDeclaration(cast<FunctionDecl>(OldDecl)); |
John McCall | d5617ee | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 7460 | |
7461 | if (isa<CXXMethodDecl>(NewFD)) { | ||||
7462 | // A valid redeclaration of a C++ method must be out-of-line, | ||||
7463 | // but (unfortunately) it's not necessarily a definition | ||||
7464 | // because of templates, which means that the previous | ||||
7465 | // declaration is not necessarily from the class definition. | ||||
7466 | |||||
7467 | // For just setting the access, that doesn't matter. | ||||
7468 | CXXMethodDecl *oldMethod = cast<CXXMethodDecl>(OldDecl); | ||||
7469 | NewFD->setAccess(oldMethod->getAccess()); | ||||
7470 | |||||
7471 | // Update the key-function state if necessary for this ABI. | ||||
7472 | if (NewFD->isInlined() && | ||||
7473 | !Context.getTargetInfo().getCXXABI().canKeyFunctionBeInline()) { | ||||
7474 | // setNonKeyFunction needs to work with the original | ||||
7475 | // declaration from the class definition, and isVirtual() is | ||||
7476 | // just faster in that case, so map back to that now. | ||||
7477 | oldMethod = cast<CXXMethodDecl>(oldMethod->getFirstDeclaration()); | ||||
7478 | if (oldMethod->isVirtual()) { | ||||
7479 | Context.setNonKeyFunction(oldMethod); | ||||
7480 | } | ||||
7481 | } | ||||
7482 | } | ||||
Zhongxing Xu | 416fcaf | 2009-01-16 01:13:29 +0000 | [diff] [blame] | 7483 | } |
Douglas Gregor | 4ce205f | 2009-02-06 17:46:57 +0000 | [diff] [blame] | 7484 | } |
Zhongxing Xu | 416fcaf | 2009-01-16 01:13:29 +0000 | [diff] [blame] | 7485 | |
Anders Carlsson | 2c59d3c | 2009-09-13 21:33:06 +0000 | [diff] [blame] | 7486 | // Semantic checking for this function declaration (in isolation). |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7487 | if (getLangOpts().CPlusPlus) { |
Anders Carlsson | 2c59d3c | 2009-09-13 21:33:06 +0000 | [diff] [blame] | 7488 | // C++-specific checks. |
7489 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(NewFD)) { | ||||
7490 | CheckConstructor(Constructor); | ||||
Anders Carlsson | 6d70139 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 7491 | } else if (CXXDestructorDecl *Destructor = |
7492 | dyn_cast<CXXDestructorDecl>(NewFD)) { | ||||
7493 | CXXRecordDecl *Record = Destructor->getParent(); | ||||
Anders Carlsson | 2c59d3c | 2009-09-13 21:33:06 +0000 | [diff] [blame] | 7494 | QualType ClassType = Context.getTypeDeclType(Record); |
Anders Carlsson | 6d70139 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 7495 | |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 7496 | // 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] | 7497 | // type is dependent? Both gcc and edg can handle that. |
Anders Carlsson | 2c59d3c | 2009-09-13 21:33:06 +0000 | [diff] [blame] | 7498 | if (!ClassType->isDependentType()) { |
7499 | DeclarationName Name | ||||
7500 | = Context.DeclarationNames.getCXXDestructorName( | ||||
7501 | Context.getCanonicalType(ClassType)); | ||||
7502 | if (NewFD->getDeclName() != Name) { | ||||
7503 | Diag(NewFD->getLocation(), diag::err_destructor_name); | ||||
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 7504 | NewFD->setInvalidDecl(); |
7505 | return Redeclaration; | ||||
Anders Carlsson | 2c59d3c | 2009-09-13 21:33:06 +0000 | [diff] [blame] | 7506 | } |
7507 | } | ||||
Anders Carlsson | 2c59d3c | 2009-09-13 21:33:06 +0000 | [diff] [blame] | 7508 | } else if (CXXConversionDecl *Conversion |
Douglas Gregor | 4ba3136 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 7509 | = dyn_cast<CXXConversionDecl>(NewFD)) { |
Anders Carlsson | 2c59d3c | 2009-09-13 21:33:06 +0000 | [diff] [blame] | 7510 | ActOnConversionDeclarator(Conversion); |
Douglas Gregor | 4ba3136 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 7511 | } |
7512 | |||||
7513 | // Find any virtual functions that this function overrides. | ||||
Douglas Gregor | e6342c0 | 2009-12-01 17:35:23 +0000 | [diff] [blame] | 7514 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(NewFD)) { |
7515 | if (!Method->isFunctionTemplateSpecialization() && | ||||
Argyrios Kyrtzidis | 38eb1e1 | 2012-10-09 01:23:45 +0000 | [diff] [blame] | 7516 | !Method->getDescribedFunctionTemplate() && |
7517 | Method->isCanonicalDecl()) { | ||||
Douglas Gregor | a6c1e3a | 2010-10-13 22:55:32 +0000 | [diff] [blame] | 7518 | if (AddOverriddenMethods(Method->getParent(), Method)) { |
7519 | // If the function was marked as "static", we have a problem. | ||||
7520 | if (NewFD->getStorageClass() == SC_Static) { | ||||
David Blaikie | 5708c18 | 2012-10-17 00:47:58 +0000 | [diff] [blame] | 7521 | ReportOverrides(*this, diag::err_static_overrides_virtual, Method); |
Douglas Gregor | a6c1e3a | 2010-10-13 22:55:32 +0000 | [diff] [blame] | 7522 | } |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 7523 | } |
Douglas Gregor | a6c1e3a | 2010-10-13 22:55:32 +0000 | [diff] [blame] | 7524 | } |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 7525 | |
7526 | if (Method->isStatic()) | ||||
7527 | checkThisInStaticMemberFunctionType(Method); | ||||
Douglas Gregor | e6342c0 | 2009-12-01 17:35:23 +0000 | [diff] [blame] | 7528 | } |
Anders Carlsson | 2c59d3c | 2009-09-13 21:33:06 +0000 | [diff] [blame] | 7529 | |
7530 | // Extra checking for C++ overloaded operators (C++ [over.oper]). | ||||
7531 | if (NewFD->isOverloadedOperator() && | ||||
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 7532 | CheckOverloadedOperatorDeclaration(NewFD)) { |
7533 | NewFD->setInvalidDecl(); | ||||
7534 | return Redeclaration; | ||||
7535 | } | ||||
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 7536 | |
7537 | // Extra checking for C++0x literal operators (C++0x [over.literal]). | ||||
7538 | if (NewFD->getLiteralIdentifier() && | ||||
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 7539 | CheckLiteralOperatorDeclaration(NewFD)) { |
7540 | NewFD->setInvalidDecl(); | ||||
7541 | return Redeclaration; | ||||
7542 | } | ||||
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 7543 | |
Anders Carlsson | 2c59d3c | 2009-09-13 21:33:06 +0000 | [diff] [blame] | 7544 | // In C++, check default arguments now that we have merged decls. Unless |
7545 | // the lexical context is the class, because in this case this is done | ||||
7546 | // during delayed parsing anyway. | ||||
7547 | if (!CurContext->isRecord()) | ||||
7548 | CheckCXXDefaultArguments(NewFD); | ||||
Douglas Gregor | b68e399 | 2010-12-21 19:47:46 +0000 | [diff] [blame] | 7549 | |
7550 | // If this function declares a builtin function, check the type of this | ||||
7551 | // declaration against the expected type for the builtin. | ||||
7552 | if (unsigned BuiltinID = NewFD->getBuiltinID()) { | ||||
7553 | ASTContext::GetBuiltinTypeError Error; | ||||
Fariborz Jahanian | 9ef1518 | 2013-01-05 21:54:55 +0000 | [diff] [blame] | 7554 | LookupPredefedObjCSuperType(*this, S, NewFD->getIdentifier()); |
Douglas Gregor | b68e399 | 2010-12-21 19:47:46 +0000 | [diff] [blame] | 7555 | QualType T = Context.GetBuiltinType(BuiltinID, Error); |
7556 | if (!T.isNull() && !Context.hasSameType(T, NewFD->getType())) { | ||||
7557 | // The type of this function differs from the type of the builtin, | ||||
7558 | // so forget about the builtin entirely. | ||||
7559 | Context.BuiltinInfo.ForgetBuiltin(BuiltinID, Context.Idents); | ||||
7560 | } | ||||
7561 | } | ||||
Aaron Ballman | 2c0bf24 | 2012-02-09 01:21:34 +0000 | [diff] [blame] | 7562 | |
7563 | // If this function is declared as being extern "C", then check to see if | ||||
7564 | // the function returns a UDT (class, struct, or union type) that is not C | ||||
7565 | // compatible, and if it does, warn the user. | ||||
Fariborz Jahanian | 96db329 | 2013-03-14 23:09:00 +0000 | [diff] [blame] | 7566 | // But, issue any diagnostic on the first declaration only. |
7567 | if (NewFD->isExternC() && Previous.empty()) { | ||||
Aaron Ballman | 2c0bf24 | 2012-02-09 01:21:34 +0000 | [diff] [blame] | 7568 | QualType R = NewFD->getResultType(); |
Hans Wennborg | 168c07b | 2012-07-24 17:59:41 +0000 | [diff] [blame] | 7569 | if (R->isIncompleteType() && !R->isVoidType()) |
7570 | Diag(NewFD->getLocation(), diag::warn_return_value_udt_incomplete) | ||||
7571 | << NewFD << R; | ||||
Douglas Gregor | b38b491 | 2012-08-07 06:14:34 +0000 | [diff] [blame] | 7572 | else if (!R.isPODType(Context) && !R->isVoidType() && |
7573 | !R->isObjCObjectPointerType()) | ||||
Hans Wennborg | 168c07b | 2012-07-24 17:59:41 +0000 | [diff] [blame] | 7574 | Diag(NewFD->getLocation(), diag::warn_return_value_udt) << NewFD << R; |
Aaron Ballman | 2c0bf24 | 2012-02-09 01:21:34 +0000 | [diff] [blame] | 7575 | } |
Anders Carlsson | 2c59d3c | 2009-09-13 21:33:06 +0000 | [diff] [blame] | 7576 | } |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 7577 | return Redeclaration; |
Zhongxing Xu | 416fcaf | 2009-01-16 01:13:29 +0000 | [diff] [blame] | 7578 | } |
7579 | |||||
Dmitri Gribenko | a6f9707 | 2013-01-17 00:26:13 +0000 | [diff] [blame] | 7580 | static SourceRange getResultSourceRange(const FunctionDecl *FD) { |
7581 | const TypeSourceInfo *TSI = FD->getTypeSourceInfo(); | ||||
7582 | if (!TSI) | ||||
7583 | return SourceRange(); | ||||
7584 | |||||
7585 | TypeLoc TL = TSI->getTypeLoc(); | ||||
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 7586 | FunctionTypeLoc FunctionTL = TL.getAs<FunctionTypeLoc>(); |
Dmitri Gribenko | a6f9707 | 2013-01-17 00:26:13 +0000 | [diff] [blame] | 7587 | if (!FunctionTL) |
7588 | return SourceRange(); | ||||
7589 | |||||
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 7590 | TypeLoc ResultTL = FunctionTL.getResultLoc(); |
7591 | if (ResultTL.getUnqualifiedLoc().getAs<BuiltinTypeLoc>()) | ||||
Dmitri Gribenko | a6f9707 | 2013-01-17 00:26:13 +0000 | [diff] [blame] | 7592 | return ResultTL.getSourceRange(); |
7593 | |||||
7594 | return SourceRange(); | ||||
7595 | } | ||||
7596 | |||||
David Blaikie | 14068e8 | 2011-09-08 06:33:04 +0000 | [diff] [blame] | 7597 | void Sema::CheckMain(FunctionDecl* FD, const DeclSpec& DS) { |
Richard Smith | a506586 | 2012-02-04 06:10:17 +0000 | [diff] [blame] | 7598 | // C++11 [basic.start.main]p3: A program that declares main to be inline, |
7599 | // static or constexpr is ill-formed. | ||||
Richard Smith | de03c15 | 2013-01-17 22:16:11 +0000 | [diff] [blame] | 7600 | // C11 6.7.4p4: In a hosted environment, no function specifier(s) shall |
7601 | // appear in a declaration of main. | ||||
John McCall | 13591ed | 2009-07-25 04:36:53 +0000 | [diff] [blame] | 7602 | // 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] | 7603 | // We accept _Noreturn main as an extension. |
David Blaikie | 14068e8 | 2011-09-08 06:33:04 +0000 | [diff] [blame] | 7604 | if (FD->getStorageClass() == SC_Static) |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7605 | Diag(DS.getStorageClassSpecLoc(), getLangOpts().CPlusPlus |
David Blaikie | 14068e8 | 2011-09-08 06:33:04 +0000 | [diff] [blame] | 7606 | ? diag::err_static_main : diag::warn_static_main) |
7607 | << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc()); | ||||
7608 | if (FD->isInlineSpecified()) | ||||
7609 | Diag(DS.getInlineSpecLoc(), diag::err_inline_main) | ||||
7610 | << FixItHint::CreateRemoval(DS.getInlineSpecLoc()); | ||||
Dmitri Gribenko | 445743d | 2013-01-21 11:25:03 +0000 | [diff] [blame] | 7611 | if (DS.isNoreturnSpecified()) { |
7612 | SourceLocation NoreturnLoc = DS.getNoreturnSpecLoc(); | ||||
7613 | SourceRange NoreturnRange(NoreturnLoc, | ||||
7614 | PP.getLocForEndOfToken(NoreturnLoc)); | ||||
7615 | Diag(NoreturnLoc, diag::ext_noreturn_main); | ||||
7616 | Diag(NoreturnLoc, diag::note_main_remove_noreturn) | ||||
7617 | << FixItHint::CreateRemoval(NoreturnRange); | ||||
7618 | } | ||||
Richard Smith | a506586 | 2012-02-04 06:10:17 +0000 | [diff] [blame] | 7619 | if (FD->isConstexpr()) { |
7620 | Diag(DS.getConstexprSpecLoc(), diag::err_constexpr_main) | ||||
7621 | << FixItHint::CreateRemoval(DS.getConstexprSpecLoc()); | ||||
7622 | FD->setConstexpr(false); | ||||
7623 | } | ||||
John McCall | 13591ed | 2009-07-25 04:36:53 +0000 | [diff] [blame] | 7624 | |
7625 | QualType T = FD->getType(); | ||||
7626 | assert(T->isFunctionType() && "function decl is not of function type"); | ||||
John McCall | 75d8ba3 | 2012-02-14 19:50:52 +0000 | [diff] [blame] | 7627 | const FunctionType* FT = T->castAs<FunctionType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7628 | |
John McCall | 75d8ba3 | 2012-02-14 19:50:52 +0000 | [diff] [blame] | 7629 | // All the standards say that main() should should return 'int'. |
7630 | if (Context.hasSameUnqualifiedType(FT->getResultType(), Context.IntTy)) { | ||||
7631 | // In C and C++, main magically returns 0 if you fall off the end; | ||||
7632 | // set the flag which tells us that. | ||||
7633 | // This is C++ [basic.start.main]p5 and C99 5.1.2.2.3. | ||||
7634 | FD->setHasImplicitReturnZero(true); | ||||
7635 | |||||
7636 | // In C with GNU extensions we allow main() to have non-integer return | ||||
7637 | // type, but we should warn about the extension, and we disable the | ||||
7638 | // implicit-return-zero rule. | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7639 | } else if (getLangOpts().GNUMode && !getLangOpts().CPlusPlus) { |
John McCall | 75d8ba3 | 2012-02-14 19:50:52 +0000 | [diff] [blame] | 7640 | Diag(FD->getTypeSpecStartLoc(), diag::ext_main_returns_nonint); |
7641 | |||||
Dmitri Gribenko | a6f9707 | 2013-01-17 00:26:13 +0000 | [diff] [blame] | 7642 | SourceRange ResultRange = getResultSourceRange(FD); |
7643 | if (ResultRange.isValid()) | ||||
7644 | Diag(ResultRange.getBegin(), diag::note_main_change_return_type) | ||||
7645 | << FixItHint::CreateReplacement(ResultRange, "int"); | ||||
7646 | |||||
John McCall | 75d8ba3 | 2012-02-14 19:50:52 +0000 | [diff] [blame] | 7647 | // Otherwise, this is just a flat-out error. |
7648 | } else { | ||||
Dmitri Gribenko | a6f9707 | 2013-01-17 00:26:13 +0000 | [diff] [blame] | 7649 | SourceRange ResultRange = getResultSourceRange(FD); |
7650 | if (ResultRange.isValid()) | ||||
7651 | Diag(FD->getTypeSpecStartLoc(), diag::err_main_returns_nonint) | ||||
7652 | << FixItHint::CreateReplacement(ResultRange, "int"); | ||||
7653 | else | ||||
7654 | Diag(FD->getTypeSpecStartLoc(), diag::err_main_returns_nonint); | ||||
7655 | |||||
John McCall | 13591ed | 2009-07-25 04:36:53 +0000 | [diff] [blame] | 7656 | FD->setInvalidDecl(true); |
7657 | } | ||||
7658 | |||||
7659 | // Treat protoless main() as nullary. | ||||
7660 | if (isa<FunctionNoProtoType>(FT)) return; | ||||
7661 | |||||
7662 | const FunctionProtoType* FTP = cast<const FunctionProtoType>(FT); | ||||
7663 | unsigned nparams = FTP->getNumArgs(); | ||||
7664 | assert(FD->getNumParams() == nparams); | ||||
7665 | |||||
John McCall | 6675586 | 2009-12-24 09:58:38 +0000 | [diff] [blame] | 7666 | bool HasExtraParameters = (nparams > 3); |
7667 | |||||
7668 | // Darwin passes an undocumented fourth argument of type char**. If | ||||
7669 | // other platforms start sprouting these, the logic below will start | ||||
7670 | // getting shifty. | ||||
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 7671 | if (nparams == 4 && Context.getTargetInfo().getTriple().isOSDarwin()) |
John McCall | 6675586 | 2009-12-24 09:58:38 +0000 | [diff] [blame] | 7672 | HasExtraParameters = false; |
7673 | |||||
7674 | if (HasExtraParameters) { | ||||
John McCall | 13591ed | 2009-07-25 04:36:53 +0000 | [diff] [blame] | 7675 | Diag(FD->getLocation(), diag::err_main_surplus_args) << nparams; |
7676 | FD->setInvalidDecl(true); | ||||
7677 | nparams = 3; | ||||
7678 | } | ||||
7679 | |||||
7680 | // FIXME: a lot of the following diagnostics would be improved | ||||
7681 | // if we had some location information about types. | ||||
7682 | |||||
7683 | QualType CharPP = | ||||
7684 | Context.getPointerType(Context.getPointerType(Context.CharTy)); | ||||
John McCall | 6675586 | 2009-12-24 09:58:38 +0000 | [diff] [blame] | 7685 | QualType Expected[] = { Context.IntTy, CharPP, CharPP, CharPP }; |
John McCall | 13591ed | 2009-07-25 04:36:53 +0000 | [diff] [blame] | 7686 | |
7687 | for (unsigned i = 0; i < nparams; ++i) { | ||||
7688 | QualType AT = FTP->getArgType(i); | ||||
7689 | |||||
7690 | bool mismatch = true; | ||||
7691 | |||||
7692 | if (Context.hasSameUnqualifiedType(AT, Expected[i])) | ||||
7693 | mismatch = false; | ||||
7694 | else if (Expected[i] == CharPP) { | ||||
7695 | // As an extension, the following forms are okay: | ||||
7696 | // char const ** | ||||
7697 | // char const * const * | ||||
7698 | // char * const * | ||||
7699 | |||||
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 7700 | QualifierCollector qs; |
John McCall | 13591ed | 2009-07-25 04:36:53 +0000 | [diff] [blame] | 7701 | const PointerType* PT; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 7702 | if ((PT = qs.strip(AT)->getAs<PointerType>()) && |
7703 | (PT = qs.strip(PT->getPointeeType())->getAs<PointerType>()) && | ||||
Richard Smith | 485b312 | 2013-01-29 02:49:47 +0000 | [diff] [blame] | 7704 | Context.hasSameType(QualType(qs.strip(PT->getPointeeType()), 0), |
7705 | Context.CharTy)) { | ||||
John McCall | 13591ed | 2009-07-25 04:36:53 +0000 | [diff] [blame] | 7706 | qs.removeConst(); |
7707 | mismatch = !qs.empty(); | ||||
7708 | } | ||||
7709 | } | ||||
7710 | |||||
7711 | if (mismatch) { | ||||
7712 | Diag(FD->getLocation(), diag::err_main_arg_wrong) << i << Expected[i]; | ||||
7713 | // TODO: suggest replacing given type with expected type | ||||
7714 | FD->setInvalidDecl(true); | ||||
7715 | } | ||||
7716 | } | ||||
7717 | |||||
7718 | if (nparams == 1 && !FD->isInvalidDecl()) { | ||||
7719 | Diag(FD->getLocation(), diag::warn_main_one_arg); | ||||
7720 | } | ||||
Douglas Gregor | 0bab54c | 2010-10-21 16:57:46 +0000 | [diff] [blame] | 7721 | |
7722 | if (!FD->isInvalidDecl() && FD->getDescribedFunctionTemplate()) { | ||||
David Majnemer | e9f6f33 | 2013-09-16 22:44:20 +0000 | [diff] [blame] | 7723 | Diag(FD->getLocation(), diag::err_mainlike_template_decl) << FD->getName(); |
7724 | FD->setInvalidDecl(); | ||||
7725 | } | ||||
7726 | } | ||||
7727 | |||||
7728 | void Sema::CheckMSVCRTEntryPoint(FunctionDecl *FD) { | ||||
7729 | QualType T = FD->getType(); | ||||
7730 | assert(T->isFunctionType() && "function decl is not of function type"); | ||||
7731 | const FunctionType *FT = T->castAs<FunctionType>(); | ||||
7732 | |||||
7733 | // Set an implicit return of 'zero' if the function can return some integral, | ||||
7734 | // enumeration, pointer or nullptr type. | ||||
7735 | if (FT->getResultType()->isIntegralOrEnumerationType() || | ||||
7736 | FT->getResultType()->isAnyPointerType() || | ||||
7737 | FT->getResultType()->isNullPtrType()) | ||||
7738 | // DllMain is exempt because a return value of zero means it failed. | ||||
7739 | if (FD->getName() != "DllMain") | ||||
7740 | FD->setHasImplicitReturnZero(true); | ||||
7741 | |||||
7742 | if (!FD->isInvalidDecl() && FD->getDescribedFunctionTemplate()) { | ||||
7743 | Diag(FD->getLocation(), diag::err_mainlike_template_decl) << FD->getName(); | ||||
Douglas Gregor | 0bab54c | 2010-10-21 16:57:46 +0000 | [diff] [blame] | 7744 | FD->setInvalidDecl(); |
7745 | } | ||||
John McCall | 8c4859a | 2009-07-24 03:03:21 +0000 | [diff] [blame] | 7746 | } |
7747 | |||||
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 7748 | bool Sema::CheckForConstantInitializer(Expr *Init, QualType DclT) { |
Eli Friedman | 3b8a36a | 2009-02-27 04:17:12 +0000 | [diff] [blame] | 7749 | // FIXME: Need strict checking. In C89, we need to check for |
7750 | // any assignment, increment, decrement, function-calls, or | ||||
7751 | // commas outside of a sizeof. In C99, it's the same list, | ||||
7752 | // except that the aforementioned are allowed in unevaluated | ||||
7753 | // expressions. Everything else falls under the | ||||
7754 | // "may accept other forms of constant expressions" exception. | ||||
7755 | // (We never end up here for C++, so the constant expression | ||||
7756 | // rules there don't matter.) | ||||
John McCall | 4204f07 | 2010-08-02 21:13:48 +0000 | [diff] [blame] | 7757 | if (Init->isConstantInitializer(Context, false)) |
Eli Friedman | 578a972 | 2009-02-22 06:45:27 +0000 | [diff] [blame] | 7758 | return false; |
Eli Friedman | 2129828 | 2009-02-26 04:47:58 +0000 | [diff] [blame] | 7759 | Diag(Init->getExprLoc(), diag::err_init_element_not_constant) |
7760 | << Init->getSourceRange(); | ||||
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 7761 | return true; |
Steve Naroff | d0091aa | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 7762 | } |
7763 | |||||
Chandler Carruth | a7689ef | 2011-03-27 09:46:56 +0000 | [diff] [blame] | 7764 | namespace { |
7765 | // Visits an initialization expression to see if OrigDecl is evaluated in | ||||
7766 | // its own initialization and throws a warning if it does. | ||||
7767 | class SelfReferenceChecker | ||||
7768 | : public EvaluatedExprVisitor<SelfReferenceChecker> { | ||||
7769 | Sema &S; | ||||
7770 | Decl *OrigDecl; | ||||
Richard Trieu | 898267f | 2011-09-01 21:44:13 +0000 | [diff] [blame] | 7771 | bool isRecordType; |
7772 | bool isPODType; | ||||
Hans Wennborg | 8be9e77 | 2012-08-17 10:12:33 +0000 | [diff] [blame] | 7773 | bool isReferenceType; |
Chandler Carruth | a7689ef | 2011-03-27 09:46:56 +0000 | [diff] [blame] | 7774 | |
7775 | public: | ||||
7776 | typedef EvaluatedExprVisitor<SelfReferenceChecker> Inherited; | ||||
7777 | |||||
7778 | SelfReferenceChecker(Sema &S, Decl *OrigDecl) : Inherited(S.Context), | ||||
Richard Trieu | 898267f | 2011-09-01 21:44:13 +0000 | [diff] [blame] | 7779 | S(S), OrigDecl(OrigDecl) { |
7780 | isPODType = false; | ||||
7781 | isRecordType = false; | ||||
Hans Wennborg | 8be9e77 | 2012-08-17 10:12:33 +0000 | [diff] [blame] | 7782 | isReferenceType = false; |
Richard Trieu | 898267f | 2011-09-01 21:44:13 +0000 | [diff] [blame] | 7783 | if (ValueDecl *VD = dyn_cast<ValueDecl>(OrigDecl)) { |
7784 | isPODType = VD->getType().isPODType(S.Context); | ||||
7785 | isRecordType = VD->getType()->isRecordType(); | ||||
Hans Wennborg | 8be9e77 | 2012-08-17 10:12:33 +0000 | [diff] [blame] | 7786 | isReferenceType = VD->getType()->isReferenceType(); |
Richard Trieu | 898267f | 2011-09-01 21:44:13 +0000 | [diff] [blame] | 7787 | } |
7788 | } | ||||
Chandler Carruth | a7689ef | 2011-03-27 09:46:56 +0000 | [diff] [blame] | 7789 | |
Richard Trieu | 7e9f8af | 2012-05-09 00:21:34 +0000 | [diff] [blame] | 7790 | // For most expressions, the cast is directly above the DeclRefExpr. |
7791 | // For conditional operators, the cast can be outside the conditional | ||||
7792 | // operator if both expressions are DeclRefExpr's. | ||||
7793 | void HandleValue(Expr *E) { | ||||
Richard Trieu | 568f785 | 2012-10-01 17:39:51 +0000 | [diff] [blame] | 7794 | if (isReferenceType) |
7795 | return; | ||||
Richard Trieu | 7e9f8af | 2012-05-09 00:21:34 +0000 | [diff] [blame] | 7796 | E = E->IgnoreParenImpCasts(); |
7797 | if (DeclRefExpr* DRE = dyn_cast<DeclRefExpr>(E)) { | ||||
7798 | HandleDeclRefExpr(DRE); | ||||
7799 | return; | ||||
7800 | } | ||||
7801 | |||||
7802 | if (ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E)) { | ||||
7803 | HandleValue(CO->getTrueExpr()); | ||||
7804 | HandleValue(CO->getFalseExpr()); | ||||
Richard Trieu | 6b2cc42 | 2012-10-03 00:41:36 +0000 | [diff] [blame] | 7805 | return; |
7806 | } | ||||
7807 | |||||
7808 | if (isa<MemberExpr>(E)) { | ||||
7809 | Expr *Base = E->IgnoreParenImpCasts(); | ||||
7810 | while (MemberExpr *ME = dyn_cast<MemberExpr>(Base)) { | ||||
7811 | // Check for static member variables and don't warn on them. | ||||
7812 | if (!isa<FieldDecl>(ME->getMemberDecl())) | ||||
7813 | return; | ||||
7814 | Base = ME->getBase()->IgnoreParenImpCasts(); | ||||
7815 | } | ||||
7816 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Base)) | ||||
7817 | HandleDeclRefExpr(DRE); | ||||
7818 | return; | ||||
Richard Trieu | 7e9f8af | 2012-05-09 00:21:34 +0000 | [diff] [blame] | 7819 | } |
7820 | } | ||||
7821 | |||||
Richard Trieu | 568f785 | 2012-10-01 17:39:51 +0000 | [diff] [blame] | 7822 | // Reference types are handled here since all uses of references are |
7823 | // bad, not just r-value uses. | ||||
7824 | void VisitDeclRefExpr(DeclRefExpr *E) { | ||||
7825 | if (isReferenceType) | ||||
7826 | HandleDeclRefExpr(E); | ||||
7827 | } | ||||
7828 | |||||
Richard Trieu | 7e9f8af | 2012-05-09 00:21:34 +0000 | [diff] [blame] | 7829 | void VisitImplicitCastExpr(ImplicitCastExpr *E) { |
Richard Trieu | 6b2cc42 | 2012-10-03 00:41:36 +0000 | [diff] [blame] | 7830 | if (E->getCastKind() == CK_LValueToRValue || |
Richard Trieu | 7e9f8af | 2012-05-09 00:21:34 +0000 | [diff] [blame] | 7831 | (isRecordType && E->getCastKind() == CK_NoOp)) |
7832 | HandleValue(E->getSubExpr()); | ||||
7833 | |||||
7834 | Inherited::VisitImplicitCastExpr(E); | ||||
Chandler Carruth | a7689ef | 2011-03-27 09:46:56 +0000 | [diff] [blame] | 7835 | } |
7836 | |||||
Richard Trieu | 898267f | 2011-09-01 21:44:13 +0000 | [diff] [blame] | 7837 | void VisitMemberExpr(MemberExpr *E) { |
Richard Trieu | 7e9f8af | 2012-05-09 00:21:34 +0000 | [diff] [blame] | 7838 | // Don't warn on arrays since they can be treated as pointers. |
Richard Trieu | 47eb898 | 2011-09-07 00:58:53 +0000 | [diff] [blame] | 7839 | if (E->getType()->canDecayToPointerType()) return; |
Richard Trieu | 7e9f8af | 2012-05-09 00:21:34 +0000 | [diff] [blame] | 7840 | |
Richard Trieu | 6b2cc42 | 2012-10-03 00:41:36 +0000 | [diff] [blame] | 7841 | // Warn when a non-static method call is followed by non-static member |
7842 | // field accesses, which is followed by a DeclRefExpr. | ||||
7843 | CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(E->getMemberDecl()); | ||||
7844 | bool Warn = (MD && !MD->isStatic()); | ||||
7845 | Expr *Base = E->getBase()->IgnoreParenImpCasts(); | ||||
7846 | while (MemberExpr *ME = dyn_cast<MemberExpr>(Base)) { | ||||
7847 | if (!isa<FieldDecl>(ME->getMemberDecl())) | ||||
7848 | Warn = false; | ||||
7849 | Base = ME->getBase()->IgnoreParenImpCasts(); | ||||
7850 | } | ||||
Richard Trieu | 898267f | 2011-09-01 21:44:13 +0000 | [diff] [blame] | 7851 | |
Richard Trieu | 6b2cc42 | 2012-10-03 00:41:36 +0000 | [diff] [blame] | 7852 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Base)) { |
7853 | if (Warn) | ||||
7854 | HandleDeclRefExpr(DRE); | ||||
7855 | return; | ||||
7856 | } | ||||
7857 | |||||
7858 | // The base of a MemberExpr is not a MemberExpr or a DeclRefExpr. | ||||
7859 | // Visit that expression. | ||||
7860 | Visit(Base); | ||||
Chandler Carruth | a7689ef | 2011-03-27 09:46:56 +0000 | [diff] [blame] | 7861 | } |
7862 | |||||
Richard Trieu | 8af742a | 2013-03-26 03:41:40 +0000 | [diff] [blame] | 7863 | void VisitCXXOperatorCallExpr(CXXOperatorCallExpr *E) { |
7864 | if (E->getNumArgs() > 0) | ||||
7865 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E->getArg(0))) | ||||
7866 | HandleDeclRefExpr(DRE); | ||||
7867 | |||||
7868 | Inherited::VisitCXXOperatorCallExpr(E); | ||||
7869 | } | ||||
7870 | |||||
Richard Trieu | 898267f | 2011-09-01 21:44:13 +0000 | [diff] [blame] | 7871 | void VisitUnaryOperator(UnaryOperator *E) { |
7872 | // For POD record types, addresses of its own members are well-defined. | ||||
Richard Trieu | 6b2cc42 | 2012-10-03 00:41:36 +0000 | [diff] [blame] | 7873 | if (E->getOpcode() == UO_AddrOf && isRecordType && |
7874 | isa<MemberExpr>(E->getSubExpr()->IgnoreParens())) { | ||||
7875 | if (!isPODType) | ||||
7876 | HandleValue(E->getSubExpr()); | ||||
7877 | return; | ||||
7878 | } | ||||
Richard Trieu | 898267f | 2011-09-01 21:44:13 +0000 | [diff] [blame] | 7879 | Inherited::VisitUnaryOperator(E); |
Richard Smith | 0f2fc5f | 2013-05-03 19:16:22 +0000 | [diff] [blame] | 7880 | } |
Richard Trieu | 7e9f8af | 2012-05-09 00:21:34 +0000 | [diff] [blame] | 7881 | |
7882 | void VisitObjCMessageExpr(ObjCMessageExpr *E) { return; } | ||||
7883 | |||||
Richard Trieu | 898267f | 2011-09-01 21:44:13 +0000 | [diff] [blame] | 7884 | void HandleDeclRefExpr(DeclRefExpr *DRE) { |
NAKAMURA Takumi | f305279 | 2013-01-19 01:54:35 +0000 | [diff] [blame] | 7885 | Decl* ReferenceDecl = DRE->getDecl(); |
Chandler Carruth | a7689ef | 2011-03-27 09:46:56 +0000 | [diff] [blame] | 7886 | if (OrigDecl != ReferenceDecl) return; |
Ted Kremenek | 39371b8 | 2013-01-19 04:33:14 +0000 | [diff] [blame] | 7887 | unsigned diag; |
7888 | if (isReferenceType) { | ||||
7889 | diag = diag::warn_uninit_self_reference_in_reference_init; | ||||
7890 | } else if (cast<VarDecl>(OrigDecl)->isStaticLocal()) { | ||||
7891 | diag = diag::warn_static_self_reference_in_init; | ||||
7892 | } else { | ||||
7893 | diag = diag::warn_uninit_self_reference_in_init; | ||||
7894 | } | ||||
7895 | |||||
Richard Trieu | 898267f | 2011-09-01 21:44:13 +0000 | [diff] [blame] | 7896 | S.DiagRuntimeBehavior(DRE->getLocStart(), DRE, |
Hans Wennborg | 5965b7c | 2012-08-20 08:52:22 +0000 | [diff] [blame] | 7897 | S.PDiag(diag) |
Hans Wennborg | 7821e07 | 2012-09-21 08:58:33 +0000 | [diff] [blame] | 7898 | << DRE->getNameInfo().getName() |
Douglas Gregor | 63fe681 | 2011-05-24 16:02:01 +0000 | [diff] [blame] | 7899 | << OrigDecl->getLocation() |
Richard Trieu | 898267f | 2011-09-01 21:44:13 +0000 | [diff] [blame] | 7900 | << DRE->getSourceRange()); |
Chandler Carruth | a7689ef | 2011-03-27 09:46:56 +0000 | [diff] [blame] | 7901 | } |
7902 | }; | ||||
Chandler Carruth | a7689ef | 2011-03-27 09:46:56 +0000 | [diff] [blame] | 7903 | |
Richard Trieu | 568f785 | 2012-10-01 17:39:51 +0000 | [diff] [blame] | 7904 | /// CheckSelfReference - Warns if OrigDecl is used in expression E. |
7905 | static void CheckSelfReference(Sema &S, Decl* OrigDecl, Expr *E, | ||||
7906 | bool DirectInit) { | ||||
7907 | // Parameters arguments are occassionially constructed with itself, | ||||
7908 | // for instance, in recursive functions. Skip them. | ||||
7909 | if (isa<ParmVarDecl>(OrigDecl)) | ||||
7910 | return; | ||||
7911 | |||||
7912 | E = E->IgnoreParens(); | ||||
7913 | |||||
7914 | // Skip checking T a = a where T is not a record or reference type. | ||||
7915 | // Doing so is a way to silence uninitialized warnings. | ||||
7916 | if (!DirectInit && !cast<VarDecl>(OrigDecl)->getType()->isRecordType()) | ||||
7917 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) | ||||
7918 | if (ICE->getCastKind() == CK_LValueToRValue) | ||||
7919 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr())) | ||||
7920 | if (DRE->getDecl() == OrigDecl) | ||||
7921 | return; | ||||
7922 | |||||
7923 | SelfReferenceChecker(S, OrigDecl).Visit(E); | ||||
7924 | } | ||||
Richard Trieu | 898267f | 2011-09-01 21:44:13 +0000 | [diff] [blame] | 7925 | } |
7926 | |||||
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 7927 | /// AddInitializerToDecl - Adds the initializer Init to the |
7928 | /// declaration dcl. If DirectInit is true, this is C++ direct | ||||
7929 | /// initialization rather than copy initialization. | ||||
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 7930 | void Sema::AddInitializerToDecl(Decl *RealDecl, Expr *Init, |
7931 | bool DirectInit, bool TypeMayContainAuto) { | ||||
Chris Lattner | 9a11b9a | 2007-10-19 20:10:30 +0000 | [diff] [blame] | 7932 | // If there is no declaration, there was an error parsing it. Just ignore |
7933 | // the initializer. | ||||
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 7934 | if (RealDecl == 0 || RealDecl->isInvalidDecl()) |
Chris Lattner | 9a11b9a | 2007-10-19 20:10:30 +0000 | [diff] [blame] | 7935 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7936 | |
Douglas Gregor | 021c3b3 | 2009-03-11 23:00:04 +0000 | [diff] [blame] | 7937 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(RealDecl)) { |
7938 | // With declarators parsed the way they are, the parser cannot | ||||
7939 | // distinguish between a normal initializer and a pure-specifier. | ||||
7940 | // Thus this grotesque test. | ||||
7941 | IntegerLiteral *IL; | ||||
Douglas Gregor | 021c3b3 | 2009-03-11 23:00:04 +0000 | [diff] [blame] | 7942 | if ((IL = dyn_cast<IntegerLiteral>(Init)) && IL->getValue() == 0 && |
Douglas Gregor | 4ba3136 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 7943 | Context.getCanonicalType(IL->getType()) == Context.IntTy) |
7944 | CheckPureMethod(Method, Init->getSourceRange()); | ||||
7945 | else { | ||||
Douglas Gregor | 021c3b3 | 2009-03-11 23:00:04 +0000 | [diff] [blame] | 7946 | Diag(Method->getLocation(), diag::err_member_function_initialization) |
7947 | << Method->getDeclName() << Init->getSourceRange(); | ||||
7948 | Method->setInvalidDecl(); | ||||
7949 | } | ||||
7950 | return; | ||||
7951 | } | ||||
7952 | |||||
Steve Naroff | 410e3e2 | 2007-09-12 20:13:48 +0000 | [diff] [blame] | 7953 | VarDecl *VDecl = dyn_cast<VarDecl>(RealDecl); |
7954 | if (!VDecl) { | ||||
Richard Smith | c2cdd53 | 2011-06-12 11:43:46 +0000 | [diff] [blame] | 7955 | assert(!isa<FieldDecl>(RealDecl) && "field init shouldn't get here"); |
7956 | Diag(RealDecl->getLocation(), diag::err_illegal_initializer); | ||||
Steve Naroff | 410e3e2 | 2007-09-12 20:13:48 +0000 | [diff] [blame] | 7957 | RealDecl->setInvalidDecl(); |
7958 | return; | ||||
Eli Friedman | 3b8a36a | 2009-02-27 04:17:12 +0000 | [diff] [blame] | 7959 | } |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 7960 | ParenListExpr *CXXDirectInit = dyn_cast<ParenListExpr>(Init); |
7961 | |||||
Richard Smith | 0188872 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 7962 | // 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] | 7963 | if (TypeMayContainAuto && VDecl->getType()->isUndeducedType()) { |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 7964 | Expr *DeduceInit = Init; |
7965 | // Initializer could be a C++ direct-initializer. Deduction only works if it | ||||
7966 | // contains exactly one expression. | ||||
7967 | if (CXXDirectInit) { | ||||
7968 | if (CXXDirectInit->getNumExprs() == 0) { | ||||
7969 | // It isn't possible to write this directly, but it is possible to | ||||
7970 | // end up in this situation with "auto x(some_pack...);" | ||||
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 7971 | Diag(CXXDirectInit->getLocStart(), |
Richard Smith | 04fa7a3 | 2013-09-28 04:02:39 +0000 | [diff] [blame] | 7972 | VDecl->isInitCapture() ? diag::err_init_capture_no_expression |
7973 | : diag::err_auto_var_init_no_expression) | ||||
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 7974 | << VDecl->getDeclName() << VDecl->getType() |
7975 | << VDecl->getSourceRange(); | ||||
7976 | RealDecl->setInvalidDecl(); | ||||
7977 | return; | ||||
7978 | } else if (CXXDirectInit->getNumExprs() > 1) { | ||||
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 7979 | Diag(CXXDirectInit->getExpr(1)->getLocStart(), |
Richard Smith | 04fa7a3 | 2013-09-28 04:02:39 +0000 | [diff] [blame] | 7980 | VDecl->isInitCapture() |
7981 | ? diag::err_init_capture_multiple_expressions | ||||
7982 | : diag::err_auto_var_init_multiple_expressions) | ||||
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 7983 | << VDecl->getDeclName() << VDecl->getType() |
7984 | << VDecl->getSourceRange(); | ||||
7985 | RealDecl->setInvalidDecl(); | ||||
7986 | return; | ||||
7987 | } else { | ||||
7988 | DeduceInit = CXXDirectInit->getExpr(0); | ||||
7989 | } | ||||
7990 | } | ||||
Douglas Gregor | 1344e94 | 2013-03-07 22:57:58 +0000 | [diff] [blame] | 7991 | |
7992 | // Expressions default to 'id' when we're in a debugger. | ||||
7993 | bool DefaultedToAuto = false; | ||||
7994 | if (getLangOpts().DebuggerCastResultToId && | ||||
7995 | Init->getType() == Context.UnknownAnyTy) { | ||||
7996 | ExprResult Result = forceUnknownAnyToType(Init, Context.getObjCIdType()); | ||||
7997 | if (Result.isInvalid()) { | ||||
7998 | VDecl->setInvalidDecl(); | ||||
7999 | return; | ||||
8000 | } | ||||
8001 | Init = Result.take(); | ||||
8002 | DefaultedToAuto = true; | ||||
8003 | } | ||||
Richard Smith | 9b13175 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 8004 | |
8005 | QualType DeducedType; | ||||
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 8006 | if (DeduceAutoType(VDecl->getTypeSourceInfo(), DeduceInit, DeducedType) == |
Sebastian Redl | b832f6d | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 8007 | DAR_Failed) |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 8008 | DiagnoseAutoDeductionFailure(VDecl, DeduceInit); |
Richard Smith | 9b13175 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 8009 | if (DeducedType.isNull()) { |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 8010 | RealDecl->setInvalidDecl(); |
8011 | return; | ||||
8012 | } | ||||
Richard Smith | 9b13175 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 8013 | VDecl->setType(DeducedType); |
Rafael Espindola | 2d1b096 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 8014 | assert(VDecl->isLinkageValid()); |
Rafael Espindola | 2d9e883 | 2013-03-12 21:06:00 +0000 | [diff] [blame] | 8015 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8016 | // In ARC, infer lifetime. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8017 | if (getLangOpts().ObjCAutoRefCount && inferObjCARCLifetime(VDecl)) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8018 | VDecl->setInvalidDecl(); |
8019 | |||||
Jordan Rose | 0abbdfe | 2012-06-08 22:46:07 +0000 | [diff] [blame] | 8020 | // Warn if we deduced 'id'. 'auto' usually implies type-safety, but using |
8021 | // 'id' instead of a specific object type prevents most of our usual checks. | ||||
8022 | // We only want to warn outside of template instantiations, though: | ||||
8023 | // inside a template, the 'id' could have come from a parameter. | ||||
Douglas Gregor | 1344e94 | 2013-03-07 22:57:58 +0000 | [diff] [blame] | 8024 | if (ActiveTemplateInstantiations.empty() && !DefaultedToAuto && |
Richard Smith | 9b13175 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 8025 | DeducedType->isObjCIdType()) { |
8026 | SourceLocation Loc = | ||||
8027 | VDecl->getTypeSourceInfo()->getTypeLoc().getBeginLoc(); | ||||
Jordan Rose | 0abbdfe | 2012-06-08 22:46:07 +0000 | [diff] [blame] | 8028 | Diag(Loc, diag::warn_auto_var_is_id) |
8029 | << VDecl->getDeclName() << DeduceInit->getSourceRange(); | ||||
8030 | } | ||||
8031 | |||||
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 8032 | // If this is a redeclaration, check that the type we just deduced matches |
8033 | // the previously declared type. | ||||
Richard Smith | dd9459f | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 8034 | if (VarDecl *Old = VDecl->getPreviousDecl()) { |
8035 | // We never need to merge the type, because we cannot form an incomplete | ||||
8036 | // array of auto, nor deduce such a type. | ||||
8037 | MergeVarDeclTypes(VDecl, Old, /*MergeTypeWithPrevious*/false); | ||||
8038 | } | ||||
Richard Smith | dc7a4f5 | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 8039 | |
8040 | // Check the deduced type is valid for a variable declaration. | ||||
8041 | CheckVariableDeclarationType(VDecl); | ||||
8042 | if (VDecl->isInvalidDecl()) | ||||
8043 | return; | ||||
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 8044 | } |
Richard Smith | 0188872 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8045 | |
8046 | if (VDecl->isLocalVarDecl() && VDecl->hasExternalStorage()) { | ||||
8047 | // C99 6.7.8p5. C++ has no such restriction, but that is a defect. | ||||
8048 | Diag(VDecl->getLocation(), diag::err_block_extern_cant_init); | ||||
8049 | VDecl->setInvalidDecl(); | ||||
8050 | return; | ||||
8051 | } | ||||
8052 | |||||
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 8053 | if (!VDecl->getType()->isDependentType()) { |
8054 | // A definition must end up with a complete type, which means it must be | ||||
8055 | // complete with the restriction that an array type might be completed by | ||||
8056 | // the initializer; note that later code assumes this restriction. | ||||
8057 | QualType BaseDeclType = VDecl->getType(); | ||||
8058 | if (const ArrayType *Array = Context.getAsIncompleteArrayType(BaseDeclType)) | ||||
8059 | BaseDeclType = Array->getElementType(); | ||||
8060 | if (RequireCompleteType(VDecl->getLocation(), BaseDeclType, | ||||
8061 | diag::err_typecheck_decl_incomplete_type)) { | ||||
8062 | RealDecl->setInvalidDecl(); | ||||
8063 | return; | ||||
8064 | } | ||||
Douglas Gregor | 3a91abf | 2010-08-24 05:27:49 +0000 | [diff] [blame] | 8065 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 8066 | // The variable can not have an abstract class type. |
8067 | if (RequireNonAbstractType(VDecl->getLocation(), VDecl->getType(), | ||||
8068 | diag::err_abstract_type_in_decl, | ||||
8069 | AbstractVariableType)) | ||||
8070 | VDecl->setInvalidDecl(); | ||||
Eli Friedman | a31feca | 2009-04-13 21:28:54 +0000 | [diff] [blame] | 8071 | } |
8072 | |||||
Sebastian Redl | 31310a2 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 8073 | const VarDecl *Def; |
8074 | if ((Def = VDecl->getDefinition()) && Def != VDecl) { | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8075 | Diag(VDecl->getLocation(), diag::err_redefinition) |
Douglas Gregor | 275a369 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 8076 | << VDecl->getDeclName(); |
8077 | Diag(Def->getLocation(), diag::note_previous_definition); | ||||
8078 | VDecl->setInvalidDecl(); | ||||
8079 | return; | ||||
8080 | } | ||||
Douglas Gregor | 3a91abf | 2010-08-24 05:27:49 +0000 | [diff] [blame] | 8081 | |
Douglas Gregor | 3a91abf | 2010-08-24 05:27:49 +0000 | [diff] [blame] | 8082 | const VarDecl* PrevInit = 0; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8083 | if (getLangOpts().CPlusPlus) { |
Douglas Gregor | a31040f | 2010-12-16 01:31:22 +0000 | [diff] [blame] | 8084 | // C++ [class.static.data]p4 |
8085 | // If a static data member is of const integral or const | ||||
8086 | // enumeration type, its declaration in the class definition can | ||||
8087 | // specify a constant-initializer which shall be an integral | ||||
8088 | // constant expression (5.19). In that case, the member can appear | ||||
8089 | // in integral constant expressions. The member shall still be | ||||
8090 | // defined in a namespace scope if it is used in the program and the | ||||
8091 | // namespace scope definition shall not contain an initializer. | ||||
8092 | // | ||||
8093 | // We already performed a redefinition check above, but for static | ||||
8094 | // data members we also need to check whether there was an in-class | ||||
8095 | // declaration with an initializer. | ||||
8096 | if (VDecl->isStaticDataMember() && VDecl->getAnyInitializer(PrevInit)) { | ||||
David Blaikie | d662a79 | 2011-10-19 22:56:21 +0000 | [diff] [blame] | 8097 | Diag(VDecl->getLocation(), diag::err_redefinition) |
8098 | << VDecl->getDeclName(); | ||||
Douglas Gregor | a31040f | 2010-12-16 01:31:22 +0000 | [diff] [blame] | 8099 | Diag(PrevInit->getLocation(), diag::note_previous_definition); |
8100 | return; | ||||
8101 | } | ||||
Douglas Gregor | 275a369 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 8102 | |
Douglas Gregor | a31040f | 2010-12-16 01:31:22 +0000 | [diff] [blame] | 8103 | if (VDecl->hasLocalStorage()) |
8104 | getCurFunction()->setHasBranchProtectedScope(); | ||||
8105 | |||||
8106 | if (DiagnoseUnexpandedParameterPack(Init, UPPC_Initializer)) { | ||||
8107 | VDecl->setInvalidDecl(); | ||||
8108 | return; | ||||
8109 | } | ||||
8110 | } | ||||
John McCall | e46f62c | 2010-08-01 01:24:59 +0000 | [diff] [blame] | 8111 | |
Peter Collingbourne | 8c25fc5 | 2011-09-19 21:14:35 +0000 | [diff] [blame] | 8112 | // OpenCL 1.1 6.5.2: "Variables allocated in the __local address space inside |
8113 | // a kernel function cannot be initialized." | ||||
8114 | if (VDecl->getStorageClass() == SC_OpenCLWorkGroupLocal) { | ||||
8115 | Diag(VDecl->getLocation(), diag::err_local_cant_init); | ||||
8116 | VDecl->setInvalidDecl(); | ||||
8117 | return; | ||||
8118 | } | ||||
8119 | |||||
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 8120 | // Get the decls type and save a reference for later, since |
Steve Naroff | d0091aa | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 8121 | // CheckInitializerTypes may change it. |
Steve Naroff | 410e3e2 | 2007-09-12 20:13:48 +0000 | [diff] [blame] | 8122 | QualType DclT = VDecl->getType(), SavT = DclT; |
Fariborz Jahanian | 509fb3e | 2012-03-09 18:47:16 +0000 | [diff] [blame] | 8123 | |
Douglas Gregor | 1344e94 | 2013-03-07 22:57:58 +0000 | [diff] [blame] | 8124 | // Expressions default to 'id' when we're in a debugger |
8125 | // and we are assigning it to a variable of Objective-C pointer type. | ||||
8126 | if (getLangOpts().DebuggerCastResultToId && DclT->isObjCObjectPointerType() && | ||||
8127 | Init->getType() == Context.UnknownAnyTy) { | ||||
8128 | ExprResult Result = forceUnknownAnyToType(Init, Context.getObjCIdType()); | ||||
8129 | if (Result.isInvalid()) { | ||||
8130 | VDecl->setInvalidDecl(); | ||||
8131 | return; | ||||
Fariborz Jahanian | 509fb3e | 2012-03-09 18:47:16 +0000 | [diff] [blame] | 8132 | } |
Douglas Gregor | 1344e94 | 2013-03-07 22:57:58 +0000 | [diff] [blame] | 8133 | Init = Result.take(); |
8134 | } | ||||
Richard Smith | 0188872 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8135 | |
8136 | // Perform the initialization. | ||||
8137 | if (!VDecl->isInvalidDecl()) { | ||||
8138 | InitializedEntity Entity = InitializedEntity::InitializeVariable(VDecl); | ||||
8139 | InitializationKind Kind | ||||
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 8140 | = DirectInit ? |
8141 | CXXDirectInit ? InitializationKind::CreateDirect(VDecl->getLocation(), | ||||
8142 | Init->getLocStart(), | ||||
8143 | Init->getLocEnd()) | ||||
8144 | : InitializationKind::CreateDirectList( | ||||
8145 | VDecl->getLocation()) | ||||
Richard Smith | 0188872 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8146 | : InitializationKind::CreateCopy(VDecl->getLocation(), |
8147 | Init->getLocStart()); | ||||
8148 | |||||
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 8149 | MultiExprArg Args = Init; |
8150 | if (CXXDirectInit) | ||||
8151 | Args = MultiExprArg(CXXDirectInit->getExprs(), | ||||
8152 | CXXDirectInit->getNumExprs()); | ||||
8153 | |||||
8154 | InitializationSequence InitSeq(*this, Entity, Kind, Args); | ||||
8155 | ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Args, &DclT); | ||||
Richard Smith | 0188872 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8156 | if (Result.isInvalid()) { |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 8157 | VDecl->setInvalidDecl(); |
Richard Smith | 0188872 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8158 | return; |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 8159 | } |
Richard Smith | 0188872 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8160 | |
8161 | Init = Result.takeAs<Expr>(); | ||||
8162 | } | ||||
8163 | |||||
Richard Trieu | 568f785 | 2012-10-01 17:39:51 +0000 | [diff] [blame] | 8164 | // Check for self-references within variable initializers. |
8165 | // Variables declared within a function/method body (except for references) | ||||
8166 | // are handled by a dataflow analysis. | ||||
8167 | if (!VDecl->hasLocalStorage() || VDecl->getType()->isRecordType() || | ||||
8168 | VDecl->getType()->isReferenceType()) { | ||||
8169 | CheckSelfReference(*this, RealDecl, Init, DirectInit); | ||||
8170 | } | ||||
8171 | |||||
Richard Smith | 0188872 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8172 | // If the type changed, it means we had an incomplete type that was |
8173 | // completed by the initializer. For example: | ||||
8174 | // int ary[] = { 1, 3, 5 }; | ||||
John McCall | 7307643 | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 8175 | // "ary" transitions from an IncompleteArrayType to a ConstantArrayType. |
Eli Friedman | 5c89c39 | 2012-02-23 02:25:10 +0000 | [diff] [blame] | 8176 | if (!VDecl->isInvalidDecl() && (DclT != SavT)) |
Richard Smith | 0188872 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8177 | VDecl->setType(DclT); |
Richard Smith | 0188872 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8178 | |
Jordan Rose | e10f4d3 | 2012-09-15 02:48:31 +0000 | [diff] [blame] | 8179 | if (!VDecl->isInvalidDecl()) { |
Richard Smith | 0188872 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8180 | checkUnsafeAssigns(VDecl->getLocation(), VDecl->getType(), Init); |
8181 | |||||
Jordan Rose | e10f4d3 | 2012-09-15 02:48:31 +0000 | [diff] [blame] | 8182 | if (VDecl->hasAttr<BlocksAttr>()) |
8183 | checkRetainCycles(VDecl, Init); | ||||
Jordan Rose | 58b6bdc | 2012-09-28 22:21:30 +0000 | [diff] [blame] | 8184 | |
8185 | // It is safe to assign a weak reference into a strong variable. | ||||
8186 | // Although this code can still have problems: | ||||
8187 | // id x = self.weakProp; | ||||
8188 | // id y = self.weakProp; | ||||
8189 | // we do not warn to warn spuriously when 'x' and 'y' are on separate | ||||
8190 | // paths through the function. This should be revisited if | ||||
8191 | // -Wrepeated-use-of-weak is made flow-sensitive. | ||||
Ted Kremenek | 904a326 | 2012-12-20 22:31:27 +0000 | [diff] [blame] | 8192 | if (VDecl->getType().getObjCLifetime() == Qualifiers::OCL_Strong) { |
Jordan Rose | 58b6bdc | 2012-09-28 22:21:30 +0000 | [diff] [blame] | 8193 | DiagnosticsEngine::Level Level = |
8194 | Diags.getDiagnosticLevel(diag::warn_arc_repeated_use_of_weak, | ||||
8195 | Init->getLocStart()); | ||||
8196 | if (Level != DiagnosticsEngine::Ignored) | ||||
8197 | getCurFunction()->markSafeWeakUse(Init); | ||||
8198 | } | ||||
Jordan Rose | e10f4d3 | 2012-09-15 02:48:31 +0000 | [diff] [blame] | 8199 | } |
8200 | |||||
Richard Smith | 4195637 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 8201 | // The initialization is usually a full-expression. |
8202 | // | ||||
8203 | // FIXME: If this is a braced initialization of an aggregate, it is not | ||||
8204 | // an expression, and each individual field initializer is a separate | ||||
8205 | // full-expression. For instance, in: | ||||
8206 | // | ||||
8207 | // struct Temp { ~Temp(); }; | ||||
8208 | // struct S { S(Temp); }; | ||||
8209 | // struct T { S a, b; } t = { Temp(), Temp() } | ||||
8210 | // | ||||
8211 | // we should destroy the first Temp before constructing the second. | ||||
Fariborz Jahanian | ad48a50 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 8212 | ExprResult Result = ActOnFinishFullExpr(Init, VDecl->getLocation(), |
8213 | false, | ||||
8214 | VDecl->isConstexpr()); | ||||
Richard Smith | 4195637 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 8215 | if (Result.isInvalid()) { |
8216 | VDecl->setInvalidDecl(); | ||||
8217 | return; | ||||
8218 | } | ||||
8219 | Init = Result.take(); | ||||
8220 | |||||
Richard Smith | 0188872 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8221 | // Attach the initializer to the decl. |
8222 | VDecl->setInit(Init); | ||||
8223 | |||||
8224 | if (VDecl->isLocalVarDecl()) { | ||||
8225 | // C99 6.7.8p4: All the expressions in an initializer for an object that has | ||||
8226 | // static storage duration shall be constant expressions or string literals. | ||||
8227 | // C++ does not have this restriction. | ||||
Enea Zaffanella | b9a5935 | 2013-07-22 10:58:26 +0000 | [diff] [blame] | 8228 | if (!getLangOpts().CPlusPlus && !VDecl->isInvalidDecl()) { |
8229 | if (VDecl->getStorageClass() == SC_Static) | ||||
8230 | CheckForConstantInitializer(Init, DclT); | ||||
8231 | // C89 is stricter than C99 for non-static aggregate types. | ||||
8232 | // C89 6.5.7p3: All the expressions [...] in an initializer list | ||||
8233 | // for an object that has aggregate or union type shall be | ||||
8234 | // constant expressions. | ||||
8235 | else if (!getLangOpts().C99 && VDecl->getType()->isAggregateType() && | ||||
Enea Zaffanella | 8202630 | 2013-07-22 19:10:20 +0000 | [diff] [blame] | 8236 | isa<InitListExpr>(Init) && |
Enea Zaffanella | b9a5935 | 2013-07-22 10:58:26 +0000 | [diff] [blame] | 8237 | !Init->isConstantInitializer(Context, false)) |
8238 | Diag(Init->getExprLoc(), | ||||
8239 | diag::ext_aggregate_init_not_constant) | ||||
8240 | << Init->getSourceRange(); | ||||
8241 | } | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8242 | } else if (VDecl->isStaticDataMember() && |
Douglas Gregor | 021c3b3 | 2009-03-11 23:00:04 +0000 | [diff] [blame] | 8243 | VDecl->getLexicalDeclContext()->isRecord()) { |
8244 | // This is an in-class initialization for a static data member, e.g., | ||||
8245 | // | ||||
8246 | // struct S { | ||||
8247 | // static const int value = 17; | ||||
8248 | // }; | ||||
8249 | |||||
Douglas Gregor | 021c3b3 | 2009-03-11 23:00:04 +0000 | [diff] [blame] | 8250 | // C++ [class.mem]p4: |
8251 | // A member-declarator can contain a constant-initializer only | ||||
8252 | // if it declares a static member (9.4) of const integral or | ||||
8253 | // const enumeration type, see 9.4.2. | ||||
Richard Smith | c6d990a | 2011-09-29 19:11:37 +0000 | [diff] [blame] | 8254 | // |
Richard Smith | 0188872 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8255 | // C++11 [class.static.data]p3: |
Richard Smith | c6d990a | 2011-09-29 19:11:37 +0000 | [diff] [blame] | 8256 | // If a non-volatile const static data member is of integral or |
8257 | // enumeration type, its declaration in the class definition can | ||||
8258 | // specify a brace-or-equal-initializer in which every initalizer-clause | ||||
8259 | // that is an assignment-expression is a constant expression. A static | ||||
8260 | // data member of literal type can be declared in the class definition | ||||
8261 | // with the constexpr specifier; if so, its declaration shall specify a | ||||
8262 | // brace-or-equal-initializer in which every initializer-clause that is | ||||
8263 | // an assignment-expression is a constant expression. | ||||
John McCall | 4e63564 | 2010-09-10 23:21:22 +0000 | [diff] [blame] | 8264 | |
8265 | // Do nothing on dependent types. | ||||
Richard Smith | 0188872 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8266 | if (DclT->isDependentType()) { |
John McCall | 4e63564 | 2010-09-10 23:21:22 +0000 | [diff] [blame] | 8267 | |
Richard Smith | c6d990a | 2011-09-29 19:11:37 +0000 | [diff] [blame] | 8268 | // Allow any 'static constexpr' members, whether or not they are of literal |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 8269 | // type. We separately check that every constexpr variable is of literal |
8270 | // type. | ||||
Richard Smith | c6d990a | 2011-09-29 19:11:37 +0000 | [diff] [blame] | 8271 | } else if (VDecl->isConstexpr()) { |
8272 | |||||
John McCall | 4e63564 | 2010-09-10 23:21:22 +0000 | [diff] [blame] | 8273 | // Require constness. |
Richard Smith | 0188872 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8274 | } else if (!DclT.isConstQualified()) { |
John McCall | 4e63564 | 2010-09-10 23:21:22 +0000 | [diff] [blame] | 8275 | Diag(VDecl->getLocation(), diag::err_in_class_initializer_non_const) |
8276 | << Init->getSourceRange(); | ||||
Douglas Gregor | 021c3b3 | 2009-03-11 23:00:04 +0000 | [diff] [blame] | 8277 | VDecl->setInvalidDecl(); |
John McCall | 4e63564 | 2010-09-10 23:21:22 +0000 | [diff] [blame] | 8278 | |
8279 | // We allow integer constant expressions in all cases. | ||||
Richard Smith | 0188872 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8280 | } else if (DclT->isIntegralOrEnumerationType()) { |
Chris Lattner | 24c38e1 | 2011-06-14 05:46:29 +0000 | [diff] [blame] | 8281 | // Check whether the expression is a constant expression. |
8282 | SourceLocation Loc; | ||||
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 8283 | if (getLangOpts().CPlusPlus11 && DclT.isVolatileQualified()) |
Richard Smith | 0188872 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8284 | // In C++11, a non-constexpr const static data member with an |
Richard Smith | 2da7a51 | 2011-09-29 21:28:14 +0000 | [diff] [blame] | 8285 | // in-class initializer cannot be volatile. |
8286 | Diag(VDecl->getLocation(), diag::err_in_class_initializer_volatile); | ||||
8287 | else if (Init->isValueDependent()) | ||||
Chris Lattner | 24c38e1 | 2011-06-14 05:46:29 +0000 | [diff] [blame] | 8288 | ; // Nothing to check. |
8289 | else if (Init->isIntegerConstantExpr(Context, &Loc)) | ||||
8290 | ; // Ok, it's an ICE! | ||||
8291 | else if (Init->isEvaluatable(Context)) { | ||||
8292 | // If we can constant fold the initializer through heroics, accept it, | ||||
8293 | // but report this as a use of an extension for -pedantic. | ||||
8294 | Diag(Loc, diag::ext_in_class_initializer_non_constant) | ||||
8295 | << Init->getSourceRange(); | ||||
8296 | } else { | ||||
8297 | // Otherwise, this is some crazy unknown case. Report the issue at the | ||||
8298 | // location provided by the isIntegerConstantExpr failed check. | ||||
8299 | Diag(Loc, diag::err_in_class_initializer_non_constant) | ||||
8300 | << Init->getSourceRange(); | ||||
8301 | VDecl->setInvalidDecl(); | ||||
John McCall | 4e63564 | 2010-09-10 23:21:22 +0000 | [diff] [blame] | 8302 | } |
8303 | |||||
Richard Smith | 0188872 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8304 | // We allow foldable floating-point constants as an extension. |
8305 | } else if (DclT->isFloatingType()) { // also permits complex, which is ok | ||||
Richard Smith | b4b1d69 | 2013-01-25 04:22:16 +0000 | [diff] [blame] | 8306 | // In C++98, this is a GNU extension. In C++11, it is not, but we support |
8307 | // it anyway and provide a fixit to add the 'constexpr'. | ||||
8308 | if (getLangOpts().CPlusPlus11) { | ||||
David Blaikie | a367e9d | 2013-01-29 22:26:08 +0000 | [diff] [blame] | 8309 | Diag(VDecl->getLocation(), |
8310 | diag::ext_in_class_initializer_float_type_cxx11) | ||||
8311 | << DclT << Init->getSourceRange(); | ||||
8312 | Diag(VDecl->getLocStart(), | ||||
8313 | diag::note_in_class_initializer_float_type_cxx11) | ||||
8314 | << FixItHint::CreateInsertion(VDecl->getLocStart(), "constexpr "); | ||||
Richard Smith | b4b1d69 | 2013-01-25 04:22:16 +0000 | [diff] [blame] | 8315 | } else { |
8316 | Diag(VDecl->getLocation(), diag::ext_in_class_initializer_float_type) | ||||
8317 | << DclT << Init->getSourceRange(); | ||||
John McCall | 4e63564 | 2010-09-10 23:21:22 +0000 | [diff] [blame] | 8318 | |
Richard Smith | b4b1d69 | 2013-01-25 04:22:16 +0000 | [diff] [blame] | 8319 | if (!Init->isValueDependent() && !Init->isEvaluatable(Context)) { |
8320 | Diag(Init->getExprLoc(), diag::err_in_class_initializer_non_constant) | ||||
8321 | << Init->getSourceRange(); | ||||
8322 | VDecl->setInvalidDecl(); | ||||
8323 | } | ||||
Douglas Gregor | 021c3b3 | 2009-03-11 23:00:04 +0000 | [diff] [blame] | 8324 | } |
Richard Smith | 947be19 | 2011-09-29 23:18:34 +0000 | [diff] [blame] | 8325 | |
Richard Smith | 0188872 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8326 | // Suggest adding 'constexpr' in C++11 for literal types. |
Richard Smith | a10b978 | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 8327 | } else if (getLangOpts().CPlusPlus11 && DclT->isLiteralType(Context)) { |
Richard Smith | 947be19 | 2011-09-29 23:18:34 +0000 | [diff] [blame] | 8328 | Diag(VDecl->getLocation(), diag::err_in_class_initializer_literal_type) |
Richard Smith | 0188872 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8329 | << DclT << Init->getSourceRange() |
Richard Smith | 947be19 | 2011-09-29 23:18:34 +0000 | [diff] [blame] | 8330 | << FixItHint::CreateInsertion(VDecl->getLocStart(), "constexpr "); |
8331 | VDecl->setConstexpr(true); | ||||
8332 | |||||
Richard Smith | c6d990a | 2011-09-29 19:11:37 +0000 | [diff] [blame] | 8333 | } else { |
8334 | Diag(VDecl->getLocation(), diag::err_in_class_initializer_bad_type) | ||||
Richard Smith | 0188872 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8335 | << DclT << Init->getSourceRange(); |
Richard Smith | c6d990a | 2011-09-29 19:11:37 +0000 | [diff] [blame] | 8336 | VDecl->setInvalidDecl(); |
Douglas Gregor | 021c3b3 | 2009-03-11 23:00:04 +0000 | [diff] [blame] | 8337 | } |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 8338 | } else if (VDecl->isFileVarDecl()) { |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 8339 | if (VDecl->getStorageClass() == SC_Extern && |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8340 | (!getLangOpts().CPlusPlus || |
Rafael Espindola | 5b34b9c | 2013-03-29 07:56:05 +0000 | [diff] [blame] | 8341 | !(Context.getBaseElementType(VDecl->getType()).isConstQualified() || |
Richard Smith | d0629eb | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 8342 | VDecl->isExternC())) && |
8343 | !isTemplateInstantiation(VDecl->getTemplateSpecializationKind())) | ||||
Steve Naroff | 410e3e2 | 2007-09-12 20:13:48 +0000 | [diff] [blame] | 8344 | Diag(VDecl->getLocation(), diag::warn_extern_init); |
Eli Friedman | a91eb54 | 2009-12-22 02:10:53 +0000 | [diff] [blame] | 8345 | |
Richard Smith | 0188872 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8346 | // C99 6.7.8p4. All file scoped initializers need to be constant. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8347 | if (!getLangOpts().CPlusPlus && !VDecl->isInvalidDecl()) |
Anders Carlsson | c5eb731 | 2008-08-22 05:00:02 +0000 | [diff] [blame] | 8348 | CheckForConstantInitializer(Init, DclT); |
Richard Smith | 6a570f6 | 2013-04-14 20:11:31 +0000 | [diff] [blame] | 8349 | else if (VDecl->getTLSKind() == VarDecl::TLS_Static && |
8350 | !VDecl->isInvalidDecl() && !DclT->isDependentType() && | ||||
8351 | !Init->isValueDependent() && !VDecl->isConstexpr() && | ||||
Richard Smith | b6b127f | 2013-04-15 08:07:34 +0000 | [diff] [blame] | 8352 | !Init->isConstantInitializer( |
8353 | Context, VDecl->getType()->isReferenceType())) { | ||||
Richard Smith | 6a570f6 | 2013-04-14 20:11:31 +0000 | [diff] [blame] | 8354 | // GNU C++98 edits for __thread, [basic.start.init]p4: |
8355 | // An object of thread storage duration shall not require dynamic | ||||
8356 | // initialization. | ||||
8357 | // FIXME: Need strict checking here. | ||||
8358 | Diag(VDecl->getLocation(), diag::err_thread_dynamic_init); | ||||
8359 | if (getLangOpts().CPlusPlus11) | ||||
8360 | Diag(VDecl->getLocation(), diag::note_use_thread_local); | ||||
8361 | } | ||||
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 8362 | } |
Douglas Gregor | b6c8c8b | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 8363 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 8364 | // We will represent direct-initialization similarly to copy-initialization: |
8365 | // int x(1); -as-> int x = 1; | ||||
8366 | // ClassType x(a,b,c); -as-> ClassType x = ClassType(a,b,c); | ||||
8367 | // | ||||
8368 | // Clients that want to distinguish between the two forms, can check for | ||||
8369 | // direct initializer using VarDecl::getInitStyle(). | ||||
8370 | // A major benefit is that clients that don't particularly care about which | ||||
8371 | // exactly form was it (like the CodeGen) can handle both cases without | ||||
8372 | // special case code. | ||||
8373 | |||||
8374 | // C++ 8.5p11: | ||||
8375 | // The form of initialization (using parentheses or '=') is generally | ||||
8376 | // insignificant, but does matter when the entity being initialized has a | ||||
8377 | // class type. | ||||
8378 | if (CXXDirectInit) { | ||||
8379 | assert(DirectInit && "Call-style initializer must be direct init."); | ||||
8380 | VDecl->setInitStyle(VarDecl::CallInit); | ||||
8381 | } else if (DirectInit) { | ||||
8382 | // This must be list-initialization. No other way is direct-initialization. | ||||
8383 | VDecl->setInitStyle(VarDecl::ListInit); | ||||
8384 | } | ||||
8385 | |||||
John McCall | 2998d6b | 2011-01-19 11:48:09 +0000 | [diff] [blame] | 8386 | CheckCompleteVariableDeclaration(VDecl); |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 8387 | } |
8388 | |||||
John McCall | 7727acf | 2010-03-31 02:13:20 +0000 | [diff] [blame] | 8389 | /// ActOnInitializerError - Given that there was an error parsing an |
8390 | /// initializer for the given declaration, try to return to some form | ||||
8391 | /// of sanity. | ||||
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8392 | void Sema::ActOnInitializerError(Decl *D) { |
John McCall | 7727acf | 2010-03-31 02:13:20 +0000 | [diff] [blame] | 8393 | // Our main concern here is re-establishing invariants like "a |
8394 | // variable's type is either dependent or complete". | ||||
John McCall | 7727acf | 2010-03-31 02:13:20 +0000 | [diff] [blame] | 8395 | if (!D || D->isInvalidDecl()) return; |
8396 | |||||
8397 | VarDecl *VD = dyn_cast<VarDecl>(D); | ||||
8398 | if (!VD) return; | ||||
8399 | |||||
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 8400 | // 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] | 8401 | if (ParsingInitForAutoVars.count(D)) { |
8402 | D->setInvalidDecl(); | ||||
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 8403 | return; |
8404 | } | ||||
8405 | |||||
John McCall | 7727acf | 2010-03-31 02:13:20 +0000 | [diff] [blame] | 8406 | QualType Ty = VD->getType(); |
8407 | if (Ty->isDependentType()) return; | ||||
8408 | |||||
8409 | // Require a complete type. | ||||
8410 | if (RequireCompleteType(VD->getLocation(), | ||||
8411 | Context.getBaseElementType(Ty), | ||||
8412 | diag::err_typecheck_decl_incomplete_type)) { | ||||
8413 | VD->setInvalidDecl(); | ||||
8414 | return; | ||||
8415 | } | ||||
8416 | |||||
8417 | // Require an abstract type. | ||||
8418 | if (RequireNonAbstractType(VD->getLocation(), Ty, | ||||
8419 | diag::err_abstract_type_in_decl, | ||||
8420 | AbstractVariableType)) { | ||||
8421 | VD->setInvalidDecl(); | ||||
8422 | return; | ||||
8423 | } | ||||
8424 | |||||
8425 | // Don't bother complaining about constructors or destructors, | ||||
8426 | // though. | ||||
8427 | } | ||||
8428 | |||||
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8429 | void Sema::ActOnUninitializedDecl(Decl *RealDecl, |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 8430 | bool TypeMayContainAuto) { |
Argyrios Kyrtzidis | 48c2e90 | 2008-11-07 13:01:22 +0000 | [diff] [blame] | 8431 | // If there is no declaration, there was an error parsing it. Just ignore it. |
8432 | if (RealDecl == 0) | ||||
8433 | return; | ||||
8434 | |||||
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 8435 | if (VarDecl *Var = dyn_cast<VarDecl>(RealDecl)) { |
8436 | QualType Type = Var->getType(); | ||||
Douglas Gregor | b6c8c8b | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 8437 | |
Richard Smith | dd4b350 | 2011-12-25 21:17:58 +0000 | [diff] [blame] | 8438 | // C++11 [dcl.spec.auto]p3 |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 8439 | if (TypeMayContainAuto && Type->getContainedAutoType()) { |
Anders Carlsson | 6a75cd9 | 2009-07-11 00:34:39 +0000 | [diff] [blame] | 8440 | Diag(Var->getLocation(), diag::err_auto_var_requires_init) |
8441 | << Var->getDeclName() << Type; | ||||
8442 | Var->setInvalidDecl(); | ||||
8443 | return; | ||||
8444 | } | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8445 | |
Richard Smith | dd4b350 | 2011-12-25 21:17:58 +0000 | [diff] [blame] | 8446 | // 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] | 8447 | // the constexpr specifier; if so, its declaration shall specify |
8448 | // a brace-or-equal-initializer. | ||||
Richard Smith | dd4b350 | 2011-12-25 21:17:58 +0000 | [diff] [blame] | 8449 | // C++11 [dcl.constexpr]p1: The constexpr specifier shall be applied only to |
8450 | // the definition of a variable [...] or the declaration of a static data | ||||
8451 | // member. | ||||
8452 | if (Var->isConstexpr() && !Var->isThisDeclarationADefinition()) { | ||||
8453 | if (Var->isStaticDataMember()) | ||||
8454 | Diag(Var->getLocation(), | ||||
8455 | diag::err_constexpr_static_mem_var_requires_init) | ||||
8456 | << Var->getDeclName(); | ||||
8457 | else | ||||
8458 | Diag(Var->getLocation(), diag::err_invalid_constexpr_var_decl); | ||||
Richard Smith | c6d990a | 2011-09-29 19:11:37 +0000 | [diff] [blame] | 8459 | Var->setInvalidDecl(); |
8460 | return; | ||||
8461 | } | ||||
8462 | |||||
Douglas Gregor | 60c93c9 | 2010-02-09 07:26:29 +0000 | [diff] [blame] | 8463 | switch (Var->isThisDeclarationADefinition()) { |
8464 | case VarDecl::Definition: | ||||
8465 | if (!Var->isStaticDataMember() || !Var->getAnyInitializer()) | ||||
8466 | break; | ||||
8467 | |||||
8468 | // We have an out-of-line definition of a static data member | ||||
8469 | // that has an in-class initializer, so we type-check this like | ||||
8470 | // a declaration. | ||||
8471 | // | ||||
8472 | // Fall through | ||||
8473 | |||||
8474 | case VarDecl::DeclarationOnly: | ||||
8475 | // It's only a declaration. | ||||
8476 | |||||
8477 | // Block scope. C99 6.7p7: If an identifier for an object is | ||||
8478 | // declared with no linkage (C99 6.2.2p6), the type for the | ||||
8479 | // object shall be complete. | ||||
John McCall | b6bbcc9 | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 8480 | if (!Type->isDependentType() && Var->isLocalVarDecl() && |
Rafael Espindola | 181e3ec | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 8481 | !Var->hasLinkage() && !Var->isInvalidDecl() && |
Douglas Gregor | 60c93c9 | 2010-02-09 07:26:29 +0000 | [diff] [blame] | 8482 | RequireCompleteType(Var->getLocation(), Type, |
8483 | diag::err_typecheck_decl_incomplete_type)) | ||||
8484 | Var->setInvalidDecl(); | ||||
8485 | |||||
8486 | // Make sure that the type is not abstract. | ||||
8487 | if (!Type->isDependentType() && !Var->isInvalidDecl() && | ||||
8488 | RequireNonAbstractType(Var->getLocation(), Type, | ||||
8489 | diag::err_abstract_type_in_decl, | ||||
8490 | AbstractVariableType)) | ||||
8491 | Var->setInvalidDecl(); | ||||
Fariborz Jahanian | 4cc83c2 | 2012-08-15 18:42:26 +0000 | [diff] [blame] | 8492 | if (!Type->isDependentType() && !Var->isInvalidDecl() && |
Fariborz Jahanian | 767a1a2 | 2012-08-17 21:44:55 +0000 | [diff] [blame] | 8493 | Var->getStorageClass() == SC_PrivateExtern) { |
Fariborz Jahanian | 4cc83c2 | 2012-08-15 18:42:26 +0000 | [diff] [blame] | 8494 | Diag(Var->getLocation(), diag::warn_private_extern); |
Fariborz Jahanian | 767a1a2 | 2012-08-17 21:44:55 +0000 | [diff] [blame] | 8495 | Diag(Var->getLocation(), diag::note_private_extern); |
8496 | } | ||||
Fariborz Jahanian | 4cc83c2 | 2012-08-15 18:42:26 +0000 | [diff] [blame] | 8497 | |
Douglas Gregor | 60c93c9 | 2010-02-09 07:26:29 +0000 | [diff] [blame] | 8498 | return; |
8499 | |||||
8500 | case VarDecl::TentativeDefinition: | ||||
8501 | // File scope. C99 6.9.2p2: A declaration of an identifier for an | ||||
8502 | // object that has file scope without an initializer, and without a | ||||
8503 | // storage-class specifier or with the storage-class specifier "static", | ||||
8504 | // constitutes a tentative definition. Note: A tentative definition with | ||||
8505 | // external linkage is valid (C99 6.2.2p5). | ||||
8506 | if (!Var->isInvalidDecl()) { | ||||
8507 | if (const IncompleteArrayType *ArrayT | ||||
8508 | = Context.getAsIncompleteArrayType(Type)) { | ||||
8509 | if (RequireCompleteType(Var->getLocation(), | ||||
8510 | ArrayT->getElementType(), | ||||
8511 | diag::err_illegal_decl_array_incomplete_type)) | ||||
8512 | Var->setInvalidDecl(); | ||||
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 8513 | } else if (Var->getStorageClass() == SC_Static) { |
Douglas Gregor | 60c93c9 | 2010-02-09 07:26:29 +0000 | [diff] [blame] | 8514 | // C99 6.9.2p3: If the declaration of an identifier for an object is |
8515 | // a tentative definition and has internal linkage (C99 6.2.2p3), the | ||||
8516 | // declared type shall not be an incomplete type. | ||||
8517 | // NOTE: code such as the following | ||||
8518 | // static struct s; | ||||
8519 | // struct s { int a; }; | ||||
8520 | // is accepted by gcc. Hence here we issue a warning instead of | ||||
8521 | // an error and we do not invalidate the static declaration. | ||||
8522 | // NOTE: to avoid multiple warnings, only check the first declaration. | ||||
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 8523 | if (Var->getPreviousDecl() == 0) |
Douglas Gregor | 60c93c9 | 2010-02-09 07:26:29 +0000 | [diff] [blame] | 8524 | RequireCompleteType(Var->getLocation(), Type, |
8525 | diag::ext_typecheck_decl_incomplete_type); | ||||
8526 | } | ||||
8527 | } | ||||
8528 | |||||
8529 | // Record the tentative definition; we're done. | ||||
8530 | if (!Var->isInvalidDecl()) | ||||
8531 | TentativeDefinitions.push_back(Var); | ||||
8532 | return; | ||||
8533 | } | ||||
8534 | |||||
8535 | // Provide a specific diagnostic for uninitialized variable | ||||
8536 | // definitions with incomplete array type. | ||||
8537 | if (Type->isIncompleteArrayType()) { | ||||
Sebastian Redl | 6e82475 | 2009-11-05 19:47:47 +0000 | [diff] [blame] | 8538 | Diag(Var->getLocation(), |
8539 | diag::err_typecheck_incomplete_array_needs_initializer); | ||||
8540 | Var->setInvalidDecl(); | ||||
8541 | return; | ||||
8542 | } | ||||
8543 | |||||
John McCall | b567a8b | 2010-08-01 01:25:24 +0000 | [diff] [blame] | 8544 | // Provide a specific diagnostic for uninitialized variable |
8545 | // definitions with reference type. | ||||
8546 | if (Type->isReferenceType()) { | ||||
8547 | Diag(Var->getLocation(), diag::err_reference_var_requires_init) | ||||
8548 | << Var->getDeclName() | ||||
8549 | << SourceRange(Var->getLocation(), Var->getLocation()); | ||||
8550 | Var->setInvalidDecl(); | ||||
8551 | return; | ||||
8552 | } | ||||
Douglas Gregor | 60c93c9 | 2010-02-09 07:26:29 +0000 | [diff] [blame] | 8553 | |
8554 | // Do not attempt to type-check the default initializer for a | ||||
8555 | // variable with dependent type. | ||||
8556 | if (Type->isDependentType()) | ||||
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8557 | return; |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 8558 | |
Douglas Gregor | 60c93c9 | 2010-02-09 07:26:29 +0000 | [diff] [blame] | 8559 | if (Var->isInvalidDecl()) |
8560 | return; | ||||
Douglas Gregor | 1ab537b | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 8561 | |
Douglas Gregor | 60c93c9 | 2010-02-09 07:26:29 +0000 | [diff] [blame] | 8562 | if (RequireCompleteType(Var->getLocation(), |
8563 | Context.getBaseElementType(Type), | ||||
8564 | diag::err_typecheck_decl_incomplete_type)) { | ||||
8565 | Var->setInvalidDecl(); | ||||
8566 | return; | ||||
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 8567 | } |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 8568 | |
Douglas Gregor | 60c93c9 | 2010-02-09 07:26:29 +0000 | [diff] [blame] | 8569 | // The variable can not have an abstract class type. |
8570 | if (RequireNonAbstractType(Var->getLocation(), Type, | ||||
8571 | diag::err_abstract_type_in_decl, | ||||
8572 | AbstractVariableType)) { | ||||
8573 | Var->setInvalidDecl(); | ||||
8574 | return; | ||||
8575 | } | ||||
8576 | |||||
Douglas Gregor | 4337dc7 | 2011-05-21 17:52:48 +0000 | [diff] [blame] | 8577 | // Check for jumps past the implicit initializer. C++0x |
8578 | // clarifies that this applies to a "variable with automatic | ||||
8579 | // storage duration", not a "local variable". | ||||
Richard Smith | 0e9e981 | 2011-10-20 21:42:12 +0000 | [diff] [blame] | 8580 | // C++11 [stmt.dcl]p3 |
Douglas Gregor | 4337dc7 | 2011-05-21 17:52:48 +0000 | [diff] [blame] | 8581 | // A program that jumps from a point where a variable with automatic |
8582 | // storage duration is not in scope to a point where it is in scope is | ||||
8583 | // ill-formed unless the variable has scalar type, class type with a | ||||
8584 | // trivial default constructor and a trivial destructor, a cv-qualified | ||||
8585 | // version of one of these types, or an array of one of the preceding | ||||
8586 | // types and is declared without an initializer. | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8587 | if (getLangOpts().CPlusPlus && Var->hasLocalStorage()) { |
Douglas Gregor | 4337dc7 | 2011-05-21 17:52:48 +0000 | [diff] [blame] | 8588 | if (const RecordType *Record |
8589 | = Context.getBaseElementType(Type)->getAs<RecordType>()) { | ||||
Sean Hunt | a6bff2c | 2011-05-11 22:50:12 +0000 | [diff] [blame] | 8590 | CXXRecordDecl *CXXRecord = cast<CXXRecordDecl>(Record->getDecl()); |
Richard Smith | 0e9e981 | 2011-10-20 21:42:12 +0000 | [diff] [blame] | 8591 | // Mark the function for further checking even if the looser rules of |
8592 | // C++11 do not require such checks, so that we can diagnose | ||||
8593 | // incompatibilities with C++98. | ||||
8594 | if (!CXXRecord->isPOD()) | ||||
Sean Hunt | a6bff2c | 2011-05-11 22:50:12 +0000 | [diff] [blame] | 8595 | getCurFunction()->setHasBranchProtectedScope(); |
8596 | } | ||||
Douglas Gregor | 60c93c9 | 2010-02-09 07:26:29 +0000 | [diff] [blame] | 8597 | } |
Douglas Gregor | 4337dc7 | 2011-05-21 17:52:48 +0000 | [diff] [blame] | 8598 | |
8599 | // C++03 [dcl.init]p9: | ||||
8600 | // If no initializer is specified for an object, and the | ||||
8601 | // object is of (possibly cv-qualified) non-POD class type (or | ||||
8602 | // array thereof), the object shall be default-initialized; if | ||||
8603 | // the object is of const-qualified type, the underlying class | ||||
8604 | // type shall have a user-declared default | ||||
8605 | // constructor. Otherwise, if no initializer is specified for | ||||
8606 | // a non- static object, the object and its subobjects, if | ||||
8607 | // any, have an indeterminate initial value); if the object | ||||
8608 | // or any of its subobjects are of const-qualified type, the | ||||
8609 | // program is ill-formed. | ||||
8610 | // C++0x [dcl.init]p11: | ||||
8611 | // If no initializer is specified for an object, the object is | ||||
8612 | // default-initialized; [...]. | ||||
8613 | InitializedEntity Entity = InitializedEntity::InitializeVariable(Var); | ||||
8614 | InitializationKind Kind | ||||
8615 | = InitializationKind::CreateDefault(Var->getLocation()); | ||||
Dmitri Gribenko | 62ed889 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 8616 | |
8617 | InitializationSequence InitSeq(*this, Entity, Kind, None); | ||||
8618 | ExprResult Init = InitSeq.Perform(*this, Entity, Kind, None); | ||||
Douglas Gregor | 4337dc7 | 2011-05-21 17:52:48 +0000 | [diff] [blame] | 8619 | if (Init.isInvalid()) |
8620 | Var->setInvalidDecl(); | ||||
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 8621 | else if (Init.get()) { |
Douglas Gregor | 4337dc7 | 2011-05-21 17:52:48 +0000 | [diff] [blame] | 8622 | Var->setInit(MaybeCreateExprWithCleanups(Init.get())); |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 8623 | // This is important for template substitution. |
8624 | Var->setInitStyle(VarDecl::CallInit); | ||||
8625 | } | ||||
Douglas Gregor | 516a6bc | 2010-03-08 02:45:10 +0000 | [diff] [blame] | 8626 | |
John McCall | 2998d6b | 2011-01-19 11:48:09 +0000 | [diff] [blame] | 8627 | CheckCompleteVariableDeclaration(Var); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 8628 | } |
8629 | } | ||||
8630 | |||||
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 8631 | void Sema::ActOnCXXForRangeDecl(Decl *D) { |
8632 | VarDecl *VD = dyn_cast<VarDecl>(D); | ||||
8633 | if (!VD) { | ||||
8634 | Diag(D->getLocation(), diag::err_for_range_decl_must_be_var); | ||||
8635 | D->setInvalidDecl(); | ||||
8636 | return; | ||||
8637 | } | ||||
8638 | |||||
8639 | VD->setCXXForRangeDecl(true); | ||||
8640 | |||||
8641 | // for-range-declaration cannot be given a storage class specifier. | ||||
8642 | int Error = -1; | ||||
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 8643 | switch (VD->getStorageClass()) { |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 8644 | case SC_None: |
8645 | break; | ||||
8646 | case SC_Extern: | ||||
8647 | Error = 0; | ||||
8648 | break; | ||||
8649 | case SC_Static: | ||||
8650 | Error = 1; | ||||
8651 | break; | ||||
8652 | case SC_PrivateExtern: | ||||
8653 | Error = 2; | ||||
8654 | break; | ||||
8655 | case SC_Auto: | ||||
8656 | Error = 3; | ||||
8657 | break; | ||||
8658 | case SC_Register: | ||||
8659 | Error = 4; | ||||
8660 | break; | ||||
Peter Collingbourne | 8c25fc5 | 2011-09-19 21:14:35 +0000 | [diff] [blame] | 8661 | case SC_OpenCLWorkGroupLocal: |
Peter Collingbourne | 8be0c74 | 2011-09-20 12:40:26 +0000 | [diff] [blame] | 8662 | llvm_unreachable("Unexpected storage class"); |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 8663 | } |
Richard Smith | c6d990a | 2011-09-29 19:11:37 +0000 | [diff] [blame] | 8664 | if (VD->isConstexpr()) |
8665 | Error = 5; | ||||
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 8666 | if (Error != -1) { |
8667 | Diag(VD->getOuterLocStart(), diag::err_for_range_storage_class) | ||||
8668 | << VD->getDeclName() << Error; | ||||
8669 | D->setInvalidDecl(); | ||||
8670 | } | ||||
8671 | } | ||||
8672 | |||||
John McCall | 2998d6b | 2011-01-19 11:48:09 +0000 | [diff] [blame] | 8673 | void Sema::CheckCompleteVariableDeclaration(VarDecl *var) { |
8674 | if (var->isInvalidDecl()) return; | ||||
8675 | |||||
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8676 | // In ARC, don't allow jumps past the implicit initialization of a |
8677 | // local retaining variable. | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8678 | if (getLangOpts().ObjCAutoRefCount && |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8679 | var->hasLocalStorage()) { |
8680 | switch (var->getType().getObjCLifetime()) { | ||||
8681 | case Qualifiers::OCL_None: | ||||
8682 | case Qualifiers::OCL_ExplicitNone: | ||||
8683 | case Qualifiers::OCL_Autoreleasing: | ||||
8684 | break; | ||||
8685 | |||||
8686 | case Qualifiers::OCL_Weak: | ||||
8687 | case Qualifiers::OCL_Strong: | ||||
8688 | getCurFunction()->setHasBranchProtectedScope(); | ||||
8689 | break; | ||||
8690 | } | ||||
8691 | } | ||||
8692 | |||||
Eli Friedman | e4851f2 | 2012-10-23 20:19:32 +0000 | [diff] [blame] | 8693 | if (var->isThisDeclarationADefinition() && |
Eli Friedman | 2ae28e5 | 2013-09-24 23:10:08 +0000 | [diff] [blame] | 8694 | var->isExternallyVisible() && var->hasLinkage() && |
Manuel Klimek | acaf110 | 2012-12-12 13:26:54 +0000 | [diff] [blame] | 8695 | getDiagnostics().getDiagnosticLevel( |
8696 | diag::warn_missing_variable_declarations, | ||||
8697 | var->getLocation())) { | ||||
Eli Friedman | e4851f2 | 2012-10-23 20:19:32 +0000 | [diff] [blame] | 8698 | // Find a previous declaration that's not a definition. |
8699 | VarDecl *prev = var->getPreviousDecl(); | ||||
8700 | while (prev && prev->isThisDeclarationADefinition()) | ||||
8701 | prev = prev->getPreviousDecl(); | ||||
8702 | |||||
8703 | if (!prev) | ||||
8704 | Diag(var->getLocation(), diag::warn_missing_variable_declarations) << var; | ||||
8705 | } | ||||
8706 | |||||
Richard Smith | 6a570f6 | 2013-04-14 20:11:31 +0000 | [diff] [blame] | 8707 | if (var->getTLSKind() == VarDecl::TLS_Static && |
8708 | var->getType().isDestructedType()) { | ||||
8709 | // GNU C++98 edits for __thread, [basic.start.term]p3: | ||||
8710 | // The type of an object with thread storage duration shall not | ||||
8711 | // have a non-trivial destructor. | ||||
8712 | Diag(var->getLocation(), diag::err_thread_nontrivial_dtor); | ||||
8713 | if (getLangOpts().CPlusPlus11) | ||||
8714 | Diag(var->getLocation(), diag::note_use_thread_local); | ||||
8715 | } | ||||
8716 | |||||
John McCall | 2998d6b | 2011-01-19 11:48:09 +0000 | [diff] [blame] | 8717 | // All the following checks are C++ only. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8718 | if (!getLangOpts().CPlusPlus) return; |
John McCall | 2998d6b | 2011-01-19 11:48:09 +0000 | [diff] [blame] | 8719 | |
Richard Smith | a67d503 | 2012-11-09 23:03:14 +0000 | [diff] [blame] | 8720 | QualType type = var->getType(); |
8721 | if (type->isDependentType()) return; | ||||
John McCall | 2998d6b | 2011-01-19 11:48:09 +0000 | [diff] [blame] | 8722 | |
8723 | // __block variables might require us to capture a copy-initializer. | ||||
8724 | if (var->hasAttr<BlocksAttr>()) { | ||||
8725 | // It's currently invalid to ever have a __block variable with an | ||||
8726 | // array type; should we diagnose that here? | ||||
8727 | |||||
8728 | // Regardless, we don't want to ignore array nesting when | ||||
8729 | // constructing this copy. | ||||
John McCall | 2998d6b | 2011-01-19 11:48:09 +0000 | [diff] [blame] | 8730 | if (type->isStructureOrClassType()) { |
John McCall | b760f11 | 2013-03-22 02:10:40 +0000 | [diff] [blame] | 8731 | EnterExpressionEvaluationContext scope(*this, PotentiallyEvaluated); |
John McCall | 2998d6b | 2011-01-19 11:48:09 +0000 | [diff] [blame] | 8732 | SourceLocation poi = var->getLocation(); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 8733 | Expr *varRef =new (Context) DeclRefExpr(var, false, type, VK_LValue, poi); |
Douglas Gregor | 6cda3e6 | 2013-03-07 22:38:24 +0000 | [diff] [blame] | 8734 | ExprResult result |
8735 | = PerformMoveOrCopyInitialization( | ||||
8736 | InitializedEntity::InitializeBlock(poi, type, false), | ||||
8737 | var, var->getType(), varRef, /*AllowNRVO=*/true); | ||||
John McCall | 2998d6b | 2011-01-19 11:48:09 +0000 | [diff] [blame] | 8738 | if (!result.isInvalid()) { |
8739 | result = MaybeCreateExprWithCleanups(result); | ||||
8740 | Expr *init = result.takeAs<Expr>(); | ||||
8741 | Context.setBlockVarCopyInits(var, init); | ||||
8742 | } | ||||
8743 | } | ||||
8744 | } | ||||
8745 | |||||
Richard Smith | 66f8571 | 2011-11-07 22:16:17 +0000 | [diff] [blame] | 8746 | Expr *Init = var->getInit(); |
8747 | bool IsGlobal = var->hasGlobalStorage() && !var->isStaticLocal(); | ||||
Richard Smith | a67d503 | 2012-11-09 23:03:14 +0000 | [diff] [blame] | 8748 | QualType baseType = Context.getBaseElementType(type); |
Richard Smith | 66f8571 | 2011-11-07 22:16:17 +0000 | [diff] [blame] | 8749 | |
Richard Smith | 9568f0c | 2012-10-29 18:26:47 +0000 | [diff] [blame] | 8750 | if (!var->getDeclContext()->isDependentContext() && |
8751 | Init && !Init->isValueDependent()) { | ||||
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 8752 | if (IsGlobal && !var->isConstexpr() && |
8753 | getDiagnostics().getDiagnosticLevel(diag::warn_global_constructor, | ||||
8754 | var->getLocation()) | ||||
Eli Friedman | 21cde05 | 2013-07-16 22:40:53 +0000 | [diff] [blame] | 8755 | != DiagnosticsEngine::Ignored) { |
8756 | // Warn about globals which don't have a constant initializer. Don't | ||||
8757 | // warn about globals with a non-trivial destructor because we already | ||||
8758 | // warned about them. | ||||
8759 | CXXRecordDecl *RD = baseType->getAsCXXRecordDecl(); | ||||
8760 | if (!(RD && !RD->hasTrivialDestructor()) && | ||||
8761 | !Init->isConstantInitializer(Context, baseType->isReferenceType())) | ||||
8762 | Diag(var->getLocation(), diag::warn_global_constructor) | ||||
8763 | << Init->getSourceRange(); | ||||
8764 | } | ||||
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 8765 | |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 8766 | if (var->isConstexpr()) { |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 8767 | SmallVector<PartialDiagnosticAt, 8> Notes; |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 8768 | if (!var->evaluateValue(Notes) || !var->isInitICE()) { |
8769 | SourceLocation DiagLoc = var->getLocation(); | ||||
8770 | // If the note doesn't add any useful information other than a source | ||||
8771 | // location, fold it into the primary diagnostic. | ||||
8772 | if (Notes.size() == 1 && Notes[0].second.getDiagID() == | ||||
8773 | diag::note_invalid_subexpr_in_const_expr) { | ||||
8774 | DiagLoc = Notes[0].first; | ||||
8775 | Notes.clear(); | ||||
8776 | } | ||||
8777 | Diag(DiagLoc, diag::err_constexpr_var_requires_const_init) | ||||
8778 | << var << Init->getSourceRange(); | ||||
8779 | for (unsigned I = 0, N = Notes.size(); I != N; ++I) | ||||
8780 | Diag(Notes[I].first, Notes[I].second); | ||||
8781 | } | ||||
Daniel Dunbar | 3d13c5a | 2012-03-09 01:51:51 +0000 | [diff] [blame] | 8782 | } else if (var->isUsableInConstantExpressions(Context)) { |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 8783 | // Check whether the initializer of a const variable of integral or |
8784 | // enumeration type is an ICE now, since we can't tell whether it was | ||||
8785 | // initialized by a constant expression if we check later. | ||||
8786 | var->checkInitIsICE(); | ||||
8787 | } | ||||
Richard Smith | 66f8571 | 2011-11-07 22:16:17 +0000 | [diff] [blame] | 8788 | } |
John McCall | 2998d6b | 2011-01-19 11:48:09 +0000 | [diff] [blame] | 8789 | |
8790 | // Require the destructor. | ||||
8791 | if (const RecordType *recordType = baseType->getAs<RecordType>()) | ||||
8792 | FinalizeVarWithDestructor(var, recordType); | ||||
8793 | } | ||||
8794 | |||||
Richard Smith | 483b9f3 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 8795 | /// FinalizeDeclaration - called by ParseDeclarationAfterDeclarator to perform |
8796 | /// any semantic actions necessary after any initializer has been attached. | ||||
8797 | void | ||||
8798 | Sema::FinalizeDeclaration(Decl *ThisDecl) { | ||||
8799 | // Note that we are no longer parsing the initializer for this declaration. | ||||
8800 | ParsingInitForAutoVars.erase(ThisDecl); | ||||
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 8801 | |
Rafael Espindola | 4c8cba8 | 2013-02-22 17:59:16 +0000 | [diff] [blame] | 8802 | VarDecl *VD = dyn_cast_or_null<VarDecl>(ThisDecl); |
Rafael Espindola | da844b3 | 2013-01-03 04:05:19 +0000 | [diff] [blame] | 8803 | if (!VD) |
8804 | return; | ||||
8805 | |||||
Rafael Espindola | 29535ba | 2013-08-16 23:18:50 +0000 | [diff] [blame] | 8806 | if (UsedAttr *Attr = VD->getAttr<UsedAttr>()) { |
8807 | if (!Attr->isInherited() && !VD->isThisDeclarationADefinition()) { | ||||
8808 | Diag(Attr->getLocation(), diag::warn_attribute_ignored) << "used"; | ||||
8809 | VD->dropAttr<UsedAttr>(); | ||||
8810 | } | ||||
8811 | } | ||||
8812 | |||||
Rafael Espindola | 4c8cba8 | 2013-02-22 17:59:16 +0000 | [diff] [blame] | 8813 | const DeclContext *DC = VD->getDeclContext(); |
8814 | // If there's a #pragma GCC visibility in scope, and this isn't a class | ||||
8815 | // member, set the visibility of this variable. | ||||
Rafael Espindola | 181e3ec | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 8816 | if (!DC->isRecord() && VD->isExternallyVisible()) |
Rafael Espindola | 4c8cba8 | 2013-02-22 17:59:16 +0000 | [diff] [blame] | 8817 | AddPushedVisibilityAttribute(VD); |
8818 | |||||
Rafael Espindola | 6769ccb | 2013-01-03 04:29:20 +0000 | [diff] [blame] | 8819 | if (VD->isFileVarDecl()) |
8820 | MarkUnusedFileScopedDecl(VD); | ||||
8821 | |||||
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 8822 | // Now we have parsed the initializer and can update the table of magic |
8823 | // tag values. | ||||
Rafael Espindola | da844b3 | 2013-01-03 04:05:19 +0000 | [diff] [blame] | 8824 | if (!VD->hasAttr<TypeTagForDatatypeAttr>() || |
8825 | !VD->getType()->isIntegralOrEnumerationType()) | ||||
8826 | return; | ||||
8827 | |||||
8828 | for (specific_attr_iterator<TypeTagForDatatypeAttr> | ||||
8829 | I = ThisDecl->specific_attr_begin<TypeTagForDatatypeAttr>(), | ||||
8830 | E = ThisDecl->specific_attr_end<TypeTagForDatatypeAttr>(); | ||||
8831 | I != E; ++I) { | ||||
8832 | const Expr *MagicValueExpr = VD->getInit(); | ||||
8833 | if (!MagicValueExpr) { | ||||
8834 | continue; | ||||
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 8835 | } |
Rafael Espindola | da844b3 | 2013-01-03 04:05:19 +0000 | [diff] [blame] | 8836 | llvm::APSInt MagicValueInt; |
8837 | if (!MagicValueExpr->isIntegerConstantExpr(MagicValueInt, Context)) { | ||||
8838 | Diag(I->getRange().getBegin(), | ||||
8839 | diag::err_type_tag_for_datatype_not_ice) | ||||
8840 | << LangOpts.CPlusPlus << MagicValueExpr->getSourceRange(); | ||||
8841 | continue; | ||||
8842 | } | ||||
8843 | if (MagicValueInt.getActiveBits() > 64) { | ||||
8844 | Diag(I->getRange().getBegin(), | ||||
8845 | diag::err_type_tag_for_datatype_too_large) | ||||
8846 | << LangOpts.CPlusPlus << MagicValueExpr->getSourceRange(); | ||||
8847 | continue; | ||||
8848 | } | ||||
8849 | uint64_t MagicValue = MagicValueInt.getZExtValue(); | ||||
8850 | RegisterTypeTagForDatatype(I->getArgumentKind(), | ||||
8851 | MagicValue, | ||||
8852 | I->getMatchingCType(), | ||||
8853 | I->getLayoutCompatible(), | ||||
8854 | I->getMustBeNull()); | ||||
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 8855 | } |
Richard Smith | 483b9f3 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 8856 | } |
8857 | |||||
Rafael Espindola | 4549d7f | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 8858 | Sema::DeclGroupPtrTy Sema::FinalizeDeclaratorGroup(Scope *S, const DeclSpec &DS, |
8859 | ArrayRef<Decl *> Group) { | ||||
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 8860 | SmallVector<Decl*, 8> Decls; |
Eli Friedman | c1dc653 | 2009-05-29 01:49:24 +0000 | [diff] [blame] | 8861 | |
8862 | if (DS.isTypeSpecOwned()) | ||||
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 8863 | Decls.push_back(DS.getRepAsDecl()); |
Eli Friedman | c1dc653 | 2009-05-29 01:49:24 +0000 | [diff] [blame] | 8864 | |
David Majnemer | aa82461 | 2013-09-17 23:57:10 +0000 | [diff] [blame] | 8865 | DeclaratorDecl *FirstDeclaratorInGroup = 0; |
Rafael Espindola | 4549d7f | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 8866 | for (unsigned i = 0, e = Group.size(); i != e; ++i) |
David Majnemer | aa82461 | 2013-09-17 23:57:10 +0000 | [diff] [blame] | 8867 | if (Decl *D = Group[i]) { |
8868 | if (DeclaratorDecl *DD = dyn_cast<DeclaratorDecl>(D)) | ||||
8869 | if (!FirstDeclaratorInGroup) | ||||
8870 | FirstDeclaratorInGroup = DD; | ||||
Richard Smith | 406c38e | 2011-02-23 00:37:57 +0000 | [diff] [blame] | 8871 | Decls.push_back(D); |
David Majnemer | aa82461 | 2013-09-17 23:57:10 +0000 | [diff] [blame] | 8872 | } |
Richard Smith | 406c38e | 2011-02-23 00:37:57 +0000 | [diff] [blame] | 8873 | |
Eli Friedman | 5e867c8 | 2013-07-10 00:30:46 +0000 | [diff] [blame] | 8874 | if (DeclSpec::isDeclRep(DS.getTypeSpecType())) { |
David Majnemer | aa82461 | 2013-09-17 23:57:10 +0000 | [diff] [blame] | 8875 | if (TagDecl *Tag = dyn_cast_or_null<TagDecl>(DS.getRepAsDecl())) { |
Eli Friedman | 5e867c8 | 2013-07-10 00:30:46 +0000 | [diff] [blame] | 8876 | HandleTagNumbering(*this, Tag); |
David Majnemer | aa82461 | 2013-09-17 23:57:10 +0000 | [diff] [blame] | 8877 | if (!Tag->hasNameForLinkage() && !Tag->hasDeclaratorForAnonDecl()) |
8878 | Tag->setDeclaratorForAnonDecl(FirstDeclaratorInGroup); | ||||
8879 | } | ||||
Eli Friedman | 5e867c8 | 2013-07-10 00:30:46 +0000 | [diff] [blame] | 8880 | } |
David Blaikie | 66cff72 | 2012-11-14 01:52:05 +0000 | [diff] [blame] | 8881 | |
Rafael Espindola | 4549d7f | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 8882 | return BuildDeclaratorGroup(Decls, DS.containsPlaceholderType()); |
Richard Smith | 406c38e | 2011-02-23 00:37:57 +0000 | [diff] [blame] | 8883 | } |
8884 | |||||
8885 | /// BuildDeclaratorGroup - convert a list of declarations into a declaration | ||||
8886 | /// group, performing any necessary semantic checking. | ||||
8887 | Sema::DeclGroupPtrTy | ||||
Rafael Espindola | 4549d7f | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 8888 | Sema::BuildDeclaratorGroup(llvm::MutableArrayRef<Decl *> Group, |
Richard Smith | 406c38e | 2011-02-23 00:37:57 +0000 | [diff] [blame] | 8889 | bool TypeMayContainAuto) { |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 8890 | // C++0x [dcl.spec.auto]p7: |
8891 | // If the type deduced for the template parameter U is not the same in each | ||||
8892 | // deduction, the program is ill-formed. | ||||
8893 | // FIXME: When initializer-list support is added, a distinction is needed | ||||
8894 | // between the deduced type U and the deduced type which 'auto' stands for. | ||||
8895 | // auto a = 0, b = { 1, 2, 3 }; | ||||
8896 | // is legal because the deduced type U is 'int' in both cases. | ||||
Rafael Espindola | 4549d7f | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 8897 | if (TypeMayContainAuto && Group.size() > 1) { |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 8898 | QualType Deduced; |
8899 | CanQualType DeducedCanon; | ||||
8900 | VarDecl *DeducedDecl = 0; | ||||
Rafael Espindola | 4549d7f | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 8901 | for (unsigned i = 0, e = Group.size(); i != e; ++i) { |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 8902 | if (VarDecl *D = dyn_cast<VarDecl>(Group[i])) { |
8903 | AutoType *AT = D->getType()->getContainedAutoType(); | ||||
Richard Smith | 406c38e | 2011-02-23 00:37:57 +0000 | [diff] [blame] | 8904 | // Don't reissue diagnostics when instantiating a template. |
8905 | if (AT && D->isInvalidDecl()) | ||||
8906 | break; | ||||
Richard Smith | dc7a4f5 | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 8907 | QualType U = AT ? AT->getDeducedType() : QualType(); |
8908 | if (!U.isNull()) { | ||||
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 8909 | CanQualType UCanon = Context.getCanonicalType(U); |
8910 | if (Deduced.isNull()) { | ||||
8911 | Deduced = U; | ||||
8912 | DeducedCanon = UCanon; | ||||
8913 | DeducedDecl = D; | ||||
8914 | } else if (DeducedCanon != UCanon) { | ||||
Richard Smith | 406c38e | 2011-02-23 00:37:57 +0000 | [diff] [blame] | 8915 | Diag(D->getTypeSourceInfo()->getTypeLoc().getBeginLoc(), |
8916 | diag::err_auto_different_deductions) | ||||
Richard Smith | ffd015e | 2013-05-04 04:19:27 +0000 | [diff] [blame] | 8917 | << (AT->isDecltypeAuto() ? 1 : 0) |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 8918 | << Deduced << DeducedDecl->getDeclName() |
8919 | << U << D->getDeclName() | ||||
8920 | << DeducedDecl->getInit()->getSourceRange() | ||||
8921 | << D->getInit()->getSourceRange(); | ||||
Richard Smith | 406c38e | 2011-02-23 00:37:57 +0000 | [diff] [blame] | 8922 | D->setInvalidDecl(); |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 8923 | break; |
8924 | } | ||||
8925 | } | ||||
8926 | } | ||||
8927 | } | ||||
8928 | } | ||||
8929 | |||||
Rafael Espindola | 4549d7f | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 8930 | ActOnDocumentableDecls(Group); |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 8931 | |
Rafael Espindola | 4549d7f | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 8932 | return DeclGroupPtrTy::make( |
8933 | DeclGroupRef::Create(Context, Group.data(), Group.size())); | ||||
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8934 | } |
Steve Naroff | e1223f7 | 2007-08-28 03:03:08 +0000 | [diff] [blame] | 8935 | |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 8936 | void Sema::ActOnDocumentableDecl(Decl *D) { |
Rafael Espindola | 4549d7f | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 8937 | ActOnDocumentableDecls(D); |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 8938 | } |
8939 | |||||
Rafael Espindola | 4549d7f | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 8940 | void Sema::ActOnDocumentableDecls(ArrayRef<Decl *> Group) { |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 8941 | // Don't parse the comment if Doxygen diagnostics are ignored. |
Rafael Espindola | 4549d7f | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 8942 | if (Group.empty() || !Group[0]) |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 8943 | return; |
8944 | |||||
8945 | if (Diags.getDiagnosticLevel(diag::warn_doc_param_not_found, | ||||
8946 | Group[0]->getLocation()) | ||||
8947 | == DiagnosticsEngine::Ignored) | ||||
8948 | return; | ||||
8949 | |||||
Rafael Espindola | 4549d7f | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 8950 | if (Group.size() >= 2) { |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 8951 | // This is a decl group. Normally it will contain only declarations |
Rafael Espindola | 4549d7f | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 8952 | // produced from declarator list. But in case we have any definitions or |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 8953 | // additional declaration references: |
8954 | // 'typedef struct S {} S;' | ||||
8955 | // 'typedef struct S *S;' | ||||
8956 | // 'struct S *pS;' | ||||
8957 | // FinalizeDeclaratorGroup adds these as separate declarations. | ||||
8958 | Decl *MaybeTagDecl = Group[0]; | ||||
8959 | if (MaybeTagDecl && isa<TagDecl>(MaybeTagDecl)) { | ||||
Rafael Espindola | 4549d7f | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 8960 | Group = Group.slice(1); |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 8961 | } |
8962 | } | ||||
8963 | |||||
8964 | // See if there are any new comments that are not attached to a decl. | ||||
8965 | ArrayRef<RawComment *> Comments = Context.getRawCommentList().getComments(); | ||||
8966 | if (!Comments.empty() && | ||||
8967 | !Comments.back()->isAttached()) { | ||||
8968 | // There is at least one comment that not attached to a decl. | ||||
8969 | // Maybe it should be attached to one of these decls? | ||||
8970 | // | ||||
8971 | // Note that this way we pick up not only comments that precede the | ||||
8972 | // declaration, but also comments that *follow* the declaration -- thanks to | ||||
8973 | // the lookahead in the lexer: we've consumed the semicolon and looked | ||||
8974 | // ahead through comments. | ||||
Rafael Espindola | 4549d7f | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 8975 | for (unsigned i = 0, e = Group.size(); i != e; ++i) |
Dmitri Gribenko | 1952354 | 2012-09-29 11:40:46 +0000 | [diff] [blame] | 8976 | Context.getCommentForDecl(Group[i], &PP); |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 8977 | } |
8978 | } | ||||
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 8979 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 8980 | /// ActOnParamDeclarator - Called from Parser::ParseFunctionDeclarator() |
8981 | /// to introduce parameters into function prototype scope. | ||||
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8982 | Decl *Sema::ActOnParamDeclarator(Scope *S, Declarator &D) { |
Chris Lattner | 985abd9 | 2008-06-26 06:49:43 +0000 | [diff] [blame] | 8983 | const DeclSpec &DS = D.getDeclSpec(); |
Douglas Gregor | 584049d | 2008-12-15 23:53:10 +0000 | [diff] [blame] | 8984 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 8985 | // Verify C99 6.7.5.3p2: The only SCS allowed is 'register'. |
Faisal Vali | fad9e13 | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 8986 | |
Peter Collingbourne | 7a8a2e3 | 2011-10-21 11:55:09 +0000 | [diff] [blame] | 8987 | // C++03 [dcl.stc]p2 also permits 'auto'. |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 8988 | VarDecl::StorageClass StorageClass = SC_None; |
Daniel Dunbar | 33ad012 | 2008-09-03 21:54:21 +0000 | [diff] [blame] | 8989 | if (DS.getStorageClassSpec() == DeclSpec::SCS_register) { |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 8990 | StorageClass = SC_Register; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8991 | } else if (getLangOpts().CPlusPlus && |
Peter Collingbourne | 7a8a2e3 | 2011-10-21 11:55:09 +0000 | [diff] [blame] | 8992 | DS.getStorageClassSpec() == DeclSpec::SCS_auto) { |
8993 | StorageClass = SC_Auto; | ||||
Daniel Dunbar | 33ad012 | 2008-09-03 21:54:21 +0000 | [diff] [blame] | 8994 | } else if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified) { |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 8995 | Diag(DS.getStorageClassSpecLoc(), |
8996 | diag::err_invalid_storage_class_in_func_decl); | ||||
Chris Lattner | 985abd9 | 2008-06-26 06:49:43 +0000 | [diff] [blame] | 8997 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 8998 | } |
Eli Friedman | 63054b3 | 2009-04-19 20:27:55 +0000 | [diff] [blame] | 8999 | |
Richard Smith | ec64244 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 9000 | if (DeclSpec::TSCS TSCS = DS.getThreadStorageClassSpec()) |
9001 | Diag(DS.getThreadStorageClassSpecLoc(), diag::err_invalid_thread) | ||||
9002 | << DeclSpec::getSpecifierName(TSCS); | ||||
9003 | if (DS.isConstexprSpecified()) | ||||
9004 | Diag(DS.getConstexprSpecLoc(), diag::err_invalid_constexpr) | ||||
Richard Smith | af1fc7a | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 9005 | << 0; |
Eli Friedman | 63054b3 | 2009-04-19 20:27:55 +0000 | [diff] [blame] | 9006 | |
Richard Smith | ec64244 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 9007 | DiagnoseFunctionSpecifiers(DS); |
Eli Friedman | 85a5319 | 2009-04-07 19:37:57 +0000 | [diff] [blame] | 9008 | |
Argyrios Kyrtzidis | 3215398 | 2011-06-28 03:01:15 +0000 | [diff] [blame] | 9009 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 9010 | QualType parmDeclType = TInfo->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9011 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9012 | if (getLangOpts().CPlusPlus) { |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 9013 | // Check that there are no default arguments inside the type of this |
9014 | // parameter. | ||||
9015 | CheckExtraCXXDefaultArguments(D); | ||||
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 9016 | |
9017 | // Parameter declarators cannot be qualified (C++ [dcl.meaning]p1). | ||||
9018 | if (D.getCXXScopeSpec().isSet()) { | ||||
9019 | Diag(D.getIdentifierLoc(), diag::err_qualified_param_declarator) | ||||
9020 | << D.getCXXScopeSpec().getRange(); | ||||
9021 | D.getCXXScopeSpec().clear(); | ||||
9022 | } | ||||
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 9023 | } |
9024 | |||||
Sean Hunt | 7533a5b | 2010-11-03 01:07:06 +0000 | [diff] [blame] | 9025 | // Ensure we have a valid name |
9026 | IdentifierInfo *II = 0; | ||||
9027 | if (D.hasName()) { | ||||
9028 | II = D.getIdentifier(); | ||||
9029 | if (!II) { | ||||
9030 | Diag(D.getIdentifierLoc(), diag::err_bad_parameter_name) | ||||
9031 | << GetNameForDeclarator(D).getName().getAsString(); | ||||
9032 | D.setInvalidType(true); | ||||
9033 | } | ||||
9034 | } | ||||
9035 | |||||
Chris Lattner | d84aac1 | 2010-02-22 00:40:25 +0000 | [diff] [blame] | 9036 | // 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] | 9037 | if (II) { |
John McCall | 10f2873 | 2010-03-18 06:42:38 +0000 | [diff] [blame] | 9038 | LookupResult R(*this, II, D.getIdentifierLoc(), LookupOrdinaryName, |
9039 | ForRedeclaration); | ||||
9040 | LookupName(R, S); | ||||
9041 | if (R.isSingleResult()) { | ||||
9042 | NamedDecl *PrevDecl = R.getFoundDecl(); | ||||
Chris Lattner | cf79b01 | 2009-01-21 02:38:50 +0000 | [diff] [blame] | 9043 | if (PrevDecl->isTemplateParameter()) { |
9044 | // Maybe we will complain about the shadowed template parameter. | ||||
9045 | DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), PrevDecl); | ||||
9046 | // Just pretend that we didn't see the previous declaration. | ||||
9047 | PrevDecl = 0; | ||||
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 9048 | } else if (S->isDeclScope(PrevDecl)) { |
Chris Lattner | cf79b01 | 2009-01-21 02:38:50 +0000 | [diff] [blame] | 9049 | Diag(D.getIdentifierLoc(), diag::err_param_redefinition) << II; |
Chris Lattner | d84aac1 | 2010-02-22 00:40:25 +0000 | [diff] [blame] | 9050 | Diag(PrevDecl->getLocation(), diag::note_previous_declaration); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 9051 | |
Chris Lattner | cf79b01 | 2009-01-21 02:38:50 +0000 | [diff] [blame] | 9052 | // Recover by removing the name |
9053 | II = 0; | ||||
9054 | D.SetIdentifier(0, D.getIdentifierLoc()); | ||||
Fariborz Jahanian | 9a66c30 | 2010-02-12 21:53:14 +0000 | [diff] [blame] | 9055 | D.setInvalidType(true); |
Chris Lattner | cf79b01 | 2009-01-21 02:38:50 +0000 | [diff] [blame] | 9056 | } |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 9057 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 9058 | } |
Steve Naroff | 6a9f3e3 | 2007-08-07 22:44:21 +0000 | [diff] [blame] | 9059 | |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 9060 | // Temporarily put parameter variables in the translation unit, not |
9061 | // the enclosing context. This prevents them from accidentally | ||||
9062 | // looking like class members in C++. | ||||
Douglas Gregor | cb27b0f | 2010-04-12 07:48:19 +0000 | [diff] [blame] | 9063 | ParmVarDecl *New = CheckParameter(Context.getTranslationUnitDecl(), |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 9064 | D.getLocStart(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 9065 | D.getIdentifierLoc(), II, |
9066 | parmDeclType, TInfo, | ||||
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 9067 | StorageClass); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9068 | |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 9069 | if (D.isInvalidType()) |
John McCall | fb44de9 | 2011-05-01 22:35:37 +0000 | [diff] [blame] | 9070 | New->setInvalidDecl(); |
9071 | |||||
9072 | assert(S->isFunctionPrototypeScope()); | ||||
9073 | assert(S->getFunctionPrototypeDepth() >= 1); | ||||
9074 | New->setScopeInfo(S->getFunctionPrototypeDepth() - 1, | ||||
9075 | S->getNextFunctionPrototypeIndex()); | ||||
Douglas Gregor | cb27b0f | 2010-04-12 07:48:19 +0000 | [diff] [blame] | 9076 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 9077 | // Add the parameter declaration into this scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 9078 | S->AddDecl(New); |
Argyrios Kyrtzidis | 87f3ff0 | 2008-04-12 00:47:19 +0000 | [diff] [blame] | 9079 | if (II) |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 9080 | IdResolver.AddDecl(New); |
Nate Begeman | b7894b5 | 2008-02-17 21:20:31 +0000 | [diff] [blame] | 9081 | |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 9082 | ProcessDeclAttributes(S, New, D); |
Mike Stump | ea000bf | 2009-04-30 00:19:40 +0000 | [diff] [blame] | 9083 | |
Douglas Gregor | e389585 | 2011-09-12 18:37:38 +0000 | [diff] [blame] | 9084 | if (D.getDeclSpec().isModulePrivateSpecified()) |
9085 | Diag(New->getLocation(), diag::err_module_private_local) | ||||
9086 | << 1 << New->getDeclName() | ||||
9087 | << SourceRange(D.getDeclSpec().getModulePrivateSpecLoc()) | ||||
9088 | << FixItHint::CreateRemoval(D.getDeclSpec().getModulePrivateSpecLoc()); | ||||
9089 | |||||
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 9090 | if (New->hasAttr<BlocksAttr>()) { |
Mike Stump | ea000bf | 2009-04-30 00:19:40 +0000 | [diff] [blame] | 9091 | Diag(New->getLocation(), diag::err_block_on_nonlocal); |
9092 | } | ||||
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 9093 | return New; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 9094 | } |
Fariborz Jahanian | 306d68f | 2007-11-08 23:49:49 +0000 | [diff] [blame] | 9095 | |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9096 | /// \brief Synthesizes a variable for a parameter arising from a |
9097 | /// typedef. | ||||
9098 | ParmVarDecl *Sema::BuildParmVarDeclForTypedef(DeclContext *DC, | ||||
9099 | SourceLocation Loc, | ||||
9100 | QualType T) { | ||||
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 9101 | /* FIXME: setting StartLoc == Loc. |
9102 | Would it be worth to modify callers so as to provide proper source | ||||
9103 | location for the unnamed parameters, embedding the parameter's type? */ | ||||
9104 | ParmVarDecl *Param = ParmVarDecl::Create(Context, DC, Loc, Loc, 0, | ||||
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9105 | T, Context.getTrivialTypeSourceInfo(T, Loc), |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 9106 | SC_None, 0); |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9107 | Param->setImplicit(); |
9108 | return Param; | ||||
9109 | } | ||||
9110 | |||||
John McCall | fbce0e1 | 2010-08-24 09:05:15 +0000 | [diff] [blame] | 9111 | void Sema::DiagnoseUnusedParameters(ParmVarDecl * const *Param, |
9112 | ParmVarDecl * const *ParamEnd) { | ||||
John McCall | fbce0e1 | 2010-08-24 09:05:15 +0000 | [diff] [blame] | 9113 | // Don't diagnose unused-parameter errors in template instantiations; we |
9114 | // will already have done so in the template itself. | ||||
9115 | if (!ActiveTemplateInstantiations.empty()) | ||||
9116 | return; | ||||
9117 | |||||
9118 | for (; Param != ParamEnd; ++Param) { | ||||
Eli Friedman | dd9d645 | 2012-01-13 23:41:25 +0000 | [diff] [blame] | 9119 | if (!(*Param)->isReferenced() && (*Param)->getDeclName() && |
John McCall | fbce0e1 | 2010-08-24 09:05:15 +0000 | [diff] [blame] | 9120 | !(*Param)->hasAttr<UnusedAttr>()) { |
9121 | Diag((*Param)->getLocation(), diag::warn_unused_parameter) | ||||
9122 | << (*Param)->getDeclName(); | ||||
9123 | } | ||||
9124 | } | ||||
9125 | } | ||||
9126 | |||||
Argyrios Kyrtzidis | 3532fdd | 2010-11-17 23:11:54 +0000 | [diff] [blame] | 9127 | void Sema::DiagnoseSizeOfParametersAndReturnValue(ParmVarDecl * const *Param, |
9128 | ParmVarDecl * const *ParamEnd, | ||||
9129 | QualType ReturnTy, | ||||
9130 | NamedDecl *D) { | ||||
Argyrios Kyrtzidis | 1380a14 | 2010-11-18 00:20:36 +0000 | [diff] [blame] | 9131 | if (LangOpts.NumLargeByValueCopy == 0) // No check. |
Argyrios Kyrtzidis | 3532fdd | 2010-11-17 23:11:54 +0000 | [diff] [blame] | 9132 | return; |
9133 | |||||
Argyrios Kyrtzidis | 1380a14 | 2010-11-18 00:20:36 +0000 | [diff] [blame] | 9134 | // Warn if the return value is pass-by-value and larger than the specified |
9135 | // threshold. | ||||
Eli Friedman | d18840d | 2012-01-09 23:46:59 +0000 | [diff] [blame] | 9136 | if (!ReturnTy->isDependentType() && ReturnTy.isPODType(Context)) { |
Argyrios Kyrtzidis | 3532fdd | 2010-11-17 23:11:54 +0000 | [diff] [blame] | 9137 | unsigned Size = Context.getTypeSizeInChars(ReturnTy).getQuantity(); |
Argyrios Kyrtzidis | 1380a14 | 2010-11-18 00:20:36 +0000 | [diff] [blame] | 9138 | if (Size > LangOpts.NumLargeByValueCopy) |
Argyrios Kyrtzidis | 3532fdd | 2010-11-17 23:11:54 +0000 | [diff] [blame] | 9139 | Diag(D->getLocation(), diag::warn_return_value_size) |
9140 | << D->getDeclName() << Size; | ||||
9141 | } | ||||
9142 | |||||
Argyrios Kyrtzidis | 1380a14 | 2010-11-18 00:20:36 +0000 | [diff] [blame] | 9143 | // Warn if any parameter is pass-by-value and larger than the specified |
9144 | // threshold. | ||||
Argyrios Kyrtzidis | 3532fdd | 2010-11-17 23:11:54 +0000 | [diff] [blame] | 9145 | for (; Param != ParamEnd; ++Param) { |
9146 | QualType T = (*Param)->getType(); | ||||
Eli Friedman | d18840d | 2012-01-09 23:46:59 +0000 | [diff] [blame] | 9147 | if (T->isDependentType() || !T.isPODType(Context)) |
Argyrios Kyrtzidis | 3532fdd | 2010-11-17 23:11:54 +0000 | [diff] [blame] | 9148 | continue; |
9149 | unsigned Size = Context.getTypeSizeInChars(T).getQuantity(); | ||||
Argyrios Kyrtzidis | 1380a14 | 2010-11-18 00:20:36 +0000 | [diff] [blame] | 9150 | if (Size > LangOpts.NumLargeByValueCopy) |
Argyrios Kyrtzidis | 3532fdd | 2010-11-17 23:11:54 +0000 | [diff] [blame] | 9151 | Diag((*Param)->getLocation(), diag::warn_parameter_size) |
9152 | << (*Param)->getDeclName() << Size; | ||||
9153 | } | ||||
9154 | } | ||||
9155 | |||||
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 9156 | ParmVarDecl *Sema::CheckParameter(DeclContext *DC, SourceLocation StartLoc, |
9157 | SourceLocation NameLoc, IdentifierInfo *Name, | ||||
9158 | QualType T, TypeSourceInfo *TSInfo, | ||||
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 9159 | VarDecl::StorageClass StorageClass) { |
Reid Kleckner | c910d4c | 2013-06-08 18:19:52 +0000 | [diff] [blame] | 9160 | // In ARC, infer a lifetime qualifier for appropriate parameter types. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9161 | if (getLangOpts().ObjCAutoRefCount && |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9162 | T.getObjCLifetime() == Qualifiers::OCL_None && |
Reid Kleckner | c910d4c | 2013-06-08 18:19:52 +0000 | [diff] [blame] | 9163 | T->isObjCLifetimeType()) { |
9164 | |||||
9165 | Qualifiers::ObjCLifetime lifetime; | ||||
9166 | |||||
9167 | // Special cases for arrays: | ||||
9168 | // - if it's const, use __unsafe_unretained | ||||
9169 | // - otherwise, it's an error | ||||
9170 | if (T->isArrayType()) { | ||||
9171 | if (!T.isConstQualified()) { | ||||
9172 | DelayedDiagnostics.add( | ||||
9173 | sema::DelayedDiagnostic::makeForbiddenType( | ||||
Fariborz Jahanian | 175fb10 | 2011-10-03 22:11:57 +0000 | [diff] [blame] | 9174 | NameLoc, diag::err_arc_array_param_no_ownership, T, false)); |
Reid Kleckner | c910d4c | 2013-06-08 18:19:52 +0000 | [diff] [blame] | 9175 | } |
9176 | lifetime = Qualifiers::OCL_ExplicitNone; | ||||
9177 | } else { | ||||
9178 | lifetime = T->getObjCARCImplicitLifetime(); | ||||
9179 | } | ||||
9180 | T = Context.getLifetimeQualifiedType(T, lifetime); | ||||
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9181 | } |
9182 | |||||
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 9183 | ParmVarDecl *New = ParmVarDecl::Create(Context, DC, StartLoc, NameLoc, Name, |
Douglas Gregor | 79e6bd3 | 2011-07-12 04:42:08 +0000 | [diff] [blame] | 9184 | Context.getAdjustedParameterType(T), |
9185 | TSInfo, | ||||
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 9186 | StorageClass, 0); |
Douglas Gregor | cb27b0f | 2010-04-12 07:48:19 +0000 | [diff] [blame] | 9187 | |
9188 | // Parameters can not be abstract class types. | ||||
9189 | // For record types, this is done by the AbstractClassUsageDiagnoser once | ||||
9190 | // the class has been completely parsed. | ||||
9191 | if (!CurContext->isRecord() && | ||||
9192 | RequireNonAbstractType(NameLoc, T, diag::err_abstract_type_in_decl, | ||||
9193 | AbstractParamType)) | ||||
9194 | New->setInvalidDecl(); | ||||
9195 | |||||
9196 | // Parameter declarators cannot be interface types. All ObjC objects are | ||||
9197 | // passed by reference. | ||||
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 9198 | if (T->isObjCObjectType()) { |
Fariborz Jahanian | 1de6a6c | 2012-05-09 21:49:29 +0000 | [diff] [blame] | 9199 | SourceLocation TypeEndLoc = TSInfo->getTypeLoc().getLocEnd(); |
Douglas Gregor | cb27b0f | 2010-04-12 07:48:19 +0000 | [diff] [blame] | 9200 | Diag(NameLoc, |
Fariborz Jahanian | 8eaefdc | 2011-07-26 17:58:54 +0000 | [diff] [blame] | 9201 | diag::err_object_cannot_be_passed_returned_by_value) << 1 << T |
Fariborz Jahanian | 1de6a6c | 2012-05-09 21:49:29 +0000 | [diff] [blame] | 9202 | << FixItHint::CreateInsertion(TypeEndLoc, "*"); |
Fariborz Jahanian | 8eaefdc | 2011-07-26 17:58:54 +0000 | [diff] [blame] | 9203 | T = Context.getObjCObjectPointerType(T); |
9204 | New->setType(T); | ||||
Douglas Gregor | cb27b0f | 2010-04-12 07:48:19 +0000 | [diff] [blame] | 9205 | } |
9206 | |||||
9207 | // ISO/IEC TR 18037 S6.7.3: "The type of an object with automatic storage | ||||
9208 | // duration shall not be qualified by an address-space qualifier." | ||||
9209 | // Since all parameters have automatic store duration, they can not have | ||||
9210 | // an address space. | ||||
9211 | if (T.getAddressSpace() != 0) { | ||||
9212 | Diag(NameLoc, diag::err_arg_with_address_space); | ||||
9213 | New->setInvalidDecl(); | ||||
9214 | } | ||||
9215 | |||||
9216 | return New; | ||||
9217 | } | ||||
9218 | |||||
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 9219 | void Sema::ActOnFinishKNRParamDeclarations(Scope *S, Declarator &D, |
9220 | SourceLocation LocAfterDecls) { | ||||
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 9221 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo(); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 9222 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 9223 | // Verify 6.9.1p6: 'every identifier in the identifier list shall be declared' |
9224 | // for a K&R function. | ||||
9225 | if (!FTI.hasPrototype) { | ||||
Douglas Gregor | 2610348 | 2009-04-02 03:14:12 +0000 | [diff] [blame] | 9226 | for (int i = FTI.NumArgs; i != 0; /* decrement in loop */) { |
9227 | --i; | ||||
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 9228 | if (FTI.ArgInfo[i].Param == 0) { |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 9229 | SmallString<256> Code; |
Daniel Dunbar | 5ffe14c | 2009-10-18 20:26:27 +0000 | [diff] [blame] | 9230 | llvm::raw_svector_ostream(Code) << " int " |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 9231 | << FTI.ArgInfo[i].Ident->getName() |
Daniel Dunbar | 5ffe14c | 2009-10-18 20:26:27 +0000 | [diff] [blame] | 9232 | << ";\n"; |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 9233 | Diag(FTI.ArgInfo[i].IdentLoc, diag::ext_param_not_declared) |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 9234 | << FTI.ArgInfo[i].Ident |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 9235 | << FixItHint::CreateInsertion(LocAfterDecls, Code.str()); |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 9236 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 9237 | // Implicitly declare the argument as type 'int' for lack of a better |
9238 | // type. | ||||
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 9239 | AttributeFactory attrs; |
9240 | DeclSpec DS(attrs); | ||||
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 9241 | const char* PrevSpec; // unused |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 9242 | unsigned DiagID; // unused |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9243 | DS.SetTypeSpecType(DeclSpec::TST_int, FTI.ArgInfo[i].IdentLoc, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 9244 | PrevSpec, DiagID); |
Abramo Bagnara | 16467f2 | 2012-10-04 21:38:29 +0000 | [diff] [blame] | 9245 | // Use the identifier location for the type source range. |
9246 | DS.SetRangeStart(FTI.ArgInfo[i].IdentLoc); | ||||
9247 | DS.SetRangeEnd(FTI.ArgInfo[i].IdentLoc); | ||||
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 9248 | Declarator ParamD(DS, Declarator::KNRTypeListContext); |
9249 | ParamD.SetIdentifier(FTI.ArgInfo[i].Ident, FTI.ArgInfo[i].IdentLoc); | ||||
Douglas Gregor | be109b3 | 2009-01-23 16:23:13 +0000 | [diff] [blame] | 9250 | FTI.ArgInfo[i].Param = ActOnParamDeclarator(S, ParamD); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 9251 | } |
9252 | } | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9253 | } |
Douglas Gregor | be109b3 | 2009-01-23 16:23:13 +0000 | [diff] [blame] | 9254 | } |
9255 | |||||
Richard Smith | 87162c2 | 2012-04-17 22:30:01 +0000 | [diff] [blame] | 9256 | Decl *Sema::ActOnStartOfFunctionDef(Scope *FnBodyScope, Declarator &D) { |
Douglas Gregor | be109b3 | 2009-01-23 16:23:13 +0000 | [diff] [blame] | 9257 | assert(getCurFunctionDecl() == 0 && "Function parsing confused"); |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 9258 | assert(D.isFunctionDeclarator() && "Not a function declarator!"); |
Douglas Gregor | 584049d | 2008-12-15 23:53:10 +0000 | [diff] [blame] | 9259 | Scope *ParentScope = FnBodyScope->getParent(); |
Steve Naroff | adbbd0c | 2008-01-14 20:51:29 +0000 | [diff] [blame] | 9260 | |
Douglas Gregor | 45fa560 | 2011-11-07 20:56:01 +0000 | [diff] [blame] | 9261 | D.setFunctionDefinitionKind(FDK_Definition); |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 9262 | Decl *DP = HandleDeclarator(ParentScope, D, MultiTemplateParamsArg()); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 9263 | return ActOnStartOfFunctionDef(FnBodyScope, DP); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 9264 | } |
9265 | |||||
Anders Carlsson | 8a0086c | 2012-12-18 01:29:20 +0000 | [diff] [blame] | 9266 | static bool ShouldWarnAboutMissingPrototype(const FunctionDecl *FD, |
9267 | const FunctionDecl*& PossibleZeroParamPrototype) { | ||||
Anders Carlsson | 9f89dd7 | 2009-12-09 03:30:09 +0000 | [diff] [blame] | 9268 | // Don't warn about invalid declarations. |
9269 | if (FD->isInvalidDecl()) | ||||
9270 | return false; | ||||
Anders Carlsson | 63fb673 | 2009-12-09 03:44:46 +0000 | [diff] [blame] | 9271 | |
Anders Carlsson | 9f89dd7 | 2009-12-09 03:30:09 +0000 | [diff] [blame] | 9272 | // Or declarations that aren't global. |
9273 | if (!FD->isGlobal()) | ||||
9274 | return false; | ||||
Anders Carlsson | 63fb673 | 2009-12-09 03:44:46 +0000 | [diff] [blame] | 9275 | |
Anders Carlsson | 9f89dd7 | 2009-12-09 03:30:09 +0000 | [diff] [blame] | 9276 | // Don't warn about C++ member functions. |
9277 | if (isa<CXXMethodDecl>(FD)) | ||||
9278 | return false; | ||||
Anders Carlsson | 63fb673 | 2009-12-09 03:44:46 +0000 | [diff] [blame] | 9279 | |
Anders Carlsson | 9f89dd7 | 2009-12-09 03:30:09 +0000 | [diff] [blame] | 9280 | // Don't warn about 'main'. |
9281 | if (FD->isMain()) | ||||
9282 | return false; | ||||
Anders Carlsson | 63fb673 | 2009-12-09 03:44:46 +0000 | [diff] [blame] | 9283 | |
Anders Carlsson | 9f89dd7 | 2009-12-09 03:30:09 +0000 | [diff] [blame] | 9284 | // Don't warn about inline functions. |
John McCall | 850d3b3 | 2011-03-22 07:16:37 +0000 | [diff] [blame] | 9285 | if (FD->isInlined()) |
Anders Carlsson | 9f89dd7 | 2009-12-09 03:30:09 +0000 | [diff] [blame] | 9286 | return false; |
Anders Carlsson | 63fb673 | 2009-12-09 03:44:46 +0000 | [diff] [blame] | 9287 | |
9288 | // Don't warn about function templates. | ||||
9289 | if (FD->getDescribedFunctionTemplate()) | ||||
9290 | return false; | ||||
9291 | |||||
9292 | // Don't warn about function template specializations. | ||||
9293 | if (FD->isFunctionTemplateSpecialization()) | ||||
9294 | return false; | ||||
9295 | |||||
Tanya Lattner | a95b4f7 | 2012-07-26 00:08:28 +0000 | [diff] [blame] | 9296 | // Don't warn for OpenCL kernels. |
9297 | if (FD->hasAttr<OpenCLKernelAttr>()) | ||||
9298 | return false; | ||||
Richard Smith | a41c97a | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 9299 | |
Anders Carlsson | 9f89dd7 | 2009-12-09 03:30:09 +0000 | [diff] [blame] | 9300 | bool MissingPrototype = true; |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 9301 | for (const FunctionDecl *Prev = FD->getPreviousDecl(); |
9302 | Prev; Prev = Prev->getPreviousDecl()) { | ||||
Anders Carlsson | 9f89dd7 | 2009-12-09 03:30:09 +0000 | [diff] [blame] | 9303 | // Ignore any declarations that occur in function or method |
9304 | // scope, because they aren't visible from the header. | ||||
Richard Smith | a41c97a | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 9305 | if (Prev->getLexicalDeclContext()->isFunctionOrMethod()) |
Anders Carlsson | 9f89dd7 | 2009-12-09 03:30:09 +0000 | [diff] [blame] | 9306 | continue; |
Richard Smith | a41c97a | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 9307 | |
Anders Carlsson | 9f89dd7 | 2009-12-09 03:30:09 +0000 | [diff] [blame] | 9308 | MissingPrototype = !Prev->getType()->isFunctionProtoType(); |
Anders Carlsson | 8a0086c | 2012-12-18 01:29:20 +0000 | [diff] [blame] | 9309 | if (FD->getNumParams() == 0) |
9310 | PossibleZeroParamPrototype = Prev; | ||||
Anders Carlsson | 9f89dd7 | 2009-12-09 03:30:09 +0000 | [diff] [blame] | 9311 | break; |
9312 | } | ||||
Richard Smith | a41c97a | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 9313 | |
Anders Carlsson | 9f89dd7 | 2009-12-09 03:30:09 +0000 | [diff] [blame] | 9314 | return MissingPrototype; |
9315 | } | ||||
9316 | |||||
Francois Pichet | d4a0caf | 2011-04-22 23:20:44 +0000 | [diff] [blame] | 9317 | void Sema::CheckForFunctionRedefinition(FunctionDecl *FD) { |
9318 | // Don't complain if we're in GNU89 mode and the previous definition | ||||
9319 | // was an extern inline function. | ||||
9320 | const FunctionDecl *Definition; | ||||
Sean Hunt | 10620eb | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 9321 | if (FD->isDefined(Definition) && |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9322 | !canRedefineFunction(Definition, getLangOpts())) { |
9323 | if (getLangOpts().GNUMode && Definition->isInlineSpecified() && | ||||
Francois Pichet | d4a0caf | 2011-04-22 23:20:44 +0000 | [diff] [blame] | 9324 | Definition->getStorageClass() == SC_Extern) |
9325 | Diag(FD->getLocation(), diag::err_redefinition_extern_inline) | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9326 | << FD->getDeclName() << getLangOpts().CPlusPlus; |
Francois Pichet | d4a0caf | 2011-04-22 23:20:44 +0000 | [diff] [blame] | 9327 | else |
9328 | Diag(FD->getLocation(), diag::err_redefinition) << FD->getDeclName(); | ||||
9329 | Diag(Definition->getLocation(), diag::note_previous_definition); | ||||
Richard Smith | eef0029 | 2012-08-06 02:25:10 +0000 | [diff] [blame] | 9330 | FD->setInvalidDecl(); |
Francois Pichet | d4a0caf | 2011-04-22 23:20:44 +0000 | [diff] [blame] | 9331 | } |
9332 | } | ||||
9333 | |||||
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 9334 | Decl *Sema::ActOnStartOfFunctionDef(Scope *FnBodyScope, Decl *D) { |
Anders Carlsson | 0ebb6d3 | 2009-10-29 15:46:07 +0000 | [diff] [blame] | 9335 | // Clear the last template instantiation error context. |
9336 | LastTemplateInstantiationErrorContext = ActiveTemplateInstantiation(); | ||||
9337 | |||||
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 9338 | if (!D) |
9339 | return D; | ||||
Douglas Gregor | d83d040 | 2009-08-22 00:34:47 +0000 | [diff] [blame] | 9340 | FunctionDecl *FD = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9341 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 9342 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D)) |
Douglas Gregor | d83d040 | 2009-08-22 00:34:47 +0000 | [diff] [blame] | 9343 | FD = FunTmpl->getTemplatedDecl(); |
9344 | else | ||||
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 9345 | FD = cast<FunctionDecl>(D); |
Faisal Vali | fad9e13 | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 9346 | // If we are instantiating a generic lambda call operator, push |
9347 | // a LambdaScopeInfo onto the function stack. But use the information | ||||
9348 | // that's already been calculated (ActOnLambdaExpr) when analyzing the | ||||
9349 | // template version, to prime the current LambdaScopeInfo. | ||||
Faisal Vali | 998c518 | 2013-09-29 20:15:45 +0000 | [diff] [blame] | 9350 | if (isGenericLambdaCallOperatorSpecialization(FD)) { |
Faisal Vali | fad9e13 | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 9351 | CXXMethodDecl *CallOperator = cast<CXXMethodDecl>(D); |
9352 | CXXRecordDecl *LambdaClass = CallOperator->getParent(); | ||||
9353 | LambdaExpr *LE = LambdaClass->getLambdaExpr(); | ||||
9354 | assert(LE && | ||||
9355 | "No LambdaExpr of closure class when instantiating a generic lambda!"); | ||||
9356 | assert(ActiveTemplateInstantiations.size() && | ||||
9357 | "There should be an active template instantiation on the stack " | ||||
9358 | "when instantiating a generic lambda!"); | ||||
9359 | PushLambdaScope(); | ||||
9360 | LambdaScopeInfo *LSI = getCurLambda(); | ||||
9361 | LSI->CallOperator = CallOperator; | ||||
9362 | LSI->Lambda = LambdaClass; | ||||
9363 | LSI->ReturnType = CallOperator->getResultType(); | ||||
Douglas Gregor | 6fc17ff | 2008-10-29 15:10:40 +0000 | [diff] [blame] | 9364 | |
Faisal Vali | fad9e13 | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 9365 | if (LE->getCaptureDefault() == LCD_None) |
9366 | LSI->ImpCaptureStyle = CapturingScopeInfo::ImpCap_None; | ||||
9367 | else if (LE->getCaptureDefault() == LCD_ByCopy) | ||||
9368 | LSI->ImpCaptureStyle = CapturingScopeInfo::ImpCap_LambdaByval; | ||||
9369 | else if (LE->getCaptureDefault() == LCD_ByRef) | ||||
9370 | LSI->ImpCaptureStyle = CapturingScopeInfo::ImpCap_LambdaByref; | ||||
9371 | |||||
9372 | LSI->IntroducerRange = LE->getIntroducerRange(); | ||||
9373 | } | ||||
9374 | else | ||||
9375 | // Enter a new function scope | ||||
9376 | PushFunctionScope(); | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9377 | |
Douglas Gregor | 6fc17ff | 2008-10-29 15:10:40 +0000 | [diff] [blame] | 9378 | // See if this is a redefinition. |
Francois Pichet | d4a0caf | 2011-04-22 23:20:44 +0000 | [diff] [blame] | 9379 | if (!FD->isLateTemplateParsed()) |
9380 | CheckForFunctionRedefinition(FD); | ||||
Douglas Gregor | 6fc17ff | 2008-10-29 15:10:40 +0000 | [diff] [blame] | 9381 | |
Douglas Gregor | cda9c67 | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 9382 | // Builtin functions cannot be defined. |
Douglas Gregor | 7814e6d | 2009-09-12 00:22:50 +0000 | [diff] [blame] | 9383 | if (unsigned BuiltinID = FD->getBuiltinID()) { |
Rafael Espindola | ad24ad4 | 2013-06-13 18:34:17 +0000 | [diff] [blame] | 9384 | if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID) && |
9385 | !Context.BuiltinInfo.isPredefinedRuntimeFunction(BuiltinID)) { | ||||
Douglas Gregor | cda9c67 | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 9386 | Diag(FD->getLocation(), diag::err_builtin_definition) << FD; |
Douglas Gregor | 655753a | 2009-02-17 16:03:01 +0000 | [diff] [blame] | 9387 | FD->setInvalidDecl(); |
9388 | } | ||||
Douglas Gregor | cda9c67 | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 9389 | } |
9390 | |||||
Eli Friedman | 7f0f5dc | 2009-03-04 07:30:59 +0000 | [diff] [blame] | 9391 | // The return type of a function definition must be complete |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 9392 | // (C99 6.9.1p3, C++ [dcl.fct]p6). |
9393 | QualType ResultType = FD->getResultType(); | ||||
9394 | if (!ResultType->isDependentType() && !ResultType->isVoidType() && | ||||
Chris Lattner | 65e6a09 | 2009-04-29 05:12:23 +0000 | [diff] [blame] | 9395 | !FD->isInvalidDecl() && |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 9396 | RequireCompleteType(FD->getLocation(), ResultType, |
9397 | diag::err_func_def_incomplete_result)) | ||||
Eli Friedman | 7f0f5dc | 2009-03-04 07:30:59 +0000 | [diff] [blame] | 9398 | FD->setInvalidDecl(); |
Eli Friedman | 7f0f5dc | 2009-03-04 07:30:59 +0000 | [diff] [blame] | 9399 | |
Douglas Gregor | 8499f3f | 2009-03-31 16:35:03 +0000 | [diff] [blame] | 9400 | // GNU warning -Wmissing-prototypes: |
9401 | // Warn if a global function is defined without a previous | ||||
9402 | // prototype declaration. This warning is issued even if the | ||||
9403 | // definition itself provides a prototype. The aim is to detect | ||||
9404 | // global functions that fail to be declared in header files. | ||||
Anders Carlsson | 8a0086c | 2012-12-18 01:29:20 +0000 | [diff] [blame] | 9405 | const FunctionDecl *PossibleZeroParamPrototype = 0; |
9406 | if (ShouldWarnAboutMissingPrototype(FD, PossibleZeroParamPrototype)) { | ||||
Anders Carlsson | 9f89dd7 | 2009-12-09 03:30:09 +0000 | [diff] [blame] | 9407 | Diag(FD->getLocation(), diag::warn_missing_prototype) << FD; |
Richard Smith | ac83a3c | 2013-06-25 20:34:17 +0000 | [diff] [blame] | 9408 | |
Anders Carlsson | 8a0086c | 2012-12-18 01:29:20 +0000 | [diff] [blame] | 9409 | if (PossibleZeroParamPrototype) { |
Richard Smith | ac83a3c | 2013-06-25 20:34:17 +0000 | [diff] [blame] | 9410 | // We found a declaration that is not a prototype, |
Anders Carlsson | 8a0086c | 2012-12-18 01:29:20 +0000 | [diff] [blame] | 9411 | // but that could be a zero-parameter prototype |
Richard Smith | ac83a3c | 2013-06-25 20:34:17 +0000 | [diff] [blame] | 9412 | if (TypeSourceInfo *TI = |
9413 | PossibleZeroParamPrototype->getTypeSourceInfo()) { | ||||
9414 | TypeLoc TL = TI->getTypeLoc(); | ||||
9415 | if (FunctionNoProtoTypeLoc FTL = TL.getAs<FunctionNoProtoTypeLoc>()) | ||||
9416 | Diag(PossibleZeroParamPrototype->getLocation(), | ||||
9417 | diag::note_declaration_not_a_prototype) | ||||
9418 | << PossibleZeroParamPrototype | ||||
9419 | << FixItHint::CreateInsertion(FTL.getRParenLoc(), "void"); | ||||
9420 | } | ||||
Anders Carlsson | 8a0086c | 2012-12-18 01:29:20 +0000 | [diff] [blame] | 9421 | } |
9422 | } | ||||
Douglas Gregor | 8499f3f | 2009-03-31 16:35:03 +0000 | [diff] [blame] | 9423 | |
Douglas Gregor | e2c31ff | 2009-05-15 17:59:04 +0000 | [diff] [blame] | 9424 | if (FnBodyScope) |
9425 | PushDeclContext(FnBodyScope, FD); | ||||
Anton Korobeynikov | 2f40270 | 2008-12-26 00:52:02 +0000 | [diff] [blame] | 9426 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 9427 | // Check the validity of our function parameters |
Douglas Gregor | 82aa713 | 2010-11-01 18:37:59 +0000 | [diff] [blame] | 9428 | CheckParmsForFunctionDef(FD->param_begin(), FD->param_end(), |
9429 | /*CheckParameterNames=*/true); | ||||
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 9430 | |
9431 | // Introduce our parameters into the function scope | ||||
9432 | for (unsigned p = 0, NumParams = FD->getNumParams(); p < NumParams; ++p) { | ||||
9433 | ParmVarDecl *Param = FD->getParamDecl(p); | ||||
Douglas Gregor | a8cc8ce | 2009-01-09 18:51:29 +0000 | [diff] [blame] | 9434 | Param->setOwningFunction(FD); |
9435 | |||||
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 9436 | // If this has an identifier, add it to the scope stack. |
John McCall | 053f4bd | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 9437 | if (Param->getIdentifier() && FnBodyScope) { |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 9438 | CheckShadow(FnBodyScope, Param); |
John McCall | 053f4bd | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 9439 | |
Argyrios Kyrtzidis | 87f3ff0 | 2008-04-12 00:47:19 +0000 | [diff] [blame] | 9440 | PushOnScopeChains(Param, FnBodyScope); |
John McCall | 053f4bd | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 9441 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 9442 | } |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 9443 | |
James Molloy | 16f1f71 | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 9444 | // If we had any tags defined in the function prototype, |
9445 | // introduce them into the function scope. | ||||
9446 | if (FnBodyScope) { | ||||
Robert Wilhelm | 834c058 | 2013-08-09 18:02:13 +0000 | [diff] [blame] | 9447 | for (ArrayRef<NamedDecl *>::iterator |
9448 | I = FD->getDeclsInPrototypeScope().begin(), | ||||
9449 | E = FD->getDeclsInPrototypeScope().end(); | ||||
9450 | I != E; ++I) { | ||||
James Molloy | 16f1f71 | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 9451 | NamedDecl *D = *I; |
9452 | |||||
9453 | // Some of these decls (like enums) may have been pinned to the translation unit | ||||
9454 | // for lack of a real context earlier. If so, remove from the translation unit | ||||
9455 | // and reattach to the current context. | ||||
9456 | if (D->getLexicalDeclContext() == Context.getTranslationUnitDecl()) { | ||||
9457 | // Is the decl actually in the context? | ||||
9458 | for (DeclContext::decl_iterator DI = Context.getTranslationUnitDecl()->decls_begin(), | ||||
9459 | DE = Context.getTranslationUnitDecl()->decls_end(); DI != DE; ++DI) { | ||||
9460 | if (*DI == D) { | ||||
9461 | Context.getTranslationUnitDecl()->removeDecl(D); | ||||
9462 | break; | ||||
9463 | } | ||||
9464 | } | ||||
9465 | // Either way, reassign the lexical decl context to our FunctionDecl. | ||||
9466 | D->setLexicalDeclContext(CurContext); | ||||
9467 | } | ||||
9468 | |||||
9469 | // If the decl has a non-null name, make accessible in the current scope. | ||||
9470 | if (!D->getName().empty()) | ||||
9471 | PushOnScopeChains(D, FnBodyScope, /*AddToContext=*/false); | ||||
9472 | |||||
9473 | // Similarly, dive into enums and fish their constants out, making them | ||||
9474 | // accessible in this scope. | ||||
9475 | if (EnumDecl *ED = dyn_cast<EnumDecl>(D)) { | ||||
9476 | for (EnumDecl::enumerator_iterator EI = ED->enumerator_begin(), | ||||
9477 | EE = ED->enumerator_end(); EI != EE; ++EI) | ||||
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 9478 | PushOnScopeChains(*EI, FnBodyScope, /*AddToContext=*/false); |
James Molloy | 16f1f71 | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 9479 | } |
9480 | } | ||||
9481 | } | ||||
9482 | |||||
Richard Smith | 87162c2 | 2012-04-17 22:30:01 +0000 | [diff] [blame] | 9483 | // Ensure that the function's exception specification is instantiated. |
9484 | if (const FunctionProtoType *FPT = FD->getType()->getAs<FunctionProtoType>()) | ||||
9485 | ResolveExceptionSpec(D->getLocation(), FPT); | ||||
9486 | |||||
Anton Korobeynikov | 2f40270 | 2008-12-26 00:52:02 +0000 | [diff] [blame] | 9487 | // Checking attributes of current function definition |
9488 | // dllimport attribute. | ||||
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 9489 | DLLImportAttr *DA = FD->getAttr<DLLImportAttr>(); |
9490 | if (DA && (!FD->getAttr<DLLExportAttr>())) { | ||||
9491 | // dllimport attribute cannot be directly applied to definition. | ||||
Francois Pichet | b613cd6 | 2011-03-29 10:39:17 +0000 | [diff] [blame] | 9492 | // Microsoft accepts dllimport for functions defined within class scope. |
9493 | if (!DA->isInherited() && | ||||
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 9494 | !(LangOpts.MicrosoftExt && FD->getLexicalDeclContext()->isRecord())) { |
Anton Korobeynikov | 2f40270 | 2008-12-26 00:52:02 +0000 | [diff] [blame] | 9495 | Diag(FD->getLocation(), |
9496 | diag::err_attribute_can_be_applied_only_to_symbol_declaration) | ||||
9497 | << "dllimport"; | ||||
9498 | FD->setInvalidDecl(); | ||||
Argyrios Kyrtzidis | a9990e8 | 2012-12-14 06:54:03 +0000 | [diff] [blame] | 9499 | return D; |
Ted Kremenek | 12911a8 | 2010-02-21 05:12:53 +0000 | [diff] [blame] | 9500 | } |
9501 | |||||
9502 | // Visual C++ appears to not think this is an issue, so only issue | ||||
9503 | // a warning when Microsoft extensions are disabled. | ||||
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 9504 | if (!LangOpts.MicrosoftExt) { |
Anton Korobeynikov | 2f40270 | 2008-12-26 00:52:02 +0000 | [diff] [blame] | 9505 | // If a symbol previously declared dllimport is later defined, the |
9506 | // attribute is ignored in subsequent references, and a warning is | ||||
9507 | // emitted. | ||||
9508 | Diag(FD->getLocation(), | ||||
9509 | diag::warn_redeclaration_without_attribute_prev_attribute_ignored) | ||||
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 9510 | << FD->getName() << "dllimport"; |
Anton Korobeynikov | 2f40270 | 2008-12-26 00:52:02 +0000 | [diff] [blame] | 9511 | } |
9512 | } | ||||
Dmitri Gribenko | c41ace9 | 2012-08-14 17:17:18 +0000 | [diff] [blame] | 9513 | // We want to attach documentation to original Decl (which might be |
9514 | // a function template). | ||||
9515 | ActOnDocumentableDecl(D); | ||||
Argyrios Kyrtzidis | a9990e8 | 2012-12-14 06:54:03 +0000 | [diff] [blame] | 9516 | return D; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 9517 | } |
9518 | |||||
Douglas Gregor | 5077c38 | 2010-05-15 06:01:05 +0000 | [diff] [blame] | 9519 | /// \brief Given the set of return statements within a function body, |
9520 | /// compute the variables that are subject to the named return value | ||||
9521 | /// optimization. | ||||
9522 | /// | ||||
9523 | /// Each of the variables that is subject to the named return value | ||||
9524 | /// optimization will be marked as NRVO variables in the AST, and any | ||||
9525 | /// return statement that has a marked NRVO variable as its NRVO candidate can | ||||
9526 | /// use the named return value optimization. | ||||
9527 | /// | ||||
9528 | /// This function applies a very simplistic algorithm for NRVO: if every return | ||||
9529 | /// statement in the function has the same NRVO candidate, that candidate is | ||||
9530 | /// the NRVO variable. | ||||
9531 | /// | ||||
9532 | /// FIXME: Employ a smarter algorithm that accounts for multiple return | ||||
9533 | /// statements and the lifetimes of the NRVO candidates. We should be able to | ||||
9534 | /// find a maximal set of NRVO variables. | ||||
Douglas Gregor | f8b7f71 | 2011-09-06 20:46:03 +0000 | [diff] [blame] | 9535 | void Sema::computeNRVO(Stmt *Body, FunctionScopeInfo *Scope) { |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 9536 | ReturnStmt **Returns = Scope->Returns.data(); |
9537 | |||||
Douglas Gregor | 5077c38 | 2010-05-15 06:01:05 +0000 | [diff] [blame] | 9538 | const VarDecl *NRVOCandidate = 0; |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 9539 | for (unsigned I = 0, E = Scope->Returns.size(); I != E; ++I) { |
Douglas Gregor | 5077c38 | 2010-05-15 06:01:05 +0000 | [diff] [blame] | 9540 | if (!Returns[I]->getNRVOCandidate()) |
9541 | return; | ||||
9542 | |||||
9543 | if (!NRVOCandidate) | ||||
9544 | NRVOCandidate = Returns[I]->getNRVOCandidate(); | ||||
9545 | else if (NRVOCandidate != Returns[I]->getNRVOCandidate()) | ||||
9546 | return; | ||||
9547 | } | ||||
9548 | |||||
9549 | if (NRVOCandidate) | ||||
9550 | const_cast<VarDecl*>(NRVOCandidate)->setNRVOVariable(true); | ||||
9551 | } | ||||
9552 | |||||
Richard Smith | 1a5bd5d | 2012-11-19 21:13:18 +0000 | [diff] [blame] | 9553 | bool Sema::canSkipFunctionBody(Decl *D) { |
Richard Smith | d1bac8d | 2012-11-27 21:31:01 +0000 | [diff] [blame] | 9554 | if (!Consumer.shouldSkipFunctionBody(D)) |
9555 | return false; | ||||
9556 | |||||
Richard Smith | 1a5bd5d | 2012-11-19 21:13:18 +0000 | [diff] [blame] | 9557 | if (isa<ObjCMethodDecl>(D)) |
9558 | return true; | ||||
9559 | |||||
9560 | FunctionDecl *FD = 0; | ||||
9561 | if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(D)) | ||||
9562 | FD = FTD->getTemplatedDecl(); | ||||
9563 | else | ||||
9564 | FD = cast<FunctionDecl>(D); | ||||
9565 | |||||
9566 | // We cannot skip the body of a function (or function template) which is | ||||
9567 | // constexpr, since we may need to evaluate its body in order to parse the | ||||
9568 | // rest of the file. | ||||
Richard Smith | 25d8c85 | 2013-05-10 04:31:10 +0000 | [diff] [blame] | 9569 | // We cannot skip the body of a function with an undeduced return type, |
9570 | // because any callers of that function need to know the type. | ||||
9571 | return !FD->isConstexpr() && !FD->getResultType()->isUndeducedType(); | ||||
Richard Smith | 1a5bd5d | 2012-11-19 21:13:18 +0000 | [diff] [blame] | 9572 | } |
9573 | |||||
Argyrios Kyrtzidis | 35f3f36 | 2012-12-06 18:59:10 +0000 | [diff] [blame] | 9574 | Decl *Sema::ActOnSkippedFunctionBody(Decl *Decl) { |
Argyrios Kyrtzidis | 1f12c47 | 2013-02-22 04:11:06 +0000 | [diff] [blame] | 9575 | if (FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(Decl)) |
Argyrios Kyrtzidis | 35f3f36 | 2012-12-06 18:59:10 +0000 | [diff] [blame] | 9576 | FD->setHasSkippedBody(); |
Argyrios Kyrtzidis | 1f12c47 | 2013-02-22 04:11:06 +0000 | [diff] [blame] | 9577 | else if (ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(Decl)) |
Argyrios Kyrtzidis | 35f3f36 | 2012-12-06 18:59:10 +0000 | [diff] [blame] | 9578 | MD->setHasSkippedBody(); |
9579 | return ActOnFinishFunctionBody(Decl, 0); | ||||
9580 | } | ||||
9581 | |||||
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 9582 | Decl *Sema::ActOnFinishFunctionBody(Decl *D, Stmt *BodyArg) { |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 9583 | return ActOnFinishFunctionBody(D, BodyArg, false); |
Douglas Gregor | e2c31ff | 2009-05-15 17:59:04 +0000 | [diff] [blame] | 9584 | } |
9585 | |||||
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9586 | Decl *Sema::ActOnFinishFunctionBody(Decl *dcl, Stmt *Body, |
9587 | bool IsInstantiation) { | ||||
Douglas Gregor | d83d040 | 2009-08-22 00:34:47 +0000 | [diff] [blame] | 9588 | FunctionDecl *FD = 0; |
9589 | FunctionTemplateDecl *FunTmpl = dyn_cast_or_null<FunctionTemplateDecl>(dcl); | ||||
9590 | if (FunTmpl) | ||||
9591 | FD = FunTmpl->getTemplatedDecl(); | ||||
9592 | else | ||||
9593 | FD = dyn_cast_or_null<FunctionDecl>(dcl); | ||||
9594 | |||||
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 9595 | sema::AnalysisBasedWarnings::Policy WP = AnalysisWarnings.getDefaultPolicy(); |
Ted Kremenek | 3ed6fc0 | 2011-02-23 01:51:48 +0000 | [diff] [blame] | 9596 | sema::AnalysisBasedWarnings::Policy *ActivePolicy = 0; |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 9597 | |
Douglas Gregor | d83d040 | 2009-08-22 00:34:47 +0000 | [diff] [blame] | 9598 | if (FD) { |
Chris Lattner | a5251fc | 2009-04-18 09:36:27 +0000 | [diff] [blame] | 9599 | FD->setBody(Body); |
John McCall | 75d8ba3 | 2012-02-14 19:50:52 +0000 | [diff] [blame] | 9600 | |
Richard Smith | 25d8c85 | 2013-05-10 04:31:10 +0000 | [diff] [blame] | 9601 | if (getLangOpts().CPlusPlus1y && !FD->isInvalidDecl() && Body && |
9602 | !FD->isDependentContext() && FD->getResultType()->isUndeducedType()) { | ||||
9603 | // If the function has a deduced result type but contains no 'return' | ||||
9604 | // statements, the result type as written must be exactly 'auto', and | ||||
9605 | // the deduced result type is 'void'. | ||||
9606 | if (!FD->getResultType()->getAs<AutoType>()) { | ||||
9607 | Diag(dcl->getLocation(), diag::err_auto_fn_no_return_but_not_auto) | ||||
9608 | << FD->getResultType(); | ||||
9609 | FD->setInvalidDecl(); | ||||
9610 | } else { | ||||
9611 | // Substitute 'void' for the 'auto' in the type. | ||||
9612 | TypeLoc ResultType = FD->getTypeSourceInfo()->getTypeLoc(). | ||||
9613 | IgnoreParens().castAs<FunctionProtoTypeLoc>().getResultLoc(); | ||||
9614 | Context.adjustDeducedFunctionResultType( | ||||
9615 | FD, SubstAutoType(ResultType.getType(), Context.VoidTy)); | ||||
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 9616 | } |
9617 | } | ||||
9618 | |||||
Nick Lewycky | cd0655b | 2013-02-01 08:13:20 +0000 | [diff] [blame] | 9619 | // The only way to be included in UndefinedButUsed is if there is an |
9620 | // ODR use before the definition. Avoid the expensive map lookup if this | ||||
Nick Lewycky | 995e26b | 2013-01-31 03:23:57 +0000 | [diff] [blame] | 9621 | // is the first declaration. |
Nick Lewycky | cd0655b | 2013-02-01 08:13:20 +0000 | [diff] [blame] | 9622 | if (FD->getPreviousDecl() != 0 && FD->getPreviousDecl()->isUsed()) { |
Rafael Espindola | 181e3ec | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 9623 | if (!FD->isExternallyVisible()) |
Nick Lewycky | cd0655b | 2013-02-01 08:13:20 +0000 | [diff] [blame] | 9624 | UndefinedButUsed.erase(FD); |
9625 | else if (FD->isInlined() && | ||||
9626 | (LangOpts.CPlusPlus || !LangOpts.GNUInline) && | ||||
9627 | (!FD->getPreviousDecl()->hasAttr<GNUInlineAttr>())) | ||||
9628 | UndefinedButUsed.erase(FD); | ||||
9629 | } | ||||
Nick Lewycky | 995e26b | 2013-01-31 03:23:57 +0000 | [diff] [blame] | 9630 | |
John McCall | 75d8ba3 | 2012-02-14 19:50:52 +0000 | [diff] [blame] | 9631 | // If the function implicitly returns zero (like 'main') or is naked, |
9632 | // don't complain about missing return statements. | ||||
9633 | if (FD->hasImplicitReturnZero() || FD->hasAttr<NakedAttr>()) | ||||
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 9634 | WP.disableCheckFallThrough(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9635 | |
Francois Pichet | 6a24747 | 2011-05-11 02:14:46 +0000 | [diff] [blame] | 9636 | // MSVC permits the use of pure specifier (=0) on function definition, |
9637 | // defined at class scope, warn about this non standard construct. | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9638 | if (getLangOpts().MicrosoftExt && FD->isPure()) |
Francois Pichet | 6a24747 | 2011-05-11 02:14:46 +0000 | [diff] [blame] | 9639 | Diag(FD->getLocation(), diag::warn_pure_function_definition); |
9640 | |||||
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 9641 | if (!FD->isInvalidDecl()) { |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9642 | DiagnoseUnusedParameters(FD->param_begin(), FD->param_end()); |
Argyrios Kyrtzidis | 3532fdd | 2010-11-17 23:11:54 +0000 | [diff] [blame] | 9643 | DiagnoseSizeOfParametersAndReturnValue(FD->param_begin(), FD->param_end(), |
9644 | FD->getResultType(), FD); | ||||
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 9645 | |
9646 | // If this is a constructor, we need a vtable. | ||||
9647 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(FD)) | ||||
9648 | MarkVTableUsed(FD->getLocation(), Constructor->getParent()); | ||||
Douglas Gregor | 5077c38 | 2010-05-15 06:01:05 +0000 | [diff] [blame] | 9649 | |
Jordan Rose | 7dd900e | 2012-07-02 21:19:23 +0000 | [diff] [blame] | 9650 | // Try to apply the named return value optimization. We have to check |
9651 | // if we can do this here because lambdas keep return statements around | ||||
9652 | // to deduce an implicit return type. | ||||
9653 | if (getLangOpts().CPlusPlus && FD->getResultType()->isRecordType() && | ||||
9654 | !FD->isDependentContext()) | ||||
9655 | computeNRVO(Body, getCurFunction()); | ||||
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 9656 | } |
9657 | |||||
Douglas Gregor | 76e3da5 | 2012-02-08 20:17:14 +0000 | [diff] [blame] | 9658 | assert((FD == getCurFunctionDecl() || getCurLambda()->CallOperator == FD) && |
9659 | "Function parsing confused"); | ||||
Steve Naroff | 394f3f4 | 2008-07-25 17:57:26 +0000 | [diff] [blame] | 9660 | } else if (ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(dcl)) { |
Chris Lattner | ffed163 | 2009-02-16 19:27:54 +0000 | [diff] [blame] | 9661 | assert(MD == getCurMethodDecl() && "Method parsing confused"); |
Chris Lattner | a5251fc | 2009-04-18 09:36:27 +0000 | [diff] [blame] | 9662 | MD->setBody(Body); |
Argyrios Kyrtzidis | 3532fdd | 2010-11-17 23:11:54 +0000 | [diff] [blame] | 9663 | if (!MD->isInvalidDecl()) { |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9664 | DiagnoseUnusedParameters(MD->param_begin(), MD->param_end()); |
Argyrios Kyrtzidis | 3532fdd | 2010-11-17 23:11:54 +0000 | [diff] [blame] | 9665 | DiagnoseSizeOfParametersAndReturnValue(MD->param_begin(), MD->param_end(), |
9666 | MD->getResultType(), MD); | ||||
Douglas Gregor | f7603f6 | 2011-09-06 20:33:37 +0000 | [diff] [blame] | 9667 | |
9668 | if (Body) | ||||
Douglas Gregor | f8b7f71 | 2011-09-06 20:46:03 +0000 | [diff] [blame] | 9669 | computeNRVO(Body, getCurFunction()); |
Argyrios Kyrtzidis | 3532fdd | 2010-11-17 23:11:54 +0000 | [diff] [blame] | 9670 | } |
Jordan Rose | 535a5d0 | 2012-10-19 16:05:26 +0000 | [diff] [blame] | 9671 | if (getCurFunction()->ObjCShouldCallSuper) { |
Fariborz Jahanian | 9f55983 | 2012-09-10 16:51:09 +0000 | [diff] [blame] | 9672 | Diag(MD->getLocEnd(), diag::warn_objc_missing_super_call) |
9673 | << MD->getSelector().getAsString(); | ||||
Jordan Rose | 535a5d0 | 2012-10-19 16:05:26 +0000 | [diff] [blame] | 9674 | getCurFunction()->ObjCShouldCallSuper = false; |
Nico Weber | 80cb6e6 | 2011-08-28 22:35:17 +0000 | [diff] [blame] | 9675 | } |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 9676 | } else { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 9677 | return 0; |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 9678 | } |
Douglas Gregor | e2c31ff | 2009-05-15 17:59:04 +0000 | [diff] [blame] | 9679 | |
Jordan Rose | 535a5d0 | 2012-10-19 16:05:26 +0000 | [diff] [blame] | 9680 | assert(!getCurFunction()->ObjCShouldCallSuper && |
Eli Friedman | 95aac15 | 2012-08-01 21:02:59 +0000 | [diff] [blame] | 9681 | "This should only be set for ObjC methods, which should have been " |
9682 | "handled in the block above."); | ||||
Nico Weber | 9a1ecf0 | 2011-08-22 17:25:57 +0000 | [diff] [blame] | 9683 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 9684 | // Verify and clean out per-function state. |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 9685 | if (Body) { |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 9686 | // C++ constructors that have function-try-blocks can't have return |
9687 | // statements in the handlers of that block. (C++ [except.handle]p14) | ||||
9688 | // Verify this. | ||||
9689 | if (FD && isa<CXXConstructorDecl>(FD) && isa<CXXTryStmt>(Body)) | ||||
9690 | DiagnoseReturnInConstructorExceptionHandler(cast<CXXTryStmt>(Body)); | ||||
9691 | |||||
Richard Smith | 37bee67 | 2011-08-12 18:44:32 +0000 | [diff] [blame] | 9692 | // Verify that gotos and switch cases don't jump into scopes illegally. |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 9693 | if (getCurFunction()->NeedsScopeChecking() && |
John McCall | 8a2ca74 | 2010-05-20 07:13:26 +0000 | [diff] [blame] | 9694 | !dcl->isInvalidDecl() && |
Douglas Gregor | 27bec77 | 2012-08-17 05:12:08 +0000 | [diff] [blame] | 9695 | !hasAnyUnrecoverableErrorsInThisFunction() && |
9696 | !PP.isCodeCompletionEnabled()) | ||||
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 9697 | DiagnoseInvalidJumps(Body); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9698 | |
John McCall | 1544282 | 2010-08-04 01:04:25 +0000 | [diff] [blame] | 9699 | if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(dcl)) { |
9700 | if (!Destructor->getParent()->isDependentType()) | ||||
9701 | CheckDestructor(Destructor); | ||||
9702 | |||||
John McCall | ef027fe | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 9703 | MarkBaseAndMemberDestructorsReferenced(Destructor->getLocation(), |
9704 | Destructor->getParent()); | ||||
John McCall | 1544282 | 2010-08-04 01:04:25 +0000 | [diff] [blame] | 9705 | } |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 9706 | |
9707 | // If any errors have occurred, clear out any temporaries that may have | ||||
9708 | // been leftover. This ensures that these temporaries won't be picked up for | ||||
9709 | // deletion in some later function. | ||||
Douglas Gregor | 26cd44d | 2011-03-04 23:08:02 +0000 | [diff] [blame] | 9710 | if (PP.getDiagnostics().hasErrorOccurred() || |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9711 | PP.getDiagnostics().getSuppressAllDiagnostics()) { |
John McCall | 80ee6e8 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 9712 | DiscardCleanupsInEvaluationContext(); |
DeLesley Hutchins | 12f37e4 | 2012-12-07 22:53:48 +0000 | [diff] [blame] | 9713 | } |
9714 | if (!PP.getDiagnostics().hasUncompilableErrorOccurred() && | ||||
9715 | !isa<FunctionTemplateDecl>(dcl)) { | ||||
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 9716 | // Since the body is valid, issue any analysis-based warnings that are |
9717 | // enabled. | ||||
Ted Kremenek | 3ed6fc0 | 2011-02-23 01:51:48 +0000 | [diff] [blame] | 9718 | ActivePolicy = &WP; |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 9719 | } |
9720 | |||||
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 9721 | if (!IsInstantiation && FD && FD->isConstexpr() && !FD->isInvalidDecl() && |
9722 | (!CheckConstexprFunctionDecl(FD) || | ||||
9723 | !CheckConstexprFunctionBody(FD, Body))) | ||||
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 9724 | FD->setInvalidDecl(); |
9725 | |||||
John McCall | 80ee6e8 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 9726 | assert(ExprCleanupObjects.empty() && "Leftover temporaries in function"); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9727 | assert(!ExprNeedsCleanups && "Unaccounted cleanups in function"); |
Eli Friedman | d2cce13 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 9728 | assert(MaybeODRUseExprs.empty() && |
9729 | "Leftover expressions for odr-use checking"); | ||||
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 9730 | } |
9731 | |||||
John McCall | 90f9789 | 2010-03-25 22:08:03 +0000 | [diff] [blame] | 9732 | if (!IsInstantiation) |
9733 | PopDeclContext(); | ||||
9734 | |||||
Eli Friedman | ec9ea72 | 2012-01-05 03:35:19 +0000 | [diff] [blame] | 9735 | PopFunctionScopeInfo(ActivePolicy, dcl); |
Anders Carlsson | f8a9a79 | 2009-11-13 19:21:49 +0000 | [diff] [blame] | 9736 | |
Douglas Gregor | d5b5728 | 2009-11-15 07:07:58 +0000 | [diff] [blame] | 9737 | // If any errors have occurred, clear out any temporaries that may have |
9738 | // been leftover. This ensures that these temporaries won't be picked up for | ||||
9739 | // deletion in some later function. | ||||
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9740 | if (getDiagnostics().hasErrorOccurred()) { |
John McCall | 80ee6e8 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 9741 | DiscardCleanupsInEvaluationContext(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9742 | } |
Argyrios Kyrtzidis | 49b96d1 | 2010-08-13 18:42:17 +0000 | [diff] [blame] | 9743 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 9744 | return dcl; |
Fariborz Jahanian | 60fbca0 | 2007-11-10 16:31:34 +0000 | [diff] [blame] | 9745 | } |
9746 | |||||
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 9747 | |
9748 | /// When we finish delayed parsing of an attribute, we must attach it to the | ||||
9749 | /// relevant Decl. | ||||
9750 | void Sema::ActOnFinishDelayedAttribute(Scope *S, Decl *D, | ||||
9751 | ParsedAttributes &Attrs) { | ||||
DeLesley Hutchins | 7b9ff0c | 2012-01-20 22:37:06 +0000 | [diff] [blame] | 9752 | // Always attach attributes to the underlying decl. |
9753 | if (TemplateDecl *TD = dyn_cast<TemplateDecl>(D)) | ||||
9754 | D = TD->getTemplatedDecl(); | ||||
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 9755 | ProcessDeclAttributeList(S, D, Attrs.getList()); |
9756 | |||||
9757 | if (CXXMethodDecl *Method = dyn_cast_or_null<CXXMethodDecl>(D)) | ||||
9758 | if (Method->isStatic()) | ||||
9759 | checkThisInStaticMemberFunctionAttributes(Method); | ||||
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 9760 | } |
9761 | |||||
9762 | |||||
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 9763 | /// ImplicitlyDefineFunction - An undeclared identifier was used in a function |
9764 | /// 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] | 9765 | NamedDecl *Sema::ImplicitlyDefineFunction(SourceLocation Loc, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 9766 | IdentifierInfo &II, Scope *S) { |
Douglas Gregor | 6393519 | 2009-03-02 00:19:53 +0000 | [diff] [blame] | 9767 | // Before we produce a declaration for an implicitly defined |
9768 | // function, see whether there was a locally-scoped declaration of | ||||
9769 | // this name as a function or variable. If so, use that | ||||
9770 | // (non-visible) declaration, and complain about it. | ||||
Richard Smith | 662f41b | 2013-06-18 20:15:12 +0000 | [diff] [blame] | 9771 | if (NamedDecl *ExternCPrev = findLocallyScopedExternCDecl(&II)) { |
9772 | Diag(Loc, diag::warn_use_out_of_scope_declaration) << ExternCPrev; | ||||
9773 | Diag(ExternCPrev->getLocation(), diag::note_previous_declaration); | ||||
9774 | return ExternCPrev; | ||||
Douglas Gregor | 6393519 | 2009-03-02 00:19:53 +0000 | [diff] [blame] | 9775 | } |
9776 | |||||
Chris Lattner | 37d1084 | 2008-05-05 21:18:06 +0000 | [diff] [blame] | 9777 | // Extension in C99. Legal in C90, but warn about it. |
Hans Wennborg | e3ca33a | 2011-12-08 15:56:07 +0000 | [diff] [blame] | 9778 | unsigned diag_id; |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 9779 | if (II.getName().startswith("__builtin_")) |
Abramo Bagnara | 753a200 | 2012-01-09 10:05:48 +0000 | [diff] [blame] | 9780 | diag_id = diag::warn_builtin_unknown; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9781 | else if (getLangOpts().C99) |
Hans Wennborg | e3ca33a | 2011-12-08 15:56:07 +0000 | [diff] [blame] | 9782 | diag_id = diag::ext_implicit_function_decl; |
Chris Lattner | 37d1084 | 2008-05-05 21:18:06 +0000 | [diff] [blame] | 9783 | else |
Hans Wennborg | e3ca33a | 2011-12-08 15:56:07 +0000 | [diff] [blame] | 9784 | diag_id = diag::warn_implicit_function_decl; |
9785 | Diag(Loc, diag_id) << &II; | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9786 | |
Hans Wennborg | e3ca33a | 2011-12-08 15:56:07 +0000 | [diff] [blame] | 9787 | // Because typo correction is expensive, only do it if the implicit |
9788 | // function declaration is going to be treated as an error. | ||||
9789 | if (Diags.getDiagnosticLevel(diag_id, Loc) >= DiagnosticsEngine::Error) { | ||||
9790 | TypoCorrection Corrected; | ||||
Kaelyn Uhrain | 43e875d | 2012-01-18 21:41:41 +0000 | [diff] [blame] | 9791 | DeclFilterCCC<FunctionDecl> Validator; |
Hans Wennborg | e3ca33a | 2011-12-08 15:56:07 +0000 | [diff] [blame] | 9792 | if (S && (Corrected = CorrectTypo(DeclarationNameInfo(&II, Loc), |
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 9793 | LookupOrdinaryName, S, 0, Validator))) |
9794 | diagnoseTypo(Corrected, PDiag(diag::note_function_suggestion), | ||||
9795 | /*ErrorRecovery*/false); | ||||
Hans Wennborg | 122de3e | 2011-12-06 09:46:12 +0000 | [diff] [blame] | 9796 | } |
9797 | |||||
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 9798 | // Set a Declarator for the implicit definition: int foo(); |
9799 | const char *Dummy; | ||||
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 9800 | AttributeFactory attrFactory; |
9801 | DeclSpec DS(attrFactory); | ||||
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 9802 | unsigned DiagID; |
9803 | bool Error = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, Dummy, DiagID); | ||||
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 9804 | (void)Error; // Silence warning. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 9805 | assert(!Error && "Error setting up implicit decl!"); |
Abramo Bagnara | 59c0a81 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 9806 | SourceLocation NoLoc; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 9807 | Declarator D(DS, Declarator::BlockContext); |
Abramo Bagnara | 59c0a81 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 9808 | D.AddTypeInfo(DeclaratorChunk::getFunction(/*HasProto=*/false, |
9809 | /*IsAmbiguous=*/false, | ||||
9810 | /*RParenLoc=*/NoLoc, | ||||
9811 | /*ArgInfo=*/0, | ||||
9812 | /*NumArgs=*/0, | ||||
9813 | /*EllipsisLoc=*/NoLoc, | ||||
9814 | /*RParenLoc=*/NoLoc, | ||||
9815 | /*TypeQuals=*/0, | ||||
9816 | /*RefQualifierIsLvalueRef=*/true, | ||||
9817 | /*RefQualifierLoc=*/NoLoc, | ||||
9818 | /*ConstQualifierLoc=*/NoLoc, | ||||
9819 | /*VolatileQualifierLoc=*/NoLoc, | ||||
9820 | /*MutableLoc=*/NoLoc, | ||||
9821 | EST_None, | ||||
9822 | /*ESpecLoc=*/NoLoc, | ||||
9823 | /*Exceptions=*/0, | ||||
9824 | /*ExceptionRanges=*/0, | ||||
9825 | /*NumExceptions=*/0, | ||||
9826 | /*NoexceptExpr=*/0, | ||||
9827 | Loc, Loc, D), | ||||
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 9828 | DS.getAttributes(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 9829 | SourceLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 9830 | D.SetIdentifier(&II, Loc); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 9831 | |
Argyrios Kyrtzidis | 93213bb | 2008-05-01 21:04:16 +0000 | [diff] [blame] | 9832 | // Insert this function into translation-unit scope. |
9833 | |||||
9834 | DeclContext *PrevDC = CurContext; | ||||
9835 | CurContext = Context.getTranslationUnitDecl(); | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9836 | |
Jordan Rose | 41f3f3a | 2013-03-05 01:27:54 +0000 | [diff] [blame] | 9837 | FunctionDecl *FD = cast<FunctionDecl>(ActOnDeclarator(TUScope, D)); |
Steve Naroff | e2ef815 | 2008-04-04 14:32:09 +0000 | [diff] [blame] | 9838 | FD->setImplicit(); |
Argyrios Kyrtzidis | 93213bb | 2008-05-01 21:04:16 +0000 | [diff] [blame] | 9839 | |
9840 | CurContext = PrevDC; | ||||
9841 | |||||
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 9842 | AddKnownFunctionAttributes(FD); |
9843 | |||||
Steve Naroff | e2ef815 | 2008-04-04 14:32:09 +0000 | [diff] [blame] | 9844 | return FD; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 9845 | } |
9846 | |||||
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 9847 | /// \brief Adds any function attributes that we know a priori based on |
9848 | /// the declaration of this function. | ||||
9849 | /// | ||||
9850 | /// These attributes can apply both to implicitly-declared builtins | ||||
9851 | /// (like __builtin___printf_chk) or to library-declared functions | ||||
9852 | /// like NSLog or printf. | ||||
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 9853 | /// |
9854 | /// We need to check for duplicate attributes both here and where user-written | ||||
9855 | /// attributes are applied to declarations. | ||||
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 9856 | void Sema::AddKnownFunctionAttributes(FunctionDecl *FD) { |
9857 | if (FD->isInvalidDecl()) | ||||
9858 | return; | ||||
9859 | |||||
9860 | // If this is a built-in function, map its builtin attributes to | ||||
9861 | // actual attributes. | ||||
Douglas Gregor | 7814e6d | 2009-09-12 00:22:50 +0000 | [diff] [blame] | 9862 | if (unsigned BuiltinID = FD->getBuiltinID()) { |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 9863 | // Handle printf-formatting attributes. |
9864 | unsigned FormatIdx; | ||||
9865 | bool HasVAListArg; | ||||
9866 | if (Context.BuiltinInfo.isPrintfLike(BuiltinID, FormatIdx, HasVAListArg)) { | ||||
Jean-Daniel Dupas | 1acbe5e | 2012-01-24 22:32:46 +0000 | [diff] [blame] | 9867 | if (!FD->getAttr<FormatAttr>()) { |
9868 | const char *fmt = "printf"; | ||||
9869 | unsigned int NumParams = FD->getNumParams(); | ||||
9870 | if (FormatIdx < NumParams && // NumParams may be 0 (e.g. vfprintf) | ||||
9871 | FD->getParamDecl(FormatIdx)->getType()->isObjCObjectPointerType()) | ||||
9872 | fmt = "NSString"; | ||||
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 9873 | FD->addAttr(::new (Context) FormatAttr(FD->getLocation(), Context, |
Aaron Ballman | caa5ab2 | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 9874 | &Context.Idents.get(fmt), |
9875 | FormatIdx+1, | ||||
Ted Kremenek | 3d2c43e | 2010-02-11 05:28:37 +0000 | [diff] [blame] | 9876 | HasVAListArg ? 0 : FormatIdx+2)); |
Jean-Daniel Dupas | 1acbe5e | 2012-01-24 22:32:46 +0000 | [diff] [blame] | 9877 | } |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 9878 | } |
Ted Kremenek | bee05c1 | 2010-07-16 02:11:15 +0000 | [diff] [blame] | 9879 | if (Context.BuiltinInfo.isScanfLike(BuiltinID, FormatIdx, |
9880 | HasVAListArg)) { | ||||
9881 | if (!FD->getAttr<FormatAttr>()) | ||||
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 9882 | FD->addAttr(::new (Context) FormatAttr(FD->getLocation(), Context, |
Aaron Ballman | caa5ab2 | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 9883 | &Context.Idents.get("scanf"), |
9884 | FormatIdx+1, | ||||
Ted Kremenek | bee05c1 | 2010-07-16 02:11:15 +0000 | [diff] [blame] | 9885 | HasVAListArg ? 0 : FormatIdx+2)); |
9886 | } | ||||
Daniel Dunbar | ef2abfe | 2009-02-16 22:43:43 +0000 | [diff] [blame] | 9887 | |
9888 | // Mark const if we don't care about errno and that is the only | ||||
9889 | // thing preventing the function from being const. This allows | ||||
9890 | // IRgen to use LLVM intrinsics for such functions. | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9891 | if (!getLangOpts().MathErrno && |
Daniel Dunbar | ef2abfe | 2009-02-16 22:43:43 +0000 | [diff] [blame] | 9892 | Context.BuiltinInfo.isConstWithoutErrno(BuiltinID)) { |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 9893 | if (!FD->getAttr<ConstAttr>()) |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 9894 | FD->addAttr(::new (Context) ConstAttr(FD->getLocation(), Context)); |
Daniel Dunbar | ef2abfe | 2009-02-16 22:43:43 +0000 | [diff] [blame] | 9895 | } |
Mike Stump | 0feecbb | 2009-07-27 19:14:18 +0000 | [diff] [blame] | 9896 | |
Rafael Espindola | 6700415 | 2011-10-12 19:51:18 +0000 | [diff] [blame] | 9897 | if (Context.BuiltinInfo.isReturnsTwice(BuiltinID) && |
9898 | !FD->getAttr<ReturnsTwiceAttr>()) | ||||
9899 | FD->addAttr(::new (Context) ReturnsTwiceAttr(FD->getLocation(), Context)); | ||||
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 9900 | if (Context.BuiltinInfo.isNoThrow(BuiltinID) && !FD->getAttr<NoThrowAttr>()) |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 9901 | FD->addAttr(::new (Context) NoThrowAttr(FD->getLocation(), Context)); |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 9902 | if (Context.BuiltinInfo.isConst(BuiltinID) && !FD->getAttr<ConstAttr>()) |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 9903 | FD->addAttr(::new (Context) ConstAttr(FD->getLocation(), Context)); |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 9904 | } |
9905 | |||||
9906 | IdentifierInfo *Name = FD->getIdentifier(); | ||||
9907 | if (!Name) | ||||
9908 | return; | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9909 | if ((!getLangOpts().CPlusPlus && |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 9910 | FD->getDeclContext()->isTranslationUnit()) || |
9911 | (isa<LinkageSpecDecl>(FD->getDeclContext()) && | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9912 | cast<LinkageSpecDecl>(FD->getDeclContext())->getLanguage() == |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 9913 | LinkageSpecDecl::lang_c)) { |
9914 | // Okay: this could be a libc/libm/Objective-C function we know | ||||
9915 | // about. | ||||
9916 | } else | ||||
9917 | return; | ||||
9918 | |||||
Jean-Daniel Dupas | 1acbe5e | 2012-01-24 22:32:46 +0000 | [diff] [blame] | 9919 | if (Name->isStr("asprintf") || Name->isStr("vasprintf")) { |
Mike Stump | 523a8fd | 2009-07-28 00:07:08 +0000 | [diff] [blame] | 9920 | // FIXME: asprintf and vasprintf aren't C99 functions. Should they be |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9921 | // target-specific builtins, perhaps? |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 9922 | if (!FD->getAttr<FormatAttr>()) |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 9923 | FD->addAttr(::new (Context) FormatAttr(FD->getLocation(), Context, |
Aaron Ballman | caa5ab2 | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 9924 | &Context.Idents.get("printf"), 2, |
Eli Friedman | d7dad72 | 2009-06-10 04:01:38 +0000 | [diff] [blame] | 9925 | Name->isStr("vasprintf") ? 0 : 3)); |
Mike Stump | 782fa30 | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 9926 | } |
Jordan Rose | 8a64f88 | 2012-08-08 21:17:31 +0000 | [diff] [blame] | 9927 | |
9928 | if (Name->isStr("__CFStringMakeConstantString")) { | ||||
9929 | // We already have a __builtin___CFStringMakeConstantString, | ||||
9930 | // but builds that use -fno-constant-cfstrings don't go through that. | ||||
9931 | if (!FD->getAttr<FormatArgAttr>()) | ||||
9932 | FD->addAttr(::new (Context) FormatArgAttr(FD->getLocation(), Context, 1)); | ||||
9933 | } | ||||
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 9934 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 9935 | |
John McCall | ba6a9bd | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 9936 | TypedefDecl *Sema::ParseTypedefDecl(Scope *S, Declarator &D, QualType T, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 9937 | TypeSourceInfo *TInfo) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 9938 | assert(D.getIdentifier() && "Wrong callback for declspec without declarator"); |
Steve Naroff | 5912a35 | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 9939 | assert(!T.isNull() && "GetTypeForDeclarator() returned null type"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9940 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 9941 | if (!TInfo) { |
John McCall | ba6a9bd | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 9942 | assert(D.isInvalidType() && "no declarator info for valid type"); |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 9943 | TInfo = Context.getTrivialTypeSourceInfo(T); |
John McCall | ba6a9bd | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 9944 | } |
9945 | |||||
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 9946 | // Scope manipulation handled by caller. |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 9947 | TypedefDecl *NewTD = TypedefDecl::Create(Context, CurContext, |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 9948 | D.getLocStart(), |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 9949 | D.getIdentifierLoc(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9950 | D.getIdentifier(), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 9951 | TInfo); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9952 | |
John McCall | cde5a40 | 2011-02-01 08:20:08 +0000 | [diff] [blame] | 9953 | // Bail out immediately if we have an invalid declaration. |
9954 | if (D.isInvalidType()) { | ||||
9955 | NewTD->setInvalidDecl(); | ||||
9956 | return NewTD; | ||||
Anders Carlsson | 4843e58 | 2009-03-10 17:07:44 +0000 | [diff] [blame] | 9957 | } |
9958 | |||||
Douglas Gregor | e389585 | 2011-09-12 18:37:38 +0000 | [diff] [blame] | 9959 | if (D.getDeclSpec().isModulePrivateSpecified()) { |
9960 | if (CurContext->isFunctionOrMethod()) | ||||
9961 | Diag(NewTD->getLocation(), diag::err_module_private_local) | ||||
9962 | << 2 << NewTD->getDeclName() | ||||
9963 | << SourceRange(D.getDeclSpec().getModulePrivateSpecLoc()) | ||||
9964 | << FixItHint::CreateRemoval(D.getDeclSpec().getModulePrivateSpecLoc()); | ||||
9965 | else | ||||
9966 | NewTD->setModulePrivate(); | ||||
9967 | } | ||||
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 9968 | |
John McCall | cde5a40 | 2011-02-01 08:20:08 +0000 | [diff] [blame] | 9969 | // C++ [dcl.typedef]p8: |
9970 | // If the typedef declaration defines an unnamed class (or | ||||
9971 | // enum), the first typedef-name declared by the declaration | ||||
9972 | // to be that class type (or enum type) is used to denote the | ||||
9973 | // class type (or enum type) for linkage purposes only. | ||||
9974 | // We need to check whether the type was declared in the declaration. | ||||
9975 | switch (D.getDeclSpec().getTypeSpecType()) { | ||||
9976 | case TST_enum: | ||||
9977 | case TST_struct: | ||||
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 9978 | case TST_interface: |
John McCall | cde5a40 | 2011-02-01 08:20:08 +0000 | [diff] [blame] | 9979 | case TST_union: |
9980 | case TST_class: { | ||||
9981 | TagDecl *tagFromDeclSpec = cast<TagDecl>(D.getDeclSpec().getRepAsDecl()); | ||||
9982 | |||||
9983 | // Do nothing if the tag is not anonymous or already has an | ||||
9984 | // associated typedef (from an earlier typedef in this decl group). | ||||
9985 | if (tagFromDeclSpec->getIdentifier()) break; | ||||
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 9986 | if (tagFromDeclSpec->getTypedefNameForAnonDecl()) break; |
John McCall | cde5a40 | 2011-02-01 08:20:08 +0000 | [diff] [blame] | 9987 | |
9988 | // A well-formed anonymous tag must always be a TUK_Definition. | ||||
9989 | assert(tagFromDeclSpec->isThisDeclarationADefinition()); | ||||
9990 | |||||
9991 | // The type must match the tag exactly; no qualifiers allowed. | ||||
9992 | if (!Context.hasSameType(T, Context.getTagDeclType(tagFromDeclSpec))) | ||||
9993 | break; | ||||
9994 | |||||
9995 | // Otherwise, set this is the anon-decl typedef for the tag. | ||||
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 9996 | tagFromDeclSpec->setTypedefNameForAnonDecl(NewTD); |
John McCall | cde5a40 | 2011-02-01 08:20:08 +0000 | [diff] [blame] | 9997 | break; |
9998 | } | ||||
9999 | |||||
10000 | default: | ||||
10001 | break; | ||||
10002 | } | ||||
10003 | |||||
Steve Naroff | 5912a35 | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 10004 | return NewTD; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 10005 | } |
10006 | |||||
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 10007 | |
Richard Smith | f1c66b4 | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 10008 | /// \brief Check that this is a valid underlying type for an enum declaration. |
10009 | bool Sema::CheckEnumUnderlyingType(TypeSourceInfo *TI) { | ||||
10010 | SourceLocation UnderlyingLoc = TI->getTypeLoc().getBeginLoc(); | ||||
10011 | QualType T = TI->getType(); | ||||
10012 | |||||
Eli Friedman | 2fcff83 | 2012-12-18 02:37:32 +0000 | [diff] [blame] | 10013 | if (T->isDependentType()) |
Richard Smith | f1c66b4 | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 10014 | return false; |
10015 | |||||
Eli Friedman | 2fcff83 | 2012-12-18 02:37:32 +0000 | [diff] [blame] | 10016 | if (const BuiltinType *BT = T->getAs<BuiltinType>()) |
10017 | if (BT->isInteger()) | ||||
10018 | return false; | ||||
10019 | |||||
Richard Smith | f1c66b4 | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 10020 | Diag(UnderlyingLoc, diag::err_enum_invalid_underlying) << T; |
10021 | return true; | ||||
10022 | } | ||||
10023 | |||||
10024 | /// Check whether this is a valid redeclaration of a previous enumeration. | ||||
10025 | /// \return true if the redeclaration was invalid. | ||||
10026 | bool Sema::CheckEnumRedeclaration(SourceLocation EnumLoc, bool IsScoped, | ||||
10027 | QualType EnumUnderlyingTy, | ||||
10028 | const EnumDecl *Prev) { | ||||
10029 | bool IsFixed = !EnumUnderlyingTy.isNull(); | ||||
10030 | |||||
10031 | if (IsScoped != Prev->isScoped()) { | ||||
10032 | Diag(EnumLoc, diag::err_enum_redeclare_scoped_mismatch) | ||||
10033 | << Prev->isScoped(); | ||||
10034 | Diag(Prev->getLocation(), diag::note_previous_use); | ||||
10035 | return true; | ||||
10036 | } | ||||
10037 | |||||
10038 | if (IsFixed && Prev->isFixed()) { | ||||
Richard Smith | 4ca93d9 | 2012-03-26 04:08:46 +0000 | [diff] [blame] | 10039 | if (!EnumUnderlyingTy->isDependentType() && |
10040 | !Prev->getIntegerType()->isDependentType() && | ||||
10041 | !Context.hasSameUnqualifiedType(EnumUnderlyingTy, | ||||
Richard Smith | f1c66b4 | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 10042 | Prev->getIntegerType())) { |
10043 | Diag(EnumLoc, diag::err_enum_redeclare_type_mismatch) | ||||
10044 | << EnumUnderlyingTy << Prev->getIntegerType(); | ||||
10045 | Diag(Prev->getLocation(), diag::note_previous_use); | ||||
10046 | return true; | ||||
10047 | } | ||||
10048 | } else if (IsFixed != Prev->isFixed()) { | ||||
10049 | Diag(EnumLoc, diag::err_enum_redeclare_fixed_mismatch) | ||||
10050 | << Prev->isFixed(); | ||||
10051 | Diag(Prev->getLocation(), diag::note_previous_use); | ||||
10052 | return true; | ||||
10053 | } | ||||
10054 | |||||
10055 | return false; | ||||
10056 | } | ||||
10057 | |||||
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 10058 | /// \brief Get diagnostic %select index for tag kind for |
10059 | /// redeclaration diagnostic message. | ||||
10060 | /// WARNING: Indexes apply to particular diagnostics only! | ||||
10061 | /// | ||||
10062 | /// \returns diagnostic %select index. | ||||
Joao Matos | f143ae9 | 2012-09-01 00:13:24 +0000 | [diff] [blame] | 10063 | static unsigned getRedeclDiagFromTagKind(TagTypeKind Tag) { |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 10064 | switch (Tag) { |
Joao Matos | f143ae9 | 2012-09-01 00:13:24 +0000 | [diff] [blame] | 10065 | case TTK_Struct: return 0; |
10066 | case TTK_Interface: return 1; | ||||
10067 | case TTK_Class: return 2; | ||||
10068 | default: llvm_unreachable("Invalid tag kind for redecl diagnostic!"); | ||||
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 10069 | } |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 10070 | } |
10071 | |||||
10072 | /// \brief Determine if tag kind is a class-key compatible with | ||||
10073 | /// class for redeclaration (class, struct, or __interface). | ||||
10074 | /// | ||||
Sylvestre Ledru | f3477c1 | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 10075 | /// \returns true iff the tag kind is compatible. |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 10076 | static bool isClassCompatTagKind(TagTypeKind Tag) |
10077 | { | ||||
10078 | return Tag == TTK_Struct || Tag == TTK_Class || Tag == TTK_Interface; | ||||
10079 | } | ||||
10080 | |||||
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 10081 | /// \brief Determine whether a tag with a given kind is acceptable |
10082 | /// as a redeclaration of the given tag declaration. | ||||
10083 | /// | ||||
10084 | /// \returns true if the new tag kind is acceptable, false otherwise. | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10085 | bool Sema::isAcceptableTagRedeclaration(const TagDecl *Previous, |
Richard Trieu | bbf34c0 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 10086 | TagTypeKind NewTag, bool isDefinition, |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 10087 | SourceLocation NewTagLoc, |
10088 | const IdentifierInfo &Name) { | ||||
10089 | // C++ [dcl.type.elab]p3: | ||||
10090 | // The class-key or enum keyword present in the | ||||
10091 | // elaborated-type-specifier shall agree in kind with the | ||||
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 10092 | // declaration to which the name in the elaborated-type-specifier |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 10093 | // refers. This rule also applies to the form of |
10094 | // elaborated-type-specifier that declares a class-name or | ||||
10095 | // friend class since it can be construed as referring to the | ||||
10096 | // definition of the class. Thus, in any | ||||
10097 | // elaborated-type-specifier, the enum keyword shall be used to | ||||
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 10098 | // refer to an enumeration (7.2), the union class-key shall be |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 10099 | // used to refer to a union (clause 9), and either the class or |
10100 | // struct class-key shall be used to refer to a class (clause 9) | ||||
10101 | // declared using the class or struct class-key. | ||||
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 10102 | TagTypeKind OldTag = Previous->getTagKind(); |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 10103 | if (!isDefinition || !isClassCompatTagKind(NewTag)) |
Richard Trieu | bbf34c0 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 10104 | if (OldTag == NewTag) |
10105 | return true; | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10106 | |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 10107 | if (isClassCompatTagKind(OldTag) && isClassCompatTagKind(NewTag)) { |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 10108 | // Warn about the struct/class tag mismatch. |
10109 | bool isTemplate = false; | ||||
10110 | if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Previous)) | ||||
10111 | isTemplate = Record->getDescribedClassTemplate(); | ||||
10112 | |||||
Richard Trieu | bbf34c0 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 10113 | if (!ActiveTemplateInstantiations.empty()) { |
10114 | // In a template instantiation, do not offer fix-its for tag mismatches | ||||
10115 | // since they usually mess up the template instead of fixing the problem. | ||||
10116 | Diag(NewTagLoc, diag::warn_struct_class_tag_mismatch) | ||||
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 10117 | << getRedeclDiagFromTagKind(NewTag) << isTemplate << &Name |
10118 | << getRedeclDiagFromTagKind(OldTag); | ||||
Richard Trieu | bbf34c0 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 10119 | return true; |
10120 | } | ||||
10121 | |||||
10122 | if (isDefinition) { | ||||
10123 | // On definitions, check previous tags and issue a fix-it for each | ||||
10124 | // one that doesn't match the current tag. | ||||
10125 | if (Previous->getDefinition()) { | ||||
10126 | // Don't suggest fix-its for redefinitions. | ||||
10127 | return true; | ||||
10128 | } | ||||
10129 | |||||
10130 | bool previousMismatch = false; | ||||
10131 | for (TagDecl::redecl_iterator I(Previous->redecls_begin()), | ||||
10132 | E(Previous->redecls_end()); I != E; ++I) { | ||||
10133 | if (I->getTagKind() != NewTag) { | ||||
10134 | if (!previousMismatch) { | ||||
10135 | previousMismatch = true; | ||||
10136 | Diag(NewTagLoc, diag::warn_struct_class_previous_tag_mismatch) | ||||
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 10137 | << getRedeclDiagFromTagKind(NewTag) << isTemplate << &Name |
10138 | << getRedeclDiagFromTagKind(I->getTagKind()); | ||||
Richard Trieu | bbf34c0 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 10139 | } |
10140 | Diag(I->getInnerLocStart(), diag::note_struct_class_suggestion) | ||||
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 10141 | << getRedeclDiagFromTagKind(NewTag) |
Richard Trieu | bbf34c0 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 10142 | << FixItHint::CreateReplacement(I->getInnerLocStart(), |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 10143 | TypeWithKeyword::getTagTypeKindName(NewTag)); |
Richard Trieu | bbf34c0 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 10144 | } |
10145 | } | ||||
10146 | return true; | ||||
10147 | } | ||||
10148 | |||||
10149 | // Check for a previous definition. If current tag and definition | ||||
10150 | // are same type, do nothing. If no definition, but disagree with | ||||
10151 | // with previous tag type, give a warning, but no fix-it. | ||||
10152 | const TagDecl *Redecl = Previous->getDefinition() ? | ||||
10153 | Previous->getDefinition() : Previous; | ||||
10154 | if (Redecl->getTagKind() == NewTag) { | ||||
10155 | return true; | ||||
10156 | } | ||||
10157 | |||||
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 10158 | Diag(NewTagLoc, diag::warn_struct_class_tag_mismatch) |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 10159 | << getRedeclDiagFromTagKind(NewTag) << isTemplate << &Name |
10160 | << getRedeclDiagFromTagKind(OldTag); | ||||
Richard Trieu | bbf34c0 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 10161 | Diag(Redecl->getLocation(), diag::note_previous_use); |
10162 | |||||
10163 | // If there is a previous defintion, suggest a fix-it. | ||||
10164 | if (Previous->getDefinition()) { | ||||
10165 | Diag(NewTagLoc, diag::note_struct_class_suggestion) | ||||
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 10166 | << getRedeclDiagFromTagKind(Redecl->getTagKind()) |
Richard Trieu | bbf34c0 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 10167 | << FixItHint::CreateReplacement(SourceRange(NewTagLoc), |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 10168 | TypeWithKeyword::getTagTypeKindName(Redecl->getTagKind())); |
Richard Trieu | bbf34c0 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 10169 | } |
10170 | |||||
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 10171 | return true; |
10172 | } | ||||
10173 | return false; | ||||
10174 | } | ||||
10175 | |||||
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 10176 | /// 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] | 10177 | /// 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] | 10178 | /// 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] | 10179 | /// reference/declaration/definition of a tag. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10180 | Decl *Sema::ActOnTag(Scope *S, unsigned TagSpec, TagUseKind TUK, |
Douglas Gregor | 069ea64 | 2010-09-16 23:58:57 +0000 | [diff] [blame] | 10181 | SourceLocation KWLoc, CXXScopeSpec &SS, |
10182 | IdentifierInfo *Name, SourceLocation NameLoc, | ||||
10183 | AttributeList *Attr, AccessSpecifier AS, | ||||
Douglas Gregor | e761230 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 10184 | SourceLocation ModulePrivateLoc, |
Douglas Gregor | 069ea64 | 2010-09-16 23:58:57 +0000 | [diff] [blame] | 10185 | MultiTemplateParamsArg TemplateParameterLists, |
Abramo Bagnara | a88cefd | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 10186 | bool &OwnedDecl, bool &IsDependent, |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 10187 | SourceLocation ScopedEnumKWLoc, |
10188 | bool ScopedEnumUsesClassTag, | ||||
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 10189 | TypeResult UnderlyingType) { |
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 10190 | // If this is not a definition, it must have a name. |
Douglas Gregor | 6960587 | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 10191 | IdentifierInfo *OrigName = Name; |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 10192 | assert((Name != 0 || TUK == TUK_Definition) && |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 10193 | "Nameless record must be a definition!"); |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 10194 | assert(TemplateParameterLists.size() == 0 || TUK != TUK_Reference); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 10195 | |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 10196 | OwnedDecl = false; |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 10197 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 10198 | bool ScopedEnum = ScopedEnumKWLoc.isValid(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10199 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 10200 | // FIXME: Check explicit specializations more carefully. |
10201 | bool isExplicitSpecialization = false; | ||||
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 10202 | bool Invalid = false; |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 10203 | |
10204 | // We only need to do this matching if we have template parameters | ||||
10205 | // or a scope specifier, which also conveniently avoids this work | ||||
10206 | // for non-C++ cases. | ||||
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 10207 | if (TemplateParameterLists.size() > 0 || |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 10208 | (SS.isNotEmpty() && TUK != TUK_Reference)) { |
Robert Wilhelm | 1169e2f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 10209 | if (TemplateParameterList *TemplateParams = |
10210 | MatchTemplateParametersToScopeSpecifier( | ||||
10211 | KWLoc, NameLoc, SS, TemplateParameterLists, TUK == TUK_Friend, | ||||
10212 | isExplicitSpecialization, Invalid)) { | ||||
Richard Smith | 725fe0e | 2013-04-01 21:43:41 +0000 | [diff] [blame] | 10213 | if (Kind == TTK_Enum) { |
10214 | Diag(KWLoc, diag::err_enum_template); | ||||
10215 | return 0; | ||||
10216 | } | ||||
10217 | |||||
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 10218 | if (TemplateParams->size() > 0) { |
Douglas Gregor | 7cdbc58 | 2009-07-22 23:48:44 +0000 | [diff] [blame] | 10219 | // This is a declaration or definition of a class template (which may |
10220 | // be a member of another template). | ||||
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 10221 | |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 10222 | if (Invalid) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10223 | return 0; |
Abramo Bagnara | c57c17d | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 10224 | |
Douglas Gregor | 7cdbc58 | 2009-07-22 23:48:44 +0000 | [diff] [blame] | 10225 | OwnedDecl = false; |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 10226 | DeclResult Result = CheckClassTemplate(S, TagSpec, TUK, KWLoc, |
Douglas Gregor | 7cdbc58 | 2009-07-22 23:48:44 +0000 | [diff] [blame] | 10227 | SS, Name, NameLoc, Attr, |
Abramo Bagnara | c57c17d | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 10228 | TemplateParams, AS, |
Douglas Gregor | e761230 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 10229 | ModulePrivateLoc, |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 10230 | TemplateParameterLists.size()-1, |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 10231 | TemplateParameterLists.data()); |
Douglas Gregor | 7cdbc58 | 2009-07-22 23:48:44 +0000 | [diff] [blame] | 10232 | return Result.get(); |
10233 | } else { | ||||
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 10234 | // The "template<>" header is extraneous. |
10235 | Diag(TemplateParams->getTemplateLoc(), diag::err_template_tag_noparams) | ||||
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 10236 | << TypeWithKeyword::getTagTypeKindName(Kind) << Name; |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 10237 | isExplicitSpecialization = true; |
Douglas Gregor | 7cdbc58 | 2009-07-22 23:48:44 +0000 | [diff] [blame] | 10238 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10239 | } |
10240 | } | ||||
10241 | |||||
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 10242 | // Figure out the underlying type if this a enum declaration. We need to do |
10243 | // this early, because it's needed to detect if this is an incompatible | ||||
10244 | // redeclaration. | ||||
10245 | llvm::PointerUnion<const Type*, TypeSourceInfo*> EnumUnderlying; | ||||
10246 | |||||
10247 | if (Kind == TTK_Enum) { | ||||
10248 | if (UnderlyingType.isInvalid() || (!UnderlyingType.get() && ScopedEnum)) | ||||
10249 | // No underlying type explicitly specified, or we failed to parse the | ||||
10250 | // type, default to int. | ||||
10251 | EnumUnderlying = Context.IntTy.getTypePtr(); | ||||
10252 | else if (UnderlyingType.get()) { | ||||
10253 | // C++0x 7.2p2: The type-specifier-seq of an enum-base shall name an | ||||
10254 | // integral type; any cv-qualification is ignored. | ||||
10255 | TypeSourceInfo *TI = 0; | ||||
Richard Smith | 878416d | 2012-03-15 00:22:18 +0000 | [diff] [blame] | 10256 | GetTypeFromParser(UnderlyingType.get(), &TI); |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 10257 | EnumUnderlying = TI; |
10258 | |||||
Richard Smith | f1c66b4 | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 10259 | if (CheckEnumUnderlyingType(TI)) |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 10260 | // Recover by falling back to int. |
10261 | EnumUnderlying = Context.IntTy.getTypePtr(); | ||||
Douglas Gregor | 0c9e479 | 2010-12-16 00:24:44 +0000 | [diff] [blame] | 10262 | |
Richard Smith | f1c66b4 | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 10263 | if (DiagnoseUnexpandedParameterPack(TI->getTypeLoc().getBeginLoc(), TI, |
Douglas Gregor | 0c9e479 | 2010-12-16 00:24:44 +0000 | [diff] [blame] | 10264 | UPPC_FixedUnderlyingType)) |
10265 | EnumUnderlying = Context.IntTy.getTypePtr(); | ||||
10266 | |||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10267 | } else if (getLangOpts().MicrosoftMode) |
Francois Pichet | 842e7a2 | 2010-10-18 15:01:13 +0000 | [diff] [blame] | 10268 | // Microsoft enums are always of int type. |
10269 | EnumUnderlying = Context.IntTy.getTypePtr(); | ||||
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 10270 | } |
10271 | |||||
Douglas Gregor | 4920f1f | 2009-01-12 22:49:06 +0000 | [diff] [blame] | 10272 | DeclContext *SearchDC = CurContext; |
Argyrios Kyrtzidis | 0f84a23 | 2008-11-09 22:53:32 +0000 | [diff] [blame] | 10273 | DeclContext *DC = CurContext; |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 10274 | bool isStdBadAlloc = false; |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 10275 | |
Chandler Carruth | 7bf3600 | 2010-03-01 21:17:36 +0000 | [diff] [blame] | 10276 | RedeclarationKind Redecl = ForRedeclaration; |
10277 | if (TUK == TUK_Friend || TUK == TUK_Reference) | ||||
10278 | Redecl = NotForRedeclaration; | ||||
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 10279 | |
10280 | LookupResult Previous(*this, Name, NameLoc, LookupTagName, Redecl); | ||||
Douglas Gregor | d943352 | 2013-06-27 20:42:30 +0000 | [diff] [blame] | 10281 | bool FriendSawTagOutsideEnclosingNamespace = false; |
Argyrios Kyrtzidis | 630c81b | 2008-11-09 22:09:58 +0000 | [diff] [blame] | 10282 | if (Name && SS.isNotEmpty()) { |
10283 | // We have a nested-name tag ('struct foo::bar'). | ||||
10284 | |||||
10285 | // Check for invalid 'foo::'. | ||||
Argyrios Kyrtzidis | 0f84a23 | 2008-11-09 22:53:32 +0000 | [diff] [blame] | 10286 | if (SS.isInvalid()) { |
Argyrios Kyrtzidis | 630c81b | 2008-11-09 22:09:58 +0000 | [diff] [blame] | 10287 | Name = 0; |
10288 | goto CreateNewDecl; | ||||
10289 | } | ||||
10290 | |||||
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 10291 | // If this is a friend or a reference to a class in a dependent |
10292 | // context, don't try to make a decl for it. | ||||
10293 | if (TUK == TUK_Friend || TUK == TUK_Reference) { | ||||
10294 | DC = computeDeclContext(SS, false); | ||||
10295 | if (!DC) { | ||||
10296 | IsDependent = true; | ||||
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10297 | return 0; |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 10298 | } |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 10299 | } else { |
10300 | DC = computeDeclContext(SS, true); | ||||
10301 | if (!DC) { | ||||
10302 | Diag(SS.getRange().getBegin(), diag::err_dependent_nested_name_spec) | ||||
10303 | << SS.getRange(); | ||||
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10304 | return 0; |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 10305 | } |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 10306 | } |
10307 | |||||
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 10308 | if (RequireCompleteDeclContext(SS, DC)) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10309 | return 0; |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 10310 | |
Douglas Gregor | 1931b44 | 2009-02-03 00:34:39 +0000 | [diff] [blame] | 10311 | SearchDC = DC; |
Argyrios Kyrtzidis | 0f84a23 | 2008-11-09 22:53:32 +0000 | [diff] [blame] | 10312 | // Look-up name inside 'foo::'. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 10313 | LookupQualifiedName(Previous, DC); |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 10314 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 10315 | if (Previous.isAmbiguous()) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10316 | return 0; |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 10317 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 10318 | if (Previous.empty()) { |
Douglas Gregor | 9edad9b | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 10319 | // Name lookup did not find anything. However, if the |
10320 | // nested-name-specifier refers to the current instantiation, | ||||
10321 | // and that current instantiation has any dependent base | ||||
10322 | // classes, we might find something at instantiation time: treat | ||||
10323 | // this as a dependent elaborated-type-specifier. | ||||
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 10324 | // But this only makes any sense for reference-like lookups. |
10325 | if (Previous.wasNotFoundInCurrentInstantiation() && | ||||
10326 | (TUK == TUK_Reference || TUK == TUK_Friend)) { | ||||
Douglas Gregor | 9edad9b | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 10327 | IsDependent = true; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10328 | return 0; |
Douglas Gregor | 9edad9b | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 10329 | } |
10330 | |||||
10331 | // A tag 'foo::bar' must already exist. | ||||
Douglas Gregor | 1eabb7d | 2010-03-31 23:17:41 +0000 | [diff] [blame] | 10332 | Diag(NameLoc, diag::err_not_tag_in_scope) |
10333 | << Kind << Name << DC << SS.getRange(); | ||||
Argyrios Kyrtzidis | 630c81b | 2008-11-09 22:09:58 +0000 | [diff] [blame] | 10334 | Name = 0; |
Douglas Gregor | d0c8737 | 2009-05-27 17:30:49 +0000 | [diff] [blame] | 10335 | Invalid = true; |
Argyrios Kyrtzidis | 630c81b | 2008-11-09 22:09:58 +0000 | [diff] [blame] | 10336 | goto CreateNewDecl; |
10337 | } | ||||
Chris Lattner | cf79b01 | 2009-01-21 02:38:50 +0000 | [diff] [blame] | 10338 | } else if (Name) { |
Argyrios Kyrtzidis | 630c81b | 2008-11-09 22:09:58 +0000 | [diff] [blame] | 10339 | // If this is a named struct, check to see if there was a previous forward |
10340 | // declaration or definition. | ||||
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 10341 | // FIXME: We're looking into outer scopes here, even when we |
10342 | // shouldn't be. Doing so can result in ambiguities that we | ||||
10343 | // shouldn't be diagnosing. | ||||
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 10344 | LookupName(Previous, S); |
10345 | |||||
John McCall | c96cd7a | 2013-03-20 01:53:00 +0000 | [diff] [blame] | 10346 | // When declaring or defining a tag, ignore ambiguities introduced |
10347 | // by types using'ed into this scope. | ||||
Douglas Gregor | 93b6bce | 2011-05-09 21:46:33 +0000 | [diff] [blame] | 10348 | if (Previous.isAmbiguous() && |
10349 | (TUK == TUK_Definition || TUK == TUK_Declaration)) { | ||||
Douglas Gregor | 61c6c44 | 2011-05-04 00:25:33 +0000 | [diff] [blame] | 10350 | LookupResult::Filter F = Previous.makeFilter(); |
10351 | while (F.hasNext()) { | ||||
10352 | NamedDecl *ND = F.next(); | ||||
10353 | if (ND->getDeclContext()->getRedeclContext() != SearchDC) | ||||
10354 | F.erase(); | ||||
10355 | } | ||||
10356 | F.done(); | ||||
Douglas Gregor | 61c6c44 | 2011-05-04 00:25:33 +0000 | [diff] [blame] | 10357 | } |
John McCall | c96cd7a | 2013-03-20 01:53:00 +0000 | [diff] [blame] | 10358 | |
10359 | // C++11 [namespace.memdef]p3: | ||||
10360 | // If the name in a friend declaration is neither qualified nor | ||||
10361 | // a template-id and the declaration is a function or an | ||||
10362 | // elaborated-type-specifier, the lookup to determine whether | ||||
10363 | // the entity has been previously declared shall not consider | ||||
10364 | // any scopes outside the innermost enclosing namespace. | ||||
10365 | // | ||||
10366 | // Does it matter that this should be by scope instead of by | ||||
10367 | // semantic context? | ||||
10368 | if (!Previous.empty() && TUK == TUK_Friend) { | ||||
10369 | DeclContext *EnclosingNS = SearchDC->getEnclosingNamespaceContext(); | ||||
10370 | LookupResult::Filter F = Previous.makeFilter(); | ||||
10371 | while (F.hasNext()) { | ||||
10372 | NamedDecl *ND = F.next(); | ||||
10373 | DeclContext *DC = ND->getDeclContext()->getRedeclContext(); | ||||
Douglas Gregor | d943352 | 2013-06-27 20:42:30 +0000 | [diff] [blame] | 10374 | if (DC->isFileContext() && |
10375 | !EnclosingNS->Encloses(ND->getDeclContext())) { | ||||
John McCall | c96cd7a | 2013-03-20 01:53:00 +0000 | [diff] [blame] | 10376 | F.erase(); |
Douglas Gregor | d943352 | 2013-06-27 20:42:30 +0000 | [diff] [blame] | 10377 | FriendSawTagOutsideEnclosingNamespace = true; |
10378 | } | ||||
John McCall | c96cd7a | 2013-03-20 01:53:00 +0000 | [diff] [blame] | 10379 | } |
10380 | F.done(); | ||||
10381 | } | ||||
Douglas Gregor | 61c6c44 | 2011-05-04 00:25:33 +0000 | [diff] [blame] | 10382 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 10383 | // Note: there used to be some attempt at recovery here. |
10384 | if (Previous.isAmbiguous()) | ||||
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10385 | return 0; |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 10386 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10387 | if (!getLangOpts().CPlusPlus && TUK != TUK_Reference) { |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 10388 | // FIXME: This makes sure that we ignore the contexts associated |
10389 | // with C structs, unions, and enums when looking for a matching | ||||
10390 | // tag declaration or definition. See the similar lookup tweak | ||||
Douglas Gregor | 4c921ae | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 10391 | // in Sema::LookupName; is there a better way to deal with this? |
Douglas Gregor | 4920f1f | 2009-01-12 22:49:06 +0000 | [diff] [blame] | 10392 | while (isa<RecordDecl>(SearchDC) || isa<EnumDecl>(SearchDC)) |
10393 | SearchDC = SearchDC->getParent(); | ||||
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 10394 | } |
Douglas Gregor | 069ea64 | 2010-09-16 23:58:57 +0000 | [diff] [blame] | 10395 | } else if (S->isFunctionPrototypeScope()) { |
10396 | // If this is an enum declaration in function prototype scope, set its | ||||
10397 | // initial context to the translation unit. | ||||
Nick Lewycky | 8d17681 | 2012-03-10 07:45:33 +0000 | [diff] [blame] | 10398 | // FIXME: [citation needed] |
Douglas Gregor | 069ea64 | 2010-09-16 23:58:57 +0000 | [diff] [blame] | 10399 | SearchDC = Context.getTranslationUnitDecl(); |
Argyrios Kyrtzidis | 630c81b | 2008-11-09 22:09:58 +0000 | [diff] [blame] | 10400 | } |
10401 | |||||
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 10402 | if (Previous.isSingleResult() && |
10403 | Previous.getFoundDecl()->isTemplateParameter()) { | ||||
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 10404 | // Maybe we will complain about the shadowed template parameter. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 10405 | DiagnoseTemplateParameterShadow(NameLoc, Previous.getFoundDecl()); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 10406 | // Just pretend that we didn't see the previous declaration. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 10407 | Previous.clear(); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 10408 | } |
10409 | |||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10410 | if (getLangOpts().CPlusPlus && Name && DC && StdNamespace && |
Argyrios Kyrtzidis | 76c38d3 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 10411 | DC->Equals(getStdNamespace()) && Name->isStr("bad_alloc")) { |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 10412 | // This is a declaration of or a reference to "std::bad_alloc". |
10413 | isStdBadAlloc = true; | ||||
10414 | |||||
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 10415 | if (Previous.empty() && StdBadAlloc) { |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 10416 | // std::bad_alloc has been implicitly declared (but made invisible to |
10417 | // name lookup). Fill in this implicit declaration as the previous | ||||
10418 | // declaration, so that the declarations get chained appropriately. | ||||
Argyrios Kyrtzidis | 76c38d3 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 10419 | Previous.addDecl(getStdBadAlloc()); |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 10420 | } |
10421 | } | ||||
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 10422 | |
John McCall | 9c86b51 | 2010-03-25 21:28:06 +0000 | [diff] [blame] | 10423 | // If we didn't find a previous declaration, and this is a reference |
10424 | // (or friend reference), move to the correct scope. In C++, we | ||||
10425 | // also need to do a redeclaration lookup there, just in case | ||||
10426 | // there's a shadow friend decl. | ||||
10427 | if (Name && Previous.empty() && | ||||
10428 | (TUK == TUK_Reference || TUK == TUK_Friend)) { | ||||
10429 | if (Invalid) goto CreateNewDecl; | ||||
10430 | assert(SS.isEmpty()); | ||||
10431 | |||||
10432 | if (TUK == TUK_Reference) { | ||||
10433 | // C++ [basic.scope.pdecl]p5: | ||||
10434 | // -- for an elaborated-type-specifier of the form | ||||
10435 | // | ||||
10436 | // class-key identifier | ||||
10437 | // | ||||
10438 | // if the elaborated-type-specifier is used in the | ||||
10439 | // decl-specifier-seq or parameter-declaration-clause of a | ||||
10440 | // function defined in namespace scope, the identifier is | ||||
10441 | // declared as a class-name in the namespace that contains | ||||
10442 | // the declaration; otherwise, except as a friend | ||||
10443 | // declaration, the identifier is declared in the smallest | ||||
10444 | // non-class, non-function-prototype scope that contains the | ||||
10445 | // declaration. | ||||
10446 | // | ||||
10447 | // C99 6.7.2.3p8 has a similar (but not identical!) provision for | ||||
10448 | // C structs and unions. | ||||
10449 | // | ||||
10450 | // It is an error in C++ to declare (rather than define) an enum | ||||
10451 | // type, including via an elaborated type specifier. We'll | ||||
10452 | // diagnose that later; for now, declare the enum in the same | ||||
10453 | // scope as we would have picked for any other tag type. | ||||
10454 | // | ||||
10455 | // GNU C also supports this behavior as part of its incomplete | ||||
10456 | // enum types extension, while GNU C++ does not. | ||||
10457 | // | ||||
10458 | // Find the context where we'll be declaring the tag. | ||||
10459 | // FIXME: We would like to maintain the current DeclContext as the | ||||
10460 | // lexical context, | ||||
Nick Lewycky | 1659c37 | 2012-03-10 07:47:07 +0000 | [diff] [blame] | 10461 | while (!SearchDC->isFileContext() && !SearchDC->isFunctionOrMethod()) |
John McCall | 9c86b51 | 2010-03-25 21:28:06 +0000 | [diff] [blame] | 10462 | SearchDC = SearchDC->getParent(); |
10463 | |||||
10464 | // Find the scope where we'll be declaring the tag. | ||||
10465 | while (S->isClassScope() || | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10466 | (getLangOpts().CPlusPlus && |
John McCall | 9c86b51 | 2010-03-25 21:28:06 +0000 | [diff] [blame] | 10467 | S->isFunctionPrototypeScope()) || |
10468 | ((S->getFlags() & Scope::DeclScope) == 0) || | ||||
10469 | (S->getEntity() && | ||||
10470 | ((DeclContext *)S->getEntity())->isTransparentContext())) | ||||
10471 | S = S->getParent(); | ||||
10472 | } else { | ||||
10473 | assert(TUK == TUK_Friend); | ||||
10474 | // C++ [namespace.memdef]p3: | ||||
10475 | // If a friend declaration in a non-local class first declares a | ||||
10476 | // class or function, the friend class or function is a member of | ||||
10477 | // the innermost enclosing namespace. | ||||
10478 | SearchDC = SearchDC->getEnclosingNamespaceContext(); | ||||
John McCall | 9c86b51 | 2010-03-25 21:28:06 +0000 | [diff] [blame] | 10479 | } |
10480 | |||||
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 10481 | // In C++, we need to do a redeclaration lookup to properly |
10482 | // diagnose some problems. | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10483 | if (getLangOpts().CPlusPlus) { |
John McCall | 9c86b51 | 2010-03-25 21:28:06 +0000 | [diff] [blame] | 10484 | Previous.setRedeclarationKind(ForRedeclaration); |
10485 | LookupQualifiedName(Previous, SearchDC); | ||||
10486 | } | ||||
10487 | } | ||||
10488 | |||||
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 10489 | if (!Previous.empty()) { |
Douglas Gregor | 57265e3 | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 10490 | NamedDecl *PrevDecl = (*Previous.begin())->getUnderlyingDecl(); |
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 10491 | |
10492 | // It's okay to have a tag decl in the same scope as a typedef | ||||
10493 | // which hides a tag decl in the same scope. Finding this | ||||
10494 | // insanity with a redeclaration lookup can only actually happen | ||||
10495 | // in C++. | ||||
10496 | // | ||||
10497 | // This is also okay for elaborated-type-specifiers, which is | ||||
10498 | // technically forbidden by the current standard but which is | ||||
10499 | // okay according to the likely resolution of an open issue; | ||||
10500 | // 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] | 10501 | if (getLangOpts().CPlusPlus) { |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 10502 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(PrevDecl)) { |
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 10503 | if (const TagType *TT = TD->getUnderlyingType()->getAs<TagType>()) { |
10504 | TagDecl *Tag = TT->getDecl(); | ||||
10505 | if (Tag->getDeclName() == Name && | ||||
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 10506 | Tag->getDeclContext()->getRedeclContext() |
10507 | ->Equals(TD->getDeclContext()->getRedeclContext())) { | ||||
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 10508 | PrevDecl = Tag; |
10509 | Previous.clear(); | ||||
10510 | Previous.addDecl(Tag); | ||||
Douglas Gregor | 757c600 | 2010-08-27 22:55:10 +0000 | [diff] [blame] | 10511 | Previous.resolveKind(); |
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 10512 | } |
10513 | } | ||||
10514 | } | ||||
10515 | } | ||||
10516 | |||||
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 10517 | if (TagDecl *PrevTagDecl = dyn_cast<TagDecl>(PrevDecl)) { |
Chris Lattner | 14943b9 | 2008-07-03 03:30:58 +0000 | [diff] [blame] | 10518 | // If this is a use of a previous tag, or if the tag is already declared |
10519 | // in the same scope (so that the definition/declaration completes or | ||||
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 10520 | // rementions the tag), reuse the decl. |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 10521 | if (TUK == TUK_Reference || TUK == TUK_Friend || |
Douglas Gregor | cc20945 | 2011-03-07 16:54:27 +0000 | [diff] [blame] | 10522 | isDeclInScope(PrevDecl, SearchDC, S, isExplicitSpecialization)) { |
Chris Lattner | 14943b9 | 2008-07-03 03:30:58 +0000 | [diff] [blame] | 10523 | // Make sure that this wasn't declared as an enum and now used as a |
10524 | // struct or something similar. | ||||
Richard Trieu | bbf34c0 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 10525 | if (!isAcceptableTagRedeclaration(PrevTagDecl, Kind, |
10526 | TUK == TUK_Definition, KWLoc, | ||||
10527 | *Name)) { | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10528 | bool SafeToContinue |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 10529 | = (PrevTagDecl->getTagKind() != TTK_Enum && |
10530 | Kind != TTK_Enum); | ||||
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 10531 | if (SafeToContinue) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10532 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 10533 | << Name |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 10534 | << FixItHint::CreateReplacement(SourceRange(KWLoc), |
10535 | PrevTagDecl->getKindName()); | ||||
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 10536 | else |
10537 | Diag(KWLoc, diag::err_use_with_wrong_tag) << Name; | ||||
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 10538 | Diag(PrevTagDecl->getLocation(), diag::note_previous_use); |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 10539 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10540 | if (SafeToContinue) |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 10541 | Kind = PrevTagDecl->getTagKind(); |
10542 | else { | ||||
10543 | // Recover by making this an anonymous redefinition. | ||||
10544 | Name = 0; | ||||
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 10545 | Previous.clear(); |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 10546 | Invalid = true; |
10547 | } | ||||
10548 | } | ||||
10549 | |||||
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 10550 | if (Kind == TTK_Enum && PrevTagDecl->getTagKind() == TTK_Enum) { |
10551 | const EnumDecl *PrevEnum = cast<EnumDecl>(PrevTagDecl); | ||||
10552 | |||||
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 10553 | // If this is an elaborated-type-specifier for a scoped enumeration, |
10554 | // the 'class' keyword is not necessary and not permitted. | ||||
10555 | if (TUK == TUK_Reference || TUK == TUK_Friend) { | ||||
10556 | if (ScopedEnum) | ||||
10557 | Diag(ScopedEnumKWLoc, diag::err_enum_class_reference) | ||||
10558 | << PrevEnum->isScoped() | ||||
10559 | << FixItHint::CreateRemoval(ScopedEnumKWLoc); | ||||
10560 | return PrevTagDecl; | ||||
10561 | } | ||||
10562 | |||||
Richard Smith | f1c66b4 | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 10563 | QualType EnumUnderlyingTy; |
10564 | if (TypeSourceInfo *TI = EnumUnderlying.dyn_cast<TypeSourceInfo*>()) | ||||
10565 | EnumUnderlyingTy = TI->getType(); | ||||
10566 | else if (const Type *T = EnumUnderlying.dyn_cast<const Type*>()) | ||||
10567 | EnumUnderlyingTy = QualType(T, 0); | ||||
10568 | |||||
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 10569 | // All conflicts with previous declarations are recovered by |
Richard Smith | 3343fad | 2012-03-23 23:09:08 +0000 | [diff] [blame] | 10570 | // returning the previous declaration, unless this is a definition, |
10571 | // in which case we want the caller to bail out. | ||||
Richard Smith | f1c66b4 | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 10572 | if (CheckEnumRedeclaration(NameLoc.isValid() ? NameLoc : KWLoc, |
10573 | ScopedEnum, EnumUnderlyingTy, PrevEnum)) | ||||
Richard Smith | 3343fad | 2012-03-23 23:09:08 +0000 | [diff] [blame] | 10574 | return TUK == TUK_Declaration ? PrevTagDecl : 0; |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 10575 | } |
10576 | |||||
David Majnemer | 2ec2b84 | 2013-06-11 03:51:23 +0000 | [diff] [blame] | 10577 | // C++11 [class.mem]p1: |
David Majnemer | 0f9b855 | 2013-06-11 06:19:45 +0000 | [diff] [blame] | 10578 | // A member shall not be declared twice in the member-specification, |
David Majnemer | 2ec2b84 | 2013-06-11 03:51:23 +0000 | [diff] [blame] | 10579 | // except that a nested class or member class template can be declared |
10580 | // and then later defined. | ||||
10581 | if (TUK == TUK_Declaration && PrevDecl->isCXXClassMember() && | ||||
10582 | S->isDeclScope(PrevDecl)) { | ||||
10583 | Diag(NameLoc, diag::ext_member_redeclared); | ||||
10584 | Diag(PrevTagDecl->getLocation(), diag::note_previous_declaration); | ||||
10585 | } | ||||
10586 | |||||
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 10587 | if (!Invalid) { |
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 10588 | // If this is a use, just return the declaration we found. |
Chris Lattner | 14943b9 | 2008-07-03 03:30:58 +0000 | [diff] [blame] | 10589 | |
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 10590 | // FIXME: In the future, return a variant or some other clue |
10591 | // for the consumer of this Decl to know it doesn't own it. | ||||
10592 | // For our current ASTs this shouldn't be a problem, but will | ||||
10593 | // need to be changed with DeclGroups. | ||||
Francois Pichet | b474603 | 2011-06-01 04:14:20 +0000 | [diff] [blame] | 10594 | if ((TUK == TUK_Reference && (!PrevTagDecl->getFriendObjectKind() || |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10595 | getLangOpts().MicrosoftExt)) || TUK == TUK_Friend) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10596 | return PrevTagDecl; |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 10597 | |
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 10598 | // Diagnose attempts to redefine a tag. |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 10599 | if (TUK == TUK_Definition) { |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 10600 | if (TagDecl *Def = PrevTagDecl->getDefinition()) { |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 10601 | // If we're defining a specialization and the previous definition |
10602 | // is from an implicit instantiation, don't emit an error | ||||
10603 | // here; we'll catch this in the general case below. | ||||
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 10604 | bool IsExplicitSpecializationAfterInstantiation = false; |
10605 | if (isExplicitSpecialization) { | ||||
10606 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Def)) | ||||
10607 | IsExplicitSpecializationAfterInstantiation = | ||||
10608 | RD->getTemplateSpecializationKind() != | ||||
10609 | TSK_ExplicitSpecialization; | ||||
10610 | else if (EnumDecl *ED = dyn_cast<EnumDecl>(Def)) | ||||
10611 | IsExplicitSpecializationAfterInstantiation = | ||||
10612 | ED->getTemplateSpecializationKind() != | ||||
10613 | TSK_ExplicitSpecialization; | ||||
10614 | } | ||||
10615 | |||||
10616 | if (!IsExplicitSpecializationAfterInstantiation) { | ||||
James Molloy | 16f1f71 | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 10617 | // A redeclaration in function prototype scope in C isn't |
10618 | // visible elsewhere, so merely issue a warning. | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10619 | if (!getLangOpts().CPlusPlus && S->containedInPrototypeScope()) |
James Molloy | 16f1f71 | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 10620 | Diag(NameLoc, diag::warn_redefinition_in_param_list) << Name; |
10621 | else | ||||
10622 | Diag(NameLoc, diag::err_redefinition) << Name; | ||||
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 10623 | Diag(Def->getLocation(), diag::note_previous_definition); |
10624 | // If this is a redefinition, recover by making this | ||||
10625 | // struct be anonymous, which will make any later | ||||
10626 | // references get the previous definition. | ||||
10627 | Name = 0; | ||||
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 10628 | Previous.clear(); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 10629 | Invalid = true; |
10630 | } | ||||
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 10631 | } else { |
10632 | // If the type is currently being defined, complain | ||||
10633 | // about a nested redefinition. | ||||
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 10634 | const TagType *Tag |
10635 | = cast<TagType>(Context.getTagDeclType(PrevTagDecl)); | ||||
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 10636 | if (Tag->isBeingDefined()) { |
10637 | Diag(NameLoc, diag::err_nested_redefinition) << Name; | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10638 | Diag(PrevTagDecl->getLocation(), |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 10639 | diag::note_previous_definition); |
10640 | Name = 0; | ||||
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 10641 | Previous.clear(); |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 10642 | Invalid = true; |
10643 | } | ||||
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 10644 | } |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 10645 | |
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 10646 | // Okay, this is definition of a previously declared or referenced |
10647 | // tag PrevDecl. We're going to create a new Decl for it. | ||||
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 10648 | } |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 10649 | } |
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 10650 | // If we get here we have (another) forward declaration or we |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 10651 | // have a definition. Just create a new decl. |
10652 | |||||
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 10653 | } else { |
10654 | // 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] | 10655 | // scope, e.g. "struct foo; void bar() { struct foo; }", just create a |
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 10656 | // new decl/type. We set PrevDecl to NULL so that the entities |
10657 | // have distinct types. | ||||
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 10658 | Previous.clear(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 10659 | } |
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 10660 | // If we get here, we're going to create a new Decl. If PrevDecl |
10661 | // is non-NULL, it's a definition of the tag declared by | ||||
10662 | // PrevDecl. If it's NULL, we have a new definition. | ||||
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 10663 | |
10664 | |||||
10665 | // Otherwise, PrevDecl is not a tag, but was found with tag | ||||
10666 | // lookup. This is only actually possible in C++, where a few | ||||
10667 | // things like templates still live in the tag namespace. | ||||
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 10668 | } else { |
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 10669 | // Use a better diagnostic if an elaborated-type-specifier |
10670 | // found the wrong kind of type on the first | ||||
10671 | // (non-redeclaration) lookup. | ||||
10672 | if ((TUK == TUK_Reference || TUK == TUK_Friend) && | ||||
10673 | !Previous.isForRedeclaration()) { | ||||
10674 | unsigned Kind = 0; | ||||
10675 | if (isa<TypedefDecl>(PrevDecl)) Kind = 1; | ||||
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 10676 | else if (isa<TypeAliasDecl>(PrevDecl)) Kind = 2; |
10677 | else if (isa<ClassTemplateDecl>(PrevDecl)) Kind = 3; | ||||
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 10678 | Diag(NameLoc, diag::err_tag_reference_non_tag) << Kind; |
10679 | Diag(PrevDecl->getLocation(), diag::note_declared_at); | ||||
10680 | Invalid = true; | ||||
10681 | |||||
10682 | // Otherwise, only diagnose if the declaration is in scope. | ||||
Douglas Gregor | cc20945 | 2011-03-07 16:54:27 +0000 | [diff] [blame] | 10683 | } else if (!isDeclInScope(PrevDecl, SearchDC, S, |
10684 | isExplicitSpecialization)) { | ||||
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 10685 | // do nothing |
10686 | |||||
10687 | // Diagnose implicit declarations introduced by elaborated types. | ||||
10688 | } else if (TUK == TUK_Reference || TUK == TUK_Friend) { | ||||
10689 | unsigned Kind = 0; | ||||
10690 | if (isa<TypedefDecl>(PrevDecl)) Kind = 1; | ||||
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 10691 | else if (isa<TypeAliasDecl>(PrevDecl)) Kind = 2; |
10692 | else if (isa<ClassTemplateDecl>(PrevDecl)) Kind = 3; | ||||
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 10693 | Diag(NameLoc, diag::err_tag_reference_conflict) << Kind; |
10694 | Diag(PrevDecl->getLocation(), diag::note_previous_decl) << PrevDecl; | ||||
10695 | Invalid = true; | ||||
10696 | |||||
10697 | // Otherwise it's a declaration. Call out a particularly common | ||||
10698 | // case here. | ||||
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 10699 | } else if (TypedefNameDecl *TND = dyn_cast<TypedefNameDecl>(PrevDecl)) { |
10700 | unsigned Kind = 0; | ||||
10701 | if (isa<TypeAliasDecl>(PrevDecl)) Kind = 1; | ||||
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 10702 | Diag(NameLoc, diag::err_tag_definition_of_typedef) |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 10703 | << Name << Kind << TND->getUnderlyingType(); |
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 10704 | Diag(PrevDecl->getLocation(), diag::note_previous_decl) << PrevDecl; |
10705 | Invalid = true; | ||||
10706 | |||||
10707 | // Otherwise, diagnose. | ||||
10708 | } else { | ||||
10709 | // The tag name clashes with something else in the target scope, | ||||
10710 | // issue an error and recover by making this tag be anonymous. | ||||
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 10711 | Diag(NameLoc, diag::err_redefinition_different_kind) << Name; |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 10712 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Argyrios Kyrtzidis | b02ef24 | 2008-07-16 07:45:46 +0000 | [diff] [blame] | 10713 | Name = 0; |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 10714 | Invalid = true; |
Argyrios Kyrtzidis | b02ef24 | 2008-07-16 07:45:46 +0000 | [diff] [blame] | 10715 | } |
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 10716 | |
10717 | // The existing declaration isn't relevant to us; we're in a | ||||
10718 | // new scope, so clear out the previous declaration. | ||||
10719 | Previous.clear(); | ||||
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 10720 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 10721 | } |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 10722 | |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 10723 | CreateNewDecl: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10724 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 10725 | TagDecl *PrevDecl = 0; |
10726 | if (Previous.isSingleResult()) | ||||
10727 | PrevDecl = cast<TagDecl>(Previous.getFoundDecl()); | ||||
10728 | |||||
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 10729 | // If there is an identifier, use the location of the identifier as the |
10730 | // location of the decl, otherwise use the location of the struct/union | ||||
10731 | // keyword. | ||||
10732 | SourceLocation Loc = NameLoc.isValid() ? NameLoc : KWLoc; | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10733 | |
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 10734 | // Otherwise, create a new declaration. If there is a previous |
10735 | // declaration of the same entity, the two will be linked via | ||||
10736 | // PrevDecl. | ||||
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 10737 | TagDecl *New; |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 10738 | |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 10739 | bool IsForwardReference = false; |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 10740 | if (Kind == TTK_Enum) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 10741 | // FIXME: Tag decls should be chained to any simultaneous vardecls, e.g.: |
10742 | // enum X { A, B, C } D; D should chain to X. | ||||
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 10743 | New = EnumDecl::Create(Context, SearchDC, KWLoc, Loc, Name, |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 10744 | cast_or_null<EnumDecl>(PrevDecl), ScopedEnum, |
Abramo Bagnara | a88cefd | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 10745 | ScopedEnumUsesClassTag, !EnumUnderlying.isNull()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 10746 | // If this is an undefined enum, warn. |
Douglas Gregor | f3a7b7c | 2010-06-22 14:26:35 +0000 | [diff] [blame] | 10747 | if (TUK != TUK_Definition && !Invalid) { |
10748 | TagDecl *Def; | ||||
Douglas Gregor | abde2c7 | 2013-03-25 22:22:35 +0000 | [diff] [blame] | 10749 | if ((getLangOpts().CPlusPlus11 || getLangOpts().ObjC2) && |
10750 | cast<EnumDecl>(New)->isFixed()) { | ||||
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 10751 | // C++0x: 7.2p2: opaque-enum-declaration. |
10752 | // Conflicts are diagnosed above. Do nothing. | ||||
10753 | } | ||||
10754 | else if (PrevDecl && (Def = cast<EnumDecl>(PrevDecl)->getDefinition())) { | ||||
Douglas Gregor | f3a7b7c | 2010-06-22 14:26:35 +0000 | [diff] [blame] | 10755 | Diag(Loc, diag::ext_forward_ref_enum_def) |
10756 | << New; | ||||
10757 | Diag(Def->getLocation(), diag::note_previous_definition); | ||||
10758 | } else { | ||||
Francois Pichet | 8dc3abc | 2010-09-12 05:06:55 +0000 | [diff] [blame] | 10759 | unsigned DiagID = diag::ext_forward_ref_enum; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10760 | if (getLangOpts().MicrosoftMode) |
Francois Pichet | 8dc3abc | 2010-09-12 05:06:55 +0000 | [diff] [blame] | 10761 | DiagID = diag::ext_ms_forward_ref_enum; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10762 | else if (getLangOpts().CPlusPlus) |
Francois Pichet | 8dc3abc | 2010-09-12 05:06:55 +0000 | [diff] [blame] | 10763 | DiagID = diag::err_forward_ref_enum; |
10764 | Diag(Loc, DiagID); | ||||
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 10765 | |
10766 | // If this is a forward-declared reference to an enumeration, make a | ||||
10767 | // note of it; we won't actually be introducing the declaration into | ||||
10768 | // the declaration context. | ||||
10769 | if (TUK == TUK_Reference) | ||||
10770 | IsForwardReference = true; | ||||
Douglas Gregor | f3a7b7c | 2010-06-22 14:26:35 +0000 | [diff] [blame] | 10771 | } |
Douglas Gregor | 80711a2 | 2009-03-06 18:34:03 +0000 | [diff] [blame] | 10772 | } |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 10773 | |
10774 | if (EnumUnderlying) { | ||||
10775 | EnumDecl *ED = cast<EnumDecl>(New); | ||||
10776 | if (TypeSourceInfo *TI = EnumUnderlying.dyn_cast<TypeSourceInfo*>()) | ||||
10777 | ED->setIntegerTypeSourceInfo(TI); | ||||
10778 | else | ||||
10779 | ED->setIntegerType(QualType(EnumUnderlying.get<const Type*>(), 0)); | ||||
10780 | ED->setPromotionType(ED->getIntegerType()); | ||||
10781 | } | ||||
10782 | |||||
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 10783 | } else { |
10784 | // struct/union/class | ||||
10785 | |||||
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 10786 | // FIXME: Tag decls should be chained to any simultaneous vardecls, e.g.: |
10787 | // struct X { int A; } D; D should chain to X. | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10788 | if (getLangOpts().CPlusPlus) { |
Ted Kremenek | 2b345eb | 2008-09-05 17:39:33 +0000 | [diff] [blame] | 10789 | // FIXME: Look for a way to use RecordDecl for simple structs. |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 10790 | New = CXXRecordDecl::Create(Context, Kind, SearchDC, KWLoc, Loc, Name, |
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 10791 | cast_or_null<CXXRecordDecl>(PrevDecl)); |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 10792 | |
Argyrios Kyrtzidis | 76c38d3 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 10793 | if (isStdBadAlloc && (!StdBadAlloc || getStdBadAlloc()->isImplicit())) |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 10794 | StdBadAlloc = cast<CXXRecordDecl>(New); |
10795 | } else | ||||
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 10796 | New = RecordDecl::Create(Context, Kind, SearchDC, KWLoc, Loc, Name, |
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 10797 | cast_or_null<RecordDecl>(PrevDecl)); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 10798 | } |
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 10799 | |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 10800 | // Maybe add qualifier info. |
10801 | if (SS.isNotEmpty()) { | ||||
Fariborz Jahanian | 4fb2053 | 2010-05-14 21:35:02 +0000 | [diff] [blame] | 10802 | if (SS.isSet()) { |
Douglas Gregor | 6960587 | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 10803 | // If this is either a declaration or a definition, check the |
10804 | // nested-name-specifier against the current context. We don't do this | ||||
10805 | // for explicit specializations, because they have similar checking | ||||
10806 | // (with more specific diagnostics) in the call to | ||||
10807 | // CheckMemberSpecialization, below. | ||||
10808 | if (!isExplicitSpecialization && | ||||
10809 | (TUK == TUK_Definition || TUK == TUK_Declaration) && | ||||
10810 | diagnoseQualifiedDeclaration(SS, DC, OrigName, NameLoc)) | ||||
10811 | Invalid = true; | ||||
10812 | |||||
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 10813 | New->setQualifierInfo(SS.getWithLocInContext(Context)); |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 10814 | if (TemplateParameterLists.size() > 0) { |
Douglas Gregor | c722ea4 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 10815 | New->setTemplateParameterListsInfo(Context, |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 10816 | TemplateParameterLists.size(), |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 10817 | TemplateParameterLists.data()); |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 10818 | } |
Fariborz Jahanian | 4fb2053 | 2010-05-14 21:35:02 +0000 | [diff] [blame] | 10819 | } |
10820 | else | ||||
10821 | Invalid = true; | ||||
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 10822 | } |
10823 | |||||
Daniel Dunbar | 9f21f89 | 2010-05-27 01:53:40 +0000 | [diff] [blame] | 10824 | if (RecordDecl *RD = dyn_cast<RecordDecl>(New)) { |
10825 | // Add alignment attributes if necessary; these attributes are checked when | ||||
10826 | // the ASTContext lays out the structure. | ||||
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 10827 | // |
10828 | // It is important for implementing the correct semantics that this | ||||
10829 | // happen here (in act on tag decl). The #pragma pack stack is | ||||
10830 | // maintained as a result of parser callbacks which can occur at | ||||
10831 | // many points during the parsing of a struct declaration (because | ||||
10832 | // the #pragma tokens are effectively skipped over during the | ||||
10833 | // parsing of the struct). | ||||
Eli Friedman | 2016c8c | 2012-08-08 21:08:34 +0000 | [diff] [blame] | 10834 | if (TUK == TUK_Definition) { |
10835 | AddAlignmentAttributesForRecord(RD); | ||||
10836 | AddMsStructLayoutForRecord(RD); | ||||
10837 | } | ||||
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 10838 | } |
10839 | |||||
Douglas Gregor | 2ccd89c | 2011-12-20 18:11:52 +0000 | [diff] [blame] | 10840 | if (ModulePrivateLoc.isValid()) { |
Douglas Gregor | d023aec | 2011-09-09 20:53:38 +0000 | [diff] [blame] | 10841 | if (isExplicitSpecialization) |
10842 | Diag(New->getLocation(), diag::err_module_private_specialization) | ||||
10843 | << 2 | ||||
10844 | << FixItHint::CreateRemoval(ModulePrivateLoc); | ||||
Douglas Gregor | e389585 | 2011-09-12 18:37:38 +0000 | [diff] [blame] | 10845 | // __module_private__ does not apply to local classes. However, we only |
10846 | // diagnose this as an error when the declaration specifiers are | ||||
10847 | // freestanding. Here, we just ignore the __module_private__. | ||||
Douglas Gregor | e389585 | 2011-09-12 18:37:38 +0000 | [diff] [blame] | 10848 | else if (!SearchDC->isFunctionOrMethod()) |
Douglas Gregor | e761230 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 10849 | New->setModulePrivate(); |
10850 | } | ||||
10851 | |||||
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 10852 | // If this is a specialization of a member class (of a class template), |
10853 | // check the specialization. | ||||
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 10854 | if (isExplicitSpecialization && CheckMemberSpecialization(New, Previous)) |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 10855 | Invalid = true; |
Douglas Gregor | 6960587 | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 10856 | |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 10857 | if (Invalid) |
10858 | New->setInvalidDecl(); | ||||
10859 | |||||
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 10860 | if (Attr) |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 10861 | ProcessDeclAttributeList(S, New, Attr); |
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 10862 | |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 10863 | // If we're declaring or defining a tag in function prototype scope |
10864 | // in C, note that this type can only be used within the function. | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10865 | if (Name && S->isFunctionPrototypeScope() && !getLangOpts().CPlusPlus) |
Douglas Gregor | 3218c4b | 2009-01-09 22:42:13 +0000 | [diff] [blame] | 10866 | Diag(Loc, diag::warn_decl_in_param_list) << Context.getTagDeclType(New); |
10867 | |||||
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 10868 | // Set the lexical context. If the tag has a C++ scope specifier, the |
10869 | // lexical context will be different from the semantic context. | ||||
Douglas Gregor | 1931b44 | 2009-02-03 00:34:39 +0000 | [diff] [blame] | 10870 | New->setLexicalDeclContext(CurContext); |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 10871 | |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 10872 | // Mark this as a friend decl if applicable. |
Francois Pichet | b474603 | 2011-06-01 04:14:20 +0000 | [diff] [blame] | 10873 | // In Microsoft mode, a friend declaration also acts as a forward |
10874 | // declaration so we always pass true to setObjectOfFriendDecl to make | ||||
10875 | // the tag name visible. | ||||
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 10876 | if (TUK == TUK_Friend) |
Richard Smith | 22050f2 | 2013-07-17 23:53:16 +0000 | [diff] [blame] | 10877 | New->setObjectOfFriendDecl(!FriendSawTagOutsideEnclosingNamespace && |
10878 | getLangOpts().MicrosoftExt); | ||||
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 10879 | |
Anders Carlsson | 0cf8830 | 2009-03-26 01:19:02 +0000 | [diff] [blame] | 10880 | // Set the access specifier. |
John McCall | 9c86b51 | 2010-03-25 21:28:06 +0000 | [diff] [blame] | 10881 | if (!Invalid && SearchDC->isRecord()) |
Douglas Gregor | d0c8737 | 2009-05-27 17:30:49 +0000 | [diff] [blame] | 10882 | SetMemberAccessSpecifier(New, PrevDecl, AS); |
Douglas Gregor | 06c0fec | 2009-03-25 22:00:53 +0000 | [diff] [blame] | 10883 | |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 10884 | if (TUK == TUK_Definition) |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 10885 | New->startDefinition(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10886 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 10887 | // If this has an identifier, add it to the scope stack. |
John McCall | d7eff68 | 2009-09-02 00:55:30 +0000 | [diff] [blame] | 10888 | if (TUK == TUK_Friend) { |
John McCall | 82b9fb8 | 2009-09-02 19:32:14 +0000 | [diff] [blame] | 10889 | // We might be replacing an existing declaration in the lookup tables; |
10890 | // if so, borrow its access specifier. | ||||
10891 | if (PrevDecl) | ||||
10892 | New->setAccess(PrevDecl->getAccess()); | ||||
10893 | |||||
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 10894 | DeclContext *DC = New->getDeclContext()->getRedeclContext(); |
Richard Smith | 1b7f9cb | 2012-03-13 03:12:56 +0000 | [diff] [blame] | 10895 | DC->makeDeclVisibleInContext(New); |
John McCall | 9c86b51 | 2010-03-25 21:28:06 +0000 | [diff] [blame] | 10896 | if (Name) // can be null along some error paths |
John McCall | d7eff68 | 2009-09-02 00:55:30 +0000 | [diff] [blame] | 10897 | if (Scope *EnclosingScope = getScopeForDeclContext(S, DC)) |
10898 | PushOnScopeChains(New, EnclosingScope, /* AddToContext = */ false); | ||||
John McCall | d7eff68 | 2009-09-02 00:55:30 +0000 | [diff] [blame] | 10899 | } else if (Name) { |
Douglas Gregor | 1a0d31a | 2009-01-12 18:45:55 +0000 | [diff] [blame] | 10900 | S = getNonFieldDeclScope(S); |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 10901 | PushOnScopeChains(New, S, !IsForwardReference); |
10902 | if (IsForwardReference) | ||||
Richard Smith | 1b7f9cb | 2012-03-13 03:12:56 +0000 | [diff] [blame] | 10903 | SearchDC->makeDeclVisibleInContext(New); |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 10904 | |
Douglas Gregor | 4920f1f | 2009-01-12 22:49:06 +0000 | [diff] [blame] | 10905 | } else { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 10906 | CurContext->addDecl(New); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 10907 | } |
Argyrios Kyrtzidis | 5239304 | 2008-11-09 23:41:00 +0000 | [diff] [blame] | 10908 | |
Douglas Gregor | c29f77b | 2009-07-07 16:35:42 +0000 | [diff] [blame] | 10909 | // If this is the C FILE type, notify the AST context. |
10910 | if (IdentifierInfo *II = New->getIdentifier()) | ||||
10911 | if (!New->isInvalidDecl() && | ||||
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 10912 | New->getDeclContext()->getRedeclContext()->isTranslationUnit() && |
Douglas Gregor | c29f77b | 2009-07-07 16:35:42 +0000 | [diff] [blame] | 10913 | II->isStr("FILE")) |
10914 | Context.setFILEDecl(New); | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10915 | |
James Molloy | 16f1f71 | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 10916 | // If we were in function prototype scope (and not in C++ mode), add this |
10917 | // tag to the list of decls to inject into the function definition scope. | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10918 | if (S->isFunctionPrototypeScope() && !getLangOpts().CPlusPlus && |
James Molloy | 16f1f71 | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 10919 | InFunctionDeclarator && Name) |
10920 | DeclsInPrototypeScope.push_back(New); | ||||
10921 | |||||
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 10922 | if (PrevDecl) |
10923 | mergeDeclAttributes(New, PrevDecl); | ||||
10924 | |||||
Rafael Espindola | 71adc5b | 2012-07-17 15:14:47 +0000 | [diff] [blame] | 10925 | // If there's a #pragma GCC visibility in scope, set the visibility of this |
10926 | // record. | ||||
10927 | AddPushedVisibilityAttribute(New); | ||||
10928 | |||||
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 10929 | OwnedDecl = true; |
Richard Smith | 37ec8d5 | 2012-12-05 11:34:06 +0000 | [diff] [blame] | 10930 | // In C++, don't return an invalid declaration. We can't recover well from |
10931 | // the cases where we make the type anonymous. | ||||
10932 | return (Invalid && getLangOpts().CPlusPlus) ? 0 : New; | ||||
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 10933 | } |
10934 | |||||
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10935 | void Sema::ActOnTagStartDefinition(Scope *S, Decl *TagD) { |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 10936 | AdjustDeclIfTemplate(TagD); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10937 | TagDecl *Tag = cast<TagDecl>(TagD); |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 10938 | |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 10939 | // Enter the tag context. |
10940 | PushDeclContext(S, Tag); | ||||
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 10941 | |
10942 | ActOnDocumentableDecl(TagD); | ||||
Rafael Espindola | 5e06529 | 2012-07-12 04:47:34 +0000 | [diff] [blame] | 10943 | |
10944 | // If there's a #pragma GCC visibility in scope, set the visibility of this | ||||
10945 | // record. | ||||
10946 | AddPushedVisibilityAttribute(Tag); | ||||
John McCall | f936815 | 2009-12-20 07:58:13 +0000 | [diff] [blame] | 10947 | } |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 10948 | |
Argyrios Kyrtzidis | 3a38744 | 2011-10-06 23:23:20 +0000 | [diff] [blame] | 10949 | Decl *Sema::ActOnObjCContainerStartDefinition(Decl *IDecl) { |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 10950 | assert(isa<ObjCContainerDecl>(IDecl) && |
10951 | "ActOnObjCContainerStartDefinition - Not ObjCContainerDecl"); | ||||
10952 | DeclContext *OCD = cast<DeclContext>(IDecl); | ||||
10953 | assert(getContainingDC(OCD) == CurContext && | ||||
10954 | "The next DeclContext should be lexically contained in the current one."); | ||||
10955 | CurContext = OCD; | ||||
Argyrios Kyrtzidis | 3a38744 | 2011-10-06 23:23:20 +0000 | [diff] [blame] | 10956 | return IDecl; |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 10957 | } |
10958 | |||||
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10959 | void Sema::ActOnStartCXXMemberDeclarations(Scope *S, Decl *TagD, |
Anders Carlsson | 2c3ee54 | 2011-03-25 14:31:08 +0000 | [diff] [blame] | 10960 | SourceLocation FinalLoc, |
John McCall | f936815 | 2009-12-20 07:58:13 +0000 | [diff] [blame] | 10961 | SourceLocation LBraceLoc) { |
10962 | AdjustDeclIfTemplate(TagD); | ||||
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10963 | CXXRecordDecl *Record = cast<CXXRecordDecl>(TagD); |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 10964 | |
John McCall | f936815 | 2009-12-20 07:58:13 +0000 | [diff] [blame] | 10965 | FieldCollector->StartClass(); |
10966 | |||||
10967 | if (!Record->getIdentifier()) | ||||
10968 | return; | ||||
10969 | |||||
Anders Carlsson | 2c3ee54 | 2011-03-25 14:31:08 +0000 | [diff] [blame] | 10970 | if (FinalLoc.isValid()) |
10971 | Record->addAttr(new (Context) FinalAttr(FinalLoc, Context)); | ||||
Anders Carlsson | dfc2f10 | 2011-01-22 17:51:53 +0000 | [diff] [blame] | 10972 | |
John McCall | f936815 | 2009-12-20 07:58:13 +0000 | [diff] [blame] | 10973 | // C++ [class]p2: |
10974 | // [...] The class-name is also inserted into the scope of the | ||||
10975 | // class itself; this is known as the injected-class-name. For | ||||
10976 | // purposes of access checking, the injected-class-name is treated | ||||
10977 | // as if it were a public member name. | ||||
10978 | CXXRecordDecl *InjectedClassName | ||||
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 10979 | = CXXRecordDecl::Create(Context, Record->getTagKind(), CurContext, |
10980 | Record->getLocStart(), Record->getLocation(), | ||||
John McCall | f936815 | 2009-12-20 07:58:13 +0000 | [diff] [blame] | 10981 | Record->getIdentifier(), |
Argyrios Kyrtzidis | 3b8f610 | 2010-10-14 20:14:21 +0000 | [diff] [blame] | 10982 | /*PrevDecl=*/0, |
10983 | /*DelayTypeCreation=*/true); | ||||
10984 | Context.getTypeDeclType(InjectedClassName, Record); | ||||
John McCall | f936815 | 2009-12-20 07:58:13 +0000 | [diff] [blame] | 10985 | InjectedClassName->setImplicit(); |
10986 | InjectedClassName->setAccess(AS_public); | ||||
10987 | if (ClassTemplateDecl *Template = Record->getDescribedClassTemplate()) | ||||
10988 | InjectedClassName->setDescribedClassTemplate(Template); | ||||
10989 | PushOnScopeChains(InjectedClassName, S); | ||||
10990 | assert(InjectedClassName->isInjectedClassName() && | ||||
10991 | "Broken injected-class-name"); | ||||
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 10992 | } |
10993 | |||||
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10994 | void Sema::ActOnTagFinishDefinition(Scope *S, Decl *TagD, |
Argyrios Kyrtzidis | 07a5b28 | 2009-07-14 03:17:52 +0000 | [diff] [blame] | 10995 | SourceLocation RBraceLoc) { |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 10996 | AdjustDeclIfTemplate(TagD); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10997 | TagDecl *Tag = cast<TagDecl>(TagD); |
Argyrios Kyrtzidis | 07a5b28 | 2009-07-14 03:17:52 +0000 | [diff] [blame] | 10998 | Tag->setRBraceLoc(RBraceLoc); |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 10999 | |
Argyrios Kyrtzidis | 216f78b | 2012-03-09 20:10:30 +0000 | [diff] [blame] | 11000 | // Make sure we "complete" the definition even it is invalid. |
11001 | if (Tag->isBeingDefined()) { | ||||
11002 | assert(Tag->isInvalidDecl() && "We should already have completed it"); | ||||
11003 | if (RecordDecl *RD = dyn_cast<RecordDecl>(Tag)) | ||||
11004 | RD->completeDefinition(); | ||||
11005 | } | ||||
11006 | |||||
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 11007 | if (isa<CXXRecordDecl>(Tag)) |
11008 | FieldCollector->FinishClass(); | ||||
11009 | |||||
11010 | // Exit this scope of this tag's definition. | ||||
11011 | PopDeclContext(); | ||||
Argyrios Kyrtzidis | 3d207e7 | 2013-01-29 18:00:54 +0000 | [diff] [blame] | 11012 | |
11013 | if (getCurLexicalContext()->isObjCContainer() && | ||||
11014 | Tag->getDeclContext()->isFileContext()) | ||||
11015 | Tag->setTopLevelDeclInObjCContainer(); | ||||
11016 | |||||
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 11017 | // Notify the consumer that we've defined a tag. |
Serge Pavlov | 439b701 | 2013-07-02 17:31:56 +0000 | [diff] [blame] | 11018 | if (!Tag->isInvalidDecl()) |
11019 | Consumer.HandleTagDeclDefinition(Tag); | ||||
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 11020 | } |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 11021 | |
Fariborz Jahanian | 10af879 | 2011-08-29 17:33:12 +0000 | [diff] [blame] | 11022 | void Sema::ActOnObjCContainerFinishDefinition() { |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 11023 | // Exit this scope of this interface definition. |
11024 | PopDeclContext(); | ||||
11025 | } | ||||
Argyrios Kyrtzidis | 3a38744 | 2011-10-06 23:23:20 +0000 | [diff] [blame] | 11026 | |
Argyrios Kyrtzidis | 458bacf | 2011-10-27 00:09:34 +0000 | [diff] [blame] | 11027 | void Sema::ActOnObjCTemporaryExitContainerContext(DeclContext *DC) { |
Argyrios Kyrtzidis | 4a7dc8a | 2011-10-27 00:53:06 +0000 | [diff] [blame] | 11028 | assert(DC == CurContext && "Mismatch of container contexts"); |
11029 | OriginalLexicalContext = DC; | ||||
Argyrios Kyrtzidis | 3a38744 | 2011-10-06 23:23:20 +0000 | [diff] [blame] | 11030 | ActOnObjCContainerFinishDefinition(); |
11031 | } | ||||
11032 | |||||
Argyrios Kyrtzidis | 458bacf | 2011-10-27 00:09:34 +0000 | [diff] [blame] | 11033 | void Sema::ActOnObjCReenterContainerContext(DeclContext *DC) { |
11034 | ActOnObjCContainerStartDefinition(cast<Decl>(DC)); | ||||
Argyrios Kyrtzidis | 3a38744 | 2011-10-06 23:23:20 +0000 | [diff] [blame] | 11035 | OriginalLexicalContext = 0; |
11036 | } | ||||
11037 | |||||
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11038 | void Sema::ActOnTagDefinitionError(Scope *S, Decl *TagD) { |
John McCall | db7bb4a | 2010-03-17 00:38:33 +0000 | [diff] [blame] | 11039 | AdjustDeclIfTemplate(TagD); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11040 | TagDecl *Tag = cast<TagDecl>(TagD); |
John McCall | db7bb4a | 2010-03-17 00:38:33 +0000 | [diff] [blame] | 11041 | Tag->setInvalidDecl(); |
11042 | |||||
Argyrios Kyrtzidis | 216f78b | 2012-03-09 20:10:30 +0000 | [diff] [blame] | 11043 | // Make sure we "complete" the definition even it is invalid. |
11044 | if (Tag->isBeingDefined()) { | ||||
11045 | if (RecordDecl *RD = dyn_cast<RecordDecl>(Tag)) | ||||
11046 | RD->completeDefinition(); | ||||
11047 | } | ||||
11048 | |||||
John McCall | a8cab01 | 2010-03-17 19:25:57 +0000 | [diff] [blame] | 11049 | // We're undoing ActOnTagStartDefinition here, not |
11050 | // ActOnStartCXXMemberDeclarations, so we don't have to mess with | ||||
11051 | // the FieldCollector. | ||||
John McCall | db7bb4a | 2010-03-17 00:38:33 +0000 | [diff] [blame] | 11052 | |
11053 | PopDeclContext(); | ||||
11054 | } | ||||
11055 | |||||
Chris Lattner | df9bcd5 | 2009-04-20 17:29:38 +0000 | [diff] [blame] | 11056 | // Note that FieldName may be null for anonymous bitfields. |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 11057 | ExprResult Sema::VerifyBitField(SourceLocation FieldLoc, |
11058 | IdentifierInfo *FieldName, | ||||
Reid Kleckner | 9a3ecb0 | 2013-07-17 20:46:03 +0000 | [diff] [blame] | 11059 | QualType FieldTy, bool IsMsStruct, |
11060 | Expr *BitWidth, bool *ZeroWidth) { | ||||
Eli Friedman | 1d954f6 | 2009-08-15 21:55:26 +0000 | [diff] [blame] | 11061 | // Default to true; that shouldn't confuse checks for emptiness |
11062 | if (ZeroWidth) | ||||
11063 | *ZeroWidth = true; | ||||
11064 | |||||
Chris Lattner | 2479366 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 11065 | // C99 6.7.2.1p4 - verify the field type. |
Chris Lattner | 8b963ef | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 11066 | // C++ 9.6p3: A bit-field shall have integral or enumeration type. |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 11067 | if (!FieldTy->isDependentType() && !FieldTy->isIntegralOrEnumerationType()) { |
Chris Lattner | 2479366 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 11068 | // Handle incomplete types with specific error. |
Douglas Gregor | a03aca8 | 2009-03-10 21:58:27 +0000 | [diff] [blame] | 11069 | if (RequireCompleteType(FieldLoc, FieldTy, diag::err_field_incomplete)) |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 11070 | return ExprError(); |
Chris Lattner | df9bcd5 | 2009-04-20 17:29:38 +0000 | [diff] [blame] | 11071 | if (FieldName) |
11072 | return Diag(FieldLoc, diag::err_not_integral_type_bitfield) | ||||
11073 | << FieldName << FieldTy << BitWidth->getSourceRange(); | ||||
11074 | return Diag(FieldLoc, diag::err_not_integral_type_anon_bitfield) | ||||
11075 | << FieldTy << BitWidth->getSourceRange(); | ||||
Douglas Gregor | e186269 | 2010-12-15 23:18:36 +0000 | [diff] [blame] | 11076 | } else if (DiagnoseUnexpandedParameterPack(const_cast<Expr *>(BitWidth), |
11077 | UPPC_BitFieldWidth)) | ||||
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 11078 | return ExprError(); |
Douglas Gregor | 3cf538d | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 11079 | |
11080 | // If the bit-width is type- or value-dependent, don't try to check | ||||
11081 | // it now. | ||||
11082 | if (BitWidth->isValueDependent() || BitWidth->isTypeDependent()) | ||||
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 11083 | return Owned(BitWidth); |
Douglas Gregor | 3cf538d | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 11084 | |
Anders Carlsson | 9f1e572 | 2008-12-06 20:33:04 +0000 | [diff] [blame] | 11085 | llvm::APSInt Value; |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 11086 | ExprResult ICE = VerifyIntegerConstantExpression(BitWidth, &Value); |
11087 | if (ICE.isInvalid()) | ||||
11088 | return ICE; | ||||
11089 | BitWidth = ICE.take(); | ||||
Anders Carlsson | 9f1e572 | 2008-12-06 20:33:04 +0000 | [diff] [blame] | 11090 | |
Eli Friedman | 1d954f6 | 2009-08-15 21:55:26 +0000 | [diff] [blame] | 11091 | if (Value != 0 && ZeroWidth) |
11092 | *ZeroWidth = false; | ||||
11093 | |||||
Chris Lattner | cd08707 | 2008-12-12 04:56:04 +0000 | [diff] [blame] | 11094 | // Zero-width bitfield is ok for anonymous field. |
11095 | if (Value == 0 && FieldName) | ||||
11096 | return Diag(FieldLoc, diag::err_bitfield_has_zero_width) << FieldName; | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11097 | |
Chris Lattner | df9bcd5 | 2009-04-20 17:29:38 +0000 | [diff] [blame] | 11098 | if (Value.isSigned() && Value.isNegative()) { |
11099 | if (FieldName) | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11100 | return Diag(FieldLoc, diag::err_bitfield_has_negative_width) |
Chris Lattner | df9bcd5 | 2009-04-20 17:29:38 +0000 | [diff] [blame] | 11101 | << FieldName << Value.toString(10); |
11102 | return Diag(FieldLoc, diag::err_anon_bitfield_has_negative_width) | ||||
11103 | << Value.toString(10); | ||||
11104 | } | ||||
Anders Carlsson | 9f1e572 | 2008-12-06 20:33:04 +0000 | [diff] [blame] | 11105 | |
Douglas Gregor | 3cf538d | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 11106 | if (!FieldTy->isDependentType()) { |
11107 | uint64_t TypeSize = Context.getTypeSize(FieldTy); | ||||
Chris Lattner | df9bcd5 | 2009-04-20 17:29:38 +0000 | [diff] [blame] | 11108 | if (Value.getZExtValue() > TypeSize) { |
Reid Kleckner | 9a3ecb0 | 2013-07-17 20:46:03 +0000 | [diff] [blame] | 11109 | if (!getLangOpts().CPlusPlus || IsMsStruct) { |
Anders Carlsson | 72468ec | 2010-04-16 15:16:32 +0000 | [diff] [blame] | 11110 | if (FieldName) |
11111 | return Diag(FieldLoc, diag::err_bitfield_width_exceeds_type_size) | ||||
11112 | << FieldName << (unsigned)Value.getZExtValue() | ||||
11113 | << (unsigned)TypeSize; | ||||
11114 | |||||
11115 | return Diag(FieldLoc, diag::err_anon_bitfield_width_exceeds_type_size) | ||||
11116 | << (unsigned)Value.getZExtValue() << (unsigned)TypeSize; | ||||
11117 | } | ||||
11118 | |||||
Chris Lattner | df9bcd5 | 2009-04-20 17:29:38 +0000 | [diff] [blame] | 11119 | if (FieldName) |
Anders Carlsson | 72468ec | 2010-04-16 15:16:32 +0000 | [diff] [blame] | 11120 | Diag(FieldLoc, diag::warn_bitfield_width_exceeds_type_size) |
11121 | << FieldName << (unsigned)Value.getZExtValue() | ||||
11122 | << (unsigned)TypeSize; | ||||
11123 | else | ||||
11124 | Diag(FieldLoc, diag::warn_anon_bitfield_width_exceeds_type_size) | ||||
11125 | << (unsigned)Value.getZExtValue() << (unsigned)TypeSize; | ||||
Chris Lattner | df9bcd5 | 2009-04-20 17:29:38 +0000 | [diff] [blame] | 11126 | } |
Douglas Gregor | 3cf538d | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 11127 | } |
Anders Carlsson | 9f1e572 | 2008-12-06 20:33:04 +0000 | [diff] [blame] | 11128 | |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 11129 | return Owned(BitWidth); |
Anders Carlsson | 9f1e572 | 2008-12-06 20:33:04 +0000 | [diff] [blame] | 11130 | } |
11131 | |||||
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 11132 | /// 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] | 11133 | /// to create a FieldDecl object for it. |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 11134 | Decl *Sema::ActOnField(Scope *S, Decl *TagD, SourceLocation DeclStart, |
Richard Trieu | f81e5a9 | 2011-09-09 02:00:50 +0000 | [diff] [blame] | 11135 | Declarator &D, Expr *BitfieldWidth) { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11136 | FieldDecl *Res = HandleField(S, cast_or_null<RecordDecl>(TagD), |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 11137 | DeclStart, D, static_cast<Expr*>(BitfieldWidth), |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 11138 | /*InitStyle=*/ICIS_NoInit, AS_public); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11139 | return Res; |
Chris Lattner | 2479366 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 11140 | } |
11141 | |||||
11142 | /// HandleField - Analyze a field of a C struct or a C++ data member. | ||||
11143 | /// | ||||
11144 | FieldDecl *Sema::HandleField(Scope *S, RecordDecl *Record, | ||||
11145 | SourceLocation DeclStart, | ||||
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 11146 | Declarator &D, Expr *BitWidth, |
11147 | InClassInitStyle InitStyle, | ||||
Douglas Gregor | 4dd55f5 | 2009-03-11 20:50:30 +0000 | [diff] [blame] | 11148 | AccessSpecifier AS) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 11149 | IdentifierInfo *II = D.getIdentifier(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 11150 | SourceLocation Loc = DeclStart; |
11151 | if (II) Loc = D.getIdentifierLoc(); | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11152 | |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 11153 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
11154 | QualType T = TInfo->getType(); | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 11155 | if (getLangOpts().CPlusPlus) { |
Douglas Gregor | 3cf538d | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 11156 | CheckExtraCXXDefaultArguments(D); |
Douglas Gregor | 021c3b3 | 2009-03-11 23:00:04 +0000 | [diff] [blame] | 11157 | |
Douglas Gregor | e186269 | 2010-12-15 23:18:36 +0000 | [diff] [blame] | 11158 | if (DiagnoseUnexpandedParameterPack(D.getIdentifierLoc(), TInfo, |
11159 | UPPC_DataMemberType)) { | ||||
11160 | D.setInvalidType(); | ||||
11161 | T = Context.IntTy; | ||||
11162 | TInfo = Context.getTrivialTypeSourceInfo(T, Loc); | ||||
11163 | } | ||||
11164 | } | ||||
11165 | |||||
Matt Arsenault | 34b0adb | 2013-02-26 21:16:00 +0000 | [diff] [blame] | 11166 | // TR 18037 does not allow fields to be declared with address spaces. |
11167 | if (T.getQualifiers().hasAddressSpace()) { | ||||
11168 | Diag(Loc, diag::err_field_with_address_space); | ||||
11169 | D.setInvalidType(); | ||||
11170 | } | ||||
11171 | |||||
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 11172 | // OpenCL 1.2 spec, s6.9 r: |
11173 | // The event type cannot be used to declare a structure or union field. | ||||
11174 | if (LangOpts.OpenCL && T->isEventT()) { | ||||
11175 | Diag(Loc, diag::err_event_t_struct_field); | ||||
11176 | D.setInvalidType(); | ||||
11177 | } | ||||
11178 | |||||
Richard Smith | c7f8116 | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 11179 | DiagnoseFunctionSpecifiers(D.getDeclSpec()); |
Eli Friedman | 85a5319 | 2009-04-07 19:37:57 +0000 | [diff] [blame] | 11180 | |
Richard Smith | ec64244 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 11181 | if (DeclSpec::TSCS TSCS = D.getDeclSpec().getThreadStorageClassSpec()) |
11182 | Diag(D.getDeclSpec().getThreadStorageClassSpecLoc(), | ||||
11183 | diag::err_invalid_thread) | ||||
11184 | << DeclSpec::getSpecifierName(TSCS); | ||||
Matt Arsenault | 34b0adb | 2013-02-26 21:16:00 +0000 | [diff] [blame] | 11185 | |
Douglas Gregor | 7f6ff02 | 2010-08-30 14:32:14 +0000 | [diff] [blame] | 11186 | // Check to see if this name was declared as a member previously |
Douglas Gregor | 95e5510 | 2011-10-21 15:47:52 +0000 | [diff] [blame] | 11187 | NamedDecl *PrevDecl = 0; |
Douglas Gregor | 7f6ff02 | 2010-08-30 14:32:14 +0000 | [diff] [blame] | 11188 | LookupResult Previous(*this, II, Loc, LookupMemberName, ForRedeclaration); |
11189 | LookupName(Previous, S); | ||||
Douglas Gregor | 95e5510 | 2011-10-21 15:47:52 +0000 | [diff] [blame] | 11190 | switch (Previous.getResultKind()) { |
11191 | case LookupResult::Found: | ||||
11192 | case LookupResult::FoundUnresolvedValue: | ||||
11193 | PrevDecl = Previous.getAsSingle<NamedDecl>(); | ||||
11194 | break; | ||||
11195 | |||||
11196 | case LookupResult::FoundOverloaded: | ||||
11197 | PrevDecl = Previous.getRepresentativeDecl(); | ||||
11198 | break; | ||||
11199 | |||||
11200 | case LookupResult::NotFound: | ||||
11201 | case LookupResult::NotFoundInCurrentInstantiation: | ||||
11202 | case LookupResult::Ambiguous: | ||||
11203 | break; | ||||
11204 | } | ||||
11205 | Previous.suppressDiagnostics(); | ||||
Douglas Gregor | c19ee3e | 2009-06-17 23:37:01 +0000 | [diff] [blame] | 11206 | |
11207 | if (PrevDecl && PrevDecl->isTemplateParameter()) { | ||||
11208 | // Maybe we will complain about the shadowed template parameter. | ||||
11209 | DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), PrevDecl); | ||||
11210 | // Just pretend that we didn't see the previous declaration. | ||||
11211 | PrevDecl = 0; | ||||
11212 | } | ||||
11213 | |||||
Douglas Gregor | 3cf538d | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 11214 | if (PrevDecl && !isDeclInScope(PrevDecl, Record, S)) |
11215 | PrevDecl = 0; | ||||
11216 | |||||
Steve Naroff | ea218b8 | 2009-07-14 14:58:18 +0000 | [diff] [blame] | 11217 | bool Mutable |
11218 | = (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_mutable); | ||||
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 11219 | SourceLocation TSSL = D.getLocStart(); |
Steve Naroff | ea218b8 | 2009-07-14 14:58:18 +0000 | [diff] [blame] | 11220 | FieldDecl *NewFD |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 11221 | = CheckFieldDecl(II, T, TInfo, Record, Loc, Mutable, BitWidth, InitStyle, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 11222 | TSSL, AS, PrevDecl, &D); |
Rafael Espindola | 0162070 | 2010-03-21 22:56:43 +0000 | [diff] [blame] | 11223 | |
11224 | if (NewFD->isInvalidDecl()) | ||||
11225 | Record->setInvalidDecl(); | ||||
11226 | |||||
Douglas Gregor | 591dc84 | 2011-09-12 16:11:24 +0000 | [diff] [blame] | 11227 | if (D.getDeclSpec().isModulePrivateSpecified()) |
11228 | NewFD->setModulePrivate(); | ||||
11229 | |||||
Douglas Gregor | 3cf538d | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 11230 | if (NewFD->isInvalidDecl() && PrevDecl) { |
11231 | // Don't introduce NewFD into scope; there's already something | ||||
11232 | // with the same name in the same scope. | ||||
11233 | } else if (II) { | ||||
11234 | PushOnScopeChains(NewFD, S); | ||||
11235 | } else | ||||
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 11236 | Record->addDecl(NewFD); |
Douglas Gregor | 3cf538d | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 11237 | |
11238 | return NewFD; | ||||
11239 | } | ||||
11240 | |||||
11241 | /// \brief Build a new FieldDecl and check its well-formedness. | ||||
11242 | /// | ||||
11243 | /// This routine builds a new FieldDecl given the fields name, type, | ||||
11244 | /// record, etc. \p PrevDecl should refer to any previous declaration | ||||
11245 | /// with the same name and in the same scope as the field to be | ||||
11246 | /// created. | ||||
11247 | /// | ||||
11248 | /// \returns a new FieldDecl. | ||||
11249 | /// | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11250 | /// \todo The Declarator argument is a hack. It will be removed once |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 11251 | FieldDecl *Sema::CheckFieldDecl(DeclarationName Name, QualType T, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 11252 | TypeSourceInfo *TInfo, |
Douglas Gregor | 3cf538d | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 11253 | RecordDecl *Record, SourceLocation Loc, |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 11254 | bool Mutable, Expr *BitWidth, |
11255 | InClassInitStyle InitStyle, | ||||
Steve Naroff | ea218b8 | 2009-07-14 14:58:18 +0000 | [diff] [blame] | 11256 | SourceLocation TSSL, |
Douglas Gregor | 4dd55f5 | 2009-03-11 20:50:30 +0000 | [diff] [blame] | 11257 | AccessSpecifier AS, NamedDecl *PrevDecl, |
Douglas Gregor | 3cf538d | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 11258 | Declarator *D) { |
11259 | IdentifierInfo *II = Name.getAsIdentifierInfo(); | ||||
Steve Naroff | 5912a35 | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 11260 | bool InvalidDecl = false; |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 11261 | if (D) InvalidDecl = D->isInvalidType(); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 11262 | |
Douglas Gregor | 3cf538d | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 11263 | // If we receive a broken type, recover by assuming 'int' and |
11264 | // marking this declaration as invalid. | ||||
11265 | if (T.isNull()) { | ||||
11266 | InvalidDecl = true; | ||||
11267 | T = Context.IntTy; | ||||
11268 | } | ||||
11269 | |||||
Eli Friedman | 721e77d | 2009-12-07 00:22:08 +0000 | [diff] [blame] | 11270 | QualType EltTy = Context.getBaseElementType(T); |
Argyrios Kyrtzidis | 216f78b | 2012-03-09 20:10:30 +0000 | [diff] [blame] | 11271 | if (!EltTy->isDependentType()) { |
11272 | if (RequireCompleteType(Loc, EltTy, diag::err_field_incomplete)) { | ||||
11273 | // Fields of incomplete type force their record to be invalid. | ||||
11274 | Record->setInvalidDecl(); | ||||
11275 | InvalidDecl = true; | ||||
11276 | } else { | ||||
11277 | NamedDecl *Def; | ||||
11278 | EltTy->isIncompleteType(&Def); | ||||
11279 | if (Def && Def->isInvalidDecl()) { | ||||
11280 | Record->setInvalidDecl(); | ||||
11281 | InvalidDecl = true; | ||||
11282 | } | ||||
11283 | } | ||||
John McCall | 2d7d2d9 | 2010-08-16 23:42:35 +0000 | [diff] [blame] | 11284 | } |
Eli Friedman | 721e77d | 2009-12-07 00:22:08 +0000 | [diff] [blame] | 11285 | |
Joey Gouly | 617bb31 | 2013-01-17 17:35:00 +0000 | [diff] [blame] | 11286 | // OpenCL v1.2 s6.9.c: bitfields are not supported. |
11287 | if (BitWidth && getLangOpts().OpenCL) { | ||||
11288 | Diag(Loc, diag::err_opencl_bitfields); | ||||
11289 | InvalidDecl = true; | ||||
11290 | } | ||||
11291 | |||||
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 11292 | // C99 6.7.2.1p8: A member of a structure or union may have any type other |
11293 | // than a variably modified type. | ||||
Eli Friedman | 721e77d | 2009-12-07 00:22:08 +0000 | [diff] [blame] | 11294 | if (!InvalidDecl && T->isVariablyModifiedType()) { |
Eli Friedman | 1ca4813 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 11295 | bool SizeIsNegative; |
Douglas Gregor | 2767ce2 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 11296 | llvm::APSInt Oversized; |
Abramo Bagnara | 4c5750e | 2012-11-08 14:44:42 +0000 | [diff] [blame] | 11297 | |
11298 | TypeSourceInfo *FixedTInfo = | ||||
11299 | TryToFixInvalidVariablyModifiedTypeSourceInfo(TInfo, Context, | ||||
11300 | SizeIsNegative, | ||||
11301 | Oversized); | ||||
11302 | if (FixedTInfo) { | ||||
Eli Friedman | 1ca4813 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 11303 | Diag(Loc, diag::warn_illegal_constant_array_size); |
Abramo Bagnara | 4c5750e | 2012-11-08 14:44:42 +0000 | [diff] [blame] | 11304 | TInfo = FixedTInfo; |
11305 | T = FixedTInfo->getType(); | ||||
Eli Friedman | 1ca4813 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 11306 | } else { |
11307 | if (SizeIsNegative) | ||||
11308 | Diag(Loc, diag::err_typecheck_negative_array_size); | ||||
Douglas Gregor | 2767ce2 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 11309 | else if (Oversized.getBoolValue()) |
11310 | Diag(Loc, diag::err_array_too_large) | ||||
11311 | << Oversized.toString(10); | ||||
Eli Friedman | 1ca4813 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 11312 | else |
11313 | Diag(Loc, diag::err_typecheck_field_variable_size); | ||||
Eli Friedman | 1ca4813 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 11314 | InvalidDecl = true; |
11315 | } | ||||
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 11316 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11317 | |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 11318 | // Fields can not have abstract class types |
Eli Friedman | 721e77d | 2009-12-07 00:22:08 +0000 | [diff] [blame] | 11319 | if (!InvalidDecl && RequireNonAbstractType(Loc, T, |
11320 | diag::err_abstract_type_in_decl, | ||||
11321 | AbstractFieldType)) | ||||
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 11322 | InvalidDecl = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11323 | |
Eli Friedman | 1d954f6 | 2009-08-15 21:55:26 +0000 | [diff] [blame] | 11324 | bool ZeroWidth = false; |
Douglas Gregor | 3cf538d | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 11325 | // If this is declared as a bit-field, check the bit-field. |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 11326 | if (!InvalidDecl && BitWidth) { |
Reid Kleckner | 9a3ecb0 | 2013-07-17 20:46:03 +0000 | [diff] [blame] | 11327 | BitWidth = VerifyBitField(Loc, II, T, Record->isMsStruct(Context), BitWidth, |
11328 | &ZeroWidth).take(); | ||||
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 11329 | if (!BitWidth) { |
11330 | InvalidDecl = true; | ||||
11331 | BitWidth = 0; | ||||
11332 | ZeroWidth = false; | ||||
11333 | } | ||||
Anders Carlsson | 9f1e572 | 2008-12-06 20:33:04 +0000 | [diff] [blame] | 11334 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11335 | |
John McCall | 4bde1e1 | 2010-06-04 08:34:12 +0000 | [diff] [blame] | 11336 | // Check that 'mutable' is consistent with the type of the declaration. |
11337 | if (!InvalidDecl && Mutable) { | ||||
11338 | unsigned DiagID = 0; | ||||
11339 | if (T->isReferenceType()) | ||||
11340 | DiagID = diag::err_mutable_reference; | ||||
11341 | else if (T.isConstQualified()) | ||||
11342 | DiagID = diag::err_mutable_const; | ||||
11343 | |||||
11344 | if (DiagID) { | ||||
11345 | SourceLocation ErrLoc = Loc; | ||||
11346 | if (D && D->getDeclSpec().getStorageClassSpecLoc().isValid()) | ||||
11347 | ErrLoc = D->getDeclSpec().getStorageClassSpecLoc(); | ||||
11348 | Diag(ErrLoc, DiagID); | ||||
11349 | Mutable = false; | ||||
11350 | InvalidDecl = true; | ||||
11351 | } | ||||
11352 | } | ||||
11353 | |||||
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 11354 | FieldDecl *NewFD = FieldDecl::Create(Context, Record, TSSL, Loc, II, T, TInfo, |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 11355 | BitWidth, Mutable, InitStyle); |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 11356 | if (InvalidDecl) |
11357 | NewFD->setInvalidDecl(); | ||||
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 11358 | |
Douglas Gregor | 3cf538d | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 11359 | if (PrevDecl && !isa<TagDecl>(PrevDecl)) { |
11360 | Diag(Loc, diag::err_duplicate_member) << II; | ||||
11361 | Diag(PrevDecl->getLocation(), diag::note_previous_declaration); | ||||
11362 | NewFD->setInvalidDecl(); | ||||
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 11363 | } |
11364 | |||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 11365 | if (!InvalidDecl && getLangOpts().CPlusPlus) { |
Anders Carlsson | dfdfc58 | 2010-11-07 19:13:55 +0000 | [diff] [blame] | 11366 | if (Record->isUnion()) { |
11367 | if (const RecordType *RT = EltTy->getAs<RecordType>()) { | ||||
11368 | CXXRecordDecl* RDecl = cast<CXXRecordDecl>(RT->getDecl()); | ||||
11369 | if (RDecl->getDefinition()) { | ||||
11370 | // C++ [class.union]p1: An object of a class with a non-trivial | ||||
11371 | // constructor, a non-trivial copy constructor, a non-trivial | ||||
11372 | // destructor, or a non-trivial copy assignment operator | ||||
11373 | // cannot be a member of a union, nor can an array of such | ||||
11374 | // objects. | ||||
Richard Smith | e7d7c39 | 2011-10-19 20:41:51 +0000 | [diff] [blame] | 11375 | if (CheckNontrivialField(NewFD)) |
Anders Carlsson | dfdfc58 | 2010-11-07 19:13:55 +0000 | [diff] [blame] | 11376 | NewFD->setInvalidDecl(); |
11377 | } | ||||
11378 | } | ||||
11379 | |||||
11380 | // 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] | 11381 | // the program is ill-formed, except when compiling with MSVC extensions |
11382 | // enabled. | ||||
Anders Carlsson | dfdfc58 | 2010-11-07 19:13:55 +0000 | [diff] [blame] | 11383 | if (EltTy->isReferenceType()) { |
Aaron Ballman | 76eed42 | 2013-05-30 16:20:00 +0000 | [diff] [blame] | 11384 | Diag(NewFD->getLocation(), getLangOpts().MicrosoftExt ? |
11385 | diag::ext_union_member_of_reference_type : | ||||
11386 | diag::err_union_member_of_reference_type) | ||||
Anders Carlsson | dfdfc58 | 2010-11-07 19:13:55 +0000 | [diff] [blame] | 11387 | << NewFD->getDeclName() << EltTy; |
Aaron Ballman | 76eed42 | 2013-05-30 16:20:00 +0000 | [diff] [blame] | 11388 | if (!getLangOpts().MicrosoftExt) |
11389 | NewFD->setInvalidDecl(); | ||||
Douglas Gregor | 1f2023a | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 11390 | } |
11391 | } | ||||
11392 | } | ||||
11393 | |||||
Douglas Gregor | 3cf538d | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 11394 | // FIXME: We need to pass in the attributes given an AST |
11395 | // representation, not a parser representation. | ||||
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 11396 | if (D) { |
Douglas Gregor | 92eb7d8 | 2013-05-02 23:25:32 +0000 | [diff] [blame] | 11397 | // FIXME: The current scope is almost... but not entirely... correct here. |
11398 | ProcessDeclAttributes(getCurScope(), NewFD, *D); | ||||
Douglas Gregor | 3cf538d | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 11399 | |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 11400 | if (NewFD->hasAttrs()) |
11401 | CheckAlignasUnderalignment(NewFD); | ||||
11402 | } | ||||
11403 | |||||
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 11404 | // In auto-retain/release, infer strong retension for fields of |
11405 | // retainable type. | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 11406 | if (getLangOpts().ObjCAutoRefCount && inferObjCARCLifetime(NewFD)) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 11407 | NewFD->setInvalidDecl(); |
11408 | |||||
Fariborz Jahanian | f6123ca | 2009-02-19 00:22:47 +0000 | [diff] [blame] | 11409 | if (T.isObjCGCWeak()) |
Fariborz Jahanian | ed7e9ef | 2009-02-18 18:14:41 +0000 | [diff] [blame] | 11410 | Diag(Loc, diag::warn_attribute_weak_on_field); |
Anders Carlsson | ad14806 | 2008-02-16 00:29:18 +0000 | [diff] [blame] | 11411 | |
Douglas Gregor | 4dd55f5 | 2009-03-11 20:50:30 +0000 | [diff] [blame] | 11412 | NewFD->setAccess(AS); |
Steve Naroff | 5912a35 | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 11413 | return NewFD; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 11414 | } |
11415 | |||||
Argyrios Kyrtzidis | dd7744d | 2010-08-16 17:27:08 +0000 | [diff] [blame] | 11416 | bool Sema::CheckNontrivialField(FieldDecl *FD) { |
11417 | assert(FD); | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 11418 | assert(getLangOpts().CPlusPlus && "valid check only for C++"); |
Argyrios Kyrtzidis | dd7744d | 2010-08-16 17:27:08 +0000 | [diff] [blame] | 11419 | |
Nick Lewycky | dccd04d | 2013-06-25 23:22:23 +0000 | [diff] [blame] | 11420 | if (FD->isInvalidDecl() || FD->getType()->isDependentType()) |
11421 | return false; | ||||
Argyrios Kyrtzidis | dd7744d | 2010-08-16 17:27:08 +0000 | [diff] [blame] | 11422 | |
11423 | QualType EltTy = Context.getBaseElementType(FD->getType()); | ||||
11424 | if (const RecordType *RT = EltTy->getAs<RecordType>()) { | ||||
Richard Smith | ac71351 | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 11425 | CXXRecordDecl *RDecl = cast<CXXRecordDecl>(RT->getDecl()); |
Argyrios Kyrtzidis | dd7744d | 2010-08-16 17:27:08 +0000 | [diff] [blame] | 11426 | if (RDecl->getDefinition()) { |
11427 | // We check for copy constructors before constructors | ||||
11428 | // because otherwise we'll never get complaints about | ||||
11429 | // copy constructors. | ||||
11430 | |||||
11431 | CXXSpecialMember member = CXXInvalid; | ||||
Richard Smith | 426391c | 2012-11-16 00:53:38 +0000 | [diff] [blame] | 11432 | // We're required to check for any non-trivial constructors. Since the |
11433 | // implicit default constructor is suppressed if there are any | ||||
11434 | // user-declared constructors, we just need to check that there is a | ||||
11435 | // trivial default constructor and a trivial copy constructor. (We don't | ||||
11436 | // worry about move constructors here, since this is a C++98 check.) | ||||
11437 | if (RDecl->hasNonTrivialCopyConstructor()) | ||||
Argyrios Kyrtzidis | dd7744d | 2010-08-16 17:27:08 +0000 | [diff] [blame] | 11438 | member = CXXCopyConstructor; |
Sean Hunt | 023df37 | 2011-05-09 18:22:59 +0000 | [diff] [blame] | 11439 | else if (!RDecl->hasTrivialDefaultConstructor()) |
Sean Hunt | f961ea5 | 2011-05-10 19:08:14 +0000 | [diff] [blame] | 11440 | member = CXXDefaultConstructor; |
Richard Smith | 426391c | 2012-11-16 00:53:38 +0000 | [diff] [blame] | 11441 | else if (RDecl->hasNonTrivialCopyAssignment()) |
Argyrios Kyrtzidis | dd7744d | 2010-08-16 17:27:08 +0000 | [diff] [blame] | 11442 | member = CXXCopyAssignment; |
Richard Smith | 426391c | 2012-11-16 00:53:38 +0000 | [diff] [blame] | 11443 | else if (RDecl->hasNonTrivialDestructor()) |
Argyrios Kyrtzidis | dd7744d | 2010-08-16 17:27:08 +0000 | [diff] [blame] | 11444 | member = CXXDestructor; |
11445 | |||||
11446 | if (member != CXXInvalid) { | ||||
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 11447 | if (!getLangOpts().CPlusPlus11 && |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 11448 | getLangOpts().ObjCAutoRefCount && RDecl->hasObjectMember()) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 11449 | // Objective-C++ ARC: it is an error to have a non-trivial field of |
11450 | // a union. However, system headers in Objective-C programs | ||||
11451 | // occasionally have Objective-C lifetime objects within unions, | ||||
11452 | // and rather than cause the program to fail, we make those | ||||
11453 | // members unavailable. | ||||
11454 | SourceLocation Loc = FD->getLocation(); | ||||
11455 | if (getSourceManager().isInSystemHeader(Loc)) { | ||||
11456 | if (!FD->hasAttr<UnavailableAttr>()) | ||||
11457 | FD->addAttr(new (Context) UnavailableAttr(Loc, Context, | ||||
Argyrios Kyrtzidis | b8b0313 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 11458 | "this system field has retaining ownership")); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 11459 | return false; |
11460 | } | ||||
11461 | } | ||||
Richard Smith | e7d7c39 | 2011-10-19 20:41:51 +0000 | [diff] [blame] | 11462 | |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 11463 | Diag(FD->getLocation(), getLangOpts().CPlusPlus11 ? |
Richard Smith | e7d7c39 | 2011-10-19 20:41:51 +0000 | [diff] [blame] | 11464 | diag::warn_cxx98_compat_nontrivial_union_or_anon_struct_member : |
11465 | diag::err_illegal_union_or_anon_struct_member) | ||||
11466 | << (int)FD->getParent()->isUnion() << FD->getDeclName() << member; | ||||
Richard Smith | ac71351 | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 11467 | DiagnoseNontrivial(RDecl, member); |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 11468 | return !getLangOpts().CPlusPlus11; |
Argyrios Kyrtzidis | dd7744d | 2010-08-16 17:27:08 +0000 | [diff] [blame] | 11469 | } |
11470 | } | ||||
11471 | } | ||||
Richard Smith | ac71351 | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 11472 | |
Argyrios Kyrtzidis | dd7744d | 2010-08-16 17:27:08 +0000 | [diff] [blame] | 11473 | return false; |
11474 | } | ||||
11475 | |||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11476 | /// TranslateIvarVisibility - Translate visibility from a token ID to an |
Fariborz Jahanian | 89204a1 | 2007-10-01 16:53:59 +0000 | [diff] [blame] | 11477 | /// AST enum value. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 11478 | static ObjCIvarDecl::AccessControl |
Fariborz Jahanian | 89204a1 | 2007-10-01 16:53:59 +0000 | [diff] [blame] | 11479 | TranslateIvarVisibility(tok::ObjCKeywordKind ivarVisibility) { |
Steve Naroff | f13271f | 2007-09-14 23:09:53 +0000 | [diff] [blame] | 11480 | switch (ivarVisibility) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 11481 | default: llvm_unreachable("Unknown visitibility kind"); |
Chris Lattner | 33d34a6 | 2008-10-12 00:28:42 +0000 | [diff] [blame] | 11482 | case tok::objc_private: return ObjCIvarDecl::Private; |
11483 | case tok::objc_public: return ObjCIvarDecl::Public; | ||||
11484 | case tok::objc_protected: return ObjCIvarDecl::Protected; | ||||
11485 | case tok::objc_package: return ObjCIvarDecl::Package; | ||||
Steve Naroff | f13271f | 2007-09-14 23:09:53 +0000 | [diff] [blame] | 11486 | } |
11487 | } | ||||
11488 | |||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11489 | /// 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] | 11490 | /// in order to create an IvarDecl object for it. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11491 | Decl *Sema::ActOnIvar(Scope *S, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11492 | SourceLocation DeclStart, |
Richard Trieu | f81e5a9 | 2011-09-09 02:00:50 +0000 | [diff] [blame] | 11493 | Declarator &D, Expr *BitfieldWidth, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 11494 | tok::ObjCKeywordKind Visibility) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11495 | |
Fariborz Jahanian | 1d78cc4 | 2008-04-10 23:32:45 +0000 | [diff] [blame] | 11496 | IdentifierInfo *II = D.getIdentifier(); |
11497 | Expr *BitWidth = (Expr*)BitfieldWidth; | ||||
11498 | SourceLocation Loc = DeclStart; | ||||
11499 | if (II) Loc = D.getIdentifierLoc(); | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11500 | |
Fariborz Jahanian | 1d78cc4 | 2008-04-10 23:32:45 +0000 | [diff] [blame] | 11501 | // FIXME: Unnamed fields can be handled in various different ways, for |
11502 | // example, unnamed unions inject all members into the struct namespace! | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11503 | |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 11504 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
11505 | QualType T = TInfo->getType(); | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11506 | |
Fariborz Jahanian | 1d78cc4 | 2008-04-10 23:32:45 +0000 | [diff] [blame] | 11507 | if (BitWidth) { |
Steve Naroff | 63359c8 | 2009-02-20 17:57:11 +0000 | [diff] [blame] | 11508 | // 6.7.2.1p3, 6.7.2.1p4 |
Reid Kleckner | 9a3ecb0 | 2013-07-17 20:46:03 +0000 | [diff] [blame] | 11509 | BitWidth = |
11510 | VerifyBitField(Loc, II, T, /*IsMsStruct=*/false, BitWidth).take(); | ||||
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 11511 | if (!BitWidth) |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 11512 | D.setInvalidType(); |
Fariborz Jahanian | 1d78cc4 | 2008-04-10 23:32:45 +0000 | [diff] [blame] | 11513 | } else { |
11514 | // Not a bitfield. | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11515 | |
Fariborz Jahanian | 1d78cc4 | 2008-04-10 23:32:45 +0000 | [diff] [blame] | 11516 | // validate II. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11517 | |
Fariborz Jahanian | 1d78cc4 | 2008-04-10 23:32:45 +0000 | [diff] [blame] | 11518 | } |
Fariborz Jahanian | 0b7bc8e | 2010-04-26 22:07:03 +0000 | [diff] [blame] | 11519 | if (T->isReferenceType()) { |
11520 | Diag(Loc, diag::err_ivar_reference_type); | ||||
11521 | D.setInvalidType(); | ||||
11522 | } | ||||
Fariborz Jahanian | 1d78cc4 | 2008-04-10 23:32:45 +0000 | [diff] [blame] | 11523 | // C99 6.7.2.1p8: A member of a structure or union may have any type other |
11524 | // than a variably modified type. | ||||
Fariborz Jahanian | 0b7bc8e | 2010-04-26 22:07:03 +0000 | [diff] [blame] | 11525 | else if (T->isVariablyModifiedType()) { |
Anders Carlsson | 96e05bc | 2008-12-07 00:20:55 +0000 | [diff] [blame] | 11526 | Diag(Loc, diag::err_typecheck_ivar_variable_size); |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 11527 | D.setInvalidType(); |
Fariborz Jahanian | 1d78cc4 | 2008-04-10 23:32:45 +0000 | [diff] [blame] | 11528 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11529 | |
Ted Kremenek | b8db21d | 2008-07-23 18:04:17 +0000 | [diff] [blame] | 11530 | // Get the visibility (access control) for this ivar. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11531 | ObjCIvarDecl::AccessControl ac = |
Ted Kremenek | b8db21d | 2008-07-23 18:04:17 +0000 | [diff] [blame] | 11532 | Visibility != tok::objc_not_keyword ? TranslateIvarVisibility(Visibility) |
11533 | : ObjCIvarDecl::None; | ||||
Fariborz Jahanian | 496b5a8 | 2009-06-05 18:16:35 +0000 | [diff] [blame] | 11534 | // Must set ivar's DeclContext to its enclosing interface. |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 11535 | ObjCContainerDecl *EnclosingDecl = cast<ObjCContainerDecl>(CurContext); |
Fariborz Jahanian | c645ddf | 2012-02-02 00:49:12 +0000 | [diff] [blame] | 11536 | if (!EnclosingDecl || EnclosingDecl->isInvalidDecl()) |
11537 | return 0; | ||||
Daniel Dunbar | a19331f | 2010-04-02 18:29:09 +0000 | [diff] [blame] | 11538 | ObjCContainerDecl *EnclosingContext; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11539 | if (ObjCImplementationDecl *IMPDecl = |
Fariborz Jahanian | 496b5a8 | 2009-06-05 18:16:35 +0000 | [diff] [blame] | 11540 | dyn_cast<ObjCImplementationDecl>(EnclosingDecl)) { |
John McCall | 260611a | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 11541 | if (LangOpts.ObjCRuntime.isFragile()) { |
Fariborz Jahanian | 496b5a8 | 2009-06-05 18:16:35 +0000 | [diff] [blame] | 11542 | // 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] | 11543 | EnclosingContext = IMPDecl->getClassInterface(); |
11544 | assert(EnclosingContext && "Implementation has no class interface!"); | ||||
11545 | } | ||||
11546 | else | ||||
11547 | EnclosingContext = EnclosingDecl; | ||||
Fariborz Jahanian | 0bd0459 | 2010-04-06 22:43:48 +0000 | [diff] [blame] | 11548 | } else { |
11549 | if (ObjCCategoryDecl *CDecl = | ||||
11550 | dyn_cast<ObjCCategoryDecl>(EnclosingDecl)) { | ||||
John McCall | 260611a | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 11551 | if (LangOpts.ObjCRuntime.isFragile() || !CDecl->IsClassExtension()) { |
Fariborz Jahanian | 0bd0459 | 2010-04-06 22:43:48 +0000 | [diff] [blame] | 11552 | Diag(Loc, diag::err_misplaced_ivar) << CDecl->IsClassExtension(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11553 | return 0; |
Fariborz Jahanian | 0bd0459 | 2010-04-06 22:43:48 +0000 | [diff] [blame] | 11554 | } |
11555 | } | ||||
Daniel Dunbar | a19331f | 2010-04-02 18:29:09 +0000 | [diff] [blame] | 11556 | EnclosingContext = EnclosingDecl; |
Fariborz Jahanian | 0bd0459 | 2010-04-06 22:43:48 +0000 | [diff] [blame] | 11557 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11558 | |
Ted Kremenek | b8db21d | 2008-07-23 18:04:17 +0000 | [diff] [blame] | 11559 | // Construct the decl. |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 11560 | ObjCIvarDecl *NewID = ObjCIvarDecl::Create(Context, EnclosingContext, |
11561 | DeclStart, Loc, II, T, | ||||
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 11562 | TInfo, ac, (Expr *)BitfieldWidth); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11563 | |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 11564 | if (II) { |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 11565 | NamedDecl *PrevDecl = LookupSingleName(S, II, Loc, LookupMemberName, |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 11566 | ForRedeclaration); |
Fariborz Jahanian | 496b5a8 | 2009-06-05 18:16:35 +0000 | [diff] [blame] | 11567 | if (PrevDecl && isDeclInScope(PrevDecl, EnclosingContext, S) |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 11568 | && !isa<TagDecl>(PrevDecl)) { |
11569 | Diag(Loc, diag::err_duplicate_member) << II; | ||||
11570 | Diag(PrevDecl->getLocation(), diag::note_previous_declaration); | ||||
11571 | NewID->setInvalidDecl(); | ||||
11572 | } | ||||
11573 | } | ||||
11574 | |||||
Ted Kremenek | b8db21d | 2008-07-23 18:04:17 +0000 | [diff] [blame] | 11575 | // Process attributes attached to the ivar. |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 11576 | ProcessDeclAttributes(S, NewID, D); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11577 | |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 11578 | if (D.isInvalidType()) |
Fariborz Jahanian | 1d78cc4 | 2008-04-10 23:32:45 +0000 | [diff] [blame] | 11579 | NewID->setInvalidDecl(); |
Ted Kremenek | b8db21d | 2008-07-23 18:04:17 +0000 | [diff] [blame] | 11580 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 11581 | // In ARC, infer 'retaining' for ivars of retainable type. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 11582 | if (getLangOpts().ObjCAutoRefCount && inferObjCARCLifetime(NewID)) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 11583 | NewID->setInvalidDecl(); |
11584 | |||||
Douglas Gregor | 591dc84 | 2011-09-12 16:11:24 +0000 | [diff] [blame] | 11585 | if (D.getDeclSpec().isModulePrivateSpecified()) |
11586 | NewID->setModulePrivate(); | ||||
11587 | |||||
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 11588 | if (II) { |
11589 | // FIXME: When interfaces are DeclContexts, we'll need to add | ||||
11590 | // these to the interface. | ||||
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11591 | S->AddDecl(NewID); |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 11592 | IdResolver.AddDecl(NewID); |
11593 | } | ||||
Fariborz Jahanian | 8f674a8 | 2012-05-15 16:33:04 +0000 | [diff] [blame] | 11594 | |
John McCall | 260611a | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 11595 | if (LangOpts.ObjCRuntime.isNonFragile() && |
Fariborz Jahanian | 8f674a8 | 2012-05-15 16:33:04 +0000 | [diff] [blame] | 11596 | !NewID->isInvalidDecl() && isa<ObjCInterfaceDecl>(EnclosingDecl)) |
Fariborz Jahanian | dc3eb6a | 2012-05-15 17:43:16 +0000 | [diff] [blame] | 11597 | Diag(Loc, diag::warn_ivars_in_interface); |
Fariborz Jahanian | 8f674a8 | 2012-05-15 16:33:04 +0000 | [diff] [blame] | 11598 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11599 | return NewID; |
Fariborz Jahanian | 1d78cc4 | 2008-04-10 23:32:45 +0000 | [diff] [blame] | 11600 | } |
11601 | |||||
Fariborz Jahanian | d097be8 | 2010-08-23 22:46:52 +0000 | [diff] [blame] | 11602 | /// ActOnLastBitfield - This routine handles synthesized bitfields rules for |
Jordan Rose | d4582b8 | 2013-04-03 01:39:23 +0000 | [diff] [blame] | 11603 | /// class and class extensions. For every class \@interface and class |
11604 | /// extension \@interface, if the last ivar is a bitfield of any type, | ||||
Fariborz Jahanian | d097be8 | 2010-08-23 22:46:52 +0000 | [diff] [blame] | 11605 | /// 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] | 11606 | void Sema::ActOnLastBitfield(SourceLocation DeclLoc, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 11607 | SmallVectorImpl<Decl *> &AllIvarDecls) { |
John McCall | 260611a | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 11608 | if (LangOpts.ObjCRuntime.isFragile() || AllIvarDecls.empty()) |
Fariborz Jahanian | d097be8 | 2010-08-23 22:46:52 +0000 | [diff] [blame] | 11609 | return; |
11610 | |||||
11611 | Decl *ivarDecl = AllIvarDecls[AllIvarDecls.size()-1]; | ||||
11612 | ObjCIvarDecl *Ivar = cast<ObjCIvarDecl>(ivarDecl); | ||||
11613 | |||||
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 11614 | if (!Ivar->isBitField() || Ivar->getBitWidthValue(Context) == 0) |
Fariborz Jahanian | d097be8 | 2010-08-23 22:46:52 +0000 | [diff] [blame] | 11615 | return; |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 11616 | ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CurContext); |
Fariborz Jahanian | d097be8 | 2010-08-23 22:46:52 +0000 | [diff] [blame] | 11617 | if (!ID) { |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 11618 | if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(CurContext)) { |
Fariborz Jahanian | d097be8 | 2010-08-23 22:46:52 +0000 | [diff] [blame] | 11619 | if (!CD->IsClassExtension()) |
11620 | return; | ||||
11621 | } | ||||
11622 | // No need to add this to end of @implementation. | ||||
11623 | else | ||||
11624 | return; | ||||
11625 | } | ||||
11626 | // 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] | 11627 | llvm::APInt Zero(Context.getTypeSize(Context.IntTy), 0); |
11628 | Expr * BW = IntegerLiteral::Create(Context, Zero, Context.IntTy, DeclLoc); | ||||
Fariborz Jahanian | d097be8 | 2010-08-23 22:46:52 +0000 | [diff] [blame] | 11629 | |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 11630 | Ivar = ObjCIvarDecl::Create(Context, cast<ObjCContainerDecl>(CurContext), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 11631 | DeclLoc, DeclLoc, 0, |
Fariborz Jahanian | d097be8 | 2010-08-23 22:46:52 +0000 | [diff] [blame] | 11632 | Context.CharTy, |
Douglas Gregor | 0bbea1b | 2011-08-03 16:26:46 +0000 | [diff] [blame] | 11633 | Context.getTrivialTypeSourceInfo(Context.CharTy, |
11634 | DeclLoc), | ||||
Fariborz Jahanian | d097be8 | 2010-08-23 22:46:52 +0000 | [diff] [blame] | 11635 | ObjCIvarDecl::Private, BW, |
11636 | true); | ||||
11637 | AllIvarDecls.push_back(Ivar); | ||||
11638 | } | ||||
11639 | |||||
Robert Wilhelm | 834c058 | 2013-08-09 18:02:13 +0000 | [diff] [blame] | 11640 | void Sema::ActOnFields(Scope *S, SourceLocation RecLoc, Decl *EnclosingDecl, |
11641 | ArrayRef<Decl *> Fields, SourceLocation LBrac, | ||||
11642 | SourceLocation RBrac, AttributeList *Attr) { | ||||
Steve Naroff | 7421664 | 2007-09-14 22:20:54 +0000 | [diff] [blame] | 11643 | assert(EnclosingDecl && "missing record or interface decl"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11644 | |
Eric Christopher | 6dba4a1 | 2012-07-19 22:22:51 +0000 | [diff] [blame] | 11645 | // If this is an Objective-C @implementation or category and we have |
11646 | // new fields here we should reset the layout of the interface since | ||||
11647 | // it will now change. | ||||
11648 | if (!Fields.empty() && isa<ObjCContainerDecl>(EnclosingDecl)) { | ||||
11649 | ObjCContainerDecl *DC = cast<ObjCContainerDecl>(EnclosingDecl); | ||||
11650 | switch (DC->getKind()) { | ||||
11651 | default: break; | ||||
11652 | case Decl::ObjCCategory: | ||||
11653 | Context.ResetObjCLayout(cast<ObjCCategoryDecl>(DC)->getClassInterface()); | ||||
11654 | break; | ||||
11655 | case Decl::ObjCImplementation: | ||||
11656 | Context. | ||||
11657 | ResetObjCLayout(cast<ObjCImplementationDecl>(DC)->getClassInterface()); | ||||
11658 | break; | ||||
11659 | } | ||||
11660 | } | ||||
11661 | |||||
Eli Friedman | 11e70d7 | 2012-02-07 05:00:47 +0000 | [diff] [blame] | 11662 | RecordDecl *Record = dyn_cast<RecordDecl>(EnclosingDecl); |
11663 | |||||
11664 | // Start counting up the number of named members; make sure to include | ||||
11665 | // members of anonymous structs and unions in the total. | ||||
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 11666 | unsigned NumNamedMembers = 0; |
Eli Friedman | 11e70d7 | 2012-02-07 05:00:47 +0000 | [diff] [blame] | 11667 | if (Record) { |
11668 | for (RecordDecl::decl_iterator i = Record->decls_begin(), | ||||
11669 | e = Record->decls_end(); i != e; i++) { | ||||
11670 | if (IndirectFieldDecl *IFD = dyn_cast<IndirectFieldDecl>(*i)) | ||||
11671 | if (IFD->getDeclName()) | ||||
11672 | ++NumNamedMembers; | ||||
11673 | } | ||||
11674 | } | ||||
11675 | |||||
11676 | // Verify that all the fields are okay. | ||||
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 11677 | SmallVector<FieldDecl*, 32> RecFields; |
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 11678 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 11679 | bool ARCErrReported = false; |
Robert Wilhelm | 834c058 | 2013-08-09 18:02:13 +0000 | [diff] [blame] | 11680 | for (ArrayRef<Decl *>::iterator i = Fields.begin(), end = Fields.end(); |
David Blaikie | 77b6de0 | 2011-09-22 02:58:26 +0000 | [diff] [blame] | 11681 | i != end; ++i) { |
11682 | FieldDecl *FD = cast<FieldDecl>(*i); | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11683 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 11684 | // Get the type for the field. |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 11685 | const Type *FDTy = FD->getType().getTypePtr(); |
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 11686 | |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 11687 | if (!FD->isAnonymousStructOrUnion()) { |
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 11688 | // Remember all fields written by the user. |
11689 | RecFields.push_back(FD); | ||||
11690 | } | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11691 | |
Chris Lattner | 2479366 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 11692 | // If the field is already invalid for some reason, don't emit more |
11693 | // diagnostics about it. | ||||
Eli Friedman | 721e77d | 2009-12-07 00:22:08 +0000 | [diff] [blame] | 11694 | if (FD->isInvalidDecl()) { |
11695 | EnclosingDecl->setInvalidDecl(); | ||||
Chris Lattner | 2479366 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 11696 | continue; |
Eli Friedman | 721e77d | 2009-12-07 00:22:08 +0000 | [diff] [blame] | 11697 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11698 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 11699 | // C99 6.7.2.1p2: |
11700 | // A structure or union shall not contain a member with | ||||
11701 | // incomplete or function type (hence, a structure shall not | ||||
11702 | // contain an instance of itself, but may contain a pointer to | ||||
11703 | // an instance of itself), except that the last member of a | ||||
11704 | // structure with more than one named member may have incomplete | ||||
11705 | // array type; such a structure (and any union containing, | ||||
11706 | // possibly recursively, a member that is such a structure) | ||||
11707 | // shall not be a member of a structure or an element of an | ||||
11708 | // array. | ||||
Chris Lattner | 02c642e | 2007-07-31 21:33:24 +0000 | [diff] [blame] | 11709 | if (FDTy->isFunctionType()) { |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 11710 | // Field declared as a function. |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 11711 | Diag(FD->getLocation(), diag::err_field_declared_as_function) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 11712 | << FD->getDeclName(); |
Steve Naroff | 7421664 | 2007-09-14 22:20:54 +0000 | [diff] [blame] | 11713 | FD->setInvalidDecl(); |
11714 | EnclosingDecl->setInvalidDecl(); | ||||
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 11715 | continue; |
Francois Pichet | 0924618 | 2010-09-15 00:14:08 +0000 | [diff] [blame] | 11716 | } else if (FDTy->isIncompleteArrayType() && Record && |
David Blaikie | 77b6de0 | 2011-09-22 02:58:26 +0000 | [diff] [blame] | 11717 | ((i + 1 == Fields.end() && !Record->isUnion()) || |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 11718 | ((getLangOpts().MicrosoftExt || |
11719 | getLangOpts().CPlusPlus) && | ||||
David Blaikie | 77b6de0 | 2011-09-22 02:58:26 +0000 | [diff] [blame] | 11720 | (i + 1 == Fields.end() || Record->isUnion())))) { |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 11721 | // Flexible array member. |
Argyrios Kyrtzidis | d97cec3 | 2011-03-07 20:04:04 +0000 | [diff] [blame] | 11722 | // Microsoft and g++ is more permissive regarding flexible array. |
Francois Pichet | 0924618 | 2010-09-15 00:14:08 +0000 | [diff] [blame] | 11723 | // It will accept flexible array in union and also |
Anders Carlsson | 4d09e84 | 2010-10-17 23:36:12 +0000 | [diff] [blame] | 11724 | // as the sole element of a struct/class. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 11725 | if (getLangOpts().MicrosoftExt) { |
Francois Pichet | 0924618 | 2010-09-15 00:14:08 +0000 | [diff] [blame] | 11726 | if (Record->isUnion()) |
Argyrios Kyrtzidis | d97cec3 | 2011-03-07 20:04:04 +0000 | [diff] [blame] | 11727 | Diag(FD->getLocation(), diag::ext_flexible_array_union_ms) |
Francois Pichet | 0924618 | 2010-09-15 00:14:08 +0000 | [diff] [blame] | 11728 | << FD->getDeclName(); |
David Blaikie | 77b6de0 | 2011-09-22 02:58:26 +0000 | [diff] [blame] | 11729 | else if (Fields.size() == 1) |
Argyrios Kyrtzidis | d97cec3 | 2011-03-07 20:04:04 +0000 | [diff] [blame] | 11730 | Diag(FD->getLocation(), diag::ext_flexible_array_empty_aggregate_ms) |
Francois Pichet | 0924618 | 2010-09-15 00:14:08 +0000 | [diff] [blame] | 11731 | << FD->getDeclName() << Record->getTagKind(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 11732 | } else if (getLangOpts().CPlusPlus) { |
Argyrios Kyrtzidis | d97cec3 | 2011-03-07 20:04:04 +0000 | [diff] [blame] | 11733 | if (Record->isUnion()) |
11734 | Diag(FD->getLocation(), diag::ext_flexible_array_union_gnu) | ||||
11735 | << FD->getDeclName(); | ||||
David Blaikie | 77b6de0 | 2011-09-22 02:58:26 +0000 | [diff] [blame] | 11736 | else if (Fields.size() == 1) |
Argyrios Kyrtzidis | d97cec3 | 2011-03-07 20:04:04 +0000 | [diff] [blame] | 11737 | Diag(FD->getLocation(), diag::ext_flexible_array_empty_aggregate_gnu) |
11738 | << FD->getDeclName() << Record->getTagKind(); | ||||
David Chisnall | 0961a01 | 2012-03-16 12:15:37 +0000 | [diff] [blame] | 11739 | } else if (!getLangOpts().C99) { |
11740 | if (Record->isUnion()) | ||||
11741 | Diag(FD->getLocation(), diag::ext_flexible_array_union_gnu) | ||||
11742 | << FD->getDeclName(); | ||||
11743 | else | ||||
11744 | Diag(FD->getLocation(), diag::ext_c99_flexible_array_member) | ||||
11745 | << FD->getDeclName() << Record->getTagKind(); | ||||
Argyrios Kyrtzidis | d97cec3 | 2011-03-07 20:04:04 +0000 | [diff] [blame] | 11746 | } else if (NumNamedMembers < 1) { |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 11747 | Diag(FD->getLocation(), diag::err_flexible_array_empty_struct) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 11748 | << FD->getDeclName(); |
Steve Naroff | 7421664 | 2007-09-14 22:20:54 +0000 | [diff] [blame] | 11749 | FD->setInvalidDecl(); |
11750 | EnclosingDecl->setInvalidDecl(); | ||||
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 11751 | continue; |
11752 | } | ||||
Fariborz Jahanian | 4142ceb | 2010-05-26 20:19:07 +0000 | [diff] [blame] | 11753 | if (!FD->getType()->isDependentType() && |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 11754 | !Context.getBaseElementType(FD->getType()).isPODType(Context)) { |
Fariborz Jahanian | 4142ceb | 2010-05-26 20:19:07 +0000 | [diff] [blame] | 11755 | Diag(FD->getLocation(), diag::err_flexible_array_has_nonpod_type) |
Fariborz Jahanian | 2c0a540 | 2010-05-26 20:46:24 +0000 | [diff] [blame] | 11756 | << FD->getDeclName() << FD->getType(); |
Fariborz Jahanian | 4142ceb | 2010-05-26 20:19:07 +0000 | [diff] [blame] | 11757 | FD->setInvalidDecl(); |
11758 | EnclosingDecl->setInvalidDecl(); | ||||
11759 | continue; | ||||
11760 | } | ||||
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 11761 | // 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] | 11762 | if (Record) |
11763 | Record->setHasFlexibleArrayMember(true); | ||||
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 11764 | } else if (!FDTy->isDependentType() && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11765 | RequireCompleteType(FD->getLocation(), FD->getType(), |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 11766 | diag::err_field_incomplete)) { |
11767 | // Incomplete type | ||||
11768 | FD->setInvalidDecl(); | ||||
11769 | EnclosingDecl->setInvalidDecl(); | ||||
11770 | continue; | ||||
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 11771 | } else if (const RecordType *FDTTy = FDTy->getAs<RecordType>()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 11772 | if (FDTTy->getDecl()->hasFlexibleArrayMember()) { |
11773 | // 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] | 11774 | if (Record && Record->isUnion()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 11775 | Record->setHasFlexibleArrayMember(true); |
11776 | } else { | ||||
11777 | // If this is a struct/class and this is not the last element, reject | ||||
11778 | // it. Note that GCC supports variable sized arrays in the middle of | ||||
11779 | // structures. | ||||
David Blaikie | 77b6de0 | 2011-09-22 02:58:26 +0000 | [diff] [blame] | 11780 | if (i + 1 != Fields.end()) |
Douglas Gregor | e4f3e06 | 2009-03-06 23:41:27 +0000 | [diff] [blame] | 11781 | Diag(FD->getLocation(), diag::ext_variable_sized_type_in_struct) |
Chris Lattner | 740782a | 2009-04-25 18:52:45 +0000 | [diff] [blame] | 11782 | << FD->getDeclName() << FD->getType(); |
Douglas Gregor | e4f3e06 | 2009-03-06 23:41:27 +0000 | [diff] [blame] | 11783 | else { |
11784 | // We support flexible arrays at the end of structs in | ||||
11785 | // other structs as an extension. | ||||
11786 | Diag(FD->getLocation(), diag::ext_flexible_array_in_struct) | ||||
11787 | << FD->getDeclName(); | ||||
11788 | if (Record) | ||||
11789 | Record->setHasFlexibleArrayMember(true); | ||||
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 11790 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 11791 | } |
11792 | } | ||||
Fariborz Jahanian | 7f90b53 | 2012-08-16 22:38:41 +0000 | [diff] [blame] | 11793 | if (isa<ObjCContainerDecl>(EnclosingDecl) && |
11794 | RequireNonAbstractType(FD->getLocation(), FD->getType(), | ||||
11795 | diag::err_abstract_type_in_decl, | ||||
11796 | AbstractIvarType)) { | ||||
11797 | // Ivars can not have abstract class types | ||||
11798 | FD->setInvalidDecl(); | ||||
11799 | } | ||||
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 11800 | if (Record && FDTTy->getDecl()->hasObjectMember()) |
11801 | Record->setHasObjectMember(true); | ||||
Fariborz Jahanian | 3ac83d6 | 2013-01-25 23:57:05 +0000 | [diff] [blame] | 11802 | if (Record && FDTTy->getDecl()->hasVolatileMember()) |
11803 | Record->setHasVolatileMember(true); | ||||
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 11804 | } else if (FDTy->isObjCObjectType()) { |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 11805 | /// A field cannot be an Objective-c object |
Fariborz Jahanian | 8eaefdc | 2011-07-26 17:58:54 +0000 | [diff] [blame] | 11806 | Diag(FD->getLocation(), diag::err_statically_allocated_object) |
11807 | << FixItHint::CreateInsertion(FD->getLocation(), "*"); | ||||
11808 | QualType T = Context.getObjCObjectPointerType(FD->getType()); | ||||
11809 | FD->setType(T); | ||||
Douglas Gregor | 4581d45 | 2013-01-28 19:08:09 +0000 | [diff] [blame] | 11810 | } else if (getLangOpts().ObjCAutoRefCount && Record && !ARCErrReported && |
11811 | (!getLangOpts().CPlusPlus || Record->isUnion())) { | ||||
11812 | // It's an error in ARC if a field has lifetime. | ||||
11813 | // We don't want to report this in a system header, though, | ||||
11814 | // so we just make the field unavailable. | ||||
11815 | // FIXME: that's really not sufficient; we need to make the type | ||||
11816 | // itself invalid to, say, initialize or copy. | ||||
11817 | QualType T = FD->getType(); | ||||
11818 | Qualifiers::ObjCLifetime lifetime = T.getObjCLifetime(); | ||||
11819 | if (lifetime && lifetime != Qualifiers::OCL_ExplicitNone) { | ||||
11820 | SourceLocation loc = FD->getLocation(); | ||||
11821 | if (getSourceManager().isInSystemHeader(loc)) { | ||||
11822 | if (!FD->hasAttr<UnavailableAttr>()) { | ||||
11823 | FD->addAttr(new (Context) UnavailableAttr(loc, Context, | ||||
11824 | "this system field has retaining ownership")); | ||||
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 11825 | } |
Douglas Gregor | 4581d45 | 2013-01-28 19:08:09 +0000 | [diff] [blame] | 11826 | } else { |
11827 | Diag(FD->getLocation(), diag::err_arc_objc_object_in_tag) | ||||
Douglas Gregor | bde67cf | 2013-01-28 20:13:44 +0000 | [diff] [blame] | 11828 | << T->isBlockPointerType() << Record->getTagKind(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 11829 | } |
Douglas Gregor | 4581d45 | 2013-01-28 19:08:09 +0000 | [diff] [blame] | 11830 | ARCErrReported = true; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 11831 | } |
Douglas Gregor | 4581d45 | 2013-01-28 19:08:09 +0000 | [diff] [blame] | 11832 | } else if (getLangOpts().ObjC1 && |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 11833 | getLangOpts().getGC() != LangOptions::NonGC && |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 11834 | Record && !Record->hasObjectMember()) { |
Douglas Gregor | 4581d45 | 2013-01-28 19:08:09 +0000 | [diff] [blame] | 11835 | if (FD->getType()->isObjCObjectPointerType() || |
11836 | FD->getType().isObjCGCStrong()) | ||||
11837 | Record->setHasObjectMember(true); | ||||
11838 | else if (Context.getAsArrayType(FD->getType())) { | ||||
11839 | QualType BaseType = Context.getBaseElementType(FD->getType()); | ||||
11840 | if (BaseType->isRecordType() && | ||||
11841 | BaseType->getAs<RecordType>()->getDecl()->hasObjectMember()) | ||||
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 11842 | Record->setHasObjectMember(true); |
Douglas Gregor | 4581d45 | 2013-01-28 19:08:09 +0000 | [diff] [blame] | 11843 | else if (BaseType->isObjCObjectPointerType() || |
11844 | BaseType.isObjCGCStrong()) | ||||
11845 | Record->setHasObjectMember(true); | ||||
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 11846 | } |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 11847 | } |
Fariborz Jahanian | 3ac83d6 | 2013-01-25 23:57:05 +0000 | [diff] [blame] | 11848 | if (Record && FD->getType().isVolatileQualified()) |
11849 | Record->setHasVolatileMember(true); | ||||
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 11850 | // Keep track of the number of named members. |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 11851 | if (FD->getIdentifier()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 11852 | ++NumNamedMembers; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 11853 | } |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 11854 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 11855 | // Okay, we successfully defined 'Record'. |
Chris Lattner | e1e7985 | 2008-02-06 00:51:33 +0000 | [diff] [blame] | 11856 | if (Record) { |
Douglas Gregor | 7a39dd0 | 2010-09-29 00:15:42 +0000 | [diff] [blame] | 11857 | bool Completed = false; |
11858 | if (CXXRecordDecl *CXXRecord = dyn_cast<CXXRecordDecl>(Record)) { | ||||
11859 | if (!CXXRecord->isInvalidDecl()) { | ||||
11860 | // Set access bits correctly on the directly-declared conversions. | ||||
Argyrios Kyrtzidis | 9d29543 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 11861 | for (CXXRecordDecl::conversion_iterator |
11862 | I = CXXRecord->conversion_begin(), | ||||
11863 | E = CXXRecord->conversion_end(); I != E; ++I) | ||||
11864 | I.setAccess((*I)->getAccess()); | ||||
Douglas Gregor | 7a39dd0 | 2010-09-29 00:15:42 +0000 | [diff] [blame] | 11865 | |
11866 | if (!CXXRecord->isDependentType()) { | ||||
Peter Collingbourne | f51cfb8 | 2013-05-20 14:12:25 +0000 | [diff] [blame] | 11867 | if (CXXRecord->hasUserDeclaredDestructor()) { |
11868 | // Adjust user-defined destructor exception spec. | ||||
11869 | if (getLangOpts().CPlusPlus11) | ||||
11870 | AdjustDestructorExceptionSpec(CXXRecord, | ||||
11871 | CXXRecord->getDestructor()); | ||||
11872 | |||||
11873 | // The Microsoft ABI requires that we perform the destructor body | ||||
11874 | // checks (i.e. operator delete() lookup) at every declaration, as | ||||
11875 | // any translation unit may need to emit a deleting destructor. | ||||
11876 | if (Context.getTargetInfo().getCXXABI().isMicrosoft()) | ||||
11877 | CheckDestructor(CXXRecord->getDestructor()); | ||||
11878 | } | ||||
Sebastian Redl | 0ee3391 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 11879 | |
Douglas Gregor | 7a39dd0 | 2010-09-29 00:15:42 +0000 | [diff] [blame] | 11880 | // Add any implicitly-declared members to this class. |
11881 | AddImplicitlyDeclaredMembersToClass(CXXRecord); | ||||
11882 | |||||
11883 | // If we have virtual base classes, we may end up finding multiple | ||||
11884 | // final overriders for a given virtual function. Check for this | ||||
11885 | // problem now. | ||||
11886 | if (CXXRecord->getNumVBases()) { | ||||
11887 | CXXFinalOverriderMap FinalOverriders; | ||||
11888 | CXXRecord->getFinalOverriders(FinalOverriders); | ||||
11889 | |||||
11890 | for (CXXFinalOverriderMap::iterator M = FinalOverriders.begin(), | ||||
11891 | MEnd = FinalOverriders.end(); | ||||
11892 | M != MEnd; ++M) { | ||||
11893 | for (OverridingMethods::iterator SO = M->second.begin(), | ||||
11894 | SOEnd = M->second.end(); | ||||
11895 | SO != SOEnd; ++SO) { | ||||
11896 | assert(SO->second.size() > 0 && | ||||
11897 | "Virtual function without overridding functions?"); | ||||
11898 | if (SO->second.size() == 1) | ||||
11899 | continue; | ||||
11900 | |||||
11901 | // C++ [class.virtual]p2: | ||||
11902 | // In a derived class, if a virtual member function of a base | ||||
11903 | // class subobject has more than one final overrider the | ||||
11904 | // program is ill-formed. | ||||
11905 | Diag(Record->getLocation(), diag::err_multiple_final_overriders) | ||||
Roman Divacky | 31ba613 | 2012-09-06 15:59:27 +0000 | [diff] [blame] | 11906 | << (const NamedDecl *)M->first << Record; |
Douglas Gregor | 7a39dd0 | 2010-09-29 00:15:42 +0000 | [diff] [blame] | 11907 | Diag(M->first->getLocation(), |
11908 | diag::note_overridden_virtual_function); | ||||
11909 | for (OverridingMethods::overriding_iterator | ||||
11910 | OM = SO->second.begin(), | ||||
11911 | OMEnd = SO->second.end(); | ||||
11912 | OM != OMEnd; ++OM) | ||||
11913 | Diag(OM->Method->getLocation(), diag::note_final_overrider) | ||||
Roman Divacky | 31ba613 | 2012-09-06 15:59:27 +0000 | [diff] [blame] | 11914 | << (const NamedDecl *)M->first << OM->Method->getParent(); |
Douglas Gregor | 7a39dd0 | 2010-09-29 00:15:42 +0000 | [diff] [blame] | 11915 | |
11916 | Record->setInvalidDecl(); | ||||
11917 | } | ||||
11918 | } | ||||
11919 | CXXRecord->completeDefinition(&FinalOverriders); | ||||
11920 | Completed = true; | ||||
11921 | } | ||||
11922 | } | ||||
11923 | } | ||||
11924 | } | ||||
11925 | |||||
11926 | if (!Completed) | ||||
11927 | Record->completeDefinition(); | ||||
Sebastian Redl | 0ee3391 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 11928 | |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 11929 | if (Record->hasAttrs()) |
11930 | CheckAlignasUnderalignment(Record); | ||||
Serge Pavlov | 122e601 | 2013-06-08 13:29:58 +0000 | [diff] [blame] | 11931 | |
11932 | // Check if the structure/union declaration is a language extension. | ||||
11933 | if (!getLangOpts().CPlusPlus) { | ||||
11934 | bool ZeroSize = true; | ||||
Serge Pavlov | 0dcea35 | 2013-06-17 17:18:51 +0000 | [diff] [blame] | 11935 | bool IsEmpty = true; |
11936 | unsigned NonBitFields = 0; | ||||
Serge Pavlov | 122e601 | 2013-06-08 13:29:58 +0000 | [diff] [blame] | 11937 | for (RecordDecl::field_iterator I = Record->field_begin(), |
Serge Pavlov | 0dcea35 | 2013-06-17 17:18:51 +0000 | [diff] [blame] | 11938 | E = Record->field_end(); |
11939 | (NonBitFields == 0 || ZeroSize) && I != E; ++I) { | ||||
11940 | IsEmpty = false; | ||||
Serge Pavlov | 122e601 | 2013-06-08 13:29:58 +0000 | [diff] [blame] | 11941 | if (I->isUnnamedBitfield()) { |
Serge Pavlov | 122e601 | 2013-06-08 13:29:58 +0000 | [diff] [blame] | 11942 | if (I->getBitWidthValue(Context) > 0) |
11943 | ZeroSize = false; | ||||
11944 | } else { | ||||
Serge Pavlov | 0dcea35 | 2013-06-17 17:18:51 +0000 | [diff] [blame] | 11945 | ++NonBitFields; |
11946 | QualType FieldType = I->getType(); | ||||
11947 | if (FieldType->isIncompleteType() || | ||||
11948 | !Context.getTypeSizeInChars(FieldType).isZero()) | ||||
11949 | ZeroSize = false; | ||||
Serge Pavlov | 122e601 | 2013-06-08 13:29:58 +0000 | [diff] [blame] | 11950 | } |
11951 | } | ||||
11952 | |||||
11953 | // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in | ||||
11954 | // C++. | ||||
Serge Pavlov | 0dcea35 | 2013-06-17 17:18:51 +0000 | [diff] [blame] | 11955 | if (ZeroSize) |
11956 | Diag(RecLoc, diag::warn_zero_size_struct_union_compat) << IsEmpty | ||||
11957 | << Record->isUnion() << (NonBitFields > 1); | ||||
Serge Pavlov | 122e601 | 2013-06-08 13:29:58 +0000 | [diff] [blame] | 11958 | |
11959 | // Structs without named members are extension in C (C99 6.7.2.1p7), but | ||||
11960 | // are accepted by GCC. | ||||
Serge Pavlov | 0dcea35 | 2013-06-17 17:18:51 +0000 | [diff] [blame] | 11961 | if (NonBitFields == 0) { |
11962 | if (IsEmpty) | ||||
Serge Pavlov | 122e601 | 2013-06-08 13:29:58 +0000 | [diff] [blame] | 11963 | Diag(RecLoc, diag::ext_empty_struct_union) << Record->isUnion(); |
11964 | else | ||||
11965 | Diag(RecLoc, diag::ext_no_named_members_in_struct_union) << Record->isUnion(); | ||||
11966 | } | ||||
11967 | } | ||||
Chris Lattner | e1e7985 | 2008-02-06 00:51:33 +0000 | [diff] [blame] | 11968 | } else { |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 11969 | ObjCIvarDecl **ClsFields = |
11970 | reinterpret_cast<ObjCIvarDecl**>(RecFields.data()); | ||||
Fariborz Jahanian | 60f8c86 | 2008-12-13 20:28:25 +0000 | [diff] [blame] | 11971 | if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(EnclosingDecl)) { |
Douglas Gregor | 05c272f | 2011-12-15 22:34:59 +0000 | [diff] [blame] | 11972 | ID->setEndOfDefinitionLoc(RBrac); |
Fariborz Jahanian | 496b5a8 | 2009-06-05 18:16:35 +0000 | [diff] [blame] | 11973 | // Add ivar's to class's DeclContext. |
11974 | for (unsigned i = 0, e = RecFields.size(); i != e; ++i) { | ||||
11975 | ClsFields[i]->setLexicalDeclContext(ID); | ||||
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 11976 | ID->addDecl(ClsFields[i]); |
Fariborz Jahanian | 496b5a8 | 2009-06-05 18:16:35 +0000 | [diff] [blame] | 11977 | } |
Fariborz Jahanian | 3281eff | 2008-12-16 01:08:35 +0000 | [diff] [blame] | 11978 | // Must enforce the rule that ivars in the base classes may not be |
11979 | // duplicates. | ||||
Fariborz Jahanian | f914b97 | 2010-02-23 23:41:11 +0000 | [diff] [blame] | 11980 | if (ID->getSuperClass()) |
11981 | DiagnoseDuplicateIvars(ID, ID->getSuperClass()); | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11982 | } else if (ObjCImplementationDecl *IMPDecl = |
Chris Lattner | 1829a6d | 2009-02-23 22:00:08 +0000 | [diff] [blame] | 11983 | dyn_cast<ObjCImplementationDecl>(EnclosingDecl)) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 11984 | assert(IMPDecl && "ActOnFields - missing ObjCImplementationDecl"); |
Fariborz Jahanian | 496b5a8 | 2009-06-05 18:16:35 +0000 | [diff] [blame] | 11985 | for (unsigned I = 0, N = RecFields.size(); I != N; ++I) |
11986 | // Ivar declared in @implementation never belongs to the implementation. | ||||
11987 | // Only it is in implementation's lexical context. | ||||
Douglas Gregor | 8f36aba | 2009-04-23 03:23:08 +0000 | [diff] [blame] | 11988 | ClsFields[I]->setLexicalDeclContext(IMPDecl); |
Fariborz Jahanian | 3a3ca1b | 2007-10-31 18:48:14 +0000 | [diff] [blame] | 11989 | CheckImplementationIvars(IMPDecl, ClsFields, RecFields.size(), RBrac); |
Fariborz Jahanian | af30029 | 2012-02-20 20:09:20 +0000 | [diff] [blame] | 11990 | IMPDecl->setIvarLBraceLoc(LBrac); |
11991 | IMPDecl->setIvarRBraceLoc(RBrac); | ||||
Fariborz Jahanian | 83c481a | 2010-02-22 23:04:20 +0000 | [diff] [blame] | 11992 | } else if (ObjCCategoryDecl *CDecl = |
11993 | dyn_cast<ObjCCategoryDecl>(EnclosingDecl)) { | ||||
Fariborz Jahanian | 0bd0459 | 2010-04-06 22:43:48 +0000 | [diff] [blame] | 11994 | // case of ivars in class extension; all other cases have been |
11995 | // reported as errors elsewhere. | ||||
11996 | // FIXME. Class extension does not have a LocEnd field. | ||||
11997 | // CDecl->setLocEnd(RBrac); | ||||
11998 | // Add ivar's to class extension's DeclContext. | ||||
Fariborz Jahanian | 3ff86f7 | 2011-10-21 18:03:52 +0000 | [diff] [blame] | 11999 | // Diagnose redeclaration of private ivars. |
12000 | ObjCInterfaceDecl *IDecl = CDecl->getClassInterface(); | ||||
Fariborz Jahanian | 0bd0459 | 2010-04-06 22:43:48 +0000 | [diff] [blame] | 12001 | for (unsigned i = 0, e = RecFields.size(); i != e; ++i) { |
Fariborz Jahanian | 3ff86f7 | 2011-10-21 18:03:52 +0000 | [diff] [blame] | 12002 | if (IDecl) { |
12003 | if (const ObjCIvarDecl *ClsIvar = | ||||
12004 | IDecl->getIvarDecl(ClsFields[i]->getIdentifier())) { | ||||
12005 | Diag(ClsFields[i]->getLocation(), | ||||
12006 | diag::err_duplicate_ivar_declaration); | ||||
12007 | Diag(ClsIvar->getLocation(), diag::note_previous_definition); | ||||
12008 | continue; | ||||
12009 | } | ||||
Douglas Gregor | d329724 | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 12010 | for (ObjCInterfaceDecl::known_extensions_iterator |
12011 | Ext = IDecl->known_extensions_begin(), | ||||
12012 | ExtEnd = IDecl->known_extensions_end(); | ||||
12013 | Ext != ExtEnd; ++Ext) { | ||||
12014 | if (const ObjCIvarDecl *ClsExtIvar | ||||
12015 | = Ext->getIvarDecl(ClsFields[i]->getIdentifier())) { | ||||
Fariborz Jahanian | 3ff86f7 | 2011-10-21 18:03:52 +0000 | [diff] [blame] | 12016 | Diag(ClsFields[i]->getLocation(), |
12017 | diag::err_duplicate_ivar_declaration); | ||||
12018 | Diag(ClsExtIvar->getLocation(), diag::note_previous_definition); | ||||
12019 | continue; | ||||
12020 | } | ||||
12021 | } | ||||
12022 | } | ||||
Fariborz Jahanian | 0bd0459 | 2010-04-06 22:43:48 +0000 | [diff] [blame] | 12023 | ClsFields[i]->setLexicalDeclContext(CDecl); |
12024 | CDecl->addDecl(ClsFields[i]); | ||||
Fariborz Jahanian | 83c481a | 2010-02-22 23:04:20 +0000 | [diff] [blame] | 12025 | } |
Fariborz Jahanian | af30029 | 2012-02-20 20:09:20 +0000 | [diff] [blame] | 12026 | CDecl->setIvarLBraceLoc(LBrac); |
12027 | CDecl->setIvarRBraceLoc(RBrac); | ||||
Fariborz Jahanian | d0b90bf | 2007-09-26 18:27:25 +0000 | [diff] [blame] | 12028 | } |
Fariborz Jahanian | b04a021 | 2007-09-14 21:08:27 +0000 | [diff] [blame] | 12029 | } |
Daniel Dunbar | 7d07664 | 2008-10-03 17:33:35 +0000 | [diff] [blame] | 12030 | |
12031 | if (Attr) | ||||
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 12032 | ProcessDeclAttributeList(S, Record, Attr); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 12033 | } |
12034 | |||||
Douglas Gregor | 677e4fe | 2010-02-01 23:36:03 +0000 | [diff] [blame] | 12035 | /// \brief Determine whether the given integral value is representable within |
12036 | /// the given type T. | ||||
12037 | static bool isRepresentableIntegerValue(ASTContext &Context, | ||||
12038 | llvm::APSInt &Value, | ||||
12039 | QualType T) { | ||||
Douglas Gregor | 9d3347a | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 12040 | assert(T->isIntegralType(Context) && "Integral type required!"); |
Douglas Gregor | af68d4e | 2010-04-15 15:53:31 +0000 | [diff] [blame] | 12041 | unsigned BitWidth = Context.getIntWidth(T); |
Douglas Gregor | 677e4fe | 2010-02-01 23:36:03 +0000 | [diff] [blame] | 12042 | |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 12043 | if (Value.isUnsigned() || Value.isNonNegative()) { |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 12044 | if (T->isSignedIntegerOrEnumerationType()) |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 12045 | --BitWidth; |
12046 | return Value.getActiveBits() <= BitWidth; | ||||
12047 | } | ||||
Douglas Gregor | 677e4fe | 2010-02-01 23:36:03 +0000 | [diff] [blame] | 12048 | return Value.getMinSignedBits() <= BitWidth; |
12049 | } | ||||
12050 | |||||
12051 | // \brief Given an integral type, return the next larger integral type | ||||
12052 | // (or a NULL type of no such type exists). | ||||
12053 | static QualType getNextLargerIntegralType(ASTContext &Context, QualType T) { | ||||
12054 | // FIXME: Int128/UInt128 support, which also needs to be introduced into | ||||
12055 | // enum checking below. | ||||
Douglas Gregor | 9d3347a | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 12056 | assert(T->isIntegralType(Context) && "Integral type required!"); |
Douglas Gregor | 677e4fe | 2010-02-01 23:36:03 +0000 | [diff] [blame] | 12057 | const unsigned NumTypes = 4; |
12058 | QualType SignedIntegralTypes[NumTypes] = { | ||||
12059 | Context.ShortTy, Context.IntTy, Context.LongTy, Context.LongLongTy | ||||
12060 | }; | ||||
12061 | QualType UnsignedIntegralTypes[NumTypes] = { | ||||
12062 | Context.UnsignedShortTy, Context.UnsignedIntTy, Context.UnsignedLongTy, | ||||
12063 | Context.UnsignedLongLongTy | ||||
12064 | }; | ||||
12065 | |||||
12066 | unsigned BitWidth = Context.getTypeSize(T); | ||||
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 12067 | QualType *Types = T->isSignedIntegerOrEnumerationType()? SignedIntegralTypes |
12068 | : UnsignedIntegralTypes; | ||||
Douglas Gregor | 677e4fe | 2010-02-01 23:36:03 +0000 | [diff] [blame] | 12069 | for (unsigned I = 0; I != NumTypes; ++I) |
12070 | if (Context.getTypeSize(Types[I]) > BitWidth) | ||||
12071 | return Types[I]; | ||||
12072 | |||||
12073 | return QualType(); | ||||
12074 | } | ||||
12075 | |||||
Douglas Gregor | 879fd49 | 2009-03-17 19:05:46 +0000 | [diff] [blame] | 12076 | EnumConstantDecl *Sema::CheckEnumConstant(EnumDecl *Enum, |
12077 | EnumConstantDecl *LastEnumConst, | ||||
12078 | SourceLocation IdLoc, | ||||
12079 | IdentifierInfo *Id, | ||||
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 12080 | Expr *Val) { |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 12081 | unsigned IntWidth = Context.getTargetInfo().getIntWidth(); |
Douglas Gregor | 677e4fe | 2010-02-01 23:36:03 +0000 | [diff] [blame] | 12082 | llvm::APSInt EnumVal(IntWidth); |
Douglas Gregor | 879fd49 | 2009-03-17 19:05:46 +0000 | [diff] [blame] | 12083 | QualType EltTy; |
Douglas Gregor | 0c9e479 | 2010-12-16 00:24:44 +0000 | [diff] [blame] | 12084 | |
12085 | if (Val && DiagnoseUnexpandedParameterPack(Val, UPPC_EnumeratorValue)) | ||||
12086 | Val = 0; | ||||
12087 | |||||
Eli Friedman | 19efa3e | 2011-12-06 00:10:34 +0000 | [diff] [blame] | 12088 | if (Val) |
12089 | Val = DefaultLvalueConversion(Val).take(); | ||||
12090 | |||||
Douglas Gregor | 4912c34 | 2009-11-06 00:03:12 +0000 | [diff] [blame] | 12091 | if (Val) { |
Douglas Gregor | 9b9edd6 | 2010-03-02 17:53:14 +0000 | [diff] [blame] | 12092 | if (Enum->isDependentType() || Val->isTypeDependent()) |
Douglas Gregor | 4912c34 | 2009-11-06 00:03:12 +0000 | [diff] [blame] | 12093 | EltTy = Context.DependentTy; |
12094 | else { | ||||
Douglas Gregor | 4912c34 | 2009-11-06 00:03:12 +0000 | [diff] [blame] | 12095 | SourceLocation ExpLoc; |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 12096 | if (getLangOpts().CPlusPlus11 && Enum->isFixed() && |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 12097 | !getLangOpts().MicrosoftMode) { |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 12098 | // C++11 [dcl.enum]p5: If the underlying type is fixed, [...] the |
12099 | // constant-expression in the enumerator-definition shall be a converted | ||||
12100 | // constant expression of the underlying type. | ||||
12101 | EltTy = Enum->getIntegerType(); | ||||
12102 | ExprResult Converted = | ||||
12103 | CheckConvertedConstantExpression(Val, EltTy, EnumVal, | ||||
12104 | CCEK_Enumerator); | ||||
12105 | if (Converted.isInvalid()) | ||||
12106 | Val = 0; | ||||
12107 | else | ||||
12108 | Val = Converted.take(); | ||||
12109 | } else if (!Val->isValueDependent() && | ||||
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 12110 | !(Val = VerifyIntegerConstantExpression(Val, |
12111 | &EnumVal).take())) { | ||||
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 12112 | // 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] | 12113 | } else { |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 12114 | if (Enum->isFixed()) { |
12115 | EltTy = Enum->getIntegerType(); | ||||
12116 | |||||
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 12117 | // In Obj-C and Microsoft mode, require the enumeration value to be |
12118 | // representable in the underlying type of the enumeration. In C++11, | ||||
12119 | // we perform a non-narrowing conversion as part of converted constant | ||||
12120 | // expression checking. | ||||
Francois Pichet | 842e7a2 | 2010-10-18 15:01:13 +0000 | [diff] [blame] | 12121 | if (!isRepresentableIntegerValue(Context, EnumVal, EltTy)) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 12122 | if (getLangOpts().MicrosoftMode) { |
Francois Pichet | 842e7a2 | 2010-10-18 15:01:13 +0000 | [diff] [blame] | 12123 | Diag(IdLoc, diag::ext_enumerator_too_large) << EltTy; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 12124 | Val = ImpCastExprToType(Val, EltTy, CK_IntegralCast).take(); |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 12125 | } else |
12126 | Diag(IdLoc, diag::err_enumerator_too_large) << EltTy; | ||||
Francois Pichet | 842e7a2 | 2010-10-18 15:01:13 +0000 | [diff] [blame] | 12127 | } else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 12128 | Val = ImpCastExprToType(Val, EltTy, CK_IntegralCast).take(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 12129 | } else if (getLangOpts().CPlusPlus) { |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 12130 | // C++11 [dcl.enum]p5: |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 12131 | // If the underlying type is not fixed, the type of each enumerator |
12132 | // is the type of its initializing value: | ||||
12133 | // - If an initializer is specified for an enumerator, the | ||||
12134 | // initializing value has the same type as the expression. | ||||
12135 | EltTy = Val->getType(); | ||||
Eli Friedman | 04ca252 | 2012-02-07 04:34:38 +0000 | [diff] [blame] | 12136 | } else { |
12137 | // C99 6.7.2.2p2: | ||||
12138 | // The expression that defines the value of an enumeration constant | ||||
12139 | // shall be an integer constant expression that has a value | ||||
12140 | // representable as an int. | ||||
12141 | |||||
12142 | // Complain if the value is not representable in an int. | ||||
12143 | if (!isRepresentableIntegerValue(Context, EnumVal, Context.IntTy)) | ||||
12144 | Diag(IdLoc, diag::ext_enum_value_not_int) | ||||
12145 | << EnumVal.toString(10) << Val->getSourceRange() | ||||
12146 | << (EnumVal.isUnsigned() || EnumVal.isNonNegative()); | ||||
12147 | else if (!Context.hasSameType(Val->getType(), Context.IntTy)) { | ||||
12148 | // Force the type of the expression to 'int'. | ||||
12149 | Val = ImpCastExprToType(Val, Context.IntTy, CK_IntegralCast).take(); | ||||
12150 | } | ||||
12151 | EltTy = Val->getType(); | ||||
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 12152 | } |
Douglas Gregor | 4912c34 | 2009-11-06 00:03:12 +0000 | [diff] [blame] | 12153 | } |
Douglas Gregor | 879fd49 | 2009-03-17 19:05:46 +0000 | [diff] [blame] | 12154 | } |
12155 | } | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12156 | |
Douglas Gregor | 879fd49 | 2009-03-17 19:05:46 +0000 | [diff] [blame] | 12157 | if (!Val) { |
Eli Friedman | ed0716b | 2009-12-11 01:34:50 +0000 | [diff] [blame] | 12158 | if (Enum->isDependentType()) |
12159 | EltTy = Context.DependentTy; | ||||
Douglas Gregor | 677e4fe | 2010-02-01 23:36:03 +0000 | [diff] [blame] | 12160 | else if (!LastEnumConst) { |
12161 | // C++0x [dcl.enum]p5: | ||||
12162 | // If the underlying type is not fixed, the type of each enumerator | ||||
12163 | // is the type of its initializing value: | ||||
12164 | // - If no initializer is specified for the first enumerator, the | ||||
12165 | // initializing value has an unspecified integral type. | ||||
12166 | // | ||||
12167 | // GCC uses 'int' for its unspecified integral type, as does | ||||
12168 | // C99 6.7.2.2p3. | ||||
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 12169 | if (Enum->isFixed()) { |
12170 | EltTy = Enum->getIntegerType(); | ||||
12171 | } | ||||
12172 | else { | ||||
12173 | EltTy = Context.IntTy; | ||||
12174 | } | ||||
Douglas Gregor | 677e4fe | 2010-02-01 23:36:03 +0000 | [diff] [blame] | 12175 | } else { |
Douglas Gregor | 879fd49 | 2009-03-17 19:05:46 +0000 | [diff] [blame] | 12176 | // Assign the last value + 1. |
12177 | EnumVal = LastEnumConst->getInitVal(); | ||||
12178 | ++EnumVal; | ||||
Douglas Gregor | 677e4fe | 2010-02-01 23:36:03 +0000 | [diff] [blame] | 12179 | EltTy = LastEnumConst->getType(); |
Douglas Gregor | 879fd49 | 2009-03-17 19:05:46 +0000 | [diff] [blame] | 12180 | |
12181 | // Check for overflow on increment. | ||||
Douglas Gregor | 677e4fe | 2010-02-01 23:36:03 +0000 | [diff] [blame] | 12182 | if (EnumVal < LastEnumConst->getInitVal()) { |
12183 | // C++0x [dcl.enum]p5: | ||||
12184 | // If the underlying type is not fixed, the type of each enumerator | ||||
12185 | // is the type of its initializing value: | ||||
12186 | // | ||||
12187 | // - Otherwise the type of the initializing value is the same as | ||||
12188 | // the type of the initializing value of the preceding enumerator | ||||
12189 | // unless the incremented value is not representable in that type, | ||||
12190 | // in which case the type is an unspecified integral type | ||||
12191 | // sufficient to contain the incremented value. If no such type | ||||
12192 | // exists, the program is ill-formed. | ||||
12193 | QualType T = getNextLargerIntegralType(Context, EltTy); | ||||
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 12194 | if (T.isNull() || Enum->isFixed()) { |
Douglas Gregor | 677e4fe | 2010-02-01 23:36:03 +0000 | [diff] [blame] | 12195 | // There is no integral type larger enough to represent this |
12196 | // value. Complain, then allow the value to wrap around. | ||||
12197 | EnumVal = LastEnumConst->getInitVal(); | ||||
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 12198 | EnumVal = EnumVal.zext(EnumVal.getBitWidth() * 2); |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 12199 | ++EnumVal; |
12200 | if (Enum->isFixed()) | ||||
12201 | // When the underlying type is fixed, this is ill-formed. | ||||
12202 | Diag(IdLoc, diag::err_enumerator_wrapped) | ||||
12203 | << EnumVal.toString(10) | ||||
12204 | << EltTy; | ||||
12205 | else | ||||
12206 | Diag(IdLoc, diag::warn_enumerator_too_large) | ||||
12207 | << EnumVal.toString(10); | ||||
Douglas Gregor | 677e4fe | 2010-02-01 23:36:03 +0000 | [diff] [blame] | 12208 | } else { |
12209 | EltTy = T; | ||||
12210 | } | ||||
12211 | |||||
12212 | // Retrieve the last enumerator's value, extent that type to the | ||||
12213 | // type that is supposed to be large enough to represent the incremented | ||||
12214 | // value, then increment. | ||||
12215 | EnumVal = LastEnumConst->getInitVal(); | ||||
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 12216 | EnumVal.setIsSigned(EltTy->isSignedIntegerOrEnumerationType()); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 12217 | EnumVal = EnumVal.zextOrTrunc(Context.getIntWidth(EltTy)); |
Douglas Gregor | 677e4fe | 2010-02-01 23:36:03 +0000 | [diff] [blame] | 12218 | ++EnumVal; |
12219 | |||||
12220 | // If we're not in C++, diagnose the overflow of enumerator values, | ||||
12221 | // which in C99 means that the enumerator value is not representable in | ||||
12222 | // an int (C99 6.7.2.2p2). However, we support GCC's extension that | ||||
12223 | // permits enumerator values that are representable in some larger | ||||
12224 | // integral type. | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 12225 | if (!getLangOpts().CPlusPlus && !T.isNull()) |
Douglas Gregor | 677e4fe | 2010-02-01 23:36:03 +0000 | [diff] [blame] | 12226 | Diag(IdLoc, diag::warn_enum_value_overflow); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 12227 | } else if (!getLangOpts().CPlusPlus && |
Douglas Gregor | 677e4fe | 2010-02-01 23:36:03 +0000 | [diff] [blame] | 12228 | !isRepresentableIntegerValue(Context, EnumVal, EltTy)) { |
12229 | // Enforce C99 6.7.2.2p2 even when we compute the next value. | ||||
12230 | Diag(IdLoc, diag::ext_enum_value_not_int) | ||||
12231 | << EnumVal.toString(10) << 1; | ||||
12232 | } | ||||
Douglas Gregor | 879fd49 | 2009-03-17 19:05:46 +0000 | [diff] [blame] | 12233 | } |
12234 | } | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12235 | |
Douglas Gregor | 9b9edd6 | 2010-03-02 17:53:14 +0000 | [diff] [blame] | 12236 | if (!EltTy->isDependentType()) { |
Douglas Gregor | 677e4fe | 2010-02-01 23:36:03 +0000 | [diff] [blame] | 12237 | // Make the enumerator value match the signedness and size of the |
12238 | // enumerator's type. | ||||
Eli Friedman | 04ca252 | 2012-02-07 04:34:38 +0000 | [diff] [blame] | 12239 | EnumVal = EnumVal.extOrTrunc(Context.getIntWidth(EltTy)); |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 12240 | EnumVal.setIsSigned(EltTy->isSignedIntegerOrEnumerationType()); |
Douglas Gregor | 677e4fe | 2010-02-01 23:36:03 +0000 | [diff] [blame] | 12241 | } |
Douglas Gregor | 4912c34 | 2009-11-06 00:03:12 +0000 | [diff] [blame] | 12242 | |
Douglas Gregor | 879fd49 | 2009-03-17 19:05:46 +0000 | [diff] [blame] | 12243 | return EnumConstantDecl::Create(Context, Enum, IdLoc, Id, EltTy, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12244 | Val, EnumVal); |
Douglas Gregor | 879fd49 | 2009-03-17 19:05:46 +0000 | [diff] [blame] | 12245 | } |
12246 | |||||
12247 | |||||
John McCall | 5b629aa | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 12248 | Decl *Sema::ActOnEnumConstant(Scope *S, Decl *theEnumDecl, Decl *lastEnumConst, |
12249 | SourceLocation IdLoc, IdentifierInfo *Id, | ||||
12250 | AttributeList *Attr, | ||||
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 12251 | SourceLocation EqualLoc, Expr *Val) { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 12252 | EnumDecl *TheEnumDecl = cast<EnumDecl>(theEnumDecl); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 12253 | EnumConstantDecl *LastEnumConst = |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 12254 | cast_or_null<EnumConstantDecl>(lastEnumConst); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 12255 | |
Chris Lattner | 31e0572 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 12256 | // The scope passed in may not be a decl scope. Zip up the scope tree until |
12257 | // we find one that is. | ||||
Douglas Gregor | 1a0d31a | 2009-01-12 18:45:55 +0000 | [diff] [blame] | 12258 | S = getNonFieldDeclScope(S); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12259 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 12260 | // Verify that there isn't already something declared with this name in this |
12261 | // scope. | ||||
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 12262 | NamedDecl *PrevDecl = LookupSingleName(S, Id, IdLoc, LookupOrdinaryName, |
Douglas Gregor | e39fe72 | 2010-01-19 06:06:57 +0000 | [diff] [blame] | 12263 | ForRedeclaration); |
Douglas Gregor | f57172b | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 12264 | if (PrevDecl && PrevDecl->isTemplateParameter()) { |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 12265 | // Maybe we will complain about the shadowed template parameter. |
12266 | DiagnoseTemplateParameterShadow(IdLoc, PrevDecl); | ||||
12267 | // Just pretend that we didn't see the previous declaration. | ||||
12268 | PrevDecl = 0; | ||||
12269 | } | ||||
12270 | |||||
12271 | if (PrevDecl) { | ||||
Argyrios Kyrtzidis | 0ff12f0 | 2008-07-16 21:01:53 +0000 | [diff] [blame] | 12272 | // When in C++, we may get a TagDecl with the same name; in this case the |
12273 | // enum constant will 'hide' the tag. | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 12274 | assert((getLangOpts().CPlusPlus || !isa<TagDecl>(PrevDecl)) && |
Argyrios Kyrtzidis | 0ff12f0 | 2008-07-16 21:01:53 +0000 | [diff] [blame] | 12275 | "Received TagDecl when not in C++!"); |
Argyrios Kyrtzidis | 15a12d0 | 2008-09-09 21:18:04 +0000 | [diff] [blame] | 12276 | if (!isa<TagDecl>(PrevDecl) && isDeclInScope(PrevDecl, CurContext, S)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 12277 | if (isa<EnumConstantDecl>(PrevDecl)) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 12278 | Diag(IdLoc, diag::err_redefinition_of_enumerator) << Id; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 12279 | else |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 12280 | Diag(IdLoc, diag::err_redefinition) << Id; |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 12281 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 12282 | return 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 12283 | } |
12284 | } | ||||
12285 | |||||
Aaron Ballman | f816787 | 2012-07-19 03:12:23 +0000 | [diff] [blame] | 12286 | // C++ [class.mem]p15: |
12287 | // If T is the name of a class, then each of the following shall have a name | ||||
12288 | // different from T: | ||||
12289 | // - every enumerator of every member of class T that is an unscoped | ||||
12290 | // enumerated type | ||||
Douglas Gregor | a6e937c | 2010-10-15 13:21:21 +0000 | [diff] [blame] | 12291 | if (CXXRecordDecl *Record |
12292 | = dyn_cast<CXXRecordDecl>( | ||||
12293 | TheEnumDecl->getDeclContext()->getRedeclContext())) | ||||
Aaron Ballman | f816787 | 2012-07-19 03:12:23 +0000 | [diff] [blame] | 12294 | if (!TheEnumDecl->isScoped() && |
12295 | Record->getIdentifier() && Record->getIdentifier() == Id) | ||||
Douglas Gregor | a6e937c | 2010-10-15 13:21:21 +0000 | [diff] [blame] | 12296 | Diag(IdLoc, diag::err_member_name_of_class) << Id; |
12297 | |||||
John McCall | 5b629aa | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 12298 | EnumConstantDecl *New = |
12299 | CheckEnumConstant(TheEnumDecl, LastEnumConst, IdLoc, Id, Val); | ||||
Chris Lattner | 421a23d | 2007-08-27 21:16:18 +0000 | [diff] [blame] | 12300 | |
John McCall | 92f8831 | 2010-01-23 00:46:32 +0000 | [diff] [blame] | 12301 | if (New) { |
John McCall | 5b629aa | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 12302 | // Process attributes. |
12303 | if (Attr) ProcessDeclAttributeList(S, New, Attr); | ||||
12304 | |||||
12305 | // Register this decl in the current scope stack. | ||||
John McCall | 92f8831 | 2010-01-23 00:46:32 +0000 | [diff] [blame] | 12306 | New->setAccess(TheEnumDecl->getAccess()); |
Douglas Gregor | 879fd49 | 2009-03-17 19:05:46 +0000 | [diff] [blame] | 12307 | PushOnScopeChains(New, S); |
John McCall | 92f8831 | 2010-01-23 00:46:32 +0000 | [diff] [blame] | 12308 | } |
Douglas Gregor | 45579f5 | 2008-12-17 02:04:30 +0000 | [diff] [blame] | 12309 | |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 12310 | ActOnDocumentableDecl(New); |
12311 | |||||
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 12312 | return New; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 12313 | } |
12314 | |||||
Ted Kremenek | a734a0e | 2012-12-22 01:34:09 +0000 | [diff] [blame] | 12315 | // Returns true when the enum initial expression does not trigger the |
12316 | // duplicate enum warning. A few common cases are exempted as follows: | ||||
12317 | // Element2 = Element1 | ||||
12318 | // Element2 = Element1 + 1 | ||||
12319 | // Element2 = Element1 - 1 | ||||
12320 | // Where Element2 and Element1 are from the same enum. | ||||
12321 | static bool ValidDuplicateEnum(EnumConstantDecl *ECD, EnumDecl *Enum) { | ||||
12322 | Expr *InitExpr = ECD->getInitExpr(); | ||||
12323 | if (!InitExpr) | ||||
12324 | return true; | ||||
12325 | InitExpr = InitExpr->IgnoreImpCasts(); | ||||
12326 | |||||
12327 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(InitExpr)) { | ||||
12328 | if (!BO->isAdditiveOp()) | ||||
12329 | return true; | ||||
12330 | IntegerLiteral *IL = dyn_cast<IntegerLiteral>(BO->getRHS()); | ||||
12331 | if (!IL) | ||||
12332 | return true; | ||||
12333 | if (IL->getValue() != 1) | ||||
12334 | return true; | ||||
12335 | |||||
12336 | InitExpr = BO->getLHS(); | ||||
12337 | } | ||||
12338 | |||||
12339 | // This checks if the elements are from the same enum. | ||||
12340 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(InitExpr); | ||||
12341 | if (!DRE) | ||||
12342 | return true; | ||||
12343 | |||||
12344 | EnumConstantDecl *EnumConstant = dyn_cast<EnumConstantDecl>(DRE->getDecl()); | ||||
12345 | if (!EnumConstant) | ||||
12346 | return true; | ||||
12347 | |||||
12348 | if (cast<EnumDecl>(TagDecl::castFromDeclContext(ECD->getDeclContext())) != | ||||
12349 | Enum) | ||||
12350 | return true; | ||||
12351 | |||||
12352 | return false; | ||||
12353 | } | ||||
12354 | |||||
12355 | struct DupKey { | ||||
12356 | int64_t val; | ||||
12357 | bool isTombstoneOrEmptyKey; | ||||
12358 | DupKey(int64_t val, bool isTombstoneOrEmptyKey) | ||||
12359 | : val(val), isTombstoneOrEmptyKey(isTombstoneOrEmptyKey) {} | ||||
12360 | }; | ||||
12361 | |||||
12362 | static DupKey GetDupKey(const llvm::APSInt& Val) { | ||||
12363 | return DupKey(Val.isSigned() ? Val.getSExtValue() : Val.getZExtValue(), | ||||
12364 | false); | ||||
12365 | } | ||||
12366 | |||||
12367 | struct DenseMapInfoDupKey { | ||||
12368 | static DupKey getEmptyKey() { return DupKey(0, true); } | ||||
12369 | static DupKey getTombstoneKey() { return DupKey(1, true); } | ||||
12370 | static unsigned getHashValue(const DupKey Key) { | ||||
12371 | return (unsigned)(Key.val * 37); | ||||
12372 | } | ||||
12373 | static bool isEqual(const DupKey& LHS, const DupKey& RHS) { | ||||
12374 | return LHS.isTombstoneOrEmptyKey == RHS.isTombstoneOrEmptyKey && | ||||
12375 | LHS.val == RHS.val; | ||||
12376 | } | ||||
12377 | }; | ||||
12378 | |||||
12379 | // Emits a warning when an element is implicitly set a value that | ||||
12380 | // a previous element has already been set to. | ||||
Dmitri Gribenko | 9ff2b42 | 2013-04-27 20:23:52 +0000 | [diff] [blame] | 12381 | static void CheckForDuplicateEnumValues(Sema &S, ArrayRef<Decl *> Elements, |
12382 | EnumDecl *Enum, | ||||
Ted Kremenek | a734a0e | 2012-12-22 01:34:09 +0000 | [diff] [blame] | 12383 | QualType EnumType) { |
12384 | if (S.Diags.getDiagnosticLevel(diag::warn_duplicate_enum_values, | ||||
12385 | Enum->getLocation()) == | ||||
12386 | DiagnosticsEngine::Ignored) | ||||
12387 | return; | ||||
12388 | // Avoid anonymous enums | ||||
12389 | if (!Enum->getIdentifier()) | ||||
12390 | return; | ||||
12391 | |||||
12392 | // Only check for small enums. | ||||
12393 | if (Enum->getNumPositiveBits() > 63 || Enum->getNumNegativeBits() > 64) | ||||
12394 | return; | ||||
12395 | |||||
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 12396 | typedef SmallVector<EnumConstantDecl *, 3> ECDVector; |
12397 | typedef SmallVector<ECDVector *, 3> DuplicatesVector; | ||||
Ted Kremenek | a734a0e | 2012-12-22 01:34:09 +0000 | [diff] [blame] | 12398 | |
12399 | typedef llvm::PointerUnion<EnumConstantDecl*, ECDVector*> DeclOrVector; | ||||
12400 | typedef llvm::DenseMap<DupKey, DeclOrVector, DenseMapInfoDupKey> | ||||
12401 | ValueToVectorMap; | ||||
12402 | |||||
12403 | DuplicatesVector DupVector; | ||||
12404 | ValueToVectorMap EnumMap; | ||||
12405 | |||||
12406 | // Populate the EnumMap with all values represented by enum constants without | ||||
12407 | // an initialier. | ||||
Dmitri Gribenko | 9ff2b42 | 2013-04-27 20:23:52 +0000 | [diff] [blame] | 12408 | for (unsigned i = 0, e = Elements.size(); i != e; ++i) { |
Benjamin Kramer | efac8da | 2013-04-07 14:10:40 +0000 | [diff] [blame] | 12409 | EnumConstantDecl *ECD = cast_or_null<EnumConstantDecl>(Elements[i]); |
Ted Kremenek | a734a0e | 2012-12-22 01:34:09 +0000 | [diff] [blame] | 12410 | |
12411 | // Null EnumConstantDecl means a previous diagnostic has been emitted for | ||||
12412 | // this constant. Skip this enum since it may be ill-formed. | ||||
12413 | if (!ECD) { | ||||
12414 | return; | ||||
12415 | } | ||||
12416 | |||||
12417 | if (ECD->getInitExpr()) | ||||
12418 | continue; | ||||
12419 | |||||
12420 | DupKey Key = GetDupKey(ECD->getInitVal()); | ||||
12421 | DeclOrVector &Entry = EnumMap[Key]; | ||||
12422 | |||||
12423 | // First time encountering this value. | ||||
12424 | if (Entry.isNull()) | ||||
12425 | Entry = ECD; | ||||
12426 | } | ||||
12427 | |||||
12428 | // Create vectors for any values that has duplicates. | ||||
Dmitri Gribenko | 9ff2b42 | 2013-04-27 20:23:52 +0000 | [diff] [blame] | 12429 | for (unsigned i = 0, e = Elements.size(); i != e; ++i) { |
Ted Kremenek | a734a0e | 2012-12-22 01:34:09 +0000 | [diff] [blame] | 12430 | EnumConstantDecl *ECD = cast<EnumConstantDecl>(Elements[i]); |
12431 | if (!ValidDuplicateEnum(ECD, Enum)) | ||||
12432 | continue; | ||||
12433 | |||||
12434 | DupKey Key = GetDupKey(ECD->getInitVal()); | ||||
12435 | |||||
12436 | DeclOrVector& Entry = EnumMap[Key]; | ||||
12437 | if (Entry.isNull()) | ||||
12438 | continue; | ||||
12439 | |||||
12440 | if (EnumConstantDecl *D = Entry.dyn_cast<EnumConstantDecl*>()) { | ||||
12441 | // Ensure constants are different. | ||||
12442 | if (D == ECD) | ||||
12443 | continue; | ||||
12444 | |||||
12445 | // Create new vector and push values onto it. | ||||
12446 | ECDVector *Vec = new ECDVector(); | ||||
12447 | Vec->push_back(D); | ||||
12448 | Vec->push_back(ECD); | ||||
12449 | |||||
12450 | // Update entry to point to the duplicates vector. | ||||
12451 | Entry = Vec; | ||||
12452 | |||||
12453 | // Store the vector somewhere we can consult later for quick emission of | ||||
12454 | // diagnostics. | ||||
12455 | DupVector.push_back(Vec); | ||||
12456 | continue; | ||||
12457 | } | ||||
12458 | |||||
12459 | ECDVector *Vec = Entry.get<ECDVector*>(); | ||||
12460 | // Make sure constants are not added more than once. | ||||
12461 | if (*Vec->begin() == ECD) | ||||
12462 | continue; | ||||
12463 | |||||
12464 | Vec->push_back(ECD); | ||||
12465 | } | ||||
12466 | |||||
12467 | // Emit diagnostics. | ||||
12468 | for (DuplicatesVector::iterator DupVectorIter = DupVector.begin(), | ||||
12469 | DupVectorEnd = DupVector.end(); | ||||
12470 | DupVectorIter != DupVectorEnd; ++DupVectorIter) { | ||||
12471 | ECDVector *Vec = *DupVectorIter; | ||||
12472 | assert(Vec->size() > 1 && "ECDVector should have at least 2 elements."); | ||||
12473 | |||||
12474 | // Emit warning for one enum constant. | ||||
12475 | ECDVector::iterator I = Vec->begin(); | ||||
12476 | S.Diag((*I)->getLocation(), diag::warn_duplicate_enum_values) | ||||
12477 | << (*I)->getName() << (*I)->getInitVal().toString(10) | ||||
12478 | << (*I)->getSourceRange(); | ||||
12479 | ++I; | ||||
12480 | |||||
12481 | // Emit one note for each of the remaining enum constants with | ||||
12482 | // the same value. | ||||
12483 | for (ECDVector::iterator E = Vec->end(); I != E; ++I) | ||||
12484 | S.Diag((*I)->getLocation(), diag::note_duplicate_element) | ||||
12485 | << (*I)->getName() << (*I)->getInitVal().toString(10) | ||||
12486 | << (*I)->getSourceRange(); | ||||
12487 | delete Vec; | ||||
12488 | } | ||||
12489 | } | ||||
12490 | |||||
Mike Stump | c6e35aa | 2009-05-16 07:06:02 +0000 | [diff] [blame] | 12491 | void Sema::ActOnEnumBody(SourceLocation EnumLoc, SourceLocation LBraceLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 12492 | SourceLocation RBraceLoc, Decl *EnumDeclX, |
Dmitri Gribenko | 9ff2b42 | 2013-04-27 20:23:52 +0000 | [diff] [blame] | 12493 | ArrayRef<Decl *> Elements, |
Edward O'Callaghan | fee1381 | 2009-08-08 14:36:57 +0000 | [diff] [blame] | 12494 | Scope *S, AttributeList *Attr) { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 12495 | EnumDecl *Enum = cast<EnumDecl>(EnumDeclX); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 12496 | QualType EnumType = Context.getTypeDeclType(Enum); |
Edward O'Callaghan | fee1381 | 2009-08-08 14:36:57 +0000 | [diff] [blame] | 12497 | |
12498 | if (Attr) | ||||
12499 | ProcessDeclAttributeList(S, Enum, Attr); | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12500 | |
Eli Friedman | ed0716b | 2009-12-11 01:34:50 +0000 | [diff] [blame] | 12501 | if (Enum->isDependentType()) { |
Dmitri Gribenko | 9ff2b42 | 2013-04-27 20:23:52 +0000 | [diff] [blame] | 12502 | for (unsigned i = 0, e = Elements.size(); i != e; ++i) { |
Eli Friedman | ed0716b | 2009-12-11 01:34:50 +0000 | [diff] [blame] | 12503 | EnumConstantDecl *ECD = |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 12504 | cast_or_null<EnumConstantDecl>(Elements[i]); |
Eli Friedman | ed0716b | 2009-12-11 01:34:50 +0000 | [diff] [blame] | 12505 | if (!ECD) continue; |
12506 | |||||
12507 | ECD->setType(EnumType); | ||||
12508 | } | ||||
12509 | |||||
John McCall | 1b5a618 | 2010-05-06 08:49:23 +0000 | [diff] [blame] | 12510 | Enum->completeDefinition(Context.DependentTy, Context.DependentTy, 0, 0); |
Eli Friedman | ed0716b | 2009-12-11 01:34:50 +0000 | [diff] [blame] | 12511 | return; |
12512 | } | ||||
12513 | |||||
Chris Lattner | e37f0be | 2007-08-28 05:10:31 +0000 | [diff] [blame] | 12514 | // TODO: If the result value doesn't fit in an int, it must be a long or long |
12515 | // long value. ISO C does not support this, but GCC does as an extension, | ||||
12516 | // emit a warning. | ||||
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 12517 | unsigned IntWidth = Context.getTargetInfo().getIntWidth(); |
12518 | unsigned CharWidth = Context.getTargetInfo().getCharWidth(); | ||||
12519 | unsigned ShortWidth = Context.getTargetInfo().getShortWidth(); | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12520 | |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 12521 | // Verify that all the values are okay, compute the size of the values, and |
12522 | // reverse the list. | ||||
12523 | unsigned NumNegativeBits = 0; | ||||
12524 | unsigned NumPositiveBits = 0; | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12525 | |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 12526 | // Keep track of whether all elements have type int. |
12527 | bool AllElementsInt = true; | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12528 | |
Dmitri Gribenko | 9ff2b42 | 2013-04-27 20:23:52 +0000 | [diff] [blame] | 12529 | for (unsigned i = 0, e = Elements.size(); i != e; ++i) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 12530 | EnumConstantDecl *ECD = |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 12531 | cast_or_null<EnumConstantDecl>(Elements[i]); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 12532 | if (!ECD) continue; // Already issued a diagnostic. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12533 | |
Chris Lattner | 211a30e | 2007-08-28 05:27:00 +0000 | [diff] [blame] | 12534 | const llvm::APSInt &InitVal = ECD->getInitVal(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12535 | |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 12536 | // Keep track of the size of positive and negative values. |
Chris Lattner | a73349d | 2008-02-26 00:33:57 +0000 | [diff] [blame] | 12537 | if (InitVal.isUnsigned() || InitVal.isNonNegative()) |
Chris Lattner | 21dd821 | 2008-01-14 21:47:29 +0000 | [diff] [blame] | 12538 | NumPositiveBits = std::max(NumPositiveBits, |
12539 | (unsigned)InitVal.getActiveBits()); | ||||
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 12540 | else |
Chris Lattner | 21dd821 | 2008-01-14 21:47:29 +0000 | [diff] [blame] | 12541 | NumNegativeBits = std::max(NumNegativeBits, |
12542 | (unsigned)InitVal.getMinSignedBits()); | ||||
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 12543 | |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 12544 | // Keep track of whether every enum element has type int (very commmon). |
12545 | if (AllElementsInt) | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12546 | AllElementsInt = ECD->getType() == Context.IntTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 12547 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12548 | |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 12549 | // Figure out the type that should be used for this enum. |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 12550 | QualType BestType; |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 12551 | unsigned BestWidth; |
Edward O'Callaghan | fee1381 | 2009-08-08 14:36:57 +0000 | [diff] [blame] | 12552 | |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 12553 | // C++0x N3000 [conv.prom]p3: |
12554 | // An rvalue of an unscoped enumeration type whose underlying | ||||
12555 | // type is not fixed can be converted to an rvalue of the first | ||||
12556 | // of the following types that can represent all the values of | ||||
12557 | // the enumeration: int, unsigned int, long int, unsigned long | ||||
12558 | // int, long long int, or unsigned long long int. | ||||
12559 | // C99 6.4.4.3p2: | ||||
12560 | // An identifier declared as an enumeration constant has type int. | ||||
12561 | // The C99 rule is modified by a gcc extension | ||||
12562 | QualType BestPromotionType; | ||||
12563 | |||||
Edward O'Callaghan | fee1381 | 2009-08-08 14:36:57 +0000 | [diff] [blame] | 12564 | bool Packed = Enum->getAttr<PackedAttr>() ? true : false; |
Argyrios Kyrtzidis | 9a2b9d7 | 2010-10-08 00:25:19 +0000 | [diff] [blame] | 12565 | // -fshort-enums is the equivalent to specifying the packed attribute on all |
12566 | // enum definitions. | ||||
12567 | if (LangOpts.ShortEnums) | ||||
12568 | Packed = true; | ||||
Edward O'Callaghan | fee1381 | 2009-08-08 14:36:57 +0000 | [diff] [blame] | 12569 | |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 12570 | if (Enum->isFixed()) { |
Eli Friedman | 3bfb571 | 2011-10-26 07:38:19 +0000 | [diff] [blame] | 12571 | BestType = Enum->getIntegerType(); |
12572 | if (BestType->isPromotableIntegerType()) | ||||
12573 | BestPromotionType = Context.getPromotedIntegerType(BestType); | ||||
12574 | else | ||||
12575 | BestPromotionType = BestType; | ||||
Duncan Sands | 240a020 | 2010-10-12 14:07:59 +0000 | [diff] [blame] | 12576 | // We don't need to set BestWidth, because BestType is going to be the type |
12577 | // of the enumerators, but we do anyway because otherwise some compilers | ||||
12578 | // warn that it might be used uninitialized. | ||||
12579 | BestWidth = CharWidth; | ||||
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 12580 | } |
12581 | else if (NumNegativeBits) { | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12582 | // 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] | 12583 | // int/long/longlong) that fits. |
Edward O'Callaghan | fee1381 | 2009-08-08 14:36:57 +0000 | [diff] [blame] | 12584 | // If it's packed, check also if it fits a char or a short. |
12585 | if (Packed && NumNegativeBits <= CharWidth && NumPositiveBits < CharWidth) { | ||||
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 12586 | BestType = Context.SignedCharTy; |
12587 | BestWidth = CharWidth; | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12588 | } else if (Packed && NumNegativeBits <= ShortWidth && |
Edward O'Callaghan | fee1381 | 2009-08-08 14:36:57 +0000 | [diff] [blame] | 12589 | NumPositiveBits < ShortWidth) { |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 12590 | BestType = Context.ShortTy; |
12591 | BestWidth = ShortWidth; | ||||
12592 | } else if (NumNegativeBits <= IntWidth && NumPositiveBits < IntWidth) { | ||||
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 12593 | BestType = Context.IntTy; |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 12594 | BestWidth = IntWidth; |
12595 | } else { | ||||
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 12596 | BestWidth = Context.getTargetInfo().getLongWidth(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12597 | |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 12598 | if (NumNegativeBits <= BestWidth && NumPositiveBits < BestWidth) { |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 12599 | BestType = Context.LongTy; |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 12600 | } else { |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 12601 | BestWidth = Context.getTargetInfo().getLongLongWidth(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12602 | |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 12603 | if (NumNegativeBits > BestWidth || NumPositiveBits >= BestWidth) |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 12604 | Diag(Enum->getLocation(), diag::warn_enum_too_large); |
12605 | BestType = Context.LongLongTy; | ||||
12606 | } | ||||
12607 | } | ||||
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 12608 | BestPromotionType = (BestWidth <= IntWidth ? Context.IntTy : BestType); |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 12609 | } else { |
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 12610 | // If there is no negative value, figure out the smallest type that fits |
12611 | // all of the enumerator values. | ||||
Edward O'Callaghan | fee1381 | 2009-08-08 14:36:57 +0000 | [diff] [blame] | 12612 | // If it's packed, check also if it fits a char or a short. |
12613 | if (Packed && NumPositiveBits <= CharWidth) { | ||||
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 12614 | BestType = Context.UnsignedCharTy; |
12615 | BestPromotionType = Context.IntTy; | ||||
12616 | BestWidth = CharWidth; | ||||
Edward O'Callaghan | fee1381 | 2009-08-08 14:36:57 +0000 | [diff] [blame] | 12617 | } else if (Packed && NumPositiveBits <= ShortWidth) { |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 12618 | BestType = Context.UnsignedShortTy; |
12619 | BestPromotionType = Context.IntTy; | ||||
12620 | BestWidth = ShortWidth; | ||||
12621 | } else if (NumPositiveBits <= IntWidth) { | ||||
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 12622 | BestType = Context.UnsignedIntTy; |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 12623 | BestWidth = IntWidth; |
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 12624 | BestPromotionType |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 12625 | = (NumPositiveBits == BestWidth || !getLangOpts().CPlusPlus) |
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 12626 | ? Context.UnsignedIntTy : Context.IntTy; |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 12627 | } else if (NumPositiveBits <= |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 12628 | (BestWidth = Context.getTargetInfo().getLongWidth())) { |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 12629 | BestType = Context.UnsignedLongTy; |
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 12630 | BestPromotionType |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 12631 | = (NumPositiveBits == BestWidth || !getLangOpts().CPlusPlus) |
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 12632 | ? Context.UnsignedLongTy : Context.LongTy; |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 12633 | } else { |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 12634 | BestWidth = Context.getTargetInfo().getLongLongWidth(); |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 12635 | assert(NumPositiveBits <= BestWidth && |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 12636 | "How could an initializer get larger than ULL?"); |
12637 | BestType = Context.UnsignedLongLongTy; | ||||
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 12638 | BestPromotionType |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 12639 | = (NumPositiveBits == BestWidth || !getLangOpts().CPlusPlus) |
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 12640 | ? Context.UnsignedLongLongTy : Context.LongLongTy; |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 12641 | } |
12642 | } | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12643 | |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 12644 | // Loop over all of the enumerator constants, changing their types to match |
12645 | // the type of the enum if needed. | ||||
Dmitri Gribenko | 9ff2b42 | 2013-04-27 20:23:52 +0000 | [diff] [blame] | 12646 | for (unsigned i = 0, e = Elements.size(); i != e; ++i) { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 12647 | EnumConstantDecl *ECD = cast_or_null<EnumConstantDecl>(Elements[i]); |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 12648 | if (!ECD) continue; // Already issued a diagnostic. |
12649 | |||||
12650 | // Standard C says the enumerators have int type, but we allow, as an | ||||
12651 | // extension, the enumerators to be larger than int size. If each | ||||
12652 | // enumerator value fits in an int, type it as an int, otherwise type it the | ||||
12653 | // same as the enumerator decl itself. This means that in "enum { X = 1U }" | ||||
12654 | // that X has type 'int', not 'unsigned'. | ||||
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 12655 | |
12656 | // Determine whether the value fits into an int. | ||||
12657 | llvm::APSInt InitVal = ECD->getInitVal(); | ||||
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 12658 | |
12659 | // If it fits into an integer type, force it. Otherwise force it to match | ||||
12660 | // the enum decl type. | ||||
12661 | QualType NewTy; | ||||
12662 | unsigned NewWidth; | ||||
12663 | bool NewSign; | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 12664 | if (!getLangOpts().CPlusPlus && |
Fariborz Jahanian | 3b25216 | 2011-11-04 18:51:24 +0000 | [diff] [blame] | 12665 | !Enum->isFixed() && |
Douglas Gregor | 677e4fe | 2010-02-01 23:36:03 +0000 | [diff] [blame] | 12666 | isRepresentableIntegerValue(Context, InitVal, Context.IntTy)) { |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 12667 | NewTy = Context.IntTy; |
12668 | NewWidth = IntWidth; | ||||
12669 | NewSign = true; | ||||
12670 | } else if (ECD->getType() == BestType) { | ||||
12671 | // Already the right type! | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 12672 | if (getLangOpts().CPlusPlus) |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 12673 | // C++ [dcl.enum]p4: Following the closing brace of an |
12674 | // enum-specifier, each enumerator has the type of its | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12675 | // enumeration. |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 12676 | ECD->setType(EnumType); |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 12677 | continue; |
12678 | } else { | ||||
12679 | NewTy = BestType; | ||||
12680 | NewWidth = BestWidth; | ||||
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 12681 | NewSign = BestType->isSignedIntegerOrEnumerationType(); |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 12682 | } |
12683 | |||||
12684 | // Adjust the APSInt value. | ||||
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 12685 | InitVal = InitVal.extOrTrunc(NewWidth); |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 12686 | InitVal.setIsSigned(NewSign); |
12687 | ECD->setInitVal(InitVal); | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12688 | |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 12689 | // Adjust the Expr initializer and type. |
Abramo Bagnara | 320e153 | 2010-12-17 15:49:53 +0000 | [diff] [blame] | 12690 | if (ECD->getInitExpr() && |
Nick Lewycky | 25af091 | 2011-07-02 02:05:12 +0000 | [diff] [blame] | 12691 | !Context.hasSameType(NewTy, ECD->getInitExpr()->getType())) |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 12692 | ECD->setInitExpr(ImplicitCastExpr::Create(Context, NewTy, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 12693 | CK_IntegralCast, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 12694 | ECD->getInitExpr(), |
12695 | /*base paths*/ 0, | ||||
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 12696 | VK_RValue)); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 12697 | if (getLangOpts().CPlusPlus) |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 12698 | // C++ [dcl.enum]p4: Following the closing brace of an |
12699 | // enum-specifier, each enumerator has the type of its | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12700 | // enumeration. |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 12701 | ECD->setType(EnumType); |
12702 | else | ||||
12703 | ECD->setType(NewTy); | ||||
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 12704 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12705 | |
John McCall | 1b5a618 | 2010-05-06 08:49:23 +0000 | [diff] [blame] | 12706 | Enum->completeDefinition(BestType, BestPromotionType, |
12707 | NumPositiveBits, NumNegativeBits); | ||||
James Molloy | 16f1f71 | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 12708 | |
12709 | // If we're declaring a function, ensure this decl isn't forgotten about - | ||||
12710 | // it needs to go into the function scope. | ||||
12711 | if (InFunctionDeclarator) | ||||
12712 | DeclsInPrototypeScope.push_back(Enum); | ||||
Ted Kremenek | a734a0e | 2012-12-22 01:34:09 +0000 | [diff] [blame] | 12713 | |
Dmitri Gribenko | 9ff2b42 | 2013-04-27 20:23:52 +0000 | [diff] [blame] | 12714 | CheckForDuplicateEnumValues(*this, Elements, Enum, EnumType); |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 12715 | |
12716 | // Now that the enum type is defined, ensure it's not been underaligned. | ||||
12717 | if (Enum->hasAttrs()) | ||||
12718 | CheckAlignasUnderalignment(Enum); | ||||
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 12719 | } |
12720 | |||||
Abramo Bagnara | 21e006e | 2011-03-03 14:20:18 +0000 | [diff] [blame] | 12721 | Decl *Sema::ActOnFileScopeAsmDecl(Expr *expr, |
12722 | SourceLocation StartLoc, | ||||
12723 | SourceLocation EndLoc) { | ||||
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 12724 | StringLiteral *AsmString = cast<StringLiteral>(expr); |
Sebastian Redl | 798d119 | 2008-12-13 16:23:55 +0000 | [diff] [blame] | 12725 | |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 12726 | FileScopeAsmDecl *New = FileScopeAsmDecl::Create(Context, CurContext, |
Abramo Bagnara | 21e006e | 2011-03-03 14:20:18 +0000 | [diff] [blame] | 12727 | AsmString, StartLoc, |
12728 | EndLoc); | ||||
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 12729 | CurContext->addDecl(New); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 12730 | return New; |
Anders Carlsson | dfab6cb | 2008-02-08 00:33:21 +0000 | [diff] [blame] | 12731 | } |
Eli Friedman | c49f19b | 2009-06-05 02:44:36 +0000 | [diff] [blame] | 12732 | |
Douglas Gregor | 5948ae1 | 2012-01-03 18:04:46 +0000 | [diff] [blame] | 12733 | DeclResult Sema::ActOnModuleImport(SourceLocation AtLoc, |
12734 | SourceLocation ImportLoc, | ||||
12735 | ModuleIdPath Path) { | ||||
Douglas Gregor | 5e35693 | 2011-12-01 17:11:21 +0000 | [diff] [blame] | 12736 | Module *Mod = PP.getModuleLoader().loadModule(ImportLoc, Path, |
Douglas Gregor | 93ebfa6 | 2011-12-02 23:42:12 +0000 | [diff] [blame] | 12737 | Module::AllVisible, |
12738 | /*IsIncludeDirective=*/false); | ||||
Douglas Gregor | 1a4761e | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 12739 | if (!Mod) |
Douglas Gregor | 6aa52ec | 2011-08-26 23:56:07 +0000 | [diff] [blame] | 12740 | return true; |
12741 | |||||
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 12742 | SmallVector<SourceLocation, 2> IdentifierLocs; |
Douglas Gregor | 15de72c | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 12743 | Module *ModCheck = Mod; |
12744 | for (unsigned I = 0, N = Path.size(); I != N; ++I) { | ||||
12745 | // If we've run out of module parents, just drop the remaining identifiers. | ||||
12746 | // We need the length to be consistent. | ||||
12747 | if (!ModCheck) | ||||
12748 | break; | ||||
12749 | ModCheck = ModCheck->Parent; | ||||
12750 | |||||
12751 | IdentifierLocs.push_back(Path[I].second); | ||||
12752 | } | ||||
12753 | |||||
12754 | ImportDecl *Import = ImportDecl::Create(Context, | ||||
12755 | Context.getTranslationUnitDecl(), | ||||
Douglas Gregor | 5948ae1 | 2012-01-03 18:04:46 +0000 | [diff] [blame] | 12756 | AtLoc.isValid()? AtLoc : ImportLoc, |
12757 | Mod, IdentifierLocs); | ||||
Douglas Gregor | 15de72c | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 12758 | Context.getTranslationUnitDecl()->addDecl(Import); |
12759 | return Import; | ||||
Douglas Gregor | 6aa52ec | 2011-08-26 23:56:07 +0000 | [diff] [blame] | 12760 | } |
12761 | |||||
Douglas Gregor | ca2ab45 | 2013-01-12 01:29:50 +0000 | [diff] [blame] | 12762 | void Sema::createImplicitModuleImport(SourceLocation Loc, Module *Mod) { |
12763 | // Create the implicit import declaration. | ||||
12764 | TranslationUnitDecl *TU = getASTContext().getTranslationUnitDecl(); | ||||
12765 | ImportDecl *ImportD = ImportDecl::CreateImplicit(getASTContext(), TU, | ||||
12766 | Loc, Mod, Loc); | ||||
12767 | TU->addDecl(ImportD); | ||||
12768 | Consumer.HandleImplicitImportDecl(ImportD); | ||||
12769 | |||||
12770 | // Make the module visible. | ||||
Douglas Gregor | 906d66a | 2013-03-20 21:10:35 +0000 | [diff] [blame] | 12771 | PP.getModuleLoader().makeModuleVisible(Mod, Module::AllVisible, Loc, |
12772 | /*Complain=*/false); | ||||
Douglas Gregor | ca2ab45 | 2013-01-12 01:29:50 +0000 | [diff] [blame] | 12773 | } |
12774 | |||||
David Chisnall | 5f3c163 | 2012-02-18 16:12:34 +0000 | [diff] [blame] | 12775 | void Sema::ActOnPragmaRedefineExtname(IdentifierInfo* Name, |
12776 | IdentifierInfo* AliasName, | ||||
12777 | SourceLocation PragmaLoc, | ||||
12778 | SourceLocation NameLoc, | ||||
12779 | SourceLocation AliasNameLoc) { | ||||
12780 | Decl *PrevDecl = LookupSingleName(TUScope, Name, NameLoc, | ||||
12781 | LookupOrdinaryName); | ||||
12782 | AsmLabelAttr *Attr = | ||||
12783 | ::new (Context) AsmLabelAttr(AliasNameLoc, Context, AliasName->getName()); | ||||
David Chisnall | 5f3c163 | 2012-02-18 16:12:34 +0000 | [diff] [blame] | 12784 | |
12785 | if (PrevDecl) | ||||
12786 | PrevDecl->addAttr(Attr); | ||||
12787 | else | ||||
12788 | (void)ExtnameUndeclaredIdentifiers.insert( | ||||
12789 | std::pair<IdentifierInfo*,AsmLabelAttr*>(Name, Attr)); | ||||
12790 | } | ||||
12791 | |||||
Eli Friedman | c49f19b | 2009-06-05 02:44:36 +0000 | [diff] [blame] | 12792 | void Sema::ActOnPragmaWeakID(IdentifierInfo* Name, |
12793 | SourceLocation PragmaLoc, | ||||
12794 | SourceLocation NameLoc) { | ||||
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 12795 | Decl *PrevDecl = LookupSingleName(TUScope, Name, NameLoc, LookupOrdinaryName); |
Eli Friedman | c49f19b | 2009-06-05 02:44:36 +0000 | [diff] [blame] | 12796 | |
Eli Friedman | c49f19b | 2009-06-05 02:44:36 +0000 | [diff] [blame] | 12797 | if (PrevDecl) { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 12798 | PrevDecl->addAttr(::new (Context) WeakAttr(PragmaLoc, Context)); |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 12799 | } else { |
12800 | (void)WeakUndeclaredIdentifiers.insert( | ||||
12801 | std::pair<IdentifierInfo*,WeakInfo> | ||||
12802 | (Name, WeakInfo((IdentifierInfo*)0, NameLoc))); | ||||
Eli Friedman | c49f19b | 2009-06-05 02:44:36 +0000 | [diff] [blame] | 12803 | } |
Eli Friedman | c49f19b | 2009-06-05 02:44:36 +0000 | [diff] [blame] | 12804 | } |
12805 | |||||
12806 | void Sema::ActOnPragmaWeakAlias(IdentifierInfo* Name, | ||||
12807 | IdentifierInfo* AliasName, | ||||
12808 | SourceLocation PragmaLoc, | ||||
12809 | SourceLocation NameLoc, | ||||
12810 | SourceLocation AliasNameLoc) { | ||||
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 12811 | Decl *PrevDecl = LookupSingleName(TUScope, AliasName, AliasNameLoc, |
12812 | LookupOrdinaryName); | ||||
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 12813 | WeakInfo W = WeakInfo(Name, NameLoc); |
Eli Friedman | c49f19b | 2009-06-05 02:44:36 +0000 | [diff] [blame] | 12814 | |
Eli Friedman | c49f19b | 2009-06-05 02:44:36 +0000 | [diff] [blame] | 12815 | if (PrevDecl) { |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 12816 | if (!PrevDecl->hasAttr<AliasAttr>()) |
12817 | if (NamedDecl *ND = dyn_cast<NamedDecl>(PrevDecl)) | ||||
Ryan Flynn | 7b1fdbd | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 12818 | DeclApplyPragmaWeak(TUScope, ND, W); |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 12819 | } else { |
12820 | (void)WeakUndeclaredIdentifiers.insert( | ||||
12821 | std::pair<IdentifierInfo*,WeakInfo>(AliasName, W)); | ||||
Eli Friedman | c49f19b | 2009-06-05 02:44:36 +0000 | [diff] [blame] | 12822 | } |
Eli Friedman | c49f19b | 2009-06-05 02:44:36 +0000 | [diff] [blame] | 12823 | } |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 12824 | |
12825 | Decl *Sema::getObjCDeclContext() const { | ||||
12826 | return (dyn_cast_or_null<ObjCContainerDecl>(CurContext)); | ||||
12827 | } | ||||
Argyrios Kyrtzidis | c076e37 | 2011-10-06 23:23:27 +0000 | [diff] [blame] | 12828 | |
12829 | AvailabilityResult Sema::getCurContextAvailability() const { | ||||
Fariborz Jahanian | 3359fa3 | 2012-09-06 18:38:58 +0000 | [diff] [blame] | 12830 | const Decl *D = cast<Decl>(getCurObjCLexicalContext()); |
Argyrios Kyrtzidis | c076e37 | 2011-10-06 23:23:27 +0000 | [diff] [blame] | 12831 | return D->getAvailability(); |
12832 | } |