1 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2
3 /*
4 * Main authors:
5 * Guido Tack <guido.tack@monash.edu>
6 */
7
8 /* This Source Code Form is subject to the terms of the Mozilla Public
9 * License, v. 2.0. If a copy of the MPL was not distributed with this
10 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
11
12 #include <minizinc/flat_exp.hh>
13
14 namespace MiniZinc {
15
flatten_unop(EnvI & env,const Ctx & ctx,Expression * e,VarDecl * r,VarDecl * b)16 EE flatten_unop(EnvI& env, const Ctx& ctx, Expression* e, VarDecl* r, VarDecl* b) {
17 CallStackItem _csi(env, e);
18 EE ret;
19 UnOp* uo = e->cast<UnOp>();
20
21 bool isBuiltin = uo->decl() == nullptr || uo->decl()->e() == nullptr;
22
23 if (isBuiltin) {
24 switch (uo->op()) {
25 case UOT_NOT: {
26 Ctx nctx = ctx;
27 nctx.b = -nctx.b;
28 nctx.neg = !nctx.neg;
29 ret = flat_exp(env, nctx, uo->e(), r, b);
30 } break;
31 case UOT_PLUS:
32 ret = flat_exp(env, ctx, uo->e(), r, b);
33 break;
34 case UOT_MINUS: {
35 GC::lock();
36 if (UnOp* uo_inner = uo->e()->dynamicCast<UnOp>()) {
37 if (uo_inner->op() == UOT_MINUS) {
38 ret = flat_exp(env, ctx, uo_inner->e(), r, b);
39 break;
40 }
41 }
42 Expression* zero;
43 if (uo->e()->type().bt() == Type::BT_INT) {
44 zero = IntLit::a(0);
45 } else {
46 zero = FloatLit::a(0.0);
47 }
48 auto* bo = new BinOp(Location().introduce(), zero, BOT_MINUS, uo->e());
49 bo->type(uo->type());
50 KeepAlive ka(bo);
51 GC::unlock();
52 ret = flat_exp(env, ctx, ka(), r, b);
53 } break;
54 default:
55 break;
56 }
57 } else {
58 GC::lock();
59 Call* c = new Call(uo->loc().introduce(), uo->opToString(), {uo->e()});
60 c->decl(env.model->matchFn(env, c, false));
61 c->type(uo->type());
62 KeepAlive ka(c);
63 GC::unlock();
64 ret = flat_exp(env, ctx, c, r, b);
65 }
66
67 return ret;
68 }
69 } // namespace MiniZinc
70