souffle  2.0.2-371-g6315b36
NumericConstant.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 NumericConstant.h
12  *
13  * Defines the numeric constant class
14  *
15  ***********************************************************************/
16 
17 #pragma once
18 
19 #include "ast/Constant.h"
20 #include "ast/Node.h"
21 #include "parser/SrcLocation.h"
22 #include "souffle/RamTypes.h"
23 #include <cassert>
24 #include <optional>
25 #include <string>
26 #include <utility>
27 
28 namespace souffle::ast {
29 
30 /**
31  * Numeric Constant
32  *
33  * The constant can be initialized with type.
34  * If this is the case, the typesystem will be forced to use it.
35  * Otherwise the type is inferred from context.
36  */
37 class NumericConstant : public Constant {
38 public:
39  enum class Type { Int, Uint, Float };
40 
41  NumericConstant(RamSigned value) : Constant(std::to_string(value)), fixedType(Type::Int) {}
42 
43  NumericConstant(std::string constant, SrcLocation loc) : Constant(std::move(constant)) {
44  setSrcLoc(std::move(loc));
45  }
46 
47  NumericConstant(std::string constant, std::optional<Type> fixedType = std::nullopt, SrcLocation loc = {})
48  : Constant(std::move(constant)), fixedType(fixedType) {
49  setSrcLoc(std::move(loc));
50  }
51 
52  NumericConstant* clone() const override {
54  copy->setSrcLoc(getSrcLoc());
55  if (finalTranslatorType.has_value()) {
56  copy->setFinalType(finalTranslatorType.value());
57  }
58  return copy;
59  }
60 
61  const std::optional<Type>& getFixedType() const {
62  return fixedType;
63  }
64 
65  void setFinalType(Type newType) {
66  finalTranslatorType = newType;
67  }
68 
69  std::optional<Type> getFinalType() const {
70  return finalTranslatorType;
71  }
72 
73 protected:
74  bool equal(const Node& node) const override {
75  const auto& other = static_cast<const NumericConstant&>(node);
76  return Constant::equal(node) && fixedType == other.fixedType;
77  }
78 
79 private:
80  std::optional<Type> fixedType;
81 
82  // TODO (azreika): remove after refactoring translator
83  std::optional<Type> finalTranslatorType;
84 };
85 
86 } // namespace souffle::ast
souffle::ast::NumericConstant::getFixedType
const std::optional< Type > & getFixedType() const
Definition: NumericConstant.h:73
SrcLocation.h
souffle::ast::Constant::getConstant
const std::string & getConstant() const
Get string representation of Constant.
Definition: Constant.h:43
souffle::ast::NumericConstant::setFinalType
void setFinalType(Type newType)
Definition: NumericConstant.h:77
souffle::ast::NumericConstant::Type::Int
@ Int
Constant.h
souffle::ast::Constant
Abstract constant class.
Definition: Constant.h:38
souffle::ast::Constant::Constant
Constant(std::string value, SrcLocation loc={})
Definition: Constant.h:57
Node.h
souffle::ast::NumericConstant::clone
NumericConstant * clone() const override
Create clone.
Definition: NumericConstant.h:64
souffle::detail::brie::copy
auto copy(span< A, arity > s)
Definition: Brie.h:98
souffle::ast::NumericConstant::NumericConstant
NumericConstant(RamSigned value)
Definition: NumericConstant.h:53
souffle::ast::Node
Abstract class for syntactic elements in an input program.
Definition: Node.h:40
std
Definition: Brie.h:3053
souffle::ast::NumericConstant::finalTranslatorType
std::optional< Type > finalTranslatorType
Definition: NumericConstant.h:95
RamTypes.h
souffle::SrcLocation
A class describing a range in an input file.
Definition: SrcLocation.h:32
souffle::ast::Node::setSrcLoc
void setSrcLoc(SrcLocation l)
Set source location for the Node.
Definition: Node.h:51
souffle::ast::NumericConstant::Type::Uint
@ Uint
souffle::ast::Constant::equal
bool equal(const Node &node) const override
Abstract equality check for two AST nodes.
Definition: Constant.h:52
souffle::ast::NumericConstant::getFinalType
std::optional< Type > getFinalType() const
Definition: NumericConstant.h:81
souffle::ast::NumericConstant::Type
Type
Definition: NumericConstant.h:51
souffle::RamSigned
RamDomain RamSigned
Definition: RamTypes.h:57
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::NumericConstant::Type::Float
@ Float
souffle::ast::NumericConstant::fixedType
std::optional< Type > fixedType
Definition: NumericConstant.h:92
souffle::ast::NumericConstant
Numeric Constant.
Definition: NumericConstant.h:43
souffle::ast::Constant::constant
const std::string constant
String representation of constant.
Definition: Constant.h:58
souffle::ast::NumericConstant::equal
bool equal(const Node &node) const override
Abstract equality check for two AST nodes.
Definition: NumericConstant.h:86