From 53cb77ff9ae5b8dc87f7924ede75be1e1944e004 Mon Sep 17 00:00:00 2001 From: "Jay Freeman (saurik)" Date: Sun, 3 Jan 2016 03:24:05 -0800 Subject: [PATCH 1/1] Make Functor/struct output fancy type definitions. --- Analyze.cpp | 2 +- Execute.cpp | 40 ++++++++++++++++++++++++++++------------ Output.cpp | 27 ++++++++++++++++----------- Syntax.hpp | 2 +- sig/copy.cpp | 6 +++++- 5 files changed, 51 insertions(+), 26 deletions(-) diff --git a/Analyze.cpp b/Analyze.cpp index 7b3e2ca..4f84e03 100644 --- a/Analyze.cpp +++ b/Analyze.cpp @@ -383,8 +383,8 @@ static CXChildVisitResult CYChildVisit(CXCursor cursor, CXCursor parent, CXClien } })); + value << "new Type([" << types.str() << "],[" << names.str() << "]).withName(\"" << name << "\")"; name += "$cy"; - value << "new Type([" << types.str() << "],[" << names.str() << "])"; } break; case CXCursor_TypedefDecl: { diff --git a/Execute.cpp b/Execute.cpp index fc19fba..7d14b10 100644 --- a/Execute.cpp +++ b/Execute.cpp @@ -1605,18 +1605,34 @@ static JSValueRef Functor_callAsFunction_valueOf(JSContextRef context, JSObjectR static JSValueRef Functor_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { cy::Functor *internal(reinterpret_cast(JSObjectGetPrivate(_this))); uint8_t *value(reinterpret_cast(internal->value_)); - std::ostringstream str; - Dl_info info; - if (internal->value_ == NULL) - str << "NULL"; - else if (dladdr(value, &info) == 0) - str << internal->value_; - else { - str << info.dli_sname; - off_t offset(value - reinterpret_cast(info.dli_saddr)); - if (offset != 0) - str << "+0x" << std::hex << offset; + + CYLocalPool pool; + + sig::Function function(internal->variadic_); + sig::Copy(pool, function.signature, internal->signature_); + + auto typed(CYDecodeType(pool, &function)); { + std::ostringstream str; + Dl_info info; + if (internal->value_ == NULL) + str << "NULL"; + else if (dladdr(value, &info) == 0) + str << internal->value_; + else { + str << info.dli_sname; + off_t offset(value - reinterpret_cast(info.dli_saddr)); + if (offset != 0) + str << "+0x" << std::hex << offset; + } + + typed->identifier_ = new(pool) CYIdentifier(pool.strdup(str.str().c_str())); } + + std::ostringstream str; + CYOptions options; + CYOutput output(*str.rdbuf(), options); + output.pretty_ = true; + (new(pool) CYExternal(new(pool) CYString("C"), typed))->Output(output, CYNoFlags); return CYCastJSValue(context, CYJSString(str.str())); } CYCatch(NULL) } @@ -1725,7 +1741,7 @@ static JSValueRef Type_callAsFunction_toCYON(JSContextRef context, JSObjectRef o std::stringbuf out; CYOptions options; CYOutput output(out, options); - (new(pool) CYTypeExpression(CYDecodeType(pool, internal->type_)))->Output(output, CYNoFlags); + (new(pool) CYTypeExpression(CYDecodeType(pool, internal->type_->Copy(pool, ""))))->Output(output, CYNoFlags); return CYCastJSValue(context, CYJSString(out.str().c_str())); } CYCatch(NULL) } diff --git a/Output.cpp b/Output.cpp index dc1f7f9..0bc1172 100644 --- a/Output.cpp +++ b/Output.cpp @@ -458,7 +458,7 @@ void CYExtend::Output(CYOutput &out, CYFlags flags) const { } void CYExternal::Output(CYOutput &out, CYFlags flags) const { - out << "extern" << abi_ << typed_; + out << "extern" << ' ' << abi_ << ' ' << typed_; out.Terminate(); } @@ -665,7 +665,7 @@ void CYTemplate::Output(CYOutput &out, CYFlags flags) const { } void CYTypeArrayOf::Output(CYOutput &out, CYIdentifier *identifier) const { - next_->Output(out, Precedence(), identifier); + next_->Output(out, Precedence(), identifier, false); out << '['; out << size_; out << ']'; @@ -673,17 +673,17 @@ void CYTypeArrayOf::Output(CYOutput &out, CYIdentifier *identifier) const { void CYTypeBlockWith::Output(CYOutput &out, CYIdentifier *identifier) const { out << '(' << '^'; - next_->Output(out, Precedence(), identifier); + next_->Output(out, Precedence(), identifier, false); out << ')' << '(' << parameters_ << ')'; } void CYTypeConstant::Output(CYOutput &out, CYIdentifier *identifier) const { - out << "const" << ' '; - next_->Output(out, Precedence(), identifier); + out << "const"; + next_->Output(out, Precedence(), identifier, false); } void CYTypeFunctionWith::Output(CYOutput &out, CYIdentifier *identifier) const { - next_->Output(out, Precedence(), identifier); + next_->Output(out, Precedence(), identifier, false); out << '(' << parameters_; if (variadic_) { if (parameters_ != NULL) @@ -695,15 +695,20 @@ void CYTypeFunctionWith::Output(CYOutput &out, CYIdentifier *identifier) const { void CYTypePointerTo::Output(CYOutput &out, CYIdentifier *identifier) const { out << '*'; - next_->Output(out, Precedence(), identifier); + next_->Output(out, Precedence(), identifier, false); } void CYTypeVolatile::Output(CYOutput &out, CYIdentifier *identifier) const { out << "volatile"; - next_->Output(out, Precedence(), identifier); + next_->Output(out, Precedence(), identifier, true); } -void CYTypeModifier::Output(CYOutput &out, int precedence, CYIdentifier *identifier) const { +void CYTypeModifier::Output(CYOutput &out, int precedence, CYIdentifier *identifier, bool space) const { + if (this == NULL && identifier == NULL) + return; + else if (space) + out << ' '; + if (this == NULL) { out << identifier; return; @@ -720,7 +725,7 @@ void CYTypeModifier::Output(CYOutput &out, int precedence, CYIdentifier *identif void CYTypedIdentifier::Output(CYOutput &out) const { specifier_->Output(out); - modifier_->Output(out, 0, identifier_); + modifier_->Output(out, 0, identifier_, true); } void CYEncodedType::Output(CYOutput &out, CYFlags flags) const { @@ -990,7 +995,7 @@ void CYStructTail::Output(CYOutput &out) const { out << '\n'; } --out.indent_; - out << '}'; + out << '\t' << '}'; } void CYSuperAccess::Output(CYOutput &out, CYFlags flags) const { diff --git a/Syntax.hpp b/Syntax.hpp index a204f58..e67031f 100644 --- a/Syntax.hpp +++ b/Syntax.hpp @@ -2175,7 +2175,7 @@ struct CYTypeModifier : CYTarget *Replace(CYContext &context, CYTarget *type); virtual void Output(CYOutput &out, CYIdentifier *identifier) const = 0; - void Output(CYOutput &out, int precedence, CYIdentifier *identifier) const; + void Output(CYOutput &out, int precedence, CYIdentifier *identifier, bool space) const; virtual CYTypeFunctionWith *Function() { return NULL; } }; diff --git a/sig/copy.cpp b/sig/copy.cpp index 664039f..63b72fe 100644 --- a/sig/copy.cpp +++ b/sig/copy.cpp @@ -86,7 +86,11 @@ Object *Object::Copy(CYPool &pool, const char *rename) const { #endif Aggregate *Aggregate::Copy(CYPool &pool, const char *rename) const { - Aggregate *copy(new(pool) Aggregate(overlap, rename ?: pool.strdup(name))); + if (rename == NULL) + rename = pool.strdup(name); + else if (rename[0] == '\0') + rename = NULL; + Aggregate *copy(new(pool) Aggregate(overlap, rename)); sig::Copy(pool, copy->signature, signature); return copy; } -- 2.47.2