@@ -254,6 +254,7 @@ mlir::LogicalResult CIRGenFunction::emitSimpleStmt(const Stmt *s,
254
254
case Stmt::NullStmtClass:
255
255
break ;
256
256
case Stmt::CaseStmtClass:
257
+ case Stmt::DefaultStmtClass:
257
258
// If we reached here, we must not handling a switch case in the top level.
258
259
return emitSwitchCase (cast<SwitchCase>(*s),
259
260
/* buildingTopLevelCase=*/ false );
@@ -458,7 +459,7 @@ CIRGenFunction::emitCaseDefaultCascade(const T *stmt, mlir::Type condType,
458
459
if (isa<DefaultStmt>(sub) && isa<CaseStmt>(stmt)) {
459
460
subStmtKind = SubStmtKind::Default;
460
461
builder.createYield (loc);
461
- } else if (isa<CaseStmt>(sub) && isa<DefaultStmt>(stmt)) {
462
+ } else if (isa<CaseStmt>(sub) && isa<DefaultStmt, CaseStmt >(stmt)) {
462
463
subStmtKind = SubStmtKind::Case;
463
464
builder.createYield (loc);
464
465
} else {
@@ -503,8 +504,8 @@ CIRGenFunction::emitCaseDefaultCascade(const T *stmt, mlir::Type condType,
503
504
if (subStmtKind == SubStmtKind::Case) {
504
505
result = emitCaseStmt (*cast<CaseStmt>(sub), condType, buildingTopLevelCase);
505
506
} else if (subStmtKind == SubStmtKind::Default) {
506
- getCIRGenModule (). errorNYI (sub-> getSourceRange ( ), " Default case " );
507
- return mlir::failure ( );
507
+ result = emitDefaultStmt (*cast<DefaultStmt>(sub ), condType,
508
+ buildingTopLevelCase );
508
509
} else if (buildingTopLevelCase) {
509
510
// If we're building a top level case, try to restore the insert point to
510
511
// the case we're building, then we can attach more random stmts to the
@@ -518,19 +519,40 @@ CIRGenFunction::emitCaseDefaultCascade(const T *stmt, mlir::Type condType,
518
519
mlir::LogicalResult CIRGenFunction::emitCaseStmt (const CaseStmt &s,
519
520
mlir::Type condType,
520
521
bool buildingTopLevelCase) {
522
+ cir::CaseOpKind kind;
523
+ mlir::ArrayAttr value;
521
524
llvm::APSInt intVal = s.getLHS ()->EvaluateKnownConstInt (getContext ());
522
- SmallVector<mlir::Attribute, 1 > caseEltValueListAttr;
523
- caseEltValueListAttr.push_back (cir::IntAttr::get (condType, intVal));
524
- mlir::ArrayAttr value = builder.getArrayAttr (caseEltValueListAttr);
525
- if (s.getRHS ()) {
526
- getCIRGenModule ().errorNYI (s.getSourceRange (), " SwitchOp range kind" );
527
- return mlir::failure ();
525
+
526
+ // If the case statement has an RHS value, it is representing a GNU
527
+ // case range statement, where LHS is the beginning of the range
528
+ // and RHS is the end of the range.
529
+ if (const Expr *rhs = s.getRHS ()) {
530
+ llvm::APSInt endVal = rhs->EvaluateKnownConstInt (getContext ());
531
+ value = builder.getArrayAttr ({cir::IntAttr::get (condType, intVal),
532
+ cir::IntAttr::get (condType, endVal)});
533
+ kind = cir::CaseOpKind::Range;
534
+
535
+ // We don't currently fold case range statements with other case statements.
536
+ // TODO(cir): Add this capability. Folding these cases is going to be
537
+ // implemented in CIRSimplify when it is upstreamed.
538
+ assert (!cir::MissingFeatures::foldRangeCase ());
539
+ assert (!cir::MissingFeatures::foldCascadingCases ());
540
+ } else {
541
+ value = builder.getArrayAttr ({cir::IntAttr::get (condType, intVal)});
542
+ kind = cir::CaseOpKind::Equal;
528
543
}
529
- assert (! cir::MissingFeatures::foldCaseStmt ());
530
- return emitCaseDefaultCascade (&s, condType, value, cir::CaseOpKind::Equal ,
544
+
545
+ return emitCaseDefaultCascade (&s, condType, value, kind ,
531
546
buildingTopLevelCase);
532
547
}
533
548
549
+ mlir::LogicalResult CIRGenFunction::emitDefaultStmt (const clang::DefaultStmt &s,
550
+ mlir::Type condType,
551
+ bool buildingTopLevelCase) {
552
+ return emitCaseDefaultCascade (&s, condType, builder.getArrayAttr ({}),
553
+ cir::CaseOpKind::Default, buildingTopLevelCase);
554
+ }
555
+
534
556
mlir::LogicalResult CIRGenFunction::emitSwitchCase (const SwitchCase &s,
535
557
bool buildingTopLevelCase) {
536
558
assert (!condTypeStack.empty () &&
@@ -540,10 +562,9 @@ mlir::LogicalResult CIRGenFunction::emitSwitchCase(const SwitchCase &s,
540
562
return emitCaseStmt (cast<CaseStmt>(s), condTypeStack.back (),
541
563
buildingTopLevelCase);
542
564
543
- if (s.getStmtClass () == Stmt::DefaultStmtClass) {
544
- getCIRGenModule ().errorNYI (s.getSourceRange (), " Default case" );
545
- return mlir::failure ();
546
- }
565
+ if (s.getStmtClass () == Stmt::DefaultStmtClass)
566
+ return emitDefaultStmt (cast<DefaultStmt>(s), condTypeStack.back (),
567
+ buildingTopLevelCase);
547
568
548
569
llvm_unreachable (" expect case or default stmt" );
549
570
}
0 commit comments