File: flatten_par.cpp

package info (click to toggle)
minizinc 2.9.3%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 17,620 kB
  • sloc: cpp: 74,682; ansic: 8,541; python: 3,322; sh: 79; makefile: 13
file content (135 lines) | stat: -rw-r--r-- 4,753 bytes parent folder | download | duplicates (2)
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
/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */

/*
 *  Main authors:
 *     Guido Tack <guido.tack@monash.edu>
 */

/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include <minizinc/flat_exp.hh>

namespace MiniZinc {

EE flatten_par(EnvI& env, const Ctx& ctx, Expression* e, VarDecl* r, VarDecl* b) {
  EE ret;
  if (Expression::type(e).cv()) {
    CallStackItem _csi(env, e);
    Ctx nctx;
    nctx.b = ctx.b == C_ROOT ? C_ROOT : C_MIX;

    try {
      KeepAlive ka = flat_cv_exp(env, nctx, e);
      ret.r = bind(env, ctx, r, ka());
      ret.b = bind(env, Ctx(), b, env.constants.literalTrue);
    } catch (ResultUndefinedError&) {
      if (Expression::type(e).isbool()) {
        ret.r = bind(env, ctx, r, env.constants.literalFalse);
        ret.b = bind(env, Ctx(), b, env.constants.literalTrue);
      } else {
        ret.r = create_dummy_value(env, Expression::type(e));
        ret.b = bind(env, Ctx(), b, env.constants.literalFalse);
      }
    }
    return ret;
  }
  if (Expression::type(e).dim() > 0) {
    auto* ident = Expression::dynamicCast<Id>(e);
    if (ident != nullptr) {
      Expression* e_val = follow_id_to_decl(ident);
      if (Expression::isa<Id>(e_val)) {
        ident = Expression::cast<Id>(e_val);
      } else if (Expression::isa<VarDecl>(e_val)) {
        ident = Expression::cast<VarDecl>(e_val)->id();
      }
      if (ident->decl()->flat() == nullptr || ident->decl()->toplevel()) {
        CallStackItem _csi(env, e);
        VarDecl* vd = ident->decl()->flat();
        if (vd == nullptr) {
          EE flat_ident = flat_exp(env, Ctx(), ident->decl(), nullptr, env.constants.varTrue);
          vd = Expression::cast<Id>(flat_ident.r())->decl();
          ident->decl()->flat(vd);
          auto* al = Expression::cast<ArrayLit>(follow_id(vd->id()));
          if (al->empty()) {
            if (r == nullptr) {
              ret.r = al;
            } else {
              ret.r = bind(env, ctx, r, al);
            }
            ret.b = bind(env, Ctx(), b, env.constants.literalTrue);
            return ret;
          }
        }
        ret.r = bind(env, ctx, r, ident->decl()->flat()->id());
        ret.b = bind(env, Ctx(), b, env.constants.literalTrue);
        return ret;
      }
    }
    auto it = env.cseMapFind(e);
    if (it != env.cseMapEnd()) {
      ret.r = bind(env, ctx, r, Expression::cast<VarDecl>(it->second.r)->id());
      ret.b = bind(env, Ctx(), b, env.constants.literalTrue);
      return ret;
    }
    GCLock lock;
    ArrayLit* al = nullptr;
    try {
      al = Expression::cast<ArrayLit>(follow_id(eval_par(env, e)));
    } catch (ResultUndefinedError&) {
      ret.r = create_dummy_value(env, Expression::type(e));
      ret.b = bind(env, Ctx(), b, env.constants.literalFalse);
      return ret;
    }
    CallStackItem _csi(env, e);
    if (al->empty() || ((r != nullptr) && r->e() == nullptr)) {
      if (r == nullptr) {
        ret.r = al;
      } else {
        ret.r = bind(env, ctx, r, al);
      }
      ret.b = bind(env, Ctx(), b, env.constants.literalTrue);
      return ret;
    }
    it = env.cseMapFind(al);
    if (it != env.cseMapEnd()) {
      ret.r = bind(env, ctx, r, Expression::cast<VarDecl>(it->second.r)->id());
      ret.b = bind(env, Ctx(), b, env.constants.literalTrue);
      return ret;
    }
    std::vector<TypeInst*> ranges(al->dims());
    for (unsigned int i = 0; i < ranges.size(); i++) {
      ranges[i] =
          new TypeInst(Expression::loc(e), Type(),
                       new SetLit(Location().introduce(), IntSetVal::a(al->min(i), al->max(i))));
    }
    ASTExprVec<TypeInst> ranges_v(ranges);
    auto* ti = new TypeInst(Expression::loc(e), al->type(), ranges_v, nullptr);
    VarDecl* vd = new_vardecl(env, ctx, ti, nullptr, nullptr, al);
    EE ee(vd, nullptr);
    env.cseMapInsert(al, ee);
    env.cseMapInsert(vd->e(), ee);

    ret.r = bind(env, ctx, r, vd->id());
    ret.b = bind(env, Ctx(), b, env.constants.literalTrue);
    return ret;
  }
  GCLock lock;
  try {
    auto* result = eval_par(env, e);
    if (Expression::type(result) == Type::parbool()) {
      if (ctx.b == C_ROOT && r == env.constants.varTrue && result == env.constants.boollit(false)) {
        env.fail("expression evaluated to false", Expression::loc(e));
      }
    }
    ret.r = bind(env, ctx, r, result);
    ret.b = bind(env, Ctx(), b, env.constants.literalTrue);
  } catch (ResultUndefinedError&) {
    ret.r = create_dummy_value(env, Expression::type(e));
    ret.b = bind(env, Ctx(), b, env.constants.literalFalse);
  }
  return ret;
}

}  // namespace MiniZinc