1 //===- Builder.cpp - Builder definitions ----------------------------------===//
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 "mlir/TableGen/Builder.h"
10 #include "llvm/TableGen/Error.h"
11 #include "llvm/TableGen/Record.h"
12 
13 using namespace mlir;
14 using namespace mlir::tblgen;
15 
16 //===----------------------------------------------------------------------===//
17 // Builder::Parameter
18 //===----------------------------------------------------------------------===//
19 
20 /// Return a string containing the C++ type of this parameter.
21 StringRef Builder::Parameter::getCppType() const {
22   if (const auto *stringInit = dyn_cast<llvm::StringInit>(def))
23     return stringInit->getValue();
24   const llvm::Record *record = cast<llvm::DefInit>(def)->getDef();
25   return record->getValueAsString("type");
26 }
27 
28 /// Return an optional string containing the default value to use for this
29 /// parameter.
30 Optional<StringRef> Builder::Parameter::getDefaultValue() const {
31   if (isa<llvm::StringInit>(def))
32     return llvm::None;
33   const llvm::Record *record = cast<llvm::DefInit>(def)->getDef();
34   Optional<StringRef> value = record->getValueAsOptionalString("defaultValue");
35   return value && !value->empty() ? value : llvm::None;
36 }
37 
38 //===----------------------------------------------------------------------===//
39 // Builder
40 //===----------------------------------------------------------------------===//
41 
42 Builder::Builder(const llvm::Record *record, ArrayRef<SMLoc> loc)
43     : def(record) {
44   // Initialize the parameters of the builder.
45   const llvm::DagInit *dag = def->getValueAsDag("dagParams");
46   auto *defInit = dyn_cast<llvm::DefInit>(dag->getOperator());
47   if (!defInit || !defInit->getDef()->getName().equals("ins"))
48     PrintFatalError(def->getLoc(), "expected 'ins' in builders");
49 
50   bool seenDefaultValue = false;
51   for (unsigned i = 0, e = dag->getNumArgs(); i < e; ++i) {
52     const llvm::StringInit *paramName = dag->getArgName(i);
53     const llvm::Init *paramValue = dag->getArg(i);
54     Parameter param(paramName ? paramName->getValue() : Optional<StringRef>(),
55                     paramValue);
56 
57     // Similarly to C++, once an argument with a default value is detected, the
58     // following arguments must have default values as well.
59     if (param.getDefaultValue()) {
60       seenDefaultValue = true;
61     } else if (seenDefaultValue) {
62       PrintFatalError(loc,
63                       "expected an argument with default value after other "
64                       "arguments with default values");
65     }
66     parameters.emplace_back(param);
67   }
68 }
69 
70 /// Return an optional string containing the body of the builder.
71 Optional<StringRef> Builder::getBody() const {
72   Optional<StringRef> body = def->getValueAsOptionalString("body");
73   return body && !body->empty() ? body : llvm::None;
74 }
75