// // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo // // The contents of this file are covered under the licence agreement in the // file "LICENCE" distributed with Cforall. // // Init.hpp -- // // Author : Aaron B. Moss // Created On : Fri May 10 10:30:00 2019 // Last Modified By : Aaron B. Moss // Created On : Fri May 10 10:30:00 2019 // Update Count : 1 // #pragma once #include // for move #include #include "ParseNode.hpp" #include "Node.hpp" // for ptr #include "Visitor.hpp" namespace ast { class Expr; class Stmt; /// List of designator (NameExpr, VariableExpr, and ConstantExpr) expressions that specify an /// object being initialized class Designation final : public ParseNode { public: std::vector> designators; Designation( const CodeLocation& loc, std::vector>&& ds = {} ) : ParseNode( loc ), designators( std::move(ds) ) {} virtual const Designation* accept( Visitor& v ) const override { return v.visit( this ); } private: virtual Designation* clone() const override { return new Designation{ *this }; } }; /// Object initializer base class class Init : public ParseNode { public: bool maybeConstructed; Init( const CodeLocation& loc, bool mc ) : ParseNode( loc ), maybeConstructed( mc ) {} virtual const Init * accept( Visitor& v ) const override = 0; private: virtual const Init * clone() const override = 0; }; /// Initializer for a common object: `int x = 4` class SingleInit final : public Init { public: /// value to initialize to. Must be compile-time constant. ptr value; SingleInit( const CodeLocation& loc, Expr* val, bool mc = false ) : Init( loc, mc ), value( val ) {} virtual const Init * accept( Visitor & v ) const override { return v.visit( this ); } private: virtual SingleInit * clone() const override { return new SingleInit{ *this }; } /// Must be copied in ALL derived classes template friend auto mutate(const node_t * node); }; /// Initializer recursively composed of a list of initializers. /// Used to initialize an array or aggregate: `int a[] = { 1, 2, 3 }` class ListInit final : public Init { public: /// list of initializers std::vector> initializers; /// list of designators; order/length is consistent with initializers std::vector> designations; ListInit( const CodeLocation& loc, std::vector>&& is, std::vector>&& ds = {}, bool mc = false ); using iterator = std::vector>::iterator; using const_iterator = std::vector>::const_iterator; iterator begin() { return initializers.begin(); } iterator end() { return initializers.end(); } const_iterator begin() const { return initializers.begin(); } const_iterator end() const { return initializers.end(); } virtual const Init * accept( Visitor & v ) const override { return v.visit( this ); } private: virtual ListInit * clone() const override { return new ListInit{ *this }; } /// Must be copied in ALL derived classes template friend auto mutate(const node_t * node); }; /// Either a constructor expression or a C-style initializer. /// Should not be necessary to create manually; instead set `maybeConstructed` true on `SingleInit` /// or `ListInit` if the object should be constructed. class ConstructorInit final : public Init { public: ptr ctor; ptr dtor; /// C-style initializer made up of SingleInit/ListInit nodes to use as a fallback if an /// appropriate constructor definition is not found by the resolver. ptr init; ConstructorInit( const CodeLocation& loc, Stmt* ctor, Stmt* dtor, Init* init ) : Init( loc, true ), ctor( ctor ), dtor( dtor ), init( init ) {} virtual const Init * accept( Visitor & v ) const override { return v.visit( this ); } private: virtual ConstructorInit * clone() const override { return new ConstructorInit{ *this }; } /// Must be copied in ALL derived classes template friend auto mutate(const node_t * node); }; //================================================================================================= /// This disgusting and giant piece of boiler-plate is here to solve a cyclic dependency /// remove only if there is a better solution /// The problem is that ast::ptr< ... > uses increment/decrement which won't work well with /// forward declarations inline void increment( const class Init * node, Node::ref_type ref ) { node->increment( ref ); } inline void decrement( const class Init * node, Node::ref_type ref ) { node->decrement( ref ); } inline void increment( const class SingleInit * node, Node::ref_type ref ) { node->increment( ref ); } inline void decrement( const class SingleInit * node, Node::ref_type ref ) { node->decrement( ref ); } inline void increment( const class ListInit * node, Node::ref_type ref ) { node->increment( ref ); } inline void decrement( const class ListInit * node, Node::ref_type ref ) { node->decrement( ref ); } inline void increment( const class ConstructorInit * node, Node::ref_type ref ) { node->increment( ref ); } inline void decrement( const class ConstructorInit * node, Node::ref_type ref ) { node->decrement( ref ); } } // Local Variables: // // tab-width: 4 // // mode: c++ // // compile-command: "make install" // // End: //