source: src/SynTree/Initializer.h @ f3ddc21

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since f3ddc21 was 6013bd7, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

first attempt at named designators

  • Property mode set to 100644
File size: 5.4 KB
RevLine 
[0dd3a2f]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//
[974906e2]7// Initializer.h --
[0dd3a2f]8//
9// Author           : Richard C. Bilson
10// Created On       : Mon May 18 07:44:20 2015
[6013bd7]11// Last Modified By : Peter A. Buhr
12// Last Modified On : Thu Mar 23 16:12:42 2017
13// Update Count     : 20
[0dd3a2f]14//
15
[51b7345]16#ifndef INITIALIZER_H
17#define INITIALIZER_H
18
[294647b]19#include <cassert>
20
21#include "BaseSyntaxNode.h"
[51b7345]22#include "Mutator.h"
[294647b]23#include "SynTree.h"
[70f89d00]24#include "Type.h"
[294647b]25#include "Visitor.h"
[51b7345]26
[f0121d7]27const std::list<Expression*> noDesignators;
28
[51b7345]29// Initializer: base class for object initializers (provide default values)
[294647b]30class Initializer : public BaseSyntaxNode {
[d9a0e76]31  public:
[0dd3a2f]32        //      Initializer( std::string _name = std::string(""), int _pos = 0 );
[974906e2]33        Initializer( bool maybeConstructed );
[70f89d00]34        Initializer( const Initializer & other );
[0dd3a2f]35        virtual ~Initializer();
36
37        static std::string designator_name( Expression *designator );
38
39        //      void set_name( std::string newValue ) { name = newValue; }
40        //      std::string get_name() const { return name; }
41
42        //      void set_pos( int newValue ) { pos = newValue; }
43        //      int get_pos() const { return pos; }
44        virtual void set_designators( std::list<Expression *> & ) { assert(false); }
45        virtual std::list<Expression *> &get_designators() {
46                assert(false);
47                std::list<Expression *> *ret = 0; return *ret;  // never reached
48        }
49
[974906e2]50        bool get_maybeConstructed() { return maybeConstructed; }
51
[0dd3a2f]52        virtual Initializer *clone() const = 0;
53        virtual void accept( Visitor &v ) = 0;
54        virtual Initializer *acceptMutator( Mutator &m ) = 0;
55        virtual void print( std::ostream &os, int indent = 0 );
[d9a0e76]56  private:
[0dd3a2f]57        //      std::string name;
58        //      int pos;
[974906e2]59        bool maybeConstructed;
[51b7345]60};
61
62// SingleInit represents an initializer for a common object (e.g., int x = 4)
[d9a0e76]63class SingleInit : public Initializer {
64  public:
[db4ecc5]65        SingleInit( Expression *value, const std::list< Expression *> &designators = std::list< Expression * >(), bool maybeConstructed = false );
[0dd3a2f]66        SingleInit( const SingleInit &other );
67        virtual ~SingleInit();
[974906e2]68
[0dd3a2f]69        Expression *get_value() { return value; }
70        void set_value( Expression *newValue ) { value = newValue; }
71
72        std::list<Expression *> &get_designators() { return designators; }
[6013bd7]73        void set_designators( std::list<Expression *> &newValue ) { designators = newValue; }
[0dd3a2f]74
[70f89d00]75        virtual SingleInit *clone() const { return new SingleInit( *this); }
[0dd3a2f]76        virtual void accept( Visitor &v ) { v.visit( this ); }
77        virtual Initializer *acceptMutator( Mutator &m ) { return m.mutate( this ); }
78        virtual void print( std::ostream &os, int indent = 0 );
[d9a0e76]79  private:
[0dd3a2f]80        //Constant *value;
81        Expression *value;      // has to be a compile-time constant
82        std::list< Expression * > designators;
[51b7345]83};
84
[d9a0e76]85// ListInit represents an initializer that is composed recursively of a list of initializers; this is used to initialize
86// an array or aggregate
87class ListInit : public Initializer {
88  public:
[5b40f30]89        ListInit( const std::list<Initializer*> &initializers,
[db4ecc5]90                          const std::list<Expression *> &designators = std::list< Expression * >(), bool maybeConstructed = false );
[fc638d2]91        ListInit( const ListInit & other );
[0dd3a2f]92        virtual ~ListInit();
93
94        void set_designators( std::list<Expression *> &newValue ) { designators = newValue; }
95        std::list<Expression *> &get_designators() { return designators; }
96        void set_initializers( std::list<Initializer*> &newValue ) { initializers = newValue; }
97        std::list<Initializer*> &get_initializers() { return initializers; }
98
[4d2434a]99        typedef std::list<Initializer*>::iterator iterator;
100        iterator begin() { return initializers.begin(); }
101        iterator end() { return initializers.end(); }
[0dd3a2f]102
[70f89d00]103        virtual ListInit *clone() const { return new ListInit( *this ); }
[0dd3a2f]104        virtual void accept( Visitor &v ) { v.visit( this ); }
105        virtual Initializer *acceptMutator( Mutator &m ) { return m.mutate( this ); }
106        virtual void print( std::ostream &os, int indent = 0 );
[d9a0e76]107  private:
[0dd3a2f]108        std::list<Initializer*> initializers;  // order *is* important
109        std::list<Expression *> designators;
[51b7345]110};
111
[71f4e4f]112// ConstructorInit represents an initializer that is either a constructor expression or
113// a C-style initializer.
[64ac636]114// It should not be necessary to create ConstructorInit nodes manually. Instead, set maybeConstructed
115// to true on SingleInit or ListInit constructors if object should be constructed.
[71f4e4f]116class ConstructorInit : public Initializer {
117  public:
[f1b1e4c]118        ConstructorInit( Statement * ctor, Statement * dtor, Initializer * init );
[70f89d00]119        ConstructorInit( const ConstructorInit &other );
[71f4e4f]120        virtual ~ConstructorInit();
121
[5b2f5bb]122        void set_ctor( Statement * newValue ) { ctor = newValue; }
123        Statement * get_ctor() const { return ctor; }
124        void set_dtor( Statement * newValue ) { dtor = newValue; }
125        Statement * get_dtor() const { return dtor; }
[71f4e4f]126        void set_init( Initializer * newValue ) { init = newValue; }
127        Initializer * get_init() const { return init; }
128
[70f89d00]129        ConstructorInit *clone() const { return new ConstructorInit( *this ); }
[71f4e4f]130        virtual void accept( Visitor &v ) { v.visit( this ); }
131        virtual Initializer *acceptMutator( Mutator &m ) { return m.mutate( this ); }
132        virtual void print( std::ostream &os, int indent = 0 );
133
134  private:
[5b2f5bb]135        Statement * ctor;
136        Statement * dtor;
[71f4e4f]137        // C-style initializer made up of SingleInit and ListInit nodes to use as a fallback
138        // if an appropriate constructor definition is not found by the resolver
139        Initializer * init;
140};
141
[7a69460]142std::ostream & operator<<( std::ostream & out, Initializer * init );
143
[d9a0e76]144#endif // INITIALIZER_H
[0dd3a2f]145
146// Local Variables: //
147// tab-width: 4 //
148// mode: c++ //
149// compile-command: "make install" //
150// End: //
Note: See TracBrowser for help on using the repository browser.