@@ -475,6 +475,14 @@ class ClangASTtoASRVisitor: public clang::RecursiveASTVisitor<ClangASTtoASRVisit
475
475
} else {
476
476
throw std::runtime_error (std::string (" Unrecognized type " ) + template_name);
477
477
}
478
+ } else if ( clang_type->getTypeClass () == clang::Type::TypeClass::Record ) {
479
+ const clang::CXXRecordDecl* record_type = clang_type->getAsCXXRecordDecl ();
480
+ std::string struct_name = record_type->getNameAsString ();
481
+ ASR::symbol_t * struct_t = current_scope->resolve_symbol (struct_name);
482
+ if ( !struct_t ) {
483
+ throw std::runtime_error (struct_name + " not defined." );
484
+ }
485
+ type = ASRUtils::TYPE (ASR::make_Struct_t (al, l, struct_t ));
478
486
} else {
479
487
throw std::runtime_error (" clang::QualType not yet supported " +
480
488
std::string (clang_type->getTypeClassName ()));
@@ -486,6 +494,40 @@ class ClangASTtoASRVisitor: public clang::RecursiveASTVisitor<ClangASTtoASRVisit
486
494
return type;
487
495
}
488
496
497
+ bool TraverseCXXRecordDecl (clang::CXXRecordDecl* x) {
498
+ SymbolTable* parent_scope = current_scope;
499
+ current_scope = al.make_new <SymbolTable>(parent_scope);
500
+ std::string struct_name = x->getNameAsString ();
501
+ Vec<char *> field_names; field_names.reserve (al, 1 );
502
+ for ( auto field = x->field_begin (); field != x->field_end (); field++ ) {
503
+ clang::FieldDecl* field_decl = *field;
504
+ TraverseFieldDecl (field_decl);
505
+ field_names.push_back (al, s2c (al, field_decl->getNameAsString ()));
506
+ }
507
+ ASR::symbol_t * struct_t = ASR::down_cast<ASR::symbol_t >(ASR::make_StructType_t (al, Lloc (x), current_scope,
508
+ s2c (al, struct_name), nullptr , 0 , field_names.p , field_names.size (), ASR::abiType::Source,
509
+ ASR::accessType::Public, false , x->isAbstract (), nullptr , 0 , nullptr , nullptr ));
510
+ parent_scope->add_symbol (struct_name, struct_t );
511
+ current_scope = parent_scope;
512
+ return true ;
513
+ }
514
+
515
+ bool TraverseFieldDecl (clang::FieldDecl* x) {
516
+ std::string name = x->getName ().str ();
517
+ if ( current_scope->get_symbol (name) ) {
518
+ throw std::runtime_error (name + std::string (" is already defined." ));
519
+ }
520
+
521
+ ASR::ttype_t *asr_type = ClangTypeToASRType (x->getType ());
522
+ ASR::symbol_t *v = ASR::down_cast<ASR::symbol_t >(ASR::make_Variable_t (al, Lloc (x),
523
+ current_scope, s2c (al, name), nullptr , 0 , ASR::intentType::Local, nullptr , nullptr ,
524
+ ASR::storage_typeType::Default, asr_type, nullptr , ASR::abiType::Source,
525
+ ASR::accessType::Public, ASR::presenceType::Required, false ));
526
+ current_scope->add_symbol (name, v);
527
+ is_stmt_created = false ;
528
+ return true ;
529
+ }
530
+
489
531
bool TraverseParmVarDecl (clang::ParmVarDecl* x) {
490
532
std::string name = x->getName ().str ();
491
533
if ( name == " " ) {
@@ -549,8 +591,33 @@ class ClangASTtoASRVisitor: public clang::RecursiveASTVisitor<ClangASTtoASRVisit
549
591
}
550
592
551
593
bool TraverseMemberExpr (clang::MemberExpr* x) {
552
- member_name_obj.set (x->getMemberDecl ()->getNameAsString ());
553
- return clang::RecursiveASTVisitor<ClangASTtoASRVisitor>::TraverseMemberExpr (x);
594
+ TraverseStmt (x->getBase ());
595
+ ASR::expr_t * base = ASRUtils::EXPR (tmp.get ());
596
+ std::string member_name = x->getMemberDecl ()->getNameAsString ();
597
+ if ( ASR::is_a<ASR::Struct_t>(*ASRUtils::expr_type (base)) ) {
598
+ ASR::Struct_t* struct_t = ASR::down_cast<ASR::Struct_t>(ASRUtils::expr_type (base));
599
+ ASR::StructType_t* struct_type_t = ASR::down_cast<ASR::StructType_t>(
600
+ ASRUtils::symbol_get_past_external (struct_t ->m_derived_type ));
601
+ ASR::symbol_t * member = struct_type_t ->m_symtab ->resolve_symbol (member_name);
602
+ if ( !member ) {
603
+ throw std::runtime_error (member_name + " not found in the scope of " + struct_type_t ->m_name );
604
+ }
605
+ std::string mangled_name = current_scope->get_unique_name (
606
+ member_name + " @" + struct_type_t ->m_name );
607
+ member = ASR::down_cast<ASR::symbol_t >(ASR::make_ExternalSymbol_t (
608
+ al, Lloc (x), current_scope, s2c (al, mangled_name), member,
609
+ struct_type_t ->m_name , nullptr , 0 , s2c (al, member_name),
610
+ ASR::accessType::Public));
611
+ current_scope->add_symbol (mangled_name, member);
612
+ tmp = ASR::make_StructInstanceMember_t (al, Lloc (x), base, member,
613
+ ASRUtils::symbol_type (member), nullptr );
614
+ } else if ( special_function_map.find (member_name) != special_function_map.end () ) {
615
+ member_name_obj.set (member_name);
616
+ return clang::RecursiveASTVisitor<ClangASTtoASRVisitor>::TraverseMemberExpr (x);
617
+ } else {
618
+ throw std::runtime_error (" clang::MemberExpr only supported for struct and special functions." );
619
+ }
620
+ return true ;
554
621
}
555
622
556
623
bool TraverseCXXMemberCallExpr (clang::CXXMemberCallExpr* x) {
@@ -679,6 +746,15 @@ class ClangASTtoASRVisitor: public clang::RecursiveASTVisitor<ClangASTtoASRVisit
679
746
is_stmt_created = true ;
680
747
}
681
748
assignment_target = nullptr ;
749
+ } else if ( ASRUtils::is_complex (*ASRUtils::expr_type (obj)) ) {
750
+ TraverseStmt (args[1 ]);
751
+ if ( !is_stmt_created ) {
752
+ ASR::expr_t * value = ASRUtils::EXPR (tmp.get ());
753
+ cast_helper (obj, value, true );
754
+ tmp = ASR::make_Assignment_t (al, Lloc (x), obj, value, nullptr );
755
+ is_stmt_created = true ;
756
+ }
757
+ assignment_target = nullptr ;
682
758
} else {
683
759
throw std::runtime_error (" operator= is supported only for arrays." );
684
760
}
@@ -1132,10 +1208,10 @@ class ClangASTtoASRVisitor: public clang::RecursiveASTVisitor<ClangASTtoASRVisit
1132
1208
return_var = ASRUtils::EXPR (ASR::make_Var_t (al, return_sym->base .loc , return_sym));
1133
1209
}
1134
1210
1135
- tmp = ASRUtils::make_Function_t_util (al, Lloc (x), current_scope, s2c (al, name), nullptr , 0 ,
1136
- args.p , args.size (), nullptr , 0 , return_var, ASR::abiType::Source, ASR::accessType::Public ,
1137
- ASR::deftypeType::Implementation, nullptr , false , false , false , false , false , nullptr , 0 ,
1138
- false , false , false );
1211
+ tmp = ASRUtils::make_Function_t_util (al, Lloc (x), current_scope, s2c (al, name),
1212
+ nullptr , 0 , args.p , args.size (), nullptr , 0 , return_var, ASR::abiType::Source,
1213
+ ASR::accessType::Public, ASR:: deftypeType::Implementation, nullptr , false , false ,
1214
+ false , false , false , nullptr , 0 , false , false , false );
1139
1215
current_function_symbol = ASR::down_cast<ASR::symbol_t >(tmp.get ());
1140
1216
current_function = ASR::down_cast<ASR::Function_t>(current_function_symbol);
1141
1217
current_scope = current_function->m_symtab ;
@@ -2023,11 +2099,15 @@ class FindNamedClassConsumer: public clang::ASTConsumer {
2023
2099
2024
2100
public:
2025
2101
2102
+ Allocator& al;
2103
+
2026
2104
explicit FindNamedClassConsumer (clang::ASTContext *Context,
2027
- Allocator& al_, ASR::asr_t *& tu_): Visitor(Context, al_, tu_) {}
2105
+ Allocator& al_, ASR::asr_t *& tu_): al(al_), Visitor(Context, al_, tu_) {}
2028
2106
2029
2107
virtual void HandleTranslationUnit (clang::ASTContext &Context) {
2030
2108
Visitor.TraverseDecl (Context.getTranslationUnitDecl ());
2109
+ PassUtils::UpdateDependenciesVisitor dependency_visitor (al);
2110
+ dependency_visitor.visit_TranslationUnit (*((ASR::TranslationUnit_t*) Visitor.tu ));
2031
2111
}
2032
2112
2033
2113
private:
0 commit comments