@@ -142,12 +142,14 @@ absl::StatusOr<InterpValue> InterpretExpr(DeduceCtx* ctx, Expr* expr,
142
142
// asserting that their values are consistent with other bindings (via argument
143
143
// types).
144
144
absl::Status EagerlyPopulateParametricEnvMap (
145
- absl::Span<const std::string> parametric_order ,
145
+ absl::Span<const ParametricWithType> typed_parametrics ,
146
146
const absl::flat_hash_map<std::string, Expr*>& parametric_default_exprs,
147
147
absl::flat_hash_map<std::string, InterpValue>& parametric_env_map,
148
148
const Span& span, std::string_view kind_name, DeduceCtx* ctx) {
149
149
// Attempt to interpret the parametric "default expressions" in order.
150
- for (const auto & name : parametric_order) {
150
+ for (const ParametricWithType& typed_parametric : typed_parametrics) {
151
+ std::string_view name = typed_parametric.identifier ();
152
+
151
153
Expr* expr = nullptr ;
152
154
if (auto it = parametric_default_exprs.find (name);
153
155
it != parametric_default_exprs.end () && it->second != nullptr ) {
@@ -205,7 +207,7 @@ absl::Status EagerlyPopulateParametricEnvMap(
205
207
return TypeInferenceErrorStatus (span, nullptr , message);
206
208
}
207
209
} else {
208
- parametric_env_map.insert ({name, result.value ()});
210
+ parametric_env_map.insert ({std::string{ name} , result.value ()});
209
211
}
210
212
}
211
213
return absl::OkStatus ();
@@ -222,6 +224,7 @@ ParametricInstantiator::ParametricInstantiator(
222
224
: span_(std::move(span)),
223
225
args_ (args),
224
226
ctx_(ABSL_DIE_IF_NULL(ctx)),
227
+ typed_parametrics_(typed_parametrics),
225
228
parametric_env_map_(explicit_parametrics),
226
229
parametric_bindings_(parametric_bindings) {
227
230
// We add derived type information so we can resolve types based on
@@ -233,10 +236,6 @@ ParametricInstantiator::ParametricInstantiator(
233
236
// interpret the expression to an InterpValue.
234
237
derived_type_info_ = ctx_->AddDerivedTypeInfo ();
235
238
236
- // Explicit parametric expressions are conceptually evaluated before other
237
- // parametric expressions.
238
- absl::flat_hash_set<std::string> ordered;
239
-
240
239
// Note: the first N explicit parametrics (given by the map) must be the first
241
240
// N parametrics for the thing we're instantiating; i.e. we can only
242
241
// explicitly instantiate from the "left hand side" of the parametric
@@ -250,28 +249,21 @@ ParametricInstantiator::ParametricInstantiator(
250
249
for (size_t i = 0 ; i < explicit_parametrics.size (); ++i) {
251
250
const std::string& identifier = parametric_bindings[i]->identifier ();
252
251
CHECK (explicit_parametrics.contains (identifier));
253
- parametric_order_.push_back (identifier);
254
- ordered.insert (identifier);
255
252
}
256
253
257
- VLOG (5 ) << " ParametricInstantiator; span: " << span_ << " ordered: ["
258
- << absl::StrJoin (ordered, " , " ) << " ]" ;
254
+ VLOG (5 ) << " ParametricInstantiator; span: " << span_;
259
255
260
256
for (const ParametricWithType& parametric : typed_parametrics) {
261
- std::string_view identifier = parametric.identifier ();
262
- if (!ordered.contains (identifier)) {
263
- parametric_order_.push_back (std::string{identifier});
264
- ordered.insert (std::string{identifier});
257
+ if (parametric.expr () != nullptr ) {
258
+ ctx_->type_info ()->SetItem (parametric.expr (), parametric.type ());
265
259
}
266
260
261
+ std::string_view identifier = parametric.identifier ();
267
262
std::unique_ptr<Type> parametric_expr_type =
268
263
parametric.type ().CloneToUnique ();
269
-
270
- if (parametric.expr () != nullptr ) {
271
- ctx_->type_info ()->SetItem (parametric.expr (), *parametric_expr_type);
272
- }
273
264
parametric_binding_types_.emplace (identifier,
274
265
std::move (parametric_expr_type));
266
+
275
267
parametric_default_exprs_[identifier] = parametric.expr ();
276
268
}
277
269
}
@@ -345,7 +337,7 @@ absl::StatusOr<TypeAndParametricEnv> FunctionInstantiator::Instantiate() {
345
337
}
346
338
347
339
XLS_RETURN_IF_ERROR (EagerlyPopulateParametricEnvMap (
348
- parametric_order (), parametric_default_exprs (), parametric_env_map (),
340
+ typed_parametrics (), parametric_default_exprs (), parametric_env_map (),
349
341
span (), GetKindName (), &ctx ()));
350
342
351
343
// Phase 2: resolve and check.
@@ -382,8 +374,9 @@ absl::StatusOr<TypeAndParametricEnv> FunctionInstantiator::Instantiate() {
382
374
383
375
parametric_env_expr_scope.Finish ();
384
376
385
- return TypeAndParametricEnv{std::move (resolved),
386
- ParametricEnv (parametric_env_map ())};
377
+ return TypeAndParametricEnv{
378
+ .type = std::move (resolved),
379
+ .parametric_env = ParametricEnv (parametric_env_map ())};
387
380
}
388
381
389
382
/* static */ absl::StatusOr<std::unique_ptr<StructInstantiator>>
@@ -409,7 +402,7 @@ absl::StatusOr<TypeAndParametricEnv> StructInstantiator::Instantiate() {
409
402
}
410
403
411
404
XLS_RETURN_IF_ERROR (EagerlyPopulateParametricEnvMap (
412
- parametric_order (), parametric_default_exprs (), parametric_env_map (),
405
+ typed_parametrics (), parametric_default_exprs (), parametric_env_map (),
413
406
span (), GetKindName (), &ctx ()));
414
407
415
408
// Phase 2: resolve and check.
@@ -427,8 +420,9 @@ absl::StatusOr<TypeAndParametricEnv> StructInstantiator::Instantiate() {
427
420
428
421
XLS_ASSIGN_OR_RETURN (std::unique_ptr<Type> resolved,
429
422
Resolve (*struct_type_, parametric_env_map ()));
430
- return TypeAndParametricEnv{std::move (resolved),
431
- ParametricEnv (parametric_env_map ())};
423
+ return TypeAndParametricEnv{
424
+ .type = std::move (resolved),
425
+ .parametric_env = ParametricEnv (parametric_env_map ())};
432
426
}
433
427
434
428
} // namespace internal
0 commit comments