Skip to content

Commit

Permalink
Fix const ref assign (#433)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcauberer authored Jan 17, 2024
1 parent 061dba5 commit 69a38bb
Show file tree
Hide file tree
Showing 16 changed files with 138 additions and 135 deletions.
14 changes: 4 additions & 10 deletions media/test-project/test.spice
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
import "std/os/thread-pool";
f<bool> fn(int& ref) {
return false;
}

f<int> main() {
int testVar = 123;
ThreadPool tp = ThreadPool(3s);
dyn proc = p() {
printf("Test: %d\n", testVar);
};
tp.enqueue(proc);
tp.start();
tp.join();
printf("TestVar: %d\n", testVar);
fn(123);
}

/*f<int> main() {
Expand Down
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ set(SOURCES
add_executable(spice ${SOURCES} ${ANTLR_Spice_CXX_OUTPUTS})

# Enable pedantic warnings
target_compile_options(spice PRIVATE -Wpedantic -Wall)
target_compile_options(spice PRIVATE -Wpedantic -Wall -Wno-unknown-pragmas)

# Include Antlr components
include_directories(../lib/antlr4/runtime/Cpp/runtime/src)
Expand Down
4 changes: 2 additions & 2 deletions src/SourceFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ namespace spice::compiler {

SourceFile::SourceFile(GlobalResourceManager &resourceManager, SourceFile *parent, std::string name,
const std::filesystem::path &filePath, bool stdFile)
: resourceManager(resourceManager), tout(resourceManager.tout), name(std::move(name)), filePath(filePath), stdFile(stdFile),
parent(parent) {
: name(std::move(name)), filePath(filePath), stdFile(stdFile), parent(parent), resourceManager(resourceManager),
tout(resourceManager.tout) {
// Deduce fileName and fileDir
fileName = std::filesystem::path(filePath).filename().string();
fileDir = std::filesystem::path(filePath).parent_path().string();
Expand Down
2 changes: 1 addition & 1 deletion src/ast/ASTBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1138,7 +1138,7 @@ std::any ASTBuilder::visitDataType(SpiceParser::DataTypeContext *ctx) {
auto dataTypeNode = createNode<DataTypeNode>(ctx);

// Enrich
for (int i = 0; i < ctx->children.size(); i++) {
for (size_t i = 0; i < ctx->children.size(); i++) {
antlr4::tree::ParseTree *subTree = ctx->children[i];
if (auto t1 = dynamic_cast<TerminalNode *>(subTree); t1 != nullptr && t1->getSymbol()->getType() == SpiceParser::MUL) {
dataTypeNode->tmQueue.emplace(DataTypeNode::TYPE_PTR, false, 0);
Expand Down
2 changes: 1 addition & 1 deletion src/irgenerator/GenControlStructures.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ std::any IRGenerator::visitSwitchStmt(const SwitchStmtNode *node) {
llvm::Value *exprValue = resolveValue(node->assignExpr());

// Generate switch instruction
llvm::SwitchInst *switchInst = builder.CreateSwitch(exprValue, bDefault ?: bExit, caseBranches.size());
llvm::SwitchInst *switchInst = builder.CreateSwitch(exprValue, bDefault ? bDefault : bExit, caseBranches.size());

// Generate case branches
for (size_t i = 0; i < caseBranches.size(); i++) {
Expand Down
4 changes: 2 additions & 2 deletions src/symboltablebuilder/Scope.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -345,11 +345,11 @@ bool Scope::hasRefFields() {
*
* @return Number of loops
*/
size_t Scope::getLoopNestingDepth() const { // NOLINT(misc-no-recursion)
unsigned int Scope::getLoopNestingDepth() const { // NOLINT(misc-no-recursion)
assert(parent != nullptr);
if (parent->parent == nullptr)
return 0;
size_t loopCount = parent->getLoopNestingDepth();
unsigned int loopCount = parent->getLoopNestingDepth();
if (type == ScopeType::WHILE_BODY || type == ScopeType::FOR_BODY || type == ScopeType::FOREACH_BODY)
loopCount++;
return loopCount;
Expand Down
2 changes: 1 addition & 1 deletion src/symboltablebuilder/Scope.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class Scope {
[[nodiscard]] size_t getFieldCount() const;
[[nodiscard]] std::vector<Function *> getVirtualMethods();
[[nodiscard]] bool hasRefFields();
[[nodiscard]] size_t getLoopNestingDepth() const;
[[nodiscard]] unsigned int getLoopNestingDepth() const;
[[nodiscard]] bool isInCaseBranch() const;
[[nodiscard]] bool isInAsyncScope() const;
[[nodiscard]] bool doesAllowUnsafeOperations() const;
Expand Down
2 changes: 1 addition & 1 deletion src/symboltablebuilder/SymbolType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ SymbolType SymbolType::toConstReference(const ASTNode *node) const {
* @param size Size of the array
* @return Array type of the current type
*/
SymbolType SymbolType::toArray(const ASTNode *node, size_t size, bool skipDynCheck /*=false*/) const {
SymbolType SymbolType::toArray(const ASTNode *node, unsigned int size, bool skipDynCheck /*=false*/) const {
// Do not allow arrays of dyn
if (!skipDynCheck && typeChain.back().superType == TY_DYN)
throw SemanticError(node, DYN_ARRAYS_NOT_ALLOWED, "Just use the dyn type without '[]' instead");
Expand Down
10 changes: 5 additions & 5 deletions src/symboltablebuilder/SymbolType.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ class SymbolType {
public:
// Unions
union TypeChainElementData {
size_t arraySize = 0; // TY_ARRAY
Scope *bodyScope; // TY_STRUCT, TY_INTERFACE, TY_ENUM
bool hasCaptures; // TY_FUNCTION, TY_PROCEDURE (lambdas)
unsigned int arraySize; // TY_ARRAY
Scope *bodyScope = nullptr; // TY_STRUCT, TY_INTERFACE, TY_ENUM
bool hasCaptures; // TY_FUNCTION, TY_PROCEDURE (lambdas)

NLOHMANN_DEFINE_TYPE_INTRUSIVE(TypeChainElementData, arraySize)
};
Expand Down Expand Up @@ -112,7 +112,7 @@ class SymbolType {
[[nodiscard]] SymbolType toPointer(const ASTNode *node) const;
[[nodiscard]] SymbolType toReference(const ASTNode *node) const;
[[nodiscard]] SymbolType toConstReference(const ASTNode *node) const;
[[nodiscard]] SymbolType toArray(const ASTNode *node, size_t size = 0, bool skipDynCheck = false) const;
[[nodiscard]] SymbolType toArray(const ASTNode *node, unsigned int size = 0, bool skipDynCheck = false) const;
[[nodiscard]] SymbolType getContainedTy() const;
[[nodiscard]] SymbolType replaceBaseType(const SymbolType &newBaseType) const;
[[nodiscard]] llvm::Type *toLLVMType(llvm::LLVMContext &context, Scope *accessScope) const;
Expand Down Expand Up @@ -165,7 +165,7 @@ class SymbolType {
[[nodiscard]] const std::vector<SymbolType> &getTemplateTypes() const;
[[nodiscard]] bool isCoveredByGenericTypeList(std::vector<GenericType> &genericTypeList) const;
[[nodiscard]] std::string getName(bool withSize = false, bool ignorePublic = false) const;
[[nodiscard]] ALWAYS_INLINE size_t getArraySize() const {
[[nodiscard]] ALWAYS_INLINE unsigned int getArraySize() const {
assert(getSuperType() == TY_ARRAY);
return typeChain.back().data.arraySize;
}
Expand Down
Loading

0 comments on commit 69a38bb

Please sign in to comment.