Skip to content
Snippets Groups Projects
Commit 24a83b5c authored by Frederik Hennig's avatar Frederik Hennig
Browse files

expanded various doc comments

parent 45c9fe80
No related branches found
No related tags found
No related merge requests found
...@@ -25,7 +25,7 @@ NodeT = TypeVar("NodeT", bound=PsAstNode) ...@@ -25,7 +25,7 @@ NodeT = TypeVar("NodeT", bound=PsAstNode)
class UndeterminedType(PsNumericType): class UndeterminedType(PsNumericType):
def create_constant(self, value: Any) -> Any: def create_constant(self, value: Any) -> Any:
return None return None
def _err(self) -> NoReturn: def _err(self) -> NoReturn:
raise PsInternalCompilerError("Calling UndeterminedType.") raise PsInternalCompilerError("Calling UndeterminedType.")
...@@ -92,16 +92,18 @@ class Typifier(Mapper): ...@@ -92,16 +92,18 @@ class Typifier(Mapper):
"""Typifier for untyped expressions. """Typifier for untyped expressions.
The typifier, when called with an AST node, will attempt to figure out The typifier, when called with an AST node, will attempt to figure out
the types for all untyped expressions within the node: the types for all untyped expressions within the node.
Plain variables will be assigned a type according to `ctx.options.default_dtype`,
- Plain variables will be assigned a type according to `ctx.options.default_dtype`. constants will be converted to typed constants according to the contextual typing scheme
- Constants will be converted to typed constants by applying the target type of the current context. described below.
Contextual Typing Contextual Typing
----------------- -----------------
Starting at an expression's root, the typifier attempts to expand a typing context as far as possible. The contextual typifier covers the expression tree with disjoint typing contexts.
The idea is that all nodes covered by a typing context must have the exact same type.
Starting at an expression's root, the typifier attempts to expand a typing context as far as possible
toward the leaves.
This happens implicitly during the recursive traversal of the expression tree. This happens implicitly during the recursive traversal of the expression tree.
At an interior node, which is modelled as a function applied to a number of arguments, producing a result, At an interior node, which is modelled as a function applied to a number of arguments, producing a result,
...@@ -112,13 +114,17 @@ class Typifier(Mapper): ...@@ -112,13 +114,17 @@ class Typifier(Mapper):
by the function signature, it will be the target type of the new context. by the function signature, it will be the target type of the new context.
At the tree's leaves, types are applied and checked. By the above propagation rule, all leaves that share a typing At the tree's leaves, types are applied and checked. By the above propagation rule, all leaves that share a typing
context must have the exact same type (modulo constness). This type is checked at variables, and applied to context must have the exact same type (modulo constness). There the actual type checking happens.
constants. If a variable is encountered and the context does not yet have a target type, it is set to the variable's type.
If a constant is encountered, it is typified using the current target type.
It may happen that the typifier arrives at a constant before the context's target type could be figured out. If no target type is known yet, the constant will first be instantiated as a DeferredTypedConstant,
In that case, the constant will first be instantiated as a DeferredTypedConstant, and stashed in the context. and stashed in the context.
As soon as the context learns its target type, it is applied to all deferred constants. As soon as the context learns its target type, it is applied to all deferred constants.
In addition to leaves, some interior nodes may also have to be checked against the target type.
In particular, these are array accesses, struct member accesses, and calls to functions with a fixed
return type.
When a context is 'closed' during the recursive unwinding, it shall be an error if it still contains unresolved When a context is 'closed' during the recursive unwinding, it shall be an error if it still contains unresolved
constants. constants.
...@@ -142,7 +148,7 @@ class Typifier(Mapper): ...@@ -142,7 +148,7 @@ class Typifier(Mapper):
new_lhs = self.rec(lhs.expression, tc) new_lhs = self.rec(lhs.expression, tc)
assert tc.target_type is not None assert tc.target_type is not None
new_rhs = self.rec(rhs.expression, tc) new_rhs = self.rec(rhs.expression, tc)
node.lhs.expression = new_lhs node.lhs.expression = new_lhs
node.rhs.expression = new_rhs node.rhs.expression = new_rhs
...@@ -154,9 +160,8 @@ class Typifier(Mapper): ...@@ -154,9 +160,8 @@ class Typifier(Mapper):
""" """
def rec(self, expr: Any, tc: TypeContext) -> ExprOrConstant def rec(self, expr: Any, tc: TypeContext) -> ExprOrConstant
All visitor methods take an expression and the target type of the current context. All visitor methods take an expression and the current type context.
They shall return the typified expression together with its type. They shall return the typified expression, or throw `TypificationError` if typification fails.
The returned type shall always be non-const, so make sure to call deconstify if necessary.
""" """
def typify_expression( def typify_expression(
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment