X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=trace_model.cpp;h=79bb7575801b4842c2bb23be04deacc9986a5c9d;hb=93dfad1dc7e20d694e2c8b63515bff8ae91f3700;hp=b083186b01bd94d26d437f359fb9df1bfd2eefe7;hpb=9bde72eb176831d6d4b185ec7880ec35e61147f8;p=apitrace diff --git a/trace_model.cpp b/trace_model.cpp index b083186..79bb757 100644 --- a/trace_model.cpp +++ b/trace_model.cpp @@ -63,54 +63,76 @@ Blob::~Blob() { // bool cast -Null ::operator bool(void) const { return false; } -Bool ::operator bool(void) const { return value; } -SInt ::operator bool(void) const { return value != 0; } -UInt ::operator bool(void) const { return value != 0; } -Float ::operator bool(void) const { return value != 0; } -String ::operator bool(void) const { return true; } -Enum ::operator bool(void) const { return static_cast(*sig->second); } -Struct ::operator bool(void) const { return true; } -Array ::operator bool(void) const { return true; } -Blob ::operator bool(void) const { return true; } -Pointer::operator bool(void) const { return value != 0; } +bool Null ::toBool(void) const { return false; } +bool Bool ::toBool(void) const { return value; } +bool SInt ::toBool(void) const { return value != 0; } +bool UInt ::toBool(void) const { return value != 0; } +bool Float ::toBool(void) const { return value != 0; } +bool String ::toBool(void) const { return true; } +bool Enum ::toBool(void) const { return sig->value != 0; } +bool Struct ::toBool(void) const { return true; } +bool Array ::toBool(void) const { return true; } +bool Blob ::toBool(void) const { return true; } +bool Pointer::toBool(void) const { return value != 0; } // signed integer cast -Value ::operator signed long long (void) const { assert(0); return 0; } -Null ::operator signed long long (void) const { return 0; } -Bool ::operator signed long long (void) const { return static_cast(value); } -SInt ::operator signed long long (void) const { return value; } -UInt ::operator signed long long (void) const { assert(static_cast(value) >= 0); return static_cast(value); } -Float ::operator signed long long (void) const { return static_cast(value); } -Enum ::operator signed long long (void) const { return static_cast(*sig->second); } +signed long long Value ::toSInt(void) const { assert(0); return 0; } +signed long long Null ::toSInt(void) const { return 0; } +signed long long Bool ::toSInt(void) const { return static_cast(value); } +signed long long SInt ::toSInt(void) const { return value; } +signed long long UInt ::toSInt(void) const { assert(static_cast(value) >= 0); return static_cast(value); } +signed long long Float ::toSInt(void) const { return static_cast(value); } +signed long long Enum ::toSInt(void) const { return sig->value; } // unsigned integer cast -Value ::operator unsigned long long (void) const { assert(0); return 0; } -Null ::operator unsigned long long (void) const { return 0; } -Bool ::operator unsigned long long (void) const { return static_cast(value); } -SInt ::operator unsigned long long (void) const { assert(value >= 0); return static_cast(value); } -UInt ::operator unsigned long long (void) const { return value; } -Float ::operator unsigned long long (void) const { return static_cast(value); } -Enum ::operator unsigned long long (void) const { return static_cast(*sig->second); } +unsigned long long Value ::toUInt(void) const { assert(0); return 0; } +unsigned long long Null ::toUInt(void) const { return 0; } +unsigned long long Bool ::toUInt(void) const { return static_cast(value); } +unsigned long long SInt ::toUInt(void) const { assert(value >= 0); return static_cast(value); } +unsigned long long UInt ::toUInt(void) const { return value; } +unsigned long long Float ::toUInt(void) const { return static_cast(value); } +unsigned long long Enum ::toUInt(void) const { assert(sig->value >= 0); return sig->value; } // floating point cast -Value ::operator double (void) const { assert(0); return 0; } -Null ::operator double (void) const { return 0; } -Bool ::operator double (void) const { return static_cast(value); } -SInt ::operator double (void) const { return static_cast(value); } -UInt ::operator double (void) const { return static_cast(value); } -Float ::operator double (void) const { return value; } -Enum ::operator double (void) const { return static_cast(*sig->second); } +float Value ::toFloat(void) const { assert(0); return 0; } +float Null ::toFloat(void) const { return 0; } +float Bool ::toFloat(void) const { return static_cast(value); } +float SInt ::toFloat(void) const { return static_cast(value); } +float UInt ::toFloat(void) const { return static_cast(value); } +float Float ::toFloat(void) const { return value; } +float Enum ::toFloat(void) const { return static_cast(sig->value); } -// blob cast -void * Value ::blob(void) const { assert(0); return NULL; } -void * Null ::blob(void) const { return NULL; } -void * Blob ::blob(void) const { return buf; } -void * Pointer::blob(void) const { assert(value < 0x100000ULL); return (void *)value; } +// floating point cast +double Value ::toDouble(void) const { assert(0); return 0; } +double Null ::toDouble(void) const { return 0; } +double Bool ::toDouble(void) const { return static_cast(value); } +double SInt ::toDouble(void) const { return static_cast(value); } +double UInt ::toDouble(void) const { return static_cast(value); } +double Float ::toDouble(void) const { return value; } +double Enum ::toDouble(void) const { return static_cast(sig->value); } + + +// pointer cast +void * Value ::toPointer(void) const { assert(0); return NULL; } +void * Null ::toPointer(void) const { return NULL; } +void * Blob ::toPointer(void) const { return buf; } +void * Pointer::toPointer(void) const { return (void *)value; } + + +// pointer cast +unsigned long long Value ::toUIntPtr(void) const { assert(0); return 0; } +unsigned long long Null ::toUIntPtr(void) const { return 0; } +unsigned long long Pointer::toUIntPtr(void) const { return value; } + + +// string cast +const char * Value ::toString(void) const { assert(0); return NULL; } +const char * Null ::toString(void) const { return NULL; } +const char * String::toString(void) const { return value; } // virtual Value::visit() @@ -134,7 +156,7 @@ void Visitor::visit(SInt *) { assert(0); } void Visitor::visit(UInt *) { assert(0); } void Visitor::visit(Float *) { assert(0); } void Visitor::visit(String *) { assert(0); } -void Visitor::visit(Enum *node) { _visit(node->sig->second); } +void Visitor::visit(Enum *node) { assert(0); } void Visitor::visit(Bitmask *node) { visit(static_cast(node)); } void Visitor::visit(Struct *) { assert(0); } void Visitor::visit(Array *) { assert(0); } @@ -155,8 +177,8 @@ protected: Formatter::Attribute *literal; public: - Dumper(std::ostream &_os) : os(_os) { - formatter = Formatter::defaultFormatter(); + Dumper(std::ostream &_os, bool color) : os(_os) { + formatter = Formatter::defaultFormatter(color); normal = formatter->normal(); bold = formatter->bold(); italic = formatter->italic(); @@ -197,7 +219,7 @@ public: void visit(String *node) { os << literal << "\""; - for (std::string::const_iterator it = node->value.begin(); it != node->value.end(); ++it) { + for (const char *it = node->value; *it; ++it) { unsigned char c = (unsigned char) *it; if (c == '\"') os << "\\\""; @@ -228,21 +250,21 @@ public: } void visit(Enum *node) { - os << literal << node->sig->first << normal; + os << literal << node->sig->name << normal; } void visit(Bitmask *bitmask) { unsigned long long value = bitmask->value; - const Bitmask::Signature *sig = bitmask->sig; + const BitmaskSig *sig = bitmask->sig; bool first = true; - for (Bitmask::Signature::const_iterator it = sig->begin(); value != 0 && it != sig->end(); ++it) { - if ((it->second && (value & it->second) == it->second) || - (!it->second && value == 0)) { + for (const BitmaskFlag *it = sig->flags; value != 0 && it != sig->flags + sig->num_flags; ++it) { + if ((it->value && (value & it->value) == it->value) || + (!it->value && value == 0)) { if (!first) { os << " | "; } - os << literal << it->first << normal; - value &= ~it->second; + os << literal << it->name << normal; + value &= ~it->value; first = false; } } @@ -295,7 +317,11 @@ public: os << bold << call->sig->name << normal << "("; for (unsigned i = 0; i < call->args.size(); ++i) { os << sep << italic << call->sig->arg_names[i] << normal << " = "; - _visit(call->args[i]); + if (call->args[i]) { + _visit(call->args[i]); + } else { + os << "?"; + } sep = ", "; } os << ")"; @@ -308,27 +334,16 @@ public: }; -std::ostream & operator <<(std::ostream &os, Value *value) { - Dumper d(os); - if (value) { - value->visit(d); - } - return os; -} - - -static inline const Value *unwrap(const Value *node) { - const Enum *c = dynamic_cast(node); - if (c) - return c->sig->second; - return node; +void Value::dump(std::ostream &os, bool color) { + Dumper d(os, color); + visit(d); } static Null null; const Value & Value::operator[](size_t index) const { - const Array *array = dynamic_cast(unwrap(this)); + const Array *array = dynamic_cast(this); if (array) { if (index < array->values.size()) { return *array->values[index]; @@ -337,22 +352,10 @@ const Value & Value::operator[](size_t index) const { return null; } -const char * Value::string(void) const { - const String *string = dynamic_cast(unwrap(this)); - if (string) - return string->value.c_str(); - const Null *null = dynamic_cast(unwrap(this)); - if (null) - return NULL; - assert(0); - return NULL; -} - -std::ostream & operator <<(std::ostream &os, Call &call) { - Dumper d(os); - os << call.no << " "; - d.visit(&call); - return os; +void Call::dump(std::ostream &os, bool color) { + Dumper d(os, color); + os << no << " "; + d.visit(this); }