Skip to content
This repository was archived by the owner on Dec 23, 2022. It is now read-only.

Commit 8ee377f

Browse files
committed
modelbuilder_base: Refactor the signature of resolve_mtype*
Only `modelize_mclass` require support for partial context. Furthermore, in a future PR, it may need to resolve types just before modeling the class definition (in a place where the `MClass` is available and must be used). Signed-off-by: Jean-Christophe Beaupré <[email protected]>
1 parent 91f30fa commit 8ee377f

File tree

6 files changed

+111
-45
lines changed

6 files changed

+111
-45
lines changed

src/metrics/static_types_metrics.nit

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ private class ATypeCounterVisitor
5454
if n isa AAnnotation then return
5555

5656
if n isa AType then
57-
var mclassdef = self.nclassdef.mclassdef
58-
var mtype = modelbuilder.resolve_mtype(mclassdef.mmodule, mclassdef, n)
57+
var mclassdef = self.nclassdef.mclassdef.as(not null)
58+
var mtype = modelbuilder.resolve_mtype(mclassdef, n)
5959
if mtype != null then
6060
self.typecount.inc(mtype)
6161
end

src/modelbuilder_base.nit

+76-15
Original file line numberDiff line numberDiff line change
@@ -260,18 +260,50 @@ class ModelBuilder
260260
end
261261

262262
# Return the static type associated to the node `ntype`.
263-
# `mmodule` and `mclassdef` is the context where the call is made (used to understand formal types)
263+
#
264+
# `mclassdef` is the context where the call is made (used to understand
265+
# formal types).
264266
# In case of problem, an error is displayed on `ntype` and null is returned.
265-
# FIXME: the name "resolve_mtype" is awful
266-
fun resolve_mtype_unchecked(mmodule: MModule, mclassdef: nullable MClassDef, ntype: AType, with_virtual: Bool): nullable MType
267+
#
268+
# Same as `resolve_mtype_unchecked3`, but get the context (module, class and
269+
# anchor) from `mclassdef`.
270+
#
271+
# SEE: `resolve_mtype`
272+
# SEE: `resolve_mtype3_unchecked`
273+
#
274+
# FIXME: Find a better name for this method.
275+
fun resolve_mtype_unchecked(mclassdef: MClassDef, ntype: AType, with_virtual: Bool): nullable MType
276+
do
277+
return resolve_mtype3_unchecked(
278+
mclassdef.mmodule,
279+
mclassdef.mclass,
280+
mclassdef.bound_mtype,
281+
ntype,
282+
with_virtual
283+
)
284+
end
285+
286+
# Return the static type associated to the node `ntype`.
287+
#
288+
# `mmodule`, `mclass` and `anchor` compose the context where the call is
289+
# made (used to understand formal types).
290+
# In case of problem, an error is displayed on `ntype` and null is returned.
291+
#
292+
# Note: The “3” is for 3 contextual parameters.
293+
#
294+
# SEE: `resolve_mtype`
295+
# SEE: `resolve_mtype_unchecked`
296+
#
297+
# FIXME: Find a better name for this method.
298+
fun resolve_mtype3_unchecked(mmodule: MModule, mclass: nullable MClass, anchor: nullable MClassType, ntype: AType, with_virtual: Bool): nullable MType
267299
do
268300
var qid = ntype.n_qid
269301
var name = qid.n_id.text
270302
var res: MType
271303

272304
# Check virtual type
273-
if mclassdef != null and with_virtual then
274-
var prop = try_get_mproperty_by_name(ntype, mclassdef, name).as(nullable MVirtualTypeProp)
305+
if anchor != null and with_virtual then
306+
var prop = try_get_mproperty_by_name2(ntype, mmodule, anchor, name).as(nullable MVirtualTypeProp)
275307
if prop != null then
276308
if not ntype.n_types.is_empty then
277309
error(ntype, "Type Error: formal type `{name}` cannot have formal parameters.")
@@ -284,8 +316,8 @@ class ModelBuilder
284316
end
285317

286318
# Check parameter type
287-
if mclassdef != null then
288-
for p in mclassdef.mclass.mparameters do
319+
if mclass != null then
320+
for p in mclass.mparameters do
289321
if p.name != name then continue
290322

291323
if not ntype.n_types.is_empty then
@@ -321,7 +353,7 @@ class ModelBuilder
321353
else
322354
var mtypes = new Array[MType]
323355
for nt in ntype.n_types do
324-
var mt = resolve_mtype_unchecked(mmodule, mclassdef, nt, with_virtual)
356+
var mt = resolve_mtype3_unchecked(mmodule, mclass, anchor, nt, with_virtual)
325357
if mt == null then return null # Forward error
326358
mtypes.add(mt)
327359
end
@@ -415,13 +447,44 @@ class ModelBuilder
415447
private var bad_class_names = new MultiHashMap[MModule, String]
416448

417449
# Return the static type associated to the node `ntype`.
418-
# `mmodule` and `mclassdef` is the context where the call is made (used to understand formal types)
450+
#
451+
# `mclassdef` is the context where the call is made (used to understand
452+
# formal types).
419453
# In case of problem, an error is displayed on `ntype` and null is returned.
420-
# FIXME: the name "resolve_mtype" is awful
421-
fun resolve_mtype(mmodule: MModule, mclassdef: nullable MClassDef, ntype: AType): nullable MType
454+
#
455+
# Same as `resolve_mtype3`, but get the context (module, class and ) from
456+
# `mclassdef`.
457+
#
458+
# SEE: `resolve_mtype3`
459+
# SEE: `resolve_mtype_unchecked`
460+
#
461+
# FIXME: Find a better name for this method.
462+
fun resolve_mtype(mclassdef: MClassDef, ntype: AType): nullable MType
463+
do
464+
return resolve_mtype3(
465+
mclassdef.mmodule,
466+
mclassdef.mclass,
467+
mclassdef.bound_mtype,
468+
ntype
469+
)
470+
end
471+
472+
# Return the static type associated to the node `ntype`.
473+
#
474+
# `mmodule`, `mclass` and `anchor` compose the context where the call is
475+
# made (used to understand formal types).
476+
# In case of problem, an error is displayed on `ntype` and null is returned.
477+
#
478+
# Note: The “3” is for 3 contextual parameters.
479+
#
480+
# SEE: `resolve_mtype`
481+
# SEE: `resolve_mtype_unchecked`
482+
#
483+
# FIXME: Find a better name for this method.
484+
fun resolve_mtype3(mmodule: MModule, mclass: nullable MClass, anchor: nullable MClassType, ntype: AType): nullable MType
422485
do
423486
var mtype = ntype.mtype
424-
if mtype == null then mtype = resolve_mtype_unchecked(mmodule, mclassdef, ntype, true)
487+
if mtype == null then mtype = resolve_mtype3_unchecked(mmodule, mclass, anchor, ntype, true)
425488
if mtype == null then return null # Forward error
426489

427490
if ntype.checked_mtype then return mtype
@@ -432,10 +495,8 @@ class ModelBuilder
432495
if intro == null then return null # skip error
433496
var bound = intro.bound_mtype.arguments[i]
434497
var nt = ntype.n_types[i]
435-
var mt = resolve_mtype(mmodule, mclassdef, nt)
498+
var mt = resolve_mtype3(mmodule, mclass, anchor, nt)
436499
if mt == null then return null # forward error
437-
var anchor
438-
if mclassdef != null then anchor = mclassdef.bound_mtype else anchor = null
439500
if not check_subtype(nt, mmodule, anchor, mt, bound) then
440501
error(nt, "Type Error: expected `{bound}`, got `{mt}`.")
441502
return null

src/modelize/modelize_class.nit

+14-4
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ redef class ModelBuilder
179179
end
180180
var nfdt = nfd.n_type
181181
if nfdt != null then
182-
var bound = resolve_mtype_unchecked(mmodule, null, nfdt, false)
182+
var bound = resolve_mtype3_unchecked(mmodule, null, null, nfdt, false)
183183
if bound == null then return # Forward error
184184
if bound.need_anchor then
185185
# No F-bounds!
@@ -256,7 +256,7 @@ redef class ModelBuilder
256256
for nsc in nclassdef.n_superclasses do
257257
specobject = false
258258
var ntype = nsc.n_type
259-
var mtype = resolve_mtype_unchecked(mmodule, mclassdef, ntype, false)
259+
var mtype = resolve_mtype_unchecked(mclassdef, ntype, false)
260260
if mtype == null then continue # Skip because of error
261261
if not mtype isa MClassType then
262262
error(ntype, "Error: supertypes cannot be a formal type.")
@@ -364,19 +364,29 @@ redef class ModelBuilder
364364
for nclassdef in nmodule.n_classdefs do
365365
if nclassdef isa AStdClassdef then
366366
var mclassdef = nclassdef.mclassdef
367+
var mclass
368+
var anchor
369+
if mclassdef == null then
370+
mclass = null
371+
anchor = null
372+
else
373+
mclass = mclassdef.mclass
374+
anchor = mclassdef.bound_mtype
375+
end
376+
367377
# check bound of formal parameter
368378
for nfd in nclassdef.n_formaldefs do
369379
var nfdt = nfd.n_type
370380
if nfdt != null and nfdt.mtype != null then
371-
var bound = resolve_mtype(mmodule, mclassdef, nfdt)
381+
var bound = resolve_mtype3(mmodule, mclass, anchor, nfdt)
372382
if bound == null then return # Forward error
373383
end
374384
end
375385
# check declared super types
376386
for nsc in nclassdef.n_superclasses do
377387
var ntype = nsc.n_type
378388
if ntype.mtype != null then
379-
var mtype = resolve_mtype(mmodule, mclassdef, ntype)
389+
var mtype = resolve_mtype3(mmodule, mclass, anchor, ntype)
380390
if mtype == null then return # Forward error
381391
end
382392
end

src/modelize/modelize_property.nit

+12-14
Original file line numberDiff line numberDiff line change
@@ -671,14 +671,13 @@ redef class ASignature
671671
# Visit and fill information about a signature
672672
private fun visit_signature(modelbuilder: ModelBuilder, mclassdef: MClassDef): Bool
673673
do
674-
var mmodule = mclassdef.mmodule
675674
var param_names = self.param_names
676675
var param_types = self.param_types
677676
for np in self.n_params do
678677
param_names.add(np.n_id.text)
679678
var ntype = np.n_type
680679
if ntype != null then
681-
var mtype = modelbuilder.resolve_mtype_unchecked(mmodule, mclassdef, ntype, true)
680+
var mtype = modelbuilder.resolve_mtype_unchecked(mclassdef, ntype, true)
682681
if mtype == null then return false # Skip error
683682
for i in [0..param_names.length-param_types.length[ do
684683
param_types.add(mtype)
@@ -695,7 +694,7 @@ redef class ASignature
695694
end
696695
var ntype = self.n_type
697696
if ntype != null then
698-
self.ret_type = modelbuilder.resolve_mtype_unchecked(mmodule, mclassdef, ntype, true)
697+
self.ret_type = modelbuilder.resolve_mtype_unchecked(mclassdef, ntype, true)
699698
if self.ret_type == null then return false # Skip error
700699
end
701700

@@ -709,14 +708,14 @@ redef class ASignature
709708
for np in self.n_params do
710709
var ntype = np.n_type
711710
if ntype != null then
712-
if modelbuilder.resolve_mtype(mclassdef.mmodule, mclassdef, ntype) == null then
711+
if modelbuilder.resolve_mtype(mclassdef, ntype) == null then
713712
res = false
714713
end
715714
end
716715
end
717716
var ntype = self.n_type
718717
if ntype != null then
719-
if modelbuilder.resolve_mtype(mclassdef.mmodule, mclassdef, ntype) == null then
718+
if modelbuilder.resolve_mtype(mclassdef, ntype) == null then
720719
res = false
721720
end
722721
end
@@ -1348,7 +1347,7 @@ redef class AAttrPropdef
13481347

13491348
var ntype = self.n_type
13501349
if ntype != null then
1351-
mtype = modelbuilder.resolve_mtype_unchecked(mmodule, mclassdef, ntype, true)
1350+
mtype = modelbuilder.resolve_mtype_unchecked(mclassdef, ntype, true)
13521351
if mtype == null then return
13531352
end
13541353

@@ -1369,9 +1368,9 @@ redef class AAttrPropdef
13691368
if mtype == null then
13701369
if nexpr != null then
13711370
if nexpr isa ANewExpr then
1372-
mtype = modelbuilder.resolve_mtype_unchecked(mmodule, mclassdef, nexpr.n_type, true)
1371+
mtype = modelbuilder.resolve_mtype_unchecked(mclassdef, nexpr.n_type, true)
13731372
else if nexpr isa AAsCastExpr then
1374-
mtype = modelbuilder.resolve_mtype_unchecked(mmodule, mclassdef, nexpr.n_type, true)
1373+
mtype = modelbuilder.resolve_mtype_unchecked(mclassdef, nexpr.n_type, true)
13751374
else if nexpr isa AIntegerExpr then
13761375
var cla: nullable MClass = null
13771376
if nexpr.value isa Int then
@@ -1432,7 +1431,7 @@ redef class AAttrPropdef
14321431
end
14331432
else if ntype != null and inherited_type == mtype then
14341433
if nexpr isa ANewExpr then
1435-
var xmtype = modelbuilder.resolve_mtype_unchecked(mmodule, mclassdef, nexpr.n_type, true)
1434+
var xmtype = modelbuilder.resolve_mtype_unchecked(mclassdef, nexpr.n_type, true)
14361435
if xmtype == mtype then
14371436
modelbuilder.advice(ntype, "useless-type", "Warning: useless type definition")
14381437
end
@@ -1488,11 +1487,11 @@ redef class AAttrPropdef
14881487

14891488
# Check types
14901489
if ntype != null then
1491-
if modelbuilder.resolve_mtype(mmodule, mclassdef, ntype) == null then return
1490+
if modelbuilder.resolve_mtype(mclassdef, ntype) == null then return
14921491
end
14931492
var nexpr = n_expr
14941493
if nexpr isa ANewExpr then
1495-
if modelbuilder.resolve_mtype(mmodule, mclassdef, nexpr.n_type) == null then return
1494+
if modelbuilder.resolve_mtype(mclassdef, nexpr.n_type) == null then return
14961495
end
14971496

14981497
# Lookup for signature in the precursor
@@ -1645,11 +1644,10 @@ redef class ATypePropdef
16451644
var mpropdef = self.mpropdef
16461645
if mpropdef == null then return # Error thus skipped
16471646
var mclassdef = mpropdef.mclassdef
1648-
var mmodule = mclassdef.mmodule
16491647
var mtype: nullable MType = null
16501648

16511649
var ntype = self.n_type
1652-
mtype = modelbuilder.resolve_mtype_unchecked(mmodule, mclassdef, ntype, true)
1650+
mtype = modelbuilder.resolve_mtype_unchecked(mclassdef, ntype, true)
16531651
if mtype == null then return
16541652

16551653
mpropdef.bound = mtype
@@ -1671,7 +1669,7 @@ redef class ATypePropdef
16711669
var anchor = mclassdef.bound_mtype
16721670

16731671
var ntype = self.n_type
1674-
if modelbuilder.resolve_mtype(mmodule, mclassdef, ntype) == null then
1672+
if modelbuilder.resolve_mtype(mclassdef, ntype) == null then
16751673
mpropdef.bound = null
16761674
return
16771675
end

src/nitni/nitni_callbacks.nit

+6-9
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ redef class AFullPropExternCall
304304
var mmodule = npropdef.mpropdef.mclassdef.mmodule
305305
var mclassdef = npropdef.mpropdef.mclassdef
306306
var mclass_type = mclassdef.bound_mtype
307-
var mtype = toolcontext.modelbuilder.resolve_mtype(mmodule, mclassdef, n_type)
307+
var mtype = toolcontext.modelbuilder.resolve_mtype(mclassdef, n_type)
308308

309309
if mtype == null then return
310310

@@ -337,7 +337,7 @@ redef class AInitPropExternCall
337337
do
338338
var mmodule = npropdef.mpropdef.mclassdef.mmodule
339339
var mclassdef = npropdef.mpropdef.mclassdef
340-
var mtype = toolcontext.modelbuilder.resolve_mtype(mmodule, mclassdef, n_type)
340+
var mtype = toolcontext.modelbuilder.resolve_mtype(mclassdef, n_type)
341341
if mtype == null then return
342342

343343
if not mtype isa MClassType then
@@ -398,9 +398,8 @@ redef class ACastAsExternCall
398398
redef fun verify_and_collect(npropdef, callback_set, toolcontext)
399399
do
400400
var mclassdef = npropdef.mpropdef.mclassdef
401-
var mmodule = mclassdef.mmodule
402-
toolcontext.modelbuilder.resolve_mtype_unchecked(mmodule, mclassdef, n_from_type, true)
403-
toolcontext.modelbuilder.resolve_mtype_unchecked(mmodule, mclassdef, n_to_type, true)
401+
toolcontext.modelbuilder.resolve_mtype_unchecked(mclassdef, n_from_type, true)
402+
toolcontext.modelbuilder.resolve_mtype_unchecked(mclassdef, n_to_type, true)
404403
super
405404
end
406405
end
@@ -412,8 +411,7 @@ redef class AAsNullableExternCall
412411
redef fun verify_and_collect(npropdef, callback_set, toolcontext)
413412
do
414413
var mclassdef = npropdef.mpropdef.mclassdef
415-
var mmodule = mclassdef.mmodule
416-
toolcontext.modelbuilder.resolve_mtype_unchecked(mmodule, mclassdef, n_type, true)
414+
toolcontext.modelbuilder.resolve_mtype_unchecked(mclassdef, n_type, true)
417415
super
418416
end
419417
end
@@ -429,8 +427,7 @@ redef class AAsNotNullableExternCall
429427
redef fun verify_and_collect(npropdef, callback_set, toolcontext)
430428
do
431429
var mclassdef = npropdef.mpropdef.mclassdef
432-
var mmodule = mclassdef.mmodule
433-
toolcontext.modelbuilder.resolve_mtype_unchecked(mmodule, mclassdef, n_type, true)
430+
toolcontext.modelbuilder.resolve_mtype_unchecked(mclassdef, n_type, true)
434431
super
435432
end
436433
end

src/semantize/typing.nit

+1-1
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ private class TypeVisitor
275275

276276
fun resolve_mtype(node: AType): nullable MType
277277
do
278-
return self.modelbuilder.resolve_mtype(mmodule, mclassdef, node)
278+
return self.modelbuilder.resolve_mtype(mclassdef, node)
279279
end
280280

281281
fun try_get_mclass(node: ANode, name: String): nullable MClass

0 commit comments

Comments
 (0)