void cpp_typecheckt::typecheck_enum_body(symbolt &enum_symbol) { typet &type=enum_symbol.type; exprt &body=static_cast<exprt &>(type.add(ID_body)); irept::subt &components=body.get_sub(); typet enum_type(ID_symbol); enum_type.set(ID_identifier, enum_symbol.name); mp_integer i=0; Forall_irep(it, components) { const irep_idt &name=it->get(ID_name); if(it->find(ID_value).is_not_nil()) { exprt &value=static_cast<exprt &>(it->add(ID_value)); typecheck_expr(value); make_constant_index(value); if(to_integer(value, i)) throw "failed to produce integer for enum"; } exprt final_value(ID_constant, enum_type); final_value.set(ID_value, integer2string(i)); symbolt symbol; symbol.name=id2string(enum_symbol.name)+"::"+id2string(name); symbol.base_name=name; symbol.value.swap(final_value); symbol.location=static_cast<const locationt &>(it->find(ID_C_location)); symbol.mode=ID_cpp; symbol.module=module; symbol.type=enum_type; symbol.is_type=false; symbol.is_macro=true; symbolt *new_symbol; if(symbol_table.move(symbol, new_symbol)) throw "cpp_typecheckt::typecheck_enum_body: symbol_table.move() failed"; cpp_idt &scope_identifier= cpp_scopes.put_into_scope(*new_symbol); scope_identifier.id_class=cpp_idt::SYMBOL; ++i; } }
//-------------------------------------------------------------------------- // Function: CommonFG::openEnumType ///\brief Opens the named enumeration datatype at this location. ///\param name - IN: Name of the enumeration datatype to open ///\return EnumType instance ///\exception H5::FileIException or H5::GroupIException // Programmer Binh-Minh Ribler - 2000 //-------------------------------------------------------------------------- EnumType CommonFG::openEnumType( const char* name ) const { // Call C function H5Topen2 to open the named datatype in this group, // given either the file or group id hid_t type_id = H5Topen2(getLocId(), name, H5P_DEFAULT); // If the datatype's opening failed, throw an exception if( type_id < 0 ) throwException("openEnumType", "H5Topen2 failed"); // No failure, create and return the EnumType object EnumType enum_type(type_id); return(enum_type); }
bool Pb2Json::Json2Message(const Json& json, ProtobufMsg& message, bool str2enum) { auto descriptor = message.GetDescriptor(); auto reflection = message.GetReflection(); if (nullptr == descriptor || nullptr == reflection) return false; auto count = descriptor->field_count(); for (auto i = 0; i < count; ++i) { const auto field = descriptor->field(i); if (nullptr == field) continue; auto& value = json[field->name()]; if (value.is_null()) continue; if (field->is_repeated()) { if (!value.is_array()) { return false; } else { Json2RepeatedMessage(value, message, field, reflection, str2enum); continue; } } switch (field->type()) { case ProtobufFieldDescriptor::TYPE_BOOL: { if (value.is_boolean()) reflection->SetBool(&message, field, value.get<bool>()); else if (value.is_number_integer()) reflection->SetBool(&message, field, value.get<uint32_t>() != 0); else if (value.is_string()) { if (value.get<std::string>() == "true") reflection->SetBool(&message, field, true); else if (value.get<std::string>() == "false") reflection->SetBool(&message, field, false); } } break; case ProtobufFieldDescriptor::TYPE_ENUM: { auto const* pedesc = field->enum_type(); const ::google::protobuf::EnumValueDescriptor* pevdesc = nullptr; if (str2enum) { pevdesc = pedesc->FindValueByName(value.get<std::string>()); } else { pevdesc = pedesc->FindValueByNumber(value.get<int>()); } if (nullptr != pevdesc) { reflection->SetEnum(&message, field, pevdesc); } } break; case ProtobufFieldDescriptor::TYPE_INT32: case ProtobufFieldDescriptor::TYPE_SINT32: case ProtobufFieldDescriptor::TYPE_SFIXED32: { if (value.is_number()) reflection->SetInt32(&message, field, value.get<int32_t>()); } break; case ProtobufFieldDescriptor::TYPE_UINT32: case ProtobufFieldDescriptor::TYPE_FIXED32: { if (value.is_number()) reflection->SetUInt32(&message, field, value.get<uint32_t>()); } break; case ProtobufFieldDescriptor::TYPE_INT64: case ProtobufFieldDescriptor::TYPE_SINT64: case ProtobufFieldDescriptor::TYPE_SFIXED64: { if (value.is_number()) reflection->SetInt64(&message, field, value.get<int64_t>()); } break; case ProtobufFieldDescriptor::TYPE_UINT64: case ProtobufFieldDescriptor::TYPE_FIXED64: { if (value.is_number()) reflection->SetUInt64(&message, field, value.get<uint64_t>()); } break; case ProtobufFieldDescriptor::TYPE_FLOAT: { if (value.is_number()) reflection->SetFloat(&message, field, value.get<float>()); } break; case ProtobufFieldDescriptor::TYPE_DOUBLE: { if (value.is_number()) reflection->SetDouble(&message, field, value.get<double>()); } break; case ProtobufFieldDescriptor::TYPE_STRING: case ProtobufFieldDescriptor::TYPE_BYTES: { if (value.is_string()) reflection->SetString(&message, field, value.get<std::string>()); } break; case ProtobufFieldDescriptor::TYPE_MESSAGE: { if (value.is_object()) Json2Message(value, *reflection->MutableMessage(&message, field)); } break; default: break; } } return true; }
void print_mcrl2(std::ostream &out, const std::set<XMASComponent *> &components, const XMASState &state) { auto types = all_types(components); auto qvars = build_queue_vars(components); Mcrl2OperatorPrinter printer; Operator::set_printer(&printer); out << "sort Msg = struct "; bool first = true; for (const auto &t : types) { out << (first ? "" : " | ") << t; first = false; } out << ";" << std::endl << std::endl; for (const auto &q : qvars) { out << "sort " << type(q) << " = Int -> Msg;" << std::endl; } out << std::endl; out << "map" << std::endl; for (const auto &q : qvars) { out << " give_" << name(q) << ": Msg # " << type(q) << " # Int -> " << type(q) << ";" << std::endl; out << " take_" << name(q) << ": " << type(q) << " -> " << type(q) << ";" << std::endl; out << " shift_" << name(q) << ": " << type(q) << " -> " << type(q) << ";" << std::endl; } out << std::endl << "var" << std::endl; out << " n : Int;" << std::endl; out << " m : Msg;" << std::endl; for (const auto &q : qvars) { out << " " << name(q) << " : " << type(q) << ";" << std::endl; } out << std::endl << "eqn" << std::endl; for (const auto &q : qvars) { out << " give_" << name(q) << "(m, " << name(q) << ", n) = " << name(q) << "[n -> m];" << std::endl; out << " take_" << name(q) << "(" << name(q) << ") = shift_" << name(q) << "(" << name(q) << ");" << std::endl; out << " shift_" << name(q) << "(" << name(q) << ")(n) = " << name(q) << "(n+1);" << std::endl; out << " shift_" << name(q) << "(" << name(q) << ")[n -> m] = " << name(q) << "[n+1 -> m];" << std::endl; } out << std::endl << "act" << std::endl; auto transitions = compute_transitions(components, true); auto ivars = build_input_vars(transitions); int i = 0; for (auto &t : transitions) { out << " T" << i; t.set_id(i++); bool first = true; for (const auto &o : t.out()) { if (enum_type(o.second).size() <= 1) continue; out << (first ? ": " : " # ") << "Msg"; first = false; } out << ";" << std::endl; } out << std::endl; out << "proc XMAS("; first = true; for (const auto &q : qvars) { out << (first ? "" : ", ") << name(q) << " : " << type(q) << ", " << size(q) << " : Int"; first = false; } out << ") = " << std::endl; first = true; for (const auto &t : transitions) { out << " " << (first ? " " : "+ ") << "("; bool first2 = true; for (auto q : t.in_queues()) { out << (first2 ? "" : " && ") << size(qv(q)) << " != " << q->c; first2 = false; } for (auto q : t.out_queues()) { out << (first2 ? "" : " && ") << size(qv(q)) << " != " << 0; first2 = false; } for (auto c : t.conditions()) { out << (first2 ? "" : " && ") << c.op; first2 = false; } out << ") -> "; first2 = true; for (auto pair : t.out()) { if (enum_type(pair.second).size() <= 1) continue; out << (first2 ? "sum " : ", ") << iv(pair.first).name() << ": Msg"; first2 = false; } if (!first2) out << ". "; out << "( T" << t.id(); first2 = true; for (auto pair : t.out()) { if (enum_type(pair.second).size() <= 1) continue; out << (first2 ? "(" : ", ") << iv(pair.first).name(); first2 = false; } if (!first2) out << ")"; out << " . XMAS("; first2 = true; for (const auto &qvar : qvars) { auto inqs = zip(t.in_queues(), t.in_functions()); auto outqs = t.out_queues(); // TODO: efficiency auto inq_it = std::find_if( inqs.begin(), inqs.end(), [&](const std::pair<XMASQueue *, const SymbolicFunction &> &pair) { return pair.first == qvar.q; }); if (inq_it != inqs.end()) { out << (first2 ? "" : ", ") << "give_" << name(qvar) << "(" << (*inq_it).second.op << ", " << name(qvar) << ", " << size(qvar) << "), " << size(qvar) << "+1"; } else if (std::find_if(outqs.begin(), outqs.end(), [&](XMASQueue *q) { return q == qvar.q; }) != outqs.end()) { out << (first2 ? "" : ", ") << "take_" << name(qvar) << "(" << name(qvar) << "), " << size(qvar) << "-1"; } else { // nothing happens... out << (first2 ? "" : ", ") << name(qvar) << ", " << size(qvar); } first2 = false; } out << ")"; out << " )"; out << std::endl << std::endl; first = false; } out << ";" << std::endl; out << "init XMAS("; first = true; for (auto &qvar : qvars) { auto &qstate = state.get_queue(qvar.q); out << (first ? "" : ",\n ") << "(lambda n : Int . " << *types.begin() << ")"; int i = 0; for (auto &packet : qstate) { // FIXME: that's a lot for one line out << "[" << i << "->" << dynamic_cast<SymbolicEnumField *>( packet[0].fields.begin()->second.get())->values[0] << "]"; } out << ", " << qstate.size(); first = false; } out << ");"; out << std::endl << std::endl << std::endl; out << "mu X("; first = true; for (const auto &qvar : qvars) { out << (first ? "" : ", ") << size(qvar) << ":Int = " << state.get_queue(qvar.q).size(); first = false; } out << ")" << std::endl << " . (" << std::endl; out << " "; first = true; for (const auto &qvar : qvars) { out << (first ? "(" : " && ") << "val(" << size(qvar) << " == 0)"; first = false; } out << ")" << std::endl << " || ("; first = true; for (const auto &qvar : qvars) { out << (first ? "(" : " || ") << "val(" << size(qvar) << " != 0)"; first = false; } out << ")" << std::endl << " && (" << std::endl; first = true; for (const auto &t : transitions) { out << " " << (first ? " " : "|| "); out << "(<"; bool first2 = true; for (auto pair : t.out()) { if (enum_type(pair.second).size() <= 1) continue; out << (first2 ? "exists " : ", ") << iv(pair.first).name() << ": Msg"; first2 = false; } if (!first2) out << " . "; out << "T" << t.id(); first2 = true; for (auto pair : t.out()) { if (enum_type(pair.second).size() <= 1) continue; out << (first2 ? "(" : ",") << iv(pair.first).name(); first2 = false; } if (!first2) out << ")"; out << "> X("; first2 = true; for (const auto &qvar : qvars) { auto inqs = zip(t.in_queues(), t.in_functions()); auto outqs = t.out_queues(); // TODO: efficiency auto inq_it = std::find_if( inqs.begin(), inqs.end(), [&](const std::pair<XMASQueue *, const SymbolicFunction &> &pair) { return pair.first == qvar.q; }); if (inq_it != inqs.end()) { out << (first2 ? "" : ", ") << size(qvar) << "+1"; } else if (std::find_if(outqs.begin(), outqs.end(), [&](XMASQueue *q) { return q == qvar.q; }) != outqs.end()) { out << (first2 ? "" : ", ") << size(qvar) << "-1"; } else { // nothing happens... out << (first2 ? "" : ", ") << size(qvar); } first2 = false; } out << "))" << std::endl; first = false; } out << " ))" << std::endl; out << ")" << std::endl; for (auto c : components) { c->clearComponentExtension<InputVarXMASComponentExtension>(); } }