From 436a877be73ebe14fecd3ef0e9b7dd6b854d2e3b Mon Sep 17 00:00:00 2001 From: "Jay Freeman (saurik)" Date: Sun, 3 Jan 2016 03:36:31 -0800 Subject: [PATCH] Add extern "C" expression, for Functor's toCYON(). --- Execute.cpp | 3 ++- Output.cpp | 6 +++++- Parser.ypp.in | 12 ++++++++++-- Replace.cpp | 8 ++++++-- Syntax.hpp | 22 ++++++++++++++++++++-- 5 files changed, 43 insertions(+), 8 deletions(-) diff --git a/Execute.cpp b/Execute.cpp index 7d14b10..b8978db 100644 --- a/Execute.cpp +++ b/Execute.cpp @@ -1632,7 +1632,7 @@ static JSValueRef Functor_callAsFunction_toCYON(JSContextRef context, JSObjectRe CYOptions options; CYOutput output(*str.rdbuf(), options); output.pretty_ = true; - (new(pool) CYExternal(new(pool) CYString("C"), typed))->Output(output, CYNoFlags); + (new(pool) CYExternalExpression(new(pool) CYString("C"), typed))->Output(output, CYNoFlags); return CYCastJSValue(context, CYJSString(str.str())); } CYCatch(NULL) } @@ -1741,6 +1741,7 @@ static JSValueRef Type_callAsFunction_toCYON(JSContextRef context, JSObjectRef o std::stringbuf out; CYOptions options; CYOutput output(out, options); + output.pretty_ = true; (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 0bc1172..06b60b2 100644 --- a/Output.cpp +++ b/Output.cpp @@ -457,11 +457,15 @@ void CYExtend::Output(CYOutput &out, CYFlags flags) const { out << ' ' << object_; } -void CYExternal::Output(CYOutput &out, CYFlags flags) const { +void CYExternalDefinition::Output(CYOutput &out, CYFlags flags) const { out << "extern" << ' ' << abi_ << ' ' << typed_; out.Terminate(); } +void CYExternalExpression::Output(CYOutput &out, CYFlags flags) const { + out << '(' << "extern" << ' ' << abi_ << ' ' << typed_ << ')'; +} + void CYFatArrow::Output(CYOutput &out, CYFlags flags) const { out << '(' << parameters_ << ')' << ' ' << "=>" << ' ' << '{' << code_ << '}'; } diff --git a/Parser.ypp.in b/Parser.ypp.in index d969fdf..11d8d29 100644 --- a/Parser.ypp.in +++ b/Parser.ypp.in @@ -2423,7 +2423,7 @@ IdentifierNoOf ; ExternCStatement - : TypedIdentifierField[typed] TerminatorHard { $$ = CYNew CYExternal(CYNew CYString("C"), $typed); } + : TypedIdentifierField[typed] TerminatorHard { $$ = CYNew CYExternalDefinition(CYNew CYString("C"), $typed); } | TypeDefinition[pass] { $$ = $pass; } ; @@ -2437,8 +2437,16 @@ ExternC | ExternCStatement[pass] { $$ = $pass; } ; +ABI + : StringLiteral[abi] { if (strcmp($abi->Value(), "C") != 0) CYERR(@abi, "unknown extern binding"); } + ; + Statement__ - : "extern" NewLineNot StringLiteral[abi] { if (strcmp($abi->Value(), "C") != 0) CYERR(@abi, "unknown extern binding"); } ExternC[pass] { $$ = $pass; } + : "extern" NewLineNot ABI[abi] ExternC[pass] { $$ = $pass; } + ; + +PrimaryExpression + : "(" LexOf "extern" NewLineOpt ABI[abi] TypedIdentifierField[typed] ")" { $$ = CYNew CYExternalExpression(CYNew CYString("C"), $typed); } ; /* }}} */ @end diff --git a/Replace.cpp b/Replace.cpp index 6f33f8f..de9489e 100644 --- a/Replace.cpp +++ b/Replace.cpp @@ -388,8 +388,12 @@ CYTarget *CYExtend::Replace(CYContext &context) { return object_.Replace(context, lhs_); } -CYStatement *CYExternal::Replace(CYContext &context) { - return $E($ CYAssign($V(typed_->identifier_), $C1(typed_->Replace(context), $C2($V("dlsym"), $V("RTLD_DEFAULT"), $S(typed_->identifier_->Word()))))); +CYStatement *CYExternalDefinition::Replace(CYContext &context) { + return $E($ CYAssign($V(typed_->identifier_), $ CYExternalExpression(abi_, typed_))); +} + +CYTarget *CYExternalExpression::Replace(CYContext &context) { + return $C1(typed_->Replace(context), $C2($V("dlsym"), $V("RTLD_DEFAULT"), $S(typed_->identifier_->Word()))); } CYNumber *CYFalse::Number(CYContext &context) { diff --git a/Syntax.hpp b/Syntax.hpp index e67031f..e3885cf 100644 --- a/Syntax.hpp +++ b/Syntax.hpp @@ -2405,13 +2405,31 @@ struct CYImportDeclaration : virtual void Output(CYOutput &out, CYFlags flags) const; }; -struct CYExternal : +struct CYExternalExpression : + CYTarget +{ + CYString *abi_; + CYTypedIdentifier *typed_; + + CYExternalExpression(CYString *abi, CYTypedIdentifier *typed) : + abi_(abi), + typed_(typed) + { + } + + CYPrecedence(0) + + virtual CYTarget *Replace(CYContext &context); + virtual void Output(CYOutput &out, CYFlags flags) const; +}; + +struct CYExternalDefinition : CYStatement { CYString *abi_; CYTypedIdentifier *typed_; - CYExternal(CYString *abi, CYTypedIdentifier *typed) : + CYExternalDefinition(CYString *abi, CYTypedIdentifier *typed) : abi_(abi), typed_(typed) { -- 2.47.2