source: src/SynTree/Initializer.cc @ e213560

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 e213560 was 62423350, checked in by Rob Schluntz <rschlunt@…>, 7 years ago

Big push on designations and initialization: works with generic types, tuples, arrays, tests pass.
Refactor guard_value_impl.
Add list of declarations to TupleType?.

  • Property mode set to 100644
File size: 4.7 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.cc --
8//
9// Author           : Richard C. Bilson
10// Created On       : Mon May 18 07:44:20 2015
11// Last Modified By : Rob Schluntz
12// Last Modified On : Fri May 13 13:23:03 2016
13// Update Count     : 28
14//
15
16#include "Initializer.h"
17#include "Expression.h"
18#include "Statement.h"
19#include "Common/utility.h"
20
21Designation::Designation( const std::list< Expression * > & designators ) : designators( designators ) {}
22Designation::Designation( const Designation & other ) : BaseSyntaxNode( other ) {
23        // std::cerr << "cloning designation" << std::endl;
24        cloneAll( other.designators, designators );
25        // std::cerr << "finished cloning designation" << std::endl;
26}
27
28Designation::~Designation() {
29        // std::cerr << "destroying designation" << std::endl;
30        deleteAll( designators );
31        // std::cerr << "finished destroying designation" << std::endl;
32}
33
34void Designation::print( std::ostream &os, int indent ) const {
35        if ( ! designators.empty() ) {
36                os << std::string(indent + 2, ' ' ) << "designated by: " << std::endl;
37                for ( std::list < Expression * >::const_iterator i = designators.begin(); i != designators.end(); i++ ) {
38                        os << std::string(indent + 4, ' ' );
39                        ( *i )->print(os, indent + 4 );
40                }
41                os << std::endl;
42        } // if
43}
44
45Initializer::Initializer( bool maybeConstructed ) : maybeConstructed( maybeConstructed ) {}
46Initializer::Initializer( const Initializer & other ) : BaseSyntaxNode( other ), maybeConstructed( other.maybeConstructed ) {
47}
48Initializer::~Initializer() {}
49
50SingleInit::SingleInit( Expression *v, bool maybeConstructed ) : Initializer( maybeConstructed ), value ( v ) {
51}
52
53SingleInit::SingleInit( const SingleInit &other ) : Initializer(other), value ( maybeClone( other.value ) ) {
54}
55
56SingleInit::~SingleInit() {
57        delete value;
58}
59
60void SingleInit::print( std::ostream &os, int indent ) const {
61        os << std::string(indent, ' ' ) << "Simple Initializer: " << std::endl;
62        os << std::string(indent+4, ' ' );
63        value->print( os, indent+4 );
64}
65
66
67ListInit::ListInit( const std::list<Initializer*> &inits, const std::list<Designation *> &des, bool maybeConstructed )
68        : Initializer( maybeConstructed ), initializers( inits ), designations( des ) {
69                // handle the common case where a ListInit is created without designations by making a list of empty designations with the same length as the initializer
70                if ( designations.empty() ) {
71                        for ( auto & i : initializers ) {
72                                (void)i;
73                                designations.push_back( new Designation( {} ) );
74                        }
75                }
76                assertf( initializers.size() == designations.size(), "Created ListInit with mismatching initializers (%d) and designations (%d)", initializers.size(), designations.size() );
77}
78
79ListInit::ListInit( const ListInit & other ) : Initializer( other ) {
80        cloneAll( other.initializers, initializers );
81        cloneAll( other.designations, designations );
82}
83
84ListInit::~ListInit() {
85        deleteAll( initializers );
86        deleteAll( designations );
87}
88
89void ListInit::print( std::ostream &os, int indent ) const {
90        os << std::string(indent, ' ') << "Compound initializer:  " << std::endl;
91        for ( Designation * d : designations ) {
92                d->print( os, indent + 2 );
93        }
94
95        for ( const Initializer * init : initializers ) {
96                init->print( os, indent + 2 );
97                os << std::endl;
98        }
99}
100
101
102ConstructorInit::ConstructorInit( Statement * ctor, Statement * dtor, Initializer * init ) : Initializer( true ), ctor( ctor ), dtor( dtor ), init( init ) {}
103ConstructorInit::ConstructorInit( const ConstructorInit &other ) : Initializer( other ), ctor( maybeClone( other.ctor ) ), dtor( maybeClone( other.dtor ) ), init( maybeClone( other.init ) ) {
104}
105
106ConstructorInit::~ConstructorInit() {
107        delete ctor;
108        delete dtor;
109        delete init;
110}
111
112void ConstructorInit::print( std::ostream &os, int indent ) const {
113        os << std::endl << std::string(indent, ' ') << "Constructor initializer: " << std::endl;
114        if ( ctor ) {
115                os << std::string(indent+2, ' ');
116                os << "initially constructed with ";
117                ctor->print( os, indent+4 );
118        } // if
119
120        if ( dtor ) {
121                os << std::string(indent+2, ' ');
122                os << "destructed with ";
123                dtor->print( os, indent+4 );
124        }
125
126        if ( init ) {
127                os << std::string(indent+2, ' ');
128                os << "with fallback C-style initializer: ";
129                init->print( os, indent+4 );
130        }
131}
132
133std::ostream & operator<<( std::ostream & out, const Initializer * init ) {
134        if ( init ) {
135                init->print( out );
136        } else {
137                out << "nullptr";
138        }
139        return out;
140}
141
142std::ostream & operator<<( std::ostream & out, const Designation * des ) {
143        if ( des ) {
144                des->print( out );
145        } else {
146                out << "nullptr";
147        }
148        return out;
149}
150
151// Local Variables: //
152// tab-width: 4 //
153// mode: c++ //
154// compile-command: "make install" //
155// End: //
Note: See TracBrowser for help on using the repository browser.