source: src/CodeGen/OperatorTable.cc @ 8a6cf7e

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

Refactor operator predicates into OperatorTable?.cc

  • Property mode set to 100644
File size: 5.1 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// OperatorTable.cc --
8//
9// Author           : Richard C. Bilson
10// Created On       : Mon May 18 07:44:20 2015
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Tue Dec 13 14:33:05 2016
13// Update Count     : 10
14//
15
16#include <algorithm>  // for any_of
17#include <map>        // for map, _Rb_tree_const_iterator, map<>::const_iterator
18#include <utility>    // for pair
19
20#include "OperatorTable.h"
21
22namespace CodeGen {
23        namespace {
24                const OperatorInfo tableValues[] = {
25                        {       "?[?]",         "",             "_operator_index",                              OT_INDEX                        },
26                        {       "?{}",          "=",    "_constructor",                                 OT_CTOR                         },
27                        {       "^?{}",         "",             "_destructor",                                  OT_DTOR                         },
28                        {       "?()",          "",             "_operator_call",                               OT_CALL                         },
29                        {       "?++",          "++",   "_operator_postincr",                   OT_POSTFIXASSIGN        },
30                        {       "?--",          "--",   "_operator_postdecr",                   OT_POSTFIXASSIGN        },
31                        {       "*?",           "*",    "_operator_deref",                              OT_PREFIX                       },
32                        {       "+?",           "+",    "_operator_unaryplus",                  OT_PREFIX                       },
33                        {       "-?",           "-",    "_operator_unaryminus",                 OT_PREFIX                       },
34                        {       "~?",           "~",    "_operator_bitnot",                             OT_PREFIX                       },
35                        {       "!?",           "!",    "_operator_lognot",                             OT_PREFIX                       },
36                        {       "++?",          "++",   "_operator_preincr",                    OT_PREFIXASSIGN         },
37                        {       "--?",          "--",   "_operator_predecr",                    OT_PREFIXASSIGN         },
38                        {       "?*?",          "*",    "_operator_multiply",                   OT_INFIX                        },
39                        {       "?/?",          "/",    "_operator_divide",                             OT_INFIX                        },
40                        {       "?%?",          "%",    "_operator_modulus",                    OT_INFIX                        },
41                        {       "?+?",          "+",    "_operator_add",                                OT_INFIX                        },
42                        {       "?-?",          "-",    "_operator_subtract",                   OT_INFIX                        },
43                        {       "?<<?",         "<<",   "_operator_shiftleft",                  OT_INFIX                        },
44                        {       "?>>?",         ">>",   "_operator_shiftright",                 OT_INFIX                        },
45                        {       "?<?",          "<",    "_operator_less",                               OT_INFIX                        },
46                        {       "?>?",          ">",    "_operator_greater",                    OT_INFIX                        },
47                        {       "?<=?",         "<=",   "_operator_lessequal",                  OT_INFIX                        },
48                        {       "?>=?",         ">=",   "_operator_greaterequal",               OT_INFIX                        },
49                        {       "?==?",         "==",   "_operator_equal",                              OT_INFIX                        },
50                        {       "?!=?",         "!=",   "_operator_notequal",                   OT_INFIX                        },
51                        {       "?&?",          "&",    "_operator_bitand",                             OT_INFIX                        },
52                        {       "?^?",          "^",    "_operator_bitxor",                             OT_INFIX                        },
53                        {       "?|?",          "|",    "_operator_bitor",                              OT_INFIX                        },
54                        {       "?=?",          "=",    "_operator_assign",                             OT_INFIXASSIGN          },
55                        {       "?*=?",         "*=",   "_operator_multassign",                 OT_INFIXASSIGN          },
56                        {       "?/=?",         "/=",   "_operator_divassign",                  OT_INFIXASSIGN          },
57                        {       "?%=?",         "%=",   "_operator_modassign",                  OT_INFIXASSIGN          },
58                        {       "?+=?",         "+=",   "_operator_addassign",                  OT_INFIXASSIGN          },
59                        {       "?-=?",         "-=",   "_operator_subassign",                  OT_INFIXASSIGN          },
60                        {       "?<<=?",        "<<=",  "_operator_shiftleftassign",    OT_INFIXASSIGN          },
61                        {       "?>>=?",        ">>=",  "_operator_shiftrightassign",   OT_INFIXASSIGN          },
62                        {       "?&=?",         "&=",   "_operator_bitandassign",               OT_INFIXASSIGN          },
63                        {       "?^=?",         "^=",   "_operator_bitxorassign",               OT_INFIXASSIGN          },
64                        {       "?|=?",         "|=",   "_operator_bitorassign",                OT_INFIXASSIGN          },
65                        {       "&&",           "&&",   "&&",                                                   OT_LABELADDRESS         },
66                        {       "0",            "0",    "_constant_zero",                               OT_CONSTANT                     },
67                        {       "1",            "1",    "_constant_one",                                OT_CONSTANT                     }
68                };
69
70                const int numOps = sizeof( tableValues ) / sizeof( OperatorInfo );
71
72                std::map< std::string, OperatorInfo > table;
73
74                void initialize() {
75                        for ( int i = 0; i < numOps; ++i ) {
76                                table[ tableValues[i].inputName ] = tableValues[i];
77                        } // for
78                }
79        } // namespace
80
81        bool operatorLookup( std::string funcName, OperatorInfo &info ) {
82                static bool init = false;
83                if ( ! init ) {
84                        initialize();
85                } // if
86                std::map< std::string, OperatorInfo >::const_iterator i = table.find( funcName );
87                if ( i == table.end() ) {
88                        return false;
89                } else {
90                        info = i->second;
91                        return true;
92                } // if
93        }
94
95        /// determines if a given function name is one of the operator types between [begin, end)
96        template<typename Iterator>
97        bool isOperatorType( const std::string & funcName, Iterator begin, Iterator end ) {
98                OperatorInfo info;
99                if ( operatorLookup( funcName, info ) ) {
100                        return std::find( begin, end, info.type ) != end;
101                }
102                return false;
103        }
104
105        bool isConstructor( const std::string & funcName ) {
106                static OperatorType types[] = { OT_CTOR };
107                return isOperatorType( funcName, std::begin(types), std::end(types) );
108        }
109
110        bool isDestructor( const std::string & funcName ) {
111                static OperatorType types[] = { OT_DTOR };
112                return isOperatorType( funcName, std::begin(types), std::end(types) );
113        }
114
115        bool isAssignment( const std::string & funcName ) {
116                static OperatorType types[] = { OT_PREFIXASSIGN, OT_POSTFIXASSIGN, OT_INFIXASSIGN };
117                return isOperatorType( funcName, std::begin(types), std::end(types) );
118        }
119
120        bool isCtorDtor( const std::string & funcName ) {
121                static OperatorType types[] = { OT_CTOR, OT_DTOR };
122                return isOperatorType( funcName, std::begin(types), std::end(types) );
123        }
124
125        bool isCtorDtorAssign( const std::string & funcName ) {
126                static OperatorType types[] = { OT_CTOR, OT_DTOR, OT_PREFIXASSIGN, OT_POSTFIXASSIGN, OT_INFIXASSIGN };
127                return isOperatorType( funcName, std::begin(types), std::end(types) );
128        }
129} // namespace CodeGen
130
131// Local Variables: //
132// tab-width: 4 //
133// mode: c++ //
134// compile-command: "make install" //
135// End: //
Note: See TracBrowser for help on using the repository browser.