Skip to content

Commit db6c94b

Browse files
committed
native method handling for type-checker
1 parent 8decac5 commit db6c94b

File tree

4 files changed

+28
-2
lines changed

4 files changed

+28
-2
lines changed

src/types/ast/extractor.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1294,8 +1294,14 @@ class AstExtractor extends BaseJavaCstVisitor {
12941294
return getIdentifier(ctx.Identifier![0])
12951295
}
12961296

1297-
methodBody(ctx: JavaParser.MethodBodyCtx): AST.MethodBody {
1297+
methodBody(ctx: JavaParser.MethodBodyCtx): AST.MethodBody | undefined {
12981298
if (ctx.block) return this.visit(ctx.block)
1299+
1300+
// handle only semicolon i.e. empty block
1301+
if (ctx.Semicolon) {
1302+
return undefined
1303+
}
1304+
12991305
throw new Error('Not implemented')
13001306
}
13011307

src/types/ast/specificationTypes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -742,7 +742,7 @@ export type MethodDeclaration = {
742742
kind: 'MethodDeclaration'
743743
methodModifiers: MethodModifier[]
744744
methodHeader: MethodHeader
745-
methodBody: MethodBody
745+
methodBody: MethodBody | undefined
746746
location: Location
747747
}
748748

src/types/checker/index.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
BadOperandTypesError,
88
CannotFindSymbolError,
99
IncompatibleTypesError,
10+
MissingMethodBodyError,
1011
NotApplicableToExpressionTypeError,
1112
TypeCheckerError,
1213
TypeCheckerInternalError,
@@ -518,6 +519,19 @@ export const typeCheckBody = (node: Node, frame: Frame = Frame.globalFrame()): R
518519
errors.push(...methodErrors)
519520
break
520521
}
522+
523+
// skip type checking for bodies of native methods (admit empty body)
524+
if (bodyDeclaration.methodModifiers.map(i => i.identifier).includes('native')) {
525+
console.log(bodyDeclaration)
526+
break
527+
}
528+
529+
// empty body is error
530+
if (bodyDeclaration.methodBody === undefined) {
531+
errors.push(new MissingMethodBodyError(bodyDeclaration.location))
532+
break
533+
}
534+
521535
const { errors: checkErrors } = typeCheckBody(bodyDeclaration.methodBody, methodFrame)
522536
if (checkErrors.length > 0) errors.push(...checkErrors)
523537
break

src/types/errors.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,12 @@ export class MethodCannotBeAppliedError extends TypeCheckerError {
118118
}
119119
}
120120

121+
export class MissingMethodBodyError extends TypeCheckerError {
122+
constructor(location?: Location) {
123+
super('missing method body', location)
124+
}
125+
}
126+
121127
export class ModifierNotAllowedHereError extends TypeCheckerError {
122128
constructor(location?: Location) {
123129
super('modifier not allowed here', location)

0 commit comments

Comments
 (0)