blob: 0a773f1b7b0fe550bbe4af2930d62b34b77726ca [file] [log] [blame]
Chris Lattner36b4ed12018-07-04 10:43:29 -07001//===- Attributes.cpp - MLIR Attribute Implementation ---------------------===//
2//
3// Copyright 2019 The MLIR Authors.
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16// =============================================================================
17
18#include "mlir/IR/Attributes.h"
19#include "llvm/Support/raw_ostream.h"
20#include "mlir/Support/STLExtras.h"
21using namespace mlir;
22
23void Attribute::print(raw_ostream &os) const {
24 switch (getKind()) {
25 case Kind::Bool:
26 os << (cast<BoolAttr>(this)->getValue() ? "true" : "false");
27 break;
28 case Kind::Integer:
29 os << cast<IntegerAttr>(this)->getValue();
30 break;
31 case Kind::Float:
32 // FIXME: this isn't precise, we should print with a hex format.
33 os << cast<FloatAttr>(this)->getValue();
34 break;
35 case Kind::String:
36 // FIXME: should escape the string.
37 os << '"' << cast<StringAttr>(this)->getValue() << '"';
38 break;
39 case Kind::Array: {
40 auto elts = cast<ArrayAttr>(this)->getValue();
41 if (elts.empty())
42 os << "[]";
43 else {
44 os << "[ ";
45 interleave(elts,
46 [&](Attribute *attr) { attr->print(os); },
47 [&]() { os << ", "; });
48 os << " ]";
49 }
50 break;
51 }
52 }
53}
54
55void Attribute::dump() const {
56 print(llvm::errs());
57}