22 #include <boost/format.hpp> 31 using namespace Constants;
33 struct type_t::child_t
39 struct type_t::type_data
44 std::vector<child_t> children;
49 data = std::make_shared<type_data>();
52 data->children.resize(size);
68 return data == type.data;
73 return data != type.data;
78 return data < type.data;
84 return data->children.size();
90 return data->children[i].child;
96 return data->children[i].child;
102 return data->children[i].label;
107 assert(isRecord() || isProcess());
109 size_t n = type.
size();
110 for (
size_t i = 0; i < n; i++)
122 return unknown() ?
UNKNOWN : data->kind;
173 return data == NULL || data->kind ==
UNKNOWN;
186 return (getKind() == kind)
187 || (isPrefix() &&
get(0).is(kind))
188 || (getKind() ==
RANGE &&
get(0).is(kind))
189 || (getKind() ==
REF &&
get(0).is(kind))
190 || (getKind() ==
LABEL &&
get(0).is(kind));
196 if (getKind() ==
REF || getKind() ==
LABEL)
198 return get(0).getSub();
202 return get(0).getSub().createPrefix(getKind());
212 assert(isRecord() || isProcess());
213 if (getKind() ==
REF || getKind() ==
LABEL)
215 return get(0).getSub(i);
219 return get(0).getSub(i).createPrefix(getKind());
229 if (isPrefix() || getKind() ==
REF || getKind() ==
LABEL)
231 return get(0).getArraySize();
235 assert(getKind() ==
ARRAY);
242 if (isPrefix() || getKind() ==
REF || getKind() ==
LABEL)
244 return get(0).getRecordSize();
248 assert(getKind() ==
RECORD);
255 if (isPrefix() || getKind() ==
REF || getKind() ==
LABEL)
257 return get(0).getRecordLabel(i);
269 if (getKind() ==
RANGE)
271 return std::make_pair(
get(1).getExpression(),
get(2).getExpression());
275 return get(0).getRange();
287 if (isPrefix() || getKind() ==
RANGE || getKind() ==
REF || getKind() ==
LABEL)
289 return get(0).strip();
309 type_t type(getKind(), getPosition(), size());
310 type.data->expr = getExpression();
311 for (
size_t i = 0; i < size(); ++i)
313 type.data->children[i].child =
get(i).rename(from, to);
314 type.data->children[i].label = getLabel(i);
316 if (getKind() ==
LABEL && getLabel(0) == from)
318 type.data->children[0].label = to;
326 for (
size_t i = 0; i < size(); i++)
328 type.data->children[i].label = getLabel(i);
329 type.data->children[i].child =
get(i).subst(symbol, expr);
331 if (!data->expr.empty())
333 type.data->expr = data->expr.subst(symbol, expr);
340 return data->position;
359 bool type_t::isProbability()
const 387 for (
size_t i = 0; i < size(); i++)
389 if (!
get(i).isConstant())
396 return size() > 0 &&
get(0).isConstant();
412 for (
size_t i = 0; i < size(); i++)
414 if (!
get(i).isNonConstant())
421 return size() == 0 ||
get(0).isNonConstant();
429 t.data->children[0].child = type;
432 t[1].data->expr = lower;
433 t[2].data->expr = upper;
438 const vector<string> &labels,
441 assert(types.size() == labels.size());
443 for (
size_t i = 0; i < types.size(); i++)
445 type.data->children[i].child = types[i];
446 type.data->children[i].label = labels[i];
452 const std::vector<type_t> ¶meters,
453 const std::vector<std::string> &labels,
456 assert(parameters.size() == labels.size());
458 type.data->children[0].child = ret;
459 for (
size_t i = 0; i < parameters.size(); i++)
461 type.data->children[i + 1].child = parameters[i];
462 type.data->children[i + 1].label = labels[i];
470 type.data->children[0].child = sub;
471 type.data->children[1].child = size;
478 t.data->children[0].label = label;
479 t.data->children[0].child = type;
486 for (
size_t i = 0; i < parameters.
getSize(); i++)
488 type.data->children[i].child = parameters[i].getType();
489 type.data->children[i].label = parameters[i].getName();
497 for (
size_t i = 0; i < parameters.
getSize(); i++)
499 type.data->children[i].child = parameters[i].getType();
500 type.data->children[i].label = parameters[i].getName();
508 for (
size_t i = 0; i < frame.
getSize(); i++)
510 type.data->children[i].child = frame[i].getType();
511 type.data->children[i].label = frame[i].getName();
519 for (
size_t i = 0; i < instance.
size(); i++)
521 type.data->children[i].child = instance[i];
522 type.data->children[i].label = instance.
getLabel(i);
529 return type_t(kind, pos, 0);
534 type_t type(kind, pos, 1);
535 type.data->children[0].child = *
this;
542 type.data->children[0].child = *
this;
543 type.data->children[0].label = label;
549 auto kind = std::string();
556 if (!data->expr.empty())
558 return string(
"\"") + data->expr.toString() +
"\"";
684 kind =
"branchpoint";
693 kind =
"instance line";
705 kind =
"LSC instance";
711 kind = (boost::format(
"type(%1%)") % getKind()).str();
715 auto str = std::string(
"(");
718 for (uint32_t i = 0; i < size(); i++)
721 if (!getLabel(i).empty())
726 str +=
get(i).toString();
736 auto kind = std::string();
740 auto typeDef =
false;
747 if (!data->expr.empty())
870 kind = (boost::format(
"type(%1%)") % getKind()).str();
874 auto str = std::string();
878 str +=
get(0).toDeclarationString();
879 if (getRange().first.getValue() != -32768 ||
880 getRange().second.getValue() != 32767)
883 str += getRange().first.toString();
885 str += getRange().second.toString();
887 str +=
get(1).toDeclarationString();
892 str +=
get(0).toDeclarationString();
894 str += getArraySize().getRange().second.get(0).toString();
905 str +=
get(0).toDeclarationString();
911 for (uint32_t i = 0; i < size(); i++)
914 if (!getLabel(i).empty())
919 str +=
get(i).toDeclarationString();
bool isPrefix() const
Returns false for non-prefix types and true otherwise.
const type_t & operator=(const type_t &)
Assignment operator.
static bool isInvariant(expression_t expr)
static bool isIntegral(expression_t expr)
bool unknown() const
Returns true if this is null-type or of kind UNKNOWN.
bool isIntegral() const
Returns true if this is a boolean or integer.
uint32_t getSize() const
Returns the number of symbols in this frame.
bool isInvariant() const
Returns true if this is an invariant, boolean or integer.
bool operator==(const type_t &) const
Equality operator.
static type_t createRange(type_t, expression_t, expression_t, position_t=position_t())
static type_t createInstance(frame_t, position_t=position_t())
Creates a new instance type.
type_t createPrefix(Constants::kind_t kind, position_t=position_t()) const
Creates a new type by adding a prefix to it.
std::string toDeclarationString() const
static type_t createPrimitive(Constants::kind_t, position_t=position_t())
Create a primitive type.
static type_t createLscInstance(frame_t, position_t=position_t())
Creates a new lsc instance type.
std::string toString() const
Generates string representation of the type.
std::pair< expression_t, expression_t > getRange() const
Returns the range of a RANGE type.
type_t rename(const std::string &from, const std::string &to) const
Replaces any LABEL labeled from occuring in the type with a LABEL to.
position_t getPosition() const
Returns the position of the type in the input file.
const std::string & getLabel(uint32_t) const
Returns the i'th label.
std::ostream & operator<<(std::ostream &os, const SignalFlow::strs_t &s)
bool operator<(const type_t &) const
Less-than operator.
bool isFormula() const
Returns true if this is a formula, constraint, guard, invariant, boolean or integer.
type_t strip() const
Removes any leading prefixes, RANGE, REF and LABEL types and returns the result.
static type_t createFunction(type_t, const std::vector< type_t > &, const std::vector< std::string > &, position_t=position_t())
Creates a new function type.
bool isConstant() const
Returns true if and only if all elements of the type are constant.
bool isConstraint() const
Returns true if this is a constraint, guard, invariant, boolean or integer.
type_t stripArray() const
Removes any leading prefixes, RANGE, REF, LABEL and ARRAY types and returns the result.
type_t getSub() const
Returns the element type of an array.
static string symbol(const char *str)
Extracts the alpha-numerical symbol used for variable/type identifiers.
static bool isGuard(expression_t expr)
expression_t getExpression() const
Returns the expression associated with the type.
static bool isInteger(expression_t expr)
bool isNonConstant() const
Returns true if and only if all elements of the type are not constant.
type_t subst(symbol_t symbol, expression_t expr) const
Substitutes any occurence of symbol in any expression in the type (expressions that occur as ranges e...
static type_t createTypeDef(const std::string &, type_t, position_t=position_t())
Creates a new type definition.
static type_t createProcessSet(type_t instance, position_t=position_t())
Creates a new processset type.
const type_t operator[](uint32_t) const
Returns the i'th child.
static bool isConstraint(expression_t expr)
bool isGuard() const
Returns true if this is a guard, invariant, boolean or integer.
type_t()
Default constructor.
const type_t get(uint32_t) const
Returns the i'th child.
size_t size() const
Returns the number of children.
int32_t findIndexOf(const std::string &) const
Returns the index of the record or process field with the given label.
std::string getRecordLabel(size_t i) const
Returns the label of the 'th field of a record.
type_t createLabel(const std::string &, position_t=position_t()) const
Creates a LABEL.
static type_t createRecord(const std::vector< type_t > &, const std::vector< std::string > &, position_t=position_t())
Creates a new record type.
bool operator!=(const type_t &) const
Inequality operator.
A reference to an expression.
type_t getArraySize() const
Returns the size of an array (this is itself a type).
bool is(Constants::kind_t kind) const
Returns true if the type has kind kind or if type is a prefix, RANGE or REF type and the getChild()...
static type_t createArray(type_t sub, type_t size, position_t=position_t())
Creates an array type.
static type_t createProcess(frame_t, position_t=position_t())
Creates a new process type.
Constants::kind_t getKind() const
Returns the kind of type object.
size_t getRecordSize() const
Returns the number of fields of a record.