Skip to content

Commit ab25091

Browse files
committed
Add CompilerLocalVariable class
1 parent f6396e9 commit ab25091

File tree

8 files changed

+118
-0
lines changed

8 files changed

+118
-0
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ if (LIBSCRATCHCPP_USE_LLVM)
7676
include/scratchcpp/dev/compiler.h
7777
include/scratchcpp/dev/compilervalue.h
7878
include/scratchcpp/dev/compilerconstant.h
79+
include/scratchcpp/dev/compilerlocalvariable.h
7980
include/scratchcpp/dev/executablecode.h
8081
include/scratchcpp/dev/executioncontext.h
8182
include/scratchcpp/dev/promise.h
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
#pragma once
4+
5+
#include "compiler.h"
6+
7+
namespace libscratchcpp
8+
{
9+
10+
class CompilerLocalVariablePrivate;
11+
12+
/*! \brief The CompilerLocalVariable class represents a statically typed local variable in compiled code. */
13+
class LIBSCRATCHCPP_EXPORT CompilerLocalVariable
14+
{
15+
public:
16+
CompilerLocalVariable(CompilerValue *ptr);
17+
CompilerLocalVariable(const CompilerLocalVariable &) = delete;
18+
19+
CompilerValue *ptr() const;
20+
Compiler::StaticType type() const;
21+
22+
private:
23+
spimpl::unique_impl_ptr<CompilerLocalVariablePrivate> impl;
24+
};
25+
26+
} // namespace libscratchcpp

src/dev/engine/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ target_sources(scratchcpp
99
compilerconstant.cpp
1010
compilerconstant_p.cpp
1111
compilerconstant_p.h
12+
compilerlocalvariable.cpp
13+
compilerlocalvariable_p.cpp
14+
compilerlocalvariable_p.h
1215
executioncontext.cpp
1316
executioncontext_p.cpp
1417
executioncontext_p.h
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
#include <scratchcpp/dev/compilerlocalvariable.h>
4+
#include <scratchcpp/dev/compilervalue.h>
5+
6+
#include "compilerlocalvariable_p.h"
7+
8+
using namespace libscratchcpp;
9+
10+
CompilerLocalVariable::CompilerLocalVariable(CompilerValue *ptr) :
11+
impl(spimpl::make_unique_impl<CompilerLocalVariablePrivate>(ptr))
12+
{
13+
}
14+
15+
CompilerValue *CompilerLocalVariable::ptr() const
16+
{
17+
return impl->ptr;
18+
}
19+
20+
Compiler::StaticType CompilerLocalVariable::type() const
21+
{
22+
return impl->ptr->type();
23+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
#include <cassert>
4+
5+
#include "compilerlocalvariable_p.h"
6+
7+
using namespace libscratchcpp;
8+
9+
CompilerLocalVariablePrivate::CompilerLocalVariablePrivate(CompilerValue *ptr) :
10+
ptr(ptr)
11+
{
12+
assert(ptr);
13+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
#pragma once
4+
5+
namespace libscratchcpp
6+
{
7+
8+
class CompilerValue;
9+
10+
struct CompilerLocalVariablePrivate
11+
{
12+
CompilerLocalVariablePrivate(CompilerValue *ptr);
13+
CompilerLocalVariablePrivate(CompilerLocalVariablePrivate &) = delete;
14+
15+
CompilerValue *ptr = nullptr;
16+
};
17+
18+
} // namespace libscratchcpp

test/dev/compiler/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ add_executable(
33
compiler_test.cpp
44
compilervalue_test.cpp
55
compilerconstant_test.cpp
6+
compilerlocalvariable_test.cpp
67
)
78

89
target_link_libraries(
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <scratchcpp/dev/compilerlocalvariable.h>
2+
#include <scratchcpp/dev/compilervalue.h>
3+
#include <gtest/gtest.h>
4+
5+
using namespace libscratchcpp;
6+
7+
TEST(CompilerLocalVariableTest, Constructors)
8+
{
9+
CompilerValue ptr(Compiler::StaticType::Number);
10+
CompilerLocalVariable var(&ptr);
11+
ASSERT_EQ(var.ptr(), &ptr);
12+
}
13+
14+
TEST(CompilerLocalVariableTest, Type)
15+
{
16+
{
17+
CompilerValue ptr(Compiler::StaticType::Number);
18+
CompilerLocalVariable var(&ptr);
19+
ASSERT_EQ(var.type(), ptr.type());
20+
}
21+
22+
{
23+
CompilerValue ptr(Compiler::StaticType::Bool);
24+
CompilerLocalVariable var(&ptr);
25+
ASSERT_EQ(var.type(), ptr.type());
26+
}
27+
28+
{
29+
CompilerValue ptr(Compiler::StaticType::String);
30+
CompilerLocalVariable var(&ptr);
31+
ASSERT_EQ(var.type(), ptr.type());
32+
}
33+
}

0 commit comments

Comments
 (0)