* Variables */
| 308 | * Variables |
| 309 | */ |
| 310 | void DeclarationBuilder::inferArgumentsFromCall(QmlJS::AST::Node* base, QmlJS::AST::ArgumentList* arguments) |
| 311 | { |
| 312 | ContextBuilder::ExpressionType expr = findType(base); |
| 313 | auto func_type = expr.type.dynamicCast<QmlJS::FunctionType>(); |
| 314 | DUChainWriteLocker lock; |
| 315 | |
| 316 | if (!func_type) { |
| 317 | return; |
| 318 | } |
| 319 | |
| 320 | auto func_declaration = dynamic_cast<FunctionDeclaration*>(func_type->declaration(topContext())); |
| 321 | |
| 322 | if (!func_declaration || !func_declaration->internalContext()) { |
| 323 | return; |
| 324 | } |
| 325 | |
| 326 | // Put the argument nodes in a list that has a definite size |
| 327 | QVector<Declaration *> argumentDecls = func_declaration->internalContext()->localDeclarations(); |
| 328 | QVector<QmlJS::AST::ArgumentList *> args; |
| 329 | |
| 330 | for (auto argument = arguments; argument; argument = argument->next) { |
| 331 | args.append(argument); |
| 332 | } |
| 333 | |
| 334 | // Don't update a function when it is called with the wrong number |
| 335 | // of arguments |
| 336 | if (args.size() != argumentDecls.count()) { |
| 337 | return; |
| 338 | } |
| 339 | |
| 340 | // Update the types of the function arguments |
| 341 | QmlJS::FunctionType::Ptr new_func_type(new QmlJS::FunctionType); |
| 342 | |
| 343 | for (int i=0; i<args.size(); ++i) { |
| 344 | QmlJS::AST::ArgumentList *argument = args.at(i); |
| 345 | AbstractType::Ptr current_type = argumentDecls.at(i)->abstractType(); |
| 346 | |
| 347 | // Merge the current type of the argument with its type in the call expression |
| 348 | AbstractType::Ptr call_type = findType(argument->expression).type; |
| 349 | AbstractType::Ptr new_type = QmlJS::mergeTypes(current_type, call_type); |
| 350 | |
| 351 | // Update the declaration of the argument and its type in the function type |
| 352 | if (func_declaration->topContext() == topContext()) { |
| 353 | new_func_type->addArgument(new_type); |
| 354 | argumentDecls.at(i)->setAbstractType(new_type); |
| 355 | } |
| 356 | |
| 357 | // Add a warning if it is possible that the argument types don't match |
| 358 | if (!m_prebuilding && !areTypesEqual(current_type, call_type)) { |
| 359 | m_session->addProblem(argument, i18n( |
| 360 | "Possible type mismatch between the argument type (%1) and the value passed as argument (%2)", |
| 361 | current_type->toString(), |
| 362 | call_type->toString() |
| 363 | ), IProblem::Hint); |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | // Replace the function's type with the new type having updated arguments |
nothing calls this directly
no test coverage detected