[cfe-dev] methods to access "base" and "index" in ArraySubscriptExpr

Ted Kremenek kremenek at apple.com
Fri Aug 17 09:58:11 PDT 2007

Recently I added a comment to the class definition for  
ArraySubscriptExpr (in Sema/Expr.h) noting that the true "base" of the  
array may actually be returned by "getIdx()" if the array happened to  
be written in the source (for example) as "4[A]" instead of "A[4]."  I  
think this is confusing, and may cause glitches down the line for  
semantic analysis tools built on the frontend.

I propose renaming the current getBase and getIdx methods to "getLHS"  
and "getRHS" respectively, as the current interpretation of these  
functions has more do with source code syntax (their position in the  
source code) rather than semantics.  I then propose adding new getBase  
and getIdx functions that look something like this:

Expr *getBase() { return (Base->getType()->isPointerType()) ? Base :  
Idx; }
Expr *getIdx() { return (!Idx->getType()->IsPointerType()) ? Idx :  
Base; }

Naturally, along with these changes I suggest changing the member  
variables "Base" and "Idx" to "LHS" and "RHS" respectively.

Comments?  One disadvantage that I see of this change is that using  
getBase() and getIdx() in tandem may require some extra unnecessary  
checking (as the same check is performed in both functions).  For such  
cases, we could consider adding an extra method that returns both the  
Base and the Idx expressions in a std::pair (would there be an  
efficiency issue here?).

More information about the cfe-dev mailing list