source: src/SymTab/Autogen.h @ c0588909

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since c0588909 was 0f8e4ac, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

change Label from a string typedef to a class

  • Property mode set to 100644
File size: 3.9 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// Autogen.h --
8//
9// Author           : Rob Schluntz
10// Created On       : Sun May 17 21:53:34 2015
11// Last Modified By : Rob Schluntz
12// Last Modified On : Tue May 19 16:49:43 2015
13// Update Count     : 1
14//
15
16#ifndef AUTOGEN_H
17#define AUTOGEN_H
18
19#include <string>
20#include "SynTree/Statement.h"
21#include "SynTree/Expression.h"
22#include "SynTree/Declaration.h"
23#include "SynTree/Initializer.h"
24
25namespace SymTab {
26  /// Generates assignment operators, constructors, and destructor for aggregate types as required
27  void autogenerateRoutines( std::list< Declaration * > &translationUnit );
28
29  // originally makeArrayAssignment - changed to Function because it is now used for ctors and dtors as well
30  // admittedly not a great name change. This used to live in Validate.cc, but has been moved so it can be reused elsewhere
31
32  /// Store in out a loop which calls fname on each element of the array with srcParam and dstParam as arguments.
33  /// If forward is true, loop goes from 0 to N-1, else N-1 to 0
34  template< typename OutputIterator >
35  void makeArrayFunction( Expression *srcParam, Expression *dstParam, ArrayType *array, std::string fname, OutputIterator out, bool forward = true ) {
36    static UniqueName indexName( "_index" );
37
38    // for a flexible array member nothing is done -- user must define own assignment
39    if ( ! array->get_dimension() ) return;
40
41    Expression * begin, * end, * update, * cmp;
42    if ( forward ) {
43      // generate: for ( int i = 0; i < 0; ++i )
44      begin = new NameExpr( "0" );
45      end = array->get_dimension()->clone();
46      cmp = new NameExpr( "?<?" );
47      update = new NameExpr( "++?" );
48    } else {
49      // generate: for ( int i = N-1; i >= 0; --i )
50      begin = new UntypedExpr( new NameExpr( "?-?" ) );
51      ((UntypedExpr*)begin)->get_args().push_back( array->get_dimension()->clone() );
52      ((UntypedExpr*)begin)->get_args().push_back( new NameExpr( "1" ) );
53      end = new NameExpr( "0" );
54      cmp = new NameExpr( "?>=?" );
55      update = new NameExpr( "--?" );
56    }
57
58    ObjectDecl *index = new ObjectDecl( indexName.newName(), DeclarationNode::NoStorageClass, LinkageSpec::C, 0, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), NULL );
59
60    UntypedExpr *init = new UntypedExpr( new NameExpr( "?=?" ) );
61    init->get_args().push_back( new AddressExpr( new VariableExpr( index ) ) );
62    init->get_args().push_back( begin );
63    index->set_init( new SingleInit( init, std::list<Expression*>() ) );
64
65    UntypedExpr *cond = new UntypedExpr( cmp );
66    cond->get_args().push_back( new VariableExpr( index ) );
67    cond->get_args().push_back( end );
68
69    UntypedExpr *inc = new UntypedExpr( update );
70    inc->get_args().push_back( new AddressExpr( new VariableExpr( index ) ) );
71
72    // want to be able to generate assignment, ctor, and dtor generically,
73    // so fname is either ?=?, ?{}, or ^?{}
74    UntypedExpr *fExpr = new UntypedExpr( new NameExpr( fname ) );
75
76    UntypedExpr *dstIndex = new UntypedExpr( new NameExpr( "?+?" ) );
77    dstIndex->get_args().push_back( dstParam );
78    dstIndex->get_args().push_back( new VariableExpr( index ) );
79    fExpr->get_args().push_back( dstIndex );
80
81    // srcParam is NULL for default ctor/dtor
82    if ( srcParam ) {
83      UntypedExpr *srcIndex = new UntypedExpr( new NameExpr( "?[?]" ) );
84      srcIndex->get_args().push_back( srcParam );
85      srcIndex->get_args().push_back( new VariableExpr( index ) );
86      fExpr->get_args().push_back( srcIndex );
87    }
88
89    std::list<Statement *> initList;
90    CompoundStmt * block = new CompoundStmt( noLabels );
91    block->get_kids().push_back( new DeclStmt( noLabels, index ) );
92    block->get_kids().push_back( new ForStmt( noLabels, initList, cond, inc, new ExprStmt( noLabels, fExpr ) ) );
93    *out++ = block;
94  }
95} // namespace SymTab
96#endif // AUTOGEN_H
97
Note: See TracBrowser for help on using the repository browser.