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 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
use fnv::FnvHashSet;
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at http://mozilla.org/MPL/2.0/.
//
// Copyright (c) 2023, Olof Kraigher olof.kraigher@gmail.com
use super::analyze::*;
use super::scope::*;
use crate::analysis::static_expression::{bit_string_to_string, BitStringConversionError};
use crate::ast::*;
use crate::data::*;
use crate::named_entity::*;
impl<'a> AnalyzeContext<'a> {
/// Analyze a string literal or expanded bit-string literal for type-matching
fn analyze_string_literal(
&self,
pos: &SrcPos,
string_lit: Latin1String,
target_base: TypeEnt,
target_type: TypeEnt,
diagnostics: &mut dyn DiagnosticHandler,
) {
if let Some((elem_type, literals)) = as_single_index_enum_array(target_base) {
for chr in string_lit.chars() {
let chr = Designator::Character(*chr);
if !literals.contains(&chr) {
diagnostics.push(Diagnostic::error(
pos,
format!("{} does not define character {}", elem_type.describe(), chr),
));
break;
}
}
} else {
diagnostics.push(Diagnostic::error(
pos,
format!("string literal does not match {}", target_type.describe()),
));
}
}
/// Returns true if the name actually matches the target type
/// None if it was uncertain
pub fn analyze_literal_with_target_type(
&self,
scope: &Scope<'a>,
target_type: TypeEnt<'a>,
pos: &SrcPos,
literal: &mut Literal,
diagnostics: &mut dyn DiagnosticHandler,
) -> FatalResult {
let target_base = target_type.base_type();
match literal {
Literal::AbstractLiteral(abst) => match abst {
AbstractLiteral::Integer(_) => {
if !self.can_be_target_type(self.universal_integer().into(), target_type.base())
{
diagnostics.push(Diagnostic::error(
pos,
format!("integer literal does not match {}", target_type.describe()),
));
}
}
AbstractLiteral::Real(_) => {
if !self.can_be_target_type(self.universal_real().into(), target_type.base()) {
diagnostics.push(Diagnostic::error(
pos,
format!("real literal does not match {}", target_type.describe()),
));
}
}
},
Literal::Character(char) => match target_base.kind() {
Type::Enum(literals) => {
if !literals.contains(&Designator::Character(*char)) {
diagnostics.push(Diagnostic::error(
pos,
format!(
"character literal does not match {}",
target_type.describe()
),
));
}
}
_ => {
diagnostics.push(Diagnostic::error(
pos,
format!(
"character literal does not match {}",
target_type.describe()
),
));
}
},
Literal::String(string_lit) => {
self.analyze_string_literal(
pos,
string_lit.to_owned(),
target_base,
target_type,
diagnostics,
);
}
Literal::BitString(bit_string) => {
match bit_string_to_string(bit_string) {
Ok(string_lit) => self.analyze_string_literal(
pos,
string_lit,
target_base,
target_type,
diagnostics,
),
Err(err) => {
match err {
BitStringConversionError::IllegalDecimalCharacter(rel_pos) => {
diagnostics.error(
pos,
format!(
"Illegal digit '{}' for base 10",
bit_string.value.bytes[rel_pos] as char,
),
)
}
BitStringConversionError::IllegalTruncate(_, _) => {
diagnostics.error(
pos,
format!(
"Truncating vector to length {} would lose information",
bit_string.length.unwrap() // Safe as this error can only happen when there is a length
),
);
}
BitStringConversionError::EmptySignedExpansion => {
diagnostics.error(pos, "Cannot expand an empty signed bit string");
}
}
}
}
}
Literal::Physical(PhysicalLiteral { ref mut unit, .. }) => {
match self.resolve_physical_unit(scope, unit) {
Ok(physical_type) => {
if physical_type.base_type() != target_base {
diagnostics.push(Diagnostic::type_mismatch(
pos,
&physical_type.describe(),
target_type,
))
}
}
Err(diagnostic) => {
diagnostics.push(diagnostic);
}
}
}
Literal::Null => {
if !matches!(target_base.kind(), Type::Access(_)) {
diagnostics.push(Diagnostic::error(
pos,
format!("null literal does not match {}", target_base.describe()),
));
}
}
};
Ok(())
}
pub fn resolve_physical_unit(
&self,
scope: &Scope<'a>,
unit: &mut WithRef<Ident>,
) -> Result<TypeEnt<'a>, Diagnostic> {
match scope.lookup(
&unit.item.pos,
&Designator::Identifier(unit.item.item.clone()),
)? {
NamedEntities::Single(unit_ent) => {
unit.set_unique_reference(unit_ent);
if let AnyEntKind::PhysicalLiteral(physical_ent) = unit_ent.actual_kind() {
Ok(*physical_ent)
} else {
Err(Diagnostic::error(
&unit.item.pos,
format!("{} is not a physical unit", unit_ent.describe()),
))
}
}
NamedEntities::Overloaded(_) => Err(Diagnostic::error(
&unit.item.pos,
"Overloaded name may not be physical unit",
)),
}
}
}
/// Must be an array type with a single index of enum type
fn as_single_index_enum_array(typ: TypeEnt) -> Option<(TypeEnt, &FnvHashSet<Designator>)> {
if let Type::Array {
indexes, elem_type, ..
} = typ.kind()
{
if indexes.len() == 1 {
if let Type::Enum(literals) = elem_type.base_type().kind() {
return Some((*elem_type, literals));
}
}
}
None
}