diff options
author | cooldome <cdome@bk.ru> | 2019-04-03 09:42:41 +0100 |
---|---|---|
committer | Andreas Rumpf <rumpf_a@web.de> | 2019-04-03 10:42:41 +0200 |
commit | 734da9544dda5a546a8add580499519be39f5591 (patch) | |
tree | 0f590444c136c325735aa6d97916f4f15990335e | |
parent | eaca62f3310404e38fefc81ca6cae6466951cbff (diff) | |
download | Nim-734da9544dda5a546a8add580499519be39f5591.tar.gz |
fixes #10948 (#10949)
-rw-r--r-- | compiler/cgen.nim | 2 | ||||
-rw-r--r-- | tests/cpp/tretvar.nim | 39 |
2 files changed, 40 insertions, 1 deletions
diff --git a/compiler/cgen.nim b/compiler/cgen.nim index 3114a7f70..18e0b1c68 100644 --- a/compiler/cgen.nim +++ b/compiler/cgen.nim @@ -540,7 +540,7 @@ proc initLocExprSingleUse(p: BProc, e: PNode, result: var TLoc) = discard "bug #8202; enforce evaluation order for nested calls for C++ too" # We may need to consider that 'f(g())' cannot be rewritten to 'tmp = g(); f(tmp)' # if 'tmp' lacks a move/assignment operator. - if e[0].kind == nkSym and sfConstructor in e[0].sym.flags: + if e[0].kind == nkSym and sfCompileToCpp in e[0].sym.flags: result.flags.incl lfSingleUse else: result.flags.incl lfSingleUse diff --git a/tests/cpp/tretvar.nim b/tests/cpp/tretvar.nim new file mode 100644 index 000000000..83c37721e --- /dev/null +++ b/tests/cpp/tretvar.nim @@ -0,0 +1,39 @@ +discard """ + targets: "cpp" + output: '''test1 +xest1 +''' +""" +{.passC: "-std=c++14".} + +{.experimental: "dotOperators".} + +import macros + +type + stdString {.importcpp: "std::string", header: "<string>".} = object + stdUniquePtr[T] {.importcpp: "std::unique_ptr", header: "<memory>".} = object + +proc c_str(a: stdString): cstring {.importcpp: "(char *)(#.c_str())", header: "<string>".} + +proc len(a: stdString): csize {.importcpp: "(#.length())", header: "<string>".} + +proc setChar(a: var stdString, i: csize, c: char) {.importcpp: "(#[#] = #)", header: "<string>".} + +proc `*`*[T](this: stdUniquePtr[T]): var T {.noSideEffect, importcpp: "(* #)", header: "<memory>".} + +proc make_unique_str(a: cstring): stdUniquePtr[stdString] {.importcpp: "std::make_unique<std::string>(#)", header: "<string>".} + +macro `.()`*[T](this: stdUniquePtr[T], name: untyped, args: varargs[untyped]): untyped = + result = nnkCall.newTree( + nnkDotExpr.newTree( + newNimNode(nnkPar).add(prefix(this, "*")), + name + ) + ) + copyChildrenTo(args, result) + +var val = make_unique_str("test1") +echo val.c_str() +val.setChar(0, 'x') +echo val.c_str() |