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

nickg / nvc / 15439237152

04 Jun 2025 09:48AM UTC coverage: 92.331% (+0.01%) from 92.321%
15439237152

push

github

nickg
Fix race reading JIT CFG

13 of 18 new or added lines in 4 files covered. (72.22%)

196 existing lines in 4 files now uncovered.

69104 of 74844 relevant lines covered (92.33%)

423598.46 hits per line

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

97.15
/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,506✔
132
{
133
   if (expect != NULL && type_is_none(expect))
21,506✔
134
      return false;   // Prevent cascading errors
135

136
   switch (tree_subkind(r)) {
21,485✔
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,745✔
162
   case RANGE_DOWNTO:
163
      {
164
         tree_t left = tree_left(r);
16,745✔
165
         if (!sem_check(left, tab))
16,745✔
166
            return false;
167

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

172
         if (expect != NULL) {
16,741✔
173
            if (!sem_check_same_type(left, right))
16,536✔
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,533✔
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,529✔
186
            sem_check_type(r, expect, tab);
16,529✔
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,101✔
196
{
197
   if (!sem_check_range(r, expect ?: tree_type(r), tab))
18,101✔
198
      return false;
199

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

205
   if (!type_is_discrete(type))
18,063✔
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,061✔
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,413✔
235
{
236
   const constraint_kind_t consk = tree_subkind(constraint);
15,413✔
237
   switch (consk) {
15,413✔
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,879✔
245
      if (!type_is_array(base))
11,879✔
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,067✔
293
      if (type_kind(base) == T_SUBTYPE && !type_is_unconstrained(base))
11,874✔
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,065✔
302
   const int ndims = tree_ranges(constraint);
15,065✔
303

304
   if (ndims != ndims_base)
15,065✔
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,462✔
309
      tree_t r = tree_range(constraint, i);
15,411✔
310
      type_t index = index_type_of(base, i);
15,411✔
311

312
      switch (consk) {
15,411✔
313
      case C_INDEX:
12,218✔
314
         if (!sem_check_discrete_range(r, index, tab))
12,218✔
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,941✔
332
{
333
   // Shared code for checking subtype declarations and implicit subtypes
334

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

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

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

348
   if (type_has_constraint(type)) {
15,929✔
349
      tree_t cons = type_constraint(type);
15,413✔
350
      if (!sem_check_constraint(cons, base, tab))
15,413✔
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,763✔
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,900✔
371
      type_t elem = type_elem(type);
324✔
372
      if (is_anonymous_subtype(elem)) {
324✔
373
         // Anonymous subtype created for array element constraint
374
         assert(standard() >= STD_08);
261✔
375

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

381
   if (type_has_resolution(type)) {
15,900✔
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,334✔
390
{
391
   // Check an anonymous subtype at the point of use
392

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

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

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

406
      if (unit != NULL && tree_kind(unit) == T_CONTEXT) {
5,665✔
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,513✔
419
{
420
   if (standard() >= STD_08) {
25,513✔
421
      ident_t name = tree_ident(t);
9,229✔
422
      tree_t unit = find_enclosing(tab, S_DESIGN_UNIT);
9,229✔
423

424
      if (unit != NULL && tree_kind(unit) == T_CONTEXT) {
9,229✔
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,269✔
436
{
437
   // Ignore the implicit WORK and STD with context declarations
438
   const int ignore = tree_kind(t) == T_CONTEXT ? 2 : 0;
12,269✔
439

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

445
   return ok;
12,269✔
446
}
447

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

456
         tree_t decl = tree_ref(t);
81,897✔
457
         switch (tree_kind(decl)) {
81,897✔
458
         case T_PORT_DECL:
1,937✔
459
            {
460
               const port_mode_t mode = tree_subkind(decl);
1,937✔
461
               if (mode == PORT_OUT && standard() < STD_08) {
1,937✔
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,928✔
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,085✔
481
            if (tree_subkind(decl) == PORT_OUT) {
22,085✔
482
               // LRM 08 section 6.5.2: The value of the interface
483
               // object is allowed to be updated and, provided it is
484
               // not a signal parameter, read.
485
               if (standard() < STD_08)
5✔
486
                  sem_error(t, "cannot read OUT parameter %s",
3✔
487
                            istr(tree_ident(t)));
488
               else if (tree_class(decl) == C_SIGNAL) {
2✔
489
                  diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
1✔
490
                  diag_printf(d, "cannot read OUT signal parameter %s",
1✔
491
                              istr(tree_ident(t)));
492
                  diag_hint(d, NULL, "OUT parameters can be read provided they "
1✔
493
                            "are not signal parameters");
494
                  diag_lrm(d, STD_08, "6.5.2");
1✔
495
                  diag_emit(d);
1✔
496
                  return false;
1✔
497
               }
498
            }
499
            break;
500

501
         default:
502
            break;
503
         }
504

505
         return true;
506
      }
507

508
   case T_ARRAY_REF:
9,364✔
509
   case T_ARRAY_SLICE:
510
      return sem_check_readable(tree_value(t));
9,364✔
511

512
   default:
513
      return true;
514
   }
515
}
516

517
static bool sem_check_array_dims(type_t type, type_t constraint, nametab_t *tab)
284✔
518
{
519
   const int ndims = dimension_of(type);
284✔
520
   for (int i = 0; i < ndims; i++) {
583✔
521
      tree_t r = range_of(type, i);
299✔
522

523
      type_t index_type = NULL;
299✔
524
      if (constraint != NULL && i < dimension_of(constraint))
299✔
UNCOV
525
         index_type = index_type_of(constraint, i);
×
526

527
      if (!sem_check_discrete_range(r, index_type, tab))
299✔
528
         return false;
529

530
      if (index_type == NULL)
299✔
531
         index_type = tree_type(r);
299✔
532

533
      if (!sem_check_type(r, index_type, tab))
299✔
534
         sem_error(r, "type of bound %s does not match type of index %s",
299✔
535
                   type_pp(tree_type(r)),
536
                   type_pp(index_type));
537
   }
538

539
   return true;
540
}
541

542
static bool sem_check_type(tree_t t, type_t expect, nametab_t *tab)
377,474✔
543
{
544
   type_t actual = tree_type(t);
377,474✔
545

546
   if (type_eq_map(actual, expect, get_generic_map(tab)))
377,474✔
547
      return true;
548

549
   // Supress cascading errors
550
   if (type_is_none(actual) || type_is_none(expect))
323✔
551
      return true;
8✔
552

553
   return false;
554
}
555

556
static bool sem_check_same_type(tree_t left, tree_t right)
32,909✔
557
{
558
   type_t left_type  = tree_type(left);
32,909✔
559
   type_t right_type = tree_type(right);
32,909✔
560

561
   if (type_eq(left_type, right_type))
32,909✔
562
      return true;
563

564
   // Supress cascading errors
565
   if (type_is_none(left_type) || type_is_none(right_type))
15✔
566
      return true;
1✔
567

568
   return false;
569
}
570

571
static bool sem_has_access(type_t t)
66,899✔
572
{
573
   // returns true if the type is an access type or is a composite
574
   // type that contains a subelement of an access type
575
   type_t base = type_base_recur(t);
96,735✔
576

577
   if (type_is_access(base))
96,735✔
578
      return true;
579

580
   if (type_is_array(base))
95,928✔
581
      return sem_has_access(type_elem(base));
29,836✔
582

583
   if (type_is_record(base)) {
66,092✔
584
      const int nfields = type_fields(base);
4,501✔
585
      for (int i = 0; i < nfields; i++) {
21,256✔
586
         if (sem_has_access(tree_type(type_field(base, i))))
16,828✔
587
            return true;
588
      }
589
   }
590

591
   return false;
592
}
593

594
static bool sem_check_type_decl(tree_t t, nametab_t *tab)
4,859✔
595
{
596
   type_t type = tree_type(t);
4,859✔
597

598
   // Nothing more to do for incomplete types
599
   if (type_kind(type) == T_INCOMPLETE)
4,859✔
600
      return true;
601

602
   type_kind_t kind = type_kind(type);
4,811✔
603

604
   if (kind == T_SUBTYPE) {
4,811✔
605
      // Implicitly created subtype for a constrained array defintion
606
      if (!sem_check_subtype_helper(t, type, tab)) {
723✔
607
         // Prevent cascading errors
608
         // TODO: can we do this check in the parser and set T_NONE earlier?
609
         type_set_base(type, type_new(T_NONE));
4✔
610
         return false;
4✔
611
      }
612

613
      type = type_base(type);
719✔
614
      kind = type_kind(type);
719✔
615
      assert(kind == T_ARRAY);
719✔
616
   }
617

618
   switch (kind) {
4,807✔
619
   case T_ARRAY:
2,481✔
620
      {
621
         type_t elem_type = type_elem(type);
2,481✔
622
         if (!sem_check_subtype(t, elem_type, tab))
2,481✔
623
            return false;
624

625
         if (standard() < STD_08 && type_is_unconstrained(elem_type)) {
2,481✔
626
            diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
1✔
627
            diag_printf(d, "array %s cannot have unconstrained element type",
1✔
628
                        istr(tree_ident(t)));
629
            diag_hint(d, NULL, "this would be allowed with $bold$--std=2008$$");
1✔
630
            diag_emit(d);
1✔
631
            return false;
1✔
632
         }
633

634
         if (type_is_file(elem_type))
2,480✔
635
            sem_error(t, "array %s cannot have element of file type",
1✔
636
                      istr(tree_ident(t)));
637
         else if (type_is_protected(elem_type))
2,479✔
638
            sem_error(t, "array %s cannot have element of protected type",
1✔
639
                      istr(tree_ident(t)));
640
         else if (!sem_check_incomplete(t, elem_type))
2,478✔
641
            return false;
642

643
         const int nindex = type_indexes(type);
2,477✔
644
         for (int i = 0; i < nindex; i++) {
5,261✔
645
            type_t index_type = type_index(type, i);
2,789✔
646
            if (type_is_none(index_type))
2,789✔
647
               return false;
648
            else if (!type_is_discrete(index_type))
2,786✔
649
               sem_error(t, "index type %s is not discrete",
2,786✔
650
                         type_pp(index_type));
651
         }
652

653
         return true;
654
      }
655

656
   case T_ENUM:
657
      return true;
658

659
   case T_PHYSICAL:
45✔
660
      {
661
         const int nunits = type_units(type);
45✔
662
         for (int i = 0; i < nunits; i++) {
200✔
663
            tree_t u = type_unit(type, i);
157✔
664
            tree_set_type(u, type);
157✔
665
            if (!sem_check(u, tab))
157✔
666
               return false;
667

668
            tree_t value = tree_value(u);
157✔
669

670
            // LRM 08 section 5.2.4.1: the abstract literal portion
671
            // shall be an integer literal
672
            if (tree_ival(value) == 0 && tree_dval(value) != 0)
157✔
673
               sem_error(value, "the abstract literal portion of a secondary "
1✔
674
                         "unit declaration must be an integer literal");
675

676
            if (i > 0 && !sem_check_type(value, type, tab))
156✔
677
               sem_error(value, "secondary unit %s must have type %s",
156✔
678
                         istr(tree_ident(u)), type_pp(type));
679
         }
680
      }
681

682
      // Fall-through
683
   case T_INTEGER:
684
      {
685
         tree_t r = type_dim(type, 0);
189✔
686

687
         if (!sem_check_range(r, NULL, tab))
189✔
688
            return false;
689

690
         switch (tree_subkind(r)) {
188✔
691
         case RANGE_TO:
185✔
692
         case RANGE_DOWNTO:
693
            {
694
               // See LRM 93 section 3.1.2: Each bound of a range
695
               // constraint that must be of some integer type, but the
696
               // two bounds need not have the same integer type.
697
               tree_t left = tree_left(r), right = tree_right(r);
185✔
698
               if (!type_is_integer(tree_type(left)))
185✔
UNCOV
699
                  sem_error(left, "type of left bound must be of some integer "
×
700
                            "type but have %s", type_pp(tree_type(left)));
701
               else if (!type_is_integer(tree_type(right)))
185✔
702
                  sem_error(right, "type of right bound must be of some "
2✔
703
                            "integer type but have %s",
704
                            type_pp(tree_type(right)));
705
            }
706
            break;
707

708
         case RANGE_EXPR:
3✔
709
            if (!type_is_integer(tree_type(r)))
3✔
710
               sem_error(r, "type of range bounds must be of some integer "
1✔
711
                         "type but have %s", type_pp(tree_type(r)));
712
            break;
713
         }
714

715
         if (!sem_locally_static(r))
185✔
716
            sem_error(r, "range constraint of type %s must be locally static",
1✔
717
                      type_pp(type));
718

719
         tree_set_type(r, type);
184✔
720
         return true;
184✔
721
      }
722

723
   case T_REAL:
23✔
724
      {
725
         tree_t r = type_dim(type, 0);
23✔
726

727
         if (!sem_check_range(r, NULL, tab))
23✔
728
            return false;
729

730
         switch (tree_subkind(r)) {
21✔
731
         case RANGE_TO:
20✔
732
         case RANGE_DOWNTO:
733
            {
734
               // See LRM 93 section 3.1.4: Each bound of a range
735
               // constraint that must be of some floating-point type, but
736
               // the two bounds need not have the same floating-point type.
737
               tree_t left = tree_left(r), right = tree_right(r);
20✔
738
               if (!type_is_real(tree_type(left)))
20✔
UNCOV
739
                  sem_error(left, "type of left bound must be of some "
×
740
                            "floating-point type but have %s",
741
                            type_pp(tree_type(left)));
742
               else if (!type_is_real(tree_type(right)))
20✔
743
                  sem_error(right, "type of right bound must be of some "
1✔
744
                            "floating-point type but have %s",
745
                            type_pp(tree_type(right)));
746
            }
747
            break;
748

749
         case RANGE_EXPR:
1✔
750
            assert(type_is_real(tree_type(r)));   // Unreachable
1✔
751
            break;
752
         }
753

754
         if (!sem_locally_static(r))
20✔
755
            sem_error(r, "range constraint of type %s must be locally static",
1✔
756
                      type_pp(type));
757

758
         tree_set_type(r, type);
19✔
759
         return true;
19✔
760
      }
761

762
   case T_RECORD:
1,230✔
763
      {
764
         const int nfields = type_fields(type);
1,230✔
765
         for (int i = 0; i < nfields; i++) {
4,929✔
766
            tree_t f = type_field(type, i);
3,705✔
767

768
            if (!sem_check(f, tab))
3,705✔
769
               return false;
770

771
            // Each field name must be distinct
772
            ident_t f_name = tree_ident(f);
3,704✔
773
            for (int j = 0; j < i; j++) {
12,248✔
774
               tree_t fj = type_field(type, j);
8,545✔
775
               if (ident_casecmp(f_name, tree_ident(fj))) {
8,545✔
776
                  diag_t *d = diag_new(DIAG_ERROR, tree_loc(f));
1✔
777
                  diag_printf(d, "duplicate field name %s", istr(f_name));
1✔
778
                  diag_hint(d, tree_loc(fj), "previously declared here");
1✔
779
                  diag_hint(d, tree_loc(f), "declared again here");
1✔
780
                  diag_emit(d);
1✔
781
                  return false;
1✔
782
               }
783
            }
784

785
            type_t f_type = tree_type(f);
3,703✔
786

787
            if (!sem_check_subtype(f, f_type, tab))
3,703✔
788
               return false;
789

790
            // Recursive record types are not allowed
791
            if (type_eq(type, f_type))
3,702✔
UNCOV
792
               sem_error(f, "recursive record types are not allowed");
×
793

794
            // Element types may not be unconstrained before VHDL-2008
795
            if (standard() < STD_08 && type_is_unconstrained(f_type)) {
3,702✔
796
               diag_t *d = diag_new(DIAG_ERROR, tree_loc(f));
1✔
797
               diag_printf(d, "record field %s cannot have unconstrained "
1✔
798
                           "array type in VHDL-%s", istr(f_name),
799
                           standard_text(standard()));
800
               diag_hint(d, NULL, "pass $bold$--std=2008$$ to enable this "
1✔
801
                         "feature");
802
               diag_emit(d);
1✔
803
               return false;
1✔
804
            }
805
            else if (type_is_file(f_type))
3,701✔
806
               sem_error(f, "record field %s cannot be of file type",
1✔
807
                         istr(f_name));
808
            else if (type_is_protected(f_type))
3,700✔
809
               sem_error(f, "record field %s cannot be of protected type",
3,700✔
810
                         istr(f_name));
811
         }
812

813
         return true;
814
      }
815

816
   case T_FILE:
101✔
817
      // Rules for file types are in LRM 93 section 3.4
818
      {
819
         type_t f = type_designated(type);
101✔
820

821
         switch (type_kind(f)) {
101✔
822
         case T_ACCESS:
1✔
823
            sem_error(t, "files may not be of access type");
1✔
824
            break;
825
         case T_FILE:
1✔
826
            sem_error(t, "files may not be of file type");
1✔
827
            break;
828
         case T_PROTECTED:
1✔
829
            sem_error(t, "files may not be of protected type");
1✔
830
            break;
831
         default:
832
            break;
98✔
833
         }
834

835
         if (sem_has_access(f))
98✔
836
            sem_error(t, "type %s has a subelement with an access type",
4✔
837
                      type_pp(f));
838

839
         type_t base_f = type_base_recur(f);
94✔
840
         if (type_is_array(base_f) && dimension_of(base_f) > 1)
94✔
841
            sem_error(t, "array type for file type must be one-dimensional");
2✔
842

843
         return true;
844
      }
845

846
   case T_ACCESS:
322✔
847
      // Rules for access types are in LRM 93 section 3.3
848
      {
849
         type_t designated = type_designated(type);
322✔
850

851
         if (!sem_check_subtype(t, designated, tab))
322✔
852
            return false;
853

854
         if (standard() < STD_19) {
321✔
855
            if (type_is_file(designated))
272✔
856
               sem_error(t, "access type %s cannot designate file type",
1✔
857
                         istr(tree_ident(t)));
858

859
            if (type_is_protected(designated))
271✔
860
               sem_error(t, "access type %s cannot designate protected type",
1✔
861
                         istr(tree_ident(t)));
862
         }
863

864
         return true;
865
      }
866

867
   case T_PROTECTED:
868
   default:
869
      return true;
870
   }
871
}
872

873
static bool sem_check_subtype_decl(tree_t t, nametab_t *tab)
1,451✔
874
{
875
   type_t type = tree_type(t);
1,451✔
876
   assert(type_kind(type) == T_SUBTYPE);
1,451✔
877
   assert(type_has_ident(type));
1,451✔
878

879
   return sem_check_subtype_helper(t, type, tab);
1,451✔
880
}
881

882
static bool sem_check_incomplete(tree_t t, type_t type)
116,960✔
883
{
884
   if (type_is_incomplete(type)) {
116,960✔
885
      diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
13✔
886
      diag_printf(d, "invalid use of incomplete type %s", type_pp(type));
13✔
887
      diag_hint(d, NULL, "prior to the end of the corresponding full type "
13✔
888
                "declaration, an incomplete type may only be used as the "
889
                "designated type of an access type declaration");
890
      diag_lrm(d, STD_08, "5.4.2");
13✔
891
      diag_emit(d);
13✔
892

893
      return false;
13✔
894
   }
895

896
   return true;
897
}
898

899
static bool sem_no_access_file_or_protected(tree_t t, type_t type, const char *what)
13,069✔
900
{
901
   // constants, signals, attributes, generics, ports
902
   // may not be of an access, file, or protected type, or
903
   // of a composite type with a subelement of an access type
904

905
   if (type_is_access(type))
13,069✔
906
      sem_error(t, "%s may not have access type", what);
4✔
907

908
   if (sem_has_access(type))
13,065✔
909
      sem_error(t, "%s may not have a type with a subelement of access type", what);
6✔
910

911
   if (type_is_protected(type))
13,059✔
912
      sem_error(t, "%s may not have protected type", what);
3✔
913

914
   if (type_is_file(type))
13,056✔
915
      sem_error(t, "%s may not have file type", what);
4✔
916

917
   return true;
918
}
919

920
static void sem_unconstrained_decl_hint(diag_t *d, type_t type)
15✔
921
{
922
   if (!type_is_record(type))
15✔
923
      return;
924

925
   // Tell the user which field is unconstrained
926

927
   type_t base = type_base_recur(type);
6✔
928
   const int nfields = type_fields(base);
6✔
929
   for (int i = 0; i < nfields; i++) {
18✔
930
      tree_t f = type_field(base, i);
12✔
931
      if (!type_is_unconstrained(tree_type(f)))
12✔
932
         continue;
3✔
933
      else if (type_constraint_for_field(type, f) == NULL)
9✔
934
         diag_hint(d, NULL, "missing record element constraint for field %s",
6✔
935
                   istr(tree_ident(f)));
936
   }
937
}
938

939
static void sem_propagate_constraints(tree_t decl, tree_t value)
7,882✔
940
{
941
   // Propagate the constraints from an initial value to an object
942
   // declaration with unconstrained type but only if the object subtype
943
   // has no constraints of its own
944

945
   if (standard() < STD_19 && tree_kind(decl) != T_CONST_DECL)
7,882✔
946
      return;
947

948
   type_t type = tree_type(decl);
6,193✔
949
   if (!type_is_unconstrained(type))
6,193✔
950
      return;
951

952
   for (type_t iter = type; type_kind(iter) == T_SUBTYPE;
951✔
953
        iter = type_base(iter)) {
26✔
954
      if (type_has_constraint(iter))
40✔
955
         return;
956
   }
957

958
   tree_set_type(decl, tree_type(value));
911✔
959
}
960

961
static bool sem_check_const_decl(tree_t t, nametab_t *tab)
6,726✔
962
{
963
   type_t type = tree_type(t);
6,726✔
964

965
   if (!sem_check_subtype(t, type, tab))
6,726✔
966
      return false;
967
   else if (type_is_none(type))
6,723✔
968
      return false;
969
   else if (!sem_check_incomplete(t, type))
6,708✔
970
      return false;
971

972
   if (!sem_no_access_file_or_protected(t, type, "constants"))
6,708✔
973
      return false;
974

975
   tree_t fwd = find_forward_decl(tab, t);
6,703✔
976

977
   if (tree_has_value(t)) {
6,703✔
978
      tree_t value = tree_value(t);
6,293✔
979
      if (!sem_check(value, tab))
6,293✔
980
         return false;
981

982
      if (!sem_check_type(value, type, tab))
6,263✔
983
         sem_error(value, "type of initial value %s does not match type "
5✔
984
                   "of declaration %s", type_pp2(tree_type(value), type),
985
                   type_pp2(type, tree_type(value)));
986

987
      if (fwd == NULL)
6,258✔
988
         sem_propagate_constraints(t, value);
5,856✔
989

990
      // A constant with a globaly static value should be treated as
991
      // globally static regardless of where it is declared
992
      if (sem_globally_static(value)) {
6,258✔
993
         tree_set_flag(t, TREE_F_GLOBALLY_STATIC);
4,535✔
994

995
         // A reference to a constant with a locally static subype and
996
         // locally static value is locally static
997
         if (sem_locally_static(value)
4,535✔
998
             && sem_static_subtype(tree_type(t), sem_locally_static))
3,491✔
999
            tree_set_flag(t, TREE_F_LOCALLY_STATIC);
3,359✔
1000
      }
1001
   }
1002
   else if (tree_kind(find_enclosing(tab, S_DESIGN_UNIT)) != T_PACKAGE)
410✔
1003
      sem_error(t, "deferred constant declarations are only permitted "
1✔
1004
                "in packages");
1005

1006
   if (fwd != NULL && !type_strict_eq(tree_type(fwd), type)) {
6,667✔
1007
      diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
1✔
1008
      diag_printf(d, "expected type %s for deferred constant %s but "
1✔
1009
                  "found %s", type_pp2(tree_type(fwd), type),
1010
                  istr(tree_ident(t)), type_pp2(type, tree_type(fwd)));
1011
      diag_hint(d, tree_loc(fwd), "originally declared with type %s",
1✔
1012
                type_pp2(tree_type(fwd), type));
1013
      diag_hint(d, tree_loc(t), "type here is %s",
1✔
1014
                type_pp2(type, tree_type(fwd)));
1015
      diag_emit(d);
1✔
1016
      return false;
1✔
1017
   }
1018

1019
   return true;
1020
}
1021

1022
static bool sem_check_signal_decl(tree_t t, nametab_t *tab)
6,262✔
1023
{
1024
   type_t type = tree_type(t);
6,262✔
1025

1026
   if (!sem_check_subtype(t, type, tab))
6,262✔
1027
      return false;
1028
   else if (type_is_none(type))
6,252✔
1029
      return false;
1030

1031
   if (type_is_unconstrained(type)) {
6,250✔
1032
      if (standard() < STD_19) {
21✔
1033
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
6✔
1034
         diag_printf(d, "declaration of signal %s cannot have unconstrained "
6✔
1035
                     "type %s", istr(tree_ident(t)), type_pp(type));
1036
         sem_unconstrained_decl_hint(d, type);
6✔
1037
         diag_emit(d);
6✔
1038
         return false;
6✔
1039
      }
1040
      else if (!tree_has_value(t)) {
15✔
1041
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
1✔
1042
         diag_printf(d, "declaration of signal %s without an initial value "
1✔
1043
                     "cannot have unconstrained type %s",
1044
                     istr(tree_ident(t)), type_pp(type));
1045
         sem_unconstrained_decl_hint(d, type);
1✔
1046
         diag_emit(d);
1✔
1047
         return false;
1✔
1048
      }
1049
   }
1050
   else if (!sem_check_incomplete(t, type))
6,229✔
1051
      return false;
1052

1053
   if (!sem_no_access_file_or_protected(t, type, "signals"))
6,243✔
1054
      return false;
1055

1056
   if (is_guarded_signal(t) && !type_is_resolved(type))
6,236✔
1057
      sem_error(t, "guarded signal must have resolved subtype");
1✔
1058

1059
   if (tree_has_value(t)) {
6,235✔
1060
      tree_t value = tree_value(t);
1,841✔
1061
      if (!sem_check(value, tab))
1,841✔
1062
         return false;
1063

1064
      if (!sem_check_type(value, type, tab))
1,838✔
UNCOV
1065
         sem_error(value, "type of initial value %s does not match type "
×
1066
                   "of declaration %s", type_pp2(tree_type(value), type),
1067
                   type_pp2(type, tree_type(value)));
1068

1069
      if (standard() >= STD_19 && type_is_unconstrained(type))
1,838✔
1070
         tree_set_type(t, tree_type(value));
14✔
1071
   }
1072

1073
   return true;
1074
}
1075

1076
static bool sem_check_var_decl(tree_t t, nametab_t *tab)
11,321✔
1077
{
1078
   type_t type = tree_type(t);
11,321✔
1079

1080
   if (!sem_check_subtype(t, type, tab))
11,321✔
1081
      return false;
1082
   else if (type_is_none(type))
11,318✔
1083
      return false;
1084

1085
   if (type_is_unconstrained(type)) {
11,305✔
1086
      if (standard() < STD_19) {
28✔
1087
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
6✔
1088
         diag_printf(d, "declaration of variable %s cannot have unconstrained "
6✔
1089
                     "type %s", istr(tree_ident(t)), type_pp(type));
1090
         sem_unconstrained_decl_hint(d, type);
6✔
1091
         diag_emit(d);
6✔
1092
         return false;
6✔
1093
      }
1094
      else if (!tree_has_value(t)) {
22✔
1095
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
2✔
1096
         diag_printf(d, "declaration of variable %s without an initial value "
2✔
1097
                     "cannot have unconstrained type %s", istr(tree_ident(t)),
1098
                     type_pp(type));
1099
         sem_unconstrained_decl_hint(d, type);
2✔
1100
         diag_emit(d);
2✔
1101
         return false;
2✔
1102
      }
1103
   }
1104
   else if (!sem_check_incomplete(t, type))
11,277✔
1105
      return false;
1106

1107
   if (tree_has_value(t)) {
11,296✔
1108
      if (type_kind(type) == T_PROTECTED)
2,043✔
1109
         sem_error(t, "variable %s with protected type may not have an "
1✔
1110
                   "initial value", istr(tree_ident(t)));
1111

1112
      tree_t value = tree_value(t);
2,042✔
1113
      if (!sem_check(value, tab))
2,042✔
1114
         return false;
1115

1116
      if (!sem_check_type(value, type, tab))
2,027✔
1117
         sem_error(value, "type of initial value %s does not match type "
1✔
1118
                   "of declaration %s", type_pp2(tree_type(value), type),
1119
                   type_pp2(type, tree_type(value)));
1120

1121
      sem_propagate_constraints(t, value);
2,026✔
1122
   }
1123

1124
   // From VHDL-2000 onwards shared variables must be protected types
1125
   if (standard() >= STD_00) {
11,279✔
1126
      if ((tree_flags(t) & TREE_F_SHARED) && !type_is_protected(type)) {
6,306✔
1127
         diag_t *d = pedantic_diag(tree_loc(t));
7✔
1128
         if (d != NULL) {
7✔
1129
            diag_printf(d, "shared variable %s must have protected type",
7✔
1130
                        istr(tree_ident(t)));
1131
            diag_emit(d);
7✔
1132
         }
1133
      }
1134
   }
1135

1136
   if (type_is_protected(type)) {
11,279✔
1137
      ident_t typeid = ident_rfrom(type_ident(type), '.');
196✔
1138
      tree_t pt = get_local_decl(tab, NULL, typeid, 0);
196✔
1139
      if (pt != NULL && tree_kind(pt) == T_PROT_DECL) {
196✔
1140
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
1✔
1141
         diag_printf(d, "cannot declare instance of protected type %s "
1✔
1142
                     "before its body has been elaborated", type_pp(type));
1143
         diag_hint(d, tree_loc(pt), "%s declared here",
1✔
1144
                   type_pp(type));
1145
         diag_lrm(d, STD_08, "14.4.2");
1✔
1146
         diag_emit(d);
1✔
1147
         return false;
1✔
1148
      }
1149

1150
      // The 2019 standard needs access to the instance and path name at
1151
      // the point of declaration
1152
      if (standard() >= STD_19)
195✔
1153
         tree_set_global_flags(t, TREE_GF_INSTANCE_NAME | TREE_GF_PATH_NAME);
53✔
1154
   }
1155

1156
   return true;
1157
}
1158

1159
static bool sem_check_param_decl(tree_t t, nametab_t *tab)
30,956✔
1160
{
1161
   type_t type = tree_type(t);
30,956✔
1162

1163
   if (!sem_check_subtype(t, type, tab))
30,956✔
1164
      return false;
1165
   else if (!sem_check_incomplete(t, type))
30,956✔
1166
      return false;
1167

1168
   // See LRM 93 section 3.3 for restrictions
1169

1170
   const type_kind_t kind = type_base_kind(type);
30,955✔
1171
   const class_t class = tree_class(t);
30,955✔
1172
   const port_mode_t mode = tree_subkind(t);
30,955✔
1173

1174
   switch (mode) {
30,955✔
1175
   case PORT_BUFFER:
1✔
1176
      sem_error(t, "subprogram formal parameters cannot have mode BUFFER");
1✔
1177
      break;
1178
   case PORT_LINKAGE:
1✔
1179
      sem_error(t, "subprogram formal parameters cannot have mode LINKAGE");
1✔
1180
      break;
1181
   default:
1182
      break;
30,953✔
1183
   }
1184

1185
   if (kind == T_FILE && class != C_FILE)
30,953✔
1186
      sem_error(t, "formal parameter %s with file type must have class FILE",
1✔
1187
                istr(tree_ident(t)));
1188

1189
   if (kind != T_FILE && class == C_FILE)
30,952✔
1190
      sem_error(t, "formal parameter %s with class FILE must have file type",
1✔
1191
                istr(tree_ident(t)));
1192

1193
   if ((kind == T_ACCESS || kind == T_PROTECTED) && class != C_VARIABLE)
30,951✔
1194
      sem_error(t, "formal parameter %s with %s type must have class VARIABLE",
6✔
1195
                istr(tree_ident(t)),
1196
                kind == T_ACCESS ? "access" : "protected");
1197

1198
   if (sem_has_access(type) && class != C_VARIABLE)
30,946✔
1199
      sem_error(t, "formal parameter %s with type containing an access type "
4✔
1200
                "must have class VARIABLE", istr(tree_ident(t)));
1201

1202
   if (class == C_CONSTANT && mode != PORT_IN)
30,942✔
1203
      sem_error(t, "parameter of class CONSTANT must have mode IN");
2✔
1204

1205
   // LRM 08 section 4.2.2.3
1206
   if (class == C_SIGNAL && tree_flags(t) & TREE_F_BUS)
30,940✔
1207
      sem_error(t, "formal signal parameter declaration may "
1✔
1208
                "not include the reserved word BUS");
1209

1210
   if (class == C_PACKAGE || class == C_TYPE)
30,939✔
1211
      sem_error(t, "parameter interface list cannot contain %s "
2✔
1212
                "interface declaration", class_str(class));
1213

1214
   if (mode == PORT_RECORD_VIEW || mode == PORT_ARRAY_VIEW) {
30,937✔
1215
      tree_t name = tree_value(t);
20✔
1216
      type_t view_type = tree_type(name);
20✔
1217

1218
      if (type_is_none(view_type))
20✔
1219
         return false;
1220

1221
      if (type_kind(view_type) != T_VIEW)
20✔
1222
         sem_error(name, "name in mode view indication of parameter %s does "
1✔
1223
                   "not denote a mode view", istr(tree_ident(t)));
1224

1225
      type_t elem_type = type;
19✔
1226
      if (mode == PORT_ARRAY_VIEW) {
19✔
1227
         if (!type_is_array(type))
1✔
1228
            sem_error(t, "parameter %s with array mode view indication has "
1✔
1229
                      "non-array type %s", istr(tree_ident(t)), type_pp(type));
1230

UNCOV
1231
         elem_type = type_elem(type);
×
1232
      }
1233

1234
      if (!type_eq(elem_type, type_designated(view_type)))
18✔
1235
         sem_error(t, "subtype %s is not compatible with mode "
1✔
1236
                   "view %s", type_pp(elem_type), type_pp(view_type));
1237
   }
1238
   else if (tree_has_value(t)) {
30,917✔
1239
      tree_t value = tree_value(t);
4,154✔
1240
      if (!sem_check(value, tab))
4,154✔
1241
         return false;
1242

1243
      if (!sem_check_type(value, type, tab))
4,152✔
1244
         sem_error(value, "type of default value %s does not match type "
2✔
1245
                   "of declaration %s", type_pp(tree_type(value)),
1246
                   type_pp(type));
1247

1248
      switch (class) {
4,150✔
1249
      case C_SIGNAL:
1✔
1250
         sem_error(t, "parameter of class SIGNAL cannot have a "
1✔
1251
                   "default value");
1252
         break;
1253

1254
      case C_VARIABLE:
6✔
1255
         if (mode == PORT_OUT || mode == PORT_INOUT)
6✔
1256
            sem_error(t, "parameter of class VARIABLE with mode OUT or "
2✔
1257
                      "INOUT cannot have a default value");
1258
         break;
1259

1260
      default:
1261
         break;
1262
      }
1263

1264
      if (!sem_globally_static(value)) {
4,147✔
1265
         diag_t *d = pedantic_diag(tree_loc(value));
8✔
1266
         if (d != NULL) {
8✔
1267
            diag_printf(d, "default value must be a static expression");
5✔
1268
            diag_emit(d);
5✔
1269
         }
1270
      }
1271

1272
      if (kind == T_PROTECTED)
4,147✔
1273
         sem_error(t, "parameter with protected type cannot have "
1✔
1274
                   "a default value");
1275
   }
1276

1277
   return true;
1278
}
1279

1280
static bool sem_check_port_decl(tree_t t, nametab_t *tab)
3,713✔
1281
{
1282
   type_t type = tree_type(t);
3,713✔
1283

1284
   if (type_is_none(type))
3,713✔
1285
      return false;
1286
   else if (!sem_check_subtype(t, type, tab))
3,711✔
1287
      return false;
1288
   else if (!sem_check_incomplete(t, type))
3,710✔
1289
      return false;
1290

1291
   const class_t class = tree_class(t);
3,709✔
1292
   const port_mode_t mode = tree_subkind(t);
3,709✔
1293

1294
   if (class == C_VARIABLE) {
3,709✔
1295
      if (standard() < STD_19) {
7✔
1296
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
1✔
1297
         diag_printf(d, "ports may not have variable class in VHDL-%s",
1✔
1298
                     standard_text(standard()));
1299
         diag_hint(d, NULL, "pass $bold$--std=2019$$ to enable this "
1✔
1300
                   "feature");
1301
         diag_emit(d);
1✔
1302
         return false;
1✔
1303
      }
1304

1305
      if (mode != PORT_INOUT)
6✔
1306
         sem_error(t, "formal variable port %s must have mode INOUT",
1✔
1307
                   istr(tree_ident(t)));
1308

1309
      if (!type_is_protected(type))
5✔
1310
         sem_error(t, "formal variable port %s must have protected type",
1✔
1311
                   istr(tree_ident(t)));
1312
   }
1313
   else if (class != C_SIGNAL)
3,702✔
1314
      sem_error(t, "invalid object class %s for port %s",
2✔
1315
                class_str(class), istr(tree_ident(t)));
1316

1317
   if (type_is_access(type))
3,704✔
1318
      sem_error(t, "port %s cannot be declared with access type %s",
1✔
1319
                istr(tree_ident(t)), type_pp(type));
1320

1321
   if (sem_has_access(type))
3,703✔
1322
      sem_error(t, "port %s cannot be declared with type %s which has a "
2✔
1323
                "subelement of access type", istr(tree_ident(t)),
1324
                type_pp(type));
1325

1326
   if (class != C_VARIABLE && type_is_protected(type)) {
3,701✔
1327
      diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
1✔
1328
      diag_printf(d, "port %s with class %s cannot be declared with "
1✔
1329
                  "protected type %s", istr(tree_ident(t)),
1330
                  class_str(class), type_pp(type));
1331
      diag_hint(d, NULL, "ports with variable class can be of protected "
1✔
1332
                "type in VHDL-2019");
1333
      diag_hint(d, NULL, "pass $bold$--std=2019$$ to enable this "
1✔
1334
                "feature");
1335
      diag_emit(d);
1✔
1336
      return false;
1✔
1337
   }
1338

1339
   if (type_is_file(type))
3,700✔
1340
      sem_error(t, "port %s cannot be declared with file type %s",
1✔
1341
                istr(tree_ident(t)), type_pp(type));
1342

1343
   if (mode == PORT_RECORD_VIEW || mode == PORT_ARRAY_VIEW) {
3,699✔
1344
      tree_t name = tree_value(t);
76✔
1345
      type_t view_type = tree_type(name);
76✔
1346

1347
      if (type_is_none(view_type))
76✔
1348
         return false;
1349

1350
      if (type_kind(view_type) != T_VIEW)
76✔
UNCOV
1351
         sem_error(name, "name in mode view indication of port %s does not "
×
1352
                   "denote a mode view", istr(tree_ident(t)));
1353

1354
      type_t elem_type = type;
76✔
1355
      if (mode == PORT_ARRAY_VIEW) {
76✔
1356
         if (!type_is_array(type))
16✔
1357
            sem_error(t, "port %s with array mode view indication has "
1✔
1358
                      "non-array type %s", istr(tree_ident(t)), type_pp(type));
1359

1360
         elem_type = type_elem(type);
15✔
1361
      }
1362

1363
      if (!type_eq(elem_type, type_designated(view_type)))
75✔
1364
         sem_error(t, "subtype %s is not compatible with mode "
2✔
1365
                   "view %s", type_pp(elem_type), type_pp(view_type));
1366
   }
1367
   else if (tree_has_value(t)) {
3,623✔
1368
      tree_t value = tree_value(t);
337✔
1369
      if (!sem_check(value, tab))
337✔
1370
         return false;
1371

1372
      if (mode == PORT_LINKAGE)
337✔
1373
         sem_error(t, "port with mode LINKAGE cannot have a default value");
1✔
1374

1375
      if (!sem_check_type(value, type, tab))
336✔
UNCOV
1376
         sem_error(value, "type of default value %s does not match type "
×
1377
                   "of declaration %s", type_pp(tree_type(value)),
1378
                   type_pp(type));
1379
   }
1380

1381
   return true;
1382
}
1383

1384
static bool sem_check_generic_decl(tree_t t, nametab_t *tab)
2,288✔
1385
{
1386
   const class_t class = tree_class(t);
2,288✔
1387
   switch (class) {
2,288✔
1388
   case C_CONSTANT:
1389
   case C_TYPE:
1390
   case C_FUNCTION:
1391
   case C_PROCEDURE:
1392
      break;
2,210✔
1393

1394
   case C_PACKAGE:
73✔
1395
      {
1396
         tree_t map = tree_value(t);
73✔
1397
         if (!tree_has_ref(map))
73✔
1398
            return false;   // Was earlier error
1399

1400
         assert(tree_kind(map) == T_PACKAGE_MAP);
70✔
1401

1402
         tree_t pack = tree_ref(map);
70✔
1403
         assert(is_uninstantiated_package(pack));
70✔
1404

1405
         switch (tree_subkind(map)) {
70✔
1406
         case PACKAGE_MAP_DEFAULT:
6✔
1407
            {
1408
               // Check each generic in the uninstantiated package has a
1409
               // default value
1410
               const int ngenerics = tree_generics(pack);
6✔
1411
               for (int i = 0; i < ngenerics; i++) {
18✔
1412
                  tree_t g = tree_generic(pack, i);
13✔
1413
                  if (!tree_has_value(g)) {
13✔
1414
                     diag_t *d = diag_new(DIAG_ERROR, tree_loc(map));
1✔
1415
                     diag_printf(d, "generic %s in package %s does not have a "
1✔
1416
                                 "default value", istr(tree_ident(g)),
1417
                                 istr(tree_ident(pack)));
1418
                     diag_hint(d, tree_loc(g), "%s declared here",
1✔
1419
                               istr(tree_ident(g)));
1420
                     diag_lrm(d, STD_08, "6.5.5");
1✔
1421

1422
                     diag_emit(d);
1✔
1423
                     return false;
1✔
1424
                  }
1425
               }
1426
            }
1427
            break;
1428

1429
         case PACKAGE_MAP_MATCHING:
9✔
1430
            sem_check_generic_map(map, pack, tab);
9✔
1431
            break;
9✔
1432

1433
         case PACKAGE_MAP_BOX:
1434
            break;
1435
         }
1436

1437
         return true;
1438
      }
1439
   default:
5✔
1440
      sem_error(t, "invalid object class %s for generic %s",
5✔
1441
                class_str(tree_class(t)), istr(tree_ident(t)));
1442
   }
1443

1444
   type_t type = tree_type(t);
2,210✔
1445

1446
   if (!sem_check_subtype(t, type, tab))
2,210✔
1447
      return false;
1448
   else if (type_is_none(type))
2,210✔
1449
      return false;
1450
   else if (!sem_check_incomplete(t, type))
2,209✔
1451
      return false;
1452

1453
   if (class != C_TYPE) {
2,208✔
1454
      if (type_is_access(type))
1,970✔
1455
         sem_error(t, "generic %s may not have access type",
1✔
1456
                   istr(tree_ident(t)));
1457
      else if (sem_has_access(type))
1,969✔
1458
         sem_error(t, "generic %s may not have a type with a subelement of "
2✔
1459
                   "access type", istr(tree_ident(t)));
1460
      else if (type_is_protected(type))
1,967✔
1461
         sem_error(t, "generic %s may not have protected type",
1✔
1462
                   istr(tree_ident(t)));
1463
      else if (type_is_file(type))
1,966✔
1464
         sem_error(t, "generic %s may not have file type", istr(tree_ident(t)));
1✔
1465
   }
1466

1467
   if (class == C_CONSTANT && tree_subkind(t) != PORT_IN)
2,203✔
1468
      sem_error(t, "generic %s with class CONSTANT must have mode IN",
1✔
1469
                istr(tree_ident(t)));
1470

1471
   if (tree_has_value(t)) {
2,202✔
1472
      tree_t value = tree_value(t);
1,162✔
1473
      if (!sem_check(value, tab))
1,162✔
1474
         return false;
1475

1476
      if (!sem_check_type(value, type, tab))
1,158✔
UNCOV
1477
         sem_error(value, "type of default value %s does not match type "
×
1478
                   "of declaration %s", type_pp(tree_type(value)),
1479
                   type_pp(type));
1480
   }
1481

1482
   if (tree_flags(t) & TREE_F_LOCALLY_STATIC) {
2,198✔
1483
      // For a generic declaration in a pacakage or subprogram to be
1484
      // locally static it must also have a locally static subtype
1485
      if (!sem_static_subtype(type, sem_locally_static))
300✔
1486
         tree_clear_flag(t, TREE_F_LOCALLY_STATIC);
10✔
1487
   }
1488

1489
   return true;
1490
}
1491

1492
static bool sem_check_field_decl(tree_t t)
3,705✔
1493
{
1494
   type_t type = tree_type(t);
3,705✔
1495

1496
   if (!sem_check_incomplete(t, type))
3,705✔
1497
      return false;
1✔
1498

1499
   return true;
1500
}
1501

1502
static bool sem_check_unit_decl(tree_t t)
157✔
1503
{
1504
   return true;
157✔
1505
}
1506

1507
static bool sem_check_alias(tree_t t, nametab_t *tab)
1,563✔
1508
{
1509
   // Rules for aliases are given in LRM 93 section 4.3.3
1510

1511
   tree_t value = tree_value(t);
1,563✔
1512
   type_t type = get_type_or_null(t);
1,563✔
1513

1514
   const tree_kind_t value_kind = tree_kind(value);
1,563✔
1515

1516
   if (type != NULL && type_is_subprogram(type)) {
1,563✔
1517
      // Alias of subprogram or enumeration literal
1518
      // Rules for matching signatures are in LRM 93 section 2.3.2
1519
      assert(tree_kind(value) == T_REF || tree_kind(value) == T_PROT_REF);
527✔
1520
      return true;
527✔
1521
   }
1522
   else if (tree_flags(t) & TREE_F_NONOBJECT_ALIAS) {
1,036✔
1523
      // TODO: cannot be label
1524
      return true;
1525
   }
1526
   else if (value_kind == T_REF && tree_has_ref(value)) {
1,035✔
1527
      tree_t decl = tree_ref(value);
869✔
1528
      if (aliased_type_decl(decl) != NULL)
869✔
1529
         return true;   // Alias of type
1530
      else if (tree_kind(decl) == T_VIEW_DECL)
803✔
1531
         return true;   // Alias of view declaration
1532
   }
1533
   else if (value_kind == T_ATTR_REF && is_type_attribute(tree_subkind(value)))
166✔
1534
      return true;   // Alias of type
1535

1536
   // Alias of object
1537
   if (!sem_check(value, tab))
963✔
1538
      return false;
1539

1540
   if (value_kind == T_ATTR_REF && tree_subkind(value) == ATTR_CONVERSE) {
955✔
1541
      // Special case handling for
1542
      //   https://gitlab.com/IEEE-P1076/VHDL-Issues/-/issues/293
1543
      return true;
1544
   }
1545
   else if (!sem_static_name(value, sem_globally_static)) {
929✔
1546
      diag_t *d = diag_new(DIAG_ERROR, tree_loc(value));
6✔
1547
      diag_printf(d, "aliased name is not static");
6✔
1548
      diag_lrm(d, STD_93, "6.1");
6✔
1549
      diag_emit(d);
6✔
1550
      return false;
6✔
1551
   }
1552

1553
   if (type != NULL) {
923✔
1554
      // Alias declaration had optional subtype indication
1555

1556
      if (!sem_check_subtype(t, type, tab))
836✔
1557
         return false;
1558

1559
      if (value_kind == T_EXTERNAL_NAME) {
836✔
1560
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
1✔
1561
         diag_printf(d, "an alias of an external name cannot have a "
1✔
1562
                     "subtype indication");
1563
         diag_lrm(d, STD_08, "6.6.2");
1✔
1564
         diag_emit(d);
1✔
1565
         return false;
1✔
1566
      }
1567

1568
      if (!sem_check_type(value, type, tab))
835✔
1569
         sem_error(t, "type of aliased object %s does not match expected "
1✔
1570
                   "type %s", type_pp2(tree_type(value), type),
1571
                   type_pp2(type, tree_type(value)));
1572

1573
      if (opt_get_int(OPT_RELAXED) && type_is_unconstrained(type)) {
834✔
1574
         // If the type of the aliased object is unconstrained then
1575
         // use its subtype instead of the subtype declared by the
1576
         // alias.  This is required for some UVVM sources.
1577
         type_t obj_type = tree_type(value);
1✔
1578
         if (!type_is_unconstrained(obj_type))
1✔
1579
            tree_set_type(t, obj_type);
1✔
1580
      }
1581
   }
1582
   else
1583
      type = tree_type(value);
87✔
1584

1585
   if (standard() < STD_08 && type_is_array(type) && dimension_of(type) > 1) {
921✔
1586
      diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
1✔
1587
      diag_printf(d, "object alias may not have multidimensional array type "
1✔
1588
                  "in VHDL-%s", standard_text(standard()));
1589
      diag_lrm(d, STD_93, "4.3.3.1");
1✔
1590
      diag_emit(d);
1✔
1591
      return false;
1✔
1592
   }
1593

1594
   return true;
1595
}
1596

1597
static void sem_missing_body_cb(tree_t t, tree_t parent, nametab_t *tab)
13,973✔
1598
{
1599
   if (parent == NULL || !opt_get_int(OPT_MISSING_BODY))
13,973✔
1600
      return;
715✔
1601
   else if (have_name_errors(tab))
13,258✔
1602
      return;   // May be missing due to parse errors
1603

1604
   const tree_kind_t kind = tree_kind(parent);
13,252✔
1605
   if (kind == T_PACKAGE || kind == T_PROT_DECL)
13,252✔
1606
      return;   // Should be in body
1607

1608
   type_t type = tree_type(t);
6,835✔
1609
   tree_t body = NULL;
6,835✔
1610
   int nth = 0;
6,835✔
1611
   do {
25,880✔
1612
      body = get_local_decl(tab, NULL, tree_ident(t), nth++);
25,880✔
1613
   } while (body != NULL && !type_eq(tree_type(body), type));
25,880✔
1614

1615
   if (body == NULL || !is_body(body)) {
6,835✔
1616
      diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
13✔
1617
      diag_printf(d, "missing body for ");
13✔
1618
      switch (tree_kind(t)) {
13✔
1619
      case T_PROT_DECL: diag_printf(d, "protected type "); break;
1✔
1620
      case T_PROC_DECL: diag_printf(d, "procedure "); break;
4✔
1621
      case T_FUNC_DECL: diag_printf(d, "function "); break;
8✔
1622
      default: break;
1623
      }
1624

1625
      diag_printf(d, "%s", type_pp(type));
13✔
1626
      diag_suppress(d, type_has_error(type));
13✔
1627

1628
      diag_hint(d, tree_loc(parent), "body not found in %s",
13✔
1629
                istr(tree_ident(parent)));
1630

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

1633
      diag_emit(d);
13✔
1634
   }
1635
}
1636

1637
static bool sem_check_func_ports(tree_t t, nametab_t *tab)
13,362✔
1638
{
1639
   const vhdl_standard_t std = standard();
13,362✔
1640

1641
   ident_t id = tree_ident(t);
13,362✔
1642
   if (is_operator_symbol(id)) {
13,362✔
1643
      const int nports = tree_ports(t);
5,231✔
1644
      const well_known_t wk = is_well_known(id);
5,231✔
1645
      if (wk >= W_OP_AND && wk <= W_OP_XNOR) {
5,231✔
1646
         if (std >= STD_08 && (nports < 1 || nports > 2))
949✔
UNCOV
1647
            sem_error(t, "logical operator must have either one or two "
×
1648
                      "operands");
1649
         else if (std < STD_08 && nports != 2)
949✔
1650
            sem_error(t, "logical operator must have two operands");
1✔
1651
      }
1652
      else if (wk >= W_OP_ABS && wk <= W_OP_CCONV && nports != 1)
4,282✔
1653
         sem_error(t, "unary operator must have one operand");
1✔
1654
      else if ((wk >= W_OP_EQUAL && wk <= W_OP_EXPONENT && nports != 2)
4,281✔
1655
               || (wk >= W_OP_MATCH_EQUAL && wk <= W_OP_MATCH_GREATER_EQUAL
4,281✔
1656
                   && nports != 2)
1657
               || (wk >= W_OP_ADD && wk <= W_OP_MINUS
4,280✔
1658
                   && (nports < 1 || nports > 2)))
926✔
1659
         sem_error(t, "binary operator must have two operands");
1✔
1660
   }
1661

1662
   const bool pure = !(tree_flags(t) & TREE_F_IMPURE);
13,359✔
1663

1664
   if (!pure && std >= STD_19)
13,359✔
1665
      return true;   // LCS2016_002 relaxed rules for impure functions
1666

1667
   const int nports = tree_ports(t);
12,960✔
1668
   for (int i = 0; i < nports; i++) {
34,208✔
1669
      tree_t p = tree_port(t, i);
21,248✔
1670
      if (tree_subkind(p) != PORT_IN) {
21,248✔
1671
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(p));
4✔
1672
         diag_printf(d, "%sfunction parameters must have mode IN",
5✔
1673
                     std < STD_19 ? "" : "pure ");
1674
         diag_hint(d, tree_loc(p), "parameter %s has mode %s",
4✔
1675
                   istr(tree_ident(p)), port_mode_str(tree_subkind(p)));
4✔
1676
         diag_lrm(d, STD_08, "4.2.2.1");
4✔
1677
         diag_emit(d);
4✔
1678
      }
1679
      else if (tree_class(p) == C_VARIABLE) {
21,244✔
1680
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(p));
3✔
1681
         diag_printf(d, "class of %sfunction parameters must be CONSTANT, "
4✔
1682
                     "SIGNAL, or FILE", std < STD_19 ? "" : "pure ");
1683
         diag_hint(d, tree_loc(p), "parameter %s has class %s",
3✔
1684
                   istr(tree_ident(p)), class_str(tree_class(p)));
1685
         diag_lrm(d, STD_08, "4.2.2.1");
3✔
1686
         diag_emit(d);
3✔
1687
      }
1688
   }
1689

1690
   return true;
1691
}
1692

1693
static bool sem_check_protected_method(tree_t t, nametab_t *tab)
457✔
1694
{
1695
   if (standard() >= STD_19)
457✔
1696
      return true;   // Relaxed in LCS2016_04
1697

1698
   const int nports = tree_ports(t);
277✔
1699
   for (int i = 0; i < nports; i++) {
456✔
1700
      tree_t p = tree_port(t, i);
179✔
1701
      type_t type = tree_type(p);
179✔
1702

1703
      if (sem_has_access(type)) {
179✔
1704
         diag_t *d = pedantic_diag(tree_loc(p));
2✔
1705
         if (d != NULL) {
2✔
1706
            diag_printf(d, "parameters of protected type methods cannot be of "
2✔
1707
                        "an access type or a composite type containing an "
1708
                        "access type");
1709
            if (type_is_access(type))
2✔
1710
               diag_hint(d, tree_loc(p), "type of %s is %s which is an "
1✔
1711
                         "access type", istr(tree_ident(p)), type_pp(type));
1712
            else
1713
               diag_hint(d, tree_loc(p), "type of %s is %s which has an "
1✔
1714
                         "access type as a subelement",
1715
                         istr(tree_ident(p)), type_pp(type));
1716
            diag_lrm(d, STD_08, "5.6.2");
2✔
1717
            diag_emit(d);
2✔
1718
         }
1719
      }
1720
      else if (type_is_file(type)) {
177✔
1721
         diag_t *d = pedantic_diag(tree_loc(p));
1✔
1722
         if (d != NULL) {
1✔
1723
            diag_printf(d, "parameters of protected type methods cannot be of "
1✔
1724
                        "a file type");
1725
            diag_hint(d, tree_loc(p), "type of %s is %s which is a file type",
1✔
1726
                      istr(tree_ident(p)), type_pp(type));
1727
            diag_lrm(d, STD_08, "5.6.2");
1✔
1728
            diag_emit(d);
1✔
1729
         }
1730
      }
1731
   }
1732

1733
   if (tree_kind(t) == T_FUNC_DECL) {
277✔
1734
      type_t result = type_result(tree_type(t));
111✔
1735
      if (sem_has_access(result) || type_is_file(result)) {
111✔
1736
         diag_t *d = pedantic_diag(tree_loc(t));
1✔
1737
         if (d != NULL) {
1✔
1738
            diag_printf(d, "return type of a protected type method cannot be "
1✔
1739
                        "of a file type, access type, or a composite type with "
1740
                        "a subelement that is an access type");
1741
            diag_lrm(d, STD_08, "5.6.2");
1✔
1742
            diag_emit(d);
1✔
1743
         }
1744
      }
1745
   }
1746

1747
   return true;
1748
}
1749

1750
static bool sem_check_func_result(tree_t t)
13,359✔
1751
{
1752
   type_t result = type_result(tree_type(t));
13,359✔
1753

1754
   if (type_is_protected(result))
13,359✔
1755
      sem_error(t, "function result subtype may not denote a protected type");
1✔
1756
   else if (type_is_file(result))
13,358✔
1757
      sem_error(t, "function result subtype may not denote a file type");
1✔
1758
   else if (!sem_check_incomplete(t, result))
13,357✔
1759
      return false;
1✔
1760

1761
   return true;
1762
}
1763

1764
static bool sem_check_func_decl(tree_t t, nametab_t *tab)
6,000✔
1765
{
1766
   const tree_flags_t flags = tree_flags(t);
6,000✔
1767
   if (flags & TREE_F_PREDEFINED)
6,000✔
1768
      return true;
1769

1770
   if (!sem_check_func_ports(t, tab))
6,000✔
1771
      return false;
1772

1773
   if (!sem_check_func_result(t))
5,997✔
1774
      return false;
1775

1776
   if ((flags & TREE_F_PROTECTED) && !sem_check_protected_method(t, tab))
5,995✔
1777
      return false;
1778

1779
   defer_check(tab, sem_missing_body_cb, t);
5,995✔
1780
   return true;
5,995✔
1781
}
1782

1783
static bool sem_compare_interfaces(tree_t decl, tree_t body, int nth,
12,778✔
1784
                                   tree_t (*getfn)(tree_t, unsigned),
1785
                                   const char *what)
1786
{
1787
   tree_t dport = (*getfn)(decl, nth);
12,778✔
1788
   tree_t bport = (*getfn)(body, nth);
12,778✔
1789

1790
   tree_flags_t dflags = tree_flags(dport);
12,778✔
1791
   tree_flags_t bflags = tree_flags(bport);
12,778✔
1792

1793
   ident_t dname = tree_ident(dport);
12,778✔
1794
   ident_t bname = tree_ident(bport);
12,778✔
1795

1796
   if (dname != bname) {
12,778✔
1797
      diag_t *d = diag_new(DIAG_ERROR, tree_loc(bport));
2✔
1798
      diag_printf(d, "%s name %s in subprogram %s body does not match "
2✔
1799
                  "name %s in declaration", what,
1800
                  istr(bname), istr(tree_ident(body)), istr(dname));
1801
      diag_hint(d, tree_loc(dport), "%s %s has name %s in specification",
2✔
1802
                ordinal_str(nth + 1), what, istr(dname));
1803
      diag_hint(d, tree_loc(bport), "%s %s has name %s in body",
2✔
1804
                ordinal_str(nth + 1), what, istr(bname));
1805
      diag_emit(d);
2✔
1806
      return false;
2✔
1807
   }
1808

1809
   const bool dcont = !!(tree_flags(dport) & TREE_F_CONTINUATION);
12,776✔
1810
   const bool bcont = !!(tree_flags(bport) & TREE_F_CONTINUATION);
12,776✔
1811

1812
   if (dcont != bcont) {
12,776✔
1813
      diag_t *d = pedantic_diag(tree_loc(bport));
1✔
1814
      if (d != NULL) {
1✔
1815
         diag_printf(d, "declaration of %s %s in subprogram body does not "
1✔
1816
                     "match specification", what, istr(bname));
1817

1818
         int bseq = 1;
1✔
1819
         for (tree_t it = bport; tree_flags(it) & TREE_F_CONTINUATION;
1✔
UNCOV
1820
              it = (*getfn)(body, nth - bseq++));
×
1821

1822
         diag_hint(d, tree_loc(bport), "%s appears %s in identifier list",
1✔
1823
                   istr(bname), ordinal_str(bseq));
1824

1825
         int dseq = 1;
1✔
1826
         for (tree_t it = dport; tree_flags(it) & TREE_F_CONTINUATION;
2✔
1827
              it = (*getfn)(decl, nth - dseq++));
1✔
1828

1829
         diag_hint(d, tree_loc(dport), "%s appears %s in identifier list",
1✔
1830
                   istr(dname), ordinal_str(dseq));
1831

1832
         diag_lrm(d, STD_08, "4.10");
1✔
1833
         diag_emit(d);
1✔
1834
         return false;
1✔
1835
      }
1836
   }
1837
   else if (dcont && bcont)
12,775✔
1838
      return true;   // Already checked first declaration in list
1839

1840
   type_t dtype = tree_type(dport);
11,655✔
1841
   type_t btype = tree_type(bport);
11,655✔
1842

1843
   // Do not use type_eq here as subtype must exactly match
1844
   if (!type_strict_eq(btype, dtype)) {
11,655✔
1845
     diag_t *d = diag_new(DIAG_ERROR, tree_loc(bport));
3✔
1846
     diag_printf(d, "subtype of %s %s does not match type %s in "
3✔
1847
                 "specification", what, istr(bname), type_pp(dtype));
1848
     diag_hint(d, tree_loc(dport), "%s %s declared with type %s",
3✔
1849
               what, istr(dname), type_pp(dtype));
1850
     diag_hint(d, tree_loc(bport), "%s %s declared with type %s ",
3✔
1851
               what, istr(bname), type_pp(btype));
1852
     diag_emit(d);
3✔
1853
     return false;
3✔
1854
   }
1855

1856
   const port_mode_t dmode = tree_subkind(dport);
11,652✔
1857
   const port_mode_t bmode = tree_subkind(bport);
11,652✔
1858

1859
   if (dmode != bmode) {
11,652✔
1860
     diag_t *d = diag_new(DIAG_ERROR, tree_loc(bport));
2✔
1861
     diag_printf(d, "%s %s of subprogram body %s with mode %s does not "
2✔
1862
                 "match mode %s in specification", what, istr(dname),
1863
                 istr(tree_ident(body)), port_mode_str(bmode),
1864
                 port_mode_str(dmode));
1865
     diag_hint(d, tree_loc(dport), "%s %s declared with mode %s",
2✔
1866
               what, istr(dname), port_mode_str(dmode));
1867
     diag_hint(d, tree_loc(bport), "%s %s declared with mode %s",
2✔
1868
               what, istr(bname), port_mode_str(bmode));
1869
     diag_emit(d);
2✔
1870
     return false;
2✔
1871
   }
1872

1873
   const bool bmode_explicit = !!(bflags & TREE_F_EXPLICIT_MODE);
11,650✔
1874
   const bool dmode_explicit = !!(dflags & TREE_F_EXPLICIT_MODE);
11,650✔
1875

1876
   if (bmode_explicit != dmode_explicit) {
11,650✔
1877
      diag_t *d = pedantic_diag(tree_loc(bport));
3✔
1878
      if (d != NULL) {
3✔
1879
         const char *dmode_str =
6✔
1880
            dmode_explicit ? port_mode_str(dmode) : "default";
3✔
1881
         const char *bmode_str =
6✔
1882
            bmode_explicit ? port_mode_str(bmode) : "default";
3✔
1883

1884
         diag_printf(d, "mode indication of subprogram %s %s %s was %s in "
3✔
1885
                     "specification but %s in body", istr(tree_ident(body)),
1886
                     what, istr(dname), dmode_str, bmode_str);
1887
         diag_hint(d, tree_loc(dport), "%s %s declared with %s mode in "
3✔
1888
                   "specification", what, istr(dname), dmode_str);
1889
         diag_hint(d, tree_loc(bport), "%s %s declared with %s mode in body",
3✔
1890
                   what, istr(bname), bmode_str);
1891
         diag_lrm(d, STD_08, "4.10");
3✔
1892
         diag_emit(d);
3✔
1893
         return false;
3✔
1894
      }
1895
   }
1896

1897
   const class_t dclass = tree_class(dport);
11,647✔
1898
   const class_t bclass = tree_class(bport);
11,647✔
1899

1900
   if (dclass != bclass) {
11,647✔
1901
     diag_t *d = diag_new(DIAG_ERROR, tree_loc(bport));
3✔
1902
     diag_printf(d, "class %s of subprogram body %s %s %s does not "
3✔
1903
                 "match class %s in specification", class_str(bclass),
1904
                 istr(tree_ident(body)), what, istr(dname), class_str(dclass));
1905
     diag_hint(d, tree_loc(dport), "%s %s declared with class %s in "
3✔
1906
               "subprogram specification", what, istr(dname),
1907
               class_str(dclass));
1908
     diag_hint(d, tree_loc(bport), "%s %s declared with class %s in "
3✔
1909
               "subprogram body", what, istr(bname), class_str(bclass));
1910
     diag_emit(d);
3✔
1911
     return false;
3✔
1912
   }
1913

1914
   const bool bclass_explicit = !!(bflags & TREE_F_EXPLICIT_CLASS);
11,644✔
1915
   const bool dclass_explicit = !!(dflags & TREE_F_EXPLICIT_CLASS);
11,644✔
1916

1917
   if (bclass_explicit != dclass_explicit) {
11,644✔
1918
      diag_t *d = pedantic_diag(tree_loc(bport));
3✔
1919
      if (d != NULL) {
3✔
1920
         const char *dclass_str =
4✔
1921
            dclass_explicit ? class_str(dclass) : "default";
2✔
1922
         const char *bclass_str =
4✔
1923
            bclass_explicit ? class_str(bclass) : "default";
2✔
1924

1925
         diag_printf(d, "class of subprogram %s %s %s was %s in specification "
2✔
1926
                     "but %s in body", istr(tree_ident(body)),
1927
                     what, istr(dname), dclass_str, bclass_str);
1928
         diag_hint(d, tree_loc(dport), "%s %s declared with %s class in "
2✔
1929
                   "specification", what, istr(dname), dclass_str);
1930
         diag_hint(d, tree_loc(bport), "%s %s declared with %s class in body",
2✔
1931
                   what, istr(bname), bclass_str);
1932
         diag_lrm(d, STD_08, "4.10");
2✔
1933
         diag_emit(d);
2✔
1934
         return false;
2✔
1935
      }
1936
   }
1937

1938
   tree_t bdef = tree_has_value(bport) ? tree_value(bport) : NULL;
11,642✔
1939
   tree_t ddef = tree_has_value(dport) ? tree_value(dport) : NULL;
11,642✔
1940

1941
   if (bdef == NULL && ddef == NULL)
11,642✔
1942
     return true;
1943

1944
   const tree_kind_t bkind = bdef ? tree_kind(bdef) : T_LAST_TREE_KIND;
1,897✔
1945
   const tree_kind_t dkind = ddef ? tree_kind(ddef) : T_LAST_TREE_KIND;
1,897✔
1946

1947
   // Work around some mismatches caused by folding
1948
   if (bdef != NULL && ddef != NULL && bkind != dkind)
1,897✔
1949
     return true;
1950

1951
   if (dkind == bkind) {
1,872✔
1952
     // This only covers a few simple cases
1953
     switch (dkind) {
1,871✔
1954
     case T_LITERAL: {
254✔
1955
       const literal_kind_t dsub = tree_subkind(ddef);
254✔
1956
       const literal_kind_t bsub = tree_subkind(bdef);
254✔
1957
       if (dsub == bsub) {
254✔
1958
         switch (dsub) {
254✔
1959
         case L_INT:
173✔
1960
           if (tree_ival(ddef) == tree_ival(bdef))
173✔
1961
             return true;
1962
           break;
1963
         case L_REAL:
23✔
1964
           if (tree_dval(ddef) == tree_dval(bdef))
23✔
1965
             return true;
1966
           break;
1967
         default:
1968
           return true;
1969
         }
1970
       }
1971
     } break;
1972

1973
     case T_REF:
1,410✔
1974
     case T_FCALL:
1975
       if (!tree_has_ref(bdef) || !tree_has_ref(ddef))
1,410✔
UNCOV
1976
         return true; // Was parse error, ignore it
×
1977

1978
       tree_t bref = tree_ref(bdef);
1,410✔
1979
       tree_t dref = tree_ref(ddef);
1,410✔
1980

1981
       if (bref == dref)
1,410✔
1982
         return true;
1983

1984
       // Work around mismatch introduced by folding
1985
       const tree_kind_t brefkind = tree_kind(bref);
153✔
1986
       if (brefkind == T_CONST_DECL || brefkind == T_GENERIC_DECL)
153✔
1987
         return true;
1988

1989
       break;
1990

1991
     default:
1992
       return true;
1993
     }
1994
   }
1995

1996
   diag_t *d = diag_new(DIAG_ERROR, tree_loc(bport));
3✔
1997
   diag_printf(d, "default value of %s %s in subprogram body %s does not "
3✔
1998
               "match declaration", what, istr(dname), istr(tree_ident(body)));
1999
   diag_hint(d, tree_loc(dport), "parameter was originally declared here");
3✔
2000
   diag_hint(d, tree_loc(bport), "body has different default value");
3✔
2001
   diag_emit(d);
3✔
2002

2003
   return false;
3✔
2004
}
2005

2006
static bool sem_check_conforming(tree_t decl, tree_t body)
6,665✔
2007
{
2008
   // Conformance rules are in LRM 08 section 4.10
2009
   // Note we don't implement strict lexical conformance here
2010

2011
   bool ok = true;
6,665✔
2012

2013
   const bool dimpure = !!(tree_flags(decl) & TREE_F_IMPURE);
6,665✔
2014
   const bool bimpure = !!(tree_flags(body) & TREE_F_IMPURE);
6,665✔
2015

2016
   if (dimpure != bimpure) {
6,665✔
2017
      diag_t *d = diag_new(DIAG_ERROR, tree_loc(body));
2✔
2018
      diag_printf(d, "function %s declaration was %s but body is %s",
4✔
2019
                  istr(tree_ident(body)), dimpure ? "impure" : "pure",
2020
                  bimpure ? "impure" : "pure");
2021
      diag_hint(d, tree_loc(decl), "declaration was %s",
2✔
2022
                dimpure ? "impure" : "pure");
2023
      diag_hint(d, tree_loc(body), "expecting keyword %s to match declaration",
3✔
2024
                bimpure ? "IMPURE" : "PURE");
2025
      diag_emit(d);
2✔
2026
      ok = false;
2✔
2027
   }
2028

2029
   // This must be true or they would be considered different overloads
2030
   assert(tree_ports(decl) == tree_ports(body));
6,665✔
2031

2032
   const int nports = tree_ports(decl);
6,665✔
2033
   for (int i = 0; i < nports; i++)
19,376✔
2034
      ok &= sem_compare_interfaces(decl, body, i, tree_port, "parameter");
12,711✔
2035

2036
   const int ngenerics = tree_generics(decl);
6,665✔
2037
   if (ngenerics != tree_generics(body)) {
6,665✔
2038
      diag_t *d = diag_new(DIAG_ERROR, tree_loc(body));
1✔
2039
      diag_printf(d, "subprogram %s declaration has %d generic%s but body "
2✔
2040
                  "has %d", istr(tree_ident(body)), ngenerics,
2041
                  ngenerics > 1 ? "s" : "", tree_generics(body));
2042
      diag_hint(d, tree_loc(decl), "declaration with %d generics", ngenerics);
1✔
2043
      diag_hint(d, tree_loc(body), "body has %d generics", tree_generics(body));
1✔
2044
      diag_emit(d);
1✔
2045
      ok = false;
1✔
2046
   }
2047
   else {
2048
      for (int i = 0; i < ngenerics; i++)
6,731✔
2049
         ok &= sem_compare_interfaces(decl, body, i, tree_generic, "generic");
67✔
2050
   }
2051

2052
   type_t dtype = tree_type(decl);
6,665✔
2053
   type_t btype = tree_type(body);
6,665✔
2054

2055
   if (type_has_result(dtype) && type_has_result(btype)) {
6,665✔
2056
      type_t dresult = type_result(dtype);
5,735✔
2057
      type_t bresult = type_result(btype);
5,735✔
2058

2059
      if (!type_strict_eq(dresult, bresult)) {
5,735✔
2060
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(body));
1✔
2061
         diag_printf(d, "return type of function body %s does not match type "
1✔
2062
                     "%s in specification", istr(tree_ident(body)),
2063
                     type_pp(dresult));
2064
         diag_hint(d, tree_loc(decl), "specification has return type %s",
1✔
2065
                   type_pp(dresult));
2066
         diag_hint(d, tree_loc(body), "body has return type %s ",
1✔
2067
                   type_pp(bresult));
2068
         diag_emit(d);
1✔
2069
         ok = false;
1✔
2070
      }
2071
   }
2072

2073
   return ok;
6,665✔
2074
}
2075

2076
static bool sem_check_func_body(tree_t t, nametab_t *tab)
7,362✔
2077
{
2078
   if (!sem_check_func_ports(t, tab))
7,362✔
2079
      return false;
2080

2081
   if (!sem_check_func_result(t))
7,362✔
2082
      return false;
2083

2084
   tree_t fwd = find_forward_decl(tab, t);
7,361✔
2085
   if (fwd != NULL && !sem_check_conforming(fwd, t))
7,361✔
2086
      return false;
16✔
2087

2088
   return true;
2089
}
2090

2091
static bool sem_check_proc_decl(tree_t t, nametab_t *tab)
1,085✔
2092
{
2093
   if (is_operator_symbol(tree_ident(t)))
1,085✔
UNCOV
2094
      sem_error(t, "procedure name must be an identifier");
×
2095

2096
   const tree_flags_t flags = tree_flags(t);
1,085✔
2097
   if (flags & TREE_F_PREDEFINED)
1,085✔
2098
      return true;
2099
   else if ((flags & TREE_F_PROTECTED) && !sem_check_protected_method(t, tab))
1,085✔
2100
      return false;
2101

2102
   defer_check(tab, sem_missing_body_cb, t);
1,085✔
2103
   return true;
1,085✔
2104
}
2105

2106
static bool sem_check_proc_body(tree_t t, nametab_t *tab)
2,148✔
2107
{
2108
   tree_t fwd = find_forward_decl(tab, t);
2,148✔
2109
   if (fwd != NULL && !sem_check_conforming(fwd, t))
2,148✔
2110
      return false;
2111

2112
   if (fwd == NULL && is_operator_symbol(tree_ident(t)))
2,142✔
2113
      sem_error(t, "procedure name must be an identifier");
1✔
2114

2115
   // Cleared by wait statement or pcall
2116
   tree_set_flag(t, TREE_F_NEVER_WAITS);
2,141✔
2117

2118
   return true;
2,141✔
2119
}
2120

2121
static bool sem_check_subprogram_inst(tree_t t, nametab_t *tab)
129✔
2122
{
2123
   if (tree_generics(t) == 0)
129✔
2124
      return false;   // Was a parse error
2125

2126
   if (!sem_check_generic_map(t, t, tab))
122✔
2127
      return false;
2✔
2128

2129
   // Other declarations were checked on the uninstantiated subprogram
2130

2131
   return true;
2132
}
2133

2134
static bool sem_check_sensitivity(tree_t t, nametab_t *tab)
14,301✔
2135
{
2136
   const int ntriggers = tree_triggers(t);
14,301✔
2137
   for (int i = 0; i < ntriggers; i++) {
15,471✔
2138
      tree_t r = tree_trigger(t, i);
1,175✔
2139
      if (tree_kind(r) == T_ALL)
1,175✔
2140
         continue;
49✔
2141
      else if (!sem_check(r, tab) || !sem_check_readable(r))
1,126✔
2142
         return false;
2✔
2143

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

2147
      if (class_of(r) != C_SIGNAL) {
1,122✔
2148
         tree_t ref = name_to_ref(r);
1✔
2149
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(r));
1✔
2150
         if (ref != NULL) {
1✔
2151
            tree_t decl = tree_ref(ref);
1✔
2152
            diag_printf(d, "name %s in sensitivity list is not a signal",
1✔
2153
                        istr(tree_ident(decl)));
2154
            diag_hint(d, tree_loc(r), "%s is a %s", istr(tree_ident(decl)),
1✔
2155
                      class_str(class_of(decl)));
2156
         }
2157
         else
UNCOV
2158
            diag_printf(d, "name in sensitivity list is not a signal");
×
2159
         diag_emit(d);
1✔
2160
         return false;
1✔
2161
      }
2162
   }
2163

2164
   return true;
2165
}
2166

2167
static void sem_check_missing_wait(tree_t t, nametab_t *tab)
4,818✔
2168
{
2169
   if (!opt_get_int(OPT_MISSING_WAIT))
4,818✔
2170
      return;
2171

2172
   const int ntriggers = tree_triggers(t);
4,178✔
2173
   if (ntriggers > 0)
4,178✔
2174
      return;
2175

2176
   const tree_flags_t flags = tree_flags(t);
3,789✔
2177
   if (flags & TREE_F_HAS_WAIT)
3,789✔
2178
      return;
2179

2180
   diag_t *d = diag_new(DIAG_WARN, tree_loc(t));
5✔
2181
   diag_printf(d, "potential infinite loop in process");
5✔
2182
   if (!(flags & TREE_F_SYNTHETIC_NAME))
5✔
2183
      diag_printf(d, " %s", istr(tree_ident(t)));
3✔
2184
   diag_printf(d, " with no sensitivity list and no wait statements.");
5✔
2185
   diag_emit(d);
5✔
2186
}
2187

2188
static bool sem_check_process(tree_t t, nametab_t *tab)
4,820✔
2189
{
2190
   if (!sem_check_sensitivity(t, tab))
4,820✔
2191
      return false;
2192

2193
   sem_check_missing_wait(t, tab);
4,818✔
2194

2195
   return true;
4,818✔
2196
}
2197

2198
static bool sem_check_package(tree_t t, nametab_t *tab)
1,463✔
2199
{
2200
   if (!sem_check_context_clause(t, tab))
1,463✔
2201
      return false;
2202

2203
   if (tree_genmaps(t) > 0 && !sem_check_generic_map(t, t, tab))
1,463✔
2204
      return false;
2205

2206
   // Subprogram bodies are not allowed in package specification
2207
   const int ndecls = tree_decls(t);
1,463✔
2208
   for (int i = 0; i < ndecls; i++) {
29,512✔
2209
     tree_t d = tree_decl(t, i);
28,051✔
2210
     tree_kind_t kind = tree_kind(d);
28,051✔
2211
     if ((kind == T_FUNC_BODY) || (kind == T_PROC_BODY))
28,051✔
2212
       sem_error(d, "subprogram body is not allowed in package specification");
28,051✔
2213
   }
2214

2215
   return true;
2216
}
2217

2218
static bool sem_check_pack_inst(tree_t t, nametab_t *tab)
278✔
2219
{
2220
   if (tree_generics(t) == 0)
278✔
2221
      return false;   // Was a parse error
2222

2223
   if (!sem_check_generic_map(t, t, tab))
275✔
2224
      return false;
6✔
2225

2226
   // Other declarations were checked on the uninstantiated package
2227

2228
   return true;
2229
}
2230

2231
static bool sem_check_missing_bodies(tree_t secondary, nametab_t *tab)
975✔
2232
{
2233
   // Check for any missing subprogram or protected type bodies, and
2234
   // deferred constants which were not given values and
2235
   tree_t primary = tree_primary(secondary);
975✔
2236
   const int ndecls = tree_decls(primary);
975✔
2237
   for (int i = 0; i < ndecls; i++) {
19,491✔
2238
      tree_t d = tree_decl(primary, i);
18,518✔
2239
      switch (tree_kind(d)) {
18,518✔
2240
      case T_FUNC_DECL:
14,445✔
2241
      case T_PROC_DECL:
2242
         if (tree_flags(d) & TREE_F_PREDEFINED)
14,445✔
2243
            break;
2244
         // Fall-through
2245
      case T_PROT_DECL:
2246
         sem_missing_body_cb(d, secondary, tab);
6,772✔
2247
         break;
6,772✔
2248
      case T_CONST_DECL:
1,177✔
2249
         if (!tree_has_value(d)) {
1,177✔
2250
            tree_t d2 = get_local_decl(tab, NULL, tree_ident(d), 0);
402✔
2251
            if (d2 == NULL || !tree_has_value(d2))
402✔
2252
               sem_error(d, "deferred constant %s was not given a value in the "
2✔
2253
                         "package body", istr(tree_ident(d)));
2254
         }
2255
      default:
2256
         break;
2257
      }
2258
   }
2259

2260
   return true;
2261
}
2262

2263
static bool sem_check_pack_body(tree_t t, nametab_t *tab)
778✔
2264
{
2265
   if (!tree_has_primary(t))
778✔
2266
      return false;
2267

2268
   if (!sem_check_context_clause(t, tab))
773✔
2269
      return false;
2270

2271
   if (!sem_check_missing_bodies(t, tab))
773✔
2272
      return false;
2✔
2273

2274
   return true;
2275
}
2276

2277
static bool sem_check_component(tree_t t, nametab_t *tab)
315✔
2278
{
2279
   return true;
315✔
2280
}
2281

2282
static void sem_passive_cb(tree_t t, void *context)
1✔
2283
{
2284
   tree_t s = context;
1✔
2285

2286
   diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
1✔
2287
   diag_printf(d, "signal assignment statement not allowed inside passive "
1✔
2288
               "process");
2289
   diag_hint(d, tree_loc(s), "process in entity statement part must "
1✔
2290
             "be passive");
2291
   diag_hint(d, tree_loc(t), "signal assignment statement");
1✔
2292
   diag_lrm(d, STD_93, "1.1.3");
1✔
2293
   diag_lrm(d, STD_93, "9.2");
1✔
2294

2295
   diag_emit(d);
1✔
2296
}
1✔
2297

2298
static bool sem_check_entity(tree_t t, nametab_t *tab)
5,002✔
2299
{
2300
   if (!sem_check_context_clause(t, tab))
5,002✔
2301
      return false;
2302

2303
   // All processes in entity statement part must be passive
2304
   const int nstmts = tree_stmts(t);
5,002✔
2305
   for (int i = 0; i < nstmts; i++) {
5,039✔
2306
      tree_t s = tree_stmt(t, i);
37✔
2307
      tree_visit_only(s, sem_passive_cb, s, T_SIGNAL_ASSIGN);
37✔
2308
   }
2309

2310
   return true;
2311
}
2312

2313
static bool sem_check_arch(tree_t t, nametab_t *tab)
5,015✔
2314
{
2315
   if (!tree_has_primary(t))
5,015✔
2316
      return false;
2317

2318
   if (!sem_check_context_clause(t, tab))
5,012✔
UNCOV
2319
      return false;
×
2320

2321
   return true;
2322
}
2323

2324
static tree_t sem_check_lvalue(tree_t t)
30,444✔
2325
{
2326
   switch (tree_kind(t)) {
68,520✔
2327
   case T_REF:
30,535✔
2328
      return sem_check_lvalue(tree_ref(t));
30,535✔
2329
   case T_ARRAY_SLICE:
7,541✔
2330
   case T_ARRAY_REF:
2331
   case T_ALIAS:
2332
   case T_RECORD_REF:
2333
   case T_ALL:
2334
      return sem_check_lvalue(tree_value(t));
7,541✔
2335
   case T_VAR_DECL:
2336
   case T_SIGNAL_DECL:
2337
   case T_PORT_DECL:
2338
   case T_CONST_DECL:
2339
   case T_IMPLICIT_SIGNAL:
2340
   case T_PARAM_DECL:
2341
   case T_EXTERNAL_NAME:
2342
      return t;
2343
   default:
10✔
2344
      return NULL;
10✔
2345
   }
2346
}
2347

2348
static bool sem_check_aggregate_target_element(tree_t target, int nth)
342✔
2349
{
2350
   tree_t a = tree_assoc(target, nth);
342✔
2351
   tree_t value = tree_value(a);
342✔
2352

2353
   if (tree_kind(value) != T_AGGREGATE) {
342✔
2354
      if (!sem_static_name(value, sem_locally_static))
324✔
2355
         sem_error(value, "aggregate element must be locally static name");
2✔
2356
   }
2357

2358
   const assoc_kind_t kind = tree_subkind(a);
340✔
2359
   switch (kind) {
340✔
2360
   case A_RANGE:
2✔
2361
      if (standard() >= STD_08) {
2✔
2362
         // LRM 08 section 10.6.2.1: it is an error if the element
2363
         // association contains a choice that is a discrete range and
2364
         // an expression of a type other than the aggregate type.
2365
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(value));
1✔
2366
         diag_printf(d, "range choice expression must have same type "
1✔
2367
                     "as aggregate");
2368
         diag_hint(d, tree_loc(value), "expression type is %s but "
1✔
2369
                   "aggregate is %s", type_pp(tree_type(value)),
2370
                   type_pp(tree_type(target)));
2371
         diag_lrm(d, STD_08, "10.6.2");
1✔
2372
         diag_emit(d);
1✔
2373
         return false;
1✔
2374
      }
2375
      // Fall-through
2376
   case A_OTHERS:
2377
      sem_error(a, "%s association not allowed in aggregate target",
4✔
2378
                assoc_kind_str(kind));
2379
   case A_NAMED:
2380
   case A_POS:
2381
   case A_SLICE:
2382
   case A_CONCAT:
2383
      break;
2384
   }
2385

2386
   const int nassocs = tree_assocs(target);
335✔
2387
   for (int j = nth + 1; j < nassocs; j++) {
605✔
2388
      tree_t cmp = tree_value(tree_assoc(target, j));
272✔
2389
      if (same_tree(value, cmp)) {
272✔
2390
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(cmp));
2✔
2391
         diag_printf(d, "%s %s is identifed more than once in "
4✔
2392
                     "aggregate target", class_str(class_of(cmp)),
2393
                     tree_kind(cmp) == T_REF ?
2✔
2394
                     istr(tree_ident(cmp)) : "subelement");
2✔
2395
         diag_hint(d, tree_loc(value), "first seen here");
2✔
2396
         diag_lrm(d, STD_08, "10.5.2");
2✔
2397
         diag_emit(d);
2✔
2398
         return false;
2✔
2399
      }
2400
   }
2401

2402
   return true;
2403
}
2404

2405
static bool sem_check_variable_target(tree_t target)
16,543✔
2406
{
2407
   if (tree_kind(target) == T_AGGREGATE) {
16,543✔
2408
      // Rules for aggregate variable targets in LRM 93 section 8.5
2409

2410
      type_t type = tree_type(target);
71✔
2411
      if (!type_is_composite(type))
71✔
UNCOV
2412
         sem_error(target, "aggregate target of variable assignment has "
×
2413
                   "non-composite type %s", type_pp(tree_type(target)));
2414

2415
      const int nassocs = tree_assocs(target);
71✔
2416
      for (int i = 0; i < nassocs; i++) {
222✔
2417
         tree_t a = tree_assoc(target, i);
157✔
2418
         tree_t value = tree_value(a);
157✔
2419

2420
         if (!sem_check_variable_target(value))
157✔
2421
            return false;
2422

2423
         if (!sem_check_aggregate_target_element(target, i))
156✔
2424
            return false;
2425
      }
2426
   }
2427
   else {
2428
      tree_t decl = sem_check_lvalue(target);
16,472✔
2429
      const tree_kind_t kind = decl ? tree_kind(decl) : T_LAST_TREE_KIND;
16,472✔
2430

2431
      const bool suitable = kind == T_VAR_DECL
32,943✔
2432
         || (kind == T_PARAM_DECL && tree_class(decl) == C_VARIABLE)
2,598✔
2433
         || (kind == T_EXTERNAL_NAME && tree_class(decl) == C_VARIABLE);
16,478✔
2434

2435
      if (!suitable) {
16,472✔
2436
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(target));
6✔
2437
         diag_printf(d, "target of variable assignment must be a variable "
6✔
2438
                     "name or aggregate");
2439

2440
         tree_t ref = name_to_ref(target);
6✔
2441
         if (ref != NULL && tree_has_ref(ref))
6✔
2442
            diag_hint(d, tree_loc(target), "%s is a %s", istr(tree_ident(ref)),
5✔
2443
                      class_str(class_of(tree_ref(ref))));
2444

2445
         diag_emit(d);
6✔
2446
         return false;
6✔
2447
      }
2448
      else if (kind == T_PARAM_DECL && tree_subkind(decl) == PORT_IN) {
16,466✔
2449
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(target));
1✔
2450
         diag_printf(d, "cannot assign to parameter %s with mode IN",
1✔
2451
                     istr(tree_ident(decl)));
2452
         diag_hint(d, tree_loc(decl), "%s declared with mode IN",
1✔
2453
                   istr(tree_ident(decl)));
2454
         diag_hint(d, tree_loc(target), "target of variable assignment");
1✔
2455
         diag_emit(d);
1✔
2456
         return false;
1✔
2457
      }
2458
      else if (type_is_protected(tree_type(target)))
16,465✔
2459
         sem_error(target, "may not assign to variable of a protected type");
1✔
2460
   }
2461

2462
   return true;
2463
}
2464

2465
static bool sem_check_var_assign(tree_t t, nametab_t *tab)
16,500✔
2466
{
2467
   tree_t target = tree_target(t);
16,500✔
2468
   tree_t value = tree_value(t);
16,500✔
2469

2470
   if (!sem_check(target, tab))
16,500✔
2471
      return false;
2472

2473
   if (!sem_check(value, tab))
16,474✔
2474
      return false;
2475

2476
   if (!sem_check_readable(value))
16,387✔
2477
      return false;
2478

2479
   if (!sem_check_variable_target(target))
16,386✔
2480
      return false;
2481

2482
   if (!sem_check_same_type(value, target)) {
16,373✔
2483
      type_t target_type = tree_type(target);
11✔
2484
      type_t value_type  = tree_type(value);
11✔
2485
      sem_error(t, "type of value %s does not match type of target %s",
11✔
2486
                type_pp2(value_type, target_type),
2487
                type_pp2(target_type, value_type));
2488
   }
2489

2490
   return true;
2491
}
2492

2493
static bool sem_check_waveforms(tree_t t, tree_t target, nametab_t *tab)
7,536✔
2494
{
2495
   type_t std_time = std_type(NULL, STD_TIME);
7,536✔
2496
   type_t expect = tree_type(target);
7,536✔
2497

2498
   const int nwaves = tree_waveforms(t);
7,536✔
2499
   for (int i = 0; i < nwaves; i++) {
15,285✔
2500
      tree_t waveform = tree_waveform(t, i);
7,767✔
2501

2502
      if (tree_has_value(waveform)) {
7,767✔
2503
         tree_t value = tree_value(waveform);
7,750✔
2504

2505
         if (!sem_check(value, tab))
7,750✔
2506
            return false;
2507

2508
         if (!sem_check_readable(value))
7,741✔
2509
            return false;
2510

2511
         if (!sem_check_type(value, expect, tab))
7,740✔
2512
            sem_error(t, "type of value %s does not match type of target %s",
6✔
2513
                      type_pp2(tree_type(value), expect),
2514
                      type_pp2(expect, tree_type(value)));
2515
      }
2516
      else {
2517
         tree_t decl = sem_check_lvalue(target);
17✔
2518
         if (decl != NULL && !is_guarded_signal(decl))
17✔
2519
            sem_error(waveform, "a null waveform element is only valid when "
2✔
2520
                      "the target is a guarded signal");
2521
      }
2522

2523
      if (tree_has_delay(waveform)) {
7,749✔
2524
         tree_t delay = tree_delay(waveform);
882✔
2525
         if (!sem_check(delay, tab))
882✔
2526
            return false;
2527

2528
         if (!sem_check_type(delay, std_time, tab))
882✔
2529
            sem_error(delay, "type of delay must be %s but have %s",
7,749✔
2530
                      type_pp(std_time), type_pp(tree_type(delay)));
2531
      }
2532
   }
2533

2534
   return true;
2535
}
2536

2537
static tree_t sem_check_view_target(tree_t target)
258✔
2538
{
2539
   switch (tree_kind(target)) {
275✔
2540
   case T_REF:
173✔
2541
      {
2542
         tree_t decl = tree_ref(target);
173✔
2543
         if (tree_kind(decl) == T_PORT_DECL) {
173✔
2544
            const port_mode_t mode = tree_subkind(decl);
78✔
2545
            if (mode == PORT_ARRAY_VIEW || mode == PORT_RECORD_VIEW)
78✔
2546
               return tree_value(decl);
78✔
2547
         }
2548

2549
         return NULL;
2550
      }
2551

2552
   case T_ARRAY_REF:
17✔
2553
   case T_ARRAY_SLICE:
2554
      return sem_check_view_target(tree_value(target));
17✔
2555

2556
   case T_RECORD_REF:
85✔
2557
      {
2558
         tree_t view = sem_check_view_target(tree_value(target));
85✔
2559
         if (view == NULL)
85✔
2560
            return NULL;
2561

2562
         bool converse = false;
61✔
2563
         tree_t f = tree_ref(target);
61✔
2564
         tree_t e = find_element_mode_indication(view, f, &converse);
61✔
2565
         if (e == NULL)
61✔
2566
            return NULL;
2567

2568
         if (converse_mode(e, converse) == PORT_IN) {
61✔
2569
            tree_t port = tree_ref(name_to_ref(target));
2✔
2570
            diag_t *d = diag_new(DIAG_ERROR, tree_loc(target));
2✔
2571
            diag_printf(d, "cannot assign to element %s of port %s which has "
2✔
2572
                        "mode IN from mode view indication",
2573
                        istr(tree_ident(e)), istr(tree_ident(port)));
2574
            diag_hint(d, tree_loc(target), "target of signal assignment");
2✔
2575
            diag_hint(d, tree_loc(port), "sub-element %s of %s declared with "
2✔
2576
                      "mode IN due to mode view indication",
2577
                      istr(tree_ident(e)), istr(tree_ident(port)));
2578
            diag_emit(d);
2✔
2579
            return NULL;
2✔
2580
         }
2581

2582
         return NULL;
2583
      }
2584

2585
   default:
2586
      return NULL;
2587
   }
2588
}
2589

2590
static bool sem_check_signal_target(tree_t target, nametab_t *tab, bool guarded)
7,607✔
2591
{
2592
   if (tree_kind(target) == T_AGGREGATE) {
7,607✔
2593
      // Rules for aggregate signal targets in LRM 93 section 8.4
2594

2595
      type_t type = tree_type(target);
79✔
2596
      if (!type_is_composite(type))
79✔
UNCOV
2597
         sem_error(target, "aggregate target of signal assignment has "
×
2598
                   "non-composite type %s", type_pp(type));
2599

2600
      bool has_guarded = false, has_unguarded = false;
79✔
2601
      const int nassocs = tree_assocs(target);
79✔
2602
      for (int i = 0; i < nassocs; i++) {
261✔
2603
         tree_t a = tree_assoc(target, i);
191✔
2604
         tree_t value = tree_value(a);
191✔
2605

2606
         if (!sem_check_signal_target(value, tab, guarded))
191✔
2607
            return false;
2608

2609
         if (!sem_check_aggregate_target_element(target, i))
186✔
2610
            return false;
2611

2612
         tree_t ref = name_to_ref(value);
182✔
2613
         if (ref != NULL && is_guarded_signal(tree_ref(ref)))
182✔
2614
            has_guarded = true;
2615
         else
2616
            has_unguarded = true;
2617
      }
2618

2619
      if (has_guarded && has_unguarded) {
70✔
2620
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(target));
1✔
2621
         diag_printf(d, "aggregate target of signal assignment contains both "
1✔
2622
                     "guarded and unguarded signals");
2623
         diag_lrm(d, STD_08, "11.6");
1✔
2624
         diag_emit(d);
1✔
2625
         return false;
1✔
2626
      }
2627

2628
      return true;
2629
   }
2630
   else {
2631
      tree_t decl = sem_check_lvalue(target);
7,528✔
2632
      if (decl == NULL)
7,528✔
2633
         sem_error(target, "target of signal assignment must be a signal "
4✔
2634
                   "name or aggregate");
2635

2636
      if (is_guarded_signal(decl) && !guarded) {
7,524✔
2637
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(target));
1✔
2638
         diag_printf(d, "guarded signal %s cannot be the target of an "
1✔
2639
                     "unguarded assignment", istr(tree_ident(decl)));
2640
         diag_hint(d, tree_loc(decl), "%s declared here",
1✔
2641
                   istr(tree_ident(decl)));
2642
         diag_lrm(d, STD_08, "11.6");
1✔
2643
         diag_emit(d);
1✔
2644
         return false;
1✔
2645
      }
2646

2647
      switch (tree_kind(decl)) {
7,523✔
2648
      case T_SIGNAL_DECL:
5,837✔
2649
         {
2650
            tree_t sub = find_enclosing(tab, S_SUBPROGRAM);
5,837✔
2651
            if (sub != NULL && find_enclosing(tab, S_PROCESS) == NULL) {
5,837✔
2652
               // LRM 08 section 10.5.2.2: if a signal assignment appears
2653
               // in a procedure not contained within a process then the
2654
               // target must be a formal parameter
2655
               diag_t *d = diag_new(DIAG_ERROR, tree_loc(target));
4✔
2656
               diag_printf(d, "signal %s is not a formal parameter and "
4✔
2657
                           "subprogram %s is not contained within a process "
2658
                           "statement", istr(tree_ident(decl)),
2659
                           type_pp(tree_type(sub)));
2660
               diag_lrm(d, STD_08, "10.5.2.2");
4✔
2661
               diag_emit(d);
4✔
2662
               return false;
4✔
2663
            }
2664
         }
2665
         break;
2666

2667
      case T_IMPLICIT_SIGNAL:
1✔
2668
         sem_error(target, "implicit signal may not be assigned");
1✔
2669

2670
      case T_PORT_DECL:
1,656✔
2671
      case T_PARAM_DECL:
2672
         {
2673
            const port_mode_t mode = tree_subkind(decl);
1,656✔
2674
            if (mode == PORT_IN) {
1,656✔
2675
               diag_t *d = diag_new(DIAG_ERROR, tree_loc(target));
5✔
2676
               diag_printf(d, "cannot assign to input %s %s",
11✔
2677
                           tree_kind(decl) == T_PORT_DECL
5✔
2678
                           ? "port" : "parameter",
2679
                           istr(tree_ident(decl)));
2680
               diag_hint(d, tree_loc(target), "target of signal assignment");
5✔
2681
               diag_hint(d, tree_loc(decl), "%s declared with mode IN",
5✔
2682
                         istr(tree_ident(decl)));
2683
               diag_emit(d);
5✔
2684
               return false;
5✔
2685
            }
2686
            else if (mode == PORT_ARRAY_VIEW || mode == PORT_RECORD_VIEW) {
1,651✔
2687
               tree_t view = sem_check_view_target(target);
80✔
2688
               if (view != NULL) {
80✔
2689
                  tree_t inport = NULL;
4✔
2690
                  type_t view_type = tree_type(view);
4✔
2691
                  const int nelems = type_fields(view_type);
4✔
2692
                  for (int i = 0; i < nelems; i++) {
10✔
2693
                     tree_t e = type_field(view_type, i);
7✔
2694
                     const port_mode_t mode = tree_subkind(e);
7✔
2695
                     if (mode == PORT_IN || mode == PORT_ARRAY_VIEW
7✔
2696
                         || mode == PORT_RECORD_VIEW) {
6✔
2697
                        // This is not correct for nested mode view
2698
                        // indications but seems like a very obscure
2699
                        // corner case
2700
                        inport = e;
2701
                        break;
2702
                     }
2703
                  }
2704

2705
                  if (inport != NULL) {
4✔
2706
                     diag_t *d = diag_new(DIAG_ERROR, tree_loc(target));
1✔
2707
                     diag_printf(d, "cannot assign to port %s with mode view "
1✔
2708
                                 "indication as one or more sub-elements have "
2709
                                 "mode IN", istr(tree_ident(decl)));
2710
                     diag_hint(d, tree_loc(target),
1✔
2711
                               "target of signal assignment");
2712
                     diag_hint(d, tree_loc(inport),
1✔
2713
                               "element %s declared with mode IN",
2714
                               istr(tree_ident(inport)));
2715
                     diag_emit(d);
1✔
2716
                     return false;
1✔
2717
                  }
2718
               }
2719

2720
               return true;
79✔
2721
            }
2722
            else if (mode == PORT_LINKAGE)
1,571✔
2723
               sem_error(target, "linkage port %s may not be updated except as "
1✔
2724
                         "an actual corresponding to an interface of mode "
2725
                         "linkage", istr(tree_ident(decl)));
2726
            else if (tree_class(decl) != C_SIGNAL) {
1,570✔
2727
               diag_t *d = diag_new(DIAG_ERROR, tree_loc(target));
1✔
2728
               diag_printf(d, "%s is not a valid target of signal assignment",
1✔
2729
                           istr(tree_ident(decl)));
2730
               diag_hint(d, tree_loc(target), "target of signal assignment");
1✔
2731
               diag_hint(d, tree_loc(decl), "declared with class %s",
1✔
2732
                         class_str(tree_class(decl)));
2733
               diag_emit(d);
1✔
2734
               return false;
1✔
2735
            }
2736
         }
2737
         break;
2738

2739
      case T_EXTERNAL_NAME:
28✔
2740
         {
2741
            if (tree_class(decl) != C_SIGNAL) {
28✔
UNCOV
2742
               tree_t tail = tree_part(decl, tree_parts(decl) - 1);
×
UNCOV
2743
               diag_t *d = diag_new(DIAG_ERROR, tree_loc(target));
×
UNCOV
2744
               diag_printf(d, "external name %s is not a valid target of "
×
2745
                           "signal assignment", istr(tree_ident(tail)));
UNCOV
2746
               diag_hint(d, tree_loc(target), "target of signal assignment");
×
UNCOV
2747
               diag_hint(d, tree_loc(decl), "declared with class %s",
×
2748
                         class_str(tree_class(decl)));
UNCOV
2749
               diag_emit(d);
×
UNCOV
2750
               return false;
×
2751
            }
2752

2753
            tree_t sub = find_enclosing(tab, S_SUBPROGRAM);
28✔
2754
            if (sub != NULL && find_enclosing(tab, S_PROCESS) == NULL)
28✔
2755
               sem_error(target, "cannot create driver for external name as "
1✔
2756
                         "subprogram %s is not contained within a process "
2757
                         "statement", type_pp(tree_type(sub)));
2758
         }
2759
         break;
2760

2761
      case T_VAR_DECL:
1✔
2762
      case T_CONST_DECL:
2763
         {
2764
            diag_t *d = diag_new(DIAG_ERROR, tree_loc(target));
1✔
2765
            diag_printf(d, "%s %s is not a valid target of signal assignment",
1✔
2766
                        class_str(class_of(decl)), istr(tree_ident(decl)));
2767
            diag_hint(d, tree_loc(target), "target of signal assignment");
1✔
2768
            diag_hint(d, tree_loc(decl), "declared as %s",
1✔
2769
                      class_str(class_of(decl)));
2770
            diag_emit(d);
1✔
2771
            return false;
1✔
2772
         }
2773

UNCOV
2774
      default:
×
UNCOV
2775
         sem_error(target, "invalid target of signal assignment");
×
2776
      }
2777

2778
      return true;
7,429✔
2779
   }
2780
}
2781

2782
static bool sem_check_reject(tree_t t, nametab_t *tab)
652✔
2783
{
2784
   if (!sem_check(t, tab))
652✔
2785
      return false;
2786

2787
   if (!type_eq(tree_type(t), std_type(NULL, STD_TIME)))
652✔
2788
      sem_error(t, "reject interval must have type TIME but have %s",
1✔
2789
                type_pp(tree_type(t)));
2790

2791
   return true;
2792
}
2793

2794
static bool sem_check_signal_assign(tree_t t, nametab_t *tab)
5,253✔
2795
{
2796
   tree_t target = tree_target(t);
5,253✔
2797

2798
   if (!sem_check(target, tab))
5,253✔
2799
      return false;
2800

2801
   if (!sem_check_signal_target(target, tab, true))
5,249✔
2802
      return false;
2803

2804
   if (!sem_check_waveforms(t, target, tab))
5,233✔
2805
      return false;
2806

2807
   if (tree_has_reject(t) && !sem_check_reject(tree_reject(t), tab))
5,219✔
UNCOV
2808
      return false;
×
2809

2810
   return true;
2811
}
2812

2813
static bool sem_check_guard(tree_t t, nametab_t *tab)
14✔
2814
{
2815
   assert(tree_kind(t) == T_GUARD);
14✔
2816

2817
   if (!sem_check_type(t, std_type(NULL, STD_BOOLEAN), tab))
14✔
2818
      sem_error(t, "guard signal must have BOOLEAN type but found %s",
1✔
2819
                type_pp(tree_type(t)));
2820

2821
   tree_t decl = tree_ref(t);
13✔
2822
   switch (tree_kind(decl)) {
13✔
2823
   case T_SIGNAL_DECL:
2824
   case T_IMPLICIT_SIGNAL:
2825
      break;
UNCOV
2826
   case T_PORT_DECL:
×
UNCOV
2827
      if (tree_class(decl) == C_SIGNAL)
×
2828
         break;
2829
      // Fall-through
2830
   default:
2831
      {
2832
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
1✔
2833
         diag_printf(d, "assignment guard must be a signal");
1✔
2834
         diag_hint(d, tree_loc(decl), "%s is a %s", istr(tree_ident(decl)),
1✔
2835
                   class_str(class_of(decl)));
2836
         diag_hint(d, tree_loc(t), "guarded statement");
1✔
2837
         diag_emit(d);
1✔
2838
         return false;
1✔
2839
      }
2840
   }
2841

2842
   return true;
2843
}
2844

2845
static bool sem_check_cond_assign(tree_t t, nametab_t *tab)
2,170✔
2846
{
2847
   tree_t target = tree_target(t);
2,170✔
2848

2849
   if (!sem_check(target, tab))
2,170✔
2850
      return false;
2851

2852
   const bool has_guard = tree_has_guard(t);
2,167✔
2853

2854
   if (!sem_check_signal_target(target, tab, has_guard))
2,167✔
2855
      return false;
2856

2857
   if (has_guard && !sem_check_guard(tree_guard(t), tab))
2,158✔
2858
      return false;
2859

2860
   type_t std_bool = std_type(NULL, STD_BOOLEAN);
2,156✔
2861

2862
   const int nconds = tree_conds(t);
2,156✔
2863
   for (int i = 0; i < nconds; i++) {
4,455✔
2864
      tree_t c = tree_cond(t, i);
2,305✔
2865

2866
      if (tree_has_value(c)) {
2,305✔
2867
         tree_t test = tree_value(c);
229✔
2868

2869
         if (!sem_check(test, tab))
229✔
2870
            return false;
2871

2872
         if (!type_eq(tree_type(test), std_bool))
229✔
2873
            sem_error(test, "type of condition must be BOOLEAN");
1✔
2874
      }
2875

2876
      assert(tree_stmts(c) == 1);
2,304✔
2877
      tree_t a = tree_stmt(c, 0);
2,304✔
2878

2879
      assert(tree_kind(a) == T_SIGNAL_ASSIGN);
2,304✔
2880
      assert(tree_target(a) == target);
2,304✔
2881

2882
      if (tree_has_reject(a) && !sem_check_reject(tree_reject(a), tab))
2,304✔
2883
         return false;
2884

2885
      if (!sem_check_waveforms(a, target, tab))
2,303✔
2886
         return false;
2887
   }
2888

2889
   return true;
2890
}
2891

2892
static bool sem_check_closely_related(type_t from, type_t to, tree_t where)
9,547✔
2893
{
2894
   if (type_eq(to, from))
9,547✔
2895
      return true;
2896

2897
   // Conversions are allowed between any abstract numeric types
2898
   if (type_is_numeric(from) && type_is_numeric(to))
5,311✔
2899
      return true;
2900

2901
   // Suppress cascading errors
2902
   if (type_is_none(from) || type_is_none(to))
2,380✔
2903
      return true;
1✔
2904

2905
   char *reason = NULL;
2,379✔
2906

2907
   if (type_is_array(from) && type_is_array(to)) {
2,379✔
2908
      const int from_dims = dimension_of(from);
2,354✔
2909
      const int to_dims = dimension_of(to);
2,354✔
2910

2911
      // Types must have same dimensionality
2912
      if (from_dims != to_dims) {
2,354✔
2913
         reason = xasprintf("%s has %d dimension%s but %s has %d",
1✔
2914
                            type_pp2(from, to), from_dims,
2915
                            from_dims == 1 ? "" : "s",
2916
                            type_pp2(to, from), to_dims);
2917
         goto not_closely_related;
1✔
2918
      }
2919

2920
      // Index types the same or closely related
2921
      for (int i = 0; i < from_dims; i++) {
4,706✔
2922
         type_t from_index = index_type_of(from, i);
2,354✔
2923
         type_t to_index = index_type_of(to, i);
2,354✔
2924

2925
         if (!sem_check_closely_related(from_index, to_index, NULL)) {
2,354✔
2926
            reason = xasprintf("%s index type of %s is %s which is not closely "
1✔
2927
                               "related to the %s index type of %s",
2928
                               ordinal_str(i + 1), type_pp2(from, to),
2929
                               type_pp(from_index), ordinal_str(i + 1),
2930
                               type_pp2(to, from));
2931
            goto not_closely_related;
1✔
2932
         }
2933
      }
2934

2935
      type_t from_e = type_elem(from);
2,352✔
2936
      type_t to_e = type_elem(to);
2,352✔
2937

2938
      if (standard() >= STD_08) {
2,352✔
2939
         // Element types must be closely related
2940
         if (!sem_check_closely_related(from_e, to_e, NULL)) {
1,774✔
2941
            reason = xasprintf("element type %s is not closely related to %s",
1✔
2942
                               type_pp2(from_e, to_e), type_pp2(to_e, from_e));
2943
            goto not_closely_related;
1✔
2944
         }
2945
      }
2946
      else {
2947
         // Element types must be the same
2948
         if (!type_eq(from_e, to_e)) {
578✔
2949
            reason = xasprintf("element type %s does not match %s",
1✔
2950
                               type_pp2(from_e, to_e), type_pp2(to_e, from_e));
2951
            goto not_closely_related;
1✔
2952
         }
2953
      }
2954

2955
      return true;
2,350✔
2956
   }
2957

2958
   if (type_is_record(from) && type_is_record(to) && standard() >= STD_19) {
25✔
2959
      // Each element of the target type must have a matching element in
2960
      // the from type
2961
      const int from_nf = type_fields(from);
21✔
2962
      const int to_nf = type_fields(to);
21✔
2963

2964
      for (int i = 0; i < to_nf; i++) {
68✔
2965
         tree_t to_f = type_field(to, i), from_f = NULL;
48✔
2966
         type_t to_ftype = tree_type(to_f);
48✔
2967
         ident_t name = tree_ident(to_f);
48✔
2968

2969
         for (int j = 0; j < from_nf && from_f == NULL; j++) {
130✔
2970
            tree_t f = type_field(from, j);
82✔
2971
            if (tree_ident(f) != name)
82✔
2972
               continue;
35✔
2973

2974
            type_t from_ftype = tree_type(f);
47✔
2975
            if (!sem_check_closely_related(from_ftype, to_ftype, NULL))
47✔
2976
               break;
2977

2978
            from_f = f;
2979
         }
2980

2981
         if (from_f != NULL)
48✔
2982
            continue;
47✔
2983

2984
         reason = xasprintf("field %s in record type %s has no matching "
1✔
2985
                            "element in type %s", istr(tree_ident(to_f)),
2986
                            type_pp2(to, from), type_pp2(from, to));
2987
         goto not_closely_related;
1✔
2988
      }
2989

2990
      return true;
2991
   }
2992

2993
 not_closely_related:
4✔
2994
   if (where != NULL) {
9✔
2995
      const loc_t *loc = tree_loc(where);
7✔
2996
      diag_t *d = diag_new(DIAG_ERROR, loc);
7✔
2997
      diag_printf(d, "conversion only allowed between closely related types");
7✔
2998
      if (reason != NULL)
7✔
2999
         diag_hint(d, loc, "%s", reason);
5✔
3000
      else
3001
         diag_hint(d, loc, "%s and %s are not closely related",
2✔
3002
                   type_pp2(from, to), type_pp2(to, from));
3003
      diag_lrm(d, STD_93, "7.3.5");
7✔
3004
      diag_emit(d);
7✔
3005

3006
      free(reason);
7✔
3007
   }
3008

3009
   return false;
3010
}
3011

3012
static bool sem_check_conversion(tree_t t, nametab_t *tab)
5,372✔
3013
{
3014
   // Type conversions are described in LRM 93 section 7.3.5
3015

3016
   tree_t value = tree_value(t);
5,372✔
3017
   if (!sem_check(value, tab))
5,372✔
3018
      return false;
3019

3020
   return sem_check_closely_related(tree_type(value), tree_type(t), t);
5,372✔
3021
}
3022

3023
static bool sem_check_compatible_view(tree_t formal, tree_t actual)
93✔
3024
{
3025
   type_t type = tree_type(formal);
93✔
3026

3027
   type_t elem_type = type;
93✔
3028
   if (tree_subkind(formal) == PORT_ARRAY_VIEW)
93✔
3029
      elem_type = type_elem(type);
12✔
3030

3031
   tree_t formal_view = tree_value(formal);
93✔
3032

3033
   tree_t actual_view = sem_check_view_target(actual);
93✔
3034
   if (actual_view != NULL) {
93✔
3035
      // Associating an interface with another interface: check the
3036
      // mode of each element is compatible
3037
      const int nfields = type_fields(elem_type);
13✔
3038
      for (int i = 0; i < nfields; i++) {
43✔
3039
         tree_t f = type_field(elem_type, i);
32✔
3040

3041
         bool formal_converse = false;
32✔
3042
         tree_t formal_elem = find_element_mode_indication(formal_view, f,
32✔
3043
                                                           &formal_converse);
3044

3045
         bool actual_converse = false;
32✔
3046
         tree_t actual_elem = find_element_mode_indication(actual_view, f,
32✔
3047
                                                           &actual_converse);
3048

3049
         const port_mode_t formal_mode =
32✔
3050
            converse_mode(formal_elem, formal_converse);
32✔
3051

3052
         const port_mode_t actual_mode =
32✔
3053
            converse_mode(actual_elem, actual_converse);
32✔
3054

3055
         if (formal_mode != actual_mode) {
32✔
3056
            diag_t *d = diag_new(DIAG_ERROR, tree_loc(actual));
2✔
3057
            diag_printf(d, "mode view indication of formal %s %s "
5✔
3058
                        "element %s is not compatible with actual",
3059
                        tree_kind(formal) == T_PORT_DECL ? "port" : "parameter",
2✔
3060
                        istr(tree_ident(formal)), istr(tree_ident(f)));
3061
            diag_hint(d, tree_loc(actual_view), "actual has mode %s from "
2✔
3062
                      "mode view indication on port %s",
3063
                      port_mode_str(actual_mode),
3064
                      istr(tree_ident(name_to_ref(actual))));
3065
            diag_hint(d, tree_loc(formal_view), "formal has mode %s",
2✔
3066
                      port_mode_str(formal_mode));
3067
            diag_emit(d);
2✔
3068
            return false;
2✔
3069
         }
3070
      }
3071
   }
3072

3073
   return true;
3074
}
3075

3076
static bool sem_check_call_args(tree_t t, tree_t decl, nametab_t *tab)
91,388✔
3077
{
3078
   const int nparams = tree_params(t);
91,388✔
3079
   const int nports  = tree_ports(decl);
91,388✔
3080

3081
   if (is_uninstantiated_subprogram(decl)) {
91,388✔
3082
      // Allow recursive calls to the same subprogram
3083
      tree_t sub = find_enclosing(tab, S_SUBPROGRAM);
5✔
3084
      if (sub != decl)
5✔
3085
         sem_error(t, "cannot call uninstantiated %s %s",
2✔
3086
                   class_str(class_of(decl)), istr(tree_ident(decl)));
3087
   }
3088

3089
   tree_t *map LOCAL = xcalloc_array(nports, sizeof(tree_t));
182,772✔
3090

3091
   bool have_named = false;
91,386✔
3092
   for (int i = 0; i < nparams; i++) {
258,915✔
3093
      tree_t param = tree_param(t, i), port = NULL;
167,618✔
3094
      type_t port_type = NULL;
167,618✔
3095
      bool partial = false;
167,618✔
3096
      int index = -1;
167,618✔
3097
      switch (tree_subkind(param)) {
167,618✔
3098
      case P_POS:
164,162✔
3099
         if (have_named)
164,162✔
3100
            sem_error(param, "positional parameters must precede named "
3✔
3101
                      "parameters");
3102
         else if ((index = tree_pos(param)) >= nports) {
164,159✔
3103
            diag_t *d = diag_new(DIAG_ERROR, tree_loc(param));
1✔
3104
            diag_printf(d, "too many positional parameters for subprogram %s",
1✔
3105
                        type_pp(tree_type(decl)));
3106
            diag_hint(d, tree_loc(param), "%s positional parameter",
1✔
3107
                      ordinal_str(index + 1));
3108
            diag_hint(d, tree_loc(decl), "%s %s has %d formal parameter%s",
2✔
3109
                      class_str(class_of(decl)), istr(tree_ident(decl)),
3110
                      nports, nports > 1 ? "s" : "");
3111
            diag_emit(d);
1✔
3112
            return false;
1✔
3113
         }
3114
         else {
3115
            port = tree_port(decl, index);
164,158✔
3116
            port_type = tree_type(port);
164,158✔
3117
         }
3118
         break;
164,158✔
3119

3120
      case P_NAMED:
3,456✔
3121
         {
3122
            have_named = true;
3,456✔
3123

3124
            tree_t name = tree_name(param);
3,456✔
3125
            const tree_kind_t name_kind = tree_kind(name);
3,456✔
3126
            if (name_kind == T_TYPE_CONV || name_kind == T_FCALL)
3,456✔
3127
               sem_error(name, "sorry, conversions are not yet supported here");
1✔
3128
            else if (!sem_check(name, tab))
3,455✔
3129
               return false;
3130

3131
            if (tree_kind(name) == T_TYPE_CONV) {
3,450✔
UNCOV
3132
               type_t to_type = tree_type(name);
×
UNCOV
3133
               type_t from_type = tree_type(tree_value(name));
×
3134

UNCOV
3135
               const bool missing_constraints =
×
UNCOV
3136
                  type_is_unconstrained(from_type)
×
UNCOV
3137
                  && type_is_unconstrained(to_type);
×
3138

UNCOV
3139
               if (missing_constraints) {
×
UNCOV
3140
                  diag_t *d = diag_new(DIAG_ERROR, tree_loc(name));
×
3141
                  diag_printf(d, "result of conversion for unconstrained "
×
3142
                              "formal %s must be a constrained array type",
3143
                              istr(tree_ident(decl)));
UNCOV
3144
                  diag_lrm(d, STD_93, "3.2.1.1");
×
UNCOV
3145
                  diag_emit(d);
×
UNCOV
3146
                  return false;
×
3147
               }
3148
            }
3149

3150
            tree_t ref = name_to_ref(name);
3,450✔
3151
            assert(ref != NULL);
3,450✔
3152

3153
            if ((partial = (ref != name))) {
3,450✔
3154
               tree_t value = tree_value(name);
57✔
3155
               if (tree_kind(value) != T_REF)
57✔
UNCOV
3156
                  sem_error(name, "sorry, this form of named parameter is "
×
3157
                            "not supported");
3158
            }
3159

3160
            ident_t id = tree_ident(ref);
3,450✔
3161
            for (int j = 0; j < nports; j++) {
13,203✔
3162
               tree_t p = tree_port(decl, j);
13,202✔
3163
               if (tree_ident(p) == id) {
13,202✔
3164
                  index = j;
3165
                  port = p;
3166
                  break;
3167
               }
3168
            }
3169

3170
            if (index == -1) {
3,450✔
3171
               diag_t *d = diag_new(DIAG_ERROR, tree_loc(param));
1✔
3172
               diag_printf(d, "subprogram %s has no parameter named %s",
1✔
3173
                           type_pp(tree_type(decl)), istr(id));
3174
               diag_hint(d, tree_loc(decl), "subprogram defined here");
1✔
3175
               diag_emit(d);
1✔
3176
               return false;
1✔
3177
            }
3178

3179
            if (!tree_has_ref(ref)) {
3,449✔
3180
               // Should have generated an error during overload
3181
               // resolution
UNCOV
3182
               assert(error_count() > 0);
×
3183
               return false;
3184
            }
3185

3186
            // Set the ref again here because solve_types may have set it
3187
            // to the wrong overload
3188
            if (tree_ref(ref) != port)
3,449✔
3189
               tree_set_name(param, (name = change_ref(name, port)));
776✔
3190

3191
            port_type = tree_type(name);
3,449✔
3192
         }
3193
      }
3194

3195
      class_t class    = tree_class(port);
167,607✔
3196
      port_mode_t mode = tree_subkind(port);
167,607✔
3197

3198
      if (map[index] != NULL && (!partial || tree_kind(map[index]) == T_REF))
167,607✔
3199
         sem_error(param, "formal parameter %s already has an associated "
2✔
3200
                   "actual", istr(tree_ident(port)));
3201

3202
      map[index] = param;
167,605✔
3203

3204
      tree_t value = tree_value(param);
167,605✔
3205
      if (!sem_check(value, tab))
167,605✔
3206
         return false;
3207

3208
      if (!sem_check_type(value, port_type, tab))
167,556✔
3209
         sem_error(value, "type of actual %s does not match formal %s type %s",
6✔
3210
                   type_pp2(tree_type(value), port_type),
3211
                   istr(tree_ident(port)),
3212
                   type_pp2(port_type, tree_type(value)));
3213

3214
      // LRM 08 sections 4.2.2.2 and 4.2.2.3
3215
      if (class == C_VARIABLE || class == C_SIGNAL) {
167,550✔
3216
         tree_t decl = sem_check_lvalue(value);
6,329✔
3217
         if (decl == NULL || class_of(value) != class) {
6,329✔
3218
            diag_t *d = diag_new(DIAG_ERROR, tree_loc(value));
8✔
3219
            diag_printf(d, "actual for formal %s with class %s must be "
12✔
3220
                        "a name denoting a %s", istr(tree_ident(port)),
3221
                        class == C_VARIABLE ? "VARIABLE" : "SIGNAL",
3222
                        class_str(class));
3223
            if (decl == NULL)
8✔
3224
               diag_hint(d, tree_loc(value), "actual designator is not a name");
4✔
3225
            else if (tree_kind(decl) == T_EXTERNAL_NAME)
4✔
3226
               diag_hint(d, tree_loc(value), "external name has class %s",
1✔
3227
                         class_str(tree_class(decl)));
3228
            else
3229
               diag_hint(d, tree_loc(value), "object %s has class %s",
3✔
3230
                         istr(tree_ident(decl)), class_str(class_of(decl)));
3231
            diag_lrm(d, STD_08, class == C_SIGNAL ? "4.2.2.3" : "4.2.2.2");
12✔
3232
            diag_emit(d);
8✔
3233
            return false;
8✔
3234
         }
3235

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

3279
      if (class == C_SIGNAL && !sem_static_name(value, sem_globally_static)) {
167,538✔
3280
         diag_t *d = pedantic_diag(tree_loc(value));
4✔
3281
         if (d != NULL) {
4✔
3282
            diag_printf(d, "actual associated with signal parameter %s must be "
4✔
3283
                        "denoted by a static signal name",
3284
                        istr(tree_ident(port)));
3285
            diag_hint(d, tree_loc(value), "not a static signal name");
4✔
3286
            diag_lrm(d, STD_08, "4.2.2.3");
4✔
3287
            diag_lrm(d, STD_08, "8.1");
4✔
3288
            diag_emit(d);
4✔
3289
            return false;
4✔
3290
         }
3291
      }
3292

3293
      // Check IN and INOUT parameters can be read
3294
      if (tree_kind(t) != T_ATTR_REF) {
167,534✔
3295
         const port_mode_t mode = tree_subkind(port);
167,534✔
3296
         if (mode == PORT_IN || mode == PORT_INOUT) {
167,534✔
3297
            if (!sem_check_readable(value))
165,243✔
3298
               return false;
3299
         }
3300
      }
3301

3302
      if (tree_kind(value) == T_OPEN && !tree_has_value(port)) {
167,530✔
3303
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(param));
1✔
3304
         diag_printf(d, "OPEN actual for formal parameter %s without "
1✔
3305
                     "default value", istr(tree_ident(port)));
3306
         diag_hint(d, tree_loc(port), "%s declared here",
1✔
3307
                   istr(tree_ident(port)));
3308
         diag_emit(d);
1✔
3309
         return false;
1✔
3310
      }
3311
   }
3312

3313
   for (int i = 0; i < nports; i++) {
264,625✔
3314
      if (map[i] == NULL) {
173,331✔
3315
         tree_t port = tree_port(decl, i);
5,847✔
3316
         if (!tree_has_value(port))
5,847✔
3317
            sem_error(t, "missing actual for formal parameter %s without "
2✔
3318
                      "default value", istr(tree_ident(port)));
3319
         else {
3320
            const port_mode_t mode = tree_subkind(port);
5,845✔
3321
            if (mode == PORT_ARRAY_VIEW || mode == PORT_RECORD_VIEW)
5,845✔
3322
               sem_error(t, "missing actual for formal parameter %s with "
173,329✔
3323
                         "mode view indication %s", istr(tree_ident(port)),
3324
                         type_pp(tree_type(tree_value(port))));
3325
         }
3326
      }
3327
   }
3328

3329
   return true;
3330
}
3331

3332
static bool sem_check_fcall(tree_t t, nametab_t *tab)
83,538✔
3333
{
3334
   if (!tree_has_ref(t))
83,538✔
3335
      return false;
3336

3337
   if (tree_kind(t) == T_PROT_FCALL && tree_has_name(t)) {
83,453✔
3338
      tree_t name = tree_name(t);
797✔
3339
      if (!sem_check(name, tab))
797✔
3340
         return false;
3341
   }
3342

3343
   tree_t decl = tree_ref(t), sub;
83,453✔
3344
   const tree_flags_t flags = tree_flags(decl);
83,453✔
3345

3346
   if ((flags & TREE_F_IMPURE) && (sub = find_enclosing(tab, S_SUBPROGRAM))) {
83,453✔
3347
      // Pure function may not call an impure function
3348
      if (tree_kind(sub) == T_FUNC_BODY && !(tree_flags(sub) & TREE_F_IMPURE)) {
1,036✔
3349
         diag_t *d = pedantic_diag(tree_loc(t));
2✔
3350
         if (d != NULL) {
2✔
3351
            diag_printf(d, "pure function %s cannot call impure function %s",
2✔
3352
                        istr(tree_ident(sub)), istr(tree_ident(decl)));
3353
            diag_emit(d);
2✔
3354
         }
3355
      }
3356

3357
      // Propagate impurity flags
3358
      tree_set_flag(sub, flags & (TREE_F_IMPURE_FILE | TREE_F_IMPURE_SHARED));
1,036✔
3359
   }
3360
   else if (tree_kind(decl) == T_FUNC_DECL && !(flags & TREE_F_PREDEFINED)
82,417✔
3361
            && is_same_region(tab, decl) && opt_get_int(OPT_MISSING_BODY)) {
13,189✔
3362
      diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
1✔
3363
      diag_printf(d, "subprogram %s called before its body has been "
1✔
3364
                  "elaborated", type_pp(tree_type(decl)));
3365
      diag_hint(d, tree_loc(decl), "%s declared here",
1✔
3366
                type_pp(tree_type(decl)));
3367
      diag_lrm(d, STD_08, "14.4.2");
1✔
3368
      diag_emit(d);
1✔
3369
      return false;
1✔
3370
   }
3371

3372
   if (!sem_check_call_args(t, decl, tab))
83,452✔
3373
      return false;
3374

3375
   if (sem_locally_static(t))
83,397✔
3376
      tree_set_flag(t, TREE_F_LOCALLY_STATIC | TREE_F_GLOBALLY_STATIC);
11,994✔
3377
   else if (sem_globally_static(t))
71,403✔
3378
      tree_set_flag(t, TREE_F_GLOBALLY_STATIC);
12,227✔
3379

3380
   return true;
3381
}
3382

3383
static bool sem_check_pcall(tree_t t, nametab_t *tab)
7,974✔
3384
{
3385
   if (!tree_has_ref(t))
7,974✔
3386
      return false;
3387

3388
   const bool is_protected = (tree_kind(t) == T_PROT_PCALL);
7,938✔
3389
   if (is_protected && tree_has_name(t)
7,938✔
3390
       && !sem_check(tree_name(t), tab))
200✔
3391
      return false;
3392

3393
   tree_t decl = tree_ref(t);
7,937✔
3394

3395
   switch (class_of(decl)) {
7,937✔
3396
   case C_PROCEDURE:
3397
      break;
7,936✔
3398
   case C_FUNCTION:
1✔
3399
      sem_error(t, "function %s cannot be called as a procedure",
1✔
3400
                type_pp(tree_type(decl)));
UNCOV
3401
   default:
×
3402
      // All other errors should be caught at parsing stage
UNCOV
3403
      assert(error_count() > 0);
×
3404
      return false;
3405
   }
3406

3407
   if (!sem_check_call_args(t, decl, tab))
7,936✔
3408
      return false;
3409

3410
   const tree_flags_t flags = tree_flags(decl);
7,897✔
3411

3412
   const bool never_waits = is_protected || !!(flags & TREE_F_NEVER_WAITS);
7,897✔
3413
   const bool has_wait = !is_protected && !!(flags & TREE_F_HAS_WAIT);
7,897✔
3414

3415
   assert(!never_waits || !has_wait);
7,897✔
3416

3417
   tree_t sub = find_enclosing(tab, S_SUBPROGRAM);
7,897✔
3418
   if (sub != NULL) {
7,897✔
3419
      if (!never_waits)
3,214✔
3420
         tree_clear_flag(sub, TREE_F_NEVER_WAITS);
164✔
3421

3422
      if (has_wait)
3,214✔
3423
         tree_set_flag(sub, TREE_F_HAS_WAIT);
27✔
3424

3425
      if (flags & TREE_F_IMPURE_FILE)
3,214✔
3426
         tree_set_flag(sub, TREE_F_IMPURE_FILE);
168✔
3427

3428
      if (flags & TREE_F_IMPURE_SHARED)
3,214✔
3429
         tree_set_flag(sub, TREE_F_IMPURE_SHARED);
6✔
3430

3431
      const bool in_func = tree_kind(sub) == T_FUNC_BODY;
3,214✔
3432
      const bool in_pure_func = in_func && !(tree_flags(sub) & TREE_F_IMPURE);
3,214✔
3433

3434
      if (has_wait && in_func)
3,214✔
3435
         sem_error(t, "function %s cannot call procedure %s which contains "
2✔
3436
                   "a wait statement", istr(tree_ident(sub)),
3437
                   istr(tree_ident(decl)));
3438
      else if ((flags & TREE_F_IMPURE_FILE) && in_pure_func) {
3,212✔
3439
         diag_t *d = pedantic_diag(tree_loc(t));
1✔
3440
         if (d != NULL) {
1✔
3441
            diag_printf(d, "pure function %s cannot call procedure %s which "
1✔
3442
                        "references a file object", istr(tree_ident(sub)),
3443
                        istr(tree_ident(decl)));
3444
            diag_emit(d);
1✔
3445
         }
3446
      }
3447
      else if ((flags & TREE_F_IMPURE_SHARED) && in_pure_func) {
3,211✔
3448
         diag_t *d = pedantic_diag(tree_loc(t));
4✔
3449
         if (d != NULL) {
4✔
3450
            diag_printf(d, "pure function %s cannot call procedure %s which "
4✔
3451
                        "references a shared variable", istr(tree_ident(sub)),
3452
                        istr(tree_ident(decl)));
3453
            diag_emit(d);
4✔
3454
         }
3455
      }
3456
   }
3457

3458
   if (!never_waits) {
7,895✔
3459
      // Procedure may wait, suppress infinite loop warning
3460
      tree_t proc = find_enclosing(tab, S_PROCESS);
868✔
3461
      if (proc != NULL)
868✔
3462
         tree_set_flag(proc, TREE_F_HAS_WAIT);
686✔
3463
   }
3464

3465
   return true;
3466
}
3467

3468
static bool sem_check_wait(tree_t t, nametab_t *tab)
9,489✔
3469
{
3470
   if (tree_has_delay(t)) {
9,489✔
3471
      type_t std_time = std_type(NULL, STD_TIME);
5,075✔
3472
      tree_t delay = tree_delay(t);
5,075✔
3473

3474
      if (!sem_check(delay, tab))
5,075✔
3475
         return false;
3476

3477
      if (!sem_check_type(delay, std_time, tab))
5,074✔
3478
         sem_error(delay, "type of delay must be %s but have %s",
2✔
3479
                   type_pp(std_time), type_pp(tree_type(delay)));
3480
   }
3481

3482
   if (tree_has_value(t)) {
9,486✔
3483
      type_t std_bool = std_type(NULL, STD_BOOLEAN);
274✔
3484
      tree_t value = tree_value(t);
274✔
3485

3486
      if (!sem_check(value, tab))
274✔
3487
         return false;
3488

3489
      if (!sem_check_type(value, std_bool, tab))
274✔
3490
         sem_error(value, "type of condition must be BOOLEAN but have %s",
1✔
3491
                   type_pp(tree_type(value)));
3492
   }
3493

3494
   if (find_enclosing(tab, S_PROTECTED))
9,485✔
3495
      sem_error(t, "wait statement not allowed in protected subprogram body");
1✔
3496

3497
   tree_t sub = find_enclosing(tab, S_SUBPROGRAM);
9,484✔
3498
   if (sub != NULL) {
9,484✔
3499
      if (tree_kind(sub) == T_FUNC_BODY)
402✔
3500
         sem_error(t, "wait statement not allowed in function body");
1✔
3501

3502
      tree_clear_flag(sub, TREE_F_NEVER_WAITS);
401✔
3503
      tree_set_flag(sub, TREE_F_HAS_WAIT);
401✔
3504
   }
3505

3506
   tree_t proc = find_enclosing(tab, S_PROCESS);
9,483✔
3507
   if (proc != NULL) {
9,483✔
3508
      // No wait statements allowed in process with sensitivity list
3509
      if (tree_triggers(proc) > 0)
9,115✔
3510
         sem_error(t, "wait statement not allowed in process with "
2✔
3511
                  "sensitvity list");
3512

3513
      tree_set_flag(proc, TREE_F_HAS_WAIT);
9,113✔
3514
   }
3515

3516
   return sem_check_sensitivity(t, tab);
9,481✔
3517
}
3518

3519
static bool sem_check_assert(tree_t t, nametab_t *tab)
17,598✔
3520
{
3521
   // Rules for asserion statements are in LRM 93 section 8.2
3522

3523
   tree_t value = tree_value(t);
17,598✔
3524
   if (!sem_check(value, tab))
17,598✔
3525
      return false;
3526

3527
   type_t std_bool = std_type(NULL, STD_BOOLEAN);
17,548✔
3528
   if (!sem_check_type(value, std_bool, tab))
17,548✔
3529
      sem_error(value, "type of assertion expression must be %s but "
1✔
3530
                "is %s", type_pp(std_bool), type_pp(tree_type(value)));
3531

3532
   if (tree_has_message(t)) {
17,547✔
3533
      tree_t message = tree_message(t);
6,386✔
3534
      if (!sem_check(message, tab))
6,386✔
3535
         return false;
3536

3537
      type_t std_string = std_type(NULL, STD_STRING);
6,386✔
3538
      if (!sem_check_type(message, std_string, tab))
6,386✔
UNCOV
3539
         sem_error(message, "type of message be %s but is %s",
×
3540
                   type_pp(std_string), type_pp(tree_type(message)));
3541
   }
3542

3543
   if (tree_has_severity(t)) {
17,547✔
3544
      tree_t severity = tree_severity(t);
5,986✔
3545
      if (!sem_check(severity, tab))
5,986✔
3546
         return false;
3547

3548
      type_t std_severity = std_type(NULL, STD_SEVERITY_LEVEL);
5,986✔
3549
      if (!sem_check_type(severity, std_severity, tab))
5,986✔
UNCOV
3550
         sem_error(severity, "type of severity must be %s but is %s",
×
3551
                   type_pp(std_severity), type_pp(tree_type(severity)));
3552
   }
3553

3554
   return true;
3555
}
3556

3557
static bool sem_check_report(tree_t t, nametab_t *tab)
2,021✔
3558
{
3559
   tree_t message = tree_message(t);
2,021✔
3560
   if (!sem_check(message, tab))
2,021✔
3561
      return false;
3562

3563
   type_t std_string = std_type(NULL, STD_STRING);
2,011✔
3564
   if (!sem_check_type(message, std_string, tab))
2,011✔
3565
      sem_error(message, "type of message be %s but is %s",
1✔
3566
                type_pp(std_string), type_pp(tree_type(message)));
3567

3568
   if (tree_has_severity(t)) {
2,010✔
3569
      tree_t severity = tree_severity(t);
439✔
3570
      if (!sem_check(severity, tab))
439✔
3571
         return false;
3572

3573
      type_t std_severity = std_type(NULL, STD_SEVERITY_LEVEL);
439✔
3574
      if (!sem_check_type(severity, std_severity, tab))
439✔
3575
         sem_error(severity, "type of severity must be %s but is %s",
1✔
3576
                   type_pp(std_severity), type_pp(tree_type(severity)));
3577
   }
3578

3579
   return true;
3580
}
3581

3582
static bool sem_check_string_literal(tree_t t)
25,283✔
3583
{
3584
   // String literals are in LRM 93 section 7.3.1
3585

3586
   type_t type = tree_type(t);
25,283✔
3587
   type_t elem = type_base_recur(type_elem(type));
25,283✔
3588

3589
   if (type_is_none(elem))
25,283✔
3590
      return false;
3591

3592
   const int nlits = type_enum_literals(elem);
25,283✔
3593
   const int nchars = tree_chars(t);
25,283✔
3594
   for (int i = 0; i < nchars; i++) {
377,232✔
3595
      tree_t ch = tree_char(t, i);
351,951✔
3596

3597
      ident_t ch_i = tree_ident(ch);
351,951✔
3598
      bool valid = false;
351,951✔
3599
      for (int j = 0; !valid && (j < nlits); j++) {
24,920,735✔
3600
         tree_t lit = type_enum_literal(elem, j);
24,568,784✔
3601
         if (ch_i == tree_ident(lit))
24,568,784✔
3602
            valid = true;
351,949✔
3603
      }
3604

3605
      if (!valid)
351,951✔
3606
         sem_error(t, "invalid character %s in string literal of type %s",
351,951✔
3607
                   istr(ch_i), type_pp(type));
3608
   }
3609

3610
   return true;
3611
}
3612

3613
static bool sem_check_literal(tree_t t)
91,510✔
3614
{
3615
   type_t type = tree_type(t);
91,510✔
3616
   if (type_is_none(type))
91,510✔
3617
      return false;
3✔
3618

3619
   return true;
3620
}
3621

3622
static bool sem_check_array_aggregate(tree_t t, nametab_t *tab)
6,486✔
3623
{
3624
   type_t composite_type = tree_type(t);
6,486✔
3625
   type_t base_type = type_base_recur(composite_type);
6,486✔
3626

3627
   const bool unconstrained = type_is_unconstrained(composite_type);
6,486✔
3628

3629
   type_t elem_type = NULL;
6,486✔
3630
   const int ndims = dimension_of(composite_type);
6,486✔
3631
   if (ndims == 1)
6,486✔
3632
      elem_type = type_elem(base_type);
6,194✔
3633
   else {
3634
      // Higher dimensions must be specified with a sub-aggregate or
3635
      // string literal
3636
      tree_t a0 = tree_value(tree_assoc(t, 0));
292✔
3637
      const tree_kind_t a0_kind = tree_kind(a0);
292✔
3638
      if (a0_kind != T_AGGREGATE && a0_kind != T_STRING)
292✔
3639
         sem_error(a0, "second dimension of %d dimensional array type %s must "
1✔
3640
                   "be specified by a sub-aggregate, string, or bit-string "
3641
                   "literal", ndims, type_pp(composite_type));
3642

3643
      // The parser will have constructed a type with ndims - 1
3644
      // dimensions.
3645
      elem_type = tree_type(tree_value(tree_assoc(t, 0)));
291✔
3646

3647
      if (!type_is_unconstrained(elem_type)) {
291✔
3648
         if (!sem_check_array_dims(elem_type, NULL, tab))
284✔
3649
            return false;
3650
      }
3651
   }
3652

3653
   type_t index_type = index_type_of(composite_type, 0);
6,485✔
3654

3655
   bool have_named = false;
6,485✔
3656
   bool have_pos = false;
6,485✔
3657

3658
   const int nassocs = tree_assocs(t);
6,485✔
3659
   for (int i = 0; i < nassocs; i++) {
38,197✔
3660
      tree_t a = tree_assoc(t, i);
31,725✔
3661

3662
      const assoc_kind_t akind = tree_subkind(a);
31,725✔
3663
      switch (akind) {
31,725✔
3664
      case A_RANGE:
635✔
3665
      case A_SLICE:
3666
         {
3667
            tree_t r = tree_range(a, 0);
635✔
3668
            if (!sem_check_discrete_range(r, index_type, tab))
635✔
3669
               return false;
3670

3671
            have_named = true;
3672
         }
3673
         break;
3674

3675
      case A_NAMED:
1,330✔
3676
         {
3677
            tree_t name = tree_name(a);
1,330✔
3678

3679
            if (!sem_check(name, tab))
1,330✔
3680
               return false;
3681

3682
            if (!sem_check_type(name, index_type, tab))
1,330✔
3683
               sem_error(name, "type of array aggregate choice %s does not "
1✔
3684
                         "match %s index type %s", type_pp(tree_type(name)),
3685
                         type_pp(composite_type), type_pp(index_type));
3686

3687
            have_named = true;
3688
         }
3689
         break;
3690

3691
      case A_POS:
27,308✔
3692
      case A_CONCAT:
3693
         have_pos = true;
27,308✔
3694
         break;
27,308✔
3695

3696
      case A_OTHERS:
2,452✔
3697
         if (unconstrained)
2,452✔
3698
            sem_error(a, "index range of array aggregate with others choice "
3✔
3699
                      "cannot be determined from the context");
3700
         break;
3701
      }
3702

3703
      tree_t value = tree_value(a);
31,718✔
3704

3705
      if (!sem_check(value, tab))
31,718✔
3706
         return false;
3707

3708
      if (!sem_check_type(value, elem_type, tab)) {
31,715✔
3709
         // LRM 08 section 9.3.3.3 allows the association to be of the
3710
         // base aggregate type as well
3711
         const bool allow_slice =
484✔
3712
            (akind == A_CONCAT || akind == A_SLICE)
242✔
3713
            || (ndims == 1 && standard() >= STD_08
242✔
3714
                && (akind == A_POS || akind == A_RANGE));
3✔
3715

3716
         if (allow_slice && !sem_check_type(value, composite_type, tab))
242✔
3717
            sem_error(value, "type of %s association %s does not match "
1✔
3718
                      "aggregate element type %s or the aggregate type "
3719
                      "itself %s", assoc_kind_str(akind),
3720
                      type_pp(tree_type(value)), type_pp(elem_type),
3721
                      type_pp(composite_type));
3722
         else if (!allow_slice)
241✔
3723
            sem_error(value, "type of %s association %s does not match "
2✔
3724
                      "aggregate element type %s", assoc_kind_str(akind),
3725
                      type_pp(tree_type(value)), type_pp(elem_type));
3726
         else
3727
            assert(akind == A_CONCAT || akind == A_SLICE);
239✔
3728
      }
3729
   }
3730

3731
   // Named and positional associations cannot be mixed in array
3732
   // aggregates
3733

3734
   if (have_named && have_pos)
6,472✔
3735
      sem_error(t, "named and positional associations cannot be "
2✔
3736
                "mixed in array aggregates");
3737

3738
   // If a choice is not locally static then it must be the only element
3739

3740
   if (have_named && nassocs > 1) {
6,470✔
3741
      for (int i = 0; i < nassocs; i++) {
1,951✔
3742
         tree_t a = tree_assoc(t, i);
1,527✔
3743
         tree_t choice = NULL;
1,527✔
3744
         switch (tree_subkind(a)) {
1,527✔
3745
         case A_NAMED: choice = tree_name(a); break;
1,186✔
3746
         case A_SLICE:
168✔
3747
         case A_RANGE: choice = tree_range(a, 0); break;
168✔
3748
         }
3749

3750
         if (choice && !sem_locally_static(choice))
1,354✔
3751
            sem_error(choice, "a choice that is not locally static is allowed"
1,527✔
3752
                      " only if the array aggregate contains a single element"
3753
                      " association");
3754
      }
3755
   }
3756

3757
   return true;
3758
}
3759

3760
static bool sem_check_record_aggregate(tree_t t, nametab_t *tab)
2,429✔
3761
{
3762
   // Checks for record aggregates are given in LRM 93 section 7.3.2.1
3763

3764
   type_t composite_type = tree_type(t);
2,429✔
3765
   type_t base_type = type_base_recur(composite_type);
2,429✔
3766

3767
   const int nfields = type_fields(base_type);
2,429✔
3768
   int pos = 0;
2,429✔
3769

3770
   LOCAL_BIT_MASK have;
4,858✔
3771
   mask_init(&have, nfields);
2,429✔
3772

3773
   const int nassocs = tree_assocs(t);
2,429✔
3774
   for (int i = 0; i < nassocs; i++) {
8,336✔
3775
      tree_t a = tree_assoc(t, i);
5,922✔
3776
      int f = -1;
5,922✔
3777

3778
      switch (tree_subkind(a)) {
5,922✔
3779
      case A_NAMED:
1,037✔
3780
         {
3781
            tree_t name = tree_name(a);
1,037✔
3782
            if (tree_kind(name) != T_REF)
1,037✔
3783
               sem_error(name, "association choice must be a field name");
3✔
3784
            else if (!tree_has_ref(name))
1,034✔
3785
               return false;   // Was parse error
3786

3787
            tree_t fdecl = tree_ref(name);
1,032✔
3788
            if (tree_kind(fdecl) != T_FIELD_DECL)
1,032✔
3789
               return false;   // Was parse error
3790

3791
            f = tree_pos(fdecl);
1,030✔
3792
         }
3793
         break;
1,030✔
3794

3795
      case A_POS:
4,774✔
3796
         {
3797
            if (pos >= nfields)
4,774✔
3798
               sem_error(a, "%d positional associations given but record type"
1✔
3799
                         " %s only has %d fields", pos + 1,
3800
                         type_pp(composite_type), nfields);
3801

3802
            f = pos++;
4,773✔
3803
         }
3804
         break;
4,773✔
3805

3806
      case A_OTHERS:
3807
         f = -1;
3808
         break;
3809

3810
      case A_RANGE:
2✔
3811
         {
3812
            diag_t *d = diag_new(DIAG_ERROR, tree_loc(tree_range(a, 0)));
2✔
3813
            diag_printf(d, "an element association with a choice that is "
2✔
3814
                        "a discrete range is only allowed in an array "
3815
                        "aggregate");
3816
            diag_lrm(d, STD_08, "9.3.3");
2✔
3817
            diag_emit(d);
2✔
3818
            return false;
2✔
3819
         }
3820

UNCOV
3821
      case A_SLICE:
×
3822
      case A_CONCAT:
3823
         should_not_reach_here();
3824
      }
3825

3826
      int nmatched = 0;
5,912✔
3827
      for (int j = 0; j < nfields; j++) {
27,541✔
3828
         if ((f != -1) && (f != j))
21,633✔
3829
            continue;
15,476✔
3830

3831
         tree_t field = type_field(base_type, j);
6,157✔
3832
         type_t field_type = tree_type(field);
6,157✔
3833

3834
         if (mask_test(&have, j)) {
6,157✔
3835
            if (f == -1)
42✔
3836
               continue;
40✔
3837

3838
            tree_t ak = NULL;
3839
            for (int k = 0; k < i; k++) {
2✔
3840
               ak = tree_assoc(t, k);
2✔
3841
               if (tree_subkind(ak) == A_POS && tree_pos(ak) == j)
2✔
3842
                  break;
3843
               else if (tree_pos(tree_ref(tree_name(ak))) == j)
1✔
3844
                  break;
3845
            }
3846
            assert(ak != NULL);
2✔
3847

3848
            diag_t *d = diag_new(DIAG_ERROR, tree_loc(a));
2✔
3849
            diag_printf(d, "field %s was already given a value by earlier "
2✔
3850
                        "%s choice", istr(tree_ident(field)),
3851
                        assoc_kind_str(tree_subkind(ak)));
2✔
3852
            diag_hint(d, tree_loc(ak), "first choice associated with field %s",
2✔
3853
                      istr(tree_ident(field)));
3854
            diag_hint(d, tree_loc(a), "duplicate choice here");
2✔
3855
            diag_emit(d);
2✔
3856
            return false;
2✔
3857
         }
3858

3859
         tree_t value = tree_value(a);
6,115✔
3860

3861
         if (!sem_check(value, tab))
6,115✔
3862
            return false;
3863

3864
         if (!sem_check_type(value, field_type, tab))
6,115✔
3865
            sem_error(value, "type of value %s does not match type %s"
2✔
3866
                      " of field %s",
3867
                      type_pp2(tree_type(value), field_type),
3868
                      type_pp2(field_type, tree_type(value)),
3869
                      istr(tree_ident(field)));
3870

3871
         mask_set(&have, j);
6,113✔
3872
         nmatched++;
6,113✔
3873
      }
3874

3875
      if (f == -1 && nmatched == 0)
5,908✔
3876
         sem_error(a, "others association must represent at least one element");
5,908✔
3877
   }
3878

3879
   for (int i = 0; i < nfields; i++) {
8,517✔
3880
      if (!mask_test(&have, i)) {
6,105✔
3881
         tree_t field = type_field(base_type, i);
2✔
3882
         sem_error(t, "field %s does not have a value",
6,105✔
3883
                   istr(tree_ident(field)));
3884
      }
3885
   }
3886

3887
   return true;
3888
}
3889

3890
static bool sem_check_aggregate(tree_t t, nametab_t *tab)
8,924✔
3891
{
3892
   // Rules for aggregates are in LRM 93 section 7.3.2
3893

3894
   type_t composite_type = tree_type(t);
8,924✔
3895

3896
   if (type_is_none(composite_type))
8,924✔
3897
      return false;
3898
   assert(type_is_composite(composite_type));
8,918✔
3899

3900
   // All positional associations must appear before named associations
3901
   // and those must appear before any others association
3902

3903
   enum { POS, NAMED, OTHERS } state = POS;
8,918✔
3904

3905
   const int nassocs = tree_assocs(t);
8,918✔
3906
   for (int i = 0; i < nassocs; i++) {
46,576✔
3907
      tree_t a = tree_assoc(t, i);
37,661✔
3908

3909
      switch (tree_subkind(a)) {
37,661✔
3910
      case A_POS:
32,085✔
3911
      case A_CONCAT:
3912
         if (state > POS)
32,085✔
3913
            sem_error(a, "positional associations must appear "
1✔
3914
                      "first in aggregate");
3915
         break;
3916

3917
      case A_NAMED:
3,011✔
3918
      case A_RANGE:
3919
      case A_SLICE:
3920
         if (state > NAMED)
3,011✔
3921
            sem_error(a, "named association must not follow "
1✔
3922
                      "others association in aggregate");
3923
         state = NAMED;
3924
         break;
3925

3926
      case A_OTHERS:
2,565✔
3927
         if (state == OTHERS)
2,565✔
3928
            sem_error(a, "only a single others association "
1✔
3929
                      "allowed in aggregate");
3930
         state = OTHERS;
3931
         break;
3932
      }
3933
   }
3934

3935
   if (type_is_array(composite_type))
8,915✔
3936
      return sem_check_array_aggregate(t, tab);
6,486✔
3937
   else
3938
      return sem_check_record_aggregate(t, tab);
2,429✔
3939
}
3940

3941
static bool sem_check_ref(tree_t t, nametab_t *tab)
175,119✔
3942
{
3943
   if (!tree_has_ref(t))
175,119✔
3944
      return false;
3945

3946
   type_t type = get_type_or_null(t);
175,072✔
3947
   if (type != NULL && type_is_none(type))
175,072✔
3948
      return false;
3949

3950
   tree_t decl = tree_ref(t);
175,064✔
3951
   const tree_kind_t kind = tree_kind(decl);
175,064✔
3952

3953
   switch (kind) {
175,064✔
3954
   case T_PORT_DECL:
3955
   case T_VAR_DECL:
3956
   case T_SIGNAL_DECL:
3957
   case T_FILE_DECL:
3958
   case T_ENUM_LIT:
3959
   case T_UNIT_DECL:
3960
   case T_FUNC_DECL:
3961
   case T_FUNC_BODY:
3962
   case T_FUNC_INST:
3963
   case T_PROC_DECL:
3964
   case T_PROC_BODY:
3965
   case T_PROC_INST:
3966
   case T_IMPLICIT_SIGNAL:
3967
   case T_PARAM_DECL:
3968
      break;
3969

3970
   case T_CONST_DECL:
19,981✔
3971
      if (!tree_has_value(decl) && is_same_region(tab, decl)) {
19,981✔
3972
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
1✔
3973
         diag_printf(d, "cannot reference deferred constant %s before the "
1✔
3974
                     "elaboration of the corresponding full declaration",
3975
                     istr(tree_ident(decl)));
3976
         diag_hint(d, tree_loc(decl), "%s declared here",
1✔
3977
                   istr(tree_ident(decl)));
3978
         diag_lrm(d, STD_08, "4.8");
1✔
3979
         diag_emit(d);
1✔
3980
         return false;
1✔
3981
      }
3982
      break;
3983

3984
   case T_ALIAS:
1,727✔
3985
      {
3986
         switch (class_of(decl)) {
1,727✔
3987
         case C_VARIABLE:
3988
         case C_SIGNAL:
3989
         case C_CONSTANT:
3990
         case C_LITERAL:
3991
            break;
3992

3993
         case C_DEFAULT:
3994
            return false;   // Must have been an earlier parse error
3995

3996
         default:
1✔
3997
            sem_error(t, "invalid use of alias %s", istr(tree_ident(decl)));
1✔
3998
         }
3999
      }
4000
      break;
4001

4002
   case T_GENERIC_DECL:
6,000✔
4003
      if (tree_class(decl) == C_CONSTANT)
6,000✔
4004
         break;
4005
      // Fall-through
4006

4007
   default:
4008
      {
4009
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
4✔
4010
         diag_printf(d, "invalid use of %s %s", class_str(class_of(decl)),
4✔
4011
                     istr(tree_ident(t)));
4012
         diag_hint(d, tree_loc(decl), "%s declared here",
4✔
4013
                   istr(tree_ident(decl)));
4014
         diag_emit(d);
4✔
4015
         return false;
4✔
4016
      }
4017
   }
4018

4019
   tree_t sub = find_enclosing(tab, S_SUBPROGRAM);
175,058✔
4020
   if (sub != NULL) {
175,058✔
4021
      if (kind == T_FILE_DECL)
93,562✔
4022
         tree_set_flag(sub, TREE_F_IMPURE_FILE);
289✔
4023
      else if (kind == T_VAR_DECL && (tree_flags(decl) & TREE_F_SHARED))
93,273✔
4024
         tree_set_flag(sub, TREE_F_IMPURE_SHARED);
64✔
4025

4026
      const bool is_pure_func =
187,124✔
4027
         tree_kind(sub) == T_FUNC_BODY && !(tree_flags(sub) & TREE_F_IMPURE);
93,562✔
4028

4029
      if (is_pure_func) {
93,562✔
4030
         const class_t class = class_of(decl);
56,971✔
4031
         if (class == C_VARIABLE || class == C_SIGNAL || class == C_FILE) {
56,971✔
4032
            tree_t container = get_container(tab, decl);
15,991✔
4033
            if (container != NULL && container != sub) {
15,991✔
4034
               diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
2✔
4035
               diag_printf(d, "cannot reference %s %s in pure function %s",
2✔
4036
                           class_str(class), istr(tree_ident(decl)),
4037
                           istr(tree_ident(sub)));
4038
               diag_lrm(d, STD_08, "4.3");
2✔
4039
               diag_emit(d);
2✔
4040
               return false;
2✔
4041
            }
4042
         }
4043
      }
4044
   }
4045

4046
   return true;
4047
}
4048

4049
static bool sem_check_name_prefix(tree_t t, nametab_t *tab, const char *what)
28,017✔
4050
{
4051
   // The prefix of a name may only be function call or another name
4052
   switch (tree_kind(t)) {
28,017✔
4053
   case T_FCALL:
4054
   case T_PROT_FCALL:
4055
   case T_REF:
4056
   case T_ATTR_REF:
4057
   case T_ALL:
4058
   case T_ARRAY_REF:
4059
   case T_ARRAY_SLICE:
4060
   case T_RECORD_REF:
4061
   case T_EXTERNAL_NAME:
4062
      break;
4063

4064
   default:
2✔
4065
      {
4066
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
2✔
4067
         diag_printf(d, "the prefix of %s must be a name or a "
2✔
4068
                     "function call", what);
4069
         diag_lrm(d, STD_08, "8.1");
2✔
4070
         diag_emit(d);
2✔
4071
         return false;
2✔
4072
      }
4073
   }
4074

4075
   return true;
4076
}
4077

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

4084
   if (!tree_has_ref(t))
7,781✔
4085
      return false;
4086

4087
   if (!sem_check_name_prefix(value, tab, "a selected name"))
7,776✔
UNCOV
4088
      return false;
×
4089

4090
   return true;
4091
}
4092

4093
static bool sem_check_array_ref(tree_t t, nametab_t *tab)
14,556✔
4094
{
4095
   tree_t value = tree_value(t);
14,556✔
4096
   if (!sem_check(value, tab))
14,556✔
4097
      return false;
4098

4099
   if (!sem_check_name_prefix(value, tab, "an indexed name"))
14,554✔
4100
      return false;
4101

4102
   type_t type = tree_type(value);
14,553✔
4103

4104
   if (!type_is_array(type))
14,553✔
4105
      return false;  // Checked earlier
4106

4107
   const int nindex  = dimension_of(type);
14,549✔
4108
   const int nparams = tree_params(t);
14,549✔
4109

4110
   if (nparams != nindex)
14,549✔
4111
      sem_error(t, "prefix of indexed name has %d dimensions but %d "
3✔
4112
                "indices given", nindex, nparams);
4113

4114
   bool ok = true;
4115
   for (int i = 0; i < nparams; i++) {
30,728✔
4116
      tree_t p = tree_param(t, i);
16,184✔
4117
      assert(tree_subkind(p) == P_POS);
16,184✔
4118

4119
      type_t expect = index_type_of(type, i);
16,184✔
4120
      tree_t value = tree_value(p);
16,184✔
4121

4122
      ok = sem_check(value, tab) && ok;
16,184✔
4123

4124
      if (ok && !sem_check_type(value, expect, tab))
16,184✔
4125
         sem_error(value, "type of index %s does not match type of "
16,184✔
4126
                   "array dimension %s",
4127
                   type_pp(tree_type(value)),
4128
                   type_pp(expect));
4129
   }
4130

4131
   return ok;
4132
}
4133

4134
static bool sem_check_array_slice(tree_t t, nametab_t *tab)
2,194✔
4135
{
4136
   tree_t value = tree_value(t);
2,194✔
4137
   if (!sem_check(value, tab))
2,194✔
4138
      return false;
4139

4140
   if (!sem_check_name_prefix(value, tab, "a slice name"))
2,193✔
4141
      return false;
4142

4143
   type_t array_type = tree_type(value);
2,192✔
4144

4145
   if (type_is_none(array_type))
2,192✔
4146
      return false;
4147
   else if (!sem_check_incomplete(t, array_type))
2,192✔
4148
      return false;
4149
   else if (!type_is_array(array_type))
2,191✔
4150
      sem_error(t, "type of slice prefix %s is not an array",
2✔
4151
                type_pp(array_type));
4152

4153
   tree_t r = tree_range(t, 0);
2,189✔
4154
   if (!sem_check_discrete_range(r, index_type_of(array_type, 0), tab))
2,189✔
4155
      return false;
4156

4157
   const bool unconstrained = type_is_unconstrained(array_type);
2,179✔
4158
   const range_kind_t prefix_dir =
4,358✔
4159
      unconstrained ? RANGE_EXPR : direction_of(array_type, 0);
2,179✔
4160

4161
   const range_kind_t rkind = tree_subkind(r);
2,179✔
4162
   const bool wrong_dir =
4,358✔
4163
      !unconstrained
2,179✔
4164
      && rkind != prefix_dir
2,179✔
4165
      && (rkind == RANGE_TO || rkind == RANGE_DOWNTO)
130✔
4166
      && (prefix_dir == RANGE_TO || prefix_dir == RANGE_DOWNTO);
2,179✔
4167

4168
   if (wrong_dir) {
2,179✔
4169
      const char *text[] = { "TO", "DOWNTO", "?", "??", "???" };
2✔
4170
      sem_error(t, "range direction of slice %s does not match prefix %s",
2✔
4171
                text[rkind], text[prefix_dir]);
4172
   }
4173

4174
   return true;
4175
}
4176

4177
static bool sem_check_signal_attr(tree_t t)
1,017✔
4178
{
4179
   tree_t name = tree_name(t);
1,045✔
4180

4181
   if (tree_kind(name) == T_ATTR_REF)
1,045✔
4182
      return sem_check_signal_attr(name);
4183

4184
   tree_t ref = name_to_ref(name);
1,017✔
4185
   if (ref != NULL && class_of(ref) == C_SIGNAL)
1,017✔
4186
      return true;
4187

4188
   sem_error(t, "prefix of attribute %s must denote a signal",
2✔
4189
             istr(tree_ident(t)));
4190
}
4191

4192
static bool sem_check_driving(tree_t t)
69✔
4193
{
4194
   // See LRM 08 section 16.2.4 for special rules about 'DRIVING and
4195
   // 'DRIVING_VALUE
4196

4197
   if (!sem_check_signal_attr(t))
69✔
4198
      return false;
4199

4200
   tree_t ref = name_to_ref(tree_name(t));
68✔
4201
   if (ref == NULL || !tree_has_ref(ref))
68✔
UNCOV
4202
      return false;
×
4203

4204
   tree_t decl = tree_ref(ref);
68✔
4205
   if (tree_kind(decl) == T_PORT_DECL) {
68✔
4206
      const port_mode_t mode = tree_subkind(decl);
13✔
4207
      if (mode != PORT_OUT && mode != PORT_INOUT && mode != PORT_BUFFER)
13✔
4208
         sem_error(t, "prefix of attribute %s must denote a signal or a port "
1✔
4209
                   "with mode IN, INOUT, or BUFFER", istr(tree_ident(t)));
4210
   }
4211

4212
   // TODO: check within a process
4213

4214
   return true;
4215
}
4216

4217
static bool sem_check_attr_param(tree_t t, type_t expect, int min, int max,
1,458✔
4218
                                 nametab_t *tab)
4219
{
4220
   const int nparams = tree_params(t);
1,458✔
4221
   if (nparams == 0 && min > 0)
1,458✔
UNCOV
4222
      sem_error(t, "attribute %s requires a parameter", istr(tree_ident(t)));
×
4223
   else if (nparams > max)
1,458✔
UNCOV
4224
      sem_error(t, "too many parameters for attribute %s", istr(tree_ident(t)));
×
4225
   else if (nparams == 1) {
1,458✔
4226
      tree_t dim = tree_value(tree_param(t, 0));
1,363✔
4227
      if (!sem_check(dim, tab))
1,363✔
4228
         return false;
4229

4230
      tree_t value = tree_value(tree_param(t, 0));
1,359✔
4231
      if (!sem_check_type(value, expect, tab))
1,359✔
UNCOV
4232
         sem_error(t, "expected type %s for attribute %s parameter but "
×
4233
                   "have %s", type_pp(expect), istr(tree_ident(t)),
4234
                   type_pp(tree_type(value)));
4235
   }
4236

4237
   return true;
4238
}
4239

4240
static bool sem_check_dimension_attr(tree_t t, nametab_t *tab)
22,128✔
4241
{
4242
   const int nparams = tree_params(t);
22,128✔
4243
   if (nparams == 0)
22,128✔
4244
      return true;
4245

4246
   assert(nparams == 1);   // Enforced by parser
1,648✔
4247

4248
   tree_t dim = tree_value(tree_param(t, 0));
1,648✔
4249
   if (!sem_check(dim, tab))
1,648✔
4250
      return false;
4251

4252
   // The parameter must be a locally static expression of type
4253
   // universal_integer
4254

4255
   type_t uint = std_type(NULL, STD_UNIVERSAL_INTEGER);
1,647✔
4256
   type_t dimtype = tree_type(dim);
1,647✔
4257
   if (!type_eq(dimtype, uint)) {
1,647✔
4258
      diag_t *d;
4✔
4259
      if (type_is_integer(dimtype))
4✔
4260
         d = pedantic_diag(tree_loc(dim));
4✔
4261
      else
UNCOV
4262
         d = diag_new(DIAG_ERROR, tree_loc(dim));
×
4263

4264
      if (d != NULL) {
4✔
4265
         diag_printf(d, "dimension parameter of attribute %s must be a locally "
4✔
4266
                     "static expression of type universal_integer",
4267
                     istr(tree_ident(t)));
4268
         diag_hint(d, tree_loc(dim), "expression has type %s",
4✔
4269
                   type_pp(tree_type(dim)));
4270
         diag_emit(d);
4✔
4271
         return false;
4✔
4272
      }
4273
   }
4274

4275
   if (!sem_locally_static(dim))
1,643✔
UNCOV
4276
      sem_error(dim, "dimension parameter of attribute %s must be a locally "
×
4277
                "static expression", istr(tree_ident(t)));
4278

4279
   if (!type_is_array(tree_type(tree_name(t))))
1,643✔
UNCOV
4280
      sem_error(t, "prefix of attribute %s with dimension is not an array",
×
4281
                istr(tree_ident(t)));
4282

4283
   return true;
4284
}
4285

4286
static bool sem_is_named_entity(tree_t t)
787✔
4287
{
4288
   const tree_kind_t kind = tree_kind(t);
787✔
4289
   if (kind != T_REF)
787✔
4290
      return false;
4291

4292
   tree_t decl = tree_ref(t);
787✔
4293

4294
   switch (tree_kind(decl)) {
787✔
4295
   case T_SIGNAL_DECL:  case T_VAR_DECL:       case T_PORT_DECL:
4296
   case T_ALIAS:        case T_ENTITY:         case T_ARCH:
4297
   case T_PACKAGE:      case T_PACK_BODY:      case T_BLOCK:
4298
   case T_FILE_DECL:    case T_CONST_DECL:     case T_FUNC_DECL:
4299
   case T_FUNC_BODY:    case T_PROC_DECL:      case T_PROC_BODY:
4300
   case T_PROCESS:      case T_GENERIC_DECL:   case T_PARAM_DECL:
4301
   case T_INSTANCE:     case T_PROT_DECL:      case T_PROT_BODY:
4302
   case T_TYPE_DECL:    case T_SUBTYPE_DECL:   case T_FOR_GENERATE:
4303
   case T_IF_GENERATE:  case T_CASE_GENERATE:
4304
      return true;
UNCOV
4305
   case T_IMPLICIT_SIGNAL:
×
UNCOV
4306
      return tree_subkind(decl) == IMPLICIT_GUARD;   // See LRM 93 section 4.3
×
4307
   default:
1✔
4308
      return false;
1✔
4309
   }
4310
}
4311

4312
static bool sem_check_attr_prefix(tree_t t, bool allow_range, nametab_t *tab,
26,917✔
4313
                                  type_t *named_type)
4314
{
4315
   switch (tree_kind(t)) {
26,917✔
4316
   case T_REF:
25,112✔
4317
      {
4318
         if (!tree_has_ref(t))
25,112✔
4319
            return false;
4320

4321
         tree_t decl = tree_ref(t), type_decl;
25,110✔
4322
         if ((type_decl = aliased_type_decl(decl)) != NULL)
25,110✔
4323
            *named_type = tree_type(type_decl);
4,835✔
4324

4325
         return true;
4326
      }
4327

4328
   case T_ATTR_REF:
93✔
4329
      {
4330
         if (is_type_attribute(tree_subkind(t)))
93✔
4331
            *named_type = tree_type(t);
41✔
4332
         else {
4333
            if (!sem_check_attr_ref(t, allow_range, tab))
52✔
UNCOV
4334
               return false;
×
4335
         }
4336

4337
         return true;
4338
      }
4339

4340
   case T_RECORD_REF:
1,145✔
4341
      {
4342
         if (!tree_has_ref(t))
1,145✔
4343
            return false;
4344

4345
         tree_t value = tree_value(t);
1,145✔
4346
         if (!sem_check_attr_prefix(value, false, tab, named_type))
1,145✔
4347
            return false;
4348

4349
         if (!sem_check_name_prefix(t, tab, "a selected name"))
1,145✔
4350
            return false;
4351

4352
         if (*named_type != NULL)
1,145✔
4353
            *named_type = tree_type(tree_ref(t));
3✔
4354

4355
         return true;
4356
      }
4357

4358
   default:
567✔
4359
      return sem_check(t, tab);
567✔
4360
   }
4361
}
4362

4363
static bool sem_check_attr_ref(tree_t t, bool allow_range, nametab_t *tab)
25,772✔
4364
{
4365
   // Attribute names are in LRM 93 section 6.6
4366

4367
   tree_t name = tree_name(t);
25,772✔
4368
   type_t named_type = NULL;
25,772✔
4369

4370
   ident_t attr = tree_ident(t);
25,772✔
4371
   const attr_kind_t predef = tree_subkind(t);
25,772✔
4372

4373
   const bool prefix_can_be_range =
25,772✔
4374
      predef == ATTR_LOW || predef == ATTR_HIGH || predef == ATTR_LEFT
4375
      || predef == ATTR_RIGHT || predef == ATTR_ASCENDING;
25,772✔
4376

4377
   if (!sem_check_attr_prefix(name, prefix_can_be_range, tab, &named_type))
25,772✔
4378
      return false;
4379

4380
   if (predef == ATTR_INSTANCE_NAME)
25,768✔
4381
      tree_set_global_flags(t, TREE_GF_INSTANCE_NAME);
499✔
4382
   else if (predef == ATTR_PATH_NAME)
25,269✔
4383
      tree_set_global_flags(t, TREE_GF_PATH_NAME);
220✔
4384

4385
   switch (predef) {
25,768✔
4386
   case ATTR_RANGE:
4,751✔
4387
   case ATTR_REVERSE_RANGE:
4388
      {
4389
         type_t type = tree_type(name);
4,751✔
4390
         if (type_is_none(type))
4,751✔
4391
            return false;
4392
         else if (!allow_range)
4,751✔
UNCOV
4393
            sem_error(t, "range expression not allowed here");
×
4394

4395
         if (!sem_check_dimension_attr(t, tab))
4,751✔
4396
            return false;
4397

4398
         if (named_type != NULL) {
4,751✔
4399
            // Range attribute of type
4400
            if (named_type != NULL && type_is_unconstrained(type))
1,202✔
4401
               sem_error(t, "cannot use attribute %s with unconstrained array "
1✔
4402
                         "type %s", istr(attr), type_pp(type));
4403
         }
4404
         else if (!type_is_array(type)) {
3,549✔
4405
            diag_t *d = diag_new(DIAG_ERROR, tree_loc(name));
3✔
4406
            diag_printf(d, "object prefix of attribute %s must be an array",
3✔
4407
                        istr(attr));
4408
            diag_hint(d, tree_loc(name), "prefix has type %s", type_pp(type));
3✔
4409
            diag_emit(d);
3✔
4410
            return false;
3✔
4411
         }
4412

4413
         return true;
4414
      }
4415

4416
   case ATTR_LENGTH:
7,701✔
4417
      {
4418
         type_t type = get_type_or_null(name);
7,701✔
4419
         if (type == NULL)
7,701✔
4420
            sem_error(name, "prefix does not have LENGTH attribute");
1✔
4421
         else if (type_is_none(type))
7,700✔
4422
            return false;
4423
         else if (!sem_check_incomplete(t, type))
7,700✔
4424
            return false;
4425
         else if (!type_is_array(type)
7,699✔
4426
                  && !(standard() >= STD_19 && type_is_discrete(type)))
11✔
4427
            sem_error(name, "prefix of attribute LENGTH must be an array%s "
1✔
4428
                      "but have type %s",
4429
                      standard() >= STD_19 ? " or a discrete type" : "",
4430
                      type_pp(type));
4431

4432
         if (!sem_check_dimension_attr(t, tab))
7,698✔
4433
            return false;
3✔
4434

4435
         return true;
4436
      }
4437

4438
   case ATTR_LEFT:
9,680✔
4439
   case ATTR_RIGHT:
4440
   case ATTR_LOW:
4441
   case ATTR_HIGH:
4442
   case ATTR_ASCENDING:
4443
      {
4444
         type_t type = tree_type(name);
9,680✔
4445

4446
         if (type_is_none(type))
9,680✔
4447
            return false;
4448
         else if (!sem_check_incomplete(t, type))
9,680✔
4449
            return false;
4450
         else if (!sem_check_dimension_attr(t, tab))
9,679✔
4451
            return false;
4452

4453
         if (!type_is_array(type) && !type_is_scalar(type))
9,677✔
4454
            sem_error(t, "prefix does not have attribute %s", istr(attr));
1✔
4455

4456
         return true;
4457
      }
4458

4459
   case ATTR_LAST_EVENT:
97✔
4460
   case ATTR_LAST_ACTIVE:
4461
      if (!sem_check_readable(name))
97✔
4462
         return false;
4463
      else if (!sem_check_attr_param(t, NULL, 0, 0, tab))
95✔
4464
         return false;
4465
      else if (!sem_check_signal_attr(t))
95✔
4466
         return false;
1✔
4467

4468
      return true;
4469

4470
   case ATTR_EVENT:
588✔
4471
   case ATTR_ACTIVE:
4472
   case ATTR_LAST_VALUE:
4473
      if (!sem_check_readable(name))
588✔
4474
         return false;
4475
      else if (!sem_check_signal_attr(t))
586✔
UNCOV
4476
         return false;
×
4477

4478
      return true;
4479

4480
   case ATTR_PATH_NAME:
787✔
4481
   case ATTR_INSTANCE_NAME:
4482
   case ATTR_SIMPLE_NAME:
4483
      if (!sem_is_named_entity(name))
787✔
4484
         sem_error(t, "prefix of %s attribute must be a named entity",
1✔
4485
                   istr(attr));
4486

4487
      tree_set_flag(name, TREE_F_ATTR_PREFIX);
786✔
4488
      return true;
786✔
4489

4490
   case ATTR_QUIET:
237✔
4491
   case ATTR_STABLE:
4492
   case ATTR_DELAYED:
4493
      {
4494
         if (!sem_check_readable(name))
237✔
4495
            return false;
4496
         else if (!sem_check_signal_attr(t))
235✔
4497
            return false;
4498

4499
         if (tree_params(t) > 0) {
235✔
4500
            tree_t value = tree_value(tree_param(t, 0));
85✔
4501

4502
            if (!sem_check(value, tab))
85✔
4503
               return false;
4504

4505
            type_t std_time = std_type(NULL, STD_TIME);
85✔
4506
            if (!sem_check_type(value, std_time, tab))
85✔
4507
               sem_error(value, "parameter of attribute %s must have type %s",
1✔
4508
                         istr(attr), type_pp(std_time));
4509
            else if (!sem_globally_static(value))
84✔
4510
               sem_error(value, "parameter of attribute %s must be a static "
1✔
4511
                         "expression", istr(attr));
4512
         }
4513

4514
         return true;
4515
      }
4516

4517
   case ATTR_TRANSACTION:
33✔
4518
      if (!sem_check_readable(name))
33✔
4519
         return false;
4520
      else if (!sem_check_signal_attr(t))
32✔
UNCOV
4521
         return false;
×
4522

4523
      return true;
4524

4525
   case ATTR_DRIVING_VALUE:
69✔
4526
   case ATTR_DRIVING:
4527
      return sem_check_driving(t);
69✔
4528

4529
   case ATTR_IMAGE:
1,183✔
4530
   case ATTR_VALUE:
4531
      {
4532
         const bool std_2019 = standard() >= STD_19;
1,183✔
4533

4534
         if (named_type == NULL && std_2019 && tree_params(t) == 0) {
1,183✔
4535
            // LCS2016-18 allows attribute with object prefix
4536
            named_type = get_type_or_null(name);
5✔
4537
            add_param(t, name, P_POS, NULL);
5✔
4538
         }
4539

4540
         if (named_type == NULL)
1,183✔
4541
            sem_error(t, "prefix of attribute %s must be a type", istr(attr));
1✔
4542
         if (!std_2019 && !type_is_scalar(named_type))
1,182✔
4543
            sem_error(t, "cannot use attribute %s with non-scalar type %s",
2✔
4544
                      istr(attr), type_pp(named_type));
4545
         else if (std_2019 && !type_is_representable(named_type))
1,180✔
4546
            sem_error(t, "cannot use attribute %s with non-representable "
1✔
4547
                      "type %s", istr(attr), type_pp(named_type));
4548

4549
         type_t std_string = std_type(NULL, STD_STRING);
1,179✔
4550
         type_t arg_type = predef == ATTR_IMAGE ? named_type : std_string;
1,179✔
4551
         if (!sem_check_attr_param(t, arg_type, 1, 1, tab))
1,179✔
4552
            return false;
4✔
4553

4554
         return true;
4555
      }
4556

4557
   case ATTR_LEFTOF:
294✔
4558
   case ATTR_RIGHTOF:
4559
   case ATTR_PRED:
4560
   case ATTR_SUCC:
4561
   case ATTR_POS:
4562
   case ATTR_VAL:
4563
      {
4564
         if (named_type == NULL && standard() >= STD_19)
294✔
4565
            named_type = get_type_or_null(name);   // LCS2016-08 relaxation
3✔
4566

4567
         if (named_type == NULL)
294✔
UNCOV
4568
            sem_error(t, "prefix of attribute %s must be a type", istr(attr));
×
4569

4570
         type_t name_type = tree_type(name);
294✔
4571

4572
         if (!type_is_discrete(name_type) && !type_is_physical(name_type))
294✔
UNCOV
4573
            sem_error(t, "prefix of attribute %s must be a discrete or "
×
4574
                      "physical type", istr(attr));
4575

4576
         if (predef == ATTR_VAL) {
294✔
4577
            // Parameter may be any integer type
4578
            if (tree_params(t) != 1)
110✔
4579
               sem_error(t, "attribute VAL requires a parameter");
1✔
4580

4581
            type_t ptype = tree_type(tree_value(tree_param(t, 0)));
109✔
4582
            if (!type_is_integer(ptype))
109✔
4583
               sem_error(t, "parameter of attribute VAL must have an integer "
1✔
4584
                         "type but found %s", type_pp(ptype));
4585
         }
4586
         else if (!sem_check_attr_param(t, name_type, 1, 1, tab))
184✔
UNCOV
4587
            return false;
×
4588

4589
         return true;
4590
      }
4591

4592
   case ATTR_BASE:
1✔
4593
      sem_error(t, "BASE attribute is allowed only as the prefix of the name "
1✔
4594
                "of another attribute");
4595

4596
   case ATTR_ELEMENT:
1✔
4597
   case ATTR_SUBTYPE:
4598
   case ATTR_INDEX:
4599
      sem_error(t, "%s attribute is only allowed in a type mark", istr(attr));
1✔
4600

4601
   case ATTR_CONVERSE:
27✔
4602
      if (type_kind(tree_type(name)) != T_VIEW)
27✔
4603
         sem_error(t, "prefix of 'CONVERSE attribute must be a named mode "
1✔
4604
                   "view or alias thereof");
4605

4606
      return true;
4607

4608
   case ATTR_REFLECT:
99✔
4609
      if (get_type_or_null(name) == NULL)
99✔
4610
         sem_error(t, "prefix of attribute REFLECT is not a type mark or "
1✔
4611
                   "an object with a type");
4612
      else if (named_type != NULL && type_is_unconstrained(named_type))
98✔
4613
         sem_error(t, "prefix of 'REFLECT attribute must be a fully "
1✔
4614
                   "constrained subtype");
4615

4616
      return true;
4617

4618
   case ATTR_USER:
220✔
4619
      if (!tree_has_value(t))
220✔
4620
         return false;
4621

4622
      if (!sem_static_name(name, sem_globally_static)) {
218✔
UNCOV
4623
         if (tree_kind(name) == T_REF)
×
UNCOV
4624
            sem_error(name, "%s is not a static name", istr(tree_ident(name)));
×
4625
         else
UNCOV
4626
            sem_error(name, "invalid attribute reference");
×
4627
      }
4628

4629
      return true;
4630

UNCOV
4631
   default:
×
4632
      fatal_trace("unhandled attribute kind %d", predef);
4633
   }
4634
}
4635

4636
static bool sem_check_qualified(tree_t t, nametab_t *tab)
4,261✔
4637
{
4638
   if (tree_has_value(t)) {
4,261✔
4639
      tree_t value = tree_value(t);
3,972✔
4640

4641
      if (!sem_check(value, tab))
3,972✔
4642
         return false;
4643

4644
      // LRM 08 section 9.3.5 qualified expressions: the operand shall have
4645
      // the same type as the base type of the type mark
4646
      type_t base = type_base_recur(tree_type(t));
3,971✔
4647
      if (!sem_check_type(value, base, tab)) {
3,971✔
4648
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(value));
1✔
4649
         diag_printf(d, "operand of qualified expression must have type %s",
1✔
4650
                     type_pp(base));
4651
         diag_hint(d, tree_loc(value), "operand has type %s",
1✔
4652
                   type_pp(tree_type(value)));
4653
         diag_lrm(d, STD_08, "9.3.5");
1✔
4654
         diag_emit(d);
1✔
4655
         return false;
1✔
4656
      }
4657
   }
4658

4659
   return true;
4660
}
4661

4662
static bool sem_static_signal_name(tree_t t)
1,466✔
4663
{
4664
   if (!sem_static_name(t, sem_globally_static))
1,466✔
4665
      return false;
4666

4667
   tree_t ref = name_to_ref(t);
1,464✔
4668
   if (ref != NULL && !tree_has_ref(ref))
1,464✔
4669
      return true;  // Suppress cascading error
4670

4671
   return ref != NULL && class_of(tree_ref(ref)) == C_SIGNAL;
1,467✔
4672
}
4673

4674
static bool sem_check_port_actual(formal_map_t *formals, int nformals,
3,655✔
4675
                                  tree_t param, tree_t unit, nametab_t *tab)
4676
{
4677
   tree_t value = tree_value(param), name = NULL, out_conv = NULL;
3,655✔
4678
   tree_t decl = NULL;
3,655✔
4679
   type_t type = NULL;
3,655✔
4680

4681
   switch (tree_subkind(param)) {
3,655✔
4682
   case P_POS:
1,384✔
4683
      {
4684
         const int pos = tree_pos(param);
1,384✔
4685
         if (pos >= nformals)
1,384✔
4686
            sem_error(value, "found at least %d positional actuals but %s "
4✔
4687
                      "has only %d port%s", pos + 1, istr(tree_ident(unit)),
4688
                      nformals, nformals == 1 ? "" : "s");
4689
         if (formals[pos].state >= MAP_PARTIAL)
1,382✔
4690
            sem_error(value, "formal port %s already has an actual",
×
4691
                      istr(tree_ident(formals[pos].decl)));
4692
         formals[pos].state = MAP_FULL;
1,382✔
4693
         decl = formals[pos].decl;
1,382✔
4694
         type = tree_type(decl);
1,382✔
4695
      }
4696
      break;
1,382✔
4697

4698
   case P_NAMED:
2,271✔
4699
      {
4700
         tree_t ref = (name = tree_name(param));
2,271✔
4701

4702
         switch (tree_kind(name)) {
2,271✔
4703
         case T_FCALL:
1✔
4704
            if (tree_params(name) != 1)
1✔
UNCOV
4705
               sem_error(name, "output conversion function must have "
×
4706
                         "exactly one parameter");
4707

4708
            // The parser would have replaced any other valid conversion
4709
            // function with T_CONV_FUNC
4710
            sem_error(name, "invalid output conversion %s",
1✔
4711
                      istr(tree_ident(name)));
4712
            break;
4713

4714
         case T_CONV_FUNC:
90✔
4715
         case T_TYPE_CONV:
4716
            out_conv = name;
90✔
4717
            name = ref = tree_value(name);
90✔
4718
            break;
90✔
4719

4720
         default:
4721
            break;
4722
         }
4723

4724
         ref = name_to_ref(ref);
2,270✔
4725
         assert(ref != NULL && tree_kind(ref) == T_REF);
2,270✔
4726

4727
         if (!tree_has_ref(ref))
2,270✔
4728
            return false;
4729

4730
         tree_t d = tree_ref(ref);
2,270✔
4731
         for (int i = 0; i < nformals; i++) {
8,686✔
4732
            if (formals[i].decl == d) {
8,684✔
4733
               if (formals[i].state == MAP_FULL)
2,268✔
4734
                  sem_error(value, "formal port %s already has an actual",
1✔
4735
                            istr(tree_ident(formals[i].decl)));
4736
               formals[i].state = ref == name ? MAP_FULL : MAP_PARTIAL;
2,267✔
4737
               decl = formals[i].decl;
2,267✔
4738
               tree_set_flag(ref, TREE_F_FORMAL_NAME);
2,267✔
4739
               break;
2,267✔
4740
            }
4741
         }
4742

4743
         if (decl == NULL)
2,269✔
4744
            sem_error(value, "%s has no port named %s",
2✔
4745
                      istr(tree_ident(unit)), istr(tree_ident(ref)));
4746

4747
         if (!sem_static_name(name, sem_locally_static))
2,267✔
4748
            sem_error(name, "formal name must be locally static");
3✔
4749

4750
         if (out_conv != NULL) {
2,264✔
4751
            port_mode_t mode = tree_subkind(decl);
90✔
4752

4753
            type = tree_type((mode == PORT_INOUT) ? name : out_conv);
158✔
4754

4755
            if (mode == PORT_IN)
90✔
4756
               sem_error(name, "output conversion not allowed for formal "
1✔
4757
                         "%s with mode IN", istr(tree_ident(decl)));
4758

4759
            if (tree_kind(value) == T_OPEN)
89✔
4760
               sem_error(name, "output conversion for formal %s must not "
1✔
4761
                         "have OPEN actual", istr(tree_ident(decl)));
4762
         }
4763
         else
4764
            type = tree_type(name);
2,174✔
4765

4766
         break;
4767
      }
4768
   }
4769

4770
   assert(type != NULL);
3,644✔
4771

4772
   if (!sem_check(value, tab))
3,644✔
4773
      return false;
4774

4775
   const tree_kind_t kind = tree_kind(value);
3,641✔
4776
   tree_t expr = kind == T_INERTIAL ? tree_value(value) : value;
3,641✔
4777

4778
   type_t value_type = tree_type(expr), atype;
3,641✔
4779

4780
   if (!sem_check_type(expr, type, tab)) {
3,641✔
4781
      diag_t *d = diag_new(DIAG_ERROR, tree_loc(value));
3✔
4782
      diag_printf(d, "type of actual %s does not match type %s of formal "
3✔
4783
                  "port %s", type_pp2(value_type, type),
4784
                  type_pp2(type, value_type), istr(tree_ident(decl)));
4785

4786
      if (type_is_generic(type)) {
3✔
4787
         hash_t *map = get_generic_map(tab);
1✔
4788
         if (map != NULL && (atype = hash_get(map, type)))
1✔
4789
            diag_hint(d, NULL, "generic type %s is mapped to %s",
1✔
4790
                      type_pp(type), type_pp(atype));
4791
      }
4792

4793
      diag_emit(d);
3✔
4794
      return false;
3✔
4795
   }
4796

4797
   const port_mode_t mode = tree_subkind(decl);
3,638✔
4798

4799
   if (kind == T_OPEN) {
3,638✔
4800
      if ((mode == PORT_IN) && !tree_has_value(decl))
55✔
4801
         sem_error(value, "unconnected port %s with mode IN must have a "
1✔
4802
                   "default value", istr(tree_ident(decl)));
4803

4804
      if ((mode != PORT_IN) && type_is_unconstrained(tree_type(decl)))
54✔
4805
         sem_error(value, "port %s of unconstrained type %s cannot "
1✔
4806
                   "be unconnected", istr(tree_ident(decl)), type_pp(type));
4807
   }
4808

4809
   // Check for type conversions and conversion functions
4810
   // These only apply if the class of the formal is not constant
4811

4812
   tree_t actual;
3,636✔
4813
   if (kind == T_TYPE_CONV || kind == T_CONV_FUNC) {
3,636✔
4814
      // Conversion functions are in LRM 93 section 4.3.2.2
4815
      actual = tree_value(value);
137✔
4816

4817
      // LRM 93 section 3.2.1.1 result of a type conversion in an
4818
      // association list cannot be an unconstrained array type
4819
      if (type_is_unconstrained(value_type) && type_is_unconstrained(type)) {
137✔
4820
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(value));
1✔
4821
         diag_printf(d, "result of conversion for unconstrained formal "
1✔
4822
                     "%s must be a constrained array type",
4823
                     istr(tree_ident(decl)));
4824
         diag_lrm(d, STD_93, "3.2.1.1");
1✔
4825
         diag_emit(d);
1✔
4826
         return false;
1✔
4827
      }
4828

4829
      if (mode == PORT_OUT)
136✔
4830
         sem_error(value, "conversion not allowed for formal %s with "
1✔
4831
                   "mode OUT", istr(tree_ident(decl)));
4832
      else if (mode == PORT_INOUT && out_conv == NULL)
135✔
4833
         sem_error(value, "INOUT port %s has output conversion but no "
1✔
4834
                   "corresponding input conversion", istr(tree_ident(decl)));
4835
   }
4836
   else
4837
      actual = value;    // No conversion
4838

4839
   tree_t ref = name_to_ref(actual);
3,633✔
4840

4841
   if (mode == PORT_IN && kind != T_INERTIAL) {
3,633✔
4842
      bool is_static = true;
2,104✔
4843
      if (ref != NULL && class_of(ref) == C_SIGNAL)
2,104✔
4844
         is_static = sem_static_name(actual, sem_globally_static);
1,927✔
4845
      else
4846
         is_static = sem_globally_static(actual);
177✔
4847

4848
      // LRM 08 section 6.5.6.3 the actual is converted to a concurrent
4849
      // signal assignment to an anonymous signal that is then
4850
      // associated with the formal
4851
      if (!is_static && standard() >= STD_08) {
2,124✔
4852
         // The rules listed for unconstrained ports in 6.5.6.3 should
4853
         // be covered by the check for a globally static subtype in
4854
         // addition to the checks above
4855
         if (type_is_unconstrained(type)
21✔
4856
             && !sem_static_subtype(value_type, sem_globally_static)) {
1✔
4857
            diag_t *d = diag_new(DIAG_ERROR, tree_loc(value));
1✔
4858
            diag_printf(d, "expression associated with unconstrained formal "
1✔
4859
                        "port %s must have a globally static subtype",
4860
                        istr(tree_ident(decl)));
4861
            diag_lrm(d, STD_08, "6.5.6.3");
1✔
4862
            diag_emit(d);
1✔
4863
            return false;
1✔
4864
         }
4865

4866
         tree_t w = tree_new(T_INERTIAL);
20✔
4867
         tree_set_loc(w, tree_loc(value));
20✔
4868
         tree_set_value(w, value);
20✔
4869
         tree_set_target(w, name ?: make_ref(decl));
20✔
4870

4871
         tree_set_value(param, w);
20✔
4872
      }
4873
      else if (!is_static)
2,083✔
4874
         sem_error(value, "actual associated with port %s of mode IN must be "
4✔
4875
                   "a globally static expression or static signal name",
4876
                   istr(tree_ident(decl)));
4877
   }
4878
   else if (mode == PORT_INOUT && tree_class(decl) == C_VARIABLE) {
1,529✔
4879
      // VHDL-2019 additions for shared variable ports
4880
      if (ref == NULL || class_of(ref) != C_VARIABLE)
5✔
4881
         sem_error(value, "actual associated with formal variable port %s "
1✔
4882
                   "must either be a shared variable or a formal variable port "
4883
                   "of another design entity", istr(tree_ident(decl)));
4884
   }
4885
   else if (mode == PORT_ARRAY_VIEW || mode == PORT_RECORD_VIEW) {
1,524✔
4886
      if (!sem_static_signal_name(actual))
77✔
4887
         sem_error(value, "actual associated with port %s with mode view "
1✔
4888
                   "indication must be a static signal name",
4889
                   istr(tree_ident(decl)));
4890

4891
      if (!sem_check_compatible_view(decl, actual))
76✔
4892
         return false;
4893
   }
4894
   else if (mode != PORT_IN && tree_kind(actual) != T_OPEN
1,447✔
4895
            && !sem_static_signal_name(actual)) {
1,389✔
4896
      sem_error(value, "actual associated with port %s of mode %s must be "
4✔
4897
                "a static signal name or OPEN",
4898
                istr(tree_ident(decl)), port_mode_str(tree_subkind(decl)));
4899
   }
4900
   else if (kind == T_INERTIAL)
1,443✔
4901
      tree_set_target(value, name ?: make_ref(decl));
15✔
4902

4903
   // Check connections between ports
4904
   if (ref != NULL && tree_has_ref(ref)) {
3,621✔
4905
      tree_t odecl = tree_ref(ref);
3,445✔
4906
      if (tree_kind(odecl) == T_PORT_DECL) {
3,445✔
4907
         const port_mode_t omode = tree_subkind(odecl);
299✔
4908

4909
         const bool error =
598✔
4910
            (omode == PORT_BUFFER && (mode == PORT_OUT || mode == PORT_INOUT))
4✔
4911
            || (omode != PORT_BUFFER && mode == PORT_BUFFER)
297✔
4912
            || (omode == PORT_IN && (mode == PORT_OUT || mode == PORT_INOUT))
295✔
4913
            || (omode == PORT_OUT && mode == PORT_IN && standard() < STD_08)
294✔
4914
            || (omode == PORT_OUT && mode == PORT_INOUT)
293✔
4915
            || (omode == PORT_LINKAGE && mode != PORT_LINKAGE);
591✔
4916

4917
         if (error) {
299✔
4918
            diag_t *d = diag_new(DIAG_ERROR, tree_loc(value));
8✔
4919
            diag_printf(d, "port %s with mode %s cannot be associated "
8✔
4920
                        "with formal port %s with mode %s",
4921
                        istr(tree_ident(odecl)), port_mode_str(omode),
4922
                        istr(tree_ident(decl)), port_mode_str(mode));
4923
            diag_hint(d, tree_loc(decl), "formal port %s declared here",
8✔
4924
                      istr(tree_ident(decl)));
4925
            diag_hint(d, tree_loc(odecl), "port %s declared here",
8✔
4926
                      istr(tree_ident(odecl)));
4927
            diag_emit(d);
8✔
4928
            return false;
8✔
4929
         }
4930
      }
4931
   }
4932

4933
   return true;
4934
}
4935

4936
static bool sem_check_port_map(tree_t t, tree_t unit, nametab_t *tab)
1,886✔
4937
{
4938
   // Check there is an actual for each formal port generic
4939
   // Rules for maps are described in LRM 93 section 5.2.1.2
4940

4941
   const int nformals = tree_ports(unit);
1,886✔
4942
   const int nactuals = tree_params(t);
1,886✔
4943

4944
   bool ok = true;
1,886✔
4945

4946
   formal_map_t *formals LOCAL = xmalloc_array(nformals, sizeof(formal_map_t));
3,772✔
4947

4948
   for (int i = 0; i < nformals; i++) {
5,456✔
4949
      formals[i].decl  = tree_port(unit, i);
3,570✔
4950
      formals[i].state = MAP_MISSING;
3,570✔
4951
   }
4952

4953
   for (int i = 0; i < nactuals; i++) {
5,552✔
4954
      tree_t p = tree_param(t, i);
3,666✔
4955
      if (tree_subkind(p) != P_NAMED)
3,666✔
4956
         continue;
1,385✔
4957

4958
      tree_t name = tree_name(p);
2,281✔
4959

4960
      ok &= sem_check(name, tab);
2,281✔
4961

4962
      const tree_kind_t name_kind = tree_kind(name);
2,281✔
4963
      if ((name_kind == T_ARRAY_REF || name_kind == T_ARRAY_SLICE)
2,281✔
4964
          && tree_kind(tree_value(p)) == T_OPEN && standard() < STD_19) {
176✔
4965
         diag_t *d = pedantic_diag(tree_loc(p));
1✔
4966
         if (d != NULL) {
1✔
4967
            diag_printf(d, "sub-elements of composite port cannot be "
1✔
4968
                        "associated with OPEN");
4969
            diag_emit(d);
1✔
4970
         }
4971
      }
4972
   }
4973

4974
   if (!ok)
1,886✔
4975
      return false;
4976

4977
   for (int i = 0; i < nactuals; i++) {
5,535✔
4978
      tree_t actual = tree_param(t, i);
3,655✔
4979
      ok &= sem_check_port_actual(formals, nformals, actual, unit, tab);
3,655✔
4980

4981
      if (!ok && tree_subkind(actual) == P_POS && i >= nformals)
3,655✔
4982
         break;   // Prevent useless repeated errors
4983
   }
4984

4985
   if (tree_kind(t) == T_BINDING)
1,882✔
4986
      return ok;
4987

4988
   for (int i = 0; i < nformals; i++) {
5,253✔
4989
      if (formals[i].state == MAP_MISSING) {
3,432✔
4990
         port_mode_t mode = tree_subkind(formals[i].decl);
315✔
4991

4992
         if (mode == PORT_IN && !tree_has_value(formals[i].decl)) {
315✔
4993
            error_at(tree_loc(t), "missing actual for port %s of "
4✔
4994
                     "mode IN without a default expression",
4995
                     istr(tree_ident(formals[i].decl)));
4✔
4996
         }
4997
         else if (mode == PORT_ARRAY_VIEW || mode == PORT_RECORD_VIEW)
311✔
4998
            error_at(tree_loc(t), "missing actual for port %s with "
2✔
4999
                     "mode view indication %s",
5000
                     istr(tree_ident(formals[i].decl)),
1✔
5001
                     type_pp(tree_type(tree_value(formals[i].decl))));
1✔
5002

5003
         type_t ftype = tree_type(formals[i].decl);
315✔
5004
         if (mode != PORT_IN && type_is_unconstrained(ftype)) {
315✔
5005
            error_at(tree_loc(t), "missing actual for port %s with "
1✔
5006
                     "unconstrained array type",
5007
                     istr(tree_ident(formals[i].decl)));
1✔
5008
         }
5009
      }
5010
   }
5011

5012
   return ok;
5013
}
5014

5015
static bool sem_check_generic_actual(formal_map_t *formals, int nformals,
1,655✔
5016
                                     tree_t param, tree_t unit, nametab_t *tab)
5017
{
5018
   tree_t name = NULL;
1,655✔
5019
   int pos = -1;
1,655✔
5020
   switch (tree_subkind(param)) {
1,655✔
5021
   case P_POS:
558✔
5022
      {
5023
         pos = tree_pos(param);
558✔
5024
         if (pos >= nformals)
558✔
5025
            sem_error(param, "found at least %d positional actuals but %s "
1✔
5026
                      "has only %d generic%s", pos + 1, istr(tree_ident(unit)),
5027
                      nformals, nformals == 1 ? "" : "s");
5028
         else if (tree_flags(formals[pos].decl) & TREE_F_PREDEFINED) {
557✔
5029
            diag_t *d = diag_new(DIAG_WARN, tree_loc(param));
2✔
5030
            diag_printf(d, "positional generic actual is associated with "
2✔
5031
                        "implicit generic subprogram %s for type %s",
5032
                        istr(tree_ident(formals[pos].decl)),
5033
                        type_pp(tree_type(tree_port(formals[pos].decl, 0))));
5034
            diag_hint(d, NULL, "use a named association if this was intended");
2✔
5035
            diag_emit(d);
2✔
5036
         }
5037
      }
5038
      break;
5039

5040
   case P_NAMED:
1,097✔
5041
      {
5042
         name = tree_name(param);
1,097✔
5043

5044
         tree_t ref = name_to_ref(name);
1,097✔
5045
         if (ref == NULL)
1,097✔
5046
            sem_error(name, "invalid name in generic map");
1✔
5047
         else if (!tree_has_ref(ref))
1,096✔
5048
            return false;
5049

5050
         tree_t d = tree_ref(ref);
1,093✔
5051
         for (pos = 0; pos < nformals; pos++) {
3,659✔
5052
            if (formals[pos].decl == d) {
2,565✔
5053
               tree_set_flag(ref, TREE_F_FORMAL_NAME);
1,092✔
5054
               break;
1,092✔
5055
            }
5056
         }
5057

5058
         if (pos == nformals)
1,093✔
5059
            sem_error(name, "%s is not a formal generic of %s",
1✔
5060
                      istr(tree_ident(ref)), istr(tree_ident(unit)));
5061
         break;
5062
      }
5063
   }
5064

5065
   assert(pos >= 0 && pos < nformals);
1,649✔
5066

5067
   tree_t decl = formals[pos].decl, value = tree_value(param);
1,649✔
5068
   type_t type = get_type_or_null(name ?: decl);
2,206✔
5069

5070
   const bool is_full = name == NULL || tree_kind(name) == T_REF;
1,649✔
5071
   const bool is_open = tree_kind(value) == T_OPEN;
1,649✔
5072

5073
   if ((is_open && formals[pos].state == MAP_PARTIAL)
1,649✔
5074
       || (!is_open && formals[pos].state == MAP_OPEN))
1,648✔
5075
      sem_error(param, "formal generic %s associated with OPEN cannot be "
2✔
5076
                "individually associated", istr(tree_ident(formals[pos].decl)));
5077

5078
   if (formals[pos].state >= (is_full ? MAP_PARTIAL : MAP_FULL))
1,661✔
UNCOV
5079
      sem_error(param, "formal generic %s already has an actual",
×
5080
                istr(tree_ident(formals[pos].decl)));
5081

5082
   if (is_open)
1,647✔
5083
      formals[pos].state = MAP_OPEN;
9✔
5084
   else if (is_full)
1,638✔
5085
      formals[pos].state = MAP_FULL;
1,625✔
5086
   else
5087
      formals[pos].state = MAP_PARTIAL;
13✔
5088

5089
   if (is_open && !tree_has_value(decl))
1,647✔
5090
      sem_error(param, "generic %s without a default expression cannot "
1✔
5091
                "be associated with OPEN", istr(tree_ident(decl)));
5092
   else if (is_open)
1,646✔
5093
      return true;   // No further checking
5094

5095
   switch (tree_class(decl)) {
1,638✔
5096
   case C_TYPE:
296✔
5097
      // The parser already called map_generic_type
5098
      assert(tree_kind(value) == T_TYPE_REF);
296✔
5099
      assert(type_kind(type) == T_GENERIC);
296✔
5100

5101
      type_t map = tree_type(value);
296✔
5102
      if (type_is_none(map))
296✔
5103
         return false;
5104
      else if (!sem_check_incomplete(param, map))
292✔
5105
         return false;
5106

5107
      static const char *class_strings[] = {
291✔
5108
         [GTYPE_SCALAR] = "a scalar",
5109
         [GTYPE_DISCRETE] = "a discrete",
5110
         [GTYPE_INTEGER] = "an integer",
5111
         [GTYPE_FLOATING] = "a floating-point",
5112
         [GTYPE_PHYSICAL] = "a physical",
5113
         [GTYPE_ACCESS] = "an access",
5114
         [GTYPE_ARRAY] = "an array",
5115
         [GTYPE_FILE] = "a file",
5116
      };
5117

5118
      const gtype_class_t class = type_subkind(type);
291✔
5119
      if (!type_matches_class(map, class))
291✔
5120
         sem_error(param, "cannot map type %s to generic interface type %s "
8✔
5121
                   "which requires %s type", type_pp(map),
5122
                   istr(tree_ident(decl)), class_strings[class]);
5123
      else if (class == GTYPE_ACCESS || class == GTYPE_FILE) {
283✔
5124
         type_t expect = type_designated(type);
9✔
5125
         type_t actual = type_designated(map);
9✔
5126

5127
         if (type_is_generic(expect)) {
9✔
5128
            const gtype_class_t expect_class = type_subkind(expect);
5✔
5129
            if (!type_matches_class(actual, expect_class))
5✔
5130
               sem_error(param, "cannot map type %s to generic interface type "
1✔
5131
                         "%s as the designated type %s is not %s type",
5132
                         type_pp(map), istr(tree_ident(decl)),
5133
                         type_pp(actual), class_strings[expect_class]);
5134
         }
5135
         else if (!type_eq(actual, expect))
4✔
5136
            sem_error(param, "cannot map type %s to generic interface type "
2✔
5137
                      "%s as the designated type %s is not %s",
5138
                      type_pp(map), istr(tree_ident(decl)),
5139
                      type_pp(actual), type_pp(expect));
5140
      }
5141
      else if (class == GTYPE_ARRAY) {
274✔
5142
         const int nindex = type_indexes(type);
46✔
5143
         if (nindex != dimension_of(map))
46✔
5144
            sem_error(param, "cannot map type %s to generic interface "
1✔
5145
                      "type %s as it has %d dimensions but the incomplete "
5146
                      "type definition has %d", type_pp(map),
5147
                      istr(tree_ident(decl)), dimension_of(map), nindex);
5148

5149
         for (int i = 0; i < nindex; i++) {
88✔
5150
            type_t itype = type_index(type, i);
45✔
5151
            type_t imap = index_type_of(map, i);
45✔
5152

5153
            if (type_is_generic(itype)) {
45✔
5154
               const gtype_class_t expect_class = type_subkind(itype);
40✔
5155
               if (!type_matches_class(imap, expect_class))
40✔
5156
                  sem_error(param, "cannot map type %s to generic interface "
1✔
5157
                            "type %s as the index type %s of the %s dimension "
5158
                            "is not %s type", type_pp(map),
5159
                            istr(tree_ident(decl)), type_pp(imap),
5160
                            ordinal_str(i + 1), class_strings[expect_class]);
5161
            }
5162
            else if (!type_eq(imap, itype))
5✔
5163
               sem_error(param, "cannot map type %s to generic interface type "
44✔
5164
                         "%s as the index type %s of the %s dimension is not "
5165
                         "%s", type_pp(map), istr(tree_ident(decl)),
5166
                         type_pp(imap), ordinal_str(i + 1), type_pp(itype));
5167
         }
5168

5169
         type_t expect = type_elem(type);
43✔
5170
         type_t actual = type_elem(map);
43✔
5171

5172
          if (type_is_generic(expect)) {
43✔
5173
            const gtype_class_t expect_class = type_subkind(expect);
39✔
5174
            if (!type_matches_class(actual, expect_class))
39✔
5175
               sem_error(param, "cannot map type %s to generic interface type "
1✔
5176
                         "%s as the element type %s is not %s type",
5177
                         type_pp(map), istr(tree_ident(decl)),
5178
                         type_pp(actual), class_strings[expect_class]);
5179
         }
5180
         else if (!type_eq(actual, expect))
4✔
5181
            sem_error(param, "cannot map type %s to generic interface type "
1✔
5182
                      "%s as the element type %s is not %s",
5183
                      type_pp(map), istr(tree_ident(decl)),
5184
                      type_pp(actual), type_pp(expect));
5185
      }
5186

5187
      break;
5188

5189
   case C_PACKAGE:
80✔
5190
      {
5191
         tree_t pack = NULL;
80✔
5192
         if (tree_kind(value) == T_REF && tree_has_ref(value))
80✔
5193
            pack = tree_ref(value);
79✔
5194

5195
         if (pack == NULL || tree_kind(pack) != T_PACK_INST)
79✔
5196
            sem_error(value, "actual for generic %s is not an "
2✔
5197
                      "instantiated package name", istr(tree_ident(decl)));
5198
         else if (!tree_has_ref(pack))
78✔
5199
            return false;   // Was parse error
5200

5201
         tree_t map = tree_value(decl);
78✔
5202
         if (!tree_has_ref(map))
78✔
5203
            return false;   // Was earlier error
5204

5205
         assert(tree_kind(map) == T_PACKAGE_MAP);
77✔
5206

5207
         tree_t base = tree_ref(pack);
77✔
5208
         tree_t expect = tree_ref(map);
77✔
5209

5210
         if (tree_ident(base) != tree_ident(expect))
77✔
5211
            sem_error(value, "expected an instance of package %s but have "
1✔
5212
                      "instance of %s for generic %s", istr(tree_ident(expect)),
5213
                      istr(tree_ident(base)), istr(tree_ident(decl)));
5214

5215
         map_generic_package(tab, expect, pack);
76✔
5216
      }
5217
      break;
76✔
5218

5219
   case C_FUNCTION:
64✔
5220
   case C_PROCEDURE:
5221
      if (!sem_check(value, tab))
64✔
5222
         return false;
5223

5224
      if (!type_eq_map(tree_type(value), type, get_generic_map(tab)))
60✔
UNCOV
5225
         sem_error(value, "type of actual %s does not match type %s of formal "
×
5226
                   "generic %s", type_pp(tree_type(value)), type_pp(type),
5227
                   istr(tree_ident(decl)));
5228

5229
      assert(tree_kind(value) == T_REF);
60✔
5230

5231
      if (!tree_has_ref(value))
60✔
5232
         return false;
5233

5234
      tree_t sub = tree_ref(value);
60✔
5235
      assert(is_subprogram(sub));
60✔
5236

5237
      const bool pure_formal = !(tree_flags(decl) & TREE_F_IMPURE);
60✔
5238
      const bool pure_actual = !(tree_flags(sub) & TREE_F_IMPURE);
60✔
5239

5240
      if (pure_formal && !pure_actual) {
60✔
5241
         diag_t *d = pedantic_diag(tree_loc(value));
1✔
5242
         if (d != NULL) {
1✔
5243
            diag_printf(d, "cannot associate impure function %s with pure "
1✔
5244
                        "generic subprogram %s", type_pp(tree_type(value)),
5245
                        istr(tree_ident(decl)));
5246
            diag_emit(d);
1✔
5247
         }
5248
      }
5249

5250
      map_generic_subprogram(tab, decl, sub);
60✔
5251
      break;
60✔
5252

5253
   case C_CONSTANT:
1,198✔
5254
      if (name != NULL) {
1,198✔
5255
         if (!sem_check(name, tab))
774✔
5256
            return false;
5257

5258
         if (!sem_static_name(name, sem_locally_static))
773✔
5259
            sem_error(name, "formal generic name must be a locally "
1✔
5260
                      "static name");
5261
      }
5262

5263
      if (!sem_check(value, tab))
1,196✔
5264
         return false;
5265

5266
      if (!sem_check_type(value, type, tab))
1,193✔
5267
         sem_error(value, "type of actual %s does not match type %s of formal "
6✔
5268
                   "generic %s", type_pp(tree_type(value)), type_pp(type),
5269
                   istr(tree_ident(decl)));
5270

5271
      if (is_full) map_generic_const(tab, decl, value);
1,187✔
5272
      break;
5273

5274
   default:
5275
      // Was an earlier error
5276
      break;
5277
   }
5278

5279
   return true;
5280
}
5281

5282
static bool sem_check_generic_map(tree_t t, tree_t unit, nametab_t *tab)
2,322✔
5283
{
5284
   // Check there is an actual for each formal generic
5285
   // Rules for maps are described in LRM 93 section 5.2.1.2
5286

5287
   const int nformals = tree_generics(unit);
2,322✔
5288
   const int nactuals = tree_genmaps(t);
2,322✔
5289

5290
   formal_map_t *formals LOCAL = xmalloc_array(nformals, sizeof(formal_map_t));
2,322✔
5291

5292
   for (int i = 0; i < nformals; i++) {
5,770✔
5293
      formals[i].decl  = tree_generic(unit, i);
3,448✔
5294
      formals[i].state = MAP_MISSING;
3,448✔
5295
   }
5296

5297
   bool ok = true;
5298

5299
   for (int i = 0; i < nactuals; i++) {
3,976✔
5300
      tree_t actual = tree_genmap(t, i);
1,655✔
5301
      ok &= sem_check_generic_actual(formals, nformals, actual, unit, tab);
1,655✔
5302

5303
      if (!ok && tree_subkind(actual) == P_POS && i >= nformals)
1,655✔
5304
         break;   // Prevent useless repeated errors
5305
   }
5306

5307
   for (int i = 0; i < nformals; i++) {
5,770✔
5308
      if (formals[i].state >= MAP_PARTIAL)
3,448✔
5309
         continue;
1,632✔
5310

5311
      const class_t class = tree_class(formals[i].decl);
1,816✔
5312
      if (class == C_PACKAGE)
1,816✔
5313
         error_at(tree_loc(t), "missing actual for generic package %s",
1✔
5314
                  istr(tree_ident(formals[i].decl)));
1✔
5315
      else if (class == C_TYPE) {
1,815✔
5316
         error_at(tree_loc(t), "missing actual for generic type %s",
1✔
5317
                  istr(tree_ident(formals[i].decl)));
1✔
5318
         map_generic_type(tab, tree_type(formals[i].decl), type_new(T_NONE));
1✔
5319
      }
5320
      else if (tree_has_value(formals[i].decl)) {
1,814✔
5321
         tree_t value = tree_value(formals[i].decl);
1,808✔
5322
         if (tree_kind(value) == T_BOX) {
1,808✔
5323
            // Need to look up the matching subprogram now while we still
5324
            // have the symbol table
5325
            map_generic_box(tab, t, formals[i].decl, i);
1,558✔
5326
         }
5327
      }
5328
      else if (formals[i].state == MAP_MISSING) {
6✔
5329
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
5✔
5330
         diag_printf(d, "missing actual for generic %s without a "
5✔
5331
                     "default expression", istr(tree_ident(formals[i].decl)));
5✔
5332
         diag_hint(d, tree_loc(formals[i].decl), "generic %s declared here",
5✔
5333
                   istr(tree_ident(formals[i].decl)));
5✔
5334
         diag_emit(d);
5✔
5335
      }
5336
   }
5337

5338
   return ok;
2,322✔
5339
}
5340

5341
static bool sem_check_instance(tree_t t, nametab_t *tab)
1,487✔
5342
{
5343
   if (!tree_has_ref(t))
1,487✔
5344
      return false;
5345

5346
   tree_t unit = primary_unit_of(tree_ref(t));
1,475✔
5347

5348
   if (tree_has_spec(t)) {
1,475✔
5349
      tree_t spec = tree_spec(t);
95✔
5350

5351
      if (tree_class(t) != C_COMPONENT) {
95✔
5352
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(spec));
2✔
5353
         diag_printf(d, "specification may only be used with component"
2✔
5354
                     " instances");
5355
         diag_hint(d, tree_loc(spec), "specification for %s",
2✔
5356
                   istr(tree_ident(t)));
5357
         diag_hint(d, tree_loc(t), "%s instance", class_str(tree_class(t)));
2✔
5358
         diag_emit(d);
2✔
5359
         return false;
2✔
5360
      }
5361

5362
      assert(tree_kind(unit) == T_COMPONENT);   // Checked by parser
93✔
5363

5364
      if (tree_has_ref(spec) && tree_ref(spec) != unit) {
93✔
5365
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(spec));
2✔
5366
         diag_printf(d, "component mismatch for instance %s: expected %s "
2✔
5367
                     "but specification has %s", istr(tree_ident(t)),
5368
                     istr(tree_ident(unit)), istr(tree_ident(tree_ref(spec))));
5369
         diag_hint(d, tree_loc(spec), "specification has component %s",
2✔
5370
                   istr(tree_ident(tree_ref(spec))));
5371
         diag_hint(d, tree_loc(t), "instance of component %s",
2✔
5372
                   istr(tree_ident(unit)));
5373
         diag_emit(d);
2✔
5374
         return false;
2✔
5375
      }
5376
   }
5377

5378
   if (!sem_check_generic_map(t, unit, tab))
1,471✔
5379
      return false;
5380

5381
   if (!sem_check_port_map(t, unit, tab))
1,440✔
5382
      return false;
33✔
5383

5384
   return true;
5385
}
5386

5387
static bool sem_check_cond(tree_t t, nametab_t *tab)
12,090✔
5388
{
5389
   if (tree_has_value(t)) {
12,090✔
5390
      type_t std_bool = std_type(NULL, STD_BOOLEAN);
9,749✔
5391

5392
      tree_t value = tree_value(t);
9,749✔
5393
      if (!sem_check(value, tab))
9,749✔
5394
         return false;
5395

5396
      if (!sem_check_type(value, std_bool, tab))
9,744✔
5397
         sem_error(value, "type of condition must be %s but have %s",
2✔
5398
                   type_pp(std_bool), type_pp(tree_type(value)));
5399

5400
      if (!sem_check_readable(value))
9,742✔
UNCOV
5401
         return false;
×
5402
   }
5403

5404
   return true;
5405
}
5406

5407
static bool sem_check_if(tree_t t, nametab_t *tab)
8,688✔
5408
{
5409
   bool ok = true;
8,688✔
5410
   const int nconds = tree_conds(t);
8,688✔
5411
   for (int i = 0; i < nconds; i++)
20,601✔
5412
      ok &= sem_check_cond(tree_cond(t, i), tab);
11,913✔
5413

5414
   return ok;
8,688✔
5415
}
5416

5417
static bool sem_static_subtype(type_t type, static_fn_t fn)
271,394✔
5418
{
5419
   // Rules for locally static subtypes are in LRM 93 7.4.1
5420

5421
   if (type_is_unconstrained(type))
271,394✔
5422
      return false;
5423

5424
   if (type_is_scalar(type))
249,074✔
5425
      return true;
5426

5427
   if (type_is_record(type)) {
240,192✔
5428
      const int nfields = type_fields(type);
294✔
5429
      for (int i = 0; i < nfields; i++) {
1,120✔
5430
         if (!sem_static_subtype(tree_type(type_field(type, i)), fn))
844✔
5431
            return false;
5432
      }
5433

5434
      return true;
5435
   }
5436

5437
   switch (type_kind(type)) {
239,898✔
5438
   case T_SUBTYPE:
239,846✔
5439
      {
5440
         const int ndims = dimension_of(type);
239,846✔
5441
         for (int i = 0; i < ndims; i++) {
271,639✔
5442
            if (!(*fn)(range_of(type, i)))
240,004✔
5443
               return false;
5444
         }
5445

5446
         return true;
5447
      }
5448
   default:
5449
      return true;
5450
   }
5451
}
5452

5453
static bool sem_ieee_locally_static(tree_t decl)
62,152✔
5454
{
5455
   // Subprograms definined in certain IEEE packages are treated the
5456
   // same as builtin operators in VHDL-2008
5457

5458
   if (standard() < STD_08)
62,152✔
5459
      return false;
5460

5461
   ident_t unit_name = tree_ident(tree_container(decl));
34,577✔
5462

5463
   switch (is_well_known(unit_name)) {
34,577✔
5464
   case W_NUMERIC_STD:
5465
   case W_NUMERIC_BIT:
5466
   case W_IEEE_1164:
5467
   case W_NUMERIC_BIT_UNSIGNED:
5468
   case W_NUMERIC_STD_UNSIGNED:
5469
      return true;
5470
   default:
25,388✔
5471
      return false;
25,388✔
5472
   }
5473
}
5474

5475
static bool sem_locally_static(tree_t t)
14,228,540✔
5476
{
5477
   // Rules for locally static expressions are in LRM 93 7.4.1
5478

5479
   type_t type = tree_type(t);
14,251,375✔
5480
   tree_kind_t kind = tree_kind(t);
14,251,375✔
5481

5482
   if (type_is_none(type))
14,251,375✔
5483
      return true;   // Prevents further cascading errors
5484

5485
   // Any literal other than of type time
5486
   if (kind == T_LITERAL) {
14,251,366✔
5487
      if (tree_subkind(t) == L_PHYSICAL)
113,517✔
5488
         return !type_eq(type, std_type(NULL, STD_TIME));
1,511✔
5489
      else
5490
         return true;
5491
   }
5492
   else if (kind == T_STRING)
14,137,849✔
5493
      return true;
5494
   else if ((kind == T_REF) && (tree_kind(tree_ref(t)) == T_ENUM_LIT))
14,125,640✔
5495
      return true;
5496
   else if (kind == T_OPEN)
14,080,249✔
5497
      return true;
5498

5499
   if (kind == T_REF) {
14,080,227✔
5500
      tree_t decl = tree_ref(t);
458,238✔
5501
      const tree_kind_t dkind = tree_kind(decl);
458,238✔
5502

5503
      // A constant reference (other than a deferred constant) with a
5504
      // locally static value
5505
      if (dkind == T_CONST_DECL && (tree_flags(decl) & TREE_F_LOCALLY_STATIC))
458,238✔
5506
         return true;
5507

5508
      // An alias of a locally static name
5509
      if (dkind == T_ALIAS)
427,740✔
5510
         return sem_locally_static(tree_value(decl));
1,771✔
5511

5512
      // [2008] A formal generic constant of a generic-mapped subprogram
5513
      // or package with a locally static subtype
5514
      if (dkind == T_GENERIC_DECL && (tree_flags(decl) & TREE_F_LOCALLY_STATIC))
425,969✔
5515
         return true;
5516
   }
5517

5518
   // A locally static range
5519
   if (kind == T_RANGE) {
14,046,772✔
5520
      switch (tree_subkind(t)) {
248,123✔
5521
      case RANGE_TO:
244,018✔
5522
      case RANGE_DOWNTO:
5523
         return sem_locally_static(tree_left(t))
244,018✔
5524
            && sem_locally_static(tree_right(t));
244,543✔
5525

5526
      case RANGE_EXPR:
4,105✔
5527
         return sem_locally_static(tree_value(t));
4,105✔
5528

5529
      default:
5530
         return false;
5531
      }
5532
   }
5533

5534
   // A function call of an implicit operator or [2008] an operation
5535
   // defined in one of the packages STD_LOGIC_1164, NUMERIC_BIT,
5536
   // NUMERIC_STD, NUMERIC_BIT_UNSIGNED, or NUMERIC_STD_UNSIGNED in
5537
   // library IEEE whose actuals are locally static expressions.
5538
   if (kind == T_FCALL) {
13,798,649✔
5539
      if (!tree_has_ref(t))
12,991,068✔
5540
         return true;  // Suppress further errors
5541
      else if (tree_flags(t) & TREE_F_LOCALLY_STATIC)
12,991,068✔
5542
         return true;
5543

5544
      tree_t decl = tree_ref(t);
12,846,903✔
5545
      if (tree_kind(decl) == T_GENERIC_DECL)
12,846,903✔
5546
         return false;   // Not known at this point
5547
      else if (tree_subkind(decl) == S_USER && !sem_ieee_locally_static(decl))
12,845,910✔
5548
         return false;
5549

5550
      const int nparams = tree_params(t);
12,792,947✔
5551
      for (int i = 0; i < nparams; i++) {
12,818,469✔
5552
         if (!sem_locally_static(tree_value(tree_param(t, i))))
12,806,553✔
5553
            return false;
5554
      }
5555

5556
      return true;
5557
   }
5558

5559
   if (kind == T_ATTR_REF) {
5560
      // A predefined attribute other than those listed below whose prefix
5561
      // prefix is either a locally static subtype or is an object that is
5562
      // of a locally static subtype
5563
      const attr_kind_t predef = tree_subkind(t);
247,492✔
5564
      if (predef == ATTR_EVENT || predef == ATTR_ACTIVE
247,492✔
5565
          || predef == ATTR_LAST_EVENT || predef == ATTR_LAST_ACTIVE
247,492✔
5566
          || predef == ATTR_LAST_VALUE || predef == ATTR_DRIVING
246,375✔
5567
          || predef == ATTR_DRIVING_VALUE || predef == ATTR_PATH_NAME
5568
          || predef == ATTR_INSTANCE_NAME || predef == ATTR_SIMPLE_NAME)
5569
         return false;
5570

5571
      // Whose actual parameter (if any) is a locally static expression
5572
      const int nparams = tree_params(t);
242,257✔
5573
      for (int i = 0; i < nparams; i++) {
242,757✔
5574
         if (!sem_locally_static(tree_value(tree_param(t, i))))
3,091✔
5575
            return false;
5576
      }
5577

5578
      if (tree_has_value(t)) {
239,666✔
5579
         // A user-defined attribute whose value is a locally static expression
5580
         return sem_locally_static(tree_value(t));
1,394✔
5581
      }
5582

5583
      type_t type = tree_type(tree_name(t));
238,272✔
5584
      return sem_static_subtype(type, sem_locally_static);
238,272✔
5585
   }
5586

5587
   // A qualified expression whose operand is locally static
5588
   if (kind == T_QUALIFIED)
5589
      return sem_locally_static(tree_value(t));
4,653✔
5590

5591
   // A type conversion whose expression is locally static
5592
   if (kind == T_TYPE_CONV)
5593
      return sem_locally_static(tree_value(t));
3,571✔
5594

5595
   // Aggregates must have locally static range and all elements
5596
   // must have locally static values
5597
   if (kind == T_AGGREGATE) {
5598
      if (type_is_unconstrained(type))
6,355✔
5599
         return false;
5600

5601
      if (type_is_array(type)) {
6,245✔
5602
         if (!sem_locally_static(range_of(type, 0)))
5,214✔
5603
            return false;
5604
      }
5605

5606
      const int nassocs = tree_assocs(t);
5,987✔
5607
      for (int i = 0; i < nassocs; i++) {
52,562✔
5608
         tree_t a = tree_assoc(t, i);
47,522✔
5609
         if ((tree_subkind(a) == A_NAMED) && !sem_locally_static(tree_name(a)))
47,522✔
5610
            return false;
5611

5612
         if (!sem_locally_static(tree_value(a)))
47,522✔
5613
            return false;
5614
      }
5615

5616
      return true;
5617
   }
5618

5619
   // A record field name
5620
   if ((kind == T_REF) && (tree_kind(tree_ref(t)) == T_FIELD_DECL))
424,783✔
5621
      return true;
5622

5623
   const bool std08_rules = standard() >= STD_08 || relaxed_rules();
544,782✔
5624

5625
   // [2008] An indexed name whose prefix and index expressions are
5626
   // locally static
5627
   if (std08_rules && kind == T_ARRAY_REF) {
544,782✔
5628
      const int nparams = tree_params(t);
6,426✔
5629
      for (int i = 0; i < nparams; i++) {
8,044✔
5630
         if (!sem_locally_static(tree_value(tree_param(t, i))))
6,480✔
5631
            return false;
5632
      }
5633

5634
      return sem_locally_static(tree_value(t));
1,564✔
5635
   }
5636

5637
   // [2008] A slice name whose prefix and range is locally static
5638
   if (std08_rules && kind == T_ARRAY_SLICE) {
538,356✔
5639
      if (!sem_locally_static(tree_range(t, 0)))
1,687✔
5640
         return false;
5641

5642
      return sem_locally_static(tree_value(t));
472✔
5643
   }
5644

5645
   // [2008] A selected name whose prefix is locally static
5646
   if (std08_rules && kind == T_RECORD_REF)
536,669✔
5647
      return sem_locally_static(tree_value(t));
5,305✔
5648

5649
   return false;
5650
}
5651

5652
static bool sem_static_name(tree_t t, static_fn_t check_fn)
11,174✔
5653
{
5654
   // Rules for static names are in LRM 93 6.1
5655

5656
   switch (tree_kind(t)) {
12,346✔
5657
   case T_REF:
10,145✔
5658
      {
5659
         tree_t decl = tree_ref(t);
10,145✔
5660
         switch (tree_kind(decl)) {
10,145✔
5661
         case T_SIGNAL_DECL:
5662
         case T_VAR_DECL:
5663
         case T_CONST_DECL:
5664
         case T_PORT_DECL:
5665
         case T_TYPE_DECL:
5666
         case T_SUBTYPE_DECL:
5667
         case T_ENTITY:
5668
         case T_ARCH:
5669
         case T_PACK_BODY:
5670
         case T_PACKAGE:
5671
         case T_FUNC_BODY:
5672
         case T_PROC_BODY:
5673
         case T_FUNC_DECL:
5674
         case T_PROC_DECL:
5675
         case T_PROCESS:
5676
         case T_BLOCK:
5677
         case T_ENUM_LIT:
5678
         case T_IMPLICIT_SIGNAL:
5679
         case T_GENERIC_DECL:
5680
         case T_PARAM_DECL:
5681
         case T_CONCURRENT:
5682
         case T_VIEW_DECL:
5683
         case T_UNIT_DECL:
5684
            return true;
5685
         case T_ALIAS:
29✔
5686
            return sem_static_name(tree_value(decl), check_fn);
29✔
UNCOV
5687
         default:
×
UNCOV
5688
            return false;
×
5689
         }
5690
      }
5691

5692
   case T_EXTERNAL_NAME:
5693
      return true;
5694

5695
   case T_RECORD_REF:
1,137✔
5696
      return sem_static_name(tree_value(t), check_fn);
1,137✔
5697

5698
   case T_ARRAY_REF:
844✔
5699
      {
5700
         if (!sem_static_name(tree_value(t), check_fn))
844✔
5701
            return false;
5702

5703
         const int nparams = tree_params(t);
844✔
5704
         for (int i = 0; i < nparams; i++) {
1,680✔
5705
            if (!(*check_fn)(tree_value(tree_param(t, i))))
847✔
5706
               return false;
5707
         }
5708

5709
         return true;
5710
      }
5711

5712
   case T_ARRAY_SLICE:
162✔
5713
      {
5714
         if (!sem_static_name(tree_value(t), check_fn))
162✔
5715
            return false;
5716

5717
         return (*check_fn)(tree_range(t, 0));
162✔
5718
      }
5719

5720
   case T_ATTR_REF:
6✔
5721
      {
5722
         switch (tree_subkind(t)) {
6✔
5723
         case ATTR_DELAYED:
6✔
5724
         case ATTR_STABLE:
5725
         case ATTR_QUIET:
5726
         case ATTR_TRANSACTION:
5727
            return sem_static_name(tree_name(t), check_fn);
6✔
5728
         default:
5729
            return false;
5730
         }
5731
      }
5732

5733
   default:
5✔
5734
      return false;
5✔
5735
   }
5736
}
5737

5738
static bool sem_globally_static(tree_t t)
792,167✔
5739
{
5740
   // Rules for globally static expressions are in LRM 93 7.4.2
5741

5742
   type_t type = tree_type(t);
796,972✔
5743
   tree_kind_t kind = tree_kind(t);
796,972✔
5744

5745
   if (type_is_none(type))
796,972✔
5746
      return true;   // Prevents further cascading errors
5747

5748
   // A literal of type TIME
5749

5750
   if (type_eq(type, std_type(NULL, STD_TIME))) {
796,971✔
5751
      if (kind == T_REF && tree_kind(tree_ref(t)) == T_UNIT_DECL)
6,304✔
5752
         return true;
5753
      else if (kind == T_LITERAL && tree_subkind(t) == L_PHYSICAL)
6,225✔
5754
         return true;
5755
   }
5756

5757
   // A locally static primary
5758

5759
   if (sem_locally_static(t))
795,986✔
5760
      return true;
5761

5762
   // A generic constant, generate parameter, or constant
5763

5764
   if (kind == T_REF) {
540,089✔
5765
      tree_t decl = tree_ref(t);
105,916✔
5766
      switch (tree_kind(decl)) {
105,916✔
5767
      case T_GENERIC_DECL:
5768
         return true;
5769
      case T_CONST_DECL:
4,143✔
5770
         // Do not treat all constants as globally static, this is a
5771
         // defect in the LRM
5772
         return !!(tree_flags(decl) & TREE_F_GLOBALLY_STATIC);
4,143✔
5773
      default:
45,006✔
5774
         return false;
45,006✔
5775
      }
5776
   }
5777
   else if (kind == T_EXTERNAL_NAME)
5778
      return tree_class(t) == C_CONSTANT;
97✔
5779

5780
   // An alias whose aliased name is globally static
5781

5782
   if (kind == T_ALIAS)
UNCOV
5783
      return sem_globally_static(tree_value(t));
×
5784

5785
   if (kind == T_RANGE) {
5786
      if (tree_subkind(t) == RANGE_EXPR)
26,785✔
5787
         return sem_globally_static(tree_value(t));
19✔
5788

5789
      if (!sem_globally_static(tree_left(t)))
26,766✔
5790
         return false;
5791

5792
      if (!sem_globally_static(tree_right(t)))
26,749✔
5793
         return false;
5794

5795
      return true;
26,707✔
5796
   }
5797

5798
   // Aggregates must have globally static range and all elements
5799
   // must have globally static values
5800
   if (kind == T_AGGREGATE) {
5801
      if (type_is_array(type)
689✔
5802
          && !type_is_unconstrained(type)
480✔
5803
          && !sem_globally_static(range_of(type, 0)))
437✔
5804
         return false;
5805

5806
      const int nassocs = tree_assocs(t);
647✔
5807
      for (int i = 0; i < nassocs; i++) {
2,609✔
5808
         tree_t a = tree_assoc(t, i);
2,008✔
5809
         if ((tree_subkind(a) == A_NAMED) && !sem_globally_static(tree_name(a)))
2,008✔
5810
            return false;
5811

5812
         if (!sem_globally_static(tree_value(a)))
2,008✔
5813
            return false;
5814
      }
5815

5816
      return true;
5817
   }
5818

5819
   // A function call of a pure function with globally static actuals
5820
   if (kind == T_FCALL) {
5821
      tree_t decl = tree_ref(t);
331,814✔
5822
      if (tree_flags(decl) & TREE_F_IMPURE)
331,814✔
5823
         return false;
5824

5825
      bool all_static = true;
328,295✔
5826
      const int nparams = tree_params(t);
328,295✔
5827
      for (int i = 0; i < nparams; i++) {
974,283✔
5828
         tree_t p = tree_param(t, i);
645,988✔
5829
         all_static = all_static && sem_globally_static(tree_value(p));
734,069✔
5830
      }
5831
      return all_static;
328,295✔
5832
   }
5833

5834
   if (kind == T_ATTR_REF) {
5835
      const attr_kind_t predef = tree_subkind(t);
36,588✔
5836

5837
      // A predefined attribute that is one of 'SIMPLE_NAME,
5838
      // 'INSTANCE_NAME, or 'PATH_NAME
5839
      if (predef == ATTR_SIMPLE_NAME || predef == ATTR_INSTANCE_NAME
36,588✔
5840
          || predef == ATTR_PATH_NAME)
35,718✔
5841
         return true;   // Clause j
5842

5843
      // A predefined attribute other than those listed below whose
5844
      // prefix is either a globally static subtype or is an object or
5845
      // function call that is of a globally static subtype, or in 2008,
5846
      // a prefix which is a appropriate for a globally static attribute
5847
      if (predef == ATTR_EVENT || predef == ATTR_ACTIVE
35,448✔
5848
          || predef == ATTR_LAST_EVENT || predef == ATTR_LAST_ACTIVE
35,448✔
5849
          || predef == ATTR_LAST_VALUE || predef == ATTR_DRIVING
35,097✔
5850
          || predef == ATTR_DRIVING_VALUE)
35,026✔
5851
         return false;   // Clause k
5852
      else if (predef == ATTR_USER) {
34,998✔
5853
         // A user-defined attribute whose value is a globally static
5854
         // expression
5855
         return sem_globally_static(tree_value(t));
120✔
5856
      }
5857

5858
      const int nparams = tree_params(t);
34,878✔
5859
      for (int i = 0; i < nparams; i++) {
35,065✔
5860
         if (!sem_globally_static(tree_value(tree_param(t, i))))
833✔
5861
            return false;
5862
      }
5863

5864
      tree_t name = tree_name(t);
34,232✔
5865

5866
      if (standard() >= STD_08 || relaxed_rules()) {
34,232✔
5867
         // LRM 08 section 9.4.3: A prefix is appropriate for a globally
5868
         // static attribute if it denotes a signal, a constant, a type
5869
         // or subtype, a globally static function call, a variable that
5870
         // is not of an access type, or a variable of an access type
5871
         // whose designated subtype is fully constrained.
5872

5873
         if (tree_kind(name) == T_FCALL)
5,800✔
5874
            return sem_globally_static(name);
5875

5876
         tree_t ref = name_to_ref(name);
5,800✔
5877
         if (ref == NULL)
5,800✔
5878
            return false;
5879

5880
         tree_t decl = tree_ref(ref);
5,796✔
5881
         const tree_kind_t dkind = tree_kind(decl);
5,796✔
5882
         if (dkind == T_VAR_DECL && type_is_access(tree_type(name)))
5,796✔
5883
            return false;
5884

5885
         return dkind == T_CONST_DECL || dkind == T_SIGNAL_DECL
5,796✔
5886
            || dkind == T_TYPE_DECL || dkind == T_VAR_DECL
5,766✔
5887
            || dkind == T_SUBTYPE_DECL || dkind == T_PORT_DECL
5888
            || dkind == T_GENERIC_DECL;
11,562✔
5889
      }
5890

5891
      type_t type = get_type_or_null(name);
28,432✔
5892
      if (type == NULL)
28,432✔
5893
         return false;
5894

5895
      return sem_static_subtype(type, sem_globally_static);
28,432✔
5896
   }
5897

5898
   // A qualified expression whose operand is globally static
5899

5900
   if (kind == T_QUALIFIED)
5901
      return sem_globally_static(tree_value(t));
238✔
5902

5903
   // A type conversion whose operand is globally static
5904

5905
   if (kind == T_TYPE_CONV)
5906
      return sem_globally_static(tree_value(t));
1,038✔
5907

5908
   // TODO: clauses o, p
5909

5910
   // A sub-element or slice where indexes are globally static
5911

5912
   if (kind == T_ARRAY_REF) {
5913
      if (!sem_globally_static(tree_value(t)))
30,153✔
5914
         return false;
5915

5916
      const int nparams = tree_params(t);
24,970✔
5917
      for (int i = 0; i < nparams; i++) {
49,555✔
5918
         if (!sem_globally_static(tree_value(tree_param(t, i))))
25,008✔
5919
            return false;
5920
      }
5921

5922
      return true;
5923
   }
5924
   else if (kind == T_ARRAY_SLICE) {
5925
      if (!sem_globally_static(tree_value(t)))
864✔
5926
         return false;
5927

5928
      if (!sem_globally_static(tree_range(t, 0)))
90✔
5929
         return false;
5930

5931
      return true;
90✔
5932
   }
5933
   else if (kind == T_RECORD_REF)
5934
      return sem_globally_static(tree_value(t));
3,390✔
5935

5936
   return false;
5937
}
5938

5939
static bool sem_check_case(tree_t t, nametab_t *tab)
611✔
5940
{
5941
   tree_t test = tree_value(t);
611✔
5942
   if (!sem_check(test, tab))
611✔
5943
      return false;
5944

5945
   type_t type = tree_type(test);
607✔
5946

5947
   // LRM 93 8.8 if the type of the expression is an array then it must be
5948
   // a one dimensional character array type
5949

5950
   const bool is_1d_character_array = type_is_character_array(type);
607✔
5951
   const bool valid = is_1d_character_array || type_is_discrete(type);
607✔
5952

5953
   if (!valid) {
607✔
5954
      diag_t *d = diag_new(DIAG_ERROR, tree_loc(test));
2✔
5955
      diag_printf(d, "case expression must have a discrete type or one "
2✔
5956
                  "dimensional character array type");
5957
      if (type_is_array(type) && dimension_of(type) != 1)
2✔
5958
         diag_hint(d, tree_loc(test), "array has %d dimensions",
1✔
5959
                   dimension_of(type));
5960
      else if (type_is_array(type))
1✔
5961
         diag_hint(d, tree_loc(test), "type %s is not a character array",
1✔
5962
                   type_pp(type));
5963
      else
UNCOV
5964
         diag_hint(d, tree_loc(test), "type is %s", type_pp(type));
×
5965
      diag_lrm(d, STD_08, "10.9");
2✔
5966
      diag_emit(d);
2✔
5967
      return false;
2✔
5968
   }
5969

5970
   if (is_1d_character_array && standard() < STD_08) {
605✔
5971
      // VHDL-93 requires a locally static subtype, relaxed in later
5972
      // revisions
5973
      if (!sem_static_subtype(type, sem_locally_static))
54✔
5974
         sem_error(test, "case expression must have locally static subtype");
2✔
5975
   }
5976

5977
   static_fn_t static_fn = sem_locally_static;
603✔
5978
   const char *static_str = "locally";
603✔
5979

5980
   if (tree_kind(t) == T_CASE_GENERATE) {
603✔
5981
      static_fn = sem_globally_static;
10✔
5982
      static_str = "globally";
10✔
5983
   }
5984

5985
   const int nstmts = tree_stmts(t);
603✔
5986
   for (int i = 0; i < nstmts; i++) {
3,314✔
5987
      tree_t alt = tree_stmt(t, i);
2,725✔
5988

5989
      const int nassocs = tree_assocs(alt);
2,725✔
5990
      for (int j = 0; j < nassocs; j++) {
6,057✔
5991
         tree_t a = tree_assoc(alt, j);
3,346✔
5992
         switch (tree_subkind(a)) {
3,346✔
5993
         case A_OTHERS:
426✔
5994
            if (j != nassocs - 1 || i != nstmts - 1) {
426✔
5995
               diag_t *d = diag_new(DIAG_ERROR, tree_loc(a));
1✔
5996
               diag_printf(d, "others choice must appear last");
1✔
5997
               diag_hint(d, tree_loc(a), "others choice");
1✔
5998

5999
               tree_t more = j + 1 < nassocs
2✔
6000
                  ? tree_assoc(alt, j + 1) : tree_assoc(tree_stmt(t, i + 1), 0);
1✔
6001
               diag_hint(d, tree_loc(more), "further choices follow this");
1✔
6002

6003
               diag_emit(d);
1✔
6004
               return false;
1✔
6005
            }
6006
            break;
6007

6008
         case A_NAMED:
2,877✔
6009
            {
6010
               tree_t name = tree_name(a);
2,877✔
6011
               if (!sem_check(name, tab))
2,877✔
6012
                  return false;
6013

6014
               if (!sem_check_type(name, type, tab))
2,877✔
6015
                  sem_error(name, "case choice must have type %s but found %s",
1✔
6016
                            type_pp(type), type_pp(tree_type(name)));
6017
               else if (!(*static_fn)(name))
2,876✔
6018
                  sem_error(name, "case choice must be %s static", static_str);
9✔
6019
            }
6020
            break;
6021

6022
         case A_RANGE:
43✔
6023
            {
6024
               tree_t r = tree_range(a, 0);
43✔
6025
               if (!sem_check_discrete_range(r, type, tab))
43✔
6026
                  return false;
6027

6028
               switch (tree_subkind(r)) {
41✔
6029
               case RANGE_TO:
33✔
6030
               case RANGE_DOWNTO:
6031
                  if (!(*static_fn)(tree_left(r)))
33✔
UNCOV
6032
                     sem_error(tree_left(r), "left index of case choice "
×
6033
                               "range is not %s static", static_str);
6034
                  else if (!(*static_fn)(tree_right(r)))
33✔
6035
                     sem_error(tree_right(r), "right index of case choice "
1✔
6036
                               "range is not %s static", static_str);
6037
                  break;
6038

6039
               case RANGE_EXPR:
8✔
6040
                  if (!(*static_fn)(tree_value(r)))
8✔
UNCOV
6041
                     sem_error(tree_value(r), "range expression is not %s "
×
6042
                               "static", static_str);
6043
                  break;
6044

6045
               default:
6046
                  return false;
6047
               }
6048
            }
6049
            break;
6050
         }
6051
      }
6052
   }
6053

6054
   return true;
6055
}
6056

6057
static bool sem_check_match_case(tree_t t, nametab_t *tab)
43✔
6058
{
6059
   // Matching case statement is in LRM 08 section 10.9
6060

6061
   if (!sem_check_case(t, tab))
43✔
6062
      return false;
6063

6064
   tree_t value = tree_value(t);
42✔
6065
   type_t type = tree_type(value);
42✔
6066

6067
   type_t std_bit = std_type(NULL, STD_BIT);
42✔
6068
   type_t std_logic = ieee_type(IEEE_STD_ULOGIC);
42✔
6069

6070
   type_t elem = type;
42✔
6071
   if (type_is_array(type) && dimension_of(type) == 1)
42✔
6072
      elem = type_elem(type);
34✔
6073

6074
   if (!type_eq(elem, std_bit) && !type_eq(elem, std_logic)) {
42✔
6075
      diag_t *d = diag_new(DIAG_ERROR, tree_loc(value));
1✔
6076
      diag_printf(d, "type of expression in a matching case statement must be "
1✔
6077
                  "BIT, STD_ULOGIC, or a one-dimensional array of these types");
6078
      diag_hint(d, tree_loc(value), "type is %s", type_pp(type));
1✔
6079
      diag_lrm(d, STD_08, "10.9");
1✔
6080
      diag_emit(d);
1✔
6081
      return false;
1✔
6082
   }
6083

6084
   return true;
6085
}
6086

6087
static bool sem_check_return(tree_t t, nametab_t *tab)
13,036✔
6088
{
6089
   tree_t sub = find_enclosing(tab, S_SUBPROGRAM);
13,036✔
6090
   if (sub == NULL)
13,036✔
6091
      sem_error(t, "return statement not allowed outside subprogram");
3✔
6092

6093
   if (tree_has_value(t)) {
13,033✔
6094
      if (tree_kind(sub) == T_PROC_BODY)
12,688✔
6095
         sem_error(t, "cannot return a value from a procedure");
1✔
6096

6097
      tree_t value = tree_value(t);
12,687✔
6098
      if (!sem_check(value, tab))
12,687✔
6099
         return false;
6100

6101
      type_t expect = tree_type(t);
12,681✔
6102
      if (!sem_check_type(value, expect, tab))
12,681✔
6103
         sem_error(t, "expected return type %s but have %s",
2✔
6104
                   type_pp(expect), type_pp(tree_type(value)));
6105
   }
6106
   else if (tree_kind(sub) == T_FUNC_BODY)
345✔
6107
      sem_error(t, "return in function must have an expression");
1✔
6108

6109
   return true;
6110
}
6111

6112
static bool sem_check_cond_return(tree_t t, nametab_t *tab)
7✔
6113
{
6114
   tree_t sub = find_enclosing(tab, S_SUBPROGRAM);
7✔
6115
   if (sub == NULL)
7✔
6116
      sem_error(t, "return statement not allowed outside subprogram");
1✔
6117

6118
   if (tree_kind(sub) != T_PROC_BODY)
6✔
6119
      sem_error(t, "conditional return statement without value is only "
1✔
6120
                "valid inside a procedure");
6121

6122
   tree_t value = tree_value(t);
5✔
6123
   if (!sem_check(value, tab))
5✔
6124
      return false;
6125

6126
   type_t std_bool = std_type(NULL, STD_BOOLEAN);
5✔
6127

6128
   if (!sem_check_type(value, std_bool, tab))
5✔
6129
      sem_error(value, "type of condition must be %s but have %s",
1✔
6130
                type_pp(std_bool), type_pp(tree_type(value)));
6131

6132
   return true;
6133
}
6134

6135
static bool sem_check_while(tree_t t, nametab_t *tab)
201✔
6136
{
6137
   type_t std_bool = std_type(NULL, STD_BOOLEAN);
201✔
6138

6139
   tree_t value = tree_value(t);
201✔
6140
   if (!sem_check(value, tab))
201✔
6141
      return false;
6142

6143
   if (!sem_check_type(value, std_bool, tab))
201✔
6144
      sem_error(value, "type of loop condition must be %s but is %s",
1✔
6145
                type_pp(std_bool), type_pp(tree_type(value)));
6146

6147
   return true;
6148
}
6149

6150
static bool sem_check_for(tree_t t, nametab_t *tab)
2,553✔
6151
{
6152
   if (!sem_check_discrete_range(tree_range(t, 0), NULL, tab))
2,553✔
6153
      return false;
6154

6155
   tree_t idecl = tree_decl(t, 0);
2,539✔
6156

6157
   if (!sem_check_subtype(idecl, tree_type(idecl), tab))
2,539✔
UNCOV
6158
      return false;
×
6159

6160
   return true;
6161
}
6162

6163
static bool sem_check_block(tree_t t, nametab_t *tab)
390✔
6164
{
6165
   if (!sem_check_generic_map(t, t, tab))
390✔
6166
      return false;
6167

6168
   if (!sem_check_port_map(t, t, tab))
385✔
6169
      return false;
5✔
6170

6171
   return true;
6172
}
6173

6174
static bool sem_check_loop_control(tree_t t, nametab_t *tab)
412✔
6175
{
6176
   if (tree_has_value(t)) {
412✔
6177
      tree_t value = tree_value(t);
195✔
6178
      if (!sem_check(value, tab))
195✔
6179
         return false;
6180

6181
      type_t std_bool = std_type(NULL, STD_BOOLEAN);
195✔
6182
      if (!type_eq(tree_type(value), std_bool))
195✔
6183
         sem_error(value, "type of %s condition must be %s but is %s",
3✔
6184
                   (tree_kind(t) == T_EXIT) ? "exit" : "next",
6185
                   type_pp(std_bool), type_pp(tree_type(value)));
6186
   }
6187

6188
   return true;
6189
}
6190

6191
static bool sem_check_attr_decl(tree_t t)
118✔
6192
{
6193
   if (!sem_no_access_file_or_protected(t, tree_type(t), "attributes"))
118✔
6194
      return false;
5✔
6195

6196
   return true;
6197
}
6198

6199
static bool sem_check_attr_spec(tree_t t, nametab_t *tab)
342✔
6200
{
6201
   tree_t value = tree_value(t);
342✔
6202
   if (!sem_check(value, tab))
342✔
6203
      return false;
6204

6205
   type_t type = tree_type(t);
342✔
6206
   if (!sem_check_type(value, type, tab))
342✔
6207
      sem_error(t, "expected attribute specification for %s to have type %s "
2✔
6208
                "but found %s", istr(tree_ident(t)), type_pp(type),
6209
                type_pp(tree_type(value)));
6210

6211
   if (!tree_has_ref(t))
340✔
6212
      return false;
6213

6214
   tree_t decl = tree_ref(t);
325✔
6215
   const class_t class = tree_class(t);
325✔
6216

6217
   if (class_of(decl) != class)
325✔
6218
      sem_error(t, "class of object %s is %s not %s",
1✔
6219
                istr(tree_ident(decl)), class_str(class_of(decl)),
6220
                class_str(class));
6221

6222
   switch (is_well_known(tree_ident(t))) {
324✔
6223
   case W_NEVER_WAITS:
110✔
6224
      {
6225
         bool flag;
110✔
6226
         if (!folded_bool(value, &flag))
110✔
6227
            sem_error(value, "expression must be a BOOLEAN literal");
2✔
6228
         else if (class != C_PROCEDURE)
109✔
6229
            sem_error(t, "NEVER_WAITS attribute can only be applied to "
1✔
6230
                      "procedures");
6231
         else if (flag && !tree_frozen(decl))
108✔
6232
            tree_set_flag(decl, TREE_F_NEVER_WAITS);
107✔
6233
      }
6234
      break;
108✔
6235

6236
   case W_FOREIGN:
72✔
6237
      if (!tree_frozen(decl))
72✔
6238
         tree_set_flag(decl, TREE_F_NEVER_WAITS);
48✔
6239
      break;
6240

6241
   default:
6242
      break;
6243
   }
6244

6245
   return true;
6246
}
6247

6248
static bool sem_check_if_generate(tree_t t, nametab_t *tab)
167✔
6249
{
6250
   const int nconds = tree_conds(t);
167✔
6251
   for (int i = 0; i < nconds; i++) {
341✔
6252
      tree_t cond = tree_cond(t, i);
177✔
6253

6254
      if (!sem_check_cond(cond, tab))
177✔
6255
         return false;
6256

6257
      if (tree_has_value(cond)) {
175✔
6258
         tree_t value = tree_value(cond);
166✔
6259
         if (!sem_globally_static(value))
166✔
6260
            sem_error(value, "condition of generate statement must be static");
175✔
6261
      }
6262
   }
6263

6264
   return true;
6265
}
6266

6267
static bool sem_check_for_generate(tree_t t, nametab_t *tab)
164✔
6268
{
6269
   tree_t r = tree_range(t, 0);
164✔
6270
   if (!sem_check_discrete_range(r, NULL, tab))
164✔
6271
      return false;
6272

6273
   if (!sem_globally_static(r))
162✔
6274
      sem_error(r, "range of generate statement must be static");
1✔
6275

6276
   tree_t idecl = tree_decl(t, 0);
161✔
6277
   assert(tree_kind(idecl) == T_GENERIC_DECL);
161✔
6278

6279
   if (!sem_check_subtype(idecl, tree_type(idecl), tab))
161✔
UNCOV
6280
      return false;
×
6281

6282
   return true;
6283
}
6284

6285
static bool sem_check_open(tree_t t)
75✔
6286
{
6287
   return true;
75✔
6288
}
6289

6290
static bool sem_check_file_decl(tree_t t, nametab_t *tab)
154✔
6291
{
6292
   // Rules for file declarations are in LRM 93 section 4.3.1.4
6293

6294
   if (!type_is_file(tree_type(t)))
154✔
6295
      sem_error(t, "file declarations must have file type");
1✔
6296

6297
   if (tree_has_value(t)) {
153✔
6298
      tree_t value = tree_value(t);
46✔
6299
      if (!sem_check(value, tab))
46✔
6300
         return false;
6301

6302
      if (!sem_check_type(value, std_type(NULL, STD_STRING), tab))
46✔
6303
         sem_error(value, "file name must have type STRING");
1✔
6304

6305
      tree_t mode = tree_file_mode(t);
45✔
6306
      if (!sem_check(mode, tab))
45✔
6307
         return false;
6308

6309
      if (!sem_check_type(mode, std_type(NULL, STD_FILE_OPEN_KIND), tab))
44✔
6310
         sem_error(mode, "open mode must have type FILE_OPEN_KIND");
1✔
6311
   }
6312

6313
   tree_t sub = find_enclosing(tab, S_SUBPROGRAM);
150✔
6314
   const bool in_pure_func =
300✔
6315
      sub != NULL && tree_kind(sub) == T_FUNC_BODY
25✔
6316
      && !(tree_flags(sub) & TREE_F_IMPURE);
153✔
6317

6318
   if (in_pure_func) {
150✔
6319
      diag_t *d = pedantic_diag(tree_loc(t));
1✔
6320
      if (d != NULL) {
1✔
6321
         diag_printf(d, "cannot declare a file object in a pure function");
1✔
6322
         diag_emit(d);
1✔
6323
      }
6324
   }
6325

6326
   return true;
6327
}
6328

6329
static bool sem_check_new(tree_t t, nametab_t *tab)
542✔
6330
{
6331
   // Rules for allocators are in LRM 93 section 7.3.6
6332

6333
   tree_t value = tree_value(t);
542✔
6334
   type_t access_type = tree_type(t);
542✔
6335

6336
   if (type_is_none(access_type))
542✔
6337
      return false;
6338

6339
   assert(type_is_access(access_type));
541✔
6340
   assert(tree_kind(value) == T_QUALIFIED);
541✔
6341

6342
   if (!sem_check(value, tab))
541✔
6343
      return false;
6344

6345
   type_t type = tree_type(value);
541✔
6346

6347
   if (type_is_none(type))
541✔
6348
      return false;
6349

6350
   if (!sem_check_subtype(value, type, tab))
538✔
6351
      return false;
6352

6353
   const bool has_initial = tree_has_value(value);
538✔
6354

6355
   if (!has_initial && type_is_unconstrained(type))
538✔
6356
      sem_error(t, "unconstrained array type %s not allowed in allocator "
1✔
6357
                "expression", type_pp(type));
6358
   else if (!sem_check_incomplete(t, type))
537✔
6359
      return false;
6360
   else if (type_is_protected(type)) {
536✔
6361
      if (has_initial)
11✔
6362
         sem_error(t, "protected type %s cannot have initial value",
1✔
6363
                   type_pp(type));
6364
      else if (standard() >= STD_19)
10✔
6365
         tree_set_global_flags(t, TREE_GF_INSTANCE_NAME | TREE_GF_PATH_NAME);
10✔
6366
   }
6367

6368
   type_t designated = type_designated(access_type);
535✔
6369

6370
   if (!type_eq(type, designated))
535✔
6371
      sem_error(value, "type of allocator expresion %s does not match "
1✔
6372
                "access type %s", type_pp(type), type_pp(designated));
6373

6374
   return true;
6375
}
6376

6377
static bool sem_check_all(tree_t t, nametab_t *tab)
2,351✔
6378
{
6379
   tree_t value = tree_value(t);
2,351✔
6380
   if (!sem_check(value, tab))
2,351✔
6381
      return false;
6382

6383
   if (!sem_check_name_prefix(value, tab, "a selected name"))
2,349✔
6384
      return false;
6385

6386
   type_t value_type = tree_type(value);
2,349✔
6387

6388
   if (type_is_none(value_type))
2,349✔
6389
      return false;
6390

6391
   if (!sem_check_readable(value))
2,349✔
6392
      return false;
6393

6394
   if (!type_is_access(value_type)) {
2,348✔
6395
      diag_t *d = diag_new(DIAG_ERROR, tree_loc(value));
2✔
6396
      diag_printf(d, "prefix of a selected name with suffix ALL must "
2✔
6397
                  "have access type");
6398
      diag_hint(d, tree_loc(value), "prefix has type %s", type_pp(value_type));
2✔
6399
      diag_lrm(d, STD_08, "8.3");
2✔
6400
      diag_emit(d);
2✔
6401
      return false;
2✔
6402
   }
6403

6404
   return true;
6405
}
6406

6407
static bool sem_check_binding(tree_t t, nametab_t *tab)
203✔
6408
{
6409
   if (!tree_has_ref(t))
203✔
6410
      return false;
6411

6412
   tree_t unit = primary_unit_of(tree_ref(t));
199✔
6413
   if (tree_kind(unit) == T_ENTITY) {
199✔
6414
      if (tree_genmaps(t) > 0 && !sem_check_generic_map(t, unit, tab))
199✔
6415
         return false;
6416

6417
      if (tree_params(t) > 0 && !sem_check_port_map(t, unit, tab))
199✔
UNCOV
6418
         return false;
×
6419
   }
6420

6421
   return true;
6422
}
6423

6424
static bool sem_check_block_config(tree_t t, nametab_t *tab)
168✔
6425
{
6426
   return true;
168✔
6427
}
6428

6429
static bool sem_check_spec(tree_t t, nametab_t *tab)
216✔
6430
{
6431
   if (!tree_has_ref(t))
216✔
6432
      return false;
6433

6434
   tree_t comp = tree_ref(t);
212✔
6435
   assert(tree_kind(comp) == T_COMPONENT);
212✔
6436

6437
   if (!tree_has_value(t))
212✔
6438
      return true;
6439

6440
   tree_t bind = tree_value(t);
203✔
6441
   assert(tree_kind(bind) == T_BINDING);
203✔
6442

6443
   if (!sem_check(bind, tab))
203✔
6444
      return false;
6445

6446
   tree_t unit = tree_ref(bind);
199✔
6447
   tree_t entity = primary_unit_of(unit);
199✔
6448
   assert(tree_kind(entity) == T_ENTITY);
199✔
6449

6450
   bool ok = true;
199✔
6451

6452
   if (tree_genmaps(bind) == 0) {
199✔
6453
      const int c_ngenerics = tree_generics(comp);
145✔
6454
      const int e_ngenerics = tree_generics(entity);
145✔
6455

6456
      bit_mask_t have;
145✔
6457
      mask_init(&have, e_ngenerics);
145✔
6458

6459
      bool have_named = false;
145✔
6460
      for (int i = 0; i < c_ngenerics; i++) {
327✔
6461
         tree_t cg = tree_generic(comp, i);
182✔
6462

6463
         int epos = 0;
182✔
6464
         tree_t match = NULL;
182✔
6465
         for (; epos < e_ngenerics; epos++) {
2,440✔
6466
            tree_t eg = tree_generic(entity, epos);
2,256✔
6467
            if (ident_casecmp(tree_ident(eg), tree_ident(cg))) {
2,256✔
6468
               match = eg;
180✔
6469
               mask_set(&have, epos);
180✔
6470
               break;
6471
            }
6472
         }
6473

6474
         if (match == NULL) {
182✔
6475
            diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
2✔
6476
            diag_printf(d, "generic %s in component %s has no corresponding "
2✔
6477
                        "generic in entity %s", istr(tree_ident(cg)),
6478
                        istr(tree_ident(comp)), istr(tree_ident(entity)));
6479
            diag_hint(d, tree_loc(cg), "generic %s declared here",
2✔
6480
                      istr(tree_ident(cg)));
6481
            diag_emit(d);
2✔
6482

6483
            ok = false;
2✔
6484
            continue;
2✔
6485
         }
6486

6487
         tree_t value;
180✔
6488
         if (tree_class(cg) == C_PACKAGE) {
180✔
6489
            tree_t pmap = tree_value(cg);
8✔
6490
            assert(tree_kind(pmap) == T_PACKAGE_MAP);
8✔
6491

6492
            tree_t expect = tree_value(match);
8✔
6493
            assert(tree_kind(expect) == T_PACKAGE_MAP);
8✔
6494

6495
            if (tree_ref(pmap) != tree_ref(expect)) {
8✔
6496
               diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
1✔
6497
               diag_printf(d, "generic package %s in component %s does not "
1✔
6498
                           "match entity %s", istr(tree_ident(cg)),
6499
                           istr(tree_ident(comp)), istr(tree_ident(entity)));
6500
               diag_hint(d, tree_loc(cg), "declaration of generic %s in "
1✔
6501
                         "component as instance of %s", istr(tree_ident(cg)),
6502
                         istr(tree_ident(pmap)));
6503
               diag_hint(d, tree_loc(match), "declaration of generic %s in "
1✔
6504
                         "entity as instance of %s", istr(tree_ident(match)),
6505
                         istr(tree_ident(expect)));
6506
               diag_emit(d);
1✔
6507

6508
               ok = false;
1✔
6509
               continue;
1✔
6510
            }
6511

6512
            value = tree_new(T_REF);
7✔
6513
            tree_set_ident(value, tree_ident(cg));
7✔
6514
            tree_set_ref(value, cg);
7✔
6515
         }
6516
         else {
6517
            type_t ctype = tree_type(cg);
172✔
6518
            type_t etype = tree_type(match);
172✔
6519
            if (!type_eq(ctype, etype)) {
172✔
6520
               diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
1✔
6521
               diag_printf(d, "generic %s in component %s has type %s which "
1✔
6522
                           "is incompatible with type %s in entity %s",
6523
                           istr(tree_ident(cg)), istr(tree_ident(comp)),
6524
                           type_pp2(ctype, etype), type_pp2(etype, ctype),
6525
                           istr(tree_ident(entity)));
6526
               diag_hint(d, tree_loc(cg), "declaration of generic %s in "
1✔
6527
                         "component", istr(tree_ident(cg)));
6528
               diag_hint(d, tree_loc(match), "declaration of generic %s in "
1✔
6529
                         "entity", istr(tree_ident(match)));
6530
               diag_emit(d);
1✔
6531

6532
               ok = false;
1✔
6533
               continue;
1✔
6534
            }
6535

6536
            value = make_ref(cg);
171✔
6537
         }
6538

6539
         tree_t map = tree_new(T_PARAM);
178✔
6540
         tree_set_loc(map, tree_loc(t));
178✔
6541
         tree_set_value(map, value);
178✔
6542

6543
         if (!have_named && epos == i) {
178✔
6544
            tree_set_subkind(map, P_POS);
99✔
6545
            tree_set_pos(map, epos);
99✔
6546
         }
6547
         else {
6548
            tree_set_subkind(map, P_NAMED);
79✔
6549
            tree_set_name(map, make_ref(match));
79✔
6550
            have_named = true;
79✔
6551
         }
6552

6553
         tree_add_genmap(bind, map);
178✔
6554
      }
6555

6556
      for (int i = 0; i < e_ngenerics; i++) {
356✔
6557
         tree_t eg = tree_generic(entity, i);
211✔
6558
         if (!mask_test(&have, i) && !tree_has_value(eg)) {
211✔
6559
            diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
3✔
6560
            diag_printf(d, "generic %s in entity %s without a default value "
3✔
6561
                        "has no corresponding generic in component %s",
6562
                        istr(tree_ident(eg)),  istr(tree_ident(entity)),
6563
                        istr(tree_ident(comp)));
6564
            diag_hint(d, tree_loc(eg), "generic %s declared here",
3✔
6565
                      istr(tree_ident(eg)));
6566
            diag_emit(d);
3✔
6567

6568
            ok = false;
3✔
6569
            continue;
3✔
6570
         }
6571
      }
6572

6573
      mask_free(&have);
145✔
6574
   }
6575

6576
   if (tree_params(bind) == 0) {
199✔
6577
      const int c_nports = tree_ports(comp);
138✔
6578
      const int e_nports = tree_ports(entity);
138✔
6579

6580
      bit_mask_t have;
138✔
6581
      mask_init(&have, e_nports);
138✔
6582

6583
      bool have_named = false;
138✔
6584
      for (int i = 0; i < c_nports; i++) {
985✔
6585
         tree_t cp = tree_port(comp, i);
847✔
6586

6587
         int epos = 0;
847✔
6588
         tree_t match = NULL;
847✔
6589
         for (; match == NULL && epos < e_nports; epos++) {
8,094✔
6590
            tree_t ep = tree_port(entity, epos);
7,242✔
6591
            if (ident_casecmp(tree_ident(ep), tree_ident(cp))) {
7,242✔
6592
               match = ep;
842✔
6593
               mask_set(&have, epos);
842✔
6594
               break;
6595
            }
6596
         }
6597

6598
         if (match == NULL) {
847✔
6599
            diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
5✔
6600
            diag_printf(d, "port %s in component %s has no corresponding port "
5✔
6601
                        "in entity %s", istr(tree_ident(cp)),
6602
                        istr(tree_ident(comp)), istr(tree_ident(entity)));
6603
            diag_hint(d, tree_loc(cp), "port %s declared here",
5✔
6604
                      istr(tree_ident(cp)));
6605
            diag_emit(d);
5✔
6606

6607
            ok = false;
5✔
6608
            continue;
5✔
6609
         }
6610

6611
         type_t ctype = tree_type(cp);
842✔
6612
         type_t etype = tree_type(match);
842✔
6613
         if (!type_eq(ctype, etype)) {
842✔
6614
            diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
1✔
6615
            diag_printf(d, "port %s in component %s has type %s which is "
1✔
6616
                        "incompatible with type %s in entity %s",
6617
                        istr(tree_ident(cp)), istr(tree_ident(comp)),
6618
                        type_pp2(ctype, etype), type_pp2(etype, ctype),
6619
                        istr(tree_ident(entity)));
6620
            diag_hint(d, tree_loc(cp), "declaration of port %s in component",
1✔
6621
                      istr(tree_ident(cp)));
6622
            diag_hint(d, tree_loc(match), "declaration of port %s in entity",
1✔
6623
                      istr(tree_ident(match)));
6624
            diag_emit(d);
1✔
6625

6626
            ok = false;
1✔
6627
            continue;
1✔
6628
         }
6629

6630
         if (!have_named && epos == i)
841✔
6631
            add_param(bind, make_ref(cp), P_POS, NULL);
823✔
6632
         else {
6633
            add_param(bind, make_ref(cp), P_NAMED, make_ref(match));
18✔
6634
            have_named = true;
18✔
6635
         }
6636
      }
6637

6638
      for (int i = 0; i < e_nports; i++) {
987✔
6639
         if (mask_test(&have, i))
849✔
6640
            continue;
842✔
6641

6642
         tree_t ep = tree_port(entity, i);
7✔
6643

6644
         const bool open_ok =
14✔
6645
            tree_has_value(ep)
7✔
6646
            || (tree_subkind(ep) == PORT_OUT
7✔
6647
                && !type_is_unconstrained(tree_type(ep)));
1✔
6648

6649
         if (!open_ok) {
7✔
6650
            diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
2✔
6651
            diag_printf(d, "port %s in entity %s without a default value "
2✔
6652
                        "has no corresponding port in component %s",
6653
                        istr(tree_ident(ep)), istr(tree_ident(entity)),
6654
                        istr(tree_ident(comp)));
6655
            diag_hint(d, tree_loc(ep), "port %s declared here",
2✔
6656
                      istr(tree_ident(ep)));
6657
            diag_emit(d);
2✔
6658

6659
            ok = false;
2✔
6660
            continue;
2✔
6661
         }
6662
      }
6663
   }
6664

6665
   return ok;
6666
}
6667

6668
static bool sem_check_configuration(tree_t t, nametab_t *tab)
116✔
6669
{
6670
   if (!tree_has_primary(t))
116✔
6671
      return false;   // Was parse error
6672

6673
   tree_t of = tree_primary(t);
115✔
6674

6675
   // LRM 08 section 3.4.1: for a configuration of a given design
6676
   // entity, both the configuration declaration and the corresponding
6677
   // entity declaration shall reside in the same library
6678
   ident_t elib = ident_until(tree_ident(of), '.');
115✔
6679
   if (standard() < STD_19 && elib != lib_name(lib_work())) {
115✔
6680
      diag_t *d = pedantic_diag(tree_loc(t));
1✔
6681
      if (d != NULL) {
1✔
6682
         ident_t ename = ident_rfrom(tree_ident(of), '.');
1✔
6683
         diag_printf(d, "configuration declaration %s must reside in the "
1✔
6684
                     "same library as entity %s", istr(tree_ident(t)),
6685
                     istr(ename));
6686
         diag_hint(d, NULL, "entity %s is in library %s which is not the same "
1✔
6687
                   "as the current working library %s",
6688
                   istr(ename), istr(elib), istr(lib_name(lib_work())));
6689
         diag_lrm(d, STD_08, "3.4");
1✔
6690
         diag_emit(d);
1✔
6691
         return false;
1✔
6692
      }
6693
   }
6694

6695
   return true;
6696
}
6697

6698
static bool sem_check_prot_body(tree_t t, nametab_t *tab)
208✔
6699
{
6700
   // Rules for protected type bodies are in LRM 00 section 3.5.2
6701

6702
   type_t type = tree_type(t);
208✔
6703
   if (type_is_none(type))
208✔
6704
      return false;
6705

6706
   if (!sem_check_missing_bodies(t, tab))
202✔
UNCOV
6707
      return false;
×
6708

6709
   return true;
6710
}
6711

6712
static bool sem_check_implicit_signal(tree_t t, nametab_t *tab)
27✔
6713
{
6714
   tree_t value = tree_value(t);
27✔
6715
   type_t type  = tree_type(t);
27✔
6716

6717
   if (!sem_check(value, tab))
27✔
6718
      return false;
6719

6720
   switch (tree_subkind(t)) {
27✔
6721
   case IMPLICIT_GUARD:
27✔
6722
      if (!sem_check_type(value, type, tab))
27✔
6723
         sem_error(value, "guard expression must have type %s but "
1✔
6724
                   "found %s", type_pp2(type, tree_type(value)),
6725
                   type_pp2(tree_type(value), type));
6726
      break;
6727
   }
6728

6729
   return true;
6730
}
6731

6732
static bool sem_check_context_decl(tree_t t, nametab_t *tab)
19✔
6733
{
6734
   // Context declarations are in LRM 08 section 13.3
6735

6736
   if (!sem_check_context_clause(t, tab))
19✔
6737
      return false;
1✔
6738

6739
   return true;
6740
}
6741

6742
static bool sem_check_context_ref(tree_t t, nametab_t *tab)
21✔
6743
{
6744
   if (standard() >= STD_08) {
21✔
6745
      tree_t unit = find_enclosing(tab, S_DESIGN_UNIT);
21✔
6746

6747
      if (unit != NULL && tree_kind(unit) == T_CONTEXT) {
21✔
6748
         // LRM 08 section 13.3
6749
         ident_t prefix = ident_until(tree_ident(t), '.');
6✔
6750
         if (prefix == well_known(W_WORK))
6✔
6751
            sem_error(t, "selected name in context declaration context "
1✔
6752
                      "reference may not have WORK as a prefix");
6753
      }
6754
   }
6755

6756
   return true;
6757
}
6758

6759
static bool sem_check_disconnect(tree_t t, nametab_t *tab)
14✔
6760
{
6761
   if (!tree_has_ref(t))
14✔
6762
      return false;
6763

6764
   tree_t decl = tree_ref(t);
14✔
6765
   if (class_of(decl) != C_SIGNAL || !is_guarded_signal(decl))
14✔
6766
      sem_error(t, "signal name %s in disconnection specification must denote "
3✔
6767
                "a guarded signal", istr(tree_ident(t)));
6768

6769
   type_t type = tree_type(t);
11✔
6770
   if (!type_eq(tree_type(decl), type))
11✔
6771
      sem_error(t, "type of declared signal %s does not match type %s in "
1✔
6772
                "disconnection specification", type_pp(tree_type(decl)),
6773
                type_pp(type));
6774

6775
   tree_t delay = tree_delay(t);
10✔
6776
   type_t std_time = std_type(NULL, STD_TIME);
10✔
6777
   if (!sem_check_type(delay, std_time, tab))
10✔
6778
      sem_error(delay, "time expression in disconnection specification must "
1✔
6779
                "have type %s but found %s", type_pp(std_time),
6780
                type_pp(tree_type(delay)));
6781

6782
   if (!sem_globally_static(delay))
9✔
6783
      sem_error(delay, "time expression in disconnection specificiation "
1✔
6784
                "must be static");
6785

6786
   return true;
6787
}
6788

6789
static bool sem_check_conv_func(tree_t t, nametab_t *tab)
189✔
6790
{
6791
   if (type_is_none(tree_type(t)))
189✔
6792
      return false;
6793
   else if (!tree_has_ref(t))
189✔
6794
      return false;
6795

6796
   tree_t value = tree_value(t);
189✔
6797
   if (!sem_check(value, tab))
189✔
6798
      return false;
6799

6800
   tree_t decl = tree_ref(t);
189✔
6801
   assert(tree_ports(decl) == 1);
189✔
6802

6803
   tree_t formal = tree_port(decl, 0);
189✔
6804
   type_t ftype = tree_type(formal);
189✔
6805
   if (!sem_check_type(value, ftype, tab))
189✔
6806
      sem_error(value, "type of conversion function actual %s does not match "
2✔
6807
                "formal %s type %s", type_pp2(tree_type(value), ftype),
6808
                istr(tree_ident(formal)), type_pp2(ftype, tree_type(value)));
6809

6810
   return true;
6811
}
6812

6813
static bool sem_check_concurrent(tree_t t, nametab_t *tab)
3,348✔
6814
{
6815
   if (tree_stmts(t) == 0)
3,348✔
6816
      return false;   // Was parse error
6817

6818
   return sem_check(tree_stmt(t, 0), tab);
3,348✔
6819
}
6820

6821
static bool sem_check_external_name(tree_t t, nametab_t *tab)
183✔
6822
{
6823
   const int nparts = tree_parts(t);
183✔
6824
   for (int i = 0; i < nparts; i++) {
825✔
6825
      tree_t pe = tree_part(t, i);
644✔
6826
      switch (tree_subkind(pe)) {
644✔
6827
      case PE_GENERATE:
30✔
6828
         {
6829
            tree_t value = tree_value(pe);
30✔
6830
            if (!sem_globally_static(value))
30✔
6831
               sem_error(value, "generate index must be a static expression");
1✔
6832
         }
6833
         break;
6834
      case PE_RELATIVE:
133✔
6835
         // Relative pathnames must have enclosing concurrent region
6836
         if (find_enclosing(tab, S_CONCURRENT_BLOCK) == NULL) {
133✔
6837
            diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
1✔
6838
            diag_printf(d, "relative pathname has no enclosing "
1✔
6839
                        "concurrent region");
6840
            diag_lrm(d, STD_08, "8.7");
1✔
6841
            diag_emit(d);
1✔
6842
            return false;
1✔
6843
         }
6844
      }
6845
   }
6846

6847
   const class_t class = tree_class(t);
181✔
6848
   if (class != C_CONSTANT) {
181✔
6849
      tree_t sub = find_enclosing(tab, S_SUBPROGRAM);
146✔
6850

6851
      const bool is_pure_func =
292✔
6852
         sub != NULL
6853
         && tree_kind(sub) == T_FUNC_BODY
8✔
6854
         && !(tree_flags(sub) & TREE_F_IMPURE);
147✔
6855

6856
      if (is_pure_func)
146✔
6857
         sem_error(t, "cannot reference external name with class %s in pure "
1✔
6858
                   "function %s", class_str(class), istr(tree_ident(sub)));
6859
   }
6860

6861
   // Cannot do any more checking until elaboration
6862
   return true;
6863
}
6864

6865
static port_mode_t sem_default_force_mode(tree_t target)
87✔
6866
{
6867
   // Rules for default force mode in LRM 08 section 10.5.2.1
6868

6869
   tree_t ref = name_to_ref(target);
87✔
6870
   if (ref == NULL || !tree_has_ref(ref))
87✔
6871
      return PORT_IN;
14✔
6872

6873
   tree_t decl = tree_ref(ref);
73✔
6874
   const tree_kind_t dkind = tree_kind(decl);
73✔
6875
   if (dkind == T_PORT_DECL || dkind == T_PARAM_DECL) {
73✔
6876
      switch (tree_subkind(decl)) {
6✔
6877
      case PORT_OUT:
6878
      case PORT_INOUT:
6879
      case PORT_BUFFER:
6880
         return PORT_OUT;
UNCOV
6881
      default:
×
UNCOV
6882
         return PORT_IN;
×
6883
      }
6884
   }
6885

6886
   return PORT_IN;
6887
}
6888

6889
static bool sem_check_force_target(tree_t target, port_mode_t mode,
98✔
6890
                                   const char *what)
6891
{
6892
   tree_t decl = sem_check_lvalue(target);
98✔
6893
   if (decl == NULL)
98✔
6894
      sem_error(target, "target of simple %s assignment must be a "
1✔
6895
                "signal name", what);
6896

6897
   switch (tree_kind(decl)) {
97✔
6898
   case T_SIGNAL_DECL:
6899
      break;
6900

6901
   case T_PORT_DECL:
9✔
6902
   case T_PARAM_DECL:
6903
      if (tree_class(decl) != C_SIGNAL) {
9✔
UNCOV
6904
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(target));
×
UNCOV
6905
         diag_printf(d, "%s is not a valid target of simple %s assignment",
×
6906
                     what, istr(tree_ident(decl)));
UNCOV
6907
         diag_hint(d, tree_loc(target), "target of simple %s assignment", what);
×
UNCOV
6908
         diag_hint(d, tree_loc(decl), "declared with class %s",
×
6909
                   class_str(tree_class(decl)));
UNCOV
6910
         diag_emit(d);
×
UNCOV
6911
         return false;
×
6912
      }
6913
      else if (mode == PORT_OUT && tree_subkind(decl) == PORT_IN)
9✔
6914
         sem_error(target, "force mode OUT may not be used with target "
1✔
6915
                   "of mode IN");
6916
      break;
6917

6918
      case T_EXTERNAL_NAME:
32✔
6919
         if (tree_class(decl) != C_SIGNAL) {
32✔
6920
            tree_t tail = tree_part(decl, tree_parts(decl) - 1);
1✔
6921
            diag_t *d = diag_new(DIAG_ERROR, tree_loc(target));
1✔
6922
            diag_printf(d, "external name %s is not a valid target of "
1✔
6923
                        "simple %s assignment", istr(tree_ident(tail)), what);
6924
            diag_hint(d, tree_loc(target), "target of signal assignment");
1✔
6925
            diag_hint(d, tree_loc(decl), "declared with class %s",
1✔
6926
                      class_str(tree_class(decl)));
6927
            diag_emit(d);
1✔
6928
            return false;
1✔
6929
         }
6930
         break;
6931

6932
   case T_VAR_DECL:
2✔
6933
   case T_CONST_DECL:
6934
      {
6935
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(target));
2✔
6936
         diag_printf(d, "%s %s is not a valid target of simple %s assignment",
2✔
6937
                     class_str(class_of(decl)), istr(tree_ident(decl)), what);
6938
         diag_hint(d, tree_loc(target), "target of simple %s assignment", what);
2✔
6939
         diag_hint(d, tree_loc(decl), "declared as %s",
2✔
6940
                   class_str(class_of(decl)));
6941
         diag_emit(d);
2✔
6942
         return false;
2✔
6943
      }
6944

UNCOV
6945
   default:
×
UNCOV
6946
      sem_error(target, "invalid target of simple %s assignment", what);
×
6947
   }
6948

6949
   return true;
6950
}
6951

6952
static bool sem_check_force(tree_t t, nametab_t *tab)
64✔
6953
{
6954
   tree_t target = tree_target(t);
64✔
6955

6956
   if (!sem_check(target, tab))
64✔
6957
      return false;
6958

6959
   port_mode_t mode = tree_subkind(t);
63✔
6960
   if (mode == PORT_INVALID)
63✔
6961
      tree_set_subkind(t, (mode = sem_default_force_mode(target)));
54✔
6962

6963
   if (!sem_check_force_target(target, mode, "force"))
63✔
6964
      return false;
6965

6966
   tree_t value = tree_value(t);
60✔
6967
   if (!sem_check(value, tab))
60✔
6968
      return false;
6969

6970
   type_t expect = tree_type(target);
60✔
6971

6972
   if (!sem_check_type(value, expect, tab))
60✔
6973
      sem_error(t, "type of force expression %s does not match type of "
2✔
6974
                "target %s", type_pp2(tree_type(value), expect),
6975
                type_pp2(expect, tree_type(value)));
6976

6977
   return true;
6978
}
6979

6980
static bool sem_check_release(tree_t t, nametab_t *tab)
36✔
6981
{
6982
   tree_t target = tree_target(t);
36✔
6983

6984
   if (!sem_check(target, tab))
36✔
6985
      return false;
6986

6987
   port_mode_t mode = tree_subkind(t);
35✔
6988
   if (mode == PORT_INVALID)
35✔
6989
      tree_set_subkind(t, (mode = sem_default_force_mode(target)));
33✔
6990

6991
   if (!sem_check_force_target(target, mode, "release"))
35✔
6992
      return false;
2✔
6993

6994
   return true;
6995
}
6996

6997
static bool sem_check_prot_ref(tree_t t, nametab_t *tab)
15✔
6998
{
6999
   if (standard() >= STD_19)
15✔
7000
      return true;   // Alias of private variable method
7001

7002
   // There are no legal ways this can appear here and should always
7003
   // have been converted to a call
7004
   assert(error_count() > 0);
1✔
7005

7006
   return false;
7007
}
7008

7009
static bool sem_check_view_decl(tree_t t, nametab_t *tab)
64✔
7010
{
7011
   type_t type = tree_type(t);
64✔
7012
   if (type_is_none(type))
64✔
7013
      return false;
7014

7015
   assert(type_kind(type) == T_VIEW);
64✔
7016

7017
   type_t rtype = type_designated(type);
64✔
7018
   if (!type_is_record(rtype)) {
64✔
7019
      assert(error_count() > 0);   // Checked by parser
1✔
7020
      return false;
7021
   }
7022
   else if (type_is_resolved(rtype))
63✔
7023
      sem_error(t, "subtype indication of a mode view declaration "
1✔
7024
                "must denote an unresolved record type");
7025

7026
   const int nfields = type_fields(rtype);
62✔
7027

7028
   LOCAL_BIT_MASK have;
124✔
7029
   mask_init(&have, nfields);
62✔
7030

7031
   const int nelems = type_fields(type);
62✔
7032
   for (int i = 0; i < nelems; i++) {
192✔
7033
      tree_t e = type_field(type, i);
137✔
7034
      assert(tree_kind(e) == T_VIEW_ELEMENT);
137✔
7035

7036
      if (!tree_has_ref(e))
137✔
7037
         return false;
7038

7039
      tree_t f = tree_ref(e);
135✔
7040
      assert(tree_kind(f) == T_FIELD_DECL);
135✔
7041

7042
      const int pos = tree_pos(f);
135✔
7043
      if (mask_test(&have, pos))
135✔
7044
         sem_error(e, "duplicate mode view element definition for field %s",
1✔
7045
                   istr(tree_ident(e)));
7046

7047
      mask_set(&have, pos);
134✔
7048

7049
      switch (tree_subkind(e)) {
134✔
7050
      case PORT_LINKAGE:
1✔
7051
         sem_error(e, "element mode indication cannot have mode LINKAGE");
1✔
7052

7053
      case PORT_RECORD_VIEW:
14✔
7054
      case PORT_ARRAY_VIEW:
7055
         {
7056
            tree_t name = tree_value(e);
14✔
7057
            type_t type = tree_type(e);
14✔
7058
            type_t view_type = tree_type(name);
14✔
7059

7060
            if (type_is_none(view_type))
14✔
7061
               return false;
7062

7063
            if (type_kind(view_type) != T_VIEW)
14✔
7064
               sem_error(name, "name in element mode view indication of field "
1✔
7065
                         "%s does not denote a mode view", istr(tree_ident(f)));
7066

7067
            type_t elem_type = type;
13✔
7068
            if (tree_subkind(e) == PORT_ARRAY_VIEW) {
13✔
7069
               if (!type_is_array(type))
2✔
7070
                  sem_error(e, "field %s with array mode view indication has "
1✔
7071
                            "non-array type %s", istr(tree_ident(f)),
7072
                            type_pp(type));
7073

7074
               elem_type = type_elem(type);
1✔
7075
            }
7076

7077
            if (!type_eq(elem_type, type_designated(view_type)))
12✔
7078
               sem_error(e, "field %s subtype %s is not compatible with mode "
1✔
7079
                         "view %s", istr(tree_ident(f)), type_pp(elem_type),
7080
                         type_pp(view_type));
7081
         }
7082
         break;
7083
      }
7084
   }
7085

7086
   if (mask_popcount(&have) != nfields) {
55✔
7087
      LOCAL_TEXT_BUF tb = tb_new();
1✔
7088
      for (int i = 0, missing = 0; i < nfields; i++) {
5✔
7089
         if (!mask_test(&have, i))
4✔
7090
            tb_printf(tb, "%s%s", missing++ > 0 ? ", " : "",
3✔
7091
                      istr(tree_ident(type_field(rtype, i))));
7092
      }
7093

7094
      sem_error(t, "missing mode view element defintion for %s", tb_get(tb));
1✔
7095
   }
7096

7097
   return true;
7098
}
7099

7100
static bool sem_check_cond_value(tree_t t, nametab_t *tab)
50✔
7101
{
7102
   type_t type = tree_type(t);
50✔
7103
   if (type_is_none(type))
50✔
7104
      return false;
7105

7106
   type_t std_bool = std_type(NULL, STD_BOOLEAN);
50✔
7107

7108
   const int nconds = tree_conds(t);
50✔
7109
   for (int i = 0; i < nconds; i++) {
162✔
7110
      tree_t cond = tree_cond(t, i);
116✔
7111
      assert(tree_kind(cond) == T_COND_EXPR);
116✔
7112

7113
      if (tree_has_value(cond)) {
116✔
7114
         tree_t value = tree_value(cond);
72✔
7115
         if (!sem_check(value, tab))
72✔
7116
            return false;
7117

7118
         if (!sem_check_type(value, std_bool, tab))
72✔
7119
            sem_error(value, "type of condition must be %s but have %s",
2✔
7120
                      type_pp(std_bool), type_pp(tree_type(value)));
7121
      }
7122
      else
7123
         assert(i == nconds - 1);
44✔
7124

7125
      if (tree_has_result(cond)) {
114✔
7126
         tree_t result = tree_result(cond);
104✔
7127
         if (!sem_check(result, tab))
104✔
7128
            return false;
7129

7130
         if (!sem_check_type(result, type, tab))
104✔
7131
            sem_error(result, "expected type of conditional expression to be "
114✔
7132
                      "%s but is %s", type_pp(type),
7133
                      type_pp(tree_type(result)));
7134
      }
7135
   }
7136

7137
   return true;
7138
}
7139

7140
static bool sem_check_prot_decl(tree_t t, nametab_t *tab)
212✔
7141
{
7142
   const int ndecls = tree_decls(t);
212✔
7143
   for (int i = 0; i < ndecls; i++) {
687✔
7144
      tree_t d = tree_decl(t, i);
476✔
7145
      if (tree_kind(d) == T_ALIAS) {
476✔
7146
         // LRM 19 section 5.6.2: it is an error if an alias declared
7147
         // within a protected type declaration denotes anything other
7148
         // than a method of a protected type
7149
         tree_t value = tree_value(d);
15✔
7150
         if (tree_kind(value) != T_PROT_REF)
15✔
7151
            sem_error(d, "an alias declared within a protected type "
476✔
7152
                      "declaration must denote a protected type method");
7153
      }
7154
   }
7155

7156
   defer_check(tab, sem_missing_body_cb, t);
211✔
7157
   return true;
211✔
7158
}
7159

7160
static bool sem_check_inertial(tree_t t, nametab_t *tab)
15✔
7161
{
7162
   if (!sem_check(tree_value(t), tab))
15✔
UNCOV
7163
      return false;
×
7164

7165
   return true;
7166
}
7167

7168
static bool sem_check_psl_union(tree_t t, nametab_t *tab)
25✔
7169
{
7170
   psl_check(tree_psl(t), tab);
25✔
7171
   return true;
25✔
7172
}
7173

7174
bool sem_check(tree_t t, nametab_t *tab)
685,527✔
7175
{
7176
   switch (tree_kind(t)) {
685,527✔
7177
   case T_ARCH:
5,015✔
7178
      return sem_check_arch(t, tab);
5,015✔
7179
   case T_PACKAGE:
1,463✔
7180
      return sem_check_package(t, tab);
1,463✔
7181
   case T_ENTITY:
5,002✔
7182
      return sem_check_entity(t, tab);
5,002✔
7183
   case T_TYPE_DECL:
4,859✔
7184
      return sem_check_type_decl(t, tab);
4,859✔
7185
   case T_SUBTYPE_DECL:
1,451✔
7186
      return sem_check_subtype_decl(t, tab);
1,451✔
7187
   case T_PORT_DECL:
3,713✔
7188
      return sem_check_port_decl(t, tab);
3,713✔
7189
   case T_PARAM_DECL:
30,956✔
7190
      return sem_check_param_decl(t, tab);
30,956✔
7191
   case T_GENERIC_DECL:
2,288✔
7192
      return sem_check_generic_decl(t, tab);
2,288✔
7193
   case T_SIGNAL_DECL:
6,262✔
7194
      return sem_check_signal_decl(t, tab);
6,262✔
7195
   case T_VAR_DECL:
11,321✔
7196
      return sem_check_var_decl(t, tab);
11,321✔
7197
   case T_CONST_DECL:
6,726✔
7198
      return sem_check_const_decl(t, tab);
6,726✔
7199
   case T_PROCESS:
4,820✔
7200
      return sem_check_process(t, tab);
4,820✔
7201
   case T_VAR_ASSIGN:
16,500✔
7202
      return sem_check_var_assign(t, tab);
16,500✔
7203
   case T_SIGNAL_ASSIGN:
5,253✔
7204
      return sem_check_signal_assign(t, tab);
5,253✔
7205
   case T_FCALL:
83,538✔
7206
   case T_PROT_FCALL:
7207
      return sem_check_fcall(t, tab);
83,538✔
7208
   case T_LITERAL:
91,510✔
7209
      return sem_check_literal(t);
91,510✔
7210
   case T_STRING:
25,283✔
7211
      return sem_check_string_literal(t);
25,283✔
7212
   case T_REF:
175,119✔
7213
      return sem_check_ref(t, tab);
175,119✔
7214
   case T_WAIT:
9,489✔
7215
      return sem_check_wait(t, tab);
9,489✔
7216
   case T_ASSERT:
17,598✔
7217
      return sem_check_assert(t, tab);
17,598✔
7218
   case T_REPORT:
2,021✔
7219
      return sem_check_report(t, tab);
2,021✔
7220
   case T_QUALIFIED:
4,261✔
7221
      return sem_check_qualified(t, tab);
4,261✔
7222
   case T_FUNC_DECL:
6,000✔
7223
      return sem_check_func_decl(t, tab);
6,000✔
7224
   case T_AGGREGATE:
8,924✔
7225
      return sem_check_aggregate(t, tab);
8,924✔
7226
   case T_ATTR_REF:
20,985✔
7227
      return sem_check_attr_ref(t, false, tab);
20,985✔
7228
   case T_ARRAY_REF:
14,556✔
7229
      return sem_check_array_ref(t, tab);
14,556✔
7230
   case T_ARRAY_SLICE:
2,194✔
7231
      return sem_check_array_slice(t, tab);
2,194✔
7232
   case T_INSTANCE:
1,487✔
7233
      return sem_check_instance(t, tab);
1,487✔
7234
   case T_IF:
8,688✔
7235
      return sem_check_if(t, tab);
8,688✔
7236
   case T_NULL:
7237
      return true;
7238
   case T_PACK_BODY:
778✔
7239
      return sem_check_pack_body(t, tab);
778✔
7240
   case T_FUNC_BODY:
7,362✔
7241
      return sem_check_func_body(t, tab);
7,362✔
7242
   case T_RETURN:
13,036✔
7243
      return sem_check_return(t, tab);
13,036✔
7244
   case T_COND_RETURN:
7✔
7245
      return sem_check_cond_return(t, tab);
7✔
7246
   case T_COND_ASSIGN:
2,170✔
7247
      return sem_check_cond_assign(t, tab);
2,170✔
7248
   case T_WHILE:
201✔
7249
      return sem_check_while(t, tab);
201✔
7250
   case T_ALIAS:
1,563✔
7251
      return sem_check_alias(t, tab);
1,563✔
7252
   case T_FOR:
2,553✔
7253
      return sem_check_for(t, tab);
2,553✔
7254
   case T_PROC_DECL:
1,085✔
7255
      return sem_check_proc_decl(t, tab);
1,085✔
7256
   case T_PROC_BODY:
2,148✔
7257
      return sem_check_proc_body(t, tab);
2,148✔
7258
   case T_BLOCK:
390✔
7259
      return sem_check_block(t, tab);
390✔
7260
   case T_CASE:
558✔
7261
   case T_SELECT:
7262
      return sem_check_case(t, tab);
558✔
7263
   case T_EXIT:
412✔
7264
   case T_NEXT:
7265
      return sem_check_loop_control(t, tab);
412✔
7266
   case T_PCALL:
7,974✔
7267
   case T_PROT_PCALL:
7268
      return sem_check_pcall(t, tab);
7,974✔
7269
   case T_ATTR_SPEC:
342✔
7270
      return sem_check_attr_spec(t, tab);
342✔
7271
   case T_ATTR_DECL:
118✔
7272
      return sem_check_attr_decl(t);
118✔
7273
   case T_COMPONENT:
315✔
7274
      return sem_check_component(t, tab);
315✔
7275
   case T_IF_GENERATE:
167✔
7276
      return sem_check_if_generate(t, tab);
167✔
7277
   case T_FOR_GENERATE:
164✔
7278
      return sem_check_for_generate(t, tab);
164✔
7279
   case T_CASE_GENERATE:
10✔
7280
      return sem_check_case(t, tab);
10✔
7281
   case T_OPEN:
75✔
7282
      return sem_check_open(t);
75✔
7283
   case T_FIELD_DECL:
3,705✔
7284
      return sem_check_field_decl(t);
3,705✔
7285
   case T_FILE_DECL:
154✔
7286
      return sem_check_file_decl(t, tab);
154✔
7287
   case T_NEW:
542✔
7288
      return sem_check_new(t, tab);
542✔
7289
   case T_ALL:
2,351✔
7290
      return sem_check_all(t, tab);
2,351✔
7291
   case T_RECORD_REF:
7,788✔
7292
      return sem_check_record_ref(t, tab);
7,788✔
7293
   case T_UNIT_DECL:
157✔
7294
      return sem_check_unit_decl(t);
157✔
7295
   case T_USE:
15,093✔
7296
      return sem_check_use_clause(t, tab);
15,093✔
7297
   case T_TYPE_CONV:
5,372✔
7298
      return sem_check_conversion(t, tab);
5,372✔
7299
   case T_SPEC:
216✔
7300
      return sem_check_spec(t, tab);
216✔
7301
   case T_BINDING:
203✔
7302
      return sem_check_binding(t, tab);
203✔
7303
   case T_LIBRARY:
25,513✔
7304
      return sem_check_library_clause(t, tab);
25,513✔
7305
   case T_CONFIGURATION:
116✔
7306
      return sem_check_configuration(t, tab);
116✔
7307
   case T_PROT_BODY:
208✔
7308
      return sem_check_prot_body(t, tab);
208✔
7309
   case T_CONTEXT:
19✔
7310
      return sem_check_context_decl(t, tab);
19✔
7311
   case T_CONTEXT_REF:
21✔
7312
      return sem_check_context_ref(t, tab);
21✔
7313
   case T_BLOCK_CONFIG:
168✔
7314
      return sem_check_block_config(t, tab);
168✔
7315
   case T_IMPLICIT_SIGNAL:
27✔
7316
      return sem_check_implicit_signal(t, tab);
27✔
7317
   case T_DISCONNECT:
14✔
7318
      return sem_check_disconnect(t, tab);
14✔
7319
   case T_GROUP:
7320
   case T_GROUP_TEMPLATE:
7321
   case T_BOX:
7322
   case T_PSL_DIRECT:
7323
   case T_PSL_DECL:
7324
   case T_PSL_FCALL:
7325
   case T_LOOP:
7326
   case T_SEQUENCE:
7327
      return true;
7328
   case T_PSL_UNION:
25✔
7329
      return sem_check_psl_union(t, tab);
25✔
7330
   case T_CONV_FUNC:
189✔
7331
      return sem_check_conv_func(t, tab);
189✔
7332
   case T_CONCURRENT:
3,348✔
7333
      return sem_check_concurrent(t, tab);
3,348✔
7334
   case T_PACK_INST:
278✔
7335
      return sem_check_pack_inst(t, tab);
278✔
7336
   case T_EXTERNAL_NAME:
183✔
7337
      return sem_check_external_name(t, tab);
183✔
7338
   case T_FORCE:
64✔
7339
      return sem_check_force(t, tab);
64✔
7340
   case T_RELEASE:
36✔
7341
      return sem_check_release(t, tab);
36✔
7342
   case T_PROT_REF:
15✔
7343
      return sem_check_prot_ref(t, tab);
15✔
7344
   case T_MATCH_CASE:
43✔
7345
   case T_MATCH_SELECT:
7346
      return sem_check_match_case(t, tab);
43✔
7347
   case T_FUNC_INST:
129✔
7348
   case T_PROC_INST:
7349
      return sem_check_subprogram_inst(t, tab);
129✔
7350
   case T_VIEW_DECL:
64✔
7351
      return sem_check_view_decl(t, tab);
64✔
7352
   case T_COND_VALUE:
50✔
7353
      return sem_check_cond_value(t, tab);
50✔
7354
   case T_PROT_DECL:
212✔
7355
      return sem_check_prot_decl(t, tab);
212✔
7356
   case T_INERTIAL:
15✔
7357
      return sem_check_inertial(t, tab);
15✔
UNCOV
7358
   default:
×
UNCOV
7359
      sem_error(t, "cannot check %s", tree_kind_str(tree_kind(t)));
×
7360
   }
7361
}
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

© 2026 Coveralls, Inc