1 //===- TypeSize.cpp - Wrapper around type sizes------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "llvm/Support/TypeSize.h"
10 #include "llvm/Support/CommandLine.h"
11 #include "llvm/Support/WithColor.h"
12 
13 #include "DebugOptions.h"
14 
15 using namespace llvm;
16 
17 #ifndef STRICT_FIXED_SIZE_VECTORS
18 namespace {
19 struct CreateScalableErrorAsWarning {
20   /// The ScalableErrorAsWarning is a temporary measure to suppress errors from
21   /// using the wrong interface on a scalable vector.
22   static void *call() {
23     return new cl::opt<bool>(
24         "treat-scalable-fixed-error-as-warning", cl::Hidden, cl::init(false),
25         cl::desc(
26             "Treat issues where a fixed-width property is requested from a "
27             "scalable type as a warning, instead of an error."),
28         cl::ZeroOrMore);
29   }
30 };
31 } // namespace
32 static ManagedStatic<cl::opt<bool>, CreateScalableErrorAsWarning>
33     ScalableErrorAsWarning;
34 void llvm::initTypeSizeOptions() { *ScalableErrorAsWarning; }
35 #else
36 void llvm::initTypeSizeOptions() {}
37 #endif
38 
39 void llvm::reportInvalidSizeRequest(const char *Msg) {
40 #ifndef STRICT_FIXED_SIZE_VECTORS
41   if (*ScalableErrorAsWarning) {
42     WithColor::warning() << "Invalid size request on a scalable vector; " << Msg
43                          << "\n";
44     return;
45   }
46 #endif
47   report_fatal_error("Invalid size request on a scalable vector.");
48 }
49 
50 TypeSize::operator TypeSize::ScalarTy() const {
51   if (isScalable()) {
52     reportInvalidSizeRequest(
53         "Cannot implicitly convert a scalable size to a fixed-width size in "
54         "`TypeSize::operator ScalarTy()`");
55     return getKnownMinValue();
56   }
57   return getFixedValue();
58 }
59