2012-12-18 22:30:41 +08:00
|
|
|
//===--- ParseTentative.cpp - Ambiguity Resolution Parsing ----------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2012-12-18 22:30:41 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// 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] ';'
|
2016-07-23 07:36:59 +08:00
|
|
|
/// decl-specifier-seq ref-qualifier[opt] '[' identifier-list ']'
|
|
|
|
/// brace-or-equal-initializer ';' [C++17]
|
2012-12-18 22:30:41 +08:00
|
|
|
///
|
|
|
|
/// (if AllowForRangeDecl specified)
|
|
|
|
/// for ( for-range-declaration : for-range-initializer ) statement
|
2016-07-23 07:36:59 +08:00
|
|
|
///
|
2018-07-31 03:24:48 +08:00
|
|
|
/// for-range-declaration:
|
2016-07-23 07:36:59 +08:00
|
|
|
/// decl-specifier-seq declarator
|
|
|
|
/// decl-specifier-seq ref-qualifier[opt] '[' identifier-list ']'
|
2018-07-31 03:24:48 +08:00
|
|
|
///
|
2016-07-23 07:36:59 +08:00
|
|
|
/// In any of the above cases there can be a preceding attribute-specifier-seq,
|
|
|
|
/// but the caller is expected to handle that.
|
2012-12-18 22:30:41 +08:00
|
|
|
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...
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2016-06-30 05:06:51 +08:00
|
|
|
{
|
|
|
|
RevertingTentativeParsingAction PA(*this);
|
|
|
|
TPR = TryParseSimpleDeclaration(AllowForRangeDecl);
|
|
|
|
}
|
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)
|
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;
|
|
|
|
}
|
Fix clang -Wimplicit-fallthrough warnings across llvm, NFC
This patch should not introduce any behavior changes. It consists of
mostly one of two changes:
1. Replacing fall through comments with the LLVM_FALLTHROUGH macro
2. Inserting 'break' before falling through into a case block consisting
of only 'break'.
We were already using this warning with GCC, but its warning behaves
slightly differently. In this patch, the following differences are
relevant:
1. GCC recognizes comments that say "fall through" as annotations, clang
doesn't
2. GCC doesn't warn on "case N: foo(); default: break;", clang does
3. GCC doesn't warn when the case contains a switch, but falls through
the outer case.
I will enable the warning separately in a follow-up patch so that it can
be cleanly reverted if necessary.
Reviewers: alexfh, rsmith, lattner, rtrieu, EricWF, bollu
Differential Revision: https://reviews.llvm.org/D53950
llvm-svn: 345882
2018-11-02 03:54:45 +08:00
|
|
|
LLVM_FALLTHROUGH;
|
2013-09-13 07:28:08 +08:00
|
|
|
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.
|
2020-02-24 20:59:26 +08:00
|
|
|
if (!TrySkipAttributes())
|
|
|
|
return TPResult::Error;
|
2013-09-13 07:28:08 +08:00
|
|
|
|
2020-01-16 10:37:32 +08:00
|
|
|
if (TryAnnotateOptionalCXXScopeToken())
|
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))
|
2017-05-19 03:21:48 +08:00
|
|
|
ConsumeAnnotationToken();
|
|
|
|
if (Tok.is(tok::identifier))
|
2013-09-13 07:28:08 +08:00
|
|
|
ConsumeToken();
|
2017-05-19 03:21:48 +08:00
|
|
|
else if (Tok.is(tok::annot_template_id))
|
|
|
|
ConsumeAnnotationToken();
|
|
|
|
else
|
2014-05-16 09:56:53 +08:00
|
|
|
return TPResult::Error;
|
2013-09-13 07:28:08 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case tok::annot_cxxscope:
|
2017-05-19 03:21:48 +08:00
|
|
|
ConsumeAnnotationToken();
|
Fix clang -Wimplicit-fallthrough warnings across llvm, NFC
This patch should not introduce any behavior changes. It consists of
mostly one of two changes:
1. Replacing fall through comments with the LLVM_FALLTHROUGH macro
2. Inserting 'break' before falling through into a case block consisting
of only 'break'.
We were already using this warning with GCC, but its warning behaves
slightly differently. In this patch, the following differences are
relevant:
1. GCC recognizes comments that say "fall through" as annotations, clang
doesn't
2. GCC doesn't warn on "case N: foo(); default: break;", clang does
3. GCC doesn't warn when the case contains a switch, but falls through
the outer case.
I will enable the warning separately in a follow-up patch so that it can
be cleanly reverted if necessary.
Reviewers: alexfh, rsmith, lattner, rtrieu, EricWF, bollu
Differential Revision: https://reviews.llvm.org/D53950
llvm-svn: 345882
2018-11-02 03:54:45 +08:00
|
|
|
LLVM_FALLTHROUGH;
|
2013-09-13 07:28:08 +08:00
|
|
|
default:
|
2017-05-19 03:21:48 +08:00
|
|
|
ConsumeAnyToken();
|
2013-09-13 07:28:08 +08:00
|
|
|
|
2018-10-31 04:31:30 +08:00
|
|
|
if (getLangOpts().ObjC && Tok.is(tok::less))
|
2013-09-13 07:28:08 +08:00
|
|
|
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
|
2018-07-31 03:24:48 +08:00
|
|
|
/// for-range-declaration:
|
2012-12-18 22:30:41 +08:00
|
|
|
/// 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
|
|
|
}
|
|
|
|
|
2016-06-30 05:17:59 +08:00
|
|
|
struct Parser::ConditionDeclarationOrInitStatementState {
|
|
|
|
Parser &P;
|
|
|
|
bool CanBeExpression = true;
|
|
|
|
bool CanBeCondition = true;
|
|
|
|
bool CanBeInitStatement;
|
2018-09-29 02:44:09 +08:00
|
|
|
bool CanBeForRangeDecl;
|
2016-06-30 05:17:59 +08:00
|
|
|
|
2018-09-29 02:44:09 +08:00
|
|
|
ConditionDeclarationOrInitStatementState(Parser &P, bool CanBeInitStatement,
|
|
|
|
bool CanBeForRangeDecl)
|
|
|
|
: P(P), CanBeInitStatement(CanBeInitStatement),
|
|
|
|
CanBeForRangeDecl(CanBeForRangeDecl) {}
|
|
|
|
|
|
|
|
bool resolved() {
|
|
|
|
return CanBeExpression + CanBeCondition + CanBeInitStatement +
|
|
|
|
CanBeForRangeDecl < 2;
|
|
|
|
}
|
2016-06-30 05:17:59 +08:00
|
|
|
|
|
|
|
void markNotExpression() {
|
|
|
|
CanBeExpression = false;
|
|
|
|
|
2018-09-29 02:44:09 +08:00
|
|
|
if (!resolved()) {
|
2016-06-30 05:17:59 +08:00
|
|
|
// FIXME: Unify the parsing codepaths for condition variables and
|
|
|
|
// simple-declarations so that we don't need to eagerly figure out which
|
|
|
|
// kind we have here. (Just parse init-declarators until we reach a
|
|
|
|
// semicolon or right paren.)
|
|
|
|
RevertingTentativeParsingAction PA(P);
|
2018-09-29 02:44:09 +08:00
|
|
|
if (CanBeForRangeDecl) {
|
|
|
|
// Skip until we hit a ')', ';', or a ':' with no matching '?'.
|
|
|
|
// The final case is a for range declaration, the rest are not.
|
|
|
|
while (true) {
|
|
|
|
unsigned QuestionColonDepth = 0;
|
|
|
|
P.SkipUntil({tok::r_paren, tok::semi, tok::question, tok::colon},
|
|
|
|
StopBeforeMatch);
|
|
|
|
if (P.Tok.is(tok::question))
|
|
|
|
++QuestionColonDepth;
|
|
|
|
else if (P.Tok.is(tok::colon)) {
|
|
|
|
if (QuestionColonDepth)
|
|
|
|
--QuestionColonDepth;
|
|
|
|
else {
|
|
|
|
CanBeCondition = CanBeInitStatement = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
CanBeForRangeDecl = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
P.ConsumeToken();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Just skip until we hit a ')' or ';'.
|
|
|
|
P.SkipUntil(tok::r_paren, tok::semi, StopBeforeMatch);
|
|
|
|
}
|
2016-06-30 05:17:59 +08:00
|
|
|
if (P.Tok.isNot(tok::r_paren))
|
2018-09-29 02:44:09 +08:00
|
|
|
CanBeCondition = CanBeForRangeDecl = false;
|
2016-06-30 05:17:59 +08:00
|
|
|
if (P.Tok.isNot(tok::semi))
|
|
|
|
CanBeInitStatement = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool markNotCondition() {
|
|
|
|
CanBeCondition = false;
|
2018-09-29 02:44:09 +08:00
|
|
|
return resolved();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool markNotForRangeDecl() {
|
|
|
|
CanBeForRangeDecl = false;
|
|
|
|
return resolved();
|
2016-06-30 05:17:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool update(TPResult IsDecl) {
|
|
|
|
switch (IsDecl) {
|
|
|
|
case TPResult::True:
|
|
|
|
markNotExpression();
|
2018-09-29 02:44:09 +08:00
|
|
|
assert(resolved() && "can't continue after tentative parsing bails out");
|
|
|
|
break;
|
2016-06-30 05:17:59 +08:00
|
|
|
case TPResult::False:
|
2018-09-29 02:44:09 +08:00
|
|
|
CanBeCondition = CanBeInitStatement = CanBeForRangeDecl = false;
|
|
|
|
break;
|
2016-06-30 05:17:59 +08:00
|
|
|
case TPResult::Ambiguous:
|
2018-09-29 02:44:09 +08:00
|
|
|
break;
|
2016-06-30 05:17:59 +08:00
|
|
|
case TPResult::Error:
|
2018-09-29 02:44:09 +08:00
|
|
|
CanBeExpression = CanBeCondition = CanBeInitStatement =
|
|
|
|
CanBeForRangeDecl = false;
|
|
|
|
break;
|
2016-06-30 05:17:59 +08:00
|
|
|
}
|
2018-09-29 02:44:09 +08:00
|
|
|
return resolved();
|
2016-06-30 05:17:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ConditionOrInitStatement result() const {
|
2018-09-29 02:44:09 +08:00
|
|
|
assert(CanBeExpression + CanBeCondition + CanBeInitStatement +
|
|
|
|
CanBeForRangeDecl < 2 &&
|
2016-06-30 05:17:59 +08:00
|
|
|
"result called but not yet resolved");
|
|
|
|
if (CanBeExpression)
|
|
|
|
return ConditionOrInitStatement::Expression;
|
|
|
|
if (CanBeCondition)
|
|
|
|
return ConditionOrInitStatement::ConditionDecl;
|
|
|
|
if (CanBeInitStatement)
|
|
|
|
return ConditionOrInitStatement::InitStmtDecl;
|
2018-09-29 02:44:09 +08:00
|
|
|
if (CanBeForRangeDecl)
|
|
|
|
return ConditionOrInitStatement::ForRangeDecl;
|
2016-06-30 05:17:59 +08:00
|
|
|
return ConditionOrInitStatement::Error;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
Fix parsing of enum-base to follow C++11 rules.
Previously we implemented non-standard disambiguation rules to
distinguish an enum-base from a bit-field but otherwise treated a :
after an elaborated-enum-specifier as introducing an enum-base. That
misparses various examples (anywhere an elaborated-type-specifier can
appear followed by a colon, such as within a ternary operator or
_Generic).
We now implement the C++11 rules, with the old cases accepted as
extensions where that seemed reasonable. These amount to:
* an enum-base must always be accompanied by an enum definition (except
in a standalone declaration of the form 'enum E : T;')
* in a member-declaration, 'enum E :' always introduces an enum-base,
never a bit-field
* in a type-specifier (or similar context), 'enum E :' is not
permitted; the colon means whatever else it would mean in that
context.
Fixed underlying types for enums are also permitted in Objective-C and
under MS extensions, plus as a language extension in all other modes.
The behavior in ObjC and MS extensions modes is unchanged (but the
bit-field disambiguation is a bit better); remaining language modes
follow the C++11 rules.
Fixes PR45726, PR39979, PR19810, PR44941, and most of PR24297, plus C++
core issues 1514 and 1966.
2020-05-09 10:24:18 +08:00
|
|
|
bool Parser::isEnumBase(bool AllowSemi) {
|
|
|
|
assert(Tok.is(tok::colon) && "should be looking at the ':'");
|
|
|
|
|
|
|
|
RevertingTentativeParsingAction PA(*this);
|
2020-05-11 04:14:51 +08:00
|
|
|
// ':'
|
Fix parsing of enum-base to follow C++11 rules.
Previously we implemented non-standard disambiguation rules to
distinguish an enum-base from a bit-field but otherwise treated a :
after an elaborated-enum-specifier as introducing an enum-base. That
misparses various examples (anywhere an elaborated-type-specifier can
appear followed by a colon, such as within a ternary operator or
_Generic).
We now implement the C++11 rules, with the old cases accepted as
extensions where that seemed reasonable. These amount to:
* an enum-base must always be accompanied by an enum definition (except
in a standalone declaration of the form 'enum E : T;')
* in a member-declaration, 'enum E :' always introduces an enum-base,
never a bit-field
* in a type-specifier (or similar context), 'enum E :' is not
permitted; the colon means whatever else it would mean in that
context.
Fixed underlying types for enums are also permitted in Objective-C and
under MS extensions, plus as a language extension in all other modes.
The behavior in ObjC and MS extensions modes is unchanged (but the
bit-field disambiguation is a bit better); remaining language modes
follow the C++11 rules.
Fixes PR45726, PR39979, PR19810, PR44941, and most of PR24297, plus C++
core issues 1514 and 1966.
2020-05-09 10:24:18 +08:00
|
|
|
ConsumeToken();
|
|
|
|
|
2020-05-11 04:14:51 +08:00
|
|
|
// type-specifier-seq
|
Fix parsing of enum-base to follow C++11 rules.
Previously we implemented non-standard disambiguation rules to
distinguish an enum-base from a bit-field but otherwise treated a :
after an elaborated-enum-specifier as introducing an enum-base. That
misparses various examples (anywhere an elaborated-type-specifier can
appear followed by a colon, such as within a ternary operator or
_Generic).
We now implement the C++11 rules, with the old cases accepted as
extensions where that seemed reasonable. These amount to:
* an enum-base must always be accompanied by an enum definition (except
in a standalone declaration of the form 'enum E : T;')
* in a member-declaration, 'enum E :' always introduces an enum-base,
never a bit-field
* in a type-specifier (or similar context), 'enum E :' is not
permitted; the colon means whatever else it would mean in that
context.
Fixed underlying types for enums are also permitted in Objective-C and
under MS extensions, plus as a language extension in all other modes.
The behavior in ObjC and MS extensions modes is unchanged (but the
bit-field disambiguation is a bit better); remaining language modes
follow the C++11 rules.
Fixes PR45726, PR39979, PR19810, PR44941, and most of PR24297, plus C++
core issues 1514 and 1966.
2020-05-09 10:24:18 +08:00
|
|
|
bool InvalidAsDeclSpec = false;
|
2020-05-11 04:14:51 +08:00
|
|
|
// FIXME: We could disallow non-type decl-specifiers here, but it makes no
|
|
|
|
// difference: those specifiers are ill-formed regardless of the
|
|
|
|
// interpretation.
|
Fix parsing of enum-base to follow C++11 rules.
Previously we implemented non-standard disambiguation rules to
distinguish an enum-base from a bit-field but otherwise treated a :
after an elaborated-enum-specifier as introducing an enum-base. That
misparses various examples (anywhere an elaborated-type-specifier can
appear followed by a colon, such as within a ternary operator or
_Generic).
We now implement the C++11 rules, with the old cases accepted as
extensions where that seemed reasonable. These amount to:
* an enum-base must always be accompanied by an enum definition (except
in a standalone declaration of the form 'enum E : T;')
* in a member-declaration, 'enum E :' always introduces an enum-base,
never a bit-field
* in a type-specifier (or similar context), 'enum E :' is not
permitted; the colon means whatever else it would mean in that
context.
Fixed underlying types for enums are also permitted in Objective-C and
under MS extensions, plus as a language extension in all other modes.
The behavior in ObjC and MS extensions modes is unchanged (but the
bit-field disambiguation is a bit better); remaining language modes
follow the C++11 rules.
Fixes PR45726, PR39979, PR19810, PR44941, and most of PR24297, plus C++
core issues 1514 and 1966.
2020-05-09 10:24:18 +08:00
|
|
|
TPResult R = isCXXDeclarationSpecifier(/*BracedCastResult*/ TPResult::True,
|
|
|
|
&InvalidAsDeclSpec);
|
|
|
|
if (R == TPResult::Ambiguous) {
|
|
|
|
// We either have a decl-specifier followed by '(' or an undeclared
|
|
|
|
// identifier.
|
|
|
|
if (TryConsumeDeclarationSpecifier() == TPResult::Error)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// If we get to the end of the enum-base, we hit either a '{' or a ';'.
|
|
|
|
// Don't bother checking the enumerator-list.
|
2020-05-11 04:39:23 +08:00
|
|
|
if (Tok.is(tok::l_brace) || (AllowSemi && Tok.is(tok::semi)))
|
Fix parsing of enum-base to follow C++11 rules.
Previously we implemented non-standard disambiguation rules to
distinguish an enum-base from a bit-field but otherwise treated a :
after an elaborated-enum-specifier as introducing an enum-base. That
misparses various examples (anywhere an elaborated-type-specifier can
appear followed by a colon, such as within a ternary operator or
_Generic).
We now implement the C++11 rules, with the old cases accepted as
extensions where that seemed reasonable. These amount to:
* an enum-base must always be accompanied by an enum definition (except
in a standalone declaration of the form 'enum E : T;')
* in a member-declaration, 'enum E :' always introduces an enum-base,
never a bit-field
* in a type-specifier (or similar context), 'enum E :' is not
permitted; the colon means whatever else it would mean in that
context.
Fixed underlying types for enums are also permitted in Objective-C and
under MS extensions, plus as a language extension in all other modes.
The behavior in ObjC and MS extensions modes is unchanged (but the
bit-field disambiguation is a bit better); remaining language modes
follow the C++11 rules.
Fixes PR45726, PR39979, PR19810, PR44941, and most of PR24297, plus C++
core issues 1514 and 1966.
2020-05-09 10:24:18 +08:00
|
|
|
return true;
|
|
|
|
|
|
|
|
// A second decl-specifier unambiguously indicatges an enum-base.
|
|
|
|
R = isCXXDeclarationSpecifier(TPResult::True, &InvalidAsDeclSpec);
|
|
|
|
}
|
|
|
|
|
|
|
|
return R != TPResult::False;
|
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Disambiguates between a declaration in a condition, a
|
2016-06-30 05:17:59 +08:00
|
|
|
/// simple-declaration in an init-statement, and an expression for
|
|
|
|
/// a condition of a if/switch statement.
|
2012-12-18 22:30:41 +08:00
|
|
|
///
|
|
|
|
/// 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
|
2016-06-30 05:17:59 +08:00
|
|
|
/// simple-declaration:
|
|
|
|
/// decl-specifier-seq init-declarator-list[opt] ';'
|
2012-12-18 22:30:41 +08:00
|
|
|
///
|
2016-06-30 05:17:59 +08:00
|
|
|
/// Note that, unlike isCXXSimpleDeclaration, we must disambiguate all the way
|
|
|
|
/// to the ';' to disambiguate cases like 'int(x))' (an expression) from
|
|
|
|
/// 'int(x);' (a simple-declaration in an init-statement).
|
|
|
|
Parser::ConditionOrInitStatement
|
2018-09-29 02:44:09 +08:00
|
|
|
Parser::isCXXConditionDeclarationOrInitStatement(bool CanBeInitStatement,
|
|
|
|
bool CanBeForRangeDecl) {
|
|
|
|
ConditionDeclarationOrInitStatementState State(*this, CanBeInitStatement,
|
|
|
|
CanBeForRangeDecl);
|
2012-12-18 22:30:41 +08:00
|
|
|
|
2016-06-30 05:17:59 +08:00
|
|
|
if (State.update(isCXXDeclarationSpecifier()))
|
|
|
|
return State.result();
|
2012-12-18 22:30:41 +08:00
|
|
|
|
2016-06-30 05:17:59 +08:00
|
|
|
// It might be a declaration; we need tentative parsing.
|
2016-06-30 05:06:51 +08:00
|
|
|
RevertingTentativeParsingAction PA(*this);
|
2012-12-18 22:30:41 +08:00
|
|
|
|
2016-06-30 05:17:59 +08:00
|
|
|
// FIXME: A tag definition unambiguously tells us this is an init-statement.
|
|
|
|
if (State.update(TryConsumeDeclarationSpecifier()))
|
|
|
|
return State.result();
|
2012-12-18 22:30:41 +08:00
|
|
|
assert(Tok.is(tok::l_paren) && "Expected '('");
|
|
|
|
|
2016-06-30 05:17:59 +08:00
|
|
|
while (true) {
|
|
|
|
// Consume a declarator.
|
|
|
|
if (State.update(TryParseDeclarator(false/*mayBeAbstract*/)))
|
|
|
|
return State.result();
|
|
|
|
|
|
|
|
// Attributes, asm label, or an initializer imply this is not an expression.
|
|
|
|
// FIXME: Disambiguate properly after an = instead of assuming that it's a
|
|
|
|
// valid declaration.
|
|
|
|
if (Tok.isOneOf(tok::equal, tok::kw_asm, tok::kw___attribute) ||
|
|
|
|
(getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace))) {
|
|
|
|
State.markNotExpression();
|
|
|
|
return State.result();
|
|
|
|
}
|
2012-12-18 22:30:41 +08:00
|
|
|
|
2018-09-29 02:44:09 +08:00
|
|
|
// A colon here identifies a for-range declaration.
|
|
|
|
if (State.CanBeForRangeDecl && Tok.is(tok::colon))
|
|
|
|
return ConditionOrInitStatement::ForRangeDecl;
|
|
|
|
|
2016-06-30 05:17:59 +08:00
|
|
|
// At this point, it can't be a condition any more, because a condition
|
|
|
|
// must have a brace-or-equal-initializer.
|
|
|
|
if (State.markNotCondition())
|
|
|
|
return State.result();
|
2012-12-18 22:30:41 +08:00
|
|
|
|
2018-09-29 02:44:09 +08:00
|
|
|
// Likewise, it can't be a for-range declaration any more.
|
|
|
|
if (State.markNotForRangeDecl())
|
|
|
|
return State.result();
|
|
|
|
|
2016-06-30 05:17:59 +08:00
|
|
|
// A parenthesized initializer could be part of an expression or a
|
|
|
|
// simple-declaration.
|
|
|
|
if (Tok.is(tok::l_paren)) {
|
|
|
|
ConsumeParen();
|
|
|
|
SkipUntil(tok::r_paren, StopAtSemi);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!TryConsumeToken(tok::comma))
|
|
|
|
break;
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
|
2016-06-30 05:17:59 +08:00
|
|
|
// We reached the end. If it can now be some kind of decl, then it is.
|
|
|
|
if (State.CanBeCondition && Tok.is(tok::r_paren))
|
|
|
|
return ConditionOrInitStatement::ConditionDecl;
|
|
|
|
else if (State.CanBeInitStatement && Tok.is(tok::semi))
|
|
|
|
return ConditionOrInitStatement::InitStmtDecl;
|
|
|
|
else
|
|
|
|
return ConditionOrInitStatement::Expression;
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Determine whether the next set of tokens contains a type-id.
|
2012-12-18 22:30:41 +08:00
|
|
|
///
|
|
|
|
/// 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
|
2017-05-20 08:21:55 +08:00
|
|
|
/// hit a '>', '>>' (in C++0x), or ','; or, in C++0x, an ellipsis immediately
|
|
|
|
/// preceding such. 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.
|
2012-12-18 22:30:41 +08:00
|
|
|
///
|
|
|
|
/// 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...
|
|
|
|
|
2016-06-30 05:06:51 +08:00
|
|
|
RevertingTentativeParsingAction PA(*this);
|
2012-12-18 22:30:41 +08:00
|
|
|
|
|
|
|
// 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
|
2017-05-20 08:21:55 +08:00
|
|
|
// ','; or, in C++0x, an ellipsis immediately preceding such, this
|
|
|
|
// is a type-id. Otherwise, it's an expression.
|
2012-12-18 22:30:41 +08:00
|
|
|
} else if (Context == TypeIdAsTemplateArgument &&
|
2015-06-18 18:59:26 +08:00
|
|
|
(Tok.isOneOf(tok::greater, tok::comma) ||
|
2017-05-20 08:21:55 +08:00
|
|
|
(getLangOpts().CPlusPlus11 &&
|
2019-05-08 08:52:33 +08:00
|
|
|
(Tok.isOneOf(tok::greatergreater,
|
|
|
|
tok::greatergreatergreater) ||
|
2017-05-20 08:21:55 +08:00
|
|
|
(Tok.is(tok::ellipsis) &&
|
|
|
|
NextToken().isOneOf(tok::greater, tok::greatergreater,
|
2019-05-08 08:52:33 +08:00
|
|
|
tok::greatergreatergreater,
|
2017-05-20 08:21:55 +08:00
|
|
|
tok::comma)))))) {
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Returns true if this is a C++11 attribute-specifier. Per
|
2012-12-18 22:30:41 +08:00
|
|
|
/// 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.
|
2018-10-31 04:31:30 +08:00
|
|
|
if (!Disambiguate && !getLangOpts().ObjC)
|
2012-12-18 22:30:41 +08:00
|
|
|
return CAK_AttributeSpecifier;
|
|
|
|
|
2019-05-21 02:01:54 +08:00
|
|
|
// '[[using ns: ...]]' is an attribute.
|
|
|
|
if (GetLookAheadToken(2).is(tok::kw_using))
|
|
|
|
return CAK_AttributeSpecifier;
|
|
|
|
|
2016-06-30 05:06:51 +08:00
|
|
|
RevertingTentativeParsingAction PA(*this);
|
2012-12-18 22:30:41 +08:00
|
|
|
|
|
|
|
// Opening brackets were checked for above.
|
|
|
|
ConsumeBracket();
|
|
|
|
|
2018-10-31 04:31:30 +08:00
|
|
|
if (!getLangOpts().ObjC) {
|
2012-12-18 22:30:41 +08:00
|
|
|
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);
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
2019-05-21 02:01:54 +08:00
|
|
|
// Check to see if this is a lambda-expression.
|
2012-12-18 22:30:41 +08:00
|
|
|
// FIXME: If this disambiguation is too slow, fold the tentative lambda parse
|
|
|
|
// into the tentative attribute parse below.
|
2019-05-21 02:01:54 +08:00
|
|
|
{
|
|
|
|
RevertingTentativeParsingAction LambdaTPA(*this);
|
|
|
|
LambdaIntroducer Intro;
|
|
|
|
LambdaIntroducerTentativeParse Tentative;
|
|
|
|
if (ParseLambdaIntroducer(Intro, &Tentative)) {
|
|
|
|
// We hit a hard error after deciding this was not an attribute.
|
|
|
|
// FIXME: Don't parse and annotate expressions when disambiguating
|
|
|
|
// against an attribute.
|
|
|
|
return CAK_NotAttributeSpecifier;
|
|
|
|
}
|
2012-12-18 22:30:41 +08:00
|
|
|
|
2019-05-21 02:01:54 +08:00
|
|
|
switch (Tentative) {
|
|
|
|
case LambdaIntroducerTentativeParse::MessageSend:
|
|
|
|
// Case 3: The inner construct is definitely a message send, so the
|
|
|
|
// outer construct is definitely not an attribute.
|
2012-12-18 22:30:41 +08:00
|
|
|
return CAK_NotAttributeSpecifier;
|
|
|
|
|
2019-05-21 02:01:54 +08:00
|
|
|
case LambdaIntroducerTentativeParse::Success:
|
|
|
|
case LambdaIntroducerTentativeParse::Incomplete:
|
|
|
|
// This is a lambda-introducer or attribute-specifier.
|
|
|
|
if (Tok.is(tok::r_square))
|
|
|
|
// 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;
|
|
|
|
|
|
|
|
case LambdaIntroducerTentativeParse::Invalid:
|
|
|
|
// No idea what this is; we couldn't parse it as a lambda-introducer.
|
|
|
|
// Might still be an attribute-specifier or a message send.
|
|
|
|
break;
|
|
|
|
}
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IsAttribute)
|
|
|
|
// Case 1: C++11 statement attribute.
|
|
|
|
return CAK_AttributeSpecifier;
|
|
|
|
|
|
|
|
// Case 3: Message send.
|
|
|
|
return CAK_NotAttributeSpecifier;
|
|
|
|
}
|
|
|
|
|
2020-02-24 20:59:26 +08:00
|
|
|
bool Parser::TrySkipAttributes() {
|
|
|
|
while (Tok.isOneOf(tok::l_square, tok::kw___attribute, tok::kw___declspec,
|
|
|
|
tok::kw_alignas)) {
|
|
|
|
if (Tok.is(tok::l_square)) {
|
|
|
|
ConsumeBracket();
|
|
|
|
if (Tok.isNot(tok::l_square))
|
|
|
|
return false;
|
|
|
|
ConsumeBracket();
|
|
|
|
if (!SkipUntil(tok::r_square) || Tok.isNot(tok::r_square))
|
|
|
|
return false;
|
|
|
|
// Note that explicitly checking for `[[` and `]]` allows to fail as
|
|
|
|
// expected in the case of the Objective-C message send syntax.
|
|
|
|
ConsumeBracket();
|
|
|
|
} else {
|
|
|
|
ConsumeToken();
|
|
|
|
if (Tok.isNot(tok::l_paren))
|
|
|
|
return false;
|
|
|
|
ConsumeParen();
|
|
|
|
if (!SkipUntil(tok::r_paren))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-09-13 07:28:08 +08:00
|
|
|
Parser::TPResult Parser::TryParsePtrOperatorSeq() {
|
|
|
|
while (true) {
|
2020-01-16 10:37:32 +08:00
|
|
|
if (TryAnnotateOptionalCXXScopeToken(true))
|
|
|
|
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
|
2017-05-19 03:21:48 +08:00
|
|
|
ConsumeAnyToken();
|
2020-02-24 20:59:26 +08:00
|
|
|
|
|
|
|
// Skip attributes.
|
|
|
|
if (!TrySkipAttributes())
|
|
|
|
return TPResult::Error;
|
|
|
|
|
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,
|
2020-12-07 22:14:25 +08:00
|
|
|
tok::kw__Nullable_result, tok::kw__Null_unspecified,
|
|
|
|
tok::kw__Atomic))
|
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]
|
2017-05-20 08:21:55 +08:00
|
|
|
/// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
|
2012-12-18 22:30:41 +08:00
|
|
|
/// exception-specification[opt]
|
|
|
|
/// direct-abstract-declarator[opt] '[' constant-expression[opt] ']'
|
|
|
|
/// '(' abstract-declarator ')'
|
2017-05-20 08:21:55 +08:00
|
|
|
/// [C++0x] ...
|
2012-12-18 22:30:41 +08:00
|
|
|
///
|
|
|
|
/// 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,
|
2018-02-03 06:24:54 +08:00
|
|
|
bool mayHaveIdentifier,
|
|
|
|
bool mayHaveDirectInit) {
|
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
|
2020-03-28 08:08:26 +08:00
|
|
|
if (Tok.is(tok::annot_cxxscope)) {
|
|
|
|
CXXScopeSpec SS;
|
|
|
|
Actions.RestoreNestedNameSpecifierAnnotation(
|
|
|
|
Tok.getAnnotationValue(), Tok.getAnnotationRange(), SS);
|
|
|
|
if (SS.isInvalid())
|
|
|
|
return TPResult::Error;
|
2017-05-19 03:21:48 +08:00
|
|
|
ConsumeAnnotationToken();
|
2020-03-28 08:08:26 +08:00
|
|
|
} else if (Tok.is(tok::identifier)) {
|
2012-12-18 22:30:41 +08:00
|
|
|
TentativelyDeclaredIdentifiers.push_back(Tok.getIdentifierInfo());
|
2020-03-28 08:08:26 +08:00
|
|
|
}
|
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,
|
2016-11-03 02:29:35 +08:00
|
|
|
tok::kw___regcall, tok::kw___vectorcall))
|
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
|
|
|
}
|
|
|
|
|
2018-02-03 06:24:54 +08:00
|
|
|
if (mayHaveDirectInit)
|
|
|
|
return TPResult::Ambiguous;
|
|
|
|
|
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
|
|
|
|
|
|
|
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();
|
2020-01-09 21:07:51 +08:00
|
|
|
} else if (Tok.is(tok::kw_requires)) {
|
|
|
|
// declarator requires-clause
|
|
|
|
// A requires clause indicates a function declaration.
|
|
|
|
TPR = TPResult::True;
|
2012-12-18 22:30:41 +08:00
|
|
|
} 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
|
|
|
}
|
|
|
|
|
|
|
|
bool Parser::isTentativelyDeclared(IdentifierInfo *II) {
|
|
|
|
return std::find(TentativelyDeclaredIdentifiers.begin(),
|
|
|
|
TentativelyDeclaredIdentifiers.end(), II)
|
|
|
|
!= TentativelyDeclaredIdentifiers.end();
|
|
|
|
}
|
|
|
|
|
2014-11-05 08:09:29 +08:00
|
|
|
namespace {
|
2019-03-26 01:08:51 +08:00
|
|
|
class TentativeParseCCC final : public CorrectionCandidateCallback {
|
2014-11-05 08:09:29 +08:00
|
|
|
public:
|
|
|
|
TentativeParseCCC(const Token &Next) {
|
|
|
|
WantRemainingKeywords = false;
|
2020-07-13 18:05:09 +08:00
|
|
|
WantTypeSpecifiers =
|
|
|
|
Next.isOneOf(tok::l_paren, tok::r_paren, tok::greater, tok::l_brace,
|
|
|
|
tok::identifier, tok::comma);
|
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() &&
|
2018-10-21 01:53:42 +08:00
|
|
|
llvm::all_of(Candidate,
|
|
|
|
[](NamedDecl *ND) { return ND->isCXXInstanceMember(); }))
|
2014-11-05 08:09:29 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return CorrectionCandidateCallback::ValidateCandidate(Candidate);
|
|
|
|
}
|
2019-03-26 01:08:51 +08:00
|
|
|
|
|
|
|
std::unique_ptr<CorrectionCandidateCallback> clone() override {
|
2019-08-15 07:04:18 +08:00
|
|
|
return std::make_unique<TentativeParseCCC>(*this);
|
2019-03-26 01:08:51 +08:00
|
|
|
}
|
2014-11-05 08:09:29 +08:00
|
|
|
};
|
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.
|
|
|
|
///
|
2019-05-09 11:31:27 +08:00
|
|
|
/// If InvalidAsDeclSpec is not null, some cases that would be ill-formed as
|
|
|
|
/// declaration specifiers but possibly valid as some other kind of construct
|
|
|
|
/// return TPResult::Ambiguous instead of TPResult::False. When this happens,
|
|
|
|
/// the intent is to keep trying to disambiguate, on the basis that we might
|
|
|
|
/// find a better reason to treat this construct as a declaration later on.
|
|
|
|
/// When this happens and the name could possibly be valid in some other
|
|
|
|
/// syntactic context, *InvalidAsDeclSpec is set to 'true'. The current cases
|
|
|
|
/// that trigger this are:
|
|
|
|
///
|
|
|
|
/// * When parsing X::Y (with no 'typename') where X is dependent
|
|
|
|
/// * When parsing X<Y> where X is undeclared
|
2012-12-18 22:30:41 +08:00
|
|
|
///
|
|
|
|
/// decl-specifier:
|
|
|
|
/// storage-class-specifier
|
|
|
|
/// type-specifier
|
|
|
|
/// function-specifier
|
|
|
|
/// 'friend'
|
|
|
|
/// 'typedef'
|
2013-04-13 06:46:28 +08:00
|
|
|
/// [C++11] 'constexpr'
|
2019-06-14 16:56:20 +08:00
|
|
|
/// [C++20] 'consteval'
|
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,
|
2019-05-09 11:31:27 +08:00
|
|
|
bool *InvalidAsDeclSpec) {
|
2020-01-22 08:03:05 +08:00
|
|
|
auto IsPlaceholderSpecifier = [&] (TemplateIdAnnotation *TemplateId,
|
|
|
|
int Lookahead) {
|
|
|
|
// We have a placeholder-constraint (we check for 'auto' or 'decltype' to
|
|
|
|
// distinguish 'C<int>;' from 'C<int> auto c = 1;')
|
|
|
|
return TemplateId->Kind == TNK_Concept_template &&
|
|
|
|
GetLookAheadToken(Lookahead + 1).isOneOf(tok::kw_auto, tok::kw_decltype,
|
|
|
|
// If we have an identifier here, the user probably forgot the
|
|
|
|
// 'auto' in the placeholder constraint, e.g. 'C<int> x = 2;'
|
|
|
|
// This will be diagnosed nicely later, so disambiguate as a
|
|
|
|
// declaration.
|
|
|
|
tok::identifier);
|
|
|
|
};
|
2012-12-18 22:30:41 +08:00
|
|
|
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.
|
2018-10-31 04:31:30 +08:00
|
|
|
if (!getLangOpts().ObjC && 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.
|
2019-03-26 01:08:51 +08:00
|
|
|
TentativeParseCCC CCC(Next);
|
2019-10-15 05:53:03 +08:00
|
|
|
switch (TryAnnotateName(&CCC)) {
|
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:
|
2018-02-28 11:02:23 +08:00
|
|
|
// In C++17, this could be a type template for class template argument
|
|
|
|
// deduction. Try to form a type annotation for it. If we're in a
|
|
|
|
// template template argument, we'll undo this when checking the
|
|
|
|
// validity of the argument.
|
|
|
|
if (getLangOpts().CPlusPlus17) {
|
|
|
|
if (TryAnnotateTypeOrScopeToken())
|
|
|
|
return TPResult::Error;
|
|
|
|
if (Tok.isNot(tok::identifier))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-12-18 22:30:41 +08:00
|
|
|
// 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:
|
2019-05-09 11:31:27 +08:00
|
|
|
return InvalidAsDeclSpec ? 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.
|
2019-05-09 11:31:27 +08:00
|
|
|
return isCXXDeclarationSpecifier(BracedCastResult, InvalidAsDeclSpec);
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2019-05-09 11:31:27 +08:00
|
|
|
return isCXXDeclarationSpecifier(BracedCastResult, InvalidAsDeclSpec);
|
2012-12-18 22:30:41 +08:00
|
|
|
|
|
|
|
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;
|
Fix clang -Wimplicit-fallthrough warnings across llvm, NFC
This patch should not introduce any behavior changes. It consists of
mostly one of two changes:
1. Replacing fall through comments with the LLVM_FALLTHROUGH macro
2. Inserting 'break' before falling through into a case block consisting
of only 'break'.
We were already using this warning with GCC, but its warning behaves
slightly differently. In this patch, the following differences are
relevant:
1. GCC recognizes comments that say "fall through" as annotations, clang
doesn't
2. GCC doesn't warn on "case N: foo(); default: break;", clang does
3. GCC doesn't warn when the case contains a switch, but falls through
the outer case.
I will enable the warning separately in a follow-up patch so that it can
be cleanly reverted if necessary.
Reviewers: alexfh, rsmith, lattner, rtrieu, EricWF, bollu
Differential Revision: https://reviews.llvm.org/D53950
llvm-svn: 345882
2018-11-02 03:54:45 +08:00
|
|
|
LLVM_FALLTHROUGH;
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
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;
|
2019-05-09 11:31:27 +08:00
|
|
|
return isCXXDeclarationSpecifier(BracedCastResult, InvalidAsDeclSpec);
|
2012-12-18 22:30:41 +08:00
|
|
|
|
|
|
|
// decl-specifier:
|
|
|
|
// storage-class-specifier
|
|
|
|
// type-specifier
|
|
|
|
// function-specifier
|
|
|
|
// 'friend'
|
|
|
|
// 'typedef'
|
|
|
|
// 'constexpr'
|
|
|
|
case tok::kw_friend:
|
|
|
|
case tok::kw_typedef:
|
|
|
|
case tok::kw_constexpr:
|
2019-06-14 16:56:20 +08:00
|
|
|
case tok::kw_consteval:
|
2019-09-05 04:30:37 +08:00
|
|
|
case tok::kw_constinit:
|
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:
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2012-12-18 22:30:41 +08:00
|
|
|
// 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:
|
2019-03-28 19:47:14 +08:00
|
|
|
return TPResult::True;
|
|
|
|
|
2019-02-15 20:07:57 +08:00
|
|
|
// OpenCL address space qualifiers
|
2019-03-25 19:54:02 +08:00
|
|
|
case tok::kw_private:
|
2019-03-28 19:47:14 +08:00
|
|
|
if (!getLangOpts().OpenCL)
|
|
|
|
return TPResult::False;
|
|
|
|
LLVM_FALLTHROUGH;
|
2018-06-23 00:20:21 +08:00
|
|
|
case tok::kw___private:
|
|
|
|
case tok::kw___local:
|
|
|
|
case tok::kw___global:
|
|
|
|
case tok::kw___constant:
|
|
|
|
case tok::kw___generic:
|
2019-02-15 20:07:57 +08:00
|
|
|
// OpenCL access qualifiers
|
|
|
|
case tok::kw___read_only:
|
|
|
|
case tok::kw___write_only:
|
|
|
|
case tok::kw___read_write:
|
2019-05-22 21:12:20 +08:00
|
|
|
// OpenCL pipe
|
|
|
|
case tok::kw_pipe:
|
2012-12-18 22:30:41 +08:00
|
|
|
|
|
|
|
// 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:
|
2016-11-03 02:29:35 +08:00
|
|
|
case tok::kw___regcall:
|
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:
|
2020-12-07 22:14:25 +08:00
|
|
|
case tok::kw__Nullable_result:
|
2015-06-25 06:02:08 +08:00
|
|
|
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;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
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);
|
2019-05-09 11:31:27 +08:00
|
|
|
// If lookup for the template-name found nothing, don't assume we have a
|
|
|
|
// definitive disambiguation result yet.
|
2020-03-28 11:26:34 +08:00
|
|
|
if ((TemplateId->hasInvalidName() ||
|
|
|
|
TemplateId->Kind == TNK_Undeclared_template) &&
|
|
|
|
InvalidAsDeclSpec) {
|
2019-05-09 11:31:27 +08:00
|
|
|
// 'template-id(' can be a valid expression but not a valid decl spec if
|
|
|
|
// the template-name is not declared, but we don't consider this to be a
|
|
|
|
// definitive disambiguation. In any other context, it's an error either
|
|
|
|
// way.
|
|
|
|
*InvalidAsDeclSpec = NextToken().is(tok::l_paren);
|
|
|
|
return TPResult::Ambiguous;
|
|
|
|
}
|
2020-03-28 08:08:26 +08:00
|
|
|
if (TemplateId->hasInvalidName())
|
|
|
|
return TPResult::Error;
|
2020-01-22 08:03:05 +08:00
|
|
|
if (IsPlaceholderSpecifier(TemplateId, /*Lookahead=*/0))
|
|
|
|
return TPResult::True;
|
2012-12-18 22:30:41 +08:00
|
|
|
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;
|
2020-01-18 07:42:11 +08:00
|
|
|
AnnotateTemplateIdTokenAsType(SS);
|
2012-12-18 22:30:41 +08:00
|
|
|
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)) {
|
2020-01-22 08:03:05 +08:00
|
|
|
if (Tok.is(tok::annot_cxxscope) &&
|
|
|
|
NextToken().is(tok::annot_template_id)) {
|
|
|
|
TemplateIdAnnotation *TemplateId =
|
|
|
|
takeTemplateIdAnnotation(NextToken());
|
2020-03-28 11:26:34 +08:00
|
|
|
if (TemplateId->hasInvalidName()) {
|
|
|
|
if (InvalidAsDeclSpec) {
|
|
|
|
*InvalidAsDeclSpec = NextToken().is(tok::l_paren);
|
|
|
|
return TPResult::Ambiguous;
|
|
|
|
}
|
2020-03-28 08:08:26 +08:00
|
|
|
return TPResult::Error;
|
2020-03-28 11:26:34 +08:00
|
|
|
}
|
2020-01-22 08:03:05 +08:00
|
|
|
if (IsPlaceholderSpecifier(TemplateId, /*Lookahead=*/1))
|
|
|
|
return TPResult::True;
|
|
|
|
}
|
2012-12-18 22:30:41 +08:00
|
|
|
// 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()) {
|
2016-06-30 05:12:37 +08:00
|
|
|
RevertingTentativeParsingAction PA(*this);
|
2017-05-19 03:21:48 +08:00
|
|
|
ConsumeAnnotationToken();
|
2012-12-18 22:30:41 +08:00
|
|
|
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,
|
2019-05-09 11:31:27 +08:00
|
|
|
InvalidAsDeclSpec);
|
2012-12-18 22:30:41 +08:00
|
|
|
|
|
|
|
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
|
|
|
|
2019-05-09 11:31:27 +08:00
|
|
|
if (InvalidAsDeclSpec) {
|
2012-12-18 22:30:41 +08:00
|
|
|
// We can't tell whether this is a missing 'typename' or a valid
|
|
|
|
// expression.
|
2019-05-09 11:31:27 +08:00
|
|
|
*InvalidAsDeclSpec = true;
|
2014-05-16 09:56:53 +08:00
|
|
|
return TPResult::Ambiguous;
|
2019-02-26 10:22:17 +08:00
|
|
|
} else {
|
2019-05-09 11:31:27 +08:00
|
|
|
// In MS mode, if InvalidAsDeclSpec is not provided, and the tokens
|
2019-02-26 10:22:17 +08:00
|
|
|
// are or the form *) or &) *> or &> &&>, this can't be an expression.
|
|
|
|
// The typename must be missing.
|
|
|
|
if (getLangOpts().MSVCCompat) {
|
|
|
|
if (((Tok.is(tok::amp) || Tok.is(tok::star)) &&
|
|
|
|
(NextToken().is(tok::r_paren) ||
|
|
|
|
NextToken().is(tok::greater))) ||
|
|
|
|
(Tok.is(tok::ampamp) && NextToken().is(tok::greater)))
|
|
|
|
return TPResult::True;
|
|
|
|
}
|
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.
|
2019-10-15 05:53:03 +08:00
|
|
|
switch (TryAnnotateName()) {
|
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:
|
2018-02-28 11:02:23 +08:00
|
|
|
// In C++17, this could be a type template for class template
|
|
|
|
// argument deduction.
|
|
|
|
if (getLangOpts().CPlusPlus17) {
|
|
|
|
if (TryAnnotateTypeOrScopeToken())
|
|
|
|
return TPResult::Error;
|
|
|
|
if (Tok.isNot(tok::identifier))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-12-18 22:30:41 +08:00
|
|
|
// A bare type template-name which can't be a template template
|
|
|
|
// argument is an error, and was probably intended to be a type.
|
2018-02-28 11:02:23 +08:00
|
|
|
// In C++17, this could be class template argument deduction.
|
|
|
|
return (getLangOpts().CPlusPlus17 || GreaterThanIsOperator)
|
|
|
|
? TPResult::True
|
|
|
|
: TPResult::False;
|
2012-12-18 22:30:41 +08:00
|
|
|
case ANK_Unresolved:
|
2019-05-09 11:31:27 +08:00
|
|
|
return InvalidAsDeclSpec ? TPResult::Ambiguous : TPResult::False;
|
2012-12-18 22:30:41 +08:00
|
|
|
case ANK_Success:
|
2018-02-28 11:02:23 +08:00
|
|
|
break;
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
2018-02-28 11:02:23 +08:00
|
|
|
|
|
|
|
// Annotated it, check again.
|
|
|
|
assert(Tok.isNot(tok::annot_cxxscope) ||
|
|
|
|
NextToken().isNot(tok::identifier));
|
2019-05-09 11:31:27 +08:00
|
|
|
return isCXXDeclarationSpecifier(BracedCastResult, InvalidAsDeclSpec);
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
}
|
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.
|
2017-06-02 05:29:45 +08:00
|
|
|
LLVM_FALLTHROUGH;
|
2012-12-18 22:30:41 +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::annot_typename:
|
|
|
|
case_typename:
|
|
|
|
// In Objective-C, we might have a protocol-qualified type.
|
2018-10-31 04:31:30 +08:00
|
|
|
if (getLangOpts().ObjC && NextToken().is(tok::less)) {
|
2015-07-07 11:57:35 +08:00
|
|
|
// Tentatively parse the protocol qualifiers.
|
2016-06-30 05:06:51 +08:00
|
|
|
RevertingTentativeParsingAction PA(*this);
|
2017-05-19 03:21:48 +08:00
|
|
|
ConsumeAnyToken(); // The type token
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2012-12-18 22:30:41 +08:00
|
|
|
TPResult TPR = TryParseProtocolQualifiers();
|
|
|
|
bool isFollowedByParen = Tok.is(tok::l_paren);
|
|
|
|
bool isFollowedByBrace = Tok.is(tok::l_brace);
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2014-05-16 09:56:53 +08:00
|
|
|
if (TPR == TPResult::Error)
|
|
|
|
return TPResult::Error;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
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;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2014-05-16 09:56:53 +08:00
|
|
|
return TPResult::True;
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
2017-06-02 05:29:45 +08:00
|
|
|
LLVM_FALLTHROUGH;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2012-12-18 22:30:41 +08:00
|
|
|
case tok::kw_char:
|
|
|
|
case tok::kw_wchar_t:
|
2018-05-01 13:02:45 +08:00
|
|
|
case tok::kw_char8_t:
|
2012-12-18 22:30:41 +08:00
|
|
|
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:
|
[ARM] Add __bf16 as new Bfloat16 C Type
Summary:
This patch upstreams support for a new storage only bfloat16 C type.
This type is used to implement primitive support for bfloat16 data, in
line with the Bfloat16 extension of the Armv8.6-a architecture, as
detailed here:
https://community.arm.com/developer/ip-products/processors/b/processors-ip-blog/posts/arm-architecture-developments-armv8-6-a
The bfloat type, and its properties are specified in the Arm Architecture
Reference Manual:
https://developer.arm.com/docs/ddi0487/latest/arm-architecture-reference-manual-armv8-for-armv8-a-architecture-profile
In detail this patch:
- introduces an opaque, storage-only C-type __bf16, which introduces a new bfloat IR type.
This is part of a patch series, starting with command-line and Bfloat16
assembly support. The subsequent patches will upstream intrinsics
support for BFloat16, followed by Matrix Multiplication and the
remaining Virtualization features of the armv8.6-a architecture.
The following people contributed to this patch:
- Luke Cheeseman
- Momchil Velikov
- Alexandros Lamprineas
- Luke Geeson
- Simon Tatham
- Ties Stuij
Reviewers: SjoerdMeijer, rjmccall, rsmith, liutianle, RKSimon, craig.topper, jfb, LukeGeeson, fpetrogalli
Reviewed By: SjoerdMeijer
Subscribers: labrinea, majnemer, asmith, dexonsmith, kristof.beyls, arphaman, danielkiss, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D76077
2020-06-05 07:20:02 +08:00
|
|
|
case tok::kw___bf16:
|
2017-09-08 23:15:00 +08:00
|
|
|
case tok::kw__Float16:
|
2016-05-09 16:52:33 +08:00
|
|
|
case tok::kw___float128:
|
2012-12-18 22:30:41 +08:00
|
|
|
case tok::kw_void:
|
|
|
|
case tok::annot_decltype:
|
2019-02-15 20:07:57 +08:00
|
|
|
#define GENERIC_IMAGE_TYPE(ImgType, Id) case tok::kw_##ImgType##_t:
|
|
|
|
#include "clang/Basic/OpenCLImageTypes.def"
|
2012-12-18 22:30:41 +08:00
|
|
|
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;
|
2018-07-31 03:24:48 +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
|
|
|
|
2016-06-30 05:06:51 +08:00
|
|
|
RevertingTentativeParsingAction PA(*this);
|
2012-12-18 22:30:41 +08:00
|
|
|
|
|
|
|
TPResult TPR = TryParseTypeofSpecifier();
|
|
|
|
bool isFollowedByParen = Tok.is(tok::l_paren);
|
|
|
|
bool isFollowedByBrace = Tok.is(tok::l_brace);
|
|
|
|
|
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
|
|
|
|
2020-04-18 01:44:19 +08:00
|
|
|
case tok::kw__ExtInt: {
|
|
|
|
if (NextToken().isNot(tok::l_paren))
|
|
|
|
return TPResult::Error;
|
|
|
|
RevertingTentativeParsingAction PA(*this);
|
|
|
|
ConsumeToken();
|
|
|
|
ConsumeParen();
|
|
|
|
|
|
|
|
if (!SkipUntil(tok::r_paren, StopAtSemi))
|
|
|
|
return TPResult::Error;
|
|
|
|
|
|
|
|
if (Tok.is(tok::l_paren))
|
|
|
|
return TPResult::Ambiguous;
|
|
|
|
|
|
|
|
if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace))
|
|
|
|
return BracedCastResult;
|
|
|
|
|
|
|
|
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:
|
2018-05-01 13:02:45 +08:00
|
|
|
case tok::kw_char8_t:
|
2013-09-13 07:28:08 +08:00
|
|
|
case tok::kw_char16_t:
|
|
|
|
case tok::kw_char32_t:
|
|
|
|
case tok::kw_bool:
|
|
|
|
case tok::kw_short:
|
|
|
|
case tok::kw_int:
|
2020-04-18 01:44:19 +08:00
|
|
|
case tok::kw__ExtInt:
|
2013-09-13 07:28:08 +08:00
|
|
|
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:
|
[ARM] Add __bf16 as new Bfloat16 C Type
Summary:
This patch upstreams support for a new storage only bfloat16 C type.
This type is used to implement primitive support for bfloat16 data, in
line with the Bfloat16 extension of the Armv8.6-a architecture, as
detailed here:
https://community.arm.com/developer/ip-products/processors/b/processors-ip-blog/posts/arm-architecture-developments-armv8-6-a
The bfloat type, and its properties are specified in the Arm Architecture
Reference Manual:
https://developer.arm.com/docs/ddi0487/latest/arm-architecture-reference-manual-armv8-for-armv8-a-architecture-profile
In detail this patch:
- introduces an opaque, storage-only C-type __bf16, which introduces a new bfloat IR type.
This is part of a patch series, starting with command-line and Bfloat16
assembly support. The subsequent patches will upstream intrinsics
support for BFloat16, followed by Matrix Multiplication and the
remaining Virtualization features of the armv8.6-a architecture.
The following people contributed to this patch:
- Luke Cheeseman
- Momchil Velikov
- Alexandros Lamprineas
- Luke Geeson
- Simon Tatham
- Ties Stuij
Reviewers: SjoerdMeijer, rjmccall, rsmith, liutianle, RKSimon, craig.topper, jfb, LukeGeeson, fpetrogalli
Reviewed By: SjoerdMeijer
Subscribers: labrinea, majnemer, asmith, dexonsmith, kristof.beyls, arphaman, danielkiss, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D76077
2020-06-05 07:20:02 +08:00
|
|
|
case tok::kw___bf16:
|
2017-09-08 23:15:00 +08:00
|
|
|
case tok::kw__Float16:
|
2016-05-09 16:52:33 +08:00
|
|
|
case tok::kw___float128:
|
2013-09-13 07:28:08 +08:00
|
|
|
case tok::kw_void:
|
|
|
|
case tok::kw___unknown_anytype:
|
2015-11-11 10:02:15 +08:00
|
|
|
case tok::kw___auto_type:
|
2019-02-15 20:07:57 +08:00
|
|
|
#define GENERIC_IMAGE_TYPE(ImgType, Id) case tok::kw_##ImgType##_t:
|
|
|
|
#include "clang/Basic/OpenCLImageTypes.def"
|
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();
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2012-12-18 22:30:41 +08:00
|
|
|
if (Tok.is(tok::comma)) {
|
|
|
|
ConsumeToken();
|
|
|
|
continue;
|
|
|
|
}
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2012-12-18 22:30:41 +08:00
|
|
|
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);
|
2018-07-31 03:24:48 +08:00
|
|
|
|
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.
|
|
|
|
|
2016-06-30 05:06:51 +08:00
|
|
|
RevertingTentativeParsingAction PA(*this);
|
2012-12-18 22:30:41 +08:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
2019-05-07 15:36:07 +08:00
|
|
|
// A declaration-specifier (not followed by '(' or '{') means this can't be
|
|
|
|
// an expression, but it could still be a template argument.
|
|
|
|
if (TPR != TPResult::Ambiguous &&
|
|
|
|
!(VersusTemplateArgument && TPR == TPResult::True))
|
|
|
|
return TPR;
|
2013-09-13 07:28:08 +08:00
|
|
|
|
2019-05-07 15:36:07 +08:00
|
|
|
bool SeenType = false;
|
|
|
|
do {
|
|
|
|
SeenType |= isCXXDeclarationSpecifierAType();
|
2014-05-16 09:56:53 +08:00
|
|
|
if (TryConsumeDeclarationSpecifier() == TPResult::Error)
|
|
|
|
return TPResult::Error;
|
2019-05-07 15:36:07 +08:00
|
|
|
|
|
|
|
// If we see a parameter name, this can't be a template argument.
|
|
|
|
if (SeenType && Tok.is(tok::identifier))
|
|
|
|
return TPResult::True;
|
|
|
|
|
|
|
|
TPR = isCXXDeclarationSpecifier(TPResult::False,
|
|
|
|
InvalidAsDeclaration);
|
|
|
|
if (TPR == TPResult::Error)
|
|
|
|
return TPR;
|
|
|
|
|
|
|
|
// Two declaration-specifiers means this can't be an expression.
|
|
|
|
if (TPR == TPResult::True && !VersusTemplateArgument)
|
|
|
|
return TPR;
|
|
|
|
} while (TPR != TPResult::False);
|
2012-12-18 22:30:41 +08:00
|
|
|
|
|
|
|
// 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.
|
2020-03-28 08:08:26 +08:00
|
|
|
// FIXME: If we reach a ')' without consuming any '>'s, then this must
|
|
|
|
// also be a function parameter (that's missing its default argument).
|
2013-09-13 07:28:08 +08:00
|
|
|
if (VersusTemplateArgument)
|
2020-03-28 08:08:26 +08:00
|
|
|
return Tok.is(tok::equal) ? 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-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
|
2018-03-08 07:26:02 +08:00
|
|
|
while (Tok.isOneOf(tok::kw_const, tok::kw_volatile, tok::kw___unaligned,
|
|
|
|
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();
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2012-12-18 22:30:41 +08:00
|
|
|
// 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();
|
2019-11-28 09:54:26 +08:00
|
|
|
|
|
|
|
// A constant-expression cannot begin with a '{', but the
|
|
|
|
// expr-or-braced-init-list of a postfix-expression can.
|
|
|
|
if (Tok.is(tok::l_brace))
|
|
|
|
return TPResult::False;
|
|
|
|
|
|
|
|
if (!SkipUntil(tok::r_square, tok::comma, StopAtSemi | StopBeforeMatch))
|
2014-05-16 09:56:53 +08:00
|
|
|
return TPResult::Error;
|
2012-12-18 22:30:41 +08:00
|
|
|
|
2019-11-28 09:54:26 +08:00
|
|
|
// If we hit a comma before the ']', this is not a constant-expression,
|
|
|
|
// but might still be the expr-or-braced-init-list of a postfix-expression.
|
|
|
|
if (Tok.isNot(tok::r_square))
|
|
|
|
return TPResult::False;
|
|
|
|
|
|
|
|
ConsumeBracket();
|
2014-05-16 09:56:53 +08:00
|
|
|
return TPResult::Ambiguous;
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
2019-05-09 11:31:27 +08:00
|
|
|
|
|
|
|
/// Determine whether we might be looking at the '<' template-argument-list '>'
|
|
|
|
/// of a template-id or simple-template-id, rather than a less-than comparison.
|
|
|
|
/// This will often fail and produce an ambiguity, but should never be wrong
|
|
|
|
/// if it returns True or False.
|
|
|
|
Parser::TPResult Parser::isTemplateArgumentList(unsigned TokensToSkip) {
|
|
|
|
if (!TokensToSkip) {
|
|
|
|
if (Tok.isNot(tok::less))
|
|
|
|
return TPResult::False;
|
|
|
|
if (NextToken().is(tok::greater))
|
|
|
|
return TPResult::True;
|
|
|
|
}
|
|
|
|
|
|
|
|
RevertingTentativeParsingAction PA(*this);
|
|
|
|
|
|
|
|
while (TokensToSkip) {
|
|
|
|
ConsumeAnyToken();
|
|
|
|
--TokensToSkip;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!TryConsumeToken(tok::less))
|
|
|
|
return TPResult::False;
|
|
|
|
|
2019-05-16 07:36:14 +08:00
|
|
|
// We can't do much to tell an expression apart from a template-argument,
|
|
|
|
// but one good distinguishing factor is that a "decl-specifier" not
|
|
|
|
// followed by '(' or '{' can't appear in an expression.
|
2019-05-09 11:31:27 +08:00
|
|
|
bool InvalidAsTemplateArgumentList = false;
|
2019-05-16 07:36:14 +08:00
|
|
|
if (isCXXDeclarationSpecifier(TPResult::False,
|
|
|
|
&InvalidAsTemplateArgumentList) ==
|
|
|
|
TPResult::True)
|
|
|
|
return TPResult::True;
|
|
|
|
if (InvalidAsTemplateArgumentList)
|
|
|
|
return TPResult::False;
|
2019-05-09 11:31:27 +08:00
|
|
|
|
2019-05-16 07:36:14 +08:00
|
|
|
// FIXME: In many contexts, X<thing1, Type> can only be a
|
|
|
|
// template-argument-list. But that's not true in general:
|
|
|
|
//
|
|
|
|
// using b = int;
|
|
|
|
// void f() {
|
|
|
|
// int a = A<B, b, c = C>D; // OK, declares b, not a template-id.
|
|
|
|
//
|
|
|
|
// X<Y<0, int> // ', int>' might be end of X's template argument list
|
|
|
|
//
|
|
|
|
// We might be able to disambiguate a few more cases if we're careful.
|
2019-05-09 11:31:27 +08:00
|
|
|
|
2019-05-16 07:36:14 +08:00
|
|
|
// A template-argument-list must be terminated by a '>'.
|
|
|
|
if (SkipUntil({tok::greater, tok::greatergreater, tok::greatergreatergreater},
|
|
|
|
StopAtSemi | StopBeforeMatch))
|
|
|
|
return TPResult::Ambiguous;
|
|
|
|
return TPResult::False;
|
2019-05-09 11:31:27 +08:00
|
|
|
}
|
2020-01-16 10:37:32 +08:00
|
|
|
|
|
|
|
/// Determine whether we might be looking at the '(' of a C++20 explicit(bool)
|
|
|
|
/// in an earlier language mode.
|
|
|
|
Parser::TPResult Parser::isExplicitBool() {
|
|
|
|
assert(Tok.is(tok::l_paren) && "expected to be looking at a '(' token");
|
|
|
|
|
|
|
|
RevertingTentativeParsingAction PA(*this);
|
|
|
|
ConsumeParen();
|
|
|
|
|
|
|
|
// We can only have 'explicit' on a constructor, conversion function, or
|
|
|
|
// deduction guide. The declarator of a deduction guide cannot be
|
|
|
|
// parenthesized, so we know this isn't a deduction guide. So the only
|
|
|
|
// thing we need to check for is some number of parens followed by either
|
|
|
|
// the current class name or 'operator'.
|
|
|
|
while (Tok.is(tok::l_paren))
|
|
|
|
ConsumeParen();
|
|
|
|
|
|
|
|
if (TryAnnotateOptionalCXXScopeToken())
|
|
|
|
return TPResult::Error;
|
|
|
|
|
|
|
|
// Class-scope constructor and conversion function names can't really be
|
|
|
|
// qualified, but we get better diagnostics if we assume they can be.
|
|
|
|
CXXScopeSpec SS;
|
|
|
|
if (Tok.is(tok::annot_cxxscope)) {
|
|
|
|
Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
|
|
|
|
Tok.getAnnotationRange(),
|
|
|
|
SS);
|
|
|
|
ConsumeAnnotationToken();
|
|
|
|
}
|
|
|
|
|
|
|
|
// 'explicit(operator' might be explicit(bool) or the declaration of a
|
|
|
|
// conversion function, but it's probably a conversion function.
|
|
|
|
if (Tok.is(tok::kw_operator))
|
|
|
|
return TPResult::Ambiguous;
|
|
|
|
|
|
|
|
// If this can't be a constructor name, it can only be explicit(bool).
|
|
|
|
if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
|
|
|
|
return TPResult::True;
|
|
|
|
if (!Actions.isCurrentClassName(Tok.is(tok::identifier)
|
|
|
|
? *Tok.getIdentifierInfo()
|
|
|
|
: *takeTemplateIdAnnotation(Tok)->Name,
|
|
|
|
getCurScope(), &SS))
|
|
|
|
return TPResult::True;
|
|
|
|
// Formally, we must have a right-paren after the constructor name to match
|
|
|
|
// the grammar for a constructor. But clang permits a parenthesized
|
|
|
|
// constructor declarator, so also allow a constructor declarator to follow
|
|
|
|
// with no ')' token after the constructor name.
|
|
|
|
if (!NextToken().is(tok::r_paren) &&
|
|
|
|
!isConstructorDeclarator(/*Unqualified=*/SS.isEmpty(),
|
|
|
|
/*DeductionGuide=*/false))
|
|
|
|
return TPResult::True;
|
|
|
|
|
|
|
|
// Might be explicit(bool) or a parenthesized constructor name.
|
|
|
|
return TPResult::Ambiguous;
|
|
|
|
}
|