| 1 | //
 | 
|---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
 | 
|---|
| 3 | //
 | 
|---|
| 4 | // The contents of this file are covered under the licence agreement in the
 | 
|---|
| 5 | // file "LICENCE" distributed with Cforall.
 | 
|---|
| 6 | //
 | 
|---|
| 7 | // Initializer.h --
 | 
|---|
| 8 | //
 | 
|---|
| 9 | // Author           : Richard C. Bilson
 | 
|---|
| 10 | // Created On       : Mon May 18 07:44:20 2015
 | 
|---|
| 11 | // Last Modified By : Peter A. Buhr
 | 
|---|
| 12 | // Last Modified On : Thu Mar 23 16:12:42 2017
 | 
|---|
| 13 | // Update Count     : 20
 | 
|---|
| 14 | //
 | 
|---|
| 15 | 
 | 
|---|
| 16 | #ifndef INITIALIZER_H
 | 
|---|
| 17 | #define INITIALIZER_H
 | 
|---|
| 18 | 
 | 
|---|
| 19 | #include <cassert>
 | 
|---|
| 20 | 
 | 
|---|
| 21 | #include "BaseSyntaxNode.h"
 | 
|---|
| 22 | #include "Mutator.h"
 | 
|---|
| 23 | #include "SynTree.h"
 | 
|---|
| 24 | #include "Type.h"
 | 
|---|
| 25 | #include "Visitor.h"
 | 
|---|
| 26 | 
 | 
|---|
| 27 | // Designation: list of designator (NameExpr, VariableExpr, and ConstantExpr) expressions that specify an object being initialized.
 | 
|---|
| 28 | class Designation : public BaseSyntaxNode {
 | 
|---|
| 29 | public:
 | 
|---|
| 30 |         Designation( const std::list< Expression * > & designators );
 | 
|---|
| 31 |         Designation( const Designation & other );
 | 
|---|
| 32 |         virtual ~Designation();
 | 
|---|
| 33 | 
 | 
|---|
| 34 |         std::list< Expression * > & get_designators() { return designators; }
 | 
|---|
| 35 | 
 | 
|---|
| 36 |         virtual Designation * clone() const { return new Designation( *this ); };
 | 
|---|
| 37 |         virtual void accept( Visitor &v ) { v.visit( this ); }
 | 
|---|
| 38 |         virtual Designation * acceptMutator( Mutator &m ) { return m.mutate( this ); }
 | 
|---|
| 39 |         virtual void print( std::ostream &os, int indent = 0 ) const;
 | 
|---|
| 40 | private:
 | 
|---|
| 41 |         std::list< Expression * > designators;
 | 
|---|
| 42 | };
 | 
|---|
| 43 | 
 | 
|---|
| 44 | const std::list<Designation *> noDesignators;
 | 
|---|
| 45 | 
 | 
|---|
| 46 | // Initializer: base class for object initializers (provide default values)
 | 
|---|
| 47 | class Initializer : public BaseSyntaxNode {
 | 
|---|
| 48 |   public:
 | 
|---|
| 49 |         Initializer( bool maybeConstructed );
 | 
|---|
| 50 |         Initializer( const Initializer & other );
 | 
|---|
| 51 |         virtual ~Initializer();
 | 
|---|
| 52 | 
 | 
|---|
| 53 |         bool get_maybeConstructed() { return maybeConstructed; }
 | 
|---|
| 54 | 
 | 
|---|
| 55 |         virtual Initializer *clone() const = 0;
 | 
|---|
| 56 |         virtual void accept( Visitor &v ) = 0;
 | 
|---|
| 57 |         virtual Initializer *acceptMutator( Mutator &m ) = 0;
 | 
|---|
| 58 |         virtual void print( std::ostream &os, int indent = 0 ) const = 0;
 | 
|---|
| 59 |   private:
 | 
|---|
| 60 |         bool maybeConstructed;
 | 
|---|
| 61 | };
 | 
|---|
| 62 | 
 | 
|---|
| 63 | // SingleInit represents an initializer for a common object (e.g., int x = 4)
 | 
|---|
| 64 | class SingleInit : public Initializer {
 | 
|---|
| 65 |   public:
 | 
|---|
| 66 |         SingleInit( Expression *value, bool maybeConstructed = false );
 | 
|---|
| 67 |         SingleInit( const SingleInit &other );
 | 
|---|
| 68 |         virtual ~SingleInit();
 | 
|---|
| 69 | 
 | 
|---|
| 70 |         Expression *get_value() { return value; }
 | 
|---|
| 71 |         void set_value( Expression *newValue ) { value = newValue; }
 | 
|---|
| 72 | 
 | 
|---|
| 73 |         virtual SingleInit *clone() const { return new SingleInit( *this); }
 | 
|---|
| 74 |         virtual void accept( Visitor &v ) { v.visit( this ); }
 | 
|---|
| 75 |         virtual Initializer *acceptMutator( Mutator &m ) { return m.mutate( this ); }
 | 
|---|
| 76 |         virtual void print( std::ostream &os, int indent = 0 ) const;
 | 
|---|
| 77 |   private:
 | 
|---|
| 78 |         //Constant *value;
 | 
|---|
| 79 |         Expression *value;      // has to be a compile-time constant
 | 
|---|
| 80 | };
 | 
|---|
| 81 | 
 | 
|---|
| 82 | // ListInit represents an initializer that is composed recursively of a list of initializers; this is used to initialize
 | 
|---|
| 83 | // an array or aggregate
 | 
|---|
| 84 | class ListInit : public Initializer {
 | 
|---|
| 85 |   public:
 | 
|---|
| 86 |         ListInit( const std::list<Initializer*> &initializers,
 | 
|---|
| 87 |                           const std::list<Designation *> &designators = {}, bool maybeConstructed = false );
 | 
|---|
| 88 |         ListInit( const ListInit & other );
 | 
|---|
| 89 |         virtual ~ListInit();
 | 
|---|
| 90 | 
 | 
|---|
| 91 |         std::list<Designation *> & get_designations() { return designations; }
 | 
|---|
| 92 |         std::list<Initializer *> & get_initializers() { return initializers; }
 | 
|---|
| 93 | 
 | 
|---|
| 94 |         typedef std::list<Initializer*>::iterator iterator;
 | 
|---|
| 95 |         typedef std::list<Initializer*>::const_iterator const_iterator;
 | 
|---|
| 96 |         iterator begin() { return initializers.begin(); }
 | 
|---|
| 97 |         iterator end() { return initializers.end(); }
 | 
|---|
| 98 |         const_iterator begin() const { return initializers.begin(); }
 | 
|---|
| 99 |         const_iterator end() const { return initializers.end(); }
 | 
|---|
| 100 | 
 | 
|---|
| 101 |         virtual ListInit *clone() const { return new ListInit( *this ); }
 | 
|---|
| 102 |         virtual void accept( Visitor &v ) { v.visit( this ); }
 | 
|---|
| 103 |         virtual Initializer *acceptMutator( Mutator &m ) { return m.mutate( this ); }
 | 
|---|
| 104 |         virtual void print( std::ostream &os, int indent = 0 ) const;
 | 
|---|
| 105 |   private:
 | 
|---|
| 106 |         std::list<Initializer *> initializers;  // order *is* important
 | 
|---|
| 107 |         std::list<Designation *> designations;  // order/length is consistent with initializers
 | 
|---|
| 108 | };
 | 
|---|
| 109 | 
 | 
|---|
| 110 | // ConstructorInit represents an initializer that is either a constructor expression or
 | 
|---|
| 111 | // a C-style initializer.
 | 
|---|
| 112 | // It should not be necessary to create ConstructorInit nodes manually. Instead, set maybeConstructed
 | 
|---|
| 113 | // to true on SingleInit or ListInit constructors if object should be constructed.
 | 
|---|
| 114 | class ConstructorInit : public Initializer {
 | 
|---|
| 115 |   public:
 | 
|---|
| 116 |         ConstructorInit( Statement * ctor, Statement * dtor, Initializer * init );
 | 
|---|
| 117 |         ConstructorInit( const ConstructorInit &other );
 | 
|---|
| 118 |         virtual ~ConstructorInit();
 | 
|---|
| 119 | 
 | 
|---|
| 120 |         void set_ctor( Statement * newValue ) { ctor = newValue; }
 | 
|---|
| 121 |         Statement * get_ctor() const { return ctor; }
 | 
|---|
| 122 |         void set_dtor( Statement * newValue ) { dtor = newValue; }
 | 
|---|
| 123 |         Statement * get_dtor() const { return dtor; }
 | 
|---|
| 124 |         void set_init( Initializer * newValue ) { init = newValue; }
 | 
|---|
| 125 |         Initializer * get_init() const { return init; }
 | 
|---|
| 126 | 
 | 
|---|
| 127 |         ConstructorInit *clone() const { return new ConstructorInit( *this ); }
 | 
|---|
| 128 |         virtual void accept( Visitor &v ) { v.visit( this ); }
 | 
|---|
| 129 |         virtual Initializer *acceptMutator( Mutator &m ) { return m.mutate( this ); }
 | 
|---|
| 130 |         virtual void print( std::ostream &os, int indent = 0 ) const;
 | 
|---|
| 131 | 
 | 
|---|
| 132 |   private:
 | 
|---|
| 133 |         Statement * ctor;
 | 
|---|
| 134 |         Statement * dtor;
 | 
|---|
| 135 |         // C-style initializer made up of SingleInit and ListInit nodes to use as a fallback
 | 
|---|
| 136 |         // if an appropriate constructor definition is not found by the resolver
 | 
|---|
| 137 |         Initializer * init;
 | 
|---|
| 138 | };
 | 
|---|
| 139 | 
 | 
|---|
| 140 | std::ostream & operator<<( std::ostream & out, const Initializer * init );
 | 
|---|
| 141 | std::ostream & operator<<( std::ostream & out, const Designation * des );
 | 
|---|
| 142 | 
 | 
|---|
| 143 | #endif // INITIALIZER_H
 | 
|---|
| 144 | 
 | 
|---|
| 145 | // Local Variables: //
 | 
|---|
| 146 | // tab-width: 4 //
 | 
|---|
| 147 | // mode: c++ //
 | 
|---|
| 148 | // compile-command: "make install" //
 | 
|---|
| 149 | // End: //
 | 
|---|