Class: KipperTypeChecker
compiler.KipperTypeChecker
Kipper Type Checker, which asserts that type logic and cohesion is valid and throws errors in case that an invalid use of types and identifiers is detected.
Since
0.7.0
Hierarchy
-
↳
KipperTypeChecker
Table of contents
Constructors
Properties
Methods
- assertError
- ensureValidGenericType
- error
- findPropertyInObject
- getCheckedType
- getTypeFromIdentifier
- getTypeOfMemberAccessExpression
- notImplementedError
- objectLikeIsIndexableOrAccessible
- refTargetCallable
- setTracebackData
- validArgumentValue
- validArithmeticExpression
- validArrayExpression
- validAssignment
- validBitwiseExpression
- validBracketNotationKey
- validConditionalExpression
- validConversion
- validFunctionCallArguments
- validInstanceofClassType
- validMatchesInterfaceType
- validRelationalExpression
- validReturnCodePathsInFunctionBody
- validReturnStatement
- validSliceNotationKey
- validUnaryExpression
- validVariableDefinition
Constructors
constructor
• new KipperTypeChecker(programCtx
)
Parameters
Name | Type |
---|---|
programCtx |
KipperProgramContext |
Overrides
KipperSemanticsAsserter.constructor
Defined in
kipper/core/src/compiler/semantics/analyser/type-checker.ts:83
Properties
col
• Protected
col: undefined
| number
Inherited from
Defined in
kipper/core/src/compiler/semantics/analyser/err-handler/semantics-error-handler.ts:16
ctx
• Protected
ctx: undefined
| CompilableASTNode
<SemanticData
, TypeData
>
Inherited from
Defined in
kipper/core/src/compiler/semantics/analyser/err-handler/semantics-error-handler.ts:17
filePath
• Protected
filePath: undefined
| string
Inherited from
KipperSemanticsAsserter.filePath
Defined in
kipper/core/src/compiler/semantics/analyser/err-handler/semantics-error-handler.ts:18
line
• Protected
line: undefined
| number
Inherited from
Defined in
kipper/core/src/compiler/semantics/analyser/err-handler/semantics-error-handler.ts:15
programCtx
• Readonly
programCtx: KipperProgramContext
Inherited from
KipperSemanticsAsserter.programCtx
Defined in
kipper/core/src/compiler/semantics/analyser/err-handler/semantics-asserter.ts:18
stream
• Protected
stream: undefined
| KipperFileStream
Inherited from
KipperSemanticsAsserter.stream
Defined in
kipper/core/src/compiler/semantics/analyser/err-handler/semantics-error-handler.ts:19
Methods
assertError
▸ Protected
assertError(error
, overwriteCtx?
): KipperError
Updates the given error and adds the proper traceback data, and returns it.
Parameters
Name | Type | Description |
---|---|---|
error |
KipperError |
The error to update. |
overwriteCtx? |
CompilableASTNode <SemanticData , TypeData > |
The context to overwrite the current context with. This is used when a parent checks children semantics in order to provide the correct traceback data and errors occur in the children. |
Returns
The Kipper error.
Inherited from
KipperSemanticsAsserter.assertError
Defined in
kipper/core/src/compiler/semantics/analyser/err-handler/semantics-asserter.ts:33
ensureValidGenericType
▸ ensureValidGenericType(type
, args
): void
Ensures that the given generic type is valid by checking whether the provided generic arguments match the generic type's constraints.
As generics are not fully implemented, this only checks for the number of arguments.
Parameters
Name | Type | Description |
---|---|---|
type |
ProcessedType | GenericType <GenericTypeArguments > |
The generic type to check. |
args |
ProcessedType [] |
The generic arguments to check. |
Returns
void
Defined in
kipper/core/src/compiler/semantics/analyser/type-checker.ts:166
error
▸ Protected
error(error
): KipperError
Updates the error and adds the proper traceback data, and returns it.
This function also automatically logs the error.
Parameters
Name | Type | Description |
---|---|---|
error |
KipperError |
The error to update. |
Returns
The Kipper error.
Inherited from
Defined in
kipper/core/src/compiler/semantics/analyser/err-handler/semantics-error-handler.ts:52
findPropertyInObject
▸ findPropertyInObject(obj
, identifier
): ProcessedType
Searches for the given identifier in the object or throws an error if it can't be found.
Throws
If the property does not exist.
Since
0.12.0
Parameters
Name | Type | Description |
---|---|---|
obj |
CustomType | BuiltInTypeObj |
The object which should be searched. |
identifier |
string |
The identifier to search for. |
Returns
The type of the property
Defined in
kipper/core/src/compiler/semantics/analyser/type-checker.ts:143
getCheckedType
▸ getCheckedType(rawType
, scope
): ProcessedType
Creates a new ProcessedType instance based on the passed KipperType.
If the rawType is invalid, the function will still return a ProcessedType, but the field isCompilable will be false and the instance WILL NOT be usable for a compilation.
Parameters
Name | Type | Description |
---|---|---|
rawType |
RawType |
The unchecked rawType to analyse. |
scope |
Scope <any , any , any > |
The scope to check in. |
Returns
Defined in
kipper/core/src/compiler/semantics/analyser/type-checker.ts:111
getTypeFromIdentifier
▸ getTypeFromIdentifier(type
, scope
): ScopeTypeDeclaration
Fetches the type from the identifier and throws an error if the type is not found.
Since
0.7.0
Parameters
Name | Type | Description |
---|---|---|
type |
string |
The type to check. |
scope |
Scope <any , any , any > |
The scope to check in. |
Returns
Defined in
kipper/core/src/compiler/semantics/analyser/type-checker.ts:93
getTypeOfMemberAccessExpression
▸ getTypeOfMemberAccessExpression(memberAccess
): ProcessedType
Get the type that this member access expression is accessing.
Since
0.10.0
Parameters
Name | Type | Description |
---|---|---|
memberAccess |
MemberAccessExpression |
The member access expression to get the type for. |
Returns
Defined in
kipper/core/src/compiler/semantics/analyser/type-checker.ts:729
notImplementedError
▸ notImplementedError(error
): KipperNotImplementedError
Modifies the metadata for a KipperNotImplementedError
Since
0.7.0
Parameters
Name | Type | Description |
---|---|---|
error |
KipperNotImplementedError |
The KipperNotImplementedError instance. |
Returns
Inherited from
KipperSemanticsAsserter.notImplementedError
Defined in
kipper/core/src/compiler/semantics/analyser/err-handler/semantics-asserter.ts:54
objectLikeIsIndexableOrAccessible
▸ objectLikeIsIndexableOrAccessible(objLike
, accessType
): void
Checks whether the members of the passed objLike can be accessed. (As well if there are members)
Throws
If the object expression is not an object.
Since
0.10.0
Parameters
Name | Type | Description |
---|---|---|
objLike |
Expression <ExpressionSemantics , ExpressionTypeSemantics , CompilableASTNode <SemanticData , TypeData >> |
The object-like expression to check. |
accessType |
"dot" | "bracket" | "slice" |
The type of accessor that is used to access the members. |
Returns
void
Defined in
kipper/core/src/compiler/semantics/analyser/type-checker.ts:657
refTargetCallable
▸ refTargetCallable(ref
): void
Asserts that the passed ref is callable. This function will only check ScopeDeclaration instances, since built-in functions will always be callable.
Throws
If the passed ref is not callable.
Since
0.10.0
Parameters
Name | Type | Description |
---|---|---|
ref |
ScopeDeclaration | Expression <ExpressionSemantics , ExpressionTypeSemantics , CompilableASTNode <SemanticData , TypeData >> |
The reference to check. |
Returns
void
Defined in
kipper/core/src/compiler/semantics/analyser/type-checker.ts:204
setTracebackData
▸ setTracebackData(data
): void
Sets the traceback related metadata that will be used to create a traceback for an KipperError instance.
In case that data.ctx is set and other fields are missing, these fields will be filled up using the data in the data.ctx AST node ctx instance.
Since
0.8.0
Parameters
Name | Type | Description |
---|---|---|
data |
Object |
The traceback data that will be used to initialise an KipperError instance. |
data.col? |
number |
- |
data.ctx? |
CompilableASTNode <SemanticData , TypeData > |
- |
data.filePath? |
string |
- |
data.line? |
number |
- |
data.stream? |
KipperFileStream |
- |
Returns
void
Inherited from
KipperSemanticsAsserter.setTracebackData
Defined in
kipper/core/src/compiler/semantics/analyser/err-handler/semantics-error-handler.ts:29
validArgumentValue
▸ validArgumentValue(arg
, receivedType
, identifier?
): void
Asserts that the argument type matches the type of the argument value passed.
Example
call print("x"); // <-- Parameter type 'str' must match type of argument "x"
Throws
If the given argument type does not match the parameter type.
Since
0.7.0
Parameters
Name | Type | Description |
---|---|---|
arg |
ParameterDeclaration | ProcessedType | BuiltInFunctionArgument |
The parameter that the value was passed to. |
receivedType |
ProcessedType |
The type that was received. |
identifier? |
string |
- |
Returns
void
Defined in
kipper/core/src/compiler/semantics/analyser/type-checker.ts:305
validArithmeticExpression
▸ validArithmeticExpression(leftOp
, rightOp
, op
): void
Asserts that the passed type allows the arithmetic operation.
Throws
If the type of the left or right operand is not a number, and the operation is not a concatenation of strings.
Since
0.9.0
Parameters
Name | Type | Description |
---|---|---|
leftOp |
Expression <ExpressionSemantics , ExpressionTypeSemantics , CompilableASTNode <SemanticData , TypeData >> |
The left validBracketNotationKeyoperand expression. |
rightOp |
Expression <ExpressionSemantics , ExpressionTypeSemantics , CompilableASTNode <SemanticData , TypeData >> |
The right operand expression. |
op |
KipperArithmeticOperator |
The arithmetic operation that is performed. |
Returns
void
Defined in
kipper/core/src/compiler/semantics/analyser/type-checker.ts:448
validArrayExpression
▸ validArrayExpression(param
): void
Checks whether the passed array expression is valid.
This for now only checks that the types of the array elements are always the same.
Since
0.12.0
Parameters
Name | Type | Description |
---|---|---|
param |
ArrayPrimaryExpression |
The array primary expression to check. |
Returns
void
Defined in
kipper/core/src/compiler/semantics/analyser/type-checker.ts:806
validAssignment
▸ validAssignment(assignmentExp
): void
Asserts that the passed expression is valid and the assigned value is compatible with the identifier.
Throws
In case a arithmetic assignment operation is used with an invalid type.
Throws
If the value type can not be assigned to the identifier type.
Since
0.7.0
Parameters
Name | Type | Description |
---|---|---|
assignmentExp |
AssignmentExpression |
The assignment expression to check. |
Returns
void
Defined in
kipper/core/src/compiler/semantics/analyser/type-checker.ts:230
validBitwiseExpression
▸ validBitwiseExpression(leftOp
, rightOp
, op
): void
Asserts that the passed type allows the bitwise operation.
Throws
If the type of the left or right operand is not a number.
Since
0.11.0
Parameters
Name | Type | Description |
---|---|---|
leftOp |
Expression <ExpressionSemantics , ExpressionTypeSemantics , CompilableASTNode <SemanticData , TypeData >> |
The left operand expression. |
rightOp |
Expression <ExpressionSemantics , ExpressionTypeSemantics , CompilableASTNode <SemanticData , TypeData >> |
The right operand expression. |
op |
KipperBitwiseOperator |
The bitwise operation that is performed. |
Returns
void
Defined in
kipper/core/src/compiler/semantics/analyser/type-checker.ts:487
validBracketNotationKey
▸ validBracketNotationKey(objLike
, key
): void
Ensures the passed key may be used to access the members of the passed objLike.
Throws
In case the key type can not be used to index the object-like expression.
Since
0.10.0
Parameters
Name | Type | Description |
---|---|---|
objLike |
Expression <ExpressionSemantics , ExpressionTypeSemantics , CompilableASTNode <SemanticData , TypeData >> |
The object-like expression to check. |
key |
Expression <ExpressionSemantics , ExpressionTypeSemantics , CompilableASTNode <SemanticData , TypeData >> |
The key that accesses the members of the object-like expression. |
Returns
void
Defined in
kipper/core/src/compiler/semantics/analyser/type-checker.ts:684
validConditionalExpression
▸ validConditionalExpression(trueBranch
, falseBranch
): void
Checks whether the conditional expression is valid.
Throws
When the branch types are mismatching, as union types are not implemented yet.
Since
0.11.0
Parameters
Name | Type | Description |
---|---|---|
trueBranch |
Expression <ExpressionSemantics , ExpressionTypeSemantics , CompilableASTNode <SemanticData , TypeData >> |
The expression which is called when the condition evaluates to true. |
falseBranch |
Expression <ExpressionSemantics , ExpressionTypeSemantics , CompilableASTNode <SemanticData , TypeData >> |
The expression which is called when the condition evaluates to false. |
Returns
void
Defined in
kipper/core/src/compiler/semantics/analyser/type-checker.ts:781
validConversion
▸ validConversion(operand
, targetType
): void
Asserts that the type conversion for the operand is valid.
Throws
If the conversion is invalid/impossible.
Since
0.8.0
Parameters
Name | Type | Description |
---|---|---|
operand |
Expression <ExpressionSemantics , ExpressionTypeSemantics , CompilableASTNode <SemanticData , TypeData >> |
The expression to convert. |
targetType |
ProcessedType |
The type to convert to. |
Returns
void
Defined in
kipper/core/src/compiler/semantics/analyser/type-checker.ts:509
validFunctionCallArguments
▸ validFunctionCallArguments(func
, args
): void
Asserts that the passed function arguments are valid.
Throws
If the amount of arguments is invalid e.g. too many or too few.
Throws
If any given argument type does not match the required parameter type.
Since
0.7.0
Parameters
Name | Type | Description |
---|---|---|
func |
ScopeDeclaration | Expression <ExpressionSemantics , ExpressionTypeSemantics , CompilableASTNode <SemanticData , TypeData >> |
The function that is called. |
args |
Expression <ExpressionSemantics , ExpressionTypeSemantics , CompilableASTNode <SemanticData , TypeData >>[] |
The arguments for the call expression. |
Returns
void
Defined in
kipper/core/src/compiler/semantics/analyser/type-checker.ts:344
validInstanceofClassType
▸ validInstanceofClassType(type
): void
Checks whether the passed object expression is valid.
Since
0.12.0
Parameters
Name | Type | Description |
---|---|---|
type |
ProcessedType |
The object primary expression to check. |
Returns
void
Defined in
kipper/core/src/compiler/semantics/analyser/type-checker.ts:827
validMatchesInterfaceType
▸ validMatchesInterfaceType(patternType
): void
Checks whether the passed expression can be checked against the given interface pattern.
Since
0.12.0
Parameters
Name | Type | Description |
---|---|---|
patternType |
ProcessedType |
The pattern to check against. |
Returns
void
Defined in
kipper/core/src/compiler/semantics/analyser/type-checker.ts:839
validRelationalExpression
▸ validRelationalExpression(exp
): void
Asserts that the passed relational expression is valid.
Throws
If the value types can not be compared.
Since
0.9.0
Parameters
Name | Type | Description |
---|---|---|
exp |
RelationalExpression |
The relational expression to check. |
Returns
void
Defined in
kipper/core/src/compiler/semantics/analyser/type-checker.ts:375
validReturnCodePathsInFunctionBody
▸ validReturnCodePathsInFunctionBody(func
): void
Ensures that the body of the func has valid return statements and all code paths return a value.
Requires func.typeSemantics to be set.
Throws
If not all code paths return a value.
Since
0.10.0
Parameters
Name | Type | Description |
---|---|---|
func |
FunctionDeclaration | LambdaPrimaryExpression |
The function where the body should be checked. |
Returns
void
Defined in
kipper/core/src/compiler/semantics/analyser/type-checker.ts:573
validReturnStatement
▸ validReturnStatement(returnStatement
): void
Asserts that the passed return statement is valid.
Throws
If the type of the return value does not match the return type of the function.
Since
0.10.0
Parameters
Name | Type | Description |
---|---|---|
returnStatement |
ReturnStatement |
The return statement to check. |
Returns
void
Defined in
kipper/core/src/compiler/semantics/analyser/type-checker.ts:542
validSliceNotationKey
▸ validSliceNotationKey(objLike
, key
): void
Ensures the passed slice keys may be used to access the members of the passed objLike.
Throws
In case the key type can not be used to index the object-like expression.
Since
0.10.0
Parameters
Name | Type | Description |
---|---|---|
objLike |
Expression <ExpressionSemantics , ExpressionTypeSemantics , CompilableASTNode <SemanticData , TypeData >> |
The object-like expression to check. |
key |
Object |
The key that accesses the members of the object-like expression. |
key.end? |
Expression <ExpressionSemantics , ExpressionTypeSemantics , CompilableASTNode <SemanticData , TypeData >> |
- |
key.start? |
Expression <ExpressionSemantics , ExpressionTypeSemantics , CompilableASTNode <SemanticData , TypeData >> |
- |
Returns
void
Defined in
kipper/core/src/compiler/semantics/analyser/type-checker.ts:706
validUnaryExpression
▸ validUnaryExpression(operand
): void
Asserts that the passed unary expression is valid by checking its operand.semanticData.operand operand and operand.semanticData.operator operator.
Throws
If the operand is not a valid operand for the operator.
Since
0.9.0
Parameters
Name | Type | Description |
---|---|---|
operand |
IncrementOrDecrementPostfixExpression | UnaryExpression <UnaryExpressionSemantics , UnaryExpressionTypeSemantics > |
The unary expression to check. (Also includes IncrementOrDecrementPostfixExpression, since even if it's a postfix expression, it's still a unary expression) |
Returns
void
Defined in
kipper/core/src/compiler/semantics/analyser/type-checker.ts:403
validVariableDefinition
▸ validVariableDefinition(scopeEntry
, value
): void
Asserts that this variable definition is valid and the assigned value is compatible with the identifier.
Throws
If the assignment value type is not compatible with the definition type.
Since
0.7.0
Parameters
Name | Type | Description |
---|---|---|
scopeEntry |
ScopeVariableDeclaration |
The scope entry/variable being assigned to. |
value |
Expression <ExpressionSemantics , ExpressionTypeSemantics , CompilableASTNode <SemanticData , TypeData >> |
The right expression/value of the assignment. |
Returns
void
Defined in
kipper/core/src/compiler/semantics/analyser/type-checker.ts:278