2006-12-05 02:06:35 +08:00
|
|
|
//===--- ParseExprCXX.cpp - C++ Expression Parsing ------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 03:59:25 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2006-12-05 02:06:35 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the Expression parsing implementation for C++.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-01-29 13:15:15 +08:00
|
|
|
#include "clang/Parse/ParseDiagnostic.h"
|
2006-12-05 02:06:35 +08:00
|
|
|
#include "clang/Parse/Parser.h"
|
2008-08-22 23:38:55 +08:00
|
|
|
#include "clang/Parse/DeclSpec.h"
|
2006-12-05 02:06:35 +08:00
|
|
|
using namespace clang;
|
|
|
|
|
2009-01-06 14:59:53 +08:00
|
|
|
/// ParseOptionalCXXScopeSpecifier - Parse global scope or
|
|
|
|
/// nested-name-specifier if present. Returns true if a nested-name-specifier
|
|
|
|
/// was parsed from the token stream. Note that this routine will not parse
|
|
|
|
/// ::new or ::delete, it will just leave them in the token stream.
|
2008-11-09 00:45:02 +08:00
|
|
|
///
|
|
|
|
/// '::'[opt] nested-name-specifier
|
|
|
|
/// '::'
|
|
|
|
///
|
|
|
|
/// nested-name-specifier:
|
|
|
|
/// type-name '::'
|
|
|
|
/// namespace-name '::'
|
|
|
|
/// nested-name-specifier identifier '::'
|
|
|
|
/// nested-name-specifier 'template'[opt] simple-template-id '::' [TODO]
|
|
|
|
///
|
2009-01-06 14:59:53 +08:00
|
|
|
bool Parser::ParseOptionalCXXScopeSpecifier(CXXScopeSpec &SS) {
|
2008-11-27 05:41:52 +08:00
|
|
|
assert(getLang().CPlusPlus &&
|
2009-01-05 09:24:05 +08:00
|
|
|
"Call sites of this function should be guarded by checking for C++");
|
2008-11-27 05:41:52 +08:00
|
|
|
|
2008-11-09 00:45:02 +08:00
|
|
|
if (Tok.is(tok::annot_cxxscope)) {
|
2009-03-27 07:56:24 +08:00
|
|
|
SS.setScopeRep(Tok.getAnnotationValue());
|
2008-11-09 00:45:02 +08:00
|
|
|
SS.setRange(Tok.getAnnotationRange());
|
|
|
|
ConsumeToken();
|
2008-11-27 05:41:52 +08:00
|
|
|
return true;
|
2008-11-09 00:45:02 +08:00
|
|
|
}
|
2009-01-05 05:14:15 +08:00
|
|
|
|
Implement parsing of nested-name-specifiers that involve template-ids, e.g.,
std::vector<int>::allocator_type
When we parse a template-id that names a type, it will become either a
template-id annotation (which is a parsed representation of a
template-id that has not yet been through semantic analysis) or a
typename annotation (where semantic analysis has resolved the
template-id to an actual type), depending on the context. We only
produce a type in contexts where we know that we only need type
information, e.g., in a type specifier. Otherwise, we create a
template-id annotation that can later be "upgraded" by transforming it
into a typename annotation when the parser needs a type. This occurs,
for example, when we've parsed "std::vector<int>" above and then see
the '::' after it. However, it means that when writing something like
this:
template<> class Outer::Inner<int> { ... };
We have two tokens to represent Outer::Inner<int>: one token for the
nested name specifier Outer::, and one template-id annotation token
for Inner<int>, which will be passed to semantic analysis to define
the class template specialization.
Most of the churn in the template tests in this patch come from an
improvement in our error recovery from ill-formed template-ids.
llvm-svn: 65467
2009-02-26 03:37:18 +08:00
|
|
|
bool HasScopeSpecifier = false;
|
|
|
|
|
2009-01-05 11:55:46 +08:00
|
|
|
if (Tok.is(tok::coloncolon)) {
|
|
|
|
// ::new and ::delete aren't nested-name-specifiers.
|
|
|
|
tok::TokenKind NextKind = NextToken().getKind();
|
|
|
|
if (NextKind == tok::kw_new || NextKind == tok::kw_delete)
|
|
|
|
return false;
|
2009-01-05 08:13:00 +08:00
|
|
|
|
|
|
|
// '::' - Global scope qualifier.
|
2009-01-05 10:07:19 +08:00
|
|
|
SourceLocation CCLoc = ConsumeToken();
|
|
|
|
SS.setBeginLoc(CCLoc);
|
2009-03-27 07:56:24 +08:00
|
|
|
SS.setScopeRep(Actions.ActOnCXXGlobalScopeSpecifier(CurScope, CCLoc));
|
2009-01-05 10:07:19 +08:00
|
|
|
SS.setEndLoc(CCLoc);
|
Implement parsing of nested-name-specifiers that involve template-ids, e.g.,
std::vector<int>::allocator_type
When we parse a template-id that names a type, it will become either a
template-id annotation (which is a parsed representation of a
template-id that has not yet been through semantic analysis) or a
typename annotation (where semantic analysis has resolved the
template-id to an actual type), depending on the context. We only
produce a type in contexts where we know that we only need type
information, e.g., in a type specifier. Otherwise, we create a
template-id annotation that can later be "upgraded" by transforming it
into a typename annotation when the parser needs a type. This occurs,
for example, when we've parsed "std::vector<int>" above and then see
the '::' after it. However, it means that when writing something like
this:
template<> class Outer::Inner<int> { ... };
We have two tokens to represent Outer::Inner<int>: one token for the
nested name specifier Outer::, and one template-id annotation token
for Inner<int>, which will be passed to semantic analysis to define
the class template specialization.
Most of the churn in the template tests in this patch come from an
improvement in our error recovery from ill-formed template-ids.
llvm-svn: 65467
2009-02-26 03:37:18 +08:00
|
|
|
HasScopeSpecifier = true;
|
2008-11-09 00:45:02 +08:00
|
|
|
}
|
|
|
|
|
Implement parsing of nested-name-specifiers that involve template-ids, e.g.,
std::vector<int>::allocator_type
When we parse a template-id that names a type, it will become either a
template-id annotation (which is a parsed representation of a
template-id that has not yet been through semantic analysis) or a
typename annotation (where semantic analysis has resolved the
template-id to an actual type), depending on the context. We only
produce a type in contexts where we know that we only need type
information, e.g., in a type specifier. Otherwise, we create a
template-id annotation that can later be "upgraded" by transforming it
into a typename annotation when the parser needs a type. This occurs,
for example, when we've parsed "std::vector<int>" above and then see
the '::' after it. However, it means that when writing something like
this:
template<> class Outer::Inner<int> { ... };
We have two tokens to represent Outer::Inner<int>: one token for the
nested name specifier Outer::, and one template-id annotation token
for Inner<int>, which will be passed to semantic analysis to define
the class template specialization.
Most of the churn in the template tests in this patch come from an
improvement in our error recovery from ill-formed template-ids.
llvm-svn: 65467
2009-02-26 03:37:18 +08:00
|
|
|
while (true) {
|
|
|
|
// nested-name-specifier:
|
|
|
|
// type-name '::'
|
|
|
|
// namespace-name '::'
|
|
|
|
// nested-name-specifier identifier '::'
|
|
|
|
if (Tok.is(tok::identifier) && NextToken().is(tok::coloncolon)) {
|
|
|
|
// We have an identifier followed by a '::'. Lookup this name
|
|
|
|
// as the name in a nested-name-specifier.
|
|
|
|
IdentifierInfo *II = Tok.getIdentifierInfo();
|
|
|
|
SourceLocation IdLoc = ConsumeToken();
|
|
|
|
assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!");
|
|
|
|
SourceLocation CCLoc = ConsumeToken();
|
|
|
|
|
|
|
|
if (!HasScopeSpecifier) {
|
|
|
|
SS.setBeginLoc(IdLoc);
|
|
|
|
HasScopeSpecifier = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (SS.isInvalid())
|
|
|
|
continue;
|
|
|
|
|
2009-03-27 07:56:24 +08:00
|
|
|
SS.setScopeRep(
|
Implement parsing of nested-name-specifiers that involve template-ids, e.g.,
std::vector<int>::allocator_type
When we parse a template-id that names a type, it will become either a
template-id annotation (which is a parsed representation of a
template-id that has not yet been through semantic analysis) or a
typename annotation (where semantic analysis has resolved the
template-id to an actual type), depending on the context. We only
produce a type in contexts where we know that we only need type
information, e.g., in a type specifier. Otherwise, we create a
template-id annotation that can later be "upgraded" by transforming it
into a typename annotation when the parser needs a type. This occurs,
for example, when we've parsed "std::vector<int>" above and then see
the '::' after it. However, it means that when writing something like
this:
template<> class Outer::Inner<int> { ... };
We have two tokens to represent Outer::Inner<int>: one token for the
nested name specifier Outer::, and one template-id annotation token
for Inner<int>, which will be passed to semantic analysis to define
the class template specialization.
Most of the churn in the template tests in this patch come from an
improvement in our error recovery from ill-formed template-ids.
llvm-svn: 65467
2009-02-26 03:37:18 +08:00
|
|
|
Actions.ActOnCXXNestedNameSpecifier(CurScope, SS, IdLoc, CCLoc, *II));
|
|
|
|
SS.setEndLoc(CCLoc);
|
2008-11-09 00:45:02 +08:00
|
|
|
continue;
|
Implement parsing of nested-name-specifiers that involve template-ids, e.g.,
std::vector<int>::allocator_type
When we parse a template-id that names a type, it will become either a
template-id annotation (which is a parsed representation of a
template-id that has not yet been through semantic analysis) or a
typename annotation (where semantic analysis has resolved the
template-id to an actual type), depending on the context. We only
produce a type in contexts where we know that we only need type
information, e.g., in a type specifier. Otherwise, we create a
template-id annotation that can later be "upgraded" by transforming it
into a typename annotation when the parser needs a type. This occurs,
for example, when we've parsed "std::vector<int>" above and then see
the '::' after it. However, it means that when writing something like
this:
template<> class Outer::Inner<int> { ... };
We have two tokens to represent Outer::Inner<int>: one token for the
nested name specifier Outer::, and one template-id annotation token
for Inner<int>, which will be passed to semantic analysis to define
the class template specialization.
Most of the churn in the template tests in this patch come from an
improvement in our error recovery from ill-formed template-ids.
llvm-svn: 65467
2009-02-26 03:37:18 +08:00
|
|
|
}
|
2008-11-09 00:45:02 +08:00
|
|
|
|
Implement parsing of nested-name-specifiers that involve template-ids, e.g.,
std::vector<int>::allocator_type
When we parse a template-id that names a type, it will become either a
template-id annotation (which is a parsed representation of a
template-id that has not yet been through semantic analysis) or a
typename annotation (where semantic analysis has resolved the
template-id to an actual type), depending on the context. We only
produce a type in contexts where we know that we only need type
information, e.g., in a type specifier. Otherwise, we create a
template-id annotation that can later be "upgraded" by transforming it
into a typename annotation when the parser needs a type. This occurs,
for example, when we've parsed "std::vector<int>" above and then see
the '::' after it. However, it means that when writing something like
this:
template<> class Outer::Inner<int> { ... };
We have two tokens to represent Outer::Inner<int>: one token for the
nested name specifier Outer::, and one template-id annotation token
for Inner<int>, which will be passed to semantic analysis to define
the class template specialization.
Most of the churn in the template tests in this patch come from an
improvement in our error recovery from ill-formed template-ids.
llvm-svn: 65467
2009-02-26 03:37:18 +08:00
|
|
|
// nested-name-specifier:
|
|
|
|
// type-name '::'
|
|
|
|
// nested-name-specifier 'template'[opt] simple-template-id '::'
|
|
|
|
if ((Tok.is(tok::identifier) && NextToken().is(tok::less)) ||
|
|
|
|
Tok.is(tok::kw_template)) {
|
|
|
|
// Parse the optional 'template' keyword, then make sure we have
|
|
|
|
// 'identifier <' after it.
|
|
|
|
if (Tok.is(tok::kw_template)) {
|
2009-03-31 08:43:58 +08:00
|
|
|
SourceLocation TemplateKWLoc = ConsumeToken();
|
Implement parsing of nested-name-specifiers that involve template-ids, e.g.,
std::vector<int>::allocator_type
When we parse a template-id that names a type, it will become either a
template-id annotation (which is a parsed representation of a
template-id that has not yet been through semantic analysis) or a
typename annotation (where semantic analysis has resolved the
template-id to an actual type), depending on the context. We only
produce a type in contexts where we know that we only need type
information, e.g., in a type specifier. Otherwise, we create a
template-id annotation that can later be "upgraded" by transforming it
into a typename annotation when the parser needs a type. This occurs,
for example, when we've parsed "std::vector<int>" above and then see
the '::' after it. However, it means that when writing something like
this:
template<> class Outer::Inner<int> { ... };
We have two tokens to represent Outer::Inner<int>: one token for the
nested name specifier Outer::, and one template-id annotation token
for Inner<int>, which will be passed to semantic analysis to define
the class template specialization.
Most of the churn in the template tests in this patch come from an
improvement in our error recovery from ill-formed template-ids.
llvm-svn: 65467
2009-02-26 03:37:18 +08:00
|
|
|
|
|
|
|
if (Tok.isNot(tok::identifier)) {
|
|
|
|
Diag(Tok.getLocation(),
|
|
|
|
diag::err_id_after_template_in_nested_name_spec)
|
|
|
|
<< SourceRange(TemplateKWLoc);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NextToken().isNot(tok::less)) {
|
|
|
|
Diag(NextToken().getLocation(),
|
|
|
|
diag::err_less_after_template_name_in_nested_name_spec)
|
|
|
|
<< Tok.getIdentifierInfo()->getName()
|
|
|
|
<< SourceRange(TemplateKWLoc, Tok.getLocation());
|
|
|
|
break;
|
|
|
|
}
|
2009-03-31 08:43:58 +08:00
|
|
|
|
|
|
|
TemplateTy Template
|
|
|
|
= Actions.ActOnDependentTemplateName(TemplateKWLoc,
|
|
|
|
*Tok.getIdentifierInfo(),
|
|
|
|
Tok.getLocation(),
|
|
|
|
SS);
|
|
|
|
AnnotateTemplateIdToken(Template, TNK_Dependent_template_name,
|
|
|
|
&SS, TemplateKWLoc, false);
|
|
|
|
continue;
|
Implement parsing of nested-name-specifiers that involve template-ids, e.g.,
std::vector<int>::allocator_type
When we parse a template-id that names a type, it will become either a
template-id annotation (which is a parsed representation of a
template-id that has not yet been through semantic analysis) or a
typename annotation (where semantic analysis has resolved the
template-id to an actual type), depending on the context. We only
produce a type in contexts where we know that we only need type
information, e.g., in a type specifier. Otherwise, we create a
template-id annotation that can later be "upgraded" by transforming it
into a typename annotation when the parser needs a type. This occurs,
for example, when we've parsed "std::vector<int>" above and then see
the '::' after it. However, it means that when writing something like
this:
template<> class Outer::Inner<int> { ... };
We have two tokens to represent Outer::Inner<int>: one token for the
nested name specifier Outer::, and one template-id annotation token
for Inner<int>, which will be passed to semantic analysis to define
the class template specialization.
Most of the churn in the template tests in this patch come from an
improvement in our error recovery from ill-formed template-ids.
llvm-svn: 65467
2009-02-26 03:37:18 +08:00
|
|
|
}
|
|
|
|
|
2009-03-31 06:58:21 +08:00
|
|
|
TemplateTy Template;
|
2009-03-31 08:43:58 +08:00
|
|
|
TemplateNameKind TNK = Actions.isTemplateName(*Tok.getIdentifierInfo(),
|
|
|
|
CurScope, Template, &SS);
|
Implement parsing of nested-name-specifiers that involve template-ids, e.g.,
std::vector<int>::allocator_type
When we parse a template-id that names a type, it will become either a
template-id annotation (which is a parsed representation of a
template-id that has not yet been through semantic analysis) or a
typename annotation (where semantic analysis has resolved the
template-id to an actual type), depending on the context. We only
produce a type in contexts where we know that we only need type
information, e.g., in a type specifier. Otherwise, we create a
template-id annotation that can later be "upgraded" by transforming it
into a typename annotation when the parser needs a type. This occurs,
for example, when we've parsed "std::vector<int>" above and then see
the '::' after it. However, it means that when writing something like
this:
template<> class Outer::Inner<int> { ... };
We have two tokens to represent Outer::Inner<int>: one token for the
nested name specifier Outer::, and one template-id annotation token
for Inner<int>, which will be passed to semantic analysis to define
the class template specialization.
Most of the churn in the template tests in this patch come from an
improvement in our error recovery from ill-formed template-ids.
llvm-svn: 65467
2009-02-26 03:37:18 +08:00
|
|
|
if (TNK) {
|
|
|
|
// We have found a template name, so annotate this this token
|
|
|
|
// with a template-id annotation. We do not permit the
|
|
|
|
// template-id to be translated into a type annotation,
|
|
|
|
// because some clients (e.g., the parsing of class template
|
|
|
|
// specializations) still want to see the original template-id
|
|
|
|
// token.
|
2009-03-31 08:43:58 +08:00
|
|
|
AnnotateTemplateIdToken(Template, TNK, &SS, SourceLocation(), false);
|
Implement parsing of nested-name-specifiers that involve template-ids, e.g.,
std::vector<int>::allocator_type
When we parse a template-id that names a type, it will become either a
template-id annotation (which is a parsed representation of a
template-id that has not yet been through semantic analysis) or a
typename annotation (where semantic analysis has resolved the
template-id to an actual type), depending on the context. We only
produce a type in contexts where we know that we only need type
information, e.g., in a type specifier. Otherwise, we create a
template-id annotation that can later be "upgraded" by transforming it
into a typename annotation when the parser needs a type. This occurs,
for example, when we've parsed "std::vector<int>" above and then see
the '::' after it. However, it means that when writing something like
this:
template<> class Outer::Inner<int> { ... };
We have two tokens to represent Outer::Inner<int>: one token for the
nested name specifier Outer::, and one template-id annotation token
for Inner<int>, which will be passed to semantic analysis to define
the class template specialization.
Most of the churn in the template tests in this patch come from an
improvement in our error recovery from ill-formed template-ids.
llvm-svn: 65467
2009-02-26 03:37:18 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) {
|
|
|
|
// We have
|
|
|
|
//
|
|
|
|
// simple-template-id '::'
|
|
|
|
//
|
|
|
|
// So we need to check whether the simple-template-id is of the
|
2009-03-31 08:43:58 +08:00
|
|
|
// right kind (it should name a type or be dependent), and then
|
|
|
|
// convert it into a type within the nested-name-specifier.
|
Implement parsing of nested-name-specifiers that involve template-ids, e.g.,
std::vector<int>::allocator_type
When we parse a template-id that names a type, it will become either a
template-id annotation (which is a parsed representation of a
template-id that has not yet been through semantic analysis) or a
typename annotation (where semantic analysis has resolved the
template-id to an actual type), depending on the context. We only
produce a type in contexts where we know that we only need type
information, e.g., in a type specifier. Otherwise, we create a
template-id annotation that can later be "upgraded" by transforming it
into a typename annotation when the parser needs a type. This occurs,
for example, when we've parsed "std::vector<int>" above and then see
the '::' after it. However, it means that when writing something like
this:
template<> class Outer::Inner<int> { ... };
We have two tokens to represent Outer::Inner<int>: one token for the
nested name specifier Outer::, and one template-id annotation token
for Inner<int>, which will be passed to semantic analysis to define
the class template specialization.
Most of the churn in the template tests in this patch come from an
improvement in our error recovery from ill-formed template-ids.
llvm-svn: 65467
2009-02-26 03:37:18 +08:00
|
|
|
TemplateIdAnnotation *TemplateId
|
|
|
|
= static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
|
2008-11-27 05:41:52 +08:00
|
|
|
|
2009-03-31 08:43:58 +08:00
|
|
|
if (TemplateId->Kind == TNK_Type_template ||
|
|
|
|
TemplateId->Kind == TNK_Dependent_template_name) {
|
2009-04-02 05:51:26 +08:00
|
|
|
AnnotateTemplateIdTokenAsType(&SS);
|
2009-05-14 08:28:11 +08:00
|
|
|
SS.setScopeRep(0);
|
Implement parsing of nested-name-specifiers that involve template-ids, e.g.,
std::vector<int>::allocator_type
When we parse a template-id that names a type, it will become either a
template-id annotation (which is a parsed representation of a
template-id that has not yet been through semantic analysis) or a
typename annotation (where semantic analysis has resolved the
template-id to an actual type), depending on the context. We only
produce a type in contexts where we know that we only need type
information, e.g., in a type specifier. Otherwise, we create a
template-id annotation that can later be "upgraded" by transforming it
into a typename annotation when the parser needs a type. This occurs,
for example, when we've parsed "std::vector<int>" above and then see
the '::' after it. However, it means that when writing something like
this:
template<> class Outer::Inner<int> { ... };
We have two tokens to represent Outer::Inner<int>: one token for the
nested name specifier Outer::, and one template-id annotation token
for Inner<int>, which will be passed to semantic analysis to define
the class template specialization.
Most of the churn in the template tests in this patch come from an
improvement in our error recovery from ill-formed template-ids.
llvm-svn: 65467
2009-02-26 03:37:18 +08:00
|
|
|
|
|
|
|
assert(Tok.is(tok::annot_typename) &&
|
|
|
|
"AnnotateTemplateIdTokenAsType isn't working");
|
|
|
|
Token TypeToken = Tok;
|
|
|
|
ConsumeToken();
|
|
|
|
assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!");
|
|
|
|
SourceLocation CCLoc = ConsumeToken();
|
|
|
|
|
|
|
|
if (!HasScopeSpecifier) {
|
|
|
|
SS.setBeginLoc(TypeToken.getLocation());
|
|
|
|
HasScopeSpecifier = true;
|
|
|
|
}
|
2009-04-02 05:51:26 +08:00
|
|
|
|
|
|
|
if (TypeToken.getAnnotationValue())
|
|
|
|
SS.setScopeRep(
|
|
|
|
Actions.ActOnCXXNestedNameSpecifier(CurScope, SS,
|
|
|
|
TypeToken.getAnnotationValue(),
|
|
|
|
TypeToken.getAnnotationRange(),
|
|
|
|
CCLoc));
|
|
|
|
else
|
|
|
|
SS.setScopeRep(0);
|
Implement parsing of nested-name-specifiers that involve template-ids, e.g.,
std::vector<int>::allocator_type
When we parse a template-id that names a type, it will become either a
template-id annotation (which is a parsed representation of a
template-id that has not yet been through semantic analysis) or a
typename annotation (where semantic analysis has resolved the
template-id to an actual type), depending on the context. We only
produce a type in contexts where we know that we only need type
information, e.g., in a type specifier. Otherwise, we create a
template-id annotation that can later be "upgraded" by transforming it
into a typename annotation when the parser needs a type. This occurs,
for example, when we've parsed "std::vector<int>" above and then see
the '::' after it. However, it means that when writing something like
this:
template<> class Outer::Inner<int> { ... };
We have two tokens to represent Outer::Inner<int>: one token for the
nested name specifier Outer::, and one template-id annotation token
for Inner<int>, which will be passed to semantic analysis to define
the class template specialization.
Most of the churn in the template tests in this patch come from an
improvement in our error recovery from ill-formed template-ids.
llvm-svn: 65467
2009-02-26 03:37:18 +08:00
|
|
|
SS.setEndLoc(CCLoc);
|
|
|
|
continue;
|
|
|
|
} else
|
2009-03-31 08:43:58 +08:00
|
|
|
assert(false && "FIXME: Only type template names supported here");
|
Implement parsing of nested-name-specifiers that involve template-ids, e.g.,
std::vector<int>::allocator_type
When we parse a template-id that names a type, it will become either a
template-id annotation (which is a parsed representation of a
template-id that has not yet been through semantic analysis) or a
typename annotation (where semantic analysis has resolved the
template-id to an actual type), depending on the context. We only
produce a type in contexts where we know that we only need type
information, e.g., in a type specifier. Otherwise, we create a
template-id annotation that can later be "upgraded" by transforming it
into a typename annotation when the parser needs a type. This occurs,
for example, when we've parsed "std::vector<int>" above and then see
the '::' after it. However, it means that when writing something like
this:
template<> class Outer::Inner<int> { ... };
We have two tokens to represent Outer::Inner<int>: one token for the
nested name specifier Outer::, and one template-id annotation token
for Inner<int>, which will be passed to semantic analysis to define
the class template specialization.
Most of the churn in the template tests in this patch come from an
improvement in our error recovery from ill-formed template-ids.
llvm-svn: 65467
2009-02-26 03:37:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// We don't have any tokens that form the beginning of a
|
|
|
|
// nested-name-specifier, so we're done.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return HasScopeSpecifier;
|
2008-11-09 00:45:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// ParseCXXIdExpression - Handle id-expression.
|
|
|
|
///
|
|
|
|
/// id-expression:
|
|
|
|
/// unqualified-id
|
|
|
|
/// qualified-id
|
|
|
|
///
|
|
|
|
/// unqualified-id:
|
|
|
|
/// identifier
|
|
|
|
/// operator-function-id
|
|
|
|
/// conversion-function-id [TODO]
|
|
|
|
/// '~' class-name [TODO]
|
|
|
|
/// template-id [TODO]
|
|
|
|
///
|
|
|
|
/// qualified-id:
|
|
|
|
/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
|
|
|
|
/// '::' identifier
|
|
|
|
/// '::' operator-function-id
|
|
|
|
/// '::' template-id [TODO]
|
|
|
|
///
|
|
|
|
/// nested-name-specifier:
|
|
|
|
/// type-name '::'
|
|
|
|
/// namespace-name '::'
|
|
|
|
/// nested-name-specifier identifier '::'
|
|
|
|
/// nested-name-specifier 'template'[opt] simple-template-id '::' [TODO]
|
|
|
|
///
|
|
|
|
/// NOTE: The standard specifies that, for qualified-id, the parser does not
|
|
|
|
/// expect:
|
|
|
|
///
|
|
|
|
/// '::' conversion-function-id
|
|
|
|
/// '::' '~' class-name
|
|
|
|
///
|
|
|
|
/// This may cause a slight inconsistency on diagnostics:
|
|
|
|
///
|
|
|
|
/// class C {};
|
|
|
|
/// namespace A {}
|
|
|
|
/// void f() {
|
|
|
|
/// :: A :: ~ C(); // Some Sema error about using destructor with a
|
|
|
|
/// // namespace.
|
|
|
|
/// :: ~ C(); // Some Parser error like 'unexpected ~'.
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// We simplify the parser a bit and make it work like:
|
|
|
|
///
|
|
|
|
/// qualified-id:
|
|
|
|
/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
|
|
|
|
/// '::' unqualified-id
|
|
|
|
///
|
|
|
|
/// That way Sema can handle and report similar errors for namespaces and the
|
|
|
|
/// global scope.
|
|
|
|
///
|
2009-02-04 04:19:35 +08:00
|
|
|
/// The isAddressOfOperand parameter indicates that this id-expression is a
|
|
|
|
/// direct operand of the address-of operator. This is, besides member contexts,
|
|
|
|
/// the only place where a qualified-id naming a non-static class member may
|
|
|
|
/// appear.
|
|
|
|
///
|
|
|
|
Parser::OwningExprResult Parser::ParseCXXIdExpression(bool isAddressOfOperand) {
|
2008-11-09 00:45:02 +08:00
|
|
|
// qualified-id:
|
|
|
|
// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
|
|
|
|
// '::' unqualified-id
|
|
|
|
//
|
|
|
|
CXXScopeSpec SS;
|
2009-01-06 14:59:53 +08:00
|
|
|
ParseOptionalCXXScopeSpecifier(SS);
|
2008-11-09 00:45:02 +08:00
|
|
|
|
|
|
|
// unqualified-id:
|
|
|
|
// identifier
|
|
|
|
// operator-function-id
|
2008-11-18 04:34:05 +08:00
|
|
|
// conversion-function-id
|
2008-11-09 00:45:02 +08:00
|
|
|
// '~' class-name [TODO]
|
|
|
|
// template-id [TODO]
|
|
|
|
//
|
|
|
|
switch (Tok.getKind()) {
|
|
|
|
default:
|
2008-12-12 06:51:44 +08:00
|
|
|
return ExprError(Diag(Tok, diag::err_expected_unqualified_id));
|
2008-11-09 00:45:02 +08:00
|
|
|
|
|
|
|
case tok::identifier: {
|
|
|
|
// Consume the identifier so that we can see if it is followed by a '('.
|
|
|
|
IdentifierInfo &II = *Tok.getIdentifierInfo();
|
|
|
|
SourceLocation L = ConsumeToken();
|
2009-02-04 04:19:35 +08:00
|
|
|
return Actions.ActOnIdentifierExpr(CurScope, L, II, Tok.is(tok::l_paren),
|
|
|
|
&SS, isAddressOfOperand);
|
2008-11-09 00:45:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
case tok::kw_operator: {
|
|
|
|
SourceLocation OperatorLoc = Tok.getLocation();
|
2009-01-05 09:24:05 +08:00
|
|
|
if (OverloadedOperatorKind Op = TryParseOperatorFunctionId())
|
2009-01-19 02:53:16 +08:00
|
|
|
return Actions.ActOnCXXOperatorFunctionIdExpr(
|
2009-02-04 04:19:35 +08:00
|
|
|
CurScope, OperatorLoc, Op, Tok.is(tok::l_paren), SS,
|
|
|
|
isAddressOfOperand);
|
2009-01-05 09:24:05 +08:00
|
|
|
if (TypeTy *Type = ParseConversionFunctionId())
|
2009-01-19 02:53:16 +08:00
|
|
|
return Actions.ActOnCXXConversionFunctionExpr(CurScope, OperatorLoc, Type,
|
2009-02-04 04:19:35 +08:00
|
|
|
Tok.is(tok::l_paren), SS,
|
|
|
|
isAddressOfOperand);
|
2008-12-12 06:51:44 +08:00
|
|
|
|
2008-11-18 04:34:05 +08:00
|
|
|
// We already complained about a bad conversion-function-id,
|
|
|
|
// above.
|
2008-12-12 06:51:44 +08:00
|
|
|
return ExprError();
|
2008-11-09 00:45:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // switch.
|
|
|
|
|
|
|
|
assert(0 && "The switch was supposed to take care everything.");
|
|
|
|
}
|
|
|
|
|
2006-12-05 02:06:35 +08:00
|
|
|
/// ParseCXXCasts - This handles the various ways to cast expressions to another
|
|
|
|
/// type.
|
|
|
|
///
|
|
|
|
/// postfix-expression: [C++ 5.2p1]
|
|
|
|
/// 'dynamic_cast' '<' type-name '>' '(' expression ')'
|
|
|
|
/// 'static_cast' '<' type-name '>' '(' expression ')'
|
|
|
|
/// 'reinterpret_cast' '<' type-name '>' '(' expression ')'
|
|
|
|
/// 'const_cast' '<' type-name '>' '(' expression ')'
|
|
|
|
///
|
2008-12-12 06:51:44 +08:00
|
|
|
Parser::OwningExprResult Parser::ParseCXXCasts() {
|
2006-12-05 02:06:35 +08:00
|
|
|
tok::TokenKind Kind = Tok.getKind();
|
|
|
|
const char *CastName = 0; // For error messages
|
|
|
|
|
|
|
|
switch (Kind) {
|
|
|
|
default: assert(0 && "Unknown C++ cast!"); abort();
|
|
|
|
case tok::kw_const_cast: CastName = "const_cast"; break;
|
|
|
|
case tok::kw_dynamic_cast: CastName = "dynamic_cast"; break;
|
|
|
|
case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break;
|
|
|
|
case tok::kw_static_cast: CastName = "static_cast"; break;
|
|
|
|
}
|
|
|
|
|
|
|
|
SourceLocation OpLoc = ConsumeToken();
|
|
|
|
SourceLocation LAngleBracketLoc = Tok.getLocation();
|
|
|
|
|
|
|
|
if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName))
|
2008-12-12 06:51:44 +08:00
|
|
|
return ExprError();
|
2006-12-05 02:06:35 +08:00
|
|
|
|
2009-02-19 01:45:20 +08:00
|
|
|
TypeResult CastTy = ParseTypeName();
|
2006-12-05 02:06:35 +08:00
|
|
|
SourceLocation RAngleBracketLoc = Tok.getLocation();
|
|
|
|
|
2008-11-18 15:48:38 +08:00
|
|
|
if (ExpectAndConsume(tok::greater, diag::err_expected_greater))
|
2008-12-12 06:51:44 +08:00
|
|
|
return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << "<");
|
2006-12-05 02:06:35 +08:00
|
|
|
|
|
|
|
SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
|
|
|
|
|
2009-05-22 18:23:16 +08:00
|
|
|
if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, CastName))
|
|
|
|
return ExprError();
|
2006-12-05 02:06:35 +08:00
|
|
|
|
2009-05-22 18:23:16 +08:00
|
|
|
OwningExprResult Result = ParseExpression();
|
|
|
|
|
|
|
|
// Match the ')'.
|
|
|
|
if (Result.isInvalid())
|
|
|
|
SkipUntil(tok::r_paren);
|
|
|
|
|
|
|
|
if (Tok.is(tok::r_paren))
|
|
|
|
RParenLoc = ConsumeParen();
|
|
|
|
else
|
|
|
|
MatchRHSPunctuation(tok::r_paren, LParenLoc);
|
2006-12-05 02:06:35 +08:00
|
|
|
|
2009-02-19 01:45:20 +08:00
|
|
|
if (!Result.isInvalid() && !CastTy.isInvalid())
|
2008-10-28 03:41:14 +08:00
|
|
|
Result = Actions.ActOnCXXNamedCast(OpLoc, Kind,
|
2009-03-16 01:47:39 +08:00
|
|
|
LAngleBracketLoc, CastTy.get(),
|
2009-02-19 01:45:20 +08:00
|
|
|
RAngleBracketLoc,
|
2009-03-16 01:47:39 +08:00
|
|
|
LParenLoc, move(Result), RParenLoc);
|
2006-12-05 02:06:35 +08:00
|
|
|
|
2008-12-12 06:51:44 +08:00
|
|
|
return move(Result);
|
2006-12-05 02:06:35 +08:00
|
|
|
}
|
2007-02-13 09:51:42 +08:00
|
|
|
|
2008-11-11 19:37:55 +08:00
|
|
|
/// ParseCXXTypeid - This handles the C++ typeid expression.
|
|
|
|
///
|
|
|
|
/// postfix-expression: [C++ 5.2p1]
|
|
|
|
/// 'typeid' '(' expression ')'
|
|
|
|
/// 'typeid' '(' type-id ')'
|
|
|
|
///
|
2008-12-12 06:51:44 +08:00
|
|
|
Parser::OwningExprResult Parser::ParseCXXTypeid() {
|
2008-11-11 19:37:55 +08:00
|
|
|
assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!");
|
|
|
|
|
|
|
|
SourceLocation OpLoc = ConsumeToken();
|
|
|
|
SourceLocation LParenLoc = Tok.getLocation();
|
|
|
|
SourceLocation RParenLoc;
|
|
|
|
|
|
|
|
// typeid expressions are always parenthesized.
|
|
|
|
if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
|
|
|
|
"typeid"))
|
2008-12-12 06:51:44 +08:00
|
|
|
return ExprError();
|
2008-11-11 19:37:55 +08:00
|
|
|
|
2008-12-10 04:22:58 +08:00
|
|
|
OwningExprResult Result(Actions);
|
2008-11-11 19:37:55 +08:00
|
|
|
|
|
|
|
if (isTypeIdInParens()) {
|
2009-02-19 01:45:20 +08:00
|
|
|
TypeResult Ty = ParseTypeName();
|
2008-11-11 19:37:55 +08:00
|
|
|
|
|
|
|
// Match the ')'.
|
|
|
|
MatchRHSPunctuation(tok::r_paren, LParenLoc);
|
|
|
|
|
2009-02-19 01:45:20 +08:00
|
|
|
if (Ty.isInvalid())
|
2008-12-12 06:51:44 +08:00
|
|
|
return ExprError();
|
2008-11-11 19:37:55 +08:00
|
|
|
|
|
|
|
Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true,
|
2009-02-19 01:45:20 +08:00
|
|
|
Ty.get(), RParenLoc);
|
2008-11-11 19:37:55 +08:00
|
|
|
} else {
|
|
|
|
Result = ParseExpression();
|
|
|
|
|
|
|
|
// Match the ')'.
|
2008-12-09 21:15:23 +08:00
|
|
|
if (Result.isInvalid())
|
2008-11-11 19:37:55 +08:00
|
|
|
SkipUntil(tok::r_paren);
|
|
|
|
else {
|
|
|
|
MatchRHSPunctuation(tok::r_paren, LParenLoc);
|
|
|
|
|
|
|
|
Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false,
|
2008-12-10 08:02:53 +08:00
|
|
|
Result.release(), RParenLoc);
|
2008-11-11 19:37:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-12 06:51:44 +08:00
|
|
|
return move(Result);
|
2008-11-11 19:37:55 +08:00
|
|
|
}
|
|
|
|
|
2007-02-13 09:51:42 +08:00
|
|
|
/// ParseCXXBoolLiteral - This handles the C++ Boolean literals.
|
|
|
|
///
|
|
|
|
/// boolean-literal: [C++ 2.13.5]
|
|
|
|
/// 'true'
|
|
|
|
/// 'false'
|
2008-12-12 06:51:44 +08:00
|
|
|
Parser::OwningExprResult Parser::ParseCXXBoolLiteral() {
|
2007-02-13 09:51:42 +08:00
|
|
|
tok::TokenKind Kind = Tok.getKind();
|
2009-03-16 01:47:39 +08:00
|
|
|
return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind);
|
2007-02-13 09:51:42 +08:00
|
|
|
}
|
2008-02-26 08:51:44 +08:00
|
|
|
|
|
|
|
/// ParseThrowExpression - This handles the C++ throw expression.
|
|
|
|
///
|
|
|
|
/// throw-expression: [C++ 15]
|
|
|
|
/// 'throw' assignment-expression[opt]
|
2008-12-12 06:51:44 +08:00
|
|
|
Parser::OwningExprResult Parser::ParseThrowExpression() {
|
2008-02-26 08:51:44 +08:00
|
|
|
assert(Tok.is(tok::kw_throw) && "Not throw!");
|
|
|
|
SourceLocation ThrowLoc = ConsumeToken(); // Eat the throw token.
|
2008-12-12 06:51:44 +08:00
|
|
|
|
2008-04-06 14:02:23 +08:00
|
|
|
// If the current token isn't the start of an assignment-expression,
|
|
|
|
// then the expression is not present. This handles things like:
|
|
|
|
// "C ? throw : (void)42", which is crazy but legal.
|
|
|
|
switch (Tok.getKind()) { // FIXME: move this predicate somewhere common.
|
|
|
|
case tok::semi:
|
|
|
|
case tok::r_paren:
|
|
|
|
case tok::r_square:
|
|
|
|
case tok::r_brace:
|
|
|
|
case tok::colon:
|
|
|
|
case tok::comma:
|
2009-03-16 01:47:39 +08:00
|
|
|
return Actions.ActOnCXXThrow(ThrowLoc, ExprArg(Actions));
|
2008-02-26 08:51:44 +08:00
|
|
|
|
2008-04-06 14:02:23 +08:00
|
|
|
default:
|
2008-12-12 05:36:32 +08:00
|
|
|
OwningExprResult Expr(ParseAssignmentExpression());
|
2008-12-12 06:51:44 +08:00
|
|
|
if (Expr.isInvalid()) return move(Expr);
|
2009-03-16 01:47:39 +08:00
|
|
|
return Actions.ActOnCXXThrow(ThrowLoc, move(Expr));
|
2008-04-06 14:02:23 +08:00
|
|
|
}
|
2008-02-26 08:51:44 +08:00
|
|
|
}
|
2008-06-25 06:12:16 +08:00
|
|
|
|
|
|
|
/// ParseCXXThis - This handles the C++ 'this' pointer.
|
|
|
|
///
|
|
|
|
/// C++ 9.3.2: In the body of a non-static member function, the keyword this is
|
|
|
|
/// a non-lvalue expression whose value is the address of the object for which
|
|
|
|
/// the function is called.
|
2008-12-12 06:51:44 +08:00
|
|
|
Parser::OwningExprResult Parser::ParseCXXThis() {
|
2008-06-25 06:12:16 +08:00
|
|
|
assert(Tok.is(tok::kw_this) && "Not 'this'!");
|
|
|
|
SourceLocation ThisLoc = ConsumeToken();
|
2009-03-16 01:47:39 +08:00
|
|
|
return Actions.ActOnCXXThis(ThisLoc);
|
2008-06-25 06:12:16 +08:00
|
|
|
}
|
2008-08-22 23:38:55 +08:00
|
|
|
|
|
|
|
/// ParseCXXTypeConstructExpression - Parse construction of a specified type.
|
|
|
|
/// Can be interpreted either as function-style casting ("int(x)")
|
|
|
|
/// or class type construction ("ClassType(x,y,z)")
|
|
|
|
/// or creation of a value-initialized type ("int()").
|
|
|
|
///
|
|
|
|
/// postfix-expression: [C++ 5.2p1]
|
|
|
|
/// simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
|
|
|
|
/// typename-specifier '(' expression-list[opt] ')' [TODO]
|
|
|
|
///
|
2008-12-12 06:51:44 +08:00
|
|
|
Parser::OwningExprResult
|
|
|
|
Parser::ParseCXXTypeConstructExpression(const DeclSpec &DS) {
|
2008-08-22 23:38:55 +08:00
|
|
|
Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
|
2009-01-27 06:44:13 +08:00
|
|
|
TypeTy *TypeRep = Actions.ActOnTypeName(CurScope, DeclaratorInfo).get();
|
2008-08-22 23:38:55 +08:00
|
|
|
|
|
|
|
assert(Tok.is(tok::l_paren) && "Expected '('!");
|
|
|
|
SourceLocation LParenLoc = ConsumeParen();
|
|
|
|
|
2008-11-26 06:21:31 +08:00
|
|
|
ExprVector Exprs(Actions);
|
2008-08-22 23:38:55 +08:00
|
|
|
CommaLocsTy CommaLocs;
|
|
|
|
|
|
|
|
if (Tok.isNot(tok::r_paren)) {
|
|
|
|
if (ParseExpressionList(Exprs, CommaLocs)) {
|
|
|
|
SkipUntil(tok::r_paren);
|
2008-12-12 06:51:44 +08:00
|
|
|
return ExprError();
|
2008-08-22 23:38:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Match the ')'.
|
|
|
|
SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
|
|
|
|
|
|
|
|
assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&&
|
|
|
|
"Unexpected number of commas!");
|
2009-03-16 01:47:39 +08:00
|
|
|
return Actions.ActOnCXXTypeConstructExpr(DS.getSourceRange(), TypeRep,
|
|
|
|
LParenLoc, move_arg(Exprs),
|
2009-05-21 17:52:38 +08:00
|
|
|
CommaLocs.data(), RParenLoc);
|
2008-08-22 23:38:55 +08:00
|
|
|
}
|
|
|
|
|
2008-09-10 04:38:47 +08:00
|
|
|
/// ParseCXXCondition - if/switch/while/for condition expression.
|
|
|
|
///
|
|
|
|
/// condition:
|
|
|
|
/// expression
|
|
|
|
/// type-specifier-seq declarator '=' assignment-expression
|
|
|
|
/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
|
|
|
|
/// '=' assignment-expression
|
|
|
|
///
|
2008-12-12 05:36:32 +08:00
|
|
|
Parser::OwningExprResult Parser::ParseCXXCondition() {
|
2008-10-05 23:03:47 +08:00
|
|
|
if (!isCXXConditionDeclaration())
|
2008-09-10 04:38:47 +08:00
|
|
|
return ParseExpression(); // expression
|
|
|
|
|
|
|
|
SourceLocation StartLoc = Tok.getLocation();
|
|
|
|
|
|
|
|
// type-specifier-seq
|
|
|
|
DeclSpec DS;
|
|
|
|
ParseSpecifierQualifierList(DS);
|
|
|
|
|
|
|
|
// declarator
|
|
|
|
Declarator DeclaratorInfo(DS, Declarator::ConditionContext);
|
|
|
|
ParseDeclarator(DeclaratorInfo);
|
|
|
|
|
|
|
|
// simple-asm-expr[opt]
|
|
|
|
if (Tok.is(tok::kw_asm)) {
|
2009-02-10 02:23:29 +08:00
|
|
|
SourceLocation Loc;
|
|
|
|
OwningExprResult AsmLabel(ParseSimpleAsm(&Loc));
|
2008-12-09 21:15:23 +08:00
|
|
|
if (AsmLabel.isInvalid()) {
|
2008-09-10 04:38:47 +08:00
|
|
|
SkipUntil(tok::semi);
|
2008-12-12 05:36:32 +08:00
|
|
|
return ExprError();
|
2008-09-10 04:38:47 +08:00
|
|
|
}
|
2008-12-10 08:02:53 +08:00
|
|
|
DeclaratorInfo.setAsmLabel(AsmLabel.release());
|
2009-02-10 02:23:29 +08:00
|
|
|
DeclaratorInfo.SetRangeEnd(Loc);
|
2008-09-10 04:38:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// If attributes are present, parse them.
|
2009-02-10 02:23:29 +08:00
|
|
|
if (Tok.is(tok::kw___attribute)) {
|
|
|
|
SourceLocation Loc;
|
|
|
|
AttributeList *AttrList = ParseAttributes(&Loc);
|
|
|
|
DeclaratorInfo.AddAttributes(AttrList, Loc);
|
|
|
|
}
|
2008-09-10 04:38:47 +08:00
|
|
|
|
|
|
|
// '=' assignment-expression
|
|
|
|
if (Tok.isNot(tok::equal))
|
2008-12-12 05:36:32 +08:00
|
|
|
return ExprError(Diag(Tok, diag::err_expected_equal_after_declarator));
|
2008-09-10 04:38:47 +08:00
|
|
|
SourceLocation EqualLoc = ConsumeToken();
|
2008-12-12 05:36:32 +08:00
|
|
|
OwningExprResult AssignExpr(ParseAssignmentExpression());
|
2008-12-09 21:15:23 +08:00
|
|
|
if (AssignExpr.isInvalid())
|
2008-12-12 05:36:32 +08:00
|
|
|
return ExprError();
|
|
|
|
|
2009-03-16 01:47:39 +08:00
|
|
|
return Actions.ActOnCXXConditionDeclarationExpr(CurScope, StartLoc,
|
|
|
|
DeclaratorInfo,EqualLoc,
|
|
|
|
move(AssignExpr));
|
2008-09-10 04:38:47 +08:00
|
|
|
}
|
|
|
|
|
2008-08-22 23:38:55 +08:00
|
|
|
/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
|
|
|
|
/// This should only be called when the current token is known to be part of
|
|
|
|
/// simple-type-specifier.
|
|
|
|
///
|
|
|
|
/// simple-type-specifier:
|
2008-11-09 00:45:02 +08:00
|
|
|
/// '::'[opt] nested-name-specifier[opt] type-name
|
2008-08-22 23:38:55 +08:00
|
|
|
/// '::'[opt] nested-name-specifier 'template' simple-template-id [TODO]
|
|
|
|
/// char
|
|
|
|
/// wchar_t
|
|
|
|
/// bool
|
|
|
|
/// short
|
|
|
|
/// int
|
|
|
|
/// long
|
|
|
|
/// signed
|
|
|
|
/// unsigned
|
|
|
|
/// float
|
|
|
|
/// double
|
|
|
|
/// void
|
|
|
|
/// [GNU] typeof-specifier
|
|
|
|
/// [C++0x] auto [TODO]
|
|
|
|
///
|
|
|
|
/// type-name:
|
|
|
|
/// class-name
|
|
|
|
/// enum-name
|
|
|
|
/// typedef-name
|
|
|
|
///
|
|
|
|
void Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
|
|
|
|
DS.SetRangeStart(Tok.getLocation());
|
|
|
|
const char *PrevSpec;
|
|
|
|
SourceLocation Loc = Tok.getLocation();
|
|
|
|
|
|
|
|
switch (Tok.getKind()) {
|
2009-01-05 08:13:00 +08:00
|
|
|
case tok::identifier: // foo::bar
|
|
|
|
case tok::coloncolon: // ::foo::bar
|
|
|
|
assert(0 && "Annotation token should already be formed!");
|
2008-08-22 23:38:55 +08:00
|
|
|
default:
|
|
|
|
assert(0 && "Not a simple-type-specifier token!");
|
|
|
|
abort();
|
2009-01-05 08:13:00 +08:00
|
|
|
|
2008-08-22 23:38:55 +08:00
|
|
|
// type-name
|
2009-01-06 13:06:21 +08:00
|
|
|
case tok::annot_typename: {
|
2009-02-09 23:09:02 +08:00
|
|
|
DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
|
2008-11-09 00:45:02 +08:00
|
|
|
Tok.getAnnotationValue());
|
2008-08-22 23:38:55 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// builtin types
|
|
|
|
case tok::kw_short:
|
|
|
|
DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
|
|
|
|
break;
|
|
|
|
case tok::kw_long:
|
|
|
|
DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
|
|
|
|
break;
|
|
|
|
case tok::kw_signed:
|
|
|
|
DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
|
|
|
|
break;
|
|
|
|
case tok::kw_unsigned:
|
|
|
|
DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
|
|
|
|
break;
|
|
|
|
case tok::kw_void:
|
|
|
|
DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
|
|
|
|
break;
|
|
|
|
case tok::kw_char:
|
|
|
|
DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
|
|
|
|
break;
|
|
|
|
case tok::kw_int:
|
|
|
|
DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
|
|
|
|
break;
|
|
|
|
case tok::kw_float:
|
|
|
|
DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
|
|
|
|
break;
|
|
|
|
case tok::kw_double:
|
|
|
|
DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
|
|
|
|
break;
|
|
|
|
case tok::kw_wchar_t:
|
|
|
|
DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
|
|
|
|
break;
|
|
|
|
case tok::kw_bool:
|
|
|
|
DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
|
|
|
|
break;
|
|
|
|
|
|
|
|
// GNU typeof support.
|
|
|
|
case tok::kw_typeof:
|
|
|
|
ParseTypeofSpecifier(DS);
|
2009-04-02 06:41:11 +08:00
|
|
|
DS.Finish(Diags, PP);
|
2008-08-22 23:38:55 +08:00
|
|
|
return;
|
|
|
|
}
|
2009-01-06 13:06:21 +08:00
|
|
|
if (Tok.is(tok::annot_typename))
|
2008-11-09 00:45:02 +08:00
|
|
|
DS.SetRangeEnd(Tok.getAnnotationEndLoc());
|
|
|
|
else
|
|
|
|
DS.SetRangeEnd(Tok.getLocation());
|
2008-08-22 23:38:55 +08:00
|
|
|
ConsumeToken();
|
2009-04-02 06:41:11 +08:00
|
|
|
DS.Finish(Diags, PP);
|
2008-08-22 23:38:55 +08:00
|
|
|
}
|
2008-11-07 06:13:31 +08:00
|
|
|
|
2008-11-08 04:08:42 +08:00
|
|
|
/// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++
|
|
|
|
/// [dcl.name]), which is a non-empty sequence of type-specifiers,
|
|
|
|
/// e.g., "const short int". Note that the DeclSpec is *not* finished
|
|
|
|
/// by parsing the type-specifier-seq, because these sequences are
|
|
|
|
/// typically followed by some form of declarator. Returns true and
|
|
|
|
/// emits diagnostics if this is not a type-specifier-seq, false
|
|
|
|
/// otherwise.
|
|
|
|
///
|
|
|
|
/// type-specifier-seq: [C++ 8.1]
|
|
|
|
/// type-specifier type-specifier-seq[opt]
|
|
|
|
///
|
|
|
|
bool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) {
|
|
|
|
DS.SetRangeStart(Tok.getLocation());
|
|
|
|
const char *PrevSpec = 0;
|
|
|
|
int isInvalid = 0;
|
|
|
|
|
|
|
|
// Parse one or more of the type specifiers.
|
2009-01-06 14:59:53 +08:00
|
|
|
if (!ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec)) {
|
2008-11-18 15:48:38 +08:00
|
|
|
Diag(Tok, diag::err_operator_missing_type_specifier);
|
2008-11-08 04:08:42 +08:00
|
|
|
return true;
|
|
|
|
}
|
2009-01-06 14:59:53 +08:00
|
|
|
|
2009-01-07 03:17:58 +08:00
|
|
|
while (ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec)) ;
|
2008-11-08 04:08:42 +08:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-11-18 00:14:12 +08:00
|
|
|
/// TryParseOperatorFunctionId - Attempts to parse a C++ overloaded
|
2008-11-07 06:13:31 +08:00
|
|
|
/// operator name (C++ [over.oper]). If successful, returns the
|
|
|
|
/// predefined identifier that corresponds to that overloaded
|
|
|
|
/// operator. Otherwise, returns NULL and does not consume any tokens.
|
|
|
|
///
|
|
|
|
/// operator-function-id: [C++ 13.5]
|
|
|
|
/// 'operator' operator
|
|
|
|
///
|
|
|
|
/// operator: one of
|
|
|
|
/// new delete new[] delete[]
|
|
|
|
/// + - * / % ^ & | ~
|
|
|
|
/// ! = < > += -= *= /= %=
|
|
|
|
/// ^= &= |= << >> >>= <<= == !=
|
|
|
|
/// <= >= && || ++ -- , ->* ->
|
|
|
|
/// () []
|
2009-02-10 02:23:29 +08:00
|
|
|
OverloadedOperatorKind
|
|
|
|
Parser::TryParseOperatorFunctionId(SourceLocation *EndLoc) {
|
2008-11-07 23:54:02 +08:00
|
|
|
assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
|
2009-02-10 02:23:29 +08:00
|
|
|
SourceLocation Loc;
|
2008-11-07 06:13:31 +08:00
|
|
|
|
|
|
|
OverloadedOperatorKind Op = OO_None;
|
|
|
|
switch (NextToken().getKind()) {
|
|
|
|
case tok::kw_new:
|
|
|
|
ConsumeToken(); // 'operator'
|
2009-02-10 02:23:29 +08:00
|
|
|
Loc = ConsumeToken(); // 'new'
|
2008-11-07 06:13:31 +08:00
|
|
|
if (Tok.is(tok::l_square)) {
|
|
|
|
ConsumeBracket(); // '['
|
2009-02-10 02:23:29 +08:00
|
|
|
Loc = Tok.getLocation();
|
2008-11-07 06:13:31 +08:00
|
|
|
ExpectAndConsume(tok::r_square, diag::err_expected_rsquare); // ']'
|
|
|
|
Op = OO_Array_New;
|
|
|
|
} else {
|
|
|
|
Op = OO_New;
|
|
|
|
}
|
2009-02-10 02:23:29 +08:00
|
|
|
if (EndLoc)
|
|
|
|
*EndLoc = Loc;
|
Extend DeclarationName to support C++ overloaded operators, e.g.,
operator+, directly, using the same mechanism as all other special
names.
Removed the "special" identifiers for the overloaded operators from
the identifier table and IdentifierInfo data structure. IdentifierInfo
is back to representing only real identifiers.
Added a new Action, ActOnOperatorFunctionIdExpr, that builds an
expression from an parsed operator-function-id (e.g., "operator
+"). ActOnIdentifierExpr used to do this job, but
operator-function-ids are no longer represented by IdentifierInfo's.
Extended Declarator to store overloaded operator names.
Sema::GetNameForDeclarator now knows how to turn the operator
name into a DeclarationName for the overloaded operator.
Except for (perhaps) consolidating the functionality of
ActOnIdentifier, ActOnOperatorFunctionIdExpr, and
ActOnConversionFunctionExpr into a common routine that builds an
appropriate DeclRefExpr by looking up a DeclarationName, all of the
work on normalizing declaration names should be complete with this
commit.
llvm-svn: 59526
2008-11-18 22:39:36 +08:00
|
|
|
return Op;
|
2008-11-07 06:13:31 +08:00
|
|
|
|
|
|
|
case tok::kw_delete:
|
|
|
|
ConsumeToken(); // 'operator'
|
2009-02-10 02:23:29 +08:00
|
|
|
Loc = ConsumeToken(); // 'delete'
|
2008-11-07 06:13:31 +08:00
|
|
|
if (Tok.is(tok::l_square)) {
|
|
|
|
ConsumeBracket(); // '['
|
2009-02-10 02:23:29 +08:00
|
|
|
Loc = Tok.getLocation();
|
2008-11-07 06:13:31 +08:00
|
|
|
ExpectAndConsume(tok::r_square, diag::err_expected_rsquare); // ']'
|
|
|
|
Op = OO_Array_Delete;
|
|
|
|
} else {
|
|
|
|
Op = OO_Delete;
|
|
|
|
}
|
2009-02-10 02:23:29 +08:00
|
|
|
if (EndLoc)
|
|
|
|
*EndLoc = Loc;
|
Extend DeclarationName to support C++ overloaded operators, e.g.,
operator+, directly, using the same mechanism as all other special
names.
Removed the "special" identifiers for the overloaded operators from
the identifier table and IdentifierInfo data structure. IdentifierInfo
is back to representing only real identifiers.
Added a new Action, ActOnOperatorFunctionIdExpr, that builds an
expression from an parsed operator-function-id (e.g., "operator
+"). ActOnIdentifierExpr used to do this job, but
operator-function-ids are no longer represented by IdentifierInfo's.
Extended Declarator to store overloaded operator names.
Sema::GetNameForDeclarator now knows how to turn the operator
name into a DeclarationName for the overloaded operator.
Except for (perhaps) consolidating the functionality of
ActOnIdentifier, ActOnOperatorFunctionIdExpr, and
ActOnConversionFunctionExpr into a common routine that builds an
appropriate DeclRefExpr by looking up a DeclarationName, all of the
work on normalizing declaration names should be complete with this
commit.
llvm-svn: 59526
2008-11-18 22:39:36 +08:00
|
|
|
return Op;
|
2008-11-07 06:13:31 +08:00
|
|
|
|
2008-11-10 21:38:07 +08:00
|
|
|
#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
|
2008-11-07 06:13:31 +08:00
|
|
|
case tok::Token: Op = OO_##Name; break;
|
2008-11-10 21:38:07 +08:00
|
|
|
#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
|
2008-11-07 06:13:31 +08:00
|
|
|
#include "clang/Basic/OperatorKinds.def"
|
|
|
|
|
|
|
|
case tok::l_paren:
|
|
|
|
ConsumeToken(); // 'operator'
|
|
|
|
ConsumeParen(); // '('
|
2009-02-10 02:23:29 +08:00
|
|
|
Loc = Tok.getLocation();
|
2008-11-07 06:13:31 +08:00
|
|
|
ExpectAndConsume(tok::r_paren, diag::err_expected_rparen); // ')'
|
2009-02-10 02:23:29 +08:00
|
|
|
if (EndLoc)
|
|
|
|
*EndLoc = Loc;
|
Extend DeclarationName to support C++ overloaded operators, e.g.,
operator+, directly, using the same mechanism as all other special
names.
Removed the "special" identifiers for the overloaded operators from
the identifier table and IdentifierInfo data structure. IdentifierInfo
is back to representing only real identifiers.
Added a new Action, ActOnOperatorFunctionIdExpr, that builds an
expression from an parsed operator-function-id (e.g., "operator
+"). ActOnIdentifierExpr used to do this job, but
operator-function-ids are no longer represented by IdentifierInfo's.
Extended Declarator to store overloaded operator names.
Sema::GetNameForDeclarator now knows how to turn the operator
name into a DeclarationName for the overloaded operator.
Except for (perhaps) consolidating the functionality of
ActOnIdentifier, ActOnOperatorFunctionIdExpr, and
ActOnConversionFunctionExpr into a common routine that builds an
appropriate DeclRefExpr by looking up a DeclarationName, all of the
work on normalizing declaration names should be complete with this
commit.
llvm-svn: 59526
2008-11-18 22:39:36 +08:00
|
|
|
return OO_Call;
|
2008-11-07 06:13:31 +08:00
|
|
|
|
|
|
|
case tok::l_square:
|
|
|
|
ConsumeToken(); // 'operator'
|
|
|
|
ConsumeBracket(); // '['
|
2009-02-10 02:23:29 +08:00
|
|
|
Loc = Tok.getLocation();
|
2008-11-07 06:13:31 +08:00
|
|
|
ExpectAndConsume(tok::r_square, diag::err_expected_rsquare); // ']'
|
2009-02-10 02:23:29 +08:00
|
|
|
if (EndLoc)
|
|
|
|
*EndLoc = Loc;
|
Extend DeclarationName to support C++ overloaded operators, e.g.,
operator+, directly, using the same mechanism as all other special
names.
Removed the "special" identifiers for the overloaded operators from
the identifier table and IdentifierInfo data structure. IdentifierInfo
is back to representing only real identifiers.
Added a new Action, ActOnOperatorFunctionIdExpr, that builds an
expression from an parsed operator-function-id (e.g., "operator
+"). ActOnIdentifierExpr used to do this job, but
operator-function-ids are no longer represented by IdentifierInfo's.
Extended Declarator to store overloaded operator names.
Sema::GetNameForDeclarator now knows how to turn the operator
name into a DeclarationName for the overloaded operator.
Except for (perhaps) consolidating the functionality of
ActOnIdentifier, ActOnOperatorFunctionIdExpr, and
ActOnConversionFunctionExpr into a common routine that builds an
appropriate DeclRefExpr by looking up a DeclarationName, all of the
work on normalizing declaration names should be complete with this
commit.
llvm-svn: 59526
2008-11-18 22:39:36 +08:00
|
|
|
return OO_Subscript;
|
2008-11-07 06:13:31 +08:00
|
|
|
|
|
|
|
default:
|
Extend DeclarationName to support C++ overloaded operators, e.g.,
operator+, directly, using the same mechanism as all other special
names.
Removed the "special" identifiers for the overloaded operators from
the identifier table and IdentifierInfo data structure. IdentifierInfo
is back to representing only real identifiers.
Added a new Action, ActOnOperatorFunctionIdExpr, that builds an
expression from an parsed operator-function-id (e.g., "operator
+"). ActOnIdentifierExpr used to do this job, but
operator-function-ids are no longer represented by IdentifierInfo's.
Extended Declarator to store overloaded operator names.
Sema::GetNameForDeclarator now knows how to turn the operator
name into a DeclarationName for the overloaded operator.
Except for (perhaps) consolidating the functionality of
ActOnIdentifier, ActOnOperatorFunctionIdExpr, and
ActOnConversionFunctionExpr into a common routine that builds an
appropriate DeclRefExpr by looking up a DeclarationName, all of the
work on normalizing declaration names should be complete with this
commit.
llvm-svn: 59526
2008-11-18 22:39:36 +08:00
|
|
|
return OO_None;
|
2008-11-07 06:13:31 +08:00
|
|
|
}
|
2008-11-18 00:14:12 +08:00
|
|
|
|
|
|
|
ConsumeToken(); // 'operator'
|
2009-02-10 02:23:29 +08:00
|
|
|
Loc = ConsumeAnyToken(); // the operator itself
|
|
|
|
if (EndLoc)
|
|
|
|
*EndLoc = Loc;
|
Extend DeclarationName to support C++ overloaded operators, e.g.,
operator+, directly, using the same mechanism as all other special
names.
Removed the "special" identifiers for the overloaded operators from
the identifier table and IdentifierInfo data structure. IdentifierInfo
is back to representing only real identifiers.
Added a new Action, ActOnOperatorFunctionIdExpr, that builds an
expression from an parsed operator-function-id (e.g., "operator
+"). ActOnIdentifierExpr used to do this job, but
operator-function-ids are no longer represented by IdentifierInfo's.
Extended Declarator to store overloaded operator names.
Sema::GetNameForDeclarator now knows how to turn the operator
name into a DeclarationName for the overloaded operator.
Except for (perhaps) consolidating the functionality of
ActOnIdentifier, ActOnOperatorFunctionIdExpr, and
ActOnConversionFunctionExpr into a common routine that builds an
appropriate DeclRefExpr by looking up a DeclarationName, all of the
work on normalizing declaration names should be complete with this
commit.
llvm-svn: 59526
2008-11-18 22:39:36 +08:00
|
|
|
return Op;
|
2008-11-07 06:13:31 +08:00
|
|
|
}
|
2008-11-08 04:08:42 +08:00
|
|
|
|
|
|
|
/// ParseConversionFunctionId - Parse a C++ conversion-function-id,
|
|
|
|
/// which expresses the name of a user-defined conversion operator
|
|
|
|
/// (C++ [class.conv.fct]p1). Returns the type that this operator is
|
|
|
|
/// specifying a conversion for, or NULL if there was an error.
|
|
|
|
///
|
|
|
|
/// conversion-function-id: [C++ 12.3.2]
|
|
|
|
/// operator conversion-type-id
|
|
|
|
///
|
|
|
|
/// conversion-type-id:
|
|
|
|
/// type-specifier-seq conversion-declarator[opt]
|
|
|
|
///
|
|
|
|
/// conversion-declarator:
|
|
|
|
/// ptr-operator conversion-declarator[opt]
|
2009-02-10 02:23:29 +08:00
|
|
|
Parser::TypeTy *Parser::ParseConversionFunctionId(SourceLocation *EndLoc) {
|
2008-11-08 04:08:42 +08:00
|
|
|
assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
|
|
|
|
ConsumeToken(); // 'operator'
|
|
|
|
|
|
|
|
// Parse the type-specifier-seq.
|
|
|
|
DeclSpec DS;
|
|
|
|
if (ParseCXXTypeSpecifierSeq(DS))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// Parse the conversion-declarator, which is merely a sequence of
|
|
|
|
// ptr-operators.
|
|
|
|
Declarator D(DS, Declarator::TypeNameContext);
|
2008-11-22 03:14:01 +08:00
|
|
|
ParseDeclaratorInternal(D, /*DirectDeclParser=*/0);
|
2009-02-10 02:23:29 +08:00
|
|
|
if (EndLoc)
|
|
|
|
*EndLoc = D.getSourceRange().getEnd();
|
2008-11-08 04:08:42 +08:00
|
|
|
|
|
|
|
// Finish up the type.
|
|
|
|
Action::TypeResult Result = Actions.ActOnTypeName(CurScope, D);
|
2009-01-27 06:44:13 +08:00
|
|
|
if (Result.isInvalid())
|
2008-11-08 04:08:42 +08:00
|
|
|
return 0;
|
|
|
|
else
|
2009-01-27 06:44:13 +08:00
|
|
|
return Result.get();
|
2008-11-08 04:08:42 +08:00
|
|
|
}
|
2008-11-22 03:14:01 +08:00
|
|
|
|
|
|
|
/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate
|
|
|
|
/// memory in a typesafe manner and call constructors.
|
2009-01-05 05:25:24 +08:00
|
|
|
///
|
|
|
|
/// This method is called to parse the new expression after the optional :: has
|
|
|
|
/// been already parsed. If the :: was present, "UseGlobal" is true and "Start"
|
|
|
|
/// is its location. Otherwise, "Start" is the location of the 'new' token.
|
2008-11-22 03:14:01 +08:00
|
|
|
///
|
|
|
|
/// new-expression:
|
|
|
|
/// '::'[opt] 'new' new-placement[opt] new-type-id
|
|
|
|
/// new-initializer[opt]
|
|
|
|
/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
|
|
|
|
/// new-initializer[opt]
|
|
|
|
///
|
|
|
|
/// new-placement:
|
|
|
|
/// '(' expression-list ')'
|
|
|
|
///
|
2008-12-02 22:43:59 +08:00
|
|
|
/// new-type-id:
|
|
|
|
/// type-specifier-seq new-declarator[opt]
|
|
|
|
///
|
|
|
|
/// new-declarator:
|
|
|
|
/// ptr-operator new-declarator[opt]
|
|
|
|
/// direct-new-declarator
|
|
|
|
///
|
2008-11-22 03:14:01 +08:00
|
|
|
/// new-initializer:
|
|
|
|
/// '(' expression-list[opt] ')'
|
|
|
|
/// [C++0x] braced-init-list [TODO]
|
|
|
|
///
|
2009-01-05 05:25:24 +08:00
|
|
|
Parser::OwningExprResult
|
|
|
|
Parser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
|
|
|
|
assert(Tok.is(tok::kw_new) && "expected 'new' token");
|
|
|
|
ConsumeToken(); // Consume 'new'
|
2008-11-22 03:14:01 +08:00
|
|
|
|
|
|
|
// A '(' now can be a new-placement or the '(' wrapping the type-id in the
|
|
|
|
// second form of new-expression. It can't be a new-type-id.
|
|
|
|
|
2008-11-26 06:21:31 +08:00
|
|
|
ExprVector PlacementArgs(Actions);
|
2008-11-22 03:14:01 +08:00
|
|
|
SourceLocation PlacementLParen, PlacementRParen;
|
|
|
|
|
|
|
|
bool ParenTypeId;
|
2008-12-02 22:43:59 +08:00
|
|
|
DeclSpec DS;
|
|
|
|
Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
|
2008-11-22 03:14:01 +08:00
|
|
|
if (Tok.is(tok::l_paren)) {
|
|
|
|
// If it turns out to be a placement, we change the type location.
|
|
|
|
PlacementLParen = ConsumeParen();
|
2008-12-02 22:43:59 +08:00
|
|
|
if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) {
|
|
|
|
SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
|
2008-12-12 06:51:44 +08:00
|
|
|
return ExprError();
|
2008-12-02 22:43:59 +08:00
|
|
|
}
|
2008-11-22 03:14:01 +08:00
|
|
|
|
|
|
|
PlacementRParen = MatchRHSPunctuation(tok::r_paren, PlacementLParen);
|
2008-12-02 22:43:59 +08:00
|
|
|
if (PlacementRParen.isInvalid()) {
|
|
|
|
SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
|
2008-12-12 06:51:44 +08:00
|
|
|
return ExprError();
|
2008-12-02 22:43:59 +08:00
|
|
|
}
|
2008-11-22 03:14:01 +08:00
|
|
|
|
2008-12-02 22:43:59 +08:00
|
|
|
if (PlacementArgs.empty()) {
|
2008-11-22 03:14:01 +08:00
|
|
|
// Reset the placement locations. There was no placement.
|
|
|
|
PlacementLParen = PlacementRParen = SourceLocation();
|
|
|
|
ParenTypeId = true;
|
|
|
|
} else {
|
|
|
|
// We still need the type.
|
|
|
|
if (Tok.is(tok::l_paren)) {
|
2008-12-02 22:43:59 +08:00
|
|
|
SourceLocation LParen = ConsumeParen();
|
|
|
|
ParseSpecifierQualifierList(DS);
|
2009-02-10 02:23:29 +08:00
|
|
|
DeclaratorInfo.SetSourceRange(DS.getSourceRange());
|
2008-12-02 22:43:59 +08:00
|
|
|
ParseDeclarator(DeclaratorInfo);
|
|
|
|
MatchRHSPunctuation(tok::r_paren, LParen);
|
2008-11-22 03:14:01 +08:00
|
|
|
ParenTypeId = true;
|
|
|
|
} else {
|
2008-12-02 22:43:59 +08:00
|
|
|
if (ParseCXXTypeSpecifierSeq(DS))
|
|
|
|
DeclaratorInfo.setInvalidType(true);
|
2009-02-10 02:23:29 +08:00
|
|
|
else {
|
|
|
|
DeclaratorInfo.SetSourceRange(DS.getSourceRange());
|
2008-12-02 22:43:59 +08:00
|
|
|
ParseDeclaratorInternal(DeclaratorInfo,
|
|
|
|
&Parser::ParseDirectNewDeclarator);
|
2009-02-10 02:23:29 +08:00
|
|
|
}
|
2008-11-22 03:14:01 +08:00
|
|
|
ParenTypeId = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2008-12-02 22:43:59 +08:00
|
|
|
// A new-type-id is a simplified type-id, where essentially the
|
|
|
|
// direct-declarator is replaced by a direct-new-declarator.
|
|
|
|
if (ParseCXXTypeSpecifierSeq(DS))
|
|
|
|
DeclaratorInfo.setInvalidType(true);
|
2009-02-10 02:23:29 +08:00
|
|
|
else {
|
|
|
|
DeclaratorInfo.SetSourceRange(DS.getSourceRange());
|
2008-12-02 22:43:59 +08:00
|
|
|
ParseDeclaratorInternal(DeclaratorInfo,
|
|
|
|
&Parser::ParseDirectNewDeclarator);
|
2009-02-10 02:23:29 +08:00
|
|
|
}
|
2008-11-22 03:14:01 +08:00
|
|
|
ParenTypeId = false;
|
|
|
|
}
|
2009-04-25 16:06:05 +08:00
|
|
|
if (DeclaratorInfo.isInvalidType()) {
|
2008-12-02 22:43:59 +08:00
|
|
|
SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
|
2008-12-12 06:51:44 +08:00
|
|
|
return ExprError();
|
2008-12-02 22:43:59 +08:00
|
|
|
}
|
2008-11-22 03:14:01 +08:00
|
|
|
|
2008-11-26 06:21:31 +08:00
|
|
|
ExprVector ConstructorArgs(Actions);
|
2008-11-22 03:14:01 +08:00
|
|
|
SourceLocation ConstructorLParen, ConstructorRParen;
|
|
|
|
|
|
|
|
if (Tok.is(tok::l_paren)) {
|
|
|
|
ConstructorLParen = ConsumeParen();
|
|
|
|
if (Tok.isNot(tok::r_paren)) {
|
|
|
|
CommaLocsTy CommaLocs;
|
2008-12-02 22:43:59 +08:00
|
|
|
if (ParseExpressionList(ConstructorArgs, CommaLocs)) {
|
|
|
|
SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
|
2008-12-12 06:51:44 +08:00
|
|
|
return ExprError();
|
2008-12-02 22:43:59 +08:00
|
|
|
}
|
2008-11-22 03:14:01 +08:00
|
|
|
}
|
|
|
|
ConstructorRParen = MatchRHSPunctuation(tok::r_paren, ConstructorLParen);
|
2008-12-02 22:43:59 +08:00
|
|
|
if (ConstructorRParen.isInvalid()) {
|
|
|
|
SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
|
2008-12-12 06:51:44 +08:00
|
|
|
return ExprError();
|
2008-12-02 22:43:59 +08:00
|
|
|
}
|
2008-11-22 03:14:01 +08:00
|
|
|
}
|
|
|
|
|
2009-03-16 01:47:39 +08:00
|
|
|
return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen,
|
|
|
|
move_arg(PlacementArgs), PlacementRParen,
|
|
|
|
ParenTypeId, DeclaratorInfo, ConstructorLParen,
|
|
|
|
move_arg(ConstructorArgs), ConstructorRParen);
|
2008-11-22 03:14:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be
|
|
|
|
/// passed to ParseDeclaratorInternal.
|
|
|
|
///
|
|
|
|
/// direct-new-declarator:
|
|
|
|
/// '[' expression ']'
|
|
|
|
/// direct-new-declarator '[' constant-expression ']'
|
|
|
|
///
|
2009-01-05 05:25:24 +08:00
|
|
|
void Parser::ParseDirectNewDeclarator(Declarator &D) {
|
2008-11-22 03:14:01 +08:00
|
|
|
// Parse the array dimensions.
|
|
|
|
bool first = true;
|
|
|
|
while (Tok.is(tok::l_square)) {
|
|
|
|
SourceLocation LLoc = ConsumeBracket();
|
2008-12-12 05:36:32 +08:00
|
|
|
OwningExprResult Size(first ? ParseExpression()
|
|
|
|
: ParseConstantExpression());
|
2008-12-09 21:15:23 +08:00
|
|
|
if (Size.isInvalid()) {
|
2008-11-22 03:14:01 +08:00
|
|
|
// Recover
|
|
|
|
SkipUntil(tok::r_square);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
first = false;
|
|
|
|
|
2009-02-10 02:23:29 +08:00
|
|
|
SourceLocation RLoc = MatchRHSPunctuation(tok::r_square, LLoc);
|
2008-11-22 03:14:01 +08:00
|
|
|
D.AddTypeInfo(DeclaratorChunk::getArray(0, /*static=*/false, /*star=*/false,
|
2009-02-10 02:23:29 +08:00
|
|
|
Size.release(), LLoc),
|
|
|
|
RLoc);
|
2008-11-22 03:14:01 +08:00
|
|
|
|
2009-02-10 02:23:29 +08:00
|
|
|
if (RLoc.isInvalid())
|
2008-11-22 03:14:01 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id.
|
|
|
|
/// This ambiguity appears in the syntax of the C++ new operator.
|
|
|
|
///
|
|
|
|
/// new-expression:
|
|
|
|
/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
|
|
|
|
/// new-initializer[opt]
|
|
|
|
///
|
|
|
|
/// new-placement:
|
|
|
|
/// '(' expression-list ')'
|
|
|
|
///
|
2008-12-02 22:43:59 +08:00
|
|
|
bool Parser::ParseExpressionListOrTypeId(ExprListTy &PlacementArgs,
|
2009-01-05 05:25:24 +08:00
|
|
|
Declarator &D) {
|
2008-11-22 03:14:01 +08:00
|
|
|
// The '(' was already consumed.
|
|
|
|
if (isTypeIdInParens()) {
|
2008-12-02 22:43:59 +08:00
|
|
|
ParseSpecifierQualifierList(D.getMutableDeclSpec());
|
2009-02-10 02:23:29 +08:00
|
|
|
D.SetSourceRange(D.getDeclSpec().getSourceRange());
|
2008-12-02 22:43:59 +08:00
|
|
|
ParseDeclarator(D);
|
2009-04-25 16:06:05 +08:00
|
|
|
return D.isInvalidType();
|
2008-11-22 03:14:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// It's not a type, it has to be an expression list.
|
|
|
|
// Discard the comma locations - ActOnCXXNew has enough parameters.
|
|
|
|
CommaLocsTy CommaLocs;
|
|
|
|
return ParseExpressionList(PlacementArgs, CommaLocs);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used
|
|
|
|
/// to free memory allocated by new.
|
|
|
|
///
|
2009-01-05 05:25:24 +08:00
|
|
|
/// This method is called to parse the 'delete' expression after the optional
|
|
|
|
/// '::' has been already parsed. If the '::' was present, "UseGlobal" is true
|
|
|
|
/// and "Start" is its location. Otherwise, "Start" is the location of the
|
|
|
|
/// 'delete' token.
|
|
|
|
///
|
2008-11-22 03:14:01 +08:00
|
|
|
/// delete-expression:
|
|
|
|
/// '::'[opt] 'delete' cast-expression
|
|
|
|
/// '::'[opt] 'delete' '[' ']' cast-expression
|
2009-01-05 05:25:24 +08:00
|
|
|
Parser::OwningExprResult
|
|
|
|
Parser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) {
|
|
|
|
assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword");
|
|
|
|
ConsumeToken(); // Consume 'delete'
|
2008-11-22 03:14:01 +08:00
|
|
|
|
|
|
|
// Array delete?
|
|
|
|
bool ArrayDelete = false;
|
|
|
|
if (Tok.is(tok::l_square)) {
|
|
|
|
ArrayDelete = true;
|
|
|
|
SourceLocation LHS = ConsumeBracket();
|
|
|
|
SourceLocation RHS = MatchRHSPunctuation(tok::r_square, LHS);
|
|
|
|
if (RHS.isInvalid())
|
2008-12-12 06:51:44 +08:00
|
|
|
return ExprError();
|
2008-11-22 03:14:01 +08:00
|
|
|
}
|
|
|
|
|
2008-12-12 05:36:32 +08:00
|
|
|
OwningExprResult Operand(ParseCastExpression(false));
|
2008-12-09 21:15:23 +08:00
|
|
|
if (Operand.isInvalid())
|
2008-12-12 06:51:44 +08:00
|
|
|
return move(Operand);
|
2008-11-22 03:14:01 +08:00
|
|
|
|
2009-03-16 01:47:39 +08:00
|
|
|
return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, move(Operand));
|
2008-11-22 03:14:01 +08:00
|
|
|
}
|
2009-01-06 04:52:13 +08:00
|
|
|
|
|
|
|
static UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind)
|
|
|
|
{
|
|
|
|
switch(kind) {
|
|
|
|
default: assert(false && "Not a known unary type trait.");
|
|
|
|
case tok::kw___has_nothrow_assign: return UTT_HasNothrowAssign;
|
|
|
|
case tok::kw___has_nothrow_copy: return UTT_HasNothrowCopy;
|
|
|
|
case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor;
|
|
|
|
case tok::kw___has_trivial_assign: return UTT_HasTrivialAssign;
|
|
|
|
case tok::kw___has_trivial_copy: return UTT_HasTrivialCopy;
|
|
|
|
case tok::kw___has_trivial_constructor: return UTT_HasTrivialConstructor;
|
|
|
|
case tok::kw___has_trivial_destructor: return UTT_HasTrivialDestructor;
|
|
|
|
case tok::kw___has_virtual_destructor: return UTT_HasVirtualDestructor;
|
|
|
|
case tok::kw___is_abstract: return UTT_IsAbstract;
|
|
|
|
case tok::kw___is_class: return UTT_IsClass;
|
|
|
|
case tok::kw___is_empty: return UTT_IsEmpty;
|
|
|
|
case tok::kw___is_enum: return UTT_IsEnum;
|
|
|
|
case tok::kw___is_pod: return UTT_IsPOD;
|
|
|
|
case tok::kw___is_polymorphic: return UTT_IsPolymorphic;
|
|
|
|
case tok::kw___is_union: return UTT_IsUnion;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// ParseUnaryTypeTrait - Parse the built-in unary type-trait
|
|
|
|
/// pseudo-functions that allow implementation of the TR1/C++0x type traits
|
|
|
|
/// templates.
|
|
|
|
///
|
|
|
|
/// primary-expression:
|
|
|
|
/// [GNU] unary-type-trait '(' type-id ')'
|
|
|
|
///
|
|
|
|
Parser::OwningExprResult Parser::ParseUnaryTypeTrait()
|
|
|
|
{
|
|
|
|
UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind());
|
|
|
|
SourceLocation Loc = ConsumeToken();
|
|
|
|
|
|
|
|
SourceLocation LParen = Tok.getLocation();
|
|
|
|
if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
|
|
|
|
return ExprError();
|
|
|
|
|
|
|
|
// FIXME: Error reporting absolutely sucks! If the this fails to parse a type
|
|
|
|
// there will be cryptic errors about mismatched parentheses and missing
|
|
|
|
// specifiers.
|
2009-02-19 01:45:20 +08:00
|
|
|
TypeResult Ty = ParseTypeName();
|
2009-01-06 04:52:13 +08:00
|
|
|
|
|
|
|
SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
|
|
|
|
|
2009-02-19 01:45:20 +08:00
|
|
|
if (Ty.isInvalid())
|
|
|
|
return ExprError();
|
|
|
|
|
|
|
|
return Actions.ActOnUnaryTypeTrait(UTT, Loc, LParen, Ty.get(), RParen);
|
2009-01-06 04:52:13 +08:00
|
|
|
}
|
2009-05-22 18:24:42 +08:00
|
|
|
|
|
|
|
/// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a
|
|
|
|
/// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate
|
|
|
|
/// based on the context past the parens.
|
|
|
|
Parser::OwningExprResult
|
|
|
|
Parser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
|
|
|
|
TypeTy *&CastTy,
|
|
|
|
SourceLocation LParenLoc,
|
|
|
|
SourceLocation &RParenLoc) {
|
|
|
|
assert(getLang().CPlusPlus && "Should only be called for C++!");
|
|
|
|
assert(ExprType == CastExpr && "Compound literals are not ambiguous!");
|
|
|
|
assert(isTypeIdInParens() && "Not a type-id!");
|
|
|
|
|
|
|
|
OwningExprResult Result(Actions, true);
|
|
|
|
CastTy = 0;
|
|
|
|
|
|
|
|
// We need to disambiguate a very ugly part of the C++ syntax:
|
|
|
|
//
|
|
|
|
// (T())x; - type-id
|
|
|
|
// (T())*x; - type-id
|
|
|
|
// (T())/x; - expression
|
|
|
|
// (T()); - expression
|
|
|
|
//
|
|
|
|
// The bad news is that we cannot use the specialized tentative parser, since
|
|
|
|
// it can only verify that the thing inside the parens can be parsed as
|
|
|
|
// type-id, it is not useful for determining the context past the parens.
|
|
|
|
//
|
|
|
|
// The good news is that the parser can disambiguate this part without
|
2009-05-22 23:12:46 +08:00
|
|
|
// making any unnecessary Action calls.
|
2009-05-23 05:09:47 +08:00
|
|
|
//
|
|
|
|
// It uses a scheme similar to parsing inline methods. The parenthesized
|
|
|
|
// tokens are cached, the context that follows is determined (possibly by
|
|
|
|
// parsing a cast-expression), and then we re-introduce the cached tokens
|
|
|
|
// into the token stream and parse them appropriately.
|
|
|
|
|
|
|
|
ParenParseOption ParseAs;
|
|
|
|
CachedTokens Toks;
|
|
|
|
|
|
|
|
// Store the tokens of the parentheses. We will parse them after we determine
|
|
|
|
// the context that follows them.
|
|
|
|
if (!ConsumeAndStoreUntil(tok::r_paren, tok::unknown, Toks, tok::semi)) {
|
|
|
|
// We didn't find the ')' we expected.
|
2009-05-22 18:24:42 +08:00
|
|
|
MatchRHSPunctuation(tok::r_paren, LParenLoc);
|
|
|
|
return ExprError();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Tok.is(tok::l_brace)) {
|
2009-05-23 05:09:47 +08:00
|
|
|
ParseAs = CompoundLiteral;
|
|
|
|
} else {
|
|
|
|
bool NotCastExpr;
|
2009-05-26 03:41:42 +08:00
|
|
|
// FIXME: Special-case ++ and --: "(S())++;" is not a cast-expression
|
|
|
|
if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) {
|
|
|
|
NotCastExpr = true;
|
|
|
|
} else {
|
|
|
|
// Try parsing the cast-expression that may follow.
|
|
|
|
// If it is not a cast-expression, NotCastExpr will be true and no token
|
|
|
|
// will be consumed.
|
|
|
|
Result = ParseCastExpression(false/*isUnaryExpression*/,
|
|
|
|
false/*isAddressofOperand*/,
|
|
|
|
NotCastExpr);
|
|
|
|
}
|
2009-05-23 05:09:47 +08:00
|
|
|
|
|
|
|
// If we parsed a cast-expression, it's really a type-id, otherwise it's
|
|
|
|
// an expression.
|
|
|
|
ParseAs = NotCastExpr ? SimpleExpr : CastExpr;
|
2009-05-22 18:24:42 +08:00
|
|
|
}
|
|
|
|
|
2009-05-23 05:09:47 +08:00
|
|
|
// The current token should go after the cached tokens.
|
|
|
|
Toks.push_back(Tok);
|
|
|
|
// Re-enter the stored parenthesized tokens into the token stream, so we may
|
|
|
|
// parse them now.
|
|
|
|
PP.EnterTokenStream(Toks.data(), Toks.size(),
|
|
|
|
true/*DisableMacroExpansion*/, false/*OwnsTokens*/);
|
|
|
|
// Drop the current token and bring the first cached one. It's the same token
|
|
|
|
// as when we entered this function.
|
|
|
|
ConsumeAnyToken();
|
|
|
|
|
|
|
|
if (ParseAs >= CompoundLiteral) {
|
|
|
|
TypeResult Ty = ParseTypeName();
|
2009-05-22 18:24:42 +08:00
|
|
|
|
2009-05-23 05:09:47 +08:00
|
|
|
// Match the ')'.
|
|
|
|
if (Tok.is(tok::r_paren))
|
|
|
|
RParenLoc = ConsumeParen();
|
|
|
|
else
|
|
|
|
MatchRHSPunctuation(tok::r_paren, LParenLoc);
|
|
|
|
|
|
|
|
if (ParseAs == CompoundLiteral) {
|
|
|
|
ExprType = CompoundLiteral;
|
|
|
|
return ParseCompoundLiteralExpression(Ty.get(), LParenLoc, RParenLoc);
|
|
|
|
}
|
|
|
|
|
|
|
|
// We parsed '(' type-id ')' and the thing after it wasn't a '{'.
|
|
|
|
assert(ParseAs == CastExpr);
|
|
|
|
|
|
|
|
if (Ty.isInvalid())
|
|
|
|
return ExprError();
|
2009-05-22 18:24:42 +08:00
|
|
|
|
|
|
|
CastTy = Ty.get();
|
2009-05-23 05:09:47 +08:00
|
|
|
|
|
|
|
// Result is what ParseCastExpression returned earlier.
|
2009-05-22 18:24:42 +08:00
|
|
|
if (!Result.isInvalid())
|
|
|
|
Result = Actions.ActOnCastExpr(LParenLoc, CastTy, RParenLoc,move(Result));
|
|
|
|
return move(Result);
|
|
|
|
}
|
2009-05-23 05:09:47 +08:00
|
|
|
|
|
|
|
// Not a compound literal, and not followed by a cast-expression.
|
|
|
|
assert(ParseAs == SimpleExpr);
|
2009-05-22 18:24:42 +08:00
|
|
|
|
|
|
|
ExprType = SimpleExpr;
|
2009-05-23 05:09:47 +08:00
|
|
|
Result = ParseExpression();
|
2009-05-22 18:24:42 +08:00
|
|
|
if (!Result.isInvalid() && Tok.is(tok::r_paren))
|
|
|
|
Result = Actions.ActOnParenExpr(LParenLoc, Tok.getLocation(), move(Result));
|
|
|
|
|
|
|
|
// Match the ')'.
|
|
|
|
if (Result.isInvalid()) {
|
|
|
|
SkipUntil(tok::r_paren);
|
|
|
|
return ExprError();
|
|
|
|
}
|
2009-05-23 05:09:47 +08:00
|
|
|
|
2009-05-22 18:24:42 +08:00
|
|
|
if (Tok.is(tok::r_paren))
|
|
|
|
RParenLoc = ConsumeParen();
|
|
|
|
else
|
|
|
|
MatchRHSPunctuation(tok::r_paren, LParenLoc);
|
|
|
|
|
|
|
|
return move(Result);
|
|
|
|
}
|