@@ -456,6 +456,35 @@ static std::string adjustMacroExpansionBufferName(StringRef name) {
456456 return result;
457457}
458458
459+ Optional<unsigned >
460+ ExpandMacroExpansionExprRequest::evaluate (Evaluator &evaluator,
461+ MacroExpansionExpr *mee) const {
462+ ConcreteDeclRef macroRef = mee->getMacroRef ();
463+ assert (macroRef && isa<MacroDecl>(macroRef.getDecl ()) &&
464+ " MacroRef should be set before expansion" );
465+
466+ auto *macro = cast<MacroDecl>(macroRef.getDecl ());
467+ if (macro->getMacroRoles ().contains (MacroRole::Expression)) {
468+ return expandMacroExpr (mee);
469+ }
470+ // For a non-expression macro, expand it as a declaration.
471+ else if (macro->getMacroRoles ().contains (MacroRole::Declaration) ||
472+ macro->getMacroRoles ().contains (MacroRole::CodeItem)) {
473+ if (!mee->getSubstituteDecl ()) {
474+ auto *med = mee->createSubstituteDecl ();
475+ TypeChecker::typeCheckDecl (med);
476+ }
477+ // Return the expanded buffer ID.
478+ return evaluateOrDefault (
479+ evaluator, ExpandMacroExpansionDeclRequest (mee->getSubstituteDecl ()),
480+ None);
481+ }
482+
483+ // Other macro roles may also be encountered here, as they use
484+ // `MacroExpansionExpr` for resolution. In those cases, do not expand.
485+ return None;
486+ }
487+
459488ArrayRef<unsigned > ExpandMemberAttributeMacros::evaluate (Evaluator &evaluator,
460489 Decl *decl) const {
461490 if (decl->isImplicit ())
@@ -666,22 +695,24 @@ static std::string expandMacroDefinition(
666695 return expandedResult;
667696}
668697
669- Expr * swift::expandMacroExpr (
670- DeclContext *dc, Expr *expr, ConcreteDeclRef macroRef, Type expandedType
671- ) {
698+ Optional< unsigned >
699+ swift::expandMacroExpr (MacroExpansionExpr *mee) {
700+ DeclContext *dc = mee-> getDeclContext ();
672701 ASTContext &ctx = dc->getASTContext ();
673702 SourceManager &sourceMgr = ctx.SourceMgr ;
703+ ConcreteDeclRef macroRef = mee->getMacroRef ();
704+ Type expandedType = mee->getType ();
674705
675706 auto moduleDecl = dc->getParentModule ();
676- auto sourceFile = moduleDecl->getSourceFileContainingLocation (expr ->getLoc ());
707+ auto sourceFile = moduleDecl->getSourceFileContainingLocation (mee ->getLoc ());
677708 if (!sourceFile)
678- return nullptr ;
709+ return None ;
679710
680711 MacroDecl *macro = cast<MacroDecl>(macroRef.getDecl ());
681712
682713 if (isFromExpansionOfMacro (sourceFile, macro, MacroRole::Expression)) {
683- ctx.Diags .diagnose (expr ->getLoc (), diag::macro_recursive, macro->getName ());
684- return nullptr ;
714+ ctx.Diags .diagnose (mee ->getLoc (), diag::macro_recursive, macro->getName ());
715+ return None ;
685716 }
686717
687718 // Evaluate the macro.
@@ -690,34 +721,33 @@ Expr *swift::expandMacroExpr(
690721 // / The discriminator used for the macro.
691722 LazyValue<std::string> discriminator ([&]() -> std::string {
692723#if SWIFT_SWIFT_PARSER
693- if (auto expansionExpr = dyn_cast<MacroExpansionExpr>(expr)) {
694- Mangle::ASTMangler mangler;
695- return mangler.mangleMacroExpansion (expansionExpr);
696- }
697- #endif
724+ Mangle::ASTMangler mangler;
725+ return mangler.mangleMacroExpansion (mee);
726+ #else
698727 return " " ;
728+ #endif
699729 });
700730
701731 auto macroDef = macro->getDefinition ();
702732 switch (macroDef.kind ) {
703733 case MacroDefinition::Kind::Undefined:
704734 case MacroDefinition::Kind::Invalid:
705735 // Already diagnosed as an error elsewhere.
706- return nullptr ;
736+ return None ;
707737
708738 case MacroDefinition::Kind::Builtin: {
709739 switch (macroDef.getBuiltinKind ()) {
710740 case BuiltinMacroKind::ExternalMacro:
711741 ctx.Diags .diagnose (
712- expr ->getLoc (), diag::external_macro_outside_macro_definition);
713- return nullptr ;
742+ mee ->getLoc (), diag::external_macro_outside_macro_definition);
743+ return None ;
714744 }
715745 }
716746
717747 case MacroDefinition::Kind::Expanded: {
718748 // Expand the definition with the given arguments.
719749 auto result = expandMacroDefinition (
720- macroDef.getExpanded (), macro, expr ->getArgs ());
750+ macroDef.getExpanded (), macro, mee ->getArgs ());
721751 evaluatedSource = llvm::MemoryBuffer::getMemBufferCopy (
722752 result, adjustMacroExpansionBufferName (*discriminator));
723753 break ;
@@ -732,41 +762,41 @@ Expr *swift::expandMacroExpr(
732762 auto externalDef = evaluateOrDefault (ctx.evaluator , request, None);
733763 if (!externalDef) {
734764 ctx.Diags .diagnose (
735- expr ->getLoc (), diag::external_macro_not_found,
765+ mee ->getLoc (), diag::external_macro_not_found,
736766 external.moduleName .str (),
737767 external.macroTypeName .str (),
738768 macro->getName ()
739769 );
740770 macro->diagnose (diag::decl_declared_here, macro->getName ());
741- return nullptr ;
771+ return None ;
742772 }
743773
744774#if SWIFT_SWIFT_PARSER
745- PrettyStackTraceExpr debugStack (ctx, " expanding macro" , expr );
775+ PrettyStackTraceExpr debugStack (ctx, " expanding macro" , mee );
746776
747777 // Builtin macros are handled via ASTGen.
748778 auto astGenSourceFile = sourceFile->exportedSourceFile ;
749779 if (!astGenSourceFile)
750- return nullptr ;
780+ return None ;
751781
752782 const char *evaluatedSourceAddress;
753783 ptrdiff_t evaluatedSourceLength;
754784 swift_ASTGen_expandFreestandingMacro (
755785 &ctx.Diags , externalDef->opaqueHandle ,
756786 static_cast <uint32_t >(externalDef->kind ), discriminator->data (),
757787 discriminator->size (), astGenSourceFile,
758- expr ->getStartLoc ().getOpaquePointerValue (), &evaluatedSourceAddress,
788+ mee ->getStartLoc ().getOpaquePointerValue (), &evaluatedSourceAddress,
759789 &evaluatedSourceLength);
760790 if (!evaluatedSourceAddress)
761- return nullptr ;
791+ return None ;
762792 evaluatedSource = llvm::MemoryBuffer::getMemBufferCopy (
763793 {evaluatedSourceAddress, (size_t )evaluatedSourceLength},
764794 adjustMacroExpansionBufferName (*discriminator));
765795 free ((void *)evaluatedSourceAddress);
766796 break ;
767797#else
768- ctx.Diags .diagnose (expr ->getLoc (), diag::macro_unsupported);
769- return nullptr ;
798+ ctx.Diags .diagnose (mee ->getLoc (), diag::macro_unsupported);
799+ return None ;
770800#endif
771801 }
772802 }
@@ -787,9 +817,9 @@ Expr *swift::expandMacroExpr(
787817 GeneratedSourceInfo sourceInfo{
788818 GeneratedSourceInfo::ExpressionMacroExpansion,
789819 Lexer::getCharSourceRangeFromSourceRange (
790- sourceMgr, expr ->getSourceRange ()),
820+ sourceMgr, mee ->getSourceRange ()),
791821 macroBufferRange,
792- ASTNode (expr ).getOpaqueValue (),
822+ ASTNode (mee ).getOpaqueValue (),
793823 dc
794824 };
795825 sourceMgr.setGeneratedSourceInfo (macroBufferID, sourceInfo);
@@ -807,7 +837,7 @@ Expr *swift::expandMacroExpr(
807837 if (topLevelItems.size () != 1 ) {
808838 ctx.Diags .diagnose (
809839 macroBufferRange.getStart (), diag::expected_macro_expansion_expr);
810- return nullptr ;
840+ return macroBufferID ;
811841 }
812842
813843 auto codeItem = topLevelItems.front ();
@@ -817,7 +847,7 @@ Expr *swift::expandMacroExpr(
817847 if (!expandedExpr) {
818848 ctx.Diags .diagnose (
819849 macroBufferRange.getStart (), diag::expected_macro_expansion_expr);
820- return nullptr ;
850+ return macroBufferID ;
821851 }
822852
823853 // Type-check the expanded expression.
@@ -834,12 +864,15 @@ Expr *swift::expandMacroExpr(
834864 Type realExpandedType = TypeChecker::typeCheckExpression (
835865 expandedExpr, dc, contextualType);
836866 if (!realExpandedType)
837- return nullptr ;
867+ return macroBufferID ;
838868
839869 assert ((expandedType->isEqual (realExpandedType) ||
840870 realExpandedType->hasError ()) &&
841871 " Type checking changed the result type?" );
842- return expandedExpr;
872+
873+ mee->setRewritten (expandedExpr);
874+
875+ return macroBufferID;
843876}
844877
845878// / Expands the given macro expansion declaration.
0 commit comments