package typing
import syntax_base
-import escape
-import control_flow
+import flow
+import scope
redef class MMSrcModule
# Walk trough the module and type statments and expressions
# * Resolve call and attribute access
# * Check type conformance
private class TypingVisitor
-special AbsSyntaxVisitor
+ super AbsSyntaxVisitor
redef fun visit(n)
do
if n != null then n.accept_typing(self)
end
- # Current knowledge about variables names and types
- fun scope_ctx: ScopeContext do return _scope_ctx.as(not null)
- writable var _scope_ctx: nullable ScopeContext
+ # Current knowledge about scoped things (variable, labels, etc.)
+ readable var _scope_ctx: ScopeContext = new ScopeContext(self)
# Current knowledge about control flow
fun flow_ctx: FlowContext do return _flow_ctx.as(not null)
fun enter_visit_block(node: nullable AExpr)
do
if node == null then return
- var old_scope_ctx = scope_ctx
- scope_ctx = scope_ctx.sub(node)
+ scope_ctx.push(node)
enter_visit(node)
- scope_ctx = old_scope_ctx
+ scope_ctx.pop
end
# Non-bypassable knowledge about variables names and types
fun base_flow_ctx: FlowContext do return _base_flow_ctx.as(not null)
writable var _base_flow_ctx: nullable FlowContext
- # Current knowledge about escapable blocks
- readable writable var _escapable_ctx: EscapableContext = new EscapableContext(self)
-
# The current reciever
fun self_var: ParamVariable do return _self_var.as(not null)
writable var _self_var: nullable ParamVariable
if ctx != null then flow_ctx = ctx
end
- # Make the if_false_scope_ctx of the expression effective
+ # Make the if_false_flow_ctx of the expression effective
private fun use_if_false_flow_ctx(e: AExpr)
do
var ctx = e.if_false_flow_ctx
# Number of nested once
readable writable var _once_count: Int = 0
- init(tc, module) do super
+ init(tc, mod) do super
private fun get_default_constructor_for(n: ANode, c: MMLocalClass, prop: MMSrcMethod): nullable MMMethod
do
redef class AClassdef
redef fun accept_typing(v)
do
- v.scope_ctx = new RootScopeContext(v, self)
v.self_var = new ParamVariable("self".to_symbol, self)
v.self_var.stype = local_class.get_type
super
v.flow_ctx = new RootFlowContext(v, self)
v.base_flow_ctx = v.flow_ctx
- var old_scope_ctx = v.scope_ctx
- v.scope_ctx = old_scope_ctx.sub(self)
-
+ v.scope_ctx.push(self)
_self_var = v.self_var
super
if n_expr != null then
v.check_conform_expr(n_expr.as(not null), prop.signature.return_type.as(not null))
end
- v.scope_ctx = old_scope_ctx
+ v.scope_ctx.pop
end
end
v.flow_ctx = new RootFlowContext(v, self)
v.base_flow_ctx = v.flow_ctx
- var old_scope_ctx = v.scope_ctx
- v.scope_ctx = old_scope_ctx.sub(self)
+ v.scope_ctx.push(self)
_self_var = v.self_var
super
- v.scope_ctx = old_scope_ctx
+ v.scope_ctx.pop
end
end
var cur_c: nullable MMLocalClass = null
if i < l then
cur_m = explicit_super_init_calls[i]
- cur_c = cur_m.global.intro.local_class.for_module(v.module)
+ cur_c = cur_m.global.intro.local_class.for_module(v.mmmodule)
end
var j = 0
while j < v.local_class.cshe.direct_greaters.length do
var c = v.local_class.cshe.direct_greaters[j]
- if c.global.is_interface or c.global.is_universal or c.global.is_mixin then
+ if c.global.is_interface or c.global.is_enum or c.global.is_mixin then
j += 1
else if cur_c != null and (c.cshe <= cur_c or cur_c.global.is_mixin) then
if c == cur_c then j += 1
i += 1
if i < l then
cur_m = explicit_super_init_calls[i]
- cur_c = cur_m.global.intro.local_class.for_module(v.module)
+ cur_c = cur_m.global.intro.local_class.for_module(v.mmmodule)
else
cur_m = null
cur_c = null
redef class AParam
redef fun after_typing(v)
do
- v.scope_ctx.add(variable)
+ v.scope_ctx.add_variable(variable)
end
end
redef fun accept_typing(v)
do
# Register the closure for ClosureCallExpr
- v.scope_ctx.add(variable)
+ v.scope_ctx.add_variable(variable)
- var old_scope_ctx = v.scope_ctx
var old_flow_ctx = v.flow_ctx
var old_base_flow_ctx = v.base_flow_ctx
v.base_flow_ctx = v.flow_ctx
- v.scope_ctx = v.scope_ctx.sub(self)
var blist: nullable Array[AExpr] = null
var t = v.local_property.signature.return_type
if t != null then blist = new Array[AExpr]
var escapable = new EscapableClosure(self, variable.closure, blist)
_escapable = escapable
- v.escapable_ctx.push(escapable, null)
+ v.scope_ctx.push_escapable(escapable, null)
v.is_default_closure_definition = true
v.check_conform_expr(x, t)
end
- v.scope_ctx = old_scope_ctx
v.flow_ctx = old_flow_ctx
v.base_flow_ctx = old_base_flow_ctx
- v.escapable_ctx.pop
+ v.scope_ctx.pop
end
end
do
var va = new VarVariable(n_id.to_symbol, n_id)
_variable = va
- v.scope_ctx.add(va)
+ v.scope_ctx.add_variable(va)
var ne = n_expr
if ne != null then v.mark_is_set(va)
redef fun after_typing(v)
do
v.mark_unreash(self)
- var esc = compute_escapable_block(v.escapable_ctx)
+ var esc = compute_escapable_block(v.scope_ctx)
if esc == null then return
if esc.is_break_block then
do
var old_flow_ctx = v.flow_ctx
v.mark_unreash(self)
- var esc = compute_escapable_block(v.escapable_ctx)
+ var esc = compute_escapable_block(v.scope_ctx)
if esc == null then return
esc.break_flow_contexts.add(old_flow_ctx)
# An abstract control structure with feature escapable block
class AAbsControl
-special AExpr
+ super AExpr
# The corresponding escapable block
readable var _escapable: nullable EscapableBlock
do
# Register the escapable block
_escapable = escapable
- v.escapable_ctx.push(escapable, n_label)
+ v.scope_ctx.push_escapable(escapable, n_label)
# Save an prepare the contextes
var old_flow_ctx = v.flow_ctx
end
if is_loop then v.base_flow_ctx = old_base_flow_ctx
- v.escapable_ctx.pop
+ v.scope_ctx.pop
_is_typed = true
end
end
redef class ADoExpr
-special AAbsControl
+ super AAbsControl
redef fun accept_typing(v)
do
process_control(v, new BreakOnlyEscapableBlock(self), n_label, false)
end
redef class AWhileExpr
-special AAbsControl
+ super AAbsControl
redef fun accept_typing(v)
do
process_control(v, new EscapableBlock(self), n_label, true)
end
redef class ALoopExpr
-special AAbsControl
+ super AAbsControl
redef fun accept_typing(v)
do
process_control(v, new EscapableBlock(self), n_label, true)
end
redef class AForExpr
-special AAbsControl
+ super AAbsControl
var _variable: nullable AutoVariable
redef fun variable do return _variable.as(not null)
redef fun process_control_inside(v)
do
- var old_scope_ctx = v.scope_ctx
- v.scope_ctx = v.scope_ctx.sub(self)
+ v.scope_ctx.push(self)
var old_flow_ctx = v.flow_ctx
# Create the automatic variable
var va = new AutoVariable(n_id.to_symbol, n_id)
_variable = va
- v.scope_ctx.add(va)
+ v.scope_ctx.add_variable(va)
# Process collection
v.enter_visit(n_expr)
# end == begin of the loop
v.flow_ctx = old_flow_ctx
- v.scope_ctx = old_scope_ctx
+ v.scope_ctx.pop
end
end
return null
end
var prop = lc.select_method(name)
- prop.global.check_visibility(v, self, v.module, false)
+ prop.global.check_visibility(v, self, v.mmmodule, false)
var psig = prop.signature_for(type_lvalue)
_assign_method = prop
if not v.check_conform_expr(n_value, psig[0].not_for_self) then return null
var stype: nullable MMType = null
for prop in precs do
assert prop isa MMMethod
- var t = prop.signature_for(v.self_var.stype.as(not null)).return_type.for_module(v.module).adapt_to(v.local_property.signature.recv)
+ var t = prop.signature_for(v.self_var.stype.as(not null)).return_type.for_module(v.mmmodule).adapt_to(v.local_property.signature.recv)
stypes.add(t)
if stype == null or stype < t then
stype = t
return
end
var prop = lc.select_attribute(name)
- if v.module.visibility_for(prop.global.local_class.module) < 3 then
- v.error(self, "Error: Attribute {name} from {prop.global.local_class.module} is invisible in {v.module}")
+ if v.mmmodule.visibility_for(prop.global.local_class.mmmodule) < 3 then
+ v.error(self, "Error: Attribute {name} from {prop.global.local_class.mmmodule} is invisible in {v.mmmodule}")
end
_prop = prop
var at = prop.signature_for(type_recv).return_type
var csi = psig.closure_named(cni)
if csi != null then
var esc = new EscapableClosure(cdi, csi, break_list)
- v.escapable_ctx.push(esc, n_label)
+ v.scope_ctx.push_escapable(esc, n_label)
cdi.accept_typing2(v, esc)
- v.escapable_ctx.pop
+ v.scope_ctx.pop
else if cs.length == 1 then
v.error(cdi.n_id, "Error: no closure named '!{cni}' in {name}; only closure is !{cs.first.name}.")
else
# Get the signature for a local property and a receiver
private fun get_signature(v: TypingVisitor, type_recv: MMType, prop: MMMethod, recv_is_self: Bool): MMSignature
do
- prop.global.check_visibility(v, self, v.module, recv_is_self)
+ prop.global.check_visibility(v, self, v.mmmodule, recv_is_self)
var psig = prop.signature_for(type_recv)
if not recv_is_self then psig = psig.not_for_self
return psig
if parent != v.top_block and self != v.top_block then
v.error(self, "Error: Constructor invocation {property} must not be in nested block.")
end
- var cla = v.module[property.global.intro.local_class.global]
+ var cla = v.mmmodule[property.global.intro.local_class.global]
var prev_class: nullable MMLocalClass = null
var esic = v.explicit_super_init_calls.as(not null)
if not esic.is_empty then
redef class ALeExpr
redef fun name do return once "<=".to_symbol
end
+redef class ALlExpr
+ redef fun name do return once "<<".to_symbol
+end
redef class AGtExpr
redef fun name do return once ">".to_symbol
end
redef class AGeExpr
redef fun name do return once ">=".to_symbol
end
+redef class AGgExpr
+ redef fun name do return once ">>".to_symbol
+end
redef class APlusExpr
redef fun name do return once "+".to_symbol
end
_closure = esc.closure
- var old_scope_ctx = v.scope_ctx
+ v.scope_ctx.push(self)
var old_flow_ctx = v.flow_ctx
var old_base_flow_ctx = v.base_flow_ctx
v.base_flow_ctx = v.flow_ctx
- v.scope_ctx = v.scope_ctx.sub(self)
variables = new Array[AutoVariable]
for i in [0..n_ids.length[ do
var va = new AutoVariable(n_ids[i].to_symbol, n_ids[i])
variables.add(va)
va.stype = sig[i]
- v.scope_ctx.add(va)
+ v.scope_ctx.add_variable(va)
end
_accept_typing2 = true
v.error(self, "Control error: Reached end of break block (a 'break' with a value was expected).")
end
end
- v.scope_ctx = old_scope_ctx
v.flow_ctx = old_flow_ctx
v.base_flow_ctx = old_base_flow_ctx
+ v.scope_ctx.pop
end
end
class ATypeCheckExpr
-special AExpr
+ super AExpr
private fun check_expr_cast(v: TypingVisitor, n_expr: AExpr, n_type: AType)
do
if not v.check_expr(n_expr) then return
end
redef class AIsaExpr
-special ATypeCheckExpr
+ super ATypeCheckExpr
redef fun after_typing(v)
do
check_expr_cast(v, n_expr, n_type)
end
redef class AAsCastExpr
-special ATypeCheckExpr
+ super ATypeCheckExpr
redef fun after_typing(v)
do
check_expr_cast(v, n_expr, n_type)