2008-10-05 08:06:24 +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"
|
2009-01-29 13:15:15 +08:00
|
|
|
#include "clang/Parse/ParseDiagnostic.h"
|
2008-10-05 08:06:24 +08:00
|
|
|
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
|
2009-03-12 00:27:10 +08:00
|
|
|
/// [C++0x] static_assert-declaration
|
2008-10-05 08:06:24 +08:00
|
|
|
///
|
|
|
|
/// 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:
|
|
|
|
return true;
|
2009-03-12 00:27:10 +08:00
|
|
|
case tok::kw_static_assert:
|
|
|
|
// static_assert-declaration
|
|
|
|
return true;
|
2008-10-05 08:06:24 +08:00
|
|
|
default:
|
|
|
|
// simple-declaration
|
|
|
|
return isCXXSimpleDeclaration();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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] ';'
|
|
|
|
///
|
|
|
|
bool Parser::isCXXSimpleDeclaration() {
|
|
|
|
// 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.
|
2008-10-06 02:52:21 +08:00
|
|
|
// isCXXDeclarationSpecifier will return TPResult::Ambiguous() only in such
|
|
|
|
// a case.
|
2008-10-05 08:06:24 +08:00
|
|
|
|
2008-10-06 02:52:21 +08:00
|
|
|
TPResult TPR = isCXXDeclarationSpecifier();
|
|
|
|
if (TPR != TPResult::Ambiguous())
|
|
|
|
return TPR != TPResult::False(); // Returns true for TPResult::True() or
|
|
|
|
// TPResult::Error().
|
2008-10-05 08:06:24 +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);
|
|
|
|
|
|
|
|
TPR = TryParseSimpleDeclaration();
|
|
|
|
SourceLocation TentativeParseLoc = Tok.getLocation();
|
|
|
|
|
|
|
|
PA.Revert();
|
|
|
|
|
|
|
|
// In case of an error, let the declaration parsing code handle it.
|
2008-10-06 02:52:21 +08:00
|
|
|
if (TPR == TPResult::Error())
|
2008-10-05 08:06:24 +08:00
|
|
|
return true;
|
|
|
|
|
|
|
|
// Declarations take precedence over expressions.
|
2008-10-06 02:52:21 +08:00
|
|
|
if (TPR == TPResult::Ambiguous())
|
|
|
|
TPR = TPResult::True();
|
2008-10-05 08:06:24 +08:00
|
|
|
|
2008-10-06 02:52:21 +08:00
|
|
|
assert(TPR == TPResult::True() || TPR == TPResult::False());
|
|
|
|
return TPR == TPResult::True();
|
2008-10-05 08:06:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// simple-declaration:
|
|
|
|
/// decl-specifier-seq init-declarator-list[opt] ';'
|
|
|
|
///
|
2008-10-06 02:52:21 +08:00
|
|
|
Parser::TPResult Parser::TryParseSimpleDeclaration() {
|
2008-10-05 08:06:24 +08:00
|
|
|
// We know that we have a simple-type-specifier/typename-specifier followed
|
|
|
|
// by a '('.
|
2008-10-06 02:52:21 +08:00
|
|
|
assert(isCXXDeclarationSpecifier() == TPResult::Ambiguous());
|
2008-10-05 08:06:24 +08:00
|
|
|
|
|
|
|
if (Tok.is(tok::kw_typeof))
|
|
|
|
TryParseTypeofSpecifier();
|
|
|
|
else
|
|
|
|
ConsumeToken();
|
|
|
|
|
|
|
|
assert(Tok.is(tok::l_paren) && "Expected '('");
|
|
|
|
|
2008-10-06 02:52:21 +08:00
|
|
|
TPResult TPR = TryParseInitDeclaratorList();
|
|
|
|
if (TPR != TPResult::Ambiguous())
|
2008-10-05 08:06:24 +08:00
|
|
|
return TPR;
|
|
|
|
|
|
|
|
if (Tok.isNot(tok::semi))
|
2008-10-06 02:52:21 +08:00
|
|
|
return TPResult::False();
|
2008-10-05 08:06:24 +08:00
|
|
|
|
2008-10-06 02:52:21 +08:00
|
|
|
return TPResult::Ambiguous();
|
2008-10-05 08:06:24 +08:00
|
|
|
}
|
|
|
|
|
2008-10-05 22:27:18 +08:00
|
|
|
/// init-declarator-list:
|
|
|
|
/// init-declarator
|
|
|
|
/// init-declarator-list ',' init-declarator
|
2008-10-05 08:06:24 +08:00
|
|
|
///
|
2008-10-05 22:27:18 +08:00
|
|
|
/// init-declarator:
|
|
|
|
/// declarator initializer[opt]
|
|
|
|
/// [GNU] declarator simple-asm-expr[opt] attributes[opt] initializer[opt]
|
2008-10-05 08:06:24 +08:00
|
|
|
///
|
|
|
|
/// initializer:
|
|
|
|
/// '=' initializer-clause
|
|
|
|
/// '(' expression-list ')'
|
|
|
|
///
|
|
|
|
/// initializer-clause:
|
|
|
|
/// assignment-expression
|
|
|
|
/// '{' initializer-list ','[opt] '}'
|
|
|
|
/// '{' '}'
|
|
|
|
///
|
2008-10-06 02:52:21 +08:00
|
|
|
Parser::TPResult Parser::TryParseInitDeclaratorList() {
|
2008-10-05 08:06:24 +08:00
|
|
|
// GCC only examines the first declarator for disambiguation:
|
|
|
|
// i.e:
|
|
|
|
// int(x), ++x; // GCC regards it as ill-formed declaration.
|
|
|
|
//
|
|
|
|
// Comeau and MSVC will regard the above statement as correct expression.
|
|
|
|
// Clang examines all of the declarators and also regards the above statement
|
|
|
|
// as correct expression.
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
// declarator
|
2008-10-06 02:52:21 +08:00
|
|
|
TPResult TPR = TryParseDeclarator(false/*mayBeAbstract*/);
|
|
|
|
if (TPR != TPResult::Ambiguous())
|
2008-10-05 08:06:24 +08:00
|
|
|
return TPR;
|
|
|
|
|
2008-10-05 22:27:18 +08:00
|
|
|
// [GNU] simple-asm-expr[opt] attributes[opt]
|
|
|
|
if (Tok.is(tok::kw_asm) || Tok.is(tok::kw___attribute))
|
2008-10-06 02:52:21 +08:00
|
|
|
return TPResult::True();
|
2008-10-05 22:27:18 +08:00
|
|
|
|
2008-10-05 08:06:24 +08:00
|
|
|
// initializer[opt]
|
|
|
|
if (Tok.is(tok::l_paren)) {
|
|
|
|
// Parse through the parens.
|
|
|
|
ConsumeParen();
|
|
|
|
if (!SkipUntil(tok::r_paren))
|
2008-10-06 02:52:21 +08:00
|
|
|
return TPResult::Error();
|
2008-10-05 08:06:24 +08:00
|
|
|
} else if (Tok.is(tok::equal)) {
|
|
|
|
// MSVC won't examine the rest of declarators if '=' is encountered, it
|
|
|
|
// will conclude that it is a declaration.
|
|
|
|
// Comeau and Clang will examine the rest of declarators.
|
|
|
|
// Note that "int(x) = {0}, ++x;" will be interpreted as ill-formed
|
|
|
|
// expression.
|
|
|
|
//
|
|
|
|
// Parse through the initializer-clause.
|
|
|
|
SkipUntil(tok::comma, true/*StopAtSemi*/, true/*DontConsume*/);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Tok.isNot(tok::comma))
|
|
|
|
break;
|
|
|
|
ConsumeToken(); // the comma.
|
|
|
|
}
|
|
|
|
|
2008-10-06 02:52:21 +08:00
|
|
|
return TPResult::Ambiguous();
|
2008-10-05 08:06:24 +08:00
|
|
|
}
|
|
|
|
|
2008-10-05 23:03:47 +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
|
|
|
|
/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
|
|
|
|
/// '=' assignment-expression
|
|
|
|
///
|
|
|
|
bool Parser::isCXXConditionDeclaration() {
|
2008-10-06 02:52:21 +08:00
|
|
|
TPResult TPR = isCXXDeclarationSpecifier();
|
|
|
|
if (TPR != TPResult::Ambiguous())
|
|
|
|
return TPR != TPResult::False(); // Returns true for TPResult::True() or
|
|
|
|
// TPResult::Error().
|
2008-10-05 23:03:47 +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
|
|
|
|
if (Tok.is(tok::kw_typeof))
|
|
|
|
TryParseTypeofSpecifier();
|
|
|
|
else
|
|
|
|
ConsumeToken();
|
|
|
|
assert(Tok.is(tok::l_paren) && "Expected '('");
|
|
|
|
|
|
|
|
// declarator
|
|
|
|
TPR = TryParseDeclarator(false/*mayBeAbstract*/);
|
|
|
|
|
|
|
|
// In case of an error, let the declaration parsing code handle it.
|
2008-10-06 02:52:21 +08:00
|
|
|
if (TPR == TPResult::Error())
|
|
|
|
TPR = TPResult::True();
|
2008-10-05 23:03:47 +08:00
|
|
|
|
2008-10-06 02:52:21 +08:00
|
|
|
if (TPR == TPResult::Ambiguous()) {
|
2008-10-05 23:03:47 +08:00
|
|
|
// '='
|
|
|
|
// [GNU] simple-asm-expr[opt] attributes[opt]
|
|
|
|
if (Tok.is(tok::equal) ||
|
|
|
|
Tok.is(tok::kw_asm) || Tok.is(tok::kw___attribute))
|
2008-10-06 02:52:21 +08:00
|
|
|
TPR = TPResult::True();
|
2008-10-05 23:03:47 +08:00
|
|
|
else
|
2008-10-06 02:52:21 +08:00
|
|
|
TPR = TPResult::False();
|
2008-10-05 23:03:47 +08:00
|
|
|
}
|
|
|
|
|
2008-10-05 23:19:49 +08:00
|
|
|
PA.Revert();
|
|
|
|
|
2008-10-06 02:52:21 +08:00
|
|
|
assert(TPR == TPResult::True() || TPR == TPResult::False());
|
|
|
|
return TPR == TPResult::True();
|
2008-10-05 23:03:47 +08:00
|
|
|
}
|
|
|
|
|
2009-02-10 08:53:15 +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]
|
|
|
|
///
|
2009-05-22 18:24:42 +08:00
|
|
|
bool Parser::isCXXTypeId(TentativeCXXTypeIdContext Context, bool &isAmbiguous) {
|
|
|
|
|
|
|
|
isAmbiguous = false;
|
2008-10-06 05:10:08 +08:00
|
|
|
|
|
|
|
// 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.
|
|
|
|
|
2008-10-06 03:56:22 +08:00
|
|
|
TPResult TPR = isCXXDeclarationSpecifier();
|
|
|
|
if (TPR != TPResult::Ambiguous())
|
|
|
|
return TPR != TPResult::False(); // Returns true for TPResult::True() or
|
|
|
|
// TPResult::Error().
|
|
|
|
|
|
|
|
// 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
|
|
|
|
if (Tok.is(tok::kw_typeof))
|
|
|
|
TryParseTypeofSpecifier();
|
|
|
|
else
|
|
|
|
ConsumeToken();
|
|
|
|
assert(Tok.is(tok::l_paren) && "Expected '('");
|
|
|
|
|
|
|
|
// declarator
|
|
|
|
TPR = TryParseDeclarator(true/*mayBeAbstract*/, false/*mayHaveIdentifier*/);
|
|
|
|
|
|
|
|
// In case of an error, let the declaration parsing code handle it.
|
|
|
|
if (TPR == TPResult::Error())
|
|
|
|
TPR = TPResult::True();
|
|
|
|
|
|
|
|
if (TPR == TPResult::Ambiguous()) {
|
|
|
|
// 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.
|
2009-05-22 18:24:42 +08:00
|
|
|
if (Context == TypeIdInParens && Tok.is(tok::r_paren)) {
|
2009-02-10 08:53:15 +08:00
|
|
|
TPR = TPResult::True();
|
2009-05-22 18:24:42 +08:00
|
|
|
isAmbiguous = true;
|
|
|
|
|
2009-02-10 08:53:15 +08:00
|
|
|
// 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.
|
2009-05-22 18:24:42 +08:00
|
|
|
} else if (Context == TypeIdAsTemplateArgument &&
|
|
|
|
(Tok.is(tok::greater) || Tok.is(tok::comma) ||
|
|
|
|
(getLang().CPlusPlus0x && Tok.is(tok::greatergreater)))) {
|
2008-10-06 03:56:22 +08:00
|
|
|
TPR = TPResult::True();
|
2009-05-22 18:24:42 +08:00
|
|
|
isAmbiguous = true;
|
|
|
|
|
|
|
|
} else
|
2008-10-06 03:56:22 +08:00
|
|
|
TPR = TPResult::False();
|
|
|
|
}
|
|
|
|
|
|
|
|
PA.Revert();
|
|
|
|
|
|
|
|
assert(TPR == TPResult::True() || TPR == TPResult::False());
|
|
|
|
return TPR == TPResult::True();
|
|
|
|
}
|
|
|
|
|
2008-10-05 08:06:24 +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 ')'
|
2008-10-05 22:27:18 +08:00
|
|
|
/// [GNU] '(' attributes declarator ')'
|
2008-10-05 08:06:24 +08:00
|
|
|
///
|
|
|
|
/// 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]
|
2009-01-25 07:29:36 +08:00
|
|
|
/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
|
2008-10-05 08:06:24 +08:00
|
|
|
///
|
|
|
|
/// cv-qualifier-seq:
|
|
|
|
/// cv-qualifier cv-qualifier-seq[opt]
|
|
|
|
///
|
|
|
|
/// cv-qualifier:
|
|
|
|
/// 'const'
|
|
|
|
/// 'volatile'
|
|
|
|
///
|
|
|
|
/// declarator-id:
|
|
|
|
/// id-expression
|
|
|
|
///
|
|
|
|
/// id-expression:
|
|
|
|
/// unqualified-id
|
|
|
|
/// qualified-id [TODO]
|
|
|
|
///
|
|
|
|
/// unqualified-id:
|
|
|
|
/// identifier
|
|
|
|
/// operator-function-id [TODO]
|
|
|
|
/// conversion-function-id [TODO]
|
|
|
|
/// '~' class-name [TODO]
|
|
|
|
/// template-id [TODO]
|
|
|
|
///
|
2008-10-06 03:56:22 +08:00
|
|
|
Parser::TPResult Parser::TryParseDeclarator(bool mayBeAbstract,
|
|
|
|
bool mayHaveIdentifier) {
|
2008-10-05 08:06:24 +08:00
|
|
|
// declarator:
|
|
|
|
// direct-declarator
|
|
|
|
// ptr-operator declarator
|
|
|
|
|
|
|
|
while (1) {
|
2009-01-25 07:29:36 +08:00
|
|
|
if (Tok.is(tok::coloncolon) || Tok.is(tok::identifier))
|
|
|
|
TryAnnotateCXXScopeToken();
|
|
|
|
|
2009-03-27 12:18:06 +08:00
|
|
|
if (Tok.is(tok::star) || Tok.is(tok::amp) || Tok.is(tok::caret) ||
|
2009-01-25 07:29:36 +08:00
|
|
|
(Tok.is(tok::annot_cxxscope) && NextToken().is(tok::star))) {
|
2008-10-05 08:06:24 +08:00
|
|
|
// ptr-operator
|
|
|
|
ConsumeToken();
|
|
|
|
while (Tok.is(tok::kw_const) ||
|
|
|
|
Tok.is(tok::kw_volatile) ||
|
2009-03-27 12:18:06 +08:00
|
|
|
Tok.is(tok::kw_restrict))
|
2008-10-05 08:06:24 +08:00
|
|
|
ConsumeToken();
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// direct-declarator:
|
|
|
|
// direct-abstract-declarator:
|
|
|
|
|
2009-07-22 01:05:03 +08:00
|
|
|
if ((Tok.is(tok::identifier) ||
|
|
|
|
(Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) &&
|
|
|
|
mayHaveIdentifier) {
|
2008-10-05 08:06:24 +08:00
|
|
|
// declarator-id
|
2009-07-22 01:05:03 +08:00
|
|
|
if (Tok.is(tok::annot_cxxscope))
|
|
|
|
ConsumeToken();
|
2008-10-05 08:06:24 +08:00
|
|
|
ConsumeToken();
|
|
|
|
} else if (Tok.is(tok::l_paren)) {
|
2008-10-06 07:15:41 +08:00
|
|
|
ConsumeParen();
|
|
|
|
if (mayBeAbstract &&
|
|
|
|
(Tok.is(tok::r_paren) || // 'int()' is a function.
|
|
|
|
Tok.is(tok::ellipsis) || // 'int(...)' is a function.
|
|
|
|
isDeclarationSpecifier())) { // 'int(int)' is a function.
|
2008-10-05 08:06:24 +08:00
|
|
|
// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
|
|
|
|
// exception-specification[opt]
|
2008-10-06 02:52:21 +08:00
|
|
|
TPResult TPR = TryParseFunctionDeclarator();
|
|
|
|
if (TPR != TPResult::Ambiguous())
|
2008-10-05 08:06:24 +08:00
|
|
|
return TPR;
|
|
|
|
} else {
|
|
|
|
// '(' declarator ')'
|
2008-10-05 22:27:18 +08:00
|
|
|
// '(' attributes declarator ')'
|
2008-10-05 08:06:24 +08:00
|
|
|
// '(' abstract-declarator ')'
|
2008-10-05 22:27:18 +08:00
|
|
|
if (Tok.is(tok::kw___attribute))
|
2008-10-06 02:52:21 +08:00
|
|
|
return TPResult::True(); // attributes indicate declaration
|
2008-10-06 03:56:22 +08:00
|
|
|
TPResult TPR = TryParseDeclarator(mayBeAbstract, mayHaveIdentifier);
|
2008-10-06 02:52:21 +08:00
|
|
|
if (TPR != TPResult::Ambiguous())
|
2008-10-05 08:06:24 +08:00
|
|
|
return TPR;
|
|
|
|
if (Tok.isNot(tok::r_paren))
|
2008-10-06 02:52:21 +08:00
|
|
|
return TPResult::False();
|
2008-10-05 08:06:24 +08:00
|
|
|
ConsumeParen();
|
|
|
|
}
|
|
|
|
} else if (!mayBeAbstract) {
|
2008-10-06 02:52:21 +08:00
|
|
|
return TPResult::False();
|
2008-10-05 08:06:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
while (1) {
|
2008-10-06 02:52:21 +08:00
|
|
|
TPResult TPR(TPResult::Ambiguous());
|
2008-10-05 08:06:24 +08:00
|
|
|
|
|
|
|
if (Tok.is(tok::l_paren)) {
|
2008-10-06 07:15:41 +08:00
|
|
|
// 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.
|
2008-10-18 07:23:35 +08:00
|
|
|
if (!mayBeAbstract && !isCXXFunctionDeclarator(false/*warnIfAmbiguous*/))
|
2008-10-06 07:15:41 +08:00
|
|
|
break;
|
|
|
|
|
2008-10-05 08:06:24 +08:00
|
|
|
// direct-declarator '(' parameter-declaration-clause ')'
|
|
|
|
// cv-qualifier-seq[opt] exception-specification[opt]
|
2008-10-06 07:15:41 +08:00
|
|
|
ConsumeParen();
|
2008-10-05 08:06:24 +08:00
|
|
|
TPR = TryParseFunctionDeclarator();
|
|
|
|
} else if (Tok.is(tok::l_square)) {
|
|
|
|
// direct-declarator '[' constant-expression[opt] ']'
|
|
|
|
// direct-abstract-declarator[opt] '[' constant-expression[opt] ']'
|
|
|
|
TPR = TryParseBracketDeclarator();
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-10-06 02:52:21 +08:00
|
|
|
if (TPR != TPResult::Ambiguous())
|
2008-10-05 08:06:24 +08:00
|
|
|
return TPR;
|
|
|
|
}
|
|
|
|
|
2008-10-06 02:52:21 +08:00
|
|
|
return TPResult::Ambiguous();
|
2008-10-05 08:06:24 +08:00
|
|
|
}
|
|
|
|
|
2008-10-06 02:52:21 +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()
|
|
|
|
/// if a parsing error was found and reported.
|
2008-10-05 08:06:24 +08:00
|
|
|
///
|
|
|
|
/// decl-specifier:
|
|
|
|
/// storage-class-specifier
|
|
|
|
/// type-specifier
|
|
|
|
/// function-specifier
|
|
|
|
/// 'friend'
|
|
|
|
/// 'typedef'
|
|
|
|
/// [GNU] attributes declaration-specifiers[opt]
|
|
|
|
///
|
|
|
|
/// storage-class-specifier:
|
|
|
|
/// 'register'
|
|
|
|
/// 'static'
|
|
|
|
/// 'extern'
|
|
|
|
/// 'mutable'
|
|
|
|
/// 'auto'
|
|
|
|
/// [GNU] '__thread'
|
|
|
|
///
|
|
|
|
/// function-specifier:
|
|
|
|
/// 'inline'
|
|
|
|
/// 'virtual'
|
|
|
|
/// 'explicit'
|
|
|
|
///
|
|
|
|
/// typedef-name:
|
|
|
|
/// identifier
|
|
|
|
///
|
|
|
|
/// type-specifier:
|
|
|
|
/// simple-type-specifier
|
|
|
|
/// class-specifier
|
|
|
|
/// enum-specifier
|
|
|
|
/// elaborated-type-specifier
|
2009-03-28 07:10:48 +08:00
|
|
|
/// typename-specifier
|
2008-10-05 08:06:24 +08:00
|
|
|
/// cv-qualifier
|
|
|
|
///
|
|
|
|
/// simple-type-specifier:
|
2009-03-28 07:10:48 +08:00
|
|
|
/// '::'[opt] nested-name-specifier[opt] type-name
|
2008-10-05 08:06:24 +08:00
|
|
|
/// '::'[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'
|
|
|
|
/// [C++0x] 'auto' [TODO]
|
2009-06-25 01:47:40 +08:00
|
|
|
/// [C++0x] 'decltype' ( expression )
|
2008-10-05 08:06:24 +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
|
|
|
|
///
|
2008-10-06 02:52:21 +08:00
|
|
|
Parser::TPResult Parser::isCXXDeclarationSpecifier() {
|
2008-10-05 08:06:24 +08:00
|
|
|
switch (Tok.getKind()) {
|
2009-01-05 07:33:56 +08:00
|
|
|
case tok::identifier: // foo::bar
|
2009-03-28 07:10:48 +08:00
|
|
|
case tok::kw_typename: // typename T::type
|
2009-01-05 07:33:56 +08:00
|
|
|
// Annotate typenames and C++ scope specifiers. If we get one, just
|
|
|
|
// recurse to handle whatever we get.
|
|
|
|
if (TryAnnotateTypeOrScopeToken())
|
|
|
|
return isCXXDeclarationSpecifier();
|
|
|
|
// Otherwise, not a typename.
|
|
|
|
return TPResult::False();
|
|
|
|
|
|
|
|
case tok::coloncolon: // ::foo::bar
|
|
|
|
if (NextToken().is(tok::kw_new) || // ::new
|
|
|
|
NextToken().is(tok::kw_delete)) // ::delete
|
|
|
|
return TPResult::False();
|
|
|
|
|
|
|
|
// Annotate typenames and C++ scope specifiers. If we get one, just
|
|
|
|
// recurse to handle whatever we get.
|
|
|
|
if (TryAnnotateTypeOrScopeToken())
|
|
|
|
return isCXXDeclarationSpecifier();
|
|
|
|
// Otherwise, not a typename.
|
|
|
|
return TPResult::False();
|
|
|
|
|
2008-10-05 08:06:24 +08:00
|
|
|
// decl-specifier:
|
|
|
|
// storage-class-specifier
|
|
|
|
// type-specifier
|
|
|
|
// function-specifier
|
|
|
|
// 'friend'
|
|
|
|
// 'typedef'
|
|
|
|
|
|
|
|
case tok::kw_friend:
|
|
|
|
case tok::kw_typedef:
|
|
|
|
// 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:
|
|
|
|
// function-specifier
|
|
|
|
case tok::kw_inline:
|
|
|
|
case tok::kw_virtual:
|
|
|
|
case tok::kw_explicit:
|
|
|
|
|
|
|
|
// 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:
|
|
|
|
// 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:
|
2008-10-06 02:52:21 +08:00
|
|
|
return TPResult::True();
|
2009-01-07 01:40:00 +08:00
|
|
|
|
|
|
|
// Microsoft
|
2009-01-07 03:34:12 +08:00
|
|
|
case tok::kw___declspec:
|
2009-01-07 01:40:00 +08:00
|
|
|
case tok::kw___cdecl:
|
|
|
|
case tok::kw___stdcall:
|
|
|
|
case tok::kw___fastcall:
|
2009-06-09 07:27:34 +08:00
|
|
|
case tok::kw___w64:
|
|
|
|
case tok::kw___ptr64:
|
|
|
|
case tok::kw___forceinline:
|
|
|
|
return TPResult::True();
|
2008-10-05 08:06:24 +08:00
|
|
|
|
|
|
|
// 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::kw_char:
|
|
|
|
case tok::kw_wchar_t:
|
2009-07-14 14:30:34 +08:00
|
|
|
case tok::kw_char16_t:
|
|
|
|
case tok::kw_char32_t:
|
2008-10-05 08:06:24 +08:00
|
|
|
case tok::kw_bool:
|
|
|
|
case tok::kw_short:
|
|
|
|
case tok::kw_int:
|
|
|
|
case tok::kw_long:
|
|
|
|
case tok::kw_signed:
|
|
|
|
case tok::kw_unsigned:
|
|
|
|
case tok::kw_float:
|
|
|
|
case tok::kw_double:
|
|
|
|
case tok::kw_void:
|
2009-01-06 13:06:21 +08:00
|
|
|
case tok::annot_typename:
|
2008-10-05 08:06:24 +08:00
|
|
|
if (NextToken().is(tok::l_paren))
|
2008-10-06 02:52:21 +08:00
|
|
|
return TPResult::Ambiguous();
|
2008-10-05 08:06:24 +08:00
|
|
|
|
2008-10-06 02:52:21 +08:00
|
|
|
return TPResult::True();
|
2008-10-05 08:06:24 +08:00
|
|
|
|
2009-06-25 01:47:40 +08:00
|
|
|
// GNU typeof support.
|
2008-10-05 08:06:24 +08:00
|
|
|
case tok::kw_typeof: {
|
|
|
|
if (NextToken().isNot(tok::l_paren))
|
2008-10-06 02:52:21 +08:00
|
|
|
return TPResult::True();
|
2008-10-05 08:06:24 +08:00
|
|
|
|
|
|
|
TentativeParsingAction PA(*this);
|
|
|
|
|
2008-10-06 02:52:21 +08:00
|
|
|
TPResult TPR = TryParseTypeofSpecifier();
|
2008-10-05 08:06:24 +08:00
|
|
|
bool isFollowedByParen = Tok.is(tok::l_paren);
|
|
|
|
|
|
|
|
PA.Revert();
|
|
|
|
|
2008-10-06 02:52:21 +08:00
|
|
|
if (TPR == TPResult::Error())
|
|
|
|
return TPResult::Error();
|
2008-10-05 08:06:24 +08:00
|
|
|
|
|
|
|
if (isFollowedByParen)
|
2008-10-06 02:52:21 +08:00
|
|
|
return TPResult::Ambiguous();
|
2008-10-05 08:06:24 +08:00
|
|
|
|
2008-10-06 02:52:21 +08:00
|
|
|
return TPResult::True();
|
2008-10-05 08:06:24 +08:00
|
|
|
}
|
|
|
|
|
2009-06-25 01:47:40 +08:00
|
|
|
// C++0x decltype support.
|
|
|
|
case tok::kw_decltype:
|
|
|
|
return TPResult::True();
|
|
|
|
|
2008-10-05 08:06:24 +08:00
|
|
|
default:
|
2008-10-06 02:52:21 +08:00
|
|
|
return TPResult::False();
|
2008-10-05 08:06:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// [GNU] typeof-specifier:
|
|
|
|
/// 'typeof' '(' expressions ')'
|
|
|
|
/// 'typeof' '(' type-name ')'
|
|
|
|
///
|
2008-10-06 02:52:21 +08:00
|
|
|
Parser::TPResult Parser::TryParseTypeofSpecifier() {
|
2008-10-05 08:06:24 +08:00
|
|
|
assert(Tok.is(tok::kw_typeof) && "Expected 'typeof'!");
|
|
|
|
ConsumeToken();
|
|
|
|
|
|
|
|
assert(Tok.is(tok::l_paren) && "Expected '('");
|
|
|
|
// Parse through the parens after 'typeof'.
|
|
|
|
ConsumeParen();
|
|
|
|
if (!SkipUntil(tok::r_paren))
|
2008-10-06 02:52:21 +08:00
|
|
|
return TPResult::Error();
|
2008-10-05 08:06:24 +08:00
|
|
|
|
2008-10-06 02:52:21 +08:00
|
|
|
return TPResult::Ambiguous();
|
2008-10-05 08:06:24 +08:00
|
|
|
}
|
|
|
|
|
2008-10-06 02:52:21 +08:00
|
|
|
Parser::TPResult Parser::TryParseDeclarationSpecifier() {
|
|
|
|
TPResult TPR = isCXXDeclarationSpecifier();
|
|
|
|
if (TPR != TPResult::Ambiguous())
|
2008-10-05 08:06:24 +08:00
|
|
|
return TPR;
|
|
|
|
|
|
|
|
if (Tok.is(tok::kw_typeof))
|
|
|
|
TryParseTypeofSpecifier();
|
|
|
|
else
|
|
|
|
ConsumeToken();
|
|
|
|
|
|
|
|
assert(Tok.is(tok::l_paren) && "Expected '('!");
|
2008-10-06 02:52:21 +08:00
|
|
|
return TPResult::Ambiguous();
|
2008-10-05 08:06:24 +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]
|
|
|
|
///
|
2008-10-18 07:23:35 +08:00
|
|
|
bool Parser::isCXXFunctionDeclarator(bool warnIfAmbiguous) {
|
2008-10-06 05:10:08 +08:00
|
|
|
|
|
|
|
// 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.
|
|
|
|
|
2008-10-05 08:06:24 +08:00
|
|
|
TentativeParsingAction PA(*this);
|
|
|
|
|
|
|
|
ConsumeParen();
|
2008-10-06 02:52:21 +08:00
|
|
|
TPResult TPR = TryParseParameterDeclarationClause();
|
|
|
|
if (TPR == TPResult::Ambiguous() && Tok.isNot(tok::r_paren))
|
|
|
|
TPR = TPResult::False();
|
2008-10-05 08:06:24 +08:00
|
|
|
|
2008-10-16 07:21:32 +08:00
|
|
|
SourceLocation TPLoc = Tok.getLocation();
|
2008-10-05 08:06:24 +08:00
|
|
|
PA.Revert();
|
|
|
|
|
|
|
|
// In case of an error, let the declaration parsing code handle it.
|
2008-10-06 02:52:21 +08:00
|
|
|
if (TPR == TPResult::Error())
|
2008-10-05 08:06:24 +08:00
|
|
|
return true;
|
|
|
|
|
2008-10-16 07:21:32 +08:00
|
|
|
if (TPR == TPResult::Ambiguous()) {
|
|
|
|
// Function declarator has precedence over constructor-style initializer.
|
|
|
|
// Emit a warning just in case the author intended a variable definition.
|
2008-10-18 07:23:35 +08:00
|
|
|
if (warnIfAmbiguous)
|
2008-11-18 15:50:21 +08:00
|
|
|
Diag(Tok, diag::warn_parens_disambiguated_as_function_decl)
|
|
|
|
<< SourceRange(Tok.getLocation(), TPLoc);
|
2008-10-06 02:52:21 +08:00
|
|
|
return true;
|
2008-10-16 07:21:32 +08:00
|
|
|
}
|
2008-10-06 02:52:21 +08:00
|
|
|
|
|
|
|
return TPR == TPResult::True();
|
2008-10-05 08:06:24 +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:
|
|
|
|
/// decl-specifier-seq declarator
|
|
|
|
/// decl-specifier-seq declarator '=' assignment-expression
|
|
|
|
/// decl-specifier-seq abstract-declarator[opt]
|
|
|
|
/// decl-specifier-seq abstract-declarator[opt] '=' assignment-expression
|
|
|
|
///
|
2008-10-06 02:52:21 +08:00
|
|
|
Parser::TPResult Parser::TryParseParameterDeclarationClause() {
|
2008-10-05 08:06:24 +08:00
|
|
|
|
|
|
|
if (Tok.is(tok::r_paren))
|
2008-10-06 02:52:21 +08:00
|
|
|
return TPResult::True();
|
2008-10-05 08:06:24 +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();
|
2008-10-06 02:52:21 +08:00
|
|
|
return TPResult::True(); // '...' is a sign of a function declarator.
|
2008-10-05 08:06:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// decl-specifier-seq
|
2008-10-06 02:52:21 +08:00
|
|
|
TPResult TPR = TryParseDeclarationSpecifier();
|
|
|
|
if (TPR != TPResult::Ambiguous())
|
2008-10-05 08:06:24 +08:00
|
|
|
return TPR;
|
|
|
|
|
|
|
|
// declarator
|
|
|
|
// abstract-declarator[opt]
|
|
|
|
TPR = TryParseDeclarator(true/*mayBeAbstract*/);
|
2008-10-06 02:52:21 +08:00
|
|
|
if (TPR != TPResult::Ambiguous())
|
2008-10-05 08:06:24 +08:00
|
|
|
return TPR;
|
|
|
|
|
|
|
|
if (Tok.is(tok::equal)) {
|
|
|
|
// '=' assignment-expression
|
|
|
|
// Parse through assignment-expression.
|
|
|
|
tok::TokenKind StopToks[3] ={ tok::comma, tok::ellipsis, tok::r_paren };
|
|
|
|
if (!SkipUntil(StopToks, 3, true/*StopAtSemi*/, true/*DontConsume*/))
|
2008-10-06 02:52:21 +08:00
|
|
|
return TPResult::Error();
|
2008-10-05 08:06:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Tok.is(tok::ellipsis)) {
|
|
|
|
ConsumeToken();
|
2008-10-06 02:52:21 +08:00
|
|
|
return TPResult::True(); // '...' is a sign of a function declarator.
|
2008-10-05 08:06:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Tok.isNot(tok::comma))
|
|
|
|
break;
|
|
|
|
ConsumeToken(); // the comma.
|
|
|
|
}
|
|
|
|
|
2008-10-06 02:52:21 +08:00
|
|
|
return TPResult::Ambiguous();
|
2008-10-05 08:06:24 +08:00
|
|
|
}
|
|
|
|
|
2008-10-06 07:15: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
|
|
|
|
/// return TPResult::Ambiguous(), otherwise it will return either False() or
|
|
|
|
/// Error().
|
2008-10-05 08:06:24 +08:00
|
|
|
///
|
|
|
|
/// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
|
|
|
|
/// exception-specification[opt]
|
|
|
|
///
|
|
|
|
/// exception-specification:
|
|
|
|
/// 'throw' '(' type-id-list[opt] ')'
|
|
|
|
///
|
2008-10-06 02:52:21 +08:00
|
|
|
Parser::TPResult Parser::TryParseFunctionDeclarator() {
|
2008-10-06 07:15:41 +08:00
|
|
|
|
|
|
|
// The '(' is already parsed.
|
|
|
|
|
|
|
|
TPResult TPR = TryParseParameterDeclarationClause();
|
|
|
|
if (TPR == TPResult::Ambiguous() && Tok.isNot(tok::r_paren))
|
|
|
|
TPR = TPResult::False();
|
|
|
|
|
|
|
|
if (TPR == TPResult::False() || TPR == TPResult::Error())
|
|
|
|
return TPR;
|
|
|
|
|
2008-10-05 08:06:24 +08:00
|
|
|
// Parse through the parens.
|
|
|
|
if (!SkipUntil(tok::r_paren))
|
2008-10-06 02:52:21 +08:00
|
|
|
return TPResult::Error();
|
2008-10-05 08:06:24 +08:00
|
|
|
|
|
|
|
// cv-qualifier-seq
|
|
|
|
while (Tok.is(tok::kw_const) ||
|
|
|
|
Tok.is(tok::kw_volatile) ||
|
|
|
|
Tok.is(tok::kw_restrict) )
|
|
|
|
ConsumeToken();
|
|
|
|
|
|
|
|
// exception-specification
|
|
|
|
if (Tok.is(tok::kw_throw)) {
|
|
|
|
ConsumeToken();
|
|
|
|
if (Tok.isNot(tok::l_paren))
|
2008-10-06 02:52:21 +08:00
|
|
|
return TPResult::Error();
|
2008-10-05 08:06:24 +08:00
|
|
|
|
|
|
|
// Parse through the parens after 'throw'.
|
|
|
|
ConsumeParen();
|
|
|
|
if (!SkipUntil(tok::r_paren))
|
2008-10-06 02:52:21 +08:00
|
|
|
return TPResult::Error();
|
2008-10-05 08:06:24 +08:00
|
|
|
}
|
|
|
|
|
2008-10-06 02:52:21 +08:00
|
|
|
return TPResult::Ambiguous();
|
2008-10-05 08:06:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// '[' constant-expression[opt] ']'
|
|
|
|
///
|
2008-10-06 02:52:21 +08:00
|
|
|
Parser::TPResult Parser::TryParseBracketDeclarator() {
|
2008-10-05 08:06:24 +08:00
|
|
|
ConsumeBracket();
|
|
|
|
if (!SkipUntil(tok::r_square))
|
2008-10-06 02:52:21 +08:00
|
|
|
return TPResult::Error();
|
2008-10-05 08:06:24 +08:00
|
|
|
|
2008-10-06 02:52:21 +08:00
|
|
|
return TPResult::Ambiguous();
|
2008-10-05 08:06:24 +08:00
|
|
|
}
|