From ef1405c3be11ac95a54e19f071fca47fae04c316 Mon Sep 17 00:00:00 2001 From: Derek Bankieris Date: Fri, 28 Oct 2016 14:37:12 -0500 Subject: [PATCH] Refactor ICG Hey buddy. I wanted to tell you this before you heard it from someone else... I refactored ICG. Now, it's gonna look a little scary at first. But I promise you'll like it when you get used to it. It'll be fine. You can trust me. #341 --- .../Interface_Code_Gen/ClassValues.cpp | 75 +-- .../codegen/Interface_Code_Gen/ClassValues.hh | 28 +- .../Interface_Code_Gen/ClassVisitor.cpp | 6 +- .../Interface_Code_Gen/ConstructValues.cpp | 76 ++- .../Interface_Code_Gen/ConstructValues.hh | 27 +- .../codegen/Interface_Code_Gen/EnumValues.cpp | 23 - .../codegen/Interface_Code_Gen/EnumValues.hh | 10 +- .../Interface_Code_Gen/FieldDescription.cpp | 47 +- .../Interface_Code_Gen/FieldDescription.hh | 3 +- .../Interface_Code_Gen/FieldVisitor.cpp | 2 +- .../Interface_Code_Gen/PrintAttributes.cpp | 4 +- .../PrintAttributesFactory.cpp | 35 - .../PrintAttributesFactory.hh | 19 - .../PrintFileContents10.cpp | 624 ++++++------------ .../Interface_Code_Gen/PrintFileContents10.hh | 2 + .../PrintFileContents13.cpp | 209 ------ .../Interface_Code_Gen/PrintFileContents13.hh | 58 -- .../PrintFileContentsBase.cpp | 75 +-- .../PrintFileContentsBase.hh | 17 +- .../Interface_Code_Gen/TypedefVisitor.cpp | 15 +- .../Interface_Code_Gen/VariableVisitor.cpp | 14 +- .../codegen/Interface_Code_Gen/main.cpp | 5 +- 22 files changed, 365 insertions(+), 1009 deletions(-) delete mode 100644 trick_source/codegen/Interface_Code_Gen/PrintAttributesFactory.cpp delete mode 100644 trick_source/codegen/Interface_Code_Gen/PrintAttributesFactory.hh delete mode 100644 trick_source/codegen/Interface_Code_Gen/PrintFileContents13.cpp delete mode 100644 trick_source/codegen/Interface_Code_Gen/PrintFileContents13.hh diff --git a/trick_source/codegen/Interface_Code_Gen/ClassValues.cpp b/trick_source/codegen/Interface_Code_Gen/ClassValues.cpp index 55f73eb8..3a59bd94 100644 --- a/trick_source/codegen/Interface_Code_Gen/ClassValues.cpp +++ b/trick_source/codegen/Interface_Code_Gen/ClassValues.cpp @@ -175,10 +175,6 @@ void ClassValues::clearAmbiguousVariables() { } -std::vector ClassValues::getFieldDescription() { - return field_descripts ; -} - void ClassValues::clearFieldDescription() { field_descripts.clear() ; } @@ -239,20 +235,6 @@ bool ClassValues::getHasPublicDestructor() { return has_public_destructor ; } -std::string ClassValues::getFullyQualifiedTypeName() { - std::ostringstream oss ; - NamespaceIterator ni ; - - for ( ni = namespace_begin() ; ni != namespace_end() ; ni++ ) { - oss << (*ni) << "::" ; - } - for ( ni = container_class_begin() ; ni != container_class_end() ; ni++ ) { - oss << (*ni) << "::" ; - } - oss << name ; - return oss.str() ; -} - void ClassValues::setMangledTypeName( std::string in_val ) { mangled_type_name = in_val ; } @@ -264,20 +246,23 @@ std::string ClassValues::getMangledTypeName() { return mangled_type_name ; } -std::string ClassValues::getFullyQualifiedMangledTypeName() { +std::string ClassValues::getFullyQualifiedMangledTypeName(const std::string& delimiter) { std::ostringstream oss ; - NamespaceIterator ni ; - - for ( ni = namespace_begin() ; ni != namespace_end() ; ni++ ) { - oss << (*ni) << "::" ; - } - for ( ni = container_class_begin() ; ni != container_class_end() ; ni++ ) { - oss << (*ni) << "::" ; - } + printNamespacesAndContainerClasses(oss, delimiter) ; oss << getMangledTypeName() ; return oss.str() ; } +std::string ClassValues::getFullyQualifiedNameIfEqual() { + std::string result; + if (getName() == getMangledTypeName()) { + result += getNamespacesAndContainerClasses(); + } + result += getName(); + return result; +} + + void ClassValues::setCompat15(bool in_val) { compat15 = in_val ; } @@ -286,38 +271,6 @@ bool ClassValues::isCompat15() { return compat15 ; } -void ClassValues::print_namespaces(std::ostream & os, const char * delimiter) { - unsigned int ii ; - for ( ii = 0 ; ii < namespaces.size() ; ii++ ) { - os << namespaces[ii] << delimiter ; - } -} - -std::ostream & operator << (std::ostream & os , ClassValues & cv ) { - os << " name = " << cv.name << std::endl ; - os << " mangled_name = " << cv.mangled_type_name << std::endl ; - os << " file_name = " << cv.file_name << std::endl ; - os << " namespaces =" ; - ConstructValues::NamespaceIterator it ; - for ( it = cv.namespace_begin() ; it != cv.namespace_end() ; it++ ) { - os << " " << *it ; - } - os << std::endl ; - os << " parent classes =" ; - for ( it = cv.container_class_begin() ; it != cv.container_class_end() ; it++ ) { - os << " " << *it ; - } - os << std::endl ; - os << " has_init_attr_friend = " << cv.has_init_attr_friend << std::endl ; - os << " is_pod = " << cv.is_pod << std::endl ; - os << " is_abstract = " << cv.is_abstract << std::endl ; - os << " has_default_constructor = " << cv.has_default_constructor << std::endl ; - os << " has_public_destructor = " << cv.has_public_destructor << std::endl ; - - ClassValues::FieldIterator fit ; - for ( fit = cv.field_begin() ; fit != cv.field_end() ; fit++ ) { - os << **fit << std::endl ; - } - - return os ; +bool ClassValues::isInStandardNamespace() { + return namespaces.size() && !namespaces[0].compare("std"); } diff --git a/trick_source/codegen/Interface_Code_Gen/ClassValues.hh b/trick_source/codegen/Interface_Code_Gen/ClassValues.hh index dcf45fea..fe8b6d10 100644 --- a/trick_source/codegen/Interface_Code_Gen/ClassValues.hh +++ b/trick_source/codegen/Interface_Code_Gen/ClassValues.hh @@ -40,28 +40,26 @@ class ClassValues : public ConstructValues { unsigned int class_offset, bool virtual_inherited) ; /** Gets the list of fields in this class */ - std::vector getFieldDescription() ; + const std::vector& getFieldDescriptions() { + return field_descripts ; + } void clearFieldDescription() ; - typedef std::vector< FieldDescription * >::iterator FieldIterator ; - FieldIterator field_begin() { return field_descripts.begin() ; } ; - FieldIterator field_end() { return field_descripts.end() ; } ; - /** Appends an inherited class name to the list this class inherits from */ void addInheritedClass( std::string class_name ) ; + /** Gets the list of inherited classes */ + const std::vector& getInheritedClasses() { + return inherited_classes ; + } + void saveInheritAncestry( ClassValues * in_cv ) ; void setContainerClassForFields() ; void clearAmbiguousVariables() ; void clearInheritedClass() ; - typedef std::vector< std::string >::iterator InheritedClassesIterator ; - InheritedClassesIterator inherit_classes_begin() { return inherited_classes.begin() ; } ; - InheritedClassesIterator inherit_classes_end() { return inherited_classes.end() ; } ; - unsigned int getNumInheritedClasses() { return inherited_classes.size() ; } ; - void setVirtualInherited(bool in_inh) ; bool isVirtualInherited() ; void setHasInitAttrFriend(bool in_val) ; @@ -76,19 +74,15 @@ class ClassValues : public ConstructValues { bool getHasDefaultConstructor() ; void setHasPublicDestructor(bool in_val) ; bool getHasPublicDestructor() ; - std::string getFullyQualifiedTypeName() ; void setMangledTypeName( std::string in_val ) ; std::string getMangledTypeName() ; - std::string getFullyQualifiedMangledTypeName() ; + std::string getFullyQualifiedMangledTypeName(const std::string& delimiter = "::") ; + std::string getFullyQualifiedNameIfEqual(); void setCompat15(bool in_val) ; bool isCompat15() ; - - void print_namespaces(std::ostream & os, const char * delimiter) ; - - friend std::ostream & operator << (std::ostream & os , ClassValues & cv ) ; + bool isInStandardNamespace(); private: - /** List of fields (data members) contained in the class */ std::vector< FieldDescription * > field_descripts ; std::map< std::string , FieldDescription * > field_name_to_info_map ; diff --git a/trick_source/codegen/Interface_Code_Gen/ClassVisitor.cpp b/trick_source/codegen/Interface_Code_Gen/ClassVisitor.cpp index cf8f9d01..e66af9e8 100644 --- a/trick_source/codegen/Interface_Code_Gen/ClassVisitor.cpp +++ b/trick_source/codegen/Interface_Code_Gen/ClassVisitor.cpp @@ -209,7 +209,7 @@ bool CXXRecordVisitor::VisitCXXRecordDecl( clang::CXXRecordDecl *rec ) { //std::cout << " " << getFileName(ci , rd->getSourceRange().getEnd(), hsd) << "" << std::endl ; CXXRecordVisitor inherit_cvis(ci , cs, hsd , pa, false) ; inherit_cvis.TraverseCXXRecordDecl(static_cast(rd)) ; - cval.addInheritedFieldDescriptions(inherit_cvis.get_class_data()->getFieldDescription(), + cval.addInheritedFieldDescriptions(inherit_cvis.get_class_data()->getFieldDescriptions(), inherit_class_offset, false) ; // clear the field list in the inherited class so they are not freed when inherit_cvis // goes out of scope. @@ -260,7 +260,7 @@ bool CXXRecordVisitor::VisitCXXRecordDecl( clang::CXXRecordDecl *rec ) { //std::cout << " " << getFileName(ci , rd->getSourceRange().getEnd(), hsd) << "" << std::endl ; CXXRecordVisitor inherit_cvis(ci , cs, hsd , pa, false) ; inherit_cvis.TraverseCXXRecordDecl(static_cast(rd)) ; - cval.addInheritedFieldDescriptions(inherit_cvis.get_class_data()->getFieldDescription(), inherit_class_offset, true) ; + cval.addInheritedFieldDescriptions(inherit_cvis.get_class_data()->getFieldDescriptions(), inherit_class_offset, true) ; // clear the field list in the inherited class so they are not freed when inherit_cvis goes out of scope. inherit_cvis.get_class_data()->clearFieldDescription() ; // If we are inheriting from a template specialization, don't save the inherited class. This list @@ -300,7 +300,7 @@ bool CXXRecordVisitor::VisitFriendDecl( clang::FriendDecl *fd ) { //nd->dump() ; std::string init_func = std::string("init_attr") ; std::stringstream name_sp ; - cval.print_namespaces( name_sp , "__") ; + cval.printNamespaces( name_sp , "__") ; init_func += name_sp.str() + cval.getName() ; std::string friend_str = nd->getName().str() ; if ( ! friend_str.find(init_func) ) { diff --git a/trick_source/codegen/Interface_Code_Gen/ConstructValues.cpp b/trick_source/codegen/Interface_Code_Gen/ConstructValues.cpp index 28d856e9..2d87a69f 100644 --- a/trick_source/codegen/Interface_Code_Gen/ConstructValues.cpp +++ b/trick_source/codegen/Interface_Code_Gen/ConstructValues.cpp @@ -1,6 +1,7 @@ #include #include +#include #include "ConstructValues.hh" @@ -39,7 +40,6 @@ void ConstructValues::getNamespacesAndClasses( const clang::DeclContext * Ctx ) for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend(); I != E; ++I) { if (const clang::NamespaceDecl *nd = clang::dyn_cast(*I)) { if (! nd->isAnonymousNamespace()) { - //std::cout << "namespace " << nd->getIdentifier()->getName().str() << std::endl ; std::string temp_name = nd->getIdentifier()->getName().str() ; if ( temp_name.compare("std") and temp_name.compare("__1")) { addNamespace(nd->getIdentifier()->getName().str()) ; @@ -47,8 +47,23 @@ void ConstructValues::getNamespacesAndClasses( const clang::DeclContext * Ctx ) } } else if (const clang::RecordDecl *rd = clang::dyn_cast(*I)) { if (rd->getIdentifier()) { - //std::cout << "in class " << rd->getName().str() << std::endl ; - addContainerClass(rd->getName().str()) ; + if (const clang::ClassTemplateSpecializationDecl *td = clang::dyn_cast(*I)) { + std::string text; + llvm::raw_string_ostream stream(text); + stream << td->getName().str() << "<"; + const clang::TemplateArgumentList& arguments = td->getTemplateArgs(); + unsigned end = arguments.size() - 1; + for (unsigned i = 0; i < end; ++i) { + arguments[i].print(printingPolicy, stream); + stream << ", "; + } + arguments[end].print(printingPolicy, stream); + stream << ">"; + addContainerClass(stream.str()); + } + else { + addContainerClass(rd->getName().str()) ; + } } } } @@ -62,16 +77,57 @@ void ConstructValues::addContainerClass( std::string in_name ) { container_classes.push_back(in_name) ; } -std::string ConstructValues::getFullyQualifiedName() { - std::ostringstream oss ; - NamespaceIterator ni ; +std::string ConstructValues::getFullyQualifiedName(const std::string& delimiter) { + std::ostringstream oss; + printNamespaces(oss, delimiter); + printContainerClasses(oss, delimiter); + oss << name; + return oss.str(); +} - for ( ni = namespace_begin() ; ni != namespace_end() ; ni++ ) { - oss << (*ni) << "::" ; +void ConstructValues::printOpenNamespaceBlocks(std::ostream& ostream) { + for (auto name : namespaces) { + ostream << "namespace " << name << " {" << std::endl; } - for ( ni = container_class_begin() ; ni != container_class_end() ; ni++ ) { - oss << (*ni) << "::" ; +} + +void ConstructValues::printCloseNamespaceBlocks(std::ostream& ostream) { + for (auto name : namespaces) { + ostream << "}" << std::endl; } +} + +void ConstructValues::printNamespaces(std::ostream& ostream, const std::string& delimiter) { + for (auto name : namespaces) { + ostream << name << delimiter; + } +} + +void ConstructValues::printContainerClasses(std::ostream& ostream, const std::string& delimiter) { + for (auto clazz : container_classes) { + ostream << clazz << delimiter; + } +} + +void ConstructValues::printNamespacesAndContainerClasses(std::ostream& ostream, const std::string& delimiter) { + printNamespaces(ostream, delimiter); + printContainerClasses(ostream, delimiter); +} + +std::string ConstructValues::getNamespacesAndContainerClasses(const std::string& delimiter) { + std::string result = ""; + for (auto name : namespaces) { + result += name + delimiter; + } + for (auto clazz : container_classes) { + result += clazz + delimiter; + } + return result; +} + +std::string ConstructValues::getFullyQualifiedTypeName(const std::string& delimiter) { + std::ostringstream oss ; + printNamespacesAndContainerClasses(oss, delimiter) ; oss << name ; return oss.str() ; } diff --git a/trick_source/codegen/Interface_Code_Gen/ConstructValues.hh b/trick_source/codegen/Interface_Code_Gen/ConstructValues.hh index 1bb93624..b84e608c 100644 --- a/trick_source/codegen/Interface_Code_Gen/ConstructValues.hh +++ b/trick_source/codegen/Interface_Code_Gen/ConstructValues.hh @@ -7,6 +7,8 @@ #include #include "clang/AST/Decl.h" +#include +#include /** @@ -39,17 +41,26 @@ class ConstructValues { void addNamespace(std::string in_name) ; - typedef std::vector< std::string >::iterator NamespaceIterator ; - NamespaceIterator namespace_begin() { return namespaces.begin() ; } ; - NamespaceIterator namespace_end() { return namespaces.end() ; } ; + const std::vector& getNamespaces() { + return namespaces; + } + void addContainerClass(std::string in_name) ; - typedef std::vector< std::string >::iterator ContainerClassIterator ; - ContainerClassIterator container_class_begin() { return container_classes.begin() ; } ; - ContainerClassIterator container_class_end() { return container_classes.end() ; } ; + const std::vector& getContainerClasses() { + return container_classes; + } - std::string getFullyQualifiedName() ; + std::string getFullyQualifiedName(const std::string& delimiter = "::") ; + + void printOpenNamespaceBlocks(std::ostream& ostream); + void printCloseNamespaceBlocks(std::ostream& ostream); + void printNamespaces(std::ostream& ostream, const std::string& delimiter = "::") ; + void printContainerClasses(std::ostream& ostream, const std::string& delimiter = "::") ; + void printNamespacesAndContainerClasses(std::ostream& ostream, const std::string& delimiter = "::") ; + std::string getNamespacesAndContainerClasses(const std::string& delimiter = "::") ; + std::string getFullyQualifiedTypeName(const std::string& delimiter = "::") ; protected: @@ -65,6 +76,8 @@ class ConstructValues { /** File where construct is defined */ std::string file_name ; + const clang::PrintingPolicy printingPolicy = clang::PrintingPolicy(clang::LangOptions()); + } ; #endif diff --git a/trick_source/codegen/Interface_Code_Gen/EnumValues.cpp b/trick_source/codegen/Interface_Code_Gen/EnumValues.cpp index 9f6986ab..4d4d6841 100644 --- a/trick_source/codegen/Interface_Code_Gen/EnumValues.cpp +++ b/trick_source/codegen/Interface_Code_Gen/EnumValues.cpp @@ -17,26 +17,3 @@ void EnumValues::setHasDefinition( bool in ) { bool EnumValues::getHasDefinition() { return has_definition ; } - -std::ostream & operator << (std::ostream & os , EnumValues & ev ) { - os << " name = " << ev.name << std::endl ; - os << " file_name = " << ev.file_name << std::endl ; - os << " namespaces =" ; - ConstructValues::NamespaceIterator it ; - for ( it = ev.namespace_begin() ; it != ev.namespace_end() ; it++ ) { - os << " " << *it ; - } - os << std::endl ; - os << " parent classes =" ; - for ( it = ev.container_class_begin() ; it != ev.container_class_end() ; it++ ) { - os << " " << *it ; - } - os << std::endl ; - - EnumValues::NameValueIterator eit ; - for ( eit = ev.begin() ; eit != ev.end() ; eit++ ) { - os << " " << (*eit).first << " " << (*eit).second << std::endl ; - } - - return os ; -} diff --git a/trick_source/codegen/Interface_Code_Gen/EnumValues.hh b/trick_source/codegen/Interface_Code_Gen/EnumValues.hh index 53fa3f7c..e1d5a7b6 100644 --- a/trick_source/codegen/Interface_Code_Gen/EnumValues.hh +++ b/trick_source/codegen/Interface_Code_Gen/EnumValues.hh @@ -32,15 +32,13 @@ class EnumValues : public ConstructValues { void addEnum(std::string in_name , long long in_val) ; - typedef std::vector< NameValuePair >::iterator NameValueIterator ; - NameValueIterator begin() { return enum_values.begin() ; } ; - NameValueIterator end() { return enum_values.end() ; } ; - - friend std::ostream & operator << (std::ostream & os , EnumValues & ev ) ; - void setHasDefinition( bool in ) ; bool getHasDefinition() ; + const std::vector& getPairs() { + return enum_values; + } + private: /** List of enums and their values */ diff --git a/trick_source/codegen/Interface_Code_Gen/FieldDescription.cpp b/trick_source/codegen/Interface_Code_Gen/FieldDescription.cpp index 5ca07774..30fe42a6 100644 --- a/trick_source/codegen/Interface_Code_Gen/FieldDescription.cpp +++ b/trick_source/codegen/Interface_Code_Gen/FieldDescription.cpp @@ -375,6 +375,13 @@ std::string FieldDescription::getMangledTypeName() { return mangled_type_name ; } +std::string FieldDescription::getFullyQualifiedMangledTypeName(const std::string& delimiter) { + std::ostringstream oss ; + printNamespacesAndContainerClasses(oss, delimiter) ; + oss << getMangledTypeName() ; + return oss.str() ; +} + std::string FieldDescription::getUnits() { return units ; } @@ -517,43 +524,3 @@ int FieldDescription::getArrayDim(unsigned int dim_num) { void FieldDescription::addArrayDim( int in_dim ) { array_sizes[num_dims++] = in_dim ; } - -std::ostream & operator << (std::ostream & os , FieldDescription & fdes ) { - os << " name = " << fdes.name << std::endl ; - os << " file_name = " << fdes.file_name << std::endl ; - os << " namespaces =" ; - ConstructValues::NamespaceIterator it ; - for ( it = fdes.namespace_begin() ; it != fdes.namespace_end() ; it++ ) { - os << " " << *it ; - } - os << std::endl ; - os << " parent classes =" ; - for ( it = fdes.container_class_begin() ; it != fdes.container_class_end() ; it++ ) { - os << " " << *it ; - } - os << std::endl ; - os << " line_no = " << fdes.line_no << std::endl ; - os << " container_class = " << fdes.container_class << std::endl ; - os << " type_name = " << fdes.type_name << std::endl ; - os << " mangled_type_name = " << fdes.mangled_type_name << std::endl ; - os << " type_enum_string = " << fdes.type_enum_string << std::endl ; - os << " units = " << fdes.units << std::endl ; - os << " io = " << fdes.io << std::endl ; - os << " description = " << fdes.description << std::endl ; - os << " access = " << fdes.access << std::endl ; - os << " is_bitfield = " << fdes.is_bitfield << std::endl ; - os << " bitfield_width = " << fdes.bitfield_width << std::endl ; - os << " bitfield_start_bit = " << fdes.bitfield_start_bit << std::endl ; - os << " bitfield_word_offset = " << fdes.bitfield_word_offset << std::endl ; - os << " num_dims = " << fdes.num_dims << std::endl ; - os << " array_sizes =" ; - for( unsigned int ii = 0 ; ii < 8 ; ii++ ) { - os << " " << fdes.array_sizes[ii] ; - } - os << std::endl ; - os << " is_enum = " << fdes.is_enum << std::endl ; - os << " is_record = " << fdes.is_record << std::endl ; - os << " is_static = " << fdes.is_static << std::endl ; - - return os ; -} diff --git a/trick_source/codegen/Interface_Code_Gen/FieldDescription.hh b/trick_source/codegen/Interface_Code_Gen/FieldDescription.hh index 8c59f763..2c5ac447 100644 --- a/trick_source/codegen/Interface_Code_Gen/FieldDescription.hh +++ b/trick_source/codegen/Interface_Code_Gen/FieldDescription.hh @@ -53,6 +53,7 @@ class FieldDescription : public ConstructValues { unsigned int getLineNo() ; void setMangledTypeName( std::string in_val ) ; std::string getMangledTypeName() ; + std::string getFullyQualifiedMangledTypeName(const std::string& delimiter = "::") ; std::string getUnits() ; bool isDashDashUnits() ; void setIO(unsigned int) ; @@ -92,8 +93,6 @@ class FieldDescription : public ConstructValues { /** Adds an array dimension to the field */ void addArrayDim( int in_dim ) ; - friend std::ostream & operator << (std::ostream & os , FieldDescription & cv ) ; - private: /** Line number in current file where field is */ diff --git a/trick_source/codegen/Interface_Code_Gen/FieldVisitor.cpp b/trick_source/codegen/Interface_Code_Gen/FieldVisitor.cpp index 7ec914fc..1faf3567 100644 --- a/trick_source/codegen/Interface_Code_Gen/FieldVisitor.cpp +++ b/trick_source/codegen/Interface_Code_Gen/FieldVisitor.cpp @@ -296,7 +296,7 @@ bool FieldVisitor::ProcessTemplate(std::string in_name , clang::CXXRecordDecl * if ( debug_level >= 4 ) { std::cout << "Added template class from FieldVisitor ProcessTemplate " ; std::cout << in_name << std::endl ; - std::cout << *fdes << std::endl ; + //std::cout << *fdes << std::endl ; } } diff --git a/trick_source/codegen/Interface_Code_Gen/PrintAttributes.cpp b/trick_source/codegen/Interface_Code_Gen/PrintAttributes.cpp index 963641c8..501ca908 100644 --- a/trick_source/codegen/Interface_Code_Gen/PrintAttributes.cpp +++ b/trick_source/codegen/Interface_Code_Gen/PrintAttributes.cpp @@ -13,7 +13,7 @@ #include "PrintAttributes.hh" #include "PrintFileContentsBase.hh" -#include "PrintAttributesFactory.hh" +#include "PrintFileContents10.hh" #include "HeaderSearchDirs.hh" #include "CommentSaver.hh" #include "ClassValues.hh" @@ -30,7 +30,7 @@ PrintAttributes::PrintAttributes(int in_attr_version , HeaderSearchDirs & in_hsd sim_services_flag( in_sim_services_flag ) , output_dir( in_output_dir ) { - printer = createFileContents(in_attr_version) ; + printer = new PrintFileContents10() ; } /** diff --git a/trick_source/codegen/Interface_Code_Gen/PrintAttributesFactory.cpp b/trick_source/codegen/Interface_Code_Gen/PrintAttributesFactory.cpp deleted file mode 100644 index a93ecfd2..00000000 --- a/trick_source/codegen/Interface_Code_Gen/PrintAttributesFactory.cpp +++ /dev/null @@ -1,35 +0,0 @@ - -#include -#include - -#include "PrintFileContentsBase.hh" -#include "PrintFileContents10.hh" -#include "PrintFileContents13.hh" - -/** returns true if the command line attributes version is supported */ -bool validAttributesVersion( int attr_version ) { - switch (attr_version) { - case 10: - case 13: - return true ; - default: - return false ; - } -} - -/** - Returns a PrintFileContentsBase pointer to a version specific - print class based on attribute version - */ -PrintFileContentsBase * createFileContents( int attr_version ) { - switch (attr_version) { - case 10: - return new PrintFileContents10() ; - case 13: - return new PrintFileContents13() ; - default: - std::cout << "OK, how did you get here with an invalid attributes version " << attr_version << std::endl ; - exit(-1) ; - } -} - diff --git a/trick_source/codegen/Interface_Code_Gen/PrintAttributesFactory.hh b/trick_source/codegen/Interface_Code_Gen/PrintAttributesFactory.hh deleted file mode 100644 index 29b2d5d6..00000000 --- a/trick_source/codegen/Interface_Code_Gen/PrintAttributesFactory.hh +++ /dev/null @@ -1,19 +0,0 @@ - -#ifndef PRINTATTRIBUTESFACTORY_HH -#define PRINTATTRIBUTESFACTORY_HH - -#include -#include - -class PrintFileContentsBase ; - -/** returns true if the command line attributes version is supported */ -bool validAttributesVersion( int attr_version ) ; - -/** - Returns a PrintFileContentsBase pointer to a version specific - print class based on attribute version - */ -PrintFileContentsBase * createFileContents( int attr_version ) ; - -#endif diff --git a/trick_source/codegen/Interface_Code_Gen/PrintFileContents10.cpp b/trick_source/codegen/Interface_Code_Gen/PrintFileContents10.cpp index b259e6f7..5f968945 100644 --- a/trick_source/codegen/Interface_Code_Gen/PrintFileContents10.cpp +++ b/trick_source/codegen/Interface_Code_Gen/PrintFileContents10.cpp @@ -19,44 +19,37 @@ void PrintFileContents10::printIOHeader(std::ostream & ostream , std::string hea } else { header_file_name = almostRealPath(header_file_name.c_str()) ; } - ostream << -"/**\n" -" * This file was automatically generated by the ICG based on the file:\n" -" * " << header_file_name << "\n" -" * This file contains database parameter declarations specific to the\n" -" * data structures and enumerated types declared in the above file.\n" -" * These database parameters are used by the Trick input and\n" -" * data recording processors to gain access to important simulation\n" -" * variable information.\n" -" */\n" -"\n" -"#define TRICK_IN_IOSRC\n" -"#include \n" -"#include \"trick/MemoryManager.hh\"\n" -"#include \"trick/attributes.h\"\n" -"#include \"trick/parameter_types.h\"\n" -"#include \"trick/ClassSizeCheck.hh\"\n" -"#include \"trick/UnitsMap.hh\"\n" -"#include \"trick/checkpoint_stl.hh\"\n" -"#include \"" << header_file_name << "\"\n" -"\n" ; - + ostream << "/**\n" + << " * This file was automatically generated by the ICG based on the file:\n" + << " * " << header_file_name << "\n" + << " * This file contains database parameter declarations specific to the\n" + << " * data structures and enumerated types declared in the above file.\n" + << " * These database parameters are used by the Trick input and\n" + << " * data recording processors to gain access to important simulation\n" + << " * variable information.\n" + << " */\n" + << "\n" + << "#define TRICK_IN_IOSRC\n" + << "#include \n" + << "#include \"trick/MemoryManager.hh\"\n" + << "#include \"trick/attributes.h\"\n" + << "#include \"trick/parameter_types.h\"\n" + << "#include \"trick/ClassSizeCheck.hh\"\n" + << "#include \"trick/UnitsMap.hh\"\n" + << "#include \"trick/checkpoint_stl.hh\"\n" + << "#include \"" << header_file_name << "\"\n" + << "\n" ; } /** Prints enumeration attributes */ void PrintFileContents10::print_enum_attr(std::ostream & ostream , EnumValues * e ) { print_open_extern_c(ostream) ; - ostream << "ENUM_ATTR enum" ; - printNamespaces( ostream, e , "__" ) ; - printContainerClasses( ostream, e , "__" ) ; - ostream << e->getName() << "[] = {\n" ; - for ( EnumValues::NameValueIterator nvit = e->begin() ; nvit != e->end() ; nvit++ ) { - ostream << "{\""; - printNamespaces( ostream, e , "::" ) ; - printContainerClasses( ostream, e , "::" ) ; - ostream << (*nvit).first << "\" , " << (*nvit).second << " , 0x0 } ,\n" ; + ostream << "ENUM_ATTR enum" << e->getFullyQualifiedTypeName("__") << "[] = {\n" ; + std::string name = e->getNamespacesAndContainerClasses(); + for (auto& pair : e->getPairs()) { + ostream << "{\"" << name << pair.first << "\", " << pair.second << ", 0x0},\n" ; } - ostream << "{\"\" , 0 , 0x0 }\n} ;\n" ; + ostream << "{\"\", 0, 0x0}\n};\n" ; print_close_extern_c(ostream) ; } @@ -64,29 +57,26 @@ void PrintFileContents10::print_enum_attr(std::ostream & ostream , EnumValues * void PrintFileContents10::print_field_attr(std::ostream & ostream , FieldDescription * fdes ) { int array_dim ; - ostream << "{ \"" << fdes->getName() << "\"" ; // name - ostream << ", \"" ; // start type_name - printNamespaces( ostream, fdes , "__" ) ; - printContainerClasses( ostream, fdes , "__" ) ; - ostream << fdes->getMangledTypeName() << "\""; // end type_name - ostream << ", \"" << fdes->getUnits() << "\"" ; // units - ostream << ", \"\", \"\"," << std::endl ; // alias , user_defined - ostream << " \"" << fdes->getDescription() << "\"," << std::endl ; // description - ostream << " " << fdes->getIO() ; // io - ostream << "," << fdes->getEnumString() ; // type + ostream << "{\"" << fdes->getName() << "\"" // name + << ", \"" << fdes->getFullyQualifiedMangledTypeName("__") << "\"" // type_name + << ", \"" << fdes->getUnits() << "\"" // units + << ", \"\", \"\"," << std::endl // alias, user_defined + << " \"" << fdes->getDescription() << "\"," << std::endl // description + << " " << fdes->getIO() // io + << "," << fdes->getEnumString() ; // type // There are several cases when printing the size of a variable. if ( fdes->isBitField() ) { // bitfields are handled in 4 byte (32 bit) chunks - ostream << ",4" ; + ostream << ", 4" ; } else if ( fdes->isRecord() or fdes->isEnum() or fdes->getTypeName().empty() ) { // records enums use io_src_get_size. The sentinel has no typename - ostream << ",0" ; + ostream << ", 0" ; } else { // print size of the underlying type - ostream << ",sizeof(" << fdes->getTypeName() << ")" ; + ostream << ", sizeof(" << fdes->getTypeName() << ")" ; } - ostream << ",0,0,Language_CPP" ; // range_min, range_max, language - ostream << "," << (fdes->isStatic() << 1) + (fdes->isDashDashUnits() << 2) << "," << std::endl ; // mods + ostream << ", 0, 0, Language_CPP" ; // range_min, range_max, language + ostream << ", " << (fdes->isStatic() << 1) + (fdes->isDashDashUnits() << 2) << "," << std::endl ; // mods if ( fdes->isBitField() ) { // For bitfields we need the offset to start on 4 byte boundaries because that is what our // insert and extract bitfield routines work with. @@ -94,23 +84,23 @@ void PrintFileContents10::print_field_attr(std::ostream & ostream , FieldDescri } else { ostream << " " << (fdes->getFieldOffset() / 8) ; // offset } - ostream << ",NULL" ; // attr - ostream << "," << fdes->getNumDims() ; // num_index + ostream << ", NULL" ; // attr + ostream << ", " << fdes->getNumDims() ; // num_index - ostream << ",{" ; + ostream << ", {" ; if ( fdes->isBitField() ) { ostream << "{" << fdes->getBitFieldWidth() ; // size of bitfield - ostream << "," << 32 - (fdes->getFieldOffset() % 32) - fdes->getBitFieldWidth() << "}" ; // start bit + ostream << ", " << 32 - (fdes->getFieldOffset() % 32) - fdes->getBitFieldWidth() << "}" ; // start bit } else { array_dim = fdes->getArrayDim(0) ; if ( array_dim < 0 ) array_dim = 0 ; - ostream << "{" << array_dim << ",0}" ; // index 0 + ostream << "{" << array_dim << ", 0}" ; // index 0 } unsigned int ii ; for ( ii = 1 ; ii < 8 ; ii++ ) { array_dim = fdes->getArrayDim(ii) ; if ( array_dim < 0 ) array_dim = 0 ; - ostream << ",{" << array_dim << ",0}" ; // indexes 1 through 7 + ostream << ", {" << array_dim << ", 0}" ; // indexes 1 through 7 } ostream << "}," << std::endl ; ostream << " NULL, NULL, NULL, NULL" ; @@ -120,19 +110,12 @@ void PrintFileContents10::print_field_attr(std::ostream & ostream , FieldDescri /** Prints class attributes */ void PrintFileContents10::print_class_attr(std::ostream & ostream , ClassValues * c ) { - unsigned int ii ; - ClassValues::FieldIterator fit ; - print_open_extern_c(ostream) ; - ostream << "ATTRIBUTES attr" ; - printNamespaces( ostream, c , "__" ) ; - printContainerClasses( ostream, c , "__" ) ; - ostream << c->getMangledTypeName() ; - ostream << "[] = {" << std::endl ; + ostream << "ATTRIBUTES attr" << c->getFullyQualifiedMangledTypeName("__") << "[] = {" << std::endl ; - for ( fit = c->field_begin() ; fit != c->field_end() ; fit++ ) { - if ( determinePrintAttr(c , *fit) ) { - print_field_attr(ostream, *fit) ; + for (auto& fieldDescription : c->getFieldDescriptions()) { + if ( determinePrintAttr(c , fieldDescription) ) { + print_field_attr(ostream, fieldDescription) ; ostream << "," << std::endl ; } } @@ -149,158 +132,80 @@ void PrintFileContents10::print_class_attr(std::ostream & ostream , ClassValues void PrintFileContents10::print_field_init_attr_stmts( std::ostream & ostream , FieldDescription * fdes , ClassValues * cv , unsigned int index ) { - // For static variables replace the offset field with the address of the static variable - if ( global_compat15 or cv->isCompat15()) { - if ( fdes->isStatic() ) { - // print a special offsetof statement if this is a static - ostream << " attr" ; - printNamespaces( ostream, cv , "__" ) ; - printContainerClasses( ostream, cv , "__" ) ; - ostream << cv->getMangledTypeName() << "[" << index << "].offset = (long)(void *)&" ; - printNamespaces( ostream, cv , "::" ) ; - printContainerClasses( ostream, cv , "::" ) ; - ostream << cv->getName() << "::" << fdes->getName() << " ;\n" ; - } else if ( fdes->isBitField() ) { - // else if this is a bitfield - ostream << " attr" ; - printNamespaces( ostream, cv , "__" ) ; - printContainerClasses( ostream, cv , "__" ) ; - ostream << cv->getMangledTypeName() << "[" << index << "].offset = " ; - ostream << fdes->getBitFieldByteOffset() << " ;\n" ; - // All bitfield offsets are in terms of unsigned ints. - ostream << " attr" ; - printNamespaces( ostream, cv , "__" ) ; - printContainerClasses( ostream, cv , "__" ) ; - ostream << cv->getMangledTypeName() << "[" << index << "].size = sizeof(unsigned int) ;\n" ; - } else if ( fdes->isVirtualInherited() ) { - // else if we have a virtually inherited class. - ostream << " attr" ; - printNamespaces( ostream, cv , "__" ) ; - printContainerClasses( ostream, cv , "__" ) ; - ostream << cv->getMangledTypeName() << "[" << index << "].offset = " << fdes->getBaseClassOffset() ; - ostream << " + offsetof(" ; - ostream << fdes->getContainerClass() << "," << fdes->getName() << ") ;\n" ; - } else if ( cv->getMangledTypeName() != cv->getName() ) { - // else if we have a template type where mangled_type_name is different then name. - ostream << " attr" ; - printNamespaces( ostream, cv , "__" ) ; - printContainerClasses( ostream, cv , "__" ) ; - ostream << cv->getMangledTypeName() << "[" << index << "].offset = offsetof(" ; - ostream << cv->getMangledTypeName() << "," << fdes->getName() << ") ;\n" ; - } else { - // else print an offsetof statement if this is not a special case - ostream << " attr" ; - printNamespaces( ostream, cv , "__" ) ; - printContainerClasses( ostream, cv , "__" ) ; - ostream << cv->getMangledTypeName() << "[" << index << "].offset = offsetof(" ; - printNamespaces( ostream, cv , "::" ) ; - printContainerClasses( ostream, cv , "::" ) ; - ostream << cv->getMangledTypeName() << "," << fdes->getName() << ") ;\n" ; + const std::string className = cv->getName(); + const std::string mangledClassName = cv->getMangledTypeName(); + const std::string fieldName = fdes->getName(); + const std::string fullyQualifiedClassNameColons = cv->getFullyQualifiedTypeName(); + const std::string fullyQualifiedMangledClassNameColons = cv->getFullyQualifiedMangledTypeName(); + const std::string fullyQualifiedMangledClassNameUnderscores = cv->getFullyQualifiedMangledTypeName("__"); + std::ostringstream oss; + oss << " attr" << fullyQualifiedMangledClassNameUnderscores << "[" << index << "]."; + const std::string prefix = oss.str(); + + if ( fdes->isStatic() ) { + ostream << prefix << "offset = (long)(void *)&" << fullyQualifiedClassNameColons << "::" << fieldName << " ;\n" ; + } + else if ( global_compat15 or cv->isCompat15() ) { + if ( fdes->isBitField() ) { + ostream << prefix << "offset = " << fdes->getBitFieldByteOffset() << " ;\n" ; + ostream << prefix << "size = sizeof(unsigned int) ;\n" ; } - } else { - if ( fdes->isStatic() ) { - ostream << " attr" ; - printNamespaces( ostream, cv , "__" ) ; - printContainerClasses( ostream, cv , "__" ) ; - ostream << cv->getMangledTypeName() << "[" << index << "].offset = (long)(void *)&" ; - printNamespaces( ostream, cv , "::" ) ; - printContainerClasses( ostream, cv , "::" ) ; - ostream << cv->getName() << "::" << fdes->getName() << " ;\n" ; + else if ( fdes->isVirtualInherited() ) { + ostream << prefix << "offset = " << fdes->getBaseClassOffset() << " + offsetof(" << fdes->getContainerClass() << ", " << fieldName << ") ;\n" ; + } + else if ( mangledClassName != className ) { + ostream << prefix << "offset = " << "offsetof(" << mangledClassName << ", " << fieldName << ") ;\n" ; + } + else { + ostream << prefix << "offset = " << "offsetof(" << fullyQualifiedMangledClassNameColons << ", " << fieldName << ") ;\n" ; } } - if ( fdes->isSTL()) { - ostream << " attr" ; - printNamespaces( ostream, cv , "__" ) ; - printContainerClasses( ostream, cv , "__" ) ; - ostream << cv->getMangledTypeName() << "[" << index << "].checkpoint_stl = checkpoint_stl_" ; - printNamespaces( ostream, cv , "__" ) ; - printContainerClasses( ostream, cv , "__" ) ; - ostream << cv->getMangledTypeName() ; - ostream << "_" ; - ostream << fdes->getName() ; - ostream << " ;\n" ; + if ( fdes->isSTL() ) { + auto print = [&](const std::string& field) { + ostream << prefix << field << " = " << field << "_" << fullyQualifiedMangledClassNameUnderscores + "_" + fieldName + " ;\n"; + }; - ostream << " attr" ; - printNamespaces( ostream, cv , "__" ) ; - printContainerClasses( ostream, cv , "__" ) ; - ostream << cv->getMangledTypeName() << "[" << index << "].post_checkpoint_stl = post_checkpoint_stl_" ; - printNamespaces( ostream, cv , "__" ) ; - printContainerClasses( ostream, cv , "__" ) ; - ostream << cv->getMangledTypeName() ; - ostream << "_" ; - ostream << fdes->getName() ; - ostream << " ;\n" ; + print("checkpoint_stl"); + print("post_checkpoint_stl"); + print("restore_stl"); - ostream << " attr" ; - printNamespaces( ostream, cv , "__" ) ; - printContainerClasses( ostream, cv , "__" ) ; - ostream << cv->getMangledTypeName() << "[" << index << "].restore_stl = restore_stl_" ; - printNamespaces( ostream, cv , "__" ) ; - printContainerClasses( ostream, cv , "__" ) ; - ostream << cv->getMangledTypeName() ; - ostream << "_" ; - ostream << fdes->getName() ; - ostream << " ;\n" ; - - if (fdes->hasSTLClear()) { - ostream << " attr" ; - printNamespaces( ostream, cv , "__" ) ; - printContainerClasses( ostream, cv , "__" ) ; - ostream << cv->getMangledTypeName() << "[" << index << "].clear_stl = clear_stl_" ; - printNamespaces( ostream, cv , "__" ) ; - printContainerClasses( ostream, cv , "__" ) ; - ostream << cv->getMangledTypeName() ; - ostream << "_" ; - ostream << fdes->getName() ; - ostream << " ;\n" ; + if ( fdes->hasSTLClear() ) { + print("clear_stl"); } } - if ( fdes->isRecord() or fdes->isEnum()) { - ostream << " trick_MM->add_attr_info(std::string(attr" ; - printNamespaces( ostream, cv , "__" ) ; - printContainerClasses( ostream, cv , "__" ) ; - ostream << cv->getMangledTypeName() << "[" << index << "].type_name) , &attr" ; - printNamespaces( ostream, cv , "__" ) ; - printContainerClasses( ostream, cv , "__" ) ; - ostream << cv->getMangledTypeName() << "[" << index << "], __FILE__ , __LINE__ ) ;\n" ; + if ( fdes->isRecord() or fdes->isEnum() ) { + ostream << " trick_MM->add_attr_info(std::string(attr" << fullyQualifiedMangledClassNameUnderscores << "[" << index << "].type_name) , &attr" << fullyQualifiedMangledClassNameUnderscores << "[" << index << "], __FILE__ , __LINE__ ) ;\n" ; } } /** Prints add_attr_info statements for each inherited class */ void PrintFileContents10::print_inherited_add_attr_info( std::ostream & ostream , ClassValues * cv ) { - ClassValues::InheritedClassesIterator cit ; - if ( cv->getNumInheritedClasses() > 0 ) { + auto inheritedClasses = cv->getInheritedClasses(); + if ( inheritedClasses.size() > 0 ) { ostream << "\n ATTRIBUTES temp_attr ;\n\n" ; } - for ( cit = cv->inherit_classes_begin() ; cit != cv->inherit_classes_end() ; cit++ ) { - ostream << " trick_MM->add_attr_info( std::string(\"" << *cit << "\"), &temp_attr , __FILE__ , __LINE__ ) ;\n" ; + for ( auto& inheritedClass : inheritedClasses ) { + ostream << " trick_MM->add_attr_info( std::string(\"" << inheritedClass << "\"), &temp_attr , __FILE__ , __LINE__ ) ;\n" ; } } /** Prints init_attr function for each class */ void PrintFileContents10::print_init_attr_func( std::ostream & ostream , ClassValues * cv ) { + cv->printOpenNamespaceBlocks(ostream) ; + ostream << "\nvoid init_attr" << cv->getFullyQualifiedMangledTypeName("__") << "() {\n\n" + << " static int initialized ;\n" + << " if (initialized) {\n" + << " return;\n" + << " }\n" + << " initialized = 1;\n\n" ; - ClassValues::FieldIterator fit ; - - printOpenNamespaceBlocks(ostream, cv) ; - ostream << "\nvoid init_attr" ; - printNamespaces( ostream, cv , "__" ) ; - printContainerClasses( ostream, cv , "__" ) ; - ostream << cv->getMangledTypeName() ; - ostream << "() {\n\n" -" static int initialized ;\n" -" if ( initialized ) {\n" -" return ;\n" -" }\n" -" initialized = 1 ;\n\n" ; - +#if 0 if ( cv->getMangledTypeName() != cv->getName() ) { ostream << " typedef " << cv->getName() << " " << cv->getMangledTypeName() << " ;\n\n" ; } -#if 0 if ( !global_compat15 and !cv->isCompat15()) { ostream << " if ( sizeof(" ; printNamespaces( ostream, cv , "::" ) ; @@ -317,296 +222,172 @@ void PrintFileContents10::print_init_attr_func( std::ostream & ostream , ClassVa } #endif - unsigned int ii = 0 ; - for ( fit = cv->field_begin() ; fit != cv->field_end() ; fit++ ) { - if ( determinePrintAttr(cv , *fit) ) { - print_field_init_attr_stmts(ostream, *fit, cv, ii) ; - ii++ ; + unsigned int ii = 0; + for ( auto field : cv->getFieldDescriptions() ) { + if ( determinePrintAttr(cv, field) ) { + print_field_init_attr_stmts(ostream, field, cv, ii++) ; } } print_inherited_add_attr_info(ostream, cv ) ; ostream << "}\n" ; - printCloseNamespaceBlocks(ostream, cv) ; + cv->printCloseNamespaceBlocks(ostream) ; } /** Prints the io_src_sizeof function for enumerations */ void PrintFileContents10::print_enum_io_src_sizeof( std::ostream & ostream , EnumValues * ev ) { print_open_extern_c(ostream) ; - ostream << "size_t io_src_sizeof_" ; - printNamespaces( ostream, ev , "__" ) ; - printContainerClasses( ostream, ev , "__" ) ; - ostream << ev->getName() << "( void ) {\n" ; + ostream << "size_t io_src_sizeof_" << ev->getFullyQualifiedName("__") << "( void ) {\n" ; if ( ev->getHasDefinition() ) { - ostream << " return( sizeof(" ; - printNamespaces( ostream, ev , "::" ) ; - printContainerClasses( ostream, ev , "::" ) ; - ostream << ev->getName() << "));\n}\n" ; + ostream << " return sizeof(" << ev->getFullyQualifiedName() << ")" ; } else { - ostream << " return(sizeof(int)) ;\n}\n" ; + ostream << " return sizeof(int)" ; } + ostream << ";\n}\n" ; print_close_extern_c(ostream) ; } /** Prints the C linkage init_attr function */ void PrintFileContents10::print_init_attr_c_intf( std::ostream & ostream , ClassValues * cv ) { - ostream << "void init_attr" ; - printNamespaces( ostream, cv , "__" ) ; - printContainerClasses( ostream, cv , "__" ) ; - ostream << cv->getMangledTypeName() << "_c_intf() {\n " ; - printNamespaces( ostream, cv , "::" ) ; - ostream << "init_attr" ; - printNamespaces( ostream, cv , "__" ) ; - printContainerClasses( ostream, cv , "__" ) ; - ostream << cv->getMangledTypeName() << "() ;\n" -"}\n\n" ; + ostream << "void init_attr" << cv->getFullyQualifiedMangledTypeName("__") << "_c_intf() {\n " ; + cv->printNamespaces( ostream, "::" ) ; + ostream << "init_attr" << cv->getFullyQualifiedMangledTypeName("__") << "() ;\n" + << "}\n\n" ; } /** Prints the io_src_sizeof function */ void PrintFileContents10::print_io_src_sizeof( std::ostream & ostream , ClassValues * cv ) { - ostream << "size_t io_src_sizeof_" ; - printNamespaces( ostream, cv , "__" ) ; - printContainerClasses( ostream, cv , "__" ) ; - ostream << cv->getMangledTypeName() << "( void ) {\n" ; - ostream << " return( sizeof(" ; - // Template types - if ( cv->getMangledTypeName() == cv->getName() ) { - printNamespaces( ostream, cv , "::" ) ; - printContainerClasses( ostream, cv , "::" ) ; - } - ostream << cv->getName() << ") );\n}\n\n" ; + ostream << "size_t io_src_sizeof_" << cv->getFullyQualifiedMangledTypeName("__") << "() {\n" ; + ostream << " return sizeof(" << cv->getFullyQualifiedNameIfEqual() << ") ;\n}\n\n" ; } /** Prints the io_src_allocate function */ void PrintFileContents10::print_io_src_allocate( std::ostream & ostream , ClassValues * cv ) { - if ( cv->isPOD() or (! cv->isAbstract() and cv->getHasDefaultConstructor()) ) { - ostream << "void * io_src_allocate_" ; - printNamespaces( ostream, cv , "__" ) ; - printContainerClasses( ostream, cv , "__" ) ; - ostream << cv->getMangledTypeName() << "( int num) {\n" ; - - ostream << " " ; - if ( cv->getMangledTypeName() == cv->getName() ) { - printNamespaces( ostream, cv , "::" ) ; - printContainerClasses( ostream, cv , "::" ) ; - } - ostream << cv->getName() << " * temp = (" ; - if ( cv->getMangledTypeName() == cv->getName() ) { - printNamespaces( ostream, cv , "::" ) ; - printContainerClasses( ostream, cv , "::" ) ; - } - ostream << cv->getName() << " * )calloc( num, sizeof(" ; - if ( cv->getMangledTypeName() == cv->getName() ) { - printNamespaces( ostream, cv , "::" ) ; - printContainerClasses( ostream, cv , "::" ) ; - } - ostream << cv->getName() << "));\n" ; + if ( cv->isPOD() or ( !cv->isAbstract() and cv->getHasDefaultConstructor()) ) { + const std::string name = cv->getFullyQualifiedNameIfEqual(); + ostream << "void* io_src_allocate_" << cv->getFullyQualifiedMangledTypeName("__") << "(int num) {\n" ; + ostream << " " << name << "* temp = (" << name << "*)calloc(num, sizeof(" << name << "));\n" ; if ( ! cv->isPOD() ) { - ostream << " for (int ii=0 ; iigetMangledTypeName() == cv->getName() ) { - printNamespaces( ostream, cv , "::" ) ; - printContainerClasses( ostream, cv , "::" ) ; - } - ostream << cv->getName() << "();\n" << " }\n" ; + ostream << " for (int ii = 0; ii < num; ++ii) {\n" ; + ostream << " new(&temp[ii]) " << name << "();\n }\n" ; } - ostream << " return ((void *)temp);\n" << "}\n\n" ; + ostream << " return (void*)temp;\n" << "}\n\n" ; } } /** Prints the io_src_allocate function */ void PrintFileContents10::print_io_src_destruct( std::ostream & ostream , ClassValues * cv ) { - if ( cv->getHasPublicDestructor()) { - ostream << "void io_src_destruct_" ; - printNamespaces( ostream, cv , "__" ) ; - printContainerClasses( ostream, cv , "__" ) ; - ostream << cv->getMangledTypeName() << "( void * addr __attribute__((unused)), int num __attribute__((unused)) ) {\n" ; + if ( cv->getHasPublicDestructor() ) { + ostream << "void io_src_destruct_" << cv->getFullyQualifiedMangledTypeName("__") << "(void* address __attribute__((unused)), int num __attribute__((unused))) {\n" ; if ( ! cv->isPOD() ) { // Add a using statement so we can call the destructor without fully qualifying it. - ClassValues::NamespaceIterator nsi = cv->namespace_begin() ; - if ( nsi != cv->namespace_end() ) { + auto namespaces = cv->getNamespaces(); + if (namespaces.size()) { ostream << " using namespace " ; - while ( nsi != cv->namespace_end() ) { - ostream << *nsi ; - nsi++ ; - if ( nsi != cv->namespace_end()) { - ostream << "::" ; + auto last = namespaces[namespaces.size() - 1]; + for (auto& name : namespaces) { + ostream << name; + if (name != last) { + ostream << "::"; } } - ostream << " ;\n" ; + ostream << ";\n " ; } - ostream << " " ; - if ( cv->getMangledTypeName() == cv->getName() ) { - printNamespaces( ostream, cv , "::" ) ; - printContainerClasses( ostream, cv , "::" ) ; + + const std::string name = cv->getName(); + const std::string qualifiedName = cv->getFullyQualifiedNameIfEqual(); + + ostream << qualifiedName << "* temp = (" << qualifiedName << "*)address;\n" ; + if ( cv->getMangledTypeName() == name ) { + ostream << " for (int ii = 0; ii < num; ++ii) {\n" + << " temp[ii].~" << name << "();\n" + << " }\n" ; } - ostream << cv->getName() << " * temp = (" ; - if ( cv->getMangledTypeName() == cv->getName() ) { - printNamespaces( ostream, cv , "::" ) ; - printContainerClasses( ostream, cv , "::" ) ; - } - ostream << cv->getName() << " * )addr ;\n" ; - ostream << " for (int ii=0 ; iigetMangledTypeName() == cv->getName() ) { - ostream << " temp[ii].~" ; - ostream << cv->getName() << "();\n" ; - } - ostream << " }\n" ; } ostream << "}\n\n" ; } } void PrintFileContents10::print_io_src_delete( std::ostream & ostream , ClassValues * cv ) { - if ( cv->getHasPublicDestructor()) { - ostream << "void io_src_delete_" ; - printNamespaces( ostream, cv , "__" ) ; - printContainerClasses( ostream, cv , "__" ) ; - ostream << cv->getMangledTypeName() << "( void * addr " ; + if ( cv->getHasPublicDestructor() ) { + ostream << "void io_src_delete_" << cv->getFullyQualifiedMangledTypeName("__") << "(void* address" ; if ( ! cv->isPOD() ) { - ostream << ") {\n" ; - ostream << " delete (" ; - if ( cv->getMangledTypeName() == cv->getName() ) { - printNamespaces( ostream, cv , "::" ) ; - printContainerClasses( ostream, cv , "::" ) ; - } - ostream << cv->getName() << "*)addr ;\n" ; + ostream << ") {\n delete (" << cv->getFullyQualifiedNameIfEqual() << "*)address;\n" ; } else { - ostream << "__attribute__((unused)) ) {" ; + ostream << " __attribute__((unused))) {" ; } ostream << "}\n" ; } } void PrintFileContents10::print_stl_helper_proto(std::ostream & ostream , ClassValues * cv ) { - std::ostringstream oss; + auto& fieldDescriptions = cv->getFieldDescriptions(); - for ( ClassValues::FieldIterator fit = cv->field_begin() ; fit != cv->field_end() ; fit++ ) { - if ( (*fit)->isSTL() and determinePrintAttr(cv , *fit) ) { - oss << "void checkpoint_stl_" ; - printNamespaces( oss, cv , "__" ) ; - printContainerClasses( oss, cv , "__" ) ; - oss << cv->getMangledTypeName() ; - oss << "_" ; - oss << (*fit)->getName() ; - oss << "(void * start_address, const char * obj_name , const char * var_name) ;" << std::endl ; + if (!fieldDescriptions.size()) { + return; + } - oss << "void post_checkpoint_stl_" ; - printNamespaces( oss, cv , "__" ) ; - printContainerClasses( oss, cv , "__" ) ; - oss << cv->getMangledTypeName() ; - oss << "_" ; - oss << (*fit)->getName() ; - oss << "(void * start_address, const char * obj_name , const char * var_name) ;" << std::endl ; + print_open_extern_c(ostream) ; - oss << "void restore_stl_" ; - printNamespaces( oss, cv , "__" ) ; - printContainerClasses( oss, cv , "__" ) ; - oss << cv->getMangledTypeName() ; - oss << "_" ; - oss << (*fit)->getName() ; - oss << "(void * start_address, const char * obj_name , const char * var_name) ;" << std::endl ; + for (auto& field : cv->getFieldDescriptions()) { + if ( field->isSTL() and determinePrintAttr(cv, field) ) { + const std::string classAndFieldName = cv->getFullyQualifiedMangledTypeName("__") + "_" + field->getName(); - if ((*fit)->hasSTLClear()) { - oss << "void clear_stl_" ; - printNamespaces( oss, cv , "__" ) ; - printContainerClasses( oss, cv , "__" ) ; - oss << cv->getMangledTypeName() ; - oss << "_" ; - oss << (*fit)->getName() ; - oss << "(void * start_address) ;" << std::endl ; + auto print = [&](const std::string& name) { + ostream << "void " << name << "_" << classAndFieldName + << "(void* start_address, const char* obj_name , const char* var_name);" << std::endl ; + }; + + print("checkpoint_stl"); + print("post_checkpoint_stl"); + print("restore_checkpoint_stl"); + + if ( field->hasSTLClear() ) { + ostream << "void clear_stl_" << classAndFieldName << "(void* start_address);" << std::endl ; } } } - std::string text = oss.str(); - if (text.size()) { - print_open_extern_c(ostream) ; - ostream << text; - print_close_extern_c(ostream) ; - } + print_close_extern_c(ostream) ; } void PrintFileContents10::print_checkpoint_stl(std::ostream & ostream , FieldDescription * fdes , ClassValues * cv ) { - ostream << "void checkpoint_stl_" ; - printNamespaces( ostream, cv , "__" ) ; - printContainerClasses( ostream, cv , "__" ) ; - ostream << cv->getMangledTypeName() ; - ostream << "_" ; - ostream << fdes->getName() ; - ostream << "(void * start_address, const char * obj_name , const char * var_name) {" << std::endl ; - - ostream << " " << fdes->getTypeName() << " * stl = reinterpret_cast<" << fdes->getTypeName() << " * >(start_address) ;" << std::endl ; - ostream << " " << "checkpoint_stl(*stl , obj_name , var_name) ;" << std::endl ; - - ostream << "}" << std::endl ; + printStlFunction("checkpoint", "void* start_address, const char* obj_name , const char* var_name", "checkpoint_stl(*stl, obj_name, var_name)", ostream, *fdes, *cv); } void PrintFileContents10::print_post_checkpoint_stl(std::ostream & ostream , FieldDescription * fdes , ClassValues * cv ) { - ostream << "void post_checkpoint_stl_" ; - printNamespaces( ostream, cv , "__" ) ; - printContainerClasses( ostream, cv , "__" ) ; - ostream << cv->getMangledTypeName() ; - ostream << "_" ; - ostream << fdes->getName() ; - ostream << "(void * start_address, const char * obj_name , const char * var_name) {" << std::endl ; - - ostream << " " << fdes->getTypeName() << " * stl = reinterpret_cast<" << fdes->getTypeName() << " * >(start_address) ;" << std::endl ; - ostream << " " << "delete_stl(*stl , obj_name , var_name) ;" << std::endl ; - - ostream << "}" << std::endl ; + printStlFunction("post_checkpoint", "void* start_address, const char* obj_name , const char* var_name", "delete_stl(*stl, obj_name, var_name)", ostream, *fdes, *cv); } void PrintFileContents10::print_restore_stl(std::ostream & ostream , FieldDescription * fdes , ClassValues * cv ) { - ostream << "void restore_stl_" ; - printNamespaces( ostream, cv , "__" ) ; - printContainerClasses( ostream, cv , "__" ) ; - ostream << cv->getMangledTypeName() ; - ostream << "_" ; - ostream << fdes->getName() ; - ostream << "(void * start_address, const char * obj_name , const char * var_name) {" << std::endl ; - - ostream << " " << fdes->getTypeName() << " * stl = reinterpret_cast<" << fdes->getTypeName() << " * >(start_address) ;" << std::endl ; - ostream << " " << "restore_stl(*stl , obj_name , var_name) ;" << std::endl ; - - ostream << "}" << std::endl ; + printStlFunction("restore", "void* start_address, const char* obj_name , const char* var_name", "restore_stl(*stl, obj_name, var_name)",ostream, *fdes, *cv); } void PrintFileContents10::print_clear_stl(std::ostream & ostream , FieldDescription * fdes , ClassValues * cv ) { - ostream << "void clear_stl_" ; - printNamespaces( ostream, cv , "__" ) ; - printContainerClasses( ostream, cv , "__" ) ; - ostream << cv->getMangledTypeName() ; - ostream << "_" ; - ostream << fdes->getName() ; - ostream << "(void * start_address) {" << std::endl ; - - ostream << " " << fdes->getTypeName() << " * stl = reinterpret_cast<" << fdes->getTypeName() << " * >(start_address) ;" << std::endl ; - ostream << " " << "stl->clear() ;" << std::endl ; - - ostream << "}" << std::endl ; + printStlFunction("clear", "void* start_address", "stl->clear()",ostream, *fdes, *cv); } void PrintFileContents10::print_stl_helper(std::ostream & ostream , ClassValues * cv ) { - std::ostringstream oss; + auto& fieldDescriptions = cv->getFieldDescriptions(); - for ( ClassValues::FieldIterator fit = cv->field_begin() ; fit != cv->field_end() ; fit++ ) { - if ( (*fit)->isSTL() and determinePrintAttr(cv , *fit) ) { - print_checkpoint_stl(oss , *fit, cv) ; - print_post_checkpoint_stl(oss , *fit, cv) ; - print_restore_stl(oss , *fit, cv) ; - if ((*fit)->hasSTLClear()) { - print_clear_stl(oss , *fit, cv) ; + if (!fieldDescriptions.size()) { + return; + } + + print_open_extern_c(ostream) ; + + for (auto& field : fieldDescriptions) { + if ( field->isSTL() and determinePrintAttr(cv, field) ) { + print_checkpoint_stl(ostream, field, cv) ; + print_post_checkpoint_stl(ostream, field, cv) ; + print_restore_stl(ostream, field, cv) ; + if (field->hasSTLClear()) { + print_clear_stl(ostream, field, cv) ; } } } - std::string text = oss.str(); - if (text.size()) { - print_open_extern_c(ostream) ; - ostream << text; - print_close_extern_c(ostream) ; - } + print_close_extern_c(ostream) ; } void PrintFileContents10::printClass( std::ostream & ostream , ClassValues * cv ) { @@ -646,19 +427,10 @@ void PrintFileContents10::printClassMapHeader( std::ostream & ostream , std::str } void PrintFileContents10::printClassMap( std::ostream & ostream , ClassValues * cv ) { - ostream << " // " << cv->getFileName() << std::endl ; - ostream << " extern ATTRIBUTES attr" ; - printNamespaces( ostream, cv , "__" ) ; - printContainerClasses( ostream, cv , "__" ) ; - ostream << cv->getMangledTypeName() << "[] ;" << std::endl ; - - ostream << " class_attribute_map->add_attr(\"" ; - printNamespaces( ostream, cv , "::" ) ; - printContainerClasses( ostream, cv , "::" ) ; - ostream << cv->getMangledTypeName() << "\" , attr" ; - printNamespaces( ostream, cv , "__" ) ; - printContainerClasses( ostream, cv , "__" ) ; - ostream << cv->getMangledTypeName() << ") ;" << std::endl ; + std::string name = cv->getFullyQualifiedMangledTypeName("__"); + ostream << " // " << cv->getFileName() << std::endl + << " extern ATTRIBUTES attr" << name << "[] ;" << std::endl + << " class_attribute_map->add_attr(\"" << cv->getFullyQualifiedMangledTypeName() << "\" , attr" << name << ") ;" << std::endl ; } void PrintFileContents10::printClassMapFooter( std::ostream & ostream ) { @@ -666,26 +438,26 @@ void PrintFileContents10::printClassMapFooter( std::ostream & ostream ) { } void PrintFileContents10::printEnumMapHeader( std::ostream & ostream , std::string function_name ) { - ostream << -"void " << function_name << "() {\n" -" Trick::EnumAttributesMap * enum_attribute_map __attribute__((unused)) = Trick::EnumAttributesMap::attributes_map();\n\n" ; + ostream << "void " << function_name << "() {\n" + << " Trick::EnumAttributesMap* enum_attribute_map __attribute__((unused)) = Trick::EnumAttributesMap::attributes_map();\n\n" ; } void PrintFileContents10::printEnumMap( std::ostream & ostream , EnumValues * ev ) { - ostream << " extern ENUM_ATTR enum" ; - printNamespaces( ostream, ev , "__" ) ; - printContainerClasses( ostream, ev , "__" ) ; - ostream << ev->getName() << "[] ;" << std::endl ; - - ostream << " enum_attribute_map->add_attr(\"" ; - printNamespaces( ostream, ev , "::" ) ; - printContainerClasses( ostream, ev , "::" ) ; - ostream << ev->getName() << "\" , enum" ; - printNamespaces( ostream, ev , "__" ) ; - printContainerClasses( ostream, ev , "__" ) ; - ostream << ev->getName() << ") ;" << std::endl ; + std::string name = ev->getFullyQualifiedTypeName("__"); + ostream << " extern ENUM_ATTR enum" << name << "[];" << std::endl + << " enum_attribute_map->add_attr(\"" << ev->getFullyQualifiedTypeName() << "\", enum" << name << ");" << std::endl ; } void PrintFileContents10::printEnumMapFooter( std::ostream & ostream ) { ostream << "}" << std::endl << std::endl ; } + +void PrintFileContents10::printStlFunction(const std::string& name, const std::string& parameters, const std::string& call, std::ostream& ostream, FieldDescription& fieldDescription, ClassValues& classValues) { + const std::string typeName = fieldDescription.getTypeName(); + const std::string functionName = name + "_stl"; + ostream << "void " << functionName << "_" << classValues.getFullyQualifiedMangledTypeName("__") << "_" << fieldDescription.getName() + << "(" << parameters << ") {" << std::endl + << " " << typeName << "* stl = reinterpret_cast<" << typeName << "*>(start_address);" << std::endl + << " " << call << ";" << std::endl + << "}" << std::endl ; +} diff --git a/trick_source/codegen/Interface_Code_Gen/PrintFileContents10.hh b/trick_source/codegen/Interface_Code_Gen/PrintFileContents10.hh index 09fc405f..96df008c 100644 --- a/trick_source/codegen/Interface_Code_Gen/PrintFileContents10.hh +++ b/trick_source/codegen/Interface_Code_Gen/PrintFileContents10.hh @@ -103,6 +103,8 @@ class PrintFileContents10 : public PrintFileContentsBase { /** Prints stl clear function */ void print_clear_stl(std::ostream & outfile , FieldDescription * fdes , ClassValues * in_class) ; + + void printStlFunction(const std::string& name, const std::string& parameters, const std::string& call, std::ostream& ostream, FieldDescription& fieldDescription, ClassValues& classValues); } ; #endif diff --git a/trick_source/codegen/Interface_Code_Gen/PrintFileContents13.cpp b/trick_source/codegen/Interface_Code_Gen/PrintFileContents13.cpp deleted file mode 100644 index e736f148..00000000 --- a/trick_source/codegen/Interface_Code_Gen/PrintFileContents13.cpp +++ /dev/null @@ -1,209 +0,0 @@ - -#include -#include -#include - -#include "PrintFileContents13.hh" -#include "FieldDescription.hh" -#include "ClassValues.hh" -#include "EnumValues.hh" - -PrintFileContents13::PrintFileContents13() {} - -/** - */ -void PrintFileContents13::printIOHeader(std::ostream & ostream , std::string header_file_name) { - if ( ! header_file_name.compare("S_source.hh") ) { - header_file_name = "../S_source.hh" ; - } - ostream << "\n" - "/*\n" - " * This file was automatically generated by the ICG based on the file:\n" - " * " << header_file_name << "\n" - " */\n" - "#include \"" << header_file_name << "\"\n" - "#include \n" // for offsetof() - "#include \"TypeDictionary.hh\"\n" - "#include \"EnumDataType.hh\"\n" - "#include \"CompositeDataType.hh\"\n" - "\n"; -} - -std::string PrintFileContents13::enumeration_identifier( EnumValues * e ) { - - std::ostringstream oss; - EnumValues::NamespaceIterator nsi ; - for ( nsi = e->namespace_begin() ; nsi != e->namespace_end() ; nsi++ ) { - oss << *nsi << "__" ; - } - EnumValues::ContainerClassIterator ci ; - for ( ci = e->container_class_begin() ; ci != e->container_class_end() ; ci++ ) { - oss << *ci << "__" ; - } - oss << e->getName(); - return oss.str(); -} - -/** Prints enumeration attributes */ -void PrintFileContents13::print_enum_attr(std::ostream & ostream , EnumValues * e ) { - EnumValues::NameValueIterator nvit ; - - ostream << "class type_" << enumeration_identifier(e) << " {\n" - << "public:\n" - << " type_" << enumeration_identifier(e) << "() {\n" - << " TypeDictionary * type_dict = TypeDictionary::getInstance();\n" - << " EnumDataType * dataType = new EnumDataType( sizeof(enum " << e->getName() << "));\n" - << " try {\n" - ; - - for ( nvit = e->begin() ; nvit != e->end() ; nvit++ ) { - ostream << " dataType->addEnumerator(\"" << (*nvit).first << "\" , " << (*nvit).second << ");\n" ; - } - - ostream << " typeDictionary->addTypeDefinition( \"" << e->getName() << "\", dataType );\n" - << " } catch ( std::logic_error e ) {\n" - << " std::cerr << e.what();\n" - << " }\n" - << " }\n" - << "};\n" - << "type_" << enumeration_identifier(e) << " __" << enumeration_identifier(e) << ";\n" - << "\n" - ; -} - -/** */ -std::string PrintFileContents13::class_identifier( ClassValues * c ) { - - std::ostringstream oss; - ClassValues::NamespaceIterator nsi ; - for ( nsi = c->namespace_begin() ; nsi != c->namespace_end() ; nsi++ ) { - oss << *nsi << "__" ; - } - ClassValues::ContainerClassIterator ci ; - for ( ci = c->container_class_begin() ; ci != c->container_class_end() ; ci++ ) { - oss << *ci << "__" ; - } - oss << c->getName(); - return oss.str(); -} - -std::string PrintFileContents13::bit_field_getter_name(ClassValues * c, FieldDescription * f) { - std::ostringstream oss; - oss << " get_" << class_identifier(c) << "__" << f->getName(); - return oss.str(); -} - -std::string PrintFileContents13::bit_field_setter_name(ClassValues * c, FieldDescription * f) { - std::ostringstream oss; - oss << " set_" << class_identifier(c) << "__" << f->getName(); - return oss.str(); -} - -/** Prints attributes for a field */ -void PrintFileContents13::print_field_attr(std::ostream & ostream , ClassValues * c, FieldDescription * fv ) { - - if (fv->isBitField()) { - ostream << " " ; - ostream << "dataType->addBitFieldMember(\"" << fv->getName() << "\", " - << bit_field_getter_name(c,fv) << ", " - << bit_field_setter_name(c,fv) << ");\n" - ; - } else { - int ndims = fv->getNumDims(); - - // For arrays and/or pointers, generate a dimension-size array. - if (ndims) { - ostream << " " ; - ostream << "unsigned int dim_" << fv->getName() << "[] = {"; - for ( unsigned int ii = 0 ; ii < ndims ; ii++ ) { - if (ii) { - ostream << ","; - } - ostream << fv->getArrayDim(ii) ; - } - ostream << "};\n"; - } - - ostream << " " ; - if (fv->isStatic()) { - ostream << "dataType->addStaticMember( \""; - ostream << fv->getName() - << "\", &" << c->getName() << "::" << fv->getName() << ", \"" << fv->getTypeName() << "\", "; - } else { - ostream << "dataType->addRegularMember( \""; - ostream << fv->getName() << "\", offsetof(" << c->getName() << ", " << fv->getName() << "), \"" << fv->getTypeName() << "\", "; - } - - - if (ndims) { - ostream << ndims << ", " << "dim_" << fv->getName() ; - } else { - ostream << "0, NULL" ; - } - ostream <<");\n" ; - - } -} - -/** Prints class attributes */ -void PrintFileContents13::print_class_attr(std::ostream & ostream , ClassValues * c ) { - - ClassValues::FieldIterator fit ; - - // Create setters and getters for any bitfields we may have. - for ( fit = c->field_begin() ; fit != c->field_end() ; fit++ ) { - - FieldDescription * f = *fit; - - if ( f->isBitField()) { - - // Create bitfield getter. - ostream << "static " << f->getTypeName() << " " << bit_field_getter_name(c,f) << "()(void* addr) {\n" - << " return ((" << class_identifier(c) << "*)addr)->" << f->getName() << ";\n" - << "}\n" - ; - - // Create bitfield setter. - ostream << "static void " << bit_field_setter_name(c,f) << "(void* addr, "<< f->getTypeName() << " v) {\n" - << " ((" << class_identifier(c) << "*)addr)->" << f->getName() << " = v;\n" - << "}\n" - ; - } - } - - ostream << "class type_" << class_identifier(c) << " {\n" - << "public:\n" - << " type_" << class_identifier(c) << "() {\n" - << " TypeDictionary * type_dict = TypeDictionary::getInstance();\n" - << " CompositeDataType * dataType = new CompositeDataType( sizeof( " << c->getName() << "));\n" - << " try {\n" - ; - - for ( fit = c->field_begin() ; fit != c->field_end() ; fit++ ) { - FieldDescription * field = *fit; - if ( determinePrintAttr(c , field) ) { - print_field_attr( ostream, c, field) ; - } - } - - ostream << " typeDictionary->addTypeDefinition( \"" << c->getName() << "\", dataType );\n" - << " } catch ( std::logic_error e ) {\n" - << " std::cerr << e.what();\n" - << " }\n" - << " }\n" - << "};\n" - << "type_" << class_identifier(c) << " __" << class_identifier(c) << ";\n\n" - ; -} - - -/** Prints io_src for a class */ -void PrintFileContents13::printClass( std::ostream & ostream , ClassValues * cv ) { - print_class_attr(ostream, cv) ; -} - -/** Prints io_src for an enum */ -void PrintFileContents13::printEnum( std::ostream & ostream , EnumValues * ev ) { - print_enum_attr(ostream, ev) ; -} - diff --git a/trick_source/codegen/Interface_Code_Gen/PrintFileContents13.hh b/trick_source/codegen/Interface_Code_Gen/PrintFileContents13.hh deleted file mode 100644 index 3ba22d3c..00000000 --- a/trick_source/codegen/Interface_Code_Gen/PrintFileContents13.hh +++ /dev/null @@ -1,58 +0,0 @@ - -#ifndef PRINTFILECONTENTS13_HH -#define PRINTFILECONTENTS13_HH - -#include -#include -#include -#include -#include - -#include "PrintFileContentsBase.hh" - -/** - - This class prints Trick 13 style io_src code. The print command is called when - the container class has added all of the classes and enumerations to this class - to print. - - @author somebody - - @date July 2012 - - */ - -class PrintFileContents13 : public PrintFileContentsBase { - - public: - - PrintFileContents13() ; - - /** Prints the io_src header information */ - virtual void printIOHeader(std::ostream & ostream , std::string header_file_name) ; - - std::string enumeration_identifier( EnumValues * e ); - /** Prints enumeration attributes */ - void print_enum_attr(std::ostream & ostream , EnumValues * in_enum) ; - - - std::string bit_field_getter_name(ClassValues * c, FieldDescription * f); - std::string bit_field_setter_name(ClassValues * c, FieldDescription * f); - - std::string class_identifier( ClassValues * c ) ; - - /** Prints attributes for a field */ - void print_field_attr(std::ostream & ostream , ClassValues * c, FieldDescription * fv ) ; - - /** Prints class attributes */ - void print_class_attr(std::ostream & ostream , ClassValues * in_class) ; - - /** Prints attributes for a class */ - virtual void printClass(std::ostream & ostream , ClassValues * cv) ; - - /** Prints attributes for an enum */ - virtual void printEnum(std::ostream & ostream , EnumValues * ev) ; - -} ; - -#endif diff --git a/trick_source/codegen/Interface_Code_Gen/PrintFileContentsBase.cpp b/trick_source/codegen/Interface_Code_Gen/PrintFileContentsBase.cpp index 56004f3b..0404b66e 100644 --- a/trick_source/codegen/Interface_Code_Gen/PrintFileContentsBase.cpp +++ b/trick_source/codegen/Interface_Code_Gen/PrintFileContentsBase.cpp @@ -26,41 +26,22 @@ void PrintFileContentsBase::printEnumMap(std::ostream & ostream, EnumValues * ev void PrintFileContentsBase::printEnumMapFooter(std::ostream & ostream) {} void PrintFileContentsBase::print_units_map(std::ostream & ostream, ClassValues * cv ) { - ClassValues::FieldIterator fit ; - unsigned int ii ; + const std::string name = cv->getFullyQualifiedMangledTypeName("__"); + ostream << "struct UnitsMap" << name << " {\n" ; - ostream << "struct UnitsMap" ; - printNamespaces( ostream, cv , "__" ) ; - printContainerClasses( ostream, cv , "__" ) ; - ostream << cv->getMangledTypeName() ; - ostream << " {\n" ; - - std::ostringstream text; - - for ( fit = cv->field_begin() ; fit != cv->field_end() ; fit++ ) { - if ( determinePrintAttr(cv , *fit) and (*fit)->getUnits().compare("1")) { - FieldDescription * fdes = *fit ; - text << " units_map_ptr->add_param(\"" ; - printContainerClasses( text, cv , "__" ) ; - text << cv->getName() << "_" << fdes->getName() << "\", \"" << fdes->getUnits() << "\") ;\n" ; + auto fields = getPrintableFields(*cv); + if (fields.size()) { + ostream << " UnitsMap" << name << "() {\n" + << " Trick::UnitsMap* units_map_ptr = Trick::UnitsMap::units_map();\n" ; + for (auto& field : fields) { + ostream << " units_map_ptr->add_param(\"" ; + cv->printContainerClasses(ostream, "__"); + ostream << cv->getName() << "_" << field->getName() << "\", \"" << field->getUnits() << "\") ;\n" ; } - } - - if (text.str().size()) { - ostream << " UnitsMap" ; - printNamespaces( ostream, cv , "__" ) ; - printContainerClasses( ostream, cv , "__" ) ; - ostream << cv->getMangledTypeName() ; - ostream << "() {\n" ; - ostream << " Trick::UnitsMap * units_map_ptr = Trick::UnitsMap::units_map() ;\n" ; - ostream << text.str() ; ostream << " }\n" ; } - ostream << "} um" ; - printNamespaces( ostream, cv , "__" ) ; - printContainerClasses( ostream, cv , "__" ) ; - ostream << cv->getMangledTypeName() ; - ostream << " ;\n" ; + + ostream << "} um" << name << ";\n" ; } /* Utility routines for printing */ @@ -103,31 +84,13 @@ bool PrintFileContentsBase::determinePrintAttr( ClassValues * c , FieldDescripti return false ; } -/** Prints namespace containers of a class delimited by delim */ -void PrintFileContentsBase::printNamespaces( std::ostream & ostream , ConstructValues * c , const char * delim ) { - for ( ClassValues::NamespaceIterator nsi = c->namespace_begin() ; nsi != c->namespace_end() ; ++nsi ) { - ostream << *nsi << delim ; - } -} - -/** Prints namespace open block */ -void PrintFileContentsBase::printOpenNamespaceBlocks( std::ostream & ostream , ClassValues * c ) { - for ( ClassValues::NamespaceIterator nsi = c->namespace_begin() ; nsi != c->namespace_end() ; ++nsi ) { - ostream << "namespace " << *nsi << " {\n" ; - } -} - -/** Prints namespace close block */ -void PrintFileContentsBase::printCloseNamespaceBlocks( std::ostream & ostream , ClassValues * c ) { - for ( ClassValues::NamespaceIterator nsi = c->namespace_begin() ; nsi != c->namespace_end() ; ++nsi ) { - ostream << "}\n" ; - } -} - -/** Prints class containers of a class delimited by delim */ -void PrintFileContentsBase::printContainerClasses( std::ostream & ostream , ConstructValues * c , const char * delim ) { - for ( ClassValues::ContainerClassIterator ci = c->container_class_begin() ; ci != c->container_class_end() ; ci++ ) { - ostream << *ci << delim ; +std::vector PrintFileContentsBase::getPrintableFields(ClassValues& classValues) { + std::vector results; + for (auto& field : classValues.getFieldDescriptions()) { + if (determinePrintAttr(&classValues, field) and field->getUnits().compare("1")) { + results.push_back(field); + } } + return results; } diff --git a/trick_source/codegen/Interface_Code_Gen/PrintFileContentsBase.hh b/trick_source/codegen/Interface_Code_Gen/PrintFileContentsBase.hh index ee1538c1..b985f8d3 100644 --- a/trick_source/codegen/Interface_Code_Gen/PrintFileContentsBase.hh +++ b/trick_source/codegen/Interface_Code_Gen/PrintFileContentsBase.hh @@ -55,21 +55,8 @@ class PrintFileContentsBase { /* internal function determines if a particular field is printable */ bool determinePrintAttr(ClassValues * c , FieldDescription *fdes) ; - /** Prints namespace containers of a class delimited by delim */ - void printNamespaces(std::ostream & ostream, ConstructValues * c , const char * delim ) ; - - /** Prints namespace open block */ - void printOpenNamespaceBlocks( std::ostream & ostream, ClassValues * c ) ; - - /** Prints namespace close block */ - void printCloseNamespaceBlocks( std::ostream & ostream, ClassValues * c ) ; - - /* Note: There are no open/close namespace blocks for enums. The - enum attributes do not have anything that needs to be enclosed in - namespaces */ - - /** Prints class containers of a class delimited by delim */ - void printContainerClasses(std::ostream & ostream, ConstructValues * c , const char * delim ) ; + /* gets a vector of fields that can be printed */ + std::vector getPrintableFields(ClassValues& classValues); } ; diff --git a/trick_source/codegen/Interface_Code_Gen/TypedefVisitor.cpp b/trick_source/codegen/Interface_Code_Gen/TypedefVisitor.cpp index c37a430b..656b9e52 100644 --- a/trick_source/codegen/Interface_Code_Gen/TypedefVisitor.cpp +++ b/trick_source/codegen/Interface_Code_Gen/TypedefVisitor.cpp @@ -107,20 +107,19 @@ bool TypedefVisitor::VisitTemplateSpecializationType(clang::TemplateSpecializati CXXRecordVisitor cvis(ci, cs, hsd, pa, true) ; cvis.TraverseCXXRecordDecl(clang::cast(td)) ; cval = cvis.get_class_data() ; - // Check to see if this typedef is to a STL. If it is we don't want it. - if ((cval->namespace_begin() == cval->namespace_end()) or - (cval->namespace_begin() != cval->namespace_end() and cval->namespace_begin()->compare("std")) ) { + + if (cval->isInStandardNamespace()) { + if ( debug_level >= 4 ) { + std::cout << "\nTypedefVisitor VisitTemplateSpecializationType not adding class" << std::endl ; + } + cval = NULL ; + } else { cval->setName(typedef_name) ; cval->clearNamespacesAndClasses() ; cval->getNamespacesAndClasses(typedef_decl_context) ; // Set the filename of the typedeffed target to the file where the typedef is. cval->setFileName(getFileName( ci , typedef_location.getBegin() , hsd )) ; pa.printClass(cval) ; - } else { - if ( debug_level >=4 ) { - std::cout << "\nTypedefVisitor VisitTemplateSpecializationType not adding class" << std::endl ; - } - cval = NULL ; } } diff --git a/trick_source/codegen/Interface_Code_Gen/VariableVisitor.cpp b/trick_source/codegen/Interface_Code_Gen/VariableVisitor.cpp index dd58f44c..bbb342f0 100644 --- a/trick_source/codegen/Interface_Code_Gen/VariableVisitor.cpp +++ b/trick_source/codegen/Interface_Code_Gen/VariableVisitor.cpp @@ -60,10 +60,13 @@ bool VariableVisitor::VisitTemplateSpecializationType(clang::TemplateSpecializat CXXRecordVisitor cvis(ci, cs, hsd, pa, true) ; cval = cvis.get_class_data() ; cvis.TraverseCXXRecordDecl(clang::cast(td)) ; - // Check to see if this typedef is to a STL. If it is we don't want it. - if ((cval->namespace_begin() == cval->namespace_end()) or - (cval->namespace_begin() != cval->namespace_end() and cval->namespace_begin()->compare("std")) ) { + if (cval->isInStandardNamespace()) { + if ( debug_level >=4 ) { + std::cout << "\nTypedefVisitor VisitTemplateSpecializationType not adding class" << std::endl ; + } + cval = NULL ; + } else { std::string mangled_name = tst->desugar().getAsString() ; size_t pos ; if ((pos = mangled_name.find("class ")) != std::string::npos ) { @@ -81,11 +84,6 @@ bool VariableVisitor::VisitTemplateSpecializationType(clang::TemplateSpecializat cval->setMangledTypeName(mangled_name) ; pa.printClass(cval) ; - } else { - if ( debug_level >=4 ) { - std::cout << "\nTypedefVisitor VisitTemplateSpecializationType not adding class" << std::endl ; - } - cval = NULL ; } } diff --git a/trick_source/codegen/Interface_Code_Gen/main.cpp b/trick_source/codegen/Interface_Code_Gen/main.cpp index 49c191ae..d9b541ac 100644 --- a/trick_source/codegen/Interface_Code_Gen/main.cpp +++ b/trick_source/codegen/Interface_Code_Gen/main.cpp @@ -22,7 +22,6 @@ #include "CommentSaver.hh" #include "TranslationUnitVisitor.hh" #include "PrintAttributes.hh" -#include "PrintAttributesFactory.hh" #include "Utilities.hh" /* Command line arguments. These work better as globals, as suggested in llvm/CommandLine documentation */ @@ -65,9 +64,9 @@ int main(int argc, char * argv[]) { */ llvm::cl::ParseCommandLineOptions(argc, argv); - if (!validAttributesVersion(attr_version)) { + /*if (!validAttributesVersion(attr_version)) { return -1; - } + }*/ if (input_file_names.empty()) { std::cerr << "No header file specified" << std::endl;