[cfe-dev] Difference between getResultType adn getCallResultType
Richard Smith
metafoo at gmail.com
Fri Jan 24 17:24:18 PST 2014
On Fri Jan 24 2014 at 9:20:44 AM, Jordan Rose <jordan_rose at apple.com> wrote:
>
> On Jan 24, 2014, at 2:51 , Pedro Delgado Perez <
> pedro.delgadoperez at mail.uca.es> wrote:
>
> Hi,
>
> My question is simple. Could anyone explain me what's the difference
> between the methods
> getResultType and getCallResultType in FunctionDecl?
>
> This is the implementation in FunctionType:
>
> 02783 <http://clang.llvm.org/doxygen/classclang_1_1FunctionType.html#aafa453cd7e81a7c3d6c61f261e68afe8> QualType <http://clang.llvm.org/doxygen/classclang_1_1QualType.html> getResultType <http://clang.llvm.org/doxygen/classclang_1_1FunctionType.html#aafa453cd7e81a7c3d6c61f261e68afe8>() const { return ResultType; }
> ...
> 02799 <http://clang.llvm.org/doxygen/classclang_1_1FunctionType.html#a5f8df0258bcf7281e7055ea01dbd8f85> QualType <http://clang.llvm.org/doxygen/classclang_1_1QualType.html> getCallResultType <http://clang.llvm.org/doxygen/classclang_1_1FunctionType.html#a5f8df0258bcf7281e7055ea01dbd8f85>(ASTContext <http://clang.llvm.org/doxygen/classclang_1_1ASTContext.html> &Context <http://clang.llvm.org/doxygen/ASTMatchFinder_8cpp.html#a779280869b9fac47a9bc391e7709171f>) const {2800 return getResultType().getNonLValueExprType <http://clang.llvm.org/doxygen/classclang_1_1QualType.html#a89e3ea65c009b7a0ea76edc56076107c>(Context);2801 }
>
> And this is the definition of getNonLValueExprType<http://clang.llvm.org/doxygen/classclang_1_1QualType.html#a89e3ea65c009b7a0ea76edc56076107c>
> :
> Determine the type of a (typically non-lvalue) expression with the
> specified result type.
> This routine should be used for expressions for which the return type is
> explicitly specified (e.g., in a cast or call) and isn't necessarily an
> lvalue. It removes a top-level reference (since there are no expressions of
> reference type) and deletes top-level cvr-qualifiers from non-class types
> (in C++) or all types (in C).
>
> But I am not able to understand this very well. Could anyone give me an
> example of the difference? Up to now, I have been using getResultType, but
> now I have found getCallResultType and I'm not sure what should I use.
>
> I'm not 100% sure about this, but my understanding is:
>
> You can declare your function to return "const int", but the caller is
> just going to get an int. Similarly, if you declare your function to return
> "int &", the calling expression has type "int" but is marked as an lvalue.
>
> On the other hand, if you return "const int *", dropping the const would
> be incorrect. (And the same is actually true for "const int &".)
>
> Basically, if you want to know the type the function says it's returning,
> use getResultType(). If you want to know the type that the CallExpr will
> have, use getCallResultType().
>
We should rename 'getResultType' to 'getReturnType' to clarify this (and to
generally follow the standard terminology). This has been on my TODO list
for a long time, but there's always been something more pressing to do...
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20140125/1a51b3e8/attachment.html>
More information about the cfe-dev
mailing list