summaryrefslogtreecommitdiff
path: root/src/api/checks.h
blob: 85322048cbdd9d21bee96bc5f09bce7262f480bb (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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*********************                                                        */
/*! \file checks.h
 ** \verbatim
 ** Top contributors (to current version):
 **   Aina Niemetz
 ** This file is part of the CVC4 project.
 ** Copyright (c) 2009-2021 by the authors listed in the file AUTHORS
 ** in the top-level source directory and their institutional affiliations.
 ** All rights reserved.  See the file COPYING in the top-level source
 ** directory for licensing information.\endverbatim
 **
 ** \brief Check macros for the CVC5 C++ API.
 **
 ** These macros implement guards for the CVC5 C++ API functions.
 **/

#include "cvc4_public.h"

#ifndef CVC4__API__CHECKS_H
#define CVC4__API__CHECKS_H

namespace cvc4 {
namespace api {

/* -------------------------------------------------------------------------- */
/* Basic check macros.                                                        */
/* -------------------------------------------------------------------------- */

/**
 * The base check macro.
 * Throws a CVC4ApiException if 'cond' is false.
 */
#define CVC4_API_CHECK(cond) \
  CVC4_PREDICT_TRUE(cond)    \
  ? (void)0 : OstreamVoider() & CVC4ApiExceptionStream().ostream()

/**
 * The base check macro for throwing recoverable exceptions.
 * Throws a CVC4RecoverableApiException if 'cond' is false.
 */
#define CVC4_API_RECOVERABLE_CHECK(cond) \
  CVC4_PREDICT_TRUE(cond)                \
  ? (void)0 : OstreamVoider() & CVC4ApiRecoverableExceptionStream().ostream()

/* -------------------------------------------------------------------------- */
/* Not null checks.                                                           */
/* -------------------------------------------------------------------------- */

/** Check it 'this' is not a null object. */
#define CVC4_API_CHECK_NOT_NULL                     \
  CVC4_API_CHECK(!isNullHelper())                   \
      << "Invalid call to '" << __PRETTY_FUNCTION__ \
      << "', expected non-null object";

/** Check if given argument is not a null object. */
#define CVC4_API_ARG_CHECK_NOT_NULL(arg) \
  CVC4_API_CHECK(!arg.isNull()) << "Invalid null argument for '" << #arg << "'";

/** Check if given argument is not a null pointer. */
#define CVC4_API_ARG_CHECK_NOT_NULLPTR(arg) \
  CVC4_API_CHECK(arg != nullptr)            \
      << "Invalid null argument for '" << #arg << "'";
/**
 * Check if given argument at given index in container 'args' is not a null
 * object.
 */
#define CVC4_API_ARG_AT_INDEX_CHECK_NOT_NULL(what, arg, args, idx)      \
  CVC4_API_CHECK(!arg.isNull()) << "Invalid null " << (what) << " in '" \
                                << #args << "' at index " << (idx);

/* -------------------------------------------------------------------------- */
/* Kind checks.                                                               */
/* -------------------------------------------------------------------------- */

/** Check if given kind is a valid kind. */
#define CVC4_API_KIND_CHECK(kind)     \
  CVC4_API_CHECK(isDefinedKind(kind)) \
      << "Invalid kind '" << kindToString(kind) << "'";

/**
 * Check if given kind is a valid kind.
 * Creates a stream to provide a message that identifies what kind was expected
 * if given kind is invalid.
 */
#define CVC4_API_KIND_CHECK_EXPECTED(cond, kind) \
  CVC4_PREDICT_TRUE(cond)                        \
  ? (void)0                                      \
  : OstreamVoider()                              \
          & CVC4ApiExceptionStream().ostream()   \
                << "Invalid kind '" << kindToString(kind) << "', expected "

/* -------------------------------------------------------------------------- */
/* Argument checks.                                                           */
/* -------------------------------------------------------------------------- */

/**
 * Check condition 'cond' for given argument 'arg'.
 * Creates a stream to provide a message that identifies what was expected to
 * hold if condition is false and throws a non-recoverable exception.
 */
#define CVC4_API_ARG_CHECK_EXPECTED(cond, arg)                      \
  CVC4_PREDICT_TRUE(cond)                                           \
  ? (void)0                                                         \
  : OstreamVoider()                                                 \
          & CVC4ApiExceptionStream().ostream()                      \
                << "Invalid argument '" << arg << "' for '" << #arg \
                << "', expected "

/**
 * Check condition 'cond' for given argument 'arg'.
 * Creates a stream to provide a message that identifies what was expected to
 * hold if condition is false and throws a recoverable exception.
 */
#define CVC4_API_RECOVERABLE_ARG_CHECK_EXPECTED(cond, arg)          \
  CVC4_PREDICT_TRUE(cond)                                           \
  ? (void)0                                                         \
  : OstreamVoider()                                                 \
          & CVC4ApiRecoverableExceptionStream().ostream()           \
                << "Invalid argument '" << arg << "' for '" << #arg \
                << "', expected "

/**
 * Check condition 'cond' for given argument 'arg'.
 * Provides a more specific error message than CVC4_API_ARG_CHECK_EXPECTED,
 * it identifies that this check is a size check.
 * Creates a stream to provide a message that identifies what was expected to
 * hold if condition is false and throws a recoverable exception.
 */
#define CVC4_API_ARG_SIZE_CHECK_EXPECTED(cond, arg) \
  CVC4_PREDICT_TRUE(cond)                           \
  ? (void)0                                         \
  : OstreamVoider()                                 \
          & CVC4ApiExceptionStream().ostream()      \
                << "Invalid size of argument '" << #arg << "', expected "

/**
 * Check condition 'cond' for the argument at given index in container 'args'.
 * Argument 'what' identifies what is being checked (e.g., "term").
 * Creates a stream to provide a message that identifies what was expected to
 * hold if condition is false.
 * Usage:
 *   CVC4_API_ARG_AT_INDEX_CHECK_EXPECTED(
 *     <condition>, "what", <container>, <idx>) << "message";
 */
#define CVC4_API_ARG_AT_INDEX_CHECK_EXPECTED(cond, what, args, idx)          \
  CVC4_PREDICT_TRUE(cond)                                                    \
  ? (void)0                                                                  \
  : OstreamVoider()                                                          \
          & CVC4ApiExceptionStream().ostream()                               \
                << "Invalid " << (what) << " in '" << #args << "' at index " \
                << (idx) << ", expected "

/* -------------------------------------------------------------------------- */
/* Solver checks.                                                             */
/* -------------------------------------------------------------------------- */

/**
 * Solver check for member functions of classes other than class Solver.
 * Check if given solver matches the solver object this object is associated
 * with.
 */
#define CVC4_API_ARG_CHECK_SOLVER(what, arg)                              \
  CVC4_API_CHECK(this->d_solver == arg.d_solver)                          \
      << "Given " << (what) << " is not associated with the solver this " \
      << "object is associated with";

/* -------------------------------------------------------------------------- */
/* Sort checks.                                                               */
/* -------------------------------------------------------------------------- */

/**
 * Sort check for member functions of classes other than class Solver.
 * Check if given sort is not null and associated with the solver object this
 * object is associated with.
 */
#define CVC4_API_CHECK_SORT(sort)            \
  do                                         \
  {                                          \
    CVC4_API_ARG_CHECK_NOT_NULL(sort);       \
    CVC4_API_ARG_CHECK_SOLVER("sort", sort); \
  } while (0)

/**
 * Sort check for member functions of classes other than class Solver.
 * Check if each sort in the given container of sorts is not null and
 * associated with the solver object this object is associated with.
 */
#define CVC4_API_CHECK_SORTS(sorts)                                         \
  do                                                                        \
  {                                                                         \
    size_t i = 0;                                                           \
    for (const auto& s : sorts)                                             \
    {                                                                       \
      CVC4_API_ARG_AT_INDEX_CHECK_NOT_NULL("sort", s, sorts, i);            \
      CVC4_API_ARG_AT_INDEX_CHECK_EXPECTED(                                 \
          this->d_solver == s.d_solver, "sort", sorts, i)                   \
          << "a sort associated with the solver this object is associated " \
             "with";                                                        \
      i += 1;                                                               \
    }                                                                       \
  } while (0)

/* -------------------------------------------------------------------------- */
/* Term checks.                                                               */
/* -------------------------------------------------------------------------- */

/**
 * Term check for member functions of classes other than class Solver.
 * Check if given term is not null and associated with the solver object this
 * object is associated with.
 */
#define CVC4_API_CHECK_TERM(term)            \
  do                                         \
  {                                          \
    CVC4_API_ARG_CHECK_NOT_NULL(term);       \
    CVC4_API_ARG_CHECK_SOLVER("term", term); \
  } while (0)

/**
 * Term check for member functions of classes other than class Solver.
 * Check if each term in the given container of terms is not null and
 * associated with the solver object this object is associated with.
 */
#define CVC4_API_CHECK_TERMS(terms)                                         \
  do                                                                        \
  {                                                                         \
    size_t i = 0;                                                           \
    for (const auto& s : terms)                                             \
    {                                                                       \
      CVC4_API_ARG_AT_INDEX_CHECK_NOT_NULL("term", s, terms, i);            \
      CVC4_API_ARG_AT_INDEX_CHECK_EXPECTED(                                 \
          this->d_solver == s.d_solver, "term", terms, i)                   \
          << "a term associated with the solver this object is associated " \
             "with";                                                        \
      i += 1;                                                               \
    }                                                                       \
  } while (0)

/**
 * Term check for member functions of classes other than class Solver.
 * Check if each term and sort in the given map (which maps terms to sorts) is
 * not null and associated with the solver object this object is associated
 * with.
 */
#define CVC4_API_CHECK_TERMS_MAP(map)                                       \
  do                                                                        \
  {                                                                         \
    size_t i = 0;                                                           \
    for (const auto& p : map)                                               \
    {                                                                       \
      CVC4_API_ARG_AT_INDEX_CHECK_NOT_NULL("term", p.first, map, i);        \
      CVC4_API_ARG_AT_INDEX_CHECK_EXPECTED(                                 \
          this->d_solver == p.first.d_solver, "term", map, i)               \
          << "a term associated with the solver this object is associated " \
             "with";                                                        \
      CVC4_API_ARG_AT_INDEX_CHECK_NOT_NULL("sort", p.second, map, i);       \
      CVC4_API_ARG_AT_INDEX_CHECK_EXPECTED(                                 \
          this->d_solver == p.second.d_solver, "sort", map, i)              \
          << "a sort associated with the solver this object is associated " \
             "with";                                                        \
      i += 1;                                                               \
    }                                                                       \
  } while (0)

/**
 * Term check for member functions of classes other than class Solver.
 * Check if each term in the given container is not null, associated with the
 * solver object this object is associated with, and of the given sort.
 */
#define CVC4_API_CHECK_TERMS_WITH_SORT(terms, sort)                            \
  do                                                                           \
  {                                                                            \
    size_t i = 0;                                                              \
    for (const auto& t : terms)                                                \
    {                                                                          \
      CVC4_API_ARG_AT_INDEX_CHECK_NOT_NULL("term", t, terms, i);               \
      CVC4_API_ARG_AT_INDEX_CHECK_EXPECTED(                                    \
          this->d_solver == t.d_solver, "term", terms, i)                      \
          << "a term associated with the solver this object is associated "    \
             "with";                                                           \
      CVC4_API_CHECK(t.getSort() == sort)                                      \
          << "Expected term with sort " << sort << " at index " << i << " in " \
          << #terms;                                                           \
      i += 1;                                                                  \
    }                                                                          \
  } while (0)

/**
 * Term check for member functions of classes other than class Solver.
 * Check if each term in both the given container is not null, associated with
 * the solver object this object is associated with, and their sorts are
 * pairwise comparable to.
 */
#define CVC4_API_TERM_CHECK_TERMS_WITH_TERMS_COMPARABLE_TO(terms1, terms2)  \
  do                                                                        \
  {                                                                         \
    size_t i = 0;                                                           \
    for (const auto& t1 : terms1)                                           \
    {                                                                       \
      const auto& t2 = terms2[i];                                           \
      CVC4_API_ARG_AT_INDEX_CHECK_NOT_NULL("term", t1, terms1, i);          \
      CVC4_API_ARG_AT_INDEX_CHECK_EXPECTED(                                 \
          this->d_solver == t1.d_solver, "term", terms1, i)                 \
          << "a term associated with the solver this object is associated " \
             "with";                                                        \
      CVC4_API_ARG_AT_INDEX_CHECK_NOT_NULL("term", t2, terms2, i);          \
      CVC4_API_ARG_AT_INDEX_CHECK_EXPECTED(                                 \
          this->d_solver == t2.d_solver, "term", terms2, i)                 \
          << "a term associated with the solver this object is associated " \
             "with";                                                        \
      CVC4_API_CHECK(t1.getSort().isComparableTo(t2.getSort()))             \
          << "Expecting terms of comparable sort at index " << i;           \
      i += 1;                                                               \
    }                                                                       \
  } while (0)

/* -------------------------------------------------------------------------- */
/* DatatypeDecl checks.                                                       */
/* -------------------------------------------------------------------------- */

/**
 * DatatypeDecl check for member functions of classes other than class Solver.
 * Check if given datatype declaration is not null and associated with the
 * solver object this DatatypeDecl object is associated with.
 */
#define CVC4_API_CHECK_DTDECL(decl)                          \
  do                                                         \
  {                                                          \
    CVC4_API_ARG_CHECK_NOT_NULL(decl);                       \
    CVC4_API_ARG_CHECK_SOLVER("datatype declaration", decl); \
  } while (0)

/* -------------------------------------------------------------------------- */
/* Checks for class Solver.                                                   */
/* -------------------------------------------------------------------------- */

/** Sort checks for member functions of class Solver. */
#define CVC4_API_SOLVER_CHECK_SORT(sort) \
  CVC4_API_CHECK(this == sort.d_solver)  \
      << "Given sort is not associated with this solver";

/** Term checks for member functions of class Solver. */
#define CVC4_API_SOLVER_CHECK_TERM(term) \
  CVC4_API_CHECK(this == term.d_solver)  \
      << "Given term is not associated with this solver";

/** Op checks for member functions of class Solver. */
#define CVC4_API_SOLVER_CHECK_OP(op)  \
  CVC4_API_CHECK(this == op.d_solver) \
      << "Given operator is not associated with this solver";

}  // namespace api
}  // namespace cvc4
#endif
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback