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 
12 #include "DebugOptions.h"
13 
14 using namespace llvm;
15 
16 #ifndef STRICT_FIXED_SIZE_VECTORS
17 namespace {
18 struct CreateScalableErrorAsWarning {
19   /// The ScalableErrorAsWarning is a temporary measure to suppress errors from
20   /// using the wrong interface on a scalable vector.
call__anon609bc8b30111::CreateScalableErrorAsWarning21   static void *call() {
22     return new cl::opt<bool>(
23         "treat-scalable-fixed-error-as-warning", cl::Hidden, cl::init(false),
24         cl::desc(
25             "Treat issues where a fixed-width property is requested from a "
26             "scalable type as a warning, instead of an error."),
27         cl::ZeroOrMore);
28   }
29 };
30 } // namespace
31 static ManagedStatic<cl::opt<bool>, CreateScalableErrorAsWarning>
32     ScalableErrorAsWarning;
initTypeSizeOptions()33 void llvm::initTypeSizeOptions() { *ScalableErrorAsWarning; }
34 #else
initTypeSizeOptions()35 void llvm::initTypeSizeOptions() {}
36 #endif
37 
reportInvalidSizeRequest(const char * Msg)38 void llvm::reportInvalidSizeRequest(const char *Msg) {
39 #ifndef STRICT_FIXED_SIZE_VECTORS
40   if (*ScalableErrorAsWarning) {
41     WithColor::warning() << "Invalid size request on a scalable vector; " << Msg
42                          << "\n";
43     return;
44   }
45 #endif
46   report_fatal_error("Invalid size request on a scalable vector.");
47 }
48 
operator TypeSize::ScalarTy() const49 TypeSize::operator TypeSize::ScalarTy() const {
50   if (isScalable()) {
51     reportInvalidSizeRequest(
52         "Cannot implicitly convert a scalable size to a fixed-width size in "
53         "`TypeSize::operator ScalarTy()`");
54     return getKnownMinValue();
55   }
56   return getFixedValue();
57 }
58