@@ -3685,12 +3685,9 @@ ExpandMacroExpansionDeclRequest::evaluate(Evaluator &evaluator,
36853685 }
36863686 // Resolve macro candidates.
36873687 MacroDecl *macro;
3688- if (auto *args = MED->getArgs ()) {
3689- macro = evaluateOrDefault (
3690- ctx.evaluator , ResolveMacroRequest{MED, MacroRole::Declaration, dc},
3691- nullptr );
3692- }
3693- else {
3688+ // Non-call declaration macros cannot be overloaded.
3689+ auto *args = MED->getArgs ();
3690+ if (!args) {
36943691 if (foundMacros.size () > 1 ) {
36953692 MED->diagnose (diag::ambiguous_decl_ref, MED->getMacro ())
36963693 .highlight (MED->getMacroLoc ().getSourceRange ());
@@ -3700,8 +3697,63 @@ ExpandMacroExpansionDeclRequest::evaluate(Evaluator &evaluator,
37003697 }
37013698 macro = foundMacros.front ();
37023699 }
3703- if (!macro)
3704- return {};
3700+ // Call-like macros need to be resolved.
3701+ else {
3702+ using namespace constraints ;
3703+ ConstraintSystem cs (dc, ConstraintSystemOptions ());
3704+ // Type-check macro arguments.
3705+ for (auto *arg : args->getArgExprs ())
3706+ cs.setType (arg, TypeChecker::typeCheckExpression (arg, dc));
3707+ auto choices = map<SmallVector<OverloadChoice, 1 >>(
3708+ foundMacros,
3709+ [](MacroDecl *md) {
3710+ return OverloadChoice (Type (), md, FunctionRefKind::SingleApply);
3711+ });
3712+ auto locator = cs.getConstraintLocator (MED);
3713+ auto macroRefType = Type (cs.createTypeVariable (locator, 0 ));
3714+ cs.addOverloadSet (macroRefType, choices, dc, locator);
3715+ if (MED->getGenericArgsRange ().isValid ()) {
3716+ // FIXME: Deal with generic args.
3717+ MED->diagnose (diag::macro_unsupported);
3718+ return {};
3719+ }
3720+ auto getMatchingParams = [&](
3721+ ArgumentList *argList,
3722+ SmallVectorImpl<AnyFunctionType::Param> &result) {
3723+ for (auto arg : *argList) {
3724+ ParameterTypeFlags flags;
3725+ auto ty = cs.getType (arg.getExpr ()); if (arg.isInOut ()) {
3726+ ty = ty->getInOutObjectType ();
3727+ flags = flags.withInOut (true );
3728+ }
3729+ if (arg.isConst ()) {
3730+ flags = flags.withCompileTimeConst (true );
3731+ }
3732+ result.emplace_back (ty, arg.getLabel (), flags);
3733+ }
3734+ };
3735+ SmallVector<AnyFunctionType::Param, 8 > params;
3736+ getMatchingParams (args, params);
3737+ cs.associateArgumentList (locator, args);
3738+ cs.addConstraint (
3739+ ConstraintKind::ApplicableFunction,
3740+ FunctionType::get (params, ctx.getVoidType ()),
3741+ macroRefType,
3742+ cs.getConstraintLocator (MED, ConstraintLocator::ApplyFunction));
3743+ // Solve.
3744+ auto solution = cs.solveSingle ();
3745+ if (!solution) {
3746+ MED->diagnose (diag::no_overloads_match_exactly_in_call,
3747+ /* reference|call*/ false , DescriptiveDeclKind::Macro,
3748+ false , MED->getMacro ().getBaseName ())
3749+ .highlight (MED->getMacroLoc ().getSourceRange ());
3750+ for (auto *candidate : foundMacros)
3751+ candidate->diagnose (diag::found_candidate);
3752+ return {};
3753+ }
3754+ auto choice = solution->getOverloadChoice (locator).choice ;
3755+ macro = cast<MacroDecl>(choice.getDecl ());
3756+ }
37053757 MED->setMacroRef (macro);
37063758
37073759 // Expand the macro.
0 commit comments