souffle  2.0.2-371-g6315b36
UnionType.h
Go to the documentation of this file.
1 /*
2  * Souffle - A Datalog Compiler
3  * Copyright (c) 2013, 2014, 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 UnionType.h
12  *
13  * Defines the union type class
14  *
15  ***********************************************************************/
16 
17 #pragma once
18 
19 #include "ast/Node.h"
20 #include "ast/QualifiedName.h"
21 #include "ast/Type.h"
22 #include "parser/SrcLocation.h"
24 #include <algorithm>
25 #include <cstddef>
26 #include <iostream>
27 #include <string>
28 #include <utility>
29 #include <vector>
30 
31 namespace souffle::ast {
32 
33 /**
34  * @class UnionType
35  * @brief The union type class
36  *
37  * Example:
38  * .type A = B1 | B2 | ... | Bk
39  *
40  * A union type combines multiple types into a new super type.
41  * Each of the enumerated types become a sub-type of the new
42  * union type.
43  */
44 class UnionType : public Type {
45 public:
46  UnionType(QualifiedName name, std::vector<QualifiedName> types, SrcLocation loc = {})
47  : Type(std::move(name), std::move(loc)), types(std::move(types)) {}
48 
49  /** Return list of unioned types */
50  const std::vector<QualifiedName>& getTypes() const {
51  return types;
52  }
53 
54  /** Add another unioned type */
55  void add(QualifiedName type) {
56  types.push_back(std::move(type));
57  }
58 
59  /** Set type */
60  void setType(size_t idx, QualifiedName type) {
61  types.at(idx) = std::move(type);
62  }
63 
64  UnionType* clone() const override {
65  return new UnionType(getQualifiedName(), types, getSrcLoc());
66  }
67 
68 protected:
69  void print(std::ostream& os) const override {
70  os << ".type " << getQualifiedName() << " = " << join(types, " | ");
71  }
72 
73  bool equal(const Node& node) const override {
74  const auto& other = static_cast<const UnionType&>(node);
75  return getQualifiedName() == other.getQualifiedName() && types == other.types;
76  }
77 
78 private:
79  /** List of unioned types */
80  std::vector<QualifiedName> types;
81 };
82 
83 } // namespace souffle::ast
SrcLocation.h
types
std::vector< Own< ast::Type > > types
Definition: ComponentInstantiation.cpp:64
souffle::ast::analysis::UnionType
A union type combining a list of types into a new, aggregated type.
Definition: TypeSystem.h:146
souffle::clone
auto clone(const std::vector< A * > &xs)
Definition: ContainerUtil.h:172
souffle::ast::analysis::UnionType::UnionType
UnionType(const TypeEnvironment &environment, const QualifiedName &name, std::vector< const Type * > elementTypes={})
Definition: TypeSystem.h:162
souffle::join
detail::joined_sequence< Iter, Printer > join(const Iter &a, const Iter &b, const std::string &sep, const Printer &p)
Creates an object to be forwarded to some output stream for printing sequences of elements interspers...
Definition: StreamUtil.h:175
Type.h
Node.h
souffle::ast::analysis::Type::name
QualifiedName name
Definition: TypeSystem.h:92
souffle::ast::analysis::Type::Type
Type(const Type &other)=delete
souffle::ast::Node
Abstract class for syntactic elements in an input program.
Definition: Node.h:40
QualifiedName.h
StreamUtil.h
souffle::ast
Definition: Aggregator.h:35
souffle::ast::analysis::UnionType::print
void print(std::ostream &out) const override
Definition: TypeSystem.cpp:38
souffle::ast::QualifiedName
Qualified Name class defines fully/partially qualified names to identify objects in components.
Definition: QualifiedName.h:39
souffle::ast::UnionType::getTypes
const std::vector< QualifiedName > & getTypes() const
Return list of unioned types.
Definition: UnionType.h:56
std::type
ElementType type
Definition: span.h:640