• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nickg / nvc / 14691306008

27 Apr 2025 10:55AM UTC coverage: 92.319% (+0.009%) from 92.31%
14691306008

push

github

nickg
Fix assertion failure with large number of array sub-elements

15 of 16 new or added lines in 2 files covered. (93.75%)

71 existing lines in 4 files now uncovered.

69070 of 74817 relevant lines covered (92.32%)

422022.01 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.14
/src/sem.c
1
//
2
//  Copyright (C) 2011-2025  Nick Gasson
3
//
4
//  This program is free software: you can redistribute it and/or modify
5
//  it under the terms of the GNU General Public License as published by
6
//  the Free Software Foundation, either version 3 of the License, or
7
//  (at your option) any later version.
8
//
9
//  This program is distributed in the hope that it will be useful,
10
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
11
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
//  GNU General Public License for more details.
13
//
14
//  You should have received a copy of the GNU General Public License
15
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
//
17

18
#include "util.h"
19
#include "common.h"
20
#include "diag.h"
21
#include "hash.h"
22
#include "lib.h"
23
#include "mask.h"
24
#include "names.h"
25
#include "option.h"
26
#include "phase.h"
27
#include "psl/psl-phase.h"
28
#include "type.h"
29

30
#include <assert.h>
31
#include <stdlib.h>
32
#include <string.h>
33

34
typedef bool (*static_fn_t)(tree_t t);
35

36
typedef enum {
37
   MAP_MISSING,
38
   MAP_OPEN,
39
   MAP_PARTIAL,
40
   MAP_FULL
41
} map_state_t;
42

43
typedef struct {
44
   tree_t      decl;
45
   map_state_t state;
46
} formal_map_t;
47

48
static bool sem_check_array_ref(tree_t t, nametab_t *tab);
49
static bool sem_locally_static(tree_t t);
50
static bool sem_globally_static(tree_t t);
51
static tree_t sem_check_lvalue(tree_t t);
52
static bool sem_check_same_type(tree_t left, tree_t right);
53
static bool sem_check_type(tree_t t, type_t expect, nametab_t *tab);
54
static bool sem_static_name(tree_t t, static_fn_t check_fn);
55
static bool sem_static_subtype(type_t type, static_fn_t fn);
56
static bool sem_check_attr_ref(tree_t t, bool allow_range, nametab_t *tab);
57
static bool sem_check_generic_map(tree_t t, tree_t unit, nametab_t *tab);
58
static bool sem_check_port_map(tree_t t, tree_t unit, nametab_t *tab);
59
static bool sem_check_subtype(tree_t decl, type_t type, nametab_t *tab);
60
static bool sem_check_incomplete(tree_t t, type_t type);
61

62
#define sem_error(t, ...) do {                        \
63
      error_at(t ? tree_loc(t) : NULL , __VA_ARGS__); \
64
      return false;                                   \
65
   } while (0)
66

67
static bool sem_check_resolution(type_t type, tree_t res)
349✔
68
{
69
   // Resolution functions are described in LRM 93 section 2.4
70

71
   if (tree_kind(res) == T_ELEM_RESOLUTION) {
378✔
72
      // VHDL-2008 element resolution
73
      assert(standard() >= STD_08);
30✔
74

75
      if (type_is_array(type)) {
30✔
76
         tree_t sub = tree_value(tree_assoc(res, 0));
29✔
77
         return sem_check_resolution(type_elem(type), sub);
29✔
78
      }
79
      else if (type_is_record(type))
1✔
80
         sem_error(res, "sorry, record element resolution functions are not"
×
81
                   "supported yet");
82
      else {
83
         // Should have been caught during name resolution
84
         assert(error_count() > 0);
1✔
85
         return false;
86
      }
87
   }
88

89
   if (tree_kind(res) != T_REF) {
348✔
90
      // Should have been caught during name resolution
91
      assert(error_count() > 0);
1✔
92
      return false;
93
   }
94

95
   if (!tree_has_ref(res))
347✔
96
      return false;
97

98
   tree_t fdecl = tree_ref(res);
343✔
99
   assert(is_subprogram(fdecl));
343✔
100

101
   type_t ftype = tree_type(fdecl);
343✔
102

103
   if (type_kind(ftype) != T_SIGNATURE || !type_has_result(ftype))
343✔
104
      sem_error(res, "resolution function name %s is not a function",
1✔
105
                istr(tree_ident(res)));
106

107
   // Must take a single parameter of array of base type
108

109
   if (type_params(ftype) != 1)
342✔
110
      sem_error(res, "resolution function must have a single argument");
1✔
111

112
   type_t param = type_param(ftype, 0);
341✔
113
   if (type_kind(param) != T_ARRAY)
341✔
114
      sem_error(res, "parameter of resolution function must be "
1✔
115
                "an unconstrained array type");
116

117
   if (!type_eq(type_elem(param), type))
340✔
118
      sem_error(res, "parameter of resolution function must be "
1✔
119
                "an array of %s but found %s", type_pp(type),
120
                type_pp(type_elem(param)));
121

122
   // Return type must be the resolved type
123

124
   if (!type_eq(type_result(ftype), type))
339✔
125
      sem_error(res, "result of resolution function must be %s but have %s",
1✔
126
                type_pp(type), type_pp(type_result(ftype)));
127

128
   return true;
129
}
130

131
static bool sem_check_range(tree_t r, type_t expect, nametab_t *tab)
21,494✔
132
{
133
   if (expect != NULL && type_is_none(expect))
21,494✔
134
      return false;   // Prevent cascading errors
135

136
   switch (tree_subkind(r)) {
21,473✔
137
   case RANGE_EXPR:
4,739✔
138
      {
139
         tree_t expr = tree_value(r);
4,739✔
140

141
         type_t type = tree_type(expr);
4,739✔
142
         if (type_is_none(type))
4,739✔
143
            return false;   // Was earlier error
144

145
         if (tree_kind(expr) != T_ATTR_REF)
4,737✔
146
            sem_error(expr, "invalid expression in range constraint");
×
147

148
         const attr_kind_t kind = tree_subkind(expr);
4,737✔
149
         if (kind != ATTR_RANGE && kind != ATTR_REVERSE_RANGE)
4,737✔
150
            sem_error(expr, "cannot use attribute %s as range", istr(tree_ident(expr)));
2✔
151

152
         if (!sem_check_attr_ref(expr, true, tab))
4,735✔
153
            return false;
154

155
         if (expect && !sem_check_type(expr, expect, tab))
4,730✔
156
            sem_error(expr, "expected type of range bound to be %s but is %s",
2✔
157
                      type_pp(expect), type_pp(type));
158
      }
159
      break;
160

161
   case RANGE_TO:
16,733✔
162
   case RANGE_DOWNTO:
163
      {
164
         tree_t left = tree_left(r);
16,733✔
165
         if (!sem_check(left, tab))
16,733✔
166
            return false;
167

168
         tree_t right = tree_right(r);
16,732✔
169
         if (!sem_check(right, tab))
16,732✔
170
            return false;
171

172
         if (expect != NULL) {
16,729✔
173
            if (!sem_check_same_type(left, right))
16,524✔
174
               sem_error(right, "type mismatch in range: left is %s,"
3✔
175
                         " right is %s", type_pp(tree_type(left)),
176
                         type_pp(tree_type(right)));
177

178
            if (!sem_check_type(left, expect, tab))
16,521✔
179
               sem_error(r, "expected type of range bounds to be %s but"
4✔
180
                         " have %s", type_pp(expect), type_pp(tree_type(left)));
181

182
            // This cannot fail because we know left and right have the
183
            // same type and left is equal to expect, but we still need
184
            // to call sem_check_type for the implicit conversion
185
            sem_check_type(right, expect, tab);
16,517✔
186
            sem_check_type(r, expect, tab);
16,517✔
187
         }
188
      }
189
      break;
190
   }
191

192
   return true;
193
}
194

195
static bool sem_check_discrete_range(tree_t r, type_t expect, nametab_t *tab)
18,089✔
196
{
197
   if (!sem_check_range(r, expect ?: tree_type(r), tab))
18,089✔
198
      return false;
199

200
   const range_kind_t kind = tree_subkind(r);
18,052✔
201
   type_t type = tree_type(r);
18,052✔
202
   if (type_is_none(type) || kind == RANGE_ERROR)
18,052✔
203
      return false;
204

205
   if (!type_is_discrete(type))
18,051✔
206
      sem_error(r, "type of range bounds %s is not discrete", type_pp(type));
2✔
207

208
   // See LRM 93 section 3.2.1.1: universal integer bound must be a
209
   // numeric literal or attribute. Later LRMs relax the wording here.
210
   if (standard() < STD_00 && !relaxed_rules() && kind != RANGE_EXPR) {
18,049✔
211
      tree_t left  = tree_left(r);
7,303✔
212
      tree_t right = tree_right(r);
7,303✔
213

214
      type_t left_type  = tree_type(left);
7,303✔
215
      type_t right_type = tree_type(right);
7,303✔
216

217
      if (type_is_universal(left_type) && type_is_universal(right_type)) {
7,303✔
218
         tree_kind_t lkind = tree_kind(left);
1✔
219
         tree_kind_t rkind = tree_kind(right);
1✔
220

221
         const bool invalid =
2✔
222
            lkind != T_LITERAL && lkind != T_ATTR_REF
1✔
223
            && rkind != T_LITERAL && rkind != T_ATTR_REF;
1✔
224

225
         if (invalid)
1✔
226
            sem_error(r, "universal integer bound must be numeric"
×
227
                      " literal or attribute");
228
      }
229
   }
230

231
   return true;
232
}
233

234
static bool sem_check_constraint(tree_t constraint, type_t base, nametab_t *tab)
15,404✔
235
{
236
   const constraint_kind_t consk = tree_subkind(constraint);
15,404✔
237
   switch (consk) {
15,404✔
238
   case C_RANGE:
3,195✔
239
      if (!type_is_scalar(base))
3,195✔
240
         sem_error(constraint, "range constraint cannot be used with "
2✔
241
                   "non-scalar type %s", type_pp(base));
242
      break;
243

244
   case C_INDEX:
11,870✔
245
      if (!type_is_array(base))
11,870✔
246
         sem_error(constraint, "index constraint cannot be used with "
5✔
247
                   "non-array type %s", type_pp(base));
248
      break;
249

250
   case C_RECORD:
339✔
251
      {
252
         if (!type_is_record(base))
339✔
253
            sem_error(constraint, "record element constraint cannot be used "
×
254
                      "with non-record type %s", type_pp(base));
255

256
         // Range list is overloaded to hold record element constraints
257
         const int nelem = tree_ranges(constraint);
339✔
258
         for (int i = 0; i < nelem; i++) {
906✔
259
            tree_t ei = tree_range(constraint, i);
572✔
260
            assert(tree_kind(ei) == T_ELEM_CONSTRAINT);
572✔
261

262
            if (!tree_has_ref(ei))
572✔
263
               return false;   // Was parse error
264

265
            tree_t decl = tree_ref(ei);
569✔
266
            assert(tree_kind(decl) == T_FIELD_DECL);  // Checked by parser
569✔
267

268
            type_t ftype = tree_type(decl);
569✔
269
            if (!type_is_unconstrained(ftype))
569✔
270
               sem_error(constraint, "field %s in record element constraint is "
1✔
271
                         "already constrained", istr(tree_ident(decl)));
272

273
            type_t sub = tree_type(ei);
568✔
274
            if (!sem_check_subtype(decl, sub, tab))
568✔
275
               return false;
276

277
            // Check for duplicate element constraints
278
            tree_t fi = tree_ref(ei);
568✔
279
            for (int j = 0; j < i; j++) {
975✔
280
               tree_t ej = tree_range(constraint, j);
408✔
281
               if (tree_pos(tree_ref(ej)) == tree_pos(fi))
408✔
282
                  sem_error(ei, "duplicate record element constraint for "
408✔
283
                            "field %s", istr(tree_ident(fi)));
284
            }
285
         }
286

287
         // Code belows handles index and range constraints
288
         return true;
289
      }
290
   }
291

292
   if (type_is_array(base)) {
15,058✔
293
      if (type_kind(base) == T_SUBTYPE && !type_is_unconstrained(base))
11,865✔
294
         sem_error(constraint, "cannot change constraints of constrained "
2✔
295
                   "array type %s", type_pp(base));
296
   }
297
   else if (type_is_record(base) && standard() < STD_08)
3,193✔
298
      sem_error(constraint, "record subtype may not have constraints "
×
299
                "in VHDL-%s", standard_text(standard()));
300

301
   const int ndims_base = type_is_array(base) ? dimension_of(base) : 1;
15,056✔
302
   const int ndims = tree_ranges(constraint);
15,056✔
303

304
   if (ndims != ndims_base)
15,056✔
305
      sem_error(constraint, "expected %d constraints for type %s but found %d",
2✔
306
                ndims_base, type_pp(base), ndims);
307

308
   for (int i = 0; i < ndims; i++) {
30,444✔
309
      tree_t r = tree_range(constraint, i);
15,402✔
310
      type_t index = index_type_of(base, i);
15,402✔
311

312
      switch (consk) {
15,402✔
313
      case C_INDEX:
12,209✔
314
         if (!sem_check_discrete_range(r, index, tab))
12,209✔
315
            return false;
316
         break;
317

318
      case C_RANGE:
3,193✔
319
         if (!sem_check_range(r, index, tab))
3,193✔
320
            return false;
321
         break;
322

323
      default:
324
         break;
325
      }
326
   }
327

328
   return true;
329
}
330

331
static bool sem_check_subtype_helper(tree_t decl, type_t type, nametab_t *tab)
15,932✔
332
{
333
   // Shared code for checking subtype declarations and implicit subtypes
334

335
   type_t base = type_base(type);
15,932✔
336
   if (type_is_none(base))
15,932✔
337
      return false;
338
   else if (type_is_access(base))
15,922✔
339
      base = type_designated(base);
2✔
340

341
   assert(!is_anonymous_subtype(base));
15,922✔
342

343
   if (type_is_protected(base))
15,922✔
344
      sem_error(decl, "subtypes may not have protected base types");
1✔
345
   else if (!sem_check_incomplete(decl, type))
15,921✔
346
      return false;
347

348
   if (type_has_constraint(type)) {
15,920✔
349
      tree_t cons = type_constraint(type);
15,404✔
350
      if (!sem_check_constraint(cons, base, tab))
15,404✔
351
         return false;
352

353
      // Check the subtype does not already have an index constraint in
354
      // this position
355
      for (type_t b = base; type_kind(b) == T_SUBTYPE; b = type_base(b)) {
16,754✔
356
         if (type_has_constraint(b)) {
1,379✔
357
            tree_t econs = type_constraint(b);
83✔
358
            if (tree_subkind(econs) == C_INDEX) {
83✔
359
               diag_t *d = diag_new(DIAG_ERROR, tree_loc(cons));
1✔
360
               diag_printf(d, "duplicate index constraint for type %s",
1✔
361
                           type_pp(base));
362
               diag_hint(d, tree_loc(econs), "already constrained here");
1✔
363
               diag_emit(d);
1✔
364
               return false;
1✔
365
            }
366
         }
367
      }
368
   }
369

370
   if (type_is_array(type) && type_has_elem(type)) {
15,891✔
371
      type_t elem = type_elem(type);
322✔
372
      if (is_anonymous_subtype(elem)) {
322✔
373
         // Anonymous subtype created for array element constraint
374
         assert(standard() >= STD_08);
259✔
375

376
         if (!sem_check_subtype_helper(decl, elem, tab))
259✔
377
            return false;
378
      }
379
   }
380

381
   if (type_has_resolution(type)) {
15,891✔
382
      if (!sem_check_resolution(type_base(type), type_resolution(type)))
349✔
383
         return false;
11✔
384
   }
385

386
   return true;
387
}
388

389
static bool sem_check_subtype(tree_t decl, type_t type, nametab_t *tab)
72,261✔
390
{
391
   // Check an anonymous subtype at the point of use
392

393
   if (type_kind(type) != T_SUBTYPE)
72,261✔
394
      return true;
395
   else if (type_has_ident(type))
27,063✔
396
      return true;   // Explicitly declared subtype
397

398
   return sem_check_subtype_helper(decl, type, tab);
13,501✔
399
}
400

401
static bool sem_check_use_clause(tree_t c, nametab_t *tab)
15,064✔
402
{
403
   if (standard() >= STD_08) {
15,064✔
404
      tree_t unit = find_enclosing(tab, S_DESIGN_UNIT);
5,636✔
405

406
      if (unit != NULL && tree_kind(unit) == T_CONTEXT) {
5,636✔
407
         // LRM 08 section 13.3
408
         ident_t prefix = ident_until(tree_ident(c), '.');
35✔
409
         if (prefix == well_known(W_WORK))
35✔
410
            sem_error(c, "selected name in context declaration use clause "
1✔
411
                      "may not have WORK as a prefix");
412
      }
413
   }
414

415
   return true;
416
}
417

418
static bool sem_check_library_clause(tree_t t, nametab_t *tab)
25,459✔
419
{
420
   if (standard() >= STD_08) {
25,459✔
421
      ident_t name = tree_ident(t);
9,175✔
422
      tree_t unit = find_enclosing(tab, S_DESIGN_UNIT);
9,175✔
423

424
      if (unit != NULL && tree_kind(unit) == T_CONTEXT) {
9,175✔
425
         // LRM 08 section 13.3
426
         if (name == well_known(W_WORK))
13✔
427
            sem_error(t, "library clause in a context declaration may not have "
1✔
428
                      "logical library name WORK");
429
      }
430
   }
431

432
   return true;
433
}
434

435
static bool sem_check_context_clause(tree_t t, nametab_t *tab)
12,242✔
436
{
437
   // Ignore the implicit WORK and STD with context declarations
438
   const int ignore = tree_kind(t) == T_CONTEXT ? 2 : 0;
12,242✔
439

440
   bool ok = true;
12,242✔
441
   const int ncontexts = tree_contexts(t);
12,242✔
442
   for (int n = ignore; n < ncontexts; n++)
52,786✔
443
      ok = sem_check(tree_context(t, n), tab) && ok;
40,547✔
444

445
   return ok;
12,242✔
446
}
447

448
static bool sem_check_readable(tree_t t)
203,443✔
449
{
450
   switch (tree_kind(t)) {
212,807✔
451
   case T_REF:
81,846✔
452
      {
453
         if (tree_flags(t) & TREE_F_FORMAL_NAME)
81,846✔
454
            return true;   // Name appearing in formal
455

456
         tree_t decl = tree_ref(t);
81,846✔
457
         switch (tree_kind(decl)) {
81,846✔
458
         case T_PORT_DECL:
1,925✔
459
            {
460
               const port_mode_t mode = tree_subkind(decl);
1,925✔
461
               if (mode == PORT_OUT && standard() < STD_08) {
1,925✔
462
                  diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
9✔
463
                  diag_printf(d, "cannot read output port %s",
9✔
464
                              istr(tree_ident(t)));
465
                  diag_hint(d, tree_loc(decl), "%s declared with mode OUT",
9✔
466
                            istr(tree_ident(decl)));
467
                  diag_hint(d, tree_loc(t), "read here");
9✔
468
                  diag_hint(d, NULL, "outputs can be read with "
9✔
469
                            "$bold$--std=2008$$");
470
                  diag_emit(d);
9✔
471
                  return false;
9✔
472
               }
473
               else if (mode == PORT_LINKAGE)
1,916✔
474
                  sem_error(t, "linkage port %s may not be read except as "
1✔
475
                            "an actual corresponding to an interface of mode "
476
                            "linkage", istr(tree_ident(t)));
477
            }
478
            break;
479

480
         case T_PARAM_DECL:
22,083✔
481
            if (tree_subkind(decl) == PORT_OUT && standard() < STD_08)
22,083✔
482
               sem_error(t, "cannot read OUT parameter %s",
3✔
483
                         istr(tree_ident(t)));
484
            break;
485

486
         default:
487
            break;
488
         }
489

490
         return true;
491
      }
492

493
   case T_ARRAY_REF:
9,364✔
494
   case T_ARRAY_SLICE:
495
      return sem_check_readable(tree_value(t));
9,364✔
496

497
   default:
498
      return true;
499
   }
500
}
501

502
static bool sem_check_array_dims(type_t type, type_t constraint, nametab_t *tab)
284✔
503
{
504
   const int ndims = dimension_of(type);
284✔
505
   for (int i = 0; i < ndims; i++) {
583✔
506
      tree_t r = range_of(type, i);
299✔
507

508
      type_t index_type = NULL;
299✔
509
      if (constraint != NULL && i < dimension_of(constraint))
299✔
510
         index_type = index_type_of(constraint, i);
×
511

512
      if (!sem_check_discrete_range(r, index_type, tab))
299✔
513
         return false;
514

515
      if (index_type == NULL)
299✔
516
         index_type = tree_type(r);
299✔
517

518
      if (!sem_check_type(r, index_type, tab))
299✔
519
         sem_error(r, "type of bound %s does not match type of index %s",
299✔
520
                   type_pp(tree_type(r)),
521
                   type_pp(index_type));
522
   }
523

524
   return true;
525
}
526

527
static bool sem_check_type(tree_t t, type_t expect, nametab_t *tab)
377,236✔
528
{
529
   type_t actual = tree_type(t);
377,236✔
530

531
   if (type_eq_map(actual, expect, get_generic_map(tab)))
377,236✔
532
      return true;
533

534
   // Supress cascading errors
535
   if (type_is_none(actual) || type_is_none(expect))
323✔
536
      return true;
8✔
537

538
   return false;
539
}
540

541
static bool sem_check_same_type(tree_t left, tree_t right)
32,896✔
542
{
543
   type_t left_type  = tree_type(left);
32,896✔
544
   type_t right_type = tree_type(right);
32,896✔
545

546
   if (type_eq(left_type, right_type))
32,896✔
547
      return true;
548

549
   // Supress cascading errors
550
   if (type_is_none(left_type) || type_is_none(right_type))
15✔
551
      return true;
1✔
552

553
   return false;
554
}
555

556
static bool sem_has_access(type_t t)
66,831✔
557
{
558
   // returns true if the type is an access type or is a composite
559
   // type that contains a subelement of an access type
560
   type_t base = type_base_recur(t);
96,659✔
561

562
   if (type_is_access(base))
96,659✔
563
      return true;
564

565
   if (type_is_array(base))
95,852✔
566
      return sem_has_access(type_elem(base));
29,828✔
567

568
   if (type_is_record(base)) {
66,024✔
569
      const int nfields = type_fields(base);
4,484✔
570
      for (int i = 0; i < nfields; i++) {
21,205✔
571
         if (sem_has_access(tree_type(type_field(base, i))))
16,794✔
572
            return true;
573
      }
574
   }
575

576
   return false;
577
}
578

579
static bool sem_check_type_decl(tree_t t, nametab_t *tab)
4,850✔
580
{
581
   type_t type = tree_type(t);
4,850✔
582

583
   // Nothing more to do for incomplete types
584
   if (type_kind(type) == T_INCOMPLETE)
4,850✔
585
      return true;
586

587
   type_kind_t kind = type_kind(type);
4,802✔
588

589
   if (kind == T_SUBTYPE) {
4,802✔
590
      // Implicitly created subtype for a constrained array defintion
591
      if (!sem_check_subtype_helper(t, type, tab)) {
722✔
592
         // Prevent cascading errors
593
         // TODO: can we do this check in the parser and set T_NONE earlier?
594
         type_set_base(type, type_new(T_NONE));
4✔
595
         return false;
4✔
596
      }
597

598
      type = type_base(type);
718✔
599
      kind = type_kind(type);
718✔
600
      assert(kind == T_ARRAY);
718✔
601
   }
602

603
   switch (kind) {
4,798✔
604
   case T_ARRAY:
2,479✔
605
      {
606
         type_t elem_type = type_elem(type);
2,479✔
607
         if (!sem_check_subtype(t, elem_type, tab))
2,479✔
608
            return false;
609

610
         if (standard() < STD_08 && type_is_unconstrained(elem_type)) {
2,479✔
611
            diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
1✔
612
            diag_printf(d, "array %s cannot have unconstrained element type",
1✔
613
                        istr(tree_ident(t)));
614
            diag_hint(d, NULL, "this would be allowed with $bold$--std=2008$$");
1✔
615
            diag_emit(d);
1✔
616
            return false;
1✔
617
         }
618

619
         if (type_is_file(elem_type))
2,478✔
620
            sem_error(t, "array %s cannot have element of file type",
1✔
621
                      istr(tree_ident(t)));
622
         else if (type_is_protected(elem_type))
2,477✔
623
            sem_error(t, "array %s cannot have element of protected type",
1✔
624
                      istr(tree_ident(t)));
625
         else if (!sem_check_incomplete(t, elem_type))
2,476✔
626
            return false;
627

628
         const int nindex = type_indexes(type);
2,475✔
629
         for (int i = 0; i < nindex; i++) {
5,257✔
630
            type_t index_type = type_index(type, i);
2,787✔
631
            if (type_is_none(index_type))
2,787✔
632
               return false;
633
            else if (!type_is_discrete(index_type))
2,784✔
634
               sem_error(t, "index type %s is not discrete",
2,784✔
635
                         type_pp(index_type));
636
         }
637

638
         return true;
639
      }
640

641
   case T_ENUM:
642
      return true;
643

644
   case T_PHYSICAL:
45✔
645
      {
646
         const int nunits = type_units(type);
45✔
647
         for (int i = 0; i < nunits; i++) {
200✔
648
            tree_t u = type_unit(type, i);
157✔
649
            tree_set_type(u, type);
157✔
650
            if (!sem_check(u, tab))
157✔
651
               return false;
652

653
            tree_t value = tree_value(u);
157✔
654

655
            // LRM 08 section 5.2.4.1: the abstract literal portion
656
            // shall be an integer literal
657
            if (tree_ival(value) == 0 && tree_dval(value) != 0)
157✔
658
               sem_error(value, "the abstract literal portion of a secondary "
1✔
659
                         "unit declaration must be an integer literal");
660

661
            if (i > 0 && !sem_check_type(value, type, tab))
156✔
662
               sem_error(value, "secondary unit %s must have type %s",
156✔
663
                         istr(tree_ident(u)), type_pp(type));
664
         }
665
      }
666

667
      // Fall-through
668
   case T_INTEGER:
669
      {
670
         tree_t r = type_dim(type, 0);
189✔
671

672
         if (!sem_check_range(r, NULL, tab))
189✔
673
            return false;
674

675
         switch (tree_subkind(r)) {
188✔
676
         case RANGE_TO:
185✔
677
         case RANGE_DOWNTO:
678
            {
679
               // See LRM 93 section 3.1.2: Each bound of a range
680
               // constraint that must be of some integer type, but the
681
               // two bounds need not have the same integer type.
682
               tree_t left = tree_left(r), right = tree_right(r);
185✔
683
               if (!type_is_integer(tree_type(left)))
185✔
684
                  sem_error(left, "type of left bound must be of some integer "
×
685
                            "type but have %s", type_pp(tree_type(left)));
686
               else if (!type_is_integer(tree_type(right)))
185✔
687
                  sem_error(right, "type of right bound must be of some "
2✔
688
                            "integer type but have %s",
689
                            type_pp(tree_type(right)));
690
            }
691
            break;
692

693
         case RANGE_EXPR:
3✔
694
            if (!type_is_integer(tree_type(r)))
3✔
695
               sem_error(r, "type of range bounds must be of some integer "
1✔
696
                         "type but have %s", type_pp(tree_type(r)));
697
            break;
698
         }
699

700
         if (!sem_locally_static(r))
185✔
701
            sem_error(r, "range constraint of type %s must be locally static",
1✔
702
                      type_pp(type));
703

704
         tree_set_type(r, type);
184✔
705
         return true;
184✔
706
      }
707

708
   case T_REAL:
23✔
709
      {
710
         tree_t r = type_dim(type, 0);
23✔
711

712
         if (!sem_check_range(r, NULL, tab))
23✔
713
            return false;
714

715
         switch (tree_subkind(r)) {
21✔
716
         case RANGE_TO:
20✔
717
         case RANGE_DOWNTO:
718
            {
719
               // See LRM 93 section 3.1.4: Each bound of a range
720
               // constraint that must be of some floating-point type, but
721
               // the two bounds need not have the same floating-point type.
722
               tree_t left = tree_left(r), right = tree_right(r);
20✔
723
               if (!type_is_real(tree_type(left)))
20✔
724
                  sem_error(left, "type of left bound must be of some "
×
725
                            "floating-point type but have %s",
726
                            type_pp(tree_type(left)));
727
               else if (!type_is_real(tree_type(right)))
20✔
728
                  sem_error(right, "type of right bound must be of some "
1✔
729
                            "floating-point type but have %s",
730
                            type_pp(tree_type(right)));
731
            }
732
            break;
733

734
         case RANGE_EXPR:
1✔
735
            assert(type_is_real(tree_type(r)));   // Unreachable
1✔
736
            break;
737
         }
738

739
         if (!sem_locally_static(r))
20✔
740
            sem_error(r, "range constraint of type %s must be locally static",
1✔
741
                      type_pp(type));
742

743
         tree_set_type(r, type);
19✔
744
         return true;
19✔
745
      }
746

747
   case T_RECORD:
1,223✔
748
      {
749
         const int nfields = type_fields(type);
1,223✔
750
         for (int i = 0; i < nfields; i++) {
4,908✔
751
            tree_t f = type_field(type, i);
3,691✔
752

753
            if (!sem_check(f, tab))
3,691✔
754
               return false;
755

756
            // Each field name must be distinct
757
            ident_t f_name = tree_ident(f);
3,690✔
758
            for (int j = 0; j < i; j++) {
12,227✔
759
               tree_t fj = type_field(type, j);
8,538✔
760
               if (ident_casecmp(f_name, tree_ident(fj))) {
8,538✔
761
                  diag_t *d = diag_new(DIAG_ERROR, tree_loc(f));
1✔
762
                  diag_printf(d, "duplicate field name %s", istr(f_name));
1✔
763
                  diag_hint(d, tree_loc(fj), "previously declared here");
1✔
764
                  diag_hint(d, tree_loc(f), "declared again here");
1✔
765
                  diag_emit(d);
1✔
766
                  return false;
1✔
767
               }
768
            }
769

770
            type_t f_type = tree_type(f);
3,689✔
771

772
            if (!sem_check_subtype(f, f_type, tab))
3,689✔
773
               return false;
774

775
            // Recursive record types are not allowed
776
            if (type_eq(type, f_type))
3,688✔
777
               sem_error(f, "recursive record types are not allowed");
×
778

779
            // Element types may not be unconstrained before VHDL-2008
780
            if (standard() < STD_08 && type_is_unconstrained(f_type)) {
3,688✔
781
               diag_t *d = diag_new(DIAG_ERROR, tree_loc(f));
1✔
782
               diag_printf(d, "record field %s cannot have unconstrained "
1✔
783
                           "array type in VHDL-%s", istr(f_name),
784
                           standard_text(standard()));
785
               diag_hint(d, NULL, "pass $bold$--std=2008$$ to enable this "
1✔
786
                         "feature");
787
               diag_emit(d);
1✔
788
               return false;
1✔
789
            }
790
            else if (type_is_file(f_type))
3,687✔
791
               sem_error(f, "record field %s cannot be of file type",
1✔
792
                         istr(f_name));
793
            else if (type_is_protected(f_type))
3,686✔
794
               sem_error(f, "record field %s cannot be of protected type",
3,686✔
795
                         istr(f_name));
796
         }
797

798
         return true;
799
      }
800

801
   case T_FILE:
101✔
802
      // Rules for file types are in LRM 93 section 3.4
803
      {
804
         type_t f = type_designated(type);
101✔
805

806
         switch (type_kind(f)) {
101✔
807
         case T_ACCESS:
1✔
808
            sem_error(t, "files may not be of access type");
1✔
809
            break;
810
         case T_FILE:
1✔
811
            sem_error(t, "files may not be of file type");
1✔
812
            break;
813
         case T_PROTECTED:
1✔
814
            sem_error(t, "files may not be of protected type");
1✔
815
            break;
816
         default:
817
            break;
98✔
818
         }
819

820
         if (sem_has_access(f))
98✔
821
            sem_error(t, "type %s has a subelement with an access type",
4✔
822
                      type_pp(f));
823

824
         type_t base_f = type_base_recur(f);
94✔
825
         if (type_is_array(base_f) && dimension_of(base_f) > 1)
94✔
826
            sem_error(t, "array type for file type must be one-dimensional");
2✔
827

828
         return true;
829
      }
830

831
   case T_ACCESS:
322✔
832
      // Rules for access types are in LRM 93 section 3.3
833
      {
834
         type_t designated = type_designated(type);
322✔
835

836
         if (!sem_check_subtype(t, designated, tab))
322✔
837
            return false;
838

839
         if (standard() < STD_19) {
321✔
840
            if (type_is_file(designated))
272✔
841
               sem_error(t, "access type %s cannot designate file type",
1✔
842
                         istr(tree_ident(t)));
843

844
            if (type_is_protected(designated))
271✔
845
               sem_error(t, "access type %s cannot designate protected type",
1✔
846
                         istr(tree_ident(t)));
847
         }
848

849
         return true;
850
      }
851

852
   case T_PROTECTED:
853
   default:
854
      return true;
855
   }
856
}
857

858
static bool sem_check_subtype_decl(tree_t t, nametab_t *tab)
1,450✔
859
{
860
   type_t type = tree_type(t);
1,450✔
861
   assert(type_kind(type) == T_SUBTYPE);
1,450✔
862
   assert(type_has_ident(type));
1,450✔
863

864
   return sem_check_subtype_helper(t, type, tab);
1,450✔
865
}
866

867
static bool sem_check_incomplete(tree_t t, type_t type)
116,865✔
868
{
869
   if (type_is_incomplete(type)) {
116,865✔
870
      diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
13✔
871
      diag_printf(d, "invalid use of incomplete type %s", type_pp(type));
13✔
872
      diag_hint(d, NULL, "prior to the end of the corresponding full type "
13✔
873
                "declaration, an incomplete type may only be used as the "
874
                "designated type of an access type declaration");
875
      diag_lrm(d, STD_08, "5.4.2");
13✔
876
      diag_emit(d);
13✔
877

878
      return false;
13✔
879
   }
880

881
   return true;
882
}
883

884
static bool sem_no_access_file_or_protected(tree_t t, type_t type, const char *what)
13,054✔
885
{
886
   // constants, signals, attributes, generics, ports
887
   // may not be of an access, file, or protected type, or
888
   // of a composite type with a subelement of an access type
889

890
   if (type_is_access(type))
13,054✔
891
      sem_error(t, "%s may not have access type", what);
4✔
892

893
   if (sem_has_access(type))
13,050✔
894
      sem_error(t, "%s may not have a type with a subelement of access type", what);
6✔
895

896
   if (type_is_protected(type))
13,044✔
897
      sem_error(t, "%s may not have protected type", what);
3✔
898

899
   if (type_is_file(type))
13,041✔
900
      sem_error(t, "%s may not have file type", what);
4✔
901

902
   return true;
903
}
904

905
static void sem_unconstrained_decl_hint(diag_t *d, type_t type)
15✔
906
{
907
   if (!type_is_record(type))
15✔
908
      return;
909

910
   // Tell the user which field is unconstrained
911

912
   type_t base = type_base_recur(type);
6✔
913
   const int nfields = type_fields(base);
6✔
914
   for (int i = 0; i < nfields; i++) {
18✔
915
      tree_t f = type_field(base, i);
12✔
916
      if (!type_is_unconstrained(tree_type(f)))
12✔
917
         continue;
3✔
918
      else if (type_constraint_for_field(type, f) == NULL)
9✔
919
         diag_hint(d, NULL, "missing record element constraint for field %s",
6✔
920
                   istr(tree_ident(f)));
921
   }
922
}
923

924
static void sem_propagate_constraints(tree_t decl, tree_t value)
7,878✔
925
{
926
   // Propagate the constraints from an initial value to an object
927
   // declaration with unconstrained type but only if the object subtype
928
   // has no constraints of its own
929

930
   if (standard() < STD_19 && tree_kind(decl) != T_CONST_DECL)
7,878✔
931
      return;
932

933
   type_t type = tree_type(decl);
6,190✔
934
   if (!type_is_unconstrained(type))
6,190✔
935
      return;
936

937
   for (type_t iter = type; type_kind(iter) == T_SUBTYPE;
951✔
938
        iter = type_base(iter)) {
26✔
939
      if (type_has_constraint(iter))
40✔
940
         return;
941
   }
942

943
   tree_set_type(decl, tree_type(value));
911✔
944
}
945

946
static bool sem_check_const_decl(tree_t t, nametab_t *tab)
6,723✔
947
{
948
   type_t type = tree_type(t);
6,723✔
949

950
   if (!sem_check_subtype(t, type, tab))
6,723✔
951
      return false;
952
   else if (type_is_none(type))
6,720✔
953
      return false;
954
   else if (!sem_check_incomplete(t, type))
6,705✔
955
      return false;
956

957
   if (!sem_no_access_file_or_protected(t, type, "constants"))
6,705✔
958
      return false;
959

960
   tree_t fwd = find_forward_decl(tab, t);
6,700✔
961

962
   if (tree_has_value(t)) {
6,700✔
963
      tree_t value = tree_value(t);
6,290✔
964
      if (!sem_check(value, tab))
6,290✔
965
         return false;
966

967
      if (!sem_check_type(value, type, tab))
6,260✔
968
         sem_error(value, "type of initial value %s does not match type "
5✔
969
                   "of declaration %s", type_pp2(tree_type(value), type),
970
                   type_pp2(type, tree_type(value)));
971

972
      if (fwd == NULL)
6,255✔
973
         sem_propagate_constraints(t, value);
5,853✔
974

975
      // A constant with a globaly static value should be treated as
976
      // globally static regardless of where it is declared
977
      if (sem_globally_static(value)) {
6,255✔
978
         tree_set_flag(t, TREE_F_GLOBALLY_STATIC);
4,539✔
979

980
         // A reference to a constant with a locally static subype and
981
         // locally static value is locally static
982
         if (sem_locally_static(value)
4,539✔
983
             && sem_static_subtype(tree_type(t), sem_locally_static))
3,496✔
984
            tree_set_flag(t, TREE_F_LOCALLY_STATIC);
3,364✔
985
      }
986
   }
987
   else if (tree_kind(find_enclosing(tab, S_DESIGN_UNIT)) != T_PACKAGE)
410✔
988
      sem_error(t, "deferred constant declarations are only permitted "
1✔
989
                "in packages");
990

991
   if (fwd != NULL && !type_strict_eq(tree_type(fwd), type)) {
6,664✔
992
      diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
1✔
993
      diag_printf(d, "expected type %s for deferred constant %s but "
1✔
994
                  "found %s", type_pp2(tree_type(fwd), type),
995
                  istr(tree_ident(t)), type_pp2(type, tree_type(fwd)));
996
      diag_hint(d, tree_loc(fwd), "originally declared with type %s",
1✔
997
                type_pp2(tree_type(fwd), type));
998
      diag_hint(d, tree_loc(t), "type here is %s",
1✔
999
                type_pp2(type, tree_type(fwd)));
1000
      diag_emit(d);
1✔
1001
      return false;
1✔
1002
   }
1003

1004
   return true;
1005
}
1006

1007
static bool sem_check_signal_decl(tree_t t, nametab_t *tab)
6,250✔
1008
{
1009
   type_t type = tree_type(t);
6,250✔
1010

1011
   if (!sem_check_subtype(t, type, tab))
6,250✔
1012
      return false;
1013
   else if (type_is_none(type))
6,240✔
1014
      return false;
1015

1016
   if (type_is_unconstrained(type)) {
6,238✔
1017
      if (standard() < STD_19) {
21✔
1018
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
6✔
1019
         diag_printf(d, "declaration of signal %s cannot have unconstrained "
6✔
1020
                     "type %s", istr(tree_ident(t)), type_pp(type));
1021
         sem_unconstrained_decl_hint(d, type);
6✔
1022
         diag_emit(d);
6✔
1023
         return false;
6✔
1024
      }
1025
      else if (!tree_has_value(t)) {
15✔
1026
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
1✔
1027
         diag_printf(d, "declaration of signal %s without an initial value "
1✔
1028
                     "cannot have unconstrained type %s",
1029
                     istr(tree_ident(t)), type_pp(type));
1030
         sem_unconstrained_decl_hint(d, type);
1✔
1031
         diag_emit(d);
1✔
1032
         return false;
1✔
1033
      }
1034
   }
1035
   else if (!sem_check_incomplete(t, type))
6,217✔
1036
      return false;
1037

1038
   if (!sem_no_access_file_or_protected(t, type, "signals"))
6,231✔
1039
      return false;
1040

1041
   if (is_guarded_signal(t) && !type_is_resolved(type))
6,224✔
1042
      sem_error(t, "guarded signal must have resolved subtype");
1✔
1043

1044
   if (tree_has_value(t)) {
6,223✔
1045
      tree_t value = tree_value(t);
1,839✔
1046
      if (!sem_check(value, tab))
1,839✔
1047
         return false;
1048

1049
      if (!sem_check_type(value, type, tab))
1,836✔
1050
         sem_error(value, "type of initial value %s does not match type "
×
1051
                   "of declaration %s", type_pp2(tree_type(value), type),
1052
                   type_pp2(type, tree_type(value)));
1053

1054
      if (standard() >= STD_19 && type_is_unconstrained(type))
1,836✔
1055
         tree_set_type(t, tree_type(value));
14✔
1056
   }
1057

1058
   return true;
1059
}
1060

1061
static bool sem_check_var_decl(tree_t t, nametab_t *tab)
11,300✔
1062
{
1063
   type_t type = tree_type(t);
11,300✔
1064

1065
   if (!sem_check_subtype(t, type, tab))
11,300✔
1066
      return false;
1067
   else if (type_is_none(type))
11,297✔
1068
      return false;
1069

1070
   if (type_is_unconstrained(type)) {
11,284✔
1071
      if (standard() < STD_19) {
28✔
1072
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
6✔
1073
         diag_printf(d, "declaration of variable %s cannot have unconstrained "
6✔
1074
                     "type %s", istr(tree_ident(t)), type_pp(type));
1075
         sem_unconstrained_decl_hint(d, type);
6✔
1076
         diag_emit(d);
6✔
1077
         return false;
6✔
1078
      }
1079
      else if (!tree_has_value(t)) {
22✔
1080
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
2✔
1081
         diag_printf(d, "declaration of variable %s without an initial value "
2✔
1082
                     "cannot have unconstrained type %s", istr(tree_ident(t)),
1083
                     type_pp(type));
1084
         sem_unconstrained_decl_hint(d, type);
2✔
1085
         diag_emit(d);
2✔
1086
         return false;
2✔
1087
      }
1088
   }
1089
   else if (!sem_check_incomplete(t, type))
11,256✔
1090
      return false;
1091

1092
   if (tree_has_value(t)) {
11,275✔
1093
      if (type_kind(type) == T_PROTECTED)
2,042✔
1094
         sem_error(t, "variable %s with protected type may not have an "
1✔
1095
                   "initial value", istr(tree_ident(t)));
1096

1097
      tree_t value = tree_value(t);
2,041✔
1098
      if (!sem_check(value, tab))
2,041✔
1099
         return false;
1100

1101
      if (!sem_check_type(value, type, tab))
2,026✔
1102
         sem_error(value, "type of initial value %s does not match type "
1✔
1103
                   "of declaration %s", type_pp2(tree_type(value), type),
1104
                   type_pp2(type, tree_type(value)));
1105

1106
      sem_propagate_constraints(t, value);
2,025✔
1107
   }
1108

1109
   // From VHDL-2000 onwards shared variables must be protected types
1110
   if (standard() >= STD_00) {
11,258✔
1111
      if ((tree_flags(t) & TREE_F_SHARED) && !type_is_protected(type)) {
6,285✔
1112
         diag_t *d = pedantic_diag(tree_loc(t));
7✔
1113
         if (d != NULL) {
7✔
1114
            diag_printf(d, "shared variable %s must have protected type",
7✔
1115
                        istr(tree_ident(t)));
1116
            diag_emit(d);
7✔
1117
         }
1118
      }
1119
   }
1120

1121
   if (type_is_protected(type)) {
11,258✔
1122
      ident_t typeid = ident_rfrom(type_ident(type), '.');
176✔
1123
      tree_t pt = get_local_decl(tab, NULL, typeid, 0);
176✔
1124
      if (pt != NULL && tree_kind(pt) == T_PROT_DECL) {
176✔
1125
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
1✔
1126
         diag_printf(d, "cannot declare instance of protected type %s "
1✔
1127
                     "before its body has been elaborated", type_pp(type));
1128
         diag_hint(d, tree_loc(pt), "%s declared here",
1✔
1129
                   type_pp(type));
1130
         diag_lrm(d, STD_08, "14.4.2");
1✔
1131
         diag_emit(d);
1✔
1132
         return false;
1✔
1133
      }
1134

1135
      // The 2019 standard needs access to the instance and path name at
1136
      // the point of declaration
1137
      if (standard() >= STD_19)
175✔
1138
         tree_set_global_flags(t, TREE_GF_INSTANCE_NAME | TREE_GF_PATH_NAME);
33✔
1139
   }
1140

1141
   return true;
1142
}
1143

1144
static bool sem_check_param_decl(tree_t t, nametab_t *tab)
30,953✔
1145
{
1146
   type_t type = tree_type(t);
30,953✔
1147

1148
   if (!sem_check_subtype(t, type, tab))
30,953✔
1149
      return false;
1150
   else if (!sem_check_incomplete(t, type))
30,953✔
1151
      return false;
1152

1153
   // See LRM 93 section 3.3 for restrictions
1154

1155
   const type_kind_t kind = type_base_kind(type);
30,952✔
1156
   const class_t class = tree_class(t);
30,952✔
1157
   const port_mode_t mode = tree_subkind(t);
30,952✔
1158

1159
   switch (mode) {
30,952✔
1160
   case PORT_BUFFER:
1✔
1161
      sem_error(t, "subprogram formal parameters cannot have mode BUFFER");
1✔
1162
      break;
1163
   case PORT_LINKAGE:
1✔
1164
      sem_error(t, "subprogram formal parameters cannot have mode LINKAGE");
1✔
1165
      break;
1166
   default:
1167
      break;
30,950✔
1168
   }
1169

1170
   if (kind == T_FILE && class != C_FILE)
30,950✔
1171
      sem_error(t, "formal parameter %s with file type must have class FILE",
1✔
1172
                istr(tree_ident(t)));
1173

1174
   if (kind != T_FILE && class == C_FILE)
30,949✔
1175
      sem_error(t, "formal parameter %s with class FILE must have file type",
1✔
1176
                istr(tree_ident(t)));
1177

1178
   if ((kind == T_ACCESS || kind == T_PROTECTED) && class != C_VARIABLE)
30,948✔
1179
      sem_error(t, "formal parameter %s with %s type must have class VARIABLE",
6✔
1180
                istr(tree_ident(t)),
1181
                kind == T_ACCESS ? "access" : "protected");
1182

1183
   if (sem_has_access(type) && class != C_VARIABLE)
30,943✔
1184
      sem_error(t, "formal parameter %s with type containing an access type "
4✔
1185
                "must have class VARIABLE", istr(tree_ident(t)));
1186

1187
   if (class == C_CONSTANT && mode != PORT_IN)
30,939✔
1188
      sem_error(t, "parameter of class CONSTANT must have mode IN");
2✔
1189

1190
   // LRM 08 section 4.2.2.3
1191
   if (class == C_SIGNAL && tree_flags(t) & TREE_F_BUS)
30,937✔
1192
      sem_error(t, "formal signal parameter declaration may "
1✔
1193
                "not include the reserved word BUS");
1194

1195
   if (class == C_PACKAGE || class == C_TYPE)
30,936✔
1196
      sem_error(t, "parameter interface list cannot contain %s "
2✔
1197
                "interface declaration", class_str(class));
1198

1199
   if (mode == PORT_RECORD_VIEW || mode == PORT_ARRAY_VIEW) {
30,934✔
1200
      tree_t name = tree_value(t);
20✔
1201
      type_t view_type = tree_type(name);
20✔
1202

1203
      if (type_is_none(view_type))
20✔
1204
         return false;
1205

1206
      if (type_kind(view_type) != T_VIEW)
20✔
1207
         sem_error(name, "name in mode view indication of parameter %s does "
1✔
1208
                   "not denote a mode view", istr(tree_ident(t)));
1209

1210
      type_t elem_type = type;
19✔
1211
      if (mode == PORT_ARRAY_VIEW) {
19✔
1212
         if (!type_is_array(type))
1✔
1213
            sem_error(t, "parameter %s with array mode view indication has "
1✔
1214
                      "non-array type %s", istr(tree_ident(t)), type_pp(type));
1215

1216
         elem_type = type_elem(type);
×
1217
      }
1218

1219
      if (!type_eq(elem_type, type_designated(view_type)))
18✔
1220
         sem_error(t, "subtype %s is not compatible with mode "
1✔
1221
                   "view %s", type_pp(elem_type), type_pp(view_type));
1222
   }
1223
   else if (tree_has_value(t)) {
30,914✔
1224
      tree_t value = tree_value(t);
4,154✔
1225
      if (!sem_check(value, tab))
4,154✔
1226
         return false;
1227

1228
      if (!sem_check_type(value, type, tab))
4,152✔
1229
         sem_error(value, "type of default value %s does not match type "
2✔
1230
                   "of declaration %s", type_pp(tree_type(value)),
1231
                   type_pp(type));
1232

1233
      switch (class) {
4,150✔
1234
      case C_SIGNAL:
1✔
1235
         sem_error(t, "parameter of class SIGNAL cannot have a "
1✔
1236
                   "default value");
1237
         break;
1238

1239
      case C_VARIABLE:
6✔
1240
         if (mode == PORT_OUT || mode == PORT_INOUT)
6✔
1241
            sem_error(t, "parameter of class VARIABLE with mode OUT or "
2✔
1242
                      "INOUT cannot have a default value");
1243
         break;
1244

1245
      default:
1246
         break;
1247
      }
1248

1249
      if (!sem_globally_static(value)) {
4,147✔
1250
         diag_t *d = pedantic_diag(tree_loc(value));
8✔
1251
         if (d != NULL) {
8✔
1252
            diag_printf(d, "default value must be a static expression");
5✔
1253
            diag_emit(d);
5✔
1254
         }
1255
      }
1256

1257
      if (kind == T_PROTECTED)
4,147✔
1258
         sem_error(t, "parameter with protected type cannot have "
1✔
1259
                   "a default value");
1260
   }
1261

1262
   return true;
1263
}
1264

1265
static bool sem_check_port_decl(tree_t t, nametab_t *tab)
3,699✔
1266
{
1267
   type_t type = tree_type(t);
3,699✔
1268

1269
   if (type_is_none(type))
3,699✔
1270
      return false;
1271
   else if (!sem_check_subtype(t, type, tab))
3,697✔
1272
      return false;
1273
   else if (!sem_check_incomplete(t, type))
3,696✔
1274
      return false;
1275

1276
   const class_t class = tree_class(t);
3,695✔
1277
   const port_mode_t mode = tree_subkind(t);
3,695✔
1278

1279
   if (class == C_VARIABLE) {
3,695✔
1280
      if (standard() < STD_19) {
7✔
1281
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
1✔
1282
         diag_printf(d, "ports may not have variable class in VHDL-%s",
1✔
1283
                     standard_text(standard()));
1284
         diag_hint(d, NULL, "pass $bold$--std=2019$$ to enable this "
1✔
1285
                   "feature");
1286
         diag_emit(d);
1✔
1287
         return false;
1✔
1288
      }
1289

1290
      if (mode != PORT_INOUT)
6✔
1291
         sem_error(t, "formal variable port %s must have mode INOUT",
1✔
1292
                   istr(tree_ident(t)));
1293

1294
      if (!type_is_protected(type))
5✔
1295
         sem_error(t, "formal variable port %s must have protected type",
1✔
1296
                   istr(tree_ident(t)));
1297
   }
1298
   else if (class != C_SIGNAL)
3,688✔
1299
      sem_error(t, "invalid object class %s for port %s",
2✔
1300
                class_str(class), istr(tree_ident(t)));
1301

1302
   if (type_is_access(type))
3,690✔
1303
      sem_error(t, "port %s cannot be declared with access type %s",
1✔
1304
                istr(tree_ident(t)), type_pp(type));
1305

1306
   if (sem_has_access(type))
3,689✔
1307
      sem_error(t, "port %s cannot be declared with type %s which has a "
2✔
1308
                "subelement of access type", istr(tree_ident(t)),
1309
                type_pp(type));
1310

1311
   if (class != C_VARIABLE && type_is_protected(type)) {
3,687✔
1312
      diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
1✔
1313
      diag_printf(d, "port %s with class %s cannot be declared with "
1✔
1314
                  "protected type %s", istr(tree_ident(t)),
1315
                  class_str(class), type_pp(type));
1316
      diag_hint(d, NULL, "ports with variable class can be of protected "
1✔
1317
                "type in VHDL-2019");
1318
      diag_hint(d, NULL, "pass $bold$--std=2019$$ to enable this "
1✔
1319
                "feature");
1320
      diag_emit(d);
1✔
1321
      return false;
1✔
1322
   }
1323

1324
   if (type_is_file(type))
3,686✔
1325
      sem_error(t, "port %s cannot be declared with file type %s",
1✔
1326
                istr(tree_ident(t)), type_pp(type));
1327

1328
   if (mode == PORT_RECORD_VIEW || mode == PORT_ARRAY_VIEW) {
3,685✔
1329
      tree_t name = tree_value(t);
71✔
1330
      type_t view_type = tree_type(name);
71✔
1331

1332
      if (type_is_none(view_type))
71✔
1333
         return false;
1334

1335
      if (type_kind(view_type) != T_VIEW)
71✔
1336
         sem_error(name, "name in mode view indication of port %s does not "
×
1337
                   "denote a mode view", istr(tree_ident(t)));
1338

1339
      type_t elem_type = type;
71✔
1340
      if (mode == PORT_ARRAY_VIEW) {
71✔
1341
         if (!type_is_array(type))
16✔
1342
            sem_error(t, "port %s with array mode view indication has "
1✔
1343
                      "non-array type %s", istr(tree_ident(t)), type_pp(type));
1344

1345
         elem_type = type_elem(type);
15✔
1346
      }
1347

1348
      if (!type_eq(elem_type, type_designated(view_type)))
70✔
1349
         sem_error(t, "subtype %s is not compatible with mode "
2✔
1350
                   "view %s", type_pp(elem_type), type_pp(view_type));
1351
   }
1352
   else if (tree_has_value(t)) {
3,614✔
1353
      tree_t value = tree_value(t);
337✔
1354
      if (!sem_check(value, tab))
337✔
1355
         return false;
1356

1357
      if (mode == PORT_LINKAGE)
337✔
1358
         sem_error(t, "port with mode LINKAGE cannot have a default value");
1✔
1359

1360
      if (!sem_check_type(value, type, tab))
336✔
1361
         sem_error(value, "type of default value %s does not match type "
×
1362
                   "of declaration %s", type_pp(tree_type(value)),
1363
                   type_pp(type));
1364
   }
1365

1366
   return true;
1367
}
1368

1369
static bool sem_check_generic_decl(tree_t t, nametab_t *tab)
2,283✔
1370
{
1371
   const class_t class = tree_class(t);
2,283✔
1372
   switch (class) {
2,283✔
1373
   case C_CONSTANT:
1374
   case C_TYPE:
1375
   case C_FUNCTION:
1376
   case C_PROCEDURE:
1377
      break;
2,206✔
1378

1379
   case C_PACKAGE:
72✔
1380
      {
1381
         tree_t map = tree_value(t);
72✔
1382
         if (!tree_has_ref(map))
72✔
1383
            return false;   // Was earlier error
1384

1385
         assert(tree_kind(map) == T_PACKAGE_MAP);
70✔
1386

1387
         tree_t pack = tree_ref(map);
70✔
1388
         assert(is_uninstantiated_package(pack));
70✔
1389

1390
         switch (tree_subkind(map)) {
70✔
1391
         case PACKAGE_MAP_DEFAULT:
6✔
1392
            {
1393
               // Check each generic in the uninstantiated package has a
1394
               // default value
1395
               const int ngenerics = tree_generics(pack);
6✔
1396
               for (int i = 0; i < ngenerics; i++) {
18✔
1397
                  tree_t g = tree_generic(pack, i);
13✔
1398
                  if (!tree_has_value(g)) {
13✔
1399
                     diag_t *d = diag_new(DIAG_ERROR, tree_loc(map));
1✔
1400
                     diag_printf(d, "generic %s in package %s does not have a "
1✔
1401
                                 "default value", istr(tree_ident(g)),
1402
                                 istr(tree_ident(pack)));
1403
                     diag_hint(d, tree_loc(g), "%s declared here",
1✔
1404
                               istr(tree_ident(g)));
1405
                     diag_lrm(d, STD_08, "6.5.5");
1✔
1406

1407
                     diag_emit(d);
1✔
1408
                     return false;
1✔
1409
                  }
1410
               }
1411
            }
1412
            break;
1413

1414
         case PACKAGE_MAP_MATCHING:
9✔
1415
            sem_check_generic_map(map, pack, tab);
9✔
1416
            break;
9✔
1417

1418
         case PACKAGE_MAP_BOX:
1419
            break;
1420
         }
1421

1422
         return true;
1423
      }
1424
   default:
5✔
1425
      sem_error(t, "invalid object class %s for generic %s",
5✔
1426
                class_str(tree_class(t)), istr(tree_ident(t)));
1427
   }
1428

1429
   type_t type = tree_type(t);
2,206✔
1430

1431
   if (!sem_check_subtype(t, type, tab))
2,206✔
1432
      return false;
1433
   else if (type_is_none(type))
2,206✔
1434
      return false;
1435
   else if (!sem_check_incomplete(t, type))
2,205✔
1436
      return false;
1437

1438
   if (class != C_TYPE) {
2,204✔
1439
      if (type_is_access(type))
1,968✔
1440
         sem_error(t, "generic %s may not have access type",
1✔
1441
                   istr(tree_ident(t)));
1442
      else if (sem_has_access(type))
1,967✔
1443
         sem_error(t, "generic %s may not have a type with a subelement of "
2✔
1444
                   "access type", istr(tree_ident(t)));
1445
      else if (type_is_protected(type))
1,965✔
1446
         sem_error(t, "generic %s may not have protected type",
1✔
1447
                   istr(tree_ident(t)));
1448
      else if (type_is_file(type))
1,964✔
1449
         sem_error(t, "generic %s may not have file type", istr(tree_ident(t)));
1✔
1450
   }
1451

1452
   if (class == C_CONSTANT && tree_subkind(t) != PORT_IN)
2,199✔
1453
      sem_error(t, "generic %s with class CONSTANT must have mode IN",
1✔
1454
                istr(tree_ident(t)));
1455

1456
   if (tree_has_value(t)) {
2,198✔
1457
      tree_t value = tree_value(t);
1,162✔
1458
      if (!sem_check(value, tab))
1,162✔
1459
         return false;
1460

1461
      if (!sem_check_type(value, type, tab))
1,158✔
1462
         sem_error(value, "type of default value %s does not match type "
×
1463
                   "of declaration %s", type_pp(tree_type(value)),
1464
                   type_pp(type));
1465
   }
1466

1467
   if (tree_flags(t) & TREE_F_LOCALLY_STATIC) {
2,194✔
1468
      // For a generic declaration in a pacakage or subprogram to be
1469
      // locally static it must also have a locally static subtype
1470
      if (!sem_static_subtype(type, sem_locally_static))
299✔
1471
         tree_clear_flag(t, TREE_F_LOCALLY_STATIC);
10✔
1472
   }
1473

1474
   return true;
1475
}
1476

1477
static bool sem_check_field_decl(tree_t t)
3,691✔
1478
{
1479
   type_t type = tree_type(t);
3,691✔
1480

1481
   if (!sem_check_incomplete(t, type))
3,691✔
1482
      return false;
1✔
1483

1484
   return true;
1485
}
1486

1487
static bool sem_check_unit_decl(tree_t t)
157✔
1488
{
1489
   return true;
157✔
1490
}
1491

1492
static bool sem_check_alias(tree_t t, nametab_t *tab)
1,560✔
1493
{
1494
   // Rules for aliases are given in LRM 93 section 4.3.3
1495

1496
   tree_t value = tree_value(t);
1,560✔
1497
   type_t type = get_type_or_null(t);
1,560✔
1498

1499
   const tree_kind_t value_kind = tree_kind(value);
1,560✔
1500

1501
   if (type != NULL && type_is_subprogram(type)) {
1,560✔
1502
      // Alias of subprogram or enumeration literal
1503
      // Rules for matching signatures are in LRM 93 section 2.3.2
1504
      assert(tree_kind(value) == T_REF || tree_kind(value) == T_PROT_REF);
527✔
1505
      return true;
527✔
1506
   }
1507
   else if (tree_flags(t) & TREE_F_NONOBJECT_ALIAS) {
1,033✔
1508
      // TODO: cannot be label
1509
      return true;
1510
   }
1511
   else if (value_kind == T_REF && tree_has_ref(value)) {
1,032✔
1512
      tree_t decl = tree_ref(value);
869✔
1513
      if (aliased_type_decl(decl) != NULL)
869✔
1514
         return true;   // Alias of type
1515
      else if (tree_kind(decl) == T_VIEW_DECL)
803✔
1516
         return true;   // Alias of view declaration
1517
   }
1518
   else if (value_kind == T_ATTR_REF && is_type_attribute(tree_subkind(value)))
163✔
1519
      return true;   // Alias of type
1520

1521
   // Alias of object
1522
   if (!sem_check(value, tab))
960✔
1523
      return false;
1524

1525
   if (value_kind == T_ATTR_REF && tree_subkind(value) == ATTR_CONVERSE) {
952✔
1526
      // Special case handling for
1527
      //   https://gitlab.com/IEEE-P1076/VHDL-Issues/-/issues/293
1528
      return true;
1529
   }
1530
   else if (!sem_static_name(value, sem_globally_static)) {
929✔
1531
      diag_t *d = diag_new(DIAG_ERROR, tree_loc(value));
6✔
1532
      diag_printf(d, "aliased name is not static");
6✔
1533
      diag_lrm(d, STD_93, "6.1");
6✔
1534
      diag_emit(d);
6✔
1535
      return false;
6✔
1536
   }
1537

1538
   if (type != NULL) {
923✔
1539
      // Alias declaration had optional subtype indication
1540

1541
      if (!sem_check_subtype(t, type, tab))
836✔
1542
         return false;
1543

1544
      if (value_kind == T_EXTERNAL_NAME) {
836✔
1545
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
1✔
1546
         diag_printf(d, "an alias of an external name cannot have a "
1✔
1547
                     "subtype indication");
1548
         diag_lrm(d, STD_08, "6.6.2");
1✔
1549
         diag_emit(d);
1✔
1550
         return false;
1✔
1551
      }
1552

1553
      if (!sem_check_type(value, type, tab))
835✔
1554
         sem_error(t, "type of aliased object %s does not match expected "
1✔
1555
                   "type %s", type_pp2(tree_type(value), type),
1556
                   type_pp2(type, tree_type(value)));
1557

1558
      if (opt_get_int(OPT_RELAXED) && type_is_unconstrained(type)) {
834✔
1559
         // If the type of the aliased object is unconstrained then
1560
         // use its subtype instead of the subtype declared by the
1561
         // alias.  This is required for some UVVM sources.
1562
         type_t obj_type = tree_type(value);
1✔
1563
         if (!type_is_unconstrained(obj_type))
1✔
1564
            tree_set_type(t, obj_type);
1✔
1565
      }
1566
   }
1567
   else
1568
      type = tree_type(value);
87✔
1569

1570
   if (standard() < STD_08 && type_is_array(type) && dimension_of(type) > 1) {
921✔
1571
      diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
1✔
1572
      diag_printf(d, "object alias may not have multidimensional array type "
1✔
1573
                  "in VHDL-%s", standard_text(standard()));
1574
      diag_lrm(d, STD_93, "4.3.3.1");
1✔
1575
      diag_emit(d);
1✔
1576
      return false;
1✔
1577
   }
1578

1579
   return true;
1580
}
1581

1582
static void sem_missing_body_cb(tree_t t, tree_t parent, nametab_t *tab)
13,973✔
1583
{
1584
   if (parent == NULL || !opt_get_int(OPT_MISSING_BODY))
13,973✔
1585
      return;
715✔
1586
   else if (have_name_errors(tab))
13,258✔
1587
      return;   // May be missing due to parse errors
1588

1589
   const tree_kind_t kind = tree_kind(parent);
13,252✔
1590
   if (kind == T_PACKAGE || kind == T_PROT_DECL)
13,252✔
1591
      return;   // Should be in body
1592

1593
   type_t type = tree_type(t);
6,835✔
1594
   tree_t body = NULL;
6,835✔
1595
   int nth = 0;
6,835✔
1596
   do {
25,880✔
1597
      body = get_local_decl(tab, NULL, tree_ident(t), nth++);
25,880✔
1598
   } while (body != NULL && !type_eq(tree_type(body), type));
25,880✔
1599

1600
   if (body == NULL || !is_body(body)) {
6,835✔
1601
      diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
13✔
1602
      diag_printf(d, "missing body for ");
13✔
1603
      switch (tree_kind(t)) {
13✔
1604
      case T_PROT_DECL: diag_printf(d, "protected type "); break;
1✔
1605
      case T_PROC_DECL: diag_printf(d, "procedure "); break;
4✔
1606
      case T_FUNC_DECL: diag_printf(d, "function "); break;
8✔
1607
      default: break;
1608
      }
1609

1610
      diag_printf(d, "%s", type_pp(type));
13✔
1611
      diag_suppress(d, type_has_error(type));
13✔
1612

1613
      diag_hint(d, tree_loc(parent), "body not found in %s",
13✔
1614
                istr(tree_ident(parent)));
1615

1616
      diag_hint(d, tree_loc(t), "%s declared here", type_pp(type));
13✔
1617

1618
      diag_emit(d);
13✔
1619
   }
1620
}
1621

1622
static bool sem_check_func_ports(tree_t t, nametab_t *tab)
13,357✔
1623
{
1624
   const vhdl_standard_t std = standard();
13,357✔
1625

1626
   ident_t id = tree_ident(t);
13,357✔
1627
   if (is_operator_symbol(id)) {
13,357✔
1628
      const int nports = tree_ports(t);
5,231✔
1629
      const well_known_t wk = is_well_known(id);
5,231✔
1630
      if (wk >= W_OP_AND && wk <= W_OP_XNOR) {
5,231✔
1631
         if (std >= STD_08 && (nports < 1 || nports > 2))
949✔
1632
            sem_error(t, "logical operator must have either one or two "
×
1633
                      "operands");
1634
         else if (std < STD_08 && nports != 2)
949✔
1635
            sem_error(t, "logical operator must have two operands");
1✔
1636
      }
1637
      else if (wk >= W_OP_ABS && wk <= W_OP_CCONV && nports != 1)
4,282✔
1638
         sem_error(t, "unary operator must have one operand");
1✔
1639
      else if ((wk >= W_OP_EQUAL && wk <= W_OP_EXPONENT && nports != 2)
4,281✔
1640
               || (wk >= W_OP_MATCH_EQUAL && wk <= W_OP_MATCH_GREATER_EQUAL
4,281✔
1641
                   && nports != 2)
1642
               || (wk >= W_OP_ADD && wk <= W_OP_MINUS
4,280✔
1643
                   && (nports < 1 || nports > 2)))
926✔
1644
         sem_error(t, "binary operator must have two operands");
1✔
1645
   }
1646

1647
   const bool pure = !(tree_flags(t) & TREE_F_IMPURE);
13,354✔
1648

1649
   if (!pure && std >= STD_19)
13,354✔
1650
      return true;   // LCS2016_002 relaxed rules for impure functions
1651

1652
   const int nports = tree_ports(t);
12,956✔
1653
   for (int i = 0; i < nports; i++) {
34,204✔
1654
      tree_t p = tree_port(t, i);
21,248✔
1655
      if (tree_subkind(p) != PORT_IN) {
21,248✔
1656
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(p));
4✔
1657
         diag_printf(d, "%sfunction parameters must have mode IN",
5✔
1658
                     std < STD_19 ? "" : "pure ");
1659
         diag_hint(d, tree_loc(p), "parameter %s has mode %s",
4✔
1660
                   istr(tree_ident(p)), port_mode_str(tree_subkind(p)));
4✔
1661
         diag_lrm(d, STD_08, "4.2.2.1");
4✔
1662
         diag_emit(d);
4✔
1663
      }
1664
      else if (tree_class(p) == C_VARIABLE) {
21,244✔
1665
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(p));
3✔
1666
         diag_printf(d, "class of %sfunction parameters must be CONSTANT, "
4✔
1667
                     "SIGNAL, or FILE", std < STD_19 ? "" : "pure ");
1668
         diag_hint(d, tree_loc(p), "parameter %s has class %s",
3✔
1669
                   istr(tree_ident(p)), class_str(tree_class(p)));
1670
         diag_lrm(d, STD_08, "4.2.2.1");
3✔
1671
         diag_emit(d);
3✔
1672
      }
1673
   }
1674

1675
   return true;
1676
}
1677

1678
static bool sem_check_protected_method(tree_t t, nametab_t *tab)
457✔
1679
{
1680
   if (standard() >= STD_19)
457✔
1681
      return true;   // Relaxed in LCS2016_04
1682

1683
   const int nports = tree_ports(t);
277✔
1684
   for (int i = 0; i < nports; i++) {
456✔
1685
      tree_t p = tree_port(t, i);
179✔
1686
      type_t type = tree_type(p);
179✔
1687

1688
      if (sem_has_access(type)) {
179✔
1689
         diag_t *d = pedantic_diag(tree_loc(p));
2✔
1690
         if (d != NULL) {
2✔
1691
            diag_printf(d, "parameters of protected type methods cannot be of "
2✔
1692
                        "an access type or a composite type containing an "
1693
                        "access type");
1694
            if (type_is_access(type))
2✔
1695
               diag_hint(d, tree_loc(p), "type of %s is %s which is an "
1✔
1696
                         "access type", istr(tree_ident(p)), type_pp(type));
1697
            else
1698
               diag_hint(d, tree_loc(p), "type of %s is %s which has an "
1✔
1699
                         "access type as a subelement",
1700
                         istr(tree_ident(p)), type_pp(type));
1701
            diag_lrm(d, STD_08, "5.6.2");
2✔
1702
            diag_emit(d);
2✔
1703
         }
1704
      }
1705
      else if (type_is_file(type)) {
177✔
1706
         diag_t *d = pedantic_diag(tree_loc(p));
1✔
1707
         if (d != NULL) {
1✔
1708
            diag_printf(d, "parameters of protected type methods cannot be of "
1✔
1709
                        "a file type");
1710
            diag_hint(d, tree_loc(p), "type of %s is %s which is a file type",
1✔
1711
                      istr(tree_ident(p)), type_pp(type));
1712
            diag_lrm(d, STD_08, "5.6.2");
1✔
1713
            diag_emit(d);
1✔
1714
         }
1715
      }
1716
   }
1717

1718
   if (tree_kind(t) == T_FUNC_DECL) {
277✔
1719
      type_t result = type_result(tree_type(t));
111✔
1720
      if (sem_has_access(result) || type_is_file(result)) {
111✔
1721
         diag_t *d = pedantic_diag(tree_loc(t));
1✔
1722
         if (d != NULL) {
1✔
1723
            diag_printf(d, "return type of a protected type method cannot be "
1✔
1724
                        "of a file type, access type, or a composite type with "
1725
                        "a subelement that is an access type");
1726
            diag_lrm(d, STD_08, "5.6.2");
1✔
1727
            diag_emit(d);
1✔
1728
         }
1729
      }
1730
   }
1731

1732
   return true;
1733
}
1734

1735
static bool sem_check_func_result(tree_t t)
13,354✔
1736
{
1737
   type_t result = type_result(tree_type(t));
13,354✔
1738

1739
   if (type_is_protected(result))
13,354✔
1740
      sem_error(t, "function result subtype may not denote a protected type");
1✔
1741
   else if (type_is_file(result))
13,353✔
1742
      sem_error(t, "function result subtype may not denote a file type");
1✔
1743
   else if (!sem_check_incomplete(t, result))
13,352✔
1744
      return false;
1✔
1745

1746
   return true;
1747
}
1748

1749
static bool sem_check_func_decl(tree_t t, nametab_t *tab)
6,000✔
1750
{
1751
   const tree_flags_t flags = tree_flags(t);
6,000✔
1752
   if (flags & TREE_F_PREDEFINED)
6,000✔
1753
      return true;
1754

1755
   if (!sem_check_func_ports(t, tab))
6,000✔
1756
      return false;
1757

1758
   if (!sem_check_func_result(t))
5,997✔
1759
      return false;
1760

1761
   if ((flags & TREE_F_PROTECTED) && !sem_check_protected_method(t, tab))
5,995✔
1762
      return false;
1763

1764
   defer_check(tab, sem_missing_body_cb, t);
5,995✔
1765
   return true;
5,995✔
1766
}
1767

1768
static bool sem_compare_interfaces(tree_t decl, tree_t body, int nth,
12,778✔
1769
                                   tree_t (*getfn)(tree_t, unsigned),
1770
                                   const char *what)
1771
{
1772
   tree_t dport = (*getfn)(decl, nth);
12,778✔
1773
   tree_t bport = (*getfn)(body, nth);
12,778✔
1774

1775
   tree_flags_t dflags = tree_flags(dport);
12,778✔
1776
   tree_flags_t bflags = tree_flags(bport);
12,778✔
1777

1778
   ident_t dname = tree_ident(dport);
12,778✔
1779
   ident_t bname = tree_ident(bport);
12,778✔
1780

1781
   if (dname != bname) {
12,778✔
1782
      diag_t *d = diag_new(DIAG_ERROR, tree_loc(bport));
2✔
1783
      diag_printf(d, "%s name %s in subprogram %s body does not match "
2✔
1784
                  "name %s in declaration", what,
1785
                  istr(bname), istr(tree_ident(body)), istr(dname));
1786
      diag_hint(d, tree_loc(dport), "%s %s has name %s in specification",
2✔
1787
                ordinal_str(nth + 1), what, istr(dname));
1788
      diag_hint(d, tree_loc(bport), "%s %s has name %s in body",
2✔
1789
                ordinal_str(nth + 1), what, istr(bname));
1790
      diag_emit(d);
2✔
1791
      return false;
2✔
1792
   }
1793

1794
   const bool dcont = !!(tree_flags(dport) & TREE_F_CONTINUATION);
12,776✔
1795
   const bool bcont = !!(tree_flags(bport) & TREE_F_CONTINUATION);
12,776✔
1796

1797
   if (dcont != bcont) {
12,776✔
1798
      diag_t *d = pedantic_diag(tree_loc(bport));
1✔
1799
      if (d != NULL) {
1✔
1800
         diag_printf(d, "declaration of %s %s in subprogram body does not "
1✔
1801
                     "match specification", what, istr(bname));
1802

1803
         int bseq = 1;
1✔
1804
         for (tree_t it = bport; tree_flags(it) & TREE_F_CONTINUATION;
1✔
1805
              it = (*getfn)(body, nth - bseq++));
×
1806

1807
         diag_hint(d, tree_loc(bport), "%s appears %s in identifier list",
1✔
1808
                   istr(bname), ordinal_str(bseq));
1809

1810
         int dseq = 1;
1✔
1811
         for (tree_t it = dport; tree_flags(it) & TREE_F_CONTINUATION;
2✔
1812
              it = (*getfn)(decl, nth - dseq++));
1✔
1813

1814
         diag_hint(d, tree_loc(dport), "%s appears %s in identifier list",
1✔
1815
                   istr(dname), ordinal_str(dseq));
1816

1817
         diag_lrm(d, STD_08, "4.10");
1✔
1818
         diag_emit(d);
1✔
1819
         return false;
1✔
1820
      }
1821
   }
1822
   else if (dcont && bcont)
12,775✔
1823
      return true;   // Already checked first declaration in list
1824

1825
   type_t dtype = tree_type(dport);
11,655✔
1826
   type_t btype = tree_type(bport);
11,655✔
1827

1828
   // Do not use type_eq here as subtype must exactly match
1829
   if (!type_strict_eq(btype, dtype)) {
11,655✔
1830
     diag_t *d = diag_new(DIAG_ERROR, tree_loc(bport));
3✔
1831
     diag_printf(d, "subtype of %s %s does not match type %s in "
3✔
1832
                 "specification", what, istr(bname), type_pp(dtype));
1833
     diag_hint(d, tree_loc(dport), "%s %s declared with type %s",
3✔
1834
               what, istr(dname), type_pp(dtype));
1835
     diag_hint(d, tree_loc(bport), "%s %s declared with type %s ",
3✔
1836
               what, istr(bname), type_pp(btype));
1837
     diag_emit(d);
3✔
1838
     return false;
3✔
1839
   }
1840

1841
   const port_mode_t dmode = tree_subkind(dport);
11,652✔
1842
   const port_mode_t bmode = tree_subkind(bport);
11,652✔
1843

1844
   if (dmode != bmode) {
11,652✔
1845
     diag_t *d = diag_new(DIAG_ERROR, tree_loc(bport));
2✔
1846
     diag_printf(d, "%s %s of subprogram body %s with mode %s does not "
2✔
1847
                 "match mode %s in specification", what, istr(dname),
1848
                 istr(tree_ident(body)), port_mode_str(bmode),
1849
                 port_mode_str(dmode));
1850
     diag_hint(d, tree_loc(dport), "%s %s declared with mode %s",
2✔
1851
               what, istr(dname), port_mode_str(dmode));
1852
     diag_hint(d, tree_loc(bport), "%s %s declared with mode %s",
2✔
1853
               what, istr(bname), port_mode_str(bmode));
1854
     diag_emit(d);
2✔
1855
     return false;
2✔
1856
   }
1857

1858
   const bool bmode_explicit = !!(bflags & TREE_F_EXPLICIT_MODE);
11,650✔
1859
   const bool dmode_explicit = !!(dflags & TREE_F_EXPLICIT_MODE);
11,650✔
1860

1861
   if (bmode_explicit != dmode_explicit) {
11,650✔
1862
      diag_t *d = pedantic_diag(tree_loc(bport));
3✔
1863
      if (d != NULL) {
3✔
1864
         const char *dmode_str =
6✔
1865
            dmode_explicit ? port_mode_str(dmode) : "default";
3✔
1866
         const char *bmode_str =
6✔
1867
            bmode_explicit ? port_mode_str(bmode) : "default";
3✔
1868

1869
         diag_printf(d, "mode indication of subprogram %s %s %s was %s in "
3✔
1870
                     "specification but %s in body", istr(tree_ident(body)),
1871
                     what, istr(dname), dmode_str, bmode_str);
1872
         diag_hint(d, tree_loc(dport), "%s %s declared with %s mode in "
3✔
1873
                   "specification", what, istr(dname), dmode_str);
1874
         diag_hint(d, tree_loc(bport), "%s %s declared with %s mode in body",
3✔
1875
                   what, istr(bname), bmode_str);
1876
         diag_lrm(d, STD_08, "4.10");
3✔
1877
         diag_emit(d);
3✔
1878
         return false;
3✔
1879
      }
1880
   }
1881

1882
   const class_t dclass = tree_class(dport);
11,647✔
1883
   const class_t bclass = tree_class(bport);
11,647✔
1884

1885
   if (dclass != bclass) {
11,647✔
1886
     diag_t *d = diag_new(DIAG_ERROR, tree_loc(bport));
3✔
1887
     diag_printf(d, "class %s of subprogram body %s %s %s does not "
3✔
1888
                 "match class %s in specification", class_str(bclass),
1889
                 istr(tree_ident(body)), what, istr(dname), class_str(dclass));
1890
     diag_hint(d, tree_loc(dport), "%s %s declared with class %s in "
3✔
1891
               "subprogram specification", what, istr(dname),
1892
               class_str(dclass));
1893
     diag_hint(d, tree_loc(bport), "%s %s declared with class %s in "
3✔
1894
               "subprogram body", what, istr(bname), class_str(bclass));
1895
     diag_emit(d);
3✔
1896
     return false;
3✔
1897
   }
1898

1899
   const bool bclass_explicit = !!(bflags & TREE_F_EXPLICIT_CLASS);
11,644✔
1900
   const bool dclass_explicit = !!(dflags & TREE_F_EXPLICIT_CLASS);
11,644✔
1901

1902
   if (bclass_explicit != dclass_explicit) {
11,644✔
1903
      diag_t *d = pedantic_diag(tree_loc(bport));
3✔
1904
      if (d != NULL) {
3✔
1905
         const char *dclass_str =
4✔
1906
            dclass_explicit ? class_str(dclass) : "default";
2✔
1907
         const char *bclass_str =
4✔
1908
            bclass_explicit ? class_str(bclass) : "default";
2✔
1909

1910
         diag_printf(d, "class of subprogram %s %s %s was %s in specification "
2✔
1911
                     "but %s in body", istr(tree_ident(body)),
1912
                     what, istr(dname), dclass_str, bclass_str);
1913
         diag_hint(d, tree_loc(dport), "%s %s declared with %s class in "
2✔
1914
                   "specification", what, istr(dname), dclass_str);
1915
         diag_hint(d, tree_loc(bport), "%s %s declared with %s class in body",
2✔
1916
                   what, istr(bname), bclass_str);
1917
         diag_lrm(d, STD_08, "4.10");
2✔
1918
         diag_emit(d);
2✔
1919
         return false;
2✔
1920
      }
1921
   }
1922

1923
   tree_t bdef = tree_has_value(bport) ? tree_value(bport) : NULL;
11,642✔
1924
   tree_t ddef = tree_has_value(dport) ? tree_value(dport) : NULL;
11,642✔
1925

1926
   if (bdef == NULL && ddef == NULL)
11,642✔
1927
     return true;
1928

1929
   const tree_kind_t bkind = bdef ? tree_kind(bdef) : T_LAST_TREE_KIND;
1,897✔
1930
   const tree_kind_t dkind = ddef ? tree_kind(ddef) : T_LAST_TREE_KIND;
1,897✔
1931

1932
   // Work around some mismatches caused by folding
1933
   if (bdef != NULL && ddef != NULL && bkind != dkind)
1,897✔
1934
     return true;
1935

1936
   if (dkind == bkind) {
1,872✔
1937
     // This only covers a few simple cases
1938
     switch (dkind) {
1,871✔
1939
     case T_LITERAL: {
254✔
1940
       const literal_kind_t dsub = tree_subkind(ddef);
254✔
1941
       const literal_kind_t bsub = tree_subkind(bdef);
254✔
1942
       if (dsub == bsub) {
254✔
1943
         switch (dsub) {
254✔
1944
         case L_INT:
173✔
1945
           if (tree_ival(ddef) == tree_ival(bdef))
173✔
1946
             return true;
1947
           break;
1948
         case L_REAL:
23✔
1949
           if (tree_dval(ddef) == tree_dval(bdef))
23✔
1950
             return true;
1951
           break;
1952
         default:
1953
           return true;
1954
         }
1955
       }
1956
     } break;
1957

1958
     case T_REF:
1,410✔
1959
     case T_FCALL:
1960
       if (!tree_has_ref(bdef) || !tree_has_ref(ddef))
1,410✔
1961
         return true; // Was parse error, ignore it
×
1962

1963
       tree_t bref = tree_ref(bdef);
1,410✔
1964
       tree_t dref = tree_ref(ddef);
1,410✔
1965

1966
       if (bref == dref)
1,410✔
1967
         return true;
1968

1969
       // Work around mismatch introduced by folding
1970
       const tree_kind_t brefkind = tree_kind(bref);
153✔
1971
       if (brefkind == T_CONST_DECL || brefkind == T_GENERIC_DECL)
153✔
1972
         return true;
1973

1974
       break;
1975

1976
     default:
1977
       return true;
1978
     }
1979
   }
1980

1981
   diag_t *d = diag_new(DIAG_ERROR, tree_loc(bport));
3✔
1982
   diag_printf(d, "default value of %s %s in subprogram body %s does not "
3✔
1983
               "match declaration", what, istr(dname), istr(tree_ident(body)));
1984
   diag_hint(d, tree_loc(dport), "parameter was originally declared here");
3✔
1985
   diag_hint(d, tree_loc(bport), "body has different default value");
3✔
1986
   diag_emit(d);
3✔
1987

1988
   return false;
3✔
1989
}
1990

1991
static bool sem_check_conforming(tree_t decl, tree_t body)
6,665✔
1992
{
1993
   // Conformance rules are in LRM 08 section 4.10
1994
   // Note we don't implement strict lexical conformance here
1995

1996
   bool ok = true;
6,665✔
1997

1998
   const bool dimpure = !!(tree_flags(decl) & TREE_F_IMPURE);
6,665✔
1999
   const bool bimpure = !!(tree_flags(body) & TREE_F_IMPURE);
6,665✔
2000

2001
   if (dimpure != bimpure) {
6,665✔
2002
      diag_t *d = diag_new(DIAG_ERROR, tree_loc(body));
2✔
2003
      diag_printf(d, "function %s declaration was %s but body is %s",
4✔
2004
                  istr(tree_ident(body)), dimpure ? "impure" : "pure",
2005
                  bimpure ? "impure" : "pure");
2006
      diag_hint(d, tree_loc(decl), "declaration was %s",
2✔
2007
                dimpure ? "impure" : "pure");
2008
      diag_hint(d, tree_loc(body), "expecting keyword %s to match declaration",
3✔
2009
                bimpure ? "IMPURE" : "PURE");
2010
      diag_emit(d);
2✔
2011
      ok = false;
2✔
2012
   }
2013

2014
   // This must be true or they would be considered different overloads
2015
   assert(tree_ports(decl) == tree_ports(body));
6,665✔
2016

2017
   const int nports = tree_ports(decl);
6,665✔
2018
   for (int i = 0; i < nports; i++)
19,376✔
2019
      ok &= sem_compare_interfaces(decl, body, i, tree_port, "parameter");
12,711✔
2020

2021
   const int ngenerics = tree_generics(decl);
6,665✔
2022
   if (ngenerics != tree_generics(body)) {
6,665✔
2023
      diag_t *d = diag_new(DIAG_ERROR, tree_loc(body));
1✔
2024
      diag_printf(d, "subprogram %s declaration has %d generic%s but body "
2✔
2025
                  "has %d", istr(tree_ident(body)), ngenerics,
2026
                  ngenerics > 1 ? "s" : "", tree_generics(body));
2027
      diag_hint(d, tree_loc(decl), "declaration with %d generics", ngenerics);
1✔
2028
      diag_hint(d, tree_loc(body), "body has %d generics", tree_generics(body));
1✔
2029
      diag_emit(d);
1✔
2030
      ok = false;
1✔
2031
   }
2032
   else {
2033
      for (int i = 0; i < ngenerics; i++)
6,731✔
2034
         ok &= sem_compare_interfaces(decl, body, i, tree_generic, "generic");
67✔
2035
   }
2036

2037
   type_t dtype = tree_type(decl);
6,665✔
2038
   type_t btype = tree_type(body);
6,665✔
2039

2040
   if (type_has_result(dtype) && type_has_result(btype)) {
6,665✔
2041
      type_t dresult = type_result(dtype);
5,735✔
2042
      type_t bresult = type_result(btype);
5,735✔
2043

2044
      if (!type_strict_eq(dresult, bresult)) {
5,735✔
2045
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(body));
1✔
2046
         diag_printf(d, "return type of function body %s does not match type "
1✔
2047
                     "%s in specification", istr(tree_ident(body)),
2048
                     type_pp(dresult));
2049
         diag_hint(d, tree_loc(decl), "specification has return type %s",
1✔
2050
                   type_pp(dresult));
2051
         diag_hint(d, tree_loc(body), "body has return type %s ",
1✔
2052
                   type_pp(bresult));
2053
         diag_emit(d);
1✔
2054
         ok = false;
1✔
2055
      }
2056
   }
2057

2058
   return ok;
6,665✔
2059
}
2060

2061
static bool sem_check_func_body(tree_t t, nametab_t *tab)
7,357✔
2062
{
2063
   if (!sem_check_func_ports(t, tab))
7,357✔
2064
      return false;
2065

2066
   if (!sem_check_func_result(t))
7,357✔
2067
      return false;
2068

2069
   tree_t fwd = find_forward_decl(tab, t);
7,356✔
2070
   if (fwd != NULL && !sem_check_conforming(fwd, t))
7,356✔
2071
      return false;
16✔
2072

2073
   return true;
2074
}
2075

2076
static bool sem_check_proc_decl(tree_t t, nametab_t *tab)
1,085✔
2077
{
2078
   if (is_operator_symbol(tree_ident(t)))
1,085✔
2079
      sem_error(t, "procedure name must be an identifier");
×
2080

2081
   const tree_flags_t flags = tree_flags(t);
1,085✔
2082
   if (flags & TREE_F_PREDEFINED)
1,085✔
2083
      return true;
2084
   else if ((flags & TREE_F_PROTECTED) && !sem_check_protected_method(t, tab))
1,085✔
2085
      return false;
2086

2087
   defer_check(tab, sem_missing_body_cb, t);
1,085✔
2088
   return true;
1,085✔
2089
}
2090

2091
static bool sem_check_proc_body(tree_t t, nametab_t *tab)
2,147✔
2092
{
2093
   tree_t fwd = find_forward_decl(tab, t);
2,147✔
2094
   if (fwd != NULL && !sem_check_conforming(fwd, t))
2,147✔
2095
      return false;
2096

2097
   if (fwd == NULL && is_operator_symbol(tree_ident(t)))
2,141✔
2098
      sem_error(t, "procedure name must be an identifier");
1✔
2099

2100
   // Cleared by wait statement or pcall
2101
   tree_set_flag(t, TREE_F_NEVER_WAITS);
2,140✔
2102

2103
   return true;
2,140✔
2104
}
2105

2106
static bool sem_check_subprogram_inst(tree_t t, nametab_t *tab)
129✔
2107
{
2108
   if (tree_generics(t) == 0)
129✔
2109
      return false;   // Was a parse error
2110

2111
   if (!sem_check_generic_map(t, t, tab))
122✔
2112
      return false;
2✔
2113

2114
   // Other declarations were checked on the uninstantiated subprogram
2115

2116
   return true;
2117
}
2118

2119
static bool sem_check_sensitivity(tree_t t, nametab_t *tab)
14,267✔
2120
{
2121
   const int ntriggers = tree_triggers(t);
14,267✔
2122
   for (int i = 0; i < ntriggers; i++) {
15,437✔
2123
      tree_t r = tree_trigger(t, i);
1,175✔
2124
      if (tree_kind(r) == T_ALL)
1,175✔
2125
         continue;
49✔
2126
      else if (!sem_check(r, tab) || !sem_check_readable(r))
1,126✔
2127
         return false;
2✔
2128

2129
      if (!sem_static_name(r, sem_globally_static))
1,124✔
2130
         sem_error(r, "name in sensitivity list is not a static signal name");
2✔
2131

2132
      if (class_of(r) != C_SIGNAL) {
1,122✔
2133
         tree_t ref = name_to_ref(r);
1✔
2134
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(r));
1✔
2135
         if (ref != NULL) {
1✔
2136
            tree_t decl = tree_ref(ref);
1✔
2137
            diag_printf(d, "name %s in sensitivity list is not a signal",
1✔
2138
                        istr(tree_ident(decl)));
2139
            diag_hint(d, tree_loc(r), "%s is a %s", istr(tree_ident(decl)),
1✔
2140
                      class_str(class_of(decl)));
2141
         }
2142
         else
2143
            diag_printf(d, "name in sensitivity list is not a signal");
×
2144
         diag_emit(d);
1✔
2145
         return false;
1✔
2146
      }
2147
   }
2148

2149
   return true;
2150
}
2151

2152
static void sem_check_missing_wait(tree_t t, nametab_t *tab)
4,812✔
2153
{
2154
   if (!opt_get_int(OPT_MISSING_WAIT))
4,812✔
2155
      return;
2156

2157
   const int ntriggers = tree_triggers(t);
4,172✔
2158
   if (ntriggers > 0)
4,172✔
2159
      return;
2160

2161
   const tree_flags_t flags = tree_flags(t);
3,783✔
2162
   if (flags & TREE_F_HAS_WAIT)
3,783✔
2163
      return;
2164

2165
   diag_t *d = diag_new(DIAG_WARN, tree_loc(t));
5✔
2166
   diag_printf(d, "potential infinite loop in process");
5✔
2167
   if (!(flags & TREE_F_SYNTHETIC_NAME))
5✔
2168
      diag_printf(d, " %s", istr(tree_ident(t)));
3✔
2169
   diag_printf(d, " with no sensitivity list and no wait statements.");
5✔
2170
   diag_emit(d);
5✔
2171
}
2172

2173
static bool sem_check_process(tree_t t, nametab_t *tab)
4,814✔
2174
{
2175
   if (!sem_check_sensitivity(t, tab))
4,814✔
2176
      return false;
2177

2178
   sem_check_missing_wait(t, tab);
4,812✔
2179

2180
   return true;
4,812✔
2181
}
2182

2183
static bool sem_check_package(tree_t t, nametab_t *tab)
1,460✔
2184
{
2185
   if (!sem_check_context_clause(t, tab))
1,460✔
2186
      return false;
2187

2188
   if (tree_genmaps(t) > 0 && !sem_check_generic_map(t, t, tab))
1,460✔
2189
      return false;
2190

2191
   // Subprogram bodies are not allowed in package specification
2192
   const int ndecls = tree_decls(t);
1,460✔
2193
   for (int i = 0; i < ndecls; i++) {
29,504✔
2194
     tree_t d = tree_decl(t, i);
28,046✔
2195
     tree_kind_t kind = tree_kind(d);
28,046✔
2196
     if ((kind == T_FUNC_BODY) || (kind == T_PROC_BODY))
28,046✔
2197
       sem_error(d, "subprogram body is not allowed in package specification");
28,046✔
2198
   }
2199

2200
   return true;
2201
}
2202

2203
static bool sem_check_pack_inst(tree_t t, nametab_t *tab)
278✔
2204
{
2205
   if (tree_generics(t) == 0)
278✔
2206
      return false;   // Was a parse error
2207

2208
   if (!sem_check_generic_map(t, t, tab))
275✔
2209
      return false;
6✔
2210

2211
   // Other declarations were checked on the uninstantiated package
2212

2213
   return true;
2214
}
2215

2216
static bool sem_check_missing_bodies(tree_t secondary, nametab_t *tab)
974✔
2217
{
2218
   // Check for any missing subprogram or protected type bodies, and
2219
   // deferred constants which were not given values and
2220
   tree_t primary = tree_primary(secondary);
974✔
2221
   const int ndecls = tree_decls(primary);
974✔
2222
   for (int i = 0; i < ndecls; i++) {
19,490✔
2223
      tree_t d = tree_decl(primary, i);
18,518✔
2224
      switch (tree_kind(d)) {
18,518✔
2225
      case T_FUNC_DECL:
14,445✔
2226
      case T_PROC_DECL:
2227
         if (tree_flags(d) & TREE_F_PREDEFINED)
14,445✔
2228
            break;
2229
         // Fall-through
2230
      case T_PROT_DECL:
2231
         sem_missing_body_cb(d, secondary, tab);
6,772✔
2232
         break;
6,772✔
2233
      case T_CONST_DECL:
1,177✔
2234
         if (!tree_has_value(d)) {
1,177✔
2235
            tree_t d2 = get_local_decl(tab, NULL, tree_ident(d), 0);
402✔
2236
            if (d2 == NULL || !tree_has_value(d2))
402✔
2237
               sem_error(d, "deferred constant %s was not given a value in the "
2✔
2238
                         "package body", istr(tree_ident(d)));
2239
         }
2240
      default:
2241
         break;
2242
      }
2243
   }
2244

2245
   return true;
2246
}
2247

2248
static bool sem_check_pack_body(tree_t t, nametab_t *tab)
777✔
2249
{
2250
   if (!tree_has_primary(t))
777✔
2251
      return false;
2252

2253
   if (!sem_check_context_clause(t, tab))
772✔
2254
      return false;
2255

2256
   if (!sem_check_missing_bodies(t, tab))
772✔
2257
      return false;
2✔
2258

2259
   return true;
2260
}
2261

2262
static bool sem_check_component(tree_t t, nametab_t *tab)
315✔
2263
{
2264
   return true;
315✔
2265
}
2266

2267
static void sem_passive_cb(tree_t t, void *context)
1✔
2268
{
2269
   tree_t s = context;
1✔
2270

2271
   diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
1✔
2272
   diag_printf(d, "signal assignment statement not allowed inside passive "
1✔
2273
               "process");
2274
   diag_hint(d, tree_loc(s), "process in entity statement part must "
1✔
2275
             "be passive");
2276
   diag_hint(d, tree_loc(t), "signal assignment statement");
1✔
2277
   diag_lrm(d, STD_93, "1.1.3");
1✔
2278
   diag_lrm(d, STD_93, "9.2");
1✔
2279

2280
   diag_emit(d);
1✔
2281
}
1✔
2282

2283
static bool sem_check_entity(tree_t t, nametab_t *tab)
4,990✔
2284
{
2285
   if (!sem_check_context_clause(t, tab))
4,990✔
2286
      return false;
2287

2288
   // All processes in entity statement part must be passive
2289
   const int nstmts = tree_stmts(t);
4,990✔
2290
   for (int i = 0; i < nstmts; i++) {
5,027✔
2291
      tree_t s = tree_stmt(t, i);
37✔
2292
      tree_visit_only(s, sem_passive_cb, s, T_SIGNAL_ASSIGN);
37✔
2293
   }
2294

2295
   return true;
2296
}
2297

2298
static bool sem_check_arch(tree_t t, nametab_t *tab)
5,004✔
2299
{
2300
   if (!tree_has_primary(t))
5,004✔
2301
      return false;
2302

2303
   if (!sem_check_context_clause(t, tab))
5,001✔
2304
      return false;
×
2305

2306
   return true;
2307
}
2308

2309
static tree_t sem_check_lvalue(tree_t t)
30,427✔
2310
{
2311
   switch (tree_kind(t)) {
68,468✔
2312
   case T_REF:
30,518✔
2313
      return sem_check_lvalue(tree_ref(t));
30,518✔
2314
   case T_ARRAY_SLICE:
7,523✔
2315
   case T_ARRAY_REF:
2316
   case T_ALIAS:
2317
   case T_RECORD_REF:
2318
   case T_ALL:
2319
      return sem_check_lvalue(tree_value(t));
7,523✔
2320
   case T_VAR_DECL:
2321
   case T_SIGNAL_DECL:
2322
   case T_PORT_DECL:
2323
   case T_CONST_DECL:
2324
   case T_IMPLICIT_SIGNAL:
2325
   case T_PARAM_DECL:
2326
   case T_EXTERNAL_NAME:
2327
      return t;
2328
   default:
10✔
2329
      return NULL;
10✔
2330
   }
2331
}
2332

2333
static bool sem_check_aggregate_target_element(tree_t target, int nth)
342✔
2334
{
2335
   tree_t a = tree_assoc(target, nth);
342✔
2336
   tree_t value = tree_value(a);
342✔
2337

2338
   if (tree_kind(value) != T_AGGREGATE) {
342✔
2339
      if (!sem_static_name(value, sem_locally_static))
324✔
2340
         sem_error(value, "aggregate element must be locally static name");
2✔
2341
   }
2342

2343
   const assoc_kind_t kind = tree_subkind(a);
340✔
2344
   switch (kind) {
340✔
2345
   case A_RANGE:
2✔
2346
      if (standard() >= STD_08) {
2✔
2347
         // LRM 08 section 10.6.2.1: it is an error if the element
2348
         // association contains a choice that is a discrete range and
2349
         // an expression of a type other than the aggregate type.
2350
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(value));
1✔
2351
         diag_printf(d, "range choice expression must have same type "
1✔
2352
                     "as aggregate");
2353
         diag_hint(d, tree_loc(value), "expression type is %s but "
1✔
2354
                   "aggregate is %s", type_pp(tree_type(value)),
2355
                   type_pp(tree_type(target)));
2356
         diag_lrm(d, STD_08, "10.6.2");
1✔
2357
         diag_emit(d);
1✔
2358
         return false;
1✔
2359
      }
2360
      // Fall-through
2361
   case A_OTHERS:
2362
      sem_error(a, "%s association not allowed in aggregate target",
4✔
2363
                assoc_kind_str(kind));
2364
   case A_NAMED:
2365
   case A_POS:
2366
   case A_SLICE:
2367
   case A_CONCAT:
2368
      break;
2369
   }
2370

2371
   const int nassocs = tree_assocs(target);
335✔
2372
   for (int j = nth + 1; j < nassocs; j++) {
605✔
2373
      tree_t cmp = tree_value(tree_assoc(target, j));
272✔
2374
      if (same_tree(value, cmp)) {
272✔
2375
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(cmp));
2✔
2376
         diag_printf(d, "%s %s is identifed more than once in "
4✔
2377
                     "aggregate target", class_str(class_of(cmp)),
2378
                     tree_kind(cmp) == T_REF ?
2✔
2379
                     istr(tree_ident(cmp)) : "subelement");
2✔
2380
         diag_hint(d, tree_loc(value), "first seen here");
2✔
2381
         diag_lrm(d, STD_08, "10.5.2");
2✔
2382
         diag_emit(d);
2✔
2383
         return false;
2✔
2384
      }
2385
   }
2386

2387
   return true;
2388
}
2389

2390
static bool sem_check_variable_target(tree_t target)
16,542✔
2391
{
2392
   if (tree_kind(target) == T_AGGREGATE) {
16,542✔
2393
      // Rules for aggregate variable targets in LRM 93 section 8.5
2394

2395
      type_t type = tree_type(target);
71✔
2396
      if (!type_is_composite(type))
71✔
2397
         sem_error(target, "aggregate target of variable assignment has "
×
2398
                   "non-composite type %s", type_pp(tree_type(target)));
2399

2400
      const int nassocs = tree_assocs(target);
71✔
2401
      for (int i = 0; i < nassocs; i++) {
222✔
2402
         tree_t a = tree_assoc(target, i);
157✔
2403
         tree_t value = tree_value(a);
157✔
2404

2405
         if (!sem_check_variable_target(value))
157✔
2406
            return false;
2407

2408
         if (!sem_check_aggregate_target_element(target, i))
156✔
2409
            return false;
2410
      }
2411
   }
2412
   else {
2413
      tree_t decl = sem_check_lvalue(target);
16,471✔
2414
      const tree_kind_t kind = decl ? tree_kind(decl) : T_LAST_TREE_KIND;
16,471✔
2415

2416
      const bool suitable = kind == T_VAR_DECL
32,941✔
2417
         || (kind == T_PARAM_DECL && tree_class(decl) == C_VARIABLE)
2,597✔
2418
         || (kind == T_EXTERNAL_NAME && tree_class(decl) == C_VARIABLE);
16,477✔
2419

2420
      if (!suitable) {
16,471✔
2421
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(target));
6✔
2422
         diag_printf(d, "target of variable assignment must be a variable "
6✔
2423
                     "name or aggregate");
2424

2425
         tree_t ref = name_to_ref(target);
6✔
2426
         if (ref != NULL && tree_has_ref(ref))
6✔
2427
            diag_hint(d, tree_loc(target), "%s is a %s", istr(tree_ident(ref)),
5✔
2428
                      class_str(class_of(tree_ref(ref))));
2429

2430
         diag_emit(d);
6✔
2431
         return false;
6✔
2432
      }
2433
      else if (kind == T_PARAM_DECL && tree_subkind(decl) == PORT_IN) {
16,465✔
2434
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(target));
1✔
2435
         diag_printf(d, "cannot assign to parameter %s with mode IN",
1✔
2436
                     istr(tree_ident(decl)));
2437
         diag_hint(d, tree_loc(decl), "%s declared with mode IN",
1✔
2438
                   istr(tree_ident(decl)));
2439
         diag_hint(d, tree_loc(target), "target of variable assignment");
1✔
2440
         diag_emit(d);
1✔
2441
         return false;
1✔
2442
      }
2443
      else if (type_is_protected(tree_type(target)))
16,464✔
2444
         sem_error(target, "may not assign to variable of a protected type");
1✔
2445
   }
2446

2447
   return true;
2448
}
2449

2450
static bool sem_check_var_assign(tree_t t, nametab_t *tab)
16,499✔
2451
{
2452
   tree_t target = tree_target(t);
16,499✔
2453
   tree_t value = tree_value(t);
16,499✔
2454

2455
   if (!sem_check(target, tab))
16,499✔
2456
      return false;
2457

2458
   if (!sem_check(value, tab))
16,473✔
2459
      return false;
2460

2461
   if (!sem_check_readable(value))
16,386✔
2462
      return false;
2463

2464
   if (!sem_check_variable_target(target))
16,385✔
2465
      return false;
2466

2467
   if (!sem_check_same_type(value, target)) {
16,372✔
2468
      type_t target_type = tree_type(target);
11✔
2469
      type_t value_type  = tree_type(value);
11✔
2470
      sem_error(t, "type of value %s does not match type of target %s",
11✔
2471
                type_pp2(value_type, target_type),
2472
                type_pp2(target_type, value_type));
2473
   }
2474

2475
   return true;
2476
}
2477

2478
static bool sem_check_waveforms(tree_t t, tree_t target, nametab_t *tab)
7,520✔
2479
{
2480
   type_t std_time = std_type(NULL, STD_TIME);
7,520✔
2481
   type_t expect = tree_type(target);
7,520✔
2482

2483
   const int nwaves = tree_waveforms(t);
7,520✔
2484
   for (int i = 0; i < nwaves; i++) {
15,248✔
2485
      tree_t waveform = tree_waveform(t, i);
7,745✔
2486

2487
      if (tree_has_value(waveform)) {
7,745✔
2488
         tree_t value = tree_value(waveform);
7,728✔
2489

2490
         if (!sem_check(value, tab))
7,728✔
2491
            return false;
2492

2493
         if (!sem_check_readable(value))
7,720✔
2494
            return false;
2495

2496
         if (!sem_check_type(value, expect, tab))
7,719✔
2497
            sem_error(t, "type of value %s does not match type of target %s",
6✔
2498
                      type_pp2(tree_type(value), expect),
2499
                      type_pp2(expect, tree_type(value)));
2500
      }
2501
      else {
2502
         tree_t decl = sem_check_lvalue(target);
17✔
2503
         if (decl != NULL && !is_guarded_signal(decl))
17✔
2504
            sem_error(waveform, "a null waveform element is only valid when "
2✔
2505
                      "the target is a guarded signal");
2506
      }
2507

2508
      if (tree_has_delay(waveform)) {
7,728✔
2509
         tree_t delay = tree_delay(waveform);
870✔
2510
         if (!sem_check(delay, tab))
870✔
2511
            return false;
2512

2513
         if (!sem_check_type(delay, std_time, tab))
870✔
2514
            sem_error(delay, "type of delay must be %s but have %s",
7,728✔
2515
                      type_pp(std_time), type_pp(tree_type(delay)));
2516
      }
2517
   }
2518

2519
   return true;
2520
}
2521

2522
static tree_t sem_check_view_target(tree_t target)
233✔
2523
{
2524
   switch (tree_kind(target)) {
250✔
2525
   case T_REF:
160✔
2526
      {
2527
         tree_t decl = tree_ref(target);
160✔
2528
         if (tree_kind(decl) == T_PORT_DECL) {
160✔
2529
            const port_mode_t mode = tree_subkind(decl);
71✔
2530
            if (mode == PORT_ARRAY_VIEW || mode == PORT_RECORD_VIEW)
71✔
2531
               return tree_value(decl);
71✔
2532
         }
2533

2534
         return NULL;
2535
      }
2536

2537
   case T_ARRAY_REF:
17✔
2538
   case T_ARRAY_SLICE:
2539
      return sem_check_view_target(tree_value(target));
17✔
2540

2541
   case T_RECORD_REF:
73✔
2542
      {
2543
         tree_t view = sem_check_view_target(tree_value(target));
73✔
2544
         if (view == NULL)
73✔
2545
            return NULL;
2546

2547
         bool converse = false;
55✔
2548
         tree_t f = tree_ref(target);
55✔
2549
         tree_t e = find_element_mode_indication(view, f, &converse);
55✔
2550
         if (e == NULL)
55✔
2551
            return NULL;
2552

2553
         if (converse_mode(e, converse) == PORT_IN) {
55✔
2554
            tree_t port = tree_ref(name_to_ref(target));
2✔
2555
            diag_t *d = diag_new(DIAG_ERROR, tree_loc(target));
2✔
2556
            diag_printf(d, "cannot assign to element %s of port %s which has "
2✔
2557
                        "mode IN from mode view indication",
2558
                        istr(tree_ident(e)), istr(tree_ident(port)));
2559
            diag_hint(d, tree_loc(target), "target of signal assignment");
2✔
2560
            diag_hint(d, tree_loc(port), "sub-element %s of %s declared with "
2✔
2561
                      "mode IN due to mode view indication",
2562
                      istr(tree_ident(e)), istr(tree_ident(port)));
2563
            diag_emit(d);
2✔
2564
            return NULL;
2✔
2565
         }
2566

2567
         return NULL;
2568
      }
2569

2570
   default:
2571
      return NULL;
2572
   }
2573
}
2574

2575
static bool sem_check_signal_target(tree_t target, nametab_t *tab, bool guarded)
7,591✔
2576
{
2577
   if (tree_kind(target) == T_AGGREGATE) {
7,591✔
2578
      // Rules for aggregate signal targets in LRM 93 section 8.4
2579

2580
      type_t type = tree_type(target);
79✔
2581
      if (!type_is_composite(type))
79✔
2582
         sem_error(target, "aggregate target of signal assignment has "
×
2583
                   "non-composite type %s", type_pp(type));
2584

2585
      bool has_guarded = false, has_unguarded = false;
79✔
2586
      const int nassocs = tree_assocs(target);
79✔
2587
      for (int i = 0; i < nassocs; i++) {
261✔
2588
         tree_t a = tree_assoc(target, i);
191✔
2589
         tree_t value = tree_value(a);
191✔
2590

2591
         if (!sem_check_signal_target(value, tab, guarded))
191✔
2592
            return false;
2593

2594
         if (!sem_check_aggregate_target_element(target, i))
186✔
2595
            return false;
2596

2597
         tree_t ref = name_to_ref(value);
182✔
2598
         if (ref != NULL && is_guarded_signal(tree_ref(ref)))
182✔
2599
            has_guarded = true;
2600
         else
2601
            has_unguarded = true;
2602
      }
2603

2604
      if (has_guarded && has_unguarded) {
70✔
2605
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(target));
1✔
2606
         diag_printf(d, "aggregate target of signal assignment contains both "
1✔
2607
                     "guarded and unguarded signals");
2608
         diag_lrm(d, STD_08, "11.6");
1✔
2609
         diag_emit(d);
1✔
2610
         return false;
1✔
2611
      }
2612

2613
      return true;
2614
   }
2615
   else {
2616
      tree_t decl = sem_check_lvalue(target);
7,512✔
2617
      if (decl == NULL)
7,512✔
2618
         sem_error(target, "target of signal assignment must be a signal "
4✔
2619
                   "name or aggregate");
2620

2621
      if (is_guarded_signal(decl) && !guarded) {
7,508✔
2622
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(target));
1✔
2623
         diag_printf(d, "guarded signal %s cannot be the target of an "
1✔
2624
                     "unguarded assignment", istr(tree_ident(decl)));
2625
         diag_hint(d, tree_loc(decl), "%s declared here",
1✔
2626
                   istr(tree_ident(decl)));
2627
         diag_lrm(d, STD_08, "11.6");
1✔
2628
         diag_emit(d);
1✔
2629
         return false;
1✔
2630
      }
2631

2632
      switch (tree_kind(decl)) {
7,507✔
2633
      case T_SIGNAL_DECL:
5,828✔
2634
         {
2635
            tree_t sub = find_enclosing(tab, S_SUBPROGRAM);
5,828✔
2636
            if (sub != NULL && find_enclosing(tab, S_PROCESS) == NULL) {
5,828✔
2637
               // LRM 08 section 10.5.2.2: if a signal assignment appears
2638
               // in a procedure not contained within a process then the
2639
               // target must be a formal parameter
2640
               diag_t *d = diag_new(DIAG_ERROR, tree_loc(target));
4✔
2641
               diag_printf(d, "signal %s is not a formal parameter and "
4✔
2642
                           "subprogram %s is not contained within a process "
2643
                           "statement", istr(tree_ident(decl)),
2644
                           type_pp(tree_type(sub)));
2645
               diag_lrm(d, STD_08, "10.5.2.2");
4✔
2646
               diag_emit(d);
4✔
2647
               return false;
4✔
2648
            }
2649
         }
2650
         break;
2651

2652
      case T_IMPLICIT_SIGNAL:
1✔
2653
         sem_error(target, "implicit signal may not be assigned");
1✔
2654

2655
      case T_PORT_DECL:
1,649✔
2656
      case T_PARAM_DECL:
2657
         {
2658
            const port_mode_t mode = tree_subkind(decl);
1,649✔
2659
            if (mode == PORT_IN) {
1,649✔
2660
               diag_t *d = diag_new(DIAG_ERROR, tree_loc(target));
5✔
2661
               diag_printf(d, "cannot assign to input %s %s",
11✔
2662
                           tree_kind(decl) == T_PORT_DECL
5✔
2663
                           ? "port" : "parameter",
2664
                           istr(tree_ident(decl)));
2665
               diag_hint(d, tree_loc(target), "target of signal assignment");
5✔
2666
               diag_hint(d, tree_loc(decl), "%s declared with mode IN",
5✔
2667
                         istr(tree_ident(decl)));
2668
               diag_emit(d);
5✔
2669
               return false;
5✔
2670
            }
2671
            else if (mode == PORT_ARRAY_VIEW || mode == PORT_RECORD_VIEW) {
1,644✔
2672
               tree_t view = sem_check_view_target(target);
74✔
2673
               if (view != NULL) {
74✔
2674
                  tree_t inport = NULL;
4✔
2675
                  type_t view_type = tree_type(view);
4✔
2676
                  const int nelems = type_fields(view_type);
4✔
2677
                  for (int i = 0; i < nelems; i++) {
10✔
2678
                     tree_t e = type_field(view_type, i);
7✔
2679
                     const port_mode_t mode = tree_subkind(e);
7✔
2680
                     if (mode == PORT_IN || mode == PORT_ARRAY_VIEW
7✔
2681
                         || mode == PORT_RECORD_VIEW) {
6✔
2682
                        // This is not correct for nested mode view
2683
                        // indications but seems like a very obscure
2684
                        // corner case
2685
                        inport = e;
2686
                        break;
2687
                     }
2688
                  }
2689

2690
                  if (inport != NULL) {
4✔
2691
                     diag_t *d = diag_new(DIAG_ERROR, tree_loc(target));
1✔
2692
                     diag_printf(d, "cannot assign to port %s with mode view "
1✔
2693
                                 "indication as one or more sub-elements have "
2694
                                 "mode IN", istr(tree_ident(decl)));
2695
                     diag_hint(d, tree_loc(target),
1✔
2696
                               "target of signal assignment");
2697
                     diag_hint(d, tree_loc(inport),
1✔
2698
                               "element %s declared with mode IN",
2699
                               istr(tree_ident(inport)));
2700
                     diag_emit(d);
1✔
2701
                     return false;
1✔
2702
                  }
2703
               }
2704

2705
               return true;
73✔
2706
            }
2707
            else if (mode == PORT_LINKAGE)
1,570✔
2708
               sem_error(target, "linkage port %s may not be updated except as "
1✔
2709
                         "an actual corresponding to an interface of mode "
2710
                         "linkage", istr(tree_ident(decl)));
2711
            else if (tree_class(decl) != C_SIGNAL) {
1,569✔
2712
               diag_t *d = diag_new(DIAG_ERROR, tree_loc(target));
1✔
2713
               diag_printf(d, "%s is not a valid target of signal assignment",
1✔
2714
                           istr(tree_ident(decl)));
2715
               diag_hint(d, tree_loc(target), "target of signal assignment");
1✔
2716
               diag_hint(d, tree_loc(decl), "declared with class %s",
1✔
2717
                         class_str(tree_class(decl)));
2718
               diag_emit(d);
1✔
2719
               return false;
1✔
2720
            }
2721
         }
2722
         break;
2723

2724
      case T_EXTERNAL_NAME:
28✔
2725
         {
2726
            if (tree_class(decl) != C_SIGNAL) {
28✔
2727
               tree_t tail = tree_part(decl, tree_parts(decl) - 1);
×
2728
               diag_t *d = diag_new(DIAG_ERROR, tree_loc(target));
×
2729
               diag_printf(d, "external name %s is not a valid target of "
×
2730
                           "signal assignment", istr(tree_ident(tail)));
2731
               diag_hint(d, tree_loc(target), "target of signal assignment");
×
2732
               diag_hint(d, tree_loc(decl), "declared with class %s",
×
2733
                         class_str(tree_class(decl)));
2734
               diag_emit(d);
×
2735
               return false;
×
2736
            }
2737

2738
            tree_t sub = find_enclosing(tab, S_SUBPROGRAM);
28✔
2739
            if (sub != NULL && find_enclosing(tab, S_PROCESS) == NULL)
28✔
2740
               sem_error(target, "cannot create driver for external name as "
1✔
2741
                         "subprogram %s is not contained within a process "
2742
                         "statement", type_pp(tree_type(sub)));
2743
         }
2744
         break;
2745

2746
      case T_VAR_DECL:
1✔
2747
      case T_CONST_DECL:
2748
         {
2749
            diag_t *d = diag_new(DIAG_ERROR, tree_loc(target));
1✔
2750
            diag_printf(d, "%s %s is not a valid target of signal assignment",
1✔
2751
                        class_str(class_of(decl)), istr(tree_ident(decl)));
2752
            diag_hint(d, tree_loc(target), "target of signal assignment");
1✔
2753
            diag_hint(d, tree_loc(decl), "declared as %s",
1✔
2754
                      class_str(class_of(decl)));
2755
            diag_emit(d);
1✔
2756
            return false;
1✔
2757
         }
2758

2759
      default:
×
2760
         sem_error(target, "invalid target of signal assignment");
×
2761
      }
2762

2763
      return true;
7,419✔
2764
   }
2765
}
2766

2767
static bool sem_check_reject(tree_t t, nametab_t *tab)
646✔
2768
{
2769
   if (!sem_check(t, tab))
646✔
2770
      return false;
2771

2772
   if (!type_eq(tree_type(t), std_type(NULL, STD_TIME)))
646✔
2773
      sem_error(t, "reject interval must have type TIME but have %s",
1✔
2774
                type_pp(tree_type(t)));
2775

2776
   return true;
2777
}
2778

2779
static bool sem_check_signal_assign(tree_t t, nametab_t *tab)
5,246✔
2780
{
2781
   tree_t target = tree_target(t);
5,246✔
2782

2783
   if (!sem_check(target, tab))
5,246✔
2784
      return false;
2785

2786
   if (!sem_check_signal_target(target, tab, true))
5,242✔
2787
      return false;
2788

2789
   if (!sem_check_waveforms(t, target, tab))
5,226✔
2790
      return false;
2791

2792
   if (tree_has_reject(t) && !sem_check_reject(tree_reject(t), tab))
5,213✔
2793
      return false;
×
2794

2795
   return true;
2796
}
2797

2798
static bool sem_check_guard(tree_t t, nametab_t *tab)
14✔
2799
{
2800
   assert(tree_kind(t) == T_GUARD);
14✔
2801

2802
   if (!sem_check_type(t, std_type(NULL, STD_BOOLEAN), tab))
14✔
2803
      sem_error(t, "guard signal must have BOOLEAN type but found %s",
1✔
2804
                type_pp(tree_type(t)));
2805

2806
   tree_t decl = tree_ref(t);
13✔
2807
   switch (tree_kind(decl)) {
13✔
2808
   case T_SIGNAL_DECL:
2809
   case T_IMPLICIT_SIGNAL:
2810
      break;
2811
   case T_PORT_DECL:
×
2812
      if (tree_class(decl) == C_SIGNAL)
×
2813
         break;
2814
      // Fall-through
2815
   default:
2816
      {
2817
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
1✔
2818
         diag_printf(d, "assignment guard must be a signal");
1✔
2819
         diag_hint(d, tree_loc(decl), "%s is a %s", istr(tree_ident(decl)),
1✔
2820
                   class_str(class_of(decl)));
2821
         diag_hint(d, tree_loc(t), "guarded statement");
1✔
2822
         diag_emit(d);
1✔
2823
         return false;
1✔
2824
      }
2825
   }
2826

2827
   return true;
2828
}
2829

2830
static bool sem_check_cond_assign(tree_t t, nametab_t *tab)
2,161✔
2831
{
2832
   tree_t target = tree_target(t);
2,161✔
2833

2834
   if (!sem_check(target, tab))
2,161✔
2835
      return false;
2836

2837
   const bool has_guard = tree_has_guard(t);
2,158✔
2838

2839
   if (!sem_check_signal_target(target, tab, has_guard))
2,158✔
2840
      return false;
2841

2842
   if (has_guard && !sem_check_guard(tree_guard(t), tab))
2,149✔
2843
      return false;
2844

2845
   type_t std_bool = std_type(NULL, STD_BOOLEAN);
2,147✔
2846

2847
   const int nconds = tree_conds(t);
2,147✔
2848
   for (int i = 0; i < nconds; i++) {
4,437✔
2849
      tree_t c = tree_cond(t, i);
2,296✔
2850

2851
      if (tree_has_value(c)) {
2,296✔
2852
         tree_t test = tree_value(c);
229✔
2853

2854
         if (!sem_check(test, tab))
229✔
2855
            return false;
2856

2857
         if (!type_eq(tree_type(test), std_bool))
229✔
2858
            sem_error(test, "type of condition must be BOOLEAN");
1✔
2859
      }
2860

2861
      assert(tree_stmts(c) == 1);
2,295✔
2862
      tree_t a = tree_stmt(c, 0);
2,295✔
2863

2864
      assert(tree_kind(a) == T_SIGNAL_ASSIGN);
2,295✔
2865
      assert(tree_target(a) == target);
2,295✔
2866

2867
      if (tree_has_reject(a) && !sem_check_reject(tree_reject(a), tab))
2,295✔
2868
         return false;
2869

2870
      if (!sem_check_waveforms(a, target, tab))
2,294✔
2871
         return false;
2872
   }
2873

2874
   return true;
2875
}
2876

2877
static bool sem_check_closely_related(type_t from, type_t to, tree_t where)
9,546✔
2878
{
2879
   if (type_eq(to, from))
9,546✔
2880
      return true;
2881

2882
   // Conversions are allowed between any abstract numeric types
2883
   if (type_is_numeric(from) && type_is_numeric(to))
5,310✔
2884
      return true;
2885

2886
   // Suppress cascading errors
2887
   if (type_is_none(from) || type_is_none(to))
2,380✔
2888
      return true;
1✔
2889

2890
   char *reason = NULL;
2,379✔
2891

2892
   if (type_is_array(from) && type_is_array(to)) {
2,379✔
2893
      const int from_dims = dimension_of(from);
2,354✔
2894
      const int to_dims = dimension_of(to);
2,354✔
2895

2896
      // Types must have same dimensionality
2897
      if (from_dims != to_dims) {
2,354✔
2898
         reason = xasprintf("%s has %d dimension%s but %s has %d",
1✔
2899
                            type_pp2(from, to), from_dims,
2900
                            from_dims == 1 ? "" : "s",
2901
                            type_pp2(to, from), to_dims);
2902
         goto not_closely_related;
1✔
2903
      }
2904

2905
      // Index types the same or closely related
2906
      for (int i = 0; i < from_dims; i++) {
4,706✔
2907
         type_t from_index = index_type_of(from, i);
2,354✔
2908
         type_t to_index = index_type_of(to, i);
2,354✔
2909

2910
         if (!sem_check_closely_related(from_index, to_index, NULL)) {
2,354✔
2911
            reason = xasprintf("%s index type of %s is %s which is not closely "
1✔
2912
                               "related to the %s index type of %s",
2913
                               ordinal_str(i + 1), type_pp2(from, to),
2914
                               type_pp(from_index), ordinal_str(i + 1),
2915
                               type_pp2(to, from));
2916
            goto not_closely_related;
1✔
2917
         }
2918
      }
2919

2920
      type_t from_e = type_elem(from);
2,352✔
2921
      type_t to_e = type_elem(to);
2,352✔
2922

2923
      if (standard() >= STD_08) {
2,352✔
2924
         // Element types must be closely related
2925
         if (!sem_check_closely_related(from_e, to_e, NULL)) {
1,774✔
2926
            reason = xasprintf("element type %s is not closely related to %s",
1✔
2927
                               type_pp2(from_e, to_e), type_pp2(to_e, from_e));
2928
            goto not_closely_related;
1✔
2929
         }
2930
      }
2931
      else {
2932
         // Element types must be the same
2933
         if (!type_eq(from_e, to_e)) {
578✔
2934
            reason = xasprintf("element type %s does not match %s",
1✔
2935
                               type_pp2(from_e, to_e), type_pp2(to_e, from_e));
2936
            goto not_closely_related;
1✔
2937
         }
2938
      }
2939

2940
      return true;
2,350✔
2941
   }
2942

2943
   if (type_is_record(from) && type_is_record(to) && standard() >= STD_19) {
25✔
2944
      // Each element of the target type must have a matching element in
2945
      // the from type
2946
      const int from_nf = type_fields(from);
21✔
2947
      const int to_nf = type_fields(to);
21✔
2948

2949
      for (int i = 0; i < to_nf; i++) {
68✔
2950
         tree_t to_f = type_field(to, i), from_f = NULL;
48✔
2951
         type_t to_ftype = tree_type(to_f);
48✔
2952
         ident_t name = tree_ident(to_f);
48✔
2953

2954
         for (int j = 0; j < from_nf && from_f == NULL; j++) {
130✔
2955
            tree_t f = type_field(from, j);
82✔
2956
            if (tree_ident(f) != name)
82✔
2957
               continue;
35✔
2958

2959
            type_t from_ftype = tree_type(f);
47✔
2960
            if (!sem_check_closely_related(from_ftype, to_ftype, NULL))
47✔
2961
               break;
2962

2963
            from_f = f;
2964
         }
2965

2966
         if (from_f != NULL)
48✔
2967
            continue;
47✔
2968

2969
         reason = xasprintf("field %s in record type %s has no matching "
1✔
2970
                            "element in type %s", istr(tree_ident(to_f)),
2971
                            type_pp2(to, from), type_pp2(from, to));
2972
         goto not_closely_related;
1✔
2973
      }
2974

2975
      return true;
2976
   }
2977

2978
 not_closely_related:
4✔
2979
   if (where != NULL) {
9✔
2980
      const loc_t *loc = tree_loc(where);
7✔
2981
      diag_t *d = diag_new(DIAG_ERROR, loc);
7✔
2982
      diag_printf(d, "conversion only allowed between closely related types");
7✔
2983
      if (reason != NULL)
7✔
2984
         diag_hint(d, loc, "%s", reason);
5✔
2985
      else
2986
         diag_hint(d, loc, "%s and %s are not closely related",
2✔
2987
                   type_pp2(from, to), type_pp2(to, from));
2988
      diag_lrm(d, STD_93, "7.3.5");
7✔
2989
      diag_emit(d);
7✔
2990

2991
      free(reason);
7✔
2992
   }
2993

2994
   return false;
2995
}
2996

2997
static bool sem_check_conversion(tree_t t, nametab_t *tab)
5,371✔
2998
{
2999
   // Type conversions are described in LRM 93 section 7.3.5
3000

3001
   tree_t value = tree_value(t);
5,371✔
3002
   if (!sem_check(value, tab))
5,371✔
3003
      return false;
3004

3005
   return sem_check_closely_related(tree_type(value), tree_type(t), t);
5,371✔
3006
}
3007

3008
static bool sem_check_compatible_view(tree_t formal, tree_t actual)
86✔
3009
{
3010
   type_t type = tree_type(formal);
86✔
3011

3012
   type_t elem_type = type;
86✔
3013
   if (tree_subkind(formal) == PORT_ARRAY_VIEW)
86✔
3014
      elem_type = type_elem(type);
12✔
3015

3016
   tree_t formal_view = tree_value(formal);
86✔
3017

3018
   tree_t actual_view = sem_check_view_target(actual);
86✔
3019
   if (actual_view != NULL) {
86✔
3020
      // Associating an interface with another interface: check the
3021
      // mode of each element is compatible
3022
      const int nfields = type_fields(elem_type);
12✔
3023
      for (int i = 0; i < nfields; i++) {
40✔
3024
         tree_t f = type_field(elem_type, i);
30✔
3025

3026
         bool formal_converse = false;
30✔
3027
         tree_t formal_elem = find_element_mode_indication(formal_view, f,
30✔
3028
                                                           &formal_converse);
3029

3030
         bool actual_converse = false;
30✔
3031
         tree_t actual_elem = find_element_mode_indication(actual_view, f,
30✔
3032
                                                           &actual_converse);
3033

3034
         const port_mode_t formal_mode =
30✔
3035
            converse_mode(formal_elem, formal_converse);
30✔
3036

3037
         const port_mode_t actual_mode =
30✔
3038
            converse_mode(actual_elem, actual_converse);
30✔
3039

3040
         if (formal_mode != actual_mode) {
30✔
3041
            diag_t *d = diag_new(DIAG_ERROR, tree_loc(actual));
2✔
3042
            diag_printf(d, "mode view indication of formal %s %s "
5✔
3043
                        "element %s is not compatible with actual",
3044
                        tree_kind(formal) == T_PORT_DECL ? "port" : "parameter",
2✔
3045
                        istr(tree_ident(formal)), istr(tree_ident(f)));
3046
            diag_hint(d, tree_loc(actual_view), "actual has mode %s from "
2✔
3047
                      "mode view indication on port %s",
3048
                      port_mode_str(actual_mode),
3049
                      istr(tree_ident(name_to_ref(actual))));
3050
            diag_hint(d, tree_loc(formal_view), "formal has mode %s",
2✔
3051
                      port_mode_str(formal_mode));
3052
            diag_emit(d);
2✔
3053
            return false;
2✔
3054
         }
3055
      }
3056
   }
3057

3058
   return true;
3059
}
3060

3061
static bool sem_check_call_args(tree_t t, tree_t decl, nametab_t *tab)
91,344✔
3062
{
3063
   const int nparams = tree_params(t);
91,344✔
3064
   const int nports  = tree_ports(decl);
91,344✔
3065

3066
   if (is_uninstantiated_subprogram(decl)) {
91,344✔
3067
      // Allow recursive calls to the same subprogram
3068
      tree_t sub = find_enclosing(tab, S_SUBPROGRAM);
5✔
3069
      if (sub != decl)
5✔
3070
         sem_error(t, "cannot call uninstantiated %s %s",
2✔
3071
                   class_str(class_of(decl)), istr(tree_ident(decl)));
3072
   }
3073

3074
   tree_t *map LOCAL = xcalloc_array(nports, sizeof(tree_t));
182,684✔
3075

3076
   bool have_named = false;
91,342✔
3077
   for (int i = 0; i < nparams; i++) {
258,796✔
3078
      tree_t param = tree_param(t, i), port = NULL;
167,542✔
3079
      type_t port_type = NULL;
167,542✔
3080
      bool partial = false;
167,542✔
3081
      int index = -1;
167,542✔
3082
      switch (tree_subkind(param)) {
167,542✔
3083
      case P_POS:
164,086✔
3084
         if (have_named)
164,086✔
3085
            sem_error(param, "positional parameters must precede named "
3✔
3086
                      "parameters");
3087
         else if ((index = tree_pos(param)) >= nports) {
164,083✔
3088
            diag_t *d = diag_new(DIAG_ERROR, tree_loc(param));
1✔
3089
            diag_printf(d, "too many positional parameters for subprogram %s",
1✔
3090
                        type_pp(tree_type(decl)));
3091
            diag_hint(d, tree_loc(param), "%s positional parameter",
1✔
3092
                      ordinal_str(index + 1));
3093
            diag_hint(d, tree_loc(decl), "%s %s has %d formal parameter%s",
2✔
3094
                      class_str(class_of(decl)), istr(tree_ident(decl)),
3095
                      nports, nports > 1 ? "s" : "");
3096
            diag_emit(d);
1✔
3097
            return false;
1✔
3098
         }
3099
         else {
3100
            port = tree_port(decl, index);
164,082✔
3101
            port_type = tree_type(port);
164,082✔
3102
         }
3103
         break;
164,082✔
3104

3105
      case P_NAMED:
3,456✔
3106
         {
3107
            have_named = true;
3,456✔
3108

3109
            tree_t name = tree_name(param);
3,456✔
3110
            const tree_kind_t name_kind = tree_kind(name);
3,456✔
3111
            if (name_kind == T_TYPE_CONV || name_kind == T_FCALL)
3,456✔
3112
               sem_error(name, "sorry, conversions are not yet supported here");
1✔
3113
            else if (!sem_check(name, tab))
3,455✔
3114
               return false;
3115

3116
            if (tree_kind(name) == T_TYPE_CONV) {
3,450✔
3117
               type_t to_type = tree_type(name);
×
3118
               type_t from_type = tree_type(tree_value(name));
×
3119

3120
               const bool missing_constraints =
×
3121
                  type_is_unconstrained(from_type)
×
3122
                  && type_is_unconstrained(to_type);
×
3123

3124
               if (missing_constraints) {
×
3125
                  diag_t *d = diag_new(DIAG_ERROR, tree_loc(name));
×
3126
                  diag_printf(d, "result of conversion for unconstrained "
×
3127
                              "formal %s must be a constrained array type",
3128
                              istr(tree_ident(decl)));
3129
                  diag_lrm(d, STD_93, "3.2.1.1");
×
3130
                  diag_emit(d);
×
3131
                  return false;
×
3132
               }
3133
            }
3134

3135
            tree_t ref = name_to_ref(name);
3,450✔
3136
            assert(ref != NULL);
3,450✔
3137

3138
            if ((partial = (ref != name))) {
3,450✔
3139
               tree_t value = tree_value(name);
57✔
3140
               if (tree_kind(value) != T_REF)
57✔
3141
                  sem_error(name, "sorry, this form of named parameter is "
×
3142
                            "not supported");
3143
            }
3144

3145
            ident_t id = tree_ident(ref);
3,450✔
3146
            for (int j = 0; j < nports; j++) {
13,203✔
3147
               tree_t p = tree_port(decl, j);
13,202✔
3148
               if (tree_ident(p) == id) {
13,202✔
3149
                  index = j;
3150
                  port = p;
3151
                  break;
3152
               }
3153
            }
3154

3155
            if (index == -1) {
3,450✔
3156
               diag_t *d = diag_new(DIAG_ERROR, tree_loc(param));
1✔
3157
               diag_printf(d, "subprogram %s has no parameter named %s",
1✔
3158
                           type_pp(tree_type(decl)), istr(id));
3159
               diag_hint(d, tree_loc(decl), "subprogram defined here");
1✔
3160
               diag_emit(d);
1✔
3161
               return false;
1✔
3162
            }
3163

3164
            if (!tree_has_ref(ref)) {
3,449✔
3165
               // Should have generated an error during overload
3166
               // resolution
3167
               assert(error_count() > 0);
×
3168
               return false;
3169
            }
3170

3171
            // Set the ref again here because solve_types may have set it
3172
            // to the wrong overload
3173
            if (tree_ref(ref) != port)
3,449✔
3174
               tree_set_name(param, (name = change_ref(name, port)));
776✔
3175

3176
            port_type = tree_type(name);
3,449✔
3177
         }
3178
      }
3179

3180
      class_t class    = tree_class(port);
167,531✔
3181
      port_mode_t mode = tree_subkind(port);
167,531✔
3182

3183
      if (map[index] != NULL && (!partial || tree_kind(map[index]) == T_REF))
167,531✔
3184
         sem_error(param, "formal parameter %s already has an associated "
2✔
3185
                   "actual", istr(tree_ident(port)));
3186

3187
      map[index] = param;
167,529✔
3188

3189
      tree_t value = tree_value(param);
167,529✔
3190
      if (!sem_check(value, tab))
167,529✔
3191
         return false;
3192

3193
      if (!sem_check_type(value, port_type, tab))
167,480✔
3194
         sem_error(value, "type of actual %s does not match formal %s type %s",
6✔
3195
                   type_pp2(tree_type(value), port_type),
3196
                   istr(tree_ident(port)),
3197
                   type_pp2(port_type, tree_type(value)));
3198

3199
      // LRM 08 sections 4.2.2.2 and 4.2.2.3
3200
      if (class == C_VARIABLE || class == C_SIGNAL) {
167,474✔
3201
         tree_t decl = sem_check_lvalue(value);
6,329✔
3202
         if (decl == NULL || class_of(value) != class) {
6,329✔
3203
            diag_t *d = diag_new(DIAG_ERROR, tree_loc(value));
8✔
3204
            diag_printf(d, "actual for formal %s with class %s must be "
12✔
3205
                        "a name denoting a %s", istr(tree_ident(port)),
3206
                        class == C_VARIABLE ? "VARIABLE" : "SIGNAL",
3207
                        class_str(class));
3208
            if (decl == NULL)
8✔
3209
               diag_hint(d, tree_loc(value), "actual designator is not a name");
4✔
3210
            else if (tree_kind(decl) == T_EXTERNAL_NAME)
4✔
3211
               diag_hint(d, tree_loc(value), "external name has class %s",
1✔
3212
                         class_str(tree_class(decl)));
3213
            else
3214
               diag_hint(d, tree_loc(value), "object %s has class %s",
3✔
3215
                         istr(tree_ident(decl)), class_str(class_of(decl)));
3216
            diag_lrm(d, STD_08, class == C_SIGNAL ? "4.2.2.3" : "4.2.2.2");
12✔
3217
            diag_emit(d);
8✔
3218
            return false;
8✔
3219
         }
3220

3221
         // Check OUT and INOUT parameters can be assigned to
3222
         if (mode == PORT_OUT || mode == PORT_INOUT) {
6,321✔
3223
            const tree_kind_t decl_kind = tree_kind(decl);
5,340✔
3224
            if ((decl_kind == T_PARAM_DECL || decl_kind == T_PORT_DECL)
5,340✔
3225
                && tree_subkind(decl) == PORT_IN) {
927✔
3226
               const char *what =
2✔
3227
                  decl_kind == T_PARAM_DECL ? "parameter" : "port";
1✔
3228
               diag_t *d = diag_new(DIAG_ERROR, tree_loc(value));
1✔
3229
               diag_printf(d, "cannot associate %s %s of mode IN with "
1✔
3230
                           "formal %s of mode %s", what,
3231
                           istr(tree_ident(decl)), istr(tree_ident(port)),
3232
                           port_mode_str(mode));
3233
               diag_hint(d, tree_loc(decl), "%s declared with mode %s",
1✔
3234
                         istr(tree_ident(decl)),
3235
                         port_mode_str(tree_subkind(decl)));
1✔
3236
               diag_hint(d, tree_loc(value), "associated with %s %s %s here",
1✔
3237
                         port_mode_str(mode), what, istr(tree_ident(port)));
3238
               diag_emit(d);
1✔
3239
               return false;
1✔
3240
            }
3241
            else if (decl_kind == T_SIGNAL_DECL && class == C_SIGNAL) {
5,339✔
3242
               tree_t sub = find_enclosing(tab, S_SUBPROGRAM);
237✔
3243
               if (sub != NULL && find_enclosing(tab, S_PROCESS) == NULL) {
237✔
3244
                  // LRM 08 section 10.5.2.2: if a signal is associated
3245
                  // with an inout or out signal parameter in a subprogram
3246
                  // call within a procedure not contained within a process
3247
                  // then the target must be a formal parameter
3248
                  diag_t *d = diag_new(DIAG_ERROR, tree_loc(value));
2✔
3249
                  diag_printf(d, "signal %s is not a formal parameter and "
2✔
3250
                              "subprogram %s is not contained within a process "
3251
                              "statement", istr(tree_ident(decl)),
3252
                              type_pp(tree_type(sub)));
3253
                  diag_lrm(d, STD_08, "10.5.2.2");
2✔
3254
                  diag_emit(d);
2✔
3255
                  return false;
2✔
3256
               }
3257
            }
3258
         }
3259
         else if ((mode == PORT_ARRAY_VIEW || mode == PORT_RECORD_VIEW)
981✔
3260
                  && !sem_check_compatible_view(port, value))
17✔
3261
            return false;
3262
      }
3263

3264
      if (class == C_SIGNAL && !sem_static_name(value, sem_globally_static)) {
167,462✔
3265
         diag_t *d = pedantic_diag(tree_loc(value));
4✔
3266
         if (d != NULL) {
4✔
3267
            diag_printf(d, "actual associated with signal parameter %s must be "
4✔
3268
                        "denoted by a static signal name",
3269
                        istr(tree_ident(port)));
3270
            diag_hint(d, tree_loc(value), "not a static signal name");
4✔
3271
            diag_lrm(d, STD_08, "4.2.2.3");
4✔
3272
            diag_lrm(d, STD_08, "8.1");
4✔
3273
            diag_emit(d);
4✔
3274
            return false;
4✔
3275
         }
3276
      }
3277

3278
      // Check IN and INOUT parameters can be read
3279
      if (tree_kind(t) != T_ATTR_REF) {
167,458✔
3280
         const port_mode_t mode = tree_subkind(port);
167,458✔
3281
         if (mode == PORT_IN || mode == PORT_INOUT) {
167,458✔
3282
            if (!sem_check_readable(value))
165,167✔
3283
               return false;
3284
         }
3285
      }
3286

3287
      if (tree_kind(value) == T_OPEN && !tree_has_value(port)) {
167,455✔
3288
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(param));
1✔
3289
         diag_printf(d, "OPEN actual for formal parameter %s without "
1✔
3290
                     "default value", istr(tree_ident(port)));
3291
         diag_hint(d, tree_loc(port), "%s declared here",
1✔
3292
                   istr(tree_ident(port)));
3293
         diag_emit(d);
1✔
3294
         return false;
1✔
3295
      }
3296
   }
3297

3298
   for (int i = 0; i < nports; i++) {
264,507✔
3299
      if (map[i] == NULL) {
173,256✔
3300
         tree_t port = tree_port(decl, i);
5,847✔
3301
         if (!tree_has_value(port))
5,847✔
3302
            sem_error(t, "missing actual for formal parameter %s without "
2✔
3303
                      "default value", istr(tree_ident(port)));
3304
         else {
3305
            const port_mode_t mode = tree_subkind(port);
5,845✔
3306
            if (mode == PORT_ARRAY_VIEW || mode == PORT_RECORD_VIEW)
5,845✔
3307
               sem_error(t, "missing actual for formal parameter %s with "
173,254✔
3308
                         "mode view indication %s", istr(tree_ident(port)),
3309
                         type_pp(tree_type(tree_value(port))));
3310
         }
3311
      }
3312
   }
3313

3314
   return true;
3315
}
3316

3317
static bool sem_check_fcall(tree_t t, nametab_t *tab)
83,494✔
3318
{
3319
   if (!tree_has_ref(t))
83,494✔
3320
      return false;
3321

3322
   if (tree_kind(t) == T_PROT_FCALL && tree_has_name(t)) {
83,409✔
3323
      tree_t name = tree_name(t);
797✔
3324
      if (!sem_check(name, tab))
797✔
3325
         return false;
3326
   }
3327

3328
   tree_t decl = tree_ref(t), sub;
83,409✔
3329
   const tree_flags_t flags = tree_flags(decl);
83,409✔
3330

3331
   if ((flags & TREE_F_IMPURE) && (sub = find_enclosing(tab, S_SUBPROGRAM))) {
83,409✔
3332
      // Pure function may not call an impure function
3333
      if (tree_kind(sub) == T_FUNC_BODY && !(tree_flags(sub) & TREE_F_IMPURE)) {
1,036✔
3334
         diag_t *d = pedantic_diag(tree_loc(t));
2✔
3335
         if (d != NULL) {
2✔
3336
            diag_printf(d, "pure function %s cannot call impure function %s",
2✔
3337
                        istr(tree_ident(sub)), istr(tree_ident(decl)));
3338
            diag_emit(d);
2✔
3339
         }
3340
      }
3341

3342
      // Propagate impurity flags
3343
      tree_set_flag(sub, flags & (TREE_F_IMPURE_FILE | TREE_F_IMPURE_SHARED));
1,036✔
3344
   }
3345
   else if (tree_kind(decl) == T_FUNC_DECL && !(flags & TREE_F_PREDEFINED)
82,373✔
3346
            && is_same_region(tab, decl) && opt_get_int(OPT_MISSING_BODY)) {
13,189✔
3347
      diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
1✔
3348
      diag_printf(d, "subprogram %s called before its body has been "
1✔
3349
                  "elaborated", type_pp(tree_type(decl)));
3350
      diag_hint(d, tree_loc(decl), "%s declared here",
1✔
3351
                type_pp(tree_type(decl)));
3352
      diag_lrm(d, STD_08, "14.4.2");
1✔
3353
      diag_emit(d);
1✔
3354
      return false;
1✔
3355
   }
3356

3357
   if (!sem_check_call_args(t, decl, tab))
83,408✔
3358
      return false;
3359

3360
   if (sem_locally_static(t))
83,354✔
3361
      tree_set_flag(t, TREE_F_LOCALLY_STATIC | TREE_F_GLOBALLY_STATIC);
12,552✔
3362
   else if (sem_globally_static(t))
70,802✔
3363
      tree_set_flag(t, TREE_F_GLOBALLY_STATIC);
12,248✔
3364

3365
   return true;
3366
}
3367

3368
static bool sem_check_pcall(tree_t t, nametab_t *tab)
7,974✔
3369
{
3370
   if (!tree_has_ref(t))
7,974✔
3371
      return false;
3372

3373
   const bool is_protected = (tree_kind(t) == T_PROT_PCALL);
7,938✔
3374
   if (is_protected && tree_has_name(t)
7,938✔
3375
       && !sem_check(tree_name(t), tab))
200✔
3376
      return false;
3377

3378
   tree_t decl = tree_ref(t);
7,937✔
3379

3380
   switch (class_of(decl)) {
7,937✔
3381
   case C_PROCEDURE:
3382
      break;
7,936✔
3383
   case C_FUNCTION:
1✔
3384
      sem_error(t, "function %s cannot be called as a procedure",
1✔
3385
                type_pp(tree_type(decl)));
3386
   default:
×
3387
      // All other errors should be caught at parsing stage
3388
      assert(error_count() > 0);
×
3389
      return false;
3390
   }
3391

3392
   if (!sem_check_call_args(t, decl, tab))
7,936✔
3393
      return false;
3394

3395
   const tree_flags_t flags = tree_flags(decl);
7,897✔
3396

3397
   const bool never_waits = is_protected || !!(flags & TREE_F_NEVER_WAITS);
7,897✔
3398
   const bool has_wait = !is_protected && !!(flags & TREE_F_HAS_WAIT);
7,897✔
3399

3400
   assert(!never_waits || !has_wait);
7,897✔
3401

3402
   tree_t sub = find_enclosing(tab, S_SUBPROGRAM);
7,897✔
3403
   if (sub != NULL) {
7,897✔
3404
      if (!never_waits)
3,214✔
3405
         tree_clear_flag(sub, TREE_F_NEVER_WAITS);
164✔
3406

3407
      if (has_wait)
3,214✔
3408
         tree_set_flag(sub, TREE_F_HAS_WAIT);
27✔
3409

3410
      if (flags & TREE_F_IMPURE_FILE)
3,214✔
3411
         tree_set_flag(sub, TREE_F_IMPURE_FILE);
168✔
3412

3413
      if (flags & TREE_F_IMPURE_SHARED)
3,214✔
3414
         tree_set_flag(sub, TREE_F_IMPURE_SHARED);
6✔
3415

3416
      const bool in_func = tree_kind(sub) == T_FUNC_BODY;
3,214✔
3417
      const bool in_pure_func = in_func && !(tree_flags(sub) & TREE_F_IMPURE);
3,214✔
3418

3419
      if (has_wait && in_func)
3,214✔
3420
         sem_error(t, "function %s cannot call procedure %s which contains "
2✔
3421
                   "a wait statement", istr(tree_ident(sub)),
3422
                   istr(tree_ident(decl)));
3423
      else if ((flags & TREE_F_IMPURE_FILE) && in_pure_func) {
3,212✔
3424
         diag_t *d = pedantic_diag(tree_loc(t));
1✔
3425
         if (d != NULL) {
1✔
3426
            diag_printf(d, "pure function %s cannot call procedure %s which "
1✔
3427
                        "references a file object", istr(tree_ident(sub)),
3428
                        istr(tree_ident(decl)));
3429
            diag_emit(d);
1✔
3430
         }
3431
      }
3432
      else if ((flags & TREE_F_IMPURE_SHARED) && in_pure_func) {
3,211✔
3433
         diag_t *d = pedantic_diag(tree_loc(t));
4✔
3434
         if (d != NULL) {
4✔
3435
            diag_printf(d, "pure function %s cannot call procedure %s which "
4✔
3436
                        "references a shared variable", istr(tree_ident(sub)),
3437
                        istr(tree_ident(decl)));
3438
            diag_emit(d);
4✔
3439
         }
3440
      }
3441
   }
3442

3443
   if (!never_waits) {
7,895✔
3444
      // Procedure may wait, suppress infinite loop warning
3445
      tree_t proc = find_enclosing(tab, S_PROCESS);
868✔
3446
      if (proc != NULL)
868✔
3447
         tree_set_flag(proc, TREE_F_HAS_WAIT);
686✔
3448
   }
3449

3450
   return true;
3451
}
3452

3453
static bool sem_check_wait(tree_t t, nametab_t *tab)
9,461✔
3454
{
3455
   if (tree_has_delay(t)) {
9,461✔
3456
      type_t std_time = std_type(NULL, STD_TIME);
5,053✔
3457
      tree_t delay = tree_delay(t);
5,053✔
3458

3459
      if (!sem_check(delay, tab))
5,053✔
3460
         return false;
3461

3462
      if (!sem_check_type(delay, std_time, tab))
5,052✔
3463
         sem_error(delay, "type of delay must be %s but have %s",
2✔
3464
                   type_pp(std_time), type_pp(tree_type(delay)));
3465
   }
3466

3467
   if (tree_has_value(t)) {
9,458✔
3468
      type_t std_bool = std_type(NULL, STD_BOOLEAN);
274✔
3469
      tree_t value = tree_value(t);
274✔
3470

3471
      if (!sem_check(value, tab))
274✔
3472
         return false;
3473

3474
      if (!sem_check_type(value, std_bool, tab))
274✔
3475
         sem_error(value, "type of condition must be BOOLEAN but have %s",
1✔
3476
                   type_pp(tree_type(value)));
3477
   }
3478

3479
   if (find_enclosing(tab, S_PROTECTED))
9,457✔
3480
      sem_error(t, "wait statement not allowed in protected subprogram body");
1✔
3481

3482
   tree_t sub = find_enclosing(tab, S_SUBPROGRAM);
9,456✔
3483
   if (sub != NULL) {
9,456✔
3484
      if (tree_kind(sub) == T_FUNC_BODY)
401✔
3485
         sem_error(t, "wait statement not allowed in function body");
1✔
3486

3487
      tree_clear_flag(sub, TREE_F_NEVER_WAITS);
400✔
3488
      tree_set_flag(sub, TREE_F_HAS_WAIT);
400✔
3489
   }
3490

3491
   tree_t proc = find_enclosing(tab, S_PROCESS);
9,455✔
3492
   if (proc != NULL) {
9,455✔
3493
      // No wait statements allowed in process with sensitivity list
3494
      if (tree_triggers(proc) > 0)
9,088✔
3495
         sem_error(t, "wait statement not allowed in process with "
2✔
3496
                  "sensitvity list");
3497

3498
      tree_set_flag(proc, TREE_F_HAS_WAIT);
9,086✔
3499
   }
3500

3501
   return sem_check_sensitivity(t, tab);
9,453✔
3502
}
3503

3504
static bool sem_check_assert(tree_t t, nametab_t *tab)
17,568✔
3505
{
3506
   // Rules for asserion statements are in LRM 93 section 8.2
3507

3508
   tree_t value = tree_value(t);
17,568✔
3509
   if (!sem_check(value, tab))
17,568✔
3510
      return false;
3511

3512
   type_t std_bool = std_type(NULL, STD_BOOLEAN);
17,518✔
3513
   if (!sem_check_type(value, std_bool, tab))
17,518✔
3514
      sem_error(value, "type of assertion expression must be %s but "
1✔
3515
                "is %s", type_pp(std_bool), type_pp(tree_type(value)));
3516

3517
   if (tree_has_message(t)) {
17,517✔
3518
      tree_t message = tree_message(t);
6,386✔
3519
      if (!sem_check(message, tab))
6,386✔
3520
         return false;
3521

3522
      type_t std_string = std_type(NULL, STD_STRING);
6,386✔
3523
      if (!sem_check_type(message, std_string, tab))
6,386✔
3524
         sem_error(message, "type of message be %s but is %s",
×
3525
                   type_pp(std_string), type_pp(tree_type(message)));
3526
   }
3527

3528
   if (tree_has_severity(t)) {
17,517✔
3529
      tree_t severity = tree_severity(t);
5,986✔
3530
      if (!sem_check(severity, tab))
5,986✔
3531
         return false;
3532

3533
      type_t std_severity = std_type(NULL, STD_SEVERITY_LEVEL);
5,986✔
3534
      if (!sem_check_type(severity, std_severity, tab))
5,986✔
3535
         sem_error(severity, "type of severity must be %s but is %s",
×
3536
                   type_pp(std_severity), type_pp(tree_type(severity)));
3537
   }
3538

3539
   return true;
3540
}
3541

3542
static bool sem_check_report(tree_t t, nametab_t *tab)
2,021✔
3543
{
3544
   tree_t message = tree_message(t);
2,021✔
3545
   if (!sem_check(message, tab))
2,021✔
3546
      return false;
3547

3548
   type_t std_string = std_type(NULL, STD_STRING);
2,011✔
3549
   if (!sem_check_type(message, std_string, tab))
2,011✔
3550
      sem_error(message, "type of message be %s but is %s",
1✔
3551
                type_pp(std_string), type_pp(tree_type(message)));
3552

3553
   if (tree_has_severity(t)) {
2,010✔
3554
      tree_t severity = tree_severity(t);
439✔
3555
      if (!sem_check(severity, tab))
439✔
3556
         return false;
3557

3558
      type_t std_severity = std_type(NULL, STD_SEVERITY_LEVEL);
439✔
3559
      if (!sem_check_type(severity, std_severity, tab))
439✔
3560
         sem_error(severity, "type of severity must be %s but is %s",
1✔
3561
                   type_pp(std_severity), type_pp(tree_type(severity)));
3562
   }
3563

3564
   return true;
3565
}
3566

3567
static bool sem_check_string_literal(tree_t t)
25,281✔
3568
{
3569
   // String literals are in LRM 93 section 7.3.1
3570

3571
   type_t type = tree_type(t);
25,281✔
3572
   type_t elem = type_base_recur(type_elem(type));
25,281✔
3573

3574
   if (type_is_none(elem))
25,281✔
3575
      return false;
3576

3577
   const int nlits = type_enum_literals(elem);
25,281✔
3578
   const int nchars = tree_chars(t);
25,281✔
3579
   for (int i = 0; i < nchars; i++) {
377,214✔
3580
      tree_t ch = tree_char(t, i);
351,935✔
3581

3582
      ident_t ch_i = tree_ident(ch);
351,935✔
3583
      bool valid = false;
351,935✔
3584
      for (int j = 0; !valid && (j < nlits); j++) {
24,920,703✔
3585
         tree_t lit = type_enum_literal(elem, j);
24,568,768✔
3586
         if (ch_i == tree_ident(lit))
24,568,768✔
3587
            valid = true;
351,933✔
3588
      }
3589

3590
      if (!valid)
351,935✔
3591
         sem_error(t, "invalid character %s in string literal of type %s",
351,935✔
3592
                   istr(ch_i), type_pp(type));
3593
   }
3594

3595
   return true;
3596
}
3597

3598
static bool sem_check_literal(tree_t t)
91,433✔
3599
{
3600
   type_t type = tree_type(t);
91,433✔
3601
   if (type_is_none(type))
91,433✔
3602
      return false;
3✔
3603

3604
   return true;
3605
}
3606

3607
static bool sem_check_array_aggregate(tree_t t, nametab_t *tab)
6,482✔
3608
{
3609
   type_t composite_type = tree_type(t);
6,482✔
3610
   type_t base_type = type_base_recur(composite_type);
6,482✔
3611

3612
   const bool unconstrained = type_is_unconstrained(composite_type);
6,482✔
3613

3614
   type_t elem_type = NULL;
6,482✔
3615
   const int ndims = dimension_of(composite_type);
6,482✔
3616
   if (ndims == 1)
6,482✔
3617
      elem_type = type_elem(base_type);
6,190✔
3618
   else {
3619
      // Higher dimensions must be specified with a sub-aggregate or
3620
      // string literal
3621
      tree_t a0 = tree_value(tree_assoc(t, 0));
292✔
3622
      const tree_kind_t a0_kind = tree_kind(a0);
292✔
3623
      if (a0_kind != T_AGGREGATE && a0_kind != T_STRING)
292✔
3624
         sem_error(a0, "second dimension of %d dimensional array type %s must "
1✔
3625
                   "be specified by a sub-aggregate, string, or bit-string "
3626
                   "literal", ndims, type_pp(composite_type));
3627

3628
      // The parser will have constructed a type with ndims - 1
3629
      // dimensions.
3630
      elem_type = tree_type(tree_value(tree_assoc(t, 0)));
291✔
3631

3632
      if (!type_is_unconstrained(elem_type)) {
291✔
3633
         if (!sem_check_array_dims(elem_type, NULL, tab))
284✔
3634
            return false;
3635
      }
3636
   }
3637

3638
   type_t index_type = index_type_of(composite_type, 0);
6,481✔
3639

3640
   bool have_named = false;
6,481✔
3641
   bool have_pos = false;
6,481✔
3642

3643
   const int nassocs = tree_assocs(t);
6,481✔
3644
   for (int i = 0; i < nassocs; i++) {
38,189✔
3645
      tree_t a = tree_assoc(t, i);
31,721✔
3646

3647
      const assoc_kind_t akind = tree_subkind(a);
31,721✔
3648
      switch (akind) {
31,721✔
3649
      case A_RANGE:
635✔
3650
      case A_SLICE:
3651
         {
3652
            tree_t r = tree_range(a, 0);
635✔
3653
            if (!sem_check_discrete_range(r, index_type, tab))
635✔
3654
               return false;
3655

3656
            have_named = true;
3657
         }
3658
         break;
3659

3660
      case A_NAMED:
1,330✔
3661
         {
3662
            tree_t name = tree_name(a);
1,330✔
3663

3664
            if (!sem_check(name, tab))
1,330✔
3665
               return false;
3666

3667
            if (!sem_check_type(name, index_type, tab))
1,330✔
3668
               sem_error(name, "type of array aggregate choice %s does not "
1✔
3669
                         "match %s index type %s", type_pp(tree_type(name)),
3670
                         type_pp(composite_type), type_pp(index_type));
3671

3672
            have_named = true;
3673
         }
3674
         break;
3675

3676
      case A_POS:
27,308✔
3677
      case A_CONCAT:
3678
         have_pos = true;
27,308✔
3679
         break;
27,308✔
3680

3681
      case A_OTHERS:
2,448✔
3682
         if (unconstrained)
2,448✔
3683
            sem_error(a, "index range of array aggregate with others choice "
3✔
3684
                      "cannot be determined from the context");
3685
         break;
3686
      }
3687

3688
      tree_t value = tree_value(a);
31,714✔
3689

3690
      if (!sem_check(value, tab))
31,714✔
3691
         return false;
3692

3693
      if (!sem_check_type(value, elem_type, tab)) {
31,711✔
3694
         // LRM 08 section 9.3.3.3 allows the association to be of the
3695
         // base aggregate type as well
3696
         const bool allow_slice =
484✔
3697
            (akind == A_CONCAT || akind == A_SLICE)
242✔
3698
            || (ndims == 1 && standard() >= STD_08
242✔
3699
                && (akind == A_POS || akind == A_RANGE));
3✔
3700

3701
         if (allow_slice && !sem_check_type(value, composite_type, tab))
242✔
3702
            sem_error(value, "type of %s association %s does not match "
1✔
3703
                      "aggregate element type %s or the aggregate type "
3704
                      "itself %s", assoc_kind_str(akind),
3705
                      type_pp(tree_type(value)), type_pp(elem_type),
3706
                      type_pp(composite_type));
3707
         else if (!allow_slice)
241✔
3708
            sem_error(value, "type of %s association %s does not match "
2✔
3709
                      "aggregate element type %s", assoc_kind_str(akind),
3710
                      type_pp(tree_type(value)), type_pp(elem_type));
3711
         else
3712
            assert(akind == A_CONCAT || akind == A_SLICE);
239✔
3713
      }
3714
   }
3715

3716
   // Named and positional associations cannot be mixed in array
3717
   // aggregates
3718

3719
   if (have_named && have_pos)
6,468✔
3720
      sem_error(t, "named and positional associations cannot be "
2✔
3721
                "mixed in array aggregates");
3722

3723
   // If a choice is not locally static then it must be the only element
3724

3725
   if (have_named && nassocs > 1) {
6,466✔
3726
      for (int i = 0; i < nassocs; i++) {
1,951✔
3727
         tree_t a = tree_assoc(t, i);
1,527✔
3728
         tree_t choice = NULL;
1,527✔
3729
         switch (tree_subkind(a)) {
1,527✔
3730
         case A_NAMED: choice = tree_name(a); break;
1,186✔
3731
         case A_SLICE:
168✔
3732
         case A_RANGE: choice = tree_range(a, 0); break;
168✔
3733
         }
3734

3735
         if (choice && !sem_locally_static(choice))
1,354✔
3736
            sem_error(choice, "a choice that is not locally static is allowed"
1,527✔
3737
                      " only if the array aggregate contains a single element"
3738
                      " association");
3739
      }
3740
   }
3741

3742
   return true;
3743
}
3744

3745
static bool sem_check_record_aggregate(tree_t t, nametab_t *tab)
2,429✔
3746
{
3747
   // Checks for record aggregates are given in LRM 93 section 7.3.2.1
3748

3749
   type_t composite_type = tree_type(t);
2,429✔
3750
   type_t base_type = type_base_recur(composite_type);
2,429✔
3751

3752
   const int nfields = type_fields(base_type);
2,429✔
3753
   int pos = 0;
2,429✔
3754

3755
   LOCAL_BIT_MASK have;
4,858✔
3756
   mask_init(&have, nfields);
2,429✔
3757

3758
   const int nassocs = tree_assocs(t);
2,429✔
3759
   for (int i = 0; i < nassocs; i++) {
8,336✔
3760
      tree_t a = tree_assoc(t, i);
5,922✔
3761
      int f = -1;
5,922✔
3762

3763
      switch (tree_subkind(a)) {
5,922✔
3764
      case A_NAMED:
1,037✔
3765
         {
3766
            tree_t name = tree_name(a);
1,037✔
3767
            if (tree_kind(name) != T_REF)
1,037✔
3768
               sem_error(name, "association choice must be a field name");
3✔
3769
            else if (!tree_has_ref(name))
1,034✔
3770
               return false;   // Was parse error
3771

3772
            tree_t fdecl = tree_ref(name);
1,032✔
3773
            if (tree_kind(fdecl) != T_FIELD_DECL)
1,032✔
3774
               return false;   // Was parse error
3775

3776
            f = tree_pos(fdecl);
1,030✔
3777
         }
3778
         break;
1,030✔
3779

3780
      case A_POS:
4,774✔
3781
         {
3782
            if (pos >= nfields)
4,774✔
3783
               sem_error(a, "%d positional associations given but record type"
1✔
3784
                         " %s only has %d fields", pos + 1,
3785
                         type_pp(composite_type), nfields);
3786

3787
            f = pos++;
4,773✔
3788
         }
3789
         break;
4,773✔
3790

3791
      case A_OTHERS:
3792
         f = -1;
3793
         break;
3794

3795
      case A_RANGE:
2✔
3796
         {
3797
            diag_t *d = diag_new(DIAG_ERROR, tree_loc(tree_range(a, 0)));
2✔
3798
            diag_printf(d, "an element association with a choice that is "
2✔
3799
                        "a discrete range is only allowed in an array "
3800
                        "aggregate");
3801
            diag_lrm(d, STD_08, "9.3.3");
2✔
3802
            diag_emit(d);
2✔
3803
            return false;
2✔
3804
         }
3805

3806
      case A_SLICE:
×
3807
      case A_CONCAT:
3808
         should_not_reach_here();
3809
      }
3810

3811
      int nmatched = 0;
5,912✔
3812
      for (int j = 0; j < nfields; j++) {
27,541✔
3813
         if ((f != -1) && (f != j))
21,633✔
3814
            continue;
15,476✔
3815

3816
         tree_t field = type_field(base_type, j);
6,157✔
3817
         type_t field_type = tree_type(field);
6,157✔
3818

3819
         if (mask_test(&have, j)) {
6,157✔
3820
            if (f == -1)
42✔
3821
               continue;
40✔
3822

3823
            tree_t ak = NULL;
3824
            for (int k = 0; k < i; k++) {
2✔
3825
               ak = tree_assoc(t, k);
2✔
3826
               if (tree_subkind(ak) == A_POS && tree_pos(ak) == j)
2✔
3827
                  break;
3828
               else if (tree_pos(tree_ref(tree_name(ak))) == j)
1✔
3829
                  break;
3830
            }
3831
            assert(ak != NULL);
2✔
3832

3833
            diag_t *d = diag_new(DIAG_ERROR, tree_loc(a));
2✔
3834
            diag_printf(d, "field %s was already given a value by earlier "
2✔
3835
                        "%s choice", istr(tree_ident(field)),
3836
                        assoc_kind_str(tree_subkind(ak)));
2✔
3837
            diag_hint(d, tree_loc(ak), "first choice associated with field %s",
2✔
3838
                      istr(tree_ident(field)));
3839
            diag_hint(d, tree_loc(a), "duplicate choice here");
2✔
3840
            diag_emit(d);
2✔
3841
            return false;
2✔
3842
         }
3843

3844
         tree_t value = tree_value(a);
6,115✔
3845

3846
         if (!sem_check(value, tab))
6,115✔
3847
            return false;
3848

3849
         if (!sem_check_type(value, field_type, tab))
6,115✔
3850
            sem_error(value, "type of value %s does not match type %s"
2✔
3851
                      " of field %s",
3852
                      type_pp2(tree_type(value), field_type),
3853
                      type_pp2(field_type, tree_type(value)),
3854
                      istr(tree_ident(field)));
3855

3856
         mask_set(&have, j);
6,113✔
3857
         nmatched++;
6,113✔
3858
      }
3859

3860
      if (f == -1 && nmatched == 0)
5,908✔
3861
         sem_error(a, "others association must represent at least one element");
5,908✔
3862
   }
3863

3864
   for (int i = 0; i < nfields; i++) {
8,517✔
3865
      if (!mask_test(&have, i)) {
6,105✔
3866
         tree_t field = type_field(base_type, i);
2✔
3867
         sem_error(t, "field %s does not have a value",
6,105✔
3868
                   istr(tree_ident(field)));
3869
      }
3870
   }
3871

3872
   return true;
3873
}
3874

3875
static bool sem_check_aggregate(tree_t t, nametab_t *tab)
8,920✔
3876
{
3877
   // Rules for aggregates are in LRM 93 section 7.3.2
3878

3879
   type_t composite_type = tree_type(t);
8,920✔
3880

3881
   if (type_is_none(composite_type))
8,920✔
3882
      return false;
3883
   assert(type_is_composite(composite_type));
8,914✔
3884

3885
   // All positional associations must appear before named associations
3886
   // and those must appear before any others association
3887

3888
   enum { POS, NAMED, OTHERS } state = POS;
8,914✔
3889

3890
   const int nassocs = tree_assocs(t);
8,914✔
3891
   for (int i = 0; i < nassocs; i++) {
46,568✔
3892
      tree_t a = tree_assoc(t, i);
37,657✔
3893

3894
      switch (tree_subkind(a)) {
37,657✔
3895
      case A_POS:
32,085✔
3896
      case A_CONCAT:
3897
         if (state > POS)
32,085✔
3898
            sem_error(a, "positional associations must appear "
1✔
3899
                      "first in aggregate");
3900
         break;
3901

3902
      case A_NAMED:
3,011✔
3903
      case A_RANGE:
3904
      case A_SLICE:
3905
         if (state > NAMED)
3,011✔
3906
            sem_error(a, "named association must not follow "
1✔
3907
                      "others association in aggregate");
3908
         state = NAMED;
3909
         break;
3910

3911
      case A_OTHERS:
2,561✔
3912
         if (state == OTHERS)
2,561✔
3913
            sem_error(a, "only a single others association "
1✔
3914
                      "allowed in aggregate");
3915
         state = OTHERS;
3916
         break;
3917
      }
3918
   }
3919

3920
   if (type_is_array(composite_type))
8,911✔
3921
      return sem_check_array_aggregate(t, tab);
6,482✔
3922
   else
3923
      return sem_check_record_aggregate(t, tab);
2,429✔
3924
}
3925

3926
static bool sem_check_ref(tree_t t, nametab_t *tab)
174,986✔
3927
{
3928
   if (!tree_has_ref(t))
174,986✔
3929
      return false;
3930

3931
   type_t type = get_type_or_null(t);
174,939✔
3932
   if (type != NULL && type_is_none(type))
174,939✔
3933
      return false;
3934

3935
   tree_t decl = tree_ref(t);
174,931✔
3936
   const tree_kind_t kind = tree_kind(decl);
174,931✔
3937

3938
   switch (kind) {
174,931✔
3939
   case T_PORT_DECL:
3940
   case T_VAR_DECL:
3941
   case T_SIGNAL_DECL:
3942
   case T_FILE_DECL:
3943
   case T_ENUM_LIT:
3944
   case T_UNIT_DECL:
3945
   case T_FUNC_DECL:
3946
   case T_FUNC_BODY:
3947
   case T_FUNC_INST:
3948
   case T_PROC_DECL:
3949
   case T_PROC_BODY:
3950
   case T_PROC_INST:
3951
   case T_IMPLICIT_SIGNAL:
3952
   case T_PARAM_DECL:
3953
      break;
3954

3955
   case T_CONST_DECL:
19,978✔
3956
      if (!tree_has_value(decl) && is_same_region(tab, decl)) {
19,978✔
3957
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
1✔
3958
         diag_printf(d, "cannot reference deferred constant %s before the "
1✔
3959
                     "elaboration of the corresponding full declaration",
3960
                     istr(tree_ident(decl)));
3961
         diag_hint(d, tree_loc(decl), "%s declared here",
1✔
3962
                   istr(tree_ident(decl)));
3963
         diag_lrm(d, STD_08, "4.8");
1✔
3964
         diag_emit(d);
1✔
3965
         return false;
1✔
3966
      }
3967
      break;
3968

3969
   case T_ALIAS:
1,727✔
3970
      {
3971
         switch (class_of(decl)) {
1,727✔
3972
         case C_VARIABLE:
3973
         case C_SIGNAL:
3974
         case C_CONSTANT:
3975
         case C_LITERAL:
3976
            break;
3977

3978
         case C_DEFAULT:
3979
            return false;   // Must have been an earlier parse error
3980

3981
         default:
1✔
3982
            sem_error(t, "invalid use of alias %s", istr(tree_ident(decl)));
1✔
3983
         }
3984
      }
3985
      break;
3986

3987
   case T_GENERIC_DECL:
5,997✔
3988
      if (tree_class(decl) == C_CONSTANT)
5,997✔
3989
         break;
3990
      // Fall-through
3991

3992
   default:
3993
      {
3994
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
4✔
3995
         diag_printf(d, "invalid use of %s %s", class_str(class_of(decl)),
4✔
3996
                     istr(tree_ident(t)));
3997
         diag_hint(d, tree_loc(decl), "%s declared here",
4✔
3998
                   istr(tree_ident(decl)));
3999
         diag_emit(d);
4✔
4000
         return false;
4✔
4001
      }
4002
   }
4003

4004
   tree_t sub = find_enclosing(tab, S_SUBPROGRAM);
174,925✔
4005
   if (sub != NULL) {
174,925✔
4006
      if (kind == T_FILE_DECL)
93,552✔
4007
         tree_set_flag(sub, TREE_F_IMPURE_FILE);
289✔
4008
      else if (kind == T_VAR_DECL && (tree_flags(decl) & TREE_F_SHARED))
93,263✔
4009
         tree_set_flag(sub, TREE_F_IMPURE_SHARED);
64✔
4010

4011
      const bool is_pure_func =
187,104✔
4012
         tree_kind(sub) == T_FUNC_BODY && !(tree_flags(sub) & TREE_F_IMPURE);
93,552✔
4013

4014
      if (is_pure_func) {
93,552✔
4015
         const class_t class = class_of(decl);
56,970✔
4016
         if (class == C_VARIABLE || class == C_SIGNAL || class == C_FILE) {
56,970✔
4017
            tree_t container = get_container(tab, decl);
15,990✔
4018
            if (container != NULL && container != sub) {
15,990✔
4019
               diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
2✔
4020
               diag_printf(d, "cannot reference %s %s in pure function %s",
2✔
4021
                           class_str(class), istr(tree_ident(decl)),
4022
                           istr(tree_ident(sub)));
4023
               diag_lrm(d, STD_08, "4.3");
2✔
4024
               diag_emit(d);
2✔
4025
               return false;
2✔
4026
            }
4027
         }
4028
      }
4029
   }
4030

4031
   return true;
4032
}
4033

4034
static bool sem_check_name_prefix(tree_t t, nametab_t *tab, const char *what)
27,958✔
4035
{
4036
   // The prefix of a name may only be function call or another name
4037
   switch (tree_kind(t)) {
27,958✔
4038
   case T_FCALL:
4039
   case T_PROT_FCALL:
4040
   case T_REF:
4041
   case T_ATTR_REF:
4042
   case T_ALL:
4043
   case T_ARRAY_REF:
4044
   case T_ARRAY_SLICE:
4045
   case T_RECORD_REF:
4046
   case T_EXTERNAL_NAME:
4047
      break;
4048

4049
   default:
2✔
4050
      {
4051
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
2✔
4052
         diag_printf(d, "the prefix of %s must be a name or a "
2✔
4053
                     "function call", what);
4054
         diag_lrm(d, STD_08, "8.1");
2✔
4055
         diag_emit(d);
2✔
4056
         return false;
2✔
4057
      }
4058
   }
4059

4060
   return true;
4061
}
4062

4063
static bool sem_check_record_ref(tree_t t, nametab_t *tab)
7,734✔
4064
{
4065
   tree_t value = tree_value(t);
7,734✔
4066
   if (!sem_check(value, tab))
7,734✔
4067
      return false;
4068

4069
   if (!tree_has_ref(t))
7,727✔
4070
      return false;
4071

4072
   if (!sem_check_name_prefix(value, tab, "a selected name"))
7,722✔
4073
      return false;
×
4074

4075
   return true;
4076
}
4077

4078
static bool sem_check_array_ref(tree_t t, nametab_t *tab)
14,554✔
4079
{
4080
   tree_t value = tree_value(t);
14,554✔
4081
   if (!sem_check(value, tab))
14,554✔
4082
      return false;
4083

4084
   if (!sem_check_name_prefix(value, tab, "an indexed name"))
14,552✔
4085
      return false;
4086

4087
   type_t type = tree_type(value);
14,551✔
4088

4089
   if (!type_is_array(type))
14,551✔
4090
      return false;  // Checked earlier
4091

4092
   const int nindex  = dimension_of(type);
14,547✔
4093
   const int nparams = tree_params(t);
14,547✔
4094

4095
   if (nparams != nindex)
14,547✔
4096
      sem_error(t, "prefix of indexed name has %d dimensions but %d "
3✔
4097
                "indices given", nindex, nparams);
4098

4099
   bool ok = true;
4100
   for (int i = 0; i < nparams; i++) {
30,724✔
4101
      tree_t p = tree_param(t, i);
16,182✔
4102
      assert(tree_subkind(p) == P_POS);
16,182✔
4103

4104
      type_t expect = index_type_of(type, i);
16,182✔
4105
      tree_t value = tree_value(p);
16,182✔
4106

4107
      ok = sem_check(value, tab) && ok;
16,182✔
4108

4109
      if (ok && !sem_check_type(value, expect, tab))
16,182✔
4110
         sem_error(value, "type of index %s does not match type of "
16,182✔
4111
                   "array dimension %s",
4112
                   type_pp(tree_type(value)),
4113
                   type_pp(expect));
4114
   }
4115

4116
   return ok;
4117
}
4118

4119
static bool sem_check_array_slice(tree_t t, nametab_t *tab)
2,191✔
4120
{
4121
   tree_t value = tree_value(t);
2,191✔
4122
   if (!sem_check(value, tab))
2,191✔
4123
      return false;
4124

4125
   if (!sem_check_name_prefix(value, tab, "a slice name"))
2,190✔
4126
      return false;
4127

4128
   type_t array_type = tree_type(value);
2,189✔
4129

4130
   if (type_is_none(array_type))
2,189✔
4131
      return false;
4132
   else if (!sem_check_incomplete(t, array_type))
2,189✔
4133
      return false;
4134
   else if (!type_is_array(array_type))
2,188✔
4135
      sem_error(t, "type of slice prefix %s is not an array",
2✔
4136
                type_pp(array_type));
4137

4138
   tree_t r = tree_range(t, 0);
2,186✔
4139
   if (!sem_check_discrete_range(r, index_type_of(array_type, 0), tab))
2,186✔
4140
      return false;
4141

4142
   const bool unconstrained = type_is_unconstrained(array_type);
2,176✔
4143
   const range_kind_t prefix_dir =
4,352✔
4144
      unconstrained ? RANGE_EXPR : direction_of(array_type, 0);
2,176✔
4145

4146
   const range_kind_t rkind = tree_subkind(r);
2,176✔
4147
   const bool wrong_dir =
4,352✔
4148
      !unconstrained
2,176✔
4149
      && rkind != prefix_dir
2,176✔
4150
      && (rkind == RANGE_TO || rkind == RANGE_DOWNTO)
130✔
4151
      && (prefix_dir == RANGE_TO || prefix_dir == RANGE_DOWNTO);
2,176✔
4152

4153
   if (wrong_dir) {
2,176✔
4154
      const char *text[] = { "TO", "DOWNTO", "?", "??", "???" };
2✔
4155
      sem_error(t, "range direction of slice %s does not match prefix %s",
2✔
4156
                text[rkind], text[prefix_dir]);
4157
   }
4158

4159
   return true;
4160
}
4161

4162
static bool sem_check_signal_attr(tree_t t)
1,017✔
4163
{
4164
   tree_t name = tree_name(t);
1,045✔
4165

4166
   if (tree_kind(name) == T_ATTR_REF)
1,045✔
4167
      return sem_check_signal_attr(name);
4168

4169
   tree_t ref = name_to_ref(name);
1,017✔
4170
   if (ref != NULL && class_of(ref) == C_SIGNAL)
1,017✔
4171
      return true;
4172

4173
   sem_error(t, "prefix of attribute %s must denote a signal",
2✔
4174
             istr(tree_ident(t)));
4175
}
4176

4177
static bool sem_check_driving(tree_t t)
69✔
4178
{
4179
   // See LRM 08 section 16.2.4 for special rules about 'DRIVING and
4180
   // 'DRIVING_VALUE
4181

4182
   if (!sem_check_signal_attr(t))
69✔
4183
      return false;
4184

4185
   tree_t ref = name_to_ref(tree_name(t));
68✔
4186
   if (ref == NULL || !tree_has_ref(ref))
68✔
4187
      return false;
×
4188

4189
   tree_t decl = tree_ref(ref);
68✔
4190
   if (tree_kind(decl) == T_PORT_DECL) {
68✔
4191
      const port_mode_t mode = tree_subkind(decl);
13✔
4192
      if (mode != PORT_OUT && mode != PORT_INOUT && mode != PORT_BUFFER)
13✔
4193
         sem_error(t, "prefix of attribute %s must denote a signal or a port "
1✔
4194
                   "with mode IN, INOUT, or BUFFER", istr(tree_ident(t)));
4195
   }
4196

4197
   // TODO: check within a process
4198

4199
   return true;
4200
}
4201

4202
static bool sem_check_attr_param(tree_t t, type_t expect, int min, int max,
1,452✔
4203
                                 nametab_t *tab)
4204
{
4205
   const int nparams = tree_params(t);
1,452✔
4206
   if (nparams == 0 && min > 0)
1,452✔
4207
      sem_error(t, "attribute %s requires a parameter", istr(tree_ident(t)));
×
4208
   else if (nparams > max)
1,452✔
4209
      sem_error(t, "too many parameters for attribute %s", istr(tree_ident(t)));
×
4210
   else if (nparams == 1) {
1,452✔
4211
      tree_t dim = tree_value(tree_param(t, 0));
1,357✔
4212
      if (!sem_check(dim, tab))
1,357✔
4213
         return false;
4214

4215
      tree_t value = tree_value(tree_param(t, 0));
1,353✔
4216
      if (!sem_check_type(value, expect, tab))
1,353✔
4217
         sem_error(t, "expected type %s for attribute %s parameter but "
×
4218
                   "have %s", type_pp(expect), istr(tree_ident(t)),
4219
                   type_pp(tree_type(value)));
4220
   }
4221

4222
   return true;
4223
}
4224

4225
static bool sem_check_dimension_attr(tree_t t, nametab_t *tab)
22,125✔
4226
{
4227
   const int nparams = tree_params(t);
22,125✔
4228
   if (nparams == 0)
22,125✔
4229
      return true;
4230

4231
   assert(nparams == 1);   // Enforced by parser
1,648✔
4232

4233
   tree_t dim = tree_value(tree_param(t, 0));
1,648✔
4234
   if (!sem_check(dim, tab))
1,648✔
4235
      return false;
4236

4237
   // The parameter must be a locally static expression of type
4238
   // universal_integer
4239

4240
   type_t uint = std_type(NULL, STD_UNIVERSAL_INTEGER);
1,647✔
4241
   type_t dimtype = tree_type(dim);
1,647✔
4242
   if (!type_eq(dimtype, uint)) {
1,647✔
4243
      diag_t *d;
4✔
4244
      if (type_is_integer(dimtype))
4✔
4245
         d = pedantic_diag(tree_loc(dim));
4✔
4246
      else
4247
         d = diag_new(DIAG_ERROR, tree_loc(dim));
×
4248

4249
      if (d != NULL) {
4✔
4250
         diag_printf(d, "dimension parameter of attribute %s must be a locally "
4✔
4251
                     "static expression of type universal_integer",
4252
                     istr(tree_ident(t)));
4253
         diag_hint(d, tree_loc(dim), "expression has type %s",
4✔
4254
                   type_pp(tree_type(dim)));
4255
         diag_emit(d);
4✔
4256
         return false;
4✔
4257
      }
4258
   }
4259

4260
   if (!sem_locally_static(dim))
1,643✔
4261
      sem_error(dim, "dimension parameter of attribute %s must be a locally "
×
4262
                "static expression", istr(tree_ident(t)));
4263

4264
   if (!type_is_array(tree_type(tree_name(t))))
1,643✔
4265
      sem_error(t, "prefix of attribute %s with dimension is not an array",
×
4266
                istr(tree_ident(t)));
4267

4268
   return true;
4269
}
4270

4271
static bool sem_is_named_entity(tree_t t)
787✔
4272
{
4273
   const tree_kind_t kind = tree_kind(t);
787✔
4274
   if (kind != T_REF)
787✔
4275
      return false;
4276

4277
   tree_t decl = tree_ref(t);
787✔
4278

4279
   switch (tree_kind(decl)) {
787✔
4280
   case T_SIGNAL_DECL:  case T_VAR_DECL:       case T_PORT_DECL:
4281
   case T_ALIAS:        case T_ENTITY:         case T_ARCH:
4282
   case T_PACKAGE:      case T_PACK_BODY:      case T_BLOCK:
4283
   case T_FILE_DECL:    case T_CONST_DECL:     case T_FUNC_DECL:
4284
   case T_FUNC_BODY:    case T_PROC_DECL:      case T_PROC_BODY:
4285
   case T_PROCESS:      case T_GENERIC_DECL:   case T_PARAM_DECL:
4286
   case T_INSTANCE:     case T_PROT_DECL:      case T_PROT_BODY:
4287
   case T_TYPE_DECL:    case T_SUBTYPE_DECL:   case T_FOR_GENERATE:
4288
   case T_IF_GENERATE:  case T_CASE_GENERATE:
4289
      return true;
4290
   case T_IMPLICIT_SIGNAL:
×
4291
      return tree_subkind(decl) == IMPLICIT_GUARD;   // See LRM 93 section 4.3
×
4292
   default:
1✔
4293
      return false;
1✔
4294
   }
4295
}
4296

4297
static bool sem_check_attr_prefix(tree_t t, bool allow_range, nametab_t *tab,
26,905✔
4298
                                  type_t *named_type)
4299
{
4300
   switch (tree_kind(t)) {
26,905✔
4301
   case T_REF:
25,100✔
4302
      {
4303
         if (!tree_has_ref(t))
25,100✔
4304
            return false;
4305

4306
         tree_t decl = tree_ref(t), type_decl;
25,098✔
4307
         if ((type_decl = aliased_type_decl(decl)) != NULL)
25,098✔
4308
            *named_type = tree_type(type_decl);
4,826✔
4309

4310
         return true;
4311
      }
4312

4313
   case T_ATTR_REF:
93✔
4314
      {
4315
         if (is_type_attribute(tree_subkind(t)))
93✔
4316
            *named_type = tree_type(t);
41✔
4317
         else {
4318
            if (!sem_check_attr_ref(t, allow_range, tab))
52✔
4319
               return false;
×
4320
         }
4321

4322
         return true;
4323
      }
4324

4325
   case T_RECORD_REF:
1,145✔
4326
      {
4327
         if (!tree_has_ref(t))
1,145✔
4328
            return false;
4329

4330
         tree_t value = tree_value(t);
1,145✔
4331
         if (!sem_check_attr_prefix(value, false, tab, named_type))
1,145✔
4332
            return false;
4333

4334
         if (!sem_check_name_prefix(t, tab, "a selected name"))
1,145✔
4335
            return false;
4336

4337
         if (*named_type != NULL)
1,145✔
4338
            *named_type = tree_type(tree_ref(t));
3✔
4339

4340
         return true;
4341
      }
4342

4343
   default:
567✔
4344
      return sem_check(t, tab);
567✔
4345
   }
4346
}
4347

4348
static bool sem_check_attr_ref(tree_t t, bool allow_range, nametab_t *tab)
25,760✔
4349
{
4350
   // Attribute names are in LRM 93 section 6.6
4351

4352
   tree_t name = tree_name(t);
25,760✔
4353
   type_t named_type = NULL;
25,760✔
4354

4355
   ident_t attr = tree_ident(t);
25,760✔
4356
   const attr_kind_t predef = tree_subkind(t);
25,760✔
4357

4358
   const bool prefix_can_be_range =
25,760✔
4359
      predef == ATTR_LOW || predef == ATTR_HIGH || predef == ATTR_LEFT
4360
      || predef == ATTR_RIGHT || predef == ATTR_ASCENDING;
25,760✔
4361

4362
   if (!sem_check_attr_prefix(name, prefix_can_be_range, tab, &named_type))
25,760✔
4363
      return false;
4364

4365
   if (predef == ATTR_INSTANCE_NAME)
25,756✔
4366
      tree_set_global_flags(t, TREE_GF_INSTANCE_NAME);
499✔
4367
   else if (predef == ATTR_PATH_NAME)
25,257✔
4368
      tree_set_global_flags(t, TREE_GF_PATH_NAME);
220✔
4369

4370
   switch (predef) {
25,756✔
4371
   case ATTR_RANGE:
4,751✔
4372
   case ATTR_REVERSE_RANGE:
4373
      {
4374
         type_t type = tree_type(name);
4,751✔
4375
         if (type_is_none(type))
4,751✔
4376
            return false;
4377
         else if (!allow_range)
4,751✔
4378
            sem_error(t, "range expression not allowed here");
×
4379

4380
         if (!sem_check_dimension_attr(t, tab))
4,751✔
4381
            return false;
4382

4383
         if (named_type != NULL) {
4,751✔
4384
            // Range attribute of type
4385
            if (named_type != NULL && type_is_unconstrained(type))
1,202✔
4386
               sem_error(t, "cannot use attribute %s with unconstrained array "
1✔
4387
                         "type %s", istr(attr), type_pp(type));
4388
         }
4389
         else if (!type_is_array(type)) {
3,549✔
4390
            diag_t *d = diag_new(DIAG_ERROR, tree_loc(name));
3✔
4391
            diag_printf(d, "object prefix of attribute %s must be an array",
3✔
4392
                        istr(attr));
4393
            diag_hint(d, tree_loc(name), "prefix has type %s", type_pp(type));
3✔
4394
            diag_emit(d);
3✔
4395
            return false;
3✔
4396
         }
4397

4398
         return true;
4399
      }
4400

4401
   case ATTR_LENGTH:
7,701✔
4402
      {
4403
         type_t type = get_type_or_null(name);
7,701✔
4404
         if (type == NULL)
7,701✔
4405
            sem_error(name, "prefix does not have LENGTH attribute");
1✔
4406
         else if (type_is_none(type))
7,700✔
4407
            return false;
4408
         else if (!sem_check_incomplete(t, type))
7,700✔
4409
            return false;
4410
         else if (!type_is_array(type)
7,699✔
4411
                  && !(standard() >= STD_19 && type_is_discrete(type)))
11✔
4412
            sem_error(name, "prefix of attribute LENGTH must be an array%s "
1✔
4413
                      "but have type %s",
4414
                      standard() >= STD_19 ? " or a discrete type" : "",
4415
                      type_pp(type));
4416

4417
         if (!sem_check_dimension_attr(t, tab))
7,698✔
4418
            return false;
3✔
4419

4420
         return true;
4421
      }
4422

4423
   case ATTR_LEFT:
9,677✔
4424
   case ATTR_RIGHT:
4425
   case ATTR_LOW:
4426
   case ATTR_HIGH:
4427
   case ATTR_ASCENDING:
4428
      {
4429
         type_t type = tree_type(name);
9,677✔
4430

4431
         if (type_is_none(type))
9,677✔
4432
            return false;
4433
         else if (!sem_check_incomplete(t, type))
9,677✔
4434
            return false;
4435
         else if (!sem_check_dimension_attr(t, tab))
9,676✔
4436
            return false;
4437

4438
         if (!type_is_array(type) && !type_is_scalar(type))
9,674✔
4439
            sem_error(t, "prefix does not have attribute %s", istr(attr));
1✔
4440

4441
         return true;
4442
      }
4443

4444
   case ATTR_LAST_EVENT:
97✔
4445
   case ATTR_LAST_ACTIVE:
4446
      if (!sem_check_readable(name))
97✔
4447
         return false;
4448
      else if (!sem_check_attr_param(t, NULL, 0, 0, tab))
95✔
4449
         return false;
4450
      else if (!sem_check_signal_attr(t))
95✔
4451
         return false;
1✔
4452

4453
      return true;
4454

4455
   case ATTR_EVENT:
588✔
4456
   case ATTR_ACTIVE:
4457
   case ATTR_LAST_VALUE:
4458
      if (!sem_check_readable(name))
588✔
4459
         return false;
4460
      else if (!sem_check_signal_attr(t))
586✔
4461
         return false;
×
4462

4463
      return true;
4464

4465
   case ATTR_PATH_NAME:
787✔
4466
   case ATTR_INSTANCE_NAME:
4467
   case ATTR_SIMPLE_NAME:
4468
      if (!sem_is_named_entity(name))
787✔
4469
         sem_error(t, "prefix of %s attribute must be a named entity",
1✔
4470
                   istr(attr));
4471

4472
      tree_set_flag(name, TREE_F_ATTR_PREFIX);
786✔
4473
      return true;
786✔
4474

4475
   case ATTR_QUIET:
237✔
4476
   case ATTR_STABLE:
4477
   case ATTR_DELAYED:
4478
      {
4479
         if (!sem_check_readable(name))
237✔
4480
            return false;
4481
         else if (!sem_check_signal_attr(t))
235✔
4482
            return false;
4483

4484
         if (tree_params(t) > 0) {
235✔
4485
            tree_t value = tree_value(tree_param(t, 0));
85✔
4486

4487
            if (!sem_check(value, tab))
85✔
4488
               return false;
4489

4490
            type_t std_time = std_type(NULL, STD_TIME);
85✔
4491
            if (!sem_check_type(value, std_time, tab))
85✔
4492
               sem_error(value, "parameter of attribute %s must have type %s",
1✔
4493
                         istr(attr), type_pp(std_time));
4494
            else if (!sem_globally_static(value))
84✔
4495
               sem_error(value, "parameter of attribute %s must be a static "
1✔
4496
                         "expression", istr(attr));
4497
         }
4498

4499
         return true;
4500
      }
4501

4502
   case ATTR_TRANSACTION:
33✔
4503
      if (!sem_check_readable(name))
33✔
4504
         return false;
4505
      else if (!sem_check_signal_attr(t))
32✔
4506
         return false;
×
4507

4508
      return true;
4509

4510
   case ATTR_DRIVING_VALUE:
69✔
4511
   case ATTR_DRIVING:
4512
      return sem_check_driving(t);
69✔
4513

4514
   case ATTR_IMAGE:
1,183✔
4515
   case ATTR_VALUE:
4516
      {
4517
         const bool std_2019 = standard() >= STD_19;
1,183✔
4518

4519
         if (named_type == NULL && std_2019 && tree_params(t) == 0) {
1,183✔
4520
            // LCS2016-18 allows attribute with object prefix
4521
            named_type = get_type_or_null(name);
5✔
4522
            add_param(t, name, P_POS, NULL);
5✔
4523
         }
4524

4525
         if (named_type == NULL)
1,183✔
4526
            sem_error(t, "prefix of attribute %s must be a type", istr(attr));
1✔
4527
         if (!std_2019 && !type_is_scalar(named_type))
1,182✔
4528
            sem_error(t, "cannot use attribute %s with non-scalar type %s",
2✔
4529
                      istr(attr), type_pp(named_type));
4530
         else if (std_2019 && !type_is_representable(named_type))
1,180✔
4531
            sem_error(t, "cannot use attribute %s with non-representable "
1✔
4532
                      "type %s", istr(attr), type_pp(named_type));
4533

4534
         type_t std_string = std_type(NULL, STD_STRING);
1,179✔
4535
         type_t arg_type = predef == ATTR_IMAGE ? named_type : std_string;
1,179✔
4536
         if (!sem_check_attr_param(t, arg_type, 1, 1, tab))
1,179✔
4537
            return false;
4✔
4538

4539
         return true;
4540
      }
4541

4542
   case ATTR_LEFTOF:
288✔
4543
   case ATTR_RIGHTOF:
4544
   case ATTR_PRED:
4545
   case ATTR_SUCC:
4546
   case ATTR_POS:
4547
   case ATTR_VAL:
4548
      {
4549
         if (named_type == NULL && standard() >= STD_19)
288✔
4550
            named_type = get_type_or_null(name);   // LCS2016-08 relaxation
3✔
4551

4552
         if (named_type == NULL)
288✔
4553
            sem_error(t, "prefix of attribute %s must be a type", istr(attr));
×
4554

4555
         type_t name_type = tree_type(name);
288✔
4556

4557
         if (!type_is_discrete(name_type) && !type_is_physical(name_type))
288✔
4558
            sem_error(t, "prefix of attribute %s must be a discrete or "
×
4559
                      "physical type", istr(attr));
4560

4561
         if (predef == ATTR_VAL) {
288✔
4562
            // Parameter may be any integer type
4563
            if (tree_params(t) != 1)
110✔
4564
               sem_error(t, "attribute VAL requires a parameter");
1✔
4565

4566
            type_t ptype = tree_type(tree_value(tree_param(t, 0)));
109✔
4567
            if (!type_is_integer(ptype))
109✔
4568
               sem_error(t, "parameter of attribute VAL must have an integer "
1✔
4569
                         "type but found %s", type_pp(ptype));
4570
         }
4571
         else if (!sem_check_attr_param(t, name_type, 1, 1, tab))
178✔
4572
            return false;
×
4573

4574
         return true;
4575
      }
4576

4577
   case ATTR_BASE:
1✔
4578
      sem_error(t, "BASE attribute is allowed only as the prefix of the name "
1✔
4579
                "of another attribute");
4580

4581
   case ATTR_ELEMENT:
1✔
4582
   case ATTR_SUBTYPE:
4583
   case ATTR_INDEX:
4584
      sem_error(t, "%s attribute is only allowed in a type mark", istr(attr));
1✔
4585

4586
   case ATTR_CONVERSE:
24✔
4587
      if (type_kind(tree_type(name)) != T_VIEW)
24✔
4588
         sem_error(t, "prefix of 'CONVERSE attribute must be a named mode "
1✔
4589
                   "view or alias thereof");
4590

4591
      return true;
4592

4593
   case ATTR_REFLECT:
99✔
4594
      if (get_type_or_null(name) == NULL)
99✔
4595
         sem_error(t, "prefix of attribute REFLECT is not a type mark or "
1✔
4596
                   "an object with a type");
4597
      else if (named_type != NULL && type_is_unconstrained(named_type))
98✔
4598
         sem_error(t, "prefix of 'REFLECT attribute must be a fully "
1✔
4599
                   "constrained subtype");
4600

4601
      return true;
4602

4603
   case ATTR_USER:
220✔
4604
      if (!tree_has_value(t))
220✔
4605
         return false;
4606

4607
      if (!sem_static_name(name, sem_globally_static)) {
218✔
4608
         if (tree_kind(name) == T_REF)
×
4609
            sem_error(name, "%s is not a static name", istr(tree_ident(name)));
×
4610
         else
4611
            sem_error(name, "invalid attribute reference");
×
4612
      }
4613

4614
      return true;
4615

4616
   default:
×
4617
      fatal_trace("unhandled attribute kind %d", predef);
4618
   }
4619
}
4620

4621
static bool sem_check_qualified(tree_t t, nametab_t *tab)
4,261✔
4622
{
4623
   if (tree_has_value(t)) {
4,261✔
4624
      tree_t value = tree_value(t);
3,972✔
4625

4626
      if (!sem_check(value, tab))
3,972✔
4627
         return false;
4628

4629
      // LRM 08 section 9.3.5 qualified expressions: the operand shall have
4630
      // the same type as the base type of the type mark
4631
      type_t base = type_base_recur(tree_type(t));
3,971✔
4632
      if (!sem_check_type(value, base, tab)) {
3,971✔
4633
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(value));
1✔
4634
         diag_printf(d, "operand of qualified expression must have type %s",
1✔
4635
                     type_pp(base));
4636
         diag_hint(d, tree_loc(value), "operand has type %s",
1✔
4637
                   type_pp(tree_type(value)));
4638
         diag_lrm(d, STD_08, "9.3.5");
1✔
4639
         diag_emit(d);
1✔
4640
         return false;
1✔
4641
      }
4642
   }
4643

4644
   return true;
4645
}
4646

4647
static bool sem_static_signal_name(tree_t t)
1,458✔
4648
{
4649
   if (!sem_static_name(t, sem_globally_static))
1,458✔
4650
      return false;
4651

4652
   tree_t ref = name_to_ref(t);
1,456✔
4653
   if (ref != NULL && !tree_has_ref(ref))
1,456✔
4654
      return true;  // Suppress cascading error
4655

4656
   return ref != NULL && class_of(tree_ref(ref)) == C_SIGNAL;
1,459✔
4657
}
4658

4659
static bool sem_check_port_actual(formal_map_t *formals, int nformals,
3,638✔
4660
                                  tree_t param, tree_t unit, nametab_t *tab)
4661
{
4662
   tree_t value = tree_value(param), name = NULL, out_conv = NULL;
3,638✔
4663
   tree_t decl = NULL;
3,638✔
4664
   type_t type = NULL;
3,638✔
4665

4666
   switch (tree_subkind(param)) {
3,638✔
4667
   case P_POS:
1,384✔
4668
      {
4669
         const int pos = tree_pos(param);
1,384✔
4670
         if (pos >= nformals)
1,384✔
4671
            sem_error(value, "found at least %d positional actuals but %s "
4✔
4672
                      "has only %d port%s", pos + 1, istr(tree_ident(unit)),
4673
                      nformals, nformals == 1 ? "" : "s");
4674
         if (formals[pos].state >= MAP_PARTIAL)
1,382✔
4675
            sem_error(value, "formal port %s already has an actual",
×
4676
                      istr(tree_ident(formals[pos].decl)));
4677
         formals[pos].state = MAP_FULL;
1,382✔
4678
         decl = formals[pos].decl;
1,382✔
4679
         type = tree_type(decl);
1,382✔
4680
      }
4681
      break;
1,382✔
4682

4683
   case P_NAMED:
2,254✔
4684
      {
4685
         tree_t ref = (name = tree_name(param));
2,254✔
4686

4687
         switch (tree_kind(name)) {
2,254✔
4688
         case T_FCALL:
1✔
4689
            if (tree_params(name) != 1)
1✔
4690
               sem_error(name, "output conversion function must have "
×
4691
                         "exactly one parameter");
4692

4693
            // The parser would have replaced any other valid conversion
4694
            // function with T_CONV_FUNC
4695
            sem_error(name, "invalid output conversion %s",
1✔
4696
                      istr(tree_ident(name)));
4697
            break;
4698

4699
         case T_CONV_FUNC:
90✔
4700
         case T_TYPE_CONV:
4701
            out_conv = name;
90✔
4702
            name = ref = tree_value(name);
90✔
4703
            break;
90✔
4704

4705
         default:
4706
            break;
4707
         }
4708

4709
         ref = name_to_ref(ref);
2,253✔
4710
         assert(ref != NULL && tree_kind(ref) == T_REF);
2,253✔
4711

4712
         if (!tree_has_ref(ref))
2,253✔
4713
            return false;
4714

4715
         tree_t d = tree_ref(ref);
2,253✔
4716
         for (int i = 0; i < nformals; i++) {
8,662✔
4717
            if (formals[i].decl == d) {
8,660✔
4718
               if (formals[i].state == MAP_FULL)
2,251✔
4719
                  sem_error(value, "formal port %s already has an actual",
1✔
4720
                            istr(tree_ident(formals[i].decl)));
4721
               formals[i].state = ref == name ? MAP_FULL : MAP_PARTIAL;
2,250✔
4722
               decl = formals[i].decl;
2,250✔
4723
               tree_set_flag(ref, TREE_F_FORMAL_NAME);
2,250✔
4724
               break;
2,250✔
4725
            }
4726
         }
4727

4728
         if (decl == NULL)
2,252✔
4729
            sem_error(value, "%s has no port named %s",
2✔
4730
                      istr(tree_ident(unit)), istr(tree_ident(ref)));
4731

4732
         if (!sem_static_name(name, sem_locally_static))
2,250✔
4733
            sem_error(name, "formal name must be locally static");
3✔
4734

4735
         if (out_conv != NULL) {
2,247✔
4736
            port_mode_t mode = tree_subkind(decl);
90✔
4737

4738
            type = tree_type((mode == PORT_INOUT) ? name : out_conv);
158✔
4739

4740
            if (mode == PORT_IN)
90✔
4741
               sem_error(name, "output conversion not allowed for formal "
1✔
4742
                         "%s with mode IN", istr(tree_ident(decl)));
4743

4744
            if (tree_kind(value) == T_OPEN)
89✔
4745
               sem_error(name, "output conversion for formal %s must not "
1✔
4746
                         "have OPEN actual", istr(tree_ident(decl)));
4747
         }
4748
         else
4749
            type = tree_type(name);
2,157✔
4750

4751
         break;
4752
      }
4753
   }
4754

4755
   assert(type != NULL);
3,627✔
4756

4757
   if (!sem_check(value, tab))
3,627✔
4758
      return false;
4759

4760
   const tree_kind_t kind = tree_kind(value);
3,624✔
4761
   tree_t expr = kind == T_INERTIAL ? tree_value(value) : value;
3,624✔
4762

4763
   type_t value_type = tree_type(expr), atype;
3,624✔
4764

4765
   if (!sem_check_type(expr, type, tab)) {
3,624✔
4766
      diag_t *d = diag_new(DIAG_ERROR, tree_loc(value));
3✔
4767
      diag_printf(d, "type of actual %s does not match type %s of formal "
3✔
4768
                  "port %s", type_pp2(value_type, type),
4769
                  type_pp2(type, value_type), istr(tree_ident(decl)));
4770

4771
      if (type_is_generic(type)) {
3✔
4772
         hash_t *map = get_generic_map(tab);
1✔
4773
         if (map != NULL && (atype = hash_get(map, type)))
1✔
4774
            diag_hint(d, NULL, "generic type %s is mapped to %s",
1✔
4775
                      type_pp(type), type_pp(atype));
4776
      }
4777

4778
      diag_emit(d);
3✔
4779
      return false;
3✔
4780
   }
4781

4782
   const port_mode_t mode = tree_subkind(decl);
3,621✔
4783

4784
   if (kind == T_OPEN) {
3,621✔
4785
      if ((mode == PORT_IN) && !tree_has_value(decl))
55✔
4786
         sem_error(value, "unconnected port %s with mode IN must have a "
1✔
4787
                   "default value", istr(tree_ident(decl)));
4788

4789
      if ((mode != PORT_IN) && type_is_unconstrained(tree_type(decl)))
54✔
4790
         sem_error(value, "port %s of unconstrained type %s cannot "
1✔
4791
                   "be unconnected", istr(tree_ident(decl)), type_pp(type));
4792
   }
4793

4794
   // Check for type conversions and conversion functions
4795
   // These only apply if the class of the formal is not constant
4796

4797
   tree_t actual;
3,619✔
4798
   if (kind == T_TYPE_CONV || kind == T_CONV_FUNC) {
3,619✔
4799
      // Conversion functions are in LRM 93 section 4.3.2.2
4800
      actual = tree_value(value);
137✔
4801

4802
      // LRM 93 section 3.2.1.1 result of a type conversion in an
4803
      // association list cannot be an unconstrained array type
4804
      if (type_is_unconstrained(value_type) && type_is_unconstrained(type)) {
137✔
4805
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(value));
1✔
4806
         diag_printf(d, "result of conversion for unconstrained formal "
1✔
4807
                     "%s must be a constrained array type",
4808
                     istr(tree_ident(decl)));
4809
         diag_lrm(d, STD_93, "3.2.1.1");
1✔
4810
         diag_emit(d);
1✔
4811
         return false;
1✔
4812
      }
4813

4814
      if (mode == PORT_OUT)
136✔
4815
         sem_error(value, "conversion not allowed for formal %s with "
1✔
4816
                   "mode OUT", istr(tree_ident(decl)));
4817
      else if (mode == PORT_INOUT && out_conv == NULL)
135✔
4818
         sem_error(value, "INOUT port %s has output conversion but no "
1✔
4819
                   "corresponding input conversion", istr(tree_ident(decl)));
4820
   }
4821
   else
4822
      actual = value;    // No conversion
4823

4824
   tree_t ref = name_to_ref(actual);
3,616✔
4825

4826
   if (mode == PORT_IN && kind != T_INERTIAL) {
3,616✔
4827
      bool is_static = true;
2,095✔
4828
      if (ref != NULL && class_of(ref) == C_SIGNAL)
2,095✔
4829
         is_static = sem_static_name(actual, sem_globally_static);
1,924✔
4830
      else
4831
         is_static = sem_globally_static(actual);
171✔
4832

4833
      // LRM 08 section 6.5.6.3 the actual is converted to a concurrent
4834
      // signal assignment to an anonymous signal that is then
4835
      // associated with the formal
4836
      if (!is_static && standard() >= STD_08) {
2,109✔
4837
         // The rules listed for unconstrained ports in 6.5.6.3 should
4838
         // be covered by the check for a globally static subtype in
4839
         // addition to the checks above
4840
         if (type_is_unconstrained(type)
15✔
4841
             && !sem_static_subtype(value_type, sem_globally_static)) {
1✔
4842
            diag_t *d = diag_new(DIAG_ERROR, tree_loc(value));
1✔
4843
            diag_printf(d, "expression associated with unconstrained formal "
1✔
4844
                        "port %s must have a globally static subtype",
4845
                        istr(tree_ident(decl)));
4846
            diag_lrm(d, STD_08, "6.5.6.3");
1✔
4847
            diag_emit(d);
1✔
4848
            return false;
1✔
4849
         }
4850

4851
         tree_t w = tree_new(T_INERTIAL);
14✔
4852
         tree_set_loc(w, tree_loc(value));
14✔
4853
         tree_set_value(w, value);
14✔
4854
         tree_set_target(w, name ?: make_ref(decl));
14✔
4855

4856
         tree_set_value(param, w);
14✔
4857
      }
4858
      else if (!is_static)
2,080✔
4859
         sem_error(value, "actual associated with port %s of mode IN must be "
4✔
4860
                   "a globally static expression or static signal name",
4861
                   istr(tree_ident(decl)));
4862
   }
4863
   else if (mode == PORT_INOUT && tree_class(decl) == C_VARIABLE) {
1,521✔
4864
      // VHDL-2019 additions for shared variable ports
4865
      if (ref == NULL || class_of(ref) != C_VARIABLE)
5✔
4866
         sem_error(value, "actual associated with formal variable port %s "
1✔
4867
                   "must either be a shared variable or a formal variable port "
4868
                   "of another design entity", istr(tree_ident(decl)));
4869
   }
4870
   else if (mode == PORT_ARRAY_VIEW || mode == PORT_RECORD_VIEW) {
1,516✔
4871
      if (!sem_static_signal_name(actual))
70✔
4872
         sem_error(value, "actual associated with port %s with mode view "
1✔
4873
                   "indication must be a static signal name",
4874
                   istr(tree_ident(decl)));
4875

4876
      if (!sem_check_compatible_view(decl, actual))
69✔
4877
         return false;
4878
   }
4879
   else if (mode != PORT_IN && tree_kind(actual) != T_OPEN
1,446✔
4880
            && !sem_static_signal_name(actual)) {
1,388✔
4881
      sem_error(value, "actual associated with port %s of mode %s must be "
4✔
4882
                "a static signal name or OPEN",
4883
                istr(tree_ident(decl)), port_mode_str(tree_subkind(decl)));
4884
   }
4885
   else if (kind == T_INERTIAL)
1,442✔
4886
      tree_set_target(value, name ?: make_ref(decl));
15✔
4887

4888
   // Check connections between ports
4889
   if (ref != NULL && tree_has_ref(ref)) {
3,604✔
4890
      tree_t odecl = tree_ref(ref);
3,434✔
4891
      if (tree_kind(odecl) == T_PORT_DECL) {
3,434✔
4892
         const port_mode_t omode = tree_subkind(odecl);
298✔
4893

4894
         const bool error =
596✔
4895
            (omode == PORT_BUFFER && (mode == PORT_OUT || mode == PORT_INOUT))
4✔
4896
            || (omode != PORT_BUFFER && mode == PORT_BUFFER)
296✔
4897
            || (omode == PORT_IN && (mode == PORT_OUT || mode == PORT_INOUT))
294✔
4898
            || (omode == PORT_OUT && mode == PORT_IN && standard() < STD_08)
293✔
4899
            || (omode == PORT_OUT && mode == PORT_INOUT)
292✔
4900
            || (omode == PORT_LINKAGE && mode != PORT_LINKAGE);
589✔
4901

4902
         if (error) {
298✔
4903
            diag_t *d = diag_new(DIAG_ERROR, tree_loc(value));
8✔
4904
            diag_printf(d, "port %s with mode %s cannot be associated "
8✔
4905
                        "with formal port %s with mode %s",
4906
                        istr(tree_ident(odecl)), port_mode_str(omode),
4907
                        istr(tree_ident(decl)), port_mode_str(mode));
4908
            diag_hint(d, tree_loc(decl), "formal port %s declared here",
8✔
4909
                      istr(tree_ident(decl)));
4910
            diag_hint(d, tree_loc(odecl), "port %s declared here",
8✔
4911
                      istr(tree_ident(odecl)));
4912
            diag_emit(d);
8✔
4913
            return false;
8✔
4914
         }
4915
      }
4916
   }
4917

4918
   return true;
4919
}
4920

4921
static bool sem_check_port_map(tree_t t, tree_t unit, nametab_t *tab)
1,878✔
4922
{
4923
   // Check there is an actual for each formal port generic
4924
   // Rules for maps are described in LRM 93 section 5.2.1.2
4925

4926
   const int nformals = tree_ports(unit);
1,878✔
4927
   const int nactuals = tree_params(t);
1,878✔
4928

4929
   bool ok = true;
1,878✔
4930

4931
   formal_map_t *formals LOCAL = xmalloc_array(nformals, sizeof(formal_map_t));
3,756✔
4932

4933
   for (int i = 0; i < nformals; i++) {
5,435✔
4934
      formals[i].decl  = tree_port(unit, i);
3,557✔
4935
      formals[i].state = MAP_MISSING;
3,557✔
4936
   }
4937

4938
   for (int i = 0; i < nactuals; i++) {
5,527✔
4939
      tree_t p = tree_param(t, i);
3,649✔
4940
      if (tree_subkind(p) != P_NAMED)
3,649✔
4941
         continue;
1,385✔
4942

4943
      tree_t name = tree_name(p);
2,264✔
4944

4945
      ok &= sem_check(name, tab);
2,264✔
4946

4947
      const tree_kind_t name_kind = tree_kind(name);
2,264✔
4948
      if ((name_kind == T_ARRAY_REF || name_kind == T_ARRAY_SLICE)
2,264✔
4949
          && tree_kind(tree_value(p)) == T_OPEN && standard() < STD_19) {
174✔
4950
         diag_t *d = pedantic_diag(tree_loc(p));
1✔
4951
         if (d != NULL) {
1✔
4952
            diag_printf(d, "sub-elements of composite port cannot be "
1✔
4953
                        "associated with OPEN");
4954
            diag_emit(d);
1✔
4955
         }
4956
      }
4957
   }
4958

4959
   if (!ok)
1,878✔
4960
      return false;
4961

4962
   for (int i = 0; i < nactuals; i++) {
5,510✔
4963
      tree_t actual = tree_param(t, i);
3,638✔
4964
      ok &= sem_check_port_actual(formals, nformals, actual, unit, tab);
3,638✔
4965

4966
      if (!ok && tree_subkind(actual) == P_POS && i >= nformals)
3,638✔
4967
         break;   // Prevent useless repeated errors
4968
   }
4969

4970
   if (tree_kind(t) == T_BINDING)
1,874✔
4971
      return ok;
4972

4973
   for (int i = 0; i < nformals; i++) {
5,232✔
4974
      if (formals[i].state == MAP_MISSING) {
3,419✔
4975
         port_mode_t mode = tree_subkind(formals[i].decl);
315✔
4976

4977
         if (mode == PORT_IN && !tree_has_value(formals[i].decl)) {
315✔
4978
            error_at(tree_loc(t), "missing actual for port %s of "
4✔
4979
                     "mode IN without a default expression",
4980
                     istr(tree_ident(formals[i].decl)));
4✔
4981
         }
4982
         else if (mode == PORT_ARRAY_VIEW || mode == PORT_RECORD_VIEW)
311✔
4983
            error_at(tree_loc(t), "missing actual for port %s with "
2✔
4984
                     "mode view indication %s",
4985
                     istr(tree_ident(formals[i].decl)),
1✔
4986
                     type_pp(tree_type(tree_value(formals[i].decl))));
1✔
4987

4988
         type_t ftype = tree_type(formals[i].decl);
315✔
4989
         if (mode != PORT_IN && type_is_unconstrained(ftype)) {
315✔
4990
            error_at(tree_loc(t), "missing actual for port %s with "
1✔
4991
                     "unconstrained array type",
4992
                     istr(tree_ident(formals[i].decl)));
1✔
4993
         }
4994
      }
4995
   }
4996

4997
   return ok;
4998
}
4999

5000
static bool sem_check_generic_actual(formal_map_t *formals, int nformals,
1,652✔
5001
                                     tree_t param, tree_t unit, nametab_t *tab)
5002
{
5003
   tree_t name = NULL;
1,652✔
5004
   int pos = -1;
1,652✔
5005
   switch (tree_subkind(param)) {
1,652✔
5006
   case P_POS:
558✔
5007
      {
5008
         pos = tree_pos(param);
558✔
5009
         if (pos >= nformals)
558✔
5010
            sem_error(param, "found at least %d positional actuals but %s "
1✔
5011
                      "has only %d generic%s", pos + 1, istr(tree_ident(unit)),
5012
                      nformals, nformals == 1 ? "" : "s");
5013
         else if (tree_flags(formals[pos].decl) & TREE_F_PREDEFINED) {
557✔
5014
            diag_t *d = diag_new(DIAG_WARN, tree_loc(param));
2✔
5015
            diag_printf(d, "positional generic actual is associated with "
2✔
5016
                        "implicit generic subprogram %s for type %s",
5017
                        istr(tree_ident(formals[pos].decl)),
5018
                        type_pp(tree_type(tree_port(formals[pos].decl, 0))));
5019
            diag_hint(d, NULL, "use a named association if this was intended");
2✔
5020
            diag_emit(d);
2✔
5021
         }
5022
      }
5023
      break;
5024

5025
   case P_NAMED:
1,094✔
5026
      {
5027
         name = tree_name(param);
1,094✔
5028

5029
         tree_t ref = name_to_ref(name);
1,094✔
5030
         if (ref == NULL)
1,094✔
5031
            sem_error(name, "invalid name in generic map");
1✔
5032
         else if (!tree_has_ref(ref))
1,093✔
5033
            return false;
5034

5035
         tree_t d = tree_ref(ref);
1,090✔
5036
         for (pos = 0; pos < nformals; pos++) {
3,644✔
5037
            if (formals[pos].decl == d) {
2,553✔
5038
               tree_set_flag(ref, TREE_F_FORMAL_NAME);
1,089✔
5039
               break;
1,089✔
5040
            }
5041
         }
5042

5043
         if (pos == nformals)
1,090✔
5044
            sem_error(name, "%s is not a formal generic of %s",
1✔
5045
                      istr(tree_ident(ref)), istr(tree_ident(unit)));
5046
         break;
5047
      }
5048
   }
5049

5050
   assert(pos >= 0 && pos < nformals);
1,646✔
5051

5052
   tree_t decl = formals[pos].decl, value = tree_value(param);
1,646✔
5053
   type_t type = get_type_or_null(name ?: decl);
2,203✔
5054

5055
   const bool is_full = name == NULL || tree_kind(name) == T_REF;
1,646✔
5056
   const bool is_open = tree_kind(value) == T_OPEN;
1,646✔
5057

5058
   if ((is_open && formals[pos].state == MAP_PARTIAL)
1,646✔
5059
       || (!is_open && formals[pos].state == MAP_OPEN))
1,645✔
5060
      sem_error(param, "formal generic %s associated with OPEN cannot be "
2✔
5061
                "individually associated", istr(tree_ident(formals[pos].decl)));
5062

5063
   if (formals[pos].state >= (is_full ? MAP_PARTIAL : MAP_FULL))
1,658✔
UNCOV
5064
      sem_error(param, "formal generic %s already has an actual",
×
5065
                istr(tree_ident(formals[pos].decl)));
5066

5067
   if (is_open)
1,644✔
5068
      formals[pos].state = MAP_OPEN;
9✔
5069
   else if (is_full)
1,635✔
5070
      formals[pos].state = MAP_FULL;
1,622✔
5071
   else
5072
      formals[pos].state = MAP_PARTIAL;
13✔
5073

5074
   if (is_open && !tree_has_value(decl))
1,644✔
5075
      sem_error(param, "generic %s without a default expression cannot "
1✔
5076
                "be associated with OPEN", istr(tree_ident(decl)));
5077
   else if (is_open)
1,643✔
5078
      return true;   // No further checking
5079

5080
   switch (tree_class(decl)) {
1,635✔
5081
   case C_TYPE:
294✔
5082
      // The parser already called map_generic_type
5083
      assert(tree_kind(value) == T_TYPE_REF);
294✔
5084
      assert(type_kind(type) == T_GENERIC);
294✔
5085

5086
      type_t map = tree_type(value);
294✔
5087
      if (type_is_none(map))
294✔
5088
         return false;
5089
      else if (!sem_check_incomplete(param, map))
290✔
5090
         return false;
5091

5092
      static const char *class_strings[] = {
289✔
5093
         [GTYPE_SCALAR] = "a scalar",
5094
         [GTYPE_DISCRETE] = "a discrete",
5095
         [GTYPE_INTEGER] = "an integer",
5096
         [GTYPE_FLOATING] = "a floating-point",
5097
         [GTYPE_PHYSICAL] = "a physical",
5098
         [GTYPE_ACCESS] = "an access",
5099
         [GTYPE_ARRAY] = "an array",
5100
         [GTYPE_FILE] = "a file",
5101
      };
5102

5103
      const gtype_class_t class = type_subkind(type);
289✔
5104
      if (!type_matches_class(map, class))
289✔
5105
         sem_error(param, "cannot map type %s to generic interface type %s "
8✔
5106
                   "which requires %s type", type_pp(map),
5107
                   istr(tree_ident(decl)), class_strings[class]);
5108
      else if (class == GTYPE_ACCESS || class == GTYPE_FILE) {
281✔
5109
         type_t expect = type_designated(type);
9✔
5110
         type_t actual = type_designated(map);
9✔
5111

5112
         if (type_is_generic(expect)) {
9✔
5113
            const gtype_class_t expect_class = type_subkind(expect);
5✔
5114
            if (!type_matches_class(actual, expect_class))
5✔
5115
               sem_error(param, "cannot map type %s to generic interface type "
1✔
5116
                         "%s as the designated type %s is not %s type",
5117
                         type_pp(map), istr(tree_ident(decl)),
5118
                         type_pp(actual), class_strings[expect_class]);
5119
         }
5120
         else if (!type_eq(actual, expect))
4✔
5121
            sem_error(param, "cannot map type %s to generic interface type "
2✔
5122
                      "%s as the designated type %s is not %s",
5123
                      type_pp(map), istr(tree_ident(decl)),
5124
                      type_pp(actual), type_pp(expect));
5125
      }
5126
      else if (class == GTYPE_ARRAY) {
272✔
5127
         const int nindex = type_indexes(type);
45✔
5128
         if (nindex != dimension_of(map))
45✔
5129
            sem_error(param, "cannot map type %s to generic interface "
1✔
5130
                      "type %s as it has %d dimensions but the incomplete "
5131
                      "type definition has %d", type_pp(map),
5132
                      istr(tree_ident(decl)), dimension_of(map), nindex);
5133

5134
         for (int i = 0; i < nindex; i++) {
86✔
5135
            type_t itype = type_index(type, i);
44✔
5136
            type_t imap = index_type_of(map, i);
44✔
5137

5138
            if (type_is_generic(itype)) {
44✔
5139
               const gtype_class_t expect_class = type_subkind(itype);
40✔
5140
               if (!type_matches_class(imap, expect_class))
40✔
5141
                  sem_error(param, "cannot map type %s to generic interface "
1✔
5142
                            "type %s as the index type %s of the %s dimension "
5143
                            "is not %s type", type_pp(map),
5144
                            istr(tree_ident(decl)), type_pp(imap),
5145
                            ordinal_str(i + 1), class_strings[expect_class]);
5146
            }
5147
            else if (!type_eq(imap, itype))
4✔
5148
               sem_error(param, "cannot map type %s to generic interface type "
43✔
5149
                         "%s as the index type %s of the %s dimension is not "
5150
                         "%s", type_pp(map), istr(tree_ident(decl)),
5151
                         type_pp(imap), ordinal_str(i + 1), type_pp(itype));
5152
         }
5153

5154
         type_t expect = type_elem(type);
42✔
5155
         type_t actual = type_elem(map);
42✔
5156

5157
          if (type_is_generic(expect)) {
42✔
5158
            const gtype_class_t expect_class = type_subkind(expect);
38✔
5159
            if (!type_matches_class(actual, expect_class))
38✔
5160
               sem_error(param, "cannot map type %s to generic interface type "
1✔
5161
                         "%s as the element type %s is not %s type",
5162
                         type_pp(map), istr(tree_ident(decl)),
5163
                         type_pp(actual), class_strings[expect_class]);
5164
         }
5165
         else if (!type_eq(actual, expect))
4✔
5166
            sem_error(param, "cannot map type %s to generic interface type "
1✔
5167
                      "%s as the element type %s is not %s",
5168
                      type_pp(map), istr(tree_ident(decl)),
5169
                      type_pp(actual), type_pp(expect));
5170
      }
5171

5172
      break;
5173

5174
   case C_PACKAGE:
80✔
5175
      {
5176
         tree_t pack = NULL;
80✔
5177
         if (tree_kind(value) == T_REF && tree_has_ref(value))
80✔
5178
            pack = tree_ref(value);
79✔
5179

5180
         if (pack == NULL || tree_kind(pack) != T_PACK_INST)
79✔
5181
            sem_error(value, "actual for generic %s is not an "
2✔
5182
                      "instantiated package name", istr(tree_ident(decl)));
5183
         else if (!tree_has_ref(pack))
78✔
5184
            return false;   // Was parse error
5185

5186
         tree_t map = tree_value(decl);
78✔
5187
         if (!tree_has_ref(map))
78✔
5188
            return false;   // Was earlier error
5189

5190
         assert(tree_kind(map) == T_PACKAGE_MAP);
77✔
5191

5192
         tree_t base = tree_ref(pack);
77✔
5193
         tree_t expect = tree_ref(map);
77✔
5194

5195
         if (tree_ident(base) != tree_ident(expect))
77✔
5196
            sem_error(value, "expected an instance of package %s but have "
1✔
5197
                      "instance of %s for generic %s", istr(tree_ident(expect)),
5198
                      istr(tree_ident(base)), istr(tree_ident(decl)));
5199

5200
         map_generic_package(tab, expect, pack);
76✔
5201
      }
5202
      break;
76✔
5203

5204
   case C_FUNCTION:
64✔
5205
   case C_PROCEDURE:
5206
      if (!sem_check(value, tab))
64✔
5207
         return false;
5208

5209
      if (!type_eq_map(tree_type(value), type, get_generic_map(tab)))
60✔
UNCOV
5210
         sem_error(value, "type of actual %s does not match type %s of formal "
×
5211
                   "generic %s", type_pp(tree_type(value)), type_pp(type),
5212
                   istr(tree_ident(decl)));
5213

5214
      assert(tree_kind(value) == T_REF);
60✔
5215

5216
      if (!tree_has_ref(value))
60✔
5217
         return false;
5218

5219
      tree_t sub = tree_ref(value);
60✔
5220
      assert(is_subprogram(sub));
60✔
5221

5222
      const bool pure_formal = !(tree_flags(decl) & TREE_F_IMPURE);
60✔
5223
      const bool pure_actual = !(tree_flags(sub) & TREE_F_IMPURE);
60✔
5224

5225
      if (pure_formal && !pure_actual) {
60✔
5226
         diag_t *d = pedantic_diag(tree_loc(value));
1✔
5227
         if (d != NULL) {
1✔
5228
            diag_printf(d, "cannot associate impure function %s with pure "
1✔
5229
                        "generic subprogram %s", type_pp(tree_type(value)),
5230
                        istr(tree_ident(decl)));
5231
            diag_emit(d);
1✔
5232
         }
5233
      }
5234

5235
      map_generic_subprogram(tab, decl, sub);
60✔
5236
      break;
60✔
5237

5238
   case C_CONSTANT:
1,197✔
5239
      if (name != NULL) {
1,197✔
5240
         if (!sem_check(name, tab))
773✔
5241
            return false;
5242

5243
         if (!sem_static_name(name, sem_locally_static))
772✔
5244
            sem_error(name, "formal generic name must be a locally "
1✔
5245
                      "static name");
5246
      }
5247

5248
      if (!sem_check(value, tab))
1,195✔
5249
         return false;
5250

5251
      if (!sem_check_type(value, type, tab))
1,192✔
5252
         sem_error(value, "type of actual %s does not match type %s of formal "
6✔
5253
                   "generic %s", type_pp(tree_type(value)), type_pp(type),
5254
                   istr(tree_ident(decl)));
5255

5256
      if (is_full) map_generic_const(tab, decl, value);
1,186✔
5257
      break;
5258

5259
   default:
5260
      // Was an earlier error
5261
      break;
5262
   }
5263

5264
   return true;
5265
}
5266

5267
static bool sem_check_generic_map(tree_t t, tree_t unit, nametab_t *tab)
2,314✔
5268
{
5269
   // Check there is an actual for each formal generic
5270
   // Rules for maps are described in LRM 93 section 5.2.1.2
5271

5272
   const int nformals = tree_generics(unit);
2,314✔
5273
   const int nactuals = tree_genmaps(t);
2,314✔
5274

5275
   formal_map_t *formals LOCAL = xmalloc_array(nformals, sizeof(formal_map_t));
2,314✔
5276

5277
   for (int i = 0; i < nformals; i++) {
5,755✔
5278
      formals[i].decl  = tree_generic(unit, i);
3,441✔
5279
      formals[i].state = MAP_MISSING;
3,441✔
5280
   }
5281

5282
   bool ok = true;
5283

5284
   for (int i = 0; i < nactuals; i++) {
3,965✔
5285
      tree_t actual = tree_genmap(t, i);
1,652✔
5286
      ok &= sem_check_generic_actual(formals, nformals, actual, unit, tab);
1,652✔
5287

5288
      if (!ok && tree_subkind(actual) == P_POS && i >= nformals)
1,652✔
5289
         break;   // Prevent useless repeated errors
5290
   }
5291

5292
   for (int i = 0; i < nformals; i++) {
5,755✔
5293
      if (formals[i].state >= MAP_PARTIAL)
3,441✔
5294
         continue;
1,629✔
5295

5296
      const class_t class = tree_class(formals[i].decl);
1,812✔
5297
      if (class == C_PACKAGE)
1,812✔
5298
         error_at(tree_loc(t), "missing actual for generic package %s",
1✔
5299
                  istr(tree_ident(formals[i].decl)));
1✔
5300
      else if (class == C_TYPE) {
1,811✔
5301
         error_at(tree_loc(t), "missing actual for generic type %s",
1✔
5302
                  istr(tree_ident(formals[i].decl)));
1✔
5303
         map_generic_type(tab, tree_type(formals[i].decl), type_new(T_NONE));
1✔
5304
      }
5305
      else if (tree_has_value(formals[i].decl)) {
1,810✔
5306
         tree_t value = tree_value(formals[i].decl);
1,804✔
5307
         if (tree_kind(value) == T_BOX) {
1,804✔
5308
            // Need to look up the matching subprogram now while we still
5309
            // have the symbol table
5310
            map_generic_box(tab, t, formals[i].decl, i);
1,554✔
5311
         }
5312
      }
5313
      else if (formals[i].state == MAP_MISSING) {
6✔
5314
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
5✔
5315
         diag_printf(d, "missing actual for generic %s without a "
5✔
5316
                     "default expression", istr(tree_ident(formals[i].decl)));
5✔
5317
         diag_hint(d, tree_loc(formals[i].decl), "generic %s declared here",
5✔
5318
                   istr(tree_ident(formals[i].decl)));
5✔
5319
         diag_emit(d);
5✔
5320
      }
5321
   }
5322

5323
   return ok;
2,314✔
5324
}
5325

5326
static bool sem_check_instance(tree_t t, nametab_t *tab)
1,485✔
5327
{
5328
   if (!tree_has_ref(t))
1,485✔
5329
      return false;
5330

5331
   tree_t unit = primary_unit_of(tree_ref(t));
1,473✔
5332

5333
   if (tree_has_spec(t)) {
1,473✔
5334
      tree_t spec = tree_spec(t);
95✔
5335

5336
      if (tree_class(t) != C_COMPONENT) {
95✔
5337
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(spec));
2✔
5338
         diag_printf(d, "specification may only be used with component"
2✔
5339
                     " instances");
5340
         diag_hint(d, tree_loc(spec), "specification for %s",
2✔
5341
                   istr(tree_ident(t)));
5342
         diag_hint(d, tree_loc(t), "%s instance", class_str(tree_class(t)));
2✔
5343
         diag_emit(d);
2✔
5344
         return false;
2✔
5345
      }
5346

5347
      assert(tree_kind(unit) == T_COMPONENT);   // Checked by parser
93✔
5348

5349
      if (tree_has_ref(spec) && tree_ref(spec) != unit) {
93✔
5350
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(spec));
2✔
5351
         diag_printf(d, "component mismatch for instance %s: expected %s "
2✔
5352
                     "but specification has %s", istr(tree_ident(t)),
5353
                     istr(tree_ident(unit)), istr(tree_ident(tree_ref(spec))));
5354
         diag_hint(d, tree_loc(spec), "specification has component %s",
2✔
5355
                   istr(tree_ident(tree_ref(spec))));
5356
         diag_hint(d, tree_loc(t), "instance of component %s",
2✔
5357
                   istr(tree_ident(unit)));
5358
         diag_emit(d);
2✔
5359
         return false;
2✔
5360
      }
5361
   }
5362

5363
   if (!sem_check_generic_map(t, unit, tab))
1,469✔
5364
      return false;
5365

5366
   if (!sem_check_port_map(t, unit, tab))
1,438✔
5367
      return false;
33✔
5368

5369
   return true;
5370
}
5371

5372
static bool sem_check_cond(tree_t t, nametab_t *tab)
12,090✔
5373
{
5374
   if (tree_has_value(t)) {
12,090✔
5375
      type_t std_bool = std_type(NULL, STD_BOOLEAN);
9,749✔
5376

5377
      tree_t value = tree_value(t);
9,749✔
5378
      if (!sem_check(value, tab))
9,749✔
5379
         return false;
5380

5381
      if (!sem_check_type(value, std_bool, tab))
9,744✔
5382
         sem_error(value, "type of condition must be %s but have %s",
2✔
5383
                   type_pp(std_bool), type_pp(tree_type(value)));
5384

5385
      if (!sem_check_readable(value))
9,742✔
UNCOV
5386
         return false;
×
5387
   }
5388

5389
   return true;
5390
}
5391

5392
static bool sem_check_if(tree_t t, nametab_t *tab)
8,688✔
5393
{
5394
   bool ok = true;
8,688✔
5395
   const int nconds = tree_conds(t);
8,688✔
5396
   for (int i = 0; i < nconds; i++)
20,601✔
5397
      ok &= sem_check_cond(tree_cond(t, i), tab);
11,913✔
5398

5399
   return ok;
8,688✔
5400
}
5401

5402
static bool sem_static_subtype(type_t type, static_fn_t fn)
271,660✔
5403
{
5404
   // Rules for locally static subtypes are in LRM 93 7.4.1
5405

5406
   if (type_is_unconstrained(type))
271,660✔
5407
      return false;
5408

5409
   if (type_is_scalar(type))
249,605✔
5410
      return true;
5411

5412
   if (type_is_record(type)) {
240,201✔
5413
      const int nfields = type_fields(type);
303✔
5414
      for (int i = 0; i < nfields; i++) {
1,150✔
5415
         if (!sem_static_subtype(tree_type(type_field(type, i)), fn))
865✔
5416
            return false;
5417
      }
5418

5419
      return true;
5420
   }
5421

5422
   switch (type_kind(type)) {
239,898✔
5423
   case T_SUBTYPE:
239,846✔
5424
      {
5425
         const int ndims = dimension_of(type);
239,846✔
5426
         for (int i = 0; i < ndims; i++) {
271,638✔
5427
            if (!(*fn)(range_of(type, i)))
240,003✔
5428
               return false;
5429
         }
5430

5431
         return true;
5432
      }
5433
   default:
5434
      return true;
5435
   }
5436
}
5437

5438
static bool sem_ieee_locally_static(tree_t decl)
62,186✔
5439
{
5440
   // Subprograms definined in certain IEEE packages are treated the
5441
   // same as builtin operators in VHDL-2008
5442

5443
   if (standard() < STD_08)
62,186✔
5444
      return false;
5445

5446
   ident_t unit_name = tree_ident(tree_container(decl));
34,659✔
5447

5448
   switch (is_well_known(unit_name)) {
34,659✔
5449
   case W_NUMERIC_STD:
5450
   case W_NUMERIC_BIT:
5451
   case W_IEEE_1164:
5452
   case W_NUMERIC_BIT_UNSIGNED:
5453
   case W_NUMERIC_STD_UNSIGNED:
5454
      return true;
5455
   default:
25,344✔
5456
      return false;
25,344✔
5457
   }
5458
}
5459

5460
static bool sem_locally_static(tree_t t)
14,219,813✔
5461
{
5462
   // Rules for locally static expressions are in LRM 93 7.4.1
5463

5464
   type_t type = tree_type(t);
14,242,343✔
5465
   tree_kind_t kind = tree_kind(t);
14,242,343✔
5466

5467
   if (type_is_none(type))
14,242,343✔
5468
      return true;   // Prevents further cascading errors
5469

5470
   // Any literal other than of type time
5471
   if (kind == T_LITERAL) {
14,242,334✔
5472
      if (tree_subkind(t) == L_PHYSICAL)
113,213✔
5473
         return !type_eq(type, std_type(NULL, STD_TIME));
1,508✔
5474
      else
5475
         return true;
5476
   }
5477
   else if (kind == T_STRING)
14,129,121✔
5478
      return true;
5479
   else if ((kind == T_REF) && (tree_kind(tree_ref(t)) == T_ENUM_LIT))
14,117,983✔
5480
      return true;
5481
   else if (kind == T_OPEN)
14,072,643✔
5482
      return true;
5483

5484
   if (kind == T_REF) {
14,072,627✔
5485
      tree_t decl = tree_ref(t);
455,740✔
5486
      const tree_kind_t dkind = tree_kind(decl);
455,740✔
5487

5488
      // A constant reference (other than a deferred constant) with a
5489
      // locally static value
5490
      if (dkind == T_CONST_DECL && (tree_flags(decl) & TREE_F_LOCALLY_STATIC))
455,740✔
5491
         return true;
5492

5493
      // An alias of a locally static name
5494
      if (dkind == T_ALIAS)
425,233✔
5495
         return sem_locally_static(tree_value(decl));
1,756✔
5496

5497
      // [2008] A formal generic constant of a generic-mapped subprogram
5498
      // or package with a locally static subtype
5499
      if (dkind == T_GENERIC_DECL && (tree_flags(decl) & TREE_F_LOCALLY_STATIC))
423,477✔
5500
         return true;
5501
   }
5502

5503
   // A locally static range
5504
   if (kind == T_RANGE) {
14,039,178✔
5505
      switch (tree_subkind(t)) {
248,120✔
5506
      case RANGE_TO:
244,012✔
5507
      case RANGE_DOWNTO:
5508
         return sem_locally_static(tree_left(t))
244,012✔
5509
            && sem_locally_static(tree_right(t));
244,537✔
5510

5511
      case RANGE_EXPR:
4,108✔
5512
         return sem_locally_static(tree_value(t));
4,108✔
5513

5514
      default:
5515
         return false;
5516
      }
5517
   }
5518

5519
   // A function call of an implicit operator or [2008] an operation
5520
   // defined in one of the packages STD_LOGIC_1164, NUMERIC_BIT,
5521
   // NUMERIC_STD, NUMERIC_BIT_UNSIGNED, or NUMERIC_STD_UNSIGNED in
5522
   // library IEEE whose actuals are locally static expressions.
5523
   if (kind == T_FCALL) {
13,791,058✔
5524
      if (!tree_has_ref(t))
12,988,748✔
5525
         return true;  // Suppress further errors
5526
      else if (tree_flags(t) & TREE_F_LOCALLY_STATIC)
12,988,748✔
5527
         return true;
5528

5529
      tree_t decl = tree_ref(t);
12,844,390✔
5530
      if (tree_kind(decl) == T_GENERIC_DECL)
12,844,390✔
5531
         return false;   // Not known at this point
5532
      else if (tree_subkind(decl) == S_USER && !sem_ieee_locally_static(decl))
12,843,391✔
5533
         return false;
5534

5535
      const int nparams = tree_params(t);
12,790,520✔
5536
      for (int i = 0; i < nparams; i++) {
12,816,166✔
5537
         if (!sem_locally_static(tree_value(tree_param(t, i))))
12,803,692✔
5538
            return false;
5539
      }
5540

5541
      return true;
5542
   }
5543

5544
   if (kind == T_ATTR_REF) {
5545
      // A predefined attribute other than those listed below whose prefix
5546
      // prefix is either a locally static subtype or is an object that is
5547
      // of a locally static subtype
5548
      const attr_kind_t predef = tree_subkind(t);
245,319✔
5549
      if (predef == ATTR_EVENT || predef == ATTR_ACTIVE
245,319✔
5550
          || predef == ATTR_LAST_EVENT || predef == ATTR_LAST_ACTIVE
245,319✔
5551
          || predef == ATTR_LAST_VALUE || predef == ATTR_DRIVING
244,202✔
5552
          || predef == ATTR_DRIVING_VALUE || predef == ATTR_PATH_NAME
5553
          || predef == ATTR_INSTANCE_NAME || predef == ATTR_SIMPLE_NAME)
5554
         return false;
5555
      else if (!tree_has_value(t)) {
240,129✔
5556
         type_t type = tree_type(tree_name(t));
238,588✔
5557
         return sem_static_subtype(type, sem_locally_static);
238,588✔
5558
      }
5559

5560
      // Whose actual parameter (if any) is a locally static expression
5561
      const int nparams = tree_params(t);
1,541✔
5562
      for (int i = 0; i < nparams; i++) {
1,541✔
5563
         if (!sem_locally_static(tree_value(tree_param(t, i))))
147✔
5564
            return false;
5565
      }
5566

5567
      // A user-defined attribute whose value is a locally static expression
5568
      assert(tree_has_value(t));
1,394✔
5569
      return sem_locally_static(tree_value(t));
1,394✔
5570
   }
5571

5572
   // A qualified expression whose operand is locally static
5573
   if (kind == T_QUALIFIED)
5574
      return sem_locally_static(tree_value(t));
4,653✔
5575

5576
   // A type conversion whose expression is locally static
5577
   if (kind == T_TYPE_CONV)
5578
      return sem_locally_static(tree_value(t));
3,522✔
5579

5580
   // Aggregates must have locally static range and all elements
5581
   // must have locally static values
5582
   if (kind == T_AGGREGATE) {
5583
      if (type_is_unconstrained(type))
6,355✔
5584
         return false;
5585

5586
      if (type_is_array(type)) {
6,245✔
5587
         if (!sem_locally_static(range_of(type, 0)))
5,214✔
5588
            return false;
5589
      }
5590

5591
      const int nassocs = tree_assocs(t);
5,987✔
5592
      for (int i = 0; i < nassocs; i++) {
52,562✔
5593
         tree_t a = tree_assoc(t, i);
47,522✔
5594
         if ((tree_subkind(a) == A_NAMED) && !sem_locally_static(tree_name(a)))
47,522✔
5595
            return false;
5596

5597
         if (!sem_locally_static(tree_value(a)))
47,522✔
5598
            return false;
5599
      }
5600

5601
      return true;
5602
   }
5603

5604
   // A record field name
5605
   if ((kind == T_REF) && (tree_kind(tree_ref(t)) == T_FIELD_DECL))
422,291✔
5606
      return true;
5607

5608
   const bool std08_rules = standard() >= STD_08 || relaxed_rules();
541,733✔
5609

5610
   // [2008] An indexed name whose prefix and index expressions are
5611
   // locally static
5612
   if (std08_rules && kind == T_ARRAY_REF) {
541,733✔
5613
      const int nparams = tree_params(t);
6,406✔
5614
      for (int i = 0; i < nparams; i++) {
8,024✔
5615
         if (!sem_locally_static(tree_value(tree_param(t, i))))
6,460✔
5616
            return false;
5617
      }
5618

5619
      return sem_locally_static(tree_value(t));
1,564✔
5620
   }
5621

5622
   // [2008] A slice name whose prefix and range is locally static
5623
   if (std08_rules && kind == T_ARRAY_SLICE) {
535,327✔
5624
      if (!sem_locally_static(tree_range(t, 0)))
1,687✔
5625
         return false;
5626

5627
      return sem_locally_static(tree_value(t));
472✔
5628
   }
5629

5630
   // [2008] A selected name whose prefix is locally static
5631
   if (std08_rules && kind == T_RECORD_REF)
533,640✔
5632
      return sem_locally_static(tree_value(t));
5,061✔
5633

5634
   return false;
5635
}
5636

5637
static bool sem_static_name(tree_t t, static_fn_t check_fn)
11,141✔
5638
{
5639
   // Rules for static names are in LRM 93 6.1
5640

5641
   switch (tree_kind(t)) {
12,307✔
5642
   case T_REF:
10,116✔
5643
      {
5644
         tree_t decl = tree_ref(t);
10,116✔
5645
         switch (tree_kind(decl)) {
10,116✔
5646
         case T_SIGNAL_DECL:
5647
         case T_VAR_DECL:
5648
         case T_CONST_DECL:
5649
         case T_PORT_DECL:
5650
         case T_TYPE_DECL:
5651
         case T_SUBTYPE_DECL:
5652
         case T_ENTITY:
5653
         case T_ARCH:
5654
         case T_PACK_BODY:
5655
         case T_PACKAGE:
5656
         case T_FUNC_BODY:
5657
         case T_PROC_BODY:
5658
         case T_FUNC_DECL:
5659
         case T_PROC_DECL:
5660
         case T_PROCESS:
5661
         case T_BLOCK:
5662
         case T_ENUM_LIT:
5663
         case T_IMPLICIT_SIGNAL:
5664
         case T_GENERIC_DECL:
5665
         case T_PARAM_DECL:
5666
         case T_CONCURRENT:
5667
         case T_VIEW_DECL:
5668
         case T_UNIT_DECL:
5669
            return true;
5670
         case T_ALIAS:
29✔
5671
            return sem_static_name(tree_value(decl), check_fn);
29✔
UNCOV
5672
         default:
×
UNCOV
5673
            return false;
×
5674
         }
5675
      }
5676

5677
   case T_EXTERNAL_NAME:
5678
      return true;
5679

5680
   case T_RECORD_REF:
1,131✔
5681
      return sem_static_name(tree_value(t), check_fn);
1,131✔
5682

5683
   case T_ARRAY_REF:
842✔
5684
      {
5685
         if (!sem_static_name(tree_value(t), check_fn))
842✔
5686
            return false;
5687

5688
         const int nparams = tree_params(t);
842✔
5689
         for (int i = 0; i < nparams; i++) {
1,676✔
5690
            if (!(*check_fn)(tree_value(tree_param(t, i))))
845✔
5691
               return false;
5692
         }
5693

5694
         return true;
5695
      }
5696

5697
   case T_ARRAY_SLICE:
160✔
5698
      {
5699
         if (!sem_static_name(tree_value(t), check_fn))
160✔
5700
            return false;
5701

5702
         return (*check_fn)(tree_range(t, 0));
160✔
5703
      }
5704

5705
   case T_ATTR_REF:
6✔
5706
      {
5707
         switch (tree_subkind(t)) {
6✔
5708
         case ATTR_DELAYED:
6✔
5709
         case ATTR_STABLE:
5710
         case ATTR_QUIET:
5711
         case ATTR_TRANSACTION:
5712
            return sem_static_name(tree_name(t), check_fn);
6✔
5713
         default:
5714
            return false;
5715
         }
5716
      }
5717

5718
   default:
5✔
5719
      return false;
5✔
5720
   }
5721
}
5722

5723
static bool sem_globally_static(tree_t t)
789,365✔
5724
{
5725
   // Rules for globally static expressions are in LRM 93 7.4.2
5726

5727
   type_t type = tree_type(t);
794,092✔
5728
   tree_kind_t kind = tree_kind(t);
794,092✔
5729

5730
   if (type_is_none(type))
794,092✔
5731
      return true;   // Prevents further cascading errors
5732

5733
   // A literal of type TIME
5734

5735
   if (type_eq(type, std_type(NULL, STD_TIME))) {
794,091✔
5736
      if (kind == T_REF && tree_kind(tree_ref(t)) == T_UNIT_DECL)
6,237✔
5737
         return true;
5738
      else if (kind == T_LITERAL && tree_subkind(t) == L_PHYSICAL)
6,158✔
5739
         return true;
5740
   }
5741

5742
   // A locally static primary
5743

5744
   if (sem_locally_static(t))
793,138✔
5745
      return true;
5746

5747
   // A generic constant, generate parameter, or constant
5748

5749
   if (kind == T_REF) {
537,549✔
5750
      tree_t decl = tree_ref(t);
105,315✔
5751
      switch (tree_kind(decl)) {
105,315✔
5752
      case T_GENERIC_DECL:
5753
         return true;
5754
      case T_CONST_DECL:
4,113✔
5755
         // Do not treat all constants as globally static, this is a
5756
         // defect in the LRM
5757
         return !!(tree_flags(decl) & TREE_F_GLOBALLY_STATIC);
4,113✔
5758
      default:
44,482✔
5759
         return false;
44,482✔
5760
      }
5761
   }
5762
   else if (kind == T_EXTERNAL_NAME)
5763
      return tree_class(t) == C_CONSTANT;
97✔
5764

5765
   // An alias whose aliased name is globally static
5766

5767
   if (kind == T_ALIAS)
UNCOV
5768
      return sem_globally_static(tree_value(t));
×
5769

5770
   if (kind == T_RANGE) {
5771
      if (tree_subkind(t) == RANGE_EXPR)
26,785✔
5772
         return sem_globally_static(tree_value(t));
19✔
5773

5774
      if (!sem_globally_static(tree_left(t)))
26,766✔
5775
         return false;
5776

5777
      if (!sem_globally_static(tree_right(t)))
26,749✔
5778
         return false;
5779

5780
      return true;
26,707✔
5781
   }
5782

5783
   // Aggregates must have globally static range and all elements
5784
   // must have globally static values
5785
   if (kind == T_AGGREGATE) {
5786
      if (type_is_array(type)
689✔
5787
          && !type_is_unconstrained(type)
480✔
5788
          && !sem_globally_static(range_of(type, 0)))
437✔
5789
         return false;
5790

5791
      const int nassocs = tree_assocs(t);
647✔
5792
      for (int i = 0; i < nassocs; i++) {
2,609✔
5793
         tree_t a = tree_assoc(t, i);
2,008✔
5794
         if ((tree_subkind(a) == A_NAMED) && !sem_globally_static(tree_name(a)))
2,008✔
5795
            return false;
5796

5797
         if (!sem_globally_static(tree_value(a)))
2,008✔
5798
            return false;
5799
      }
5800

5801
      return true;
5802
   }
5803

5804
   // A function call of a pure function with globally static actuals
5805
   if (kind == T_FCALL) {
5806
      tree_t decl = tree_ref(t);
330,760✔
5807
      if (tree_flags(decl) & TREE_F_IMPURE)
330,760✔
5808
         return false;
5809

5810
      bool all_static = true;
327,261✔
5811
      const int nparams = tree_params(t);
327,261✔
5812
      for (int i = 0; i < nparams; i++) {
971,170✔
5813
         tree_t p = tree_param(t, i);
643,909✔
5814
         all_static = all_static && sem_globally_static(tree_value(p));
730,890✔
5815
      }
5816
      return all_static;
327,261✔
5817
   }
5818

5819
   if (kind == T_ATTR_REF) {
5820
      const attr_kind_t predef = tree_subkind(t);
35,814✔
5821

5822
      // A predefined attribute that is one of 'SIMPLE_NAME,
5823
      // 'INSTANCE_NAME, or 'PATH_NAME
5824
      if (predef == ATTR_SIMPLE_NAME || predef == ATTR_INSTANCE_NAME
35,814✔
5825
          || predef == ATTR_PATH_NAME)
34,944✔
5826
         return true;   // Clause j
5827

5828
      // A predefined attribute other than those listed below whose
5829
      // prefix is either a globally static subtype or is an object or
5830
      // function call that is of a globally static subtype, or in 2008,
5831
      // a prefix which is a appropriate for a globally static attribute
5832
      if (predef == ATTR_EVENT || predef == ATTR_ACTIVE
34,674✔
5833
          || predef == ATTR_LAST_EVENT || predef == ATTR_LAST_ACTIVE
34,674✔
5834
          || predef == ATTR_LAST_VALUE || predef == ATTR_DRIVING
34,323✔
5835
          || predef == ATTR_DRIVING_VALUE)
34,261✔
5836
         return false;   // Clause k
5837
      else if (predef == ATTR_USER) {
34,233✔
5838
         // A user-defined attribute whose value is a globally static
5839
         // expression
5840
         return sem_globally_static(tree_value(t));
120✔
5841
      }
5842

5843
      tree_t name = tree_name(t);
34,113✔
5844

5845
      if (standard() >= STD_08 || relaxed_rules()) {
34,113✔
5846
         // LRM 08 section 9.4.3: A prefix is appropriate for a globally
5847
         // static attribute if it denotes a signal, a constant, a type
5848
         // or subtype, a globally static function call, a variable that
5849
         // is not of an access type, or a variable of an access type
5850
         // whose designated subtype is fully constrained.
5851

5852
         if (tree_kind(name) == T_FCALL)
5,756✔
5853
            return sem_globally_static(name);
5854

5855
         tree_t ref = name_to_ref(name);
5,756✔
5856
         if (ref == NULL)
5,756✔
5857
            return false;
5858

5859
         tree_t decl = tree_ref(ref);
5,752✔
5860
         const tree_kind_t dkind = tree_kind(decl);
5,752✔
5861
         if (dkind == T_VAR_DECL && type_is_access(tree_type(name)))
5,752✔
5862
            return false;
5863

5864
         return dkind == T_CONST_DECL || dkind == T_SIGNAL_DECL
5,752✔
5865
            || dkind == T_TYPE_DECL || dkind == T_VAR_DECL
5,722✔
5866
            || dkind == T_SUBTYPE_DECL || dkind == T_PORT_DECL
5867
            || dkind == T_GENERIC_DECL;
11,474✔
5868
      }
5869

5870
      type_t type = get_type_or_null(name);
28,357✔
5871
      if (type == NULL)
28,357✔
5872
         return false;
5873

5874
      return sem_static_subtype(type, sem_globally_static);
28,357✔
5875
   }
5876

5877
   // A qualified expression whose operand is globally static
5878

5879
   if (kind == T_QUALIFIED)
5880
      return sem_globally_static(tree_value(t));
238✔
5881

5882
   // A type conversion whose operand is globally static
5883

5884
   if (kind == T_TYPE_CONV)
5885
      return sem_globally_static(tree_value(t));
1,027✔
5886

5887
   // TODO: clauses o, p
5888

5889
   // A sub-element or slice where indexes are globally static
5890

5891
   if (kind == T_ARRAY_REF) {
5892
      if (!sem_globally_static(tree_value(t)))
30,110✔
5893
         return false;
5894

5895
      const int nparams = tree_params(t);
24,966✔
5896
      for (int i = 0; i < nparams; i++) {
49,547✔
5897
         if (!sem_globally_static(tree_value(tree_param(t, i))))
25,004✔
5898
            return false;
5899
      }
5900

5901
      return true;
5902
   }
5903
   else if (kind == T_ARRAY_SLICE) {
5904
      if (!sem_globally_static(tree_value(t)))
861✔
5905
         return false;
5906

5907
      if (!sem_globally_static(tree_range(t, 0)))
90✔
5908
         return false;
5909

5910
      return true;
90✔
5911
   }
5912
   else if (kind == T_RECORD_REF)
5913
      return sem_globally_static(tree_value(t));
3,323✔
5914

5915
   return false;
5916
}
5917

5918
static bool sem_check_case(tree_t t, nametab_t *tab)
611✔
5919
{
5920
   tree_t test = tree_value(t);
611✔
5921
   if (!sem_check(test, tab))
611✔
5922
      return false;
5923

5924
   type_t type = tree_type(test);
607✔
5925

5926
   // LRM 93 8.8 if the type of the expression is an array then it must be
5927
   // a one dimensional character array type
5928

5929
   const bool is_1d_character_array = type_is_character_array(type);
607✔
5930
   const bool valid = is_1d_character_array || type_is_discrete(type);
607✔
5931

5932
   if (!valid) {
607✔
5933
      diag_t *d = diag_new(DIAG_ERROR, tree_loc(test));
2✔
5934
      diag_printf(d, "case expression must have a discrete type or one "
2✔
5935
                  "dimensional character array type");
5936
      if (type_is_array(type) && dimension_of(type) != 1)
2✔
5937
         diag_hint(d, tree_loc(test), "array has %d dimensions",
1✔
5938
                   dimension_of(type));
5939
      else if (type_is_array(type))
1✔
5940
         diag_hint(d, tree_loc(test), "type %s is not a character array",
1✔
5941
                   type_pp(type));
5942
      else
UNCOV
5943
         diag_hint(d, tree_loc(test), "type is %s", type_pp(type));
×
5944
      diag_lrm(d, STD_08, "10.9");
2✔
5945
      diag_emit(d);
2✔
5946
      return false;
2✔
5947
   }
5948

5949
   if (is_1d_character_array && standard() < STD_08) {
605✔
5950
      // VHDL-93 requires a locally static subtype, relaxed in later
5951
      // revisions
5952
      if (!sem_static_subtype(type, sem_locally_static))
54✔
5953
         sem_error(test, "case expression must have locally static subtype");
2✔
5954
   }
5955

5956
   static_fn_t static_fn = sem_locally_static;
603✔
5957
   const char *static_str = "locally";
603✔
5958

5959
   if (tree_kind(t) == T_CASE_GENERATE) {
603✔
5960
      static_fn = sem_globally_static;
10✔
5961
      static_str = "globally";
10✔
5962
   }
5963

5964
   const int nstmts = tree_stmts(t);
603✔
5965
   for (int i = 0; i < nstmts; i++) {
3,314✔
5966
      tree_t alt = tree_stmt(t, i);
2,725✔
5967

5968
      const int nassocs = tree_assocs(alt);
2,725✔
5969
      for (int j = 0; j < nassocs; j++) {
6,057✔
5970
         tree_t a = tree_assoc(alt, j);
3,346✔
5971
         switch (tree_subkind(a)) {
3,346✔
5972
         case A_OTHERS:
426✔
5973
            if (j != nassocs - 1 || i != nstmts - 1) {
426✔
5974
               diag_t *d = diag_new(DIAG_ERROR, tree_loc(a));
1✔
5975
               diag_printf(d, "others choice must appear last");
1✔
5976
               diag_hint(d, tree_loc(a), "others choice");
1✔
5977

5978
               tree_t more = j + 1 < nassocs
2✔
5979
                  ? tree_assoc(alt, j + 1) : tree_assoc(tree_stmt(t, i + 1), 0);
1✔
5980
               diag_hint(d, tree_loc(more), "further choices follow this");
1✔
5981

5982
               diag_emit(d);
1✔
5983
               return false;
1✔
5984
            }
5985
            break;
5986

5987
         case A_NAMED:
2,877✔
5988
            {
5989
               tree_t name = tree_name(a);
2,877✔
5990
               if (!sem_check(name, tab))
2,877✔
5991
                  return false;
5992

5993
               if (!sem_check_type(name, type, tab))
2,877✔
5994
                  sem_error(name, "case choice must have type %s but found %s",
1✔
5995
                            type_pp(type), type_pp(tree_type(name)));
5996
               else if (!(*static_fn)(name))
2,876✔
5997
                  sem_error(name, "case choice must be %s static", static_str);
9✔
5998
            }
5999
            break;
6000

6001
         case A_RANGE:
43✔
6002
            {
6003
               tree_t r = tree_range(a, 0);
43✔
6004
               if (!sem_check_discrete_range(r, type, tab))
43✔
6005
                  return false;
6006

6007
               switch (tree_subkind(r)) {
41✔
6008
               case RANGE_TO:
33✔
6009
               case RANGE_DOWNTO:
6010
                  if (!(*static_fn)(tree_left(r)))
33✔
UNCOV
6011
                     sem_error(tree_left(r), "left index of case choice "
×
6012
                               "range is not %s static", static_str);
6013
                  else if (!(*static_fn)(tree_right(r)))
33✔
6014
                     sem_error(tree_right(r), "right index of case choice "
1✔
6015
                               "range is not %s static", static_str);
6016
                  break;
6017

6018
               case RANGE_EXPR:
8✔
6019
                  if (!(*static_fn)(tree_value(r)))
8✔
UNCOV
6020
                     sem_error(tree_value(r), "range expression is not %s "
×
6021
                               "static", static_str);
6022
                  break;
6023

6024
               default:
6025
                  return false;
6026
               }
6027
            }
6028
            break;
6029
         }
6030
      }
6031
   }
6032

6033
   return true;
6034
}
6035

6036
static bool sem_check_match_case(tree_t t, nametab_t *tab)
43✔
6037
{
6038
   // Matching case statement is in LRM 08 section 10.9
6039

6040
   if (!sem_check_case(t, tab))
43✔
6041
      return false;
6042

6043
   tree_t value = tree_value(t);
42✔
6044
   type_t type = tree_type(value);
42✔
6045

6046
   type_t std_bit = std_type(NULL, STD_BIT);
42✔
6047
   type_t std_logic = ieee_type(IEEE_STD_ULOGIC);
42✔
6048

6049
   type_t elem = type;
42✔
6050
   if (type_is_array(type) && dimension_of(type) == 1)
42✔
6051
      elem = type_elem(type);
34✔
6052

6053
   if (!type_eq(elem, std_bit) && !type_eq(elem, std_logic)) {
42✔
6054
      diag_t *d = diag_new(DIAG_ERROR, tree_loc(value));
1✔
6055
      diag_printf(d, "type of expression in a matching case statement must be "
1✔
6056
                  "BIT, STD_ULOGIC, or a one-dimensional array of these types");
6057
      diag_hint(d, tree_loc(value), "type is %s", type_pp(type));
1✔
6058
      diag_lrm(d, STD_08, "10.9");
1✔
6059
      diag_emit(d);
1✔
6060
      return false;
1✔
6061
   }
6062

6063
   return true;
6064
}
6065

6066
static bool sem_check_return(tree_t t, nametab_t *tab)
13,031✔
6067
{
6068
   tree_t sub = find_enclosing(tab, S_SUBPROGRAM);
13,031✔
6069
   if (sub == NULL)
13,031✔
6070
      sem_error(t, "return statement not allowed outside subprogram");
3✔
6071

6072
   if (tree_has_value(t)) {
13,028✔
6073
      if (tree_kind(sub) == T_PROC_BODY)
12,683✔
6074
         sem_error(t, "cannot return a value from a procedure");
1✔
6075

6076
      tree_t value = tree_value(t);
12,682✔
6077
      if (!sem_check(value, tab))
12,682✔
6078
         return false;
6079

6080
      type_t expect = tree_type(t);
12,676✔
6081
      if (!sem_check_type(value, expect, tab))
12,676✔
6082
         sem_error(t, "expected return type %s but have %s",
2✔
6083
                   type_pp(expect), type_pp(tree_type(value)));
6084
   }
6085
   else if (tree_kind(sub) == T_FUNC_BODY)
345✔
6086
      sem_error(t, "return in function must have an expression");
1✔
6087

6088
   return true;
6089
}
6090

6091
static bool sem_check_cond_return(tree_t t, nametab_t *tab)
7✔
6092
{
6093
   tree_t sub = find_enclosing(tab, S_SUBPROGRAM);
7✔
6094
   if (sub == NULL)
7✔
6095
      sem_error(t, "return statement not allowed outside subprogram");
1✔
6096

6097
   if (tree_kind(sub) != T_PROC_BODY)
6✔
6098
      sem_error(t, "conditional return statement without value is only "
1✔
6099
                "valid inside a procedure");
6100

6101
   tree_t value = tree_value(t);
5✔
6102
   if (!sem_check(value, tab))
5✔
6103
      return false;
6104

6105
   type_t std_bool = std_type(NULL, STD_BOOLEAN);
5✔
6106

6107
   if (!sem_check_type(value, std_bool, tab))
5✔
6108
      sem_error(value, "type of condition must be %s but have %s",
1✔
6109
                type_pp(std_bool), type_pp(tree_type(value)));
6110

6111
   return true;
6112
}
6113

6114
static bool sem_check_while(tree_t t, nametab_t *tab)
201✔
6115
{
6116
   type_t std_bool = std_type(NULL, STD_BOOLEAN);
201✔
6117

6118
   tree_t value = tree_value(t);
201✔
6119
   if (!sem_check(value, tab))
201✔
6120
      return false;
6121

6122
   if (!sem_check_type(value, std_bool, tab))
201✔
6123
      sem_error(value, "type of loop condition must be %s but is %s",
1✔
6124
                type_pp(std_bool), type_pp(tree_type(value)));
6125

6126
   return true;
6127
}
6128

6129
static bool sem_check_for(tree_t t, nametab_t *tab)
2,553✔
6130
{
6131
   if (!sem_check_discrete_range(tree_range(t, 0), NULL, tab))
2,553✔
6132
      return false;
6133

6134
   tree_t idecl = tree_decl(t, 0);
2,539✔
6135

6136
   if (!sem_check_subtype(idecl, tree_type(idecl), tab))
2,539✔
UNCOV
6137
      return false;
×
6138

6139
   return true;
6140
}
6141

6142
static bool sem_check_block(tree_t t, nametab_t *tab)
384✔
6143
{
6144
   if (!sem_check_generic_map(t, t, tab))
384✔
6145
      return false;
6146

6147
   if (!sem_check_port_map(t, t, tab))
379✔
6148
      return false;
5✔
6149

6150
   return true;
6151
}
6152

6153
static bool sem_check_loop_control(tree_t t, nametab_t *tab)
412✔
6154
{
6155
   if (tree_has_value(t)) {
412✔
6156
      tree_t value = tree_value(t);
195✔
6157
      if (!sem_check(value, tab))
195✔
6158
         return false;
6159

6160
      type_t std_bool = std_type(NULL, STD_BOOLEAN);
195✔
6161
      if (!type_eq(tree_type(value), std_bool))
195✔
6162
         sem_error(value, "type of %s condition must be %s but is %s",
3✔
6163
                   (tree_kind(t) == T_EXIT) ? "exit" : "next",
6164
                   type_pp(std_bool), type_pp(tree_type(value)));
6165
   }
6166

6167
   return true;
6168
}
6169

6170
static bool sem_check_attr_decl(tree_t t)
118✔
6171
{
6172
   if (!sem_no_access_file_or_protected(t, tree_type(t), "attributes"))
118✔
6173
      return false;
5✔
6174

6175
   return true;
6176
}
6177

6178
static bool sem_check_attr_spec(tree_t t, nametab_t *tab)
342✔
6179
{
6180
   tree_t value = tree_value(t);
342✔
6181
   if (!sem_check(value, tab))
342✔
6182
      return false;
6183

6184
   type_t type = tree_type(t);
342✔
6185
   if (!sem_check_type(value, type, tab))
342✔
6186
      sem_error(t, "expected attribute specification for %s to have type %s "
2✔
6187
                "but found %s", istr(tree_ident(t)), type_pp(type),
6188
                type_pp(tree_type(value)));
6189

6190
   if (!tree_has_ref(t))
340✔
6191
      return false;
6192

6193
   tree_t decl = tree_ref(t);
325✔
6194
   const class_t class = tree_class(t);
325✔
6195

6196
   if (class_of(decl) != class)
325✔
6197
      sem_error(t, "class of object %s is %s not %s",
1✔
6198
                istr(tree_ident(decl)), class_str(class_of(decl)),
6199
                class_str(class));
6200

6201
   switch (is_well_known(tree_ident(t))) {
324✔
6202
   case W_NEVER_WAITS:
110✔
6203
      {
6204
         bool flag;
110✔
6205
         if (!folded_bool(value, &flag))
110✔
6206
            sem_error(value, "expression must be a BOOLEAN literal");
2✔
6207
         else if (class != C_PROCEDURE)
109✔
6208
            sem_error(t, "NEVER_WAITS attribute can only be applied to "
1✔
6209
                      "procedures");
6210
         else if (flag && !tree_frozen(decl))
108✔
6211
            tree_set_flag(decl, TREE_F_NEVER_WAITS);
107✔
6212
      }
6213
      break;
108✔
6214

6215
   case W_FOREIGN:
72✔
6216
      if (!tree_frozen(decl))
72✔
6217
         tree_set_flag(decl, TREE_F_NEVER_WAITS);
48✔
6218
      break;
6219

6220
   default:
6221
      break;
6222
   }
6223

6224
   return true;
6225
}
6226

6227
static bool sem_check_if_generate(tree_t t, nametab_t *tab)
167✔
6228
{
6229
   const int nconds = tree_conds(t);
167✔
6230
   for (int i = 0; i < nconds; i++) {
341✔
6231
      tree_t cond = tree_cond(t, i);
177✔
6232

6233
      if (!sem_check_cond(cond, tab))
177✔
6234
         return false;
6235

6236
      if (tree_has_value(cond)) {
175✔
6237
         tree_t value = tree_value(cond);
166✔
6238
         if (!sem_globally_static(value))
166✔
6239
            sem_error(value, "condition of generate statement must be static");
175✔
6240
      }
6241
   }
6242

6243
   return true;
6244
}
6245

6246
static bool sem_check_for_generate(tree_t t, nametab_t *tab)
164✔
6247
{
6248
   tree_t r = tree_range(t, 0);
164✔
6249
   if (!sem_check_discrete_range(r, NULL, tab))
164✔
6250
      return false;
6251

6252
   if (!sem_globally_static(r))
162✔
6253
      sem_error(r, "range of generate statement must be static");
1✔
6254

6255
   tree_t idecl = tree_decl(t, 0);
161✔
6256
   assert(tree_kind(idecl) == T_GENERIC_DECL);
161✔
6257

6258
   if (!sem_check_subtype(idecl, tree_type(idecl), tab))
161✔
UNCOV
6259
      return false;
×
6260

6261
   return true;
6262
}
6263

6264
static bool sem_check_open(tree_t t)
75✔
6265
{
6266
   return true;
75✔
6267
}
6268

6269
static bool sem_check_file_decl(tree_t t, nametab_t *tab)
154✔
6270
{
6271
   // Rules for file declarations are in LRM 93 section 4.3.1.4
6272

6273
   if (!type_is_file(tree_type(t)))
154✔
6274
      sem_error(t, "file declarations must have file type");
1✔
6275

6276
   if (tree_has_value(t)) {
153✔
6277
      tree_t value = tree_value(t);
46✔
6278
      if (!sem_check(value, tab))
46✔
6279
         return false;
6280

6281
      if (!sem_check_type(value, std_type(NULL, STD_STRING), tab))
46✔
6282
         sem_error(value, "file name must have type STRING");
1✔
6283

6284
      tree_t mode = tree_file_mode(t);
45✔
6285
      if (!sem_check(mode, tab))
45✔
6286
         return false;
6287

6288
      if (!sem_check_type(mode, std_type(NULL, STD_FILE_OPEN_KIND), tab))
44✔
6289
         sem_error(mode, "open mode must have type FILE_OPEN_KIND");
1✔
6290
   }
6291

6292
   tree_t sub = find_enclosing(tab, S_SUBPROGRAM);
150✔
6293
   const bool in_pure_func =
300✔
6294
      sub != NULL && tree_kind(sub) == T_FUNC_BODY
25✔
6295
      && !(tree_flags(sub) & TREE_F_IMPURE);
153✔
6296

6297
   if (in_pure_func) {
150✔
6298
      diag_t *d = pedantic_diag(tree_loc(t));
1✔
6299
      if (d != NULL) {
1✔
6300
         diag_printf(d, "cannot declare a file object in a pure function");
1✔
6301
         diag_emit(d);
1✔
6302
      }
6303
   }
6304

6305
   return true;
6306
}
6307

6308
static bool sem_check_new(tree_t t, nametab_t *tab)
542✔
6309
{
6310
   // Rules for allocators are in LRM 93 section 7.3.6
6311

6312
   tree_t value = tree_value(t);
542✔
6313
   type_t access_type = tree_type(t);
542✔
6314

6315
   if (type_is_none(access_type))
542✔
6316
      return false;
6317

6318
   assert(type_is_access(access_type));
541✔
6319
   assert(tree_kind(value) == T_QUALIFIED);
541✔
6320

6321
   if (!sem_check(value, tab))
541✔
6322
      return false;
6323

6324
   type_t type = tree_type(value);
541✔
6325

6326
   if (type_is_none(type))
541✔
6327
      return false;
6328

6329
   if (!sem_check_subtype(value, type, tab))
538✔
6330
      return false;
6331

6332
   const bool has_initial = tree_has_value(value);
538✔
6333

6334
   if (!has_initial && type_is_unconstrained(type))
538✔
6335
      sem_error(t, "unconstrained array type %s not allowed in allocator "
1✔
6336
                "expression", type_pp(type));
6337
   else if (!sem_check_incomplete(t, type))
537✔
6338
      return false;
6339
   else if (type_is_protected(type)) {
536✔
6340
      if (has_initial)
11✔
6341
         sem_error(t, "protected type %s cannot have initial value",
1✔
6342
                   type_pp(type));
6343
      else if (standard() >= STD_19)
10✔
6344
         tree_set_global_flags(t, TREE_GF_INSTANCE_NAME | TREE_GF_PATH_NAME);
10✔
6345
   }
6346

6347
   type_t designated = type_designated(access_type);
535✔
6348

6349
   if (!type_eq(type, designated))
535✔
6350
      sem_error(value, "type of allocator expresion %s does not match "
1✔
6351
                "access type %s", type_pp(type), type_pp(designated));
6352

6353
   return true;
6354
}
6355

6356
static bool sem_check_all(tree_t t, nametab_t *tab)
2,351✔
6357
{
6358
   tree_t value = tree_value(t);
2,351✔
6359
   if (!sem_check(value, tab))
2,351✔
6360
      return false;
6361

6362
   if (!sem_check_name_prefix(value, tab, "a selected name"))
2,349✔
6363
      return false;
6364

6365
   type_t value_type = tree_type(value);
2,349✔
6366

6367
   if (type_is_none(value_type))
2,349✔
6368
      return false;
6369

6370
   if (!sem_check_readable(value))
2,349✔
6371
      return false;
6372

6373
   if (!type_is_access(value_type)) {
2,348✔
6374
      diag_t *d = diag_new(DIAG_ERROR, tree_loc(value));
2✔
6375
      diag_printf(d, "prefix of a selected name with suffix ALL must "
2✔
6376
                  "have access type");
6377
      diag_hint(d, tree_loc(value), "prefix has type %s", type_pp(value_type));
2✔
6378
      diag_lrm(d, STD_08, "8.3");
2✔
6379
      diag_emit(d);
2✔
6380
      return false;
2✔
6381
   }
6382

6383
   return true;
6384
}
6385

6386
static bool sem_check_binding(tree_t t, nametab_t *tab)
203✔
6387
{
6388
   if (!tree_has_ref(t))
203✔
6389
      return false;
6390

6391
   tree_t unit = primary_unit_of(tree_ref(t));
199✔
6392
   if (tree_kind(unit) == T_ENTITY) {
199✔
6393
      if (tree_genmaps(t) > 0 && !sem_check_generic_map(t, unit, tab))
199✔
6394
         return false;
6395

6396
      if (tree_params(t) > 0 && !sem_check_port_map(t, unit, tab))
199✔
UNCOV
6397
         return false;
×
6398
   }
6399

6400
   return true;
6401
}
6402

6403
static bool sem_check_block_config(tree_t t, nametab_t *tab)
168✔
6404
{
6405
   return true;
168✔
6406
}
6407

6408
static bool sem_check_spec(tree_t t, nametab_t *tab)
216✔
6409
{
6410
   if (!tree_has_ref(t))
216✔
6411
      return false;
6412

6413
   tree_t comp = tree_ref(t);
212✔
6414
   assert(tree_kind(comp) == T_COMPONENT);
212✔
6415

6416
   if (!tree_has_value(t))
212✔
6417
      return true;
6418

6419
   tree_t bind = tree_value(t);
203✔
6420
   assert(tree_kind(bind) == T_BINDING);
203✔
6421

6422
   if (!sem_check(bind, tab))
203✔
6423
      return false;
6424

6425
   tree_t unit = tree_ref(bind);
199✔
6426
   tree_t entity = primary_unit_of(unit);
199✔
6427
   assert(tree_kind(entity) == T_ENTITY);
199✔
6428

6429
   bool ok = true;
199✔
6430

6431
   if (tree_genmaps(bind) == 0) {
199✔
6432
      const int c_ngenerics = tree_generics(comp);
145✔
6433
      const int e_ngenerics = tree_generics(entity);
145✔
6434

6435
      bit_mask_t have;
145✔
6436
      mask_init(&have, e_ngenerics);
145✔
6437

6438
      bool have_named = false;
145✔
6439
      for (int i = 0; i < c_ngenerics; i++) {
327✔
6440
         tree_t cg = tree_generic(comp, i);
182✔
6441

6442
         int epos = 0;
182✔
6443
         tree_t match = NULL;
182✔
6444
         for (; epos < e_ngenerics; epos++) {
2,440✔
6445
            tree_t eg = tree_generic(entity, epos);
2,256✔
6446
            if (ident_casecmp(tree_ident(eg), tree_ident(cg))) {
2,256✔
6447
               match = eg;
180✔
6448
               mask_set(&have, epos);
180✔
6449
               break;
6450
            }
6451
         }
6452

6453
         if (match == NULL) {
182✔
6454
            diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
2✔
6455
            diag_printf(d, "generic %s in component %s has no corresponding "
2✔
6456
                        "generic in entity %s", istr(tree_ident(cg)),
6457
                        istr(tree_ident(comp)), istr(tree_ident(entity)));
6458
            diag_hint(d, tree_loc(cg), "generic %s declared here",
2✔
6459
                      istr(tree_ident(cg)));
6460
            diag_emit(d);
2✔
6461

6462
            ok = false;
2✔
6463
            continue;
2✔
6464
         }
6465

6466
         tree_t value;
180✔
6467
         if (tree_class(cg) == C_PACKAGE) {
180✔
6468
            tree_t pmap = tree_value(cg);
8✔
6469
            assert(tree_kind(pmap) == T_PACKAGE_MAP);
8✔
6470

6471
            tree_t expect = tree_value(match);
8✔
6472
            assert(tree_kind(expect) == T_PACKAGE_MAP);
8✔
6473

6474
            if (tree_ref(pmap) != tree_ref(expect)) {
8✔
6475
               diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
1✔
6476
               diag_printf(d, "generic package %s in component %s does not "
1✔
6477
                           "match entity %s", istr(tree_ident(cg)),
6478
                           istr(tree_ident(comp)), istr(tree_ident(entity)));
6479
               diag_hint(d, tree_loc(cg), "declaration of generic %s in "
1✔
6480
                         "component as instance of %s", istr(tree_ident(cg)),
6481
                         istr(tree_ident(pmap)));
6482
               diag_hint(d, tree_loc(match), "declaration of generic %s in "
1✔
6483
                         "entity as instance of %s", istr(tree_ident(match)),
6484
                         istr(tree_ident(expect)));
6485
               diag_emit(d);
1✔
6486

6487
               ok = false;
1✔
6488
               continue;
1✔
6489
            }
6490

6491
            value = tree_new(T_REF);
7✔
6492
            tree_set_ident(value, tree_ident(cg));
7✔
6493
            tree_set_ref(value, cg);
7✔
6494
         }
6495
         else {
6496
            type_t ctype = tree_type(cg);
172✔
6497
            type_t etype = tree_type(match);
172✔
6498
            if (!type_eq(ctype, etype)) {
172✔
6499
               diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
1✔
6500
               diag_printf(d, "generic %s in component %s has type %s which "
1✔
6501
                           "is incompatible with type %s in entity %s",
6502
                           istr(tree_ident(cg)), istr(tree_ident(comp)),
6503
                           type_pp2(ctype, etype), type_pp2(etype, ctype),
6504
                           istr(tree_ident(entity)));
6505
               diag_hint(d, tree_loc(cg), "declaration of generic %s in "
1✔
6506
                         "component", istr(tree_ident(cg)));
6507
               diag_hint(d, tree_loc(match), "declaration of generic %s in "
1✔
6508
                         "entity", istr(tree_ident(match)));
6509
               diag_emit(d);
1✔
6510

6511
               ok = false;
1✔
6512
               continue;
1✔
6513
            }
6514

6515
            value = make_ref(cg);
171✔
6516
         }
6517

6518
         tree_t map = tree_new(T_PARAM);
178✔
6519
         tree_set_loc(map, tree_loc(t));
178✔
6520
         tree_set_value(map, value);
178✔
6521

6522
         if (!have_named && epos == i) {
178✔
6523
            tree_set_subkind(map, P_POS);
99✔
6524
            tree_set_pos(map, epos);
99✔
6525
         }
6526
         else {
6527
            tree_set_subkind(map, P_NAMED);
79✔
6528
            tree_set_name(map, make_ref(match));
79✔
6529
            have_named = true;
79✔
6530
         }
6531

6532
         tree_add_genmap(bind, map);
178✔
6533
      }
6534

6535
      for (int i = 0; i < e_ngenerics; i++) {
356✔
6536
         tree_t eg = tree_generic(entity, i);
211✔
6537
         if (!mask_test(&have, i) && !tree_has_value(eg)) {
211✔
6538
            diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
3✔
6539
            diag_printf(d, "generic %s in entity %s without a default value "
3✔
6540
                        "has no corresponding generic in component %s",
6541
                        istr(tree_ident(eg)),  istr(tree_ident(entity)),
6542
                        istr(tree_ident(comp)));
6543
            diag_hint(d, tree_loc(eg), "generic %s declared here",
3✔
6544
                      istr(tree_ident(eg)));
6545
            diag_emit(d);
3✔
6546

6547
            ok = false;
3✔
6548
            continue;
3✔
6549
         }
6550
      }
6551

6552
      mask_free(&have);
145✔
6553
   }
6554

6555
   if (tree_params(bind) == 0) {
199✔
6556
      const int c_nports = tree_ports(comp);
138✔
6557
      const int e_nports = tree_ports(entity);
138✔
6558

6559
      bit_mask_t have;
138✔
6560
      mask_init(&have, e_nports);
138✔
6561

6562
      bool have_named = false;
138✔
6563
      for (int i = 0; i < c_nports; i++) {
985✔
6564
         tree_t cp = tree_port(comp, i);
847✔
6565

6566
         int epos = 0;
847✔
6567
         tree_t match = NULL;
847✔
6568
         for (; match == NULL && epos < e_nports; epos++) {
8,094✔
6569
            tree_t ep = tree_port(entity, epos);
7,242✔
6570
            if (ident_casecmp(tree_ident(ep), tree_ident(cp))) {
7,242✔
6571
               match = ep;
842✔
6572
               mask_set(&have, epos);
842✔
6573
               break;
6574
            }
6575
         }
6576

6577
         if (match == NULL) {
847✔
6578
            diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
5✔
6579
            diag_printf(d, "port %s in component %s has no corresponding port "
5✔
6580
                        "in entity %s", istr(tree_ident(cp)),
6581
                        istr(tree_ident(comp)), istr(tree_ident(entity)));
6582
            diag_hint(d, tree_loc(cp), "port %s declared here",
5✔
6583
                      istr(tree_ident(cp)));
6584
            diag_emit(d);
5✔
6585

6586
            ok = false;
5✔
6587
            continue;
5✔
6588
         }
6589

6590
         type_t ctype = tree_type(cp);
842✔
6591
         type_t etype = tree_type(match);
842✔
6592
         if (!type_eq(ctype, etype)) {
842✔
6593
            diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
1✔
6594
            diag_printf(d, "port %s in component %s has type %s which is "
1✔
6595
                        "incompatible with type %s in entity %s",
6596
                        istr(tree_ident(cp)), istr(tree_ident(comp)),
6597
                        type_pp2(ctype, etype), type_pp2(etype, ctype),
6598
                        istr(tree_ident(entity)));
6599
            diag_hint(d, tree_loc(cp), "declaration of port %s in component",
1✔
6600
                      istr(tree_ident(cp)));
6601
            diag_hint(d, tree_loc(match), "declaration of port %s in entity",
1✔
6602
                      istr(tree_ident(match)));
6603
            diag_emit(d);
1✔
6604

6605
            ok = false;
1✔
6606
            continue;
1✔
6607
         }
6608

6609
         if (!have_named && epos == i)
841✔
6610
            add_param(bind, make_ref(cp), P_POS, NULL);
823✔
6611
         else {
6612
            add_param(bind, make_ref(cp), P_NAMED, make_ref(match));
18✔
6613
            have_named = true;
18✔
6614
         }
6615
      }
6616

6617
      for (int i = 0; i < e_nports; i++) {
987✔
6618
         if (mask_test(&have, i))
849✔
6619
            continue;
842✔
6620

6621
         tree_t ep = tree_port(entity, i);
7✔
6622

6623
         const bool open_ok =
14✔
6624
            tree_has_value(ep)
7✔
6625
            || (tree_subkind(ep) == PORT_OUT
7✔
6626
                && !type_is_unconstrained(tree_type(ep)));
1✔
6627

6628
         if (!open_ok) {
7✔
6629
            diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
2✔
6630
            diag_printf(d, "port %s in entity %s without a default value "
2✔
6631
                        "has no corresponding port in component %s",
6632
                        istr(tree_ident(ep)), istr(tree_ident(entity)),
6633
                        istr(tree_ident(comp)));
6634
            diag_hint(d, tree_loc(ep), "port %s declared here",
2✔
6635
                      istr(tree_ident(ep)));
6636
            diag_emit(d);
2✔
6637

6638
            ok = false;
2✔
6639
            continue;
2✔
6640
         }
6641
      }
6642
   }
6643

6644
   return ok;
6645
}
6646

6647
static bool sem_check_configuration(tree_t t, nametab_t *tab)
116✔
6648
{
6649
   if (!tree_has_primary(t))
116✔
6650
      return false;   // Was parse error
6651

6652
   tree_t of = tree_primary(t);
115✔
6653

6654
   // LRM 08 section 3.4.1: for a configuration of a given design
6655
   // entity, both the configuration declaration and the corresponding
6656
   // entity declaration shall reside in the same library
6657
   ident_t elib = ident_until(tree_ident(of), '.');
115✔
6658
   if (standard() < STD_19 && elib != lib_name(lib_work())) {
115✔
6659
      diag_t *d = pedantic_diag(tree_loc(t));
1✔
6660
      if (d != NULL) {
1✔
6661
         ident_t ename = ident_rfrom(tree_ident(of), '.');
1✔
6662
         diag_printf(d, "configuration declaration %s must reside in the "
1✔
6663
                     "same library as entity %s", istr(tree_ident(t)),
6664
                     istr(ename));
6665
         diag_hint(d, NULL, "entity %s is in library %s which is not the same "
1✔
6666
                   "as the current working library %s",
6667
                   istr(ename), istr(elib), istr(lib_name(lib_work())));
6668
         diag_lrm(d, STD_08, "3.4");
1✔
6669
         diag_emit(d);
1✔
6670
         return false;
1✔
6671
      }
6672
   }
6673

6674
   return true;
6675
}
6676

6677
static bool sem_check_prot_body(tree_t t, nametab_t *tab)
208✔
6678
{
6679
   // Rules for protected type bodies are in LRM 00 section 3.5.2
6680

6681
   type_t type = tree_type(t);
208✔
6682
   if (type_is_none(type))
208✔
6683
      return false;
6684

6685
   if (!sem_check_missing_bodies(t, tab))
202✔
UNCOV
6686
      return false;
×
6687

6688
   return true;
6689
}
6690

6691
static bool sem_check_implicit_signal(tree_t t, nametab_t *tab)
27✔
6692
{
6693
   tree_t value = tree_value(t);
27✔
6694
   type_t type  = tree_type(t);
27✔
6695

6696
   if (!sem_check(value, tab))
27✔
6697
      return false;
6698

6699
   switch (tree_subkind(t)) {
27✔
6700
   case IMPLICIT_GUARD:
27✔
6701
      if (!sem_check_type(value, type, tab))
27✔
6702
         sem_error(value, "guard expression must have type %s but "
1✔
6703
                   "found %s", type_pp2(type, tree_type(value)),
6704
                   type_pp2(tree_type(value), type));
6705
      break;
6706
   }
6707

6708
   return true;
6709
}
6710

6711
static bool sem_check_context_decl(tree_t t, nametab_t *tab)
19✔
6712
{
6713
   // Context declarations are in LRM 08 section 13.3
6714

6715
   if (!sem_check_context_clause(t, tab))
19✔
6716
      return false;
1✔
6717

6718
   return true;
6719
}
6720

6721
static bool sem_check_context_ref(tree_t t, nametab_t *tab)
21✔
6722
{
6723
   if (standard() >= STD_08) {
21✔
6724
      tree_t unit = find_enclosing(tab, S_DESIGN_UNIT);
21✔
6725

6726
      if (unit != NULL && tree_kind(unit) == T_CONTEXT) {
21✔
6727
         // LRM 08 section 13.3
6728
         ident_t prefix = ident_until(tree_ident(t), '.');
6✔
6729
         if (prefix == well_known(W_WORK))
6✔
6730
            sem_error(t, "selected name in context declaration context "
1✔
6731
                      "reference may not have WORK as a prefix");
6732
      }
6733
   }
6734

6735
   return true;
6736
}
6737

6738
static bool sem_check_disconnect(tree_t t, nametab_t *tab)
14✔
6739
{
6740
   if (!tree_has_ref(t))
14✔
6741
      return false;
6742

6743
   tree_t decl = tree_ref(t);
14✔
6744
   if (class_of(decl) != C_SIGNAL || !is_guarded_signal(decl))
14✔
6745
      sem_error(t, "signal name %s in disconnection specification must denote "
3✔
6746
                "a guarded signal", istr(tree_ident(t)));
6747

6748
   type_t type = tree_type(t);
11✔
6749
   if (!type_eq(tree_type(decl), type))
11✔
6750
      sem_error(t, "type of declared signal %s does not match type %s in "
1✔
6751
                "disconnection specification", type_pp(tree_type(decl)),
6752
                type_pp(type));
6753

6754
   tree_t delay = tree_delay(t);
10✔
6755
   type_t std_time = std_type(NULL, STD_TIME);
10✔
6756
   if (!sem_check_type(delay, std_time, tab))
10✔
6757
      sem_error(delay, "time expression in disconnection specification must "
1✔
6758
                "have type %s but found %s", type_pp(std_time),
6759
                type_pp(tree_type(delay)));
6760

6761
   if (!sem_globally_static(delay))
9✔
6762
      sem_error(delay, "time expression in disconnection specificiation "
1✔
6763
                "must be static");
6764

6765
   return true;
6766
}
6767

6768
static bool sem_check_conv_func(tree_t t, nametab_t *tab)
189✔
6769
{
6770
   if (type_is_none(tree_type(t)))
189✔
6771
      return false;
6772
   else if (!tree_has_ref(t))
189✔
6773
      return false;
6774

6775
   tree_t value = tree_value(t);
189✔
6776
   if (!sem_check(value, tab))
189✔
6777
      return false;
6778

6779
   tree_t decl = tree_ref(t);
189✔
6780
   assert(tree_ports(decl) == 1);
189✔
6781

6782
   tree_t formal = tree_port(decl, 0);
189✔
6783
   type_t ftype = tree_type(formal);
189✔
6784
   if (!sem_check_type(value, ftype, tab))
189✔
6785
      sem_error(value, "type of conversion function actual %s does not match "
2✔
6786
                "formal %s type %s", type_pp2(tree_type(value), ftype),
6787
                istr(tree_ident(formal)), type_pp2(ftype, tree_type(value)));
6788

6789
   return true;
6790
}
6791

6792
static bool sem_check_concurrent(tree_t t, nametab_t *tab)
3,339✔
6793
{
6794
   if (tree_stmts(t) == 0)
3,339✔
6795
      return false;   // Was parse error
6796

6797
   return sem_check(tree_stmt(t, 0), tab);
3,339✔
6798
}
6799

6800
static bool sem_check_external_name(tree_t t, nametab_t *tab)
183✔
6801
{
6802
   const int nparts = tree_parts(t);
183✔
6803
   for (int i = 0; i < nparts; i++) {
825✔
6804
      tree_t pe = tree_part(t, i);
644✔
6805
      switch (tree_subkind(pe)) {
644✔
6806
      case PE_GENERATE:
30✔
6807
         {
6808
            tree_t value = tree_value(pe);
30✔
6809
            if (!sem_globally_static(value))
30✔
6810
               sem_error(value, "generate index must be a static expression");
1✔
6811
         }
6812
         break;
6813
      case PE_RELATIVE:
133✔
6814
         // Relative pathnames must have enclosing concurrent region
6815
         if (find_enclosing(tab, S_CONCURRENT_BLOCK) == NULL) {
133✔
6816
            diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
1✔
6817
            diag_printf(d, "relative pathname has no enclosing "
1✔
6818
                        "concurrent region");
6819
            diag_lrm(d, STD_08, "8.7");
1✔
6820
            diag_emit(d);
1✔
6821
            return false;
1✔
6822
         }
6823
      }
6824
   }
6825

6826
   const class_t class = tree_class(t);
181✔
6827
   if (class != C_CONSTANT) {
181✔
6828
      tree_t sub = find_enclosing(tab, S_SUBPROGRAM);
146✔
6829

6830
      const bool is_pure_func =
292✔
6831
         sub != NULL
6832
         && tree_kind(sub) == T_FUNC_BODY
8✔
6833
         && !(tree_flags(sub) & TREE_F_IMPURE);
147✔
6834

6835
      if (is_pure_func)
146✔
6836
         sem_error(t, "cannot reference external name with class %s in pure "
1✔
6837
                   "function %s", class_str(class), istr(tree_ident(sub)));
6838
   }
6839

6840
   // Cannot do any more checking until elaboration
6841
   return true;
6842
}
6843

6844
static port_mode_t sem_default_force_mode(tree_t target)
87✔
6845
{
6846
   // Rules for default force mode in LRM 08 section 10.5.2.1
6847

6848
   tree_t ref = name_to_ref(target);
87✔
6849
   if (ref == NULL || !tree_has_ref(ref))
87✔
6850
      return PORT_IN;
14✔
6851

6852
   tree_t decl = tree_ref(ref);
73✔
6853
   const tree_kind_t dkind = tree_kind(decl);
73✔
6854
   if (dkind == T_PORT_DECL || dkind == T_PARAM_DECL) {
73✔
6855
      switch (tree_subkind(decl)) {
6✔
6856
      case PORT_OUT:
6857
      case PORT_INOUT:
6858
      case PORT_BUFFER:
6859
         return PORT_OUT;
UNCOV
6860
      default:
×
UNCOV
6861
         return PORT_IN;
×
6862
      }
6863
   }
6864

6865
   return PORT_IN;
6866
}
6867

6868
static bool sem_check_force_target(tree_t target, port_mode_t mode,
98✔
6869
                                   const char *what)
6870
{
6871
   tree_t decl = sem_check_lvalue(target);
98✔
6872
   if (decl == NULL)
98✔
6873
      sem_error(target, "target of simple %s assignment must be a "
1✔
6874
                "signal name", what);
6875

6876
   switch (tree_kind(decl)) {
97✔
6877
   case T_SIGNAL_DECL:
6878
      break;
6879

6880
   case T_PORT_DECL:
9✔
6881
   case T_PARAM_DECL:
6882
      if (tree_class(decl) != C_SIGNAL) {
9✔
6883
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(target));
×
6884
         diag_printf(d, "%s is not a valid target of simple %s assignment",
×
6885
                     what, istr(tree_ident(decl)));
6886
         diag_hint(d, tree_loc(target), "target of simple %s assignment", what);
×
6887
         diag_hint(d, tree_loc(decl), "declared with class %s",
×
6888
                   class_str(tree_class(decl)));
UNCOV
6889
         diag_emit(d);
×
UNCOV
6890
         return false;
×
6891
      }
6892
      else if (mode == PORT_OUT && tree_subkind(decl) == PORT_IN)
9✔
6893
         sem_error(target, "force mode OUT may not be used with target "
1✔
6894
                   "of mode IN");
6895
      break;
6896

6897
      case T_EXTERNAL_NAME:
32✔
6898
         if (tree_class(decl) != C_SIGNAL) {
32✔
6899
            tree_t tail = tree_part(decl, tree_parts(decl) - 1);
1✔
6900
            diag_t *d = diag_new(DIAG_ERROR, tree_loc(target));
1✔
6901
            diag_printf(d, "external name %s is not a valid target of "
1✔
6902
                        "simple %s assignment", istr(tree_ident(tail)), what);
6903
            diag_hint(d, tree_loc(target), "target of signal assignment");
1✔
6904
            diag_hint(d, tree_loc(decl), "declared with class %s",
1✔
6905
                      class_str(tree_class(decl)));
6906
            diag_emit(d);
1✔
6907
            return false;
1✔
6908
         }
6909
         break;
6910

6911
   case T_VAR_DECL:
2✔
6912
   case T_CONST_DECL:
6913
      {
6914
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(target));
2✔
6915
         diag_printf(d, "%s %s is not a valid target of simple %s assignment",
2✔
6916
                     class_str(class_of(decl)), istr(tree_ident(decl)), what);
6917
         diag_hint(d, tree_loc(target), "target of simple %s assignment", what);
2✔
6918
         diag_hint(d, tree_loc(decl), "declared as %s",
2✔
6919
                   class_str(class_of(decl)));
6920
         diag_emit(d);
2✔
6921
         return false;
2✔
6922
      }
6923

UNCOV
6924
   default:
×
UNCOV
6925
      sem_error(target, "invalid target of simple %s assignment", what);
×
6926
   }
6927

6928
   return true;
6929
}
6930

6931
static bool sem_check_force(tree_t t, nametab_t *tab)
64✔
6932
{
6933
   tree_t target = tree_target(t);
64✔
6934

6935
   if (!sem_check(target, tab))
64✔
6936
      return false;
6937

6938
   port_mode_t mode = tree_subkind(t);
63✔
6939
   if (mode == PORT_INVALID)
63✔
6940
      tree_set_subkind(t, (mode = sem_default_force_mode(target)));
54✔
6941

6942
   if (!sem_check_force_target(target, mode, "force"))
63✔
6943
      return false;
6944

6945
   tree_t value = tree_value(t);
60✔
6946
   if (!sem_check(value, tab))
60✔
6947
      return false;
6948

6949
   type_t expect = tree_type(target);
60✔
6950

6951
   if (!sem_check_type(value, expect, tab))
60✔
6952
      sem_error(t, "type of force expression %s does not match type of "
2✔
6953
                "target %s", type_pp2(tree_type(value), expect),
6954
                type_pp2(expect, tree_type(value)));
6955

6956
   return true;
6957
}
6958

6959
static bool sem_check_release(tree_t t, nametab_t *tab)
36✔
6960
{
6961
   tree_t target = tree_target(t);
36✔
6962

6963
   if (!sem_check(target, tab))
36✔
6964
      return false;
6965

6966
   port_mode_t mode = tree_subkind(t);
35✔
6967
   if (mode == PORT_INVALID)
35✔
6968
      tree_set_subkind(t, (mode = sem_default_force_mode(target)));
33✔
6969

6970
   if (!sem_check_force_target(target, mode, "release"))
35✔
6971
      return false;
2✔
6972

6973
   return true;
6974
}
6975

6976
static bool sem_check_prot_ref(tree_t t, nametab_t *tab)
15✔
6977
{
6978
   if (standard() >= STD_19)
15✔
6979
      return true;   // Alias of private variable method
6980

6981
   // There are no legal ways this can appear here and should always
6982
   // have been converted to a call
6983
   assert(error_count() > 0);
1✔
6984

6985
   return false;
6986
}
6987

6988
static bool sem_check_view_decl(tree_t t, nametab_t *tab)
57✔
6989
{
6990
   type_t type = tree_type(t);
57✔
6991
   if (type_is_none(type))
57✔
6992
      return false;
6993

6994
   assert(type_kind(type) == T_VIEW);
57✔
6995

6996
   type_t rtype = type_designated(type);
57✔
6997
   if (!type_is_record(rtype)) {
57✔
6998
      assert(error_count() > 0);   // Checked by parser
1✔
6999
      return false;
7000
   }
7001
   else if (type_is_resolved(rtype))
56✔
7002
      sem_error(t, "subtype indication of a mode view declaration "
1✔
7003
                "must denote an unresolved record type");
7004

7005
   const int nfields = type_fields(rtype);
55✔
7006

7007
   LOCAL_BIT_MASK have;
110✔
7008
   mask_init(&have, nfields);
55✔
7009

7010
   const int nelems = type_fields(type);
55✔
7011
   for (int i = 0; i < nelems; i++) {
171✔
7012
      tree_t e = type_field(type, i);
123✔
7013
      assert(tree_kind(e) == T_VIEW_ELEMENT);
123✔
7014

7015
      if (!tree_has_ref(e))
123✔
7016
         return false;
7017

7018
      tree_t f = tree_ref(e);
121✔
7019
      assert(tree_kind(f) == T_FIELD_DECL);
121✔
7020

7021
      const int pos = tree_pos(f);
121✔
7022
      if (mask_test(&have, pos))
121✔
7023
         sem_error(e, "duplicate mode view element definition for field %s",
1✔
7024
                   istr(tree_ident(e)));
7025

7026
      mask_set(&have, pos);
120✔
7027

7028
      switch (tree_subkind(e)) {
120✔
7029
      case PORT_LINKAGE:
1✔
7030
         sem_error(e, "element mode indication cannot have mode LINKAGE");
1✔
7031

7032
      case PORT_RECORD_VIEW:
8✔
7033
      case PORT_ARRAY_VIEW:
7034
         {
7035
            tree_t name = tree_value(e);
8✔
7036
            type_t type = tree_type(e);
8✔
7037
            type_t view_type = tree_type(name);
8✔
7038

7039
            if (type_is_none(view_type))
8✔
7040
               return false;
7041

7042
            if (type_kind(view_type) != T_VIEW)
8✔
7043
               sem_error(name, "name in element mode view indication of field "
1✔
7044
                         "%s does not denote a mode view", istr(tree_ident(f)));
7045

7046
            type_t elem_type = type;
7✔
7047
            if (tree_subkind(e) == PORT_ARRAY_VIEW) {
7✔
7048
               if (!type_is_array(type))
2✔
7049
                  sem_error(e, "field %s with array mode view indication has "
1✔
7050
                            "non-array type %s", istr(tree_ident(f)),
7051
                            type_pp(type));
7052

7053
               elem_type = type_elem(type);
1✔
7054
            }
7055

7056
            if (!type_eq(elem_type, type_designated(view_type)))
6✔
7057
               sem_error(e, "field %s subtype %s is not compatible with mode "
1✔
7058
                         "view %s", istr(tree_ident(f)), type_pp(elem_type),
7059
                         type_pp(view_type));
7060
         }
7061
         break;
7062
      }
7063
   }
7064

7065
   if (mask_popcount(&have) != nfields) {
48✔
7066
      LOCAL_TEXT_BUF tb = tb_new();
1✔
7067
      for (int i = 0, missing = 0; i < nfields; i++) {
5✔
7068
         if (!mask_test(&have, i))
4✔
7069
            tb_printf(tb, "%s%s", missing++ > 0 ? ", " : "",
3✔
7070
                      istr(tree_ident(type_field(rtype, i))));
7071
      }
7072

7073
      sem_error(t, "missing mode view element defintion for %s", tb_get(tb));
1✔
7074
   }
7075

7076
   return true;
7077
}
7078

7079
static bool sem_check_cond_value(tree_t t, nametab_t *tab)
50✔
7080
{
7081
   type_t type = tree_type(t);
50✔
7082
   if (type_is_none(type))
50✔
7083
      return false;
7084

7085
   type_t std_bool = std_type(NULL, STD_BOOLEAN);
50✔
7086

7087
   const int nconds = tree_conds(t);
50✔
7088
   for (int i = 0; i < nconds; i++) {
162✔
7089
      tree_t cond = tree_cond(t, i);
116✔
7090
      assert(tree_kind(cond) == T_COND_EXPR);
116✔
7091

7092
      if (tree_has_value(cond)) {
116✔
7093
         tree_t value = tree_value(cond);
72✔
7094
         if (!sem_check(value, tab))
72✔
7095
            return false;
7096

7097
         if (!sem_check_type(value, std_bool, tab))
72✔
7098
            sem_error(value, "type of condition must be %s but have %s",
2✔
7099
                      type_pp(std_bool), type_pp(tree_type(value)));
7100
      }
7101
      else
7102
         assert(i == nconds - 1);
44✔
7103

7104
      if (tree_has_result(cond)) {
114✔
7105
         tree_t result = tree_result(cond);
104✔
7106
         if (!sem_check(result, tab))
104✔
7107
            return false;
7108

7109
         if (!sem_check_type(result, type, tab))
104✔
7110
            sem_error(result, "expected type of conditional expression to be "
114✔
7111
                      "%s but is %s", type_pp(type),
7112
                      type_pp(tree_type(result)));
7113
      }
7114
   }
7115

7116
   return true;
7117
}
7118

7119
static bool sem_check_prot_decl(tree_t t, nametab_t *tab)
212✔
7120
{
7121
   const int ndecls = tree_decls(t);
212✔
7122
   for (int i = 0; i < ndecls; i++) {
687✔
7123
      tree_t d = tree_decl(t, i);
476✔
7124
      if (tree_kind(d) == T_ALIAS) {
476✔
7125
         // LRM 19 section 5.6.2: it is an error if an alias declared
7126
         // within a protected type declaration denotes anything other
7127
         // than a method of a protected type
7128
         tree_t value = tree_value(d);
15✔
7129
         if (tree_kind(value) != T_PROT_REF)
15✔
7130
            sem_error(d, "an alias declared within a protected type "
476✔
7131
                      "declaration must denote a protected type method");
7132
      }
7133
   }
7134

7135
   defer_check(tab, sem_missing_body_cb, t);
211✔
7136
   return true;
211✔
7137
}
7138

7139
static bool sem_check_inertial(tree_t t, nametab_t *tab)
15✔
7140
{
7141
   if (!sem_check(tree_value(t), tab))
15✔
UNCOV
7142
      return false;
×
7143

7144
   return true;
7145
}
7146

7147
static bool sem_check_psl_union(tree_t t, nametab_t *tab)
25✔
7148
{
7149
   psl_check(tree_psl(t), tab);
25✔
7150
   return true;
25✔
7151
}
7152

7153
bool sem_check(tree_t t, nametab_t *tab)
684,883✔
7154
{
7155
   switch (tree_kind(t)) {
684,883✔
7156
   case T_ARCH:
5,004✔
7157
      return sem_check_arch(t, tab);
5,004✔
7158
   case T_PACKAGE:
1,460✔
7159
      return sem_check_package(t, tab);
1,460✔
7160
   case T_ENTITY:
4,990✔
7161
      return sem_check_entity(t, tab);
4,990✔
7162
   case T_TYPE_DECL:
4,850✔
7163
      return sem_check_type_decl(t, tab);
4,850✔
7164
   case T_SUBTYPE_DECL:
1,450✔
7165
      return sem_check_subtype_decl(t, tab);
1,450✔
7166
   case T_PORT_DECL:
3,699✔
7167
      return sem_check_port_decl(t, tab);
3,699✔
7168
   case T_PARAM_DECL:
30,953✔
7169
      return sem_check_param_decl(t, tab);
30,953✔
7170
   case T_GENERIC_DECL:
2,283✔
7171
      return sem_check_generic_decl(t, tab);
2,283✔
7172
   case T_SIGNAL_DECL:
6,250✔
7173
      return sem_check_signal_decl(t, tab);
6,250✔
7174
   case T_VAR_DECL:
11,300✔
7175
      return sem_check_var_decl(t, tab);
11,300✔
7176
   case T_CONST_DECL:
6,723✔
7177
      return sem_check_const_decl(t, tab);
6,723✔
7178
   case T_PROCESS:
4,814✔
7179
      return sem_check_process(t, tab);
4,814✔
7180
   case T_VAR_ASSIGN:
16,499✔
7181
      return sem_check_var_assign(t, tab);
16,499✔
7182
   case T_SIGNAL_ASSIGN:
5,246✔
7183
      return sem_check_signal_assign(t, tab);
5,246✔
7184
   case T_FCALL:
83,494✔
7185
   case T_PROT_FCALL:
7186
      return sem_check_fcall(t, tab);
83,494✔
7187
   case T_LITERAL:
91,433✔
7188
      return sem_check_literal(t);
91,433✔
7189
   case T_STRING:
25,281✔
7190
      return sem_check_string_literal(t);
25,281✔
7191
   case T_REF:
174,986✔
7192
      return sem_check_ref(t, tab);
174,986✔
7193
   case T_WAIT:
9,461✔
7194
      return sem_check_wait(t, tab);
9,461✔
7195
   case T_ASSERT:
17,568✔
7196
      return sem_check_assert(t, tab);
17,568✔
7197
   case T_REPORT:
2,021✔
7198
      return sem_check_report(t, tab);
2,021✔
7199
   case T_QUALIFIED:
4,261✔
7200
      return sem_check_qualified(t, tab);
4,261✔
7201
   case T_FUNC_DECL:
6,000✔
7202
      return sem_check_func_decl(t, tab);
6,000✔
7203
   case T_AGGREGATE:
8,920✔
7204
      return sem_check_aggregate(t, tab);
8,920✔
7205
   case T_ATTR_REF:
20,973✔
7206
      return sem_check_attr_ref(t, false, tab);
20,973✔
7207
   case T_ARRAY_REF:
14,554✔
7208
      return sem_check_array_ref(t, tab);
14,554✔
7209
   case T_ARRAY_SLICE:
2,191✔
7210
      return sem_check_array_slice(t, tab);
2,191✔
7211
   case T_INSTANCE:
1,485✔
7212
      return sem_check_instance(t, tab);
1,485✔
7213
   case T_IF:
8,688✔
7214
      return sem_check_if(t, tab);
8,688✔
7215
   case T_NULL:
7216
      return true;
7217
   case T_PACK_BODY:
777✔
7218
      return sem_check_pack_body(t, tab);
777✔
7219
   case T_FUNC_BODY:
7,357✔
7220
      return sem_check_func_body(t, tab);
7,357✔
7221
   case T_RETURN:
13,031✔
7222
      return sem_check_return(t, tab);
13,031✔
7223
   case T_COND_RETURN:
7✔
7224
      return sem_check_cond_return(t, tab);
7✔
7225
   case T_COND_ASSIGN:
2,161✔
7226
      return sem_check_cond_assign(t, tab);
2,161✔
7227
   case T_WHILE:
201✔
7228
      return sem_check_while(t, tab);
201✔
7229
   case T_ALIAS:
1,560✔
7230
      return sem_check_alias(t, tab);
1,560✔
7231
   case T_FOR:
2,553✔
7232
      return sem_check_for(t, tab);
2,553✔
7233
   case T_PROC_DECL:
1,085✔
7234
      return sem_check_proc_decl(t, tab);
1,085✔
7235
   case T_PROC_BODY:
2,147✔
7236
      return sem_check_proc_body(t, tab);
2,147✔
7237
   case T_BLOCK:
384✔
7238
      return sem_check_block(t, tab);
384✔
7239
   case T_CASE:
558✔
7240
   case T_SELECT:
7241
      return sem_check_case(t, tab);
558✔
7242
   case T_EXIT:
412✔
7243
   case T_NEXT:
7244
      return sem_check_loop_control(t, tab);
412✔
7245
   case T_PCALL:
7,974✔
7246
   case T_PROT_PCALL:
7247
      return sem_check_pcall(t, tab);
7,974✔
7248
   case T_ATTR_SPEC:
342✔
7249
      return sem_check_attr_spec(t, tab);
342✔
7250
   case T_ATTR_DECL:
118✔
7251
      return sem_check_attr_decl(t);
118✔
7252
   case T_COMPONENT:
315✔
7253
      return sem_check_component(t, tab);
315✔
7254
   case T_IF_GENERATE:
167✔
7255
      return sem_check_if_generate(t, tab);
167✔
7256
   case T_FOR_GENERATE:
164✔
7257
      return sem_check_for_generate(t, tab);
164✔
7258
   case T_CASE_GENERATE:
10✔
7259
      return sem_check_case(t, tab);
10✔
7260
   case T_OPEN:
75✔
7261
      return sem_check_open(t);
75✔
7262
   case T_FIELD_DECL:
3,691✔
7263
      return sem_check_field_decl(t);
3,691✔
7264
   case T_FILE_DECL:
154✔
7265
      return sem_check_file_decl(t, tab);
154✔
7266
   case T_NEW:
542✔
7267
      return sem_check_new(t, tab);
542✔
7268
   case T_ALL:
2,351✔
7269
      return sem_check_all(t, tab);
2,351✔
7270
   case T_RECORD_REF:
7,734✔
7271
      return sem_check_record_ref(t, tab);
7,734✔
7272
   case T_UNIT_DECL:
157✔
7273
      return sem_check_unit_decl(t);
157✔
7274
   case T_USE:
15,064✔
7275
      return sem_check_use_clause(t, tab);
15,064✔
7276
   case T_TYPE_CONV:
5,371✔
7277
      return sem_check_conversion(t, tab);
5,371✔
7278
   case T_SPEC:
216✔
7279
      return sem_check_spec(t, tab);
216✔
7280
   case T_BINDING:
203✔
7281
      return sem_check_binding(t, tab);
203✔
7282
   case T_LIBRARY:
25,459✔
7283
      return sem_check_library_clause(t, tab);
25,459✔
7284
   case T_CONFIGURATION:
116✔
7285
      return sem_check_configuration(t, tab);
116✔
7286
   case T_PROT_BODY:
208✔
7287
      return sem_check_prot_body(t, tab);
208✔
7288
   case T_CONTEXT:
19✔
7289
      return sem_check_context_decl(t, tab);
19✔
7290
   case T_CONTEXT_REF:
21✔
7291
      return sem_check_context_ref(t, tab);
21✔
7292
   case T_BLOCK_CONFIG:
168✔
7293
      return sem_check_block_config(t, tab);
168✔
7294
   case T_IMPLICIT_SIGNAL:
27✔
7295
      return sem_check_implicit_signal(t, tab);
27✔
7296
   case T_DISCONNECT:
14✔
7297
      return sem_check_disconnect(t, tab);
14✔
7298
   case T_GROUP:
7299
   case T_GROUP_TEMPLATE:
7300
   case T_BOX:
7301
   case T_PSL_DIRECT:
7302
   case T_PSL_DECL:
7303
   case T_PSL_FCALL:
7304
   case T_LOOP:
7305
   case T_SEQUENCE:
7306
      return true;
7307
   case T_PSL_UNION:
25✔
7308
      return sem_check_psl_union(t, tab);
25✔
7309
   case T_CONV_FUNC:
189✔
7310
      return sem_check_conv_func(t, tab);
189✔
7311
   case T_CONCURRENT:
3,339✔
7312
      return sem_check_concurrent(t, tab);
3,339✔
7313
   case T_PACK_INST:
278✔
7314
      return sem_check_pack_inst(t, tab);
278✔
7315
   case T_EXTERNAL_NAME:
183✔
7316
      return sem_check_external_name(t, tab);
183✔
7317
   case T_FORCE:
64✔
7318
      return sem_check_force(t, tab);
64✔
7319
   case T_RELEASE:
36✔
7320
      return sem_check_release(t, tab);
36✔
7321
   case T_PROT_REF:
15✔
7322
      return sem_check_prot_ref(t, tab);
15✔
7323
   case T_MATCH_CASE:
43✔
7324
   case T_MATCH_SELECT:
7325
      return sem_check_match_case(t, tab);
43✔
7326
   case T_FUNC_INST:
129✔
7327
   case T_PROC_INST:
7328
      return sem_check_subprogram_inst(t, tab);
129✔
7329
   case T_VIEW_DECL:
57✔
7330
      return sem_check_view_decl(t, tab);
57✔
7331
   case T_COND_VALUE:
50✔
7332
      return sem_check_cond_value(t, tab);
50✔
7333
   case T_PROT_DECL:
212✔
7334
      return sem_check_prot_decl(t, tab);
212✔
7335
   case T_INERTIAL:
15✔
7336
      return sem_check_inertial(t, tab);
15✔
UNCOV
7337
   default:
×
UNCOV
7338
      sem_error(t, "cannot check %s", tree_kind_str(tree_kind(t)));
×
7339
   }
7340
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc