source: src/CodeGen/OperatorTable.cpp@ 56ec508

Last change on this file since 56ec508 was 15cb790, checked in by Andrew Beach <ajbeach@…>, 9 months ago

Added a check in operatorLookup so bad operators give a proper error message instead of segment fault.

  • Property mode set to 100644
File size: 7.2 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.cpp --
8//
9// Author : Richard C. Bilson
10// Created On : Mon May 18 07:44:20 2015
11// Last Modified By : Andrew Beach
12// Last Modified On : Fri Nov 3 16:00:00 2023
13// Update Count : 56
14//
15
16#include "OperatorTable.hpp"
17
18#include <cassert> // for assert
19#include <unordered_map> // for unordered_map
20
21namespace CodeGen {
22
23static const OperatorInfo tableValues[] = {
24 // inputName symbol outputName friendlyName type
25 { "?[?]", "", "_operator_index", "Index", OT_INDEX },
26 { "?{}", "=", "_constructor", "Constructor", OT_CTOR },
27 { "^?{}", "", "_destructor", "Destructor", OT_DTOR },
28 { "?()", "", "_operator_call", "Call Operator", OT_CALL },
29 { "?++", "++", "_operator_postincr", "Postfix Increment", OT_POSTFIXASSIGN },
30 { "?--", "--", "_operator_postdecr", "Postfix Decrement", OT_POSTFIXASSIGN },
31 { "*?", "*", "_operator_deref", "Dereference", OT_PREFIX },
32 { "+?", "+", "_operator_unaryplus", "Plus", OT_PREFIX },
33 { "-?", "-", "_operator_unaryminus", "Minus", OT_PREFIX },
34 { "~?", "~", "_operator_bitnot", "Bitwise Not", OT_PREFIX },
35 { "!?", "!", "_operator_lognot", "Logical Not", OT_PREFIX },
36 { "++?", "++", "_operator_preincr", "Prefix Increment", OT_PREFIXASSIGN },
37 { "--?", "--", "_operator_predecr", "Prefix Decrement", OT_PREFIXASSIGN },
38 { "?\\?", "\\", "_operator_exponential", "Exponentiation", OT_INFIX },
39 { "?*?", "*", "_operator_multiply", "Multiplication", OT_INFIX },
40 { "?/?", "/", "_operator_divide", "Division", OT_INFIX },
41 { "?%?", "%", "_operator_modulus", "Modulo", OT_INFIX },
42 { "?+?", "+", "_operator_add", "Addition", OT_INFIX },
43 { "?-?", "-", "_operator_subtract", "Substraction", OT_INFIX },
44 { "?<<?", "<<", "_operator_shiftleft", "Shift Left", OT_INFIX },
45 { "?>>?", ">>", "_operator_shiftright", "Shift Right", OT_INFIX },
46 { "?<?", "<", "_operator_less", "Less-than", OT_INFIX },
47 { "?>?", ">", "_operator_greater", "Greater-than", OT_INFIX },
48 { "?<=?", "<=", "_operator_lessequal", "Less-than-or-Equal", OT_INFIX },
49 { "?>=?", ">=", "_operator_greaterequal", "Greater-than-or-Equal", OT_INFIX },
50 { "?==?", "==", "_operator_equal", "Equality", OT_INFIX },
51 { "?!=?", "!=", "_operator_notequal", "Not-Equal", OT_INFIX },
52 { "?&?", "&", "_operator_bitand", "Bitwise And", OT_INFIX },
53 { "?^?", "^", "_operator_bitxor", "Bitwise Xor", OT_INFIX },
54 { "?|?", "|", "_operator_bitor", "Bitwise Or", OT_INFIX },
55 { "?=?", "=", "_operator_assign", "Assignment", OT_INFIXASSIGN },
56 { "?\\=?", "\\=", "_operator_expassign", "Exponentiation Assignment", OT_INFIXASSIGN },
57 { "?*=?", "*=", "_operator_multassign", "Multiplication Assignment", OT_INFIXASSIGN },
58 { "?/=?", "/=", "_operator_divassign", "Division Assignment", OT_INFIXASSIGN },
59 { "?%=?", "%=", "_operator_modassign", "Modulo Assignment", OT_INFIXASSIGN },
60 { "?+=?", "+=", "_operator_addassign", "Addition Assignment", OT_INFIXASSIGN },
61 { "?-=?", "-=", "_operator_subassign", "Substrction Assignment", OT_INFIXASSIGN },
62 { "?<<=?", "<<=", "_operator_shiftleftassign", "Shift Left Assignment", OT_INFIXASSIGN },
63 { "?>>=?", ">>=", "_operator_shiftrightassign", "Shift Right Assignment", OT_INFIXASSIGN },
64 { "?&=?", "&=", "_operator_bitandassign", "Bitwise And Assignment", OT_INFIXASSIGN },
65 { "?^=?", "^=", "_operator_bitxorassign", "Bitwise Xor Assignment", OT_INFIXASSIGN },
66 { "?|=?", "|=", "_operator_bitorassign", "Bitwise Or Assignment", OT_INFIXASSIGN },
67}; // tableValues
68
69enum { numOps = sizeof( tableValues ) / sizeof( OperatorInfo ) };
70
71const OperatorInfo * operatorLookup( const std::string & inputName ) {
72 // Static information set up:
73 static std::unordered_map<std::string, const OperatorInfo *> inputTable;
74 if ( inputTable.empty() ) for ( const OperatorInfo & op : tableValues ) {
75 inputTable[ op.inputName ] = &op;
76 }
77
78 // Filter out non-operator names:
79 if ( inputName.find_first_of( "?^*+-!", 0, 1 ) == std::string::npos ) return nullptr;
80 auto entry = inputTable.find( inputName );
81 // This can only happen if an invalid identifier name has been used.
82 assertf( entry != inputTable.end(), "Not a real operator: %s\n", inputName.c_str() );
83 const OperatorInfo * ret = entry->second;
84 assert( ret );
85 return ret;
86}
87
88bool isOperator( const std::string & inputName ) {
89 return operatorLookup( inputName ) != nullptr;
90}
91
92std::string operatorFriendlyName( const std::string & inputName ) {
93 const OperatorInfo * info = operatorLookup( inputName );
94 if ( info ) return info->friendlyName;
95 return "";
96}
97
98// This is only used in the demangler, so it is smaller (and only maybe slow).
99const OperatorInfo * operatorLookupByOutput( const std::string & outputName ) {
100 if ( '_' != outputName[0] ) return nullptr;
101 for ( const OperatorInfo & op : tableValues ) {
102 if ( outputName == op.outputName ) {
103 return &op;
104 }
105 }
106 return nullptr;
107}
108
109bool isConstructor( const std::string & inputName ) {
110 const OperatorInfo * info = operatorLookup( inputName );
111 if ( info ) return info->type == OT_CTOR;
112 return false;
113}
114
115bool isDestructor( const std::string & inputName ) {
116 const OperatorInfo * info = operatorLookup( inputName );
117 if ( info ) return info->type == OT_DTOR;
118 return false;
119}
120
121bool isCtorDtor( const std::string & inputName ) {
122 const OperatorInfo * info = operatorLookup( inputName );
123 if ( info ) return info->type <= OT_CONSTRUCTOR;
124 return false;
125}
126
127bool isAssignment( const std::string & inputName ) {
128 const OperatorInfo * info = operatorLookup( inputName );
129 if ( info ) return info->type > OT_CONSTRUCTOR && info->type <= OT_ASSIGNMENT;
130 return false;
131}
132
133bool isCtorDtorAssign( const std::string & inputName ) {
134 const OperatorInfo * info = operatorLookup( inputName );
135 if ( info ) return info->type <= OT_ASSIGNMENT;
136 return false;
137}
138
139} // namespace CodeGen
140
141// Local Variables: //
142// tab-width: 4 //
143// End: //
Note: See TracBrowser for help on using the repository browser.