Files
qt-creator/src/libs/cplusplus/ResolveExpression.cpp

789 lines
24 KiB
C++
Raw Normal View History

/**************************************************************************
2008-12-02 12:01:29 +01:00
**
** This file is part of Qt Creator
**
2011-01-11 16:28:15 +01:00
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
2008-12-02 12:01:29 +01:00
**
2011-04-13 08:42:33 +02:00
** Contact: Nokia Corporation (info@qt.nokia.com)
2008-12-02 12:01:29 +01:00
**
**
** GNU Lesser General Public License Usage
**
2011-04-13 08:42:33 +02:00
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this file.
** Please review the following information to ensure the GNU Lesser General
** Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
2010-12-17 16:01:08 +01:00
** In addition, as a special exception, Nokia gives you certain additional
2011-04-13 08:42:33 +02:00
** rights. These rights are described in the Nokia Qt LGPL Exception
2010-12-17 16:01:08 +01:00
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
2011-04-13 08:42:33 +02:00
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
2010-12-17 16:01:08 +01:00
** If you have questions regarding the use of this file, please contact
** Nokia at info@qt.nokia.com.
2008-12-02 12:01:29 +01:00
**
**************************************************************************/
2008-12-02 12:01:29 +01:00
#include "ResolveExpression.h"
2010-05-05 12:06:38 +02:00
#include "LookupContext.h"
2008-12-02 12:01:29 +01:00
#include "Overview.h"
#include "DeprecatedGenTemplateInstance.h"
#include "CppRewriter.h"
2008-12-02 12:01:29 +01:00
#include <Control.h>
#include <AST.h>
#include <Scope.h>
#include <Names.h>
#include <Symbols.h>
#include <Literals.h>
#include <CoreTypes.h>
#include <TypeVisitor.h>
#include <NameVisitor.h>
2008-12-02 14:09:21 +01:00
2008-12-09 15:25:01 +01:00
#include <QtCore/QList>
#include <QtCore/QtDebug>
2008-12-02 12:01:29 +01:00
using namespace CPlusPlus;
2008-12-09 15:25:01 +01:00
namespace {
2008-12-02 12:01:29 +01:00
2009-10-05 11:38:54 +02:00
template <typename _Tp>
static QList<_Tp> removeDuplicates(const QList<_Tp> &results)
{
QList<_Tp> uniqueList;
QSet<_Tp> processed;
foreach (const _Tp &r, results) {
if (processed.contains(r))
continue;
processed.insert(r);
uniqueList.append(r);
}
return uniqueList;
}
2008-12-02 12:01:29 +01:00
} // end of anonymous namespace
/////////////////////////////////////////////////////////////////////
// ResolveExpression
/////////////////////////////////////////////////////////////////////
ResolveExpression::ResolveExpression(const LookupContext &context)
: ASTVisitor(context.expressionDocument()->translationUnit()),
_scope(0),
2008-12-02 12:01:29 +01:00
_context(context),
bind(context.expressionDocument()->translationUnit()),
_reference(false)
{ }
2008-12-02 12:01:29 +01:00
ResolveExpression::~ResolveExpression()
{ }
QList<LookupItem> ResolveExpression::operator()(ExpressionAST *ast, Scope *scope)
{ return resolve(ast, scope); }
QList<LookupItem> ResolveExpression::reference(ExpressionAST *ast, Scope *scope)
{ return resolve(ast, scope, true); }
QList<LookupItem> ResolveExpression::resolve(ExpressionAST *ast, Scope *scope, bool ref)
{
2010-08-11 12:26:02 +02:00
if (! scope)
return QList<LookupItem>();
std::swap(_scope, scope);
std::swap(_reference, ref);
const QList<LookupItem> result = expression(ast);
std::swap(_reference, ref);
std::swap(_scope, scope);
return result;
}
QList<LookupItem> ResolveExpression::expression(ExpressionAST *ast)
2008-12-02 12:01:29 +01:00
{
const QList<LookupItem> previousResults = switchResults(QList<LookupItem>());
2008-12-02 12:01:29 +01:00
accept(ast);
2009-10-05 11:38:54 +02:00
return removeDuplicates(switchResults(previousResults));
2008-12-02 12:01:29 +01:00
}
QList<LookupItem> ResolveExpression::switchResults(const QList<LookupItem> &results)
2008-12-02 12:01:29 +01:00
{
const QList<LookupItem> previousResults = _results;
2008-12-02 12:01:29 +01:00
_results = results;
return previousResults;
}
void ResolveExpression::addResults(const QList<Symbol *> &symbols)
2008-12-02 12:01:29 +01:00
{
foreach (Symbol *symbol, symbols) {
LookupItem item;
item.setType(symbol->type());
item.setScope(symbol->enclosingScope());
item.setDeclaration(symbol);
_results.append(item);
}
2008-12-02 12:01:29 +01:00
}
void ResolveExpression::addResults(const QList<LookupItem> &items)
{
_results += items;
}
void ResolveExpression::addResult(const FullySpecifiedType &ty, Scope *scope)
2008-12-02 12:01:29 +01:00
{
LookupItem item;
item.setType(ty);
item.setScope(scope);
2008-12-02 12:01:29 +01:00
_results.append(item);
2010-05-05 12:06:38 +02:00
}
2008-12-02 12:01:29 +01:00
2010-08-02 12:04:59 +02:00
bool ResolveExpression::visit(IdExpressionAST *ast)
{
accept(ast->name);
return false;
}
bool ResolveExpression::visit(BinaryExpressionAST *ast)
2008-12-02 12:01:29 +01:00
{
if (tokenKind(ast->binary_op_token) == T_COMMA && ast->right_expression && ast->right_expression->asQtMethod() != 0) {
if (ast->left_expression && ast->left_expression->asQtMethod() != 0)
thisObject();
else
accept(ast->left_expression);
QtMethodAST *qtMethod = ast->right_expression->asQtMethod();
if (DeclaratorAST *d = qtMethod->declarator) {
if (d->core_declarator) {
2010-05-12 15:25:16 +02:00
if (DeclaratorIdAST *declaratorId = d->core_declarator->asDeclaratorId()) {
if (NameAST *nameAST = declaratorId->name) {
if (ClassOrNamespace *binding = baseExpression(_results, T_ARROW)) {
_results.clear();
addResults(binding->lookup(nameAST->name));
}
}
}
}
}
return false;
}
accept(ast->left_expression);
2008-12-02 12:01:29 +01:00
return false;
}
bool ResolveExpression::visit(CastExpressionAST *ast)
{
2010-08-11 12:26:02 +02:00
Scope *dummyScope = _context.expressionDocument()->globalNamespace();
2010-08-13 16:17:44 +02:00
FullySpecifiedType ty = bind(ast->type_id, dummyScope);
addResult(ty, _scope);
2008-12-02 12:01:29 +01:00
return false;
}
bool ResolveExpression::visit(ConditionAST *)
{
// nothing to do.
return false;
}
bool ResolveExpression::visit(ConditionalExpressionAST *ast)
2008-12-02 12:01:29 +01:00
{
if (ast->left_expression)
accept(ast->left_expression);
else if (ast->right_expression)
accept(ast->right_expression);
2008-12-02 12:01:29 +01:00
return false;
}
bool ResolveExpression::visit(CppCastExpressionAST *ast)
{
2010-08-11 12:26:02 +02:00
Scope *dummyScope = _context.expressionDocument()->globalNamespace();
2010-08-13 16:17:44 +02:00
FullySpecifiedType ty = bind(ast->type_id, dummyScope);
addResult(ty, _scope);
2008-12-02 12:01:29 +01:00
return false;
}
bool ResolveExpression::visit(DeleteExpressionAST *)
{
FullySpecifiedType ty(control()->voidType());
addResult(ty, _scope);
2008-12-02 12:01:29 +01:00
return false;
}
bool ResolveExpression::visit(ArrayInitializerAST *)
{
// nothing to do.
return false;
}
bool ResolveExpression::visit(NewExpressionAST *ast)
2008-12-02 12:01:29 +01:00
{
if (ast->new_type_id) {
2010-08-11 12:26:02 +02:00
Scope *dummyScope = _context.expressionDocument()->globalNamespace();
2010-08-13 16:17:44 +02:00
FullySpecifiedType ty = bind(ast->new_type_id, dummyScope);
FullySpecifiedType ptrTy(control()->pointerType(ty));
addResult(ptrTy, _scope);
}
2008-12-02 12:01:29 +01:00
// nothing to do.
return false;
}
bool ResolveExpression::visit(TypeidExpressionAST *)
{
const Name *stdName = control()->identifier("std");
const Name *tiName = control()->identifier("type_info");
const Name *q = control()->qualifiedNameId(control()->qualifiedNameId(/* :: */ 0, stdName), tiName);
2008-12-02 12:01:29 +01:00
FullySpecifiedType ty(control()->namedType(q));
addResult(ty, _scope);
2008-12-02 12:01:29 +01:00
return false;
}
bool ResolveExpression::visit(TypenameCallExpressionAST *)
{
// nothing to do
return false;
}
bool ResolveExpression::visit(TypeConstructorCallAST *)
{
// nothing to do.
return false;
}
bool ResolveExpression::visit(SizeofExpressionAST *)
{
FullySpecifiedType ty(control()->integerType(IntegerType::Int));
ty.setUnsigned(true);
addResult(ty, _scope);
2008-12-02 12:01:29 +01:00
return false;
}
2009-07-10 17:07:47 +02:00
bool ResolveExpression::visit(NumericLiteralAST *ast)
2008-12-02 12:01:29 +01:00
{
const Token &tk = tokenAt(ast->literal_token);
2009-07-10 17:07:47 +02:00
Type *type = 0;
bool isUnsigned = false;
2009-07-10 17:07:47 +02:00
if (tk.is(T_CHAR_LITERAL))
2009-07-10 17:07:47 +02:00
type = control()->integerType(IntegerType::Char);
else if (tk.is(T_WIDE_CHAR_LITERAL))
2009-07-10 17:07:47 +02:00
type = control()->integerType(IntegerType::WideChar);
else if (const NumericLiteral *literal = numericLiteral(ast->literal_token)) {
isUnsigned = literal->isUnsigned();
if (literal->isInt())
type = control()->integerType(IntegerType::Int);
else if (literal->isLong())
type = control()->integerType(IntegerType::Long);
else if (literal->isLongLong())
type = control()->integerType(IntegerType::LongLong);
else if (literal->isFloat())
type = control()->floatType(FloatType::Float);
else if (literal->isDouble())
type = control()->floatType(FloatType::Double);
else if (literal->isLongDouble())
type = control()->floatType(FloatType::LongDouble);
else
type = control()->integerType(IntegerType::Int);
}
2009-07-10 17:07:47 +02:00
FullySpecifiedType ty(type);
ty.setUnsigned(isUnsigned);
addResult(ty, _scope);
2008-12-02 12:01:29 +01:00
return false;
}
bool ResolveExpression::visit(BoolLiteralAST *)
{
FullySpecifiedType ty(control()->integerType(IntegerType::Bool));
addResult(ty, _scope);
2008-12-02 12:01:29 +01:00
return false;
}
bool ResolveExpression::visit(ThisExpressionAST *)
{
thisObject();
return false;
}
void ResolveExpression::thisObject()
2008-12-02 12:01:29 +01:00
{
2010-05-05 12:06:38 +02:00
Scope *scope = _scope;
for (; scope; scope = scope->enclosingScope()) {
2010-08-11 12:26:02 +02:00
if (Function *fun = scope->asFunction()) {
if (Class *klass = scope->enclosingClass()) {
2008-12-02 12:01:29 +01:00
FullySpecifiedType classTy(control()->namedType(klass->name()));
FullySpecifiedType ptrTy(control()->pointerType(classTy));
addResult(ptrTy, fun->enclosingScope());
2008-12-02 12:01:29 +01:00
break;
2009-12-01 12:46:15 +01:00
} else if (const QualifiedNameId *q = fun->name()->asQualifiedNameId()) {
if (q->base()) {
FullySpecifiedType classTy(control()->namedType(q->base()));
FullySpecifiedType ptrTy(control()->pointerType(classTy));
addResult(ptrTy, fun->enclosingScope());
}
2008-12-02 12:01:29 +01:00
break;
}
}
}
}
bool ResolveExpression::visit(CompoundExpressionAST *ast)
{
CompoundStatementAST *cStmt = ast->statement;
if (cStmt && cStmt->statement_list) {
accept(cStmt->statement_list->lastValue());
}
return false;
}
2008-12-02 12:01:29 +01:00
bool ResolveExpression::visit(NestedExpressionAST *ast)
{
accept(ast->expression);
return false;
}
bool ResolveExpression::visit(StringLiteralAST *)
{
FullySpecifiedType charTy = control()->integerType(IntegerType::Char);
charTy.setConst(true);
FullySpecifiedType ty(control()->pointerType(charTy));
addResult(ty, _scope);
2008-12-02 12:01:29 +01:00
return false;
}
bool ResolveExpression::visit(ThrowExpressionAST *)
{
return false;
}
bool ResolveExpression::visit(TypeIdAST *)
{
return false;
}
bool ResolveExpression::visit(UnaryExpressionAST *ast)
{
accept(ast->expression);
unsigned unaryOp = tokenKind(ast->unary_op_token);
if (unaryOp == T_AMPER) {
QMutableListIterator<LookupItem > it(_results);
2008-12-02 12:01:29 +01:00
while (it.hasNext()) {
LookupItem p = it.next();
FullySpecifiedType ty = p.type();
ty.setType(control()->pointerType(ty));
p.setType(ty);
2008-12-02 12:01:29 +01:00
it.setValue(p);
}
} else if (unaryOp == T_STAR) {
QMutableListIterator<LookupItem > it(_results);
2008-12-02 12:01:29 +01:00
while (it.hasNext()) {
LookupItem p = it.next();
if (PointerType *ptrTy = p.type()->asPointerType()) {
p.setType(ptrTy->elementType());
2008-12-02 12:01:29 +01:00
it.setValue(p);
} else {
it.remove();
}
}
}
return false;
}
2009-01-13 16:03:46 +01:00
bool ResolveExpression::visit(CompoundLiteralAST *ast)
{
accept(ast->type_id);
return false;
}
2008-12-02 12:01:29 +01:00
bool ResolveExpression::visit(QualifiedNameAST *ast)
{
2010-05-05 12:06:38 +02:00
if (const Name *name = ast->name) {
const QList<LookupItem> candidates = _context.lookup(name, _scope);
addResults(candidates);
2008-12-02 12:01:29 +01:00
}
return false;
}
2010-05-05 12:06:38 +02:00
bool ResolveExpression::visit(SimpleNameAST *ast)
2008-12-02 12:01:29 +01:00
{
const QList<LookupItem> candidates = _context.lookup(ast->name, _scope);
addResults(candidates);
2008-12-02 12:01:29 +01:00
return false;
}
2010-05-05 12:06:38 +02:00
bool ResolveExpression::visit(TemplateIdAST *ast)
2008-12-02 12:01:29 +01:00
{
const QList<LookupItem> candidates = _context.lookup(ast->name, _scope);
addResults(candidates);
2008-12-02 12:01:29 +01:00
return false;
}
bool ResolveExpression::visit(DestructorNameAST *)
{
FullySpecifiedType ty(control()->voidType());
addResult(ty, _scope);
2008-12-02 12:01:29 +01:00
return false;
}
2010-05-05 12:06:38 +02:00
bool ResolveExpression::visit(OperatorFunctionIdAST *)
2008-12-02 12:01:29 +01:00
{
2010-05-05 12:06:38 +02:00
return false;
}
2008-12-02 12:01:29 +01:00
2010-05-05 12:06:38 +02:00
bool ResolveExpression::visit(ConversionFunctionIdAST *)
{
2008-12-02 12:01:29 +01:00
return false;
}
bool ResolveExpression::maybeValidPrototype(Function *funTy, unsigned actualArgumentCount)
{
return funTy->maybeValidPrototype(actualArgumentCount);
}
bool ResolveExpression::implicitConversion(const FullySpecifiedType &sourceTy, const FullySpecifiedType &targetTy) const
{
if (sourceTy.isEqualTo(targetTy))
return true;
else if (sourceTy.simplified().isEqualTo(targetTy.simplified()))
return true;
return false;
}
2008-12-02 12:01:29 +01:00
bool ResolveExpression::visit(CallAST *ast)
{
const QList<LookupItem> baseResults = resolve(ast->base_expression, _scope);
2008-12-02 12:01:29 +01:00
// Compute the types of the actual arguments.
unsigned actualArgumentCount = 0;
2008-12-02 12:01:29 +01:00
QList< QList<LookupItem> > arguments;
for (ExpressionListAST *exprIt = ast->expression_list; exprIt; exprIt = exprIt->next) {
if (_reference)
arguments.append(resolve(exprIt->value, _scope));
++actualArgumentCount;
}
2008-12-02 12:01:29 +01:00
if (_reference) {
_results.clear();
foreach (const LookupItem &base, baseResults) {
if (Function *funTy = base.type()->asFunctionType()) {
if (! maybeValidPrototype(funTy, actualArgumentCount))
continue;
int score = 0;
for (unsigned i = 0; i < funTy->argumentCount(); ++i) {
const FullySpecifiedType formalTy = funTy->argumentAt(i)->type();
FullySpecifiedType actualTy;
if (i < unsigned(arguments.size())) {
const QList<LookupItem> actual = arguments.at(i);
if (actual.isEmpty())
continue;
actualTy = actual.first().type();
} else
actualTy = formalTy;
if (implicitConversion(actualTy, formalTy))
++score;
}
if (score)
_results.prepend(base);
else
_results.append(base);
}
}
if (_results.isEmpty())
_results = baseResults;
return false;
}
2009-12-01 12:46:15 +01:00
const Name *functionCallOp = control()->operatorNameId(OperatorNameId::FunctionCallOp);
foreach (const LookupItem &result, baseResults) {
FullySpecifiedType ty = result.type().simplified();
Scope *scope = result.scope();
if (NamedType *namedTy = ty->asNamedType()) {
if (ClassOrNamespace *b = _context.lookupType(namedTy->name(), scope)) {
foreach (const LookupItem &r, b->find(functionCallOp)) {
Symbol *overload = r.declaration();
2010-05-05 12:06:38 +02:00
if (Function *funTy = overload->type()->asFunctionType()) {
if (maybeValidPrototype(funTy, actualArgumentCount)) {
if (Function *proto = instantiate(namedTy->name(), funTy)->asFunctionType())
addResult(proto->returnType().simplified(), scope);
2010-05-05 12:06:38 +02:00
}
}
}
2008-12-02 12:01:29 +01:00
}
} else if (Function *funTy = ty->asFunctionType()) {
if (maybeValidPrototype(funTy, actualArgumentCount))
addResult(funTy->returnType().simplified(), scope);
} else if (Class *classTy = ty->asClassType()) {
2008-12-02 12:01:29 +01:00
// Constructor call
FullySpecifiedType ctorTy = control()->namedType(classTy->name());
addResult(ctorTy, scope);
2008-12-02 12:01:29 +01:00
}
}
return false;
}
bool ResolveExpression::visit(ArrayAccessAST *ast)
{
const QList<LookupItem> baseResults = resolve(ast->base_expression, _scope);
const QList<LookupItem> indexResults = resolve(ast->expression, _scope);
2008-12-02 12:01:29 +01:00
2009-12-01 12:46:15 +01:00
const Name *arrayAccessOp = control()->operatorNameId(OperatorNameId::ArrayAccessOp);
2008-12-02 12:01:29 +01:00
foreach (const LookupItem &result, baseResults) {
FullySpecifiedType ty = result.type().simplified();
Scope *scope = result.scope();
2008-12-02 12:01:29 +01:00
if (PointerType *ptrTy = ty->asPointerType()) {
addResult(ptrTy->elementType().simplified(), scope);
2008-12-02 12:01:29 +01:00
} else if (ArrayType *arrTy = ty->asArrayType()) {
addResult(arrTy->elementType().simplified(), scope);
2008-12-02 12:01:29 +01:00
} else if (NamedType *namedTy = ty->asNamedType()) {
if (ClassOrNamespace *b = _context.lookupType(namedTy->name(), scope)) {
foreach (const LookupItem &r, b->find(arrayAccessOp)) {
Symbol *overload = r.declaration();
2010-05-05 12:06:38 +02:00
if (Function *funTy = overload->type()->asFunctionType()) {
if (Function *proto = instantiate(namedTy->name(), funTy)->asFunctionType())
// ### TODO: check the actual arguments
addResult(proto->returnType().simplified(), scope);
}
2008-12-02 12:01:29 +01:00
}
2010-05-05 12:06:38 +02:00
2008-12-02 12:01:29 +01:00
}
}
}
return false;
}
QList<LookupItem> ResolveExpression::getMembers(ClassOrNamespace *binding, const Name *memberName) const
{
2010-08-11 15:48:21 +02:00
Q_UNUSED(binding);
Q_UNUSED(memberName);
// ### port me
QList<LookupItem> members;
2010-08-11 12:47:28 +02:00
#if 0
const QList<LookupItem> originalMembers = binding->find(memberName);
foreach (const LookupItem &m, originalMembers) {
if (! m.binding() || ! m.binding()->templateId()) {
members.append(m);
continue;
}
Symbol *decl = m.declaration();
2010-08-11 12:26:02 +02:00
if (Class *klass = decl->scope()->asClass()) {
if (klass->templateParameters() != 0) {
SubstitutionMap map;
const TemplateNameId *templateId = m.binding()->templateId();
unsigned count = qMin(klass->templateParameterCount(), templateId->templateArgumentCount());
for (unsigned i = 0; i < count; ++i) {
map.bind(klass->templateParameterAt(i)->name(), templateId->templateArgumentAt(i));
}
SubstitutionEnvironment env;
if (m.scope())
env.switchScope(m.scope());
env.setContext(_context);
env.enter(&map);
FullySpecifiedType instantiatedTy = rewriteType(decl->type(), &env, _context.control().data());
Overview oo;
oo.setShowReturnTypes(true);
oo.setShowFunctionSignatures(true);
qDebug() << "original:" << oo(decl->type(), decl->name()) << "inst:" << oo(instantiatedTy, decl->name());
LookupItem newItem;
newItem = m;
newItem.setType(instantiatedTy);
members.append(newItem);
}
}
}
2010-08-11 12:47:28 +02:00
#endif
return members;
}
2008-12-02 12:01:29 +01:00
bool ResolveExpression::visit(MemberAccessAST *ast)
{
// The candidate types for the base expression are stored in
// _results.
const QList<LookupItem> baseResults = resolve(ast->base_expression, _scope);
2008-12-02 12:01:29 +01:00
// Evaluate the expression-id that follows the access operator.
2009-12-01 12:46:15 +01:00
const Name *memberName = 0;
2009-06-17 11:11:21 +02:00
if (ast->member_name)
memberName = ast->member_name->name;
2008-12-02 12:01:29 +01:00
// Remember the access operator.
const int accessOp = tokenKind(ast->access_token);
2008-12-02 12:01:29 +01:00
2010-05-12 15:25:16 +02:00
if (ClassOrNamespace *binding = baseExpression(baseResults, accessOp))
addResults(binding->lookup(memberName));
2008-12-02 12:01:29 +01:00
return false;
}
2010-05-12 15:25:16 +02:00
ClassOrNamespace *ResolveExpression::findClass(const FullySpecifiedType &originalTy, Scope *scope) const
{
FullySpecifiedType ty = originalTy.simplified();
ClassOrNamespace *binding = 0;
2010-05-12 15:25:16 +02:00
if (Class *klass = ty->asClassType())
binding = _context.lookupType(klass);
2010-05-12 15:25:16 +02:00
else if (NamedType *namedTy = ty->asNamedType())
binding = _context.lookupType(namedTy->name(), scope);
2010-05-12 15:25:16 +02:00
else if (Function *funTy = ty->asFunctionType())
return findClass(funTy->returnType(), scope);
2010-05-12 15:25:16 +02:00
return binding;
}
2010-05-12 14:52:24 +02:00
ClassOrNamespace *ResolveExpression::baseExpression(const QList<LookupItem> &baseResults,
int accessOp,
bool *replacedDotOperator) const
{
foreach (const LookupItem &r, baseResults) {
FullySpecifiedType ty = r.type().simplified();
2010-05-12 15:25:16 +02:00
Scope *scope = r.scope();
2010-05-12 14:52:24 +02:00
2010-05-12 15:25:16 +02:00
if (accessOp == T_ARROW) {
2010-05-12 14:52:24 +02:00
if (PointerType *ptrTy = ty->asPointerType()) {
2010-05-12 15:25:16 +02:00
if (ClassOrNamespace *binding = findClass(ptrTy->elementType(), scope))
return binding;
} else if (ClassOrNamespace *binding = findClass(ty, scope)) {
2010-05-12 15:25:16 +02:00
// lookup for overloads of operator->
2010-05-12 15:25:16 +02:00
const OperatorNameId *arrowOp = control()->operatorNameId(OperatorNameId::ArrowOp);
foreach (const LookupItem &r, binding->find(arrowOp)) {
Symbol *overload = r.declaration();
if (! overload)
continue;
2010-05-12 14:52:24 +02:00
2010-05-14 09:52:53 +02:00
if (overload->type()->isFunctionType()) {
FullySpecifiedType overloadTy = instantiate(binding->templateId(), overload);
2010-05-12 16:04:43 +02:00
Function *instantiatedFunction = overloadTy->asFunctionType();
Q_ASSERT(instantiatedFunction != 0);
FullySpecifiedType retTy = instantiatedFunction->returnType().simplified();
if (PointerType *ptrTy = retTy->asPointerType()) {
if (ClassOrNamespace *retBinding = findClass(ptrTy->elementType(), overload->enclosingScope()))
2010-05-12 16:04:43 +02:00
return retBinding;
2010-05-12 15:25:16 +02:00
else if (scope != overload->enclosingScope()) {
if (ClassOrNamespace *retBinding = findClass(ptrTy->elementType(), scope))
return retBinding;
}
2010-05-12 16:04:43 +02:00
}
}
2010-05-12 15:25:16 +02:00
}
2010-05-12 14:52:24 +02:00
}
} else if (accessOp == T_DOT) {
if (replacedDotOperator) {
if (PointerType *ptrTy = ty->asPointerType()) {
// replace . with ->
ty = ptrTy->elementType();
*replacedDotOperator = true;
}
2010-05-12 15:25:16 +02:00
}
if (ClassOrNamespace *binding = findClass(ty, scope))
return binding;
}
2010-05-12 14:52:24 +02:00
}
return 0;
}
2010-05-05 12:06:38 +02:00
FullySpecifiedType ResolveExpression::instantiate(const Name *className, Symbol *candidate) const
{
return DeprecatedGenTemplateInstance::instantiate(className, candidate, _context.control());
2010-05-05 12:06:38 +02:00
}
bool ResolveExpression::visit(PostIncrDecrAST *ast)
2008-12-02 12:01:29 +01:00
{
const QList<LookupItem> baseResults = resolve(ast->base_expression, _scope);
_results = baseResults;
2008-12-02 12:01:29 +01:00
return false;
}
2008-12-29 11:53:40 +01:00
bool ResolveExpression::visit(ObjCMessageExpressionAST *ast)
{
const QList<LookupItem> receiverResults = resolve(ast->receiver_expression, _scope);
foreach (const LookupItem &result, receiverResults) {
FullySpecifiedType ty = result.type().simplified();
ClassOrNamespace *binding = 0;
if (ObjCClass *clazz = ty->asObjCClassType()) {
// static access, e.g.:
// [NSObject description];
binding = _context.lookupType(clazz);
} else if (PointerType *ptrTy = ty->asPointerType()) {
if (NamedType *namedTy = ptrTy->elementType()->asNamedType()) {
// dynamic access, e.g.:
// NSObject *obj = ...; [obj release];
binding = _context.lookupType(namedTy->name(), result.scope());
}
}
if (binding) {
foreach (const LookupItem &r, binding->lookup(ast->selector->name)) {
Symbol *s = r.declaration();
if (ObjCMethod *m = s->asObjCMethod())
addResult(m->returnType(), result.scope());
}
}
}
return false;
}
2010-09-22 11:38:04 +02:00
const LookupContext &ResolveExpression::context() const
{
return _context;
}