@@ -641,7 +641,9 @@ class SemaExpressionTree :
641
641
<< E->getSourceRange ();
642
642
return nullptr ;
643
643
}
644
-
644
+
645
+ Expr *makeBinOp (Expr *Op, Expr *LHS, Expr *RHS);
646
+ Expr *foldSequence (Expr *LHS, ArrayRef<Expr*> &S, unsigned MinPrecedence);
645
647
Expr *visitSequenceExpr (SequenceExpr *E);
646
648
647
649
void PreProcessBraceStmt (BraceStmt *BS);
@@ -842,18 +844,26 @@ static InfixData getInfixData(TypeChecker &TC, Expr *E) {
842
844
return InfixData (~0U , Associativity::Left);
843
845
}
844
846
845
- static Expr *makeBinOp (TypeChecker &TC, Expr *Op, Expr *LHS, Expr *RHS) {
847
+ Expr *SemaExpressionTree::makeBinOp (Expr *Op, Expr *LHS, Expr *RHS) {
848
+ if (!LHS || !RHS)
849
+ return nullptr ;
850
+
851
+ // Build the argument to the operation.
846
852
Expr *ArgElts[] = { LHS, RHS };
847
853
auto ArgElts2 = TC.Context .AllocateCopy (MutableArrayRef<Expr*>(ArgElts));
848
854
TupleExpr *Arg = new (TC.Context ) TupleExpr (SourceLoc (),
849
855
ArgElts2, 0 , SourceLoc ());
850
- return new (TC.Context ) BinaryExpr (Op, Arg);
856
+ if (TC.semaTupleExpr (Arg))
857
+ return nullptr ;
858
+
859
+ // Build the operation.
860
+ return visit (new (TC.Context ) BinaryExpr (Op, Arg));
851
861
}
852
862
853
863
// / foldSequence - Take a sequence of expressions and fold a prefix of
854
864
// / it into a tree of BinaryExprs using precedence parsing.
855
- static Expr *foldSequence (TypeChecker &TC, Expr *LHS, ArrayRef<Expr*> &S,
856
- unsigned MinPrecedence) {
865
+ Expr *SemaExpressionTree:: foldSequence (Expr *LHS, ArrayRef<Expr*> &S,
866
+ unsigned MinPrecedence) {
857
867
// Invariant: S is even-sized.
858
868
// Invariant: All elements at even indices are operator references.
859
869
assert (!S.empty ());
@@ -888,7 +898,7 @@ static Expr *foldSequence(TypeChecker &TC, Expr *LHS, ArrayRef<Expr*> &S,
888
898
// both left-associative, fold LHS and RHS immediately.
889
899
if (Op1Info.getPrecedence () > Op2Info.getPrecedence () ||
890
900
(Op1Info == Op2Info && Op1Info.isLeftAssociative ())) {
891
- LHS = makeBinOp (TC, Op1, LHS, RHS);
901
+ LHS = makeBinOp (Op1, LHS, RHS);
892
902
RHS = S[1 ];
893
903
Op1 = Op2;
894
904
Op1Info = Op2Info;
@@ -901,7 +911,7 @@ static Expr *foldSequence(TypeChecker &TC, Expr *LHS, ArrayRef<Expr*> &S,
901
911
// higher-precedence operators starting from this point, then
902
912
// repeat.
903
913
if (Op1Info.getPrecedence () < Op2Info.getPrecedence ()) {
904
- RHS = foldSequence (TC, RHS, S, Op1Info.getPrecedence () + 1 );
914
+ RHS = foldSequence (RHS, S, Op1Info.getPrecedence () + 1 );
905
915
continue ;
906
916
}
907
917
@@ -910,14 +920,14 @@ static Expr *foldSequence(TypeChecker &TC, Expr *LHS, ArrayRef<Expr*> &S,
910
920
// recursively fold operators starting from this point, then
911
921
// immediately fold LHS and RHS.
912
922
if (Op1Info == Op2Info && Op1Info.isRightAssociative ()) {
913
- RHS = foldSequence (TC, RHS, S, Op1Info.getPrecedence ());
914
- LHS = makeBinOp (TC, Op1, LHS, RHS);
923
+ RHS = foldSequence (RHS, S, Op1Info.getPrecedence ());
924
+ LHS = makeBinOp (Op1, LHS, RHS);
915
925
916
926
// If we've drained the entire sequence, we're done.
917
927
if (S.empty ()) return LHS;
918
928
919
929
// Otherwise, start all over with our new LHS.
920
- return foldSequence (TC, LHS, S, MinPrecedence);
930
+ return foldSequence (LHS, S, MinPrecedence);
921
931
}
922
932
923
933
// If we ended up here, it's because we have two operators
@@ -936,33 +946,29 @@ static Expr *foldSequence(TypeChecker &TC, Expr *LHS, ArrayRef<Expr*> &S,
936
946
}
937
947
938
948
// Recover by arbitrarily binding the first two.
939
- LHS = makeBinOp (TC, Op1, LHS, RHS);
940
- return foldSequence (TC, LHS, S, MinPrecedence);
949
+ LHS = makeBinOp (Op1, LHS, RHS);
950
+ return foldSequence (LHS, S, MinPrecedence);
941
951
}
942
952
943
953
// Fold LHS and RHS together and declare completion.
944
- return makeBinOp (TC, Op1, LHS, RHS);
954
+ return makeBinOp (Op1, LHS, RHS);
945
955
}
946
956
947
957
// / foldSequence - Take a SequenceExpr and fold it into a tree of
948
958
// / BinaryExprs using precedence parsing.
949
- Expr *TypeChecker::foldSequence (SequenceExpr *E) {
959
+ Expr *SemaExpressionTree::visitSequenceExpr (SequenceExpr *E) {
950
960
ArrayRef<Expr*> Elts = E->getElements ();
951
961
assert (Elts.size () > 1 && " inadequate number of elements in sequence" );
952
962
assert ((Elts.size () & 1 ) == 1 && " even number of elements in sequence" );
953
963
954
964
Expr *LHS = Elts[0 ];
955
965
Elts = Elts.slice (1 );
956
966
957
- Expr *Result = :: foldSequence (* this , LHS, Elts, /* min precedence*/ 0 );
967
+ Expr *Result = foldSequence (LHS, Elts, /* min precedence*/ 0 );
958
968
assert (Elts.empty ());
959
969
return Result;
960
970
}
961
971
962
- Expr *SemaExpressionTree::visitSequenceExpr (SequenceExpr *E) {
963
- llvm_unreachable (" visiting sequence expression during normal type-checking!" );
964
- }
965
-
966
972
Expr *TypeChecker::recheckTypes (Expr *E) {
967
973
if (!E)
968
974
return nullptr ;
0 commit comments