# Concrete NIT source properties by name
readable attr _src_local_properties: Map[Symbol, MMLocalProperty]
- init(n: Symbol, cla: PClassdef, a: Int)
+ init(mod: MMSrcModule, n: Symbol, cla: PClassdef, a: Int)
do
- super(n, a)
+ super(mod, n, a)
_nodes = [cla]
_src_local_properties = new HashMap[Symbol, MMLocalProperty]
end
init(n: Symbol, d: PNode)
do
- assert n != null
- assert d != null
+ #assert n != null
+ #assert d != null
_name = n
_decl = d
end
init(n: Symbol, d: PNode) do super
end
+# False variable corresponding to closures declared in signatures
+# Lives in the same namespace than variables
+class ClosureVariable
+special Variable
+ redef meth kind do return once "closure"
+
+ # The signature of the closure
+ readable attr _closure: MMClosure
+
+ init(n: Symbol, d: PNode, c: MMClosure)
+ do
+ super(n, d)
+ _closure = c
+ end
+end
+
###############################################################################
# Visitor used during the syntax analysis
class AbsSyntaxVisitor
special Visitor
+ # The root type Object
+ meth type_object: MMType
+ do
+ return _module.class_by_name(once ("Object".to_symbol)).get_type
+ end
+
# The primitive type Bool
meth type_bool: MMType
do
error(n, "Type error: expected {stype}, got {subtype}")
return false
end
+
+ # Check that an expression has a static type and that
+ # Display an error and return false if n is a statement
+ # Require that the static type of n is known
+ meth check_expr(n: PExpr): Bool
+ do
+ # FIXME: The tc.error_count is a workaround since currently there is no way
+ # to distingate statements from buggy expressions: both have a null stype
+ if tc.error_count == 0 and n.stype == null then
+ error(n, "Type error: expected expression.")
+ return false
+ end
+ return true
+ end
+ # Combine check_conform and check_expr
+ meth check_conform_expr(n: PExpr, stype: MMType): Bool
+ do
+ if check_expr(n) then return check_conform(n, n.stype, stype) else return false
+ end
+
+ # Check conformance between multiple expressions and a static type
+ # Conformance is granted if among them there is a most general type
+ # Return the most general type if a conformance is found
+ # Display an error and return null if no conformance is found
+ # @param stype is a possible additional type (without node)
+ # Examples:
+ # Int, Int, Object => return Object
+ # Int, Float => display error, return null
+ meth check_conform_multiexpr(stype: MMType, nodes: Collection[PExpr]): MMType
+ do
+ var node: PExpr = null # candidate node
+ for n in nodes do
+ if not check_expr(n) then return null
+ var ntype = n.stype
+ if stype == null or (ntype != null and stype < ntype) then
+ stype = ntype
+ node = n
+ end
+ end
+ for n in nodes do
+ if not n.stype < stype then
+ if node == null then
+ error(n, "Type error: no most general type. Got {n.stype} and {stype}.")
+ else
+ error(n, "Type error: no most general type. Got {n.stype} and {stype} at {node.locate}.")
+ end
+ return null
+ end
+ end
+ return stype
+ end
protected init(tc: ToolContext, module: MMSrcModule)
do
redef class AMethPropdef
# Associated method (MM entity)
meth method: MMMethSrcMethod is abstract
+
+ # Associated 'self' variable
+ meth self_var: ParamVariable is abstract
end
redef class ATypePropdef
meth variable: ParamVariable is abstract
end
+redef class PClosureDecl
+ # Associated closure variable
+ meth variable: ClosureVariable is abstract
+end
+
redef class PType
# Retrieve the local class corresponding to the type.
# Display an error and return null if there is no class
redef class AType
attr _stype_cache: MMType
- attr _stype_cached: Bool
+ attr _stype_cached: Bool = false
redef meth get_local_class(v)
do
readable writable attr _variable: VarVariable
end
-redef class AForVardeclExpr
+redef class AForExpr
# Associated automatic local variable
readable writable attr _variable: AutoVariable
end
+redef class ASelfExpr
+ # Associated local variable
+ readable writable attr _variable: ParamVariable
+end
+
redef class AVarFormExpr
# Associated local variable
readable writable attr _variable: Variable
end
+redef class AClosureCallExpr
+ # Associated closure variable
+ readable writable attr _variable: ClosureVariable
+end
+
+redef class PClosureDef
+ # Associated closure
+ readable writable attr _closure: MMClosure
+
+ # Automatic variables
+ readable writable attr _variables: Array[AutoVariable]
+end