145 Sema(
const Ast &ast, std::vector<Diagnostic> &diagnostics,
148 , diagnostics_(diagnostics)
155 result_.
exprs.resize(ast_.exprs.size());
156 result_.stmts.resize(ast_.stmts.size());
157 precheck_string_pool();
166 result_.ok = !has_error_;
171 void precheck_string_pool()
173 std::uint64_t total = 0;
179 std::vector<std::uint8_t> bytes;
180 std::vector<std::uint32_t> scalars;
181 if(!decode_utf8(expr.
text, bytes, scalars)) {
185 ? scalars.size() * 4ULL
189 "string constant pool");
202 static Expected none()
214 return {
true,
type,
id};
217 void diag(
DiagCode code, std::int32_t line, std::int32_t column,
218 const std::string ¬e = std::string())
221 if(diagnostics_.size() >= limits_.max_diagnostics) {
233 diagnostics_.push_back(
static_cast<Diagnostic &&
>(
d));
236 static std::string lower_copy(
const std::string &text)
239 lower.reserve(text.size());
241 lower.push_back((c >=
'A' && c <=
'Z')
242 ?
static_cast<char>(c -
'A' +
'a')
264 decl.line, decl.column, decl.name);
284 if(has_type_cycle()) {
287 std::size_t declared = 0;
288 for(
const UserTypeDecl &decl : ast_.user_types) {
289 if(declared >= limits_.max_user_types ||
290 decl.name.size() > limits_.max_type_name_bytes ||
292 decl.enum_members.size() > limits_.max_enum_members)) {
300 std::vector<EnumItem> items;
301 items.reserve(decl.enum_members.size());
302 std::int64_t next = 0;
303 bool overflow =
false;
304 for(
const EnumMemberDecl &member : decl.enum_members) {
305 if(!member.explicit_value && overflow) {
308 const std::int64_t value = member.explicit_value
313 overflow = value >= std::numeric_limits<std::int32_t>::max();
318 if(items.empty() || items.size() != decl.enum_members.size()) {
325 error = result_.types.add_subrange(
326 decl.name,
st::type_id(decl.base), decl.range_lower,
327 decl.range_upper,
id);
329 if(decl.array_bounds.empty() || decl.array_bounds.size() > 3) {
332 const TypeId element = resolve_type_ref(
333 decl.element_type, decl.element_type_name, decl);
336 : result_.types.add_array(
337 decl.name, element, decl.array_bounds,
341 if(decl.struct_fields.size() > limits_.max_struct_fields) {
346 std::vector<StructFieldSpec>
fields;
347 fields.reserve(decl.struct_fields.size());
348 for(
const StructFieldDecl &
field : decl.struct_fields) {
358 error = result_.types.add_struct(decl.name, fields,
id);
362 type_diag(
error, decl);
365 const TypeDesc *added = result_.types.get(
id);
367 std::uint64_t elements = 1;
368 for(
const ArrayDimension &dimension :
369 added->array.dimensions) {
370 if(dimension.extent > limits_.max_array_elements /
372 elements = limits_.max_array_elements + 1ULL;
375 elements *= dimension.extent;
377 if(elements > limits_.max_array_elements) {
382 if(aggregate_depth(
id) > limits_.max_aggregate_depth) {
390 TypeId resolve_type_ref(
Type builtin_type,
const std::string &name,
391 const UserTypeDecl &owner)
const
396 if(lower_copy(name) == owner.lower) {
397 return result_.types.next_type_id();
405 std::uint16_t aggregate_depth(
TypeId id)
const
407 const TypeDesc *desc = result_.types.get(
id);
412 std::uint16_t child = 0;
414 child = aggregate_depth(desc->array.element);
416 for(
const StructField &
field : desc->structure.fields) {
417 const std::uint16_t depth = aggregate_depth(
field.type);
423 return static_cast<std::uint16_t
>(child + 1U);
426 int user_type_index(
const std::string &lower)
const
428 for(std::size_t i = 0; i < ast_.user_types.size(); ++i) {
429 if(ast_.user_types[i].lower == lower) {
430 return static_cast<int>(i);
436 bool cycle_visit(std::size_t index, std::vector<std::uint8_t> &state)
438 if(state[index] == 1) {
439 const UserTypeDecl &decl = ast_.user_types[index];
444 if(state[index] == 2) {
448 const UserTypeDecl &decl = ast_.user_types[index];
449 std::vector<std::string> refs;
451 !decl.element_type_name.empty()) {
452 refs.push_back(lower_copy(decl.element_type_name));
454 for(
const StructFieldDecl &
field : decl.struct_fields) {
455 if(!
field.type_name.empty()) {
456 refs.push_back(lower_copy(
field.type_name));
460 for(
const std::string &
ref : refs) {
461 const int target = user_type_index(
ref);
462 if(target >= 0 && cycle_visit(
static_cast<std::size_t
>(target),
471 bool has_type_cycle()
473 std::vector<std::uint8_t> state(ast_.user_types.size(), 0);
474 for(std::size_t i = 0; i < ast_.user_types.size(); ++i) {
475 if(cycle_visit(i, state)) {
484 std::uint32_t fb_offset = 0;
485 std::uint16_t axis_refs = 0;
486 std::uint16_t group_refs = 0;
487 for(
const VarDecl &decl : ast_.vars) {
488 if(find_var(decl.lower) >= 0 || find_fb(decl.lower) >= 0) {
490 decl.column, decl.name);
494 if(result_.fbs.size() >= limits_.max_fb_instances) {
496 decl.column, decl.name);
501 "FB instances take no initializer");
505 info.name = decl.name;
506 info.lower = decl.lower;
507 info.type = decl.fb_type;
508 info.offset = fb_offset;
509 const std::size_t size =
fb_size(decl.fb_type);
510 fb_offset +=
static_cast<std::uint32_t
>(
512 result_.fbs.push_back(
static_cast<FbInfo &&
>(info));
516 axis_refs++ >= limits_.max_axis_refs) {
522 group_refs++ >= limits_.max_group_refs) {
527 if((result_.vars.size() + 1U) * 8U >
528 limits_.max_vars_bytes) {
534 info.name = decl.name;
535 info.lower = decl.lower;
536 info.type = decl.type;
539 if(decl.string_capacity < 1U || decl.string_capacity > 4096U) {
541 decl.column, decl.name);
544 if(ast_.user_types.size() + string_type_count_ >=
545 limits_.max_user_types) {
550 const std::string type_name =
551 "__st_string_" + std::to_string(string_type_count_++);
554 ? result_.types.add_wstring(type_name,
555 decl.string_capacity,
557 : result_.types.add_string(type_name,
558 decl.string_capacity,
566 if(!decl.type_name.empty()) {
567 if(result_.types.find(decl.type_name, info.type_id) !=
570 decl.column, decl.type_name);
573 const TypeDesc *desc = result_.types.get(info.type_id);
574 if(desc ==
nullptr) {
583 : desc->subrange.base;
594 if(!desc->enum_items.empty()) {
595 info.init_bits =
static_cast<std::uint64_t
>(
596 desc->enum_items.front().value.as_signed());
600 info.init_bits =
static_cast<std::uint64_t
>(
601 desc->subrange.lower.as_signed());
603 info.init_bits = desc->subrange.lower.as_unsigned();
608 axis_refs++ >= limits_.max_axis_refs) {
613 info.constant = decl.is_constant;
614 const TypeDesc *storage = result_.types.get(info.type_id);
615 if(storage ==
nullptr) {
620 const std::uint32_t alignment =
621 storage->alignment > 8 ? 8U : storage->alignment;
622 const std::uint64_t storage_size =
624 const std::uint64_t aligned =
625 (
static_cast<std::uint64_t
>(result_.vars_bytes) +
626 alignment - 1U) & ~(
static_cast<std::uint64_t
>(alignment) - 1U);
627 if(aligned > limits_.max_vars_bytes ||
628 storage_size > limits_.max_vars_bytes - aligned) {
633 info.offset =
static_cast<std::uint32_t
>(aligned);
634 result_.vars_bytes =
static_cast<std::uint32_t
>(aligned +
636 result_.initial_data.resize(result_.vars_bytes, 0);
637 default_initialize(info.type_id, info.offset);
643 decl.column, decl.name);
644 result_.vars.push_back(
static_cast<VarInfo &&
>(info));
647 if(storage !=
nullptr &&
650 if(check_expr(decl.init, want(info.type, info.type_id))) {
651 const ExprInfo &init = result_.exprs[
652 static_cast<std::size_t
>(decl.init)];
653 if(!init.object_constant ||
654 init.object_bytes.size() != storage->size) {
656 decl.column, decl.name);
658 std::memcpy(result_.initial_data.data() + info.offset,
659 init.object_bytes.data(),
660 init.object_bytes.size());
661 info.initial_length =
662 static_cast<std::uint32_t
>(init.object_bytes[0]) |
663 (
static_cast<std::uint32_t
>(init.object_bytes[1]) << 8U) |
664 (
static_cast<std::uint32_t
>(init.object_bytes[2]) << 16U) |
665 (
static_cast<std::uint32_t
>(init.object_bytes[3]) << 24U);
668 }
else if(storage !=
nullptr &&
671 initialize_value(decl.init, info.type_id, info.offset);
672 }
else if(check_expr(decl.init,
673 want(info.type, info.type_id))) {
674 const ExprInfo &init = result_.exprs[
675 static_cast<std::size_t
>(decl.init)];
678 decl.column, decl.name);
680 info.init_bits = init.bits;
681 write_initial_scalar(info.offset, info.type_id,
685 }
else if(decl.is_constant) {
687 "VAR CONSTANT requires an initializer");
689 if(decl.is_located && !register_location(decl, info)) {
692 result_.vars.push_back(
static_cast<VarInfo &&
>(info));
694 result_.fb_bytes = fb_offset;
697 static std::uint64_t stable_id(std::string_view name)
699 std::uint64_t
hash = 1469598103934665603ULL;
701 hash ^=
static_cast<unsigned char>(c);
702 hash *= 1099511628211ULL;
707 bool register_location(
const VarDecl &decl,
const VarInfo &info)
709 const std::uint8_t width = decl.location_width ==
'X' ? 1U
710 : decl.location_width ==
'B' ? 1U
711 : decl.location_width ==
'W' ? 2U
712 : decl.location_width ==
'D' ? 4U
713 : decl.location_width ==
'L' ? 8U : 0U;
719 if(width == 0 || info.type_id != expected ||
720 (decl.location_width ==
'X' && decl.location_bit > 7U)) {
725 if(width > 1U && decl.location_byte % width != 0U) {
730 if((decl.is_retain || decl.is_persistent) &&
731 decl.location_area !=
'M') {
736 if((decl.is_retain || decl.is_persistent) &&
737 result_.process_image.retain_entries >= limits_.max_retain_entries) {
742 const std::uint64_t end =
static_cast<std::uint64_t
>(
743 decl.location_byte) + width;
744 const std::uint32_t
limit = decl.location_area ==
'I'
745 ? limits_.max_input_image_bytes
746 : decl.location_area ==
'Q' ? limits_.max_output_image_bytes
747 : limits_.max_memory_image_bytes;
754 const std::uint64_t first_bit =
755 static_cast<std::uint64_t
>(decl.location_byte) * 8U +
756 (decl.location_width ==
'X' ? decl.location_bit : 0U);
757 const std::uint64_t last_bit = decl.location_width ==
'X'
759 : first_bit +
static_cast<std::uint64_t
>(width) * 8U - 1U;
760 for(
const LocatedVarInfo &old : result_.process_image.variables) {
764 decl.location_area) {
767 const std::uint64_t old_first =
768 static_cast<std::uint64_t
>(old.byte_offset) * 8U +
769 (old.bit_address ? old.bit : 0U);
770 const std::uint64_t old_last = old.bit_address
772 : old_first +
static_cast<std::uint64_t
>(old.byte_width) *
774 if(last_bit < old_first || old_last < first_bit)
continue;
775 bool compatible =
false;
776 if(decl.location_area ==
'I') {
777 compatible = first_bit == old_first && last_bit == old_last;
778 if(decl.location_width ==
'X' && !old.bit_address) {
779 compatible = first_bit >= old_first &&
780 first_bit <= old_last;
781 }
else if(decl.location_width !=
'X' && old.bit_address) {
782 compatible = old_first >= first_bit &&
783 old_first <= last_bit;
788 decl.column, decl.name);
793 LocatedVarInfo located;
794 located.name = decl.name;
795 located.lower = decl.lower;
796 located.type_id = info.type_id;
800 located.byte_offset = decl.location_byte;
801 located.var_offset = info.offset;
802 located.byte_width = width;
803 located.bit = decl.location_bit;
804 located.bit_address = decl.location_width ==
'X';
805 located.retain = decl.is_retain;
806 located.persistent = decl.is_persistent;
807 located.stable_id = stable_id(decl.lower);
808 result_.process_image.variables.push_back(
809 static_cast<LocatedVarInfo &&
>(located));
810 std::uint32_t &bytes = decl.location_area ==
'I'
811 ? result_.process_image.input_bytes
812 : decl.location_area ==
'Q' ? result_.process_image.output_bytes
813 : result_.process_image.memory_bytes;
814 bytes = std::max(bytes,
static_cast<std::uint32_t
>(end));
815 if(decl.is_retain || decl.is_persistent) {
816 ++result_.process_image.retain_entries;
818 result_.process_image.max_force_entries = limits_.max_force_entries;
822 void write_initial_scalar(std::uint32_t offset,
TypeId type_id,
825 const TypeDesc *desc = result_.types.get(
type_id);
826 if(desc ==
nullptr || offset + desc->size > result_.initial_data.size()) {
831 ? desc->subrange.base
834 std::uint32_t
raw = 0;
835 std::memcpy(&raw, &value,
sizeof(raw));
836 for(std::uint32_t i = 0; i < 4; ++i) {
837 result_.initial_data[offset + i] =
838 static_cast<std::uint8_t
>(
raw >> (i * 8U));
842 for(std::uint64_t i = 0; i < desc->size && i < 8; ++i) {
843 result_.initial_data[offset +
static_cast<std::size_t
>(i)] =
844 static_cast<std::uint8_t
>(bits >> (i * 8U));
848 void default_initialize(
TypeId type_id, std::uint32_t offset)
850 const TypeDesc *desc = result_.types.get(
type_id);
851 if(desc ==
nullptr) {
855 if(!desc->enum_items.empty()) {
856 write_initial_scalar(
858 static_cast<std::uint64_t
>(
859 desc->enum_items.front().value.as_signed()));
864 write_initial_scalar(
867 ?
static_cast<std::uint64_t
>(
868 desc->subrange.lower.as_signed())
869 : desc->subrange.lower.as_unsigned());
873 const TypeDesc *element = result_.types.get(desc->array.element);
874 if(element ==
nullptr || element->size == 0) {
877 for(std::uint64_t at = 0; at < desc->size; at += element->size) {
878 default_initialize(desc->array.element,
879 offset +
static_cast<std::uint32_t
>(at));
882 for(
const StructField &
field : desc->structure.fields) {
883 default_initialize(
field.type,
884 offset +
static_cast<std::uint32_t
>(
field.offset));
890 std::uint32_t offset)
892 const TypeDesc *desc = result_.types.get(
type_id);
893 if(desc ==
nullptr || index ==
kNoExpr) {
897 return initialize_array(index, *desc, 0, offset);
900 return initialize_struct(index, *desc, offset);
909 const ExprInfo &value = result_.exprs[
static_cast<std::size_t
>(index)];
910 if(!value.is_const) {
911 const Expr &at = ast_.exprs[
static_cast<std::size_t
>(index)];
913 "initializer must be constant");
916 write_initial_scalar(offset,
type_id, value.bits);
921 std::size_t dimension, std::uint32_t offset)
923 const Expr &expr = ast_.exprs[
static_cast<std::size_t
>(index)];
925 dimension >=
array.array.dimensions.size()) {
930 const ArrayDimension &dim =
array.array.dimensions[dimension];
931 if(expr.items.size() > dim.extent) {
933 "too many array initializer elements");
937 for(std::size_t i = 0; i < expr.items.size(); ++i) {
938 if(!expr.items[i].name.empty()) {
940 "array initializer cannot name elements");
944 const std::uint32_t child =
945 offset +
static_cast<std::uint32_t
>(i * dim.stride);
946 if(dimension + 1U <
array.array.dimensions.size()) {
947 ok = initialize_array(expr.items[i].value,
array,
948 dimension + 1U, child) &&
ok;
950 ok = initialize_value(expr.items[i].value,
951 array.array.element, child) &&
ok;
957 bool initialize_struct(
ExprIndex index,
const TypeDesc &structure,
958 std::uint32_t offset)
960 const Expr &expr = ast_.exprs[
static_cast<std::size_t
>(index)];
967 bool positional =
false;
968 std::vector<bool> seen(structure.structure.fields.size(),
false);
970 for(std::size_t i = 0; i < expr.items.size(); ++i) {
971 const InitItem &item = expr.items[i];
972 named = named || !item.name.empty();
973 positional = positional || item.name.empty();
974 if(named && positional) {
976 "cannot mix positional and named struct initialization");
979 std::size_t field_index = i;
981 field_index = structure.structure.fields.size();
982 for(std::size_t f = 0;
983 f < structure.structure.fields.size(); ++f) {
984 if(lower_copy(structure.structure.fields[f].name) ==
985 lower_copy(item.name)) {
991 if(field_index >= structure.structure.fields.size() ||
994 "unknown, duplicate, or excess struct initializer");
998 seen[field_index] =
true;
999 const StructField &
field =
1000 structure.structure.fields[field_index];
1001 ok = initialize_value(
1002 item.value,
field.type,
1003 offset +
static_cast<std::uint32_t
>(
field.offset)) &&
ok;
1008 int find_var(
const std::string &lower)
const
1010 for(std::size_t i = 0; i < result_.vars.size(); ++i) {
1011 if(result_.vars[i].lower == lower) {
1012 return static_cast<int>(i);
1018 int find_fb(
const std::string &lower)
const
1020 for(std::size_t i = 0; i < result_.fbs.size(); ++i) {
1021 if(result_.fbs[i].lower == lower) {
1022 return static_cast<int>(i);
1028 int find_pin(
FbType type,
const std::string &lower)
const
1031 for(std::uint16_t i = 0; i < table.count; ++i) {
1032 if(table.pins[i].lower_name == lower) {
1039 struct NamedConversion
1047 bool resolve_named_conversion(
const std::string &name,
1048 NamedConversion &conversion)
const
1050 const std::string lower = lower_copy(name);
1051 const std::size_t split = lower.find(
"_to_");
1052 if(split == std::string::npos) {
1057 if(result_.types.find(lower.substr(0, split), from) !=
TypeError::ok ||
1058 result_.types.find(lower.substr(split + 4), to) !=
TypeError::ok) {
1061 const TypeDesc *from_desc = result_.types.get(from);
1062 const TypeDesc *to_desc = result_.types.get(to);
1063 if(from_desc ==
nullptr || to_desc ==
nullptr) {
1066 auto base_id = [](
const TypeDesc &desc) {
1068 return desc.enum_base;
1071 return desc.subrange.base;
1075 const TypeId from_base = base_id(*from_desc);
1076 const TypeId to_base = base_id(*to_desc);
1091 conversion.from_id = from;
1092 conversion.to_id = to;
1101 static_cast<std::size_t
>(index) >= ast_.exprs.size()) {
1104 const Expr &expr = ast_.exprs[
static_cast<std::size_t
>(index)];
1107 result_.types.find(expr.name,
id);
1111 const int var = find_var(lower_copy(expr.name));
1113 return access_result_type(
1114 result_.vars[
static_cast<std::size_t
>(var)].type_id,
1119 NamedConversion conversion;
1120 if(resolve_named_conversion(expr.name, conversion)) {
1121 return conversion.to_id;
1128 const std::vector<AccessStep> &steps)
const
1130 for(
const AccessStep &
step : steps) {
1131 const TypeDesc *desc = result_.types.get(
type_id);
1132 if(desc ==
nullptr) {
1136 const StructField *
field =
nullptr;
1146 type_id = desc->array.element;
1154 const TypeDesc *desc = result_.types.get(
id);
1155 if(desc ==
nullptr) {
1178 static_cast<std::size_t
>(index) >= ast_.stmts.size()) {
1181 const Stmt &stmt = ast_.stmts[
static_cast<std::size_t
>(index)];
1182 StmtInfo &info = result_.stmts[
static_cast<std::size_t
>(index)];
1192 for(
const StmtIndex child : stmt.body) {
1199 if(stmt.params.empty() || (stmt.params.size() & 1U) != 0U) {
1201 "L2B_COMMIT source/destination pairs");
1204 for(std::size_t i = 0; i < stmt.params.size(); i += 2U) {
1205 if(!check_expr(stmt.params[i + 1U].value, Expected{})) {
1208 const Expr &destination = ast_.exprs[
static_cast<std::size_t
>(
1209 stmt.params[i + 1U].value)];
1211 !result_.exprs[
static_cast<std::size_t
>(
1212 stmt.params[i + 1U].value)].memory_access) {
1214 destination.column);
1217 const ExprInfo &target = result_.exprs[
static_cast<std::size_t
>(
1218 stmt.params[i + 1U].value)];
1219 check_expr(stmt.params[i].value,
1220 want(target.type, target.type_id));
1225 if(loop_depth_ == 0) {
1235 void check_assign(
const Stmt &stmt, StmtInfo &info)
1237 const std::string lower = lower_copy(stmt.target);
1238 const int var = find_var(lower);
1240 if(find_fb(lower) >= 0) {
1242 "FB instances cannot be assigned");
1249 for(
const std::string &control : control_stack_) {
1250 if(control == lower) {
1252 stmt.column, stmt.target);
1256 if(result_.vars[
static_cast<std::size_t
>(var)].constant) {
1258 "VAR CONSTANT member");
1261 const VarInfo &target = result_.vars[
static_cast<std::size_t
>(var)];
1262 info.offset = target.offset;
1263 info.type = target.type;
1264 info.type_id = target.type_id;
1265 if(!resolve_access(stmt.target_access, info.offset, info.type,
1266 info.type_id, info.dynamic_indices, stmt.line,
1270 const TypeDesc *desc = result_.types.get(info.type_id);
1272 const Expr &source_expr =
1273 ast_.exprs[
static_cast<std::size_t
>(stmt.value)];
1279 if(!check_expr(stmt.value, Expected{}))
return;
1280 const ExprInfo &source =
1281 result_.exprs[
static_cast<std::size_t
>(stmt.value)];
1282 if(source.memory_access || source.type != info.type ||
1283 source.type_id != info.type_id) {
1294 if(desc !=
nullptr &&
1297 type_contains_reference(info.type_id)) {
1299 "aggregates containing host references are not assignable");
1304 if(check_expr(stmt.value, want(info.type, info.type_id))) {
1305 const ExprInfo &source = result_.exprs[
1306 static_cast<std::size_t
>(stmt.value)];
1307 const TypeDesc *source_desc = result_.types.get(
1310 : source.storage_type_id);
1311 const std::uint64_t source_capacity =
1312 source_desc ==
nullptr ? 0U
1313 : source_desc->string.capacity;
1314 result_.max_string_operation_cost = std::max(
1315 result_.max_string_operation_cost,
1316 static_cast<std::uint32_t
>(std::max(
1317 desc->string.capacity, source_capacity)));
1323 if(!info.dynamic_indices.empty() ||
1324 !check_expr(stmt.value, want(info.type, info.type_id))) {
1327 const ExprInfo &source = result_.exprs[
1328 static_cast<std::size_t
>(stmt.value)];
1329 if(!source.dynamic_indices.empty()) {
1331 "dynamic aggregate copy is not supported in L1b2");
1334 if(source.memory_access) {
1335 info.aggregate_copy =
true;
1336 info.source_offset = source.offset;
1338 info.fb_output_copy =
true;
1339 info.fb_index = source.fb_index;
1340 info.pin_id = source.pin_id;
1342 info.copy_size =
static_cast<std::uint32_t
>(desc->size);
1345 check_expr(stmt.value, want(info.type, info.type_id));
1348 void check_if(
const Stmt &stmt)
1350 for(std::size_t i = 0; i < stmt.conditions.size(); ++i) {
1351 check_expr(stmt.conditions[i], want(
Type::bool_));
1352 for(
const StmtIndex child : stmt.branches[i]) {
1356 for(
const StmtIndex child : stmt.else_body) {
1361 void check_case(
const Stmt &stmt, StmtInfo &info)
1364 TypeId selector_id = nominal_anchor(stmt.selector);
1365 const TypeDesc *nominal = result_.types.get(selector_id);
1367 if(nominal !=
nullptr &&
1371 ? nominal->enum_base
1372 : nominal->subrange.base;
1375 selector = anchored;
1379 ast_.exprs[
static_cast<std::size_t
>(stmt.selector)];
1381 "CASE selector must be INT or DINT");
1383 check_expr(stmt.selector, want(selector, selector_id));
1384 info.type = selector;
1385 info.type_id = selector_id;
1392 std::vector<Interval> seen;
1393 for(
const CaseArm &arm : stmt.arms) {
1394 for(
const CaseLabel &label : arm.labels) {
1395 std::int64_t low = 0;
1396 std::int64_t high = 0;
1397 if(!const_label(label.low, selector, selector_id, low)) {
1403 const Expr &at = ast_.exprs[
1404 static_cast<std::size_t
>(label.high)];
1406 at.column,
"enum CASE labels cannot be ranges");
1409 if(!const_label(label.high, selector, selector_id, high)) {
1414 ast_.exprs[
static_cast<std::size_t
>(label.low)];
1420 for(
const Interval &other : seen) {
1421 if(low <= other.high && other.low <= high) {
1427 seen.push_back({low, high});
1436 std::int64_t &value)
1441 if(!check_expr(index, want(selector, selector_id))) {
1444 const ExprInfo &info = result_.exprs[
static_cast<std::size_t
>(index)];
1445 if(!info.is_const) {
1446 const Expr &at = ast_.exprs[
static_cast<std::size_t
>(index)];
1448 "CASE labels must be constant");
1451 value =
static_cast<std::int64_t
>(info.bits);
1455 void check_for(
const Stmt &stmt, StmtInfo &info)
1457 const std::string lower = lower_copy(stmt.control);
1458 const int var = find_var(lower);
1464 control = result_.vars[
static_cast<std::size_t
>(var)].
type;
1467 stmt.column,
"FOR control must be INT or DINT");
1470 info.offset = result_.vars[
static_cast<std::size_t
>(var)].offset;
1471 info.type = control;
1472 info.type_id = result_.vars[
static_cast<std::size_t
>(var)].
type_id;
1475 for(
const std::string &outer : control_stack_) {
1476 if(outer == lower) {
1478 stmt.column, stmt.control);
1481 check_expr(stmt.from, want(control));
1482 check_expr(stmt.to, want(control));
1484 if(check_expr(stmt.by, want(control))) {
1485 const ExprInfo &by =
1486 result_.exprs[
static_cast<std::size_t
>(stmt.by)];
1487 if(by.is_const &&
static_cast<std::int64_t
>(by.bits) == 0) {
1489 ast_.exprs[
static_cast<std::size_t
>(stmt.by)];
1495 control_stack_.push_back(lower);
1497 for(
const StmtIndex child : stmt.body) {
1501 control_stack_.pop_back();
1504 void check_fb_call(
const Stmt &stmt, StmtInfo &info)
1506 const std::string lower = lower_copy(stmt.instance);
1507 const int fb = find_fb(lower);
1509 if(find_var(lower) >= 0) {
1518 info.fb_index =
static_cast<std::uint16_t
>(
fb);
1521 info.param_pins.reserve(stmt.params.size());
1525 if(!stmt.params.empty() && stmt.params[0].pin.empty()) {
1526 std::uint16_t input_count = 0;
1527 for(std::uint16_t i = 0; i < table.count; ++i) {
1528 if(table.pins[i].is_input) {
1532 if(stmt.params.size() != input_count) {
1534 "non-formal call must cover every input pin");
1537 for(std::size_t i = 0; i < stmt.params.size(); ++i) {
1538 info.param_pins.push_back(
static_cast<std::uint8_t
>(i));
1539 check_expr(stmt.params[i].value,
1540 want(table.pins[i].type,
1546 std::vector<std::uint8_t> used;
1547 for(
const CallParam ¶m : stmt.params) {
1548 const std::string pin_lower = lower_copy(param.pin);
1549 const int pin = find_pin(
type, pin_lower);
1553 info.param_pins.push_back(0xFF);
1556 if(!table.pins[pin].is_input) {
1559 info.param_pins.push_back(0xFF);
1563 for(
const std::uint8_t prior : used) {
1570 param.column, param.pin);
1571 info.param_pins.push_back(0xFF);
1574 used.push_back(
static_cast<std::uint8_t
>(pin));
1575 info.param_pins.push_back(
static_cast<std::uint8_t
>(pin));
1576 check_expr(param.value,
1577 want(table.pins[pin].type,
1590 bool saw_real =
false;
1591 bool saw_time =
false;
1592 bool saw_bool =
false;
1593 anchor_found_ =
false;
1594 const Type anchored = anchor_walk(index, saw_real, saw_time, saw_bool);
1610 Type anchor_walk(
ExprIndex index,
bool &saw_real,
bool &saw_time,
1611 bool &saw_bool)
const
1616 const Expr &expr = ast_.exprs[
static_cast<std::size_t
>(index)];
1630 anchor_found_ =
true;
1631 return expr.literal_type;
1635 const TypeDesc *desc = result_.types.get(
id);
1637 anchor_found_ =
true;
1648 anchor_found_ =
true;
1651 anchor_found_ =
true;
1654 anchor_found_ =
true;
1659 anchor_found_ =
true;
1662 NamedConversion named;
1663 if(resolve_named_conversion(expr.name, named)) {
1664 anchor_found_ =
true;
1670 const int var = find_var(lower_copy(expr.name));
1672 anchor_found_ =
true;
1673 if(expr.access.empty()) {
1674 return result_.vars[
static_cast<std::size_t
>(var)].
type;
1676 return value_type(access_result_type(
1677 result_.vars[
static_cast<std::size_t
>(var)].type_id,
1683 const int var = find_var(lower_copy(expr.name));
1685 anchor_found_ =
true;
1686 if(expr.access.empty()) {
1687 return result_.vars[
static_cast<std::size_t
>(var)].
type;
1689 return value_type(access_result_type(
1690 result_.vars[
static_cast<std::size_t
>(var)].type_id,
1693 const int fb = find_fb(lower_copy(expr.name));
1696 result_.fbs[
static_cast<std::size_t
>(
fb)].
type;
1697 const int pin = find_pin(
type, lower_copy(expr.pin));
1699 anchor_found_ =
true;
1707 anchor_walk(expr.lhs, saw_real, saw_time, saw_bool);
1709 anchor_found_ =
true;
1715 switch(expr.binary_op) {
1725 anchor_found_ =
true;
1731 anchor_walk(expr.lhs, saw_real, saw_time, saw_bool);
1735 return anchor_walk(expr.rhs, saw_real, saw_time, saw_bool);
1743 bool check_expr(
ExprIndex index, Expected expected)
1746 static_cast<std::size_t
>(index) >= ast_.exprs.size()) {
1749 const Expr &expr = ast_.exprs[
static_cast<std::size_t
>(index)];
1750 ExprInfo &info = result_.exprs[
static_cast<std::size_t
>(index)];
1754 return literal_typed(expr, info, expected);
1756 return literal_enum(expr, info, expected);
1759 return string_literal(expr, info, expected);
1761 return date_literal(expr, info, expected,
Type::date,
1764 return date_literal(expr, info, expected,
Type::tod,
1768 case ExprKind::call:
return conversion_call(expr, info, expected);
1771 if(expected.has && expected.type !=
Type::bool_) {
1772 return mismatch(expr, expected.type,
Type::bool_);
1776 info.is_const =
true;
1777 info.bits = expr.unsigned_value ? 1 : 0;
1781 if(expected.has && expected.type !=
Type::time) {
1782 return mismatch(expr, expected.type,
Type::time);
1786 info.is_const =
true;
1787 info.bits =
static_cast<std::uint64_t
>(expr.signed_value);
1796 "aggregate initializer is only valid in a declaration");
1802 bool mismatch(
const Expr &expr,
Type expected,
Type actual)
1804 std::string note =
"expected ";
1812 bool nominal_mismatch(
const Expr &expr,
TypeId expected,
TypeId actual)
1814 const TypeDesc *want_desc = result_.types.get(expected);
1815 const TypeDesc *got_desc = result_.types.get(actual);
1816 std::string note =
"expected ";
1817 note += want_desc ? want_desc->name :
"?";
1819 note += got_desc ? got_desc->name :
"?";
1824 bool value_in_nominal(
TypeId id, std::uint64_t bits)
const
1826 const TypeDesc *desc = result_.types.get(
id);
1827 if(desc ==
nullptr) {
1831 for(
const EnumItem &item : desc->enum_items) {
1832 const std::uint64_t value =
static_cast<std::uint64_t
>(
1833 item.value.as_signed());
1844 const std::int64_t value =
static_cast<std::int64_t
>(bits);
1845 return value >= desc->subrange.lower.as_signed() &&
1846 value <= desc->subrange.upper.as_signed();
1848 return bits >= desc->subrange.lower.as_unsigned() &&
1849 bits <= desc->subrange.upper.as_unsigned();
1852 static void append_u32(std::vector<std::uint8_t> &bytes,
1853 std::uint32_t value)
1855 bytes.push_back(
static_cast<std::uint8_t
>(value));
1856 bytes.push_back(
static_cast<std::uint8_t
>(value >> 8U));
1857 bytes.push_back(
static_cast<std::uint8_t
>(value >> 16U));
1858 bytes.push_back(
static_cast<std::uint8_t
>(value >> 24U));
1861 static bool decode_utf8(
const std::string &text,
1862 std::vector<std::uint8_t> &bytes,
1863 std::vector<std::uint32_t> &scalars)
1865 std::vector<std::uint8_t> escaped;
1866 escaped.reserve(text.size());
1867 for(std::size_t i = 0; i < text.size(); ++i) {
1868 const std::uint8_t c =
static_cast<std::uint8_t
>(text[i]);
1869 if(c !=
static_cast<std::uint8_t
>(
'$')) {
1870 escaped.push_back(c);
1873 if(i + 1 >= text.size()) {
1876 const char next = text[++i];
1878 escaped.push_back(
static_cast<std::uint8_t
>(
'$'));
1879 }
else if(next ==
'N' || next ==
'n') {
1880 escaped.push_back(
static_cast<std::uint8_t
>(
'\n'));
1885 for(std::size_t i = 0; i < escaped.size();) {
1886 const std::uint8_t lead = escaped[i];
1887 std::uint32_t scalar = 0;
1888 std::size_t
count = 0;
1892 }
else if(lead >= 0xC2U && lead <= 0xDFU) {
1893 scalar = lead & 0x1FU;
1895 }
else if(lead >= 0xE0U && lead <= 0xEFU) {
1896 scalar = lead & 0x0FU;
1898 }
else if(lead >= 0xF0U && lead <= 0xF4U) {
1899 scalar = lead & 0x07U;
1904 if(i +
count > escaped.size()) {
1907 for(std::size_t j = 1; j <
count; ++j) {
1908 const std::uint8_t part = escaped[i + j];
1909 if((part & 0xC0U) != 0x80U) {
1912 scalar = (scalar << 6U) | (part & 0x3FU);
1914 if((
count == 3 && lead == 0xE0U && escaped[i + 1] < 0xA0U) ||
1915 (
count == 3 && lead == 0xEDU && escaped[i + 1] >= 0xA0U) ||
1916 (
count == 4 && lead == 0xF0U && escaped[i + 1] < 0x90U) ||
1917 (
count == 4 && lead == 0xF4U && escaped[i + 1] >= 0x90U) ||
1918 scalar > 0x10FFFFU ||
1919 (scalar >= 0xD800U && scalar <= 0xDFFFU)) {
1922 scalars.push_back(scalar);
1925 bytes.swap(escaped);
1929 bool string_literal(
const Expr &expr, ExprInfo &info, Expected expected)
1931 std::vector<std::uint8_t> bytes;
1932 std::vector<std::uint32_t> scalars;
1933 if(!decode_utf8(expr.text, bytes, scalars)) {
1939 if(expected.has && expected.type == (wide ?
Type::wchar
1941 if((wide && scalars.size() != 1U) ||
1942 (!wide && bytes.size() != 1U)) {
1947 info.type = expected.type;
1948 info.type_id = expected.type_id;
1949 info.is_const =
true;
1950 info.bits = wide ? scalars[0] : bytes[0];
1955 if(!expected.has || expected.type != string_type) {
1956 return mismatch(expr, expected.has ? expected.type : string_type,
1959 const TypeDesc *desc = result_.types.get(expected.type_id);
1960 if(desc ==
nullptr ||
1964 const std::size_t length = wide ? scalars.size() : bytes.size();
1965 if(length > desc->string.capacity) {
1970 const std::uint64_t added = 4U + (wide ? scalars.size() * 4ULL
1972 if(added > 65536U - result_.string_constant_bytes) {
1974 "string constant pool");
1977 result_.string_constant_bytes +=
static_cast<std::uint32_t
>(added);
1978 info.object_bytes.reserve(
static_cast<std::size_t
>(desc->size));
1979 append_u32(info.object_bytes,
static_cast<std::uint32_t
>(length));
1981 for(
const std::uint32_t scalar : scalars) {
1982 append_u32(info.object_bytes, scalar);
1985 info.object_bytes.insert(info.object_bytes.end(), bytes.begin(),
1988 info.object_bytes.resize(
static_cast<std::size_t
>(desc->size), 0);
1989 info.type = string_type;
1990 info.type_id = expected.type_id;
1991 info.object_constant =
true;
1993 result_.max_string_operation_cost = std::max(
1994 result_.max_string_operation_cost,
1995 static_cast<std::uint32_t
>(desc->string.capacity));
1999 bool date_literal(
const Expr &expr, ExprInfo &info, Expected expected,
2002 if(expected.has && expected.type !=
type) {
2003 return mismatch(expr, expected.type,
type);
2007 info.is_const =
true;
2008 info.bits =
static_cast<std::uint64_t
>(expr.signed_value);
2013 bool literal_enum(
const Expr &expr, ExprInfo &info, Expected expected)
2021 const TypeDesc *desc = result_.types.get(
id);
2024 expr.name +
" is not an enum");
2027 if(expected.has && expected.type_id !=
id) {
2028 return nominal_mismatch(expr, expected.type_id,
id);
2031 if(result_.types.enum_value(
id, expr.pin, value) !=
TypeError::ok) {
2038 info.is_const =
true;
2039 info.bits =
static_cast<std::uint64_t
>(value.as_signed());
2047 bool int_payload_bits(
const Expr &expr,
Type target,
2048 std::uint64_t magnitude,
bool negative,
bool based,
2049 std::uint64_t &bits)
2052 const std::uint64_t umax =
2053 width >= 64 ? ~0ULL : ((1ULL << width) - 1ULL);
2057 expr.column,
"based literals cannot be negated");
2060 if(magnitude > umax) {
2061 return out_of_range(expr);
2067 if(negative || magnitude > umax) {
2068 return out_of_range(expr);
2074 const std::uint64_t smax =
2075 width >= 64 ? 0x7FFFFFFFFFFFFFFFULL
2076 : ((1ULL << (width - 1)) - 1ULL);
2078 if(magnitude > smax + 1ULL) {
2079 return out_of_range(expr);
2081 bits = 0ULL - magnitude;
2084 if(magnitude > smax) {
2085 return out_of_range(expr);
2091 bool literal_int(
const Expr &expr, ExprInfo &info, Expected expected)
2093 const TypeDesc *nominal =
2094 expected.has ? result_.types.get(expected.type_id) :
nullptr;
2096 return nominal_mismatch(expr, expected.type_id,
builtin::dint);
2099 const std::uint64_t magnitude = expr.unsigned_value;
2100 const bool negative = expr.signed_value < 0;
2105 double as_real =
static_cast<double>(magnitude);
2110 const float narrowed =
static_cast<float>(as_real);
2117 info.type_id = expected.has ? expected.type_id :
st::type_id(target);
2118 info.is_const =
true;
2125 std::uint64_t bits = 0;
2126 if(!int_payload_bits(expr, target, magnitude, negative, expr.based,
2131 info.type_id = expected.has ? expected.type_id :
st::type_id(target);
2132 info.is_const =
true;
2136 !value_in_nominal(expected.type_id, bits)) {
2146 bool literal_typed(
const Expr &expr, ExprInfo &info, Expected expected)
2148 const Type own = expr.literal_type;
2149 if(expected.has && own != expected.type &&
2151 return mismatch(expr, expected.type, own);
2153 const bool negative = expr.signed_value < 0;
2154 if(expr.real_form) {
2157 "real payload on a non-real typed literal");
2162 static_cast<float>(expr.real_value)))
2165 double as_real =
static_cast<double>(expr.unsigned_value);
2171 static_cast<float>(as_real)))
2174 info.bits = expr.unsigned_value ? 1 : 0;
2176 std::uint64_t bits = 0;
2177 if(!int_payload_bits(expr, own, expr.unsigned_value, negative,
2178 expr.based, bits)) {
2183 info.type = expected.has ? expected.type : own;
2184 info.type_id = expected.has ? expected.type_id :
st::type_id(own);
2185 info.is_const =
true;
2187 const TypeDesc *nominal =
2188 expected.has ? result_.types.get(expected.type_id) :
nullptr;
2190 !value_in_nominal(expected.type_id, info.bits)) {
2198 bool literal_real(
const Expr &expr, ExprInfo &info, Expected expected)
2202 const float narrowed =
static_cast<float>(expr.real_value);
2210 info.type_id = expected.has ? expected.type_id :
st::type_id(target);
2211 info.is_const =
true;
2216 bool out_of_range(
const Expr &expr)
2222 bool standard_error(
const Expr &expr,
DiagCode code)
2224 diag(code, expr.line, expr.column, expr.name);
2228 Type standard_join(
const Expr &expr, std::size_t first,
Type fallback,
2229 bool &compatible)
const
2231 Type joined = fallback;
2233 Type literal_fallback = fallback;
2235 for(std::size_t i = first; i < expr.arguments.size(); ++i) {
2236 const Type candidate = anchor_type(expr.arguments[i], fallback);
2237 const bool candidate_found = anchor_found_;
2238 if(!candidate_found) {
2239 if(candidate != fallback) literal_fallback = candidate;
2245 }
else if(candidate == joined ||
widens_to(candidate, joined)) {
2247 }
else if(
widens_to(joined, candidate)) {
2253 return found ? joined : literal_fallback;
2256 bool check_standard_args(
const Expr &expr, std::size_t first,
2259 for(std::size_t i = first; i < expr.arguments.size(); ++i) {
2260 if(!check_expr(expr.arguments[i], expected))
return false;
2265 bool standard_function_call(
const Expr &expr, ExprInfo &info,
2268 const std::size_t argc = expr.arguments.size();
2269 const auto arity = [argc](std::size_t low, std::size_t high) {
2270 return argc >= low && argc <= high;
2272 info.standard_function = function;
2273 info.standard_argc =
static_cast<std::uint8_t
>(argc);
2274 info.standard_cost =
static_cast<std::uint32_t
>(
2275 std::max<std::size_t>(argc, 1U));
2276 result_.max_standard_function_cost = std::max(
2277 result_.max_standard_function_cost,
2278 info.standard_cost);
2282 return standard_string_function(expr, info, expected, function);
2289 if((variadic && !arity(2, 32)) ||
2301 ast_.exprs[
static_cast<std::size_t
>(expr.arguments[0])].kind ==
2303 ast_.exprs[
static_cast<std::size_t
>(expr.arguments[0])].based) {
2309 bool compatible =
true;
2326 if(!check_expr(expr.arguments[0], want(
left)) ||
2327 !check_expr(expr.arguments[1], want(
right))) {
2334 !check_expr(expr.arguments[0], want(bits)) ||
2335 !check_expr(expr.arguments[1], want(
Type::dint))) {
2338 const ExprInfo &
count = result_.exprs[
static_cast<std::size_t
>(expr.arguments[1])];
2339 if(
count.is_const &&
static_cast<std::int64_t
>(
count.bits) < 0) {
2345 const Type operand = standard_join(expr, 0,
Type::dint, compatible);
2348 !check_standard_args(expr, 0, want(operand))) {
2354 constexpr std::size_t first = 1U;
2355 result_type = standard_join(expr, first,
Type::dint, compatible);
2357 !check_expr(expr.arguments[0],
2359 !check_standard_args(expr, first, want(result_type))) {
2364 result_type = standard_join(expr, 0,
Type::dint, compatible);
2369 if(!compatible || !numeric || (transcendental && !
is_real_family(result_type)) ||
2372 !check_standard_args(expr, 0, want(result_type))) {
2377 const ExprInfo &minimum = result_.exprs[
static_cast<std::size_t
>(expr.arguments[0])];
2378 const ExprInfo &maximum = result_.exprs[
static_cast<std::size_t
>(expr.arguments[2])];
2379 if(minimum.is_const && maximum.is_const) {
2383 ? minimum.bits > maximum.bits
2384 : static_cast<std::int64_t>(minimum.bits) >
2385 static_cast<std::int64_t>(maximum.bits);
2391 if(expected.has && expected.type != result_type &&
2392 !
widens_to(result_type, expected.type)) {
2393 return mismatch(expr, expected.type, result_type);
2395 info.type = result_type;
2396 info.type_id = result_id;
2401 bool standard_string_function(
const Expr &expr, ExprInfo &info,
2405 const std::size_t argc = expr.arguments.size();
2424 string_type = expected.type;
2425 string_id = expected.type_id;
2430 string_type = anchored;
2431 if(!check_expr(expr.arguments[0], Expected{}))
2433 const ExprInfo &first = result_.exprs[
static_cast<std::size_t
>(expr.arguments[0])];
2435 : first.storage_type_id;
2437 const TypeDesc *string_desc = result_.types.get(string_id);
2440 for(std::size_t i = result_string ? 0U : 1U; i < argc; ++i) {
2447 if(!check_expr(expr.arguments[i], scalar ? want(
Type::dint)
2448 : want(string_type, string_id)))
2451 std::uint32_t capacities[32] = {};
2452 std::uint8_t string_count = 0;
2453 for(
const ExprIndex argument : expr.arguments) {
2454 const ExprInfo &argument_info =
2455 result_.exprs[
static_cast<std::size_t
>(argument)];
2458 const TypeId argument_id =
2460 ? argument_info.type_id : argument_info.storage_type_id;
2461 const TypeDesc *argument_desc = result_.types.get(argument_id);
2462 if(argument_desc !=
nullptr) {
2463 capacities[string_count++] =
static_cast<std::uint32_t
>(
2464 argument_desc->string.capacity);
2468 function, capacities, string_count,
2469 result_string ?
static_cast<std::uint32_t
>(string_desc->string.capacity)
2471 result_.max_string_operation_cost = std::max(
2472 result_.max_string_operation_cost, info.standard_cost);
2473 result_.max_standard_function_cost = std::max(
2474 result_.max_standard_function_cost, info.standard_cost);
2475 info.standard_function = function;
2476 info.standard_argc =
static_cast<std::uint8_t
>(argc);
2478 if(!result_string) {
2483 const std::uint32_t aligned = (result_.vars_bytes + 7U) & ~7U;
2484 if(aligned > limits_.max_vars_bytes || string_desc->size >
2485 limits_.max_vars_bytes - aligned) {
2488 info.type = string_type;
2489 info.type_id = string_id;
2490 info.storage_type_id = string_id;
2491 info.offset = aligned;
2492 info.memory_access =
true;
2493 result_.vars_bytes = aligned +
static_cast<std::uint32_t
>(string_desc->size);
2494 if(result_.initial_data.size() < result_.vars_bytes)
2495 result_.initial_data.resize(result_.vars_bytes, 0);
2504 bool conversion_call(
const Expr &expr, ExprInfo &info, Expected expected)
2506 const std::string conversion_name = lower_copy(expr.name);
2509 return standard_function_call(expr, info, expected, standard);
2511 if(conversion_name ==
"l2b_alias_guard") {
2512 if(expected.has && expected.type !=
Type::dint) {
2513 return mismatch(expr, expected.type,
Type::dint);
2515 if(!check_expr(expr.lhs, want(
Type::dint)))
return false;
2521 if(conversion_name ==
"len") {
2522 if(expected.has && expected.type !=
Type::dint) {
2523 return mismatch(expr, expected.type,
Type::dint);
2525 if(!check_expr(expr.lhs, Expected{}))
return false;
2526 const ExprInfo &argument =
2527 result_.exprs[
static_cast<std::size_t
>(expr.lhs)];
2528 const TypeDesc *desc = result_.types.get(
2531 : argument.storage_type_id);
2535 "LEN requires STRING or WSTRING");
2541 result_.max_string_operation_cost = std::max(
2542 result_.max_string_operation_cost,
2543 static_cast<std::uint32_t
>(desc->string.capacity));
2548 bool unicode_check =
false;
2549 if(conversion_name ==
"usint_to_char") {
2552 }
else if(conversion_name ==
"char_to_usint") {
2555 }
else if(conversion_name ==
"udint_to_wchar") {
2558 unicode_check =
true;
2559 }
else if(conversion_name ==
"wchar_to_udint") {
2564 if(expected.has && expected.type != special_to) {
2565 return mismatch(expr, expected.type, special_to);
2567 if(!check_expr(expr.lhs, want(special_from))) {
2570 const ExprInfo &arg =
2571 result_.exprs[
static_cast<std::size_t
>(expr.lhs)];
2572 if(unicode_check && arg.is_const &&
2573 (arg.bits > 0x10FFFFU ||
2574 (arg.bits >= 0xD800U && arg.bits <= 0xDFFFU))) {
2575 return out_of_range(expr);
2577 info.type = special_to;
2581 info.is_const =
true;
2582 info.bits = arg.bits;
2586 NamedConversion named;
2587 if(resolve_named_conversion(expr.name, named)) {
2588 if(expected.has && expected.type_id != named.to_id &&
2590 named.to == expected.type &&
2592 return nominal_mismatch(expr, expected.type_id, named.to_id);
2594 if(!check_expr(expr.lhs, want(named.from, named.from_id))) {
2597 const ExprInfo &arg =
2598 result_.exprs[
static_cast<std::size_t
>(expr.lhs)];
2599 info.type = named.to;
2600 info.type_id = named.to_id;
2606 !value_in_nominal(named.to_id, converted)) {
2607 const TypeDesc *desc = result_.types.get(named.to_id);
2609 expr.column, desc ? desc->name : expr.name);
2612 info.is_const =
true;
2613 info.bits = converted;
2623 if(expected.has && desc.to != expected.type &&
2625 return mismatch(expr, expected.type, desc.to);
2627 if(!check_expr(expr.lhs, want(desc.from))) {
2630 const ExprInfo &arg = result_.exprs[
static_cast<std::size_t
>(expr.lhs)];
2631 info.type = expected.has ? expected.type : desc.to;
2632 info.type_id = expected.has ? expected.type_id :
st::type_id(desc.to);
2633 info.fb_index =
static_cast<std::uint16_t
>(desc.to);
2634 info.pin_id =
static_cast<std::uint8_t
>(desc.kind);
2635 info.slot = desc.trunc ? 1 : 0;
2638 std::uint64_t folded = 0;
2639 if(fold_conversion(desc, arg.bits, folded)) {
2640 info.is_const =
true;
2647 static bool fold_conversion(
const ConvDesc &desc, std::uint64_t arg,
2659 const double value =
2660 static_cast<double>(
static_cast<std::int64_t
>(arg));
2663 static_cast<float>(value)))
2668 const double value =
static_cast<double>(arg);
2671 static_cast<float>(value)))
2677 if(!std::isfinite(value)) {
2680 const double adjusted =
2681 desc.trunc ? std::trunc(value) : std::nearbyint(value);
2690 out = arg != 0 ? 1 : 0;
2700 bool resolve_access(
const std::vector<AccessStep> &steps,
2702 std::vector<ExprInfo::DynamicIndex> &dynamic,
2703 std::int32_t line, std::int32_t column)
2705 for(
const AccessStep &
step : steps) {
2706 const TypeDesc *desc = result_.types.get(
type_id);
2708 const StructField *
field =
nullptr;
2713 "field access requires a STRUCT member");
2716 offset +=
static_cast<std::uint32_t
>(
field->offset);
2720 step.indices.size() != desc->array.dimensions.size()) {
2722 "array subscript dimension mismatch");
2725 for(std::size_t i = 0; i <
step.indices.size(); ++i) {
2728 TypeId index_id = nominal_anchor(index);
2729 const TypeDesc *index_desc = result_.types.get(index_id);
2730 if(index_desc !=
nullptr &&
2733 }
else if(index_desc !=
nullptr &&
2736 ast_.exprs[
static_cast<std::size_t
>(index)];
2738 "enum is not an array index");
2744 !check_expr(index, want(index_type, index_id))) {
2746 ast_.exprs[
static_cast<std::size_t
>(index)];
2748 "array index must be integer");
2751 const ExprInfo &index_info =
2752 result_.exprs[
static_cast<std::size_t
>(index)];
2753 const ArrayDimension &dimension =
2754 desc->array.dimensions[i];
2755 if(index_info.is_const) {
2757 ? (index_info.bits >
static_cast<std::uint64_t
>(
2758 std::numeric_limits<std::int64_t>::max())
2759 ? std::numeric_limits<std::int64_t>::max()
2760 : static_cast<std::int64_t>(index_info.bits))
2761 : static_cast<std::int64_t>(index_info.bits);
2762 if(value < dimension.lower || value > dimension.upper) {
2764 ast_.exprs[static_cast<std::size_t>(index)];
2765 diag(DiagCode::sema_index_out_of_range, at.line,
2769 offset +=
static_cast<std::uint32_t
>(
2770 static_cast<std::uint64_t
>(value - dimension.lower) *
2773 dynamic.push_back({index, dimension.lower,
2778 type_id = desc->array.element;
2780 const TypeDesc *next = result_.types.get(
type_id);
2781 if(next ==
nullptr) {
2797 const TypeDesc *desc = result_.types.get(
type_id);
2798 if(desc ==
nullptr)
return false;
2801 return type_contains_reference(desc->array.element);
2804 for(
const StructField &
field : desc->structure.fields) {
2805 if(type_contains_reference(
field.type))
return true;
2811 bool variable(
const Expr &expr, ExprInfo &info, Expected expected)
2813 const std::string lower = lower_copy(expr.name);
2814 const int var = find_var(lower);
2816 if(find_fb(lower) >= 0) {
2818 expr.column,
"FB instance used as a value");
2825 const VarInfo &decl = result_.vars[
static_cast<std::size_t
>(var)];
2826 info.offset = decl.offset;
2827 Type actual_type = decl.type;
2828 TypeId actual_id = decl.type_id;
2829 const TypeDesc *root_desc = result_.types.get(actual_id);
2830 if(root_desc !=
nullptr &&
2833 !expr.access.empty()) {
2834 if(expr.access.size() != 1U || expr.access[0].field ||
2835 expr.access[0].indices.size() != 1U) {
2837 "string index requires one subscript");
2840 const ExprIndex index = expr.access[0].indices[0];
2843 !check_expr(index, want(index_type))) {
2845 "string index must be integer");
2848 const ExprInfo &index_info =
2849 result_.exprs[
static_cast<std::size_t
>(index)];
2850 if(index_info.is_const) {
2851 const std::int64_t value =
2852 static_cast<std::int64_t
>(index_info.bits);
2853 const std::uint64_t upper = decl.constant
2854 ? decl.initial_length
2855 : root_desc->string.capacity;
2856 if(value < 1 ||
static_cast<std::uint64_t
>(value) > upper) {
2857 const Expr &at = ast_.exprs[
static_cast<std::size_t
>(index)];
2863 result_.max_string_operation_cost = std::max(
2864 result_.max_string_operation_cost,
2865 static_cast<std::uint32_t
>(root_desc->string.capacity));
2870 if(expected.has && expected.type != actual_type) {
2871 return mismatch(expr, expected.type, actual_type);
2873 info.type = actual_type;
2874 info.type_id = actual_id;
2875 info.storage_type_id = decl.type_id;
2876 info.string_index = index;
2878 info.memory_access =
true;
2881 if(!resolve_access(expr.access, info.offset, actual_type, actual_id,
2882 info.dynamic_indices, expr.line, expr.column)) {
2885 const TypeDesc *decl_desc = result_.types.get(actual_id);
2886 const TypeDesc *expected_desc =
2887 expected.has ? result_.types.get(expected.type_id) :
nullptr;
2888 bool compatible = !expected.has ||
2889 (actual_id == expected.type_id &&
2890 actual_type == expected.type);
2891 if(expected.has && !compatible && decl_desc !=
nullptr &&
2892 expected_desc !=
nullptr &&
2899 if(expected.has && !compatible && decl_desc !=
nullptr &&
2902 compatible = actual_type == expected.type ||
2907 compatible = actual_type == expected.type ||
2910 if(expected.has && !compatible) {
2913 if(decl_desc !=
nullptr || expected_desc !=
nullptr) {
2914 return nominal_mismatch(expr, expected.type_id, actual_id);
2916 return mismatch(expr, expected.type, actual_type);
2918 info.type = expected.has ? expected.type : actual_type;
2919 info.type_id = expected.has ? expected.type_id : actual_id;
2920 info.storage_type_id = actual_id;
2922 info.memory_access =
true;
2923 if(decl.constant && expr.access.empty()) {
2924 info.is_const =
true;
2925 info.bits = decl.init_bits;
2930 bool pin_read(
const Expr &expr, ExprInfo &info, Expected expected)
2932 if(find_var(lower_copy(expr.name)) >= 0) {
2933 return variable(expr, info, expected);
2935 const int fb = find_fb(lower_copy(expr.name));
2942 const int pin = find_pin(
type, lower_copy(expr.pin));
2954 if(expected.has && desc.type != expected.type &&
2956 return mismatch(expr, expected.type, desc.type);
2958 info.
type = expected.has ? expected.type : desc.type;
2959 info.type_id = expected.has ? expected.type_id :
pin_type_id(desc);
2960 info.fb_index =
static_cast<std::uint16_t
>(
fb);
2961 info.pin_id =
static_cast<std::uint8_t
>(pin);
2966 bool unary(
const Expr &expr, ExprInfo &info, Expected expected)
2973 target = expected.type;
2983 if(!check_expr(expr.lhs, want(target))) {
2986 const ExprInfo &operand =
2987 result_.exprs[
static_cast<std::size_t
>(expr.lhs)];
2991 if(operand.is_const) {
2992 info.is_const =
true;
2994 ? (operand.bits ? 0 : 1)
2995 : detail::
canon(target, ~operand.bits);
3002 "unary arithmetic produces the base type");
3005 Expected inner = expected;
3007 inner = want(anchor_type(expr.lhs,
Type::dint));
3011 "unary '-' needs a numeric operand");
3014 if(!check_expr(expr.lhs, inner)) {
3017 const ExprInfo &operand =
3018 result_.exprs[
static_cast<std::size_t
>(expr.lhs)];
3019 info.type = inner.type;
3020 info.type_id = inner.type_id;
3022 if(operand.is_const) {
3023 info.is_const =
true;
3024 switch(inner.type) {
3026 const float value = -
static_cast<float>(
3041 0,
static_cast<std::int64_t
>(operand.bits))));
3048 static bool is_comparison(
BinaryOp op)
3063 static bool is_logical(
BinaryOp op)
3074 Type left = anchor_type(lhs, fallback);
3075 const bool left_found = anchor_found_;
3076 Type right = anchor_type(rhs, fallback);
3077 const bool right_found = anchor_found_;
3078 if(!left_found && !right_found) {
3096 bool binary(
const Expr &expr, ExprInfo &info, Expected expected)
3098 if(is_logical(expr.binary_op)) {
3103 target = expected.type;
3114 const bool lhs_ok = check_expr(expr.lhs, want(target));
3115 const bool rhs_ok = check_expr(expr.rhs, want(target));
3116 if(!lhs_ok || !rhs_ok) {
3122 fold_logical(expr, info, target);
3125 if(is_comparison(expr.binary_op)) {
3126 if(expected.has && expected.type !=
Type::bool_) {
3127 return mismatch(expr, expected.type,
Type::bool_);
3129 const Type joined = join_anchors(expr.lhs, expr.rhs,
Type::dint);
3131 TypeId string_id = nominal_anchor(expr.lhs);
3132 const TypeDesc *string_desc = result_.types.get(string_id);
3133 if(string_desc ==
nullptr ||
3136 string_id = nominal_anchor(expr.rhs);
3137 string_desc = result_.types.get(string_id);
3139 if(string_desc ==
nullptr ||
3144 expr.column,
"string width mismatch");
3148 check_expr(expr.lhs, want(joined, string_id));
3150 check_expr(expr.rhs, want(joined, string_id));
3151 if(!lhs_ok || !rhs_ok) {
3157 result_.max_string_operation_cost = std::max(
3158 result_.max_string_operation_cost,
3159 static_cast<std::uint32_t
>(string_desc->string.capacity));
3160 for(
const ExprIndex operand : {expr.lhs, expr.rhs}) {
3161 const ExprInfo &operand_info = result_.exprs[
3162 static_cast<std::size_t
>(operand)];
3163 const TypeDesc *operand_desc = result_.types.get(
3165 ? operand_info.type_id
3166 : operand_info.storage_type_id);
3167 if(operand_desc !=
nullptr) {
3168 result_.max_string_operation_cost = std::max(
3169 result_.max_string_operation_cost,
3170 static_cast<std::uint32_t
>(
3171 operand_desc->string.capacity));
3176 const TypeId left_nominal = nominal_anchor(expr.lhs);
3177 const TypeId right_nominal = nominal_anchor(expr.rhs);
3178 const TypeDesc *left_desc = result_.types.get(left_nominal);
3179 const TypeDesc *right_desc = result_.types.get(right_nominal);
3182 if(left_desc ==
nullptr || right_desc ==
nullptr ||
3183 left_nominal != right_nominal) {
3184 return nominal_mismatch(expr, left_nominal,
3193 "aggregate comparison is not defined");
3198 expr.column,
"enum ordering is not defined");
3203 ? left_desc->enum_base
3204 : left_desc->subrange.base);
3206 check_expr(expr.lhs, want(operand, left_nominal));
3208 check_expr(expr.rhs, want(operand, right_nominal));
3209 if(!lhs_ok || !rhs_ok) {
3215 fold_compare(expr, info, operand);
3218 const Type operand = join_anchors(expr.lhs, expr.rhs,
Type::dint);
3225 "ordering comparison on BOOL/bit strings");
3228 const bool lhs_ok = check_expr(expr.lhs, want(operand));
3229 const bool rhs_ok = check_expr(expr.rhs, want(operand));
3230 if(!lhs_ok || !rhs_ok) {
3236 fold_compare(expr, info, operand);
3240 return power_op(expr, info, expected);
3247 if(expected.has && expected.type != left_type) {
3248 return mismatch(expr, expected.type, left_type);
3252 if(!check_expr(expr.lhs, want(left_type)) ||
3253 !check_expr(expr.rhs, want(rhs_type))) {
3256 info.type = left_type;
3263 ((expected.has && expected.type ==
Type::time) ||
3265 return time_scale(expr, info, expected);
3269 const TypeDesc *desc = result_.types.get(expected.type_id);
3270 if(desc !=
nullptr) {
3272 "arithmetic produces the base type");
3276 Expected operand = expected;
3278 operand = want(join_anchors(expr.lhs, expr.rhs,
Type::dint));
3280 const bool located_memory_add =
3282 is_located_memory_expr(expr.lhs);
3283 if(!valid_arith(expr.binary_op, operand.type) &&
3284 !located_memory_add) {
3286 "operator not defined for this type");
3289 const bool lhs_ok = check_expr(expr.lhs, operand);
3290 const bool rhs_ok = check_expr(expr.rhs, operand);
3291 if(!lhs_ok || !rhs_ok) {
3294 info.type = operand.type;
3295 info.type_id = operand.type_id;
3298 const ExprInfo &rhs = result_.exprs[
static_cast<std::size_t
>(expr.rhs)];
3301 if(divides &&
is_integer(operand.type) && rhs.is_const &&
3307 fold_arith(expr, info, operand.type);
3314 bool power_op(
const Expr &expr, ExprInfo &info, Expected expected)
3318 base = expected.type;
3324 "** base must be REAL or LREAL");
3327 if(!check_expr(expr.lhs, want(
base))) {
3330 const Type exp_anchor = anchor_type(expr.rhs,
base);
3331 const Type exp_type =
3333 if(!check_expr(expr.rhs, want(exp_type))) {
3339 "** exponent must be numeric");
3350 bool time_scale(
const Expr &expr, ExprInfo &info, Expected expected)
3352 if(expected.has && expected.type !=
Type::time) {
3353 return mismatch(expr, expected.type,
Type::time);
3355 if(!check_expr(expr.lhs, want(
Type::time))) {
3361 "TIME scale must be numeric");
3364 if(!check_expr(expr.rhs, want(scale_type))) {
3367 const ExprInfo &rhs =
3368 result_.exprs[
static_cast<std::size_t
>(expr.rhs)];
3379 const ExprInfo &lhs =
3380 result_.exprs[
static_cast<std::size_t
>(expr.lhs)];
3381 if(lhs.is_const && rhs.is_const) {
3382 const std::int64_t t =
static_cast<std::int64_t
>(lhs.bits);
3384 const std::int64_t
s =
static_cast<std::int64_t
>(rhs.bits);
3385 info.is_const =
true;
3386 info.bits =
static_cast<std::uint64_t
>(
3392 const double value =
3394 ?
static_cast<double>(t) *
s
3395 : static_cast<double>(t) /
s;
3396 if(std::isfinite(value)) {
3397 info.is_const =
true;
3423 bool is_located_memory_expr(
ExprIndex index)
const
3425 if(index ==
kNoExpr ||
static_cast<std::size_t
>(index) >=
3426 ast_.exprs.size()) {
3429 const Expr &expr = ast_.exprs[
static_cast<std::size_t
>(index)];
3433 const std::string lower = lower_copy(expr.name);
3434 for(
const LocatedVarInfo &var : result_.process_image.variables) {
3442 void fold_logical(
const Expr &expr, ExprInfo &info,
Type target)
3444 const ExprInfo &lhs = result_.exprs[
static_cast<std::size_t
>(expr.lhs)];
3445 const ExprInfo &rhs = result_.exprs[
static_cast<std::size_t
>(expr.rhs)];
3446 if(!lhs.is_const || !rhs.is_const) {
3449 info.is_const =
true;
3451 switch(expr.binary_op) {
3454 default: info.bits = lhs.bits ^ rhs.bits;
break;
3458 const bool a = lhs.bits != 0;
3459 const bool b = rhs.bits != 0;
3460 switch(expr.binary_op) {
3463 default: info.bits = (a != b) ? 1 : 0;
break;
3467 void fold_compare(
const Expr &expr, ExprInfo &info,
Type operand)
3469 const ExprInfo &lhs = result_.exprs[
static_cast<std::size_t
>(expr.lhs)];
3470 const ExprInfo &rhs = result_.exprs[
static_cast<std::size_t
>(expr.rhs)];
3471 if(!lhs.is_const || !rhs.is_const) {
3474 info.is_const =
true;
3479 switch(expr.binary_op) {
3485 default: value = a >= b;
break;
3488 const std::uint64_t a = lhs.bits;
3489 const std::uint64_t b = rhs.bits;
3490 switch(expr.binary_op) {
3496 default: value = a >= b;
break;
3499 const std::int64_t a =
static_cast<std::int64_t
>(lhs.bits);
3500 const std::int64_t b =
static_cast<std::int64_t
>(rhs.bits);
3501 switch(expr.binary_op) {
3507 default: value = a >= b;
break;
3510 info.bits = value ? 1 : 0;
3513 void fold_arith(
const Expr &expr, ExprInfo &info,
Type type)
3515 const ExprInfo &lhs = result_.exprs[
static_cast<std::size_t
>(expr.lhs)];
3516 const ExprInfo &rhs = result_.exprs[
static_cast<std::size_t
>(expr.rhs)];
3517 if(!lhs.is_const || !rhs.is_const) {
3524 switch(expr.binary_op) {
3528 default: value = a / b;
break;
3530 info.is_const =
true;
3538 switch(expr.binary_op) {
3542 default: value = a / b;
break;
3544 info.is_const =
true;
3551 std::uint64_t bits = 0;
3553 const std::uint64_t a = lhs.bits;
3554 const std::uint64_t b = rhs.bits;
3555 switch(expr.binary_op) {
3560 default: bits = a % b;
break;
3563 const std::int64_t a =
static_cast<std::int64_t
>(lhs.bits);
3564 const std::int64_t b =
static_cast<std::int64_t
>(rhs.bits);
3565 std::int64_t value = 0;
3566 switch(expr.binary_op) {
3574 value = b == -1 ? 0 : a % b;
3577 bits =
static_cast<std::uint64_t
>(value);
3579 info.is_const =
true;
3584 std::vector<Diagnostic> &diagnostics_;
3587 std::vector<std::string> control_stack_;
3588 int loop_depth_ = 0;
3589 std::size_t string_type_count_ = 0;
3590 bool has_error_ =
false;
3591 mutable bool anchor_found_ =
false;