this repo has no description
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
14namespace MiniZinc {
15
16CallArgItem::CallArgItem(EnvI& env0) : env(env0) {
17 env.idStack.push_back(static_cast<int>(env.callStack.size()));
18}
19CallArgItem::~CallArgItem(void) { env.idStack.pop_back(); }
20
21Expression* createDummyValue(EnvI& env, const Type& t) {
22 if (t.dim() > 0) {
23 Expression* ret = new ArrayLit(Location().introduce(), std::vector<Expression*>());
24 Type ret_t = t;
25 ret_t.ti(Type::TI_PAR);
26 ret->type(ret_t);
27 return ret;
28 }
29 if (t.st() == Type::ST_SET) {
30 Expression* ret = new SetLit(Location().introduce(), std::vector<Expression*>());
31 Type ret_t = t;
32 ret_t.ti(Type::TI_PAR);
33 ret->type(ret_t);
34 return ret;
35 }
36 switch (t.bt()) {
37 case Type::BT_INT:
38 return IntLit::a(0);
39 case Type::BT_BOOL:
40 return constants().boollit(false);
41 case Type::BT_FLOAT:
42 return FloatLit::a(0);
43 case Type::BT_STRING:
44 return new StringLit(Location().introduce(), "");
45 case Type::BT_ANN:
46 return constants().ann.promise_total;
47 default:
48 return NULL;
49 }
50}
51
52EE flatten_error(EnvI& env, Ctx ctx, Expression* e, VarDecl* r, VarDecl* b) {
53 throw InternalError("invalid expression encountered during compilation");
54}
55
56#ifndef NDEBUG
57void mzn_break_here(Expression* e) { std::cerr << "% mzn_break_here: " << *e << "\n"; }
58#endif
59
60typedef EE (*ExprFlattener)(EnvI& env, Ctx ctx, Expression* e, VarDecl* r, VarDecl* b);
61
62EE flatten_setlit(EnvI& env, Ctx ctx, Expression* e, VarDecl* r, VarDecl* b);
63EE flatten_id(EnvI& env, Ctx ctx, Expression* e, VarDecl* r, VarDecl* b);
64EE flatten_anon(EnvI& env, Ctx ctx, Expression* e, VarDecl* r, VarDecl* b);
65EE flatten_arraylit(EnvI& env, Ctx ctx, Expression* e, VarDecl* r, VarDecl* b);
66EE flatten_arrayaccess(EnvI& env, Ctx ctx, Expression* e, VarDecl* r, VarDecl* b);
67EE flatten_comp(EnvI& env, Ctx ctx, Expression* e, VarDecl* r, VarDecl* b);
68EE flatten_ite(EnvI& env, Ctx ctx, Expression* e, VarDecl* r, VarDecl* b);
69EE flatten_binop(EnvI& env, Ctx ctx, Expression* e, VarDecl* r, VarDecl* b);
70EE flatten_unop(EnvI& env, Ctx ctx, Expression* e, VarDecl* r, VarDecl* b);
71EE flatten_call(EnvI& env, Ctx ctx, Expression* e, VarDecl* r, VarDecl* b);
72EE flatten_vardecl(EnvI& env, Ctx ctx, Expression* e, VarDecl* r, VarDecl* b);
73EE flatten_let(EnvI& env, Ctx ctx, Expression* e, VarDecl* r, VarDecl* b);
74EE flatten_par(EnvI& env, Ctx ctx, Expression* e, VarDecl* r, VarDecl* b);
75EE flatten_error(EnvI& env, Ctx ctx, Expression* e, VarDecl* r, VarDecl* b);
76
77EE flat_exp(EnvI& env, Ctx ctx, Expression* e, VarDecl* r, VarDecl* b) {
78 if (e == NULL) return EE();
79
80#ifndef NDEBUG
81 Annotation& e_ann = e->ann();
82 if (e_ann.contains(constants().ann.mzn_break_here)) mzn_break_here(e);
83#endif
84
85 assert(!e->type().isunknown());
86
87 static const ExprFlattener flattener_dispatch[] = {
88 &flatten_par, // par expressions
89 &flatten_error, // E_INTLIT
90 &flatten_error, // E_FLOATLIT
91 &flatten_setlit, // E_SETLIT
92 &flatten_error, // E_BOOLLIT
93 &flatten_error, // E_STRINGLIT
94 &flatten_id, // E_ID
95 &flatten_anon, // E_ANON
96 &flatten_arraylit, // E_ARRAYLIT
97 &flatten_arrayaccess, // E_ARRAYACCESS
98 &flatten_comp, // E_COMP
99 &flatten_ite, // E_ITE
100 &flatten_binop, // E_BINOP
101 &flatten_unop, // E_UNOP
102 &flatten_call, // E_CALL
103 &flatten_vardecl, // E_VARDECL
104 &flatten_let, // E_LET
105 &flatten_error, // E_TI
106 &flatten_error // E_TIID
107 };
108
109 int dispatch =
110 (e->type().ispar() && !e->isa<Let>() && !e->isa<VarDecl>() && e->type().bt() != Type::BT_ANN)
111 ? 0
112 : e->eid() - Expression::E_INTLIT + 1;
113
114 return flattener_dispatch[dispatch](env, ctx, e, r, b);
115}
116
117} // namespace MiniZinc