Process names.

This commit is contained in:
Roberto Raggi
2010-08-12 16:55:14 +02:00
parent 47069717f8
commit bcad21e526
2 changed files with 259 additions and 73 deletions

View File

@@ -55,6 +55,7 @@
#include "CoreTypes.h" #include "CoreTypes.h"
#include "Literals.h" #include "Literals.h"
#include "Scope.h" #include "Scope.h"
#include <vector>
#include <memory> #include <memory>
#include <cassert> #include <cassert>
@@ -64,9 +65,9 @@ namespace { bool debug_todo = false; }
Bind::Bind(TranslationUnit *unit) Bind::Bind(TranslationUnit *unit)
: ASTVisitor(unit), : ASTVisitor(unit),
_currentScope(0), _scope(0),
_currentExpression(0), _expression(0),
_currentName(0) _name(0)
{ {
if (unit->ast()) if (unit->ast())
translationUnit(unit->ast()->asTranslationUnit()); translationUnit(unit->ast()->asTranslationUnit());
@@ -74,13 +75,13 @@ Bind::Bind(TranslationUnit *unit)
Scope *Bind::currentScope() const Scope *Bind::currentScope() const
{ {
return _currentScope; return _scope;
} }
Scope *Bind::switchScope(Scope *scope) Scope *Bind::switchScope(Scope *scope)
{ {
Scope *previousScope = _currentScope; Scope *previousScope = _scope;
_currentScope = scope; _scope = scope;
return previousScope; return previousScope;
} }
@@ -92,9 +93,9 @@ void Bind::statement(StatementAST *ast)
Bind::ExpressionTy Bind::expression(ExpressionAST *ast) Bind::ExpressionTy Bind::expression(ExpressionAST *ast)
{ {
ExpressionTy value = ExpressionTy(); ExpressionTy value = ExpressionTy();
std::swap(_currentExpression, value); std::swap(_expression, value);
accept(ast); accept(ast);
std::swap(_currentExpression, value); std::swap(_expression, value);
return value; return value;
} }
@@ -106,45 +107,45 @@ void Bind::declaration(DeclarationAST *ast)
const Name *Bind::name(NameAST *ast) const Name *Bind::name(NameAST *ast)
{ {
const Name *value = 0; const Name *value = 0;
std::swap(_currentName, value); std::swap(_name, value);
accept(ast); accept(ast);
std::swap(_currentName, value); std::swap(_name, value);
return value; return value;
} }
FullySpecifiedType Bind::specifier(SpecifierAST *ast, const FullySpecifiedType &init) FullySpecifiedType Bind::specifier(SpecifierAST *ast, const FullySpecifiedType &init)
{ {
FullySpecifiedType value = init; FullySpecifiedType value = init;
std::swap(_currentType, value); std::swap(_type, value);
accept(ast); accept(ast);
std::swap(_currentType, value); std::swap(_type, value);
return value; return value;
} }
FullySpecifiedType Bind::ptrOperator(PtrOperatorAST *ast, const FullySpecifiedType &init) FullySpecifiedType Bind::ptrOperator(PtrOperatorAST *ast, const FullySpecifiedType &init)
{ {
FullySpecifiedType value = init; FullySpecifiedType value = init;
std::swap(_currentType, value); std::swap(_type, value);
accept(ast); accept(ast);
std::swap(_currentType, value); std::swap(_type, value);
return value; return value;
} }
FullySpecifiedType Bind::coreDeclarator(CoreDeclaratorAST *ast, const FullySpecifiedType &init) FullySpecifiedType Bind::coreDeclarator(CoreDeclaratorAST *ast, const FullySpecifiedType &init)
{ {
FullySpecifiedType value = init; FullySpecifiedType value = init;
std::swap(_currentType, value); std::swap(_type, value);
accept(ast); accept(ast);
std::swap(_currentType, value); std::swap(_type, value);
return value; return value;
} }
FullySpecifiedType Bind::postfixDeclarator(PostfixDeclaratorAST *ast, const FullySpecifiedType &init) FullySpecifiedType Bind::postfixDeclarator(PostfixDeclaratorAST *ast, const FullySpecifiedType &init)
{ {
FullySpecifiedType value = init; FullySpecifiedType value = init;
std::swap(_currentType, value); std::swap(_type, value);
accept(ast); accept(ast);
std::swap(_currentType, value); std::swap(_type, value);
return value; return value;
} }
@@ -156,15 +157,15 @@ bool Bind::visit(ObjCSelectorArgumentAST *ast)
return false; return false;
} }
void Bind::objCSelectorArgument(ObjCSelectorArgumentAST *ast) const Name *Bind::objCSelectorArgument(ObjCSelectorArgumentAST *ast, bool *hasArg)
{ {
if (! ast) if (! (ast && ast->name_token))
return; return 0;
if (debug_todo) if (ast->colon_token)
translationUnit()->warning(ast->firstToken(), "TODO: %s", __func__); *hasArg = true;
// unsigned name_token = ast->name_token;
// unsigned colon_token = ast->colon_token; return control()->nameId(identifier(ast->name_token));
} }
bool Bind::visit(AttributeAST *ast) bool Bind::visit(AttributeAST *ast)
@@ -378,15 +379,13 @@ bool Bind::visit(NestedNameSpecifierAST *ast)
return false; return false;
} }
void Bind::nestedNameSpecifier(NestedNameSpecifierAST *ast) const Name *Bind::nestedNameSpecifier(NestedNameSpecifierAST *ast)
{ {
if (! ast) if (! ast)
return; return 0;
if (debug_todo) const Name *class_or_namespace_name = this->name(ast->class_or_namespace_name);
translationUnit()->warning(ast->firstToken(), "TODO: %s", __func__); return class_or_namespace_name;
/*const Name *class_or_namespace_name =*/ this->name(ast->class_or_namespace_name);
// unsigned scope_token = ast->scope_token;
} }
bool Bind::visit(NewPlacementAST *ast) bool Bind::visit(NewPlacementAST *ast)
@@ -487,16 +486,189 @@ bool Bind::visit(OperatorAST *ast)
return false; return false;
} }
void Bind::cppOperator(OperatorAST *ast) int Bind::cppOperator(OperatorAST *ast)
{ {
if (! ast) OperatorNameId::Kind kind = OperatorNameId::InvalidOp;
return;
if (! ast)
return kind;
if (debug_todo)
translationUnit()->warning(ast->firstToken(), "TODO: %s", __func__);
// unsigned op_token = ast->op_token; // unsigned op_token = ast->op_token;
// unsigned open_token = ast->open_token; // unsigned open_token = ast->open_token;
// unsigned close_token = ast->close_token; // unsigned close_token = ast->close_token;
switch (tokenKind(ast->op_token)) {
case T_NEW:
if (ast->open_token)
kind = OperatorNameId::NewArrayOp;
else
kind = OperatorNameId::NewOp;
break;
case T_DELETE:
if (ast->open_token)
kind = OperatorNameId::DeleteArrayOp;
else
kind = OperatorNameId::DeleteOp;
break;
case T_PLUS:
kind = OperatorNameId::PlusOp;
break;
case T_MINUS:
kind = OperatorNameId::MinusOp;
break;
case T_STAR:
kind = OperatorNameId::StarOp;
break;
case T_SLASH:
kind = OperatorNameId::SlashOp;
break;
case T_PERCENT:
kind = OperatorNameId::PercentOp;
break;
case T_CARET:
kind = OperatorNameId::CaretOp;
break;
case T_AMPER:
kind = OperatorNameId::AmpOp;
break;
case T_PIPE:
kind = OperatorNameId::PipeOp;
break;
case T_TILDE:
kind = OperatorNameId::TildeOp;
break;
case T_EXCLAIM:
kind = OperatorNameId::ExclaimOp;
break;
case T_EQUAL:
kind = OperatorNameId::EqualOp;
break;
case T_LESS:
kind = OperatorNameId::LessOp;
break;
case T_GREATER:
kind = OperatorNameId::GreaterOp;
break;
case T_PLUS_EQUAL:
kind = OperatorNameId::PlusEqualOp;
break;
case T_MINUS_EQUAL:
kind = OperatorNameId::MinusEqualOp;
break;
case T_STAR_EQUAL:
kind = OperatorNameId::StarEqualOp;
break;
case T_SLASH_EQUAL:
kind = OperatorNameId::SlashEqualOp;
break;
case T_PERCENT_EQUAL:
kind = OperatorNameId::PercentEqualOp;
break;
case T_CARET_EQUAL:
kind = OperatorNameId::CaretEqualOp;
break;
case T_AMPER_EQUAL:
kind = OperatorNameId::AmpEqualOp;
break;
case T_PIPE_EQUAL:
kind = OperatorNameId::PipeEqualOp;
break;
case T_LESS_LESS:
kind = OperatorNameId::LessLessOp;
break;
case T_GREATER_GREATER:
kind = OperatorNameId::GreaterGreaterOp;
break;
case T_LESS_LESS_EQUAL:
kind = OperatorNameId::LessLessEqualOp;
break;
case T_GREATER_GREATER_EQUAL:
kind = OperatorNameId::GreaterGreaterEqualOp;
break;
case T_EQUAL_EQUAL:
kind = OperatorNameId::EqualEqualOp;
break;
case T_EXCLAIM_EQUAL:
kind = OperatorNameId::ExclaimEqualOp;
break;
case T_LESS_EQUAL:
kind = OperatorNameId::LessEqualOp;
break;
case T_GREATER_EQUAL:
kind = OperatorNameId::GreaterEqualOp;
break;
case T_AMPER_AMPER:
kind = OperatorNameId::AmpAmpOp;
break;
case T_PIPE_PIPE:
kind = OperatorNameId::PipePipeOp;
break;
case T_PLUS_PLUS:
kind = OperatorNameId::PlusPlusOp;
break;
case T_MINUS_MINUS:
kind = OperatorNameId::MinusMinusOp;
break;
case T_COMMA:
kind = OperatorNameId::CommaOp;
break;
case T_ARROW_STAR:
kind = OperatorNameId::ArrowStarOp;
break;
case T_ARROW:
kind = OperatorNameId::ArrowOp;
break;
case T_LPAREN:
kind = OperatorNameId::FunctionCallOp;
break;
case T_LBRACKET:
kind = OperatorNameId::ArrayAccessOp;
break;
default:
kind = OperatorNameId::InvalidOp;
} // switch
return kind;
} }
bool Bind::visit(ParameterDeclarationClauseAST *ast) bool Bind::visit(ParameterDeclarationClauseAST *ast)
@@ -1845,42 +2017,53 @@ bool Bind::visit(ObjCDynamicPropertiesDeclarationAST *ast)
// NameAST // NameAST
bool Bind::visit(ObjCSelectorAST *ast) bool Bind::visit(ObjCSelectorAST *ast) // ### review
{ {
if (debug_todo) std::vector<const Name *> arguments;
translationUnit()->warning(ast->firstToken(), "TODO: %s", __func__); bool hasArgs = false;
for (ObjCSelectorArgumentListAST *it = ast->selector_argument_list; it; it = it->next) { for (ObjCSelectorArgumentListAST *it = ast->selector_argument_list; it; it = it->next) {
this->objCSelectorArgument(it->value); if (const Name *selector_argument = this->objCSelectorArgument(it->value, &hasArgs))
arguments.push_back(selector_argument);
} }
if (! arguments.empty()) {
_name = control()->selectorNameId(&arguments[0], arguments.size(), hasArgs);
ast->name = _name;
}
return false; return false;
} }
bool Bind::visit(QualifiedNameAST *ast) bool Bind::visit(QualifiedNameAST *ast)
{ {
if (debug_todo)
translationUnit()->warning(ast->firstToken(), "TODO: %s", __func__);
// unsigned global_scope_token = ast->global_scope_token;
for (NestedNameSpecifierListAST *it = ast->nested_name_specifier_list; it; it = it->next) { for (NestedNameSpecifierListAST *it = ast->nested_name_specifier_list; it; it = it->next) {
this->nestedNameSpecifier(it->value); const Name *class_or_namespace_name = this->nestedNameSpecifier(it->value);
if (_name || ast->global_scope_token)
_name = control()->qualifiedNameId(_name, class_or_namespace_name);
else
_name = class_or_namespace_name;
} }
/*const Name *unqualified_name =*/ this->name(ast->unqualified_name);
const Name *unqualified_name = this->name(ast->unqualified_name);
if (_name || ast->global_scope_token)
_name = control()->qualifiedNameId(_name, unqualified_name);
else
_name = unqualified_name;
ast->name = _name;
return false; return false;
} }
bool Bind::visit(OperatorFunctionIdAST *ast) bool Bind::visit(OperatorFunctionIdAST *ast)
{ {
if (debug_todo) const int op = this->cppOperator(ast->op);
translationUnit()->warning(ast->firstToken(), "TODO: %s", __func__); ast->name = _name = control()->operatorNameId(op);
// unsigned operator_token = ast->operator_token;
this->cppOperator(ast->op);
return false; return false;
} }
bool Bind::visit(ConversionFunctionIdAST *ast) bool Bind::visit(ConversionFunctionIdAST *ast)
{ {
if (debug_todo)
translationUnit()->warning(ast->firstToken(), "TODO: %s", __func__);
// unsigned operator_token = ast->operator_token;
FullySpecifiedType type; FullySpecifiedType type;
for (SpecifierListAST *it = ast->type_specifier_list; it; it = it->next) { for (SpecifierListAST *it = ast->type_specifier_list; it; it = it->next) {
type = this->specifier(it->value, type); type = this->specifier(it->value, type);
@@ -1888,37 +2071,40 @@ bool Bind::visit(ConversionFunctionIdAST *ast)
for (PtrOperatorListAST *it = ast->ptr_operator_list; it; it = it->next) { for (PtrOperatorListAST *it = ast->ptr_operator_list; it; it = it->next) {
type = this->ptrOperator(it->value, type); type = this->ptrOperator(it->value, type);
} }
ast->name = _name = control()->conversionNameId(type);
return false; return false;
} }
bool Bind::visit(SimpleNameAST *ast) bool Bind::visit(SimpleNameAST *ast)
{ {
if (debug_todo) const Identifier *id = identifier(ast->identifier_token);
translationUnit()->warning(ast->firstToken(), "TODO: %s", __func__); ast->name = _name = control()->nameId(id);
// unsigned identifier_token = ast->identifier_token;
return false; return false;
} }
bool Bind::visit(DestructorNameAST *ast) bool Bind::visit(DestructorNameAST *ast)
{ {
if (debug_todo) const Identifier *id = identifier(ast->identifier_token);
translationUnit()->warning(ast->firstToken(), "TODO: %s", __func__); ast->name = _name = control()->destructorNameId(id);
// unsigned tilde_token = ast->tilde_token;
// unsigned identifier_token = ast->identifier_token;
return false; return false;
} }
bool Bind::visit(TemplateIdAST *ast) bool Bind::visit(TemplateIdAST *ast)
{ {
if (debug_todo) // collect the template parameters
translationUnit()->warning(ast->firstToken(), "TODO: %s", __func__); std::vector<FullySpecifiedType> templateArguments;
// unsigned template_token = ast->template_token;
// unsigned identifier_token = ast->identifier_token;
// unsigned less_token = ast->less_token;
for (ExpressionListAST *it = ast->template_argument_list; it; it = it->next) { for (ExpressionListAST *it = ast->template_argument_list; it; it = it->next) {
ExpressionTy value = this->expression(it->value); ExpressionTy value = this->expression(it->value);
templateArguments.push_back(value);
} }
// unsigned greater_token = ast->greater_token;
const Identifier *id = identifier(ast->identifier_token);
if (templateArguments.empty())
_name = control()->templateNameId(id);
else
_name = control()->templateNameId(id, &templateArguments[0], templateArguments.size());
ast->name = _name;
return false; return false;
} }
@@ -2026,7 +2212,7 @@ bool Bind::visit(PointerToMemberAST *ast)
translationUnit()->warning(ast->firstToken(), "TODO: %s", __func__); translationUnit()->warning(ast->firstToken(), "TODO: %s", __func__);
// unsigned global_scope_token = ast->global_scope_token; // unsigned global_scope_token = ast->global_scope_token;
for (NestedNameSpecifierListAST *it = ast->nested_name_specifier_list; it; it = it->next) { for (NestedNameSpecifierListAST *it = ast->nested_name_specifier_list; it; it = it->next) {
this->nestedNameSpecifier(it->value); /*const Name *nested_name_specifier = */ this->nestedNameSpecifier(it->value);
} }
FullySpecifiedType type; FullySpecifiedType type;
// unsigned star_token = ast->star_token; // unsigned star_token = ast->star_token;

View File

@@ -78,7 +78,7 @@ public:
protected: protected:
using ASTVisitor::translationUnit; using ASTVisitor::translationUnit;
void objCSelectorArgument(ObjCSelectorArgumentAST *ast); const Name *objCSelectorArgument(ObjCSelectorArgumentAST *ast, bool *hasArg);
void attribute(AttributeAST *ast); void attribute(AttributeAST *ast);
FullySpecifiedType declarator(DeclaratorAST *ast, const FullySpecifiedType &init); FullySpecifiedType declarator(DeclaratorAST *ast, const FullySpecifiedType &init);
void qtPropertyDeclarationItem(QtPropertyDeclarationItemAST *ast); void qtPropertyDeclarationItem(QtPropertyDeclarationItemAST *ast);
@@ -88,12 +88,12 @@ protected:
void enumerator(EnumeratorAST *ast); void enumerator(EnumeratorAST *ast);
FullySpecifiedType exceptionSpecification(ExceptionSpecificationAST *ast, const FullySpecifiedType &init); FullySpecifiedType exceptionSpecification(ExceptionSpecificationAST *ast, const FullySpecifiedType &init);
void memInitializer(MemInitializerAST *ast); void memInitializer(MemInitializerAST *ast);
void nestedNameSpecifier(NestedNameSpecifierAST *ast); const Name *nestedNameSpecifier(NestedNameSpecifierAST *ast);
void newPlacement(NewPlacementAST *ast); void newPlacement(NewPlacementAST *ast);
FullySpecifiedType newArrayDeclarator(NewArrayDeclaratorAST *ast, const FullySpecifiedType &init); FullySpecifiedType newArrayDeclarator(NewArrayDeclaratorAST *ast, const FullySpecifiedType &init);
void newInitializer(NewInitializerAST *ast); void newInitializer(NewInitializerAST *ast);
FullySpecifiedType newTypeId(NewTypeIdAST *ast); FullySpecifiedType newTypeId(NewTypeIdAST *ast);
void cppOperator(OperatorAST *ast); int cppOperator(OperatorAST *ast);
void parameterDeclarationClause(ParameterDeclarationClauseAST *ast); void parameterDeclarationClause(ParameterDeclarationClauseAST *ast);
void translationUnit(TranslationUnitAST *ast); void translationUnit(TranslationUnitAST *ast);
void objCProtocolRefs(ObjCProtocolRefsAST *ast); void objCProtocolRefs(ObjCProtocolRefsAST *ast);
@@ -269,10 +269,10 @@ protected:
virtual bool visit(ArrayDeclaratorAST *ast); virtual bool visit(ArrayDeclaratorAST *ast);
private: private:
Scope *_currentScope; Scope *_scope;
ExpressionTy _currentExpression; ExpressionTy _expression;
const Name *_currentName; const Name *_name;
FullySpecifiedType _currentType; FullySpecifiedType _type;
}; };
} // end of namespace CPlusPlus } // end of namespace CPlusPlus