Skip to content

Commit

Permalink
Add two new semantic checks for dtors (#187)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcauberer authored Aug 11, 2022
1 parent 0bdeade commit 5331d41
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 0 deletions.
9 changes: 9 additions & 0 deletions media/test-project/os-test.spice
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ f<int> main() {
printf("Hello %s!", p1.getSecond());
}*/

type TestStruct struct {
bool test
}

p TestStruct.dtor(int test) {
printf("Dtor called");
}

f<int> main() {
TestStruct t = TestStruct();
printf("Test: %d\n", 0o0000777);
}
7 changes: 7 additions & 0 deletions src/analyzer/AnalyzerVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ std::any AnalyzerVisitor::visitFctDef(FctDefNode *node) {
}

if (runNumber == 1) { // First run
// Check if name is dtor
if (node->functionName == "dtor")
throw err->get(node->codeLoc, DTOR_MUST_BE_PROCEDURE, "Destructors are not allowed to be of type function");

// Create a new scope
node->fctScope = currentScope = currentScope->createChildBlock(node->getScopeId(), SCOPE_FUNC_PROC_BODY);

Expand Down Expand Up @@ -365,6 +369,9 @@ std::any AnalyzerVisitor::visitProcDef(ProcDefNode *node) {
}
}

if (node->hasParams && node->procedureName == "dtor")
throw err->get(node->codeLoc, DTOR_WITH_PARAMS, "It is not allowed to specify parameters for destructors");

// Visit arguments in new scope
std::vector<std::string> argNames;
ArgList argTypes;
Expand Down
4 changes: 4 additions & 0 deletions src/exception/SemanticError.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ std::string SemanticError::getMessagePrefix(SemanticErrorType type) {
return "Missing return statement";
case INVALID_PARAM_ORDER:
return "Invalid argument order";
case DTOR_MUST_BE_PROCEDURE:
return "Destructor must be a procedure";
case DTOR_WITH_PARAMS:
return "Destructors must not have parameters";
case OPERATOR_WRONG_DATA_TYPE:
return "Wrong data type for operator";
case UNEXPECTED_DYN_TYPE_SA:
Expand Down
2 changes: 2 additions & 0 deletions src/exception/SemanticError.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ enum SemanticErrorType {
GLOBAL_OF_INVALID_TYPE,
FUNCTION_WITHOUT_RETURN_STMT,
INVALID_PARAM_ORDER,
DTOR_MUST_BE_PROCEDURE,
DTOR_WITH_PARAMS,
OPERATOR_WRONG_DATA_TYPE,
UNEXPECTED_DYN_TYPE_SA,
REASSIGN_CONST_VARIABLE,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Semantic error in ./test-files/analyzer/methods/error-destructor-is-function/source.spice:5:1: Destructor must be a procedure: Destructors are not allowed to be of type function
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
type TestStruct struct {
bool test
}

f<bool> TestStruct.dtor() {
printf("Dtor called");
return false;
}

f<int> main() {
TestStruct t = TestStruct();
printf("Test: %d\n", 0o0000777);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Semantic error in ./test-files/analyzer/methods/error-destructor-with-args/source.spice:5:1: Destructors must not have parameters: It is not allowed to specify parameters for destructors
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
type TestStruct struct {
bool test
}

p TestStruct.dtor(int test) {
printf("Dtor called");
}

f<int> main() {
TestStruct t = TestStruct();
printf("Test: %d\n", 0o0000777);
}

0 comments on commit 5331d41

Please sign in to comment.