ROSE 0.11.145.147
Protected Member Functions | Protected Attributes | List of all members
Rosebud::CxxGenerator Class Reference

Description

Base class for generators that produce C++ code.

Definition at line 8 of file CxxGenerator.h.

#include <Rosebud/CxxGenerator.h>

Inheritance diagram for Rosebud::CxxGenerator:
Inheritance graph
[legend]
Collaboration diagram for Rosebud::CxxGenerator:
Collaboration graph
[legend]

Protected Member Functions

virtual std::string machineGenerated (char commentType='/')
 Return a title comment that says the file is machine generated.
 
virtual std::string ctorInitializerExpression (const Ast::PropertyPtr &, const std::string &expr)
 Expression for initializing a property in a constructor, or empty.
 
virtual std::string resetStatement (const Ast::PropertyPtr &)
 Statement to initialize a property.
 
virtual std::string propertyDataMemberType (const Ast::PropertyPtr &)
 Type for the data member for a property.
 
virtual std::string propertyAccessorReturnType (const Ast::PropertyPtr &)
 Type for the return value for a property accessor.
 
virtual std::string propertyMutatorArgumentType (const Ast::PropertyPtr &)
 Type for the argument for a property mutator.
 
virtual std::string initialValue (const Ast::PropertyPtr &)
 Initial value expression for the property.
 
virtual std::string valueType (const Ast::PropertyPtr &)
 Type of value for initializing a property.
 
virtual void genDestructor (std::ostream &header, std::ostream &impl, const Ast::ClassPtr &)
 Emit code for the class destructor.
 
virtual void genDefaultConstructor (std::ostream &header, std::ostream &impl, const Ast::ClassPtr &, Access)
 Emit code for the class default constructor.
 
virtual bool genArgsConstructor (std::ostream &header, std::ostream &impl, const Ast::ClassPtr &, const Hierarchy &, Access)
 Emit code for the constructor with ctor_args property arguments.
 
virtual void genConstructorBody (std::ostream &, const Ast::ClassPtr &)
 Emit code for the constructor body.
 
virtual void genInitProperties (std::ostream &header, std::ostream &impl, const Ast::ClassPtr &)
 Emit code that initializes all local properties.
 
virtual std::string removePointer (const std::string &) const
 Given a type name, remove the outer pointer if present.
 
- Protected Member Functions inherited from Rosebud::Generator
std::string generatedByRosebud (const std::string &prefix)
 Return a multi-line comment that this file was generated by Rosebud.
 

Protected Attributes

size_t outputWidth = 130
 

Additional Inherited Members

- Public Types inherited from Rosebud::Generator
using Ptr = GeneratorPtr
 
- Public Member Functions inherited from Rosebud::Generator
virtual void adjustParser (Sawyer::CommandLine::Parser &)
 Add command-line switches and documentation to a parser.
 
virtual std::string name () const =0
 Property: Every generator should have a unique name.
 
virtual std::string purpose () const =0
 Property: Single-line description for documentation.
 
virtual void generate (const Ast::ProjectPtr &)=0
 Generate code.
 
virtual std::string propertyDataMemberName (const Ast::PropertyPtr &) const
 Data member name for a property.
 
virtual std::vector< std::string > propertyAccessorNames (const Ast::PropertyPtr &) const
 Accessor names for a property.
 
virtual std::vector< std::string > propertyMutatorNames (const Ast::PropertyPtr &) const
 Mutator names for a property.
 
void commandLine (const std::vector< std::string > &)
 Property: Entire Rosebud command line.
 
const std::vector< std::string > & commandLine () const
 Property: Entire Rosebud command line.
 
- Static Public Member Functions inherited from Rosebud::Generator
static void registerGenerator (const Ptr &)
 Register a backend for use later.
 
static const std::vector< Ptr > & registeredGenerators ()
 Return all registered backends.
 
static Ptr lookup (const std::string &)
 Return the registered generator with the specified name.
 
static void addAllToParser (Sawyer::CommandLine::Parser &)
 Add all known generator switches to parser.
 

Member Function Documentation

◆ ctorInitializerExpression()

virtual std::string Rosebud::CxxGenerator::ctorInitializerExpression ( const Ast::PropertyPtr ,
const std::string &  expr 
)
protectedvirtual

Expression for initializing a property in a constructor, or empty.

For most properties, this is simply the initialization expression parsed from the input. However, some properties need extra arguments, etc. that can be returned by this funciton. For instance, a TreeEdge property is initialized in a special way:

// Class with property definition
class Node: public TreeNode {
[[Rosebud::property]]
TreeEdge<Foo> foo = Foo::instance();
[[Rosebud::property]]
int bar = 42;
};
// Generated default constructor
Node::Node()
: foo_(*this, Foo::instance()), bar_(42) {}

So the initializer expression for the "foo" property is overridden to add the "*this" argument, but for "bar" it is not overridden.

The expr argument is the default initializer expression such as the string from the right hand side of the "=" in the property definition, or some other expression.

Reimplemented in Rosebud::SawyerGenerator.

◆ resetStatement()

virtual std::string Rosebud::CxxGenerator::resetStatement ( const Ast::PropertyPtr )
protectedvirtual

Statement to initialize a property.

The return string is a statement that initializes the property data member if the data member needs to be initialized other than with a default constructor. This is used in the generated "initializeProperties" member function which is called by user-defined constructors prior to the user assigning a different value. The returned string is either empty or includes the terminating semicolon.

◆ propertyDataMemberType()

virtual std::string Rosebud::CxxGenerator::propertyDataMemberType ( const Ast::PropertyPtr )
protectedvirtual

Type for the data member for a property.

The base implementation simply returns the property type from the input.

Reimplemented in Rosebud::SawyerGenerator.

◆ propertyAccessorReturnType()

virtual std::string Rosebud::CxxGenerator::propertyAccessorReturnType ( const Ast::PropertyPtr )
protectedvirtual

Type for the return value for a property accessor.

This is usually the same as the property data member type but without any 'mutable'. Do not add reference or 'const'.

◆ propertyMutatorArgumentType()

virtual std::string Rosebud::CxxGenerator::propertyMutatorArgumentType ( const Ast::PropertyPtr )
protectedvirtual

Type for the argument for a property mutator.

If the property data member type is T, then this is usually const T&.

Reimplemented in Rosebud::SawyerGenerator.

◆ initialValue()

virtual std::string Rosebud::CxxGenerator::initialValue ( const Ast::PropertyPtr )
protectedvirtual

Initial value expression for the property.

The base implementation returns whatever expression was to the right of the "=" in the property definition.

◆ valueType()

virtual std::string Rosebud::CxxGenerator::valueType ( const Ast::PropertyPtr )
protectedvirtual

Type of value for initializing a property.

This is the type used by mutator arguments, but without the const reference part. The base implementation simply returns the type from the property definition after removing volatile and mutable, but sometimes we need something else. For instance, a property of type TreeEdge<T> is never initialized from another tree edge, but rather from a std::shared_ptr<T>, as follows:

// Class with property definition
class Node: public TreeNode {
[[Rosebud::property]]
TreeEdge<Foo> foo;
};
// Generated mutator member function declaration
void foo(std::shared_ptr<Foo> const&);

Reimplemented in Rosebud::SawyerGenerator.

◆ genDefaultConstructor()

virtual void Rosebud::CxxGenerator::genDefaultConstructor ( std::ostream &  header,
std::ostream &  impl,
const Ast::ClassPtr ,
Access   
)
protectedvirtual

Emit code for the class default constructor.

Reimplemented in Rosebud::SawyerGenerator.

◆ genArgsConstructor()

virtual bool Rosebud::CxxGenerator::genArgsConstructor ( std::ostream &  header,
std::ostream &  impl,
const Ast::ClassPtr ,
const Hierarchy ,
Access   
)
protectedvirtual

Emit code for the constructor with ctor_args property arguments.

Returns true if it generated a constructor.

Reimplemented in Rosebud::SawyerGenerator.

◆ genConstructorBody()

virtual void Rosebud::CxxGenerator::genConstructorBody ( std::ostream &  ,
const Ast::ClassPtr  
)
protectedvirtual

Emit code for the constructor body.

Reimplemented in Rosebud::SawyerGenerator.

◆ removePointer()

virtual std::string Rosebud::CxxGenerator::removePointer ( const std::string &  ) const
protectedvirtual

Given a type name, remove the outer pointer if present.

  • Given "Thing*" return "Thing"
  • Given "ThingPtr" return "Thing"
  • Given "ThingConstPtr" return "Thing"
  • Given "Thing::Ptr" return "Thing"
  • Given "Thing::ConstPtr" return "Thing"
  • Given "Thing" (not a pointer) return "Thing"

Member Data Documentation

◆ outputWidth

size_t Rosebud::CxxGenerator::outputWidth = 130
protected

Definition at line 10 of file CxxGenerator.h.


The documentation for this class was generated from the following file: