getLHS() and getRHS()

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

getLHS() and getRHS()

Rambo
Hi,

Now I study this page,the link is http://clang.llvm.org/doxygen/classclang_1_1ArraySubscriptExpr.htmland be puzzled in this sentence "getBase() and getIdx() always present the normalized view: A[4]. In this case getBase() returns "A" and getIdx() returns "4"."The sentence can be found at "Expr * getLHS ()"
Can you tell me How it return "A" by getBase() and return "4" by getIdx().

Regards,
Rambo
Reply | Threaded
Open this post in threaded view
|

Re: getLHS() and getRHS()

Tim Northover-2
Hi Rambo,

> Can you tell me How it return "A" by getBase() and return "4" by getIdx().

If I've understood the question properly, getBase() returns the
pointer operand and getIdx() returns the integral one regardless of
which is outside the brackets. So even if you'd written "4[A]" the
type of A must be a pointer and getBase can tell which it is.

Tim.
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: getLHS() and getRHS()

Rambo
Thanks for your respose.You seem the type of return for getLHS() is Expr *,How can you get A form A[4].As far as I know ,I
cann't get a std::string form such type as Expr *.
Reply | Threaded
Open this post in threaded view
|

Re: getLHS() and getRHS()

Tim Northover-2
On Mon, Nov 19, 2012 at 2:17 PM, Rambo <[hidden email]> wrote:
> Thanks for your respose.You seem the type of return for getLHS() is Expr
> *,How can you get A form A[4].As far as I know ,I
> cann't get a std::string form such type as Expr *.

Ah, I'm afraid I don't know that. Hopefully someone else will be along
with an answer.

Tim.
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: getLHS() and getRHS()

Rambo
I can do it ,the codes as follows:for A[4]
 Expr *rhs=Decl2->getRHS();
         if(ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(rhs))
         {
           Curr=ICE->getSubExpr();
           if(DeclRefExpr *ICE1 = dyn_cast<DeclRefExpr>(Curr))
             De=ICE1->getNameInfo();
 //          llvm::errs()<<De.getAsString()<<"\n";
         }
This is get "A"and
Expr *lhs=Decl2->getLHS();
         if(ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(lhs))
         {
           Curr=ICE->getSubExpr();
           if(DeclRefExpr *ICE1 = dyn_cast<DeclRefExpr>(Curr))
             De=ICE1->getNameInfo();
//           llvm::errs()<<De.getAsString()<<"\n";
         }
This is get 4.
Thank your for your reply again.


Rambo
Reply | Threaded
Open this post in threaded view
|

Re: getLHS() and getRHS()

Yang Chen
Rambo wrote:

> I can do it ,the codes as follows:for A[4]
>  Expr *rhs=Decl2->getRHS();
>          if(ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(rhs))
>          {
>            Curr=ICE->getSubExpr();
>            if(DeclRefExpr *ICE1 = dyn_cast<DeclRefExpr>(Curr))
>              De=ICE1->getNameInfo();
>  //          llvm::errs()<<De.getAsString()&lt;&lt;&quot;\n&quot;;
>          }
> This is get &quot;A&quot;and
> Expr *lhs=Decl2->getLHS();
>          if(ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(lhs))
>          {
>            Curr=ICE->getSubExpr();
>            if(DeclRefExpr *ICE1 = dyn_cast<DeclRefExpr>(Curr))
>              De=ICE1->getNameInfo();
> //           llvm::errs()<<De.getAsString()<<"\n";
>          }
> This is get 4.
>  

A couple of member functions of Expr would be helpful to you -

IgnoreImpCasts, IgnoreParens, IgnoreParenCasts

http://clang.llvm.org/doxygen/classclang_1_1Expr.html

- Yang

> Thank your for your reply again.
>
>
> Rambo
>
>
>
> --
> View this message in context: http://clang-developers.42468.n3.nabble.com/getLHS-and-getRHS-tp4028364p4028368.html
> Sent from the Clang Developers mailing list archive at Nabble.com.
> _______________________________________________
> cfe-dev mailing list
> [hidden email]
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
>  

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: getLHS() and getRHS()

Rambo
As far as I know,IgnoreImpCasts, IgnoreParens, IgnoreParenCasts don't help to my question.http://a6128786ad6c6ad61b66ad4ddf09b1bd8this link show us that "IgnoreParens - Ignore parentheses. If this Expr is a ParenExpr, return its subexpression. If that subexpression is also a ParenExpr, then this method recursively returns its subexpression, and so forth. Otherwise, the method returns the current Expr. "I doubt your solution exactly.
Reply | Threaded
Open this post in threaded view
|

Re: getLHS() and getRHS()

Yang Chen
Rambo wrote:
> As far as I know,IgnoreImpCasts, IgnoreParens, IgnoreParenCasts don't help to
> my question. http://a6128786ad6c6ad61b66ad4ddf09b1bd8
> <http://a6128786ad6c6ad61b66ad4ddf09b1bd8>  this link show us that
> "IgnoreParens - Ignore parentheses. If this Expr is a ParenExpr, return its
> subexpression. If that subexpression is also a ParenExpr, then this method
> recursively returns its subexpression, and so forth. Otherwise, the method
> returns the current Expr. "I doubt your solution exactly.
>
>  

I meant that often you don't need to explicitly dyn_cast an Expr to
ImplicitCastExpr. Instead, you can use IgnoreXXX function to strip off
casts and/or parentheses. For example, your code was:

----------------------------------------------------------------------------------------------

 Expr *rhs=Decl2->getRHS();
         if(ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(rhs))
         {
           Curr=ICE->getSubExpr();
           if(DeclRefExpr *ICE1 = dyn_cast<DeclRefExpr>(Curr))
             De=ICE1->getNameInfo();
         ...


---------------------------------------------------------------------------------------------


It could handle cases like "A[4]" based on your example. But how about
"A[(4)]"? You might say that "A[(4)]" is ugly and does not exist in any
real code base, but there is no such guarantee. Anyway, since I don't
know what you are trying to achieve, just my 2 cents.

- Yang

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev