blob: df23424ed25dc2741d0ea14822d56131efef22d2 [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();
Chris Lattner7121b802018-07-04 20:45:39 -070041 os << '[';
42 interleave(elts,
43 [&](Attribute *attr) { attr->print(os); },
44 [&]() { os << ", "; });
45 os << ']';
Chris Lattner36b4ed12018-07-04 10:43:29 -070046 break;
47 }
48 }
49}
50
51void Attribute::dump() const {
52 print(llvm::errs());
53}