getLHS() and getRHS()

Hi,

Now I study this page,the link is
http://clang.llvm.org/doxygen/classclang_1_1ArraySubscriptExpr.html
<http://clang.llvm.org/doxygen/classclang_1_1ArraySubscriptExpr.html> and
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

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.

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.

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.
Thank your for your reply again.

Rambo

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

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.

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: