2012-12-18 22:30:41 +08:00
|
|
|
//===--- ParseTentative.cpp - Ambiguity Resolution Parsing ----------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the tentative parsing portions of the Parser
|
|
|
|
// interfaces, for ambiguity resolution.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Parse/Parser.h"
|
|
|
|
#include "clang/Parse/ParseDiagnostic.h"
|
|
|
|
#include "clang/Sema/ParsedTemplate.h"
|
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
/// isCXXDeclarationStatement - C++-specialized function that disambiguates
|
|
|
|
/// between a declaration or an expression statement, when parsing function
|
|
|
|
/// bodies. Returns true for declaration, false for expression.
|
|
|
|
///
|
|
|
|
/// declaration-statement:
|
|
|
|
/// block-declaration
|
|
|
|
///
|
|
|
|
/// block-declaration:
|
|
|
|
/// simple-declaration
|
|
|
|
/// asm-definition
|
|
|
|
/// namespace-alias-definition
|
|
|
|
/// using-declaration
|
|
|
|
/// using-directive
|
|
|
|
/// [C++0x] static_assert-declaration
|
|
|
|
///
|
|
|
|
/// asm-definition:
|
|
|
|
/// 'asm' '(' string-literal ')' ';'
|
|
|
|
///
|
|
|
|
/// namespace-alias-definition:
|
|
|
|
/// 'namespace' identifier = qualified-namespace-specifier ';'
|
|
|
|
///
|
|
|
|
/// using-declaration:
|
|
|
|
/// 'using' typename[opt] '::'[opt] nested-name-specifier
|
|
|
|
/// unqualified-id ';'
|
|
|
|
/// 'using' '::' unqualified-id ;
|
|
|
|
///
|
|
|
|
/// using-directive:
|
|
|
|
/// 'using' 'namespace' '::'[opt] nested-name-specifier[opt]
|
|
|
|
/// namespace-name ';'
|
|
|
|
///
|
|
|
|
bool Parser::isCXXDeclarationStatement() {
|
|
|
|
switch (Tok.getKind()) {
|
|
|
|
// asm-definition
|
|
|
|
case tok::kw_asm:
|
|
|
|
// namespace-alias-definition
|
|
|
|
case tok::kw_namespace:
|
|
|
|
// using-declaration
|
|
|
|
// using-directive
|
|
|
|
case tok::kw_using:
|
|
|
|
// static_assert-declaration
|
|
|
|
case tok::kw_static_assert:
|
|
|
|
case tok::kw__Static_assert:
|
|
|
|
return true;
|
|
|
|
// simple-declaration
|
|
|
|
default:
|
|
|
|
return isCXXSimpleDeclaration(/*AllowForRangeDecl=*/false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// isCXXSimpleDeclaration - C++-specialized function that disambiguates
|
|
|
|
/// between a simple-declaration or an expression-statement.
|
|
|
|
/// If during the disambiguation process a parsing error is encountered,
|
|
|
|
/// the function returns true to let the declaration parsing code handle it.
|
|
|
|
/// Returns false if the statement is disambiguated as expression.
|
|
|
|
///
|
|
|
|
/// simple-declaration:
|
|
|
|
/// decl-specifier-seq init-declarator-list[opt] ';'
|
|
|
|
///
|
|
|
|
/// (if AllowForRangeDecl specified)
|
|
|
|
/// for ( for-range-declaration : for-range-initializer ) statement
|
|
|
|
/// for-range-declaration:
|
|
|
|
/// attribute-specifier-seqopt type-specifier-seq declarator
|
|
|
|
bool Parser::isCXXSimpleDeclaration(bool AllowForRangeDecl) {
|
|
|
|
// C++ 6.8p1:
|
|
|
|
// There is an ambiguity in the grammar involving expression-statements and
|
|
|
|
// declarations: An expression-statement with a function-style explicit type
|
|
|
|
// conversion (5.2.3) as its leftmost subexpression can be indistinguishable
|
|
|
|
// from a declaration where the first declarator starts with a '('. In those
|
|
|
|
// cases the statement is a declaration. [Note: To disambiguate, the whole
|
|
|
|
// statement might have to be examined to determine if it is an
|
|
|
|
// expression-statement or a declaration].
|
|
|
|
|
|
|
|
// C++ 6.8p3:
|
|
|
|
// The disambiguation is purely syntactic; that is, the meaning of the names
|
|
|
|
// occurring in such a statement, beyond whether they are type-names or not,
|
|
|
|
// is not generally used in or changed by the disambiguation. Class
|
|
|
|
// templates are instantiated as necessary to determine if a qualified name
|
|
|
|
// is a type-name. Disambiguation precedes parsing, and a statement
|
|
|
|
// disambiguated as a declaration may be an ill-formed declaration.
|
|
|
|
|
|
|
|
// We don't have to parse all of the decl-specifier-seq part. There's only
|
|
|
|
// an ambiguity if the first decl-specifier is
|
|
|
|
// simple-type-specifier/typename-specifier followed by a '(', which may
|
|
|
|
// indicate a function-style cast expression.
|
2014-05-16 09:56:53 +08:00
|
|
|
// isCXXDeclarationSpecifier will return TPResult::Ambiguous only in such
|
2012-12-18 22:30:41 +08:00
|
|
|
// a case.
|
|
|
|
|
|
|
|
bool InvalidAsDeclaration = false;
|
2014-05-16 09:56:53 +08:00
|
|
|
TPResult TPR = isCXXDeclarationSpecifier(TPResult::False,
|
2012-12-18 22:30:41 +08:00
|
|
|
&InvalidAsDeclaration);
|
2014-05-16 09:56:53 +08:00
|
|
|
if (TPR != TPResult::Ambiguous)
|
|
|
|
return TPR != TPResult::False; // Returns true for TPResult::True or
|
|
|
|
// TPResult::Error.
|
2012-12-18 22:30:41 +08:00
|
|
|
|
|
|
|
// FIXME: TryParseSimpleDeclaration doesn't look past the first initializer,
|
|
|
|
// and so gets some cases wrong. We can't carry on if we've already seen
|
|
|
|
// something which makes this statement invalid as a declaration in this case,
|
|
|
|
// since it can cause us to misparse valid code. Revisit this once
|
|
|
|
// TryParseInitDeclaratorList is fixed.
|
|
|
|
if (InvalidAsDeclaration)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// FIXME: Add statistics about the number of ambiguous statements encountered
|
|
|
|
// and how they were resolved (number of declarations+number of expressions).
|
|
|
|
|
|
|
|
// Ok, we have a simple-type-specifier/typename-specifier followed by a '(',
|
|
|
|
// or an identifier which doesn't resolve as anything. We need tentative
|
|
|
|
// parsing...
|
|
|
|
|
|
|
|
TentativeParsingAction PA(*this);
|
|
|
|
TPR = TryParseSimpleDeclaration(AllowForRangeDecl);
|
|
|
|
PA.Revert();
|
|
|
|
|
|
|
|
// In case of an error, let the declaration parsing code handle it.
|
2014-05-16 09:56:53 +08:00
|
|
|
if (TPR == TPResult::Error)
|
2012-12-18 22:30:41 +08:00
|
|
|
return true;
|
|
|
|
|
|
|
|
// Declarations take precedence over expressions.
|
2014-05-16 09:56:53 +08:00
|
|
|
if (TPR == TPResult::Ambiguous)
|
|
|
|
TPR = TPResult::True;
|
2012-12-18 22:30:41 +08:00
|
|
|
|
2014-05-16 09:56:53 +08:00
|
|
|
assert(TPR == TPResult::True || TPR == TPResult::False);
|
|
|
|
return TPR == TPResult::True;
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
|
2013-09-13 07:28:08 +08:00
|
|
|
/// Try to consume a token sequence that we've already identified as
|
|
|
|
/// (potentially) starting a decl-specifier.
|
|
|
|
Parser::TPResult Parser::TryConsumeDeclarationSpecifier() {
|
|
|
|
switch (Tok.getKind()) {
|
|
|
|
case tok::kw__Atomic:
|
|
|
|
if (NextToken().isNot(tok::l_paren)) {
|
|
|
|
ConsumeToken();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Fall through.
|
|
|
|
case tok::kw_typeof:
|
|
|
|
case tok::kw___attribute:
|
|
|
|
case tok::kw___underlying_type: {
|
|
|
|
ConsumeToken();
|
|
|
|
if (Tok.isNot(tok::l_paren))
|
2014-05-16 09:56:53 +08:00
|
|
|
return TPResult::Error;
|
2013-09-13 07:28:08 +08:00
|
|
|
ConsumeParen();
|
2013-11-18 16:17:37 +08:00
|
|
|
if (!SkipUntil(tok::r_paren))
|
2014-05-16 09:56:53 +08:00
|
|
|
return TPResult::Error;
|
2013-09-13 07:28:08 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case tok::kw_class:
|
|
|
|
case tok::kw_struct:
|
|
|
|
case tok::kw_union:
|
|
|
|
case tok::kw___interface:
|
|
|
|
case tok::kw_enum:
|
|
|
|
// elaborated-type-specifier:
|
|
|
|
// class-key attribute-specifier-seq[opt]
|
|
|
|
// nested-name-specifier[opt] identifier
|
|
|
|
// class-key nested-name-specifier[opt] template[opt] simple-template-id
|
|
|
|
// enum nested-name-specifier[opt] identifier
|
|
|
|
//
|
|
|
|
// FIXME: We don't support class-specifiers nor enum-specifiers here.
|
|
|
|
ConsumeToken();
|
|
|
|
|
|
|
|
// Skip attributes.
|
2015-06-18 18:59:26 +08:00
|
|
|
while (Tok.isOneOf(tok::l_square, tok::kw___attribute, tok::kw___declspec,
|
|
|
|
tok::kw_alignas)) {
|
2013-09-13 07:28:08 +08:00
|
|
|
if (Tok.is(tok::l_square)) {
|
|
|
|
ConsumeBracket();
|
2013-11-18 16:17:37 +08:00
|
|
|
if (!SkipUntil(tok::r_square))
|
2014-05-16 09:56:53 +08:00
|
|
|
return TPResult::Error;
|
2013-09-13 07:28:08 +08:00
|
|
|
} else {
|
|
|
|
ConsumeToken();
|
|
|
|
if (Tok.isNot(tok::l_paren))
|
2014-05-16 09:56:53 +08:00
|
|
|
return TPResult::Error;
|
2013-09-13 07:28:08 +08:00
|
|
|
ConsumeParen();
|
2013-11-18 16:17:37 +08:00
|
|
|
if (!SkipUntil(tok::r_paren))
|
2014-05-16 09:56:53 +08:00
|
|
|
return TPResult::Error;
|
2013-09-13 07:28:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-18 18:59:26 +08:00
|
|
|
if (Tok.isOneOf(tok::identifier, tok::coloncolon, tok::kw_decltype,
|
|
|
|
tok::annot_template_id) &&
|
2014-12-29 07:24:02 +08:00
|
|
|
TryAnnotateCXXScopeToken())
|
2014-05-16 09:56:53 +08:00
|
|
|
return TPResult::Error;
|
2013-09-13 07:28:08 +08:00
|
|
|
if (Tok.is(tok::annot_cxxscope))
|
|
|
|
ConsumeToken();
|
|
|
|
if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
|
2014-05-16 09:56:53 +08:00
|
|
|
return TPResult::Error;
|
2013-09-13 07:28:08 +08:00
|
|
|
ConsumeToken();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case tok::annot_cxxscope:
|
|
|
|
ConsumeToken();
|
|
|
|
// Fall through.
|
|
|
|
default:
|
|
|
|
ConsumeToken();
|
|
|
|
|
|
|
|
if (getLangOpts().ObjC1 && Tok.is(tok::less))
|
|
|
|
return TryParseProtocolQualifiers();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-05-16 09:56:53 +08:00
|
|
|
return TPResult::Ambiguous;
|
2013-09-13 07:28:08 +08:00
|
|
|
}
|
|
|
|
|
2012-12-18 22:30:41 +08:00
|
|
|
/// simple-declaration:
|
|
|
|
/// decl-specifier-seq init-declarator-list[opt] ';'
|
|
|
|
///
|
|
|
|
/// (if AllowForRangeDecl specified)
|
|
|
|
/// for ( for-range-declaration : for-range-initializer ) statement
|
|
|
|
/// for-range-declaration:
|
|
|
|
/// attribute-specifier-seqopt type-specifier-seq declarator
|
|
|
|
///
|
|
|
|
Parser::TPResult Parser::TryParseSimpleDeclaration(bool AllowForRangeDecl) {
|
2014-05-16 09:56:53 +08:00
|
|
|
if (TryConsumeDeclarationSpecifier() == TPResult::Error)
|
|
|
|
return TPResult::Error;
|
2012-12-18 22:30:41 +08:00
|
|
|
|
|
|
|
// Two decl-specifiers in a row conclusively disambiguate this as being a
|
|
|
|
// simple-declaration. Don't bother calling isCXXDeclarationSpecifier in the
|
|
|
|
// overwhelmingly common case that the next token is a '('.
|
|
|
|
if (Tok.isNot(tok::l_paren)) {
|
|
|
|
TPResult TPR = isCXXDeclarationSpecifier();
|
2014-05-16 09:56:53 +08:00
|
|
|
if (TPR == TPResult::Ambiguous)
|
|
|
|
return TPResult::True;
|
|
|
|
if (TPR == TPResult::True || TPR == TPResult::Error)
|
2012-12-18 22:30:41 +08:00
|
|
|
return TPR;
|
2014-05-16 09:56:53 +08:00
|
|
|
assert(TPR == TPResult::False);
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TPResult TPR = TryParseInitDeclaratorList();
|
2014-05-16 09:56:53 +08:00
|
|
|
if (TPR != TPResult::Ambiguous)
|
2012-12-18 22:30:41 +08:00
|
|
|
return TPR;
|
|
|
|
|
|
|
|
if (Tok.isNot(tok::semi) && (!AllowForRangeDecl || Tok.isNot(tok::colon)))
|
2014-05-16 09:56:53 +08:00
|
|
|
return TPResult::False;
|
2012-12-18 22:30:41 +08:00
|
|
|
|
2014-05-16 09:56:53 +08:00
|
|
|
return TPResult::Ambiguous;
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
|
2013-03-20 11:35:02 +08:00
|
|
|
/// Tentatively parse an init-declarator-list in order to disambiguate it from
|
|
|
|
/// an expression.
|
|
|
|
///
|
2012-12-18 22:30:41 +08:00
|
|
|
/// init-declarator-list:
|
|
|
|
/// init-declarator
|
|
|
|
/// init-declarator-list ',' init-declarator
|
|
|
|
///
|
|
|
|
/// init-declarator:
|
|
|
|
/// declarator initializer[opt]
|
|
|
|
/// [GNU] declarator simple-asm-expr[opt] attributes[opt] initializer[opt]
|
|
|
|
///
|
2013-03-20 11:35:02 +08:00
|
|
|
/// initializer:
|
|
|
|
/// brace-or-equal-initializer
|
|
|
|
/// '(' expression-list ')'
|
|
|
|
///
|
|
|
|
/// brace-or-equal-initializer:
|
|
|
|
/// '=' initializer-clause
|
|
|
|
/// [C++11] braced-init-list
|
2012-12-18 22:30:41 +08:00
|
|
|
///
|
2013-03-20 11:35:02 +08:00
|
|
|
/// initializer-clause:
|
|
|
|
/// assignment-expression
|
|
|
|
/// braced-init-list
|
|
|
|
///
|
|
|
|
/// braced-init-list:
|
|
|
|
/// '{' initializer-list ','[opt] '}'
|
|
|
|
/// '{' '}'
|
2012-12-18 22:30:41 +08:00
|
|
|
///
|
|
|
|
Parser::TPResult Parser::TryParseInitDeclaratorList() {
|
|
|
|
while (1) {
|
|
|
|
// declarator
|
2015-02-24 06:36:28 +08:00
|
|
|
TPResult TPR = TryParseDeclarator(false/*mayBeAbstract*/);
|
2014-05-16 09:56:53 +08:00
|
|
|
if (TPR != TPResult::Ambiguous)
|
2012-12-18 22:30:41 +08:00
|
|
|
return TPR;
|
|
|
|
|
|
|
|
// [GNU] simple-asm-expr[opt] attributes[opt]
|
2015-06-18 18:59:26 +08:00
|
|
|
if (Tok.isOneOf(tok::kw_asm, tok::kw___attribute))
|
2014-05-16 09:56:53 +08:00
|
|
|
return TPResult::True;
|
2012-12-18 22:30:41 +08:00
|
|
|
|
|
|
|
// initializer[opt]
|
|
|
|
if (Tok.is(tok::l_paren)) {
|
|
|
|
// Parse through the parens.
|
|
|
|
ConsumeParen();
|
2013-11-18 16:17:37 +08:00
|
|
|
if (!SkipUntil(tok::r_paren, StopAtSemi))
|
2014-05-16 09:56:53 +08:00
|
|
|
return TPResult::Error;
|
2013-03-20 11:35:02 +08:00
|
|
|
} else if (Tok.is(tok::l_brace)) {
|
|
|
|
// A left-brace here is sufficient to disambiguate the parse; an
|
|
|
|
// expression can never be followed directly by a braced-init-list.
|
2014-05-16 09:56:53 +08:00
|
|
|
return TPResult::True;
|
2012-12-18 22:30:41 +08:00
|
|
|
} else if (Tok.is(tok::equal) || isTokIdentifier_in()) {
|
2013-09-13 07:28:08 +08:00
|
|
|
// MSVC and g++ won't examine the rest of declarators if '=' is
|
2012-12-18 22:30:41 +08:00
|
|
|
// encountered; they just conclude that we have a declaration.
|
|
|
|
// EDG parses the initializer completely, which is the proper behavior
|
|
|
|
// for this case.
|
|
|
|
//
|
|
|
|
// At present, Clang follows MSVC and g++, since the parser does not have
|
|
|
|
// the ability to parse an expression fully without recording the
|
|
|
|
// results of that parse.
|
2013-09-13 07:28:08 +08:00
|
|
|
// FIXME: Handle this case correctly.
|
|
|
|
//
|
|
|
|
// Also allow 'in' after an Objective-C declaration as in:
|
|
|
|
// for (int (^b)(void) in array). Ideally this should be done in the
|
2012-12-18 22:30:41 +08:00
|
|
|
// context of parsing for-init-statement of a foreach statement only. But,
|
|
|
|
// in any other context 'in' is invalid after a declaration and parser
|
|
|
|
// issues the error regardless of outcome of this decision.
|
2013-09-13 07:28:08 +08:00
|
|
|
// FIXME: Change if above assumption does not hold.
|
2014-05-16 09:56:53 +08:00
|
|
|
return TPResult::True;
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
|
2014-01-10 19:19:30 +08:00
|
|
|
if (!TryConsumeToken(tok::comma))
|
2012-12-18 22:30:41 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-05-16 09:56:53 +08:00
|
|
|
return TPResult::Ambiguous;
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// isCXXConditionDeclaration - Disambiguates between a declaration or an
|
|
|
|
/// expression for a condition of a if/switch/while/for statement.
|
|
|
|
/// If during the disambiguation process a parsing error is encountered,
|
|
|
|
/// the function returns true to let the declaration parsing code handle it.
|
|
|
|
///
|
|
|
|
/// condition:
|
|
|
|
/// expression
|
|
|
|
/// type-specifier-seq declarator '=' assignment-expression
|
|
|
|
/// [C++11] type-specifier-seq declarator '=' initializer-clause
|
|
|
|
/// [C++11] type-specifier-seq declarator braced-init-list
|
|
|
|
/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
|
|
|
|
/// '=' assignment-expression
|
|
|
|
///
|
|
|
|
bool Parser::isCXXConditionDeclaration() {
|
|
|
|
TPResult TPR = isCXXDeclarationSpecifier();
|
2014-05-16 09:56:53 +08:00
|
|
|
if (TPR != TPResult::Ambiguous)
|
|
|
|
return TPR != TPResult::False; // Returns true for TPResult::True or
|
|
|
|
// TPResult::Error.
|
2012-12-18 22:30:41 +08:00
|
|
|
|
|
|
|
// FIXME: Add statistics about the number of ambiguous statements encountered
|
|
|
|
// and how they were resolved (number of declarations+number of expressions).
|
|
|
|
|
|
|
|
// Ok, we have a simple-type-specifier/typename-specifier followed by a '('.
|
|
|
|
// We need tentative parsing...
|
|
|
|
|
|
|
|
TentativeParsingAction PA(*this);
|
|
|
|
|
|
|
|
// type-specifier-seq
|
2013-09-13 07:28:08 +08:00
|
|
|
TryConsumeDeclarationSpecifier();
|
2012-12-18 22:30:41 +08:00
|
|
|
assert(Tok.is(tok::l_paren) && "Expected '('");
|
|
|
|
|
|
|
|
// declarator
|
2015-02-24 06:36:28 +08:00
|
|
|
TPR = TryParseDeclarator(false/*mayBeAbstract*/);
|
2012-12-18 22:30:41 +08:00
|
|
|
|
|
|
|
// In case of an error, let the declaration parsing code handle it.
|
2014-05-16 09:56:53 +08:00
|
|
|
if (TPR == TPResult::Error)
|
|
|
|
TPR = TPResult::True;
|
2012-12-18 22:30:41 +08:00
|
|
|
|
2014-05-16 09:56:53 +08:00
|
|
|
if (TPR == TPResult::Ambiguous) {
|
2012-12-18 22:30:41 +08:00
|
|
|
// '='
|
|
|
|
// [GNU] simple-asm-expr[opt] attributes[opt]
|
2015-06-18 18:59:26 +08:00
|
|
|
if (Tok.isOneOf(tok::equal, tok::kw_asm, tok::kw___attribute))
|
2014-05-16 09:56:53 +08:00
|
|
|
TPR = TPResult::True;
|
2013-01-02 19:42:31 +08:00
|
|
|
else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace))
|
2014-05-16 09:56:53 +08:00
|
|
|
TPR = TPResult::True;
|
2012-12-18 22:30:41 +08:00
|
|
|
else
|
2014-05-16 09:56:53 +08:00
|
|
|
TPR = TPResult::False;
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
PA.Revert();
|
|
|
|
|
2014-05-16 09:56:53 +08:00
|
|
|
assert(TPR == TPResult::True || TPR == TPResult::False);
|
|
|
|
return TPR == TPResult::True;
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Determine whether the next set of tokens contains a type-id.
|
|
|
|
///
|
|
|
|
/// The context parameter states what context we're parsing right
|
|
|
|
/// now, which affects how this routine copes with the token
|
|
|
|
/// following the type-id. If the context is TypeIdInParens, we have
|
|
|
|
/// already parsed the '(' and we will cease lookahead when we hit
|
|
|
|
/// the corresponding ')'. If the context is
|
|
|
|
/// TypeIdAsTemplateArgument, we've already parsed the '<' or ','
|
|
|
|
/// before this template argument, and will cease lookahead when we
|
|
|
|
/// hit a '>', '>>' (in C++0x), or ','. Returns true for a type-id
|
|
|
|
/// and false for an expression. If during the disambiguation
|
|
|
|
/// process a parsing error is encountered, the function returns
|
|
|
|
/// true to let the declaration parsing code handle it.
|
|
|
|
///
|
|
|
|
/// type-id:
|
|
|
|
/// type-specifier-seq abstract-declarator[opt]
|
|
|
|
///
|
|
|
|
bool Parser::isCXXTypeId(TentativeCXXTypeIdContext Context, bool &isAmbiguous) {
|
|
|
|
|
|
|
|
isAmbiguous = false;
|
|
|
|
|
|
|
|
// C++ 8.2p2:
|
|
|
|
// The ambiguity arising from the similarity between a function-style cast and
|
|
|
|
// a type-id can occur in different contexts. The ambiguity appears as a
|
|
|
|
// choice between a function-style cast expression and a declaration of a
|
|
|
|
// type. The resolution is that any construct that could possibly be a type-id
|
|
|
|
// in its syntactic context shall be considered a type-id.
|
|
|
|
|
|
|
|
TPResult TPR = isCXXDeclarationSpecifier();
|
2014-05-16 09:56:53 +08:00
|
|
|
if (TPR != TPResult::Ambiguous)
|
|
|
|
return TPR != TPResult::False; // Returns true for TPResult::True or
|
|
|
|
// TPResult::Error.
|
2012-12-18 22:30:41 +08:00
|
|
|
|
|
|
|
// FIXME: Add statistics about the number of ambiguous statements encountered
|
|
|
|
// and how they were resolved (number of declarations+number of expressions).
|
|
|
|
|
|
|
|
// Ok, we have a simple-type-specifier/typename-specifier followed by a '('.
|
|
|
|
// We need tentative parsing...
|
|
|
|
|
|
|
|
TentativeParsingAction PA(*this);
|
|
|
|
|
|
|
|
// type-specifier-seq
|
2013-09-13 07:28:08 +08:00
|
|
|
TryConsumeDeclarationSpecifier();
|
2012-12-18 22:30:41 +08:00
|
|
|
assert(Tok.is(tok::l_paren) && "Expected '('");
|
|
|
|
|
|
|
|
// declarator
|
2015-02-24 06:36:28 +08:00
|
|
|
TPR = TryParseDeclarator(true/*mayBeAbstract*/, false/*mayHaveIdentifier*/);
|
2012-12-18 22:30:41 +08:00
|
|
|
|
|
|
|
// In case of an error, let the declaration parsing code handle it.
|
2014-05-16 09:56:53 +08:00
|
|
|
if (TPR == TPResult::Error)
|
|
|
|
TPR = TPResult::True;
|
2012-12-18 22:30:41 +08:00
|
|
|
|
2014-05-16 09:56:53 +08:00
|
|
|
if (TPR == TPResult::Ambiguous) {
|
2012-12-18 22:30:41 +08:00
|
|
|
// We are supposed to be inside parens, so if after the abstract declarator
|
|
|
|
// we encounter a ')' this is a type-id, otherwise it's an expression.
|
|
|
|
if (Context == TypeIdInParens && Tok.is(tok::r_paren)) {
|
2014-05-16 09:56:53 +08:00
|
|
|
TPR = TPResult::True;
|
2012-12-18 22:30:41 +08:00
|
|
|
isAmbiguous = true;
|
|
|
|
|
|
|
|
// We are supposed to be inside a template argument, so if after
|
|
|
|
// the abstract declarator we encounter a '>', '>>' (in C++0x), or
|
|
|
|
// ',', this is a type-id. Otherwise, it's an expression.
|
|
|
|
} else if (Context == TypeIdAsTemplateArgument &&
|
2015-06-18 18:59:26 +08:00
|
|
|
(Tok.isOneOf(tok::greater, tok::comma) ||
|
2013-01-02 19:42:31 +08:00
|
|
|
(getLangOpts().CPlusPlus11 && Tok.is(tok::greatergreater)))) {
|
2014-05-16 09:56:53 +08:00
|
|
|
TPR = TPResult::True;
|
2012-12-18 22:30:41 +08:00
|
|
|
isAmbiguous = true;
|
|
|
|
|
|
|
|
} else
|
2014-05-16 09:56:53 +08:00
|
|
|
TPR = TPResult::False;
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
PA.Revert();
|
|
|
|
|
2014-05-16 09:56:53 +08:00
|
|
|
assert(TPR == TPResult::True || TPR == TPResult::False);
|
|
|
|
return TPR == TPResult::True;
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Returns true if this is a C++11 attribute-specifier. Per
|
|
|
|
/// C++11 [dcl.attr.grammar]p6, two consecutive left square bracket tokens
|
|
|
|
/// always introduce an attribute. In Objective-C++11, this rule does not
|
|
|
|
/// apply if either '[' begins a message-send.
|
|
|
|
///
|
|
|
|
/// If Disambiguate is true, we try harder to determine whether a '[[' starts
|
|
|
|
/// an attribute-specifier, and return CAK_InvalidAttributeSpecifier if not.
|
|
|
|
///
|
|
|
|
/// If OuterMightBeMessageSend is true, we assume the outer '[' is either an
|
|
|
|
/// Obj-C message send or the start of an attribute. Otherwise, we assume it
|
|
|
|
/// is not an Obj-C message send.
|
|
|
|
///
|
|
|
|
/// C++11 [dcl.attr.grammar]:
|
|
|
|
///
|
|
|
|
/// attribute-specifier:
|
|
|
|
/// '[' '[' attribute-list ']' ']'
|
|
|
|
/// alignment-specifier
|
|
|
|
///
|
|
|
|
/// attribute-list:
|
|
|
|
/// attribute[opt]
|
|
|
|
/// attribute-list ',' attribute[opt]
|
|
|
|
/// attribute '...'
|
|
|
|
/// attribute-list ',' attribute '...'
|
|
|
|
///
|
|
|
|
/// attribute:
|
|
|
|
/// attribute-token attribute-argument-clause[opt]
|
|
|
|
///
|
|
|
|
/// attribute-token:
|
|
|
|
/// identifier
|
|
|
|
/// identifier '::' identifier
|
|
|
|
///
|
|
|
|
/// attribute-argument-clause:
|
|
|
|
/// '(' balanced-token-seq ')'
|
|
|
|
Parser::CXX11AttributeKind
|
|
|
|
Parser::isCXX11AttributeSpecifier(bool Disambiguate,
|
|
|
|
bool OuterMightBeMessageSend) {
|
|
|
|
if (Tok.is(tok::kw_alignas))
|
|
|
|
return CAK_AttributeSpecifier;
|
|
|
|
|
|
|
|
if (Tok.isNot(tok::l_square) || NextToken().isNot(tok::l_square))
|
|
|
|
return CAK_NotAttributeSpecifier;
|
|
|
|
|
|
|
|
// No tentative parsing if we don't need to look for ']]' or a lambda.
|
|
|
|
if (!Disambiguate && !getLangOpts().ObjC1)
|
|
|
|
return CAK_AttributeSpecifier;
|
|
|
|
|
|
|
|
TentativeParsingAction PA(*this);
|
|
|
|
|
|
|
|
// Opening brackets were checked for above.
|
|
|
|
ConsumeBracket();
|
|
|
|
|
|
|
|
// Outside Obj-C++11, treat anything with a matching ']]' as an attribute.
|
|
|
|
if (!getLangOpts().ObjC1) {
|
|
|
|
ConsumeBracket();
|
|
|
|
|
2013-11-18 16:17:37 +08:00
|
|
|
bool IsAttribute = SkipUntil(tok::r_square);
|
2012-12-18 22:30:41 +08:00
|
|
|
IsAttribute &= Tok.is(tok::r_square);
|
|
|
|
|
|
|
|
PA.Revert();
|
|
|
|
|
|
|
|
return IsAttribute ? CAK_AttributeSpecifier : CAK_InvalidAttributeSpecifier;
|
|
|
|
}
|
|
|
|
|
|
|
|
// In Obj-C++11, we need to distinguish four situations:
|
|
|
|
// 1a) int x[[attr]]; C++11 attribute.
|
|
|
|
// 1b) [[attr]]; C++11 statement attribute.
|
|
|
|
// 2) int x[[obj](){ return 1; }()]; Lambda in array size/index.
|
|
|
|
// 3a) int x[[obj get]]; Message send in array size/index.
|
|
|
|
// 3b) [[Class alloc] init]; Message send in message send.
|
|
|
|
// 4) [[obj]{ return self; }() doStuff]; Lambda in message send.
|
|
|
|
// (1) is an attribute, (2) is ill-formed, and (3) and (4) are accepted.
|
|
|
|
|
|
|
|
// If we have a lambda-introducer, then this is definitely not a message send.
|
|
|
|
// FIXME: If this disambiguation is too slow, fold the tentative lambda parse
|
|
|
|
// into the tentative attribute parse below.
|
|
|
|
LambdaIntroducer Intro;
|
|
|
|
if (!TryParseLambdaIntroducer(Intro)) {
|
|
|
|
// A lambda cannot end with ']]', and an attribute must.
|
|
|
|
bool IsAttribute = Tok.is(tok::r_square);
|
|
|
|
|
|
|
|
PA.Revert();
|
|
|
|
|
|
|
|
if (IsAttribute)
|
|
|
|
// Case 1: C++11 attribute.
|
|
|
|
return CAK_AttributeSpecifier;
|
|
|
|
|
|
|
|
if (OuterMightBeMessageSend)
|
|
|
|
// Case 4: Lambda in message send.
|
|
|
|
return CAK_NotAttributeSpecifier;
|
|
|
|
|
|
|
|
// Case 2: Lambda in array size / index.
|
|
|
|
return CAK_InvalidAttributeSpecifier;
|
|
|
|
}
|
|
|
|
|
|
|
|
ConsumeBracket();
|
|
|
|
|
|
|
|
// If we don't have a lambda-introducer, then we have an attribute or a
|
|
|
|
// message-send.
|
|
|
|
bool IsAttribute = true;
|
|
|
|
while (Tok.isNot(tok::r_square)) {
|
|
|
|
if (Tok.is(tok::comma)) {
|
|
|
|
// Case 1: Stray commas can only occur in attributes.
|
|
|
|
PA.Revert();
|
|
|
|
return CAK_AttributeSpecifier;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse the attribute-token, if present.
|
|
|
|
// C++11 [dcl.attr.grammar]:
|
|
|
|
// If a keyword or an alternative token that satisfies the syntactic
|
|
|
|
// requirements of an identifier is contained in an attribute-token,
|
|
|
|
// it is considered an identifier.
|
|
|
|
SourceLocation Loc;
|
|
|
|
if (!TryParseCXX11AttributeIdentifier(Loc)) {
|
|
|
|
IsAttribute = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (Tok.is(tok::coloncolon)) {
|
|
|
|
ConsumeToken();
|
|
|
|
if (!TryParseCXX11AttributeIdentifier(Loc)) {
|
|
|
|
IsAttribute = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse the attribute-argument-clause, if present.
|
|
|
|
if (Tok.is(tok::l_paren)) {
|
|
|
|
ConsumeParen();
|
2013-11-18 16:17:37 +08:00
|
|
|
if (!SkipUntil(tok::r_paren)) {
|
2012-12-18 22:30:41 +08:00
|
|
|
IsAttribute = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-10 19:19:30 +08:00
|
|
|
TryConsumeToken(tok::ellipsis);
|
2012-12-18 22:30:41 +08:00
|
|
|
|
2014-01-10 19:19:30 +08:00
|
|
|
if (!TryConsumeToken(tok::comma))
|
2012-12-18 22:30:41 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// An attribute must end ']]'.
|
|
|
|
if (IsAttribute) {
|
|
|
|
if (Tok.is(tok::r_square)) {
|
|
|
|
ConsumeBracket();
|
|
|
|
IsAttribute = Tok.is(tok::r_square);
|
|
|
|
} else {
|
|
|
|
IsAttribute = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PA.Revert();
|
|
|
|
|
|
|
|
if (IsAttribute)
|
|
|
|
// Case 1: C++11 statement attribute.
|
|
|
|
return CAK_AttributeSpecifier;
|
|
|
|
|
|
|
|
// Case 3: Message send.
|
|
|
|
return CAK_NotAttributeSpecifier;
|
|
|
|
}
|
|
|
|
|
2013-09-13 07:28:08 +08:00
|
|
|
Parser::TPResult Parser::TryParsePtrOperatorSeq() {
|
|
|
|
while (true) {
|
2015-06-18 18:59:26 +08:00
|
|
|
if (Tok.isOneOf(tok::coloncolon, tok::identifier))
|
2013-09-13 07:28:08 +08:00
|
|
|
if (TryAnnotateCXXScopeToken(true))
|
2014-05-16 09:56:53 +08:00
|
|
|
return TPResult::Error;
|
2013-09-13 07:28:08 +08:00
|
|
|
|
2015-06-18 18:59:26 +08:00
|
|
|
if (Tok.isOneOf(tok::star, tok::amp, tok::caret, tok::ampamp) ||
|
2013-09-13 07:28:08 +08:00
|
|
|
(Tok.is(tok::annot_cxxscope) && NextToken().is(tok::star))) {
|
|
|
|
// ptr-operator
|
|
|
|
ConsumeToken();
|
Introduce type nullability specifiers for C/C++.
Introduces the type specifiers __nonnull, __nullable, and
__null_unspecified that describe the nullability of the pointer type
to which the specifier appertains. Nullability type specifiers improve
on the existing nonnull attributes in a few ways:
- They apply to types, so one can represent a pointer to a non-null
pointer, use them in function pointer types, etc.
- As type specifiers, they are syntactically more lightweight than
__attribute__s or [[attribute]]s.
- They can express both the notion of 'should never be null' and
also 'it makes sense for this to be null', and therefore can more
easily catch errors of omission where one forgot to annotate the
nullability of a particular pointer (this will come in a subsequent
patch).
Nullability type specifiers are maintained as type sugar, and
therefore have no effect on mangling, encoding, overloading,
etc. Nonetheless, they will be used for warnings about, e.g., passing
'null' to a method that does not accept it.
This is the C/C++ part of rdar://problem/18868820.
llvm-svn: 240146
2015-06-20 01:51:05 +08:00
|
|
|
while (Tok.isOneOf(tok::kw_const, tok::kw_volatile, tok::kw_restrict,
|
2015-06-25 06:02:08 +08:00
|
|
|
tok::kw__Nonnull, tok::kw__Nullable,
|
|
|
|
tok::kw__Null_unspecified))
|
2013-09-13 07:28:08 +08:00
|
|
|
ConsumeToken();
|
|
|
|
} else {
|
2015-02-24 06:36:28 +08:00
|
|
|
return TPResult::True;
|
2013-09-13 07:28:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// operator-function-id:
|
|
|
|
/// 'operator' operator
|
|
|
|
///
|
|
|
|
/// operator: one of
|
|
|
|
/// new delete new[] delete[] + - * / % ^ [...]
|
|
|
|
///
|
|
|
|
/// conversion-function-id:
|
|
|
|
/// 'operator' conversion-type-id
|
|
|
|
///
|
|
|
|
/// conversion-type-id:
|
|
|
|
/// type-specifier-seq conversion-declarator[opt]
|
|
|
|
///
|
|
|
|
/// conversion-declarator:
|
|
|
|
/// ptr-operator conversion-declarator[opt]
|
|
|
|
///
|
|
|
|
/// literal-operator-id:
|
|
|
|
/// 'operator' string-literal identifier
|
|
|
|
/// 'operator' user-defined-string-literal
|
|
|
|
Parser::TPResult Parser::TryParseOperatorId() {
|
|
|
|
assert(Tok.is(tok::kw_operator));
|
|
|
|
ConsumeToken();
|
|
|
|
|
|
|
|
// Maybe this is an operator-function-id.
|
|
|
|
switch (Tok.getKind()) {
|
|
|
|
case tok::kw_new: case tok::kw_delete:
|
|
|
|
ConsumeToken();
|
|
|
|
if (Tok.is(tok::l_square) && NextToken().is(tok::r_square)) {
|
|
|
|
ConsumeBracket();
|
|
|
|
ConsumeBracket();
|
|
|
|
}
|
2014-05-16 09:56:53 +08:00
|
|
|
return TPResult::True;
|
2013-09-13 07:28:08 +08:00
|
|
|
|
|
|
|
#define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemOnly) \
|
|
|
|
case tok::Token:
|
|
|
|
#define OVERLOADED_OPERATOR_MULTI(Name, Spelling, Unary, Binary, MemOnly)
|
|
|
|
#include "clang/Basic/OperatorKinds.def"
|
|
|
|
ConsumeToken();
|
2014-05-16 09:56:53 +08:00
|
|
|
return TPResult::True;
|
2013-09-13 07:28:08 +08:00
|
|
|
|
|
|
|
case tok::l_square:
|
|
|
|
if (NextToken().is(tok::r_square)) {
|
|
|
|
ConsumeBracket();
|
|
|
|
ConsumeBracket();
|
2014-05-16 09:56:53 +08:00
|
|
|
return TPResult::True;
|
2013-09-13 07:28:08 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case tok::l_paren:
|
|
|
|
if (NextToken().is(tok::r_paren)) {
|
|
|
|
ConsumeParen();
|
|
|
|
ConsumeParen();
|
2014-05-16 09:56:53 +08:00
|
|
|
return TPResult::True;
|
2013-09-13 07:28:08 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Maybe this is a literal-operator-id.
|
|
|
|
if (getLangOpts().CPlusPlus11 && isTokenStringLiteral()) {
|
|
|
|
bool FoundUDSuffix = false;
|
|
|
|
do {
|
|
|
|
FoundUDSuffix |= Tok.hasUDSuffix();
|
|
|
|
ConsumeStringToken();
|
|
|
|
} while (isTokenStringLiteral());
|
|
|
|
|
|
|
|
if (!FoundUDSuffix) {
|
|
|
|
if (Tok.is(tok::identifier))
|
|
|
|
ConsumeToken();
|
|
|
|
else
|
2014-05-16 09:56:53 +08:00
|
|
|
return TPResult::Error;
|
2013-09-13 07:28:08 +08:00
|
|
|
}
|
2014-05-16 09:56:53 +08:00
|
|
|
return TPResult::True;
|
2013-09-13 07:28:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Maybe this is a conversion-function-id.
|
|
|
|
bool AnyDeclSpecifiers = false;
|
|
|
|
while (true) {
|
|
|
|
TPResult TPR = isCXXDeclarationSpecifier();
|
2014-05-16 09:56:53 +08:00
|
|
|
if (TPR == TPResult::Error)
|
2013-09-13 07:28:08 +08:00
|
|
|
return TPR;
|
2014-05-16 09:56:53 +08:00
|
|
|
if (TPR == TPResult::False) {
|
2013-09-13 07:28:08 +08:00
|
|
|
if (!AnyDeclSpecifiers)
|
2014-05-16 09:56:53 +08:00
|
|
|
return TPResult::Error;
|
2013-09-13 07:28:08 +08:00
|
|
|
break;
|
|
|
|
}
|
2014-05-16 09:56:53 +08:00
|
|
|
if (TryConsumeDeclarationSpecifier() == TPResult::Error)
|
|
|
|
return TPResult::Error;
|
2013-09-13 07:28:08 +08:00
|
|
|
AnyDeclSpecifiers = true;
|
|
|
|
}
|
2015-02-24 06:36:28 +08:00
|
|
|
return TryParsePtrOperatorSeq();
|
2013-09-13 07:28:08 +08:00
|
|
|
}
|
|
|
|
|
2012-12-18 22:30:41 +08:00
|
|
|
/// declarator:
|
|
|
|
/// direct-declarator
|
|
|
|
/// ptr-operator declarator
|
|
|
|
///
|
|
|
|
/// direct-declarator:
|
|
|
|
/// declarator-id
|
|
|
|
/// direct-declarator '(' parameter-declaration-clause ')'
|
|
|
|
/// cv-qualifier-seq[opt] exception-specification[opt]
|
|
|
|
/// direct-declarator '[' constant-expression[opt] ']'
|
|
|
|
/// '(' declarator ')'
|
|
|
|
/// [GNU] '(' attributes declarator ')'
|
|
|
|
///
|
|
|
|
/// abstract-declarator:
|
|
|
|
/// ptr-operator abstract-declarator[opt]
|
|
|
|
/// direct-abstract-declarator
|
|
|
|
/// ...
|
|
|
|
///
|
|
|
|
/// direct-abstract-declarator:
|
|
|
|
/// direct-abstract-declarator[opt]
|
|
|
|
/// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
|
|
|
|
/// exception-specification[opt]
|
|
|
|
/// direct-abstract-declarator[opt] '[' constant-expression[opt] ']'
|
|
|
|
/// '(' abstract-declarator ')'
|
|
|
|
///
|
|
|
|
/// ptr-operator:
|
|
|
|
/// '*' cv-qualifier-seq[opt]
|
|
|
|
/// '&'
|
|
|
|
/// [C++0x] '&&' [TODO]
|
|
|
|
/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
|
|
|
|
///
|
|
|
|
/// cv-qualifier-seq:
|
|
|
|
/// cv-qualifier cv-qualifier-seq[opt]
|
|
|
|
///
|
|
|
|
/// cv-qualifier:
|
|
|
|
/// 'const'
|
|
|
|
/// 'volatile'
|
|
|
|
///
|
|
|
|
/// declarator-id:
|
|
|
|
/// '...'[opt] id-expression
|
|
|
|
///
|
|
|
|
/// id-expression:
|
|
|
|
/// unqualified-id
|
|
|
|
/// qualified-id [TODO]
|
|
|
|
///
|
|
|
|
/// unqualified-id:
|
|
|
|
/// identifier
|
2013-09-13 07:28:08 +08:00
|
|
|
/// operator-function-id
|
|
|
|
/// conversion-function-id
|
|
|
|
/// literal-operator-id
|
2012-12-18 22:30:41 +08:00
|
|
|
/// '~' class-name [TODO]
|
2013-09-13 07:28:08 +08:00
|
|
|
/// '~' decltype-specifier [TODO]
|
2012-12-18 22:30:41 +08:00
|
|
|
/// template-id [TODO]
|
|
|
|
///
|
2015-02-24 06:36:28 +08:00
|
|
|
Parser::TPResult Parser::TryParseDeclarator(bool mayBeAbstract,
|
|
|
|
bool mayHaveIdentifier) {
|
2012-12-18 22:30:41 +08:00
|
|
|
// declarator:
|
|
|
|
// direct-declarator
|
|
|
|
// ptr-operator declarator
|
2015-02-24 06:36:28 +08:00
|
|
|
if (TryParsePtrOperatorSeq() == TPResult::Error)
|
|
|
|
return TPResult::Error;
|
2012-12-18 22:30:41 +08:00
|
|
|
|
|
|
|
// direct-declarator:
|
|
|
|
// direct-abstract-declarator:
|
|
|
|
if (Tok.is(tok::ellipsis))
|
|
|
|
ConsumeToken();
|
2013-09-13 07:28:08 +08:00
|
|
|
|
2015-06-18 18:59:26 +08:00
|
|
|
if ((Tok.isOneOf(tok::identifier, tok::kw_operator) ||
|
2013-09-13 07:28:08 +08:00
|
|
|
(Tok.is(tok::annot_cxxscope) && (NextToken().is(tok::identifier) ||
|
|
|
|
NextToken().is(tok::kw_operator)))) &&
|
2015-02-24 06:36:28 +08:00
|
|
|
mayHaveIdentifier) {
|
2012-12-18 22:30:41 +08:00
|
|
|
// declarator-id
|
|
|
|
if (Tok.is(tok::annot_cxxscope))
|
|
|
|
ConsumeToken();
|
2013-09-13 07:28:08 +08:00
|
|
|
else if (Tok.is(tok::identifier))
|
2012-12-18 22:30:41 +08:00
|
|
|
TentativelyDeclaredIdentifiers.push_back(Tok.getIdentifierInfo());
|
2013-09-13 07:28:08 +08:00
|
|
|
if (Tok.is(tok::kw_operator)) {
|
2014-05-16 09:56:53 +08:00
|
|
|
if (TryParseOperatorId() == TPResult::Error)
|
|
|
|
return TPResult::Error;
|
2013-09-13 07:28:08 +08:00
|
|
|
} else
|
|
|
|
ConsumeToken();
|
2012-12-18 22:30:41 +08:00
|
|
|
} else if (Tok.is(tok::l_paren)) {
|
|
|
|
ConsumeParen();
|
2015-02-24 06:36:28 +08:00
|
|
|
if (mayBeAbstract &&
|
2012-12-18 22:30:41 +08:00
|
|
|
(Tok.is(tok::r_paren) || // 'int()' is a function.
|
|
|
|
// 'int(...)' is a function.
|
|
|
|
(Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren)) ||
|
|
|
|
isDeclarationSpecifier())) { // 'int(int)' is a function.
|
|
|
|
// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
|
|
|
|
// exception-specification[opt]
|
2015-02-24 06:36:28 +08:00
|
|
|
TPResult TPR = TryParseFunctionDeclarator();
|
2014-05-16 09:56:53 +08:00
|
|
|
if (TPR != TPResult::Ambiguous)
|
2012-12-18 22:30:41 +08:00
|
|
|
return TPR;
|
|
|
|
} else {
|
|
|
|
// '(' declarator ')'
|
|
|
|
// '(' attributes declarator ')'
|
|
|
|
// '(' abstract-declarator ')'
|
2015-06-18 18:59:26 +08:00
|
|
|
if (Tok.isOneOf(tok::kw___attribute, tok::kw___declspec, tok::kw___cdecl,
|
|
|
|
tok::kw___stdcall, tok::kw___fastcall, tok::kw___thiscall,
|
|
|
|
tok::kw___vectorcall, tok::kw___unaligned))
|
2014-05-16 09:56:53 +08:00
|
|
|
return TPResult::True; // attributes indicate declaration
|
2015-02-24 06:36:28 +08:00
|
|
|
TPResult TPR = TryParseDeclarator(mayBeAbstract, mayHaveIdentifier);
|
2014-05-16 09:56:53 +08:00
|
|
|
if (TPR != TPResult::Ambiguous)
|
2012-12-18 22:30:41 +08:00
|
|
|
return TPR;
|
|
|
|
if (Tok.isNot(tok::r_paren))
|
2014-05-16 09:56:53 +08:00
|
|
|
return TPResult::False;
|
2012-12-18 22:30:41 +08:00
|
|
|
ConsumeParen();
|
|
|
|
}
|
2015-02-24 06:36:28 +08:00
|
|
|
} else if (!mayBeAbstract) {
|
2014-05-16 09:56:53 +08:00
|
|
|
return TPResult::False;
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
while (1) {
|
2014-05-16 09:56:53 +08:00
|
|
|
TPResult TPR(TPResult::Ambiguous);
|
2012-12-18 22:30:41 +08:00
|
|
|
|
|
|
|
// abstract-declarator: ...
|
|
|
|
if (Tok.is(tok::ellipsis))
|
|
|
|
ConsumeToken();
|
|
|
|
|
|
|
|
if (Tok.is(tok::l_paren)) {
|
|
|
|
// Check whether we have a function declarator or a possible ctor-style
|
|
|
|
// initializer that follows the declarator. Note that ctor-style
|
|
|
|
// initializers are not possible in contexts where abstract declarators
|
|
|
|
// are allowed.
|
2015-02-24 06:36:28 +08:00
|
|
|
if (!mayBeAbstract && !isCXXFunctionDeclarator())
|
2012-12-18 22:30:41 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
// direct-declarator '(' parameter-declaration-clause ')'
|
|
|
|
// cv-qualifier-seq[opt] exception-specification[opt]
|
|
|
|
ConsumeParen();
|
2015-02-24 06:36:28 +08:00
|
|
|
TPR = TryParseFunctionDeclarator();
|
2012-12-18 22:30:41 +08:00
|
|
|
} else if (Tok.is(tok::l_square)) {
|
|
|
|
// direct-declarator '[' constant-expression[opt] ']'
|
|
|
|
// direct-abstract-declarator[opt] '[' constant-expression[opt] ']'
|
|
|
|
TPR = TryParseBracketDeclarator();
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-05-16 09:56:53 +08:00
|
|
|
if (TPR != TPResult::Ambiguous)
|
2012-12-18 22:30:41 +08:00
|
|
|
return TPR;
|
|
|
|
}
|
|
|
|
|
2014-05-16 09:56:53 +08:00
|
|
|
return TPResult::Ambiguous;
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Parser::TPResult
|
|
|
|
Parser::isExpressionOrTypeSpecifierSimple(tok::TokenKind Kind) {
|
|
|
|
switch (Kind) {
|
|
|
|
// Obviously starts an expression.
|
|
|
|
case tok::numeric_constant:
|
|
|
|
case tok::char_constant:
|
|
|
|
case tok::wide_char_constant:
|
2014-11-08 14:08:42 +08:00
|
|
|
case tok::utf8_char_constant:
|
2012-12-18 22:30:41 +08:00
|
|
|
case tok::utf16_char_constant:
|
|
|
|
case tok::utf32_char_constant:
|
|
|
|
case tok::string_literal:
|
|
|
|
case tok::wide_string_literal:
|
|
|
|
case tok::utf8_string_literal:
|
|
|
|
case tok::utf16_string_literal:
|
|
|
|
case tok::utf32_string_literal:
|
|
|
|
case tok::l_square:
|
|
|
|
case tok::l_paren:
|
|
|
|
case tok::amp:
|
|
|
|
case tok::ampamp:
|
|
|
|
case tok::star:
|
|
|
|
case tok::plus:
|
|
|
|
case tok::plusplus:
|
|
|
|
case tok::minus:
|
|
|
|
case tok::minusminus:
|
|
|
|
case tok::tilde:
|
|
|
|
case tok::exclaim:
|
|
|
|
case tok::kw_sizeof:
|
|
|
|
case tok::kw___func__:
|
|
|
|
case tok::kw_const_cast:
|
|
|
|
case tok::kw_delete:
|
|
|
|
case tok::kw_dynamic_cast:
|
|
|
|
case tok::kw_false:
|
|
|
|
case tok::kw_new:
|
|
|
|
case tok::kw_operator:
|
|
|
|
case tok::kw_reinterpret_cast:
|
|
|
|
case tok::kw_static_cast:
|
|
|
|
case tok::kw_this:
|
|
|
|
case tok::kw_throw:
|
|
|
|
case tok::kw_true:
|
|
|
|
case tok::kw_typeid:
|
|
|
|
case tok::kw_alignof:
|
|
|
|
case tok::kw_noexcept:
|
|
|
|
case tok::kw_nullptr:
|
|
|
|
case tok::kw__Alignof:
|
|
|
|
case tok::kw___null:
|
|
|
|
case tok::kw___alignof:
|
|
|
|
case tok::kw___builtin_choose_expr:
|
|
|
|
case tok::kw___builtin_offsetof:
|
|
|
|
case tok::kw___builtin_va_arg:
|
|
|
|
case tok::kw___imag:
|
|
|
|
case tok::kw___real:
|
|
|
|
case tok::kw___FUNCTION__:
|
2013-11-07 07:31:56 +08:00
|
|
|
case tok::kw___FUNCDNAME__:
|
2014-04-09 02:13:24 +08:00
|
|
|
case tok::kw___FUNCSIG__:
|
2012-12-18 22:30:41 +08:00
|
|
|
case tok::kw_L__FUNCTION__:
|
|
|
|
case tok::kw___PRETTY_FUNCTION__:
|
|
|
|
case tok::kw___uuidof:
|
2013-12-13 05:23:03 +08:00
|
|
|
#define TYPE_TRAIT(N,Spelling,K) \
|
|
|
|
case tok::kw_##Spelling:
|
|
|
|
#include "clang/Basic/TokenKinds.def"
|
2014-05-16 09:56:53 +08:00
|
|
|
return TPResult::True;
|
2012-12-18 22:30:41 +08:00
|
|
|
|
|
|
|
// Obviously starts a type-specifier-seq:
|
|
|
|
case tok::kw_char:
|
|
|
|
case tok::kw_const:
|
|
|
|
case tok::kw_double:
|
|
|
|
case tok::kw_enum:
|
|
|
|
case tok::kw_half:
|
|
|
|
case tok::kw_float:
|
|
|
|
case tok::kw_int:
|
|
|
|
case tok::kw_long:
|
|
|
|
case tok::kw___int64:
|
|
|
|
case tok::kw___int128:
|
|
|
|
case tok::kw_restrict:
|
|
|
|
case tok::kw_short:
|
|
|
|
case tok::kw_signed:
|
|
|
|
case tok::kw_struct:
|
|
|
|
case tok::kw_union:
|
|
|
|
case tok::kw_unsigned:
|
|
|
|
case tok::kw_void:
|
|
|
|
case tok::kw_volatile:
|
|
|
|
case tok::kw__Bool:
|
|
|
|
case tok::kw__Complex:
|
|
|
|
case tok::kw_class:
|
|
|
|
case tok::kw_typename:
|
|
|
|
case tok::kw_wchar_t:
|
|
|
|
case tok::kw_char16_t:
|
|
|
|
case tok::kw_char32_t:
|
|
|
|
case tok::kw__Decimal32:
|
|
|
|
case tok::kw__Decimal64:
|
|
|
|
case tok::kw__Decimal128:
|
2013-09-13 07:28:08 +08:00
|
|
|
case tok::kw___interface:
|
2012-12-18 22:30:41 +08:00
|
|
|
case tok::kw___thread:
|
2013-04-13 06:46:28 +08:00
|
|
|
case tok::kw_thread_local:
|
|
|
|
case tok::kw__Thread_local:
|
2012-12-18 22:30:41 +08:00
|
|
|
case tok::kw_typeof:
|
2013-09-13 07:28:08 +08:00
|
|
|
case tok::kw___underlying_type:
|
2012-12-18 22:30:41 +08:00
|
|
|
case tok::kw___cdecl:
|
|
|
|
case tok::kw___stdcall:
|
|
|
|
case tok::kw___fastcall:
|
|
|
|
case tok::kw___thiscall:
|
2014-10-25 01:42:17 +08:00
|
|
|
case tok::kw___vectorcall:
|
2012-12-18 22:30:41 +08:00
|
|
|
case tok::kw___unaligned:
|
|
|
|
case tok::kw___vector:
|
|
|
|
case tok::kw___pixel:
|
2015-01-13 03:35:51 +08:00
|
|
|
case tok::kw___bool:
|
2012-12-18 22:30:41 +08:00
|
|
|
case tok::kw__Atomic:
|
|
|
|
case tok::kw___unknown_anytype:
|
2014-05-16 09:56:53 +08:00
|
|
|
return TPResult::False;
|
2012-12-18 22:30:41 +08:00
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-05-16 09:56:53 +08:00
|
|
|
return TPResult::Ambiguous;
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Parser::isTentativelyDeclared(IdentifierInfo *II) {
|
|
|
|
return std::find(TentativelyDeclaredIdentifiers.begin(),
|
|
|
|
TentativelyDeclaredIdentifiers.end(), II)
|
|
|
|
!= TentativelyDeclaredIdentifiers.end();
|
|
|
|
}
|
|
|
|
|
2014-11-05 08:09:29 +08:00
|
|
|
namespace {
|
|
|
|
class TentativeParseCCC : public CorrectionCandidateCallback {
|
|
|
|
public:
|
|
|
|
TentativeParseCCC(const Token &Next) {
|
|
|
|
WantRemainingKeywords = false;
|
2015-06-18 18:59:26 +08:00
|
|
|
WantTypeSpecifiers = Next.isOneOf(tok::l_paren, tok::r_paren, tok::greater,
|
|
|
|
tok::l_brace, tok::identifier);
|
2014-11-05 08:09:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ValidateCandidate(const TypoCorrection &Candidate) override {
|
|
|
|
// Reject any candidate that only resolves to instance members since they
|
|
|
|
// aren't viable as standalone identifiers instead of member references.
|
|
|
|
if (Candidate.isResolved() && !Candidate.isKeyword() &&
|
|
|
|
std::all_of(Candidate.begin(), Candidate.end(),
|
|
|
|
[](NamedDecl *ND) { return ND->isCXXInstanceMember(); }))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return CorrectionCandidateCallback::ValidateCandidate(Candidate);
|
|
|
|
}
|
|
|
|
};
|
2015-06-23 07:07:51 +08:00
|
|
|
}
|
2014-05-16 09:56:53 +08:00
|
|
|
/// isCXXDeclarationSpecifier - Returns TPResult::True if it is a declaration
|
|
|
|
/// specifier, TPResult::False if it is not, TPResult::Ambiguous if it could
|
|
|
|
/// be either a decl-specifier or a function-style cast, and TPResult::Error
|
2012-12-18 22:30:41 +08:00
|
|
|
/// if a parsing error was found and reported.
|
|
|
|
///
|
|
|
|
/// If HasMissingTypename is provided, a name with a dependent scope specifier
|
|
|
|
/// will be treated as ambiguous if the 'typename' keyword is missing. If this
|
|
|
|
/// happens, *HasMissingTypename will be set to 'true'. This will also be used
|
|
|
|
/// as an indicator that undeclared identifiers (which will trigger a later
|
2014-05-16 09:56:53 +08:00
|
|
|
/// parse error) should be treated as types. Returns TPResult::Ambiguous in
|
2012-12-18 22:30:41 +08:00
|
|
|
/// such cases.
|
|
|
|
///
|
|
|
|
/// decl-specifier:
|
|
|
|
/// storage-class-specifier
|
|
|
|
/// type-specifier
|
|
|
|
/// function-specifier
|
|
|
|
/// 'friend'
|
|
|
|
/// 'typedef'
|
2013-04-13 06:46:28 +08:00
|
|
|
/// [C++11] 'constexpr'
|
2012-12-18 22:30:41 +08:00
|
|
|
/// [GNU] attributes declaration-specifiers[opt]
|
|
|
|
///
|
|
|
|
/// storage-class-specifier:
|
|
|
|
/// 'register'
|
|
|
|
/// 'static'
|
|
|
|
/// 'extern'
|
|
|
|
/// 'mutable'
|
|
|
|
/// 'auto'
|
|
|
|
/// [GNU] '__thread'
|
2013-04-13 06:46:28 +08:00
|
|
|
/// [C++11] 'thread_local'
|
|
|
|
/// [C11] '_Thread_local'
|
2012-12-18 22:30:41 +08:00
|
|
|
///
|
|
|
|
/// function-specifier:
|
|
|
|
/// 'inline'
|
|
|
|
/// 'virtual'
|
|
|
|
/// 'explicit'
|
|
|
|
///
|
|
|
|
/// typedef-name:
|
|
|
|
/// identifier
|
|
|
|
///
|
|
|
|
/// type-specifier:
|
|
|
|
/// simple-type-specifier
|
|
|
|
/// class-specifier
|
|
|
|
/// enum-specifier
|
|
|
|
/// elaborated-type-specifier
|
|
|
|
/// typename-specifier
|
|
|
|
/// cv-qualifier
|
|
|
|
///
|
|
|
|
/// simple-type-specifier:
|
|
|
|
/// '::'[opt] nested-name-specifier[opt] type-name
|
|
|
|
/// '::'[opt] nested-name-specifier 'template'
|
|
|
|
/// simple-template-id [TODO]
|
|
|
|
/// 'char'
|
|
|
|
/// 'wchar_t'
|
|
|
|
/// 'bool'
|
|
|
|
/// 'short'
|
|
|
|
/// 'int'
|
|
|
|
/// 'long'
|
|
|
|
/// 'signed'
|
|
|
|
/// 'unsigned'
|
|
|
|
/// 'float'
|
|
|
|
/// 'double'
|
|
|
|
/// 'void'
|
|
|
|
/// [GNU] typeof-specifier
|
|
|
|
/// [GNU] '_Complex'
|
2013-04-13 06:46:28 +08:00
|
|
|
/// [C++11] 'auto'
|
2015-11-11 10:02:15 +08:00
|
|
|
/// [GNU] '__auto_type'
|
2013-04-13 06:46:28 +08:00
|
|
|
/// [C++11] 'decltype' ( expression )
|
2013-04-27 00:15:35 +08:00
|
|
|
/// [C++1y] 'decltype' ( 'auto' )
|
2012-12-18 22:30:41 +08:00
|
|
|
///
|
|
|
|
/// type-name:
|
|
|
|
/// class-name
|
|
|
|
/// enum-name
|
|
|
|
/// typedef-name
|
|
|
|
///
|
|
|
|
/// elaborated-type-specifier:
|
|
|
|
/// class-key '::'[opt] nested-name-specifier[opt] identifier
|
|
|
|
/// class-key '::'[opt] nested-name-specifier[opt] 'template'[opt]
|
|
|
|
/// simple-template-id
|
|
|
|
/// 'enum' '::'[opt] nested-name-specifier[opt] identifier
|
|
|
|
///
|
|
|
|
/// enum-name:
|
|
|
|
/// identifier
|
|
|
|
///
|
|
|
|
/// enum-specifier:
|
|
|
|
/// 'enum' identifier[opt] '{' enumerator-list[opt] '}'
|
|
|
|
/// 'enum' identifier[opt] '{' enumerator-list ',' '}'
|
|
|
|
///
|
|
|
|
/// class-specifier:
|
|
|
|
/// class-head '{' member-specification[opt] '}'
|
|
|
|
///
|
|
|
|
/// class-head:
|
|
|
|
/// class-key identifier[opt] base-clause[opt]
|
|
|
|
/// class-key nested-name-specifier identifier base-clause[opt]
|
|
|
|
/// class-key nested-name-specifier[opt] simple-template-id
|
|
|
|
/// base-clause[opt]
|
|
|
|
///
|
|
|
|
/// class-key:
|
|
|
|
/// 'class'
|
|
|
|
/// 'struct'
|
|
|
|
/// 'union'
|
|
|
|
///
|
|
|
|
/// cv-qualifier:
|
|
|
|
/// 'const'
|
|
|
|
/// 'volatile'
|
|
|
|
/// [GNU] restrict
|
|
|
|
///
|
|
|
|
Parser::TPResult
|
|
|
|
Parser::isCXXDeclarationSpecifier(Parser::TPResult BracedCastResult,
|
|
|
|
bool *HasMissingTypename) {
|
|
|
|
switch (Tok.getKind()) {
|
|
|
|
case tok::identifier: {
|
|
|
|
// Check for need to substitute AltiVec __vector keyword
|
|
|
|
// for "vector" identifier.
|
|
|
|
if (TryAltiVecVectorToken())
|
2014-05-16 09:56:53 +08:00
|
|
|
return TPResult::True;
|
2012-12-18 22:30:41 +08:00
|
|
|
|
|
|
|
const Token &Next = NextToken();
|
|
|
|
// In 'foo bar', 'foo' is always a type name outside of Objective-C.
|
|
|
|
if (!getLangOpts().ObjC1 && Next.is(tok::identifier))
|
2014-05-16 09:56:53 +08:00
|
|
|
return TPResult::True;
|
2012-12-18 22:30:41 +08:00
|
|
|
|
|
|
|
if (Next.isNot(tok::coloncolon) && Next.isNot(tok::less)) {
|
|
|
|
// Determine whether this is a valid expression. If not, we will hit
|
|
|
|
// a parse error one way or another. In that case, tell the caller that
|
|
|
|
// this is ambiguous. Typo-correct to type and expression keywords and
|
|
|
|
// to types and identifiers, in order to try to recover from errors.
|
|
|
|
switch (TryAnnotateName(false /* no nested name specifier */,
|
2014-11-05 08:09:29 +08:00
|
|
|
llvm::make_unique<TentativeParseCCC>(Next))) {
|
2012-12-18 22:30:41 +08:00
|
|
|
case ANK_Error:
|
2014-05-16 09:56:53 +08:00
|
|
|
return TPResult::Error;
|
2012-12-18 22:30:41 +08:00
|
|
|
case ANK_TentativeDecl:
|
2014-05-16 09:56:53 +08:00
|
|
|
return TPResult::False;
|
2012-12-18 22:30:41 +08:00
|
|
|
case ANK_TemplateName:
|
|
|
|
// A bare type template-name which can't be a template template
|
|
|
|
// argument is an error, and was probably intended to be a type.
|
2014-05-16 09:56:53 +08:00
|
|
|
return GreaterThanIsOperator ? TPResult::True : TPResult::False;
|
2012-12-18 22:30:41 +08:00
|
|
|
case ANK_Unresolved:
|
2014-05-16 09:56:53 +08:00
|
|
|
return HasMissingTypename ? TPResult::Ambiguous : TPResult::False;
|
2012-12-18 22:30:41 +08:00
|
|
|
case ANK_Success:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
assert(Tok.isNot(tok::identifier) &&
|
|
|
|
"TryAnnotateName succeeded without producing an annotation");
|
|
|
|
} else {
|
|
|
|
// This might possibly be a type with a dependent scope specifier and
|
|
|
|
// a missing 'typename' keyword. Don't use TryAnnotateName in this case,
|
|
|
|
// since it will annotate as a primary expression, and we want to use the
|
|
|
|
// "missing 'typename'" logic.
|
|
|
|
if (TryAnnotateTypeOrScopeToken())
|
2014-05-16 09:56:53 +08:00
|
|
|
return TPResult::Error;
|
2012-12-18 22:30:41 +08:00
|
|
|
// If annotation failed, assume it's a non-type.
|
|
|
|
// FIXME: If this happens due to an undeclared identifier, treat it as
|
|
|
|
// ambiguous.
|
|
|
|
if (Tok.is(tok::identifier))
|
2014-05-16 09:56:53 +08:00
|
|
|
return TPResult::False;
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// We annotated this token as something. Recurse to handle whatever we got.
|
|
|
|
return isCXXDeclarationSpecifier(BracedCastResult, HasMissingTypename);
|
|
|
|
}
|
|
|
|
|
|
|
|
case tok::kw_typename: // typename T::type
|
|
|
|
// Annotate typenames and C++ scope specifiers. If we get one, just
|
|
|
|
// recurse to handle whatever we get.
|
|
|
|
if (TryAnnotateTypeOrScopeToken())
|
2014-05-16 09:56:53 +08:00
|
|
|
return TPResult::Error;
|
2012-12-18 22:30:41 +08:00
|
|
|
return isCXXDeclarationSpecifier(BracedCastResult, HasMissingTypename);
|
|
|
|
|
|
|
|
case tok::coloncolon: { // ::foo::bar
|
|
|
|
const Token &Next = NextToken();
|
2015-06-18 18:59:26 +08:00
|
|
|
if (Next.isOneOf(tok::kw_new, // ::new
|
|
|
|
tok::kw_delete)) // ::delete
|
2014-05-16 09:56:53 +08:00
|
|
|
return TPResult::False;
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
// Fall through.
|
2014-09-26 08:28:20 +08:00
|
|
|
case tok::kw___super:
|
2012-12-18 22:30:41 +08:00
|
|
|
case tok::kw_decltype:
|
|
|
|
// Annotate typenames and C++ scope specifiers. If we get one, just
|
|
|
|
// recurse to handle whatever we get.
|
|
|
|
if (TryAnnotateTypeOrScopeToken())
|
2014-05-16 09:56:53 +08:00
|
|
|
return TPResult::Error;
|
2012-12-18 22:30:41 +08:00
|
|
|
return isCXXDeclarationSpecifier(BracedCastResult, HasMissingTypename);
|
|
|
|
|
|
|
|
// decl-specifier:
|
|
|
|
// storage-class-specifier
|
|
|
|
// type-specifier
|
|
|
|
// function-specifier
|
|
|
|
// 'friend'
|
|
|
|
// 'typedef'
|
|
|
|
// 'constexpr'
|
2015-06-30 20:14:52 +08:00
|
|
|
// 'concept'
|
2012-12-18 22:30:41 +08:00
|
|
|
case tok::kw_friend:
|
|
|
|
case tok::kw_typedef:
|
|
|
|
case tok::kw_constexpr:
|
2015-06-30 20:14:52 +08:00
|
|
|
case tok::kw_concept:
|
2012-12-18 22:30:41 +08:00
|
|
|
// storage-class-specifier
|
|
|
|
case tok::kw_register:
|
|
|
|
case tok::kw_static:
|
|
|
|
case tok::kw_extern:
|
|
|
|
case tok::kw_mutable:
|
|
|
|
case tok::kw_auto:
|
|
|
|
case tok::kw___thread:
|
2013-04-13 06:46:28 +08:00
|
|
|
case tok::kw_thread_local:
|
|
|
|
case tok::kw__Thread_local:
|
2012-12-18 22:30:41 +08:00
|
|
|
// function-specifier
|
|
|
|
case tok::kw_inline:
|
|
|
|
case tok::kw_virtual:
|
|
|
|
case tok::kw_explicit:
|
|
|
|
|
|
|
|
// Modules
|
|
|
|
case tok::kw___module_private__:
|
|
|
|
|
|
|
|
// Debugger support
|
|
|
|
case tok::kw___unknown_anytype:
|
|
|
|
|
|
|
|
// type-specifier:
|
|
|
|
// simple-type-specifier
|
|
|
|
// class-specifier
|
|
|
|
// enum-specifier
|
|
|
|
// elaborated-type-specifier
|
|
|
|
// typename-specifier
|
|
|
|
// cv-qualifier
|
|
|
|
|
|
|
|
// class-specifier
|
|
|
|
// elaborated-type-specifier
|
|
|
|
case tok::kw_class:
|
|
|
|
case tok::kw_struct:
|
|
|
|
case tok::kw_union:
|
2013-09-13 07:28:08 +08:00
|
|
|
case tok::kw___interface:
|
2012-12-18 22:30:41 +08:00
|
|
|
// enum-specifier
|
|
|
|
case tok::kw_enum:
|
|
|
|
// cv-qualifier
|
|
|
|
case tok::kw_const:
|
|
|
|
case tok::kw_volatile:
|
|
|
|
|
|
|
|
// GNU
|
|
|
|
case tok::kw_restrict:
|
|
|
|
case tok::kw__Complex:
|
|
|
|
case tok::kw___attribute:
|
2015-11-11 10:02:15 +08:00
|
|
|
case tok::kw___auto_type:
|
2014-05-16 09:56:53 +08:00
|
|
|
return TPResult::True;
|
2012-12-18 22:30:41 +08:00
|
|
|
|
|
|
|
// Microsoft
|
|
|
|
case tok::kw___declspec:
|
|
|
|
case tok::kw___cdecl:
|
|
|
|
case tok::kw___stdcall:
|
|
|
|
case tok::kw___fastcall:
|
|
|
|
case tok::kw___thiscall:
|
2014-10-25 01:42:17 +08:00
|
|
|
case tok::kw___vectorcall:
|
2012-12-18 22:30:41 +08:00
|
|
|
case tok::kw___w64:
|
2013-05-23 07:25:32 +08:00
|
|
|
case tok::kw___sptr:
|
|
|
|
case tok::kw___uptr:
|
2012-12-18 22:30:41 +08:00
|
|
|
case tok::kw___ptr64:
|
|
|
|
case tok::kw___ptr32:
|
|
|
|
case tok::kw___forceinline:
|
|
|
|
case tok::kw___unaligned:
|
2015-06-25 06:02:08 +08:00
|
|
|
case tok::kw__Nonnull:
|
|
|
|
case tok::kw__Nullable:
|
|
|
|
case tok::kw__Null_unspecified:
|
2015-07-07 11:58:42 +08:00
|
|
|
case tok::kw___kindof:
|
2014-05-16 09:56:53 +08:00
|
|
|
return TPResult::True;
|
2012-12-18 22:30:41 +08:00
|
|
|
|
|
|
|
// Borland
|
|
|
|
case tok::kw___pascal:
|
2014-05-16 09:56:53 +08:00
|
|
|
return TPResult::True;
|
2012-12-18 22:30:41 +08:00
|
|
|
|
|
|
|
// AltiVec
|
|
|
|
case tok::kw___vector:
|
2014-05-16 09:56:53 +08:00
|
|
|
return TPResult::True;
|
2012-12-18 22:30:41 +08:00
|
|
|
|
|
|
|
case tok::annot_template_id: {
|
|
|
|
TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
|
|
|
|
if (TemplateId->Kind != TNK_Type_template)
|
2014-05-16 09:56:53 +08:00
|
|
|
return TPResult::False;
|
2012-12-18 22:30:41 +08:00
|
|
|
CXXScopeSpec SS;
|
|
|
|
AnnotateTemplateIdTokenAsType();
|
|
|
|
assert(Tok.is(tok::annot_typename));
|
|
|
|
goto case_typename;
|
|
|
|
}
|
|
|
|
|
|
|
|
case tok::annot_cxxscope: // foo::bar or ::foo::bar, but already parsed
|
|
|
|
// We've already annotated a scope; try to annotate a type.
|
|
|
|
if (TryAnnotateTypeOrScopeToken())
|
2014-05-16 09:56:53 +08:00
|
|
|
return TPResult::Error;
|
2012-12-18 22:30:41 +08:00
|
|
|
if (!Tok.is(tok::annot_typename)) {
|
|
|
|
// If the next token is an identifier or a type qualifier, then this
|
|
|
|
// can't possibly be a valid expression either.
|
|
|
|
if (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier)) {
|
|
|
|
CXXScopeSpec SS;
|
|
|
|
Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
|
|
|
|
Tok.getAnnotationRange(),
|
|
|
|
SS);
|
|
|
|
if (SS.getScopeRep() && SS.getScopeRep()->isDependent()) {
|
|
|
|
TentativeParsingAction PA(*this);
|
|
|
|
ConsumeToken();
|
|
|
|
ConsumeToken();
|
|
|
|
bool isIdentifier = Tok.is(tok::identifier);
|
2014-05-16 09:56:53 +08:00
|
|
|
TPResult TPR = TPResult::False;
|
2012-12-18 22:30:41 +08:00
|
|
|
if (!isIdentifier)
|
|
|
|
TPR = isCXXDeclarationSpecifier(BracedCastResult,
|
|
|
|
HasMissingTypename);
|
|
|
|
PA.Revert();
|
|
|
|
|
|
|
|
if (isIdentifier ||
|
2014-05-16 09:56:53 +08:00
|
|
|
TPR == TPResult::True || TPR == TPResult::Error)
|
|
|
|
return TPResult::Error;
|
2012-12-18 22:30:41 +08:00
|
|
|
|
|
|
|
if (HasMissingTypename) {
|
|
|
|
// We can't tell whether this is a missing 'typename' or a valid
|
|
|
|
// expression.
|
|
|
|
*HasMissingTypename = true;
|
2014-05-16 09:56:53 +08:00
|
|
|
return TPResult::Ambiguous;
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Try to resolve the name. If it doesn't exist, assume it was
|
|
|
|
// intended to name a type and keep disambiguating.
|
|
|
|
switch (TryAnnotateName(false /* SS is not dependent */)) {
|
|
|
|
case ANK_Error:
|
2014-05-16 09:56:53 +08:00
|
|
|
return TPResult::Error;
|
2012-12-18 22:30:41 +08:00
|
|
|
case ANK_TentativeDecl:
|
2014-05-16 09:56:53 +08:00
|
|
|
return TPResult::False;
|
2012-12-18 22:30:41 +08:00
|
|
|
case ANK_TemplateName:
|
|
|
|
// A bare type template-name which can't be a template template
|
|
|
|
// argument is an error, and was probably intended to be a type.
|
2014-05-16 09:56:53 +08:00
|
|
|
return GreaterThanIsOperator ? TPResult::True : TPResult::False;
|
2012-12-18 22:30:41 +08:00
|
|
|
case ANK_Unresolved:
|
2014-05-16 09:56:53 +08:00
|
|
|
return HasMissingTypename ? TPResult::Ambiguous
|
|
|
|
: TPResult::False;
|
2012-12-18 22:30:41 +08:00
|
|
|
case ANK_Success:
|
|
|
|
// Annotated it, check again.
|
|
|
|
assert(Tok.isNot(tok::annot_cxxscope) ||
|
|
|
|
NextToken().isNot(tok::identifier));
|
|
|
|
return isCXXDeclarationSpecifier(BracedCastResult,
|
|
|
|
HasMissingTypename);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-05-16 09:56:53 +08:00
|
|
|
return TPResult::False;
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
// If that succeeded, fallthrough into the generic simple-type-id case.
|
|
|
|
|
|
|
|
// The ambiguity resides in a simple-type-specifier/typename-specifier
|
|
|
|
// followed by a '('. The '(' could either be the start of:
|
|
|
|
//
|
|
|
|
// direct-declarator:
|
|
|
|
// '(' declarator ')'
|
|
|
|
//
|
|
|
|
// direct-abstract-declarator:
|
|
|
|
// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
|
|
|
|
// exception-specification[opt]
|
|
|
|
// '(' abstract-declarator ')'
|
|
|
|
//
|
|
|
|
// or part of a function-style cast expression:
|
|
|
|
//
|
|
|
|
// simple-type-specifier '(' expression-list[opt] ')'
|
|
|
|
//
|
|
|
|
|
|
|
|
// simple-type-specifier:
|
|
|
|
|
|
|
|
case tok::annot_typename:
|
|
|
|
case_typename:
|
|
|
|
// In Objective-C, we might have a protocol-qualified type.
|
|
|
|
if (getLangOpts().ObjC1 && NextToken().is(tok::less)) {
|
2015-07-07 11:57:35 +08:00
|
|
|
// Tentatively parse the protocol qualifiers.
|
2012-12-18 22:30:41 +08:00
|
|
|
TentativeParsingAction PA(*this);
|
|
|
|
ConsumeToken(); // The type token
|
|
|
|
|
|
|
|
TPResult TPR = TryParseProtocolQualifiers();
|
|
|
|
bool isFollowedByParen = Tok.is(tok::l_paren);
|
|
|
|
bool isFollowedByBrace = Tok.is(tok::l_brace);
|
|
|
|
|
|
|
|
PA.Revert();
|
|
|
|
|
2014-05-16 09:56:53 +08:00
|
|
|
if (TPR == TPResult::Error)
|
|
|
|
return TPResult::Error;
|
2012-12-18 22:30:41 +08:00
|
|
|
|
|
|
|
if (isFollowedByParen)
|
2014-05-16 09:56:53 +08:00
|
|
|
return TPResult::Ambiguous;
|
2012-12-18 22:30:41 +08:00
|
|
|
|
2013-01-02 19:42:31 +08:00
|
|
|
if (getLangOpts().CPlusPlus11 && isFollowedByBrace)
|
2012-12-18 22:30:41 +08:00
|
|
|
return BracedCastResult;
|
|
|
|
|
2014-05-16 09:56:53 +08:00
|
|
|
return TPResult::True;
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
case tok::kw_char:
|
|
|
|
case tok::kw_wchar_t:
|
|
|
|
case tok::kw_char16_t:
|
|
|
|
case tok::kw_char32_t:
|
|
|
|
case tok::kw_bool:
|
|
|
|
case tok::kw_short:
|
|
|
|
case tok::kw_int:
|
|
|
|
case tok::kw_long:
|
|
|
|
case tok::kw___int64:
|
|
|
|
case tok::kw___int128:
|
|
|
|
case tok::kw_signed:
|
|
|
|
case tok::kw_unsigned:
|
|
|
|
case tok::kw_half:
|
|
|
|
case tok::kw_float:
|
|
|
|
case tok::kw_double:
|
|
|
|
case tok::kw_void:
|
|
|
|
case tok::annot_decltype:
|
|
|
|
if (NextToken().is(tok::l_paren))
|
2014-05-16 09:56:53 +08:00
|
|
|
return TPResult::Ambiguous;
|
2012-12-18 22:30:41 +08:00
|
|
|
|
|
|
|
// This is a function-style cast in all cases we disambiguate other than
|
|
|
|
// one:
|
|
|
|
// struct S {
|
|
|
|
// enum E : int { a = 4 }; // enum
|
|
|
|
// enum E : int { 4 }; // bit-field
|
|
|
|
// };
|
2013-01-02 19:42:31 +08:00
|
|
|
if (getLangOpts().CPlusPlus11 && NextToken().is(tok::l_brace))
|
2012-12-18 22:30:41 +08:00
|
|
|
return BracedCastResult;
|
|
|
|
|
|
|
|
if (isStartOfObjCClassMessageMissingOpenBracket())
|
2014-05-16 09:56:53 +08:00
|
|
|
return TPResult::False;
|
2012-12-18 22:30:41 +08:00
|
|
|
|
2014-05-16 09:56:53 +08:00
|
|
|
return TPResult::True;
|
2012-12-18 22:30:41 +08:00
|
|
|
|
|
|
|
// GNU typeof support.
|
|
|
|
case tok::kw_typeof: {
|
|
|
|
if (NextToken().isNot(tok::l_paren))
|
2014-05-16 09:56:53 +08:00
|
|
|
return TPResult::True;
|
2012-12-18 22:30:41 +08:00
|
|
|
|
|
|
|
TentativeParsingAction PA(*this);
|
|
|
|
|
|
|
|
TPResult TPR = TryParseTypeofSpecifier();
|
|
|
|
bool isFollowedByParen = Tok.is(tok::l_paren);
|
|
|
|
bool isFollowedByBrace = Tok.is(tok::l_brace);
|
|
|
|
|
|
|
|
PA.Revert();
|
|
|
|
|
2014-05-16 09:56:53 +08:00
|
|
|
if (TPR == TPResult::Error)
|
|
|
|
return TPResult::Error;
|
2012-12-18 22:30:41 +08:00
|
|
|
|
|
|
|
if (isFollowedByParen)
|
2014-05-16 09:56:53 +08:00
|
|
|
return TPResult::Ambiguous;
|
2012-12-18 22:30:41 +08:00
|
|
|
|
2013-01-02 19:42:31 +08:00
|
|
|
if (getLangOpts().CPlusPlus11 && isFollowedByBrace)
|
2012-12-18 22:30:41 +08:00
|
|
|
return BracedCastResult;
|
|
|
|
|
2014-05-16 09:56:53 +08:00
|
|
|
return TPResult::True;
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// C++0x type traits support
|
|
|
|
case tok::kw___underlying_type:
|
2014-05-16 09:56:53 +08:00
|
|
|
return TPResult::True;
|
2012-12-18 22:30:41 +08:00
|
|
|
|
|
|
|
// C11 _Atomic
|
|
|
|
case tok::kw__Atomic:
|
2014-05-16 09:56:53 +08:00
|
|
|
return TPResult::True;
|
2012-12-18 22:30:41 +08:00
|
|
|
|
|
|
|
default:
|
2014-05-16 09:56:53 +08:00
|
|
|
return TPResult::False;
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-13 07:28:08 +08:00
|
|
|
bool Parser::isCXXDeclarationSpecifierAType() {
|
|
|
|
switch (Tok.getKind()) {
|
|
|
|
// typename-specifier
|
|
|
|
case tok::annot_decltype:
|
|
|
|
case tok::annot_template_id:
|
|
|
|
case tok::annot_typename:
|
|
|
|
case tok::kw_typeof:
|
|
|
|
case tok::kw___underlying_type:
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// elaborated-type-specifier
|
|
|
|
case tok::kw_class:
|
|
|
|
case tok::kw_struct:
|
|
|
|
case tok::kw_union:
|
|
|
|
case tok::kw___interface:
|
|
|
|
case tok::kw_enum:
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// simple-type-specifier
|
|
|
|
case tok::kw_char:
|
|
|
|
case tok::kw_wchar_t:
|
|
|
|
case tok::kw_char16_t:
|
|
|
|
case tok::kw_char32_t:
|
|
|
|
case tok::kw_bool:
|
|
|
|
case tok::kw_short:
|
|
|
|
case tok::kw_int:
|
|
|
|
case tok::kw_long:
|
|
|
|
case tok::kw___int64:
|
|
|
|
case tok::kw___int128:
|
|
|
|
case tok::kw_signed:
|
|
|
|
case tok::kw_unsigned:
|
|
|
|
case tok::kw_half:
|
|
|
|
case tok::kw_float:
|
|
|
|
case tok::kw_double:
|
|
|
|
case tok::kw_void:
|
|
|
|
case tok::kw___unknown_anytype:
|
2015-11-11 10:02:15 +08:00
|
|
|
case tok::kw___auto_type:
|
2013-09-13 07:28:08 +08:00
|
|
|
return true;
|
|
|
|
|
|
|
|
case tok::kw_auto:
|
|
|
|
return getLangOpts().CPlusPlus11;
|
|
|
|
|
|
|
|
case tok::kw__Atomic:
|
|
|
|
// "_Atomic foo"
|
|
|
|
return NextToken().is(tok::l_paren);
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-18 22:30:41 +08:00
|
|
|
/// [GNU] typeof-specifier:
|
|
|
|
/// 'typeof' '(' expressions ')'
|
|
|
|
/// 'typeof' '(' type-name ')'
|
|
|
|
///
|
|
|
|
Parser::TPResult Parser::TryParseTypeofSpecifier() {
|
|
|
|
assert(Tok.is(tok::kw_typeof) && "Expected 'typeof'!");
|
|
|
|
ConsumeToken();
|
|
|
|
|
|
|
|
assert(Tok.is(tok::l_paren) && "Expected '('");
|
|
|
|
// Parse through the parens after 'typeof'.
|
|
|
|
ConsumeParen();
|
2013-11-18 16:17:37 +08:00
|
|
|
if (!SkipUntil(tok::r_paren, StopAtSemi))
|
2014-05-16 09:56:53 +08:00
|
|
|
return TPResult::Error;
|
2012-12-18 22:30:41 +08:00
|
|
|
|
2014-05-16 09:56:53 +08:00
|
|
|
return TPResult::Ambiguous;
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// [ObjC] protocol-qualifiers:
|
|
|
|
//// '<' identifier-list '>'
|
|
|
|
Parser::TPResult Parser::TryParseProtocolQualifiers() {
|
|
|
|
assert(Tok.is(tok::less) && "Expected '<' for qualifier list");
|
|
|
|
ConsumeToken();
|
|
|
|
do {
|
|
|
|
if (Tok.isNot(tok::identifier))
|
2014-05-16 09:56:53 +08:00
|
|
|
return TPResult::Error;
|
2012-12-18 22:30:41 +08:00
|
|
|
ConsumeToken();
|
|
|
|
|
|
|
|
if (Tok.is(tok::comma)) {
|
|
|
|
ConsumeToken();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Tok.is(tok::greater)) {
|
|
|
|
ConsumeToken();
|
2014-05-16 09:56:53 +08:00
|
|
|
return TPResult::Ambiguous;
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
} while (false);
|
|
|
|
|
2014-05-16 09:56:53 +08:00
|
|
|
return TPResult::Error;
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// isCXXFunctionDeclarator - Disambiguates between a function declarator or
|
|
|
|
/// a constructor-style initializer, when parsing declaration statements.
|
|
|
|
/// Returns true for function declarator and false for constructor-style
|
|
|
|
/// initializer.
|
|
|
|
/// If during the disambiguation process a parsing error is encountered,
|
|
|
|
/// the function returns true to let the declaration parsing code handle it.
|
|
|
|
///
|
|
|
|
/// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
|
|
|
|
/// exception-specification[opt]
|
|
|
|
///
|
|
|
|
bool Parser::isCXXFunctionDeclarator(bool *IsAmbiguous) {
|
|
|
|
|
|
|
|
// C++ 8.2p1:
|
|
|
|
// The ambiguity arising from the similarity between a function-style cast and
|
|
|
|
// a declaration mentioned in 6.8 can also occur in the context of a
|
|
|
|
// declaration. In that context, the choice is between a function declaration
|
|
|
|
// with a redundant set of parentheses around a parameter name and an object
|
|
|
|
// declaration with a function-style cast as the initializer. Just as for the
|
|
|
|
// ambiguities mentioned in 6.8, the resolution is to consider any construct
|
|
|
|
// that could possibly be a declaration a declaration.
|
|
|
|
|
|
|
|
TentativeParsingAction PA(*this);
|
|
|
|
|
|
|
|
ConsumeParen();
|
|
|
|
bool InvalidAsDeclaration = false;
|
|
|
|
TPResult TPR = TryParseParameterDeclarationClause(&InvalidAsDeclaration);
|
2014-05-16 09:56:53 +08:00
|
|
|
if (TPR == TPResult::Ambiguous) {
|
2012-12-18 22:30:41 +08:00
|
|
|
if (Tok.isNot(tok::r_paren))
|
2014-05-16 09:56:53 +08:00
|
|
|
TPR = TPResult::False;
|
2012-12-18 22:30:41 +08:00
|
|
|
else {
|
|
|
|
const Token &Next = NextToken();
|
2015-06-18 18:59:26 +08:00
|
|
|
if (Next.isOneOf(tok::amp, tok::ampamp, tok::kw_const, tok::kw_volatile,
|
|
|
|
tok::kw_throw, tok::kw_noexcept, tok::l_square,
|
|
|
|
tok::l_brace, tok::kw_try, tok::equal, tok::arrow) ||
|
|
|
|
isCXX11VirtSpecifier(Next))
|
2012-12-18 22:30:41 +08:00
|
|
|
// The next token cannot appear after a constructor-style initializer,
|
|
|
|
// and can appear next in a function definition. This must be a function
|
|
|
|
// declarator.
|
2014-05-16 09:56:53 +08:00
|
|
|
TPR = TPResult::True;
|
2012-12-18 22:30:41 +08:00
|
|
|
else if (InvalidAsDeclaration)
|
|
|
|
// Use the absence of 'typename' as a tie-breaker.
|
2014-05-16 09:56:53 +08:00
|
|
|
TPR = TPResult::False;
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PA.Revert();
|
|
|
|
|
2014-05-16 09:56:53 +08:00
|
|
|
if (IsAmbiguous && TPR == TPResult::Ambiguous)
|
2012-12-18 22:30:41 +08:00
|
|
|
*IsAmbiguous = true;
|
|
|
|
|
|
|
|
// In case of an error, let the declaration parsing code handle it.
|
2014-05-16 09:56:53 +08:00
|
|
|
return TPR != TPResult::False;
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// parameter-declaration-clause:
|
|
|
|
/// parameter-declaration-list[opt] '...'[opt]
|
|
|
|
/// parameter-declaration-list ',' '...'
|
|
|
|
///
|
|
|
|
/// parameter-declaration-list:
|
|
|
|
/// parameter-declaration
|
|
|
|
/// parameter-declaration-list ',' parameter-declaration
|
|
|
|
///
|
|
|
|
/// parameter-declaration:
|
|
|
|
/// attribute-specifier-seq[opt] decl-specifier-seq declarator attributes[opt]
|
|
|
|
/// attribute-specifier-seq[opt] decl-specifier-seq declarator attributes[opt]
|
|
|
|
/// '=' assignment-expression
|
|
|
|
/// attribute-specifier-seq[opt] decl-specifier-seq abstract-declarator[opt]
|
|
|
|
/// attributes[opt]
|
|
|
|
/// attribute-specifier-seq[opt] decl-specifier-seq abstract-declarator[opt]
|
|
|
|
/// attributes[opt] '=' assignment-expression
|
|
|
|
///
|
|
|
|
Parser::TPResult
|
2013-09-13 07:28:08 +08:00
|
|
|
Parser::TryParseParameterDeclarationClause(bool *InvalidAsDeclaration,
|
|
|
|
bool VersusTemplateArgument) {
|
2012-12-18 22:30:41 +08:00
|
|
|
|
|
|
|
if (Tok.is(tok::r_paren))
|
2014-05-16 09:56:53 +08:00
|
|
|
return TPResult::Ambiguous;
|
2012-12-18 22:30:41 +08:00
|
|
|
|
|
|
|
// parameter-declaration-list[opt] '...'[opt]
|
|
|
|
// parameter-declaration-list ',' '...'
|
|
|
|
//
|
|
|
|
// parameter-declaration-list:
|
|
|
|
// parameter-declaration
|
|
|
|
// parameter-declaration-list ',' parameter-declaration
|
|
|
|
//
|
|
|
|
while (1) {
|
|
|
|
// '...'[opt]
|
|
|
|
if (Tok.is(tok::ellipsis)) {
|
|
|
|
ConsumeToken();
|
|
|
|
if (Tok.is(tok::r_paren))
|
2014-05-16 09:56:53 +08:00
|
|
|
return TPResult::True; // '...)' is a sign of a function declarator.
|
2012-12-18 22:30:41 +08:00
|
|
|
else
|
2014-05-16 09:56:53 +08:00
|
|
|
return TPResult::False;
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// An attribute-specifier-seq here is a sign of a function declarator.
|
|
|
|
if (isCXX11AttributeSpecifier(/*Disambiguate*/false,
|
|
|
|
/*OuterMightBeMessageSend*/true))
|
2014-05-16 09:56:53 +08:00
|
|
|
return TPResult::True;
|
2012-12-18 22:30:41 +08:00
|
|
|
|
|
|
|
ParsedAttributes attrs(AttrFactory);
|
|
|
|
MaybeParseMicrosoftAttributes(attrs);
|
|
|
|
|
|
|
|
// decl-specifier-seq
|
|
|
|
// A parameter-declaration's initializer must be preceded by an '=', so
|
|
|
|
// decl-specifier-seq '{' is not a parameter in C++11.
|
2014-05-16 09:56:53 +08:00
|
|
|
TPResult TPR = isCXXDeclarationSpecifier(TPResult::False,
|
2013-09-13 07:28:08 +08:00
|
|
|
InvalidAsDeclaration);
|
|
|
|
|
2014-05-16 09:56:53 +08:00
|
|
|
if (VersusTemplateArgument && TPR == TPResult::True) {
|
2013-09-13 07:28:08 +08:00
|
|
|
// Consume the decl-specifier-seq. We have to look past it, since a
|
|
|
|
// type-id might appear here in a template argument.
|
|
|
|
bool SeenType = false;
|
|
|
|
do {
|
|
|
|
SeenType |= isCXXDeclarationSpecifierAType();
|
2014-05-16 09:56:53 +08:00
|
|
|
if (TryConsumeDeclarationSpecifier() == TPResult::Error)
|
|
|
|
return TPResult::Error;
|
2013-09-13 07:28:08 +08:00
|
|
|
|
|
|
|
// If we see a parameter name, this can't be a template argument.
|
|
|
|
if (SeenType && Tok.is(tok::identifier))
|
2014-05-16 09:56:53 +08:00
|
|
|
return TPResult::True;
|
2013-09-13 07:28:08 +08:00
|
|
|
|
2014-05-16 09:56:53 +08:00
|
|
|
TPR = isCXXDeclarationSpecifier(TPResult::False,
|
2013-09-13 07:28:08 +08:00
|
|
|
InvalidAsDeclaration);
|
2014-05-16 09:56:53 +08:00
|
|
|
if (TPR == TPResult::Error)
|
2013-09-13 07:28:08 +08:00
|
|
|
return TPR;
|
2014-05-16 09:56:53 +08:00
|
|
|
} while (TPR != TPResult::False);
|
|
|
|
} else if (TPR == TPResult::Ambiguous) {
|
2013-09-13 07:28:08 +08:00
|
|
|
// Disambiguate what follows the decl-specifier.
|
2014-05-16 09:56:53 +08:00
|
|
|
if (TryConsumeDeclarationSpecifier() == TPResult::Error)
|
|
|
|
return TPResult::Error;
|
2013-09-13 07:28:08 +08:00
|
|
|
} else
|
2012-12-18 22:30:41 +08:00
|
|
|
return TPR;
|
|
|
|
|
|
|
|
// declarator
|
|
|
|
// abstract-declarator[opt]
|
2015-02-24 06:36:28 +08:00
|
|
|
TPR = TryParseDeclarator(true/*mayBeAbstract*/);
|
2014-05-16 09:56:53 +08:00
|
|
|
if (TPR != TPResult::Ambiguous)
|
2012-12-18 22:30:41 +08:00
|
|
|
return TPR;
|
|
|
|
|
|
|
|
// [GNU] attributes[opt]
|
|
|
|
if (Tok.is(tok::kw___attribute))
|
2014-05-16 09:56:53 +08:00
|
|
|
return TPResult::True;
|
2012-12-18 22:30:41 +08:00
|
|
|
|
2013-09-13 07:28:08 +08:00
|
|
|
// If we're disambiguating a template argument in a default argument in
|
|
|
|
// a class definition versus a parameter declaration, an '=' here
|
|
|
|
// disambiguates the parse one way or the other.
|
|
|
|
// If this is a parameter, it must have a default argument because
|
|
|
|
// (a) the previous parameter did, and
|
|
|
|
// (b) this must be the first declaration of the function, so we can't
|
|
|
|
// inherit any default arguments from elsewhere.
|
|
|
|
// If we see an ')', then we've reached the end of a
|
|
|
|
// parameter-declaration-clause, and the last param is missing its default
|
|
|
|
// argument.
|
|
|
|
if (VersusTemplateArgument)
|
2015-06-18 18:59:26 +08:00
|
|
|
return Tok.isOneOf(tok::equal, tok::r_paren) ? TPResult::True
|
|
|
|
: TPResult::False;
|
2013-09-13 07:28:08 +08:00
|
|
|
|
2012-12-18 22:30:41 +08:00
|
|
|
if (Tok.is(tok::equal)) {
|
|
|
|
// '=' assignment-expression
|
|
|
|
// Parse through assignment-expression.
|
2013-09-13 07:28:08 +08:00
|
|
|
// FIXME: assignment-expression may contain an unparenthesized comma.
|
2013-11-18 16:17:37 +08:00
|
|
|
if (!SkipUntil(tok::comma, tok::r_paren, StopAtSemi | StopBeforeMatch))
|
2014-05-16 09:56:53 +08:00
|
|
|
return TPResult::Error;
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Tok.is(tok::ellipsis)) {
|
|
|
|
ConsumeToken();
|
|
|
|
if (Tok.is(tok::r_paren))
|
2014-05-16 09:56:53 +08:00
|
|
|
return TPResult::True; // '...)' is a sign of a function declarator.
|
2012-12-18 22:30:41 +08:00
|
|
|
else
|
2014-05-16 09:56:53 +08:00
|
|
|
return TPResult::False;
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
|
2014-01-10 19:19:30 +08:00
|
|
|
if (!TryConsumeToken(tok::comma))
|
2012-12-18 22:30:41 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-05-16 09:56:53 +08:00
|
|
|
return TPResult::Ambiguous;
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// TryParseFunctionDeclarator - We parsed a '(' and we want to try to continue
|
|
|
|
/// parsing as a function declarator.
|
|
|
|
/// If TryParseFunctionDeclarator fully parsed the function declarator, it will
|
2015-02-24 06:36:28 +08:00
|
|
|
/// return TPResult::Ambiguous, otherwise it will return either False() or
|
|
|
|
/// Error().
|
2012-12-18 22:30:41 +08:00
|
|
|
///
|
|
|
|
/// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
|
|
|
|
/// exception-specification[opt]
|
|
|
|
///
|
|
|
|
/// exception-specification:
|
|
|
|
/// 'throw' '(' type-id-list[opt] ')'
|
|
|
|
///
|
2015-02-24 06:36:28 +08:00
|
|
|
Parser::TPResult Parser::TryParseFunctionDeclarator() {
|
2012-12-18 22:30:41 +08:00
|
|
|
|
|
|
|
// The '(' is already parsed.
|
|
|
|
|
|
|
|
TPResult TPR = TryParseParameterDeclarationClause();
|
2014-05-16 09:56:53 +08:00
|
|
|
if (TPR == TPResult::Ambiguous && Tok.isNot(tok::r_paren))
|
|
|
|
TPR = TPResult::False;
|
2012-12-18 22:30:41 +08:00
|
|
|
|
2015-02-24 06:36:28 +08:00
|
|
|
if (TPR == TPResult::False || TPR == TPResult::Error)
|
|
|
|
return TPR;
|
2012-12-18 22:30:41 +08:00
|
|
|
|
|
|
|
// Parse through the parens.
|
2013-11-18 16:17:37 +08:00
|
|
|
if (!SkipUntil(tok::r_paren, StopAtSemi))
|
2014-05-16 09:56:53 +08:00
|
|
|
return TPResult::Error;
|
2012-12-18 22:30:41 +08:00
|
|
|
|
|
|
|
// cv-qualifier-seq
|
2015-06-18 18:59:26 +08:00
|
|
|
while (Tok.isOneOf(tok::kw_const, tok::kw_volatile, tok::kw_restrict))
|
2012-12-18 22:30:41 +08:00
|
|
|
ConsumeToken();
|
|
|
|
|
|
|
|
// ref-qualifier[opt]
|
2015-06-18 18:59:26 +08:00
|
|
|
if (Tok.isOneOf(tok::amp, tok::ampamp))
|
2012-12-18 22:30:41 +08:00
|
|
|
ConsumeToken();
|
|
|
|
|
|
|
|
// exception-specification
|
|
|
|
if (Tok.is(tok::kw_throw)) {
|
|
|
|
ConsumeToken();
|
|
|
|
if (Tok.isNot(tok::l_paren))
|
2014-05-16 09:56:53 +08:00
|
|
|
return TPResult::Error;
|
2012-12-18 22:30:41 +08:00
|
|
|
|
|
|
|
// Parse through the parens after 'throw'.
|
|
|
|
ConsumeParen();
|
2013-11-18 16:17:37 +08:00
|
|
|
if (!SkipUntil(tok::r_paren, StopAtSemi))
|
2014-05-16 09:56:53 +08:00
|
|
|
return TPResult::Error;
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
if (Tok.is(tok::kw_noexcept)) {
|
|
|
|
ConsumeToken();
|
|
|
|
// Possibly an expression as well.
|
|
|
|
if (Tok.is(tok::l_paren)) {
|
|
|
|
// Find the matching rparen.
|
|
|
|
ConsumeParen();
|
2013-11-18 16:17:37 +08:00
|
|
|
if (!SkipUntil(tok::r_paren, StopAtSemi))
|
2014-05-16 09:56:53 +08:00
|
|
|
return TPResult::Error;
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-16 09:56:53 +08:00
|
|
|
return TPResult::Ambiguous;
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// '[' constant-expression[opt] ']'
|
|
|
|
///
|
|
|
|
Parser::TPResult Parser::TryParseBracketDeclarator() {
|
|
|
|
ConsumeBracket();
|
2013-11-18 16:17:37 +08:00
|
|
|
if (!SkipUntil(tok::r_square, StopAtSemi))
|
2014-05-16 09:56:53 +08:00
|
|
|
return TPResult::Error;
|
2012-12-18 22:30:41 +08:00
|
|
|
|
2014-05-16 09:56:53 +08:00
|
|
|
return TPResult::Ambiguous;
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|