source: src/SynTree/Initializer.h @ bbbc067

no_list
Last change on this file since bbbc067 was bbbc067, checked in by Thierry Delisle <tdelisle@…>, 3 years ago

Changed std::list<Initializer> to vector

  • Property mode set to 100644
File size: 5.6 KB
Line 
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 : Andrew Beach
12// Last Modified On : Wed Aug  9 10:19:00 2017
13// Update Count     : 22
14//
15
16#pragma once
17
18#include <iosfwd>            // for ostream
19#include <list>              // for list, list<>::const_iterator, list<>::it...
20#include <vector>
21
22#include "BaseSyntaxNode.h"  // for BaseSyntaxNode
23#include "Mutator.h"         // for Mutator
24#include "Visitor.h"         // for Visitor
25
26class Expression;
27class Statement;
28
29// Designation: list of designator (NameExpr, VariableExpr, and ConstantExpr) expressions that specify an object being initialized.
30class Designation : public BaseSyntaxNode {
31public:
32        std::list< Expression * > designators;
33
34        Designation( const std::list< Expression * > & designators );
35        Designation( const Designation & other );
36        virtual ~Designation();
37
38        std::list< Expression * > & get_designators() { return designators; }
39
40        virtual Designation * clone() const override { return new Designation( *this ); };
41        virtual void accept( Visitor &v ) override { v.visit( this ); }
42        virtual Designation * acceptMutator( Mutator &m ) override { return m.mutate( this ); }
43        virtual void print( std::ostream &os, Indenter indent = {} ) const override;
44};
45
46const std::list<Designation *> noDesignators;
47
48// Initializer: base class for object initializers (provide default values)
49class Initializer : public BaseSyntaxNode {
50  public:
51        Initializer( bool maybeConstructed );
52        Initializer( const Initializer & other );
53        virtual ~Initializer();
54
55        bool get_maybeConstructed() { return maybeConstructed; }
56
57        virtual Initializer *clone() const override = 0;
58        virtual void accept( Visitor &v ) override = 0;
59        virtual Initializer *acceptMutator( Mutator &m ) override = 0;
60        virtual void print( std::ostream &os, Indenter indent = {} ) const override = 0;
61  private:
62        bool maybeConstructed;
63};
64
65// SingleInit represents an initializer for a common object (e.g., int x = 4)
66class SingleInit : public Initializer {
67  public:
68        //Constant *value;
69        Expression *value;      // has to be a compile-time constant
70
71        SingleInit( Expression *value, bool maybeConstructed = false );
72        SingleInit( const SingleInit &other );
73        virtual ~SingleInit();
74
75        Expression *get_value() { return value; }
76        void set_value( Expression *newValue ) { value = newValue; }
77
78        virtual SingleInit *clone() const override { return new SingleInit( *this); }
79        virtual void accept( Visitor &v ) override { v.visit( this ); }
80        virtual Initializer *acceptMutator( Mutator &m )  override { return m.mutate( this ); }
81        virtual void print( std::ostream &os, Indenter indent = {} ) const override;
82};
83
84// ListInit represents an initializer that is composed recursively of a list of initializers; this is used to initialize
85// an array or aggregate
86class ListInit : public Initializer {
87  public:
88        std::vector< Initializer * > initializers;  // order *is* important
89        std::list<Designation *> designations;  // order/length is consistent with initializers
90
91        ListInit( const std::vector< Initializer * > &initializers,
92                          const std::list<Designation *> &designators = {}, bool maybeConstructed = false );
93        ListInit( const ListInit & other );
94        virtual ~ListInit();
95
96        std::list<Designation *> & get_designations() { return designations; }
97        std::vector< Initializer * > & get_initializers() { return initializers; }
98
99        typedef std::vector< Initializer * >::iterator iterator;
100        typedef std::vector< Initializer * >::const_iterator const_iterator;
101        iterator begin() { return initializers.begin(); }
102        iterator end() { return initializers.end(); }
103        const_iterator begin() const { return initializers.begin(); }
104        const_iterator end() const { return initializers.end(); }
105
106        virtual ListInit *clone() const override { return new ListInit( *this ); }
107        virtual void accept( Visitor &v ) override { v.visit( this ); }
108        virtual Initializer *acceptMutator( Mutator &m )  override { return m.mutate( this ); }
109        virtual void print( std::ostream &os, Indenter indent = {} ) const override;
110};
111
112// ConstructorInit represents an initializer that is either a constructor expression or
113// a C-style initializer.
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.
116class ConstructorInit : public Initializer {
117  public:
118        Statement * ctor;
119        Statement * dtor;
120        // C-style initializer made up of SingleInit and ListInit nodes to use as a fallback
121        // if an appropriate constructor definition is not found by the resolver
122        Initializer * init;
123
124        ConstructorInit( Statement * ctor, Statement * dtor, Initializer * init );
125        ConstructorInit( const ConstructorInit &other );
126        virtual ~ConstructorInit();
127
128        void set_ctor( Statement * newValue ) { ctor = newValue; }
129        Statement * get_ctor() const { return ctor; }
130        void set_dtor( Statement * newValue ) { dtor = newValue; }
131        Statement * get_dtor() const { return dtor; }
132        void set_init( Initializer * newValue ) { init = newValue; }
133        Initializer * get_init() const { return init; }
134
135        ConstructorInit *clone() const override { return new ConstructorInit( *this ); }
136        virtual void accept( Visitor &v ) override { v.visit( this ); }
137        virtual Initializer *acceptMutator( Mutator &m )  override { return m.mutate( this ); }
138        virtual void print( std::ostream &os, Indenter indent = {} ) const override;
139
140  private:
141};
142
143// Local Variables: //
144// tab-width: 4 //
145// mode: c++ //
146// compile-command: "make install" //
147// End: //
Note: See TracBrowser for help on using the repository browser.