Skip to content

Commit

Permalink
Merge pull request #72 from AjayBrahmakshatriya/master
Browse files Browse the repository at this point in the history
Added support for types to be const and volatile qualified
  • Loading branch information
AjayBrahmakshatriya authored Apr 22, 2024
2 parents 9e098d4 + 5d60a58 commit 767068e
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 9 deletions.
3 changes: 3 additions & 0 deletions include/blocks/var.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ class type : public block {
public:
typedef std::shared_ptr<type> Ptr;

bool is_const = false;
bool is_volatile = false;

virtual void accept(block_visitor *a) override {
a->visit(self<type>());
}
Expand Down
36 changes: 36 additions & 0 deletions include/builder/block_type_extractor.h
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,42 @@ class type_extractor<T[]> {
}
};

template <typename T>
class type_extractor<const T> {
public:
static block::type::Ptr extract_type(void) {
block::type::Ptr type = type_extractor<T>::extract_type();
type->is_const = true;
return type;
}

};

template <typename T>
class type_extractor<volatile T> {
public:
static block::type::Ptr extract_type(void) {
block::type::Ptr type = type_extractor<T>::extract_type();
type->is_volatile = true;
return type;
}

};

template <typename T>
class type_extractor<const volatile T> {
public:
static block::type::Ptr extract_type(void) {
block::type::Ptr type = type_extractor<T>::extract_type();
type->is_volatile = true;
type->is_const = true;
return type;
}

};



// Type extractor for complete closure
template <typename T>
class type_extractor<dyn_var<T>> {
Expand Down
3 changes: 3 additions & 0 deletions include/builder/builder_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ class builder {
push_to_sequence(block_expr);
}

builder(const unsigned long &a): builder((unsigned long long)a){}
builder(const long &a): builder((long long)a){}

builder(const double &a) {
if (builder_precheck()) {
builder_from_sequence();
Expand Down
14 changes: 10 additions & 4 deletions samples/outputs.var_names/sample30
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ STMT_BLOCK
DECL_STMT
SCALAR_TYPE (UNSIGNED_LONG_INT)
VAR (f_5)
NO_INITIALIZATION
INT_CONST (5)
DECL_STMT
SCALAR_TYPE (LONG_LONG_INT)
VAR (g_6)
Expand Down Expand Up @@ -63,17 +63,22 @@ STMT_BLOCK
VAR_EXPR
VAR (n_13)
STRING_CONST ("new string")
DECL_STMT
POINTER_TYPE
SCALAR_TYPE (CHAR)
VAR (o_14)
STRING_CONST ("Hello world")
DECL_STMT
SCALAR_TYPE (INT)
VAR (x_14)
VAR (x_15)
INT_CONST (0)
{
short int a_0;
unsigned short int b_1;
int c_2;
unsigned int d_3;
long int e_4;
unsigned long int f_5;
unsigned long int f_5 = 5ll;
long long int g_6;
unsigned long long int h_7 = 4ll;
char i_8;
Expand All @@ -83,5 +88,6 @@ STMT_BLOCK
void* m_12;
char n_13[] = "Hello world";
n_13 = "new string";
int x_14 = 0;
char const* const volatile o_14 = "Hello world";
int x_15 = 0;
}
14 changes: 10 additions & 4 deletions samples/outputs/sample30
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ STMT_BLOCK
DECL_STMT
SCALAR_TYPE (UNSIGNED_LONG_INT)
VAR (var5)
NO_INITIALIZATION
INT_CONST (5)
DECL_STMT
SCALAR_TYPE (LONG_LONG_INT)
VAR (var6)
Expand Down Expand Up @@ -64,16 +64,21 @@ STMT_BLOCK
VAR (var13)
STRING_CONST ("new string")
DECL_STMT
SCALAR_TYPE (INT)
POINTER_TYPE
SCALAR_TYPE (CHAR)
VAR (var14)
STRING_CONST ("Hello world")
DECL_STMT
SCALAR_TYPE (INT)
VAR (var15)
INT_CONST (0)
{
short int var0;
unsigned short int var1;
int var2;
unsigned int var3;
long int var4;
unsigned long int var5;
unsigned long int var5 = 5ll;
long long int var6;
unsigned long long int var7 = 4ll;
char var8;
Expand All @@ -83,5 +88,6 @@ STMT_BLOCK
void* var12;
char var13[] = "Hello world";
var13 = "new string";
int var14 = 0;
char const* const volatile var14 = "Hello world";
int var15 = 0;
}
4 changes: 3 additions & 1 deletion samples/sample30.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ static void foo(void) {
dyn_var<int> c;
dyn_var<unsigned int> d;
dyn_var<long> e;
dyn_var<unsigned long> f;
dyn_var<unsigned long> f = (unsigned long)5;
dyn_var<long long> g;
dyn_var<unsigned long long> h = (unsigned long long)4;
dyn_var<char> i;
Expand All @@ -26,6 +26,8 @@ static void foo(void) {
dyn_var<char[]> n = "Hello world";
n = "new string";

dyn_var<const char* const volatile> o = "Hello world";

// bool test, fixes a bug
// that causes false as an init value creates a variable
// without context
Expand Down
8 changes: 8 additions & 0 deletions src/blocks/c_code_generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,8 @@ void c_code_generator::visit(scalar_type::Ptr type) {
default:
assert(false && "Invalid scalar type");
}
if (type->is_const) oss << " const";
if (type->is_volatile) oss << " volatile";
}
void c_code_generator::visit(named_type::Ptr type) {
oss << type->type_name;
Expand All @@ -235,20 +237,26 @@ void c_code_generator::visit(named_type::Ptr type) {
}
oss << ">";
}
if (type->is_const) oss << " const";
if (type->is_volatile) oss << " volatile";
}
void c_code_generator::visit(pointer_type::Ptr type) {
if (!isa<scalar_type>(type->pointee_type) && !isa<pointer_type>(type->pointee_type) &&
!isa<named_type>(type->pointee_type))
assert(false && "Printing pointers of complex type is not supported yet");
type->pointee_type->accept(this);
oss << "*";
if (type->is_const) oss << " const";
if (type->is_volatile) oss << " volatile";
}
void c_code_generator::visit(reference_type::Ptr type) {
if (!isa<scalar_type>(type->referenced_type) && !isa<pointer_type>(type->referenced_type) &&
!isa<named_type>(type->referenced_type))
assert(false && "Printing pointers of complex type is not supported yet");
type->referenced_type->accept(this);
oss << "&";
if (type->is_const) oss << " const";
if (type->is_volatile) oss << " volatile";
}
void c_code_generator::visit(array_type::Ptr type) {
if (!isa<scalar_type>(type->element_type) && !isa<pointer_type>(type->element_type) &&
Expand Down

0 comments on commit 767068e

Please sign in to comment.