@@ -210,6 +210,17 @@ class ClangASTtoASRVisitor: public clang::RecursiveASTVisitor<ClangASTtoASRVisit
210210 return dummy_variable_sym;
211211 }
212212
213+ ASR::asr_t * make_Assignment_t_util (Allocator &al, const Location &a_loc,
214+ ASR::expr_t * a_target, ASR::expr_t * a_value, ASR::stmt_t * a_overloaded) {
215+ if ( ASR::is_a<ASR::AddressOf_t>(*a_value) ) {
216+ a_value = ASRUtils::EXPR (ASR::make_Var_t (al, a_value->base .loc ,
217+ ASR::down_cast<ASR::AddressOf_t>(a_value)->m_v ));
218+ return ASR::make_Associate_t (al, a_loc, a_target, a_value);
219+ }
220+
221+ return ASR::make_Assignment_t (al, a_loc, a_target, a_value, a_overloaded);
222+ }
223+
213224 void construct_program () {
214225 // Convert the main function into a program
215226 ASR::TranslationUnit_t* tu = (ASR::TranslationUnit_t*)this ->tu ;
@@ -236,7 +247,7 @@ class ClangASTtoASRVisitor: public clang::RecursiveASTVisitor<ClangASTtoASRVisit
236247
237248 ASR::symbol_t * exit_variable_sym = declare_dummy_variable (" exit_code" , program_scope, loc, int32_type);
238249 ASR::expr_t * variable_var = ASRUtils::EXPR (ASR::make_Var_t (al, loc, exit_variable_sym));
239- ASR::asr_t * assign_stmt = ASR::make_Assignment_t (al, loc, variable_var, ASRUtils::EXPR (func_call_main), nullptr );
250+ ASR::asr_t * assign_stmt = make_Assignment_t_util (al, loc, variable_var, ASRUtils::EXPR (func_call_main), nullptr );
240251
241252 prog_body.push_back (al, ASRUtils::STMT (assign_stmt));
242253
@@ -726,7 +737,7 @@ class ClangASTtoASRVisitor: public clang::RecursiveASTVisitor<ClangASTtoASRVisit
726737 ASR::expr_t * value = ASRUtils::EXPR (tmp.get ());
727738 cast_helper (obj, value, true );
728739 ASRUtils::make_ArrayBroadcast_t_util (al, Lloc (x), obj, value);
729- tmp = ASR::make_Assignment_t (al, Lloc (x), obj, value, nullptr );
740+ tmp = make_Assignment_t_util (al, Lloc (x), obj, value, nullptr );
730741 is_stmt_created = true ;
731742 }
732743 assignment_target = nullptr ;
@@ -737,7 +748,7 @@ class ClangASTtoASRVisitor: public clang::RecursiveASTVisitor<ClangASTtoASRVisit
737748 if ( !is_stmt_created ) {
738749 ASR::expr_t * value = ASRUtils::EXPR (tmp.get ());
739750 cast_helper (obj, value, true );
740- tmp = ASR::make_Assignment_t (al, Lloc (x), obj, value, nullptr );
751+ tmp = make_Assignment_t_util (al, Lloc (x), obj, value, nullptr );
741752 is_stmt_created = true ;
742753 }
743754 assignment_target = nullptr ;
@@ -962,7 +973,7 @@ class ClangASTtoASRVisitor: public clang::RecursiveASTVisitor<ClangASTtoASRVisit
962973 ASR::abiType::Source, false , target_physical_type, override_physical_type, false );
963974 ASR::expr_t * new_shape = ASRUtils::cast_to_descriptor (al, args.p [0 ]);
964975 tmp = ASR::make_ArrayReshape_t (al, Lloc (x), callee, new_shape, target_type, nullptr );
965- tmp = ASR::make_Assignment_t (al, Lloc (x), callee, ASRUtils::EXPR (tmp.get ()), nullptr );
976+ tmp = make_Assignment_t_util (al, Lloc (x), callee, ASRUtils::EXPR (tmp.get ()), nullptr );
966977 is_stmt_created = true ;
967978 } else if (sf == SpecialFunc::Size) {
968979 if ( args.size () != 0 ) {
@@ -982,7 +993,7 @@ class ClangASTtoASRVisitor: public clang::RecursiveASTVisitor<ClangASTtoASRVisit
982993
983994 ASR::expr_t * arg = args.p [0 ];
984995 ASRUtils::make_ArrayBroadcast_t_util (al, Lloc (x), callee, arg);
985- tmp = ASR::make_Assignment_t (al, Lloc (x), callee, arg, nullptr );
996+ tmp = make_Assignment_t_util (al, Lloc (x), callee, arg, nullptr );
986997 is_stmt_created = true ;
987998 } else if ( sf == SpecialFunc::All ) {
988999 // Handles xt::all() - no arguments
@@ -1331,7 +1342,7 @@ class ClangASTtoASRVisitor: public clang::RecursiveASTVisitor<ClangASTtoASRVisit
13311342 if ( tmp != nullptr && !is_stmt_created ) {
13321343 ASR::expr_t * init_val = ASRUtils::EXPR (tmp.get ());
13331344 add_reshape_if_needed (init_val, var);
1334- tmp = ASR::make_Assignment_t (al, Lloc (x), var, init_val, nullptr );
1345+ tmp = make_Assignment_t_util (al, Lloc (x), var, init_val, nullptr );
13351346 is_stmt_created = true ;
13361347 }
13371348 }
@@ -1545,7 +1556,7 @@ class ClangASTtoASRVisitor: public clang::RecursiveASTVisitor<ClangASTtoASRVisit
15451556 if ( op == clang::BO_Assign ) {
15461557 cast_helper (x_lhs, x_rhs, true );
15471558 ASRUtils::make_ArrayBroadcast_t_util (al, Lloc (x), x_lhs, x_rhs);
1548- tmp = ASR::make_Assignment_t (al, Lloc (x), x_lhs, x_rhs, nullptr );
1559+ tmp = make_Assignment_t_util (al, Lloc (x), x_lhs, x_rhs, nullptr );
15491560 is_stmt_created = true ;
15501561 } else {
15511562 bool is_binop = false , is_cmpop = false ;
@@ -1689,22 +1700,10 @@ class ClangASTtoASRVisitor: public clang::RecursiveASTVisitor<ClangASTtoASRVisit
16891700 }
16901701
16911702 void cast_helper (ASR::expr_t *& left, ASR::expr_t *& right, bool is_assign) {
1692- bool no_cast = ((ASR::is_a<ASR::Pointer_t>(*ASRUtils::expr_type (left)) &&
1693- ASR::is_a<ASR::Var_t>(*left)) ||
1694- (ASR::is_a<ASR::Pointer_t>(*ASRUtils::expr_type (right)) &&
1695- ASR::is_a<ASR::Var_t>(*right)));
16961703 ASR::ttype_t *right_type = ASRUtils::expr_type (right);
16971704 ASR::ttype_t *left_type = ASRUtils::expr_type (left);
16981705 left_type = ASRUtils::extract_type (left_type);
16991706 right_type = ASRUtils::extract_type (right_type);
1700- if ( no_cast ) {
1701- int lkind = ASRUtils::extract_kind_from_ttype_t (left_type);
1702- int rkind = ASRUtils::extract_kind_from_ttype_t (right_type);
1703- if ( left_type->type != right_type->type || lkind != rkind ) {
1704- throw SemanticError (" Casting for mismatching pointer types not supported yet." ,
1705- right_type->base .loc );
1706- }
1707- }
17081707
17091708 // Handle presence of logical types in binary operations
17101709 // by converting them into 32-bit integers.
@@ -1886,7 +1885,7 @@ class ClangASTtoASRVisitor: public clang::RecursiveASTVisitor<ClangASTtoASRVisit
18861885 ASR::symbol_t * return_sym = current_scope->resolve_symbol (" __return_var" );
18871886 ASR::expr_t * return_var = ASRUtils::EXPR (ASR::make_Var_t (al, Lloc (x), return_sym));
18881887 TraverseStmt (x->getRetValue ());
1889- tmp = ASR::make_Assignment_t (al, Lloc (x), return_var, ASRUtils::EXPR (tmp.get ()), nullptr );
1888+ tmp = make_Assignment_t_util (al, Lloc (x), return_var, ASRUtils::EXPR (tmp.get ()), nullptr );
18901889 current_body->push_back (al, ASRUtils::STMT (tmp.get ()));
18911890 tmp = ASR::make_Return_t (al, Lloc (x));
18921891 is_stmt_created = true ;
@@ -1939,7 +1938,7 @@ class ClangASTtoASRVisitor: public clang::RecursiveASTVisitor<ClangASTtoASRVisit
19391938 ASR::expr_t * x_rhs = ASRUtils::EXPR (tmp.get ());
19401939 CreateBinOp (x_lhs, x_rhs, ASR::binopType::Add, Lloc (x));
19411940 ASR::expr_t * sum_expr = ASRUtils::EXPR (tmp.get ());
1942- tmp = ASR::make_Assignment_t (al, Lloc (x), x_lhs, sum_expr, nullptr );
1941+ tmp = make_Assignment_t_util (al, Lloc (x), x_lhs, sum_expr, nullptr );
19431942 is_stmt_created = true ;
19441943 return true ;
19451944 }
@@ -1956,7 +1955,7 @@ class ClangASTtoASRVisitor: public clang::RecursiveASTVisitor<ClangASTtoASRVisit
19561955 ASRUtils::EXPR (ASR::make_IntegerConstant_t (
19571956 al, Lloc (x), 1 , ASRUtils::expr_type (var))),
19581957 ASRUtils::expr_type (var), nullptr ));
1959- tmp = ASR::make_Assignment_t (al, Lloc (x), var, incbyone, nullptr );
1958+ tmp = make_Assignment_t_util (al, Lloc (x), var, incbyone, nullptr );
19601959 is_stmt_created = true ;
19611960 break ;
19621961 }
@@ -1968,8 +1967,31 @@ class ClangASTtoASRVisitor: public clang::RecursiveASTVisitor<ClangASTtoASRVisit
19681967 CreateLogicalNot (var, Lloc (x));
19691968 break ;
19701969 }
1970+ case clang::UnaryOperatorKind::UO_AddrOf: {
1971+ if ( !ASR::is_a<ASR::Var_t>(*var) ) {
1972+ throw std::runtime_error (" Address of operator is only supported for symbols." );
1973+ }
1974+
1975+ ASR::Var_t* var_t = ASR::down_cast<ASR::Var_t>(var);
1976+ tmp = ASR::make_AddressOf_t (al, Lloc (x), var_t ->m_v , ASRUtils::TYPE (
1977+ ASR::make_Pointer_t (al, Lloc (x), ASRUtils::expr_type (var))));
1978+ is_stmt_created = false ;
1979+ break ;
1980+ }
1981+ case clang::UnaryOperatorKind::UO_Deref: {
1982+ if ( !ASR::is_a<ASR::Var_t>(*var) ) {
1983+ throw std::runtime_error (" Dereference operator is only supported for symbols." );
1984+ }
1985+
1986+ ASR::Var_t* var_t = ASR::down_cast<ASR::Var_t>(var);
1987+ tmp = ASR::make_DereferencePointer_t (al, Lloc (x), var_t ->m_v ,
1988+ ASRUtils::type_get_past_pointer (ASRUtils::expr_type (var)));
1989+ is_stmt_created = false ;
1990+ break ;
1991+ }
19711992 default : {
1972- throw std::runtime_error (" Only postfix increment and minus are supported so far." );
1993+ throw std::runtime_error (" Only postfix increment, minus and "
1994+ " address of operators are supported so far." );
19731995 }
19741996 }
19751997 return true ;
0 commit comments