diff --git a/integration_tests/CMakeLists.txt b/integration_tests/CMakeLists.txt index 6892ea7..ee610a8 100644 --- a/integration_tests/CMakeLists.txt +++ b/integration_tests/CMakeLists.txt @@ -226,3 +226,4 @@ RUN(NAME nbody_02.cpp LABELS gcc llvm NOFAST) RUN(NAME enum_01.cpp LABELS gcc llvm NOFAST) RUN(NAME enum_02.cpp LABELS gcc llvm NOFAST) RUN(NAME enum_03.cpp LABELS gcc llvm NOFAST) +RUN(NAME enum_04.cpp LABELS gcc llvm NOFAST) diff --git a/integration_tests/enum_04.cpp b/integration_tests/enum_04.cpp new file mode 100644 index 0000000..1ff8233 --- /dev/null +++ b/integration_tests/enum_04.cpp @@ -0,0 +1,56 @@ +#include +#include + +enum Color { + RED, + GREEN, + BLUE, + PINK, + WHITE, + YELLOW +}; + +struct Truck_t { + float horsepower; + int32_t seats; + double price; + enum Color color; +}; + +void init_Truck(Truck_t& truck, float horsepower_, int32_t seats_, double price_, enum Color color_) { + truck.horsepower = horsepower_; + truck.seats = seats_; + truck.price = price_; + truck.color = color_; +} + +void print_Truck(const Truck_t& car) { + std::cout << car.horsepower << " " << car.seats << " " << car.price << " " << car.color << std::endl; +} + +void assert_(bool condition) { + if( !condition ) { + exit(2); + } +} + +void test_enum_as_struct_member() { + xt::xtensor_fixed> cars; + init_Truck(cars(0), 700.0, 4, 100000.0, RED); + init_Truck(cars(1), 800.0, 5, 200000.0, BLUE); + init_Truck(cars(2), 400.0, 4, 50000.0, WHITE); + print_Truck(cars(0)); + print_Truck(cars(1)); + print_Truck(cars(2)); + assert_( cars(2).color == WHITE ); + assert_( cars(1).color == BLUE ); + assert_( cars(0).color == RED ); +} + +int main() { + +test_enum_as_struct_member(); + +return 0; + +} diff --git a/src/libasr/pass/pass_utils.h b/src/libasr/pass/pass_utils.h index d9b4a10..326e908 100644 --- a/src/libasr/pass/pass_utils.h +++ b/src/libasr/pass/pass_utils.h @@ -440,6 +440,9 @@ namespace LCompilers { if( ASR::is_a(*type) ) { ASR::Struct_t* struct_t = ASR::down_cast(type); vec.push_back(al, ASRUtils::symbol_name(struct_t->m_derived_type)); + } else if( ASR::is_a(*type) ) { + ASR::Enum_t* enum_t = ASR::down_cast(type); + vec.push_back(al, ASRUtils::symbol_name(enum_t->m_enum_type)); } } xx.m_dependencies = vec.p;