source: src/SynTree/Initializer.h @ 43e0949

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

more lists converted to vectors

  • Property mode set to 100644
File size: 5.4 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        virtual Designation * clone() const override { return new Designation( *this ); };
39        virtual void accept( Visitor &v ) override { v.visit( this ); }
40        virtual Designation * acceptMutator( Mutator &m ) override { return m.mutate( this ); }
41        virtual void print( std::ostream &os, Indenter indent = {} ) const override;
42};
43
44const std::vector<Designation *> noDesignators;
45
46// Initializer: base class for object initializers (provide default values)
47class 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 override = 0;
56        virtual void accept( Visitor &v ) override = 0;
57        virtual Initializer *acceptMutator( Mutator &m ) override = 0;
58        virtual void print( std::ostream &os, Indenter indent = {} ) const override = 0;
59  private:
60        bool maybeConstructed;
61};
62
63// SingleInit represents an initializer for a common object (e.g., int x = 4)
64class SingleInit : public Initializer {
65  public:
66        //Constant *value;
67        Expression *value;      // has to be a compile-time constant
68
69        SingleInit( Expression *value, bool maybeConstructed = false );
70        SingleInit( const SingleInit &other );
71        virtual ~SingleInit();
72
73        Expression *get_value() { return value; }
74        void set_value( Expression *newValue ) { value = newValue; }
75
76        virtual SingleInit *clone() const override { return new SingleInit( *this); }
77        virtual void accept( Visitor &v ) override { v.visit( this ); }
78        virtual Initializer *acceptMutator( Mutator &m )  override { return m.mutate( this ); }
79        virtual void print( std::ostream &os, Indenter indent = {} ) const override;
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
84class ListInit : public Initializer {
85  public:
86        std::vector< Initializer * > initializers;  // order *is* important
87        std::vector<Designation *> designations;  // order/length is consistent with initializers
88
89        ListInit( const std::vector< Initializer * > &initializers,
90                          const std::vector<Designation *> &designators = {}, bool maybeConstructed = false );
91        ListInit( const ListInit & other );
92        virtual ~ListInit();
93
94        typedef std::vector< Initializer * >::iterator iterator;
95        typedef std::vector< 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 override { return new ListInit( *this ); }
102        virtual void accept( Visitor &v ) override { v.visit( this ); }
103        virtual Initializer *acceptMutator( Mutator &m )  override { return m.mutate( this ); }
104        virtual void print( std::ostream &os, Indenter indent = {} ) const override;
105};
106
107// ConstructorInit represents an initializer that is either a constructor expression or
108// a C-style initializer.
109// It should not be necessary to create ConstructorInit nodes manually. Instead, set maybeConstructed
110// to true on SingleInit or ListInit constructors if object should be constructed.
111class ConstructorInit : public Initializer {
112  public:
113        Statement * ctor;
114        Statement * dtor;
115        // C-style initializer made up of SingleInit and ListInit nodes to use as a fallback
116        // if an appropriate constructor definition is not found by the resolver
117        Initializer * init;
118
119        ConstructorInit( Statement * ctor, Statement * dtor, Initializer * init );
120        ConstructorInit( const ConstructorInit &other );
121        virtual ~ConstructorInit();
122
123        void set_ctor( Statement * newValue ) { ctor = newValue; }
124        Statement * get_ctor() const { return ctor; }
125        void set_dtor( Statement * newValue ) { dtor = newValue; }
126        Statement * get_dtor() const { return dtor; }
127        void set_init( Initializer * newValue ) { init = newValue; }
128        Initializer * get_init() const { return init; }
129
130        ConstructorInit *clone() const override { return new ConstructorInit( *this ); }
131        virtual void accept( Visitor &v ) override { v.visit( this ); }
132        virtual Initializer *acceptMutator( Mutator &m )  override { return m.mutate( this ); }
133        virtual void print( std::ostream &os, Indenter indent = {} ) const override;
134
135  private:
136};
137
138// Local Variables: //
139// tab-width: 4 //
140// mode: c++ //
141// compile-command: "make install" //
142// End: //
Note: See TracBrowser for help on using the repository browser.