Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support soft errors in type checker #293

Merged
merged 5 commits into from
Jul 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .run/spice.run.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="spice" type="CMakeRunConfiguration" factoryName="Application" PROGRAM_PARAMS="build -d -OO -ir ../../media/test-project/test.spice" REDIRECT_INPUT="false" ELEVATE="false" USE_EXTERNAL_CONSOLE="false" PASS_PARENT_ENVS_2="true" PROJECT_NAME="Spice" TARGET_NAME="spice" CONFIG_NAME="Debug" RUN_TARGET_PROJECT_NAME="Spice" RUN_TARGET_NAME="spice">
<configuration default="false" name="spice" type="CMakeRunConfiguration" factoryName="Application" PROGRAM_PARAMS="run -d -OO ../../media/test-project/test.spice" REDIRECT_INPUT="false" ELEVATE="false" USE_EXTERNAL_CONSOLE="false" PASS_PARENT_ENVS_2="true" PROJECT_NAME="Spice" TARGET_NAME="spice" CONFIG_NAME="Debug" RUN_TARGET_PROJECT_NAME="Spice" RUN_TARGET_NAME="spice">
<envs>
<env name="RUN_TESTS" value="OFF" />
<env name="SPICE_STD_DIR" value="$PROJECT_DIR$/std" />
Expand Down
11 changes: 3 additions & 8 deletions media/test-project/test.spice
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
type TestStruct struct {
int value
}

f<int> operator<<(TestStruct& ts, int i) {
return ts.value << i;
p testProc(string arg0, bool arg1 = false, int arg2) {
printf("Test");
}

f<int> main() {
TestStruct ts = TestStruct{ 123 };
ts << 123;
testProc("test", true);
}

/*import "std/os/thread";
Expand Down
4 changes: 2 additions & 2 deletions src-bootstrap/exception/IRError.spice
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import "../util/CodeLoc" as cl;
public type IRErrorType enum {
TARGET_NOT_AVAILABLE,
CANT_OPEN_OUTPUT_FILE,
WRONG_TYPE,
WRONG_OUTPUT_TYPE,
BRANCH_NOT_FOUND,
REFERENCED_UNDEFINED_FUNCTION_IR,
PRINTF_NULL_TYPE,
Expand Down Expand Up @@ -45,7 +45,7 @@ public p IRError.ctor(const IRErrorType errorType, const string message) {
f<string> IRError.getMessagePrefix(const IRErrorType errorType) {
if errorType == LinkerErrorType.TARGET_NOT_AVAILABLE { return "Selected target not available"; }
if errorType == LinkerErrorType.CANT_OPEN_OUTPUT_FILE { return "Could not open output file"; }
if errorType == LinkerErrorType.WRONG_TYPE { return "Wrong type of output file"; }
if errorType == LinkerErrorType.WRONG_OUTPUT_TYPE { return "Wrong type of output file"; }
if errorType == LinkerErrorType.BRANCH_NOT_FOUND { return "Branch not found"; }
if errorType == LinkerErrorType.REFERENCED_UNDEFINED_FUNCTION_IR { return "Referenced undefined function"; }
if errorType == LinkerErrorType.PRINTF_NULL_TYPE { return "Printf has null type"; }
Expand Down
5 changes: 3 additions & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ set(SOURCES
typechecker/InterfaceManager.h
typechecker/TypeMatcher.cpp
typechecker/TypeMatcher.h
typechecker/MacroDefs.h
# Borrow checker
borrowchecker/BorrowChecker.cpp
borrowchecker/BorrowChecker.h
Expand Down Expand Up @@ -118,14 +119,14 @@ set(SOURCES
model/GenericType.cpp
model/GenericType.h
# Exception handling
exception/ErrorManager.cpp
exception/ErrorManager.h
exception/CompilerError.cpp
exception/CompilerError.h
exception/CliError.cpp
exception/CliError.h
exception/SemanticError.cpp
exception/SemanticError.h
exception/IRError.cpp
exception/IRError.h
exception/LinkerError.cpp
exception/LinkerError.h
exception/LexerError.cpp
Expand Down
11 changes: 10 additions & 1 deletion src/SourceFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,15 @@ void SourceFile::runTypeCheckerPost() { // NOLINT(misc-no-recursion)
// GCOV_EXCL_STOP
} while (typeChecker.reVisitRequested);

// Check if there are any soft errors and if so, print them
if (!resourceManager.errorManager.softErrors.empty()) {
std::stringstream errorStream;
errorStream << "There are unresolved errors. Please fix them and recompile.";
for (const ErrorManager::SoftError &error : resourceManager.errorManager.softErrors)
errorStream << "\n\n" << error.message;
throw CompilerError(UNRESOLVED_SOFT_ERRORS, errorStream.str());
}

// Check if all dyn variables were type-inferred successfully
globalScope->checkSuccessfulTypeInference();

Expand Down Expand Up @@ -738,7 +747,7 @@ void SourceFile::printStatusMessage(const char *stage, const CompileStageIOType
outputStr << compilerStageIoTypeName[in] << " --> " << compilerStageIoTypeName[out];
outputStr << " (" << std::to_string(stageRuntime) << " ms";
if (stageRuns > 0)
outputStr << "; " << std::to_string(stageRuns) << " runs";
outputStr << "; " << std::to_string(stageRuns) << " run(s)";
outputStr << ")\n";
// Print
if (fromThread) {
Expand Down
2 changes: 2 additions & 0 deletions src/ast/ASTNodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ class FctDefNode : public ASTNode {
bool isMethod = false;
bool hasTemplateTypes = false;
bool hasParams = false;
bool isGeneric = false;
SymbolTableEntry *entry = nullptr;
TypeSpecifiers functionSpecifiers = TypeSpecifiers::of(TY_FUNCTION);
Scope *structScope = nullptr;
Expand Down Expand Up @@ -348,6 +349,7 @@ class ProcDefNode : public ASTNode {
bool isMethod = false;
bool hasTemplateTypes = false;
bool hasParams = false;
bool isGeneric = false;
bool isCtor = false;
SymbolTableEntry *entry = nullptr;
TypeSpecifiers procedureSpecifiers = TypeSpecifiers::of(TY_PROCEDURE);
Expand Down
3 changes: 2 additions & 1 deletion src/exception/CliError.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

#pragma once

#include <cstdint>
#include <exception>
#include <string>

namespace spice::compiler {

// GCOV_EXCL_START

enum CliErrorType {
enum CliErrorType : uint8_t {
INCOMPLETE_TARGET_TRIPLE,
INVALID_TARGET_TRIPLE,
SOURCE_FILE_MISSING,
Expand Down
29 changes: 27 additions & 2 deletions src/exception/CompilerError.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,20 @@

#include "CompilerError.h"

#include <util/CodeLoc.h>

namespace spice::compiler {

CompilerError::CompilerError(const CompilerErrorType &type, const std::string &message) {
errorMessage = "[Error|Compiler]:\n";
errorMessage += getMessagePrefix(type) + ": " + message;
}

CompilerError::CompilerError(const CodeLoc &codeLoc, const spice::compiler::CompilerErrorType &type, const std::string &message) {
errorMessage = "[Error|Compiler] " + codeLoc.toPrettyString() + ":\n";
errorMessage += getMessagePrefix(type) + ": " + message;
}

/**
* Get the message for this particular error instance
*
Expand All @@ -24,16 +31,34 @@ const char *CompilerError::what() const noexcept { return errorMessage.c_str();
*/
std::string CompilerError::getMessagePrefix(CompilerErrorType type) {
switch (type) {
case UNRESOLVED_SOFT_ERRORS:
return "Unresolved soft errors";
case SOURCE_FILE_NOT_FOUND:
return "Source file not found";
case CANT_OPEN_OUTPUT_FILE:
return "Could not open output file";
case WRONG_OUTPUT_TYPE:
return "Wrong type of output file";
case INTERNAL_ERROR:
return "Internal compiler error";
case IO_ERROR:
return "I/O Error";
case UNHANDLED_BRANCH:
return "Unhandled code branch";
case SOURCE_FILE_NOT_FOUND:
return "Source file not found";
case TYPE_CHECKER_RUNS_EXCEEDED:
return "Type-checker runs exceeded";
case TARGET_NOT_AVAILABLE:
return "Selected target not available";
case BRANCH_NOT_FOUND:
return "Branch not found";
case REFERENCED_UNDEFINED_FUNCTION_IR:
return "Referenced undefined function";
case PRINTF_NULL_TYPE:
return "Printf has null type";
case INVALID_FUNCTION:
return "Invalid function";
case INVALID_MODULE:
return "Invalid module";
}
return "Unknown error"; // GCOV_EXCL_LINE
}
Expand Down
28 changes: 22 additions & 6 deletions src/exception/CompilerError.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,25 @@

namespace spice::compiler {

enum CompilerErrorType { INTERNAL_ERROR, IO_ERROR, UNHANDLED_BRANCH, SOURCE_FILE_NOT_FOUND, TYPE_CHECKER_RUNS_EXCEEDED };
// Forward declarations
struct CodeLoc;

enum CompilerErrorType : uint8_t {
UNRESOLVED_SOFT_ERRORS,
SOURCE_FILE_NOT_FOUND,
CANT_OPEN_OUTPUT_FILE,
WRONG_OUTPUT_TYPE,
INTERNAL_ERROR,
IO_ERROR,
UNHANDLED_BRANCH,
TYPE_CHECKER_RUNS_EXCEEDED,
TARGET_NOT_AVAILABLE,
BRANCH_NOT_FOUND,
REFERENCED_UNDEFINED_FUNCTION_IR,
PRINTF_NULL_TYPE,
INVALID_FUNCTION,
INVALID_MODULE
};

/**
* Custom exception for errors, occurring in the general context of the compiler
Expand All @@ -18,16 +36,14 @@ class CompilerError : public std::exception {
public:
// Constructors
CompilerError(const CompilerErrorType &type, const std::string &message);
CompilerError(const CodeLoc &codeLoc, const CompilerErrorType &type, const std::string &message);

// Public methods
[[nodiscard]] const char *what() const noexcept override;
[[nodiscard]] static std::string getMessagePrefix(CompilerErrorType errorType);

private:
// Members
// Public members
std::string errorMessage;

// Private methods
[[nodiscard]] static std::string getMessagePrefix(CompilerErrorType errorType);
};

} // namespace spice::compiler
24 changes: 24 additions & 0 deletions src/exception/ErrorManager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (c) 2021-2023 ChilliBits. All rights reserved.

#include "ErrorManager.h"

#include <ast/ASTNodes.h>

namespace spice::compiler {

void ErrorManager::addSoftError(const ASTNode *astNode, SemanticErrorType errorType, const std::string &message) {
// Build error message
const SemanticError semanticError = SemanticError(astNode, errorType, message);
// Add to soft errors list
addSoftError(astNode->codeLoc, semanticError.what());
}

void ErrorManager::addSoftError(const CodeLoc &codeLoc, const std::string &message) {
// Avoid duplicate errors
for (const SoftError &error : softErrors)
if (error.codeLoc == codeLoc)
return;
softErrors.emplace_back(SoftError{codeLoc, message});
}

} // namespace spice::compiler
42 changes: 42 additions & 0 deletions src/exception/ErrorManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (c) 2021-2023 ChilliBits. All rights reserved.

#pragma once

#include <cstdint>
#include <string>

#include <exception/CompilerError.h>
#include <exception/LexerError.h>
#include <exception/LinkerError.h>
#include <exception/ParserError.h>
#include <exception/SemanticError.h>
#include <util/CodeLoc.h>

namespace spice::compiler {

// Forward declarations
class ASTNode;

class ErrorManager {
public:
// Structs
struct SoftError {
const CodeLoc codeLoc;
std::string message;
};

// Constructors
ErrorManager() = default;

// Public methods
void addSoftError(const ASTNode *astNode, SemanticErrorType errorType, const std::string &message);

// Public members
std::vector<SoftError> softErrors;

private:
// Private methods
void addSoftError(const CodeLoc &codeLoc, const std::string &message);
};

} // namespace spice::compiler
67 changes: 0 additions & 67 deletions src/exception/IRError.cpp

This file was deleted.

Loading