xref: /minix3/external/bsd/llvm/dist/clang/lib/Parse/ParseDeclCXX.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc //===--- ParseDeclCXX.cpp - C++ Declaration Parsing -----------------------===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc //                     The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc //  This file implements the C++ Declaration portions of the Parser interfaces.
11f4a2713aSLionel Sambuc //
12f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
13f4a2713aSLionel Sambuc 
14f4a2713aSLionel Sambuc #include "clang/Parse/Parser.h"
15f4a2713aSLionel Sambuc #include "RAIIObjectsForParser.h"
16*0a6a1f1dSLionel Sambuc #include "clang/AST/ASTContext.h"
17*0a6a1f1dSLionel Sambuc #include "clang/AST/DeclTemplate.h"
18*0a6a1f1dSLionel Sambuc #include "clang/Basic/Attributes.h"
19f4a2713aSLionel Sambuc #include "clang/Basic/CharInfo.h"
20f4a2713aSLionel Sambuc #include "clang/Basic/OperatorKinds.h"
21*0a6a1f1dSLionel Sambuc #include "clang/Basic/TargetInfo.h"
22f4a2713aSLionel Sambuc #include "clang/Parse/ParseDiagnostic.h"
23f4a2713aSLionel Sambuc #include "clang/Sema/DeclSpec.h"
24f4a2713aSLionel Sambuc #include "clang/Sema/ParsedTemplate.h"
25f4a2713aSLionel Sambuc #include "clang/Sema/PrettyDeclStackTrace.h"
26f4a2713aSLionel Sambuc #include "clang/Sema/Scope.h"
27f4a2713aSLionel Sambuc #include "clang/Sema/SemaDiagnostic.h"
28f4a2713aSLionel Sambuc #include "llvm/ADT/SmallString.h"
29f4a2713aSLionel Sambuc using namespace clang;
30f4a2713aSLionel Sambuc 
31f4a2713aSLionel Sambuc /// ParseNamespace - We know that the current token is a namespace keyword. This
32f4a2713aSLionel Sambuc /// may either be a top level namespace or a block-level namespace alias. If
33f4a2713aSLionel Sambuc /// there was an inline keyword, it has already been parsed.
34f4a2713aSLionel Sambuc ///
35f4a2713aSLionel Sambuc ///       namespace-definition: [C++ 7.3: basic.namespace]
36f4a2713aSLionel Sambuc ///         named-namespace-definition
37f4a2713aSLionel Sambuc ///         unnamed-namespace-definition
38f4a2713aSLionel Sambuc ///
39f4a2713aSLionel Sambuc ///       unnamed-namespace-definition:
40f4a2713aSLionel Sambuc ///         'inline'[opt] 'namespace' attributes[opt] '{' namespace-body '}'
41f4a2713aSLionel Sambuc ///
42f4a2713aSLionel Sambuc ///       named-namespace-definition:
43f4a2713aSLionel Sambuc ///         original-namespace-definition
44f4a2713aSLionel Sambuc ///         extension-namespace-definition
45f4a2713aSLionel Sambuc ///
46f4a2713aSLionel Sambuc ///       original-namespace-definition:
47f4a2713aSLionel Sambuc ///         'inline'[opt] 'namespace' identifier attributes[opt]
48f4a2713aSLionel Sambuc ///             '{' namespace-body '}'
49f4a2713aSLionel Sambuc ///
50f4a2713aSLionel Sambuc ///       extension-namespace-definition:
51f4a2713aSLionel Sambuc ///         'inline'[opt] 'namespace' original-namespace-name
52f4a2713aSLionel Sambuc ///             '{' namespace-body '}'
53f4a2713aSLionel Sambuc ///
54f4a2713aSLionel Sambuc ///       namespace-alias-definition:  [C++ 7.3.2: namespace.alias]
55f4a2713aSLionel Sambuc ///         'namespace' identifier '=' qualified-namespace-specifier ';'
56f4a2713aSLionel Sambuc ///
ParseNamespace(unsigned Context,SourceLocation & DeclEnd,SourceLocation InlineLoc)57f4a2713aSLionel Sambuc Decl *Parser::ParseNamespace(unsigned Context,
58f4a2713aSLionel Sambuc                              SourceLocation &DeclEnd,
59f4a2713aSLionel Sambuc                              SourceLocation InlineLoc) {
60f4a2713aSLionel Sambuc   assert(Tok.is(tok::kw_namespace) && "Not a namespace!");
61f4a2713aSLionel Sambuc   SourceLocation NamespaceLoc = ConsumeToken();  // eat the 'namespace'.
62f4a2713aSLionel Sambuc   ObjCDeclContextSwitch ObjCDC(*this);
63f4a2713aSLionel Sambuc 
64f4a2713aSLionel Sambuc   if (Tok.is(tok::code_completion)) {
65f4a2713aSLionel Sambuc     Actions.CodeCompleteNamespaceDecl(getCurScope());
66f4a2713aSLionel Sambuc     cutOffParsing();
67*0a6a1f1dSLionel Sambuc     return nullptr;
68f4a2713aSLionel Sambuc   }
69f4a2713aSLionel Sambuc 
70f4a2713aSLionel Sambuc   SourceLocation IdentLoc;
71*0a6a1f1dSLionel Sambuc   IdentifierInfo *Ident = nullptr;
72f4a2713aSLionel Sambuc   std::vector<SourceLocation> ExtraIdentLoc;
73f4a2713aSLionel Sambuc   std::vector<IdentifierInfo*> ExtraIdent;
74f4a2713aSLionel Sambuc   std::vector<SourceLocation> ExtraNamespaceLoc;
75f4a2713aSLionel Sambuc 
76*0a6a1f1dSLionel Sambuc   ParsedAttributesWithRange attrs(AttrFactory);
77*0a6a1f1dSLionel Sambuc   SourceLocation attrLoc;
78*0a6a1f1dSLionel Sambuc   if (getLangOpts().CPlusPlus11 && isCXX11AttributeSpecifier()) {
79*0a6a1f1dSLionel Sambuc     if (!getLangOpts().CPlusPlus1z)
80*0a6a1f1dSLionel Sambuc       Diag(Tok.getLocation(), diag::warn_cxx14_compat_attribute)
81*0a6a1f1dSLionel Sambuc           << 0 /*namespace*/;
82*0a6a1f1dSLionel Sambuc     attrLoc = Tok.getLocation();
83*0a6a1f1dSLionel Sambuc     ParseCXX11Attributes(attrs);
84*0a6a1f1dSLionel Sambuc   }
85f4a2713aSLionel Sambuc 
86f4a2713aSLionel Sambuc   if (Tok.is(tok::identifier)) {
87f4a2713aSLionel Sambuc     Ident = Tok.getIdentifierInfo();
88f4a2713aSLionel Sambuc     IdentLoc = ConsumeToken();  // eat the identifier.
89f4a2713aSLionel Sambuc     while (Tok.is(tok::coloncolon) && NextToken().is(tok::identifier)) {
90f4a2713aSLionel Sambuc       ExtraNamespaceLoc.push_back(ConsumeToken());
91f4a2713aSLionel Sambuc       ExtraIdent.push_back(Tok.getIdentifierInfo());
92f4a2713aSLionel Sambuc       ExtraIdentLoc.push_back(ConsumeToken());
93f4a2713aSLionel Sambuc     }
94f4a2713aSLionel Sambuc   }
95f4a2713aSLionel Sambuc 
96*0a6a1f1dSLionel Sambuc   // A nested namespace definition cannot have attributes.
97*0a6a1f1dSLionel Sambuc   if (!ExtraNamespaceLoc.empty() && attrLoc.isValid())
98*0a6a1f1dSLionel Sambuc     Diag(attrLoc, diag::err_unexpected_nested_namespace_attribute);
99*0a6a1f1dSLionel Sambuc 
100f4a2713aSLionel Sambuc   // Read label attributes, if present.
101f4a2713aSLionel Sambuc   if (Tok.is(tok::kw___attribute)) {
102*0a6a1f1dSLionel Sambuc     attrLoc = Tok.getLocation();
103f4a2713aSLionel Sambuc     ParseGNUAttributes(attrs);
104f4a2713aSLionel Sambuc   }
105f4a2713aSLionel Sambuc 
106f4a2713aSLionel Sambuc   if (Tok.is(tok::equal)) {
107*0a6a1f1dSLionel Sambuc     if (!Ident) {
108*0a6a1f1dSLionel Sambuc       Diag(Tok, diag::err_expected) << tok::identifier;
109f4a2713aSLionel Sambuc       // Skip to end of the definition and eat the ';'.
110f4a2713aSLionel Sambuc       SkipUntil(tok::semi);
111*0a6a1f1dSLionel Sambuc       return nullptr;
112f4a2713aSLionel Sambuc     }
113*0a6a1f1dSLionel Sambuc     if (attrLoc.isValid())
114*0a6a1f1dSLionel Sambuc       Diag(attrLoc, diag::err_unexpected_namespace_attributes_alias);
115f4a2713aSLionel Sambuc     if (InlineLoc.isValid())
116f4a2713aSLionel Sambuc       Diag(InlineLoc, diag::err_inline_namespace_alias)
117f4a2713aSLionel Sambuc           << FixItHint::CreateRemoval(InlineLoc);
118f4a2713aSLionel Sambuc     return ParseNamespaceAlias(NamespaceLoc, IdentLoc, Ident, DeclEnd);
119f4a2713aSLionel Sambuc   }
120f4a2713aSLionel Sambuc 
121f4a2713aSLionel Sambuc 
122f4a2713aSLionel Sambuc   BalancedDelimiterTracker T(*this, tok::l_brace);
123f4a2713aSLionel Sambuc   if (T.consumeOpen()) {
124*0a6a1f1dSLionel Sambuc     if (Ident)
125*0a6a1f1dSLionel Sambuc       Diag(Tok, diag::err_expected) << tok::l_brace;
126*0a6a1f1dSLionel Sambuc     else
127*0a6a1f1dSLionel Sambuc       Diag(Tok, diag::err_expected_either) << tok::identifier << tok::l_brace;
128*0a6a1f1dSLionel Sambuc     return nullptr;
129f4a2713aSLionel Sambuc   }
130f4a2713aSLionel Sambuc 
131f4a2713aSLionel Sambuc   if (getCurScope()->isClassScope() || getCurScope()->isTemplateParamScope() ||
132f4a2713aSLionel Sambuc       getCurScope()->isInObjcMethodScope() || getCurScope()->getBlockParent() ||
133f4a2713aSLionel Sambuc       getCurScope()->getFnParent()) {
134f4a2713aSLionel Sambuc     Diag(T.getOpenLocation(), diag::err_namespace_nonnamespace_scope);
135f4a2713aSLionel Sambuc     SkipUntil(tok::r_brace);
136*0a6a1f1dSLionel Sambuc     return nullptr;
137f4a2713aSLionel Sambuc   }
138f4a2713aSLionel Sambuc 
139*0a6a1f1dSLionel Sambuc   if (ExtraIdent.empty()) {
140*0a6a1f1dSLionel Sambuc     // Normal namespace definition, not a nested-namespace-definition.
141*0a6a1f1dSLionel Sambuc   } else if (InlineLoc.isValid()) {
142*0a6a1f1dSLionel Sambuc     Diag(InlineLoc, diag::err_inline_nested_namespace_definition);
143*0a6a1f1dSLionel Sambuc   } else if (getLangOpts().CPlusPlus1z) {
144*0a6a1f1dSLionel Sambuc     Diag(ExtraNamespaceLoc[0],
145*0a6a1f1dSLionel Sambuc          diag::warn_cxx14_compat_nested_namespace_definition);
146*0a6a1f1dSLionel Sambuc   } else {
147f4a2713aSLionel Sambuc     TentativeParsingAction TPA(*this);
148f4a2713aSLionel Sambuc     SkipUntil(tok::r_brace, StopBeforeMatch);
149f4a2713aSLionel Sambuc     Token rBraceToken = Tok;
150f4a2713aSLionel Sambuc     TPA.Revert();
151f4a2713aSLionel Sambuc 
152f4a2713aSLionel Sambuc     if (!rBraceToken.is(tok::r_brace)) {
153*0a6a1f1dSLionel Sambuc       Diag(ExtraNamespaceLoc[0], diag::ext_nested_namespace_definition)
154f4a2713aSLionel Sambuc           << SourceRange(ExtraNamespaceLoc.front(), ExtraIdentLoc.back());
155f4a2713aSLionel Sambuc     } else {
156f4a2713aSLionel Sambuc       std::string NamespaceFix;
157f4a2713aSLionel Sambuc       for (std::vector<IdentifierInfo*>::iterator I = ExtraIdent.begin(),
158f4a2713aSLionel Sambuc            E = ExtraIdent.end(); I != E; ++I) {
159f4a2713aSLionel Sambuc         NamespaceFix += " { namespace ";
160f4a2713aSLionel Sambuc         NamespaceFix += (*I)->getName();
161f4a2713aSLionel Sambuc       }
162f4a2713aSLionel Sambuc 
163f4a2713aSLionel Sambuc       std::string RBraces;
164f4a2713aSLionel Sambuc       for (unsigned i = 0, e = ExtraIdent.size(); i != e; ++i)
165f4a2713aSLionel Sambuc         RBraces +=  "} ";
166f4a2713aSLionel Sambuc 
167*0a6a1f1dSLionel Sambuc       Diag(ExtraNamespaceLoc[0], diag::ext_nested_namespace_definition)
168f4a2713aSLionel Sambuc           << FixItHint::CreateReplacement(SourceRange(ExtraNamespaceLoc.front(),
169f4a2713aSLionel Sambuc                                                       ExtraIdentLoc.back()),
170f4a2713aSLionel Sambuc                                           NamespaceFix)
171f4a2713aSLionel Sambuc           << FixItHint::CreateInsertion(rBraceToken.getLocation(), RBraces);
172f4a2713aSLionel Sambuc     }
173f4a2713aSLionel Sambuc   }
174f4a2713aSLionel Sambuc 
175f4a2713aSLionel Sambuc   // If we're still good, complain about inline namespaces in non-C++0x now.
176f4a2713aSLionel Sambuc   if (InlineLoc.isValid())
177f4a2713aSLionel Sambuc     Diag(InlineLoc, getLangOpts().CPlusPlus11 ?
178f4a2713aSLionel Sambuc          diag::warn_cxx98_compat_inline_namespace : diag::ext_inline_namespace);
179f4a2713aSLionel Sambuc 
180f4a2713aSLionel Sambuc   // Enter a scope for the namespace.
181f4a2713aSLionel Sambuc   ParseScope NamespaceScope(this, Scope::DeclScope);
182f4a2713aSLionel Sambuc 
183f4a2713aSLionel Sambuc   Decl *NamespcDecl =
184f4a2713aSLionel Sambuc     Actions.ActOnStartNamespaceDef(getCurScope(), InlineLoc, NamespaceLoc,
185f4a2713aSLionel Sambuc                                    IdentLoc, Ident, T.getOpenLocation(),
186f4a2713aSLionel Sambuc                                    attrs.getList());
187f4a2713aSLionel Sambuc 
188f4a2713aSLionel Sambuc   PrettyDeclStackTraceEntry CrashInfo(Actions, NamespcDecl, NamespaceLoc,
189f4a2713aSLionel Sambuc                                       "parsing namespace");
190f4a2713aSLionel Sambuc 
191f4a2713aSLionel Sambuc   // Parse the contents of the namespace.  This includes parsing recovery on
192f4a2713aSLionel Sambuc   // any improperly nested namespaces.
193f4a2713aSLionel Sambuc   ParseInnerNamespace(ExtraIdentLoc, ExtraIdent, ExtraNamespaceLoc, 0,
194f4a2713aSLionel Sambuc                       InlineLoc, attrs, T);
195f4a2713aSLionel Sambuc 
196f4a2713aSLionel Sambuc   // Leave the namespace scope.
197f4a2713aSLionel Sambuc   NamespaceScope.Exit();
198f4a2713aSLionel Sambuc 
199f4a2713aSLionel Sambuc   DeclEnd = T.getCloseLocation();
200f4a2713aSLionel Sambuc   Actions.ActOnFinishNamespaceDef(NamespcDecl, DeclEnd);
201f4a2713aSLionel Sambuc 
202f4a2713aSLionel Sambuc   return NamespcDecl;
203f4a2713aSLionel Sambuc }
204f4a2713aSLionel Sambuc 
205f4a2713aSLionel Sambuc /// ParseInnerNamespace - Parse the contents of a namespace.
ParseInnerNamespace(std::vector<SourceLocation> & IdentLoc,std::vector<IdentifierInfo * > & Ident,std::vector<SourceLocation> & NamespaceLoc,unsigned int index,SourceLocation & InlineLoc,ParsedAttributes & attrs,BalancedDelimiterTracker & Tracker)206f4a2713aSLionel Sambuc void Parser::ParseInnerNamespace(std::vector<SourceLocation> &IdentLoc,
207f4a2713aSLionel Sambuc                                  std::vector<IdentifierInfo *> &Ident,
208f4a2713aSLionel Sambuc                                  std::vector<SourceLocation> &NamespaceLoc,
209f4a2713aSLionel Sambuc                                  unsigned int index, SourceLocation &InlineLoc,
210f4a2713aSLionel Sambuc                                  ParsedAttributes &attrs,
211f4a2713aSLionel Sambuc                                  BalancedDelimiterTracker &Tracker) {
212f4a2713aSLionel Sambuc   if (index == Ident.size()) {
213*0a6a1f1dSLionel Sambuc     while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
214f4a2713aSLionel Sambuc       ParsedAttributesWithRange attrs(AttrFactory);
215f4a2713aSLionel Sambuc       MaybeParseCXX11Attributes(attrs);
216f4a2713aSLionel Sambuc       MaybeParseMicrosoftAttributes(attrs);
217f4a2713aSLionel Sambuc       ParseExternalDeclaration(attrs);
218f4a2713aSLionel Sambuc     }
219f4a2713aSLionel Sambuc 
220f4a2713aSLionel Sambuc     // The caller is what called check -- we are simply calling
221f4a2713aSLionel Sambuc     // the close for it.
222f4a2713aSLionel Sambuc     Tracker.consumeClose();
223f4a2713aSLionel Sambuc 
224f4a2713aSLionel Sambuc     return;
225f4a2713aSLionel Sambuc   }
226f4a2713aSLionel Sambuc 
227*0a6a1f1dSLionel Sambuc   // Handle a nested namespace definition.
228*0a6a1f1dSLionel Sambuc   // FIXME: Preserve the source information through to the AST rather than
229*0a6a1f1dSLionel Sambuc   // desugaring it here.
230f4a2713aSLionel Sambuc   ParseScope NamespaceScope(this, Scope::DeclScope);
231f4a2713aSLionel Sambuc   Decl *NamespcDecl =
232f4a2713aSLionel Sambuc     Actions.ActOnStartNamespaceDef(getCurScope(), SourceLocation(),
233f4a2713aSLionel Sambuc                                    NamespaceLoc[index], IdentLoc[index],
234f4a2713aSLionel Sambuc                                    Ident[index], Tracker.getOpenLocation(),
235f4a2713aSLionel Sambuc                                    attrs.getList());
236f4a2713aSLionel Sambuc 
237f4a2713aSLionel Sambuc   ParseInnerNamespace(IdentLoc, Ident, NamespaceLoc, ++index, InlineLoc,
238f4a2713aSLionel Sambuc                       attrs, Tracker);
239f4a2713aSLionel Sambuc 
240f4a2713aSLionel Sambuc   NamespaceScope.Exit();
241f4a2713aSLionel Sambuc 
242f4a2713aSLionel Sambuc   Actions.ActOnFinishNamespaceDef(NamespcDecl, Tracker.getCloseLocation());
243f4a2713aSLionel Sambuc }
244f4a2713aSLionel Sambuc 
245f4a2713aSLionel Sambuc /// ParseNamespaceAlias - Parse the part after the '=' in a namespace
246f4a2713aSLionel Sambuc /// alias definition.
247f4a2713aSLionel Sambuc ///
ParseNamespaceAlias(SourceLocation NamespaceLoc,SourceLocation AliasLoc,IdentifierInfo * Alias,SourceLocation & DeclEnd)248f4a2713aSLionel Sambuc Decl *Parser::ParseNamespaceAlias(SourceLocation NamespaceLoc,
249f4a2713aSLionel Sambuc                                   SourceLocation AliasLoc,
250f4a2713aSLionel Sambuc                                   IdentifierInfo *Alias,
251f4a2713aSLionel Sambuc                                   SourceLocation &DeclEnd) {
252f4a2713aSLionel Sambuc   assert(Tok.is(tok::equal) && "Not equal token");
253f4a2713aSLionel Sambuc 
254f4a2713aSLionel Sambuc   ConsumeToken(); // eat the '='.
255f4a2713aSLionel Sambuc 
256f4a2713aSLionel Sambuc   if (Tok.is(tok::code_completion)) {
257f4a2713aSLionel Sambuc     Actions.CodeCompleteNamespaceAliasDecl(getCurScope());
258f4a2713aSLionel Sambuc     cutOffParsing();
259*0a6a1f1dSLionel Sambuc     return nullptr;
260f4a2713aSLionel Sambuc   }
261f4a2713aSLionel Sambuc 
262f4a2713aSLionel Sambuc   CXXScopeSpec SS;
263f4a2713aSLionel Sambuc   // Parse (optional) nested-name-specifier.
264f4a2713aSLionel Sambuc   ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
265f4a2713aSLionel Sambuc 
266f4a2713aSLionel Sambuc   if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
267f4a2713aSLionel Sambuc     Diag(Tok, diag::err_expected_namespace_name);
268f4a2713aSLionel Sambuc     // Skip to end of the definition and eat the ';'.
269f4a2713aSLionel Sambuc     SkipUntil(tok::semi);
270*0a6a1f1dSLionel Sambuc     return nullptr;
271f4a2713aSLionel Sambuc   }
272f4a2713aSLionel Sambuc 
273f4a2713aSLionel Sambuc   // Parse identifier.
274f4a2713aSLionel Sambuc   IdentifierInfo *Ident = Tok.getIdentifierInfo();
275f4a2713aSLionel Sambuc   SourceLocation IdentLoc = ConsumeToken();
276f4a2713aSLionel Sambuc 
277f4a2713aSLionel Sambuc   // Eat the ';'.
278f4a2713aSLionel Sambuc   DeclEnd = Tok.getLocation();
279*0a6a1f1dSLionel Sambuc   if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after_namespace_name))
280*0a6a1f1dSLionel Sambuc     SkipUntil(tok::semi);
281f4a2713aSLionel Sambuc 
282f4a2713aSLionel Sambuc   return Actions.ActOnNamespaceAliasDef(getCurScope(), NamespaceLoc, AliasLoc, Alias,
283f4a2713aSLionel Sambuc                                         SS, IdentLoc, Ident);
284f4a2713aSLionel Sambuc }
285f4a2713aSLionel Sambuc 
286f4a2713aSLionel Sambuc /// ParseLinkage - We know that the current token is a string_literal
287f4a2713aSLionel Sambuc /// and just before that, that extern was seen.
288f4a2713aSLionel Sambuc ///
289f4a2713aSLionel Sambuc ///       linkage-specification: [C++ 7.5p2: dcl.link]
290f4a2713aSLionel Sambuc ///         'extern' string-literal '{' declaration-seq[opt] '}'
291f4a2713aSLionel Sambuc ///         'extern' string-literal declaration
292f4a2713aSLionel Sambuc ///
ParseLinkage(ParsingDeclSpec & DS,unsigned Context)293f4a2713aSLionel Sambuc Decl *Parser::ParseLinkage(ParsingDeclSpec &DS, unsigned Context) {
294*0a6a1f1dSLionel Sambuc   assert(isTokenStringLiteral() && "Not a string literal!");
295*0a6a1f1dSLionel Sambuc   ExprResult Lang = ParseStringLiteralExpression(false);
296f4a2713aSLionel Sambuc 
297f4a2713aSLionel Sambuc   ParseScope LinkageScope(this, Scope::DeclScope);
298*0a6a1f1dSLionel Sambuc   Decl *LinkageSpec =
299*0a6a1f1dSLionel Sambuc       Lang.isInvalid()
300*0a6a1f1dSLionel Sambuc           ? nullptr
301*0a6a1f1dSLionel Sambuc           : Actions.ActOnStartLinkageSpecification(
302*0a6a1f1dSLionel Sambuc                 getCurScope(), DS.getSourceRange().getBegin(), Lang.get(),
303*0a6a1f1dSLionel Sambuc                 Tok.is(tok::l_brace) ? Tok.getLocation() : SourceLocation());
304f4a2713aSLionel Sambuc 
305f4a2713aSLionel Sambuc   ParsedAttributesWithRange attrs(AttrFactory);
306f4a2713aSLionel Sambuc   MaybeParseCXX11Attributes(attrs);
307f4a2713aSLionel Sambuc   MaybeParseMicrosoftAttributes(attrs);
308f4a2713aSLionel Sambuc 
309f4a2713aSLionel Sambuc   if (Tok.isNot(tok::l_brace)) {
310f4a2713aSLionel Sambuc     // Reset the source range in DS, as the leading "extern"
311f4a2713aSLionel Sambuc     // does not really belong to the inner declaration ...
312f4a2713aSLionel Sambuc     DS.SetRangeStart(SourceLocation());
313f4a2713aSLionel Sambuc     DS.SetRangeEnd(SourceLocation());
314f4a2713aSLionel Sambuc     // ... but anyway remember that such an "extern" was seen.
315f4a2713aSLionel Sambuc     DS.setExternInLinkageSpec(true);
316f4a2713aSLionel Sambuc     ParseExternalDeclaration(attrs, &DS);
317*0a6a1f1dSLionel Sambuc     return LinkageSpec ? Actions.ActOnFinishLinkageSpecification(
318*0a6a1f1dSLionel Sambuc                              getCurScope(), LinkageSpec, SourceLocation())
319*0a6a1f1dSLionel Sambuc                        : nullptr;
320f4a2713aSLionel Sambuc   }
321f4a2713aSLionel Sambuc 
322f4a2713aSLionel Sambuc   DS.abort();
323f4a2713aSLionel Sambuc 
324f4a2713aSLionel Sambuc   ProhibitAttributes(attrs);
325f4a2713aSLionel Sambuc 
326f4a2713aSLionel Sambuc   BalancedDelimiterTracker T(*this, tok::l_brace);
327f4a2713aSLionel Sambuc   T.consumeOpen();
328*0a6a1f1dSLionel Sambuc 
329*0a6a1f1dSLionel Sambuc   unsigned NestedModules = 0;
330*0a6a1f1dSLionel Sambuc   while (true) {
331*0a6a1f1dSLionel Sambuc     switch (Tok.getKind()) {
332*0a6a1f1dSLionel Sambuc     case tok::annot_module_begin:
333*0a6a1f1dSLionel Sambuc       ++NestedModules;
334*0a6a1f1dSLionel Sambuc       ParseTopLevelDecl();
335*0a6a1f1dSLionel Sambuc       continue;
336*0a6a1f1dSLionel Sambuc 
337*0a6a1f1dSLionel Sambuc     case tok::annot_module_end:
338*0a6a1f1dSLionel Sambuc       if (!NestedModules)
339*0a6a1f1dSLionel Sambuc         break;
340*0a6a1f1dSLionel Sambuc       --NestedModules;
341*0a6a1f1dSLionel Sambuc       ParseTopLevelDecl();
342*0a6a1f1dSLionel Sambuc       continue;
343*0a6a1f1dSLionel Sambuc 
344*0a6a1f1dSLionel Sambuc     case tok::annot_module_include:
345*0a6a1f1dSLionel Sambuc       ParseTopLevelDecl();
346*0a6a1f1dSLionel Sambuc       continue;
347*0a6a1f1dSLionel Sambuc 
348*0a6a1f1dSLionel Sambuc     case tok::eof:
349*0a6a1f1dSLionel Sambuc       break;
350*0a6a1f1dSLionel Sambuc 
351*0a6a1f1dSLionel Sambuc     case tok::r_brace:
352*0a6a1f1dSLionel Sambuc       if (!NestedModules)
353*0a6a1f1dSLionel Sambuc         break;
354*0a6a1f1dSLionel Sambuc       // Fall through.
355*0a6a1f1dSLionel Sambuc     default:
356f4a2713aSLionel Sambuc       ParsedAttributesWithRange attrs(AttrFactory);
357f4a2713aSLionel Sambuc       MaybeParseCXX11Attributes(attrs);
358f4a2713aSLionel Sambuc       MaybeParseMicrosoftAttributes(attrs);
359f4a2713aSLionel Sambuc       ParseExternalDeclaration(attrs);
360*0a6a1f1dSLionel Sambuc       continue;
361*0a6a1f1dSLionel Sambuc     }
362*0a6a1f1dSLionel Sambuc 
363*0a6a1f1dSLionel Sambuc     break;
364f4a2713aSLionel Sambuc   }
365f4a2713aSLionel Sambuc 
366f4a2713aSLionel Sambuc   T.consumeClose();
367*0a6a1f1dSLionel Sambuc   return LinkageSpec ? Actions.ActOnFinishLinkageSpecification(
368*0a6a1f1dSLionel Sambuc                            getCurScope(), LinkageSpec, T.getCloseLocation())
369*0a6a1f1dSLionel Sambuc                      : nullptr;
370f4a2713aSLionel Sambuc }
371f4a2713aSLionel Sambuc 
372f4a2713aSLionel Sambuc /// ParseUsingDirectiveOrDeclaration - Parse C++ using using-declaration or
373f4a2713aSLionel Sambuc /// using-directive. Assumes that current token is 'using'.
ParseUsingDirectiveOrDeclaration(unsigned Context,const ParsedTemplateInfo & TemplateInfo,SourceLocation & DeclEnd,ParsedAttributesWithRange & attrs,Decl ** OwnedType)374f4a2713aSLionel Sambuc Decl *Parser::ParseUsingDirectiveOrDeclaration(unsigned Context,
375f4a2713aSLionel Sambuc                                          const ParsedTemplateInfo &TemplateInfo,
376f4a2713aSLionel Sambuc                                                SourceLocation &DeclEnd,
377f4a2713aSLionel Sambuc                                              ParsedAttributesWithRange &attrs,
378f4a2713aSLionel Sambuc                                                Decl **OwnedType) {
379f4a2713aSLionel Sambuc   assert(Tok.is(tok::kw_using) && "Not using token");
380f4a2713aSLionel Sambuc   ObjCDeclContextSwitch ObjCDC(*this);
381f4a2713aSLionel Sambuc 
382f4a2713aSLionel Sambuc   // Eat 'using'.
383f4a2713aSLionel Sambuc   SourceLocation UsingLoc = ConsumeToken();
384f4a2713aSLionel Sambuc 
385f4a2713aSLionel Sambuc   if (Tok.is(tok::code_completion)) {
386f4a2713aSLionel Sambuc     Actions.CodeCompleteUsing(getCurScope());
387f4a2713aSLionel Sambuc     cutOffParsing();
388*0a6a1f1dSLionel Sambuc     return nullptr;
389f4a2713aSLionel Sambuc   }
390f4a2713aSLionel Sambuc 
391f4a2713aSLionel Sambuc   // 'using namespace' means this is a using-directive.
392f4a2713aSLionel Sambuc   if (Tok.is(tok::kw_namespace)) {
393f4a2713aSLionel Sambuc     // Template parameters are always an error here.
394f4a2713aSLionel Sambuc     if (TemplateInfo.Kind) {
395f4a2713aSLionel Sambuc       SourceRange R = TemplateInfo.getSourceRange();
396f4a2713aSLionel Sambuc       Diag(UsingLoc, diag::err_templated_using_directive)
397f4a2713aSLionel Sambuc         << R << FixItHint::CreateRemoval(R);
398f4a2713aSLionel Sambuc     }
399f4a2713aSLionel Sambuc 
400f4a2713aSLionel Sambuc     return ParseUsingDirective(Context, UsingLoc, DeclEnd, attrs);
401f4a2713aSLionel Sambuc   }
402f4a2713aSLionel Sambuc 
403f4a2713aSLionel Sambuc   // Otherwise, it must be a using-declaration or an alias-declaration.
404f4a2713aSLionel Sambuc 
405f4a2713aSLionel Sambuc   // Using declarations can't have attributes.
406f4a2713aSLionel Sambuc   ProhibitAttributes(attrs);
407f4a2713aSLionel Sambuc 
408f4a2713aSLionel Sambuc   return ParseUsingDeclaration(Context, TemplateInfo, UsingLoc, DeclEnd,
409f4a2713aSLionel Sambuc                                     AS_none, OwnedType);
410f4a2713aSLionel Sambuc }
411f4a2713aSLionel Sambuc 
412f4a2713aSLionel Sambuc /// ParseUsingDirective - Parse C++ using-directive, assumes
413f4a2713aSLionel Sambuc /// that current token is 'namespace' and 'using' was already parsed.
414f4a2713aSLionel Sambuc ///
415f4a2713aSLionel Sambuc ///       using-directive: [C++ 7.3.p4: namespace.udir]
416f4a2713aSLionel Sambuc ///        'using' 'namespace' ::[opt] nested-name-specifier[opt]
417f4a2713aSLionel Sambuc ///                 namespace-name ;
418f4a2713aSLionel Sambuc /// [GNU] using-directive:
419f4a2713aSLionel Sambuc ///        'using' 'namespace' ::[opt] nested-name-specifier[opt]
420f4a2713aSLionel Sambuc ///                 namespace-name attributes[opt] ;
421f4a2713aSLionel Sambuc ///
ParseUsingDirective(unsigned Context,SourceLocation UsingLoc,SourceLocation & DeclEnd,ParsedAttributes & attrs)422f4a2713aSLionel Sambuc Decl *Parser::ParseUsingDirective(unsigned Context,
423f4a2713aSLionel Sambuc                                   SourceLocation UsingLoc,
424f4a2713aSLionel Sambuc                                   SourceLocation &DeclEnd,
425f4a2713aSLionel Sambuc                                   ParsedAttributes &attrs) {
426f4a2713aSLionel Sambuc   assert(Tok.is(tok::kw_namespace) && "Not 'namespace' token");
427f4a2713aSLionel Sambuc 
428f4a2713aSLionel Sambuc   // Eat 'namespace'.
429f4a2713aSLionel Sambuc   SourceLocation NamespcLoc = ConsumeToken();
430f4a2713aSLionel Sambuc 
431f4a2713aSLionel Sambuc   if (Tok.is(tok::code_completion)) {
432f4a2713aSLionel Sambuc     Actions.CodeCompleteUsingDirective(getCurScope());
433f4a2713aSLionel Sambuc     cutOffParsing();
434*0a6a1f1dSLionel Sambuc     return nullptr;
435f4a2713aSLionel Sambuc   }
436f4a2713aSLionel Sambuc 
437f4a2713aSLionel Sambuc   CXXScopeSpec SS;
438f4a2713aSLionel Sambuc   // Parse (optional) nested-name-specifier.
439f4a2713aSLionel Sambuc   ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
440f4a2713aSLionel Sambuc 
441*0a6a1f1dSLionel Sambuc   IdentifierInfo *NamespcName = nullptr;
442f4a2713aSLionel Sambuc   SourceLocation IdentLoc = SourceLocation();
443f4a2713aSLionel Sambuc 
444f4a2713aSLionel Sambuc   // Parse namespace-name.
445f4a2713aSLionel Sambuc   if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
446f4a2713aSLionel Sambuc     Diag(Tok, diag::err_expected_namespace_name);
447f4a2713aSLionel Sambuc     // If there was invalid namespace name, skip to end of decl, and eat ';'.
448f4a2713aSLionel Sambuc     SkipUntil(tok::semi);
449f4a2713aSLionel Sambuc     // FIXME: Are there cases, when we would like to call ActOnUsingDirective?
450*0a6a1f1dSLionel Sambuc     return nullptr;
451f4a2713aSLionel Sambuc   }
452f4a2713aSLionel Sambuc 
453f4a2713aSLionel Sambuc   // Parse identifier.
454f4a2713aSLionel Sambuc   NamespcName = Tok.getIdentifierInfo();
455f4a2713aSLionel Sambuc   IdentLoc = ConsumeToken();
456f4a2713aSLionel Sambuc 
457f4a2713aSLionel Sambuc   // Parse (optional) attributes (most likely GNU strong-using extension).
458f4a2713aSLionel Sambuc   bool GNUAttr = false;
459f4a2713aSLionel Sambuc   if (Tok.is(tok::kw___attribute)) {
460f4a2713aSLionel Sambuc     GNUAttr = true;
461f4a2713aSLionel Sambuc     ParseGNUAttributes(attrs);
462f4a2713aSLionel Sambuc   }
463f4a2713aSLionel Sambuc 
464f4a2713aSLionel Sambuc   // Eat ';'.
465f4a2713aSLionel Sambuc   DeclEnd = Tok.getLocation();
466*0a6a1f1dSLionel Sambuc   if (ExpectAndConsume(tok::semi,
467f4a2713aSLionel Sambuc                        GNUAttr ? diag::err_expected_semi_after_attribute_list
468*0a6a1f1dSLionel Sambuc                                : diag::err_expected_semi_after_namespace_name))
469*0a6a1f1dSLionel Sambuc     SkipUntil(tok::semi);
470f4a2713aSLionel Sambuc 
471f4a2713aSLionel Sambuc   return Actions.ActOnUsingDirective(getCurScope(), UsingLoc, NamespcLoc, SS,
472f4a2713aSLionel Sambuc                                      IdentLoc, NamespcName, attrs.getList());
473f4a2713aSLionel Sambuc }
474f4a2713aSLionel Sambuc 
475f4a2713aSLionel Sambuc /// ParseUsingDeclaration - Parse C++ using-declaration or alias-declaration.
476f4a2713aSLionel Sambuc /// Assumes that 'using' was already seen.
477f4a2713aSLionel Sambuc ///
478f4a2713aSLionel Sambuc ///     using-declaration: [C++ 7.3.p3: namespace.udecl]
479f4a2713aSLionel Sambuc ///       'using' 'typename'[opt] ::[opt] nested-name-specifier
480f4a2713aSLionel Sambuc ///               unqualified-id
481f4a2713aSLionel Sambuc ///       'using' :: unqualified-id
482f4a2713aSLionel Sambuc ///
483f4a2713aSLionel Sambuc ///     alias-declaration: C++11 [dcl.dcl]p1
484f4a2713aSLionel Sambuc ///       'using' identifier attribute-specifier-seq[opt] = type-id ;
485f4a2713aSLionel Sambuc ///
ParseUsingDeclaration(unsigned Context,const ParsedTemplateInfo & TemplateInfo,SourceLocation UsingLoc,SourceLocation & DeclEnd,AccessSpecifier AS,Decl ** OwnedType)486f4a2713aSLionel Sambuc Decl *Parser::ParseUsingDeclaration(unsigned Context,
487f4a2713aSLionel Sambuc                                     const ParsedTemplateInfo &TemplateInfo,
488f4a2713aSLionel Sambuc                                     SourceLocation UsingLoc,
489f4a2713aSLionel Sambuc                                     SourceLocation &DeclEnd,
490f4a2713aSLionel Sambuc                                     AccessSpecifier AS,
491f4a2713aSLionel Sambuc                                     Decl **OwnedType) {
492f4a2713aSLionel Sambuc   CXXScopeSpec SS;
493f4a2713aSLionel Sambuc   SourceLocation TypenameLoc;
494f4a2713aSLionel Sambuc   bool HasTypenameKeyword = false;
495f4a2713aSLionel Sambuc 
496f4a2713aSLionel Sambuc   // Check for misplaced attributes before the identifier in an
497f4a2713aSLionel Sambuc   // alias-declaration.
498f4a2713aSLionel Sambuc   ParsedAttributesWithRange MisplacedAttrs(AttrFactory);
499f4a2713aSLionel Sambuc   MaybeParseCXX11Attributes(MisplacedAttrs);
500f4a2713aSLionel Sambuc 
501f4a2713aSLionel Sambuc   // Ignore optional 'typename'.
502f4a2713aSLionel Sambuc   // FIXME: This is wrong; we should parse this as a typename-specifier.
503*0a6a1f1dSLionel Sambuc   if (TryConsumeToken(tok::kw_typename, TypenameLoc))
504f4a2713aSLionel Sambuc     HasTypenameKeyword = true;
505*0a6a1f1dSLionel Sambuc 
506*0a6a1f1dSLionel Sambuc   if (Tok.is(tok::kw___super)) {
507*0a6a1f1dSLionel Sambuc     Diag(Tok.getLocation(), diag::err_super_in_using_declaration);
508*0a6a1f1dSLionel Sambuc     SkipUntil(tok::semi);
509*0a6a1f1dSLionel Sambuc     return nullptr;
510f4a2713aSLionel Sambuc   }
511f4a2713aSLionel Sambuc 
512f4a2713aSLionel Sambuc   // Parse nested-name-specifier.
513*0a6a1f1dSLionel Sambuc   IdentifierInfo *LastII = nullptr;
514f4a2713aSLionel Sambuc   ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false,
515*0a6a1f1dSLionel Sambuc                                  /*MayBePseudoDtor=*/nullptr,
516*0a6a1f1dSLionel Sambuc                                  /*IsTypename=*/false,
517f4a2713aSLionel Sambuc                                  /*LastII=*/&LastII);
518f4a2713aSLionel Sambuc 
519f4a2713aSLionel Sambuc   // Check nested-name specifier.
520f4a2713aSLionel Sambuc   if (SS.isInvalid()) {
521f4a2713aSLionel Sambuc     SkipUntil(tok::semi);
522*0a6a1f1dSLionel Sambuc     return nullptr;
523f4a2713aSLionel Sambuc   }
524f4a2713aSLionel Sambuc 
525f4a2713aSLionel Sambuc   SourceLocation TemplateKWLoc;
526f4a2713aSLionel Sambuc   UnqualifiedId Name;
527f4a2713aSLionel Sambuc 
528f4a2713aSLionel Sambuc   // Parse the unqualified-id. We allow parsing of both constructor and
529f4a2713aSLionel Sambuc   // destructor names and allow the action module to diagnose any semantic
530f4a2713aSLionel Sambuc   // errors.
531f4a2713aSLionel Sambuc   //
532f4a2713aSLionel Sambuc   // C++11 [class.qual]p2:
533f4a2713aSLionel Sambuc   //   [...] in a using-declaration that is a member-declaration, if the name
534f4a2713aSLionel Sambuc   //   specified after the nested-name-specifier is the same as the identifier
535f4a2713aSLionel Sambuc   //   or the simple-template-id's template-name in the last component of the
536f4a2713aSLionel Sambuc   //   nested-name-specifier, the name is [...] considered to name the
537f4a2713aSLionel Sambuc   //   constructor.
538f4a2713aSLionel Sambuc   if (getLangOpts().CPlusPlus11 && Context == Declarator::MemberContext &&
539f4a2713aSLionel Sambuc       Tok.is(tok::identifier) && NextToken().is(tok::semi) &&
540f4a2713aSLionel Sambuc       SS.isNotEmpty() && LastII == Tok.getIdentifierInfo() &&
541f4a2713aSLionel Sambuc       !SS.getScopeRep()->getAsNamespace() &&
542f4a2713aSLionel Sambuc       !SS.getScopeRep()->getAsNamespaceAlias()) {
543f4a2713aSLionel Sambuc     SourceLocation IdLoc = ConsumeToken();
544f4a2713aSLionel Sambuc     ParsedType Type = Actions.getInheritingConstructorName(SS, IdLoc, *LastII);
545f4a2713aSLionel Sambuc     Name.setConstructorName(Type, IdLoc, IdLoc);
546f4a2713aSLionel Sambuc   } else if (ParseUnqualifiedId(SS, /*EnteringContext=*/ false,
547f4a2713aSLionel Sambuc                                 /*AllowDestructorName=*/ true,
548f4a2713aSLionel Sambuc                                 /*AllowConstructorName=*/ true, ParsedType(),
549f4a2713aSLionel Sambuc                                 TemplateKWLoc, Name)) {
550f4a2713aSLionel Sambuc     SkipUntil(tok::semi);
551*0a6a1f1dSLionel Sambuc     return nullptr;
552f4a2713aSLionel Sambuc   }
553f4a2713aSLionel Sambuc 
554f4a2713aSLionel Sambuc   ParsedAttributesWithRange Attrs(AttrFactory);
555f4a2713aSLionel Sambuc   MaybeParseGNUAttributes(Attrs);
556f4a2713aSLionel Sambuc   MaybeParseCXX11Attributes(Attrs);
557f4a2713aSLionel Sambuc 
558f4a2713aSLionel Sambuc   // Maybe this is an alias-declaration.
559f4a2713aSLionel Sambuc   TypeResult TypeAlias;
560f4a2713aSLionel Sambuc   bool IsAliasDecl = Tok.is(tok::equal);
561f4a2713aSLionel Sambuc   if (IsAliasDecl) {
562f4a2713aSLionel Sambuc     // If we had any misplaced attributes from earlier, this is where they
563f4a2713aSLionel Sambuc     // should have been written.
564f4a2713aSLionel Sambuc     if (MisplacedAttrs.Range.isValid()) {
565f4a2713aSLionel Sambuc       Diag(MisplacedAttrs.Range.getBegin(), diag::err_attributes_not_allowed)
566f4a2713aSLionel Sambuc         << FixItHint::CreateInsertionFromRange(
567f4a2713aSLionel Sambuc                Tok.getLocation(),
568f4a2713aSLionel Sambuc                CharSourceRange::getTokenRange(MisplacedAttrs.Range))
569f4a2713aSLionel Sambuc         << FixItHint::CreateRemoval(MisplacedAttrs.Range);
570f4a2713aSLionel Sambuc       Attrs.takeAllFrom(MisplacedAttrs);
571f4a2713aSLionel Sambuc     }
572f4a2713aSLionel Sambuc 
573f4a2713aSLionel Sambuc     ConsumeToken();
574f4a2713aSLionel Sambuc 
575f4a2713aSLionel Sambuc     Diag(Tok.getLocation(), getLangOpts().CPlusPlus11 ?
576f4a2713aSLionel Sambuc          diag::warn_cxx98_compat_alias_declaration :
577f4a2713aSLionel Sambuc          diag::ext_alias_declaration);
578f4a2713aSLionel Sambuc 
579f4a2713aSLionel Sambuc     // Type alias templates cannot be specialized.
580f4a2713aSLionel Sambuc     int SpecKind = -1;
581f4a2713aSLionel Sambuc     if (TemplateInfo.Kind == ParsedTemplateInfo::Template &&
582f4a2713aSLionel Sambuc         Name.getKind() == UnqualifiedId::IK_TemplateId)
583f4a2713aSLionel Sambuc       SpecKind = 0;
584f4a2713aSLionel Sambuc     if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization)
585f4a2713aSLionel Sambuc       SpecKind = 1;
586f4a2713aSLionel Sambuc     if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
587f4a2713aSLionel Sambuc       SpecKind = 2;
588f4a2713aSLionel Sambuc     if (SpecKind != -1) {
589f4a2713aSLionel Sambuc       SourceRange Range;
590f4a2713aSLionel Sambuc       if (SpecKind == 0)
591f4a2713aSLionel Sambuc         Range = SourceRange(Name.TemplateId->LAngleLoc,
592f4a2713aSLionel Sambuc                             Name.TemplateId->RAngleLoc);
593f4a2713aSLionel Sambuc       else
594f4a2713aSLionel Sambuc         Range = TemplateInfo.getSourceRange();
595f4a2713aSLionel Sambuc       Diag(Range.getBegin(), diag::err_alias_declaration_specialization)
596f4a2713aSLionel Sambuc         << SpecKind << Range;
597f4a2713aSLionel Sambuc       SkipUntil(tok::semi);
598*0a6a1f1dSLionel Sambuc       return nullptr;
599f4a2713aSLionel Sambuc     }
600f4a2713aSLionel Sambuc 
601f4a2713aSLionel Sambuc     // Name must be an identifier.
602f4a2713aSLionel Sambuc     if (Name.getKind() != UnqualifiedId::IK_Identifier) {
603f4a2713aSLionel Sambuc       Diag(Name.StartLocation, diag::err_alias_declaration_not_identifier);
604f4a2713aSLionel Sambuc       // No removal fixit: can't recover from this.
605f4a2713aSLionel Sambuc       SkipUntil(tok::semi);
606*0a6a1f1dSLionel Sambuc       return nullptr;
607f4a2713aSLionel Sambuc     } else if (HasTypenameKeyword)
608f4a2713aSLionel Sambuc       Diag(TypenameLoc, diag::err_alias_declaration_not_identifier)
609f4a2713aSLionel Sambuc         << FixItHint::CreateRemoval(SourceRange(TypenameLoc,
610f4a2713aSLionel Sambuc                              SS.isNotEmpty() ? SS.getEndLoc() : TypenameLoc));
611f4a2713aSLionel Sambuc     else if (SS.isNotEmpty())
612f4a2713aSLionel Sambuc       Diag(SS.getBeginLoc(), diag::err_alias_declaration_not_identifier)
613f4a2713aSLionel Sambuc         << FixItHint::CreateRemoval(SS.getRange());
614f4a2713aSLionel Sambuc 
615*0a6a1f1dSLionel Sambuc     TypeAlias = ParseTypeName(nullptr, TemplateInfo.Kind ?
616f4a2713aSLionel Sambuc                               Declarator::AliasTemplateContext :
617f4a2713aSLionel Sambuc                               Declarator::AliasDeclContext, AS, OwnedType,
618f4a2713aSLionel Sambuc                               &Attrs);
619f4a2713aSLionel Sambuc   } else {
620f4a2713aSLionel Sambuc     // C++11 attributes are not allowed on a using-declaration, but GNU ones
621f4a2713aSLionel Sambuc     // are.
622f4a2713aSLionel Sambuc     ProhibitAttributes(MisplacedAttrs);
623f4a2713aSLionel Sambuc     ProhibitAttributes(Attrs);
624f4a2713aSLionel Sambuc 
625f4a2713aSLionel Sambuc     // Parse (optional) attributes (most likely GNU strong-using extension).
626f4a2713aSLionel Sambuc     MaybeParseGNUAttributes(Attrs);
627f4a2713aSLionel Sambuc   }
628f4a2713aSLionel Sambuc 
629f4a2713aSLionel Sambuc   // Eat ';'.
630f4a2713aSLionel Sambuc   DeclEnd = Tok.getLocation();
631*0a6a1f1dSLionel Sambuc   if (ExpectAndConsume(tok::semi, diag::err_expected_after,
632*0a6a1f1dSLionel Sambuc                        !Attrs.empty() ? "attributes list"
633*0a6a1f1dSLionel Sambuc                                       : IsAliasDecl ? "alias declaration"
634*0a6a1f1dSLionel Sambuc                                                     : "using declaration"))
635*0a6a1f1dSLionel Sambuc     SkipUntil(tok::semi);
636f4a2713aSLionel Sambuc 
637f4a2713aSLionel Sambuc   // Diagnose an attempt to declare a templated using-declaration.
638f4a2713aSLionel Sambuc   // In C++11, alias-declarations can be templates:
639f4a2713aSLionel Sambuc   //   template <...> using id = type;
640f4a2713aSLionel Sambuc   if (TemplateInfo.Kind && !IsAliasDecl) {
641f4a2713aSLionel Sambuc     SourceRange R = TemplateInfo.getSourceRange();
642f4a2713aSLionel Sambuc     Diag(UsingLoc, diag::err_templated_using_declaration)
643f4a2713aSLionel Sambuc       << R << FixItHint::CreateRemoval(R);
644f4a2713aSLionel Sambuc 
645f4a2713aSLionel Sambuc     // Unfortunately, we have to bail out instead of recovering by
646f4a2713aSLionel Sambuc     // ignoring the parameters, just in case the nested name specifier
647f4a2713aSLionel Sambuc     // depends on the parameters.
648*0a6a1f1dSLionel Sambuc     return nullptr;
649f4a2713aSLionel Sambuc   }
650f4a2713aSLionel Sambuc 
651f4a2713aSLionel Sambuc   // "typename" keyword is allowed for identifiers only,
652f4a2713aSLionel Sambuc   // because it may be a type definition.
653f4a2713aSLionel Sambuc   if (HasTypenameKeyword && Name.getKind() != UnqualifiedId::IK_Identifier) {
654f4a2713aSLionel Sambuc     Diag(Name.getSourceRange().getBegin(), diag::err_typename_identifiers_only)
655f4a2713aSLionel Sambuc       << FixItHint::CreateRemoval(SourceRange(TypenameLoc));
656f4a2713aSLionel Sambuc     // Proceed parsing, but reset the HasTypenameKeyword flag.
657f4a2713aSLionel Sambuc     HasTypenameKeyword = false;
658f4a2713aSLionel Sambuc   }
659f4a2713aSLionel Sambuc 
660f4a2713aSLionel Sambuc   if (IsAliasDecl) {
661f4a2713aSLionel Sambuc     TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
662f4a2713aSLionel Sambuc     MultiTemplateParamsArg TemplateParamsArg(
663*0a6a1f1dSLionel Sambuc       TemplateParams ? TemplateParams->data() : nullptr,
664f4a2713aSLionel Sambuc       TemplateParams ? TemplateParams->size() : 0);
665f4a2713aSLionel Sambuc     return Actions.ActOnAliasDeclaration(getCurScope(), AS, TemplateParamsArg,
666f4a2713aSLionel Sambuc                                          UsingLoc, Name, Attrs.getList(),
667f4a2713aSLionel Sambuc                                          TypeAlias);
668f4a2713aSLionel Sambuc   }
669f4a2713aSLionel Sambuc 
670f4a2713aSLionel Sambuc   return Actions.ActOnUsingDeclaration(getCurScope(), AS,
671f4a2713aSLionel Sambuc                                        /* HasUsingKeyword */ true, UsingLoc,
672f4a2713aSLionel Sambuc                                        SS, Name, Attrs.getList(),
673f4a2713aSLionel Sambuc                                        HasTypenameKeyword, TypenameLoc);
674f4a2713aSLionel Sambuc }
675f4a2713aSLionel Sambuc 
676f4a2713aSLionel Sambuc /// ParseStaticAssertDeclaration - Parse C++0x or C11 static_assert-declaration.
677f4a2713aSLionel Sambuc ///
678f4a2713aSLionel Sambuc /// [C++0x] static_assert-declaration:
679f4a2713aSLionel Sambuc ///           static_assert ( constant-expression  ,  string-literal  ) ;
680f4a2713aSLionel Sambuc ///
681f4a2713aSLionel Sambuc /// [C11]   static_assert-declaration:
682f4a2713aSLionel Sambuc ///           _Static_assert ( constant-expression  ,  string-literal  ) ;
683f4a2713aSLionel Sambuc ///
ParseStaticAssertDeclaration(SourceLocation & DeclEnd)684f4a2713aSLionel Sambuc Decl *Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){
685f4a2713aSLionel Sambuc   assert((Tok.is(tok::kw_static_assert) || Tok.is(tok::kw__Static_assert)) &&
686f4a2713aSLionel Sambuc          "Not a static_assert declaration");
687f4a2713aSLionel Sambuc 
688f4a2713aSLionel Sambuc   if (Tok.is(tok::kw__Static_assert) && !getLangOpts().C11)
689f4a2713aSLionel Sambuc     Diag(Tok, diag::ext_c11_static_assert);
690f4a2713aSLionel Sambuc   if (Tok.is(tok::kw_static_assert))
691f4a2713aSLionel Sambuc     Diag(Tok, diag::warn_cxx98_compat_static_assert);
692f4a2713aSLionel Sambuc 
693f4a2713aSLionel Sambuc   SourceLocation StaticAssertLoc = ConsumeToken();
694f4a2713aSLionel Sambuc 
695f4a2713aSLionel Sambuc   BalancedDelimiterTracker T(*this, tok::l_paren);
696f4a2713aSLionel Sambuc   if (T.consumeOpen()) {
697*0a6a1f1dSLionel Sambuc     Diag(Tok, diag::err_expected) << tok::l_paren;
698f4a2713aSLionel Sambuc     SkipMalformedDecl();
699*0a6a1f1dSLionel Sambuc     return nullptr;
700f4a2713aSLionel Sambuc   }
701f4a2713aSLionel Sambuc 
702f4a2713aSLionel Sambuc   ExprResult AssertExpr(ParseConstantExpression());
703f4a2713aSLionel Sambuc   if (AssertExpr.isInvalid()) {
704f4a2713aSLionel Sambuc     SkipMalformedDecl();
705*0a6a1f1dSLionel Sambuc     return nullptr;
706f4a2713aSLionel Sambuc   }
707f4a2713aSLionel Sambuc 
708*0a6a1f1dSLionel Sambuc   ExprResult AssertMessage;
709*0a6a1f1dSLionel Sambuc   if (Tok.is(tok::r_paren)) {
710*0a6a1f1dSLionel Sambuc     Diag(Tok, getLangOpts().CPlusPlus1z
711*0a6a1f1dSLionel Sambuc                   ? diag::warn_cxx14_compat_static_assert_no_message
712*0a6a1f1dSLionel Sambuc                   : diag::ext_static_assert_no_message)
713*0a6a1f1dSLionel Sambuc       << (getLangOpts().CPlusPlus1z
714*0a6a1f1dSLionel Sambuc               ? FixItHint()
715*0a6a1f1dSLionel Sambuc               : FixItHint::CreateInsertion(Tok.getLocation(), ", \"\""));
716*0a6a1f1dSLionel Sambuc   } else {
717*0a6a1f1dSLionel Sambuc     if (ExpectAndConsume(tok::comma)) {
718*0a6a1f1dSLionel Sambuc       SkipUntil(tok::semi);
719*0a6a1f1dSLionel Sambuc       return nullptr;
720*0a6a1f1dSLionel Sambuc     }
721f4a2713aSLionel Sambuc 
722f4a2713aSLionel Sambuc     if (!isTokenStringLiteral()) {
723f4a2713aSLionel Sambuc       Diag(Tok, diag::err_expected_string_literal)
724f4a2713aSLionel Sambuc         << /*Source='static_assert'*/1;
725f4a2713aSLionel Sambuc       SkipMalformedDecl();
726*0a6a1f1dSLionel Sambuc       return nullptr;
727f4a2713aSLionel Sambuc     }
728f4a2713aSLionel Sambuc 
729*0a6a1f1dSLionel Sambuc     AssertMessage = ParseStringLiteralExpression();
730f4a2713aSLionel Sambuc     if (AssertMessage.isInvalid()) {
731f4a2713aSLionel Sambuc       SkipMalformedDecl();
732*0a6a1f1dSLionel Sambuc       return nullptr;
733*0a6a1f1dSLionel Sambuc     }
734f4a2713aSLionel Sambuc   }
735f4a2713aSLionel Sambuc 
736f4a2713aSLionel Sambuc   T.consumeClose();
737f4a2713aSLionel Sambuc 
738f4a2713aSLionel Sambuc   DeclEnd = Tok.getLocation();
739f4a2713aSLionel Sambuc   ExpectAndConsumeSemi(diag::err_expected_semi_after_static_assert);
740f4a2713aSLionel Sambuc 
741f4a2713aSLionel Sambuc   return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc,
742*0a6a1f1dSLionel Sambuc                                               AssertExpr.get(),
743*0a6a1f1dSLionel Sambuc                                               AssertMessage.get(),
744f4a2713aSLionel Sambuc                                               T.getCloseLocation());
745f4a2713aSLionel Sambuc }
746f4a2713aSLionel Sambuc 
747f4a2713aSLionel Sambuc /// ParseDecltypeSpecifier - Parse a C++11 decltype specifier.
748f4a2713aSLionel Sambuc ///
749f4a2713aSLionel Sambuc /// 'decltype' ( expression )
750f4a2713aSLionel Sambuc /// 'decltype' ( 'auto' )      [C++1y]
751f4a2713aSLionel Sambuc ///
ParseDecltypeSpecifier(DeclSpec & DS)752f4a2713aSLionel Sambuc SourceLocation Parser::ParseDecltypeSpecifier(DeclSpec &DS) {
753f4a2713aSLionel Sambuc   assert((Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype))
754f4a2713aSLionel Sambuc            && "Not a decltype specifier");
755f4a2713aSLionel Sambuc 
756f4a2713aSLionel Sambuc   ExprResult Result;
757f4a2713aSLionel Sambuc   SourceLocation StartLoc = Tok.getLocation();
758f4a2713aSLionel Sambuc   SourceLocation EndLoc;
759f4a2713aSLionel Sambuc 
760f4a2713aSLionel Sambuc   if (Tok.is(tok::annot_decltype)) {
761f4a2713aSLionel Sambuc     Result = getExprAnnotation(Tok);
762f4a2713aSLionel Sambuc     EndLoc = Tok.getAnnotationEndLoc();
763f4a2713aSLionel Sambuc     ConsumeToken();
764f4a2713aSLionel Sambuc     if (Result.isInvalid()) {
765f4a2713aSLionel Sambuc       DS.SetTypeSpecError();
766f4a2713aSLionel Sambuc       return EndLoc;
767f4a2713aSLionel Sambuc     }
768f4a2713aSLionel Sambuc   } else {
769f4a2713aSLionel Sambuc     if (Tok.getIdentifierInfo()->isStr("decltype"))
770f4a2713aSLionel Sambuc       Diag(Tok, diag::warn_cxx98_compat_decltype);
771f4a2713aSLionel Sambuc 
772f4a2713aSLionel Sambuc     ConsumeToken();
773f4a2713aSLionel Sambuc 
774f4a2713aSLionel Sambuc     BalancedDelimiterTracker T(*this, tok::l_paren);
775f4a2713aSLionel Sambuc     if (T.expectAndConsume(diag::err_expected_lparen_after,
776f4a2713aSLionel Sambuc                            "decltype", tok::r_paren)) {
777f4a2713aSLionel Sambuc       DS.SetTypeSpecError();
778f4a2713aSLionel Sambuc       return T.getOpenLocation() == Tok.getLocation() ?
779f4a2713aSLionel Sambuc              StartLoc : T.getOpenLocation();
780f4a2713aSLionel Sambuc     }
781f4a2713aSLionel Sambuc 
782f4a2713aSLionel Sambuc     // Check for C++1y 'decltype(auto)'.
783f4a2713aSLionel Sambuc     if (Tok.is(tok::kw_auto)) {
784f4a2713aSLionel Sambuc       // No need to disambiguate here: an expression can't start with 'auto',
785f4a2713aSLionel Sambuc       // because the typename-specifier in a function-style cast operation can't
786f4a2713aSLionel Sambuc       // be 'auto'.
787f4a2713aSLionel Sambuc       Diag(Tok.getLocation(),
788*0a6a1f1dSLionel Sambuc            getLangOpts().CPlusPlus14
789f4a2713aSLionel Sambuc              ? diag::warn_cxx11_compat_decltype_auto_type_specifier
790f4a2713aSLionel Sambuc              : diag::ext_decltype_auto_type_specifier);
791f4a2713aSLionel Sambuc       ConsumeToken();
792f4a2713aSLionel Sambuc     } else {
793f4a2713aSLionel Sambuc       // Parse the expression
794f4a2713aSLionel Sambuc 
795f4a2713aSLionel Sambuc       // C++11 [dcl.type.simple]p4:
796f4a2713aSLionel Sambuc       //   The operand of the decltype specifier is an unevaluated operand.
797f4a2713aSLionel Sambuc       EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
798*0a6a1f1dSLionel Sambuc                                                    nullptr,/*IsDecltype=*/true);
799*0a6a1f1dSLionel Sambuc       Result = Actions.CorrectDelayedTyposInExpr(ParseExpression());
800f4a2713aSLionel Sambuc       if (Result.isInvalid()) {
801f4a2713aSLionel Sambuc         DS.SetTypeSpecError();
802f4a2713aSLionel Sambuc         if (SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch)) {
803f4a2713aSLionel Sambuc           EndLoc = ConsumeParen();
804f4a2713aSLionel Sambuc         } else {
805f4a2713aSLionel Sambuc           if (PP.isBacktrackEnabled() && Tok.is(tok::semi)) {
806f4a2713aSLionel Sambuc             // Backtrack to get the location of the last token before the semi.
807f4a2713aSLionel Sambuc             PP.RevertCachedTokens(2);
808f4a2713aSLionel Sambuc             ConsumeToken(); // the semi.
809f4a2713aSLionel Sambuc             EndLoc = ConsumeAnyToken();
810f4a2713aSLionel Sambuc             assert(Tok.is(tok::semi));
811f4a2713aSLionel Sambuc           } else {
812f4a2713aSLionel Sambuc             EndLoc = Tok.getLocation();
813f4a2713aSLionel Sambuc           }
814f4a2713aSLionel Sambuc         }
815f4a2713aSLionel Sambuc         return EndLoc;
816f4a2713aSLionel Sambuc       }
817f4a2713aSLionel Sambuc 
818*0a6a1f1dSLionel Sambuc       Result = Actions.ActOnDecltypeExpression(Result.get());
819f4a2713aSLionel Sambuc     }
820f4a2713aSLionel Sambuc 
821f4a2713aSLionel Sambuc     // Match the ')'
822f4a2713aSLionel Sambuc     T.consumeClose();
823f4a2713aSLionel Sambuc     if (T.getCloseLocation().isInvalid()) {
824f4a2713aSLionel Sambuc       DS.SetTypeSpecError();
825f4a2713aSLionel Sambuc       // FIXME: this should return the location of the last token
826f4a2713aSLionel Sambuc       //        that was consumed (by "consumeClose()")
827f4a2713aSLionel Sambuc       return T.getCloseLocation();
828f4a2713aSLionel Sambuc     }
829f4a2713aSLionel Sambuc 
830f4a2713aSLionel Sambuc     if (Result.isInvalid()) {
831f4a2713aSLionel Sambuc       DS.SetTypeSpecError();
832f4a2713aSLionel Sambuc       return T.getCloseLocation();
833f4a2713aSLionel Sambuc     }
834f4a2713aSLionel Sambuc 
835f4a2713aSLionel Sambuc     EndLoc = T.getCloseLocation();
836f4a2713aSLionel Sambuc   }
837f4a2713aSLionel Sambuc   assert(!Result.isInvalid());
838f4a2713aSLionel Sambuc 
839*0a6a1f1dSLionel Sambuc   const char *PrevSpec = nullptr;
840f4a2713aSLionel Sambuc   unsigned DiagID;
841*0a6a1f1dSLionel Sambuc   const PrintingPolicy &Policy = Actions.getASTContext().getPrintingPolicy();
842f4a2713aSLionel Sambuc   // Check for duplicate type specifiers (e.g. "int decltype(a)").
843f4a2713aSLionel Sambuc   if (Result.get()
844f4a2713aSLionel Sambuc         ? DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec,
845*0a6a1f1dSLionel Sambuc                              DiagID, Result.get(), Policy)
846f4a2713aSLionel Sambuc         : DS.SetTypeSpecType(DeclSpec::TST_decltype_auto, StartLoc, PrevSpec,
847*0a6a1f1dSLionel Sambuc                              DiagID, Policy)) {
848f4a2713aSLionel Sambuc     Diag(StartLoc, DiagID) << PrevSpec;
849f4a2713aSLionel Sambuc     DS.SetTypeSpecError();
850f4a2713aSLionel Sambuc   }
851f4a2713aSLionel Sambuc   return EndLoc;
852f4a2713aSLionel Sambuc }
853f4a2713aSLionel Sambuc 
AnnotateExistingDecltypeSpecifier(const DeclSpec & DS,SourceLocation StartLoc,SourceLocation EndLoc)854f4a2713aSLionel Sambuc void Parser::AnnotateExistingDecltypeSpecifier(const DeclSpec& DS,
855f4a2713aSLionel Sambuc                                                SourceLocation StartLoc,
856f4a2713aSLionel Sambuc                                                SourceLocation EndLoc) {
857f4a2713aSLionel Sambuc   // make sure we have a token we can turn into an annotation token
858f4a2713aSLionel Sambuc   if (PP.isBacktrackEnabled())
859f4a2713aSLionel Sambuc     PP.RevertCachedTokens(1);
860f4a2713aSLionel Sambuc   else
861f4a2713aSLionel Sambuc     PP.EnterToken(Tok);
862f4a2713aSLionel Sambuc 
863f4a2713aSLionel Sambuc   Tok.setKind(tok::annot_decltype);
864f4a2713aSLionel Sambuc   setExprAnnotation(Tok,
865f4a2713aSLionel Sambuc                     DS.getTypeSpecType() == TST_decltype ? DS.getRepAsExpr() :
866f4a2713aSLionel Sambuc                     DS.getTypeSpecType() == TST_decltype_auto ? ExprResult() :
867f4a2713aSLionel Sambuc                     ExprError());
868f4a2713aSLionel Sambuc   Tok.setAnnotationEndLoc(EndLoc);
869f4a2713aSLionel Sambuc   Tok.setLocation(StartLoc);
870f4a2713aSLionel Sambuc   PP.AnnotateCachedTokens(Tok);
871f4a2713aSLionel Sambuc }
872f4a2713aSLionel Sambuc 
ParseUnderlyingTypeSpecifier(DeclSpec & DS)873f4a2713aSLionel Sambuc void Parser::ParseUnderlyingTypeSpecifier(DeclSpec &DS) {
874f4a2713aSLionel Sambuc   assert(Tok.is(tok::kw___underlying_type) &&
875f4a2713aSLionel Sambuc          "Not an underlying type specifier");
876f4a2713aSLionel Sambuc 
877f4a2713aSLionel Sambuc   SourceLocation StartLoc = ConsumeToken();
878f4a2713aSLionel Sambuc   BalancedDelimiterTracker T(*this, tok::l_paren);
879f4a2713aSLionel Sambuc   if (T.expectAndConsume(diag::err_expected_lparen_after,
880f4a2713aSLionel Sambuc                        "__underlying_type", tok::r_paren)) {
881f4a2713aSLionel Sambuc     return;
882f4a2713aSLionel Sambuc   }
883f4a2713aSLionel Sambuc 
884f4a2713aSLionel Sambuc   TypeResult Result = ParseTypeName();
885f4a2713aSLionel Sambuc   if (Result.isInvalid()) {
886f4a2713aSLionel Sambuc     SkipUntil(tok::r_paren, StopAtSemi);
887f4a2713aSLionel Sambuc     return;
888f4a2713aSLionel Sambuc   }
889f4a2713aSLionel Sambuc 
890f4a2713aSLionel Sambuc   // Match the ')'
891f4a2713aSLionel Sambuc   T.consumeClose();
892f4a2713aSLionel Sambuc   if (T.getCloseLocation().isInvalid())
893f4a2713aSLionel Sambuc     return;
894f4a2713aSLionel Sambuc 
895*0a6a1f1dSLionel Sambuc   const char *PrevSpec = nullptr;
896f4a2713aSLionel Sambuc   unsigned DiagID;
897f4a2713aSLionel Sambuc   if (DS.SetTypeSpecType(DeclSpec::TST_underlyingType, StartLoc, PrevSpec,
898*0a6a1f1dSLionel Sambuc                          DiagID, Result.get(),
899*0a6a1f1dSLionel Sambuc                          Actions.getASTContext().getPrintingPolicy()))
900f4a2713aSLionel Sambuc     Diag(StartLoc, DiagID) << PrevSpec;
901f4a2713aSLionel Sambuc   DS.setTypeofParensRange(T.getRange());
902f4a2713aSLionel Sambuc }
903f4a2713aSLionel Sambuc 
904f4a2713aSLionel Sambuc /// ParseBaseTypeSpecifier - Parse a C++ base-type-specifier which is either a
905f4a2713aSLionel Sambuc /// class name or decltype-specifier. Note that we only check that the result
906f4a2713aSLionel Sambuc /// names a type; semantic analysis will need to verify that the type names a
907f4a2713aSLionel Sambuc /// class. The result is either a type or null, depending on whether a type
908f4a2713aSLionel Sambuc /// name was found.
909f4a2713aSLionel Sambuc ///
910f4a2713aSLionel Sambuc ///       base-type-specifier: [C++11 class.derived]
911f4a2713aSLionel Sambuc ///         class-or-decltype
912f4a2713aSLionel Sambuc ///       class-or-decltype: [C++11 class.derived]
913f4a2713aSLionel Sambuc ///         nested-name-specifier[opt] class-name
914f4a2713aSLionel Sambuc ///         decltype-specifier
915f4a2713aSLionel Sambuc ///       class-name: [C++ class.name]
916f4a2713aSLionel Sambuc ///         identifier
917f4a2713aSLionel Sambuc ///         simple-template-id
918f4a2713aSLionel Sambuc ///
919f4a2713aSLionel Sambuc /// In C++98, instead of base-type-specifier, we have:
920f4a2713aSLionel Sambuc ///
921f4a2713aSLionel Sambuc ///         ::[opt] nested-name-specifier[opt] class-name
ParseBaseTypeSpecifier(SourceLocation & BaseLoc,SourceLocation & EndLocation)922*0a6a1f1dSLionel Sambuc TypeResult Parser::ParseBaseTypeSpecifier(SourceLocation &BaseLoc,
923f4a2713aSLionel Sambuc                                           SourceLocation &EndLocation) {
924f4a2713aSLionel Sambuc   // Ignore attempts to use typename
925f4a2713aSLionel Sambuc   if (Tok.is(tok::kw_typename)) {
926f4a2713aSLionel Sambuc     Diag(Tok, diag::err_expected_class_name_not_template)
927f4a2713aSLionel Sambuc       << FixItHint::CreateRemoval(Tok.getLocation());
928f4a2713aSLionel Sambuc     ConsumeToken();
929f4a2713aSLionel Sambuc   }
930f4a2713aSLionel Sambuc 
931f4a2713aSLionel Sambuc   // Parse optional nested-name-specifier
932f4a2713aSLionel Sambuc   CXXScopeSpec SS;
933f4a2713aSLionel Sambuc   ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
934f4a2713aSLionel Sambuc 
935f4a2713aSLionel Sambuc   BaseLoc = Tok.getLocation();
936f4a2713aSLionel Sambuc 
937f4a2713aSLionel Sambuc   // Parse decltype-specifier
938f4a2713aSLionel Sambuc   // tok == kw_decltype is just error recovery, it can only happen when SS
939f4a2713aSLionel Sambuc   // isn't empty
940f4a2713aSLionel Sambuc   if (Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype)) {
941f4a2713aSLionel Sambuc     if (SS.isNotEmpty())
942f4a2713aSLionel Sambuc       Diag(SS.getBeginLoc(), diag::err_unexpected_scope_on_base_decltype)
943f4a2713aSLionel Sambuc         << FixItHint::CreateRemoval(SS.getRange());
944f4a2713aSLionel Sambuc     // Fake up a Declarator to use with ActOnTypeName.
945f4a2713aSLionel Sambuc     DeclSpec DS(AttrFactory);
946f4a2713aSLionel Sambuc 
947f4a2713aSLionel Sambuc     EndLocation = ParseDecltypeSpecifier(DS);
948f4a2713aSLionel Sambuc 
949f4a2713aSLionel Sambuc     Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
950f4a2713aSLionel Sambuc     return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
951f4a2713aSLionel Sambuc   }
952f4a2713aSLionel Sambuc 
953f4a2713aSLionel Sambuc   // Check whether we have a template-id that names a type.
954f4a2713aSLionel Sambuc   if (Tok.is(tok::annot_template_id)) {
955f4a2713aSLionel Sambuc     TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
956f4a2713aSLionel Sambuc     if (TemplateId->Kind == TNK_Type_template ||
957f4a2713aSLionel Sambuc         TemplateId->Kind == TNK_Dependent_template_name) {
958f4a2713aSLionel Sambuc       AnnotateTemplateIdTokenAsType();
959f4a2713aSLionel Sambuc 
960f4a2713aSLionel Sambuc       assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
961f4a2713aSLionel Sambuc       ParsedType Type = getTypeAnnotation(Tok);
962f4a2713aSLionel Sambuc       EndLocation = Tok.getAnnotationEndLoc();
963f4a2713aSLionel Sambuc       ConsumeToken();
964f4a2713aSLionel Sambuc 
965f4a2713aSLionel Sambuc       if (Type)
966f4a2713aSLionel Sambuc         return Type;
967f4a2713aSLionel Sambuc       return true;
968f4a2713aSLionel Sambuc     }
969f4a2713aSLionel Sambuc 
970f4a2713aSLionel Sambuc     // Fall through to produce an error below.
971f4a2713aSLionel Sambuc   }
972f4a2713aSLionel Sambuc 
973f4a2713aSLionel Sambuc   if (Tok.isNot(tok::identifier)) {
974f4a2713aSLionel Sambuc     Diag(Tok, diag::err_expected_class_name);
975f4a2713aSLionel Sambuc     return true;
976f4a2713aSLionel Sambuc   }
977f4a2713aSLionel Sambuc 
978f4a2713aSLionel Sambuc   IdentifierInfo *Id = Tok.getIdentifierInfo();
979f4a2713aSLionel Sambuc   SourceLocation IdLoc = ConsumeToken();
980f4a2713aSLionel Sambuc 
981f4a2713aSLionel Sambuc   if (Tok.is(tok::less)) {
982f4a2713aSLionel Sambuc     // It looks the user intended to write a template-id here, but the
983f4a2713aSLionel Sambuc     // template-name was wrong. Try to fix that.
984f4a2713aSLionel Sambuc     TemplateNameKind TNK = TNK_Type_template;
985f4a2713aSLionel Sambuc     TemplateTy Template;
986f4a2713aSLionel Sambuc     if (!Actions.DiagnoseUnknownTemplateName(*Id, IdLoc, getCurScope(),
987f4a2713aSLionel Sambuc                                              &SS, Template, TNK)) {
988f4a2713aSLionel Sambuc       Diag(IdLoc, diag::err_unknown_template_name)
989f4a2713aSLionel Sambuc         << Id;
990f4a2713aSLionel Sambuc     }
991f4a2713aSLionel Sambuc 
992f4a2713aSLionel Sambuc     if (!Template) {
993f4a2713aSLionel Sambuc       TemplateArgList TemplateArgs;
994f4a2713aSLionel Sambuc       SourceLocation LAngleLoc, RAngleLoc;
995f4a2713aSLionel Sambuc       ParseTemplateIdAfterTemplateName(TemplateTy(), IdLoc, SS,
996f4a2713aSLionel Sambuc           true, LAngleLoc, TemplateArgs, RAngleLoc);
997f4a2713aSLionel Sambuc       return true;
998f4a2713aSLionel Sambuc     }
999f4a2713aSLionel Sambuc 
1000f4a2713aSLionel Sambuc     // Form the template name
1001f4a2713aSLionel Sambuc     UnqualifiedId TemplateName;
1002f4a2713aSLionel Sambuc     TemplateName.setIdentifier(Id, IdLoc);
1003f4a2713aSLionel Sambuc 
1004f4a2713aSLionel Sambuc     // Parse the full template-id, then turn it into a type.
1005f4a2713aSLionel Sambuc     if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
1006f4a2713aSLionel Sambuc                                 TemplateName, true))
1007f4a2713aSLionel Sambuc       return true;
1008f4a2713aSLionel Sambuc     if (TNK == TNK_Dependent_template_name)
1009f4a2713aSLionel Sambuc       AnnotateTemplateIdTokenAsType();
1010f4a2713aSLionel Sambuc 
1011f4a2713aSLionel Sambuc     // If we didn't end up with a typename token, there's nothing more we
1012f4a2713aSLionel Sambuc     // can do.
1013f4a2713aSLionel Sambuc     if (Tok.isNot(tok::annot_typename))
1014f4a2713aSLionel Sambuc       return true;
1015f4a2713aSLionel Sambuc 
1016f4a2713aSLionel Sambuc     // Retrieve the type from the annotation token, consume that token, and
1017f4a2713aSLionel Sambuc     // return.
1018f4a2713aSLionel Sambuc     EndLocation = Tok.getAnnotationEndLoc();
1019f4a2713aSLionel Sambuc     ParsedType Type = getTypeAnnotation(Tok);
1020f4a2713aSLionel Sambuc     ConsumeToken();
1021f4a2713aSLionel Sambuc     return Type;
1022f4a2713aSLionel Sambuc   }
1023f4a2713aSLionel Sambuc 
1024f4a2713aSLionel Sambuc   // We have an identifier; check whether it is actually a type.
1025*0a6a1f1dSLionel Sambuc   IdentifierInfo *CorrectedII = nullptr;
1026f4a2713aSLionel Sambuc   ParsedType Type = Actions.getTypeName(*Id, IdLoc, getCurScope(), &SS, true,
1027f4a2713aSLionel Sambuc                                         false, ParsedType(),
1028f4a2713aSLionel Sambuc                                         /*IsCtorOrDtorName=*/false,
1029f4a2713aSLionel Sambuc                                         /*NonTrivialTypeSourceInfo=*/true,
1030f4a2713aSLionel Sambuc                                         &CorrectedII);
1031f4a2713aSLionel Sambuc   if (!Type) {
1032f4a2713aSLionel Sambuc     Diag(IdLoc, diag::err_expected_class_name);
1033f4a2713aSLionel Sambuc     return true;
1034f4a2713aSLionel Sambuc   }
1035f4a2713aSLionel Sambuc 
1036f4a2713aSLionel Sambuc   // Consume the identifier.
1037f4a2713aSLionel Sambuc   EndLocation = IdLoc;
1038f4a2713aSLionel Sambuc 
1039f4a2713aSLionel Sambuc   // Fake up a Declarator to use with ActOnTypeName.
1040f4a2713aSLionel Sambuc   DeclSpec DS(AttrFactory);
1041f4a2713aSLionel Sambuc   DS.SetRangeStart(IdLoc);
1042f4a2713aSLionel Sambuc   DS.SetRangeEnd(EndLocation);
1043f4a2713aSLionel Sambuc   DS.getTypeSpecScope() = SS;
1044f4a2713aSLionel Sambuc 
1045*0a6a1f1dSLionel Sambuc   const char *PrevSpec = nullptr;
1046f4a2713aSLionel Sambuc   unsigned DiagID;
1047*0a6a1f1dSLionel Sambuc   DS.SetTypeSpecType(TST_typename, IdLoc, PrevSpec, DiagID, Type,
1048*0a6a1f1dSLionel Sambuc                      Actions.getASTContext().getPrintingPolicy());
1049f4a2713aSLionel Sambuc 
1050f4a2713aSLionel Sambuc   Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
1051f4a2713aSLionel Sambuc   return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
1052f4a2713aSLionel Sambuc }
1053f4a2713aSLionel Sambuc 
ParseMicrosoftInheritanceClassAttributes(ParsedAttributes & attrs)1054f4a2713aSLionel Sambuc void Parser::ParseMicrosoftInheritanceClassAttributes(ParsedAttributes &attrs) {
1055f4a2713aSLionel Sambuc   while (Tok.is(tok::kw___single_inheritance) ||
1056f4a2713aSLionel Sambuc          Tok.is(tok::kw___multiple_inheritance) ||
1057f4a2713aSLionel Sambuc          Tok.is(tok::kw___virtual_inheritance)) {
1058f4a2713aSLionel Sambuc     IdentifierInfo *AttrName = Tok.getIdentifierInfo();
1059f4a2713aSLionel Sambuc     SourceLocation AttrNameLoc = ConsumeToken();
1060*0a6a1f1dSLionel Sambuc     attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0,
1061*0a6a1f1dSLionel Sambuc                  AttributeList::AS_Keyword);
1062f4a2713aSLionel Sambuc   }
1063f4a2713aSLionel Sambuc }
1064f4a2713aSLionel Sambuc 
1065f4a2713aSLionel Sambuc /// Determine whether the following tokens are valid after a type-specifier
1066f4a2713aSLionel Sambuc /// which could be a standalone declaration. This will conservatively return
1067f4a2713aSLionel Sambuc /// true if there's any doubt, and is appropriate for insert-';' fixits.
isValidAfterTypeSpecifier(bool CouldBeBitfield)1068f4a2713aSLionel Sambuc bool Parser::isValidAfterTypeSpecifier(bool CouldBeBitfield) {
1069f4a2713aSLionel Sambuc   // This switch enumerates the valid "follow" set for type-specifiers.
1070f4a2713aSLionel Sambuc   switch (Tok.getKind()) {
1071f4a2713aSLionel Sambuc   default: break;
1072f4a2713aSLionel Sambuc   case tok::semi:               // struct foo {...} ;
1073f4a2713aSLionel Sambuc   case tok::star:               // struct foo {...} *         P;
1074f4a2713aSLionel Sambuc   case tok::amp:                // struct foo {...} &         R = ...
1075f4a2713aSLionel Sambuc   case tok::ampamp:             // struct foo {...} &&        R = ...
1076f4a2713aSLionel Sambuc   case tok::identifier:         // struct foo {...} V         ;
1077f4a2713aSLionel Sambuc   case tok::r_paren:            //(struct foo {...} )         {4}
1078f4a2713aSLionel Sambuc   case tok::annot_cxxscope:     // struct foo {...} a::       b;
1079f4a2713aSLionel Sambuc   case tok::annot_typename:     // struct foo {...} a         ::b;
1080f4a2713aSLionel Sambuc   case tok::annot_template_id:  // struct foo {...} a<int>    ::b;
1081f4a2713aSLionel Sambuc   case tok::l_paren:            // struct foo {...} (         x);
1082f4a2713aSLionel Sambuc   case tok::comma:              // __builtin_offsetof(struct foo{...} ,
1083f4a2713aSLionel Sambuc   case tok::kw_operator:        // struct foo       operator  ++() {...}
1084*0a6a1f1dSLionel Sambuc   case tok::kw___declspec:      // struct foo {...} __declspec(...)
1085*0a6a1f1dSLionel Sambuc   case tok::l_square:           // void f(struct f  [         3])
1086*0a6a1f1dSLionel Sambuc   case tok::ellipsis:           // void f(struct f  ...       [Ns])
1087*0a6a1f1dSLionel Sambuc   // FIXME: we should emit semantic diagnostic when declaration
1088*0a6a1f1dSLionel Sambuc   // attribute is in type attribute position.
1089*0a6a1f1dSLionel Sambuc   case tok::kw___attribute:     // struct foo __attribute__((used)) x;
1090f4a2713aSLionel Sambuc     return true;
1091f4a2713aSLionel Sambuc   case tok::colon:
1092f4a2713aSLionel Sambuc     return CouldBeBitfield;     // enum E { ... }   :         2;
1093f4a2713aSLionel Sambuc   // Type qualifiers
1094f4a2713aSLionel Sambuc   case tok::kw_const:           // struct foo {...} const     x;
1095f4a2713aSLionel Sambuc   case tok::kw_volatile:        // struct foo {...} volatile  x;
1096f4a2713aSLionel Sambuc   case tok::kw_restrict:        // struct foo {...} restrict  x;
1097*0a6a1f1dSLionel Sambuc   case tok::kw__Atomic:         // struct foo {...} _Atomic   x;
1098*0a6a1f1dSLionel Sambuc   case tok::kw___unaligned:     // struct foo {...} __unaligned *x;
1099f4a2713aSLionel Sambuc   // Function specifiers
1100f4a2713aSLionel Sambuc   // Note, no 'explicit'. An explicit function must be either a conversion
1101f4a2713aSLionel Sambuc   // operator or a constructor. Either way, it can't have a return type.
1102f4a2713aSLionel Sambuc   case tok::kw_inline:          // struct foo       inline    f();
1103f4a2713aSLionel Sambuc   case tok::kw_virtual:         // struct foo       virtual   f();
1104f4a2713aSLionel Sambuc   case tok::kw_friend:          // struct foo       friend    f();
1105f4a2713aSLionel Sambuc   // Storage-class specifiers
1106f4a2713aSLionel Sambuc   case tok::kw_static:          // struct foo {...} static    x;
1107f4a2713aSLionel Sambuc   case tok::kw_extern:          // struct foo {...} extern    x;
1108f4a2713aSLionel Sambuc   case tok::kw_typedef:         // struct foo {...} typedef   x;
1109f4a2713aSLionel Sambuc   case tok::kw_register:        // struct foo {...} register  x;
1110f4a2713aSLionel Sambuc   case tok::kw_auto:            // struct foo {...} auto      x;
1111f4a2713aSLionel Sambuc   case tok::kw_mutable:         // struct foo {...} mutable   x;
1112f4a2713aSLionel Sambuc   case tok::kw_thread_local:    // struct foo {...} thread_local x;
1113f4a2713aSLionel Sambuc   case tok::kw_constexpr:       // struct foo {...} constexpr x;
1114f4a2713aSLionel Sambuc     // As shown above, type qualifiers and storage class specifiers absolutely
1115f4a2713aSLionel Sambuc     // can occur after class specifiers according to the grammar.  However,
1116f4a2713aSLionel Sambuc     // almost no one actually writes code like this.  If we see one of these,
1117f4a2713aSLionel Sambuc     // it is much more likely that someone missed a semi colon and the
1118f4a2713aSLionel Sambuc     // type/storage class specifier we're seeing is part of the *next*
1119f4a2713aSLionel Sambuc     // intended declaration, as in:
1120f4a2713aSLionel Sambuc     //
1121f4a2713aSLionel Sambuc     //   struct foo { ... }
1122f4a2713aSLionel Sambuc     //   typedef int X;
1123f4a2713aSLionel Sambuc     //
1124f4a2713aSLionel Sambuc     // We'd really like to emit a missing semicolon error instead of emitting
1125f4a2713aSLionel Sambuc     // an error on the 'int' saying that you can't have two type specifiers in
1126f4a2713aSLionel Sambuc     // the same declaration of X.  Because of this, we look ahead past this
1127f4a2713aSLionel Sambuc     // token to see if it's a type specifier.  If so, we know the code is
1128f4a2713aSLionel Sambuc     // otherwise invalid, so we can produce the expected semi error.
1129f4a2713aSLionel Sambuc     if (!isKnownToBeTypeSpecifier(NextToken()))
1130f4a2713aSLionel Sambuc       return true;
1131f4a2713aSLionel Sambuc     break;
1132f4a2713aSLionel Sambuc   case tok::r_brace:  // struct bar { struct foo {...} }
1133f4a2713aSLionel Sambuc     // Missing ';' at end of struct is accepted as an extension in C mode.
1134f4a2713aSLionel Sambuc     if (!getLangOpts().CPlusPlus)
1135f4a2713aSLionel Sambuc       return true;
1136f4a2713aSLionel Sambuc     break;
1137f4a2713aSLionel Sambuc   case tok::greater:
1138f4a2713aSLionel Sambuc     // template<class T = class X>
1139f4a2713aSLionel Sambuc     return getLangOpts().CPlusPlus;
1140f4a2713aSLionel Sambuc   }
1141f4a2713aSLionel Sambuc   return false;
1142f4a2713aSLionel Sambuc }
1143f4a2713aSLionel Sambuc 
1144f4a2713aSLionel Sambuc /// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
1145f4a2713aSLionel Sambuc /// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
1146f4a2713aSLionel Sambuc /// until we reach the start of a definition or see a token that
1147f4a2713aSLionel Sambuc /// cannot start a definition.
1148f4a2713aSLionel Sambuc ///
1149f4a2713aSLionel Sambuc ///       class-specifier: [C++ class]
1150f4a2713aSLionel Sambuc ///         class-head '{' member-specification[opt] '}'
1151f4a2713aSLionel Sambuc ///         class-head '{' member-specification[opt] '}' attributes[opt]
1152f4a2713aSLionel Sambuc ///       class-head:
1153f4a2713aSLionel Sambuc ///         class-key identifier[opt] base-clause[opt]
1154f4a2713aSLionel Sambuc ///         class-key nested-name-specifier identifier base-clause[opt]
1155f4a2713aSLionel Sambuc ///         class-key nested-name-specifier[opt] simple-template-id
1156f4a2713aSLionel Sambuc ///                          base-clause[opt]
1157f4a2713aSLionel Sambuc /// [GNU]   class-key attributes[opt] identifier[opt] base-clause[opt]
1158f4a2713aSLionel Sambuc /// [GNU]   class-key attributes[opt] nested-name-specifier
1159f4a2713aSLionel Sambuc ///                          identifier base-clause[opt]
1160f4a2713aSLionel Sambuc /// [GNU]   class-key attributes[opt] nested-name-specifier[opt]
1161f4a2713aSLionel Sambuc ///                          simple-template-id base-clause[opt]
1162f4a2713aSLionel Sambuc ///       class-key:
1163f4a2713aSLionel Sambuc ///         'class'
1164f4a2713aSLionel Sambuc ///         'struct'
1165f4a2713aSLionel Sambuc ///         'union'
1166f4a2713aSLionel Sambuc ///
1167f4a2713aSLionel Sambuc ///       elaborated-type-specifier: [C++ dcl.type.elab]
1168f4a2713aSLionel Sambuc ///         class-key ::[opt] nested-name-specifier[opt] identifier
1169f4a2713aSLionel Sambuc ///         class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
1170f4a2713aSLionel Sambuc ///                          simple-template-id
1171f4a2713aSLionel Sambuc ///
1172f4a2713aSLionel Sambuc ///  Note that the C++ class-specifier and elaborated-type-specifier,
1173f4a2713aSLionel Sambuc ///  together, subsume the C99 struct-or-union-specifier:
1174f4a2713aSLionel Sambuc ///
1175f4a2713aSLionel Sambuc ///       struct-or-union-specifier: [C99 6.7.2.1]
1176f4a2713aSLionel Sambuc ///         struct-or-union identifier[opt] '{' struct-contents '}'
1177f4a2713aSLionel Sambuc ///         struct-or-union identifier
1178f4a2713aSLionel Sambuc /// [GNU]   struct-or-union attributes[opt] identifier[opt] '{' struct-contents
1179f4a2713aSLionel Sambuc ///                                                         '}' attributes[opt]
1180f4a2713aSLionel Sambuc /// [GNU]   struct-or-union attributes[opt] identifier
1181f4a2713aSLionel Sambuc ///       struct-or-union:
1182f4a2713aSLionel Sambuc ///         'struct'
1183f4a2713aSLionel Sambuc ///         'union'
ParseClassSpecifier(tok::TokenKind TagTokKind,SourceLocation StartLoc,DeclSpec & DS,const ParsedTemplateInfo & TemplateInfo,AccessSpecifier AS,bool EnteringContext,DeclSpecContext DSC,ParsedAttributesWithRange & Attributes)1184f4a2713aSLionel Sambuc void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
1185f4a2713aSLionel Sambuc                                  SourceLocation StartLoc, DeclSpec &DS,
1186f4a2713aSLionel Sambuc                                  const ParsedTemplateInfo &TemplateInfo,
1187f4a2713aSLionel Sambuc                                  AccessSpecifier AS,
1188f4a2713aSLionel Sambuc                                  bool EnteringContext, DeclSpecContext DSC,
1189f4a2713aSLionel Sambuc                                  ParsedAttributesWithRange &Attributes) {
1190f4a2713aSLionel Sambuc   DeclSpec::TST TagType;
1191f4a2713aSLionel Sambuc   if (TagTokKind == tok::kw_struct)
1192f4a2713aSLionel Sambuc     TagType = DeclSpec::TST_struct;
1193f4a2713aSLionel Sambuc   else if (TagTokKind == tok::kw___interface)
1194f4a2713aSLionel Sambuc     TagType = DeclSpec::TST_interface;
1195f4a2713aSLionel Sambuc   else if (TagTokKind == tok::kw_class)
1196f4a2713aSLionel Sambuc     TagType = DeclSpec::TST_class;
1197f4a2713aSLionel Sambuc   else {
1198f4a2713aSLionel Sambuc     assert(TagTokKind == tok::kw_union && "Not a class specifier");
1199f4a2713aSLionel Sambuc     TagType = DeclSpec::TST_union;
1200f4a2713aSLionel Sambuc   }
1201f4a2713aSLionel Sambuc 
1202f4a2713aSLionel Sambuc   if (Tok.is(tok::code_completion)) {
1203f4a2713aSLionel Sambuc     // Code completion for a struct, class, or union name.
1204f4a2713aSLionel Sambuc     Actions.CodeCompleteTag(getCurScope(), TagType);
1205f4a2713aSLionel Sambuc     return cutOffParsing();
1206f4a2713aSLionel Sambuc   }
1207f4a2713aSLionel Sambuc 
1208f4a2713aSLionel Sambuc   // C++03 [temp.explicit] 14.7.2/8:
1209f4a2713aSLionel Sambuc   //   The usual access checking rules do not apply to names used to specify
1210f4a2713aSLionel Sambuc   //   explicit instantiations.
1211f4a2713aSLionel Sambuc   //
1212f4a2713aSLionel Sambuc   // As an extension we do not perform access checking on the names used to
1213f4a2713aSLionel Sambuc   // specify explicit specializations either. This is important to allow
1214f4a2713aSLionel Sambuc   // specializing traits classes for private types.
1215f4a2713aSLionel Sambuc   //
1216f4a2713aSLionel Sambuc   // Note that we don't suppress if this turns out to be an elaborated
1217f4a2713aSLionel Sambuc   // type specifier.
1218f4a2713aSLionel Sambuc   bool shouldDelayDiagsInTag =
1219f4a2713aSLionel Sambuc     (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
1220f4a2713aSLionel Sambuc      TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
1221f4a2713aSLionel Sambuc   SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
1222f4a2713aSLionel Sambuc 
1223f4a2713aSLionel Sambuc   ParsedAttributesWithRange attrs(AttrFactory);
1224f4a2713aSLionel Sambuc   // If attributes exist after tag, parse them.
1225f4a2713aSLionel Sambuc   MaybeParseGNUAttributes(attrs);
1226f4a2713aSLionel Sambuc 
1227f4a2713aSLionel Sambuc   // If declspecs exist after tag, parse them.
1228f4a2713aSLionel Sambuc   while (Tok.is(tok::kw___declspec))
1229f4a2713aSLionel Sambuc     ParseMicrosoftDeclSpec(attrs);
1230f4a2713aSLionel Sambuc 
1231f4a2713aSLionel Sambuc   // Parse inheritance specifiers.
1232f4a2713aSLionel Sambuc   if (Tok.is(tok::kw___single_inheritance) ||
1233f4a2713aSLionel Sambuc       Tok.is(tok::kw___multiple_inheritance) ||
1234f4a2713aSLionel Sambuc       Tok.is(tok::kw___virtual_inheritance))
1235f4a2713aSLionel Sambuc     ParseMicrosoftInheritanceClassAttributes(attrs);
1236f4a2713aSLionel Sambuc 
1237f4a2713aSLionel Sambuc   // If C++0x attributes exist here, parse them.
1238f4a2713aSLionel Sambuc   // FIXME: Are we consistent with the ordering of parsing of different
1239f4a2713aSLionel Sambuc   // styles of attributes?
1240f4a2713aSLionel Sambuc   MaybeParseCXX11Attributes(attrs);
1241f4a2713aSLionel Sambuc 
1242f4a2713aSLionel Sambuc   // Source location used by FIXIT to insert misplaced
1243f4a2713aSLionel Sambuc   // C++11 attributes
1244f4a2713aSLionel Sambuc   SourceLocation AttrFixitLoc = Tok.getLocation();
1245f4a2713aSLionel Sambuc 
1246f4a2713aSLionel Sambuc   if (TagType == DeclSpec::TST_struct &&
1247*0a6a1f1dSLionel Sambuc       Tok.isNot(tok::identifier) &&
1248*0a6a1f1dSLionel Sambuc       !Tok.isAnnotation() &&
1249f4a2713aSLionel Sambuc       Tok.getIdentifierInfo() &&
1250*0a6a1f1dSLionel Sambuc       (Tok.is(tok::kw___is_abstract) ||
1251*0a6a1f1dSLionel Sambuc        Tok.is(tok::kw___is_arithmetic) ||
1252*0a6a1f1dSLionel Sambuc        Tok.is(tok::kw___is_array) ||
1253*0a6a1f1dSLionel Sambuc        Tok.is(tok::kw___is_base_of) ||
1254*0a6a1f1dSLionel Sambuc        Tok.is(tok::kw___is_class) ||
1255*0a6a1f1dSLionel Sambuc        Tok.is(tok::kw___is_complete_type) ||
1256*0a6a1f1dSLionel Sambuc        Tok.is(tok::kw___is_compound) ||
1257*0a6a1f1dSLionel Sambuc        Tok.is(tok::kw___is_const) ||
1258*0a6a1f1dSLionel Sambuc        Tok.is(tok::kw___is_constructible) ||
1259f4a2713aSLionel Sambuc        Tok.is(tok::kw___is_convertible) ||
1260*0a6a1f1dSLionel Sambuc        Tok.is(tok::kw___is_convertible_to) ||
1261*0a6a1f1dSLionel Sambuc        Tok.is(tok::kw___is_destructible) ||
1262f4a2713aSLionel Sambuc        Tok.is(tok::kw___is_empty) ||
1263*0a6a1f1dSLionel Sambuc        Tok.is(tok::kw___is_enum) ||
1264f4a2713aSLionel Sambuc        Tok.is(tok::kw___is_floating_point) ||
1265*0a6a1f1dSLionel Sambuc        Tok.is(tok::kw___is_final) ||
1266f4a2713aSLionel Sambuc        Tok.is(tok::kw___is_function) ||
1267f4a2713aSLionel Sambuc        Tok.is(tok::kw___is_fundamental) ||
1268f4a2713aSLionel Sambuc        Tok.is(tok::kw___is_integral) ||
1269*0a6a1f1dSLionel Sambuc        Tok.is(tok::kw___is_interface_class) ||
1270*0a6a1f1dSLionel Sambuc        Tok.is(tok::kw___is_literal) ||
1271*0a6a1f1dSLionel Sambuc        Tok.is(tok::kw___is_lvalue_expr) ||
1272*0a6a1f1dSLionel Sambuc        Tok.is(tok::kw___is_lvalue_reference) ||
1273f4a2713aSLionel Sambuc        Tok.is(tok::kw___is_member_function_pointer) ||
1274*0a6a1f1dSLionel Sambuc        Tok.is(tok::kw___is_member_object_pointer) ||
1275f4a2713aSLionel Sambuc        Tok.is(tok::kw___is_member_pointer) ||
1276*0a6a1f1dSLionel Sambuc        Tok.is(tok::kw___is_nothrow_assignable) ||
1277*0a6a1f1dSLionel Sambuc        Tok.is(tok::kw___is_nothrow_constructible) ||
1278*0a6a1f1dSLionel Sambuc        Tok.is(tok::kw___is_nothrow_destructible) ||
1279*0a6a1f1dSLionel Sambuc        Tok.is(tok::kw___is_object) ||
1280f4a2713aSLionel Sambuc        Tok.is(tok::kw___is_pod) ||
1281f4a2713aSLionel Sambuc        Tok.is(tok::kw___is_pointer) ||
1282*0a6a1f1dSLionel Sambuc        Tok.is(tok::kw___is_polymorphic) ||
1283*0a6a1f1dSLionel Sambuc        Tok.is(tok::kw___is_reference) ||
1284*0a6a1f1dSLionel Sambuc        Tok.is(tok::kw___is_rvalue_expr) ||
1285*0a6a1f1dSLionel Sambuc        Tok.is(tok::kw___is_rvalue_reference) ||
1286f4a2713aSLionel Sambuc        Tok.is(tok::kw___is_same) ||
1287f4a2713aSLionel Sambuc        Tok.is(tok::kw___is_scalar) ||
1288*0a6a1f1dSLionel Sambuc        Tok.is(tok::kw___is_sealed) ||
1289f4a2713aSLionel Sambuc        Tok.is(tok::kw___is_signed) ||
1290*0a6a1f1dSLionel Sambuc        Tok.is(tok::kw___is_standard_layout) ||
1291*0a6a1f1dSLionel Sambuc        Tok.is(tok::kw___is_trivial) ||
1292*0a6a1f1dSLionel Sambuc        Tok.is(tok::kw___is_trivially_assignable) ||
1293*0a6a1f1dSLionel Sambuc        Tok.is(tok::kw___is_trivially_constructible) ||
1294*0a6a1f1dSLionel Sambuc        Tok.is(tok::kw___is_trivially_copyable) ||
1295*0a6a1f1dSLionel Sambuc        Tok.is(tok::kw___is_union) ||
1296f4a2713aSLionel Sambuc        Tok.is(tok::kw___is_unsigned) ||
1297*0a6a1f1dSLionel Sambuc        Tok.is(tok::kw___is_void) ||
1298*0a6a1f1dSLionel Sambuc        Tok.is(tok::kw___is_volatile)))
1299f4a2713aSLionel Sambuc     // GNU libstdc++ 4.2 and libc++ use certain intrinsic names as the
1300f4a2713aSLionel Sambuc     // name of struct templates, but some are keywords in GCC >= 4.3
1301f4a2713aSLionel Sambuc     // and Clang. Therefore, when we see the token sequence "struct
1302f4a2713aSLionel Sambuc     // X", make X into a normal identifier rather than a keyword, to
1303f4a2713aSLionel Sambuc     // allow libstdc++ 4.2 and libc++ to work properly.
1304*0a6a1f1dSLionel Sambuc     TryKeywordIdentFallback(true);
1305f4a2713aSLionel Sambuc 
1306f4a2713aSLionel Sambuc   // Parse the (optional) nested-name-specifier.
1307f4a2713aSLionel Sambuc   CXXScopeSpec &SS = DS.getTypeSpecScope();
1308f4a2713aSLionel Sambuc   if (getLangOpts().CPlusPlus) {
1309*0a6a1f1dSLionel Sambuc     // "FOO : BAR" is not a potential typo for "FOO::BAR".  In this context it
1310*0a6a1f1dSLionel Sambuc     // is a base-specifier-list.
1311f4a2713aSLionel Sambuc     ColonProtectionRAIIObject X(*this);
1312f4a2713aSLionel Sambuc 
1313f4a2713aSLionel Sambuc     if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext))
1314f4a2713aSLionel Sambuc       DS.SetTypeSpecError();
1315f4a2713aSLionel Sambuc     if (SS.isSet())
1316f4a2713aSLionel Sambuc       if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
1317*0a6a1f1dSLionel Sambuc         Diag(Tok, diag::err_expected) << tok::identifier;
1318f4a2713aSLionel Sambuc   }
1319f4a2713aSLionel Sambuc 
1320f4a2713aSLionel Sambuc   TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
1321f4a2713aSLionel Sambuc 
1322f4a2713aSLionel Sambuc   // Parse the (optional) class name or simple-template-id.
1323*0a6a1f1dSLionel Sambuc   IdentifierInfo *Name = nullptr;
1324f4a2713aSLionel Sambuc   SourceLocation NameLoc;
1325*0a6a1f1dSLionel Sambuc   TemplateIdAnnotation *TemplateId = nullptr;
1326f4a2713aSLionel Sambuc   if (Tok.is(tok::identifier)) {
1327f4a2713aSLionel Sambuc     Name = Tok.getIdentifierInfo();
1328f4a2713aSLionel Sambuc     NameLoc = ConsumeToken();
1329f4a2713aSLionel Sambuc 
1330f4a2713aSLionel Sambuc     if (Tok.is(tok::less) && getLangOpts().CPlusPlus) {
1331f4a2713aSLionel Sambuc       // The name was supposed to refer to a template, but didn't.
1332f4a2713aSLionel Sambuc       // Eat the template argument list and try to continue parsing this as
1333f4a2713aSLionel Sambuc       // a class (or template thereof).
1334f4a2713aSLionel Sambuc       TemplateArgList TemplateArgs;
1335f4a2713aSLionel Sambuc       SourceLocation LAngleLoc, RAngleLoc;
1336f4a2713aSLionel Sambuc       if (ParseTemplateIdAfterTemplateName(TemplateTy(), NameLoc, SS,
1337f4a2713aSLionel Sambuc                                            true, LAngleLoc,
1338f4a2713aSLionel Sambuc                                            TemplateArgs, RAngleLoc)) {
1339f4a2713aSLionel Sambuc         // We couldn't parse the template argument list at all, so don't
1340f4a2713aSLionel Sambuc         // try to give any location information for the list.
1341f4a2713aSLionel Sambuc         LAngleLoc = RAngleLoc = SourceLocation();
1342f4a2713aSLionel Sambuc       }
1343f4a2713aSLionel Sambuc 
1344f4a2713aSLionel Sambuc       Diag(NameLoc, diag::err_explicit_spec_non_template)
1345f4a2713aSLionel Sambuc           << (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
1346*0a6a1f1dSLionel Sambuc           << TagTokKind << Name << SourceRange(LAngleLoc, RAngleLoc);
1347f4a2713aSLionel Sambuc 
1348f4a2713aSLionel Sambuc       // Strip off the last template parameter list if it was empty, since
1349f4a2713aSLionel Sambuc       // we've removed its template argument list.
1350f4a2713aSLionel Sambuc       if (TemplateParams && TemplateInfo.LastParameterListWasEmpty) {
1351f4a2713aSLionel Sambuc         if (TemplateParams && TemplateParams->size() > 1) {
1352f4a2713aSLionel Sambuc           TemplateParams->pop_back();
1353f4a2713aSLionel Sambuc         } else {
1354*0a6a1f1dSLionel Sambuc           TemplateParams = nullptr;
1355f4a2713aSLionel Sambuc           const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
1356f4a2713aSLionel Sambuc             = ParsedTemplateInfo::NonTemplate;
1357f4a2713aSLionel Sambuc         }
1358f4a2713aSLionel Sambuc       } else if (TemplateInfo.Kind
1359f4a2713aSLionel Sambuc                                 == ParsedTemplateInfo::ExplicitInstantiation) {
1360f4a2713aSLionel Sambuc         // Pretend this is just a forward declaration.
1361*0a6a1f1dSLionel Sambuc         TemplateParams = nullptr;
1362f4a2713aSLionel Sambuc         const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
1363f4a2713aSLionel Sambuc           = ParsedTemplateInfo::NonTemplate;
1364f4a2713aSLionel Sambuc         const_cast<ParsedTemplateInfo&>(TemplateInfo).TemplateLoc
1365f4a2713aSLionel Sambuc           = SourceLocation();
1366f4a2713aSLionel Sambuc         const_cast<ParsedTemplateInfo&>(TemplateInfo).ExternLoc
1367f4a2713aSLionel Sambuc           = SourceLocation();
1368f4a2713aSLionel Sambuc       }
1369f4a2713aSLionel Sambuc     }
1370f4a2713aSLionel Sambuc   } else if (Tok.is(tok::annot_template_id)) {
1371f4a2713aSLionel Sambuc     TemplateId = takeTemplateIdAnnotation(Tok);
1372f4a2713aSLionel Sambuc     NameLoc = ConsumeToken();
1373f4a2713aSLionel Sambuc 
1374f4a2713aSLionel Sambuc     if (TemplateId->Kind != TNK_Type_template &&
1375f4a2713aSLionel Sambuc         TemplateId->Kind != TNK_Dependent_template_name) {
1376f4a2713aSLionel Sambuc       // The template-name in the simple-template-id refers to
1377f4a2713aSLionel Sambuc       // something other than a class template. Give an appropriate
1378f4a2713aSLionel Sambuc       // error message and skip to the ';'.
1379f4a2713aSLionel Sambuc       SourceRange Range(NameLoc);
1380f4a2713aSLionel Sambuc       if (SS.isNotEmpty())
1381f4a2713aSLionel Sambuc         Range.setBegin(SS.getBeginLoc());
1382f4a2713aSLionel Sambuc 
1383*0a6a1f1dSLionel Sambuc       // FIXME: Name may be null here.
1384f4a2713aSLionel Sambuc       Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
1385f4a2713aSLionel Sambuc         << TemplateId->Name << static_cast<int>(TemplateId->Kind) << Range;
1386f4a2713aSLionel Sambuc 
1387f4a2713aSLionel Sambuc       DS.SetTypeSpecError();
1388f4a2713aSLionel Sambuc       SkipUntil(tok::semi, StopBeforeMatch);
1389f4a2713aSLionel Sambuc       return;
1390f4a2713aSLionel Sambuc     }
1391f4a2713aSLionel Sambuc   }
1392f4a2713aSLionel Sambuc 
1393f4a2713aSLionel Sambuc   // There are four options here.
1394f4a2713aSLionel Sambuc   //  - If we are in a trailing return type, this is always just a reference,
1395f4a2713aSLionel Sambuc   //    and we must not try to parse a definition. For instance,
1396f4a2713aSLionel Sambuc   //      [] () -> struct S { };
1397f4a2713aSLionel Sambuc   //    does not define a type.
1398f4a2713aSLionel Sambuc   //  - If we have 'struct foo {...', 'struct foo :...',
1399f4a2713aSLionel Sambuc   //    'struct foo final :' or 'struct foo final {', then this is a definition.
1400f4a2713aSLionel Sambuc   //  - If we have 'struct foo;', then this is either a forward declaration
1401f4a2713aSLionel Sambuc   //    or a friend declaration, which have to be treated differently.
1402f4a2713aSLionel Sambuc   //  - Otherwise we have something like 'struct foo xyz', a reference.
1403f4a2713aSLionel Sambuc   //
1404f4a2713aSLionel Sambuc   //  We also detect these erroneous cases to provide better diagnostic for
1405f4a2713aSLionel Sambuc   //  C++11 attributes parsing.
1406f4a2713aSLionel Sambuc   //  - attributes follow class name:
1407f4a2713aSLionel Sambuc   //    struct foo [[]] {};
1408f4a2713aSLionel Sambuc   //  - attributes appear before or after 'final':
1409f4a2713aSLionel Sambuc   //    struct foo [[]] final [[]] {};
1410f4a2713aSLionel Sambuc   //
1411f4a2713aSLionel Sambuc   // However, in type-specifier-seq's, things look like declarations but are
1412f4a2713aSLionel Sambuc   // just references, e.g.
1413f4a2713aSLionel Sambuc   //   new struct s;
1414f4a2713aSLionel Sambuc   // or
1415f4a2713aSLionel Sambuc   //   &T::operator struct s;
1416*0a6a1f1dSLionel Sambuc   // For these, DSC is DSC_type_specifier or DSC_alias_declaration.
1417f4a2713aSLionel Sambuc 
1418f4a2713aSLionel Sambuc   // If there are attributes after class name, parse them.
1419f4a2713aSLionel Sambuc   MaybeParseCXX11Attributes(Attributes);
1420f4a2713aSLionel Sambuc 
1421*0a6a1f1dSLionel Sambuc   const PrintingPolicy &Policy = Actions.getASTContext().getPrintingPolicy();
1422f4a2713aSLionel Sambuc   Sema::TagUseKind TUK;
1423f4a2713aSLionel Sambuc   if (DSC == DSC_trailing)
1424f4a2713aSLionel Sambuc     TUK = Sema::TUK_Reference;
1425f4a2713aSLionel Sambuc   else if (Tok.is(tok::l_brace) ||
1426f4a2713aSLionel Sambuc            (getLangOpts().CPlusPlus && Tok.is(tok::colon)) ||
1427f4a2713aSLionel Sambuc            (isCXX11FinalKeyword() &&
1428f4a2713aSLionel Sambuc             (NextToken().is(tok::l_brace) || NextToken().is(tok::colon)))) {
1429f4a2713aSLionel Sambuc     if (DS.isFriendSpecified()) {
1430f4a2713aSLionel Sambuc       // C++ [class.friend]p2:
1431f4a2713aSLionel Sambuc       //   A class shall not be defined in a friend declaration.
1432f4a2713aSLionel Sambuc       Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
1433f4a2713aSLionel Sambuc         << SourceRange(DS.getFriendSpecLoc());
1434f4a2713aSLionel Sambuc 
1435f4a2713aSLionel Sambuc       // Skip everything up to the semicolon, so that this looks like a proper
1436f4a2713aSLionel Sambuc       // friend class (or template thereof) declaration.
1437f4a2713aSLionel Sambuc       SkipUntil(tok::semi, StopBeforeMatch);
1438f4a2713aSLionel Sambuc       TUK = Sema::TUK_Friend;
1439f4a2713aSLionel Sambuc     } else {
1440f4a2713aSLionel Sambuc       // Okay, this is a class definition.
1441f4a2713aSLionel Sambuc       TUK = Sema::TUK_Definition;
1442f4a2713aSLionel Sambuc     }
1443f4a2713aSLionel Sambuc   } else if (isCXX11FinalKeyword() && (NextToken().is(tok::l_square) ||
1444f4a2713aSLionel Sambuc                                        NextToken().is(tok::kw_alignas))) {
1445f4a2713aSLionel Sambuc     // We can't tell if this is a definition or reference
1446f4a2713aSLionel Sambuc     // until we skipped the 'final' and C++11 attribute specifiers.
1447f4a2713aSLionel Sambuc     TentativeParsingAction PA(*this);
1448f4a2713aSLionel Sambuc 
1449f4a2713aSLionel Sambuc     // Skip the 'final' keyword.
1450f4a2713aSLionel Sambuc     ConsumeToken();
1451f4a2713aSLionel Sambuc 
1452f4a2713aSLionel Sambuc     // Skip C++11 attribute specifiers.
1453f4a2713aSLionel Sambuc     while (true) {
1454f4a2713aSLionel Sambuc       if (Tok.is(tok::l_square) && NextToken().is(tok::l_square)) {
1455f4a2713aSLionel Sambuc         ConsumeBracket();
1456f4a2713aSLionel Sambuc         if (!SkipUntil(tok::r_square, StopAtSemi))
1457f4a2713aSLionel Sambuc           break;
1458f4a2713aSLionel Sambuc       } else if (Tok.is(tok::kw_alignas) && NextToken().is(tok::l_paren)) {
1459f4a2713aSLionel Sambuc         ConsumeToken();
1460f4a2713aSLionel Sambuc         ConsumeParen();
1461f4a2713aSLionel Sambuc         if (!SkipUntil(tok::r_paren, StopAtSemi))
1462f4a2713aSLionel Sambuc           break;
1463f4a2713aSLionel Sambuc       } else {
1464f4a2713aSLionel Sambuc         break;
1465f4a2713aSLionel Sambuc       }
1466f4a2713aSLionel Sambuc     }
1467f4a2713aSLionel Sambuc 
1468f4a2713aSLionel Sambuc     if (Tok.is(tok::l_brace) || Tok.is(tok::colon))
1469f4a2713aSLionel Sambuc       TUK = Sema::TUK_Definition;
1470f4a2713aSLionel Sambuc     else
1471f4a2713aSLionel Sambuc       TUK = Sema::TUK_Reference;
1472f4a2713aSLionel Sambuc 
1473f4a2713aSLionel Sambuc     PA.Revert();
1474*0a6a1f1dSLionel Sambuc   } else if (!isTypeSpecifier(DSC) &&
1475f4a2713aSLionel Sambuc              (Tok.is(tok::semi) ||
1476f4a2713aSLionel Sambuc               (Tok.isAtStartOfLine() && !isValidAfterTypeSpecifier(false)))) {
1477f4a2713aSLionel Sambuc     TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
1478f4a2713aSLionel Sambuc     if (Tok.isNot(tok::semi)) {
1479*0a6a1f1dSLionel Sambuc       const PrintingPolicy &PPol = Actions.getASTContext().getPrintingPolicy();
1480f4a2713aSLionel Sambuc       // A semicolon was missing after this declaration. Diagnose and recover.
1481*0a6a1f1dSLionel Sambuc       ExpectAndConsume(tok::semi, diag::err_expected_after,
1482*0a6a1f1dSLionel Sambuc                        DeclSpec::getSpecifierName(TagType, PPol));
1483f4a2713aSLionel Sambuc       PP.EnterToken(Tok);
1484f4a2713aSLionel Sambuc       Tok.setKind(tok::semi);
1485f4a2713aSLionel Sambuc     }
1486f4a2713aSLionel Sambuc   } else
1487f4a2713aSLionel Sambuc     TUK = Sema::TUK_Reference;
1488f4a2713aSLionel Sambuc 
1489f4a2713aSLionel Sambuc   // Forbid misplaced attributes. In cases of a reference, we pass attributes
1490f4a2713aSLionel Sambuc   // to caller to handle.
1491f4a2713aSLionel Sambuc   if (TUK != Sema::TUK_Reference) {
1492f4a2713aSLionel Sambuc     // If this is not a reference, then the only possible
1493f4a2713aSLionel Sambuc     // valid place for C++11 attributes to appear here
1494f4a2713aSLionel Sambuc     // is between class-key and class-name. If there are
1495f4a2713aSLionel Sambuc     // any attributes after class-name, we try a fixit to move
1496f4a2713aSLionel Sambuc     // them to the right place.
1497f4a2713aSLionel Sambuc     SourceRange AttrRange = Attributes.Range;
1498f4a2713aSLionel Sambuc     if (AttrRange.isValid()) {
1499f4a2713aSLionel Sambuc       Diag(AttrRange.getBegin(), diag::err_attributes_not_allowed)
1500f4a2713aSLionel Sambuc         << AttrRange
1501f4a2713aSLionel Sambuc         << FixItHint::CreateInsertionFromRange(AttrFixitLoc,
1502f4a2713aSLionel Sambuc                                                CharSourceRange(AttrRange, true))
1503f4a2713aSLionel Sambuc         << FixItHint::CreateRemoval(AttrRange);
1504f4a2713aSLionel Sambuc 
1505f4a2713aSLionel Sambuc       // Recover by adding misplaced attributes to the attribute list
1506f4a2713aSLionel Sambuc       // of the class so they can be applied on the class later.
1507f4a2713aSLionel Sambuc       attrs.takeAllFrom(Attributes);
1508f4a2713aSLionel Sambuc     }
1509f4a2713aSLionel Sambuc   }
1510f4a2713aSLionel Sambuc 
1511f4a2713aSLionel Sambuc   // If this is an elaborated type specifier, and we delayed
1512f4a2713aSLionel Sambuc   // diagnostics before, just merge them into the current pool.
1513f4a2713aSLionel Sambuc   if (shouldDelayDiagsInTag) {
1514f4a2713aSLionel Sambuc     diagsFromTag.done();
1515f4a2713aSLionel Sambuc     if (TUK == Sema::TUK_Reference)
1516f4a2713aSLionel Sambuc       diagsFromTag.redelay();
1517f4a2713aSLionel Sambuc   }
1518f4a2713aSLionel Sambuc 
1519f4a2713aSLionel Sambuc   if (!Name && !TemplateId && (DS.getTypeSpecType() == DeclSpec::TST_error ||
1520f4a2713aSLionel Sambuc                                TUK != Sema::TUK_Definition)) {
1521f4a2713aSLionel Sambuc     if (DS.getTypeSpecType() != DeclSpec::TST_error) {
1522f4a2713aSLionel Sambuc       // We have a declaration or reference to an anonymous class.
1523f4a2713aSLionel Sambuc       Diag(StartLoc, diag::err_anon_type_definition)
1524*0a6a1f1dSLionel Sambuc         << DeclSpec::getSpecifierName(TagType, Policy);
1525f4a2713aSLionel Sambuc     }
1526f4a2713aSLionel Sambuc 
1527*0a6a1f1dSLionel Sambuc     // If we are parsing a definition and stop at a base-clause, continue on
1528*0a6a1f1dSLionel Sambuc     // until the semicolon.  Continuing from the comma will just trick us into
1529*0a6a1f1dSLionel Sambuc     // thinking we are seeing a variable declaration.
1530*0a6a1f1dSLionel Sambuc     if (TUK == Sema::TUK_Definition && Tok.is(tok::colon))
1531*0a6a1f1dSLionel Sambuc       SkipUntil(tok::semi, StopBeforeMatch);
1532*0a6a1f1dSLionel Sambuc     else
1533f4a2713aSLionel Sambuc       SkipUntil(tok::comma, StopAtSemi);
1534f4a2713aSLionel Sambuc     return;
1535f4a2713aSLionel Sambuc   }
1536f4a2713aSLionel Sambuc 
1537f4a2713aSLionel Sambuc   // Create the tag portion of the class or class template.
1538f4a2713aSLionel Sambuc   DeclResult TagOrTempResult = true; // invalid
1539f4a2713aSLionel Sambuc   TypeResult TypeResult = true; // invalid
1540f4a2713aSLionel Sambuc 
1541f4a2713aSLionel Sambuc   bool Owned = false;
1542f4a2713aSLionel Sambuc   if (TemplateId) {
1543f4a2713aSLionel Sambuc     // Explicit specialization, class template partial specialization,
1544f4a2713aSLionel Sambuc     // or explicit instantiation.
1545f4a2713aSLionel Sambuc     ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(),
1546f4a2713aSLionel Sambuc                                        TemplateId->NumArgs);
1547f4a2713aSLionel Sambuc     if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
1548f4a2713aSLionel Sambuc         TUK == Sema::TUK_Declaration) {
1549f4a2713aSLionel Sambuc       // This is an explicit instantiation of a class template.
1550f4a2713aSLionel Sambuc       ProhibitAttributes(attrs);
1551f4a2713aSLionel Sambuc 
1552f4a2713aSLionel Sambuc       TagOrTempResult
1553f4a2713aSLionel Sambuc         = Actions.ActOnExplicitInstantiation(getCurScope(),
1554f4a2713aSLionel Sambuc                                              TemplateInfo.ExternLoc,
1555f4a2713aSLionel Sambuc                                              TemplateInfo.TemplateLoc,
1556f4a2713aSLionel Sambuc                                              TagType,
1557f4a2713aSLionel Sambuc                                              StartLoc,
1558f4a2713aSLionel Sambuc                                              SS,
1559f4a2713aSLionel Sambuc                                              TemplateId->Template,
1560f4a2713aSLionel Sambuc                                              TemplateId->TemplateNameLoc,
1561f4a2713aSLionel Sambuc                                              TemplateId->LAngleLoc,
1562f4a2713aSLionel Sambuc                                              TemplateArgsPtr,
1563f4a2713aSLionel Sambuc                                              TemplateId->RAngleLoc,
1564f4a2713aSLionel Sambuc                                              attrs.getList());
1565f4a2713aSLionel Sambuc 
1566f4a2713aSLionel Sambuc     // Friend template-ids are treated as references unless
1567f4a2713aSLionel Sambuc     // they have template headers, in which case they're ill-formed
1568f4a2713aSLionel Sambuc     // (FIXME: "template <class T> friend class A<T>::B<int>;").
1569f4a2713aSLionel Sambuc     // We diagnose this error in ActOnClassTemplateSpecialization.
1570f4a2713aSLionel Sambuc     } else if (TUK == Sema::TUK_Reference ||
1571f4a2713aSLionel Sambuc                (TUK == Sema::TUK_Friend &&
1572f4a2713aSLionel Sambuc                 TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate)) {
1573f4a2713aSLionel Sambuc       ProhibitAttributes(attrs);
1574f4a2713aSLionel Sambuc       TypeResult = Actions.ActOnTagTemplateIdType(TUK, TagType, StartLoc,
1575f4a2713aSLionel Sambuc                                                   TemplateId->SS,
1576f4a2713aSLionel Sambuc                                                   TemplateId->TemplateKWLoc,
1577f4a2713aSLionel Sambuc                                                   TemplateId->Template,
1578f4a2713aSLionel Sambuc                                                   TemplateId->TemplateNameLoc,
1579f4a2713aSLionel Sambuc                                                   TemplateId->LAngleLoc,
1580f4a2713aSLionel Sambuc                                                   TemplateArgsPtr,
1581f4a2713aSLionel Sambuc                                                   TemplateId->RAngleLoc);
1582f4a2713aSLionel Sambuc     } else {
1583f4a2713aSLionel Sambuc       // This is an explicit specialization or a class template
1584f4a2713aSLionel Sambuc       // partial specialization.
1585f4a2713aSLionel Sambuc       TemplateParameterLists FakedParamLists;
1586f4a2713aSLionel Sambuc       if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
1587f4a2713aSLionel Sambuc         // This looks like an explicit instantiation, because we have
1588f4a2713aSLionel Sambuc         // something like
1589f4a2713aSLionel Sambuc         //
1590f4a2713aSLionel Sambuc         //   template class Foo<X>
1591f4a2713aSLionel Sambuc         //
1592f4a2713aSLionel Sambuc         // but it actually has a definition. Most likely, this was
1593f4a2713aSLionel Sambuc         // meant to be an explicit specialization, but the user forgot
1594f4a2713aSLionel Sambuc         // the '<>' after 'template'.
1595f4a2713aSLionel Sambuc         // It this is friend declaration however, since it cannot have a
1596f4a2713aSLionel Sambuc         // template header, it is most likely that the user meant to
1597f4a2713aSLionel Sambuc         // remove the 'template' keyword.
1598f4a2713aSLionel Sambuc         assert((TUK == Sema::TUK_Definition || TUK == Sema::TUK_Friend) &&
1599f4a2713aSLionel Sambuc                "Expected a definition here");
1600f4a2713aSLionel Sambuc 
1601f4a2713aSLionel Sambuc         if (TUK == Sema::TUK_Friend) {
1602f4a2713aSLionel Sambuc           Diag(DS.getFriendSpecLoc(), diag::err_friend_explicit_instantiation);
1603*0a6a1f1dSLionel Sambuc           TemplateParams = nullptr;
1604f4a2713aSLionel Sambuc         } else {
1605f4a2713aSLionel Sambuc           SourceLocation LAngleLoc =
1606f4a2713aSLionel Sambuc               PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
1607f4a2713aSLionel Sambuc           Diag(TemplateId->TemplateNameLoc,
1608f4a2713aSLionel Sambuc                diag::err_explicit_instantiation_with_definition)
1609f4a2713aSLionel Sambuc               << SourceRange(TemplateInfo.TemplateLoc)
1610f4a2713aSLionel Sambuc               << FixItHint::CreateInsertion(LAngleLoc, "<>");
1611f4a2713aSLionel Sambuc 
1612f4a2713aSLionel Sambuc           // Create a fake template parameter list that contains only
1613f4a2713aSLionel Sambuc           // "template<>", so that we treat this construct as a class
1614f4a2713aSLionel Sambuc           // template specialization.
1615f4a2713aSLionel Sambuc           FakedParamLists.push_back(Actions.ActOnTemplateParameterList(
1616*0a6a1f1dSLionel Sambuc               0, SourceLocation(), TemplateInfo.TemplateLoc, LAngleLoc, nullptr,
1617*0a6a1f1dSLionel Sambuc               0, LAngleLoc));
1618f4a2713aSLionel Sambuc           TemplateParams = &FakedParamLists;
1619f4a2713aSLionel Sambuc         }
1620f4a2713aSLionel Sambuc       }
1621f4a2713aSLionel Sambuc 
1622f4a2713aSLionel Sambuc       // Build the class template specialization.
1623*0a6a1f1dSLionel Sambuc       TagOrTempResult = Actions.ActOnClassTemplateSpecialization(
1624*0a6a1f1dSLionel Sambuc           getCurScope(), TagType, TUK, StartLoc, DS.getModulePrivateSpecLoc(),
1625*0a6a1f1dSLionel Sambuc           *TemplateId, attrs.getList(),
1626*0a6a1f1dSLionel Sambuc           MultiTemplateParamsArg(TemplateParams ? &(*TemplateParams)[0]
1627*0a6a1f1dSLionel Sambuc                                                 : nullptr,
1628f4a2713aSLionel Sambuc                                  TemplateParams ? TemplateParams->size() : 0));
1629f4a2713aSLionel Sambuc     }
1630f4a2713aSLionel Sambuc   } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
1631f4a2713aSLionel Sambuc              TUK == Sema::TUK_Declaration) {
1632f4a2713aSLionel Sambuc     // Explicit instantiation of a member of a class template
1633f4a2713aSLionel Sambuc     // specialization, e.g.,
1634f4a2713aSLionel Sambuc     //
1635f4a2713aSLionel Sambuc     //   template struct Outer<int>::Inner;
1636f4a2713aSLionel Sambuc     //
1637f4a2713aSLionel Sambuc     ProhibitAttributes(attrs);
1638f4a2713aSLionel Sambuc 
1639f4a2713aSLionel Sambuc     TagOrTempResult
1640f4a2713aSLionel Sambuc       = Actions.ActOnExplicitInstantiation(getCurScope(),
1641f4a2713aSLionel Sambuc                                            TemplateInfo.ExternLoc,
1642f4a2713aSLionel Sambuc                                            TemplateInfo.TemplateLoc,
1643f4a2713aSLionel Sambuc                                            TagType, StartLoc, SS, Name,
1644f4a2713aSLionel Sambuc                                            NameLoc, attrs.getList());
1645f4a2713aSLionel Sambuc   } else if (TUK == Sema::TUK_Friend &&
1646f4a2713aSLionel Sambuc              TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) {
1647f4a2713aSLionel Sambuc     ProhibitAttributes(attrs);
1648f4a2713aSLionel Sambuc 
1649f4a2713aSLionel Sambuc     TagOrTempResult =
1650f4a2713aSLionel Sambuc       Actions.ActOnTemplatedFriendTag(getCurScope(), DS.getFriendSpecLoc(),
1651f4a2713aSLionel Sambuc                                       TagType, StartLoc, SS,
1652f4a2713aSLionel Sambuc                                       Name, NameLoc, attrs.getList(),
1653f4a2713aSLionel Sambuc                                       MultiTemplateParamsArg(
1654*0a6a1f1dSLionel Sambuc                                     TemplateParams? &(*TemplateParams)[0]
1655*0a6a1f1dSLionel Sambuc                                                   : nullptr,
1656f4a2713aSLionel Sambuc                                  TemplateParams? TemplateParams->size() : 0));
1657f4a2713aSLionel Sambuc   } else {
1658f4a2713aSLionel Sambuc     if (TUK != Sema::TUK_Declaration && TUK != Sema::TUK_Definition)
1659f4a2713aSLionel Sambuc       ProhibitAttributes(attrs);
1660f4a2713aSLionel Sambuc 
1661f4a2713aSLionel Sambuc     if (TUK == Sema::TUK_Definition &&
1662f4a2713aSLionel Sambuc         TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
1663f4a2713aSLionel Sambuc       // If the declarator-id is not a template-id, issue a diagnostic and
1664f4a2713aSLionel Sambuc       // recover by ignoring the 'template' keyword.
1665f4a2713aSLionel Sambuc       Diag(Tok, diag::err_template_defn_explicit_instantiation)
1666f4a2713aSLionel Sambuc         << 1 << FixItHint::CreateRemoval(TemplateInfo.TemplateLoc);
1667*0a6a1f1dSLionel Sambuc       TemplateParams = nullptr;
1668f4a2713aSLionel Sambuc     }
1669f4a2713aSLionel Sambuc 
1670f4a2713aSLionel Sambuc     bool IsDependent = false;
1671f4a2713aSLionel Sambuc 
1672f4a2713aSLionel Sambuc     // Don't pass down template parameter lists if this is just a tag
1673f4a2713aSLionel Sambuc     // reference.  For example, we don't need the template parameters here:
1674f4a2713aSLionel Sambuc     //   template <class T> class A *makeA(T t);
1675f4a2713aSLionel Sambuc     MultiTemplateParamsArg TParams;
1676f4a2713aSLionel Sambuc     if (TUK != Sema::TUK_Reference && TemplateParams)
1677f4a2713aSLionel Sambuc       TParams =
1678f4a2713aSLionel Sambuc         MultiTemplateParamsArg(&(*TemplateParams)[0], TemplateParams->size());
1679f4a2713aSLionel Sambuc 
1680f4a2713aSLionel Sambuc     // Declaration or definition of a class type
1681f4a2713aSLionel Sambuc     TagOrTempResult = Actions.ActOnTag(getCurScope(), TagType, TUK, StartLoc,
1682f4a2713aSLionel Sambuc                                        SS, Name, NameLoc, attrs.getList(), AS,
1683f4a2713aSLionel Sambuc                                        DS.getModulePrivateSpecLoc(),
1684f4a2713aSLionel Sambuc                                        TParams, Owned, IsDependent,
1685f4a2713aSLionel Sambuc                                        SourceLocation(), false,
1686*0a6a1f1dSLionel Sambuc                                        clang::TypeResult(),
1687*0a6a1f1dSLionel Sambuc                                        DSC == DSC_type_specifier);
1688f4a2713aSLionel Sambuc 
1689f4a2713aSLionel Sambuc     // If ActOnTag said the type was dependent, try again with the
1690f4a2713aSLionel Sambuc     // less common call.
1691f4a2713aSLionel Sambuc     if (IsDependent) {
1692f4a2713aSLionel Sambuc       assert(TUK == Sema::TUK_Reference || TUK == Sema::TUK_Friend);
1693f4a2713aSLionel Sambuc       TypeResult = Actions.ActOnDependentTag(getCurScope(), TagType, TUK,
1694f4a2713aSLionel Sambuc                                              SS, Name, StartLoc, NameLoc);
1695f4a2713aSLionel Sambuc     }
1696f4a2713aSLionel Sambuc   }
1697f4a2713aSLionel Sambuc 
1698f4a2713aSLionel Sambuc   // If there is a body, parse it and inform the actions module.
1699f4a2713aSLionel Sambuc   if (TUK == Sema::TUK_Definition) {
1700f4a2713aSLionel Sambuc     assert(Tok.is(tok::l_brace) ||
1701f4a2713aSLionel Sambuc            (getLangOpts().CPlusPlus && Tok.is(tok::colon)) ||
1702f4a2713aSLionel Sambuc            isCXX11FinalKeyword());
1703f4a2713aSLionel Sambuc     if (getLangOpts().CPlusPlus)
1704f4a2713aSLionel Sambuc       ParseCXXMemberSpecification(StartLoc, AttrFixitLoc, attrs, TagType,
1705f4a2713aSLionel Sambuc                                   TagOrTempResult.get());
1706f4a2713aSLionel Sambuc     else
1707f4a2713aSLionel Sambuc       ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
1708f4a2713aSLionel Sambuc   }
1709f4a2713aSLionel Sambuc 
1710*0a6a1f1dSLionel Sambuc   const char *PrevSpec = nullptr;
1711f4a2713aSLionel Sambuc   unsigned DiagID;
1712f4a2713aSLionel Sambuc   bool Result;
1713f4a2713aSLionel Sambuc   if (!TypeResult.isInvalid()) {
1714f4a2713aSLionel Sambuc     Result = DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
1715f4a2713aSLionel Sambuc                                 NameLoc.isValid() ? NameLoc : StartLoc,
1716*0a6a1f1dSLionel Sambuc                                 PrevSpec, DiagID, TypeResult.get(), Policy);
1717f4a2713aSLionel Sambuc   } else if (!TagOrTempResult.isInvalid()) {
1718f4a2713aSLionel Sambuc     Result = DS.SetTypeSpecType(TagType, StartLoc,
1719f4a2713aSLionel Sambuc                                 NameLoc.isValid() ? NameLoc : StartLoc,
1720*0a6a1f1dSLionel Sambuc                                 PrevSpec, DiagID, TagOrTempResult.get(), Owned,
1721*0a6a1f1dSLionel Sambuc                                 Policy);
1722f4a2713aSLionel Sambuc   } else {
1723f4a2713aSLionel Sambuc     DS.SetTypeSpecError();
1724f4a2713aSLionel Sambuc     return;
1725f4a2713aSLionel Sambuc   }
1726f4a2713aSLionel Sambuc 
1727f4a2713aSLionel Sambuc   if (Result)
1728f4a2713aSLionel Sambuc     Diag(StartLoc, DiagID) << PrevSpec;
1729f4a2713aSLionel Sambuc 
1730f4a2713aSLionel Sambuc   // At this point, we've successfully parsed a class-specifier in 'definition'
1731f4a2713aSLionel Sambuc   // form (e.g. "struct foo { int x; }".  While we could just return here, we're
1732f4a2713aSLionel Sambuc   // going to look at what comes after it to improve error recovery.  If an
1733f4a2713aSLionel Sambuc   // impossible token occurs next, we assume that the programmer forgot a ; at
1734f4a2713aSLionel Sambuc   // the end of the declaration and recover that way.
1735f4a2713aSLionel Sambuc   //
1736f4a2713aSLionel Sambuc   // Also enforce C++ [temp]p3:
1737f4a2713aSLionel Sambuc   //   In a template-declaration which defines a class, no declarator
1738f4a2713aSLionel Sambuc   //   is permitted.
1739*0a6a1f1dSLionel Sambuc   //
1740*0a6a1f1dSLionel Sambuc   // After a type-specifier, we don't expect a semicolon. This only happens in
1741*0a6a1f1dSLionel Sambuc   // C, since definitions are not permitted in this context in C++.
1742f4a2713aSLionel Sambuc   if (TUK == Sema::TUK_Definition &&
1743*0a6a1f1dSLionel Sambuc       (getLangOpts().CPlusPlus || !isTypeSpecifier(DSC)) &&
1744f4a2713aSLionel Sambuc       (TemplateInfo.Kind || !isValidAfterTypeSpecifier(false))) {
1745f4a2713aSLionel Sambuc     if (Tok.isNot(tok::semi)) {
1746*0a6a1f1dSLionel Sambuc       const PrintingPolicy &PPol = Actions.getASTContext().getPrintingPolicy();
1747*0a6a1f1dSLionel Sambuc       ExpectAndConsume(tok::semi, diag::err_expected_after,
1748*0a6a1f1dSLionel Sambuc                        DeclSpec::getSpecifierName(TagType, PPol));
1749f4a2713aSLionel Sambuc       // Push this token back into the preprocessor and change our current token
1750f4a2713aSLionel Sambuc       // to ';' so that the rest of the code recovers as though there were an
1751f4a2713aSLionel Sambuc       // ';' after the definition.
1752f4a2713aSLionel Sambuc       PP.EnterToken(Tok);
1753f4a2713aSLionel Sambuc       Tok.setKind(tok::semi);
1754f4a2713aSLionel Sambuc     }
1755f4a2713aSLionel Sambuc   }
1756f4a2713aSLionel Sambuc }
1757f4a2713aSLionel Sambuc 
1758f4a2713aSLionel Sambuc /// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
1759f4a2713aSLionel Sambuc ///
1760f4a2713aSLionel Sambuc ///       base-clause : [C++ class.derived]
1761f4a2713aSLionel Sambuc ///         ':' base-specifier-list
1762f4a2713aSLionel Sambuc ///       base-specifier-list:
1763f4a2713aSLionel Sambuc ///         base-specifier '...'[opt]
1764f4a2713aSLionel Sambuc ///         base-specifier-list ',' base-specifier '...'[opt]
ParseBaseClause(Decl * ClassDecl)1765f4a2713aSLionel Sambuc void Parser::ParseBaseClause(Decl *ClassDecl) {
1766f4a2713aSLionel Sambuc   assert(Tok.is(tok::colon) && "Not a base clause");
1767f4a2713aSLionel Sambuc   ConsumeToken();
1768f4a2713aSLionel Sambuc 
1769f4a2713aSLionel Sambuc   // Build up an array of parsed base specifiers.
1770f4a2713aSLionel Sambuc   SmallVector<CXXBaseSpecifier *, 8> BaseInfo;
1771f4a2713aSLionel Sambuc 
1772f4a2713aSLionel Sambuc   while (true) {
1773f4a2713aSLionel Sambuc     // Parse a base-specifier.
1774f4a2713aSLionel Sambuc     BaseResult Result = ParseBaseSpecifier(ClassDecl);
1775f4a2713aSLionel Sambuc     if (Result.isInvalid()) {
1776f4a2713aSLionel Sambuc       // Skip the rest of this base specifier, up until the comma or
1777f4a2713aSLionel Sambuc       // opening brace.
1778f4a2713aSLionel Sambuc       SkipUntil(tok::comma, tok::l_brace, StopAtSemi | StopBeforeMatch);
1779f4a2713aSLionel Sambuc     } else {
1780f4a2713aSLionel Sambuc       // Add this to our array of base specifiers.
1781f4a2713aSLionel Sambuc       BaseInfo.push_back(Result.get());
1782f4a2713aSLionel Sambuc     }
1783f4a2713aSLionel Sambuc 
1784f4a2713aSLionel Sambuc     // If the next token is a comma, consume it and keep reading
1785f4a2713aSLionel Sambuc     // base-specifiers.
1786*0a6a1f1dSLionel Sambuc     if (!TryConsumeToken(tok::comma))
1787*0a6a1f1dSLionel Sambuc       break;
1788f4a2713aSLionel Sambuc   }
1789f4a2713aSLionel Sambuc 
1790f4a2713aSLionel Sambuc   // Attach the base specifiers
1791f4a2713aSLionel Sambuc   Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size());
1792f4a2713aSLionel Sambuc }
1793f4a2713aSLionel Sambuc 
1794f4a2713aSLionel Sambuc /// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
1795f4a2713aSLionel Sambuc /// one entry in the base class list of a class specifier, for example:
1796f4a2713aSLionel Sambuc ///    class foo : public bar, virtual private baz {
1797f4a2713aSLionel Sambuc /// 'public bar' and 'virtual private baz' are each base-specifiers.
1798f4a2713aSLionel Sambuc ///
1799f4a2713aSLionel Sambuc ///       base-specifier: [C++ class.derived]
1800f4a2713aSLionel Sambuc ///         attribute-specifier-seq[opt] base-type-specifier
1801f4a2713aSLionel Sambuc ///         attribute-specifier-seq[opt] 'virtual' access-specifier[opt]
1802f4a2713aSLionel Sambuc ///                 base-type-specifier
1803f4a2713aSLionel Sambuc ///         attribute-specifier-seq[opt] access-specifier 'virtual'[opt]
1804f4a2713aSLionel Sambuc ///                 base-type-specifier
ParseBaseSpecifier(Decl * ClassDecl)1805*0a6a1f1dSLionel Sambuc BaseResult Parser::ParseBaseSpecifier(Decl *ClassDecl) {
1806f4a2713aSLionel Sambuc   bool IsVirtual = false;
1807f4a2713aSLionel Sambuc   SourceLocation StartLoc = Tok.getLocation();
1808f4a2713aSLionel Sambuc 
1809f4a2713aSLionel Sambuc   ParsedAttributesWithRange Attributes(AttrFactory);
1810f4a2713aSLionel Sambuc   MaybeParseCXX11Attributes(Attributes);
1811f4a2713aSLionel Sambuc 
1812f4a2713aSLionel Sambuc   // Parse the 'virtual' keyword.
1813*0a6a1f1dSLionel Sambuc   if (TryConsumeToken(tok::kw_virtual))
1814f4a2713aSLionel Sambuc     IsVirtual = true;
1815f4a2713aSLionel Sambuc 
1816f4a2713aSLionel Sambuc   CheckMisplacedCXX11Attribute(Attributes, StartLoc);
1817f4a2713aSLionel Sambuc 
1818f4a2713aSLionel Sambuc   // Parse an (optional) access specifier.
1819f4a2713aSLionel Sambuc   AccessSpecifier Access = getAccessSpecifierIfPresent();
1820f4a2713aSLionel Sambuc   if (Access != AS_none)
1821f4a2713aSLionel Sambuc     ConsumeToken();
1822f4a2713aSLionel Sambuc 
1823f4a2713aSLionel Sambuc   CheckMisplacedCXX11Attribute(Attributes, StartLoc);
1824f4a2713aSLionel Sambuc 
1825f4a2713aSLionel Sambuc   // Parse the 'virtual' keyword (again!), in case it came after the
1826f4a2713aSLionel Sambuc   // access specifier.
1827f4a2713aSLionel Sambuc   if (Tok.is(tok::kw_virtual))  {
1828f4a2713aSLionel Sambuc     SourceLocation VirtualLoc = ConsumeToken();
1829f4a2713aSLionel Sambuc     if (IsVirtual) {
1830f4a2713aSLionel Sambuc       // Complain about duplicate 'virtual'
1831f4a2713aSLionel Sambuc       Diag(VirtualLoc, diag::err_dup_virtual)
1832f4a2713aSLionel Sambuc         << FixItHint::CreateRemoval(VirtualLoc);
1833f4a2713aSLionel Sambuc     }
1834f4a2713aSLionel Sambuc 
1835f4a2713aSLionel Sambuc     IsVirtual = true;
1836f4a2713aSLionel Sambuc   }
1837f4a2713aSLionel Sambuc 
1838f4a2713aSLionel Sambuc   CheckMisplacedCXX11Attribute(Attributes, StartLoc);
1839f4a2713aSLionel Sambuc 
1840f4a2713aSLionel Sambuc   // Parse the class-name.
1841f4a2713aSLionel Sambuc   SourceLocation EndLocation;
1842f4a2713aSLionel Sambuc   SourceLocation BaseLoc;
1843f4a2713aSLionel Sambuc   TypeResult BaseType = ParseBaseTypeSpecifier(BaseLoc, EndLocation);
1844f4a2713aSLionel Sambuc   if (BaseType.isInvalid())
1845f4a2713aSLionel Sambuc     return true;
1846f4a2713aSLionel Sambuc 
1847f4a2713aSLionel Sambuc   // Parse the optional ellipsis (for a pack expansion). The ellipsis is
1848f4a2713aSLionel Sambuc   // actually part of the base-specifier-list grammar productions, but we
1849f4a2713aSLionel Sambuc   // parse it here for convenience.
1850f4a2713aSLionel Sambuc   SourceLocation EllipsisLoc;
1851*0a6a1f1dSLionel Sambuc   TryConsumeToken(tok::ellipsis, EllipsisLoc);
1852f4a2713aSLionel Sambuc 
1853f4a2713aSLionel Sambuc   // Find the complete source range for the base-specifier.
1854f4a2713aSLionel Sambuc   SourceRange Range(StartLoc, EndLocation);
1855f4a2713aSLionel Sambuc 
1856f4a2713aSLionel Sambuc   // Notify semantic analysis that we have parsed a complete
1857f4a2713aSLionel Sambuc   // base-specifier.
1858f4a2713aSLionel Sambuc   return Actions.ActOnBaseSpecifier(ClassDecl, Range, Attributes, IsVirtual,
1859f4a2713aSLionel Sambuc                                     Access, BaseType.get(), BaseLoc,
1860f4a2713aSLionel Sambuc                                     EllipsisLoc);
1861f4a2713aSLionel Sambuc }
1862f4a2713aSLionel Sambuc 
1863f4a2713aSLionel Sambuc /// getAccessSpecifierIfPresent - Determine whether the next token is
1864f4a2713aSLionel Sambuc /// a C++ access-specifier.
1865f4a2713aSLionel Sambuc ///
1866f4a2713aSLionel Sambuc ///       access-specifier: [C++ class.derived]
1867f4a2713aSLionel Sambuc ///         'private'
1868f4a2713aSLionel Sambuc ///         'protected'
1869f4a2713aSLionel Sambuc ///         'public'
getAccessSpecifierIfPresent() const1870f4a2713aSLionel Sambuc AccessSpecifier Parser::getAccessSpecifierIfPresent() const {
1871f4a2713aSLionel Sambuc   switch (Tok.getKind()) {
1872f4a2713aSLionel Sambuc   default: return AS_none;
1873f4a2713aSLionel Sambuc   case tok::kw_private: return AS_private;
1874f4a2713aSLionel Sambuc   case tok::kw_protected: return AS_protected;
1875f4a2713aSLionel Sambuc   case tok::kw_public: return AS_public;
1876f4a2713aSLionel Sambuc   }
1877f4a2713aSLionel Sambuc }
1878f4a2713aSLionel Sambuc 
1879f4a2713aSLionel Sambuc /// \brief If the given declarator has any parts for which parsing has to be
1880*0a6a1f1dSLionel Sambuc /// delayed, e.g., default arguments or an exception-specification, create a
1881*0a6a1f1dSLionel Sambuc /// late-parsed method declaration record to handle the parsing at the end of
1882*0a6a1f1dSLionel Sambuc /// the class definition.
HandleMemberFunctionDeclDelays(Declarator & DeclaratorInfo,Decl * ThisDecl)1883f4a2713aSLionel Sambuc void Parser::HandleMemberFunctionDeclDelays(Declarator& DeclaratorInfo,
1884f4a2713aSLionel Sambuc                                             Decl *ThisDecl) {
1885f4a2713aSLionel Sambuc   // We just declared a member function. If this member function
1886*0a6a1f1dSLionel Sambuc   // has any default arguments or an exception-specification, we'll need to
1887*0a6a1f1dSLionel Sambuc   // parse them later.
1888*0a6a1f1dSLionel Sambuc   LateParsedMethodDeclaration *LateMethod = nullptr;
1889f4a2713aSLionel Sambuc   DeclaratorChunk::FunctionTypeInfo &FTI
1890f4a2713aSLionel Sambuc     = DeclaratorInfo.getFunctionTypeInfo();
1891f4a2713aSLionel Sambuc 
1892*0a6a1f1dSLionel Sambuc   // If there was a late-parsed exception-specification, hold onto its tokens.
1893*0a6a1f1dSLionel Sambuc   if (FTI.getExceptionSpecType() == EST_Unparsed) {
1894*0a6a1f1dSLionel Sambuc     // Push this method onto the stack of late-parsed method
1895*0a6a1f1dSLionel Sambuc     // declarations.
1896*0a6a1f1dSLionel Sambuc     LateMethod = new LateParsedMethodDeclaration(this, ThisDecl);
1897*0a6a1f1dSLionel Sambuc     getCurrentClass().LateParsedDeclarations.push_back(LateMethod);
1898*0a6a1f1dSLionel Sambuc     LateMethod->TemplateScope = getCurScope()->isTemplateParamScope();
1899*0a6a1f1dSLionel Sambuc 
1900*0a6a1f1dSLionel Sambuc     // Stash the exception-specification tokens in the late-pased mthod.
1901*0a6a1f1dSLionel Sambuc     LateMethod->ExceptionSpecTokens = FTI.ExceptionSpecTokens;
1902*0a6a1f1dSLionel Sambuc     FTI.ExceptionSpecTokens = 0;
1903*0a6a1f1dSLionel Sambuc 
1904*0a6a1f1dSLionel Sambuc     // Reserve space for the parameters.
1905*0a6a1f1dSLionel Sambuc     LateMethod->DefaultArgs.reserve(FTI.NumParams);
1906*0a6a1f1dSLionel Sambuc   }
1907*0a6a1f1dSLionel Sambuc 
1908*0a6a1f1dSLionel Sambuc   for (unsigned ParamIdx = 0; ParamIdx < FTI.NumParams; ++ParamIdx) {
1909*0a6a1f1dSLionel Sambuc     if (LateMethod || FTI.Params[ParamIdx].DefaultArgTokens) {
1910f4a2713aSLionel Sambuc       if (!LateMethod) {
1911f4a2713aSLionel Sambuc         // Push this method onto the stack of late-parsed method
1912f4a2713aSLionel Sambuc         // declarations.
1913f4a2713aSLionel Sambuc         LateMethod = new LateParsedMethodDeclaration(this, ThisDecl);
1914f4a2713aSLionel Sambuc         getCurrentClass().LateParsedDeclarations.push_back(LateMethod);
1915f4a2713aSLionel Sambuc         LateMethod->TemplateScope = getCurScope()->isTemplateParamScope();
1916f4a2713aSLionel Sambuc 
1917f4a2713aSLionel Sambuc         // Add all of the parameters prior to this one (they don't
1918f4a2713aSLionel Sambuc         // have default arguments).
1919*0a6a1f1dSLionel Sambuc         LateMethod->DefaultArgs.reserve(FTI.NumParams);
1920f4a2713aSLionel Sambuc         for (unsigned I = 0; I < ParamIdx; ++I)
1921f4a2713aSLionel Sambuc           LateMethod->DefaultArgs.push_back(
1922*0a6a1f1dSLionel Sambuc               LateParsedDefaultArgument(FTI.Params[I].Param));
1923f4a2713aSLionel Sambuc       }
1924f4a2713aSLionel Sambuc 
1925f4a2713aSLionel Sambuc       // Add this parameter to the list of parameters (it may or may
1926f4a2713aSLionel Sambuc       // not have a default argument).
1927*0a6a1f1dSLionel Sambuc       LateMethod->DefaultArgs.push_back(LateParsedDefaultArgument(
1928*0a6a1f1dSLionel Sambuc           FTI.Params[ParamIdx].Param, FTI.Params[ParamIdx].DefaultArgTokens));
1929f4a2713aSLionel Sambuc     }
1930f4a2713aSLionel Sambuc   }
1931f4a2713aSLionel Sambuc }
1932f4a2713aSLionel Sambuc 
1933f4a2713aSLionel Sambuc /// isCXX11VirtSpecifier - Determine whether the given token is a C++11
1934f4a2713aSLionel Sambuc /// virt-specifier.
1935f4a2713aSLionel Sambuc ///
1936f4a2713aSLionel Sambuc ///       virt-specifier:
1937f4a2713aSLionel Sambuc ///         override
1938f4a2713aSLionel Sambuc ///         final
isCXX11VirtSpecifier(const Token & Tok) const1939f4a2713aSLionel Sambuc VirtSpecifiers::Specifier Parser::isCXX11VirtSpecifier(const Token &Tok) const {
1940*0a6a1f1dSLionel Sambuc   if (!getLangOpts().CPlusPlus || Tok.isNot(tok::identifier))
1941f4a2713aSLionel Sambuc     return VirtSpecifiers::VS_None;
1942f4a2713aSLionel Sambuc 
1943f4a2713aSLionel Sambuc   IdentifierInfo *II = Tok.getIdentifierInfo();
1944f4a2713aSLionel Sambuc 
1945f4a2713aSLionel Sambuc   // Initialize the contextual keywords.
1946f4a2713aSLionel Sambuc   if (!Ident_final) {
1947f4a2713aSLionel Sambuc     Ident_final = &PP.getIdentifierTable().get("final");
1948f4a2713aSLionel Sambuc     if (getLangOpts().MicrosoftExt)
1949f4a2713aSLionel Sambuc       Ident_sealed = &PP.getIdentifierTable().get("sealed");
1950f4a2713aSLionel Sambuc     Ident_override = &PP.getIdentifierTable().get("override");
1951f4a2713aSLionel Sambuc   }
1952f4a2713aSLionel Sambuc 
1953f4a2713aSLionel Sambuc   if (II == Ident_override)
1954f4a2713aSLionel Sambuc     return VirtSpecifiers::VS_Override;
1955f4a2713aSLionel Sambuc 
1956f4a2713aSLionel Sambuc   if (II == Ident_sealed)
1957f4a2713aSLionel Sambuc     return VirtSpecifiers::VS_Sealed;
1958f4a2713aSLionel Sambuc 
1959f4a2713aSLionel Sambuc   if (II == Ident_final)
1960f4a2713aSLionel Sambuc     return VirtSpecifiers::VS_Final;
1961f4a2713aSLionel Sambuc 
1962f4a2713aSLionel Sambuc   return VirtSpecifiers::VS_None;
1963f4a2713aSLionel Sambuc }
1964f4a2713aSLionel Sambuc 
1965f4a2713aSLionel Sambuc /// ParseOptionalCXX11VirtSpecifierSeq - Parse a virt-specifier-seq.
1966f4a2713aSLionel Sambuc ///
1967f4a2713aSLionel Sambuc ///       virt-specifier-seq:
1968f4a2713aSLionel Sambuc ///         virt-specifier
1969f4a2713aSLionel Sambuc ///         virt-specifier-seq virt-specifier
ParseOptionalCXX11VirtSpecifierSeq(VirtSpecifiers & VS,bool IsInterface,SourceLocation FriendLoc)1970f4a2713aSLionel Sambuc void Parser::ParseOptionalCXX11VirtSpecifierSeq(VirtSpecifiers &VS,
1971*0a6a1f1dSLionel Sambuc                                                 bool IsInterface,
1972*0a6a1f1dSLionel Sambuc                                                 SourceLocation FriendLoc) {
1973f4a2713aSLionel Sambuc   while (true) {
1974f4a2713aSLionel Sambuc     VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier();
1975f4a2713aSLionel Sambuc     if (Specifier == VirtSpecifiers::VS_None)
1976f4a2713aSLionel Sambuc       return;
1977f4a2713aSLionel Sambuc 
1978*0a6a1f1dSLionel Sambuc     if (FriendLoc.isValid()) {
1979*0a6a1f1dSLionel Sambuc       Diag(Tok.getLocation(), diag::err_friend_decl_spec)
1980*0a6a1f1dSLionel Sambuc         << VirtSpecifiers::getSpecifierName(Specifier)
1981*0a6a1f1dSLionel Sambuc         << FixItHint::CreateRemoval(Tok.getLocation())
1982*0a6a1f1dSLionel Sambuc         << SourceRange(FriendLoc, FriendLoc);
1983*0a6a1f1dSLionel Sambuc       ConsumeToken();
1984*0a6a1f1dSLionel Sambuc       continue;
1985*0a6a1f1dSLionel Sambuc     }
1986*0a6a1f1dSLionel Sambuc 
1987f4a2713aSLionel Sambuc     // C++ [class.mem]p8:
1988f4a2713aSLionel Sambuc     //   A virt-specifier-seq shall contain at most one of each virt-specifier.
1989*0a6a1f1dSLionel Sambuc     const char *PrevSpec = nullptr;
1990f4a2713aSLionel Sambuc     if (VS.SetSpecifier(Specifier, Tok.getLocation(), PrevSpec))
1991f4a2713aSLionel Sambuc       Diag(Tok.getLocation(), diag::err_duplicate_virt_specifier)
1992f4a2713aSLionel Sambuc         << PrevSpec
1993f4a2713aSLionel Sambuc         << FixItHint::CreateRemoval(Tok.getLocation());
1994f4a2713aSLionel Sambuc 
1995f4a2713aSLionel Sambuc     if (IsInterface && (Specifier == VirtSpecifiers::VS_Final ||
1996f4a2713aSLionel Sambuc                         Specifier == VirtSpecifiers::VS_Sealed)) {
1997f4a2713aSLionel Sambuc       Diag(Tok.getLocation(), diag::err_override_control_interface)
1998f4a2713aSLionel Sambuc         << VirtSpecifiers::getSpecifierName(Specifier);
1999f4a2713aSLionel Sambuc     } else if (Specifier == VirtSpecifiers::VS_Sealed) {
2000f4a2713aSLionel Sambuc       Diag(Tok.getLocation(), diag::ext_ms_sealed_keyword);
2001f4a2713aSLionel Sambuc     } else {
2002f4a2713aSLionel Sambuc       Diag(Tok.getLocation(),
2003f4a2713aSLionel Sambuc            getLangOpts().CPlusPlus11
2004f4a2713aSLionel Sambuc                ? diag::warn_cxx98_compat_override_control_keyword
2005f4a2713aSLionel Sambuc                : diag::ext_override_control_keyword)
2006f4a2713aSLionel Sambuc           << VirtSpecifiers::getSpecifierName(Specifier);
2007f4a2713aSLionel Sambuc     }
2008f4a2713aSLionel Sambuc     ConsumeToken();
2009f4a2713aSLionel Sambuc   }
2010f4a2713aSLionel Sambuc }
2011f4a2713aSLionel Sambuc 
2012f4a2713aSLionel Sambuc /// isCXX11FinalKeyword - Determine whether the next token is a C++11
2013*0a6a1f1dSLionel Sambuc /// 'final' or Microsoft 'sealed' contextual keyword.
isCXX11FinalKeyword() const2014f4a2713aSLionel Sambuc bool Parser::isCXX11FinalKeyword() const {
2015*0a6a1f1dSLionel Sambuc   VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier();
2016*0a6a1f1dSLionel Sambuc   return Specifier == VirtSpecifiers::VS_Final ||
2017*0a6a1f1dSLionel Sambuc          Specifier == VirtSpecifiers::VS_Sealed;
2018f4a2713aSLionel Sambuc }
2019f4a2713aSLionel Sambuc 
2020*0a6a1f1dSLionel Sambuc /// \brief Parse a C++ member-declarator up to, but not including, the optional
2021*0a6a1f1dSLionel Sambuc /// brace-or-equal-initializer or pure-specifier.
ParseCXXMemberDeclaratorBeforeInitializer(Declarator & DeclaratorInfo,VirtSpecifiers & VS,ExprResult & BitfieldSize,LateParsedAttrList & LateParsedAttrs)2022*0a6a1f1dSLionel Sambuc void Parser::ParseCXXMemberDeclaratorBeforeInitializer(
2023*0a6a1f1dSLionel Sambuc     Declarator &DeclaratorInfo, VirtSpecifiers &VS, ExprResult &BitfieldSize,
2024*0a6a1f1dSLionel Sambuc     LateParsedAttrList &LateParsedAttrs) {
2025*0a6a1f1dSLionel Sambuc   // member-declarator:
2026*0a6a1f1dSLionel Sambuc   //   declarator pure-specifier[opt]
2027*0a6a1f1dSLionel Sambuc   //   declarator brace-or-equal-initializer[opt]
2028*0a6a1f1dSLionel Sambuc   //   identifier[opt] ':' constant-expression
2029*0a6a1f1dSLionel Sambuc   if (Tok.isNot(tok::colon))
2030*0a6a1f1dSLionel Sambuc     ParseDeclarator(DeclaratorInfo);
2031*0a6a1f1dSLionel Sambuc   else
2032*0a6a1f1dSLionel Sambuc     DeclaratorInfo.SetIdentifier(nullptr, Tok.getLocation());
2033*0a6a1f1dSLionel Sambuc 
2034*0a6a1f1dSLionel Sambuc   if (!DeclaratorInfo.isFunctionDeclarator() && TryConsumeToken(tok::colon)) {
2035*0a6a1f1dSLionel Sambuc     assert(DeclaratorInfo.isPastIdentifier() &&
2036*0a6a1f1dSLionel Sambuc            "don't know where identifier would go yet?");
2037*0a6a1f1dSLionel Sambuc     BitfieldSize = ParseConstantExpression();
2038*0a6a1f1dSLionel Sambuc     if (BitfieldSize.isInvalid())
2039*0a6a1f1dSLionel Sambuc       SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
2040*0a6a1f1dSLionel Sambuc   } else
2041*0a6a1f1dSLionel Sambuc     ParseOptionalCXX11VirtSpecifierSeq(
2042*0a6a1f1dSLionel Sambuc         VS, getCurrentClass().IsInterface,
2043*0a6a1f1dSLionel Sambuc         DeclaratorInfo.getDeclSpec().getFriendSpecLoc());
2044*0a6a1f1dSLionel Sambuc 
2045*0a6a1f1dSLionel Sambuc   // If a simple-asm-expr is present, parse it.
2046*0a6a1f1dSLionel Sambuc   if (Tok.is(tok::kw_asm)) {
2047*0a6a1f1dSLionel Sambuc     SourceLocation Loc;
2048*0a6a1f1dSLionel Sambuc     ExprResult AsmLabel(ParseSimpleAsm(&Loc));
2049*0a6a1f1dSLionel Sambuc     if (AsmLabel.isInvalid())
2050*0a6a1f1dSLionel Sambuc       SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
2051*0a6a1f1dSLionel Sambuc 
2052*0a6a1f1dSLionel Sambuc     DeclaratorInfo.setAsmLabel(AsmLabel.get());
2053*0a6a1f1dSLionel Sambuc     DeclaratorInfo.SetRangeEnd(Loc);
2054*0a6a1f1dSLionel Sambuc   }
2055*0a6a1f1dSLionel Sambuc 
2056*0a6a1f1dSLionel Sambuc   // If attributes exist after the declarator, but before an '{', parse them.
2057*0a6a1f1dSLionel Sambuc   MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs);
2058*0a6a1f1dSLionel Sambuc 
2059*0a6a1f1dSLionel Sambuc   // For compatibility with code written to older Clang, also accept a
2060*0a6a1f1dSLionel Sambuc   // virt-specifier *after* the GNU attributes.
2061*0a6a1f1dSLionel Sambuc   if (BitfieldSize.isUnset() && VS.isUnset()) {
2062*0a6a1f1dSLionel Sambuc     ParseOptionalCXX11VirtSpecifierSeq(
2063*0a6a1f1dSLionel Sambuc         VS, getCurrentClass().IsInterface,
2064*0a6a1f1dSLionel Sambuc         DeclaratorInfo.getDeclSpec().getFriendSpecLoc());
2065*0a6a1f1dSLionel Sambuc     if (!VS.isUnset()) {
2066*0a6a1f1dSLionel Sambuc       // If we saw any GNU-style attributes that are known to GCC followed by a
2067*0a6a1f1dSLionel Sambuc       // virt-specifier, issue a GCC-compat warning.
2068*0a6a1f1dSLionel Sambuc       const AttributeList *Attr = DeclaratorInfo.getAttributes();
2069*0a6a1f1dSLionel Sambuc       while (Attr) {
2070*0a6a1f1dSLionel Sambuc         if (Attr->isKnownToGCC() && !Attr->isCXX11Attribute())
2071*0a6a1f1dSLionel Sambuc           Diag(Attr->getLoc(), diag::warn_gcc_attribute_location);
2072*0a6a1f1dSLionel Sambuc         Attr = Attr->getNext();
2073*0a6a1f1dSLionel Sambuc       }
2074*0a6a1f1dSLionel Sambuc     }
2075*0a6a1f1dSLionel Sambuc   }
2076f4a2713aSLionel Sambuc }
2077f4a2713aSLionel Sambuc 
2078f4a2713aSLionel Sambuc /// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
2079f4a2713aSLionel Sambuc ///
2080f4a2713aSLionel Sambuc ///       member-declaration:
2081f4a2713aSLionel Sambuc ///         decl-specifier-seq[opt] member-declarator-list[opt] ';'
2082f4a2713aSLionel Sambuc ///         function-definition ';'[opt]
2083f4a2713aSLionel Sambuc ///         ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
2084f4a2713aSLionel Sambuc ///         using-declaration                                            [TODO]
2085f4a2713aSLionel Sambuc /// [C++0x] static_assert-declaration
2086f4a2713aSLionel Sambuc ///         template-declaration
2087f4a2713aSLionel Sambuc /// [GNU]   '__extension__' member-declaration
2088f4a2713aSLionel Sambuc ///
2089f4a2713aSLionel Sambuc ///       member-declarator-list:
2090f4a2713aSLionel Sambuc ///         member-declarator
2091f4a2713aSLionel Sambuc ///         member-declarator-list ',' member-declarator
2092f4a2713aSLionel Sambuc ///
2093f4a2713aSLionel Sambuc ///       member-declarator:
2094f4a2713aSLionel Sambuc ///         declarator virt-specifier-seq[opt] pure-specifier[opt]
2095f4a2713aSLionel Sambuc ///         declarator constant-initializer[opt]
2096f4a2713aSLionel Sambuc /// [C++11] declarator brace-or-equal-initializer[opt]
2097f4a2713aSLionel Sambuc ///         identifier[opt] ':' constant-expression
2098f4a2713aSLionel Sambuc ///
2099f4a2713aSLionel Sambuc ///       virt-specifier-seq:
2100f4a2713aSLionel Sambuc ///         virt-specifier
2101f4a2713aSLionel Sambuc ///         virt-specifier-seq virt-specifier
2102f4a2713aSLionel Sambuc ///
2103f4a2713aSLionel Sambuc ///       virt-specifier:
2104f4a2713aSLionel Sambuc ///         override
2105f4a2713aSLionel Sambuc ///         final
2106f4a2713aSLionel Sambuc /// [MS]    sealed
2107f4a2713aSLionel Sambuc ///
2108f4a2713aSLionel Sambuc ///       pure-specifier:
2109f4a2713aSLionel Sambuc ///         '= 0'
2110f4a2713aSLionel Sambuc ///
2111f4a2713aSLionel Sambuc ///       constant-initializer:
2112f4a2713aSLionel Sambuc ///         '=' constant-expression
2113f4a2713aSLionel Sambuc ///
ParseCXXClassMemberDeclaration(AccessSpecifier AS,AttributeList * AccessAttrs,const ParsedTemplateInfo & TemplateInfo,ParsingDeclRAIIObject * TemplateDiags)2114f4a2713aSLionel Sambuc void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS,
2115f4a2713aSLionel Sambuc                                             AttributeList *AccessAttrs,
2116f4a2713aSLionel Sambuc                                        const ParsedTemplateInfo &TemplateInfo,
2117f4a2713aSLionel Sambuc                                        ParsingDeclRAIIObject *TemplateDiags) {
2118f4a2713aSLionel Sambuc   if (Tok.is(tok::at)) {
2119f4a2713aSLionel Sambuc     if (getLangOpts().ObjC1 && NextToken().isObjCAtKeyword(tok::objc_defs))
2120f4a2713aSLionel Sambuc       Diag(Tok, diag::err_at_defs_cxx);
2121f4a2713aSLionel Sambuc     else
2122f4a2713aSLionel Sambuc       Diag(Tok, diag::err_at_in_class);
2123f4a2713aSLionel Sambuc 
2124f4a2713aSLionel Sambuc     ConsumeToken();
2125f4a2713aSLionel Sambuc     SkipUntil(tok::r_brace, StopAtSemi);
2126f4a2713aSLionel Sambuc     return;
2127f4a2713aSLionel Sambuc   }
2128f4a2713aSLionel Sambuc 
2129*0a6a1f1dSLionel Sambuc   // Turn on colon protection early, while parsing declspec, although there is
2130*0a6a1f1dSLionel Sambuc   // nothing to protect there. It prevents from false errors if error recovery
2131*0a6a1f1dSLionel Sambuc   // incorrectly determines where the declspec ends, as in the example:
2132*0a6a1f1dSLionel Sambuc   //   struct A { enum class B { C }; };
2133*0a6a1f1dSLionel Sambuc   //   const int C = 4;
2134*0a6a1f1dSLionel Sambuc   //   struct D { A::B : C; };
2135*0a6a1f1dSLionel Sambuc   ColonProtectionRAIIObject X(*this);
2136*0a6a1f1dSLionel Sambuc 
2137f4a2713aSLionel Sambuc   // Access declarations.
2138f4a2713aSLionel Sambuc   bool MalformedTypeSpec = false;
2139f4a2713aSLionel Sambuc   if (!TemplateInfo.Kind &&
2140*0a6a1f1dSLionel Sambuc       (Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
2141*0a6a1f1dSLionel Sambuc        Tok.is(tok::kw___super))) {
2142f4a2713aSLionel Sambuc     if (TryAnnotateCXXScopeToken())
2143f4a2713aSLionel Sambuc       MalformedTypeSpec = true;
2144f4a2713aSLionel Sambuc 
2145f4a2713aSLionel Sambuc     bool isAccessDecl;
2146f4a2713aSLionel Sambuc     if (Tok.isNot(tok::annot_cxxscope))
2147f4a2713aSLionel Sambuc       isAccessDecl = false;
2148f4a2713aSLionel Sambuc     else if (NextToken().is(tok::identifier))
2149f4a2713aSLionel Sambuc       isAccessDecl = GetLookAheadToken(2).is(tok::semi);
2150f4a2713aSLionel Sambuc     else
2151f4a2713aSLionel Sambuc       isAccessDecl = NextToken().is(tok::kw_operator);
2152f4a2713aSLionel Sambuc 
2153f4a2713aSLionel Sambuc     if (isAccessDecl) {
2154f4a2713aSLionel Sambuc       // Collect the scope specifier token we annotated earlier.
2155f4a2713aSLionel Sambuc       CXXScopeSpec SS;
2156f4a2713aSLionel Sambuc       ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
2157f4a2713aSLionel Sambuc                                      /*EnteringContext=*/false);
2158f4a2713aSLionel Sambuc 
2159*0a6a1f1dSLionel Sambuc       if (SS.isInvalid()) {
2160*0a6a1f1dSLionel Sambuc         SkipUntil(tok::semi);
2161*0a6a1f1dSLionel Sambuc         return;
2162*0a6a1f1dSLionel Sambuc       }
2163*0a6a1f1dSLionel Sambuc 
2164f4a2713aSLionel Sambuc       // Try to parse an unqualified-id.
2165f4a2713aSLionel Sambuc       SourceLocation TemplateKWLoc;
2166f4a2713aSLionel Sambuc       UnqualifiedId Name;
2167f4a2713aSLionel Sambuc       if (ParseUnqualifiedId(SS, false, true, true, ParsedType(),
2168f4a2713aSLionel Sambuc                              TemplateKWLoc, Name)) {
2169f4a2713aSLionel Sambuc         SkipUntil(tok::semi);
2170f4a2713aSLionel Sambuc         return;
2171f4a2713aSLionel Sambuc       }
2172f4a2713aSLionel Sambuc 
2173f4a2713aSLionel Sambuc       // TODO: recover from mistakenly-qualified operator declarations.
2174*0a6a1f1dSLionel Sambuc       if (ExpectAndConsume(tok::semi, diag::err_expected_after,
2175*0a6a1f1dSLionel Sambuc                            "access declaration")) {
2176*0a6a1f1dSLionel Sambuc         SkipUntil(tok::semi);
2177f4a2713aSLionel Sambuc         return;
2178*0a6a1f1dSLionel Sambuc       }
2179f4a2713aSLionel Sambuc 
2180f4a2713aSLionel Sambuc       Actions.ActOnUsingDeclaration(getCurScope(), AS,
2181f4a2713aSLionel Sambuc                                     /* HasUsingKeyword */ false,
2182f4a2713aSLionel Sambuc                                     SourceLocation(),
2183f4a2713aSLionel Sambuc                                     SS, Name,
2184*0a6a1f1dSLionel Sambuc                                     /* AttrList */ nullptr,
2185f4a2713aSLionel Sambuc                                     /* HasTypenameKeyword */ false,
2186f4a2713aSLionel Sambuc                                     SourceLocation());
2187f4a2713aSLionel Sambuc       return;
2188f4a2713aSLionel Sambuc     }
2189f4a2713aSLionel Sambuc   }
2190f4a2713aSLionel Sambuc 
2191*0a6a1f1dSLionel Sambuc   // static_assert-declaration. A templated static_assert declaration is
2192*0a6a1f1dSLionel Sambuc   // diagnosed in Parser::ParseSingleDeclarationAfterTemplate.
2193*0a6a1f1dSLionel Sambuc   if (!TemplateInfo.Kind &&
2194*0a6a1f1dSLionel Sambuc       (Tok.is(tok::kw_static_assert) || Tok.is(tok::kw__Static_assert))) {
2195f4a2713aSLionel Sambuc     SourceLocation DeclEnd;
2196f4a2713aSLionel Sambuc     ParseStaticAssertDeclaration(DeclEnd);
2197f4a2713aSLionel Sambuc     return;
2198f4a2713aSLionel Sambuc   }
2199f4a2713aSLionel Sambuc 
2200f4a2713aSLionel Sambuc   if (Tok.is(tok::kw_template)) {
2201f4a2713aSLionel Sambuc     assert(!TemplateInfo.TemplateParams &&
2202f4a2713aSLionel Sambuc            "Nested template improperly parsed?");
2203f4a2713aSLionel Sambuc     SourceLocation DeclEnd;
2204f4a2713aSLionel Sambuc     ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd,
2205f4a2713aSLionel Sambuc                                          AS, AccessAttrs);
2206f4a2713aSLionel Sambuc     return;
2207f4a2713aSLionel Sambuc   }
2208f4a2713aSLionel Sambuc 
2209f4a2713aSLionel Sambuc   // Handle:  member-declaration ::= '__extension__' member-declaration
2210f4a2713aSLionel Sambuc   if (Tok.is(tok::kw___extension__)) {
2211f4a2713aSLionel Sambuc     // __extension__ silences extension warnings in the subexpression.
2212f4a2713aSLionel Sambuc     ExtensionRAIIObject O(Diags);  // Use RAII to do this.
2213f4a2713aSLionel Sambuc     ConsumeToken();
2214f4a2713aSLionel Sambuc     return ParseCXXClassMemberDeclaration(AS, AccessAttrs,
2215f4a2713aSLionel Sambuc                                           TemplateInfo, TemplateDiags);
2216f4a2713aSLionel Sambuc   }
2217f4a2713aSLionel Sambuc 
2218f4a2713aSLionel Sambuc   ParsedAttributesWithRange attrs(AttrFactory);
2219f4a2713aSLionel Sambuc   ParsedAttributesWithRange FnAttrs(AttrFactory);
2220f4a2713aSLionel Sambuc   // Optional C++11 attribute-specifier
2221f4a2713aSLionel Sambuc   MaybeParseCXX11Attributes(attrs);
2222f4a2713aSLionel Sambuc   // We need to keep these attributes for future diagnostic
2223f4a2713aSLionel Sambuc   // before they are taken over by declaration specifier.
2224f4a2713aSLionel Sambuc   FnAttrs.addAll(attrs.getList());
2225f4a2713aSLionel Sambuc   FnAttrs.Range = attrs.Range;
2226f4a2713aSLionel Sambuc 
2227f4a2713aSLionel Sambuc   MaybeParseMicrosoftAttributes(attrs);
2228f4a2713aSLionel Sambuc 
2229f4a2713aSLionel Sambuc   if (Tok.is(tok::kw_using)) {
2230f4a2713aSLionel Sambuc     ProhibitAttributes(attrs);
2231f4a2713aSLionel Sambuc 
2232f4a2713aSLionel Sambuc     // Eat 'using'.
2233f4a2713aSLionel Sambuc     SourceLocation UsingLoc = ConsumeToken();
2234f4a2713aSLionel Sambuc 
2235f4a2713aSLionel Sambuc     if (Tok.is(tok::kw_namespace)) {
2236f4a2713aSLionel Sambuc       Diag(UsingLoc, diag::err_using_namespace_in_class);
2237f4a2713aSLionel Sambuc       SkipUntil(tok::semi, StopBeforeMatch);
2238f4a2713aSLionel Sambuc     } else {
2239f4a2713aSLionel Sambuc       SourceLocation DeclEnd;
2240f4a2713aSLionel Sambuc       // Otherwise, it must be a using-declaration or an alias-declaration.
2241f4a2713aSLionel Sambuc       ParseUsingDeclaration(Declarator::MemberContext, TemplateInfo,
2242f4a2713aSLionel Sambuc                             UsingLoc, DeclEnd, AS);
2243f4a2713aSLionel Sambuc     }
2244f4a2713aSLionel Sambuc     return;
2245f4a2713aSLionel Sambuc   }
2246f4a2713aSLionel Sambuc 
2247f4a2713aSLionel Sambuc   // Hold late-parsed attributes so we can attach a Decl to them later.
2248f4a2713aSLionel Sambuc   LateParsedAttrList CommonLateParsedAttrs;
2249f4a2713aSLionel Sambuc 
2250f4a2713aSLionel Sambuc   // decl-specifier-seq:
2251f4a2713aSLionel Sambuc   // Parse the common declaration-specifiers piece.
2252f4a2713aSLionel Sambuc   ParsingDeclSpec DS(*this, TemplateDiags);
2253f4a2713aSLionel Sambuc   DS.takeAttributesFrom(attrs);
2254f4a2713aSLionel Sambuc   if (MalformedTypeSpec)
2255f4a2713aSLionel Sambuc     DS.SetTypeSpecError();
2256*0a6a1f1dSLionel Sambuc 
2257f4a2713aSLionel Sambuc   ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class,
2258f4a2713aSLionel Sambuc                              &CommonLateParsedAttrs);
2259f4a2713aSLionel Sambuc 
2260*0a6a1f1dSLionel Sambuc   // Turn off colon protection that was set for declspec.
2261*0a6a1f1dSLionel Sambuc   X.restore();
2262*0a6a1f1dSLionel Sambuc 
2263f4a2713aSLionel Sambuc   // If we had a free-standing type definition with a missing semicolon, we
2264f4a2713aSLionel Sambuc   // may get this far before the problem becomes obvious.
2265f4a2713aSLionel Sambuc   if (DS.hasTagDefinition() &&
2266f4a2713aSLionel Sambuc       TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate &&
2267f4a2713aSLionel Sambuc       DiagnoseMissingSemiAfterTagDefinition(DS, AS, DSC_class,
2268f4a2713aSLionel Sambuc                                             &CommonLateParsedAttrs))
2269f4a2713aSLionel Sambuc     return;
2270f4a2713aSLionel Sambuc 
2271f4a2713aSLionel Sambuc   MultiTemplateParamsArg TemplateParams(
2272*0a6a1f1dSLionel Sambuc       TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data()
2273*0a6a1f1dSLionel Sambuc                                  : nullptr,
2274f4a2713aSLionel Sambuc       TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0);
2275f4a2713aSLionel Sambuc 
2276*0a6a1f1dSLionel Sambuc   if (TryConsumeToken(tok::semi)) {
2277f4a2713aSLionel Sambuc     if (DS.isFriendSpecified())
2278f4a2713aSLionel Sambuc       ProhibitAttributes(FnAttrs);
2279f4a2713aSLionel Sambuc 
2280f4a2713aSLionel Sambuc     Decl *TheDecl =
2281f4a2713aSLionel Sambuc       Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS, DS, TemplateParams);
2282f4a2713aSLionel Sambuc     DS.complete(TheDecl);
2283f4a2713aSLionel Sambuc     return;
2284f4a2713aSLionel Sambuc   }
2285f4a2713aSLionel Sambuc 
2286f4a2713aSLionel Sambuc   ParsingDeclarator DeclaratorInfo(*this, DS, Declarator::MemberContext);
2287f4a2713aSLionel Sambuc   VirtSpecifiers VS;
2288f4a2713aSLionel Sambuc 
2289f4a2713aSLionel Sambuc   // Hold late-parsed attributes so we can attach a Decl to them later.
2290f4a2713aSLionel Sambuc   LateParsedAttrList LateParsedAttrs;
2291f4a2713aSLionel Sambuc 
2292f4a2713aSLionel Sambuc   SourceLocation EqualLoc;
2293f4a2713aSLionel Sambuc   bool HasInitializer = false;
2294f4a2713aSLionel Sambuc   ExprResult Init;
2295*0a6a1f1dSLionel Sambuc 
2296*0a6a1f1dSLionel Sambuc   SmallVector<Decl *, 8> DeclsInGroup;
2297*0a6a1f1dSLionel Sambuc   ExprResult BitfieldSize;
2298*0a6a1f1dSLionel Sambuc   bool ExpectSemi = true;
2299f4a2713aSLionel Sambuc 
2300f4a2713aSLionel Sambuc   // Parse the first declarator.
2301*0a6a1f1dSLionel Sambuc   ParseCXXMemberDeclaratorBeforeInitializer(DeclaratorInfo, VS, BitfieldSize,
2302*0a6a1f1dSLionel Sambuc                                             LateParsedAttrs);
2303*0a6a1f1dSLionel Sambuc 
2304*0a6a1f1dSLionel Sambuc   // If this has neither a name nor a bit width, something has gone seriously
2305*0a6a1f1dSLionel Sambuc   // wrong. Skip until the semi-colon or }.
2306*0a6a1f1dSLionel Sambuc   if (!DeclaratorInfo.hasName() && BitfieldSize.isUnset()) {
2307f4a2713aSLionel Sambuc     // If so, skip until the semi-colon or a }.
2308f4a2713aSLionel Sambuc     SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
2309*0a6a1f1dSLionel Sambuc     TryConsumeToken(tok::semi);
2310f4a2713aSLionel Sambuc     return;
2311f4a2713aSLionel Sambuc   }
2312f4a2713aSLionel Sambuc 
2313*0a6a1f1dSLionel Sambuc   // Check for a member function definition.
2314*0a6a1f1dSLionel Sambuc   if (BitfieldSize.isUnset()) {
2315*0a6a1f1dSLionel Sambuc     // MSVC permits pure specifier on inline functions defined at class scope.
2316f4a2713aSLionel Sambuc     // Hence check for =0 before checking for function definition.
2317f4a2713aSLionel Sambuc     if (getLangOpts().MicrosoftExt && Tok.is(tok::equal) &&
2318f4a2713aSLionel Sambuc         DeclaratorInfo.isFunctionDeclarator() &&
2319f4a2713aSLionel Sambuc         NextToken().is(tok::numeric_constant)) {
2320f4a2713aSLionel Sambuc       EqualLoc = ConsumeToken();
2321f4a2713aSLionel Sambuc       Init = ParseInitializer();
2322f4a2713aSLionel Sambuc       if (Init.isInvalid())
2323f4a2713aSLionel Sambuc         SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
2324f4a2713aSLionel Sambuc       else
2325f4a2713aSLionel Sambuc         HasInitializer = true;
2326f4a2713aSLionel Sambuc     }
2327f4a2713aSLionel Sambuc 
2328f4a2713aSLionel Sambuc     FunctionDefinitionKind DefinitionKind = FDK_Declaration;
2329f4a2713aSLionel Sambuc     // function-definition:
2330f4a2713aSLionel Sambuc     //
2331f4a2713aSLionel Sambuc     // In C++11, a non-function declarator followed by an open brace is a
2332f4a2713aSLionel Sambuc     // braced-init-list for an in-class member initialization, not an
2333f4a2713aSLionel Sambuc     // erroneous function definition.
2334f4a2713aSLionel Sambuc     if (Tok.is(tok::l_brace) && !getLangOpts().CPlusPlus11) {
2335f4a2713aSLionel Sambuc       DefinitionKind = FDK_Definition;
2336f4a2713aSLionel Sambuc     } else if (DeclaratorInfo.isFunctionDeclarator()) {
2337f4a2713aSLionel Sambuc       if (Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try)) {
2338f4a2713aSLionel Sambuc         DefinitionKind = FDK_Definition;
2339f4a2713aSLionel Sambuc       } else if (Tok.is(tok::equal)) {
2340f4a2713aSLionel Sambuc         const Token &KW = NextToken();
2341f4a2713aSLionel Sambuc         if (KW.is(tok::kw_default))
2342f4a2713aSLionel Sambuc           DefinitionKind = FDK_Defaulted;
2343f4a2713aSLionel Sambuc         else if (KW.is(tok::kw_delete))
2344f4a2713aSLionel Sambuc           DefinitionKind = FDK_Deleted;
2345f4a2713aSLionel Sambuc       }
2346f4a2713aSLionel Sambuc     }
2347f4a2713aSLionel Sambuc 
2348f4a2713aSLionel Sambuc     // C++11 [dcl.attr.grammar] p4: If an attribute-specifier-seq appertains
2349f4a2713aSLionel Sambuc     // to a friend declaration, that declaration shall be a definition.
2350f4a2713aSLionel Sambuc     if (DeclaratorInfo.isFunctionDeclarator() &&
2351f4a2713aSLionel Sambuc         DefinitionKind != FDK_Definition && DS.isFriendSpecified()) {
2352f4a2713aSLionel Sambuc       // Diagnose attributes that appear before decl specifier:
2353f4a2713aSLionel Sambuc       // [[]] friend int foo();
2354f4a2713aSLionel Sambuc       ProhibitAttributes(FnAttrs);
2355f4a2713aSLionel Sambuc     }
2356f4a2713aSLionel Sambuc 
2357f4a2713aSLionel Sambuc     if (DefinitionKind) {
2358f4a2713aSLionel Sambuc       if (!DeclaratorInfo.isFunctionDeclarator()) {
2359f4a2713aSLionel Sambuc         Diag(DeclaratorInfo.getIdentifierLoc(), diag::err_func_def_no_params);
2360f4a2713aSLionel Sambuc         ConsumeBrace();
2361f4a2713aSLionel Sambuc         SkipUntil(tok::r_brace);
2362f4a2713aSLionel Sambuc 
2363f4a2713aSLionel Sambuc         // Consume the optional ';'
2364*0a6a1f1dSLionel Sambuc         TryConsumeToken(tok::semi);
2365*0a6a1f1dSLionel Sambuc 
2366f4a2713aSLionel Sambuc         return;
2367f4a2713aSLionel Sambuc       }
2368f4a2713aSLionel Sambuc 
2369f4a2713aSLionel Sambuc       if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
2370f4a2713aSLionel Sambuc         Diag(DeclaratorInfo.getIdentifierLoc(),
2371f4a2713aSLionel Sambuc              diag::err_function_declared_typedef);
2372f4a2713aSLionel Sambuc 
2373f4a2713aSLionel Sambuc         // Recover by treating the 'typedef' as spurious.
2374f4a2713aSLionel Sambuc         DS.ClearStorageClassSpecs();
2375f4a2713aSLionel Sambuc       }
2376f4a2713aSLionel Sambuc 
2377f4a2713aSLionel Sambuc       Decl *FunDecl =
2378f4a2713aSLionel Sambuc         ParseCXXInlineMethodDef(AS, AccessAttrs, DeclaratorInfo, TemplateInfo,
2379f4a2713aSLionel Sambuc                                 VS, DefinitionKind, Init);
2380f4a2713aSLionel Sambuc 
2381f4a2713aSLionel Sambuc       if (FunDecl) {
2382f4a2713aSLionel Sambuc         for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i) {
2383f4a2713aSLionel Sambuc           CommonLateParsedAttrs[i]->addDecl(FunDecl);
2384f4a2713aSLionel Sambuc         }
2385f4a2713aSLionel Sambuc         for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i) {
2386f4a2713aSLionel Sambuc           LateParsedAttrs[i]->addDecl(FunDecl);
2387f4a2713aSLionel Sambuc         }
2388f4a2713aSLionel Sambuc       }
2389f4a2713aSLionel Sambuc       LateParsedAttrs.clear();
2390f4a2713aSLionel Sambuc 
2391f4a2713aSLionel Sambuc       // Consume the ';' - it's optional unless we have a delete or default
2392f4a2713aSLionel Sambuc       if (Tok.is(tok::semi))
2393f4a2713aSLionel Sambuc         ConsumeExtraSemi(AfterMemberFunctionDefinition);
2394f4a2713aSLionel Sambuc 
2395f4a2713aSLionel Sambuc       return;
2396f4a2713aSLionel Sambuc     }
2397f4a2713aSLionel Sambuc   }
2398f4a2713aSLionel Sambuc 
2399f4a2713aSLionel Sambuc   // member-declarator-list:
2400f4a2713aSLionel Sambuc   //   member-declarator
2401f4a2713aSLionel Sambuc   //   member-declarator-list ',' member-declarator
2402f4a2713aSLionel Sambuc 
2403f4a2713aSLionel Sambuc   while (1) {
2404f4a2713aSLionel Sambuc     InClassInitStyle HasInClassInit = ICIS_NoInit;
2405f4a2713aSLionel Sambuc     if ((Tok.is(tok::equal) || Tok.is(tok::l_brace)) && !HasInitializer) {
2406f4a2713aSLionel Sambuc       if (BitfieldSize.get()) {
2407f4a2713aSLionel Sambuc         Diag(Tok, diag::err_bitfield_member_init);
2408f4a2713aSLionel Sambuc         SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
2409f4a2713aSLionel Sambuc       } else {
2410f4a2713aSLionel Sambuc         HasInitializer = true;
2411f4a2713aSLionel Sambuc         if (!DeclaratorInfo.isDeclarationOfFunction() &&
2412f4a2713aSLionel Sambuc             DeclaratorInfo.getDeclSpec().getStorageClassSpec()
2413f4a2713aSLionel Sambuc               != DeclSpec::SCS_typedef)
2414f4a2713aSLionel Sambuc           HasInClassInit = Tok.is(tok::equal) ? ICIS_CopyInit : ICIS_ListInit;
2415f4a2713aSLionel Sambuc       }
2416f4a2713aSLionel Sambuc     }
2417f4a2713aSLionel Sambuc 
2418f4a2713aSLionel Sambuc     // NOTE: If Sema is the Action module and declarator is an instance field,
2419f4a2713aSLionel Sambuc     // this call will *not* return the created decl; It will return null.
2420f4a2713aSLionel Sambuc     // See Sema::ActOnCXXMemberDeclarator for details.
2421f4a2713aSLionel Sambuc 
2422*0a6a1f1dSLionel Sambuc     NamedDecl *ThisDecl = nullptr;
2423f4a2713aSLionel Sambuc     if (DS.isFriendSpecified()) {
2424f4a2713aSLionel Sambuc       // C++11 [dcl.attr.grammar] p4: If an attribute-specifier-seq appertains
2425f4a2713aSLionel Sambuc       // to a friend declaration, that declaration shall be a definition.
2426f4a2713aSLionel Sambuc       //
2427*0a6a1f1dSLionel Sambuc       // Diagnose attributes that appear in a friend member function declarator:
2428*0a6a1f1dSLionel Sambuc       //   friend int foo [[]] ();
2429f4a2713aSLionel Sambuc       SmallVector<SourceRange, 4> Ranges;
2430f4a2713aSLionel Sambuc       DeclaratorInfo.getCXX11AttributeRanges(Ranges);
2431f4a2713aSLionel Sambuc       for (SmallVectorImpl<SourceRange>::iterator I = Ranges.begin(),
2432*0a6a1f1dSLionel Sambuc            E = Ranges.end(); I != E; ++I)
2433*0a6a1f1dSLionel Sambuc         Diag((*I).getBegin(), diag::err_attributes_not_allowed) << *I;
2434f4a2713aSLionel Sambuc 
2435f4a2713aSLionel Sambuc       ThisDecl = Actions.ActOnFriendFunctionDecl(getCurScope(), DeclaratorInfo,
2436f4a2713aSLionel Sambuc                                                  TemplateParams);
2437f4a2713aSLionel Sambuc     } else {
2438f4a2713aSLionel Sambuc       ThisDecl = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS,
2439f4a2713aSLionel Sambuc                                                   DeclaratorInfo,
2440f4a2713aSLionel Sambuc                                                   TemplateParams,
2441*0a6a1f1dSLionel Sambuc                                                   BitfieldSize.get(),
2442f4a2713aSLionel Sambuc                                                   VS, HasInClassInit);
2443f4a2713aSLionel Sambuc 
2444f4a2713aSLionel Sambuc       if (VarTemplateDecl *VT =
2445*0a6a1f1dSLionel Sambuc               ThisDecl ? dyn_cast<VarTemplateDecl>(ThisDecl) : nullptr)
2446f4a2713aSLionel Sambuc         // Re-direct this decl to refer to the templated decl so that we can
2447f4a2713aSLionel Sambuc         // initialize it.
2448f4a2713aSLionel Sambuc         ThisDecl = VT->getTemplatedDecl();
2449f4a2713aSLionel Sambuc 
2450f4a2713aSLionel Sambuc       if (ThisDecl && AccessAttrs)
2451f4a2713aSLionel Sambuc         Actions.ProcessDeclAttributeList(getCurScope(), ThisDecl, AccessAttrs);
2452f4a2713aSLionel Sambuc     }
2453f4a2713aSLionel Sambuc 
2454f4a2713aSLionel Sambuc     // Handle the initializer.
2455f4a2713aSLionel Sambuc     if (HasInClassInit != ICIS_NoInit &&
2456f4a2713aSLionel Sambuc         DeclaratorInfo.getDeclSpec().getStorageClassSpec() !=
2457f4a2713aSLionel Sambuc         DeclSpec::SCS_static) {
2458f4a2713aSLionel Sambuc       // The initializer was deferred; parse it and cache the tokens.
2459f4a2713aSLionel Sambuc       Diag(Tok, getLangOpts().CPlusPlus11
2460f4a2713aSLionel Sambuc                     ? diag::warn_cxx98_compat_nonstatic_member_init
2461f4a2713aSLionel Sambuc                     : diag::ext_nonstatic_member_init);
2462f4a2713aSLionel Sambuc 
2463f4a2713aSLionel Sambuc       if (DeclaratorInfo.isArrayOfUnknownBound()) {
2464f4a2713aSLionel Sambuc         // C++11 [dcl.array]p3: An array bound may also be omitted when the
2465f4a2713aSLionel Sambuc         // declarator is followed by an initializer.
2466f4a2713aSLionel Sambuc         //
2467f4a2713aSLionel Sambuc         // A brace-or-equal-initializer for a member-declarator is not an
2468f4a2713aSLionel Sambuc         // initializer in the grammar, so this is ill-formed.
2469f4a2713aSLionel Sambuc         Diag(Tok, diag::err_incomplete_array_member_init);
2470f4a2713aSLionel Sambuc         SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
2471f4a2713aSLionel Sambuc 
2472f4a2713aSLionel Sambuc         // Avoid later warnings about a class member of incomplete type.
2473f4a2713aSLionel Sambuc         if (ThisDecl)
2474f4a2713aSLionel Sambuc           ThisDecl->setInvalidDecl();
2475f4a2713aSLionel Sambuc       } else
2476f4a2713aSLionel Sambuc         ParseCXXNonStaticMemberInitializer(ThisDecl);
2477f4a2713aSLionel Sambuc     } else if (HasInitializer) {
2478f4a2713aSLionel Sambuc       // Normal initializer.
2479f4a2713aSLionel Sambuc       if (!Init.isUsable())
2480f4a2713aSLionel Sambuc         Init = ParseCXXMemberInitializer(
2481f4a2713aSLionel Sambuc             ThisDecl, DeclaratorInfo.isDeclarationOfFunction(), EqualLoc);
2482f4a2713aSLionel Sambuc 
2483f4a2713aSLionel Sambuc       if (Init.isInvalid())
2484f4a2713aSLionel Sambuc         SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
2485f4a2713aSLionel Sambuc       else if (ThisDecl)
2486f4a2713aSLionel Sambuc         Actions.AddInitializerToDecl(ThisDecl, Init.get(), EqualLoc.isInvalid(),
2487f4a2713aSLionel Sambuc                                      DS.containsPlaceholderType());
2488f4a2713aSLionel Sambuc     } else if (ThisDecl && DS.getStorageClassSpec() == DeclSpec::SCS_static)
2489f4a2713aSLionel Sambuc       // No initializer.
2490f4a2713aSLionel Sambuc       Actions.ActOnUninitializedDecl(ThisDecl, DS.containsPlaceholderType());
2491f4a2713aSLionel Sambuc 
2492f4a2713aSLionel Sambuc     if (ThisDecl) {
2493f4a2713aSLionel Sambuc       if (!ThisDecl->isInvalidDecl()) {
2494f4a2713aSLionel Sambuc         // Set the Decl for any late parsed attributes
2495f4a2713aSLionel Sambuc         for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i)
2496f4a2713aSLionel Sambuc           CommonLateParsedAttrs[i]->addDecl(ThisDecl);
2497f4a2713aSLionel Sambuc 
2498f4a2713aSLionel Sambuc         for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i)
2499f4a2713aSLionel Sambuc           LateParsedAttrs[i]->addDecl(ThisDecl);
2500f4a2713aSLionel Sambuc       }
2501f4a2713aSLionel Sambuc       Actions.FinalizeDeclaration(ThisDecl);
2502f4a2713aSLionel Sambuc       DeclsInGroup.push_back(ThisDecl);
2503f4a2713aSLionel Sambuc 
2504f4a2713aSLionel Sambuc       if (DeclaratorInfo.isFunctionDeclarator() &&
2505f4a2713aSLionel Sambuc           DeclaratorInfo.getDeclSpec().getStorageClassSpec() !=
2506f4a2713aSLionel Sambuc               DeclSpec::SCS_typedef)
2507f4a2713aSLionel Sambuc         HandleMemberFunctionDeclDelays(DeclaratorInfo, ThisDecl);
2508f4a2713aSLionel Sambuc     }
2509f4a2713aSLionel Sambuc     LateParsedAttrs.clear();
2510f4a2713aSLionel Sambuc 
2511f4a2713aSLionel Sambuc     DeclaratorInfo.complete(ThisDecl);
2512f4a2713aSLionel Sambuc 
2513f4a2713aSLionel Sambuc     // If we don't have a comma, it is either the end of the list (a ';')
2514f4a2713aSLionel Sambuc     // or an error, bail out.
2515*0a6a1f1dSLionel Sambuc     SourceLocation CommaLoc;
2516*0a6a1f1dSLionel Sambuc     if (!TryConsumeToken(tok::comma, CommaLoc))
2517f4a2713aSLionel Sambuc       break;
2518f4a2713aSLionel Sambuc 
2519f4a2713aSLionel Sambuc     if (Tok.isAtStartOfLine() &&
2520f4a2713aSLionel Sambuc         !MightBeDeclarator(Declarator::MemberContext)) {
2521f4a2713aSLionel Sambuc       // This comma was followed by a line-break and something which can't be
2522f4a2713aSLionel Sambuc       // the start of a declarator. The comma was probably a typo for a
2523f4a2713aSLionel Sambuc       // semicolon.
2524f4a2713aSLionel Sambuc       Diag(CommaLoc, diag::err_expected_semi_declaration)
2525f4a2713aSLionel Sambuc         << FixItHint::CreateReplacement(CommaLoc, ";");
2526f4a2713aSLionel Sambuc       ExpectSemi = false;
2527f4a2713aSLionel Sambuc       break;
2528f4a2713aSLionel Sambuc     }
2529f4a2713aSLionel Sambuc 
2530f4a2713aSLionel Sambuc     // Parse the next declarator.
2531f4a2713aSLionel Sambuc     DeclaratorInfo.clear();
2532f4a2713aSLionel Sambuc     VS.clear();
2533f4a2713aSLionel Sambuc     BitfieldSize = true;
2534f4a2713aSLionel Sambuc     Init = true;
2535f4a2713aSLionel Sambuc     HasInitializer = false;
2536f4a2713aSLionel Sambuc     DeclaratorInfo.setCommaLoc(CommaLoc);
2537f4a2713aSLionel Sambuc 
2538*0a6a1f1dSLionel Sambuc     // GNU attributes are allowed before the second and subsequent declarator.
2539f4a2713aSLionel Sambuc     MaybeParseGNUAttributes(DeclaratorInfo);
2540f4a2713aSLionel Sambuc 
2541*0a6a1f1dSLionel Sambuc     ParseCXXMemberDeclaratorBeforeInitializer(DeclaratorInfo, VS, BitfieldSize,
2542*0a6a1f1dSLionel Sambuc                                               LateParsedAttrs);
2543f4a2713aSLionel Sambuc   }
2544f4a2713aSLionel Sambuc 
2545f4a2713aSLionel Sambuc   if (ExpectSemi &&
2546f4a2713aSLionel Sambuc       ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list)) {
2547f4a2713aSLionel Sambuc     // Skip to end of block or statement.
2548f4a2713aSLionel Sambuc     SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
2549f4a2713aSLionel Sambuc     // If we stopped at a ';', eat it.
2550*0a6a1f1dSLionel Sambuc     TryConsumeToken(tok::semi);
2551f4a2713aSLionel Sambuc     return;
2552f4a2713aSLionel Sambuc   }
2553f4a2713aSLionel Sambuc 
2554f4a2713aSLionel Sambuc   Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup);
2555f4a2713aSLionel Sambuc }
2556f4a2713aSLionel Sambuc 
2557f4a2713aSLionel Sambuc /// ParseCXXMemberInitializer - Parse the brace-or-equal-initializer or
2558f4a2713aSLionel Sambuc /// pure-specifier. Also detect and reject any attempted defaulted/deleted
2559f4a2713aSLionel Sambuc /// function definition. The location of the '=', if any, will be placed in
2560f4a2713aSLionel Sambuc /// EqualLoc.
2561f4a2713aSLionel Sambuc ///
2562f4a2713aSLionel Sambuc ///   pure-specifier:
2563f4a2713aSLionel Sambuc ///     '= 0'
2564f4a2713aSLionel Sambuc ///
2565f4a2713aSLionel Sambuc ///   brace-or-equal-initializer:
2566f4a2713aSLionel Sambuc ///     '=' initializer-expression
2567f4a2713aSLionel Sambuc ///     braced-init-list
2568f4a2713aSLionel Sambuc ///
2569f4a2713aSLionel Sambuc ///   initializer-clause:
2570f4a2713aSLionel Sambuc ///     assignment-expression
2571f4a2713aSLionel Sambuc ///     braced-init-list
2572f4a2713aSLionel Sambuc ///
2573f4a2713aSLionel Sambuc ///   defaulted/deleted function-definition:
2574f4a2713aSLionel Sambuc ///     '=' 'default'
2575f4a2713aSLionel Sambuc ///     '=' 'delete'
2576f4a2713aSLionel Sambuc ///
2577f4a2713aSLionel Sambuc /// Prior to C++0x, the assignment-expression in an initializer-clause must
2578f4a2713aSLionel Sambuc /// be a constant-expression.
ParseCXXMemberInitializer(Decl * D,bool IsFunction,SourceLocation & EqualLoc)2579f4a2713aSLionel Sambuc ExprResult Parser::ParseCXXMemberInitializer(Decl *D, bool IsFunction,
2580f4a2713aSLionel Sambuc                                              SourceLocation &EqualLoc) {
2581f4a2713aSLionel Sambuc   assert((Tok.is(tok::equal) || Tok.is(tok::l_brace))
2582f4a2713aSLionel Sambuc          && "Data member initializer not starting with '=' or '{'");
2583f4a2713aSLionel Sambuc 
2584f4a2713aSLionel Sambuc   EnterExpressionEvaluationContext Context(Actions,
2585f4a2713aSLionel Sambuc                                            Sema::PotentiallyEvaluated,
2586f4a2713aSLionel Sambuc                                            D);
2587*0a6a1f1dSLionel Sambuc   if (TryConsumeToken(tok::equal, EqualLoc)) {
2588f4a2713aSLionel Sambuc     if (Tok.is(tok::kw_delete)) {
2589f4a2713aSLionel Sambuc       // In principle, an initializer of '= delete p;' is legal, but it will
2590f4a2713aSLionel Sambuc       // never type-check. It's better to diagnose it as an ill-formed expression
2591f4a2713aSLionel Sambuc       // than as an ill-formed deleted non-function member.
2592f4a2713aSLionel Sambuc       // An initializer of '= delete p, foo' will never be parsed, because
2593f4a2713aSLionel Sambuc       // a top-level comma always ends the initializer expression.
2594f4a2713aSLionel Sambuc       const Token &Next = NextToken();
2595f4a2713aSLionel Sambuc       if (IsFunction || Next.is(tok::semi) || Next.is(tok::comma) ||
2596f4a2713aSLionel Sambuc           Next.is(tok::eof)) {
2597f4a2713aSLionel Sambuc         if (IsFunction)
2598f4a2713aSLionel Sambuc           Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
2599f4a2713aSLionel Sambuc             << 1 /* delete */;
2600f4a2713aSLionel Sambuc         else
2601f4a2713aSLionel Sambuc           Diag(ConsumeToken(), diag::err_deleted_non_function);
2602*0a6a1f1dSLionel Sambuc         return ExprError();
2603f4a2713aSLionel Sambuc       }
2604f4a2713aSLionel Sambuc     } else if (Tok.is(tok::kw_default)) {
2605f4a2713aSLionel Sambuc       if (IsFunction)
2606f4a2713aSLionel Sambuc         Diag(Tok, diag::err_default_delete_in_multiple_declaration)
2607f4a2713aSLionel Sambuc           << 0 /* default */;
2608f4a2713aSLionel Sambuc       else
2609f4a2713aSLionel Sambuc         Diag(ConsumeToken(), diag::err_default_special_members);
2610*0a6a1f1dSLionel Sambuc       return ExprError();
2611f4a2713aSLionel Sambuc     }
2612*0a6a1f1dSLionel Sambuc   }
2613*0a6a1f1dSLionel Sambuc   if (const auto *PD = dyn_cast_or_null<MSPropertyDecl>(D)) {
2614*0a6a1f1dSLionel Sambuc     Diag(Tok, diag::err_ms_property_initializer) << PD;
2615*0a6a1f1dSLionel Sambuc     return ExprError();
2616f4a2713aSLionel Sambuc   }
2617f4a2713aSLionel Sambuc   return ParseInitializer();
2618f4a2713aSLionel Sambuc }
2619f4a2713aSLionel Sambuc 
2620f4a2713aSLionel Sambuc /// ParseCXXMemberSpecification - Parse the class definition.
2621f4a2713aSLionel Sambuc ///
2622f4a2713aSLionel Sambuc ///       member-specification:
2623f4a2713aSLionel Sambuc ///         member-declaration member-specification[opt]
2624f4a2713aSLionel Sambuc ///         access-specifier ':' member-specification[opt]
2625f4a2713aSLionel Sambuc ///
ParseCXXMemberSpecification(SourceLocation RecordLoc,SourceLocation AttrFixitLoc,ParsedAttributesWithRange & Attrs,unsigned TagType,Decl * TagDecl)2626f4a2713aSLionel Sambuc void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
2627f4a2713aSLionel Sambuc                                          SourceLocation AttrFixitLoc,
2628f4a2713aSLionel Sambuc                                          ParsedAttributesWithRange &Attrs,
2629f4a2713aSLionel Sambuc                                          unsigned TagType, Decl *TagDecl) {
2630f4a2713aSLionel Sambuc   assert((TagType == DeclSpec::TST_struct ||
2631f4a2713aSLionel Sambuc          TagType == DeclSpec::TST_interface ||
2632f4a2713aSLionel Sambuc          TagType == DeclSpec::TST_union  ||
2633f4a2713aSLionel Sambuc          TagType == DeclSpec::TST_class) && "Invalid TagType!");
2634f4a2713aSLionel Sambuc 
2635f4a2713aSLionel Sambuc   PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2636f4a2713aSLionel Sambuc                                       "parsing struct/union/class body");
2637f4a2713aSLionel Sambuc 
2638f4a2713aSLionel Sambuc   // Determine whether this is a non-nested class. Note that local
2639f4a2713aSLionel Sambuc   // classes are *not* considered to be nested classes.
2640f4a2713aSLionel Sambuc   bool NonNestedClass = true;
2641f4a2713aSLionel Sambuc   if (!ClassStack.empty()) {
2642f4a2713aSLionel Sambuc     for (const Scope *S = getCurScope(); S; S = S->getParent()) {
2643f4a2713aSLionel Sambuc       if (S->isClassScope()) {
2644f4a2713aSLionel Sambuc         // We're inside a class scope, so this is a nested class.
2645f4a2713aSLionel Sambuc         NonNestedClass = false;
2646f4a2713aSLionel Sambuc 
2647f4a2713aSLionel Sambuc         // The Microsoft extension __interface does not permit nested classes.
2648f4a2713aSLionel Sambuc         if (getCurrentClass().IsInterface) {
2649f4a2713aSLionel Sambuc           Diag(RecordLoc, diag::err_invalid_member_in_interface)
2650f4a2713aSLionel Sambuc             << /*ErrorType=*/6
2651f4a2713aSLionel Sambuc             << (isa<NamedDecl>(TagDecl)
2652f4a2713aSLionel Sambuc                   ? cast<NamedDecl>(TagDecl)->getQualifiedNameAsString()
2653*0a6a1f1dSLionel Sambuc                   : "(anonymous)");
2654f4a2713aSLionel Sambuc         }
2655f4a2713aSLionel Sambuc         break;
2656f4a2713aSLionel Sambuc       }
2657f4a2713aSLionel Sambuc 
2658f4a2713aSLionel Sambuc       if ((S->getFlags() & Scope::FnScope)) {
2659f4a2713aSLionel Sambuc         // If we're in a function or function template declared in the
2660f4a2713aSLionel Sambuc         // body of a class, then this is a local class rather than a
2661f4a2713aSLionel Sambuc         // nested class.
2662f4a2713aSLionel Sambuc         const Scope *Parent = S->getParent();
2663f4a2713aSLionel Sambuc         if (Parent->isTemplateParamScope())
2664f4a2713aSLionel Sambuc           Parent = Parent->getParent();
2665f4a2713aSLionel Sambuc         if (Parent->isClassScope())
2666f4a2713aSLionel Sambuc           break;
2667f4a2713aSLionel Sambuc       }
2668f4a2713aSLionel Sambuc     }
2669f4a2713aSLionel Sambuc   }
2670f4a2713aSLionel Sambuc 
2671f4a2713aSLionel Sambuc   // Enter a scope for the class.
2672f4a2713aSLionel Sambuc   ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
2673f4a2713aSLionel Sambuc 
2674f4a2713aSLionel Sambuc   // Note that we are parsing a new (potentially-nested) class definition.
2675f4a2713aSLionel Sambuc   ParsingClassDefinition ParsingDef(*this, TagDecl, NonNestedClass,
2676f4a2713aSLionel Sambuc                                     TagType == DeclSpec::TST_interface);
2677f4a2713aSLionel Sambuc 
2678f4a2713aSLionel Sambuc   if (TagDecl)
2679f4a2713aSLionel Sambuc     Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
2680f4a2713aSLionel Sambuc 
2681f4a2713aSLionel Sambuc   SourceLocation FinalLoc;
2682f4a2713aSLionel Sambuc   bool IsFinalSpelledSealed = false;
2683f4a2713aSLionel Sambuc 
2684f4a2713aSLionel Sambuc   // Parse the optional 'final' keyword.
2685f4a2713aSLionel Sambuc   if (getLangOpts().CPlusPlus && Tok.is(tok::identifier)) {
2686f4a2713aSLionel Sambuc     VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier(Tok);
2687f4a2713aSLionel Sambuc     assert((Specifier == VirtSpecifiers::VS_Final ||
2688f4a2713aSLionel Sambuc             Specifier == VirtSpecifiers::VS_Sealed) &&
2689f4a2713aSLionel Sambuc            "not a class definition");
2690f4a2713aSLionel Sambuc     FinalLoc = ConsumeToken();
2691f4a2713aSLionel Sambuc     IsFinalSpelledSealed = Specifier == VirtSpecifiers::VS_Sealed;
2692f4a2713aSLionel Sambuc 
2693f4a2713aSLionel Sambuc     if (TagType == DeclSpec::TST_interface)
2694f4a2713aSLionel Sambuc       Diag(FinalLoc, diag::err_override_control_interface)
2695f4a2713aSLionel Sambuc         << VirtSpecifiers::getSpecifierName(Specifier);
2696f4a2713aSLionel Sambuc     else if (Specifier == VirtSpecifiers::VS_Final)
2697f4a2713aSLionel Sambuc       Diag(FinalLoc, getLangOpts().CPlusPlus11
2698f4a2713aSLionel Sambuc                          ? diag::warn_cxx98_compat_override_control_keyword
2699f4a2713aSLionel Sambuc                          : diag::ext_override_control_keyword)
2700f4a2713aSLionel Sambuc         << VirtSpecifiers::getSpecifierName(Specifier);
2701f4a2713aSLionel Sambuc     else if (Specifier == VirtSpecifiers::VS_Sealed)
2702f4a2713aSLionel Sambuc       Diag(FinalLoc, diag::ext_ms_sealed_keyword);
2703f4a2713aSLionel Sambuc 
2704f4a2713aSLionel Sambuc     // Parse any C++11 attributes after 'final' keyword.
2705f4a2713aSLionel Sambuc     // These attributes are not allowed to appear here,
2706f4a2713aSLionel Sambuc     // and the only possible place for them to appertain
2707f4a2713aSLionel Sambuc     // to the class would be between class-key and class-name.
2708f4a2713aSLionel Sambuc     CheckMisplacedCXX11Attribute(Attrs, AttrFixitLoc);
2709*0a6a1f1dSLionel Sambuc 
2710*0a6a1f1dSLionel Sambuc     // ParseClassSpecifier() does only a superficial check for attributes before
2711*0a6a1f1dSLionel Sambuc     // deciding to call this method.  For example, for
2712*0a6a1f1dSLionel Sambuc     // `class C final alignas ([l) {` it will decide that this looks like a
2713*0a6a1f1dSLionel Sambuc     // misplaced attribute since it sees `alignas '(' ')'`.  But the actual
2714*0a6a1f1dSLionel Sambuc     // attribute parsing code will try to parse the '[' as a constexpr lambda
2715*0a6a1f1dSLionel Sambuc     // and consume enough tokens that the alignas parsing code will eat the
2716*0a6a1f1dSLionel Sambuc     // opening '{'.  So bail out if the next token isn't one we expect.
2717*0a6a1f1dSLionel Sambuc     if (!Tok.is(tok::colon) && !Tok.is(tok::l_brace)) {
2718*0a6a1f1dSLionel Sambuc       if (TagDecl)
2719*0a6a1f1dSLionel Sambuc         Actions.ActOnTagDefinitionError(getCurScope(), TagDecl);
2720*0a6a1f1dSLionel Sambuc       return;
2721*0a6a1f1dSLionel Sambuc     }
2722f4a2713aSLionel Sambuc   }
2723f4a2713aSLionel Sambuc 
2724f4a2713aSLionel Sambuc   if (Tok.is(tok::colon)) {
2725f4a2713aSLionel Sambuc     ParseBaseClause(TagDecl);
2726f4a2713aSLionel Sambuc     if (!Tok.is(tok::l_brace)) {
2727*0a6a1f1dSLionel Sambuc       bool SuggestFixIt = false;
2728*0a6a1f1dSLionel Sambuc       SourceLocation BraceLoc = PP.getLocForEndOfToken(PrevTokLocation);
2729*0a6a1f1dSLionel Sambuc       if (Tok.isAtStartOfLine()) {
2730*0a6a1f1dSLionel Sambuc         switch (Tok.getKind()) {
2731*0a6a1f1dSLionel Sambuc         case tok::kw_private:
2732*0a6a1f1dSLionel Sambuc         case tok::kw_protected:
2733*0a6a1f1dSLionel Sambuc         case tok::kw_public:
2734*0a6a1f1dSLionel Sambuc           SuggestFixIt = NextToken().getKind() == tok::colon;
2735*0a6a1f1dSLionel Sambuc           break;
2736*0a6a1f1dSLionel Sambuc         case tok::kw_static_assert:
2737*0a6a1f1dSLionel Sambuc         case tok::r_brace:
2738*0a6a1f1dSLionel Sambuc         case tok::kw_using:
2739*0a6a1f1dSLionel Sambuc         // base-clause can have simple-template-id; 'template' can't be there
2740*0a6a1f1dSLionel Sambuc         case tok::kw_template:
2741*0a6a1f1dSLionel Sambuc           SuggestFixIt = true;
2742*0a6a1f1dSLionel Sambuc           break;
2743*0a6a1f1dSLionel Sambuc         case tok::identifier:
2744*0a6a1f1dSLionel Sambuc           SuggestFixIt = isConstructorDeclarator(true);
2745*0a6a1f1dSLionel Sambuc           break;
2746*0a6a1f1dSLionel Sambuc         default:
2747*0a6a1f1dSLionel Sambuc           SuggestFixIt = isCXXSimpleDeclaration(/*AllowForRangeDecl=*/false);
2748*0a6a1f1dSLionel Sambuc           break;
2749*0a6a1f1dSLionel Sambuc         }
2750*0a6a1f1dSLionel Sambuc       }
2751*0a6a1f1dSLionel Sambuc       DiagnosticBuilder LBraceDiag =
2752*0a6a1f1dSLionel Sambuc           Diag(BraceLoc, diag::err_expected_lbrace_after_base_specifiers);
2753*0a6a1f1dSLionel Sambuc       if (SuggestFixIt) {
2754*0a6a1f1dSLionel Sambuc         LBraceDiag << FixItHint::CreateInsertion(BraceLoc, " {");
2755*0a6a1f1dSLionel Sambuc         // Try recovering from missing { after base-clause.
2756*0a6a1f1dSLionel Sambuc         PP.EnterToken(Tok);
2757*0a6a1f1dSLionel Sambuc         Tok.setKind(tok::l_brace);
2758*0a6a1f1dSLionel Sambuc       } else {
2759f4a2713aSLionel Sambuc         if (TagDecl)
2760f4a2713aSLionel Sambuc           Actions.ActOnTagDefinitionError(getCurScope(), TagDecl);
2761f4a2713aSLionel Sambuc         return;
2762f4a2713aSLionel Sambuc       }
2763f4a2713aSLionel Sambuc     }
2764*0a6a1f1dSLionel Sambuc   }
2765f4a2713aSLionel Sambuc 
2766f4a2713aSLionel Sambuc   assert(Tok.is(tok::l_brace));
2767f4a2713aSLionel Sambuc   BalancedDelimiterTracker T(*this, tok::l_brace);
2768f4a2713aSLionel Sambuc   T.consumeOpen();
2769f4a2713aSLionel Sambuc 
2770f4a2713aSLionel Sambuc   if (TagDecl)
2771f4a2713aSLionel Sambuc     Actions.ActOnStartCXXMemberDeclarations(getCurScope(), TagDecl, FinalLoc,
2772f4a2713aSLionel Sambuc                                             IsFinalSpelledSealed,
2773f4a2713aSLionel Sambuc                                             T.getOpenLocation());
2774f4a2713aSLionel Sambuc 
2775f4a2713aSLionel Sambuc   // C++ 11p3: Members of a class defined with the keyword class are private
2776f4a2713aSLionel Sambuc   // by default. Members of a class defined with the keywords struct or union
2777f4a2713aSLionel Sambuc   // are public by default.
2778f4a2713aSLionel Sambuc   AccessSpecifier CurAS;
2779f4a2713aSLionel Sambuc   if (TagType == DeclSpec::TST_class)
2780f4a2713aSLionel Sambuc     CurAS = AS_private;
2781f4a2713aSLionel Sambuc   else
2782f4a2713aSLionel Sambuc     CurAS = AS_public;
2783f4a2713aSLionel Sambuc   ParsedAttributes AccessAttrs(AttrFactory);
2784f4a2713aSLionel Sambuc 
2785f4a2713aSLionel Sambuc   if (TagDecl) {
2786f4a2713aSLionel Sambuc     // While we still have something to read, read the member-declarations.
2787*0a6a1f1dSLionel Sambuc     while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
2788f4a2713aSLionel Sambuc       // Each iteration of this loop reads one member-declaration.
2789f4a2713aSLionel Sambuc 
2790f4a2713aSLionel Sambuc       if (getLangOpts().MicrosoftExt && (Tok.is(tok::kw___if_exists) ||
2791f4a2713aSLionel Sambuc           Tok.is(tok::kw___if_not_exists))) {
2792f4a2713aSLionel Sambuc         ParseMicrosoftIfExistsClassDeclaration((DeclSpec::TST)TagType, CurAS);
2793f4a2713aSLionel Sambuc         continue;
2794f4a2713aSLionel Sambuc       }
2795f4a2713aSLionel Sambuc 
2796f4a2713aSLionel Sambuc       // Check for extraneous top-level semicolon.
2797f4a2713aSLionel Sambuc       if (Tok.is(tok::semi)) {
2798f4a2713aSLionel Sambuc         ConsumeExtraSemi(InsideStruct, TagType);
2799f4a2713aSLionel Sambuc         continue;
2800f4a2713aSLionel Sambuc       }
2801f4a2713aSLionel Sambuc 
2802f4a2713aSLionel Sambuc       if (Tok.is(tok::annot_pragma_vis)) {
2803f4a2713aSLionel Sambuc         HandlePragmaVisibility();
2804f4a2713aSLionel Sambuc         continue;
2805f4a2713aSLionel Sambuc       }
2806f4a2713aSLionel Sambuc 
2807f4a2713aSLionel Sambuc       if (Tok.is(tok::annot_pragma_pack)) {
2808f4a2713aSLionel Sambuc         HandlePragmaPack();
2809f4a2713aSLionel Sambuc         continue;
2810f4a2713aSLionel Sambuc       }
2811f4a2713aSLionel Sambuc 
2812f4a2713aSLionel Sambuc       if (Tok.is(tok::annot_pragma_align)) {
2813f4a2713aSLionel Sambuc         HandlePragmaAlign();
2814f4a2713aSLionel Sambuc         continue;
2815f4a2713aSLionel Sambuc       }
2816f4a2713aSLionel Sambuc 
2817f4a2713aSLionel Sambuc       if (Tok.is(tok::annot_pragma_openmp)) {
2818f4a2713aSLionel Sambuc         ParseOpenMPDeclarativeDirective();
2819f4a2713aSLionel Sambuc         continue;
2820f4a2713aSLionel Sambuc       }
2821f4a2713aSLionel Sambuc 
2822*0a6a1f1dSLionel Sambuc       if (Tok.is(tok::annot_pragma_ms_pointers_to_members)) {
2823*0a6a1f1dSLionel Sambuc         HandlePragmaMSPointersToMembers();
2824*0a6a1f1dSLionel Sambuc         continue;
2825*0a6a1f1dSLionel Sambuc       }
2826*0a6a1f1dSLionel Sambuc 
2827*0a6a1f1dSLionel Sambuc       if (Tok.is(tok::annot_pragma_ms_pragma)) {
2828*0a6a1f1dSLionel Sambuc         HandlePragmaMSPragma();
2829*0a6a1f1dSLionel Sambuc         continue;
2830*0a6a1f1dSLionel Sambuc       }
2831*0a6a1f1dSLionel Sambuc 
2832f4a2713aSLionel Sambuc       // If we see a namespace here, a close brace was missing somewhere.
2833f4a2713aSLionel Sambuc       if (Tok.is(tok::kw_namespace)) {
2834f4a2713aSLionel Sambuc         DiagnoseUnexpectedNamespace(cast<NamedDecl>(TagDecl));
2835f4a2713aSLionel Sambuc         break;
2836f4a2713aSLionel Sambuc       }
2837f4a2713aSLionel Sambuc 
2838f4a2713aSLionel Sambuc       AccessSpecifier AS = getAccessSpecifierIfPresent();
2839f4a2713aSLionel Sambuc       if (AS != AS_none) {
2840f4a2713aSLionel Sambuc         // Current token is a C++ access specifier.
2841f4a2713aSLionel Sambuc         CurAS = AS;
2842f4a2713aSLionel Sambuc         SourceLocation ASLoc = Tok.getLocation();
2843f4a2713aSLionel Sambuc         unsigned TokLength = Tok.getLength();
2844f4a2713aSLionel Sambuc         ConsumeToken();
2845f4a2713aSLionel Sambuc         AccessAttrs.clear();
2846f4a2713aSLionel Sambuc         MaybeParseGNUAttributes(AccessAttrs);
2847f4a2713aSLionel Sambuc 
2848f4a2713aSLionel Sambuc         SourceLocation EndLoc;
2849*0a6a1f1dSLionel Sambuc         if (TryConsumeToken(tok::colon, EndLoc)) {
2850*0a6a1f1dSLionel Sambuc         } else if (TryConsumeToken(tok::semi, EndLoc)) {
2851*0a6a1f1dSLionel Sambuc           Diag(EndLoc, diag::err_expected)
2852*0a6a1f1dSLionel Sambuc               << tok::colon << FixItHint::CreateReplacement(EndLoc, ":");
2853f4a2713aSLionel Sambuc         } else {
2854f4a2713aSLionel Sambuc           EndLoc = ASLoc.getLocWithOffset(TokLength);
2855*0a6a1f1dSLionel Sambuc           Diag(EndLoc, diag::err_expected)
2856*0a6a1f1dSLionel Sambuc               << tok::colon << FixItHint::CreateInsertion(EndLoc, ":");
2857f4a2713aSLionel Sambuc         }
2858f4a2713aSLionel Sambuc 
2859f4a2713aSLionel Sambuc         // The Microsoft extension __interface does not permit non-public
2860f4a2713aSLionel Sambuc         // access specifiers.
2861f4a2713aSLionel Sambuc         if (TagType == DeclSpec::TST_interface && CurAS != AS_public) {
2862f4a2713aSLionel Sambuc           Diag(ASLoc, diag::err_access_specifier_interface)
2863f4a2713aSLionel Sambuc             << (CurAS == AS_protected);
2864f4a2713aSLionel Sambuc         }
2865f4a2713aSLionel Sambuc 
2866f4a2713aSLionel Sambuc         if (Actions.ActOnAccessSpecifier(AS, ASLoc, EndLoc,
2867f4a2713aSLionel Sambuc                                          AccessAttrs.getList())) {
2868f4a2713aSLionel Sambuc           // found another attribute than only annotations
2869f4a2713aSLionel Sambuc           AccessAttrs.clear();
2870f4a2713aSLionel Sambuc         }
2871f4a2713aSLionel Sambuc 
2872f4a2713aSLionel Sambuc         continue;
2873f4a2713aSLionel Sambuc       }
2874f4a2713aSLionel Sambuc 
2875f4a2713aSLionel Sambuc       // Parse all the comma separated declarators.
2876f4a2713aSLionel Sambuc       ParseCXXClassMemberDeclaration(CurAS, AccessAttrs.getList());
2877f4a2713aSLionel Sambuc     }
2878f4a2713aSLionel Sambuc 
2879f4a2713aSLionel Sambuc     T.consumeClose();
2880f4a2713aSLionel Sambuc   } else {
2881f4a2713aSLionel Sambuc     SkipUntil(tok::r_brace);
2882f4a2713aSLionel Sambuc   }
2883f4a2713aSLionel Sambuc 
2884f4a2713aSLionel Sambuc   // If attributes exist after class contents, parse them.
2885f4a2713aSLionel Sambuc   ParsedAttributes attrs(AttrFactory);
2886f4a2713aSLionel Sambuc   MaybeParseGNUAttributes(attrs);
2887f4a2713aSLionel Sambuc 
2888f4a2713aSLionel Sambuc   if (TagDecl)
2889f4a2713aSLionel Sambuc     Actions.ActOnFinishCXXMemberSpecification(getCurScope(), RecordLoc, TagDecl,
2890f4a2713aSLionel Sambuc                                               T.getOpenLocation(),
2891f4a2713aSLionel Sambuc                                               T.getCloseLocation(),
2892f4a2713aSLionel Sambuc                                               attrs.getList());
2893f4a2713aSLionel Sambuc 
2894f4a2713aSLionel Sambuc   // C++11 [class.mem]p2:
2895f4a2713aSLionel Sambuc   //   Within the class member-specification, the class is regarded as complete
2896*0a6a1f1dSLionel Sambuc   //   within function bodies, default arguments, exception-specifications, and
2897f4a2713aSLionel Sambuc   //   brace-or-equal-initializers for non-static data members (including such
2898f4a2713aSLionel Sambuc   //   things in nested classes).
2899f4a2713aSLionel Sambuc   if (TagDecl && NonNestedClass) {
2900f4a2713aSLionel Sambuc     // We are not inside a nested class. This class and its nested classes
2901f4a2713aSLionel Sambuc     // are complete and we can parse the delayed portions of method
2902f4a2713aSLionel Sambuc     // declarations and the lexed inline method definitions, along with any
2903f4a2713aSLionel Sambuc     // delayed attributes.
2904f4a2713aSLionel Sambuc     SourceLocation SavedPrevTokLocation = PrevTokLocation;
2905f4a2713aSLionel Sambuc     ParseLexedAttributes(getCurrentClass());
2906f4a2713aSLionel Sambuc     ParseLexedMethodDeclarations(getCurrentClass());
2907f4a2713aSLionel Sambuc 
2908f4a2713aSLionel Sambuc     // We've finished with all pending member declarations.
2909f4a2713aSLionel Sambuc     Actions.ActOnFinishCXXMemberDecls();
2910f4a2713aSLionel Sambuc 
2911f4a2713aSLionel Sambuc     ParseLexedMemberInitializers(getCurrentClass());
2912f4a2713aSLionel Sambuc     ParseLexedMethodDefs(getCurrentClass());
2913f4a2713aSLionel Sambuc     PrevTokLocation = SavedPrevTokLocation;
2914f4a2713aSLionel Sambuc   }
2915f4a2713aSLionel Sambuc 
2916f4a2713aSLionel Sambuc   if (TagDecl)
2917f4a2713aSLionel Sambuc     Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
2918f4a2713aSLionel Sambuc                                      T.getCloseLocation());
2919f4a2713aSLionel Sambuc 
2920f4a2713aSLionel Sambuc   // Leave the class scope.
2921f4a2713aSLionel Sambuc   ParsingDef.Pop();
2922f4a2713aSLionel Sambuc   ClassScope.Exit();
2923f4a2713aSLionel Sambuc }
2924f4a2713aSLionel Sambuc 
DiagnoseUnexpectedNamespace(NamedDecl * D)2925f4a2713aSLionel Sambuc void Parser::DiagnoseUnexpectedNamespace(NamedDecl *D) {
2926f4a2713aSLionel Sambuc   assert(Tok.is(tok::kw_namespace));
2927f4a2713aSLionel Sambuc 
2928f4a2713aSLionel Sambuc   // FIXME: Suggest where the close brace should have gone by looking
2929f4a2713aSLionel Sambuc   // at indentation changes within the definition body.
2930f4a2713aSLionel Sambuc   Diag(D->getLocation(),
2931f4a2713aSLionel Sambuc        diag::err_missing_end_of_definition) << D;
2932f4a2713aSLionel Sambuc   Diag(Tok.getLocation(),
2933f4a2713aSLionel Sambuc        diag::note_missing_end_of_definition_before) << D;
2934f4a2713aSLionel Sambuc 
2935f4a2713aSLionel Sambuc   // Push '};' onto the token stream to recover.
2936f4a2713aSLionel Sambuc   PP.EnterToken(Tok);
2937f4a2713aSLionel Sambuc 
2938f4a2713aSLionel Sambuc   Tok.startToken();
2939f4a2713aSLionel Sambuc   Tok.setLocation(PP.getLocForEndOfToken(PrevTokLocation));
2940f4a2713aSLionel Sambuc   Tok.setKind(tok::semi);
2941f4a2713aSLionel Sambuc   PP.EnterToken(Tok);
2942f4a2713aSLionel Sambuc 
2943f4a2713aSLionel Sambuc   Tok.setKind(tok::r_brace);
2944f4a2713aSLionel Sambuc }
2945f4a2713aSLionel Sambuc 
2946f4a2713aSLionel Sambuc /// ParseConstructorInitializer - Parse a C++ constructor initializer,
2947f4a2713aSLionel Sambuc /// which explicitly initializes the members or base classes of a
2948f4a2713aSLionel Sambuc /// class (C++ [class.base.init]). For example, the three initializers
2949f4a2713aSLionel Sambuc /// after the ':' in the Derived constructor below:
2950f4a2713aSLionel Sambuc ///
2951f4a2713aSLionel Sambuc /// @code
2952f4a2713aSLionel Sambuc /// class Base { };
2953f4a2713aSLionel Sambuc /// class Derived : Base {
2954f4a2713aSLionel Sambuc ///   int x;
2955f4a2713aSLionel Sambuc ///   float f;
2956f4a2713aSLionel Sambuc /// public:
2957f4a2713aSLionel Sambuc ///   Derived(float f) : Base(), x(17), f(f) { }
2958f4a2713aSLionel Sambuc /// };
2959f4a2713aSLionel Sambuc /// @endcode
2960f4a2713aSLionel Sambuc ///
2961f4a2713aSLionel Sambuc /// [C++]  ctor-initializer:
2962f4a2713aSLionel Sambuc ///          ':' mem-initializer-list
2963f4a2713aSLionel Sambuc ///
2964f4a2713aSLionel Sambuc /// [C++]  mem-initializer-list:
2965f4a2713aSLionel Sambuc ///          mem-initializer ...[opt]
2966f4a2713aSLionel Sambuc ///          mem-initializer ...[opt] , mem-initializer-list
ParseConstructorInitializer(Decl * ConstructorDecl)2967f4a2713aSLionel Sambuc void Parser::ParseConstructorInitializer(Decl *ConstructorDecl) {
2968f4a2713aSLionel Sambuc   assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
2969f4a2713aSLionel Sambuc 
2970f4a2713aSLionel Sambuc   // Poison the SEH identifiers so they are flagged as illegal in constructor initializers
2971f4a2713aSLionel Sambuc   PoisonSEHIdentifiersRAIIObject PoisonSEHIdentifiers(*this, true);
2972f4a2713aSLionel Sambuc   SourceLocation ColonLoc = ConsumeToken();
2973f4a2713aSLionel Sambuc 
2974f4a2713aSLionel Sambuc   SmallVector<CXXCtorInitializer*, 4> MemInitializers;
2975f4a2713aSLionel Sambuc   bool AnyErrors = false;
2976f4a2713aSLionel Sambuc 
2977f4a2713aSLionel Sambuc   do {
2978f4a2713aSLionel Sambuc     if (Tok.is(tok::code_completion)) {
2979f4a2713aSLionel Sambuc       Actions.CodeCompleteConstructorInitializer(ConstructorDecl,
2980f4a2713aSLionel Sambuc                                                  MemInitializers);
2981f4a2713aSLionel Sambuc       return cutOffParsing();
2982f4a2713aSLionel Sambuc     } else {
2983f4a2713aSLionel Sambuc       MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
2984f4a2713aSLionel Sambuc       if (!MemInit.isInvalid())
2985f4a2713aSLionel Sambuc         MemInitializers.push_back(MemInit.get());
2986f4a2713aSLionel Sambuc       else
2987f4a2713aSLionel Sambuc         AnyErrors = true;
2988f4a2713aSLionel Sambuc     }
2989f4a2713aSLionel Sambuc 
2990f4a2713aSLionel Sambuc     if (Tok.is(tok::comma))
2991f4a2713aSLionel Sambuc       ConsumeToken();
2992f4a2713aSLionel Sambuc     else if (Tok.is(tok::l_brace))
2993f4a2713aSLionel Sambuc       break;
2994f4a2713aSLionel Sambuc     // If the next token looks like a base or member initializer, assume that
2995f4a2713aSLionel Sambuc     // we're just missing a comma.
2996f4a2713aSLionel Sambuc     else if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) {
2997f4a2713aSLionel Sambuc       SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
2998f4a2713aSLionel Sambuc       Diag(Loc, diag::err_ctor_init_missing_comma)
2999f4a2713aSLionel Sambuc         << FixItHint::CreateInsertion(Loc, ", ");
3000f4a2713aSLionel Sambuc     } else {
3001f4a2713aSLionel Sambuc       // Skip over garbage, until we get to '{'.  Don't eat the '{'.
3002*0a6a1f1dSLionel Sambuc       Diag(Tok.getLocation(), diag::err_expected_either) << tok::l_brace
3003*0a6a1f1dSLionel Sambuc                                                          << tok::comma;
3004f4a2713aSLionel Sambuc       SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch);
3005f4a2713aSLionel Sambuc       break;
3006f4a2713aSLionel Sambuc     }
3007f4a2713aSLionel Sambuc   } while (true);
3008f4a2713aSLionel Sambuc 
3009f4a2713aSLionel Sambuc   Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc, MemInitializers,
3010f4a2713aSLionel Sambuc                                AnyErrors);
3011f4a2713aSLionel Sambuc }
3012f4a2713aSLionel Sambuc 
3013f4a2713aSLionel Sambuc /// ParseMemInitializer - Parse a C++ member initializer, which is
3014f4a2713aSLionel Sambuc /// part of a constructor initializer that explicitly initializes one
3015f4a2713aSLionel Sambuc /// member or base class (C++ [class.base.init]). See
3016f4a2713aSLionel Sambuc /// ParseConstructorInitializer for an example.
3017f4a2713aSLionel Sambuc ///
3018f4a2713aSLionel Sambuc /// [C++] mem-initializer:
3019f4a2713aSLionel Sambuc ///         mem-initializer-id '(' expression-list[opt] ')'
3020f4a2713aSLionel Sambuc /// [C++0x] mem-initializer-id braced-init-list
3021f4a2713aSLionel Sambuc ///
3022f4a2713aSLionel Sambuc /// [C++] mem-initializer-id:
3023f4a2713aSLionel Sambuc ///         '::'[opt] nested-name-specifier[opt] class-name
3024f4a2713aSLionel Sambuc ///         identifier
ParseMemInitializer(Decl * ConstructorDecl)3025*0a6a1f1dSLionel Sambuc MemInitResult Parser::ParseMemInitializer(Decl *ConstructorDecl) {
3026f4a2713aSLionel Sambuc   // parse '::'[opt] nested-name-specifier[opt]
3027f4a2713aSLionel Sambuc   CXXScopeSpec SS;
3028f4a2713aSLionel Sambuc   ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
3029f4a2713aSLionel Sambuc   ParsedType TemplateTypeTy;
3030f4a2713aSLionel Sambuc   if (Tok.is(tok::annot_template_id)) {
3031f4a2713aSLionel Sambuc     TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
3032f4a2713aSLionel Sambuc     if (TemplateId->Kind == TNK_Type_template ||
3033f4a2713aSLionel Sambuc         TemplateId->Kind == TNK_Dependent_template_name) {
3034f4a2713aSLionel Sambuc       AnnotateTemplateIdTokenAsType();
3035f4a2713aSLionel Sambuc       assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
3036f4a2713aSLionel Sambuc       TemplateTypeTy = getTypeAnnotation(Tok);
3037f4a2713aSLionel Sambuc     }
3038f4a2713aSLionel Sambuc   }
3039f4a2713aSLionel Sambuc   // Uses of decltype will already have been converted to annot_decltype by
3040f4a2713aSLionel Sambuc   // ParseOptionalCXXScopeSpecifier at this point.
3041f4a2713aSLionel Sambuc   if (!TemplateTypeTy && Tok.isNot(tok::identifier)
3042f4a2713aSLionel Sambuc       && Tok.isNot(tok::annot_decltype)) {
3043f4a2713aSLionel Sambuc     Diag(Tok, diag::err_expected_member_or_base_name);
3044f4a2713aSLionel Sambuc     return true;
3045f4a2713aSLionel Sambuc   }
3046f4a2713aSLionel Sambuc 
3047*0a6a1f1dSLionel Sambuc   IdentifierInfo *II = nullptr;
3048f4a2713aSLionel Sambuc   DeclSpec DS(AttrFactory);
3049f4a2713aSLionel Sambuc   SourceLocation IdLoc = Tok.getLocation();
3050f4a2713aSLionel Sambuc   if (Tok.is(tok::annot_decltype)) {
3051f4a2713aSLionel Sambuc     // Get the decltype expression, if there is one.
3052f4a2713aSLionel Sambuc     ParseDecltypeSpecifier(DS);
3053f4a2713aSLionel Sambuc   } else {
3054f4a2713aSLionel Sambuc     if (Tok.is(tok::identifier))
3055f4a2713aSLionel Sambuc       // Get the identifier. This may be a member name or a class name,
3056f4a2713aSLionel Sambuc       // but we'll let the semantic analysis determine which it is.
3057f4a2713aSLionel Sambuc       II = Tok.getIdentifierInfo();
3058f4a2713aSLionel Sambuc     ConsumeToken();
3059f4a2713aSLionel Sambuc   }
3060f4a2713aSLionel Sambuc 
3061f4a2713aSLionel Sambuc 
3062f4a2713aSLionel Sambuc   // Parse the '('.
3063f4a2713aSLionel Sambuc   if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
3064f4a2713aSLionel Sambuc     Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
3065f4a2713aSLionel Sambuc 
3066f4a2713aSLionel Sambuc     ExprResult InitList = ParseBraceInitializer();
3067f4a2713aSLionel Sambuc     if (InitList.isInvalid())
3068f4a2713aSLionel Sambuc       return true;
3069f4a2713aSLionel Sambuc 
3070f4a2713aSLionel Sambuc     SourceLocation EllipsisLoc;
3071*0a6a1f1dSLionel Sambuc     TryConsumeToken(tok::ellipsis, EllipsisLoc);
3072f4a2713aSLionel Sambuc 
3073f4a2713aSLionel Sambuc     return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
3074f4a2713aSLionel Sambuc                                        TemplateTypeTy, DS, IdLoc,
3075*0a6a1f1dSLionel Sambuc                                        InitList.get(), EllipsisLoc);
3076f4a2713aSLionel Sambuc   } else if(Tok.is(tok::l_paren)) {
3077f4a2713aSLionel Sambuc     BalancedDelimiterTracker T(*this, tok::l_paren);
3078f4a2713aSLionel Sambuc     T.consumeOpen();
3079f4a2713aSLionel Sambuc 
3080f4a2713aSLionel Sambuc     // Parse the optional expression-list.
3081f4a2713aSLionel Sambuc     ExprVector ArgExprs;
3082f4a2713aSLionel Sambuc     CommaLocsTy CommaLocs;
3083f4a2713aSLionel Sambuc     if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
3084f4a2713aSLionel Sambuc       SkipUntil(tok::r_paren, StopAtSemi);
3085f4a2713aSLionel Sambuc       return true;
3086f4a2713aSLionel Sambuc     }
3087f4a2713aSLionel Sambuc 
3088f4a2713aSLionel Sambuc     T.consumeClose();
3089f4a2713aSLionel Sambuc 
3090f4a2713aSLionel Sambuc     SourceLocation EllipsisLoc;
3091*0a6a1f1dSLionel Sambuc     TryConsumeToken(tok::ellipsis, EllipsisLoc);
3092f4a2713aSLionel Sambuc 
3093f4a2713aSLionel Sambuc     return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
3094f4a2713aSLionel Sambuc                                        TemplateTypeTy, DS, IdLoc,
3095f4a2713aSLionel Sambuc                                        T.getOpenLocation(), ArgExprs,
3096f4a2713aSLionel Sambuc                                        T.getCloseLocation(), EllipsisLoc);
3097f4a2713aSLionel Sambuc   }
3098f4a2713aSLionel Sambuc 
3099*0a6a1f1dSLionel Sambuc   if (getLangOpts().CPlusPlus11)
3100*0a6a1f1dSLionel Sambuc     return Diag(Tok, diag::err_expected_either) << tok::l_paren << tok::l_brace;
3101*0a6a1f1dSLionel Sambuc   else
3102*0a6a1f1dSLionel Sambuc     return Diag(Tok, diag::err_expected) << tok::l_paren;
3103f4a2713aSLionel Sambuc }
3104f4a2713aSLionel Sambuc 
3105f4a2713aSLionel Sambuc /// \brief Parse a C++ exception-specification if present (C++0x [except.spec]).
3106f4a2713aSLionel Sambuc ///
3107f4a2713aSLionel Sambuc ///       exception-specification:
3108f4a2713aSLionel Sambuc ///         dynamic-exception-specification
3109f4a2713aSLionel Sambuc ///         noexcept-specification
3110f4a2713aSLionel Sambuc ///
3111f4a2713aSLionel Sambuc ///       noexcept-specification:
3112f4a2713aSLionel Sambuc ///         'noexcept'
3113f4a2713aSLionel Sambuc ///         'noexcept' '(' constant-expression ')'
3114f4a2713aSLionel Sambuc ExceptionSpecificationType
tryParseExceptionSpecification(bool Delayed,SourceRange & SpecificationRange,SmallVectorImpl<ParsedType> & DynamicExceptions,SmallVectorImpl<SourceRange> & DynamicExceptionRanges,ExprResult & NoexceptExpr,CachedTokens * & ExceptionSpecTokens)3115*0a6a1f1dSLionel Sambuc Parser::tryParseExceptionSpecification(bool Delayed,
3116f4a2713aSLionel Sambuc                     SourceRange &SpecificationRange,
3117f4a2713aSLionel Sambuc                     SmallVectorImpl<ParsedType> &DynamicExceptions,
3118f4a2713aSLionel Sambuc                     SmallVectorImpl<SourceRange> &DynamicExceptionRanges,
3119*0a6a1f1dSLionel Sambuc                     ExprResult &NoexceptExpr,
3120*0a6a1f1dSLionel Sambuc                     CachedTokens *&ExceptionSpecTokens) {
3121f4a2713aSLionel Sambuc   ExceptionSpecificationType Result = EST_None;
3122*0a6a1f1dSLionel Sambuc   ExceptionSpecTokens = 0;
3123*0a6a1f1dSLionel Sambuc 
3124*0a6a1f1dSLionel Sambuc   // Handle delayed parsing of exception-specifications.
3125*0a6a1f1dSLionel Sambuc   if (Delayed) {
3126*0a6a1f1dSLionel Sambuc     if (Tok.isNot(tok::kw_throw) && Tok.isNot(tok::kw_noexcept))
3127*0a6a1f1dSLionel Sambuc       return EST_None;
3128*0a6a1f1dSLionel Sambuc 
3129*0a6a1f1dSLionel Sambuc     // Consume and cache the starting token.
3130*0a6a1f1dSLionel Sambuc     bool IsNoexcept = Tok.is(tok::kw_noexcept);
3131*0a6a1f1dSLionel Sambuc     Token StartTok = Tok;
3132*0a6a1f1dSLionel Sambuc     SpecificationRange = SourceRange(ConsumeToken());
3133*0a6a1f1dSLionel Sambuc 
3134*0a6a1f1dSLionel Sambuc     // Check for a '('.
3135*0a6a1f1dSLionel Sambuc     if (!Tok.is(tok::l_paren)) {
3136*0a6a1f1dSLionel Sambuc       // If this is a bare 'noexcept', we're done.
3137*0a6a1f1dSLionel Sambuc       if (IsNoexcept) {
3138*0a6a1f1dSLionel Sambuc         Diag(Tok, diag::warn_cxx98_compat_noexcept_decl);
3139*0a6a1f1dSLionel Sambuc         NoexceptExpr = 0;
3140*0a6a1f1dSLionel Sambuc         return EST_BasicNoexcept;
3141*0a6a1f1dSLionel Sambuc       }
3142*0a6a1f1dSLionel Sambuc 
3143*0a6a1f1dSLionel Sambuc       Diag(Tok, diag::err_expected_lparen_after) << "throw";
3144*0a6a1f1dSLionel Sambuc       return EST_DynamicNone;
3145*0a6a1f1dSLionel Sambuc     }
3146*0a6a1f1dSLionel Sambuc 
3147*0a6a1f1dSLionel Sambuc     // Cache the tokens for the exception-specification.
3148*0a6a1f1dSLionel Sambuc     ExceptionSpecTokens = new CachedTokens;
3149*0a6a1f1dSLionel Sambuc     ExceptionSpecTokens->push_back(StartTok); // 'throw' or 'noexcept'
3150*0a6a1f1dSLionel Sambuc     ExceptionSpecTokens->push_back(Tok); // '('
3151*0a6a1f1dSLionel Sambuc     SpecificationRange.setEnd(ConsumeParen()); // '('
3152*0a6a1f1dSLionel Sambuc 
3153*0a6a1f1dSLionel Sambuc     ConsumeAndStoreUntil(tok::r_paren, *ExceptionSpecTokens,
3154*0a6a1f1dSLionel Sambuc                          /*StopAtSemi=*/true,
3155*0a6a1f1dSLionel Sambuc                          /*ConsumeFinalToken=*/true);
3156*0a6a1f1dSLionel Sambuc     SpecificationRange.setEnd(Tok.getLocation());
3157*0a6a1f1dSLionel Sambuc     return EST_Unparsed;
3158*0a6a1f1dSLionel Sambuc   }
3159f4a2713aSLionel Sambuc 
3160f4a2713aSLionel Sambuc   // See if there's a dynamic specification.
3161f4a2713aSLionel Sambuc   if (Tok.is(tok::kw_throw)) {
3162f4a2713aSLionel Sambuc     Result = ParseDynamicExceptionSpecification(SpecificationRange,
3163f4a2713aSLionel Sambuc                                                 DynamicExceptions,
3164f4a2713aSLionel Sambuc                                                 DynamicExceptionRanges);
3165f4a2713aSLionel Sambuc     assert(DynamicExceptions.size() == DynamicExceptionRanges.size() &&
3166f4a2713aSLionel Sambuc            "Produced different number of exception types and ranges.");
3167f4a2713aSLionel Sambuc   }
3168f4a2713aSLionel Sambuc 
3169f4a2713aSLionel Sambuc   // If there's no noexcept specification, we're done.
3170f4a2713aSLionel Sambuc   if (Tok.isNot(tok::kw_noexcept))
3171f4a2713aSLionel Sambuc     return Result;
3172f4a2713aSLionel Sambuc 
3173f4a2713aSLionel Sambuc   Diag(Tok, diag::warn_cxx98_compat_noexcept_decl);
3174f4a2713aSLionel Sambuc 
3175f4a2713aSLionel Sambuc   // If we already had a dynamic specification, parse the noexcept for,
3176f4a2713aSLionel Sambuc   // recovery, but emit a diagnostic and don't store the results.
3177f4a2713aSLionel Sambuc   SourceRange NoexceptRange;
3178f4a2713aSLionel Sambuc   ExceptionSpecificationType NoexceptType = EST_None;
3179f4a2713aSLionel Sambuc 
3180f4a2713aSLionel Sambuc   SourceLocation KeywordLoc = ConsumeToken();
3181f4a2713aSLionel Sambuc   if (Tok.is(tok::l_paren)) {
3182f4a2713aSLionel Sambuc     // There is an argument.
3183f4a2713aSLionel Sambuc     BalancedDelimiterTracker T(*this, tok::l_paren);
3184f4a2713aSLionel Sambuc     T.consumeOpen();
3185f4a2713aSLionel Sambuc     NoexceptType = EST_ComputedNoexcept;
3186f4a2713aSLionel Sambuc     NoexceptExpr = ParseConstantExpression();
3187f4a2713aSLionel Sambuc     // The argument must be contextually convertible to bool. We use
3188f4a2713aSLionel Sambuc     // ActOnBooleanCondition for this purpose.
3189f4a2713aSLionel Sambuc     if (!NoexceptExpr.isInvalid())
3190f4a2713aSLionel Sambuc       NoexceptExpr = Actions.ActOnBooleanCondition(getCurScope(), KeywordLoc,
3191f4a2713aSLionel Sambuc                                                    NoexceptExpr.get());
3192f4a2713aSLionel Sambuc     T.consumeClose();
3193f4a2713aSLionel Sambuc     NoexceptRange = SourceRange(KeywordLoc, T.getCloseLocation());
3194f4a2713aSLionel Sambuc   } else {
3195f4a2713aSLionel Sambuc     // There is no argument.
3196f4a2713aSLionel Sambuc     NoexceptType = EST_BasicNoexcept;
3197f4a2713aSLionel Sambuc     NoexceptRange = SourceRange(KeywordLoc, KeywordLoc);
3198f4a2713aSLionel Sambuc   }
3199f4a2713aSLionel Sambuc 
3200f4a2713aSLionel Sambuc   if (Result == EST_None) {
3201f4a2713aSLionel Sambuc     SpecificationRange = NoexceptRange;
3202f4a2713aSLionel Sambuc     Result = NoexceptType;
3203f4a2713aSLionel Sambuc 
3204f4a2713aSLionel Sambuc     // If there's a dynamic specification after a noexcept specification,
3205f4a2713aSLionel Sambuc     // parse that and ignore the results.
3206f4a2713aSLionel Sambuc     if (Tok.is(tok::kw_throw)) {
3207f4a2713aSLionel Sambuc       Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
3208f4a2713aSLionel Sambuc       ParseDynamicExceptionSpecification(NoexceptRange, DynamicExceptions,
3209f4a2713aSLionel Sambuc                                          DynamicExceptionRanges);
3210f4a2713aSLionel Sambuc     }
3211f4a2713aSLionel Sambuc   } else {
3212f4a2713aSLionel Sambuc     Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
3213f4a2713aSLionel Sambuc   }
3214f4a2713aSLionel Sambuc 
3215f4a2713aSLionel Sambuc   return Result;
3216f4a2713aSLionel Sambuc }
3217f4a2713aSLionel Sambuc 
diagnoseDynamicExceptionSpecification(Parser & P,const SourceRange & Range,bool IsNoexcept)3218f4a2713aSLionel Sambuc static void diagnoseDynamicExceptionSpecification(
3219f4a2713aSLionel Sambuc     Parser &P, const SourceRange &Range, bool IsNoexcept) {
3220f4a2713aSLionel Sambuc   if (P.getLangOpts().CPlusPlus11) {
3221f4a2713aSLionel Sambuc     const char *Replacement = IsNoexcept ? "noexcept" : "noexcept(false)";
3222f4a2713aSLionel Sambuc     P.Diag(Range.getBegin(), diag::warn_exception_spec_deprecated) << Range;
3223f4a2713aSLionel Sambuc     P.Diag(Range.getBegin(), diag::note_exception_spec_deprecated)
3224f4a2713aSLionel Sambuc       << Replacement << FixItHint::CreateReplacement(Range, Replacement);
3225f4a2713aSLionel Sambuc   }
3226f4a2713aSLionel Sambuc }
3227f4a2713aSLionel Sambuc 
3228f4a2713aSLionel Sambuc /// ParseDynamicExceptionSpecification - Parse a C++
3229f4a2713aSLionel Sambuc /// dynamic-exception-specification (C++ [except.spec]).
3230f4a2713aSLionel Sambuc ///
3231f4a2713aSLionel Sambuc ///       dynamic-exception-specification:
3232f4a2713aSLionel Sambuc ///         'throw' '(' type-id-list [opt] ')'
3233f4a2713aSLionel Sambuc /// [MS]    'throw' '(' '...' ')'
3234f4a2713aSLionel Sambuc ///
3235f4a2713aSLionel Sambuc ///       type-id-list:
3236f4a2713aSLionel Sambuc ///         type-id ... [opt]
3237f4a2713aSLionel Sambuc ///         type-id-list ',' type-id ... [opt]
3238f4a2713aSLionel Sambuc ///
ParseDynamicExceptionSpecification(SourceRange & SpecificationRange,SmallVectorImpl<ParsedType> & Exceptions,SmallVectorImpl<SourceRange> & Ranges)3239f4a2713aSLionel Sambuc ExceptionSpecificationType Parser::ParseDynamicExceptionSpecification(
3240f4a2713aSLionel Sambuc                                   SourceRange &SpecificationRange,
3241f4a2713aSLionel Sambuc                                   SmallVectorImpl<ParsedType> &Exceptions,
3242f4a2713aSLionel Sambuc                                   SmallVectorImpl<SourceRange> &Ranges) {
3243f4a2713aSLionel Sambuc   assert(Tok.is(tok::kw_throw) && "expected throw");
3244f4a2713aSLionel Sambuc 
3245f4a2713aSLionel Sambuc   SpecificationRange.setBegin(ConsumeToken());
3246f4a2713aSLionel Sambuc   BalancedDelimiterTracker T(*this, tok::l_paren);
3247f4a2713aSLionel Sambuc   if (T.consumeOpen()) {
3248f4a2713aSLionel Sambuc     Diag(Tok, diag::err_expected_lparen_after) << "throw";
3249f4a2713aSLionel Sambuc     SpecificationRange.setEnd(SpecificationRange.getBegin());
3250f4a2713aSLionel Sambuc     return EST_DynamicNone;
3251f4a2713aSLionel Sambuc   }
3252f4a2713aSLionel Sambuc 
3253f4a2713aSLionel Sambuc   // Parse throw(...), a Microsoft extension that means "this function
3254f4a2713aSLionel Sambuc   // can throw anything".
3255f4a2713aSLionel Sambuc   if (Tok.is(tok::ellipsis)) {
3256f4a2713aSLionel Sambuc     SourceLocation EllipsisLoc = ConsumeToken();
3257f4a2713aSLionel Sambuc     if (!getLangOpts().MicrosoftExt)
3258f4a2713aSLionel Sambuc       Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
3259f4a2713aSLionel Sambuc     T.consumeClose();
3260f4a2713aSLionel Sambuc     SpecificationRange.setEnd(T.getCloseLocation());
3261f4a2713aSLionel Sambuc     diagnoseDynamicExceptionSpecification(*this, SpecificationRange, false);
3262f4a2713aSLionel Sambuc     return EST_MSAny;
3263f4a2713aSLionel Sambuc   }
3264f4a2713aSLionel Sambuc 
3265f4a2713aSLionel Sambuc   // Parse the sequence of type-ids.
3266f4a2713aSLionel Sambuc   SourceRange Range;
3267f4a2713aSLionel Sambuc   while (Tok.isNot(tok::r_paren)) {
3268f4a2713aSLionel Sambuc     TypeResult Res(ParseTypeName(&Range));
3269f4a2713aSLionel Sambuc 
3270f4a2713aSLionel Sambuc     if (Tok.is(tok::ellipsis)) {
3271f4a2713aSLionel Sambuc       // C++0x [temp.variadic]p5:
3272f4a2713aSLionel Sambuc       //   - In a dynamic-exception-specification (15.4); the pattern is a
3273f4a2713aSLionel Sambuc       //     type-id.
3274f4a2713aSLionel Sambuc       SourceLocation Ellipsis = ConsumeToken();
3275f4a2713aSLionel Sambuc       Range.setEnd(Ellipsis);
3276f4a2713aSLionel Sambuc       if (!Res.isInvalid())
3277f4a2713aSLionel Sambuc         Res = Actions.ActOnPackExpansion(Res.get(), Ellipsis);
3278f4a2713aSLionel Sambuc     }
3279f4a2713aSLionel Sambuc 
3280f4a2713aSLionel Sambuc     if (!Res.isInvalid()) {
3281f4a2713aSLionel Sambuc       Exceptions.push_back(Res.get());
3282f4a2713aSLionel Sambuc       Ranges.push_back(Range);
3283f4a2713aSLionel Sambuc     }
3284f4a2713aSLionel Sambuc 
3285*0a6a1f1dSLionel Sambuc     if (!TryConsumeToken(tok::comma))
3286f4a2713aSLionel Sambuc       break;
3287f4a2713aSLionel Sambuc   }
3288f4a2713aSLionel Sambuc 
3289f4a2713aSLionel Sambuc   T.consumeClose();
3290f4a2713aSLionel Sambuc   SpecificationRange.setEnd(T.getCloseLocation());
3291f4a2713aSLionel Sambuc   diagnoseDynamicExceptionSpecification(*this, SpecificationRange,
3292f4a2713aSLionel Sambuc                                         Exceptions.empty());
3293f4a2713aSLionel Sambuc   return Exceptions.empty() ? EST_DynamicNone : EST_Dynamic;
3294f4a2713aSLionel Sambuc }
3295f4a2713aSLionel Sambuc 
3296f4a2713aSLionel Sambuc /// ParseTrailingReturnType - Parse a trailing return type on a new-style
3297f4a2713aSLionel Sambuc /// function declaration.
ParseTrailingReturnType(SourceRange & Range)3298f4a2713aSLionel Sambuc TypeResult Parser::ParseTrailingReturnType(SourceRange &Range) {
3299f4a2713aSLionel Sambuc   assert(Tok.is(tok::arrow) && "expected arrow");
3300f4a2713aSLionel Sambuc 
3301f4a2713aSLionel Sambuc   ConsumeToken();
3302f4a2713aSLionel Sambuc 
3303f4a2713aSLionel Sambuc   return ParseTypeName(&Range, Declarator::TrailingReturnContext);
3304f4a2713aSLionel Sambuc }
3305f4a2713aSLionel Sambuc 
3306f4a2713aSLionel Sambuc /// \brief We have just started parsing the definition of a new class,
3307f4a2713aSLionel Sambuc /// so push that class onto our stack of classes that is currently
3308f4a2713aSLionel Sambuc /// being parsed.
3309f4a2713aSLionel Sambuc Sema::ParsingClassState
PushParsingClass(Decl * ClassDecl,bool NonNestedClass,bool IsInterface)3310f4a2713aSLionel Sambuc Parser::PushParsingClass(Decl *ClassDecl, bool NonNestedClass,
3311f4a2713aSLionel Sambuc                          bool IsInterface) {
3312f4a2713aSLionel Sambuc   assert((NonNestedClass || !ClassStack.empty()) &&
3313f4a2713aSLionel Sambuc          "Nested class without outer class");
3314f4a2713aSLionel Sambuc   ClassStack.push(new ParsingClass(ClassDecl, NonNestedClass, IsInterface));
3315f4a2713aSLionel Sambuc   return Actions.PushParsingClass();
3316f4a2713aSLionel Sambuc }
3317f4a2713aSLionel Sambuc 
3318f4a2713aSLionel Sambuc /// \brief Deallocate the given parsed class and all of its nested
3319f4a2713aSLionel Sambuc /// classes.
DeallocateParsedClasses(Parser::ParsingClass * Class)3320f4a2713aSLionel Sambuc void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
3321f4a2713aSLionel Sambuc   for (unsigned I = 0, N = Class->LateParsedDeclarations.size(); I != N; ++I)
3322f4a2713aSLionel Sambuc     delete Class->LateParsedDeclarations[I];
3323f4a2713aSLionel Sambuc   delete Class;
3324f4a2713aSLionel Sambuc }
3325f4a2713aSLionel Sambuc 
3326f4a2713aSLionel Sambuc /// \brief Pop the top class of the stack of classes that are
3327f4a2713aSLionel Sambuc /// currently being parsed.
3328f4a2713aSLionel Sambuc ///
3329f4a2713aSLionel Sambuc /// This routine should be called when we have finished parsing the
3330f4a2713aSLionel Sambuc /// definition of a class, but have not yet popped the Scope
3331f4a2713aSLionel Sambuc /// associated with the class's definition.
PopParsingClass(Sema::ParsingClassState state)3332f4a2713aSLionel Sambuc void Parser::PopParsingClass(Sema::ParsingClassState state) {
3333f4a2713aSLionel Sambuc   assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
3334f4a2713aSLionel Sambuc 
3335f4a2713aSLionel Sambuc   Actions.PopParsingClass(state);
3336f4a2713aSLionel Sambuc 
3337f4a2713aSLionel Sambuc   ParsingClass *Victim = ClassStack.top();
3338f4a2713aSLionel Sambuc   ClassStack.pop();
3339f4a2713aSLionel Sambuc   if (Victim->TopLevelClass) {
3340f4a2713aSLionel Sambuc     // Deallocate all of the nested classes of this class,
3341f4a2713aSLionel Sambuc     // recursively: we don't need to keep any of this information.
3342f4a2713aSLionel Sambuc     DeallocateParsedClasses(Victim);
3343f4a2713aSLionel Sambuc     return;
3344f4a2713aSLionel Sambuc   }
3345f4a2713aSLionel Sambuc   assert(!ClassStack.empty() && "Missing top-level class?");
3346f4a2713aSLionel Sambuc 
3347f4a2713aSLionel Sambuc   if (Victim->LateParsedDeclarations.empty()) {
3348f4a2713aSLionel Sambuc     // The victim is a nested class, but we will not need to perform
3349f4a2713aSLionel Sambuc     // any processing after the definition of this class since it has
3350f4a2713aSLionel Sambuc     // no members whose handling was delayed. Therefore, we can just
3351f4a2713aSLionel Sambuc     // remove this nested class.
3352f4a2713aSLionel Sambuc     DeallocateParsedClasses(Victim);
3353f4a2713aSLionel Sambuc     return;
3354f4a2713aSLionel Sambuc   }
3355f4a2713aSLionel Sambuc 
3356f4a2713aSLionel Sambuc   // This nested class has some members that will need to be processed
3357f4a2713aSLionel Sambuc   // after the top-level class is completely defined. Therefore, add
3358f4a2713aSLionel Sambuc   // it to the list of nested classes within its parent.
3359f4a2713aSLionel Sambuc   assert(getCurScope()->isClassScope() && "Nested class outside of class scope?");
3360f4a2713aSLionel Sambuc   ClassStack.top()->LateParsedDeclarations.push_back(new LateParsedClass(this, Victim));
3361f4a2713aSLionel Sambuc   Victim->TemplateScope = getCurScope()->getParent()->isTemplateParamScope();
3362f4a2713aSLionel Sambuc }
3363f4a2713aSLionel Sambuc 
3364f4a2713aSLionel Sambuc /// \brief Try to parse an 'identifier' which appears within an attribute-token.
3365f4a2713aSLionel Sambuc ///
3366f4a2713aSLionel Sambuc /// \return the parsed identifier on success, and 0 if the next token is not an
3367f4a2713aSLionel Sambuc /// attribute-token.
3368f4a2713aSLionel Sambuc ///
3369f4a2713aSLionel Sambuc /// C++11 [dcl.attr.grammar]p3:
3370f4a2713aSLionel Sambuc ///   If a keyword or an alternative token that satisfies the syntactic
3371f4a2713aSLionel Sambuc ///   requirements of an identifier is contained in an attribute-token,
3372f4a2713aSLionel Sambuc ///   it is considered an identifier.
TryParseCXX11AttributeIdentifier(SourceLocation & Loc)3373f4a2713aSLionel Sambuc IdentifierInfo *Parser::TryParseCXX11AttributeIdentifier(SourceLocation &Loc) {
3374f4a2713aSLionel Sambuc   switch (Tok.getKind()) {
3375f4a2713aSLionel Sambuc   default:
3376f4a2713aSLionel Sambuc     // Identifiers and keywords have identifier info attached.
3377*0a6a1f1dSLionel Sambuc     if (!Tok.isAnnotation()) {
3378f4a2713aSLionel Sambuc       if (IdentifierInfo *II = Tok.getIdentifierInfo()) {
3379f4a2713aSLionel Sambuc         Loc = ConsumeToken();
3380f4a2713aSLionel Sambuc         return II;
3381f4a2713aSLionel Sambuc       }
3382*0a6a1f1dSLionel Sambuc     }
3383*0a6a1f1dSLionel Sambuc     return nullptr;
3384f4a2713aSLionel Sambuc 
3385f4a2713aSLionel Sambuc   case tok::ampamp:       // 'and'
3386f4a2713aSLionel Sambuc   case tok::pipe:         // 'bitor'
3387f4a2713aSLionel Sambuc   case tok::pipepipe:     // 'or'
3388f4a2713aSLionel Sambuc   case tok::caret:        // 'xor'
3389f4a2713aSLionel Sambuc   case tok::tilde:        // 'compl'
3390f4a2713aSLionel Sambuc   case tok::amp:          // 'bitand'
3391f4a2713aSLionel Sambuc   case tok::ampequal:     // 'and_eq'
3392f4a2713aSLionel Sambuc   case tok::pipeequal:    // 'or_eq'
3393f4a2713aSLionel Sambuc   case tok::caretequal:   // 'xor_eq'
3394f4a2713aSLionel Sambuc   case tok::exclaim:      // 'not'
3395f4a2713aSLionel Sambuc   case tok::exclaimequal: // 'not_eq'
3396f4a2713aSLionel Sambuc     // Alternative tokens do not have identifier info, but their spelling
3397f4a2713aSLionel Sambuc     // starts with an alphabetical character.
3398f4a2713aSLionel Sambuc     SmallString<8> SpellingBuf;
3399f4a2713aSLionel Sambuc     StringRef Spelling = PP.getSpelling(Tok.getLocation(), SpellingBuf);
3400f4a2713aSLionel Sambuc     if (isLetter(Spelling[0])) {
3401f4a2713aSLionel Sambuc       Loc = ConsumeToken();
3402f4a2713aSLionel Sambuc       return &PP.getIdentifierTable().get(Spelling);
3403f4a2713aSLionel Sambuc     }
3404*0a6a1f1dSLionel Sambuc     return nullptr;
3405f4a2713aSLionel Sambuc   }
3406f4a2713aSLionel Sambuc }
3407f4a2713aSLionel Sambuc 
IsBuiltInOrStandardCXX11Attribute(IdentifierInfo * AttrName,IdentifierInfo * ScopeName)3408f4a2713aSLionel Sambuc static bool IsBuiltInOrStandardCXX11Attribute(IdentifierInfo *AttrName,
3409f4a2713aSLionel Sambuc                                                IdentifierInfo *ScopeName) {
3410f4a2713aSLionel Sambuc   switch (AttributeList::getKind(AttrName, ScopeName,
3411f4a2713aSLionel Sambuc                                  AttributeList::AS_CXX11)) {
3412f4a2713aSLionel Sambuc   case AttributeList::AT_CarriesDependency:
3413*0a6a1f1dSLionel Sambuc   case AttributeList::AT_Deprecated:
3414f4a2713aSLionel Sambuc   case AttributeList::AT_FallThrough:
3415f4a2713aSLionel Sambuc   case AttributeList::AT_CXX11NoReturn: {
3416f4a2713aSLionel Sambuc     return true;
3417f4a2713aSLionel Sambuc   }
3418f4a2713aSLionel Sambuc 
3419f4a2713aSLionel Sambuc   default:
3420f4a2713aSLionel Sambuc     return false;
3421f4a2713aSLionel Sambuc   }
3422f4a2713aSLionel Sambuc }
3423f4a2713aSLionel Sambuc 
3424*0a6a1f1dSLionel Sambuc /// ParseCXX11AttributeArgs -- Parse a C++11 attribute-argument-clause.
3425*0a6a1f1dSLionel Sambuc ///
3426*0a6a1f1dSLionel Sambuc /// [C++11] attribute-argument-clause:
3427*0a6a1f1dSLionel Sambuc ///         '(' balanced-token-seq ')'
3428*0a6a1f1dSLionel Sambuc ///
3429*0a6a1f1dSLionel Sambuc /// [C++11] balanced-token-seq:
3430*0a6a1f1dSLionel Sambuc ///         balanced-token
3431*0a6a1f1dSLionel Sambuc ///         balanced-token-seq balanced-token
3432*0a6a1f1dSLionel Sambuc ///
3433*0a6a1f1dSLionel Sambuc /// [C++11] balanced-token:
3434*0a6a1f1dSLionel Sambuc ///         '(' balanced-token-seq ')'
3435*0a6a1f1dSLionel Sambuc ///         '[' balanced-token-seq ']'
3436*0a6a1f1dSLionel Sambuc ///         '{' balanced-token-seq '}'
3437*0a6a1f1dSLionel Sambuc ///         any token but '(', ')', '[', ']', '{', or '}'
ParseCXX11AttributeArgs(IdentifierInfo * AttrName,SourceLocation AttrNameLoc,ParsedAttributes & Attrs,SourceLocation * EndLoc,IdentifierInfo * ScopeName,SourceLocation ScopeLoc)3438*0a6a1f1dSLionel Sambuc bool Parser::ParseCXX11AttributeArgs(IdentifierInfo *AttrName,
3439*0a6a1f1dSLionel Sambuc                                      SourceLocation AttrNameLoc,
3440*0a6a1f1dSLionel Sambuc                                      ParsedAttributes &Attrs,
3441*0a6a1f1dSLionel Sambuc                                      SourceLocation *EndLoc,
3442*0a6a1f1dSLionel Sambuc                                      IdentifierInfo *ScopeName,
3443*0a6a1f1dSLionel Sambuc                                      SourceLocation ScopeLoc) {
3444*0a6a1f1dSLionel Sambuc   assert(Tok.is(tok::l_paren) && "Not a C++11 attribute argument list");
3445*0a6a1f1dSLionel Sambuc   SourceLocation LParenLoc = Tok.getLocation();
3446*0a6a1f1dSLionel Sambuc 
3447*0a6a1f1dSLionel Sambuc   // If the attribute isn't known, we will not attempt to parse any
3448*0a6a1f1dSLionel Sambuc   // arguments.
3449*0a6a1f1dSLionel Sambuc   if (!hasAttribute(AttrSyntax::CXX, ScopeName, AttrName,
3450*0a6a1f1dSLionel Sambuc                     getTargetInfo().getTriple(), getLangOpts())) {
3451*0a6a1f1dSLionel Sambuc     // Eat the left paren, then skip to the ending right paren.
3452*0a6a1f1dSLionel Sambuc     ConsumeParen();
3453*0a6a1f1dSLionel Sambuc     SkipUntil(tok::r_paren);
3454*0a6a1f1dSLionel Sambuc     return false;
3455*0a6a1f1dSLionel Sambuc   }
3456*0a6a1f1dSLionel Sambuc 
3457*0a6a1f1dSLionel Sambuc   if (ScopeName && ScopeName->getName() == "gnu")
3458*0a6a1f1dSLionel Sambuc     // GNU-scoped attributes have some special cases to handle GNU-specific
3459*0a6a1f1dSLionel Sambuc     // behaviors.
3460*0a6a1f1dSLionel Sambuc     ParseGNUAttributeArgs(AttrName, AttrNameLoc, Attrs, EndLoc, ScopeName,
3461*0a6a1f1dSLionel Sambuc                           ScopeLoc, AttributeList::AS_CXX11, nullptr);
3462*0a6a1f1dSLionel Sambuc   else {
3463*0a6a1f1dSLionel Sambuc     unsigned NumArgs =
3464*0a6a1f1dSLionel Sambuc         ParseAttributeArgsCommon(AttrName, AttrNameLoc, Attrs, EndLoc,
3465*0a6a1f1dSLionel Sambuc                                  ScopeName, ScopeLoc, AttributeList::AS_CXX11);
3466*0a6a1f1dSLionel Sambuc 
3467*0a6a1f1dSLionel Sambuc     const AttributeList *Attr = Attrs.getList();
3468*0a6a1f1dSLionel Sambuc     if (Attr && IsBuiltInOrStandardCXX11Attribute(AttrName, ScopeName)) {
3469*0a6a1f1dSLionel Sambuc       // If the attribute is a standard or built-in attribute and we are
3470*0a6a1f1dSLionel Sambuc       // parsing an argument list, we need to determine whether this attribute
3471*0a6a1f1dSLionel Sambuc       // was allowed to have an argument list (such as [[deprecated]]), and how
3472*0a6a1f1dSLionel Sambuc       // many arguments were parsed (so we can diagnose on [[deprecated()]]).
3473*0a6a1f1dSLionel Sambuc       if (Attr->getMaxArgs() && !NumArgs) {
3474*0a6a1f1dSLionel Sambuc         // The attribute was allowed to have arguments, but none were provided
3475*0a6a1f1dSLionel Sambuc         // even though the attribute parsed successfully. This is an error.
3476*0a6a1f1dSLionel Sambuc         Diag(LParenLoc, diag::err_attribute_requires_arguments) << AttrName;
3477*0a6a1f1dSLionel Sambuc         return false;
3478*0a6a1f1dSLionel Sambuc       } else if (!Attr->getMaxArgs()) {
3479*0a6a1f1dSLionel Sambuc         // The attribute parsed successfully, but was not allowed to have any
3480*0a6a1f1dSLionel Sambuc         // arguments. It doesn't matter whether any were provided -- the
3481*0a6a1f1dSLionel Sambuc         // presence of the argument list (even if empty) is diagnosed.
3482*0a6a1f1dSLionel Sambuc         Diag(LParenLoc, diag::err_cxx11_attribute_forbids_arguments)
3483*0a6a1f1dSLionel Sambuc             << AttrName
3484*0a6a1f1dSLionel Sambuc             << FixItHint::CreateRemoval(SourceRange(LParenLoc, *EndLoc));
3485*0a6a1f1dSLionel Sambuc         return false;
3486*0a6a1f1dSLionel Sambuc       }
3487*0a6a1f1dSLionel Sambuc     }
3488*0a6a1f1dSLionel Sambuc   }
3489*0a6a1f1dSLionel Sambuc   return true;
3490*0a6a1f1dSLionel Sambuc }
3491*0a6a1f1dSLionel Sambuc 
3492*0a6a1f1dSLionel Sambuc /// ParseCXX11AttributeSpecifier - Parse a C++11 attribute-specifier.
3493f4a2713aSLionel Sambuc ///
3494f4a2713aSLionel Sambuc /// [C++11] attribute-specifier:
3495f4a2713aSLionel Sambuc ///         '[' '[' attribute-list ']' ']'
3496f4a2713aSLionel Sambuc ///         alignment-specifier
3497f4a2713aSLionel Sambuc ///
3498f4a2713aSLionel Sambuc /// [C++11] attribute-list:
3499f4a2713aSLionel Sambuc ///         attribute[opt]
3500f4a2713aSLionel Sambuc ///         attribute-list ',' attribute[opt]
3501f4a2713aSLionel Sambuc ///         attribute '...'
3502f4a2713aSLionel Sambuc ///         attribute-list ',' attribute '...'
3503f4a2713aSLionel Sambuc ///
3504f4a2713aSLionel Sambuc /// [C++11] attribute:
3505f4a2713aSLionel Sambuc ///         attribute-token attribute-argument-clause[opt]
3506f4a2713aSLionel Sambuc ///
3507f4a2713aSLionel Sambuc /// [C++11] attribute-token:
3508f4a2713aSLionel Sambuc ///         identifier
3509f4a2713aSLionel Sambuc ///         attribute-scoped-token
3510f4a2713aSLionel Sambuc ///
3511f4a2713aSLionel Sambuc /// [C++11] attribute-scoped-token:
3512f4a2713aSLionel Sambuc ///         attribute-namespace '::' identifier
3513f4a2713aSLionel Sambuc ///
3514f4a2713aSLionel Sambuc /// [C++11] attribute-namespace:
3515f4a2713aSLionel Sambuc ///         identifier
ParseCXX11AttributeSpecifier(ParsedAttributes & attrs,SourceLocation * endLoc)3516f4a2713aSLionel Sambuc void Parser::ParseCXX11AttributeSpecifier(ParsedAttributes &attrs,
3517f4a2713aSLionel Sambuc                                           SourceLocation *endLoc) {
3518f4a2713aSLionel Sambuc   if (Tok.is(tok::kw_alignas)) {
3519f4a2713aSLionel Sambuc     Diag(Tok.getLocation(), diag::warn_cxx98_compat_alignas);
3520f4a2713aSLionel Sambuc     ParseAlignmentSpecifier(attrs, endLoc);
3521f4a2713aSLionel Sambuc     return;
3522f4a2713aSLionel Sambuc   }
3523f4a2713aSLionel Sambuc 
3524f4a2713aSLionel Sambuc   assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square)
3525f4a2713aSLionel Sambuc       && "Not a C++11 attribute list");
3526f4a2713aSLionel Sambuc 
3527f4a2713aSLionel Sambuc   Diag(Tok.getLocation(), diag::warn_cxx98_compat_attribute);
3528f4a2713aSLionel Sambuc 
3529f4a2713aSLionel Sambuc   ConsumeBracket();
3530f4a2713aSLionel Sambuc   ConsumeBracket();
3531f4a2713aSLionel Sambuc 
3532f4a2713aSLionel Sambuc   llvm::SmallDenseMap<IdentifierInfo*, SourceLocation, 4> SeenAttrs;
3533f4a2713aSLionel Sambuc 
3534f4a2713aSLionel Sambuc   while (Tok.isNot(tok::r_square)) {
3535f4a2713aSLionel Sambuc     // attribute not present
3536*0a6a1f1dSLionel Sambuc     if (TryConsumeToken(tok::comma))
3537f4a2713aSLionel Sambuc       continue;
3538f4a2713aSLionel Sambuc 
3539f4a2713aSLionel Sambuc     SourceLocation ScopeLoc, AttrLoc;
3540*0a6a1f1dSLionel Sambuc     IdentifierInfo *ScopeName = nullptr, *AttrName = nullptr;
3541f4a2713aSLionel Sambuc 
3542f4a2713aSLionel Sambuc     AttrName = TryParseCXX11AttributeIdentifier(AttrLoc);
3543f4a2713aSLionel Sambuc     if (!AttrName)
3544f4a2713aSLionel Sambuc       // Break out to the "expected ']'" diagnostic.
3545f4a2713aSLionel Sambuc       break;
3546f4a2713aSLionel Sambuc 
3547f4a2713aSLionel Sambuc     // scoped attribute
3548*0a6a1f1dSLionel Sambuc     if (TryConsumeToken(tok::coloncolon)) {
3549f4a2713aSLionel Sambuc       ScopeName = AttrName;
3550f4a2713aSLionel Sambuc       ScopeLoc = AttrLoc;
3551f4a2713aSLionel Sambuc 
3552f4a2713aSLionel Sambuc       AttrName = TryParseCXX11AttributeIdentifier(AttrLoc);
3553f4a2713aSLionel Sambuc       if (!AttrName) {
3554*0a6a1f1dSLionel Sambuc         Diag(Tok.getLocation(), diag::err_expected) << tok::identifier;
3555f4a2713aSLionel Sambuc         SkipUntil(tok::r_square, tok::comma, StopAtSemi | StopBeforeMatch);
3556f4a2713aSLionel Sambuc         continue;
3557f4a2713aSLionel Sambuc       }
3558f4a2713aSLionel Sambuc     }
3559f4a2713aSLionel Sambuc 
3560f4a2713aSLionel Sambuc     bool StandardAttr = IsBuiltInOrStandardCXX11Attribute(AttrName, ScopeName);
3561f4a2713aSLionel Sambuc     bool AttrParsed = false;
3562f4a2713aSLionel Sambuc 
3563f4a2713aSLionel Sambuc     if (StandardAttr &&
3564f4a2713aSLionel Sambuc         !SeenAttrs.insert(std::make_pair(AttrName, AttrLoc)).second)
3565f4a2713aSLionel Sambuc       Diag(AttrLoc, diag::err_cxx11_attribute_repeated)
3566f4a2713aSLionel Sambuc           << AttrName << SourceRange(SeenAttrs[AttrName]);
3567f4a2713aSLionel Sambuc 
3568f4a2713aSLionel Sambuc     // Parse attribute arguments
3569*0a6a1f1dSLionel Sambuc     if (Tok.is(tok::l_paren))
3570*0a6a1f1dSLionel Sambuc       AttrParsed = ParseCXX11AttributeArgs(AttrName, AttrLoc, attrs, endLoc,
3571*0a6a1f1dSLionel Sambuc                                            ScopeName, ScopeLoc);
3572f4a2713aSLionel Sambuc 
3573f4a2713aSLionel Sambuc     if (!AttrParsed)
3574f4a2713aSLionel Sambuc       attrs.addNew(AttrName,
3575f4a2713aSLionel Sambuc                    SourceRange(ScopeLoc.isValid() ? ScopeLoc : AttrLoc,
3576f4a2713aSLionel Sambuc                                AttrLoc),
3577*0a6a1f1dSLionel Sambuc                    ScopeName, ScopeLoc, nullptr, 0, AttributeList::AS_CXX11);
3578f4a2713aSLionel Sambuc 
3579*0a6a1f1dSLionel Sambuc     if (TryConsumeToken(tok::ellipsis))
3580f4a2713aSLionel Sambuc       Diag(Tok, diag::err_cxx11_attribute_forbids_ellipsis)
3581f4a2713aSLionel Sambuc         << AttrName->getName();
3582f4a2713aSLionel Sambuc   }
3583f4a2713aSLionel Sambuc 
3584*0a6a1f1dSLionel Sambuc   if (ExpectAndConsume(tok::r_square))
3585f4a2713aSLionel Sambuc     SkipUntil(tok::r_square);
3586f4a2713aSLionel Sambuc   if (endLoc)
3587f4a2713aSLionel Sambuc     *endLoc = Tok.getLocation();
3588*0a6a1f1dSLionel Sambuc   if (ExpectAndConsume(tok::r_square))
3589f4a2713aSLionel Sambuc     SkipUntil(tok::r_square);
3590f4a2713aSLionel Sambuc }
3591f4a2713aSLionel Sambuc 
3592f4a2713aSLionel Sambuc /// ParseCXX11Attributes - Parse a C++11 attribute-specifier-seq.
3593f4a2713aSLionel Sambuc ///
3594f4a2713aSLionel Sambuc /// attribute-specifier-seq:
3595f4a2713aSLionel Sambuc ///       attribute-specifier-seq[opt] attribute-specifier
ParseCXX11Attributes(ParsedAttributesWithRange & attrs,SourceLocation * endLoc)3596f4a2713aSLionel Sambuc void Parser::ParseCXX11Attributes(ParsedAttributesWithRange &attrs,
3597f4a2713aSLionel Sambuc                                   SourceLocation *endLoc) {
3598f4a2713aSLionel Sambuc   assert(getLangOpts().CPlusPlus11);
3599f4a2713aSLionel Sambuc 
3600f4a2713aSLionel Sambuc   SourceLocation StartLoc = Tok.getLocation(), Loc;
3601f4a2713aSLionel Sambuc   if (!endLoc)
3602f4a2713aSLionel Sambuc     endLoc = &Loc;
3603f4a2713aSLionel Sambuc 
3604f4a2713aSLionel Sambuc   do {
3605f4a2713aSLionel Sambuc     ParseCXX11AttributeSpecifier(attrs, endLoc);
3606f4a2713aSLionel Sambuc   } while (isCXX11AttributeSpecifier());
3607f4a2713aSLionel Sambuc 
3608f4a2713aSLionel Sambuc   attrs.Range = SourceRange(StartLoc, *endLoc);
3609f4a2713aSLionel Sambuc }
3610f4a2713aSLionel Sambuc 
DiagnoseAndSkipCXX11Attributes()3611f4a2713aSLionel Sambuc void Parser::DiagnoseAndSkipCXX11Attributes() {
3612f4a2713aSLionel Sambuc   // Start and end location of an attribute or an attribute list.
3613f4a2713aSLionel Sambuc   SourceLocation StartLoc = Tok.getLocation();
3614*0a6a1f1dSLionel Sambuc   SourceLocation EndLoc = SkipCXX11Attributes();
3615*0a6a1f1dSLionel Sambuc 
3616*0a6a1f1dSLionel Sambuc   if (EndLoc.isValid()) {
3617*0a6a1f1dSLionel Sambuc     SourceRange Range(StartLoc, EndLoc);
3618*0a6a1f1dSLionel Sambuc     Diag(StartLoc, diag::err_attributes_not_allowed)
3619*0a6a1f1dSLionel Sambuc       << Range;
3620*0a6a1f1dSLionel Sambuc   }
3621*0a6a1f1dSLionel Sambuc }
3622*0a6a1f1dSLionel Sambuc 
SkipCXX11Attributes()3623*0a6a1f1dSLionel Sambuc SourceLocation Parser::SkipCXX11Attributes() {
3624f4a2713aSLionel Sambuc   SourceLocation EndLoc;
3625f4a2713aSLionel Sambuc 
3626*0a6a1f1dSLionel Sambuc   if (!isCXX11AttributeSpecifier())
3627*0a6a1f1dSLionel Sambuc     return EndLoc;
3628*0a6a1f1dSLionel Sambuc 
3629f4a2713aSLionel Sambuc   do {
3630f4a2713aSLionel Sambuc     if (Tok.is(tok::l_square)) {
3631f4a2713aSLionel Sambuc       BalancedDelimiterTracker T(*this, tok::l_square);
3632f4a2713aSLionel Sambuc       T.consumeOpen();
3633f4a2713aSLionel Sambuc       T.skipToEnd();
3634f4a2713aSLionel Sambuc       EndLoc = T.getCloseLocation();
3635f4a2713aSLionel Sambuc     } else {
3636f4a2713aSLionel Sambuc       assert(Tok.is(tok::kw_alignas) && "not an attribute specifier");
3637f4a2713aSLionel Sambuc       ConsumeToken();
3638f4a2713aSLionel Sambuc       BalancedDelimiterTracker T(*this, tok::l_paren);
3639f4a2713aSLionel Sambuc       if (!T.consumeOpen())
3640f4a2713aSLionel Sambuc         T.skipToEnd();
3641f4a2713aSLionel Sambuc       EndLoc = T.getCloseLocation();
3642f4a2713aSLionel Sambuc     }
3643f4a2713aSLionel Sambuc   } while (isCXX11AttributeSpecifier());
3644f4a2713aSLionel Sambuc 
3645*0a6a1f1dSLionel Sambuc   return EndLoc;
3646f4a2713aSLionel Sambuc }
3647f4a2713aSLionel Sambuc 
3648f4a2713aSLionel Sambuc /// ParseMicrosoftAttributes - Parse a Microsoft attribute [Attr]
3649f4a2713aSLionel Sambuc ///
3650f4a2713aSLionel Sambuc /// [MS] ms-attribute:
3651f4a2713aSLionel Sambuc ///             '[' token-seq ']'
3652f4a2713aSLionel Sambuc ///
3653f4a2713aSLionel Sambuc /// [MS] ms-attribute-seq:
3654f4a2713aSLionel Sambuc ///             ms-attribute[opt]
3655f4a2713aSLionel Sambuc ///             ms-attribute ms-attribute-seq
ParseMicrosoftAttributes(ParsedAttributes & attrs,SourceLocation * endLoc)3656f4a2713aSLionel Sambuc void Parser::ParseMicrosoftAttributes(ParsedAttributes &attrs,
3657f4a2713aSLionel Sambuc                                       SourceLocation *endLoc) {
3658f4a2713aSLionel Sambuc   assert(Tok.is(tok::l_square) && "Not a Microsoft attribute list");
3659f4a2713aSLionel Sambuc 
3660f4a2713aSLionel Sambuc   while (Tok.is(tok::l_square)) {
3661f4a2713aSLionel Sambuc     // FIXME: If this is actually a C++11 attribute, parse it as one.
3662f4a2713aSLionel Sambuc     ConsumeBracket();
3663f4a2713aSLionel Sambuc     SkipUntil(tok::r_square, StopAtSemi | StopBeforeMatch);
3664f4a2713aSLionel Sambuc     if (endLoc) *endLoc = Tok.getLocation();
3665*0a6a1f1dSLionel Sambuc     ExpectAndConsume(tok::r_square);
3666f4a2713aSLionel Sambuc   }
3667f4a2713aSLionel Sambuc }
3668f4a2713aSLionel Sambuc 
ParseMicrosoftIfExistsClassDeclaration(DeclSpec::TST TagType,AccessSpecifier & CurAS)3669f4a2713aSLionel Sambuc void Parser::ParseMicrosoftIfExistsClassDeclaration(DeclSpec::TST TagType,
3670f4a2713aSLionel Sambuc                                                     AccessSpecifier& CurAS) {
3671f4a2713aSLionel Sambuc   IfExistsCondition Result;
3672f4a2713aSLionel Sambuc   if (ParseMicrosoftIfExistsCondition(Result))
3673f4a2713aSLionel Sambuc     return;
3674f4a2713aSLionel Sambuc 
3675f4a2713aSLionel Sambuc   BalancedDelimiterTracker Braces(*this, tok::l_brace);
3676f4a2713aSLionel Sambuc   if (Braces.consumeOpen()) {
3677*0a6a1f1dSLionel Sambuc     Diag(Tok, diag::err_expected) << tok::l_brace;
3678f4a2713aSLionel Sambuc     return;
3679f4a2713aSLionel Sambuc   }
3680f4a2713aSLionel Sambuc 
3681f4a2713aSLionel Sambuc   switch (Result.Behavior) {
3682f4a2713aSLionel Sambuc   case IEB_Parse:
3683f4a2713aSLionel Sambuc     // Parse the declarations below.
3684f4a2713aSLionel Sambuc     break;
3685f4a2713aSLionel Sambuc 
3686f4a2713aSLionel Sambuc   case IEB_Dependent:
3687f4a2713aSLionel Sambuc     Diag(Result.KeywordLoc, diag::warn_microsoft_dependent_exists)
3688f4a2713aSLionel Sambuc       << Result.IsIfExists;
3689f4a2713aSLionel Sambuc     // Fall through to skip.
3690f4a2713aSLionel Sambuc 
3691f4a2713aSLionel Sambuc   case IEB_Skip:
3692f4a2713aSLionel Sambuc     Braces.skipToEnd();
3693f4a2713aSLionel Sambuc     return;
3694f4a2713aSLionel Sambuc   }
3695f4a2713aSLionel Sambuc 
3696*0a6a1f1dSLionel Sambuc   while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
3697f4a2713aSLionel Sambuc     // __if_exists, __if_not_exists can nest.
3698f4a2713aSLionel Sambuc     if ((Tok.is(tok::kw___if_exists) || Tok.is(tok::kw___if_not_exists))) {
3699f4a2713aSLionel Sambuc       ParseMicrosoftIfExistsClassDeclaration((DeclSpec::TST)TagType, CurAS);
3700f4a2713aSLionel Sambuc       continue;
3701f4a2713aSLionel Sambuc     }
3702f4a2713aSLionel Sambuc 
3703f4a2713aSLionel Sambuc     // Check for extraneous top-level semicolon.
3704f4a2713aSLionel Sambuc     if (Tok.is(tok::semi)) {
3705f4a2713aSLionel Sambuc       ConsumeExtraSemi(InsideStruct, TagType);
3706f4a2713aSLionel Sambuc       continue;
3707f4a2713aSLionel Sambuc     }
3708f4a2713aSLionel Sambuc 
3709f4a2713aSLionel Sambuc     AccessSpecifier AS = getAccessSpecifierIfPresent();
3710f4a2713aSLionel Sambuc     if (AS != AS_none) {
3711f4a2713aSLionel Sambuc       // Current token is a C++ access specifier.
3712f4a2713aSLionel Sambuc       CurAS = AS;
3713f4a2713aSLionel Sambuc       SourceLocation ASLoc = Tok.getLocation();
3714f4a2713aSLionel Sambuc       ConsumeToken();
3715f4a2713aSLionel Sambuc       if (Tok.is(tok::colon))
3716f4a2713aSLionel Sambuc         Actions.ActOnAccessSpecifier(AS, ASLoc, Tok.getLocation());
3717f4a2713aSLionel Sambuc       else
3718*0a6a1f1dSLionel Sambuc         Diag(Tok, diag::err_expected) << tok::colon;
3719f4a2713aSLionel Sambuc       ConsumeToken();
3720f4a2713aSLionel Sambuc       continue;
3721f4a2713aSLionel Sambuc     }
3722f4a2713aSLionel Sambuc 
3723f4a2713aSLionel Sambuc     // Parse all the comma separated declarators.
3724*0a6a1f1dSLionel Sambuc     ParseCXXClassMemberDeclaration(CurAS, nullptr);
3725f4a2713aSLionel Sambuc   }
3726f4a2713aSLionel Sambuc 
3727f4a2713aSLionel Sambuc   Braces.consumeClose();
3728f4a2713aSLionel Sambuc }
3729