@@ -41,6 +41,11 @@ class PatternTemplate : ValueVisitor<PatternTemplate, mlir::Value> {
4141 return OpGen.getContext ().getLoc ();
4242 }
4343
44+ mlir::Location createLoc (heavy::SourceLocation Loc) {
45+ return mlir::OpaqueLoc::get (Loc.getOpaqueEncoding (),
46+ OpGen.Builder .getContext ());
47+ }
48+
4449public:
4550 PatternTemplate (heavy::OpGen& O,
4651 heavy::Symbol* Keyword,
@@ -101,7 +106,7 @@ class PatternTemplate : ValueVisitor<PatternTemplate, mlir::Value> {
101106 return OpGen.SetError (" invalid pattern node" , P);
102107 }
103108
104- mlir::Value VisitSubPattern (heavy::SourceLocation Loc,
109+ mlir::Value VisitSubpattern (heavy::SourceLocation Loc,
105110 Value P, Value Cdr, mlir::Value E) {
106111 // Visit the subpattern.
107112 auto Body = std::make_unique<mlir::Region>();
@@ -110,8 +115,10 @@ class PatternTemplate : ValueVisitor<PatternTemplate, mlir::Value> {
110115 {
111116 mlir::OpBuilder::InsertionGuard IG (OpGen.Builder );
112117 OpGen.Builder .setInsertionPointToStart (&Block);
113-
114- Visit (P, E);
118+ mlir::Type HeavyValueT = OpGen.Builder .getType <HeavyValueTy>();
119+ mlir::Location MLoc = createLoc (Loc);
120+ mlir::Value BodyArg = Block.addArgument (HeavyValueT, MLoc);
121+ Visit (P, BodyArg);
115122
116123 // Create range for pack values by finding the
117124 // SyntaxClosureOps in the Body region.
@@ -124,13 +131,16 @@ class PatternTemplate : ValueVisitor<PatternTemplate, mlir::Value> {
124131 OpGen.create <ResolveOp>(Loc, Packs);
125132 }
126133
127- // Create the SubPatternOp .
134+ // Create the SubpatternOp .
128135 auto SubpatternOp = OpGen.create <heavy::SubpatternOp>(
129136 Loc, E, std::move (Body), Packs.size ());
130137
131138 if (!OpGen.CheckError ())
132139 Visit (Cdr, SubpatternOp.getCdr ());
133140
141+ // In the template, the nested syntax closures
142+ // will be looked up and check if its parent is
143+ // a subpattern op finding its result.
134144 return mlir::Value ();
135145 }
136146
@@ -141,12 +151,14 @@ class PatternTemplate : ValueVisitor<PatternTemplate, mlir::Value> {
141151 if (auto * P2 = dyn_cast<Pair>(P->Cdr );
142152 P2 && isa<Symbol>(P2->Car ) &&
143153 cast<Symbol>(P2->Car )->equals (Ellipsis)) {
144- VisitSubPattern (Loc, P->Car , P2->Cdr , E);
154+ // P->Car is the subpattern.
155+ // P2->Cdr is the rest of the pattern after the ...
156+ VisitSubpattern (Loc, P->Car , P2->Cdr , E);
145157 } else {
146- auto MatchPairOp = OpGen.create <heavy:: MatchPairOp>(Loc, E);
147- Visit (P->Car , MatchPairOp .getCar ());
158+ auto M1 = OpGen.create <MatchPairOp>(Loc, E);
159+ Visit (P->Car , M1 .getCar ());
148160 if (!OpGen.CheckError ())
149- Visit (P->Cdr , MatchPairOp .getCdr ());
161+ Visit (P->Cdr , M1 .getCdr ());
150162 }
151163
152164 return mlir::Value ();
@@ -180,9 +192,8 @@ class PatternTemplate : ValueVisitor<PatternTemplate, mlir::Value> {
180192 }
181193
182194 // <ellipsis>
183- if (P->equals (Ellipsis)) {
195+ if (P->equals (Ellipsis))
184196 return OpGen.SetError (" <ellipsis> is not a valid pattern" , P);
185- }
186197
187198 // everything else is a pattern variable
188199 return bindPatternVar (P, E);
0 commit comments