[51587aa] | 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 | // |
---|
[6943f051] | 7 | // OperatorTable.cc -- |
---|
[51587aa] | 8 | // |
---|
| 9 | // Author : Richard C. Bilson |
---|
| 10 | // Created On : Mon May 18 07:44:20 2015 |
---|
[58dd019] | 11 | // Last Modified By : Peter A. Buhr |
---|
[e5f2a67] | 12 | // Last Modified On : Sat Jul 15 17:12:22 2017 |
---|
| 13 | // Update Count : 15 |
---|
[51587aa] | 14 | // |
---|
[51b7345] | 15 | |
---|
[bff227f] | 16 | #include <algorithm> // for any_of |
---|
| 17 | #include <map> // for map, _Rb_tree_const_iterator, map<>::const_iterator |
---|
| 18 | #include <utility> // for pair |
---|
[bf2438c] | 19 | |
---|
[51b7345] | 20 | #include "OperatorTable.h" |
---|
[bc3127d] | 21 | #include "Common/utility.h" |
---|
[51b7345] | 22 | |
---|
| 23 | namespace CodeGen { |
---|
[51587aa] | 24 | namespace { |
---|
| 25 | const OperatorInfo tableValues[] = { |
---|
[98538288] | 26 | { "?[?]", "", "_operator_index", "Index", OT_INDEX }, |
---|
| 27 | { "?{}", "=", "_constructor", "Constructor", OT_CTOR }, |
---|
| 28 | { "^?{}", "", "_destructor", "Destructor", OT_DTOR }, |
---|
| 29 | { "?()", "", "_operator_call", "Call Operator", OT_CALL }, |
---|
| 30 | { "?++", "++", "_operator_postincr", "Postfix Increment", OT_POSTFIXASSIGN }, |
---|
| 31 | { "?--", "--", "_operator_postdecr", "Postfix Decrement", OT_POSTFIXASSIGN }, |
---|
| 32 | { "*?", "*", "_operator_deref", "Dereference", OT_PREFIX }, |
---|
| 33 | { "+?", "+", "_operator_unaryplus", "Plus", OT_PREFIX }, |
---|
| 34 | { "-?", "-", "_operator_unaryminus", "Minus", OT_PREFIX }, |
---|
| 35 | { "~?", "~", "_operator_bitnot", "Bitwise Not", OT_PREFIX }, |
---|
| 36 | { "!?", "!", "_operator_lognot", "Logical Not", OT_PREFIX }, |
---|
| 37 | { "++?", "++", "_operator_preincr", "Prefix Increment", OT_PREFIXASSIGN }, |
---|
| 38 | { "--?", "--", "_operator_predecr", "Prefix Decrement", OT_PREFIXASSIGN }, |
---|
| 39 | { "?\\?", "\\", "_operator_exponential", "Exponentiation", OT_INFIX }, |
---|
| 40 | { "?*?", "*", "_operator_multiply", "Multiplication", OT_INFIX }, |
---|
| 41 | { "?/?", "/", "_operator_divide", "Division", OT_INFIX }, |
---|
| 42 | { "?%?", "%", "_operator_modulus", "Modulo", OT_INFIX }, |
---|
| 43 | { "?+?", "+", "_operator_add", "Addition", OT_INFIX }, |
---|
| 44 | { "?-?", "-", "_operator_subtract", "Substraction", OT_INFIX }, |
---|
| 45 | { "?<<?", "<<", "_operator_shiftleft", "Shift Left", OT_INFIX }, |
---|
| 46 | { "?>>?", ">>", "_operator_shiftright", "Shift Right", OT_INFIX }, |
---|
| 47 | { "?<?", "<", "_operator_less", "Less-than", OT_INFIX }, |
---|
| 48 | { "?>?", ">", "_operator_greater", "Greater-than", OT_INFIX }, |
---|
| 49 | { "?<=?", "<=", "_operator_lessequal", "Less-than-or-Equal", OT_INFIX }, |
---|
| 50 | { "?>=?", ">=", "_operator_greaterequal", "Greater-than-or-Equal", OT_INFIX }, |
---|
| 51 | { "?==?", "==", "_operator_equal", "Equality", OT_INFIX }, |
---|
| 52 | { "?!=?", "!=", "_operator_notequal", "Not-Equal", OT_INFIX }, |
---|
| 53 | { "?&?", "&", "_operator_bitand", "Bitwise And", OT_INFIX }, |
---|
| 54 | { "?^?", "^", "_operator_bitxor", "Bitwise Xor", OT_INFIX }, |
---|
| 55 | { "?|?", "|", "_operator_bitor", "Bitwise Or", OT_INFIX }, |
---|
| 56 | { "?=?", "=", "_operator_assign", "Assignment", OT_INFIXASSIGN }, |
---|
| 57 | { "?\\=?", "\\=", "_operator_expassign", "Exponentiation Assignment", OT_INFIXASSIGN }, |
---|
| 58 | { "?*=?", "*=", "_operator_multassign", "Multiplication Assignment", OT_INFIXASSIGN }, |
---|
| 59 | { "?/=?", "/=", "_operator_divassign", "Division Assignment", OT_INFIXASSIGN }, |
---|
| 60 | { "?%=?", "%=", "_operator_modassign", "Modulo Assignment", OT_INFIXASSIGN }, |
---|
| 61 | { "?+=?", "+=", "_operator_addassign", "Addition Assignment", OT_INFIXASSIGN }, |
---|
| 62 | { "?-=?", "-=", "_operator_subassign", "Substrction Assignment", OT_INFIXASSIGN }, |
---|
| 63 | { "?<<=?", "<<=", "_operator_shiftleftassign", "Shift Left Assignment", OT_INFIXASSIGN }, |
---|
| 64 | { "?>>=?", ">>=", "_operator_shiftrightassign", "Shift Right Assignment", OT_INFIXASSIGN }, |
---|
| 65 | { "?&=?", "&=", "_operator_bitandassign", "Bitwise And Assignment", OT_INFIXASSIGN }, |
---|
| 66 | { "?^=?", "^=", "_operator_bitxorassign", "Bitwise Xor Assignment", OT_INFIXASSIGN }, |
---|
| 67 | { "?|=?", "|=", "_operator_bitorassign", "Bitwise Or Assignment", OT_INFIXASSIGN }, |
---|
[51587aa] | 68 | }; |
---|
[51b7345] | 69 | |
---|
[51587aa] | 70 | const int numOps = sizeof( tableValues ) / sizeof( OperatorInfo ); |
---|
[51b7345] | 71 | |
---|
[51587aa] | 72 | std::map< std::string, OperatorInfo > table; |
---|
[51b7345] | 73 | |
---|
[51587aa] | 74 | void initialize() { |
---|
| 75 | for ( int i = 0; i < numOps; ++i ) { |
---|
| 76 | table[ tableValues[i].inputName ] = tableValues[i]; |
---|
| 77 | } // for |
---|
| 78 | } |
---|
| 79 | } // namespace |
---|
[51b7345] | 80 | |
---|
[3d2b7bc] | 81 | bool operatorLookup( const std::string & funcName, OperatorInfo & info ) { |
---|
[51587aa] | 82 | static bool init = false; |
---|
| 83 | if ( ! init ) { |
---|
| 84 | initialize(); |
---|
| 85 | } // if |
---|
[bc3127d] | 86 | |
---|
[51587aa] | 87 | std::map< std::string, OperatorInfo >::const_iterator i = table.find( funcName ); |
---|
| 88 | if ( i == table.end() ) { |
---|
[bc3127d] | 89 | if ( isPrefix( funcName, "?`" ) ) { |
---|
| 90 | // handle literal suffixes, which are user-defined postfix operators |
---|
| 91 | info.inputName = funcName; |
---|
| 92 | info.symbol = funcName.substr(2); |
---|
| 93 | info.outputName = toString( "__operator_literal_", info.symbol ); |
---|
| 94 | info.type = OT_POSTFIX; |
---|
| 95 | return true; |
---|
| 96 | } |
---|
[51587aa] | 97 | return false; |
---|
| 98 | } else { |
---|
| 99 | info = i->second; |
---|
| 100 | return true; |
---|
| 101 | } // if |
---|
| 102 | } |
---|
[bff227f] | 103 | |
---|
[3d2b7bc] | 104 | bool isOperator( const std::string & funcName ) { |
---|
| 105 | OperatorInfo info; |
---|
| 106 | return operatorLookup( funcName, info ); |
---|
| 107 | } |
---|
| 108 | |
---|
[98538288] | 109 | std::string operatorFriendlyName( const std::string & funcName ) { |
---|
| 110 | OperatorInfo info; |
---|
| 111 | if( operatorLookup( funcName, info ) ) { |
---|
| 112 | return info.friendlyName; |
---|
| 113 | } |
---|
| 114 | return ""; |
---|
| 115 | } |
---|
| 116 | |
---|
[bff227f] | 117 | /// determines if a given function name is one of the operator types between [begin, end) |
---|
| 118 | template<typename Iterator> |
---|
| 119 | bool isOperatorType( const std::string & funcName, Iterator begin, Iterator end ) { |
---|
| 120 | OperatorInfo info; |
---|
| 121 | if ( operatorLookup( funcName, info ) ) { |
---|
| 122 | return std::find( begin, end, info.type ) != end; |
---|
| 123 | } |
---|
| 124 | return false; |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | bool isConstructor( const std::string & funcName ) { |
---|
| 128 | static OperatorType types[] = { OT_CTOR }; |
---|
| 129 | return isOperatorType( funcName, std::begin(types), std::end(types) ); |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | bool isDestructor( const std::string & funcName ) { |
---|
| 133 | static OperatorType types[] = { OT_DTOR }; |
---|
| 134 | return isOperatorType( funcName, std::begin(types), std::end(types) ); |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | bool isAssignment( const std::string & funcName ) { |
---|
| 138 | static OperatorType types[] = { OT_PREFIXASSIGN, OT_POSTFIXASSIGN, OT_INFIXASSIGN }; |
---|
| 139 | return isOperatorType( funcName, std::begin(types), std::end(types) ); |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | bool isCtorDtor( const std::string & funcName ) { |
---|
| 143 | static OperatorType types[] = { OT_CTOR, OT_DTOR }; |
---|
| 144 | return isOperatorType( funcName, std::begin(types), std::end(types) ); |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | bool isCtorDtorAssign( const std::string & funcName ) { |
---|
| 148 | static OperatorType types[] = { OT_CTOR, OT_DTOR, OT_PREFIXASSIGN, OT_POSTFIXASSIGN, OT_INFIXASSIGN }; |
---|
| 149 | return isOperatorType( funcName, std::begin(types), std::end(types) ); |
---|
| 150 | } |
---|
[51b7345] | 151 | } // namespace CodeGen |
---|
[51587aa] | 152 | |
---|
| 153 | // Local Variables: // |
---|
| 154 | // tab-width: 4 // |
---|
| 155 | // mode: c++ // |
---|
| 156 | // compile-command: "make install" // |
---|
| 157 | // End: // |
---|