souffle  2.0.2-371-g6315b36
TypeCast.h
Go to the documentation of this file.
1 /*
2  * Souffle - A Datalog Compiler
3  * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved
4  * Licensed under the Universal Permissive License v 1.0 as shown at:
5  * - https://opensource.org/licenses/UPL
6  * - <souffle root>/licenses/SOUFFLE-UPL.txt
7  */
8 
9 /************************************************************************
10  *
11  * @file TypeCast.h
12  *
13  * Defines the type cast class
14  *
15  ***********************************************************************/
16 
17 #pragma once
18 
19 #include "ast/Argument.h"
20 #include "ast/Node.h"
21 #include "ast/QualifiedName.h"
22 #include "ast/utility/NodeMapper.h"
23 #include "parser/SrcLocation.h"
27 #include <memory>
28 #include <ostream>
29 #include <string>
30 #include <utility>
31 #include <vector>
32 
33 namespace souffle::ast {
34 
35 /**
36  * @class TypeCast
37  * @brief Defines a type cast class for expressions
38  */
39 
40 class TypeCast : public Argument {
41 public:
42  TypeCast(Own<Argument> value, QualifiedName type, SrcLocation loc = {})
43  : Argument(std::move(loc)), value(std::move(value)), type(std::move(type)) {}
44 
45  /** Return value */
46  Argument* getValue() const {
47  return value.get();
48  }
49 
50  /** Return cast type */
51  const QualifiedName& getType() const {
52  return type;
53  }
54 
55  /** Set cast type */
56  void setType(const QualifiedName& type) {
57  this->type = type;
58  }
59 
60  std::vector<const Node*> getChildNodes() const override {
61  auto res = Argument::getChildNodes();
62  res.push_back(value.get());
63  return res;
64  }
65 
66  TypeCast* clone() const override {
67  return new TypeCast(souffle::clone(value), type, getSrcLoc());
68  }
69 
70  void apply(const NodeMapper& map) override {
71  value = map(std::move(value));
72  }
73 
74 protected:
75  void print(std::ostream& os) const override {
76  os << tfm::format("as(%s, %s)", *value, type);
77  }
78 
79  bool equal(const Node& node) const override {
80  const auto& other = static_cast<const TypeCast&>(node);
81  return type == other.type && equal_ptr(value, other.value);
82  }
83 
84  /** Casted value */
86 
87  /** Cast type */
89 };
90 
91 } // namespace souffle::ast
souffle::ast::TypeCast::clone
TypeCast * clone() const override
Create clone.
Definition: TypeCast.h:72
souffle::ast::TypeCast::TypeCast
TypeCast(Own< Argument > value, QualifiedName type, SrcLocation loc={})
Definition: TypeCast.h:48
tinyformat::format
void format(std::ostream &out, const char *fmt)
Definition: tinyformat.h:1089
souffle::ast::TypeCast::value
Own< Argument > value
Casted value.
Definition: TypeCast.h:91
souffle::ast::TypeCast::setType
void setType(const QualifiedName &type)
Set cast type.
Definition: TypeCast.h:62
SrcLocation.h
souffle::ast::NodeMapper
An abstract class for manipulating AST Nodes by substitution.
Definition: NodeMapper.h:36
souffle::Own
std::unique_ptr< A > Own
Definition: ContainerUtil.h:42
souffle::ast::TypeCast::getType
const QualifiedName & getType() const
Return cast type.
Definition: TypeCast.h:57
souffle::map
auto map(const std::vector< A > &xs, F &&f)
Applies a function to each element of a vector and returns the results.
Definition: ContainerUtil.h:158
MiscUtil.h
tinyformat.h
souffle::ast::TypeCast::equal
bool equal(const Node &node) const override
Abstract equality check for two AST nodes.
Definition: TypeCast.h:85
NodeMapper.h
souffle::ast::Argument
An abstract class for arguments.
Definition: Argument.h:33
Argument.h
souffle::clone
auto clone(const std::vector< A * > &xs)
Definition: ContainerUtil.h:172
ContainerUtil.h
souffle::ast::TypeCast
Defines a type cast class for expressions.
Definition: TypeCast.h:46
souffle::ast::TypeCast::print
void print(std::ostream &os) const override
Output to a given output stream.
Definition: TypeCast.h:81
souffle::equal_ptr
bool equal_ptr(const T *a, const T *b)
Compares two values referenced by a pointer where the case where both pointers are null is also consi...
Definition: MiscUtil.h:130
souffle::ast::TypeCast::getChildNodes
std::vector< const Node * > getChildNodes() const override
Obtain a list of all embedded AST child nodes.
Definition: TypeCast.h:66
souffle::ast::TypeCast::type
QualifiedName type
Cast type.
Definition: TypeCast.h:94
Node.h
souffle::ast::TypeCast::apply
void apply(const NodeMapper &map) override
Apply the mapper to all child nodes.
Definition: TypeCast.h:76
souffle::ast::Node::getChildNodes
virtual std::vector< const Node * > getChildNodes() const
Obtain a list of all embedded AST child nodes.
Definition: Node.h:82
souffle::ast::Node
Abstract class for syntactic elements in an input program.
Definition: Node.h:40
QualifiedName.h
souffle::ast::TypeCast::getValue
Argument * getValue() const
Return value.
Definition: TypeCast.h:52
souffle::ast::Node::getSrcLoc
const SrcLocation & getSrcLoc() const
Return source location of the Node.
Definition: Node.h:46
souffle::ast
Definition: Aggregator.h:35
souffle::ast::QualifiedName
Qualified Name class defines fully/partially qualified names to identify objects in components.
Definition: QualifiedName.h:39