summaryrefslogtreecommitdiff
path: root/src/expr/mkmetakind
blob: c4968af262b06a126b1a57fa16265a2b8d6014a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#!/bin/bash
#
# mkmetakind
# Morgan Deters <mdeters@cs.nyu.edu> for CVC4
# Copyright (c) 2010  The CVC4 Project
#
# The purpose of this script is to create metakind.h from a template
# and a list of theory kinds.
#
# This is kept distinct from kind.h because kind.h is a public header
# and metakind.h is intended for the expr package only.
#
# Invocation:
#
#   mkmetakind template-file theory-kind-files...
#
# Output is to standard out.
#

copyright=2010

cat <<EOF
/*********************                                                        */
/** metakind.h
 **
 ** Copyright $copyright  The AcSys Group, New York University, and as below.
 **
 ** This header file automatically generated by:
 **
 **     $0 $@
 **
 ** for the CVC4 project.
 **/

EOF

me=$(basename "$0")

template=$1; shift

metakind_includes=
metakind_kinds=
metakind_constantMaps=
metakind_compares=
metakind_constHashes=
metakind_constPrinters=
metakind_ubchildren=
metakind_lbchildren=
metakind_operatorKinds=

seen_theory=false
seen_theory_builtin=false

function theory {
  # theory T header

  lineno=${BASH_LINENO[0]}

  # this script doesn't care about the theory class information, but
  # makes does make sure it's there
  seen_theory=true
  if [ "$1" = builtin ]; then
    if $seen_theory_builtin; then
      echo "$kf:$lineno: error: \"builtin\" theory redefined" >&2
      exit 1
    fi
    seen_theory_builtin=true
  elif [ -z "$1" -o -z "$2" ]; then
    echo "$kf:$lineno: error: \"theory\" directive missing class or header argument" >&2
    exit 1
  elif ! expr "$1" : '\(::*\)' >/dev/null; then
    echo "$kf:$lineno: warning: theory class \`$1' isn't fully-qualified (e.g., ::CVC4::theory::foo)" >&2
  elif ! expr "$1" : '\(::CVC4::theory::*\)' >/dev/null; then
    echo "$kf:$lineno: warning: theory class not under ::CVC4::theory namespace" >&2
  fi

  theory_class=$1
  metakind_includes="${metakind_includes}
// #include \"theory/$b/$2\""
}

function variable {
  # variable K ["comment"]

  lineno=${BASH_LINENO[0]}

  check_theory_seen
  register_metakind VARIABLE "$1" 0
}

function operator {
  # operator K #children ["comment"]

  lineno=${BASH_LINENO[0]}

  check_theory_seen
  register_metakind OPERATOR "$1" "$2"
}

function parameterized {
  # parameterized K1 K2 #children ["comment"]

  lineno=${BASH_LINENO[0]}

  check_theory_seen
  register_metakind PARAMETERIZED "$1" "$3"
  registerOperatorToKind "$1" "$2"
}

function constant {
  # constant K T Hasher header ["comment"]

  lineno=${BASH_LINENO[0]}

  check_theory_seen

  if ! expr "$2" : '\(::*\)' >/dev/null; then
    if ! primitive_type "$2"; then
      # if there's an embedded space, we're probably doing something
      # tricky to specify the CONST payload, like "int const*"; in any
      # case, this warning gives too many false positives, so disable it
      if ! expr "$2" : '..* ..*' >/dev/null; then
        echo "$kf:$lineno: warning: constant $1 class \`$2' isn't fully-qualified (e.g., ::CVC4::Rational)" >&2
      fi
    fi
  fi
  if ! expr "$3" : '\(::*\)' >/dev/null; then
    echo "$kf:$lineno: warning: constant $1 hasher \`$3' isn't fully-qualified (e.g., ::CVC4::RationalHashFcn)" >&2
  fi

  if [ -n "$4" ]; then
    metakind_includes="${metakind_includes}
#include \"$4\""
  fi
  register_metakind CONSTANT "$1" 0
  metakind_constantMaps="${metakind_constantMaps}
}/* CVC4::kind::metakind namespace */
}/* CVC4::kind namespace */

namespace expr {

// The reinterpret_cast of d_children to \"$2 const*\"
// flags a \"strict aliasing\" warning; it's okay, because we never access
// the embedded constant as a NodeValue* child, and never access an embedded
// NodeValue* child as a constant.
#pragma GCC diagnostic ignored \"-Wstrict-aliasing\"

template <>
inline $2 const& NodeValue::getConst< $2 >() const {
  AssertArgument(getKind() == ::CVC4::kind::$1, *this,
                 \"Improper kind for getConst<$2>()\");
  // To support non-inlined CONSTANT-kinded NodeValues (those that are
  // \"constructed\" when initially checking them against the NodeManager
  // pool), we must check d_nchildren here.
  return d_nchildren == 0
    ? *reinterpret_cast< $2 const* >(d_children)
    : *reinterpret_cast< $2 const* >(d_children[0]);
}

// re-enable the warning
#pragma GCC diagnostic warning \"-Wstrict-aliasing\"

}/* CVC4::expr namespace */

namespace kind {
namespace metakind {

template <>
struct ConstantMap< $2 > {
  // typedef $theory_class OwningTheory;
  enum { kind = ::CVC4::kind::$1 };
};/* ConstantMap< $2 > */

template <>
struct ConstantMapReverse< ::CVC4::kind::$1 > {
  typedef $2 T;
};/* ConstantMapReverse< ::CVC4::kind::$1 > */

"
  metakind_compares="${metakind_compares}
    case kind::$1:
      return NodeValueConstCompare< kind::$1, pool >::compare(nv1, nv2);
"
  metakind_constHashes="${metakind_constHashes}
  case kind::$1:
#line $lineno \"$kf\"
    return $3::hash(nv->getConst< $2 >());
"
  metakind_constPrinters="${metakind_constPrinters}
  case kind::$1:
#line $lineno \"$kf\"
    out << nv->getConst< $2 >();
    break;
"
}

function registerOperatorToKind {
	operatorKind=$1
	applyKind=$2
	metakind_operatorKinds="${metakind_operatorKinds}    case kind::$applyKind: return kind::$operatorKind;
";
}

function register_metakind {
  mk=$1
  k=$2
  nc=$3

  metakind_kinds="${metakind_kinds}    metakind::$mk, /* $k */
";

  # figure out the range given by $nc
  if expr "$nc" : '[0-9][0-9]*$' >/dev/null; then
    lb=$nc
    ub=$nc
  elif expr "$nc" : '[0-9][0-9]*:$' >/dev/null; then
    let `echo "$nc" | awk 'BEGIN{FS=":"}{print"lb="$1}'`
    ub=MAX_CHILDREN
  elif expr "$nc" : '[0-9][0-9]*:[0-9][0-9]*$' >/dev/null; then
    let `echo "$nc" | awk 'BEGIN{FS=":"}{print"lb="$1" ub="$2}'`
    if [ $ub -lt $lb ]; then
      echo "$kf:$lineno: error in range \`$nc': LB < UB (in definition of $k)" >&2
      exit 1
    fi
  else
    echo "$kf:$lineno: can't parse range \`$nc' in definition of $k" >&2
    exit 1
  fi

  if [ $mk = OPERATOR ] || [ $mk = PARAMETERIZED -a "$k" != SORT_TYPE ]; then
    if [ $lb = 0 ]; then
      echo "$kf:$lineno: error in range \`$nc' for \`$k': $mk-kinded kinds must always take at least one child" >&2
      exit 1
    fi
  fi

  metakind_lbchildren="${metakind_lbchildren}
    $lb, /* $k */"
  metakind_ubchildren="${metakind_ubchildren}
    $ub, /* $k */"
}

# Returns 0 if arg is a primitive C++ type, or a pointer to same; 1
# otherwise.  Really all this does is check whether we should issue a
# "not fully qualified" warning or not.
function primitive_type {
  strip=`expr "$1" : ' *\(.*\)\* *'`
  if [ -n "$strip" ]; then
    primitive_type "$strip" >&2
    return $?
  fi

  case "$1" in
    bool|int|size_t|long|void|char|float|double) return 0;;
    *) return 1;;
  esac
}

function check_theory_seen {
  if ! $seen_theory; then
    echo "$kf:$lineno: error: no \"theory\" declaration found (it has to be first)" >&2
    exit 1
  fi
}

function check_builtin_theory_seen {
  if ! $seen_theory_builtin; then
    echo "$me: warning: no declaration for the builtin theory found" >&2
  fi
}

while [ $# -gt 0 ]; do
  kf=$1
  seen_theory=false
  b=$(basename $(dirname "$kf"))
  metakind_kinds="${metakind_kinds}
    /* from $b */
"
  metakind_operatorKinds="${metakind_operatorKinds}
    /* from $b */
"
  source "$kf"
  check_theory_seen
  shift
done
check_builtin_theory_seen

## output

text=$(cat "$template")
for var in \
    metakind_includes \
    metakind_kinds \
    metakind_constantMaps \
    metakind_compares \
    metakind_constHashes \
    metakind_constPrinters \
    metakind_ubchildren \
    metakind_lbchildren \
    metakind_operatorKinds; do
  eval text="\${text//\\\$\\{$var\\}/\${$var}}"
done
error=`expr "$text" : '.*\${\([^}]*\)}.*'`
if [ -n "$error" ]; then
  echo "$template:0: error: undefined replacement \${$error}" >&2
  exit 1
fi
echo "$text"
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback