[llvm-dev] How does Twine work?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

[llvm-dev] How does Twine work?

Tingyuan LIANG via llvm-dev
I'm looking at the documentation on Twine at http://llvm.org/docs/ProgrammersManual.html#llvm-adt-twine-h and it gives example code:

void foo(const Twine &T);
...
StringRef X = ...
unsigned i = ...
foo(X + "." + Twine(i));

How exactly does that last line work? Since addition is left associative, I would expect it to be parsed as (X + ".") ... so it's trying to add a StringRef and a const char* before Twine can come into the picture at all?


_______________________________________________
LLVM Developers mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
Reply | Threaded
Open this post in threaded view
|

Re: [llvm-dev] How does Twine work?

Tingyuan LIANG via llvm-dev
I believe StringRef + const char* is overloaded at the bottom of Twine.h. And without those overloads it might try to implicitly convert both StringRef and const char* to Twine and then add them since StringRef + const char * wouldn't otherwise be defined.

  inline Twine operator+(const Twine &LHS, const Twine &RHS) {
    return LHS.concat(RHS);
  }

  /// Additional overload to guarantee simplified codegen; this is equivalent to
  /// concat().

  inline Twine operator+(const char *LHS, const StringRef &RHS) {
    return Twine(LHS, RHS);
  }

  /// Additional overload to guarantee simplified codegen; this is equivalent to
  /// concat().

  inline Twine operator+(const StringRef &LHS, const char *RHS) {
    return Twine(LHS, RHS);
  }



~Craig


On Mon, Apr 29, 2019 at 3:18 PM Russell Wallace via llvm-dev <[hidden email]> wrote:
I'm looking at the documentation on Twine at http://llvm.org/docs/ProgrammersManual.html#llvm-adt-twine-h and it gives example code:

void foo(const Twine &T);
...
StringRef X = ...
unsigned i = ...
foo(X + "." + Twine(i));

How exactly does that last line work? Since addition is left associative, I would expect it to be parsed as (X + ".") ... so it's trying to add a StringRef and a const char* before Twine can come into the picture at all?

_______________________________________________
LLVM Developers mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev

_______________________________________________
LLVM Developers mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
Reply | Threaded
Open this post in threaded view
|

Re: [llvm-dev] How does Twine work?

Tingyuan LIANG via llvm-dev
Ah, that makes sense. Thanks!

On Mon, Apr 29, 2019 at 11:43 PM Craig Topper <[hidden email]> wrote:
I believe StringRef + const char* is overloaded at the bottom of Twine.h. And without those overloads it might try to implicitly convert both StringRef and const char* to Twine and then add them since StringRef + const char * wouldn't otherwise be defined.

  inline Twine operator+(const Twine &LHS, const Twine &RHS) {
    return LHS.concat(RHS);
  }

  /// Additional overload to guarantee simplified codegen; this is equivalent to
  /// concat().

  inline Twine operator+(const char *LHS, const StringRef &RHS) {
    return Twine(LHS, RHS);
  }

  /// Additional overload to guarantee simplified codegen; this is equivalent to
  /// concat().

  inline Twine operator+(const StringRef &LHS, const char *RHS) {
    return Twine(LHS, RHS);
  }



~Craig


On Mon, Apr 29, 2019 at 3:18 PM Russell Wallace via llvm-dev <[hidden email]> wrote:
I'm looking at the documentation on Twine at http://llvm.org/docs/ProgrammersManual.html#llvm-adt-twine-h and it gives example code:

void foo(const Twine &T);
...
StringRef X = ...
unsigned i = ...
foo(X + "." + Twine(i));

How exactly does that last line work? Since addition is left associative, I would expect it to be parsed as (X + ".") ... so it's trying to add a StringRef and a const char* before Twine can come into the picture at all?

_______________________________________________
LLVM Developers mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev

_______________________________________________
LLVM Developers mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev