/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Parse/ParseDeclCXX.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===--- ParseDeclCXX.cpp - C++ Declaration Parsing -------------*- C++ -*-===// |
2 | | // |
3 | | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | | // See https://llvm.org/LICENSE.txt for license information. |
5 | | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | | // |
7 | | //===----------------------------------------------------------------------===// |
8 | | // |
9 | | // This file implements the C++ Declaration portions of the Parser interfaces. |
10 | | // |
11 | | //===----------------------------------------------------------------------===// |
12 | | |
13 | | #include "clang/Parse/Parser.h" |
14 | | #include "clang/AST/ASTContext.h" |
15 | | #include "clang/AST/DeclTemplate.h" |
16 | | #include "clang/AST/PrettyDeclStackTrace.h" |
17 | | #include "clang/Basic/Attributes.h" |
18 | | #include "clang/Basic/CharInfo.h" |
19 | | #include "clang/Basic/OperatorKinds.h" |
20 | | #include "clang/Basic/TargetInfo.h" |
21 | | #include "clang/Parse/ParseDiagnostic.h" |
22 | | #include "clang/Parse/RAIIObjectsForParser.h" |
23 | | #include "clang/Sema/DeclSpec.h" |
24 | | #include "clang/Sema/ParsedTemplate.h" |
25 | | #include "clang/Sema/Scope.h" |
26 | | #include "llvm/ADT/SmallString.h" |
27 | | #include "llvm/Support/TimeProfiler.h" |
28 | | |
29 | | using namespace clang; |
30 | | |
31 | | /// ParseNamespace - We know that the current token is a namespace keyword. This |
32 | | /// may either be a top level namespace or a block-level namespace alias. If |
33 | | /// there was an inline keyword, it has already been parsed. |
34 | | /// |
35 | | /// namespace-definition: [C++: namespace.def] |
36 | | /// named-namespace-definition |
37 | | /// unnamed-namespace-definition |
38 | | /// nested-namespace-definition |
39 | | /// |
40 | | /// named-namespace-definition: |
41 | | /// 'inline'[opt] 'namespace' attributes[opt] identifier '{' |
42 | | /// namespace-body '}' |
43 | | /// |
44 | | /// unnamed-namespace-definition: |
45 | | /// 'inline'[opt] 'namespace' attributes[opt] '{' namespace-body '}' |
46 | | /// |
47 | | /// nested-namespace-definition: |
48 | | /// 'namespace' enclosing-namespace-specifier '::' 'inline'[opt] |
49 | | /// identifier '{' namespace-body '}' |
50 | | /// |
51 | | /// enclosing-namespace-specifier: |
52 | | /// identifier |
53 | | /// enclosing-namespace-specifier '::' 'inline'[opt] identifier |
54 | | /// |
55 | | /// namespace-alias-definition: [C++ 7.3.2: namespace.alias] |
56 | | /// 'namespace' identifier '=' qualified-namespace-specifier ';' |
57 | | /// |
58 | | Parser::DeclGroupPtrTy Parser::ParseNamespace(DeclaratorContext Context, |
59 | | SourceLocation &DeclEnd, |
60 | 76.9k | SourceLocation InlineLoc) { |
61 | 76.9k | assert(Tok.is(tok::kw_namespace) && "Not a namespace!"); |
62 | 76.9k | SourceLocation NamespaceLoc = ConsumeToken(); // eat the 'namespace'. |
63 | 76.9k | ObjCDeclContextSwitch ObjCDC(*this); |
64 | | |
65 | 76.9k | if (Tok.is(tok::code_completion)) { |
66 | 1 | Actions.CodeCompleteNamespaceDecl(getCurScope()); |
67 | 1 | cutOffParsing(); |
68 | 1 | return nullptr; |
69 | 1 | } |
70 | | |
71 | 76.9k | SourceLocation IdentLoc; |
72 | 76.9k | IdentifierInfo *Ident = nullptr; |
73 | 76.9k | InnerNamespaceInfoList ExtraNSs; |
74 | 76.9k | SourceLocation FirstNestedInlineLoc; |
75 | | |
76 | 76.9k | ParsedAttributesWithRange attrs(AttrFactory); |
77 | 76.9k | SourceLocation attrLoc; |
78 | 76.9k | if (getLangOpts().CPlusPlus11 && isCXX11AttributeSpecifier()74.2k ) { |
79 | 10 | Diag(Tok.getLocation(), getLangOpts().CPlusPlus17 |
80 | 9 | ? diag::warn_cxx14_compat_ns_enum_attribute |
81 | 1 | : diag::ext_ns_enum_attribute) |
82 | 10 | << 0 /*namespace*/; |
83 | 10 | attrLoc = Tok.getLocation(); |
84 | 10 | ParseCXX11Attributes(attrs); |
85 | 10 | } |
86 | | |
87 | 76.9k | if (Tok.is(tok::identifier)) { |
88 | 75.0k | Ident = Tok.getIdentifierInfo(); |
89 | 75.0k | IdentLoc = ConsumeToken(); // eat the identifier. |
90 | 75.1k | while (Tok.is(tok::coloncolon) && |
91 | 98 | (NextToken().is(tok::identifier) || |
92 | 13 | (NextToken().is(tok::kw_inline) && |
93 | 98 | GetLookAheadToken(2).is(tok::identifier)13 ))) { |
94 | | |
95 | 98 | InnerNamespaceInfo Info; |
96 | 98 | Info.NamespaceLoc = ConsumeToken(); |
97 | | |
98 | 98 | if (Tok.is(tok::kw_inline)) { |
99 | 13 | Info.InlineLoc = ConsumeToken(); |
100 | 13 | if (FirstNestedInlineLoc.isInvalid()) |
101 | 7 | FirstNestedInlineLoc = Info.InlineLoc; |
102 | 13 | } |
103 | | |
104 | 98 | Info.Ident = Tok.getIdentifierInfo(); |
105 | 98 | Info.IdentLoc = ConsumeToken(); |
106 | | |
107 | 98 | ExtraNSs.push_back(Info); |
108 | 98 | } |
109 | 75.0k | } |
110 | | |
111 | | // A nested namespace definition cannot have attributes. |
112 | 76.9k | if (!ExtraNSs.empty() && attrLoc.isValid()65 ) |
113 | 1 | Diag(attrLoc, diag::err_unexpected_nested_namespace_attribute); |
114 | | |
115 | | // Read label attributes, if present. |
116 | 76.9k | if (Tok.is(tok::kw___attribute)) { |
117 | 48 | attrLoc = Tok.getLocation(); |
118 | 48 | ParseGNUAttributes(attrs); |
119 | 48 | } |
120 | | |
121 | 76.9k | if (Tok.is(tok::equal)) { |
122 | 339 | if (!Ident) { |
123 | 1 | Diag(Tok, diag::err_expected) << tok::identifier; |
124 | | // Skip to end of the definition and eat the ';'. |
125 | 1 | SkipUntil(tok::semi); |
126 | 1 | return nullptr; |
127 | 1 | } |
128 | 338 | if (attrLoc.isValid()) |
129 | 2 | Diag(attrLoc, diag::err_unexpected_namespace_attributes_alias); |
130 | 338 | if (InlineLoc.isValid()) |
131 | 0 | Diag(InlineLoc, diag::err_inline_namespace_alias) |
132 | 0 | << FixItHint::CreateRemoval(InlineLoc); |
133 | 338 | Decl *NSAlias = ParseNamespaceAlias(NamespaceLoc, IdentLoc, Ident, DeclEnd); |
134 | 338 | return Actions.ConvertDeclToDeclGroup(NSAlias); |
135 | 338 | } |
136 | | |
137 | 76.5k | BalancedDelimiterTracker T(*this, tok::l_brace); |
138 | 76.5k | if (T.consumeOpen()) { |
139 | 7 | if (Ident) |
140 | 0 | Diag(Tok, diag::err_expected) << tok::l_brace; |
141 | 7 | else |
142 | 7 | Diag(Tok, diag::err_expected_either) << tok::identifier << tok::l_brace; |
143 | 7 | return nullptr; |
144 | 7 | } |
145 | | |
146 | 76.5k | if (getCurScope()->isClassScope() || getCurScope()->isTemplateParamScope() || |
147 | 76.5k | getCurScope()->isInObjcMethodScope() || getCurScope()->getBlockParent() || |
148 | 76.5k | getCurScope()->getFnParent()) { |
149 | 1 | Diag(T.getOpenLocation(), diag::err_namespace_nonnamespace_scope); |
150 | 1 | SkipUntil(tok::r_brace); |
151 | 1 | return nullptr; |
152 | 1 | } |
153 | | |
154 | 76.5k | if (ExtraNSs.empty()) { |
155 | | // Normal namespace definition, not a nested-namespace-definition. |
156 | 65 | } else if (InlineLoc.isValid()) { |
157 | 5 | Diag(InlineLoc, diag::err_inline_nested_namespace_definition); |
158 | 60 | } else if (getLangOpts().CPlusPlus20) { |
159 | 26 | Diag(ExtraNSs[0].NamespaceLoc, |
160 | 26 | diag::warn_cxx14_compat_nested_namespace_definition); |
161 | 26 | if (FirstNestedInlineLoc.isValid()) |
162 | 3 | Diag(FirstNestedInlineLoc, |
163 | 3 | diag::warn_cxx17_compat_inline_nested_namespace_definition); |
164 | 34 | } else if (getLangOpts().CPlusPlus17) { |
165 | 22 | Diag(ExtraNSs[0].NamespaceLoc, |
166 | 22 | diag::warn_cxx14_compat_nested_namespace_definition); |
167 | 22 | if (FirstNestedInlineLoc.isValid()) |
168 | 2 | Diag(FirstNestedInlineLoc, diag::ext_inline_nested_namespace_definition); |
169 | 12 | } else { |
170 | 12 | TentativeParsingAction TPA(*this); |
171 | 12 | SkipUntil(tok::r_brace, StopBeforeMatch); |
172 | 12 | Token rBraceToken = Tok; |
173 | 12 | TPA.Revert(); |
174 | | |
175 | 12 | if (!rBraceToken.is(tok::r_brace)) { |
176 | 0 | Diag(ExtraNSs[0].NamespaceLoc, diag::ext_nested_namespace_definition) |
177 | 0 | << SourceRange(ExtraNSs.front().NamespaceLoc, |
178 | 0 | ExtraNSs.back().IdentLoc); |
179 | 12 | } else { |
180 | 12 | std::string NamespaceFix; |
181 | 23 | for (const auto &ExtraNS : ExtraNSs) { |
182 | 23 | NamespaceFix += " { "; |
183 | 23 | if (ExtraNS.InlineLoc.isValid()) |
184 | 4 | NamespaceFix += "inline "; |
185 | 23 | NamespaceFix += "namespace "; |
186 | 23 | NamespaceFix += ExtraNS.Ident->getName(); |
187 | 23 | } |
188 | | |
189 | 12 | std::string RBraces; |
190 | 35 | for (unsigned i = 0, e = ExtraNSs.size(); i != e; ++i23 ) |
191 | 23 | RBraces += "} "; |
192 | | |
193 | 12 | Diag(ExtraNSs[0].NamespaceLoc, diag::ext_nested_namespace_definition) |
194 | 12 | << FixItHint::CreateReplacement( |
195 | 12 | SourceRange(ExtraNSs.front().NamespaceLoc, |
196 | 12 | ExtraNSs.back().IdentLoc), |
197 | 12 | NamespaceFix) |
198 | 12 | << FixItHint::CreateInsertion(rBraceToken.getLocation(), RBraces); |
199 | 12 | } |
200 | | |
201 | | // Warn about nested inline namespaces. |
202 | 12 | if (FirstNestedInlineLoc.isValid()) |
203 | 2 | Diag(FirstNestedInlineLoc, diag::ext_inline_nested_namespace_definition); |
204 | 12 | } |
205 | | |
206 | | // If we're still good, complain about inline namespaces in non-C++0x now. |
207 | 76.5k | if (InlineLoc.isValid()) |
208 | 20.1k | Diag(InlineLoc, getLangOpts().CPlusPlus11 ? |
209 | 20.1k | diag::warn_cxx98_compat_inline_namespace : diag::ext_inline_namespace16 ); |
210 | | |
211 | | // Enter a scope for the namespace. |
212 | 76.5k | ParseScope NamespaceScope(this, Scope::DeclScope); |
213 | | |
214 | 76.5k | UsingDirectiveDecl *ImplicitUsingDirectiveDecl = nullptr; |
215 | 76.5k | Decl *NamespcDecl = Actions.ActOnStartNamespaceDef( |
216 | 76.5k | getCurScope(), InlineLoc, NamespaceLoc, IdentLoc, Ident, |
217 | 76.5k | T.getOpenLocation(), attrs, ImplicitUsingDirectiveDecl); |
218 | | |
219 | 76.5k | PrettyDeclStackTraceEntry CrashInfo(Actions.Context, NamespcDecl, |
220 | 76.5k | NamespaceLoc, "parsing namespace"); |
221 | | |
222 | | // Parse the contents of the namespace. This includes parsing recovery on |
223 | | // any improperly nested namespaces. |
224 | 76.5k | ParseInnerNamespace(ExtraNSs, 0, InlineLoc, attrs, T); |
225 | | |
226 | | // Leave the namespace scope. |
227 | 76.5k | NamespaceScope.Exit(); |
228 | | |
229 | 76.5k | DeclEnd = T.getCloseLocation(); |
230 | 76.5k | Actions.ActOnFinishNamespaceDef(NamespcDecl, DeclEnd); |
231 | | |
232 | 76.5k | return Actions.ConvertDeclToDeclGroup(NamespcDecl, |
233 | 76.5k | ImplicitUsingDirectiveDecl); |
234 | 76.5k | } |
235 | | |
236 | | /// ParseInnerNamespace - Parse the contents of a namespace. |
237 | | void Parser::ParseInnerNamespace(const InnerNamespaceInfoList &InnerNSs, |
238 | | unsigned int index, SourceLocation &InlineLoc, |
239 | | ParsedAttributes &attrs, |
240 | 76.6k | BalancedDelimiterTracker &Tracker) { |
241 | 76.6k | if (index == InnerNSs.size()) { |
242 | 1.47M | while (!tryParseMisplacedModuleImport() && Tok.isNot(tok::r_brace) && |
243 | 1.39M | Tok.isNot(tok::eof)) { |
244 | 1.39M | ParsedAttributesWithRange attrs(AttrFactory); |
245 | 1.39M | MaybeParseCXX11Attributes(attrs); |
246 | 1.39M | ParseExternalDeclaration(attrs); |
247 | 1.39M | } |
248 | | |
249 | | // The caller is what called check -- we are simply calling |
250 | | // the close for it. |
251 | 76.5k | Tracker.consumeClose(); |
252 | | |
253 | 76.5k | return; |
254 | 76.5k | } |
255 | | |
256 | | // Handle a nested namespace definition. |
257 | | // FIXME: Preserve the source information through to the AST rather than |
258 | | // desugaring it here. |
259 | 98 | ParseScope NamespaceScope(this, Scope::DeclScope); |
260 | 98 | UsingDirectiveDecl *ImplicitUsingDirectiveDecl = nullptr; |
261 | 98 | Decl *NamespcDecl = Actions.ActOnStartNamespaceDef( |
262 | 98 | getCurScope(), InnerNSs[index].InlineLoc, InnerNSs[index].NamespaceLoc, |
263 | 98 | InnerNSs[index].IdentLoc, InnerNSs[index].Ident, |
264 | 98 | Tracker.getOpenLocation(), attrs, ImplicitUsingDirectiveDecl); |
265 | 98 | assert(!ImplicitUsingDirectiveDecl && |
266 | 98 | "nested namespace definition cannot define anonymous namespace"); |
267 | | |
268 | 98 | ParseInnerNamespace(InnerNSs, ++index, InlineLoc, attrs, Tracker); |
269 | | |
270 | 98 | NamespaceScope.Exit(); |
271 | 98 | Actions.ActOnFinishNamespaceDef(NamespcDecl, Tracker.getCloseLocation()); |
272 | 98 | } |
273 | | |
274 | | /// ParseNamespaceAlias - Parse the part after the '=' in a namespace |
275 | | /// alias definition. |
276 | | /// |
277 | | Decl *Parser::ParseNamespaceAlias(SourceLocation NamespaceLoc, |
278 | | SourceLocation AliasLoc, |
279 | | IdentifierInfo *Alias, |
280 | 338 | SourceLocation &DeclEnd) { |
281 | 338 | assert(Tok.is(tok::equal) && "Not equal token"); |
282 | | |
283 | 338 | ConsumeToken(); // eat the '='. |
284 | | |
285 | 338 | if (Tok.is(tok::code_completion)) { |
286 | 1 | Actions.CodeCompleteNamespaceAliasDecl(getCurScope()); |
287 | 1 | cutOffParsing(); |
288 | 1 | return nullptr; |
289 | 1 | } |
290 | | |
291 | 337 | CXXScopeSpec SS; |
292 | | // Parse (optional) nested-name-specifier. |
293 | 337 | ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/nullptr, |
294 | 337 | /*ObjectHadErrors=*/false, |
295 | 337 | /*EnteringContext=*/false, |
296 | 337 | /*MayBePseudoDestructor=*/nullptr, |
297 | 337 | /*IsTypename=*/false, |
298 | 337 | /*LastII=*/nullptr, |
299 | 337 | /*OnlyNamespace=*/true); |
300 | | |
301 | 337 | if (Tok.isNot(tok::identifier)) { |
302 | 2 | Diag(Tok, diag::err_expected_namespace_name); |
303 | | // Skip to end of the definition and eat the ';'. |
304 | 2 | SkipUntil(tok::semi); |
305 | 2 | return nullptr; |
306 | 2 | } |
307 | | |
308 | 335 | if (SS.isInvalid()) { |
309 | | // Diagnostics have been emitted in ParseOptionalCXXScopeSpecifier. |
310 | | // Skip to end of the definition and eat the ';'. |
311 | 4 | SkipUntil(tok::semi); |
312 | 4 | return nullptr; |
313 | 4 | } |
314 | | |
315 | | // Parse identifier. |
316 | 331 | IdentifierInfo *Ident = Tok.getIdentifierInfo(); |
317 | 331 | SourceLocation IdentLoc = ConsumeToken(); |
318 | | |
319 | | // Eat the ';'. |
320 | 331 | DeclEnd = Tok.getLocation(); |
321 | 331 | if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after_namespace_name)) |
322 | 0 | SkipUntil(tok::semi); |
323 | | |
324 | 331 | return Actions.ActOnNamespaceAliasDef(getCurScope(), NamespaceLoc, AliasLoc, |
325 | 331 | Alias, SS, IdentLoc, Ident); |
326 | 331 | } |
327 | | |
328 | | /// ParseLinkage - We know that the current token is a string_literal |
329 | | /// and just before that, that extern was seen. |
330 | | /// |
331 | | /// linkage-specification: [C++ 7.5p2: dcl.link] |
332 | | /// 'extern' string-literal '{' declaration-seq[opt] '}' |
333 | | /// 'extern' string-literal declaration |
334 | | /// |
335 | 151k | Decl *Parser::ParseLinkage(ParsingDeclSpec &DS, DeclaratorContext Context) { |
336 | 151k | assert(isTokenStringLiteral() && "Not a string literal!"); |
337 | 151k | ExprResult Lang = ParseStringLiteralExpression(false); |
338 | | |
339 | 151k | ParseScope LinkageScope(this, Scope::DeclScope); |
340 | 151k | Decl *LinkageSpec = |
341 | 151k | Lang.isInvalid() |
342 | 1 | ? nullptr |
343 | 151k | : Actions.ActOnStartLinkageSpecification( |
344 | 151k | getCurScope(), DS.getSourceRange().getBegin(), Lang.get(), |
345 | 87.9k | Tok.is(tok::l_brace) ? Tok.getLocation()63.6k : SourceLocation()); |
346 | | |
347 | 151k | ParsedAttributesWithRange attrs(AttrFactory); |
348 | 151k | MaybeParseCXX11Attributes(attrs); |
349 | | |
350 | 151k | if (Tok.isNot(tok::l_brace)) { |
351 | | // Reset the source range in DS, as the leading "extern" |
352 | | // does not really belong to the inner declaration ... |
353 | 87.9k | DS.SetRangeStart(SourceLocation()); |
354 | 87.9k | DS.SetRangeEnd(SourceLocation()); |
355 | | // ... but anyway remember that such an "extern" was seen. |
356 | 87.9k | DS.setExternInLinkageSpec(true); |
357 | 87.9k | ParseExternalDeclaration(attrs, &DS); |
358 | 87.9k | return LinkageSpec ? Actions.ActOnFinishLinkageSpecification( |
359 | 87.9k | getCurScope(), LinkageSpec, SourceLocation()) |
360 | 4 | : nullptr; |
361 | 87.9k | } |
362 | | |
363 | 63.6k | DS.abort(); |
364 | | |
365 | 63.6k | ProhibitAttributes(attrs); |
366 | | |
367 | 63.6k | BalancedDelimiterTracker T(*this, tok::l_brace); |
368 | 63.6k | T.consumeOpen(); |
369 | | |
370 | 63.6k | unsigned NestedModules = 0; |
371 | 1.17M | while (true) { |
372 | 1.17M | switch (Tok.getKind()) { |
373 | 26.4k | case tok::annot_module_begin: |
374 | 26.4k | ++NestedModules; |
375 | 26.4k | ParseTopLevelDecl(); |
376 | 26.4k | continue; |
377 | | |
378 | 26.4k | case tok::annot_module_end: |
379 | 26.4k | if (!NestedModules) |
380 | 2 | break; |
381 | 26.4k | --NestedModules; |
382 | 26.4k | ParseTopLevelDecl(); |
383 | 26.4k | continue; |
384 | | |
385 | 6.79k | case tok::annot_module_include: |
386 | 6.79k | ParseTopLevelDecl(); |
387 | 6.79k | continue; |
388 | | |
389 | 1 | case tok::eof: |
390 | 1 | break; |
391 | | |
392 | 63.6k | case tok::r_brace: |
393 | 63.6k | if (!NestedModules) |
394 | 63.6k | break; |
395 | 2 | LLVM_FALLTHROUGH; |
396 | 1.04M | default: |
397 | 1.04M | ParsedAttributesWithRange attrs(AttrFactory); |
398 | 1.04M | MaybeParseCXX11Attributes(attrs); |
399 | 1.04M | ParseExternalDeclaration(attrs); |
400 | 1.04M | continue; |
401 | 63.6k | } |
402 | | |
403 | 63.6k | break; |
404 | 63.6k | } |
405 | | |
406 | 63.6k | T.consumeClose(); |
407 | 63.6k | return LinkageSpec ? Actions.ActOnFinishLinkageSpecification( |
408 | 63.6k | getCurScope(), LinkageSpec, T.getCloseLocation()) |
409 | 5 | : nullptr; |
410 | 63.6k | } |
411 | | |
412 | | /// Parse a C++ Modules TS export-declaration. |
413 | | /// |
414 | | /// export-declaration: |
415 | | /// 'export' declaration |
416 | | /// 'export' '{' declaration-seq[opt] '}' |
417 | | /// |
418 | 103 | Decl *Parser::ParseExportDeclaration() { |
419 | 103 | assert(Tok.is(tok::kw_export)); |
420 | 103 | SourceLocation ExportLoc = ConsumeToken(); |
421 | | |
422 | 103 | ParseScope ExportScope(this, Scope::DeclScope); |
423 | 103 | Decl *ExportDecl = Actions.ActOnStartExportDecl( |
424 | 103 | getCurScope(), ExportLoc, |
425 | 83 | Tok.is(tok::l_brace) ? Tok.getLocation()20 : SourceLocation()); |
426 | | |
427 | 103 | if (Tok.isNot(tok::l_brace)) { |
428 | | // FIXME: Factor out a ParseExternalDeclarationWithAttrs. |
429 | 83 | ParsedAttributesWithRange Attrs(AttrFactory); |
430 | 83 | MaybeParseCXX11Attributes(Attrs); |
431 | 83 | MaybeParseMicrosoftAttributes(Attrs); |
432 | 83 | ParseExternalDeclaration(Attrs); |
433 | 83 | return Actions.ActOnFinishExportDecl(getCurScope(), ExportDecl, |
434 | 83 | SourceLocation()); |
435 | 83 | } |
436 | | |
437 | 20 | BalancedDelimiterTracker T(*this, tok::l_brace); |
438 | 20 | T.consumeOpen(); |
439 | | |
440 | | // The Modules TS draft says "An export-declaration shall declare at least one |
441 | | // entity", but the intent is that it shall contain at least one declaration. |
442 | 20 | if (Tok.is(tok::r_brace) && getLangOpts().ModulesTS1 ) { |
443 | 1 | Diag(ExportLoc, diag::err_export_empty) |
444 | 1 | << SourceRange(ExportLoc, Tok.getLocation()); |
445 | 1 | } |
446 | | |
447 | 109 | while (!tryParseMisplacedModuleImport() && Tok.isNot(tok::r_brace) && |
448 | 89 | Tok.isNot(tok::eof)) { |
449 | 89 | ParsedAttributesWithRange Attrs(AttrFactory); |
450 | 89 | MaybeParseCXX11Attributes(Attrs); |
451 | 89 | MaybeParseMicrosoftAttributes(Attrs); |
452 | 89 | ParseExternalDeclaration(Attrs); |
453 | 89 | } |
454 | | |
455 | 20 | T.consumeClose(); |
456 | 20 | return Actions.ActOnFinishExportDecl(getCurScope(), ExportDecl, |
457 | 20 | T.getCloseLocation()); |
458 | 20 | } |
459 | | |
460 | | /// ParseUsingDirectiveOrDeclaration - Parse C++ using using-declaration or |
461 | | /// using-directive. Assumes that current token is 'using'. |
462 | | Parser::DeclGroupPtrTy |
463 | | Parser::ParseUsingDirectiveOrDeclaration(DeclaratorContext Context, |
464 | | const ParsedTemplateInfo &TemplateInfo, |
465 | | SourceLocation &DeclEnd, |
466 | 158k | ParsedAttributesWithRange &attrs) { |
467 | 158k | assert(Tok.is(tok::kw_using) && "Not using token"); |
468 | 158k | ObjCDeclContextSwitch ObjCDC(*this); |
469 | | |
470 | | // Eat 'using'. |
471 | 158k | SourceLocation UsingLoc = ConsumeToken(); |
472 | | |
473 | 158k | if (Tok.is(tok::code_completion)) { |
474 | 1 | Actions.CodeCompleteUsing(getCurScope()); |
475 | 1 | cutOffParsing(); |
476 | 1 | return nullptr; |
477 | 1 | } |
478 | | |
479 | | // Consume unexpected 'template' keywords. |
480 | 158k | while (158k Tok.is(tok::kw_template)) { |
481 | 5 | SourceLocation TemplateLoc = ConsumeToken(); |
482 | 5 | Diag(TemplateLoc, diag::err_unexpected_template_after_using) |
483 | 5 | << FixItHint::CreateRemoval(TemplateLoc); |
484 | 5 | } |
485 | | |
486 | | // 'using namespace' means this is a using-directive. |
487 | 158k | if (Tok.is(tok::kw_namespace)) { |
488 | | // Template parameters are always an error here. |
489 | 2.37k | if (TemplateInfo.Kind) { |
490 | 1 | SourceRange R = TemplateInfo.getSourceRange(); |
491 | 1 | Diag(UsingLoc, diag::err_templated_using_directive_declaration) |
492 | 1 | << 0 /* directive */ << R << FixItHint::CreateRemoval(R); |
493 | 1 | } |
494 | | |
495 | 2.37k | Decl *UsingDir = ParseUsingDirective(Context, UsingLoc, DeclEnd, attrs); |
496 | 2.37k | return Actions.ConvertDeclToDeclGroup(UsingDir); |
497 | 2.37k | } |
498 | | |
499 | | // Otherwise, it must be a using-declaration or an alias-declaration. |
500 | | |
501 | | // Using declarations can't have attributes. |
502 | 155k | ProhibitAttributes(attrs); |
503 | | |
504 | 155k | return ParseUsingDeclaration(Context, TemplateInfo, UsingLoc, DeclEnd, |
505 | 155k | AS_none); |
506 | 155k | } |
507 | | |
508 | | /// ParseUsingDirective - Parse C++ using-directive, assumes |
509 | | /// that current token is 'namespace' and 'using' was already parsed. |
510 | | /// |
511 | | /// using-directive: [C++ 7.3.p4: namespace.udir] |
512 | | /// 'using' 'namespace' ::[opt] nested-name-specifier[opt] |
513 | | /// namespace-name ; |
514 | | /// [GNU] using-directive: |
515 | | /// 'using' 'namespace' ::[opt] nested-name-specifier[opt] |
516 | | /// namespace-name attributes[opt] ; |
517 | | /// |
518 | | Decl *Parser::ParseUsingDirective(DeclaratorContext Context, |
519 | | SourceLocation UsingLoc, |
520 | | SourceLocation &DeclEnd, |
521 | 2.37k | ParsedAttributes &attrs) { |
522 | 2.37k | assert(Tok.is(tok::kw_namespace) && "Not 'namespace' token"); |
523 | | |
524 | | // Eat 'namespace'. |
525 | 2.37k | SourceLocation NamespcLoc = ConsumeToken(); |
526 | | |
527 | 2.37k | if (Tok.is(tok::code_completion)) { |
528 | 1 | Actions.CodeCompleteUsingDirective(getCurScope()); |
529 | 1 | cutOffParsing(); |
530 | 1 | return nullptr; |
531 | 1 | } |
532 | | |
533 | 2.36k | CXXScopeSpec SS; |
534 | | // Parse (optional) nested-name-specifier. |
535 | 2.36k | ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/nullptr, |
536 | 2.36k | /*ObjectHadErrors=*/false, |
537 | 2.36k | /*EnteringContext=*/false, |
538 | 2.36k | /*MayBePseudoDestructor=*/nullptr, |
539 | 2.36k | /*IsTypename=*/false, |
540 | 2.36k | /*LastII=*/nullptr, |
541 | 2.36k | /*OnlyNamespace=*/true); |
542 | | |
543 | 2.36k | IdentifierInfo *NamespcName = nullptr; |
544 | 2.36k | SourceLocation IdentLoc = SourceLocation(); |
545 | | |
546 | | // Parse namespace-name. |
547 | 2.36k | if (Tok.isNot(tok::identifier)) { |
548 | 1 | Diag(Tok, diag::err_expected_namespace_name); |
549 | | // If there was invalid namespace name, skip to end of decl, and eat ';'. |
550 | 1 | SkipUntil(tok::semi); |
551 | | // FIXME: Are there cases, when we would like to call ActOnUsingDirective? |
552 | 1 | return nullptr; |
553 | 1 | } |
554 | | |
555 | 2.36k | if (SS.isInvalid()) { |
556 | | // Diagnostics have been emitted in ParseOptionalCXXScopeSpecifier. |
557 | | // Skip to end of the definition and eat the ';'. |
558 | 4 | SkipUntil(tok::semi); |
559 | 4 | return nullptr; |
560 | 4 | } |
561 | | |
562 | | // Parse identifier. |
563 | 2.36k | NamespcName = Tok.getIdentifierInfo(); |
564 | 2.36k | IdentLoc = ConsumeToken(); |
565 | | |
566 | | // Parse (optional) attributes (most likely GNU strong-using extension). |
567 | 2.36k | bool GNUAttr = false; |
568 | 2.36k | if (Tok.is(tok::kw___attribute)) { |
569 | 0 | GNUAttr = true; |
570 | 0 | ParseGNUAttributes(attrs); |
571 | 0 | } |
572 | | |
573 | | // Eat ';'. |
574 | 2.36k | DeclEnd = Tok.getLocation(); |
575 | 2.36k | if (ExpectAndConsume(tok::semi, |
576 | 0 | GNUAttr ? diag::err_expected_semi_after_attribute_list |
577 | 2.36k | : diag::err_expected_semi_after_namespace_name)) |
578 | 1 | SkipUntil(tok::semi); |
579 | | |
580 | 2.36k | return Actions.ActOnUsingDirective(getCurScope(), UsingLoc, NamespcLoc, SS, |
581 | 2.36k | IdentLoc, NamespcName, attrs); |
582 | 2.36k | } |
583 | | |
584 | | /// Parse a using-declarator (or the identifier in a C++11 alias-declaration). |
585 | | /// |
586 | | /// using-declarator: |
587 | | /// 'typename'[opt] nested-name-specifier unqualified-id |
588 | | /// |
589 | | bool Parser::ParseUsingDeclarator(DeclaratorContext Context, |
590 | 202k | UsingDeclarator &D) { |
591 | 202k | D.clear(); |
592 | | |
593 | | // Ignore optional 'typename'. |
594 | | // FIXME: This is wrong; we should parse this as a typename-specifier. |
595 | 202k | TryConsumeToken(tok::kw_typename, D.TypenameLoc); |
596 | | |
597 | 202k | if (Tok.is(tok::kw___super)) { |
598 | 1 | Diag(Tok.getLocation(), diag::err_super_in_using_declaration); |
599 | 1 | return true; |
600 | 1 | } |
601 | | |
602 | | // Parse nested-name-specifier. |
603 | 202k | IdentifierInfo *LastII = nullptr; |
604 | 202k | if (ParseOptionalCXXScopeSpecifier(D.SS, /*ObjectType=*/nullptr, |
605 | 202k | /*ObjectHadErrors=*/false, |
606 | 202k | /*EnteringContext=*/false, |
607 | 202k | /*MayBePseudoDtor=*/nullptr, |
608 | 202k | /*IsTypename=*/false, |
609 | 202k | /*LastII=*/&LastII, |
610 | 202k | /*OnlyNamespace=*/false, |
611 | 202k | /*InUsingDeclaration=*/true)) |
612 | | |
613 | 0 | return true; |
614 | 202k | if (D.SS.isInvalid()) |
615 | 4 | return true; |
616 | | |
617 | | // Parse the unqualified-id. We allow parsing of both constructor and |
618 | | // destructor names and allow the action module to diagnose any semantic |
619 | | // errors. |
620 | | // |
621 | | // C++11 [class.qual]p2: |
622 | | // [...] in a using-declaration that is a member-declaration, if the name |
623 | | // specified after the nested-name-specifier is the same as the identifier |
624 | | // or the simple-template-id's template-name in the last component of the |
625 | | // nested-name-specifier, the name is [...] considered to name the |
626 | | // constructor. |
627 | 202k | if (getLangOpts().CPlusPlus11 && Context == DeclaratorContext::Member201k && |
628 | 46.9k | Tok.is(tok::identifier) && |
629 | 46.7k | (NextToken().is(tok::semi) || NextToken().is(tok::comma)45.5k || |
630 | 45.5k | NextToken().is(tok::ellipsis)) && |
631 | 1.20k | D.SS.isNotEmpty() && LastII == Tok.getIdentifierInfo() && |
632 | 421 | !D.SS.getScopeRep()->getAsNamespace() && |
633 | 420 | !D.SS.getScopeRep()->getAsNamespaceAlias()) { |
634 | 420 | SourceLocation IdLoc = ConsumeToken(); |
635 | 420 | ParsedType Type = |
636 | 420 | Actions.getInheritingConstructorName(D.SS, IdLoc, *LastII); |
637 | 420 | D.Name.setConstructorName(Type, IdLoc, IdLoc); |
638 | 202k | } else { |
639 | 202k | if (ParseUnqualifiedId( |
640 | 202k | D.SS, /*ObjectType=*/nullptr, |
641 | 202k | /*ObjectHadErrors=*/false, /*EnteringContext=*/false, |
642 | 202k | /*AllowDestructorName=*/true, |
643 | | /*AllowConstructorName=*/ |
644 | 202k | !(Tok.is(tok::identifier) && NextToken().is(tok::equal)202k ), |
645 | 202k | /*AllowDeductionGuide=*/false, nullptr, D.Name)) |
646 | 16 | return true; |
647 | 202k | } |
648 | | |
649 | 202k | if (TryConsumeToken(tok::ellipsis, D.EllipsisLoc)) |
650 | 22 | Diag(Tok.getLocation(), getLangOpts().CPlusPlus17 ? |
651 | 22 | diag::warn_cxx17_compat_using_declaration_pack : |
652 | 0 | diag::ext_using_declaration_pack); |
653 | | |
654 | 202k | return false; |
655 | 202k | } |
656 | | |
657 | | /// ParseUsingDeclaration - Parse C++ using-declaration or alias-declaration. |
658 | | /// Assumes that 'using' was already seen. |
659 | | /// |
660 | | /// using-declaration: [C++ 7.3.p3: namespace.udecl] |
661 | | /// 'using' using-declarator-list[opt] ; |
662 | | /// |
663 | | /// using-declarator-list: [C++1z] |
664 | | /// using-declarator '...'[opt] |
665 | | /// using-declarator-list ',' using-declarator '...'[opt] |
666 | | /// |
667 | | /// using-declarator-list: [C++98-14] |
668 | | /// using-declarator |
669 | | /// |
670 | | /// alias-declaration: C++11 [dcl.dcl]p1 |
671 | | /// 'using' identifier attribute-specifier-seq[opt] = type-id ; |
672 | | /// |
673 | | Parser::DeclGroupPtrTy |
674 | | Parser::ParseUsingDeclaration(DeclaratorContext Context, |
675 | | const ParsedTemplateInfo &TemplateInfo, |
676 | | SourceLocation UsingLoc, SourceLocation &DeclEnd, |
677 | 202k | AccessSpecifier AS) { |
678 | | // Check for misplaced attributes before the identifier in an |
679 | | // alias-declaration. |
680 | 202k | ParsedAttributesWithRange MisplacedAttrs(AttrFactory); |
681 | 202k | MaybeParseCXX11Attributes(MisplacedAttrs); |
682 | | |
683 | 202k | UsingDeclarator D; |
684 | 202k | bool InvalidDeclarator = ParseUsingDeclarator(Context, D); |
685 | | |
686 | 202k | ParsedAttributesWithRange Attrs(AttrFactory); |
687 | 202k | MaybeParseGNUAttributes(Attrs); |
688 | 202k | MaybeParseCXX11Attributes(Attrs); |
689 | | |
690 | | // Maybe this is an alias-declaration. |
691 | 202k | if (Tok.is(tok::equal)) { |
692 | 73.8k | if (InvalidDeclarator) { |
693 | 0 | SkipUntil(tok::semi); |
694 | 0 | return nullptr; |
695 | 0 | } |
696 | | |
697 | | // If we had any misplaced attributes from earlier, this is where they |
698 | | // should have been written. |
699 | 73.8k | if (MisplacedAttrs.Range.isValid()) { |
700 | 2 | Diag(MisplacedAttrs.Range.getBegin(), diag::err_attributes_not_allowed) |
701 | 2 | << FixItHint::CreateInsertionFromRange( |
702 | 2 | Tok.getLocation(), |
703 | 2 | CharSourceRange::getTokenRange(MisplacedAttrs.Range)) |
704 | 2 | << FixItHint::CreateRemoval(MisplacedAttrs.Range); |
705 | 2 | Attrs.takeAllFrom(MisplacedAttrs); |
706 | 2 | } |
707 | | |
708 | 73.8k | Decl *DeclFromDeclSpec = nullptr; |
709 | 73.8k | Decl *AD = ParseAliasDeclarationAfterDeclarator( |
710 | 73.8k | TemplateInfo, UsingLoc, D, DeclEnd, AS, Attrs, &DeclFromDeclSpec); |
711 | 73.8k | return Actions.ConvertDeclToDeclGroup(AD, DeclFromDeclSpec); |
712 | 73.8k | } |
713 | | |
714 | | // C++11 attributes are not allowed on a using-declaration, but GNU ones |
715 | | // are. |
716 | 129k | ProhibitAttributes(MisplacedAttrs); |
717 | 129k | ProhibitAttributes(Attrs); |
718 | | |
719 | | // Diagnose an attempt to declare a templated using-declaration. |
720 | | // In C++11, alias-declarations can be templates: |
721 | | // template <...> using id = type; |
722 | 129k | if (TemplateInfo.Kind) { |
723 | 4 | SourceRange R = TemplateInfo.getSourceRange(); |
724 | 4 | Diag(UsingLoc, diag::err_templated_using_directive_declaration) |
725 | 4 | << 1 /* declaration */ << R << FixItHint::CreateRemoval(R); |
726 | | |
727 | | // Unfortunately, we have to bail out instead of recovering by |
728 | | // ignoring the parameters, just in case the nested name specifier |
729 | | // depends on the parameters. |
730 | 4 | return nullptr; |
731 | 4 | } |
732 | | |
733 | 129k | SmallVector<Decl *, 8> DeclsInGroup; |
734 | 129k | while (true) { |
735 | | // Parse (optional) attributes (most likely GNU strong-using extension). |
736 | 129k | MaybeParseGNUAttributes(Attrs); |
737 | | |
738 | 129k | if (InvalidDeclarator) |
739 | 21 | SkipUntil(tok::comma, tok::semi, StopBeforeMatch); |
740 | 129k | else { |
741 | | // "typename" keyword is allowed for identifiers only, |
742 | | // because it may be a type definition. |
743 | 129k | if (D.TypenameLoc.isValid() && |
744 | 381 | D.Name.getKind() != UnqualifiedIdKind::IK_Identifier) { |
745 | 6 | Diag(D.Name.getSourceRange().getBegin(), |
746 | 6 | diag::err_typename_identifiers_only) |
747 | 6 | << FixItHint::CreateRemoval(SourceRange(D.TypenameLoc)); |
748 | | // Proceed parsing, but discard the typename keyword. |
749 | 6 | D.TypenameLoc = SourceLocation(); |
750 | 6 | } |
751 | | |
752 | 129k | Decl *UD = Actions.ActOnUsingDeclaration(getCurScope(), AS, UsingLoc, |
753 | 129k | D.TypenameLoc, D.SS, D.Name, |
754 | 129k | D.EllipsisLoc, Attrs); |
755 | 129k | if (UD) |
756 | 128k | DeclsInGroup.push_back(UD); |
757 | 129k | } |
758 | | |
759 | 129k | if (!TryConsumeToken(tok::comma)) |
760 | 129k | break; |
761 | | |
762 | | // Parse another using-declarator. |
763 | 8 | Attrs.clear(); |
764 | 8 | InvalidDeclarator = ParseUsingDeclarator(Context, D); |
765 | 8 | } |
766 | | |
767 | 129k | if (DeclsInGroup.size() > 1) |
768 | 2 | Diag(Tok.getLocation(), getLangOpts().CPlusPlus17 ? |
769 | 2 | diag::warn_cxx17_compat_multi_using_declaration : |
770 | 0 | diag::ext_multi_using_declaration); |
771 | | |
772 | | // Eat ';'. |
773 | 129k | DeclEnd = Tok.getLocation(); |
774 | 129k | if (ExpectAndConsume(tok::semi, diag::err_expected_after, |
775 | 0 | !Attrs.empty() ? "attributes list" |
776 | 129k | : "using declaration")) |
777 | 3 | SkipUntil(tok::semi); |
778 | | |
779 | 129k | return Actions.BuildDeclaratorGroup(DeclsInGroup); |
780 | 129k | } |
781 | | |
782 | | Decl *Parser::ParseAliasDeclarationAfterDeclarator( |
783 | | const ParsedTemplateInfo &TemplateInfo, SourceLocation UsingLoc, |
784 | | UsingDeclarator &D, SourceLocation &DeclEnd, AccessSpecifier AS, |
785 | 73.8k | ParsedAttributes &Attrs, Decl **OwnedType) { |
786 | 73.8k | if (ExpectAndConsume(tok::equal)) { |
787 | 0 | SkipUntil(tok::semi); |
788 | 0 | return nullptr; |
789 | 0 | } |
790 | | |
791 | 73.8k | Diag(Tok.getLocation(), getLangOpts().CPlusPlus11 ? |
792 | 73.7k | diag::warn_cxx98_compat_alias_declaration : |
793 | 27 | diag::ext_alias_declaration); |
794 | | |
795 | | // Type alias templates cannot be specialized. |
796 | 73.8k | int SpecKind = -1; |
797 | 73.8k | if (TemplateInfo.Kind == ParsedTemplateInfo::Template && |
798 | 44.1k | D.Name.getKind() == UnqualifiedIdKind::IK_TemplateId) |
799 | 1 | SpecKind = 0; |
800 | 73.8k | if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization) |
801 | 1 | SpecKind = 1; |
802 | 73.8k | if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) |
803 | 1 | SpecKind = 2; |
804 | 73.8k | if (SpecKind != -1) { |
805 | 3 | SourceRange Range; |
806 | 3 | if (SpecKind == 0) |
807 | 1 | Range = SourceRange(D.Name.TemplateId->LAngleLoc, |
808 | 1 | D.Name.TemplateId->RAngleLoc); |
809 | 2 | else |
810 | 2 | Range = TemplateInfo.getSourceRange(); |
811 | 3 | Diag(Range.getBegin(), diag::err_alias_declaration_specialization) |
812 | 3 | << SpecKind << Range; |
813 | 3 | SkipUntil(tok::semi); |
814 | 3 | return nullptr; |
815 | 3 | } |
816 | | |
817 | | // Name must be an identifier. |
818 | 73.8k | if (D.Name.getKind() != UnqualifiedIdKind::IK_Identifier) { |
819 | 5 | Diag(D.Name.StartLocation, diag::err_alias_declaration_not_identifier); |
820 | | // No removal fixit: can't recover from this. |
821 | 5 | SkipUntil(tok::semi); |
822 | 5 | return nullptr; |
823 | 73.8k | } else if (D.TypenameLoc.isValid()) |
824 | 8 | Diag(D.TypenameLoc, diag::err_alias_declaration_not_identifier) |
825 | 8 | << FixItHint::CreateRemoval(SourceRange( |
826 | 8 | D.TypenameLoc, |
827 | 4 | D.SS.isNotEmpty() ? D.SS.getEndLoc() : D.TypenameLoc)); |
828 | 73.8k | else if (D.SS.isNotEmpty()) |
829 | 4 | Diag(D.SS.getBeginLoc(), diag::err_alias_declaration_not_identifier) |
830 | 4 | << FixItHint::CreateRemoval(D.SS.getRange()); |
831 | 73.8k | if (D.EllipsisLoc.isValid()) |
832 | 0 | Diag(D.EllipsisLoc, diag::err_alias_declaration_pack_expansion) |
833 | 0 | << FixItHint::CreateRemoval(SourceRange(D.EllipsisLoc)); |
834 | | |
835 | 73.8k | Decl *DeclFromDeclSpec = nullptr; |
836 | 73.8k | TypeResult TypeAlias = |
837 | 73.8k | ParseTypeName(nullptr, |
838 | 44.1k | TemplateInfo.Kind ? DeclaratorContext::AliasTemplate |
839 | 29.6k | : DeclaratorContext::AliasDecl, |
840 | 73.8k | AS, &DeclFromDeclSpec, &Attrs); |
841 | 73.8k | if (OwnedType) |
842 | 73.8k | *OwnedType = DeclFromDeclSpec; |
843 | | |
844 | | // Eat ';'. |
845 | 73.8k | DeclEnd = Tok.getLocation(); |
846 | 73.8k | if (ExpectAndConsume(tok::semi, diag::err_expected_after, |
847 | 30.0k | !Attrs.empty() ? "attributes list" |
848 | 43.7k | : "alias declaration")) |
849 | 8 | SkipUntil(tok::semi); |
850 | | |
851 | 73.8k | TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams; |
852 | 73.8k | MultiTemplateParamsArg TemplateParamsArg( |
853 | 44.1k | TemplateParams ? TemplateParams->data() : nullptr29.6k , |
854 | 44.1k | TemplateParams ? TemplateParams->size() : 029.6k ); |
855 | 73.8k | return Actions.ActOnAliasDeclaration(getCurScope(), AS, TemplateParamsArg, |
856 | 73.8k | UsingLoc, D.Name, Attrs, TypeAlias, |
857 | 73.8k | DeclFromDeclSpec); |
858 | 73.8k | } |
859 | | |
860 | | /// ParseStaticAssertDeclaration - Parse C++0x or C11 static_assert-declaration. |
861 | | /// |
862 | | /// [C++0x] static_assert-declaration: |
863 | | /// static_assert ( constant-expression , string-literal ) ; |
864 | | /// |
865 | | /// [C11] static_assert-declaration: |
866 | | /// _Static_assert ( constant-expression , string-literal ) ; |
867 | | /// |
868 | 42.9k | Decl *Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){ |
869 | 42.9k | assert(Tok.isOneOf(tok::kw_static_assert, tok::kw__Static_assert) && |
870 | 42.9k | "Not a static_assert declaration"); |
871 | | |
872 | 42.9k | if (Tok.is(tok::kw__Static_assert) && !getLangOpts().C112.68k ) |
873 | 2.12k | Diag(Tok, diag::ext_c11_feature) << Tok.getName(); |
874 | 42.9k | if (Tok.is(tok::kw_static_assert)) |
875 | 40.2k | Diag(Tok, diag::warn_cxx98_compat_static_assert); |
876 | | |
877 | 42.9k | SourceLocation StaticAssertLoc = ConsumeToken(); |
878 | | |
879 | 42.9k | BalancedDelimiterTracker T(*this, tok::l_paren); |
880 | 42.9k | if (T.consumeOpen()) { |
881 | 0 | Diag(Tok, diag::err_expected) << tok::l_paren; |
882 | 0 | SkipMalformedDecl(); |
883 | 0 | return nullptr; |
884 | 0 | } |
885 | | |
886 | 42.9k | EnterExpressionEvaluationContext ConstantEvaluated( |
887 | 42.9k | Actions, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
888 | 42.9k | ExprResult AssertExpr(ParseConstantExpressionInExprEvalContext()); |
889 | 42.9k | if (AssertExpr.isInvalid()) { |
890 | 89 | SkipMalformedDecl(); |
891 | 89 | return nullptr; |
892 | 89 | } |
893 | | |
894 | 42.8k | ExprResult AssertMessage; |
895 | 42.8k | if (Tok.is(tok::r_paren)) { |
896 | 4.06k | Diag(Tok, getLangOpts().CPlusPlus17 |
897 | 4.04k | ? diag::warn_cxx14_compat_static_assert_no_message |
898 | 14 | : diag::ext_static_assert_no_message) |
899 | 4.06k | << (getLangOpts().CPlusPlus17 |
900 | 4.04k | ? FixItHint() |
901 | 14 | : FixItHint::CreateInsertion(Tok.getLocation(), ", \"\"")); |
902 | 38.8k | } else { |
903 | 38.8k | if (ExpectAndConsume(tok::comma)) { |
904 | 0 | SkipUntil(tok::semi); |
905 | 0 | return nullptr; |
906 | 0 | } |
907 | | |
908 | 38.8k | if (!isTokenStringLiteral()) { |
909 | 3 | Diag(Tok, diag::err_expected_string_literal) |
910 | 3 | << /*Source='static_assert'*/1; |
911 | 3 | SkipMalformedDecl(); |
912 | 3 | return nullptr; |
913 | 3 | } |
914 | | |
915 | 38.8k | AssertMessage = ParseStringLiteralExpression(); |
916 | 38.8k | if (AssertMessage.isInvalid()) { |
917 | 1 | SkipMalformedDecl(); |
918 | 1 | return nullptr; |
919 | 1 | } |
920 | 42.8k | } |
921 | | |
922 | 42.8k | T.consumeClose(); |
923 | | |
924 | 42.8k | DeclEnd = Tok.getLocation(); |
925 | 42.8k | ExpectAndConsumeSemi(diag::err_expected_semi_after_static_assert); |
926 | | |
927 | 42.8k | return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc, |
928 | 42.8k | AssertExpr.get(), |
929 | 42.8k | AssertMessage.get(), |
930 | 42.8k | T.getCloseLocation()); |
931 | 42.8k | } |
932 | | |
933 | | /// ParseDecltypeSpecifier - Parse a C++11 decltype specifier. |
934 | | /// |
935 | | /// 'decltype' ( expression ) |
936 | | /// 'decltype' ( 'auto' ) [C++1y] |
937 | | /// |
938 | 67.4k | SourceLocation Parser::ParseDecltypeSpecifier(DeclSpec &DS) { |
939 | 67.4k | assert(Tok.isOneOf(tok::kw_decltype, tok::annot_decltype) |
940 | 67.4k | && "Not a decltype specifier"); |
941 | | |
942 | 67.4k | ExprResult Result; |
943 | 67.4k | SourceLocation StartLoc = Tok.getLocation(); |
944 | 67.4k | SourceLocation EndLoc; |
945 | | |
946 | 67.4k | if (Tok.is(tok::annot_decltype)) { |
947 | 33.2k | Result = getExprAnnotation(Tok); |
948 | 33.2k | EndLoc = Tok.getAnnotationEndLoc(); |
949 | 33.2k | ConsumeAnnotationToken(); |
950 | 33.2k | if (Result.isInvalid()) { |
951 | 42 | DS.SetTypeSpecError(); |
952 | 42 | return EndLoc; |
953 | 42 | } |
954 | 34.2k | } else { |
955 | 34.2k | if (Tok.getIdentifierInfo()->isStr("decltype")) |
956 | 34.2k | Diag(Tok, diag::warn_cxx98_compat_decltype); |
957 | | |
958 | 34.2k | ConsumeToken(); |
959 | | |
960 | 34.2k | BalancedDelimiterTracker T(*this, tok::l_paren); |
961 | 34.2k | if (T.expectAndConsume(diag::err_expected_lparen_after, |
962 | 1 | "decltype", tok::r_paren)) { |
963 | 1 | DS.SetTypeSpecError(); |
964 | 1 | return T.getOpenLocation() == Tok.getLocation() ? |
965 | 1 | StartLoc : T.getOpenLocation()0 ; |
966 | 1 | } |
967 | | |
968 | | // Check for C++1y 'decltype(auto)'. |
969 | 34.2k | if (Tok.is(tok::kw_auto)) { |
970 | | // No need to disambiguate here: an expression can't start with 'auto', |
971 | | // because the typename-specifier in a function-style cast operation can't |
972 | | // be 'auto'. |
973 | 267 | Diag(Tok.getLocation(), |
974 | 267 | getLangOpts().CPlusPlus14 |
975 | 250 | ? diag::warn_cxx11_compat_decltype_auto_type_specifier |
976 | 17 | : diag::ext_decltype_auto_type_specifier); |
977 | 267 | ConsumeToken(); |
978 | 33.9k | } else { |
979 | | // Parse the expression |
980 | | |
981 | | // C++11 [dcl.type.simple]p4: |
982 | | // The operand of the decltype specifier is an unevaluated operand. |
983 | 33.9k | EnterExpressionEvaluationContext Unevaluated( |
984 | 33.9k | Actions, Sema::ExpressionEvaluationContext::Unevaluated, nullptr, |
985 | 33.9k | Sema::ExpressionEvaluationContextRecord::EK_Decltype); |
986 | 33.9k | Result = Actions.CorrectDelayedTyposInExpr( |
987 | 33.9k | ParseExpression(), /*InitDecl=*/nullptr, |
988 | 33.9k | /*RecoverUncorrectedTypos=*/false, |
989 | 9 | [](Expr *E) { return E->hasPlaceholderType() ? ExprError()3 : E6 ; }); |
990 | 33.9k | if (Result.isInvalid()) { |
991 | 37 | DS.SetTypeSpecError(); |
992 | 37 | if (SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch)) { |
993 | 34 | EndLoc = ConsumeParen(); |
994 | 3 | } else { |
995 | 3 | if (PP.isBacktrackEnabled() && Tok.is(tok::semi)1 ) { |
996 | | // Backtrack to get the location of the last token before the semi. |
997 | 1 | PP.RevertCachedTokens(2); |
998 | 1 | ConsumeToken(); // the semi. |
999 | 1 | EndLoc = ConsumeAnyToken(); |
1000 | 1 | assert(Tok.is(tok::semi)); |
1001 | 2 | } else { |
1002 | 2 | EndLoc = Tok.getLocation(); |
1003 | 2 | } |
1004 | 3 | } |
1005 | 37 | return EndLoc; |
1006 | 37 | } |
1007 | | |
1008 | 33.9k | Result = Actions.ActOnDecltypeExpression(Result.get()); |
1009 | 33.9k | } |
1010 | | |
1011 | | // Match the ')' |
1012 | 34.2k | T.consumeClose(); |
1013 | 34.2k | if (T.getCloseLocation().isInvalid()) { |
1014 | 0 | DS.SetTypeSpecError(); |
1015 | | // FIXME: this should return the location of the last token |
1016 | | // that was consumed (by "consumeClose()") |
1017 | 0 | return T.getCloseLocation(); |
1018 | 0 | } |
1019 | | |
1020 | 34.2k | if (Result.isInvalid()) { |
1021 | 9 | DS.SetTypeSpecError(); |
1022 | 9 | return T.getCloseLocation(); |
1023 | 9 | } |
1024 | | |
1025 | 34.2k | EndLoc = T.getCloseLocation(); |
1026 | 34.2k | } |
1027 | 67.3k | assert(!Result.isInvalid()); |
1028 | | |
1029 | 67.3k | const char *PrevSpec = nullptr; |
1030 | 67.3k | unsigned DiagID; |
1031 | 67.3k | const PrintingPolicy &Policy = Actions.getASTContext().getPrintingPolicy(); |
1032 | | // Check for duplicate type specifiers (e.g. "int decltype(a)"). |
1033 | 67.3k | if (Result.get() |
1034 | 66.8k | ? DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec, |
1035 | 66.8k | DiagID, Result.get(), Policy) |
1036 | 530 | : DS.SetTypeSpecType(DeclSpec::TST_decltype_auto, StartLoc, PrevSpec, |
1037 | 0 | DiagID, Policy)) { |
1038 | 0 | Diag(StartLoc, DiagID) << PrevSpec; |
1039 | 0 | DS.SetTypeSpecError(); |
1040 | 0 | } |
1041 | 67.3k | return EndLoc; |
1042 | 67.4k | } |
1043 | | |
1044 | | void Parser::AnnotateExistingDecltypeSpecifier(const DeclSpec& DS, |
1045 | | SourceLocation StartLoc, |
1046 | 33.2k | SourceLocation EndLoc) { |
1047 | | // make sure we have a token we can turn into an annotation token |
1048 | 33.2k | if (PP.isBacktrackEnabled()) { |
1049 | 174 | PP.RevertCachedTokens(1); |
1050 | 174 | if (DS.getTypeSpecType() == TST_error) { |
1051 | | // We encountered an error in parsing 'decltype(...)' so lets annotate all |
1052 | | // the tokens in the backtracking cache - that we likely had to skip over |
1053 | | // to get to a token that allows us to resume parsing, such as a |
1054 | | // semi-colon. |
1055 | 3 | EndLoc = PP.getLastCachedTokenLocation(); |
1056 | 3 | } |
1057 | 174 | } |
1058 | 33.0k | else |
1059 | 33.0k | PP.EnterToken(Tok, /*IsReinject*/true); |
1060 | | |
1061 | 33.2k | Tok.setKind(tok::annot_decltype); |
1062 | 33.2k | setExprAnnotation(Tok, |
1063 | 32.9k | DS.getTypeSpecType() == TST_decltype ? DS.getRepAsExpr() : |
1064 | 306 | DS.getTypeSpecType() == TST_decltype_auto ? ExprResult()263 : |
1065 | 43 | ExprError()); |
1066 | 33.2k | Tok.setAnnotationEndLoc(EndLoc); |
1067 | 33.2k | Tok.setLocation(StartLoc); |
1068 | 33.2k | PP.AnnotateCachedTokens(Tok); |
1069 | 33.2k | } |
1070 | | |
1071 | 497 | void Parser::ParseUnderlyingTypeSpecifier(DeclSpec &DS) { |
1072 | 497 | assert(Tok.is(tok::kw___underlying_type) && |
1073 | 497 | "Not an underlying type specifier"); |
1074 | | |
1075 | 497 | SourceLocation StartLoc = ConsumeToken(); |
1076 | 497 | BalancedDelimiterTracker T(*this, tok::l_paren); |
1077 | 497 | if (T.expectAndConsume(diag::err_expected_lparen_after, |
1078 | 0 | "__underlying_type", tok::r_paren)) { |
1079 | 0 | return; |
1080 | 0 | } |
1081 | | |
1082 | 497 | TypeResult Result = ParseTypeName(); |
1083 | 497 | if (Result.isInvalid()) { |
1084 | 0 | SkipUntil(tok::r_paren, StopAtSemi); |
1085 | 0 | return; |
1086 | 0 | } |
1087 | | |
1088 | | // Match the ')' |
1089 | 497 | T.consumeClose(); |
1090 | 497 | if (T.getCloseLocation().isInvalid()) |
1091 | 0 | return; |
1092 | | |
1093 | 497 | const char *PrevSpec = nullptr; |
1094 | 497 | unsigned DiagID; |
1095 | 497 | if (DS.SetTypeSpecType(DeclSpec::TST_underlyingType, StartLoc, PrevSpec, |
1096 | 497 | DiagID, Result.get(), |
1097 | 497 | Actions.getASTContext().getPrintingPolicy())) |
1098 | 0 | Diag(StartLoc, DiagID) << PrevSpec; |
1099 | 497 | DS.setTypeofParensRange(T.getRange()); |
1100 | 497 | } |
1101 | | |
1102 | | /// ParseBaseTypeSpecifier - Parse a C++ base-type-specifier which is either a |
1103 | | /// class name or decltype-specifier. Note that we only check that the result |
1104 | | /// names a type; semantic analysis will need to verify that the type names a |
1105 | | /// class. The result is either a type or null, depending on whether a type |
1106 | | /// name was found. |
1107 | | /// |
1108 | | /// base-type-specifier: [C++11 class.derived] |
1109 | | /// class-or-decltype |
1110 | | /// class-or-decltype: [C++11 class.derived] |
1111 | | /// nested-name-specifier[opt] class-name |
1112 | | /// decltype-specifier |
1113 | | /// class-name: [C++ class.name] |
1114 | | /// identifier |
1115 | | /// simple-template-id |
1116 | | /// |
1117 | | /// In C++98, instead of base-type-specifier, we have: |
1118 | | /// |
1119 | | /// ::[opt] nested-name-specifier[opt] class-name |
1120 | | TypeResult Parser::ParseBaseTypeSpecifier(SourceLocation &BaseLoc, |
1121 | 193k | SourceLocation &EndLocation) { |
1122 | | // Ignore attempts to use typename |
1123 | 193k | if (Tok.is(tok::kw_typename)) { |
1124 | 1 | Diag(Tok, diag::err_expected_class_name_not_template) |
1125 | 1 | << FixItHint::CreateRemoval(Tok.getLocation()); |
1126 | 1 | ConsumeToken(); |
1127 | 1 | } |
1128 | | |
1129 | | // Parse optional nested-name-specifier |
1130 | 193k | CXXScopeSpec SS; |
1131 | 193k | if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/nullptr, |
1132 | 193k | /*ObjectHadErrors=*/false, |
1133 | 193k | /*EnteringContext=*/false)) |
1134 | 6 | return true; |
1135 | | |
1136 | 193k | BaseLoc = Tok.getLocation(); |
1137 | | |
1138 | | // Parse decltype-specifier |
1139 | | // tok == kw_decltype is just error recovery, it can only happen when SS |
1140 | | // isn't empty |
1141 | 193k | if (Tok.isOneOf(tok::kw_decltype, tok::annot_decltype)) { |
1142 | 17 | if (SS.isNotEmpty()) |
1143 | 2 | Diag(SS.getBeginLoc(), diag::err_unexpected_scope_on_base_decltype) |
1144 | 2 | << FixItHint::CreateRemoval(SS.getRange()); |
1145 | | // Fake up a Declarator to use with ActOnTypeName. |
1146 | 17 | DeclSpec DS(AttrFactory); |
1147 | | |
1148 | 17 | EndLocation = ParseDecltypeSpecifier(DS); |
1149 | | |
1150 | 17 | Declarator DeclaratorInfo(DS, DeclaratorContext::TypeName); |
1151 | 17 | return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo); |
1152 | 17 | } |
1153 | | |
1154 | | // Check whether we have a template-id that names a type. |
1155 | 193k | if (Tok.is(tok::annot_template_id)) { |
1156 | 120k | TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok); |
1157 | 120k | if (TemplateId->mightBeType()) { |
1158 | 120k | AnnotateTemplateIdTokenAsType(SS, /*IsClassName*/true); |
1159 | | |
1160 | 120k | assert(Tok.is(tok::annot_typename) && "template-id -> type failed"); |
1161 | 120k | TypeResult Type = getTypeAnnotation(Tok); |
1162 | 120k | EndLocation = Tok.getAnnotationEndLoc(); |
1163 | 120k | ConsumeAnnotationToken(); |
1164 | 120k | return Type; |
1165 | 120k | } |
1166 | | |
1167 | | // Fall through to produce an error below. |
1168 | 120k | } |
1169 | | |
1170 | 73.4k | if (Tok.isNot(tok::identifier)) { |
1171 | 7 | Diag(Tok, diag::err_expected_class_name); |
1172 | 7 | return true; |
1173 | 7 | } |
1174 | | |
1175 | 73.4k | IdentifierInfo *Id = Tok.getIdentifierInfo(); |
1176 | 73.4k | SourceLocation IdLoc = ConsumeToken(); |
1177 | | |
1178 | 73.4k | if (Tok.is(tok::less)) { |
1179 | | // It looks the user intended to write a template-id here, but the |
1180 | | // template-name was wrong. Try to fix that. |
1181 | | // FIXME: Invoke ParseOptionalCXXScopeSpecifier in a "'template' is neither |
1182 | | // required nor permitted" mode, and do this there. |
1183 | 4 | TemplateNameKind TNK = TNK_Non_template; |
1184 | 4 | TemplateTy Template; |
1185 | 4 | if (!Actions.DiagnoseUnknownTemplateName(*Id, IdLoc, getCurScope(), |
1186 | 4 | &SS, Template, TNK)) { |
1187 | 4 | Diag(IdLoc, diag::err_unknown_template_name) |
1188 | 4 | << Id; |
1189 | 4 | } |
1190 | | |
1191 | | // Form the template name |
1192 | 4 | UnqualifiedId TemplateName; |
1193 | 4 | TemplateName.setIdentifier(Id, IdLoc); |
1194 | | |
1195 | | // Parse the full template-id, then turn it into a type. |
1196 | 4 | if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(), |
1197 | 4 | TemplateName)) |
1198 | 1 | return true; |
1199 | 3 | if (Tok.is(tok::annot_template_id) && |
1200 | 3 | takeTemplateIdAnnotation(Tok)->mightBeType()) |
1201 | 3 | AnnotateTemplateIdTokenAsType(SS, /*IsClassName*/true); |
1202 | | |
1203 | | // If we didn't end up with a typename token, there's nothing more we |
1204 | | // can do. |
1205 | 3 | if (Tok.isNot(tok::annot_typename)) |
1206 | 0 | return true; |
1207 | | |
1208 | | // Retrieve the type from the annotation token, consume that token, and |
1209 | | // return. |
1210 | 3 | EndLocation = Tok.getAnnotationEndLoc(); |
1211 | 3 | TypeResult Type = getTypeAnnotation(Tok); |
1212 | 3 | ConsumeAnnotationToken(); |
1213 | 3 | return Type; |
1214 | 3 | } |
1215 | | |
1216 | | // We have an identifier; check whether it is actually a type. |
1217 | 73.4k | IdentifierInfo *CorrectedII = nullptr; |
1218 | 73.4k | ParsedType Type = Actions.getTypeName( |
1219 | 73.4k | *Id, IdLoc, getCurScope(), &SS, /*isClassName=*/true, false, nullptr, |
1220 | 73.4k | /*IsCtorOrDtorName=*/false, |
1221 | 73.4k | /*WantNontrivialTypeSourceInfo=*/true, |
1222 | 73.4k | /*IsClassTemplateDeductionContext*/ false, &CorrectedII); |
1223 | 73.4k | if (!Type) { |
1224 | 15 | Diag(IdLoc, diag::err_expected_class_name); |
1225 | 15 | return true; |
1226 | 15 | } |
1227 | | |
1228 | | // Consume the identifier. |
1229 | 73.4k | EndLocation = IdLoc; |
1230 | | |
1231 | | // Fake up a Declarator to use with ActOnTypeName. |
1232 | 73.4k | DeclSpec DS(AttrFactory); |
1233 | 73.4k | DS.SetRangeStart(IdLoc); |
1234 | 73.4k | DS.SetRangeEnd(EndLocation); |
1235 | 73.4k | DS.getTypeSpecScope() = SS; |
1236 | | |
1237 | 73.4k | const char *PrevSpec = nullptr; |
1238 | 73.4k | unsigned DiagID; |
1239 | 73.4k | DS.SetTypeSpecType(TST_typename, IdLoc, PrevSpec, DiagID, Type, |
1240 | 73.4k | Actions.getASTContext().getPrintingPolicy()); |
1241 | | |
1242 | 73.4k | Declarator DeclaratorInfo(DS, DeclaratorContext::TypeName); |
1243 | 73.4k | return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo); |
1244 | 73.4k | } |
1245 | | |
1246 | 58 | void Parser::ParseMicrosoftInheritanceClassAttributes(ParsedAttributes &attrs) { |
1247 | 116 | while (Tok.isOneOf(tok::kw___single_inheritance, |
1248 | 116 | tok::kw___multiple_inheritance, |
1249 | 58 | tok::kw___virtual_inheritance)) { |
1250 | 58 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
1251 | 58 | SourceLocation AttrNameLoc = ConsumeToken(); |
1252 | 58 | attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0, |
1253 | 58 | ParsedAttr::AS_Keyword); |
1254 | 58 | } |
1255 | 58 | } |
1256 | | |
1257 | | /// Determine whether the following tokens are valid after a type-specifier |
1258 | | /// which could be a standalone declaration. This will conservatively return |
1259 | | /// true if there's any doubt, and is appropriate for insert-';' fixits. |
1260 | 1.37M | bool Parser::isValidAfterTypeSpecifier(bool CouldBeBitfield) { |
1261 | | // This switch enumerates the valid "follow" set for type-specifiers. |
1262 | 1.37M | switch (Tok.getKind()) { |
1263 | 148 | default: break; |
1264 | 1.02M | case tok::semi: // struct foo {...} ; |
1265 | 1.02M | case tok::star: // struct foo {...} * P; |
1266 | 1.02M | case tok::amp: // struct foo {...} & R = ... |
1267 | 1.02M | case tok::ampamp: // struct foo {...} && R = ... |
1268 | 1.37M | case tok::identifier: // struct foo {...} V ; |
1269 | 1.37M | case tok::r_paren: //(struct foo {...} ) {4} |
1270 | 1.37M | case tok::coloncolon: // struct foo {...} :: a::b; |
1271 | 1.37M | case tok::annot_cxxscope: // struct foo {...} a:: b; |
1272 | 1.37M | case tok::annot_typename: // struct foo {...} a ::b; |
1273 | 1.37M | case tok::annot_template_id: // struct foo {...} a<int> ::b; |
1274 | 1.37M | case tok::kw_decltype: // struct foo {...} decltype (a)::b; |
1275 | 1.37M | case tok::l_paren: // struct foo {...} ( x); |
1276 | 1.37M | case tok::comma: // __builtin_offsetof(struct foo{...} , |
1277 | 1.37M | case tok::kw_operator: // struct foo operator ++() {...} |
1278 | 1.37M | case tok::kw___declspec: // struct foo {...} __declspec(...) |
1279 | 1.37M | case tok::l_square: // void f(struct f [ 3]) |
1280 | 1.37M | case tok::ellipsis: // void f(struct f ... [Ns]) |
1281 | | // FIXME: we should emit semantic diagnostic when declaration |
1282 | | // attribute is in type attribute position. |
1283 | 1.37M | case tok::kw___attribute: // struct foo __attribute__((used)) x; |
1284 | 1.37M | case tok::annot_pragma_pack: // struct foo {...} _Pragma(pack(pop)); |
1285 | | // struct foo {...} _Pragma(section(...)); |
1286 | 1.37M | case tok::annot_pragma_ms_pragma: |
1287 | | // struct foo {...} _Pragma(vtordisp(pop)); |
1288 | 1.37M | case tok::annot_pragma_ms_vtordisp: |
1289 | | // struct foo {...} _Pragma(pointers_to_members(...)); |
1290 | 1.37M | case tok::annot_pragma_ms_pointers_to_members: |
1291 | 1.37M | return true; |
1292 | 4 | case tok::colon: |
1293 | 4 | return CouldBeBitfield || // enum E { ... } : 2; |
1294 | 3 | ColonIsSacred; // _Generic(..., enum E : 2); |
1295 | | // Microsoft compatibility |
1296 | 1 | case tok::kw___cdecl: // struct foo {...} __cdecl x; |
1297 | 1 | case tok::kw___fastcall: // struct foo {...} __fastcall x; |
1298 | 1 | case tok::kw___stdcall: // struct foo {...} __stdcall x; |
1299 | 1 | case tok::kw___thiscall: // struct foo {...} __thiscall x; |
1300 | 1 | case tok::kw___vectorcall: // struct foo {...} __vectorcall x; |
1301 | | // We will diagnose these calling-convention specifiers on non-function |
1302 | | // declarations later, so claim they are valid after a type specifier. |
1303 | 1 | return getLangOpts().MicrosoftExt; |
1304 | | // Type qualifiers |
1305 | 23 | case tok::kw_const: // struct foo {...} const x; |
1306 | 29 | case tok::kw_volatile: // struct foo {...} volatile x; |
1307 | 29 | case tok::kw_restrict: // struct foo {...} restrict x; |
1308 | 31 | case tok::kw__Atomic: // struct foo {...} _Atomic x; |
1309 | 34 | case tok::kw___unaligned: // struct foo {...} __unaligned *x; |
1310 | | // Function specifiers |
1311 | | // Note, no 'explicit'. An explicit function must be either a conversion |
1312 | | // operator or a constructor. Either way, it can't have a return type. |
1313 | 36 | case tok::kw_inline: // struct foo inline f(); |
1314 | 39 | case tok::kw_virtual: // struct foo virtual f(); |
1315 | 42 | case tok::kw_friend: // struct foo friend f(); |
1316 | | // Storage-class specifiers |
1317 | 47 | case tok::kw_static: // struct foo {...} static x; |
1318 | 128 | case tok::kw_extern: // struct foo {...} extern x; |
1319 | 138 | case tok::kw_typedef: // struct foo {...} typedef x; |
1320 | 138 | case tok::kw_register: // struct foo {...} register x; |
1321 | 138 | case tok::kw_auto: // struct foo {...} auto x; |
1322 | 140 | case tok::kw_mutable: // struct foo {...} mutable x; |
1323 | 141 | case tok::kw_thread_local: // struct foo {...} thread_local x; |
1324 | 199 | case tok::kw_constexpr: // struct foo {...} constexpr x; |
1325 | 199 | case tok::kw_consteval: // struct foo {...} consteval x; |
1326 | 199 | case tok::kw_constinit: // struct foo {...} constinit x; |
1327 | | // As shown above, type qualifiers and storage class specifiers absolutely |
1328 | | // can occur after class specifiers according to the grammar. However, |
1329 | | // almost no one actually writes code like this. If we see one of these, |
1330 | | // it is much more likely that someone missed a semi colon and the |
1331 | | // type/storage class specifier we're seeing is part of the *next* |
1332 | | // intended declaration, as in: |
1333 | | // |
1334 | | // struct foo { ... } |
1335 | | // typedef int X; |
1336 | | // |
1337 | | // We'd really like to emit a missing semicolon error instead of emitting |
1338 | | // an error on the 'int' saying that you can't have two type specifiers in |
1339 | | // the same declaration of X. Because of this, we look ahead past this |
1340 | | // token to see if it's a type specifier. If so, we know the code is |
1341 | | // otherwise invalid, so we can produce the expected semi error. |
1342 | 199 | if (!isKnownToBeTypeSpecifier(NextToken())) |
1343 | 197 | return true; |
1344 | 2 | break; |
1345 | 6 | case tok::r_brace: // struct bar { struct foo {...} } |
1346 | | // Missing ';' at end of struct is accepted as an extension in C mode. |
1347 | 6 | if (!getLangOpts().CPlusPlus) |
1348 | 2 | return true; |
1349 | 4 | break; |
1350 | 3 | case tok::greater: |
1351 | | // template<class T = class X> |
1352 | 3 | return getLangOpts().CPlusPlus; |
1353 | 154 | } |
1354 | 154 | return false; |
1355 | 154 | } |
1356 | | |
1357 | | /// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or |
1358 | | /// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which |
1359 | | /// until we reach the start of a definition or see a token that |
1360 | | /// cannot start a definition. |
1361 | | /// |
1362 | | /// class-specifier: [C++ class] |
1363 | | /// class-head '{' member-specification[opt] '}' |
1364 | | /// class-head '{' member-specification[opt] '}' attributes[opt] |
1365 | | /// class-head: |
1366 | | /// class-key identifier[opt] base-clause[opt] |
1367 | | /// class-key nested-name-specifier identifier base-clause[opt] |
1368 | | /// class-key nested-name-specifier[opt] simple-template-id |
1369 | | /// base-clause[opt] |
1370 | | /// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt] |
1371 | | /// [GNU] class-key attributes[opt] nested-name-specifier |
1372 | | /// identifier base-clause[opt] |
1373 | | /// [GNU] class-key attributes[opt] nested-name-specifier[opt] |
1374 | | /// simple-template-id base-clause[opt] |
1375 | | /// class-key: |
1376 | | /// 'class' |
1377 | | /// 'struct' |
1378 | | /// 'union' |
1379 | | /// |
1380 | | /// elaborated-type-specifier: [C++ dcl.type.elab] |
1381 | | /// class-key ::[opt] nested-name-specifier[opt] identifier |
1382 | | /// class-key ::[opt] nested-name-specifier[opt] 'template'[opt] |
1383 | | /// simple-template-id |
1384 | | /// |
1385 | | /// Note that the C++ class-specifier and elaborated-type-specifier, |
1386 | | /// together, subsume the C99 struct-or-union-specifier: |
1387 | | /// |
1388 | | /// struct-or-union-specifier: [C99 6.7.2.1] |
1389 | | /// struct-or-union identifier[opt] '{' struct-contents '}' |
1390 | | /// struct-or-union identifier |
1391 | | /// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents |
1392 | | /// '}' attributes[opt] |
1393 | | /// [GNU] struct-or-union attributes[opt] identifier |
1394 | | /// struct-or-union: |
1395 | | /// 'struct' |
1396 | | /// 'union' |
1397 | | void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind, |
1398 | | SourceLocation StartLoc, DeclSpec &DS, |
1399 | | const ParsedTemplateInfo &TemplateInfo, |
1400 | | AccessSpecifier AS, |
1401 | | bool EnteringContext, DeclSpecContext DSC, |
1402 | 1.94M | ParsedAttributesWithRange &Attributes) { |
1403 | 1.94M | DeclSpec::TST TagType; |
1404 | 1.94M | if (TagTokKind == tok::kw_struct) |
1405 | 1.73M | TagType = DeclSpec::TST_struct; |
1406 | 206k | else if (TagTokKind == tok::kw___interface) |
1407 | 32 | TagType = DeclSpec::TST_interface; |
1408 | 206k | else if (TagTokKind == tok::kw_class) |
1409 | 152k | TagType = DeclSpec::TST_class; |
1410 | 54.5k | else { |
1411 | 54.5k | assert(TagTokKind == tok::kw_union && "Not a class specifier"); |
1412 | 54.5k | TagType = DeclSpec::TST_union; |
1413 | 54.5k | } |
1414 | | |
1415 | 1.94M | if (Tok.is(tok::code_completion)) { |
1416 | | // Code completion for a struct, class, or union name. |
1417 | 11 | Actions.CodeCompleteTag(getCurScope(), TagType); |
1418 | 11 | return cutOffParsing(); |
1419 | 11 | } |
1420 | | |
1421 | | // C++03 [temp.explicit] 14.7.2/8: |
1422 | | // The usual access checking rules do not apply to names used to specify |
1423 | | // explicit instantiations. |
1424 | | // |
1425 | | // As an extension we do not perform access checking on the names used to |
1426 | | // specify explicit specializations either. This is important to allow |
1427 | | // specializing traits classes for private types. |
1428 | | // |
1429 | | // Note that we don't suppress if this turns out to be an elaborated |
1430 | | // type specifier. |
1431 | 1.94M | bool shouldDelayDiagsInTag = |
1432 | 1.94M | (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation || |
1433 | 1.93M | TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization); |
1434 | 1.94M | SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag); |
1435 | | |
1436 | 1.94M | ParsedAttributesWithRange attrs(AttrFactory); |
1437 | | // If attributes exist after tag, parse them. |
1438 | 1.94M | MaybeParseGNUAttributes(attrs); |
1439 | 1.94M | MaybeParseMicrosoftDeclSpecs(attrs); |
1440 | | |
1441 | | // Parse inheritance specifiers. |
1442 | 1.94M | if (Tok.isOneOf(tok::kw___single_inheritance, |
1443 | 1.94M | tok::kw___multiple_inheritance, |
1444 | 1.94M | tok::kw___virtual_inheritance)) |
1445 | 58 | ParseMicrosoftInheritanceClassAttributes(attrs); |
1446 | | |
1447 | | // If C++0x attributes exist here, parse them. |
1448 | | // FIXME: Are we consistent with the ordering of parsing of different |
1449 | | // styles of attributes? |
1450 | 1.94M | MaybeParseCXX11Attributes(attrs); |
1451 | | |
1452 | | // Source location used by FIXIT to insert misplaced |
1453 | | // C++11 attributes |
1454 | 1.94M | SourceLocation AttrFixitLoc = Tok.getLocation(); |
1455 | | |
1456 | 1.94M | if (TagType == DeclSpec::TST_struct && |
1457 | 1.73M | Tok.isNot(tok::identifier) && |
1458 | 203k | !Tok.isAnnotation() && |
1459 | 203k | Tok.getIdentifierInfo() && |
1460 | 115 | Tok.isOneOf(tok::kw___is_abstract, |
1461 | 115 | tok::kw___is_aggregate, |
1462 | 115 | tok::kw___is_arithmetic, |
1463 | 115 | tok::kw___is_array, |
1464 | 115 | tok::kw___is_assignable, |
1465 | 115 | tok::kw___is_base_of, |
1466 | 115 | tok::kw___is_class, |
1467 | 115 | tok::kw___is_complete_type, |
1468 | 115 | tok::kw___is_compound, |
1469 | 115 | tok::kw___is_const, |
1470 | 115 | tok::kw___is_constructible, |
1471 | 115 | tok::kw___is_convertible, |
1472 | 115 | tok::kw___is_convertible_to, |
1473 | 115 | tok::kw___is_destructible, |
1474 | 115 | tok::kw___is_empty, |
1475 | 115 | tok::kw___is_enum, |
1476 | 115 | tok::kw___is_floating_point, |
1477 | 115 | tok::kw___is_final, |
1478 | 115 | tok::kw___is_function, |
1479 | 115 | tok::kw___is_fundamental, |
1480 | 115 | tok::kw___is_integral, |
1481 | 115 | tok::kw___is_interface_class, |
1482 | 115 | tok::kw___is_literal, |
1483 | 115 | tok::kw___is_lvalue_expr, |
1484 | 115 | tok::kw___is_lvalue_reference, |
1485 | 115 | tok::kw___is_member_function_pointer, |
1486 | 115 | tok::kw___is_member_object_pointer, |
1487 | 115 | tok::kw___is_member_pointer, |
1488 | 115 | tok::kw___is_nothrow_assignable, |
1489 | 115 | tok::kw___is_nothrow_constructible, |
1490 | 115 | tok::kw___is_nothrow_destructible, |
1491 | 115 | tok::kw___is_object, |
1492 | 115 | tok::kw___is_pod, |
1493 | 115 | tok::kw___is_pointer, |
1494 | 115 | tok::kw___is_polymorphic, |
1495 | 115 | tok::kw___is_reference, |
1496 | 115 | tok::kw___is_rvalue_expr, |
1497 | 115 | tok::kw___is_rvalue_reference, |
1498 | 115 | tok::kw___is_same, |
1499 | 115 | tok::kw___is_scalar, |
1500 | 115 | tok::kw___is_sealed, |
1501 | 115 | tok::kw___is_signed, |
1502 | 115 | tok::kw___is_standard_layout, |
1503 | 115 | tok::kw___is_trivial, |
1504 | 115 | tok::kw___is_trivially_assignable, |
1505 | 115 | tok::kw___is_trivially_constructible, |
1506 | 115 | tok::kw___is_trivially_copyable, |
1507 | 115 | tok::kw___is_union, |
1508 | 115 | tok::kw___is_unsigned, |
1509 | 115 | tok::kw___is_void, |
1510 | 115 | tok::kw___is_volatile)) |
1511 | | // GNU libstdc++ 4.2 and libc++ use certain intrinsic names as the |
1512 | | // name of struct templates, but some are keywords in GCC >= 4.3 |
1513 | | // and Clang. Therefore, when we see the token sequence "struct |
1514 | | // X", make X into a normal identifier rather than a keyword, to |
1515 | | // allow libstdc++ 4.2 and libc++ to work properly. |
1516 | 113 | TryKeywordIdentFallback(true); |
1517 | | |
1518 | 1.94M | struct PreserveAtomicIdentifierInfoRAII { |
1519 | 1.94M | PreserveAtomicIdentifierInfoRAII(Token &Tok, bool Enabled) |
1520 | 1.94M | : AtomicII(nullptr) { |
1521 | 1.94M | if (!Enabled) |
1522 | 1.94M | return; |
1523 | 2 | assert(Tok.is(tok::kw__Atomic)); |
1524 | 2 | AtomicII = Tok.getIdentifierInfo(); |
1525 | 2 | AtomicII->revertTokenIDToIdentifier(); |
1526 | 2 | Tok.setKind(tok::identifier); |
1527 | 2 | } |
1528 | 1.94M | ~PreserveAtomicIdentifierInfoRAII() { |
1529 | 1.94M | if (!AtomicII) |
1530 | 1.94M | return; |
1531 | 2 | AtomicII->revertIdentifierToTokenID(tok::kw__Atomic); |
1532 | 2 | } |
1533 | 1.94M | IdentifierInfo *AtomicII; |
1534 | 1.94M | }; |
1535 | | |
1536 | | // HACK: MSVC doesn't consider _Atomic to be a keyword and its STL |
1537 | | // implementation for VS2013 uses _Atomic as an identifier for one of the |
1538 | | // classes in <atomic>. When we are parsing 'struct _Atomic', don't consider |
1539 | | // '_Atomic' to be a keyword. We are careful to undo this so that clang can |
1540 | | // use '_Atomic' in its own header files. |
1541 | 1.94M | bool ShouldChangeAtomicToIdentifier = getLangOpts().MSVCCompat && |
1542 | 9.66k | Tok.is(tok::kw__Atomic) && |
1543 | 2 | TagType == DeclSpec::TST_struct; |
1544 | 1.94M | PreserveAtomicIdentifierInfoRAII AtomicTokenGuard( |
1545 | 1.94M | Tok, ShouldChangeAtomicToIdentifier); |
1546 | | |
1547 | | // Parse the (optional) nested-name-specifier. |
1548 | 1.94M | CXXScopeSpec &SS = DS.getTypeSpecScope(); |
1549 | 1.94M | if (getLangOpts().CPlusPlus) { |
1550 | | // "FOO : BAR" is not a potential typo for "FOO::BAR". In this context it |
1551 | | // is a base-specifier-list. |
1552 | 1.00M | ColonProtectionRAIIObject X(*this); |
1553 | | |
1554 | 1.00M | CXXScopeSpec Spec; |
1555 | 1.00M | bool HasValidSpec = true; |
1556 | 1.00M | if (ParseOptionalCXXScopeSpecifier(Spec, /*ObjectType=*/nullptr, |
1557 | 1.00M | /*ObjectHadErrors=*/false, |
1558 | 2 | EnteringContext)) { |
1559 | 2 | DS.SetTypeSpecError(); |
1560 | 2 | HasValidSpec = false; |
1561 | 2 | } |
1562 | 1.00M | if (Spec.isSet()) |
1563 | 1.87k | if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id)426 ) { |
1564 | 17 | Diag(Tok, diag::err_expected) << tok::identifier; |
1565 | 17 | HasValidSpec = false; |
1566 | 17 | } |
1567 | 1.00M | if (HasValidSpec) |
1568 | 1.00M | SS = Spec; |
1569 | 1.00M | } |
1570 | | |
1571 | 1.94M | TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams; |
1572 | | |
1573 | 1.94M | auto RecoverFromUndeclaredTemplateName = [&](IdentifierInfo *Name, |
1574 | 1.94M | SourceLocation NameLoc, |
1575 | 1.94M | SourceRange TemplateArgRange, |
1576 | 24 | bool KnownUndeclared) { |
1577 | 24 | Diag(NameLoc, diag::err_explicit_spec_non_template) |
1578 | 24 | << (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) |
1579 | 24 | << TagTokKind << Name << TemplateArgRange << KnownUndeclared; |
1580 | | |
1581 | | // Strip off the last template parameter list if it was empty, since |
1582 | | // we've removed its template argument list. |
1583 | 24 | if (TemplateParams && TemplateInfo.LastParameterListWasEmpty16 ) { |
1584 | 16 | if (TemplateParams->size() > 1) { |
1585 | 0 | TemplateParams->pop_back(); |
1586 | 16 | } else { |
1587 | 16 | TemplateParams = nullptr; |
1588 | 16 | const_cast<ParsedTemplateInfo &>(TemplateInfo).Kind = |
1589 | 16 | ParsedTemplateInfo::NonTemplate; |
1590 | 16 | } |
1591 | 8 | } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) { |
1592 | | // Pretend this is just a forward declaration. |
1593 | 6 | TemplateParams = nullptr; |
1594 | 6 | const_cast<ParsedTemplateInfo &>(TemplateInfo).Kind = |
1595 | 6 | ParsedTemplateInfo::NonTemplate; |
1596 | 6 | const_cast<ParsedTemplateInfo &>(TemplateInfo).TemplateLoc = |
1597 | 6 | SourceLocation(); |
1598 | 6 | const_cast<ParsedTemplateInfo &>(TemplateInfo).ExternLoc = |
1599 | 6 | SourceLocation(); |
1600 | 6 | } |
1601 | 24 | }; |
1602 | | |
1603 | | // Parse the (optional) class name or simple-template-id. |
1604 | 1.94M | IdentifierInfo *Name = nullptr; |
1605 | 1.94M | SourceLocation NameLoc; |
1606 | 1.94M | TemplateIdAnnotation *TemplateId = nullptr; |
1607 | 1.94M | if (Tok.is(tok::identifier)) { |
1608 | 1.52M | Name = Tok.getIdentifierInfo(); |
1609 | 1.52M | NameLoc = ConsumeToken(); |
1610 | | |
1611 | 1.52M | if (Tok.is(tok::less) && getLangOpts().CPlusPlus12 ) { |
1612 | | // The name was supposed to refer to a template, but didn't. |
1613 | | // Eat the template argument list and try to continue parsing this as |
1614 | | // a class (or template thereof). |
1615 | 11 | TemplateArgList TemplateArgs; |
1616 | 11 | SourceLocation LAngleLoc, RAngleLoc; |
1617 | 11 | if (ParseTemplateIdAfterTemplateName(true, LAngleLoc, TemplateArgs, |
1618 | 2 | RAngleLoc)) { |
1619 | | // We couldn't parse the template argument list at all, so don't |
1620 | | // try to give any location information for the list. |
1621 | 2 | LAngleLoc = RAngleLoc = SourceLocation(); |
1622 | 2 | } |
1623 | 11 | RecoverFromUndeclaredTemplateName( |
1624 | 11 | Name, NameLoc, SourceRange(LAngleLoc, RAngleLoc), false); |
1625 | 11 | } |
1626 | 424k | } else if (Tok.is(tok::annot_template_id)) { |
1627 | 205k | TemplateId = takeTemplateIdAnnotation(Tok); |
1628 | 205k | NameLoc = ConsumeAnnotationToken(); |
1629 | | |
1630 | 205k | if (TemplateId->Kind == TNK_Undeclared_template) { |
1631 | | // Try to resolve the template name to a type template. May update Kind. |
1632 | 16 | Actions.ActOnUndeclaredTypeTemplateName( |
1633 | 16 | getCurScope(), TemplateId->Template, TemplateId->Kind, NameLoc, Name); |
1634 | 16 | if (TemplateId->Kind == TNK_Undeclared_template) { |
1635 | 13 | RecoverFromUndeclaredTemplateName( |
1636 | 13 | Name, NameLoc, |
1637 | 13 | SourceRange(TemplateId->LAngleLoc, TemplateId->RAngleLoc), true); |
1638 | 13 | TemplateId = nullptr; |
1639 | 13 | } |
1640 | 16 | } |
1641 | | |
1642 | 205k | if (TemplateId && !TemplateId->mightBeType()205k ) { |
1643 | | // The template-name in the simple-template-id refers to |
1644 | | // something other than a type template. Give an appropriate |
1645 | | // error message and skip to the ';'. |
1646 | 10 | SourceRange Range(NameLoc); |
1647 | 10 | if (SS.isNotEmpty()) |
1648 | 5 | Range.setBegin(SS.getBeginLoc()); |
1649 | | |
1650 | | // FIXME: Name may be null here. |
1651 | 10 | Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template) |
1652 | 10 | << TemplateId->Name << static_cast<int>(TemplateId->Kind) << Range; |
1653 | | |
1654 | 10 | DS.SetTypeSpecError(); |
1655 | 10 | SkipUntil(tok::semi, StopBeforeMatch); |
1656 | 10 | return; |
1657 | 10 | } |
1658 | 1.94M | } |
1659 | | |
1660 | | // There are four options here. |
1661 | | // - If we are in a trailing return type, this is always just a reference, |
1662 | | // and we must not try to parse a definition. For instance, |
1663 | | // [] () -> struct S { }; |
1664 | | // does not define a type. |
1665 | | // - If we have 'struct foo {...', 'struct foo :...', |
1666 | | // 'struct foo final :' or 'struct foo final {', then this is a definition. |
1667 | | // - If we have 'struct foo;', then this is either a forward declaration |
1668 | | // or a friend declaration, which have to be treated differently. |
1669 | | // - Otherwise we have something like 'struct foo xyz', a reference. |
1670 | | // |
1671 | | // We also detect these erroneous cases to provide better diagnostic for |
1672 | | // C++11 attributes parsing. |
1673 | | // - attributes follow class name: |
1674 | | // struct foo [[]] {}; |
1675 | | // - attributes appear before or after 'final': |
1676 | | // struct foo [[]] final [[]] {}; |
1677 | | // |
1678 | | // However, in type-specifier-seq's, things look like declarations but are |
1679 | | // just references, e.g. |
1680 | | // new struct s; |
1681 | | // or |
1682 | | // &T::operator struct s; |
1683 | | // For these, DSC is DeclSpecContext::DSC_type_specifier or |
1684 | | // DeclSpecContext::DSC_alias_declaration. |
1685 | | |
1686 | | // If there are attributes after class name, parse them. |
1687 | 1.94M | MaybeParseCXX11Attributes(Attributes); |
1688 | | |
1689 | 1.94M | const PrintingPolicy &Policy = Actions.getASTContext().getPrintingPolicy(); |
1690 | 1.94M | Sema::TagUseKind TUK; |
1691 | 1.94M | if (isDefiningTypeSpecifierContext(DSC) == AllowDefiningTypeSpec::No || |
1692 | 1.94M | (getLangOpts().OpenMP && OpenMPDirectiveParsing16.0k )) |
1693 | 127 | TUK = Sema::TUK_Reference; |
1694 | 1.94M | else if (Tok.is(tok::l_brace) || |
1695 | 983k | (getLangOpts().CPlusPlus && Tok.is(tok::colon)546k ) || |
1696 | 795k | (isCXX11FinalKeyword() && |
1697 | 1.15M | (118 NextToken().is(tok::l_brace)118 || NextToken().is(tok::colon)72 ))) { |
1698 | 1.15M | if (DS.isFriendSpecified()) { |
1699 | | // C++ [class.friend]p2: |
1700 | | // A class shall not be defined in a friend declaration. |
1701 | 4 | Diag(Tok.getLocation(), diag::err_friend_decl_defines_type) |
1702 | 4 | << SourceRange(DS.getFriendSpecLoc()); |
1703 | | |
1704 | | // Skip everything up to the semicolon, so that this looks like a proper |
1705 | | // friend class (or template thereof) declaration. |
1706 | 4 | SkipUntil(tok::semi, StopBeforeMatch); |
1707 | 4 | TUK = Sema::TUK_Friend; |
1708 | 1.15M | } else { |
1709 | | // Okay, this is a class definition. |
1710 | 1.15M | TUK = Sema::TUK_Definition; |
1711 | 1.15M | } |
1712 | 795k | } else if (isCXX11FinalKeyword() && (16 NextToken().is(tok::l_square)16 || |
1713 | 12 | NextToken().is(tok::kw_alignas)9 )) { |
1714 | | // We can't tell if this is a definition or reference |
1715 | | // until we skipped the 'final' and C++11 attribute specifiers. |
1716 | 12 | TentativeParsingAction PA(*this); |
1717 | | |
1718 | | // Skip the 'final' keyword. |
1719 | 12 | ConsumeToken(); |
1720 | | |
1721 | | // Skip C++11 attribute specifiers. |
1722 | 47 | while (true) { |
1723 | 47 | if (Tok.is(tok::l_square) && NextToken().is(tok::l_square)23 ) { |
1724 | 22 | ConsumeBracket(); |
1725 | 22 | if (!SkipUntil(tok::r_square, StopAtSemi)) |
1726 | 0 | break; |
1727 | 25 | } else if (Tok.is(tok::kw_alignas) && NextToken().is(tok::l_paren)13 ) { |
1728 | 13 | ConsumeToken(); |
1729 | 13 | ConsumeParen(); |
1730 | 13 | if (!SkipUntil(tok::r_paren, StopAtSemi)) |
1731 | 0 | break; |
1732 | 12 | } else { |
1733 | 12 | break; |
1734 | 12 | } |
1735 | 47 | } |
1736 | | |
1737 | 12 | if (Tok.isOneOf(tok::l_brace, tok::colon)) |
1738 | 10 | TUK = Sema::TUK_Definition; |
1739 | 2 | else |
1740 | 2 | TUK = Sema::TUK_Reference; |
1741 | | |
1742 | 12 | PA.Revert(); |
1743 | 795k | } else if (!isTypeSpecifier(DSC) && |
1744 | 794k | (Tok.is(tok::semi) || |
1745 | 697k | (Tok.isAtStartOfLine() && !isValidAfterTypeSpecifier(false)1.29k ))) { |
1746 | 78.7k | TUK = DS.isFriendSpecified() ? Sema::TUK_Friend18.1k : Sema::TUK_Declaration; |
1747 | 96.9k | if (Tok.isNot(tok::semi)) { |
1748 | 14 | const PrintingPolicy &PPol = Actions.getASTContext().getPrintingPolicy(); |
1749 | | // A semicolon was missing after this declaration. Diagnose and recover. |
1750 | 14 | ExpectAndConsume(tok::semi, diag::err_expected_after, |
1751 | 14 | DeclSpec::getSpecifierName(TagType, PPol)); |
1752 | 14 | PP.EnterToken(Tok, /*IsReinject*/true); |
1753 | 14 | Tok.setKind(tok::semi); |
1754 | 14 | } |
1755 | 96.9k | } else |
1756 | 698k | TUK = Sema::TUK_Reference; |
1757 | | |
1758 | | // Forbid misplaced attributes. In cases of a reference, we pass attributes |
1759 | | // to caller to handle. |
1760 | 1.94M | if (TUK != Sema::TUK_Reference) { |
1761 | | // If this is not a reference, then the only possible |
1762 | | // valid place for C++11 attributes to appear here |
1763 | | // is between class-key and class-name. If there are |
1764 | | // any attributes after class-name, we try a fixit to move |
1765 | | // them to the right place. |
1766 | 1.24M | SourceRange AttrRange = Attributes.Range; |
1767 | 1.24M | if (AttrRange.isValid()) { |
1768 | 20 | Diag(AttrRange.getBegin(), diag::err_attributes_not_allowed) |
1769 | 20 | << AttrRange |
1770 | 20 | << FixItHint::CreateInsertionFromRange(AttrFixitLoc, |
1771 | 20 | CharSourceRange(AttrRange, true)) |
1772 | 20 | << FixItHint::CreateRemoval(AttrRange); |
1773 | | |
1774 | | // Recover by adding misplaced attributes to the attribute list |
1775 | | // of the class so they can be applied on the class later. |
1776 | 20 | attrs.takeAllFrom(Attributes); |
1777 | 20 | } |
1778 | 1.24M | } |
1779 | | |
1780 | | // If this is an elaborated type specifier, and we delayed |
1781 | | // diagnostics before, just merge them into the current pool. |
1782 | 1.94M | if (shouldDelayDiagsInTag) { |
1783 | 57.4k | diagsFromTag.done(); |
1784 | 57.4k | if (TUK == Sema::TUK_Reference) |
1785 | 4 | diagsFromTag.redelay(); |
1786 | 57.4k | } |
1787 | | |
1788 | 1.94M | if (!Name && !TemplateId424k && (219k DS.getTypeSpecType() == DeclSpec::TST_error219k || |
1789 | 219k | TUK != Sema::TUK_Definition)) { |
1790 | 28 | if (DS.getTypeSpecType() != DeclSpec::TST_error) { |
1791 | | // We have a declaration or reference to an anonymous class. |
1792 | 24 | Diag(StartLoc, diag::err_anon_type_definition) |
1793 | 24 | << DeclSpec::getSpecifierName(TagType, Policy); |
1794 | 24 | } |
1795 | | |
1796 | | // If we are parsing a definition and stop at a base-clause, continue on |
1797 | | // until the semicolon. Continuing from the comma will just trick us into |
1798 | | // thinking we are seeing a variable declaration. |
1799 | 28 | if (TUK == Sema::TUK_Definition && Tok.is(tok::colon)0 ) |
1800 | 0 | SkipUntil(tok::semi, StopBeforeMatch); |
1801 | 28 | else |
1802 | 28 | SkipUntil(tok::comma, StopAtSemi); |
1803 | 28 | return; |
1804 | 28 | } |
1805 | | |
1806 | | // Create the tag portion of the class or class template. |
1807 | 1.94M | DeclResult TagOrTempResult = true; // invalid |
1808 | 1.94M | TypeResult TypeResult = true; // invalid |
1809 | | |
1810 | 1.94M | bool Owned = false; |
1811 | 1.94M | Sema::SkipBodyInfo SkipBody; |
1812 | 1.94M | if (TemplateId) { |
1813 | | // Explicit specialization, class template partial specialization, |
1814 | | // or explicit instantiation. |
1815 | 205k | ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(), |
1816 | 205k | TemplateId->NumArgs); |
1817 | 205k | if (TemplateId->isInvalid()) { |
1818 | | // Can't build the declaration. |
1819 | 205k | } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation && |
1820 | 6.92k | TUK == Sema::TUK_Declaration) { |
1821 | | // This is an explicit instantiation of a class template. |
1822 | 6.90k | ProhibitAttributes(attrs); |
1823 | | |
1824 | 6.90k | TagOrTempResult = Actions.ActOnExplicitInstantiation( |
1825 | 6.90k | getCurScope(), TemplateInfo.ExternLoc, TemplateInfo.TemplateLoc, |
1826 | 6.90k | TagType, StartLoc, SS, TemplateId->Template, |
1827 | 6.90k | TemplateId->TemplateNameLoc, TemplateId->LAngleLoc, TemplateArgsPtr, |
1828 | 6.90k | TemplateId->RAngleLoc, attrs); |
1829 | | |
1830 | | // Friend template-ids are treated as references unless |
1831 | | // they have template headers, in which case they're ill-formed |
1832 | | // (FIXME: "template <class T> friend class A<T>::B<int>;"). |
1833 | | // We diagnose this error in ActOnClassTemplateSpecialization. |
1834 | 198k | } else if (TUK == Sema::TUK_Reference || |
1835 | 198k | (TUK == Sema::TUK_Friend && |
1836 | 4.51k | TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate4.46k )) { |
1837 | 4.51k | ProhibitAttributes(attrs); |
1838 | 4.51k | TypeResult = Actions.ActOnTagTemplateIdType(TUK, TagType, StartLoc, |
1839 | 4.51k | SS, |
1840 | 4.51k | TemplateId->TemplateKWLoc, |
1841 | 4.51k | TemplateId->Template, |
1842 | 4.51k | TemplateId->TemplateNameLoc, |
1843 | 4.51k | TemplateId->LAngleLoc, |
1844 | 4.51k | TemplateArgsPtr, |
1845 | 4.51k | TemplateId->RAngleLoc); |
1846 | 194k | } else { |
1847 | | // This is an explicit specialization or a class template |
1848 | | // partial specialization. |
1849 | 194k | TemplateParameterLists FakedParamLists; |
1850 | 194k | if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) { |
1851 | | // This looks like an explicit instantiation, because we have |
1852 | | // something like |
1853 | | // |
1854 | | // template class Foo<X> |
1855 | | // |
1856 | | // but it actually has a definition. Most likely, this was |
1857 | | // meant to be an explicit specialization, but the user forgot |
1858 | | // the '<>' after 'template'. |
1859 | | // It this is friend declaration however, since it cannot have a |
1860 | | // template header, it is most likely that the user meant to |
1861 | | // remove the 'template' keyword. |
1862 | 10 | assert((TUK == Sema::TUK_Definition || TUK == Sema::TUK_Friend) && |
1863 | 10 | "Expected a definition here"); |
1864 | | |
1865 | 10 | if (TUK == Sema::TUK_Friend) { |
1866 | 0 | Diag(DS.getFriendSpecLoc(), diag::err_friend_explicit_instantiation); |
1867 | 0 | TemplateParams = nullptr; |
1868 | 10 | } else { |
1869 | 10 | SourceLocation LAngleLoc = |
1870 | 10 | PP.getLocForEndOfToken(TemplateInfo.TemplateLoc); |
1871 | 10 | Diag(TemplateId->TemplateNameLoc, |
1872 | 10 | diag::err_explicit_instantiation_with_definition) |
1873 | 10 | << SourceRange(TemplateInfo.TemplateLoc) |
1874 | 10 | << FixItHint::CreateInsertion(LAngleLoc, "<>"); |
1875 | | |
1876 | | // Create a fake template parameter list that contains only |
1877 | | // "template<>", so that we treat this construct as a class |
1878 | | // template specialization. |
1879 | 10 | FakedParamLists.push_back(Actions.ActOnTemplateParameterList( |
1880 | 10 | 0, SourceLocation(), TemplateInfo.TemplateLoc, LAngleLoc, None, |
1881 | 10 | LAngleLoc, nullptr)); |
1882 | 10 | TemplateParams = &FakedParamLists; |
1883 | 10 | } |
1884 | 10 | } |
1885 | | |
1886 | | // Build the class template specialization. |
1887 | 194k | TagOrTempResult = Actions.ActOnClassTemplateSpecialization( |
1888 | 194k | getCurScope(), TagType, TUK, StartLoc, DS.getModulePrivateSpecLoc(), |
1889 | 194k | SS, *TemplateId, attrs, |
1890 | 194k | MultiTemplateParamsArg(TemplateParams ? &(*TemplateParams)[0] |
1891 | 14 | : nullptr, |
1892 | 194k | TemplateParams ? TemplateParams->size() : 014 ), |
1893 | 194k | &SkipBody); |
1894 | 194k | } |
1895 | 1.74M | } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation && |
1896 | 46 | TUK == Sema::TUK_Declaration) { |
1897 | | // Explicit instantiation of a member of a class template |
1898 | | // specialization, e.g., |
1899 | | // |
1900 | | // template struct Outer<int>::Inner; |
1901 | | // |
1902 | 42 | ProhibitAttributes(attrs); |
1903 | | |
1904 | 42 | TagOrTempResult = Actions.ActOnExplicitInstantiation( |
1905 | 42 | getCurScope(), TemplateInfo.ExternLoc, TemplateInfo.TemplateLoc, |
1906 | 42 | TagType, StartLoc, SS, Name, NameLoc, attrs); |
1907 | 1.74M | } else if (TUK == Sema::TUK_Friend && |
1908 | 13.7k | TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) { |
1909 | 6.26k | ProhibitAttributes(attrs); |
1910 | | |
1911 | 6.26k | TagOrTempResult = Actions.ActOnTemplatedFriendTag( |
1912 | 6.26k | getCurScope(), DS.getFriendSpecLoc(), TagType, StartLoc, SS, Name, |
1913 | 6.26k | NameLoc, attrs, |
1914 | 6.26k | MultiTemplateParamsArg(TemplateParams ? &(*TemplateParams)[0] : nullptr0 , |
1915 | 6.26k | TemplateParams ? TemplateParams->size() : 00 )); |
1916 | 1.73M | } else { |
1917 | 1.73M | if (TUK != Sema::TUK_Declaration && TUK != Sema::TUK_Definition1.66M ) |
1918 | 706k | ProhibitAttributes(attrs); |
1919 | | |
1920 | 1.73M | if (TUK == Sema::TUK_Definition && |
1921 | 959k | TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) { |
1922 | | // If the declarator-id is not a template-id, issue a diagnostic and |
1923 | | // recover by ignoring the 'template' keyword. |
1924 | 4 | Diag(Tok, diag::err_template_defn_explicit_instantiation) |
1925 | 4 | << 1 << FixItHint::CreateRemoval(TemplateInfo.TemplateLoc); |
1926 | 4 | TemplateParams = nullptr; |
1927 | 4 | } |
1928 | | |
1929 | 1.73M | bool IsDependent = false; |
1930 | | |
1931 | | // Don't pass down template parameter lists if this is just a tag |
1932 | | // reference. For example, we don't need the template parameters here: |
1933 | | // template <class T> class A *makeA(T t); |
1934 | 1.73M | MultiTemplateParamsArg TParams; |
1935 | 1.73M | if (TUK != Sema::TUK_Reference && TemplateParams1.03M ) |
1936 | 236k | TParams = |
1937 | 236k | MultiTemplateParamsArg(&(*TemplateParams)[0], TemplateParams->size()); |
1938 | | |
1939 | 1.73M | stripTypeAttributesOffDeclSpec(attrs, DS, TUK); |
1940 | | |
1941 | | // Declaration or definition of a class type |
1942 | 1.73M | TagOrTempResult = Actions.ActOnTag( |
1943 | 1.73M | getCurScope(), TagType, TUK, StartLoc, SS, Name, NameLoc, attrs, AS, |
1944 | 1.73M | DS.getModulePrivateSpecLoc(), TParams, Owned, IsDependent, |
1945 | 1.73M | SourceLocation(), false, clang::TypeResult(), |
1946 | 1.73M | DSC == DeclSpecContext::DSC_type_specifier, |
1947 | 1.73M | DSC == DeclSpecContext::DSC_template_param || |
1948 | 1.73M | DSC == DeclSpecContext::DSC_template_type_arg, |
1949 | 1.73M | &SkipBody); |
1950 | | |
1951 | | // If ActOnTag said the type was dependent, try again with the |
1952 | | // less common call. |
1953 | 1.73M | if (IsDependent) { |
1954 | 31 | assert(TUK == Sema::TUK_Reference || TUK == Sema::TUK_Friend); |
1955 | 31 | TypeResult = Actions.ActOnDependentTag(getCurScope(), TagType, TUK, |
1956 | 31 | SS, Name, StartLoc, NameLoc); |
1957 | 31 | } |
1958 | 1.73M | } |
1959 | | |
1960 | | // If there is a body, parse it and inform the actions module. |
1961 | 1.94M | if (TUK == Sema::TUK_Definition) { |
1962 | 1.15M | assert(Tok.is(tok::l_brace) || |
1963 | 1.15M | (getLangOpts().CPlusPlus && Tok.is(tok::colon)) || |
1964 | 1.15M | isCXX11FinalKeyword()); |
1965 | 1.15M | if (SkipBody.ShouldSkip) |
1966 | 254 | SkipCXXMemberSpecification(StartLoc, AttrFixitLoc, TagType, |
1967 | 254 | TagOrTempResult.get()); |
1968 | 1.15M | else if (getLangOpts().CPlusPlus) |
1969 | 642k | ParseCXXMemberSpecification(StartLoc, AttrFixitLoc, attrs, TagType, |
1970 | 642k | TagOrTempResult.get()); |
1971 | 507k | else { |
1972 | 507k | Decl *D = |
1973 | 507k | SkipBody.CheckSameAsPrevious ? SkipBody.New9 : TagOrTempResult.get(); |
1974 | | // Parse the definition body. |
1975 | 507k | ParseStructUnionBody(StartLoc, TagType, cast<RecordDecl>(D)); |
1976 | 507k | if (SkipBody.CheckSameAsPrevious && |
1977 | 9 | !Actions.ActOnDuplicateDefinition(DS, TagOrTempResult.get(), |
1978 | 2 | SkipBody)) { |
1979 | 2 | DS.SetTypeSpecError(); |
1980 | 2 | return; |
1981 | 2 | } |
1982 | 1.94M | } |
1983 | 1.15M | } |
1984 | | |
1985 | 1.94M | if (!TagOrTempResult.isInvalid()) |
1986 | | // Delayed processing of attributes. |
1987 | 1.94M | Actions.ProcessDeclAttributeDelayed(TagOrTempResult.get(), attrs); |
1988 | | |
1989 | 1.94M | const char *PrevSpec = nullptr; |
1990 | 1.94M | unsigned DiagID; |
1991 | 1.94M | bool Result; |
1992 | 1.94M | if (!TypeResult.isInvalid()) { |
1993 | 4.53k | Result = DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc, |
1994 | 4.53k | NameLoc.isValid() ? NameLoc : StartLoc0 , |
1995 | 4.53k | PrevSpec, DiagID, TypeResult.get(), Policy); |
1996 | 1.94M | } else if (!TagOrTempResult.isInvalid()) { |
1997 | 1.94M | Result = DS.SetTypeSpecType(TagType, StartLoc, |
1998 | 1.72M | NameLoc.isValid() ? NameLoc : StartLoc219k , |
1999 | 1.94M | PrevSpec, DiagID, TagOrTempResult.get(), Owned, |
2000 | 1.94M | Policy); |
2001 | 124 | } else { |
2002 | 124 | DS.SetTypeSpecError(); |
2003 | 124 | return; |
2004 | 124 | } |
2005 | | |
2006 | 1.94M | if (Result) |
2007 | 3 | Diag(StartLoc, DiagID) << PrevSpec; |
2008 | | |
2009 | | // At this point, we've successfully parsed a class-specifier in 'definition' |
2010 | | // form (e.g. "struct foo { int x; }". While we could just return here, we're |
2011 | | // going to look at what comes after it to improve error recovery. If an |
2012 | | // impossible token occurs next, we assume that the programmer forgot a ; at |
2013 | | // the end of the declaration and recover that way. |
2014 | | // |
2015 | | // Also enforce C++ [temp]p3: |
2016 | | // In a template-declaration which defines a class, no declarator |
2017 | | // is permitted. |
2018 | | // |
2019 | | // After a type-specifier, we don't expect a semicolon. This only happens in |
2020 | | // C, since definitions are not permitted in this context in C++. |
2021 | 1.94M | if (TUK == Sema::TUK_Definition && |
2022 | 1.15M | (getLangOpts().CPlusPlus || !isTypeSpecifier(DSC)507k ) && |
2023 | 1.15M | (TemplateInfo.Kind || !isValidAfterTypeSpecifier(false)768k )) { |
2024 | 382k | if (Tok.isNot(tok::semi)) { |
2025 | 159 | const PrintingPolicy &PPol = Actions.getASTContext().getPrintingPolicy(); |
2026 | 159 | ExpectAndConsume(tok::semi, diag::err_expected_after, |
2027 | 159 | DeclSpec::getSpecifierName(TagType, PPol)); |
2028 | | // Push this token back into the preprocessor and change our current token |
2029 | | // to ';' so that the rest of the code recovers as though there were an |
2030 | | // ';' after the definition. |
2031 | 159 | PP.EnterToken(Tok, /*IsReinject=*/true); |
2032 | 159 | Tok.setKind(tok::semi); |
2033 | 159 | } |
2034 | 382k | } |
2035 | 1.94M | } |
2036 | | |
2037 | | /// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived]. |
2038 | | /// |
2039 | | /// base-clause : [C++ class.derived] |
2040 | | /// ':' base-specifier-list |
2041 | | /// base-specifier-list: |
2042 | | /// base-specifier '...'[opt] |
2043 | | /// base-specifier-list ',' base-specifier '...'[opt] |
2044 | 187k | void Parser::ParseBaseClause(Decl *ClassDecl) { |
2045 | 187k | assert(Tok.is(tok::colon) && "Not a base clause"); |
2046 | 187k | ConsumeToken(); |
2047 | | |
2048 | | // Build up an array of parsed base specifiers. |
2049 | 187k | SmallVector<CXXBaseSpecifier *, 8> BaseInfo; |
2050 | | |
2051 | 193k | while (true) { |
2052 | | // Parse a base-specifier. |
2053 | 193k | BaseResult Result = ParseBaseSpecifier(ClassDecl); |
2054 | 193k | if (Result.isInvalid()) { |
2055 | | // Skip the rest of this base specifier, up until the comma or |
2056 | | // opening brace. |
2057 | 241 | SkipUntil(tok::comma, tok::l_brace, StopAtSemi | StopBeforeMatch); |
2058 | 193k | } else { |
2059 | | // Add this to our array of base specifiers. |
2060 | 193k | BaseInfo.push_back(Result.get()); |
2061 | 193k | } |
2062 | | |
2063 | | // If the next token is a comma, consume it and keep reading |
2064 | | // base-specifiers. |
2065 | 193k | if (!TryConsumeToken(tok::comma)) |
2066 | 187k | break; |
2067 | 193k | } |
2068 | | |
2069 | | // Attach the base specifiers |
2070 | 187k | Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo); |
2071 | 187k | } |
2072 | | |
2073 | | /// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is |
2074 | | /// one entry in the base class list of a class specifier, for example: |
2075 | | /// class foo : public bar, virtual private baz { |
2076 | | /// 'public bar' and 'virtual private baz' are each base-specifiers. |
2077 | | /// |
2078 | | /// base-specifier: [C++ class.derived] |
2079 | | /// attribute-specifier-seq[opt] base-type-specifier |
2080 | | /// attribute-specifier-seq[opt] 'virtual' access-specifier[opt] |
2081 | | /// base-type-specifier |
2082 | | /// attribute-specifier-seq[opt] access-specifier 'virtual'[opt] |
2083 | | /// base-type-specifier |
2084 | 193k | BaseResult Parser::ParseBaseSpecifier(Decl *ClassDecl) { |
2085 | 193k | bool IsVirtual = false; |
2086 | 193k | SourceLocation StartLoc = Tok.getLocation(); |
2087 | | |
2088 | 193k | ParsedAttributesWithRange Attributes(AttrFactory); |
2089 | 193k | MaybeParseCXX11Attributes(Attributes); |
2090 | | |
2091 | | // Parse the 'virtual' keyword. |
2092 | 193k | if (TryConsumeToken(tok::kw_virtual)) |
2093 | 2.07k | IsVirtual = true; |
2094 | | |
2095 | 193k | CheckMisplacedCXX11Attribute(Attributes, StartLoc); |
2096 | | |
2097 | | // Parse an (optional) access specifier. |
2098 | 193k | AccessSpecifier Access = getAccessSpecifierIfPresent(); |
2099 | 193k | if (Access != AS_none) |
2100 | 120k | ConsumeToken(); |
2101 | | |
2102 | 193k | CheckMisplacedCXX11Attribute(Attributes, StartLoc); |
2103 | | |
2104 | | // Parse the 'virtual' keyword (again!), in case it came after the |
2105 | | // access specifier. |
2106 | 193k | if (Tok.is(tok::kw_virtual)) { |
2107 | 294 | SourceLocation VirtualLoc = ConsumeToken(); |
2108 | 294 | if (IsVirtual) { |
2109 | | // Complain about duplicate 'virtual' |
2110 | 6 | Diag(VirtualLoc, diag::err_dup_virtual) |
2111 | 6 | << FixItHint::CreateRemoval(VirtualLoc); |
2112 | 6 | } |
2113 | | |
2114 | 294 | IsVirtual = true; |
2115 | 294 | } |
2116 | | |
2117 | 193k | CheckMisplacedCXX11Attribute(Attributes, StartLoc); |
2118 | | |
2119 | | // Parse the class-name. |
2120 | | |
2121 | | // HACK: MSVC doesn't consider _Atomic to be a keyword and its STL |
2122 | | // implementation for VS2013 uses _Atomic as an identifier for one of the |
2123 | | // classes in <atomic>. Treat '_Atomic' to be an identifier when we are |
2124 | | // parsing the class-name for a base specifier. |
2125 | 193k | if (getLangOpts().MSVCCompat && Tok.is(tok::kw__Atomic)1.00k && |
2126 | 2 | NextToken().is(tok::less)) |
2127 | 2 | Tok.setKind(tok::identifier); |
2128 | | |
2129 | 193k | SourceLocation EndLocation; |
2130 | 193k | SourceLocation BaseLoc; |
2131 | 193k | TypeResult BaseType = ParseBaseTypeSpecifier(BaseLoc, EndLocation); |
2132 | 193k | if (BaseType.isInvalid()) |
2133 | 136 | return true; |
2134 | | |
2135 | | // Parse the optional ellipsis (for a pack expansion). The ellipsis is |
2136 | | // actually part of the base-specifier-list grammar productions, but we |
2137 | | // parse it here for convenience. |
2138 | 193k | SourceLocation EllipsisLoc; |
2139 | 193k | TryConsumeToken(tok::ellipsis, EllipsisLoc); |
2140 | | |
2141 | | // Find the complete source range for the base-specifier. |
2142 | 193k | SourceRange Range(StartLoc, EndLocation); |
2143 | | |
2144 | | // Notify semantic analysis that we have parsed a complete |
2145 | | // base-specifier. |
2146 | 193k | return Actions.ActOnBaseSpecifier(ClassDecl, Range, Attributes, IsVirtual, |
2147 | 193k | Access, BaseType.get(), BaseLoc, |
2148 | 193k | EllipsisLoc); |
2149 | 193k | } |
2150 | | |
2151 | | /// getAccessSpecifierIfPresent - Determine whether the next token is |
2152 | | /// a C++ access-specifier. |
2153 | | /// |
2154 | | /// access-specifier: [C++ class.derived] |
2155 | | /// 'private' |
2156 | | /// 'protected' |
2157 | | /// 'public' |
2158 | 342k | AccessSpecifier Parser::getAccessSpecifierIfPresent() const { |
2159 | 342k | switch (Tok.getKind()) { |
2160 | 72.8k | default: return AS_none; |
2161 | 44.8k | case tok::kw_private: return AS_private; |
2162 | 14.7k | case tok::kw_protected: return AS_protected; |
2163 | 209k | case tok::kw_public: return AS_public; |
2164 | 342k | } |
2165 | 342k | } |
2166 | | |
2167 | | /// If the given declarator has any parts for which parsing has to be |
2168 | | /// delayed, e.g., default arguments or an exception-specification, create a |
2169 | | /// late-parsed method declaration record to handle the parsing at the end of |
2170 | | /// the class definition. |
2171 | | void Parser::HandleMemberFunctionDeclDelays(Declarator& DeclaratorInfo, |
2172 | 994k | Decl *ThisDecl) { |
2173 | 994k | DeclaratorChunk::FunctionTypeInfo &FTI |
2174 | 994k | = DeclaratorInfo.getFunctionTypeInfo(); |
2175 | | // If there was a late-parsed exception-specification, we'll need a |
2176 | | // late parse |
2177 | 994k | bool NeedLateParse = FTI.getExceptionSpecType() == EST_Unparsed; |
2178 | | |
2179 | 994k | if (!NeedLateParse) { |
2180 | | // Look ahead to see if there are any default args |
2181 | 1.87M | for (unsigned ParamIdx = 0; ParamIdx < FTI.NumParams; ++ParamIdx905k ) { |
2182 | 965k | auto Param = cast<ParmVarDecl>(FTI.Params[ParamIdx].Param); |
2183 | 965k | if (Param->hasUnparsedDefaultArg()) { |
2184 | 60.1k | NeedLateParse = true; |
2185 | 60.1k | break; |
2186 | 60.1k | } |
2187 | 965k | } |
2188 | 965k | } |
2189 | | |
2190 | 994k | if (NeedLateParse) { |
2191 | | // Push this method onto the stack of late-parsed method |
2192 | | // declarations. |
2193 | 89.6k | auto LateMethod = new LateParsedMethodDeclaration(this, ThisDecl); |
2194 | 89.6k | getCurrentClass().LateParsedDeclarations.push_back(LateMethod); |
2195 | | |
2196 | | // Push tokens for each parameter. Those that do not have defaults will be |
2197 | | // NULL. We need to track all the parameters so that we can push them into |
2198 | | // scope for later parameters and perhaps for the exception specification. |
2199 | 89.6k | LateMethod->DefaultArgs.reserve(FTI.NumParams); |
2200 | 248k | for (unsigned ParamIdx = 0; ParamIdx < FTI.NumParams; ++ParamIdx158k ) |
2201 | 158k | LateMethod->DefaultArgs.push_back(LateParsedDefaultArgument( |
2202 | 158k | FTI.Params[ParamIdx].Param, |
2203 | 158k | std::move(FTI.Params[ParamIdx].DefaultArgTokens))); |
2204 | | |
2205 | | // Stash the exception-specification tokens in the late-pased method. |
2206 | 89.6k | if (FTI.getExceptionSpecType() == EST_Unparsed) { |
2207 | 29.5k | LateMethod->ExceptionSpecTokens = FTI.ExceptionSpecTokens; |
2208 | 29.5k | FTI.ExceptionSpecTokens = nullptr; |
2209 | 29.5k | } |
2210 | 89.6k | } |
2211 | 994k | } |
2212 | | |
2213 | | /// isCXX11VirtSpecifier - Determine whether the given token is a C++11 |
2214 | | /// virt-specifier. |
2215 | | /// |
2216 | | /// virt-specifier: |
2217 | | /// override |
2218 | | /// final |
2219 | | /// __final |
2220 | 6.42M | VirtSpecifiers::Specifier Parser::isCXX11VirtSpecifier(const Token &Tok) const { |
2221 | 6.42M | if (!getLangOpts().CPlusPlus || Tok.isNot(tok::identifier)5.55M ) |
2222 | 6.00M | return VirtSpecifiers::VS_None; |
2223 | | |
2224 | 423k | IdentifierInfo *II = Tok.getIdentifierInfo(); |
2225 | | |
2226 | | // Initialize the contextual keywords. |
2227 | 423k | if (!Ident_final) { |
2228 | 1.97k | Ident_final = &PP.getIdentifierTable().get("final"); |
2229 | 1.97k | if (getLangOpts().GNUKeywords) |
2230 | 490 | Ident_GNU_final = &PP.getIdentifierTable().get("__final"); |
2231 | 1.97k | if (getLangOpts().MicrosoftExt) |
2232 | 126 | Ident_sealed = &PP.getIdentifierTable().get("sealed"); |
2233 | 1.97k | Ident_override = &PP.getIdentifierTable().get("override"); |
2234 | 1.97k | } |
2235 | | |
2236 | 423k | if (II == Ident_override) |
2237 | 454 | return VirtSpecifiers::VS_Override; |
2238 | | |
2239 | 422k | if (II == Ident_sealed) |
2240 | 51 | return VirtSpecifiers::VS_Sealed; |
2241 | | |
2242 | 422k | if (II == Ident_final) |
2243 | 398 | return VirtSpecifiers::VS_Final; |
2244 | | |
2245 | 422k | if (II == Ident_GNU_final) |
2246 | 8 | return VirtSpecifiers::VS_GNU_Final; |
2247 | | |
2248 | 422k | return VirtSpecifiers::VS_None; |
2249 | 422k | } |
2250 | | |
2251 | | /// ParseOptionalCXX11VirtSpecifierSeq - Parse a virt-specifier-seq. |
2252 | | /// |
2253 | | /// virt-specifier-seq: |
2254 | | /// virt-specifier |
2255 | | /// virt-specifier-seq virt-specifier |
2256 | | void Parser::ParseOptionalCXX11VirtSpecifierSeq(VirtSpecifiers &VS, |
2257 | | bool IsInterface, |
2258 | 4.81M | SourceLocation FriendLoc) { |
2259 | 4.81M | while (true) { |
2260 | 4.81M | VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier(); |
2261 | 4.81M | if (Specifier == VirtSpecifiers::VS_None) |
2262 | 4.81M | return; |
2263 | | |
2264 | 541 | if (FriendLoc.isValid()) { |
2265 | 6 | Diag(Tok.getLocation(), diag::err_friend_decl_spec) |
2266 | 6 | << VirtSpecifiers::getSpecifierName(Specifier) |
2267 | 6 | << FixItHint::CreateRemoval(Tok.getLocation()) |
2268 | 6 | << SourceRange(FriendLoc, FriendLoc); |
2269 | 6 | ConsumeToken(); |
2270 | 6 | continue; |
2271 | 6 | } |
2272 | | |
2273 | | // C++ [class.mem]p8: |
2274 | | // A virt-specifier-seq shall contain at most one of each virt-specifier. |
2275 | 535 | const char *PrevSpec = nullptr; |
2276 | 535 | if (VS.SetSpecifier(Specifier, Tok.getLocation(), PrevSpec)) |
2277 | 4 | Diag(Tok.getLocation(), diag::err_duplicate_virt_specifier) |
2278 | 4 | << PrevSpec |
2279 | 4 | << FixItHint::CreateRemoval(Tok.getLocation()); |
2280 | | |
2281 | 535 | if (IsInterface && (3 Specifier == VirtSpecifiers::VS_Final3 || |
2282 | 2 | Specifier == VirtSpecifiers::VS_Sealed)) { |
2283 | 1 | Diag(Tok.getLocation(), diag::err_override_control_interface) |
2284 | 1 | << VirtSpecifiers::getSpecifierName(Specifier); |
2285 | 534 | } else if (Specifier == VirtSpecifiers::VS_Sealed) { |
2286 | 6 | Diag(Tok.getLocation(), diag::ext_ms_sealed_keyword); |
2287 | 528 | } else if (Specifier == VirtSpecifiers::VS_GNU_Final) { |
2288 | 2 | Diag(Tok.getLocation(), diag::ext_warn_gnu_final); |
2289 | 526 | } else { |
2290 | 526 | Diag(Tok.getLocation(), |
2291 | 526 | getLangOpts().CPlusPlus11 |
2292 | 518 | ? diag::warn_cxx98_compat_override_control_keyword |
2293 | 8 | : diag::ext_override_control_keyword) |
2294 | 526 | << VirtSpecifiers::getSpecifierName(Specifier); |
2295 | 526 | } |
2296 | 535 | ConsumeToken(); |
2297 | 535 | } |
2298 | 4.81M | } |
2299 | | |
2300 | | /// isCXX11FinalKeyword - Determine whether the next token is a C++11 |
2301 | | /// 'final' or Microsoft 'sealed' contextual keyword. |
2302 | 1.59M | bool Parser::isCXX11FinalKeyword() const { |
2303 | 1.59M | VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier(); |
2304 | 1.59M | return Specifier == VirtSpecifiers::VS_Final || |
2305 | 1.59M | Specifier == VirtSpecifiers::VS_GNU_Final || |
2306 | 1.59M | Specifier == VirtSpecifiers::VS_Sealed; |
2307 | 1.59M | } |
2308 | | |
2309 | | /// Parse a C++ member-declarator up to, but not including, the optional |
2310 | | /// brace-or-equal-initializer or pure-specifier. |
2311 | | bool Parser::ParseCXXMemberDeclaratorBeforeInitializer( |
2312 | | Declarator &DeclaratorInfo, VirtSpecifiers &VS, ExprResult &BitfieldSize, |
2313 | 2.43M | LateParsedAttrList &LateParsedAttrs) { |
2314 | | // member-declarator: |
2315 | | // declarator virt-specifier-seq[opt] pure-specifier[opt] |
2316 | | // declarator requires-clause |
2317 | | // declarator brace-or-equal-initializer[opt] |
2318 | | // identifier attribute-specifier-seq[opt] ':' constant-expression |
2319 | | // brace-or-equal-initializer[opt] |
2320 | | // ':' constant-expression |
2321 | | // |
2322 | | // NOTE: the latter two productions are a proposed bugfix rather than the |
2323 | | // current grammar rules as of C++20. |
2324 | 2.43M | if (Tok.isNot(tok::colon)) |
2325 | 2.43M | ParseDeclarator(DeclaratorInfo); |
2326 | 2.21k | else |
2327 | 2.21k | DeclaratorInfo.SetIdentifier(nullptr, Tok.getLocation()); |
2328 | | |
2329 | 2.43M | if (!DeclaratorInfo.isFunctionDeclarator() && TryConsumeToken(tok::colon)1.42M ) { |
2330 | 26.6k | assert(DeclaratorInfo.isPastIdentifier() && |
2331 | 26.6k | "don't know where identifier would go yet?"); |
2332 | 26.6k | BitfieldSize = ParseConstantExpression(); |
2333 | 26.6k | if (BitfieldSize.isInvalid()) |
2334 | 9 | SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch); |
2335 | 2.40M | } else if (Tok.is(tok::kw_requires)) { |
2336 | 42 | ParseTrailingRequiresClause(DeclaratorInfo); |
2337 | 2.40M | } else { |
2338 | 2.40M | ParseOptionalCXX11VirtSpecifierSeq( |
2339 | 2.40M | VS, getCurrentClass().IsInterface, |
2340 | 2.40M | DeclaratorInfo.getDeclSpec().getFriendSpecLoc()); |
2341 | 2.40M | if (!VS.isUnset()) |
2342 | 514 | MaybeParseAndDiagnoseDeclSpecAfterCXX11VirtSpecifierSeq(DeclaratorInfo, VS); |
2343 | 2.40M | } |
2344 | | |
2345 | | // If a simple-asm-expr is present, parse it. |
2346 | 2.43M | if (Tok.is(tok::kw_asm)) { |
2347 | 8 | SourceLocation Loc; |
2348 | 8 | ExprResult AsmLabel(ParseSimpleAsm(/*ForAsmLabel*/ true, &Loc)); |
2349 | 8 | if (AsmLabel.isInvalid()) |
2350 | 0 | SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch); |
2351 | | |
2352 | 8 | DeclaratorInfo.setAsmLabel(AsmLabel.get()); |
2353 | 8 | DeclaratorInfo.SetRangeEnd(Loc); |
2354 | 8 | } |
2355 | | |
2356 | | // If attributes exist after the declarator, but before an '{', parse them. |
2357 | | // However, this does not apply for [[]] attributes (which could show up |
2358 | | // before or after the __attribute__ attributes). |
2359 | 2.43M | DiagnoseAndSkipCXX11Attributes(); |
2360 | 2.43M | MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs); |
2361 | 2.43M | DiagnoseAndSkipCXX11Attributes(); |
2362 | | |
2363 | | // For compatibility with code written to older Clang, also accept a |
2364 | | // virt-specifier *after* the GNU attributes. |
2365 | 2.43M | if (BitfieldSize.isUnset() && VS.isUnset()2.40M ) { |
2366 | 2.40M | ParseOptionalCXX11VirtSpecifierSeq( |
2367 | 2.40M | VS, getCurrentClass().IsInterface, |
2368 | 2.40M | DeclaratorInfo.getDeclSpec().getFriendSpecLoc()); |
2369 | 2.40M | if (!VS.isUnset()) { |
2370 | | // If we saw any GNU-style attributes that are known to GCC followed by a |
2371 | | // virt-specifier, issue a GCC-compat warning. |
2372 | 5 | for (const ParsedAttr &AL : DeclaratorInfo.getAttributes()) |
2373 | 2 | if (AL.isKnownToGCC() && !AL.isCXX11Attribute()1 ) |
2374 | 1 | Diag(AL.getLoc(), diag::warn_gcc_attribute_location); |
2375 | | |
2376 | 5 | MaybeParseAndDiagnoseDeclSpecAfterCXX11VirtSpecifierSeq(DeclaratorInfo, VS); |
2377 | 5 | } |
2378 | 2.40M | } |
2379 | | |
2380 | | // If this has neither a name nor a bit width, something has gone seriously |
2381 | | // wrong. Skip until the semi-colon or }. |
2382 | 2.43M | if (!DeclaratorInfo.hasName() && BitfieldSize.isUnset()2.31k ) { |
2383 | | // If so, skip until the semi-colon or a }. |
2384 | 99 | SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch); |
2385 | 99 | return true; |
2386 | 99 | } |
2387 | 2.43M | return false; |
2388 | 2.43M | } |
2389 | | |
2390 | | /// Look for declaration specifiers possibly occurring after C++11 |
2391 | | /// virt-specifier-seq and diagnose them. |
2392 | | void Parser::MaybeParseAndDiagnoseDeclSpecAfterCXX11VirtSpecifierSeq( |
2393 | | Declarator &D, |
2394 | 519 | VirtSpecifiers &VS) { |
2395 | 519 | DeclSpec DS(AttrFactory); |
2396 | | |
2397 | | // GNU-style and C++11 attributes are not allowed here, but they will be |
2398 | | // handled by the caller. Diagnose everything else. |
2399 | 519 | ParseTypeQualifierListOpt( |
2400 | 519 | DS, AR_NoAttributesParsed, false, |
2401 | 2 | /*IdentifierRequired=*/false, llvm::function_ref<void()>([&]() { |
2402 | 2 | Actions.CodeCompleteFunctionQualifiers(DS, D, &VS); |
2403 | 2 | })); |
2404 | 519 | D.ExtendWithDeclSpec(DS); |
2405 | | |
2406 | 519 | if (D.isFunctionDeclarator()) { |
2407 | 511 | auto &Function = D.getFunctionTypeInfo(); |
2408 | 511 | if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) { |
2409 | 4 | auto DeclSpecCheck = [&](DeclSpec::TQ TypeQual, StringRef FixItName, |
2410 | 8 | SourceLocation SpecLoc) { |
2411 | 8 | FixItHint Insertion; |
2412 | 8 | auto &MQ = Function.getOrCreateMethodQualifiers(); |
2413 | 8 | if (!(MQ.getTypeQualifiers() & TypeQual)) { |
2414 | 8 | std::string Name(FixItName.data()); |
2415 | 8 | Name += " "; |
2416 | 8 | Insertion = FixItHint::CreateInsertion(VS.getFirstLocation(), Name); |
2417 | 8 | MQ.SetTypeQual(TypeQual, SpecLoc); |
2418 | 8 | } |
2419 | 8 | Diag(SpecLoc, diag::err_declspec_after_virtspec) |
2420 | 8 | << FixItName |
2421 | 8 | << VirtSpecifiers::getSpecifierName(VS.getLastSpecifier()) |
2422 | 8 | << FixItHint::CreateRemoval(SpecLoc) << Insertion; |
2423 | 8 | }; |
2424 | 4 | DS.forEachQualifier(DeclSpecCheck); |
2425 | 4 | } |
2426 | | |
2427 | | // Parse ref-qualifiers. |
2428 | 511 | bool RefQualifierIsLValueRef = true; |
2429 | 511 | SourceLocation RefQualifierLoc; |
2430 | 511 | if (ParseRefQualifier(RefQualifierIsLValueRef, RefQualifierLoc)) { |
2431 | 2 | const char *Name = (RefQualifierIsLValueRef ? "& " : "&& "); |
2432 | 4 | FixItHint Insertion = FixItHint::CreateInsertion(VS.getFirstLocation(), Name); |
2433 | 4 | Function.RefQualifierIsLValueRef = RefQualifierIsLValueRef; |
2434 | 4 | Function.RefQualifierLoc = RefQualifierLoc; |
2435 | | |
2436 | 4 | Diag(RefQualifierLoc, diag::err_declspec_after_virtspec) |
2437 | 2 | << (RefQualifierIsLValueRef ? "&" : "&&") |
2438 | 4 | << VirtSpecifiers::getSpecifierName(VS.getLastSpecifier()) |
2439 | 4 | << FixItHint::CreateRemoval(RefQualifierLoc) |
2440 | 4 | << Insertion; |
2441 | 4 | D.SetRangeEnd(RefQualifierLoc); |
2442 | 4 | } |
2443 | 511 | } |
2444 | 519 | } |
2445 | | |
2446 | | /// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration. |
2447 | | /// |
2448 | | /// member-declaration: |
2449 | | /// decl-specifier-seq[opt] member-declarator-list[opt] ';' |
2450 | | /// function-definition ';'[opt] |
2451 | | /// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO] |
2452 | | /// using-declaration [TODO] |
2453 | | /// [C++0x] static_assert-declaration |
2454 | | /// template-declaration |
2455 | | /// [GNU] '__extension__' member-declaration |
2456 | | /// |
2457 | | /// member-declarator-list: |
2458 | | /// member-declarator |
2459 | | /// member-declarator-list ',' member-declarator |
2460 | | /// |
2461 | | /// member-declarator: |
2462 | | /// declarator virt-specifier-seq[opt] pure-specifier[opt] |
2463 | | /// [C++2a] declarator requires-clause |
2464 | | /// declarator constant-initializer[opt] |
2465 | | /// [C++11] declarator brace-or-equal-initializer[opt] |
2466 | | /// identifier[opt] ':' constant-expression |
2467 | | /// |
2468 | | /// virt-specifier-seq: |
2469 | | /// virt-specifier |
2470 | | /// virt-specifier-seq virt-specifier |
2471 | | /// |
2472 | | /// virt-specifier: |
2473 | | /// override |
2474 | | /// final |
2475 | | /// [MS] sealed |
2476 | | /// |
2477 | | /// pure-specifier: |
2478 | | /// '= 0' |
2479 | | /// |
2480 | | /// constant-initializer: |
2481 | | /// '=' constant-expression |
2482 | | /// |
2483 | | Parser::DeclGroupPtrTy |
2484 | | Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS, |
2485 | | ParsedAttributes &AccessAttrs, |
2486 | | const ParsedTemplateInfo &TemplateInfo, |
2487 | 2.74M | ParsingDeclRAIIObject *TemplateDiags) { |
2488 | 2.74M | if (Tok.is(tok::at)) { |
2489 | 2 | if (getLangOpts().ObjC && NextToken().isObjCAtKeyword(tok::objc_defs)) |
2490 | 1 | Diag(Tok, diag::err_at_defs_cxx); |
2491 | 1 | else |
2492 | 1 | Diag(Tok, diag::err_at_in_class); |
2493 | | |
2494 | 2 | ConsumeToken(); |
2495 | 2 | SkipUntil(tok::r_brace, StopAtSemi); |
2496 | 2 | return nullptr; |
2497 | 2 | } |
2498 | | |
2499 | | // Turn on colon protection early, while parsing declspec, although there is |
2500 | | // nothing to protect there. It prevents from false errors if error recovery |
2501 | | // incorrectly determines where the declspec ends, as in the example: |
2502 | | // struct A { enum class B { C }; }; |
2503 | | // const int C = 4; |
2504 | | // struct D { A::B : C; }; |
2505 | 2.74M | ColonProtectionRAIIObject X(*this); |
2506 | | |
2507 | | // Access declarations. |
2508 | 2.74M | bool MalformedTypeSpec = false; |
2509 | 2.74M | if (!TemplateInfo.Kind && |
2510 | 2.54M | Tok.isOneOf(tok::identifier, tok::coloncolon, tok::kw___super)) { |
2511 | 718k | if (TryAnnotateCXXScopeToken()) |
2512 | 2 | MalformedTypeSpec = true; |
2513 | | |
2514 | 718k | bool isAccessDecl; |
2515 | 718k | if (Tok.isNot(tok::annot_cxxscope)) |
2516 | 706k | isAccessDecl = false; |
2517 | 11.8k | else if (NextToken().is(tok::identifier)) |
2518 | 11.1k | isAccessDecl = GetLookAheadToken(2).is(tok::semi); |
2519 | 708 | else |
2520 | 708 | isAccessDecl = NextToken().is(tok::kw_operator); |
2521 | | |
2522 | 718k | if (isAccessDecl) { |
2523 | | // Collect the scope specifier token we annotated earlier. |
2524 | 105 | CXXScopeSpec SS; |
2525 | 105 | ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/nullptr, |
2526 | 105 | /*ObjectHadErrors=*/false, |
2527 | 105 | /*EnteringContext=*/false); |
2528 | | |
2529 | 105 | if (SS.isInvalid()) { |
2530 | 7 | SkipUntil(tok::semi); |
2531 | 7 | return nullptr; |
2532 | 7 | } |
2533 | | |
2534 | | // Try to parse an unqualified-id. |
2535 | 98 | SourceLocation TemplateKWLoc; |
2536 | 98 | UnqualifiedId Name; |
2537 | 98 | if (ParseUnqualifiedId(SS, /*ObjectType=*/nullptr, |
2538 | 98 | /*ObjectHadErrors=*/false, false, true, true, |
2539 | 0 | false, &TemplateKWLoc, Name)) { |
2540 | 0 | SkipUntil(tok::semi); |
2541 | 0 | return nullptr; |
2542 | 0 | } |
2543 | | |
2544 | | // TODO: recover from mistakenly-qualified operator declarations. |
2545 | 98 | if (ExpectAndConsume(tok::semi, diag::err_expected_after, |
2546 | 0 | "access declaration")) { |
2547 | 0 | SkipUntil(tok::semi); |
2548 | 0 | return nullptr; |
2549 | 0 | } |
2550 | | |
2551 | | // FIXME: We should do something with the 'template' keyword here. |
2552 | 98 | return DeclGroupPtrTy::make(DeclGroupRef(Actions.ActOnUsingDeclaration( |
2553 | 98 | getCurScope(), AS, /*UsingLoc*/ SourceLocation(), |
2554 | 98 | /*TypenameLoc*/ SourceLocation(), SS, Name, |
2555 | 98 | /*EllipsisLoc*/ SourceLocation(), |
2556 | 98 | /*AttrList*/ ParsedAttributesView()))); |
2557 | 98 | } |
2558 | 718k | } |
2559 | | |
2560 | | // static_assert-declaration. A templated static_assert declaration is |
2561 | | // diagnosed in Parser::ParseSingleDeclarationAfterTemplate. |
2562 | 2.74M | if (!TemplateInfo.Kind && |
2563 | 2.54M | Tok.isOneOf(tok::kw_static_assert, tok::kw__Static_assert)) { |
2564 | 14.5k | SourceLocation DeclEnd; |
2565 | 14.5k | return DeclGroupPtrTy::make( |
2566 | 14.5k | DeclGroupRef(ParseStaticAssertDeclaration(DeclEnd))); |
2567 | 14.5k | } |
2568 | | |
2569 | 2.73M | if (Tok.is(tok::kw_template)) { |
2570 | 206k | assert(!TemplateInfo.TemplateParams && |
2571 | 206k | "Nested template improperly parsed?"); |
2572 | 206k | ObjCDeclContextSwitch ObjCDC(*this); |
2573 | 206k | SourceLocation DeclEnd; |
2574 | 206k | return DeclGroupPtrTy::make( |
2575 | 206k | DeclGroupRef(ParseTemplateDeclarationOrSpecialization( |
2576 | 206k | DeclaratorContext::Member, DeclEnd, AccessAttrs, AS))); |
2577 | 206k | } |
2578 | | |
2579 | | // Handle: member-declaration ::= '__extension__' member-declaration |
2580 | 2.52M | if (Tok.is(tok::kw___extension__)) { |
2581 | | // __extension__ silences extension warnings in the subexpression. |
2582 | 404 | ExtensionRAIIObject O(Diags); // Use RAII to do this. |
2583 | 404 | ConsumeToken(); |
2584 | 404 | return ParseCXXClassMemberDeclaration(AS, AccessAttrs, |
2585 | 404 | TemplateInfo, TemplateDiags); |
2586 | 404 | } |
2587 | | |
2588 | 2.52M | ParsedAttributesWithRange attrs(AttrFactory); |
2589 | 2.52M | ParsedAttributesViewWithRange FnAttrs; |
2590 | | // Optional C++11 attribute-specifier |
2591 | 2.52M | MaybeParseCXX11Attributes(attrs); |
2592 | | // We need to keep these attributes for future diagnostic |
2593 | | // before they are taken over by declaration specifier. |
2594 | 2.52M | FnAttrs.addAll(attrs.begin(), attrs.end()); |
2595 | 2.52M | FnAttrs.Range = attrs.Range; |
2596 | | |
2597 | 2.52M | MaybeParseMicrosoftAttributes(attrs); |
2598 | | |
2599 | 2.52M | if (Tok.is(tok::kw_using)) { |
2600 | 47.1k | ProhibitAttributes(attrs); |
2601 | | |
2602 | | // Eat 'using'. |
2603 | 47.1k | SourceLocation UsingLoc = ConsumeToken(); |
2604 | | |
2605 | | // Consume unexpected 'template' keywords. |
2606 | 47.1k | while (Tok.is(tok::kw_template)) { |
2607 | 1 | SourceLocation TemplateLoc = ConsumeToken(); |
2608 | 1 | Diag(TemplateLoc, diag::err_unexpected_template_after_using) |
2609 | 1 | << FixItHint::CreateRemoval(TemplateLoc); |
2610 | 1 | } |
2611 | | |
2612 | 47.1k | if (Tok.is(tok::kw_namespace)) { |
2613 | 3 | Diag(UsingLoc, diag::err_using_namespace_in_class); |
2614 | 3 | SkipUntil(tok::semi, StopBeforeMatch); |
2615 | 3 | return nullptr; |
2616 | 3 | } |
2617 | 47.1k | SourceLocation DeclEnd; |
2618 | | // Otherwise, it must be a using-declaration or an alias-declaration. |
2619 | 47.1k | return ParseUsingDeclaration(DeclaratorContext::Member, TemplateInfo, |
2620 | 47.1k | UsingLoc, DeclEnd, AS); |
2621 | 47.1k | } |
2622 | | |
2623 | | // Hold late-parsed attributes so we can attach a Decl to them later. |
2624 | 2.47M | LateParsedAttrList CommonLateParsedAttrs; |
2625 | | |
2626 | | // decl-specifier-seq: |
2627 | | // Parse the common declaration-specifiers piece. |
2628 | 2.47M | ParsingDeclSpec DS(*this, TemplateDiags); |
2629 | 2.47M | DS.takeAttributesFrom(attrs); |
2630 | 2.47M | if (MalformedTypeSpec) |
2631 | 2 | DS.SetTypeSpecError(); |
2632 | | |
2633 | 2.47M | ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DeclSpecContext::DSC_class, |
2634 | 2.47M | &CommonLateParsedAttrs); |
2635 | | |
2636 | | // Turn off colon protection that was set for declspec. |
2637 | 2.47M | X.restore(); |
2638 | | |
2639 | | // If we had a free-standing type definition with a missing semicolon, we |
2640 | | // may get this far before the problem becomes obvious. |
2641 | 2.47M | if (DS.hasTagDefinition() && |
2642 | 40.6k | TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate && |
2643 | 40.6k | DiagnoseMissingSemiAfterTagDefinition(DS, AS, DeclSpecContext::DSC_class, |
2644 | 40.6k | &CommonLateParsedAttrs)) |
2645 | 0 | return nullptr; |
2646 | | |
2647 | 2.47M | MultiTemplateParamsArg TemplateParams( |
2648 | 181k | TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data() |
2649 | 2.29M | : nullptr, |
2650 | 2.29M | TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size()181k : 0); |
2651 | | |
2652 | 2.47M | if (TryConsumeToken(tok::semi)) { |
2653 | 66.8k | if (DS.isFriendSpecified()) |
2654 | 19.0k | ProhibitAttributes(FnAttrs); |
2655 | | |
2656 | 66.8k | RecordDecl *AnonRecord = nullptr; |
2657 | 66.8k | Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec( |
2658 | 66.8k | getCurScope(), AS, DS, TemplateParams, false, AnonRecord); |
2659 | 66.8k | DS.complete(TheDecl); |
2660 | 66.8k | if (AnonRecord) { |
2661 | 0 | Decl* decls[] = {AnonRecord, TheDecl}; |
2662 | 0 | return Actions.BuildDeclaratorGroup(decls); |
2663 | 0 | } |
2664 | 66.8k | return Actions.ConvertDeclToDeclGroup(TheDecl); |
2665 | 66.8k | } |
2666 | | |
2667 | 2.41M | ParsingDeclarator DeclaratorInfo(*this, DS, DeclaratorContext::Member); |
2668 | 2.41M | if (TemplateInfo.TemplateParams) |
2669 | 163k | DeclaratorInfo.setTemplateParameterLists(TemplateParams); |
2670 | 2.41M | VirtSpecifiers VS; |
2671 | | |
2672 | | // Hold late-parsed attributes so we can attach a Decl to them later. |
2673 | 2.41M | LateParsedAttrList LateParsedAttrs; |
2674 | | |
2675 | 2.41M | SourceLocation EqualLoc; |
2676 | 2.41M | SourceLocation PureSpecLoc; |
2677 | | |
2678 | 12.7k | auto TryConsumePureSpecifier = [&] (bool AllowDefinition) { |
2679 | 12.7k | if (Tok.isNot(tok::equal)) |
2680 | 7.60k | return false; |
2681 | | |
2682 | 5.13k | auto &Zero = NextToken(); |
2683 | 5.13k | SmallString<8> Buffer; |
2684 | 5.13k | if (Zero.isNot(tok::numeric_constant) || |
2685 | 4.64k | PP.getSpelling(Zero, Buffer) != "0") |
2686 | 514 | return false; |
2687 | | |
2688 | 4.62k | auto &After = GetLookAheadToken(2); |
2689 | 4.62k | if (!After.isOneOf(tok::semi, tok::comma) && |
2690 | 3 | !(AllowDefinition && |
2691 | 3 | After.isOneOf(tok::l_brace, tok::colon, tok::kw_try))) |
2692 | 0 | return false; |
2693 | | |
2694 | 4.62k | EqualLoc = ConsumeToken(); |
2695 | 4.62k | PureSpecLoc = ConsumeToken(); |
2696 | 4.62k | return true; |
2697 | 4.62k | }; |
2698 | | |
2699 | 2.41M | SmallVector<Decl *, 8> DeclsInGroup; |
2700 | 2.41M | ExprResult BitfieldSize; |
2701 | 2.41M | ExprResult TrailingRequiresClause; |
2702 | 2.41M | bool ExpectSemi = true; |
2703 | | |
2704 | | // Parse the first declarator. |
2705 | 2.41M | if (ParseCXXMemberDeclaratorBeforeInitializer( |
2706 | 93 | DeclaratorInfo, VS, BitfieldSize, LateParsedAttrs)) { |
2707 | 93 | TryConsumeToken(tok::semi); |
2708 | 93 | return nullptr; |
2709 | 93 | } |
2710 | | |
2711 | | // Check for a member function definition. |
2712 | 2.41M | if (BitfieldSize.isUnset()) { |
2713 | | // MSVC permits pure specifier on inline functions defined at class scope. |
2714 | | // Hence check for =0 before checking for function definition. |
2715 | 2.40M | if (getLangOpts().MicrosoftExt && DeclaratorInfo.isDeclarationOfFunction()12.3k ) |
2716 | 8.13k | TryConsumePureSpecifier(/*AllowDefinition*/ true); |
2717 | | |
2718 | 2.40M | FunctionDefinitionKind DefinitionKind = FunctionDefinitionKind::Declaration; |
2719 | | // function-definition: |
2720 | | // |
2721 | | // In C++11, a non-function declarator followed by an open brace is a |
2722 | | // braced-init-list for an in-class member initialization, not an |
2723 | | // erroneous function definition. |
2724 | 2.40M | if (Tok.is(tok::l_brace) && !getLangOpts().CPlusPlus11492k ) { |
2725 | 2.09k | DefinitionKind = FunctionDefinitionKind::Definition; |
2726 | 2.40M | } else if (DeclaratorInfo.isFunctionDeclarator()) { |
2727 | 1.00M | if (Tok.isOneOf(tok::l_brace, tok::colon, tok::kw_try)) { |
2728 | 606k | DefinitionKind = FunctionDefinitionKind::Definition; |
2729 | 397k | } else if (Tok.is(tok::equal)) { |
2730 | 35.7k | const Token &KW = NextToken(); |
2731 | 35.7k | if (KW.is(tok::kw_default)) |
2732 | 21.0k | DefinitionKind = FunctionDefinitionKind::Defaulted; |
2733 | 14.6k | else if (KW.is(tok::kw_delete)) |
2734 | 10.0k | DefinitionKind = FunctionDefinitionKind::Deleted; |
2735 | 4.61k | else if (KW.is(tok::code_completion)) { |
2736 | 7 | Actions.CodeCompleteAfterFunctionEquals(DeclaratorInfo); |
2737 | 7 | cutOffParsing(); |
2738 | 7 | return nullptr; |
2739 | 7 | } |
2740 | 2.40M | } |
2741 | 1.00M | } |
2742 | 2.40M | DeclaratorInfo.setFunctionDefinitionKind(DefinitionKind); |
2743 | | |
2744 | | // C++11 [dcl.attr.grammar] p4: If an attribute-specifier-seq appertains |
2745 | | // to a friend declaration, that declaration shall be a definition. |
2746 | 2.40M | if (DeclaratorInfo.isFunctionDeclarator() && |
2747 | 1.00M | DefinitionKind == FunctionDefinitionKind::Declaration && |
2748 | 366k | DS.isFriendSpecified()) { |
2749 | | // Diagnose attributes that appear before decl specifier: |
2750 | | // [[]] friend int foo(); |
2751 | 21.1k | ProhibitAttributes(FnAttrs); |
2752 | 21.1k | } |
2753 | | |
2754 | 2.40M | if (DefinitionKind != FunctionDefinitionKind::Declaration) { |
2755 | 639k | if (!DeclaratorInfo.isFunctionDeclarator()) { |
2756 | 2 | Diag(DeclaratorInfo.getIdentifierLoc(), diag::err_func_def_no_params); |
2757 | 2 | ConsumeBrace(); |
2758 | 2 | SkipUntil(tok::r_brace); |
2759 | | |
2760 | | // Consume the optional ';' |
2761 | 2 | TryConsumeToken(tok::semi); |
2762 | | |
2763 | 2 | return nullptr; |
2764 | 2 | } |
2765 | | |
2766 | 639k | if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { |
2767 | 12 | Diag(DeclaratorInfo.getIdentifierLoc(), |
2768 | 12 | diag::err_function_declared_typedef); |
2769 | | |
2770 | | // Recover by treating the 'typedef' as spurious. |
2771 | 12 | DS.ClearStorageClassSpecs(); |
2772 | 12 | } |
2773 | | |
2774 | 639k | Decl *FunDecl = |
2775 | 639k | ParseCXXInlineMethodDef(AS, AccessAttrs, DeclaratorInfo, TemplateInfo, |
2776 | 639k | VS, PureSpecLoc); |
2777 | | |
2778 | 639k | if (FunDecl) { |
2779 | 639k | for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i0 ) { |
2780 | 0 | CommonLateParsedAttrs[i]->addDecl(FunDecl); |
2781 | 0 | } |
2782 | 643k | for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i3.55k ) { |
2783 | 3.55k | LateParsedAttrs[i]->addDecl(FunDecl); |
2784 | 3.55k | } |
2785 | 639k | } |
2786 | 639k | LateParsedAttrs.clear(); |
2787 | | |
2788 | | // Consume the ';' - it's optional unless we have a delete or default |
2789 | 639k | if (Tok.is(tok::semi)) |
2790 | 902 | ConsumeExtraSemi(AfterMemberFunctionDefinition); |
2791 | | |
2792 | 639k | return DeclGroupPtrTy::make(DeclGroupRef(FunDecl)); |
2793 | 639k | } |
2794 | 2.40M | } |
2795 | | |
2796 | | // member-declarator-list: |
2797 | | // member-declarator |
2798 | | // member-declarator-list ',' member-declarator |
2799 | | |
2800 | 1.79M | while (1.77M 1) { |
2801 | 1.79M | InClassInitStyle HasInClassInit = ICIS_NoInit; |
2802 | 1.79M | bool HasStaticInitializer = false; |
2803 | 1.79M | if (Tok.isOneOf(tok::equal, tok::l_brace) && PureSpecLoc.isInvalid()170k ) { |
2804 | | // DRXXXX: Anonymous bit-fields cannot have a brace-or-equal-initializer. |
2805 | 170k | if (BitfieldSize.isUsable() && !DeclaratorInfo.hasName()21 ) { |
2806 | | // Diagnose the error and pretend there is no in-class initializer. |
2807 | 7 | Diag(Tok, diag::err_anon_bitfield_member_init); |
2808 | 7 | SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch); |
2809 | 170k | } else if (DeclaratorInfo.isDeclarationOfFunction()) { |
2810 | | // It's a pure-specifier. |
2811 | 4.61k | if (!TryConsumePureSpecifier(/*AllowFunctionDefinition*/ false)) |
2812 | | // Parse it as an expression so that Sema can diagnose it. |
2813 | 23 | HasStaticInitializer = true; |
2814 | 165k | } else if (DeclaratorInfo.getDeclSpec().getStorageClassSpec() != |
2815 | 165k | DeclSpec::SCS_static && |
2816 | 2.97k | DeclaratorInfo.getDeclSpec().getStorageClassSpec() != |
2817 | 2.97k | DeclSpec::SCS_typedef && |
2818 | 2.96k | !DS.isFriendSpecified()) { |
2819 | | // It's a default member initializer. |
2820 | 2.96k | if (BitfieldSize.get()) |
2821 | 14 | Diag(Tok, getLangOpts().CPlusPlus20 |
2822 | 12 | ? diag::warn_cxx17_compat_bitfield_member_init |
2823 | 2 | : diag::ext_bitfield_member_init); |
2824 | 2.89k | HasInClassInit = Tok.is(tok::equal) ? ICIS_CopyInit : ICIS_ListInit69 ; |
2825 | 163k | } else { |
2826 | 163k | HasStaticInitializer = true; |
2827 | 163k | } |
2828 | 170k | } |
2829 | | |
2830 | | // NOTE: If Sema is the Action module and declarator is an instance field, |
2831 | | // this call will *not* return the created decl; It will return null. |
2832 | | // See Sema::ActOnCXXMemberDeclarator for details. |
2833 | | |
2834 | 1.79M | NamedDecl *ThisDecl = nullptr; |
2835 | 1.79M | if (DS.isFriendSpecified()) { |
2836 | | // C++11 [dcl.attr.grammar] p4: If an attribute-specifier-seq appertains |
2837 | | // to a friend declaration, that declaration shall be a definition. |
2838 | | // |
2839 | | // Diagnose attributes that appear in a friend member function declarator: |
2840 | | // friend int foo [[]] (); |
2841 | 21.1k | SmallVector<SourceRange, 4> Ranges; |
2842 | 21.1k | DeclaratorInfo.getCXX11AttributeRanges(Ranges); |
2843 | 21.1k | for (SmallVectorImpl<SourceRange>::iterator I = Ranges.begin(), |
2844 | 21.1k | E = Ranges.end(); I != E; ++I4 ) |
2845 | 4 | Diag((*I).getBegin(), diag::err_attributes_not_allowed) << *I; |
2846 | | |
2847 | 21.1k | ThisDecl = Actions.ActOnFriendFunctionDecl(getCurScope(), DeclaratorInfo, |
2848 | 21.1k | TemplateParams); |
2849 | 1.77M | } else { |
2850 | 1.77M | ThisDecl = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS, |
2851 | 1.77M | DeclaratorInfo, |
2852 | 1.77M | TemplateParams, |
2853 | 1.77M | BitfieldSize.get(), |
2854 | 1.77M | VS, HasInClassInit); |
2855 | | |
2856 | 1.77M | if (VarTemplateDecl *VT = |
2857 | 485 | ThisDecl ? dyn_cast<VarTemplateDecl>(ThisDecl) : nullptr) |
2858 | | // Re-direct this decl to refer to the templated decl so that we can |
2859 | | // initialize it. |
2860 | 485 | ThisDecl = VT->getTemplatedDecl(); |
2861 | | |
2862 | 1.77M | if (ThisDecl) |
2863 | 1.77M | Actions.ProcessDeclAttributeList(getCurScope(), ThisDecl, AccessAttrs); |
2864 | 1.77M | } |
2865 | | |
2866 | | // Error recovery might have converted a non-static member into a static |
2867 | | // member. |
2868 | 1.79M | if (HasInClassInit != ICIS_NoInit && |
2869 | 2.96k | DeclaratorInfo.getDeclSpec().getStorageClassSpec() == |
2870 | 6 | DeclSpec::SCS_static) { |
2871 | 6 | HasInClassInit = ICIS_NoInit; |
2872 | 6 | HasStaticInitializer = true; |
2873 | 6 | } |
2874 | | |
2875 | 1.79M | if (ThisDecl && PureSpecLoc.isValid()1.79M ) |
2876 | 4.62k | Actions.ActOnPureSpecifier(ThisDecl, PureSpecLoc); |
2877 | | |
2878 | | // Handle the initializer. |
2879 | 1.79M | if (HasInClassInit != ICIS_NoInit) { |
2880 | | // The initializer was deferred; parse it and cache the tokens. |
2881 | 2.96k | Diag(Tok, getLangOpts().CPlusPlus11 |
2882 | 2.94k | ? diag::warn_cxx98_compat_nonstatic_member_init |
2883 | 21 | : diag::ext_nonstatic_member_init); |
2884 | | |
2885 | 2.96k | if (DeclaratorInfo.isArrayOfUnknownBound()) { |
2886 | | // C++11 [dcl.array]p3: An array bound may also be omitted when the |
2887 | | // declarator is followed by an initializer. |
2888 | | // |
2889 | | // A brace-or-equal-initializer for a member-declarator is not an |
2890 | | // initializer in the grammar, so this is ill-formed. |
2891 | 4 | Diag(Tok, diag::err_incomplete_array_member_init); |
2892 | 4 | SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch); |
2893 | | |
2894 | | // Avoid later warnings about a class member of incomplete type. |
2895 | 4 | if (ThisDecl) |
2896 | 3 | ThisDecl->setInvalidDecl(); |
2897 | 4 | } else |
2898 | 2.95k | ParseCXXNonStaticMemberInitializer(ThisDecl); |
2899 | 1.79M | } else if (HasStaticInitializer) { |
2900 | | // Normal initializer. |
2901 | 163k | ExprResult Init = ParseCXXMemberInitializer( |
2902 | 163k | ThisDecl, DeclaratorInfo.isDeclarationOfFunction(), EqualLoc); |
2903 | | |
2904 | 163k | if (Init.isInvalid()) |
2905 | 10 | SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch); |
2906 | 163k | else if (ThisDecl) |
2907 | 163k | Actions.AddInitializerToDecl(ThisDecl, Init.get(), EqualLoc.isInvalid()); |
2908 | 1.62M | } else if (ThisDecl && DS.getStorageClassSpec() == DeclSpec::SCS_static1.62M ) |
2909 | | // No initializer. |
2910 | 40.0k | Actions.ActOnUninitializedDecl(ThisDecl); |
2911 | | |
2912 | 1.79M | if (ThisDecl) { |
2913 | 1.79M | if (!ThisDecl->isInvalidDecl()) { |
2914 | | // Set the Decl for any late parsed attributes |
2915 | 1.79M | for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i21 ) |
2916 | 21 | CommonLateParsedAttrs[i]->addDecl(ThisDecl); |
2917 | | |
2918 | 1.79M | for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i1.64k ) |
2919 | 1.64k | LateParsedAttrs[i]->addDecl(ThisDecl); |
2920 | 1.79M | } |
2921 | 1.79M | Actions.FinalizeDeclaration(ThisDecl); |
2922 | 1.79M | DeclsInGroup.push_back(ThisDecl); |
2923 | | |
2924 | 1.79M | if (DeclaratorInfo.isFunctionDeclarator() && |
2925 | 366k | DeclaratorInfo.getDeclSpec().getStorageClassSpec() != |
2926 | 366k | DeclSpec::SCS_typedef) |
2927 | 355k | HandleMemberFunctionDeclDelays(DeclaratorInfo, ThisDecl); |
2928 | 1.79M | } |
2929 | 1.79M | LateParsedAttrs.clear(); |
2930 | | |
2931 | 1.79M | DeclaratorInfo.complete(ThisDecl); |
2932 | | |
2933 | | // If we don't have a comma, it is either the end of the list (a ';') |
2934 | | // or an error, bail out. |
2935 | 1.79M | SourceLocation CommaLoc; |
2936 | 1.79M | if (!TryConsumeToken(tok::comma, CommaLoc)) |
2937 | 1.77M | break; |
2938 | | |
2939 | 22.0k | if (Tok.isAtStartOfLine() && |
2940 | 18.2k | !MightBeDeclarator(DeclaratorContext::Member)) { |
2941 | | // This comma was followed by a line-break and something which can't be |
2942 | | // the start of a declarator. The comma was probably a typo for a |
2943 | | // semicolon. |
2944 | 43 | Diag(CommaLoc, diag::err_expected_semi_declaration) |
2945 | 43 | << FixItHint::CreateReplacement(CommaLoc, ";"); |
2946 | 43 | ExpectSemi = false; |
2947 | 43 | break; |
2948 | 43 | } |
2949 | | |
2950 | | // Parse the next declarator. |
2951 | 22.0k | DeclaratorInfo.clear(); |
2952 | 22.0k | VS.clear(); |
2953 | 22.0k | BitfieldSize = ExprResult(/*Invalid=*/false); |
2954 | 22.0k | EqualLoc = PureSpecLoc = SourceLocation(); |
2955 | 22.0k | DeclaratorInfo.setCommaLoc(CommaLoc); |
2956 | | |
2957 | | // GNU attributes are allowed before the second and subsequent declarator. |
2958 | | // However, this does not apply for [[]] attributes (which could show up |
2959 | | // before or after the __attribute__ attributes). |
2960 | 22.0k | DiagnoseAndSkipCXX11Attributes(); |
2961 | 22.0k | MaybeParseGNUAttributes(DeclaratorInfo); |
2962 | 22.0k | DiagnoseAndSkipCXX11Attributes(); |
2963 | | |
2964 | 22.0k | if (ParseCXXMemberDeclaratorBeforeInitializer( |
2965 | 22.0k | DeclaratorInfo, VS, BitfieldSize, LateParsedAttrs)) |
2966 | 6 | break; |
2967 | 22.0k | } |
2968 | | |
2969 | 1.77M | if (ExpectSemi && |
2970 | 1.77M | ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list)) { |
2971 | | // Skip to end of block or statement. |
2972 | 41 | SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch); |
2973 | | // If we stopped at a ';', eat it. |
2974 | 41 | TryConsumeToken(tok::semi); |
2975 | 41 | return nullptr; |
2976 | 41 | } |
2977 | | |
2978 | 1.77M | return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup); |
2979 | 1.77M | } |
2980 | | |
2981 | | /// ParseCXXMemberInitializer - Parse the brace-or-equal-initializer. |
2982 | | /// Also detect and reject any attempted defaulted/deleted function definition. |
2983 | | /// The location of the '=', if any, will be placed in EqualLoc. |
2984 | | /// |
2985 | | /// This does not check for a pure-specifier; that's handled elsewhere. |
2986 | | /// |
2987 | | /// brace-or-equal-initializer: |
2988 | | /// '=' initializer-expression |
2989 | | /// braced-init-list |
2990 | | /// |
2991 | | /// initializer-clause: |
2992 | | /// assignment-expression |
2993 | | /// braced-init-list |
2994 | | /// |
2995 | | /// defaulted/deleted function-definition: |
2996 | | /// '=' 'default' |
2997 | | /// '=' 'delete' |
2998 | | /// |
2999 | | /// Prior to C++0x, the assignment-expression in an initializer-clause must |
3000 | | /// be a constant-expression. |
3001 | | ExprResult Parser::ParseCXXMemberInitializer(Decl *D, bool IsFunction, |
3002 | 165k | SourceLocation &EqualLoc) { |
3003 | 165k | assert(Tok.isOneOf(tok::equal, tok::l_brace) |
3004 | 165k | && "Data member initializer not starting with '=' or '{'"); |
3005 | | |
3006 | 165k | EnterExpressionEvaluationContext Context( |
3007 | 165k | Actions, Sema::ExpressionEvaluationContext::PotentiallyEvaluated, D); |
3008 | 165k | if (TryConsumeToken(tok::equal, EqualLoc)) { |
3009 | 165k | if (Tok.is(tok::kw_delete)) { |
3010 | | // In principle, an initializer of '= delete p;' is legal, but it will |
3011 | | // never type-check. It's better to diagnose it as an ill-formed expression |
3012 | | // than as an ill-formed deleted non-function member. |
3013 | | // An initializer of '= delete p, foo' will never be parsed, because |
3014 | | // a top-level comma always ends the initializer expression. |
3015 | 2 | const Token &Next = NextToken(); |
3016 | 2 | if (IsFunction || Next.isOneOf(tok::semi, tok::comma, tok::eof)) { |
3017 | 2 | if (IsFunction) |
3018 | 0 | Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration) |
3019 | 0 | << 1 /* delete */; |
3020 | 2 | else |
3021 | 2 | Diag(ConsumeToken(), diag::err_deleted_non_function); |
3022 | 2 | return ExprError(); |
3023 | 2 | } |
3024 | 165k | } else if (Tok.is(tok::kw_default)) { |
3025 | 0 | if (IsFunction) |
3026 | 0 | Diag(Tok, diag::err_default_delete_in_multiple_declaration) |
3027 | 0 | << 0 /* default */; |
3028 | 0 | else |
3029 | 0 | Diag(ConsumeToken(), diag::err_default_special_members) |
3030 | 0 | << getLangOpts().CPlusPlus20; |
3031 | 0 | return ExprError(); |
3032 | 0 | } |
3033 | 165k | } |
3034 | 165k | if (const auto *PD = dyn_cast_or_null<MSPropertyDecl>(D)) { |
3035 | 1 | Diag(Tok, diag::err_ms_property_initializer) << PD; |
3036 | 1 | return ExprError(); |
3037 | 1 | } |
3038 | 165k | return ParseInitializer(); |
3039 | 165k | } |
3040 | | |
3041 | | void Parser::SkipCXXMemberSpecification(SourceLocation RecordLoc, |
3042 | | SourceLocation AttrFixitLoc, |
3043 | 254 | unsigned TagType, Decl *TagDecl) { |
3044 | | // Skip the optional 'final' keyword. |
3045 | 254 | if (getLangOpts().CPlusPlus && Tok.is(tok::identifier)) { |
3046 | 0 | assert(isCXX11FinalKeyword() && "not a class definition"); |
3047 | 0 | ConsumeToken(); |
3048 | | |
3049 | | // Diagnose any C++11 attributes after 'final' keyword. |
3050 | | // We deliberately discard these attributes. |
3051 | 0 | ParsedAttributesWithRange Attrs(AttrFactory); |
3052 | 0 | CheckMisplacedCXX11Attribute(Attrs, AttrFixitLoc); |
3053 | | |
3054 | | // This can only happen if we had malformed misplaced attributes; |
3055 | | // we only get called if there is a colon or left-brace after the |
3056 | | // attributes. |
3057 | 0 | if (Tok.isNot(tok::colon) && Tok.isNot(tok::l_brace)) |
3058 | 0 | return; |
3059 | 254 | } |
3060 | | |
3061 | | // Skip the base clauses. This requires actually parsing them, because |
3062 | | // otherwise we can't be sure where they end (a left brace may appear |
3063 | | // within a template argument). |
3064 | 254 | if (Tok.is(tok::colon)) { |
3065 | | // Enter the scope of the class so that we can correctly parse its bases. |
3066 | 47 | ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope); |
3067 | 47 | ParsingClassDefinition ParsingDef(*this, TagDecl, /*NonNestedClass*/ true, |
3068 | 47 | TagType == DeclSpec::TST_interface); |
3069 | 47 | auto OldContext = |
3070 | 47 | Actions.ActOnTagStartSkippedDefinition(getCurScope(), TagDecl); |
3071 | | |
3072 | | // Parse the bases but don't attach them to the class. |
3073 | 47 | ParseBaseClause(nullptr); |
3074 | | |
3075 | 47 | Actions.ActOnTagFinishSkippedDefinition(OldContext); |
3076 | | |
3077 | 47 | if (!Tok.is(tok::l_brace)) { |
3078 | 0 | Diag(PP.getLocForEndOfToken(PrevTokLocation), |
3079 | 0 | diag::err_expected_lbrace_after_base_specifiers); |
3080 | 0 | return; |
3081 | 0 | } |
3082 | 254 | } |
3083 | | |
3084 | | // Skip the body. |
3085 | 254 | assert(Tok.is(tok::l_brace)); |
3086 | 254 | BalancedDelimiterTracker T(*this, tok::l_brace); |
3087 | 254 | T.consumeOpen(); |
3088 | 254 | T.skipToEnd(); |
3089 | | |
3090 | | // Parse and discard any trailing attributes. |
3091 | 254 | ParsedAttributes Attrs(AttrFactory); |
3092 | 254 | if (Tok.is(tok::kw___attribute)) |
3093 | 5 | MaybeParseGNUAttributes(Attrs); |
3094 | 254 | } |
3095 | | |
3096 | | Parser::DeclGroupPtrTy Parser::ParseCXXClassMemberDeclarationWithPragmas( |
3097 | | AccessSpecifier &AS, ParsedAttributesWithRange &AccessAttrs, |
3098 | 2.68M | DeclSpec::TST TagType, Decl *TagDecl) { |
3099 | 2.68M | ParenBraceBracketBalancer BalancerRAIIObj(*this); |
3100 | | |
3101 | 2.68M | switch (Tok.getKind()) { |
3102 | 5 | case tok::kw___if_exists: |
3103 | 8 | case tok::kw___if_not_exists: |
3104 | 8 | ParseMicrosoftIfExistsClassDeclaration(TagType, AccessAttrs, AS); |
3105 | 8 | return nullptr; |
3106 | | |
3107 | 39 | case tok::semi: |
3108 | | // Check for extraneous top-level semicolon. |
3109 | 39 | ConsumeExtraSemi(InsideStruct, TagType); |
3110 | 39 | return nullptr; |
3111 | | |
3112 | | // Handle pragmas that can appear as member declarations. |
3113 | 2 | case tok::annot_pragma_vis: |
3114 | 2 | HandlePragmaVisibility(); |
3115 | 2 | return nullptr; |
3116 | 0 | case tok::annot_pragma_pack: |
3117 | 0 | HandlePragmaPack(); |
3118 | 0 | return nullptr; |
3119 | 1 | case tok::annot_pragma_align: |
3120 | 1 | HandlePragmaAlign(); |
3121 | 1 | return nullptr; |
3122 | 0 | case tok::annot_pragma_ms_pointers_to_members: |
3123 | 0 | HandlePragmaMSPointersToMembers(); |
3124 | 0 | return nullptr; |
3125 | 6 | case tok::annot_pragma_ms_pragma: |
3126 | 6 | HandlePragmaMSPragma(); |
3127 | 6 | return nullptr; |
3128 | 2 | case tok::annot_pragma_ms_vtordisp: |
3129 | 2 | HandlePragmaMSVtorDisp(); |
3130 | 2 | return nullptr; |
3131 | 0 | case tok::annot_pragma_dump: |
3132 | 0 | HandlePragmaDump(); |
3133 | 0 | return nullptr; |
3134 | | |
3135 | 2 | case tok::kw_namespace: |
3136 | | // If we see a namespace here, a close brace was missing somewhere. |
3137 | 2 | DiagnoseUnexpectedNamespace(cast<NamedDecl>(TagDecl)); |
3138 | 2 | return nullptr; |
3139 | | |
3140 | 39.1k | case tok::kw_private: |
3141 | | // FIXME: We don't accept GNU attributes on access specifiers in OpenCL mode |
3142 | | // yet. |
3143 | 39.1k | if (getLangOpts().OpenCL && !NextToken().is(tok::colon)6 ) |
3144 | 4 | return ParseCXXClassMemberDeclaration(AS, AccessAttrs); |
3145 | 39.1k | LLVM_FALLTHROUGH; |
3146 | 134k | case tok::kw_public: |
3147 | 148k | case tok::kw_protected: { |
3148 | 148k | AccessSpecifier NewAS = getAccessSpecifierIfPresent(); |
3149 | 148k | assert(NewAS != AS_none); |
3150 | | // Current token is a C++ access specifier. |
3151 | 148k | AS = NewAS; |
3152 | 148k | SourceLocation ASLoc = Tok.getLocation(); |
3153 | 148k | unsigned TokLength = Tok.getLength(); |
3154 | 148k | ConsumeToken(); |
3155 | 148k | AccessAttrs.clear(); |
3156 | 148k | MaybeParseGNUAttributes(AccessAttrs); |
3157 | | |
3158 | 148k | SourceLocation EndLoc; |
3159 | 148k | if (TryConsumeToken(tok::colon, EndLoc)) { |
3160 | 10 | } else if (TryConsumeToken(tok::semi, EndLoc)) { |
3161 | 3 | Diag(EndLoc, diag::err_expected) |
3162 | 3 | << tok::colon << FixItHint::CreateReplacement(EndLoc, ":"); |
3163 | 7 | } else { |
3164 | 7 | EndLoc = ASLoc.getLocWithOffset(TokLength); |
3165 | 7 | Diag(EndLoc, diag::err_expected) |
3166 | 7 | << tok::colon << FixItHint::CreateInsertion(EndLoc, ":"); |
3167 | 7 | } |
3168 | | |
3169 | | // The Microsoft extension __interface does not permit non-public |
3170 | | // access specifiers. |
3171 | 148k | if (TagType == DeclSpec::TST_interface && AS != AS_public5 ) { |
3172 | 4 | Diag(ASLoc, diag::err_access_specifier_interface) << (AS == AS_protected); |
3173 | 4 | } |
3174 | | |
3175 | 148k | if (Actions.ActOnAccessSpecifier(NewAS, ASLoc, EndLoc, AccessAttrs)) { |
3176 | | // found another attribute than only annotations |
3177 | 1 | AccessAttrs.clear(); |
3178 | 1 | } |
3179 | | |
3180 | 148k | return nullptr; |
3181 | 134k | } |
3182 | | |
3183 | 633 | case tok::annot_pragma_openmp: |
3184 | 633 | return ParseOpenMPDeclarativeDirectiveWithExtDecl( |
3185 | 633 | AS, AccessAttrs, /*Delayed=*/true, TagType, TagDecl); |
3186 | | |
3187 | 2.54M | default: |
3188 | 2.54M | if (tok::isPragmaAnnotation(Tok.getKind())) { |
3189 | 4 | Diag(Tok.getLocation(), diag::err_pragma_misplaced_in_decl) |
3190 | 4 | << DeclSpec::getSpecifierName(TagType, |
3191 | 4 | Actions.getASTContext().getPrintingPolicy()); |
3192 | 4 | ConsumeAnnotationToken(); |
3193 | 4 | return nullptr; |
3194 | 4 | } |
3195 | 2.54M | return ParseCXXClassMemberDeclaration(AS, AccessAttrs); |
3196 | 2.68M | } |
3197 | 2.68M | } |
3198 | | |
3199 | | /// ParseCXXMemberSpecification - Parse the class definition. |
3200 | | /// |
3201 | | /// member-specification: |
3202 | | /// member-declaration member-specification[opt] |
3203 | | /// access-specifier ':' member-specification[opt] |
3204 | | /// |
3205 | | void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc, |
3206 | | SourceLocation AttrFixitLoc, |
3207 | | ParsedAttributesWithRange &Attrs, |
3208 | 642k | unsigned TagType, Decl *TagDecl) { |
3209 | 642k | assert((TagType == DeclSpec::TST_struct || |
3210 | 642k | TagType == DeclSpec::TST_interface || |
3211 | 642k | TagType == DeclSpec::TST_union || |
3212 | 642k | TagType == DeclSpec::TST_class) && "Invalid TagType!"); |
3213 | | |
3214 | 1 | llvm::TimeTraceScope TimeScope("ParseClass", [&]() { |
3215 | 1 | if (auto *TD = dyn_cast_or_null<NamedDecl>(TagDecl)) |
3216 | 1 | return TD->getQualifiedNameAsString(); |
3217 | 0 | return std::string("<anonymous>"); |
3218 | 0 | }); |
3219 | | |
3220 | 642k | PrettyDeclStackTraceEntry CrashInfo(Actions.Context, TagDecl, RecordLoc, |
3221 | 642k | "parsing struct/union/class body"); |
3222 | | |
3223 | | // Determine whether this is a non-nested class. Note that local |
3224 | | // classes are *not* considered to be nested classes. |
3225 | 642k | bool NonNestedClass = true; |
3226 | 642k | if (!ClassStack.empty()) { |
3227 | 53.1k | for (const Scope *S = getCurScope(); S; S = S->getParent()10.7k ) { |
3228 | 53.1k | if (S->isClassScope()) { |
3229 | | // We're inside a class scope, so this is a nested class. |
3230 | 39.4k | NonNestedClass = false; |
3231 | | |
3232 | | // The Microsoft extension __interface does not permit nested classes. |
3233 | 39.4k | if (getCurrentClass().IsInterface) { |
3234 | 1 | Diag(RecordLoc, diag::err_invalid_member_in_interface) |
3235 | 1 | << /*ErrorType=*/6 |
3236 | 1 | << (isa<NamedDecl>(TagDecl) |
3237 | 1 | ? cast<NamedDecl>(TagDecl)->getQualifiedNameAsString() |
3238 | 0 | : "(anonymous)"); |
3239 | 1 | } |
3240 | 39.4k | break; |
3241 | 39.4k | } |
3242 | | |
3243 | 13.7k | if ((S->getFlags() & Scope::FnScope)) |
3244 | | // If we're in a function or function template then this is a local |
3245 | | // class rather than a nested class. |
3246 | 3.00k | break; |
3247 | 13.7k | } |
3248 | 42.4k | } |
3249 | | |
3250 | | // Enter a scope for the class. |
3251 | 642k | ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope); |
3252 | | |
3253 | | // Note that we are parsing a new (potentially-nested) class definition. |
3254 | 642k | ParsingClassDefinition ParsingDef(*this, TagDecl, NonNestedClass, |
3255 | 642k | TagType == DeclSpec::TST_interface); |
3256 | | |
3257 | 642k | if (TagDecl) |
3258 | 642k | Actions.ActOnTagStartDefinition(getCurScope(), TagDecl); |
3259 | | |
3260 | 642k | SourceLocation FinalLoc; |
3261 | 642k | bool IsFinalSpelledSealed = false; |
3262 | | |
3263 | | // Parse the optional 'final' keyword. |
3264 | 642k | if (getLangOpts().CPlusPlus && Tok.is(tok::identifier)) { |
3265 | 112 | VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier(Tok); |
3266 | 112 | assert((Specifier == VirtSpecifiers::VS_Final || |
3267 | 112 | Specifier == VirtSpecifiers::VS_GNU_Final || |
3268 | 112 | Specifier == VirtSpecifiers::VS_Sealed) && |
3269 | 112 | "not a class definition"); |
3270 | 112 | FinalLoc = ConsumeToken(); |
3271 | 112 | IsFinalSpelledSealed = Specifier == VirtSpecifiers::VS_Sealed; |
3272 | | |
3273 | 112 | if (TagType == DeclSpec::TST_interface) |
3274 | 4 | Diag(FinalLoc, diag::err_override_control_interface) |
3275 | 4 | << VirtSpecifiers::getSpecifierName(Specifier); |
3276 | 108 | else if (Specifier == VirtSpecifiers::VS_Final) |
3277 | 94 | Diag(FinalLoc, getLangOpts().CPlusPlus11 |
3278 | 92 | ? diag::warn_cxx98_compat_override_control_keyword |
3279 | 2 | : diag::ext_override_control_keyword) |
3280 | 94 | << VirtSpecifiers::getSpecifierName(Specifier); |
3281 | 14 | else if (Specifier == VirtSpecifiers::VS_Sealed) |
3282 | 12 | Diag(FinalLoc, diag::ext_ms_sealed_keyword); |
3283 | 2 | else if (Specifier == VirtSpecifiers::VS_GNU_Final) |
3284 | 2 | Diag(FinalLoc, diag::ext_warn_gnu_final); |
3285 | | |
3286 | | // Parse any C++11 attributes after 'final' keyword. |
3287 | | // These attributes are not allowed to appear here, |
3288 | | // and the only possible place for them to appertain |
3289 | | // to the class would be between class-key and class-name. |
3290 | 112 | CheckMisplacedCXX11Attribute(Attrs, AttrFixitLoc); |
3291 | | |
3292 | | // ParseClassSpecifier() does only a superficial check for attributes before |
3293 | | // deciding to call this method. For example, for |
3294 | | // `class C final alignas ([l) {` it will decide that this looks like a |
3295 | | // misplaced attribute since it sees `alignas '(' ')'`. But the actual |
3296 | | // attribute parsing code will try to parse the '[' as a constexpr lambda |
3297 | | // and consume enough tokens that the alignas parsing code will eat the |
3298 | | // opening '{'. So bail out if the next token isn't one we expect. |
3299 | 112 | if (!Tok.is(tok::colon) && !Tok.is(tok::l_brace)52 ) { |
3300 | 2 | if (TagDecl) |
3301 | 2 | Actions.ActOnTagDefinitionError(getCurScope(), TagDecl); |
3302 | 2 | return; |
3303 | 2 | } |
3304 | 642k | } |
3305 | | |
3306 | 642k | if (Tok.is(tok::colon)) { |
3307 | 187k | ParseScope InheritanceScope(this, getCurScope()->getFlags() | |
3308 | 187k | Scope::ClassInheritanceScope); |
3309 | | |
3310 | 187k | ParseBaseClause(TagDecl); |
3311 | 187k | if (!Tok.is(tok::l_brace)) { |
3312 | 44 | bool SuggestFixIt = false; |
3313 | 44 | SourceLocation BraceLoc = PP.getLocForEndOfToken(PrevTokLocation); |
3314 | 44 | if (Tok.isAtStartOfLine()) { |
3315 | 36 | switch (Tok.getKind()) { |
3316 | 0 | case tok::kw_private: |
3317 | 5 | case tok::kw_protected: |
3318 | 5 | case tok::kw_public: |
3319 | 5 | SuggestFixIt = NextToken().getKind() == tok::colon; |
3320 | 5 | break; |
3321 | 3 | case tok::kw_static_assert: |
3322 | 3 | case tok::r_brace: |
3323 | 8 | case tok::kw_using: |
3324 | | // base-clause can have simple-template-id; 'template' can't be there |
3325 | 13 | case tok::kw_template: |
3326 | 13 | SuggestFixIt = true; |
3327 | 13 | break; |
3328 | 9 | case tok::identifier: |
3329 | 9 | SuggestFixIt = isConstructorDeclarator(true); |
3330 | 9 | break; |
3331 | 9 | default: |
3332 | 9 | SuggestFixIt = isCXXSimpleDeclaration(/*AllowForRangeDecl=*/false); |
3333 | 9 | break; |
3334 | 44 | } |
3335 | 44 | } |
3336 | 44 | DiagnosticBuilder LBraceDiag = |
3337 | 44 | Diag(BraceLoc, diag::err_expected_lbrace_after_base_specifiers); |
3338 | 44 | if (SuggestFixIt) { |
3339 | 31 | LBraceDiag << FixItHint::CreateInsertion(BraceLoc, " {"); |
3340 | | // Try recovering from missing { after base-clause. |
3341 | 31 | PP.EnterToken(Tok, /*IsReinject*/true); |
3342 | 31 | Tok.setKind(tok::l_brace); |
3343 | 13 | } else { |
3344 | 13 | if (TagDecl) |
3345 | 12 | Actions.ActOnTagDefinitionError(getCurScope(), TagDecl); |
3346 | 13 | return; |
3347 | 13 | } |
3348 | 642k | } |
3349 | 187k | } |
3350 | | |
3351 | 642k | assert(Tok.is(tok::l_brace)); |
3352 | 642k | BalancedDelimiterTracker T(*this, tok::l_brace); |
3353 | 642k | T.consumeOpen(); |
3354 | | |
3355 | 642k | if (TagDecl) |
3356 | 642k | Actions.ActOnStartCXXMemberDeclarations(getCurScope(), TagDecl, FinalLoc, |
3357 | 642k | IsFinalSpelledSealed, |
3358 | 642k | T.getOpenLocation()); |
3359 | | |
3360 | | // C++ 11p3: Members of a class defined with the keyword class are private |
3361 | | // by default. Members of a class defined with the keywords struct or union |
3362 | | // are public by default. |
3363 | 642k | AccessSpecifier CurAS; |
3364 | 642k | if (TagType == DeclSpec::TST_class) |
3365 | 93.0k | CurAS = AS_private; |
3366 | 549k | else |
3367 | 549k | CurAS = AS_public; |
3368 | 642k | ParsedAttributesWithRange AccessAttrs(AttrFactory); |
3369 | | |
3370 | 642k | if (TagDecl) { |
3371 | | // While we still have something to read, read the member-declarations. |
3372 | 3.33M | while (!tryParseMisplacedModuleImport() && Tok.isNot(tok::r_brace) && |
3373 | 2.68M | Tok.isNot(tok::eof)) { |
3374 | | // Each iteration of this loop reads one member-declaration. |
3375 | 2.68M | ParseCXXClassMemberDeclarationWithPragmas( |
3376 | 2.68M | CurAS, AccessAttrs, static_cast<DeclSpec::TST>(TagType), TagDecl); |
3377 | 2.68M | MaybeDestroyTemplateIds(); |
3378 | 2.68M | } |
3379 | 642k | T.consumeClose(); |
3380 | 209 | } else { |
3381 | 209 | SkipUntil(tok::r_brace); |
3382 | 209 | } |
3383 | | |
3384 | | // If attributes exist after class contents, parse them. |
3385 | 642k | ParsedAttributes attrs(AttrFactory); |
3386 | 642k | MaybeParseGNUAttributes(attrs); |
3387 | | |
3388 | 642k | if (TagDecl) |
3389 | 642k | Actions.ActOnFinishCXXMemberSpecification(getCurScope(), RecordLoc, TagDecl, |
3390 | 642k | T.getOpenLocation(), |
3391 | 642k | T.getCloseLocation(), attrs); |
3392 | | |
3393 | | // C++11 [class.mem]p2: |
3394 | | // Within the class member-specification, the class is regarded as complete |
3395 | | // within function bodies, default arguments, exception-specifications, and |
3396 | | // brace-or-equal-initializers for non-static data members (including such |
3397 | | // things in nested classes). |
3398 | 642k | if (TagDecl && NonNestedClass642k ) { |
3399 | | // We are not inside a nested class. This class and its nested classes |
3400 | | // are complete and we can parse the delayed portions of method |
3401 | | // declarations and the lexed inline method definitions, along with any |
3402 | | // delayed attributes. |
3403 | | |
3404 | | // Save the state of Sema.FPFeatures, and change the setting |
3405 | | // to the levels specified on the command line. Previous level |
3406 | | // will be restored when the RAII object is destroyed. |
3407 | 603k | Sema::FPFeaturesStateRAII SaveFPFeaturesState(Actions); |
3408 | 603k | FPOptionsOverride NewOverrides; |
3409 | 603k | Actions.CurFPFeatures = NewOverrides.applyOverrides(getLangOpts()); |
3410 | 603k | Actions.FpPragmaStack.Act(Tok.getLocation(), Sema::PSK_Reset, StringRef(), |
3411 | 603k | {} /*unused*/); |
3412 | | |
3413 | 603k | SourceLocation SavedPrevTokLocation = PrevTokLocation; |
3414 | 603k | ParseLexedPragmas(getCurrentClass()); |
3415 | 603k | ParseLexedAttributes(getCurrentClass()); |
3416 | 603k | ParseLexedMethodDeclarations(getCurrentClass()); |
3417 | | |
3418 | | // We've finished with all pending member declarations. |
3419 | 603k | Actions.ActOnFinishCXXMemberDecls(); |
3420 | | |
3421 | 603k | ParseLexedMemberInitializers(getCurrentClass()); |
3422 | 603k | ParseLexedMethodDefs(getCurrentClass()); |
3423 | 603k | PrevTokLocation = SavedPrevTokLocation; |
3424 | | |
3425 | | // We've finished parsing everything, including default argument |
3426 | | // initializers. |
3427 | 603k | Actions.ActOnFinishCXXNonNestedClass(); |
3428 | 603k | } |
3429 | | |
3430 | 642k | if (TagDecl) |
3431 | 642k | Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, T.getRange()); |
3432 | | |
3433 | | // Leave the class scope. |
3434 | 642k | ParsingDef.Pop(); |
3435 | 642k | ClassScope.Exit(); |
3436 | 642k | } |
3437 | | |
3438 | 2 | void Parser::DiagnoseUnexpectedNamespace(NamedDecl *D) { |
3439 | 2 | assert(Tok.is(tok::kw_namespace)); |
3440 | | |
3441 | | // FIXME: Suggest where the close brace should have gone by looking |
3442 | | // at indentation changes within the definition body. |
3443 | 2 | Diag(D->getLocation(), |
3444 | 2 | diag::err_missing_end_of_definition) << D; |
3445 | 2 | Diag(Tok.getLocation(), |
3446 | 2 | diag::note_missing_end_of_definition_before) << D; |
3447 | | |
3448 | | // Push '};' onto the token stream to recover. |
3449 | 2 | PP.EnterToken(Tok, /*IsReinject*/ true); |
3450 | | |
3451 | 2 | Tok.startToken(); |
3452 | 2 | Tok.setLocation(PP.getLocForEndOfToken(PrevTokLocation)); |
3453 | 2 | Tok.setKind(tok::semi); |
3454 | 2 | PP.EnterToken(Tok, /*IsReinject*/ true); |
3455 | | |
3456 | 2 | Tok.setKind(tok::r_brace); |
3457 | 2 | } |
3458 | | |
3459 | | /// ParseConstructorInitializer - Parse a C++ constructor initializer, |
3460 | | /// which explicitly initializes the members or base classes of a |
3461 | | /// class (C++ [class.base.init]). For example, the three initializers |
3462 | | /// after the ':' in the Derived constructor below: |
3463 | | /// |
3464 | | /// @code |
3465 | | /// class Base { }; |
3466 | | /// class Derived : Base { |
3467 | | /// int x; |
3468 | | /// float f; |
3469 | | /// public: |
3470 | | /// Derived(float f) : Base(), x(17), f(f) { } |
3471 | | /// }; |
3472 | | /// @endcode |
3473 | | /// |
3474 | | /// [C++] ctor-initializer: |
3475 | | /// ':' mem-initializer-list |
3476 | | /// |
3477 | | /// [C++] mem-initializer-list: |
3478 | | /// mem-initializer ...[opt] |
3479 | | /// mem-initializer ...[opt] , mem-initializer-list |
3480 | 144k | void Parser::ParseConstructorInitializer(Decl *ConstructorDecl) { |
3481 | 144k | assert(Tok.is(tok::colon) && |
3482 | 144k | "Constructor initializer always starts with ':'"); |
3483 | | |
3484 | | // Poison the SEH identifiers so they are flagged as illegal in constructor |
3485 | | // initializers. |
3486 | 144k | PoisonSEHIdentifiersRAIIObject PoisonSEHIdentifiers(*this, true); |
3487 | 144k | SourceLocation ColonLoc = ConsumeToken(); |
3488 | | |
3489 | 144k | SmallVector<CXXCtorInitializer*, 4> MemInitializers; |
3490 | 144k | bool AnyErrors = false; |
3491 | | |
3492 | 200k | do { |
3493 | 200k | if (Tok.is(tok::code_completion)) { |
3494 | 28 | Actions.CodeCompleteConstructorInitializer(ConstructorDecl, |
3495 | 28 | MemInitializers); |
3496 | 28 | return cutOffParsing(); |
3497 | 28 | } |
3498 | | |
3499 | 200k | MemInitResult MemInit = ParseMemInitializer(ConstructorDecl); |
3500 | 200k | if (!MemInit.isInvalid()) |
3501 | 200k | MemInitializers.push_back(MemInit.get()); |
3502 | 131 | else |
3503 | 131 | AnyErrors = true; |
3504 | | |
3505 | 200k | if (Tok.is(tok::comma)) |
3506 | 55.6k | ConsumeToken(); |
3507 | 144k | else if (Tok.is(tok::l_brace)) |
3508 | 144k | break; |
3509 | | // If the previous initializer was valid and the next token looks like a |
3510 | | // base or member initializer, assume that we're just missing a comma. |
3511 | 23 | else if (!MemInit.isInvalid() && |
3512 | 9 | Tok.isOneOf(tok::identifier, tok::coloncolon)) { |
3513 | 8 | SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation); |
3514 | 8 | Diag(Loc, diag::err_ctor_init_missing_comma) |
3515 | 8 | << FixItHint::CreateInsertion(Loc, ", "); |
3516 | 15 | } else { |
3517 | | // Skip over garbage, until we get to '{'. Don't eat the '{'. |
3518 | 15 | if (!MemInit.isInvalid()) |
3519 | 1 | Diag(Tok.getLocation(), diag::err_expected_either) << tok::l_brace |
3520 | 1 | << tok::comma; |
3521 | 15 | SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch); |
3522 | 15 | break; |
3523 | 15 | } |
3524 | 55.6k | } while (true); |
3525 | | |
3526 | 144k | Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc, MemInitializers, |
3527 | 144k | AnyErrors); |
3528 | 144k | } |
3529 | | |
3530 | | /// ParseMemInitializer - Parse a C++ member initializer, which is |
3531 | | /// part of a constructor initializer that explicitly initializes one |
3532 | | /// member or base class (C++ [class.base.init]). See |
3533 | | /// ParseConstructorInitializer for an example. |
3534 | | /// |
3535 | | /// [C++] mem-initializer: |
3536 | | /// mem-initializer-id '(' expression-list[opt] ')' |
3537 | | /// [C++0x] mem-initializer-id braced-init-list |
3538 | | /// |
3539 | | /// [C++] mem-initializer-id: |
3540 | | /// '::'[opt] nested-name-specifier[opt] class-name |
3541 | | /// identifier |
3542 | 200k | MemInitResult Parser::ParseMemInitializer(Decl *ConstructorDecl) { |
3543 | | // parse '::'[opt] nested-name-specifier[opt] |
3544 | 200k | CXXScopeSpec SS; |
3545 | 200k | if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/nullptr, |
3546 | 200k | /*ObjectHadErrors=*/false, |
3547 | 200k | /*EnteringContext=*/false)) |
3548 | 2 | return true; |
3549 | | |
3550 | | // : identifier |
3551 | 200k | IdentifierInfo *II = nullptr; |
3552 | 200k | SourceLocation IdLoc = Tok.getLocation(); |
3553 | | // : declype(...) |
3554 | 200k | DeclSpec DS(AttrFactory); |
3555 | | // : template_name<...> |
3556 | 200k | TypeResult TemplateTypeTy; |
3557 | | |
3558 | 200k | if (Tok.is(tok::identifier)) { |
3559 | | // Get the identifier. This may be a member name or a class name, |
3560 | | // but we'll let the semantic analysis determine which it is. |
3561 | 195k | II = Tok.getIdentifierInfo(); |
3562 | 195k | ConsumeToken(); |
3563 | 4.83k | } else if (Tok.is(tok::annot_decltype)) { |
3564 | | // Get the decltype expression, if there is one. |
3565 | | // Uses of decltype will already have been converted to annot_decltype by |
3566 | | // ParseOptionalCXXScopeSpecifier at this point. |
3567 | | // FIXME: Can we get here with a scope specifier? |
3568 | 6 | ParseDecltypeSpecifier(DS); |
3569 | 4.82k | } else { |
3570 | 4.82k | TemplateIdAnnotation *TemplateId = Tok.is(tok::annot_template_id) |
3571 | 4.80k | ? takeTemplateIdAnnotation(Tok) |
3572 | 26 | : nullptr; |
3573 | 4.82k | if (TemplateId && TemplateId->mightBeType()4.80k ) { |
3574 | 4.80k | AnnotateTemplateIdTokenAsType(SS, /*IsClassName*/true); |
3575 | 4.80k | assert(Tok.is(tok::annot_typename) && "template-id -> type failed"); |
3576 | 4.80k | TemplateTypeTy = getTypeAnnotation(Tok); |
3577 | 4.80k | ConsumeAnnotationToken(); |
3578 | 27 | } else { |
3579 | 27 | Diag(Tok, diag::err_expected_member_or_base_name); |
3580 | 27 | return true; |
3581 | 27 | } |
3582 | 200k | } |
3583 | | |
3584 | | // Parse the '('. |
3585 | 200k | if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)198k ) { |
3586 | 355 | Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists); |
3587 | | |
3588 | | // FIXME: Add support for signature help inside initializer lists. |
3589 | 355 | ExprResult InitList = ParseBraceInitializer(); |
3590 | 355 | if (InitList.isInvalid()) |
3591 | 0 | return true; |
3592 | | |
3593 | 355 | SourceLocation EllipsisLoc; |
3594 | 355 | TryConsumeToken(tok::ellipsis, EllipsisLoc); |
3595 | | |
3596 | 355 | if (TemplateTypeTy.isInvalid()) |
3597 | 0 | return true; |
3598 | 355 | return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II, |
3599 | 355 | TemplateTypeTy.get(), DS, IdLoc, |
3600 | 355 | InitList.get(), EllipsisLoc); |
3601 | 199k | } else if(Tok.is(tok::l_paren)) { |
3602 | 199k | BalancedDelimiterTracker T(*this, tok::l_paren); |
3603 | 199k | T.consumeOpen(); |
3604 | | |
3605 | | // Parse the optional expression-list. |
3606 | 199k | ExprVector ArgExprs; |
3607 | 199k | CommaLocsTy CommaLocs; |
3608 | 2 | auto RunSignatureHelp = [&] { |
3609 | 2 | if (TemplateTypeTy.isInvalid()) |
3610 | 0 | return QualType(); |
3611 | 2 | QualType PreferredType = Actions.ProduceCtorInitMemberSignatureHelp( |
3612 | 2 | getCurScope(), ConstructorDecl, SS, TemplateTypeTy.get(), ArgExprs, II, |
3613 | 2 | T.getOpenLocation()); |
3614 | 2 | CalledSignatureHelp = true; |
3615 | 2 | return PreferredType; |
3616 | 2 | }; |
3617 | 199k | if (Tok.isNot(tok::r_paren) && |
3618 | 248k | ParseExpressionList(ArgExprs, CommaLocs, [&] 190k { |
3619 | 248k | PreferredType.enterFunctionArgument(Tok.getLocation(), |
3620 | 248k | RunSignatureHelp); |
3621 | 13 | })) { |
3622 | 13 | if (PP.isCodeCompletionReached() && !CalledSignatureHelp6 ) |
3623 | 0 | RunSignatureHelp(); |
3624 | 13 | SkipUntil(tok::r_paren, StopAtSemi); |
3625 | 13 | return true; |
3626 | 13 | } |
3627 | | |
3628 | 199k | T.consumeClose(); |
3629 | | |
3630 | 199k | SourceLocation EllipsisLoc; |
3631 | 199k | TryConsumeToken(tok::ellipsis, EllipsisLoc); |
3632 | | |
3633 | 199k | if (TemplateTypeTy.isInvalid()) |
3634 | 1 | return true; |
3635 | 199k | return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II, |
3636 | 199k | TemplateTypeTy.get(), DS, IdLoc, |
3637 | 199k | T.getOpenLocation(), ArgExprs, |
3638 | 199k | T.getCloseLocation(), EllipsisLoc); |
3639 | 199k | } |
3640 | | |
3641 | 0 | if (TemplateTypeTy.isInvalid()) |
3642 | 0 | return true; |
3643 | | |
3644 | 0 | if (getLangOpts().CPlusPlus11) |
3645 | 0 | return Diag(Tok, diag::err_expected_either) << tok::l_paren << tok::l_brace; |
3646 | 0 | else |
3647 | 0 | return Diag(Tok, diag::err_expected) << tok::l_paren; |
3648 | 0 | } |
3649 | | |
3650 | | /// Parse a C++ exception-specification if present (C++0x [except.spec]). |
3651 | | /// |
3652 | | /// exception-specification: |
3653 | | /// dynamic-exception-specification |
3654 | | /// noexcept-specification |
3655 | | /// |
3656 | | /// noexcept-specification: |
3657 | | /// 'noexcept' |
3658 | | /// 'noexcept' '(' constant-expression ')' |
3659 | | ExceptionSpecificationType |
3660 | | Parser::tryParseExceptionSpecification(bool Delayed, |
3661 | | SourceRange &SpecificationRange, |
3662 | | SmallVectorImpl<ParsedType> &DynamicExceptions, |
3663 | | SmallVectorImpl<SourceRange> &DynamicExceptionRanges, |
3664 | | ExprResult &NoexceptExpr, |
3665 | 2.88M | CachedTokens *&ExceptionSpecTokens) { |
3666 | 2.88M | ExceptionSpecificationType Result = EST_None; |
3667 | 2.88M | ExceptionSpecTokens = nullptr; |
3668 | | |
3669 | | // Handle delayed parsing of exception-specifications. |
3670 | 2.88M | if (Delayed) { |
3671 | 963k | if (Tok.isNot(tok::kw_throw) && Tok.isNot(tok::kw_noexcept)961k ) |
3672 | 587k | return EST_None; |
3673 | | |
3674 | | // Consume and cache the starting token. |
3675 | 375k | bool IsNoexcept = Tok.is(tok::kw_noexcept); |
3676 | 375k | Token StartTok = Tok; |
3677 | 375k | SpecificationRange = SourceRange(ConsumeToken()); |
3678 | | |
3679 | | // Check for a '('. |
3680 | 375k | if (!Tok.is(tok::l_paren)) { |
3681 | | // If this is a bare 'noexcept', we're done. |
3682 | 345k | if (IsNoexcept) { |
3683 | 345k | Diag(Tok, diag::warn_cxx98_compat_noexcept_decl); |
3684 | 345k | NoexceptExpr = nullptr; |
3685 | 345k | return EST_BasicNoexcept; |
3686 | 345k | } |
3687 | | |
3688 | 0 | Diag(Tok, diag::err_expected_lparen_after) << "throw"; |
3689 | 0 | return EST_DynamicNone; |
3690 | 0 | } |
3691 | | |
3692 | | // Cache the tokens for the exception-specification. |
3693 | 29.5k | ExceptionSpecTokens = new CachedTokens; |
3694 | 29.5k | ExceptionSpecTokens->push_back(StartTok); // 'throw' or 'noexcept' |
3695 | 29.5k | ExceptionSpecTokens->push_back(Tok); // '(' |
3696 | 29.5k | SpecificationRange.setEnd(ConsumeParen()); // '(' |
3697 | | |
3698 | 29.5k | ConsumeAndStoreUntil(tok::r_paren, *ExceptionSpecTokens, |
3699 | 29.5k | /*StopAtSemi=*/true, |
3700 | 29.5k | /*ConsumeFinalToken=*/true); |
3701 | 29.5k | SpecificationRange.setEnd(ExceptionSpecTokens->back().getLocation()); |
3702 | | |
3703 | 29.5k | return EST_Unparsed; |
3704 | 29.5k | } |
3705 | | |
3706 | | // See if there's a dynamic specification. |
3707 | 1.92M | if (Tok.is(tok::kw_throw)) { |
3708 | 4.20k | Result = ParseDynamicExceptionSpecification(SpecificationRange, |
3709 | 4.20k | DynamicExceptions, |
3710 | 4.20k | DynamicExceptionRanges); |
3711 | 4.20k | assert(DynamicExceptions.size() == DynamicExceptionRanges.size() && |
3712 | 4.20k | "Produced different number of exception types and ranges."); |
3713 | 4.20k | } |
3714 | | |
3715 | | // If there's no noexcept specification, we're done. |
3716 | 1.92M | if (Tok.isNot(tok::kw_noexcept)) |
3717 | 1.68M | return Result; |
3718 | | |
3719 | 242k | Diag(Tok, diag::warn_cxx98_compat_noexcept_decl); |
3720 | | |
3721 | | // If we already had a dynamic specification, parse the noexcept for, |
3722 | | // recovery, but emit a diagnostic and don't store the results. |
3723 | 242k | SourceRange NoexceptRange; |
3724 | 242k | ExceptionSpecificationType NoexceptType = EST_None; |
3725 | | |
3726 | 242k | SourceLocation KeywordLoc = ConsumeToken(); |
3727 | 242k | if (Tok.is(tok::l_paren)) { |
3728 | | // There is an argument. |
3729 | 45.8k | BalancedDelimiterTracker T(*this, tok::l_paren); |
3730 | 45.8k | T.consumeOpen(); |
3731 | 45.8k | NoexceptExpr = ParseConstantExpression(); |
3732 | 45.8k | T.consumeClose(); |
3733 | 45.8k | if (!NoexceptExpr.isInvalid()) { |
3734 | 45.8k | NoexceptExpr = Actions.ActOnNoexceptSpec(KeywordLoc, NoexceptExpr.get(), |
3735 | 45.8k | NoexceptType); |
3736 | 45.8k | NoexceptRange = SourceRange(KeywordLoc, T.getCloseLocation()); |
3737 | 2 | } else { |
3738 | 2 | NoexceptType = EST_BasicNoexcept; |
3739 | 2 | } |
3740 | 196k | } else { |
3741 | | // There is no argument. |
3742 | 196k | NoexceptType = EST_BasicNoexcept; |
3743 | 196k | NoexceptRange = SourceRange(KeywordLoc, KeywordLoc); |
3744 | 196k | } |
3745 | | |
3746 | 242k | if (Result == EST_None) { |
3747 | 242k | SpecificationRange = NoexceptRange; |
3748 | 242k | Result = NoexceptType; |
3749 | | |
3750 | | // If there's a dynamic specification after a noexcept specification, |
3751 | | // parse that and ignore the results. |
3752 | 242k | if (Tok.is(tok::kw_throw)) { |
3753 | 1 | Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification); |
3754 | 1 | ParseDynamicExceptionSpecification(NoexceptRange, DynamicExceptions, |
3755 | 1 | DynamicExceptionRanges); |
3756 | 1 | } |
3757 | 0 | } else { |
3758 | 0 | Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification); |
3759 | 0 | } |
3760 | | |
3761 | 242k | return Result; |
3762 | 242k | } |
3763 | | |
3764 | | static void diagnoseDynamicExceptionSpecification( |
3765 | 4.20k | Parser &P, SourceRange Range, bool IsNoexcept) { |
3766 | 4.20k | if (P.getLangOpts().CPlusPlus11) { |
3767 | 3.43k | const char *Replacement = IsNoexcept ? "noexcept" : "noexcept(false)"545 ; |
3768 | 3.98k | P.Diag(Range.getBegin(), |
3769 | 3.98k | P.getLangOpts().CPlusPlus17 && !IsNoexcept380 |
3770 | 45 | ? diag::ext_dynamic_exception_spec |
3771 | 3.93k | : diag::warn_exception_spec_deprecated) |
3772 | 3.98k | << Range; |
3773 | 3.98k | P.Diag(Range.getBegin(), diag::note_exception_spec_deprecated) |
3774 | 3.98k | << Replacement << FixItHint::CreateReplacement(Range, Replacement); |
3775 | 3.98k | } |
3776 | 4.20k | } |
3777 | | |
3778 | | /// ParseDynamicExceptionSpecification - Parse a C++ |
3779 | | /// dynamic-exception-specification (C++ [except.spec]). |
3780 | | /// |
3781 | | /// dynamic-exception-specification: |
3782 | | /// 'throw' '(' type-id-list [opt] ')' |
3783 | | /// [MS] 'throw' '(' '...' ')' |
3784 | | /// |
3785 | | /// type-id-list: |
3786 | | /// type-id ... [opt] |
3787 | | /// type-id-list ',' type-id ... [opt] |
3788 | | /// |
3789 | | ExceptionSpecificationType Parser::ParseDynamicExceptionSpecification( |
3790 | | SourceRange &SpecificationRange, |
3791 | | SmallVectorImpl<ParsedType> &Exceptions, |
3792 | 4.20k | SmallVectorImpl<SourceRange> &Ranges) { |
3793 | 4.20k | assert(Tok.is(tok::kw_throw) && "expected throw"); |
3794 | | |
3795 | 4.20k | SpecificationRange.setBegin(ConsumeToken()); |
3796 | 4.20k | BalancedDelimiterTracker T(*this, tok::l_paren); |
3797 | 4.20k | if (T.consumeOpen()) { |
3798 | 0 | Diag(Tok, diag::err_expected_lparen_after) << "throw"; |
3799 | 0 | SpecificationRange.setEnd(SpecificationRange.getBegin()); |
3800 | 0 | return EST_DynamicNone; |
3801 | 0 | } |
3802 | | |
3803 | | // Parse throw(...), a Microsoft extension that means "this function |
3804 | | // can throw anything". |
3805 | 4.20k | if (Tok.is(tok::ellipsis)) { |
3806 | 66 | SourceLocation EllipsisLoc = ConsumeToken(); |
3807 | 66 | if (!getLangOpts().MicrosoftExt) |
3808 | 44 | Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec); |
3809 | 66 | T.consumeClose(); |
3810 | 66 | SpecificationRange.setEnd(T.getCloseLocation()); |
3811 | 66 | diagnoseDynamicExceptionSpecification(*this, SpecificationRange, false); |
3812 | 66 | return EST_MSAny; |
3813 | 66 | } |
3814 | | |
3815 | | // Parse the sequence of type-ids. |
3816 | 4.13k | SourceRange Range; |
3817 | 4.21k | while (Tok.isNot(tok::r_paren)) { |
3818 | 680 | TypeResult Res(ParseTypeName(&Range)); |
3819 | | |
3820 | 680 | if (Tok.is(tok::ellipsis)) { |
3821 | | // C++0x [temp.variadic]p5: |
3822 | | // - In a dynamic-exception-specification (15.4); the pattern is a |
3823 | | // type-id. |
3824 | 13 | SourceLocation Ellipsis = ConsumeToken(); |
3825 | 13 | Range.setEnd(Ellipsis); |
3826 | 13 | if (!Res.isInvalid()) |
3827 | 13 | Res = Actions.ActOnPackExpansion(Res.get(), Ellipsis); |
3828 | 13 | } |
3829 | | |
3830 | 680 | if (!Res.isInvalid()) { |
3831 | 672 | Exceptions.push_back(Res.get()); |
3832 | 672 | Ranges.push_back(Range); |
3833 | 672 | } |
3834 | | |
3835 | 680 | if (!TryConsumeToken(tok::comma)) |
3836 | 596 | break; |
3837 | 680 | } |
3838 | | |
3839 | 4.13k | T.consumeClose(); |
3840 | 4.13k | SpecificationRange.setEnd(T.getCloseLocation()); |
3841 | 4.13k | diagnoseDynamicExceptionSpecification(*this, SpecificationRange, |
3842 | 4.13k | Exceptions.empty()); |
3843 | 3.54k | return Exceptions.empty() ? EST_DynamicNone : EST_Dynamic588 ; |
3844 | 4.13k | } |
3845 | | |
3846 | | /// ParseTrailingReturnType - Parse a trailing return type on a new-style |
3847 | | /// function declaration. |
3848 | | TypeResult Parser::ParseTrailingReturnType(SourceRange &Range, |
3849 | 16.1k | bool MayBeFollowedByDirectInit) { |
3850 | 16.1k | assert(Tok.is(tok::arrow) && "expected arrow"); |
3851 | | |
3852 | 16.1k | ConsumeToken(); |
3853 | | |
3854 | 16.1k | return ParseTypeName(&Range, MayBeFollowedByDirectInit |
3855 | 9.33k | ? DeclaratorContext::TrailingReturnVar |
3856 | 6.81k | : DeclaratorContext::TrailingReturn); |
3857 | 16.1k | } |
3858 | | |
3859 | | /// Parse a requires-clause as part of a function declaration. |
3860 | 127 | void Parser::ParseTrailingRequiresClause(Declarator &D) { |
3861 | 127 | assert(Tok.is(tok::kw_requires) && "expected requires"); |
3862 | | |
3863 | 127 | SourceLocation RequiresKWLoc = ConsumeToken(); |
3864 | | |
3865 | 127 | ExprResult TrailingRequiresClause; |
3866 | 127 | ParseScope ParamScope(this, |
3867 | 127 | Scope::DeclScope | |
3868 | 127 | Scope::FunctionDeclarationScope | |
3869 | 127 | Scope::FunctionPrototypeScope); |
3870 | | |
3871 | 127 | Actions.ActOnStartTrailingRequiresClause(getCurScope(), D); |
3872 | | |
3873 | 127 | llvm::Optional<Sema::CXXThisScopeRAII> ThisScope; |
3874 | 127 | InitCXXThisScopeForDeclaratorIfRelevant(D, D.getDeclSpec(), ThisScope); |
3875 | | |
3876 | 127 | TrailingRequiresClause = |
3877 | 127 | ParseConstraintLogicalOrExpression(/*IsTrailingRequiresClause=*/true); |
3878 | | |
3879 | 127 | TrailingRequiresClause = |
3880 | 127 | Actions.ActOnFinishTrailingRequiresClause(TrailingRequiresClause); |
3881 | | |
3882 | 127 | if (!D.isDeclarationOfFunction()) { |
3883 | 1 | Diag(RequiresKWLoc, |
3884 | 1 | diag::err_requires_clause_on_declarator_not_declaring_a_function); |
3885 | 1 | return; |
3886 | 1 | } |
3887 | | |
3888 | 126 | if (TrailingRequiresClause.isInvalid()) |
3889 | 7 | SkipUntil({tok::l_brace, tok::arrow, tok::kw_try, tok::comma, tok::colon}, |
3890 | 7 | StopAtSemi | StopBeforeMatch); |
3891 | 119 | else |
3892 | 119 | D.setTrailingRequiresClause(TrailingRequiresClause.get()); |
3893 | | |
3894 | | // Did the user swap the trailing return type and requires clause? |
3895 | 126 | if (D.isFunctionDeclarator() && Tok.is(tok::arrow)123 && |
3896 | 1 | D.getDeclSpec().getTypeSpecType() == TST_auto) { |
3897 | 1 | SourceLocation ArrowLoc = Tok.getLocation(); |
3898 | 1 | SourceRange Range; |
3899 | 1 | TypeResult TrailingReturnType = |
3900 | 1 | ParseTrailingReturnType(Range, /*MayBeFollowedByDirectInit=*/false); |
3901 | | |
3902 | 1 | if (!TrailingReturnType.isInvalid()) { |
3903 | 1 | Diag(ArrowLoc, |
3904 | 1 | diag::err_requires_clause_must_appear_after_trailing_return) |
3905 | 1 | << Range; |
3906 | 1 | auto &FunctionChunk = D.getFunctionTypeInfo(); |
3907 | 1 | FunctionChunk.HasTrailingReturnType = TrailingReturnType.isUsable(); |
3908 | 1 | FunctionChunk.TrailingReturnType = TrailingReturnType.get(); |
3909 | 1 | FunctionChunk.TrailingReturnTypeLoc = Range.getBegin(); |
3910 | 1 | } else |
3911 | 0 | SkipUntil({tok::equal, tok::l_brace, tok::arrow, tok::kw_try, tok::comma}, |
3912 | 0 | StopAtSemi | StopBeforeMatch); |
3913 | 1 | } |
3914 | 126 | } |
3915 | | |
3916 | | /// We have just started parsing the definition of a new class, |
3917 | | /// so push that class onto our stack of classes that is currently |
3918 | | /// being parsed. |
3919 | | Sema::ParsingClassState |
3920 | | Parser::PushParsingClass(Decl *ClassDecl, bool NonNestedClass, |
3921 | 642k | bool IsInterface) { |
3922 | 642k | assert((NonNestedClass || !ClassStack.empty()) && |
3923 | 642k | "Nested class without outer class"); |
3924 | 642k | ClassStack.push(new ParsingClass(ClassDecl, NonNestedClass, IsInterface)); |
3925 | 642k | return Actions.PushParsingClass(); |
3926 | 642k | } |
3927 | | |
3928 | | /// Deallocate the given parsed class and all of its nested |
3929 | | /// classes. |
3930 | 642k | void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) { |
3931 | 1.35M | for (unsigned I = 0, N = Class->LateParsedDeclarations.size(); I != N; ++I712k ) |
3932 | 712k | delete Class->LateParsedDeclarations[I]; |
3933 | 642k | delete Class; |
3934 | 642k | } |
3935 | | |
3936 | | /// Pop the top class of the stack of classes that are |
3937 | | /// currently being parsed. |
3938 | | /// |
3939 | | /// This routine should be called when we have finished parsing the |
3940 | | /// definition of a class, but have not yet popped the Scope |
3941 | | /// associated with the class's definition. |
3942 | 642k | void Parser::PopParsingClass(Sema::ParsingClassState state) { |
3943 | 642k | assert(!ClassStack.empty() && "Mismatched push/pop for class parsing"); |
3944 | | |
3945 | 642k | Actions.PopParsingClass(state); |
3946 | | |
3947 | 642k | ParsingClass *Victim = ClassStack.top(); |
3948 | 642k | ClassStack.pop(); |
3949 | 642k | if (Victim->TopLevelClass) { |
3950 | | // Deallocate all of the nested classes of this class, |
3951 | | // recursively: we don't need to keep any of this information. |
3952 | 603k | DeallocateParsedClasses(Victim); |
3953 | 603k | return; |
3954 | 603k | } |
3955 | 39.4k | assert(!ClassStack.empty() && "Missing top-level class?"); |
3956 | | |
3957 | 39.4k | if (Victim->LateParsedDeclarations.empty()) { |
3958 | | // The victim is a nested class, but we will not need to perform |
3959 | | // any processing after the definition of this class since it has |
3960 | | // no members whose handling was delayed. Therefore, we can just |
3961 | | // remove this nested class. |
3962 | 32.3k | DeallocateParsedClasses(Victim); |
3963 | 32.3k | return; |
3964 | 32.3k | } |
3965 | | |
3966 | | // This nested class has some members that will need to be processed |
3967 | | // after the top-level class is completely defined. Therefore, add |
3968 | | // it to the list of nested classes within its parent. |
3969 | 7.07k | assert(getCurScope()->isClassScope() && "Nested class outside of class scope?"); |
3970 | 7.07k | ClassStack.top()->LateParsedDeclarations.push_back( |
3971 | 7.07k | new LateParsedClass(this, Victim)); |
3972 | 7.07k | } |
3973 | | |
3974 | | /// Try to parse an 'identifier' which appears within an attribute-token. |
3975 | | /// |
3976 | | /// \return the parsed identifier on success, and 0 if the next token is not an |
3977 | | /// attribute-token. |
3978 | | /// |
3979 | | /// C++11 [dcl.attr.grammar]p3: |
3980 | | /// If a keyword or an alternative token that satisfies the syntactic |
3981 | | /// requirements of an identifier is contained in an attribute-token, |
3982 | | /// it is considered an identifier. |
3983 | 14.6k | IdentifierInfo *Parser::TryParseCXX11AttributeIdentifier(SourceLocation &Loc) { |
3984 | 14.6k | switch (Tok.getKind()) { |
3985 | 14.6k | default: |
3986 | | // Identifiers and keywords have identifier info attached. |
3987 | 14.6k | if (!Tok.isAnnotation()) { |
3988 | 14.6k | if (IdentifierInfo *II = Tok.getIdentifierInfo()) { |
3989 | 14.6k | Loc = ConsumeToken(); |
3990 | 14.6k | return II; |
3991 | 14.6k | } |
3992 | 16 | } |
3993 | 16 | return nullptr; |
3994 | | |
3995 | 7 | case tok::numeric_constant: { |
3996 | | // If we got a numeric constant, check to see if it comes from a macro that |
3997 | | // corresponds to the predefined __clang__ macro. If it does, warn the user |
3998 | | // and recover by pretending they said _Clang instead. |
3999 | 7 | if (Tok.getLocation().isMacroID()) { |
4000 | 5 | SmallString<8> ExpansionBuf; |
4001 | 5 | SourceLocation ExpansionLoc = |
4002 | 5 | PP.getSourceManager().getExpansionLoc(Tok.getLocation()); |
4003 | 5 | StringRef Spelling = PP.getSpelling(ExpansionLoc, ExpansionBuf); |
4004 | 5 | if (Spelling == "__clang__") { |
4005 | 5 | SourceRange TokRange( |
4006 | 5 | ExpansionLoc, |
4007 | 5 | PP.getSourceManager().getExpansionLoc(Tok.getEndLoc())); |
4008 | 5 | Diag(Tok, diag::warn_wrong_clang_attr_namespace) |
4009 | 5 | << FixItHint::CreateReplacement(TokRange, "_Clang"); |
4010 | 5 | Loc = ConsumeToken(); |
4011 | 5 | return &PP.getIdentifierTable().get("_Clang"); |
4012 | 5 | } |
4013 | 2 | } |
4014 | 2 | return nullptr; |
4015 | 2 | } |
4016 | | |
4017 | 0 | case tok::ampamp: // 'and' |
4018 | 2 | case tok::pipe: // 'bitor' |
4019 | 2 | case tok::pipepipe: // 'or' |
4020 | 3 | case tok::caret: // 'xor' |
4021 | 5 | case tok::tilde: // 'compl' |
4022 | 8 | case tok::amp: // 'bitand' |
4023 | 8 | case tok::ampequal: // 'and_eq' |
4024 | 8 | case tok::pipeequal: // 'or_eq' |
4025 | 8 | case tok::caretequal: // 'xor_eq' |
4026 | 8 | case tok::exclaim: // 'not' |
4027 | 8 | case tok::exclaimequal: // 'not_eq' |
4028 | | // Alternative tokens do not have identifier info, but their spelling |
4029 | | // starts with an alphabetical character. |
4030 | 8 | SmallString<8> SpellingBuf; |
4031 | 8 | SourceLocation SpellingLoc = |
4032 | 8 | PP.getSourceManager().getSpellingLoc(Tok.getLocation()); |
4033 | 8 | StringRef Spelling = PP.getSpelling(SpellingLoc, SpellingBuf); |
4034 | 8 | if (isLetter(Spelling[0])) { |
4035 | 7 | Loc = ConsumeToken(); |
4036 | 7 | return &PP.getIdentifierTable().get(Spelling); |
4037 | 7 | } |
4038 | 1 | return nullptr; |
4039 | 14.6k | } |
4040 | 14.6k | } |
4041 | | |
4042 | | static bool IsBuiltInOrStandardCXX11Attribute(IdentifierInfo *AttrName, |
4043 | 12.9k | IdentifierInfo *ScopeName) { |
4044 | 12.9k | switch ( |
4045 | 12.9k | ParsedAttr::getParsedKind(AttrName, ScopeName, ParsedAttr::AS_CXX11)) { |
4046 | 52 | case ParsedAttr::AT_CarriesDependency: |
4047 | 136 | case ParsedAttr::AT_Deprecated: |
4048 | 1.13k | case ParsedAttr::AT_FallThrough: |
4049 | 11.4k | case ParsedAttr::AT_CXX11NoReturn: |
4050 | 11.5k | case ParsedAttr::AT_NoUniqueAddress: |
4051 | 11.6k | case ParsedAttr::AT_Likely: |
4052 | 11.7k | case ParsedAttr::AT_Unlikely: |
4053 | 11.7k | return true; |
4054 | 131 | case ParsedAttr::AT_WarnUnusedResult: |
4055 | 131 | return !ScopeName && AttrName->getName().equals("nodiscard")117 ; |
4056 | 53 | case ParsedAttr::AT_Unused: |
4057 | 53 | return !ScopeName && AttrName->getName().equals("maybe_unused")44 ; |
4058 | 1.07k | default: |
4059 | 1.07k | return false; |
4060 | 12.9k | } |
4061 | 12.9k | } |
4062 | | |
4063 | | /// ParseCXX11AttributeArgs -- Parse a C++11 attribute-argument-clause. |
4064 | | /// |
4065 | | /// [C++11] attribute-argument-clause: |
4066 | | /// '(' balanced-token-seq ')' |
4067 | | /// |
4068 | | /// [C++11] balanced-token-seq: |
4069 | | /// balanced-token |
4070 | | /// balanced-token-seq balanced-token |
4071 | | /// |
4072 | | /// [C++11] balanced-token: |
4073 | | /// '(' balanced-token-seq ')' |
4074 | | /// '[' balanced-token-seq ']' |
4075 | | /// '{' balanced-token-seq '}' |
4076 | | /// any token but '(', ')', '[', ']', '{', or '}' |
4077 | | bool Parser::ParseCXX11AttributeArgs(IdentifierInfo *AttrName, |
4078 | | SourceLocation AttrNameLoc, |
4079 | | ParsedAttributes &Attrs, |
4080 | | SourceLocation *EndLoc, |
4081 | | IdentifierInfo *ScopeName, |
4082 | 307 | SourceLocation ScopeLoc) { |
4083 | 307 | assert(Tok.is(tok::l_paren) && "Not a C++11 attribute argument list"); |
4084 | 307 | SourceLocation LParenLoc = Tok.getLocation(); |
4085 | 307 | const LangOptions &LO = getLangOpts(); |
4086 | 307 | ParsedAttr::Syntax Syntax = |
4087 | 255 | LO.CPlusPlus ? ParsedAttr::AS_CXX11 : ParsedAttr::AS_C2x52 ; |
4088 | | |
4089 | | // If the attribute isn't known, we will not attempt to parse any |
4090 | | // arguments. |
4091 | 307 | if (!hasAttribute(LO.CPlusPlus ? AttrSyntax::CXX255 : AttrSyntax::C52 , ScopeName, |
4092 | 15 | AttrName, getTargetInfo(), getLangOpts())) { |
4093 | | // Eat the left paren, then skip to the ending right paren. |
4094 | 15 | ConsumeParen(); |
4095 | 15 | SkipUntil(tok::r_paren); |
4096 | 15 | return false; |
4097 | 15 | } |
4098 | | |
4099 | 292 | if (ScopeName && (239 ScopeName->isStr("gnu")239 || ScopeName->isStr("__gnu__")202 )) { |
4100 | | // GNU-scoped attributes have some special cases to handle GNU-specific |
4101 | | // behaviors. |
4102 | 37 | ParseGNUAttributeArgs(AttrName, AttrNameLoc, Attrs, EndLoc, ScopeName, |
4103 | 37 | ScopeLoc, Syntax, nullptr); |
4104 | 37 | return true; |
4105 | 37 | } |
4106 | | |
4107 | 255 | unsigned NumArgs; |
4108 | | // Some Clang-scoped attributes have some special parsing behavior. |
4109 | 255 | if (ScopeName && (202 ScopeName->isStr("clang")202 || ScopeName->isStr("_Clang")58 )) |
4110 | 150 | NumArgs = ParseClangAttributeArgs(AttrName, AttrNameLoc, Attrs, EndLoc, |
4111 | 150 | ScopeName, ScopeLoc, Syntax); |
4112 | 105 | else |
4113 | 105 | NumArgs = |
4114 | 105 | ParseAttributeArgsCommon(AttrName, AttrNameLoc, Attrs, EndLoc, |
4115 | 105 | ScopeName, ScopeLoc, Syntax); |
4116 | | |
4117 | 255 | if (!Attrs.empty() && |
4118 | 241 | IsBuiltInOrStandardCXX11Attribute(AttrName, ScopeName)) { |
4119 | 52 | ParsedAttr &Attr = Attrs.back(); |
4120 | | // If the attribute is a standard or built-in attribute and we are |
4121 | | // parsing an argument list, we need to determine whether this attribute |
4122 | | // was allowed to have an argument list (such as [[deprecated]]), and how |
4123 | | // many arguments were parsed (so we can diagnose on [[deprecated()]]). |
4124 | 52 | if (Attr.getMaxArgs() && !NumArgs43 ) { |
4125 | | // The attribute was allowed to have arguments, but none were provided |
4126 | | // even though the attribute parsed successfully. This is an error. |
4127 | 1 | Diag(LParenLoc, diag::err_attribute_requires_arguments) << AttrName; |
4128 | 1 | Attr.setInvalid(true); |
4129 | 51 | } else if (!Attr.getMaxArgs()) { |
4130 | | // The attribute parsed successfully, but was not allowed to have any |
4131 | | // arguments. It doesn't matter whether any were provided -- the |
4132 | | // presence of the argument list (even if empty) is diagnosed. |
4133 | 9 | Diag(LParenLoc, diag::err_cxx11_attribute_forbids_arguments) |
4134 | 9 | << AttrName |
4135 | 9 | << FixItHint::CreateRemoval(SourceRange(LParenLoc, *EndLoc)); |
4136 | 9 | Attr.setInvalid(true); |
4137 | 9 | } |
4138 | 52 | } |
4139 | 255 | return true; |
4140 | 255 | } |
4141 | | |
4142 | | /// ParseCXX11AttributeSpecifier - Parse a C++11 or C2x attribute-specifier. |
4143 | | /// |
4144 | | /// [C++11] attribute-specifier: |
4145 | | /// '[' '[' attribute-list ']' ']' |
4146 | | /// alignment-specifier |
4147 | | /// |
4148 | | /// [C++11] attribute-list: |
4149 | | /// attribute[opt] |
4150 | | /// attribute-list ',' attribute[opt] |
4151 | | /// attribute '...' |
4152 | | /// attribute-list ',' attribute '...' |
4153 | | /// |
4154 | | /// [C++11] attribute: |
4155 | | /// attribute-token attribute-argument-clause[opt] |
4156 | | /// |
4157 | | /// [C++11] attribute-token: |
4158 | | /// identifier |
4159 | | /// attribute-scoped-token |
4160 | | /// |
4161 | | /// [C++11] attribute-scoped-token: |
4162 | | /// attribute-namespace '::' identifier |
4163 | | /// |
4164 | | /// [C++11] attribute-namespace: |
4165 | | /// identifier |
4166 | | void Parser::ParseCXX11AttributeSpecifier(ParsedAttributes &attrs, |
4167 | 21.2k | SourceLocation *endLoc) { |
4168 | 21.2k | if (Tok.is(tok::kw_alignas)) { |
4169 | 8.04k | Diag(Tok.getLocation(), diag::warn_cxx98_compat_alignas); |
4170 | 8.04k | ParseAlignmentSpecifier(attrs, endLoc); |
4171 | 8.04k | return; |
4172 | 8.04k | } |
4173 | | |
4174 | 13.1k | assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square) && |
4175 | 13.1k | "Not a double square bracket attribute list"); |
4176 | | |
4177 | 13.1k | SourceLocation OpenLoc = Tok.getLocation(); |
4178 | 13.1k | Diag(OpenLoc, diag::warn_cxx98_compat_attribute); |
4179 | | |
4180 | 13.1k | ConsumeBracket(); |
4181 | 13.1k | checkCompoundToken(OpenLoc, tok::l_square, CompoundToken::AttrBegin); |
4182 | 13.1k | ConsumeBracket(); |
4183 | | |
4184 | 13.1k | SourceLocation CommonScopeLoc; |
4185 | 13.1k | IdentifierInfo *CommonScopeName = nullptr; |
4186 | 13.1k | if (Tok.is(tok::kw_using)) { |
4187 | 10 | Diag(Tok.getLocation(), getLangOpts().CPlusPlus17 |
4188 | 10 | ? diag::warn_cxx14_compat_using_attribute_ns |
4189 | 0 | : diag::ext_using_attribute_ns); |
4190 | 10 | ConsumeToken(); |
4191 | | |
4192 | 10 | CommonScopeName = TryParseCXX11AttributeIdentifier(CommonScopeLoc); |
4193 | 10 | if (!CommonScopeName) { |
4194 | 2 | Diag(Tok.getLocation(), diag::err_expected) << tok::identifier; |
4195 | 2 | SkipUntil(tok::r_square, tok::colon, StopBeforeMatch); |
4196 | 2 | } |
4197 | 10 | if (!TryConsumeToken(tok::colon) && CommonScopeName2 ) |
4198 | 1 | Diag(Tok.getLocation(), diag::err_expected) << tok::colon; |
4199 | 10 | } |
4200 | | |
4201 | 13.1k | llvm::SmallDenseMap<IdentifierInfo*, SourceLocation, 4> SeenAttrs; |
4202 | | |
4203 | 25.9k | while (Tok.isNot(tok::r_square)) { |
4204 | | // attribute not present |
4205 | 12.7k | if (TryConsumeToken(tok::comma)) |
4206 | 47 | continue; |
4207 | | |
4208 | 12.7k | SourceLocation ScopeLoc, AttrLoc; |
4209 | 12.7k | IdentifierInfo *ScopeName = nullptr, *AttrName = nullptr; |
4210 | | |
4211 | 12.7k | AttrName = TryParseCXX11AttributeIdentifier(AttrLoc); |
4212 | 12.7k | if (!AttrName) |
4213 | | // Break out to the "expected ']'" diagnostic. |
4214 | 6 | break; |
4215 | | |
4216 | | // scoped attribute |
4217 | 12.7k | if (TryConsumeToken(tok::coloncolon)) { |
4218 | 1.75k | ScopeName = AttrName; |
4219 | 1.75k | ScopeLoc = AttrLoc; |
4220 | | |
4221 | 1.75k | AttrName = TryParseCXX11AttributeIdentifier(AttrLoc); |
4222 | 1.75k | if (!AttrName) { |
4223 | 1 | Diag(Tok.getLocation(), diag::err_expected) << tok::identifier; |
4224 | 1 | SkipUntil(tok::r_square, tok::comma, StopAtSemi | StopBeforeMatch); |
4225 | 1 | continue; |
4226 | 1 | } |
4227 | 12.7k | } |
4228 | | |
4229 | 12.7k | if (CommonScopeName) { |
4230 | 8 | if (ScopeName) { |
4231 | 1 | Diag(ScopeLoc, diag::err_using_attribute_ns_conflict) |
4232 | 1 | << SourceRange(CommonScopeLoc); |
4233 | 7 | } else { |
4234 | 7 | ScopeName = CommonScopeName; |
4235 | 7 | ScopeLoc = CommonScopeLoc; |
4236 | 7 | } |
4237 | 8 | } |
4238 | | |
4239 | 12.7k | bool StandardAttr = IsBuiltInOrStandardCXX11Attribute(AttrName, ScopeName); |
4240 | 12.7k | bool AttrParsed = false; |
4241 | | |
4242 | 12.7k | if (StandardAttr && |
4243 | 11.8k | !SeenAttrs.insert(std::make_pair(AttrName, AttrLoc)).second) |
4244 | 10 | Diag(AttrLoc, diag::err_cxx11_attribute_repeated) |
4245 | 10 | << AttrName << SourceRange(SeenAttrs[AttrName]); |
4246 | | |
4247 | | // Parse attribute arguments |
4248 | 12.7k | if (Tok.is(tok::l_paren)) |
4249 | 307 | AttrParsed = ParseCXX11AttributeArgs(AttrName, AttrLoc, attrs, endLoc, |
4250 | 307 | ScopeName, ScopeLoc); |
4251 | | |
4252 | 12.7k | if (!AttrParsed) |
4253 | 12.4k | attrs.addNew( |
4254 | 12.4k | AttrName, |
4255 | 10.9k | SourceRange(ScopeLoc.isValid() ? ScopeLoc1.52k : AttrLoc, AttrLoc), |
4256 | 12.4k | ScopeName, ScopeLoc, nullptr, 0, |
4257 | 12.2k | getLangOpts().CPlusPlus ? ParsedAttr::AS_CXX11 : ParsedAttr::AS_C2x144 ); |
4258 | | |
4259 | 12.7k | if (TryConsumeToken(tok::ellipsis)) |
4260 | 5 | Diag(Tok, diag::err_cxx11_attribute_forbids_ellipsis) |
4261 | 5 | << AttrName; |
4262 | 12.7k | } |
4263 | | |
4264 | 13.1k | SourceLocation CloseLoc = Tok.getLocation(); |
4265 | 13.1k | if (ExpectAndConsume(tok::r_square)) |
4266 | 6 | SkipUntil(tok::r_square); |
4267 | 13.1k | else if (Tok.is(tok::r_square)) |
4268 | 13.1k | checkCompoundToken(CloseLoc, tok::r_square, CompoundToken::AttrEnd); |
4269 | 13.1k | if (endLoc) |
4270 | 13.1k | *endLoc = Tok.getLocation(); |
4271 | 13.1k | if (ExpectAndConsume(tok::r_square)) |
4272 | 5 | SkipUntil(tok::r_square); |
4273 | 13.1k | } |
4274 | | |
4275 | | /// ParseCXX11Attributes - Parse a C++11 or C2x attribute-specifier-seq. |
4276 | | /// |
4277 | | /// attribute-specifier-seq: |
4278 | | /// attribute-specifier-seq[opt] attribute-specifier |
4279 | | void Parser::ParseCXX11Attributes(ParsedAttributesWithRange &attrs, |
4280 | 21.0k | SourceLocation *endLoc) { |
4281 | 21.0k | assert(standardAttributesAllowed()); |
4282 | | |
4283 | 21.0k | SourceLocation StartLoc = Tok.getLocation(), Loc; |
4284 | 21.0k | if (!endLoc) |
4285 | 20.8k | endLoc = &Loc; |
4286 | | |
4287 | 21.2k | do { |
4288 | 21.2k | ParseCXX11AttributeSpecifier(attrs, endLoc); |
4289 | 21.2k | } while (isCXX11AttributeSpecifier()); |
4290 | | |
4291 | 21.0k | attrs.Range = SourceRange(StartLoc, *endLoc); |
4292 | 21.0k | } |
4293 | | |
4294 | 4.96M | void Parser::DiagnoseAndSkipCXX11Attributes() { |
4295 | | // Start and end location of an attribute or an attribute list. |
4296 | 4.96M | SourceLocation StartLoc = Tok.getLocation(); |
4297 | 4.96M | SourceLocation EndLoc = SkipCXX11Attributes(); |
4298 | | |
4299 | 4.96M | if (EndLoc.isValid()) { |
4300 | 9 | SourceRange Range(StartLoc, EndLoc); |
4301 | 9 | Diag(StartLoc, diag::err_attributes_not_allowed) |
4302 | 9 | << Range; |
4303 | 9 | } |
4304 | 4.96M | } |
4305 | | |
4306 | 5.34M | SourceLocation Parser::SkipCXX11Attributes() { |
4307 | 5.34M | SourceLocation EndLoc; |
4308 | | |
4309 | 5.34M | if (!isCXX11AttributeSpecifier()) |
4310 | 5.34M | return EndLoc; |
4311 | | |
4312 | 19 | do { |
4313 | 19 | if (Tok.is(tok::l_square)) { |
4314 | 16 | BalancedDelimiterTracker T(*this, tok::l_square); |
4315 | 16 | T.consumeOpen(); |
4316 | 16 | T.skipToEnd(); |
4317 | 16 | EndLoc = T.getCloseLocation(); |
4318 | 3 | } else { |
4319 | 3 | assert(Tok.is(tok::kw_alignas) && "not an attribute specifier"); |
4320 | 3 | ConsumeToken(); |
4321 | 3 | BalancedDelimiterTracker T(*this, tok::l_paren); |
4322 | 3 | if (!T.consumeOpen()) |
4323 | 3 | T.skipToEnd(); |
4324 | 3 | EndLoc = T.getCloseLocation(); |
4325 | 3 | } |
4326 | 19 | } while (isCXX11AttributeSpecifier()); |
4327 | | |
4328 | 19 | return EndLoc; |
4329 | 19 | } |
4330 | | |
4331 | | /// Parse uuid() attribute when it appears in a [] Microsoft attribute. |
4332 | 61 | void Parser::ParseMicrosoftUuidAttributeArgs(ParsedAttributes &Attrs) { |
4333 | 61 | assert(Tok.is(tok::identifier) && "Not a Microsoft attribute list"); |
4334 | 61 | IdentifierInfo *UuidIdent = Tok.getIdentifierInfo(); |
4335 | 61 | assert(UuidIdent->getName() == "uuid" && "Not a Microsoft attribute list"); |
4336 | | |
4337 | 61 | SourceLocation UuidLoc = Tok.getLocation(); |
4338 | 61 | ConsumeToken(); |
4339 | | |
4340 | | // Ignore the left paren location for now. |
4341 | 61 | BalancedDelimiterTracker T(*this, tok::l_paren); |
4342 | 61 | if (T.consumeOpen()) { |
4343 | 1 | Diag(Tok, diag::err_expected) << tok::l_paren; |
4344 | 1 | return; |
4345 | 1 | } |
4346 | | |
4347 | 60 | ArgsVector ArgExprs; |
4348 | 60 | if (Tok.is(tok::string_literal)) { |
4349 | | // Easy case: uuid("...") -- quoted string. |
4350 | 35 | ExprResult StringResult = ParseStringLiteralExpression(); |
4351 | 35 | if (StringResult.isInvalid()) |
4352 | 0 | return; |
4353 | 35 | ArgExprs.push_back(StringResult.get()); |
4354 | 25 | } else { |
4355 | | // something like uuid({000000A0-0000-0000-C000-000000000049}) -- no |
4356 | | // quotes in the parens. Just append the spelling of all tokens encountered |
4357 | | // until the closing paren. |
4358 | | |
4359 | 25 | SmallString<42> StrBuffer; // 2 "", 36 bytes UUID, 2 optional {}, 1 nul |
4360 | 25 | StrBuffer += "\""; |
4361 | | |
4362 | | // Since none of C++'s keywords match [a-f]+, accepting just tok::l_brace, |
4363 | | // tok::r_brace, tok::minus, tok::identifier (think C000) and |
4364 | | // tok::numeric_constant (0000) should be enough. But the spelling of the |
4365 | | // uuid argument is checked later anyways, so there's no harm in accepting |
4366 | | // almost anything here. |
4367 | | // cl is very strict about whitespace in this form and errors out if any |
4368 | | // is present, so check the space flags on the tokens. |
4369 | 25 | SourceLocation StartLoc = Tok.getLocation(); |
4370 | 218 | while (Tok.isNot(tok::r_paren)) { |
4371 | 197 | if (Tok.hasLeadingSpace() || Tok.isAtStartOfLine()194 ) { |
4372 | 4 | Diag(Tok, diag::err_attribute_uuid_malformed_guid); |
4373 | 4 | SkipUntil(tok::r_paren, StopAtSemi); |
4374 | 4 | return; |
4375 | 4 | } |
4376 | 193 | SmallString<16> SpellingBuffer; |
4377 | 193 | SpellingBuffer.resize(Tok.getLength() + 1); |
4378 | 193 | bool Invalid = false; |
4379 | 193 | StringRef TokSpelling = PP.getSpelling(Tok, SpellingBuffer, &Invalid); |
4380 | 193 | if (Invalid) { |
4381 | 0 | SkipUntil(tok::r_paren, StopAtSemi); |
4382 | 0 | return; |
4383 | 0 | } |
4384 | 193 | StrBuffer += TokSpelling; |
4385 | 193 | ConsumeAnyToken(); |
4386 | 193 | } |
4387 | 21 | StrBuffer += "\""; |
4388 | | |
4389 | 21 | if (Tok.hasLeadingSpace() || Tok.isAtStartOfLine()20 ) { |
4390 | 2 | Diag(Tok, diag::err_attribute_uuid_malformed_guid); |
4391 | 2 | ConsumeParen(); |
4392 | 2 | return; |
4393 | 2 | } |
4394 | | |
4395 | | // Pretend the user wrote the appropriate string literal here. |
4396 | | // ActOnStringLiteral() copies the string data into the literal, so it's |
4397 | | // ok that the Token points to StrBuffer. |
4398 | 19 | Token Toks[1]; |
4399 | 19 | Toks[0].startToken(); |
4400 | 19 | Toks[0].setKind(tok::string_literal); |
4401 | 19 | Toks[0].setLocation(StartLoc); |
4402 | 19 | Toks[0].setLiteralData(StrBuffer.data()); |
4403 | 19 | Toks[0].setLength(StrBuffer.size()); |
4404 | 19 | StringLiteral *UuidString = |
4405 | 19 | cast<StringLiteral>(Actions.ActOnStringLiteral(Toks, nullptr).get()); |
4406 | 19 | ArgExprs.push_back(UuidString); |
4407 | 19 | } |
4408 | | |
4409 | 54 | if (!T.consumeClose()) { |
4410 | 52 | Attrs.addNew(UuidIdent, SourceRange(UuidLoc, T.getCloseLocation()), nullptr, |
4411 | 52 | SourceLocation(), ArgExprs.data(), ArgExprs.size(), |
4412 | 52 | ParsedAttr::AS_Microsoft); |
4413 | 52 | } |
4414 | 54 | } |
4415 | | |
4416 | | /// ParseMicrosoftAttributes - Parse Microsoft attributes [Attr] |
4417 | | /// |
4418 | | /// [MS] ms-attribute: |
4419 | | /// '[' token-seq ']' |
4420 | | /// |
4421 | | /// [MS] ms-attribute-seq: |
4422 | | /// ms-attribute[opt] |
4423 | | /// ms-attribute ms-attribute-seq |
4424 | | void Parser::ParseMicrosoftAttributes(ParsedAttributes &attrs, |
4425 | 75 | SourceLocation *endLoc) { |
4426 | 75 | assert(Tok.is(tok::l_square) && "Not a Microsoft attribute list"); |
4427 | | |
4428 | 76 | do { |
4429 | | // FIXME: If this is actually a C++11 attribute, parse it as one. |
4430 | 76 | BalancedDelimiterTracker T(*this, tok::l_square); |
4431 | 76 | T.consumeOpen(); |
4432 | | |
4433 | | // Skip most ms attributes except for a specific list. |
4434 | 161 | while (true) { |
4435 | 161 | SkipUntil(tok::r_square, tok::identifier, StopAtSemi | StopBeforeMatch); |
4436 | 161 | if (Tok.isNot(tok::identifier)) // ']', but also eof |
4437 | 76 | break; |
4438 | 85 | if (Tok.getIdentifierInfo()->getName() == "uuid") |
4439 | 61 | ParseMicrosoftUuidAttributeArgs(attrs); |
4440 | 24 | else |
4441 | 24 | ConsumeToken(); |
4442 | 85 | } |
4443 | | |
4444 | 76 | T.consumeClose(); |
4445 | 76 | if (endLoc) |
4446 | 0 | *endLoc = T.getCloseLocation(); |
4447 | 76 | } while (Tok.is(tok::l_square)); |
4448 | 75 | } |
4449 | | |
4450 | | void Parser::ParseMicrosoftIfExistsClassDeclaration( |
4451 | | DeclSpec::TST TagType, ParsedAttributes &AccessAttrs, |
4452 | 9 | AccessSpecifier &CurAS) { |
4453 | 9 | IfExistsCondition Result; |
4454 | 9 | if (ParseMicrosoftIfExistsCondition(Result)) |
4455 | 0 | return; |
4456 | | |
4457 | 9 | BalancedDelimiterTracker Braces(*this, tok::l_brace); |
4458 | 9 | if (Braces.consumeOpen()) { |
4459 | 0 | Diag(Tok, diag::err_expected) << tok::l_brace; |
4460 | 0 | return; |
4461 | 0 | } |
4462 | | |
4463 | 9 | switch (Result.Behavior) { |
4464 | 5 | case IEB_Parse: |
4465 | | // Parse the declarations below. |
4466 | 5 | break; |
4467 | | |
4468 | 1 | case IEB_Dependent: |
4469 | 1 | Diag(Result.KeywordLoc, diag::warn_microsoft_dependent_exists) |
4470 | 1 | << Result.IsIfExists; |
4471 | | // Fall through to skip. |
4472 | 1 | LLVM_FALLTHROUGH; |
4473 | | |
4474 | 4 | case IEB_Skip: |
4475 | 4 | Braces.skipToEnd(); |
4476 | 4 | return; |
4477 | 5 | } |
4478 | | |
4479 | 11 | while (5 Tok.isNot(tok::r_brace) && !isEofOrEom()6 ) { |
4480 | | // __if_exists, __if_not_exists can nest. |
4481 | 6 | if (Tok.isOneOf(tok::kw___if_exists, tok::kw___if_not_exists)) { |
4482 | 1 | ParseMicrosoftIfExistsClassDeclaration(TagType, |
4483 | 1 | AccessAttrs, CurAS); |
4484 | 1 | continue; |
4485 | 1 | } |
4486 | | |
4487 | | // Check for extraneous top-level semicolon. |
4488 | 5 | if (Tok.is(tok::semi)) { |
4489 | 0 | ConsumeExtraSemi(InsideStruct, TagType); |
4490 | 0 | continue; |
4491 | 0 | } |
4492 | | |
4493 | 5 | AccessSpecifier AS = getAccessSpecifierIfPresent(); |
4494 | 5 | if (AS != AS_none) { |
4495 | | // Current token is a C++ access specifier. |
4496 | 0 | CurAS = AS; |
4497 | 0 | SourceLocation ASLoc = Tok.getLocation(); |
4498 | 0 | ConsumeToken(); |
4499 | 0 | if (Tok.is(tok::colon)) |
4500 | 0 | Actions.ActOnAccessSpecifier(AS, ASLoc, Tok.getLocation(), |
4501 | 0 | ParsedAttributesView{}); |
4502 | 0 | else |
4503 | 0 | Diag(Tok, diag::err_expected) << tok::colon; |
4504 | 0 | ConsumeToken(); |
4505 | 0 | continue; |
4506 | 0 | } |
4507 | | |
4508 | | // Parse all the comma separated declarators. |
4509 | 5 | ParseCXXClassMemberDeclaration(CurAS, AccessAttrs); |
4510 | 5 | } |
4511 | | |
4512 | 5 | Braces.consumeClose(); |
4513 | 5 | } |