Skip to content

Commit 553b309

Browse files
committed
Add unsafeConstants parameter to CompileContext
1 parent 26a8a2b commit 553b309

11 files changed

Lines changed: 23 additions & 16 deletions

‎include/scratchcpp/compilercontext.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
#pragma once
44

5+
#include <unordered_set>
6+
57
#include "global.h"
68
#include "spimpl.h"
79

@@ -16,7 +18,7 @@ class CompilerContextPrivate;
1618
class LIBSCRATCHCPP_EXPORT CompilerContext
1719
{
1820
public:
19-
CompilerContext(IEngine *engine, Target *target);
21+
CompilerContext(IEngine *engine, Target *target, const std::unordered_set<std::string> &unsafeConstants = {});
2022
CompilerContext(const CompilerContext &) = delete;
2123
virtual ~CompilerContext() { }
2224

‎src/engine/compilercontext.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
using namespace libscratchcpp;
88

99
/*! Constructs CompilerContext. */
10-
CompilerContext::CompilerContext(IEngine *engine, Target *target) :
11-
impl(spimpl::make_unique_impl<CompilerContextPrivate>(engine, target))
10+
CompilerContext::CompilerContext(IEngine *engine, Target *target, const std::unordered_set<std::string> &unsafeConstants) :
11+
impl(spimpl::make_unique_impl<CompilerContextPrivate>(engine, target, unsafeConstants))
1212
{
1313
}
1414

‎src/engine/compilercontext_p.cpp‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44

55
using namespace libscratchcpp;
66

7-
CompilerContextPrivate::CompilerContextPrivate(IEngine *engine, Target *target) :
7+
CompilerContextPrivate::CompilerContextPrivate(IEngine *engine, Target *target, const std::unordered_set<std::string> &unsafeConstants) :
88
engine(engine),
9-
target(target)
9+
target(target),
10+
unsafeConstants(unsafeConstants)
1011
{
1112
}

‎src/engine/compilercontext_p.h‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
#pragma once
44

5+
#include <unordered_set>
6+
#include <string>
7+
58
namespace libscratchcpp
69
{
710

@@ -10,10 +13,11 @@ class Target;
1013

1114
struct CompilerContextPrivate
1215
{
13-
CompilerContextPrivate(IEngine *engine, Target *target);
16+
CompilerContextPrivate(IEngine *engine, Target *target, const std::unordered_set<std::string> &unsafeConstants);
1417

1518
IEngine *engine = nullptr;
1619
Target *target = nullptr;
20+
const std::unordered_set<std::string> unsafeConstants;
1721
};
1822

1923
} // namespace libscratchcpp

‎src/engine/internal/codegen/codebuilderfactory.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ std::shared_ptr<ICodeBuilder> CodeBuilderFactory::create(CompilerContext *ctx, B
1919
return std::make_shared<LLVMCodeBuilder>(static_cast<LLVMCompilerContext *>(ctx), procedurePrototype, codeType);
2020
}
2121

22-
std::shared_ptr<CompilerContext> CodeBuilderFactory::createCtx(IEngine *engine, Target *target) const
22+
std::shared_ptr<CompilerContext> CodeBuilderFactory::createCtx(IEngine *engine, Target *target, const std::unordered_set<std::string> &unsafeConstants) const
2323
{
24-
auto ptr = std::make_shared<LLVMCompilerContext>(engine, target);
24+
auto ptr = std::make_shared<LLVMCompilerContext>(engine, target, unsafeConstants);
2525
return ptr;
2626
}

‎src/engine/internal/codegen/codebuilderfactory.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class LIBSCRATCHCPP_TEST_EXPORT CodeBuilderFactory : public ICodeBuilderFactory
1313
public:
1414
static std::shared_ptr<CodeBuilderFactory> instance();
1515
std::shared_ptr<ICodeBuilder> create(CompilerContext *ctx, BlockPrototype *procedurePrototype, Compiler::CodeType codeType) const override;
16-
std::shared_ptr<CompilerContext> createCtx(IEngine *engine, Target *target) const override;
16+
std::shared_ptr<CompilerContext> createCtx(IEngine *engine, Target *target, const std::unordered_set<std::string> &unsafeConstants = {}) const override;
1717

1818
private:
1919
static std::shared_ptr<CodeBuilderFactory> m_instance;

‎src/engine/internal/codegen/icodebuilderfactory.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class ICodeBuilderFactory
1616
virtual ~ICodeBuilderFactory() { }
1717

1818
virtual std::shared_ptr<ICodeBuilder> create(CompilerContext *ctx, BlockPrototype *procedurePrototype = nullptr, Compiler::CodeType codeType = Compiler::CodeType::Script) const = 0;
19-
virtual std::shared_ptr<CompilerContext> createCtx(IEngine *engine, Target *target) const = 0;
19+
virtual std::shared_ptr<CompilerContext> createCtx(IEngine *engine, Target *target, const std::unordered_set<std::string> &unsafeConstants = {}) const = 0;
2020
};
2121

2222
} // namespace libscratchcpp

‎src/engine/internal/codegen/llvm/llvmcompilercontext.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717

1818
using namespace libscratchcpp;
1919

20-
LLVMCompilerContext::LLVMCompilerContext(IEngine *engine, Target *target) :
21-
CompilerContext(engine, target),
20+
LLVMCompilerContext::LLVMCompilerContext(IEngine *engine, Target *target, const std::unordered_set<std::string> &unsafeConstants) :
21+
CompilerContext(engine, target, unsafeConstants),
2222
m_llvmCtx(std::make_unique<llvm::LLVMContext>()),
2323
m_module(std::make_unique<llvm::Module>(target ? target->name() : "", *m_llvmCtx)),
2424
m_llvmCtxPtr(m_llvmCtx.get()),

‎src/engine/internal/codegen/llvm/llvmcompilercontext.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ using function_id_t = unsigned int;
2828
class LIBSCRATCHCPP_TEST_EXPORT LLVMCompilerContext : public CompilerContext
2929
{
3030
public:
31-
LLVMCompilerContext(IEngine *engine, Target *target);
31+
LLVMCompilerContext(IEngine *engine, Target *target, const std::unordered_set<std::string> &unsafeConstants = {});
3232

3333
void preoptimize() override;
3434

‎test/compiler/compiler_test.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class CompilerTest : public testing::Test
3131
m_ctx = std::make_shared<CompilerContext>(&m_engine, &m_target);
3232
CompilerPrivate::setBuilderFactory(&m_builderFactory);
3333

34-
EXPECT_CALL(m_builderFactory, createCtx(&m_engine, &m_target)).WillOnce(Return(m_ctx));
34+
EXPECT_CALL(m_builderFactory, createCtx(&m_engine, &m_target, _)).WillOnce(Return(m_ctx));
3535
m_compiler = std::make_unique<Compiler>(&m_engine, &m_target);
3636

3737
m_builder = std::make_shared<CodeBuilderMock>();
@@ -1873,7 +1873,7 @@ TEST_F(CompilerTest, Preoptimize)
18731873
{
18741874
auto ctx = std::make_shared<CompilerContextMock>(&m_engine, &m_target);
18751875

1876-
EXPECT_CALL(m_builderFactory, createCtx(&m_engine, &m_target)).WillOnce(Return(ctx));
1876+
EXPECT_CALL(m_builderFactory, createCtx(&m_engine, &m_target, _)).WillOnce(Return(ctx));
18771877
Compiler compiler(&m_engine, &m_target);
18781878

18791879
EXPECT_CALL(*ctx, preoptimize());

0 commit comments

Comments
 (0)