@@ -446,13 +446,23 @@ bool abi_generator::is_vector(const clang::QualType& vqt) {
446
446
qt = qt->getAs <clang::ElaboratedType>()->getNamedType ();
447
447
448
448
return isa<clang::TemplateSpecializationType>(qt.getTypePtr ()) \
449
- && boost::starts_with ( get_type_name (qt, false ), " vector" );
449
+ && ( boost::starts_with ( get_type_name (qt, false ), " vector" )|| boost::starts_with ( get_type_name (qt, false ), " set " ) );
450
450
}
451
451
452
452
bool abi_generator::is_vector (const string& type_name) {
453
453
return boost::ends_with (type_name, " []" );
454
454
}
455
455
456
+ bool abi_generator::is_map (const clang::QualType& mqt) {
457
+ QualType qt (mqt);
458
+
459
+ if ( is_elaborated (qt) )
460
+ qt = qt->getAs <clang::ElaboratedType>()->getNamedType ();
461
+
462
+ return isa<clang::TemplateSpecializationType>(qt.getTypePtr ()) \
463
+ && boost::starts_with ( get_type_name (qt, false ), " map" );
464
+ }
465
+
456
466
bool abi_generator::is_struct_specialization (const clang::QualType& qt) {
457
467
return is_struct (qt) && isa<clang::TemplateSpecializationType>(qt.getTypePtr ());
458
468
}
@@ -476,6 +486,17 @@ string abi_generator::get_vector_element_type(const string& type_name) {
476
486
return type_name;
477
487
}
478
488
489
+ std::vector<clang::QualType> abi_generator::get_map_element_type (const clang::QualType& qt)
490
+ {
491
+ const auto * tst = clang::dyn_cast<const clang::TemplateSpecializationType>(qt.getTypePtr ());
492
+ ABI_ASSERT (tst != nullptr );
493
+ const clang::TemplateArgument& arg0 = tst->getArg (0 );
494
+ const clang::TemplateArgument& arg1 = tst->getArg (1 );
495
+ std::vector<clang::QualType> varg;
496
+ varg.emplace_back (arg0.getAsType ());
497
+ varg.emplace_back (arg1.getAsType ());
498
+ return varg;
499
+ }
479
500
string abi_generator::get_type_name (const clang::QualType& qt, bool with_namespace=false ) {
480
501
auto name = clang::TypeName::getFullyQualifiedName (qt, *ast_context);
481
502
if (!with_namespace)
@@ -557,6 +578,7 @@ string abi_generator::add_vector(const clang::QualType& vqt, size_t recursion_de
557
578
558
579
auto vector_element_type = get_vector_element_type (qt);
559
580
ABI_ASSERT (!is_vector (vector_element_type), " Only one-dimensional arrays are supported" );
581
+ ABI_ASSERT (!is_map (vector_element_type), " Only one-dimensional maps are supported" );
560
582
561
583
add_type (vector_element_type, recursion_depth);
562
584
@@ -566,6 +588,54 @@ string abi_generator::add_vector(const clang::QualType& vqt, size_t recursion_de
566
588
return vector_element_type_str;
567
589
}
568
590
591
+ string abi_generator::add_map (const clang::QualType& mqt, size_t recursion_depth)
592
+ {
593
+ ABI_ASSERT ( ++recursion_depth < max_recursion_depth, " recursive definition, max_recursion_depth" );
594
+
595
+ clang::QualType qt (get_named_type_if_elaborated (mqt));
596
+
597
+ auto map_element_type_list = get_map_element_type (qt);
598
+ ABI_ASSERT (!is_map (map_element_type_list[0 ]), " Only one-dimensional maps are supported" );
599
+ ABI_ASSERT (!is_map (map_element_type_list[1 ]), " Only one-dimensional maps are supported" );
600
+
601
+ add_type (map_element_type_list[0 ], recursion_depth);
602
+ add_type (map_element_type_list[1 ], recursion_depth);
603
+
604
+ std::string map_element_type_str_0;
605
+ std::string map_element_type_str_1;
606
+ if (is_vector (map_element_type_list[0 ]))
607
+ map_element_type_str_0 = add_vector (map_element_type_list[0 ],recursion_depth);
608
+ else
609
+ map_element_type_str_0 = translate_type (get_type_name (map_element_type_list[0 ]));
610
+ if (is_vector (map_element_type_list[1 ]))
611
+ map_element_type_str_1 = add_vector (map_element_type_list[1 ],recursion_depth);
612
+ else
613
+ map_element_type_str_1 = translate_type (get_type_name (map_element_type_list[1 ]));
614
+
615
+ static uint64_t index = 1 ;
616
+ std::string index_name = std::to_string (index );
617
+ std::string map_element_type_str = " map" + index_name + " []" ;
618
+ index ++;
619
+
620
+ // add struct
621
+ struct_def map_def;
622
+ map_def.name = map_element_type_str.substr (0 , map_element_type_str.length () - 2 );
623
+ map_def.base = " " ;
624
+
625
+ std::string key_field_name = " key" ;
626
+ std::string key_field_type_name = map_element_type_str_0;
627
+ field_def key_struct_field{key_field_name, key_field_type_name};
628
+
629
+ std::string value_field_name = " value" ;
630
+ std::string value_field_type_name = map_element_type_str_1;
631
+ field_def value_struct_field{value_field_name, value_field_type_name};
632
+
633
+ map_def.fields .push_back (key_struct_field);
634
+ map_def.fields .push_back (value_struct_field);
635
+
636
+ output->structs .push_back (map_def);
637
+ return map_element_type_str;
638
+ }
569
639
string abi_generator::add_type (const clang::QualType& tqt, size_t recursion_depth) {
570
640
571
641
ABI_ASSERT ( ++recursion_depth < max_recursion_depth, " recursive definition, max_recursion_depth" );
@@ -593,6 +663,11 @@ string abi_generator::add_type(const clang::QualType& tqt, size_t recursion_dept
593
663
return is_type_def ? type_name : vector_type_name;
594
664
}
595
665
666
+ if ( is_map (qt) ){
667
+ auto map_type_name = add_map (qt, recursion_depth);
668
+ return is_type_def ? type_name : map_type_name;
669
+ }
670
+
596
671
if ( is_struct (qt) ) {
597
672
return add_struct (qt, full_type_name, recursion_depth);
598
673
}
0 commit comments