1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// Copyright (c) 2017 Fabian Schuiki

//! This module implements constant value calculation for VHDL.

use std::fmt;
use num::BigInt;
use ty::*;
use score::TypeDeclRef;
pub use hir::Dir;


/// A constant value.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Const {
	Null,
	Int(ConstInt),
	Float(ConstFloat),
	Enum(ConstEnum),
	IntRange(ConstIntRange),
	FloatRange(ConstFloatRange),
}

impl Const {
	pub fn negate(self) -> Const {
		match self {
			Const::Null => panic!("cannot negate null"),
			Const::Int(c) => Const::Int(c.negate()),
			Const::Float(c) => Const::Float(c.negate()),
			Const::Enum(_) => panic!("cannot negate enumeration literal"),
			Const::IntRange(_) => panic!("cannot negate integer range"),
			Const::FloatRange(_) => panic!("cannot negate float range"),
		}
	}


	/// Provide a textual description of the kind of constant.
	pub fn kind_desc(&self) -> &'static str {
		match *self {
			Const::Null => "null",
			Const::Int(_) => "integer",
			Const::Float(_) => "float",
			Const::Enum(_) => "enumeration literal",
			Const::IntRange(_) => "integer range",
			Const::FloatRange(_) => "float range",
		}
	}
}

impl From<ConstInt> for Const {
	fn from(k: ConstInt) -> Const {
		Const::Int(k)
	}
}

impl From<ConstFloat> for Const {
	fn from(k: ConstFloat) -> Const {
		Const::Float(k)
	}
}

impl From<ConstEnum> for Const {
	fn from(k: ConstEnum) -> Const {
		Const::Enum(k)
	}
}

impl From<ConstIntRange> for Const {
	fn from(k: ConstIntRange) -> Const {
		Const::IntRange(k)
	}
}

impl From<ConstFloatRange> for Const {
	fn from(k: ConstFloatRange) -> Const {
		Const::FloatRange(k)
	}
}


/// A constant integer value.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ConstInt {
	/// The type of the constant. If `None`, the constant is assumed to be an
	/// unbounded integer which cannot be mapped to LLHD.
	pub ty: Option<IntTy>,
	/// The value of the constant.
	pub value: BigInt,
}

impl ConstInt {
	/// Create a new constant integer.
	pub fn new(ty: Option<IntTy>, value: BigInt) -> ConstInt {
		ConstInt {
			ty: ty,
			value: value,
		}
	}

	pub fn negate(self) -> ConstInt {
		ConstInt::new(self.ty, -self.value)
	}
}


/// A constant float value.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ConstFloat {
}

impl ConstFloat {
	pub fn negate(self) -> ConstFloat {
		ConstFloat{}
	}
}


/// A constant enumeration value.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ConstEnum {
	/// The type declaration which declared the enum.
	pub decl: TypeDeclRef,
	/// The index of the literal.
	pub index: usize,
}

impl ConstEnum {
	/// Create a new constant integer.
	pub fn new(decl: TypeDeclRef, index: usize) -> ConstEnum {
		ConstEnum {
			decl: decl,
			index: index,
		}
	}
}


/// A constant range value.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ConstRange<T: fmt::Display + fmt::Debug> {
	pub dir: Dir,
	pub left_bound: T,
	pub right_bound: T,
}

impl<T> ConstRange<T> where T: fmt::Display + fmt::Debug {
	/// Create a new constant range.
	pub fn new(dir: Dir, left_bound: T, right_bound: T) -> ConstRange<T> {
		ConstRange {
			dir: dir,
			left_bound: left_bound,
			right_bound: right_bound,
		}
	}
}

pub type ConstIntRange = ConstRange<ConstInt>;
pub type ConstFloatRange = ConstRange<ConstFloat>;


// ----- FORMATTING ------------------------------------------------------------

impl fmt::Display for Const {
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		match *self {
			Const::Null => write!(f, "null"),
			Const::Int(ref k) => k.fmt(f),
			Const::Float(ref k) => k.fmt(f),
			Const::Enum(ref k) => k.fmt(f),
			Const::IntRange(ref k) => k.fmt(f),
			Const::FloatRange(ref k) => k.fmt(f),
		}
	}
}

impl fmt::Display for ConstInt {
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		self.value.fmt(f)
	}
}

impl fmt::Display for ConstEnum {
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		write!(f, "<enum>")
	}
}

impl fmt::Display for ConstFloat {
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		write!(f, "<float>")
	}
}

impl<T> fmt::Display for ConstRange<T> where T: fmt::Display + fmt::Debug {
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		write!(f, "{} {} {}", self.left_bound, self.dir, self.right_bound)
	}
}