1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
|
/*************************************************************************************
* Copyright (C) 2013 by Andrea Scarpino <scarpino@kde.org> *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; either version 2 *
* of the License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the Free Software *
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA *
*************************************************************************************/
#include "expressionvisitor.h"
#include <language/duchain/topducontext.h>
#include <language/duchain/declaration.h>
#include <language/duchain/persistentsymboltable.h>
#include <language/duchain/duchainlock.h>
#include <language/duchain/types/structuretype.h>
#include <util/path.h>
#include "helper.h"
#include "functiontype.h"
#include "parsesession.h"
#include "frameworks/nodejs.h"
#include <cmath>
using namespace KDevelop;
ExpressionVisitor::ExpressionVisitor(DUContext* context)
: DynamicLanguageExpressionVisitor(context)
, m_prototypeDepth(0)
{
}
void ExpressionVisitor::postVisit(QmlJS::AST::Node* node)
{
// Each time a node is closed, decrement the prototype depth. This way,
// if a "prototype" node has been encountered, ExpressionVisitor can know
// whether it appeared at the top of the tree ("foo.bar.prototype") or
// somewhere else ("foo.prototype.bar").
--m_prototypeDepth;
QmlJS::AST::Visitor::postVisit(node);
}
bool ExpressionVisitor::isPrototype() const
{
return m_prototypeDepth == 1;
}
/*
* Literals
*/
bool ExpressionVisitor::visit(QmlJS::AST::NumericLiteral* node)
{
int num_int_digits = (int)std::log10(node->value) + 1;
encounter(
num_int_digits == (int)node->literalToken.length ?
IntegralType::TypeInt :
IntegralType::TypeDouble
);
return false;
}
bool ExpressionVisitor::visit(QmlJS::AST::StringLiteral*)
{
encounter(IntegralType::TypeString);
return false;
}
bool ExpressionVisitor::visit(QmlJS::AST::RegExpLiteral*)
{
encounter(QStringLiteral("RegExp"));
if (lastDeclaration()) {
instantiateCurrentDeclaration();
}
return false;
}
bool ExpressionVisitor::visit(QmlJS::AST::TrueLiteral*)
{
encounter(IntegralType::TypeBoolean);
return false;
}
bool ExpressionVisitor::visit(QmlJS::AST::FalseLiteral*)
{
encounter(IntegralType::TypeBoolean);
return false;
}
/*
* Object and arrays
*/
bool ExpressionVisitor::visit(QmlJS::AST::ArrayLiteral*)
{
encounter(AbstractType::Ptr(new IntegralType(IntegralType::TypeArray)));
return false;
}
bool ExpressionVisitor::visit(QmlJS::AST::ObjectLiteral* node)
{
encounterObjectAtLocation(node->lbraceToken);
return false;
}
bool ExpressionVisitor::visit(QmlJS::AST::ArrayMemberExpression* node)
{
// array["string_literal"] is equivalent to array.string_literal
auto literal = QmlJS::AST::cast<QmlJS::AST::StringLiteral*>(node->expression);
if (literal) {
node->base->accept(this);
encounterFieldMember(literal->value.toString());
}
return false;
}
bool ExpressionVisitor::visit(QmlJS::AST::FieldMemberExpression* node)
{
// Find the type of the base, and if this type has a declaration, use
// its inner context to get the type of the field member
node->base->accept(this);
encounterFieldMember(node->name.toString());
return false;
}
/*
* Identifiers and common expressions
*/
bool ExpressionVisitor::visit(QmlJS::AST::BinaryExpression* node)
{
switch (node->op) {
case QSOperator::BitAnd:
case QSOperator::BitOr:
case QSOperator::BitXor:
case QSOperator::LShift:
case QSOperator::RShift:
case QSOperator::URShift:
encounter(IntegralType::TypeInt);
break;
case QSOperator::And:
case QSOperator::Equal:
case QSOperator::Ge:
case QSOperator::Gt:
case QSOperator::In:
case QSOperator::InstanceOf:
case QSOperator::Le:
case QSOperator::Lt:
case QSOperator::Or:
case QSOperator::StrictEqual:
case QSOperator::StrictNotEqual:
encounter(IntegralType::TypeBoolean);
break;
case QSOperator::Assign:
node->right->accept(this);
break;
default:
encounterNothing();
break;
}
return false;
}
bool ExpressionVisitor::visit(QmlJS::AST::IdentifierExpression* node)
{
encounter(node->name.toString());
return false;
}
bool ExpressionVisitor::visit(QmlJS::AST::UiQualifiedId* node)
{
// "anchors.parent" results in an UiQualifiedId id having a "next" attribute.
// This node reprensents "anchors", the next one is for "parent"
encounter(node->name.toString());
for (node = node->next; node && lastDeclaration(); node = node->next) {
encounterFieldMember(node->name.toString());
}
return false;
}
bool ExpressionVisitor::visit(QmlJS::AST::ThisExpression* node)
{
Q_UNUSED(node)
DUChainReadLocker lock;
DUContext* paramsContext;
DUContext* internalContext;
Declaration* owner;
// "this" points to the current function (not semantically valid in JS,
// but this allows ExpressionVisitor to see the declarations of the
// function's prototype)
if (m_context->type() == DUContext::Other && // Code of the function
(paramsContext = m_context->parentContext()) && // Parameters of the function (this context has the function as owner)
(owner = QmlJS::getOwnerOfContext(paramsContext)) && // The function itself (owner of its parameters)
(internalContext = QmlJS::getInternalContext(DeclarationPointer(owner))) && // The prototype context of the function
(owner = QmlJS::getOwnerOfContext(internalContext)) && // The function that declared the prototype context (paramsContext may belong to a method of a class)
owner->abstractType()) {
encounterLvalue(DeclarationPointer(owner));
instantiateCurrentDeclaration();
} else {
encounterNothing();
}
return false;
}
/*
* Functions
*/
bool ExpressionVisitor::visit(QmlJS::AST::FunctionExpression* node)
{
encounterObjectAtLocation(node->lparenToken);
return false;
}
bool ExpressionVisitor::visit(QmlJS::AST::CallExpression* node)
{
// Special-case functions that have a specific meaning in some frameworks
auto functionIdentifier = QmlJS::AST::cast<QmlJS::AST::IdentifierExpression*>(node->base);
if (functionIdentifier &&
node->arguments && // One argument
!node->arguments->next && // But not two
functionIdentifier->name.toString() == QLatin1String("require")) {
auto moduleName = QmlJS::AST::cast<QmlJS::AST::StringLiteral*>(node->arguments->expression);
if (moduleName) {
encounterLvalue(QmlJS::NodeJS::instance().moduleExports(
moduleName->value.toString(),
m_context->topContext()->url()
));
} else {
encounterNothing();
}
return false;
}
// Find the type of the function called
node->base->accept(this);
QmlJS::FunctionType::Ptr func = QmlJS::FunctionType::Ptr::dynamicCast(m_lastType);
if (func && func->returnType()) {
encounter(func->returnType());
} else {
encounterNothing();
}
return false;
}
bool ExpressionVisitor::visit(QmlJS::AST::NewMemberExpression* node)
{
// Find the type of the function used as constructor, and build a
// StructureType representing an instance of this function.
node->base->accept(this);
if (lastDeclaration()) {
instantiateCurrentDeclaration();
} else {
encounterNothing();
}
return false;
}
void ExpressionVisitor::encounterNothing()
{
encounter(AbstractType::Ptr(), DeclarationPointer());
}
void ExpressionVisitor::encounter(IntegralType::CommonIntegralTypes type)
{
encounter(AbstractType::Ptr(new IntegralType(type)));
}
void ExpressionVisitor::encounter(const QString& declaration, KDevelop::DUContext* context)
{
QualifiedIdentifier name(declaration);
DUChainReadLocker lock;
if (!encounterParent(declaration) &&
!encounterDeclarationInContext(name, context) &&
!(!QmlJS::isQmlFile(m_context) && encounterDeclarationInNodeModule(name, QStringLiteral("__builtin_dom"))) &&
!encounterDeclarationInNodeModule(name, QStringLiteral("__builtin_ecmascript")) &&
!(context == nullptr && encounterGlobalDeclaration(name))) {
encounterNothing();
}
}
bool ExpressionVisitor::encounterParent(const QString& declaration)
{
if (declaration != QLatin1String("parent") ||
!QmlJS::isQmlFile(m_context)) {
return false;
}
// Go up until we find a class context (the enclosing QML component)
const DUContext* parent = m_context;
Declaration* owner;
while (parent && parent->type() != DUContext::Class) {
parent = parent->parentContext();
}
// Take the parent context of the current QML component, it is its parent
// component
if (parent) {
parent = parent->parentContext();
}
// Parent now points to the parent QML component. This is not always what
// the user wants when typing "parent", but already works well for
// "anchors.centerIn: parent" and things like that.
if (parent &&
(owner = QmlJS::getOwnerOfContext(parent)) &&
owner->abstractType()) {
encounterLvalue(DeclarationPointer(owner));
instantiateCurrentDeclaration();
return true;
}
return false;
}
bool ExpressionVisitor::encounterDeclarationInContext(const QualifiedIdentifier& id, DUContext* context)
{
DeclarationPointer dec = QmlJS::getDeclarationOrSignal(id,
context ? context : m_context,
context == nullptr);
if (dec) {
encounterLvalue(dec);
return true;
}
return false;
}
bool ExpressionVisitor::encounterDeclarationInNodeModule(const QualifiedIdentifier& id,
const QString& module)
{
return encounterDeclarationInContext(
id,
QmlJS::getInternalContext(
QmlJS::NodeJS::instance().moduleExports(module, m_context->url())
)
);
}
bool ExpressionVisitor::encounterGlobalDeclaration(const QualifiedIdentifier& id)
{
// Use the persistent symbol table to find this declaration, even if it is in another file
uint count = 0;
const IndexedDeclaration* declarations = nullptr;
PersistentSymbolTable::self().declarations(IndexedQualifiedIdentifier(id), count, declarations);
if (count && !m_currentDir.isValid()) {
m_currentDir = Path(m_context->topContext()->url().str()).parent();
}
// Explore the declarations and filter-out those that come from a file
// outside the current directory
for (uint i=0; i<count; ++i) {
const IndexedDeclaration& decl = declarations[i];
IndexedTopDUContext declTopContext = decl.indexedTopContext();
if (!declTopContext.isValid()) {
continue;
}
if (m_currentDir.isDirectParentOf(Path(declTopContext.url().str()))) {
encounterLvalue(DeclarationPointer(decl.declaration()));
return true;
}
}
return false;
}
void ExpressionVisitor::encounterFieldMember(const QString& name)
{
if (QmlJS::isPrototypeIdentifier(name)) {
// "prototype" is transparent: "object.prototype.foo" = "object.foo", and
// "function.prototype" should point to "function".
m_prototypeDepth = 2;
return;
}
DeclarationPointer declaration = lastDeclaration();
DUContext* context = QmlJS::getInternalContext(declaration);
if (context) {
encounter(name, context);
} else {
encounterNothing();
}
}
void ExpressionVisitor::encounterObjectAtLocation(const QmlJS::AST::SourceLocation& location)
{
DUChainReadLocker lock;
// Find the anonymous declaration corresponding to the function. This is
// the owner of the current context (function expressions create new contexts)
Declaration* dec = QmlJS::getOwnerOfContext(m_context->topContext()->findContextAt(
CursorInRevision(location.startLine-1, location.startColumn)
));
if (dec && dec->abstractType()) {
encounterLvalue(DeclarationPointer(dec));
} else {
encounterNothing();
}
}
void ExpressionVisitor::instantiateCurrentDeclaration()
{
StructureType::Ptr type = StructureType::Ptr(new StructureType);
DeclarationPointer decl = lastDeclaration();
{
DUChainReadLocker lock;
auto funcType = QmlJS::FunctionType::Ptr::dynamicCast(decl->abstractType());
if (funcType) {
decl = funcType->declaration(topContext());
}
type->setDeclaration(decl.data());
}
encounter(AbstractType::Ptr::staticCast(type), decl);
}
|