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" |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 18 | #include "clang/AST/CXXInheritance.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 19 | #include "clang/AST/CharUnits.h" |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 20 | #include "clang/AST/CommentDiagnostic.h" |
John McCall | 384aff8 | 2010-08-25 07:42:41 +0000 | [diff] [blame] | 21 | #include "clang/AST/DeclCXX.h" |
John McCall | 7cd088e | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 22 | #include "clang/AST/DeclObjC.h" |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 23 | #include "clang/AST/DeclTemplate.h" |
Chandler Carruth | a7689ef | 2011-03-27 09:46:56 +0000 | [diff] [blame] | 24 | #include "clang/AST/EvaluatedExprVisitor.h" |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 25 | #include "clang/AST/ExprCXX.h" |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 26 | #include "clang/AST/StmtCXX.h" |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 27 | #include "clang/Basic/PartialDiagnostic.h" |
Steve Naroff | 4c49a6c | 2008-01-30 23:46:05 +0000 | [diff] [blame] | 28 | #include "clang/Basic/SourceManager.h" |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 29 | #include "clang/Basic/TargetInfo.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 30 | #include "clang/Lex/HeaderSearch.h" // FIXME: Sema shouldn't depend on Lex |
31 | #include "clang/Lex/ModuleLoader.h" // FIXME: Sema shouldn't depend on Lex | ||||
32 | #include "clang/Lex/Preprocessor.h" // FIXME: Sema shouldn't depend on Lex | ||||
33 | #include "clang/Parse/ParseDiagnostic.h" | ||||
34 | #include "clang/Sema/CXXFieldCollector.h" | ||||
35 | #include "clang/Sema/DeclSpec.h" | ||||
36 | #include "clang/Sema/DelayedDiagnostic.h" | ||||
37 | #include "clang/Sema/Initialization.h" | ||||
Faisal Vali | ecb5819 | 2013-08-22 01:49:11 +0000 | [diff] [blame] | 38 | #include "clang/Sema/SemaLambda.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 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: | ||||
112 | case tok::kw_decltype: | ||||
113 | return getLangOpts().CPlusPlus; | ||||
114 | |||||
115 | default: | ||||
116 | break; | ||||
117 | } | ||||
118 | |||||
119 | return false; | ||||
120 | } | ||||
121 | |||||
Douglas Gregor | d6efafa | 2009-02-04 19:16:12 +0000 | [diff] [blame] | 122 | /// \brief If the identifier refers to a type name within this scope, |
123 | /// return the declaration of that type. | ||||
124 | /// | ||||
125 | /// This routine performs ordinary name lookup of the identifier II | ||||
126 | /// within the given scope, with optional C++ scope specifier SS, to | ||||
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 127 | /// determine whether the name refers to a type. If so, returns an |
128 | /// opaque pointer (actually a QualType) corresponding to that | ||||
129 | /// type. Otherwise, returns NULL. | ||||
Douglas Gregor | d6efafa | 2009-02-04 19:16:12 +0000 | [diff] [blame] | 130 | /// |
131 | /// If name lookup results in an ambiguity, this routine will complain | ||||
132 | /// and then return NULL. | ||||
Dmitri Gribenko | 8eead16 | 2013-05-03 13:12:11 +0000 | [diff] [blame] | 133 | ParsedType Sema::getTypeName(const IdentifierInfo &II, SourceLocation NameLoc, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 134 | Scope *S, CXXScopeSpec *SS, |
Fariborz Jahanian | 1e52dfc | 2011-02-08 18:05:59 +0000 | [diff] [blame] | 135 | bool isClassName, bool HasTrailingDot, |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 136 | ParsedType ObjectTypePtr, |
Abramo Bagnara | fad03b7 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 137 | bool IsCtorOrDtorName, |
Kaelyn Uhrain | fac9467 | 2011-10-11 01:02:41 +0000 | [diff] [blame] | 138 | bool WantNontrivialTypeSourceInfo, |
139 | IdentifierInfo **CorrectedII) { | ||||
Douglas Gregor | f6e6fc8 | 2009-11-20 22:03:38 +0000 | [diff] [blame] | 140 | // Determine where we will perform name lookup. |
141 | DeclContext *LookupCtx = 0; | ||||
142 | if (ObjectTypePtr) { | ||||
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 143 | QualType ObjectType = ObjectTypePtr.get(); |
Douglas Gregor | f6e6fc8 | 2009-11-20 22:03:38 +0000 | [diff] [blame] | 144 | if (ObjectType->isRecordType()) |
145 | LookupCtx = computeDeclContext(ObjectType); | ||||
Jeffrey Yasskin | edc2877 | 2010-04-07 23:29:58 +0000 | [diff] [blame] | 146 | } else if (SS && SS->isNotEmpty()) { |
Douglas Gregor | f6e6fc8 | 2009-11-20 22:03:38 +0000 | [diff] [blame] | 147 | LookupCtx = computeDeclContext(*SS, false); |
148 | |||||
149 | if (!LookupCtx) { | ||||
150 | if (isDependentScopeSpecifier(*SS)) { | ||||
151 | // C++ [temp.res]p3: | ||||
152 | // A qualified-id that refers to a type and in which the | ||||
153 | // nested-name-specifier depends on a template-parameter (14.6.2) | ||||
154 | // shall be prefixed by the keyword typename to indicate that the | ||||
155 | // qualified-id denotes a type, forming an | ||||
156 | // elaborated-type-specifier (7.1.5.3). | ||||
157 | // | ||||
158 | // We therefore do not perform any name lookup if the result would | ||||
159 | // refer to a member of an unknown specialization. | ||||
Richard Smith | c5a89a1 | 2012-04-02 01:30:27 +0000 | [diff] [blame] | 160 | if (!isClassName && !IsCtorOrDtorName) |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 161 | return ParsedType(); |
Douglas Gregor | f6e6fc8 | 2009-11-20 22:03:38 +0000 | [diff] [blame] | 162 | |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 163 | // We know from the grammar that this name refers to a type, |
164 | // so build a dependent node to describe the type. | ||||
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 165 | if (WantNontrivialTypeSourceInfo) |
166 | return ActOnTypenameType(S, SourceLocation(), *SS, II, NameLoc).get(); | ||||
167 | |||||
168 | NestedNameSpecifierLoc QualifierLoc = SS->getWithLocInContext(Context); | ||||
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 169 | QualType T = |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 170 | CheckTypenameType(ETK_None, SourceLocation(), QualifierLoc, |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 171 | II, NameLoc); |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 172 | |
173 | return ParsedType::make(T); | ||||
Douglas Gregor | f6e6fc8 | 2009-11-20 22:03:38 +0000 | [diff] [blame] | 174 | } |
175 | |||||
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 176 | return ParsedType(); |
Douglas Gregor | f6e6fc8 | 2009-11-20 22:03:38 +0000 | [diff] [blame] | 177 | } |
178 | |||||
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 179 | if (!LookupCtx->isDependentContext() && |
180 | RequireCompleteDeclContext(*SS, LookupCtx)) | ||||
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 181 | return ParsedType(); |
Douglas Gregor | 42c39f3 | 2009-08-26 18:27:52 +0000 | [diff] [blame] | 182 | } |
Eli Friedman | 0f0615b | 2009-12-21 01:42:38 +0000 | [diff] [blame] | 183 | |
184 | // FIXME: LookupNestedNameSpecifierName isn't the right kind of | ||||
185 | // lookup for class-names. | ||||
186 | LookupNameKind Kind = isClassName ? LookupNestedNameSpecifierName : | ||||
187 | LookupOrdinaryName; | ||||
188 | LookupResult Result(*this, &II, NameLoc, Kind); | ||||
Douglas Gregor | f6e6fc8 | 2009-11-20 22:03:38 +0000 | [diff] [blame] | 189 | if (LookupCtx) { |
190 | // Perform "qualified" name lookup into the declaration context we | ||||
191 | // computed, which is either the type of the base of a member access | ||||
192 | // expression or the declaration context associated with a prior | ||||
193 | // nested-name-specifier. | ||||
194 | LookupQualifiedName(Result, LookupCtx); | ||||
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 195 | |
Douglas Gregor | f6e6fc8 | 2009-11-20 22:03:38 +0000 | [diff] [blame] | 196 | if (ObjectTypePtr && Result.empty()) { |
197 | // C++ [basic.lookup.classref]p3: | ||||
198 | // If the unqualified-id is ~type-name, the type-name is looked up | ||||
199 | // in the context of the entire postfix-expression. If the type T of | ||||
200 | // the object expression is of a class type C, the type-name is also | ||||
201 | // looked up in the scope of class C. At least one of the lookups shall | ||||
202 | // find a name that refers to (possibly cv-qualified) T. | ||||
203 | LookupName(Result, S); | ||||
204 | } | ||||
205 | } else { | ||||
206 | // Perform unqualified name lookup. | ||||
207 | LookupName(Result, S); | ||||
208 | } | ||||
209 | |||||
Chris Lattner | 22bd905 | 2009-02-16 22:07:16 +0000 | [diff] [blame] | 210 | NamedDecl *IIDecl = 0; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 211 | switch (Result.getResultKind()) { |
Chris Lattner | 22bd905 | 2009-02-16 22:07:16 +0000 | [diff] [blame] | 212 | case LookupResult::NotFound: |
Douglas Gregor | 7d3f576 | 2010-01-15 01:44:47 +0000 | [diff] [blame] | 213 | case LookupResult::NotFoundInCurrentInstantiation: |
Kaelyn Uhrain | fac9467 | 2011-10-11 01:02:41 +0000 | [diff] [blame] | 214 | if (CorrectedII) { |
Kaelyn Uhrain | c1fb542 | 2012-06-22 23:37:05 +0000 | [diff] [blame] | 215 | TypeNameValidatorCCC Validator(true, isClassName); |
Kaelyn Uhrain | fac9467 | 2011-10-11 01:02:41 +0000 | [diff] [blame] | 216 | TypoCorrection Correction = CorrectTypo(Result.getLookupNameInfo(), |
Kaelyn Uhrain | 16e46dd | 2012-01-31 23:49:25 +0000 | [diff] [blame] | 217 | Kind, S, SS, Validator); |
Kaelyn Uhrain | fac9467 | 2011-10-11 01:02:41 +0000 | [diff] [blame] | 218 | IdentifierInfo *NewII = Correction.getCorrectionAsIdentifierInfo(); |
219 | TemplateTy Template; | ||||
220 | bool MemberOfUnknownSpecialization; | ||||
221 | UnqualifiedId TemplateName; | ||||
222 | TemplateName.setIdentifier(NewII, NameLoc); | ||||
223 | NestedNameSpecifier *NNS = Correction.getCorrectionSpecifier(); | ||||
224 | CXXScopeSpec NewSS, *NewSSPtr = SS; | ||||
225 | if (SS && NNS) { | ||||
226 | NewSS.MakeTrivial(Context, NNS, SourceRange(NameLoc)); | ||||
227 | NewSSPtr = &NewSS; | ||||
228 | } | ||||
229 | if (Correction && (NNS || NewII != &II) && | ||||
230 | // Ignore a correction to a template type as the to-be-corrected | ||||
231 | // identifier is not a template (typo correction for template names | ||||
232 | // is handled elsewhere). | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 233 | !(getLangOpts().CPlusPlus && NewSSPtr && |
Kaelyn Uhrain | fac9467 | 2011-10-11 01:02:41 +0000 | [diff] [blame] | 234 | isTemplateName(S, *NewSSPtr, false, TemplateName, ParsedType(), |
235 | false, Template, MemberOfUnknownSpecialization))) { | ||||
236 | ParsedType Ty = getTypeName(*NewII, NameLoc, S, NewSSPtr, | ||||
237 | isClassName, HasTrailingDot, ObjectTypePtr, | ||||
Abramo Bagnara | fad03b7 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 238 | IsCtorOrDtorName, |
Kaelyn Uhrain | fac9467 | 2011-10-11 01:02:41 +0000 | [diff] [blame] | 239 | WantNontrivialTypeSourceInfo); |
240 | if (Ty) { | ||||
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 241 | diagnoseTypo(Correction, |
242 | PDiag(diag::err_unknown_type_or_class_name_suggest) | ||||
243 | << Result.getLookupName() << isClassName); | ||||
Kaelyn Uhrain | fac9467 | 2011-10-11 01:02:41 +0000 | [diff] [blame] | 244 | if (SS && NNS) |
245 | SS->MakeTrivial(Context, NNS, SourceRange(NameLoc)); | ||||
246 | *CorrectedII = NewII; | ||||
247 | return Ty; | ||||
248 | } | ||||
249 | } | ||||
250 | } | ||||
251 | // If typo correction failed or was not performed, fall through | ||||
Chris Lattner | 22bd905 | 2009-02-16 22:07:16 +0000 | [diff] [blame] | 252 | case LookupResult::FoundOverloaded: |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 253 | case LookupResult::FoundUnresolvedValue: |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 254 | Result.suppressDiagnostics(); |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 255 | return ParsedType(); |
Douglas Gregor | b696ea3 | 2009-02-04 17:00:24 +0000 | [diff] [blame] | 256 | |
Chris Lattner | a64ef0a | 2009-10-25 22:09:09 +0000 | [diff] [blame] | 257 | case LookupResult::Ambiguous: |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 258 | // Recover from type-hiding ambiguities by hiding the type. We'll |
259 | // do the lookup again when looking for an object, and we can | ||||
260 | // diagnose the error then. If we don't do this, then the error | ||||
261 | // about hiding the type will be immediately followed by an error | ||||
262 | // that only makes sense if the identifier was treated like a type. | ||||
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 263 | if (Result.getAmbiguityKind() == LookupResult::AmbiguousTagHiding) { |
264 | Result.suppressDiagnostics(); | ||||
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 265 | return ParsedType(); |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 266 | } |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 267 | |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 268 | // Look to see if we have a type anywhere in the list of results. |
269 | for (LookupResult::iterator Res = Result.begin(), ResEnd = Result.end(); | ||||
270 | Res != ResEnd; ++Res) { | ||||
271 | if (isa<TypeDecl>(*Res) || isa<ObjCInterfaceDecl>(*Res)) { | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 272 | if (!IIDecl || |
273 | (*Res)->getLocation().getRawEncoding() < | ||||
Douglas Gregor | 841b53c | 2009-04-13 15:14:38 +0000 | [diff] [blame] | 274 | IIDecl->getLocation().getRawEncoding()) |
275 | IIDecl = *Res; | ||||
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 276 | } |
277 | } | ||||
278 | |||||
279 | if (!IIDecl) { | ||||
280 | // None of the entities we found is a type, so there is no way | ||||
281 | // to even assume that the result is a type. In this case, don't | ||||
282 | // complain about the ambiguity. The parser will either try to | ||||
283 | // perform this lookup again (e.g., as an object name), which | ||||
284 | // will produce the ambiguity, or will complain that it expected | ||||
285 | // a type name. | ||||
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 286 | Result.suppressDiagnostics(); |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 287 | return ParsedType(); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 288 | } |
289 | |||||
290 | // We found a type within the ambiguous lookup; diagnose the | ||||
291 | // ambiguity and then return that type. This might be the right | ||||
292 | // answer, or it might not be, but it suppresses any attempt to | ||||
293 | // perform the name lookup again. | ||||
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 294 | break; |
Douglas Gregor | b696ea3 | 2009-02-04 17:00:24 +0000 | [diff] [blame] | 295 | |
Chris Lattner | 22bd905 | 2009-02-16 22:07:16 +0000 | [diff] [blame] | 296 | case LookupResult::Found: |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 297 | IIDecl = Result.getFoundDecl(); |
Chris Lattner | 22bd905 | 2009-02-16 22:07:16 +0000 | [diff] [blame] | 298 | break; |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 299 | } |
300 | |||||
Chris Lattner | 10ca337 | 2009-10-25 17:16:46 +0000 | [diff] [blame] | 301 | assert(IIDecl && "Didn't find decl"); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 302 | |
Chris Lattner | 10ca337 | 2009-10-25 17:16:46 +0000 | [diff] [blame] | 303 | QualType T; |
304 | if (TypeDecl *TD = dyn_cast<TypeDecl>(IIDecl)) { | ||||
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 305 | DiagnoseUseOfDecl(IIDecl, NameLoc); |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 306 | |
Chris Lattner | 10ca337 | 2009-10-25 17:16:46 +0000 | [diff] [blame] | 307 | if (T.isNull()) |
308 | T = Context.getTypeDeclType(TD); | ||||
Abramo Bagnara | fad03b7 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 309 | |
310 | // NOTE: avoid constructing an ElaboratedType(Loc) if this is a | ||||
311 | // constructor or destructor name (in such a case, the scope specifier | ||||
312 | // will be attached to the enclosing Expr or Decl node). | ||||
313 | if (SS && SS->isNotEmpty() && !IsCtorOrDtorName) { | ||||
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 314 | if (WantNontrivialTypeSourceInfo) { |
315 | // Construct a type with type-source information. | ||||
316 | TypeLocBuilder Builder; | ||||
317 | Builder.pushTypeSpec(T).setNameLoc(NameLoc); | ||||
318 | |||||
319 | T = getElaboratedType(ETK_None, *SS, T); | ||||
320 | ElaboratedTypeLoc ElabTL = Builder.push<ElaboratedTypeLoc>(T); | ||||
Abramo Bagnara | 38a4291 | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 321 | ElabTL.setElaboratedKeywordLoc(SourceLocation()); |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 322 | ElabTL.setQualifierLoc(SS->getWithLocInContext(Context)); |
323 | return CreateParsedType(T, Builder.getTypeSourceInfo(Context, T)); | ||||
324 | } else { | ||||
325 | T = getElaboratedType(ETK_None, *SS, T); | ||||
326 | } | ||||
327 | } | ||||
Chris Lattner | 10ca337 | 2009-10-25 17:16:46 +0000 | [diff] [blame] | 328 | } else if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(IIDecl)) { |
Fariborz Jahanian | 02b0d65 | 2011-03-08 19:12:46 +0000 | [diff] [blame] | 329 | (void)DiagnoseUseOfDecl(IDecl, NameLoc); |
Fariborz Jahanian | 1e52dfc | 2011-02-08 18:05:59 +0000 | [diff] [blame] | 330 | if (!HasTrailingDot) |
331 | T = Context.getObjCInterfaceType(IDecl); | ||||
332 | } | ||||
333 | |||||
334 | if (T.isNull()) { | ||||
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 335 | // If it's not plausibly a type, suppress diagnostics. |
336 | Result.suppressDiagnostics(); | ||||
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 337 | return ParsedType(); |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 338 | } |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 339 | return ParsedType::make(T); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 340 | } |
341 | |||||
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 342 | /// isTagName() - This method is called *for error recovery purposes only* |
343 | /// to determine if the specified name is a valid tag name ("struct foo"). If | ||||
344 | /// 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] | 345 | /// TST_union, TST_struct, TST_interface, TST_class). This is used to diagnose |
346 | /// cases in C where the user forgot to specify the tag. | ||||
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 347 | DeclSpec::TST Sema::isTagName(IdentifierInfo &II, Scope *S) { |
348 | // Do a tag name lookup in this scope. | ||||
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 349 | LookupResult R(*this, &II, SourceLocation(), LookupTagName); |
350 | LookupName(R, S, false); | ||||
351 | R.suppressDiagnostics(); | ||||
352 | if (R.getResultKind() == LookupResult::Found) | ||||
John McCall | 1bcee0a | 2009-12-02 08:25:40 +0000 | [diff] [blame] | 353 | if (const TagDecl *TD = R.getAsSingle<TagDecl>()) { |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 354 | switch (TD->getTagKind()) { |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 355 | case TTK_Struct: return DeclSpec::TST_struct; |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 356 | case TTK_Interface: return DeclSpec::TST_interface; |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 357 | case TTK_Union: return DeclSpec::TST_union; |
358 | case TTK_Class: return DeclSpec::TST_class; | ||||
359 | case TTK_Enum: return DeclSpec::TST_enum; | ||||
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 360 | } |
361 | } | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 362 | |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 363 | return DeclSpec::TST_unspecified; |
364 | } | ||||
365 | |||||
Francois Pichet | 6943e9b | 2011-04-13 02:38:49 +0000 | [diff] [blame] | 366 | /// isMicrosoftMissingTypename - In Microsoft mode, within class scope, |
367 | /// if a CXXScopeSpec's type is equal to the type of one of the base classes | ||||
368 | /// then downgrade the missing typename error to a warning. | ||||
369 | /// This is needed for MSVC compatibility; Example: | ||||
370 | /// @code | ||||
371 | /// template<class T> class A { | ||||
372 | /// public: | ||||
373 | /// typedef int TYPE; | ||||
374 | /// }; | ||||
375 | /// template<class T> class B : public A<T> { | ||||
376 | /// public: | ||||
377 | /// A<T>::TYPE a; // no typename required because A<T> is a base class. | ||||
378 | /// }; | ||||
379 | /// @endcode | ||||
Francois Pichet | f11dbe9 | 2011-10-11 01:50:09 +0000 | [diff] [blame] | 380 | bool Sema::isMicrosoftMissingTypename(const CXXScopeSpec *SS, Scope *S) { |
Francois Pichet | 6943e9b | 2011-04-13 02:38:49 +0000 | [diff] [blame] | 381 | if (CurContext->isRecord()) { |
Francois Pichet | 3441a52 | 2011-04-13 02:44:57 +0000 | [diff] [blame] | 382 | const Type *Ty = SS->getScopeRep()->getAsType(); |
Francois Pichet | 6943e9b | 2011-04-13 02:38:49 +0000 | [diff] [blame] | 383 | |
384 | CXXRecordDecl *RD = cast<CXXRecordDecl>(CurContext); | ||||
385 | for (CXXRecordDecl::base_class_const_iterator Base = RD->bases_begin(), | ||||
386 | BaseEnd = RD->bases_end(); Base != BaseEnd; ++Base) | ||||
387 | if (Context.hasSameUnqualifiedType(QualType(Ty, 1), Base->getType())) | ||||
388 | return true; | ||||
Francois Pichet | f11dbe9 | 2011-10-11 01:50:09 +0000 | [diff] [blame] | 389 | return S->isFunctionPrototypeScope(); |
Francois Pichet | 6943e9b | 2011-04-13 02:38:49 +0000 | [diff] [blame] | 390 | } |
Francois Pichet | f11dbe9 | 2011-10-11 01:50:09 +0000 | [diff] [blame] | 391 | return CurContext->isFunctionOrMethod() || S->isFunctionPrototypeScope(); |
Francois Pichet | 6943e9b | 2011-04-13 02:38:49 +0000 | [diff] [blame] | 392 | } |
393 | |||||
Kaelyn Uhrain | 50dc12a | 2012-06-15 23:45:58 +0000 | [diff] [blame] | 394 | bool Sema::DiagnoseUnknownTypeName(IdentifierInfo *&II, |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 395 | SourceLocation IILoc, |
396 | Scope *S, | ||||
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 397 | CXXScopeSpec *SS, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 398 | ParsedType &SuggestedType) { |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 399 | // We don't have anything to suggest (yet). |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 400 | SuggestedType = ParsedType(); |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 401 | |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 402 | // There may have been a typo in the name of the type. Look up typo |
403 | // results, in case we have something that we can suggest. | ||||
Kaelyn Uhrain | 43e875d | 2012-01-18 21:41:41 +0000 | [diff] [blame] | 404 | TypeNameValidatorCCC Validator(false); |
Kaelyn Uhrain | 50dc12a | 2012-06-15 23:45:58 +0000 | [diff] [blame] | 405 | if (TypoCorrection Corrected = CorrectTypo(DeclarationNameInfo(II, IILoc), |
Kaelyn Uhrain | 43e875d | 2012-01-18 21:41:41 +0000 | [diff] [blame] | 406 | LookupOrdinaryName, S, SS, |
Kaelyn Uhrain | 16e46dd | 2012-01-31 23:49:25 +0000 | [diff] [blame] | 407 | Validator)) { |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 408 | if (Corrected.isKeyword()) { |
409 | // We corrected to a keyword. | ||||
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 410 | diagnoseTypo(Corrected, PDiag(diag::err_unknown_typename_suggest) << II); |
411 | II = Corrected.getCorrectionAsIdentifierInfo(); | ||||
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 412 | } else { |
Kaelyn Uhrain | 43e875d | 2012-01-18 21:41:41 +0000 | [diff] [blame] | 413 | // We found a similarly-named type or interface; suggest that. |
Kaelyn Uhrain | b2567dd | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 414 | if (!SS || !SS->isSet()) { |
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 415 | diagnoseTypo(Corrected, |
416 | PDiag(diag::err_unknown_typename_suggest) << II); | ||||
Kaelyn Uhrain | b2567dd | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 417 | } else if (DeclContext *DC = computeDeclContext(*SS, false)) { |
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 418 | std::string CorrectedStr(Corrected.getAsString(getLangOpts())); |
419 | bool DroppedSpecifier = Corrected.WillReplaceSpecifier() && | ||||
Kaelyn Uhrain | b2567dd | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 420 | II->getName().equals(CorrectedStr); |
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 421 | diagnoseTypo(Corrected, |
422 | PDiag(diag::err_unknown_nested_typename_suggest) | ||||
423 | << II << DC << DroppedSpecifier << SS->getRange()); | ||||
424 | } else { | ||||
Kaelyn Uhrain | 43e875d | 2012-01-18 21:41:41 +0000 | [diff] [blame] | 425 | llvm_unreachable("could not have corrected a typo here"); |
Kaelyn Uhrain | b2567dd | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 426 | } |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 427 | |
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 428 | SuggestedType = getTypeName(*Corrected.getCorrectionAsIdentifierInfo(), |
429 | IILoc, S, SS, false, false, ParsedType(), | ||||
Abramo Bagnara | fad03b7 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 430 | /*IsCtorOrDtorName=*/false, |
Kaelyn Uhrain | 43e875d | 2012-01-18 21:41:41 +0000 | [diff] [blame] | 431 | /*NonTrivialTypeSourceInfo=*/true); |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 432 | } |
Kaelyn Uhrain | 43e875d | 2012-01-18 21:41:41 +0000 | [diff] [blame] | 433 | return true; |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 434 | } |
435 | |||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 436 | if (getLangOpts().CPlusPlus) { |
Jeffrey Yasskin | c173be2 | 2010-04-08 21:04:54 +0000 | [diff] [blame] | 437 | // See if II is a class template that the user forgot to pass arguments to. |
438 | UnqualifiedId Name; | ||||
Kaelyn Uhrain | 50dc12a | 2012-06-15 23:45:58 +0000 | [diff] [blame] | 439 | Name.setIdentifier(II, IILoc); |
Jeffrey Yasskin | c173be2 | 2010-04-08 21:04:54 +0000 | [diff] [blame] | 440 | CXXScopeSpec EmptySS; |
441 | TemplateTy TemplateResult; | ||||
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 442 | bool MemberOfUnknownSpecialization; |
Abramo Bagnara | 7c15353 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 443 | if (isTemplateName(S, SS ? *SS : EmptySS, /*hasTemplateKeyword=*/false, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 444 | Name, ParsedType(), true, TemplateResult, |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 445 | MemberOfUnknownSpecialization) == TNK_Type_template) { |
Jeffrey Yasskin | c173be2 | 2010-04-08 21:04:54 +0000 | [diff] [blame] | 446 | TemplateName TplName = TemplateResult.getAsVal<TemplateName>(); |
447 | Diag(IILoc, diag::err_template_missing_args) << TplName; | ||||
448 | if (TemplateDecl *TplDecl = TplName.getAsTemplateDecl()) { | ||||
449 | Diag(TplDecl->getLocation(), diag::note_template_decl_here) | ||||
450 | << TplDecl->getTemplateParameters()->getSourceRange(); | ||||
451 | } | ||||
452 | return true; | ||||
453 | } | ||||
454 | } | ||||
455 | |||||
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 456 | // FIXME: Should we move the logic that tries to recover from a missing tag |
457 | // (struct, union, enum) from Parser::ParseImplicitInt here, instead? | ||||
458 | |||||
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 459 | if (!SS || (!SS->isSet() && !SS->isInvalid())) |
Kaelyn Uhrain | 50dc12a | 2012-06-15 23:45:58 +0000 | [diff] [blame] | 460 | Diag(IILoc, diag::err_unknown_typename) << II; |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 461 | else if (DeclContext *DC = computeDeclContext(*SS, false)) |
462 | Diag(IILoc, diag::err_typename_nested_not_found) | ||||
Kaelyn Uhrain | 50dc12a | 2012-06-15 23:45:58 +0000 | [diff] [blame] | 463 | << II << DC << SS->getRange(); |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 464 | else if (isDependentScopeSpecifier(*SS)) { |
Francois Pichet | 6943e9b | 2011-04-13 02:38:49 +0000 | [diff] [blame] | 465 | unsigned DiagID = diag::err_typename_missing; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 466 | if (getLangOpts().MicrosoftMode && isMicrosoftMissingTypename(SS, S)) |
Francois Pichet | cf320c6 | 2011-04-22 08:25:24 +0000 | [diff] [blame] | 467 | DiagID = diag::warn_typename_missing; |
Francois Pichet | 6943e9b | 2011-04-13 02:38:49 +0000 | [diff] [blame] | 468 | |
469 | Diag(SS->getRange().getBegin(), DiagID) | ||||
Kaelyn Uhrain | 50dc12a | 2012-06-15 23:45:58 +0000 | [diff] [blame] | 470 | << (NestedNameSpecifier *)SS->getScopeRep() << II->getName() |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 471 | << SourceRange(SS->getRange().getBegin(), IILoc) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 472 | << FixItHint::CreateInsertion(SS->getRange().getBegin(), "typename "); |
Kaelyn Uhrain | 50dc12a | 2012-06-15 23:45:58 +0000 | [diff] [blame] | 473 | SuggestedType = ActOnTypenameType(S, SourceLocation(), |
474 | *SS, *II, IILoc).get(); | ||||
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 475 | } else { |
476 | assert(SS && SS->isInvalid() && | ||||
477 | "Invalid scope specifier has already been diagnosed"); | ||||
478 | } | ||||
479 | |||||
480 | return true; | ||||
481 | } | ||||
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 482 | |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 483 | /// \brief Determine whether the given result set contains either a type name |
484 | /// or | ||||
485 | static bool isResultTypeOrTemplate(LookupResult &R, const Token &NextToken) { | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 486 | bool CheckTemplate = R.getSema().getLangOpts().CPlusPlus && |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 487 | NextToken.is(tok::less); |
488 | |||||
489 | for (LookupResult::iterator I = R.begin(), IEnd = R.end(); I != IEnd; ++I) { | ||||
490 | if (isa<TypeDecl>(*I) || isa<ObjCInterfaceDecl>(*I)) | ||||
491 | return true; | ||||
492 | |||||
493 | if (CheckTemplate && isa<TemplateDecl>(*I)) | ||||
494 | return true; | ||||
495 | } | ||||
496 | |||||
497 | return false; | ||||
498 | } | ||||
499 | |||||
Kaelyn Uhrain | 12f3297 | 2012-05-02 00:11:40 +0000 | [diff] [blame] | 500 | static bool isTagTypeWithMissingTag(Sema &SemaRef, LookupResult &Result, |
501 | Scope *S, CXXScopeSpec &SS, | ||||
502 | IdentifierInfo *&Name, | ||||
503 | SourceLocation NameLoc) { | ||||
Richard Smith | 69e4826 | 2012-09-06 01:37:56 +0000 | [diff] [blame] | 504 | LookupResult R(SemaRef, Name, NameLoc, Sema::LookupTagName); |
505 | SemaRef.LookupParsedName(R, S, &SS); | ||||
506 | if (TagDecl *Tag = R.getAsSingle<TagDecl>()) { | ||||
Kaelyn Uhrain | 12f3297 | 2012-05-02 00:11:40 +0000 | [diff] [blame] | 507 | const char *TagName = 0; |
508 | const char *FixItTagName = 0; | ||||
509 | switch (Tag->getTagKind()) { | ||||
510 | case TTK_Class: | ||||
511 | TagName = "class"; | ||||
512 | FixItTagName = "class "; | ||||
513 | break; | ||||
514 | |||||
515 | case TTK_Enum: | ||||
516 | TagName = "enum"; | ||||
517 | FixItTagName = "enum "; | ||||
518 | break; | ||||
519 | |||||
520 | case TTK_Struct: | ||||
521 | TagName = "struct"; | ||||
522 | FixItTagName = "struct "; | ||||
523 | break; | ||||
524 | |||||
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 525 | case TTK_Interface: |
526 | TagName = "__interface"; | ||||
527 | FixItTagName = "__interface "; | ||||
528 | break; | ||||
529 | |||||
Kaelyn Uhrain | 12f3297 | 2012-05-02 00:11:40 +0000 | [diff] [blame] | 530 | case TTK_Union: |
531 | TagName = "union"; | ||||
532 | FixItTagName = "union "; | ||||
533 | break; | ||||
534 | } | ||||
535 | |||||
536 | SemaRef.Diag(NameLoc, diag::err_use_of_tag_name_without_tag) | ||||
537 | << Name << TagName << SemaRef.getLangOpts().CPlusPlus | ||||
538 | << FixItHint::CreateInsertion(NameLoc, FixItTagName); | ||||
539 | |||||
Richard Smith | 69e4826 | 2012-09-06 01:37:56 +0000 | [diff] [blame] | 540 | for (LookupResult::iterator I = Result.begin(), IEnd = Result.end(); |
541 | I != IEnd; ++I) | ||||
542 | SemaRef.Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type) | ||||
543 | << Name << TagName; | ||||
544 | |||||
545 | // Replace lookup results with just the tag decl. | ||||
546 | Result.clear(Sema::LookupTagName); | ||||
547 | SemaRef.LookupParsedName(Result, S, &SS); | ||||
Kaelyn Uhrain | 12f3297 | 2012-05-02 00:11:40 +0000 | [diff] [blame] | 548 | return true; |
549 | } | ||||
550 | |||||
Kaelyn Uhrain | 12f3297 | 2012-05-02 00:11:40 +0000 | [diff] [blame] | 551 | return false; |
552 | } | ||||
553 | |||||
Richard Smith | 0576681 | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 554 | /// Build a ParsedType for a simple-type-specifier with a nested-name-specifier. |
555 | static ParsedType buildNestedType(Sema &S, CXXScopeSpec &SS, | ||||
556 | QualType T, SourceLocation NameLoc) { | ||||
557 | ASTContext &Context = S.Context; | ||||
558 | |||||
559 | TypeLocBuilder Builder; | ||||
560 | Builder.pushTypeSpec(T).setNameLoc(NameLoc); | ||||
561 | |||||
562 | T = S.getElaboratedType(ETK_None, SS, T); | ||||
563 | ElaboratedTypeLoc ElabTL = Builder.push<ElaboratedTypeLoc>(T); | ||||
564 | ElabTL.setElaboratedKeywordLoc(SourceLocation()); | ||||
565 | ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); | ||||
566 | return S.CreateParsedType(T, Builder.getTypeSourceInfo(Context, T)); | ||||
567 | } | ||||
568 | |||||
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 569 | Sema::NameClassification Sema::ClassifyName(Scope *S, |
570 | CXXScopeSpec &SS, | ||||
571 | IdentifierInfo *&Name, | ||||
572 | SourceLocation NameLoc, | ||||
Richard Smith | 0576681 | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 573 | const Token &NextToken, |
574 | bool IsAddressOfOperand, | ||||
575 | CorrectionCandidateCallback *CCC) { | ||||
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 576 | DeclarationNameInfo NameInfo(Name, NameLoc); |
577 | ObjCMethodDecl *CurMethod = getCurMethodDecl(); | ||||
578 | |||||
579 | if (NextToken.is(tok::coloncolon)) { | ||||
580 | BuildCXXNestedNameSpecifier(S, *Name, NameLoc, NextToken.getLocation(), | ||||
581 | QualType(), false, SS, 0, false); | ||||
582 | |||||
583 | } | ||||
584 | |||||
585 | LookupResult Result(*this, Name, NameLoc, LookupOrdinaryName); | ||||
586 | LookupParsedName(Result, S, &SS, !CurMethod); | ||||
587 | |||||
588 | // Perform lookup for Objective-C instance variables (including automatically | ||||
589 | // synthesized instance variables), if we're in an Objective-C method. | ||||
590 | // FIXME: This lookup really, really needs to be folded in to the normal | ||||
591 | // unqualified lookup mechanism. | ||||
592 | if (!SS.isSet() && CurMethod && !isResultTypeOrTemplate(Result, NextToken)) { | ||||
593 | ExprResult E = LookupInObjCMethod(Result, S, Name, true); | ||||
Douglas Gregor | ec385cf | 2011-04-25 15:05:41 +0000 | [diff] [blame] | 594 | if (E.get() || E.isInvalid()) |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 595 | return E; |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 596 | } |
597 | |||||
598 | bool SecondTry = false; | ||||
599 | bool IsFilteredTemplateName = false; | ||||
600 | |||||
601 | Corrected: | ||||
602 | switch (Result.getResultKind()) { | ||||
603 | case LookupResult::NotFound: | ||||
604 | // If an unqualified-id is followed by a '(', then we have a function | ||||
605 | // call. | ||||
606 | if (!SS.isSet() && NextToken.is(tok::l_paren)) { | ||||
607 | // In C++, this is an ADL-only call. | ||||
608 | // FIXME: Reference? | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 609 | if (getLangOpts().CPlusPlus) |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 610 | return BuildDeclarationNameExpr(SS, Result, /*ADL=*/true); |
611 | |||||
612 | // C90 6.3.2.2: | ||||
613 | // If the expression that precedes the parenthesized argument list in a | ||||
614 | // function call consists solely of an identifier, and if no | ||||
615 | // declaration is visible for this identifier, the identifier is | ||||
616 | // implicitly declared exactly as if, in the innermost block containing | ||||
617 | // the function call, the declaration | ||||
618 | // | ||||
619 | // extern int identifier (); | ||||
620 | // | ||||
621 | // appeared. | ||||
622 | // | ||||
623 | // We also allow this in C99 as an extension. | ||||
624 | if (NamedDecl *D = ImplicitlyDefineFunction(NameLoc, *Name, S)) { | ||||
625 | Result.addDecl(D); | ||||
626 | Result.resolveKind(); | ||||
627 | return BuildDeclarationNameExpr(SS, Result, /*ADL=*/false); | ||||
628 | } | ||||
629 | } | ||||
630 | |||||
631 | // In C, we first see whether there is a tag type by the same name, in | ||||
632 | // which case it's likely that the user just forget to write "enum", | ||||
633 | // "struct", or "union". | ||||
Kaelyn Uhrain | 12f3297 | 2012-05-02 00:11:40 +0000 | [diff] [blame] | 634 | if (!getLangOpts().CPlusPlus && !SecondTry && |
635 | isTagTypeWithMissingTag(*this, Result, S, SS, Name, NameLoc)) { | ||||
636 | break; | ||||
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 637 | } |
638 | |||||
639 | // Perform typo correction to determine if there is another name that is | ||||
640 | // close to this name. | ||||
Richard Smith | 0576681 | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 641 | if (!SecondTry && CCC) { |
Douglas Gregor | 3a348c8 | 2011-07-14 04:54:23 +0000 | [diff] [blame] | 642 | SecondTry = true; |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 643 | if (TypoCorrection Corrected = CorrectTypo(Result.getLookupNameInfo(), |
David Blaikie | d662a79 | 2011-10-19 22:56:21 +0000 | [diff] [blame] | 644 | Result.getLookupKind(), S, |
Richard Smith | 0576681 | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 645 | &SS, *CCC)) { |
Douglas Gregor | 27766d2 | 2011-04-27 03:47:06 +0000 | [diff] [blame] | 646 | unsigned UnqualifiedDiag = diag::err_undeclared_var_use_suggest; |
647 | unsigned QualifiedDiag = diag::err_no_member_suggest; | ||||
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 648 | |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 649 | NamedDecl *FirstDecl = Corrected.getCorrectionDecl(); |
Douglas Gregor | 3b88735 | 2011-04-27 04:48:22 +0000 | [diff] [blame] | 650 | NamedDecl *UnderlyingFirstDecl |
651 | = FirstDecl? FirstDecl->getUnderlyingDecl() : 0; | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 652 | if (getLangOpts().CPlusPlus && NextToken.is(tok::less) && |
Douglas Gregor | 3b88735 | 2011-04-27 04:48:22 +0000 | [diff] [blame] | 653 | UnderlyingFirstDecl && isa<TemplateDecl>(UnderlyingFirstDecl)) { |
Douglas Gregor | 27766d2 | 2011-04-27 03:47:06 +0000 | [diff] [blame] | 654 | UnqualifiedDiag = diag::err_no_template_suggest; |
655 | QualifiedDiag = diag::err_no_member_template_suggest; | ||||
Douglas Gregor | 3b88735 | 2011-04-27 04:48:22 +0000 | [diff] [blame] | 656 | } else if (UnderlyingFirstDecl && |
657 | (isa<TypeDecl>(UnderlyingFirstDecl) || | ||||
658 | isa<ObjCInterfaceDecl>(UnderlyingFirstDecl) || | ||||
659 | isa<ObjCCompatibleAliasDecl>(UnderlyingFirstDecl))) { | ||||
David Blaikie | 30262b7 | 2013-03-21 21:35:15 +0000 | [diff] [blame] | 660 | UnqualifiedDiag = diag::err_unknown_typename_suggest; |
661 | QualifiedDiag = diag::err_unknown_nested_typename_suggest; | ||||
662 | } | ||||
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 663 | |
Kaelyn Uhrain | b2567dd | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 664 | if (SS.isEmpty()) { |
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 665 | diagnoseTypo(Corrected, PDiag(UnqualifiedDiag) << Name); |
Kaelyn Uhrain | b2567dd | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 666 | } else {// FIXME: is this even reachable? Test it. |
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 667 | std::string CorrectedStr(Corrected.getAsString(getLangOpts())); |
668 | bool DroppedSpecifier = Corrected.WillReplaceSpecifier() && | ||||
Kaelyn Uhrain | b2567dd | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 669 | Name->getName().equals(CorrectedStr); |
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 670 | diagnoseTypo(Corrected, PDiag(QualifiedDiag) |
671 | << Name << computeDeclContext(SS, false) | ||||
672 | << DroppedSpecifier << SS.getRange()); | ||||
Kaelyn Uhrain | b2567dd | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 673 | } |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 674 | |
675 | // Update the name, so that the caller has the new name. | ||||
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 676 | Name = Corrected.getCorrectionAsIdentifierInfo(); |
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 677 | |
Kaelyn Uhrain | a5ee634 | 2012-01-24 19:45:35 +0000 | [diff] [blame] | 678 | // Typo correction corrected to a keyword. |
679 | if (Corrected.isKeyword()) | ||||
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 680 | return Name; |
Kaelyn Uhrain | a5ee634 | 2012-01-24 19:45:35 +0000 | [diff] [blame] | 681 | |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 682 | // Also update the LookupResult... |
683 | // FIXME: This should probably go away at some point | ||||
684 | Result.clear(); | ||||
685 | Result.setLookupName(Corrected.getCorrection()); | ||||
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 686 | if (FirstDecl) |
Kaelyn Uhrain | a5ee634 | 2012-01-24 19:45:35 +0000 | [diff] [blame] | 687 | Result.addDecl(FirstDecl); |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 688 | |
689 | // If we found an Objective-C instance variable, let | ||||
690 | // LookupInObjCMethod build the appropriate expression to | ||||
691 | // reference the ivar. | ||||
692 | // FIXME: This is a gross hack. | ||||
693 | if (ObjCIvarDecl *Ivar = Result.getAsSingle<ObjCIvarDecl>()) { | ||||
694 | Result.clear(); | ||||
695 | ExprResult E(LookupInObjCMethod(Result, S, Ivar->getIdentifier())); | ||||
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 696 | return E; |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 697 | } |
698 | |||||
699 | goto Corrected; | ||||
700 | } | ||||
701 | } | ||||
702 | |||||
703 | // We failed to correct; just fall through and let the parser deal with it. | ||||
704 | Result.suppressDiagnostics(); | ||||
705 | return NameClassification::Unknown(); | ||||
706 | |||||
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 707 | case LookupResult::NotFoundInCurrentInstantiation: { |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 708 | // We performed name lookup into the current instantiation, and there were |
709 | // dependent bases, so we treat this result the same way as any other | ||||
710 | // dependent nested-name-specifier. | ||||
711 | |||||
712 | // C++ [temp.res]p2: | ||||
713 | // A name used in a template declaration or definition and that is | ||||
714 | // dependent on a template-parameter is assumed not to name a type | ||||
715 | // unless the applicable name lookup finds a type name or the name is | ||||
716 | // qualified by the keyword typename. | ||||
717 | // | ||||
718 | // FIXME: If the next token is '<', we might want to ask the parser to | ||||
719 | // perform some heroics to see if we actually have a | ||||
720 | // template-argument-list, which would indicate a missing 'template' | ||||
721 | // keyword here. | ||||
Richard Smith | 0576681 | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 722 | return ActOnDependentIdExpression(SS, /*TemplateKWLoc=*/SourceLocation(), |
723 | NameInfo, IsAddressOfOperand, | ||||
724 | /*TemplateArgs=*/0); | ||||
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 725 | } |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 726 | |
727 | case LookupResult::Found: | ||||
728 | case LookupResult::FoundOverloaded: | ||||
729 | case LookupResult::FoundUnresolvedValue: | ||||
730 | break; | ||||
731 | |||||
732 | case LookupResult::Ambiguous: | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 733 | if (getLangOpts().CPlusPlus && NextToken.is(tok::less) && |
Douglas Gregor | 3b88735 | 2011-04-27 04:48:22 +0000 | [diff] [blame] | 734 | hasAnyAcceptableTemplateNames(Result)) { |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 735 | // C++ [temp.local]p3: |
736 | // A lookup that finds an injected-class-name (10.2) can result in an | ||||
737 | // ambiguity in certain cases (for example, if it is found in more than | ||||
738 | // one base class). If all of the injected-class-names that are found | ||||
739 | // refer to specializations of the same class template, and if the name | ||||
740 | // is followed by a template-argument-list, the reference refers to the | ||||
741 | // class template itself and not a specialization thereof, and is not | ||||
742 | // ambiguous. | ||||
743 | // | ||||
744 | // This filtering can make an ambiguous result into an unambiguous one, | ||||
745 | // so try again after filtering out template names. | ||||
746 | FilterAcceptableTemplateNames(Result); | ||||
747 | if (!Result.isAmbiguous()) { | ||||
748 | IsFilteredTemplateName = true; | ||||
749 | break; | ||||
750 | } | ||||
751 | } | ||||
752 | |||||
753 | // Diagnose the ambiguity and return an error. | ||||
754 | return NameClassification::Error(); | ||||
755 | } | ||||
756 | |||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 757 | if (getLangOpts().CPlusPlus && NextToken.is(tok::less) && |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 758 | (IsFilteredTemplateName || hasAnyAcceptableTemplateNames(Result))) { |
759 | // C++ [temp.names]p3: | ||||
760 | // After name lookup (3.4) finds that a name is a template-name or that | ||||
761 | // an operator-function-id or a literal- operator-id refers to a set of | ||||
762 | // overloaded functions any member of which is a function template if | ||||
763 | // this is followed by a <, the < is always taken as the delimiter of a | ||||
764 | // template-argument-list and never as the less-than operator. | ||||
765 | if (!IsFilteredTemplateName) | ||||
766 | FilterAcceptableTemplateNames(Result); | ||||
767 | |||||
Douglas Gregor | 3b88735 | 2011-04-27 04:48:22 +0000 | [diff] [blame] | 768 | if (!Result.empty()) { |
769 | bool IsFunctionTemplate; | ||||
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 770 | bool IsVarTemplate; |
Douglas Gregor | 3b88735 | 2011-04-27 04:48:22 +0000 | [diff] [blame] | 771 | TemplateName Template; |
772 | if (Result.end() - Result.begin() > 1) { | ||||
773 | IsFunctionTemplate = true; | ||||
774 | Template = Context.getOverloadedTemplateName(Result.begin(), | ||||
775 | Result.end()); | ||||
776 | } else { | ||||
777 | TemplateDecl *TD | ||||
778 | = cast<TemplateDecl>((*Result.begin())->getUnderlyingDecl()); | ||||
779 | IsFunctionTemplate = isa<FunctionTemplateDecl>(TD); | ||||
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 780 | IsVarTemplate = isa<VarTemplateDecl>(TD); |
781 | |||||
Douglas Gregor | 3b88735 | 2011-04-27 04:48:22 +0000 | [diff] [blame] | 782 | if (SS.isSet() && !SS.isInvalid()) |
783 | Template = Context.getQualifiedTemplateName(SS.getScopeRep(), | ||||
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 784 | /*TemplateKeyword=*/false, |
Douglas Gregor | 3b88735 | 2011-04-27 04:48:22 +0000 | [diff] [blame] | 785 | TD); |
786 | else | ||||
787 | Template = TemplateName(TD); | ||||
788 | } | ||||
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 789 | |
Douglas Gregor | 3b88735 | 2011-04-27 04:48:22 +0000 | [diff] [blame] | 790 | if (IsFunctionTemplate) { |
791 | // Function templates always go through overload resolution, at which | ||||
792 | // point we'll perform the various checks (e.g., accessibility) we need | ||||
793 | // to based on which function we selected. | ||||
794 | Result.suppressDiagnostics(); | ||||
795 | |||||
796 | return NameClassification::FunctionTemplate(Template); | ||||
797 | } | ||||
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 798 | |
799 | return IsVarTemplate ? NameClassification::VarTemplate(Template) | ||||
800 | : NameClassification::TypeTemplate(Template); | ||||
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 801 | } |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 802 | } |
Richard Smith | 0576681 | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 803 | |
Douglas Gregor | 3b88735 | 2011-04-27 04:48:22 +0000 | [diff] [blame] | 804 | NamedDecl *FirstDecl = (*Result.begin())->getUnderlyingDecl(); |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 805 | if (TypeDecl *Type = dyn_cast<TypeDecl>(FirstDecl)) { |
806 | DiagnoseUseOfDecl(Type, NameLoc); | ||||
807 | QualType T = Context.getTypeDeclType(Type); | ||||
Richard Smith | 0576681 | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 808 | if (SS.isNotEmpty()) |
809 | return buildNestedType(*this, SS, T, NameLoc); | ||||
Kaelyn Uhrain | 12f3297 | 2012-05-02 00:11:40 +0000 | [diff] [blame] | 810 | return ParsedType::make(T); |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 811 | } |
Richard Smith | 0576681 | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 812 | |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 813 | ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(FirstDecl); |
814 | if (!Class) { | ||||
815 | // FIXME: It's unfortunate that we don't have a Type node for handling this. | ||||
816 | if (ObjCCompatibleAliasDecl *Alias | ||||
817 | = dyn_cast<ObjCCompatibleAliasDecl>(FirstDecl)) | ||||
818 | Class = Alias->getClassInterface(); | ||||
819 | } | ||||
820 | |||||
821 | if (Class) { | ||||
822 | DiagnoseUseOfDecl(Class, NameLoc); | ||||
823 | |||||
824 | if (NextToken.is(tok::period)) { | ||||
825 | // Interface. <something> is parsed as a property reference expression. | ||||
826 | // Just return "unknown" as a fall-through for now. | ||||
827 | Result.suppressDiagnostics(); | ||||
828 | return NameClassification::Unknown(); | ||||
829 | } | ||||
830 | |||||
831 | QualType T = Context.getObjCInterfaceType(Class); | ||||
832 | return ParsedType::make(T); | ||||
833 | } | ||||
Kaelyn Uhrain | 12f3297 | 2012-05-02 00:11:40 +0000 | [diff] [blame] | 834 | |
Richard Smith | 0576681 | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 835 | // We can have a type template here if we're classifying a template argument. |
836 | if (isa<TemplateDecl>(FirstDecl) && !isa<FunctionTemplateDecl>(FirstDecl)) | ||||
837 | return NameClassification::TypeTemplate( | ||||
838 | TemplateName(cast<TemplateDecl>(FirstDecl))); | ||||
839 | |||||
Kaelyn Uhrain | 12f3297 | 2012-05-02 00:11:40 +0000 | [diff] [blame] | 840 | // Check for a tag type hidden by a non-type decl in a few cases where it |
841 | // 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] | 842 | bool NextIsOp = NextToken.is(tok::amp) || NextToken.is(tok::star); |
843 | if ((NextToken.is(tok::identifier) || | ||||
844 | (NextIsOp && FirstDecl->isFunctionOrFunctionTemplate())) && | ||||
845 | isTagTypeWithMissingTag(*this, Result, S, SS, Name, NameLoc)) { | ||||
846 | TypeDecl *Type = Result.getAsSingle<TypeDecl>(); | ||||
847 | DiagnoseUseOfDecl(Type, NameLoc); | ||||
848 | QualType T = Context.getTypeDeclType(Type); | ||||
849 | if (SS.isNotEmpty()) | ||||
850 | return buildNestedType(*this, SS, T, NameLoc); | ||||
851 | return ParsedType::make(T); | ||||
Kaelyn Uhrain | 12f3297 | 2012-05-02 00:11:40 +0000 | [diff] [blame] | 852 | } |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 853 | |
Richard Smith | 0576681 | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 854 | if (FirstDecl->isCXXClassMember()) |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 855 | return BuildPossibleImplicitMemberExpr(SS, SourceLocation(), Result, 0); |
Douglas Gregor | 3b88735 | 2011-04-27 04:48:22 +0000 | [diff] [blame] | 856 | |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 857 | bool ADL = UseArgumentDependentLookup(SS, Result, NextToken.is(tok::l_paren)); |
858 | return BuildDeclarationNameExpr(SS, Result, ADL); | ||||
859 | } | ||||
860 | |||||
John McCall | 88232aa | 2009-08-18 00:00:49 +0000 | [diff] [blame] | 861 | // Determines the context to return to after temporarily entering a |
862 | // context. This depends in an unnecessarily complicated way on the | ||||
863 | // exact ordering of callbacks from the parser. | ||||
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 864 | DeclContext *Sema::getContainingDC(DeclContext *DC) { |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 865 | |
John McCall | 88232aa | 2009-08-18 00:00:49 +0000 | [diff] [blame] | 866 | // Functions defined inline within classes aren't parsed until we've |
867 | // finished parsing the top-level class, so the top-level class is | ||||
868 | // the context we'll need to return to. | ||||
869 | if (isa<FunctionDecl>(DC)) { | ||||
870 | DC = DC->getLexicalParent(); | ||||
871 | |||||
872 | // A function not defined within a class will always return to its | ||||
873 | // lexical context. | ||||
874 | if (!isa<CXXRecordDecl>(DC)) | ||||
875 | return DC; | ||||
876 | |||||
877 | // A C++ inline method/friend is parsed *after* the topmost class | ||||
878 | // it was declared in is fully parsed ("complete"); the topmost | ||||
879 | // class is the context we need to return to. | ||||
Argyrios Kyrtzidis | 77407b8 | 2008-11-19 18:01:13 +0000 | [diff] [blame] | 880 | while (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(DC->getLexicalParent())) |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 881 | DC = RD; |
882 | |||||
883 | // Return the declaration context of the topmost class the inline method is | ||||
884 | // declared in. | ||||
885 | return DC; | ||||
886 | } | ||||
887 | |||||
Argyrios Kyrtzidis | 77407b8 | 2008-11-19 18:01:13 +0000 | [diff] [blame] | 888 | return DC->getLexicalParent(); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 889 | } |
890 | |||||
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 891 | void Sema::PushDeclContext(Scope *S, DeclContext *DC) { |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 892 | assert(getContainingDC(DC) == CurContext && |
Zhongxing Xu | e50897a | 2008-12-08 07:14:51 +0000 | [diff] [blame] | 893 | "The next DeclContext should be lexically contained in the current one."); |
Chris Lattner | 9fdf9c6 | 2008-04-22 18:39:57 +0000 | [diff] [blame] | 894 | CurContext = DC; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 895 | S->setEntity(DC); |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 896 | } |
897 | |||||
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 898 | void Sema::PopDeclContext() { |
899 | assert(CurContext && "DeclContext imbalance!"); | ||||
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 900 | |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 901 | CurContext = getContainingDC(CurContext); |
John McCall | acb7039 | 2010-07-23 22:45:07 +0000 | [diff] [blame] | 902 | assert(CurContext && "Popped translation unit!"); |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 903 | } |
904 | |||||
Argyrios Kyrtzidis | 179fe1a | 2009-06-17 23:19:02 +0000 | [diff] [blame] | 905 | /// EnterDeclaratorContext - Used when we must lookup names in the context |
906 | /// of a declarator's nested name specifier. | ||||
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 907 | /// |
Argyrios Kyrtzidis | 1d17553 | 2009-06-17 23:15:40 +0000 | [diff] [blame] | 908 | void Sema::EnterDeclaratorContext(Scope *S, DeclContext *DC) { |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 909 | // C++0x [basic.lookup.unqual]p13: |
910 | // A name used in the definition of a static data member of class | ||||
911 | // X (after the qualified-id of the static member) is looked up as | ||||
912 | // if the name was used in a member function of X. | ||||
913 | // C++0x [basic.lookup.unqual]p14: | ||||
914 | // If a variable member of a namespace is defined outside of the | ||||
915 | // scope of its namespace then any name used in the definition of | ||||
916 | // the variable member (after the declarator-id) is looked up as | ||||
917 | // if the definition of the variable member occurred in its | ||||
918 | // namespace. | ||||
919 | // Both of these imply that we should push a scope whose context | ||||
920 | // is the semantic context of the declaration. We can't use | ||||
921 | // PushDeclContext here because that context is not necessarily | ||||
922 | // lexically contained in the current context. Fortunately, | ||||
923 | // the containing scope should have the appropriate information. | ||||
924 | |||||
925 | assert(!S->getEntity() && "scope already has entity"); | ||||
926 | |||||
927 | #ifndef NDEBUG | ||||
928 | Scope *Ancestor = S->getParent(); | ||||
929 | while (!Ancestor->getEntity()) Ancestor = Ancestor->getParent(); | ||||
930 | assert(Ancestor->getEntity() == CurContext && "ancestor context mismatch"); | ||||
931 | #endif | ||||
932 | |||||
Argyrios Kyrtzidis | 1d17553 | 2009-06-17 23:15:40 +0000 | [diff] [blame] | 933 | CurContext = DC; |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 934 | S->setEntity(DC); |
Argyrios Kyrtzidis | 1d17553 | 2009-06-17 23:15:40 +0000 | [diff] [blame] | 935 | } |
936 | |||||
Argyrios Kyrtzidis | 1d17553 | 2009-06-17 23:15:40 +0000 | [diff] [blame] | 937 | void Sema::ExitDeclaratorContext(Scope *S) { |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 938 | assert(S->getEntity() == CurContext && "Context imbalance!"); |
Argyrios Kyrtzidis | 1d17553 | 2009-06-17 23:15:40 +0000 | [diff] [blame] | 939 | |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 940 | // Switch back to the lexical context. The safety of this is |
941 | // enforced by an assert in EnterDeclaratorContext. | ||||
942 | Scope *Ancestor = S->getParent(); | ||||
943 | while (!Ancestor->getEntity()) Ancestor = Ancestor->getParent(); | ||||
944 | CurContext = (DeclContext*) Ancestor->getEntity(); | ||||
945 | |||||
946 | // We don't need to do anything with the scope, which is going to | ||||
947 | // disappear. | ||||
Argyrios Kyrtzidis | 1d17553 | 2009-06-17 23:15:40 +0000 | [diff] [blame] | 948 | } |
949 | |||||
Caitlin Sadowski | ed9d84a | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 950 | |
951 | void Sema::ActOnReenterFunctionContext(Scope* S, Decl *D) { | ||||
952 | FunctionDecl *FD = dyn_cast<FunctionDecl>(D); | ||||
953 | if (FunctionTemplateDecl *TFD = dyn_cast_or_null<FunctionTemplateDecl>(D)) { | ||||
954 | // We assume that the caller has already called | ||||
955 | // ActOnReenterTemplateScope | ||||
956 | FD = TFD->getTemplatedDecl(); | ||||
957 | } | ||||
958 | if (!FD) | ||||
959 | return; | ||||
960 | |||||
DeLesley Hutchins | cf2fa2f | 2012-04-06 15:10:17 +0000 | [diff] [blame] | 961 | // Same implementation as PushDeclContext, but enters the context |
962 | // from the lexical parent, rather than the top-level class. | ||||
963 | assert(CurContext == FD->getLexicalParent() && | ||||
964 | "The next DeclContext should be lexically contained in the current one."); | ||||
965 | CurContext = FD; | ||||
966 | S->setEntity(CurContext); | ||||
967 | |||||
Caitlin Sadowski | ed9d84a | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 968 | for (unsigned P = 0, NumParams = FD->getNumParams(); P < NumParams; ++P) { |
969 | ParmVarDecl *Param = FD->getParamDecl(P); | ||||
970 | // If the parameter has an identifier, then add it to the scope | ||||
971 | if (Param->getIdentifier()) { | ||||
972 | S->AddDecl(Param); | ||||
973 | IdResolver.AddDecl(Param); | ||||
974 | } | ||||
975 | } | ||||
976 | } | ||||
977 | |||||
978 | |||||
DeLesley Hutchins | cf2fa2f | 2012-04-06 15:10:17 +0000 | [diff] [blame] | 979 | void Sema::ActOnExitFunctionContext() { |
980 | // Same implementation as PopDeclContext, but returns to the lexical parent, | ||||
981 | // rather than the top-level class. | ||||
982 | assert(CurContext && "DeclContext imbalance!"); | ||||
983 | CurContext = CurContext->getLexicalParent(); | ||||
984 | assert(CurContext && "Popped translation unit!"); | ||||
985 | } | ||||
986 | |||||
987 | |||||
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 988 | /// \brief Determine whether we allow overloading of the function |
989 | /// PrevDecl with another declaration. | ||||
990 | /// | ||||
991 | /// This routine determines whether overloading is possible, not | ||||
992 | /// whether some new function is actually an overload. It will return | ||||
993 | /// true in C++ (where we can always provide overloads) or, as an | ||||
994 | /// extension, in C when the previous function is already an | ||||
995 | /// overloaded function declaration or has the "overloadable" | ||||
996 | /// attribute. | ||||
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 997 | static bool AllowOverloadingOfFunction(LookupResult &Previous, |
998 | ASTContext &Context) { | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 999 | if (Context.getLangOpts().CPlusPlus) |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1000 | return true; |
1001 | |||||
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1002 | if (Previous.getResultKind() == LookupResult::FoundOverloaded) |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1003 | return true; |
1004 | |||||
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1005 | return (Previous.getResultKind() == LookupResult::Found |
1006 | && Previous.getFoundDecl()->hasAttr<OverloadableAttr>()); | ||||
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1007 | } |
1008 | |||||
Argyrios Kyrtzidis | 87f3ff0 | 2008-04-12 00:47:19 +0000 | [diff] [blame] | 1009 | /// Add this decl to the scope shadowed decl chains. |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 1010 | void Sema::PushOnScopeChains(NamedDecl *D, Scope *S, bool AddToContext) { |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 1011 | // Move up the scope chain until we find the nearest enclosing |
1012 | // non-transparent context. The declaration will be introduced into this | ||||
1013 | // scope. | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1014 | while (S->getEntity() && |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 1015 | ((DeclContext *)S->getEntity())->isTransparentContext()) |
1016 | S = S->getParent(); | ||||
1017 | |||||
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 1018 | // Add scoped declarations into their context, so that they can be |
1019 | // found later. Declarations without a context won't be inserted | ||||
1020 | // into any context. | ||||
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 1021 | if (AddToContext) |
1022 | CurContext->addDecl(D); | ||||
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 1023 | |
Chandler Carruth | 8761d68 | 2010-02-21 07:08:09 +0000 | [diff] [blame] | 1024 | // Out-of-line definitions shouldn't be pushed into scope in C++. |
1025 | // Out-of-line variable and function definitions shouldn't even in C. | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1026 | if ((getLangOpts().CPlusPlus || isa<VarDecl>(D) || isa<FunctionDecl>(D)) && |
Douglas Gregor | 6d0468b | 2011-10-09 22:57:49 +0000 | [diff] [blame] | 1027 | D->isOutOfLine() && |
1028 | !D->getDeclContext()->getRedeclContext()->Equals( | ||||
1029 | D->getLexicalDeclContext()->getRedeclContext())) | ||||
Chandler Carruth | 8761d68 | 2010-02-21 07:08:09 +0000 | [diff] [blame] | 1030 | return; |
1031 | |||||
1032 | // Template instantiations should also not be pushed into scope. | ||||
1033 | if (isa<FunctionDecl>(D) && | ||||
1034 | cast<FunctionDecl>(D)->isFunctionTemplateSpecialization()) | ||||
Douglas Gregor | d04b1be | 2009-09-28 18:41:37 +0000 | [diff] [blame] | 1035 | return; |
1036 | |||||
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1037 | // If this replaces anything in the current scope, |
1038 | IdentifierResolver::iterator I = IdResolver.begin(D->getDeclName()), | ||||
1039 | IEnd = IdResolver.end(); | ||||
1040 | for (; I != IEnd; ++I) { | ||||
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1041 | if (S->isDeclScope(*I) && D->declarationReplaces(*I)) { |
1042 | S->RemoveDecl(*I); | ||||
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1043 | IdResolver.RemoveDecl(*I); |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 1044 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1045 | // Should only need to replace one decl. |
1046 | break; | ||||
Douglas Gregor | 516ff43 | 2009-04-24 02:57:34 +0000 | [diff] [blame] | 1047 | } |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 1048 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1049 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1050 | S->AddDecl(D); |
Douglas Gregor | 7cbc558 | 2011-03-14 21:19:51 +0000 | [diff] [blame] | 1051 | |
1052 | if (isa<LabelDecl>(D) && !cast<LabelDecl>(D)->isGnuLocal()) { | ||||
1053 | // Implicitly-generated labels may end up getting generated in an order that | ||||
1054 | // isn't strictly lexical, which breaks name lookup. Be careful to insert | ||||
1055 | // the label at the appropriate place in the identifier chain. | ||||
1056 | for (I = IdResolver.begin(D->getDeclName()); I != IEnd; ++I) { | ||||
Douglas Gregor | 1d2de76 | 2011-03-24 14:35:16 +0000 | [diff] [blame] | 1057 | DeclContext *IDC = (*I)->getLexicalDeclContext()->getRedeclContext(); |
Douglas Gregor | 250e7a7 | 2011-03-16 16:39:03 +0000 | [diff] [blame] | 1058 | if (IDC == CurContext) { |
1059 | if (!S->isDeclScope(*I)) | ||||
1060 | continue; | ||||
1061 | } else if (IDC->Encloses(CurContext)) | ||||
Douglas Gregor | 7cbc558 | 2011-03-14 21:19:51 +0000 | [diff] [blame] | 1062 | break; |
1063 | } | ||||
1064 | |||||
Douglas Gregor | 250e7a7 | 2011-03-16 16:39:03 +0000 | [diff] [blame] | 1065 | IdResolver.InsertDeclAfter(I, D); |
Douglas Gregor | 7cbc558 | 2011-03-14 21:19:51 +0000 | [diff] [blame] | 1066 | } else { |
1067 | IdResolver.AddDecl(D); | ||||
1068 | } | ||||
Argyrios Kyrtzidis | 87f3ff0 | 2008-04-12 00:47:19 +0000 | [diff] [blame] | 1069 | } |
1070 | |||||
Douglas Gregor | eee242f | 2011-10-27 09:33:13 +0000 | [diff] [blame] | 1071 | void Sema::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) { |
1072 | if (IdResolver.tryAddTopLevelDecl(D, Name) && TUScope) | ||||
1073 | TUScope->AddDecl(D); | ||||
1074 | } | ||||
1075 | |||||
Richard Smith | dd9459f | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 1076 | bool Sema::isDeclInScope(NamedDecl *D, DeclContext *Ctx, Scope *S, |
Douglas Gregor | cc20945 | 2011-03-07 16:54:27 +0000 | [diff] [blame] | 1077 | bool ExplicitInstantiationOrSpecialization) { |
Nico Weber | 355a166 | 2012-12-17 03:51:09 +0000 | [diff] [blame] | 1078 | return IdResolver.isDeclInScope(D, Ctx, S, |
Douglas Gregor | cc20945 | 2011-03-07 16:54:27 +0000 | [diff] [blame] | 1079 | ExplicitInstantiationOrSpecialization); |
Douglas Gregor | 2531c2d | 2009-09-28 00:47:05 +0000 | [diff] [blame] | 1080 | } |
1081 | |||||
John McCall | 5f1e094 | 2010-08-24 08:50:51 +0000 | [diff] [blame] | 1082 | Scope *Sema::getScopeForDeclContext(Scope *S, DeclContext *DC) { |
1083 | DeclContext *TargetDC = DC->getPrimaryContext(); | ||||
1084 | do { | ||||
1085 | if (DeclContext *ScopeDC = (DeclContext*) S->getEntity()) | ||||
1086 | if (ScopeDC->getPrimaryContext() == TargetDC) | ||||
1087 | return S; | ||||
1088 | } while ((S = S->getParent())); | ||||
1089 | |||||
1090 | return 0; | ||||
1091 | } | ||||
1092 | |||||
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1093 | static bool isOutOfScopePreviousDeclaration(NamedDecl *, |
1094 | DeclContext*, | ||||
1095 | ASTContext&); | ||||
1096 | |||||
1097 | /// Filters out lookup results that don't fall within the given scope | ||||
1098 | /// as determined by isDeclInScope. | ||||
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1099 | void Sema::FilterLookupForScope(LookupResult &R, |
1100 | DeclContext *Ctx, Scope *S, | ||||
1101 | bool ConsiderLinkage, | ||||
1102 | bool ExplicitInstantiationOrSpecialization) { | ||||
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1103 | LookupResult::Filter F = R.makeFilter(); |
1104 | while (F.hasNext()) { | ||||
1105 | NamedDecl *D = F.next(); | ||||
1106 | |||||
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1107 | if (isDeclInScope(D, Ctx, S, ExplicitInstantiationOrSpecialization)) |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1108 | continue; |
1109 | |||||
1110 | if (ConsiderLinkage && | ||||
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1111 | isOutOfScopePreviousDeclaration(D, Ctx, Context)) |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1112 | continue; |
1113 | |||||
1114 | F.erase(); | ||||
1115 | } | ||||
1116 | |||||
1117 | F.done(); | ||||
1118 | } | ||||
1119 | |||||
1120 | static bool isUsingDecl(NamedDecl *D) { | ||||
1121 | return isa<UsingShadowDecl>(D) || | ||||
1122 | isa<UnresolvedUsingTypenameDecl>(D) || | ||||
1123 | isa<UnresolvedUsingValueDecl>(D); | ||||
1124 | } | ||||
1125 | |||||
1126 | /// Removes using shadow declarations from the lookup results. | ||||
1127 | static void RemoveUsingDecls(LookupResult &R) { | ||||
1128 | LookupResult::Filter F = R.makeFilter(); | ||||
1129 | while (F.hasNext()) | ||||
1130 | if (isUsingDecl(F.next())) | ||||
1131 | F.erase(); | ||||
1132 | |||||
1133 | F.done(); | ||||
1134 | } | ||||
1135 | |||||
Argyrios Kyrtzidis | 06999f8 | 2010-08-15 10:17:33 +0000 | [diff] [blame] | 1136 | /// \brief Check for this common pattern: |
1137 | /// @code | ||||
1138 | /// class S { | ||||
1139 | /// S(const S&); // DO NOT IMPLEMENT | ||||
1140 | /// void operator=(const S&); // DO NOT IMPLEMENT | ||||
1141 | /// }; | ||||
1142 | /// @endcode | ||||
1143 | static bool IsDisallowedCopyOrAssign(const CXXMethodDecl *D) { | ||||
1144 | // FIXME: Should check for private access too but access is set after we get | ||||
1145 | // the decl here. | ||||
Sean Hunt | 10620eb | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 1146 | if (D->doesThisDeclarationHaveABody()) |
Argyrios Kyrtzidis | 06999f8 | 2010-08-15 10:17:33 +0000 | [diff] [blame] | 1147 | return false; |
1148 | |||||
1149 | if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(D)) | ||||
1150 | return CD->isCopyConstructor(); | ||||
Douglas Gregor | 27c08ab | 2010-09-27 22:06:20 +0000 | [diff] [blame] | 1151 | if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) |
1152 | return Method->isCopyAssignmentOperator(); | ||||
1153 | return false; | ||||
Argyrios Kyrtzidis | 06999f8 | 2010-08-15 10:17:33 +0000 | [diff] [blame] | 1154 | } |
1155 | |||||
Rafael Espindola | 2d1b096 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 1156 | // We need this to handle |
1157 | // | ||||
1158 | // typedef struct { | ||||
1159 | // void *foo() { return 0; } | ||||
1160 | // } A; | ||||
1161 | // | ||||
1162 | // When we see foo we don't know if after the typedef we will get 'A' or '*A' | ||||
1163 | // for example. If 'A', foo will have external linkage. If we have '*A', | ||||
1164 | // foo will have no linkage. Since we can't know untill we get to the end | ||||
1165 | // of the typedef, this function finds out if D might have non external linkage. | ||||
1166 | // Callers should verify at the end of the TU if it D has external linkage or | ||||
1167 | // not. | ||||
1168 | bool Sema::mightHaveNonExternalLinkage(const DeclaratorDecl *D) { | ||||
1169 | const DeclContext *DC = D->getDeclContext(); | ||||
1170 | while (!DC->isTranslationUnit()) { | ||||
1171 | if (const RecordDecl *RD = dyn_cast<RecordDecl>(DC)){ | ||||
1172 | if (!RD->hasNameForLinkage()) | ||||
1173 | return true; | ||||
1174 | } | ||||
1175 | DC = DC->getParent(); | ||||
1176 | } | ||||
1177 | |||||
Rafael Espindola | 181e3ec | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 1178 | return !D->isExternallyVisible(); |
Rafael Espindola | 2d1b096 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 1179 | } |
1180 | |||||
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 1181 | bool Sema::ShouldWarnIfUnusedFileScopedDecl(const DeclaratorDecl *D) const { |
1182 | assert(D); | ||||
Argyrios Kyrtzidis | f6d1d43 | 2010-08-13 18:42:29 +0000 | [diff] [blame] | 1183 | |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 1184 | if (D->isInvalidDecl() || D->isUsed() || D->hasAttr<UnusedAttr>()) |
1185 | return false; | ||||
1186 | |||||
1187 | // Ignore class templates. | ||||
Chandler Carruth | ef9d09c | 2011-01-03 19:27:19 +0000 | [diff] [blame] | 1188 | if (D->getDeclContext()->isDependentContext() || |
1189 | D->getLexicalDeclContext()->isDependentContext()) | ||||
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 1190 | return false; |
1191 | |||||
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 1192 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Argyrios Kyrtzidis | 06999f8 | 2010-08-15 10:17:33 +0000 | [diff] [blame] | 1193 | if (FD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation) |
1194 | return false; | ||||
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 1195 | |
Argyrios Kyrtzidis | 06999f8 | 2010-08-15 10:17:33 +0000 | [diff] [blame] | 1196 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) { |
1197 | if (MD->isVirtual() || IsDisallowedCopyOrAssign(MD)) | ||||
1198 | return false; | ||||
1199 | } else { | ||||
1200 | // 'static inline' functions are used in headers; don't warn. | ||||
Rafael Espindola | 6ca6752 | 2013-04-16 15:21:30 +0000 | [diff] [blame] | 1201 | // Make sure we get the storage class from the canonical declaration, |
1202 | // since otherwise we will get spurious warnings on specialized | ||||
1203 | // static template functions. | ||||
1204 | if (FD->getCanonicalDecl()->getStorageClass() == SC_Static && | ||||
Argyrios Kyrtzidis | 06999f8 | 2010-08-15 10:17:33 +0000 | [diff] [blame] | 1205 | FD->isInlineSpecified()) |
1206 | return false; | ||||
1207 | } | ||||
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 1208 | |
Sean Hunt | 10620eb | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 1209 | if (FD->doesThisDeclarationHaveABody() && |
John McCall | 82b9659 | 2010-10-27 01:41:35 +0000 | [diff] [blame] | 1210 | Context.DeclMustBeEmitted(FD)) |
1211 | return false; | ||||
John McCall | 82b9659 | 2010-10-27 01:41:35 +0000 | [diff] [blame] | 1212 | } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
Richard Smith | c36e359 | 2012-10-28 07:39:29 +0000 | [diff] [blame] | 1213 | // Don't warn on variables of const-qualified or reference type, since their |
1214 | // values can be used even if though they're not odr-used, and because const | ||||
1215 | // qualified variables can appear in headers in contexts where they're not | ||||
1216 | // intended to be used. | ||||
1217 | // FIXME: Use more principled rules for these exemptions. | ||||
John McCall | 82b9659 | 2010-10-27 01:41:35 +0000 | [diff] [blame] | 1218 | if (!VD->isFileVarDecl() || |
Richard Smith | c36e359 | 2012-10-28 07:39:29 +0000 | [diff] [blame] | 1219 | VD->getType().isConstQualified() || |
1220 | VD->getType()->isReferenceType() || | ||||
John McCall | 82b9659 | 2010-10-27 01:41:35 +0000 | [diff] [blame] | 1221 | Context.DeclMustBeEmitted(VD)) |
1222 | return false; | ||||
1223 | |||||
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 1224 | if (VD->isStaticDataMember() && |
1225 | VD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation) | ||||
1226 | return false; | ||||
1227 | |||||
John McCall | 82b9659 | 2010-10-27 01:41:35 +0000 | [diff] [blame] | 1228 | } else { |
1229 | return false; | ||||
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 1230 | } |
1231 | |||||
John McCall | 82b9659 | 2010-10-27 01:41:35 +0000 | [diff] [blame] | 1232 | // Only warn for unused decls internal to the translation unit. |
Rafael Espindola | 2d1b096 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 1233 | return mightHaveNonExternalLinkage(D); |
John McCall | 82b9659 | 2010-10-27 01:41:35 +0000 | [diff] [blame] | 1234 | } |
1235 | |||||
1236 | void Sema::MarkUnusedFileScopedDecl(const DeclaratorDecl *D) { | ||||
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 1237 | if (!D) |
1238 | return; | ||||
1239 | |||||
1240 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { | ||||
1241 | const FunctionDecl *First = FD->getFirstDeclaration(); | ||||
1242 | if (FD != First && ShouldWarnIfUnusedFileScopedDecl(First)) | ||||
1243 | return; // First should already be in the vector. | ||||
1244 | } | ||||
1245 | |||||
1246 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { | ||||
1247 | const VarDecl *First = VD->getFirstDeclaration(); | ||||
1248 | if (VD != First && ShouldWarnIfUnusedFileScopedDecl(First)) | ||||
1249 | return; // First should already be in the vector. | ||||
1250 | } | ||||
1251 | |||||
David Blaikie | 7f7c42b | 2012-05-26 05:35:39 +0000 | [diff] [blame] | 1252 | if (ShouldWarnIfUnusedFileScopedDecl(D)) |
1253 | UnusedFileScopedDecls.push_back(D); | ||||
1254 | } | ||||
Argyrios Kyrtzidis | 49b96d1 | 2010-08-13 18:42:17 +0000 | [diff] [blame] | 1255 | |
Anders Carlsson | 99a000e | 2009-11-07 07:18:14 +0000 | [diff] [blame] | 1256 | static bool ShouldDiagnoseUnusedDecl(const NamedDecl *D) { |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 1257 | if (D->isInvalidDecl()) |
1258 | return false; | ||||
1259 | |||||
Eli Friedman | dd9d645 | 2012-01-13 23:41:25 +0000 | [diff] [blame] | 1260 | if (D->isReferenced() || D->isUsed() || D->hasAttr<UnusedAttr>()) |
Anders Carlsson | f7613d5 | 2009-11-07 07:26:56 +0000 | [diff] [blame] | 1261 | return false; |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 1262 | |
Chris Lattner | 57ad378 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 1263 | if (isa<LabelDecl>(D)) |
1264 | return true; | ||||
1265 | |||||
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 1266 | // White-list anything that isn't a local variable. |
1267 | if (!isa<VarDecl>(D) || isa<ParmVarDecl>(D) || isa<ImplicitParamDecl>(D) || | ||||
1268 | !D->getDeclContext()->isFunctionOrMethod()) | ||||
1269 | return false; | ||||
1270 | |||||
1271 | // Types of valid local variables should be complete, so this should succeed. | ||||
Rafael Espindola | 1a5d355 | 2012-01-06 04:54:01 +0000 | [diff] [blame] | 1272 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
John McCall | aec5860 | 2010-03-31 02:47:45 +0000 | [diff] [blame] | 1273 | |
1274 | // White-list anything with an __attribute__((unused)) type. | ||||
1275 | QualType Ty = VD->getType(); | ||||
1276 | |||||
1277 | // Only look at the outermost level of typedef. | ||||
Douglas Gregor | 2c8e81e | 2012-09-14 05:10:40 +0000 | [diff] [blame] | 1278 | if (const TypedefType *TT = Ty->getAs<TypedefType>()) { |
John McCall | aec5860 | 2010-03-31 02:47:45 +0000 | [diff] [blame] | 1279 | if (TT->getDecl()->hasAttr<UnusedAttr>()) |
1280 | return false; | ||||
1281 | } | ||||
1282 | |||||
Douglas Gregor | 5764f61 | 2010-05-08 23:05:03 +0000 | [diff] [blame] | 1283 | // If we failed to complete the type for some reason, or if the type is |
1284 | // dependent, don't diagnose the variable. | ||||
1285 | if (Ty->isIncompleteType() || Ty->isDependentType()) | ||||
Douglas Gregor | a6a292b | 2010-04-27 16:20:13 +0000 | [diff] [blame] | 1286 | return false; |
1287 | |||||
John McCall | aec5860 | 2010-03-31 02:47:45 +0000 | [diff] [blame] | 1288 | if (const TagType *TT = Ty->getAs<TagType>()) { |
1289 | const TagDecl *Tag = TT->getDecl(); | ||||
1290 | if (Tag->hasAttr<UnusedAttr>()) | ||||
1291 | return false; | ||||
1292 | |||||
1293 | if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Tag)) { | ||||
Lubos Lunak | 1d3ce65 | 2013-07-20 15:05:36 +0000 | [diff] [blame] | 1294 | if (!RD->hasTrivialDestructor() && !RD->hasAttr<WarnUnusedAttr>()) |
Anders Carlsson | f7613d5 | 2009-11-07 07:26:56 +0000 | [diff] [blame] | 1295 | return false; |
Rafael Espindola | 1a5d355 | 2012-01-06 04:54:01 +0000 | [diff] [blame] | 1296 | |
1297 | if (const Expr *Init = VD->getInit()) { | ||||
David Blaikie | 39e1776 | 2012-10-24 21:29:06 +0000 | [diff] [blame] | 1298 | if (const ExprWithCleanups *Cleanups = dyn_cast<ExprWithCleanups>(Init)) |
1299 | Init = Cleanups->getSubExpr(); | ||||
Rafael Espindola | 1a5d355 | 2012-01-06 04:54:01 +0000 | [diff] [blame] | 1300 | const CXXConstructExpr *Construct = |
1301 | dyn_cast<CXXConstructExpr>(Init); | ||||
1302 | if (Construct && !Construct->isElidable()) { | ||||
1303 | CXXConstructorDecl *CD = Construct->getConstructor(); | ||||
Lubos Lunak | 1d3ce65 | 2013-07-20 15:05:36 +0000 | [diff] [blame] | 1304 | if (!CD->isTrivial() && !RD->hasAttr<WarnUnusedAttr>()) |
Rafael Espindola | 1a5d355 | 2012-01-06 04:54:01 +0000 | [diff] [blame] | 1305 | return false; |
1306 | } | ||||
1307 | } | ||||
Anders Carlsson | f7613d5 | 2009-11-07 07:26:56 +0000 | [diff] [blame] | 1308 | } |
1309 | } | ||||
John McCall | aec5860 | 2010-03-31 02:47:45 +0000 | [diff] [blame] | 1310 | |
1311 | // TODO: __attribute__((unused)) templates? | ||||
Anders Carlsson | f7613d5 | 2009-11-07 07:26:56 +0000 | [diff] [blame] | 1312 | } |
1313 | |||||
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 1314 | return true; |
Anders Carlsson | 99a000e | 2009-11-07 07:18:14 +0000 | [diff] [blame] | 1315 | } |
1316 | |||||
Anna Zaks | d5612a2 | 2011-07-28 20:52:06 +0000 | [diff] [blame] | 1317 | static void GenerateFixForUnusedDecl(const NamedDecl *D, ASTContext &Ctx, |
1318 | FixItHint &Hint) { | ||||
1319 | if (isa<LabelDecl>(D)) { | ||||
1320 | SourceLocation AfterColon = Lexer::findLocationAfterToken(D->getLocEnd(), | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1321 | tok::colon, Ctx.getSourceManager(), Ctx.getLangOpts(), true); |
Anna Zaks | d5612a2 | 2011-07-28 20:52:06 +0000 | [diff] [blame] | 1322 | if (AfterColon.isInvalid()) |
1323 | return; | ||||
1324 | Hint = FixItHint::CreateRemoval(CharSourceRange:: | ||||
1325 | getCharRange(D->getLocStart(), AfterColon)); | ||||
1326 | } | ||||
1327 | return; | ||||
1328 | } | ||||
1329 | |||||
Chris Lattner | 337e550 | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1330 | /// DiagnoseUnusedDecl - Emit warnings about declarations that are not used |
1331 | /// unless they are marked attr(unused). | ||||
Douglas Gregor | 5764f61 | 2010-05-08 23:05:03 +0000 | [diff] [blame] | 1332 | void Sema::DiagnoseUnusedDecl(const NamedDecl *D) { |
Anna Zaks | d5612a2 | 2011-07-28 20:52:06 +0000 | [diff] [blame] | 1333 | FixItHint Hint; |
Douglas Gregor | 5764f61 | 2010-05-08 23:05:03 +0000 | [diff] [blame] | 1334 | if (!ShouldDiagnoseUnusedDecl(D)) |
1335 | return; | ||||
1336 | |||||
Anna Zaks | d5612a2 | 2011-07-28 20:52:06 +0000 | [diff] [blame] | 1337 | GenerateFixForUnusedDecl(D, Context, Hint); |
1338 | |||||
Chris Lattner | 57ad378 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 1339 | unsigned DiagID; |
Douglas Gregor | 5764f61 | 2010-05-08 23:05:03 +0000 | [diff] [blame] | 1340 | if (isa<VarDecl>(D) && cast<VarDecl>(D)->isExceptionVariable()) |
Chris Lattner | 57ad378 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 1341 | DiagID = diag::warn_unused_exception_param; |
1342 | else if (isa<LabelDecl>(D)) | ||||
1343 | DiagID = diag::warn_unused_label; | ||||
Douglas Gregor | 5764f61 | 2010-05-08 23:05:03 +0000 | [diff] [blame] | 1344 | else |
Chris Lattner | 57ad378 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 1345 | DiagID = diag::warn_unused_variable; |
1346 | |||||
Anna Zaks | d5612a2 | 2011-07-28 20:52:06 +0000 | [diff] [blame] | 1347 | Diag(D->getLocation(), DiagID) << D->getDeclName() << Hint; |
Douglas Gregor | 5764f61 | 2010-05-08 23:05:03 +0000 | [diff] [blame] | 1348 | } |
1349 | |||||
Chris Lattner | 337e550 | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1350 | static void CheckPoppedLabel(LabelDecl *L, Sema &S) { |
1351 | // Verify that we have no forward references left. If so, there was a goto | ||||
1352 | // or address of a label taken, but no definition of it. Label fwd | ||||
1353 | // definitions are indicated with a null substmt. | ||||
1354 | if (L->getStmt() == 0) | ||||
1355 | S.Diag(L->getLocation(), diag::err_undeclared_label_use) <<L->getDeclName(); | ||||
1356 | } | ||||
1357 | |||||
Steve Naroff | b216c88 | 2007-10-09 22:01:59 +0000 | [diff] [blame] | 1358 | void Sema::ActOnPopScope(SourceLocation Loc, Scope *S) { |
Chris Lattner | 31e0572 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 1359 | if (S->decl_empty()) return; |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 1360 | assert((S->getFlags() & (Scope::DeclScope | Scope::TemplateParamScope)) && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1361 | "Scope shouldn't contain decls!"); |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 1362 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1363 | for (Scope::decl_iterator I = S->decl_begin(), E = S->decl_end(); |
1364 | I != E; ++I) { | ||||
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1365 | Decl *TmpD = (*I); |
Steve Naroff | c752d04 | 2007-09-13 18:10:37 +0000 | [diff] [blame] | 1366 | assert(TmpD && "This decl didn't get pushed??"); |
Argyrios Kyrtzidis | 7643536 | 2008-06-10 01:32:09 +0000 | [diff] [blame] | 1367 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1368 | assert(isa<NamedDecl>(TmpD) && "Decl isn't NamedDecl?"); |
1369 | NamedDecl *D = cast<NamedDecl>(TmpD); | ||||
Argyrios Kyrtzidis | 7643536 | 2008-06-10 01:32:09 +0000 | [diff] [blame] | 1370 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1371 | if (!D->getDeclName()) continue; |
Chris Lattner | 7f925cc | 2008-04-11 07:00:53 +0000 | [diff] [blame] | 1372 | |
Douglas Gregor | b5352cf | 2009-10-08 21:35:42 +0000 | [diff] [blame] | 1373 | // Diagnose unused variables in this scope. |
Matt Beaumont-Gay | 59d8ccb | 2013-03-28 21:46:45 +0000 | [diff] [blame] | 1374 | if (!S->hasUnrecoverableErrorOccurred()) |
Douglas Gregor | 5764f61 | 2010-05-08 23:05:03 +0000 | [diff] [blame] | 1375 | DiagnoseUnusedDecl(D); |
1376 | |||||
Chris Lattner | 337e550 | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1377 | // If this was a forward reference to a label, verify it was defined. |
1378 | if (LabelDecl *LD = dyn_cast<LabelDecl>(D)) | ||||
1379 | CheckPoppedLabel(LD, *this); | ||||
1380 | |||||
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1381 | // Remove this name from our lexical scope. |
1382 | IdResolver.RemoveDecl(D); | ||||
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1383 | } |
1384 | } | ||||
1385 | |||||
James Molloy | 16f1f71 | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 1386 | void Sema::ActOnStartFunctionDeclarator() { |
1387 | ++InFunctionDeclarator; | ||||
1388 | } | ||||
1389 | |||||
1390 | void Sema::ActOnEndFunctionDeclarator() { | ||||
1391 | assert(InFunctionDeclarator); | ||||
1392 | --InFunctionDeclarator; | ||||
1393 | } | ||||
1394 | |||||
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 1395 | /// \brief Look for an Objective-C class in the translation unit. |
1396 | /// | ||||
1397 | /// \param Id The name of the Objective-C class we're looking for. If | ||||
1398 | /// typo-correction fixes this name, the Id will be updated | ||||
1399 | /// to the fixed name. | ||||
1400 | /// | ||||
1401 | /// \param IdLoc The location of the name in the translation unit. | ||||
1402 | /// | ||||
James Dennett | 16ae9de | 2012-06-22 10:16:05 +0000 | [diff] [blame] | 1403 | /// \param DoTypoCorrection If true, this routine will attempt typo correction |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 1404 | /// if there is no class with the given name. |
1405 | /// | ||||
1406 | /// \returns The declaration of the named Objective-C class, or NULL if the | ||||
1407 | /// class could not be found. | ||||
1408 | ObjCInterfaceDecl *Sema::getObjCInterfaceDecl(IdentifierInfo *&Id, | ||||
1409 | SourceLocation IdLoc, | ||||
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1410 | bool DoTypoCorrection) { |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 1411 | // The third "scope" argument is 0 since we aren't enabling lazy built-in |
1412 | // creation from this context. | ||||
1413 | NamedDecl *IDecl = LookupSingleName(TUScope, Id, IdLoc, LookupOrdinaryName); | ||||
1414 | |||||
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1415 | if (!IDecl && DoTypoCorrection) { |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 1416 | // Perform typo correction at the given location, but only if we |
1417 | // find an Objective-C class name. | ||||
Kaelyn Uhrain | 43e875d | 2012-01-18 21:41:41 +0000 | [diff] [blame] | 1418 | DeclFilterCCC<ObjCInterfaceDecl> Validator; |
1419 | if (TypoCorrection C = CorrectTypo(DeclarationNameInfo(Id, IdLoc), | ||||
1420 | LookupOrdinaryName, TUScope, NULL, | ||||
Kaelyn Uhrain | 16e46dd | 2012-01-31 23:49:25 +0000 | [diff] [blame] | 1421 | Validator)) { |
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 1422 | diagnoseTypo(C, PDiag(diag::err_undef_interface_suggest) << Id); |
Kaelyn Uhrain | 43e875d | 2012-01-18 21:41:41 +0000 | [diff] [blame] | 1423 | IDecl = C.getCorrectionDeclAs<ObjCInterfaceDecl>(); |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 1424 | Id = IDecl->getIdentifier(); |
1425 | } | ||||
1426 | } | ||||
Fariborz Jahanian | 3306f96 | 2012-01-12 00:18:35 +0000 | [diff] [blame] | 1427 | ObjCInterfaceDecl *Def = dyn_cast_or_null<ObjCInterfaceDecl>(IDecl); |
1428 | // This routine must always return a class definition, if any. | ||||
1429 | if (Def && Def->getDefinition()) | ||||
1430 | Def = Def->getDefinition(); | ||||
1431 | return Def; | ||||
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 1432 | } |
1433 | |||||
Douglas Gregor | 1a0d31a | 2009-01-12 18:45:55 +0000 | [diff] [blame] | 1434 | /// getNonFieldDeclScope - Retrieves the innermost scope, starting |
1435 | /// from S, where a non-field would be declared. This routine copes | ||||
1436 | /// with the difference between C and C++ scoping rules in structs and | ||||
1437 | /// unions. For example, the following code is well-formed in C but | ||||
1438 | /// ill-formed in C++: | ||||
1439 | /// @code | ||||
1440 | /// struct S6 { | ||||
1441 | /// enum { BAR } e; | ||||
1442 | /// }; | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1443 | /// |
Douglas Gregor | 1a0d31a | 2009-01-12 18:45:55 +0000 | [diff] [blame] | 1444 | /// void test_S6() { |
1445 | /// struct S6 a; | ||||
1446 | /// a.e = BAR; | ||||
1447 | /// } | ||||
1448 | /// @endcode | ||||
1449 | /// For the declaration of BAR, this routine will return a different | ||||
1450 | /// scope. The scope S will be the scope of the unnamed enumeration | ||||
1451 | /// within S6. In C++, this routine will return the scope associated | ||||
1452 | /// with S6, because the enumeration's scope is a transparent | ||||
1453 | /// context but structures can contain non-field names. In C, this | ||||
1454 | /// routine will return the translation unit scope, since the | ||||
1455 | /// enumeration's scope is a transparent context and structures cannot | ||||
1456 | /// contain non-field names. | ||||
1457 | Scope *Sema::getNonFieldDeclScope(Scope *S) { | ||||
1458 | while (((S->getFlags() & Scope::DeclScope) == 0) || | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1459 | (S->getEntity() && |
Douglas Gregor | 1a0d31a | 2009-01-12 18:45:55 +0000 | [diff] [blame] | 1460 | ((DeclContext *)S->getEntity())->isTransparentContext()) || |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1461 | (S->isClassScope() && !getLangOpts().CPlusPlus)) |
Douglas Gregor | 1a0d31a | 2009-01-12 18:45:55 +0000 | [diff] [blame] | 1462 | S = S->getParent(); |
1463 | return S; | ||||
1464 | } | ||||
1465 | |||||
Fariborz Jahanian | f799213 | 2013-01-04 18:45:40 +0000 | [diff] [blame] | 1466 | /// \brief Looks up the declaration of "struct objc_super" and |
1467 | /// saves it for later use in building builtin declaration of | ||||
1468 | /// objc_msgSendSuper and objc_msgSendSuper_stret. If no such | ||||
1469 | /// pre-existing declaration exists no action takes place. | ||||
1470 | static void LookupPredefedObjCSuperType(Sema &ThisSema, Scope *S, | ||||
1471 | IdentifierInfo *II) { | ||||
1472 | if (!II->isStr("objc_msgSendSuper")) | ||||
1473 | return; | ||||
1474 | ASTContext &Context = ThisSema.Context; | ||||
1475 | |||||
1476 | LookupResult Result(ThisSema, &Context.Idents.get("objc_super"), | ||||
1477 | SourceLocation(), Sema::LookupTagName); | ||||
1478 | ThisSema.LookupName(Result, S); | ||||
1479 | if (Result.getResultKind() == LookupResult::Found) | ||||
1480 | if (const TagDecl *TD = Result.getAsSingle<TagDecl>()) | ||||
1481 | Context.setObjCSuperType(Context.getTagDeclType(TD)); | ||||
1482 | } | ||||
1483 | |||||
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 1484 | /// LazilyCreateBuiltin - The specified Builtin-ID was first used at |
1485 | /// file scope. lazily create a decl for it. ForRedeclaration is true | ||||
1486 | /// if we're creating this built-in in anticipation of redeclaring the | ||||
1487 | /// built-in. | ||||
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1488 | NamedDecl *Sema::LazilyCreateBuiltin(IdentifierInfo *II, unsigned bid, |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 1489 | Scope *S, bool ForRedeclaration, |
1490 | SourceLocation Loc) { | ||||
Fariborz Jahanian | f799213 | 2013-01-04 18:45:40 +0000 | [diff] [blame] | 1491 | LookupPredefedObjCSuperType(*this, S, II); |
1492 | |||||
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1493 | Builtin::ID BID = (Builtin::ID)bid; |
1494 | |||||
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 1495 | ASTContext::GetBuiltinTypeError Error; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1496 | QualType R = Context.GetBuiltinType(BID, Error); |
Douglas Gregor | 370ab3f | 2009-02-14 01:52:53 +0000 | [diff] [blame] | 1497 | switch (Error) { |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 1498 | case ASTContext::GE_None: |
Douglas Gregor | 370ab3f | 2009-02-14 01:52:53 +0000 | [diff] [blame] | 1499 | // Okay |
1500 | break; | ||||
1501 | |||||
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 1502 | case ASTContext::GE_Missing_stdio: |
Douglas Gregor | 370ab3f | 2009-02-14 01:52:53 +0000 | [diff] [blame] | 1503 | if (ForRedeclaration) |
Douglas Gregor | 6b9109e | 2011-01-03 09:37:44 +0000 | [diff] [blame] | 1504 | Diag(Loc, diag::warn_implicit_decl_requires_stdio) |
Douglas Gregor | 370ab3f | 2009-02-14 01:52:53 +0000 | [diff] [blame] | 1505 | << Context.BuiltinInfo.GetName(BID); |
1506 | return 0; | ||||
Mike Stump | 782fa30 | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 1507 | |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 1508 | case ASTContext::GE_Missing_setjmp: |
Mike Stump | 782fa30 | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 1509 | if (ForRedeclaration) |
Douglas Gregor | 6b9109e | 2011-01-03 09:37:44 +0000 | [diff] [blame] | 1510 | Diag(Loc, diag::warn_implicit_decl_requires_setjmp) |
Mike Stump | 782fa30 | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 1511 | << Context.BuiltinInfo.GetName(BID); |
1512 | return 0; | ||||
Rafael Espindola | e2d4f4e | 2011-11-13 21:51:09 +0000 | [diff] [blame] | 1513 | |
1514 | case ASTContext::GE_Missing_ucontext: | ||||
1515 | if (ForRedeclaration) | ||||
1516 | Diag(Loc, diag::warn_implicit_decl_requires_ucontext) | ||||
1517 | << Context.BuiltinInfo.GetName(BID); | ||||
1518 | return 0; | ||||
Douglas Gregor | 370ab3f | 2009-02-14 01:52:53 +0000 | [diff] [blame] | 1519 | } |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 1520 | |
1521 | if (!ForRedeclaration && Context.BuiltinInfo.isPredefinedLibFunction(BID)) { | ||||
1522 | Diag(Loc, diag::ext_implicit_lib_function_decl) | ||||
1523 | << Context.BuiltinInfo.GetName(BID) | ||||
1524 | << R; | ||||
Douglas Gregor | b1152d8 | 2009-02-16 21:58:21 +0000 | [diff] [blame] | 1525 | if (Context.BuiltinInfo.getHeaderName(BID) && |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 1526 | Diags.getDiagnosticLevel(diag::ext_implicit_lib_function_decl, Loc) |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 1527 | != DiagnosticsEngine::Ignored) |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 1528 | Diag(Loc, diag::note_please_include_header) |
1529 | << Context.BuiltinInfo.getHeaderName(BID) | ||||
1530 | << Context.BuiltinInfo.GetName(BID); | ||||
1531 | } | ||||
1532 | |||||
Argyrios Kyrtzidis | ff898cd | 2008-04-17 14:47:13 +0000 | [diff] [blame] | 1533 | FunctionDecl *New = FunctionDecl::Create(Context, |
1534 | Context.getTranslationUnitDecl(), | ||||
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1535 | Loc, Loc, II, R, /*TInfo=*/0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 1536 | SC_Extern, |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 1537 | false, |
Douglas Gregor | 2224f84 | 2009-02-25 16:33:18 +0000 | [diff] [blame] | 1538 | /*hasPrototype=*/true); |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 1539 | New->setImplicit(); |
1540 | |||||
Chris Lattner | 95e2c71 | 2008-05-05 22:18:14 +0000 | [diff] [blame] | 1541 | // Create Decl objects for each parameter, adding them to the |
1542 | // FunctionDecl. | ||||
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1543 | if (const FunctionProtoType *FT = dyn_cast<FunctionProtoType>(R)) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1544 | SmallVector<ParmVarDecl*, 16> Params; |
John McCall | fb44de9 | 2011-05-01 22:35:37 +0000 | [diff] [blame] | 1545 | for (unsigned i = 0, e = FT->getNumArgs(); i != e; ++i) { |
1546 | ParmVarDecl *parm = | ||||
1547 | ParmVarDecl::Create(Context, New, SourceLocation(), | ||||
1548 | SourceLocation(), 0, | ||||
1549 | FT->getArgType(i), /*TInfo=*/0, | ||||
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 1550 | SC_None, 0); |
John McCall | fb44de9 | 2011-05-01 22:35:37 +0000 | [diff] [blame] | 1551 | parm->setScopeInfo(0, i); |
1552 | Params.push_back(parm); | ||||
1553 | } | ||||
David Blaikie | 4278c65 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 1554 | New->setParams(Params); |
Chris Lattner | 95e2c71 | 2008-05-05 22:18:14 +0000 | [diff] [blame] | 1555 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1556 | |
1557 | AddKnownFunctionAttributes(New); | ||||
1558 | |||||
Chris Lattner | 7f925cc | 2008-04-11 07:00:53 +0000 | [diff] [blame] | 1559 | // TUScope is the translation-unit scope to insert this function into. |
Douglas Gregor | a8cc8ce | 2009-01-09 18:51:29 +0000 | [diff] [blame] | 1560 | // FIXME: This is hideous. We need to teach PushOnScopeChains to |
1561 | // relate Scopes to DeclContexts, and probably eliminate CurContext | ||||
1562 | // entirely, but we're not there yet. | ||||
1563 | DeclContext *SavedContext = CurContext; | ||||
1564 | CurContext = Context.getTranslationUnitDecl(); | ||||
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 1565 | PushOnScopeChains(New, TUScope); |
Douglas Gregor | a8cc8ce | 2009-01-09 18:51:29 +0000 | [diff] [blame] | 1566 | CurContext = SavedContext; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1567 | return New; |
1568 | } | ||||
1569 | |||||
Douglas Gregor | 7dc80e1 | 2013-01-09 00:47:56 +0000 | [diff] [blame] | 1570 | /// \brief Filter out any previous declarations that the given declaration |
1571 | /// should not consider because they are not permitted to conflict, e.g., | ||||
1572 | /// because they come from hidden sub-modules and do not refer to the same | ||||
1573 | /// entity. | ||||
1574 | static void filterNonConflictingPreviousDecls(ASTContext &context, | ||||
1575 | NamedDecl *decl, | ||||
1576 | LookupResult &previous){ | ||||
1577 | // This is only interesting when modules are enabled. | ||||
1578 | if (!context.getLangOpts().Modules) | ||||
1579 | return; | ||||
1580 | |||||
1581 | // Empty sets are uninteresting. | ||||
1582 | if (previous.empty()) | ||||
1583 | return; | ||||
1584 | |||||
Douglas Gregor | 7dc80e1 | 2013-01-09 00:47:56 +0000 | [diff] [blame] | 1585 | LookupResult::Filter filter = previous.makeFilter(); |
1586 | while (filter.hasNext()) { | ||||
1587 | NamedDecl *old = filter.next(); | ||||
1588 | |||||
1589 | // Non-hidden declarations are never ignored. | ||||
1590 | if (!old->isHidden()) | ||||
1591 | continue; | ||||
1592 | |||||
Rafael Espindola | 181e3ec | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 1593 | if (!old->isExternallyVisible()) |
Douglas Gregor | 7dc80e1 | 2013-01-09 00:47:56 +0000 | [diff] [blame] | 1594 | filter.erase(); |
1595 | } | ||||
1596 | |||||
1597 | filter.done(); | ||||
1598 | } | ||||
1599 | |||||
Rafael Espindola | 5df37bd | 2011-12-26 22:42:47 +0000 | [diff] [blame] | 1600 | bool Sema::isIncompatibleTypedef(TypeDecl *Old, TypedefNameDecl *New) { |
1601 | QualType OldType; | ||||
1602 | if (TypedefNameDecl *OldTypedef = dyn_cast<TypedefNameDecl>(Old)) | ||||
1603 | OldType = OldTypedef->getUnderlyingType(); | ||||
1604 | else | ||||
1605 | OldType = Context.getTypeDeclType(Old); | ||||
1606 | QualType NewType = New->getUnderlyingType(); | ||||
1607 | |||||
Douglas Gregor | ec3bd72 | 2012-01-11 22:33:48 +0000 | [diff] [blame] | 1608 | if (NewType->isVariablyModifiedType()) { |
1609 | // Must not redefine a typedef with a variably-modified type. | ||||
1610 | int Kind = isa<TypeAliasDecl>(Old) ? 1 : 0; | ||||
1611 | Diag(New->getLocation(), diag::err_redefinition_variably_modified_typedef) | ||||
1612 | << Kind << NewType; | ||||
1613 | if (Old->getLocation().isValid()) | ||||
1614 | Diag(Old->getLocation(), diag::note_previous_definition); | ||||
1615 | New->setInvalidDecl(); | ||||
1616 | return true; | ||||
1617 | } | ||||
1618 | |||||
Rafael Espindola | 5df37bd | 2011-12-26 22:42:47 +0000 | [diff] [blame] | 1619 | if (OldType != NewType && |
1620 | !OldType->isDependentType() && | ||||
1621 | !NewType->isDependentType() && | ||||
Douglas Gregor | ec3bd72 | 2012-01-11 22:33:48 +0000 | [diff] [blame] | 1622 | !Context.hasSameType(OldType, NewType)) { |
Rafael Espindola | 5df37bd | 2011-12-26 22:42:47 +0000 | [diff] [blame] | 1623 | int Kind = isa<TypeAliasDecl>(Old) ? 1 : 0; |
1624 | Diag(New->getLocation(), diag::err_redefinition_different_typedef) | ||||
1625 | << Kind << NewType << OldType; | ||||
1626 | if (Old->getLocation().isValid()) | ||||
1627 | Diag(Old->getLocation(), diag::note_previous_definition); | ||||
1628 | New->setInvalidDecl(); | ||||
1629 | return true; | ||||
1630 | } | ||||
1631 | return false; | ||||
1632 | } | ||||
1633 | |||||
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1634 | /// MergeTypedefNameDecl - We just parsed a typedef 'New' which has the |
Douglas Gregor | cda9c67 | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 1635 | /// same name and scope as a previous declaration 'Old'. Figure out |
1636 | /// how to resolve this situation, merging decls or emitting | ||||
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 1637 | /// 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] | 1638 | /// |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1639 | void Sema::MergeTypedefNameDecl(TypedefNameDecl *New, LookupResult &OldDecls) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1640 | // If the new decl is known invalid already, don't bother doing any |
1641 | // merging checks. | ||||
1642 | if (New->isInvalidDecl()) return; | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1643 | |
Steve Naroff | 2b255c4 | 2008-09-09 14:32:20 +0000 | [diff] [blame] | 1644 | // Allow multiple definitions for ObjC built-in typedefs. |
1645 | // FIXME: Verify the underlying types are equivalent! | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1646 | if (getLangOpts().ObjC1) { |
Chris Lattner | 2bac0f6 | 2008-11-20 05:41:43 +0000 | [diff] [blame] | 1647 | const IdentifierInfo *TypeID = New->getIdentifier(); |
1648 | switch (TypeID->getLength()) { | ||||
1649 | default: break; | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1650 | case 2: |
Fariborz Jahanian | 0cd00be | 2012-05-14 22:48:56 +0000 | [diff] [blame] | 1651 | { |
1652 | if (!TypeID->isStr("id")) | ||||
1653 | break; | ||||
1654 | QualType T = New->getUnderlyingType(); | ||||
1655 | if (!T->isPointerType()) | ||||
1656 | break; | ||||
1657 | if (!T->isVoidPointerType()) { | ||||
1658 | QualType PT = T->getAs<PointerType>()->getPointeeType(); | ||||
1659 | if (!PT->isStructureType()) | ||||
1660 | break; | ||||
1661 | } | ||||
1662 | Context.setObjCIdRedefinitionType(T); | ||||
1663 | // Install the built-in type for 'id', ignoring the current definition. | ||||
1664 | New->setTypeForDecl(Context.getObjCIdType().getTypePtr()); | ||||
1665 | return; | ||||
1666 | } | ||||
Chris Lattner | 2bac0f6 | 2008-11-20 05:41:43 +0000 | [diff] [blame] | 1667 | case 5: |
1668 | if (!TypeID->isStr("Class")) | ||||
1669 | break; | ||||
Douglas Gregor | 01a4cf1 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 1670 | Context.setObjCClassRedefinitionType(New->getUnderlyingType()); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1671 | // Install the built-in type for 'Class', ignoring the current definition. |
1672 | New->setTypeForDecl(Context.getObjCClassType().getTypePtr()); | ||||
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 1673 | return; |
Chris Lattner | 2bac0f6 | 2008-11-20 05:41:43 +0000 | [diff] [blame] | 1674 | case 3: |
1675 | if (!TypeID->isStr("SEL")) | ||||
1676 | break; | ||||
Douglas Gregor | 01a4cf1 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 1677 | Context.setObjCSelRedefinitionType(New->getUnderlyingType()); |
Fariborz Jahanian | 13dcd00 | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 1678 | // Install the built-in type for 'SEL', ignoring the current definition. |
1679 | New->setTypeForDecl(Context.getObjCSelType().getTypePtr()); | ||||
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 1680 | return; |
Steve Naroff | 2b255c4 | 2008-09-09 14:32:20 +0000 | [diff] [blame] | 1681 | } |
1682 | // Fall through - the typedef name was not a builtin type. | ||||
1683 | } | ||||
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1684 | |
Douglas Gregor | 6697312 | 2009-01-28 17:15:10 +0000 | [diff] [blame] | 1685 | // Verify the old decl was also a type. |
John McCall | 5126fd0 | 2009-12-30 00:31:22 +0000 | [diff] [blame] | 1686 | TypeDecl *Old = OldDecls.getAsSingle<TypeDecl>(); |
1687 | if (!Old) { | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1688 | Diag(New->getLocation(), diag::err_redefinition_different_kind) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 1689 | << New->getDeclName(); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1690 | |
1691 | NamedDecl *OldD = OldDecls.getRepresentativeDecl(); | ||||
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 1692 | if (OldD->getLocation().isValid()) |
Fariborz Jahanian | c55a240 | 2009-01-16 19:58:32 +0000 | [diff] [blame] | 1693 | Diag(OldD->getLocation(), diag::note_previous_definition); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1694 | |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 1695 | return New->setInvalidDecl(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1696 | } |
Douglas Gregor | 6697312 | 2009-01-28 17:15:10 +0000 | [diff] [blame] | 1697 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1698 | // If the old declaration is invalid, just give up here. |
1699 | if (Old->isInvalidDecl()) | ||||
1700 | return New->setInvalidDecl(); | ||||
1701 | |||||
Chris Lattner | 99cb997 | 2008-07-25 18:44:27 +0000 | [diff] [blame] | 1702 | // If the typedef types are not identical, reject them in all languages and |
1703 | // with any extensions enabled. | ||||
Rafael Espindola | 5df37bd | 2011-12-26 22:42:47 +0000 | [diff] [blame] | 1704 | if (isIncompatibleTypedef(Old, New)) |
1705 | return; | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1706 | |
John McCall | 5126fd0 | 2009-12-30 00:31:22 +0000 | [diff] [blame] | 1707 | // The types match. Link up the redeclaration chain if the old |
1708 | // declaration was a typedef. | ||||
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1709 | if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Old)) |
1710 | New->setPreviousDeclaration(Typedef); | ||||
John McCall | 5126fd0 | 2009-12-30 00:31:22 +0000 | [diff] [blame] | 1711 | |
Eli Friedman | 9ec4099 | 2013-07-16 02:07:49 +0000 | [diff] [blame] | 1712 | mergeDeclAttributes(New, Old); |
1713 | |||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1714 | if (getLangOpts().MicrosoftExt) |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 1715 | return; |
Eli Friedman | 54ecfce | 2008-06-11 06:20:39 +0000 | [diff] [blame] | 1716 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1717 | if (getLangOpts().CPlusPlus) { |
Douglas Gregor | 93dda72 | 2010-01-11 21:54:40 +0000 | [diff] [blame] | 1718 | // C++ [dcl.typedef]p2: |
1719 | // In a given non-class scope, a typedef specifier can be used to | ||||
1720 | // redefine the name of any type declared in that scope to refer | ||||
1721 | // to the type to which it already refers. | ||||
Chris Lattner | 32b0675 | 2009-04-17 22:04:20 +0000 | [diff] [blame] | 1722 | if (!isa<CXXRecordDecl>(CurContext)) |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 1723 | return; |
Douglas Gregor | 93dda72 | 2010-01-11 21:54:40 +0000 | [diff] [blame] | 1724 | |
1725 | // C++0x [dcl.typedef]p4: | ||||
1726 | // In a given class scope, a typedef specifier can be used to redefine | ||||
1727 | // any class-name declared in that scope that is not also a typedef-name | ||||
1728 | // to refer to the type to which it already refers. | ||||
1729 | // | ||||
1730 | // This wording came in via DR424, which was a correction to the | ||||
1731 | // wording in DR56, which accidentally banned code like: | ||||
1732 | // | ||||
1733 | // struct S { | ||||
1734 | // typedef struct A { } A; | ||||
1735 | // }; | ||||
1736 | // | ||||
1737 | // in the C++03 standard. We implement the C++0x semantics, which | ||||
1738 | // allow the above but disallow | ||||
1739 | // | ||||
1740 | // struct S { | ||||
1741 | // typedef int I; | ||||
1742 | // typedef int I; | ||||
1743 | // }; | ||||
1744 | // | ||||
1745 | // since that was the intent of DR56. | ||||
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1746 | if (!isa<TypedefNameDecl>(Old)) |
Douglas Gregor | 93dda72 | 2010-01-11 21:54:40 +0000 | [diff] [blame] | 1747 | return; |
1748 | |||||
Chris Lattner | 32b0675 | 2009-04-17 22:04:20 +0000 | [diff] [blame] | 1749 | Diag(New->getLocation(), diag::err_redefinition) |
1750 | << New->getDeclName(); | ||||
1751 | Diag(Old->getLocation(), diag::note_previous_definition); | ||||
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 1752 | return New->setInvalidDecl(); |
Daniel Dunbar | 2fe0997 | 2008-09-12 18:10:20 +0000 | [diff] [blame] | 1753 | } |
Eli Friedman | 54ecfce | 2008-06-11 06:20:39 +0000 | [diff] [blame] | 1754 | |
Douglas Gregor | c0004df | 2012-01-11 04:25:01 +0000 | [diff] [blame] | 1755 | // Modules always permit redefinition of typedefs, as does C11. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1756 | if (getLangOpts().Modules || getLangOpts().C11) |
Douglas Gregor | c02d62f | 2012-01-09 15:36:04 +0000 | [diff] [blame] | 1757 | return; |
1758 | |||||
Chris Lattner | 32b0675 | 2009-04-17 22:04:20 +0000 | [diff] [blame] | 1759 | // If we have a redefinition of a typedef in C, emit a warning. This warning |
1760 | // is normally mapped to an error, but can be controlled with | ||||
Eli Friedman | 340a4e5 | 2009-06-04 23:03:07 +0000 | [diff] [blame] | 1761 | // -Wtypedef-redefinition. If either the original or the redefinition is |
1762 | // in a system header, don't emit this for compatibility with GCC. | ||||
Chris Lattner | 6d97e5e | 2010-03-01 20:59:53 +0000 | [diff] [blame] | 1763 | if (getDiagnostics().getSuppressSystemWarnings() && |
Eli Friedman | 340a4e5 | 2009-06-04 23:03:07 +0000 | [diff] [blame] | 1764 | (Context.getSourceManager().isInSystemHeader(Old->getLocation()) || |
1765 | Context.getSourceManager().isInSystemHeader(New->getLocation()))) | ||||
Chris Lattner | d0359af | 2009-04-27 01:46:12 +0000 | [diff] [blame] | 1766 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1767 | |
Chris Lattner | 32b0675 | 2009-04-17 22:04:20 +0000 | [diff] [blame] | 1768 | Diag(New->getLocation(), diag::warn_redefinition_of_typedef) |
1769 | << New->getDeclName(); | ||||
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 1770 | Diag(Old->getLocation(), diag::note_previous_definition); |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 1771 | return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1772 | } |
1773 | |||||
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1774 | /// DeclhasAttr - returns true if decl Declaration already has the target |
1775 | /// attribute. | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1776 | static bool |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1777 | DeclHasAttr(const Decl *D, const Attr *A) { |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1778 | // There can be multiple AvailabilityAttr in a Decl. Make sure we copy |
1779 | // all of them. It is mergeAvailabilityAttr in SemaDeclAttr.cpp that is | ||||
1780 | // responsible for making sure they are consistent. | ||||
1781 | const AvailabilityAttr *AA = dyn_cast<AvailabilityAttr>(A); | ||||
1782 | if (AA) | ||||
1783 | return false; | ||||
1784 | |||||
DeLesley Hutchins | 3ce9fae | 2012-10-12 21:38:12 +0000 | [diff] [blame] | 1785 | // The following thread safety attributes can also be duplicated. |
1786 | switch (A->getKind()) { | ||||
1787 | case attr::ExclusiveLocksRequired: | ||||
1788 | case attr::SharedLocksRequired: | ||||
1789 | case attr::LocksExcluded: | ||||
1790 | case attr::ExclusiveLockFunction: | ||||
1791 | case attr::SharedLockFunction: | ||||
1792 | case attr::UnlockFunction: | ||||
1793 | case attr::ExclusiveTrylockFunction: | ||||
1794 | case attr::SharedTrylockFunction: | ||||
1795 | case attr::GuardedBy: | ||||
1796 | case attr::PtGuardedBy: | ||||
1797 | case attr::AcquiredBefore: | ||||
1798 | case attr::AcquiredAfter: | ||||
1799 | return false; | ||||
DeLesley Hutchins | 6c500b1 | 2012-10-12 21:49:04 +0000 | [diff] [blame] | 1800 | default: |
1801 | ; | ||||
DeLesley Hutchins | 3ce9fae | 2012-10-12 21:38:12 +0000 | [diff] [blame] | 1802 | } |
1803 | |||||
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1804 | const OwnershipAttr *OA = dyn_cast<OwnershipAttr>(A); |
Julien Lerouge | 77f68bb | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 1805 | const AnnotateAttr *Ann = dyn_cast<AnnotateAttr>(A); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1806 | for (Decl::attr_iterator i = D->attr_begin(), e = D->attr_end(); i != e; ++i) |
1807 | if ((*i)->getKind() == A->getKind()) { | ||||
Julien Lerouge | 77f68bb | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 1808 | if (Ann) { |
1809 | if (Ann->getAnnotation() == cast<AnnotateAttr>(*i)->getAnnotation()) | ||||
1810 | return true; | ||||
1811 | continue; | ||||
1812 | } | ||||
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1813 | // FIXME: Don't hardcode this check |
1814 | if (OA && isa<OwnershipAttr>(*i)) | ||||
1815 | return OA->getOwnKind() == cast<OwnershipAttr>(*i)->getOwnKind(); | ||||
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 1816 | return true; |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1817 | } |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 1818 | |
1819 | return false; | ||||
1820 | } | ||||
1821 | |||||
Richard Smith | 671b321 | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 1822 | static bool isAttributeTargetADefinition(Decl *D) { |
1823 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) | ||||
1824 | return VD->isThisDeclarationADefinition(); | ||||
1825 | if (TagDecl *TD = dyn_cast<TagDecl>(D)) | ||||
1826 | return TD->isCompleteDefinition() || TD->isBeingDefined(); | ||||
1827 | return true; | ||||
1828 | } | ||||
1829 | |||||
1830 | /// Merge alignment attributes from \p Old to \p New, taking into account the | ||||
1831 | /// special semantics of C11's _Alignas specifier and C++11's alignas attribute. | ||||
1832 | /// | ||||
1833 | /// \return \c true if any attributes were added to \p New. | ||||
1834 | static bool mergeAlignedAttrs(Sema &S, NamedDecl *New, Decl *Old) { | ||||
1835 | // Look for alignas attributes on Old, and pick out whichever attribute | ||||
1836 | // specifies the strictest alignment requirement. | ||||
1837 | AlignedAttr *OldAlignasAttr = 0; | ||||
1838 | AlignedAttr *OldStrictestAlignAttr = 0; | ||||
1839 | unsigned OldAlign = 0; | ||||
1840 | for (specific_attr_iterator<AlignedAttr> | ||||
1841 | I = Old->specific_attr_begin<AlignedAttr>(), | ||||
1842 | E = Old->specific_attr_end<AlignedAttr>(); I != E; ++I) { | ||||
1843 | // FIXME: We have no way of representing inherited dependent alignments | ||||
1844 | // in a case like: | ||||
1845 | // template<int A, int B> struct alignas(A) X; | ||||
1846 | // template<int A, int B> struct alignas(B) X {}; | ||||
1847 | // For now, we just ignore any alignas attributes which are not on the | ||||
1848 | // definition in such a case. | ||||
1849 | if (I->isAlignmentDependent()) | ||||
1850 | return false; | ||||
1851 | |||||
1852 | if (I->isAlignas()) | ||||
1853 | OldAlignasAttr = *I; | ||||
1854 | |||||
1855 | unsigned Align = I->getAlignment(S.Context); | ||||
1856 | if (Align > OldAlign) { | ||||
1857 | OldAlign = Align; | ||||
1858 | OldStrictestAlignAttr = *I; | ||||
1859 | } | ||||
1860 | } | ||||
1861 | |||||
1862 | // Look for alignas attributes on New. | ||||
1863 | AlignedAttr *NewAlignasAttr = 0; | ||||
1864 | unsigned NewAlign = 0; | ||||
1865 | for (specific_attr_iterator<AlignedAttr> | ||||
1866 | I = New->specific_attr_begin<AlignedAttr>(), | ||||
1867 | E = New->specific_attr_end<AlignedAttr>(); I != E; ++I) { | ||||
1868 | if (I->isAlignmentDependent()) | ||||
1869 | return false; | ||||
1870 | |||||
1871 | if (I->isAlignas()) | ||||
1872 | NewAlignasAttr = *I; | ||||
1873 | |||||
1874 | unsigned Align = I->getAlignment(S.Context); | ||||
1875 | if (Align > NewAlign) | ||||
1876 | NewAlign = Align; | ||||
1877 | } | ||||
1878 | |||||
1879 | if (OldAlignasAttr && NewAlignasAttr && OldAlign != NewAlign) { | ||||
1880 | // Both declarations have 'alignas' attributes. We require them to match. | ||||
1881 | // C++11 [dcl.align]p6 and C11 6.7.5/7 both come close to saying this, but | ||||
1882 | // fall short. (If two declarations both have alignas, they must both match | ||||
1883 | // every definition, and so must match each other if there is a definition.) | ||||
1884 | |||||
1885 | // If either declaration only contains 'alignas(0)' specifiers, then it | ||||
1886 | // specifies the natural alignment for the type. | ||||
1887 | if (OldAlign == 0 || NewAlign == 0) { | ||||
1888 | QualType Ty; | ||||
1889 | if (ValueDecl *VD = dyn_cast<ValueDecl>(New)) | ||||
1890 | Ty = VD->getType(); | ||||
1891 | else | ||||
1892 | Ty = S.Context.getTagDeclType(cast<TagDecl>(New)); | ||||
1893 | |||||
1894 | if (OldAlign == 0) | ||||
1895 | OldAlign = S.Context.getTypeAlign(Ty); | ||||
1896 | if (NewAlign == 0) | ||||
1897 | NewAlign = S.Context.getTypeAlign(Ty); | ||||
1898 | } | ||||
1899 | |||||
1900 | if (OldAlign != NewAlign) { | ||||
1901 | S.Diag(NewAlignasAttr->getLocation(), diag::err_alignas_mismatch) | ||||
1902 | << (unsigned)S.Context.toCharUnitsFromBits(OldAlign).getQuantity() | ||||
1903 | << (unsigned)S.Context.toCharUnitsFromBits(NewAlign).getQuantity(); | ||||
1904 | S.Diag(OldAlignasAttr->getLocation(), diag::note_previous_declaration); | ||||
1905 | } | ||||
1906 | } | ||||
1907 | |||||
1908 | if (OldAlignasAttr && !NewAlignasAttr && isAttributeTargetADefinition(New)) { | ||||
1909 | // C++11 [dcl.align]p6: | ||||
1910 | // if any declaration of an entity has an alignment-specifier, | ||||
1911 | // every defining declaration of that entity shall specify an | ||||
1912 | // equivalent alignment. | ||||
1913 | // C11 6.7.5/7: | ||||
1914 | // If the definition of an object does not have an alignment | ||||
1915 | // specifier, any other declaration of that object shall also | ||||
1916 | // have no alignment specifier. | ||||
1917 | S.Diag(New->getLocation(), diag::err_alignas_missing_on_definition) | ||||
1918 | << OldAlignasAttr->isC11(); | ||||
1919 | S.Diag(OldAlignasAttr->getLocation(), diag::note_alignas_on_declaration) | ||||
1920 | << OldAlignasAttr->isC11(); | ||||
1921 | } | ||||
1922 | |||||
1923 | bool AnyAdded = false; | ||||
1924 | |||||
1925 | // Ensure we have an attribute representing the strictest alignment. | ||||
1926 | if (OldAlign > NewAlign) { | ||||
1927 | AlignedAttr *Clone = OldStrictestAlignAttr->clone(S.Context); | ||||
1928 | Clone->setInherited(true); | ||||
1929 | New->addAttr(Clone); | ||||
1930 | AnyAdded = true; | ||||
1931 | } | ||||
1932 | |||||
1933 | // Ensure we have an alignas attribute if the old declaration had one. | ||||
1934 | if (OldAlignasAttr && !NewAlignasAttr && | ||||
1935 | !(AnyAdded && OldStrictestAlignAttr->isAlignas())) { | ||||
1936 | AlignedAttr *Clone = OldAlignasAttr->clone(S.Context); | ||||
1937 | Clone->setInherited(true); | ||||
1938 | New->addAttr(Clone); | ||||
1939 | AnyAdded = true; | ||||
1940 | } | ||||
1941 | |||||
1942 | return AnyAdded; | ||||
1943 | } | ||||
1944 | |||||
1945 | static bool mergeDeclAttribute(Sema &S, NamedDecl *D, InheritableAttr *Attr, | ||||
1946 | bool Override) { | ||||
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1947 | InheritableAttr *NewAttr = NULL; |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1948 | unsigned AttrSpellingListIndex = Attr->getSpellingListIndex(); |
Rafael Espindola | 838dc59 | 2013-01-12 06:42:30 +0000 | [diff] [blame] | 1949 | if (AvailabilityAttr *AA = dyn_cast<AvailabilityAttr>(Attr)) |
Richard Smith | 671b321 | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 1950 | NewAttr = S.mergeAvailabilityAttr(D, AA->getRange(), AA->getPlatform(), |
1951 | AA->getIntroduced(), AA->getDeprecated(), | ||||
1952 | AA->getObsoleted(), AA->getUnavailable(), | ||||
1953 | AA->getMessage(), Override, | ||||
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 1954 | AttrSpellingListIndex); |
Richard Smith | 671b321 | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 1955 | else if (VisibilityAttr *VA = dyn_cast<VisibilityAttr>(Attr)) |
1956 | NewAttr = S.mergeVisibilityAttr(D, VA->getRange(), VA->getVisibility(), | ||||
1957 | AttrSpellingListIndex); | ||||
1958 | else if (TypeVisibilityAttr *VA = dyn_cast<TypeVisibilityAttr>(Attr)) | ||||
1959 | NewAttr = S.mergeTypeVisibilityAttr(D, VA->getRange(), VA->getVisibility(), | ||||
1960 | AttrSpellingListIndex); | ||||
Rafael Espindola | 838dc59 | 2013-01-12 06:42:30 +0000 | [diff] [blame] | 1961 | else if (DLLImportAttr *ImportA = dyn_cast<DLLImportAttr>(Attr)) |
Richard Smith | 671b321 | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 1962 | NewAttr = S.mergeDLLImportAttr(D, ImportA->getRange(), |
1963 | AttrSpellingListIndex); | ||||
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1964 | else if (DLLExportAttr *ExportA = dyn_cast<DLLExportAttr>(Attr)) |
Richard Smith | 671b321 | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 1965 | NewAttr = S.mergeDLLExportAttr(D, ExportA->getRange(), |
1966 | AttrSpellingListIndex); | ||||
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1967 | else if (FormatAttr *FA = dyn_cast<FormatAttr>(Attr)) |
Richard Smith | 671b321 | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 1968 | NewAttr = S.mergeFormatAttr(D, FA->getRange(), FA->getType(), |
1969 | FA->getFormatIdx(), FA->getFirstArg(), | ||||
1970 | AttrSpellingListIndex); | ||||
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1971 | else if (SectionAttr *SA = dyn_cast<SectionAttr>(Attr)) |
Richard Smith | 671b321 | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 1972 | NewAttr = S.mergeSectionAttr(D, SA->getRange(), SA->getName(), |
1973 | AttrSpellingListIndex); | ||||
1974 | else if (isa<AlignedAttr>(Attr)) | ||||
1975 | // AlignedAttrs are handled separately, because we need to handle all | ||||
1976 | // such attributes on a declaration at the same time. | ||||
1977 | NewAttr = 0; | ||||
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1978 | else if (!DeclHasAttr(D, Attr)) |
Richard Smith | 671b321 | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 1979 | NewAttr = cast<InheritableAttr>(Attr->clone(S.Context)); |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1980 | |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1981 | if (NewAttr) { |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1982 | NewAttr->setInherited(true); |
1983 | D->addAttr(NewAttr); | ||||
1984 | return true; | ||||
1985 | } | ||||
1986 | |||||
1987 | return false; | ||||
1988 | } | ||||
1989 | |||||
Rafael Espindola | 4b044c6 | 2012-07-15 01:05:36 +0000 | [diff] [blame] | 1990 | static const Decl *getDefinition(const Decl *D) { |
1991 | if (const TagDecl *TD = dyn_cast<TagDecl>(D)) | ||||
Rafael Espindola | 3f66406 | 2012-05-18 01:47:00 +0000 | [diff] [blame] | 1992 | return TD->getDefinition(); |
Rafael Espindola | 4b044c6 | 2012-07-15 01:05:36 +0000 | [diff] [blame] | 1993 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) |
Rafael Espindola | 3f66406 | 2012-05-18 01:47:00 +0000 | [diff] [blame] | 1994 | return VD->getDefinition(); |
Rafael Espindola | 4b044c6 | 2012-07-15 01:05:36 +0000 | [diff] [blame] | 1995 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Rafael Espindola | 3f66406 | 2012-05-18 01:47:00 +0000 | [diff] [blame] | 1996 | const FunctionDecl* Def; |
1997 | if (FD->hasBody(Def)) | ||||
1998 | return Def; | ||||
1999 | } | ||||
2000 | return NULL; | ||||
2001 | } | ||||
2002 | |||||
Rafael Espindola | d320ffc | 2012-07-15 01:33:40 +0000 | [diff] [blame] | 2003 | static bool hasAttribute(const Decl *D, attr::Kind Kind) { |
2004 | for (Decl::attr_iterator I = D->attr_begin(), E = D->attr_end(); | ||||
2005 | I != E; ++I) { | ||||
2006 | Attr *Attribute = *I; | ||||
2007 | if (Attribute->getKind() == Kind) | ||||
2008 | return true; | ||||
2009 | } | ||||
2010 | return false; | ||||
2011 | } | ||||
2012 | |||||
2013 | /// checkNewAttributesAfterDef - If we already have a definition, check that | ||||
2014 | /// there are no new attributes in this declaration. | ||||
2015 | static void checkNewAttributesAfterDef(Sema &S, Decl *New, const Decl *Old) { | ||||
2016 | if (!New->hasAttrs()) | ||||
2017 | return; | ||||
2018 | |||||
2019 | const Decl *Def = getDefinition(Old); | ||||
2020 | if (!Def || Def == New) | ||||
2021 | return; | ||||
2022 | |||||
2023 | AttrVec &NewAttributes = New->getAttrs(); | ||||
2024 | for (unsigned I = 0, E = NewAttributes.size(); I != E;) { | ||||
2025 | const Attr *NewAttribute = NewAttributes[I]; | ||||
2026 | if (hasAttribute(Def, NewAttribute->getKind())) { | ||||
2027 | ++I; | ||||
2028 | continue; // regular attr merging will take care of validating this. | ||||
2029 | } | ||||
Richard Smith | 671b321 | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 2030 | |
Richard Smith | 7586a6e | 2013-01-30 05:45:05 +0000 | [diff] [blame] | 2031 | if (isa<C11NoReturnAttr>(NewAttribute)) { |
Richard Smith | 671b321 | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 2032 | // 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] | 2033 | ++I; |
2034 | continue; | ||||
Richard Smith | 671b321 | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 2035 | } else if (const AlignedAttr *AA = dyn_cast<AlignedAttr>(NewAttribute)) { |
2036 | if (AA->isAlignas()) { | ||||
2037 | // C++11 [dcl.align]p6: | ||||
2038 | // if any declaration of an entity has an alignment-specifier, | ||||
2039 | // every defining declaration of that entity shall specify an | ||||
2040 | // equivalent alignment. | ||||
2041 | // C11 6.7.5/7: | ||||
2042 | // If the definition of an object does not have an alignment | ||||
2043 | // specifier, any other declaration of that object shall also | ||||
2044 | // have no alignment specifier. | ||||
2045 | S.Diag(Def->getLocation(), diag::err_alignas_missing_on_definition) | ||||
2046 | << AA->isC11(); | ||||
2047 | S.Diag(NewAttribute->getLocation(), diag::note_alignas_on_declaration) | ||||
2048 | << AA->isC11(); | ||||
2049 | NewAttributes.erase(NewAttributes.begin() + I); | ||||
2050 | --E; | ||||
2051 | continue; | ||||
2052 | } | ||||
Richard Smith | 7586a6e | 2013-01-30 05:45:05 +0000 | [diff] [blame] | 2053 | } |
Richard Smith | 671b321 | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 2054 | |
Rafael Espindola | d320ffc | 2012-07-15 01:33:40 +0000 | [diff] [blame] | 2055 | S.Diag(NewAttribute->getLocation(), |
2056 | diag::warn_attribute_precede_definition); | ||||
2057 | S.Diag(Def->getLocation(), diag::note_previous_definition); | ||||
2058 | NewAttributes.erase(NewAttributes.begin() + I); | ||||
2059 | --E; | ||||
2060 | } | ||||
2061 | } | ||||
2062 | |||||
John McCall | eca5d22 | 2011-03-02 04:00:57 +0000 | [diff] [blame] | 2063 | /// mergeDeclAttributes - Copy attributes from the Old decl to the New one. |
Rafael Espindola | 51be6e3 | 2013-01-08 22:04:34 +0000 | [diff] [blame] | 2064 | void Sema::mergeDeclAttributes(NamedDecl *New, Decl *Old, |
Douglas Gregor | f4d918f | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2065 | AvailabilityMergeKind AMK) { |
Richard Smith | 3a2b7a1 | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 2066 | if (!Old->hasAttrs() && !New->hasAttrs()) |
2067 | return; | ||||
2068 | |||||
Rafael Espindola | 3f66406 | 2012-05-18 01:47:00 +0000 | [diff] [blame] | 2069 | // attributes declared post-definition are currently ignored |
Rafael Espindola | d320ffc | 2012-07-15 01:33:40 +0000 | [diff] [blame] | 2070 | checkNewAttributesAfterDef(*this, New, Old); |
Rafael Espindola | 3f66406 | 2012-05-18 01:47:00 +0000 | [diff] [blame] | 2071 | |
Douglas Gregor | 27c6da2 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 2072 | if (!Old->hasAttrs()) |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2073 | return; |
John McCall | eca5d22 | 2011-03-02 04:00:57 +0000 | [diff] [blame] | 2074 | |
Douglas Gregor | 27c6da2 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 2075 | bool foundAny = New->hasAttrs(); |
John McCall | eca5d22 | 2011-03-02 04:00:57 +0000 | [diff] [blame] | 2076 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2077 | // Ensure that any moving of objects within the allocated map is done before |
2078 | // we process them. | ||||
Douglas Gregor | 27c6da2 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 2079 | if (!foundAny) New->setAttrs(AttrVec()); |
John McCall | eca5d22 | 2011-03-02 04:00:57 +0000 | [diff] [blame] | 2080 | |
Peter Collingbourne | a97d70b | 2011-01-21 02:08:36 +0000 | [diff] [blame] | 2081 | for (specific_attr_iterator<InheritableAttr> |
Douglas Gregor | 27c6da2 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 2082 | i = Old->specific_attr_begin<InheritableAttr>(), |
2083 | e = Old->specific_attr_end<InheritableAttr>(); | ||||
2084 | i != e; ++i) { | ||||
Douglas Gregor | f4d918f | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2085 | bool Override = false; |
Douglas Gregor | c193dd8 | 2011-09-23 20:23:42 +0000 | [diff] [blame] | 2086 | // Ignore deprecated/unavailable/availability attributes if requested. |
Douglas Gregor | f4d918f | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2087 | if (isa<DeprecatedAttr>(*i) || |
2088 | isa<UnavailableAttr>(*i) || | ||||
2089 | isa<AvailabilityAttr>(*i)) { | ||||
2090 | switch (AMK) { | ||||
2091 | case AMK_None: | ||||
2092 | continue; | ||||
John McCall | 6c2c250 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 2093 | |
Douglas Gregor | f4d918f | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2094 | case AMK_Redeclaration: |
2095 | break; | ||||
2096 | |||||
2097 | case AMK_Override: | ||||
2098 | Override = true; | ||||
2099 | break; | ||||
2100 | } | ||||
2101 | } | ||||
2102 | |||||
Richard Smith | 671b321 | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 2103 | if (mergeDeclAttribute(*this, New, *i, Override)) |
John McCall | eca5d22 | 2011-03-02 04:00:57 +0000 | [diff] [blame] | 2104 | foundAny = true; |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 2105 | } |
John McCall | eca5d22 | 2011-03-02 04:00:57 +0000 | [diff] [blame] | 2106 | |
Richard Smith | 671b321 | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 2107 | if (mergeAlignedAttrs(*this, New, Old)) |
2108 | foundAny = true; | ||||
2109 | |||||
Douglas Gregor | 27c6da2 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 2110 | if (!foundAny) New->dropAttrs(); |
John McCall | eca5d22 | 2011-03-02 04:00:57 +0000 | [diff] [blame] | 2111 | } |
2112 | |||||
2113 | /// mergeParamDeclAttributes - Copy attributes from the old parameter | ||||
2114 | /// to the new one. | ||||
2115 | static void mergeParamDeclAttributes(ParmVarDecl *newDecl, | ||||
2116 | const ParmVarDecl *oldDecl, | ||||
Richard Smith | 3a2b7a1 | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 2117 | Sema &S) { |
2118 | // C++11 [dcl.attr.depend]p2: | ||||
2119 | // The first declaration of a function shall specify the | ||||
2120 | // carries_dependency attribute for its declarator-id if any declaration | ||||
2121 | // of the function specifies the carries_dependency attribute. | ||||
2122 | if (newDecl->hasAttr<CarriesDependencyAttr>() && | ||||
2123 | !oldDecl->hasAttr<CarriesDependencyAttr>()) { | ||||
2124 | S.Diag(newDecl->getAttr<CarriesDependencyAttr>()->getLocation(), | ||||
2125 | diag::err_carries_dependency_missing_on_first_decl) << 1/*Param*/; | ||||
2126 | // Find the first declaration of the parameter. | ||||
2127 | // FIXME: Should we build redeclaration chains for function parameters? | ||||
2128 | const FunctionDecl *FirstFD = | ||||
2129 | cast<FunctionDecl>(oldDecl->getDeclContext())->getFirstDeclaration(); | ||||
2130 | const ParmVarDecl *FirstVD = | ||||
2131 | FirstFD->getParamDecl(oldDecl->getFunctionScopeIndex()); | ||||
2132 | S.Diag(FirstVD->getLocation(), | ||||
2133 | diag::note_carries_dependency_missing_first_decl) << 1/*Param*/; | ||||
2134 | } | ||||
2135 | |||||
John McCall | eca5d22 | 2011-03-02 04:00:57 +0000 | [diff] [blame] | 2136 | if (!oldDecl->hasAttrs()) |
2137 | return; | ||||
2138 | |||||
2139 | bool foundAny = newDecl->hasAttrs(); | ||||
2140 | |||||
2141 | // Ensure that any moving of objects within the allocated map is | ||||
2142 | // done before we process them. | ||||
2143 | if (!foundAny) newDecl->setAttrs(AttrVec()); | ||||
2144 | |||||
2145 | for (specific_attr_iterator<InheritableParamAttr> | ||||
2146 | i = oldDecl->specific_attr_begin<InheritableParamAttr>(), | ||||
2147 | e = oldDecl->specific_attr_end<InheritableParamAttr>(); i != e; ++i) { | ||||
2148 | if (!DeclHasAttr(newDecl, *i)) { | ||||
Richard Smith | 3a2b7a1 | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 2149 | InheritableAttr *newAttr = |
2150 | cast<InheritableParamAttr>((*i)->clone(S.Context)); | ||||
John McCall | eca5d22 | 2011-03-02 04:00:57 +0000 | [diff] [blame] | 2151 | newAttr->setInherited(true); |
2152 | newDecl->addAttr(newAttr); | ||||
2153 | foundAny = true; | ||||
2154 | } | ||||
2155 | } | ||||
2156 | |||||
2157 | if (!foundAny) newDecl->dropAttrs(); | ||||
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 2158 | } |
2159 | |||||
Dan Gohman | 3c46e8d | 2010-07-26 21:25:24 +0000 | [diff] [blame] | 2160 | namespace { |
2161 | |||||
Douglas Gregor | c837656 | 2009-03-06 22:43:54 +0000 | [diff] [blame] | 2162 | /// Used in MergeFunctionDecl to keep track of function parameters in |
2163 | /// C. | ||||
2164 | struct GNUCompatibleParamWarning { | ||||
2165 | ParmVarDecl *OldParm; | ||||
2166 | ParmVarDecl *NewParm; | ||||
2167 | QualType PromotedType; | ||||
2168 | }; | ||||
2169 | |||||
Dan Gohman | 3c46e8d | 2010-07-26 21:25:24 +0000 | [diff] [blame] | 2170 | } |
Anders Carlsson | 5c478cf | 2009-12-04 22:33:25 +0000 | [diff] [blame] | 2171 | |
2172 | /// getSpecialMember - get the special member enum for a method. | ||||
Anders Carlsson | 3b8c53b | 2010-04-22 05:40:53 +0000 | [diff] [blame] | 2173 | Sema::CXXSpecialMember Sema::getSpecialMember(const CXXMethodDecl *MD) { |
Anders Carlsson | 5c478cf | 2009-12-04 22:33:25 +0000 | [diff] [blame] | 2174 | if (const CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(MD)) { |
Sean Hunt | f961ea5 | 2011-05-10 19:08:14 +0000 | [diff] [blame] | 2175 | if (Ctor->isDefaultConstructor()) |
2176 | return Sema::CXXDefaultConstructor; | ||||
Sean Hunt | 9ae60d5 | 2011-05-26 01:26:05 +0000 | [diff] [blame] | 2177 | |
2178 | if (Ctor->isCopyConstructor()) | ||||
2179 | return Sema::CXXCopyConstructor; | ||||
2180 | |||||
2181 | if (Ctor->isMoveConstructor()) | ||||
2182 | return Sema::CXXMoveConstructor; | ||||
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 2183 | } else if (isa<CXXDestructorDecl>(MD)) { |
Anders Carlsson | 5c478cf | 2009-12-04 22:33:25 +0000 | [diff] [blame] | 2184 | return Sema::CXXDestructor; |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 2185 | } else if (MD->isCopyAssignmentOperator()) { |
Sean Hunt | f961ea5 | 2011-05-10 19:08:14 +0000 | [diff] [blame] | 2186 | return Sema::CXXCopyAssignment; |
Sebastian Redl | 74e611a | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 2187 | } else if (MD->isMoveAssignmentOperator()) { |
2188 | return Sema::CXXMoveAssignment; | ||||
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 2189 | } |
Sean Hunt | f961ea5 | 2011-05-10 19:08:14 +0000 | [diff] [blame] | 2190 | |
Sean Hunt | f961ea5 | 2011-05-10 19:08:14 +0000 | [diff] [blame] | 2191 | return Sema::CXXInvalid; |
Anders Carlsson | 5c478cf | 2009-12-04 22:33:25 +0000 | [diff] [blame] | 2192 | } |
2193 | |||||
Sebastian Redl | 515ddd8 | 2010-06-09 21:17:41 +0000 | [diff] [blame] | 2194 | /// canRedefineFunction - checks if a function can be redefined. Currently, |
Charles Davis | f3f8d2a | 2010-02-18 02:00:42 +0000 | [diff] [blame] | 2195 | /// only extern inline functions can be redefined, and even then only in |
2196 | /// GNU89 mode. | ||||
2197 | static bool canRedefineFunction(const FunctionDecl *FD, | ||||
2198 | const LangOptions& LangOpts) { | ||||
Eli Friedman | eca3ed7 | 2011-06-13 23:56:42 +0000 | [diff] [blame] | 2199 | return ((FD->hasAttr<GNUInlineAttr>() || LangOpts.GNUInline) && |
2200 | !LangOpts.CPlusPlus && | ||||
Charles Davis | f3f8d2a | 2010-02-18 02:00:42 +0000 | [diff] [blame] | 2201 | FD->isInlineSpecified() && |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2202 | FD->getStorageClass() == SC_Extern); |
Charles Davis | f3f8d2a | 2010-02-18 02:00:42 +0000 | [diff] [blame] | 2203 | } |
2204 | |||||
John McCall | fb60914 | 2012-08-25 02:00:03 +0000 | [diff] [blame] | 2205 | /// Is the given calling convention the ABI default for the given |
2206 | /// declaration? | ||||
2207 | static bool isABIDefaultCC(Sema &S, CallingConv CC, FunctionDecl *D) { | ||||
2208 | CallingConv ABIDefaultCC; | ||||
2209 | if (isa<CXXMethodDecl>(D) && cast<CXXMethodDecl>(D)->isInstance()) { | ||||
2210 | ABIDefaultCC = S.Context.getDefaultCXXMethodCallConv(D->isVariadic()); | ||||
2211 | } else { | ||||
2212 | // Free C function or a static method. | ||||
2213 | ABIDefaultCC = (S.Context.getLangOpts().MRTD ? CC_X86StdCall : CC_C); | ||||
2214 | } | ||||
2215 | return ABIDefaultCC == CC; | ||||
2216 | } | ||||
2217 | |||||
Benjamin Kramer | a574c89 | 2013-02-15 12:30:38 +0000 | [diff] [blame] | 2218 | template <typename T> |
2219 | static bool haveIncompatibleLanguageLinkages(const T *Old, const T *New) { | ||||
Rafael Espindola | 950fee2 | 2013-02-14 01:18:37 +0000 | [diff] [blame] | 2220 | const DeclContext *DC = Old->getDeclContext(); |
2221 | if (DC->isRecord()) | ||||
2222 | return false; | ||||
2223 | |||||
2224 | LanguageLinkage OldLinkage = Old->getLanguageLinkage(); | ||||
Rafael Espindola | d8ffd0b | 2013-05-05 20:15:21 +0000 | [diff] [blame] | 2225 | if (OldLinkage == CXXLanguageLinkage && New->isInExternCContext()) |
Rafael Espindola | 950fee2 | 2013-02-14 01:18:37 +0000 | [diff] [blame] | 2226 | return true; |
Rafael Espindola | d8ffd0b | 2013-05-05 20:15:21 +0000 | [diff] [blame] | 2227 | if (OldLinkage == CLanguageLinkage && New->isInExternCXXContext()) |
Rafael Espindola | 950fee2 | 2013-02-14 01:18:37 +0000 | [diff] [blame] | 2228 | return true; |
2229 | return false; | ||||
2230 | } | ||||
2231 | |||||
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2232 | /// MergeFunctionDecl - We just parsed a function 'New' from |
2233 | /// declarator D which has the same name and scope as a previous | ||||
2234 | /// declaration 'Old'. Figure out how to resolve this situation, | ||||
2235 | /// merging decls or emitting diagnostics as appropriate. | ||||
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2236 | /// |
2237 | /// In C++, New and Old must be declarations that are not | ||||
2238 | /// overloaded. Use IsOverload to determine whether New and Old are | ||||
2239 | /// overloaded, and to select the Old declaration that New should be | ||||
2240 | /// merged with. | ||||
Douglas Gregor | cda9c67 | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 2241 | /// |
2242 | /// Returns true if there was an error, false otherwise. | ||||
Richard Smith | dd9459f | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 2243 | bool Sema::MergeFunctionDecl(FunctionDecl *New, Decl *OldD, Scope *S, |
2244 | bool MergeTypeWithOld) { | ||||
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2245 | // Verify the old decl was also a function. |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2246 | FunctionDecl *Old = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2247 | if (FunctionTemplateDecl *OldFunctionTemplate |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2248 | = dyn_cast<FunctionTemplateDecl>(OldD)) |
2249 | Old = OldFunctionTemplate->getTemplatedDecl(); | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2250 | else |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2251 | Old = dyn_cast<FunctionDecl>(OldD); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2252 | if (!Old) { |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 2253 | if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(OldD)) { |
John McCall | 78037ac | 2013-04-03 21:19:47 +0000 | [diff] [blame] | 2254 | if (New->getFriendObjectKind()) { |
2255 | Diag(New->getLocation(), diag::err_using_decl_friend); | ||||
2256 | Diag(Shadow->getTargetDecl()->getLocation(), | ||||
2257 | diag::note_using_decl_target); | ||||
2258 | Diag(Shadow->getUsingDecl()->getLocation(), | ||||
2259 | diag::note_using_decl) << 0; | ||||
2260 | return true; | ||||
2261 | } | ||||
2262 | |||||
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 2263 | Diag(New->getLocation(), diag::err_using_decl_conflict_reverse); |
2264 | Diag(Shadow->getTargetDecl()->getLocation(), | ||||
2265 | diag::note_using_decl_target); | ||||
2266 | Diag(Shadow->getUsingDecl()->getLocation(), | ||||
2267 | diag::note_using_decl) << 0; | ||||
2268 | return true; | ||||
2269 | } | ||||
2270 | |||||
Chris Lattner | 5dc266a | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 2271 | Diag(New->getLocation(), diag::err_redefinition_different_kind) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 2272 | << New->getDeclName(); |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 2273 | Diag(OldD->getLocation(), diag::note_previous_definition); |
Douglas Gregor | cda9c67 | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 2274 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2275 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2276 | |
David Majnemer | bcd0650 | 2013-07-07 23:49:50 +0000 | [diff] [blame] | 2277 | // If the old declaration is invalid, just give up here. |
2278 | if (Old->isInvalidDecl()) | ||||
2279 | return true; | ||||
2280 | |||||
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2281 | // Determine whether the previous declaration was a definition, |
2282 | // implicit declaration, or a declaration. | ||||
2283 | diag::kind PrevDiag; | ||||
2284 | if (Old->isThisDeclarationADefinition()) | ||||
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 2285 | PrevDiag = diag::note_previous_definition; |
Douglas Gregor | cda9c67 | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 2286 | else if (Old->isImplicit()) |
2287 | PrevDiag = diag::note_previous_implicit_declaration; | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2288 | else |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 2289 | PrevDiag = diag::note_previous_declaration; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2290 | |
Chris Lattner | 8bcfc5b | 2008-04-06 23:10:54 +0000 | [diff] [blame] | 2291 | QualType OldQType = Context.getCanonicalType(Old->getType()); |
2292 | QualType NewQType = Context.getCanonicalType(New->getType()); | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2293 | |
Charles Davis | f3f8d2a | 2010-02-18 02:00:42 +0000 | [diff] [blame] | 2294 | // Don't complain about this if we're in GNU89 mode and the old function |
2295 | // is an extern inline function. | ||||
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 2296 | // Don't complain about specializations. They are not supposed to have |
2297 | // storage classes. | ||||
Douglas Gregor | 04495c8 | 2009-02-24 01:23:02 +0000 | [diff] [blame] | 2298 | if (!isa<CXXMethodDecl>(New) && !isa<CXXMethodDecl>(Old) && |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2299 | New->getStorageClass() == SC_Static && |
Rafael Espindola | 181e3ec | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 2300 | Old->hasExternalFormalLinkage() && |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 2301 | !New->getTemplateSpecializationInfo() && |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2302 | !canRedefineFunction(Old, getLangOpts())) { |
2303 | if (getLangOpts().MicrosoftExt) { | ||||
Francois Pichet | 4bada2e | 2011-04-22 19:50:06 +0000 | [diff] [blame] | 2304 | Diag(New->getLocation(), diag::warn_static_non_static) << New; |
2305 | Diag(Old->getLocation(), PrevDiag); | ||||
2306 | } else { | ||||
2307 | Diag(New->getLocation(), diag::err_static_non_static) << New; | ||||
2308 | Diag(Old->getLocation(), PrevDiag); | ||||
2309 | return true; | ||||
2310 | } | ||||
Douglas Gregor | 04495c8 | 2009-02-24 01:23:02 +0000 | [diff] [blame] | 2311 | } |
2312 | |||||
John McCall | f82b4e8 | 2010-02-04 05:44:44 +0000 | [diff] [blame] | 2313 | // If a function is first declared with a calling convention, but is |
2314 | // later declared or defined without one, the second decl assumes the | ||||
2315 | // calling convention of the first. | ||||
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 | // | ||||
John McCall | f82b4e8 | 2010-02-04 05:44:44 +0000 | [diff] [blame] | 2320 | // For the new decl, we have to look at the NON-canonical type to tell the |
2321 | // difference between a function that really doesn't have a calling | ||||
2322 | // convention and one that is declared cdecl. That's because in | ||||
2323 | // canonicalization (see ASTContext.cpp), cdecl is canonicalized away | ||||
2324 | // because it is the default calling convention. | ||||
2325 | // | ||||
2326 | // Note also that we DO NOT return at this point, because we still have | ||||
2327 | // other tests to run. | ||||
John McCall | e6a365d | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 2328 | const FunctionType *OldType = cast<FunctionType>(OldQType); |
John McCall | f82b4e8 | 2010-02-04 05:44:44 +0000 | [diff] [blame] | 2329 | const FunctionType *NewType = New->getType()->getAs<FunctionType>(); |
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 | if (OldTypeInfo.getCC() == NewTypeInfo.getCC()) { |
2334 | // Fast path: nothing to do. | ||||
2335 | |||||
2336 | // Inherit the CC from the previous declaration if it was specified | ||||
2337 | // there but not here. | ||||
2338 | } else if (NewTypeInfo.getCC() == CC_Default) { | ||||
John McCall | e6a365d | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 2339 | NewTypeInfo = NewTypeInfo.withCallingConv(OldTypeInfo.getCC()); |
2340 | RequiresAdjustment = true; | ||||
John McCall | fb60914 | 2012-08-25 02:00:03 +0000 | [diff] [blame] | 2341 | |
2342 | // Don't complain about mismatches when the default CC is | ||||
Timur Iskhodzhanov | 33b52ad | 2013-02-22 12:42:50 +0000 | [diff] [blame] | 2343 | // effectively the same as the explict one. Only Old decl contains correct |
2344 | // information about storage class of CXXMethod. | ||||
John McCall | fb60914 | 2012-08-25 02:00:03 +0000 | [diff] [blame] | 2345 | } else if (OldTypeInfo.getCC() == CC_Default && |
Timur Iskhodzhanov | 33b52ad | 2013-02-22 12:42:50 +0000 | [diff] [blame] | 2346 | isABIDefaultCC(*this, NewTypeInfo.getCC(), Old)) { |
John McCall | fb60914 | 2012-08-25 02:00:03 +0000 | [diff] [blame] | 2347 | NewTypeInfo = NewTypeInfo.withCallingConv(OldTypeInfo.getCC()); |
2348 | RequiresAdjustment = true; | ||||
2349 | |||||
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 2350 | } else if (!Context.isSameCallConv(OldTypeInfo.getCC(), |
2351 | NewTypeInfo.getCC())) { | ||||
John McCall | f82b4e8 | 2010-02-04 05:44:44 +0000 | [diff] [blame] | 2352 | // Calling conventions really aren't compatible, so complain. |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 2353 | Diag(New->getLocation(), diag::err_cconv_change) |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 2354 | << FunctionType::getNameForCallConv(NewTypeInfo.getCC()) |
2355 | << (OldTypeInfo.getCC() == CC_Default) | ||||
2356 | << (OldTypeInfo.getCC() == CC_Default ? "" : | ||||
2357 | FunctionType::getNameForCallConv(OldTypeInfo.getCC())); | ||||
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 2358 | Diag(Old->getLocation(), diag::note_previous_declaration); |
John McCall | f82b4e8 | 2010-02-04 05:44:44 +0000 | [diff] [blame] | 2359 | return true; |
2360 | } | ||||
2361 | |||||
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 2362 | // FIXME: diagnose the other way around? |
John McCall | e6a365d | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 2363 | if (OldTypeInfo.getNoReturn() && !NewTypeInfo.getNoReturn()) { |
2364 | NewTypeInfo = NewTypeInfo.withNoReturn(true); | ||||
2365 | RequiresAdjustment = true; | ||||
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 2366 | } |
2367 | |||||
Douglas Gregor | d2c6490 | 2010-06-18 21:30:25 +0000 | [diff] [blame] | 2368 | // Merge regparm attribute. |
Eli Friedman | a49218e | 2011-04-09 08:18:08 +0000 | [diff] [blame] | 2369 | if (OldTypeInfo.getHasRegParm() != NewTypeInfo.getHasRegParm() || |
2370 | OldTypeInfo.getRegParm() != NewTypeInfo.getRegParm()) { | ||||
2371 | if (NewTypeInfo.getHasRegParm()) { | ||||
Douglas Gregor | d2c6490 | 2010-06-18 21:30:25 +0000 | [diff] [blame] | 2372 | Diag(New->getLocation(), diag::err_regparm_mismatch) |
2373 | << NewType->getRegParmType() | ||||
2374 | << OldType->getRegParmType(); | ||||
2375 | Diag(Old->getLocation(), diag::note_previous_declaration); | ||||
2376 | return true; | ||||
2377 | } | ||||
John McCall | e6a365d | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 2378 | |
2379 | NewTypeInfo = NewTypeInfo.withRegParm(OldTypeInfo.getRegParm()); | ||||
2380 | RequiresAdjustment = true; | ||||
2381 | } | ||||
2382 | |||||
Douglas Gregor | cb1c9c3 | 2011-10-14 15:55:40 +0000 | [diff] [blame] | 2383 | // Merge ns_returns_retained attribute. |
2384 | if (OldTypeInfo.getProducesResult() != NewTypeInfo.getProducesResult()) { | ||||
2385 | if (NewTypeInfo.getProducesResult()) { | ||||
2386 | Diag(New->getLocation(), diag::err_returns_retained_mismatch); | ||||
2387 | Diag(Old->getLocation(), diag::note_previous_declaration); | ||||
2388 | return true; | ||||
2389 | } | ||||
2390 | |||||
2391 | NewTypeInfo = NewTypeInfo.withProducesResult(true); | ||||
2392 | RequiresAdjustment = true; | ||||
2393 | } | ||||
2394 | |||||
John McCall | e6a365d | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 2395 | if (RequiresAdjustment) { |
2396 | NewType = Context.adjustFunctionType(NewType, NewTypeInfo); | ||||
2397 | New->setType(QualType(NewType, 0)); | ||||
2398 | NewQType = Context.getCanonicalType(New->getType()); | ||||
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 | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2436 | if (!Context.hasSameType(OldDeclaredReturnType, NewDeclaredReturnType)) { |
2437 | if (NewDeclaredReturnType->isObjCObjectPointerType() && | ||||
2438 | OldDeclaredReturnType->isObjCObjectPointerType()) | ||||
Fariborz Jahanian | 2390a72 | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 2439 | ResQT = Context.mergeObjCGCQualifiers(NewQType, OldQType); |
2440 | if (ResQT.isNull()) { | ||||
Argyrios Kyrtzidis | 1de34dd | 2011-02-05 05:54:49 +0000 | [diff] [blame] | 2441 | if (New->isCXXClassMember() && New->isOutOfLine()) |
2442 | Diag(New->getLocation(), | ||||
2443 | diag::err_member_def_does_not_match_ret_type) << New; | ||||
2444 | else | ||||
2445 | Diag(New->getLocation(), diag::err_ovl_diff_return_type); | ||||
Fariborz Jahanian | 2390a72 | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 2446 | Diag(Old->getLocation(), PrevDiag) << Old << Old->getType(); |
2447 | return true; | ||||
2448 | } | ||||
2449 | else | ||||
2450 | NewQType = ResQT; | ||||
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2451 | } |
2452 | |||||
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2453 | QualType OldReturnType = OldType->getResultType(); |
2454 | QualType NewReturnType = cast<FunctionType>(NewQType)->getResultType(); | ||||
2455 | if (OldReturnType != NewReturnType) { | ||||
2456 | // If this function has a deduced return type and has already been | ||||
2457 | // defined, copy the deduced value from the old declaration. | ||||
2458 | AutoType *OldAT = Old->getResultType()->getContainedAutoType(); | ||||
2459 | if (OldAT && OldAT->isDeduced()) { | ||||
Richard Smith | 37e849a | 2013-08-14 20:16:31 +0000 | [diff] [blame] | 2460 | New->setType( |
2461 | SubstAutoType(New->getType(), | ||||
2462 | OldAT->isDependentType() ? Context.DependentTy | ||||
2463 | : OldAT->getDeducedType())); | ||||
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2464 | NewQType = Context.getCanonicalType( |
Richard Smith | 37e849a | 2013-08-14 20:16:31 +0000 | [diff] [blame] | 2465 | SubstAutoType(NewQType, |
2466 | OldAT->isDependentType() ? Context.DependentTy | ||||
2467 | : OldAT->getDeducedType())); | ||||
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2468 | } |
2469 | } | ||||
2470 | |||||
2471 | const CXXMethodDecl *OldMethod = dyn_cast<CXXMethodDecl>(Old); | ||||
2472 | CXXMethodDecl *NewMethod = dyn_cast<CXXMethodDecl>(New); | ||||
Anders Carlsson | 5c478cf | 2009-12-04 22:33:25 +0000 | [diff] [blame] | 2473 | if (OldMethod && NewMethod) { |
John McCall | 3d04336 | 2010-04-13 07:45:41 +0000 | [diff] [blame] | 2474 | // Preserve triviality. |
2475 | NewMethod->setTrivial(OldMethod->isTrivial()); | ||||
Francois Pichet | e1e96a6 | 2011-05-14 19:17:07 +0000 | [diff] [blame] | 2476 | |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 2477 | // MSVC allows explicit template specialization at class scope: |
2478 | // 2 CXMethodDecls referring to the same function will be injected. | ||||
2479 | // We don't want a redeclartion error. | ||||
2480 | bool IsClassScopeExplicitSpecialization = | ||||
2481 | OldMethod->isFunctionTemplateSpecialization() && | ||||
2482 | NewMethod->isFunctionTemplateSpecialization(); | ||||
John McCall | 3d04336 | 2010-04-13 07:45:41 +0000 | [diff] [blame] | 2483 | bool isFriend = NewMethod->getFriendObjectKind(); |
2484 | |||||
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 2485 | if (!isFriend && NewMethod->getLexicalDeclContext()->isRecord() && |
2486 | !IsClassScopeExplicitSpecialization) { | ||||
Anders Carlsson | 5c478cf | 2009-12-04 22:33:25 +0000 | [diff] [blame] | 2487 | // -- Member function declarations with the same name and the |
2488 | // same parameter types cannot be overloaded if any of them | ||||
2489 | // is a static member function declaration. | ||||
Eli Friedman | fa0d3f8 | 2013-06-19 22:43:55 +0000 | [diff] [blame] | 2490 | if (OldMethod->isStatic() != NewMethod->isStatic()) { |
Anders Carlsson | 5c478cf | 2009-12-04 22:33:25 +0000 | [diff] [blame] | 2491 | Diag(New->getLocation(), diag::err_ovl_static_nonstatic_member); |
2492 | Diag(Old->getLocation(), PrevDiag) << Old << Old->getType(); | ||||
2493 | return true; | ||||
2494 | } | ||||
Richard Smith | 838925d | 2012-07-13 04:12:04 +0000 | [diff] [blame] | 2495 | |
Anders Carlsson | 5c478cf | 2009-12-04 22:33:25 +0000 | [diff] [blame] | 2496 | // C++ [class.mem]p1: |
2497 | // [...] A member shall not be declared twice in the | ||||
2498 | // member-specification, except that a nested class or member | ||||
2499 | // class template can be declared and then later defined. | ||||
Richard Smith | 838925d | 2012-07-13 04:12:04 +0000 | [diff] [blame] | 2500 | if (ActiveTemplateInstantiations.empty()) { |
2501 | unsigned NewDiag; | ||||
2502 | if (isa<CXXConstructorDecl>(OldMethod)) | ||||
2503 | NewDiag = diag::err_constructor_redeclared; | ||||
2504 | else if (isa<CXXDestructorDecl>(NewMethod)) | ||||
2505 | NewDiag = diag::err_destructor_redeclared; | ||||
2506 | else if (isa<CXXConversionDecl>(NewMethod)) | ||||
2507 | NewDiag = diag::err_conv_function_redeclared; | ||||
2508 | else | ||||
2509 | NewDiag = diag::err_member_redeclared; | ||||
Anders Carlsson | 5c478cf | 2009-12-04 22:33:25 +0000 | [diff] [blame] | 2510 | |
Richard Smith | 838925d | 2012-07-13 04:12:04 +0000 | [diff] [blame] | 2511 | Diag(New->getLocation(), NewDiag); |
2512 | } else { | ||||
2513 | Diag(New->getLocation(), diag::err_member_redeclared_in_instantiation) | ||||
2514 | << New << New->getType(); | ||||
2515 | } | ||||
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 2516 | Diag(Old->getLocation(), PrevDiag) << Old << Old->getType(); |
John McCall | 3d04336 | 2010-04-13 07:45:41 +0000 | [diff] [blame] | 2517 | |
2518 | // Complain if this is an explicit declaration of a special | ||||
2519 | // member that was initially declared implicitly. | ||||
2520 | // | ||||
2521 | // As an exception, it's okay to befriend such methods in order | ||||
2522 | // to permit the implicit constructor/destructor/operator calls. | ||||
2523 | } else if (OldMethod->isImplicit()) { | ||||
2524 | if (isFriend) { | ||||
2525 | NewMethod->setImplicit(); | ||||
2526 | } else { | ||||
Anders Carlsson | 5c478cf | 2009-12-04 22:33:25 +0000 | [diff] [blame] | 2527 | Diag(NewMethod->getLocation(), |
2528 | diag::err_definition_of_implicitly_declared_member) | ||||
Anders Carlsson | 3b8c53b | 2010-04-22 05:40:53 +0000 | [diff] [blame] | 2529 | << New << getSpecialMember(OldMethod); |
Anders Carlsson | 5c478cf | 2009-12-04 22:33:25 +0000 | [diff] [blame] | 2530 | return true; |
2531 | } | ||||
Richard Smith | f4fe843 | 2012-06-08 01:30:54 +0000 | [diff] [blame] | 2532 | } else if (OldMethod->isExplicitlyDefaulted() && !isFriend) { |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 2533 | Diag(NewMethod->getLocation(), |
2534 | diag::err_definition_of_explicitly_defaulted_member) | ||||
2535 | << getSpecialMember(OldMethod); | ||||
2536 | return true; | ||||
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2537 | } |
2538 | } | ||||
2539 | |||||
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 2540 | // C++11 [dcl.attr.noreturn]p1: |
2541 | // The first declaration of a function shall specify the noreturn | ||||
2542 | // attribute if any declaration of that function specifies the noreturn | ||||
2543 | // attribute. | ||||
2544 | if (New->hasAttr<CXX11NoReturnAttr>() && | ||||
2545 | !Old->hasAttr<CXX11NoReturnAttr>()) { | ||||
2546 | Diag(New->getAttr<CXX11NoReturnAttr>()->getLocation(), | ||||
2547 | diag::err_noreturn_missing_on_first_decl); | ||||
2548 | Diag(Old->getFirstDeclaration()->getLocation(), | ||||
2549 | diag::note_noreturn_missing_first_decl); | ||||
2550 | } | ||||
2551 | |||||
Richard Smith | 3a2b7a1 | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 2552 | // C++11 [dcl.attr.depend]p2: |
2553 | // The first declaration of a function shall specify the | ||||
2554 | // carries_dependency attribute for its declarator-id if any declaration | ||||
2555 | // of the function specifies the carries_dependency attribute. | ||||
2556 | if (New->hasAttr<CarriesDependencyAttr>() && | ||||
2557 | !Old->hasAttr<CarriesDependencyAttr>()) { | ||||
2558 | Diag(New->getAttr<CarriesDependencyAttr>()->getLocation(), | ||||
2559 | diag::err_carries_dependency_missing_on_first_decl) << 0/*Function*/; | ||||
2560 | Diag(Old->getFirstDeclaration()->getLocation(), | ||||
2561 | diag::note_carries_dependency_missing_first_decl) << 0/*Function*/; | ||||
2562 | } | ||||
2563 | |||||
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2564 | // (C++98 8.3.5p3): |
2565 | // All declarations for a function shall agree exactly in both the | ||||
2566 | // return type and the parameter-type-list. | ||||
John McCall | e6a365d | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 2567 | // We also want to respect all the extended bits except noreturn. |
2568 | |||||
2569 | // noreturn should now match unless the old type info didn't have it. | ||||
2570 | QualType OldQTypeForComparison = OldQType; | ||||
2571 | if (!OldTypeInfo.getNoReturn() && NewTypeInfo.getNoReturn()) { | ||||
2572 | assert(OldQType == QualType(OldType, 0)); | ||||
2573 | const FunctionType *OldTypeForComparison | ||||
2574 | = Context.adjustFunctionType(OldType, OldTypeInfo.withNoReturn(true)); | ||||
2575 | OldQTypeForComparison = QualType(OldTypeForComparison, 0); | ||||
2576 | assert(OldQTypeForComparison.isCanonical()); | ||||
2577 | } | ||||
2578 | |||||
Rafael Espindola | 950fee2 | 2013-02-14 01:18:37 +0000 | [diff] [blame] | 2579 | if (haveIncompatibleLanguageLinkages(Old, New)) { |
Rafael Espindola | e57e3d3 | 2012-12-27 03:56:20 +0000 | [diff] [blame] | 2580 | Diag(New->getLocation(), diag::err_different_language_linkage) << New; |
2581 | Diag(Old->getLocation(), PrevDiag); | ||||
2582 | return true; | ||||
2583 | } | ||||
2584 | |||||
John McCall | e6a365d | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 2585 | if (OldQTypeForComparison == NewQType) |
Richard Smith | dd9459f | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 2586 | return MergeCompatibleFunctionDecls(New, Old, S, MergeTypeWithOld); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2587 | |
2588 | // Fall through for conflicting redeclarations and redefinitions. | ||||
Douglas Gregor | f009795 | 2008-04-21 02:02:58 +0000 | [diff] [blame] | 2589 | } |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2590 | |
2591 | // C: Function types need to be compatible, not identical. This handles | ||||
Steve Naroff | adbbd0c | 2008-01-14 20:51:29 +0000 | [diff] [blame] | 2592 | // duplicate function decls like "void f(int); void f(enum X);" properly. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2593 | if (!getLangOpts().CPlusPlus && |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2594 | Context.typesAreCompatible(OldQType, NewQType)) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2595 | const FunctionType *OldFuncType = OldQType->getAs<FunctionType>(); |
2596 | const FunctionType *NewFuncType = NewQType->getAs<FunctionType>(); | ||||
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2597 | const FunctionProtoType *OldProto = 0; |
Richard Smith | dd9459f | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 2598 | if (MergeTypeWithOld && isa<FunctionNoProtoType>(NewFuncType) && |
Douglas Gregor | c837656 | 2009-03-06 22:43:54 +0000 | [diff] [blame] | 2599 | (OldProto = dyn_cast<FunctionProtoType>(OldFuncType))) { |
Douglas Gregor | 6871981 | 2009-02-16 18:20:44 +0000 | [diff] [blame] | 2600 | // The old declaration provided a function prototype, but the |
2601 | // new declaration does not. Merge in the prototype. | ||||
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2602 | assert(!OldProto->hasExceptionSpec() && "Exception spec in C"); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2603 | SmallVector<QualType, 16> ParamTypes(OldProto->arg_type_begin(), |
Douglas Gregor | 6871981 | 2009-02-16 18:20:44 +0000 | [diff] [blame] | 2604 | OldProto->arg_type_end()); |
2605 | NewQType = Context.getFunctionType(NewFuncType->getResultType(), | ||||
Jordan Rose | bea522f | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2606 | ParamTypes, |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2607 | OldProto->getExtProtoInfo()); |
Douglas Gregor | 6871981 | 2009-02-16 18:20:44 +0000 | [diff] [blame] | 2608 | New->setType(NewQType); |
Anders Carlsson | a75e853 | 2009-05-14 21:46:00 +0000 | [diff] [blame] | 2609 | New->setHasInheritedPrototype(); |
Douglas Gregor | 450da98 | 2009-02-16 20:58:07 +0000 | [diff] [blame] | 2610 | |
2611 | // Synthesize a parameter for each argument type. | ||||
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2612 | SmallVector<ParmVarDecl*, 16> Params; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2613 | for (FunctionProtoType::arg_type_iterator |
2614 | ParamType = OldProto->arg_type_begin(), | ||||
Douglas Gregor | 450da98 | 2009-02-16 20:58:07 +0000 | [diff] [blame] | 2615 | ParamEnd = OldProto->arg_type_end(); |
2616 | ParamType != ParamEnd; ++ParamType) { | ||||
2617 | ParmVarDecl *Param = ParmVarDecl::Create(Context, New, | ||||
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2618 | SourceLocation(), |
Douglas Gregor | 450da98 | 2009-02-16 20:58:07 +0000 | [diff] [blame] | 2619 | SourceLocation(), 0, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2620 | *ParamType, /*TInfo=*/0, |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 2621 | SC_None, |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 2622 | 0); |
John McCall | fb44de9 | 2011-05-01 22:35:37 +0000 | [diff] [blame] | 2623 | Param->setScopeInfo(0, Params.size()); |
Douglas Gregor | 450da98 | 2009-02-16 20:58:07 +0000 | [diff] [blame] | 2624 | Param->setImplicit(); |
2625 | Params.push_back(Param); | ||||
2626 | } | ||||
2627 | |||||
David Blaikie | 4278c65 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 2628 | New->setParams(Params); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2629 | } |
Douglas Gregor | 6871981 | 2009-02-16 18:20:44 +0000 | [diff] [blame] | 2630 | |
Richard Smith | dd9459f | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 2631 | return MergeCompatibleFunctionDecls(New, Old, S, MergeTypeWithOld); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2632 | } |
Chris Lattner | e3995fe | 2007-11-06 06:07:26 +0000 | [diff] [blame] | 2633 | |
Douglas Gregor | c837656 | 2009-03-06 22:43:54 +0000 | [diff] [blame] | 2634 | // GNU C permits a K&R definition to follow a prototype declaration |
2635 | // if the declared types of the parameters in the K&R definition | ||||
2636 | // match the types in the prototype declaration, even when the | ||||
2637 | // promoted types of the parameters from the K&R definition differ | ||||
2638 | // from the types in the prototype. GCC then keeps the types from | ||||
2639 | // the prototype. | ||||
Eli Friedman | bc4e29f | 2009-06-01 09:24:59 +0000 | [diff] [blame] | 2640 | // |
2641 | // If a variadic prototype is followed by a non-variadic K&R definition, | ||||
2642 | // the K&R definition becomes variadic. This is sort of an edge case, but | ||||
2643 | // it's legal per the standard depending on how you read C99 6.7.5.3p15 and | ||||
2644 | // C99 6.9.1p8. | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2645 | if (!getLangOpts().CPlusPlus && |
Douglas Gregor | c837656 | 2009-03-06 22:43:54 +0000 | [diff] [blame] | 2646 | Old->hasPrototype() && !New->hasPrototype() && |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2647 | New->getType()->getAs<FunctionProtoType>() && |
Douglas Gregor | c837656 | 2009-03-06 22:43:54 +0000 | [diff] [blame] | 2648 | Old->getNumParams() == New->getNumParams()) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2649 | SmallVector<QualType, 16> ArgTypes; |
2650 | SmallVector<GNUCompatibleParamWarning, 16> Warnings; | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2651 | const FunctionProtoType *OldProto |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2652 | = Old->getType()->getAs<FunctionProtoType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2653 | const FunctionProtoType *NewProto |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2654 | = New->getType()->getAs<FunctionProtoType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2655 | |
Douglas Gregor | c837656 | 2009-03-06 22:43:54 +0000 | [diff] [blame] | 2656 | // Determine whether this is the GNU C extension. |
Eli Friedman | bc4e29f | 2009-06-01 09:24:59 +0000 | [diff] [blame] | 2657 | QualType MergedReturn = Context.mergeTypes(OldProto->getResultType(), |
2658 | NewProto->getResultType()); | ||||
2659 | bool LooseCompatible = !MergedReturn.isNull(); | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2660 | for (unsigned Idx = 0, End = Old->getNumParams(); |
Eli Friedman | bc4e29f | 2009-06-01 09:24:59 +0000 | [diff] [blame] | 2661 | LooseCompatible && Idx != End; ++Idx) { |
Douglas Gregor | c837656 | 2009-03-06 22:43:54 +0000 | [diff] [blame] | 2662 | ParmVarDecl *OldParm = Old->getParamDecl(Idx); |
2663 | ParmVarDecl *NewParm = New->getParamDecl(Idx); | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2664 | if (Context.typesAreCompatible(OldParm->getType(), |
Douglas Gregor | c837656 | 2009-03-06 22:43:54 +0000 | [diff] [blame] | 2665 | NewProto->getArgType(Idx))) { |
2666 | ArgTypes.push_back(NewParm->getType()); | ||||
2667 | } else if (Context.typesAreCompatible(OldParm->getType(), | ||||
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 2668 | NewParm->getType(), |
2669 | /*CompareUnqualified=*/true)) { | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2670 | GNUCompatibleParamWarning Warn |
Douglas Gregor | c837656 | 2009-03-06 22:43:54 +0000 | [diff] [blame] | 2671 | = { OldParm, NewParm, NewProto->getArgType(Idx) }; |
2672 | Warnings.push_back(Warn); | ||||
2673 | ArgTypes.push_back(NewParm->getType()); | ||||
2674 | } else | ||||
Eli Friedman | bc4e29f | 2009-06-01 09:24:59 +0000 | [diff] [blame] | 2675 | LooseCompatible = false; |
Douglas Gregor | c837656 | 2009-03-06 22:43:54 +0000 | [diff] [blame] | 2676 | } |
2677 | |||||
Eli Friedman | bc4e29f | 2009-06-01 09:24:59 +0000 | [diff] [blame] | 2678 | if (LooseCompatible) { |
Douglas Gregor | c837656 | 2009-03-06 22:43:54 +0000 | [diff] [blame] | 2679 | for (unsigned Warn = 0; Warn < Warnings.size(); ++Warn) { |
2680 | Diag(Warnings[Warn].NewParm->getLocation(), | ||||
2681 | diag::ext_param_promoted_not_compatible_with_prototype) | ||||
2682 | << Warnings[Warn].PromotedType | ||||
2683 | << Warnings[Warn].OldParm->getType(); | ||||
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 2684 | if (Warnings[Warn].OldParm->getLocation().isValid()) |
2685 | Diag(Warnings[Warn].OldParm->getLocation(), | ||||
2686 | diag::note_previous_declaration); | ||||
Douglas Gregor | c837656 | 2009-03-06 22:43:54 +0000 | [diff] [blame] | 2687 | } |
2688 | |||||
Richard Smith | dd9459f | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 2689 | if (MergeTypeWithOld) |
2690 | New->setType(Context.getFunctionType(MergedReturn, ArgTypes, | ||||
2691 | OldProto->getExtProtoInfo())); | ||||
2692 | return MergeCompatibleFunctionDecls(New, Old, S, MergeTypeWithOld); | ||||
Douglas Gregor | c837656 | 2009-03-06 22:43:54 +0000 | [diff] [blame] | 2693 | } |
2694 | |||||
2695 | // Fall through to diagnose conflicting types. | ||||
2696 | } | ||||
2697 | |||||
John McCall | 088831d | 2013-04-14 08:50:55 +0000 | [diff] [blame] | 2698 | // A function that has already been declared has been redeclared or |
2699 | // defined with a different type; show an appropriate diagnostic. | ||||
2700 | |||||
2701 | // If the previous declaration was an implicitly-generated builtin | ||||
2702 | // declaration, then at the very least we should use a specialized note. | ||||
2703 | unsigned BuiltinID; | ||||
2704 | if (Old->isImplicit() && (BuiltinID = Old->getBuiltinID())) { | ||||
2705 | // If it's actually a library-defined builtin function like 'malloc' | ||||
2706 | // or 'printf', just warn about the incompatible redeclaration. | ||||
Douglas Gregor | cda9c67 | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 2707 | if (Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)) { |
Douglas Gregor | cda9c67 | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 2708 | Diag(New->getLocation(), diag::warn_redecl_library_builtin) << New; |
2709 | Diag(Old->getLocation(), diag::note_previous_builtin_declaration) | ||||
2710 | << Old << Old->getType(); | ||||
John McCall | 088831d | 2013-04-14 08:50:55 +0000 | [diff] [blame] | 2711 | |
2712 | // If this is a global redeclaration, just forget hereafter | ||||
2713 | // about the "builtin-ness" of the function. | ||||
2714 | // | ||||
2715 | // Doing this for local extern declarations is problematic. If | ||||
2716 | // the builtin declaration remains visible, a second invalid | ||||
2717 | // local declaration will produce a hard error; if it doesn't | ||||
2718 | // remain visible, a single bogus local redeclaration (which is | ||||
2719 | // actually only a warning) could break all the downstream code. | ||||
2720 | if (!New->getDeclContext()->isFunctionOrMethod()) | ||||
2721 | New->getIdentifier()->setBuiltinID(Builtin::NotBuiltin); | ||||
2722 | |||||
Douglas Gregor | 374e156 | 2009-03-23 17:47:24 +0000 | [diff] [blame] | 2723 | return false; |
Douglas Gregor | cda9c67 | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 2724 | } |
Steve Naroff | 837618c | 2008-01-16 15:01:34 +0000 | [diff] [blame] | 2725 | |
Douglas Gregor | cda9c67 | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 2726 | PrevDiag = diag::note_previous_builtin_declaration; |
2727 | } | ||||
2728 | |||||
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2729 | Diag(New->getLocation(), diag::err_conflicting_types) << New->getDeclName(); |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 2730 | Diag(Old->getLocation(), PrevDiag) << Old << Old->getType(); |
Douglas Gregor | cda9c67 | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 2731 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2732 | } |
2733 | |||||
Douglas Gregor | 04495c8 | 2009-02-24 01:23:02 +0000 | [diff] [blame] | 2734 | /// \brief Completes the merge of two function declarations that are |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2735 | /// known to be compatible. |
Douglas Gregor | 04495c8 | 2009-02-24 01:23:02 +0000 | [diff] [blame] | 2736 | /// |
2737 | /// This routine handles the merging of attributes and other | ||||
2738 | /// properties of function declarations form the old declaration to | ||||
2739 | /// the new declaration, once we know that New is in fact a | ||||
2740 | /// redeclaration of Old. | ||||
2741 | /// | ||||
2742 | /// \returns false | ||||
James Molloy | 9cda03f | 2012-03-13 08:55:35 +0000 | [diff] [blame] | 2743 | bool Sema::MergeCompatibleFunctionDecls(FunctionDecl *New, FunctionDecl *Old, |
Richard Smith | dd9459f | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 2744 | Scope *S, bool MergeTypeWithOld) { |
Douglas Gregor | 04495c8 | 2009-02-24 01:23:02 +0000 | [diff] [blame] | 2745 | // Merge the attributes |
Douglas Gregor | 27c6da2 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 2746 | mergeDeclAttributes(New, Old); |
Douglas Gregor | 04495c8 | 2009-02-24 01:23:02 +0000 | [diff] [blame] | 2747 | |
Douglas Gregor | 04495c8 | 2009-02-24 01:23:02 +0000 | [diff] [blame] | 2748 | // Merge "pure" flag. |
2749 | if (Old->isPure()) | ||||
2750 | New->setPure(); | ||||
2751 | |||||
Rafael Espindola | 8dbf697 | 2012-11-25 14:07:59 +0000 | [diff] [blame] | 2752 | // Merge "used" flag. |
2753 | if (Old->isUsed(false)) | ||||
2754 | New->setUsed(); | ||||
2755 | |||||
John McCall | eca5d22 | 2011-03-02 04:00:57 +0000 | [diff] [blame] | 2756 | // Merge attributes from the parameters. These can mismatch with K&R |
2757 | // declarations. | ||||
2758 | if (New->getNumParams() == Old->getNumParams()) | ||||
2759 | for (unsigned i = 0, e = New->getNumParams(); i != e; ++i) | ||||
2760 | mergeParamDeclAttributes(New->getParamDecl(i), Old->getParamDecl(i), | ||||
Richard Smith | 3a2b7a1 | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 2761 | *this); |
John McCall | eca5d22 | 2011-03-02 04:00:57 +0000 | [diff] [blame] | 2762 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2763 | if (getLangOpts().CPlusPlus) |
James Molloy | 9cda03f | 2012-03-13 08:55:35 +0000 | [diff] [blame] | 2764 | return MergeCXXFunctionDecl(New, Old, S); |
Douglas Gregor | 04495c8 | 2009-02-24 01:23:02 +0000 | [diff] [blame] | 2765 | |
Rafael Espindola | 8b8a09e | 2012-11-29 16:09:03 +0000 | [diff] [blame] | 2766 | // 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] | 2767 | // and argument types. Per C11 6.2.7/4, only update the type if the old decl |
2768 | // was visible. | ||||
Rafael Espindola | 8b8a09e | 2012-11-29 16:09:03 +0000 | [diff] [blame] | 2769 | QualType Merged = Context.mergeTypes(Old->getType(), New->getType()); |
Richard Smith | dd9459f | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 2770 | if (!Merged.isNull() && MergeTypeWithOld) |
Rafael Espindola | 8b8a09e | 2012-11-29 16:09:03 +0000 | [diff] [blame] | 2771 | New->setType(Merged); |
2772 | |||||
Douglas Gregor | 04495c8 | 2009-02-24 01:23:02 +0000 | [diff] [blame] | 2773 | return false; |
2774 | } | ||||
2775 | |||||
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2776 | |
John McCall | eca5d22 | 2011-03-02 04:00:57 +0000 | [diff] [blame] | 2777 | void Sema::mergeObjCMethodDecls(ObjCMethodDecl *newMethod, |
Douglas Gregor | 27c6da2 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 2778 | ObjCMethodDecl *oldMethod) { |
John McCall | 6c2c250 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 2779 | |
Fariborz Jahanian | 1ea6744 | 2012-06-05 21:14:46 +0000 | [diff] [blame] | 2780 | // Merge the attributes, including deprecated/unavailable |
Ted Kremenek | cb34439 | 2013-04-06 00:34:27 +0000 | [diff] [blame] | 2781 | AvailabilityMergeKind MergeKind = |
2782 | isa<ObjCImplDecl>(newMethod->getDeclContext()) ? AMK_Redeclaration | ||||
2783 | : AMK_Override; | ||||
2784 | mergeDeclAttributes(newMethod, oldMethod, MergeKind); | ||||
John McCall | eca5d22 | 2011-03-02 04:00:57 +0000 | [diff] [blame] | 2785 | |
2786 | // Merge attributes from the parameters. | ||||
Douglas Gregor | 0a4a23a | 2012-05-17 23:13:29 +0000 | [diff] [blame] | 2787 | ObjCMethodDecl::param_const_iterator oi = oldMethod->param_begin(), |
2788 | oe = oldMethod->param_end(); | ||||
Argyrios Kyrtzidis | 491306a | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 2789 | for (ObjCMethodDecl::param_iterator |
John McCall | eca5d22 | 2011-03-02 04:00:57 +0000 | [diff] [blame] | 2790 | ni = newMethod->param_begin(), ne = newMethod->param_end(); |
Douglas Gregor | 0a4a23a | 2012-05-17 23:13:29 +0000 | [diff] [blame] | 2791 | ni != ne && oi != oe; ++ni, ++oi) |
Richard Smith | 3a2b7a1 | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 2792 | mergeParamDeclAttributes(*ni, *oi, *this); |
John McCall | 6c2c250 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 2793 | |
Douglas Gregor | f4d918f | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2794 | CheckObjCMethodOverride(newMethod, oldMethod); |
John McCall | eca5d22 | 2011-03-02 04:00:57 +0000 | [diff] [blame] | 2795 | } |
2796 | |||||
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 2797 | /// MergeVarDeclTypes - We parsed a variable 'New' which has the same name and |
2798 | /// 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] | 2799 | /// emitting diagnostics as appropriate. |
2800 | /// | ||||
2801 | /// Declarations using the auto type specifier (C++ [decl.spec.auto]) call back | ||||
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2802 | /// to here in AddInitializerToDecl. We can't check them before the initializer |
2803 | /// is attached. | ||||
Richard Smith | dd9459f | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 2804 | void Sema::MergeVarDeclTypes(VarDecl *New, VarDecl *Old, |
2805 | bool MergeTypeWithOld) { | ||||
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 2806 | if (New->isInvalidDecl() || Old->isInvalidDecl()) |
2807 | return; | ||||
2808 | |||||
2809 | QualType MergedT; | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2810 | if (getLangOpts().CPlusPlus) { |
Richard Smith | dc7a4f5 | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 2811 | if (New->getType()->isUndeducedType()) { |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 2812 | // We don't know what the new type is until the initializer is attached. |
2813 | return; | ||||
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 2814 | } else if (Context.hasSameType(New->getType(), Old->getType())) { |
2815 | // These could still be something that needs exception specs checked. | ||||
2816 | return MergeVarDeclExceptionSpecs(New, Old); | ||||
2817 | } | ||||
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 2818 | // C++ [basic.link]p10: |
2819 | // [...] the types specified by all declarations referring to a given | ||||
2820 | // object or function shall be identical, except that declarations for an | ||||
2821 | // array object can specify array types that differ by the presence or | ||||
2822 | // absence of a major array bound (8.3.4). | ||||
2823 | else if (Old->getType()->isIncompleteArrayType() && | ||||
2824 | New->getType()->isArrayType()) { | ||||
Eli Friedman | 6febf12 | 2012-12-13 01:43:21 +0000 | [diff] [blame] | 2825 | const ArrayType *OldArray = Context.getAsArrayType(Old->getType()); |
2826 | const ArrayType *NewArray = Context.getAsArrayType(New->getType()); | ||||
2827 | if (Context.hasSameType(OldArray->getElementType(), | ||||
2828 | NewArray->getElementType())) | ||||
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 2829 | MergedT = New->getType(); |
2830 | } else if (Old->getType()->isArrayType() && | ||||
2831 | New->getType()->isIncompleteArrayType()) { | ||||
Eli Friedman | 6febf12 | 2012-12-13 01:43:21 +0000 | [diff] [blame] | 2832 | const ArrayType *OldArray = Context.getAsArrayType(Old->getType()); |
2833 | const ArrayType *NewArray = Context.getAsArrayType(New->getType()); | ||||
2834 | if (Context.hasSameType(OldArray->getElementType(), | ||||
2835 | NewArray->getElementType())) | ||||
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 2836 | MergedT = Old->getType(); |
2837 | } else if (New->getType()->isObjCObjectPointerType() | ||||
2838 | && Old->getType()->isObjCObjectPointerType()) { | ||||
2839 | MergedT = Context.mergeObjCGCQualifiers(New->getType(), | ||||
2840 | Old->getType()); | ||||
2841 | } | ||||
2842 | } else { | ||||
2843 | MergedT = Context.mergeTypes(New->getType(), Old->getType()); | ||||
2844 | } | ||||
2845 | if (MergedT.isNull()) { | ||||
Richard Smith | dd9459f | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 2846 | // It's OK if we couldn't merge types if either type is dependent, for a |
2847 | // block-scope variable. In other cases (static data members of class | ||||
2848 | // templates, variable templates, ...), we require the types to be | ||||
2849 | // equivalent. | ||||
2850 | // FIXME: The C++ standard doesn't say anything about this. | ||||
2851 | if ((New->getType()->isDependentType() || | ||||
2852 | Old->getType()->isDependentType()) && New->isLocalVarDecl()) { | ||||
2853 | // If the old type was dependent, we can't merge with it, so the new type | ||||
2854 | // becomes dependent for now. We'll reproduce the original type when we | ||||
2855 | // instantiate the TypeSourceInfo for the variable. | ||||
2856 | if (!New->getType()->isDependentType() && MergeTypeWithOld) | ||||
2857 | New->setType(Context.DependentTy); | ||||
2858 | return; | ||||
2859 | } | ||||
2860 | |||||
2861 | // FIXME: Even if this merging succeeds, some other non-visible declaration | ||||
2862 | // of this variable might have an incompatible type. For instance: | ||||
2863 | // | ||||
2864 | // extern int arr[]; | ||||
2865 | // void f() { extern int arr[2]; } | ||||
2866 | // void g() { extern int arr[3]; } | ||||
2867 | // | ||||
2868 | // Neither C nor C++ requires a diagnostic for this, but we should still try | ||||
2869 | // to diagnose it. | ||||
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 2870 | Diag(New->getLocation(), diag::err_redefinition_different_type) |
David Blaikie | a405b25 | 2012-09-20 18:38:57 +0000 | [diff] [blame] | 2871 | << New->getDeclName() << New->getType() << Old->getType(); |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 2872 | Diag(Old->getLocation(), diag::note_previous_definition); |
2873 | return New->setInvalidDecl(); | ||||
2874 | } | ||||
John McCall | 5b8740f | 2013-04-01 18:34:28 +0000 | [diff] [blame] | 2875 | |
2876 | // Don't actually update the type on the new declaration if the old | ||||
2877 | // declaration was a extern declaration in a different scope. | ||||
Richard Smith | dd9459f | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 2878 | if (MergeTypeWithOld) |
John McCall | 5b8740f | 2013-04-01 18:34:28 +0000 | [diff] [blame] | 2879 | New->setType(MergedT); |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 2880 | } |
2881 | |||||
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2882 | /// MergeVarDecl - We just parsed a variable 'New' which has the same name |
2883 | /// and scope as a previous declaration 'Old'. Figure out how to resolve this | ||||
2884 | /// situation, merging decls or emitting diagnostics as appropriate. | ||||
2885 | /// | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2886 | /// Tentative definition rules (C99 6.9.2p2) are checked by |
2887 | /// FinalizeDeclaratorGroup. Unfortunately, we can't analyze tentative | ||||
Steve Naroff | ff9eb1f | 2008-08-08 17:50:35 +0000 | [diff] [blame] | 2888 | /// definitions here, since the initializer hasn't been attached. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2889 | /// |
John McCall | 5b8740f | 2013-04-01 18:34:28 +0000 | [diff] [blame] | 2890 | void Sema::MergeVarDecl(VarDecl *New, LookupResult &Previous, |
Larisse Voufo | 567f917 | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 2891 | bool MergeTypeWithPrevious) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 2892 | // If the new decl is already invalid, don't do any other checking. |
2893 | if (New->isInvalidDecl()) | ||||
2894 | return; | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2895 | |
Larisse Voufo | 4a91989 | 2013-08-14 03:09:19 +0000 | [diff] [blame] | 2896 | // Verify the old decl was also a variable or variable template. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 2897 | VarDecl *Old = 0; |
Larisse Voufo | 4a91989 | 2013-08-14 03:09:19 +0000 | [diff] [blame] | 2898 | if (Previous.isSingleResult() && |
2899 | (Old = dyn_cast<VarDecl>(Previous.getFoundDecl()))) { | ||||
Larisse Voufo | 567f917 | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 2900 | if (New->getDescribedVarTemplate()) |
Larisse Voufo | 4a91989 | 2013-08-14 03:09:19 +0000 | [diff] [blame] | 2901 | Old = Old->getDescribedVarTemplate() ? Old : 0; |
2902 | else | ||||
2903 | Old = Old->getDescribedVarTemplate() ? 0 : Old; | ||||
2904 | } | ||||
2905 | if (!Old) { | ||||
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 2906 | Diag(New->getLocation(), diag::err_redefinition_different_kind) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 2907 | << New->getDeclName(); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 2908 | Diag(Previous.getRepresentativeDecl()->getLocation(), |
2909 | diag::note_previous_definition); | ||||
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 2910 | return New->setInvalidDecl(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2911 | } |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 2912 | |
Rafael Espindola | 90cc390 | 2013-04-15 12:49:13 +0000 | [diff] [blame] | 2913 | if (!shouldLinkPossiblyHiddenDecl(Old, New)) |
2914 | return; | ||||
2915 | |||||
Douglas Gregor | 7f6ff02 | 2010-08-30 14:32:14 +0000 | [diff] [blame] | 2916 | // C++ [class.mem]p1: |
2917 | // A member shall not be declared twice in the member-specification [...] | ||||
2918 | // | ||||
2919 | // Here, we need only consider static data members. | ||||
2920 | if (Old->isStaticDataMember() && !New->isOutOfLine()) { | ||||
2921 | Diag(New->getLocation(), diag::err_duplicate_member) | ||||
2922 | << New->getIdentifier(); | ||||
2923 | Diag(Old->getLocation(), diag::note_previous_declaration); | ||||
2924 | New->setInvalidDecl(); | ||||
2925 | } | ||||
2926 | |||||
Douglas Gregor | 27c6da2 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 2927 | mergeDeclAttributes(New, Old); |
David Blaikie | d662a79 | 2011-10-19 22:56:21 +0000 | [diff] [blame] | 2928 | // Warn if an already-declared variable is made a weak_import in a subsequent |
2929 | // declaration | ||||
Fariborz Jahanian | ab27d6e | 2011-06-20 17:50:03 +0000 | [diff] [blame] | 2930 | if (New->getAttr<WeakImportAttr>() && |
2931 | Old->getStorageClass() == SC_None && | ||||
Fariborz Jahanian | d543130 | 2011-06-22 22:08:50 +0000 | [diff] [blame] | 2932 | !Old->getAttr<WeakImportAttr>()) { |
2933 | Diag(New->getLocation(), diag::warn_weak_import) << New->getDeclName(); | ||||
2934 | Diag(Old->getLocation(), diag::note_previous_definition); | ||||
2935 | // Remove weak_import attribute on new declaration. | ||||
Fariborz Jahanian | c3ca14d | 2011-06-23 17:50:10 +0000 | [diff] [blame] | 2936 | New->dropAttr<WeakImportAttr>(); |
Fariborz Jahanian | d543130 | 2011-06-22 22:08:50 +0000 | [diff] [blame] | 2937 | } |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 2938 | |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 2939 | // Merge the types. |
Richard Smith | dd9459f | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 2940 | MergeVarDeclTypes(New, Old, MergeTypeWithPrevious); |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 2941 | if (New->isInvalidDecl()) |
2942 | return; | ||||
Douglas Gregor | 656de63 | 2009-03-11 23:52:16 +0000 | [diff] [blame] | 2943 | |
Rafael Espindola | ea4b111 | 2013-04-04 21:21:25 +0000 | [diff] [blame] | 2944 | // [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] | 2945 | if (New->getStorageClass() == SC_Static && |
Rafael Espindola | ea4b111 | 2013-04-04 21:21:25 +0000 | [diff] [blame] | 2946 | !New->isStaticDataMember() && |
Rafael Espindola | 181e3ec | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 2947 | Old->hasExternalFormalLinkage()) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2948 | Diag(New->getLocation(), diag::err_static_non_static) << New->getDeclName(); |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 2949 | Diag(Old->getLocation(), diag::note_previous_definition); |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 2950 | return New->setInvalidDecl(); |
Steve Naroff | b7b032e | 2008-01-30 00:44:01 +0000 | [diff] [blame] | 2951 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2952 | // C99 6.2.2p4: |
Douglas Gregor | 5ef122e | 2009-03-19 22:01:50 +0000 | [diff] [blame] | 2953 | // For an identifier declared with the storage-class specifier |
2954 | // extern in a scope in which a prior declaration of that | ||||
2955 | // identifier is visible,23) if the prior declaration specifies | ||||
2956 | // internal or external linkage, the linkage of the identifier at | ||||
2957 | // the later declaration is the same as the linkage specified at | ||||
2958 | // the prior declaration. If no prior declaration is visible, or | ||||
2959 | // if the prior declaration specifies no linkage, then the | ||||
2960 | // identifier has external linkage. | ||||
Douglas Gregor | 38179b2 | 2009-03-23 16:17:01 +0000 | [diff] [blame] | 2961 | if (New->hasExternalStorage() && Old->hasLinkage()) |
Douglas Gregor | 5ef122e | 2009-03-19 22:01:50 +0000 | [diff] [blame] | 2962 | /* Okay */; |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 2963 | else if (New->getCanonicalDecl()->getStorageClass() != SC_Static && |
Rafael Espindola | ea4b111 | 2013-04-04 21:21:25 +0000 | [diff] [blame] | 2964 | !New->isStaticDataMember() && |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 2965 | Old->getCanonicalDecl()->getStorageClass() == SC_Static) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2966 | Diag(New->getLocation(), diag::err_non_static_static) << New->getDeclName(); |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 2967 | Diag(Old->getLocation(), diag::note_previous_definition); |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 2968 | return New->setInvalidDecl(); |
Steve Naroff | b7b032e | 2008-01-30 00:44:01 +0000 | [diff] [blame] | 2969 | } |
Daniel Dunbar | 5466c7b | 2009-04-14 02:25:56 +0000 | [diff] [blame] | 2970 | |
Argyrios Kyrtzidis | 6684d85 | 2011-01-31 07:04:46 +0000 | [diff] [blame] | 2971 | // Check if extern is followed by non-extern and vice-versa. |
2972 | if (New->hasExternalStorage() && | ||||
2973 | !Old->hasLinkage() && Old->isLocalVarDecl()) { | ||||
2974 | Diag(New->getLocation(), diag::err_extern_non_extern) << New->getDeclName(); | ||||
2975 | Diag(Old->getLocation(), diag::note_previous_definition); | ||||
2976 | return New->setInvalidDecl(); | ||||
2977 | } | ||||
Rafael Espindola | 80a8689 | 2013-04-04 02:47:57 +0000 | [diff] [blame] | 2978 | if (Old->hasLinkage() && New->isLocalVarDecl() && |
2979 | !New->hasExternalStorage()) { | ||||
Argyrios Kyrtzidis | 6684d85 | 2011-01-31 07:04:46 +0000 | [diff] [blame] | 2980 | Diag(New->getLocation(), diag::err_non_extern_extern) << New->getDeclName(); |
2981 | Diag(Old->getLocation(), diag::note_previous_definition); | ||||
2982 | return New->setInvalidDecl(); | ||||
2983 | } | ||||
2984 | |||||
Steve Naroff | 094cefb | 2008-09-17 14:05:40 +0000 | [diff] [blame] | 2985 | // Variables with external linkage are analyzed in FinalizeDeclaratorGroup. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2986 | |
Daniel Dunbar | 5466c7b | 2009-04-14 02:25:56 +0000 | [diff] [blame] | 2987 | // FIXME: The test for external storage here seems wrong? We still |
2988 | // need to check for mismatches. | ||||
2989 | if (!New->hasExternalStorage() && !New->isFileVarDecl() && | ||||
Douglas Gregor | 656de63 | 2009-03-11 23:52:16 +0000 | [diff] [blame] | 2990 | // Don't complain about out-of-line definitions of static members. |
2991 | !(Old->getLexicalDeclContext()->isRecord() && | ||||
2992 | !New->getLexicalDeclContext()->isRecord())) { | ||||
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 2993 | Diag(New->getLocation(), diag::err_redefinition) << New->getDeclName(); |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 2994 | Diag(Old->getLocation(), diag::note_previous_definition); |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 2995 | return New->setInvalidDecl(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2996 | } |
Douglas Gregor | 275a369 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 2997 | |
Richard Smith | 38afbc7 | 2013-04-13 02:43:54 +0000 | [diff] [blame] | 2998 | if (New->getTLSKind() != Old->getTLSKind()) { |
2999 | if (!Old->getTLSKind()) { | ||||
3000 | Diag(New->getLocation(), diag::err_thread_non_thread) << New->getDeclName(); | ||||
3001 | Diag(Old->getLocation(), diag::note_previous_declaration); | ||||
3002 | } else if (!New->getTLSKind()) { | ||||
3003 | Diag(New->getLocation(), diag::err_non_thread_thread) << New->getDeclName(); | ||||
3004 | Diag(Old->getLocation(), diag::note_previous_declaration); | ||||
3005 | } else { | ||||
3006 | // Do not allow redeclaration to change the variable between requiring | ||||
3007 | // static and dynamic initialization. | ||||
3008 | // FIXME: GCC allows this, but uses the TLS keyword on the first | ||||
3009 | // declaration to determine the kind. Do we need to be compatible here? | ||||
3010 | Diag(New->getLocation(), diag::err_thread_thread_different_kind) | ||||
3011 | << New->getDeclName() << (New->getTLSKind() == VarDecl::TLS_Dynamic); | ||||
3012 | Diag(Old->getLocation(), diag::note_previous_declaration); | ||||
3013 | } | ||||
Eli Friedman | 63054b3 | 2009-04-19 20:27:55 +0000 | [diff] [blame] | 3014 | } |
3015 | |||||
Sebastian Redl | 4cae1b3 | 2010-02-02 18:35:11 +0000 | [diff] [blame] | 3016 | // C++ doesn't have tentative definitions, so go right ahead and check here. |
3017 | const VarDecl *Def; | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3018 | if (getLangOpts().CPlusPlus && |
Sebastian Redl | 6c048a9 | 2010-02-03 02:08:48 +0000 | [diff] [blame] | 3019 | New->isThisDeclarationADefinition() == VarDecl::Definition && |
Sebastian Redl | 4cae1b3 | 2010-02-02 18:35:11 +0000 | [diff] [blame] | 3020 | (Def = Old->getDefinition())) { |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3021 | Diag(New->getLocation(), diag::err_redefinition) << New; |
Sebastian Redl | 4cae1b3 | 2010-02-02 18:35:11 +0000 | [diff] [blame] | 3022 | Diag(Def->getLocation(), diag::note_previous_definition); |
3023 | New->setInvalidDecl(); | ||||
3024 | return; | ||||
3025 | } | ||||
Rafael Espindola | e57e3d3 | 2012-12-27 03:56:20 +0000 | [diff] [blame] | 3026 | |
Rafael Espindola | 950fee2 | 2013-02-14 01:18:37 +0000 | [diff] [blame] | 3027 | if (haveIncompatibleLanguageLinkages(Old, New)) { |
Rafael Espindola | e57e3d3 | 2012-12-27 03:56:20 +0000 | [diff] [blame] | 3028 | Diag(New->getLocation(), diag::err_different_language_linkage) << New; |
3029 | Diag(Old->getLocation(), diag::note_previous_definition); | ||||
3030 | New->setInvalidDecl(); | ||||
3031 | return; | ||||
3032 | } | ||||
3033 | |||||
Rafael Espindola | 8dbf697 | 2012-11-25 14:07:59 +0000 | [diff] [blame] | 3034 | // Merge "used" flag. |
3035 | if (Old->isUsed(false)) | ||||
3036 | New->setUsed(); | ||||
3037 | |||||
Douglas Gregor | 275a369 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 3038 | // Keep a chain of previous declarations. |
3039 | New->setPreviousDeclaration(Old); | ||||
John McCall | 46460a6 | 2010-01-20 21:53:11 +0000 | [diff] [blame] | 3040 | |
3041 | // Inherit access appropriately. | ||||
3042 | New->setAccess(Old->getAccess()); | ||||
Larisse Voufo | 567f917 | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 3043 | |
3044 | if (VarTemplateDecl *VTD = New->getDescribedVarTemplate()) { | ||||
3045 | if (New->isStaticDataMember() && New->isOutOfLine()) | ||||
3046 | VTD->setAccess(New->getAccess()); | ||||
3047 | } | ||||
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3048 | } |
3049 | |||||
3050 | /// ParsedFreeStandingDeclSpec - This method is invoked when a declspec with | ||||
3051 | /// no declarator (e.g. "struct foo;") is parsed. | ||||
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3052 | Decl *Sema::ParsedFreeStandingDeclSpec(Scope *S, AccessSpecifier AS, |
John McCall | ac4df24 | 2011-03-22 23:00:04 +0000 | [diff] [blame] | 3053 | DeclSpec &DS) { |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 3054 | return ParsedFreeStandingDeclSpec(S, AS, DS, MultiTemplateParamsArg()); |
Chandler Carruth | 0f4be74 | 2011-05-03 18:35:10 +0000 | [diff] [blame] | 3055 | } |
3056 | |||||
Eli Friedman | 5e867c8 | 2013-07-10 00:30:46 +0000 | [diff] [blame] | 3057 | static void HandleTagNumbering(Sema &S, const TagDecl *Tag) { |
3058 | if (isa<CXXRecordDecl>(Tag->getParent())) { | ||||
3059 | // If this tag is the direct child of a class, number it if | ||||
3060 | // it is anonymous. | ||||
3061 | if (!Tag->getName().empty() || Tag->getTypedefNameForAnonDecl()) | ||||
3062 | return; | ||||
3063 | MangleNumberingContext &MCtx = | ||||
3064 | S.Context.getManglingNumberContext(Tag->getParent()); | ||||
3065 | S.Context.setManglingNumber(Tag, MCtx.getManglingNumber(Tag)); | ||||
3066 | return; | ||||
3067 | } | ||||
3068 | |||||
3069 | // If this tag isn't a direct child of a class, number it if it is local. | ||||
3070 | Decl *ManglingContextDecl; | ||||
3071 | if (MangleNumberingContext *MCtx = | ||||
3072 | S.getCurrentMangleNumberContext(Tag->getDeclContext(), | ||||
3073 | ManglingContextDecl)) { | ||||
3074 | S.Context.setManglingNumber(Tag, MCtx->getManglingNumber(Tag)); | ||||
3075 | } | ||||
3076 | } | ||||
3077 | |||||
Chandler Carruth | 0f4be74 | 2011-05-03 18:35:10 +0000 | [diff] [blame] | 3078 | /// ParsedFreeStandingDeclSpec - This method is invoked when a declspec with |
Richard Smith | c7f8116 | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 3079 | /// no declarator (e.g. "struct foo;") is parsed. It also accepts template |
Chandler Carruth | 0f4be74 | 2011-05-03 18:35:10 +0000 | [diff] [blame] | 3080 | /// parameters to cope with template friend declarations. |
3081 | Decl *Sema::ParsedFreeStandingDeclSpec(Scope *S, AccessSpecifier AS, | ||||
3082 | DeclSpec &DS, | ||||
Richard Smith | c7f8116 | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 3083 | MultiTemplateParamsArg TemplateParams, |
3084 | bool IsExplicitInstantiation) { | ||||
John McCall | e3af023 | 2009-10-07 23:34:25 +0000 | [diff] [blame] | 3085 | Decl *TagD = 0; |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 3086 | TagDecl *Tag = 0; |
3087 | if (DS.getTypeSpecType() == DeclSpec::TST_class || | ||||
3088 | DS.getTypeSpecType() == DeclSpec::TST_struct || | ||||
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 3089 | DS.getTypeSpecType() == DeclSpec::TST_interface || |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 3090 | DS.getTypeSpecType() == DeclSpec::TST_union || |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 3091 | DS.getTypeSpecType() == DeclSpec::TST_enum) { |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3092 | TagD = DS.getRepAsDecl(); |
John McCall | e3af023 | 2009-10-07 23:34:25 +0000 | [diff] [blame] | 3093 | |
3094 | if (!TagD) // We probably had an error | ||||
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3095 | return 0; |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 3096 | |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 3097 | // Note that the above type specs guarantee that the |
3098 | // type rep is a Decl, whereas in many of the others | ||||
3099 | // it's a Type. | ||||
Peter Collingbourne | 0661bd0c | 2011-10-23 17:07:16 +0000 | [diff] [blame] | 3100 | if (isa<TagDecl>(TagD)) |
3101 | Tag = cast<TagDecl>(TagD); | ||||
3102 | else if (ClassTemplateDecl *CTD = dyn_cast<ClassTemplateDecl>(TagD)) | ||||
3103 | Tag = CTD->getTemplatedDecl(); | ||||
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 3104 | } |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 3105 | |
Argyrios Kyrtzidis | 216f78b | 2012-03-09 20:10:30 +0000 | [diff] [blame] | 3106 | if (Tag) { |
Eli Friedman | 5e867c8 | 2013-07-10 00:30:46 +0000 | [diff] [blame] | 3107 | HandleTagNumbering(*this, Tag); |
Argyrios Kyrtzidis | 717a20b | 2011-09-30 22:11:31 +0000 | [diff] [blame] | 3108 | Tag->setFreeStanding(); |
Argyrios Kyrtzidis | 216f78b | 2012-03-09 20:10:30 +0000 | [diff] [blame] | 3109 | if (Tag->isInvalidDecl()) |
3110 | return Tag; | ||||
3111 | } | ||||
Argyrios Kyrtzidis | 717a20b | 2011-09-30 22:11:31 +0000 | [diff] [blame] | 3112 | |
Nuno Lopes | 0a8bab0 | 2009-12-17 11:35:26 +0000 | [diff] [blame] | 3113 | if (unsigned TypeQuals = DS.getTypeQualifiers()) { |
3114 | // Enforce C99 6.7.3p2: "Types other than pointer types derived from object | ||||
3115 | // or incomplete types shall not be restrict-qualified." | ||||
3116 | if (TypeQuals & DeclSpec::TQ_restrict) | ||||
3117 | Diag(DS.getRestrictSpecLoc(), | ||||
3118 | diag::err_typecheck_invalid_restrict_not_pointer_noarg) | ||||
3119 | << DS.getSourceRange(); | ||||
3120 | } | ||||
3121 | |||||
Richard Smith | af1fc7a | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 3122 | if (DS.isConstexprSpecified()) { |
3123 | // C++0x [dcl.constexpr]p1: constexpr can only be applied to declarations | ||||
3124 | // and definitions of functions and variables. | ||||
3125 | if (Tag) | ||||
3126 | Diag(DS.getConstexprSpecLoc(), diag::err_constexpr_tag) | ||||
3127 | << (DS.getTypeSpecType() == DeclSpec::TST_class ? 0 : | ||||
3128 | DS.getTypeSpecType() == DeclSpec::TST_struct ? 1 : | ||||
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 3129 | DS.getTypeSpecType() == DeclSpec::TST_interface ? 2 : |
3130 | DS.getTypeSpecType() == DeclSpec::TST_union ? 3 : 4); | ||||
Richard Smith | af1fc7a | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 3131 | else |
3132 | Diag(DS.getConstexprSpecLoc(), diag::err_constexpr_no_declarators); | ||||
3133 | // Don't emit warnings after this error. | ||||
3134 | return TagD; | ||||
3135 | } | ||||
3136 | |||||
Richard Smith | c7f8116 | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 3137 | DiagnoseFunctionSpecifiers(DS); |
3138 | |||||
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 3139 | if (DS.isFriendSpecified()) { |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 3140 | // If we're dealing with a decl but not a TagDecl, assume that |
3141 | // whatever routines created it handled the friendship aspect. | ||||
3142 | if (TagD && !Tag) | ||||
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3143 | return 0; |
Chandler Carruth | 0f4be74 | 2011-05-03 18:35:10 +0000 | [diff] [blame] | 3144 | return ActOnFriendTypeDecl(S, DS, TemplateParams); |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 3145 | } |
John McCall | ac4df24 | 2011-03-22 23:00:04 +0000 | [diff] [blame] | 3146 | |
Richard Smith | c7f8116 | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 3147 | CXXScopeSpec &SS = DS.getTypeSpecScope(); |
3148 | bool IsExplicitSpecialization = | ||||
3149 | !TemplateParams.empty() && TemplateParams.back()->size() == 0; | ||||
3150 | if (Tag && SS.isNotEmpty() && !Tag->isCompleteDefinition() && | ||||
3151 | !IsExplicitInstantiation && !IsExplicitSpecialization) { | ||||
3152 | // Per C++ [dcl.type.elab]p1, a class declaration cannot have a | ||||
3153 | // nested-name-specifier unless it is an explicit instantiation | ||||
3154 | // or an explicit specialization. | ||||
3155 | // Per C++ [dcl.enum]p1, an opaque-enum-declaration can't either. | ||||
3156 | Diag(SS.getBeginLoc(), diag::err_standalone_class_nested_name_specifier) | ||||
3157 | << (DS.getTypeSpecType() == DeclSpec::TST_class ? 0 : | ||||
3158 | DS.getTypeSpecType() == DeclSpec::TST_struct ? 1 : | ||||
3159 | DS.getTypeSpecType() == DeclSpec::TST_interface ? 2 : | ||||
3160 | DS.getTypeSpecType() == DeclSpec::TST_union ? 3 : 4) | ||||
3161 | << SS.getRange(); | ||||
3162 | return 0; | ||||
3163 | } | ||||
3164 | |||||
3165 | // Track whether this decl-specifier declares anything. | ||||
3166 | bool DeclaresAnything = true; | ||||
3167 | |||||
3168 | // Handle anonymous struct definitions. | ||||
Douglas Gregor | 4920f1f | 2009-01-12 22:49:06 +0000 | [diff] [blame] | 3169 | if (RecordDecl *Record = dyn_cast_or_null<RecordDecl>(Tag)) { |
John McCall | 5e1cdac | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 3170 | if (!Record->getDeclName() && Record->isCompleteDefinition() && |
Douglas Gregor | a71c129 | 2009-03-06 23:06:59 +0000 | [diff] [blame] | 3171 | DS.getStorageClassSpec() != DeclSpec::SCS_typedef) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3172 | if (getLangOpts().CPlusPlus || |
Douglas Gregor | a71c129 | 2009-03-06 23:06:59 +0000 | [diff] [blame] | 3173 | Record->getDeclContext()->isRecord()) |
John McCall | aec0371 | 2010-05-21 20:45:30 +0000 | [diff] [blame] | 3174 | return BuildAnonymousStructOrUnion(S, DS, AS, Record); |
Douglas Gregor | a71c129 | 2009-03-06 23:06:59 +0000 | [diff] [blame] | 3175 | |
Richard Smith | c7f8116 | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 3176 | DeclaresAnything = false; |
Douglas Gregor | a71c129 | 2009-03-06 23:06:59 +0000 | [diff] [blame] | 3177 | } |
Francois Pichet | 8e161ed | 2010-11-23 06:07:27 +0000 | [diff] [blame] | 3178 | } |
Douglas Gregor | 4920f1f | 2009-01-12 22:49:06 +0000 | [diff] [blame] | 3179 | |
Richard Smith | c7f8116 | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 3180 | // Check for Microsoft C extension: anonymous struct member. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3181 | if (getLangOpts().MicrosoftExt && !getLangOpts().CPlusPlus && |
Francois Pichet | 8e161ed | 2010-11-23 06:07:27 +0000 | [diff] [blame] | 3182 | CurContext->isRecord() && |
3183 | DS.getStorageClassSpec() == DeclSpec::SCS_unspecified) { | ||||
3184 | // Handle 2 kinds of anonymous struct: | ||||
3185 | // struct STRUCT; | ||||
3186 | // and | ||||
3187 | // STRUCT_TYPE; <- where STRUCT_TYPE is a typedef struct. | ||||
3188 | RecordDecl *Record = dyn_cast_or_null<RecordDecl>(Tag); | ||||
John McCall | 5e1cdac | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 3189 | if ((Record && Record->getDeclName() && !Record->isCompleteDefinition()) || |
Francois Pichet | 8e161ed | 2010-11-23 06:07:27 +0000 | [diff] [blame] | 3190 | (DS.getTypeSpecType() == DeclSpec::TST_typename && |
3191 | DS.getRepAsType().get()->isStructureType())) { | ||||
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3192 | Diag(DS.getLocStart(), diag::ext_ms_anonymous_struct) |
Francois Pichet | 8e161ed | 2010-11-23 06:07:27 +0000 | [diff] [blame] | 3193 | << DS.getSourceRange(); |
3194 | return BuildMicrosoftCAnonymousStruct(S, DS, Record); | ||||
3195 | } | ||||
Douglas Gregor | 4920f1f | 2009-01-12 22:49:06 +0000 | [diff] [blame] | 3196 | } |
Richard Smith | c7f8116 | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 3197 | |
3198 | // Skip all the checks below if we have a type error. | ||||
3199 | if (DS.getTypeSpecType() == DeclSpec::TST_error || | ||||
3200 | (TagD && TagD->isInvalidDecl())) | ||||
3201 | return TagD; | ||||
3202 | |||||
3203 | if (getLangOpts().CPlusPlus && | ||||
Douglas Gregor | a131d0f | 2010-07-13 06:24:26 +0000 | [diff] [blame] | 3204 | DS.getStorageClassSpec() != DeclSpec::SCS_typedef) |
3205 | if (EnumDecl *Enum = dyn_cast_or_null<EnumDecl>(Tag)) | ||||
3206 | if (Enum->enumerator_begin() == Enum->enumerator_end() && | ||||
Richard Smith | c7f8116 | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 3207 | !Enum->getIdentifier() && !Enum->isInvalidDecl()) |
3208 | DeclaresAnything = false; | ||||
John McCall | ac4df24 | 2011-03-22 23:00:04 +0000 | [diff] [blame] | 3209 | |
John McCall | ac4df24 | 2011-03-22 23:00:04 +0000 | [diff] [blame] | 3210 | if (!DS.isMissingDeclaratorOk()) { |
Richard Smith | c7f8116 | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 3211 | // Customize diagnostic for a typedef missing a name. |
3212 | if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) | ||||
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3213 | Diag(DS.getLocStart(), diag::ext_typedef_without_a_name) |
Douglas Gregor | a0ebd60 | 2010-07-16 15:40:40 +0000 | [diff] [blame] | 3214 | << DS.getSourceRange(); |
Richard Smith | c7f8116 | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 3215 | else |
3216 | DeclaresAnything = false; | ||||
Sebastian Redl | a4ed0d8 | 2008-12-28 15:28:59 +0000 | [diff] [blame] | 3217 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3218 | |
Richard Smith | c7f8116 | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 3219 | if (DS.isModulePrivateSpecified() && |
Douglas Gregor | e389585 | 2011-09-12 18:37:38 +0000 | [diff] [blame] | 3220 | Tag && Tag->getDeclContext()->isFunctionOrMethod()) |
3221 | Diag(DS.getModulePrivateSpecLoc(), diag::err_module_private_local_class) | ||||
3222 | << Tag->getTagKind() | ||||
3223 | << FixItHint::CreateRemoval(DS.getModulePrivateSpecLoc()); | ||||
3224 | |||||
Richard Smith | c7f8116 | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 3225 | ActOnDocumentableDecl(TagD); |
3226 | |||||
3227 | // C 6.7/2: | ||||
3228 | // A declaration [...] shall declare at least a declarator [...], a tag, | ||||
3229 | // or the members of an enumeration. | ||||
3230 | // C++ [dcl.dcl]p3: | ||||
3231 | // [If there are no declarators], and except for the declaration of an | ||||
3232 | // unnamed bit-field, the decl-specifier-seq shall introduce one or more | ||||
3233 | // names into the program, or shall redeclare a name introduced by a | ||||
3234 | // previous declaration. | ||||
3235 | if (!DeclaresAnything) { | ||||
3236 | // In C, we allow this as a (popular) extension / bug. Don't bother | ||||
3237 | // producing further diagnostics for redundant qualifiers after this. | ||||
3238 | Diag(DS.getLocStart(), diag::ext_no_declarators) << DS.getSourceRange(); | ||||
3239 | return TagD; | ||||
3240 | } | ||||
3241 | |||||
3242 | // C++ [dcl.stc]p1: | ||||
3243 | // If a storage-class-specifier appears in a decl-specifier-seq, [...] the | ||||
3244 | // init-declarator-list of the declaration shall not be empty. | ||||
3245 | // C++ [dcl.fct.spec]p1: | ||||
3246 | // If a cv-qualifier appears in a decl-specifier-seq, the | ||||
3247 | // init-declarator-list of the declaration shall not be empty. | ||||
3248 | // | ||||
3249 | // Spurious qualifiers here appear to be valid in C. | ||||
3250 | unsigned DiagID = diag::warn_standalone_specifier; | ||||
3251 | if (getLangOpts().CPlusPlus) | ||||
3252 | DiagID = diag::ext_standalone_specifier; | ||||
3253 | |||||
3254 | // Note that a linkage-specification sets a storage class, but | ||||
3255 | // 'extern "C" struct foo;' is actually valid and not theoretically | ||||
3256 | // useless. | ||||
3257 | if (DeclSpec::SCS SCS = DS.getStorageClassSpec()) | ||||
3258 | if (!DS.isExternInLinkageSpec() && SCS != DeclSpec::SCS_typedef) | ||||
3259 | Diag(DS.getStorageClassSpecLoc(), DiagID) | ||||
3260 | << DeclSpec::getSpecifierName(SCS); | ||||
3261 | |||||
Richard Smith | ec64244 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 3262 | if (DeclSpec::TSCS TSCS = DS.getThreadStorageClassSpec()) |
3263 | Diag(DS.getThreadStorageClassSpecLoc(), DiagID) | ||||
3264 | << DeclSpec::getSpecifierName(TSCS); | ||||
Richard Smith | c7f8116 | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 3265 | if (DS.getTypeQualifiers()) { |
3266 | if (DS.getTypeQualifiers() & DeclSpec::TQ_const) | ||||
3267 | Diag(DS.getConstSpecLoc(), DiagID) << "const"; | ||||
3268 | if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile) | ||||
3269 | Diag(DS.getConstSpecLoc(), DiagID) << "volatile"; | ||||
3270 | // Restrict is covered above. | ||||
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 3271 | if (DS.getTypeQualifiers() & DeclSpec::TQ_atomic) |
3272 | Diag(DS.getAtomicSpecLoc(), DiagID) << "_Atomic"; | ||||
Richard Smith | c7f8116 | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 3273 | } |
3274 | |||||
Eli Friedman | fc038e9 | 2011-12-17 00:36:09 +0000 | [diff] [blame] | 3275 | // Warn about ignored type attributes, for example: |
3276 | // __attribute__((aligned)) struct A; | ||||
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3277 | // Attributes should be placed after tag to apply to type declaration. |
Eli Friedman | fc038e9 | 2011-12-17 00:36:09 +0000 | [diff] [blame] | 3278 | if (!DS.getAttributes().empty()) { |
3279 | DeclSpec::TST TypeSpecType = DS.getTypeSpecType(); | ||||
3280 | if (TypeSpecType == DeclSpec::TST_class || | ||||
3281 | TypeSpecType == DeclSpec::TST_struct || | ||||
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 3282 | TypeSpecType == DeclSpec::TST_interface || |
Eli Friedman | fc038e9 | 2011-12-17 00:36:09 +0000 | [diff] [blame] | 3283 | TypeSpecType == DeclSpec::TST_union || |
3284 | TypeSpecType == DeclSpec::TST_enum) { | ||||
3285 | AttributeList* attrs = DS.getAttributes().getList(); | ||||
3286 | while (attrs) { | ||||
Michael Han | 45bed13 | 2012-10-04 16:42:52 +0000 | [diff] [blame] | 3287 | Diag(attrs->getLoc(), diag::warn_declspec_attribute_ignored) |
Eli Friedman | fc038e9 | 2011-12-17 00:36:09 +0000 | [diff] [blame] | 3288 | << attrs->getName() |
3289 | << (TypeSpecType == DeclSpec::TST_class ? 0 : | ||||
3290 | TypeSpecType == DeclSpec::TST_struct ? 1 : | ||||
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 3291 | TypeSpecType == DeclSpec::TST_union ? 2 : |
3292 | TypeSpecType == DeclSpec::TST_interface ? 3 : 4); | ||||
Eli Friedman | fc038e9 | 2011-12-17 00:36:09 +0000 | [diff] [blame] | 3293 | attrs = attrs->getNext(); |
3294 | } | ||||
3295 | } | ||||
3296 | } | ||||
John McCall | ac4df24 | 2011-03-22 23:00:04 +0000 | [diff] [blame] | 3297 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3298 | return TagD; |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3299 | } |
3300 | |||||
John McCall | 1d7c528 | 2009-12-18 10:40:03 +0000 | [diff] [blame] | 3301 | /// We are trying to inject an anonymous member into the given scope; |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 3302 | /// check if there's an existing declaration that can't be overloaded. |
3303 | /// | ||||
3304 | /// \return true if this is a forbidden redeclaration | ||||
John McCall | 1d7c528 | 2009-12-18 10:40:03 +0000 | [diff] [blame] | 3305 | static bool CheckAnonMemberRedeclaration(Sema &SemaRef, |
3306 | Scope *S, | ||||
Fariborz Jahanian | 588a4ad | 2010-01-22 18:30:17 +0000 | [diff] [blame] | 3307 | DeclContext *Owner, |
John McCall | 1d7c528 | 2009-12-18 10:40:03 +0000 | [diff] [blame] | 3308 | DeclarationName Name, |
3309 | SourceLocation NameLoc, | ||||
3310 | unsigned diagnostic) { | ||||
3311 | LookupResult R(SemaRef, Name, NameLoc, Sema::LookupMemberName, | ||||
3312 | Sema::ForRedeclaration); | ||||
3313 | if (!SemaRef.LookupName(R, S)) return false; | ||||
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 3314 | |
John McCall | 1d7c528 | 2009-12-18 10:40:03 +0000 | [diff] [blame] | 3315 | if (R.getAsSingle<TagDecl>()) |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 3316 | return false; |
3317 | |||||
3318 | // Pick a representative declaration. | ||||
John McCall | 1d7c528 | 2009-12-18 10:40:03 +0000 | [diff] [blame] | 3319 | NamedDecl *PrevDecl = R.getRepresentativeDecl()->getUnderlyingDecl(); |
Argyrios Kyrtzidis | 2b64239 | 2010-09-23 14:26:01 +0000 | [diff] [blame] | 3320 | assert(PrevDecl && "Expected a non-null Decl"); |
3321 | |||||
3322 | if (!SemaRef.isDeclInScope(PrevDecl, Owner, S)) | ||||
3323 | return false; | ||||
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 3324 | |
John McCall | 1d7c528 | 2009-12-18 10:40:03 +0000 | [diff] [blame] | 3325 | SemaRef.Diag(NameLoc, diagnostic) << Name; |
3326 | SemaRef.Diag(PrevDecl->getLocation(), diag::note_previous_declaration); | ||||
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 3327 | |
3328 | return true; | ||||
3329 | } | ||||
3330 | |||||
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3331 | /// InjectAnonymousStructOrUnionMembers - Inject the members of the |
3332 | /// anonymous struct or union AnonRecord into the owning context Owner | ||||
3333 | /// and scope S. This routine will be invoked just after we realize | ||||
3334 | /// that an unnamed union or struct is actually an anonymous union or | ||||
3335 | /// struct, e.g., | ||||
3336 | /// | ||||
3337 | /// @code | ||||
3338 | /// union { | ||||
3339 | /// int i; | ||||
3340 | /// float f; | ||||
3341 | /// }; // InjectAnonymousStructOrUnionMembers called here to inject i and | ||||
3342 | /// // f into the surrounding scope.x | ||||
3343 | /// @endcode | ||||
3344 | /// | ||||
3345 | /// This routine is recursive, injecting the names of nested anonymous | ||||
3346 | /// structs/unions into the owning context and scope as well. | ||||
John McCall | aec0371 | 2010-05-21 20:45:30 +0000 | [diff] [blame] | 3347 | static bool InjectAnonymousStructOrUnionMembers(Sema &SemaRef, Scope *S, |
Craig Topper | 6b9240e | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 3348 | DeclContext *Owner, |
3349 | RecordDecl *AnonRecord, | ||||
3350 | AccessSpecifier AS, | ||||
3351 | SmallVectorImpl<NamedDecl *> &Chaining, | ||||
3352 | bool MSAnonStruct) { | ||||
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 3353 | unsigned diagKind |
3354 | = AnonRecord->isUnion() ? diag::err_anonymous_union_member_redecl | ||||
3355 | : diag::err_anonymous_struct_member_redecl; | ||||
3356 | |||||
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3357 | bool Invalid = false; |
Francois Pichet | 8e161ed | 2010-11-23 06:07:27 +0000 | [diff] [blame] | 3358 | |
3359 | // Look every FieldDecl and IndirectFieldDecl with a name. | ||||
3360 | for (RecordDecl::decl_iterator D = AnonRecord->decls_begin(), | ||||
3361 | DEnd = AnonRecord->decls_end(); | ||||
3362 | D != DEnd; ++D) { | ||||
3363 | if ((isa<FieldDecl>(*D) || isa<IndirectFieldDecl>(*D)) && | ||||
3364 | cast<NamedDecl>(*D)->getDeclName()) { | ||||
3365 | ValueDecl *VD = cast<ValueDecl>(*D); | ||||
3366 | if (CheckAnonMemberRedeclaration(SemaRef, S, Owner, VD->getDeclName(), | ||||
3367 | VD->getLocation(), diagKind)) { | ||||
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3368 | // C++ [class.union]p2: |
3369 | // The names of the members of an anonymous union shall be | ||||
3370 | // distinct from the names of any other entity in the | ||||
3371 | // scope in which the anonymous union is declared. | ||||
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3372 | Invalid = true; |
3373 | } else { | ||||
3374 | // C++ [class.union]p2: | ||||
3375 | // For the purpose of name lookup, after the anonymous union | ||||
3376 | // definition, the members of the anonymous union are | ||||
3377 | // considered to have been defined in the scope in which the | ||||
3378 | // anonymous union is declared. | ||||
Francois Pichet | 8e161ed | 2010-11-23 06:07:27 +0000 | [diff] [blame] | 3379 | unsigned OldChainingSize = Chaining.size(); |
3380 | if (IndirectFieldDecl *IF = dyn_cast<IndirectFieldDecl>(VD)) | ||||
3381 | for (IndirectFieldDecl::chain_iterator PI = IF->chain_begin(), | ||||
3382 | PE = IF->chain_end(); PI != PE; ++PI) | ||||
3383 | Chaining.push_back(*PI); | ||||
3384 | else | ||||
3385 | Chaining.push_back(VD); | ||||
3386 | |||||
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 3387 | assert(Chaining.size() >= 2); |
3388 | NamedDecl **NamedChain = | ||||
3389 | new (SemaRef.Context)NamedDecl*[Chaining.size()]; | ||||
3390 | for (unsigned i = 0; i < Chaining.size(); i++) | ||||
3391 | NamedChain[i] = Chaining[i]; | ||||
3392 | |||||
3393 | IndirectFieldDecl* IndirectField = | ||||
Francois Pichet | 8e161ed | 2010-11-23 06:07:27 +0000 | [diff] [blame] | 3394 | IndirectFieldDecl::Create(SemaRef.Context, Owner, VD->getLocation(), |
3395 | VD->getIdentifier(), VD->getType(), | ||||
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 3396 | NamedChain, Chaining.size()); |
3397 | |||||
3398 | IndirectField->setAccess(AS); | ||||
3399 | IndirectField->setImplicit(); | ||||
3400 | SemaRef.PushOnScopeChains(IndirectField, S); | ||||
John McCall | aec0371 | 2010-05-21 20:45:30 +0000 | [diff] [blame] | 3401 | |
3402 | // That includes picking up the appropriate access specifier. | ||||
Francois Pichet | 8e161ed | 2010-11-23 06:07:27 +0000 | [diff] [blame] | 3403 | if (AS != AS_none) IndirectField->setAccess(AS); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 3404 | |
Francois Pichet | 8e161ed | 2010-11-23 06:07:27 +0000 | [diff] [blame] | 3405 | Chaining.resize(OldChainingSize); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3406 | } |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3407 | } |
3408 | } | ||||
3409 | |||||
3410 | return Invalid; | ||||
3411 | } | ||||
3412 | |||||
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 3413 | /// StorageClassSpecToVarDeclStorageClass - Maps a DeclSpec::SCS to |
3414 | /// a VarDecl::StorageClass. Any error reporting is up to the caller: | ||||
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 3415 | /// illegal input values are mapped to SC_None. |
3416 | static StorageClass | ||||
Rafael Espindola | 65dfa2b | 2013-04-25 12:11:36 +0000 | [diff] [blame] | 3417 | StorageClassSpecToVarDeclStorageClass(const DeclSpec &DS) { |
3418 | DeclSpec::SCS StorageClassSpec = DS.getStorageClassSpec(); | ||||
3419 | assert(StorageClassSpec != DeclSpec::SCS_typedef && | ||||
3420 | "Parser allowed 'typedef' as storage class VarDecl."); | ||||
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 3421 | switch (StorageClassSpec) { |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 3422 | case DeclSpec::SCS_unspecified: return SC_None; |
Rafael Espindola | 65dfa2b | 2013-04-25 12:11:36 +0000 | [diff] [blame] | 3423 | case DeclSpec::SCS_extern: |
3424 | if (DS.isExternInLinkageSpec()) | ||||
3425 | return SC_None; | ||||
3426 | return SC_Extern; | ||||
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 3427 | case DeclSpec::SCS_static: return SC_Static; |
3428 | case DeclSpec::SCS_auto: return SC_Auto; | ||||
3429 | case DeclSpec::SCS_register: return SC_Register; | ||||
3430 | case DeclSpec::SCS_private_extern: return SC_PrivateExtern; | ||||
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 3431 | // Illegal SCSs map to None: error reporting is up to the caller. |
3432 | case DeclSpec::SCS_mutable: // Fall through. | ||||
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 3433 | case DeclSpec::SCS_typedef: return SC_None; |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 3434 | } |
3435 | llvm_unreachable("unknown storage class specifier"); | ||||
3436 | } | ||||
3437 | |||||
Francois Pichet | 8e161ed | 2010-11-23 06:07:27 +0000 | [diff] [blame] | 3438 | /// BuildAnonymousStructOrUnion - Handle the declaration of an |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3439 | /// anonymous structure or union. Anonymous unions are a C++ feature |
Hans Wennborg | acbabf1 | 2012-02-03 15:47:04 +0000 | [diff] [blame] | 3440 | /// (C++ [class.union]) and a C11 feature; anonymous structures |
3441 | /// are a C11 feature and GNU C++ extension. | ||||
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3442 | Decl *Sema::BuildAnonymousStructOrUnion(Scope *S, DeclSpec &DS, |
3443 | AccessSpecifier AS, | ||||
3444 | RecordDecl *Record) { | ||||
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3445 | DeclContext *Owner = Record->getDeclContext(); |
3446 | |||||
3447 | // Diagnose whether this anonymous struct/union is an extension. | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3448 | if (Record->isUnion() && !getLangOpts().CPlusPlus && !getLangOpts().C11) |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3449 | Diag(Record->getLocation(), diag::ext_anonymous_union); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3450 | else if (!Record->isUnion() && getLangOpts().CPlusPlus) |
Hans Wennborg | acbabf1 | 2012-02-03 15:47:04 +0000 | [diff] [blame] | 3451 | Diag(Record->getLocation(), diag::ext_gnu_anonymous_struct); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3452 | else if (!Record->isUnion() && !getLangOpts().C11) |
Hans Wennborg | acbabf1 | 2012-02-03 15:47:04 +0000 | [diff] [blame] | 3453 | Diag(Record->getLocation(), diag::ext_c11_anonymous_struct); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3454 | |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3455 | // C and C++ require different kinds of checks for anonymous |
3456 | // structs/unions. | ||||
3457 | bool Invalid = false; | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3458 | if (getLangOpts().CPlusPlus) { |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3459 | const char* PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3460 | unsigned DiagID; |
David Blaikie | 2b79c32 | 2011-10-19 22:43:29 +0000 | [diff] [blame] | 3461 | if (Record->isUnion()) { |
3462 | // C++ [class.union]p6: | ||||
3463 | // Anonymous unions declared in a named namespace or in the | ||||
3464 | // global namespace shall be declared static. | ||||
3465 | if (DS.getStorageClassSpec() != DeclSpec::SCS_static && | ||||
3466 | (isa<TranslationUnitDecl>(Owner) || | ||||
3467 | (isa<NamespaceDecl>(Owner) && | ||||
3468 | cast<NamespaceDecl>(Owner)->getDeclName()))) { | ||||
David Blaikie | 82c8ca1 | 2011-10-20 02:49:08 +0000 | [diff] [blame] | 3469 | Diag(Record->getLocation(), diag::err_anonymous_union_not_static) |
3470 | << FixItHint::CreateInsertion(Record->getLocation(), "static "); | ||||
David Blaikie | 2b79c32 | 2011-10-19 22:43:29 +0000 | [diff] [blame] | 3471 | |
3472 | // Recover by adding 'static'. | ||||
3473 | DS.SetStorageClassSpec(*this, DeclSpec::SCS_static, SourceLocation(), | ||||
3474 | PrevSpec, DiagID); | ||||
3475 | } | ||||
3476 | // C++ [class.union]p6: | ||||
3477 | // A storage class is not allowed in a declaration of an | ||||
3478 | // anonymous union in a class scope. | ||||
3479 | else if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified && | ||||
3480 | isa<RecordDecl>(Owner)) { | ||||
3481 | Diag(DS.getStorageClassSpecLoc(), | ||||
David Blaikie | f6f876c | 2011-10-20 02:10:55 +0000 | [diff] [blame] | 3482 | diag::err_anonymous_union_with_storage_spec) |
3483 | << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc()); | ||||
David Blaikie | 2b79c32 | 2011-10-19 22:43:29 +0000 | [diff] [blame] | 3484 | |
3485 | // Recover by removing the storage specifier. | ||||
David Blaikie | d662a79 | 2011-10-19 22:56:21 +0000 | [diff] [blame] | 3486 | DS.SetStorageClassSpec(*this, DeclSpec::SCS_unspecified, |
3487 | SourceLocation(), | ||||
David Blaikie | 2b79c32 | 2011-10-19 22:43:29 +0000 | [diff] [blame] | 3488 | PrevSpec, DiagID); |
3489 | } | ||||
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3490 | } |
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 3491 | |
Douglas Gregor | 7604f64 | 2011-05-09 23:05:33 +0000 | [diff] [blame] | 3492 | // Ignore const/volatile/restrict qualifiers. |
3493 | if (DS.getTypeQualifiers()) { | ||||
3494 | if (DS.getTypeQualifiers() & DeclSpec::TQ_const) | ||||
3495 | Diag(DS.getConstSpecLoc(), diag::ext_anonymous_struct_union_qualified) | ||||
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 3496 | << Record->isUnion() << "const" |
Douglas Gregor | 7604f64 | 2011-05-09 23:05:33 +0000 | [diff] [blame] | 3497 | << FixItHint::CreateRemoval(DS.getConstSpecLoc()); |
3498 | if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile) | ||||
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 3499 | Diag(DS.getVolatileSpecLoc(), |
David Blaikie | d662a79 | 2011-10-19 22:56:21 +0000 | [diff] [blame] | 3500 | diag::ext_anonymous_struct_union_qualified) |
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 3501 | << Record->isUnion() << "volatile" |
Douglas Gregor | 7604f64 | 2011-05-09 23:05:33 +0000 | [diff] [blame] | 3502 | << FixItHint::CreateRemoval(DS.getVolatileSpecLoc()); |
3503 | if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict) | ||||
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 3504 | Diag(DS.getRestrictSpecLoc(), |
David Blaikie | d662a79 | 2011-10-19 22:56:21 +0000 | [diff] [blame] | 3505 | diag::ext_anonymous_struct_union_qualified) |
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 3506 | << Record->isUnion() << "restrict" |
Douglas Gregor | 7604f64 | 2011-05-09 23:05:33 +0000 | [diff] [blame] | 3507 | << FixItHint::CreateRemoval(DS.getRestrictSpecLoc()); |
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 3508 | if (DS.getTypeQualifiers() & DeclSpec::TQ_atomic) |
3509 | Diag(DS.getAtomicSpecLoc(), | ||||
3510 | diag::ext_anonymous_struct_union_qualified) | ||||
3511 | << Record->isUnion() << "_Atomic" | ||||
3512 | << FixItHint::CreateRemoval(DS.getAtomicSpecLoc()); | ||||
Douglas Gregor | 7604f64 | 2011-05-09 23:05:33 +0000 | [diff] [blame] | 3513 | |
3514 | DS.ClearTypeQualifiers(); | ||||
3515 | } | ||||
3516 | |||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3517 | // C++ [class.union]p2: |
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 3518 | // The member-specification of an anonymous union shall only |
3519 | // define non-static data members. [Note: nested types and | ||||
3520 | // functions cannot be declared within an anonymous union. ] | ||||
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3521 | for (DeclContext::decl_iterator Mem = Record->decls_begin(), |
3522 | MemEnd = Record->decls_end(); | ||||
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 3523 | Mem != MemEnd; ++Mem) { |
3524 | if (FieldDecl *FD = dyn_cast<FieldDecl>(*Mem)) { | ||||
3525 | // C++ [class.union]p3: | ||||
3526 | // An anonymous union shall not have private or protected | ||||
3527 | // members (clause 11). | ||||
John McCall | aec0371 | 2010-05-21 20:45:30 +0000 | [diff] [blame] | 3528 | assert(FD->getAccess() != AS_none); |
3529 | if (FD->getAccess() != AS_public) { | ||||
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 3530 | Diag(FD->getLocation(), diag::err_anonymous_record_nonpublic_member) |
3531 | << (int)Record->isUnion() << (int)(FD->getAccess() == AS_protected); | ||||
3532 | Invalid = true; | ||||
3533 | } | ||||
Argyrios Kyrtzidis | dd7744d | 2010-08-16 17:27:08 +0000 | [diff] [blame] | 3534 | |
Sean Hunt | cf34e75 | 2011-05-16 22:41:40 +0000 | [diff] [blame] | 3535 | // C++ [class.union]p1 |
3536 | // An object of a class with a non-trivial constructor, a non-trivial | ||||
3537 | // copy constructor, a non-trivial destructor, or a non-trivial copy | ||||
3538 | // assignment operator cannot be a member of a union, nor can an | ||||
3539 | // array of such objects. | ||||
Richard Smith | e7d7c39 | 2011-10-19 20:41:51 +0000 | [diff] [blame] | 3540 | if (CheckNontrivialField(FD)) |
Argyrios Kyrtzidis | dd7744d | 2010-08-16 17:27:08 +0000 | [diff] [blame] | 3541 | Invalid = true; |
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 3542 | } else if ((*Mem)->isImplicit()) { |
3543 | // Any implicit members are fine. | ||||
Douglas Gregor | 1931b44 | 2009-02-03 00:34:39 +0000 | [diff] [blame] | 3544 | } else if (isa<TagDecl>(*Mem) && (*Mem)->getDeclContext() != Record) { |
3545 | // This is a type that showed up in an | ||||
3546 | // elaborated-type-specifier inside the anonymous struct or | ||||
3547 | // union, but which actually declares a type outside of the | ||||
3548 | // anonymous struct or union. It's okay. | ||||
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 3549 | } else if (RecordDecl *MemRecord = dyn_cast<RecordDecl>(*Mem)) { |
3550 | if (!MemRecord->isAnonymousStructOrUnion() && | ||||
3551 | MemRecord->getDeclName()) { | ||||
Francois Pichet | 538e0d0 | 2010-09-08 11:32:25 +0000 | [diff] [blame] | 3552 | // Visual C++ allows type definition in anonymous struct or union. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3553 | if (getLangOpts().MicrosoftExt) |
Francois Pichet | 538e0d0 | 2010-09-08 11:32:25 +0000 | [diff] [blame] | 3554 | Diag(MemRecord->getLocation(), diag::ext_anonymous_record_with_type) |
3555 | << (int)Record->isUnion(); | ||||
3556 | else { | ||||
3557 | // This is a nested type declaration. | ||||
3558 | Diag(MemRecord->getLocation(), diag::err_anonymous_record_with_type) | ||||
3559 | << (int)Record->isUnion(); | ||||
3560 | Invalid = true; | ||||
3561 | } | ||||
Richard Smith | c5f7d6a | 2013-01-28 00:54:05 +0000 | [diff] [blame] | 3562 | } else { |
3563 | // This is an anonymous type definition within another anonymous type. | ||||
3564 | // This is a popular extension, provided by Plan9, MSVC and GCC, but | ||||
3565 | // not part of standard C++. | ||||
3566 | Diag(MemRecord->getLocation(), | ||||
Richard Smith | f270519 | 2013-01-31 03:11:12 +0000 | [diff] [blame] | 3567 | diag::ext_anonymous_record_with_anonymous_type) |
3568 | << (int)Record->isUnion(); | ||||
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 3569 | } |
Abramo Bagnara | 6206d53 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 3570 | } else if (isa<AccessSpecDecl>(*Mem)) { |
3571 | // Any access specifier is fine. | ||||
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 3572 | } else { |
3573 | // We have something that isn't a non-static data | ||||
3574 | // member. Complain about it. | ||||
3575 | unsigned DK = diag::err_anonymous_record_bad_member; | ||||
3576 | if (isa<TypeDecl>(*Mem)) | ||||
3577 | DK = diag::err_anonymous_record_with_type; | ||||
3578 | else if (isa<FunctionDecl>(*Mem)) | ||||
3579 | DK = diag::err_anonymous_record_with_function; | ||||
3580 | else if (isa<VarDecl>(*Mem)) | ||||
3581 | DK = diag::err_anonymous_record_with_static; | ||||
Francois Pichet | 538e0d0 | 2010-09-08 11:32:25 +0000 | [diff] [blame] | 3582 | |
3583 | // Visual C++ allows type definition in anonymous struct or union. | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3584 | if (getLangOpts().MicrosoftExt && |
Francois Pichet | 538e0d0 | 2010-09-08 11:32:25 +0000 | [diff] [blame] | 3585 | DK == diag::err_anonymous_record_with_type) |
3586 | Diag((*Mem)->getLocation(), diag::ext_anonymous_record_with_type) | ||||
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 3587 | << (int)Record->isUnion(); |
Francois Pichet | 538e0d0 | 2010-09-08 11:32:25 +0000 | [diff] [blame] | 3588 | else { |
3589 | Diag((*Mem)->getLocation(), DK) | ||||
3590 | << (int)Record->isUnion(); | ||||
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 3591 | Invalid = true; |
Francois Pichet | 538e0d0 | 2010-09-08 11:32:25 +0000 | [diff] [blame] | 3592 | } |
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 3593 | } |
3594 | } | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3595 | } |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3596 | |
3597 | if (!Record->isUnion() && !Owner->isRecord()) { | ||||
Douglas Gregor | 4920f1f | 2009-01-12 22:49:06 +0000 | [diff] [blame] | 3598 | Diag(Record->getLocation(), diag::err_anonymous_struct_not_member) |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3599 | << (int)getLangOpts().CPlusPlus; |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3600 | Invalid = true; |
3601 | } | ||||
3602 | |||||
John McCall | eb692e0 | 2009-10-22 23:31:08 +0000 | [diff] [blame] | 3603 | // Mock up a declarator. |
Argyrios Kyrtzidis | d3880f8 | 2011-06-28 03:01:18 +0000 | [diff] [blame] | 3604 | Declarator Dc(DS, Declarator::MemberContext); |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 3605 | TypeSourceInfo *TInfo = GetTypeForDeclarator(Dc, S); |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3606 | assert(TInfo && "couldn't build declarator info for anonymous struct/union"); |
John McCall | eb692e0 | 2009-10-22 23:31:08 +0000 | [diff] [blame] | 3607 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3608 | // Create a declaration for this anonymous struct/union. |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 3609 | NamedDecl *Anon = 0; |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3610 | if (RecordDecl *OwningClass = dyn_cast<RecordDecl>(Owner)) { |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3611 | Anon = FieldDecl::Create(Context, OwningClass, |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3612 | DS.getLocStart(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3613 | Record->getLocation(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3614 | /*IdentifierInfo=*/0, |
Argyrios Kyrtzidis | a5d8200 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 3615 | Context.getTypeDeclType(Record), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3616 | TInfo, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3617 | /*BitWidth=*/0, /*Mutable=*/false, |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 3618 | /*InitStyle=*/ICIS_NoInit); |
John McCall | aec0371 | 2010-05-21 20:45:30 +0000 | [diff] [blame] | 3619 | Anon->setAccess(AS); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3620 | if (getLangOpts().CPlusPlus) |
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 3621 | FieldCollector->Add(cast<FieldDecl>(Anon)); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3622 | } else { |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 3623 | DeclSpec::SCS SCSpec = DS.getStorageClassSpec(); |
Rafael Espindola | 65dfa2b | 2013-04-25 12:11:36 +0000 | [diff] [blame] | 3624 | VarDecl::StorageClass SC = StorageClassSpecToVarDeclStorageClass(DS); |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 3625 | if (SCSpec == DeclSpec::SCS_mutable) { |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3626 | // mutable can only appear on non-static class members, so it's always |
3627 | // an error here | ||||
3628 | Diag(Record->getLocation(), diag::err_mutable_nonmember); | ||||
3629 | Invalid = true; | ||||
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 3630 | SC = SC_None; |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3631 | } |
3632 | |||||
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3633 | Anon = VarDecl::Create(Context, Owner, |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3634 | DS.getLocStart(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3635 | Record->getLocation(), /*IdentifierInfo=*/0, |
Argyrios Kyrtzidis | a5d8200 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 3636 | Context.getTypeDeclType(Record), |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 3637 | TInfo, SC); |
Richard Smith | 16ee819 | 2011-09-18 00:06:34 +0000 | [diff] [blame] | 3638 | |
3639 | // Default-initialize the implicit variable. This initialization will be | ||||
3640 | // trivial in almost all cases, except if a union member has an in-class | ||||
3641 | // initializer: | ||||
3642 | // union { int n = 0; }; | ||||
3643 | ActOnUninitializedDecl(Anon, /*TypeMayContainAuto=*/false); | ||||
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3644 | } |
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 3645 | Anon->setImplicit(); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3646 | |
3647 | // Add the anonymous struct/union object to the current | ||||
3648 | // context. We'll be referencing this object when we refer to one of | ||||
3649 | // its members. | ||||
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3650 | Owner->addDecl(Anon); |
Douglas Gregor | fe60f84 | 2010-05-03 15:18:25 +0000 | [diff] [blame] | 3651 | |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3652 | // Inject the members of the anonymous struct/union into the owning |
3653 | // context and into the identifier resolver chain for name lookup | ||||
3654 | // purposes. | ||||
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3655 | SmallVector<NamedDecl*, 2> Chain; |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 3656 | Chain.push_back(Anon); |
3657 | |||||
Francois Pichet | 8e161ed | 2010-11-23 06:07:27 +0000 | [diff] [blame] | 3658 | if (InjectAnonymousStructOrUnionMembers(*this, S, Owner, Record, AS, |
3659 | Chain, false)) | ||||
Douglas Gregor | 4920f1f | 2009-01-12 22:49:06 +0000 | [diff] [blame] | 3660 | Invalid = true; |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3661 | |
3662 | // Mark this as an anonymous struct/union type. Note that we do not | ||||
3663 | // do this until after we have already checked and injected the | ||||
3664 | // members of this anonymous struct/union type, because otherwise | ||||
3665 | // the members could be injected twice: once by DeclContext when it | ||||
3666 | // builds its lookup table, and once by | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3667 | // InjectAnonymousStructOrUnionMembers. |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3668 | Record->setAnonymousStructOrUnion(true); |
3669 | |||||
3670 | if (Invalid) | ||||
3671 | Anon->setInvalidDecl(); | ||||
3672 | |||||
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3673 | return Anon; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3674 | } |
3675 | |||||
Francois Pichet | 8e161ed | 2010-11-23 06:07:27 +0000 | [diff] [blame] | 3676 | /// BuildMicrosoftCAnonymousStruct - Handle the declaration of an |
3677 | /// Microsoft C anonymous structure. | ||||
3678 | /// Ref: http://msdn.microsoft.com/en-us/library/z2cx9y4f.aspx | ||||
3679 | /// Example: | ||||
3680 | /// | ||||
3681 | /// struct A { int a; }; | ||||
3682 | /// struct B { struct A; int b; }; | ||||
3683 | /// | ||||
3684 | /// void foo() { | ||||
3685 | /// B var; | ||||
3686 | /// var.a = 3; | ||||
3687 | /// } | ||||
3688 | /// | ||||
3689 | Decl *Sema::BuildMicrosoftCAnonymousStruct(Scope *S, DeclSpec &DS, | ||||
3690 | RecordDecl *Record) { | ||||
3691 | |||||
3692 | // If there is no Record, get the record via the typedef. | ||||
3693 | if (!Record) | ||||
3694 | Record = DS.getRepAsType().get()->getAsStructureType()->getDecl(); | ||||
3695 | |||||
3696 | // Mock up a declarator. | ||||
3697 | Declarator Dc(DS, Declarator::TypeNameContext); | ||||
3698 | TypeSourceInfo *TInfo = GetTypeForDeclarator(Dc, S); | ||||
3699 | assert(TInfo && "couldn't build declarator info for anonymous struct"); | ||||
3700 | |||||
3701 | // Create a declaration for this anonymous struct. | ||||
3702 | NamedDecl* Anon = FieldDecl::Create(Context, | ||||
3703 | cast<RecordDecl>(CurContext), | ||||
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3704 | DS.getLocStart(), |
3705 | DS.getLocStart(), | ||||
Francois Pichet | 8e161ed | 2010-11-23 06:07:27 +0000 | [diff] [blame] | 3706 | /*IdentifierInfo=*/0, |
3707 | Context.getTypeDeclType(Record), | ||||
3708 | TInfo, | ||||
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3709 | /*BitWidth=*/0, /*Mutable=*/false, |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 3710 | /*InitStyle=*/ICIS_NoInit); |
Francois Pichet | 8e161ed | 2010-11-23 06:07:27 +0000 | [diff] [blame] | 3711 | Anon->setImplicit(); |
3712 | |||||
3713 | // Add the anonymous struct object to the current context. | ||||
3714 | CurContext->addDecl(Anon); | ||||
3715 | |||||
3716 | // Inject the members of the anonymous struct into the current | ||||
3717 | // context and into the identifier resolver chain for name lookup | ||||
3718 | // purposes. | ||||
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3719 | SmallVector<NamedDecl*, 2> Chain; |
Francois Pichet | 8e161ed | 2010-11-23 06:07:27 +0000 | [diff] [blame] | 3720 | Chain.push_back(Anon); |
3721 | |||||
Nico Weber | ee625af | 2012-02-01 00:41:00 +0000 | [diff] [blame] | 3722 | RecordDecl *RecordDef = Record->getDefinition(); |
3723 | if (!RecordDef || InjectAnonymousStructOrUnionMembers(*this, S, CurContext, | ||||
3724 | RecordDef, AS_none, | ||||
3725 | Chain, true)) | ||||
Francois Pichet | 8e161ed | 2010-11-23 06:07:27 +0000 | [diff] [blame] | 3726 | Anon->setInvalidDecl(); |
3727 | |||||
3728 | return Anon; | ||||
3729 | } | ||||
Steve Naroff | f009063 | 2007-09-02 02:04:30 +0000 | [diff] [blame] | 3730 | |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 3731 | /// GetNameForDeclarator - Determine the full declaration name for the |
3732 | /// given Declarator. | ||||
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3733 | DeclarationNameInfo Sema::GetNameForDeclarator(Declarator &D) { |
Douglas Gregor | 02a24ee | 2009-11-03 16:56:39 +0000 | [diff] [blame] | 3734 | return GetNameFromUnqualifiedId(D.getName()); |
3735 | } | ||||
3736 | |||||
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3737 | /// \brief Retrieves the declaration name from a parsed unqualified-id. |
3738 | DeclarationNameInfo | ||||
3739 | Sema::GetNameFromUnqualifiedId(const UnqualifiedId &Name) { | ||||
3740 | DeclarationNameInfo NameInfo; | ||||
3741 | NameInfo.setLoc(Name.StartLocation); | ||||
3742 | |||||
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 3743 | switch (Name.getKind()) { |
Sean Hunt | 0486d74 | 2009-11-28 04:44:28 +0000 | [diff] [blame] | 3744 | |
Fariborz Jahanian | 98a5403 | 2011-07-12 17:16:56 +0000 | [diff] [blame] | 3745 | case UnqualifiedId::IK_ImplicitSelfParam: |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3746 | case UnqualifiedId::IK_Identifier: |
3747 | NameInfo.setName(Name.Identifier); | ||||
3748 | NameInfo.setLoc(Name.StartLocation); | ||||
3749 | return NameInfo; | ||||
Sean Hunt | 0486d74 | 2009-11-28 04:44:28 +0000 | [diff] [blame] | 3750 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3751 | case UnqualifiedId::IK_OperatorFunctionId: |
3752 | NameInfo.setName(Context.DeclarationNames.getCXXOperatorName( | ||||
3753 | Name.OperatorFunctionId.Operator)); | ||||
3754 | NameInfo.setLoc(Name.StartLocation); | ||||
3755 | NameInfo.getInfo().CXXOperatorName.BeginOpNameLoc | ||||
3756 | = Name.OperatorFunctionId.SymbolLocations[0]; | ||||
3757 | NameInfo.getInfo().CXXOperatorName.EndOpNameLoc | ||||
3758 | = Name.EndLocation.getRawEncoding(); | ||||
3759 | return NameInfo; | ||||
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3760 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3761 | case UnqualifiedId::IK_LiteralOperatorId: |
3762 | NameInfo.setName(Context.DeclarationNames.getCXXLiteralOperatorName( | ||||
3763 | Name.Identifier)); | ||||
3764 | NameInfo.setLoc(Name.StartLocation); | ||||
3765 | NameInfo.setCXXLiteralOperatorNameLoc(Name.EndLocation); | ||||
3766 | return NameInfo; | ||||
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3767 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3768 | case UnqualifiedId::IK_ConversionFunctionId: { |
3769 | TypeSourceInfo *TInfo; | ||||
3770 | QualType Ty = GetTypeFromParser(Name.ConversionFunctionId, &TInfo); | ||||
3771 | if (Ty.isNull()) | ||||
3772 | return DeclarationNameInfo(); | ||||
3773 | NameInfo.setName(Context.DeclarationNames.getCXXConversionFunctionName( | ||||
3774 | Context.getCanonicalType(Ty))); | ||||
3775 | NameInfo.setLoc(Name.StartLocation); | ||||
3776 | NameInfo.setNamedTypeInfo(TInfo); | ||||
3777 | return NameInfo; | ||||
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 3778 | } |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3779 | |
3780 | case UnqualifiedId::IK_ConstructorName: { | ||||
3781 | TypeSourceInfo *TInfo; | ||||
3782 | QualType Ty = GetTypeFromParser(Name.ConstructorName, &TInfo); | ||||
3783 | if (Ty.isNull()) | ||||
3784 | return DeclarationNameInfo(); | ||||
3785 | NameInfo.setName(Context.DeclarationNames.getCXXConstructorName( | ||||
3786 | Context.getCanonicalType(Ty))); | ||||
3787 | NameInfo.setLoc(Name.StartLocation); | ||||
3788 | NameInfo.setNamedTypeInfo(TInfo); | ||||
3789 | return NameInfo; | ||||
3790 | } | ||||
3791 | |||||
3792 | case UnqualifiedId::IK_ConstructorTemplateId: { | ||||
3793 | // In well-formed code, we can only have a constructor | ||||
3794 | // template-id that refers to the current context, so go there | ||||
3795 | // to find the actual type being constructed. | ||||
3796 | CXXRecordDecl *CurClass = dyn_cast<CXXRecordDecl>(CurContext); | ||||
3797 | if (!CurClass || CurClass->getIdentifier() != Name.TemplateId->Name) | ||||
3798 | return DeclarationNameInfo(); | ||||
3799 | |||||
3800 | // Determine the type of the class being constructed. | ||||
3801 | QualType CurClassType = Context.getTypeDeclType(CurClass); | ||||
3802 | |||||
3803 | // FIXME: Check two things: that the template-id names the same type as | ||||
3804 | // CurClassType, and that the template-id does not occur when the name | ||||
3805 | // was qualified. | ||||
3806 | |||||
3807 | NameInfo.setName(Context.DeclarationNames.getCXXConstructorName( | ||||
3808 | Context.getCanonicalType(CurClassType))); | ||||
3809 | NameInfo.setLoc(Name.StartLocation); | ||||
3810 | // FIXME: should we retrieve TypeSourceInfo? | ||||
3811 | NameInfo.setNamedTypeInfo(0); | ||||
3812 | return NameInfo; | ||||
3813 | } | ||||
3814 | |||||
3815 | case UnqualifiedId::IK_DestructorName: { | ||||
3816 | TypeSourceInfo *TInfo; | ||||
3817 | QualType Ty = GetTypeFromParser(Name.DestructorName, &TInfo); | ||||
3818 | if (Ty.isNull()) | ||||
3819 | return DeclarationNameInfo(); | ||||
3820 | NameInfo.setName(Context.DeclarationNames.getCXXDestructorName( | ||||
3821 | Context.getCanonicalType(Ty))); | ||||
3822 | NameInfo.setLoc(Name.StartLocation); | ||||
3823 | NameInfo.setNamedTypeInfo(TInfo); | ||||
3824 | return NameInfo; | ||||
3825 | } | ||||
3826 | |||||
3827 | case UnqualifiedId::IK_TemplateId: { | ||||
John McCall | 2b5289b | 2010-08-23 07:28:44 +0000 | [diff] [blame] | 3828 | TemplateName TName = Name.TemplateId->Template.get(); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3829 | SourceLocation TNameLoc = Name.TemplateId->TemplateNameLoc; |
3830 | return Context.getNameForTemplate(TName, TNameLoc); | ||||
3831 | } | ||||
3832 | |||||
3833 | } // switch (Name.getKind()) | ||||
3834 | |||||
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3835 | llvm_unreachable("Unknown name kind"); |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 3836 | } |
3837 | |||||
Kaelyn Uhrain | 4d9d157 | 2011-08-04 17:40:00 +0000 | [diff] [blame] | 3838 | static QualType getCoreType(QualType Ty) { |
3839 | do { | ||||
3840 | if (Ty->isPointerType() || Ty->isReferenceType()) | ||||
3841 | Ty = Ty->getPointeeType(); | ||||
3842 | else if (Ty->isArrayType()) | ||||
3843 | Ty = Ty->castAsArrayTypeUnsafe()->getElementType(); | ||||
3844 | else | ||||
3845 | return Ty.withoutLocalFastQualifiers(); | ||||
3846 | } while (true); | ||||
3847 | } | ||||
3848 | |||||
Kaelyn Uhrain | 2afd766 | 2011-10-11 00:28:39 +0000 | [diff] [blame] | 3849 | /// hasSimilarParameters - Determine whether the C++ functions Declaration |
3850 | /// and Definition have "nearly" matching parameters. This heuristic is | ||||
3851 | /// used to improve diagnostics in the case where an out-of-line function | ||||
3852 | /// definition doesn't match any declaration within the class or namespace. | ||||
3853 | /// Also sets Params to the list of indices to the parameters that differ | ||||
3854 | /// between the declaration and the definition. If hasSimilarParameters | ||||
3855 | /// returns true and Params is empty, then all of the parameters match. | ||||
3856 | static bool hasSimilarParameters(ASTContext &Context, | ||||
Douglas Gregor | 4ce205f | 2009-02-06 17:46:57 +0000 | [diff] [blame] | 3857 | FunctionDecl *Declaration, |
Kaelyn Uhrain | 4d9d157 | 2011-08-04 17:40:00 +0000 | [diff] [blame] | 3858 | FunctionDecl *Definition, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 3859 | SmallVectorImpl<unsigned> &Params) { |
Kaelyn Uhrain | 4d9d157 | 2011-08-04 17:40:00 +0000 | [diff] [blame] | 3860 | Params.clear(); |
Douglas Gregor | 584049d | 2008-12-15 23:53:10 +0000 | [diff] [blame] | 3861 | if (Declaration->param_size() != Definition->param_size()) |
3862 | return false; | ||||
3863 | for (unsigned Idx = 0; Idx < Declaration->param_size(); ++Idx) { | ||||
3864 | QualType DeclParamTy = Declaration->getParamDecl(Idx)->getType(); | ||||
3865 | QualType DefParamTy = Definition->getParamDecl(Idx)->getType(); | ||||
3866 | |||||
Kaelyn Uhrain | 4d9d157 | 2011-08-04 17:40:00 +0000 | [diff] [blame] | 3867 | // The parameter types are identical |
Matt Beaumont-Gay | 903d6dc | 2011-08-23 01:35:51 +0000 | [diff] [blame] | 3868 | if (Context.hasSameType(DefParamTy, DeclParamTy)) |
Kaelyn Uhrain | 4d9d157 | 2011-08-04 17:40:00 +0000 | [diff] [blame] | 3869 | continue; |
3870 | |||||
3871 | QualType DeclParamBaseTy = getCoreType(DeclParamTy); | ||||
3872 | QualType DefParamBaseTy = getCoreType(DefParamTy); | ||||
3873 | const IdentifierInfo *DeclTyName = DeclParamBaseTy.getBaseTypeIdentifier(); | ||||
3874 | const IdentifierInfo *DefTyName = DefParamBaseTy.getBaseTypeIdentifier(); | ||||
3875 | |||||
3876 | if (Context.hasSameUnqualifiedType(DeclParamBaseTy, DefParamBaseTy) || | ||||
3877 | (DeclTyName && DeclTyName == DefTyName)) | ||||
3878 | Params.push_back(Idx); | ||||
3879 | else // The two parameters aren't even close | ||||
Douglas Gregor | 584049d | 2008-12-15 23:53:10 +0000 | [diff] [blame] | 3880 | return false; |
3881 | } | ||||
3882 | |||||
3883 | return true; | ||||
3884 | } | ||||
3885 | |||||
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 3886 | /// NeedsRebuildingInCurrentInstantiation - Checks whether the given |
3887 | /// declarator needs to be rebuilt in the current instantiation. | ||||
3888 | /// Any bits of declarator which appear before the name are valid for | ||||
3889 | /// consideration here. That's specifically the type in the decl spec | ||||
3890 | /// and the base type in any member-pointer chunks. | ||||
3891 | static bool RebuildDeclaratorInCurrentInstantiation(Sema &S, Declarator &D, | ||||
3892 | DeclarationName Name) { | ||||
3893 | // The types we specifically need to rebuild are: | ||||
3894 | // - typenames, typeofs, and decltypes | ||||
3895 | // - types which will become injected class names | ||||
3896 | // Of course, we also need to rebuild any type referencing such a | ||||
3897 | // type. It's safest to just say "dependent", but we call out a | ||||
3898 | // few cases here. | ||||
3899 | |||||
3900 | DeclSpec &DS = D.getMutableDeclSpec(); | ||||
3901 | switch (DS.getTypeSpecType()) { | ||||
3902 | case DeclSpec::TST_typename: | ||||
3903 | case DeclSpec::TST_typeofType: | ||||
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3904 | case DeclSpec::TST_underlyingType: |
3905 | case DeclSpec::TST_atomic: { | ||||
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 3906 | // Grab the type from the parser. |
3907 | TypeSourceInfo *TSI = 0; | ||||
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3908 | QualType T = S.GetTypeFromParser(DS.getRepAsType(), &TSI); |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 3909 | if (T.isNull() || !T->isDependentType()) break; |
3910 | |||||
3911 | // Make sure there's a type source info. This isn't really much | ||||
3912 | // of a waste; most dependent types should have type source info | ||||
3913 | // attached already. | ||||
3914 | if (!TSI) | ||||
3915 | TSI = S.Context.getTrivialTypeSourceInfo(T, DS.getTypeSpecTypeLoc()); | ||||
3916 | |||||
3917 | // Rebuild the type in the current instantiation. | ||||
3918 | TSI = S.RebuildTypeInCurrentInstantiation(TSI, D.getIdentifierLoc(), Name); | ||||
3919 | if (!TSI) return true; | ||||
3920 | |||||
3921 | // Store the new type back in the decl spec. | ||||
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3922 | ParsedType LocType = S.CreateParsedType(TSI->getType(), TSI); |
3923 | DS.UpdateTypeRep(LocType); | ||||
3924 | break; | ||||
3925 | } | ||||
3926 | |||||
Richard Smith | c4a8391 | 2012-10-01 20:35:07 +0000 | [diff] [blame] | 3927 | case DeclSpec::TST_decltype: |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3928 | case DeclSpec::TST_typeofExpr: { |
3929 | Expr *E = DS.getRepAsExpr(); | ||||
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3930 | ExprResult Result = S.RebuildExprInCurrentInstantiation(E); |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3931 | if (Result.isInvalid()) return true; |
3932 | DS.UpdateExprRep(Result.get()); | ||||
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 3933 | break; |
3934 | } | ||||
3935 | |||||
3936 | default: | ||||
3937 | // Nothing to do for these decl specs. | ||||
3938 | break; | ||||
3939 | } | ||||
3940 | |||||
3941 | // It doesn't matter what order we do this in. | ||||
3942 | for (unsigned I = 0, E = D.getNumTypeObjects(); I != E; ++I) { | ||||
3943 | DeclaratorChunk &Chunk = D.getTypeObject(I); | ||||
3944 | |||||
3945 | // The only type information in the declarator which can come | ||||
3946 | // before the declaration name is the base type of a member | ||||
3947 | // pointer. | ||||
3948 | if (Chunk.Kind != DeclaratorChunk::MemberPointer) | ||||
3949 | continue; | ||||
3950 | |||||
3951 | // Rebuild the scope specifier in-place. | ||||
3952 | CXXScopeSpec &SS = Chunk.Mem.Scope(); | ||||
3953 | if (S.RebuildNestedNameSpecifierInCurrentInstantiation(SS)) | ||||
3954 | return true; | ||||
3955 | } | ||||
3956 | |||||
3957 | return false; | ||||
3958 | } | ||||
3959 | |||||
Anders Carlsson | 3242ee0 | 2011-07-04 16:28:17 +0000 | [diff] [blame] | 3960 | Decl *Sema::ActOnDeclarator(Scope *S, Declarator &D) { |
Douglas Gregor | 45fa560 | 2011-11-07 20:56:01 +0000 | [diff] [blame] | 3961 | D.setFunctionDefinitionKind(FDK_Declaration); |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 3962 | Decl *Dcl = HandleDeclarator(S, D, MultiTemplateParamsArg()); |
Argyrios Kyrtzidis | c14a03d | 2011-11-23 20:27:36 +0000 | [diff] [blame] | 3963 | |
3964 | if (OriginalLexicalContext && OriginalLexicalContext->isObjCContainer() && | ||||
Douglas Gregor | e7be109 | 2012-04-30 18:13:01 +0000 | [diff] [blame] | 3965 | Dcl && Dcl->getDeclContext()->isFileContext()) |
Argyrios Kyrtzidis | c14a03d | 2011-11-23 20:27:36 +0000 | [diff] [blame] | 3966 | Dcl->setTopLevelDeclInObjCContainer(); |
3967 | |||||
3968 | return Dcl; | ||||
John McCall | 7cd088e | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 3969 | } |
3970 | |||||
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3971 | /// DiagnoseClassNameShadow - Implement C++ [class.mem]p13: |
3972 | /// If T is the name of a class, then each of the following shall have a | ||||
3973 | /// name different from T: | ||||
3974 | /// - every static data member of class T; | ||||
3975 | /// - every member function of class T | ||||
3976 | /// - every member of class T that is itself a type; | ||||
3977 | /// \returns true if the declaration name violates these rules. | ||||
3978 | bool Sema::DiagnoseClassNameShadow(DeclContext *DC, | ||||
3979 | DeclarationNameInfo NameInfo) { | ||||
3980 | DeclarationName Name = NameInfo.getName(); | ||||
3981 | |||||
3982 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(DC)) | ||||
3983 | if (Record->getIdentifier() && Record->getDeclName() == Name) { | ||||
3984 | Diag(NameInfo.getLoc(), diag::err_member_name_of_class) << Name; | ||||
3985 | return true; | ||||
3986 | } | ||||
3987 | |||||
3988 | return false; | ||||
3989 | } | ||||
Douglas Gregor | 42acead | 2012-03-17 23:06:31 +0000 | [diff] [blame] | 3990 | |
Douglas Gregor | 6960587 | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 3991 | /// \brief Diagnose a declaration whose declarator-id has the given |
3992 | /// nested-name-specifier. | ||||
3993 | /// | ||||
3994 | /// \param SS The nested-name-specifier of the declarator-id. | ||||
3995 | /// | ||||
3996 | /// \param DC The declaration context to which the nested-name-specifier | ||||
3997 | /// resolves. | ||||
3998 | /// | ||||
3999 | /// \param Name The name of the entity being declared. | ||||
4000 | /// | ||||
4001 | /// \param Loc The location of the name of the entity being declared. | ||||
Douglas Gregor | 42acead | 2012-03-17 23:06:31 +0000 | [diff] [blame] | 4002 | /// |
4003 | /// \returns true if we cannot safely recover from this error, false otherwise. | ||||
Douglas Gregor | 6960587 | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 4004 | bool Sema::diagnoseQualifiedDeclaration(CXXScopeSpec &SS, DeclContext *DC, |
Douglas Gregor | 42acead | 2012-03-17 23:06:31 +0000 | [diff] [blame] | 4005 | DeclarationName Name, |
Douglas Gregor | 6960587 | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 4006 | SourceLocation Loc) { |
4007 | DeclContext *Cur = CurContext; | ||||
Eli Friedman | a03c5ee | 2013-08-12 21:54:01 +0000 | [diff] [blame] | 4008 | while (isa<LinkageSpecDecl>(Cur) || isa<CapturedDecl>(Cur)) |
Douglas Gregor | 6960587 | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 4009 | Cur = Cur->getParent(); |
4010 | |||||
4011 | // C++ [dcl.meaning]p1: | ||||
4012 | // A declarator-id shall not be qualified except for the definition | ||||
4013 | // of a member function (9.3) or static data member (9.4) outside of | ||||
4014 | // its class, the definition or explicit instantiation of a function | ||||
4015 | // or variable member of a namespace outside of its namespace, or the | ||||
4016 | // definition of an explicit specialization outside of its namespace, | ||||
4017 | // or the declaration of a friend function that is a member of | ||||
4018 | // another class or namespace (11.3). [...] | ||||
4019 | |||||
4020 | // The user provided a superfluous scope specifier that refers back to the | ||||
4021 | // class or namespaces in which the entity is already declared. | ||||
Douglas Gregor | 42acead | 2012-03-17 23:06:31 +0000 | [diff] [blame] | 4022 | // |
4023 | // class X { | ||||
4024 | // void X::f(); | ||||
4025 | // }; | ||||
Douglas Gregor | 6960587 | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 4026 | if (Cur->Equals(DC)) { |
Douglas Gregor | 7537945 | 2012-09-13 20:16:20 +0000 | [diff] [blame] | 4027 | Diag(Loc, LangOpts.MicrosoftExt? diag::warn_member_extra_qualification |
4028 | : diag::err_member_extra_qualification) | ||||
Douglas Gregor | 42acead | 2012-03-17 23:06:31 +0000 | [diff] [blame] | 4029 | << Name << FixItHint::CreateRemoval(SS.getRange()); |
4030 | SS.clear(); | ||||
4031 | return false; | ||||
4032 | } | ||||
Douglas Gregor | 6960587 | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 4033 | |
4034 | // Check whether the qualifying scope encloses the scope of the original | ||||
4035 | // declaration. | ||||
4036 | if (!Cur->Encloses(DC)) { | ||||
4037 | if (Cur->isRecord()) | ||||
4038 | Diag(Loc, diag::err_member_qualification) | ||||
4039 | << Name << SS.getRange(); | ||||
4040 | else if (isa<TranslationUnitDecl>(DC)) | ||||
4041 | Diag(Loc, diag::err_invalid_declarator_global_scope) | ||||
4042 | << Name << SS.getRange(); | ||||
4043 | else if (isa<FunctionDecl>(Cur)) | ||||
4044 | Diag(Loc, diag::err_invalid_declarator_in_function) | ||||
4045 | << Name << SS.getRange(); | ||||
Eli Friedman | a03c5ee | 2013-08-12 21:54:01 +0000 | [diff] [blame] | 4046 | else if (isa<BlockDecl>(Cur)) |
4047 | Diag(Loc, diag::err_invalid_declarator_in_block) | ||||
4048 | << Name << SS.getRange(); | ||||
Douglas Gregor | 6960587 | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 4049 | else |
4050 | Diag(Loc, diag::err_invalid_declarator_scope) | ||||
Richard Smith | a1c4f7c | 2012-04-13 04:07:40 +0000 | [diff] [blame] | 4051 | << Name << cast<NamedDecl>(Cur) << cast<NamedDecl>(DC) << SS.getRange(); |
Douglas Gregor | 6960587 | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 4052 | |
Douglas Gregor | 42acead | 2012-03-17 23:06:31 +0000 | [diff] [blame] | 4053 | return true; |
Douglas Gregor | 6960587 | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 4054 | } |
4055 | |||||
4056 | if (Cur->isRecord()) { | ||||
4057 | // Cannot qualify members within a class. | ||||
4058 | Diag(Loc, diag::err_member_qualification) | ||||
4059 | << Name << SS.getRange(); | ||||
4060 | SS.clear(); | ||||
4061 | |||||
4062 | // C++ constructors and destructors with incorrect scopes can break | ||||
4063 | // our AST invariants by having the wrong underlying types. If | ||||
4064 | // that's the case, then drop this declaration entirely. | ||||
4065 | if ((Name.getNameKind() == DeclarationName::CXXConstructorName || | ||||
4066 | Name.getNameKind() == DeclarationName::CXXDestructorName) && | ||||
4067 | !Context.hasSameType(Name.getCXXNameType(), | ||||
4068 | Context.getTypeDeclType(cast<CXXRecordDecl>(Cur)))) | ||||
4069 | return true; | ||||
4070 | |||||
4071 | return false; | ||||
4072 | } | ||||
Douglas Gregor | 42acead | 2012-03-17 23:06:31 +0000 | [diff] [blame] | 4073 | |
Douglas Gregor | 6960587 | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 4074 | // C++11 [dcl.meaning]p1: |
4075 | // [...] "The nested-name-specifier of the qualified declarator-id shall | ||||
4076 | // not begin with a decltype-specifer" | ||||
4077 | NestedNameSpecifierLoc SpecLoc(SS.getScopeRep(), SS.location_data()); | ||||
4078 | while (SpecLoc.getPrefix()) | ||||
4079 | SpecLoc = SpecLoc.getPrefix(); | ||||
4080 | if (dyn_cast_or_null<DecltypeType>( | ||||
4081 | SpecLoc.getNestedNameSpecifier()->getAsType())) | ||||
4082 | Diag(Loc, diag::err_decltype_in_declarator) | ||||
4083 | << SpecLoc.getTypeLoc().getSourceRange(); | ||||
4084 | |||||
Douglas Gregor | 42acead | 2012-03-17 23:06:31 +0000 | [diff] [blame] | 4085 | return false; |
4086 | } | ||||
4087 | |||||
Rafael Espindola | fc35cbc | 2013-01-08 20:44:06 +0000 | [diff] [blame] | 4088 | NamedDecl *Sema::HandleDeclarator(Scope *S, Declarator &D, |
4089 | MultiTemplateParamsArg TemplateParamLists) { | ||||
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4090 | // TODO: consider using NameInfo for diagnostic. |
4091 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); | ||||
4092 | DeclarationName Name = NameInfo.getName(); | ||||
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 4093 | |
Chris Lattner | e80a59c | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4094 | // All of these full declarators require an identifier. If it doesn't have |
4095 | // one, the ParsedFreeStandingDeclSpec action should be used. | ||||
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 4096 | if (!Name) { |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 4097 | if (!D.isInvalidType()) // Reject this if we think it is valid. |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4098 | Diag(D.getDeclSpec().getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 4099 | diag::err_declarator_need_ident) |
4100 | << D.getDeclSpec().getSourceRange() << D.getSourceRange(); | ||||
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4101 | return 0; |
Douglas Gregor | 56c0458 | 2010-12-16 00:46:58 +0000 | [diff] [blame] | 4102 | } else if (DiagnoseUnexpandedParameterPack(NameInfo, UPPC_DeclarationType)) |
4103 | return 0; | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4104 | |
Chris Lattner | 31e0572 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 4105 | // The scope passed in may not be a decl scope. Zip up the scope tree until |
4106 | // we find one that is. | ||||
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 4107 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 4108 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
Chris Lattner | 31e0572 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 4109 | S = S->getParent(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4110 | |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 4111 | DeclContext *DC = CurContext; |
4112 | if (D.getCXXScopeSpec().isInvalid()) | ||||
4113 | D.setInvalidType(); | ||||
4114 | else if (D.getCXXScopeSpec().isSet()) { | ||||
Douglas Gregor | 6ccab97 | 2010-12-16 01:14:37 +0000 | [diff] [blame] | 4115 | if (DiagnoseUnexpandedParameterPack(D.getCXXScopeSpec(), |
4116 | UPPC_DeclarationQualifier)) | ||||
4117 | return 0; | ||||
4118 | |||||
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 4119 | bool EnteringContext = !D.getDeclSpec().isFriendSpecified(); |
4120 | DC = computeDeclContext(D.getCXXScopeSpec(), EnteringContext); | ||||
4121 | if (!DC) { | ||||
4122 | // If we could not compute the declaration context, it's because the | ||||
4123 | // declaration context is dependent but does not refer to a class, | ||||
4124 | // class template, or class template partial specialization. Complain | ||||
4125 | // and return early, to avoid the coming semantic disaster. | ||||
4126 | Diag(D.getIdentifierLoc(), | ||||
4127 | diag::err_template_qualified_declarator_no_match) | ||||
4128 | << (NestedNameSpecifier*)D.getCXXScopeSpec().getScopeRep() | ||||
4129 | << D.getCXXScopeSpec().getRange(); | ||||
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4130 | return 0; |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 4131 | } |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 4132 | bool IsDependentContext = DC->isDependentContext(); |
John McCall | 0dd7ceb | 2009-12-19 09:35:56 +0000 | [diff] [blame] | 4133 | |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 4134 | if (!IsDependentContext && |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 4135 | RequireCompleteDeclContext(D.getCXXScopeSpec(), DC)) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4136 | return 0; |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 4137 | |
Douglas Gregor | 6960587 | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 4138 | if (isa<CXXRecordDecl>(DC) && !cast<CXXRecordDecl>(DC)->hasDefinition()) { |
4139 | Diag(D.getIdentifierLoc(), | ||||
4140 | diag::err_member_def_undefined_record) | ||||
4141 | << Name << DC << D.getCXXScopeSpec().getRange(); | ||||
4142 | D.setInvalidType(); | ||||
4143 | } else if (!D.getDeclSpec().isFriendSpecified()) { | ||||
4144 | if (diagnoseQualifiedDeclaration(D.getCXXScopeSpec(), DC, | ||||
4145 | Name, D.getIdentifierLoc())) { | ||||
4146 | if (DC->isRecord()) | ||||
Douglas Gregor | 42acead | 2012-03-17 23:06:31 +0000 | [diff] [blame] | 4147 | return 0; |
Douglas Gregor | 6960587 | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 4148 | |
4149 | D.setInvalidType(); | ||||
Douglas Gregor | 922fff2 | 2010-10-13 22:19:53 +0000 | [diff] [blame] | 4150 | } |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 4151 | } |
4152 | |||||
4153 | // Check whether we need to rebuild the type of the given | ||||
4154 | // declaration in the current instantiation. | ||||
4155 | if (EnteringContext && IsDependentContext && | ||||
4156 | TemplateParamLists.size() != 0) { | ||||
4157 | ContextRAII SavedContext(*this, DC); | ||||
4158 | if (RebuildDeclaratorInCurrentInstantiation(*this, D, Name)) | ||||
4159 | D.setInvalidType(); | ||||
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 4160 | } |
4161 | } | ||||
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4162 | |
4163 | if (DiagnoseClassNameShadow(DC, NameInfo)) | ||||
4164 | // If this is a typedef, we'll end up spewing multiple diagnostics. | ||||
4165 | // Just return early; it's safer. | ||||
4166 | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) | ||||
4167 | return 0; | ||||
Douglas Gregor | a6e937c | 2010-10-15 13:21:21 +0000 | [diff] [blame] | 4168 | |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 4169 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
4170 | QualType R = TInfo->getType(); | ||||
Douglas Gregor | d6f7e9d | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 4171 | |
Douglas Gregor | d093722 | 2010-12-13 22:49:22 +0000 | [diff] [blame] | 4172 | if (DiagnoseUnexpandedParameterPack(D.getIdentifierLoc(), TInfo, |
4173 | UPPC_DeclarationType)) | ||||
4174 | D.setInvalidType(); | ||||
4175 | |||||
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4176 | LookupResult Previous(*this, NameInfo, LookupOrdinaryName, |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4177 | ForRedeclaration); |
4178 | |||||
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 4179 | // 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] | 4180 | if (!D.getCXXScopeSpec().isSet()) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4181 | bool IsLinkageLookup = false; |
Richard Smith | dd9459f | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 4182 | bool CreateBuiltins = false; |
Douglas Gregor | d6f7e9d | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 4183 | |
4184 | // If the declaration we're planning to build will be a function | ||||
4185 | // or object with linkage, then look for another declaration with | ||||
4186 | // linkage (C99 6.2.2p4-5 and C++ [basic.link]p6). | ||||
Richard Smith | dd9459f | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 4187 | // |
4188 | // If the declaration we're planning to build will be declared with | ||||
4189 | // external linkage in the translation unit, create any builtin with | ||||
4190 | // the same name. | ||||
Douglas Gregor | d6f7e9d | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 4191 | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) |
4192 | /* Do nothing*/; | ||||
Richard Smith | dd9459f | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 4193 | else if (CurContext->isFunctionOrMethod() && |
4194 | (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_extern || | ||||
4195 | R->isFunctionType())) { | ||||
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4196 | IsLinkageLookup = true; |
Richard Smith | dd9459f | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 4197 | CreateBuiltins = |
4198 | CurContext->getEnclosingNamespaceContext()->isTranslationUnit(); | ||||
4199 | } else if (CurContext->getRedeclContext()->isTranslationUnit() && | ||||
4200 | D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_static) | ||||
4201 | CreateBuiltins = true; | ||||
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4202 | |
4203 | if (IsLinkageLookup) | ||||
4204 | Previous.clear(LookupRedeclarationWithLinkage); | ||||
Douglas Gregor | d6f7e9d | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 4205 | |
Richard Smith | dd9459f | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 4206 | LookupName(Previous, S, CreateBuiltins); |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 4207 | } else { // Something like "int foo::x;" |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4208 | LookupQualifiedName(Previous, DC); |
4209 | |||||
Douglas Gregor | 6960587 | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 4210 | // C++ [dcl.meaning]p1: |
4211 | // When the declarator-id is qualified, the declaration shall refer to a | ||||
4212 | // previously declared member of the class or namespace to which the | ||||
4213 | // qualifier refers (or, in the case of a namespace, of an element of the | ||||
4214 | // inline namespace set of that namespace (7.3.1)) or to a specialization | ||||
4215 | // thereof; [...] | ||||
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 4216 | // |
Douglas Gregor | 6960587 | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 4217 | // Note that we already checked the context above, and that we do not have |
4218 | // enough information to make sure that Previous contains the declaration | ||||
4219 | // we want to match. For example, given: | ||||
Douglas Gregor | 584049d | 2008-12-15 23:53:10 +0000 | [diff] [blame] | 4220 | // |
Douglas Gregor | 9d35097 | 2008-12-12 08:25:50 +0000 | [diff] [blame] | 4221 | // class X { |
4222 | // void f(); | ||||
Douglas Gregor | 584049d | 2008-12-15 23:53:10 +0000 | [diff] [blame] | 4223 | // void f(float); |
Douglas Gregor | 9d35097 | 2008-12-12 08:25:50 +0000 | [diff] [blame] | 4224 | // }; |
4225 | // | ||||
Douglas Gregor | 584049d | 2008-12-15 23:53:10 +0000 | [diff] [blame] | 4226 | // void X::f(int) { } // ill-formed |
4227 | // | ||||
Douglas Gregor | 6960587 | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 4228 | // In this case, Previous will point to the overload set |
Douglas Gregor | 584049d | 2008-12-15 23:53:10 +0000 | [diff] [blame] | 4229 | // containing the two f's declared in X, but neither of them |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4230 | // matches. |
Douglas Gregor | 6960587 | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 4231 | |
4232 | // C++ [dcl.meaning]p1: | ||||
4233 | // [...] the member shall not merely have been introduced by a | ||||
4234 | // using-declaration in the scope of the class or namespace nominated by | ||||
4235 | // the nested-name-specifier of the declarator-id. | ||||
4236 | RemoveUsingDecls(Previous); | ||||
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 4237 | } |
4238 | |||||
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4239 | if (Previous.isSingleResult() && |
4240 | Previous.getFoundDecl()->isTemplateParameter()) { | ||||
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 4241 | // Maybe we will complain about the shadowed template parameter. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4242 | if (!D.isInvalidType()) |
Douglas Gregor | cb8f951 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 4243 | DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), |
4244 | Previous.getFoundDecl()); | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4245 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 4246 | // Just pretend that we didn't see the previous declaration. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4247 | Previous.clear(); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 4248 | } |
4249 | |||||
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 4250 | // In C++, the previous declaration we find might be a tag type |
4251 | // (class or enum). In this case, the new declaration will hide the | ||||
Douglas Gregor | 6697312 | 2009-01-28 17:15:10 +0000 | [diff] [blame] | 4252 | // tag type. Note that this does does not apply if we're declaring a |
4253 | // typedef (C++ [dcl.typedef]p4). | ||||
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4254 | if (Previous.isSingleTagDecl() && |
Douglas Gregor | 6697312 | 2009-01-28 17:15:10 +0000 | [diff] [blame] | 4255 | D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef) |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4256 | Previous.clear(); |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 4257 | |
Richard Smith | 3cdbbdc | 2013-03-06 01:37:38 +0000 | [diff] [blame] | 4258 | // Check that there are no default arguments other than in the parameters |
4259 | // of a function declaration (C++ only). | ||||
4260 | if (getLangOpts().CPlusPlus) | ||||
4261 | CheckExtraCXXDefaultArguments(D); | ||||
4262 | |||||
Nico Weber | e6bb76c | 2012-12-23 00:40:46 +0000 | [diff] [blame] | 4263 | NamedDecl *New; |
4264 | |||||
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 4265 | bool AddToScope = true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4266 | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) { |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 4267 | if (TemplateParamLists.size()) { |
4268 | Diag(D.getIdentifierLoc(), diag::err_template_typedef); | ||||
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4269 | return 0; |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 4270 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4271 | |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 4272 | New = ActOnTypedefDeclarator(S, D, DC, TInfo, Previous); |
Douglas Gregor | d6f7e9d | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 4273 | } else if (R->isFunctionType()) { |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 4274 | New = ActOnFunctionDeclarator(S, D, DC, TInfo, Previous, |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 4275 | TemplateParamLists, |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 4276 | AddToScope); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4277 | } else { |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4278 | New = ActOnVariableDeclarator(S, D, DC, TInfo, Previous, TemplateParamLists, |
4279 | AddToScope); | ||||
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4280 | } |
Zhongxing Xu | 416fcaf | 2009-01-16 01:13:29 +0000 | [diff] [blame] | 4281 | |
4282 | if (New == 0) | ||||
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4283 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4284 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4285 | // If this has an identifier and is not an invalid redeclaration or |
4286 | // function template specialization, add it to the scope stack. | ||||
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 4287 | if (New->getDeclName() && AddToScope && |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 4288 | !(D.isRedeclaration() && New->isInvalidDecl())) |
Argyrios Kyrtzidis | 87f3ff0 | 2008-04-12 00:47:19 +0000 | [diff] [blame] | 4289 | PushOnScopeChains(New, S); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4290 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4291 | return New; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4292 | } |
4293 | |||||
Abramo Bagnara | 88adb98 | 2012-11-08 16:27:30 +0000 | [diff] [blame] | 4294 | /// Helper method to turn variable array types into constant array |
4295 | /// types in certain situations which would otherwise be errors (for | ||||
4296 | /// GCC compatibility). | ||||
Eli Friedman | 1ca4813 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 4297 | static QualType TryToFixInvalidVariablyModifiedType(QualType T, |
4298 | ASTContext &Context, | ||||
Douglas Gregor | 2767ce2 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 4299 | bool &SizeIsNegative, |
4300 | llvm::APSInt &Oversized) { | ||||
Eli Friedman | 1ca4813 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 4301 | // This method tries to turn a variable array into a constant |
4302 | // array even when the size isn't an ICE. This is necessary | ||||
4303 | // for compatibility with code that depends on gcc's buggy | ||||
4304 | // constant expression folding, like struct {char x[(int)(char*)2];} | ||||
4305 | SizeIsNegative = false; | ||||
Douglas Gregor | 2767ce2 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 4306 | Oversized = 0; |
4307 | |||||
4308 | if (T->isDependentType()) | ||||
4309 | return QualType(); | ||||
4310 | |||||
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4311 | QualifierCollector Qs; |
4312 | const Type *Ty = Qs.strip(T); | ||||
4313 | |||||
4314 | if (const PointerType* PTy = dyn_cast<PointerType>(Ty)) { | ||||
Eli Friedman | 1ca4813 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 4315 | QualType Pointee = PTy->getPointeeType(); |
4316 | QualType FixedType = | ||||
Douglas Gregor | 2767ce2 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 4317 | TryToFixInvalidVariablyModifiedType(Pointee, Context, SizeIsNegative, |
4318 | Oversized); | ||||
Eli Friedman | 1ca4813 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 4319 | if (FixedType.isNull()) return FixedType; |
Eli Friedman | 61125c8 | 2009-02-21 00:58:02 +0000 | [diff] [blame] | 4320 | FixedType = Context.getPointerType(FixedType); |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 4321 | return Qs.apply(Context, FixedType); |
Eli Friedman | 1ca4813 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 4322 | } |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 4323 | if (const ParenType* PTy = dyn_cast<ParenType>(Ty)) { |
4324 | QualType Inner = PTy->getInnerType(); | ||||
4325 | QualType FixedType = | ||||
4326 | TryToFixInvalidVariablyModifiedType(Inner, Context, SizeIsNegative, | ||||
4327 | Oversized); | ||||
4328 | if (FixedType.isNull()) return FixedType; | ||||
4329 | FixedType = Context.getParenType(FixedType); | ||||
4330 | return Qs.apply(Context, FixedType); | ||||
4331 | } | ||||
Eli Friedman | 1ca4813 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 4332 | |
4333 | const VariableArrayType* VLATy = dyn_cast<VariableArrayType>(T); | ||||
Eli Friedman | bc592e6 | 2009-02-26 03:58:54 +0000 | [diff] [blame] | 4334 | if (!VLATy) |
4335 | return QualType(); | ||||
4336 | // FIXME: We should probably handle this case | ||||
4337 | if (VLATy->getElementType()->isVariablyModifiedType()) | ||||
4338 | return QualType(); | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4339 | |
Richard Smith | aa9c350 | 2011-12-07 00:43:50 +0000 | [diff] [blame] | 4340 | llvm::APSInt Res; |
Eli Friedman | 1ca4813 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 4341 | if (!VLATy->getSizeExpr() || |
Richard Smith | aa9c350 | 2011-12-07 00:43:50 +0000 | [diff] [blame] | 4342 | !VLATy->getSizeExpr()->EvaluateAsInt(Res, Context)) |
Eli Friedman | 1ca4813 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 4343 | return QualType(); |
Eli Friedman | bc592e6 | 2009-02-26 03:58:54 +0000 | [diff] [blame] | 4344 | |
Douglas Gregor | 2767ce2 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 4345 | // Check whether the array size is negative. |
Douglas Gregor | 2767ce2 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 4346 | if (Res.isSigned() && Res.isNegative()) { |
4347 | SizeIsNegative = true; | ||||
4348 | return QualType(); | ||||
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 4349 | } |
Eli Friedman | 1ca4813 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 4350 | |
Douglas Gregor | 2767ce2 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 4351 | // Check whether the array is too large to be addressed. |
4352 | unsigned ActiveSizeBits | ||||
4353 | = ConstantArrayType::getNumAddressingBits(Context, VLATy->getElementType(), | ||||
4354 | Res); | ||||
4355 | if (ActiveSizeBits > ConstantArrayType::getMaxSizeBits(Context)) { | ||||
4356 | Oversized = Res; | ||||
4357 | return QualType(); | ||||
4358 | } | ||||
4359 | |||||
4360 | return Context.getConstantArrayType(VLATy->getElementType(), | ||||
4361 | Res, ArrayType::Normal, 0); | ||||
Eli Friedman | 1ca4813 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 4362 | } |
4363 | |||||
Abramo Bagnara | 4c5750e | 2012-11-08 14:44:42 +0000 | [diff] [blame] | 4364 | static void |
4365 | FixInvalidVariablyModifiedTypeLoc(TypeLoc SrcTL, TypeLoc DstTL) { | ||||
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 4366 | if (PointerTypeLoc SrcPTL = SrcTL.getAs<PointerTypeLoc>()) { |
4367 | PointerTypeLoc DstPTL = DstTL.castAs<PointerTypeLoc>(); | ||||
4368 | FixInvalidVariablyModifiedTypeLoc(SrcPTL.getPointeeLoc(), | ||||
4369 | DstPTL.getPointeeLoc()); | ||||
4370 | DstPTL.setStarLoc(SrcPTL.getStarLoc()); | ||||
Abramo Bagnara | 4c5750e | 2012-11-08 14:44:42 +0000 | [diff] [blame] | 4371 | return; |
4372 | } | ||||
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 4373 | if (ParenTypeLoc SrcPTL = SrcTL.getAs<ParenTypeLoc>()) { |
4374 | ParenTypeLoc DstPTL = DstTL.castAs<ParenTypeLoc>(); | ||||
4375 | FixInvalidVariablyModifiedTypeLoc(SrcPTL.getInnerLoc(), | ||||
4376 | DstPTL.getInnerLoc()); | ||||
4377 | DstPTL.setLParenLoc(SrcPTL.getLParenLoc()); | ||||
4378 | DstPTL.setRParenLoc(SrcPTL.getRParenLoc()); | ||||
Abramo Bagnara | 4c5750e | 2012-11-08 14:44:42 +0000 | [diff] [blame] | 4379 | return; |
4380 | } | ||||
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 4381 | ArrayTypeLoc SrcATL = SrcTL.castAs<ArrayTypeLoc>(); |
4382 | ArrayTypeLoc DstATL = DstTL.castAs<ArrayTypeLoc>(); | ||||
4383 | TypeLoc SrcElemTL = SrcATL.getElementLoc(); | ||||
4384 | TypeLoc DstElemTL = DstATL.getElementLoc(); | ||||
Abramo Bagnara | 4c5750e | 2012-11-08 14:44:42 +0000 | [diff] [blame] | 4385 | DstElemTL.initializeFullCopy(SrcElemTL); |
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 4386 | DstATL.setLBracketLoc(SrcATL.getLBracketLoc()); |
4387 | DstATL.setSizeExpr(SrcATL.getSizeExpr()); | ||||
4388 | DstATL.setRBracketLoc(SrcATL.getRBracketLoc()); | ||||
Abramo Bagnara | 4c5750e | 2012-11-08 14:44:42 +0000 | [diff] [blame] | 4389 | } |
4390 | |||||
Abramo Bagnara | 88adb98 | 2012-11-08 16:27:30 +0000 | [diff] [blame] | 4391 | /// Helper method to turn variable array types into constant array |
4392 | /// types in certain situations which would otherwise be errors (for | ||||
4393 | /// GCC compatibility). | ||||
Abramo Bagnara | 4c5750e | 2012-11-08 14:44:42 +0000 | [diff] [blame] | 4394 | static TypeSourceInfo* |
4395 | TryToFixInvalidVariablyModifiedTypeSourceInfo(TypeSourceInfo *TInfo, | ||||
4396 | ASTContext &Context, | ||||
4397 | bool &SizeIsNegative, | ||||
4398 | llvm::APSInt &Oversized) { | ||||
4399 | QualType FixedTy | ||||
4400 | = TryToFixInvalidVariablyModifiedType(TInfo->getType(), Context, | ||||
4401 | SizeIsNegative, Oversized); | ||||
4402 | if (FixedTy.isNull()) | ||||
4403 | return 0; | ||||
4404 | TypeSourceInfo *FixedTInfo = Context.getTrivialTypeSourceInfo(FixedTy); | ||||
4405 | FixInvalidVariablyModifiedTypeLoc(TInfo->getTypeLoc(), | ||||
4406 | FixedTInfo->getTypeLoc()); | ||||
4407 | return FixedTInfo; | ||||
4408 | } | ||||
4409 | |||||
Richard Smith | 5ea6ef4 | 2013-01-10 23:43:47 +0000 | [diff] [blame] | 4410 | /// \brief Register the given locally-scoped extern "C" declaration so |
Richard Smith | 662f41b | 2013-06-18 20:15:12 +0000 | [diff] [blame] | 4411 | /// that it can be found later for redeclarations. We include any extern "C" |
4412 | /// declaration that is not visible in the translation unit here, not just | ||||
4413 | /// function-scope declarations. | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4414 | void |
Richard Smith | 662f41b | 2013-06-18 20:15:12 +0000 | [diff] [blame] | 4415 | Sema::RegisterLocallyScopedExternCDecl(NamedDecl *ND, Scope *S) { |
Richard Smith | aa4bc18 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 4416 | if (!getLangOpts().CPlusPlus && |
4417 | ND->getLexicalDeclContext()->getRedeclContext()->isTranslationUnit()) | ||||
4418 | // Don't need to track declarations in the TU in C. | ||||
4419 | return; | ||||
4420 | |||||
Douglas Gregor | 6393519 | 2009-03-02 00:19:53 +0000 | [diff] [blame] | 4421 | // Note that we have a locally-scoped external with this name. |
Richard Smith | aa4bc18 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 4422 | // FIXME: There can be multiple such declarations if they are functions marked |
4423 | // __attribute__((overloadable)) declared in function scope in C. | ||||
Richard Smith | 5ea6ef4 | 2013-01-10 23:43:47 +0000 | [diff] [blame] | 4424 | LocallyScopedExternCDecls[ND->getDeclName()] = ND; |
Douglas Gregor | 6393519 | 2009-03-02 00:19:53 +0000 | [diff] [blame] | 4425 | } |
4426 | |||||
Richard Smith | 662f41b | 2013-06-18 20:15:12 +0000 | [diff] [blame] | 4427 | NamedDecl *Sema::findLocallyScopedExternCDecl(DeclarationName Name) { |
Douglas Gregor | ec12ce2 | 2011-07-28 14:20:37 +0000 | [diff] [blame] | 4428 | if (ExternalSource) { |
4429 | // Load locally-scoped external decls from the external source. | ||||
Richard Smith | 662f41b | 2013-06-18 20:15:12 +0000 | [diff] [blame] | 4430 | // FIXME: This is inefficient. Maybe add a DeclContext for extern "C" decls? |
Douglas Gregor | ec12ce2 | 2011-07-28 14:20:37 +0000 | [diff] [blame] | 4431 | SmallVector<NamedDecl *, 4> Decls; |
Richard Smith | 5ea6ef4 | 2013-01-10 23:43:47 +0000 | [diff] [blame] | 4432 | ExternalSource->ReadLocallyScopedExternCDecls(Decls); |
Douglas Gregor | ec12ce2 | 2011-07-28 14:20:37 +0000 | [diff] [blame] | 4433 | for (unsigned I = 0, N = Decls.size(); I != N; ++I) { |
4434 | llvm::DenseMap<DeclarationName, NamedDecl *>::iterator Pos | ||||
Richard Smith | 5ea6ef4 | 2013-01-10 23:43:47 +0000 | [diff] [blame] | 4435 | = LocallyScopedExternCDecls.find(Decls[I]->getDeclName()); |
4436 | if (Pos == LocallyScopedExternCDecls.end()) | ||||
4437 | LocallyScopedExternCDecls[Decls[I]->getDeclName()] = Decls[I]; | ||||
Douglas Gregor | ec12ce2 | 2011-07-28 14:20:37 +0000 | [diff] [blame] | 4438 | } |
4439 | } | ||||
Richard Smith | 662f41b | 2013-06-18 20:15:12 +0000 | [diff] [blame] | 4440 | |
4441 | NamedDecl *D = LocallyScopedExternCDecls.lookup(Name); | ||||
4442 | return D ? cast<NamedDecl>(D->getMostRecentDecl()) : 0; | ||||
Douglas Gregor | ec12ce2 | 2011-07-28 14:20:37 +0000 | [diff] [blame] | 4443 | } |
4444 | |||||
Eli Friedman | 85a5319 | 2009-04-07 19:37:57 +0000 | [diff] [blame] | 4445 | /// \brief Diagnose function specifiers on a declaration of an identifier that |
4446 | /// does not identify a function. | ||||
Richard Smith | c7f8116 | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 4447 | void Sema::DiagnoseFunctionSpecifiers(const DeclSpec &DS) { |
Eli Friedman | 85a5319 | 2009-04-07 19:37:57 +0000 | [diff] [blame] | 4448 | // FIXME: We should probably indicate the identifier in question to avoid |
4449 | // confusion for constructs like "inline int a(), b;" | ||||
Richard Smith | c7f8116 | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 4450 | if (DS.isInlineSpecified()) |
4451 | Diag(DS.getInlineSpecLoc(), | ||||
Eli Friedman | 85a5319 | 2009-04-07 19:37:57 +0000 | [diff] [blame] | 4452 | diag::err_inline_non_function); |
4453 | |||||
Richard Smith | c7f8116 | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 4454 | if (DS.isVirtualSpecified()) |
4455 | Diag(DS.getVirtualSpecLoc(), | ||||
Eli Friedman | 85a5319 | 2009-04-07 19:37:57 +0000 | [diff] [blame] | 4456 | diag::err_virtual_non_function); |
4457 | |||||
Richard Smith | c7f8116 | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 4458 | if (DS.isExplicitSpecified()) |
4459 | Diag(DS.getExplicitSpecLoc(), | ||||
Eli Friedman | 85a5319 | 2009-04-07 19:37:57 +0000 | [diff] [blame] | 4460 | diag::err_explicit_non_function); |
Richard Smith | de03c15 | 2013-01-17 22:16:11 +0000 | [diff] [blame] | 4461 | |
Richard Smith | c7f8116 | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 4462 | if (DS.isNoreturnSpecified()) |
4463 | Diag(DS.getNoreturnSpecLoc(), | ||||
Richard Smith | de03c15 | 2013-01-17 22:16:11 +0000 | [diff] [blame] | 4464 | diag::err_noreturn_non_function); |
Eli Friedman | 85a5319 | 2009-04-07 19:37:57 +0000 | [diff] [blame] | 4465 | } |
4466 | |||||
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 4467 | NamedDecl* |
Zhongxing Xu | d5ed8c3 | 2009-01-16 03:34:13 +0000 | [diff] [blame] | 4468 | Sema::ActOnTypedefDeclarator(Scope* S, Declarator& D, DeclContext* DC, |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 4469 | TypeSourceInfo *TInfo, LookupResult &Previous) { |
Zhongxing Xu | d5ed8c3 | 2009-01-16 03:34:13 +0000 | [diff] [blame] | 4470 | // Typedef declarators cannot be qualified (C++ [dcl.meaning]p1). |
4471 | if (D.getCXXScopeSpec().isSet()) { | ||||
4472 | Diag(D.getIdentifierLoc(), diag::err_qualified_typedef_declarator) | ||||
4473 | << D.getCXXScopeSpec().getRange(); | ||||
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 4474 | D.setInvalidType(); |
Zhongxing Xu | d5ed8c3 | 2009-01-16 03:34:13 +0000 | [diff] [blame] | 4475 | // Pretend we didn't see the scope specifier. |
Douglas Gregor | 9de672f | 2010-03-23 15:26:55 +0000 | [diff] [blame] | 4476 | DC = CurContext; |
4477 | Previous.clear(); | ||||
Zhongxing Xu | d5ed8c3 | 2009-01-16 03:34:13 +0000 | [diff] [blame] | 4478 | } |
4479 | |||||
Richard Smith | c7f8116 | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 4480 | DiagnoseFunctionSpecifiers(D.getDeclSpec()); |
Eli Friedman | 85a5319 | 2009-04-07 19:37:57 +0000 | [diff] [blame] | 4481 | |
Richard Smith | af1fc7a | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 4482 | if (D.getDeclSpec().isConstexprSpecified()) |
4483 | Diag(D.getDeclSpec().getConstexprSpecLoc(), diag::err_invalid_constexpr) | ||||
4484 | << 1; | ||||
Eli Friedman | 63054b3 | 2009-04-19 20:27:55 +0000 | [diff] [blame] | 4485 | |
Douglas Gregor | aef0199 | 2010-07-13 06:37:01 +0000 | [diff] [blame] | 4486 | if (D.getName().Kind != UnqualifiedId::IK_Identifier) { |
4487 | Diag(D.getName().StartLocation, diag::err_typedef_not_identifier) | ||||
4488 | << D.getName().getSourceRange(); | ||||
4489 | return 0; | ||||
4490 | } | ||||
4491 | |||||
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 4492 | TypedefDecl *NewTD = ParseTypedefDecl(S, D, TInfo->getType(), TInfo); |
Zhongxing Xu | d5ed8c3 | 2009-01-16 03:34:13 +0000 | [diff] [blame] | 4493 | if (!NewTD) return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4494 | |
Zhongxing Xu | d5ed8c3 | 2009-01-16 03:34:13 +0000 | [diff] [blame] | 4495 | // Handle attributes prior to checking for duplicates in MergeVarDecl |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 4496 | ProcessDeclAttributes(S, NewTD, D); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4497 | |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4498 | CheckTypedefForVariablyModifiedType(S, NewTD); |
4499 | |||||
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 4500 | bool Redeclaration = D.isRedeclaration(); |
4501 | NamedDecl *ND = ActOnTypedefNameDecl(S, DC, NewTD, Previous, Redeclaration); | ||||
4502 | D.setRedeclaration(Redeclaration); | ||||
4503 | return ND; | ||||
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4504 | } |
4505 | |||||
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4506 | void |
4507 | Sema::CheckTypedefForVariablyModifiedType(Scope *S, TypedefNameDecl *NewTD) { | ||||
Chris Lattner | 38c5ebd | 2009-04-19 05:21:20 +0000 | [diff] [blame] | 4508 | // C99 6.7.7p2: If a typedef name specifies a variably modified type |
4509 | // then it shall have block scope. | ||||
Eli Friedman | bf87f2c | 2010-08-10 03:13:15 +0000 | [diff] [blame] | 4510 | // Note that variably modified types must be fixed before merging the decl so |
4511 | // that redeclarations will match. | ||||
Abramo Bagnara | 4c5750e | 2012-11-08 14:44:42 +0000 | [diff] [blame] | 4512 | TypeSourceInfo *TInfo = NewTD->getTypeSourceInfo(); |
4513 | QualType T = TInfo->getType(); | ||||
Chris Lattner | 38c5ebd | 2009-04-19 05:21:20 +0000 | [diff] [blame] | 4514 | if (T->isVariablyModifiedType()) { |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 4515 | getCurFunction()->setHasBranchProtectedScope(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4516 | |
Chris Lattner | 38c5ebd | 2009-04-19 05:21:20 +0000 | [diff] [blame] | 4517 | if (S->getFnParent() == 0) { |
Eli Friedman | 1ca4813 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 4518 | bool SizeIsNegative; |
Douglas Gregor | 2767ce2 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 4519 | llvm::APSInt Oversized; |
Abramo Bagnara | 4c5750e | 2012-11-08 14:44:42 +0000 | [diff] [blame] | 4520 | TypeSourceInfo *FixedTInfo = |
4521 | TryToFixInvalidVariablyModifiedTypeSourceInfo(TInfo, Context, | ||||
4522 | SizeIsNegative, | ||||
4523 | Oversized); | ||||
4524 | if (FixedTInfo) { | ||||
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4525 | Diag(NewTD->getLocation(), diag::warn_illegal_constant_array_size); |
Abramo Bagnara | 4c5750e | 2012-11-08 14:44:42 +0000 | [diff] [blame] | 4526 | NewTD->setTypeSourceInfo(FixedTInfo); |
Eli Friedman | 1ca4813 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 4527 | } else { |
4528 | if (SizeIsNegative) | ||||
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4529 | Diag(NewTD->getLocation(), diag::err_typecheck_negative_array_size); |
Eli Friedman | 1ca4813 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 4530 | else if (T->isVariableArrayType()) |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4531 | Diag(NewTD->getLocation(), diag::err_vla_decl_in_file_scope); |
Douglas Gregor | 2767ce2 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 4532 | else if (Oversized.getBoolValue()) |
David Blaikie | d662a79 | 2011-10-19 22:56:21 +0000 | [diff] [blame] | 4533 | Diag(NewTD->getLocation(), diag::err_array_too_large) |
4534 | << Oversized.toString(10); | ||||
Eli Friedman | 1ca4813 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 4535 | else |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4536 | Diag(NewTD->getLocation(), diag::err_vm_decl_in_file_scope); |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 4537 | NewTD->setInvalidDecl(); |
Eli Friedman | 1ca4813 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 4538 | } |
Zhongxing Xu | d5ed8c3 | 2009-01-16 03:34:13 +0000 | [diff] [blame] | 4539 | } |
4540 | } | ||||
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4541 | } |
Douglas Gregor | c29f77b | 2009-07-07 16:35:42 +0000 | [diff] [blame] | 4542 | |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4543 | |
4544 | /// ActOnTypedefNameDecl - Perform semantic checking for a declaration which | ||||
4545 | /// declares a typedef-name, either using the 'typedef' type specifier or via | ||||
4546 | /// a C++0x [dcl.typedef]p2 alias-declaration: 'using T = A;'. | ||||
4547 | NamedDecl* | ||||
4548 | Sema::ActOnTypedefNameDecl(Scope *S, DeclContext *DC, TypedefNameDecl *NewTD, | ||||
4549 | LookupResult &Previous, bool &Redeclaration) { | ||||
Eli Friedman | bf87f2c | 2010-08-10 03:13:15 +0000 | [diff] [blame] | 4550 | // Merge the decl with the existing one if appropriate. If the decl is |
4551 | // in an outer scope, it isn't the same thing. | ||||
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4552 | FilterLookupForScope(Previous, DC, S, /*ConsiderLinkage*/ false, |
Douglas Gregor | cc20945 | 2011-03-07 16:54:27 +0000 | [diff] [blame] | 4553 | /*ExplicitInstantiationOrSpecialization=*/false); |
Douglas Gregor | 7dc80e1 | 2013-01-09 00:47:56 +0000 | [diff] [blame] | 4554 | filterNonConflictingPreviousDecls(Context, NewTD, Previous); |
Eli Friedman | bf87f2c | 2010-08-10 03:13:15 +0000 | [diff] [blame] | 4555 | if (!Previous.empty()) { |
4556 | Redeclaration = true; | ||||
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4557 | MergeTypedefNameDecl(NewTD, Previous); |
Eli Friedman | bf87f2c | 2010-08-10 03:13:15 +0000 | [diff] [blame] | 4558 | } |
4559 | |||||
Douglas Gregor | c29f77b | 2009-07-07 16:35:42 +0000 | [diff] [blame] | 4560 | // If this is the C FILE type, notify the AST context. |
4561 | if (IdentifierInfo *II = NewTD->getIdentifier()) | ||||
4562 | if (!NewTD->isInvalidDecl() && | ||||
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 4563 | NewTD->getDeclContext()->getRedeclContext()->isTranslationUnit()) { |
Mike Stump | 782fa30 | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 4564 | if (II->isStr("FILE")) |
4565 | Context.setFILEDecl(NewTD); | ||||
4566 | else if (II->isStr("jmp_buf")) | ||||
4567 | Context.setjmp_bufDecl(NewTD); | ||||
4568 | else if (II->isStr("sigjmp_buf")) | ||||
4569 | Context.setsigjmp_bufDecl(NewTD); | ||||
Rafael Espindola | e2d4f4e | 2011-11-13 21:51:09 +0000 | [diff] [blame] | 4570 | else if (II->isStr("ucontext_t")) |
4571 | Context.setucontext_tDecl(NewTD); | ||||
Mike Stump | 782fa30 | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 4572 | } |
4573 | |||||
Zhongxing Xu | d5ed8c3 | 2009-01-16 03:34:13 +0000 | [diff] [blame] | 4574 | return NewTD; |
4575 | } | ||||
4576 | |||||
Douglas Gregor | 8f30105 | 2009-02-24 19:23:27 +0000 | [diff] [blame] | 4577 | /// \brief Determines whether the given declaration is an out-of-scope |
4578 | /// previous declaration. | ||||
4579 | /// | ||||
4580 | /// This routine should be invoked when name lookup has found a | ||||
4581 | /// previous declaration (PrevDecl) that is not in the scope where a | ||||
4582 | /// new declaration by the same name is being introduced. If the new | ||||
4583 | /// declaration occurs in a local scope, previous declarations with | ||||
4584 | /// linkage may still be considered previous declarations (C99 | ||||
4585 | /// 6.2.2p4-5, C++ [basic.link]p6). | ||||
4586 | /// | ||||
4587 | /// \param PrevDecl the previous declaration found by name | ||||
4588 | /// lookup | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4589 | /// |
Douglas Gregor | 8f30105 | 2009-02-24 19:23:27 +0000 | [diff] [blame] | 4590 | /// \param DC the context in which the new declaration is being |
4591 | /// declared. | ||||
4592 | /// | ||||
4593 | /// \returns true if PrevDecl is an out-of-scope previous declaration | ||||
4594 | /// for a new delcaration with the same name. | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4595 | static bool |
Douglas Gregor | 8f30105 | 2009-02-24 19:23:27 +0000 | [diff] [blame] | 4596 | isOutOfScopePreviousDeclaration(NamedDecl *PrevDecl, DeclContext *DC, |
4597 | ASTContext &Context) { | ||||
4598 | if (!PrevDecl) | ||||
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 4599 | return false; |
Douglas Gregor | 8f30105 | 2009-02-24 19:23:27 +0000 | [diff] [blame] | 4600 | |
Douglas Gregor | d6f7e9d | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 4601 | if (!PrevDecl->hasLinkage()) |
4602 | return false; | ||||
Douglas Gregor | 8f30105 | 2009-02-24 19:23:27 +0000 | [diff] [blame] | 4603 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4604 | if (Context.getLangOpts().CPlusPlus) { |
Douglas Gregor | 8f30105 | 2009-02-24 19:23:27 +0000 | [diff] [blame] | 4605 | // C++ [basic.link]p6: |
4606 | // If there is a visible declaration of an entity with linkage | ||||
4607 | // having the same name and type, ignoring entities declared | ||||
4608 | // outside the innermost enclosing namespace scope, the block | ||||
4609 | // scope declaration declares that same entity and receives the | ||||
4610 | // linkage of the previous declaration. | ||||
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 4611 | DeclContext *OuterContext = DC->getRedeclContext(); |
Douglas Gregor | 8f30105 | 2009-02-24 19:23:27 +0000 | [diff] [blame] | 4612 | if (!OuterContext->isFunctionOrMethod()) |
4613 | // This rule only applies to block-scope declarations. | ||||
4614 | return false; | ||||
Douglas Gregor | 757c600 | 2010-08-27 22:55:10 +0000 | [diff] [blame] | 4615 | |
4616 | DeclContext *PrevOuterContext = PrevDecl->getDeclContext(); | ||||
4617 | if (PrevOuterContext->isRecord()) | ||||
4618 | // We found a member function: ignore it. | ||||
4619 | return false; | ||||
4620 | |||||
4621 | // Find the innermost enclosing namespace for the new and | ||||
4622 | // previous declarations. | ||||
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 4623 | OuterContext = OuterContext->getEnclosingNamespaceContext(); |
4624 | PrevOuterContext = PrevOuterContext->getEnclosingNamespaceContext(); | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4625 | |
Douglas Gregor | 757c600 | 2010-08-27 22:55:10 +0000 | [diff] [blame] | 4626 | // The previous declaration is in a different namespace, so it |
4627 | // isn't the same function. | ||||
4628 | if (!OuterContext->Equals(PrevOuterContext)) | ||||
4629 | return false; | ||||
Douglas Gregor | 8f30105 | 2009-02-24 19:23:27 +0000 | [diff] [blame] | 4630 | } |
4631 | |||||
Douglas Gregor | 8f30105 | 2009-02-24 19:23:27 +0000 | [diff] [blame] | 4632 | return true; |
4633 | } | ||||
4634 | |||||
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4635 | static void SetNestedNameSpecifier(DeclaratorDecl *DD, Declarator &D) { |
4636 | CXXScopeSpec &SS = D.getCXXScopeSpec(); | ||||
4637 | if (!SS.isSet()) return; | ||||
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 4638 | DD->setQualifierInfo(SS.getWithLocInContext(DD->getASTContext())); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4639 | } |
4640 | |||||
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4641 | bool Sema::inferObjCARCLifetime(ValueDecl *decl) { |
4642 | QualType type = decl->getType(); | ||||
4643 | Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime(); | ||||
4644 | if (lifetime == Qualifiers::OCL_Autoreleasing) { | ||||
4645 | // Various kinds of declaration aren't allowed to be __autoreleasing. | ||||
4646 | unsigned kind = -1U; | ||||
4647 | if (VarDecl *var = dyn_cast<VarDecl>(decl)) { | ||||
4648 | if (var->hasAttr<BlocksAttr>()) | ||||
4649 | kind = 0; // __block | ||||
4650 | else if (!var->hasLocalStorage()) | ||||
4651 | kind = 1; // global | ||||
4652 | } else if (isa<ObjCIvarDecl>(decl)) { | ||||
4653 | kind = 3; // ivar | ||||
4654 | } else if (isa<FieldDecl>(decl)) { | ||||
4655 | kind = 2; // field | ||||
4656 | } | ||||
4657 | |||||
4658 | if (kind != -1U) { | ||||
4659 | Diag(decl->getLocation(), diag::err_arc_autoreleasing_var) | ||||
4660 | << kind; | ||||
4661 | } | ||||
4662 | } else if (lifetime == Qualifiers::OCL_None) { | ||||
4663 | // Try to infer lifetime. | ||||
4664 | if (!type->isObjCLifetimeType()) | ||||
4665 | return false; | ||||
4666 | |||||
4667 | lifetime = type->getObjCARCImplicitLifetime(); | ||||
4668 | type = Context.getLifetimeQualifiedType(type, lifetime); | ||||
4669 | decl->setType(type); | ||||
4670 | } | ||||
4671 | |||||
4672 | if (VarDecl *var = dyn_cast<VarDecl>(decl)) { | ||||
4673 | // Thread-local variables cannot have lifetime. | ||||
4674 | if (lifetime && lifetime != Qualifiers::OCL_ExplicitNone && | ||||
Richard Smith | 38afbc7 | 2013-04-13 02:43:54 +0000 | [diff] [blame] | 4675 | var->getTLSKind()) { |
Argyrios Kyrtzidis | b8b0313 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 4676 | Diag(var->getLocation(), diag::err_arc_thread_ownership) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4677 | << var->getType(); |
4678 | return true; | ||||
4679 | } | ||||
4680 | } | ||||
4681 | |||||
4682 | return false; | ||||
4683 | } | ||||
4684 | |||||
Rafael Espindola | 2a5bb50 | 2013-01-16 23:11:15 +0000 | [diff] [blame] | 4685 | static void checkAttributesAfterMerging(Sema &S, NamedDecl &ND) { |
4686 | // 'weak' only applies to declarations with external linkage. | ||||
Rafael Espindola | 4d8a33b | 2013-01-16 23:49:06 +0000 | [diff] [blame] | 4687 | if (WeakAttr *Attr = ND.getAttr<WeakAttr>()) { |
Rafael Espindola | 181e3ec | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 4688 | if (!ND.isExternallyVisible()) { |
Rafael Espindola | 4d8a33b | 2013-01-16 23:49:06 +0000 | [diff] [blame] | 4689 | S.Diag(Attr->getLocation(), diag::err_attribute_weak_static); |
4690 | ND.dropAttr<WeakAttr>(); | ||||
4691 | } | ||||
4692 | } | ||||
4693 | if (WeakRefAttr *Attr = ND.getAttr<WeakRefAttr>()) { | ||||
Rafael Espindola | 181e3ec | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 4694 | if (ND.isExternallyVisible()) { |
Rafael Espindola | 4d8a33b | 2013-01-16 23:49:06 +0000 | [diff] [blame] | 4695 | S.Diag(Attr->getLocation(), diag::err_attribute_weakref_not_static); |
4696 | ND.dropAttr<WeakRefAttr>(); | ||||
4697 | } | ||||
Rafael Espindola | 2a5bb50 | 2013-01-16 23:11:15 +0000 | [diff] [blame] | 4698 | } |
Reid Kleckner | a722534 | 2013-05-20 14:02:37 +0000 | [diff] [blame] | 4699 | |
4700 | // 'selectany' only applies to externally visible varable declarations. | ||||
4701 | // It does not apply to functions. | ||||
4702 | if (SelectAnyAttr *Attr = ND.getAttr<SelectAnyAttr>()) { | ||||
4703 | if (isa<FunctionDecl>(ND) || !ND.isExternallyVisible()) { | ||||
4704 | S.Diag(Attr->getLocation(), diag::err_attribute_selectany_non_extern_data); | ||||
4705 | ND.dropAttr<SelectAnyAttr>(); | ||||
4706 | } | ||||
4707 | } | ||||
Rafael Espindola | 2a5bb50 | 2013-01-16 23:11:15 +0000 | [diff] [blame] | 4708 | } |
4709 | |||||
John McCall | b421d92 | 2013-04-02 02:48:58 +0000 | [diff] [blame] | 4710 | /// Given that we are within the definition of the given function, |
4711 | /// will that definition behave like C99's 'inline', where the | ||||
4712 | /// definition is discarded except for optimization purposes? | ||||
4713 | static bool isFunctionDefinitionDiscarded(Sema &S, FunctionDecl *FD) { | ||||
4714 | // Try to avoid calling GetGVALinkageForFunction. | ||||
4715 | |||||
4716 | // All cases of this require the 'inline' keyword. | ||||
4717 | if (!FD->isInlined()) return false; | ||||
4718 | |||||
4719 | // This is only possible in C++ with the gnu_inline attribute. | ||||
4720 | if (S.getLangOpts().CPlusPlus && !FD->hasAttr<GNUInlineAttr>()) | ||||
4721 | return false; | ||||
4722 | |||||
4723 | // Okay, go ahead and call the relatively-more-expensive function. | ||||
4724 | |||||
4725 | #ifndef NDEBUG | ||||
4726 | // AST quite reasonably asserts that it's working on a function | ||||
4727 | // definition. We don't really have a way to tell it that we're | ||||
4728 | // currently defining the function, so just lie to it in +Asserts | ||||
4729 | // builds. This is an awful hack. | ||||
4730 | FD->setLazyBody(1); | ||||
4731 | #endif | ||||
4732 | |||||
4733 | bool isC99Inline = (S.Context.GetGVALinkageForFunction(FD) == GVA_C99Inline); | ||||
4734 | |||||
4735 | #ifndef NDEBUG | ||||
4736 | FD->setLazyBody(0); | ||||
4737 | #endif | ||||
4738 | |||||
4739 | return isC99Inline; | ||||
4740 | } | ||||
4741 | |||||
Richard Smith | aa4bc18 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 4742 | /// Determine whether a variable is extern "C" prior to attaching |
4743 | /// an initializer. We can't just call isExternC() here, because that | ||||
4744 | /// will also compute and cache whether the declaration is externally | ||||
4745 | /// visible, which might change when we attach the initializer. | ||||
4746 | /// | ||||
4747 | /// This can only be used if the declaration is known to not be a | ||||
4748 | /// redeclaration of an internal linkage declaration. | ||||
4749 | /// | ||||
4750 | /// For instance: | ||||
4751 | /// | ||||
4752 | /// auto x = []{}; | ||||
4753 | /// | ||||
4754 | /// Attaching the initializer here makes this declaration not externally | ||||
4755 | /// visible, because its type has internal linkage. | ||||
4756 | /// | ||||
4757 | /// FIXME: This is a hack. | ||||
4758 | template<typename T> | ||||
4759 | static bool isIncompleteDeclExternC(Sema &S, const T *D) { | ||||
4760 | if (S.getLangOpts().CPlusPlus) { | ||||
4761 | // In C++, the overloadable attribute negates the effects of extern "C". | ||||
4762 | if (!D->isInExternCContext() || D->template hasAttr<OverloadableAttr>()) | ||||
4763 | return false; | ||||
4764 | } | ||||
4765 | return D->isExternC(); | ||||
4766 | } | ||||
4767 | |||||
Rafael Espindola | 2d1b096 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 4768 | static bool shouldConsiderLinkage(const VarDecl *VD) { |
4769 | const DeclContext *DC = VD->getDeclContext()->getRedeclContext(); | ||||
4770 | if (DC->isFunctionOrMethod()) | ||||
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 4771 | return VD->hasExternalStorage(); |
Rafael Espindola | 2d1b096 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 4772 | if (DC->isFileContext()) |
4773 | return true; | ||||
4774 | if (DC->isRecord()) | ||||
4775 | return false; | ||||
4776 | llvm_unreachable("Unexpected context"); | ||||
4777 | } | ||||
4778 | |||||
4779 | static bool shouldConsiderLinkage(const FunctionDecl *FD) { | ||||
4780 | const DeclContext *DC = FD->getDeclContext()->getRedeclContext(); | ||||
4781 | if (DC->isFileContext() || DC->isFunctionOrMethod()) | ||||
4782 | return true; | ||||
4783 | if (DC->isRecord()) | ||||
4784 | return false; | ||||
4785 | llvm_unreachable("Unexpected context"); | ||||
4786 | } | ||||
4787 | |||||
Larisse Voufo | 06935f3 | 2013-08-06 03:43:07 +0000 | [diff] [blame] | 4788 | bool Sema::HandleVariableRedeclaration(Decl *D, CXXScopeSpec &SS) { |
4789 | // If this is a redeclaration of a variable template or a forward | ||||
4790 | // declaration of a variable template partial specialization | ||||
4791 | // with nested name specifier, complain. | ||||
4792 | |||||
4793 | if (D && SS.isNotEmpty() && | ||||
4794 | (isa<VarTemplateDecl>(D) || | ||||
4795 | isa<VarTemplatePartialSpecializationDecl>(D))) { | ||||
4796 | Diag(SS.getBeginLoc(), diag::err_forward_var_nested_name_specifier) | ||||
4797 | << isa<VarTemplatePartialSpecializationDecl>(D) << SS.getRange(); | ||||
4798 | return true; | ||||
4799 | } | ||||
4800 | return false; | ||||
4801 | } | ||||
4802 | |||||
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4803 | NamedDecl * |
Chris Lattner | 16c5dea | 2010-10-10 18:16:20 +0000 | [diff] [blame] | 4804 | Sema::ActOnVariableDeclarator(Scope *S, Declarator &D, DeclContext *DC, |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 4805 | TypeSourceInfo *TInfo, LookupResult &Previous, |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4806 | MultiTemplateParamsArg TemplateParamLists, |
4807 | bool &AddToScope) { | ||||
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 4808 | QualType R = TInfo->getType(); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4809 | DeclarationName Name = GetNameForDeclarator(D).getName(); |
Zhongxing Xu | cb8f4f1 | 2009-01-16 02:36:34 +0000 | [diff] [blame] | 4810 | |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 4811 | DeclSpec::SCS SCSpec = D.getDeclSpec().getStorageClassSpec(); |
Rafael Espindola | 65dfa2b | 2013-04-25 12:11:36 +0000 | [diff] [blame] | 4812 | VarDecl::StorageClass SC = |
4813 | StorageClassSpecToVarDeclStorageClass(D.getDeclSpec()); | ||||
Joey Gouly | 19dbb20 | 2013-01-23 11:56:20 +0000 | [diff] [blame] | 4814 | |
Richard Smith | df4cc0a | 2013-04-15 08:33:22 +0000 | [diff] [blame] | 4815 | if (getLangOpts().OpenCL && !getOpenCLOptions().cl_khr_fp16) { |
Joey Gouly | 19dbb20 | 2013-01-23 11:56:20 +0000 | [diff] [blame] | 4816 | // OpenCL v1.2 s6.1.1.1: reject declaring variables of the half and |
4817 | // half array type (unless the cl_khr_fp16 extension is enabled). | ||||
4818 | if (Context.getBaseElementType(R)->isHalfType()) { | ||||
4819 | Diag(D.getIdentifierLoc(), diag::err_opencl_half_declaration) << R; | ||||
4820 | D.setInvalidType(); | ||||
4821 | } | ||||
4822 | } | ||||
4823 | |||||
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 4824 | if (SCSpec == DeclSpec::SCS_mutable) { |
Zhongxing Xu | cb8f4f1 | 2009-01-16 02:36:34 +0000 | [diff] [blame] | 4825 | // mutable can only appear on non-static class members, so it's always |
4826 | // an error here | ||||
4827 | Diag(D.getIdentifierLoc(), diag::err_mutable_nonmember); | ||||
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 4828 | D.setInvalidType(); |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 4829 | SC = SC_None; |
Zhongxing Xu | cb8f4f1 | 2009-01-16 02:36:34 +0000 | [diff] [blame] | 4830 | } |
John McCall | b421d92 | 2013-04-02 02:48:58 +0000 | [diff] [blame] | 4831 | |
Richard Smith | 9109bf1 | 2013-06-17 01:34:01 +0000 | [diff] [blame] | 4832 | if (getLangOpts().CPlusPlus11 && SCSpec == DeclSpec::SCS_register && |
4833 | !D.getAsmLabel() && !getSourceManager().isInSystemMacro( | ||||
4834 | D.getDeclSpec().getStorageClassSpecLoc())) { | ||||
4835 | // In C++11, the 'register' storage class specifier is deprecated. | ||||
4836 | // Suppress the warning in system macros, it's used in macros in some | ||||
4837 | // popular C system headers, such as in glibc's htonl() macro. | ||||
4838 | Diag(D.getDeclSpec().getStorageClassSpecLoc(), | ||||
4839 | diag::warn_deprecated_register) | ||||
4840 | << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc()); | ||||
4841 | } | ||||
4842 | |||||
Zhongxing Xu | cb8f4f1 | 2009-01-16 02:36:34 +0000 | [diff] [blame] | 4843 | IdentifierInfo *II = Name.getAsIdentifierInfo(); |
4844 | if (!II) { | ||||
4845 | Diag(D.getIdentifierLoc(), diag::err_bad_variable_name) | ||||
Douglas Gregor | b5a0187 | 2011-10-09 18:55:59 +0000 | [diff] [blame] | 4846 | << Name; |
Zhongxing Xu | cb8f4f1 | 2009-01-16 02:36:34 +0000 | [diff] [blame] | 4847 | return 0; |
4848 | } | ||||
4849 | |||||
Richard Smith | c7f8116 | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 4850 | DiagnoseFunctionSpecifiers(D.getDeclSpec()); |
Douglas Gregor | 021c3b3 | 2009-03-11 23:00:04 +0000 | [diff] [blame] | 4851 | |
Douglas Gregor | 2d2e9cf | 2009-03-11 20:22:50 +0000 | [diff] [blame] | 4852 | if (!DC->isRecord() && S->getFnParent() == 0) { |
4853 | // C99 6.9p2: The storage-class specifiers auto and register shall not | ||||
4854 | // appear in the declaration specifiers in an external declaration. | ||||
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 4855 | if (SC == SC_Auto || SC == SC_Register) { |
Chris Lattner | d4b19d5 | 2009-05-12 21:44:00 +0000 | [diff] [blame] | 4856 | // If this is a register variable with an asm label specified, then this |
4857 | // is a GNU extension. | ||||
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 4858 | if (SC == SC_Register && D.getAsmLabel()) |
Chris Lattner | d4b19d5 | 2009-05-12 21:44:00 +0000 | [diff] [blame] | 4859 | Diag(D.getIdentifierLoc(), diag::err_unsupported_global_register); |
4860 | else | ||||
4861 | Diag(D.getIdentifierLoc(), diag::err_typecheck_sclass_fscope); | ||||
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 4862 | D.setInvalidType(); |
Zhongxing Xu | cb8f4f1 | 2009-01-16 02:36:34 +0000 | [diff] [blame] | 4863 | } |
Zhongxing Xu | cb8f4f1 | 2009-01-16 02:36:34 +0000 | [diff] [blame] | 4864 | } |
Richard Smith | 9109bf1 | 2013-06-17 01:34:01 +0000 | [diff] [blame] | 4865 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4866 | if (getLangOpts().OpenCL) { |
Peter Collingbourne | 8c25fc5 | 2011-09-19 21:14:35 +0000 | [diff] [blame] | 4867 | // Set up the special work-group-local storage class for variables in the |
4868 | // OpenCL __local address space. | ||||
Rafael Espindola | 0db661e | 2012-12-21 01:21:33 +0000 | [diff] [blame] | 4869 | if (R.getAddressSpace() == LangAS::opencl_local) { |
Peter Collingbourne | 8c25fc5 | 2011-09-19 21:14:35 +0000 | [diff] [blame] | 4870 | SC = SC_OpenCLWorkGroupLocal; |
Rafael Espindola | 0db661e | 2012-12-21 01:21:33 +0000 | [diff] [blame] | 4871 | } |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 4872 | |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 4873 | // OpenCL v1.2 s6.9.b p4: |
4874 | // The sampler type cannot be used with the __local and __global address | ||||
4875 | // space qualifiers. | ||||
4876 | if (R->isSamplerT() && (R.getAddressSpace() == LangAS::opencl_local || | ||||
4877 | R.getAddressSpace() == LangAS::opencl_global)) { | ||||
4878 | Diag(D.getIdentifierLoc(), diag::err_wrong_sampler_addressspace); | ||||
4879 | } | ||||
4880 | |||||
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 4881 | // OpenCL 1.2 spec, p6.9 r: |
4882 | // The event type cannot be used to declare a program scope variable. | ||||
4883 | // The event type cannot be used with the __local, __constant and __global | ||||
4884 | // address space qualifiers. | ||||
4885 | if (R->isEventT()) { | ||||
4886 | if (S->getParent() == 0) { | ||||
4887 | Diag(D.getLocStart(), diag::err_event_t_global_var); | ||||
4888 | D.setInvalidType(); | ||||
4889 | } | ||||
4890 | |||||
4891 | if (R.getAddressSpace()) { | ||||
4892 | Diag(D.getLocStart(), diag::err_event_t_addr_space_qual); | ||||
4893 | D.setInvalidType(); | ||||
4894 | } | ||||
4895 | } | ||||
Peter Collingbourne | 8c25fc5 | 2011-09-19 21:14:35 +0000 | [diff] [blame] | 4896 | } |
4897 | |||||
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4898 | bool IsExplicitSpecialization = false; |
4899 | bool IsVariableTemplateSpecialization = false; | ||||
4900 | bool IsPartialSpecialization = false; | ||||
Larisse Voufo | 4a91989 | 2013-08-14 03:09:19 +0000 | [diff] [blame] | 4901 | bool IsVariableTemplate = false; |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4902 | VarTemplateDecl *PrevVarTemplate = 0; |
Larisse Voufo | 567f917 | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 4903 | VarDecl *NewVD = 0; |
4904 | VarTemplateDecl *NewTemplate = 0; | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4905 | if (!getLangOpts().CPlusPlus) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4906 | NewVD = VarDecl::Create(Context, DC, D.getLocStart(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4907 | D.getIdentifierLoc(), II, |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 4908 | R, TInfo, SC); |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 4909 | |
4910 | if (D.isInvalidType()) | ||||
4911 | NewVD->setInvalidDecl(); | ||||
4912 | } else { | ||||
Larisse Voufo | 567f917 | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 4913 | bool Invalid = false; |
4914 | |||||
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 4915 | if (DC->isRecord() && !CurContext->isRecord()) { |
4916 | // This is an out-of-line definition of a static data member. | ||||
Rafael Espindola | 3882aed | 2013-06-19 13:41:54 +0000 | [diff] [blame] | 4917 | switch (SC) { |
4918 | case SC_None: | ||||
4919 | break; | ||||
4920 | case SC_Static: | ||||
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 4921 | Diag(D.getDeclSpec().getStorageClassSpecLoc(), |
4922 | diag::err_static_out_of_line) | ||||
4923 | << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc()); | ||||
Rafael Espindola | 3882aed | 2013-06-19 13:41:54 +0000 | [diff] [blame] | 4924 | break; |
4925 | case SC_Auto: | ||||
4926 | case SC_Register: | ||||
4927 | case SC_Extern: | ||||
4928 | // [dcl.stc] p2: The auto or register specifiers shall be applied only | ||||
4929 | // to names of variables declared in a block or to function parameters. | ||||
4930 | // [dcl.stc] p6: The extern specifier cannot be used in the declaration | ||||
4931 | // of class members | ||||
4932 | |||||
4933 | Diag(D.getDeclSpec().getStorageClassSpecLoc(), | ||||
4934 | diag::err_storage_class_for_static_member) | ||||
4935 | << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc()); | ||||
4936 | break; | ||||
4937 | case SC_PrivateExtern: | ||||
4938 | llvm_unreachable("C storage class in c++!"); | ||||
4939 | case SC_OpenCLWorkGroupLocal: | ||||
4940 | llvm_unreachable("OpenCL storage class in c++!"); | ||||
Rafael Espindola | ea4b111 | 2013-04-04 21:21:25 +0000 | [diff] [blame] | 4941 | } |
Larisse Voufo | 06935f3 | 2013-08-06 03:43:07 +0000 | [diff] [blame] | 4942 | } |
4943 | |||||
Richard Smith | b9c64d8 | 2012-02-16 20:41:22 +0000 | [diff] [blame] | 4944 | if (SC == SC_Static && CurContext->isRecord()) { |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 4945 | if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(DC)) { |
4946 | if (RD->isLocalClass()) | ||||
4947 | Diag(D.getIdentifierLoc(), | ||||
4948 | diag::err_static_data_member_not_allowed_in_local_class) | ||||
4949 | << Name << RD->getDeclName(); | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4950 | |
Richard Smith | b9c64d8 | 2012-02-16 20:41:22 +0000 | [diff] [blame] | 4951 | // C++98 [class.union]p1: If a union contains a static data member, |
4952 | // the program is ill-formed. C++11 drops this restriction. | ||||
4953 | if (RD->isUnion()) | ||||
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 4954 | Diag(D.getIdentifierLoc(), |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4955 | getLangOpts().CPlusPlus11 |
Richard Smith | b9c64d8 | 2012-02-16 20:41:22 +0000 | [diff] [blame] | 4956 | ? diag::warn_cxx98_compat_static_data_member_in_union |
4957 | : diag::ext_static_data_member_in_union) << Name; | ||||
4958 | // We conservatively disallow static data members in anonymous structs. | ||||
4959 | else if (!RD->getDeclName()) | ||||
4960 | Diag(D.getIdentifierLoc(), | ||||
4961 | diag::err_static_data_member_not_allowed_in_anon_struct) | ||||
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 4962 | << Name << RD->isUnion(); |
4963 | } | ||||
4964 | } | ||||
4965 | |||||
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4966 | NamedDecl *PrevDecl = 0; |
4967 | if (Previous.begin() != Previous.end()) | ||||
4968 | PrevDecl = (*Previous.begin())->getUnderlyingDecl(); | ||||
4969 | PrevVarTemplate = dyn_cast_or_null<VarTemplateDecl>(PrevDecl); | ||||
4970 | |||||
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 4971 | // Match up the template parameter lists with the scope specifier, then |
4972 | // determine whether we have a template or a template specialization. | ||||
Larisse Voufo | 567f917 | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 4973 | TemplateParameterList *TemplateParams = |
4974 | MatchTemplateParametersToScopeSpecifier( | ||||
4975 | D.getDeclSpec().getLocStart(), D.getIdentifierLoc(), | ||||
4976 | D.getCXXScopeSpec(), TemplateParamLists, | ||||
4977 | /*never a friend*/ false, IsExplicitSpecialization, Invalid); | ||||
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4978 | if (TemplateParams) { |
4979 | if (!TemplateParams->size() && | ||||
4980 | D.getName().getKind() != UnqualifiedId::IK_TemplateId) { | ||||
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 4981 | // There is an extraneous 'template<>' for this variable. Complain |
4982 | // about it, but allow the declaration of the variable. | ||||
4983 | Diag(TemplateParams->getTemplateLoc(), | ||||
4984 | diag::err_template_variable_noparams) | ||||
4985 | << II | ||||
4986 | << SourceRange(TemplateParams->getTemplateLoc(), | ||||
4987 | TemplateParams->getRAngleLoc()); | ||||
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4988 | } else { |
4989 | // Only C++1y supports variable templates (N3651). | ||||
4990 | Diag(D.getIdentifierLoc(), | ||||
4991 | getLangOpts().CPlusPlus1y | ||||
4992 | ? diag::warn_cxx11_compat_variable_template | ||||
4993 | : diag::ext_variable_template); | ||||
4994 | |||||
4995 | if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) { | ||||
4996 | // This is an explicit specialization or a partial specialization. | ||||
4997 | // Check that we can declare a specialization here | ||||
4998 | |||||
4999 | IsVariableTemplateSpecialization = true; | ||||
5000 | IsPartialSpecialization = TemplateParams->size() > 0; | ||||
5001 | |||||
5002 | } else { // if (TemplateParams->size() > 0) | ||||
Larisse Voufo | 06935f3 | 2013-08-06 03:43:07 +0000 | [diff] [blame] | 5003 | // This is a template declaration. |
Larisse Voufo | 4a91989 | 2013-08-14 03:09:19 +0000 | [diff] [blame] | 5004 | IsVariableTemplate = true; |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5005 | |
5006 | // Check that we can declare a template here. | ||||
5007 | if (CheckTemplateDeclScope(S, TemplateParams)) | ||||
5008 | return 0; | ||||
5009 | |||||
5010 | // If there is a previous declaration with the same name, check | ||||
5011 | // whether this is a valid redeclaration. | ||||
5012 | if (PrevDecl && !isDeclInScope(PrevDecl, DC, S)) | ||||
5013 | PrevDecl = PrevVarTemplate = 0; | ||||
5014 | |||||
5015 | if (PrevVarTemplate) { | ||||
5016 | // Ensure that the template parameter lists are compatible. | ||||
5017 | if (!TemplateParameterListsAreEqual( | ||||
5018 | TemplateParams, PrevVarTemplate->getTemplateParameters(), | ||||
5019 | /*Complain=*/true, TPL_TemplateMatch)) | ||||
5020 | return 0; | ||||
5021 | } else if (PrevDecl && PrevDecl->isTemplateParameter()) { | ||||
5022 | // Maybe we will complain about the shadowed template parameter. | ||||
5023 | DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), PrevDecl); | ||||
5024 | |||||
5025 | // Just pretend that we didn't see the previous declaration. | ||||
5026 | PrevDecl = 0; | ||||
5027 | } else if (PrevDecl) { | ||||
5028 | // C++ [temp]p5: | ||||
5029 | // ... a template name declared in namespace scope or in class | ||||
5030 | // scope shall be unique in that scope. | ||||
5031 | Diag(D.getIdentifierLoc(), diag::err_redefinition_different_kind) | ||||
5032 | << Name; | ||||
5033 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); | ||||
5034 | return 0; | ||||
5035 | } | ||||
5036 | |||||
5037 | // Check the template parameter list of this declaration, possibly | ||||
5038 | // merging in the template parameter list from the previous variable | ||||
5039 | // template declaration. | ||||
5040 | if (CheckTemplateParameterList( | ||||
5041 | TemplateParams, | ||||
5042 | PrevVarTemplate ? PrevVarTemplate->getTemplateParameters() | ||||
5043 | : 0, | ||||
5044 | (D.getCXXScopeSpec().isSet() && DC && DC->isRecord() && | ||||
5045 | DC->isDependentContext()) | ||||
5046 | ? TPC_ClassTemplateMember | ||||
5047 | : TPC_VarTemplate)) | ||||
5048 | Invalid = true; | ||||
5049 | |||||
5050 | if (D.getCXXScopeSpec().isSet()) { | ||||
5051 | // If the name of the template was qualified, we must be defining | ||||
5052 | // the template out-of-line. | ||||
5053 | if (!D.getCXXScopeSpec().isInvalid() && !Invalid && | ||||
5054 | !PrevVarTemplate) { | ||||
Richard Smith | 4e9686b | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 5055 | Diag(D.getIdentifierLoc(), diag::err_member_decl_does_not_match) |
5056 | << Name << DC << /*IsDefinition*/true | ||||
5057 | << D.getCXXScopeSpec().getRange(); | ||||
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5058 | Invalid = true; |
5059 | } | ||||
5060 | } | ||||
5061 | } | ||||
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 5062 | } |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5063 | } else if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) { |
5064 | TemplateIdAnnotation *TemplateId = D.getName().TemplateId; | ||||
5065 | |||||
5066 | // We have encountered something that the user meant to be a | ||||
5067 | // specialization (because it has explicitly-specified template | ||||
5068 | // arguments) but that was not introduced with a "template<>" (or had | ||||
5069 | // too few of them). | ||||
5070 | // FIXME: Differentiate between attempts for explicit instantiations | ||||
5071 | // (starting with "template") and the rest. | ||||
5072 | Diag(D.getIdentifierLoc(), diag::err_template_spec_needs_header) | ||||
5073 | << SourceRange(TemplateId->LAngleLoc, TemplateId->RAngleLoc) | ||||
5074 | << FixItHint::CreateInsertion(D.getDeclSpec().getLocStart(), | ||||
5075 | "template<> "); | ||||
5076 | IsVariableTemplateSpecialization = true; | ||||
Douglas Gregor | dfe3f2d | 2009-07-22 17:18:37 +0000 | [diff] [blame] | 5077 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5078 | |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5079 | if (IsVariableTemplateSpecialization) { |
5080 | if (!PrevVarTemplate) { | ||||
5081 | Diag(D.getIdentifierLoc(), diag::err_var_spec_no_template) | ||||
5082 | << IsPartialSpecialization; | ||||
5083 | return 0; | ||||
5084 | } | ||||
5085 | |||||
5086 | SourceLocation TemplateKWLoc = | ||||
5087 | TemplateParamLists.size() > 0 | ||||
5088 | ? TemplateParamLists[0]->getTemplateLoc() | ||||
5089 | : SourceLocation(); | ||||
5090 | DeclResult Res = ActOnVarTemplateSpecialization( | ||||
5091 | S, PrevVarTemplate, D, TInfo, TemplateKWLoc, TemplateParams, SC, | ||||
5092 | IsPartialSpecialization); | ||||
5093 | if (Res.isInvalid()) | ||||
5094 | return 0; | ||||
5095 | NewVD = cast<VarDecl>(Res.get()); | ||||
5096 | AddToScope = false; | ||||
5097 | } else | ||||
5098 | NewVD = VarDecl::Create(Context, DC, D.getLocStart(), | ||||
5099 | D.getIdentifierLoc(), II, R, TInfo, SC); | ||||
Eli Friedman | 63054b3 | 2009-04-19 20:27:55 +0000 | [diff] [blame] | 5100 | |
Larisse Voufo | 567f917 | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 5101 | // If this is supposed to be a variable template, create it as such. |
5102 | if (IsVariableTemplate) { | ||||
5103 | NewTemplate = | ||||
5104 | VarTemplateDecl::Create(Context, DC, D.getIdentifierLoc(), Name, | ||||
5105 | TemplateParams, NewVD, PrevVarTemplate); | ||||
5106 | NewVD->setDescribedVarTemplate(NewTemplate); | ||||
5107 | } | ||||
5108 | |||||
Richard Smith | 483b9f3 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 5109 | // If this decl has an auto type in need of deduction, make a note of the |
5110 | // Decl so we can diagnose uses of it in its own initializer. | ||||
Richard Smith | a2c3646 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 5111 | if (D.getDeclSpec().containsPlaceholderType() && R->getContainedAutoType()) |
Richard Smith | 483b9f3 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 5112 | ParsingInitForAutoVars.insert(NewVD); |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 5113 | |
Larisse Voufo | 567f917 | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 5114 | if (D.isInvalidType() || Invalid) { |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 5115 | NewVD->setInvalidDecl(); |
Larisse Voufo | 567f917 | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 5116 | if (NewTemplate) |
5117 | NewTemplate->setInvalidDecl(); | ||||
5118 | } | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5119 | |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 5120 | SetNestedNameSpecifier(NewVD, D); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 5121 | |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5122 | // FIXME: Do we need D.getCXXScopeSpec().isSet()? |
5123 | if (TemplateParams && TemplateParamLists.size() > 1 && | ||||
5124 | (!IsVariableTemplateSpecialization || D.getCXXScopeSpec().isSet())) { | ||||
5125 | NewVD->setTemplateParameterListsInfo( | ||||
5126 | Context, TemplateParamLists.size() - 1, TemplateParamLists.data()); | ||||
5127 | } else if (IsVariableTemplateSpecialization || | ||||
5128 | (!TemplateParams && TemplateParamLists.size() > 0 && | ||||
5129 | (D.getCXXScopeSpec().isSet()))) { | ||||
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 5130 | NewVD->setTemplateParameterListsInfo(Context, |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5131 | TemplateParamLists.size(), |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 5132 | TemplateParamLists.data()); |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 5133 | } |
Richard Smith | af1fc7a | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 5134 | |
Richard Smith | 7ca4850 | 2012-02-13 22:16:19 +0000 | [diff] [blame] | 5135 | if (D.getDeclSpec().isConstexprSpecified()) |
Richard Smith | dd4b350 | 2011-12-25 21:17:58 +0000 | [diff] [blame] | 5136 | NewVD->setConstexpr(true); |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 5137 | } |
5138 | |||||
Douglas Gregor | e389585 | 2011-09-12 18:37:38 +0000 | [diff] [blame] | 5139 | // Set the lexical context. If the declarator has a C++ scope specifier, the |
5140 | // lexical context will be different from the semantic context. | ||||
5141 | NewVD->setLexicalDeclContext(CurContext); | ||||
Larisse Voufo | 567f917 | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 5142 | if (NewTemplate) |
5143 | NewTemplate->setLexicalDeclContext(CurContext); | ||||
Douglas Gregor | e389585 | 2011-09-12 18:37:38 +0000 | [diff] [blame] | 5144 | |
Richard Smith | ec64244 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 5145 | if (DeclSpec::TSCS TSCS = D.getDeclSpec().getThreadStorageClassSpec()) { |
Enea Zaffanella | 9cbcab8 | 2013-05-10 20:34:44 +0000 | [diff] [blame] | 5146 | if (NewVD->hasLocalStorage()) { |
5147 | // C++11 [dcl.stc]p4: | ||||
5148 | // When thread_local is applied to a variable of block scope the | ||||
5149 | // storage-class-specifier static is implied if it does not appear | ||||
5150 | // explicitly. | ||||
5151 | // Core issue: 'static' is not implied if the variable is declared | ||||
5152 | // 'extern'. | ||||
5153 | if (SCSpec == DeclSpec::SCS_unspecified && | ||||
5154 | TSCS == DeclSpec::TSCS_thread_local && | ||||
5155 | DC->isFunctionOrMethod()) | ||||
5156 | NewVD->setTSCSpec(TSCS); | ||||
5157 | else | ||||
5158 | Diag(D.getDeclSpec().getThreadStorageClassSpecLoc(), | ||||
5159 | diag::err_thread_non_global) | ||||
5160 | << DeclSpec::getSpecifierName(TSCS); | ||||
5161 | } else if (!Context.getTargetInfo().isTLSSupported()) | ||||
Richard Smith | ec64244 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 5162 | Diag(D.getDeclSpec().getThreadStorageClassSpecLoc(), |
5163 | diag::err_thread_unsupported); | ||||
Eli Friedman | 63054b3 | 2009-04-19 20:27:55 +0000 | [diff] [blame] | 5164 | else |
Enea Zaffanella | dc17384 | 2013-05-04 08:27:07 +0000 | [diff] [blame] | 5165 | NewVD->setTSCSpec(TSCS); |
Eli Friedman | 63054b3 | 2009-04-19 20:27:55 +0000 | [diff] [blame] | 5166 | } |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 5167 | |
John McCall | b421d92 | 2013-04-02 02:48:58 +0000 | [diff] [blame] | 5168 | // C99 6.7.4p3 |
5169 | // An inline definition of a function with external linkage shall | ||||
5170 | // not contain a definition of a modifiable object with static or | ||||
5171 | // thread storage duration... | ||||
5172 | // We only apply this when the function is required to be defined | ||||
5173 | // elsewhere, i.e. when the function is not 'extern inline'. Note | ||||
5174 | // that a local variable with thread storage duration still has to | ||||
5175 | // be marked 'static'. Also note that it's possible to get these | ||||
5176 | // semantics in C++ using __attribute__((gnu_inline)). | ||||
5177 | if (SC == SC_Static && S->getFnParent() != 0 && | ||||
5178 | !NewVD->getType().isConstQualified()) { | ||||
5179 | FunctionDecl *CurFD = getCurFunctionDecl(); | ||||
5180 | if (CurFD && isFunctionDefinitionDiscarded(*this, CurFD)) { | ||||
5181 | Diag(D.getDeclSpec().getStorageClassSpecLoc(), | ||||
5182 | diag::warn_static_local_in_extern_inline); | ||||
5183 | MaybeSuggestAddingStaticToDecl(CurFD); | ||||
5184 | } | ||||
5185 | } | ||||
5186 | |||||
Douglas Gregor | d023aec | 2011-09-09 20:53:38 +0000 | [diff] [blame] | 5187 | if (D.getDeclSpec().isModulePrivateSpecified()) { |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5188 | if (IsVariableTemplateSpecialization) |
5189 | Diag(NewVD->getLocation(), diag::err_module_private_specialization) | ||||
5190 | << (IsPartialSpecialization ? 1 : 0) | ||||
5191 | << FixItHint::CreateRemoval( | ||||
5192 | D.getDeclSpec().getModulePrivateSpecLoc()); | ||||
5193 | else if (IsExplicitSpecialization) | ||||
Douglas Gregor | d023aec | 2011-09-09 20:53:38 +0000 | [diff] [blame] | 5194 | Diag(NewVD->getLocation(), diag::err_module_private_specialization) |
5195 | << 2 | ||||
5196 | << FixItHint::CreateRemoval(D.getDeclSpec().getModulePrivateSpecLoc()); | ||||
Douglas Gregor | e389585 | 2011-09-12 18:37:38 +0000 | [diff] [blame] | 5197 | else if (NewVD->hasLocalStorage()) |
5198 | Diag(NewVD->getLocation(), diag::err_module_private_local) | ||||
5199 | << 0 << NewVD->getDeclName() | ||||
5200 | << SourceRange(D.getDeclSpec().getModulePrivateSpecLoc()) | ||||
5201 | << FixItHint::CreateRemoval(D.getDeclSpec().getModulePrivateSpecLoc()); | ||||
Larisse Voufo | 567f917 | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 5202 | else { |
Douglas Gregor | d023aec | 2011-09-09 20:53:38 +0000 | [diff] [blame] | 5203 | NewVD->setModulePrivate(); |
Larisse Voufo | 567f917 | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 5204 | if (NewTemplate) |
5205 | NewTemplate->setModulePrivate(); | ||||
5206 | } | ||||
Douglas Gregor | d023aec | 2011-09-09 20:53:38 +0000 | [diff] [blame] | 5207 | } |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 5208 | |
Zhongxing Xu | cb8f4f1 | 2009-01-16 02:36:34 +0000 | [diff] [blame] | 5209 | // Handle attributes prior to checking for duplicates in MergeVarDecl |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 5210 | ProcessDeclAttributes(S, NewVD, D); |
Zhongxing Xu | cb8f4f1 | 2009-01-16 02:36:34 +0000 | [diff] [blame] | 5211 | |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 5212 | if (NewVD->hasAttrs()) |
5213 | CheckAlignasUnderalignment(NewVD); | ||||
5214 | |||||
Peter Collingbourne | c0c0066 | 2012-08-28 20:37:50 +0000 | [diff] [blame] | 5215 | if (getLangOpts().CUDA) { |
5216 | // CUDA B.2.5: "__shared__ and __constant__ variables have implied static | ||||
5217 | // storage [duration]." | ||||
5218 | if (SC == SC_None && S->getFnParent() != 0 && | ||||
Rafael Espindola | 0db661e | 2012-12-21 01:21:33 +0000 | [diff] [blame] | 5219 | (NewVD->hasAttr<CUDASharedAttr>() || |
5220 | NewVD->hasAttr<CUDAConstantAttr>())) { | ||||
Peter Collingbourne | c0c0066 | 2012-08-28 20:37:50 +0000 | [diff] [blame] | 5221 | NewVD->setStorageClass(SC_Static); |
Rafael Espindola | 0db661e | 2012-12-21 01:21:33 +0000 | [diff] [blame] | 5222 | } |
Peter Collingbourne | c0c0066 | 2012-08-28 20:37:50 +0000 | [diff] [blame] | 5223 | } |
5224 | |||||
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5225 | // In auto-retain/release, infer strong retension for variables of |
5226 | // retainable type. | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5227 | if (getLangOpts().ObjCAutoRefCount && inferObjCARCLifetime(NewVD)) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5228 | NewVD->setInvalidDecl(); |
5229 | |||||
Zhongxing Xu | cb8f4f1 | 2009-01-16 02:36:34 +0000 | [diff] [blame] | 5230 | // Handle GNU asm-label extension (encoded as an attribute). |
Chris Lattner | 16c5dea | 2010-10-10 18:16:20 +0000 | [diff] [blame] | 5231 | if (Expr *E = (Expr*)D.getAsmLabel()) { |
Zhongxing Xu | cb8f4f1 | 2009-01-16 02:36:34 +0000 | [diff] [blame] | 5232 | // The parser guarantees this is a string. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5233 | StringLiteral *SE = cast<StringLiteral>(E); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5234 | StringRef Label = SE->getString(); |
Abramo Bagnara | 2b57aef | 2011-01-11 15:16:52 +0000 | [diff] [blame] | 5235 | if (S->getFnParent() != 0) { |
5236 | switch (SC) { | ||||
5237 | case SC_None: | ||||
5238 | case SC_Auto: | ||||
5239 | Diag(E->getExprLoc(), diag::warn_asm_label_on_auto_decl) << Label; | ||||
5240 | break; | ||||
5241 | case SC_Register: | ||||
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 5242 | if (!Context.getTargetInfo().isValidGCCRegisterName(Label)) |
Abramo Bagnara | 2b57aef | 2011-01-11 15:16:52 +0000 | [diff] [blame] | 5243 | Diag(E->getExprLoc(), diag::err_asm_unknown_register_name) << Label; |
5244 | break; | ||||
5245 | case SC_Static: | ||||
5246 | case SC_Extern: | ||||
5247 | case SC_PrivateExtern: | ||||
Peter Collingbourne | 8c25fc5 | 2011-09-19 21:14:35 +0000 | [diff] [blame] | 5248 | case SC_OpenCLWorkGroupLocal: |
Abramo Bagnara | 2b57aef | 2011-01-11 15:16:52 +0000 | [diff] [blame] | 5249 | break; |
5250 | } | ||||
5251 | } | ||||
5252 | |||||
5253 | NewVD->addAttr(::new (Context) AsmLabelAttr(SE->getStrTokenLoc(0), | ||||
Rafael Espindola | baf8695 | 2011-01-01 21:47:03 +0000 | [diff] [blame] | 5254 | Context, Label)); |
David Chisnall | 5f3c163 | 2012-02-18 16:12:34 +0000 | [diff] [blame] | 5255 | } else if (!ExtnameUndeclaredIdentifiers.empty()) { |
5256 | llvm::DenseMap<IdentifierInfo*,AsmLabelAttr*>::iterator I = | ||||
5257 | ExtnameUndeclaredIdentifiers.find(NewVD->getIdentifier()); | ||||
5258 | if (I != ExtnameUndeclaredIdentifiers.end()) { | ||||
5259 | NewVD->addAttr(I->second); | ||||
5260 | ExtnameUndeclaredIdentifiers.erase(I); | ||||
5261 | } | ||||
Zhongxing Xu | cb8f4f1 | 2009-01-16 02:36:34 +0000 | [diff] [blame] | 5262 | } |
5263 | |||||
John McCall | 8472af4 | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 5264 | // Diagnose shadowed variables before filtering for scope. |
John McCall | a369a95 | 2010-03-20 04:12:52 +0000 | [diff] [blame] | 5265 | if (!D.getCXXScopeSpec().isSet()) |
John McCall | 053f4bd | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 5266 | CheckShadow(S, NewVD, Previous); |
John McCall | 8472af4 | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 5267 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5268 | // Don't consider existing declarations that are in a different |
5269 | // scope and are out-of-semantic-context declarations (if the new | ||||
5270 | // declaration has linkage). | ||||
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5271 | FilterLookupForScope( |
5272 | Previous, DC, S, shouldConsiderLinkage(NewVD), | ||||
5273 | IsExplicitSpecialization || IsVariableTemplateSpecialization); | ||||
5274 | |||||
Richard Smith | dd9459f | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 5275 | // Check whether the previous declaration is in the same block scope. This |
5276 | // affects whether we merge types with it, per C++11 [dcl.array]p3. | ||||
5277 | if (getLangOpts().CPlusPlus && | ||||
5278 | NewVD->isLocalVarDecl() && NewVD->hasExternalStorage()) | ||||
5279 | NewVD->setPreviousDeclInSameBlockScope( | ||||
5280 | Previous.isSingleResult() && !Previous.isShadowed() && | ||||
5281 | isDeclInScope(Previous.getFoundDecl(), DC, S, false)); | ||||
5282 | |||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5283 | if (!getLangOpts().CPlusPlus) { |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 5284 | D.setRedeclaration(CheckVariableDeclaration(NewVD, Previous)); |
5285 | } else { | ||||
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 5286 | // Merge the decl with the existing one if appropriate. |
5287 | if (!Previous.empty()) { | ||||
5288 | if (Previous.isSingleResult() && | ||||
5289 | isa<FieldDecl>(Previous.getFoundDecl()) && | ||||
5290 | D.getCXXScopeSpec().isSet()) { | ||||
5291 | // The user tried to define a non-static data member | ||||
5292 | // out-of-line (C++ [dcl.meaning]p1). | ||||
5293 | Diag(NewVD->getLocation(), diag::err_nonstatic_member_out_of_line) | ||||
5294 | << D.getCXXScopeSpec().getRange(); | ||||
5295 | Previous.clear(); | ||||
5296 | NewVD->setInvalidDecl(); | ||||
5297 | } | ||||
5298 | } else if (D.getCXXScopeSpec().isSet()) { | ||||
5299 | // No previous declaration in the qualifying scope. | ||||
5300 | Diag(D.getIdentifierLoc(), diag::err_no_member) | ||||
5301 | << Name << computeDeclContext(D.getCXXScopeSpec(), true) | ||||
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 5302 | << D.getCXXScopeSpec().getRange(); |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 5303 | NewVD->setInvalidDecl(); |
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 5304 | } |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 5305 | |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5306 | if (!IsVariableTemplateSpecialization) { |
5307 | if (PrevVarTemplate) { | ||||
5308 | LookupResult PrevDecl(*this, GetNameForDeclarator(D), | ||||
5309 | LookupOrdinaryName, ForRedeclaration); | ||||
5310 | PrevDecl.addDecl(PrevVarTemplate->getTemplatedDecl()); | ||||
Larisse Voufo | 567f917 | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 5311 | D.setRedeclaration(CheckVariableDeclaration(NewVD, PrevDecl)); |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5312 | } else |
Larisse Voufo | 567f917 | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 5313 | D.setRedeclaration(CheckVariableDeclaration(NewVD, Previous)); |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5314 | } |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 5315 | |
5316 | // This is an explicit specialization of a static data member. Check it. | ||||
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5317 | if (IsExplicitSpecialization && !NewVD->isInvalidDecl() && |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 5318 | CheckMemberSpecialization(NewVD, Previous)) |
5319 | NewVD->setInvalidDecl(); | ||||
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 5320 | } |
Ryan Flynn | 478fbc6 | 2009-07-25 22:29:44 +0000 | [diff] [blame] | 5321 | |
Rafael Espindola | 65611bf | 2013-03-02 21:41:48 +0000 | [diff] [blame] | 5322 | ProcessPragmaWeak(S, NewVD); |
Rafael Espindola | 2a5bb50 | 2013-01-16 23:11:15 +0000 | [diff] [blame] | 5323 | checkAttributesAfterMerging(*this, *NewVD); |
5324 | |||||
Richard Smith | aa4bc18 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 5325 | // If this is the first declaration of an extern C variable, update |
5326 | // the map of such variables. | ||||
5327 | if (!NewVD->getPreviousDecl() && !NewVD->isInvalidDecl() && | ||||
5328 | isIncompleteDeclExternC(*this, NewVD)) | ||||
Richard Smith | 662f41b | 2013-06-18 20:15:12 +0000 | [diff] [blame] | 5329 | RegisterLocallyScopedExternCDecl(NewVD, S); |
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 5330 | |
Eli Friedman | 5e867c8 | 2013-07-10 00:30:46 +0000 | [diff] [blame] | 5331 | if (NewVD->isStaticLocal()) { |
5332 | Decl *ManglingContextDecl; | ||||
5333 | if (MangleNumberingContext *MCtx = | ||||
5334 | getCurrentMangleNumberContext(NewVD->getDeclContext(), | ||||
5335 | ManglingContextDecl)) { | ||||
5336 | Context.setManglingNumber(NewVD, MCtx->getManglingNumber(NewVD)); | ||||
5337 | } | ||||
5338 | } | ||||
5339 | |||||
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5340 | // If we are providing an explicit specialization of a static variable |
5341 | // template, make a note of that. | ||||
5342 | if (PrevVarTemplate && PrevVarTemplate->getInstantiatedFromMemberTemplate()) | ||||
Larisse Voufo | 04592e7 | 2013-08-22 00:28:27 +0000 | [diff] [blame] | 5343 | PrevVarTemplate->setMemberSpecialization(); |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5344 | |
Larisse Voufo | 567f917 | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 5345 | if (NewTemplate) { |
5346 | ActOnDocumentableDecl(NewTemplate); | ||||
5347 | return NewTemplate; | ||||
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5348 | } |
5349 | |||||
Larisse Voufo | 567f917 | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 5350 | return NewVD; |
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 5351 | } |
5352 | |||||
John McCall | 053f4bd | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 5353 | /// \brief Diagnose variable or built-in function shadowing. Implements |
5354 | /// -Wshadow. | ||||
John McCall | 8472af4 | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 5355 | /// |
John McCall | 053f4bd | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 5356 | /// This method is called whenever a VarDecl is added to a "useful" |
5357 | /// scope. | ||||
John McCall | 8472af4 | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 5358 | /// |
John McCall | a369a95 | 2010-03-20 04:12:52 +0000 | [diff] [blame] | 5359 | /// \param S the scope in which the shadowing name is being declared |
5360 | /// \param R the lookup of the name | ||||
John McCall | 8472af4 | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 5361 | /// |
John McCall | 053f4bd | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 5362 | void Sema::CheckShadow(Scope *S, VarDecl *D, const LookupResult& R) { |
John McCall | 8472af4 | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 5363 | // Return if warning is ignored. |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 5364 | if (Diags.getDiagnosticLevel(diag::warn_decl_shadow, R.getNameLoc()) == |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 5365 | DiagnosticsEngine::Ignored) |
John McCall | 8472af4 | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 5366 | return; |
5367 | |||||
Argyrios Kyrtzidis | 651f86f | 2011-02-08 18:21:25 +0000 | [diff] [blame] | 5368 | // Don't diagnose declarations at file scope. |
Argyrios Kyrtzidis | 865dd8c | 2011-04-25 21:39:50 +0000 | [diff] [blame] | 5369 | if (D->hasGlobalStorage()) |
John McCall | 8472af4 | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 5370 | return; |
Argyrios Kyrtzidis | 865dd8c | 2011-04-25 21:39:50 +0000 | [diff] [blame] | 5371 | |
5372 | DeclContext *NewDC = D->getDeclContext(); | ||||
5373 | |||||
John McCall | a369a95 | 2010-03-20 04:12:52 +0000 | [diff] [blame] | 5374 | // Only diagnose if we're shadowing an unambiguous field or variable. |
Douglas Gregor | c48c916 | 2010-03-17 16:03:44 +0000 | [diff] [blame] | 5375 | if (R.getResultKind() != LookupResult::Found) |
John McCall | 8472af4 | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 5376 | return; |
John McCall | 8472af4 | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 5377 | |
John McCall | 8472af4 | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 5378 | NamedDecl* ShadowedDecl = R.getFoundDecl(); |
5379 | if (!isa<VarDecl>(ShadowedDecl) && !isa<FieldDecl>(ShadowedDecl)) | ||||
5380 | return; | ||||
5381 | |||||
Argyrios Kyrtzidis | 36eb5e4 | 2011-01-31 07:04:54 +0000 | [diff] [blame] | 5382 | // Fields are not shadowed by variables in C++ static methods. |
5383 | if (isa<FieldDecl>(ShadowedDecl)) | ||||
5384 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(NewDC)) | ||||
5385 | if (MD->isStatic()) | ||||
5386 | return; | ||||
5387 | |||||
Argyrios Kyrtzidis | 49a6172 | 2011-01-31 07:04:50 +0000 | [diff] [blame] | 5388 | if (VarDecl *shadowedVar = dyn_cast<VarDecl>(ShadowedDecl)) |
5389 | if (shadowedVar->isExternC()) { | ||||
Argyrios Kyrtzidis | 49a6172 | 2011-01-31 07:04:50 +0000 | [diff] [blame] | 5390 | // For shadowing external vars, make sure that we point to the global |
5391 | // declaration, not a locally scoped extern declaration. | ||||
5392 | for (VarDecl::redecl_iterator | ||||
5393 | I = shadowedVar->redecls_begin(), E = shadowedVar->redecls_end(); | ||||
5394 | I != E; ++I) | ||||
5395 | if (I->isFileVarDecl()) { | ||||
5396 | ShadowedDecl = *I; | ||||
5397 | break; | ||||
5398 | } | ||||
5399 | } | ||||
5400 | |||||
5401 | DeclContext *OldDC = ShadowedDecl->getDeclContext(); | ||||
5402 | |||||
John McCall | a369a95 | 2010-03-20 04:12:52 +0000 | [diff] [blame] | 5403 | // Only warn about certain kinds of shadowing for class members. |
5404 | if (NewDC && NewDC->isRecord()) { | ||||
5405 | // In particular, don't warn about shadowing non-class members. | ||||
5406 | if (!OldDC->isRecord()) | ||||
5407 | return; | ||||
5408 | |||||
5409 | // TODO: should we warn about static data members shadowing | ||||
5410 | // static data members from base classes? | ||||
5411 | |||||
5412 | // TODO: don't diagnose for inaccessible shadowed members. | ||||
5413 | // This is hard to do perfectly because we might friend the | ||||
5414 | // shadowing context, but that's just a false negative. | ||||
5415 | } | ||||
5416 | |||||
5417 | // Determine what kind of declaration we're shadowing. | ||||
John McCall | 8472af4 | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 5418 | unsigned Kind; |
John McCall | a369a95 | 2010-03-20 04:12:52 +0000 | [diff] [blame] | 5419 | if (isa<RecordDecl>(OldDC)) { |
John McCall | 8472af4 | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 5420 | if (isa<FieldDecl>(ShadowedDecl)) |
5421 | Kind = 3; // field | ||||
5422 | else | ||||
5423 | Kind = 2; // static data member | ||||
John McCall | a369a95 | 2010-03-20 04:12:52 +0000 | [diff] [blame] | 5424 | } else if (OldDC->isFileContext()) |
John McCall | 8472af4 | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 5425 | Kind = 1; // global |
5426 | else | ||||
5427 | Kind = 0; // local | ||||
5428 | |||||
John McCall | a369a95 | 2010-03-20 04:12:52 +0000 | [diff] [blame] | 5429 | DeclarationName Name = R.getLookupName(); |
5430 | |||||
John McCall | 8472af4 | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 5431 | // Emit warning and note. |
John McCall | a369a95 | 2010-03-20 04:12:52 +0000 | [diff] [blame] | 5432 | Diag(R.getNameLoc(), diag::warn_decl_shadow) << Name << Kind << OldDC; |
John McCall | 8472af4 | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 5433 | Diag(ShadowedDecl->getLocation(), diag::note_previous_declaration); |
5434 | } | ||||
5435 | |||||
John McCall | 053f4bd | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 5436 | /// \brief Check -Wshadow without the advantage of a previous lookup. |
5437 | void Sema::CheckShadow(Scope *S, VarDecl *D) { | ||||
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 5438 | if (Diags.getDiagnosticLevel(diag::warn_decl_shadow, D->getLocation()) == |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 5439 | DiagnosticsEngine::Ignored) |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 5440 | return; |
5441 | |||||
John McCall | 053f4bd | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 5442 | LookupResult R(*this, D->getDeclName(), D->getLocation(), |
5443 | Sema::LookupOrdinaryName, Sema::ForRedeclaration); | ||||
5444 | LookupName(R, S); | ||||
5445 | CheckShadow(S, D, R); | ||||
5446 | } | ||||
5447 | |||||
Richard Smith | aa4bc18 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 5448 | /// Check for conflict between this global or extern "C" declaration and |
5449 | /// previous global or extern "C" declarations. This is only used in C++. | ||||
Rafael Espindola | 294ddc6 | 2013-01-11 19:34:23 +0000 | [diff] [blame] | 5450 | template<typename T> |
Richard Smith | aa4bc18 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 5451 | static bool checkGlobalOrExternCConflict( |
5452 | Sema &S, const T *ND, bool IsGlobal, LookupResult &Previous) { | ||||
5453 | assert(S.getLangOpts().CPlusPlus && "only C++ has extern \"C\""); | ||||
5454 | NamedDecl *Prev = S.findLocallyScopedExternCDecl(ND->getDeclName()); | ||||
Rafael Espindola | 2d1b096 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 5455 | |
Richard Smith | aa4bc18 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 5456 | if (!Prev && IsGlobal && !isIncompleteDeclExternC(S, ND)) { |
5457 | // The common case: this global doesn't conflict with any extern "C" | ||||
5458 | // declaration. | ||||
5459 | return false; | ||||
5460 | } | ||||
5461 | |||||
5462 | if (Prev) { | ||||
5463 | if (!IsGlobal || isIncompleteDeclExternC(S, ND)) { | ||||
5464 | // Both the old and new declarations have C language linkage. This is a | ||||
5465 | // redeclaration. | ||||
5466 | Previous.clear(); | ||||
5467 | Previous.addDecl(Prev); | ||||
5468 | return true; | ||||
5469 | } | ||||
5470 | |||||
5471 | // This is a global, non-extern "C" declaration, and there is a previous | ||||
5472 | // non-global extern "C" declaration. Diagnose if this is a variable | ||||
5473 | // declaration. | ||||
5474 | if (!isa<VarDecl>(ND)) | ||||
5475 | return false; | ||||
5476 | } else { | ||||
5477 | // The declaration is extern "C". Check for any declaration in the | ||||
5478 | // translation unit which might conflict. | ||||
5479 | if (IsGlobal) { | ||||
5480 | // We have already performed the lookup into the translation unit. | ||||
5481 | IsGlobal = false; | ||||
5482 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); | ||||
5483 | I != E; ++I) { | ||||
5484 | if (isa<VarDecl>(*I)) { | ||||
5485 | Prev = *I; | ||||
5486 | break; | ||||
5487 | } | ||||
5488 | } | ||||
5489 | } else { | ||||
5490 | DeclContext::lookup_result R = | ||||
5491 | S.Context.getTranslationUnitDecl()->lookup(ND->getDeclName()); | ||||
5492 | for (DeclContext::lookup_result::iterator I = R.begin(), E = R.end(); | ||||
5493 | I != E; ++I) { | ||||
5494 | if (isa<VarDecl>(*I)) { | ||||
5495 | Prev = *I; | ||||
5496 | break; | ||||
5497 | } | ||||
5498 | // FIXME: If we have any other entity with this name in global scope, | ||||
5499 | // the declaration is ill-formed, but that is a defect: it breaks the | ||||
5500 | // 'stat' hack, for instance. Only variables can have mangled name | ||||
5501 | // clashes with extern "C" declarations, so only they deserve a | ||||
5502 | // diagnostic. | ||||
5503 | } | ||||
5504 | } | ||||
5505 | |||||
5506 | if (!Prev) | ||||
Rafael Espindola | 2d1b096 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 5507 | return false; |
5508 | } | ||||
5509 | |||||
Richard Smith | aa4bc18 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 5510 | // Use the first declaration's location to ensure we point at something which |
5511 | // is lexically inside an extern "C" linkage-spec. | ||||
5512 | assert(Prev && "should have found a previous declaration to diagnose"); | ||||
5513 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(Prev)) | ||||
5514 | Prev = FD->getFirstDeclaration(); | ||||
5515 | else | ||||
5516 | Prev = cast<VarDecl>(Prev)->getFirstDeclaration(); | ||||
5517 | |||||
5518 | S.Diag(ND->getLocation(), diag::err_extern_c_global_conflict) | ||||
5519 | << IsGlobal << ND; | ||||
5520 | S.Diag(Prev->getLocation(), diag::note_extern_c_global_conflict) | ||||
5521 | << IsGlobal; | ||||
5522 | return false; | ||||
5523 | } | ||||
5524 | |||||
5525 | /// Apply special rules for handling extern "C" declarations. Returns \c true | ||||
5526 | /// if we have found that this is a redeclaration of some prior entity. | ||||
5527 | /// | ||||
5528 | /// Per C++ [dcl.link]p6: | ||||
5529 | /// Two declarations [for a function or variable] with C language linkage | ||||
5530 | /// with the same name that appear in different scopes refer to the same | ||||
5531 | /// [entity]. An entity with C language linkage shall not be declared with | ||||
5532 | /// the same name as an entity in global scope. | ||||
5533 | template<typename T> | ||||
5534 | static bool checkForConflictWithNonVisibleExternC(Sema &S, const T *ND, | ||||
5535 | LookupResult &Previous) { | ||||
5536 | if (!S.getLangOpts().CPlusPlus) { | ||||
5537 | // In C, when declaring a global variable, look for a corresponding 'extern' | ||||
5538 | // variable declared in function scope. | ||||
5539 | // | ||||
5540 | // FIXME: The corresponding case in C++ does not work. We should instead | ||||
5541 | // set the semantic DC for an extern local variable to be the innermost | ||||
5542 | // enclosing namespace, and ensure they are only found by redeclaration | ||||
5543 | // lookup. | ||||
5544 | if (ND->getDeclContext()->getRedeclContext()->isTranslationUnit()) { | ||||
5545 | if (NamedDecl *Prev = S.findLocallyScopedExternCDecl(ND->getDeclName())) { | ||||
5546 | Previous.clear(); | ||||
5547 | Previous.addDecl(Prev); | ||||
5548 | return true; | ||||
5549 | } | ||||
5550 | } | ||||
5551 | return false; | ||||
5552 | } | ||||
5553 | |||||
5554 | // A declaration in the translation unit can conflict with an extern "C" | ||||
5555 | // declaration. | ||||
5556 | if (ND->getDeclContext()->getRedeclContext()->isTranslationUnit()) | ||||
5557 | return checkGlobalOrExternCConflict(S, ND, /*IsGlobal*/true, Previous); | ||||
5558 | |||||
5559 | // An extern "C" declaration can conflict with a declaration in the | ||||
5560 | // translation unit or can be a redeclaration of an extern "C" declaration | ||||
5561 | // in another scope. | ||||
5562 | if (isIncompleteDeclExternC(S,ND)) | ||||
5563 | return checkGlobalOrExternCConflict(S, ND, /*IsGlobal*/false, Previous); | ||||
5564 | |||||
5565 | // Neither global nor extern "C": nothing to do. | ||||
5566 | return false; | ||||
Rafael Espindola | 294ddc6 | 2013-01-11 19:34:23 +0000 | [diff] [blame] | 5567 | } |
5568 | |||||
Richard Smith | dc7a4f5 | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 5569 | void Sema::CheckVariableDeclarationType(VarDecl *NewVD) { |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 5570 | // If the decl is already known invalid, don't check it. |
5571 | if (NewVD->isInvalidDecl()) | ||||
Richard Smith | dc7a4f5 | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 5572 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5573 | |
Abramo Bagnara | 4c5750e | 2012-11-08 14:44:42 +0000 | [diff] [blame] | 5574 | TypeSourceInfo *TInfo = NewVD->getTypeSourceInfo(); |
5575 | QualType T = TInfo->getType(); | ||||
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 5576 | |
Richard Smith | dc7a4f5 | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 5577 | // Defer checking an 'auto' type until its initializer is attached. |
5578 | if (T->isUndeducedType()) | ||||
5579 | return; | ||||
5580 | |||||
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5581 | if (T->isObjCObjectType()) { |
Fariborz Jahanian | dcf1011 | 2011-07-25 21:12:27 +0000 | [diff] [blame] | 5582 | Diag(NewVD->getLocation(), diag::err_statically_allocated_object) |
5583 | << FixItHint::CreateInsertion(NewVD->getLocation(), "*"); | ||||
Fariborz Jahanian | 8eaefdc | 2011-07-26 17:58:54 +0000 | [diff] [blame] | 5584 | T = Context.getObjCObjectPointerType(T); |
5585 | NewVD->setType(T); | ||||
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 5586 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5587 | |
Zhongxing Xu | cb8f4f1 | 2009-01-16 02:36:34 +0000 | [diff] [blame] | 5588 | // Emit an error if an address space was applied to decl with local storage. |
5589 | // This includes arrays of objects with address space qualifiers, but not | ||||
5590 | // automatic variables that point to other address spaces. | ||||
5591 | // ISO/IEC TR 18037 S5.1.2 | ||||
Chris Lattner | 16c5dea | 2010-10-10 18:16:20 +0000 | [diff] [blame] | 5592 | if (NewVD->hasLocalStorage() && T.getAddressSpace() != 0) { |
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 5593 | Diag(NewVD->getLocation(), diag::err_as_qualified_auto_decl); |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 5594 | NewVD->setInvalidDecl(); |
Richard Smith | dc7a4f5 | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 5595 | return; |
Zhongxing Xu | cb8f4f1 | 2009-01-16 02:36:34 +0000 | [diff] [blame] | 5596 | } |
Fariborz Jahanian | 7b5b317 | 2009-02-21 19:44:02 +0000 | [diff] [blame] | 5597 | |
Tanya Lattner | 8aa86d1 | 2013-04-05 20:14:50 +0000 | [diff] [blame] | 5598 | // OpenCL v1.2 s6.5 - All program scope variables must be declared in the |
5599 | // __constant address space. | ||||
5600 | if (getLangOpts().OpenCL && NewVD->isFileVarDecl() | ||||
5601 | && T.getAddressSpace() != LangAS::opencl_constant | ||||
5602 | && !T->isSamplerT()){ | ||||
5603 | Diag(NewVD->getLocation(), diag::err_opencl_global_invalid_addr_space); | ||||
5604 | NewVD->setInvalidDecl(); | ||||
Richard Smith | dc7a4f5 | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 5605 | return; |
Tanya Lattner | 8aa86d1 | 2013-04-05 20:14:50 +0000 | [diff] [blame] | 5606 | } |
5607 | |||||
Tanya Lattner | 5e94d6f | 2012-06-19 23:09:52 +0000 | [diff] [blame] | 5608 | // OpenCL v1.2 s6.8 -- The static qualifier is valid only in program |
5609 | // scope. | ||||
5610 | if ((getLangOpts().OpenCLVersion >= 120) | ||||
5611 | && NewVD->isStaticLocal()) { | ||||
5612 | Diag(NewVD->getLocation(), diag::err_static_function_scope); | ||||
5613 | NewVD->setInvalidDecl(); | ||||
Richard Smith | dc7a4f5 | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 5614 | return; |
Tanya Lattner | 5e94d6f | 2012-06-19 23:09:52 +0000 | [diff] [blame] | 5615 | } |
5616 | |||||
Mike Stump | f33651c | 2009-04-14 00:57:29 +0000 | [diff] [blame] | 5617 | if (NewVD->hasLocalStorage() && T.isObjCGCWeak() |
Fariborz Jahanian | 175df89 | 2011-06-07 20:15:46 +0000 | [diff] [blame] | 5618 | && !NewVD->hasAttr<BlocksAttr>()) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5619 | if (getLangOpts().getGC() != LangOptions::NonGC) |
Fariborz Jahanian | 175df89 | 2011-06-07 20:15:46 +0000 | [diff] [blame] | 5620 | Diag(NewVD->getLocation(), diag::warn_gc_attribute_weak_on_local); |
Ted Kremenek | 3ba17ee | 2012-10-02 05:36:02 +0000 | [diff] [blame] | 5621 | else { |
5622 | assert(!getLangOpts().ObjCAutoRefCount); | ||||
Fariborz Jahanian | 175df89 | 2011-06-07 20:15:46 +0000 | [diff] [blame] | 5623 | Diag(NewVD->getLocation(), diag::warn_attribute_weak_on_local); |
Ted Kremenek | 3ba17ee | 2012-10-02 05:36:02 +0000 | [diff] [blame] | 5624 | } |
Fariborz Jahanian | 175df89 | 2011-06-07 20:15:46 +0000 | [diff] [blame] | 5625 | } |
Chris Lattner | 16c5dea | 2010-10-10 18:16:20 +0000 | [diff] [blame] | 5626 | |
Chris Lattner | 38c5ebd | 2009-04-19 05:21:20 +0000 | [diff] [blame] | 5627 | bool isVM = T->isVariablyModifiedType(); |
Chris Lattner | be6d259 | 2009-07-19 20:17:11 +0000 | [diff] [blame] | 5628 | if (isVM || NewVD->hasAttr<CleanupAttr>() || |
John McCall | e46f62c | 2010-08-01 01:24:59 +0000 | [diff] [blame] | 5629 | NewVD->hasAttr<BlocksAttr>()) |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 5630 | getCurFunction()->setHasBranchProtectedScope(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5631 | |
Chris Lattner | 38c5ebd | 2009-04-19 05:21:20 +0000 | [diff] [blame] | 5632 | if ((isVM && NewVD->hasLinkage()) || |
5633 | (T->isVariableArrayType() && NewVD->hasGlobalStorage())) { | ||||
Anders Carlsson | 1a7acfa | 2009-02-28 21:56:50 +0000 | [diff] [blame] | 5634 | bool SizeIsNegative; |
Douglas Gregor | 2767ce2 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 5635 | llvm::APSInt Oversized; |
Abramo Bagnara | 4c5750e | 2012-11-08 14:44:42 +0000 | [diff] [blame] | 5636 | TypeSourceInfo *FixedTInfo = |
5637 | TryToFixInvalidVariablyModifiedTypeSourceInfo(TInfo, Context, | ||||
5638 | SizeIsNegative, Oversized); | ||||
5639 | if (FixedTInfo == 0 && T->isVariableArrayType()) { | ||||
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 5640 | const VariableArrayType *VAT = Context.getAsVariableArrayType(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5641 | // FIXME: This won't give the correct result for |
5642 | // int a[10][n]; | ||||
Anders Carlsson | 1a7acfa | 2009-02-28 21:56:50 +0000 | [diff] [blame] | 5643 | SourceRange SizeRange = VAT->getSizeExpr()->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5644 | |
Anders Carlsson | 1a7acfa | 2009-02-28 21:56:50 +0000 | [diff] [blame] | 5645 | if (NewVD->isFileVarDecl()) |
5646 | Diag(NewVD->getLocation(), diag::err_vla_decl_in_file_scope) | ||||
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 5647 | << SizeRange; |
Enea Zaffanella | 9cbcab8 | 2013-05-10 20:34:44 +0000 | [diff] [blame] | 5648 | else if (NewVD->isStaticLocal()) |
Anders Carlsson | 1a7acfa | 2009-02-28 21:56:50 +0000 | [diff] [blame] | 5649 | Diag(NewVD->getLocation(), diag::err_vla_decl_has_static_storage) |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 5650 | << SizeRange; |
Anders Carlsson | 1a7acfa | 2009-02-28 21:56:50 +0000 | [diff] [blame] | 5651 | else |
5652 | Diag(NewVD->getLocation(), diag::err_vla_decl_has_extern_linkage) | ||||
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 5653 | << SizeRange; |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 5654 | NewVD->setInvalidDecl(); |
Richard Smith | dc7a4f5 | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 5655 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5656 | } |
5657 | |||||
Abramo Bagnara | 4c5750e | 2012-11-08 14:44:42 +0000 | [diff] [blame] | 5658 | if (FixedTInfo == 0) { |
Anders Carlsson | 1a7acfa | 2009-02-28 21:56:50 +0000 | [diff] [blame] | 5659 | if (NewVD->isFileVarDecl()) |
5660 | Diag(NewVD->getLocation(), diag::err_vm_decl_in_file_scope); | ||||
5661 | else | ||||
5662 | Diag(NewVD->getLocation(), diag::err_vm_decl_has_extern_linkage); | ||||
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 5663 | NewVD->setInvalidDecl(); |
Richard Smith | dc7a4f5 | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 5664 | return; |
Anders Carlsson | 1a7acfa | 2009-02-28 21:56:50 +0000 | [diff] [blame] | 5665 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5666 | |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 5667 | Diag(NewVD->getLocation(), diag::warn_illegal_constant_array_size); |
Abramo Bagnara | eae859a | 2012-11-08 16:01:51 +0000 | [diff] [blame] | 5668 | NewVD->setType(FixedTInfo->getType()); |
Abramo Bagnara | 4c5750e | 2012-11-08 14:44:42 +0000 | [diff] [blame] | 5669 | NewVD->setTypeSourceInfo(FixedTInfo); |
Anders Carlsson | 1a7acfa | 2009-02-28 21:56:50 +0000 | [diff] [blame] | 5670 | } |
5671 | |||||
David Majnemer | aa71567 | 2013-05-29 00:56:45 +0000 | [diff] [blame] | 5672 | if (T->isVoidType()) { |
5673 | // C++98 [dcl.stc]p5: The extern specifier can be applied only to the names | ||||
5674 | // of objects and functions. | ||||
5675 | if (NewVD->isThisDeclarationADefinition() || getLangOpts().CPlusPlus) { | ||||
5676 | Diag(NewVD->getLocation(), diag::err_typecheck_decl_incomplete_type) | ||||
5677 | << T; | ||||
5678 | NewVD->setInvalidDecl(); | ||||
5679 | return; | ||||
5680 | } | ||||
Richard Smith | dc7a4f5 | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 5681 | } |
5682 | |||||
5683 | if (!NewVD->hasLocalStorage() && NewVD->hasAttr<BlocksAttr>()) { | ||||
5684 | Diag(NewVD->getLocation(), diag::err_block_on_nonlocal); | ||||
5685 | NewVD->setInvalidDecl(); | ||||
5686 | return; | ||||
5687 | } | ||||
5688 | |||||
5689 | if (isVM && NewVD->hasAttr<BlocksAttr>()) { | ||||
5690 | Diag(NewVD->getLocation(), diag::err_block_on_vm); | ||||
5691 | NewVD->setInvalidDecl(); | ||||
5692 | return; | ||||
5693 | } | ||||
5694 | |||||
5695 | if (NewVD->isConstexpr() && !T->isDependentType() && | ||||
5696 | RequireLiteralType(NewVD->getLocation(), T, | ||||
5697 | diag::err_constexpr_var_non_literal)) { | ||||
5698 | // Can't perform this check until the type is deduced. | ||||
5699 | NewVD->setInvalidDecl(); | ||||
5700 | return; | ||||
5701 | } | ||||
5702 | } | ||||
5703 | |||||
5704 | /// \brief Perform semantic checking on a newly-created variable | ||||
5705 | /// declaration. | ||||
5706 | /// | ||||
5707 | /// This routine performs all of the type-checking required for a | ||||
5708 | /// variable declaration once it has been built. It is used both to | ||||
5709 | /// check variables after they have been parsed and their declarators | ||||
5710 | /// have been translated into a declaration, and to check variables | ||||
5711 | /// that have been instantiated from a template. | ||||
5712 | /// | ||||
5713 | /// Sets NewVD->isInvalidDecl() if an error was encountered. | ||||
5714 | /// | ||||
5715 | /// Returns true if the variable declaration is a redeclaration. | ||||
Larisse Voufo | 567f917 | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 5716 | bool Sema::CheckVariableDeclaration(VarDecl *NewVD, LookupResult &Previous) { |
Richard Smith | dc7a4f5 | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 5717 | CheckVariableDeclarationType(NewVD); |
5718 | |||||
5719 | // If the decl is already known invalid, don't check it. | ||||
5720 | if (NewVD->isInvalidDecl()) | ||||
5721 | return false; | ||||
5722 | |||||
John McCall | 5b8740f | 2013-04-01 18:34:28 +0000 | [diff] [blame] | 5723 | // If we did not find anything by this name, look for a non-visible |
5724 | // extern "C" declaration with the same name. | ||||
5725 | // | ||||
John McCall | 5b8740f | 2013-04-01 18:34:28 +0000 | [diff] [blame] | 5726 | // The actual standards text here is: |
5727 | // | ||||
5728 | // C++11 [basic.link]p6: | ||||
5729 | // The name of a function declared in block scope and the name | ||||
5730 | // of a variable declared by a block scope extern declaration | ||||
5731 | // have linkage. If there is a visible declaration of an entity | ||||
5732 | // with linkage having the same name and type, ignoring entities | ||||
5733 | // declared outside the innermost enclosing namespace scope, the | ||||
5734 | // block scope declaration declares that same entity and | ||||
5735 | // receives the linkage of the previous declaration. | ||||
5736 | // | ||||
Richard Smith | dd9459f | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 5737 | // C++11 [dcl.array]p3: |
5738 | // If there is a preceding declaration of the entity in the same | ||||
5739 | // scope in which the bound was specified, an omitted array bound | ||||
5740 | // is taken to be the same as in that earlier declaration. | ||||
5741 | // | ||||
John McCall | 5b8740f | 2013-04-01 18:34:28 +0000 | [diff] [blame] | 5742 | // C11 6.2.7p4: |
5743 | // For an identifier with internal or external linkage declared | ||||
5744 | // in a scope in which a prior declaration of that identifier is | ||||
5745 | // visible, if the prior declaration specifies internal or | ||||
5746 | // external linkage, the type of the identifier at the later | ||||
5747 | // declaration becomes the composite type. | ||||
5748 | // | ||||
5749 | // The most important point here is that we're not allowed to | ||||
5750 | // update our understanding of the type according to declarations | ||||
Richard Smith | dd9459f | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 5751 | // not in scope (in C++) or not visible (in C). |
5752 | bool MergeTypeWithPrevious; | ||||
5753 | if (Previous.empty() && | ||||
5754 | checkForConflictWithNonVisibleExternC(*this, NewVD, Previous)) | ||||
5755 | MergeTypeWithPrevious = false; | ||||
5756 | else | ||||
5757 | MergeTypeWithPrevious = | ||||
5758 | !Previous.isShadowed() && | ||||
5759 | (!getLangOpts().CPlusPlus || NewVD->isPreviousDeclInSameBlockScope() || | ||||
5760 | !NewVD->getLexicalDeclContext()->isFunctionOrMethod()); | ||||
Douglas Gregor | 6393519 | 2009-03-02 00:19:53 +0000 | [diff] [blame] | 5761 | |
Douglas Gregor | 7dc80e1 | 2013-01-09 00:47:56 +0000 | [diff] [blame] | 5762 | // Filter out any non-conflicting previous declarations. |
5763 | filterNonConflictingPreviousDecls(Context, NewVD, Previous); | ||||
5764 | |||||
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5765 | if (!Previous.empty()) { |
Larisse Voufo | 567f917 | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 5766 | MergeVarDecl(NewVD, Previous, MergeTypeWithPrevious); |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 5767 | return true; |
Zhongxing Xu | cb8f4f1 | 2009-01-16 02:36:34 +0000 | [diff] [blame] | 5768 | } |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 5769 | return false; |
Zhongxing Xu | cb8f4f1 | 2009-01-16 02:36:34 +0000 | [diff] [blame] | 5770 | } |
5771 | |||||
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 5772 | /// \brief Data used with FindOverriddenMethod |
5773 | struct FindOverriddenMethodData { | ||||
5774 | Sema *S; | ||||
5775 | CXXMethodDecl *Method; | ||||
5776 | }; | ||||
5777 | |||||
5778 | /// \brief Member lookup function that determines whether a given C++ | ||||
5779 | /// method overrides a method in a base class, to be used with | ||||
5780 | /// CXXRecordDecl::lookupInBases(). | ||||
John McCall | af8e6ed | 2009-11-12 03:15:40 +0000 | [diff] [blame] | 5781 | static bool FindOverriddenMethod(const CXXBaseSpecifier *Specifier, |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 5782 | CXXBasePath &Path, |
5783 | void *UserData) { | ||||
5784 | RecordDecl *BaseRecord = Specifier->getType()->getAs<RecordType>()->getDecl(); | ||||
Anders Carlsson | 9549680 | 2009-11-26 20:50:40 +0000 | [diff] [blame] | 5785 | |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 5786 | FindOverriddenMethodData *Data |
5787 | = reinterpret_cast<FindOverriddenMethodData*>(UserData); | ||||
Anders Carlsson | 9549680 | 2009-11-26 20:50:40 +0000 | [diff] [blame] | 5788 | |
5789 | DeclarationName Name = Data->Method->getDeclName(); | ||||
5790 | |||||
5791 | // FIXME: Do we care about other names here too? | ||||
5792 | if (Name.getNameKind() == DeclarationName::CXXDestructorName) { | ||||
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 5793 | // We really want to find the base class destructor here. |
Anders Carlsson | 9549680 | 2009-11-26 20:50:40 +0000 | [diff] [blame] | 5794 | QualType T = Data->S->Context.getTypeDeclType(BaseRecord); |
5795 | CanQualType CT = Data->S->Context.getCanonicalType(T); | ||||
5796 | |||||
Anders Carlsson | 1a68972 | 2009-11-27 01:26:58 +0000 | [diff] [blame] | 5797 | Name = Data->S->Context.DeclarationNames.getCXXDestructorName(CT); |
Anders Carlsson | 9549680 | 2009-11-26 20:50:40 +0000 | [diff] [blame] | 5798 | } |
5799 | |||||
5800 | for (Path.Decls = BaseRecord->lookup(Name); | ||||
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 5801 | !Path.Decls.empty(); |
5802 | Path.Decls = Path.Decls.slice(1)) { | ||||
5803 | NamedDecl *D = Path.Decls.front(); | ||||
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 5804 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) { |
5805 | if (MD->isVirtual() && !Data->S->IsOverload(Data->Method, MD, false)) | ||||
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 5806 | return true; |
5807 | } | ||||
5808 | } | ||||
5809 | |||||
5810 | return false; | ||||
5811 | } | ||||
5812 | |||||
David Blaikie | 5708c18 | 2012-10-17 00:47:58 +0000 | [diff] [blame] | 5813 | namespace { |
5814 | enum OverrideErrorKind { OEK_All, OEK_NonDeleted, OEK_Deleted }; | ||||
5815 | } | ||||
5816 | /// \brief Report an error regarding overriding, along with any relevant | ||||
5817 | /// overriden methods. | ||||
5818 | /// | ||||
5819 | /// \param DiagID the primary error to report. | ||||
5820 | /// \param MD the overriding method. | ||||
5821 | /// \param OEK which overrides to include as notes. | ||||
5822 | static void ReportOverrides(Sema& S, unsigned DiagID, const CXXMethodDecl *MD, | ||||
5823 | OverrideErrorKind OEK = OEK_All) { | ||||
5824 | S.Diag(MD->getLocation(), DiagID) << MD->getDeclName(); | ||||
5825 | for (CXXMethodDecl::method_iterator I = MD->begin_overridden_methods(), | ||||
5826 | E = MD->end_overridden_methods(); | ||||
5827 | I != E; ++I) { | ||||
5828 | // This check (& the OEK parameter) could be replaced by a predicate, but | ||||
5829 | // without lambdas that would be overkill. This is still nicer than writing | ||||
5830 | // out the diag loop 3 times. | ||||
5831 | if ((OEK == OEK_All) || | ||||
5832 | (OEK == OEK_NonDeleted && !(*I)->isDeleted()) || | ||||
5833 | (OEK == OEK_Deleted && (*I)->isDeleted())) | ||||
5834 | S.Diag((*I)->getLocation(), diag::note_overridden_virtual_function); | ||||
5835 | } | ||||
5836 | } | ||||
5837 | |||||
Sebastian Redl | a165da0 | 2009-11-18 21:51:29 +0000 | [diff] [blame] | 5838 | /// AddOverriddenMethods - See if a method overrides any in the base classes, |
5839 | /// 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] | 5840 | bool Sema::AddOverriddenMethods(CXXRecordDecl *DC, CXXMethodDecl *MD) { |
Sebastian Redl | a165da0 | 2009-11-18 21:51:29 +0000 | [diff] [blame] | 5841 | // Look for virtual methods in base classes that this method might override. |
5842 | CXXBasePaths Paths; | ||||
5843 | FindOverriddenMethodData Data; | ||||
5844 | Data.Method = MD; | ||||
5845 | Data.S = this; | ||||
David Blaikie | 5708c18 | 2012-10-17 00:47:58 +0000 | [diff] [blame] | 5846 | bool hasDeletedOverridenMethods = false; |
5847 | bool hasNonDeletedOverridenMethods = false; | ||||
Douglas Gregor | a6c1e3a | 2010-10-13 22:55:32 +0000 | [diff] [blame] | 5848 | bool AddedAny = false; |
Sebastian Redl | a165da0 | 2009-11-18 21:51:29 +0000 | [diff] [blame] | 5849 | if (DC->lookupInBases(&FindOverriddenMethod, &Data, Paths)) { |
5850 | for (CXXBasePaths::decl_iterator I = Paths.found_decls_begin(), | ||||
5851 | E = Paths.found_decls_end(); I != E; ++I) { | ||||
5852 | if (CXXMethodDecl *OldMD = dyn_cast<CXXMethodDecl>(*I)) { | ||||
Richard Trieu | 304e233 | 2011-07-01 20:02:53 +0000 | [diff] [blame] | 5853 | MD->addOverriddenMethod(OldMD->getCanonicalDecl()); |
Sebastian Redl | a165da0 | 2009-11-18 21:51:29 +0000 | [diff] [blame] | 5854 | if (!CheckOverridingFunctionReturnType(MD, OldMD) && |
Aaron Ballman | fff3248 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 5855 | !CheckOverridingFunctionAttributes(MD, OldMD) && |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 5856 | !CheckOverridingFunctionExceptionSpec(MD, OldMD) && |
Anders Carlsson | 2e1c730 | 2011-01-20 16:25:36 +0000 | [diff] [blame] | 5857 | !CheckIfOverriddenFunctionIsMarkedFinal(MD, OldMD)) { |
David Blaikie | 5708c18 | 2012-10-17 00:47:58 +0000 | [diff] [blame] | 5858 | hasDeletedOverridenMethods |= OldMD->isDeleted(); |
5859 | hasNonDeletedOverridenMethods |= !OldMD->isDeleted(); | ||||
Douglas Gregor | a6c1e3a | 2010-10-13 22:55:32 +0000 | [diff] [blame] | 5860 | AddedAny = true; |
5861 | } | ||||
Sebastian Redl | a165da0 | 2009-11-18 21:51:29 +0000 | [diff] [blame] | 5862 | } |
5863 | } | ||||
5864 | } | ||||
David Blaikie | 5708c18 | 2012-10-17 00:47:58 +0000 | [diff] [blame] | 5865 | |
5866 | if (hasDeletedOverridenMethods && !MD->isDeleted()) { | ||||
5867 | ReportOverrides(*this, diag::err_non_deleted_override, MD, OEK_Deleted); | ||||
5868 | } | ||||
5869 | if (hasNonDeletedOverridenMethods && MD->isDeleted()) { | ||||
5870 | ReportOverrides(*this, diag::err_deleted_override, MD, OEK_NonDeleted); | ||||
5871 | } | ||||
5872 | |||||
Douglas Gregor | a6c1e3a | 2010-10-13 22:55:32 +0000 | [diff] [blame] | 5873 | return AddedAny; |
Sebastian Redl | a165da0 | 2009-11-18 21:51:29 +0000 | [diff] [blame] | 5874 | } |
5875 | |||||
Kaelyn Uhrain | 2afd766 | 2011-10-11 00:28:39 +0000 | [diff] [blame] | 5876 | namespace { |
5877 | // Struct for holding all of the extra arguments needed by | ||||
5878 | // DiagnoseInvalidRedeclaration to call Sema::ActOnFunctionDeclarator. | ||||
5879 | struct ActOnFDArgs { | ||||
5880 | Scope *S; | ||||
5881 | Declarator &D; | ||||
Kaelyn Uhrain | 2afd766 | 2011-10-11 00:28:39 +0000 | [diff] [blame] | 5882 | MultiTemplateParamsArg TemplateParamLists; |
Kaelyn Uhrain | 2afd766 | 2011-10-11 00:28:39 +0000 | [diff] [blame] | 5883 | bool AddToScope; |
5884 | }; | ||||
5885 | } | ||||
5886 | |||||
Kaelyn Uhrain | 43e875d | 2012-01-18 21:41:41 +0000 | [diff] [blame] | 5887 | namespace { |
5888 | |||||
5889 | // 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] | 5890 | // Also only accept corrections that have the same parent decl. |
Kaelyn Uhrain | 43e875d | 2012-01-18 21:41:41 +0000 | [diff] [blame] | 5891 | class DifferentNameValidatorCCC : public CorrectionCandidateCallback { |
5892 | public: | ||||
Kaelyn Uhrain | ef094a1 | 2012-06-07 23:57:08 +0000 | [diff] [blame] | 5893 | DifferentNameValidatorCCC(ASTContext &Context, FunctionDecl *TypoFD, |
5894 | CXXRecordDecl *Parent) | ||||
5895 | : Context(Context), OriginalFD(TypoFD), | ||||
5896 | ExpectedParent(Parent ? Parent->getCanonicalDecl() : 0) {} | ||||
Kaelyn Uhrain | 3336353 | 2012-02-16 22:40:59 +0000 | [diff] [blame] | 5897 | |
Kaelyn Uhrain | 43e875d | 2012-01-18 21:41:41 +0000 | [diff] [blame] | 5898 | virtual bool ValidateCandidate(const TypoCorrection &candidate) { |
Kaelyn Uhrain | 3336353 | 2012-02-16 22:40:59 +0000 | [diff] [blame] | 5899 | if (candidate.getEditDistance() == 0) |
5900 | return false; | ||||
5901 | |||||
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 5902 | SmallVector<unsigned, 1> MismatchedParams; |
Kaelyn Uhrain | ef094a1 | 2012-06-07 23:57:08 +0000 | [diff] [blame] | 5903 | for (TypoCorrection::const_decl_iterator CDecl = candidate.begin(), |
5904 | CDeclEnd = candidate.end(); | ||||
5905 | CDecl != CDeclEnd; ++CDecl) { | ||||
5906 | FunctionDecl *FD = dyn_cast<FunctionDecl>(*CDecl); | ||||
5907 | |||||
5908 | if (FD && !FD->hasBody() && | ||||
5909 | hasSimilarParameters(Context, FD, OriginalFD, MismatchedParams)) { | ||||
5910 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) { | ||||
5911 | CXXRecordDecl *Parent = MD->getParent(); | ||||
5912 | if (Parent && Parent->getCanonicalDecl() == ExpectedParent) | ||||
5913 | return true; | ||||
5914 | } else if (!ExpectedParent) { | ||||
5915 | return true; | ||||
5916 | } | ||||
5917 | } | ||||
Kaelyn Uhrain | 3336353 | 2012-02-16 22:40:59 +0000 | [diff] [blame] | 5918 | } |
5919 | |||||
Kaelyn Uhrain | ef094a1 | 2012-06-07 23:57:08 +0000 | [diff] [blame] | 5920 | return false; |
Kaelyn Uhrain | 43e875d | 2012-01-18 21:41:41 +0000 | [diff] [blame] | 5921 | } |
Kaelyn Uhrain | 3336353 | 2012-02-16 22:40:59 +0000 | [diff] [blame] | 5922 | |
5923 | private: | ||||
Kaelyn Uhrain | ef094a1 | 2012-06-07 23:57:08 +0000 | [diff] [blame] | 5924 | ASTContext &Context; |
5925 | FunctionDecl *OriginalFD; | ||||
Kaelyn Uhrain | 3336353 | 2012-02-16 22:40:59 +0000 | [diff] [blame] | 5926 | CXXRecordDecl *ExpectedParent; |
Kaelyn Uhrain | 43e875d | 2012-01-18 21:41:41 +0000 | [diff] [blame] | 5927 | }; |
5928 | |||||
5929 | } | ||||
5930 | |||||
Kaelyn Uhrain | 2afd766 | 2011-10-11 00:28:39 +0000 | [diff] [blame] | 5931 | /// \brief Generate diagnostics for an invalid function redeclaration. |
5932 | /// | ||||
5933 | /// This routine handles generating the diagnostic messages for an invalid | ||||
5934 | /// function redeclaration, including finding possible similar declarations | ||||
5935 | /// or performing typo correction if there are no previous declarations with | ||||
5936 | /// the same name. | ||||
5937 | /// | ||||
Sylvestre Ledru | f3477c1 | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 5938 | /// Returns a NamedDecl iff typo correction was performed and substituting in |
Kaelyn Uhrain | 2afd766 | 2011-10-11 00:28:39 +0000 | [diff] [blame] | 5939 | /// the new declaration name does not cause new errors. |
Richard Smith | 4e9686b | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 5940 | static NamedDecl *DiagnoseInvalidRedeclaration( |
Kaelyn Uhrain | f09ce39 | 2011-10-11 00:28:52 +0000 | [diff] [blame] | 5941 | Sema &SemaRef, LookupResult &Previous, FunctionDecl *NewFD, |
Richard Smith | 4e9686b | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 5942 | ActOnFDArgs &ExtraArgs, bool IsLocalFriend, Scope *S) { |
Kaelyn Uhrain | 5161163 | 2011-08-18 18:19:12 +0000 | [diff] [blame] | 5943 | DeclarationName Name = NewFD->getDeclName(); |
Kaelyn Uhrain | 2afd766 | 2011-10-11 00:28:39 +0000 | [diff] [blame] | 5944 | DeclContext *NewDC = NewFD->getDeclContext(); |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 5945 | SmallVector<unsigned, 1> MismatchedParams; |
5946 | SmallVector<std::pair<FunctionDecl *, unsigned>, 1> NearMatches; | ||||
Kaelyn Uhrain | 5161163 | 2011-08-18 18:19:12 +0000 | [diff] [blame] | 5947 | TypoCorrection Correction; |
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 5948 | bool IsDefinition = ExtraArgs.D.isFunctionDefinition(); |
Richard Smith | 4e9686b | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 5949 | unsigned DiagMsg = IsLocalFriend ? diag::err_no_matching_local_friend |
5950 | : diag::err_member_decl_does_not_match; | ||||
5951 | LookupResult Prev(SemaRef, Name, NewFD->getLocation(), | ||||
5952 | IsLocalFriend ? Sema::LookupLocalFriendName | ||||
5953 | : Sema::LookupOrdinaryName, | ||||
5954 | Sema::ForRedeclaration); | ||||
Kaelyn Uhrain | 5161163 | 2011-08-18 18:19:12 +0000 | [diff] [blame] | 5955 | |
5956 | NewFD->setInvalidDecl(); | ||||
Richard Smith | 4e9686b | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 5957 | if (IsLocalFriend) |
5958 | SemaRef.LookupName(Prev, S); | ||||
5959 | else | ||||
5960 | SemaRef.LookupQualifiedName(Prev, NewDC); | ||||
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 5961 | assert(!Prev.isAmbiguous() && |
5962 | "Cannot have an ambiguity in previous-declaration lookup"); | ||||
Kaelyn Uhrain | 3336353 | 2012-02-16 22:40:59 +0000 | [diff] [blame] | 5963 | CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(NewFD); |
Kaelyn Uhrain | ef094a1 | 2012-06-07 23:57:08 +0000 | [diff] [blame] | 5964 | DifferentNameValidatorCCC Validator(SemaRef.Context, NewFD, |
5965 | MD ? MD->getParent() : 0); | ||||
Kaelyn Uhrain | 5161163 | 2011-08-18 18:19:12 +0000 | [diff] [blame] | 5966 | if (!Prev.empty()) { |
5967 | for (LookupResult::iterator Func = Prev.begin(), FuncEnd = Prev.end(); | ||||
5968 | Func != FuncEnd; ++Func) { | ||||
5969 | FunctionDecl *FD = dyn_cast<FunctionDecl>(*Func); | ||||
Kaelyn Uhrain | f09ce39 | 2011-10-11 00:28:52 +0000 | [diff] [blame] | 5970 | if (FD && |
5971 | hasSimilarParameters(SemaRef.Context, FD, NewFD, MismatchedParams)) { | ||||
Kaelyn Uhrain | 5161163 | 2011-08-18 18:19:12 +0000 | [diff] [blame] | 5972 | // Add 1 to the index so that 0 can mean the mismatch didn't |
5973 | // involve a parameter | ||||
5974 | unsigned ParamNum = | ||||
5975 | MismatchedParams.empty() ? 0 : MismatchedParams.front() + 1; | ||||
5976 | NearMatches.push_back(std::make_pair(FD, ParamNum)); | ||||
5977 | } | ||||
Kaelyn Uhrain | 4d9d157 | 2011-08-04 17:40:00 +0000 | [diff] [blame] | 5978 | } |
Kaelyn Uhrain | 5161163 | 2011-08-18 18:19:12 +0000 | [diff] [blame] | 5979 | // If the qualified name lookup yielded nothing, try typo correction |
Richard Smith | 4e9686b | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 5980 | } else if ((Correction = SemaRef.CorrectTypo( |
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 5981 | Prev.getLookupNameInfo(), Prev.getLookupKind(), S, |
5982 | &ExtraArgs.D.getCXXScopeSpec(), Validator, | ||||
5983 | IsLocalFriend ? 0 : NewDC))) { | ||||
Kaelyn Uhrain | 2afd766 | 2011-10-11 00:28:39 +0000 | [diff] [blame] | 5984 | // Set up everything for the call to ActOnFunctionDeclarator |
5985 | ExtraArgs.D.SetIdentifier(Correction.getCorrectionAsIdentifierInfo(), | ||||
5986 | ExtraArgs.D.getIdentifierLoc()); | ||||
5987 | Previous.clear(); | ||||
5988 | Previous.setLookupName(Correction.getCorrection()); | ||||
Kaelyn Uhrain | 5161163 | 2011-08-18 18:19:12 +0000 | [diff] [blame] | 5989 | for (TypoCorrection::decl_iterator CDecl = Correction.begin(), |
5990 | CDeclEnd = Correction.end(); | ||||
5991 | CDecl != CDeclEnd; ++CDecl) { | ||||
5992 | FunctionDecl *FD = dyn_cast<FunctionDecl>(*CDecl); | ||||
Kaelyn Uhrain | ef094a1 | 2012-06-07 23:57:08 +0000 | [diff] [blame] | 5993 | if (FD && !FD->hasBody() && |
5994 | hasSimilarParameters(SemaRef.Context, FD, NewFD, MismatchedParams)) { | ||||
Kaelyn Uhrain | 2afd766 | 2011-10-11 00:28:39 +0000 | [diff] [blame] | 5995 | Previous.addDecl(FD); |
Kaelyn Uhrain | 5161163 | 2011-08-18 18:19:12 +0000 | [diff] [blame] | 5996 | } |
5997 | } | ||||
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 5998 | bool wasRedeclaration = ExtraArgs.D.isRedeclaration(); |
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 5999 | |
6000 | NamedDecl *Result; | ||||
6001 | // Retry building the function declaration with the new previous | ||||
6002 | // declarations, and with errors suppressed. | ||||
6003 | { | ||||
6004 | // Trap errors. | ||||
6005 | Sema::SFINAETrap Trap(SemaRef); | ||||
6006 | |||||
6007 | // TODO: Refactor ActOnFunctionDeclarator so that we can call only the | ||||
6008 | // pieces need to verify the typo-corrected C++ declaration and hopefully | ||||
6009 | // eliminate the need for the parameter pack ExtraArgs. | ||||
6010 | Result = SemaRef.ActOnFunctionDeclarator( | ||||
6011 | ExtraArgs.S, ExtraArgs.D, | ||||
6012 | Correction.getCorrectionDecl()->getDeclContext(), | ||||
6013 | NewFD->getTypeSourceInfo(), Previous, ExtraArgs.TemplateParamLists, | ||||
6014 | ExtraArgs.AddToScope); | ||||
6015 | |||||
6016 | if (Trap.hasErrorOccurred()) | ||||
6017 | Result = 0; | ||||
Kaelyn Uhrain | 2afd766 | 2011-10-11 00:28:39 +0000 | [diff] [blame] | 6018 | } |
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 6019 | |
6020 | if (Result) { | ||||
6021 | // Determine which correction we picked. | ||||
6022 | Decl *Canonical = Result->getCanonicalDecl(); | ||||
6023 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); | ||||
6024 | I != E; ++I) | ||||
6025 | if ((*I)->getCanonicalDecl() == Canonical) | ||||
6026 | Correction.setCorrectionDecl(*I); | ||||
6027 | |||||
6028 | SemaRef.diagnoseTypo( | ||||
6029 | Correction, | ||||
6030 | SemaRef.PDiag(IsLocalFriend | ||||
6031 | ? diag::err_no_matching_local_friend_suggest | ||||
6032 | : diag::err_member_decl_does_not_match_suggest) | ||||
6033 | << Name << NewDC << IsDefinition); | ||||
6034 | return Result; | ||||
Kaelyn Uhrain | 2afd766 | 2011-10-11 00:28:39 +0000 | [diff] [blame] | 6035 | } |
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 6036 | |
6037 | // Pretend the typo correction never occurred | ||||
6038 | ExtraArgs.D.SetIdentifier(Name.getAsIdentifierInfo(), | ||||
6039 | ExtraArgs.D.getIdentifierLoc()); | ||||
6040 | ExtraArgs.D.setRedeclaration(wasRedeclaration); | ||||
6041 | Previous.clear(); | ||||
6042 | Previous.setLookupName(Name); | ||||
Kaelyn Uhrain | 5161163 | 2011-08-18 18:19:12 +0000 | [diff] [blame] | 6043 | } |
6044 | |||||
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 6045 | SemaRef.Diag(NewFD->getLocation(), DiagMsg) |
6046 | << Name << NewDC << IsDefinition << NewFD->getLocation(); | ||||
Kaelyn Uhrain | 5161163 | 2011-08-18 18:19:12 +0000 | [diff] [blame] | 6047 | |
Kaelyn Uhrain | 1055393 | 2011-10-10 18:01:37 +0000 | [diff] [blame] | 6048 | bool NewFDisConst = false; |
6049 | if (CXXMethodDecl *NewMD = dyn_cast<CXXMethodDecl>(NewFD)) | ||||
David Blaikie | 4ef832f | 2012-08-10 00:55:35 +0000 | [diff] [blame] | 6050 | NewFDisConst = NewMD->isConst(); |
Kaelyn Uhrain | 1055393 | 2011-10-10 18:01:37 +0000 | [diff] [blame] | 6051 | |
Craig Topper | 8bc99dd | 2013-07-04 03:15:42 +0000 | [diff] [blame] | 6052 | for (SmallVectorImpl<std::pair<FunctionDecl *, unsigned> >::iterator |
Kaelyn Uhrain | 5161163 | 2011-08-18 18:19:12 +0000 | [diff] [blame] | 6053 | NearMatch = NearMatches.begin(), NearMatchEnd = NearMatches.end(); |
6054 | NearMatch != NearMatchEnd; ++NearMatch) { | ||||
6055 | FunctionDecl *FD = NearMatch->first; | ||||
Richard Smith | 4e9686b | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 6056 | CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD); |
6057 | bool FDisConst = MD && MD->isConst(); | ||||
6058 | bool IsMember = MD || !IsLocalFriend; | ||||
Kaelyn Uhrain | 5161163 | 2011-08-18 18:19:12 +0000 | [diff] [blame] | 6059 | |
6060 | if (unsigned Idx = NearMatch->second) { | ||||
6061 | ParmVarDecl *FDParam = FD->getParamDecl(Idx-1); | ||||
Richard Smith | 1c931be | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 6062 | SourceLocation Loc = FDParam->getTypeSpecStartLoc(); |
6063 | if (Loc.isInvalid()) Loc = FD->getLocation(); | ||||
Richard Smith | 4e9686b | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 6064 | SemaRef.Diag(Loc, IsMember ? diag::note_member_def_close_param_match |
6065 | : diag::note_local_decl_close_param_match) | ||||
6066 | << Idx << FDParam->getType() | ||||
6067 | << NewFD->getParamDecl(Idx - 1)->getType(); | ||||
Kaelyn Uhrain | 1055393 | 2011-10-10 18:01:37 +0000 | [diff] [blame] | 6068 | } else if (FDisConst != NewFDisConst) { |
Kaelyn Uhrain | f09ce39 | 2011-10-11 00:28:52 +0000 | [diff] [blame] | 6069 | SemaRef.Diag(FD->getLocation(), diag::note_member_def_close_const_match) |
Kaelyn Uhrain | 1055393 | 2011-10-10 18:01:37 +0000 | [diff] [blame] | 6070 | << NewFDisConst << FD->getSourceRange().getEnd(); |
Kaelyn Uhrain | 5161163 | 2011-08-18 18:19:12 +0000 | [diff] [blame] | 6071 | } else |
Richard Smith | 4e9686b | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 6072 | SemaRef.Diag(FD->getLocation(), |
6073 | IsMember ? diag::note_member_def_close_match | ||||
6074 | : diag::note_local_decl_close_match); | ||||
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 6075 | } |
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 6076 | return 0; |
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 6077 | } |
6078 | |||||
David Blaikie | d662a79 | 2011-10-19 22:56:21 +0000 | [diff] [blame] | 6079 | static FunctionDecl::StorageClass getFunctionStorageClass(Sema &SemaRef, |
6080 | Declarator &D) { | ||||
Kaelyn Uhrain | d7e19ce | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6081 | switch (D.getDeclSpec().getStorageClassSpec()) { |
6082 | default: llvm_unreachable("Unknown storage class!"); | ||||
6083 | case DeclSpec::SCS_auto: | ||||
6084 | case DeclSpec::SCS_register: | ||||
6085 | case DeclSpec::SCS_mutable: | ||||
6086 | SemaRef.Diag(D.getDeclSpec().getStorageClassSpecLoc(), | ||||
6087 | diag::err_typecheck_sclass_func); | ||||
6088 | D.setInvalidType(); | ||||
6089 | break; | ||||
6090 | case DeclSpec::SCS_unspecified: break; | ||||
Rafael Espindola | 65dfa2b | 2013-04-25 12:11:36 +0000 | [diff] [blame] | 6091 | case DeclSpec::SCS_extern: |
6092 | if (D.getDeclSpec().isExternInLinkageSpec()) | ||||
6093 | return SC_None; | ||||
6094 | return SC_Extern; | ||||
Kaelyn Uhrain | d7e19ce | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6095 | case DeclSpec::SCS_static: { |
6096 | if (SemaRef.CurContext->getRedeclContext()->isFunctionOrMethod()) { | ||||
6097 | // C99 6.7.1p5: | ||||
6098 | // The declaration of an identifier for a function that has | ||||
6099 | // block scope shall have no explicit storage-class specifier | ||||
6100 | // other than extern | ||||
6101 | // See also (C++ [dcl.stc]p4). | ||||
6102 | SemaRef.Diag(D.getDeclSpec().getStorageClassSpecLoc(), | ||||
6103 | diag::err_static_block_func); | ||||
6104 | break; | ||||
6105 | } else | ||||
6106 | return SC_Static; | ||||
6107 | } | ||||
6108 | case DeclSpec::SCS_private_extern: return SC_PrivateExtern; | ||||
6109 | } | ||||
6110 | |||||
6111 | // No explicit storage class has already been returned | ||||
6112 | return SC_None; | ||||
6113 | } | ||||
6114 | |||||
6115 | static FunctionDecl* CreateNewFunctionDecl(Sema &SemaRef, Declarator &D, | ||||
6116 | DeclContext *DC, QualType &R, | ||||
6117 | TypeSourceInfo *TInfo, | ||||
6118 | FunctionDecl::StorageClass SC, | ||||
6119 | bool &IsVirtualOkay) { | ||||
6120 | DeclarationNameInfo NameInfo = SemaRef.GetNameForDeclarator(D); | ||||
6121 | DeclarationName Name = NameInfo.getName(); | ||||
6122 | |||||
6123 | FunctionDecl *NewFD = 0; | ||||
6124 | bool isInline = D.getDeclSpec().isInlineSpecified(); | ||||
Kaelyn Uhrain | d7e19ce | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6125 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6126 | if (!SemaRef.getLangOpts().CPlusPlus) { |
Kaelyn Uhrain | d7e19ce | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6127 | // Determine whether the function was written with a |
6128 | // prototype. This true when: | ||||
6129 | // - there is a prototype in the declarator, or | ||||
6130 | // - the type R of the function is some kind of typedef or other reference | ||||
6131 | // to a type name (which eventually refers to a function type). | ||||
6132 | bool HasPrototype = | ||||
6133 | (D.isFunctionDeclarator() && D.getFunctionTypeInfo().hasPrototype) || | ||||
6134 | (!isa<FunctionType>(R.getTypePtr()) && R->isFunctionProtoType()); | ||||
6135 | |||||
David Blaikie | d662a79 | 2011-10-19 22:56:21 +0000 | [diff] [blame] | 6136 | NewFD = FunctionDecl::Create(SemaRef.Context, DC, |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 6137 | D.getLocStart(), NameInfo, R, |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 6138 | TInfo, SC, isInline, |
6139 | HasPrototype, false); | ||||
Kaelyn Uhrain | d7e19ce | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6140 | if (D.isInvalidType()) |
6141 | NewFD->setInvalidDecl(); | ||||
6142 | |||||
6143 | // Set the lexical context. | ||||
6144 | NewFD->setLexicalDeclContext(SemaRef.CurContext); | ||||
6145 | |||||
6146 | return NewFD; | ||||
6147 | } | ||||
6148 | |||||
6149 | bool isExplicit = D.getDeclSpec().isExplicitSpecified(); | ||||
6150 | bool isConstexpr = D.getDeclSpec().isConstexprSpecified(); | ||||
6151 | |||||
6152 | // Check that the return type is not an abstract class type. | ||||
6153 | // For record types, this is done by the AbstractClassUsageDiagnoser once | ||||
6154 | // the class has been completely parsed. | ||||
6155 | if (!DC->isRecord() && | ||||
6156 | SemaRef.RequireNonAbstractType(D.getIdentifierLoc(), | ||||
6157 | R->getAs<FunctionType>()->getResultType(), | ||||
6158 | diag::err_abstract_type_in_decl, | ||||
6159 | SemaRef.AbstractReturnType)) | ||||
6160 | D.setInvalidType(); | ||||
6161 | |||||
6162 | if (Name.getNameKind() == DeclarationName::CXXConstructorName) { | ||||
6163 | // This is a C++ constructor declaration. | ||||
6164 | assert(DC->isRecord() && | ||||
6165 | "Constructors can only be declared in a member context"); | ||||
6166 | |||||
6167 | R = SemaRef.CheckConstructorDeclarator(D, R, SC); | ||||
6168 | return CXXConstructorDecl::Create(SemaRef.Context, cast<CXXRecordDecl>(DC), | ||||
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 6169 | D.getLocStart(), NameInfo, |
Kaelyn Uhrain | d7e19ce | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6170 | R, TInfo, isExplicit, isInline, |
6171 | /*isImplicitlyDeclared=*/false, | ||||
6172 | isConstexpr); | ||||
6173 | |||||
6174 | } else if (Name.getNameKind() == DeclarationName::CXXDestructorName) { | ||||
6175 | // This is a C++ destructor declaration. | ||||
6176 | if (DC->isRecord()) { | ||||
6177 | R = SemaRef.CheckDestructorDeclarator(D, R, SC); | ||||
6178 | CXXRecordDecl *Record = cast<CXXRecordDecl>(DC); | ||||
6179 | CXXDestructorDecl *NewDD = CXXDestructorDecl::Create( | ||||
6180 | SemaRef.Context, Record, | ||||
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 6181 | D.getLocStart(), |
Kaelyn Uhrain | d7e19ce | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6182 | NameInfo, R, TInfo, isInline, |
6183 | /*isImplicitlyDeclared=*/false); | ||||
6184 | |||||
6185 | // If the class is complete, then we now create the implicit exception | ||||
6186 | // specification. If the class is incomplete or dependent, we can't do | ||||
6187 | // it yet. | ||||
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6188 | if (SemaRef.getLangOpts().CPlusPlus11 && !Record->isDependentType() && |
Kaelyn Uhrain | d7e19ce | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6189 | Record->getDefinition() && !Record->isBeingDefined() && |
6190 | R->getAs<FunctionProtoType>()->getExceptionSpecType() == EST_None) { | ||||
6191 | SemaRef.AdjustDestructorExceptionSpec(Record, NewDD); | ||||
6192 | } | ||||
6193 | |||||
Peter Collingbourne | f51cfb8 | 2013-05-20 14:12:25 +0000 | [diff] [blame] | 6194 | // The Microsoft ABI requires that we perform the destructor body |
6195 | // checks (i.e. operator delete() lookup) at every declaration, as | ||||
6196 | // any translation unit may need to emit a deleting destructor. | ||||
6197 | if (SemaRef.Context.getTargetInfo().getCXXABI().isMicrosoft() && | ||||
6198 | !Record->isDependentType() && Record->getDefinition() && | ||||
6199 | !Record->isBeingDefined()) { | ||||
6200 | SemaRef.CheckDestructor(NewDD); | ||||
6201 | } | ||||
6202 | |||||
Kaelyn Uhrain | d7e19ce | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6203 | IsVirtualOkay = true; |
6204 | return NewDD; | ||||
6205 | |||||
6206 | } else { | ||||
6207 | SemaRef.Diag(D.getIdentifierLoc(), diag::err_destructor_not_member); | ||||
6208 | D.setInvalidType(); | ||||
6209 | |||||
6210 | // Create a FunctionDecl to satisfy the function definition parsing | ||||
6211 | // code path. | ||||
6212 | return FunctionDecl::Create(SemaRef.Context, DC, | ||||
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 6213 | D.getLocStart(), |
Kaelyn Uhrain | d7e19ce | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6214 | D.getIdentifierLoc(), Name, R, TInfo, |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 6215 | SC, isInline, |
Kaelyn Uhrain | d7e19ce | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6216 | /*hasPrototype=*/true, isConstexpr); |
6217 | } | ||||
6218 | |||||
6219 | } else if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName) { | ||||
6220 | if (!DC->isRecord()) { | ||||
6221 | SemaRef.Diag(D.getIdentifierLoc(), | ||||
6222 | diag::err_conv_function_not_member); | ||||
6223 | return 0; | ||||
6224 | } | ||||
6225 | |||||
6226 | SemaRef.CheckConversionDeclarator(D, R, SC); | ||||
6227 | IsVirtualOkay = true; | ||||
6228 | return CXXConversionDecl::Create(SemaRef.Context, cast<CXXRecordDecl>(DC), | ||||
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 6229 | D.getLocStart(), NameInfo, |
Kaelyn Uhrain | d7e19ce | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6230 | R, TInfo, isInline, isExplicit, |
6231 | isConstexpr, SourceLocation()); | ||||
6232 | |||||
6233 | } else if (DC->isRecord()) { | ||||
6234 | // If the name of the function is the same as the name of the record, | ||||
6235 | // then this must be an invalid constructor that has a return type. | ||||
6236 | // (The parser checks for a return type and makes the declarator a | ||||
6237 | // constructor if it has no return type). | ||||
6238 | if (Name.getAsIdentifierInfo() && | ||||
6239 | Name.getAsIdentifierInfo() == cast<CXXRecordDecl>(DC)->getIdentifier()){ | ||||
6240 | SemaRef.Diag(D.getIdentifierLoc(), diag::err_constructor_return_type) | ||||
6241 | << SourceRange(D.getDeclSpec().getTypeSpecTypeLoc()) | ||||
6242 | << SourceRange(D.getIdentifierLoc()); | ||||
6243 | return 0; | ||||
6244 | } | ||||
6245 | |||||
Kaelyn Uhrain | d7e19ce | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6246 | // This is a C++ method declaration. |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 6247 | CXXMethodDecl *Ret = CXXMethodDecl::Create(SemaRef.Context, |
6248 | cast<CXXRecordDecl>(DC), | ||||
6249 | D.getLocStart(), NameInfo, R, | ||||
6250 | TInfo, SC, isInline, | ||||
6251 | isConstexpr, SourceLocation()); | ||||
6252 | IsVirtualOkay = !Ret->isStatic(); | ||||
6253 | return Ret; | ||||
Kaelyn Uhrain | d7e19ce | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6254 | } else { |
6255 | // Determine whether the function was written with a | ||||
6256 | // prototype. This true when: | ||||
6257 | // - we're in C++ (where every function has a prototype), | ||||
6258 | return FunctionDecl::Create(SemaRef.Context, DC, | ||||
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 6259 | D.getLocStart(), |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 6260 | NameInfo, R, TInfo, SC, isInline, |
Kaelyn Uhrain | d7e19ce | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6261 | true/*HasPrototype*/, isConstexpr); |
6262 | } | ||||
6263 | } | ||||
6264 | |||||
Eli Friedman | 7c3c6bc | 2012-09-20 01:40:23 +0000 | [diff] [blame] | 6265 | void Sema::checkVoidParamDecl(ParmVarDecl *Param) { |
6266 | // In C++, the empty parameter-type-list must be spelled "void"; a | ||||
6267 | // typedef of void is not permitted. | ||||
6268 | if (getLangOpts().CPlusPlus && | ||||
6269 | Param->getType().getUnqualifiedType() != Context.VoidTy) { | ||||
6270 | bool IsTypeAlias = false; | ||||
6271 | if (const TypedefType *TT = Param->getType()->getAs<TypedefType>()) | ||||
6272 | IsTypeAlias = isa<TypeAliasDecl>(TT->getDecl()); | ||||
6273 | else if (const TemplateSpecializationType *TST = | ||||
6274 | Param->getType()->getAs<TemplateSpecializationType>()) | ||||
6275 | IsTypeAlias = TST->isTypeAlias(); | ||||
6276 | Diag(Param->getLocation(), diag::err_param_typedef_of_void) | ||||
6277 | << IsTypeAlias; | ||||
6278 | } | ||||
6279 | } | ||||
6280 | |||||
Matt Arsenault | e6c8afc | 2013-07-23 01:23:36 +0000 | [diff] [blame] | 6281 | enum OpenCLParamType { |
6282 | ValidKernelParam, | ||||
6283 | PtrPtrKernelParam, | ||||
6284 | PtrKernelParam, | ||||
6285 | InvalidKernelParam, | ||||
6286 | RecordKernelParam | ||||
6287 | }; | ||||
6288 | |||||
6289 | static OpenCLParamType getOpenCLKernelParameterType(QualType PT) { | ||||
6290 | if (PT->isPointerType()) { | ||||
6291 | QualType PointeeType = PT->getPointeeType(); | ||||
6292 | return PointeeType->isPointerType() ? PtrPtrKernelParam : PtrKernelParam; | ||||
6293 | } | ||||
6294 | |||||
6295 | // TODO: Forbid the other integer types (size_t, ptrdiff_t...) when they can | ||||
6296 | // be used as builtin types. | ||||
6297 | |||||
6298 | if (PT->isImageType()) | ||||
6299 | return PtrKernelParam; | ||||
6300 | |||||
6301 | if (PT->isBooleanType()) | ||||
6302 | return InvalidKernelParam; | ||||
6303 | |||||
6304 | if (PT->isEventT()) | ||||
6305 | return InvalidKernelParam; | ||||
6306 | |||||
6307 | if (PT->isHalfType()) | ||||
6308 | return InvalidKernelParam; | ||||
6309 | |||||
6310 | if (PT->isRecordType()) | ||||
6311 | return RecordKernelParam; | ||||
6312 | |||||
6313 | return ValidKernelParam; | ||||
6314 | } | ||||
6315 | |||||
6316 | static void checkIsValidOpenCLKernelParameter( | ||||
6317 | Sema &S, | ||||
6318 | Declarator &D, | ||||
6319 | ParmVarDecl *Param, | ||||
6320 | llvm::SmallPtrSet<const Type *, 16> &ValidTypes) { | ||||
6321 | QualType PT = Param->getType(); | ||||
6322 | |||||
6323 | // Cache the valid types we encounter to avoid rechecking structs that are | ||||
6324 | // used again | ||||
6325 | if (ValidTypes.count(PT.getTypePtr())) | ||||
6326 | return; | ||||
6327 | |||||
6328 | switch (getOpenCLKernelParameterType(PT)) { | ||||
6329 | case PtrPtrKernelParam: | ||||
6330 | // OpenCL v1.2 s6.9.a: | ||||
6331 | // A kernel function argument cannot be declared as a | ||||
6332 | // pointer to a pointer type. | ||||
6333 | S.Diag(Param->getLocation(), diag::err_opencl_ptrptr_kernel_param); | ||||
6334 | D.setInvalidType(); | ||||
6335 | return; | ||||
6336 | |||||
6337 | // OpenCL v1.2 s6.9.k: | ||||
6338 | // Arguments to kernel functions in a program cannot be declared with the | ||||
6339 | // built-in scalar types bool, half, size_t, ptrdiff_t, intptr_t, and | ||||
6340 | // uintptr_t or a struct and/or union that contain fields declared to be | ||||
6341 | // one of these built-in scalar types. | ||||
6342 | |||||
6343 | case InvalidKernelParam: | ||||
6344 | // OpenCL v1.2 s6.8 n: | ||||
6345 | // A kernel function argument cannot be declared | ||||
6346 | // of event_t type. | ||||
6347 | S.Diag(Param->getLocation(), diag::err_bad_kernel_param_type) << PT; | ||||
6348 | D.setInvalidType(); | ||||
6349 | return; | ||||
6350 | |||||
6351 | case PtrKernelParam: | ||||
6352 | case ValidKernelParam: | ||||
6353 | ValidTypes.insert(PT.getTypePtr()); | ||||
6354 | return; | ||||
6355 | |||||
6356 | case RecordKernelParam: | ||||
6357 | break; | ||||
6358 | } | ||||
6359 | |||||
6360 | // Track nested structs we will inspect | ||||
6361 | SmallVector<const Decl *, 4> VisitStack; | ||||
6362 | |||||
6363 | // Track where we are in the nested structs. Items will migrate from | ||||
6364 | // VisitStack to HistoryStack as we do the DFS for bad field. | ||||
6365 | SmallVector<const FieldDecl *, 4> HistoryStack; | ||||
6366 | HistoryStack.push_back((const FieldDecl *) 0); | ||||
6367 | |||||
6368 | const RecordDecl *PD = PT->castAs<RecordType>()->getDecl(); | ||||
6369 | VisitStack.push_back(PD); | ||||
6370 | |||||
6371 | assert(VisitStack.back() && "First decl null?"); | ||||
6372 | |||||
6373 | do { | ||||
6374 | const Decl *Next = VisitStack.pop_back_val(); | ||||
6375 | if (!Next) { | ||||
6376 | assert(!HistoryStack.empty()); | ||||
6377 | // Found a marker, we have gone up a level | ||||
6378 | if (const FieldDecl *Hist = HistoryStack.pop_back_val()) | ||||
6379 | ValidTypes.insert(Hist->getType().getTypePtr()); | ||||
6380 | |||||
6381 | continue; | ||||
6382 | } | ||||
6383 | |||||
6384 | // Adds everything except the original parameter declaration (which is not a | ||||
6385 | // field itself) to the history stack. | ||||
6386 | const RecordDecl *RD; | ||||
6387 | if (const FieldDecl *Field = dyn_cast<FieldDecl>(Next)) { | ||||
6388 | HistoryStack.push_back(Field); | ||||
6389 | RD = Field->getType()->castAs<RecordType>()->getDecl(); | ||||
6390 | } else { | ||||
6391 | RD = cast<RecordDecl>(Next); | ||||
6392 | } | ||||
6393 | |||||
6394 | // Add a null marker so we know when we've gone back up a level | ||||
6395 | VisitStack.push_back((const Decl *) 0); | ||||
6396 | |||||
6397 | for (RecordDecl::field_iterator I = RD->field_begin(), | ||||
6398 | E = RD->field_end(); I != E; ++I) { | ||||
6399 | const FieldDecl *FD = *I; | ||||
6400 | QualType QT = FD->getType(); | ||||
6401 | |||||
6402 | if (ValidTypes.count(QT.getTypePtr())) | ||||
6403 | continue; | ||||
6404 | |||||
6405 | OpenCLParamType ParamType = getOpenCLKernelParameterType(QT); | ||||
6406 | if (ParamType == ValidKernelParam) | ||||
6407 | continue; | ||||
6408 | |||||
6409 | if (ParamType == RecordKernelParam) { | ||||
6410 | VisitStack.push_back(FD); | ||||
6411 | continue; | ||||
6412 | } | ||||
6413 | |||||
6414 | // OpenCL v1.2 s6.9.p: | ||||
6415 | // Arguments to kernel functions that are declared to be a struct or union | ||||
6416 | // do not allow OpenCL objects to be passed as elements of the struct or | ||||
6417 | // union. | ||||
6418 | if (ParamType == PtrKernelParam || ParamType == PtrPtrKernelParam) { | ||||
6419 | S.Diag(Param->getLocation(), | ||||
6420 | diag::err_record_with_pointers_kernel_param) | ||||
6421 | << PT->isUnionType() | ||||
6422 | << PT; | ||||
6423 | } else { | ||||
6424 | S.Diag(Param->getLocation(), diag::err_bad_kernel_param_type) << PT; | ||||
6425 | } | ||||
6426 | |||||
6427 | S.Diag(PD->getLocation(), diag::note_within_field_of_type) | ||||
6428 | << PD->getDeclName(); | ||||
6429 | |||||
6430 | // We have an error, now let's go back up through history and show where | ||||
6431 | // the offending field came from | ||||
6432 | for (ArrayRef<const FieldDecl *>::const_iterator I = HistoryStack.begin() + 1, | ||||
6433 | E = HistoryStack.end(); I != E; ++I) { | ||||
6434 | const FieldDecl *OuterField = *I; | ||||
6435 | S.Diag(OuterField->getLocation(), diag::note_within_field_of_type) | ||||
6436 | << OuterField->getType(); | ||||
6437 | } | ||||
6438 | |||||
6439 | S.Diag(FD->getLocation(), diag::note_illegal_field_declared_here) | ||||
6440 | << QT->isPointerType() | ||||
6441 | << QT; | ||||
6442 | D.setInvalidType(); | ||||
6443 | return; | ||||
6444 | } | ||||
6445 | } while (!VisitStack.empty()); | ||||
6446 | } | ||||
6447 | |||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6448 | NamedDecl* |
Nick Lewycky | 25af091 | 2011-07-02 02:05:12 +0000 | [diff] [blame] | 6449 | Sema::ActOnFunctionDeclarator(Scope *S, Declarator &D, DeclContext *DC, |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 6450 | TypeSourceInfo *TInfo, LookupResult &Previous, |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 6451 | MultiTemplateParamsArg TemplateParamLists, |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 6452 | bool &AddToScope) { |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 6453 | QualType R = TInfo->getType(); |
6454 | |||||
Zhongxing Xu | 416fcaf | 2009-01-16 01:13:29 +0000 | [diff] [blame] | 6455 | assert(R.getTypePtr()->isFunctionType()); |
6456 | |||||
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 6457 | // TODO: consider using NameInfo for diagnostic. |
6458 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); | ||||
6459 | DeclarationName Name = NameInfo.getName(); | ||||
Kaelyn Uhrain | d7e19ce | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6460 | FunctionDecl::StorageClass SC = getFunctionStorageClass(*this, D); |
Zhongxing Xu | 416fcaf | 2009-01-16 01:13:29 +0000 | [diff] [blame] | 6461 | |
Richard Smith | ec64244 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 6462 | if (DeclSpec::TSCS TSCS = D.getDeclSpec().getThreadStorageClassSpec()) |
6463 | Diag(D.getDeclSpec().getThreadStorageClassSpecLoc(), | ||||
6464 | diag::err_invalid_thread) | ||||
6465 | << DeclSpec::getSpecifierName(TSCS); | ||||
Eli Friedman | 63054b3 | 2009-04-19 20:27:55 +0000 | [diff] [blame] | 6466 | |
Douglas Gregor | 3922ed0 | 2010-12-10 19:28:19 +0000 | [diff] [blame] | 6467 | bool isFriend = false; |
Douglas Gregor | 3922ed0 | 2010-12-10 19:28:19 +0000 | [diff] [blame] | 6468 | FunctionTemplateDecl *FunctionTemplate = 0; |
6469 | bool isExplicitSpecialization = false; | ||||
6470 | bool isFunctionTemplateSpecialization = false; | ||||
Nico Weber | 6b02009 | 2012-06-25 17:21:05 +0000 | [diff] [blame] | 6471 | |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 6472 | bool isDependentClassScopeExplicitSpecialization = false; |
Nico Weber | 6b02009 | 2012-06-25 17:21:05 +0000 | [diff] [blame] | 6473 | bool HasExplicitTemplateArgs = false; |
6474 | TemplateArgumentListInfo TemplateArgs; | ||||
6475 | |||||
Kaelyn Uhrain | d7e19ce | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6476 | bool isVirtualOkay = false; |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6477 | |
Kaelyn Uhrain | d7e19ce | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6478 | FunctionDecl *NewFD = CreateNewFunctionDecl(*this, D, DC, R, TInfo, SC, |
6479 | isVirtualOkay); | ||||
6480 | if (!NewFD) return 0; | ||||
6481 | |||||
Argyrios Kyrtzidis | c14a03d | 2011-11-23 20:27:36 +0000 | [diff] [blame] | 6482 | if (OriginalLexicalContext && OriginalLexicalContext->isObjCContainer()) |
6483 | NewFD->setTopLevelDeclInObjCContainer(); | ||||
6484 | |||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6485 | if (getLangOpts().CPlusPlus) { |
Kaelyn Uhrain | d7e19ce | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6486 | bool isInline = D.getDeclSpec().isInlineSpecified(); |
Douglas Gregor | 3922ed0 | 2010-12-10 19:28:19 +0000 | [diff] [blame] | 6487 | bool isVirtual = D.getDeclSpec().isVirtualSpecified(); |
6488 | bool isExplicit = D.getDeclSpec().isExplicitSpecified(); | ||||
Richard Smith | af1fc7a | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 6489 | bool isConstexpr = D.getDeclSpec().isConstexprSpecified(); |
Kaelyn Uhrain | d7e19ce | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6490 | isFriend = D.getDeclSpec().isFriendSpecified(); |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 6491 | if (isFriend && !isInline && D.isFunctionDefinition()) { |
Abramo Bagnara | b0a2fcc | 2011-03-18 15:21:59 +0000 | [diff] [blame] | 6492 | // C++ [class.friend]p5 |
6493 | // A function can be defined in a friend declaration of a | ||||
6494 | // class . . . . Such a function is implicitly inline. | ||||
6495 | NewFD->setImplicitlyInline(); | ||||
6496 | } | ||||
6497 | |||||
John McCall | e402e72 | 2012-09-25 07:32:39 +0000 | [diff] [blame] | 6498 | // If this is a method defined in an __interface, and is not a constructor |
6499 | // or an overloaded operator, then set the pure flag (isVirtual will already | ||||
6500 | // return true). | ||||
6501 | if (const CXXRecordDecl *Parent = | ||||
6502 | dyn_cast<CXXRecordDecl>(NewFD->getDeclContext())) { | ||||
6503 | if (Parent->isInterface() && cast<CXXMethodDecl>(NewFD)->isUserProvided()) | ||||
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 6504 | NewFD->setPure(true); |
6505 | } | ||||
6506 | |||||
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 6507 | SetNestedNameSpecifier(NewFD, D); |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 6508 | isExplicitSpecialization = false; |
6509 | isFunctionTemplateSpecialization = false; | ||||
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 6510 | if (D.isInvalidType()) |
6511 | NewFD->setInvalidDecl(); | ||||
6512 | |||||
6513 | // Set the lexical context. If the declarator has a C++ | ||||
6514 | // scope specifier, or is the object of a friend declaration, the | ||||
6515 | // lexical context will be different from the semantic context. | ||||
6516 | NewFD->setLexicalDeclContext(CurContext); | ||||
Douglas Gregor | 45fa560 | 2011-11-07 20:56:01 +0000 | [diff] [blame] | 6517 | |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 6518 | // Match up the template parameter lists with the scope specifier, then |
6519 | // determine whether we have a template or a template specialization. | ||||
6520 | bool Invalid = false; | ||||
Robert Wilhelm | 1169e2f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 6521 | if (TemplateParameterList *TemplateParams = |
6522 | MatchTemplateParametersToScopeSpecifier( | ||||
6523 | D.getDeclSpec().getLocStart(), D.getIdentifierLoc(), | ||||
6524 | D.getCXXScopeSpec(), TemplateParamLists, isFriend, | ||||
6525 | isExplicitSpecialization, Invalid)) { | ||||
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6526 | if (TemplateParams->size() > 0) { |
6527 | // This is a function template | ||||
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 6528 | |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6529 | // Check that we can declare a template here. |
6530 | if (CheckTemplateDeclScope(S, TemplateParams)) | ||||
6531 | return 0; | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6532 | |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6533 | // A destructor cannot be a template. |
6534 | if (Name.getNameKind() == DeclarationName::CXXDestructorName) { | ||||
6535 | Diag(NewFD->getLocation(), diag::err_destructor_template); | ||||
6536 | return 0; | ||||
John McCall | 5fd378b | 2010-03-24 08:27:58 +0000 | [diff] [blame] | 6537 | } |
Douglas Gregor | 2060650 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 6538 | |
6539 | // 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] | 6540 | // rebuilding some of the types used within the template parameter list, |
Douglas Gregor | 2060650 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 6541 | // now that we know what the current instantiation is. |
6542 | if (DC->isDependentContext()) { | ||||
6543 | ContextRAII SavedContext(*this, DC); | ||||
6544 | if (RebuildTemplateParamsInCurrentInstantiation(TemplateParams)) | ||||
6545 | Invalid = true; | ||||
6546 | } | ||||
6547 | |||||
John McCall | 5fd378b | 2010-03-24 08:27:58 +0000 | [diff] [blame] | 6548 | |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6549 | FunctionTemplate = FunctionTemplateDecl::Create(Context, DC, |
6550 | NewFD->getLocation(), | ||||
6551 | Name, TemplateParams, | ||||
6552 | NewFD); | ||||
6553 | FunctionTemplate->setLexicalDeclContext(CurContext); | ||||
6554 | NewFD->setDescribedFunctionTemplate(FunctionTemplate); | ||||
6555 | |||||
6556 | // For source fidelity, store the other template param lists. | ||||
6557 | if (TemplateParamLists.size() > 1) { | ||||
6558 | NewFD->setTemplateParameterListsInfo(Context, | ||||
6559 | TemplateParamLists.size() - 1, | ||||
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 6560 | TemplateParamLists.data()); |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6561 | } |
6562 | } else { | ||||
6563 | // This is a function template specialization. | ||||
6564 | isFunctionTemplateSpecialization = true; | ||||
6565 | // For source fidelity, store all the template param lists. | ||||
6566 | NewFD->setTemplateParameterListsInfo(Context, | ||||
6567 | TemplateParamLists.size(), | ||||
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 6568 | TemplateParamLists.data()); |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6569 | |
6570 | // C++0x [temp.expl.spec]p20 forbids "template<> friend void foo(int);". | ||||
6571 | if (isFriend) { | ||||
6572 | // We want to remove the "template<>", found here. | ||||
6573 | SourceRange RemoveRange = TemplateParams->getSourceRange(); | ||||
6574 | |||||
6575 | // If we remove the template<> and the name is not a | ||||
6576 | // template-id, we're actually silently creating a problem: | ||||
6577 | // the friend declaration will refer to an untemplated decl, | ||||
6578 | // and clearly the user wants a template specialization. So | ||||
6579 | // we need to insert '<>' after the name. | ||||
6580 | SourceLocation InsertLoc; | ||||
6581 | if (D.getName().getKind() != UnqualifiedId::IK_TemplateId) { | ||||
6582 | InsertLoc = D.getName().getSourceRange().getEnd(); | ||||
6583 | InsertLoc = PP.getLocForEndOfToken(InsertLoc); | ||||
6584 | } | ||||
6585 | |||||
6586 | Diag(D.getIdentifierLoc(), diag::err_template_spec_decl_friend) | ||||
6587 | << Name << RemoveRange | ||||
6588 | << FixItHint::CreateRemoval(RemoveRange) | ||||
6589 | << FixItHint::CreateInsertion(InsertLoc, "<>"); | ||||
6590 | } | ||||
6591 | } | ||||
6592 | } | ||||
6593 | else { | ||||
6594 | // All template param lists were matched against the scope specifier: | ||||
6595 | // this is NOT (an explicit specialization of) a template. | ||||
6596 | if (TemplateParamLists.size() > 0) | ||||
6597 | // For source fidelity, store all the template param lists. | ||||
6598 | NewFD->setTemplateParameterListsInfo(Context, | ||||
6599 | TemplateParamLists.size(), | ||||
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 6600 | TemplateParamLists.data()); |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 6601 | } |
6602 | |||||
6603 | if (Invalid) { | ||||
6604 | NewFD->setInvalidDecl(); | ||||
6605 | if (FunctionTemplate) | ||||
6606 | FunctionTemplate->setInvalidDecl(); | ||||
6607 | } | ||||
Kaelyn Uhrain | d7e19ce | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6608 | |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 6609 | // C++ [dcl.fct.spec]p5: |
6610 | // The virtual specifier shall only be used in declarations of | ||||
6611 | // nonstatic class member functions that appear within a | ||||
6612 | // member-specification of a class declaration; see 10.3. | ||||
6613 | // | ||||
6614 | if (isVirtual && !NewFD->isInvalidDecl()) { | ||||
6615 | if (!isVirtualOkay) { | ||||
6616 | Diag(D.getDeclSpec().getVirtualSpecLoc(), | ||||
6617 | diag::err_virtual_non_function); | ||||
6618 | } else if (!CurContext->isRecord()) { | ||||
6619 | // 'virtual' was specified outside of the class. | ||||
Anders Carlsson | f1602a5 | 2011-01-22 14:43:56 +0000 | [diff] [blame] | 6620 | Diag(D.getDeclSpec().getVirtualSpecLoc(), |
6621 | diag::err_virtual_out_of_class) | ||||
6622 | << FixItHint::CreateRemoval(D.getDeclSpec().getVirtualSpecLoc()); | ||||
6623 | } else if (NewFD->getDescribedFunctionTemplate()) { | ||||
6624 | // C++ [temp.mem]p3: | ||||
6625 | // A member function template shall not be virtual. | ||||
6626 | Diag(D.getDeclSpec().getVirtualSpecLoc(), | ||||
6627 | diag::err_virtual_member_function_template) | ||||
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 6628 | << FixItHint::CreateRemoval(D.getDeclSpec().getVirtualSpecLoc()); |
6629 | } else { | ||||
6630 | // Okay: Add virtual to the method. | ||||
6631 | NewFD->setVirtualAsWritten(true); | ||||
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 6632 | } |
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 6633 | |
6634 | if (getLangOpts().CPlusPlus1y && | ||||
6635 | NewFD->getResultType()->isUndeducedType()) | ||||
6636 | Diag(D.getDeclSpec().getVirtualSpecLoc(), diag::err_auto_fn_virtual); | ||||
Douglas Gregor | c5c903a | 2009-06-24 00:23:40 +0000 | [diff] [blame] | 6637 | } |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 6638 | |
Richard Smith | 37e849a | 2013-08-14 20:16:31 +0000 | [diff] [blame] | 6639 | if (getLangOpts().CPlusPlus1y && NewFD->isDependentContext() && |
6640 | NewFD->getResultType()->isUndeducedType()) { | ||||
6641 | // If the function template is referenced directly (for instance, as a | ||||
6642 | // member of the current instantiation), pretend it has a dependent type. | ||||
6643 | // This is not really justified by the standard, but is the only sane | ||||
6644 | // thing to do. | ||||
6645 | const FunctionProtoType *FPT = | ||||
6646 | NewFD->getType()->castAs<FunctionProtoType>(); | ||||
6647 | QualType Result = SubstAutoType(FPT->getResultType(), | ||||
6648 | Context.DependentTy); | ||||
6649 | NewFD->setType(Context.getFunctionType(Result, FPT->getArgTypes(), | ||||
6650 | FPT->getExtProtoInfo())); | ||||
6651 | } | ||||
6652 | |||||
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 6653 | // C++ [dcl.fct.spec]p3: |
David Blaikie | d662a79 | 2011-10-19 22:56:21 +0000 | [diff] [blame] | 6654 | // The inline specifier shall not appear on a block scope function |
6655 | // declaration. | ||||
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 6656 | if (isInline && !NewFD->isInvalidDecl()) { |
6657 | if (CurContext->isFunctionOrMethod()) { | ||||
6658 | // 'inline' is not allowed on block scope function declaration. | ||||
6659 | Diag(D.getDeclSpec().getInlineSpecLoc(), | ||||
6660 | diag::err_inline_declaration_block_scope) << Name | ||||
6661 | << FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc()); | ||||
6662 | } | ||||
6663 | } | ||||
Kaelyn Uhrain | d7e19ce | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6664 | |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 6665 | // C++ [dcl.fct.spec]p6: |
6666 | // The explicit specifier shall be used only in the declaration of a | ||||
David Blaikie | d662a79 | 2011-10-19 22:56:21 +0000 | [diff] [blame] | 6667 | // constructor or conversion function within its class definition; |
6668 | // see 12.3.1 and 12.3.2. | ||||
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 6669 | if (isExplicit && !NewFD->isInvalidDecl()) { |
6670 | if (!CurContext->isRecord()) { | ||||
6671 | // 'explicit' was specified outside of the class. | ||||
6672 | Diag(D.getDeclSpec().getExplicitSpecLoc(), | ||||
6673 | diag::err_explicit_out_of_class) | ||||
6674 | << FixItHint::CreateRemoval(D.getDeclSpec().getExplicitSpecLoc()); | ||||
6675 | } else if (!isa<CXXConstructorDecl>(NewFD) && | ||||
6676 | !isa<CXXConversionDecl>(NewFD)) { | ||||
6677 | // 'explicit' was specified on a function that wasn't a constructor | ||||
6678 | // or conversion function. | ||||
6679 | Diag(D.getDeclSpec().getExplicitSpecLoc(), | ||||
6680 | diag::err_explicit_non_ctor_or_conv_function) | ||||
6681 | << FixItHint::CreateRemoval(D.getDeclSpec().getExplicitSpecLoc()); | ||||
6682 | } | ||||
6683 | } | ||||
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 6684 | |
Richard Smith | af1fc7a | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 6685 | if (isConstexpr) { |
Richard Smith | 21c8fa8 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 6686 | // C++11 [dcl.constexpr]p2: constexpr functions and constexpr constructors |
Richard Smith | af1fc7a | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 6687 | // are implicitly inline. |
6688 | NewFD->setImplicitlyInline(); | ||||
6689 | |||||
Richard Smith | 21c8fa8 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 6690 | // C++11 [dcl.constexpr]p3: functions declared constexpr are required to |
Richard Smith | af1fc7a | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 6691 | // be either constructors or to return a literal type. Therefore, |
6692 | // destructors cannot be declared constexpr. | ||||
6693 | if (isa<CXXDestructorDecl>(NewFD)) | ||||
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 6694 | Diag(D.getDeclSpec().getConstexprSpecLoc(), diag::err_constexpr_dtor); |
Richard Smith | af1fc7a | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 6695 | } |
6696 | |||||
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 6697 | // If __module_private__ was specified, mark the function accordingly. |
6698 | if (D.getDeclSpec().isModulePrivateSpecified()) { | ||||
Douglas Gregor | d023aec | 2011-09-09 20:53:38 +0000 | [diff] [blame] | 6699 | if (isFunctionTemplateSpecialization) { |
6700 | SourceLocation ModulePrivateLoc | ||||
6701 | = D.getDeclSpec().getModulePrivateSpecLoc(); | ||||
6702 | Diag(ModulePrivateLoc, diag::err_module_private_specialization) | ||||
6703 | << 0 | ||||
6704 | << FixItHint::CreateRemoval(ModulePrivateLoc); | ||||
6705 | } else { | ||||
6706 | NewFD->setModulePrivate(); | ||||
6707 | if (FunctionTemplate) | ||||
6708 | FunctionTemplate->setModulePrivate(); | ||||
6709 | } | ||||
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 6710 | } |
Richard Smith | af1fc7a | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 6711 | |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 6712 | if (isFriend) { |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 6713 | if (FunctionTemplate) { |
Richard Smith | 22050f2 | 2013-07-17 23:53:16 +0000 | [diff] [blame] | 6714 | FunctionTemplate->setObjectOfFriendDecl(); |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 6715 | FunctionTemplate->setAccess(AS_public); |
6716 | } | ||||
Richard Smith | 22050f2 | 2013-07-17 23:53:16 +0000 | [diff] [blame] | 6717 | NewFD->setObjectOfFriendDecl(); |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 6718 | NewFD->setAccess(AS_public); |
6719 | } | ||||
6720 | |||||
Douglas Gregor | 45fa560 | 2011-11-07 20:56:01 +0000 | [diff] [blame] | 6721 | // If a function is defined as defaulted or deleted, mark it as such now. |
6722 | switch (D.getFunctionDefinitionKind()) { | ||||
6723 | case FDK_Declaration: | ||||
6724 | case FDK_Definition: | ||||
6725 | break; | ||||
6726 | |||||
6727 | case FDK_Defaulted: | ||||
6728 | NewFD->setDefaulted(); | ||||
6729 | break; | ||||
6730 | |||||
6731 | case FDK_Deleted: | ||||
6732 | NewFD->setDeletedAsWritten(); | ||||
6733 | break; | ||||
6734 | } | ||||
6735 | |||||
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 6736 | if (isa<CXXMethodDecl>(NewFD) && DC == CurContext && |
6737 | D.isFunctionDefinition()) { | ||||
Douglas Gregor | 45fa560 | 2011-11-07 20:56:01 +0000 | [diff] [blame] | 6738 | // C++ [class.mfct]p2: |
6739 | // A member function may be defined (8.4) in its class definition, in | ||||
6740 | // which case it is an inline member function (7.1.2) | ||||
John McCall | bfdcdc8 | 2010-12-15 04:00:32 +0000 | [diff] [blame] | 6741 | NewFD->setImplicitlyInline(); |
6742 | } | ||||
6743 | |||||
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 6744 | if (SC == SC_Static && isa<CXXMethodDecl>(NewFD) && |
6745 | !CurContext->isRecord()) { | ||||
6746 | // C++ [class.static]p1: | ||||
6747 | // A data or function member of a class may be declared static | ||||
6748 | // in a class definition, in which case it is a static member of | ||||
6749 | // the class. | ||||
6750 | |||||
6751 | // Complain about the 'static' specifier if it's on an out-of-line | ||||
6752 | // member function definition. | ||||
6753 | Diag(D.getDeclSpec().getStorageClassSpecLoc(), | ||||
6754 | diag::err_static_out_of_line) | ||||
6755 | << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc()); | ||||
6756 | } | ||||
Richard Smith | 444d384 | 2012-10-20 08:26:51 +0000 | [diff] [blame] | 6757 | |
6758 | // C++11 [except.spec]p15: | ||||
6759 | // A deallocation function with no exception-specification is treated | ||||
6760 | // as if it were specified with noexcept(true). | ||||
6761 | const FunctionProtoType *FPT = R->getAs<FunctionProtoType>(); | ||||
6762 | if ((Name.getCXXOverloadedOperator() == OO_Delete || | ||||
6763 | Name.getCXXOverloadedOperator() == OO_Array_Delete) && | ||||
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6764 | getLangOpts().CPlusPlus11 && FPT && !FPT->hasExceptionSpec()) { |
Richard Smith | 444d384 | 2012-10-20 08:26:51 +0000 | [diff] [blame] | 6765 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
6766 | EPI.ExceptionSpecType = EST_BasicNoexcept; | ||||
6767 | NewFD->setType(Context.getFunctionType(FPT->getResultType(), | ||||
Reid Kleckner | 0567a79 | 2013-06-10 20:51:09 +0000 | [diff] [blame] | 6768 | FPT->getArgTypes(), EPI)); |
Richard Smith | 444d384 | 2012-10-20 08:26:51 +0000 | [diff] [blame] | 6769 | } |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 6770 | } |
Kaelyn Uhrain | d7e19ce | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6771 | |
6772 | // Filter out previous declarations that don't match the scope. | ||||
Rafael Espindola | 2d1b096 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 6773 | FilterLookupForScope(Previous, DC, S, shouldConsiderLinkage(NewFD), |
Kaelyn Uhrain | d7e19ce | 2011-10-11 00:28:49 +0000 | [diff] [blame] | 6774 | isExplicitSpecialization || |
6775 | isFunctionTemplateSpecialization); | ||||
Richard Smith | dd9459f | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 6776 | |
Zhongxing Xu | 416fcaf | 2009-01-16 01:13:29 +0000 | [diff] [blame] | 6777 | // Handle GNU asm-label extension (encoded as an attribute). |
6778 | if (Expr *E = (Expr*) D.getAsmLabel()) { | ||||
6779 | // The parser guarantees this is a string. | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6780 | StringLiteral *SE = cast<StringLiteral>(E); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 6781 | NewFD->addAttr(::new (Context) AsmLabelAttr(SE->getStrTokenLoc(0), Context, |
6782 | SE->getString())); | ||||
David Chisnall | 5f3c163 | 2012-02-18 16:12:34 +0000 | [diff] [blame] | 6783 | } else if (!ExtnameUndeclaredIdentifiers.empty()) { |
6784 | llvm::DenseMap<IdentifierInfo*,AsmLabelAttr*>::iterator I = | ||||
6785 | ExtnameUndeclaredIdentifiers.find(NewFD->getIdentifier()); | ||||
6786 | if (I != ExtnameUndeclaredIdentifiers.end()) { | ||||
6787 | NewFD->addAttr(I->second); | ||||
6788 | ExtnameUndeclaredIdentifiers.erase(I); | ||||
6789 | } | ||||
Zhongxing Xu | 416fcaf | 2009-01-16 01:13:29 +0000 | [diff] [blame] | 6790 | } |
6791 | |||||
Chris Lattner | 2dbd285 | 2009-04-25 06:12:16 +0000 | [diff] [blame] | 6792 | // Copy the parameter declarations from the declarator D to the function |
6793 | // declaration NewFD, if they are available. First scavenge them into Params. | ||||
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6794 | SmallVector<ParmVarDecl*, 16> Params; |
Abramo Bagnara | 723df24 | 2010-12-14 22:11:44 +0000 | [diff] [blame] | 6795 | if (D.isFunctionDeclarator()) { |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 6796 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo(); |
Zhongxing Xu | 416fcaf | 2009-01-16 01:13:29 +0000 | [diff] [blame] | 6797 | |
Zhongxing Xu | 416fcaf | 2009-01-16 01:13:29 +0000 | [diff] [blame] | 6798 | // Check for C99 6.7.5.3p10 - foo(void) is a non-varargs |
6799 | // function that takes no arguments, not a function that takes a | ||||
6800 | // single void argument. | ||||
6801 | // We let through "const void" here because Sema::GetTypeForDeclarator | ||||
6802 | // already checks for that case. | ||||
6803 | if (FTI.NumArgs == 1 && !FTI.isVariadic && FTI.ArgInfo[0].Ident == 0 && | ||||
6804 | FTI.ArgInfo[0].Param && | ||||
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6805 | cast<ParmVarDecl>(FTI.ArgInfo[0].Param)->getType()->isVoidType()) { |
Chris Lattner | 2dbd285 | 2009-04-25 06:12:16 +0000 | [diff] [blame] | 6806 | // Empty arg list, don't push any params. |
Eli Friedman | 7c3c6bc | 2012-09-20 01:40:23 +0000 | [diff] [blame] | 6807 | checkVoidParamDecl(cast<ParmVarDecl>(FTI.ArgInfo[0].Param)); |
Zhongxing Xu | 416fcaf | 2009-01-16 01:13:29 +0000 | [diff] [blame] | 6808 | } else if (FTI.NumArgs > 0 && FTI.ArgInfo[0].Param != 0) { |
Argyrios Kyrtzidis | 9bedef6 | 2009-07-14 03:18:53 +0000 | [diff] [blame] | 6809 | for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6810 | ParmVarDecl *Param = cast<ParmVarDecl>(FTI.ArgInfo[i].Param); |
Argyrios Kyrtzidis | 9bedef6 | 2009-07-14 03:18:53 +0000 | [diff] [blame] | 6811 | assert(Param->getDeclContext() != NewFD && "Was set before ?"); |
6812 | Param->setDeclContext(NewFD); | ||||
6813 | Params.push_back(Param); | ||||
John McCall | f19de1c | 2010-04-14 01:27:20 +0000 | [diff] [blame] | 6814 | |
6815 | if (Param->isInvalidDecl()) | ||||
6816 | NewFD->setInvalidDecl(); | ||||
Argyrios Kyrtzidis | 9bedef6 | 2009-07-14 03:18:53 +0000 | [diff] [blame] | 6817 | } |
Zhongxing Xu | 416fcaf | 2009-01-16 01:13:29 +0000 | [diff] [blame] | 6818 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6819 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6820 | } else if (const FunctionProtoType *FT = R->getAs<FunctionProtoType>()) { |
Chris Lattner | 1ad9b28 | 2009-04-25 06:03:53 +0000 | [diff] [blame] | 6821 | // 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] | 6822 | // following example, we'll need to synthesize (unnamed) |
6823 | // parameters for use in the declaration. | ||||
6824 | // | ||||
6825 | // @code | ||||
6826 | // typedef void fn(int); | ||||
6827 | // fn f; | ||||
6828 | // @endcode | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6829 | |
Chris Lattner | 1ad9b28 | 2009-04-25 06:03:53 +0000 | [diff] [blame] | 6830 | // Synthesize a parameter for each argument type. |
Chris Lattner | 1ad9b28 | 2009-04-25 06:03:53 +0000 | [diff] [blame] | 6831 | for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(), |
6832 | AE = FT->arg_type_end(); AI != AE; ++AI) { | ||||
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 6833 | ParmVarDecl *Param = |
6834 | BuildParmVarDeclForTypedef(NewFD, D.getIdentifierLoc(), *AI); | ||||
John McCall | fb44de9 | 2011-05-01 22:35:37 +0000 | [diff] [blame] | 6835 | Param->setScopeInfo(0, Params.size()); |
Chris Lattner | 1ad9b28 | 2009-04-25 06:03:53 +0000 | [diff] [blame] | 6836 | Params.push_back(Param); |
Zhongxing Xu | 416fcaf | 2009-01-16 01:13:29 +0000 | [diff] [blame] | 6837 | } |
Chris Lattner | 84bb944 | 2009-04-25 18:38:18 +0000 | [diff] [blame] | 6838 | } else { |
6839 | assert(R->isFunctionNoProtoType() && NewFD->getNumParams() == 0 && | ||||
6840 | "Should not need args for typedef of non-prototype fn"); | ||||
Zhongxing Xu | 416fcaf | 2009-01-16 01:13:29 +0000 | [diff] [blame] | 6841 | } |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 6842 | |
Chris Lattner | 2dbd285 | 2009-04-25 06:12:16 +0000 | [diff] [blame] | 6843 | // Finally, we know we have the right number of parameters, install them. |
David Blaikie | 4278c65 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 6844 | NewFD->setParams(Params); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6845 | |
James Molloy | 16f1f71 | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 6846 | // Find all anonymous symbols defined during the declaration of this function |
6847 | // and add to NewFD. This lets us track decls such 'enum Y' in: | ||||
6848 | // | ||||
6849 | // void f(enum Y {AA} x) {} | ||||
6850 | // | ||||
6851 | // which would otherwise incorrectly end up in the translation unit scope. | ||||
6852 | NewFD->setDeclsInPrototypeScope(DeclsInPrototypeScope); | ||||
6853 | DeclsInPrototypeScope.clear(); | ||||
6854 | |||||
Richard Smith | 7586a6e | 2013-01-30 05:45:05 +0000 | [diff] [blame] | 6855 | if (D.getDeclSpec().isNoreturnSpecified()) |
6856 | NewFD->addAttr( | ||||
6857 | ::new(Context) C11NoReturnAttr(D.getDeclSpec().getNoreturnSpecLoc(), | ||||
6858 | Context)); | ||||
6859 | |||||
Peter Collingbourne | c80e811 | 2011-01-21 02:08:54 +0000 | [diff] [blame] | 6860 | // Process the non-inheritable attributes on this declaration. |
6861 | ProcessDeclAttributes(S, NewFD, D, | ||||
6862 | /*NonInheritable=*/true, /*Inheritable=*/false); | ||||
6863 | |||||
Richard Smith | b03a9df | 2012-03-13 05:56:40 +0000 | [diff] [blame] | 6864 | // Functions returning a variably modified type violate C99 6.7.5.2p2 |
6865 | // because all functions have linkage. | ||||
6866 | if (!NewFD->isInvalidDecl() && | ||||
6867 | NewFD->getResultType()->isVariablyModifiedType()) { | ||||
6868 | Diag(NewFD->getLocation(), diag::err_vm_func_decl); | ||||
6869 | NewFD->setInvalidDecl(); | ||||
6870 | } | ||||
6871 | |||||
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 6872 | // Handle attributes. |
6873 | ProcessDeclAttributes(S, NewFD, D, | ||||
6874 | /*NonInheritable=*/false, /*Inheritable=*/true); | ||||
6875 | |||||
Kaelyn Uhrain | 51ceb7b | 2012-11-12 23:48:05 +0000 | [diff] [blame] | 6876 | QualType RetType = NewFD->getResultType(); |
6877 | const CXXRecordDecl *Ret = RetType->isRecordType() ? | ||||
6878 | RetType->getAsCXXRecordDecl() : RetType->getPointeeCXXRecordDecl(); | ||||
6879 | if (!NewFD->isInvalidDecl() && !NewFD->hasAttr<WarnUnusedResultAttr>() && | ||||
6880 | Ret && Ret->hasAttr<WarnUnusedResultAttr>()) { | ||||
Kaelyn Uhrain | 97c81bf | 2012-11-13 21:23:31 +0000 | [diff] [blame] | 6881 | const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(NewFD); |
6882 | if (!(MD && MD->getCorrespondingMethodInClass(Ret, true))) { | ||||
6883 | NewFD->addAttr(new (Context) WarnUnusedResultAttr(SourceRange(), | ||||
6884 | Context)); | ||||
6885 | } | ||||
Kaelyn Uhrain | 51ceb7b | 2012-11-12 23:48:05 +0000 | [diff] [blame] | 6886 | } |
6887 | |||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6888 | if (!getLangOpts().CPlusPlus) { |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 6889 | // Perform semantic checking on the function declaration. |
Douglas Gregor | 89b9f10 | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 6890 | bool isExplicitSpecialization=false; |
David Majnemer | c371db6 | 2013-07-06 02:13:46 +0000 | [diff] [blame] | 6891 | if (!NewFD->isInvalidDecl() && NewFD->isMain()) |
6892 | CheckMain(NewFD, D.getDeclSpec()); | ||||
6893 | |||||
6894 | if (!NewFD->isInvalidDecl()) | ||||
Richard Smith | b03a9df | 2012-03-13 05:56:40 +0000 | [diff] [blame] | 6895 | D.setRedeclaration(CheckFunctionDeclaration(S, NewFD, Previous, |
6896 | isExplicitSpecialization)); | ||||
Fariborz Jahanian | 37c765a | 2012-09-05 17:52:12 +0000 | [diff] [blame] | 6897 | else if (!Previous.empty()) |
Richard Smith | dd9459f | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 6898 | // Make graceful recovery from an invalid redeclaration. |
6899 | D.setRedeclaration(true); | ||||
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 6900 | assert((NewFD->isInvalidDecl() || !D.isRedeclaration() || |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 6901 | Previous.getResultKind() != LookupResult::FoundOverloaded) && |
6902 | "previous declaration set still overloaded"); | ||||
6903 | } else { | ||||
6904 | // If the declarator is a template-id, translate the parser's template | ||||
6905 | // argument list into our AST format. | ||||
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 6906 | if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) { |
6907 | TemplateIdAnnotation *TemplateId = D.getName().TemplateId; | ||||
6908 | TemplateArgs.setLAngleLoc(TemplateId->LAngleLoc); | ||||
6909 | TemplateArgs.setRAngleLoc(TemplateId->RAngleLoc); | ||||
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 6910 | ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(), |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 6911 | TemplateId->NumArgs); |
6912 | translateTemplateArguments(TemplateArgsPtr, | ||||
6913 | TemplateArgs); | ||||
Douglas Gregor | 0b60d9e | 2009-09-25 23:53:26 +0000 | [diff] [blame] | 6914 | |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 6915 | HasExplicitTemplateArgs = true; |
Douglas Gregor | 0b60d9e | 2009-09-25 23:53:26 +0000 | [diff] [blame] | 6916 | |
Douglas Gregor | 89b9f10 | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 6917 | if (NewFD->isInvalidDecl()) { |
6918 | HasExplicitTemplateArgs = false; | ||||
6919 | } else if (FunctionTemplate) { | ||||
Douglas Gregor | 5505c72 | 2011-01-24 18:54:39 +0000 | [diff] [blame] | 6920 | // Function template with explicit template arguments. |
6921 | Diag(D.getIdentifierLoc(), diag::err_function_template_partial_spec) | ||||
6922 | << SourceRange(TemplateId->LAngleLoc, TemplateId->RAngleLoc); | ||||
6923 | |||||
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 6924 | HasExplicitTemplateArgs = false; |
6925 | } else if (!isFunctionTemplateSpecialization && | ||||
6926 | !D.getDeclSpec().isFriendSpecified()) { | ||||
6927 | // We have encountered something that the user meant to be a | ||||
6928 | // specialization (because it has explicitly-specified template | ||||
6929 | // arguments) but that was not introduced with a "template<>" (or had | ||||
6930 | // too few of them). | ||||
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 6931 | // FIXME: Differentiate between attempts for explicit instantiations |
6932 | // (starting with "template") and the rest. | ||||
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 6933 | Diag(D.getIdentifierLoc(), diag::err_template_spec_needs_header) |
6934 | << SourceRange(TemplateId->LAngleLoc, TemplateId->RAngleLoc) | ||||
6935 | << FixItHint::CreateInsertion( | ||||
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 6936 | D.getDeclSpec().getLocStart(), |
David Blaikie | d662a79 | 2011-10-19 22:56:21 +0000 | [diff] [blame] | 6937 | "template<> "); |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 6938 | isFunctionTemplateSpecialization = true; |
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 6939 | } else { |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 6940 | // "friend void foo<>(int);" is an implicit specialization decl. |
6941 | isFunctionTemplateSpecialization = true; | ||||
Francois Pichet | c71d8eb | 2010-10-01 21:19:28 +0000 | [diff] [blame] | 6942 | } |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 6943 | } else if (isFriend && isFunctionTemplateSpecialization) { |
6944 | // This combination is only possible in a recovery case; the user | ||||
6945 | // wrote something like: | ||||
6946 | // template <> friend void foo(int); | ||||
6947 | // which we're recovering from as if the user had written: | ||||
6948 | // friend void foo<>(int); | ||||
6949 | // Go ahead and fake up a template id. | ||||
6950 | HasExplicitTemplateArgs = true; | ||||
6951 | TemplateArgs.setLAngleLoc(D.getIdentifierLoc()); | ||||
6952 | TemplateArgs.setRAngleLoc(D.getIdentifierLoc()); | ||||
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 6953 | } |
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 6954 | |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 6955 | // If it's a friend (and only if it's a friend), it's possible |
6956 | // that either the specialized function type or the specialized | ||||
6957 | // template is dependent, and therefore matching will fail. In | ||||
6958 | // this case, don't check the specialization yet. | ||||
Douglas Gregor | 33ab0da | 2011-10-09 20:59:17 +0000 | [diff] [blame] | 6959 | bool InstantiationDependent = false; |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 6960 | if (isFunctionTemplateSpecialization && isFriend && |
Douglas Gregor | 33ab0da | 2011-10-09 20:59:17 +0000 | [diff] [blame] | 6961 | (NewFD->getType()->isDependentType() || DC->isDependentContext() || |
6962 | TemplateSpecializationType::anyDependentTemplateArguments( | ||||
6963 | TemplateArgs.getArgumentArray(), TemplateArgs.size(), | ||||
6964 | InstantiationDependent))) { | ||||
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 6965 | assert(HasExplicitTemplateArgs && |
6966 | "friend function specialization without template args"); | ||||
6967 | if (CheckDependentFunctionTemplateSpecialization(NewFD, TemplateArgs, | ||||
6968 | Previous)) | ||||
6969 | NewFD->setInvalidDecl(); | ||||
6970 | } else if (isFunctionTemplateSpecialization) { | ||||
Douglas Gregor | eef7ac5 | 2011-03-16 19:27:09 +0000 | [diff] [blame] | 6971 | if (CurContext->isDependentContext() && CurContext->isRecord() |
Francois Pichet | ab01add | 2011-06-03 13:59:45 +0000 | [diff] [blame] | 6972 | && !isFriend) { |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 6973 | isDependentClassScopeExplicitSpecialization = true; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6974 | Diag(NewFD->getLocation(), getLangOpts().MicrosoftExt ? |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 6975 | diag::ext_function_specialization_in_class : |
6976 | diag::err_function_specialization_in_class) | ||||
Douglas Gregor | eef7ac5 | 2011-03-16 19:27:09 +0000 | [diff] [blame] | 6977 | << NewFD->getDeclName(); |
Douglas Gregor | eef7ac5 | 2011-03-16 19:27:09 +0000 | [diff] [blame] | 6978 | } else if (CheckFunctionTemplateSpecialization(NewFD, |
6979 | (HasExplicitTemplateArgs ? &TemplateArgs : 0), | ||||
6980 | Previous)) | ||||
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 6981 | NewFD->setInvalidDecl(); |
Douglas Gregor | e885e18 | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 6982 | |
6983 | // C++ [dcl.stc]p1: | ||||
6984 | // A storage-class-specifier shall not be specified in an explicit | ||||
6985 | // specialization (14.7.3) | ||||
Richard Trieu | 62ab010 | 2013-05-16 02:14:08 +0000 | [diff] [blame] | 6986 | FunctionTemplateSpecializationInfo *Info = |
6987 | NewFD->getTemplateSpecializationInfo(); | ||||
6988 | if (Info && SC != SC_None) { | ||||
6989 | if (SC != Info->getTemplate()->getTemplatedDecl()->getStorageClass()) | ||||
Douglas Gregor | 0f9dc86 | 2011-06-17 05:09:08 +0000 | [diff] [blame] | 6990 | Diag(NewFD->getLocation(), |
6991 | diag::err_explicit_specialization_inconsistent_storage_class) | ||||
6992 | << SC | ||||
6993 | << FixItHint::CreateRemoval( | ||||
6994 | D.getDeclSpec().getStorageClassSpecLoc()); | ||||
6995 | |||||
6996 | else | ||||
6997 | Diag(NewFD->getLocation(), | ||||
6998 | diag::ext_explicit_specialization_storage_class) | ||||
6999 | << FixItHint::CreateRemoval( | ||||
7000 | D.getDeclSpec().getStorageClassSpecLoc()); | ||||
Douglas Gregor | e885e18 | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 7001 | } |
7002 | |||||
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7003 | } else if (isExplicitSpecialization && isa<CXXMethodDecl>(NewFD)) { |
7004 | if (CheckMemberSpecialization(NewFD, Previous)) | ||||
7005 | NewFD->setInvalidDecl(); | ||||
7006 | } | ||||
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 7007 | |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7008 | // Perform semantic checking on the function declaration. |
David Blaikie | 14068e8 | 2011-09-08 06:33:04 +0000 | [diff] [blame] | 7009 | if (!isDependentClassScopeExplicitSpecialization) { |
David Majnemer | c371db6 | 2013-07-06 02:13:46 +0000 | [diff] [blame] | 7010 | if (!NewFD->isInvalidDecl() && NewFD->isMain()) |
7011 | CheckMain(NewFD, D.getDeclSpec()); | ||||
7012 | |||||
David Blaikie | 14068e8 | 2011-09-08 06:33:04 +0000 | [diff] [blame] | 7013 | if (NewFD->isInvalidDecl()) { |
7014 | // If this is a class member, mark the class invalid immediately. | ||||
7015 | // This avoids some consistency errors later. | ||||
7016 | if (CXXMethodDecl* methodDecl = dyn_cast<CXXMethodDecl>(NewFD)) | ||||
7017 | methodDecl->getParent()->setInvalidDecl(); | ||||
David Majnemer | c371db6 | 2013-07-06 02:13:46 +0000 | [diff] [blame] | 7018 | } else |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 7019 | D.setRedeclaration(CheckFunctionDeclaration(S, NewFD, Previous, |
7020 | isExplicitSpecialization)); | ||||
David Blaikie | 14068e8 | 2011-09-08 06:33:04 +0000 | [diff] [blame] | 7021 | } |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7022 | |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 7023 | assert((NewFD->isInvalidDecl() || !D.isRedeclaration() || |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7024 | Previous.getResultKind() != LookupResult::FoundOverloaded) && |
7025 | "previous declaration set still overloaded"); | ||||
7026 | |||||
7027 | NamedDecl *PrincipalDecl = (FunctionTemplate | ||||
7028 | ? cast<NamedDecl>(FunctionTemplate) | ||||
7029 | : NewFD); | ||||
7030 | |||||
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 7031 | if (isFriend && D.isRedeclaration()) { |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7032 | AccessSpecifier Access = AS_public; |
7033 | if (!NewFD->isInvalidDecl()) | ||||
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 7034 | Access = NewFD->getPreviousDecl()->getAccess(); |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7035 | |
7036 | NewFD->setAccess(Access); | ||||
7037 | if (FunctionTemplate) FunctionTemplate->setAccess(Access); | ||||
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7038 | } |
7039 | |||||
7040 | if (NewFD->isOverloadedOperator() && !DC->isRecord() && | ||||
7041 | PrincipalDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary)) | ||||
7042 | PrincipalDecl->setNonMemberOperator(); | ||||
7043 | |||||
7044 | // If we have a function template, check the template parameter | ||||
7045 | // list. This will check and merge default template arguments. | ||||
7046 | if (FunctionTemplate) { | ||||
David Blaikie | d662a79 | 2011-10-19 22:56:21 +0000 | [diff] [blame] | 7047 | FunctionTemplateDecl *PrevTemplate = |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 7048 | FunctionTemplate->getPreviousDecl(); |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7049 | CheckTemplateParameterList(FunctionTemplate->getTemplateParameters(), |
David Blaikie | d662a79 | 2011-10-19 22:56:21 +0000 | [diff] [blame] | 7050 | PrevTemplate ? PrevTemplate->getTemplateParameters() : 0, |
Douglas Gregor | d89d86f | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 7051 | D.getDeclSpec().isFriendSpecified() |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 7052 | ? (D.isFunctionDefinition() |
Douglas Gregor | d89d86f | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 7053 | ? TPC_FriendFunctionTemplateDefinition |
7054 | : TPC_FriendFunctionTemplate) | ||||
7055 | : (D.getCXXScopeSpec().isSet() && | ||||
Douglas Gregor | 461bf2e | 2011-02-04 12:22:53 +0000 | [diff] [blame] | 7056 | DC && DC->isRecord() && |
7057 | DC->isDependentContext()) | ||||
Douglas Gregor | d89d86f | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 7058 | ? TPC_ClassTemplateMember |
7059 | : TPC_FunctionTemplate); | ||||
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7060 | } |
7061 | |||||
7062 | if (NewFD->isInvalidDecl()) { | ||||
7063 | // Ignore all the rest of this. | ||||
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 7064 | } else if (!D.isRedeclaration()) { |
Kaelyn Uhrain | f09ce39 | 2011-10-11 00:28:52 +0000 | [diff] [blame] | 7065 | struct ActOnFDArgs ExtraArgs = { S, D, TemplateParamLists, |
Kaelyn Uhrain | 2afd766 | 2011-10-11 00:28:39 +0000 | [diff] [blame] | 7066 | AddToScope }; |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7067 | // Fake up an access specifier if it's supposed to be a class member. |
7068 | if (isa<CXXRecordDecl>(NewFD->getDeclContext())) | ||||
7069 | NewFD->setAccess(AS_public); | ||||
7070 | |||||
7071 | // Qualified decls generally require a previous declaration. | ||||
7072 | if (D.getCXXScopeSpec().isSet()) { | ||||
7073 | // ...with the major exception of templated-scope or | ||||
7074 | // dependent-scope friend declarations. | ||||
7075 | |||||
7076 | // TODO: we currently also suppress this check in dependent | ||||
7077 | // contexts because (1) the parameter depth will be off when | ||||
7078 | // matching friend templates and (2) we might actually be | ||||
7079 | // selecting a friend based on a dependent factor. But there | ||||
7080 | // are situations where these conditions don't apply and we | ||||
7081 | // can actually do this check immediately. | ||||
7082 | if (isFriend && | ||||
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 7083 | (TemplateParamLists.size() || |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7084 | D.getCXXScopeSpec().getScopeRep()->isDependent() || |
7085 | CurContext->isDependentContext())) { | ||||
Chandler Carruth | 47eb2b6 | 2011-08-19 01:38:33 +0000 | [diff] [blame] | 7086 | // ignore these |
7087 | } else { | ||||
7088 | // The user tried to provide an out-of-line definition for a | ||||
7089 | // function that is a member of a class or namespace, but there | ||||
7090 | // was no such member function declared (C++ [class.mfct]p2, | ||||
7091 | // C++ [namespace.memdef]p2). For example: | ||||
7092 | // | ||||
7093 | // class X { | ||||
7094 | // void f() const; | ||||
7095 | // }; | ||||
7096 | // | ||||
7097 | // void X::f() { } // ill-formed | ||||
7098 | // | ||||
7099 | // Complain about this problem, and attempt to suggest close | ||||
7100 | // matches (e.g., those that differ only in cv-qualifiers and | ||||
7101 | // whether the parameter types are references). | ||||
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7102 | |
Richard Smith | 4e9686b | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 7103 | if (NamedDecl *Result = DiagnoseInvalidRedeclaration( |
7104 | *this, Previous, NewFD, ExtraArgs, false, 0)) { | ||||
Kaelyn Uhrain | 2afd766 | 2011-10-11 00:28:39 +0000 | [diff] [blame] | 7105 | AddToScope = ExtraArgs.AddToScope; |
7106 | return Result; | ||||
7107 | } | ||||
Chandler Carruth | 47eb2b6 | 2011-08-19 01:38:33 +0000 | [diff] [blame] | 7108 | } |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7109 | |
7110 | // Unqualified local friend declarations are required to resolve | ||||
7111 | // to something. | ||||
Chandler Carruth | 3d095fe | 2011-08-19 01:40:11 +0000 | [diff] [blame] | 7112 | } else if (isFriend && cast<CXXRecordDecl>(CurContext)->isLocalClass()) { |
Richard Smith | 4e9686b | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 7113 | if (NamedDecl *Result = DiagnoseInvalidRedeclaration( |
7114 | *this, Previous, NewFD, ExtraArgs, true, S)) { | ||||
Kaelyn Uhrain | 2afd766 | 2011-10-11 00:28:39 +0000 | [diff] [blame] | 7115 | AddToScope = ExtraArgs.AddToScope; |
7116 | return Result; | ||||
7117 | } | ||||
Chandler Carruth | 3d095fe | 2011-08-19 01:40:11 +0000 | [diff] [blame] | 7118 | } |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7119 | |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 7120 | } else if (!D.isFunctionDefinition() && D.getCXXScopeSpec().isSet() && |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7121 | !isFriend && !isFunctionTemplateSpecialization && |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 7122 | !isExplicitSpecialization) { |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7123 | // An out-of-line member function declaration must also be a |
7124 | // definition (C++ [dcl.meaning]p1). | ||||
7125 | // Note that this is not the case for explicit specializations of | ||||
7126 | // function templates or member functions of class templates, per | ||||
David Blaikie | d662a79 | 2011-10-19 22:56:21 +0000 | [diff] [blame] | 7127 | // C++ [temp.expl.spec]p2. We also allow these declarations as an |
7128 | // extension for compatibility with old SWIG code which likes to | ||||
7129 | // generate them. | ||||
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7130 | Diag(NewFD->getLocation(), diag::ext_out_of_line_declaration) |
7131 | << D.getCXXScopeSpec().getRange(); | ||||
7132 | } | ||||
7133 | } | ||||
Ryan Flynn | 478fbc6 | 2009-07-25 22:29:44 +0000 | [diff] [blame] | 7134 | |
Rafael Espindola | 65611bf | 2013-03-02 21:41:48 +0000 | [diff] [blame] | 7135 | ProcessPragmaWeak(S, NewFD); |
Rafael Espindola | 2a5bb50 | 2013-01-16 23:11:15 +0000 | [diff] [blame] | 7136 | checkAttributesAfterMerging(*this, *NewFD); |
7137 | |||||
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 7138 | AddKnownFunctionAttributes(NewFD); |
7139 | |||||
Douglas Gregor | d945538 | 2010-08-06 13:50:58 +0000 | [diff] [blame] | 7140 | if (NewFD->hasAttr<OverloadableAttr>() && |
7141 | !NewFD->getType()->getAs<FunctionProtoType>()) { | ||||
7142 | Diag(NewFD->getLocation(), | ||||
7143 | diag::err_attribute_overloadable_no_prototype) | ||||
7144 | << NewFD; | ||||
7145 | |||||
7146 | // Turn this into a variadic function with no parameters. | ||||
7147 | const FunctionType *FT = NewFD->getType()->getAs<FunctionType>(); | ||||
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 7148 | FunctionProtoType::ExtProtoInfo EPI; |
7149 | EPI.Variadic = true; | ||||
7150 | EPI.ExtInfo = FT->getExtInfo(); | ||||
7151 | |||||
Dmitri Gribenko | 5543169 | 2013-05-05 00:41:58 +0000 | [diff] [blame] | 7152 | QualType R = Context.getFunctionType(FT->getResultType(), None, EPI); |
Douglas Gregor | d945538 | 2010-08-06 13:50:58 +0000 | [diff] [blame] | 7153 | NewFD->setType(R); |
7154 | } | ||||
7155 | |||||
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 7156 | // If there's a #pragma GCC visibility in scope, and this isn't a class |
7157 | // member, set the visibility of this function. | ||||
Rafael Espindola | 181e3ec | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 7158 | if (!DC->isRecord() && NewFD->isExternallyVisible()) |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 7159 | AddPushedVisibilityAttribute(NewFD); |
7160 | |||||
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 7161 | // If there's a #pragma clang arc_cf_code_audited in scope, consider |
7162 | // marking the function. | ||||
7163 | AddCFAuditedAttribute(NewFD); | ||||
7164 | |||||
Richard Smith | aa4bc18 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 7165 | // If this is the first declaration of an extern C variable, update |
7166 | // the map of such variables. | ||||
7167 | if (!NewFD->getPreviousDecl() && !NewFD->isInvalidDecl() && | ||||
7168 | isIncompleteDeclExternC(*this, NewFD)) | ||||
Richard Smith | 662f41b | 2013-06-18 20:15:12 +0000 | [diff] [blame] | 7169 | RegisterLocallyScopedExternCDecl(NewFD, S); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 7170 | |
Argyrios Kyrtzidis | 16f1930 | 2009-06-25 18:22:24 +0000 | [diff] [blame] | 7171 | // Set this FunctionDecl's range up to the right paren. |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 7172 | NewFD->setRangeEnd(D.getSourceRange().getEnd()); |
Argyrios Kyrtzidis | 16f1930 | 2009-06-25 18:22:24 +0000 | [diff] [blame] | 7173 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7174 | if (getLangOpts().CPlusPlus) { |
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7175 | if (FunctionTemplate) { |
7176 | if (NewFD->isInvalidDecl()) | ||||
7177 | FunctionTemplate->setInvalidDecl(); | ||||
7178 | return FunctionTemplate; | ||||
7179 | } | ||||
Fariborz Jahanian | bfe5788 | 2010-12-09 23:11:32 +0000 | [diff] [blame] | 7180 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7181 | |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 7182 | if (NewFD->hasAttr<OpenCLKernelAttr>()) { |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 7183 | // OpenCL v1.2 s6.8 static is invalid for kernel functions. |
7184 | if ((getLangOpts().OpenCLVersion >= 120) | ||||
7185 | && (SC == SC_Static)) { | ||||
7186 | Diag(D.getIdentifierLoc(), diag::err_static_kernel); | ||||
7187 | D.setInvalidType(); | ||||
7188 | } | ||||
Tanya Lattner | 7564bcc | 2013-01-30 19:48:52 +0000 | [diff] [blame] | 7189 | |
7190 | // OpenCL v1.2, s6.9 -- Kernels can only have return type void. | ||||
7191 | if (!NewFD->getResultType()->isVoidType()) { | ||||
7192 | Diag(D.getIdentifierLoc(), | ||||
7193 | diag::err_expected_kernel_void_return_type); | ||||
7194 | D.setInvalidType(); | ||||
7195 | } | ||||
Matt Arsenault | e6c8afc | 2013-07-23 01:23:36 +0000 | [diff] [blame] | 7196 | |
7197 | llvm::SmallPtrSet<const Type *, 16> ValidTypes; | ||||
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 7198 | for (FunctionDecl::param_iterator PI = NewFD->param_begin(), |
7199 | PE = NewFD->param_end(); PI != PE; ++PI) { | ||||
Joey Gouly | 98f988d | 2013-01-29 10:54:06 +0000 | [diff] [blame] | 7200 | ParmVarDecl *Param = *PI; |
Matt Arsenault | e6c8afc | 2013-07-23 01:23:36 +0000 | [diff] [blame] | 7201 | checkIsValidOpenCLKernelParameter(*this, D, Param, ValidTypes); |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 7202 | } |
Tanya Lattner | 5e94d6f | 2012-06-19 23:09:52 +0000 | [diff] [blame] | 7203 | } |
7204 | |||||
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 7205 | MarkUnusedFileScopedDecl(NewFD); |
Peter Collingbourne | 14b6ba7 | 2011-02-09 21:04:32 +0000 | [diff] [blame] | 7206 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7207 | if (getLangOpts().CUDA) |
Peter Collingbourne | 14b6ba7 | 2011-02-09 21:04:32 +0000 | [diff] [blame] | 7208 | if (IdentifierInfo *II = NewFD->getIdentifier()) |
7209 | if (!NewFD->isInvalidDecl() && | ||||
7210 | NewFD->getDeclContext()->getRedeclContext()->isTranslationUnit()) { | ||||
7211 | if (II->isStr("cudaConfigureCall")) { | ||||
7212 | if (!R->getAs<FunctionType>()->getResultType()->isScalarType()) | ||||
7213 | Diag(NewFD->getLocation(), diag::err_config_scalar_return); | ||||
7214 | |||||
7215 | Context.setcudaConfigureCallDecl(NewFD); | ||||
7216 | } | ||||
7217 | } | ||||
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 7218 | |
7219 | // Here we have an function template explicit specialization at class scope. | ||||
7220 | // The actually specialization will be postponed to template instatiation | ||||
7221 | // time via the ClassScopeFunctionSpecializationDecl node. | ||||
7222 | if (isDependentClassScopeExplicitSpecialization) { | ||||
7223 | ClassScopeFunctionSpecializationDecl *NewSpec = | ||||
7224 | ClassScopeFunctionSpecializationDecl::Create( | ||||
Nico Weber | 6b02009 | 2012-06-25 17:21:05 +0000 | [diff] [blame] | 7225 | Context, CurContext, SourceLocation(), |
7226 | cast<CXXMethodDecl>(NewFD), | ||||
7227 | HasExplicitTemplateArgs, TemplateArgs); | ||||
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 7228 | CurContext->addDecl(NewSpec); |
7229 | AddToScope = false; | ||||
7230 | } | ||||
Peter Collingbourne | 14b6ba7 | 2011-02-09 21:04:32 +0000 | [diff] [blame] | 7231 | |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 7232 | return NewFD; |
7233 | } | ||||
7234 | |||||
7235 | /// \brief Perform semantic checking of a new function declaration. | ||||
7236 | /// | ||||
7237 | /// Performs semantic analysis of the new function declaration | ||||
7238 | /// NewFD. This routine performs all semantic checking that does not | ||||
7239 | /// require the actual declarator involved in the declaration, and is | ||||
7240 | /// used both for the declaration of functions as they are parsed | ||||
7241 | /// (called via ActOnDeclarator) and for the declaration of functions | ||||
7242 | /// that have been instantiated via C++ template instantiation (called | ||||
7243 | /// via InstantiateDecl). | ||||
7244 | /// | ||||
James Dennett | efce31f | 2012-06-22 08:10:18 +0000 | [diff] [blame] | 7245 | /// \param IsExplicitSpecialization whether this new function declaration is |
Douglas Gregor | fd056bc | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 7246 | /// an explicit specialization of the previous declaration. |
7247 | /// | ||||
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 7248 | /// This sets NewFD->isInvalidDecl() to true if there was an error. |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 7249 | /// |
James Dennett | efce31f | 2012-06-22 08:10:18 +0000 | [diff] [blame] | 7250 | /// \returns true if the function declaration is a redeclaration. |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 7251 | bool Sema::CheckFunctionDeclaration(Scope *S, FunctionDecl *NewFD, |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7252 | LookupResult &Previous, |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 7253 | bool IsExplicitSpecialization) { |
David Blaikie | 14068e8 | 2011-09-08 06:33:04 +0000 | [diff] [blame] | 7254 | assert(!NewFD->getResultType()->isVariablyModifiedType() |
7255 | && "Variably modified return types are not handled here"); | ||||
John McCall | 8c4859a | 2009-07-24 03:03:21 +0000 | [diff] [blame] | 7256 | |
Richard Smith | dd9459f | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 7257 | // Determine whether the type of this function should be merged with |
7258 | // a previous visible declaration. This never happens for functions in C++, | ||||
7259 | // and always happens in C if the previous declaration was visible. | ||||
7260 | bool MergeTypeWithPrevious = !getLangOpts().CPlusPlus && | ||||
7261 | !Previous.isShadowed(); | ||||
7262 | |||||
Douglas Gregor | 7dc80e1 | 2013-01-09 00:47:56 +0000 | [diff] [blame] | 7263 | // Filter out any non-conflicting previous declarations. |
7264 | filterNonConflictingPreviousDecls(Context, NewFD, Previous); | ||||
7265 | |||||
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 7266 | bool Redeclaration = false; |
Richard Smith | 21c8fa8 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 7267 | NamedDecl *OldDecl = 0; |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 7268 | |
Douglas Gregor | 04495c8 | 2009-02-24 01:23:02 +0000 | [diff] [blame] | 7269 | // Merge or overload the declaration with an existing declaration of |
7270 | // the same name, if appropriate. | ||||
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7271 | if (!Previous.empty()) { |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 7272 | // Determine whether NewFD is an overload of PrevDecl or |
Zhongxing Xu | 416fcaf | 2009-01-16 01:13:29 +0000 | [diff] [blame] | 7273 | // a declaration that requires merging. If it's an overload, |
7274 | // there's no more work to do here; we'll just add the new | ||||
7275 | // function to the scope. | ||||
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 7276 | if (!AllowOverloadingOfFunction(Previous, Context)) { |
Rafael Espindola | 90cc390 | 2013-04-15 12:49:13 +0000 | [diff] [blame] | 7277 | NamedDecl *Candidate = Previous.getFoundDecl(); |
7278 | if (shouldLinkPossiblyHiddenDecl(Candidate, NewFD)) { | ||||
7279 | Redeclaration = true; | ||||
7280 | OldDecl = Candidate; | ||||
7281 | } | ||||
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 7282 | } else { |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 7283 | switch (CheckOverload(S, NewFD, Previous, OldDecl, |
7284 | /*NewIsUsingDecl*/ false)) { | ||||
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 7285 | case Ovl_Match: |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7286 | Redeclaration = true; |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 7287 | break; |
7288 | |||||
7289 | case Ovl_NonFunction: | ||||
7290 | Redeclaration = true; | ||||
7291 | break; | ||||
7292 | |||||
7293 | case Ovl_Overload: | ||||
7294 | Redeclaration = false; | ||||
7295 | break; | ||||
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7296 | } |
Peter Collingbourne | c80e811 | 2011-01-21 02:08:54 +0000 | [diff] [blame] | 7297 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7298 | if (!getLangOpts().CPlusPlus && !NewFD->hasAttr<OverloadableAttr>()) { |
Peter Collingbourne | c80e811 | 2011-01-21 02:08:54 +0000 | [diff] [blame] | 7299 | // If a function name is overloadable in C, then every function |
7300 | // with that name must be marked "overloadable". | ||||
7301 | Diag(NewFD->getLocation(), diag::err_attribute_overloadable_missing) | ||||
7302 | << Redeclaration << NewFD; | ||||
7303 | NamedDecl *OverloadedDecl = 0; | ||||
7304 | if (Redeclaration) | ||||
7305 | OverloadedDecl = OldDecl; | ||||
7306 | else if (!Previous.empty()) | ||||
7307 | OverloadedDecl = Previous.getRepresentativeDecl(); | ||||
7308 | if (OverloadedDecl) | ||||
7309 | Diag(OverloadedDecl->getLocation(), | ||||
7310 | diag::note_attribute_overloadable_prev_overload); | ||||
7311 | NewFD->addAttr(::new (Context) OverloadableAttr(SourceLocation(), | ||||
7312 | Context)); | ||||
7313 | } | ||||
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7314 | } |
Richard Smith | 21c8fa8 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 7315 | } |
Zhongxing Xu | 416fcaf | 2009-01-16 01:13:29 +0000 | [diff] [blame] | 7316 | |
Richard Smith | aa4bc18 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 7317 | // Check for a previous extern "C" declaration with this name. |
7318 | if (!Redeclaration && | ||||
7319 | checkForConflictWithNonVisibleExternC(*this, NewFD, Previous)) { | ||||
7320 | filterNonConflictingPreviousDecls(Context, NewFD, Previous); | ||||
7321 | if (!Previous.empty()) { | ||||
7322 | // This is an extern "C" declaration with the same name as a previous | ||||
7323 | // declaration, and thus redeclares that entity... | ||||
7324 | Redeclaration = true; | ||||
7325 | OldDecl = Previous.getFoundDecl(); | ||||
Richard Smith | dd9459f | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 7326 | MergeTypeWithPrevious = false; |
Richard Smith | aa4bc18 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 7327 | |
7328 | // ... except in the presence of __attribute__((overloadable)). | ||||
7329 | if (OldDecl->hasAttr<OverloadableAttr>()) { | ||||
7330 | if (!getLangOpts().CPlusPlus && !NewFD->hasAttr<OverloadableAttr>()) { | ||||
7331 | Diag(NewFD->getLocation(), diag::err_attribute_overloadable_missing) | ||||
7332 | << Redeclaration << NewFD; | ||||
7333 | Diag(Previous.getFoundDecl()->getLocation(), | ||||
7334 | diag::note_attribute_overloadable_prev_overload); | ||||
7335 | NewFD->addAttr(::new (Context) OverloadableAttr(SourceLocation(), | ||||
7336 | Context)); | ||||
7337 | } | ||||
7338 | if (IsOverload(NewFD, cast<FunctionDecl>(OldDecl), false)) { | ||||
7339 | Redeclaration = false; | ||||
7340 | OldDecl = 0; | ||||
7341 | } | ||||
7342 | } | ||||
7343 | } | ||||
7344 | } | ||||
7345 | |||||
Richard Smith | 21c8fa8 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 7346 | // C++11 [dcl.constexpr]p8: |
7347 | // A constexpr specifier for a non-static member function that is not | ||||
7348 | // a constructor declares that member function to be const. | ||||
7349 | // | ||||
7350 | // This needs to be delayed until we know whether this is an out-of-line | ||||
7351 | // definition of a static member function. | ||||
Richard Smith | 8404626 | 2013-04-21 01:08:50 +0000 | [diff] [blame] | 7352 | // |
7353 | // This rule is not present in C++1y, so we produce a backwards | ||||
7354 | // compatibility warning whenever it happens in C++11. | ||||
Richard Smith | 21c8fa8 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 7355 | CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(NewFD); |
Richard Smith | 8404626 | 2013-04-21 01:08:50 +0000 | [diff] [blame] | 7356 | if (!getLangOpts().CPlusPlus1y && MD && MD->isConstexpr() && |
7357 | !MD->isStatic() && !isa<CXXConstructorDecl>(MD) && | ||||
Richard Smith | 21c8fa8 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 7358 | (MD->getTypeQualifiers() & Qualifiers::Const) == 0) { |
7359 | CXXMethodDecl *OldMD = dyn_cast_or_null<CXXMethodDecl>(OldDecl); | ||||
7360 | if (FunctionTemplateDecl *OldTD = | ||||
7361 | dyn_cast_or_null<FunctionTemplateDecl>(OldDecl)) | ||||
7362 | OldMD = dyn_cast<CXXMethodDecl>(OldTD->getTemplatedDecl()); | ||||
7363 | if (!OldMD || !OldMD->isStatic()) { | ||||
7364 | const FunctionProtoType *FPT = | ||||
7365 | MD->getType()->castAs<FunctionProtoType>(); | ||||
7366 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); | ||||
7367 | EPI.TypeQuals |= Qualifiers::Const; | ||||
7368 | MD->setType(Context.getFunctionType(FPT->getResultType(), | ||||
Reid Kleckner | 0567a79 | 2013-06-10 20:51:09 +0000 | [diff] [blame] | 7369 | FPT->getArgTypes(), EPI)); |
Richard Smith | 8404626 | 2013-04-21 01:08:50 +0000 | [diff] [blame] | 7370 | |
7371 | // Warn that we did this, if we're not performing template instantiation. | ||||
7372 | // In that case, we'll have warned already when the template was defined. | ||||
7373 | if (ActiveTemplateInstantiations.empty()) { | ||||
7374 | SourceLocation AddConstLoc; | ||||
7375 | if (FunctionTypeLoc FTL = MD->getTypeSourceInfo()->getTypeLoc() | ||||
7376 | .IgnoreParens().getAs<FunctionTypeLoc>()) | ||||
7377 | AddConstLoc = PP.getLocForEndOfToken(FTL.getRParenLoc()); | ||||
7378 | |||||
7379 | Diag(MD->getLocation(), diag::warn_cxx1y_compat_constexpr_not_const) | ||||
7380 | << FixItHint::CreateInsertion(AddConstLoc, " const"); | ||||
7381 | } | ||||
Richard Smith | 21c8fa8 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 7382 | } |
7383 | } | ||||
7384 | |||||
7385 | if (Redeclaration) { | ||||
7386 | // NewFD and OldDecl represent declarations that need to be | ||||
7387 | // merged. | ||||
Richard Smith | dd9459f | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 7388 | if (MergeFunctionDecl(NewFD, OldDecl, S, MergeTypeWithPrevious)) { |
Richard Smith | 21c8fa8 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 7389 | NewFD->setInvalidDecl(); |
7390 | return Redeclaration; | ||||
7391 | } | ||||
7392 | |||||
7393 | Previous.clear(); | ||||
7394 | Previous.addDecl(OldDecl); | ||||
7395 | |||||
7396 | if (FunctionTemplateDecl *OldTemplateDecl | ||||
7397 | = dyn_cast<FunctionTemplateDecl>(OldDecl)) { | ||||
7398 | NewFD->setPreviousDeclaration(OldTemplateDecl->getTemplatedDecl()); | ||||
7399 | FunctionTemplateDecl *NewTemplateDecl | ||||
7400 | = NewFD->getDescribedFunctionTemplate(); | ||||
7401 | assert(NewTemplateDecl && "Template/non-template mismatch"); | ||||
7402 | if (CXXMethodDecl *Method | ||||
7403 | = dyn_cast<CXXMethodDecl>(NewTemplateDecl->getTemplatedDecl())) { | ||||
7404 | Method->setAccess(OldTemplateDecl->getAccess()); | ||||
7405 | NewTemplateDecl->setAccess(OldTemplateDecl->getAccess()); | ||||
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 7406 | } |
Richard Smith | 21c8fa8 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 7407 | |
7408 | // If this is an explicit specialization of a member that is a function | ||||
7409 | // template, mark it as a member specialization. | ||||
7410 | if (IsExplicitSpecialization && | ||||
7411 | NewTemplateDecl->getInstantiatedFromMemberTemplate()) { | ||||
7412 | NewTemplateDecl->setMemberSpecialization(); | ||||
7413 | assert(OldTemplateDecl->isMemberSpecialization()); | ||||
Argyrios Kyrtzidis | 9bedef6 | 2009-07-14 03:18:53 +0000 | [diff] [blame] | 7414 | } |
Richard Smith | 21c8fa8 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 7415 | |
7416 | } else { | ||||
John McCall | d5617ee | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 7417 | // This needs to happen first so that 'inline' propagates. |
Richard Smith | 21c8fa8 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 7418 | NewFD->setPreviousDeclaration(cast<FunctionDecl>(OldDecl)); |
John McCall | d5617ee | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 7419 | |
7420 | if (isa<CXXMethodDecl>(NewFD)) { | ||||
7421 | // A valid redeclaration of a C++ method must be out-of-line, | ||||
7422 | // but (unfortunately) it's not necessarily a definition | ||||
7423 | // because of templates, which means that the previous | ||||
7424 | // declaration is not necessarily from the class definition. | ||||
7425 | |||||
7426 | // For just setting the access, that doesn't matter. | ||||
7427 | CXXMethodDecl *oldMethod = cast<CXXMethodDecl>(OldDecl); | ||||
7428 | NewFD->setAccess(oldMethod->getAccess()); | ||||
7429 | |||||
7430 | // Update the key-function state if necessary for this ABI. | ||||
7431 | if (NewFD->isInlined() && | ||||
7432 | !Context.getTargetInfo().getCXXABI().canKeyFunctionBeInline()) { | ||||
7433 | // setNonKeyFunction needs to work with the original | ||||
7434 | // declaration from the class definition, and isVirtual() is | ||||
7435 | // just faster in that case, so map back to that now. | ||||
7436 | oldMethod = cast<CXXMethodDecl>(oldMethod->getFirstDeclaration()); | ||||
7437 | if (oldMethod->isVirtual()) { | ||||
7438 | Context.setNonKeyFunction(oldMethod); | ||||
7439 | } | ||||
7440 | } | ||||
7441 | } | ||||
Zhongxing Xu | 416fcaf | 2009-01-16 01:13:29 +0000 | [diff] [blame] | 7442 | } |
Douglas Gregor | 4ce205f | 2009-02-06 17:46:57 +0000 | [diff] [blame] | 7443 | } |
Zhongxing Xu | 416fcaf | 2009-01-16 01:13:29 +0000 | [diff] [blame] | 7444 | |
Anders Carlsson | 2c59d3c | 2009-09-13 21:33:06 +0000 | [diff] [blame] | 7445 | // Semantic checking for this function declaration (in isolation). |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7446 | if (getLangOpts().CPlusPlus) { |
Anders Carlsson | 2c59d3c | 2009-09-13 21:33:06 +0000 | [diff] [blame] | 7447 | // C++-specific checks. |
7448 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(NewFD)) { | ||||
7449 | CheckConstructor(Constructor); | ||||
Anders Carlsson | 6d70139 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 7450 | } else if (CXXDestructorDecl *Destructor = |
7451 | dyn_cast<CXXDestructorDecl>(NewFD)) { | ||||
7452 | CXXRecordDecl *Record = Destructor->getParent(); | ||||
Anders Carlsson | 2c59d3c | 2009-09-13 21:33:06 +0000 | [diff] [blame] | 7453 | QualType ClassType = Context.getTypeDeclType(Record); |
Anders Carlsson | 6d70139 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 7454 | |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 7455 | // 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] | 7456 | // type is dependent? Both gcc and edg can handle that. |
Anders Carlsson | 2c59d3c | 2009-09-13 21:33:06 +0000 | [diff] [blame] | 7457 | if (!ClassType->isDependentType()) { |
7458 | DeclarationName Name | ||||
7459 | = Context.DeclarationNames.getCXXDestructorName( | ||||
7460 | Context.getCanonicalType(ClassType)); | ||||
7461 | if (NewFD->getDeclName() != Name) { | ||||
7462 | Diag(NewFD->getLocation(), diag::err_destructor_name); | ||||
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 7463 | NewFD->setInvalidDecl(); |
7464 | return Redeclaration; | ||||
Anders Carlsson | 2c59d3c | 2009-09-13 21:33:06 +0000 | [diff] [blame] | 7465 | } |
7466 | } | ||||
Anders Carlsson | 2c59d3c | 2009-09-13 21:33:06 +0000 | [diff] [blame] | 7467 | } else if (CXXConversionDecl *Conversion |
Douglas Gregor | 4ba3136 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 7468 | = dyn_cast<CXXConversionDecl>(NewFD)) { |
Anders Carlsson | 2c59d3c | 2009-09-13 21:33:06 +0000 | [diff] [blame] | 7469 | ActOnConversionDeclarator(Conversion); |
Douglas Gregor | 4ba3136 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 7470 | } |
7471 | |||||
7472 | // Find any virtual functions that this function overrides. | ||||
Douglas Gregor | e6342c0 | 2009-12-01 17:35:23 +0000 | [diff] [blame] | 7473 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(NewFD)) { |
7474 | if (!Method->isFunctionTemplateSpecialization() && | ||||
Argyrios Kyrtzidis | 38eb1e1 | 2012-10-09 01:23:45 +0000 | [diff] [blame] | 7475 | !Method->getDescribedFunctionTemplate() && |
7476 | Method->isCanonicalDecl()) { | ||||
Douglas Gregor | a6c1e3a | 2010-10-13 22:55:32 +0000 | [diff] [blame] | 7477 | if (AddOverriddenMethods(Method->getParent(), Method)) { |
7478 | // If the function was marked as "static", we have a problem. | ||||
7479 | if (NewFD->getStorageClass() == SC_Static) { | ||||
David Blaikie | 5708c18 | 2012-10-17 00:47:58 +0000 | [diff] [blame] | 7480 | ReportOverrides(*this, diag::err_static_overrides_virtual, Method); |
Douglas Gregor | a6c1e3a | 2010-10-13 22:55:32 +0000 | [diff] [blame] | 7481 | } |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 7482 | } |
Douglas Gregor | a6c1e3a | 2010-10-13 22:55:32 +0000 | [diff] [blame] | 7483 | } |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 7484 | |
7485 | if (Method->isStatic()) | ||||
7486 | checkThisInStaticMemberFunctionType(Method); | ||||
Douglas Gregor | e6342c0 | 2009-12-01 17:35:23 +0000 | [diff] [blame] | 7487 | } |
Anders Carlsson | 2c59d3c | 2009-09-13 21:33:06 +0000 | [diff] [blame] | 7488 | |
7489 | // Extra checking for C++ overloaded operators (C++ [over.oper]). | ||||
7490 | if (NewFD->isOverloadedOperator() && | ||||
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 7491 | CheckOverloadedOperatorDeclaration(NewFD)) { |
7492 | NewFD->setInvalidDecl(); | ||||
7493 | return Redeclaration; | ||||
7494 | } | ||||
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 7495 | |
7496 | // Extra checking for C++0x literal operators (C++0x [over.literal]). | ||||
7497 | if (NewFD->getLiteralIdentifier() && | ||||
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 7498 | CheckLiteralOperatorDeclaration(NewFD)) { |
7499 | NewFD->setInvalidDecl(); | ||||
7500 | return Redeclaration; | ||||
7501 | } | ||||
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 7502 | |
Anders Carlsson | 2c59d3c | 2009-09-13 21:33:06 +0000 | [diff] [blame] | 7503 | // In C++, check default arguments now that we have merged decls. Unless |
7504 | // the lexical context is the class, because in this case this is done | ||||
7505 | // during delayed parsing anyway. | ||||
7506 | if (!CurContext->isRecord()) | ||||
7507 | CheckCXXDefaultArguments(NewFD); | ||||
Douglas Gregor | b68e399 | 2010-12-21 19:47:46 +0000 | [diff] [blame] | 7508 | |
7509 | // If this function declares a builtin function, check the type of this | ||||
7510 | // declaration against the expected type for the builtin. | ||||
7511 | if (unsigned BuiltinID = NewFD->getBuiltinID()) { | ||||
7512 | ASTContext::GetBuiltinTypeError Error; | ||||
Fariborz Jahanian | 9ef1518 | 2013-01-05 21:54:55 +0000 | [diff] [blame] | 7513 | LookupPredefedObjCSuperType(*this, S, NewFD->getIdentifier()); |
Douglas Gregor | b68e399 | 2010-12-21 19:47:46 +0000 | [diff] [blame] | 7514 | QualType T = Context.GetBuiltinType(BuiltinID, Error); |
7515 | if (!T.isNull() && !Context.hasSameType(T, NewFD->getType())) { | ||||
7516 | // The type of this function differs from the type of the builtin, | ||||
7517 | // so forget about the builtin entirely. | ||||
7518 | Context.BuiltinInfo.ForgetBuiltin(BuiltinID, Context.Idents); | ||||
7519 | } | ||||
7520 | } | ||||
Aaron Ballman | 2c0bf24 | 2012-02-09 01:21:34 +0000 | [diff] [blame] | 7521 | |
7522 | // If this function is declared as being extern "C", then check to see if | ||||
7523 | // the function returns a UDT (class, struct, or union type) that is not C | ||||
7524 | // compatible, and if it does, warn the user. | ||||
Fariborz Jahanian | 96db329 | 2013-03-14 23:09:00 +0000 | [diff] [blame] | 7525 | // But, issue any diagnostic on the first declaration only. |
7526 | if (NewFD->isExternC() && Previous.empty()) { | ||||
Aaron Ballman | 2c0bf24 | 2012-02-09 01:21:34 +0000 | [diff] [blame] | 7527 | QualType R = NewFD->getResultType(); |
Hans Wennborg | 168c07b | 2012-07-24 17:59:41 +0000 | [diff] [blame] | 7528 | if (R->isIncompleteType() && !R->isVoidType()) |
7529 | Diag(NewFD->getLocation(), diag::warn_return_value_udt_incomplete) | ||||
7530 | << NewFD << R; | ||||
Douglas Gregor | b38b491 | 2012-08-07 06:14:34 +0000 | [diff] [blame] | 7531 | else if (!R.isPODType(Context) && !R->isVoidType() && |
7532 | !R->isObjCObjectPointerType()) | ||||
Hans Wennborg | 168c07b | 2012-07-24 17:59:41 +0000 | [diff] [blame] | 7533 | Diag(NewFD->getLocation(), diag::warn_return_value_udt) << NewFD << R; |
Aaron Ballman | 2c0bf24 | 2012-02-09 01:21:34 +0000 | [diff] [blame] | 7534 | } |
Anders Carlsson | 2c59d3c | 2009-09-13 21:33:06 +0000 | [diff] [blame] | 7535 | } |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 7536 | return Redeclaration; |
Zhongxing Xu | 416fcaf | 2009-01-16 01:13:29 +0000 | [diff] [blame] | 7537 | } |
7538 | |||||
Dmitri Gribenko | a6f9707 | 2013-01-17 00:26:13 +0000 | [diff] [blame] | 7539 | static SourceRange getResultSourceRange(const FunctionDecl *FD) { |
7540 | const TypeSourceInfo *TSI = FD->getTypeSourceInfo(); | ||||
7541 | if (!TSI) | ||||
7542 | return SourceRange(); | ||||
7543 | |||||
7544 | TypeLoc TL = TSI->getTypeLoc(); | ||||
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 7545 | FunctionTypeLoc FunctionTL = TL.getAs<FunctionTypeLoc>(); |
Dmitri Gribenko | a6f9707 | 2013-01-17 00:26:13 +0000 | [diff] [blame] | 7546 | if (!FunctionTL) |
7547 | return SourceRange(); | ||||
7548 | |||||
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 7549 | TypeLoc ResultTL = FunctionTL.getResultLoc(); |
7550 | if (ResultTL.getUnqualifiedLoc().getAs<BuiltinTypeLoc>()) | ||||
Dmitri Gribenko | a6f9707 | 2013-01-17 00:26:13 +0000 | [diff] [blame] | 7551 | return ResultTL.getSourceRange(); |
7552 | |||||
7553 | return SourceRange(); | ||||
7554 | } | ||||
7555 | |||||
David Blaikie | 14068e8 | 2011-09-08 06:33:04 +0000 | [diff] [blame] | 7556 | void Sema::CheckMain(FunctionDecl* FD, const DeclSpec& DS) { |
Richard Smith | a506586 | 2012-02-04 06:10:17 +0000 | [diff] [blame] | 7557 | // C++11 [basic.start.main]p3: A program that declares main to be inline, |
7558 | // static or constexpr is ill-formed. | ||||
Richard Smith | de03c15 | 2013-01-17 22:16:11 +0000 | [diff] [blame] | 7559 | // C11 6.7.4p4: In a hosted environment, no function specifier(s) shall |
7560 | // appear in a declaration of main. | ||||
John McCall | 13591ed | 2009-07-25 04:36:53 +0000 | [diff] [blame] | 7561 | // 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] | 7562 | // We accept _Noreturn main as an extension. |
David Blaikie | 14068e8 | 2011-09-08 06:33:04 +0000 | [diff] [blame] | 7563 | if (FD->getStorageClass() == SC_Static) |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7564 | Diag(DS.getStorageClassSpecLoc(), getLangOpts().CPlusPlus |
David Blaikie | 14068e8 | 2011-09-08 06:33:04 +0000 | [diff] [blame] | 7565 | ? diag::err_static_main : diag::warn_static_main) |
7566 | << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc()); | ||||
7567 | if (FD->isInlineSpecified()) | ||||
7568 | Diag(DS.getInlineSpecLoc(), diag::err_inline_main) | ||||
7569 | << FixItHint::CreateRemoval(DS.getInlineSpecLoc()); | ||||
Dmitri Gribenko | 445743d | 2013-01-21 11:25:03 +0000 | [diff] [blame] | 7570 | if (DS.isNoreturnSpecified()) { |
7571 | SourceLocation NoreturnLoc = DS.getNoreturnSpecLoc(); | ||||
7572 | SourceRange NoreturnRange(NoreturnLoc, | ||||
7573 | PP.getLocForEndOfToken(NoreturnLoc)); | ||||
7574 | Diag(NoreturnLoc, diag::ext_noreturn_main); | ||||
7575 | Diag(NoreturnLoc, diag::note_main_remove_noreturn) | ||||
7576 | << FixItHint::CreateRemoval(NoreturnRange); | ||||
7577 | } | ||||
Richard Smith | a506586 | 2012-02-04 06:10:17 +0000 | [diff] [blame] | 7578 | if (FD->isConstexpr()) { |
7579 | Diag(DS.getConstexprSpecLoc(), diag::err_constexpr_main) | ||||
7580 | << FixItHint::CreateRemoval(DS.getConstexprSpecLoc()); | ||||
7581 | FD->setConstexpr(false); | ||||
7582 | } | ||||
John McCall | 13591ed | 2009-07-25 04:36:53 +0000 | [diff] [blame] | 7583 | |
7584 | QualType T = FD->getType(); | ||||
7585 | assert(T->isFunctionType() && "function decl is not of function type"); | ||||
John McCall | 75d8ba3 | 2012-02-14 19:50:52 +0000 | [diff] [blame] | 7586 | const FunctionType* FT = T->castAs<FunctionType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7587 | |
John McCall | 75d8ba3 | 2012-02-14 19:50:52 +0000 | [diff] [blame] | 7588 | // All the standards say that main() should should return 'int'. |
7589 | if (Context.hasSameUnqualifiedType(FT->getResultType(), Context.IntTy)) { | ||||
7590 | // In C and C++, main magically returns 0 if you fall off the end; | ||||
7591 | // set the flag which tells us that. | ||||
7592 | // This is C++ [basic.start.main]p5 and C99 5.1.2.2.3. | ||||
7593 | FD->setHasImplicitReturnZero(true); | ||||
7594 | |||||
7595 | // In C with GNU extensions we allow main() to have non-integer return | ||||
7596 | // type, but we should warn about the extension, and we disable the | ||||
7597 | // implicit-return-zero rule. | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7598 | } else if (getLangOpts().GNUMode && !getLangOpts().CPlusPlus) { |
John McCall | 75d8ba3 | 2012-02-14 19:50:52 +0000 | [diff] [blame] | 7599 | Diag(FD->getTypeSpecStartLoc(), diag::ext_main_returns_nonint); |
7600 | |||||
Dmitri Gribenko | a6f9707 | 2013-01-17 00:26:13 +0000 | [diff] [blame] | 7601 | SourceRange ResultRange = getResultSourceRange(FD); |
7602 | if (ResultRange.isValid()) | ||||
7603 | Diag(ResultRange.getBegin(), diag::note_main_change_return_type) | ||||
7604 | << FixItHint::CreateReplacement(ResultRange, "int"); | ||||
7605 | |||||
John McCall | 75d8ba3 | 2012-02-14 19:50:52 +0000 | [diff] [blame] | 7606 | // Otherwise, this is just a flat-out error. |
7607 | } else { | ||||
Dmitri Gribenko | a6f9707 | 2013-01-17 00:26:13 +0000 | [diff] [blame] | 7608 | SourceRange ResultRange = getResultSourceRange(FD); |
7609 | if (ResultRange.isValid()) | ||||
7610 | Diag(FD->getTypeSpecStartLoc(), diag::err_main_returns_nonint) | ||||
7611 | << FixItHint::CreateReplacement(ResultRange, "int"); | ||||
7612 | else | ||||
7613 | Diag(FD->getTypeSpecStartLoc(), diag::err_main_returns_nonint); | ||||
7614 | |||||
John McCall | 13591ed | 2009-07-25 04:36:53 +0000 | [diff] [blame] | 7615 | FD->setInvalidDecl(true); |
7616 | } | ||||
7617 | |||||
7618 | // Treat protoless main() as nullary. | ||||
7619 | if (isa<FunctionNoProtoType>(FT)) return; | ||||
7620 | |||||
7621 | const FunctionProtoType* FTP = cast<const FunctionProtoType>(FT); | ||||
7622 | unsigned nparams = FTP->getNumArgs(); | ||||
7623 | assert(FD->getNumParams() == nparams); | ||||
7624 | |||||
John McCall | 6675586 | 2009-12-24 09:58:38 +0000 | [diff] [blame] | 7625 | bool HasExtraParameters = (nparams > 3); |
7626 | |||||
7627 | // Darwin passes an undocumented fourth argument of type char**. If | ||||
7628 | // other platforms start sprouting these, the logic below will start | ||||
7629 | // getting shifty. | ||||
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 7630 | if (nparams == 4 && Context.getTargetInfo().getTriple().isOSDarwin()) |
John McCall | 6675586 | 2009-12-24 09:58:38 +0000 | [diff] [blame] | 7631 | HasExtraParameters = false; |
7632 | |||||
7633 | if (HasExtraParameters) { | ||||
John McCall | 13591ed | 2009-07-25 04:36:53 +0000 | [diff] [blame] | 7634 | Diag(FD->getLocation(), diag::err_main_surplus_args) << nparams; |
7635 | FD->setInvalidDecl(true); | ||||
7636 | nparams = 3; | ||||
7637 | } | ||||
7638 | |||||
7639 | // FIXME: a lot of the following diagnostics would be improved | ||||
7640 | // if we had some location information about types. | ||||
7641 | |||||
7642 | QualType CharPP = | ||||
7643 | Context.getPointerType(Context.getPointerType(Context.CharTy)); | ||||
John McCall | 6675586 | 2009-12-24 09:58:38 +0000 | [diff] [blame] | 7644 | QualType Expected[] = { Context.IntTy, CharPP, CharPP, CharPP }; |
John McCall | 13591ed | 2009-07-25 04:36:53 +0000 | [diff] [blame] | 7645 | |
7646 | for (unsigned i = 0; i < nparams; ++i) { | ||||
7647 | QualType AT = FTP->getArgType(i); | ||||
7648 | |||||
7649 | bool mismatch = true; | ||||
7650 | |||||
7651 | if (Context.hasSameUnqualifiedType(AT, Expected[i])) | ||||
7652 | mismatch = false; | ||||
7653 | else if (Expected[i] == CharPP) { | ||||
7654 | // As an extension, the following forms are okay: | ||||
7655 | // char const ** | ||||
7656 | // char const * const * | ||||
7657 | // char * const * | ||||
7658 | |||||
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 7659 | QualifierCollector qs; |
John McCall | 13591ed | 2009-07-25 04:36:53 +0000 | [diff] [blame] | 7660 | const PointerType* PT; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 7661 | if ((PT = qs.strip(AT)->getAs<PointerType>()) && |
7662 | (PT = qs.strip(PT->getPointeeType())->getAs<PointerType>()) && | ||||
Richard Smith | 485b312 | 2013-01-29 02:49:47 +0000 | [diff] [blame] | 7663 | Context.hasSameType(QualType(qs.strip(PT->getPointeeType()), 0), |
7664 | Context.CharTy)) { | ||||
John McCall | 13591ed | 2009-07-25 04:36:53 +0000 | [diff] [blame] | 7665 | qs.removeConst(); |
7666 | mismatch = !qs.empty(); | ||||
7667 | } | ||||
7668 | } | ||||
7669 | |||||
7670 | if (mismatch) { | ||||
7671 | Diag(FD->getLocation(), diag::err_main_arg_wrong) << i << Expected[i]; | ||||
7672 | // TODO: suggest replacing given type with expected type | ||||
7673 | FD->setInvalidDecl(true); | ||||
7674 | } | ||||
7675 | } | ||||
7676 | |||||
7677 | if (nparams == 1 && !FD->isInvalidDecl()) { | ||||
7678 | Diag(FD->getLocation(), diag::warn_main_one_arg); | ||||
7679 | } | ||||
Douglas Gregor | 0bab54c | 2010-10-21 16:57:46 +0000 | [diff] [blame] | 7680 | |
7681 | if (!FD->isInvalidDecl() && FD->getDescribedFunctionTemplate()) { | ||||
7682 | Diag(FD->getLocation(), diag::err_main_template_decl); | ||||
7683 | FD->setInvalidDecl(); | ||||
7684 | } | ||||
John McCall | 8c4859a | 2009-07-24 03:03:21 +0000 | [diff] [blame] | 7685 | } |
7686 | |||||
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 7687 | bool Sema::CheckForConstantInitializer(Expr *Init, QualType DclT) { |
Eli Friedman | 3b8a36a | 2009-02-27 04:17:12 +0000 | [diff] [blame] | 7688 | // FIXME: Need strict checking. In C89, we need to check for |
7689 | // any assignment, increment, decrement, function-calls, or | ||||
7690 | // commas outside of a sizeof. In C99, it's the same list, | ||||
7691 | // except that the aforementioned are allowed in unevaluated | ||||
7692 | // expressions. Everything else falls under the | ||||
7693 | // "may accept other forms of constant expressions" exception. | ||||
7694 | // (We never end up here for C++, so the constant expression | ||||
7695 | // rules there don't matter.) | ||||
John McCall | 4204f07 | 2010-08-02 21:13:48 +0000 | [diff] [blame] | 7696 | if (Init->isConstantInitializer(Context, false)) |
Eli Friedman | 578a972 | 2009-02-22 06:45:27 +0000 | [diff] [blame] | 7697 | return false; |
Eli Friedman | 2129828 | 2009-02-26 04:47:58 +0000 | [diff] [blame] | 7698 | Diag(Init->getExprLoc(), diag::err_init_element_not_constant) |
7699 | << Init->getSourceRange(); | ||||
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 7700 | return true; |
Steve Naroff | d0091aa | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 7701 | } |
7702 | |||||
Chandler Carruth | a7689ef | 2011-03-27 09:46:56 +0000 | [diff] [blame] | 7703 | namespace { |
7704 | // Visits an initialization expression to see if OrigDecl is evaluated in | ||||
7705 | // its own initialization and throws a warning if it does. | ||||
7706 | class SelfReferenceChecker | ||||
7707 | : public EvaluatedExprVisitor<SelfReferenceChecker> { | ||||
7708 | Sema &S; | ||||
7709 | Decl *OrigDecl; | ||||
Richard Trieu | 898267f | 2011-09-01 21:44:13 +0000 | [diff] [blame] | 7710 | bool isRecordType; |
7711 | bool isPODType; | ||||
Hans Wennborg | 8be9e77 | 2012-08-17 10:12:33 +0000 | [diff] [blame] | 7712 | bool isReferenceType; |
Chandler Carruth | a7689ef | 2011-03-27 09:46:56 +0000 | [diff] [blame] | 7713 | |
7714 | public: | ||||
7715 | typedef EvaluatedExprVisitor<SelfReferenceChecker> Inherited; | ||||
7716 | |||||
7717 | SelfReferenceChecker(Sema &S, Decl *OrigDecl) : Inherited(S.Context), | ||||
Richard Trieu | 898267f | 2011-09-01 21:44:13 +0000 | [diff] [blame] | 7718 | S(S), OrigDecl(OrigDecl) { |
7719 | isPODType = false; | ||||
7720 | isRecordType = false; | ||||
Hans Wennborg | 8be9e77 | 2012-08-17 10:12:33 +0000 | [diff] [blame] | 7721 | isReferenceType = false; |
Richard Trieu | 898267f | 2011-09-01 21:44:13 +0000 | [diff] [blame] | 7722 | if (ValueDecl *VD = dyn_cast<ValueDecl>(OrigDecl)) { |
7723 | isPODType = VD->getType().isPODType(S.Context); | ||||
7724 | isRecordType = VD->getType()->isRecordType(); | ||||
Hans Wennborg | 8be9e77 | 2012-08-17 10:12:33 +0000 | [diff] [blame] | 7725 | isReferenceType = VD->getType()->isReferenceType(); |
Richard Trieu | 898267f | 2011-09-01 21:44:13 +0000 | [diff] [blame] | 7726 | } |
7727 | } | ||||
Chandler Carruth | a7689ef | 2011-03-27 09:46:56 +0000 | [diff] [blame] | 7728 | |
Richard Trieu | 7e9f8af | 2012-05-09 00:21:34 +0000 | [diff] [blame] | 7729 | // For most expressions, the cast is directly above the DeclRefExpr. |
7730 | // For conditional operators, the cast can be outside the conditional | ||||
7731 | // operator if both expressions are DeclRefExpr's. | ||||
7732 | void HandleValue(Expr *E) { | ||||
Richard Trieu | 568f785 | 2012-10-01 17:39:51 +0000 | [diff] [blame] | 7733 | if (isReferenceType) |
7734 | return; | ||||
Richard Trieu | 7e9f8af | 2012-05-09 00:21:34 +0000 | [diff] [blame] | 7735 | E = E->IgnoreParenImpCasts(); |
7736 | if (DeclRefExpr* DRE = dyn_cast<DeclRefExpr>(E)) { | ||||
7737 | HandleDeclRefExpr(DRE); | ||||
7738 | return; | ||||
7739 | } | ||||
7740 | |||||
7741 | if (ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E)) { | ||||
7742 | HandleValue(CO->getTrueExpr()); | ||||
7743 | HandleValue(CO->getFalseExpr()); | ||||
Richard Trieu | 6b2cc42 | 2012-10-03 00:41:36 +0000 | [diff] [blame] | 7744 | return; |
7745 | } | ||||
7746 | |||||
7747 | if (isa<MemberExpr>(E)) { | ||||
7748 | Expr *Base = E->IgnoreParenImpCasts(); | ||||
7749 | while (MemberExpr *ME = dyn_cast<MemberExpr>(Base)) { | ||||
7750 | // Check for static member variables and don't warn on them. | ||||
7751 | if (!isa<FieldDecl>(ME->getMemberDecl())) | ||||
7752 | return; | ||||
7753 | Base = ME->getBase()->IgnoreParenImpCasts(); | ||||
7754 | } | ||||
7755 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Base)) | ||||
7756 | HandleDeclRefExpr(DRE); | ||||
7757 | return; | ||||
Richard Trieu | 7e9f8af | 2012-05-09 00:21:34 +0000 | [diff] [blame] | 7758 | } |
7759 | } | ||||
7760 | |||||
Richard Trieu | 568f785 | 2012-10-01 17:39:51 +0000 | [diff] [blame] | 7761 | // Reference types are handled here since all uses of references are |
7762 | // bad, not just r-value uses. | ||||
7763 | void VisitDeclRefExpr(DeclRefExpr *E) { | ||||
7764 | if (isReferenceType) | ||||
7765 | HandleDeclRefExpr(E); | ||||
7766 | } | ||||
7767 | |||||
Richard Trieu | 7e9f8af | 2012-05-09 00:21:34 +0000 | [diff] [blame] | 7768 | void VisitImplicitCastExpr(ImplicitCastExpr *E) { |
Richard Trieu | 6b2cc42 | 2012-10-03 00:41:36 +0000 | [diff] [blame] | 7769 | if (E->getCastKind() == CK_LValueToRValue || |
Richard Trieu | 7e9f8af | 2012-05-09 00:21:34 +0000 | [diff] [blame] | 7770 | (isRecordType && E->getCastKind() == CK_NoOp)) |
7771 | HandleValue(E->getSubExpr()); | ||||
7772 | |||||
7773 | Inherited::VisitImplicitCastExpr(E); | ||||
Chandler Carruth | a7689ef | 2011-03-27 09:46:56 +0000 | [diff] [blame] | 7774 | } |
7775 | |||||
Richard Trieu | 898267f | 2011-09-01 21:44:13 +0000 | [diff] [blame] | 7776 | void VisitMemberExpr(MemberExpr *E) { |
Richard Trieu | 7e9f8af | 2012-05-09 00:21:34 +0000 | [diff] [blame] | 7777 | // Don't warn on arrays since they can be treated as pointers. |
Richard Trieu | 47eb898 | 2011-09-07 00:58:53 +0000 | [diff] [blame] | 7778 | if (E->getType()->canDecayToPointerType()) return; |
Richard Trieu | 7e9f8af | 2012-05-09 00:21:34 +0000 | [diff] [blame] | 7779 | |
Richard Trieu | 6b2cc42 | 2012-10-03 00:41:36 +0000 | [diff] [blame] | 7780 | // Warn when a non-static method call is followed by non-static member |
7781 | // field accesses, which is followed by a DeclRefExpr. | ||||
7782 | CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(E->getMemberDecl()); | ||||
7783 | bool Warn = (MD && !MD->isStatic()); | ||||
7784 | Expr *Base = E->getBase()->IgnoreParenImpCasts(); | ||||
7785 | while (MemberExpr *ME = dyn_cast<MemberExpr>(Base)) { | ||||
7786 | if (!isa<FieldDecl>(ME->getMemberDecl())) | ||||
7787 | Warn = false; | ||||
7788 | Base = ME->getBase()->IgnoreParenImpCasts(); | ||||
7789 | } | ||||
Richard Trieu | 898267f | 2011-09-01 21:44:13 +0000 | [diff] [blame] | 7790 | |
Richard Trieu | 6b2cc42 | 2012-10-03 00:41:36 +0000 | [diff] [blame] | 7791 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Base)) { |
7792 | if (Warn) | ||||
7793 | HandleDeclRefExpr(DRE); | ||||
7794 | return; | ||||
7795 | } | ||||
7796 | |||||
7797 | // The base of a MemberExpr is not a MemberExpr or a DeclRefExpr. | ||||
7798 | // Visit that expression. | ||||
7799 | Visit(Base); | ||||
Chandler Carruth | a7689ef | 2011-03-27 09:46:56 +0000 | [diff] [blame] | 7800 | } |
7801 | |||||
Richard Trieu | 8af742a | 2013-03-26 03:41:40 +0000 | [diff] [blame] | 7802 | void VisitCXXOperatorCallExpr(CXXOperatorCallExpr *E) { |
7803 | if (E->getNumArgs() > 0) | ||||
7804 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E->getArg(0))) | ||||
7805 | HandleDeclRefExpr(DRE); | ||||
7806 | |||||
7807 | Inherited::VisitCXXOperatorCallExpr(E); | ||||
7808 | } | ||||
7809 | |||||
Richard Trieu | 898267f | 2011-09-01 21:44:13 +0000 | [diff] [blame] | 7810 | void VisitUnaryOperator(UnaryOperator *E) { |
7811 | // For POD record types, addresses of its own members are well-defined. | ||||
Richard Trieu | 6b2cc42 | 2012-10-03 00:41:36 +0000 | [diff] [blame] | 7812 | if (E->getOpcode() == UO_AddrOf && isRecordType && |
7813 | isa<MemberExpr>(E->getSubExpr()->IgnoreParens())) { | ||||
7814 | if (!isPODType) | ||||
7815 | HandleValue(E->getSubExpr()); | ||||
7816 | return; | ||||
7817 | } | ||||
Richard Trieu | 898267f | 2011-09-01 21:44:13 +0000 | [diff] [blame] | 7818 | Inherited::VisitUnaryOperator(E); |
Richard Smith | 0f2fc5f | 2013-05-03 19:16:22 +0000 | [diff] [blame] | 7819 | } |
Richard Trieu | 7e9f8af | 2012-05-09 00:21:34 +0000 | [diff] [blame] | 7820 | |
7821 | void VisitObjCMessageExpr(ObjCMessageExpr *E) { return; } | ||||
7822 | |||||
Richard Trieu | 898267f | 2011-09-01 21:44:13 +0000 | [diff] [blame] | 7823 | void HandleDeclRefExpr(DeclRefExpr *DRE) { |
NAKAMURA Takumi | f305279 | 2013-01-19 01:54:35 +0000 | [diff] [blame] | 7824 | Decl* ReferenceDecl = DRE->getDecl(); |
Chandler Carruth | a7689ef | 2011-03-27 09:46:56 +0000 | [diff] [blame] | 7825 | if (OrigDecl != ReferenceDecl) return; |
Ted Kremenek | 39371b8 | 2013-01-19 04:33:14 +0000 | [diff] [blame] | 7826 | unsigned diag; |
7827 | if (isReferenceType) { | ||||
7828 | diag = diag::warn_uninit_self_reference_in_reference_init; | ||||
7829 | } else if (cast<VarDecl>(OrigDecl)->isStaticLocal()) { | ||||
7830 | diag = diag::warn_static_self_reference_in_init; | ||||
7831 | } else { | ||||
7832 | diag = diag::warn_uninit_self_reference_in_init; | ||||
7833 | } | ||||
7834 | |||||
Richard Trieu | 898267f | 2011-09-01 21:44:13 +0000 | [diff] [blame] | 7835 | S.DiagRuntimeBehavior(DRE->getLocStart(), DRE, |
Hans Wennborg | 5965b7c | 2012-08-20 08:52:22 +0000 | [diff] [blame] | 7836 | S.PDiag(diag) |
Hans Wennborg | 7821e07 | 2012-09-21 08:58:33 +0000 | [diff] [blame] | 7837 | << DRE->getNameInfo().getName() |
Douglas Gregor | 63fe681 | 2011-05-24 16:02:01 +0000 | [diff] [blame] | 7838 | << OrigDecl->getLocation() |
Richard Trieu | 898267f | 2011-09-01 21:44:13 +0000 | [diff] [blame] | 7839 | << DRE->getSourceRange()); |
Chandler Carruth | a7689ef | 2011-03-27 09:46:56 +0000 | [diff] [blame] | 7840 | } |
7841 | }; | ||||
Chandler Carruth | a7689ef | 2011-03-27 09:46:56 +0000 | [diff] [blame] | 7842 | |
Richard Trieu | 568f785 | 2012-10-01 17:39:51 +0000 | [diff] [blame] | 7843 | /// CheckSelfReference - Warns if OrigDecl is used in expression E. |
7844 | static void CheckSelfReference(Sema &S, Decl* OrigDecl, Expr *E, | ||||
7845 | bool DirectInit) { | ||||
7846 | // Parameters arguments are occassionially constructed with itself, | ||||
7847 | // for instance, in recursive functions. Skip them. | ||||
7848 | if (isa<ParmVarDecl>(OrigDecl)) | ||||
7849 | return; | ||||
7850 | |||||
7851 | E = E->IgnoreParens(); | ||||
7852 | |||||
7853 | // Skip checking T a = a where T is not a record or reference type. | ||||
7854 | // Doing so is a way to silence uninitialized warnings. | ||||
7855 | if (!DirectInit && !cast<VarDecl>(OrigDecl)->getType()->isRecordType()) | ||||
7856 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) | ||||
7857 | if (ICE->getCastKind() == CK_LValueToRValue) | ||||
7858 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr())) | ||||
7859 | if (DRE->getDecl() == OrigDecl) | ||||
7860 | return; | ||||
7861 | |||||
7862 | SelfReferenceChecker(S, OrigDecl).Visit(E); | ||||
7863 | } | ||||
Richard Trieu | 898267f | 2011-09-01 21:44:13 +0000 | [diff] [blame] | 7864 | } |
7865 | |||||
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 7866 | /// AddInitializerToDecl - Adds the initializer Init to the |
7867 | /// declaration dcl. If DirectInit is true, this is C++ direct | ||||
7868 | /// initialization rather than copy initialization. | ||||
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 7869 | void Sema::AddInitializerToDecl(Decl *RealDecl, Expr *Init, |
7870 | bool DirectInit, bool TypeMayContainAuto) { | ||||
Chris Lattner | 9a11b9a | 2007-10-19 20:10:30 +0000 | [diff] [blame] | 7871 | // If there is no declaration, there was an error parsing it. Just ignore |
7872 | // the initializer. | ||||
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 7873 | if (RealDecl == 0 || RealDecl->isInvalidDecl()) |
Chris Lattner | 9a11b9a | 2007-10-19 20:10:30 +0000 | [diff] [blame] | 7874 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7875 | |
Douglas Gregor | 021c3b3 | 2009-03-11 23:00:04 +0000 | [diff] [blame] | 7876 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(RealDecl)) { |
7877 | // With declarators parsed the way they are, the parser cannot | ||||
7878 | // distinguish between a normal initializer and a pure-specifier. | ||||
7879 | // Thus this grotesque test. | ||||
7880 | IntegerLiteral *IL; | ||||
Douglas Gregor | 021c3b3 | 2009-03-11 23:00:04 +0000 | [diff] [blame] | 7881 | if ((IL = dyn_cast<IntegerLiteral>(Init)) && IL->getValue() == 0 && |
Douglas Gregor | 4ba3136 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 7882 | Context.getCanonicalType(IL->getType()) == Context.IntTy) |
7883 | CheckPureMethod(Method, Init->getSourceRange()); | ||||
7884 | else { | ||||
Douglas Gregor | 021c3b3 | 2009-03-11 23:00:04 +0000 | [diff] [blame] | 7885 | Diag(Method->getLocation(), diag::err_member_function_initialization) |
7886 | << Method->getDeclName() << Init->getSourceRange(); | ||||
7887 | Method->setInvalidDecl(); | ||||
7888 | } | ||||
7889 | return; | ||||
7890 | } | ||||
7891 | |||||
Steve Naroff | 410e3e2 | 2007-09-12 20:13:48 +0000 | [diff] [blame] | 7892 | VarDecl *VDecl = dyn_cast<VarDecl>(RealDecl); |
7893 | if (!VDecl) { | ||||
Richard Smith | c2cdd53 | 2011-06-12 11:43:46 +0000 | [diff] [blame] | 7894 | assert(!isa<FieldDecl>(RealDecl) && "field init shouldn't get here"); |
7895 | Diag(RealDecl->getLocation(), diag::err_illegal_initializer); | ||||
Steve Naroff | 410e3e2 | 2007-09-12 20:13:48 +0000 | [diff] [blame] | 7896 | RealDecl->setInvalidDecl(); |
7897 | return; | ||||
Eli Friedman | 3b8a36a | 2009-02-27 04:17:12 +0000 | [diff] [blame] | 7898 | } |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 7899 | ParenListExpr *CXXDirectInit = dyn_cast<ParenListExpr>(Init); |
7900 | |||||
Richard Smith | 0188872 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 7901 | // 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] | 7902 | if (TypeMayContainAuto && VDecl->getType()->isUndeducedType()) { |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 7903 | Expr *DeduceInit = Init; |
7904 | // Initializer could be a C++ direct-initializer. Deduction only works if it | ||||
7905 | // contains exactly one expression. | ||||
7906 | if (CXXDirectInit) { | ||||
7907 | if (CXXDirectInit->getNumExprs() == 0) { | ||||
7908 | // It isn't possible to write this directly, but it is possible to | ||||
7909 | // end up in this situation with "auto x(some_pack...);" | ||||
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 7910 | Diag(CXXDirectInit->getLocStart(), |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 7911 | diag::err_auto_var_init_no_expression) |
7912 | << VDecl->getDeclName() << VDecl->getType() | ||||
7913 | << VDecl->getSourceRange(); | ||||
7914 | RealDecl->setInvalidDecl(); | ||||
7915 | return; | ||||
7916 | } else if (CXXDirectInit->getNumExprs() > 1) { | ||||
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 7917 | Diag(CXXDirectInit->getExpr(1)->getLocStart(), |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 7918 | diag::err_auto_var_init_multiple_expressions) |
7919 | << VDecl->getDeclName() << VDecl->getType() | ||||
7920 | << VDecl->getSourceRange(); | ||||
7921 | RealDecl->setInvalidDecl(); | ||||
7922 | return; | ||||
7923 | } else { | ||||
7924 | DeduceInit = CXXDirectInit->getExpr(0); | ||||
7925 | } | ||||
7926 | } | ||||
Douglas Gregor | 1344e94 | 2013-03-07 22:57:58 +0000 | [diff] [blame] | 7927 | |
7928 | // Expressions default to 'id' when we're in a debugger. | ||||
7929 | bool DefaultedToAuto = false; | ||||
7930 | if (getLangOpts().DebuggerCastResultToId && | ||||
7931 | Init->getType() == Context.UnknownAnyTy) { | ||||
7932 | ExprResult Result = forceUnknownAnyToType(Init, Context.getObjCIdType()); | ||||
7933 | if (Result.isInvalid()) { | ||||
7934 | VDecl->setInvalidDecl(); | ||||
7935 | return; | ||||
7936 | } | ||||
7937 | Init = Result.take(); | ||||
7938 | DefaultedToAuto = true; | ||||
7939 | } | ||||
Richard Smith | 9b13175 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 7940 | |
7941 | QualType DeducedType; | ||||
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 7942 | if (DeduceAutoType(VDecl->getTypeSourceInfo(), DeduceInit, DeducedType) == |
Sebastian Redl | b832f6d | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 7943 | DAR_Failed) |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 7944 | DiagnoseAutoDeductionFailure(VDecl, DeduceInit); |
Richard Smith | 9b13175 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 7945 | if (DeducedType.isNull()) { |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 7946 | RealDecl->setInvalidDecl(); |
7947 | return; | ||||
7948 | } | ||||
Richard Smith | 9b13175 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 7949 | VDecl->setType(DeducedType); |
Rafael Espindola | 2d1b096 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 7950 | assert(VDecl->isLinkageValid()); |
Rafael Espindola | 2d9e883 | 2013-03-12 21:06:00 +0000 | [diff] [blame] | 7951 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7952 | // In ARC, infer lifetime. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7953 | if (getLangOpts().ObjCAutoRefCount && inferObjCARCLifetime(VDecl)) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7954 | VDecl->setInvalidDecl(); |
7955 | |||||
Jordan Rose | 0abbdfe | 2012-06-08 22:46:07 +0000 | [diff] [blame] | 7956 | // Warn if we deduced 'id'. 'auto' usually implies type-safety, but using |
7957 | // 'id' instead of a specific object type prevents most of our usual checks. | ||||
7958 | // We only want to warn outside of template instantiations, though: | ||||
7959 | // inside a template, the 'id' could have come from a parameter. | ||||
Douglas Gregor | 1344e94 | 2013-03-07 22:57:58 +0000 | [diff] [blame] | 7960 | if (ActiveTemplateInstantiations.empty() && !DefaultedToAuto && |
Richard Smith | 9b13175 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 7961 | DeducedType->isObjCIdType()) { |
7962 | SourceLocation Loc = | ||||
7963 | VDecl->getTypeSourceInfo()->getTypeLoc().getBeginLoc(); | ||||
Jordan Rose | 0abbdfe | 2012-06-08 22:46:07 +0000 | [diff] [blame] | 7964 | Diag(Loc, diag::warn_auto_var_is_id) |
7965 | << VDecl->getDeclName() << DeduceInit->getSourceRange(); | ||||
7966 | } | ||||
7967 | |||||
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 7968 | // If this is a redeclaration, check that the type we just deduced matches |
7969 | // the previously declared type. | ||||
Richard Smith | dd9459f | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 7970 | if (VarDecl *Old = VDecl->getPreviousDecl()) { |
7971 | // We never need to merge the type, because we cannot form an incomplete | ||||
7972 | // array of auto, nor deduce such a type. | ||||
7973 | MergeVarDeclTypes(VDecl, Old, /*MergeTypeWithPrevious*/false); | ||||
7974 | } | ||||
Richard Smith | dc7a4f5 | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 7975 | |
7976 | // Check the deduced type is valid for a variable declaration. | ||||
7977 | CheckVariableDeclarationType(VDecl); | ||||
7978 | if (VDecl->isInvalidDecl()) | ||||
7979 | return; | ||||
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 7980 | } |
Richard Smith | 0188872 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 7981 | |
7982 | if (VDecl->isLocalVarDecl() && VDecl->hasExternalStorage()) { | ||||
7983 | // C99 6.7.8p5. C++ has no such restriction, but that is a defect. | ||||
7984 | Diag(VDecl->getLocation(), diag::err_block_extern_cant_init); | ||||
7985 | VDecl->setInvalidDecl(); | ||||
7986 | return; | ||||
7987 | } | ||||
7988 | |||||
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 7989 | if (!VDecl->getType()->isDependentType()) { |
7990 | // A definition must end up with a complete type, which means it must be | ||||
7991 | // complete with the restriction that an array type might be completed by | ||||
7992 | // the initializer; note that later code assumes this restriction. | ||||
7993 | QualType BaseDeclType = VDecl->getType(); | ||||
7994 | if (const ArrayType *Array = Context.getAsIncompleteArrayType(BaseDeclType)) | ||||
7995 | BaseDeclType = Array->getElementType(); | ||||
7996 | if (RequireCompleteType(VDecl->getLocation(), BaseDeclType, | ||||
7997 | diag::err_typecheck_decl_incomplete_type)) { | ||||
7998 | RealDecl->setInvalidDecl(); | ||||
7999 | return; | ||||
8000 | } | ||||
Douglas Gregor | 3a91abf | 2010-08-24 05:27:49 +0000 | [diff] [blame] | 8001 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 8002 | // The variable can not have an abstract class type. |
8003 | if (RequireNonAbstractType(VDecl->getLocation(), VDecl->getType(), | ||||
8004 | diag::err_abstract_type_in_decl, | ||||
8005 | AbstractVariableType)) | ||||
8006 | VDecl->setInvalidDecl(); | ||||
Eli Friedman | a31feca | 2009-04-13 21:28:54 +0000 | [diff] [blame] | 8007 | } |
8008 | |||||
Sebastian Redl | 31310a2 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 8009 | const VarDecl *Def; |
8010 | if ((Def = VDecl->getDefinition()) && Def != VDecl) { | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8011 | Diag(VDecl->getLocation(), diag::err_redefinition) |
Douglas Gregor | 275a369 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 8012 | << VDecl->getDeclName(); |
8013 | Diag(Def->getLocation(), diag::note_previous_definition); | ||||
8014 | VDecl->setInvalidDecl(); | ||||
8015 | return; | ||||
8016 | } | ||||
Douglas Gregor | 3a91abf | 2010-08-24 05:27:49 +0000 | [diff] [blame] | 8017 | |
Douglas Gregor | 3a91abf | 2010-08-24 05:27:49 +0000 | [diff] [blame] | 8018 | const VarDecl* PrevInit = 0; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8019 | if (getLangOpts().CPlusPlus) { |
Douglas Gregor | a31040f | 2010-12-16 01:31:22 +0000 | [diff] [blame] | 8020 | // C++ [class.static.data]p4 |
8021 | // If a static data member is of const integral or const | ||||
8022 | // enumeration type, its declaration in the class definition can | ||||
8023 | // specify a constant-initializer which shall be an integral | ||||
8024 | // constant expression (5.19). In that case, the member can appear | ||||
8025 | // in integral constant expressions. The member shall still be | ||||
8026 | // defined in a namespace scope if it is used in the program and the | ||||
8027 | // namespace scope definition shall not contain an initializer. | ||||
8028 | // | ||||
8029 | // We already performed a redefinition check above, but for static | ||||
8030 | // data members we also need to check whether there was an in-class | ||||
8031 | // declaration with an initializer. | ||||
8032 | if (VDecl->isStaticDataMember() && VDecl->getAnyInitializer(PrevInit)) { | ||||
David Blaikie | d662a79 | 2011-10-19 22:56:21 +0000 | [diff] [blame] | 8033 | Diag(VDecl->getLocation(), diag::err_redefinition) |
8034 | << VDecl->getDeclName(); | ||||
Douglas Gregor | a31040f | 2010-12-16 01:31:22 +0000 | [diff] [blame] | 8035 | Diag(PrevInit->getLocation(), diag::note_previous_definition); |
8036 | return; | ||||
8037 | } | ||||
Douglas Gregor | 275a369 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 8038 | |
Douglas Gregor | a31040f | 2010-12-16 01:31:22 +0000 | [diff] [blame] | 8039 | if (VDecl->hasLocalStorage()) |
8040 | getCurFunction()->setHasBranchProtectedScope(); | ||||
8041 | |||||
8042 | if (DiagnoseUnexpandedParameterPack(Init, UPPC_Initializer)) { | ||||
8043 | VDecl->setInvalidDecl(); | ||||
8044 | return; | ||||
8045 | } | ||||
8046 | } | ||||
John McCall | e46f62c | 2010-08-01 01:24:59 +0000 | [diff] [blame] | 8047 | |
Peter Collingbourne | 8c25fc5 | 2011-09-19 21:14:35 +0000 | [diff] [blame] | 8048 | // OpenCL 1.1 6.5.2: "Variables allocated in the __local address space inside |
8049 | // a kernel function cannot be initialized." | ||||
8050 | if (VDecl->getStorageClass() == SC_OpenCLWorkGroupLocal) { | ||||
8051 | Diag(VDecl->getLocation(), diag::err_local_cant_init); | ||||
8052 | VDecl->setInvalidDecl(); | ||||
8053 | return; | ||||
8054 | } | ||||
8055 | |||||
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 8056 | // Get the decls type and save a reference for later, since |
Steve Naroff | d0091aa | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 8057 | // CheckInitializerTypes may change it. |
Steve Naroff | 410e3e2 | 2007-09-12 20:13:48 +0000 | [diff] [blame] | 8058 | QualType DclT = VDecl->getType(), SavT = DclT; |
Fariborz Jahanian | 509fb3e | 2012-03-09 18:47:16 +0000 | [diff] [blame] | 8059 | |
Douglas Gregor | 1344e94 | 2013-03-07 22:57:58 +0000 | [diff] [blame] | 8060 | // Expressions default to 'id' when we're in a debugger |
8061 | // and we are assigning it to a variable of Objective-C pointer type. | ||||
8062 | if (getLangOpts().DebuggerCastResultToId && DclT->isObjCObjectPointerType() && | ||||
8063 | Init->getType() == Context.UnknownAnyTy) { | ||||
8064 | ExprResult Result = forceUnknownAnyToType(Init, Context.getObjCIdType()); | ||||
8065 | if (Result.isInvalid()) { | ||||
8066 | VDecl->setInvalidDecl(); | ||||
8067 | return; | ||||
Fariborz Jahanian | 509fb3e | 2012-03-09 18:47:16 +0000 | [diff] [blame] | 8068 | } |
Douglas Gregor | 1344e94 | 2013-03-07 22:57:58 +0000 | [diff] [blame] | 8069 | Init = Result.take(); |
8070 | } | ||||
Richard Smith | 0188872 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8071 | |
8072 | // Perform the initialization. | ||||
8073 | if (!VDecl->isInvalidDecl()) { | ||||
8074 | InitializedEntity Entity = InitializedEntity::InitializeVariable(VDecl); | ||||
8075 | InitializationKind Kind | ||||
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 8076 | = DirectInit ? |
8077 | CXXDirectInit ? InitializationKind::CreateDirect(VDecl->getLocation(), | ||||
8078 | Init->getLocStart(), | ||||
8079 | Init->getLocEnd()) | ||||
8080 | : InitializationKind::CreateDirectList( | ||||
8081 | VDecl->getLocation()) | ||||
Richard Smith | 0188872 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8082 | : InitializationKind::CreateCopy(VDecl->getLocation(), |
8083 | Init->getLocStart()); | ||||
8084 | |||||
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 8085 | MultiExprArg Args = Init; |
8086 | if (CXXDirectInit) | ||||
8087 | Args = MultiExprArg(CXXDirectInit->getExprs(), | ||||
8088 | CXXDirectInit->getNumExprs()); | ||||
8089 | |||||
8090 | InitializationSequence InitSeq(*this, Entity, Kind, Args); | ||||
8091 | ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Args, &DclT); | ||||
Richard Smith | 0188872 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8092 | if (Result.isInvalid()) { |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 8093 | VDecl->setInvalidDecl(); |
Richard Smith | 0188872 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8094 | return; |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 8095 | } |
Richard Smith | 0188872 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8096 | |
8097 | Init = Result.takeAs<Expr>(); | ||||
8098 | } | ||||
8099 | |||||
Richard Trieu | 568f785 | 2012-10-01 17:39:51 +0000 | [diff] [blame] | 8100 | // Check for self-references within variable initializers. |
8101 | // Variables declared within a function/method body (except for references) | ||||
8102 | // are handled by a dataflow analysis. | ||||
8103 | if (!VDecl->hasLocalStorage() || VDecl->getType()->isRecordType() || | ||||
8104 | VDecl->getType()->isReferenceType()) { | ||||
8105 | CheckSelfReference(*this, RealDecl, Init, DirectInit); | ||||
8106 | } | ||||
8107 | |||||
Richard Smith | 0188872 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8108 | // If the type changed, it means we had an incomplete type that was |
8109 | // completed by the initializer. For example: | ||||
8110 | // int ary[] = { 1, 3, 5 }; | ||||
John McCall | 7307643 | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 8111 | // "ary" transitions from an IncompleteArrayType to a ConstantArrayType. |
Eli Friedman | 5c89c39 | 2012-02-23 02:25:10 +0000 | [diff] [blame] | 8112 | if (!VDecl->isInvalidDecl() && (DclT != SavT)) |
Richard Smith | 0188872 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8113 | VDecl->setType(DclT); |
Richard Smith | 0188872 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8114 | |
Jordan Rose | e10f4d3 | 2012-09-15 02:48:31 +0000 | [diff] [blame] | 8115 | if (!VDecl->isInvalidDecl()) { |
Richard Smith | 0188872 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8116 | checkUnsafeAssigns(VDecl->getLocation(), VDecl->getType(), Init); |
8117 | |||||
Jordan Rose | e10f4d3 | 2012-09-15 02:48:31 +0000 | [diff] [blame] | 8118 | if (VDecl->hasAttr<BlocksAttr>()) |
8119 | checkRetainCycles(VDecl, Init); | ||||
Jordan Rose | 58b6bdc | 2012-09-28 22:21:30 +0000 | [diff] [blame] | 8120 | |
8121 | // It is safe to assign a weak reference into a strong variable. | ||||
8122 | // Although this code can still have problems: | ||||
8123 | // id x = self.weakProp; | ||||
8124 | // id y = self.weakProp; | ||||
8125 | // we do not warn to warn spuriously when 'x' and 'y' are on separate | ||||
8126 | // paths through the function. This should be revisited if | ||||
8127 | // -Wrepeated-use-of-weak is made flow-sensitive. | ||||
Ted Kremenek | 904a326 | 2012-12-20 22:31:27 +0000 | [diff] [blame] | 8128 | if (VDecl->getType().getObjCLifetime() == Qualifiers::OCL_Strong) { |
Jordan Rose | 58b6bdc | 2012-09-28 22:21:30 +0000 | [diff] [blame] | 8129 | DiagnosticsEngine::Level Level = |
8130 | Diags.getDiagnosticLevel(diag::warn_arc_repeated_use_of_weak, | ||||
8131 | Init->getLocStart()); | ||||
8132 | if (Level != DiagnosticsEngine::Ignored) | ||||
8133 | getCurFunction()->markSafeWeakUse(Init); | ||||
8134 | } | ||||
Jordan Rose | e10f4d3 | 2012-09-15 02:48:31 +0000 | [diff] [blame] | 8135 | } |
8136 | |||||
Richard Smith | 4195637 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 8137 | // The initialization is usually a full-expression. |
8138 | // | ||||
8139 | // FIXME: If this is a braced initialization of an aggregate, it is not | ||||
8140 | // an expression, and each individual field initializer is a separate | ||||
8141 | // full-expression. For instance, in: | ||||
8142 | // | ||||
8143 | // struct Temp { ~Temp(); }; | ||||
8144 | // struct S { S(Temp); }; | ||||
8145 | // struct T { S a, b; } t = { Temp(), Temp() } | ||||
8146 | // | ||||
8147 | // we should destroy the first Temp before constructing the second. | ||||
Fariborz Jahanian | ad48a50 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 8148 | ExprResult Result = ActOnFinishFullExpr(Init, VDecl->getLocation(), |
8149 | false, | ||||
8150 | VDecl->isConstexpr()); | ||||
Richard Smith | 4195637 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 8151 | if (Result.isInvalid()) { |
8152 | VDecl->setInvalidDecl(); | ||||
8153 | return; | ||||
8154 | } | ||||
8155 | Init = Result.take(); | ||||
8156 | |||||
Richard Smith | 0188872 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8157 | // Attach the initializer to the decl. |
8158 | VDecl->setInit(Init); | ||||
8159 | |||||
8160 | if (VDecl->isLocalVarDecl()) { | ||||
8161 | // C99 6.7.8p4: All the expressions in an initializer for an object that has | ||||
8162 | // static storage duration shall be constant expressions or string literals. | ||||
8163 | // C++ does not have this restriction. | ||||
Enea Zaffanella | b9a5935 | 2013-07-22 10:58:26 +0000 | [diff] [blame] | 8164 | if (!getLangOpts().CPlusPlus && !VDecl->isInvalidDecl()) { |
8165 | if (VDecl->getStorageClass() == SC_Static) | ||||
8166 | CheckForConstantInitializer(Init, DclT); | ||||
8167 | // C89 is stricter than C99 for non-static aggregate types. | ||||
8168 | // C89 6.5.7p3: All the expressions [...] in an initializer list | ||||
8169 | // for an object that has aggregate or union type shall be | ||||
8170 | // constant expressions. | ||||
8171 | else if (!getLangOpts().C99 && VDecl->getType()->isAggregateType() && | ||||
Enea Zaffanella | 8202630 | 2013-07-22 19:10:20 +0000 | [diff] [blame] | 8172 | isa<InitListExpr>(Init) && |
Enea Zaffanella | b9a5935 | 2013-07-22 10:58:26 +0000 | [diff] [blame] | 8173 | !Init->isConstantInitializer(Context, false)) |
8174 | Diag(Init->getExprLoc(), | ||||
8175 | diag::ext_aggregate_init_not_constant) | ||||
8176 | << Init->getSourceRange(); | ||||
8177 | } | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8178 | } else if (VDecl->isStaticDataMember() && |
Douglas Gregor | 021c3b3 | 2009-03-11 23:00:04 +0000 | [diff] [blame] | 8179 | VDecl->getLexicalDeclContext()->isRecord()) { |
8180 | // This is an in-class initialization for a static data member, e.g., | ||||
8181 | // | ||||
8182 | // struct S { | ||||
8183 | // static const int value = 17; | ||||
8184 | // }; | ||||
8185 | |||||
Douglas Gregor | 021c3b3 | 2009-03-11 23:00:04 +0000 | [diff] [blame] | 8186 | // C++ [class.mem]p4: |
8187 | // A member-declarator can contain a constant-initializer only | ||||
8188 | // if it declares a static member (9.4) of const integral or | ||||
8189 | // const enumeration type, see 9.4.2. | ||||
Richard Smith | c6d990a | 2011-09-29 19:11:37 +0000 | [diff] [blame] | 8190 | // |
Richard Smith | 0188872 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8191 | // C++11 [class.static.data]p3: |
Richard Smith | c6d990a | 2011-09-29 19:11:37 +0000 | [diff] [blame] | 8192 | // If a non-volatile const static data member is of integral or |
8193 | // enumeration type, its declaration in the class definition can | ||||
8194 | // specify a brace-or-equal-initializer in which every initalizer-clause | ||||
8195 | // that is an assignment-expression is a constant expression. A static | ||||
8196 | // data member of literal type can be declared in the class definition | ||||
8197 | // with the constexpr specifier; if so, its declaration shall specify a | ||||
8198 | // brace-or-equal-initializer in which every initializer-clause that is | ||||
8199 | // an assignment-expression is a constant expression. | ||||
John McCall | 4e63564 | 2010-09-10 23:21:22 +0000 | [diff] [blame] | 8200 | |
8201 | // Do nothing on dependent types. | ||||
Richard Smith | 0188872 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8202 | if (DclT->isDependentType()) { |
John McCall | 4e63564 | 2010-09-10 23:21:22 +0000 | [diff] [blame] | 8203 | |
Richard Smith | c6d990a | 2011-09-29 19:11:37 +0000 | [diff] [blame] | 8204 | // Allow any 'static constexpr' members, whether or not they are of literal |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 8205 | // type. We separately check that every constexpr variable is of literal |
8206 | // type. | ||||
Richard Smith | c6d990a | 2011-09-29 19:11:37 +0000 | [diff] [blame] | 8207 | } else if (VDecl->isConstexpr()) { |
8208 | |||||
John McCall | 4e63564 | 2010-09-10 23:21:22 +0000 | [diff] [blame] | 8209 | // Require constness. |
Richard Smith | 0188872 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8210 | } else if (!DclT.isConstQualified()) { |
John McCall | 4e63564 | 2010-09-10 23:21:22 +0000 | [diff] [blame] | 8211 | Diag(VDecl->getLocation(), diag::err_in_class_initializer_non_const) |
8212 | << Init->getSourceRange(); | ||||
Douglas Gregor | 021c3b3 | 2009-03-11 23:00:04 +0000 | [diff] [blame] | 8213 | VDecl->setInvalidDecl(); |
John McCall | 4e63564 | 2010-09-10 23:21:22 +0000 | [diff] [blame] | 8214 | |
8215 | // We allow integer constant expressions in all cases. | ||||
Richard Smith | 0188872 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8216 | } else if (DclT->isIntegralOrEnumerationType()) { |
Chris Lattner | 24c38e1 | 2011-06-14 05:46:29 +0000 | [diff] [blame] | 8217 | // Check whether the expression is a constant expression. |
8218 | SourceLocation Loc; | ||||
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 8219 | if (getLangOpts().CPlusPlus11 && DclT.isVolatileQualified()) |
Richard Smith | 0188872 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8220 | // In C++11, a non-constexpr const static data member with an |
Richard Smith | 2da7a51 | 2011-09-29 21:28:14 +0000 | [diff] [blame] | 8221 | // in-class initializer cannot be volatile. |
8222 | Diag(VDecl->getLocation(), diag::err_in_class_initializer_volatile); | ||||
8223 | else if (Init->isValueDependent()) | ||||
Chris Lattner | 24c38e1 | 2011-06-14 05:46:29 +0000 | [diff] [blame] | 8224 | ; // Nothing to check. |
8225 | else if (Init->isIntegerConstantExpr(Context, &Loc)) | ||||
8226 | ; // Ok, it's an ICE! | ||||
8227 | else if (Init->isEvaluatable(Context)) { | ||||
8228 | // If we can constant fold the initializer through heroics, accept it, | ||||
8229 | // but report this as a use of an extension for -pedantic. | ||||
8230 | Diag(Loc, diag::ext_in_class_initializer_non_constant) | ||||
8231 | << Init->getSourceRange(); | ||||
8232 | } else { | ||||
8233 | // Otherwise, this is some crazy unknown case. Report the issue at the | ||||
8234 | // location provided by the isIntegerConstantExpr failed check. | ||||
8235 | Diag(Loc, diag::err_in_class_initializer_non_constant) | ||||
8236 | << Init->getSourceRange(); | ||||
8237 | VDecl->setInvalidDecl(); | ||||
John McCall | 4e63564 | 2010-09-10 23:21:22 +0000 | [diff] [blame] | 8238 | } |
8239 | |||||
Richard Smith | 0188872 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8240 | // We allow foldable floating-point constants as an extension. |
8241 | } else if (DclT->isFloatingType()) { // also permits complex, which is ok | ||||
Richard Smith | b4b1d69 | 2013-01-25 04:22:16 +0000 | [diff] [blame] | 8242 | // In C++98, this is a GNU extension. In C++11, it is not, but we support |
8243 | // it anyway and provide a fixit to add the 'constexpr'. | ||||
8244 | if (getLangOpts().CPlusPlus11) { | ||||
David Blaikie | a367e9d | 2013-01-29 22:26:08 +0000 | [diff] [blame] | 8245 | Diag(VDecl->getLocation(), |
8246 | diag::ext_in_class_initializer_float_type_cxx11) | ||||
8247 | << DclT << Init->getSourceRange(); | ||||
8248 | Diag(VDecl->getLocStart(), | ||||
8249 | diag::note_in_class_initializer_float_type_cxx11) | ||||
8250 | << FixItHint::CreateInsertion(VDecl->getLocStart(), "constexpr "); | ||||
Richard Smith | b4b1d69 | 2013-01-25 04:22:16 +0000 | [diff] [blame] | 8251 | } else { |
8252 | Diag(VDecl->getLocation(), diag::ext_in_class_initializer_float_type) | ||||
8253 | << DclT << Init->getSourceRange(); | ||||
John McCall | 4e63564 | 2010-09-10 23:21:22 +0000 | [diff] [blame] | 8254 | |
Richard Smith | b4b1d69 | 2013-01-25 04:22:16 +0000 | [diff] [blame] | 8255 | if (!Init->isValueDependent() && !Init->isEvaluatable(Context)) { |
8256 | Diag(Init->getExprLoc(), diag::err_in_class_initializer_non_constant) | ||||
8257 | << Init->getSourceRange(); | ||||
8258 | VDecl->setInvalidDecl(); | ||||
8259 | } | ||||
Douglas Gregor | 021c3b3 | 2009-03-11 23:00:04 +0000 | [diff] [blame] | 8260 | } |
Richard Smith | 947be19 | 2011-09-29 23:18:34 +0000 | [diff] [blame] | 8261 | |
Richard Smith | 0188872 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8262 | // Suggest adding 'constexpr' in C++11 for literal types. |
Richard Smith | a10b978 | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 8263 | } else if (getLangOpts().CPlusPlus11 && DclT->isLiteralType(Context)) { |
Richard Smith | 947be19 | 2011-09-29 23:18:34 +0000 | [diff] [blame] | 8264 | Diag(VDecl->getLocation(), diag::err_in_class_initializer_literal_type) |
Richard Smith | 0188872 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8265 | << DclT << Init->getSourceRange() |
Richard Smith | 947be19 | 2011-09-29 23:18:34 +0000 | [diff] [blame] | 8266 | << FixItHint::CreateInsertion(VDecl->getLocStart(), "constexpr "); |
8267 | VDecl->setConstexpr(true); | ||||
8268 | |||||
Richard Smith | c6d990a | 2011-09-29 19:11:37 +0000 | [diff] [blame] | 8269 | } else { |
8270 | Diag(VDecl->getLocation(), diag::err_in_class_initializer_bad_type) | ||||
Richard Smith | 0188872 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8271 | << DclT << Init->getSourceRange(); |
Richard Smith | c6d990a | 2011-09-29 19:11:37 +0000 | [diff] [blame] | 8272 | VDecl->setInvalidDecl(); |
Douglas Gregor | 021c3b3 | 2009-03-11 23:00:04 +0000 | [diff] [blame] | 8273 | } |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 8274 | } else if (VDecl->isFileVarDecl()) { |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 8275 | if (VDecl->getStorageClass() == SC_Extern && |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8276 | (!getLangOpts().CPlusPlus || |
Rafael Espindola | 5b34b9c | 2013-03-29 07:56:05 +0000 | [diff] [blame] | 8277 | !(Context.getBaseElementType(VDecl->getType()).isConstQualified() || |
8278 | VDecl->isExternC()))) | ||||
Steve Naroff | 410e3e2 | 2007-09-12 20:13:48 +0000 | [diff] [blame] | 8279 | Diag(VDecl->getLocation(), diag::warn_extern_init); |
Eli Friedman | a91eb54 | 2009-12-22 02:10:53 +0000 | [diff] [blame] | 8280 | |
Richard Smith | 0188872 | 2011-12-15 19:20:59 +0000 | [diff] [blame] | 8281 | // C99 6.7.8p4. All file scoped initializers need to be constant. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8282 | if (!getLangOpts().CPlusPlus && !VDecl->isInvalidDecl()) |
Anders Carlsson | c5eb731 | 2008-08-22 05:00:02 +0000 | [diff] [blame] | 8283 | CheckForConstantInitializer(Init, DclT); |
Richard Smith | 6a570f6 | 2013-04-14 20:11:31 +0000 | [diff] [blame] | 8284 | else if (VDecl->getTLSKind() == VarDecl::TLS_Static && |
8285 | !VDecl->isInvalidDecl() && !DclT->isDependentType() && | ||||
8286 | !Init->isValueDependent() && !VDecl->isConstexpr() && | ||||
Richard Smith | b6b127f | 2013-04-15 08:07:34 +0000 | [diff] [blame] | 8287 | !Init->isConstantInitializer( |
8288 | Context, VDecl->getType()->isReferenceType())) { | ||||
Richard Smith | 6a570f6 | 2013-04-14 20:11:31 +0000 | [diff] [blame] | 8289 | // GNU C++98 edits for __thread, [basic.start.init]p4: |
8290 | // An object of thread storage duration shall not require dynamic | ||||
8291 | // initialization. | ||||
8292 | // FIXME: Need strict checking here. | ||||
8293 | Diag(VDecl->getLocation(), diag::err_thread_dynamic_init); | ||||
8294 | if (getLangOpts().CPlusPlus11) | ||||
8295 | Diag(VDecl->getLocation(), diag::note_use_thread_local); | ||||
8296 | } | ||||
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 8297 | } |
Douglas Gregor | b6c8c8b | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 8298 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 8299 | // We will represent direct-initialization similarly to copy-initialization: |
8300 | // int x(1); -as-> int x = 1; | ||||
8301 | // ClassType x(a,b,c); -as-> ClassType x = ClassType(a,b,c); | ||||
8302 | // | ||||
8303 | // Clients that want to distinguish between the two forms, can check for | ||||
8304 | // direct initializer using VarDecl::getInitStyle(). | ||||
8305 | // A major benefit is that clients that don't particularly care about which | ||||
8306 | // exactly form was it (like the CodeGen) can handle both cases without | ||||
8307 | // special case code. | ||||
8308 | |||||
8309 | // C++ 8.5p11: | ||||
8310 | // The form of initialization (using parentheses or '=') is generally | ||||
8311 | // insignificant, but does matter when the entity being initialized has a | ||||
8312 | // class type. | ||||
8313 | if (CXXDirectInit) { | ||||
8314 | assert(DirectInit && "Call-style initializer must be direct init."); | ||||
8315 | VDecl->setInitStyle(VarDecl::CallInit); | ||||
8316 | } else if (DirectInit) { | ||||
8317 | // This must be list-initialization. No other way is direct-initialization. | ||||
8318 | VDecl->setInitStyle(VarDecl::ListInit); | ||||
8319 | } | ||||
8320 | |||||
John McCall | 2998d6b | 2011-01-19 11:48:09 +0000 | [diff] [blame] | 8321 | CheckCompleteVariableDeclaration(VDecl); |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 8322 | } |
8323 | |||||
John McCall | 7727acf | 2010-03-31 02:13:20 +0000 | [diff] [blame] | 8324 | /// ActOnInitializerError - Given that there was an error parsing an |
8325 | /// initializer for the given declaration, try to return to some form | ||||
8326 | /// of sanity. | ||||
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8327 | void Sema::ActOnInitializerError(Decl *D) { |
John McCall | 7727acf | 2010-03-31 02:13:20 +0000 | [diff] [blame] | 8328 | // Our main concern here is re-establishing invariants like "a |
8329 | // variable's type is either dependent or complete". | ||||
John McCall | 7727acf | 2010-03-31 02:13:20 +0000 | [diff] [blame] | 8330 | if (!D || D->isInvalidDecl()) return; |
8331 | |||||
8332 | VarDecl *VD = dyn_cast<VarDecl>(D); | ||||
8333 | if (!VD) return; | ||||
8334 | |||||
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 8335 | // 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] | 8336 | if (ParsingInitForAutoVars.count(D)) { |
8337 | D->setInvalidDecl(); | ||||
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 8338 | return; |
8339 | } | ||||
8340 | |||||
John McCall | 7727acf | 2010-03-31 02:13:20 +0000 | [diff] [blame] | 8341 | QualType Ty = VD->getType(); |
8342 | if (Ty->isDependentType()) return; | ||||
8343 | |||||
8344 | // Require a complete type. | ||||
8345 | if (RequireCompleteType(VD->getLocation(), | ||||
8346 | Context.getBaseElementType(Ty), | ||||
8347 | diag::err_typecheck_decl_incomplete_type)) { | ||||
8348 | VD->setInvalidDecl(); | ||||
8349 | return; | ||||
8350 | } | ||||
8351 | |||||
8352 | // Require an abstract type. | ||||
8353 | if (RequireNonAbstractType(VD->getLocation(), Ty, | ||||
8354 | diag::err_abstract_type_in_decl, | ||||
8355 | AbstractVariableType)) { | ||||
8356 | VD->setInvalidDecl(); | ||||
8357 | return; | ||||
8358 | } | ||||
8359 | |||||
8360 | // Don't bother complaining about constructors or destructors, | ||||
8361 | // though. | ||||
8362 | } | ||||
8363 | |||||
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8364 | void Sema::ActOnUninitializedDecl(Decl *RealDecl, |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 8365 | bool TypeMayContainAuto) { |
Argyrios Kyrtzidis | 48c2e90 | 2008-11-07 13:01:22 +0000 | [diff] [blame] | 8366 | // If there is no declaration, there was an error parsing it. Just ignore it. |
8367 | if (RealDecl == 0) | ||||
8368 | return; | ||||
8369 | |||||
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 8370 | if (VarDecl *Var = dyn_cast<VarDecl>(RealDecl)) { |
8371 | QualType Type = Var->getType(); | ||||
Douglas Gregor | b6c8c8b | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 8372 | |
Richard Smith | dd4b350 | 2011-12-25 21:17:58 +0000 | [diff] [blame] | 8373 | // C++11 [dcl.spec.auto]p3 |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 8374 | if (TypeMayContainAuto && Type->getContainedAutoType()) { |
Anders Carlsson | 6a75cd9 | 2009-07-11 00:34:39 +0000 | [diff] [blame] | 8375 | Diag(Var->getLocation(), diag::err_auto_var_requires_init) |
8376 | << Var->getDeclName() << Type; | ||||
8377 | Var->setInvalidDecl(); | ||||
8378 | return; | ||||
8379 | } | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8380 | |
Richard Smith | dd4b350 | 2011-12-25 21:17:58 +0000 | [diff] [blame] | 8381 | // 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] | 8382 | // the constexpr specifier; if so, its declaration shall specify |
8383 | // a brace-or-equal-initializer. | ||||
Richard Smith | dd4b350 | 2011-12-25 21:17:58 +0000 | [diff] [blame] | 8384 | // C++11 [dcl.constexpr]p1: The constexpr specifier shall be applied only to |
8385 | // the definition of a variable [...] or the declaration of a static data | ||||
8386 | // member. | ||||
8387 | if (Var->isConstexpr() && !Var->isThisDeclarationADefinition()) { | ||||
8388 | if (Var->isStaticDataMember()) | ||||
8389 | Diag(Var->getLocation(), | ||||
8390 | diag::err_constexpr_static_mem_var_requires_init) | ||||
8391 | << Var->getDeclName(); | ||||
8392 | else | ||||
8393 | Diag(Var->getLocation(), diag::err_invalid_constexpr_var_decl); | ||||
Richard Smith | c6d990a | 2011-09-29 19:11:37 +0000 | [diff] [blame] | 8394 | Var->setInvalidDecl(); |
8395 | return; | ||||
8396 | } | ||||
8397 | |||||
Douglas Gregor | 60c93c9 | 2010-02-09 07:26:29 +0000 | [diff] [blame] | 8398 | switch (Var->isThisDeclarationADefinition()) { |
8399 | case VarDecl::Definition: | ||||
8400 | if (!Var->isStaticDataMember() || !Var->getAnyInitializer()) | ||||
8401 | break; | ||||
8402 | |||||
8403 | // We have an out-of-line definition of a static data member | ||||
8404 | // that has an in-class initializer, so we type-check this like | ||||
8405 | // a declaration. | ||||
8406 | // | ||||
8407 | // Fall through | ||||
8408 | |||||
8409 | case VarDecl::DeclarationOnly: | ||||
8410 | // It's only a declaration. | ||||
8411 | |||||
8412 | // Block scope. C99 6.7p7: If an identifier for an object is | ||||
8413 | // declared with no linkage (C99 6.2.2p6), the type for the | ||||
8414 | // object shall be complete. | ||||
John McCall | b6bbcc9 | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 8415 | if (!Type->isDependentType() && Var->isLocalVarDecl() && |
Rafael Espindola | 181e3ec | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 8416 | !Var->hasLinkage() && !Var->isInvalidDecl() && |
Douglas Gregor | 60c93c9 | 2010-02-09 07:26:29 +0000 | [diff] [blame] | 8417 | RequireCompleteType(Var->getLocation(), Type, |
8418 | diag::err_typecheck_decl_incomplete_type)) | ||||
8419 | Var->setInvalidDecl(); | ||||
8420 | |||||
8421 | // Make sure that the type is not abstract. | ||||
8422 | if (!Type->isDependentType() && !Var->isInvalidDecl() && | ||||
8423 | RequireNonAbstractType(Var->getLocation(), Type, | ||||
8424 | diag::err_abstract_type_in_decl, | ||||
8425 | AbstractVariableType)) | ||||
8426 | Var->setInvalidDecl(); | ||||
Fariborz Jahanian | 4cc83c2 | 2012-08-15 18:42:26 +0000 | [diff] [blame] | 8427 | if (!Type->isDependentType() && !Var->isInvalidDecl() && |
Fariborz Jahanian | 767a1a2 | 2012-08-17 21:44:55 +0000 | [diff] [blame] | 8428 | Var->getStorageClass() == SC_PrivateExtern) { |
Fariborz Jahanian | 4cc83c2 | 2012-08-15 18:42:26 +0000 | [diff] [blame] | 8429 | Diag(Var->getLocation(), diag::warn_private_extern); |
Fariborz Jahanian | 767a1a2 | 2012-08-17 21:44:55 +0000 | [diff] [blame] | 8430 | Diag(Var->getLocation(), diag::note_private_extern); |
8431 | } | ||||
Fariborz Jahanian | 4cc83c2 | 2012-08-15 18:42:26 +0000 | [diff] [blame] | 8432 | |
Douglas Gregor | 60c93c9 | 2010-02-09 07:26:29 +0000 | [diff] [blame] | 8433 | return; |
8434 | |||||
8435 | case VarDecl::TentativeDefinition: | ||||
8436 | // File scope. C99 6.9.2p2: A declaration of an identifier for an | ||||
8437 | // object that has file scope without an initializer, and without a | ||||
8438 | // storage-class specifier or with the storage-class specifier "static", | ||||
8439 | // constitutes a tentative definition. Note: A tentative definition with | ||||
8440 | // external linkage is valid (C99 6.2.2p5). | ||||
8441 | if (!Var->isInvalidDecl()) { | ||||
8442 | if (const IncompleteArrayType *ArrayT | ||||
8443 | = Context.getAsIncompleteArrayType(Type)) { | ||||
8444 | if (RequireCompleteType(Var->getLocation(), | ||||
8445 | ArrayT->getElementType(), | ||||
8446 | diag::err_illegal_decl_array_incomplete_type)) | ||||
8447 | Var->setInvalidDecl(); | ||||
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 8448 | } else if (Var->getStorageClass() == SC_Static) { |
Douglas Gregor | 60c93c9 | 2010-02-09 07:26:29 +0000 | [diff] [blame] | 8449 | // C99 6.9.2p3: If the declaration of an identifier for an object is |
8450 | // a tentative definition and has internal linkage (C99 6.2.2p3), the | ||||
8451 | // declared type shall not be an incomplete type. | ||||
8452 | // NOTE: code such as the following | ||||
8453 | // static struct s; | ||||
8454 | // struct s { int a; }; | ||||
8455 | // is accepted by gcc. Hence here we issue a warning instead of | ||||
8456 | // an error and we do not invalidate the static declaration. | ||||
8457 | // NOTE: to avoid multiple warnings, only check the first declaration. | ||||
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 8458 | if (Var->getPreviousDecl() == 0) |
Douglas Gregor | 60c93c9 | 2010-02-09 07:26:29 +0000 | [diff] [blame] | 8459 | RequireCompleteType(Var->getLocation(), Type, |
8460 | diag::ext_typecheck_decl_incomplete_type); | ||||
8461 | } | ||||
8462 | } | ||||
8463 | |||||
8464 | // Record the tentative definition; we're done. | ||||
8465 | if (!Var->isInvalidDecl()) | ||||
8466 | TentativeDefinitions.push_back(Var); | ||||
8467 | return; | ||||
8468 | } | ||||
8469 | |||||
8470 | // Provide a specific diagnostic for uninitialized variable | ||||
8471 | // definitions with incomplete array type. | ||||
8472 | if (Type->isIncompleteArrayType()) { | ||||
Sebastian Redl | 6e82475 | 2009-11-05 19:47:47 +0000 | [diff] [blame] | 8473 | Diag(Var->getLocation(), |
8474 | diag::err_typecheck_incomplete_array_needs_initializer); | ||||
8475 | Var->setInvalidDecl(); | ||||
8476 | return; | ||||
8477 | } | ||||
8478 | |||||
John McCall | b567a8b | 2010-08-01 01:25:24 +0000 | [diff] [blame] | 8479 | // Provide a specific diagnostic for uninitialized variable |
8480 | // definitions with reference type. | ||||
8481 | if (Type->isReferenceType()) { | ||||
8482 | Diag(Var->getLocation(), diag::err_reference_var_requires_init) | ||||
8483 | << Var->getDeclName() | ||||
8484 | << SourceRange(Var->getLocation(), Var->getLocation()); | ||||
8485 | Var->setInvalidDecl(); | ||||
8486 | return; | ||||
8487 | } | ||||
Douglas Gregor | 60c93c9 | 2010-02-09 07:26:29 +0000 | [diff] [blame] | 8488 | |
8489 | // Do not attempt to type-check the default initializer for a | ||||
8490 | // variable with dependent type. | ||||
8491 | if (Type->isDependentType()) | ||||
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8492 | return; |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 8493 | |
Douglas Gregor | 60c93c9 | 2010-02-09 07:26:29 +0000 | [diff] [blame] | 8494 | if (Var->isInvalidDecl()) |
8495 | return; | ||||
Douglas Gregor | 1ab537b | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 8496 | |
Douglas Gregor | 60c93c9 | 2010-02-09 07:26:29 +0000 | [diff] [blame] | 8497 | if (RequireCompleteType(Var->getLocation(), |
8498 | Context.getBaseElementType(Type), | ||||
8499 | diag::err_typecheck_decl_incomplete_type)) { | ||||
8500 | Var->setInvalidDecl(); | ||||
8501 | return; | ||||
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 8502 | } |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 8503 | |
Douglas Gregor | 60c93c9 | 2010-02-09 07:26:29 +0000 | [diff] [blame] | 8504 | // The variable can not have an abstract class type. |
8505 | if (RequireNonAbstractType(Var->getLocation(), Type, | ||||
8506 | diag::err_abstract_type_in_decl, | ||||
8507 | AbstractVariableType)) { | ||||
8508 | Var->setInvalidDecl(); | ||||
8509 | return; | ||||
8510 | } | ||||
8511 | |||||
Douglas Gregor | 4337dc7 | 2011-05-21 17:52:48 +0000 | [diff] [blame] | 8512 | // Check for jumps past the implicit initializer. C++0x |
8513 | // clarifies that this applies to a "variable with automatic | ||||
8514 | // storage duration", not a "local variable". | ||||
Richard Smith | 0e9e981 | 2011-10-20 21:42:12 +0000 | [diff] [blame] | 8515 | // C++11 [stmt.dcl]p3 |
Douglas Gregor | 4337dc7 | 2011-05-21 17:52:48 +0000 | [diff] [blame] | 8516 | // A program that jumps from a point where a variable with automatic |
8517 | // storage duration is not in scope to a point where it is in scope is | ||||
8518 | // ill-formed unless the variable has scalar type, class type with a | ||||
8519 | // trivial default constructor and a trivial destructor, a cv-qualified | ||||
8520 | // version of one of these types, or an array of one of the preceding | ||||
8521 | // types and is declared without an initializer. | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8522 | if (getLangOpts().CPlusPlus && Var->hasLocalStorage()) { |
Douglas Gregor | 4337dc7 | 2011-05-21 17:52:48 +0000 | [diff] [blame] | 8523 | if (const RecordType *Record |
8524 | = Context.getBaseElementType(Type)->getAs<RecordType>()) { | ||||
Sean Hunt | a6bff2c | 2011-05-11 22:50:12 +0000 | [diff] [blame] | 8525 | CXXRecordDecl *CXXRecord = cast<CXXRecordDecl>(Record->getDecl()); |
Richard Smith | 0e9e981 | 2011-10-20 21:42:12 +0000 | [diff] [blame] | 8526 | // Mark the function for further checking even if the looser rules of |
8527 | // C++11 do not require such checks, so that we can diagnose | ||||
8528 | // incompatibilities with C++98. | ||||
8529 | if (!CXXRecord->isPOD()) | ||||
Sean Hunt | a6bff2c | 2011-05-11 22:50:12 +0000 | [diff] [blame] | 8530 | getCurFunction()->setHasBranchProtectedScope(); |
8531 | } | ||||
Douglas Gregor | 60c93c9 | 2010-02-09 07:26:29 +0000 | [diff] [blame] | 8532 | } |
Douglas Gregor | 4337dc7 | 2011-05-21 17:52:48 +0000 | [diff] [blame] | 8533 | |
8534 | // C++03 [dcl.init]p9: | ||||
8535 | // If no initializer is specified for an object, and the | ||||
8536 | // object is of (possibly cv-qualified) non-POD class type (or | ||||
8537 | // array thereof), the object shall be default-initialized; if | ||||
8538 | // the object is of const-qualified type, the underlying class | ||||
8539 | // type shall have a user-declared default | ||||
8540 | // constructor. Otherwise, if no initializer is specified for | ||||
8541 | // a non- static object, the object and its subobjects, if | ||||
8542 | // any, have an indeterminate initial value); if the object | ||||
8543 | // or any of its subobjects are of const-qualified type, the | ||||
8544 | // program is ill-formed. | ||||
8545 | // C++0x [dcl.init]p11: | ||||
8546 | // If no initializer is specified for an object, the object is | ||||
8547 | // default-initialized; [...]. | ||||
8548 | InitializedEntity Entity = InitializedEntity::InitializeVariable(Var); | ||||
8549 | InitializationKind Kind | ||||
8550 | = InitializationKind::CreateDefault(Var->getLocation()); | ||||
Dmitri Gribenko | 62ed889 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 8551 | |
8552 | InitializationSequence InitSeq(*this, Entity, Kind, None); | ||||
8553 | ExprResult Init = InitSeq.Perform(*this, Entity, Kind, None); | ||||
Douglas Gregor | 4337dc7 | 2011-05-21 17:52:48 +0000 | [diff] [blame] | 8554 | if (Init.isInvalid()) |
8555 | Var->setInvalidDecl(); | ||||
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 8556 | else if (Init.get()) { |
Douglas Gregor | 4337dc7 | 2011-05-21 17:52:48 +0000 | [diff] [blame] | 8557 | Var->setInit(MaybeCreateExprWithCleanups(Init.get())); |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 8558 | // This is important for template substitution. |
8559 | Var->setInitStyle(VarDecl::CallInit); | ||||
8560 | } | ||||
Douglas Gregor | 516a6bc | 2010-03-08 02:45:10 +0000 | [diff] [blame] | 8561 | |
John McCall | 2998d6b | 2011-01-19 11:48:09 +0000 | [diff] [blame] | 8562 | CheckCompleteVariableDeclaration(Var); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 8563 | } |
8564 | } | ||||
8565 | |||||
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 8566 | void Sema::ActOnCXXForRangeDecl(Decl *D) { |
8567 | VarDecl *VD = dyn_cast<VarDecl>(D); | ||||
8568 | if (!VD) { | ||||
8569 | Diag(D->getLocation(), diag::err_for_range_decl_must_be_var); | ||||
8570 | D->setInvalidDecl(); | ||||
8571 | return; | ||||
8572 | } | ||||
8573 | |||||
8574 | VD->setCXXForRangeDecl(true); | ||||
8575 | |||||
8576 | // for-range-declaration cannot be given a storage class specifier. | ||||
8577 | int Error = -1; | ||||
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 8578 | switch (VD->getStorageClass()) { |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 8579 | case SC_None: |
8580 | break; | ||||
8581 | case SC_Extern: | ||||
8582 | Error = 0; | ||||
8583 | break; | ||||
8584 | case SC_Static: | ||||
8585 | Error = 1; | ||||
8586 | break; | ||||
8587 | case SC_PrivateExtern: | ||||
8588 | Error = 2; | ||||
8589 | break; | ||||
8590 | case SC_Auto: | ||||
8591 | Error = 3; | ||||
8592 | break; | ||||
8593 | case SC_Register: | ||||
8594 | Error = 4; | ||||
8595 | break; | ||||
Peter Collingbourne | 8c25fc5 | 2011-09-19 21:14:35 +0000 | [diff] [blame] | 8596 | case SC_OpenCLWorkGroupLocal: |
Peter Collingbourne | 8be0c74 | 2011-09-20 12:40:26 +0000 | [diff] [blame] | 8597 | llvm_unreachable("Unexpected storage class"); |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 8598 | } |
Richard Smith | c6d990a | 2011-09-29 19:11:37 +0000 | [diff] [blame] | 8599 | if (VD->isConstexpr()) |
8600 | Error = 5; | ||||
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 8601 | if (Error != -1) { |
8602 | Diag(VD->getOuterLocStart(), diag::err_for_range_storage_class) | ||||
8603 | << VD->getDeclName() << Error; | ||||
8604 | D->setInvalidDecl(); | ||||
8605 | } | ||||
8606 | } | ||||
8607 | |||||
John McCall | 2998d6b | 2011-01-19 11:48:09 +0000 | [diff] [blame] | 8608 | void Sema::CheckCompleteVariableDeclaration(VarDecl *var) { |
8609 | if (var->isInvalidDecl()) return; | ||||
8610 | |||||
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8611 | // In ARC, don't allow jumps past the implicit initialization of a |
8612 | // local retaining variable. | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8613 | if (getLangOpts().ObjCAutoRefCount && |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8614 | var->hasLocalStorage()) { |
8615 | switch (var->getType().getObjCLifetime()) { | ||||
8616 | case Qualifiers::OCL_None: | ||||
8617 | case Qualifiers::OCL_ExplicitNone: | ||||
8618 | case Qualifiers::OCL_Autoreleasing: | ||||
8619 | break; | ||||
8620 | |||||
8621 | case Qualifiers::OCL_Weak: | ||||
8622 | case Qualifiers::OCL_Strong: | ||||
8623 | getCurFunction()->setHasBranchProtectedScope(); | ||||
8624 | break; | ||||
8625 | } | ||||
8626 | } | ||||
8627 | |||||
Eli Friedman | e4851f2 | 2012-10-23 20:19:32 +0000 | [diff] [blame] | 8628 | if (var->isThisDeclarationADefinition() && |
Rafael Espindola | 181e3ec | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 8629 | var->isExternallyVisible() && |
Manuel Klimek | acaf110 | 2012-12-12 13:26:54 +0000 | [diff] [blame] | 8630 | getDiagnostics().getDiagnosticLevel( |
8631 | diag::warn_missing_variable_declarations, | ||||
8632 | var->getLocation())) { | ||||
Eli Friedman | e4851f2 | 2012-10-23 20:19:32 +0000 | [diff] [blame] | 8633 | // Find a previous declaration that's not a definition. |
8634 | VarDecl *prev = var->getPreviousDecl(); | ||||
8635 | while (prev && prev->isThisDeclarationADefinition()) | ||||
8636 | prev = prev->getPreviousDecl(); | ||||
8637 | |||||
8638 | if (!prev) | ||||
8639 | Diag(var->getLocation(), diag::warn_missing_variable_declarations) << var; | ||||
8640 | } | ||||
8641 | |||||
Richard Smith | 6a570f6 | 2013-04-14 20:11:31 +0000 | [diff] [blame] | 8642 | if (var->getTLSKind() == VarDecl::TLS_Static && |
8643 | var->getType().isDestructedType()) { | ||||
8644 | // GNU C++98 edits for __thread, [basic.start.term]p3: | ||||
8645 | // The type of an object with thread storage duration shall not | ||||
8646 | // have a non-trivial destructor. | ||||
8647 | Diag(var->getLocation(), diag::err_thread_nontrivial_dtor); | ||||
8648 | if (getLangOpts().CPlusPlus11) | ||||
8649 | Diag(var->getLocation(), diag::note_use_thread_local); | ||||
8650 | } | ||||
8651 | |||||
John McCall | 2998d6b | 2011-01-19 11:48:09 +0000 | [diff] [blame] | 8652 | // All the following checks are C++ only. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8653 | if (!getLangOpts().CPlusPlus) return; |
John McCall | 2998d6b | 2011-01-19 11:48:09 +0000 | [diff] [blame] | 8654 | |
Richard Smith | a67d503 | 2012-11-09 23:03:14 +0000 | [diff] [blame] | 8655 | QualType type = var->getType(); |
8656 | if (type->isDependentType()) return; | ||||
John McCall | 2998d6b | 2011-01-19 11:48:09 +0000 | [diff] [blame] | 8657 | |
8658 | // __block variables might require us to capture a copy-initializer. | ||||
8659 | if (var->hasAttr<BlocksAttr>()) { | ||||
8660 | // It's currently invalid to ever have a __block variable with an | ||||
8661 | // array type; should we diagnose that here? | ||||
8662 | |||||
8663 | // Regardless, we don't want to ignore array nesting when | ||||
8664 | // constructing this copy. | ||||
John McCall | 2998d6b | 2011-01-19 11:48:09 +0000 | [diff] [blame] | 8665 | if (type->isStructureOrClassType()) { |
John McCall | b760f11 | 2013-03-22 02:10:40 +0000 | [diff] [blame] | 8666 | EnterExpressionEvaluationContext scope(*this, PotentiallyEvaluated); |
John McCall | 2998d6b | 2011-01-19 11:48:09 +0000 | [diff] [blame] | 8667 | SourceLocation poi = var->getLocation(); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 8668 | Expr *varRef =new (Context) DeclRefExpr(var, false, type, VK_LValue, poi); |
Douglas Gregor | 6cda3e6 | 2013-03-07 22:38:24 +0000 | [diff] [blame] | 8669 | ExprResult result |
8670 | = PerformMoveOrCopyInitialization( | ||||
8671 | InitializedEntity::InitializeBlock(poi, type, false), | ||||
8672 | var, var->getType(), varRef, /*AllowNRVO=*/true); | ||||
John McCall | 2998d6b | 2011-01-19 11:48:09 +0000 | [diff] [blame] | 8673 | if (!result.isInvalid()) { |
8674 | result = MaybeCreateExprWithCleanups(result); | ||||
8675 | Expr *init = result.takeAs<Expr>(); | ||||
8676 | Context.setBlockVarCopyInits(var, init); | ||||
8677 | } | ||||
8678 | } | ||||
8679 | } | ||||
8680 | |||||
Richard Smith | 66f8571 | 2011-11-07 22:16:17 +0000 | [diff] [blame] | 8681 | Expr *Init = var->getInit(); |
8682 | bool IsGlobal = var->hasGlobalStorage() && !var->isStaticLocal(); | ||||
Richard Smith | a67d503 | 2012-11-09 23:03:14 +0000 | [diff] [blame] | 8683 | QualType baseType = Context.getBaseElementType(type); |
Richard Smith | 66f8571 | 2011-11-07 22:16:17 +0000 | [diff] [blame] | 8684 | |
Richard Smith | 9568f0c | 2012-10-29 18:26:47 +0000 | [diff] [blame] | 8685 | if (!var->getDeclContext()->isDependentContext() && |
8686 | Init && !Init->isValueDependent()) { | ||||
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 8687 | if (IsGlobal && !var->isConstexpr() && |
8688 | getDiagnostics().getDiagnosticLevel(diag::warn_global_constructor, | ||||
8689 | var->getLocation()) | ||||
Eli Friedman | 21cde05 | 2013-07-16 22:40:53 +0000 | [diff] [blame] | 8690 | != DiagnosticsEngine::Ignored) { |
8691 | // Warn about globals which don't have a constant initializer. Don't | ||||
8692 | // warn about globals with a non-trivial destructor because we already | ||||
8693 | // warned about them. | ||||
8694 | CXXRecordDecl *RD = baseType->getAsCXXRecordDecl(); | ||||
8695 | if (!(RD && !RD->hasTrivialDestructor()) && | ||||
8696 | !Init->isConstantInitializer(Context, baseType->isReferenceType())) | ||||
8697 | Diag(var->getLocation(), diag::warn_global_constructor) | ||||
8698 | << Init->getSourceRange(); | ||||
8699 | } | ||||
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 8700 | |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 8701 | if (var->isConstexpr()) { |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 8702 | SmallVector<PartialDiagnosticAt, 8> Notes; |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 8703 | if (!var->evaluateValue(Notes) || !var->isInitICE()) { |
8704 | SourceLocation DiagLoc = var->getLocation(); | ||||
8705 | // If the note doesn't add any useful information other than a source | ||||
8706 | // location, fold it into the primary diagnostic. | ||||
8707 | if (Notes.size() == 1 && Notes[0].second.getDiagID() == | ||||
8708 | diag::note_invalid_subexpr_in_const_expr) { | ||||
8709 | DiagLoc = Notes[0].first; | ||||
8710 | Notes.clear(); | ||||
8711 | } | ||||
8712 | Diag(DiagLoc, diag::err_constexpr_var_requires_const_init) | ||||
8713 | << var << Init->getSourceRange(); | ||||
8714 | for (unsigned I = 0, N = Notes.size(); I != N; ++I) | ||||
8715 | Diag(Notes[I].first, Notes[I].second); | ||||
8716 | } | ||||
Daniel Dunbar | 3d13c5a | 2012-03-09 01:51:51 +0000 | [diff] [blame] | 8717 | } else if (var->isUsableInConstantExpressions(Context)) { |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 8718 | // Check whether the initializer of a const variable of integral or |
8719 | // enumeration type is an ICE now, since we can't tell whether it was | ||||
8720 | // initialized by a constant expression if we check later. | ||||
8721 | var->checkInitIsICE(); | ||||
8722 | } | ||||
Richard Smith | 66f8571 | 2011-11-07 22:16:17 +0000 | [diff] [blame] | 8723 | } |
John McCall | 2998d6b | 2011-01-19 11:48:09 +0000 | [diff] [blame] | 8724 | |
8725 | // Require the destructor. | ||||
8726 | if (const RecordType *recordType = baseType->getAs<RecordType>()) | ||||
8727 | FinalizeVarWithDestructor(var, recordType); | ||||
8728 | } | ||||
8729 | |||||
Richard Smith | 483b9f3 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 8730 | /// FinalizeDeclaration - called by ParseDeclarationAfterDeclarator to perform |
8731 | /// any semantic actions necessary after any initializer has been attached. | ||||
8732 | void | ||||
8733 | Sema::FinalizeDeclaration(Decl *ThisDecl) { | ||||
8734 | // Note that we are no longer parsing the initializer for this declaration. | ||||
8735 | ParsingInitForAutoVars.erase(ThisDecl); | ||||
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 8736 | |
Rafael Espindola | 4c8cba8 | 2013-02-22 17:59:16 +0000 | [diff] [blame] | 8737 | VarDecl *VD = dyn_cast_or_null<VarDecl>(ThisDecl); |
Rafael Espindola | da844b3 | 2013-01-03 04:05:19 +0000 | [diff] [blame] | 8738 | if (!VD) |
8739 | return; | ||||
8740 | |||||
Rafael Espindola | 29535ba | 2013-08-16 23:18:50 +0000 | [diff] [blame] | 8741 | if (UsedAttr *Attr = VD->getAttr<UsedAttr>()) { |
8742 | if (!Attr->isInherited() && !VD->isThisDeclarationADefinition()) { | ||||
8743 | Diag(Attr->getLocation(), diag::warn_attribute_ignored) << "used"; | ||||
8744 | VD->dropAttr<UsedAttr>(); | ||||
8745 | } | ||||
8746 | } | ||||
8747 | |||||
Rafael Espindola | 4c8cba8 | 2013-02-22 17:59:16 +0000 | [diff] [blame] | 8748 | const DeclContext *DC = VD->getDeclContext(); |
8749 | // If there's a #pragma GCC visibility in scope, and this isn't a class | ||||
8750 | // member, set the visibility of this variable. | ||||
Rafael Espindola | 181e3ec | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 8751 | if (!DC->isRecord() && VD->isExternallyVisible()) |
Rafael Espindola | 4c8cba8 | 2013-02-22 17:59:16 +0000 | [diff] [blame] | 8752 | AddPushedVisibilityAttribute(VD); |
8753 | |||||
Rafael Espindola | 6769ccb | 2013-01-03 04:29:20 +0000 | [diff] [blame] | 8754 | if (VD->isFileVarDecl()) |
8755 | MarkUnusedFileScopedDecl(VD); | ||||
8756 | |||||
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 8757 | // Now we have parsed the initializer and can update the table of magic |
8758 | // tag values. | ||||
Rafael Espindola | da844b3 | 2013-01-03 04:05:19 +0000 | [diff] [blame] | 8759 | if (!VD->hasAttr<TypeTagForDatatypeAttr>() || |
8760 | !VD->getType()->isIntegralOrEnumerationType()) | ||||
8761 | return; | ||||
8762 | |||||
8763 | for (specific_attr_iterator<TypeTagForDatatypeAttr> | ||||
8764 | I = ThisDecl->specific_attr_begin<TypeTagForDatatypeAttr>(), | ||||
8765 | E = ThisDecl->specific_attr_end<TypeTagForDatatypeAttr>(); | ||||
8766 | I != E; ++I) { | ||||
8767 | const Expr *MagicValueExpr = VD->getInit(); | ||||
8768 | if (!MagicValueExpr) { | ||||
8769 | continue; | ||||
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 8770 | } |
Rafael Espindola | da844b3 | 2013-01-03 04:05:19 +0000 | [diff] [blame] | 8771 | llvm::APSInt MagicValueInt; |
8772 | if (!MagicValueExpr->isIntegerConstantExpr(MagicValueInt, Context)) { | ||||
8773 | Diag(I->getRange().getBegin(), | ||||
8774 | diag::err_type_tag_for_datatype_not_ice) | ||||
8775 | << LangOpts.CPlusPlus << MagicValueExpr->getSourceRange(); | ||||
8776 | continue; | ||||
8777 | } | ||||
8778 | if (MagicValueInt.getActiveBits() > 64) { | ||||
8779 | Diag(I->getRange().getBegin(), | ||||
8780 | diag::err_type_tag_for_datatype_too_large) | ||||
8781 | << LangOpts.CPlusPlus << MagicValueExpr->getSourceRange(); | ||||
8782 | continue; | ||||
8783 | } | ||||
8784 | uint64_t MagicValue = MagicValueInt.getZExtValue(); | ||||
8785 | RegisterTypeTagForDatatype(I->getArgumentKind(), | ||||
8786 | MagicValue, | ||||
8787 | I->getMatchingCType(), | ||||
8788 | I->getLayoutCompatible(), | ||||
8789 | I->getMustBeNull()); | ||||
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 8790 | } |
Richard Smith | 483b9f3 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 8791 | } |
8792 | |||||
Rafael Espindola | 4549d7f | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 8793 | Sema::DeclGroupPtrTy Sema::FinalizeDeclaratorGroup(Scope *S, const DeclSpec &DS, |
8794 | ArrayRef<Decl *> Group) { | ||||
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 8795 | SmallVector<Decl*, 8> Decls; |
Eli Friedman | c1dc653 | 2009-05-29 01:49:24 +0000 | [diff] [blame] | 8796 | |
8797 | if (DS.isTypeSpecOwned()) | ||||
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 8798 | Decls.push_back(DS.getRepAsDecl()); |
Eli Friedman | c1dc653 | 2009-05-29 01:49:24 +0000 | [diff] [blame] | 8799 | |
Rafael Espindola | 4549d7f | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 8800 | for (unsigned i = 0, e = Group.size(); i != e; ++i) |
Richard Smith | 406c38e | 2011-02-23 00:37:57 +0000 | [diff] [blame] | 8801 | if (Decl *D = Group[i]) |
8802 | Decls.push_back(D); | ||||
8803 | |||||
Eli Friedman | 5e867c8 | 2013-07-10 00:30:46 +0000 | [diff] [blame] | 8804 | if (DeclSpec::isDeclRep(DS.getTypeSpecType())) { |
David Blaikie | 66cff72 | 2012-11-14 01:52:05 +0000 | [diff] [blame] | 8805 | if (const TagDecl *Tag = dyn_cast_or_null<TagDecl>(DS.getRepAsDecl())) |
Eli Friedman | 5e867c8 | 2013-07-10 00:30:46 +0000 | [diff] [blame] | 8806 | HandleTagNumbering(*this, Tag); |
8807 | } | ||||
David Blaikie | 66cff72 | 2012-11-14 01:52:05 +0000 | [diff] [blame] | 8808 | |
Rafael Espindola | 4549d7f | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 8809 | return BuildDeclaratorGroup(Decls, DS.containsPlaceholderType()); |
Richard Smith | 406c38e | 2011-02-23 00:37:57 +0000 | [diff] [blame] | 8810 | } |
8811 | |||||
8812 | /// BuildDeclaratorGroup - convert a list of declarations into a declaration | ||||
8813 | /// group, performing any necessary semantic checking. | ||||
8814 | Sema::DeclGroupPtrTy | ||||
Rafael Espindola | 4549d7f | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 8815 | Sema::BuildDeclaratorGroup(llvm::MutableArrayRef<Decl *> Group, |
Richard Smith | 406c38e | 2011-02-23 00:37:57 +0000 | [diff] [blame] | 8816 | bool TypeMayContainAuto) { |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 8817 | // C++0x [dcl.spec.auto]p7: |
8818 | // If the type deduced for the template parameter U is not the same in each | ||||
8819 | // deduction, the program is ill-formed. | ||||
8820 | // FIXME: When initializer-list support is added, a distinction is needed | ||||
8821 | // between the deduced type U and the deduced type which 'auto' stands for. | ||||
8822 | // auto a = 0, b = { 1, 2, 3 }; | ||||
8823 | // is legal because the deduced type U is 'int' in both cases. | ||||
Rafael Espindola | 4549d7f | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 8824 | if (TypeMayContainAuto && Group.size() > 1) { |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 8825 | QualType Deduced; |
8826 | CanQualType DeducedCanon; | ||||
8827 | VarDecl *DeducedDecl = 0; | ||||
Rafael Espindola | 4549d7f | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 8828 | for (unsigned i = 0, e = Group.size(); i != e; ++i) { |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 8829 | if (VarDecl *D = dyn_cast<VarDecl>(Group[i])) { |
8830 | AutoType *AT = D->getType()->getContainedAutoType(); | ||||
Richard Smith | 406c38e | 2011-02-23 00:37:57 +0000 | [diff] [blame] | 8831 | // Don't reissue diagnostics when instantiating a template. |
8832 | if (AT && D->isInvalidDecl()) | ||||
8833 | break; | ||||
Richard Smith | dc7a4f5 | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 8834 | QualType U = AT ? AT->getDeducedType() : QualType(); |
8835 | if (!U.isNull()) { | ||||
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 8836 | CanQualType UCanon = Context.getCanonicalType(U); |
8837 | if (Deduced.isNull()) { | ||||
8838 | Deduced = U; | ||||
8839 | DeducedCanon = UCanon; | ||||
8840 | DeducedDecl = D; | ||||
8841 | } else if (DeducedCanon != UCanon) { | ||||
Richard Smith | 406c38e | 2011-02-23 00:37:57 +0000 | [diff] [blame] | 8842 | Diag(D->getTypeSourceInfo()->getTypeLoc().getBeginLoc(), |
8843 | diag::err_auto_different_deductions) | ||||
Richard Smith | ffd015e | 2013-05-04 04:19:27 +0000 | [diff] [blame] | 8844 | << (AT->isDecltypeAuto() ? 1 : 0) |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 8845 | << Deduced << DeducedDecl->getDeclName() |
8846 | << U << D->getDeclName() | ||||
8847 | << DeducedDecl->getInit()->getSourceRange() | ||||
8848 | << D->getInit()->getSourceRange(); | ||||
Richard Smith | 406c38e | 2011-02-23 00:37:57 +0000 | [diff] [blame] | 8849 | D->setInvalidDecl(); |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 8850 | break; |
8851 | } | ||||
8852 | } | ||||
8853 | } | ||||
8854 | } | ||||
8855 | } | ||||
8856 | |||||
Rafael Espindola | 4549d7f | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 8857 | ActOnDocumentableDecls(Group); |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 8858 | |
Rafael Espindola | 4549d7f | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 8859 | return DeclGroupPtrTy::make( |
8860 | DeclGroupRef::Create(Context, Group.data(), Group.size())); | ||||
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8861 | } |
Steve Naroff | e1223f7 | 2007-08-28 03:03:08 +0000 | [diff] [blame] | 8862 | |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 8863 | void Sema::ActOnDocumentableDecl(Decl *D) { |
Rafael Espindola | 4549d7f | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 8864 | ActOnDocumentableDecls(D); |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 8865 | } |
8866 | |||||
Rafael Espindola | 4549d7f | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 8867 | void Sema::ActOnDocumentableDecls(ArrayRef<Decl *> Group) { |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 8868 | // Don't parse the comment if Doxygen diagnostics are ignored. |
Rafael Espindola | 4549d7f | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 8869 | if (Group.empty() || !Group[0]) |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 8870 | return; |
8871 | |||||
8872 | if (Diags.getDiagnosticLevel(diag::warn_doc_param_not_found, | ||||
8873 | Group[0]->getLocation()) | ||||
8874 | == DiagnosticsEngine::Ignored) | ||||
8875 | return; | ||||
8876 | |||||
Rafael Espindola | 4549d7f | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 8877 | if (Group.size() >= 2) { |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 8878 | // This is a decl group. Normally it will contain only declarations |
Rafael Espindola | 4549d7f | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 8879 | // produced from declarator list. But in case we have any definitions or |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 8880 | // additional declaration references: |
8881 | // 'typedef struct S {} S;' | ||||
8882 | // 'typedef struct S *S;' | ||||
8883 | // 'struct S *pS;' | ||||
8884 | // FinalizeDeclaratorGroup adds these as separate declarations. | ||||
8885 | Decl *MaybeTagDecl = Group[0]; | ||||
8886 | if (MaybeTagDecl && isa<TagDecl>(MaybeTagDecl)) { | ||||
Rafael Espindola | 4549d7f | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 8887 | Group = Group.slice(1); |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 8888 | } |
8889 | } | ||||
8890 | |||||
8891 | // See if there are any new comments that are not attached to a decl. | ||||
8892 | ArrayRef<RawComment *> Comments = Context.getRawCommentList().getComments(); | ||||
8893 | if (!Comments.empty() && | ||||
8894 | !Comments.back()->isAttached()) { | ||||
8895 | // There is at least one comment that not attached to a decl. | ||||
8896 | // Maybe it should be attached to one of these decls? | ||||
8897 | // | ||||
8898 | // Note that this way we pick up not only comments that precede the | ||||
8899 | // declaration, but also comments that *follow* the declaration -- thanks to | ||||
8900 | // the lookahead in the lexer: we've consumed the semicolon and looked | ||||
8901 | // ahead through comments. | ||||
Rafael Espindola | 4549d7f | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 8902 | for (unsigned i = 0, e = Group.size(); i != e; ++i) |
Dmitri Gribenko | 1952354 | 2012-09-29 11:40:46 +0000 | [diff] [blame] | 8903 | Context.getCommentForDecl(Group[i], &PP); |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 8904 | } |
8905 | } | ||||
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 8906 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 8907 | /// ActOnParamDeclarator - Called from Parser::ParseFunctionDeclarator() |
8908 | /// to introduce parameters into function prototype scope. | ||||
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8909 | Decl *Sema::ActOnParamDeclarator(Scope *S, Declarator &D) { |
Chris Lattner | 985abd9 | 2008-06-26 06:49:43 +0000 | [diff] [blame] | 8910 | const DeclSpec &DS = D.getDeclSpec(); |
Douglas Gregor | 584049d | 2008-12-15 23:53:10 +0000 | [diff] [blame] | 8911 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 8912 | // Verify C99 6.7.5.3p2: The only SCS allowed is 'register'. |
Faisal Vali | ecb5819 | 2013-08-22 01:49:11 +0000 | [diff] [blame] | 8913 | |
Peter Collingbourne | 7a8a2e3 | 2011-10-21 11:55:09 +0000 | [diff] [blame] | 8914 | // C++03 [dcl.stc]p2 also permits 'auto'. |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 8915 | VarDecl::StorageClass StorageClass = SC_None; |
Daniel Dunbar | 33ad012 | 2008-09-03 21:54:21 +0000 | [diff] [blame] | 8916 | if (DS.getStorageClassSpec() == DeclSpec::SCS_register) { |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 8917 | StorageClass = SC_Register; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8918 | } else if (getLangOpts().CPlusPlus && |
Peter Collingbourne | 7a8a2e3 | 2011-10-21 11:55:09 +0000 | [diff] [blame] | 8919 | DS.getStorageClassSpec() == DeclSpec::SCS_auto) { |
8920 | StorageClass = SC_Auto; | ||||
Daniel Dunbar | 33ad012 | 2008-09-03 21:54:21 +0000 | [diff] [blame] | 8921 | } else if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified) { |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 8922 | Diag(DS.getStorageClassSpecLoc(), |
8923 | diag::err_invalid_storage_class_in_func_decl); | ||||
Chris Lattner | 985abd9 | 2008-06-26 06:49:43 +0000 | [diff] [blame] | 8924 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 8925 | } |
Eli Friedman | 63054b3 | 2009-04-19 20:27:55 +0000 | [diff] [blame] | 8926 | |
Richard Smith | ec64244 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 8927 | if (DeclSpec::TSCS TSCS = DS.getThreadStorageClassSpec()) |
8928 | Diag(DS.getThreadStorageClassSpecLoc(), diag::err_invalid_thread) | ||||
8929 | << DeclSpec::getSpecifierName(TSCS); | ||||
8930 | if (DS.isConstexprSpecified()) | ||||
8931 | Diag(DS.getConstexprSpecLoc(), diag::err_invalid_constexpr) | ||||
Richard Smith | af1fc7a | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 8932 | << 0; |
Eli Friedman | 63054b3 | 2009-04-19 20:27:55 +0000 | [diff] [blame] | 8933 | |
Richard Smith | ec64244 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 8934 | DiagnoseFunctionSpecifiers(DS); |
Eli Friedman | 85a5319 | 2009-04-07 19:37:57 +0000 | [diff] [blame] | 8935 | |
Argyrios Kyrtzidis | 3215398 | 2011-06-28 03:01:15 +0000 | [diff] [blame] | 8936 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 8937 | QualType parmDeclType = TInfo->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8938 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8939 | if (getLangOpts().CPlusPlus) { |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 8940 | // Check that there are no default arguments inside the type of this |
8941 | // parameter. | ||||
8942 | CheckExtraCXXDefaultArguments(D); | ||||
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 8943 | |
8944 | // Parameter declarators cannot be qualified (C++ [dcl.meaning]p1). | ||||
8945 | if (D.getCXXScopeSpec().isSet()) { | ||||
8946 | Diag(D.getIdentifierLoc(), diag::err_qualified_param_declarator) | ||||
8947 | << D.getCXXScopeSpec().getRange(); | ||||
8948 | D.getCXXScopeSpec().clear(); | ||||
8949 | } | ||||
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 8950 | } |
8951 | |||||
Sean Hunt | 7533a5b | 2010-11-03 01:07:06 +0000 | [diff] [blame] | 8952 | // Ensure we have a valid name |
8953 | IdentifierInfo *II = 0; | ||||
8954 | if (D.hasName()) { | ||||
8955 | II = D.getIdentifier(); | ||||
8956 | if (!II) { | ||||
8957 | Diag(D.getIdentifierLoc(), diag::err_bad_parameter_name) | ||||
8958 | << GetNameForDeclarator(D).getName().getAsString(); | ||||
8959 | D.setInvalidType(true); | ||||
8960 | } | ||||
8961 | } | ||||
8962 | |||||
Chris Lattner | d84aac1 | 2010-02-22 00:40:25 +0000 | [diff] [blame] | 8963 | // 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] | 8964 | if (II) { |
John McCall | 10f2873 | 2010-03-18 06:42:38 +0000 | [diff] [blame] | 8965 | LookupResult R(*this, II, D.getIdentifierLoc(), LookupOrdinaryName, |
8966 | ForRedeclaration); | ||||
8967 | LookupName(R, S); | ||||
8968 | if (R.isSingleResult()) { | ||||
8969 | NamedDecl *PrevDecl = R.getFoundDecl(); | ||||
Chris Lattner | cf79b01 | 2009-01-21 02:38:50 +0000 | [diff] [blame] | 8970 | if (PrevDecl->isTemplateParameter()) { |
8971 | // Maybe we will complain about the shadowed template parameter. | ||||
8972 | DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), PrevDecl); | ||||
8973 | // Just pretend that we didn't see the previous declaration. | ||||
8974 | PrevDecl = 0; | ||||
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8975 | } else if (S->isDeclScope(PrevDecl)) { |
Chris Lattner | cf79b01 | 2009-01-21 02:38:50 +0000 | [diff] [blame] | 8976 | Diag(D.getIdentifierLoc(), diag::err_param_redefinition) << II; |
Chris Lattner | d84aac1 | 2010-02-22 00:40:25 +0000 | [diff] [blame] | 8977 | Diag(PrevDecl->getLocation(), diag::note_previous_declaration); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 8978 | |
Chris Lattner | cf79b01 | 2009-01-21 02:38:50 +0000 | [diff] [blame] | 8979 | // Recover by removing the name |
8980 | II = 0; | ||||
8981 | D.SetIdentifier(0, D.getIdentifierLoc()); | ||||
Fariborz Jahanian | 9a66c30 | 2010-02-12 21:53:14 +0000 | [diff] [blame] | 8982 | D.setInvalidType(true); |
Chris Lattner | cf79b01 | 2009-01-21 02:38:50 +0000 | [diff] [blame] | 8983 | } |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 8984 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8985 | } |
Steve Naroff | 6a9f3e3 | 2007-08-07 22:44:21 +0000 | [diff] [blame] | 8986 | |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 8987 | // Temporarily put parameter variables in the translation unit, not |
8988 | // the enclosing context. This prevents them from accidentally | ||||
8989 | // looking like class members in C++. | ||||
Douglas Gregor | cb27b0f | 2010-04-12 07:48:19 +0000 | [diff] [blame] | 8990 | ParmVarDecl *New = CheckParameter(Context.getTranslationUnitDecl(), |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 8991 | D.getLocStart(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 8992 | D.getIdentifierLoc(), II, |
8993 | parmDeclType, TInfo, | ||||
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 8994 | StorageClass); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8995 | |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 8996 | if (D.isInvalidType()) |
John McCall | fb44de9 | 2011-05-01 22:35:37 +0000 | [diff] [blame] | 8997 | New->setInvalidDecl(); |
8998 | |||||
8999 | assert(S->isFunctionPrototypeScope()); | ||||
9000 | assert(S->getFunctionPrototypeDepth() >= 1); | ||||
9001 | New->setScopeInfo(S->getFunctionPrototypeDepth() - 1, | ||||
9002 | S->getNextFunctionPrototypeIndex()); | ||||
Douglas Gregor | cb27b0f | 2010-04-12 07:48:19 +0000 | [diff] [blame] | 9003 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 9004 | // Add the parameter declaration into this scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 9005 | S->AddDecl(New); |
Argyrios Kyrtzidis | 87f3ff0 | 2008-04-12 00:47:19 +0000 | [diff] [blame] | 9006 | if (II) |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 9007 | IdResolver.AddDecl(New); |
Nate Begeman | b7894b5 | 2008-02-17 21:20:31 +0000 | [diff] [blame] | 9008 | |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 9009 | ProcessDeclAttributes(S, New, D); |
Mike Stump | ea000bf | 2009-04-30 00:19:40 +0000 | [diff] [blame] | 9010 | |
Douglas Gregor | e389585 | 2011-09-12 18:37:38 +0000 | [diff] [blame] | 9011 | if (D.getDeclSpec().isModulePrivateSpecified()) |
9012 | Diag(New->getLocation(), diag::err_module_private_local) | ||||
9013 | << 1 << New->getDeclName() | ||||
9014 | << SourceRange(D.getDeclSpec().getModulePrivateSpecLoc()) | ||||
9015 | << FixItHint::CreateRemoval(D.getDeclSpec().getModulePrivateSpecLoc()); | ||||
9016 | |||||
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 9017 | if (New->hasAttr<BlocksAttr>()) { |
Mike Stump | ea000bf | 2009-04-30 00:19:40 +0000 | [diff] [blame] | 9018 | Diag(New->getLocation(), diag::err_block_on_nonlocal); |
9019 | } | ||||
Faisal Vali | ecb5819 | 2013-08-22 01:49:11 +0000 | [diff] [blame] | 9020 | |
9021 | // Handle 'auto' within a generic lambda. | ||||
9022 | QualType ParamType = New->getType(); | ||||
9023 | if (getLangOpts().CPlusPlus1y && ParamType->getContainedAutoType()) { | ||||
9024 | assert(getCurLambda() && | ||||
9025 | "'auto' in parameter type only allowed in lambdas!"); | ||||
9026 | New = ActOnLambdaAutoParameter(New); | ||||
9027 | } | ||||
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 9028 | return New; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 9029 | } |
Fariborz Jahanian | 306d68f | 2007-11-08 23:49:49 +0000 | [diff] [blame] | 9030 | |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9031 | /// \brief Synthesizes a variable for a parameter arising from a |
9032 | /// typedef. | ||||
9033 | ParmVarDecl *Sema::BuildParmVarDeclForTypedef(DeclContext *DC, | ||||
9034 | SourceLocation Loc, | ||||
9035 | QualType T) { | ||||
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 9036 | /* FIXME: setting StartLoc == Loc. |
9037 | Would it be worth to modify callers so as to provide proper source | ||||
9038 | location for the unnamed parameters, embedding the parameter's type? */ | ||||
9039 | ParmVarDecl *Param = ParmVarDecl::Create(Context, DC, Loc, Loc, 0, | ||||
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9040 | T, Context.getTrivialTypeSourceInfo(T, Loc), |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 9041 | SC_None, 0); |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9042 | Param->setImplicit(); |
9043 | return Param; | ||||
9044 | } | ||||
9045 | |||||
John McCall | fbce0e1 | 2010-08-24 09:05:15 +0000 | [diff] [blame] | 9046 | void Sema::DiagnoseUnusedParameters(ParmVarDecl * const *Param, |
9047 | ParmVarDecl * const *ParamEnd) { | ||||
John McCall | fbce0e1 | 2010-08-24 09:05:15 +0000 | [diff] [blame] | 9048 | // Don't diagnose unused-parameter errors in template instantiations; we |
9049 | // will already have done so in the template itself. | ||||
9050 | if (!ActiveTemplateInstantiations.empty()) | ||||
9051 | return; | ||||
9052 | |||||
9053 | for (; Param != ParamEnd; ++Param) { | ||||
Eli Friedman | dd9d645 | 2012-01-13 23:41:25 +0000 | [diff] [blame] | 9054 | if (!(*Param)->isReferenced() && (*Param)->getDeclName() && |
John McCall | fbce0e1 | 2010-08-24 09:05:15 +0000 | [diff] [blame] | 9055 | !(*Param)->hasAttr<UnusedAttr>()) { |
9056 | Diag((*Param)->getLocation(), diag::warn_unused_parameter) | ||||
9057 | << (*Param)->getDeclName(); | ||||
9058 | } | ||||
9059 | } | ||||
9060 | } | ||||
9061 | |||||
Argyrios Kyrtzidis | 3532fdd | 2010-11-17 23:11:54 +0000 | [diff] [blame] | 9062 | void Sema::DiagnoseSizeOfParametersAndReturnValue(ParmVarDecl * const *Param, |
9063 | ParmVarDecl * const *ParamEnd, | ||||
9064 | QualType ReturnTy, | ||||
9065 | NamedDecl *D) { | ||||
Argyrios Kyrtzidis | 1380a14 | 2010-11-18 00:20:36 +0000 | [diff] [blame] | 9066 | if (LangOpts.NumLargeByValueCopy == 0) // No check. |
Argyrios Kyrtzidis | 3532fdd | 2010-11-17 23:11:54 +0000 | [diff] [blame] | 9067 | return; |
9068 | |||||
Argyrios Kyrtzidis | 1380a14 | 2010-11-18 00:20:36 +0000 | [diff] [blame] | 9069 | // Warn if the return value is pass-by-value and larger than the specified |
9070 | // threshold. | ||||
Eli Friedman | d18840d | 2012-01-09 23:46:59 +0000 | [diff] [blame] | 9071 | if (!ReturnTy->isDependentType() && ReturnTy.isPODType(Context)) { |
Argyrios Kyrtzidis | 3532fdd | 2010-11-17 23:11:54 +0000 | [diff] [blame] | 9072 | unsigned Size = Context.getTypeSizeInChars(ReturnTy).getQuantity(); |
Argyrios Kyrtzidis | 1380a14 | 2010-11-18 00:20:36 +0000 | [diff] [blame] | 9073 | if (Size > LangOpts.NumLargeByValueCopy) |
Argyrios Kyrtzidis | 3532fdd | 2010-11-17 23:11:54 +0000 | [diff] [blame] | 9074 | Diag(D->getLocation(), diag::warn_return_value_size) |
9075 | << D->getDeclName() << Size; | ||||
9076 | } | ||||
9077 | |||||
Argyrios Kyrtzidis | 1380a14 | 2010-11-18 00:20:36 +0000 | [diff] [blame] | 9078 | // Warn if any parameter is pass-by-value and larger than the specified |
9079 | // threshold. | ||||
Argyrios Kyrtzidis | 3532fdd | 2010-11-17 23:11:54 +0000 | [diff] [blame] | 9080 | for (; Param != ParamEnd; ++Param) { |
9081 | QualType T = (*Param)->getType(); | ||||
Eli Friedman | d18840d | 2012-01-09 23:46:59 +0000 | [diff] [blame] | 9082 | if (T->isDependentType() || !T.isPODType(Context)) |
Argyrios Kyrtzidis | 3532fdd | 2010-11-17 23:11:54 +0000 | [diff] [blame] | 9083 | continue; |
9084 | unsigned Size = Context.getTypeSizeInChars(T).getQuantity(); | ||||
Argyrios Kyrtzidis | 1380a14 | 2010-11-18 00:20:36 +0000 | [diff] [blame] | 9085 | if (Size > LangOpts.NumLargeByValueCopy) |
Argyrios Kyrtzidis | 3532fdd | 2010-11-17 23:11:54 +0000 | [diff] [blame] | 9086 | Diag((*Param)->getLocation(), diag::warn_parameter_size) |
9087 | << (*Param)->getDeclName() << Size; | ||||
9088 | } | ||||
9089 | } | ||||
9090 | |||||
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 9091 | ParmVarDecl *Sema::CheckParameter(DeclContext *DC, SourceLocation StartLoc, |
9092 | SourceLocation NameLoc, IdentifierInfo *Name, | ||||
9093 | QualType T, TypeSourceInfo *TSInfo, | ||||
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 9094 | VarDecl::StorageClass StorageClass) { |
Reid Kleckner | c910d4c | 2013-06-08 18:19:52 +0000 | [diff] [blame] | 9095 | // In ARC, infer a lifetime qualifier for appropriate parameter types. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9096 | if (getLangOpts().ObjCAutoRefCount && |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9097 | T.getObjCLifetime() == Qualifiers::OCL_None && |
Reid Kleckner | c910d4c | 2013-06-08 18:19:52 +0000 | [diff] [blame] | 9098 | T->isObjCLifetimeType()) { |
9099 | |||||
9100 | Qualifiers::ObjCLifetime lifetime; | ||||
9101 | |||||
9102 | // Special cases for arrays: | ||||
9103 | // - if it's const, use __unsafe_unretained | ||||
9104 | // - otherwise, it's an error | ||||
9105 | if (T->isArrayType()) { | ||||
9106 | if (!T.isConstQualified()) { | ||||
9107 | DelayedDiagnostics.add( | ||||
9108 | sema::DelayedDiagnostic::makeForbiddenType( | ||||
Fariborz Jahanian | 175fb10 | 2011-10-03 22:11:57 +0000 | [diff] [blame] | 9109 | NameLoc, diag::err_arc_array_param_no_ownership, T, false)); |
Reid Kleckner | c910d4c | 2013-06-08 18:19:52 +0000 | [diff] [blame] | 9110 | } |
9111 | lifetime = Qualifiers::OCL_ExplicitNone; | ||||
9112 | } else { | ||||
9113 | lifetime = T->getObjCARCImplicitLifetime(); | ||||
9114 | } | ||||
9115 | T = Context.getLifetimeQualifiedType(T, lifetime); | ||||
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9116 | } |
9117 | |||||
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 9118 | ParmVarDecl *New = ParmVarDecl::Create(Context, DC, StartLoc, NameLoc, Name, |
Douglas Gregor | 79e6bd3 | 2011-07-12 04:42:08 +0000 | [diff] [blame] | 9119 | Context.getAdjustedParameterType(T), |
9120 | TSInfo, | ||||
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 9121 | StorageClass, 0); |
Douglas Gregor | cb27b0f | 2010-04-12 07:48:19 +0000 | [diff] [blame] | 9122 | |
9123 | // Parameters can not be abstract class types. | ||||
9124 | // For record types, this is done by the AbstractClassUsageDiagnoser once | ||||
9125 | // the class has been completely parsed. | ||||
9126 | if (!CurContext->isRecord() && | ||||
9127 | RequireNonAbstractType(NameLoc, T, diag::err_abstract_type_in_decl, | ||||
9128 | AbstractParamType)) | ||||
9129 | New->setInvalidDecl(); | ||||
9130 | |||||
9131 | // Parameter declarators cannot be interface types. All ObjC objects are | ||||
9132 | // passed by reference. | ||||
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 9133 | if (T->isObjCObjectType()) { |
Fariborz Jahanian | 1de6a6c | 2012-05-09 21:49:29 +0000 | [diff] [blame] | 9134 | SourceLocation TypeEndLoc = TSInfo->getTypeLoc().getLocEnd(); |
Douglas Gregor | cb27b0f | 2010-04-12 07:48:19 +0000 | [diff] [blame] | 9135 | Diag(NameLoc, |
Fariborz Jahanian | 8eaefdc | 2011-07-26 17:58:54 +0000 | [diff] [blame] | 9136 | diag::err_object_cannot_be_passed_returned_by_value) << 1 << T |
Fariborz Jahanian | 1de6a6c | 2012-05-09 21:49:29 +0000 | [diff] [blame] | 9137 | << FixItHint::CreateInsertion(TypeEndLoc, "*"); |
Fariborz Jahanian | 8eaefdc | 2011-07-26 17:58:54 +0000 | [diff] [blame] | 9138 | T = Context.getObjCObjectPointerType(T); |
9139 | New->setType(T); | ||||
Douglas Gregor | cb27b0f | 2010-04-12 07:48:19 +0000 | [diff] [blame] | 9140 | } |
9141 | |||||
9142 | // ISO/IEC TR 18037 S6.7.3: "The type of an object with automatic storage | ||||
9143 | // duration shall not be qualified by an address-space qualifier." | ||||
9144 | // Since all parameters have automatic store duration, they can not have | ||||
9145 | // an address space. | ||||
9146 | if (T.getAddressSpace() != 0) { | ||||
9147 | Diag(NameLoc, diag::err_arg_with_address_space); | ||||
9148 | New->setInvalidDecl(); | ||||
9149 | } | ||||
9150 | |||||
9151 | return New; | ||||
9152 | } | ||||
9153 | |||||
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 9154 | void Sema::ActOnFinishKNRParamDeclarations(Scope *S, Declarator &D, |
9155 | SourceLocation LocAfterDecls) { | ||||
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 9156 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo(); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 9157 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 9158 | // Verify 6.9.1p6: 'every identifier in the identifier list shall be declared' |
9159 | // for a K&R function. | ||||
9160 | if (!FTI.hasPrototype) { | ||||
Douglas Gregor | 2610348 | 2009-04-02 03:14:12 +0000 | [diff] [blame] | 9161 | for (int i = FTI.NumArgs; i != 0; /* decrement in loop */) { |
9162 | --i; | ||||
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 9163 | if (FTI.ArgInfo[i].Param == 0) { |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 9164 | SmallString<256> Code; |
Daniel Dunbar | 5ffe14c | 2009-10-18 20:26:27 +0000 | [diff] [blame] | 9165 | llvm::raw_svector_ostream(Code) << " int " |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 9166 | << FTI.ArgInfo[i].Ident->getName() |
Daniel Dunbar | 5ffe14c | 2009-10-18 20:26:27 +0000 | [diff] [blame] | 9167 | << ";\n"; |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 9168 | Diag(FTI.ArgInfo[i].IdentLoc, diag::ext_param_not_declared) |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 9169 | << FTI.ArgInfo[i].Ident |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 9170 | << FixItHint::CreateInsertion(LocAfterDecls, Code.str()); |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 9171 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 9172 | // Implicitly declare the argument as type 'int' for lack of a better |
9173 | // type. | ||||
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 9174 | AttributeFactory attrs; |
9175 | DeclSpec DS(attrs); | ||||
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 9176 | const char* PrevSpec; // unused |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 9177 | unsigned DiagID; // unused |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9178 | DS.SetTypeSpecType(DeclSpec::TST_int, FTI.ArgInfo[i].IdentLoc, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 9179 | PrevSpec, DiagID); |
Abramo Bagnara | 16467f2 | 2012-10-04 21:38:29 +0000 | [diff] [blame] | 9180 | // Use the identifier location for the type source range. |
9181 | DS.SetRangeStart(FTI.ArgInfo[i].IdentLoc); | ||||
9182 | DS.SetRangeEnd(FTI.ArgInfo[i].IdentLoc); | ||||
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 9183 | Declarator ParamD(DS, Declarator::KNRTypeListContext); |
9184 | ParamD.SetIdentifier(FTI.ArgInfo[i].Ident, FTI.ArgInfo[i].IdentLoc); | ||||
Douglas Gregor | be109b3 | 2009-01-23 16:23:13 +0000 | [diff] [blame] | 9185 | FTI.ArgInfo[i].Param = ActOnParamDeclarator(S, ParamD); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 9186 | } |
9187 | } | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9188 | } |
Douglas Gregor | be109b3 | 2009-01-23 16:23:13 +0000 | [diff] [blame] | 9189 | } |
9190 | |||||
Richard Smith | 87162c2 | 2012-04-17 22:30:01 +0000 | [diff] [blame] | 9191 | Decl *Sema::ActOnStartOfFunctionDef(Scope *FnBodyScope, Declarator &D) { |
Douglas Gregor | be109b3 | 2009-01-23 16:23:13 +0000 | [diff] [blame] | 9192 | assert(getCurFunctionDecl() == 0 && "Function parsing confused"); |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 9193 | assert(D.isFunctionDeclarator() && "Not a function declarator!"); |
Douglas Gregor | 584049d | 2008-12-15 23:53:10 +0000 | [diff] [blame] | 9194 | Scope *ParentScope = FnBodyScope->getParent(); |
Steve Naroff | adbbd0c | 2008-01-14 20:51:29 +0000 | [diff] [blame] | 9195 | |
Douglas Gregor | 45fa560 | 2011-11-07 20:56:01 +0000 | [diff] [blame] | 9196 | D.setFunctionDefinitionKind(FDK_Definition); |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 9197 | Decl *DP = HandleDeclarator(ParentScope, D, MultiTemplateParamsArg()); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 9198 | return ActOnStartOfFunctionDef(FnBodyScope, DP); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 9199 | } |
9200 | |||||
Anders Carlsson | 8a0086c | 2012-12-18 01:29:20 +0000 | [diff] [blame] | 9201 | static bool ShouldWarnAboutMissingPrototype(const FunctionDecl *FD, |
9202 | const FunctionDecl*& PossibleZeroParamPrototype) { | ||||
Anders Carlsson | 9f89dd7 | 2009-12-09 03:30:09 +0000 | [diff] [blame] | 9203 | // Don't warn about invalid declarations. |
9204 | if (FD->isInvalidDecl()) | ||||
9205 | return false; | ||||
Anders Carlsson | 63fb673 | 2009-12-09 03:44:46 +0000 | [diff] [blame] | 9206 | |
Anders Carlsson | 9f89dd7 | 2009-12-09 03:30:09 +0000 | [diff] [blame] | 9207 | // Or declarations that aren't global. |
9208 | if (!FD->isGlobal()) | ||||
9209 | return false; | ||||
Anders Carlsson | 63fb673 | 2009-12-09 03:44:46 +0000 | [diff] [blame] | 9210 | |
Anders Carlsson | 9f89dd7 | 2009-12-09 03:30:09 +0000 | [diff] [blame] | 9211 | // Don't warn about C++ member functions. |
9212 | if (isa<CXXMethodDecl>(FD)) | ||||
9213 | return false; | ||||
Anders Carlsson | 63fb673 | 2009-12-09 03:44:46 +0000 | [diff] [blame] | 9214 | |
Anders Carlsson | 9f89dd7 | 2009-12-09 03:30:09 +0000 | [diff] [blame] | 9215 | // Don't warn about 'main'. |
9216 | if (FD->isMain()) | ||||
9217 | return false; | ||||
Anders Carlsson | 63fb673 | 2009-12-09 03:44:46 +0000 | [diff] [blame] | 9218 | |
Anders Carlsson | 9f89dd7 | 2009-12-09 03:30:09 +0000 | [diff] [blame] | 9219 | // Don't warn about inline functions. |
John McCall | 850d3b3 | 2011-03-22 07:16:37 +0000 | [diff] [blame] | 9220 | if (FD->isInlined()) |
Anders Carlsson | 9f89dd7 | 2009-12-09 03:30:09 +0000 | [diff] [blame] | 9221 | return false; |
Anders Carlsson | 63fb673 | 2009-12-09 03:44:46 +0000 | [diff] [blame] | 9222 | |
9223 | // Don't warn about function templates. | ||||
9224 | if (FD->getDescribedFunctionTemplate()) | ||||
9225 | return false; | ||||
9226 | |||||
9227 | // Don't warn about function template specializations. | ||||
9228 | if (FD->isFunctionTemplateSpecialization()) | ||||
9229 | return false; | ||||
9230 | |||||
Tanya Lattner | a95b4f7 | 2012-07-26 00:08:28 +0000 | [diff] [blame] | 9231 | // Don't warn for OpenCL kernels. |
9232 | if (FD->hasAttr<OpenCLKernelAttr>()) | ||||
9233 | return false; | ||||
9234 | |||||
Anders Carlsson | 9f89dd7 | 2009-12-09 03:30:09 +0000 | [diff] [blame] | 9235 | bool MissingPrototype = true; |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 9236 | for (const FunctionDecl *Prev = FD->getPreviousDecl(); |
9237 | Prev; Prev = Prev->getPreviousDecl()) { | ||||
Anders Carlsson | 9f89dd7 | 2009-12-09 03:30:09 +0000 | [diff] [blame] | 9238 | // Ignore any declarations that occur in function or method |
9239 | // scope, because they aren't visible from the header. | ||||
9240 | if (Prev->getDeclContext()->isFunctionOrMethod()) | ||||
9241 | continue; | ||||
9242 | |||||
9243 | MissingPrototype = !Prev->getType()->isFunctionProtoType(); | ||||
Anders Carlsson | 8a0086c | 2012-12-18 01:29:20 +0000 | [diff] [blame] | 9244 | if (FD->getNumParams() == 0) |
9245 | PossibleZeroParamPrototype = Prev; | ||||
Anders Carlsson | 9f89dd7 | 2009-12-09 03:30:09 +0000 | [diff] [blame] | 9246 | break; |
9247 | } | ||||
9248 | |||||
9249 | return MissingPrototype; | ||||
9250 | } | ||||
9251 | |||||
Francois Pichet | d4a0caf | 2011-04-22 23:20:44 +0000 | [diff] [blame] | 9252 | void Sema::CheckForFunctionRedefinition(FunctionDecl *FD) { |
9253 | // Don't complain if we're in GNU89 mode and the previous definition | ||||
9254 | // was an extern inline function. | ||||
9255 | const FunctionDecl *Definition; | ||||
Sean Hunt | 10620eb | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 9256 | if (FD->isDefined(Definition) && |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9257 | !canRedefineFunction(Definition, getLangOpts())) { |
9258 | if (getLangOpts().GNUMode && Definition->isInlineSpecified() && | ||||
Francois Pichet | d4a0caf | 2011-04-22 23:20:44 +0000 | [diff] [blame] | 9259 | Definition->getStorageClass() == SC_Extern) |
9260 | Diag(FD->getLocation(), diag::err_redefinition_extern_inline) | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9261 | << FD->getDeclName() << getLangOpts().CPlusPlus; |
Francois Pichet | d4a0caf | 2011-04-22 23:20:44 +0000 | [diff] [blame] | 9262 | else |
9263 | Diag(FD->getLocation(), diag::err_redefinition) << FD->getDeclName(); | ||||
9264 | Diag(Definition->getLocation(), diag::note_previous_definition); | ||||
Richard Smith | eef0029 | 2012-08-06 02:25:10 +0000 | [diff] [blame] | 9265 | FD->setInvalidDecl(); |
Francois Pichet | d4a0caf | 2011-04-22 23:20:44 +0000 | [diff] [blame] | 9266 | } |
9267 | } | ||||
9268 | |||||
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 9269 | Decl *Sema::ActOnStartOfFunctionDef(Scope *FnBodyScope, Decl *D) { |
Anders Carlsson | 0ebb6d3 | 2009-10-29 15:46:07 +0000 | [diff] [blame] | 9270 | // Clear the last template instantiation error context. |
9271 | LastTemplateInstantiationErrorContext = ActiveTemplateInstantiation(); | ||||
9272 | |||||
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 9273 | if (!D) |
9274 | return D; | ||||
Douglas Gregor | d83d040 | 2009-08-22 00:34:47 +0000 | [diff] [blame] | 9275 | FunctionDecl *FD = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9276 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 9277 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D)) |
Douglas Gregor | d83d040 | 2009-08-22 00:34:47 +0000 | [diff] [blame] | 9278 | FD = FunTmpl->getTemplatedDecl(); |
9279 | else | ||||
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 9280 | FD = cast<FunctionDecl>(D); |
Faisal Vali | ecb5819 | 2013-08-22 01:49:11 +0000 | [diff] [blame] | 9281 | // If we are instantiating a generic lambda call operator, push |
9282 | // a LambdaScopeInfo onto the function stack. But use the information | ||||
9283 | // that's already been calculated (ActOnLambdaExpr) when analyzing the | ||||
9284 | // template version, to prime the current LambdaScopeInfo. | ||||
9285 | if (getLangOpts().CPlusPlus1y | ||||
9286 | && isGenericLambdaCallOperatorSpecialization(D)) { | ||||
9287 | CXXMethodDecl *CallOperator = cast<CXXMethodDecl>(D); | ||||
9288 | CXXRecordDecl *LambdaClass = CallOperator->getParent(); | ||||
9289 | LambdaExpr *LE = LambdaClass->getLambdaExpr(); | ||||
9290 | assert(LE && | ||||
9291 | "No LambdaExpr of closure class when instantiating a generic lambda!"); | ||||
9292 | assert(ActiveTemplateInstantiations.size() && | ||||
9293 | "There should be an active template instantiation on the stack " | ||||
9294 | "when instantiating a generic lambda!"); | ||||
9295 | PushLambdaScope(); | ||||
9296 | LambdaScopeInfo *LSI = getCurLambda(); | ||||
9297 | LSI->CallOperator = CallOperator; | ||||
9298 | LSI->Lambda = LambdaClass; | ||||
9299 | LSI->ReturnType = CallOperator->getResultType(); | ||||
Douglas Gregor | 6fc17ff | 2008-10-29 15:10:40 +0000 | [diff] [blame] | 9300 | |
Faisal Vali | ecb5819 | 2013-08-22 01:49:11 +0000 | [diff] [blame] | 9301 | if (LE->getCaptureDefault() == LCD_None) |
9302 | LSI->ImpCaptureStyle = CapturingScopeInfo::ImpCap_None; | ||||
9303 | else if (LE->getCaptureDefault() == LCD_ByCopy) | ||||
9304 | LSI->ImpCaptureStyle = CapturingScopeInfo::ImpCap_LambdaByval; | ||||
9305 | else if (LE->getCaptureDefault() == LCD_ByRef) | ||||
9306 | LSI->ImpCaptureStyle = CapturingScopeInfo::ImpCap_LambdaByref; | ||||
9307 | |||||
9308 | LSI->IntroducerRange = LE->getIntroducerRange(); | ||||
9309 | } | ||||
9310 | else | ||||
9311 | // Enter a new function scope | ||||
9312 | PushFunctionScope(); | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9313 | |
Douglas Gregor | 6fc17ff | 2008-10-29 15:10:40 +0000 | [diff] [blame] | 9314 | // See if this is a redefinition. |
Francois Pichet | d4a0caf | 2011-04-22 23:20:44 +0000 | [diff] [blame] | 9315 | if (!FD->isLateTemplateParsed()) |
9316 | CheckForFunctionRedefinition(FD); | ||||
Douglas Gregor | 6fc17ff | 2008-10-29 15:10:40 +0000 | [diff] [blame] | 9317 | |
Douglas Gregor | cda9c67 | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 9318 | // Builtin functions cannot be defined. |
Douglas Gregor | 7814e6d | 2009-09-12 00:22:50 +0000 | [diff] [blame] | 9319 | if (unsigned BuiltinID = FD->getBuiltinID()) { |
Rafael Espindola | ad24ad4 | 2013-06-13 18:34:17 +0000 | [diff] [blame] | 9320 | if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID) && |
9321 | !Context.BuiltinInfo.isPredefinedRuntimeFunction(BuiltinID)) { | ||||
Douglas Gregor | cda9c67 | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 9322 | Diag(FD->getLocation(), diag::err_builtin_definition) << FD; |
Douglas Gregor | 655753a | 2009-02-17 16:03:01 +0000 | [diff] [blame] | 9323 | FD->setInvalidDecl(); |
9324 | } | ||||
Douglas Gregor | cda9c67 | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 9325 | } |
9326 | |||||
Eli Friedman | 7f0f5dc | 2009-03-04 07:30:59 +0000 | [diff] [blame] | 9327 | // The return type of a function definition must be complete |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 9328 | // (C99 6.9.1p3, C++ [dcl.fct]p6). |
9329 | QualType ResultType = FD->getResultType(); | ||||
9330 | if (!ResultType->isDependentType() && !ResultType->isVoidType() && | ||||
Chris Lattner | 65e6a09 | 2009-04-29 05:12:23 +0000 | [diff] [blame] | 9331 | !FD->isInvalidDecl() && |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 9332 | RequireCompleteType(FD->getLocation(), ResultType, |
9333 | diag::err_func_def_incomplete_result)) | ||||
Eli Friedman | 7f0f5dc | 2009-03-04 07:30:59 +0000 | [diff] [blame] | 9334 | FD->setInvalidDecl(); |
Eli Friedman | 7f0f5dc | 2009-03-04 07:30:59 +0000 | [diff] [blame] | 9335 | |
Douglas Gregor | 8499f3f | 2009-03-31 16:35:03 +0000 | [diff] [blame] | 9336 | // GNU warning -Wmissing-prototypes: |
9337 | // Warn if a global function is defined without a previous | ||||
9338 | // prototype declaration. This warning is issued even if the | ||||
9339 | // definition itself provides a prototype. The aim is to detect | ||||
9340 | // global functions that fail to be declared in header files. | ||||
Anders Carlsson | 8a0086c | 2012-12-18 01:29:20 +0000 | [diff] [blame] | 9341 | const FunctionDecl *PossibleZeroParamPrototype = 0; |
9342 | if (ShouldWarnAboutMissingPrototype(FD, PossibleZeroParamPrototype)) { | ||||
Anders Carlsson | 9f89dd7 | 2009-12-09 03:30:09 +0000 | [diff] [blame] | 9343 | Diag(FD->getLocation(), diag::warn_missing_prototype) << FD; |
Richard Smith | ac83a3c | 2013-06-25 20:34:17 +0000 | [diff] [blame] | 9344 | |
Anders Carlsson | 8a0086c | 2012-12-18 01:29:20 +0000 | [diff] [blame] | 9345 | if (PossibleZeroParamPrototype) { |
Richard Smith | ac83a3c | 2013-06-25 20:34:17 +0000 | [diff] [blame] | 9346 | // We found a declaration that is not a prototype, |
Anders Carlsson | 8a0086c | 2012-12-18 01:29:20 +0000 | [diff] [blame] | 9347 | // but that could be a zero-parameter prototype |
Richard Smith | ac83a3c | 2013-06-25 20:34:17 +0000 | [diff] [blame] | 9348 | if (TypeSourceInfo *TI = |
9349 | PossibleZeroParamPrototype->getTypeSourceInfo()) { | ||||
9350 | TypeLoc TL = TI->getTypeLoc(); | ||||
9351 | if (FunctionNoProtoTypeLoc FTL = TL.getAs<FunctionNoProtoTypeLoc>()) | ||||
9352 | Diag(PossibleZeroParamPrototype->getLocation(), | ||||
9353 | diag::note_declaration_not_a_prototype) | ||||
9354 | << PossibleZeroParamPrototype | ||||
9355 | << FixItHint::CreateInsertion(FTL.getRParenLoc(), "void"); | ||||
9356 | } | ||||
Anders Carlsson | 8a0086c | 2012-12-18 01:29:20 +0000 | [diff] [blame] | 9357 | } |
9358 | } | ||||
Douglas Gregor | 8499f3f | 2009-03-31 16:35:03 +0000 | [diff] [blame] | 9359 | |
Douglas Gregor | e2c31ff | 2009-05-15 17:59:04 +0000 | [diff] [blame] | 9360 | if (FnBodyScope) |
9361 | PushDeclContext(FnBodyScope, FD); | ||||
Anton Korobeynikov | 2f40270 | 2008-12-26 00:52:02 +0000 | [diff] [blame] | 9362 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 9363 | // Check the validity of our function parameters |
Douglas Gregor | 82aa713 | 2010-11-01 18:37:59 +0000 | [diff] [blame] | 9364 | CheckParmsForFunctionDef(FD->param_begin(), FD->param_end(), |
9365 | /*CheckParameterNames=*/true); | ||||
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 9366 | |
9367 | // Introduce our parameters into the function scope | ||||
9368 | for (unsigned p = 0, NumParams = FD->getNumParams(); p < NumParams; ++p) { | ||||
9369 | ParmVarDecl *Param = FD->getParamDecl(p); | ||||
Douglas Gregor | a8cc8ce | 2009-01-09 18:51:29 +0000 | [diff] [blame] | 9370 | Param->setOwningFunction(FD); |
9371 | |||||
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 9372 | // If this has an identifier, add it to the scope stack. |
John McCall | 053f4bd | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 9373 | if (Param->getIdentifier() && FnBodyScope) { |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 9374 | CheckShadow(FnBodyScope, Param); |
John McCall | 053f4bd | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 9375 | |
Argyrios Kyrtzidis | 87f3ff0 | 2008-04-12 00:47:19 +0000 | [diff] [blame] | 9376 | PushOnScopeChains(Param, FnBodyScope); |
John McCall | 053f4bd | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 9377 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 9378 | } |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 9379 | |
James Molloy | 16f1f71 | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 9380 | // If we had any tags defined in the function prototype, |
9381 | // introduce them into the function scope. | ||||
9382 | if (FnBodyScope) { | ||||
Robert Wilhelm | 834c058 | 2013-08-09 18:02:13 +0000 | [diff] [blame] | 9383 | for (ArrayRef<NamedDecl *>::iterator |
9384 | I = FD->getDeclsInPrototypeScope().begin(), | ||||
9385 | E = FD->getDeclsInPrototypeScope().end(); | ||||
9386 | I != E; ++I) { | ||||
James Molloy | 16f1f71 | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 9387 | NamedDecl *D = *I; |
9388 | |||||
9389 | // Some of these decls (like enums) may have been pinned to the translation unit | ||||
9390 | // for lack of a real context earlier. If so, remove from the translation unit | ||||
9391 | // and reattach to the current context. | ||||
9392 | if (D->getLexicalDeclContext() == Context.getTranslationUnitDecl()) { | ||||
9393 | // Is the decl actually in the context? | ||||
9394 | for (DeclContext::decl_iterator DI = Context.getTranslationUnitDecl()->decls_begin(), | ||||
9395 | DE = Context.getTranslationUnitDecl()->decls_end(); DI != DE; ++DI) { | ||||
9396 | if (*DI == D) { | ||||
9397 | Context.getTranslationUnitDecl()->removeDecl(D); | ||||
9398 | break; | ||||
9399 | } | ||||
9400 | } | ||||
9401 | // Either way, reassign the lexical decl context to our FunctionDecl. | ||||
9402 | D->setLexicalDeclContext(CurContext); | ||||
9403 | } | ||||
9404 | |||||
9405 | // If the decl has a non-null name, make accessible in the current scope. | ||||
9406 | if (!D->getName().empty()) | ||||
9407 | PushOnScopeChains(D, FnBodyScope, /*AddToContext=*/false); | ||||
9408 | |||||
9409 | // Similarly, dive into enums and fish their constants out, making them | ||||
9410 | // accessible in this scope. | ||||
9411 | if (EnumDecl *ED = dyn_cast<EnumDecl>(D)) { | ||||
9412 | for (EnumDecl::enumerator_iterator EI = ED->enumerator_begin(), | ||||
9413 | EE = ED->enumerator_end(); EI != EE; ++EI) | ||||
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 9414 | PushOnScopeChains(*EI, FnBodyScope, /*AddToContext=*/false); |
James Molloy | 16f1f71 | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 9415 | } |
9416 | } | ||||
9417 | } | ||||
9418 | |||||
Richard Smith | 87162c2 | 2012-04-17 22:30:01 +0000 | [diff] [blame] | 9419 | // Ensure that the function's exception specification is instantiated. |
9420 | if (const FunctionProtoType *FPT = FD->getType()->getAs<FunctionProtoType>()) | ||||
9421 | ResolveExceptionSpec(D->getLocation(), FPT); | ||||
9422 | |||||
Anton Korobeynikov | 2f40270 | 2008-12-26 00:52:02 +0000 | [diff] [blame] | 9423 | // Checking attributes of current function definition |
9424 | // dllimport attribute. | ||||
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 9425 | DLLImportAttr *DA = FD->getAttr<DLLImportAttr>(); |
9426 | if (DA && (!FD->getAttr<DLLExportAttr>())) { | ||||
9427 | // dllimport attribute cannot be directly applied to definition. | ||||
Francois Pichet | b613cd6 | 2011-03-29 10:39:17 +0000 | [diff] [blame] | 9428 | // Microsoft accepts dllimport for functions defined within class scope. |
9429 | if (!DA->isInherited() && | ||||
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 9430 | !(LangOpts.MicrosoftExt && FD->getLexicalDeclContext()->isRecord())) { |
Anton Korobeynikov | 2f40270 | 2008-12-26 00:52:02 +0000 | [diff] [blame] | 9431 | Diag(FD->getLocation(), |
9432 | diag::err_attribute_can_be_applied_only_to_symbol_declaration) | ||||
9433 | << "dllimport"; | ||||
9434 | FD->setInvalidDecl(); | ||||
Argyrios Kyrtzidis | a9990e8 | 2012-12-14 06:54:03 +0000 | [diff] [blame] | 9435 | return D; |
Ted Kremenek | 12911a8 | 2010-02-21 05:12:53 +0000 | [diff] [blame] | 9436 | } |
9437 | |||||
9438 | // Visual C++ appears to not think this is an issue, so only issue | ||||
9439 | // a warning when Microsoft extensions are disabled. | ||||
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 9440 | if (!LangOpts.MicrosoftExt) { |
Anton Korobeynikov | 2f40270 | 2008-12-26 00:52:02 +0000 | [diff] [blame] | 9441 | // If a symbol previously declared dllimport is later defined, the |
9442 | // attribute is ignored in subsequent references, and a warning is | ||||
9443 | // emitted. | ||||
9444 | Diag(FD->getLocation(), | ||||
9445 | diag::warn_redeclaration_without_attribute_prev_attribute_ignored) | ||||
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 9446 | << FD->getName() << "dllimport"; |
Anton Korobeynikov | 2f40270 | 2008-12-26 00:52:02 +0000 | [diff] [blame] | 9447 | } |
9448 | } | ||||
Dmitri Gribenko | c41ace9 | 2012-08-14 17:17:18 +0000 | [diff] [blame] | 9449 | // We want to attach documentation to original Decl (which might be |
9450 | // a function template). | ||||
9451 | ActOnDocumentableDecl(D); | ||||
Argyrios Kyrtzidis | a9990e8 | 2012-12-14 06:54:03 +0000 | [diff] [blame] | 9452 | return D; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 9453 | } |
9454 | |||||
Douglas Gregor | 5077c38 | 2010-05-15 06:01:05 +0000 | [diff] [blame] | 9455 | /// \brief Given the set of return statements within a function body, |
9456 | /// compute the variables that are subject to the named return value | ||||
9457 | /// optimization. | ||||
9458 | /// | ||||
9459 | /// Each of the variables that is subject to the named return value | ||||
9460 | /// optimization will be marked as NRVO variables in the AST, and any | ||||
9461 | /// return statement that has a marked NRVO variable as its NRVO candidate can | ||||
9462 | /// use the named return value optimization. | ||||
9463 | /// | ||||
9464 | /// This function applies a very simplistic algorithm for NRVO: if every return | ||||
9465 | /// statement in the function has the same NRVO candidate, that candidate is | ||||
9466 | /// the NRVO variable. | ||||
9467 | /// | ||||
9468 | /// FIXME: Employ a smarter algorithm that accounts for multiple return | ||||
9469 | /// statements and the lifetimes of the NRVO candidates. We should be able to | ||||
9470 | /// find a maximal set of NRVO variables. | ||||
Douglas Gregor | f8b7f71 | 2011-09-06 20:46:03 +0000 | [diff] [blame] | 9471 | void Sema::computeNRVO(Stmt *Body, FunctionScopeInfo *Scope) { |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 9472 | ReturnStmt **Returns = Scope->Returns.data(); |
9473 | |||||
Douglas Gregor | 5077c38 | 2010-05-15 06:01:05 +0000 | [diff] [blame] | 9474 | const VarDecl *NRVOCandidate = 0; |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 9475 | for (unsigned I = 0, E = Scope->Returns.size(); I != E; ++I) { |
Douglas Gregor | 5077c38 | 2010-05-15 06:01:05 +0000 | [diff] [blame] | 9476 | if (!Returns[I]->getNRVOCandidate()) |
9477 | return; | ||||
9478 | |||||
9479 | if (!NRVOCandidate) | ||||
9480 | NRVOCandidate = Returns[I]->getNRVOCandidate(); | ||||
9481 | else if (NRVOCandidate != Returns[I]->getNRVOCandidate()) | ||||
9482 | return; | ||||
9483 | } | ||||
9484 | |||||
9485 | if (NRVOCandidate) | ||||
9486 | const_cast<VarDecl*>(NRVOCandidate)->setNRVOVariable(true); | ||||
9487 | } | ||||
9488 | |||||
Richard Smith | 1a5bd5d | 2012-11-19 21:13:18 +0000 | [diff] [blame] | 9489 | bool Sema::canSkipFunctionBody(Decl *D) { |
Richard Smith | d1bac8d | 2012-11-27 21:31:01 +0000 | [diff] [blame] | 9490 | if (!Consumer.shouldSkipFunctionBody(D)) |
9491 | return false; | ||||
9492 | |||||
Richard Smith | 1a5bd5d | 2012-11-19 21:13:18 +0000 | [diff] [blame] | 9493 | if (isa<ObjCMethodDecl>(D)) |
9494 | return true; | ||||
9495 | |||||
9496 | FunctionDecl *FD = 0; | ||||
9497 | if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(D)) | ||||
9498 | FD = FTD->getTemplatedDecl(); | ||||
9499 | else | ||||
9500 | FD = cast<FunctionDecl>(D); | ||||
9501 | |||||
9502 | // We cannot skip the body of a function (or function template) which is | ||||
9503 | // constexpr, since we may need to evaluate its body in order to parse the | ||||
9504 | // rest of the file. | ||||
Richard Smith | 25d8c85 | 2013-05-10 04:31:10 +0000 | [diff] [blame] | 9505 | // We cannot skip the body of a function with an undeduced return type, |
9506 | // because any callers of that function need to know the type. | ||||
9507 | return !FD->isConstexpr() && !FD->getResultType()->isUndeducedType(); | ||||
Richard Smith | 1a5bd5d | 2012-11-19 21:13:18 +0000 | [diff] [blame] | 9508 | } |
9509 | |||||
Argyrios Kyrtzidis | 35f3f36 | 2012-12-06 18:59:10 +0000 | [diff] [blame] | 9510 | Decl *Sema::ActOnSkippedFunctionBody(Decl *Decl) { |
Argyrios Kyrtzidis | 1f12c47 | 2013-02-22 04:11:06 +0000 | [diff] [blame] | 9511 | if (FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(Decl)) |
Argyrios Kyrtzidis | 35f3f36 | 2012-12-06 18:59:10 +0000 | [diff] [blame] | 9512 | FD->setHasSkippedBody(); |
Argyrios Kyrtzidis | 1f12c47 | 2013-02-22 04:11:06 +0000 | [diff] [blame] | 9513 | else if (ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(Decl)) |
Argyrios Kyrtzidis | 35f3f36 | 2012-12-06 18:59:10 +0000 | [diff] [blame] | 9514 | MD->setHasSkippedBody(); |
9515 | return ActOnFinishFunctionBody(Decl, 0); | ||||
9516 | } | ||||
9517 | |||||
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 9518 | Decl *Sema::ActOnFinishFunctionBody(Decl *D, Stmt *BodyArg) { |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 9519 | return ActOnFinishFunctionBody(D, BodyArg, false); |
Douglas Gregor | e2c31ff | 2009-05-15 17:59:04 +0000 | [diff] [blame] | 9520 | } |
9521 | |||||
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9522 | Decl *Sema::ActOnFinishFunctionBody(Decl *dcl, Stmt *Body, |
9523 | bool IsInstantiation) { | ||||
Douglas Gregor | d83d040 | 2009-08-22 00:34:47 +0000 | [diff] [blame] | 9524 | FunctionDecl *FD = 0; |
9525 | FunctionTemplateDecl *FunTmpl = dyn_cast_or_null<FunctionTemplateDecl>(dcl); | ||||
9526 | if (FunTmpl) | ||||
9527 | FD = FunTmpl->getTemplatedDecl(); | ||||
9528 | else | ||||
9529 | FD = dyn_cast_or_null<FunctionDecl>(dcl); | ||||
9530 | |||||
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 9531 | sema::AnalysisBasedWarnings::Policy WP = AnalysisWarnings.getDefaultPolicy(); |
Ted Kremenek | 3ed6fc0 | 2011-02-23 01:51:48 +0000 | [diff] [blame] | 9532 | sema::AnalysisBasedWarnings::Policy *ActivePolicy = 0; |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 9533 | |
Douglas Gregor | d83d040 | 2009-08-22 00:34:47 +0000 | [diff] [blame] | 9534 | if (FD) { |
Chris Lattner | a5251fc | 2009-04-18 09:36:27 +0000 | [diff] [blame] | 9535 | FD->setBody(Body); |
John McCall | 75d8ba3 | 2012-02-14 19:50:52 +0000 | [diff] [blame] | 9536 | |
Richard Smith | 25d8c85 | 2013-05-10 04:31:10 +0000 | [diff] [blame] | 9537 | if (getLangOpts().CPlusPlus1y && !FD->isInvalidDecl() && Body && |
9538 | !FD->isDependentContext() && FD->getResultType()->isUndeducedType()) { | ||||
9539 | // If the function has a deduced result type but contains no 'return' | ||||
9540 | // statements, the result type as written must be exactly 'auto', and | ||||
9541 | // the deduced result type is 'void'. | ||||
9542 | if (!FD->getResultType()->getAs<AutoType>()) { | ||||
9543 | Diag(dcl->getLocation(), diag::err_auto_fn_no_return_but_not_auto) | ||||
9544 | << FD->getResultType(); | ||||
9545 | FD->setInvalidDecl(); | ||||
9546 | } else { | ||||
9547 | // Substitute 'void' for the 'auto' in the type. | ||||
9548 | TypeLoc ResultType = FD->getTypeSourceInfo()->getTypeLoc(). | ||||
9549 | IgnoreParens().castAs<FunctionProtoTypeLoc>().getResultLoc(); | ||||
9550 | Context.adjustDeducedFunctionResultType( | ||||
9551 | FD, SubstAutoType(ResultType.getType(), Context.VoidTy)); | ||||
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 9552 | } |
9553 | } | ||||
9554 | |||||
Nick Lewycky | cd0655b | 2013-02-01 08:13:20 +0000 | [diff] [blame] | 9555 | // The only way to be included in UndefinedButUsed is if there is an |
9556 | // ODR use before the definition. Avoid the expensive map lookup if this | ||||
Nick Lewycky | 995e26b | 2013-01-31 03:23:57 +0000 | [diff] [blame] | 9557 | // is the first declaration. |
Nick Lewycky | cd0655b | 2013-02-01 08:13:20 +0000 | [diff] [blame] | 9558 | if (FD->getPreviousDecl() != 0 && FD->getPreviousDecl()->isUsed()) { |
Rafael Espindola | 181e3ec | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 9559 | if (!FD->isExternallyVisible()) |
Nick Lewycky | cd0655b | 2013-02-01 08:13:20 +0000 | [diff] [blame] | 9560 | UndefinedButUsed.erase(FD); |
9561 | else if (FD->isInlined() && | ||||
9562 | (LangOpts.CPlusPlus || !LangOpts.GNUInline) && | ||||
9563 | (!FD->getPreviousDecl()->hasAttr<GNUInlineAttr>())) | ||||
9564 | UndefinedButUsed.erase(FD); | ||||
9565 | } | ||||
Nick Lewycky | 995e26b | 2013-01-31 03:23:57 +0000 | [diff] [blame] | 9566 | |
John McCall | 75d8ba3 | 2012-02-14 19:50:52 +0000 | [diff] [blame] | 9567 | // If the function implicitly returns zero (like 'main') or is naked, |
9568 | // don't complain about missing return statements. | ||||
9569 | if (FD->hasImplicitReturnZero() || FD->hasAttr<NakedAttr>()) | ||||
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 9570 | WP.disableCheckFallThrough(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9571 | |
Francois Pichet | 6a24747 | 2011-05-11 02:14:46 +0000 | [diff] [blame] | 9572 | // MSVC permits the use of pure specifier (=0) on function definition, |
9573 | // defined at class scope, warn about this non standard construct. | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9574 | if (getLangOpts().MicrosoftExt && FD->isPure()) |
Francois Pichet | 6a24747 | 2011-05-11 02:14:46 +0000 | [diff] [blame] | 9575 | Diag(FD->getLocation(), diag::warn_pure_function_definition); |
9576 | |||||
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 9577 | if (!FD->isInvalidDecl()) { |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9578 | DiagnoseUnusedParameters(FD->param_begin(), FD->param_end()); |
Argyrios Kyrtzidis | 3532fdd | 2010-11-17 23:11:54 +0000 | [diff] [blame] | 9579 | DiagnoseSizeOfParametersAndReturnValue(FD->param_begin(), FD->param_end(), |
9580 | FD->getResultType(), FD); | ||||
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 9581 | |
9582 | // If this is a constructor, we need a vtable. | ||||
9583 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(FD)) | ||||
9584 | MarkVTableUsed(FD->getLocation(), Constructor->getParent()); | ||||
Douglas Gregor | 5077c38 | 2010-05-15 06:01:05 +0000 | [diff] [blame] | 9585 | |
Jordan Rose | 7dd900e | 2012-07-02 21:19:23 +0000 | [diff] [blame] | 9586 | // Try to apply the named return value optimization. We have to check |
9587 | // if we can do this here because lambdas keep return statements around | ||||
9588 | // to deduce an implicit return type. | ||||
9589 | if (getLangOpts().CPlusPlus && FD->getResultType()->isRecordType() && | ||||
9590 | !FD->isDependentContext()) | ||||
9591 | computeNRVO(Body, getCurFunction()); | ||||
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 9592 | } |
9593 | |||||
Douglas Gregor | 76e3da5 | 2012-02-08 20:17:14 +0000 | [diff] [blame] | 9594 | assert((FD == getCurFunctionDecl() || getCurLambda()->CallOperator == FD) && |
9595 | "Function parsing confused"); | ||||
Steve Naroff | 394f3f4 | 2008-07-25 17:57:26 +0000 | [diff] [blame] | 9596 | } else if (ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(dcl)) { |
Chris Lattner | ffed163 | 2009-02-16 19:27:54 +0000 | [diff] [blame] | 9597 | assert(MD == getCurMethodDecl() && "Method parsing confused"); |
Chris Lattner | a5251fc | 2009-04-18 09:36:27 +0000 | [diff] [blame] | 9598 | MD->setBody(Body); |
Argyrios Kyrtzidis | 3532fdd | 2010-11-17 23:11:54 +0000 | [diff] [blame] | 9599 | if (!MD->isInvalidDecl()) { |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9600 | DiagnoseUnusedParameters(MD->param_begin(), MD->param_end()); |
Argyrios Kyrtzidis | 3532fdd | 2010-11-17 23:11:54 +0000 | [diff] [blame] | 9601 | DiagnoseSizeOfParametersAndReturnValue(MD->param_begin(), MD->param_end(), |
9602 | MD->getResultType(), MD); | ||||
Douglas Gregor | f7603f6 | 2011-09-06 20:33:37 +0000 | [diff] [blame] | 9603 | |
9604 | if (Body) | ||||
Douglas Gregor | f8b7f71 | 2011-09-06 20:46:03 +0000 | [diff] [blame] | 9605 | computeNRVO(Body, getCurFunction()); |
Argyrios Kyrtzidis | 3532fdd | 2010-11-17 23:11:54 +0000 | [diff] [blame] | 9606 | } |
Jordan Rose | 535a5d0 | 2012-10-19 16:05:26 +0000 | [diff] [blame] | 9607 | if (getCurFunction()->ObjCShouldCallSuper) { |
Fariborz Jahanian | 9f55983 | 2012-09-10 16:51:09 +0000 | [diff] [blame] | 9608 | Diag(MD->getLocEnd(), diag::warn_objc_missing_super_call) |
9609 | << MD->getSelector().getAsString(); | ||||
Jordan Rose | 535a5d0 | 2012-10-19 16:05:26 +0000 | [diff] [blame] | 9610 | getCurFunction()->ObjCShouldCallSuper = false; |
Nico Weber | 80cb6e6 | 2011-08-28 22:35:17 +0000 | [diff] [blame] | 9611 | } |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 9612 | } else { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 9613 | return 0; |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 9614 | } |
Douglas Gregor | e2c31ff | 2009-05-15 17:59:04 +0000 | [diff] [blame] | 9615 | |
Jordan Rose | 535a5d0 | 2012-10-19 16:05:26 +0000 | [diff] [blame] | 9616 | assert(!getCurFunction()->ObjCShouldCallSuper && |
Eli Friedman | 95aac15 | 2012-08-01 21:02:59 +0000 | [diff] [blame] | 9617 | "This should only be set for ObjC methods, which should have been " |
9618 | "handled in the block above."); | ||||
Nico Weber | 9a1ecf0 | 2011-08-22 17:25:57 +0000 | [diff] [blame] | 9619 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 9620 | // Verify and clean out per-function state. |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 9621 | if (Body) { |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 9622 | // C++ constructors that have function-try-blocks can't have return |
9623 | // statements in the handlers of that block. (C++ [except.handle]p14) | ||||
9624 | // Verify this. | ||||
9625 | if (FD && isa<CXXConstructorDecl>(FD) && isa<CXXTryStmt>(Body)) | ||||
9626 | DiagnoseReturnInConstructorExceptionHandler(cast<CXXTryStmt>(Body)); | ||||
9627 | |||||
Richard Smith | 37bee67 | 2011-08-12 18:44:32 +0000 | [diff] [blame] | 9628 | // Verify that gotos and switch cases don't jump into scopes illegally. |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 9629 | if (getCurFunction()->NeedsScopeChecking() && |
John McCall | 8a2ca74 | 2010-05-20 07:13:26 +0000 | [diff] [blame] | 9630 | !dcl->isInvalidDecl() && |
Douglas Gregor | 27bec77 | 2012-08-17 05:12:08 +0000 | [diff] [blame] | 9631 | !hasAnyUnrecoverableErrorsInThisFunction() && |
9632 | !PP.isCodeCompletionEnabled()) | ||||
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 9633 | DiagnoseInvalidJumps(Body); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9634 | |
John McCall | 1544282 | 2010-08-04 01:04:25 +0000 | [diff] [blame] | 9635 | if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(dcl)) { |
9636 | if (!Destructor->getParent()->isDependentType()) | ||||
9637 | CheckDestructor(Destructor); | ||||
9638 | |||||
John McCall | ef027fe | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 9639 | MarkBaseAndMemberDestructorsReferenced(Destructor->getLocation(), |
9640 | Destructor->getParent()); | ||||
John McCall | 1544282 | 2010-08-04 01:04:25 +0000 | [diff] [blame] | 9641 | } |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 9642 | |
9643 | // If any errors have occurred, clear out any temporaries that may have | ||||
9644 | // been leftover. This ensures that these temporaries won't be picked up for | ||||
9645 | // deletion in some later function. | ||||
Douglas Gregor | 26cd44d | 2011-03-04 23:08:02 +0000 | [diff] [blame] | 9646 | if (PP.getDiagnostics().hasErrorOccurred() || |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9647 | PP.getDiagnostics().getSuppressAllDiagnostics()) { |
John McCall | 80ee6e8 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 9648 | DiscardCleanupsInEvaluationContext(); |
DeLesley Hutchins | 12f37e4 | 2012-12-07 22:53:48 +0000 | [diff] [blame] | 9649 | } |
9650 | if (!PP.getDiagnostics().hasUncompilableErrorOccurred() && | ||||
9651 | !isa<FunctionTemplateDecl>(dcl)) { | ||||
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 9652 | // Since the body is valid, issue any analysis-based warnings that are |
9653 | // enabled. | ||||
Ted Kremenek | 3ed6fc0 | 2011-02-23 01:51:48 +0000 | [diff] [blame] | 9654 | ActivePolicy = &WP; |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 9655 | } |
9656 | |||||
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 9657 | if (!IsInstantiation && FD && FD->isConstexpr() && !FD->isInvalidDecl() && |
9658 | (!CheckConstexprFunctionDecl(FD) || | ||||
9659 | !CheckConstexprFunctionBody(FD, Body))) | ||||
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 9660 | FD->setInvalidDecl(); |
9661 | |||||
John McCall | 80ee6e8 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 9662 | assert(ExprCleanupObjects.empty() && "Leftover temporaries in function"); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9663 | assert(!ExprNeedsCleanups && "Unaccounted cleanups in function"); |
Eli Friedman | d2cce13 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 9664 | assert(MaybeODRUseExprs.empty() && |
9665 | "Leftover expressions for odr-use checking"); | ||||
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 9666 | } |
9667 | |||||
John McCall | 90f9789 | 2010-03-25 22:08:03 +0000 | [diff] [blame] | 9668 | if (!IsInstantiation) |
9669 | PopDeclContext(); | ||||
9670 | |||||
Eli Friedman | ec9ea72 | 2012-01-05 03:35:19 +0000 | [diff] [blame] | 9671 | PopFunctionScopeInfo(ActivePolicy, dcl); |
Anders Carlsson | f8a9a79 | 2009-11-13 19:21:49 +0000 | [diff] [blame] | 9672 | |
Douglas Gregor | d5b5728 | 2009-11-15 07:07:58 +0000 | [diff] [blame] | 9673 | // If any errors have occurred, clear out any temporaries that may have |
9674 | // been leftover. This ensures that these temporaries won't be picked up for | ||||
9675 | // deletion in some later function. | ||||
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9676 | if (getDiagnostics().hasErrorOccurred()) { |
John McCall | 80ee6e8 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 9677 | DiscardCleanupsInEvaluationContext(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9678 | } |
Argyrios Kyrtzidis | 49b96d1 | 2010-08-13 18:42:17 +0000 | [diff] [blame] | 9679 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 9680 | return dcl; |
Fariborz Jahanian | 60fbca0 | 2007-11-10 16:31:34 +0000 | [diff] [blame] | 9681 | } |
9682 | |||||
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 9683 | |
9684 | /// When we finish delayed parsing of an attribute, we must attach it to the | ||||
9685 | /// relevant Decl. | ||||
9686 | void Sema::ActOnFinishDelayedAttribute(Scope *S, Decl *D, | ||||
9687 | ParsedAttributes &Attrs) { | ||||
DeLesley Hutchins | 7b9ff0c | 2012-01-20 22:37:06 +0000 | [diff] [blame] | 9688 | // Always attach attributes to the underlying decl. |
9689 | if (TemplateDecl *TD = dyn_cast<TemplateDecl>(D)) | ||||
9690 | D = TD->getTemplatedDecl(); | ||||
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 9691 | ProcessDeclAttributeList(S, D, Attrs.getList()); |
9692 | |||||
9693 | if (CXXMethodDecl *Method = dyn_cast_or_null<CXXMethodDecl>(D)) | ||||
9694 | if (Method->isStatic()) | ||||
9695 | checkThisInStaticMemberFunctionAttributes(Method); | ||||
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 9696 | } |
9697 | |||||
9698 | |||||
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 9699 | /// ImplicitlyDefineFunction - An undeclared identifier was used in a function |
9700 | /// 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] | 9701 | NamedDecl *Sema::ImplicitlyDefineFunction(SourceLocation Loc, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 9702 | IdentifierInfo &II, Scope *S) { |
Douglas Gregor | 6393519 | 2009-03-02 00:19:53 +0000 | [diff] [blame] | 9703 | // Before we produce a declaration for an implicitly defined |
9704 | // function, see whether there was a locally-scoped declaration of | ||||
9705 | // this name as a function or variable. If so, use that | ||||
9706 | // (non-visible) declaration, and complain about it. | ||||
Richard Smith | 662f41b | 2013-06-18 20:15:12 +0000 | [diff] [blame] | 9707 | if (NamedDecl *ExternCPrev = findLocallyScopedExternCDecl(&II)) { |
9708 | Diag(Loc, diag::warn_use_out_of_scope_declaration) << ExternCPrev; | ||||
9709 | Diag(ExternCPrev->getLocation(), diag::note_previous_declaration); | ||||
9710 | return ExternCPrev; | ||||
Douglas Gregor | 6393519 | 2009-03-02 00:19:53 +0000 | [diff] [blame] | 9711 | } |
9712 | |||||
Chris Lattner | 37d1084 | 2008-05-05 21:18:06 +0000 | [diff] [blame] | 9713 | // Extension in C99. Legal in C90, but warn about it. |
Hans Wennborg | e3ca33a | 2011-12-08 15:56:07 +0000 | [diff] [blame] | 9714 | unsigned diag_id; |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 9715 | if (II.getName().startswith("__builtin_")) |
Abramo Bagnara | 753a200 | 2012-01-09 10:05:48 +0000 | [diff] [blame] | 9716 | diag_id = diag::warn_builtin_unknown; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9717 | else if (getLangOpts().C99) |
Hans Wennborg | e3ca33a | 2011-12-08 15:56:07 +0000 | [diff] [blame] | 9718 | diag_id = diag::ext_implicit_function_decl; |
Chris Lattner | 37d1084 | 2008-05-05 21:18:06 +0000 | [diff] [blame] | 9719 | else |
Hans Wennborg | e3ca33a | 2011-12-08 15:56:07 +0000 | [diff] [blame] | 9720 | diag_id = diag::warn_implicit_function_decl; |
9721 | Diag(Loc, diag_id) << &II; | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9722 | |
Hans Wennborg | e3ca33a | 2011-12-08 15:56:07 +0000 | [diff] [blame] | 9723 | // Because typo correction is expensive, only do it if the implicit |
9724 | // function declaration is going to be treated as an error. | ||||
9725 | if (Diags.getDiagnosticLevel(diag_id, Loc) >= DiagnosticsEngine::Error) { | ||||
9726 | TypoCorrection Corrected; | ||||
Kaelyn Uhrain | 43e875d | 2012-01-18 21:41:41 +0000 | [diff] [blame] | 9727 | DeclFilterCCC<FunctionDecl> Validator; |
Hans Wennborg | e3ca33a | 2011-12-08 15:56:07 +0000 | [diff] [blame] | 9728 | if (S && (Corrected = CorrectTypo(DeclarationNameInfo(&II, Loc), |
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 9729 | LookupOrdinaryName, S, 0, Validator))) |
9730 | diagnoseTypo(Corrected, PDiag(diag::note_function_suggestion), | ||||
9731 | /*ErrorRecovery*/false); | ||||
Hans Wennborg | 122de3e | 2011-12-06 09:46:12 +0000 | [diff] [blame] | 9732 | } |
9733 | |||||
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 9734 | // Set a Declarator for the implicit definition: int foo(); |
9735 | const char *Dummy; | ||||
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 9736 | AttributeFactory attrFactory; |
9737 | DeclSpec DS(attrFactory); | ||||
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 9738 | unsigned DiagID; |
9739 | bool Error = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, Dummy, DiagID); | ||||
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 9740 | (void)Error; // Silence warning. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 9741 | assert(!Error && "Error setting up implicit decl!"); |
Abramo Bagnara | 59c0a81 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 9742 | SourceLocation NoLoc; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 9743 | Declarator D(DS, Declarator::BlockContext); |
Abramo Bagnara | 59c0a81 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 9744 | D.AddTypeInfo(DeclaratorChunk::getFunction(/*HasProto=*/false, |
9745 | /*IsAmbiguous=*/false, | ||||
9746 | /*RParenLoc=*/NoLoc, | ||||
9747 | /*ArgInfo=*/0, | ||||
9748 | /*NumArgs=*/0, | ||||
9749 | /*EllipsisLoc=*/NoLoc, | ||||
9750 | /*RParenLoc=*/NoLoc, | ||||
9751 | /*TypeQuals=*/0, | ||||
9752 | /*RefQualifierIsLvalueRef=*/true, | ||||
9753 | /*RefQualifierLoc=*/NoLoc, | ||||
9754 | /*ConstQualifierLoc=*/NoLoc, | ||||
9755 | /*VolatileQualifierLoc=*/NoLoc, | ||||
9756 | /*MutableLoc=*/NoLoc, | ||||
9757 | EST_None, | ||||
9758 | /*ESpecLoc=*/NoLoc, | ||||
9759 | /*Exceptions=*/0, | ||||
9760 | /*ExceptionRanges=*/0, | ||||
9761 | /*NumExceptions=*/0, | ||||
9762 | /*NoexceptExpr=*/0, | ||||
9763 | Loc, Loc, D), | ||||
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 9764 | DS.getAttributes(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 9765 | SourceLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 9766 | D.SetIdentifier(&II, Loc); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 9767 | |
Argyrios Kyrtzidis | 93213bb | 2008-05-01 21:04:16 +0000 | [diff] [blame] | 9768 | // Insert this function into translation-unit scope. |
9769 | |||||
9770 | DeclContext *PrevDC = CurContext; | ||||
9771 | CurContext = Context.getTranslationUnitDecl(); | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9772 | |
Jordan Rose | 41f3f3a | 2013-03-05 01:27:54 +0000 | [diff] [blame] | 9773 | FunctionDecl *FD = cast<FunctionDecl>(ActOnDeclarator(TUScope, D)); |
Steve Naroff | e2ef815 | 2008-04-04 14:32:09 +0000 | [diff] [blame] | 9774 | FD->setImplicit(); |
Argyrios Kyrtzidis | 93213bb | 2008-05-01 21:04:16 +0000 | [diff] [blame] | 9775 | |
9776 | CurContext = PrevDC; | ||||
9777 | |||||
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 9778 | AddKnownFunctionAttributes(FD); |
9779 | |||||
Steve Naroff | e2ef815 | 2008-04-04 14:32:09 +0000 | [diff] [blame] | 9780 | return FD; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 9781 | } |
9782 | |||||
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 9783 | /// \brief Adds any function attributes that we know a priori based on |
9784 | /// the declaration of this function. | ||||
9785 | /// | ||||
9786 | /// These attributes can apply both to implicitly-declared builtins | ||||
9787 | /// (like __builtin___printf_chk) or to library-declared functions | ||||
9788 | /// like NSLog or printf. | ||||
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 9789 | /// |
9790 | /// We need to check for duplicate attributes both here and where user-written | ||||
9791 | /// attributes are applied to declarations. | ||||
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 9792 | void Sema::AddKnownFunctionAttributes(FunctionDecl *FD) { |
9793 | if (FD->isInvalidDecl()) | ||||
9794 | return; | ||||
9795 | |||||
9796 | // If this is a built-in function, map its builtin attributes to | ||||
9797 | // actual attributes. | ||||
Douglas Gregor | 7814e6d | 2009-09-12 00:22:50 +0000 | [diff] [blame] | 9798 | if (unsigned BuiltinID = FD->getBuiltinID()) { |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 9799 | // Handle printf-formatting attributes. |
9800 | unsigned FormatIdx; | ||||
9801 | bool HasVAListArg; | ||||
9802 | if (Context.BuiltinInfo.isPrintfLike(BuiltinID, FormatIdx, HasVAListArg)) { | ||||
Jean-Daniel Dupas | 1acbe5e | 2012-01-24 22:32:46 +0000 | [diff] [blame] | 9803 | if (!FD->getAttr<FormatAttr>()) { |
9804 | const char *fmt = "printf"; | ||||
9805 | unsigned int NumParams = FD->getNumParams(); | ||||
9806 | if (FormatIdx < NumParams && // NumParams may be 0 (e.g. vfprintf) | ||||
9807 | FD->getParamDecl(FormatIdx)->getType()->isObjCObjectPointerType()) | ||||
9808 | fmt = "NSString"; | ||||
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 9809 | FD->addAttr(::new (Context) FormatAttr(FD->getLocation(), Context, |
Jean-Daniel Dupas | 1acbe5e | 2012-01-24 22:32:46 +0000 | [diff] [blame] | 9810 | fmt, FormatIdx+1, |
Ted Kremenek | 3d2c43e | 2010-02-11 05:28:37 +0000 | [diff] [blame] | 9811 | HasVAListArg ? 0 : FormatIdx+2)); |
Jean-Daniel Dupas | 1acbe5e | 2012-01-24 22:32:46 +0000 | [diff] [blame] | 9812 | } |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 9813 | } |
Ted Kremenek | bee05c1 | 2010-07-16 02:11:15 +0000 | [diff] [blame] | 9814 | if (Context.BuiltinInfo.isScanfLike(BuiltinID, FormatIdx, |
9815 | HasVAListArg)) { | ||||
9816 | if (!FD->getAttr<FormatAttr>()) | ||||
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 9817 | FD->addAttr(::new (Context) FormatAttr(FD->getLocation(), Context, |
9818 | "scanf", FormatIdx+1, | ||||
Ted Kremenek | bee05c1 | 2010-07-16 02:11:15 +0000 | [diff] [blame] | 9819 | HasVAListArg ? 0 : FormatIdx+2)); |
9820 | } | ||||
Daniel Dunbar | ef2abfe | 2009-02-16 22:43:43 +0000 | [diff] [blame] | 9821 | |
9822 | // Mark const if we don't care about errno and that is the only | ||||
9823 | // thing preventing the function from being const. This allows | ||||
9824 | // IRgen to use LLVM intrinsics for such functions. | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9825 | if (!getLangOpts().MathErrno && |
Daniel Dunbar | ef2abfe | 2009-02-16 22:43:43 +0000 | [diff] [blame] | 9826 | Context.BuiltinInfo.isConstWithoutErrno(BuiltinID)) { |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 9827 | if (!FD->getAttr<ConstAttr>()) |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 9828 | FD->addAttr(::new (Context) ConstAttr(FD->getLocation(), Context)); |
Daniel Dunbar | ef2abfe | 2009-02-16 22:43:43 +0000 | [diff] [blame] | 9829 | } |
Mike Stump | 0feecbb | 2009-07-27 19:14:18 +0000 | [diff] [blame] | 9830 | |
Rafael Espindola | 6700415 | 2011-10-12 19:51:18 +0000 | [diff] [blame] | 9831 | if (Context.BuiltinInfo.isReturnsTwice(BuiltinID) && |
9832 | !FD->getAttr<ReturnsTwiceAttr>()) | ||||
9833 | FD->addAttr(::new (Context) ReturnsTwiceAttr(FD->getLocation(), Context)); | ||||
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 9834 | if (Context.BuiltinInfo.isNoThrow(BuiltinID) && !FD->getAttr<NoThrowAttr>()) |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 9835 | FD->addAttr(::new (Context) NoThrowAttr(FD->getLocation(), Context)); |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 9836 | if (Context.BuiltinInfo.isConst(BuiltinID) && !FD->getAttr<ConstAttr>()) |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 9837 | FD->addAttr(::new (Context) ConstAttr(FD->getLocation(), Context)); |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 9838 | } |
9839 | |||||
9840 | IdentifierInfo *Name = FD->getIdentifier(); | ||||
9841 | if (!Name) | ||||
9842 | return; | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9843 | if ((!getLangOpts().CPlusPlus && |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 9844 | FD->getDeclContext()->isTranslationUnit()) || |
9845 | (isa<LinkageSpecDecl>(FD->getDeclContext()) && | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9846 | cast<LinkageSpecDecl>(FD->getDeclContext())->getLanguage() == |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 9847 | LinkageSpecDecl::lang_c)) { |
9848 | // Okay: this could be a libc/libm/Objective-C function we know | ||||
9849 | // about. | ||||
9850 | } else | ||||
9851 | return; | ||||
9852 | |||||
Jean-Daniel Dupas | 1acbe5e | 2012-01-24 22:32:46 +0000 | [diff] [blame] | 9853 | if (Name->isStr("asprintf") || Name->isStr("vasprintf")) { |
Mike Stump | 523a8fd | 2009-07-28 00:07:08 +0000 | [diff] [blame] | 9854 | // FIXME: asprintf and vasprintf aren't C99 functions. Should they be |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9855 | // target-specific builtins, perhaps? |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 9856 | if (!FD->getAttr<FormatAttr>()) |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 9857 | FD->addAttr(::new (Context) FormatAttr(FD->getLocation(), Context, |
9858 | "printf", 2, | ||||
Eli Friedman | d7dad72 | 2009-06-10 04:01:38 +0000 | [diff] [blame] | 9859 | Name->isStr("vasprintf") ? 0 : 3)); |
Mike Stump | 782fa30 | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 9860 | } |
Jordan Rose | 8a64f88 | 2012-08-08 21:17:31 +0000 | [diff] [blame] | 9861 | |
9862 | if (Name->isStr("__CFStringMakeConstantString")) { | ||||
9863 | // We already have a __builtin___CFStringMakeConstantString, | ||||
9864 | // but builds that use -fno-constant-cfstrings don't go through that. | ||||
9865 | if (!FD->getAttr<FormatArgAttr>()) | ||||
9866 | FD->addAttr(::new (Context) FormatArgAttr(FD->getLocation(), Context, 1)); | ||||
9867 | } | ||||
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 9868 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 9869 | |
John McCall | ba6a9bd | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 9870 | TypedefDecl *Sema::ParseTypedefDecl(Scope *S, Declarator &D, QualType T, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 9871 | TypeSourceInfo *TInfo) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 9872 | assert(D.getIdentifier() && "Wrong callback for declspec without declarator"); |
Steve Naroff | 5912a35 | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 9873 | assert(!T.isNull() && "GetTypeForDeclarator() returned null type"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9874 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 9875 | if (!TInfo) { |
John McCall | ba6a9bd | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 9876 | assert(D.isInvalidType() && "no declarator info for valid type"); |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 9877 | TInfo = Context.getTrivialTypeSourceInfo(T); |
John McCall | ba6a9bd | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 9878 | } |
9879 | |||||
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 9880 | // Scope manipulation handled by caller. |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 9881 | TypedefDecl *NewTD = TypedefDecl::Create(Context, CurContext, |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 9882 | D.getLocStart(), |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 9883 | D.getIdentifierLoc(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9884 | D.getIdentifier(), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 9885 | TInfo); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9886 | |
John McCall | cde5a40 | 2011-02-01 08:20:08 +0000 | [diff] [blame] | 9887 | // Bail out immediately if we have an invalid declaration. |
9888 | if (D.isInvalidType()) { | ||||
9889 | NewTD->setInvalidDecl(); | ||||
9890 | return NewTD; | ||||
Anders Carlsson | 4843e58 | 2009-03-10 17:07:44 +0000 | [diff] [blame] | 9891 | } |
9892 | |||||
Douglas Gregor | e389585 | 2011-09-12 18:37:38 +0000 | [diff] [blame] | 9893 | if (D.getDeclSpec().isModulePrivateSpecified()) { |
9894 | if (CurContext->isFunctionOrMethod()) | ||||
9895 | Diag(NewTD->getLocation(), diag::err_module_private_local) | ||||
9896 | << 2 << NewTD->getDeclName() | ||||
9897 | << SourceRange(D.getDeclSpec().getModulePrivateSpecLoc()) | ||||
9898 | << FixItHint::CreateRemoval(D.getDeclSpec().getModulePrivateSpecLoc()); | ||||
9899 | else | ||||
9900 | NewTD->setModulePrivate(); | ||||
9901 | } | ||||
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 9902 | |
John McCall | cde5a40 | 2011-02-01 08:20:08 +0000 | [diff] [blame] | 9903 | // C++ [dcl.typedef]p8: |
9904 | // If the typedef declaration defines an unnamed class (or | ||||
9905 | // enum), the first typedef-name declared by the declaration | ||||
9906 | // to be that class type (or enum type) is used to denote the | ||||
9907 | // class type (or enum type) for linkage purposes only. | ||||
9908 | // We need to check whether the type was declared in the declaration. | ||||
9909 | switch (D.getDeclSpec().getTypeSpecType()) { | ||||
9910 | case TST_enum: | ||||
9911 | case TST_struct: | ||||
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 9912 | case TST_interface: |
John McCall | cde5a40 | 2011-02-01 08:20:08 +0000 | [diff] [blame] | 9913 | case TST_union: |
9914 | case TST_class: { | ||||
9915 | TagDecl *tagFromDeclSpec = cast<TagDecl>(D.getDeclSpec().getRepAsDecl()); | ||||
9916 | |||||
9917 | // Do nothing if the tag is not anonymous or already has an | ||||
9918 | // associated typedef (from an earlier typedef in this decl group). | ||||
9919 | if (tagFromDeclSpec->getIdentifier()) break; | ||||
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 9920 | if (tagFromDeclSpec->getTypedefNameForAnonDecl()) break; |
John McCall | cde5a40 | 2011-02-01 08:20:08 +0000 | [diff] [blame] | 9921 | |
9922 | // A well-formed anonymous tag must always be a TUK_Definition. | ||||
9923 | assert(tagFromDeclSpec->isThisDeclarationADefinition()); | ||||
9924 | |||||
9925 | // The type must match the tag exactly; no qualifiers allowed. | ||||
9926 | if (!Context.hasSameType(T, Context.getTagDeclType(tagFromDeclSpec))) | ||||
9927 | break; | ||||
9928 | |||||
9929 | // Otherwise, set this is the anon-decl typedef for the tag. | ||||
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 9930 | tagFromDeclSpec->setTypedefNameForAnonDecl(NewTD); |
John McCall | cde5a40 | 2011-02-01 08:20:08 +0000 | [diff] [blame] | 9931 | break; |
9932 | } | ||||
9933 | |||||
9934 | default: | ||||
9935 | break; | ||||
9936 | } | ||||
9937 | |||||
Steve Naroff | 5912a35 | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 9938 | return NewTD; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 9939 | } |
9940 | |||||
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 9941 | |
Richard Smith | f1c66b4 | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 9942 | /// \brief Check that this is a valid underlying type for an enum declaration. |
9943 | bool Sema::CheckEnumUnderlyingType(TypeSourceInfo *TI) { | ||||
9944 | SourceLocation UnderlyingLoc = TI->getTypeLoc().getBeginLoc(); | ||||
9945 | QualType T = TI->getType(); | ||||
9946 | |||||
Eli Friedman | 2fcff83 | 2012-12-18 02:37:32 +0000 | [diff] [blame] | 9947 | if (T->isDependentType()) |
Richard Smith | f1c66b4 | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 9948 | return false; |
9949 | |||||
Eli Friedman | 2fcff83 | 2012-12-18 02:37:32 +0000 | [diff] [blame] | 9950 | if (const BuiltinType *BT = T->getAs<BuiltinType>()) |
9951 | if (BT->isInteger()) | ||||
9952 | return false; | ||||
9953 | |||||
Richard Smith | f1c66b4 | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 9954 | Diag(UnderlyingLoc, diag::err_enum_invalid_underlying) << T; |
9955 | return true; | ||||
9956 | } | ||||
9957 | |||||
9958 | /// Check whether this is a valid redeclaration of a previous enumeration. | ||||
9959 | /// \return true if the redeclaration was invalid. | ||||
9960 | bool Sema::CheckEnumRedeclaration(SourceLocation EnumLoc, bool IsScoped, | ||||
9961 | QualType EnumUnderlyingTy, | ||||
9962 | const EnumDecl *Prev) { | ||||
9963 | bool IsFixed = !EnumUnderlyingTy.isNull(); | ||||
9964 | |||||
9965 | if (IsScoped != Prev->isScoped()) { | ||||
9966 | Diag(EnumLoc, diag::err_enum_redeclare_scoped_mismatch) | ||||
9967 | << Prev->isScoped(); | ||||
9968 | Diag(Prev->getLocation(), diag::note_previous_use); | ||||
9969 | return true; | ||||
9970 | } | ||||
9971 | |||||
9972 | if (IsFixed && Prev->isFixed()) { | ||||
Richard Smith | 4ca93d9 | 2012-03-26 04:08:46 +0000 | [diff] [blame] | 9973 | if (!EnumUnderlyingTy->isDependentType() && |
9974 | !Prev->getIntegerType()->isDependentType() && | ||||
9975 | !Context.hasSameUnqualifiedType(EnumUnderlyingTy, | ||||
Richard Smith | f1c66b4 | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 9976 | Prev->getIntegerType())) { |
9977 | Diag(EnumLoc, diag::err_enum_redeclare_type_mismatch) | ||||
9978 | << EnumUnderlyingTy << Prev->getIntegerType(); | ||||
9979 | Diag(Prev->getLocation(), diag::note_previous_use); | ||||
9980 | return true; | ||||
9981 | } | ||||
9982 | } else if (IsFixed != Prev->isFixed()) { | ||||
9983 | Diag(EnumLoc, diag::err_enum_redeclare_fixed_mismatch) | ||||
9984 | << Prev->isFixed(); | ||||
9985 | Diag(Prev->getLocation(), diag::note_previous_use); | ||||
9986 | return true; | ||||
9987 | } | ||||
9988 | |||||
9989 | return false; | ||||
9990 | } | ||||
9991 | |||||
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 9992 | /// \brief Get diagnostic %select index for tag kind for |
9993 | /// redeclaration diagnostic message. | ||||
9994 | /// WARNING: Indexes apply to particular diagnostics only! | ||||
9995 | /// | ||||
9996 | /// \returns diagnostic %select index. | ||||
Joao Matos | f143ae9 | 2012-09-01 00:13:24 +0000 | [diff] [blame] | 9997 | static unsigned getRedeclDiagFromTagKind(TagTypeKind Tag) { |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 9998 | switch (Tag) { |
Joao Matos | f143ae9 | 2012-09-01 00:13:24 +0000 | [diff] [blame] | 9999 | case TTK_Struct: return 0; |
10000 | case TTK_Interface: return 1; | ||||
10001 | case TTK_Class: return 2; | ||||
10002 | default: llvm_unreachable("Invalid tag kind for redecl diagnostic!"); | ||||
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 10003 | } |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 10004 | } |
10005 | |||||
10006 | /// \brief Determine if tag kind is a class-key compatible with | ||||
10007 | /// class for redeclaration (class, struct, or __interface). | ||||
10008 | /// | ||||
Sylvestre Ledru | f3477c1 | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 10009 | /// \returns true iff the tag kind is compatible. |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 10010 | static bool isClassCompatTagKind(TagTypeKind Tag) |
10011 | { | ||||
10012 | return Tag == TTK_Struct || Tag == TTK_Class || Tag == TTK_Interface; | ||||
10013 | } | ||||
10014 | |||||
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 10015 | /// \brief Determine whether a tag with a given kind is acceptable |
10016 | /// as a redeclaration of the given tag declaration. | ||||
10017 | /// | ||||
10018 | /// \returns true if the new tag kind is acceptable, false otherwise. | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10019 | bool Sema::isAcceptableTagRedeclaration(const TagDecl *Previous, |
Richard Trieu | bbf34c0 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 10020 | TagTypeKind NewTag, bool isDefinition, |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 10021 | SourceLocation NewTagLoc, |
10022 | const IdentifierInfo &Name) { | ||||
10023 | // C++ [dcl.type.elab]p3: | ||||
10024 | // The class-key or enum keyword present in the | ||||
10025 | // elaborated-type-specifier shall agree in kind with the | ||||
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 10026 | // declaration to which the name in the elaborated-type-specifier |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 10027 | // refers. This rule also applies to the form of |
10028 | // elaborated-type-specifier that declares a class-name or | ||||
10029 | // friend class since it can be construed as referring to the | ||||
10030 | // definition of the class. Thus, in any | ||||
10031 | // elaborated-type-specifier, the enum keyword shall be used to | ||||
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 10032 | // refer to an enumeration (7.2), the union class-key shall be |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 10033 | // used to refer to a union (clause 9), and either the class or |
10034 | // struct class-key shall be used to refer to a class (clause 9) | ||||
10035 | // declared using the class or struct class-key. | ||||
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 10036 | TagTypeKind OldTag = Previous->getTagKind(); |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 10037 | if (!isDefinition || !isClassCompatTagKind(NewTag)) |
Richard Trieu | bbf34c0 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 10038 | if (OldTag == NewTag) |
10039 | return true; | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10040 | |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 10041 | if (isClassCompatTagKind(OldTag) && isClassCompatTagKind(NewTag)) { |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 10042 | // Warn about the struct/class tag mismatch. |
10043 | bool isTemplate = false; | ||||
10044 | if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Previous)) | ||||
10045 | isTemplate = Record->getDescribedClassTemplate(); | ||||
10046 | |||||
Richard Trieu | bbf34c0 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 10047 | if (!ActiveTemplateInstantiations.empty()) { |
10048 | // In a template instantiation, do not offer fix-its for tag mismatches | ||||
10049 | // since they usually mess up the template instead of fixing the problem. | ||||
10050 | Diag(NewTagLoc, diag::warn_struct_class_tag_mismatch) | ||||
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 10051 | << getRedeclDiagFromTagKind(NewTag) << isTemplate << &Name |
10052 | << getRedeclDiagFromTagKind(OldTag); | ||||
Richard Trieu | bbf34c0 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 10053 | return true; |
10054 | } | ||||
10055 | |||||
10056 | if (isDefinition) { | ||||
10057 | // On definitions, check previous tags and issue a fix-it for each | ||||
10058 | // one that doesn't match the current tag. | ||||
10059 | if (Previous->getDefinition()) { | ||||
10060 | // Don't suggest fix-its for redefinitions. | ||||
10061 | return true; | ||||
10062 | } | ||||
10063 | |||||
10064 | bool previousMismatch = false; | ||||
10065 | for (TagDecl::redecl_iterator I(Previous->redecls_begin()), | ||||
10066 | E(Previous->redecls_end()); I != E; ++I) { | ||||
10067 | if (I->getTagKind() != NewTag) { | ||||
10068 | if (!previousMismatch) { | ||||
10069 | previousMismatch = true; | ||||
10070 | Diag(NewTagLoc, diag::warn_struct_class_previous_tag_mismatch) | ||||
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 10071 | << getRedeclDiagFromTagKind(NewTag) << isTemplate << &Name |
10072 | << getRedeclDiagFromTagKind(I->getTagKind()); | ||||
Richard Trieu | bbf34c0 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 10073 | } |
10074 | Diag(I->getInnerLocStart(), diag::note_struct_class_suggestion) | ||||
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 10075 | << getRedeclDiagFromTagKind(NewTag) |
Richard Trieu | bbf34c0 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 10076 | << FixItHint::CreateReplacement(I->getInnerLocStart(), |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 10077 | TypeWithKeyword::getTagTypeKindName(NewTag)); |
Richard Trieu | bbf34c0 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 10078 | } |
10079 | } | ||||
10080 | return true; | ||||
10081 | } | ||||
10082 | |||||
10083 | // Check for a previous definition. If current tag and definition | ||||
10084 | // are same type, do nothing. If no definition, but disagree with | ||||
10085 | // with previous tag type, give a warning, but no fix-it. | ||||
10086 | const TagDecl *Redecl = Previous->getDefinition() ? | ||||
10087 | Previous->getDefinition() : Previous; | ||||
10088 | if (Redecl->getTagKind() == NewTag) { | ||||
10089 | return true; | ||||
10090 | } | ||||
10091 | |||||
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 10092 | Diag(NewTagLoc, diag::warn_struct_class_tag_mismatch) |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 10093 | << getRedeclDiagFromTagKind(NewTag) << isTemplate << &Name |
10094 | << getRedeclDiagFromTagKind(OldTag); | ||||
Richard Trieu | bbf34c0 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 10095 | Diag(Redecl->getLocation(), diag::note_previous_use); |
10096 | |||||
10097 | // If there is a previous defintion, suggest a fix-it. | ||||
10098 | if (Previous->getDefinition()) { | ||||
10099 | Diag(NewTagLoc, diag::note_struct_class_suggestion) | ||||
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 10100 | << getRedeclDiagFromTagKind(Redecl->getTagKind()) |
Richard Trieu | bbf34c0 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 10101 | << FixItHint::CreateReplacement(SourceRange(NewTagLoc), |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 10102 | TypeWithKeyword::getTagTypeKindName(Redecl->getTagKind())); |
Richard Trieu | bbf34c0 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 10103 | } |
10104 | |||||
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 10105 | return true; |
10106 | } | ||||
10107 | return false; | ||||
10108 | } | ||||
10109 | |||||
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 10110 | /// 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] | 10111 | /// 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] | 10112 | /// 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] | 10113 | /// reference/declaration/definition of a tag. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10114 | Decl *Sema::ActOnTag(Scope *S, unsigned TagSpec, TagUseKind TUK, |
Douglas Gregor | 069ea64 | 2010-09-16 23:58:57 +0000 | [diff] [blame] | 10115 | SourceLocation KWLoc, CXXScopeSpec &SS, |
10116 | IdentifierInfo *Name, SourceLocation NameLoc, | ||||
10117 | AttributeList *Attr, AccessSpecifier AS, | ||||
Douglas Gregor | e761230 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 10118 | SourceLocation ModulePrivateLoc, |
Douglas Gregor | 069ea64 | 2010-09-16 23:58:57 +0000 | [diff] [blame] | 10119 | MultiTemplateParamsArg TemplateParameterLists, |
Abramo Bagnara | a88cefd | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 10120 | bool &OwnedDecl, bool &IsDependent, |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 10121 | SourceLocation ScopedEnumKWLoc, |
10122 | bool ScopedEnumUsesClassTag, | ||||
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 10123 | TypeResult UnderlyingType) { |
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 10124 | // If this is not a definition, it must have a name. |
Douglas Gregor | 6960587 | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 10125 | IdentifierInfo *OrigName = Name; |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 10126 | assert((Name != 0 || TUK == TUK_Definition) && |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 10127 | "Nameless record must be a definition!"); |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 10128 | assert(TemplateParameterLists.size() == 0 || TUK != TUK_Reference); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 10129 | |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 10130 | OwnedDecl = false; |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 10131 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 10132 | bool ScopedEnum = ScopedEnumKWLoc.isValid(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10133 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 10134 | // FIXME: Check explicit specializations more carefully. |
10135 | bool isExplicitSpecialization = false; | ||||
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 10136 | bool Invalid = false; |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 10137 | |
10138 | // We only need to do this matching if we have template parameters | ||||
10139 | // or a scope specifier, which also conveniently avoids this work | ||||
10140 | // for non-C++ cases. | ||||
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 10141 | if (TemplateParameterLists.size() > 0 || |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 10142 | (SS.isNotEmpty() && TUK != TUK_Reference)) { |
Robert Wilhelm | 1169e2f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 10143 | if (TemplateParameterList *TemplateParams = |
10144 | MatchTemplateParametersToScopeSpecifier( | ||||
10145 | KWLoc, NameLoc, SS, TemplateParameterLists, TUK == TUK_Friend, | ||||
10146 | isExplicitSpecialization, Invalid)) { | ||||
Richard Smith | 725fe0e | 2013-04-01 21:43:41 +0000 | [diff] [blame] | 10147 | if (Kind == TTK_Enum) { |
10148 | Diag(KWLoc, diag::err_enum_template); | ||||
10149 | return 0; | ||||
10150 | } | ||||
10151 | |||||
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 10152 | if (TemplateParams->size() > 0) { |
Douglas Gregor | 7cdbc58 | 2009-07-22 23:48:44 +0000 | [diff] [blame] | 10153 | // This is a declaration or definition of a class template (which may |
10154 | // be a member of another template). | ||||
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 10155 | |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 10156 | if (Invalid) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10157 | return 0; |
Abramo Bagnara | c57c17d | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 10158 | |
Douglas Gregor | 7cdbc58 | 2009-07-22 23:48:44 +0000 | [diff] [blame] | 10159 | OwnedDecl = false; |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 10160 | DeclResult Result = CheckClassTemplate(S, TagSpec, TUK, KWLoc, |
Douglas Gregor | 7cdbc58 | 2009-07-22 23:48:44 +0000 | [diff] [blame] | 10161 | SS, Name, NameLoc, Attr, |
Abramo Bagnara | c57c17d | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 10162 | TemplateParams, AS, |
Douglas Gregor | e761230 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 10163 | ModulePrivateLoc, |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 10164 | TemplateParameterLists.size()-1, |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 10165 | TemplateParameterLists.data()); |
Douglas Gregor | 7cdbc58 | 2009-07-22 23:48:44 +0000 | [diff] [blame] | 10166 | return Result.get(); |
10167 | } else { | ||||
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 10168 | // The "template<>" header is extraneous. |
10169 | Diag(TemplateParams->getTemplateLoc(), diag::err_template_tag_noparams) | ||||
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 10170 | << TypeWithKeyword::getTagTypeKindName(Kind) << Name; |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 10171 | isExplicitSpecialization = true; |
Douglas Gregor | 7cdbc58 | 2009-07-22 23:48:44 +0000 | [diff] [blame] | 10172 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10173 | } |
10174 | } | ||||
10175 | |||||
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 10176 | // Figure out the underlying type if this a enum declaration. We need to do |
10177 | // this early, because it's needed to detect if this is an incompatible | ||||
10178 | // redeclaration. | ||||
10179 | llvm::PointerUnion<const Type*, TypeSourceInfo*> EnumUnderlying; | ||||
10180 | |||||
10181 | if (Kind == TTK_Enum) { | ||||
10182 | if (UnderlyingType.isInvalid() || (!UnderlyingType.get() && ScopedEnum)) | ||||
10183 | // No underlying type explicitly specified, or we failed to parse the | ||||
10184 | // type, default to int. | ||||
10185 | EnumUnderlying = Context.IntTy.getTypePtr(); | ||||
10186 | else if (UnderlyingType.get()) { | ||||
10187 | // C++0x 7.2p2: The type-specifier-seq of an enum-base shall name an | ||||
10188 | // integral type; any cv-qualification is ignored. | ||||
10189 | TypeSourceInfo *TI = 0; | ||||
Richard Smith | 878416d | 2012-03-15 00:22:18 +0000 | [diff] [blame] | 10190 | GetTypeFromParser(UnderlyingType.get(), &TI); |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 10191 | EnumUnderlying = TI; |
10192 | |||||
Richard Smith | f1c66b4 | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 10193 | if (CheckEnumUnderlyingType(TI)) |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 10194 | // Recover by falling back to int. |
10195 | EnumUnderlying = Context.IntTy.getTypePtr(); | ||||
Douglas Gregor | 0c9e479 | 2010-12-16 00:24:44 +0000 | [diff] [blame] | 10196 | |
Richard Smith | f1c66b4 | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 10197 | if (DiagnoseUnexpandedParameterPack(TI->getTypeLoc().getBeginLoc(), TI, |
Douglas Gregor | 0c9e479 | 2010-12-16 00:24:44 +0000 | [diff] [blame] | 10198 | UPPC_FixedUnderlyingType)) |
10199 | EnumUnderlying = Context.IntTy.getTypePtr(); | ||||
10200 | |||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10201 | } else if (getLangOpts().MicrosoftMode) |
Francois Pichet | 842e7a2 | 2010-10-18 15:01:13 +0000 | [diff] [blame] | 10202 | // Microsoft enums are always of int type. |
10203 | EnumUnderlying = Context.IntTy.getTypePtr(); | ||||
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 10204 | } |
10205 | |||||
Douglas Gregor | 4920f1f | 2009-01-12 22:49:06 +0000 | [diff] [blame] | 10206 | DeclContext *SearchDC = CurContext; |
Argyrios Kyrtzidis | 0f84a23 | 2008-11-09 22:53:32 +0000 | [diff] [blame] | 10207 | DeclContext *DC = CurContext; |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 10208 | bool isStdBadAlloc = false; |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 10209 | |
Chandler Carruth | 7bf3600 | 2010-03-01 21:17:36 +0000 | [diff] [blame] | 10210 | RedeclarationKind Redecl = ForRedeclaration; |
10211 | if (TUK == TUK_Friend || TUK == TUK_Reference) | ||||
10212 | Redecl = NotForRedeclaration; | ||||
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 10213 | |
10214 | LookupResult Previous(*this, Name, NameLoc, LookupTagName, Redecl); | ||||
Douglas Gregor | d943352 | 2013-06-27 20:42:30 +0000 | [diff] [blame] | 10215 | bool FriendSawTagOutsideEnclosingNamespace = false; |
Argyrios Kyrtzidis | 630c81b | 2008-11-09 22:09:58 +0000 | [diff] [blame] | 10216 | if (Name && SS.isNotEmpty()) { |
10217 | // We have a nested-name tag ('struct foo::bar'). | ||||
10218 | |||||
10219 | // Check for invalid 'foo::'. | ||||
Argyrios Kyrtzidis | 0f84a23 | 2008-11-09 22:53:32 +0000 | [diff] [blame] | 10220 | if (SS.isInvalid()) { |
Argyrios Kyrtzidis | 630c81b | 2008-11-09 22:09:58 +0000 | [diff] [blame] | 10221 | Name = 0; |
10222 | goto CreateNewDecl; | ||||
10223 | } | ||||
10224 | |||||
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 10225 | // If this is a friend or a reference to a class in a dependent |
10226 | // context, don't try to make a decl for it. | ||||
10227 | if (TUK == TUK_Friend || TUK == TUK_Reference) { | ||||
10228 | DC = computeDeclContext(SS, false); | ||||
10229 | if (!DC) { | ||||
10230 | IsDependent = true; | ||||
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10231 | return 0; |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 10232 | } |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 10233 | } else { |
10234 | DC = computeDeclContext(SS, true); | ||||
10235 | if (!DC) { | ||||
10236 | Diag(SS.getRange().getBegin(), diag::err_dependent_nested_name_spec) | ||||
10237 | << SS.getRange(); | ||||
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10238 | return 0; |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 10239 | } |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 10240 | } |
10241 | |||||
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 10242 | if (RequireCompleteDeclContext(SS, DC)) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10243 | return 0; |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 10244 | |
Douglas Gregor | 1931b44 | 2009-02-03 00:34:39 +0000 | [diff] [blame] | 10245 | SearchDC = DC; |
Argyrios Kyrtzidis | 0f84a23 | 2008-11-09 22:53:32 +0000 | [diff] [blame] | 10246 | // Look-up name inside 'foo::'. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 10247 | LookupQualifiedName(Previous, DC); |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 10248 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 10249 | if (Previous.isAmbiguous()) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10250 | return 0; |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 10251 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 10252 | if (Previous.empty()) { |
Douglas Gregor | 9edad9b | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 10253 | // Name lookup did not find anything. However, if the |
10254 | // nested-name-specifier refers to the current instantiation, | ||||
10255 | // and that current instantiation has any dependent base | ||||
10256 | // classes, we might find something at instantiation time: treat | ||||
10257 | // this as a dependent elaborated-type-specifier. | ||||
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 10258 | // But this only makes any sense for reference-like lookups. |
10259 | if (Previous.wasNotFoundInCurrentInstantiation() && | ||||
10260 | (TUK == TUK_Reference || TUK == TUK_Friend)) { | ||||
Douglas Gregor | 9edad9b | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 10261 | IsDependent = true; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10262 | return 0; |
Douglas Gregor | 9edad9b | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 10263 | } |
10264 | |||||
10265 | // A tag 'foo::bar' must already exist. | ||||
Douglas Gregor | 1eabb7d | 2010-03-31 23:17:41 +0000 | [diff] [blame] | 10266 | Diag(NameLoc, diag::err_not_tag_in_scope) |
10267 | << Kind << Name << DC << SS.getRange(); | ||||
Argyrios Kyrtzidis | 630c81b | 2008-11-09 22:09:58 +0000 | [diff] [blame] | 10268 | Name = 0; |
Douglas Gregor | d0c8737 | 2009-05-27 17:30:49 +0000 | [diff] [blame] | 10269 | Invalid = true; |
Argyrios Kyrtzidis | 630c81b | 2008-11-09 22:09:58 +0000 | [diff] [blame] | 10270 | goto CreateNewDecl; |
10271 | } | ||||
Chris Lattner | cf79b01 | 2009-01-21 02:38:50 +0000 | [diff] [blame] | 10272 | } else if (Name) { |
Argyrios Kyrtzidis | 630c81b | 2008-11-09 22:09:58 +0000 | [diff] [blame] | 10273 | // If this is a named struct, check to see if there was a previous forward |
10274 | // declaration or definition. | ||||
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 10275 | // FIXME: We're looking into outer scopes here, even when we |
10276 | // shouldn't be. Doing so can result in ambiguities that we | ||||
10277 | // shouldn't be diagnosing. | ||||
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 10278 | LookupName(Previous, S); |
10279 | |||||
John McCall | c96cd7a | 2013-03-20 01:53:00 +0000 | [diff] [blame] | 10280 | // When declaring or defining a tag, ignore ambiguities introduced |
10281 | // by types using'ed into this scope. | ||||
Douglas Gregor | 93b6bce | 2011-05-09 21:46:33 +0000 | [diff] [blame] | 10282 | if (Previous.isAmbiguous() && |
10283 | (TUK == TUK_Definition || TUK == TUK_Declaration)) { | ||||
Douglas Gregor | 61c6c44 | 2011-05-04 00:25:33 +0000 | [diff] [blame] | 10284 | LookupResult::Filter F = Previous.makeFilter(); |
10285 | while (F.hasNext()) { | ||||
10286 | NamedDecl *ND = F.next(); | ||||
10287 | if (ND->getDeclContext()->getRedeclContext() != SearchDC) | ||||
10288 | F.erase(); | ||||
10289 | } | ||||
10290 | F.done(); | ||||
Douglas Gregor | 61c6c44 | 2011-05-04 00:25:33 +0000 | [diff] [blame] | 10291 | } |
John McCall | c96cd7a | 2013-03-20 01:53:00 +0000 | [diff] [blame] | 10292 | |
10293 | // C++11 [namespace.memdef]p3: | ||||
10294 | // If the name in a friend declaration is neither qualified nor | ||||
10295 | // a template-id and the declaration is a function or an | ||||
10296 | // elaborated-type-specifier, the lookup to determine whether | ||||
10297 | // the entity has been previously declared shall not consider | ||||
10298 | // any scopes outside the innermost enclosing namespace. | ||||
10299 | // | ||||
10300 | // Does it matter that this should be by scope instead of by | ||||
10301 | // semantic context? | ||||
10302 | if (!Previous.empty() && TUK == TUK_Friend) { | ||||
10303 | DeclContext *EnclosingNS = SearchDC->getEnclosingNamespaceContext(); | ||||
10304 | LookupResult::Filter F = Previous.makeFilter(); | ||||
10305 | while (F.hasNext()) { | ||||
10306 | NamedDecl *ND = F.next(); | ||||
10307 | DeclContext *DC = ND->getDeclContext()->getRedeclContext(); | ||||
Douglas Gregor | d943352 | 2013-06-27 20:42:30 +0000 | [diff] [blame] | 10308 | if (DC->isFileContext() && |
10309 | !EnclosingNS->Encloses(ND->getDeclContext())) { | ||||
John McCall | c96cd7a | 2013-03-20 01:53:00 +0000 | [diff] [blame] | 10310 | F.erase(); |
Douglas Gregor | d943352 | 2013-06-27 20:42:30 +0000 | [diff] [blame] | 10311 | FriendSawTagOutsideEnclosingNamespace = true; |
10312 | } | ||||
John McCall | c96cd7a | 2013-03-20 01:53:00 +0000 | [diff] [blame] | 10313 | } |
10314 | F.done(); | ||||
10315 | } | ||||
Douglas Gregor | 61c6c44 | 2011-05-04 00:25:33 +0000 | [diff] [blame] | 10316 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 10317 | // Note: there used to be some attempt at recovery here. |
10318 | if (Previous.isAmbiguous()) | ||||
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10319 | return 0; |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 10320 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10321 | if (!getLangOpts().CPlusPlus && TUK != TUK_Reference) { |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 10322 | // FIXME: This makes sure that we ignore the contexts associated |
10323 | // with C structs, unions, and enums when looking for a matching | ||||
10324 | // tag declaration or definition. See the similar lookup tweak | ||||
Douglas Gregor | 4c921ae | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 10325 | // in Sema::LookupName; is there a better way to deal with this? |
Douglas Gregor | 4920f1f | 2009-01-12 22:49:06 +0000 | [diff] [blame] | 10326 | while (isa<RecordDecl>(SearchDC) || isa<EnumDecl>(SearchDC)) |
10327 | SearchDC = SearchDC->getParent(); | ||||
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 10328 | } |
Douglas Gregor | 069ea64 | 2010-09-16 23:58:57 +0000 | [diff] [blame] | 10329 | } else if (S->isFunctionPrototypeScope()) { |
10330 | // If this is an enum declaration in function prototype scope, set its | ||||
10331 | // initial context to the translation unit. | ||||
Nick Lewycky | 8d17681 | 2012-03-10 07:45:33 +0000 | [diff] [blame] | 10332 | // FIXME: [citation needed] |
Douglas Gregor | 069ea64 | 2010-09-16 23:58:57 +0000 | [diff] [blame] | 10333 | SearchDC = Context.getTranslationUnitDecl(); |
Argyrios Kyrtzidis | 630c81b | 2008-11-09 22:09:58 +0000 | [diff] [blame] | 10334 | } |
10335 | |||||
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 10336 | if (Previous.isSingleResult() && |
10337 | Previous.getFoundDecl()->isTemplateParameter()) { | ||||
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 10338 | // Maybe we will complain about the shadowed template parameter. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 10339 | DiagnoseTemplateParameterShadow(NameLoc, Previous.getFoundDecl()); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 10340 | // Just pretend that we didn't see the previous declaration. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 10341 | Previous.clear(); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 10342 | } |
10343 | |||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10344 | if (getLangOpts().CPlusPlus && Name && DC && StdNamespace && |
Argyrios Kyrtzidis | 76c38d3 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 10345 | DC->Equals(getStdNamespace()) && Name->isStr("bad_alloc")) { |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 10346 | // This is a declaration of or a reference to "std::bad_alloc". |
10347 | isStdBadAlloc = true; | ||||
10348 | |||||
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 10349 | if (Previous.empty() && StdBadAlloc) { |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 10350 | // std::bad_alloc has been implicitly declared (but made invisible to |
10351 | // name lookup). Fill in this implicit declaration as the previous | ||||
10352 | // declaration, so that the declarations get chained appropriately. | ||||
Argyrios Kyrtzidis | 76c38d3 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 10353 | Previous.addDecl(getStdBadAlloc()); |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 10354 | } |
10355 | } | ||||
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 10356 | |
John McCall | 9c86b51 | 2010-03-25 21:28:06 +0000 | [diff] [blame] | 10357 | // If we didn't find a previous declaration, and this is a reference |
10358 | // (or friend reference), move to the correct scope. In C++, we | ||||
10359 | // also need to do a redeclaration lookup there, just in case | ||||
10360 | // there's a shadow friend decl. | ||||
10361 | if (Name && Previous.empty() && | ||||
10362 | (TUK == TUK_Reference || TUK == TUK_Friend)) { | ||||
10363 | if (Invalid) goto CreateNewDecl; | ||||
10364 | assert(SS.isEmpty()); | ||||
10365 | |||||
10366 | if (TUK == TUK_Reference) { | ||||
10367 | // C++ [basic.scope.pdecl]p5: | ||||
10368 | // -- for an elaborated-type-specifier of the form | ||||
10369 | // | ||||
10370 | // class-key identifier | ||||
10371 | // | ||||
10372 | // if the elaborated-type-specifier is used in the | ||||
10373 | // decl-specifier-seq or parameter-declaration-clause of a | ||||
10374 | // function defined in namespace scope, the identifier is | ||||
10375 | // declared as a class-name in the namespace that contains | ||||
10376 | // the declaration; otherwise, except as a friend | ||||
10377 | // declaration, the identifier is declared in the smallest | ||||
10378 | // non-class, non-function-prototype scope that contains the | ||||
10379 | // declaration. | ||||
10380 | // | ||||
10381 | // C99 6.7.2.3p8 has a similar (but not identical!) provision for | ||||
10382 | // C structs and unions. | ||||
10383 | // | ||||
10384 | // It is an error in C++ to declare (rather than define) an enum | ||||
10385 | // type, including via an elaborated type specifier. We'll | ||||
10386 | // diagnose that later; for now, declare the enum in the same | ||||
10387 | // scope as we would have picked for any other tag type. | ||||
10388 | // | ||||
10389 | // GNU C also supports this behavior as part of its incomplete | ||||
10390 | // enum types extension, while GNU C++ does not. | ||||
10391 | // | ||||
10392 | // Find the context where we'll be declaring the tag. | ||||
10393 | // FIXME: We would like to maintain the current DeclContext as the | ||||
10394 | // lexical context, | ||||
Nick Lewycky | 1659c37 | 2012-03-10 07:47:07 +0000 | [diff] [blame] | 10395 | while (!SearchDC->isFileContext() && !SearchDC->isFunctionOrMethod()) |
John McCall | 9c86b51 | 2010-03-25 21:28:06 +0000 | [diff] [blame] | 10396 | SearchDC = SearchDC->getParent(); |
10397 | |||||
10398 | // Find the scope where we'll be declaring the tag. | ||||
10399 | while (S->isClassScope() || | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10400 | (getLangOpts().CPlusPlus && |
John McCall | 9c86b51 | 2010-03-25 21:28:06 +0000 | [diff] [blame] | 10401 | S->isFunctionPrototypeScope()) || |
10402 | ((S->getFlags() & Scope::DeclScope) == 0) || | ||||
10403 | (S->getEntity() && | ||||
10404 | ((DeclContext *)S->getEntity())->isTransparentContext())) | ||||
10405 | S = S->getParent(); | ||||
10406 | } else { | ||||
10407 | assert(TUK == TUK_Friend); | ||||
10408 | // C++ [namespace.memdef]p3: | ||||
10409 | // If a friend declaration in a non-local class first declares a | ||||
10410 | // class or function, the friend class or function is a member of | ||||
10411 | // the innermost enclosing namespace. | ||||
10412 | SearchDC = SearchDC->getEnclosingNamespaceContext(); | ||||
John McCall | 9c86b51 | 2010-03-25 21:28:06 +0000 | [diff] [blame] | 10413 | } |
10414 | |||||
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 10415 | // In C++, we need to do a redeclaration lookup to properly |
10416 | // diagnose some problems. | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10417 | if (getLangOpts().CPlusPlus) { |
John McCall | 9c86b51 | 2010-03-25 21:28:06 +0000 | [diff] [blame] | 10418 | Previous.setRedeclarationKind(ForRedeclaration); |
10419 | LookupQualifiedName(Previous, SearchDC); | ||||
10420 | } | ||||
10421 | } | ||||
10422 | |||||
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 10423 | if (!Previous.empty()) { |
Douglas Gregor | 57265e3 | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 10424 | NamedDecl *PrevDecl = (*Previous.begin())->getUnderlyingDecl(); |
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 10425 | |
10426 | // It's okay to have a tag decl in the same scope as a typedef | ||||
10427 | // which hides a tag decl in the same scope. Finding this | ||||
10428 | // insanity with a redeclaration lookup can only actually happen | ||||
10429 | // in C++. | ||||
10430 | // | ||||
10431 | // This is also okay for elaborated-type-specifiers, which is | ||||
10432 | // technically forbidden by the current standard but which is | ||||
10433 | // okay according to the likely resolution of an open issue; | ||||
10434 | // 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] | 10435 | if (getLangOpts().CPlusPlus) { |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 10436 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(PrevDecl)) { |
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 10437 | if (const TagType *TT = TD->getUnderlyingType()->getAs<TagType>()) { |
10438 | TagDecl *Tag = TT->getDecl(); | ||||
10439 | if (Tag->getDeclName() == Name && | ||||
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 10440 | Tag->getDeclContext()->getRedeclContext() |
10441 | ->Equals(TD->getDeclContext()->getRedeclContext())) { | ||||
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 10442 | PrevDecl = Tag; |
10443 | Previous.clear(); | ||||
10444 | Previous.addDecl(Tag); | ||||
Douglas Gregor | 757c600 | 2010-08-27 22:55:10 +0000 | [diff] [blame] | 10445 | Previous.resolveKind(); |
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 10446 | } |
10447 | } | ||||
10448 | } | ||||
10449 | } | ||||
10450 | |||||
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 10451 | if (TagDecl *PrevTagDecl = dyn_cast<TagDecl>(PrevDecl)) { |
Chris Lattner | 14943b9 | 2008-07-03 03:30:58 +0000 | [diff] [blame] | 10452 | // If this is a use of a previous tag, or if the tag is already declared |
10453 | // in the same scope (so that the definition/declaration completes or | ||||
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 10454 | // rementions the tag), reuse the decl. |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 10455 | if (TUK == TUK_Reference || TUK == TUK_Friend || |
Douglas Gregor | cc20945 | 2011-03-07 16:54:27 +0000 | [diff] [blame] | 10456 | isDeclInScope(PrevDecl, SearchDC, S, isExplicitSpecialization)) { |
Chris Lattner | 14943b9 | 2008-07-03 03:30:58 +0000 | [diff] [blame] | 10457 | // Make sure that this wasn't declared as an enum and now used as a |
10458 | // struct or something similar. | ||||
Richard Trieu | bbf34c0 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 10459 | if (!isAcceptableTagRedeclaration(PrevTagDecl, Kind, |
10460 | TUK == TUK_Definition, KWLoc, | ||||
10461 | *Name)) { | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10462 | bool SafeToContinue |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 10463 | = (PrevTagDecl->getTagKind() != TTK_Enum && |
10464 | Kind != TTK_Enum); | ||||
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 10465 | if (SafeToContinue) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10466 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 10467 | << Name |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 10468 | << FixItHint::CreateReplacement(SourceRange(KWLoc), |
10469 | PrevTagDecl->getKindName()); | ||||
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 10470 | else |
10471 | Diag(KWLoc, diag::err_use_with_wrong_tag) << Name; | ||||
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 10472 | Diag(PrevTagDecl->getLocation(), diag::note_previous_use); |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 10473 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10474 | if (SafeToContinue) |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 10475 | Kind = PrevTagDecl->getTagKind(); |
10476 | else { | ||||
10477 | // Recover by making this an anonymous redefinition. | ||||
10478 | Name = 0; | ||||
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 10479 | Previous.clear(); |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 10480 | Invalid = true; |
10481 | } | ||||
10482 | } | ||||
10483 | |||||
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 10484 | if (Kind == TTK_Enum && PrevTagDecl->getTagKind() == TTK_Enum) { |
10485 | const EnumDecl *PrevEnum = cast<EnumDecl>(PrevTagDecl); | ||||
10486 | |||||
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 10487 | // If this is an elaborated-type-specifier for a scoped enumeration, |
10488 | // the 'class' keyword is not necessary and not permitted. | ||||
10489 | if (TUK == TUK_Reference || TUK == TUK_Friend) { | ||||
10490 | if (ScopedEnum) | ||||
10491 | Diag(ScopedEnumKWLoc, diag::err_enum_class_reference) | ||||
10492 | << PrevEnum->isScoped() | ||||
10493 | << FixItHint::CreateRemoval(ScopedEnumKWLoc); | ||||
10494 | return PrevTagDecl; | ||||
10495 | } | ||||
10496 | |||||
Richard Smith | f1c66b4 | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 10497 | QualType EnumUnderlyingTy; |
10498 | if (TypeSourceInfo *TI = EnumUnderlying.dyn_cast<TypeSourceInfo*>()) | ||||
10499 | EnumUnderlyingTy = TI->getType(); | ||||
10500 | else if (const Type *T = EnumUnderlying.dyn_cast<const Type*>()) | ||||
10501 | EnumUnderlyingTy = QualType(T, 0); | ||||
10502 | |||||
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 10503 | // All conflicts with previous declarations are recovered by |
Richard Smith | 3343fad | 2012-03-23 23:09:08 +0000 | [diff] [blame] | 10504 | // returning the previous declaration, unless this is a definition, |
10505 | // in which case we want the caller to bail out. | ||||
Richard Smith | f1c66b4 | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 10506 | if (CheckEnumRedeclaration(NameLoc.isValid() ? NameLoc : KWLoc, |
10507 | ScopedEnum, EnumUnderlyingTy, PrevEnum)) | ||||
Richard Smith | 3343fad | 2012-03-23 23:09:08 +0000 | [diff] [blame] | 10508 | return TUK == TUK_Declaration ? PrevTagDecl : 0; |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 10509 | } |
10510 | |||||
David Majnemer | 2ec2b84 | 2013-06-11 03:51:23 +0000 | [diff] [blame] | 10511 | // C++11 [class.mem]p1: |
David Majnemer | 0f9b855 | 2013-06-11 06:19:45 +0000 | [diff] [blame] | 10512 | // A member shall not be declared twice in the member-specification, |
David Majnemer | 2ec2b84 | 2013-06-11 03:51:23 +0000 | [diff] [blame] | 10513 | // except that a nested class or member class template can be declared |
10514 | // and then later defined. | ||||
10515 | if (TUK == TUK_Declaration && PrevDecl->isCXXClassMember() && | ||||
10516 | S->isDeclScope(PrevDecl)) { | ||||
10517 | Diag(NameLoc, diag::ext_member_redeclared); | ||||
10518 | Diag(PrevTagDecl->getLocation(), diag::note_previous_declaration); | ||||
10519 | } | ||||
10520 | |||||
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 10521 | if (!Invalid) { |
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 10522 | // If this is a use, just return the declaration we found. |
Chris Lattner | 14943b9 | 2008-07-03 03:30:58 +0000 | [diff] [blame] | 10523 | |
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 10524 | // FIXME: In the future, return a variant or some other clue |
10525 | // for the consumer of this Decl to know it doesn't own it. | ||||
10526 | // For our current ASTs this shouldn't be a problem, but will | ||||
10527 | // need to be changed with DeclGroups. | ||||
Francois Pichet | b474603 | 2011-06-01 04:14:20 +0000 | [diff] [blame] | 10528 | if ((TUK == TUK_Reference && (!PrevTagDecl->getFriendObjectKind() || |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10529 | getLangOpts().MicrosoftExt)) || TUK == TUK_Friend) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10530 | return PrevTagDecl; |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 10531 | |
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 10532 | // Diagnose attempts to redefine a tag. |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 10533 | if (TUK == TUK_Definition) { |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 10534 | if (TagDecl *Def = PrevTagDecl->getDefinition()) { |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 10535 | // If we're defining a specialization and the previous definition |
10536 | // is from an implicit instantiation, don't emit an error | ||||
10537 | // here; we'll catch this in the general case below. | ||||
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 10538 | bool IsExplicitSpecializationAfterInstantiation = false; |
10539 | if (isExplicitSpecialization) { | ||||
10540 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Def)) | ||||
10541 | IsExplicitSpecializationAfterInstantiation = | ||||
10542 | RD->getTemplateSpecializationKind() != | ||||
10543 | TSK_ExplicitSpecialization; | ||||
10544 | else if (EnumDecl *ED = dyn_cast<EnumDecl>(Def)) | ||||
10545 | IsExplicitSpecializationAfterInstantiation = | ||||
10546 | ED->getTemplateSpecializationKind() != | ||||
10547 | TSK_ExplicitSpecialization; | ||||
10548 | } | ||||
10549 | |||||
10550 | if (!IsExplicitSpecializationAfterInstantiation) { | ||||
James Molloy | 16f1f71 | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 10551 | // A redeclaration in function prototype scope in C isn't |
10552 | // visible elsewhere, so merely issue a warning. | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10553 | if (!getLangOpts().CPlusPlus && S->containedInPrototypeScope()) |
James Molloy | 16f1f71 | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 10554 | Diag(NameLoc, diag::warn_redefinition_in_param_list) << Name; |
10555 | else | ||||
10556 | Diag(NameLoc, diag::err_redefinition) << Name; | ||||
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 10557 | Diag(Def->getLocation(), diag::note_previous_definition); |
10558 | // If this is a redefinition, recover by making this | ||||
10559 | // struct be anonymous, which will make any later | ||||
10560 | // references get the previous definition. | ||||
10561 | Name = 0; | ||||
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 10562 | Previous.clear(); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 10563 | Invalid = true; |
10564 | } | ||||
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 10565 | } else { |
10566 | // If the type is currently being defined, complain | ||||
10567 | // about a nested redefinition. | ||||
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 10568 | const TagType *Tag |
10569 | = cast<TagType>(Context.getTagDeclType(PrevTagDecl)); | ||||
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 10570 | if (Tag->isBeingDefined()) { |
10571 | Diag(NameLoc, diag::err_nested_redefinition) << Name; | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10572 | Diag(PrevTagDecl->getLocation(), |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 10573 | diag::note_previous_definition); |
10574 | Name = 0; | ||||
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 10575 | Previous.clear(); |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 10576 | Invalid = true; |
10577 | } | ||||
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 10578 | } |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 10579 | |
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 10580 | // Okay, this is definition of a previously declared or referenced |
10581 | // tag PrevDecl. We're going to create a new Decl for it. | ||||
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 10582 | } |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 10583 | } |
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 10584 | // If we get here we have (another) forward declaration or we |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 10585 | // have a definition. Just create a new decl. |
10586 | |||||
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 10587 | } else { |
10588 | // 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] | 10589 | // scope, e.g. "struct foo; void bar() { struct foo; }", just create a |
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 10590 | // new decl/type. We set PrevDecl to NULL so that the entities |
10591 | // have distinct types. | ||||
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 10592 | Previous.clear(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 10593 | } |
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 10594 | // If we get here, we're going to create a new Decl. If PrevDecl |
10595 | // is non-NULL, it's a definition of the tag declared by | ||||
10596 | // PrevDecl. If it's NULL, we have a new definition. | ||||
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 10597 | |
10598 | |||||
10599 | // Otherwise, PrevDecl is not a tag, but was found with tag | ||||
10600 | // lookup. This is only actually possible in C++, where a few | ||||
10601 | // things like templates still live in the tag namespace. | ||||
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 10602 | } else { |
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 10603 | // Use a better diagnostic if an elaborated-type-specifier |
10604 | // found the wrong kind of type on the first | ||||
10605 | // (non-redeclaration) lookup. | ||||
10606 | if ((TUK == TUK_Reference || TUK == TUK_Friend) && | ||||
10607 | !Previous.isForRedeclaration()) { | ||||
10608 | unsigned Kind = 0; | ||||
10609 | if (isa<TypedefDecl>(PrevDecl)) Kind = 1; | ||||
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 10610 | else if (isa<TypeAliasDecl>(PrevDecl)) Kind = 2; |
10611 | else if (isa<ClassTemplateDecl>(PrevDecl)) Kind = 3; | ||||
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 10612 | Diag(NameLoc, diag::err_tag_reference_non_tag) << Kind; |
10613 | Diag(PrevDecl->getLocation(), diag::note_declared_at); | ||||
10614 | Invalid = true; | ||||
10615 | |||||
10616 | // Otherwise, only diagnose if the declaration is in scope. | ||||
Douglas Gregor | cc20945 | 2011-03-07 16:54:27 +0000 | [diff] [blame] | 10617 | } else if (!isDeclInScope(PrevDecl, SearchDC, S, |
10618 | isExplicitSpecialization)) { | ||||
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 10619 | // do nothing |
10620 | |||||
10621 | // Diagnose implicit declarations introduced by elaborated types. | ||||
10622 | } else if (TUK == TUK_Reference || TUK == TUK_Friend) { | ||||
10623 | unsigned Kind = 0; | ||||
10624 | if (isa<TypedefDecl>(PrevDecl)) Kind = 1; | ||||
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 10625 | else if (isa<TypeAliasDecl>(PrevDecl)) Kind = 2; |
10626 | else if (isa<ClassTemplateDecl>(PrevDecl)) Kind = 3; | ||||
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 10627 | Diag(NameLoc, diag::err_tag_reference_conflict) << Kind; |
10628 | Diag(PrevDecl->getLocation(), diag::note_previous_decl) << PrevDecl; | ||||
10629 | Invalid = true; | ||||
10630 | |||||
10631 | // Otherwise it's a declaration. Call out a particularly common | ||||
10632 | // case here. | ||||
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 10633 | } else if (TypedefNameDecl *TND = dyn_cast<TypedefNameDecl>(PrevDecl)) { |
10634 | unsigned Kind = 0; | ||||
10635 | if (isa<TypeAliasDecl>(PrevDecl)) Kind = 1; | ||||
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 10636 | Diag(NameLoc, diag::err_tag_definition_of_typedef) |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 10637 | << Name << Kind << TND->getUnderlyingType(); |
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 10638 | Diag(PrevDecl->getLocation(), diag::note_previous_decl) << PrevDecl; |
10639 | Invalid = true; | ||||
10640 | |||||
10641 | // Otherwise, diagnose. | ||||
10642 | } else { | ||||
10643 | // The tag name clashes with something else in the target scope, | ||||
10644 | // issue an error and recover by making this tag be anonymous. | ||||
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 10645 | Diag(NameLoc, diag::err_redefinition_different_kind) << Name; |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 10646 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Argyrios Kyrtzidis | b02ef24 | 2008-07-16 07:45:46 +0000 | [diff] [blame] | 10647 | Name = 0; |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 10648 | Invalid = true; |
Argyrios Kyrtzidis | b02ef24 | 2008-07-16 07:45:46 +0000 | [diff] [blame] | 10649 | } |
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 10650 | |
10651 | // The existing declaration isn't relevant to us; we're in a | ||||
10652 | // new scope, so clear out the previous declaration. | ||||
10653 | Previous.clear(); | ||||
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 10654 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 10655 | } |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 10656 | |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 10657 | CreateNewDecl: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10658 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 10659 | TagDecl *PrevDecl = 0; |
10660 | if (Previous.isSingleResult()) | ||||
10661 | PrevDecl = cast<TagDecl>(Previous.getFoundDecl()); | ||||
10662 | |||||
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 10663 | // If there is an identifier, use the location of the identifier as the |
10664 | // location of the decl, otherwise use the location of the struct/union | ||||
10665 | // keyword. | ||||
10666 | SourceLocation Loc = NameLoc.isValid() ? NameLoc : KWLoc; | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10667 | |
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 10668 | // Otherwise, create a new declaration. If there is a previous |
10669 | // declaration of the same entity, the two will be linked via | ||||
10670 | // PrevDecl. | ||||
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 10671 | TagDecl *New; |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 10672 | |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 10673 | bool IsForwardReference = false; |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 10674 | if (Kind == TTK_Enum) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 10675 | // FIXME: Tag decls should be chained to any simultaneous vardecls, e.g.: |
10676 | // enum X { A, B, C } D; D should chain to X. | ||||
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 10677 | New = EnumDecl::Create(Context, SearchDC, KWLoc, Loc, Name, |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 10678 | cast_or_null<EnumDecl>(PrevDecl), ScopedEnum, |
Abramo Bagnara | a88cefd | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 10679 | ScopedEnumUsesClassTag, !EnumUnderlying.isNull()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 10680 | // If this is an undefined enum, warn. |
Douglas Gregor | f3a7b7c | 2010-06-22 14:26:35 +0000 | [diff] [blame] | 10681 | if (TUK != TUK_Definition && !Invalid) { |
10682 | TagDecl *Def; | ||||
Douglas Gregor | abde2c7 | 2013-03-25 22:22:35 +0000 | [diff] [blame] | 10683 | if ((getLangOpts().CPlusPlus11 || getLangOpts().ObjC2) && |
10684 | cast<EnumDecl>(New)->isFixed()) { | ||||
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 10685 | // C++0x: 7.2p2: opaque-enum-declaration. |
10686 | // Conflicts are diagnosed above. Do nothing. | ||||
10687 | } | ||||
10688 | else if (PrevDecl && (Def = cast<EnumDecl>(PrevDecl)->getDefinition())) { | ||||
Douglas Gregor | f3a7b7c | 2010-06-22 14:26:35 +0000 | [diff] [blame] | 10689 | Diag(Loc, diag::ext_forward_ref_enum_def) |
10690 | << New; | ||||
10691 | Diag(Def->getLocation(), diag::note_previous_definition); | ||||
10692 | } else { | ||||
Francois Pichet | 8dc3abc | 2010-09-12 05:06:55 +0000 | [diff] [blame] | 10693 | unsigned DiagID = diag::ext_forward_ref_enum; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10694 | if (getLangOpts().MicrosoftMode) |
Francois Pichet | 8dc3abc | 2010-09-12 05:06:55 +0000 | [diff] [blame] | 10695 | DiagID = diag::ext_ms_forward_ref_enum; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10696 | else if (getLangOpts().CPlusPlus) |
Francois Pichet | 8dc3abc | 2010-09-12 05:06:55 +0000 | [diff] [blame] | 10697 | DiagID = diag::err_forward_ref_enum; |
10698 | Diag(Loc, DiagID); | ||||
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 10699 | |
10700 | // If this is a forward-declared reference to an enumeration, make a | ||||
10701 | // note of it; we won't actually be introducing the declaration into | ||||
10702 | // the declaration context. | ||||
10703 | if (TUK == TUK_Reference) | ||||
10704 | IsForwardReference = true; | ||||
Douglas Gregor | f3a7b7c | 2010-06-22 14:26:35 +0000 | [diff] [blame] | 10705 | } |
Douglas Gregor | 80711a2 | 2009-03-06 18:34:03 +0000 | [diff] [blame] | 10706 | } |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 10707 | |
10708 | if (EnumUnderlying) { | ||||
10709 | EnumDecl *ED = cast<EnumDecl>(New); | ||||
10710 | if (TypeSourceInfo *TI = EnumUnderlying.dyn_cast<TypeSourceInfo*>()) | ||||
10711 | ED->setIntegerTypeSourceInfo(TI); | ||||
10712 | else | ||||
10713 | ED->setIntegerType(QualType(EnumUnderlying.get<const Type*>(), 0)); | ||||
10714 | ED->setPromotionType(ED->getIntegerType()); | ||||
10715 | } | ||||
10716 | |||||
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 10717 | } else { |
10718 | // struct/union/class | ||||
10719 | |||||
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 10720 | // FIXME: Tag decls should be chained to any simultaneous vardecls, e.g.: |
10721 | // struct X { int A; } D; D should chain to X. | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10722 | if (getLangOpts().CPlusPlus) { |
Ted Kremenek | 2b345eb | 2008-09-05 17:39:33 +0000 | [diff] [blame] | 10723 | // FIXME: Look for a way to use RecordDecl for simple structs. |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 10724 | New = CXXRecordDecl::Create(Context, Kind, SearchDC, KWLoc, Loc, Name, |
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 10725 | cast_or_null<CXXRecordDecl>(PrevDecl)); |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 10726 | |
Argyrios Kyrtzidis | 76c38d3 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 10727 | if (isStdBadAlloc && (!StdBadAlloc || getStdBadAlloc()->isImplicit())) |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 10728 | StdBadAlloc = cast<CXXRecordDecl>(New); |
10729 | } else | ||||
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 10730 | New = RecordDecl::Create(Context, Kind, SearchDC, KWLoc, Loc, Name, |
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 10731 | cast_or_null<RecordDecl>(PrevDecl)); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 10732 | } |
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 10733 | |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 10734 | // Maybe add qualifier info. |
10735 | if (SS.isNotEmpty()) { | ||||
Fariborz Jahanian | 4fb2053 | 2010-05-14 21:35:02 +0000 | [diff] [blame] | 10736 | if (SS.isSet()) { |
Douglas Gregor | 6960587 | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 10737 | // If this is either a declaration or a definition, check the |
10738 | // nested-name-specifier against the current context. We don't do this | ||||
10739 | // for explicit specializations, because they have similar checking | ||||
10740 | // (with more specific diagnostics) in the call to | ||||
10741 | // CheckMemberSpecialization, below. | ||||
10742 | if (!isExplicitSpecialization && | ||||
10743 | (TUK == TUK_Definition || TUK == TUK_Declaration) && | ||||
10744 | diagnoseQualifiedDeclaration(SS, DC, OrigName, NameLoc)) | ||||
10745 | Invalid = true; | ||||
10746 | |||||
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 10747 | New->setQualifierInfo(SS.getWithLocInContext(Context)); |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 10748 | if (TemplateParameterLists.size() > 0) { |
Douglas Gregor | c722ea4 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 10749 | New->setTemplateParameterListsInfo(Context, |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 10750 | TemplateParameterLists.size(), |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 10751 | TemplateParameterLists.data()); |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 10752 | } |
Fariborz Jahanian | 4fb2053 | 2010-05-14 21:35:02 +0000 | [diff] [blame] | 10753 | } |
10754 | else | ||||
10755 | Invalid = true; | ||||
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 10756 | } |
10757 | |||||
Daniel Dunbar | 9f21f89 | 2010-05-27 01:53:40 +0000 | [diff] [blame] | 10758 | if (RecordDecl *RD = dyn_cast<RecordDecl>(New)) { |
10759 | // Add alignment attributes if necessary; these attributes are checked when | ||||
10760 | // the ASTContext lays out the structure. | ||||
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 10761 | // |
10762 | // It is important for implementing the correct semantics that this | ||||
10763 | // happen here (in act on tag decl). The #pragma pack stack is | ||||
10764 | // maintained as a result of parser callbacks which can occur at | ||||
10765 | // many points during the parsing of a struct declaration (because | ||||
10766 | // the #pragma tokens are effectively skipped over during the | ||||
10767 | // parsing of the struct). | ||||
Eli Friedman | 2016c8c | 2012-08-08 21:08:34 +0000 | [diff] [blame] | 10768 | if (TUK == TUK_Definition) { |
10769 | AddAlignmentAttributesForRecord(RD); | ||||
10770 | AddMsStructLayoutForRecord(RD); | ||||
10771 | } | ||||
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 10772 | } |
10773 | |||||
Douglas Gregor | 2ccd89c | 2011-12-20 18:11:52 +0000 | [diff] [blame] | 10774 | if (ModulePrivateLoc.isValid()) { |
Douglas Gregor | d023aec | 2011-09-09 20:53:38 +0000 | [diff] [blame] | 10775 | if (isExplicitSpecialization) |
10776 | Diag(New->getLocation(), diag::err_module_private_specialization) | ||||
10777 | << 2 | ||||
10778 | << FixItHint::CreateRemoval(ModulePrivateLoc); | ||||
Douglas Gregor | e389585 | 2011-09-12 18:37:38 +0000 | [diff] [blame] | 10779 | // __module_private__ does not apply to local classes. However, we only |
10780 | // diagnose this as an error when the declaration specifiers are | ||||
10781 | // freestanding. Here, we just ignore the __module_private__. | ||||
Douglas Gregor | e389585 | 2011-09-12 18:37:38 +0000 | [diff] [blame] | 10782 | else if (!SearchDC->isFunctionOrMethod()) |
Douglas Gregor | e761230 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 10783 | New->setModulePrivate(); |
10784 | } | ||||
10785 | |||||
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 10786 | // If this is a specialization of a member class (of a class template), |
10787 | // check the specialization. | ||||
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 10788 | if (isExplicitSpecialization && CheckMemberSpecialization(New, Previous)) |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 10789 | Invalid = true; |
Douglas Gregor | 6960587 | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 10790 | |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 10791 | if (Invalid) |
10792 | New->setInvalidDecl(); | ||||
10793 | |||||
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 10794 | if (Attr) |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 10795 | ProcessDeclAttributeList(S, New, Attr); |
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 10796 | |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 10797 | // If we're declaring or defining a tag in function prototype scope |
10798 | // 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] | 10799 | if (Name && S->isFunctionPrototypeScope() && !getLangOpts().CPlusPlus) |
Douglas Gregor | 3218c4b | 2009-01-09 22:42:13 +0000 | [diff] [blame] | 10800 | Diag(Loc, diag::warn_decl_in_param_list) << Context.getTagDeclType(New); |
10801 | |||||
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 10802 | // Set the lexical context. If the tag has a C++ scope specifier, the |
10803 | // lexical context will be different from the semantic context. | ||||
Douglas Gregor | 1931b44 | 2009-02-03 00:34:39 +0000 | [diff] [blame] | 10804 | New->setLexicalDeclContext(CurContext); |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 10805 | |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 10806 | // Mark this as a friend decl if applicable. |
Francois Pichet | b474603 | 2011-06-01 04:14:20 +0000 | [diff] [blame] | 10807 | // In Microsoft mode, a friend declaration also acts as a forward |
10808 | // declaration so we always pass true to setObjectOfFriendDecl to make | ||||
10809 | // the tag name visible. | ||||
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 10810 | if (TUK == TUK_Friend) |
Richard Smith | 22050f2 | 2013-07-17 23:53:16 +0000 | [diff] [blame] | 10811 | New->setObjectOfFriendDecl(!FriendSawTagOutsideEnclosingNamespace && |
10812 | getLangOpts().MicrosoftExt); | ||||
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 10813 | |
Anders Carlsson | 0cf8830 | 2009-03-26 01:19:02 +0000 | [diff] [blame] | 10814 | // Set the access specifier. |
John McCall | 9c86b51 | 2010-03-25 21:28:06 +0000 | [diff] [blame] | 10815 | if (!Invalid && SearchDC->isRecord()) |
Douglas Gregor | d0c8737 | 2009-05-27 17:30:49 +0000 | [diff] [blame] | 10816 | SetMemberAccessSpecifier(New, PrevDecl, AS); |
Douglas Gregor | 06c0fec | 2009-03-25 22:00:53 +0000 | [diff] [blame] | 10817 | |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 10818 | if (TUK == TUK_Definition) |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 10819 | New->startDefinition(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10820 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 10821 | // If this has an identifier, add it to the scope stack. |
John McCall | d7eff68 | 2009-09-02 00:55:30 +0000 | [diff] [blame] | 10822 | if (TUK == TUK_Friend) { |
John McCall | 82b9fb8 | 2009-09-02 19:32:14 +0000 | [diff] [blame] | 10823 | // We might be replacing an existing declaration in the lookup tables; |
10824 | // if so, borrow its access specifier. | ||||
10825 | if (PrevDecl) | ||||
10826 | New->setAccess(PrevDecl->getAccess()); | ||||
10827 | |||||
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 10828 | DeclContext *DC = New->getDeclContext()->getRedeclContext(); |
Richard Smith | 1b7f9cb | 2012-03-13 03:12:56 +0000 | [diff] [blame] | 10829 | DC->makeDeclVisibleInContext(New); |
John McCall | 9c86b51 | 2010-03-25 21:28:06 +0000 | [diff] [blame] | 10830 | if (Name) // can be null along some error paths |
John McCall | d7eff68 | 2009-09-02 00:55:30 +0000 | [diff] [blame] | 10831 | if (Scope *EnclosingScope = getScopeForDeclContext(S, DC)) |
10832 | PushOnScopeChains(New, EnclosingScope, /* AddToContext = */ false); | ||||
John McCall | d7eff68 | 2009-09-02 00:55:30 +0000 | [diff] [blame] | 10833 | } else if (Name) { |
Douglas Gregor | 1a0d31a | 2009-01-12 18:45:55 +0000 | [diff] [blame] | 10834 | S = getNonFieldDeclScope(S); |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 10835 | PushOnScopeChains(New, S, !IsForwardReference); |
10836 | if (IsForwardReference) | ||||
Richard Smith | 1b7f9cb | 2012-03-13 03:12:56 +0000 | [diff] [blame] | 10837 | SearchDC->makeDeclVisibleInContext(New); |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 10838 | |
Douglas Gregor | 4920f1f | 2009-01-12 22:49:06 +0000 | [diff] [blame] | 10839 | } else { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 10840 | CurContext->addDecl(New); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 10841 | } |
Argyrios Kyrtzidis | 5239304 | 2008-11-09 23:41:00 +0000 | [diff] [blame] | 10842 | |
Douglas Gregor | c29f77b | 2009-07-07 16:35:42 +0000 | [diff] [blame] | 10843 | // If this is the C FILE type, notify the AST context. |
10844 | if (IdentifierInfo *II = New->getIdentifier()) | ||||
10845 | if (!New->isInvalidDecl() && | ||||
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 10846 | New->getDeclContext()->getRedeclContext()->isTranslationUnit() && |
Douglas Gregor | c29f77b | 2009-07-07 16:35:42 +0000 | [diff] [blame] | 10847 | II->isStr("FILE")) |
10848 | Context.setFILEDecl(New); | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10849 | |
James Molloy | 16f1f71 | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 10850 | // If we were in function prototype scope (and not in C++ mode), add this |
10851 | // 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] | 10852 | if (S->isFunctionPrototypeScope() && !getLangOpts().CPlusPlus && |
James Molloy | 16f1f71 | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 10853 | InFunctionDeclarator && Name) |
10854 | DeclsInPrototypeScope.push_back(New); | ||||
10855 | |||||
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 10856 | if (PrevDecl) |
10857 | mergeDeclAttributes(New, PrevDecl); | ||||
10858 | |||||
Rafael Espindola | 71adc5b | 2012-07-17 15:14:47 +0000 | [diff] [blame] | 10859 | // If there's a #pragma GCC visibility in scope, set the visibility of this |
10860 | // record. | ||||
10861 | AddPushedVisibilityAttribute(New); | ||||
10862 | |||||
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 10863 | OwnedDecl = true; |
Richard Smith | 37ec8d5 | 2012-12-05 11:34:06 +0000 | [diff] [blame] | 10864 | // In C++, don't return an invalid declaration. We can't recover well from |
10865 | // the cases where we make the type anonymous. | ||||
10866 | return (Invalid && getLangOpts().CPlusPlus) ? 0 : New; | ||||
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 10867 | } |
10868 | |||||
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10869 | void Sema::ActOnTagStartDefinition(Scope *S, Decl *TagD) { |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 10870 | AdjustDeclIfTemplate(TagD); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10871 | TagDecl *Tag = cast<TagDecl>(TagD); |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 10872 | |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 10873 | // Enter the tag context. |
10874 | PushDeclContext(S, Tag); | ||||
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 10875 | |
10876 | ActOnDocumentableDecl(TagD); | ||||
Rafael Espindola | 5e06529 | 2012-07-12 04:47:34 +0000 | [diff] [blame] | 10877 | |
10878 | // If there's a #pragma GCC visibility in scope, set the visibility of this | ||||
10879 | // record. | ||||
10880 | AddPushedVisibilityAttribute(Tag); | ||||
John McCall | f936815 | 2009-12-20 07:58:13 +0000 | [diff] [blame] | 10881 | } |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 10882 | |
Argyrios Kyrtzidis | 3a38744 | 2011-10-06 23:23:20 +0000 | [diff] [blame] | 10883 | Decl *Sema::ActOnObjCContainerStartDefinition(Decl *IDecl) { |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 10884 | assert(isa<ObjCContainerDecl>(IDecl) && |
10885 | "ActOnObjCContainerStartDefinition - Not ObjCContainerDecl"); | ||||
10886 | DeclContext *OCD = cast<DeclContext>(IDecl); | ||||
10887 | assert(getContainingDC(OCD) == CurContext && | ||||
10888 | "The next DeclContext should be lexically contained in the current one."); | ||||
10889 | CurContext = OCD; | ||||
Argyrios Kyrtzidis | 3a38744 | 2011-10-06 23:23:20 +0000 | [diff] [blame] | 10890 | return IDecl; |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 10891 | } |
10892 | |||||
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10893 | void Sema::ActOnStartCXXMemberDeclarations(Scope *S, Decl *TagD, |
Anders Carlsson | 2c3ee54 | 2011-03-25 14:31:08 +0000 | [diff] [blame] | 10894 | SourceLocation FinalLoc, |
John McCall | f936815 | 2009-12-20 07:58:13 +0000 | [diff] [blame] | 10895 | SourceLocation LBraceLoc) { |
10896 | AdjustDeclIfTemplate(TagD); | ||||
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10897 | CXXRecordDecl *Record = cast<CXXRecordDecl>(TagD); |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 10898 | |
John McCall | f936815 | 2009-12-20 07:58:13 +0000 | [diff] [blame] | 10899 | FieldCollector->StartClass(); |
10900 | |||||
10901 | if (!Record->getIdentifier()) | ||||
10902 | return; | ||||
10903 | |||||
Anders Carlsson | 2c3ee54 | 2011-03-25 14:31:08 +0000 | [diff] [blame] | 10904 | if (FinalLoc.isValid()) |
10905 | Record->addAttr(new (Context) FinalAttr(FinalLoc, Context)); | ||||
Anders Carlsson | dfc2f10 | 2011-01-22 17:51:53 +0000 | [diff] [blame] | 10906 | |
John McCall | f936815 | 2009-12-20 07:58:13 +0000 | [diff] [blame] | 10907 | // C++ [class]p2: |
10908 | // [...] The class-name is also inserted into the scope of the | ||||
10909 | // class itself; this is known as the injected-class-name. For | ||||
10910 | // purposes of access checking, the injected-class-name is treated | ||||
10911 | // as if it were a public member name. | ||||
10912 | CXXRecordDecl *InjectedClassName | ||||
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 10913 | = CXXRecordDecl::Create(Context, Record->getTagKind(), CurContext, |
10914 | Record->getLocStart(), Record->getLocation(), | ||||
John McCall | f936815 | 2009-12-20 07:58:13 +0000 | [diff] [blame] | 10915 | Record->getIdentifier(), |
Argyrios Kyrtzidis | 3b8f610 | 2010-10-14 20:14:21 +0000 | [diff] [blame] | 10916 | /*PrevDecl=*/0, |
10917 | /*DelayTypeCreation=*/true); | ||||
10918 | Context.getTypeDeclType(InjectedClassName, Record); | ||||
John McCall | f936815 | 2009-12-20 07:58:13 +0000 | [diff] [blame] | 10919 | InjectedClassName->setImplicit(); |
10920 | InjectedClassName->setAccess(AS_public); | ||||
10921 | if (ClassTemplateDecl *Template = Record->getDescribedClassTemplate()) | ||||
10922 | InjectedClassName->setDescribedClassTemplate(Template); | ||||
10923 | PushOnScopeChains(InjectedClassName, S); | ||||
10924 | assert(InjectedClassName->isInjectedClassName() && | ||||
10925 | "Broken injected-class-name"); | ||||
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 10926 | } |
10927 | |||||
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10928 | void Sema::ActOnTagFinishDefinition(Scope *S, Decl *TagD, |
Argyrios Kyrtzidis | 07a5b28 | 2009-07-14 03:17:52 +0000 | [diff] [blame] | 10929 | SourceLocation RBraceLoc) { |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 10930 | AdjustDeclIfTemplate(TagD); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10931 | TagDecl *Tag = cast<TagDecl>(TagD); |
Argyrios Kyrtzidis | 07a5b28 | 2009-07-14 03:17:52 +0000 | [diff] [blame] | 10932 | Tag->setRBraceLoc(RBraceLoc); |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 10933 | |
Argyrios Kyrtzidis | 216f78b | 2012-03-09 20:10:30 +0000 | [diff] [blame] | 10934 | // Make sure we "complete" the definition even it is invalid. |
10935 | if (Tag->isBeingDefined()) { | ||||
10936 | assert(Tag->isInvalidDecl() && "We should already have completed it"); | ||||
10937 | if (RecordDecl *RD = dyn_cast<RecordDecl>(Tag)) | ||||
10938 | RD->completeDefinition(); | ||||
10939 | } | ||||
10940 | |||||
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 10941 | if (isa<CXXRecordDecl>(Tag)) |
10942 | FieldCollector->FinishClass(); | ||||
10943 | |||||
10944 | // Exit this scope of this tag's definition. | ||||
10945 | PopDeclContext(); | ||||
Argyrios Kyrtzidis | 3d207e7 | 2013-01-29 18:00:54 +0000 | [diff] [blame] | 10946 | |
10947 | if (getCurLexicalContext()->isObjCContainer() && | ||||
10948 | Tag->getDeclContext()->isFileContext()) | ||||
10949 | Tag->setTopLevelDeclInObjCContainer(); | ||||
10950 | |||||
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 10951 | // Notify the consumer that we've defined a tag. |
Serge Pavlov | 439b701 | 2013-07-02 17:31:56 +0000 | [diff] [blame] | 10952 | if (!Tag->isInvalidDecl()) |
10953 | Consumer.HandleTagDeclDefinition(Tag); | ||||
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 10954 | } |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 10955 | |
Fariborz Jahanian | 10af879 | 2011-08-29 17:33:12 +0000 | [diff] [blame] | 10956 | void Sema::ActOnObjCContainerFinishDefinition() { |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 10957 | // Exit this scope of this interface definition. |
10958 | PopDeclContext(); | ||||
10959 | } | ||||
Argyrios Kyrtzidis | 3a38744 | 2011-10-06 23:23:20 +0000 | [diff] [blame] | 10960 | |
Argyrios Kyrtzidis | 458bacf | 2011-10-27 00:09:34 +0000 | [diff] [blame] | 10961 | void Sema::ActOnObjCTemporaryExitContainerContext(DeclContext *DC) { |
Argyrios Kyrtzidis | 4a7dc8a | 2011-10-27 00:53:06 +0000 | [diff] [blame] | 10962 | assert(DC == CurContext && "Mismatch of container contexts"); |
10963 | OriginalLexicalContext = DC; | ||||
Argyrios Kyrtzidis | 3a38744 | 2011-10-06 23:23:20 +0000 | [diff] [blame] | 10964 | ActOnObjCContainerFinishDefinition(); |
10965 | } | ||||
10966 | |||||
Argyrios Kyrtzidis | 458bacf | 2011-10-27 00:09:34 +0000 | [diff] [blame] | 10967 | void Sema::ActOnObjCReenterContainerContext(DeclContext *DC) { |
10968 | ActOnObjCContainerStartDefinition(cast<Decl>(DC)); | ||||
Argyrios Kyrtzidis | 3a38744 | 2011-10-06 23:23:20 +0000 | [diff] [blame] | 10969 | OriginalLexicalContext = 0; |
10970 | } | ||||
10971 | |||||
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10972 | void Sema::ActOnTagDefinitionError(Scope *S, Decl *TagD) { |
John McCall | db7bb4a | 2010-03-17 00:38:33 +0000 | [diff] [blame] | 10973 | AdjustDeclIfTemplate(TagD); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10974 | TagDecl *Tag = cast<TagDecl>(TagD); |
John McCall | db7bb4a | 2010-03-17 00:38:33 +0000 | [diff] [blame] | 10975 | Tag->setInvalidDecl(); |
10976 | |||||
Argyrios Kyrtzidis | 216f78b | 2012-03-09 20:10:30 +0000 | [diff] [blame] | 10977 | // Make sure we "complete" the definition even it is invalid. |
10978 | if (Tag->isBeingDefined()) { | ||||
10979 | if (RecordDecl *RD = dyn_cast<RecordDecl>(Tag)) | ||||
10980 | RD->completeDefinition(); | ||||
10981 | } | ||||
10982 | |||||
John McCall | a8cab01 | 2010-03-17 19:25:57 +0000 | [diff] [blame] | 10983 | // We're undoing ActOnTagStartDefinition here, not |
10984 | // ActOnStartCXXMemberDeclarations, so we don't have to mess with | ||||
10985 | // the FieldCollector. | ||||
John McCall | db7bb4a | 2010-03-17 00:38:33 +0000 | [diff] [blame] | 10986 | |
10987 | PopDeclContext(); | ||||
10988 | } | ||||
10989 | |||||
Chris Lattner | df9bcd5 | 2009-04-20 17:29:38 +0000 | [diff] [blame] | 10990 | // Note that FieldName may be null for anonymous bitfields. |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 10991 | ExprResult Sema::VerifyBitField(SourceLocation FieldLoc, |
10992 | IdentifierInfo *FieldName, | ||||
Reid Kleckner | 9a3ecb0 | 2013-07-17 20:46:03 +0000 | [diff] [blame] | 10993 | QualType FieldTy, bool IsMsStruct, |
10994 | Expr *BitWidth, bool *ZeroWidth) { | ||||
Eli Friedman | 1d954f6 | 2009-08-15 21:55:26 +0000 | [diff] [blame] | 10995 | // Default to true; that shouldn't confuse checks for emptiness |
10996 | if (ZeroWidth) | ||||
10997 | *ZeroWidth = true; | ||||
10998 | |||||
Chris Lattner | 2479366 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 10999 | // C99 6.7.2.1p4 - verify the field type. |
Chris Lattner | 8b963ef | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 11000 | // C++ 9.6p3: A bit-field shall have integral or enumeration type. |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 11001 | if (!FieldTy->isDependentType() && !FieldTy->isIntegralOrEnumerationType()) { |
Chris Lattner | 2479366 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 11002 | // Handle incomplete types with specific error. |
Douglas Gregor | a03aca8 | 2009-03-10 21:58:27 +0000 | [diff] [blame] | 11003 | if (RequireCompleteType(FieldLoc, FieldTy, diag::err_field_incomplete)) |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 11004 | return ExprError(); |
Chris Lattner | df9bcd5 | 2009-04-20 17:29:38 +0000 | [diff] [blame] | 11005 | if (FieldName) |
11006 | return Diag(FieldLoc, diag::err_not_integral_type_bitfield) | ||||
11007 | << FieldName << FieldTy << BitWidth->getSourceRange(); | ||||
11008 | return Diag(FieldLoc, diag::err_not_integral_type_anon_bitfield) | ||||
11009 | << FieldTy << BitWidth->getSourceRange(); | ||||
Douglas Gregor | e186269 | 2010-12-15 23:18:36 +0000 | [diff] [blame] | 11010 | } else if (DiagnoseUnexpandedParameterPack(const_cast<Expr *>(BitWidth), |
11011 | UPPC_BitFieldWidth)) | ||||
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 11012 | return ExprError(); |
Douglas Gregor | 3cf538d | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 11013 | |
11014 | // If the bit-width is type- or value-dependent, don't try to check | ||||
11015 | // it now. | ||||
11016 | if (BitWidth->isValueDependent() || BitWidth->isTypeDependent()) | ||||
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 11017 | return Owned(BitWidth); |
Douglas Gregor | 3cf538d | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 11018 | |
Anders Carlsson | 9f1e572 | 2008-12-06 20:33:04 +0000 | [diff] [blame] | 11019 | llvm::APSInt Value; |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 11020 | ExprResult ICE = VerifyIntegerConstantExpression(BitWidth, &Value); |
11021 | if (ICE.isInvalid()) | ||||
11022 | return ICE; | ||||
11023 | BitWidth = ICE.take(); | ||||
Anders Carlsson | 9f1e572 | 2008-12-06 20:33:04 +0000 | [diff] [blame] | 11024 | |
Eli Friedman | 1d954f6 | 2009-08-15 21:55:26 +0000 | [diff] [blame] | 11025 | if (Value != 0 && ZeroWidth) |
11026 | *ZeroWidth = false; | ||||
11027 | |||||
Chris Lattner | cd08707 | 2008-12-12 04:56:04 +0000 | [diff] [blame] | 11028 | // Zero-width bitfield is ok for anonymous field. |
11029 | if (Value == 0 && FieldName) | ||||
11030 | return Diag(FieldLoc, diag::err_bitfield_has_zero_width) << FieldName; | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11031 | |
Chris Lattner | df9bcd5 | 2009-04-20 17:29:38 +0000 | [diff] [blame] | 11032 | if (Value.isSigned() && Value.isNegative()) { |
11033 | if (FieldName) | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11034 | return Diag(FieldLoc, diag::err_bitfield_has_negative_width) |
Chris Lattner | df9bcd5 | 2009-04-20 17:29:38 +0000 | [diff] [blame] | 11035 | << FieldName << Value.toString(10); |
11036 | return Diag(FieldLoc, diag::err_anon_bitfield_has_negative_width) | ||||
11037 | << Value.toString(10); | ||||
11038 | } | ||||
Anders Carlsson | 9f1e572 | 2008-12-06 20:33:04 +0000 | [diff] [blame] | 11039 | |
Douglas Gregor | 3cf538d | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 11040 | if (!FieldTy->isDependentType()) { |
11041 | uint64_t TypeSize = Context.getTypeSize(FieldTy); | ||||
Chris Lattner | df9bcd5 | 2009-04-20 17:29:38 +0000 | [diff] [blame] | 11042 | if (Value.getZExtValue() > TypeSize) { |
Reid Kleckner | 9a3ecb0 | 2013-07-17 20:46:03 +0000 | [diff] [blame] | 11043 | if (!getLangOpts().CPlusPlus || IsMsStruct) { |
Anders Carlsson | 72468ec | 2010-04-16 15:16:32 +0000 | [diff] [blame] | 11044 | if (FieldName) |
11045 | return Diag(FieldLoc, diag::err_bitfield_width_exceeds_type_size) | ||||
11046 | << FieldName << (unsigned)Value.getZExtValue() | ||||
11047 | << (unsigned)TypeSize; | ||||
11048 | |||||
11049 | return Diag(FieldLoc, diag::err_anon_bitfield_width_exceeds_type_size) | ||||
11050 | << (unsigned)Value.getZExtValue() << (unsigned)TypeSize; | ||||
11051 | } | ||||
11052 | |||||
Chris Lattner | df9bcd5 | 2009-04-20 17:29:38 +0000 | [diff] [blame] | 11053 | if (FieldName) |
Anders Carlsson | 72468ec | 2010-04-16 15:16:32 +0000 | [diff] [blame] | 11054 | Diag(FieldLoc, diag::warn_bitfield_width_exceeds_type_size) |
11055 | << FieldName << (unsigned)Value.getZExtValue() | ||||
11056 | << (unsigned)TypeSize; | ||||
11057 | else | ||||
11058 | Diag(FieldLoc, diag::warn_anon_bitfield_width_exceeds_type_size) | ||||
11059 | << (unsigned)Value.getZExtValue() << (unsigned)TypeSize; | ||||
Chris Lattner | df9bcd5 | 2009-04-20 17:29:38 +0000 | [diff] [blame] | 11060 | } |
Douglas Gregor | 3cf538d | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 11061 | } |
Anders Carlsson | 9f1e572 | 2008-12-06 20:33:04 +0000 | [diff] [blame] | 11062 | |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 11063 | return Owned(BitWidth); |
Anders Carlsson | 9f1e572 | 2008-12-06 20:33:04 +0000 | [diff] [blame] | 11064 | } |
11065 | |||||
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 11066 | /// 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] | 11067 | /// to create a FieldDecl object for it. |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 11068 | Decl *Sema::ActOnField(Scope *S, Decl *TagD, SourceLocation DeclStart, |
Richard Trieu | f81e5a9 | 2011-09-09 02:00:50 +0000 | [diff] [blame] | 11069 | Declarator &D, Expr *BitfieldWidth) { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11070 | FieldDecl *Res = HandleField(S, cast_or_null<RecordDecl>(TagD), |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 11071 | DeclStart, D, static_cast<Expr*>(BitfieldWidth), |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 11072 | /*InitStyle=*/ICIS_NoInit, AS_public); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11073 | return Res; |
Chris Lattner | 2479366 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 11074 | } |
11075 | |||||
11076 | /// HandleField - Analyze a field of a C struct or a C++ data member. | ||||
11077 | /// | ||||
11078 | FieldDecl *Sema::HandleField(Scope *S, RecordDecl *Record, | ||||
11079 | SourceLocation DeclStart, | ||||
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 11080 | Declarator &D, Expr *BitWidth, |
11081 | InClassInitStyle InitStyle, | ||||
Douglas Gregor | 4dd55f5 | 2009-03-11 20:50:30 +0000 | [diff] [blame] | 11082 | AccessSpecifier AS) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 11083 | IdentifierInfo *II = D.getIdentifier(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 11084 | SourceLocation Loc = DeclStart; |
11085 | if (II) Loc = D.getIdentifierLoc(); | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11086 | |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 11087 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
11088 | QualType T = TInfo->getType(); | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 11089 | if (getLangOpts().CPlusPlus) { |
Douglas Gregor | 3cf538d | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 11090 | CheckExtraCXXDefaultArguments(D); |
Douglas Gregor | 021c3b3 | 2009-03-11 23:00:04 +0000 | [diff] [blame] | 11091 | |
Douglas Gregor | e186269 | 2010-12-15 23:18:36 +0000 | [diff] [blame] | 11092 | if (DiagnoseUnexpandedParameterPack(D.getIdentifierLoc(), TInfo, |
11093 | UPPC_DataMemberType)) { | ||||
11094 | D.setInvalidType(); | ||||
11095 | T = Context.IntTy; | ||||
11096 | TInfo = Context.getTrivialTypeSourceInfo(T, Loc); | ||||
11097 | } | ||||
11098 | } | ||||
11099 | |||||
Matt Arsenault | 34b0adb | 2013-02-26 21:16:00 +0000 | [diff] [blame] | 11100 | // TR 18037 does not allow fields to be declared with address spaces. |
11101 | if (T.getQualifiers().hasAddressSpace()) { | ||||
11102 | Diag(Loc, diag::err_field_with_address_space); | ||||
11103 | D.setInvalidType(); | ||||
11104 | } | ||||
11105 | |||||
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 11106 | // OpenCL 1.2 spec, s6.9 r: |
11107 | // The event type cannot be used to declare a structure or union field. | ||||
11108 | if (LangOpts.OpenCL && T->isEventT()) { | ||||
11109 | Diag(Loc, diag::err_event_t_struct_field); | ||||
11110 | D.setInvalidType(); | ||||
11111 | } | ||||
11112 | |||||
Richard Smith | c7f8116 | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 11113 | DiagnoseFunctionSpecifiers(D.getDeclSpec()); |
Eli Friedman | 85a5319 | 2009-04-07 19:37:57 +0000 | [diff] [blame] | 11114 | |
Richard Smith | ec64244 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 11115 | if (DeclSpec::TSCS TSCS = D.getDeclSpec().getThreadStorageClassSpec()) |
11116 | Diag(D.getDeclSpec().getThreadStorageClassSpecLoc(), | ||||
11117 | diag::err_invalid_thread) | ||||
11118 | << DeclSpec::getSpecifierName(TSCS); | ||||
Matt Arsenault | 34b0adb | 2013-02-26 21:16:00 +0000 | [diff] [blame] | 11119 | |
Douglas Gregor | 7f6ff02 | 2010-08-30 14:32:14 +0000 | [diff] [blame] | 11120 | // Check to see if this name was declared as a member previously |
Douglas Gregor | 95e5510 | 2011-10-21 15:47:52 +0000 | [diff] [blame] | 11121 | NamedDecl *PrevDecl = 0; |
Douglas Gregor | 7f6ff02 | 2010-08-30 14:32:14 +0000 | [diff] [blame] | 11122 | LookupResult Previous(*this, II, Loc, LookupMemberName, ForRedeclaration); |
11123 | LookupName(Previous, S); | ||||
Douglas Gregor | 95e5510 | 2011-10-21 15:47:52 +0000 | [diff] [blame] | 11124 | switch (Previous.getResultKind()) { |
11125 | case LookupResult::Found: | ||||
11126 | case LookupResult::FoundUnresolvedValue: | ||||
11127 | PrevDecl = Previous.getAsSingle<NamedDecl>(); | ||||
11128 | break; | ||||
11129 | |||||
11130 | case LookupResult::FoundOverloaded: | ||||
11131 | PrevDecl = Previous.getRepresentativeDecl(); | ||||
11132 | break; | ||||
11133 | |||||
11134 | case LookupResult::NotFound: | ||||
11135 | case LookupResult::NotFoundInCurrentInstantiation: | ||||
11136 | case LookupResult::Ambiguous: | ||||
11137 | break; | ||||
11138 | } | ||||
11139 | Previous.suppressDiagnostics(); | ||||
Douglas Gregor | c19ee3e | 2009-06-17 23:37:01 +0000 | [diff] [blame] | 11140 | |
11141 | if (PrevDecl && PrevDecl->isTemplateParameter()) { | ||||
11142 | // Maybe we will complain about the shadowed template parameter. | ||||
11143 | DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), PrevDecl); | ||||
11144 | // Just pretend that we didn't see the previous declaration. | ||||
11145 | PrevDecl = 0; | ||||
11146 | } | ||||
11147 | |||||
Douglas Gregor | 3cf538d | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 11148 | if (PrevDecl && !isDeclInScope(PrevDecl, Record, S)) |
11149 | PrevDecl = 0; | ||||
11150 | |||||
Steve Naroff | ea218b8 | 2009-07-14 14:58:18 +0000 | [diff] [blame] | 11151 | bool Mutable |
11152 | = (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_mutable); | ||||
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 11153 | SourceLocation TSSL = D.getLocStart(); |
Steve Naroff | ea218b8 | 2009-07-14 14:58:18 +0000 | [diff] [blame] | 11154 | FieldDecl *NewFD |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 11155 | = CheckFieldDecl(II, T, TInfo, Record, Loc, Mutable, BitWidth, InitStyle, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 11156 | TSSL, AS, PrevDecl, &D); |
Rafael Espindola | 0162070 | 2010-03-21 22:56:43 +0000 | [diff] [blame] | 11157 | |
11158 | if (NewFD->isInvalidDecl()) | ||||
11159 | Record->setInvalidDecl(); | ||||
11160 | |||||
Douglas Gregor | 591dc84 | 2011-09-12 16:11:24 +0000 | [diff] [blame] | 11161 | if (D.getDeclSpec().isModulePrivateSpecified()) |
11162 | NewFD->setModulePrivate(); | ||||
11163 | |||||
Douglas Gregor | 3cf538d | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 11164 | if (NewFD->isInvalidDecl() && PrevDecl) { |
11165 | // Don't introduce NewFD into scope; there's already something | ||||
11166 | // with the same name in the same scope. | ||||
11167 | } else if (II) { | ||||
11168 | PushOnScopeChains(NewFD, S); | ||||
11169 | } else | ||||
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 11170 | Record->addDecl(NewFD); |
Douglas Gregor | 3cf538d | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 11171 | |
11172 | return NewFD; | ||||
11173 | } | ||||
11174 | |||||
11175 | /// \brief Build a new FieldDecl and check its well-formedness. | ||||
11176 | /// | ||||
11177 | /// This routine builds a new FieldDecl given the fields name, type, | ||||
11178 | /// record, etc. \p PrevDecl should refer to any previous declaration | ||||
11179 | /// with the same name and in the same scope as the field to be | ||||
11180 | /// created. | ||||
11181 | /// | ||||
11182 | /// \returns a new FieldDecl. | ||||
11183 | /// | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11184 | /// \todo The Declarator argument is a hack. It will be removed once |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 11185 | FieldDecl *Sema::CheckFieldDecl(DeclarationName Name, QualType T, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 11186 | TypeSourceInfo *TInfo, |
Douglas Gregor | 3cf538d | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 11187 | RecordDecl *Record, SourceLocation Loc, |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 11188 | bool Mutable, Expr *BitWidth, |
11189 | InClassInitStyle InitStyle, | ||||
Steve Naroff | ea218b8 | 2009-07-14 14:58:18 +0000 | [diff] [blame] | 11190 | SourceLocation TSSL, |
Douglas Gregor | 4dd55f5 | 2009-03-11 20:50:30 +0000 | [diff] [blame] | 11191 | AccessSpecifier AS, NamedDecl *PrevDecl, |
Douglas Gregor | 3cf538d | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 11192 | Declarator *D) { |
11193 | IdentifierInfo *II = Name.getAsIdentifierInfo(); | ||||
Steve Naroff | 5912a35 | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 11194 | bool InvalidDecl = false; |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 11195 | if (D) InvalidDecl = D->isInvalidType(); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 11196 | |
Douglas Gregor | 3cf538d | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 11197 | // If we receive a broken type, recover by assuming 'int' and |
11198 | // marking this declaration as invalid. | ||||
11199 | if (T.isNull()) { | ||||
11200 | InvalidDecl = true; | ||||
11201 | T = Context.IntTy; | ||||
11202 | } | ||||
11203 | |||||
Eli Friedman | 721e77d | 2009-12-07 00:22:08 +0000 | [diff] [blame] | 11204 | QualType EltTy = Context.getBaseElementType(T); |
Argyrios Kyrtzidis | 216f78b | 2012-03-09 20:10:30 +0000 | [diff] [blame] | 11205 | if (!EltTy->isDependentType()) { |
11206 | if (RequireCompleteType(Loc, EltTy, diag::err_field_incomplete)) { | ||||
11207 | // Fields of incomplete type force their record to be invalid. | ||||
11208 | Record->setInvalidDecl(); | ||||
11209 | InvalidDecl = true; | ||||
11210 | } else { | ||||
11211 | NamedDecl *Def; | ||||
11212 | EltTy->isIncompleteType(&Def); | ||||
11213 | if (Def && Def->isInvalidDecl()) { | ||||
11214 | Record->setInvalidDecl(); | ||||
11215 | InvalidDecl = true; | ||||
11216 | } | ||||
11217 | } | ||||
John McCall | 2d7d2d9 | 2010-08-16 23:42:35 +0000 | [diff] [blame] | 11218 | } |
Eli Friedman | 721e77d | 2009-12-07 00:22:08 +0000 | [diff] [blame] | 11219 | |
Joey Gouly | 617bb31 | 2013-01-17 17:35:00 +0000 | [diff] [blame] | 11220 | // OpenCL v1.2 s6.9.c: bitfields are not supported. |
11221 | if (BitWidth && getLangOpts().OpenCL) { | ||||
11222 | Diag(Loc, diag::err_opencl_bitfields); | ||||
11223 | InvalidDecl = true; | ||||
11224 | } | ||||
11225 | |||||
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 11226 | // C99 6.7.2.1p8: A member of a structure or union may have any type other |
11227 | // than a variably modified type. | ||||
Eli Friedman | 721e77d | 2009-12-07 00:22:08 +0000 | [diff] [blame] | 11228 | if (!InvalidDecl && T->isVariablyModifiedType()) { |
Eli Friedman | 1ca4813 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 11229 | bool SizeIsNegative; |
Douglas Gregor | 2767ce2 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 11230 | llvm::APSInt Oversized; |
Abramo Bagnara | 4c5750e | 2012-11-08 14:44:42 +0000 | [diff] [blame] | 11231 | |
11232 | TypeSourceInfo *FixedTInfo = | ||||
11233 | TryToFixInvalidVariablyModifiedTypeSourceInfo(TInfo, Context, | ||||
11234 | SizeIsNegative, | ||||
11235 | Oversized); | ||||
11236 | if (FixedTInfo) { | ||||
Eli Friedman | 1ca4813 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 11237 | Diag(Loc, diag::warn_illegal_constant_array_size); |
Abramo Bagnara | 4c5750e | 2012-11-08 14:44:42 +0000 | [diff] [blame] | 11238 | TInfo = FixedTInfo; |
11239 | T = FixedTInfo->getType(); | ||||
Eli Friedman | 1ca4813 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 11240 | } else { |
11241 | if (SizeIsNegative) | ||||
11242 | Diag(Loc, diag::err_typecheck_negative_array_size); | ||||
Douglas Gregor | 2767ce2 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 11243 | else if (Oversized.getBoolValue()) |
11244 | Diag(Loc, diag::err_array_too_large) | ||||
11245 | << Oversized.toString(10); | ||||
Eli Friedman | 1ca4813 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 11246 | else |
11247 | Diag(Loc, diag::err_typecheck_field_variable_size); | ||||
Eli Friedman | 1ca4813 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 11248 | InvalidDecl = true; |
11249 | } | ||||
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 11250 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11251 | |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 11252 | // Fields can not have abstract class types |
Eli Friedman | 721e77d | 2009-12-07 00:22:08 +0000 | [diff] [blame] | 11253 | if (!InvalidDecl && RequireNonAbstractType(Loc, T, |
11254 | diag::err_abstract_type_in_decl, | ||||
11255 | AbstractFieldType)) | ||||
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 11256 | InvalidDecl = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11257 | |
Eli Friedman | 1d954f6 | 2009-08-15 21:55:26 +0000 | [diff] [blame] | 11258 | bool ZeroWidth = false; |
Douglas Gregor | 3cf538d | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 11259 | // If this is declared as a bit-field, check the bit-field. |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 11260 | if (!InvalidDecl && BitWidth) { |
Reid Kleckner | 9a3ecb0 | 2013-07-17 20:46:03 +0000 | [diff] [blame] | 11261 | BitWidth = VerifyBitField(Loc, II, T, Record->isMsStruct(Context), BitWidth, |
11262 | &ZeroWidth).take(); | ||||
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 11263 | if (!BitWidth) { |
11264 | InvalidDecl = true; | ||||
11265 | BitWidth = 0; | ||||
11266 | ZeroWidth = false; | ||||
11267 | } | ||||
Anders Carlsson | 9f1e572 | 2008-12-06 20:33:04 +0000 | [diff] [blame] | 11268 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11269 | |
John McCall | 4bde1e1 | 2010-06-04 08:34:12 +0000 | [diff] [blame] | 11270 | // Check that 'mutable' is consistent with the type of the declaration. |
11271 | if (!InvalidDecl && Mutable) { | ||||
11272 | unsigned DiagID = 0; | ||||
11273 | if (T->isReferenceType()) | ||||
11274 | DiagID = diag::err_mutable_reference; | ||||
11275 | else if (T.isConstQualified()) | ||||
11276 | DiagID = diag::err_mutable_const; | ||||
11277 | |||||
11278 | if (DiagID) { | ||||
11279 | SourceLocation ErrLoc = Loc; | ||||
11280 | if (D && D->getDeclSpec().getStorageClassSpecLoc().isValid()) | ||||
11281 | ErrLoc = D->getDeclSpec().getStorageClassSpecLoc(); | ||||
11282 | Diag(ErrLoc, DiagID); | ||||
11283 | Mutable = false; | ||||
11284 | InvalidDecl = true; | ||||
11285 | } | ||||
11286 | } | ||||
11287 | |||||
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 11288 | FieldDecl *NewFD = FieldDecl::Create(Context, Record, TSSL, Loc, II, T, TInfo, |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 11289 | BitWidth, Mutable, InitStyle); |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 11290 | if (InvalidDecl) |
11291 | NewFD->setInvalidDecl(); | ||||
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 11292 | |
Douglas Gregor | 3cf538d | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 11293 | if (PrevDecl && !isa<TagDecl>(PrevDecl)) { |
11294 | Diag(Loc, diag::err_duplicate_member) << II; | ||||
11295 | Diag(PrevDecl->getLocation(), diag::note_previous_declaration); | ||||
11296 | NewFD->setInvalidDecl(); | ||||
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 11297 | } |
11298 | |||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 11299 | if (!InvalidDecl && getLangOpts().CPlusPlus) { |
Anders Carlsson | dfdfc58 | 2010-11-07 19:13:55 +0000 | [diff] [blame] | 11300 | if (Record->isUnion()) { |
11301 | if (const RecordType *RT = EltTy->getAs<RecordType>()) { | ||||
11302 | CXXRecordDecl* RDecl = cast<CXXRecordDecl>(RT->getDecl()); | ||||
11303 | if (RDecl->getDefinition()) { | ||||
11304 | // C++ [class.union]p1: An object of a class with a non-trivial | ||||
11305 | // constructor, a non-trivial copy constructor, a non-trivial | ||||
11306 | // destructor, or a non-trivial copy assignment operator | ||||
11307 | // cannot be a member of a union, nor can an array of such | ||||
11308 | // objects. | ||||
Richard Smith | e7d7c39 | 2011-10-19 20:41:51 +0000 | [diff] [blame] | 11309 | if (CheckNontrivialField(NewFD)) |
Anders Carlsson | dfdfc58 | 2010-11-07 19:13:55 +0000 | [diff] [blame] | 11310 | NewFD->setInvalidDecl(); |
11311 | } | ||||
11312 | } | ||||
11313 | |||||
11314 | // 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] | 11315 | // the program is ill-formed, except when compiling with MSVC extensions |
11316 | // enabled. | ||||
Anders Carlsson | dfdfc58 | 2010-11-07 19:13:55 +0000 | [diff] [blame] | 11317 | if (EltTy->isReferenceType()) { |
Aaron Ballman | 76eed42 | 2013-05-30 16:20:00 +0000 | [diff] [blame] | 11318 | Diag(NewFD->getLocation(), getLangOpts().MicrosoftExt ? |
11319 | diag::ext_union_member_of_reference_type : | ||||
11320 | diag::err_union_member_of_reference_type) | ||||
Anders Carlsson | dfdfc58 | 2010-11-07 19:13:55 +0000 | [diff] [blame] | 11321 | << NewFD->getDeclName() << EltTy; |
Aaron Ballman | 76eed42 | 2013-05-30 16:20:00 +0000 | [diff] [blame] | 11322 | if (!getLangOpts().MicrosoftExt) |
11323 | NewFD->setInvalidDecl(); | ||||
Douglas Gregor | 1f2023a | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 11324 | } |
11325 | } | ||||
11326 | } | ||||
11327 | |||||
Douglas Gregor | 3cf538d | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 11328 | // FIXME: We need to pass in the attributes given an AST |
11329 | // representation, not a parser representation. | ||||
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 11330 | if (D) { |
Douglas Gregor | 92eb7d8 | 2013-05-02 23:25:32 +0000 | [diff] [blame] | 11331 | // FIXME: The current scope is almost... but not entirely... correct here. |
11332 | ProcessDeclAttributes(getCurScope(), NewFD, *D); | ||||
Douglas Gregor | 3cf538d | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 11333 | |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 11334 | if (NewFD->hasAttrs()) |
11335 | CheckAlignasUnderalignment(NewFD); | ||||
11336 | } | ||||
11337 | |||||
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 11338 | // In auto-retain/release, infer strong retension for fields of |
11339 | // retainable type. | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 11340 | if (getLangOpts().ObjCAutoRefCount && inferObjCARCLifetime(NewFD)) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 11341 | NewFD->setInvalidDecl(); |
11342 | |||||
Fariborz Jahanian | f6123ca | 2009-02-19 00:22:47 +0000 | [diff] [blame] | 11343 | if (T.isObjCGCWeak()) |
Fariborz Jahanian | ed7e9ef | 2009-02-18 18:14:41 +0000 | [diff] [blame] | 11344 | Diag(Loc, diag::warn_attribute_weak_on_field); |
Anders Carlsson | ad14806 | 2008-02-16 00:29:18 +0000 | [diff] [blame] | 11345 | |
Douglas Gregor | 4dd55f5 | 2009-03-11 20:50:30 +0000 | [diff] [blame] | 11346 | NewFD->setAccess(AS); |
Steve Naroff | 5912a35 | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 11347 | return NewFD; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 11348 | } |
11349 | |||||
Argyrios Kyrtzidis | dd7744d | 2010-08-16 17:27:08 +0000 | [diff] [blame] | 11350 | bool Sema::CheckNontrivialField(FieldDecl *FD) { |
11351 | assert(FD); | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 11352 | assert(getLangOpts().CPlusPlus && "valid check only for C++"); |
Argyrios Kyrtzidis | dd7744d | 2010-08-16 17:27:08 +0000 | [diff] [blame] | 11353 | |
Nick Lewycky | dccd04d | 2013-06-25 23:22:23 +0000 | [diff] [blame] | 11354 | if (FD->isInvalidDecl() || FD->getType()->isDependentType()) |
11355 | return false; | ||||
Argyrios Kyrtzidis | dd7744d | 2010-08-16 17:27:08 +0000 | [diff] [blame] | 11356 | |
11357 | QualType EltTy = Context.getBaseElementType(FD->getType()); | ||||
11358 | if (const RecordType *RT = EltTy->getAs<RecordType>()) { | ||||
Richard Smith | ac71351 | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 11359 | CXXRecordDecl *RDecl = cast<CXXRecordDecl>(RT->getDecl()); |
Argyrios Kyrtzidis | dd7744d | 2010-08-16 17:27:08 +0000 | [diff] [blame] | 11360 | if (RDecl->getDefinition()) { |
11361 | // We check for copy constructors before constructors | ||||
11362 | // because otherwise we'll never get complaints about | ||||
11363 | // copy constructors. | ||||
11364 | |||||
11365 | CXXSpecialMember member = CXXInvalid; | ||||
Richard Smith | 426391c | 2012-11-16 00:53:38 +0000 | [diff] [blame] | 11366 | // We're required to check for any non-trivial constructors. Since the |
11367 | // implicit default constructor is suppressed if there are any | ||||
11368 | // user-declared constructors, we just need to check that there is a | ||||
11369 | // trivial default constructor and a trivial copy constructor. (We don't | ||||
11370 | // worry about move constructors here, since this is a C++98 check.) | ||||
11371 | if (RDecl->hasNonTrivialCopyConstructor()) | ||||
Argyrios Kyrtzidis | dd7744d | 2010-08-16 17:27:08 +0000 | [diff] [blame] | 11372 | member = CXXCopyConstructor; |
Sean Hunt | 023df37 | 2011-05-09 18:22:59 +0000 | [diff] [blame] | 11373 | else if (!RDecl->hasTrivialDefaultConstructor()) |
Sean Hunt | f961ea5 | 2011-05-10 19:08:14 +0000 | [diff] [blame] | 11374 | member = CXXDefaultConstructor; |
Richard Smith | 426391c | 2012-11-16 00:53:38 +0000 | [diff] [blame] | 11375 | else if (RDecl->hasNonTrivialCopyAssignment()) |
Argyrios Kyrtzidis | dd7744d | 2010-08-16 17:27:08 +0000 | [diff] [blame] | 11376 | member = CXXCopyAssignment; |
Richard Smith | 426391c | 2012-11-16 00:53:38 +0000 | [diff] [blame] | 11377 | else if (RDecl->hasNonTrivialDestructor()) |
Argyrios Kyrtzidis | dd7744d | 2010-08-16 17:27:08 +0000 | [diff] [blame] | 11378 | member = CXXDestructor; |
11379 | |||||
11380 | if (member != CXXInvalid) { | ||||
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 11381 | if (!getLangOpts().CPlusPlus11 && |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 11382 | getLangOpts().ObjCAutoRefCount && RDecl->hasObjectMember()) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 11383 | // Objective-C++ ARC: it is an error to have a non-trivial field of |
11384 | // a union. However, system headers in Objective-C programs | ||||
11385 | // occasionally have Objective-C lifetime objects within unions, | ||||
11386 | // and rather than cause the program to fail, we make those | ||||
11387 | // members unavailable. | ||||
11388 | SourceLocation Loc = FD->getLocation(); | ||||
11389 | if (getSourceManager().isInSystemHeader(Loc)) { | ||||
11390 | if (!FD->hasAttr<UnavailableAttr>()) | ||||
11391 | FD->addAttr(new (Context) UnavailableAttr(Loc, Context, | ||||
Argyrios Kyrtzidis | b8b0313 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 11392 | "this system field has retaining ownership")); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 11393 | return false; |
11394 | } | ||||
11395 | } | ||||
Richard Smith | e7d7c39 | 2011-10-19 20:41:51 +0000 | [diff] [blame] | 11396 | |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 11397 | Diag(FD->getLocation(), getLangOpts().CPlusPlus11 ? |
Richard Smith | e7d7c39 | 2011-10-19 20:41:51 +0000 | [diff] [blame] | 11398 | diag::warn_cxx98_compat_nontrivial_union_or_anon_struct_member : |
11399 | diag::err_illegal_union_or_anon_struct_member) | ||||
11400 | << (int)FD->getParent()->isUnion() << FD->getDeclName() << member; | ||||
Richard Smith | ac71351 | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 11401 | DiagnoseNontrivial(RDecl, member); |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 11402 | return !getLangOpts().CPlusPlus11; |
Argyrios Kyrtzidis | dd7744d | 2010-08-16 17:27:08 +0000 | [diff] [blame] | 11403 | } |
11404 | } | ||||
11405 | } | ||||
Richard Smith | ac71351 | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 11406 | |
Argyrios Kyrtzidis | dd7744d | 2010-08-16 17:27:08 +0000 | [diff] [blame] | 11407 | return false; |
11408 | } | ||||
11409 | |||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11410 | /// TranslateIvarVisibility - Translate visibility from a token ID to an |
Fariborz Jahanian | 89204a1 | 2007-10-01 16:53:59 +0000 | [diff] [blame] | 11411 | /// AST enum value. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 11412 | static ObjCIvarDecl::AccessControl |
Fariborz Jahanian | 89204a1 | 2007-10-01 16:53:59 +0000 | [diff] [blame] | 11413 | TranslateIvarVisibility(tok::ObjCKeywordKind ivarVisibility) { |
Steve Naroff | f13271f | 2007-09-14 23:09:53 +0000 | [diff] [blame] | 11414 | switch (ivarVisibility) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 11415 | default: llvm_unreachable("Unknown visitibility kind"); |
Chris Lattner | 33d34a6 | 2008-10-12 00:28:42 +0000 | [diff] [blame] | 11416 | case tok::objc_private: return ObjCIvarDecl::Private; |
11417 | case tok::objc_public: return ObjCIvarDecl::Public; | ||||
11418 | case tok::objc_protected: return ObjCIvarDecl::Protected; | ||||
11419 | case tok::objc_package: return ObjCIvarDecl::Package; | ||||
Steve Naroff | f13271f | 2007-09-14 23:09:53 +0000 | [diff] [blame] | 11420 | } |
11421 | } | ||||
11422 | |||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11423 | /// 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] | 11424 | /// in order to create an IvarDecl object for it. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11425 | Decl *Sema::ActOnIvar(Scope *S, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11426 | SourceLocation DeclStart, |
Richard Trieu | f81e5a9 | 2011-09-09 02:00:50 +0000 | [diff] [blame] | 11427 | Declarator &D, Expr *BitfieldWidth, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 11428 | tok::ObjCKeywordKind Visibility) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11429 | |
Fariborz Jahanian | 1d78cc4 | 2008-04-10 23:32:45 +0000 | [diff] [blame] | 11430 | IdentifierInfo *II = D.getIdentifier(); |
11431 | Expr *BitWidth = (Expr*)BitfieldWidth; | ||||
11432 | SourceLocation Loc = DeclStart; | ||||
11433 | if (II) Loc = D.getIdentifierLoc(); | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11434 | |
Fariborz Jahanian | 1d78cc4 | 2008-04-10 23:32:45 +0000 | [diff] [blame] | 11435 | // FIXME: Unnamed fields can be handled in various different ways, for |
11436 | // example, unnamed unions inject all members into the struct namespace! | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11437 | |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 11438 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
11439 | QualType T = TInfo->getType(); | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11440 | |
Fariborz Jahanian | 1d78cc4 | 2008-04-10 23:32:45 +0000 | [diff] [blame] | 11441 | if (BitWidth) { |
Steve Naroff | 63359c8 | 2009-02-20 17:57:11 +0000 | [diff] [blame] | 11442 | // 6.7.2.1p3, 6.7.2.1p4 |
Reid Kleckner | 9a3ecb0 | 2013-07-17 20:46:03 +0000 | [diff] [blame] | 11443 | BitWidth = |
11444 | VerifyBitField(Loc, II, T, /*IsMsStruct=*/false, BitWidth).take(); | ||||
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 11445 | if (!BitWidth) |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 11446 | D.setInvalidType(); |
Fariborz Jahanian | 1d78cc4 | 2008-04-10 23:32:45 +0000 | [diff] [blame] | 11447 | } else { |
11448 | // Not a bitfield. | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11449 | |
Fariborz Jahanian | 1d78cc4 | 2008-04-10 23:32:45 +0000 | [diff] [blame] | 11450 | // validate II. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11451 | |
Fariborz Jahanian | 1d78cc4 | 2008-04-10 23:32:45 +0000 | [diff] [blame] | 11452 | } |
Fariborz Jahanian | 0b7bc8e | 2010-04-26 22:07:03 +0000 | [diff] [blame] | 11453 | if (T->isReferenceType()) { |
11454 | Diag(Loc, diag::err_ivar_reference_type); | ||||
11455 | D.setInvalidType(); | ||||
11456 | } | ||||
Fariborz Jahanian | 1d78cc4 | 2008-04-10 23:32:45 +0000 | [diff] [blame] | 11457 | // C99 6.7.2.1p8: A member of a structure or union may have any type other |
11458 | // than a variably modified type. | ||||
Fariborz Jahanian | 0b7bc8e | 2010-04-26 22:07:03 +0000 | [diff] [blame] | 11459 | else if (T->isVariablyModifiedType()) { |
Anders Carlsson | 96e05bc | 2008-12-07 00:20:55 +0000 | [diff] [blame] | 11460 | Diag(Loc, diag::err_typecheck_ivar_variable_size); |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 11461 | D.setInvalidType(); |
Fariborz Jahanian | 1d78cc4 | 2008-04-10 23:32:45 +0000 | [diff] [blame] | 11462 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11463 | |
Ted Kremenek | b8db21d | 2008-07-23 18:04:17 +0000 | [diff] [blame] | 11464 | // Get the visibility (access control) for this ivar. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11465 | ObjCIvarDecl::AccessControl ac = |
Ted Kremenek | b8db21d | 2008-07-23 18:04:17 +0000 | [diff] [blame] | 11466 | Visibility != tok::objc_not_keyword ? TranslateIvarVisibility(Visibility) |
11467 | : ObjCIvarDecl::None; | ||||
Fariborz Jahanian | 496b5a8 | 2009-06-05 18:16:35 +0000 | [diff] [blame] | 11468 | // Must set ivar's DeclContext to its enclosing interface. |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 11469 | ObjCContainerDecl *EnclosingDecl = cast<ObjCContainerDecl>(CurContext); |
Fariborz Jahanian | c645ddf | 2012-02-02 00:49:12 +0000 | [diff] [blame] | 11470 | if (!EnclosingDecl || EnclosingDecl->isInvalidDecl()) |
11471 | return 0; | ||||
Daniel Dunbar | a19331f | 2010-04-02 18:29:09 +0000 | [diff] [blame] | 11472 | ObjCContainerDecl *EnclosingContext; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11473 | if (ObjCImplementationDecl *IMPDecl = |
Fariborz Jahanian | 496b5a8 | 2009-06-05 18:16:35 +0000 | [diff] [blame] | 11474 | dyn_cast<ObjCImplementationDecl>(EnclosingDecl)) { |
John McCall | 260611a | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 11475 | if (LangOpts.ObjCRuntime.isFragile()) { |
Fariborz Jahanian | 496b5a8 | 2009-06-05 18:16:35 +0000 | [diff] [blame] | 11476 | // 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] | 11477 | EnclosingContext = IMPDecl->getClassInterface(); |
11478 | assert(EnclosingContext && "Implementation has no class interface!"); | ||||
11479 | } | ||||
11480 | else | ||||
11481 | EnclosingContext = EnclosingDecl; | ||||
Fariborz Jahanian | 0bd0459 | 2010-04-06 22:43:48 +0000 | [diff] [blame] | 11482 | } else { |
11483 | if (ObjCCategoryDecl *CDecl = | ||||
11484 | dyn_cast<ObjCCategoryDecl>(EnclosingDecl)) { | ||||
John McCall | 260611a | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 11485 | if (LangOpts.ObjCRuntime.isFragile() || !CDecl->IsClassExtension()) { |
Fariborz Jahanian | 0bd0459 | 2010-04-06 22:43:48 +0000 | [diff] [blame] | 11486 | Diag(Loc, diag::err_misplaced_ivar) << CDecl->IsClassExtension(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11487 | return 0; |
Fariborz Jahanian | 0bd0459 | 2010-04-06 22:43:48 +0000 | [diff] [blame] | 11488 | } |
11489 | } | ||||
Daniel Dunbar | a19331f | 2010-04-02 18:29:09 +0000 | [diff] [blame] | 11490 | EnclosingContext = EnclosingDecl; |
Fariborz Jahanian | 0bd0459 | 2010-04-06 22:43:48 +0000 | [diff] [blame] | 11491 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11492 | |
Ted Kremenek | b8db21d | 2008-07-23 18:04:17 +0000 | [diff] [blame] | 11493 | // Construct the decl. |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 11494 | ObjCIvarDecl *NewID = ObjCIvarDecl::Create(Context, EnclosingContext, |
11495 | DeclStart, Loc, II, T, | ||||
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 11496 | TInfo, ac, (Expr *)BitfieldWidth); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11497 | |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 11498 | if (II) { |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 11499 | NamedDecl *PrevDecl = LookupSingleName(S, II, Loc, LookupMemberName, |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 11500 | ForRedeclaration); |
Fariborz Jahanian | 496b5a8 | 2009-06-05 18:16:35 +0000 | [diff] [blame] | 11501 | if (PrevDecl && isDeclInScope(PrevDecl, EnclosingContext, S) |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 11502 | && !isa<TagDecl>(PrevDecl)) { |
11503 | Diag(Loc, diag::err_duplicate_member) << II; | ||||
11504 | Diag(PrevDecl->getLocation(), diag::note_previous_declaration); | ||||
11505 | NewID->setInvalidDecl(); | ||||
11506 | } | ||||
11507 | } | ||||
11508 | |||||
Ted Kremenek | b8db21d | 2008-07-23 18:04:17 +0000 | [diff] [blame] | 11509 | // Process attributes attached to the ivar. |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 11510 | ProcessDeclAttributes(S, NewID, D); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11511 | |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 11512 | if (D.isInvalidType()) |
Fariborz Jahanian | 1d78cc4 | 2008-04-10 23:32:45 +0000 | [diff] [blame] | 11513 | NewID->setInvalidDecl(); |
Ted Kremenek | b8db21d | 2008-07-23 18:04:17 +0000 | [diff] [blame] | 11514 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 11515 | // In ARC, infer 'retaining' for ivars of retainable type. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 11516 | if (getLangOpts().ObjCAutoRefCount && inferObjCARCLifetime(NewID)) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 11517 | NewID->setInvalidDecl(); |
11518 | |||||
Douglas Gregor | 591dc84 | 2011-09-12 16:11:24 +0000 | [diff] [blame] | 11519 | if (D.getDeclSpec().isModulePrivateSpecified()) |
11520 | NewID->setModulePrivate(); | ||||
11521 | |||||
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 11522 | if (II) { |
11523 | // FIXME: When interfaces are DeclContexts, we'll need to add | ||||
11524 | // these to the interface. | ||||
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11525 | S->AddDecl(NewID); |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 11526 | IdResolver.AddDecl(NewID); |
11527 | } | ||||
Fariborz Jahanian | 8f674a8 | 2012-05-15 16:33:04 +0000 | [diff] [blame] | 11528 | |
John McCall | 260611a | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 11529 | if (LangOpts.ObjCRuntime.isNonFragile() && |
Fariborz Jahanian | 8f674a8 | 2012-05-15 16:33:04 +0000 | [diff] [blame] | 11530 | !NewID->isInvalidDecl() && isa<ObjCInterfaceDecl>(EnclosingDecl)) |
Fariborz Jahanian | dc3eb6a | 2012-05-15 17:43:16 +0000 | [diff] [blame] | 11531 | Diag(Loc, diag::warn_ivars_in_interface); |
Fariborz Jahanian | 8f674a8 | 2012-05-15 16:33:04 +0000 | [diff] [blame] | 11532 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11533 | return NewID; |
Fariborz Jahanian | 1d78cc4 | 2008-04-10 23:32:45 +0000 | [diff] [blame] | 11534 | } |
11535 | |||||
Fariborz Jahanian | d097be8 | 2010-08-23 22:46:52 +0000 | [diff] [blame] | 11536 | /// ActOnLastBitfield - This routine handles synthesized bitfields rules for |
Jordan Rose | d4582b8 | 2013-04-03 01:39:23 +0000 | [diff] [blame] | 11537 | /// class and class extensions. For every class \@interface and class |
11538 | /// extension \@interface, if the last ivar is a bitfield of any type, | ||||
Fariborz Jahanian | d097be8 | 2010-08-23 22:46:52 +0000 | [diff] [blame] | 11539 | /// 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] | 11540 | void Sema::ActOnLastBitfield(SourceLocation DeclLoc, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 11541 | SmallVectorImpl<Decl *> &AllIvarDecls) { |
John McCall | 260611a | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 11542 | if (LangOpts.ObjCRuntime.isFragile() || AllIvarDecls.empty()) |
Fariborz Jahanian | d097be8 | 2010-08-23 22:46:52 +0000 | [diff] [blame] | 11543 | return; |
11544 | |||||
11545 | Decl *ivarDecl = AllIvarDecls[AllIvarDecls.size()-1]; | ||||
11546 | ObjCIvarDecl *Ivar = cast<ObjCIvarDecl>(ivarDecl); | ||||
11547 | |||||
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 11548 | if (!Ivar->isBitField() || Ivar->getBitWidthValue(Context) == 0) |
Fariborz Jahanian | d097be8 | 2010-08-23 22:46:52 +0000 | [diff] [blame] | 11549 | return; |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 11550 | ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CurContext); |
Fariborz Jahanian | d097be8 | 2010-08-23 22:46:52 +0000 | [diff] [blame] | 11551 | if (!ID) { |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 11552 | if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(CurContext)) { |
Fariborz Jahanian | d097be8 | 2010-08-23 22:46:52 +0000 | [diff] [blame] | 11553 | if (!CD->IsClassExtension()) |
11554 | return; | ||||
11555 | } | ||||
11556 | // No need to add this to end of @implementation. | ||||
11557 | else | ||||
11558 | return; | ||||
11559 | } | ||||
11560 | // 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] | 11561 | llvm::APInt Zero(Context.getTypeSize(Context.IntTy), 0); |
11562 | Expr * BW = IntegerLiteral::Create(Context, Zero, Context.IntTy, DeclLoc); | ||||
Fariborz Jahanian | d097be8 | 2010-08-23 22:46:52 +0000 | [diff] [blame] | 11563 | |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 11564 | Ivar = ObjCIvarDecl::Create(Context, cast<ObjCContainerDecl>(CurContext), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 11565 | DeclLoc, DeclLoc, 0, |
Fariborz Jahanian | d097be8 | 2010-08-23 22:46:52 +0000 | [diff] [blame] | 11566 | Context.CharTy, |
Douglas Gregor | 0bbea1b | 2011-08-03 16:26:46 +0000 | [diff] [blame] | 11567 | Context.getTrivialTypeSourceInfo(Context.CharTy, |
11568 | DeclLoc), | ||||
Fariborz Jahanian | d097be8 | 2010-08-23 22:46:52 +0000 | [diff] [blame] | 11569 | ObjCIvarDecl::Private, BW, |
11570 | true); | ||||
11571 | AllIvarDecls.push_back(Ivar); | ||||
11572 | } | ||||
11573 | |||||
Robert Wilhelm | 834c058 | 2013-08-09 18:02:13 +0000 | [diff] [blame] | 11574 | void Sema::ActOnFields(Scope *S, SourceLocation RecLoc, Decl *EnclosingDecl, |
11575 | ArrayRef<Decl *> Fields, SourceLocation LBrac, | ||||
11576 | SourceLocation RBrac, AttributeList *Attr) { | ||||
Steve Naroff | 7421664 | 2007-09-14 22:20:54 +0000 | [diff] [blame] | 11577 | assert(EnclosingDecl && "missing record or interface decl"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11578 | |
Eric Christopher | 6dba4a1 | 2012-07-19 22:22:51 +0000 | [diff] [blame] | 11579 | // If this is an Objective-C @implementation or category and we have |
11580 | // new fields here we should reset the layout of the interface since | ||||
11581 | // it will now change. | ||||
11582 | if (!Fields.empty() && isa<ObjCContainerDecl>(EnclosingDecl)) { | ||||
11583 | ObjCContainerDecl *DC = cast<ObjCContainerDecl>(EnclosingDecl); | ||||
11584 | switch (DC->getKind()) { | ||||
11585 | default: break; | ||||
11586 | case Decl::ObjCCategory: | ||||
11587 | Context.ResetObjCLayout(cast<ObjCCategoryDecl>(DC)->getClassInterface()); | ||||
11588 | break; | ||||
11589 | case Decl::ObjCImplementation: | ||||
11590 | Context. | ||||
11591 | ResetObjCLayout(cast<ObjCImplementationDecl>(DC)->getClassInterface()); | ||||
11592 | break; | ||||
11593 | } | ||||
11594 | } | ||||
11595 | |||||
Eli Friedman | 11e70d7 | 2012-02-07 05:00:47 +0000 | [diff] [blame] | 11596 | RecordDecl *Record = dyn_cast<RecordDecl>(EnclosingDecl); |
11597 | |||||
11598 | // Start counting up the number of named members; make sure to include | ||||
11599 | // members of anonymous structs and unions in the total. | ||||
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 11600 | unsigned NumNamedMembers = 0; |
Eli Friedman | 11e70d7 | 2012-02-07 05:00:47 +0000 | [diff] [blame] | 11601 | if (Record) { |
11602 | for (RecordDecl::decl_iterator i = Record->decls_begin(), | ||||
11603 | e = Record->decls_end(); i != e; i++) { | ||||
11604 | if (IndirectFieldDecl *IFD = dyn_cast<IndirectFieldDecl>(*i)) | ||||
11605 | if (IFD->getDeclName()) | ||||
11606 | ++NumNamedMembers; | ||||
11607 | } | ||||
11608 | } | ||||
11609 | |||||
11610 | // Verify that all the fields are okay. | ||||
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 11611 | SmallVector<FieldDecl*, 32> RecFields; |
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 11612 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 11613 | bool ARCErrReported = false; |
Robert Wilhelm | 834c058 | 2013-08-09 18:02:13 +0000 | [diff] [blame] | 11614 | for (ArrayRef<Decl *>::iterator i = Fields.begin(), end = Fields.end(); |
David Blaikie | 77b6de0 | 2011-09-22 02:58:26 +0000 | [diff] [blame] | 11615 | i != end; ++i) { |
11616 | FieldDecl *FD = cast<FieldDecl>(*i); | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11617 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 11618 | // Get the type for the field. |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 11619 | const Type *FDTy = FD->getType().getTypePtr(); |
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 11620 | |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 11621 | if (!FD->isAnonymousStructOrUnion()) { |
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 11622 | // Remember all fields written by the user. |
11623 | RecFields.push_back(FD); | ||||
11624 | } | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11625 | |
Chris Lattner | 2479366 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 11626 | // If the field is already invalid for some reason, don't emit more |
11627 | // diagnostics about it. | ||||
Eli Friedman | 721e77d | 2009-12-07 00:22:08 +0000 | [diff] [blame] | 11628 | if (FD->isInvalidDecl()) { |
11629 | EnclosingDecl->setInvalidDecl(); | ||||
Chris Lattner | 2479366 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 11630 | continue; |
Eli Friedman | 721e77d | 2009-12-07 00:22:08 +0000 | [diff] [blame] | 11631 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11632 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 11633 | // C99 6.7.2.1p2: |
11634 | // A structure or union shall not contain a member with | ||||
11635 | // incomplete or function type (hence, a structure shall not | ||||
11636 | // contain an instance of itself, but may contain a pointer to | ||||
11637 | // an instance of itself), except that the last member of a | ||||
11638 | // structure with more than one named member may have incomplete | ||||
11639 | // array type; such a structure (and any union containing, | ||||
11640 | // possibly recursively, a member that is such a structure) | ||||
11641 | // shall not be a member of a structure or an element of an | ||||
11642 | // array. | ||||
Chris Lattner | 02c642e | 2007-07-31 21:33:24 +0000 | [diff] [blame] | 11643 | if (FDTy->isFunctionType()) { |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 11644 | // Field declared as a function. |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 11645 | Diag(FD->getLocation(), diag::err_field_declared_as_function) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 11646 | << FD->getDeclName(); |
Steve Naroff | 7421664 | 2007-09-14 22:20:54 +0000 | [diff] [blame] | 11647 | FD->setInvalidDecl(); |
11648 | EnclosingDecl->setInvalidDecl(); | ||||
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 11649 | continue; |
Francois Pichet | 0924618 | 2010-09-15 00:14:08 +0000 | [diff] [blame] | 11650 | } else if (FDTy->isIncompleteArrayType() && Record && |
David Blaikie | 77b6de0 | 2011-09-22 02:58:26 +0000 | [diff] [blame] | 11651 | ((i + 1 == Fields.end() && !Record->isUnion()) || |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 11652 | ((getLangOpts().MicrosoftExt || |
11653 | getLangOpts().CPlusPlus) && | ||||
David Blaikie | 77b6de0 | 2011-09-22 02:58:26 +0000 | [diff] [blame] | 11654 | (i + 1 == Fields.end() || Record->isUnion())))) { |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 11655 | // Flexible array member. |
Argyrios Kyrtzidis | d97cec3 | 2011-03-07 20:04:04 +0000 | [diff] [blame] | 11656 | // Microsoft and g++ is more permissive regarding flexible array. |
Francois Pichet | 0924618 | 2010-09-15 00:14:08 +0000 | [diff] [blame] | 11657 | // It will accept flexible array in union and also |
Anders Carlsson | 4d09e84 | 2010-10-17 23:36:12 +0000 | [diff] [blame] | 11658 | // as the sole element of a struct/class. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 11659 | if (getLangOpts().MicrosoftExt) { |
Francois Pichet | 0924618 | 2010-09-15 00:14:08 +0000 | [diff] [blame] | 11660 | if (Record->isUnion()) |
Argyrios Kyrtzidis | d97cec3 | 2011-03-07 20:04:04 +0000 | [diff] [blame] | 11661 | Diag(FD->getLocation(), diag::ext_flexible_array_union_ms) |
Francois Pichet | 0924618 | 2010-09-15 00:14:08 +0000 | [diff] [blame] | 11662 | << FD->getDeclName(); |
David Blaikie | 77b6de0 | 2011-09-22 02:58:26 +0000 | [diff] [blame] | 11663 | else if (Fields.size() == 1) |
Argyrios Kyrtzidis | d97cec3 | 2011-03-07 20:04:04 +0000 | [diff] [blame] | 11664 | Diag(FD->getLocation(), diag::ext_flexible_array_empty_aggregate_ms) |
Francois Pichet | 0924618 | 2010-09-15 00:14:08 +0000 | [diff] [blame] | 11665 | << FD->getDeclName() << Record->getTagKind(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 11666 | } else if (getLangOpts().CPlusPlus) { |
Argyrios Kyrtzidis | d97cec3 | 2011-03-07 20:04:04 +0000 | [diff] [blame] | 11667 | if (Record->isUnion()) |
11668 | Diag(FD->getLocation(), diag::ext_flexible_array_union_gnu) | ||||
11669 | << FD->getDeclName(); | ||||
David Blaikie | 77b6de0 | 2011-09-22 02:58:26 +0000 | [diff] [blame] | 11670 | else if (Fields.size() == 1) |
Argyrios Kyrtzidis | d97cec3 | 2011-03-07 20:04:04 +0000 | [diff] [blame] | 11671 | Diag(FD->getLocation(), diag::ext_flexible_array_empty_aggregate_gnu) |
11672 | << FD->getDeclName() << Record->getTagKind(); | ||||
David Chisnall | 0961a01 | 2012-03-16 12:15:37 +0000 | [diff] [blame] | 11673 | } else if (!getLangOpts().C99) { |
11674 | if (Record->isUnion()) | ||||
11675 | Diag(FD->getLocation(), diag::ext_flexible_array_union_gnu) | ||||
11676 | << FD->getDeclName(); | ||||
11677 | else | ||||
11678 | Diag(FD->getLocation(), diag::ext_c99_flexible_array_member) | ||||
11679 | << FD->getDeclName() << Record->getTagKind(); | ||||
Argyrios Kyrtzidis | d97cec3 | 2011-03-07 20:04:04 +0000 | [diff] [blame] | 11680 | } else if (NumNamedMembers < 1) { |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 11681 | Diag(FD->getLocation(), diag::err_flexible_array_empty_struct) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 11682 | << FD->getDeclName(); |
Steve Naroff | 7421664 | 2007-09-14 22:20:54 +0000 | [diff] [blame] | 11683 | FD->setInvalidDecl(); |
11684 | EnclosingDecl->setInvalidDecl(); | ||||
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 11685 | continue; |
11686 | } | ||||
Fariborz Jahanian | 4142ceb | 2010-05-26 20:19:07 +0000 | [diff] [blame] | 11687 | if (!FD->getType()->isDependentType() && |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 11688 | !Context.getBaseElementType(FD->getType()).isPODType(Context)) { |
Fariborz Jahanian | 4142ceb | 2010-05-26 20:19:07 +0000 | [diff] [blame] | 11689 | Diag(FD->getLocation(), diag::err_flexible_array_has_nonpod_type) |
Fariborz Jahanian | 2c0a540 | 2010-05-26 20:46:24 +0000 | [diff] [blame] | 11690 | << FD->getDeclName() << FD->getType(); |
Fariborz Jahanian | 4142ceb | 2010-05-26 20:19:07 +0000 | [diff] [blame] | 11691 | FD->setInvalidDecl(); |
11692 | EnclosingDecl->setInvalidDecl(); | ||||
11693 | continue; | ||||
11694 | } | ||||
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 11695 | // 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] | 11696 | if (Record) |
11697 | Record->setHasFlexibleArrayMember(true); | ||||
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 11698 | } else if (!FDTy->isDependentType() && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11699 | RequireCompleteType(FD->getLocation(), FD->getType(), |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 11700 | diag::err_field_incomplete)) { |
11701 | // Incomplete type | ||||
11702 | FD->setInvalidDecl(); | ||||
11703 | EnclosingDecl->setInvalidDecl(); | ||||
11704 | continue; | ||||
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 11705 | } else if (const RecordType *FDTTy = FDTy->getAs<RecordType>()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 11706 | if (FDTTy->getDecl()->hasFlexibleArrayMember()) { |
11707 | // 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] | 11708 | if (Record && Record->isUnion()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 11709 | Record->setHasFlexibleArrayMember(true); |
11710 | } else { | ||||
11711 | // If this is a struct/class and this is not the last element, reject | ||||
11712 | // it. Note that GCC supports variable sized arrays in the middle of | ||||
11713 | // structures. | ||||
David Blaikie | 77b6de0 | 2011-09-22 02:58:26 +0000 | [diff] [blame] | 11714 | if (i + 1 != Fields.end()) |
Douglas Gregor | e4f3e06 | 2009-03-06 23:41:27 +0000 | [diff] [blame] | 11715 | Diag(FD->getLocation(), diag::ext_variable_sized_type_in_struct) |
Chris Lattner | 740782a | 2009-04-25 18:52:45 +0000 | [diff] [blame] | 11716 | << FD->getDeclName() << FD->getType(); |
Douglas Gregor | e4f3e06 | 2009-03-06 23:41:27 +0000 | [diff] [blame] | 11717 | else { |
11718 | // We support flexible arrays at the end of structs in | ||||
11719 | // other structs as an extension. | ||||
11720 | Diag(FD->getLocation(), diag::ext_flexible_array_in_struct) | ||||
11721 | << FD->getDeclName(); | ||||
11722 | if (Record) | ||||
11723 | Record->setHasFlexibleArrayMember(true); | ||||
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 11724 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 11725 | } |
11726 | } | ||||
Fariborz Jahanian | 7f90b53 | 2012-08-16 22:38:41 +0000 | [diff] [blame] | 11727 | if (isa<ObjCContainerDecl>(EnclosingDecl) && |
11728 | RequireNonAbstractType(FD->getLocation(), FD->getType(), | ||||
11729 | diag::err_abstract_type_in_decl, | ||||
11730 | AbstractIvarType)) { | ||||
11731 | // Ivars can not have abstract class types | ||||
11732 | FD->setInvalidDecl(); | ||||
11733 | } | ||||
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 11734 | if (Record && FDTTy->getDecl()->hasObjectMember()) |
11735 | Record->setHasObjectMember(true); | ||||
Fariborz Jahanian | 3ac83d6 | 2013-01-25 23:57:05 +0000 | [diff] [blame] | 11736 | if (Record && FDTTy->getDecl()->hasVolatileMember()) |
11737 | Record->setHasVolatileMember(true); | ||||
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 11738 | } else if (FDTy->isObjCObjectType()) { |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 11739 | /// A field cannot be an Objective-c object |
Fariborz Jahanian | 8eaefdc | 2011-07-26 17:58:54 +0000 | [diff] [blame] | 11740 | Diag(FD->getLocation(), diag::err_statically_allocated_object) |
11741 | << FixItHint::CreateInsertion(FD->getLocation(), "*"); | ||||
11742 | QualType T = Context.getObjCObjectPointerType(FD->getType()); | ||||
11743 | FD->setType(T); | ||||
Douglas Gregor | 4581d45 | 2013-01-28 19:08:09 +0000 | [diff] [blame] | 11744 | } else if (getLangOpts().ObjCAutoRefCount && Record && !ARCErrReported && |
11745 | (!getLangOpts().CPlusPlus || Record->isUnion())) { | ||||
11746 | // It's an error in ARC if a field has lifetime. | ||||
11747 | // We don't want to report this in a system header, though, | ||||
11748 | // so we just make the field unavailable. | ||||
11749 | // FIXME: that's really not sufficient; we need to make the type | ||||
11750 | // itself invalid to, say, initialize or copy. | ||||
11751 | QualType T = FD->getType(); | ||||
11752 | Qualifiers::ObjCLifetime lifetime = T.getObjCLifetime(); | ||||
11753 | if (lifetime && lifetime != Qualifiers::OCL_ExplicitNone) { | ||||
11754 | SourceLocation loc = FD->getLocation(); | ||||
11755 | if (getSourceManager().isInSystemHeader(loc)) { | ||||
11756 | if (!FD->hasAttr<UnavailableAttr>()) { | ||||
11757 | FD->addAttr(new (Context) UnavailableAttr(loc, Context, | ||||
11758 | "this system field has retaining ownership")); | ||||
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 11759 | } |
Douglas Gregor | 4581d45 | 2013-01-28 19:08:09 +0000 | [diff] [blame] | 11760 | } else { |
11761 | Diag(FD->getLocation(), diag::err_arc_objc_object_in_tag) | ||||
Douglas Gregor | bde67cf | 2013-01-28 20:13:44 +0000 | [diff] [blame] | 11762 | << T->isBlockPointerType() << Record->getTagKind(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 11763 | } |
Douglas Gregor | 4581d45 | 2013-01-28 19:08:09 +0000 | [diff] [blame] | 11764 | ARCErrReported = true; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 11765 | } |
Douglas Gregor | 4581d45 | 2013-01-28 19:08:09 +0000 | [diff] [blame] | 11766 | } else if (getLangOpts().ObjC1 && |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 11767 | getLangOpts().getGC() != LangOptions::NonGC && |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 11768 | Record && !Record->hasObjectMember()) { |
Douglas Gregor | 4581d45 | 2013-01-28 19:08:09 +0000 | [diff] [blame] | 11769 | if (FD->getType()->isObjCObjectPointerType() || |
11770 | FD->getType().isObjCGCStrong()) | ||||
11771 | Record->setHasObjectMember(true); | ||||
11772 | else if (Context.getAsArrayType(FD->getType())) { | ||||
11773 | QualType BaseType = Context.getBaseElementType(FD->getType()); | ||||
11774 | if (BaseType->isRecordType() && | ||||
11775 | BaseType->getAs<RecordType>()->getDecl()->hasObjectMember()) | ||||
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 11776 | Record->setHasObjectMember(true); |
Douglas Gregor | 4581d45 | 2013-01-28 19:08:09 +0000 | [diff] [blame] | 11777 | else if (BaseType->isObjCObjectPointerType() || |
11778 | BaseType.isObjCGCStrong()) | ||||
11779 | Record->setHasObjectMember(true); | ||||
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 11780 | } |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 11781 | } |
Fariborz Jahanian | 3ac83d6 | 2013-01-25 23:57:05 +0000 | [diff] [blame] | 11782 | if (Record && FD->getType().isVolatileQualified()) |
11783 | Record->setHasVolatileMember(true); | ||||
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 11784 | // Keep track of the number of named members. |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 11785 | if (FD->getIdentifier()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 11786 | ++NumNamedMembers; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 11787 | } |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 11788 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 11789 | // Okay, we successfully defined 'Record'. |
Chris Lattner | e1e7985 | 2008-02-06 00:51:33 +0000 | [diff] [blame] | 11790 | if (Record) { |
Douglas Gregor | 7a39dd0 | 2010-09-29 00:15:42 +0000 | [diff] [blame] | 11791 | bool Completed = false; |
11792 | if (CXXRecordDecl *CXXRecord = dyn_cast<CXXRecordDecl>(Record)) { | ||||
11793 | if (!CXXRecord->isInvalidDecl()) { | ||||
11794 | // Set access bits correctly on the directly-declared conversions. | ||||
Argyrios Kyrtzidis | 9d29543 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 11795 | for (CXXRecordDecl::conversion_iterator |
11796 | I = CXXRecord->conversion_begin(), | ||||
11797 | E = CXXRecord->conversion_end(); I != E; ++I) | ||||
11798 | I.setAccess((*I)->getAccess()); | ||||
Douglas Gregor | 7a39dd0 | 2010-09-29 00:15:42 +0000 | [diff] [blame] | 11799 | |
11800 | if (!CXXRecord->isDependentType()) { | ||||
Peter Collingbourne | f51cfb8 | 2013-05-20 14:12:25 +0000 | [diff] [blame] | 11801 | if (CXXRecord->hasUserDeclaredDestructor()) { |
11802 | // Adjust user-defined destructor exception spec. | ||||
11803 | if (getLangOpts().CPlusPlus11) | ||||
11804 | AdjustDestructorExceptionSpec(CXXRecord, | ||||
11805 | CXXRecord->getDestructor()); | ||||
11806 | |||||
11807 | // The Microsoft ABI requires that we perform the destructor body | ||||
11808 | // checks (i.e. operator delete() lookup) at every declaration, as | ||||
11809 | // any translation unit may need to emit a deleting destructor. | ||||
11810 | if (Context.getTargetInfo().getCXXABI().isMicrosoft()) | ||||
11811 | CheckDestructor(CXXRecord->getDestructor()); | ||||
11812 | } | ||||
Sebastian Redl | 0ee3391 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 11813 | |
Douglas Gregor | 7a39dd0 | 2010-09-29 00:15:42 +0000 | [diff] [blame] | 11814 | // Add any implicitly-declared members to this class. |
11815 | AddImplicitlyDeclaredMembersToClass(CXXRecord); | ||||
11816 | |||||
11817 | // If we have virtual base classes, we may end up finding multiple | ||||
11818 | // final overriders for a given virtual function. Check for this | ||||
11819 | // problem now. | ||||
11820 | if (CXXRecord->getNumVBases()) { | ||||
11821 | CXXFinalOverriderMap FinalOverriders; | ||||
11822 | CXXRecord->getFinalOverriders(FinalOverriders); | ||||
11823 | |||||
11824 | for (CXXFinalOverriderMap::iterator M = FinalOverriders.begin(), | ||||
11825 | MEnd = FinalOverriders.end(); | ||||
11826 | M != MEnd; ++M) { | ||||
11827 | for (OverridingMethods::iterator SO = M->second.begin(), | ||||
11828 | SOEnd = M->second.end(); | ||||
11829 | SO != SOEnd; ++SO) { | ||||
11830 | assert(SO->second.size() > 0 && | ||||
11831 | "Virtual function without overridding functions?"); | ||||
11832 | if (SO->second.size() == 1) | ||||
11833 | continue; | ||||
11834 | |||||
11835 | // C++ [class.virtual]p2: | ||||
11836 | // In a derived class, if a virtual member function of a base | ||||
11837 | // class subobject has more than one final overrider the | ||||
11838 | // program is ill-formed. | ||||
11839 | Diag(Record->getLocation(), diag::err_multiple_final_overriders) | ||||
Roman Divacky | 31ba613 | 2012-09-06 15:59:27 +0000 | [diff] [blame] | 11840 | << (const NamedDecl *)M->first << Record; |
Douglas Gregor | 7a39dd0 | 2010-09-29 00:15:42 +0000 | [diff] [blame] | 11841 | Diag(M->first->getLocation(), |
11842 | diag::note_overridden_virtual_function); | ||||
11843 | for (OverridingMethods::overriding_iterator | ||||
11844 | OM = SO->second.begin(), | ||||
11845 | OMEnd = SO->second.end(); | ||||
11846 | OM != OMEnd; ++OM) | ||||
11847 | Diag(OM->Method->getLocation(), diag::note_final_overrider) | ||||
Roman Divacky | 31ba613 | 2012-09-06 15:59:27 +0000 | [diff] [blame] | 11848 | << (const NamedDecl *)M->first << OM->Method->getParent(); |
Douglas Gregor | 7a39dd0 | 2010-09-29 00:15:42 +0000 | [diff] [blame] | 11849 | |
11850 | Record->setInvalidDecl(); | ||||
11851 | } | ||||
11852 | } | ||||
11853 | CXXRecord->completeDefinition(&FinalOverriders); | ||||
11854 | Completed = true; | ||||
11855 | } | ||||
11856 | } | ||||
11857 | } | ||||
11858 | } | ||||
11859 | |||||
11860 | if (!Completed) | ||||
11861 | Record->completeDefinition(); | ||||
Sebastian Redl | 0ee3391 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 11862 | |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 11863 | if (Record->hasAttrs()) |
11864 | CheckAlignasUnderalignment(Record); | ||||
Serge Pavlov | 122e601 | 2013-06-08 13:29:58 +0000 | [diff] [blame] | 11865 | |
11866 | // Check if the structure/union declaration is a language extension. | ||||
11867 | if (!getLangOpts().CPlusPlus) { | ||||
11868 | bool ZeroSize = true; | ||||
Serge Pavlov | 0dcea35 | 2013-06-17 17:18:51 +0000 | [diff] [blame] | 11869 | bool IsEmpty = true; |
11870 | unsigned NonBitFields = 0; | ||||
Serge Pavlov | 122e601 | 2013-06-08 13:29:58 +0000 | [diff] [blame] | 11871 | for (RecordDecl::field_iterator I = Record->field_begin(), |
Serge Pavlov | 0dcea35 | 2013-06-17 17:18:51 +0000 | [diff] [blame] | 11872 | E = Record->field_end(); |
11873 | (NonBitFields == 0 || ZeroSize) && I != E; ++I) { | ||||
11874 | IsEmpty = false; | ||||
Serge Pavlov | 122e601 | 2013-06-08 13:29:58 +0000 | [diff] [blame] | 11875 | if (I->isUnnamedBitfield()) { |
Serge Pavlov | 122e601 | 2013-06-08 13:29:58 +0000 | [diff] [blame] | 11876 | if (I->getBitWidthValue(Context) > 0) |
11877 | ZeroSize = false; | ||||
11878 | } else { | ||||
Serge Pavlov | 0dcea35 | 2013-06-17 17:18:51 +0000 | [diff] [blame] | 11879 | ++NonBitFields; |
11880 | QualType FieldType = I->getType(); | ||||
11881 | if (FieldType->isIncompleteType() || | ||||
11882 | !Context.getTypeSizeInChars(FieldType).isZero()) | ||||
11883 | ZeroSize = false; | ||||
Serge Pavlov | 122e601 | 2013-06-08 13:29:58 +0000 | [diff] [blame] | 11884 | } |
11885 | } | ||||
11886 | |||||
11887 | // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in | ||||
11888 | // C++. | ||||
Serge Pavlov | 0dcea35 | 2013-06-17 17:18:51 +0000 | [diff] [blame] | 11889 | if (ZeroSize) |
11890 | Diag(RecLoc, diag::warn_zero_size_struct_union_compat) << IsEmpty | ||||
11891 | << Record->isUnion() << (NonBitFields > 1); | ||||
Serge Pavlov | 122e601 | 2013-06-08 13:29:58 +0000 | [diff] [blame] | 11892 | |
11893 | // Structs without named members are extension in C (C99 6.7.2.1p7), but | ||||
11894 | // are accepted by GCC. | ||||
Serge Pavlov | 0dcea35 | 2013-06-17 17:18:51 +0000 | [diff] [blame] | 11895 | if (NonBitFields == 0) { |
11896 | if (IsEmpty) | ||||
Serge Pavlov | 122e601 | 2013-06-08 13:29:58 +0000 | [diff] [blame] | 11897 | Diag(RecLoc, diag::ext_empty_struct_union) << Record->isUnion(); |
11898 | else | ||||
11899 | Diag(RecLoc, diag::ext_no_named_members_in_struct_union) << Record->isUnion(); | ||||
11900 | } | ||||
11901 | } | ||||
Chris Lattner | e1e7985 | 2008-02-06 00:51:33 +0000 | [diff] [blame] | 11902 | } else { |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 11903 | ObjCIvarDecl **ClsFields = |
11904 | reinterpret_cast<ObjCIvarDecl**>(RecFields.data()); | ||||
Fariborz Jahanian | 60f8c86 | 2008-12-13 20:28:25 +0000 | [diff] [blame] | 11905 | if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(EnclosingDecl)) { |
Douglas Gregor | 05c272f | 2011-12-15 22:34:59 +0000 | [diff] [blame] | 11906 | ID->setEndOfDefinitionLoc(RBrac); |
Fariborz Jahanian | 496b5a8 | 2009-06-05 18:16:35 +0000 | [diff] [blame] | 11907 | // Add ivar's to class's DeclContext. |
11908 | for (unsigned i = 0, e = RecFields.size(); i != e; ++i) { | ||||
11909 | ClsFields[i]->setLexicalDeclContext(ID); | ||||
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 11910 | ID->addDecl(ClsFields[i]); |
Fariborz Jahanian | 496b5a8 | 2009-06-05 18:16:35 +0000 | [diff] [blame] | 11911 | } |
Fariborz Jahanian | 3281eff | 2008-12-16 01:08:35 +0000 | [diff] [blame] | 11912 | // Must enforce the rule that ivars in the base classes may not be |
11913 | // duplicates. | ||||
Fariborz Jahanian | f914b97 | 2010-02-23 23:41:11 +0000 | [diff] [blame] | 11914 | if (ID->getSuperClass()) |
11915 | DiagnoseDuplicateIvars(ID, ID->getSuperClass()); | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11916 | } else if (ObjCImplementationDecl *IMPDecl = |
Chris Lattner | 1829a6d | 2009-02-23 22:00:08 +0000 | [diff] [blame] | 11917 | dyn_cast<ObjCImplementationDecl>(EnclosingDecl)) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 11918 | assert(IMPDecl && "ActOnFields - missing ObjCImplementationDecl"); |
Fariborz Jahanian | 496b5a8 | 2009-06-05 18:16:35 +0000 | [diff] [blame] | 11919 | for (unsigned I = 0, N = RecFields.size(); I != N; ++I) |
11920 | // Ivar declared in @implementation never belongs to the implementation. | ||||
11921 | // Only it is in implementation's lexical context. | ||||
Douglas Gregor | 8f36aba | 2009-04-23 03:23:08 +0000 | [diff] [blame] | 11922 | ClsFields[I]->setLexicalDeclContext(IMPDecl); |
Fariborz Jahanian | 3a3ca1b | 2007-10-31 18:48:14 +0000 | [diff] [blame] | 11923 | CheckImplementationIvars(IMPDecl, ClsFields, RecFields.size(), RBrac); |
Fariborz Jahanian | af30029 | 2012-02-20 20:09:20 +0000 | [diff] [blame] | 11924 | IMPDecl->setIvarLBraceLoc(LBrac); |
11925 | IMPDecl->setIvarRBraceLoc(RBrac); | ||||
Fariborz Jahanian | 83c481a | 2010-02-22 23:04:20 +0000 | [diff] [blame] | 11926 | } else if (ObjCCategoryDecl *CDecl = |
11927 | dyn_cast<ObjCCategoryDecl>(EnclosingDecl)) { | ||||
Fariborz Jahanian | 0bd0459 | 2010-04-06 22:43:48 +0000 | [diff] [blame] | 11928 | // case of ivars in class extension; all other cases have been |
11929 | // reported as errors elsewhere. | ||||
11930 | // FIXME. Class extension does not have a LocEnd field. | ||||
11931 | // CDecl->setLocEnd(RBrac); | ||||
11932 | // Add ivar's to class extension's DeclContext. | ||||
Fariborz Jahanian | 3ff86f7 | 2011-10-21 18:03:52 +0000 | [diff] [blame] | 11933 | // Diagnose redeclaration of private ivars. |
11934 | ObjCInterfaceDecl *IDecl = CDecl->getClassInterface(); | ||||
Fariborz Jahanian | 0bd0459 | 2010-04-06 22:43:48 +0000 | [diff] [blame] | 11935 | for (unsigned i = 0, e = RecFields.size(); i != e; ++i) { |
Fariborz Jahanian | 3ff86f7 | 2011-10-21 18:03:52 +0000 | [diff] [blame] | 11936 | if (IDecl) { |
11937 | if (const ObjCIvarDecl *ClsIvar = | ||||
11938 | IDecl->getIvarDecl(ClsFields[i]->getIdentifier())) { | ||||
11939 | Diag(ClsFields[i]->getLocation(), | ||||
11940 | diag::err_duplicate_ivar_declaration); | ||||
11941 | Diag(ClsIvar->getLocation(), diag::note_previous_definition); | ||||
11942 | continue; | ||||
11943 | } | ||||
Douglas Gregor | d329724 | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 11944 | for (ObjCInterfaceDecl::known_extensions_iterator |
11945 | Ext = IDecl->known_extensions_begin(), | ||||
11946 | ExtEnd = IDecl->known_extensions_end(); | ||||
11947 | Ext != ExtEnd; ++Ext) { | ||||
11948 | if (const ObjCIvarDecl *ClsExtIvar | ||||
11949 | = Ext->getIvarDecl(ClsFields[i]->getIdentifier())) { | ||||
Fariborz Jahanian | 3ff86f7 | 2011-10-21 18:03:52 +0000 | [diff] [blame] | 11950 | Diag(ClsFields[i]->getLocation(), |
11951 | diag::err_duplicate_ivar_declaration); | ||||
11952 | Diag(ClsExtIvar->getLocation(), diag::note_previous_definition); | ||||
11953 | continue; | ||||
11954 | } | ||||
11955 | } | ||||
11956 | } | ||||
Fariborz Jahanian | 0bd0459 | 2010-04-06 22:43:48 +0000 | [diff] [blame] | 11957 | ClsFields[i]->setLexicalDeclContext(CDecl); |
11958 | CDecl->addDecl(ClsFields[i]); | ||||
Fariborz Jahanian | 83c481a | 2010-02-22 23:04:20 +0000 | [diff] [blame] | 11959 | } |
Fariborz Jahanian | af30029 | 2012-02-20 20:09:20 +0000 | [diff] [blame] | 11960 | CDecl->setIvarLBraceLoc(LBrac); |
11961 | CDecl->setIvarRBraceLoc(RBrac); | ||||
Fariborz Jahanian | d0b90bf | 2007-09-26 18:27:25 +0000 | [diff] [blame] | 11962 | } |
Fariborz Jahanian | b04a021 | 2007-09-14 21:08:27 +0000 | [diff] [blame] | 11963 | } |
Daniel Dunbar | 7d07664 | 2008-10-03 17:33:35 +0000 | [diff] [blame] | 11964 | |
11965 | if (Attr) | ||||
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 11966 | ProcessDeclAttributeList(S, Record, Attr); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 11967 | } |
11968 | |||||
Douglas Gregor | 677e4fe | 2010-02-01 23:36:03 +0000 | [diff] [blame] | 11969 | /// \brief Determine whether the given integral value is representable within |
11970 | /// the given type T. | ||||
11971 | static bool isRepresentableIntegerValue(ASTContext &Context, | ||||
11972 | llvm::APSInt &Value, | ||||
11973 | QualType T) { | ||||
Douglas Gregor | 9d3347a | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 11974 | assert(T->isIntegralType(Context) && "Integral type required!"); |
Douglas Gregor | af68d4e | 2010-04-15 15:53:31 +0000 | [diff] [blame] | 11975 | unsigned BitWidth = Context.getIntWidth(T); |
Douglas Gregor | 677e4fe | 2010-02-01 23:36:03 +0000 | [diff] [blame] | 11976 | |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 11977 | if (Value.isUnsigned() || Value.isNonNegative()) { |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 11978 | if (T->isSignedIntegerOrEnumerationType()) |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 11979 | --BitWidth; |
11980 | return Value.getActiveBits() <= BitWidth; | ||||
11981 | } | ||||
Douglas Gregor | 677e4fe | 2010-02-01 23:36:03 +0000 | [diff] [blame] | 11982 | return Value.getMinSignedBits() <= BitWidth; |
11983 | } | ||||
11984 | |||||
11985 | // \brief Given an integral type, return the next larger integral type | ||||
11986 | // (or a NULL type of no such type exists). | ||||
11987 | static QualType getNextLargerIntegralType(ASTContext &Context, QualType T) { | ||||
11988 | // FIXME: Int128/UInt128 support, which also needs to be introduced into | ||||
11989 | // enum checking below. | ||||
Douglas Gregor | 9d3347a | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 11990 | assert(T->isIntegralType(Context) && "Integral type required!"); |
Douglas Gregor | 677e4fe | 2010-02-01 23:36:03 +0000 | [diff] [blame] | 11991 | const unsigned NumTypes = 4; |
11992 | QualType SignedIntegralTypes[NumTypes] = { | ||||
11993 | Context.ShortTy, Context.IntTy, Context.LongTy, Context.LongLongTy | ||||
11994 | }; | ||||
11995 | QualType UnsignedIntegralTypes[NumTypes] = { | ||||
11996 | Context.UnsignedShortTy, Context.UnsignedIntTy, Context.UnsignedLongTy, | ||||
11997 | Context.UnsignedLongLongTy | ||||
11998 | }; | ||||
11999 | |||||
12000 | unsigned BitWidth = Context.getTypeSize(T); | ||||
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 12001 | QualType *Types = T->isSignedIntegerOrEnumerationType()? SignedIntegralTypes |
12002 | : UnsignedIntegralTypes; | ||||
Douglas Gregor | 677e4fe | 2010-02-01 23:36:03 +0000 | [diff] [blame] | 12003 | for (unsigned I = 0; I != NumTypes; ++I) |
12004 | if (Context.getTypeSize(Types[I]) > BitWidth) | ||||
12005 | return Types[I]; | ||||
12006 | |||||
12007 | return QualType(); | ||||
12008 | } | ||||
12009 | |||||
Douglas Gregor | 879fd49 | 2009-03-17 19:05:46 +0000 | [diff] [blame] | 12010 | EnumConstantDecl *Sema::CheckEnumConstant(EnumDecl *Enum, |
12011 | EnumConstantDecl *LastEnumConst, | ||||
12012 | SourceLocation IdLoc, | ||||
12013 | IdentifierInfo *Id, | ||||
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 12014 | Expr *Val) { |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 12015 | unsigned IntWidth = Context.getTargetInfo().getIntWidth(); |
Douglas Gregor | 677e4fe | 2010-02-01 23:36:03 +0000 | [diff] [blame] | 12016 | llvm::APSInt EnumVal(IntWidth); |
Douglas Gregor | 879fd49 | 2009-03-17 19:05:46 +0000 | [diff] [blame] | 12017 | QualType EltTy; |
Douglas Gregor | 0c9e479 | 2010-12-16 00:24:44 +0000 | [diff] [blame] | 12018 | |
12019 | if (Val && DiagnoseUnexpandedParameterPack(Val, UPPC_EnumeratorValue)) | ||||
12020 | Val = 0; | ||||
12021 | |||||
Eli Friedman | 19efa3e | 2011-12-06 00:10:34 +0000 | [diff] [blame] | 12022 | if (Val) |
12023 | Val = DefaultLvalueConversion(Val).take(); | ||||
12024 | |||||
Douglas Gregor | 4912c34 | 2009-11-06 00:03:12 +0000 | [diff] [blame] | 12025 | if (Val) { |
Douglas Gregor | 9b9edd6 | 2010-03-02 17:53:14 +0000 | [diff] [blame] | 12026 | if (Enum->isDependentType() || Val->isTypeDependent()) |
Douglas Gregor | 4912c34 | 2009-11-06 00:03:12 +0000 | [diff] [blame] | 12027 | EltTy = Context.DependentTy; |
12028 | else { | ||||
Douglas Gregor | 4912c34 | 2009-11-06 00:03:12 +0000 | [diff] [blame] | 12029 | SourceLocation ExpLoc; |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 12030 | if (getLangOpts().CPlusPlus11 && Enum->isFixed() && |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 12031 | !getLangOpts().MicrosoftMode) { |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 12032 | // C++11 [dcl.enum]p5: If the underlying type is fixed, [...] the |
12033 | // constant-expression in the enumerator-definition shall be a converted | ||||
12034 | // constant expression of the underlying type. | ||||
12035 | EltTy = Enum->getIntegerType(); | ||||
12036 | ExprResult Converted = | ||||
12037 | CheckConvertedConstantExpression(Val, EltTy, EnumVal, | ||||
12038 | CCEK_Enumerator); | ||||
12039 | if (Converted.isInvalid()) | ||||
12040 | Val = 0; | ||||
12041 | else | ||||
12042 | Val = Converted.take(); | ||||
12043 | } else if (!Val->isValueDependent() && | ||||
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 12044 | !(Val = VerifyIntegerConstantExpression(Val, |
12045 | &EnumVal).take())) { | ||||
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 12046 | // 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] | 12047 | } else { |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 12048 | if (Enum->isFixed()) { |
12049 | EltTy = Enum->getIntegerType(); | ||||
12050 | |||||
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 12051 | // In Obj-C and Microsoft mode, require the enumeration value to be |
12052 | // representable in the underlying type of the enumeration. In C++11, | ||||
12053 | // we perform a non-narrowing conversion as part of converted constant | ||||
12054 | // expression checking. | ||||
Francois Pichet | 842e7a2 | 2010-10-18 15:01:13 +0000 | [diff] [blame] | 12055 | if (!isRepresentableIntegerValue(Context, EnumVal, EltTy)) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 12056 | if (getLangOpts().MicrosoftMode) { |
Francois Pichet | 842e7a2 | 2010-10-18 15:01:13 +0000 | [diff] [blame] | 12057 | Diag(IdLoc, diag::ext_enumerator_too_large) << EltTy; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 12058 | Val = ImpCastExprToType(Val, EltTy, CK_IntegralCast).take(); |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 12059 | } else |
12060 | Diag(IdLoc, diag::err_enumerator_too_large) << EltTy; | ||||
Francois Pichet | 842e7a2 | 2010-10-18 15:01:13 +0000 | [diff] [blame] | 12061 | } else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 12062 | Val = ImpCastExprToType(Val, EltTy, CK_IntegralCast).take(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 12063 | } else if (getLangOpts().CPlusPlus) { |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 12064 | // C++11 [dcl.enum]p5: |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 12065 | // If the underlying type is not fixed, the type of each enumerator |
12066 | // is the type of its initializing value: | ||||
12067 | // - If an initializer is specified for an enumerator, the | ||||
12068 | // initializing value has the same type as the expression. | ||||
12069 | EltTy = Val->getType(); | ||||
Eli Friedman | 04ca252 | 2012-02-07 04:34:38 +0000 | [diff] [blame] | 12070 | } else { |
12071 | // C99 6.7.2.2p2: | ||||
12072 | // The expression that defines the value of an enumeration constant | ||||
12073 | // shall be an integer constant expression that has a value | ||||
12074 | // representable as an int. | ||||
12075 | |||||
12076 | // Complain if the value is not representable in an int. | ||||
12077 | if (!isRepresentableIntegerValue(Context, EnumVal, Context.IntTy)) | ||||
12078 | Diag(IdLoc, diag::ext_enum_value_not_int) | ||||
12079 | << EnumVal.toString(10) << Val->getSourceRange() | ||||
12080 | << (EnumVal.isUnsigned() || EnumVal.isNonNegative()); | ||||
12081 | else if (!Context.hasSameType(Val->getType(), Context.IntTy)) { | ||||
12082 | // Force the type of the expression to 'int'. | ||||
12083 | Val = ImpCastExprToType(Val, Context.IntTy, CK_IntegralCast).take(); | ||||
12084 | } | ||||
12085 | EltTy = Val->getType(); | ||||
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 12086 | } |
Douglas Gregor | 4912c34 | 2009-11-06 00:03:12 +0000 | [diff] [blame] | 12087 | } |
Douglas Gregor | 879fd49 | 2009-03-17 19:05:46 +0000 | [diff] [blame] | 12088 | } |
12089 | } | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12090 | |
Douglas Gregor | 879fd49 | 2009-03-17 19:05:46 +0000 | [diff] [blame] | 12091 | if (!Val) { |
Eli Friedman | ed0716b | 2009-12-11 01:34:50 +0000 | [diff] [blame] | 12092 | if (Enum->isDependentType()) |
12093 | EltTy = Context.DependentTy; | ||||
Douglas Gregor | 677e4fe | 2010-02-01 23:36:03 +0000 | [diff] [blame] | 12094 | else if (!LastEnumConst) { |
12095 | // C++0x [dcl.enum]p5: | ||||
12096 | // If the underlying type is not fixed, the type of each enumerator | ||||
12097 | // is the type of its initializing value: | ||||
12098 | // - If no initializer is specified for the first enumerator, the | ||||
12099 | // initializing value has an unspecified integral type. | ||||
12100 | // | ||||
12101 | // GCC uses 'int' for its unspecified integral type, as does | ||||
12102 | // C99 6.7.2.2p3. | ||||
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 12103 | if (Enum->isFixed()) { |
12104 | EltTy = Enum->getIntegerType(); | ||||
12105 | } | ||||
12106 | else { | ||||
12107 | EltTy = Context.IntTy; | ||||
12108 | } | ||||
Douglas Gregor | 677e4fe | 2010-02-01 23:36:03 +0000 | [diff] [blame] | 12109 | } else { |
Douglas Gregor | 879fd49 | 2009-03-17 19:05:46 +0000 | [diff] [blame] | 12110 | // Assign the last value + 1. |
12111 | EnumVal = LastEnumConst->getInitVal(); | ||||
12112 | ++EnumVal; | ||||
Douglas Gregor | 677e4fe | 2010-02-01 23:36:03 +0000 | [diff] [blame] | 12113 | EltTy = LastEnumConst->getType(); |
Douglas Gregor | 879fd49 | 2009-03-17 19:05:46 +0000 | [diff] [blame] | 12114 | |
12115 | // Check for overflow on increment. | ||||
Douglas Gregor | 677e4fe | 2010-02-01 23:36:03 +0000 | [diff] [blame] | 12116 | if (EnumVal < LastEnumConst->getInitVal()) { |
12117 | // C++0x [dcl.enum]p5: | ||||
12118 | // If the underlying type is not fixed, the type of each enumerator | ||||
12119 | // is the type of its initializing value: | ||||
12120 | // | ||||
12121 | // - Otherwise the type of the initializing value is the same as | ||||
12122 | // the type of the initializing value of the preceding enumerator | ||||
12123 | // unless the incremented value is not representable in that type, | ||||
12124 | // in which case the type is an unspecified integral type | ||||
12125 | // sufficient to contain the incremented value. If no such type | ||||
12126 | // exists, the program is ill-formed. | ||||
12127 | QualType T = getNextLargerIntegralType(Context, EltTy); | ||||
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 12128 | if (T.isNull() || Enum->isFixed()) { |
Douglas Gregor | 677e4fe | 2010-02-01 23:36:03 +0000 | [diff] [blame] | 12129 | // There is no integral type larger enough to represent this |
12130 | // value. Complain, then allow the value to wrap around. | ||||
12131 | EnumVal = LastEnumConst->getInitVal(); | ||||
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 12132 | EnumVal = EnumVal.zext(EnumVal.getBitWidth() * 2); |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 12133 | ++EnumVal; |
12134 | if (Enum->isFixed()) | ||||
12135 | // When the underlying type is fixed, this is ill-formed. | ||||
12136 | Diag(IdLoc, diag::err_enumerator_wrapped) | ||||
12137 | << EnumVal.toString(10) | ||||
12138 | << EltTy; | ||||
12139 | else | ||||
12140 | Diag(IdLoc, diag::warn_enumerator_too_large) | ||||
12141 | << EnumVal.toString(10); | ||||
Douglas Gregor | 677e4fe | 2010-02-01 23:36:03 +0000 | [diff] [blame] | 12142 | } else { |
12143 | EltTy = T; | ||||
12144 | } | ||||
12145 | |||||
12146 | // Retrieve the last enumerator's value, extent that type to the | ||||
12147 | // type that is supposed to be large enough to represent the incremented | ||||
12148 | // value, then increment. | ||||
12149 | EnumVal = LastEnumConst->getInitVal(); | ||||
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 12150 | EnumVal.setIsSigned(EltTy->isSignedIntegerOrEnumerationType()); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 12151 | EnumVal = EnumVal.zextOrTrunc(Context.getIntWidth(EltTy)); |
Douglas Gregor | 677e4fe | 2010-02-01 23:36:03 +0000 | [diff] [blame] | 12152 | ++EnumVal; |
12153 | |||||
12154 | // If we're not in C++, diagnose the overflow of enumerator values, | ||||
12155 | // which in C99 means that the enumerator value is not representable in | ||||
12156 | // an int (C99 6.7.2.2p2). However, we support GCC's extension that | ||||
12157 | // permits enumerator values that are representable in some larger | ||||
12158 | // integral type. | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 12159 | if (!getLangOpts().CPlusPlus && !T.isNull()) |
Douglas Gregor | 677e4fe | 2010-02-01 23:36:03 +0000 | [diff] [blame] | 12160 | Diag(IdLoc, diag::warn_enum_value_overflow); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 12161 | } else if (!getLangOpts().CPlusPlus && |
Douglas Gregor | 677e4fe | 2010-02-01 23:36:03 +0000 | [diff] [blame] | 12162 | !isRepresentableIntegerValue(Context, EnumVal, EltTy)) { |
12163 | // Enforce C99 6.7.2.2p2 even when we compute the next value. | ||||
12164 | Diag(IdLoc, diag::ext_enum_value_not_int) | ||||
12165 | << EnumVal.toString(10) << 1; | ||||
12166 | } | ||||
Douglas Gregor | 879fd49 | 2009-03-17 19:05:46 +0000 | [diff] [blame] | 12167 | } |
12168 | } | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12169 | |
Douglas Gregor | 9b9edd6 | 2010-03-02 17:53:14 +0000 | [diff] [blame] | 12170 | if (!EltTy->isDependentType()) { |
Douglas Gregor | 677e4fe | 2010-02-01 23:36:03 +0000 | [diff] [blame] | 12171 | // Make the enumerator value match the signedness and size of the |
12172 | // enumerator's type. | ||||
Eli Friedman | 04ca252 | 2012-02-07 04:34:38 +0000 | [diff] [blame] | 12173 | EnumVal = EnumVal.extOrTrunc(Context.getIntWidth(EltTy)); |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 12174 | EnumVal.setIsSigned(EltTy->isSignedIntegerOrEnumerationType()); |
Douglas Gregor | 677e4fe | 2010-02-01 23:36:03 +0000 | [diff] [blame] | 12175 | } |
Douglas Gregor | 4912c34 | 2009-11-06 00:03:12 +0000 | [diff] [blame] | 12176 | |
Douglas Gregor | 879fd49 | 2009-03-17 19:05:46 +0000 | [diff] [blame] | 12177 | return EnumConstantDecl::Create(Context, Enum, IdLoc, Id, EltTy, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12178 | Val, EnumVal); |
Douglas Gregor | 879fd49 | 2009-03-17 19:05:46 +0000 | [diff] [blame] | 12179 | } |
12180 | |||||
12181 | |||||
John McCall | 5b629aa | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 12182 | Decl *Sema::ActOnEnumConstant(Scope *S, Decl *theEnumDecl, Decl *lastEnumConst, |
12183 | SourceLocation IdLoc, IdentifierInfo *Id, | ||||
12184 | AttributeList *Attr, | ||||
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 12185 | SourceLocation EqualLoc, Expr *Val) { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 12186 | EnumDecl *TheEnumDecl = cast<EnumDecl>(theEnumDecl); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 12187 | EnumConstantDecl *LastEnumConst = |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 12188 | cast_or_null<EnumConstantDecl>(lastEnumConst); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 12189 | |
Chris Lattner | 31e0572 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 12190 | // The scope passed in may not be a decl scope. Zip up the scope tree until |
12191 | // we find one that is. | ||||
Douglas Gregor | 1a0d31a | 2009-01-12 18:45:55 +0000 | [diff] [blame] | 12192 | S = getNonFieldDeclScope(S); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12193 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 12194 | // Verify that there isn't already something declared with this name in this |
12195 | // scope. | ||||
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 12196 | NamedDecl *PrevDecl = LookupSingleName(S, Id, IdLoc, LookupOrdinaryName, |
Douglas Gregor | e39fe72 | 2010-01-19 06:06:57 +0000 | [diff] [blame] | 12197 | ForRedeclaration); |
Douglas Gregor | f57172b | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 12198 | if (PrevDecl && PrevDecl->isTemplateParameter()) { |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 12199 | // Maybe we will complain about the shadowed template parameter. |
12200 | DiagnoseTemplateParameterShadow(IdLoc, PrevDecl); | ||||
12201 | // Just pretend that we didn't see the previous declaration. | ||||
12202 | PrevDecl = 0; | ||||
12203 | } | ||||
12204 | |||||
12205 | if (PrevDecl) { | ||||
Argyrios Kyrtzidis | 0ff12f0 | 2008-07-16 21:01:53 +0000 | [diff] [blame] | 12206 | // When in C++, we may get a TagDecl with the same name; in this case the |
12207 | // enum constant will 'hide' the tag. | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 12208 | assert((getLangOpts().CPlusPlus || !isa<TagDecl>(PrevDecl)) && |
Argyrios Kyrtzidis | 0ff12f0 | 2008-07-16 21:01:53 +0000 | [diff] [blame] | 12209 | "Received TagDecl when not in C++!"); |
Argyrios Kyrtzidis | 15a12d0 | 2008-09-09 21:18:04 +0000 | [diff] [blame] | 12210 | if (!isa<TagDecl>(PrevDecl) && isDeclInScope(PrevDecl, CurContext, S)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 12211 | if (isa<EnumConstantDecl>(PrevDecl)) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 12212 | Diag(IdLoc, diag::err_redefinition_of_enumerator) << Id; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 12213 | else |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 12214 | Diag(IdLoc, diag::err_redefinition) << Id; |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 12215 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 12216 | return 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 12217 | } |
12218 | } | ||||
12219 | |||||
Aaron Ballman | f816787 | 2012-07-19 03:12:23 +0000 | [diff] [blame] | 12220 | // C++ [class.mem]p15: |
12221 | // If T is the name of a class, then each of the following shall have a name | ||||
12222 | // different from T: | ||||
12223 | // - every enumerator of every member of class T that is an unscoped | ||||
12224 | // enumerated type | ||||
Douglas Gregor | a6e937c | 2010-10-15 13:21:21 +0000 | [diff] [blame] | 12225 | if (CXXRecordDecl *Record |
12226 | = dyn_cast<CXXRecordDecl>( | ||||
12227 | TheEnumDecl->getDeclContext()->getRedeclContext())) | ||||
Aaron Ballman | f816787 | 2012-07-19 03:12:23 +0000 | [diff] [blame] | 12228 | if (!TheEnumDecl->isScoped() && |
12229 | Record->getIdentifier() && Record->getIdentifier() == Id) | ||||
Douglas Gregor | a6e937c | 2010-10-15 13:21:21 +0000 | [diff] [blame] | 12230 | Diag(IdLoc, diag::err_member_name_of_class) << Id; |
12231 | |||||
John McCall | 5b629aa | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 12232 | EnumConstantDecl *New = |
12233 | CheckEnumConstant(TheEnumDecl, LastEnumConst, IdLoc, Id, Val); | ||||
Chris Lattner | 421a23d | 2007-08-27 21:16:18 +0000 | [diff] [blame] | 12234 | |
John McCall | 92f8831 | 2010-01-23 00:46:32 +0000 | [diff] [blame] | 12235 | if (New) { |
John McCall | 5b629aa | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 12236 | // Process attributes. |
12237 | if (Attr) ProcessDeclAttributeList(S, New, Attr); | ||||
12238 | |||||
12239 | // Register this decl in the current scope stack. | ||||
John McCall | 92f8831 | 2010-01-23 00:46:32 +0000 | [diff] [blame] | 12240 | New->setAccess(TheEnumDecl->getAccess()); |
Douglas Gregor | 879fd49 | 2009-03-17 19:05:46 +0000 | [diff] [blame] | 12241 | PushOnScopeChains(New, S); |
John McCall | 92f8831 | 2010-01-23 00:46:32 +0000 | [diff] [blame] | 12242 | } |
Douglas Gregor | 45579f5 | 2008-12-17 02:04:30 +0000 | [diff] [blame] | 12243 | |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 12244 | ActOnDocumentableDecl(New); |
12245 | |||||
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 12246 | return New; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 12247 | } |
12248 | |||||
Ted Kremenek | a734a0e | 2012-12-22 01:34:09 +0000 | [diff] [blame] | 12249 | // Returns true when the enum initial expression does not trigger the |
12250 | // duplicate enum warning. A few common cases are exempted as follows: | ||||
12251 | // Element2 = Element1 | ||||
12252 | // Element2 = Element1 + 1 | ||||
12253 | // Element2 = Element1 - 1 | ||||
12254 | // Where Element2 and Element1 are from the same enum. | ||||
12255 | static bool ValidDuplicateEnum(EnumConstantDecl *ECD, EnumDecl *Enum) { | ||||
12256 | Expr *InitExpr = ECD->getInitExpr(); | ||||
12257 | if (!InitExpr) | ||||
12258 | return true; | ||||
12259 | InitExpr = InitExpr->IgnoreImpCasts(); | ||||
12260 | |||||
12261 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(InitExpr)) { | ||||
12262 | if (!BO->isAdditiveOp()) | ||||
12263 | return true; | ||||
12264 | IntegerLiteral *IL = dyn_cast<IntegerLiteral>(BO->getRHS()); | ||||
12265 | if (!IL) | ||||
12266 | return true; | ||||
12267 | if (IL->getValue() != 1) | ||||
12268 | return true; | ||||
12269 | |||||
12270 | InitExpr = BO->getLHS(); | ||||
12271 | } | ||||
12272 | |||||
12273 | // This checks if the elements are from the same enum. | ||||
12274 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(InitExpr); | ||||
12275 | if (!DRE) | ||||
12276 | return true; | ||||
12277 | |||||
12278 | EnumConstantDecl *EnumConstant = dyn_cast<EnumConstantDecl>(DRE->getDecl()); | ||||
12279 | if (!EnumConstant) | ||||
12280 | return true; | ||||
12281 | |||||
12282 | if (cast<EnumDecl>(TagDecl::castFromDeclContext(ECD->getDeclContext())) != | ||||
12283 | Enum) | ||||
12284 | return true; | ||||
12285 | |||||
12286 | return false; | ||||
12287 | } | ||||
12288 | |||||
12289 | struct DupKey { | ||||
12290 | int64_t val; | ||||
12291 | bool isTombstoneOrEmptyKey; | ||||
12292 | DupKey(int64_t val, bool isTombstoneOrEmptyKey) | ||||
12293 | : val(val), isTombstoneOrEmptyKey(isTombstoneOrEmptyKey) {} | ||||
12294 | }; | ||||
12295 | |||||
12296 | static DupKey GetDupKey(const llvm::APSInt& Val) { | ||||
12297 | return DupKey(Val.isSigned() ? Val.getSExtValue() : Val.getZExtValue(), | ||||
12298 | false); | ||||
12299 | } | ||||
12300 | |||||
12301 | struct DenseMapInfoDupKey { | ||||
12302 | static DupKey getEmptyKey() { return DupKey(0, true); } | ||||
12303 | static DupKey getTombstoneKey() { return DupKey(1, true); } | ||||
12304 | static unsigned getHashValue(const DupKey Key) { | ||||
12305 | return (unsigned)(Key.val * 37); | ||||
12306 | } | ||||
12307 | static bool isEqual(const DupKey& LHS, const DupKey& RHS) { | ||||
12308 | return LHS.isTombstoneOrEmptyKey == RHS.isTombstoneOrEmptyKey && | ||||
12309 | LHS.val == RHS.val; | ||||
12310 | } | ||||
12311 | }; | ||||
12312 | |||||
12313 | // Emits a warning when an element is implicitly set a value that | ||||
12314 | // a previous element has already been set to. | ||||
Dmitri Gribenko | 9ff2b42 | 2013-04-27 20:23:52 +0000 | [diff] [blame] | 12315 | static void CheckForDuplicateEnumValues(Sema &S, ArrayRef<Decl *> Elements, |
12316 | EnumDecl *Enum, | ||||
Ted Kremenek | a734a0e | 2012-12-22 01:34:09 +0000 | [diff] [blame] | 12317 | QualType EnumType) { |
12318 | if (S.Diags.getDiagnosticLevel(diag::warn_duplicate_enum_values, | ||||
12319 | Enum->getLocation()) == | ||||
12320 | DiagnosticsEngine::Ignored) | ||||
12321 | return; | ||||
12322 | // Avoid anonymous enums | ||||
12323 | if (!Enum->getIdentifier()) | ||||
12324 | return; | ||||
12325 | |||||
12326 | // Only check for small enums. | ||||
12327 | if (Enum->getNumPositiveBits() > 63 || Enum->getNumNegativeBits() > 64) | ||||
12328 | return; | ||||
12329 | |||||
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 12330 | typedef SmallVector<EnumConstantDecl *, 3> ECDVector; |
12331 | typedef SmallVector<ECDVector *, 3> DuplicatesVector; | ||||
Ted Kremenek | a734a0e | 2012-12-22 01:34:09 +0000 | [diff] [blame] | 12332 | |
12333 | typedef llvm::PointerUnion<EnumConstantDecl*, ECDVector*> DeclOrVector; | ||||
12334 | typedef llvm::DenseMap<DupKey, DeclOrVector, DenseMapInfoDupKey> | ||||
12335 | ValueToVectorMap; | ||||
12336 | |||||
12337 | DuplicatesVector DupVector; | ||||
12338 | ValueToVectorMap EnumMap; | ||||
12339 | |||||
12340 | // Populate the EnumMap with all values represented by enum constants without | ||||
12341 | // an initialier. | ||||
Dmitri Gribenko | 9ff2b42 | 2013-04-27 20:23:52 +0000 | [diff] [blame] | 12342 | for (unsigned i = 0, e = Elements.size(); i != e; ++i) { |
Benjamin Kramer | efac8da | 2013-04-07 14:10:40 +0000 | [diff] [blame] | 12343 | EnumConstantDecl *ECD = cast_or_null<EnumConstantDecl>(Elements[i]); |
Ted Kremenek | a734a0e | 2012-12-22 01:34:09 +0000 | [diff] [blame] | 12344 | |
12345 | // Null EnumConstantDecl means a previous diagnostic has been emitted for | ||||
12346 | // this constant. Skip this enum since it may be ill-formed. | ||||
12347 | if (!ECD) { | ||||
12348 | return; | ||||
12349 | } | ||||
12350 | |||||
12351 | if (ECD->getInitExpr()) | ||||
12352 | continue; | ||||
12353 | |||||
12354 | DupKey Key = GetDupKey(ECD->getInitVal()); | ||||
12355 | DeclOrVector &Entry = EnumMap[Key]; | ||||
12356 | |||||
12357 | // First time encountering this value. | ||||
12358 | if (Entry.isNull()) | ||||
12359 | Entry = ECD; | ||||
12360 | } | ||||
12361 | |||||
12362 | // Create vectors for any values that has duplicates. | ||||
Dmitri Gribenko | 9ff2b42 | 2013-04-27 20:23:52 +0000 | [diff] [blame] | 12363 | for (unsigned i = 0, e = Elements.size(); i != e; ++i) { |
Ted Kremenek | a734a0e | 2012-12-22 01:34:09 +0000 | [diff] [blame] | 12364 | EnumConstantDecl *ECD = cast<EnumConstantDecl>(Elements[i]); |
12365 | if (!ValidDuplicateEnum(ECD, Enum)) | ||||
12366 | continue; | ||||
12367 | |||||
12368 | DupKey Key = GetDupKey(ECD->getInitVal()); | ||||
12369 | |||||
12370 | DeclOrVector& Entry = EnumMap[Key]; | ||||
12371 | if (Entry.isNull()) | ||||
12372 | continue; | ||||
12373 | |||||
12374 | if (EnumConstantDecl *D = Entry.dyn_cast<EnumConstantDecl*>()) { | ||||
12375 | // Ensure constants are different. | ||||
12376 | if (D == ECD) | ||||
12377 | continue; | ||||
12378 | |||||
12379 | // Create new vector and push values onto it. | ||||
12380 | ECDVector *Vec = new ECDVector(); | ||||
12381 | Vec->push_back(D); | ||||
12382 | Vec->push_back(ECD); | ||||
12383 | |||||
12384 | // Update entry to point to the duplicates vector. | ||||
12385 | Entry = Vec; | ||||
12386 | |||||
12387 | // Store the vector somewhere we can consult later for quick emission of | ||||
12388 | // diagnostics. | ||||
12389 | DupVector.push_back(Vec); | ||||
12390 | continue; | ||||
12391 | } | ||||
12392 | |||||
12393 | ECDVector *Vec = Entry.get<ECDVector*>(); | ||||
12394 | // Make sure constants are not added more than once. | ||||
12395 | if (*Vec->begin() == ECD) | ||||
12396 | continue; | ||||
12397 | |||||
12398 | Vec->push_back(ECD); | ||||
12399 | } | ||||
12400 | |||||
12401 | // Emit diagnostics. | ||||
12402 | for (DuplicatesVector::iterator DupVectorIter = DupVector.begin(), | ||||
12403 | DupVectorEnd = DupVector.end(); | ||||
12404 | DupVectorIter != DupVectorEnd; ++DupVectorIter) { | ||||
12405 | ECDVector *Vec = *DupVectorIter; | ||||
12406 | assert(Vec->size() > 1 && "ECDVector should have at least 2 elements."); | ||||
12407 | |||||
12408 | // Emit warning for one enum constant. | ||||
12409 | ECDVector::iterator I = Vec->begin(); | ||||
12410 | S.Diag((*I)->getLocation(), diag::warn_duplicate_enum_values) | ||||
12411 | << (*I)->getName() << (*I)->getInitVal().toString(10) | ||||
12412 | << (*I)->getSourceRange(); | ||||
12413 | ++I; | ||||
12414 | |||||
12415 | // Emit one note for each of the remaining enum constants with | ||||
12416 | // the same value. | ||||
12417 | for (ECDVector::iterator E = Vec->end(); I != E; ++I) | ||||
12418 | S.Diag((*I)->getLocation(), diag::note_duplicate_element) | ||||
12419 | << (*I)->getName() << (*I)->getInitVal().toString(10) | ||||
12420 | << (*I)->getSourceRange(); | ||||
12421 | delete Vec; | ||||
12422 | } | ||||
12423 | } | ||||
12424 | |||||
Mike Stump | c6e35aa | 2009-05-16 07:06:02 +0000 | [diff] [blame] | 12425 | void Sema::ActOnEnumBody(SourceLocation EnumLoc, SourceLocation LBraceLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 12426 | SourceLocation RBraceLoc, Decl *EnumDeclX, |
Dmitri Gribenko | 9ff2b42 | 2013-04-27 20:23:52 +0000 | [diff] [blame] | 12427 | ArrayRef<Decl *> Elements, |
Edward O'Callaghan | fee1381 | 2009-08-08 14:36:57 +0000 | [diff] [blame] | 12428 | Scope *S, AttributeList *Attr) { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 12429 | EnumDecl *Enum = cast<EnumDecl>(EnumDeclX); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 12430 | QualType EnumType = Context.getTypeDeclType(Enum); |
Edward O'Callaghan | fee1381 | 2009-08-08 14:36:57 +0000 | [diff] [blame] | 12431 | |
12432 | if (Attr) | ||||
12433 | ProcessDeclAttributeList(S, Enum, Attr); | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12434 | |
Eli Friedman | ed0716b | 2009-12-11 01:34:50 +0000 | [diff] [blame] | 12435 | if (Enum->isDependentType()) { |
Dmitri Gribenko | 9ff2b42 | 2013-04-27 20:23:52 +0000 | [diff] [blame] | 12436 | for (unsigned i = 0, e = Elements.size(); i != e; ++i) { |
Eli Friedman | ed0716b | 2009-12-11 01:34:50 +0000 | [diff] [blame] | 12437 | EnumConstantDecl *ECD = |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 12438 | cast_or_null<EnumConstantDecl>(Elements[i]); |
Eli Friedman | ed0716b | 2009-12-11 01:34:50 +0000 | [diff] [blame] | 12439 | if (!ECD) continue; |
12440 | |||||
12441 | ECD->setType(EnumType); | ||||
12442 | } | ||||
12443 | |||||
John McCall | 1b5a618 | 2010-05-06 08:49:23 +0000 | [diff] [blame] | 12444 | Enum->completeDefinition(Context.DependentTy, Context.DependentTy, 0, 0); |
Eli Friedman | ed0716b | 2009-12-11 01:34:50 +0000 | [diff] [blame] | 12445 | return; |
12446 | } | ||||
12447 | |||||
Chris Lattner | e37f0be | 2007-08-28 05:10:31 +0000 | [diff] [blame] | 12448 | // TODO: If the result value doesn't fit in an int, it must be a long or long |
12449 | // long value. ISO C does not support this, but GCC does as an extension, | ||||
12450 | // emit a warning. | ||||
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 12451 | unsigned IntWidth = Context.getTargetInfo().getIntWidth(); |
12452 | unsigned CharWidth = Context.getTargetInfo().getCharWidth(); | ||||
12453 | unsigned ShortWidth = Context.getTargetInfo().getShortWidth(); | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12454 | |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 12455 | // Verify that all the values are okay, compute the size of the values, and |
12456 | // reverse the list. | ||||
12457 | unsigned NumNegativeBits = 0; | ||||
12458 | unsigned NumPositiveBits = 0; | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12459 | |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 12460 | // Keep track of whether all elements have type int. |
12461 | bool AllElementsInt = true; | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12462 | |
Dmitri Gribenko | 9ff2b42 | 2013-04-27 20:23:52 +0000 | [diff] [blame] | 12463 | for (unsigned i = 0, e = Elements.size(); i != e; ++i) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 12464 | EnumConstantDecl *ECD = |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 12465 | cast_or_null<EnumConstantDecl>(Elements[i]); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 12466 | if (!ECD) continue; // Already issued a diagnostic. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12467 | |
Chris Lattner | 211a30e | 2007-08-28 05:27:00 +0000 | [diff] [blame] | 12468 | const llvm::APSInt &InitVal = ECD->getInitVal(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12469 | |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 12470 | // Keep track of the size of positive and negative values. |
Chris Lattner | a73349d | 2008-02-26 00:33:57 +0000 | [diff] [blame] | 12471 | if (InitVal.isUnsigned() || InitVal.isNonNegative()) |
Chris Lattner | 21dd821 | 2008-01-14 21:47:29 +0000 | [diff] [blame] | 12472 | NumPositiveBits = std::max(NumPositiveBits, |
12473 | (unsigned)InitVal.getActiveBits()); | ||||
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 12474 | else |
Chris Lattner | 21dd821 | 2008-01-14 21:47:29 +0000 | [diff] [blame] | 12475 | NumNegativeBits = std::max(NumNegativeBits, |
12476 | (unsigned)InitVal.getMinSignedBits()); | ||||
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 12477 | |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 12478 | // Keep track of whether every enum element has type int (very commmon). |
12479 | if (AllElementsInt) | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12480 | AllElementsInt = ECD->getType() == Context.IntTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 12481 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12482 | |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 12483 | // Figure out the type that should be used for this enum. |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 12484 | QualType BestType; |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 12485 | unsigned BestWidth; |
Edward O'Callaghan | fee1381 | 2009-08-08 14:36:57 +0000 | [diff] [blame] | 12486 | |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 12487 | // C++0x N3000 [conv.prom]p3: |
12488 | // An rvalue of an unscoped enumeration type whose underlying | ||||
12489 | // type is not fixed can be converted to an rvalue of the first | ||||
12490 | // of the following types that can represent all the values of | ||||
12491 | // the enumeration: int, unsigned int, long int, unsigned long | ||||
12492 | // int, long long int, or unsigned long long int. | ||||
12493 | // C99 6.4.4.3p2: | ||||
12494 | // An identifier declared as an enumeration constant has type int. | ||||
12495 | // The C99 rule is modified by a gcc extension | ||||
12496 | QualType BestPromotionType; | ||||
12497 | |||||
Edward O'Callaghan | fee1381 | 2009-08-08 14:36:57 +0000 | [diff] [blame] | 12498 | bool Packed = Enum->getAttr<PackedAttr>() ? true : false; |
Argyrios Kyrtzidis | 9a2b9d7 | 2010-10-08 00:25:19 +0000 | [diff] [blame] | 12499 | // -fshort-enums is the equivalent to specifying the packed attribute on all |
12500 | // enum definitions. | ||||
12501 | if (LangOpts.ShortEnums) | ||||
12502 | Packed = true; | ||||
Edward O'Callaghan | fee1381 | 2009-08-08 14:36:57 +0000 | [diff] [blame] | 12503 | |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 12504 | if (Enum->isFixed()) { |
Eli Friedman | 3bfb571 | 2011-10-26 07:38:19 +0000 | [diff] [blame] | 12505 | BestType = Enum->getIntegerType(); |
12506 | if (BestType->isPromotableIntegerType()) | ||||
12507 | BestPromotionType = Context.getPromotedIntegerType(BestType); | ||||
12508 | else | ||||
12509 | BestPromotionType = BestType; | ||||
Duncan Sands | 240a020 | 2010-10-12 14:07:59 +0000 | [diff] [blame] | 12510 | // We don't need to set BestWidth, because BestType is going to be the type |
12511 | // of the enumerators, but we do anyway because otherwise some compilers | ||||
12512 | // warn that it might be used uninitialized. | ||||
12513 | BestWidth = CharWidth; | ||||
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 12514 | } |
12515 | else if (NumNegativeBits) { | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12516 | // 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] | 12517 | // int/long/longlong) that fits. |
Edward O'Callaghan | fee1381 | 2009-08-08 14:36:57 +0000 | [diff] [blame] | 12518 | // If it's packed, check also if it fits a char or a short. |
12519 | if (Packed && NumNegativeBits <= CharWidth && NumPositiveBits < CharWidth) { | ||||
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 12520 | BestType = Context.SignedCharTy; |
12521 | BestWidth = CharWidth; | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12522 | } else if (Packed && NumNegativeBits <= ShortWidth && |
Edward O'Callaghan | fee1381 | 2009-08-08 14:36:57 +0000 | [diff] [blame] | 12523 | NumPositiveBits < ShortWidth) { |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 12524 | BestType = Context.ShortTy; |
12525 | BestWidth = ShortWidth; | ||||
12526 | } else if (NumNegativeBits <= IntWidth && NumPositiveBits < IntWidth) { | ||||
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 12527 | BestType = Context.IntTy; |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 12528 | BestWidth = IntWidth; |
12529 | } else { | ||||
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 12530 | BestWidth = Context.getTargetInfo().getLongWidth(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12531 | |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 12532 | if (NumNegativeBits <= BestWidth && NumPositiveBits < BestWidth) { |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 12533 | BestType = Context.LongTy; |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 12534 | } else { |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 12535 | BestWidth = Context.getTargetInfo().getLongLongWidth(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12536 | |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 12537 | if (NumNegativeBits > BestWidth || NumPositiveBits >= BestWidth) |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 12538 | Diag(Enum->getLocation(), diag::warn_enum_too_large); |
12539 | BestType = Context.LongLongTy; | ||||
12540 | } | ||||
12541 | } | ||||
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 12542 | BestPromotionType = (BestWidth <= IntWidth ? Context.IntTy : BestType); |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 12543 | } else { |
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 12544 | // If there is no negative value, figure out the smallest type that fits |
12545 | // all of the enumerator values. | ||||
Edward O'Callaghan | fee1381 | 2009-08-08 14:36:57 +0000 | [diff] [blame] | 12546 | // If it's packed, check also if it fits a char or a short. |
12547 | if (Packed && NumPositiveBits <= CharWidth) { | ||||
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 12548 | BestType = Context.UnsignedCharTy; |
12549 | BestPromotionType = Context.IntTy; | ||||
12550 | BestWidth = CharWidth; | ||||
Edward O'Callaghan | fee1381 | 2009-08-08 14:36:57 +0000 | [diff] [blame] | 12551 | } else if (Packed && NumPositiveBits <= ShortWidth) { |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 12552 | BestType = Context.UnsignedShortTy; |
12553 | BestPromotionType = Context.IntTy; | ||||
12554 | BestWidth = ShortWidth; | ||||
12555 | } else if (NumPositiveBits <= IntWidth) { | ||||
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 12556 | BestType = Context.UnsignedIntTy; |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 12557 | BestWidth = IntWidth; |
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 12558 | BestPromotionType |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 12559 | = (NumPositiveBits == BestWidth || !getLangOpts().CPlusPlus) |
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 12560 | ? Context.UnsignedIntTy : Context.IntTy; |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 12561 | } else if (NumPositiveBits <= |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 12562 | (BestWidth = Context.getTargetInfo().getLongWidth())) { |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 12563 | BestType = Context.UnsignedLongTy; |
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 12564 | BestPromotionType |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 12565 | = (NumPositiveBits == BestWidth || !getLangOpts().CPlusPlus) |
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 12566 | ? Context.UnsignedLongTy : Context.LongTy; |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 12567 | } else { |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 12568 | BestWidth = Context.getTargetInfo().getLongLongWidth(); |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 12569 | assert(NumPositiveBits <= BestWidth && |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 12570 | "How could an initializer get larger than ULL?"); |
12571 | BestType = Context.UnsignedLongLongTy; | ||||
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 12572 | BestPromotionType |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 12573 | = (NumPositiveBits == BestWidth || !getLangOpts().CPlusPlus) |
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 12574 | ? Context.UnsignedLongLongTy : Context.LongLongTy; |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 12575 | } |
12576 | } | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12577 | |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 12578 | // Loop over all of the enumerator constants, changing their types to match |
12579 | // the type of the enum if needed. | ||||
Dmitri Gribenko | 9ff2b42 | 2013-04-27 20:23:52 +0000 | [diff] [blame] | 12580 | for (unsigned i = 0, e = Elements.size(); i != e; ++i) { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 12581 | EnumConstantDecl *ECD = cast_or_null<EnumConstantDecl>(Elements[i]); |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 12582 | if (!ECD) continue; // Already issued a diagnostic. |
12583 | |||||
12584 | // Standard C says the enumerators have int type, but we allow, as an | ||||
12585 | // extension, the enumerators to be larger than int size. If each | ||||
12586 | // enumerator value fits in an int, type it as an int, otherwise type it the | ||||
12587 | // same as the enumerator decl itself. This means that in "enum { X = 1U }" | ||||
12588 | // that X has type 'int', not 'unsigned'. | ||||
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 12589 | |
12590 | // Determine whether the value fits into an int. | ||||
12591 | llvm::APSInt InitVal = ECD->getInitVal(); | ||||
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 12592 | |
12593 | // If it fits into an integer type, force it. Otherwise force it to match | ||||
12594 | // the enum decl type. | ||||
12595 | QualType NewTy; | ||||
12596 | unsigned NewWidth; | ||||
12597 | bool NewSign; | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 12598 | if (!getLangOpts().CPlusPlus && |
Fariborz Jahanian | 3b25216 | 2011-11-04 18:51:24 +0000 | [diff] [blame] | 12599 | !Enum->isFixed() && |
Douglas Gregor | 677e4fe | 2010-02-01 23:36:03 +0000 | [diff] [blame] | 12600 | isRepresentableIntegerValue(Context, InitVal, Context.IntTy)) { |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 12601 | NewTy = Context.IntTy; |
12602 | NewWidth = IntWidth; | ||||
12603 | NewSign = true; | ||||
12604 | } else if (ECD->getType() == BestType) { | ||||
12605 | // Already the right type! | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 12606 | if (getLangOpts().CPlusPlus) |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 12607 | // C++ [dcl.enum]p4: Following the closing brace of an |
12608 | // enum-specifier, each enumerator has the type of its | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12609 | // enumeration. |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 12610 | ECD->setType(EnumType); |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 12611 | continue; |
12612 | } else { | ||||
12613 | NewTy = BestType; | ||||
12614 | NewWidth = BestWidth; | ||||
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 12615 | NewSign = BestType->isSignedIntegerOrEnumerationType(); |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 12616 | } |
12617 | |||||
12618 | // Adjust the APSInt value. | ||||
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 12619 | InitVal = InitVal.extOrTrunc(NewWidth); |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 12620 | InitVal.setIsSigned(NewSign); |
12621 | ECD->setInitVal(InitVal); | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12622 | |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 12623 | // Adjust the Expr initializer and type. |
Abramo Bagnara | 320e153 | 2010-12-17 15:49:53 +0000 | [diff] [blame] | 12624 | if (ECD->getInitExpr() && |
Nick Lewycky | 25af091 | 2011-07-02 02:05:12 +0000 | [diff] [blame] | 12625 | !Context.hasSameType(NewTy, ECD->getInitExpr()->getType())) |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 12626 | ECD->setInitExpr(ImplicitCastExpr::Create(Context, NewTy, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 12627 | CK_IntegralCast, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 12628 | ECD->getInitExpr(), |
12629 | /*base paths*/ 0, | ||||
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 12630 | VK_RValue)); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 12631 | if (getLangOpts().CPlusPlus) |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 12632 | // C++ [dcl.enum]p4: Following the closing brace of an |
12633 | // enum-specifier, each enumerator has the type of its | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12634 | // enumeration. |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 12635 | ECD->setType(EnumType); |
12636 | else | ||||
12637 | ECD->setType(NewTy); | ||||
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 12638 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12639 | |
John McCall | 1b5a618 | 2010-05-06 08:49:23 +0000 | [diff] [blame] | 12640 | Enum->completeDefinition(BestType, BestPromotionType, |
12641 | NumPositiveBits, NumNegativeBits); | ||||
James Molloy | 16f1f71 | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 12642 | |
12643 | // If we're declaring a function, ensure this decl isn't forgotten about - | ||||
12644 | // it needs to go into the function scope. | ||||
12645 | if (InFunctionDeclarator) | ||||
12646 | DeclsInPrototypeScope.push_back(Enum); | ||||
Ted Kremenek | a734a0e | 2012-12-22 01:34:09 +0000 | [diff] [blame] | 12647 | |
Dmitri Gribenko | 9ff2b42 | 2013-04-27 20:23:52 +0000 | [diff] [blame] | 12648 | CheckForDuplicateEnumValues(*this, Elements, Enum, EnumType); |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 12649 | |
12650 | // Now that the enum type is defined, ensure it's not been underaligned. | ||||
12651 | if (Enum->hasAttrs()) | ||||
12652 | CheckAlignasUnderalignment(Enum); | ||||
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 12653 | } |
12654 | |||||
Abramo Bagnara | 21e006e | 2011-03-03 14:20:18 +0000 | [diff] [blame] | 12655 | Decl *Sema::ActOnFileScopeAsmDecl(Expr *expr, |
12656 | SourceLocation StartLoc, | ||||
12657 | SourceLocation EndLoc) { | ||||
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 12658 | StringLiteral *AsmString = cast<StringLiteral>(expr); |
Sebastian Redl | 798d119 | 2008-12-13 16:23:55 +0000 | [diff] [blame] | 12659 | |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 12660 | FileScopeAsmDecl *New = FileScopeAsmDecl::Create(Context, CurContext, |
Abramo Bagnara | 21e006e | 2011-03-03 14:20:18 +0000 | [diff] [blame] | 12661 | AsmString, StartLoc, |
12662 | EndLoc); | ||||
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 12663 | CurContext->addDecl(New); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 12664 | return New; |
Anders Carlsson | dfab6cb | 2008-02-08 00:33:21 +0000 | [diff] [blame] | 12665 | } |
Eli Friedman | c49f19b | 2009-06-05 02:44:36 +0000 | [diff] [blame] | 12666 | |
Douglas Gregor | 5948ae1 | 2012-01-03 18:04:46 +0000 | [diff] [blame] | 12667 | DeclResult Sema::ActOnModuleImport(SourceLocation AtLoc, |
12668 | SourceLocation ImportLoc, | ||||
12669 | ModuleIdPath Path) { | ||||
Douglas Gregor | 5e35693 | 2011-12-01 17:11:21 +0000 | [diff] [blame] | 12670 | Module *Mod = PP.getModuleLoader().loadModule(ImportLoc, Path, |
Douglas Gregor | 93ebfa6 | 2011-12-02 23:42:12 +0000 | [diff] [blame] | 12671 | Module::AllVisible, |
12672 | /*IsIncludeDirective=*/false); | ||||
Douglas Gregor | 1a4761e | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 12673 | if (!Mod) |
Douglas Gregor | 6aa52ec | 2011-08-26 23:56:07 +0000 | [diff] [blame] | 12674 | return true; |
12675 | |||||
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 12676 | SmallVector<SourceLocation, 2> IdentifierLocs; |
Douglas Gregor | 15de72c | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 12677 | Module *ModCheck = Mod; |
12678 | for (unsigned I = 0, N = Path.size(); I != N; ++I) { | ||||
12679 | // If we've run out of module parents, just drop the remaining identifiers. | ||||
12680 | // We need the length to be consistent. | ||||
12681 | if (!ModCheck) | ||||
12682 | break; | ||||
12683 | ModCheck = ModCheck->Parent; | ||||
12684 | |||||
12685 | IdentifierLocs.push_back(Path[I].second); | ||||
12686 | } | ||||
12687 | |||||
12688 | ImportDecl *Import = ImportDecl::Create(Context, | ||||
12689 | Context.getTranslationUnitDecl(), | ||||
Douglas Gregor | 5948ae1 | 2012-01-03 18:04:46 +0000 | [diff] [blame] | 12690 | AtLoc.isValid()? AtLoc : ImportLoc, |
12691 | Mod, IdentifierLocs); | ||||
Douglas Gregor | 15de72c | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 12692 | Context.getTranslationUnitDecl()->addDecl(Import); |
12693 | return Import; | ||||
Douglas Gregor | 6aa52ec | 2011-08-26 23:56:07 +0000 | [diff] [blame] | 12694 | } |
12695 | |||||
Douglas Gregor | ca2ab45 | 2013-01-12 01:29:50 +0000 | [diff] [blame] | 12696 | void Sema::createImplicitModuleImport(SourceLocation Loc, Module *Mod) { |
12697 | // Create the implicit import declaration. | ||||
12698 | TranslationUnitDecl *TU = getASTContext().getTranslationUnitDecl(); | ||||
12699 | ImportDecl *ImportD = ImportDecl::CreateImplicit(getASTContext(), TU, | ||||
12700 | Loc, Mod, Loc); | ||||
12701 | TU->addDecl(ImportD); | ||||
12702 | Consumer.HandleImplicitImportDecl(ImportD); | ||||
12703 | |||||
12704 | // Make the module visible. | ||||
Douglas Gregor | 906d66a | 2013-03-20 21:10:35 +0000 | [diff] [blame] | 12705 | PP.getModuleLoader().makeModuleVisible(Mod, Module::AllVisible, Loc, |
12706 | /*Complain=*/false); | ||||
Douglas Gregor | ca2ab45 | 2013-01-12 01:29:50 +0000 | [diff] [blame] | 12707 | } |
12708 | |||||
David Chisnall | 5f3c163 | 2012-02-18 16:12:34 +0000 | [diff] [blame] | 12709 | void Sema::ActOnPragmaRedefineExtname(IdentifierInfo* Name, |
12710 | IdentifierInfo* AliasName, | ||||
12711 | SourceLocation PragmaLoc, | ||||
12712 | SourceLocation NameLoc, | ||||
12713 | SourceLocation AliasNameLoc) { | ||||
12714 | Decl *PrevDecl = LookupSingleName(TUScope, Name, NameLoc, | ||||
12715 | LookupOrdinaryName); | ||||
12716 | AsmLabelAttr *Attr = | ||||
12717 | ::new (Context) AsmLabelAttr(AliasNameLoc, Context, AliasName->getName()); | ||||
David Chisnall | 5f3c163 | 2012-02-18 16:12:34 +0000 | [diff] [blame] | 12718 | |
12719 | if (PrevDecl) | ||||
12720 | PrevDecl->addAttr(Attr); | ||||
12721 | else | ||||
12722 | (void)ExtnameUndeclaredIdentifiers.insert( | ||||
12723 | std::pair<IdentifierInfo*,AsmLabelAttr*>(Name, Attr)); | ||||
12724 | } | ||||
12725 | |||||
Eli Friedman | c49f19b | 2009-06-05 02:44:36 +0000 | [diff] [blame] | 12726 | void Sema::ActOnPragmaWeakID(IdentifierInfo* Name, |
12727 | SourceLocation PragmaLoc, | ||||
12728 | SourceLocation NameLoc) { | ||||
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 12729 | Decl *PrevDecl = LookupSingleName(TUScope, Name, NameLoc, LookupOrdinaryName); |
Eli Friedman | c49f19b | 2009-06-05 02:44:36 +0000 | [diff] [blame] | 12730 | |
Eli Friedman | c49f19b | 2009-06-05 02:44:36 +0000 | [diff] [blame] | 12731 | if (PrevDecl) { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 12732 | PrevDecl->addAttr(::new (Context) WeakAttr(PragmaLoc, Context)); |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 12733 | } else { |
12734 | (void)WeakUndeclaredIdentifiers.insert( | ||||
12735 | std::pair<IdentifierInfo*,WeakInfo> | ||||
12736 | (Name, WeakInfo((IdentifierInfo*)0, NameLoc))); | ||||
Eli Friedman | c49f19b | 2009-06-05 02:44:36 +0000 | [diff] [blame] | 12737 | } |
Eli Friedman | c49f19b | 2009-06-05 02:44:36 +0000 | [diff] [blame] | 12738 | } |
12739 | |||||
12740 | void Sema::ActOnPragmaWeakAlias(IdentifierInfo* Name, | ||||
12741 | IdentifierInfo* AliasName, | ||||
12742 | SourceLocation PragmaLoc, | ||||
12743 | SourceLocation NameLoc, | ||||
12744 | SourceLocation AliasNameLoc) { | ||||
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 12745 | Decl *PrevDecl = LookupSingleName(TUScope, AliasName, AliasNameLoc, |
12746 | LookupOrdinaryName); | ||||
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 12747 | WeakInfo W = WeakInfo(Name, NameLoc); |
Eli Friedman | c49f19b | 2009-06-05 02:44:36 +0000 | [diff] [blame] | 12748 | |
Eli Friedman | c49f19b | 2009-06-05 02:44:36 +0000 | [diff] [blame] | 12749 | if (PrevDecl) { |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 12750 | if (!PrevDecl->hasAttr<AliasAttr>()) |
12751 | if (NamedDecl *ND = dyn_cast<NamedDecl>(PrevDecl)) | ||||
Ryan Flynn | 7b1fdbd | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 12752 | DeclApplyPragmaWeak(TUScope, ND, W); |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 12753 | } else { |
12754 | (void)WeakUndeclaredIdentifiers.insert( | ||||
12755 | std::pair<IdentifierInfo*,WeakInfo>(AliasName, W)); | ||||
Eli Friedman | c49f19b | 2009-06-05 02:44:36 +0000 | [diff] [blame] | 12756 | } |
Eli Friedman | c49f19b | 2009-06-05 02:44:36 +0000 | [diff] [blame] | 12757 | } |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 12758 | |
12759 | Decl *Sema::getObjCDeclContext() const { | ||||
12760 | return (dyn_cast_or_null<ObjCContainerDecl>(CurContext)); | ||||
12761 | } | ||||
Argyrios Kyrtzidis | c076e37 | 2011-10-06 23:23:27 +0000 | [diff] [blame] | 12762 | |
12763 | AvailabilityResult Sema::getCurContextAvailability() const { | ||||
Fariborz Jahanian | 3359fa3 | 2012-09-06 18:38:58 +0000 | [diff] [blame] | 12764 | const Decl *D = cast<Decl>(getCurObjCLexicalContext()); |
Argyrios Kyrtzidis | c076e37 | 2011-10-06 23:23:27 +0000 | [diff] [blame] | 12765 | return D->getAvailability(); |
12766 | } |