GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/parser/smt2/smt2.cpp Lines: 587 672 87.4 %
Date: 2021-09-17 Branches: 1054 2212 47.6 %

Line Exec Source
1
/******************************************************************************
2
 * Top contributors (to current version):
3
 *   Andrew Reynolds, Andres Noetzli, Morgan Deters
4
 *
5
 * This file is part of the cvc5 project.
6
 *
7
 * Copyright (c) 2009-2021 by the authors listed in the file AUTHORS
8
 * in the top-level source directory and their institutional affiliations.
9
 * All rights reserved.  See the file COPYING in the top-level source
10
 * directory for licensing information.
11
 * ****************************************************************************
12
 *
13
 * Definitions of SMT2 constants.
14
 */
15
#include "parser/smt2/smt2.h"
16
17
#include <algorithm>
18
19
#include "base/check.h"
20
#include "parser/antlr_input.h"
21
#include "parser/parser.h"
22
#include "parser/smt2/smt2_input.h"
23
24
// ANTLR defines these, which is really bad!
25
#undef true
26
#undef false
27
28
namespace cvc5 {
29
namespace parser {
30
31
5473
Smt2::Smt2(api::Solver* solver,
32
           SymbolManager* sm,
33
           bool strictMode,
34
5473
           bool parseOnly)
35
    : Parser(solver, sm, strictMode, parseOnly),
36
      d_logicSet(false),
37
5473
      d_seenSetLogic(false)
38
{
39
5473
}
40
41
10946
Smt2::~Smt2() {}
42
43
5492
void Smt2::addArithmeticOperators() {
44
5492
  addOperator(api::PLUS, "+");
45
5492
  addOperator(api::MINUS, "-");
46
  // api::MINUS is converted to api::UMINUS if there is only a single operand
47
5492
  Parser::addOperator(api::UMINUS);
48
5492
  addOperator(api::MULT, "*");
49
5492
  addOperator(api::LT, "<");
50
5492
  addOperator(api::LEQ, "<=");
51
5492
  addOperator(api::GT, ">");
52
5492
  addOperator(api::GEQ, ">=");
53
54
5492
  if (!strictModeEnabled())
55
  {
56
    // NOTE: this operator is non-standard
57
5488
    addOperator(api::POW, "^");
58
  }
59
5492
}
60
61
1666
void Smt2::addTranscendentalOperators()
62
{
63
1666
  addOperator(api::EXPONENTIAL, "exp");
64
1666
  addOperator(api::SINE, "sin");
65
1666
  addOperator(api::COSINE, "cos");
66
1666
  addOperator(api::TANGENT, "tan");
67
1666
  addOperator(api::COSECANT, "csc");
68
1666
  addOperator(api::SECANT, "sec");
69
1666
  addOperator(api::COTANGENT, "cot");
70
1666
  addOperator(api::ARCSINE, "arcsin");
71
1666
  addOperator(api::ARCCOSINE, "arccos");
72
1666
  addOperator(api::ARCTANGENT, "arctan");
73
1666
  addOperator(api::ARCCOSECANT, "arccsc");
74
1666
  addOperator(api::ARCSECANT, "arcsec");
75
1666
  addOperator(api::ARCCOTANGENT, "arccot");
76
1666
  addOperator(api::SQRT, "sqrt");
77
1666
}
78
79
2354
void Smt2::addQuantifiersOperators()
80
{
81
2354
}
82
83
2812
void Smt2::addBitvectorOperators() {
84
2812
  addOperator(api::BITVECTOR_CONCAT, "concat");
85
2812
  addOperator(api::BITVECTOR_NOT, "bvnot");
86
2812
  addOperator(api::BITVECTOR_AND, "bvand");
87
2812
  addOperator(api::BITVECTOR_OR, "bvor");
88
2812
  addOperator(api::BITVECTOR_NEG, "bvneg");
89
2812
  addOperator(api::BITVECTOR_ADD, "bvadd");
90
2812
  addOperator(api::BITVECTOR_MULT, "bvmul");
91
2812
  addOperator(api::BITVECTOR_UDIV, "bvudiv");
92
2812
  addOperator(api::BITVECTOR_UREM, "bvurem");
93
2812
  addOperator(api::BITVECTOR_SHL, "bvshl");
94
2812
  addOperator(api::BITVECTOR_LSHR, "bvlshr");
95
2812
  addOperator(api::BITVECTOR_ULT, "bvult");
96
2812
  addOperator(api::BITVECTOR_NAND, "bvnand");
97
2812
  addOperator(api::BITVECTOR_NOR, "bvnor");
98
2812
  addOperator(api::BITVECTOR_XOR, "bvxor");
99
2812
  addOperator(api::BITVECTOR_XNOR, "bvxnor");
100
2812
  addOperator(api::BITVECTOR_COMP, "bvcomp");
101
2812
  addOperator(api::BITVECTOR_SUB, "bvsub");
102
2812
  addOperator(api::BITVECTOR_SDIV, "bvsdiv");
103
2812
  addOperator(api::BITVECTOR_SREM, "bvsrem");
104
2812
  addOperator(api::BITVECTOR_SMOD, "bvsmod");
105
2812
  addOperator(api::BITVECTOR_ASHR, "bvashr");
106
2812
  addOperator(api::BITVECTOR_ULE, "bvule");
107
2812
  addOperator(api::BITVECTOR_UGT, "bvugt");
108
2812
  addOperator(api::BITVECTOR_UGE, "bvuge");
109
2812
  addOperator(api::BITVECTOR_SLT, "bvslt");
110
2812
  addOperator(api::BITVECTOR_SLE, "bvsle");
111
2812
  addOperator(api::BITVECTOR_SGT, "bvsgt");
112
2812
  addOperator(api::BITVECTOR_SGE, "bvsge");
113
2812
  addOperator(api::BITVECTOR_REDOR, "bvredor");
114
2812
  addOperator(api::BITVECTOR_REDAND, "bvredand");
115
116
2812
  addIndexedOperator(api::BITVECTOR_EXTRACT, api::BITVECTOR_EXTRACT, "extract");
117
2812
  addIndexedOperator(api::BITVECTOR_REPEAT, api::BITVECTOR_REPEAT, "repeat");
118
2812
  addIndexedOperator(
119
      api::BITVECTOR_ZERO_EXTEND, api::BITVECTOR_ZERO_EXTEND, "zero_extend");
120
2812
  addIndexedOperator(
121
      api::BITVECTOR_SIGN_EXTEND, api::BITVECTOR_SIGN_EXTEND, "sign_extend");
122
2812
  addIndexedOperator(
123
      api::BITVECTOR_ROTATE_LEFT, api::BITVECTOR_ROTATE_LEFT, "rotate_left");
124
2812
  addIndexedOperator(
125
      api::BITVECTOR_ROTATE_RIGHT, api::BITVECTOR_ROTATE_RIGHT, "rotate_right");
126
2812
}
127
128
1630
void Smt2::addDatatypesOperators()
129
{
130
1630
  Parser::addOperator(api::APPLY_CONSTRUCTOR);
131
1630
  Parser::addOperator(api::APPLY_TESTER);
132
1630
  Parser::addOperator(api::APPLY_SELECTOR);
133
134
1630
  if (!strictModeEnabled())
135
  {
136
1630
    Parser::addOperator(api::APPLY_UPDATER);
137
1630
    addOperator(api::DT_SIZE, "dt.size");
138
  }
139
1630
}
140
141
2013
void Smt2::addStringOperators() {
142
4026
  defineVar(
143
      "re.all",
144
4026
      getSolver()->mkTerm(api::REGEXP_STAR, getSolver()->mkRegexpSigma()));
145
2013
  addOperator(api::STRING_CONCAT, "str.++");
146
2013
  addOperator(api::STRING_LENGTH, "str.len");
147
2013
  addOperator(api::STRING_SUBSTR, "str.substr");
148
2013
  addOperator(api::STRING_CONTAINS, "str.contains");
149
2013
  addOperator(api::STRING_CHARAT, "str.at");
150
2013
  addOperator(api::STRING_INDEXOF, "str.indexof");
151
2013
  addOperator(api::STRING_REPLACE, "str.replace");
152
2013
  addOperator(api::STRING_PREFIX, "str.prefixof");
153
2013
  addOperator(api::STRING_SUFFIX, "str.suffixof");
154
2013
  addOperator(api::STRING_FROM_CODE, "str.from_code");
155
2013
  addOperator(api::STRING_IS_DIGIT, "str.is_digit");
156
2013
  addOperator(api::STRING_REPLACE_RE, "str.replace_re");
157
2013
  addOperator(api::STRING_REPLACE_RE_ALL, "str.replace_re_all");
158
2013
  if (!strictModeEnabled())
159
  {
160
2013
    addOperator(api::STRING_INDEXOF_RE, "str.indexof_re");
161
2013
    addOperator(api::STRING_UPDATE, "str.update");
162
2013
    addOperator(api::STRING_TOLOWER, "str.tolower");
163
2013
    addOperator(api::STRING_TOUPPER, "str.toupper");
164
2013
    addOperator(api::STRING_REV, "str.rev");
165
    // sequence versions
166
2013
    addOperator(api::SEQ_CONCAT, "seq.++");
167
2013
    addOperator(api::SEQ_LENGTH, "seq.len");
168
2013
    addOperator(api::SEQ_EXTRACT, "seq.extract");
169
2013
    addOperator(api::SEQ_UPDATE, "seq.update");
170
2013
    addOperator(api::SEQ_AT, "seq.at");
171
2013
    addOperator(api::SEQ_CONTAINS, "seq.contains");
172
2013
    addOperator(api::SEQ_INDEXOF, "seq.indexof");
173
2013
    addOperator(api::SEQ_REPLACE, "seq.replace");
174
2013
    addOperator(api::SEQ_PREFIX, "seq.prefixof");
175
2013
    addOperator(api::SEQ_SUFFIX, "seq.suffixof");
176
2013
    addOperator(api::SEQ_REV, "seq.rev");
177
2013
    addOperator(api::SEQ_REPLACE_ALL, "seq.replace_all");
178
2013
    addOperator(api::SEQ_UNIT, "seq.unit");
179
2013
    addOperator(api::SEQ_NTH, "seq.nth");
180
  }
181
2013
  addOperator(api::STRING_FROM_INT, "str.from_int");
182
2013
  addOperator(api::STRING_TO_INT, "str.to_int");
183
2013
  addOperator(api::STRING_IN_REGEXP, "str.in_re");
184
2013
  addOperator(api::STRING_TO_REGEXP, "str.to_re");
185
2013
  addOperator(api::STRING_TO_CODE, "str.to_code");
186
2013
  addOperator(api::STRING_REPLACE_ALL, "str.replace_all");
187
188
2013
  addOperator(api::REGEXP_CONCAT, "re.++");
189
2013
  addOperator(api::REGEXP_UNION, "re.union");
190
2013
  addOperator(api::REGEXP_INTER, "re.inter");
191
2013
  addOperator(api::REGEXP_STAR, "re.*");
192
2013
  addOperator(api::REGEXP_PLUS, "re.+");
193
2013
  addOperator(api::REGEXP_OPT, "re.opt");
194
2013
  addIndexedOperator(api::REGEXP_REPEAT, api::REGEXP_REPEAT, "re.^");
195
2013
  addIndexedOperator(api::REGEXP_LOOP, api::REGEXP_LOOP, "re.loop");
196
2013
  addOperator(api::REGEXP_RANGE, "re.range");
197
2013
  addOperator(api::REGEXP_COMPLEMENT, "re.comp");
198
2013
  addOperator(api::REGEXP_DIFF, "re.diff");
199
2013
  addOperator(api::STRING_LT, "str.<");
200
2013
  addOperator(api::STRING_LEQ, "str.<=");
201
2013
}
202
203
1627
void Smt2::addFloatingPointOperators() {
204
1627
  addOperator(api::FLOATINGPOINT_FP, "fp");
205
1627
  addOperator(api::FLOATINGPOINT_EQ, "fp.eq");
206
1627
  addOperator(api::FLOATINGPOINT_ABS, "fp.abs");
207
1627
  addOperator(api::FLOATINGPOINT_NEG, "fp.neg");
208
1627
  addOperator(api::FLOATINGPOINT_ADD, "fp.add");
209
1627
  addOperator(api::FLOATINGPOINT_SUB, "fp.sub");
210
1627
  addOperator(api::FLOATINGPOINT_MULT, "fp.mul");
211
1627
  addOperator(api::FLOATINGPOINT_DIV, "fp.div");
212
1627
  addOperator(api::FLOATINGPOINT_FMA, "fp.fma");
213
1627
  addOperator(api::FLOATINGPOINT_SQRT, "fp.sqrt");
214
1627
  addOperator(api::FLOATINGPOINT_REM, "fp.rem");
215
1627
  addOperator(api::FLOATINGPOINT_RTI, "fp.roundToIntegral");
216
1627
  addOperator(api::FLOATINGPOINT_MIN, "fp.min");
217
1627
  addOperator(api::FLOATINGPOINT_MAX, "fp.max");
218
1627
  addOperator(api::FLOATINGPOINT_LEQ, "fp.leq");
219
1627
  addOperator(api::FLOATINGPOINT_LT, "fp.lt");
220
1627
  addOperator(api::FLOATINGPOINT_GEQ, "fp.geq");
221
1627
  addOperator(api::FLOATINGPOINT_GT, "fp.gt");
222
1627
  addOperator(api::FLOATINGPOINT_ISN, "fp.isNormal");
223
1627
  addOperator(api::FLOATINGPOINT_ISSN, "fp.isSubnormal");
224
1627
  addOperator(api::FLOATINGPOINT_ISZ, "fp.isZero");
225
1627
  addOperator(api::FLOATINGPOINT_ISINF, "fp.isInfinite");
226
1627
  addOperator(api::FLOATINGPOINT_ISNAN, "fp.isNaN");
227
1627
  addOperator(api::FLOATINGPOINT_ISNEG, "fp.isNegative");
228
1627
  addOperator(api::FLOATINGPOINT_ISPOS, "fp.isPositive");
229
1627
  addOperator(api::FLOATINGPOINT_TO_REAL, "fp.to_real");
230
231
1627
  addIndexedOperator(api::FLOATINGPOINT_TO_FP_GENERIC,
232
                     api::FLOATINGPOINT_TO_FP_GENERIC,
233
                     "to_fp");
234
1627
  addIndexedOperator(api::FLOATINGPOINT_TO_FP_UNSIGNED_BITVECTOR,
235
                     api::FLOATINGPOINT_TO_FP_UNSIGNED_BITVECTOR,
236
                     "to_fp_unsigned");
237
1627
  addIndexedOperator(
238
      api::FLOATINGPOINT_TO_UBV, api::FLOATINGPOINT_TO_UBV, "fp.to_ubv");
239
1627
  addIndexedOperator(
240
      api::FLOATINGPOINT_TO_SBV, api::FLOATINGPOINT_TO_SBV, "fp.to_sbv");
241
242
1627
  if (!strictModeEnabled())
243
  {
244
1625
    addIndexedOperator(api::FLOATINGPOINT_TO_FP_IEEE_BITVECTOR,
245
                       api::FLOATINGPOINT_TO_FP_IEEE_BITVECTOR,
246
                       "to_fp_bv");
247
1625
    addIndexedOperator(api::FLOATINGPOINT_TO_FP_FLOATINGPOINT,
248
                       api::FLOATINGPOINT_TO_FP_FLOATINGPOINT,
249
                       "to_fp_fp");
250
1625
    addIndexedOperator(api::FLOATINGPOINT_TO_FP_REAL,
251
                       api::FLOATINGPOINT_TO_FP_REAL,
252
                       "to_fp_real");
253
1625
    addIndexedOperator(api::FLOATINGPOINT_TO_FP_SIGNED_BITVECTOR,
254
                       api::FLOATINGPOINT_TO_FP_SIGNED_BITVECTOR,
255
                       "to_fp_signed");
256
  }
257
1627
}
258
259
1572
void Smt2::addSepOperators() {
260
1572
  addOperator(api::SEP_STAR, "sep");
261
1572
  addOperator(api::SEP_PTO, "pto");
262
1572
  addOperator(api::SEP_WAND, "wand");
263
1572
  addOperator(api::SEP_EMP, "emp");
264
1572
  Parser::addOperator(api::SEP_STAR);
265
1572
  Parser::addOperator(api::SEP_PTO);
266
1572
  Parser::addOperator(api::SEP_WAND);
267
1572
  Parser::addOperator(api::SEP_EMP);
268
1572
}
269
270
5431
void Smt2::addCoreSymbols()
271
{
272
5431
  defineType("Bool", d_solver->getBooleanSort(), true, true);
273
5431
  defineVar("true", d_solver->mkTrue(), true, true);
274
5431
  defineVar("false", d_solver->mkFalse(), true, true);
275
5431
  addOperator(api::AND, "and");
276
5431
  addOperator(api::DISTINCT, "distinct");
277
5431
  addOperator(api::EQUAL, "=");
278
5431
  addOperator(api::IMPLIES, "=>");
279
5431
  addOperator(api::ITE, "ite");
280
5431
  addOperator(api::NOT, "not");
281
5431
  addOperator(api::OR, "or");
282
5431
  addOperator(api::XOR, "xor");
283
5431
}
284
285
426498
void Smt2::addOperator(api::Kind kind, const std::string& name)
286
{
287
852996
  Debug("parser") << "Smt2::addOperator( " << kind << ", " << name << " )"
288
426498
                  << std::endl;
289
426498
  Parser::addOperator(kind);
290
426498
  operatorKindMap[name] = kind;
291
426498
}
292
293
42616
void Smt2::addIndexedOperator(api::Kind tKind,
294
                              api::Kind opKind,
295
                              const std::string& name)
296
{
297
42616
  Parser::addOperator(tKind);
298
42616
  d_indexedOpKindMap[name] = opKind;
299
42616
}
300
301
2892390
api::Kind Smt2::getOperatorKind(const std::string& name) const
302
{
303
  // precondition: isOperatorEnabled(name)
304
2892390
  return operatorKindMap.find(name)->second;
305
}
306
307
3393482
bool Smt2::isOperatorEnabled(const std::string& name) const {
308
3393482
  return operatorKindMap.find(name) != operatorKindMap.end();
309
}
310
311
40453
bool Smt2::isTheoryEnabled(theory::TheoryId theory) const
312
{
313
40453
  return d_logic.isTheoryEnabled(theory);
314
}
315
316
2894646
bool Smt2::isHoEnabled() const { return d_logic.isHigherOrder(); }
317
318
259173
bool Smt2::logicIsSet() {
319
259173
  return d_logicSet;
320
}
321
322
3485582
api::Term Smt2::getExpressionForNameAndType(const std::string& name,
323
                                            api::Sort t)
324
{
325
3485582
  if (isAbstractValue(name))
326
  {
327
    return mkAbstractValue(name);
328
  }
329
3485582
  return Parser::getExpressionForNameAndType(name, t);
330
}
331
332
2197
bool Smt2::getTesterName(api::Term cons, std::string& name)
333
{
334
2197
  if ((v2_6() || sygus()) && strictModeEnabled())
335
  {
336
    // 2.6 or above uses indexed tester symbols, if we are in strict mode,
337
    // we do not automatically define is-cons for constructor cons.
338
    return false;
339
  }
340
4394
  std::stringstream ss;
341
2197
  ss << "is-" << cons;
342
2197
  name = ss.str();
343
2197
  return true;
344
}
345
346
89123
api::Term Smt2::mkIndexedConstant(const std::string& name,
347
                                  const std::vector<uint64_t>& numerals)
348
{
349
89123
  if (d_logic.isTheoryEnabled(theory::THEORY_FP))
350
  {
351
1090
    if (name == "+oo")
352
    {
353
4
      return d_solver->mkPosInf(numerals[0], numerals[1]);
354
    }
355
1086
    else if (name == "-oo")
356
    {
357
2
      return d_solver->mkNegInf(numerals[0], numerals[1]);
358
    }
359
1084
    else if (name == "NaN")
360
    {
361
4
      return d_solver->mkNaN(numerals[0], numerals[1]);
362
    }
363
1080
    else if (name == "+zero")
364
    {
365
5
      return d_solver->mkPosZero(numerals[0], numerals[1]);
366
    }
367
1075
    else if (name == "-zero")
368
    {
369
2
      return d_solver->mkNegZero(numerals[0], numerals[1]);
370
    }
371
  }
372
373
89106
  if (d_logic.isTheoryEnabled(theory::THEORY_BV) && name.find("bv") == 0)
374
  {
375
178212
    std::string bvStr = name.substr(2);
376
89106
    return d_solver->mkBitVector(numerals[0], bvStr, 10);
377
  }
378
379
  // NOTE: Theory parametric constants go here
380
381
  parseError(std::string("Unknown indexed literal `") + name + "'");
382
  return api::Term();
383
}
384
385
76441
api::Op Smt2::mkIndexedOp(const std::string& name,
386
                          const std::vector<uint64_t>& numerals)
387
{
388
76441
  const auto& kIt = d_indexedOpKindMap.find(name);
389
76441
  if (kIt != d_indexedOpKindMap.end())
390
  {
391
76441
    api::Kind k = (*kIt).second;
392
76441
    if (numerals.size() == 1)
393
    {
394
64184
      return d_solver->mkOp(k, numerals[0]);
395
    }
396
12257
    else if (numerals.size() == 2)
397
    {
398
12257
      return d_solver->mkOp(k, numerals[0], numerals[1]);
399
    }
400
  }
401
402
  parseError(std::string("Unknown indexed function `") + name + "'");
403
  return api::Op();
404
}
405
406
150
api::Term Smt2::bindDefineFunRec(
407
    const std::string& fname,
408
    const std::vector<std::pair<std::string, api::Sort>>& sortedVarNames,
409
    api::Sort t,
410
    std::vector<api::Term>& flattenVars)
411
{
412
300
  std::vector<api::Sort> sorts;
413
454
  for (const std::pair<std::string, api::Sort>& svn : sortedVarNames)
414
  {
415
304
    sorts.push_back(svn.second);
416
  }
417
418
  // make the flattened function type, add bound variables
419
  // to flattenVars if the defined function was given a function return type.
420
300
  api::Sort ft = mkFlatFunctionType(sorts, t, flattenVars);
421
422
  // allow overloading
423
300
  return bindVar(fname, ft, false, true);
424
}
425
426
150
void Smt2::pushDefineFunRecScope(
427
    const std::vector<std::pair<std::string, api::Sort>>& sortedVarNames,
428
    api::Term func,
429
    const std::vector<api::Term>& flattenVars,
430
    std::vector<api::Term>& bvs)
431
{
432
150
  pushScope();
433
434
  // bound variables are those that are explicitly named in the preamble
435
  // of the define-fun(s)-rec command, we define them here
436
454
  for (const std::pair<std::string, api::Sort>& svn : sortedVarNames)
437
  {
438
608
    api::Term v = bindBoundVar(svn.first, svn.second);
439
304
    bvs.push_back(v);
440
  }
441
442
150
  bvs.insert(bvs.end(), flattenVars.begin(), flattenVars.end());
443
150
}
444
445
41
void Smt2::reset() {
446
41
  d_logicSet = false;
447
41
  d_seenSetLogic = false;
448
41
  d_logic = LogicInfo();
449
41
  operatorKindMap.clear();
450
41
  d_lastNamedTerm = std::pair<api::Term, std::string>();
451
41
}
452
453
14
std::unique_ptr<Command> Smt2::invConstraint(
454
    const std::vector<std::string>& names)
455
{
456
14
  checkThatLogicIsSet();
457
14
  Debug("parser-sygus") << "Sygus : define sygus funs..." << std::endl;
458
14
  Debug("parser-sygus") << "Sygus : read inv-constraint..." << std::endl;
459
460
14
  if (names.size() != 4)
461
  {
462
    parseError(
463
        "Bad syntax for inv-constraint: expected 4 "
464
        "arguments.");
465
  }
466
467
28
  std::vector<api::Term> terms;
468
70
  for (const std::string& name : names)
469
  {
470
56
    if (!isDeclared(name))
471
    {
472
      std::stringstream ss;
473
      ss << "Function " << name << " in inv-constraint is not defined.";
474
      parseError(ss.str());
475
    }
476
477
56
    terms.push_back(getVariable(name));
478
  }
479
480
28
  return std::unique_ptr<Command>(new SygusInvConstraintCommand(terms));
481
}
482
483
5437
Command* Smt2::setLogic(std::string name, bool fromCommand)
484
{
485
5437
  if (fromCommand)
486
  {
487
5361
    if (d_seenSetLogic)
488
    {
489
2
      parseError("Only one set-logic is allowed.");
490
    }
491
5360
    d_seenSetLogic = true;
492
493
5360
    if (logicIsForced())
494
    {
495
      // If the logic is forced, we ignore all set-logic requests from commands.
496
5
      return new EmptyCommand();
497
    }
498
  }
499
500
5431
  d_logicSet = true;
501
5431
  d_logic = name;
502
503
  // if sygus is enabled, we must enable UF, datatypes, and integer arithmetic
504
5431
  if(sygus()) {
505
198
    if (!d_logic.isQuantified())
506
    {
507
      warning("Logics in sygus are assumed to contain quantifiers.");
508
      warning("Omit QF_ from the logic to avoid this warning.");
509
    }
510
  }
511
512
  // Core theory belongs to every logic
513
5431
  addCoreSymbols();
514
515
5431
  if(d_logic.isTheoryEnabled(theory::THEORY_UF)) {
516
2967
    Parser::addOperator(api::APPLY_UF);
517
518
2967
    if (!strictModeEnabled() && d_logic.hasCardinalityConstraints())
519
    {
520
22
      addOperator(api::CARDINALITY_CONSTRAINT, "fmf.card");
521
22
      addOperator(api::CARDINALITY_VALUE, "fmf.card.val");
522
    }
523
  }
524
525
5431
  if(d_logic.isTheoryEnabled(theory::THEORY_ARITH)) {
526
3769
    if(d_logic.areIntegersUsed()) {
527
3243
      defineType("Int", d_solver->getIntegerSort(), true, true);
528
3243
      addArithmeticOperators();
529
3243
      if (!strictModeEnabled() || !d_logic.isLinear())
530
      {
531
3239
        addOperator(api::INTS_DIVISION, "div");
532
3239
        addOperator(api::INTS_MODULUS, "mod");
533
3239
        addOperator(api::ABS, "abs");
534
      }
535
3243
      addIndexedOperator(api::DIVISIBLE, api::DIVISIBLE, "divisible");
536
    }
537
538
3769
    if (d_logic.areRealsUsed())
539
    {
540
2249
      defineType("Real", d_solver->getRealSort(), true, true);
541
2249
      addArithmeticOperators();
542
2249
      addOperator(api::DIVISION, "/");
543
2249
      if (!strictModeEnabled())
544
      {
545
2249
        addOperator(api::ABS, "abs");
546
      }
547
    }
548
549
3769
    if (d_logic.areIntegersUsed() && d_logic.areRealsUsed())
550
    {
551
1723
      addOperator(api::TO_INTEGER, "to_int");
552
1723
      addOperator(api::IS_INTEGER, "is_int");
553
1723
      addOperator(api::TO_REAL, "to_real");
554
    }
555
556
3769
    if (d_logic.areTranscendentalsUsed())
557
    {
558
1666
      defineVar("real.pi", d_solver->mkTerm(api::PI));
559
1666
      addTranscendentalOperators();
560
    }
561
3769
    if (!strictModeEnabled())
562
    {
563
      // integer version of AND
564
3765
      addIndexedOperator(api::IAND, api::IAND, "iand");
565
      // pow2
566
3765
      addOperator(api::POW2, "int.pow2");
567
    }
568
  }
569
570
5431
  if(d_logic.isTheoryEnabled(theory::THEORY_ARRAYS)) {
571
2200
    addOperator(api::SELECT, "select");
572
2200
    addOperator(api::STORE, "store");
573
2200
    addOperator(api::EQ_RANGE, "eqrange");
574
  }
575
576
5431
  if(d_logic.isTheoryEnabled(theory::THEORY_BV)) {
577
2812
    addBitvectorOperators();
578
579
8431
    if (!strictModeEnabled() && d_logic.isTheoryEnabled(theory::THEORY_ARITH)
580
4520
        && d_logic.areIntegersUsed())
581
    {
582
      // Conversions between bit-vectors and integers
583
1702
      addOperator(api::BITVECTOR_TO_NAT, "bv2nat");
584
1702
      addIndexedOperator(
585
          api::INT_TO_BITVECTOR, api::INT_TO_BITVECTOR, "int2bv");
586
    }
587
  }
588
589
5431
  if(d_logic.isTheoryEnabled(theory::THEORY_DATATYPES)) {
590
3260
    const std::vector<api::Sort> types;
591
1630
    defineType("Tuple", d_solver->mkTupleSort(types), true, true);
592
1630
    addDatatypesOperators();
593
  }
594
595
5431
  if(d_logic.isTheoryEnabled(theory::THEORY_SETS)) {
596
1650
    defineVar("emptyset", d_solver->mkEmptySet(d_solver->getNullSort()));
597
    // the Boolean sort is a placeholder here since we don't have type info
598
    // without type annotation
599
1650
    defineVar("univset", d_solver->mkUniverseSet(d_solver->getBooleanSort()));
600
601
1650
    addOperator(api::UNION, "union");
602
1650
    addOperator(api::INTERSECTION, "intersection");
603
1650
    addOperator(api::SETMINUS, "setminus");
604
1650
    addOperator(api::SUBSET, "subset");
605
1650
    addOperator(api::MEMBER, "member");
606
1650
    addOperator(api::SINGLETON, "singleton");
607
1650
    addOperator(api::INSERT, "insert");
608
1650
    addOperator(api::CARD, "card");
609
1650
    addOperator(api::COMPLEMENT, "complement");
610
1650
    addOperator(api::CHOOSE, "choose");
611
1650
    addOperator(api::IS_SINGLETON, "is_singleton");
612
1650
    addOperator(api::JOIN, "join");
613
1650
    addOperator(api::PRODUCT, "product");
614
1650
    addOperator(api::TRANSPOSE, "transpose");
615
1650
    addOperator(api::TCLOSURE, "tclosure");
616
  }
617
618
5431
  if (d_logic.isTheoryEnabled(theory::THEORY_BAGS))
619
  {
620
1568
    defineVar("emptybag", d_solver->mkEmptyBag(d_solver->getNullSort()));
621
1568
    addOperator(api::UNION_MAX, "union_max");
622
1568
    addOperator(api::UNION_DISJOINT, "union_disjoint");
623
1568
    addOperator(api::INTERSECTION_MIN, "intersection_min");
624
1568
    addOperator(api::DIFFERENCE_SUBTRACT, "difference_subtract");
625
1568
    addOperator(api::DIFFERENCE_REMOVE, "difference_remove");
626
1568
    addOperator(api::SUBBAG, "subbag");
627
1568
    addOperator(api::BAG_COUNT, "bag.count");
628
1568
    addOperator(api::DUPLICATE_REMOVAL, "duplicate_removal");
629
1568
    addOperator(api::MK_BAG, "bag");
630
1568
    addOperator(api::BAG_CARD, "bag.card");
631
1568
    addOperator(api::BAG_CHOOSE, "bag.choose");
632
1568
    addOperator(api::BAG_IS_SINGLETON, "bag.is_singleton");
633
1568
    addOperator(api::BAG_FROM_SET, "bag.from_set");
634
1568
    addOperator(api::BAG_TO_SET, "bag.to_set");
635
1568
    addOperator(api::BAG_MAP, "bag.map");
636
  }
637
5431
  if(d_logic.isTheoryEnabled(theory::THEORY_STRINGS)) {
638
2013
    defineType("String", d_solver->getStringSort(), true, true);
639
2013
    defineType("RegLan", d_solver->getRegExpSort(), true, true);
640
2013
    defineType("Int", d_solver->getIntegerSort(), true, true);
641
642
2013
    defineVar("re.none", d_solver->mkRegexpEmpty());
643
2013
    defineVar("re.allchar", d_solver->mkRegexpSigma());
644
645
    // Boolean is a placeholder
646
4026
    defineVar("seq.empty",
647
4026
              d_solver->mkEmptySequence(d_solver->getBooleanSort()));
648
649
2013
    addStringOperators();
650
  }
651
652
5431
  if(d_logic.isQuantified()) {
653
2354
    addQuantifiersOperators();
654
  }
655
656
5431
  if (d_logic.isTheoryEnabled(theory::THEORY_FP)) {
657
1627
    defineType("RoundingMode", d_solver->getRoundingModeSort(), true, true);
658
1627
    defineType("Float16", d_solver->mkFloatingPointSort(5, 11), true, true);
659
1627
    defineType("Float32", d_solver->mkFloatingPointSort(8, 24), true, true);
660
1627
    defineType("Float64", d_solver->mkFloatingPointSort(11, 53), true, true);
661
1627
    defineType("Float128", d_solver->mkFloatingPointSort(15, 113), true, true);
662
663
1627
    defineVar("RNE", d_solver->mkRoundingMode(api::ROUND_NEAREST_TIES_TO_EVEN));
664
3254
    defineVar("roundNearestTiesToEven",
665
3254
              d_solver->mkRoundingMode(api::ROUND_NEAREST_TIES_TO_EVEN));
666
1627
    defineVar("RNA", d_solver->mkRoundingMode(api::ROUND_NEAREST_TIES_TO_AWAY));
667
3254
    defineVar("roundNearestTiesToAway",
668
3254
              d_solver->mkRoundingMode(api::ROUND_NEAREST_TIES_TO_AWAY));
669
1627
    defineVar("RTP", d_solver->mkRoundingMode(api::ROUND_TOWARD_POSITIVE));
670
3254
    defineVar("roundTowardPositive",
671
3254
              d_solver->mkRoundingMode(api::ROUND_TOWARD_POSITIVE));
672
1627
    defineVar("RTN", d_solver->mkRoundingMode(api::ROUND_TOWARD_NEGATIVE));
673
3254
    defineVar("roundTowardNegative",
674
3254
              d_solver->mkRoundingMode(api::ROUND_TOWARD_NEGATIVE));
675
1627
    defineVar("RTZ", d_solver->mkRoundingMode(api::ROUND_TOWARD_ZERO));
676
3254
    defineVar("roundTowardZero",
677
3254
              d_solver->mkRoundingMode(api::ROUND_TOWARD_ZERO));
678
679
1627
    addFloatingPointOperators();
680
  }
681
682
5431
  if (d_logic.isTheoryEnabled(theory::THEORY_SEP)) {
683
    // the Boolean sort is a placeholder here since we don't have type info
684
    // without type annotation
685
1572
    defineVar("sep.nil", d_solver->mkSepNil(d_solver->getBooleanSort()));
686
687
1572
    addSepOperators();
688
  }
689
690
10862
  std::string logic = sygus() ? d_logic.getLogicString() : name;
691
5431
  if (!fromCommand)
692
  {
693
    // If not from a command, just set the logic directly. Notice this is
694
    // important since we do not want to enqueue a set-logic command and
695
    // fully initialize the underlying SmtEngine in the meantime before the
696
    // command has a chance to execute, which would lead to an error.
697
76
    d_solver->setLogic(logic);
698
76
    return nullptr;
699
  }
700
5355
  Command* cmd = new SetBenchmarkLogicCommand(logic);
701
5355
  return cmd;
702
} /* Smt2::setLogic() */
703
704
152
api::Grammar* Smt2::mkGrammar(const std::vector<api::Term>& boundVars,
705
                              const std::vector<api::Term>& ntSymbols)
706
{
707
152
  d_allocGrammars.emplace_back(
708
304
      new api::Grammar(d_solver->mkSygusGrammar(boundVars, ntSymbols)));
709
152
  return d_allocGrammars.back().get();
710
}
711
712
236648
bool Smt2::sygus() const
713
{
714
236648
  return d_solver->getOption("input-language") == "LANG_SYGUS_V2";
715
}
716
717
259173
void Smt2::checkThatLogicIsSet()
718
{
719
259173
  if (!logicIsSet())
720
  {
721
80
    if (strictModeEnabled())
722
    {
723
8
      parseError("set-logic must appear before this point.");
724
    }
725
    else
726
    {
727
      // the calls to setLogic below set the logic on the solver directly
728
76
      if (logicIsForced())
729
      {
730
8
        setLogic(getForcedLogic(), false);
731
      }
732
      else
733
      {
734
68
        warning("No set-logic command was given before this point.");
735
68
        warning("cvc5 will make all theories available.");
736
68
        warning(
737
            "Consider setting a stricter logic for (likely) better "
738
            "performance.");
739
68
        warning("To suppress this warning in the future use (set-logic ALL).");
740
741
68
        setLogic("ALL", false);
742
      }
743
    }
744
  }
745
259169
}
746
747
3698
void Smt2::checkLogicAllowsFreeSorts()
748
{
749
7396
  if (!d_logic.isTheoryEnabled(theory::THEORY_UF)
750
34
      && !d_logic.isTheoryEnabled(theory::THEORY_ARRAYS)
751
      && !d_logic.isTheoryEnabled(theory::THEORY_DATATYPES)
752
      && !d_logic.isTheoryEnabled(theory::THEORY_SETS)
753
3698
      && !d_logic.isTheoryEnabled(theory::THEORY_BAGS))
754
  {
755
    parseErrorLogic("Free sort symbols not allowed in ");
756
  }
757
3698
}
758
759
14414
void Smt2::checkLogicAllowsFunctions()
760
{
761
14414
  if (!d_logic.isTheoryEnabled(theory::THEORY_UF) && !isHoEnabled())
762
  {
763
    parseError(
764
        "Functions (of non-zero arity) cannot "
765
        "be declared in logic "
766
        + d_logic.getLogicString()
767
        + ". Try including UF or adding the prefix HO_.");
768
  }
769
14414
}
770
771
/* The include are managed in the lexer but called in the parser */
772
// Inspired by http://www.antlr3.org/api/C/interop.html
773
774
static bool newInputStream(const std::string& filename, pANTLR3_LEXER lexer) {
775
  Debug("parser") << "Including " << filename << std::endl;
776
  // Create a new input stream and take advantage of built in stream stacking
777
  // in C target runtime.
778
  //
779
  pANTLR3_INPUT_STREAM    in;
780
#ifdef CVC5_ANTLR3_OLD_INPUT_STREAM
781
  in = antlr3AsciiFileStreamNew((pANTLR3_UINT8) filename.c_str());
782
#else  /* CVC5_ANTLR3_OLD_INPUT_STREAM */
783
  in = antlr3FileStreamNew((pANTLR3_UINT8) filename.c_str(), ANTLR3_ENC_8BIT);
784
#endif /* CVC5_ANTLR3_OLD_INPUT_STREAM */
785
  if( in == NULL ) {
786
    Debug("parser") << "Can't open " << filename << std::endl;
787
    return false;
788
  }
789
  // Same thing as the predefined PUSHSTREAM(in);
790
  lexer->pushCharStream(lexer, in);
791
  // restart it
792
  //lexer->rec->state->tokenStartCharIndex      = -10;
793
  //lexer->emit(lexer);
794
795
  // Note that the input stream is not closed when it EOFs, I don't bother
796
  // to do it here, but it is up to you to track streams created like this
797
  // and destroy them when the whole parse session is complete. Remember that you
798
  // don't want to do this until all tokens have been manipulated all the way through
799
  // your tree parsers etc as the token does not store the text it just refers
800
  // back to the input stream and trying to get the text for it will abort if you
801
  // close the input stream too early.
802
803
  //TODO what said before
804
  return true;
805
}
806
807
void Smt2::includeFile(const std::string& filename) {
808
  // security for online version
809
  if(!canIncludeFile()) {
810
    parseError("include-file feature was disabled for this run.");
811
  }
812
813
  // Get the lexer
814
  AntlrInput* ai = static_cast<AntlrInput*>(getInput());
815
  pANTLR3_LEXER lexer = ai->getAntlr3Lexer();
816
  // get the name of the current stream "Does it work inside an include?"
817
  const std::string inputName = ai->getInputStreamName();
818
819
  // Find the directory of the current input file
820
  std::string path;
821
  size_t pos = inputName.rfind('/');
822
  if(pos != std::string::npos) {
823
    path = std::string(inputName, 0, pos + 1);
824
  }
825
  path.append(filename);
826
  if(!newInputStream(path, lexer)) {
827
    parseError("Couldn't open include file `" + path + "'");
828
  }
829
}
830
10809788
bool Smt2::isAbstractValue(const std::string& name)
831
{
832
20890261
  return name.length() >= 2 && name[0] == '@' && name[1] != '0'
833
10809800
         && name.find_first_not_of("0123456789", 1) == std::string::npos;
834
}
835
836
api::Term Smt2::mkAbstractValue(const std::string& name)
837
{
838
  Assert(isAbstractValue(name));
839
  // remove the '@'
840
  return d_solver->mkAbstractValue(name.substr(1));
841
}
842
843
355
void Smt2::parseOpApplyTypeAscription(ParseOp& p, api::Sort type)
844
{
845
710
  Debug("parser") << "parseOpApplyTypeAscription : " << p << " " << type
846
355
                  << std::endl;
847
  // (as const (Array T1 T2))
848
355
  if (p.d_kind == api::CONST_ARRAY)
849
  {
850
65
    if (!type.isArray())
851
    {
852
      std::stringstream ss;
853
      ss << "expected array constant term, but cast is not of array type"
854
         << std::endl
855
         << "cast type: " << type;
856
      parseError(ss.str());
857
    }
858
65
    p.d_type = type;
859
65
    return;
860
  }
861
290
  if (p.d_expr.isNull())
862
  {
863
580
    Trace("parser-overloading")
864
290
        << "Getting variable expression with name " << p.d_name << " and type "
865
290
        << type << std::endl;
866
    // get the variable expression for the type
867
290
    if (isDeclared(p.d_name, SYM_VARIABLE))
868
    {
869
290
      p.d_expr = getExpressionForNameAndType(p.d_name, type);
870
290
      p.d_name = std::string("");
871
    }
872
290
    if (p.d_expr.isNull())
873
    {
874
      std::stringstream ss;
875
      ss << "Could not resolve expression with name " << p.d_name
876
         << " and type " << type << std::endl;
877
      parseError(ss.str());
878
    }
879
  }
880
290
  Trace("parser-qid") << "Resolve ascription " << type << " on " << p.d_expr;
881
290
  Trace("parser-qid") << " " << p.d_expr.getKind() << " " << p.d_expr.getSort();
882
290
  Trace("parser-qid") << std::endl;
883
  // otherwise, we process the type ascription
884
290
  p.d_expr = applyTypeAscription(p.d_expr, type);
885
}
886
887
3485589
api::Term Smt2::parseOpToExpr(ParseOp& p)
888
{
889
3485589
  Debug("parser") << "parseOpToExpr: " << p << std::endl;
890
3485589
  api::Term expr;
891
3485589
  if (p.d_kind != api::NULL_EXPR || !p.d_type.isNull())
892
  {
893
    parseError(
894
        "Bad syntax for qualified identifier operator in term position.");
895
  }
896
3485589
  else if (!p.d_expr.isNull())
897
  {
898
288
    expr = p.d_expr;
899
  }
900
3485301
  else if (!isDeclared(p.d_name, SYM_VARIABLE))
901
  {
902
18
    std::stringstream ss;
903
9
    ss << "Symbol " << p.d_name << " is not declared.";
904
18
    parseError(ss.str());
905
  }
906
  else
907
  {
908
3485292
    expr = getExpressionForName(p.d_name);
909
  }
910
3485580
  Assert(!expr.isNull());
911
3485580
  return expr;
912
}
913
914
3295566
api::Term Smt2::applyParseOp(ParseOp& p, std::vector<api::Term>& args)
915
{
916
3295566
  bool isBuiltinOperator = false;
917
  // the builtin kind of the overall return expression
918
3295566
  api::Kind kind = api::NULL_EXPR;
919
  // First phase: process the operator
920
3295566
  if (Debug.isOn("parser"))
921
  {
922
    Debug("parser") << "applyParseOp: " << p << " to:" << std::endl;
923
    for (std::vector<api::Term>::iterator i = args.begin(); i != args.end();
924
         ++i)
925
    {
926
      Debug("parser") << "++ " << *i << std::endl;
927
    }
928
  }
929
6591132
  api::Op op;
930
3295566
  if (p.d_kind != api::NULL_EXPR)
931
  {
932
    // It is a special case, e.g. tupSel or array constant specification.
933
    // We have to wait until the arguments are parsed to resolve it.
934
  }
935
3295495
  else if (!p.d_expr.isNull())
936
  {
937
    // An explicit operator, e.g. an apply function
938
1424
    api::Kind fkind = getKindForFunction(p.d_expr);
939
1424
    if (fkind != api::UNDEFINED_KIND)
940
    {
941
      // Some operators may require a specific kind.
942
      // Testers are handled differently than other indexed operators,
943
      // since they require a kind.
944
1424
      kind = fkind;
945
2848
      Debug("parser") << "Got function kind " << kind << " for expression "
946
1424
                      << std::endl;
947
    }
948
1424
    args.insert(args.begin(), p.d_expr);
949
  }
950
3294071
  else if (!p.d_op.isNull())
951
  {
952
    // it was given an operator
953
76440
    op = p.d_op;
954
  }
955
  else
956
  {
957
3217631
    isBuiltinOperator = isOperatorEnabled(p.d_name);
958
3217631
    if (isBuiltinOperator)
959
    {
960
      // a builtin operator, convert to kind
961
2892390
      kind = getOperatorKind(p.d_name);
962
    }
963
    else
964
    {
965
      // A non-built-in function application, get the expression
966
325263
      checkDeclaration(p.d_name, CHECK_DECLARED, SYM_VARIABLE);
967
650438
      api::Term v = getVariable(p.d_name);
968
325219
      if (!v.isNull())
969
      {
970
325099
        checkFunctionLike(v);
971
325095
        kind = getKindForFunction(v);
972
325095
        args.insert(args.begin(), v);
973
      }
974
      else
975
      {
976
        // Overloaded symbol?
977
        // Could not find the expression. It may be an overloaded symbol,
978
        // in which case we may find it after knowing the types of its
979
        // arguments.
980
244
        std::vector<api::Sort> argTypes;
981
268
        for (std::vector<api::Term>::iterator i = args.begin(); i != args.end();
982
             ++i)
983
        {
984
146
          argTypes.push_back((*i).getSort());
985
        }
986
244
        api::Term fop = getOverloadedFunctionForTypes(p.d_name, argTypes);
987
122
        if (!fop.isNull())
988
        {
989
122
          checkFunctionLike(fop);
990
122
          kind = getKindForFunction(fop);
991
122
          args.insert(args.begin(), fop);
992
        }
993
        else
994
        {
995
          parseError(
996
              "Cannot find unambiguous overloaded function for argument "
997
              "types.");
998
        }
999
      }
1000
    }
1001
  }
1002
  // handle special cases
1003
3295542
  if (p.d_kind == api::CONST_ARRAY && !p.d_type.isNull())
1004
  {
1005
65
    if (args.size() != 1)
1006
    {
1007
      parseError("Too many arguments to array constant.");
1008
    }
1009
130
    api::Term constVal = args[0];
1010
1011
    // To parse array constants taking reals whose values are specified by
1012
    // rationals, e.g. ((as const (Array Int Real)) (/ 1 3)), we must handle
1013
    // the fact that (/ 1 3) is the division of constants 1 and 3, and not
1014
    // the resulting constant rational value. Thus, we must construct the
1015
    // resulting rational here. This also is applied for integral real values
1016
    // like 5.0 which are converted to (/ 5 1) to distinguish them from
1017
    // integer constants. We must ensure numerator and denominator are
1018
    // constant and the denominator is non-zero.
1019
65
    if (constVal.getKind() == api::DIVISION)
1020
    {
1021
4
      std::stringstream sdiv;
1022
2
      sdiv << constVal[0] << "/" << constVal[1];
1023
2
      constVal = d_solver->mkReal(sdiv.str());
1024
    }
1025
1026
65
    if (!p.d_type.getArrayElementSort().isComparableTo(constVal.getSort()))
1027
    {
1028
      std::stringstream ss;
1029
      ss << "type mismatch inside array constant term:" << std::endl
1030
         << "array type:          " << p.d_type << std::endl
1031
         << "expected const type: " << p.d_type.getArrayElementSort()
1032
         << std::endl
1033
         << "computed const type: " << constVal.getSort();
1034
      parseError(ss.str());
1035
    }
1036
130
    api::Term ret = d_solver->mkConstArray(p.d_type, constVal);
1037
65
    Debug("parser") << "applyParseOp: return store all " << ret << std::endl;
1038
65
    return ret;
1039
  }
1040
3295477
  else if (p.d_kind == api::APPLY_SELECTOR && !p.d_expr.isNull())
1041
  {
1042
    // tuple selector case
1043
4
    if (!p.d_expr.isUInt64Value())
1044
    {
1045
      parseError("index of tupSel is larger than size of uint64_t");
1046
    }
1047
4
    uint64_t n = p.d_expr.getUInt64Value();
1048
4
    if (args.size() != 1)
1049
    {
1050
      parseError("tupSel should only be applied to one tuple argument");
1051
    }
1052
8
    api::Sort t = args[0].getSort();
1053
4
    if (!t.isTuple())
1054
    {
1055
      parseError("tupSel applied to non-tuple");
1056
    }
1057
4
    size_t length = t.getTupleLength();
1058
4
    if (n >= length)
1059
    {
1060
      std::stringstream ss;
1061
      ss << "tuple is of length " << length << "; cannot access index " << n;
1062
      parseError(ss.str());
1063
    }
1064
8
    const api::Datatype& dt = t.getDatatype();
1065
4
    api::Term ret = d_solver->mkTerm(
1066
8
        api::APPLY_SELECTOR, dt[0][n].getSelectorTerm(), args[0]);
1067
4
    Debug("parser") << "applyParseOp: return selector " << ret << std::endl;
1068
4
    return ret;
1069
  }
1070
3295473
  else if (p.d_kind == api::TUPLE_PROJECT)
1071
  {
1072
4
    api::Term ret = d_solver->mkTerm(p.d_op, args[0]);
1073
2
    Debug("parser") << "applyParseOp: return projection " << ret << std::endl;
1074
2
    return ret;
1075
  }
1076
3295471
  else if (p.d_kind != api::NULL_EXPR)
1077
  {
1078
    // it should not have an expression or type specified at this point
1079
    if (!p.d_expr.isNull() || !p.d_type.isNull())
1080
    {
1081
      std::stringstream ss;
1082
      ss << "Could not process parsed qualified identifier kind " << p.d_kind;
1083
      parseError(ss.str());
1084
    }
1085
    // otherwise it is a simple application
1086
    kind = p.d_kind;
1087
  }
1088
3295471
  else if (isBuiltinOperator)
1089
  {
1090
2892390
    if (!isHoEnabled() && (kind == api::EQUAL || kind == api::DISTINCT))
1091
    {
1092
      // need hol if these operators are applied over function args
1093
719597
      for (std::vector<api::Term>::iterator i = args.begin(); i != args.end();
1094
           ++i)
1095
      {
1096
481737
        if ((*i).getSort().isFunction())
1097
        {
1098
          parseError(
1099
              "Cannot apply equality to functions unless logic is prefixed by "
1100
              "HO_.");
1101
        }
1102
      }
1103
    }
1104
8677083
    if (!strictModeEnabled() && (kind == api::AND || kind == api::OR)
1105
3726175
        && args.size() == 1)
1106
    {
1107
      // Unary AND/OR can be replaced with the argument.
1108
96
      Debug("parser") << "applyParseOp: return unary " << args[0] << std::endl;
1109
96
      return args[0];
1110
    }
1111
2892294
    else if (kind == api::MINUS && args.size() == 1)
1112
    {
1113
240762
      api::Term ret = d_solver->mkTerm(api::UMINUS, args[0]);
1114
120381
      Debug("parser") << "applyParseOp: return uminus " << ret << std::endl;
1115
120381
      return ret;
1116
    }
1117
2771913
    if (kind == api::EQ_RANGE && d_solver->getOption("arrays-exp") != "true")
1118
    {
1119
      parseError(
1120
          "eqrange predicate requires option --arrays-exp to be enabled.");
1121
    }
1122
2771913
    if (kind == api::SINGLETON && args.size() == 1)
1123
    {
1124
386
      api::Term ret = d_solver->mkTerm(api::SINGLETON, args[0]);
1125
193
      Debug("parser") << "applyParseOp: return singleton " << ret << std::endl;
1126
193
      return ret;
1127
    }
1128
5543440
    api::Term ret = d_solver->mkTerm(kind, args);
1129
5543440
    Debug("parser") << "applyParseOp: return default builtin " << ret
1130
2771720
                    << std::endl;
1131
2771720
    return ret;
1132
  }
1133
1134
403081
  if (args.size() >= 2)
1135
  {
1136
    // may be partially applied function, in this case we use HO_APPLY
1137
653249
    api::Sort argt = args[0].getSort();
1138
326778
    if (argt.isFunction())
1139
    {
1140
310287
      unsigned arity = argt.getFunctionArity();
1141
310287
      if (args.size() - 1 < arity)
1142
      {
1143
307
        if (!isHoEnabled())
1144
        {
1145
          parseError(
1146
              "Cannot partially apply functions unless logic is prefixed by "
1147
              "HO_.");
1148
        }
1149
307
        Debug("parser") << "Partial application of " << args[0];
1150
307
        Debug("parser") << " : #argTypes = " << arity;
1151
307
        Debug("parser") << ", #args = " << args.size() - 1 << std::endl;
1152
614
        api::Term ret = d_solver->mkTerm(api::HO_APPLY, args);
1153
614
        Debug("parser") << "applyParseOp: return curry higher order " << ret
1154
307
                        << std::endl;
1155
        // must curry the partial application
1156
307
        return ret;
1157
      }
1158
    }
1159
  }
1160
402774
  if (!op.isNull())
1161
  {
1162
152879
    api::Term ret = d_solver->mkTerm(op, args);
1163
76439
    Debug("parser") << "applyParseOp: return op : " << ret << std::endl;
1164
76439
    return ret;
1165
  }
1166
326334
  if (kind == api::NULL_EXPR)
1167
  {
1168
    // should never happen in the new API
1169
    parseError("do not know how to process parse op");
1170
  }
1171
652668
  Debug("parser") << "Try default term construction for kind " << kind
1172
326334
                  << " #args = " << args.size() << "..." << std::endl;
1173
652667
  api::Term ret = d_solver->mkTerm(kind, args);
1174
326333
  Debug("parser") << "applyParseOp: return : " << ret << std::endl;
1175
326333
  return ret;
1176
}
1177
1178
4673
void Smt2::notifyNamedExpression(api::Term& expr, std::string name)
1179
{
1180
4673
  checkUserSymbol(name);
1181
  // remember the expression name in the symbol manager
1182
9346
  if (getSymbolManager()->setExpressionName(expr, name, false)
1183
4673
      == NamingResult::ERROR_IN_BINDER)
1184
  {
1185
2
    parseError(
1186
        "Cannot name a term in a binder (e.g., quantifiers, definitions)");
1187
  }
1188
  // define the variable
1189
4672
  defineVar(name, expr);
1190
  // set the last named term, which ensures that we catch when assertions are
1191
  // named
1192
4672
  setLastNamedTerm(expr, name);
1193
4672
}
1194
1195
api::Term Smt2::mkAnd(const std::vector<api::Term>& es)
1196
{
1197
  if (es.size() == 0)
1198
  {
1199
    return d_solver->mkTrue();
1200
  }
1201
  else if (es.size() == 1)
1202
  {
1203
    return es[0];
1204
  }
1205
  else
1206
  {
1207
    return d_solver->mkTerm(api::AND, es);
1208
  }
1209
}
1210
1211
}  // namespace parser
1212
29562
}  // namespace cvc5