]> granicus.if.org Git - clang/commitdiff
AST work to support [C++] [IRgen] for ?: with missing LHS
authorFariborz Jahanian <fjahanian@apple.com>
Tue, 31 Aug 2010 18:02:20 +0000 (18:02 +0000)
committerFariborz Jahanian <fjahanian@apple.com>
Tue, 31 Aug 2010 18:02:20 +0000 (18:02 +0000)
This is also pr7726 and wip. No change in functionality
at this time.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@112612 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/AST/Expr.h
lib/AST/StmtDumper.cpp
lib/Rewrite/RewriteObjC.cpp
lib/Sema/SemaExpr.cpp
lib/Serialization/ASTReaderStmt.cpp
lib/Serialization/ASTWriterStmt.cpp

index 25624569240e39de177c58f227c118d1f9c4c220..48130becf3b5b6fd11540a3809f206f3b557f0fe 100644 (file)
@@ -2391,10 +2391,11 @@ public:
 class ConditionalOperator : public Expr {
   enum { COND, LHS, RHS, END_EXPR };
   Stmt* SubExprs[END_EXPR]; // Left/Middle/Right hand sides.
+  Stmt* Save;
   SourceLocation QuestionLoc, ColonLoc;
 public:
   ConditionalOperator(Expr *cond, SourceLocation QLoc, Expr *lhs,
-                      SourceLocation CLoc, Expr *rhs, QualType t)
+                      SourceLocation CLoc, Expr *rhs, Expr *save, QualType t)
     : Expr(ConditionalOperatorClass, t,
            // FIXME: the type of the conditional operator doesn't
            // depend on the type of the conditional, but the standard
@@ -2408,6 +2409,7 @@ public:
     SubExprs[COND] = cond;
     SubExprs[LHS] = lhs;
     SubExprs[RHS] = rhs;
+    Save = save;
   }
 
   /// \brief Build an empty conditional operator.
@@ -2420,25 +2422,31 @@ public:
   void setCond(Expr *E) { SubExprs[COND] = E; }
 
   // getTrueExpr - Return the subexpression representing the value of the ?:
-  //  expression if the condition evaluates to true.  In most cases this value
-  //  will be the same as getLHS() except a GCC extension allows the left
-  //  subexpression to be omitted, and instead of the condition be returned.
-  //  e.g: x ?: y is shorthand for x ? x : y, except that the expression "x"
-  //  is only evaluated once.
+  //  expression if the condition evaluates to true.  
   Expr *getTrueExpr() const {
-    return cast<Expr>(SubExprs[LHS] ? SubExprs[LHS] : SubExprs[COND]);
+    return cast<Expr>(!Save ? SubExprs[LHS] : SubExprs[COND]);
   }
 
   // getFalseExpr - Return the subexpression representing the value of the ?:
   // expression if the condition evaluates to false. This is the same as getRHS.
   Expr *getFalseExpr() const { return cast<Expr>(SubExprs[RHS]); }
-
-  Expr *getLHS() const { return cast_or_null<Expr>(SubExprs[LHS]); }
+  
+  // getSaveExpr - In most cases this value will be null. Except a GCC extension 
+  // allows the left subexpression to be omitted, and instead of that condition 
+  // be returned. e.g: x ?: y is shorthand for x ? x : y, except that the 
+  // expression "x" is only evaluated once. Under this senario, this function
+  // returns the original, non-converted condition expression for the ?:operator
+  Expr *getSaveExpr() const { return Save? cast<Expr>(Save) : (Expr*)0; }
+
+  Expr *getLHS() const { return Save ? 0 : cast<Expr>(SubExprs[LHS]); }
   void setLHS(Expr *E) { SubExprs[LHS] = E; }
 
   Expr *getRHS() const { return cast<Expr>(SubExprs[RHS]); }
   void setRHS(Expr *E) { SubExprs[RHS] = E; }
 
+  Expr *getSAVE() const { return Save? cast<Expr>(Save) : (Expr*)0; }
+  void setSAVE(Expr *E) { Save = E; }
+  
   SourceLocation getQuestionLoc() const { return QuestionLoc; }
   void setQuestionLoc(SourceLocation L) { QuestionLoc = L; }
 
index 850bba40b7bb38517a69c0df2b6e2b69163aac09..5c236a45a690cfa8ca072b4c8f247000785858ac 100644 (file)
@@ -65,6 +65,13 @@ namespace  {
               OS << '\n';
               DumpSubTree(*CI++);
             }
+            if (const ConditionalOperator *CO = 
+                  dyn_cast<ConditionalOperator>(S)) {
+              if (CO->getSAVE()) {
+                OS << '\n';
+                DumpSubTree(CO->getSAVE());
+              }
+            }
           }
         }
         OS << ')';
index 4a7de4bed0d691bccda3c07d0011e16c059fef1b..578a063614a1b651ba4f8f5c61012d9e29243388 100644 (file)
@@ -3041,8 +3041,10 @@ Stmt *RewriteObjC::SynthMessageExpr(ObjCMessageExpr *Exp,
     ConditionalOperator *CondExpr =
       new (Context) ConditionalOperator(lessThanExpr,
                                         SourceLocation(), CE,
-                                        SourceLocation(), STCE, returnType);
-    ReplacingStmt = new (Context) ParenExpr(SourceLocation(), SourceLocation(), CondExpr);
+                                        SourceLocation(), STCE, (Expr*)0,
+                                        returnType);
+    ReplacingStmt = new (Context) ParenExpr(SourceLocation(), SourceLocation(), 
+                                            CondExpr);
   }
   // delete Exp; leak for now, see RewritePropertySetter() usage for more info.
   return ReplacingStmt;
@@ -4566,7 +4568,8 @@ Stmt *RewriteObjC::SynthesizeBlockCall(CallExpr *Exp, const Expr *BlockExp) {
     ConditionalOperator *CondExpr =
       new (Context) ConditionalOperator(CONDExp,
                                       SourceLocation(), cast<Expr>(LHSStmt),
-                                      SourceLocation(), cast<Expr>(RHSStmt), 
+                                      SourceLocation(), cast<Expr>(RHSStmt),
+                                      (Expr*)0,
                                       Exp->getType());
     return CondExpr;
   } else if (const ObjCIvarRefExpr *IRE = dyn_cast<ObjCIvarRefExpr>(BlockExp)) {
index 455730c909bbd3925de839d9c6aef391d58ddd08..ca9c5dc56e8d279be0f4ae159063a1645bb634e5 100644 (file)
@@ -4521,8 +4521,10 @@ ExprResult Sema::ActOnConditionalOp(SourceLocation QuestionLoc,
   // If this is the gnu "x ?: y" extension, analyze the types as though the LHS
   // was the condition.
   bool isLHSNull = LHSExpr == 0;
-  if (isLHSNull)
-    LHSExpr = CondExpr;
+  Expr *SAVEExpr = 0;
+  if (isLHSNull) {
+    LHSExpr = SAVEExpr = CondExpr;
+  }
 
   QualType result = CheckConditionalOperands(CondExpr, LHSExpr,
                                              RHSExpr, QuestionLoc);
@@ -4530,8 +4532,9 @@ ExprResult Sema::ActOnConditionalOp(SourceLocation QuestionLoc,
     return ExprError();
 
   return Owned(new (Context) ConditionalOperator(CondExpr, QuestionLoc,
-                                                 isLHSNull ? 0 : LHSExpr,
-                                                 ColonLoc, RHSExpr, result));
+                                                 LHSExpr, ColonLoc, 
+                                                 RHSExpr, SAVEExpr,
+                                                 result));
 }
 
 // CheckPointerTypesForAssignment - This is a very tricky routine (despite
index ec227e2836d82427d6027d505fdc19ebff6ece1f..da07f8c4cd3843a8e71d09361ef8ab182d3e01a7 100644 (file)
@@ -592,6 +592,7 @@ void ASTStmtReader::VisitConditionalOperator(ConditionalOperator *E) {
   E->setCond(Reader.ReadSubExpr());
   E->setLHS(Reader.ReadSubExpr());
   E->setRHS(Reader.ReadSubExpr());
+  E->setSAVE(Reader.ReadSubExpr());
   E->setQuestionLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
   E->setColonLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
 }
index 4bde550e0d78bbf6dffbfab545451f5d0c14ee52..922a1cda38b440b79770f8c273c59ed7dd772a3c 100644 (file)
@@ -598,6 +598,7 @@ void ASTStmtWriter::VisitConditionalOperator(ConditionalOperator *E) {
   Writer.AddStmt(E->getCond());
   Writer.AddStmt(E->getLHS());
   Writer.AddStmt(E->getRHS());
+  Writer.AddStmt(E->getSAVE());
   Writer.AddSourceLocation(E->getQuestionLoc(), Record);
   Writer.AddSourceLocation(E->getColonLoc(), Record);
   Code = serialization::EXPR_CONDITIONAL_OPERATOR;