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

nickg / nvc / 6903463504

17 Nov 2023 11:35AM UTC coverage: 91.168% (+0.006%) from 91.162%
6903463504

push

github

nickg
Improve handling of implicit literal conversions

329 of 343 new or added lines in 7 files covered. (95.92%)

759 existing lines in 8 files now uncovered.

50580 of 55480 relevant lines covered (91.17%)

608081.41 hits per line

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

96.93
/src/sem.c
1
//
2
//  Copyright (C) 2011-2023  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 "lib.h"
22
#include "mask.h"
23
#include "names.h"
24
#include "option.h"
25
#include "phase.h"
26
#include "type.h"
27

28
#include <assert.h>
29
#include <stdlib.h>
30
#include <string.h>
31

32
typedef bool (*static_fn_t)(tree_t t);
33

34
typedef struct {
35
   tree_t decl;
36
   bool   have;
37
   bool   partial;
38
} formal_map_t;
39

40
static bool sem_check_array_ref(tree_t t, nametab_t *tab);
41
static bool sem_locally_static(tree_t t);
42
static bool sem_globally_static(tree_t t);
43
static tree_t sem_check_lvalue(tree_t t);
44
static bool sem_check_same_type(tree_t left, tree_t right);
45
static bool sem_check_type(tree_t t, type_t expect);
46
static bool sem_static_name(tree_t t, static_fn_t check_fn);
47
static bool sem_check_attr_ref(tree_t t, bool allow_range, nametab_t *tab);
48
static bool sem_check_generic_map(tree_t t, tree_t unit, nametab_t *tab);
49
static bool sem_check_port_map(tree_t t, tree_t unit, nametab_t *tab);
50
static bool sem_check_subtype(tree_t decl, type_t type, nametab_t *tab);
51

52
#define sem_error(t, ...) do {                        \
53
      error_at(t ? tree_loc(t) : NULL , __VA_ARGS__); \
54
      return false;                                   \
55
   } while (0)
56

57
#define pedantic_diag(t) ({                            \
58
         static int _warned = 0;                       \
59
         _pedantic_diag(tree_loc(t), &_warned, NULL);  \
60
      })
61

62
static diag_t *_pedantic_diag(const loc_t *loc, int *warned, bool *error)
50✔
63
{
64
   const bool relaxed = opt_get_int(OPT_RELAXED);
50✔
65
   if (!relaxed || !*warned) {
50✔
66
      const diag_level_t level = relaxed ? DIAG_WARN : DIAG_ERROR;
43✔
67
      diag_t *d = diag_new(level, loc);
43✔
68
      if (level == DIAG_ERROR)
43✔
69
         diag_hint(d, NULL, "the $bold$--relaxed$$ option downgrades this "
21✔
70
                   "to a warning");
71

72
      *warned = 1;
43✔
73
      if (error) *error = !relaxed;
43✔
74
      return d;
43✔
75
   }
76
   else
77
      return NULL;
78
}
79

80
static tree_t sem_int_lit(type_t type, int64_t i)
82✔
81
{
82
   tree_t f = tree_new(T_LITERAL);
82✔
83
   tree_set_subkind(f, L_INT);
82✔
84
   tree_set_ival(f, i);
82✔
85
   tree_set_type(f, type);
82✔
86

87
   return f;
82✔
88
}
89

90
static bool sem_check_resolution(type_t type, tree_t res)
328✔
91
{
92
   // Resolution functions are described in LRM 93 section 2.4
93

94
   if (tree_kind(res) == T_AGGREGATE) {
357✔
95
      // VHDL-2008 element resolution
96
      assert(standard() >= STD_08);
30✔
97

98
      if (type_is_array(type)) {
30✔
99
         tree_t sub = tree_value(tree_assoc(res, 0));
29✔
100
         return sem_check_resolution(type_elem(type), sub);
29✔
101
      }
102
      else if (type_is_record(type))
1✔
103
         sem_error(res, "sorry, record element resolution functions are not"
×
104
                   "supported yet");
105
      else {
106
         // Should have been caught during name resolution
107
         assert(error_count() > 0);
1✔
108
         return false;
109
      }
110
   }
111

112
   assert(tree_kind(res) == T_REF);
327✔
113

114
   if (!tree_has_ref(res))
327✔
115
      return false;
116

117
   tree_t fdecl = tree_ref(res);
324✔
118
   assert(is_subprogram(fdecl));
324✔
119

120
   type_t ftype = tree_type(fdecl);
324✔
121

122
   if (type_kind(ftype) != T_FUNC)
324✔
123
      sem_error(res, "resolution function name %s is not a function",
1✔
124
                istr(tree_ident(res)));
125

126
   // Must take a single parameter of array of base type
127

128
   if (type_params(ftype) != 1)
323✔
129
      sem_error(res, "resolution function must have a single argument");
1✔
130

131
   type_t param = type_param(ftype, 0);
322✔
132
   if (type_kind(param) != T_ARRAY)
322✔
133
      sem_error(res, "parameter of resolution function must be "
1✔
134
                "an unconstrained array type");
135

136
   if (!type_eq(type_elem(param), type))
321✔
137
      sem_error(res, "parameter of resolution function must be "
1✔
138
                "an array of %s but found %s", type_pp(type),
139
                type_pp(type_elem(param)));
140

141
   // Return type must be the resolved type
142

143
   if (!type_eq(type_result(ftype), type))
320✔
144
      sem_error(res, "result of resolution function must be %s but have %s",
1✔
145
                type_pp(type), type_pp(type_result(ftype)));
146

147
   return true;
148
}
149

150
static bool sem_check_range(tree_t r, type_t expect, type_kind_t kind,
19,053✔
151
                            nametab_t *tab)
152
{
153
   if (expect != NULL && type_is_none(expect))
19,053✔
154
      return false;   // Prevent cascading errors
155

156
   switch (tree_subkind(r)) {
19,033✔
157
   case RANGE_EXPR:
4,298✔
158
      {
159
         tree_t expr = tree_value(r);
4,298✔
160

161
         if (tree_kind(expr) != T_ATTR_REF)
4,298✔
162
            sem_error(expr, "invalid expression in range constraint");
1✔
163

164
         if (!sem_check_attr_ref(expr, true, tab))
4,297✔
165
            return false;
166

167
         if (expect && !sem_check_type(expr, expect))
4,291✔
168
            sem_error(expr, "expected type of range bound to be %s but is %s",
2✔
169
                      type_pp(expect), type_pp(tree_type(expr)));
170

171
         if (kind != T_LAST_TYPE_KIND) {
4,289✔
172
            assert(kind == T_INTEGER || kind == T_REAL);
1✔
173
            if (type_base_kind(tree_type(expr)) != kind)
1✔
174
               sem_error(expr, "type of range bounds must be of some %s type "
1✔
175
                         "but have %s", kind == T_INTEGER ? "integer" : "real",
176
                         type_pp(tree_type(expr)));
177
         }
178
      }
179
      break;
180

181
   case RANGE_TO:
14,734✔
182
   case RANGE_DOWNTO:
183
      {
184
         tree_t left = tree_left(r);
14,734✔
185
         if (!sem_check(left, tab))
14,734✔
186
            return false;
187

188
         tree_t right = tree_right(r);
14,733✔
189
         if (!sem_check(right, tab))
14,733✔
190
            return false;
191

192
         if (expect != NULL) {
14,730✔
193
            if (!sem_check_same_type(left, right))
14,559✔
194
               sem_error(right, "type mismatch in range: left is %s,"
3✔
195
                         " right is %s", type_pp(tree_type(left)),
196
                         type_pp(tree_type(right)));
197

198
            if (!sem_check_type(left, expect))
14,556✔
199
               sem_error(r, "expected type of range bounds to be %s but"
4✔
200
                         " have %s", type_pp(expect), type_pp(tree_type(left)));
201

202
            // This cannot fail because we know left and right have the
203
            // same type and left is equal to expect, but we still need
204
            // to call sem_check_type for the implicit conversion
205
            sem_check_type(right, expect);
14,552✔
206
            sem_check_type(r, expect);
14,552✔
207
         }
208

209
         if (kind != T_LAST_TYPE_KIND) {
14,723✔
210
            // See LRM 93 section 3.1.2: Each bound of a range
211
            // constraint that must be of some integer type, but the two
212
            // bounds need not have the same integer type.
213
            assert(kind == T_INTEGER || kind == T_REAL);
171✔
214
            if (type_base_kind(tree_type(left)) != kind)
171✔
215
               sem_error(left, "type of left bound must be of some %s type "
×
216
                         "but have %s", kind == T_INTEGER ? "integer" : "real",
217
                         type_pp(tree_type(left)));
218
            else if (type_base_kind(tree_type(right)) != kind)
171✔
219
               sem_error(right, "type of right bound must be of some %s type "
4✔
220
                         "but have %s", kind == T_INTEGER ? "integer" : "real",
221
                         type_pp(tree_type(right)));
222
         }
223
      }
224
      break;
225
   }
226

227
   return true;
228
}
229

230
static bool sem_check_discrete_range(tree_t r, type_t expect, nametab_t *tab)
16,041✔
231
{
232
   if (!sem_check_range(r, expect ?: tree_type(r), T_LAST_TYPE_KIND, tab))
16,041✔
233
      return false;
234

235
   const range_kind_t kind = tree_subkind(r);
16,005✔
236
   type_t type = tree_type(r);
16,005✔
237
   if (type_is_none(type) || kind == RANGE_ERROR)
16,005✔
238
      return false;
239

240
   if (!type_is_discrete(type))
16,004✔
241
      sem_error(r, "type of range bounds %s is not discrete", type_pp(type));
1✔
242

243
   // See LRM 93 section 3.2.1.1: universal integer bound must be a
244
   // numeric literal or attribute. Later LRMs relax the wording here.
245
   if (standard() < STD_00 && !relaxed_rules() && kind != RANGE_EXPR) {
16,003✔
246
      tree_t left  = tree_left(r);
6,749✔
247
      tree_t right = tree_right(r);
6,749✔
248

249
      type_t left_type  = tree_type(left);
6,749✔
250
      type_t right_type = tree_type(right);
6,749✔
251

252
      if (type_is_universal(left_type) && type_is_universal(right_type)) {
6,749✔
253
         tree_kind_t lkind = tree_kind(left);
1✔
254
         tree_kind_t rkind = tree_kind(right);
1✔
255

256
         const bool invalid =
2✔
257
            lkind != T_LITERAL && lkind != T_ATTR_REF
1✔
258
            && rkind != T_LITERAL && rkind != T_ATTR_REF;
1✔
259

260
         if (invalid)
1✔
261
            sem_error(r, "universal integer bound must be numeric"
×
262
                      " literal or attribute");
263
      }
264
   }
265

266
   return true;
267
}
268

269
static bool sem_check_constraint(tree_t constraint, type_t base, nametab_t *tab)
13,664✔
270
{
271
   if (base != NULL && type_is_access(base))
13,664✔
272
      base = type_designated(base);
2✔
273

274
   const constraint_kind_t consk = tree_subkind(constraint);
13,664✔
275
   switch (consk) {
13,664✔
276
   case C_RANGE:
2,841✔
277
      if (!type_is_scalar(base))
2,841✔
278
         sem_error(constraint, "range constraint cannot be used with "
2✔
279
                   "non-scalar type %s", type_pp(base));
280
      break;
281

282
   case C_INDEX:
10,540✔
283
      if (!type_is_array(base))
10,540✔
284
         sem_error(constraint, "index constraint cannot be used with "
4✔
285
                   "non-array type %s", type_pp(base));
286
      break;
287

288
   case C_OPEN:
29✔
289
      if (!type_is_array(base))
29✔
290
         sem_error(constraint, "array constraint cannot be used with "
1✔
291
                   "non-array type %s", type_pp(base));
292
      return true;
293

294
   case C_RECORD:
254✔
295
      {
296
         if (!type_is_record(base))
254✔
297
            sem_error(constraint, "record element constraint cannot be used "
×
298
                      "with non-record type %s", type_pp(base));
299

300
         // Range list is overloaded to hold record element constraints
301
         const int nelem = tree_ranges(constraint);
254✔
302
         for (int i = 0; i < nelem; i++) {
697✔
303
            tree_t ei = tree_range(constraint, i);
449✔
304
            assert(tree_kind(ei) == T_ELEM_CONSTRAINT);
449✔
305

306
            if (!tree_has_ref(ei))
449✔
307
               return false;   // Was parse error
308

309
            tree_t decl = tree_ref(ei);
446✔
310
            assert(tree_kind(decl) == T_FIELD_DECL);  // Checked by parser
446✔
311

312
            type_t ftype = tree_type(decl);
446✔
313
            if (!type_is_unconstrained(ftype))
446✔
314
               sem_error(constraint, "field %s in record element constraint is "
1✔
315
                         "already constrained", istr(tree_ident(decl)));
316

317
            type_t sub = tree_type(ei);
445✔
318
            if (!sem_check_subtype(decl, sub, tab))
445✔
319
               return false;
320

321
            // Check for duplicate element constraints
322
            tree_t fi = tree_ref(ei);
444✔
323
            for (int j = 0; j < i; j++) {
809✔
324
               tree_t ej = tree_range(constraint, j);
366✔
325
               if (tree_pos(tree_ref(ej)) == tree_pos(fi))
366✔
326
                  sem_error(ei, "duplicate record element constraint for "
366✔
327
                            "field %s", istr(tree_ident(fi)));
328
            }
329

330
            if (type_kind(base) == T_SUBTYPE) {
443✔
331
               tree_t dup = type_constraint_for_field(base, fi);
3✔
332
               if (dup != NULL && !type_is_unconstrained(tree_type(dup))) {
3✔
333
                  diag_t *d = diag_new(DIAG_ERROR, tree_loc(ei));
×
334
                  diag_printf(d, "duplicate record element constraint for "
×
335
                              "field %s", istr(tree_ident(fi)));
336
                  diag_hint(d, tree_loc(dup), "constraint in subtype %s",
×
337
                            type_pp(base));
338
                  diag_hint(d, tree_loc(ei), "duplicate constraint here");
×
339
                  diag_emit(d);
×
340
                  return false;
×
341
               }
342
            }
343
         }
344

345
         // Code belows handles index and range constraints
346
         return true;
347
      }
348
   }
349

350
   if (type_is_array(base)) {
13,375✔
351
      if (type_kind(base) == T_SUBTYPE && !type_is_unconstrained(base))
10,536✔
352
         sem_error(constraint, "cannot change constraints of constrained "
5✔
353
                   "array type %s", type_pp(base));
354
   }
355
   else if (type_is_record(base) && standard() < STD_08)
2,839✔
356
      sem_error(constraint, "record subtype may not have constraints "
×
357
                "in VHDL-%s", standard_text(standard()));
358

359
   const int ndims_base = type_is_array(base) ? dimension_of(base) : 1;
13,370✔
360
   const int ndims = tree_ranges(constraint);
13,370✔
361

362
   if (ndims != ndims_base)
13,370✔
363
      sem_error(constraint, "expected %d constraints for type %s but found %d",
1✔
364
                ndims_base, type_pp(base), ndims);
365

366
   for (int i = 0; i < ndims; i++) {
27,040✔
367
      tree_t r = tree_range(constraint, i);
13,682✔
368
      type_t index = index_type_of(base, i);
13,682✔
369

370
      switch (consk) {
13,682✔
371
      case C_INDEX:
10,843✔
372
         if (!sem_check_discrete_range(r, index, tab))
10,843✔
373
            return false;
374
         break;
375

376
      case C_RANGE:
2,839✔
377
         if (!sem_check_range(r, index, T_LAST_TYPE_KIND, tab))
2,839✔
378
            return false;
379
         break;
380

381
      default:
382
         break;
383
      }
384
   }
385

386
   return true;
387
}
388

389
static bool sem_check_subtype_helper(tree_t decl, type_t type, nametab_t *tab)
14,091✔
390
{
391
   // Shared code for checking subtype declarations and implicit subtypes
392

393
   type_t base = type_base(type);
14,091✔
394
   if (type_is_none(base))
14,091✔
395
      return false;
396

397
   if (type_is_protected(base))
14,085✔
398
      sem_error(decl, "subtypes may not have protected base types");
1✔
399

400
   type_t elem = base;
14,084✔
401
   const int ncon = type_constraints(type);
14,084✔
402
   for (int i = 0; i < ncon; i++) {
27,718✔
403
      tree_t cons = type_constraint(type, i);
13,664✔
404
      if (!sem_check_constraint(cons, elem, tab))
13,664✔
405
         return false;
406

407
      const constraint_kind_t consk = tree_subkind(cons);
13,634✔
408
      if (i + 1 < ncon && (consk == C_INDEX || consk == C_OPEN))
13,634✔
409
         elem = type_elem(elem);
×
410
   }
411

412
   if (type_is_array(type) && type_has_elem(type)) {
14,054✔
413
      type_t elem = type_elem(type);
146✔
414
      if (type_kind(elem) == T_SUBTYPE && !type_has_ident(elem)) {
146✔
415
         // Anonymous subtype created for array element constraint
416
         assert(standard() >= STD_08);
146✔
417

418
         if (!sem_check_subtype_helper(decl, elem, tab))
146✔
419
            return false;
420
      }
421
   }
422

423
   if (type_has_resolution(type)) {
14,052✔
424
      if (!sem_check_resolution(type_base(type), type_resolution(type)))
328✔
425
         return false;
9✔
426
   }
427

428
   return true;
429
}
430

431
static bool sem_check_subtype(tree_t decl, type_t type, nametab_t *tab)
65,231✔
432
{
433
   // Check an anonymous subtype at the point of use
434

435
   if (type_kind(type) != T_SUBTYPE)
65,231✔
436
      return true;
437
   else if (type_has_ident(type))
23,912✔
438
      return true;   // Explicitly declared subtype
439

440
   return sem_check_subtype_helper(decl, type, tab);
12,008✔
441
}
442

443
static bool sem_check_use_clause(tree_t c, nametab_t *tab)
13,139✔
444
{
445
   if (standard() >= STD_08) {
13,139✔
446
      tree_t unit = find_enclosing(tab, S_DESIGN_UNIT);
3,685✔
447

448
      if (unit != NULL && tree_kind(unit) == T_CONTEXT) {
3,685✔
449
         // LRM 08 section 13.3
450
         ident_t prefix = ident_until(tree_ident(c), '.');
29✔
451
         if (prefix == well_known(W_WORK))
29✔
452
            sem_error(c, "selected name in context declaration use clause "
1✔
453
                      "may not have WORK as a prefix");
454
      }
455
   }
456

457
   return true;
458
}
459

460
static bool sem_check_library_clause(tree_t t, nametab_t *tab)
22,091✔
461
{
462
   if (standard() >= STD_08) {
22,091✔
463
      ident_t name = tree_ident(t);
5,838✔
464
      tree_t unit = find_enclosing(tab, S_DESIGN_UNIT);
5,838✔
465

466
      if (unit != NULL && tree_kind(unit) == T_CONTEXT) {
5,838✔
467
         // LRM 08 section 13.3
468
         if (name == well_known(W_WORK))
10✔
469
            sem_error(t, "library clause in a context declaration may not have "
1✔
470
                      "logical library name WORK");
471
      }
472
   }
473

474
   return true;
475
}
476

477
static bool sem_check_context_clause(tree_t t, nametab_t *tab)
10,627✔
478
{
479
   // Ignore the implicit WORK and STD with context declarations
480
   const int ignore = tree_kind(t) == T_CONTEXT ? 2 : 0;
10,627✔
481

482
   bool ok = true;
10,627✔
483
   const int ncontexts = tree_contexts(t);
10,627✔
484
   for (int n = ignore; n < ncontexts; n++)
45,869✔
485
      ok = sem_check(tree_context(t, n), tab) && ok;
35,245✔
486

487
   return ok;
10,627✔
488
}
489

490
static bool sem_readable(tree_t t)
185,110✔
491
{
492
   switch (tree_kind(t)) {
193,733✔
493
   case T_REF:
73,373✔
494
      {
495
         if (tree_flags(t) & TREE_F_FORMAL_NAME)
73,373✔
496
            return true;   // Name appearing in formal
497

498
         tree_t decl = tree_ref(t);
73,373✔
499
         switch (tree_kind(decl)) {
73,373✔
500
         case T_PORT_DECL:
1,364✔
501
            {
502
               const port_mode_t mode = tree_subkind(decl);
1,364✔
503
               if (mode == PORT_OUT && standard() < STD_08) {
1,364✔
504
                  diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
2✔
505
                  diag_printf(d, "cannot read output port %s",
2✔
506
                              istr(tree_ident(t)));
507
                  diag_hint(d, tree_loc(decl), "%s declared with mode OUT",
2✔
508
                            istr(tree_ident(decl)));
509
                  diag_hint(d, tree_loc(t), "read here");
2✔
510
                  diag_hint(d, NULL, "outputs can be read with "
2✔
511
                            "$bold$--std=2008$$");
512
                  diag_emit(d);
2✔
513
                  return false;
2✔
514
               }
515
               else if (mode == PORT_LINKAGE)
1,362✔
516
                  sem_error(t, "linkage port %s may not be read except as "
1✔
517
                            "an actual corresponding to an interface of mode "
518
                            "linkage", istr(tree_ident(t)));
519
            }
520
            break;
521

522
         case T_PARAM_DECL:
20,932✔
523
            if (tree_subkind(decl) == PORT_OUT && standard() < STD_08)
20,932✔
524
               sem_error(t, "cannot read OUT parameter %s",
2✔
525
                         istr(tree_ident(t)));
526
            break;
527

528
         default:
529
            break;
530
         }
531

532
         return true;
533
      }
534

535
   case T_ARRAY_REF:
8,623✔
536
   case T_ARRAY_SLICE:
537
      return sem_readable(tree_value(t));
8,623✔
538

539
   default:
540
      return true;
541
   }
542
}
543

544
static bool sem_check_array_dims(type_t type, type_t constraint, nametab_t *tab)
265✔
545
{
546
   const int ndims = dimension_of(type);
265✔
547
   for (int i = 0; i < ndims; i++) {
549✔
548
      tree_t r = range_of(type, i);
284✔
549

550
      type_t index_type = NULL;
284✔
551
      if (constraint != NULL && i < dimension_of(constraint))
284✔
552
         index_type = index_type_of(constraint, i);
×
553

554
      if (!sem_check_discrete_range(r, index_type, tab))
284✔
555
         return false;
556

557
      if (index_type == NULL)
284✔
558
         index_type = tree_type(r);
284✔
559

560
      if (!sem_check_type(r, index_type))
284✔
561
         sem_error(r, "type of bound %s does not match type of index %s",
284✔
562
                   type_pp(tree_type(r)),
563
                   type_pp(index_type));
564
   }
565

566
   return true;
567
}
568

569
static bool sem_check_mapped_type(tree_t t, type_t expect, hash_t *map)
356,455✔
570
{
571
   type_t actual = tree_type(t);
356,455✔
572

573
   if (type_eq_map(actual, expect, map))
356,455✔
574
      return true;
575

576
   // Supress cascading errors
577
   if (type_is_none(actual) || type_is_none(expect))
215✔
578
      return true;
5✔
579

580
   return false;
581
}
582

583
static inline bool sem_check_type(tree_t t, type_t expect)
352,635✔
584
{
585
   return sem_check_mapped_type(t, expect, NULL);
352,635✔
586
}
587

588
static bool sem_check_same_type(tree_t left, tree_t right)
30,265✔
589
{
590
   type_t left_type  = tree_type(left);
30,265✔
591
   type_t right_type = tree_type(right);
30,265✔
592

593
   if (type_eq(left_type, right_type))
30,265✔
594
      return true;
595

596
   // Supress cascading errors
597
   if (type_is_none(left_type) || type_is_none(right_type))
18✔
598
      return true;
4✔
599

600
   return false;
601
}
602

603
static bool sem_has_access(type_t t)
60,572✔
604
{
605
   // returns true if the type is an access type or is a composite
606
   // type that contains a subelement of an access type
607
   type_t base = type_base_recur(t);
88,227✔
608

609
   if (type_is_access(base))
88,227✔
610
      return true;
611

612
   if (type_is_array(base))
87,443✔
613
      return sem_has_access(type_elem(base));
27,655✔
614

615
   if (type_is_record(base)) {
59,788✔
616
      const int nfields = type_fields(base);
3,878✔
617
      for (int i = 0; i < nfields; i++) {
19,452✔
618
         if (sem_has_access(tree_type(type_field(base, i))))
15,648✔
619
            return true;
620
      }
621
   }
622

623
   return false;
624
}
625

626
static bool sem_check_type_decl(tree_t t, nametab_t *tab)
4,243✔
627
{
628
   type_t type = tree_type(t);
4,243✔
629

630
   // Nothing more to do for incomplete types
631
   if (type_kind(type) == T_INCOMPLETE)
4,243✔
632
      return true;
633

634
   type_kind_t kind = type_kind(type);
4,202✔
635

636
   if (kind == T_SUBTYPE) {
4,202✔
637
      // Implicitly created subtype for a constrained array defintion
638
      if (!sem_check_subtype_helper(t, type, tab)) {
644✔
639
         // Prevent cascading errors
640
         // TODO: can we do this check in the parser and set T_NONE earlier?
641
         type_set_base(type, type_new(T_NONE));
4✔
642
         return false;
4✔
643
      }
644

645
      type = type_base(type);
640✔
646
      kind = type_kind(type);
640✔
647
      assert(kind == T_ARRAY);
640✔
648
   }
649

650
   switch (kind) {
4,198✔
651
   case T_ARRAY:
2,190✔
652
      {
653
         type_t elem_type = type_elem(type);
2,190✔
654
         if (!sem_check_subtype(t, elem_type, tab))
2,190✔
655
            return false;
656

657
         if (standard() < STD_08 && type_is_unconstrained(elem_type)) {
2,190✔
658
            diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
1✔
659
            diag_printf(d, "array %s cannot have unconstrained element type",
1✔
660
                        istr(tree_ident(t)));
661
            diag_hint(d, NULL, "this would be allowed with $bold$--std=2008$$");
1✔
662
            diag_emit(d);
1✔
663
            return false;
1✔
664
         }
665

666
         if (type_is_file(elem_type))
2,189✔
667
            sem_error(t, "array %s cannot have element of file type",
1✔
668
                      istr(tree_ident(t)));
669

670
         if (type_is_protected(elem_type))
2,188✔
671
            sem_error(t, "array %s cannot have element of protected type",
1✔
672
                      istr(tree_ident(t)));
673

674
         const int nindex = type_indexes(type);
2,187✔
675
         for (int i = 0; i < nindex; i++) {
4,663✔
676
            type_t index_type = type_index(type, i);
2,480✔
677
            if (type_is_none(index_type))
2,480✔
678
               return false;
679
            else if (!type_is_discrete(index_type))
2,478✔
680
               sem_error(t, "index type %s is not discrete",
2,478✔
681
                         type_pp(index_type));
682
         }
683

684
         return true;
685
      }
686

687
   case T_ENUM:
688
      return true;
689

690
   case T_PHYSICAL:
42✔
691
      {
692
         const int nunits = type_units(type);
42✔
693
         for (int i = 0; i < nunits; i++) {
182✔
694
            tree_t u = type_unit(type, i);
142✔
695
            tree_set_type(u, type);
142✔
696
            if (!sem_check(u, tab))
142✔
697
               return false;
698

699
            tree_t value = tree_value(u);
142✔
700

701
            // LRM 08 section 5.2.4.1: the abstract literal portion
702
            // shall be an integer literal
703
            if (tree_ival(value) == 0 && tree_dval(value) != 0)
142✔
704
               sem_error(value, "the abstract literal portion of a secondary "
1✔
705
                         "unit declaration must be an integer literal");
706

707
            if (i > 0 && !sem_check_type(value, type))
141✔
708
               sem_error(value, "secondary unit %s must have type %s",
141✔
709
                         istr(tree_ident(u)), type_pp(type));
710
         }
711
      }
712

713
      // Fall-through
714
   case T_INTEGER:
715
   case T_REAL:
716
      {
717
         tree_t r = type_dim(type, 0);
173✔
718

719
         const type_kind_t check_kind = kind == T_PHYSICAL ? T_INTEGER : kind;
173✔
720
         if (!sem_check_range(r, NULL, check_kind, tab))
173✔
721
            return false;
722

723
         // Standard specifies type of 'LEFT and 'RIGHT are same
724
         // as the declared type
725
         switch (tree_subkind(r)) {
168✔
726
         case RANGE_TO:
168✔
727
         case RANGE_DOWNTO:
728
            tree_set_type(tree_left(r), type);
168✔
729
            tree_set_type(tree_right(r), type);
168✔
730
            break;
168✔
UNCOV
731
         case RANGE_EXPR:
×
UNCOV
732
            tree_set_type(tree_value(r), type);
×
UNCOV
733
            break;
×
734
         }
735

736
         tree_set_type(r, type);
168✔
737
         return true;
168✔
738
      }
739

740
   case T_RECORD:
1,030✔
741
      {
742
         const int nfields = type_fields(type);
1,030✔
743
         for (int i = 0; i < nfields; i++) {
4,335✔
744
            tree_t f = type_field(type, i);
3,311✔
745

746
            if (!sem_check(f, tab))
3,311✔
747
               return false;
748

749
            // Each field name must be distinct
750
            ident_t f_name = tree_ident(f);
3,311✔
751
            for (int j = 0; j < i; j++) {
11,587✔
752
               tree_t fj = type_field(type, j);
8,277✔
753
               if (f_name == tree_ident(fj)) {
8,277✔
754
                  diag_t *d = diag_new(DIAG_ERROR, tree_loc(f));
1✔
755
                  diag_printf(d, "duplicate field name %s", istr(f_name));
1✔
756
                  diag_hint(d, tree_loc(fj), "previously declared here");
1✔
757
                  diag_hint(d, tree_loc(f), "declared again here");
1✔
758
                  diag_emit(d);
1✔
759
                  return false;
1✔
760
               }
761
            }
762

763
            type_t f_type = tree_type(f);
3,310✔
764

765
            if (!sem_check_subtype(f, f_type, tab))
3,310✔
766
               return false;
767

768
            // Recursive record types are not allowed
769
            if (type_eq(type, f_type))
3,309✔
770
               sem_error(f, "recursive record types are not allowed");
1✔
771

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

791
         return true;
792
      }
793

794
   case T_FILE:
88✔
795
      // Rules for file types are in LRM 93 section 3.4
796
      {
797
         type_t f = type_designated(type);
88✔
798

799
         switch (type_kind(f)) {
88✔
800
         case T_ACCESS:
1✔
801
            sem_error(t, "files may not be of access type");
1✔
802
            break;
1✔
803
         case T_FILE:
1✔
804
            sem_error(t, "files may not be of file type");
1✔
805
            break;
1✔
806
         case T_PROTECTED:
1✔
807
            sem_error(t, "files may not be of protected type");
1✔
808
            break;
85✔
809
         default:
810
            break;
85✔
811
         }
812

813
         if (sem_has_access(f))
85✔
814
            sem_error(t, "type %s has a subelement with an access type",
4✔
815
                      type_pp(f));
816

817
         type_t base_f = type_base_recur(f);
81✔
818
         if (type_is_array(base_f) && dimension_of(base_f) > 1)
81✔
819
            sem_error(t, "array type for file type must be one-dimensional");
2✔
820

821
         return true;
822
      }
823

824
   case T_ACCESS:
305✔
825
      // Rules for access types are in LRM 93 section 3.3
826
      {
827
         type_t designated = type_designated(type);
305✔
828

829
         if (!sem_check_subtype(t, designated, tab))
305✔
830
            return false;
831

832
         if (standard() < STD_19) {
305✔
833
            if (type_is_file(designated))
257✔
834
               sem_error(t, "access type %s cannot designate file type",
1✔
835
                         istr(tree_ident(t)));
836

837
            if (type_is_protected(designated))
256✔
838
               sem_error(t, "access type %s cannot designate protected type",
1✔
839
                         istr(tree_ident(t)));
840
         }
841

842
         return true;
843
      }
844

845
   case T_PROTECTED:
846
   default:
847
      return true;
848
   }
849
}
850

851
static bool sem_check_subtype_decl(tree_t t, nametab_t *tab)
1,293✔
852
{
853
   type_t type = tree_type(t);
1,293✔
854
   assert(type_kind(type) == T_SUBTYPE);
1,293✔
855
   assert(type_has_ident(type));
1,293✔
856

857
   return sem_check_subtype_helper(t, type, tab);
1,293✔
858
}
859

860
static bool sem_no_access_file_or_protected(tree_t t, type_t type, const char *what)
11,055✔
861
{
862
   // constants, signals, attributes, generics, ports
863
   // may not be of an access, file, or protected type, or
864
   // of a composite type with a subelement of an access type
865

866
   if (type_is_access(type))
11,055✔
867
      sem_error(t, "%s may not have access type", what);
4✔
868

869
   if (sem_has_access(type))
11,051✔
870
      sem_error(t, "%s may not have a type with a subelement of access type", what);
6✔
871

872
   if (type_is_protected(type))
11,045✔
873
      sem_error(t, "%s may not have protected type", what);
3✔
874

875
   if (type_is_file(type))
11,042✔
876
      sem_error(t, "%s may not have file type", what);
4✔
877

878
   return true;
879
}
880

881
static void sem_unconstrained_decl_hint(diag_t *d, type_t type)
14✔
882
{
883
   if (!type_is_record(type))
14✔
884
      return;
885

886
   // Tell the user which field is unconstrained
887

888
   type_t base = type_base_recur(type);
5✔
889
   const int nfields = type_fields(base);
5✔
890
   for (int i = 0; i < nfields; i++) {
15✔
891
      tree_t f = type_field(base, i);
10✔
892
      if (!type_is_unconstrained(tree_type(f)))
10✔
893
         continue;
2✔
894
      else if (type_constraint_for_field(type, f) == NULL)
8✔
895
         diag_hint(d, NULL, "missing record element constraint for field %s",
6✔
896
                   istr(tree_ident(f)));
897
   }
898
}
899

900
static bool sem_check_const_decl(tree_t t, nametab_t *tab)
6,177✔
901
{
902
   type_t type = tree_type(t);
6,177✔
903

904
   if (!sem_check_subtype(t, type, tab))
6,177✔
905
      return false;
906
   else if (type_is_none(type))
6,175✔
907
      return false;
908

909
   if (type_is_incomplete(type))
6,159✔
UNCOV
910
      sem_error(t, "type %s is incomplete", type_pp(type));
×
911

912
   if (!sem_no_access_file_or_protected(t, type, "constants"))
6,159✔
913
      return false;
914

915
   tree_t fwd = find_forward_decl(tab, t);
6,154✔
916

917
   if (tree_has_value(t)) {
6,154✔
918
      tree_t value = tree_value(t);
5,757✔
919
      if (!sem_check(value, tab))
5,757✔
920
         return false;
921

922
      if (!sem_check_type(value, type))
5,735✔
923
         sem_error(value, "type of initial value %s does not match type "
5✔
924
                   "of declaration %s", type_pp2(tree_type(value), type),
925
                   type_pp2(type, tree_type(value)));
926

927
      if (fwd == NULL && type_is_unconstrained(type))
5,730✔
928
         tree_set_type(t, tree_type(value));
719✔
929
   }
930
   else if (tree_kind(find_enclosing(tab, S_DESIGN_UNIT)) != T_PACKAGE)
397✔
931
      sem_error(t, "deferred constant declarations are only permitted "
1✔
932
                "in packages");
933

934
   if (fwd != NULL && !type_strict_eq(tree_type(fwd), type)) {
6,126✔
935
      diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
1✔
936
      diag_printf(d, "expected type %s for deferred constant %s but "
1✔
937
                  "found %s", type_pp2(tree_type(fwd), type),
938
                  istr(tree_ident(t)), type_pp2(type, tree_type(fwd)));
939
      diag_hint(d, tree_loc(fwd), "originally declared with type %s",
1✔
940
                type_pp2(tree_type(fwd), type));
941
      diag_hint(d, tree_loc(t), "type here is %s",
1✔
942
                type_pp2(type, tree_type(fwd)));
943
      diag_emit(d);
1✔
944
      return false;
1✔
945
   }
946

947
   return true;
948
}
949

950
static bool sem_check_signal_decl(tree_t t, nametab_t *tab)
4,798✔
951
{
952
   type_t type = tree_type(t);
4,798✔
953

954
   if (!sem_check_subtype(t, type, tab))
4,798✔
955
      return false;
956
   else if (type_is_none(type))
4,791✔
957
      return false;
958

959
   if (type_is_unconstrained(type)) {
4,790✔
960
      if (standard() < STD_19) {
15✔
961
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
6✔
962
         diag_printf(d, "declaration of signal %s cannot have unconstrained "
6✔
963
                     "type %s", istr(tree_ident(t)), type_pp(type));
964
         sem_unconstrained_decl_hint(d, type);
6✔
965
         diag_emit(d);
6✔
966
         return false;
6✔
967
      }
968
      else if (!tree_has_value(t)) {
9✔
969
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
1✔
970
         diag_printf(d, "declaration of signal %s without an initial value "
1✔
971
                     "cannot have unconstrained type %s",
972
                     istr(tree_ident(t)), type_pp(type));
973
         sem_unconstrained_decl_hint(d, type);
1✔
974
         diag_emit(d);
1✔
975
         return false;
1✔
976
      }
977
   }
978
   else if (type_is_incomplete(type))
4,775✔
UNCOV
979
      sem_error(t, "declaration of signal %s cannot have incomplete type %s",
×
980
                istr(tree_ident(t)), type_pp(type));
981

982
   if (!sem_no_access_file_or_protected(t, type, "signals"))
4,783✔
983
      return false;
984

985
   if (is_guarded_signal(t) && !type_is_resolved(type))
4,776✔
986
      sem_error(t, "guarded signal must have resolved subtype");
1✔
987

988
   if (tree_has_value(t)) {
4,775✔
989
      tree_t value = tree_value(t);
1,337✔
990
      if (!sem_check(value, tab))
1,337✔
991
         return false;
992

993
      if (!sem_check_type(value, type))
1,334✔
UNCOV
994
         sem_error(value, "type of initial value %s does not match type "
×
995
                   "of declaration %s", type_pp2(tree_type(value), type),
996
                   type_pp2(type, tree_type(value)));
997

998
      if (standard() >= STD_19 && type_is_unconstrained(type))
1,334✔
999
         tree_set_type(t, tree_type(value));
8✔
1000
   }
1001

1002
   return true;
1003
}
1004

1005
static bool sem_check_var_decl(tree_t t, nametab_t *tab)
10,626✔
1006
{
1007
   type_t type = tree_type(t);
10,626✔
1008

1009
   if (!sem_check_subtype(t, type, tab))
10,626✔
1010
      return false;
1011
   else if (type_is_none(type))
10,623✔
1012
      return false;
1013

1014
   if (type_is_unconstrained(type)) {
10,612✔
1015
      if (standard() < STD_19) {
18✔
1016
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
5✔
1017
         diag_printf(d, "declaration of variable %s cannot have unconstrained "
5✔
1018
                     "type %s", istr(tree_ident(t)), type_pp(type));
1019
         sem_unconstrained_decl_hint(d, type);
5✔
1020
         diag_emit(d);
5✔
1021
         return false;
5✔
1022
      }
1023
      else if (!tree_has_value(t)) {
13✔
1024
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
2✔
1025
         diag_printf(d, "declaration of variable %s without an initial value "
2✔
1026
                     "cannot have unconstrained type %s", istr(tree_ident(t)),
1027
                     type_pp(type));
1028
         sem_unconstrained_decl_hint(d, type);
2✔
1029
         diag_emit(d);
2✔
1030
         return false;
2✔
1031
      }
1032
   }
1033
   else if (type_is_incomplete(type))
10,594✔
1034
      sem_error(t, "declaration of variable %s cannot have incomplete type %s",
1✔
1035
                istr(tree_ident(t)), type_pp(type));
1036

1037
   if (tree_has_value(t)) {
10,604✔
1038
      if (type_kind(type) == T_PROTECTED)
1,882✔
1039
         sem_error(t, "variable %s with protected type may not have an "
1✔
1040
                   "initial value", istr(tree_ident(t)));
1041

1042
      tree_t value = tree_value(t);
1,881✔
1043
      if (!sem_check(value, tab))
1,881✔
1044
         return false;
1045

1046
      if (!sem_check_type(value, type))
1,868✔
1047
         sem_error(value, "type of initial value %s does not match type "
1✔
1048
                   "of declaration %s", type_pp2(tree_type(value), type),
1049
                   type_pp2(type, tree_type(value)));
1050

1051
      if (standard() >= STD_08 && type_is_unconstrained(type))
1,867✔
1052
         tree_set_type(t, tree_type(value));
11✔
1053
   }
1054

1055
   // From VHDL-2000 onwards shared variables must be protected types
1056
   if (standard() >= STD_00) {
10,589✔
1057
      if ((tree_flags(t) & TREE_F_SHARED) && type_kind(type) != T_PROTECTED) {
5,879✔
1058
         diag_t *d = pedantic_diag(t);
7✔
1059
         if (d != NULL) {
7✔
1060
            diag_printf(d, "shared variable %s must have protected type",
7✔
1061
                        istr(tree_ident(t)));
1062
            diag_emit(d);
7✔
1063
         }
1064
      }
1065
   }
1066

1067
   return true;
1068
}
1069

1070
static bool sem_check_param_decl(tree_t t, nametab_t *tab)
29,251✔
1071
{
1072
   type_t type = tree_type(t);
29,251✔
1073

1074
   if (!sem_check_subtype(t, type, tab))
29,251✔
1075
      return false;
1076

1077
   // See LRM 93 section 3.3 for restrictions
1078

1079
   const type_kind_t kind = type_base_kind(type);
29,251✔
1080
   const class_t class = tree_class(t);
29,251✔
1081
   const port_mode_t mode = tree_subkind(t);
29,251✔
1082

1083
   switch (mode) {
29,251✔
1084
   case PORT_BUFFER:
1✔
1085
      sem_error(t, "subprogram formal parameters cannot have mode BUFFER");
1✔
1086
      break;
1✔
1087
   case PORT_LINKAGE:
1✔
1088
      sem_error(t, "subprogram formal parameters cannot have mode LINKAGE");
1✔
1089
      break;
29,249✔
1090
   default:
1091
      break;
29,249✔
1092
   }
1093

1094
   if (kind == T_FILE && class != C_FILE)
29,249✔
1095
      sem_error(t, "formal parameter %s with file type must have class FILE",
1✔
1096
                istr(tree_ident(t)));
1097

1098
   if (kind != T_FILE && class == C_FILE)
29,248✔
1099
      sem_error(t, "formal parameter %s with class FILE must have file type",
1✔
1100
                istr(tree_ident(t)));
1101

1102
   if ((kind == T_ACCESS || kind == T_PROTECTED) && class != C_VARIABLE)
29,247✔
1103
      sem_error(t, "formal parameter %s with %s type must have class VARIABLE",
6✔
1104
                istr(tree_ident(t)),
1105
                kind == T_ACCESS ? "access" : "protected");
1106

1107
   if (sem_has_access(type) && class != C_VARIABLE)
29,242✔
1108
      sem_error(t, "formal parameter %s with type containing an access type "
4✔
1109
                "must have class VARIABLE", istr(tree_ident(t)));
1110

1111
   if (class == C_CONSTANT && mode != PORT_IN)
29,238✔
1112
      sem_error(t, "parameter of class CONSTANT must have mode IN");
2✔
1113

1114
   // LRM 08 section 4.2.2.3
1115
   if (class == C_SIGNAL && tree_flags(t) & TREE_F_BUS)
29,236✔
1116
      sem_error(t, "formal signal parameter declaration may "
1✔
1117
                "not include the reserved word BUS");
1118

1119
   if (mode == PORT_RECORD_VIEW || mode == PORT_ARRAY_VIEW) {
29,235✔
1120
      tree_t name = tree_value(t);
17✔
1121
      type_t view_type = tree_type(name);
17✔
1122

1123
      if (type_is_none(view_type))
17✔
1124
         return false;
1125

1126
      if (type_kind(view_type) != T_VIEW)
17✔
1127
         sem_error(name, "name in mode view indication of parameter %s does "
1✔
1128
                   "not denote a mode view", istr(tree_ident(t)));
1129

1130
      type_t elem_type = type;
16✔
1131
      if (mode == PORT_ARRAY_VIEW) {
16✔
1132
         if (!type_is_array(type))
1✔
1133
            sem_error(t, "parameter %s with array mode view indication has "
1✔
1134
                      "non-array type %s", istr(tree_ident(t)), type_pp(type));
1135

UNCOV
1136
         elem_type = type_elem(type);
×
1137
      }
1138

1139
      if (!type_eq(elem_type, type_designated(view_type)))
15✔
1140
         sem_error(t, "subtype %s is not compatible with mode "
1✔
1141
                   "view %s", type_pp(elem_type), type_pp(view_type));
1142
   }
1143
   else if (tree_has_value(t)) {
29,218✔
1144
      tree_t value = tree_value(t);
4,102✔
1145
      if (!sem_check(value, tab))
4,102✔
1146
         return false;
1147

1148
      if (!sem_check_type(value, type))
4,101✔
1149
         sem_error(value, "type of default value %s does not match type "
2✔
1150
                   "of declaration %s", type_pp(tree_type(value)),
1151
                   type_pp(type));
1152

1153
      switch (class) {
4,099✔
1154
      case C_SIGNAL:
1✔
1155
         sem_error(t, "parameter of class SIGNAL cannot have a "
1✔
1156
                   "default value");
1157
         break;
6✔
1158

1159
      case C_VARIABLE:
6✔
1160
         if (mode == PORT_OUT || mode == PORT_INOUT)
6✔
1161
            sem_error(t, "parameter of class VARIABLE with mode OUT or "
2✔
1162
                      "INOUT cannot have a default value");
1163
         break;
1164

1165
      default:
1166
         break;
1167
      }
1168

1169
      if (!sem_globally_static(value)) {
4,096✔
1170
         diag_t *d = pedantic_diag(value);
8✔
1171
         if (d != NULL) {
8✔
1172
            diag_printf(d, "default value must be a static expression");
5✔
1173
            diag_emit(d);
5✔
1174
         }
1175
      }
1176

1177
      if (kind == T_PROTECTED)
4,096✔
1178
         sem_error(t, "parameter with protected type cannot have "
1✔
1179
                   "a default value");
1180
   }
1181

1182
   return true;
1183
}
1184

1185
static bool sem_check_port_decl(tree_t t, nametab_t *tab)
2,898✔
1186
{
1187
   type_t type = tree_type(t);
2,898✔
1188

1189
   if (type_is_none(type))
2,898✔
1190
      return false;
1191
   else if (!sem_check_subtype(t, type, tab))
2,896✔
1192
      return false;
1193

1194
   if (type_is_unconstrained(type)) {
2,895✔
1195
      // This port needs to be copied and updated to a constrained type
1196
      // during elaboration
1197
      tree_set_flag(t, TREE_F_UNCONSTRAINED);
116✔
1198
   }
1199

1200
   const class_t class = tree_class(t);
2,895✔
1201
   const port_mode_t mode = tree_subkind(t);
2,895✔
1202

1203
   if (class == C_VARIABLE) {
2,895✔
1204
      if (standard() < STD_19) {
7✔
1205
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
1✔
1206
         diag_printf(d, "ports may not have variable class in VHDL-%s",
1✔
1207
                     standard_text(standard()));
1208
         diag_hint(d, NULL, "pass $bold$--std=2019$$ to enable this "
1✔
1209
                   "feature");
1210
         diag_emit(d);
1✔
1211
         return false;
1✔
1212
      }
1213

1214
      if (mode != PORT_INOUT)
6✔
1215
         sem_error(t, "formal variable port %s must have mode INOUT",
1✔
1216
                   istr(tree_ident(t)));
1217

1218
      if (!type_is_protected(type))
5✔
1219
         sem_error(t, "formal variable port %s must have protected type",
1✔
1220
                   istr(tree_ident(t)));
1221
   }
1222
   else if (class != C_SIGNAL)
2,888✔
1223
      sem_error(t, "invalid object class %s for port %s",
2✔
1224
                class_str(class), istr(tree_ident(t)));
1225

1226
   if (type_is_access(type))
2,890✔
1227
      sem_error(t, "port %s cannot be declared with access type %s",
1✔
1228
                istr(tree_ident(t)), type_pp(type));
1229

1230
   if (sem_has_access(type))
2,889✔
1231
      sem_error(t, "port %s cannot be declared with type %s which has a "
2✔
1232
                "subelement of access type", istr(tree_ident(t)),
1233
                type_pp(type));
1234

1235
   if (class != C_VARIABLE && type_is_protected(type)) {
2,887✔
1236
      diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
1✔
1237
      diag_printf(d, "port %s with class %s cannot be declared with "
1✔
1238
                  "protected type %s", istr(tree_ident(t)),
1239
                  class_str(class), type_pp(type));
1240
      diag_hint(d, NULL, "ports with variable class can be of protected "
1✔
1241
                "type in VHDL-2019");
1242
      diag_hint(d, NULL, "pass $bold$--std=2019$$ to enable this "
1✔
1243
                "feature");
1244
      diag_emit(d);
1✔
1245
      return false;
1✔
1246
   }
1247

1248
   if (type_is_file(type))
2,886✔
1249
      sem_error(t, "port %s cannot be declared with file type %s",
1✔
1250
                istr(tree_ident(t)), type_pp(type));
1251

1252
   if (mode == PORT_RECORD_VIEW || mode == PORT_ARRAY_VIEW) {
2,885✔
1253
      tree_t name = tree_value(t);
51✔
1254
      type_t view_type = tree_type(name);
51✔
1255

1256
      if (type_is_none(view_type))
51✔
1257
         return false;
1258

1259
      if (type_kind(view_type) != T_VIEW)
51✔
UNCOV
1260
         sem_error(name, "name in mode view indication of port %s does not "
×
1261
                   "denote a mode view", istr(tree_ident(t)));
1262

1263
      type_t elem_type = type;
51✔
1264
      if (mode == PORT_ARRAY_VIEW) {
51✔
1265
         if (!type_is_array(type))
10✔
1266
            sem_error(t, "port %s with array mode view indication has "
1✔
1267
                      "non-array type %s", istr(tree_ident(t)), type_pp(type));
1268

1269
         elem_type = type_elem(type);
9✔
1270
      }
1271

1272
      if (!type_eq(elem_type, type_designated(view_type)))
50✔
1273
         sem_error(t, "subtype %s is not compatible with mode "
2✔
1274
                   "view %s", type_pp(elem_type), type_pp(view_type));
1275
   }
1276
   else if (tree_has_value(t)) {
2,834✔
1277
      tree_t value = tree_value(t);
154✔
1278
      if (!sem_check(value, tab))
154✔
1279
         return false;
1280

1281
      if (mode == PORT_LINKAGE)
154✔
1282
         sem_error(t, "port with mode LINKAGE cannot have a default value");
1✔
1283

1284
      if (!sem_check_type(value, type))
153✔
UNCOV
1285
         sem_error(value, "type of default value %s does not match type "
×
1286
                   "of declaration %s", type_pp(tree_type(value)),
1287
                   type_pp(type));
1288
   }
1289

1290
   return true;
1291
}
1292

1293
static bool sem_check_generic_decl(tree_t t, nametab_t *tab)
1,588✔
1294
{
1295
   const class_t class = tree_class(t);
1,588✔
1296
   switch (class) {
1,588✔
1297
   case C_CONSTANT:
1298
   case C_TYPE:
1299
   case C_FUNCTION:
1300
   case C_PROCEDURE:
1301
      break;
1,540✔
1302

1303
   case C_PACKAGE:
42✔
1304
      {
1305
         tree_t map = tree_value(t);
42✔
1306
         if (!tree_has_ref(map))
42✔
1307
            return false;   // Was earlier error
1308

1309
         assert(tree_kind(map) == T_PACKAGE_MAP);
41✔
1310

1311
         tree_t pack = tree_ref(map);
41✔
1312
         assert(is_uninstantiated_package(pack));
41✔
1313

1314
         switch (tree_subkind(map)) {
41✔
1315
         case PACKAGE_MAP_DEFAULT:
6✔
1316
            {
1317
               // Check each generic in the uninstantiated package has a
1318
               // default value
1319
               const int ngenerics = tree_generics(pack);
6✔
1320
               for (int i = 0; i < ngenerics; i++) {
18✔
1321
                  tree_t g = tree_generic(pack, i);
13✔
1322
                  if (!tree_has_value(g)) {
13✔
1323
                     diag_t *d = diag_new(DIAG_ERROR, tree_loc(map));
1✔
1324
                     diag_printf(d, "generic %s in package %s does not have a "
1✔
1325
                                 "default value", istr(tree_ident(g)),
1326
                                 istr(tree_ident(pack)));
1327
                     diag_hint(d, tree_loc(g), "%s declared here",
1✔
1328
                               istr(tree_ident(g)));
1329
                     diag_lrm(d, STD_08, "6.5.5");
1✔
1330

1331
                     diag_emit(d);
1✔
1332
                     return false;
1✔
1333
                  }
1334
               }
1335
            }
1336
            break;
1337

1338
         case PACKAGE_MAP_MATCHING:
9✔
1339
            sem_check_generic_map(map, pack, tab);
9✔
1340
            break;
9✔
1341

1342
         case PACKAGE_MAP_BOX:
1343
            break;
1344
         }
1345

1346
         return true;
1347
      }
1348
   default:
6✔
1349
      sem_error(t, "invalid object class %s for generic %s",
6✔
1350
                class_str(tree_class(t)), istr(tree_ident(t)));
1351
   }
1352

1353
   type_t type = tree_type(t);
1,540✔
1354

1355
   if (!sem_check_subtype(t, type, tab))
1,540✔
1356
      return false;
1357
   else if (type_is_none(type))
1,540✔
1358
      return false;
1359

1360
   if (class != C_TYPE) {
1,539✔
1361
      if (type_is_access(type))
1,410✔
1362
         sem_error(t, "generic %s may not have access type",
1✔
1363
                   istr(tree_ident(t)));
1364
      else if (sem_has_access(type))
1,409✔
1365
         sem_error(t, "generic %s may not have a type with a subelement of "
2✔
1366
                   "access type", istr(tree_ident(t)));
1367
      else if (type_is_protected(type))
1,407✔
1368
         sem_error(t, "generic %s may not have protected type",
1✔
1369
                   istr(tree_ident(t)));
1370
      else if (type_is_file(type))
1,406✔
1371
         sem_error(t, "generic %s may not have file type", istr(tree_ident(t)));
1✔
1372
   }
1373

1374
   if (tree_has_value(t)) {
1,534✔
1375
      tree_t value = tree_value(t);
744✔
1376
      if (!sem_check(value, tab))
744✔
1377
         return false;
1378

1379
      if (!sem_check_type(value, type))
740✔
UNCOV
1380
         sem_error(value, "type of default value %s does not match type "
×
1381
                   "of declaration %s", type_pp(tree_type(value)),
1382
                   type_pp(type));
1383
   }
1384

1385
   return true;
1386
}
1387

1388
static bool sem_check_field_decl(tree_t t)
1389
{
1390
   return true;
1391
}
1392

1393
static bool sem_check_unit_decl(tree_t t)
1394
{
1395
   return true;
1396
}
1397

1398
static bool sem_check_alias(tree_t t, nametab_t *tab)
1,392✔
1399
{
1400
   // Rules for aliases are given in LRM 93 section 4.3.3
1401

1402
   tree_t value = tree_value(t);
1,392✔
1403
   type_t type = get_type_or_null(t);
1,392✔
1404

1405
   const tree_kind_t value_kind = tree_kind(value);
1,392✔
1406

1407
   if (type != NULL && type_is_subprogram(type)) {
1,392✔
1408
      // Alias of subprogram or enumeration literal
1409
      // Rules for matching signatures are in LRM 93 section 2.3.2
1410
      assert(tree_kind(value) == T_REF || tree_kind(value) == T_PROT_REF);
512✔
1411
      return true;
512✔
1412
   }
1413
   else if (value_kind == T_REF && tree_has_ref(value)) {
880✔
1414
      tree_t decl = tree_ref(value);
774✔
1415
      if (aliased_type_decl(decl) != NULL)
774✔
1416
         return true;   // Alias of type
1417
      else if (tree_kind(decl) == T_VIEW_DECL)
725✔
1418
         return true;   // Alias of view declaration
1419
   }
1420

1421
   // Alias of object
1422
   if (!sem_check(value, tab))
830✔
1423
      return false;
1424

1425
   if (value_kind == T_ATTR_REF && tree_subkind(value) == ATTR_CONVERSE) {
823✔
1426
      // Special case handling for
1427
      //   https://gitlab.com/IEEE-P1076/VHDL-Issues/-/issues/293
1428
      return true;
1429
   }
1430
   else if (!sem_static_name(value, sem_globally_static)) {
806✔
1431
      diag_t *d = diag_new(DIAG_ERROR, tree_loc(value));
6✔
1432
      diag_printf(d, "aliased name is not static");
6✔
1433
      diag_lrm(d, STD_93, "6.1");
6✔
1434
      diag_emit(d);
6✔
1435
      return false;
6✔
1436
   }
1437

1438
   if (type != NULL) {
800✔
1439
      // Alias declaration had optional subtype indication
1440

1441
      if (!sem_check_subtype(t, type, tab))
743✔
1442
         return false;
1443

1444
      if (!sem_check_type(value, type))
743✔
1445
         sem_error(t, "type of aliased object %s does not match expected "
1✔
1446
                   "type %s", type_pp2(tree_type(value), type),
1447
                   type_pp2(type, tree_type(value)));
1448

1449
      if (opt_get_int(OPT_RELAXED) && type_is_unconstrained(type)) {
742✔
1450
         // If the type of the aliased object is unconstrained then
1451
         // use its subtype instead of the subtype declared by the
1452
         // alias.  This is required for some UVVM sources.
1453
         type_t obj_type = tree_type(value);
1✔
1454
         if (!type_is_unconstrained(obj_type))
1✔
1455
            tree_set_type(t, obj_type);
1✔
1456
      }
1457
   }
1458
   else
1459
      type = tree_type(value);
57✔
1460

1461
   if (standard() < STD_08 && type_is_array(type) && dimension_of(type) > 1) {
799✔
1462
      diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
1✔
1463
      diag_printf(d, "object alias may not have multidimensional array type "
1✔
1464
                  "in VHDL-%s", standard_text(standard()));
1465
      diag_lrm(d, STD_93, "4.3.3.1");
1✔
1466
      diag_emit(d);
1✔
1467
      return false;
1✔
1468
   }
1469

1470
   return true;
1471
}
1472

1473
static bool sem_check_func_ports(tree_t t, nametab_t *tab)
1474
{
1475
   const bool pure = !(tree_flags(t) & TREE_F_IMPURE);
1476
   const bool pre_std19 = (standard() < STD_19);
1477

1478
   if (!pure && !pre_std19)
1479
      return true;   // LCS2016_002 relaxed rules for impure functions
1480

1481
   const int nports = tree_ports(t);
1482
   for (int i = 0; i < nports; i++) {
1483
      tree_t p = tree_port(t, i);
1484
      if (tree_subkind(p) != PORT_IN) {
1485
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(p));
1486
         diag_printf(d, "%sfunction parameters must have mode IN",
1487
                     pre_std19 ? "" : "pure ");
1488
         diag_hint(d, tree_loc(p), "parameter %s has mode %s",
1489
                   istr(tree_ident(p)), port_mode_str(tree_subkind(p)));
1490
         diag_lrm(d, STD_08, "4.2.2.1");
1491
         diag_emit(d);
1492
      }
1493
      else if (tree_class(p) == C_VARIABLE) {
1494
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(p));
1495
         diag_printf(d, "class of %sfunction parameters must be CONSTANT, "
1496
                     "SIGNAL, or FILE", pre_std19 ? "" : "pure ");
1497
         diag_hint(d, tree_loc(p), "parameter %s has class %s",
1498
                   istr(tree_ident(p)), class_str(tree_class(p)));
1499
         diag_lrm(d, STD_08, "4.2.2.1");
1500
         diag_emit(d);
1501
      }
1502
   }
1503

1504
   return true;
1505
}
1506

1507
static bool sem_check_protected_method(tree_t t, nametab_t *tab)
1508
{
1509
   if (standard() >= STD_19)
1510
      return true;   // Relaxed in LCS2016_04
1511

1512
   const int nports = tree_ports(t);
1513
   for (int i = 0; i < nports; i++) {
1514
      tree_t p = tree_port(t, i);
1515
      type_t type = tree_type(p);
1516

1517
      if (sem_has_access(type)) {
1518
         diag_t *d = pedantic_diag(p);
1519
         if (d != NULL) {
1520
            diag_printf(d, "parameters of protected type methods cannot be of "
1521
                        "an access type or a composite type containing an "
1522
                        "access type");
1523
            if (type_is_access(type))
1524
               diag_hint(d, tree_loc(p), "type of %s is %s which is an "
1525
                         "access type", istr(tree_ident(p)), type_pp(type));
1526
            else
1527
               diag_hint(d, tree_loc(p), "type of %s is %s which has an "
1528
                         "access type as a subelement",
1529
                         istr(tree_ident(p)), type_pp(type));
1530
            diag_lrm(d, STD_08, "5.6.2");
1531
            diag_emit(d);
1532
         }
1533
      }
1534
      else if (type_is_file(type)) {
1535
         diag_t *d = pedantic_diag(p);
1536
         if (d != NULL) {
1537
            diag_printf(d, "parameters of protected type methods cannot be of "
1538
                        "a file type");
1539
            diag_hint(d, tree_loc(p), "type of %s is %s which is a file type",
1540
                      istr(tree_ident(p)), type_pp(type));
1541
            diag_lrm(d, STD_08, "5.6.2");
1542
            diag_emit(d);
1543
         }
1544
      }
1545
   }
1546

1547
   if (tree_kind(t) == T_FUNC_DECL) {
1548
      type_t result = type_result(tree_type(t));
1549
      if (sem_has_access(result) || type_is_file(result)) {
1550
         diag_t *d = pedantic_diag(t);
1551
         if (d != NULL) {
1552
            diag_printf(d, "return type of a protected type method cannot be "
1553
                        "of a file type, access type, or a composite type with "
1554
                        "a subelement that is an access type");
1555
            diag_lrm(d, STD_08, "5.6.2");
1556
            diag_emit(d);
1557
         }
1558
      }
1559
   }
1560

1561
   return true;
1562
}
1563

1564
static bool sem_check_func_result(tree_t t)
12,430✔
1565
{
1566
   type_t result = type_result(tree_type(t));
12,430✔
1567

1568
   if (type_is_protected(result))
12,430✔
1569
      sem_error(t, "function result subtype may not denote a protected type");
1✔
1570
   else if (type_is_file(result))
12,429✔
1571
      sem_error(t, "function result subtype may not denote a file type");
1✔
1572

1573
   return true;
1574
}
1575

1576
static bool sem_check_func_decl(tree_t t, nametab_t *tab)
1577
{
1578
   const tree_flags_t flags = tree_flags(t);
1579
   if (flags & TREE_F_PREDEFINED)
1580
      return true;
1581

1582
   if (!sem_check_func_ports(t, tab))
1583
      return false;
1584

1585
   if (!sem_check_func_result(t))
1586
      return false;
1587

1588
   if ((flags & TREE_F_PROTECTED) && !sem_check_protected_method(t, tab))
1589
      return false;
1590

1591
   return true;
1592
}
1593

1594
static bool sem_compare_interfaces(tree_t dport, tree_t bport,
12,187✔
1595
                                   int nth, tree_t body, const char *what)
1596
{
1597
   tree_flags_t dflags = tree_flags(dport);
12,187✔
1598
   tree_flags_t bflags = tree_flags(bport);
12,187✔
1599

1600
   ident_t dname = tree_ident(dport);
12,187✔
1601
   ident_t bname = tree_ident(bport);
12,187✔
1602

1603
   if (dname != bname) {
12,187✔
1604
      diag_t *d = diag_new(DIAG_ERROR, tree_loc(bport));
2✔
1605
      diag_printf(d, "%s name %s in subprogram %s body does not match "
2✔
1606
                  "name %s in declaration", what,
1607
                  istr(bname), istr(tree_ident(body)), istr(dname));
1608
      diag_hint(d, tree_loc(dport), "%s %s has name %s in specification",
2✔
1609
                ordinal_str(nth + 1), what, istr(dname));
1610
      diag_hint(d, tree_loc(bport), "%s %s has name %s in body",
2✔
1611
                ordinal_str(nth + 1), what, istr(bname));
1612
      diag_emit(d);
2✔
1613
      return false;
2✔
1614
   }
1615

1616
   type_t dtype = tree_type(dport);
12,185✔
1617
   type_t btype = tree_type(bport);
12,185✔
1618

1619
   // Do not use type_eq here as subtype must exactly match
1620
   if (!type_strict_eq(btype, dtype)) {
12,185✔
1621
     diag_t *d = diag_new(DIAG_ERROR, tree_loc(bport));
2✔
1622
     diag_printf(d, "subtype of %s %s does not match type %s in "
2✔
1623
                 "specification", what, istr(bname), type_pp(dtype));
1624
     diag_hint(d, tree_loc(dport), "%s %s declared with type %s",
2✔
1625
               what, istr(dname), type_pp(dtype));
1626
     diag_hint(d, tree_loc(bport), "%s %s declared with type %s ",
2✔
1627
               what, istr(bname), type_pp(btype));
1628
     diag_emit(d);
2✔
1629
     return false;
2✔
1630
   }
1631

1632
   const port_mode_t dmode = tree_subkind(dport);
12,183✔
1633
   const port_mode_t bmode = tree_subkind(bport);
12,183✔
1634

1635
   if (dmode != bmode) {
12,183✔
1636
     diag_t *d = diag_new(DIAG_ERROR, tree_loc(bport));
2✔
1637
     diag_printf(d, "%s %s of subprogram body %s with mode %s does not "
2✔
1638
                 "match mode %s in specification", what, istr(dname),
1639
                 istr(tree_ident(body)), port_mode_str(bmode),
1640
                 port_mode_str(dmode));
1641
     diag_hint(d, tree_loc(dport), "%s %s declared with mode %s",
2✔
1642
               what, istr(dname), port_mode_str(dmode));
1643
     diag_hint(d, tree_loc(bport), "%s %s declared with mode %s",
2✔
1644
               what, istr(bname), port_mode_str(bmode));
1645
     diag_emit(d);
2✔
1646
     return false;
2✔
1647
   }
1648

1649
   const bool bmode_explicit = !!(bflags & TREE_F_EXPLICIT_MODE);
12,181✔
1650
   const bool dmode_explicit = !!(dflags & TREE_F_EXPLICIT_MODE);
12,181✔
1651

1652
   if (bmode_explicit != dmode_explicit) {
12,181✔
1653
      diag_t *d = pedantic_diag(bport);
1✔
1654
      if (d != NULL) {
1✔
1655
         diag_printf(d, "mode (%s) of %s %s of subprogram %s not defined "
1✔
1656
                     "equally in subprogram specification and "
1657
                     "subprogram body", port_mode_str(dmode), what,
1658
                     istr(dname), istr(tree_ident(body)));
1659

1660
         diag_hint(d, tree_loc(dport), "%s mode %sdeclared explicitly",
2✔
1661
                   what, dmode_explicit ? "" : "not ");
1662
         diag_hint(d, tree_loc(bport), "%s mode %sdeclared explicitly",
1✔
1663
                   what, bmode_explicit ? "" : "not ");
1664
         diag_emit(d);
1✔
1665
      }
1666
      return false;
1✔
1667
   }
1668

1669
   const class_t dclass = tree_class(dport);
12,180✔
1670
   const class_t bclass = tree_class(bport);
12,180✔
1671

1672
   if (dclass != bclass) {
12,180✔
1673
     diag_t *d = diag_new(DIAG_ERROR, tree_loc(bport));
3✔
1674
     diag_printf(d, "class %s of subprogram body %s %s %s does not "
3✔
1675
                 "match class %s in specification", class_str(bclass),
1676
                 istr(tree_ident(body)), what, istr(dname), class_str(dclass));
1677
     diag_hint(d, tree_loc(dport), "%s %s declared with class %s in "
3✔
1678
               "subprogram specification", what, istr(dname),
1679
               class_str(dclass));
1680
     diag_hint(d, tree_loc(bport), "%s %s declared with class %s in "
3✔
1681
               "subprogram body", what, istr(bname), class_str(bclass));
1682
     diag_emit(d);
3✔
1683
     return false;
3✔
1684
   }
1685

1686
   const bool bclass_explicit = !!(bflags & TREE_F_EXPLICIT_CLASS);
12,177✔
1687
   const bool dclass_explicit = !!(dflags & TREE_F_EXPLICIT_CLASS);
12,177✔
1688

1689
   if (bclass_explicit != dclass_explicit) {
12,177✔
1690
      diag_t *d = pedantic_diag(bport);
3✔
1691
      if (d != NULL) {
3✔
1692
         diag_printf(d, "class (%s) of %s %s of subprogram %s not defined "
2✔
1693
                     "equally in subprogram specification and "
1694
                     "subprogram body", class_str(dclass), what,
1695
                     istr(dname), istr(tree_ident(body)));
1696

1697
         diag_hint(d, tree_loc(dport), "%s class %sdeclared explicitly in "
3✔
1698
                   "subprogram specification", what,
1699
                   dclass_explicit ? "" : "not ");
1700
         diag_hint(d, tree_loc(bport), "%s class %sdeclared explicitly in "
3✔
1701
                   "subprogram body", what, bclass_explicit ? "" : "not ");
1702
         diag_emit(d);
2✔
1703
      }
1704
      return false;
3✔
1705
   }
1706

1707
   tree_t bdef = tree_has_value(bport) ? tree_value(bport) : NULL;
12,174✔
1708
   tree_t ddef = tree_has_value(dport) ? tree_value(dport) : NULL;
12,174✔
1709

1710
   if (bdef == NULL && ddef == NULL)
12,174✔
1711
     return true;
1712

1713
   const tree_kind_t bkind = bdef ? tree_kind(bdef) : T_LAST_TREE_KIND;
1,894✔
1714
   const tree_kind_t dkind = ddef ? tree_kind(ddef) : T_LAST_TREE_KIND;
1,894✔
1715

1716
   // Work around some mismatches caused by folding
1717
   if (bdef != NULL && ddef != NULL && bkind != dkind)
1,894✔
1718
     return true;
1719

1720
   if (dkind == bkind) {
1,869✔
1721
     // This only covers a few simple cases
1722
     switch (dkind) {
1,868✔
1723
     case T_LITERAL: {
250✔
1724
       const literal_kind_t dsub = tree_subkind(ddef);
250✔
1725
       const literal_kind_t bsub = tree_subkind(bdef);
250✔
1726
       if (dsub == bsub) {
250✔
1727
         switch (dsub) {
250✔
1728
         case L_INT:
187✔
1729
           if (tree_ival(ddef) == tree_ival(bdef))
187✔
1730
             return true;
1731
           break;
1732
         case L_REAL:
5✔
1733
           if (tree_dval(ddef) == tree_dval(bdef))
5✔
1734
             return true;
1735
           break;
1736
         default:
1737
           return true;
1738
         }
UNCOV
1739
       }
×
1740
     } break;
1741

1742
     case T_REF:
1,437✔
1743
     case T_FCALL:
1744
       if (!tree_has_ref(bdef) || !tree_has_ref(ddef))
1,437✔
UNCOV
1745
         return true; // Was parse error, ignore it
×
1746

1747
       tree_t bref = tree_ref(bdef);
1,437✔
1748
       tree_t dref = tree_ref(ddef);
1,437✔
1749

1750
       if (bref == dref)
1,437✔
1751
         return true;
1752

1753
       // Work around mismatch introduced by folding
1754
       const tree_kind_t brefkind = tree_kind(bref);
147✔
1755
       if (brefkind == T_CONST_DECL || brefkind == T_GENERIC_DECL)
147✔
1756
         return true;
1757

1758
       break;
1759

1760
     default:
1761
       return true;
1762
     }
1763
   }
1✔
1764

1765
   diag_t *d = diag_new(DIAG_ERROR, tree_loc(bport));
3✔
1766
   diag_printf(d, "default value of %s %s in subprogram body %s does not "
3✔
1767
               "match declaration", what, istr(dname), istr(tree_ident(body)));
1768
   diag_hint(d, tree_loc(dport), "parameter was originally declared here");
3✔
1769
   diag_hint(d, tree_loc(bport), "body has different default value");
3✔
1770
   diag_emit(d);
3✔
1771

1772
   return false;
3✔
1773
}
1774

1775
static bool sem_check_conforming(tree_t decl, tree_t body)
6,254✔
1776
{
1777
   // Conformance rules are in LRM 08 section 4.10
1778
   // Note we don't implement strict lexical conformance here
1779

1780
   bool ok = true;
6,254✔
1781

1782
   const bool dimpure = !!(tree_flags(decl) & TREE_F_IMPURE);
6,254✔
1783
   const bool bimpure = !!(tree_flags(body) & TREE_F_IMPURE);
6,254✔
1784

1785
   if (dimpure != bimpure) {
6,254✔
1786
      diag_t *d = diag_new(DIAG_ERROR, tree_loc(body));
2✔
1787
      diag_printf(d, "function %s declaration was %s but body is %s",
4✔
1788
                  istr(tree_ident(body)), dimpure ? "impure" : "pure",
1789
                  bimpure ? "impure" : "pure");
1790
      diag_hint(d, tree_loc(decl), "declaration was %s",
2✔
1791
                dimpure ? "impure" : "pure");
1792
      diag_hint(d, tree_loc(body), "expecting keyword %s to match declaration",
3✔
1793
                bimpure ? "IMPURE" : "PURE");
1794
      diag_emit(d);
2✔
1795
      ok = false;
2✔
1796
   }
1797

1798
   // This must be true or they would be considered different overloads
1799
   assert(tree_ports(decl) == tree_ports(body));
6,254✔
1800

1801
   const int nports = tree_ports(decl);
6,254✔
1802
   for (int i = 0; i < nports; i++) {
18,401✔
1803
      tree_t dport = tree_port(decl, i);
12,147✔
1804
      tree_t bport = tree_port(body, i);
12,147✔
1805
      ok &= sem_compare_interfaces(dport, bport, i, body, "parameter");
12,147✔
1806
   }
1807

1808
   const int ngenerics = tree_generics(decl);
6,254✔
1809
   if (ngenerics != tree_generics(body)) {
6,254✔
1810
      diag_t *d = diag_new(DIAG_ERROR, tree_loc(body));
1✔
1811
      diag_printf(d, "subprogram %s declaration has %d generic%s but body "
2✔
1812
                  "has %d", istr(tree_ident(body)), ngenerics,
1813
                  ngenerics > 1 ? "s" : "", tree_generics(body));
1814
      diag_hint(d, tree_loc(decl), "declaration with %d generics", ngenerics);
1✔
1815
      diag_hint(d, tree_loc(body), "body has %d generics", tree_generics(body));
1✔
1816
      diag_emit(d);
1✔
1817
      ok = false;
1✔
1818
   }
1819
   else {
1820
      for (int i = 0; i < ngenerics; i++) {
6,293✔
1821
         tree_t dgen = tree_generic(decl, i);
40✔
1822
         tree_t bgen = tree_generic(body, i);
40✔
1823
         ok &= sem_compare_interfaces(dgen, bgen, i, body, "generic");
40✔
1824
      }
1825
   }
1826

1827
   return ok;
6,254✔
1828
}
1829

1830
static bool sem_check_func_body(tree_t t, nametab_t *tab)
6,758✔
1831
{
1832
   if (!sem_check_func_ports(t, tab))
6,758✔
1833
      return false;
1834

1835
   if (!sem_check_func_result(t))
6,758✔
1836
      return false;
1837

1838
   tree_t fwd = find_forward_decl(tab, t);
6,757✔
1839
   if (fwd != NULL && !sem_check_conforming(fwd, t))
6,757✔
1840
      return false;
13✔
1841

1842
   return true;
1843
}
1844

1845
static bool sem_check_proc_decl(tree_t t, nametab_t *tab)
1846
{
1847
   const tree_flags_t flags = tree_flags(t);
1848
   if ((flags & TREE_F_PROTECTED) && !sem_check_protected_method(t, tab))
1849
      return false;
1850

1851
   return true;
1852
}
1853

1854
static bool sem_check_proc_body(tree_t t, nametab_t *tab)
1,965✔
1855
{
1856
   tree_t fwd = find_forward_decl(tab, t);
1,965✔
1857
   if (fwd != NULL && !sem_check_conforming(fwd, t))
1,965✔
1858
      return false;
1859

1860
   // Cleared by wait statement or pcall
1861
   tree_set_flag(t, TREE_F_NEVER_WAITS);
1,960✔
1862

1863
   return true;
1,960✔
1864
}
1865

1866
static bool sem_check_subprogram_inst(tree_t t, nametab_t *tab)
57✔
1867
{
1868
   if (tree_generics(t) == 0)
57✔
1869
      return false;   // Was a parse error
1870

1871
   if (!sem_check_generic_map(t, t, tab))
52✔
1872
      return false;
2✔
1873

1874
   // Other declarations were checked on the uninstantiated subprogram
1875

1876
   return true;
1877
}
1878

1879
static bool sem_check_sensitivity(tree_t t, nametab_t *tab)
11,831✔
1880
{
1881
   const int ntriggers = tree_triggers(t);
11,831✔
1882
   for (int i = 0; i < ntriggers; i++) {
12,845✔
1883
      tree_t r = tree_trigger(t, i);
1,019✔
1884
      if (tree_kind(r) == T_ALL)
1,019✔
1885
         continue;
21✔
1886
      else if (!sem_check(r, tab) || !sem_readable(r))
998✔
1887
         return false;
2✔
1888

1889
      if (!sem_static_name(r, sem_globally_static))
996✔
1890
         sem_error(r, "name in sensitivity list is not a static signal name");
2✔
1891

1892
      if (class_of(r) != C_SIGNAL) {
994✔
1893
         tree_t ref = name_to_ref(r);
1✔
1894
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(r));
1✔
1895
         if (ref != NULL) {
1✔
1896
            tree_t decl = tree_ref(ref);
1✔
1897
            diag_printf(d, "name %s in sensitivity list is not a signal",
1✔
1898
                        istr(tree_ident(decl)));
1899
            diag_hint(d, tree_loc(r), "%s is a %s", istr(tree_ident(decl)),
1✔
1900
                      class_str(class_of(decl)));
1901
         }
1902
         else
UNCOV
1903
            diag_printf(d, "name in sensitivity list is not a signal");
×
1904
         diag_emit(d);
1✔
1905
         return false;
1✔
1906
      }
1907
   }
1908

1909
   return true;
1910
}
1911

1912
static void sem_check_static_elab(tree_t t)
46,025✔
1913
{
1914
   // LRM 93 12.3 forbirds references to signals before the design has
1915
   // been elaborated: "The value of any object denoted by a primary in
1916
   // such an expression must be defined at the time the primary is read"
1917

1918
   switch (tree_kind(t)) {
58,314✔
1919
   case T_REF:
5,214✔
1920
   case T_EXTERNAL_NAME:
1921
      if (class_of(t) == C_SIGNAL) {
5,214✔
1922
         ident_t id;
18✔
1923
         if (tree_kind(t) == T_EXTERNAL_NAME)
18✔
1924
            id = tree_ident(tree_part(t, tree_parts(t) - 1));
1✔
1925
         else
1926
            id = tree_ident(t);
17✔
1927

1928
         diag_t *d = pedantic_diag(t);
18✔
1929
         if (d != NULL) {
18✔
1930
            diag_printf(d, "cannot reference signal %s during static "
15✔
1931
                        "elaboration", istr(id));
1932
            diag_hint(d, NULL, "the value of a signal is not defined "
15✔
1933
                      "until after the design hierarchy is elaborated");
1934
            diag_lrm(d, STD_93, "12.3");
15✔
1935
            diag_emit(d);
15✔
1936
         }
1937
      }
1938
      break;
1939

1940
   case T_SIGNAL_DECL:
9,703✔
1941
   case T_VAR_DECL:
1942
   case T_CONST_DECL:
1943
      {
1944
         type_t type = tree_type(t);
9,703✔
1945
         if (type_is_generic(type))
9,703✔
1946
            ;  // Cannot check further
1947
         else if (type_is_scalar(type) && !type_is_none(type))
9,690✔
1948
            sem_check_static_elab(range_of(type, 0));
4,421✔
1949
         else if (type_is_array(type) && !type_is_unconstrained(type)) {
5,269✔
1950
            const int ndims = dimension_of(type);
3,932✔
1951
            for (int i = 0; i < ndims; i++)
8,003✔
1952
               sem_check_static_elab(range_of(type, i));
4,071✔
1953
         }
1954

1955
         if (tree_has_value(t))
9,703✔
1956
            sem_check_static_elab(tree_value(t));
3,718✔
1957
      }
1958
      break;
1959

1960
   case T_ARRAY_REF:
4✔
1961
      {
1962
         sem_check_static_elab(tree_value(t));
4✔
1963

1964
         const int nparams = tree_params(t);
4✔
1965
         for (int i = 0; i < nparams; i++)
8✔
1966
            sem_check_static_elab(tree_value(tree_param(t, i)));
4✔
1967
      }
1968
      break;
1969

1970
   case T_FCALL:
628✔
1971
      if (!(tree_flags(t) & TREE_F_GLOBALLY_STATIC)) {
628✔
1972
         const int nparams = tree_params(t);
54✔
1973
         for (int i = 0; i < nparams; i++)
93✔
1974
            sem_check_static_elab(tree_value(tree_param(t, i)));
39✔
1975
      }
1976
      break;
1977

1978
   case T_ARRAY_SLICE:
12✔
1979
      sem_check_static_elab(tree_value(t));
12✔
1980
      sem_check_static_elab(tree_range(t, 0));
12✔
1981
      break;
12✔
1982

1983
   case T_RECORD_REF:
54✔
1984
   case T_TYPE_CONV:
1985
      sem_check_static_elab(tree_value(t));
54✔
1986
      break;
54✔
1987

1988
   case T_RANGE:
8,504✔
1989
      switch (tree_subkind(t)) {
8,504✔
1990
      case RANGE_TO:
8,164✔
1991
      case RANGE_DOWNTO:
1992
         sem_check_static_elab(tree_left(t));
8,164✔
1993
         sem_check_static_elab(tree_right(t));
8,164✔
1994
         break;
8,164✔
1995
      case RANGE_EXPR:
340✔
1996
         sem_check_static_elab(tree_value(t));
340✔
1997
         break;
340✔
1998
      }
1999
      break;
2000

2001
   case T_ATTR_REF:
803✔
2002
      {
2003
         // Same list of predefined attributes as sem_globally_static
2004
         const attr_kind_t predef = tree_subkind(t);
803✔
2005
         const bool non_static = predef == ATTR_EVENT || predef == ATTR_ACTIVE
1,606✔
2006
            || predef == ATTR_LAST_EVENT || predef == ATTR_LAST_ACTIVE
803✔
2007
            || predef == ATTR_LAST_VALUE || predef == ATTR_DRIVING
802✔
2008
            || predef == ATTR_DRIVING_VALUE;
1,605✔
2009

2010
         if (non_static)
803✔
2011
            sem_check_static_elab(tree_name(t));
1✔
2012
      }
2013
      break;
2014

2015
   default:
2016
      break;
2017
   }
2018
}
46,025✔
2019

2020
static bool sem_check_process(tree_t t, nametab_t *tab)
4,031✔
2021
{
2022
   bool ok = sem_check_sensitivity(t, tab);
4,031✔
2023

2024
   const int ndecls = tree_decls(t);
4,031✔
2025
   for (int n = 0; n < ndecls; n++) {
11,284✔
2026
      tree_t d = tree_decl(t, n);
7,253✔
2027
      sem_check_static_elab(d);
7,253✔
2028
   }
2029

2030
   if (tree_triggers(t) > 0) {
4,031✔
2031
      // No wait statements allowed in process with sensitivity list
2032
      if (tree_visit_only(t, NULL, NULL, T_WAIT) > 0)
350✔
2033
         sem_error(t, "wait statement not allowed in process "
1✔
2034
                   "with sensitvity list");
2035
   }
2036

2037
   return ok;
2038
}
2039

2040
static bool sem_check_package(tree_t t, nametab_t *tab)
1,195✔
2041
{
2042
   if (!sem_check_context_clause(t, tab))
1,195✔
2043
      return false;
2044

2045
   if (tree_genmaps(t) > 0 && !sem_check_generic_map(t, t, tab))
1,195✔
2046
      return false;
2047

2048
   // Subprogram bodies are not allowed in package specification
2049
   const int ndecls = tree_decls(t);
1,195✔
2050
   for (int i = 0; i < ndecls; i++) {
26,660✔
2051
     tree_t d = tree_decl(t, i);
25,467✔
2052
     tree_kind_t kind = tree_kind(d);
25,467✔
2053
     if ((kind == T_FUNC_BODY) || (kind == T_PROC_BODY))
25,467✔
2054
       sem_error(d, "subprogram body is not allowed in package specification");
25,467✔
2055
   }
2056

2057
   return true;
2058
}
2059

2060
static bool sem_check_pack_inst(tree_t t, nametab_t *tab)
197✔
2061
{
2062
   if (tree_generics(t) == 0)
197✔
2063
      return false;   // Was a parse error
2064

2065
   if (!sem_check_generic_map(t, t, tab))
194✔
2066
      return false;
4✔
2067

2068
   // Other declarations were checked on the uninstantiated package
2069

2070
   return true;
2071
}
2072

2073
static bool sem_check_missing_body(tree_t body, tree_t spec)
5,985✔
2074
{
2075
   // Check for any subprogram declarations or protected types without bodies
2076
   bool ok = true;
5,985✔
2077
   const int ndecls = tree_decls(spec);
5,985✔
2078
   for (int i = 0; i < ndecls; i++) {
62,891✔
2079
      tree_t d = tree_decl(spec, i);
56,906✔
2080

2081
      const tree_kind_t dkind = tree_kind(d);
56,906✔
2082
      if (dkind != T_FUNC_DECL && dkind != T_PROC_DECL && dkind != T_PROT_DECL)
56,906✔
2083
         continue;
29,182✔
2084

2085
      type_t dtype = tree_type(d);
27,724✔
2086

2087
      bool found = false;
27,724✔
2088
      const int nbody_decls = tree_decls(body);
27,724✔
2089
      const int start = (body == spec ? i + 1 : 0);
27,724✔
2090
      for (int j = start; !found && (j < nbody_decls); j++) {
2,069,920✔
2091
         tree_t b = tree_decl(body, j);
2,042,190✔
2092
         const tree_kind_t bkind = tree_kind(b);
2,042,190✔
2093
         if (bkind != T_FUNC_BODY && bkind != T_PROC_BODY
2,042,190✔
2094
             && bkind != T_PROT_BODY)
1,076,850✔
2095
            continue;
1,073,270✔
2096
         else if (type_eq(dtype, tree_type(b)))
968,921✔
2097
            found = true;
6,759✔
2098
      }
2099

2100
      if (found)
27,724✔
2101
         continue;
6,759✔
2102

2103
      const bool missing = dkind == T_PROT_DECL
41,930✔
2104
         || (!(tree_flags(d) & TREE_F_PREDEFINED)
20,965✔
2105
             && !is_foreign(tree_subkind(d)));
132✔
2106

2107
      if (missing && opt_get_int(OPT_MISSING_BODY)) {
20,965✔
2108
         warn_at(tree_loc(d), "missing body for %s %s",
15✔
2109
                 (dkind == T_PROT_DECL) ? "protected type"
2110
                 : (dkind == T_PROC_DECL ? "procedure" : "function"),
7✔
2111
                 type_pp(dtype));
2112
      }
2113
   }
2114

2115
   if (body != spec)
5,985✔
2116
      ok = sem_check_missing_body(body, body) && ok;
637✔
2117

2118
   return ok;
5,985✔
2119
}
2120

2121
static bool sem_check_pack_body(tree_t t, nametab_t *tab)
641✔
2122
{
2123
   if (!tree_has_primary(t))
641✔
2124
      return false;
2125

2126
   tree_t pack = tree_primary(t);
637✔
2127

2128
   if (!sem_check_context_clause(pack, tab))
637✔
2129
      return false;
2130

2131
   if (!sem_check_context_clause(t, tab))
637✔
2132
      return false;
2133

2134
   if (!sem_check_missing_body(t, pack))
637✔
2135
      return false;
2136

2137
   if (!sem_check_missing_body(t, t))
637✔
2138
      return false;
2139

2140
   // Check for any deferred constants which were not given values
2141
   const int ndecls = tree_decls(pack);
637✔
2142
   for (int i = 0; i < ndecls; i++) {
16,568✔
2143
      tree_t d = tree_decl(pack, i);
15,933✔
2144
      if ((tree_kind(d) == T_CONST_DECL) && !tree_has_value(d)) {
15,933✔
2145
         tree_t d2 = search_decls(t, tree_ident(d), 0);
393✔
2146
         if (d2 == NULL || !tree_has_value(d2))
393✔
2147
            sem_error(d, "deferred constant %s was not given a value in the "
15,933✔
2148
                      "package body", istr(tree_ident(d)));
2149
      }
2150
   }
2151

2152
   return true;
2153
}
2154

2155
static bool sem_check_component(tree_t t, nametab_t *tab)
2156
{
2157
   return true;
2158
}
2159

2160
static void sem_passive_cb(tree_t t, void *context)
1✔
2161
{
2162
   tree_t s = context;
1✔
2163

2164
   diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
1✔
2165
   diag_printf(d, "signal assignment statement not allowed inside passive "
1✔
2166
               "process");
2167
   diag_hint(d, tree_loc(s), "process in entity statement part must "
1✔
2168
             "be passive");
2169
   diag_hint(d, tree_loc(t), "signal assignment statement");
1✔
2170
   diag_lrm(d, STD_93, "1.1.3");
1✔
2171
   diag_lrm(d, STD_93, "9.2");
1✔
2172

2173
   diag_emit(d);
1✔
2174
}
1✔
2175

2176
static bool sem_check_entity(tree_t t, nametab_t *tab)
4,068✔
2177
{
2178
   if (!sem_check_context_clause(t, tab))
4,068✔
2179
      return false;
2180

2181
   // All processes in entity statement part must be passive
2182
   const int nstmts = tree_stmts(t);
4,068✔
2183
   for (int i = 0; i < nstmts; i++) {
4,104✔
2184
      tree_t s = tree_stmt(t, i);
36✔
2185
      tree_visit_only(s, sem_passive_cb, s, T_SIGNAL_ASSIGN);
36✔
2186
   }
2187

2188
   return true;
2189
}
2190

2191
static bool sem_check_arch(tree_t t, nametab_t *tab)
4,077✔
2192
{
2193
   if (!tree_has_primary(t))
4,077✔
2194
      return false;
2195

2196
   if (!sem_check_context_clause(t, tab))
4,074✔
2197
      return false;
2198

2199
   const int ndecls = tree_decls(t);
4,074✔
2200
   for (int n = 0; n < ndecls; n++) {
25,595✔
2201
      tree_t d = tree_decl(t, n);
21,521✔
2202
      sem_check_static_elab(d);
21,521✔
2203
   }
2204

2205
   if (!sem_check_missing_body(t, t))
4,074✔
UNCOV
2206
      return false;
×
2207

2208
   return true;
2209
}
2210

2211
static tree_t sem_check_lvalue(tree_t t)
21,821✔
2212
{
2213
   switch (tree_kind(t)) {
50,316✔
2214
   case T_REF:
21,899✔
2215
      return sem_check_lvalue(tree_ref(t));
21,899✔
2216
   case T_ARRAY_SLICE:
6,596✔
2217
   case T_ARRAY_REF:
2218
   case T_ALIAS:
2219
   case T_RECORD_REF:
2220
   case T_ALL:
2221
      return sem_check_lvalue(tree_value(t));
6,596✔
2222
   case T_VAR_DECL:
2223
   case T_SIGNAL_DECL:
2224
   case T_PORT_DECL:
2225
   case T_CONST_DECL:
2226
   case T_IMPLICIT_SIGNAL:
2227
   case T_PARAM_DECL:
2228
   case T_EXTERNAL_NAME:
2229
      return t;
2230
   default:
5✔
2231
      return NULL;
5✔
2232
   }
2233
}
2234

2235
static bool sem_check_aggregate_target_element(tree_t a, type_t type)
324✔
2236
{
2237
   tree_t value = tree_value(a);
324✔
2238

2239
   if (tree_kind(value) != T_AGGREGATE) {
324✔
2240
      if (!sem_static_name(value, sem_locally_static))
306✔
2241
         sem_error(value, "aggregate element must be locally static name");
2✔
2242
   }
2243

2244
   const assoc_kind_t kind = tree_subkind(a);
322✔
2245
   switch (kind) {
322✔
2246
   case A_RANGE:
9✔
2247
      if (standard() >= STD_08) {
9✔
2248
         // LRM 08 section 10.6.2.1: it is an error if the element
2249
         // association contains a choice that is a discrete range and
2250
         // an expression of a type other than the aggregate type.
2251
         if (type_eq(tree_type(value), type))
8✔
2252
            break;
2253
         else {
2254
            diag_t *d = diag_new(DIAG_ERROR, tree_loc(value));
1✔
2255
            diag_printf(d, "range choice expression must have same type "
1✔
2256
                        "as aggregate");
2257
            diag_hint(d, tree_loc(value), "expression type is %s but "
1✔
2258
                      "aggregate is %s", type_pp(tree_type(value)),
2259
                      type_pp(type));
2260
            diag_lrm(d, STD_08, "10.6.2");
1✔
2261
            diag_emit(d);
1✔
2262
            return false;
1✔
2263
         }
2264
      }
2265
      // Fall-through
2266
   case A_OTHERS:
2267
      sem_error(a, "%s association not allowed in aggregate target",
4✔
2268
                assoc_kind_str(kind));
2269
   case A_NAMED:
2270
   case A_POS:
2271
      break;
2272
   }
2273

2274
   return true;
7✔
2275
}
2276

2277
static bool sem_check_variable_target(tree_t target)
15,874✔
2278
{
2279
   if (tree_kind(target) == T_AGGREGATE) {
15,874✔
2280
      // Rules for aggregate variable targets in LRM 93 section 8.5
2281

2282
      type_t type = tree_type(target);
70✔
2283
      if (!type_is_composite(type))
70✔
UNCOV
2284
         sem_error(target, "aggregate target of variable assignment has "
×
2285
                   "non-composite type %s", type_pp(tree_type(target)));
2286

2287
      const int nassocs = tree_assocs(target);
70✔
2288
      for (int i = 0; i < nassocs; i++) {
221✔
2289
         tree_t a = tree_assoc(target, i);
156✔
2290
         tree_t value = tree_value(a);
156✔
2291

2292
         if (!sem_check_variable_target(value))
156✔
2293
            return false;
2294

2295
         if (!sem_check_aggregate_target_element(a, type))
155✔
2296
            return false;
2297
      }
2298
   }
2299
   else {
2300
      tree_t decl = sem_check_lvalue(target);
15,804✔
2301
      const tree_kind_t kind = decl ? tree_kind(decl) : T_LAST_TREE_KIND;
15,804✔
2302

2303
      const bool suitable = kind == T_VAR_DECL
31,607✔
2304
         || (kind == T_PARAM_DECL && tree_class(decl) == C_VARIABLE)
2,565✔
2305
         || (kind == T_EXTERNAL_NAME && tree_class(decl) == C_VARIABLE);
15,810✔
2306

2307
      if (!suitable) {
15,804✔
2308
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(target));
6✔
2309
         diag_printf(d, "target of variable assignment must be a variable "
6✔
2310
                     "name or aggregate");
2311

2312
         tree_t ref = name_to_ref(target);
6✔
2313
         if (ref != NULL && tree_has_ref(ref))
6✔
2314
            diag_hint(d, tree_loc(target), "%s is a %s", istr(tree_ident(ref)),
5✔
2315
                      class_str(class_of(tree_ref(ref))));
2316

2317
         diag_emit(d);
6✔
2318
         return false;
6✔
2319
      }
2320
      else if (kind == T_PARAM_DECL && tree_subkind(decl) == PORT_IN) {
15,798✔
2321
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(target));
1✔
2322
         diag_printf(d, "cannot assign to parameter %s with mode IN",
1✔
2323
                     istr(tree_ident(decl)));
2324
         diag_hint(d, tree_loc(decl), "%s declared with mode IN",
1✔
2325
                   istr(tree_ident(decl)));
2326
         diag_hint(d, tree_loc(target), "target of variable assignment");
1✔
2327
         diag_emit(d);
1✔
2328
         return false;
1✔
2329
      }
2330
      else if (type_is_protected(tree_type(target)))
15,797✔
2331
         sem_error(target, "may not assign to variable of a protected type");
1✔
2332
   }
2333

2334
   return true;
2335
}
2336

2337
static bool sem_check_var_assign(tree_t t, nametab_t *tab)
15,823✔
2338
{
2339
   tree_t target = tree_target(t);
15,823✔
2340
   tree_t value = tree_value(t);
15,823✔
2341

2342
   if (!sem_check(target, tab))
15,823✔
2343
      return false;
2344

2345
   if (!sem_check(value, tab))
15,804✔
2346
      return false;
2347

2348
   if (!sem_readable(value))
15,719✔
2349
      return false;
2350

2351
   if (!sem_check_variable_target(target))
15,718✔
2352
      return false;
2353

2354
   if (!sem_check_same_type(value, target)) {
15,706✔
2355
      type_t target_type = tree_type(target);
11✔
2356
      type_t value_type  = tree_type(value);
11✔
2357
      sem_error(t, "type of value %s does not match type of target %s",
11✔
2358
                type_pp2(value_type, target_type),
2359
                type_pp2(target_type, value_type));
2360
   }
2361

2362
   return true;
2363
}
2364

2365
static bool sem_check_waveforms(tree_t t, tree_t target, nametab_t *tab)
5,937✔
2366
{
2367
   type_t std_time = std_type(NULL, STD_TIME);
5,937✔
2368
   type_t expect = tree_type(target);
5,937✔
2369

2370
   const int nwaves = tree_waveforms(t);
5,937✔
2371
   for (int i = 0; i < nwaves; i++) {
11,992✔
2372
      tree_t waveform = tree_waveform(t, i);
6,071✔
2373

2374
      if (tree_has_value(waveform)) {
6,071✔
2375
         tree_t value = tree_value(waveform);
6,057✔
2376

2377
         if (!sem_check(value, tab))
6,057✔
2378
            return false;
2379

2380
         if (!sem_readable(value))
6,050✔
2381
            return false;
2382

2383
         if (!sem_check_type(value, expect))
6,049✔
2384
            sem_error(t, "type of value %s does not match type of target %s",
6✔
2385
                      type_pp2(tree_type(value), expect),
2386
                      type_pp2(expect, tree_type(value)));
2387
      }
2388
      else {
2389
         tree_t decl = sem_check_lvalue(target);
14✔
2390
         if (decl != NULL && !is_guarded_signal(decl))
14✔
2391
            sem_error(waveform, "a null waveform element is only valid when "
2✔
2392
                      "the target is a guarded signal");
2393
      }
2394

2395
      if (tree_has_delay(waveform)) {
6,055✔
2396
         tree_t delay = tree_delay(waveform);
578✔
2397
         if (!sem_check(delay, tab))
578✔
2398
            return false;
2399

2400
         if (!sem_check_type(delay, std_time))
578✔
2401
            sem_error(delay, "type of delay must be %s but have %s",
6,055✔
2402
                      type_pp(std_time), type_pp(tree_type(delay)));
2403
      }
2404
   }
2405

2406
   return true;
2407
}
2408

2409
static tree_t sem_check_view_target(tree_t target)
176✔
2410
{
2411
   switch (tree_kind(target)) {
176✔
2412
   case T_REF:
113✔
2413
      {
2414
         tree_t decl = tree_ref(target);
113✔
2415
         if (tree_kind(decl) == T_PORT_DECL) {
113✔
2416
            const port_mode_t mode = tree_subkind(decl);
56✔
2417
            if (mode == PORT_ARRAY_VIEW || mode == PORT_RECORD_VIEW)
56✔
2418
               return tree_value(decl);
56✔
2419
         }
2420

2421
         return NULL;
2422
      }
2423

2424
   case T_ARRAY_REF:
8✔
2425
   case T_ARRAY_SLICE:
2426
      return sem_check_view_target(tree_value(target));
8✔
2427

2428
   case T_RECORD_REF:
55✔
2429
      {
2430
         tree_t view = sem_check_view_target(tree_value(target));
55✔
2431
         if (view == NULL)
55✔
2432
            return NULL;
2433

2434
         bool converse = false;
43✔
2435
         tree_t f = tree_ref(target);
43✔
2436
         tree_t e = find_element_mode_indication(view, f, &converse);
43✔
2437
         if (e == NULL)
43✔
2438
            return NULL;
2439

2440
         if (converse_mode(e, converse) == PORT_IN) {
43✔
2441
            tree_t port = tree_ref(name_to_ref(target));
2✔
2442
            diag_t *d = diag_new(DIAG_ERROR, tree_loc(target));
2✔
2443
            diag_printf(d, "cannot assign to element %s of port %s which has "
2✔
2444
                        "mode IN from mode view indication",
2445
                        istr(tree_ident(e)), istr(tree_ident(port)));
2446
            diag_hint(d, tree_loc(target), "target of signal assignment");
2✔
2447
            diag_hint(d, tree_loc(port), "sub-element %s of %s declared with "
2✔
2448
                      "mode IN due to mode view indication",
2449
                      istr(tree_ident(e)), istr(tree_ident(port)));
2450
            diag_emit(d);
2✔
2451
            return NULL;
2✔
2452
         }
2453

2454
         return NULL;
2455
      }
2456

2457
   default:
2458
      return NULL;
2459
   }
2460
}
2461

2462
static bool sem_check_signal_target(tree_t target, nametab_t *tab)
5,989✔
2463
{
2464
   if (tree_kind(target) == T_AGGREGATE) {
5,989✔
2465
      // Rules for aggregate signal targets in LRM 93 section 8.4
2466

2467
      type_t type = tree_type(target);
69✔
2468
      if (!type_is_composite(type))
69✔
UNCOV
2469
         sem_error(target, "aggregate target of signal assignment has "
×
2470
                   "non-composite type %s", type_pp(tree_type(target)));
2471

2472
      const int nassocs = tree_assocs(target);
69✔
2473
      for (int i = 0; i < nassocs; i++) {
235✔
2474
         tree_t a = tree_assoc(target, i);
173✔
2475
         tree_t value = tree_value(a);
173✔
2476

2477
         if (!sem_check_signal_target(value, tab))
173✔
2478
            return false;
2479

2480
         if (!sem_check_aggregate_target_element(a, type))
169✔
2481
            return false;
2482
      }
2483

2484
      return true;
2485
   }
2486
   else {
2487
      tree_t decl = sem_check_lvalue(target);
5,920✔
2488
      if (decl == NULL)
5,920✔
2489
         sem_error(target, "target of signal assignment must be a signal "
3✔
2490
                   "name or aggregate");
2491

2492
      switch (tree_kind(decl)) {
5,917✔
2493
      case T_SIGNAL_DECL:
4,563✔
2494
         {
2495
            tree_t sub = find_enclosing(tab, S_SUBPROGRAM);
4,563✔
2496
            if (sub != NULL && find_enclosing(tab, S_PROCESS) == NULL) {
4,563✔
2497
               // LRM 08 section 10.5.2.2: if a signal assignment appears
2498
               // in a procedure not contained within a process then the
2499
               // target must be a formal parameter
2500
               sem_error(target, "signal %s is not a formal parameter and "
3✔
2501
                         "subprogram %s is not contained within a process "
2502
                         "statement", istr(tree_ident(decl)),
2503
                         type_pp(tree_type(sub)));
2504
            }
2505
         }
2506
         break;
2507

2508
      case T_IMPLICIT_SIGNAL:
1✔
2509
         sem_error(target, "implicit signal may not be assigned");
1✔
2510

2511
      case T_PORT_DECL:
1,344✔
2512
      case T_PARAM_DECL:
2513
         {
2514
            const port_mode_t mode = tree_subkind(decl);
1,344✔
2515
            if (mode == PORT_IN) {
1,344✔
2516
               diag_t *d = diag_new(DIAG_ERROR, tree_loc(target));
5✔
2517
               diag_printf(d, "cannot assign to input %s %s",
11✔
2518
                           tree_kind(decl) == T_PORT_DECL
5✔
2519
                           ? "port" : "parameter",
2520
                           istr(tree_ident(decl)));
2521
               diag_hint(d, tree_loc(target), "target of signal assignment");
5✔
2522
               diag_hint(d, tree_loc(decl), "%s declared with mode IN",
5✔
2523
                         istr(tree_ident(decl)));
2524
               diag_emit(d);
5✔
2525
               return false;
5✔
2526
            }
2527
            else if (mode == PORT_ARRAY_VIEW || mode == PORT_RECORD_VIEW) {
1,339✔
2528
               tree_t view = sem_check_view_target(target);
56✔
2529
               if (view != NULL) {
56✔
2530
                  tree_t inport = NULL;
1✔
2531
                  type_t view_type = tree_type(view);
1✔
2532
                  const int nelems = type_fields(view_type);
1✔
2533
                  for (int i = 0; i < nelems; i++) {
1✔
2534
                     tree_t e = type_field(view_type, i);
1✔
2535
                     const port_mode_t mode = tree_subkind(e);
1✔
2536
                     if (mode == PORT_IN || mode == PORT_ARRAY_VIEW
1✔
UNCOV
2537
                         || mode == PORT_RECORD_VIEW) {
×
2538
                        // This is not correct for nested mode view
2539
                        // indications but seems like a very obscure
2540
                        // corner case
2541
                        inport = e;
2542
                        break;
2543
                     }
2544
                  }
2545

2546
                  if (inport != NULL) {
1✔
2547
                     diag_t *d = diag_new(DIAG_ERROR, tree_loc(target));
1✔
2548
                     diag_printf(d, "cannot assign to port %s with mode view "
1✔
2549
                                 "indication as one or more sub-elements have "
2550
                                 "mode IN", istr(tree_ident(decl)));
2551
                     diag_hint(d, tree_loc(target),
1✔
2552
                               "target of signal assignment");
2553
                     diag_hint(d, tree_loc(inport),
1✔
2554
                               "element %s declared with mode IN",
2555
                               istr(tree_ident(inport)));
2556
                     diag_emit(d);
1✔
2557
                     return false;
1✔
2558
                  }
2559
               }
2560

2561
               return true;
55✔
2562
            }
2563
            else if (mode == PORT_LINKAGE)
1,283✔
2564
               sem_error(target, "linkage port %s may not be updated except as "
1✔
2565
                         "an actual corresponding to an interface of mode "
2566
                         "linkage", istr(tree_ident(decl)));
2567
            else if (tree_class(decl) != C_SIGNAL) {
1,282✔
2568
               diag_t *d = diag_new(DIAG_ERROR, tree_loc(target));
1✔
2569
               diag_printf(d, "%s is not a valid target of signal assignment",
1✔
2570
                           istr(tree_ident(decl)));
2571
               diag_hint(d, tree_loc(target), "target of signal assignment");
1✔
2572
               diag_hint(d, tree_loc(decl), "declared with class %s",
1✔
2573
                         class_str(tree_class(decl)));
2574
               diag_emit(d);
1✔
2575
               return false;
1✔
2576
            }
2577
         }
2578
         break;
2579

2580
      case T_EXTERNAL_NAME:
8✔
2581
         if (tree_class(decl) != C_SIGNAL) {
8✔
UNCOV
2582
            tree_t tail = tree_part(decl, tree_parts(decl) - 1);
×
UNCOV
2583
            diag_t *d = diag_new(DIAG_ERROR, tree_loc(target));
×
UNCOV
2584
            diag_printf(d, "external name %s is not a valid target of "
×
2585
                        "signal assignment", istr(tree_ident(tail)));
UNCOV
2586
            diag_hint(d, tree_loc(target), "target of signal assignment");
×
UNCOV
2587
            diag_hint(d, tree_loc(decl), "declared with class %s",
×
2588
                      class_str(tree_class(decl)));
UNCOV
2589
            diag_emit(d);
×
UNCOV
2590
            return false;
×
2591
         }
2592
         break;
2593

2594
      case T_VAR_DECL:
1✔
2595
      case T_CONST_DECL:
2596
         {
2597
            diag_t *d = diag_new(DIAG_ERROR, tree_loc(target));
1✔
2598
            diag_printf(d, "%s %s is not a valid target of signal assignment",
1✔
2599
                        class_str(class_of(decl)), istr(tree_ident(decl)));
2600
            diag_hint(d, tree_loc(target), "target of signal assignment");
1✔
2601
            diag_hint(d, tree_loc(decl), "declared as %s",
1✔
2602
                      class_str(class_of(decl)));
2603
            diag_emit(d);
1✔
2604
            return false;
1✔
2605
         }
2606

UNCOV
2607
      default:
×
UNCOV
2608
         sem_error(target, "invalid target of signal assignment");
×
2609
      }
2610

2611
      return true;
5,849✔
2612
   }
2613
}
2614

2615
static bool sem_check_reject(tree_t t, nametab_t *tab)
445✔
2616
{
2617
   if (!sem_check(t, tab))
445✔
2618
      return false;
2619

2620
   if (!type_eq(tree_type(t), std_type(NULL, STD_TIME)))
445✔
2621
      sem_error(t, "reject interval must have type TIME but have %s",
1✔
2622
                type_pp(tree_type(t)));
2623

2624
   return true;
2625
}
2626

2627
static bool sem_check_signal_assign(tree_t t, nametab_t *tab)
4,172✔
2628
{
2629
   tree_t target = tree_target(t);
4,172✔
2630

2631
   if (!sem_check(target, tab))
4,172✔
2632
      return false;
2633

2634
   if (!sem_check_signal_target(target, tab))
4,169✔
2635
      return false;
2636

2637
   if (!sem_check_waveforms(t, target, tab))
4,156✔
2638
      return false;
2639

2640
   if (tree_has_reject(t) && !sem_check_reject(tree_reject(t), tab))
4,144✔
UNCOV
2641
      return false;
×
2642

2643
   return true;
2644
}
2645

2646
static bool sem_check_guard(tree_t t)
14✔
2647
{
2648
   assert(tree_kind(t) == T_REF);
14✔
2649

2650
   if (!sem_check_type(t, std_type(NULL, STD_BOOLEAN)))
14✔
2651
      sem_error(t, "guard signal must have BOOLEAN type but found %s",
1✔
2652
                type_pp(tree_type(t)));
2653

2654
   tree_t decl = tree_ref(t);
13✔
2655
   switch (tree_kind(decl)) {
13✔
2656
   case T_SIGNAL_DECL:
2657
   case T_IMPLICIT_SIGNAL:
2658
      break;
2659
   case T_PORT_DECL:
×
UNCOV
2660
      if (tree_class(decl) == C_SIGNAL)
×
2661
         break;
2662
      // Fall-through
2663
   default:
2664
      {
2665
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
1✔
2666
         diag_printf(d, "assignment guard must be a signal");
1✔
2667
         diag_hint(d, tree_loc(decl), "%s is a %s", istr(tree_ident(decl)),
1✔
2668
                   class_str(class_of(decl)));
2669
         diag_hint(d, tree_loc(t), "guarded statement");
1✔
2670
         diag_emit(d);
1✔
2671
         return false;
1✔
2672
      }
2673
   }
2674

UNCOV
2675
   return true;
×
2676
}
2677

2678
static bool sem_check_cond_assign(tree_t t, nametab_t *tab)
1,650✔
2679
{
2680
   tree_t target = tree_target(t);
1,650✔
2681

2682
   if (!sem_check(target, tab))
1,650✔
2683
      return false;
2684

2685
   if (!sem_check_signal_target(target, tab))
1,647✔
2686
      return false;
2687

2688
   type_t std_bool = std_type(NULL, STD_BOOLEAN);
1,641✔
2689

2690
   const int nconds = tree_conds(t);
1,641✔
2691
   for (int i = 0; i < nconds; i++) {
3,418✔
2692
      tree_t c = tree_cond(t, i);
1,783✔
2693

2694
      if (tree_has_value(c)) {
1,783✔
2695
         tree_t test = tree_value(c);
174✔
2696

2697
         if (!sem_check(test, tab))
174✔
2698
            return false;
2699

2700
         if (!type_eq(tree_type(test), std_bool))
174✔
2701
            sem_error(test, "type of condition must be BOOLEAN");
1✔
2702
      }
2703

2704
      assert(tree_stmts(c) == 1);
1,782✔
2705
      tree_t a = tree_stmt(c, 0);
1,782✔
2706

2707
      assert(tree_kind(a) == T_SIGNAL_ASSIGN);
1,782✔
2708
      assert(tree_target(a) == target);
1,782✔
2709

2710
      if (tree_has_reject(a) && !sem_check_reject(tree_reject(a), tab))
1,782✔
2711
         return false;
2712

2713
      if (!sem_check_waveforms(a, target, tab))
1,781✔
2714
         return false;
2715
   }
2716

2717
   return true;
2718
}
2719

2720
static bool sem_check_closely_related(type_t from, type_t to, tree_t where)
8,553✔
2721
{
2722
   if (type_eq(to, from))
8,553✔
2723
      return true;
2724

2725
   // Resolve both types to their base types
2726
   from = type_base_recur(from);
4,374✔
2727
   to   = type_base_recur(to);
4,374✔
2728

2729
   type_kind_t from_k = type_kind(from);
4,374✔
2730
   type_kind_t to_k   = type_kind(to);
4,374✔
2731

2732
   const bool from_num = (from_k == T_INTEGER) || (from_k == T_REAL);
4,374✔
2733
   const bool to_num   = (to_k == T_INTEGER) || (to_k == T_REAL);
4,374✔
2734

2735
   // Conversions are allowed between any abstract numeric types
2736
   if (from_num && to_num)
4,374✔
2737
      return true;
2738

2739
   // Suppress cascading errors
2740
   if (from_k == T_NONE || to_k == T_NONE)
2,341✔
2741
      return true;
2742

2743
   char *reason = NULL;
2,340✔
2744

2745
   if (from_k == T_ARRAY && to_k == T_ARRAY) {
2,340✔
2746
      const int from_dims = dimension_of(from);
2,315✔
2747
      const int to_dims = dimension_of(to);
2,315✔
2748

2749
      // Types must have same dimensionality
2750
      if (from_dims != to_dims) {
2,315✔
2751
         reason = xasprintf("%s has %d dimension%s but %s has %d",
1✔
2752
                            type_pp2(from, to), from_dims,
2753
                            from_dims == 1 ? "" : "s",
2754
                            type_pp2(to, from), to_dims);
2755
         goto not_closely_related;
1✔
2756
      }
2757

2758
      // Index types the same or closely related
2759
      for (int i = 0; i < from_dims; i++) {
4,628✔
2760
         type_t from_index = index_type_of(from, i);
2,315✔
2761
         type_t to_index = index_type_of(to, i);
2,315✔
2762

2763
         if (!sem_check_closely_related(from_index, to_index, NULL)) {
2,315✔
2764
            reason = xasprintf("%s index type of %s is %s which is not closely "
1✔
2765
                               "related to the %s index type of %s",
2766
                               ordinal_str(i + 1), type_pp2(from, to),
2767
                               type_pp(from_index), ordinal_str(i + 1),
2768
                               type_pp2(to, from));
2769
            goto not_closely_related;
1✔
2770
         }
2771
      }
2772

2773
      type_t from_e = type_elem(from);
2,313✔
2774
      type_t to_e = type_elem(to);
2,313✔
2775

2776
      if (standard() >= STD_08) {
2,313✔
2777
         // Element types must be closely related
2778
         if (!sem_check_closely_related(from_e, to_e, NULL)) {
1,751✔
2779
            reason = xasprintf("element type %s is not closely related to %s",
1✔
2780
                               type_pp2(from_e, to_e), type_pp2(to_e, from_e));
2781
            goto not_closely_related;
1✔
2782
         }
2783
      }
2784
      else {
2785
         // Element types must be the same
2786
         if (!type_eq(from_e, to_e)) {
562✔
2787
            reason = xasprintf("element type %s does not match %s",
1✔
2788
                               type_pp2(from_e, to_e), type_pp2(to_e, from_e));
2789
            goto not_closely_related;
1✔
2790
         }
2791
      }
2792

2793
      return true;
2,311✔
2794
   }
2795

2796
   if (from_k == T_RECORD && to_k == T_RECORD && standard() >= STD_19) {
25✔
2797
      // Each element of the target type must have a matching element in
2798
      // the from type
2799
      const int from_nf = type_fields(from);
21✔
2800
      const int to_nf = type_fields(to);
21✔
2801

2802
      for (int i = 0; i < to_nf; i++) {
68✔
2803
         tree_t to_f = type_field(to, i), from_f = NULL;
48✔
2804
         type_t to_ftype = tree_type(to_f);
48✔
2805
         ident_t name = tree_ident(to_f);
48✔
2806

2807
         for (int j = 0; j < from_nf && from_f == NULL; j++) {
130✔
2808
            tree_t f = type_field(from, j);
82✔
2809
            if (tree_ident(f) != name)
82✔
2810
               continue;
35✔
2811

2812
            type_t from_ftype = tree_type(f);
47✔
2813
            if (!sem_check_closely_related(from_ftype, to_ftype, NULL))
47✔
2814
               break;
2815

2816
            from_f = f;
2817
         }
2818

2819
         if (from_f != NULL)
48✔
2820
            continue;
47✔
2821

2822
         reason = xasprintf("field %s in record type %s has no matching "
1✔
2823
                            "element in type %s", istr(tree_ident(to_f)),
2824
                            type_pp2(to, from), type_pp2(from, to));
2825
         goto not_closely_related;
1✔
2826
      }
2827

2828
      return true;
2829
   }
2830

2831
 not_closely_related:
4✔
2832
   if (where != NULL) {
9✔
2833
      const loc_t *loc = tree_loc(where);
7✔
2834
      diag_t *d = diag_new(DIAG_ERROR, loc);
7✔
2835
      diag_printf(d, "conversion only allowed between closely related types");
7✔
2836
      if (reason != NULL)
7✔
2837
         diag_hint(d, loc, "%s", reason);
5✔
2838
      else
2839
         diag_hint(d, loc, "%s and %s are not closely related",
2✔
2840
                   type_pp2(from, to), type_pp2(to, from));
2841
      diag_lrm(d, STD_93, "7.3.5");
7✔
2842
      diag_emit(d);
7✔
2843

2844
      free(reason);
7✔
2845
   }
2846

2847
   return false;
2848
}
2849

2850
static bool sem_check_conversion(tree_t t, nametab_t *tab)
4,440✔
2851
{
2852
   // Type conversions are described in LRM 93 section 7.3.5
2853

2854
   tree_t value = tree_value(t);
4,440✔
2855
   if (!sem_check(value, tab))
4,440✔
2856
      return false;
2857

2858
   return sem_check_closely_related(tree_type(value), tree_type(t), t);
4,440✔
2859
}
2860

2861
static bool sem_check_compatible_view(tree_t formal, tree_t actual)
57✔
2862
{
2863
   type_t type = tree_type(formal);
57✔
2864

2865
   type_t elem_type = type;
57✔
2866
   if (tree_subkind(formal) == PORT_ARRAY_VIEW)
57✔
2867
      elem_type = type_elem(type);
6✔
2868

2869
   tree_t formal_view = tree_value(formal);
57✔
2870

2871
   tree_t actual_view = sem_check_view_target(actual);
57✔
2872
   if (actual_view != NULL) {
57✔
2873
      // Associating an interface with another interface: check the
2874
      // mode of each element is compatible
2875
      const int nfields = type_fields(elem_type);
12✔
2876
      for (int i = 0; i < nfields; i++) {
40✔
2877
         tree_t f = type_field(elem_type, i);
30✔
2878

2879
         bool formal_converse = false;
30✔
2880
         tree_t formal_elem = find_element_mode_indication(formal_view, f,
30✔
2881
                                                           &formal_converse);
2882

2883
         bool actual_converse = false;
30✔
2884
         tree_t actual_elem = find_element_mode_indication(actual_view, f,
30✔
2885
                                                           &actual_converse);
2886

2887
         const port_mode_t formal_mode =
30✔
2888
            converse_mode(formal_elem, formal_converse);
30✔
2889

2890
         const port_mode_t actual_mode =
30✔
2891
            converse_mode(actual_elem, actual_converse);
30✔
2892

2893
         if (formal_mode != actual_mode) {
30✔
2894
            diag_t *d = diag_new(DIAG_ERROR, tree_loc(actual));
2✔
2895
            diag_printf(d, "mode view indication of formal %s %s "
5✔
2896
                        "element %s is not compatible with actual",
2897
                        tree_kind(formal) == T_PORT_DECL ? "port" : "parameter",
2✔
2898
                        istr(tree_ident(formal)), istr(tree_ident(f)));
2899
            diag_hint(d, tree_loc(actual_view), "actual has mode %s from "
2✔
2900
                      "mode view indication on port %s",
2901
                      port_mode_str(actual_mode),
2902
                      istr(tree_ident(name_to_ref(actual))));
2903
            diag_hint(d, tree_loc(formal_view), "formal has mode %s",
2✔
2904
                      port_mode_str(formal_mode));
2905
            diag_emit(d);
2✔
2906
            return false;
2✔
2907
         }
2908
      }
2909
   }
2910

2911
   return true;
2912
}
2913

2914
static bool sem_check_call_args(tree_t t, tree_t decl, nametab_t *tab)
84,474✔
2915
{
2916
   const int nparams = tree_params(t);
84,474✔
2917
   const int nports  = tree_ports(decl);
84,474✔
2918

2919
   if (is_uninstantiated_subprogram(decl)) {
84,474✔
2920
      // Allow recursive calls to the same subprogram
2921
      tree_t sub = find_enclosing(tab, S_SUBPROGRAM);
5✔
2922
      if (sub != decl)
5✔
2923
         sem_error(t, "cannot call uninstantiated %s %s",
2✔
2924
                   class_str(class_of(decl)), istr(tree_ident(decl)));
2925
   }
2926

2927
   tree_t *map LOCAL = xcalloc_array(nports, sizeof(tree_t));
168,944✔
2928

2929
   bool have_named = false;
84,472✔
2930
   for (int i = 0; i < nparams; i++) {
239,531✔
2931
      tree_t param = tree_param(t, i), port = NULL;
155,106✔
2932
      type_t port_type = NULL;
155,106✔
2933
      bool partial = false;
155,106✔
2934
      int index = -1;
155,106✔
2935
      switch (tree_subkind(param)) {
155,106✔
2936
      case P_POS:
151,663✔
2937
         if (have_named)
151,663✔
2938
            sem_error(param, "positional parameters must precede named "
3✔
2939
                      "parameters");
2940
         else if ((index = tree_pos(param)) >= nports) {
151,660✔
2941
            diag_t *d = diag_new(DIAG_ERROR, tree_loc(param));
1✔
2942
            diag_printf(d, "too many positional parameters for subprogram %s",
1✔
2943
                        type_pp(tree_type(decl)));
2944
            diag_hint(d, tree_loc(param), "%s positional parameter",
1✔
2945
                      ordinal_str(index + 1));
2946
            diag_hint(d, tree_loc(decl), "%s %s has %d formal parameter%s",
2✔
2947
                      class_str(class_of(decl)), istr(tree_ident(decl)),
2948
                      nports, nports > 1 ? "s" : "");
2949
            diag_emit(d);
1✔
2950
            return false;
1✔
2951
         }
2952
         else {
2953
            port = tree_port(decl, index);
151,659✔
2954
            port_type = tree_type(port);
151,659✔
2955
         }
2956
         break;
151,659✔
2957

2958
      case P_NAMED:
3,443✔
2959
         {
2960
            have_named = true;
3,443✔
2961

2962
            tree_t name = tree_name(param);
3,443✔
2963
            tree_t ref = name_to_ref(name);
3,443✔
2964
            assert(ref != NULL);
3,443✔
2965

2966
            if ((partial = (ref != name))) {
3,443✔
2967
               tree_t value = tree_value(name);
58✔
2968
               if (tree_kind(value) != T_REF)
58✔
UNCOV
2969
                  sem_error(name, "sorry, this form of named parameter is "
×
2970
                            "not supported");
2971
            }
2972

2973
            ident_t id = tree_ident(ref);
3,443✔
2974
            for (int j = 0; j < nports; j++) {
13,198✔
2975
               tree_t p = tree_port(decl, j);
13,196✔
2976
               if (tree_ident(p) == id) {
13,196✔
2977
                  index = j;
2978
                  port = p;
2979
                  break;
2980
               }
2981
            }
2982

2983
            if (index == -1 || !tree_has_ref(ref)) {
3,443✔
2984
               // Should have generated an error during overload
2985
               // resolution
2986
               assert(error_count() > 0);
2✔
2987
               return false;
2988
            }
2989

2990
            // Set the ref again here because solve_types may have set it
2991
            // to the wrong overload
2992
            if (tree_ref(ref) != port)
3,441✔
2993
               tree_set_name(param, (name = change_ref(name, port)));
775✔
2994

2995
            port_type = tree_type(name);
3,441✔
2996
         }
2997
      }
2998

2999
      class_t class    = tree_class(port);
155,100✔
3000
      port_mode_t mode = tree_subkind(port);
155,100✔
3001

3002
      if (map[index] != NULL && (!partial || tree_kind(map[index]) == T_REF))
155,100✔
3003
         sem_error(param, "formal parameter %s already has an associated "
2✔
3004
                   "actual", istr(tree_ident(port)));
3005

3006
      map[index] = param;
155,098✔
3007

3008
      tree_t value = tree_value(param);
155,098✔
3009
      if (!sem_check(value, tab))
155,098✔
3010
         return false;
3011

3012
      if (!sem_check_type(value, port_type))
155,079✔
3013
         sem_error(value, "type of actual %s does not match formal %s type %s",
4✔
3014
                   type_pp2(tree_type(value), port_type),
3015
                   istr(tree_ident(port)),
3016
                   type_pp2(port_type, tree_type(value)));
3017

3018
      // LRM 08 sections 4.2.2.2 and 4.2.2.3
3019
      if (class == C_VARIABLE || class == C_SIGNAL) {
155,075✔
3020
         tree_t ref = name_to_ref(value);
5,839✔
3021
         if (ref == NULL || class_of(ref) != class) {
5,839✔
3022
            diag_t *d = diag_new(DIAG_ERROR, tree_loc(value));
7✔
3023
            diag_printf(d, "actual for formal %s with class %s must be "
11✔
3024
                        "a name denoting a %s", istr(tree_ident(port)),
3025
                        class == C_VARIABLE ? "VARIABLE" : "SIGNAL",
3026
                        class_str(class));
3027
            if (ref == NULL)
7✔
3028
               diag_hint(d, tree_loc(value), "actual designator is not a name");
3✔
3029
            else if (tree_has_ref(ref))
4✔
3030
               diag_hint(d, tree_loc(value), "object %s has class %s",
4✔
3031
                         istr(tree_ident(ref)), class_str(class_of(ref)));
3032
            diag_lrm(d, STD_08, class == C_SIGNAL ? "4.2.2.3" : "4.2.2.2");
10✔
3033
            diag_emit(d);
7✔
3034
            return false;
7✔
3035
         }
3036

3037
         // Check OUT and INOUT parameters can be assigned to
3038
         if (mode == PORT_OUT || mode == PORT_INOUT) {
5,832✔
3039
            tree_t decl = tree_ref(ref);
4,988✔
3040
            const tree_kind_t decl_kind = tree_kind(decl);
4,988✔
3041

3042
            if ((decl_kind == T_PARAM_DECL || decl_kind == T_PORT_DECL)
4,988✔
3043
                && tree_subkind(decl) == PORT_IN) {
925✔
3044
               const char *what =
2✔
3045
                  decl_kind == T_PARAM_DECL ? "parameter" : "port";
1✔
3046
               diag_t *d = diag_new(DIAG_ERROR, tree_loc(value));
1✔
3047
               diag_printf(d, "cannot associate %s %s of mode IN with "
1✔
3048
                           "formal %s of mode %s", what,
3049
                           istr(tree_ident(decl)), istr(tree_ident(port)),
3050
                           port_mode_str(mode));
3051
               diag_hint(d, tree_loc(decl), "%s declared with mode %s",
1✔
3052
                         istr(tree_ident(decl)),
3053
                         port_mode_str(tree_subkind(decl)));
1✔
3054
               diag_hint(d, tree_loc(value), "associated with %s %s %s here",
1✔
3055
                         port_mode_str(mode), what, istr(tree_ident(port)));
3056
               diag_emit(d);
1✔
3057
               return false;
1✔
3058
            }
3059
         }
3060
         else if ((mode == PORT_ARRAY_VIEW || mode == PORT_RECORD_VIEW)
844✔
3061
                  && !sem_check_compatible_view(port, value))
14✔
3062
            return false;
3063
      }
3064

3065
      if (class == C_SIGNAL && !sem_static_name(value, sem_globally_static)) {
155,066✔
3066
         diag_t *d = pedantic_diag(value);
4✔
3067
         if (d != NULL) {
4✔
3068
            diag_printf(d, "actual associated with signal parameter %s must be "
4✔
3069
                        "denoted by a static signal name",
3070
                        istr(tree_ident(port)));
3071
            diag_hint(d, tree_loc(value), "not a static signal name");
4✔
3072
            diag_lrm(d, STD_08, "4.2.2.3");
4✔
3073
            diag_lrm(d, STD_08, "8.1");
4✔
3074
            diag_emit(d);
4✔
3075
            return false;
4✔
3076
         }
3077
      }
3078

3079
      // Check IN and INOUT parameters can be read
3080
      if (tree_kind(t) != T_ATTR_REF) {
155,062✔
3081
         const port_mode_t mode = tree_subkind(port);
155,062✔
3082
         if (mode == PORT_IN || mode == PORT_INOUT) {
155,062✔
3083
            if (!sem_readable(value))
152,954✔
3084
               return false;
3085
         }
3086
      }
3087
   }
3088

3089
   for (int i = 0; i < nports; i++) {
243,365✔
3090
      if (map[i] == NULL) {
158,943✔
3091
         tree_t port = tree_port(decl, i);
3,925✔
3092
         if (!tree_has_value(port))
3,925✔
3093
            sem_error(t, "missing actual for formal parameter %s without "
2✔
3094
                      "default value", istr(tree_ident(port)));
3095
         else {
3096
            const port_mode_t mode = tree_subkind(port);
3,923✔
3097
            if (mode == PORT_ARRAY_VIEW || mode == PORT_RECORD_VIEW)
3,923✔
3098
               sem_error(t, "missing actual for formal parameter %s with "
158,941✔
3099
                         "mode view indication %s", istr(tree_ident(port)),
3100
                         type_pp(tree_type(tree_value(port))));
3101
         }
3102
      }
3103
   }
3104

3105
   return true;
3106
}
3107

3108
static bool sem_check_fcall(tree_t t, nametab_t *tab)
77,075✔
3109
{
3110
   if (!tree_has_ref(t))
77,075✔
3111
      return false;
3112

3113
   if (tree_kind(t) == T_PROT_FCALL && tree_has_name(t)) {
76,993✔
3114
      tree_t name = tree_name(t);
774✔
3115
      if (!sem_check(name, tab))
774✔
3116
         return false;
3117
   }
3118

3119
   tree_t decl = tree_ref(t);
76,993✔
3120

3121
   // Pure function may not call an impure function
3122
   tree_t sub = find_enclosing(tab, S_SUBPROGRAM);
76,993✔
3123

3124
   const bool pure_call_to_impure =
153,986✔
3125
      sub != NULL && tree_kind(sub) == T_FUNC_BODY
36,743✔
3126
      && !(tree_flags(sub) & TREE_F_IMPURE)
27,073✔
3127
      && (tree_flags(decl) & TREE_F_IMPURE);
103,326✔
3128

3129
   if (pure_call_to_impure) {
76,993✔
3130
      diag_t *d = pedantic_diag(t);
2✔
3131
      if (d != NULL) {
2✔
3132
         diag_printf(d, "pure function %s cannot call impure function %s",
2✔
3133
                     istr(tree_ident(sub)), istr(tree_ident(decl)));
3134
         diag_emit(d);
2✔
3135
      }
3136
   }
3137

3138
   if (!sem_check_call_args(t, decl, tab))
76,993✔
3139
      return false;
3140

3141
   if (sem_locally_static(t))
76,965✔
3142
      tree_set_flag(t, TREE_F_LOCALLY_STATIC | TREE_F_GLOBALLY_STATIC);
11,832✔
3143
   else if (sem_globally_static(t))
65,133✔
3144
      tree_set_flag(t, TREE_F_GLOBALLY_STATIC);
12,568✔
3145

3146
   return true;
3147
}
3148

3149
static bool sem_check_pcall(tree_t t, nametab_t *tab)
7,535✔
3150
{
3151
   if (!tree_has_ref(t))
7,535✔
3152
      return false;
3153

3154
   const bool is_protected = (tree_kind(t) == T_PROT_PCALL);
7,500✔
3155
   if (is_protected && tree_has_name(t)
7,500✔
3156
       && !sem_check(tree_name(t), tab))
185✔
3157
      return false;
3158

3159
   tree_t decl = tree_ref(t);
7,499✔
3160

3161
   const tree_kind_t kind = tree_kind(decl);
7,499✔
3162
   if (kind == T_FUNC_DECL || kind == T_FUNC_BODY)
7,499✔
3163
      sem_error(t, "function %s cannot be called as a procedure",
1✔
3164
                istr(tree_ident2(t)));
3165
   else if (kind != T_PROC_DECL && kind != T_PROC_BODY) {
7,498✔
3166
      // All other errors should be caught at parsing stage
3167
      return false;
3168
   }
3169

3170
   if (!sem_check_call_args(t, decl, tab))
7,481✔
3171
      return false;
3172

3173
   const tree_flags_t flags = tree_flags(decl);
7,457✔
3174

3175
   const bool never_waits = is_protected || !!(flags & TREE_F_NEVER_WAITS);
7,457✔
3176
   const bool has_wait = !is_protected && !!(flags & TREE_F_HAS_WAIT);
7,457✔
3177

3178
   assert(!never_waits || !has_wait);
7,457✔
3179

3180
   tree_t sub = find_enclosing(tab, S_SUBPROGRAM);
7,457✔
3181
   if (sub != NULL) {
7,457✔
3182
      if (!never_waits)
3,137✔
3183
         tree_clear_flag(sub, TREE_F_NEVER_WAITS);
160✔
3184

3185
      if (has_wait)
3,137✔
3186
         tree_set_flag(sub, TREE_F_HAS_WAIT);
26✔
3187

3188
      if (flags & TREE_F_IMPURE_FILE)
3,137✔
3189
         tree_set_flag(sub, TREE_F_IMPURE_FILE);
168✔
3190

3191
      if (flags & TREE_F_IMPURE_SHARED)
3,137✔
3192
         tree_set_flag(sub, TREE_F_IMPURE_SHARED);
2✔
3193

3194
      const bool in_func = tree_kind(sub) == T_FUNC_BODY;
3,137✔
3195
      const bool in_pure_func = in_func && !(tree_flags(sub) & TREE_F_IMPURE);
3,137✔
3196

3197
      if (has_wait && in_func)
3,137✔
3198
         sem_error(t, "function %s cannot call procedure %s which contains "
2✔
3199
                   "a wait statement", istr(tree_ident(sub)),
3200
                   istr(tree_ident(decl)));
3201
      else if ((flags & TREE_F_IMPURE_FILE) && in_pure_func)
3,135✔
3202
         sem_error(t, "pure function %s cannot call procedure %s which "
1✔
3203
                   "references a file object", istr(tree_ident(sub)),
3204
                   istr(tree_ident(decl)));
3205
      else if ((flags & TREE_F_IMPURE_SHARED) && in_pure_func)
3,134✔
3206
         sem_error(t, "pure function %s cannot call procedure %s which "
1✔
3207
                   "references a shared variable", istr(tree_ident(sub)),
3208
                   istr(tree_ident(decl)));
3209
   }
3210

3211
   return true;
3212
}
3213

3214
static bool sem_check_wait(tree_t t, nametab_t *tab)
7,806✔
3215
{
3216
   if (tree_has_delay(t)) {
7,806✔
3217
      type_t std_time = std_type(NULL, STD_TIME);
4,049✔
3218
      tree_t delay = tree_delay(t);
4,049✔
3219

3220
      if (!sem_check(delay, tab))
4,049✔
3221
         return false;
3222

3223
      if (!sem_check_type(delay, std_time))
4,048✔
3224
         sem_error(delay, "type of delay must be %s but have %s",
2✔
3225
                   type_pp(std_time), type_pp(tree_type(delay)));
3226
   }
3227

3228
   if (tree_has_value(t)) {
7,803✔
3229
      type_t std_bool = std_type(NULL, STD_BOOLEAN);
232✔
3230
      tree_t value = tree_value(t);
232✔
3231

3232
      if (!sem_check(value, tab))
232✔
3233
         return false;
3234

3235
      if (!sem_check_type(value, std_bool))
232✔
3236
         sem_error(value, "type of condition must be BOOLEAN but have %s",
1✔
3237
                   type_pp(tree_type(value)));
3238
   }
3239

3240
   if (find_enclosing(tab, S_PROTECTED))
7,802✔
3241
      sem_error(t, "wait statement not allowed in protected subprogram body");
1✔
3242

3243
   tree_t sub = find_enclosing(tab, S_SUBPROGRAM);
7,801✔
3244
   if (sub != NULL) {
7,801✔
3245
      if (tree_kind(sub) == T_FUNC_BODY)
348✔
3246
         sem_error(t, "wait statement not allowed in function body");
1✔
3247

3248
      tree_clear_flag(sub, TREE_F_NEVER_WAITS);
347✔
3249
      tree_set_flag(sub, TREE_F_HAS_WAIT);
347✔
3250
   }
3251

3252
   return sem_check_sensitivity(t, tab);
7,800✔
3253
}
3254

3255
static bool sem_check_assert(tree_t t, nametab_t *tab)
17,692✔
3256
{
3257
   // Rules for asserion statements are in LRM 93 section 8.2
3258

3259
   type_t std_bool     = std_type(NULL, STD_BOOLEAN);
17,692✔
3260
   type_t std_string   = std_type(NULL, STD_STRING);
17,692✔
3261
   type_t std_severity = std_type(NULL, STD_SEVERITY_LEVEL);
17,692✔
3262

3263
   tree_t value    = tree_has_value(t) ? tree_value(t) : NULL;
17,692✔
3264
   tree_t severity = tree_severity(t);
17,692✔
3265
   tree_t message  = tree_has_message(t) ? tree_message(t) : NULL;
17,692✔
3266

3267
   if (value != NULL && !sem_check(value, tab))
17,692✔
3268
      return false;
3269

3270
   if (!sem_check(severity, tab))
17,653✔
3271
      return false;
3272

3273
   if (message != NULL && !sem_check(message, tab))
17,653✔
3274
      return false;
3275

3276
   if (value != NULL && !sem_check_type(value, std_bool))
17,643✔
3277
      sem_error(value, "type of assertion expression must "
1✔
3278
                "be %s but is %s", type_pp(std_bool),
3279
                type_pp(tree_type(value)));
3280

3281
   if (!sem_check_type(severity, std_severity))
17,642✔
3282
      sem_error(severity, "type of severity must be %s but is %s",
1✔
3283
                type_pp(std_severity),
3284
                type_pp(tree_type(severity)));
3285

3286
   if (message != NULL && !sem_check_type(message, std_string))
17,641✔
3287
      sem_error(message, "type of message be %s but is %s",
1✔
3288
                type_pp(std_string),
3289
                type_pp(tree_type(message)));
3290

3291
   return true;
3292
}
3293

3294
static bool sem_check_string_literal(tree_t t)
23,732✔
3295
{
3296
   // String literals are in LRM 93 section 7.3.1
3297

3298
   type_t type = tree_type(t);
23,732✔
3299
   type_t elem = type_base_recur(type_elem(type));
23,732✔
3300

3301
   if (type_is_none(elem))
23,732✔
3302
      return false;
3303

3304
   const int nlits = type_enum_literals(elem);
23,732✔
3305
   const int nchars = tree_chars(t);
23,732✔
3306
   for (int i = 0; i < nchars; i++) {
358,554✔
3307
      tree_t ch = tree_char(t, i);
334,823✔
3308

3309
      ident_t ch_i = tree_ident(ch);
334,823✔
3310
      bool valid = false;
334,823✔
3311
      for (int j = 0; !valid && (j < nlits); j++) {
24,371,200✔
3312
         tree_t lit = type_enum_literal(elem, j);
24,036,400✔
3313
         if (ch_i == tree_ident(lit))
24,036,400✔
3314
            valid = true;
334,822✔
3315
      }
3316

3317
      if (!valid)
334,823✔
3318
         sem_error(t, "invalid character %s in string literal of type %s",
334,823✔
3319
                   istr(ch_i), type_pp(type));
3320
   }
3321

3322
   return true;
3323
}
3324

3325
static bool sem_check_literal(tree_t t)
81,743✔
3326
{
3327
   type_t type = tree_type(t);
81,743✔
3328
   if (type_is_none(type))
81,743✔
3329
      return false;
2✔
3330

3331
   return true;
3332
}
3333

3334
static bool sem_check_array_aggregate(tree_t t, nametab_t *tab)
5,879✔
3335
{
3336
   type_t composite_type = tree_type(t);
5,879✔
3337
   type_t base_type = type_base_recur(composite_type);
5,879✔
3338

3339
   const bool unconstrained = type_is_unconstrained(composite_type);
5,879✔
3340

3341
   type_t elem_type = NULL;
5,879✔
3342
   const int ndims = dimension_of(composite_type);
5,879✔
3343
   if (ndims == 1)
5,879✔
3344
      elem_type = type_elem(base_type);
5,610✔
3345
   else {
3346
      // Higher dimensions must be specified with a sub-aggregate or
3347
      // string literal
3348
      tree_t a0 = tree_value(tree_assoc(t, 0));
269✔
3349
      const tree_kind_t a0_kind = tree_kind(a0);
269✔
3350
      if (a0_kind != T_AGGREGATE && a0_kind != T_STRING)
269✔
3351
         sem_error(a0, "second dimension of %d dimensional array type %s must "
1✔
3352
                   "be specified by a sub-aggregate, string, or bit-string "
3353
                   "literal", ndims, type_pp(composite_type));
3354

3355
      // The parser will have constructed a type with ndims - 1
3356
      // dimensions.
3357
      elem_type = tree_type(tree_value(tree_assoc(t, 0)));
268✔
3358

3359
      if (!type_is_unconstrained(elem_type)) {
268✔
3360
         if (!sem_check_array_dims(elem_type, NULL, tab))
265✔
3361
            return false;
3362
      }
3363
   }
3364

3365
   type_t index_type = index_type_of(composite_type, 0);
5,878✔
3366

3367
   bool have_named = false;
5,878✔
3368
   bool have_pos = false;
5,878✔
3369

3370
   const int nassocs = tree_assocs(t);
5,878✔
3371
   for (int i = 0; i < nassocs; i++) {
36,134✔
3372
      tree_t a = tree_assoc(t, i);
30,267✔
3373

3374
      const assoc_kind_t akind = tree_subkind(a);
30,267✔
3375
      switch (akind) {
30,267✔
3376
      case A_RANGE:
495✔
3377
         {
3378
            tree_t r = tree_range(a, 0);
495✔
3379
            if (!sem_check_discrete_range(r, index_type, tab))
495✔
3380
               return false;
3381

3382
            have_named = true;
3383
         }
3384
         break;
3385

3386
      case A_NAMED:
1,094✔
3387
         {
3388
            tree_t name = tree_name(a);
1,094✔
3389

3390
            if (!sem_check(name, tab))
1,094✔
3391
               return false;
3392

3393
            if (!sem_check_type(name, index_type))
1,094✔
3394
               sem_error(name, "type of array aggregate choice %s does not "
1✔
3395
                         "match %s index type %s", type_pp(tree_type(name)),
3396
                         type_pp(composite_type), type_pp(index_type));
3397

3398
            have_named = true;
3399
         }
3400
         break;
3401

3402
      case A_POS:
26,460✔
3403
         have_pos = true;
26,460✔
3404
         break;
26,460✔
3405

3406
      case A_OTHERS:
2,218✔
3407
         if (unconstrained)
2,218✔
3408
            sem_error(a, "index range of array aggregate with others choice "
2✔
3409
                      "cannot be determined from the context");
3410
         break;
3411
      }
3412

3413
      tree_t value = tree_value(a);
30,261✔
3414

3415
      if (!sem_check(value, tab))
30,261✔
3416
         return false;
3417

3418
      if (!sem_check_type(value, elem_type)) {
30,259✔
3419
         // LRM 08 section 9.3.3.3 allows the association to be of the
3420
         // base aggregate type as well
3421
         const bool allow_slice =
286✔
3422
            ndims == 1 && standard() >= STD_08
143✔
3423
            && (akind == A_POS || akind == A_RANGE);
286✔
3424

3425
         if (allow_slice && !sem_check_type(value, composite_type))
143✔
3426
            sem_error(value, "type of %s association %s does not match "
1✔
3427
                      "aggregate element type %s or the aggregate type "
3428
                      "itself %s", assoc_kind_str(akind),
3429
                      type_pp(tree_type(value)), type_pp(elem_type),
3430
                      type_pp(composite_type));
3431
         else if (!allow_slice)
142✔
3432
            sem_error(value, "type of %s association %s does not match "
30,258✔
3433
                      "aggregate element type %s", assoc_kind_str(akind),
3434
                      type_pp(tree_type(value)), type_pp(elem_type));
3435
      }
3436
   }
3437

3438
   // Named and positional associations cannot be mixed in array
3439
   // aggregates
3440

3441
   if (have_named && have_pos)
5,867✔
3442
      sem_error(t, "named and positional associations cannot be "
2✔
3443
                "mixed in array aggregates");
3444

3445
   // If a choice is not locally static then it must be the only element
3446

3447
   if (have_named && nassocs > 1) {
5,865✔
3448
      for (int i = 0; i < nassocs; i++) {
1,568✔
3449
         tree_t a = tree_assoc(t, i);
1,249✔
3450
         tree_t choice = NULL;
1,249✔
3451
         switch (tree_subkind(a)) {
1,249✔
3452
         case A_NAMED: choice = tree_name(a); break;
991✔
3453
         case A_RANGE: choice = tree_range(a, 0); break;
98✔
3454
         }
3455

3456
         if (choice && !sem_locally_static(choice))
1,089✔
3457
            sem_error(choice, "a choice that is not locally static is allowed"
1,249✔
3458
                      " only if the array aggregate contains a single element"
3459
                      " association");
3460
      }
3461
   }
3462

3463
   return true;
3464
}
3465

3466
static bool sem_check_record_aggregate(tree_t t, nametab_t *tab)
2,028✔
3467
{
3468
   // Checks for record aggregates are given in LRM 93 section 7.3.2.1
3469

3470
   type_t composite_type = tree_type(t);
2,028✔
3471
   type_t base_type = type_base_recur(composite_type);
2,028✔
3472

3473
   const int nfields = type_fields(base_type);
2,028✔
3474
   int pos = 0;
2,028✔
3475

3476
   LOCAL_BIT_MASK have;
4,056✔
3477
   mask_init(&have, nfields);
2,028✔
3478

3479
   const int nassocs = tree_assocs(t);
2,028✔
3480
   for (int i = 0; i < nassocs; i++) {
7,163✔
3481
      tree_t a = tree_assoc(t, i);
5,148✔
3482
      int f = -1;
5,148✔
3483

3484
      switch (tree_subkind(a)) {
5,148✔
3485
      case A_NAMED:
822✔
3486
         {
3487
            tree_t name = tree_name(a);
822✔
3488
            if (tree_kind(name) != T_REF)
822✔
3489
               sem_error(name, "association choice must be a field name");
1✔
3490
            else if (!tree_has_ref(name))
821✔
3491
               return false;   // Was parse error
3492

3493
            tree_t fdecl = tree_ref(name);
819✔
3494
            if (tree_kind(fdecl) != T_FIELD_DECL)
819✔
3495
               return false;   // Was parse error
3496

3497
            f = tree_pos(fdecl);
817✔
3498
         }
3499
         break;
817✔
3500

3501
      case A_POS:
4,215✔
3502
         {
3503
            if (pos >= nfields)
4,215✔
3504
               sem_error(a, "%d positional associations given but record type"
1✔
3505
                         " %s only has %d fields", pos + 1,
3506
                         type_pp(composite_type), nfields);
3507

3508
            f = pos++;
4,214✔
3509
         }
3510
         break;
4,214✔
3511

3512
      case A_OTHERS:
3513
         f = -1;
3514
         break;
3515

3516
      case A_RANGE:
2✔
3517
         sem_error(a, "range association invalid in record aggregate");
2✔
3518
      }
3519

3520
      int nmatched = 0;
5,140✔
3521
      for (int j = 0; j < nfields; j++) {
25,197✔
3522
         if ((f != -1) && (f != j))
20,061✔
3523
            continue;
14,676✔
3524

3525
         tree_t field = type_field(base_type, j);
5,385✔
3526
         type_t field_type = tree_type(field);
5,385✔
3527

3528
         if (mask_test(&have, j)) {
5,385✔
3529
            if (f == -1)
42✔
3530
               continue;
40✔
3531

3532
            tree_t ak = NULL;
3533
            for (int k = 0; k < i; k++) {
2✔
3534
               ak = tree_assoc(t, k);
2✔
3535
               if (tree_subkind(ak) == A_POS && tree_pos(ak) == j)
2✔
3536
                  break;
3537
               else if (tree_pos(tree_ref(tree_name(ak))) == j)
1✔
3538
                  break;
3539
            }
3540
            assert(ak != NULL);
2✔
3541

3542
            diag_t *d = diag_new(DIAG_ERROR, tree_loc(a));
2✔
3543
            diag_printf(d, "field %s was already given a value by earlier "
2✔
3544
                        "%s choice", istr(tree_ident(field)),
3545
                        assoc_kind_str(tree_subkind(ak)));
2✔
3546
            diag_hint(d, tree_loc(ak), "first choice associated with field %s",
2✔
3547
                      istr(tree_ident(field)));
3548
            diag_hint(d, tree_loc(a), "duplicate choice here");
2✔
3549
            diag_emit(d);
2✔
3550
            return false;
2✔
3551
         }
3552

3553
         tree_t value = tree_value(a);
5,343✔
3554

3555
         if (!sem_check(value, tab))
5,343✔
3556
            return false;
3557

3558
         if (!sem_check_type(value, field_type))
5,343✔
3559
            sem_error(value, "type of value %s does not match type %s"
2✔
3560
                      " of field %s",
3561
                      type_pp2(tree_type(value), field_type),
3562
                      type_pp2(field_type, tree_type(value)),
3563
                      istr(tree_ident(field)));
3564

3565
         mask_set(&have, j);
5,341✔
3566
         nmatched++;
5,341✔
3567
      }
3568

3569
      if (f == -1 && nmatched == 0)
5,136✔
3570
         sem_error(a, "others association must represent at least one element");
5,136✔
3571
   }
3572

3573
   for (int i = 0; i < nfields; i++) {
7,346✔
3574
      if (!mask_test(&have, i)) {
5,333✔
3575
         tree_t field = type_field(base_type, i);
2✔
3576
         sem_error(t, "field %s does not have a value",
5,333✔
3577
                   istr(tree_ident(field)));
3578
      }
3579
   }
3580

3581
   return true;
3582
}
3583

3584
static bool sem_check_aggregate(tree_t t, nametab_t *tab)
7,916✔
3585
{
3586
   // Rules for aggregates are in LRM 93 section 7.3.2
3587

3588
   type_t composite_type = tree_type(t);
7,916✔
3589

3590
   if (type_is_none(composite_type))
7,916✔
3591
      return false;
3592
   assert(type_is_composite(composite_type));
7,910✔
3593

3594
   // All positional associations must appear before named associations
3595
   // and those must appear before any others association
3596

3597
   enum { POS, NAMED, OTHERS } state = POS;
7,910✔
3598

3599
   const int nassocs = tree_assocs(t);
7,910✔
3600
   for (int i = 0; i < nassocs; i++) {
43,336✔
3601
      tree_t a = tree_assoc(t, i);
35,429✔
3602

3603
      switch (tree_subkind(a)) {
35,429✔
3604
      case A_POS:
30,678✔
3605
         if (state > POS)
30,678✔
3606
            sem_error(a, "positional associations must appear "
1✔
3607
                      "first in aggregate");
3608
         break;
3609

3610
      case A_NAMED:
2,420✔
3611
      case A_RANGE:
3612
         if (state > NAMED)
2,420✔
3613
            sem_error(a, "named association must not follow "
1✔
3614
                      "others association in aggregate");
3615
         state = NAMED;
3616
         break;
3617

3618
      case A_OTHERS:
2,331✔
3619
         if (state == OTHERS)
2,331✔
3620
            sem_error(a, "only a single others association "
1✔
3621
                      "allowed in aggregate");
3622
         state = OTHERS;
3623
         break;
3624
      }
3625
   }
35,426✔
3626

3627
   if (type_is_array(composite_type))
7,907✔
3628
      return sem_check_array_aggregate(t, tab);
5,879✔
3629
   else
3630
      return sem_check_record_aggregate(t, tab);
2,028✔
3631
}
3632

3633
static bool sem_check_ref(tree_t t, nametab_t *tab)
170,447✔
3634
{
3635
   if (!tree_has_ref(t))
170,447✔
3636
      return false;
3637

3638
   type_t type = get_type_or_null(t);
170,412✔
3639
   if (type != NULL && type_is_none(type))
170,412✔
3640
      return false;
3641

3642
   tree_t decl = tree_ref(t);
170,406✔
3643
   const tree_kind_t kind = tree_kind(decl);
170,406✔
3644

3645
   switch (kind) {
170,406✔
3646
   case T_PORT_DECL:
3647
   case T_VAR_DECL:
3648
   case T_SIGNAL_DECL:
3649
   case T_FILE_DECL:
3650
   case T_CONST_DECL:
3651
   case T_ENUM_LIT:
3652
   case T_UNIT_DECL:
3653
   case T_FUNC_DECL:
3654
   case T_FUNC_BODY:
3655
   case T_FUNC_INST:
3656
   case T_PROC_DECL:
3657
   case T_PROC_BODY:
3658
   case T_PROC_INST:
3659
   case T_IMPLICIT_SIGNAL:
3660
   case T_PARAM_DECL:
3661
      break;
3662

3663
   case T_ALIAS:
1,499✔
3664
      {
3665
         switch (class_of(decl)) {
1,499✔
3666
         case C_VARIABLE:
3667
         case C_SIGNAL:
3668
         case C_CONSTANT:
3669
         case C_LITERAL:
3670
            break;
3671

3672
         case C_DEFAULT:
3673
            return false;   // Must have been an earlier parse error
3674

3675
         default:
1✔
3676
            sem_error(t, "invalid use of alias %s", istr(tree_ident(decl)));
1✔
3677
         }
3678
      }
3679
      break;
3680

3681
   case T_GENERIC_DECL:
5,353✔
3682
      if (tree_class(decl) == C_CONSTANT)
5,353✔
3683
         break;
3684
      // Fall-through
3685

3686
   default:
3687
      {
3688
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
2✔
3689
         diag_printf(d, "invalid use of %s %s", class_str(class_of(decl)),
2✔
3690
                     istr(tree_ident(t)));
3691
         diag_hint(d, tree_loc(decl), "%s declared here",
2✔
3692
                   istr(tree_ident(decl)));
3693
         diag_emit(d);
2✔
3694
         return false;
2✔
3695
      }
3696
   }
3697

3698
   tree_t sub = find_enclosing(tab, S_SUBPROGRAM);
170,403✔
3699
   if (sub != NULL) {
170,403✔
3700
      if (kind == T_FILE_DECL)
87,868✔
3701
         tree_set_flag(sub, TREE_F_IMPURE_FILE);
275✔
3702
      else if (kind == T_VAR_DECL && (tree_flags(decl) & TREE_F_SHARED))
87,593✔
3703
         tree_set_flag(sub, TREE_F_IMPURE_SHARED);
56✔
3704
   }
3705

3706
   return true;
3707
}
3708

3709
static bool sem_check_record_ref(tree_t t, nametab_t *tab)
8,161✔
3710
{
3711
   tree_t value = tree_value(t);
8,161✔
3712
   if (!sem_check(value, tab))
8,161✔
3713
      return false;
3714

3715
   type_t value_type = tree_type(value);
8,157✔
3716

3717
   if (type_is_none(value_type))
8,157✔
3718
      return false;
3719
   else if (!type_is_record(value_type))
8,157✔
UNCOV
3720
      sem_error(value, "expected record type but found %s%s",
×
3721
                type_is_incomplete(value_type) ? "incomplete type " : "",
3722
                type_pp(value_type));
3723

3724
   return true;
3725
}
3726

3727
static bool sem_check_array_ref(tree_t t, nametab_t *tab)
13,465✔
3728
{
3729
   tree_t value = tree_value(t);
13,465✔
3730
   if (!sem_check(value, tab))
13,465✔
3731
      return false;
3732

3733
   type_t type = tree_type(tree_value(t));
13,464✔
3734

3735
   if (!type_is_array(type))
13,464✔
3736
      return false;  // Checked earlier
3737

3738
   const int nindex  = dimension_of(type);
13,460✔
3739
   const int nparams = tree_params(t);
13,460✔
3740

3741
   if (nparams != nindex)
13,460✔
3742
      sem_error(t, "array %s has %d dimensions but %d indices given",
2✔
3743
                istr(tree_ident(value)), nindex, nparams);
3744

3745
   bool ok = true;
3746
   for (int i = 0; i < nparams; i++) {
28,533✔
3747
      tree_t p = tree_param(t, i);
15,077✔
3748
      assert(tree_subkind(p) == P_POS);
15,077✔
3749

3750
      type_t expect = index_type_of(type, i);
15,077✔
3751
      tree_t value = tree_value(p);
15,077✔
3752

3753
      ok = sem_check(value, tab) && ok;
15,077✔
3754

3755
      if (ok && !sem_check_type(value, expect))
15,077✔
3756
         sem_error(value, "type of index %s does not match type of "
15,077✔
3757
                   "array dimension %s",
3758
                   type_pp(tree_type(value)),
3759
                   type_pp(expect));
3760
   }
3761

3762
   return ok;
3763
}
3764

3765
static bool sem_check_array_slice(tree_t t, nametab_t *tab)
1,949✔
3766
{
3767
   if (!sem_check(tree_value(t), tab))
1,949✔
3768
      return false;
3769

3770
   type_t array_type = tree_type(tree_value(t));
1,948✔
3771

3772
   if (type_is_none(array_type))
1,948✔
3773
      return false;
3774
   else if (!type_is_array(array_type))
1,948✔
3775
      sem_error(t, "type of slice prefix %s is not an array",
2✔
3776
                type_pp(array_type));
3777

3778
   tree_t r = tree_range(t, 0);
1,946✔
3779
   if (!sem_check_discrete_range(r, index_type_of(array_type, 0), tab))
1,946✔
3780
      return false;
3781

3782
   const bool unconstrained = type_is_unconstrained(array_type);
1,936✔
3783
   const range_kind_t prefix_dir =
3,872✔
3784
      unconstrained ? RANGE_EXPR : direction_of(array_type, 0);
1,936✔
3785

3786
   const range_kind_t rkind = tree_subkind(r);
1,936✔
3787
   const bool wrong_dir =
3,872✔
3788
      !unconstrained
1,936✔
3789
      && rkind != prefix_dir
1,936✔
3790
      && (rkind == RANGE_TO || rkind == RANGE_DOWNTO)
121✔
3791
      && (prefix_dir == RANGE_TO || prefix_dir == RANGE_DOWNTO);
1,936✔
3792

3793
   if (wrong_dir) {
1,936✔
3794
      const char *text[] = { "TO", "DOWNTO", "?", "??", "???" };
2✔
3795
      sem_error(t, "range direction of slice %s does not match prefix %s",
2✔
3796
                text[rkind], text[prefix_dir]);
3797
   }
3798

3799
   return true;
3800
}
3801

3802
static bool sem_check_valid_implicit_signal(tree_t t, nametab_t *tab)
49✔
3803
{
3804
   // Certain attributes are illegal inside a subprogram according to LRM
3805
   // 93 section 2.1.1.2
3806

3807
   if (find_enclosing(tab, S_SUBPROGRAM) != NULL)
49✔
3808
      sem_error(t, "implicit signal %s cannot be used in a "
2✔
3809
                "subprogram body", istr(tree_ident(t)));
3810

3811
   return true;
3812
}
3813

3814
static bool sem_check_signal_attr(tree_t t)
764✔
3815
{
3816
   tree_t name = tree_name(t);
765✔
3817

3818
   if (tree_kind(name) == T_ATTR_REF)
765✔
3819
      return sem_check_signal_attr(name);
3820

3821
   tree_t ref = name_to_ref(name);
764✔
3822
   if (ref != NULL && class_of(ref) == C_SIGNAL)
764✔
3823
      return true;
3824

3825
   sem_error(t, "prefix of attribute %s must denote a signal",
2✔
3826
             istr(tree_ident(t)));
3827
}
3828

3829
static bool sem_check_driving(tree_t t)
58✔
3830
{
3831
   // See LRM 08 section 16.2.4 for special rules about 'DRIVING and
3832
   // 'DRIVING_VALUE
3833

3834
   if (!sem_check_signal_attr(t))
58✔
3835
      return false;
3836

3837
   tree_t ref = name_to_ref(tree_name(t));
57✔
3838
   if (ref == NULL || !tree_has_ref(ref))
57✔
UNCOV
3839
      return false;
×
3840

3841
   tree_t decl = tree_ref(ref);
57✔
3842
   if (tree_kind(decl) == T_PORT_DECL) {
57✔
3843
      const port_mode_t mode = tree_subkind(decl);
2✔
3844
      if (mode != PORT_OUT && mode != PORT_INOUT && mode != PORT_BUFFER)
2✔
3845
         sem_error(t, "prefix of attribute %s must denote a signal or a port "
1✔
3846
                   "with mode IN, INOUT, or BUFFER", istr(tree_ident(t)));
3847
   }
3848

3849
   // TODO: check within a process
3850

3851
   return true;
3852
}
3853

3854
static bool sem_check_attr_param(tree_t t, type_t expect, int min, int max,
1,255✔
3855
                                 nametab_t *tab)
3856
{
3857
   const int nparams = tree_params(t);
1,255✔
3858
   if (nparams == 0 && min > 0)
1,255✔
UNCOV
3859
      sem_error(t, "attribute %s requires a parameter", istr(tree_ident(t)));
×
3860
   else if (nparams > max)
1,255✔
UNCOV
3861
      sem_error(t, "too many parameters for attribute %s", istr(tree_ident(t)));
×
3862
   else if (nparams == 1) {
1,255✔
3863
      tree_t dim = tree_value(tree_param(t, 0));
1,196✔
3864
      if (!sem_check(dim, tab))
1,196✔
3865
         return false;
3866

3867
      tree_t value = tree_value(tree_param(t, 0));
1,195✔
3868
      if (!sem_check_type(value, expect))
1,195✔
UNCOV
3869
         sem_error(t, "expected type %s for attribute %s parameter but "
×
3870
                   "have %s", type_pp(expect), istr(tree_ident(t)),
3871
                   type_pp(tree_type(value)));
3872
   }
3873

3874
   return true;
3875
}
3876

3877
static bool sem_check_dimension_attr(tree_t t, nametab_t *tab)
20,016✔
3878
{
3879
   const int nparams = tree_params(t);
20,016✔
3880
   if (nparams == 0)
20,016✔
3881
      return true;
3882

3883
   assert(nparams == 1);   // Enforced by parser
1,643✔
3884

3885
   tree_t dim = tree_value(tree_param(t, 0));
1,643✔
3886
   if (!sem_check(dim, tab))
1,643✔
3887
      return false;
3888

3889
   // The parameter must be a locally static expression of type
3890
   // universal_integer
3891

3892
   type_t uint = std_type(NULL, STD_UNIVERSAL_INTEGER);
1,643✔
3893
   if (!type_eq(tree_type(dim), uint)) {
1,643✔
3894
      diag_t *d = diag_new(DIAG_ERROR, tree_loc(dim));
1✔
3895
      diag_printf(d, "dimension parameter of attribute %s must be a locally "
1✔
3896
                  "static expression of type universal_integer",
3897
                  istr(tree_ident(t)));
3898
      diag_hint(d, tree_loc(dim), "expression has type %s",
1✔
3899
                type_pp(tree_type(dim)));
3900
      diag_emit(d);
1✔
3901
      return false;
1✔
3902
   }
3903

3904
   if (!sem_locally_static(dim))
1,642✔
NEW
3905
      sem_error(dim, "dimension parameter of attribute %s must be a locally "
×
3906
                "static expression", istr(tree_ident(t)));
3907

3908
   if (!type_is_array(tree_type(tree_name(t))))
1,642✔
NEW
3909
      sem_error(t, "prefix of attribute %s with dimension is not an array",
×
3910
                istr(tree_ident(t)));
3911

3912
   return true;
3913
}
3914

3915
static bool sem_is_named_entity(tree_t t)
745✔
3916
{
3917
   const tree_kind_t kind = tree_kind(t);
745✔
3918
   if (kind != T_REF)
745✔
3919
      return false;
3920

3921
   tree_t decl = tree_ref(t);
745✔
3922

3923
   switch (tree_kind(decl)) {
745✔
3924
   case T_SIGNAL_DECL:  case T_VAR_DECL:     case T_PORT_DECL:
3925
   case T_ALIAS:        case T_ENTITY:       case T_ARCH:
3926
   case T_PACKAGE:      case T_PACK_BODY:    case T_BLOCK:
3927
   case T_FILE_DECL:    case T_CONST_DECL:   case T_FUNC_DECL:
3928
   case T_FUNC_BODY:    case T_PROC_DECL:    case T_PROC_BODY:
3929
   case T_PROCESS:      case T_GENERIC_DECL: case T_PARAM_DECL:
3930
   case T_INSTANCE:     case T_PROT_DECL:
3931
      return true;
UNCOV
3932
   case T_IMPLICIT_SIGNAL:
×
UNCOV
3933
      return tree_subkind(decl) == IMPLICIT_GUARD;   // See LRM 93 section 4.3
×
3934
   default:
2✔
3935
      return false;
2✔
3936
   }
3937
}
3938

3939
static bool sem_check_attr_ref(tree_t t, bool allow_range, nametab_t *tab)
23,154✔
3940
{
3941
   // Attribute names are in LRM 93 section 6.6
3942

3943
   tree_t name = tree_name(t), decl = NULL, type_decl = NULL;
23,154✔
3944
   type_t named_type = NULL;
23,154✔
3945

3946
   ident_t attr = tree_ident(t);
23,154✔
3947
   const attr_kind_t predef = tree_subkind(t);
23,154✔
3948

3949
   switch (tree_kind(name)) {
23,154✔
3950
   case T_REF:
21,546✔
3951
      {
3952
         if (!tree_has_ref(name))
21,546✔
3953
            return false;
3954

3955
         decl = tree_ref(name);
21,544✔
3956

3957
         if ((type_decl = aliased_type_decl(decl)) != NULL)
21,544✔
3958
            named_type = tree_type(type_decl);
3,363✔
3959
      }
3960
      break;
3961

3962
   case T_ATTR_REF:
64✔
3963
      if (is_type_attribute(tree_subkind(name)))
64✔
3964
         named_type = tree_type(name);
39✔
3965
      else {
3966
         const bool prefix_can_be_range =
25✔
3967
            predef == ATTR_LOW || predef == ATTR_HIGH || predef == ATTR_LEFT
3968
            || predef == ATTR_RIGHT || predef == ATTR_ASCENDING;
25✔
3969

3970
         if (!sem_check_attr_ref(name, prefix_can_be_range, tab))
25✔
3971
            return false;
3972
      }
3973
      break;
3974

3975
   default:
1,544✔
3976
      if (!sem_check(name, tab))
1,544✔
3977
         return false;
3978
   }
3979

3980
   switch (predef) {
23,150✔
3981
   case ATTR_RANGE:
4,313✔
3982
   case ATTR_REVERSE_RANGE:
3983
      {
3984
         if (!allow_range)
4,313✔
UNCOV
3985
            sem_error(t, "range expression not allowed here");
×
3986

3987
         type_t name_type = tree_has_type(name) ? tree_type(name) : NULL;
4,313✔
3988
         const bool is_type = type_decl != NULL;
8,625✔
3989
         const bool is_discrete =
8,625✔
3990
            name_type != NULL && type_is_discrete(name_type);
4,312✔
3991
         const bool invalid =
8,626✔
3992
            name_type == NULL
3993
            || (!(is_discrete && is_type) && !type_is_array(name_type));
4,313✔
3994

3995
         if (invalid) {
4,313✔
3996
            if (name_type != NULL && type_is_none(name_type))
4✔
3997
               return false;
3998
            else if (decl != NULL && class_has_type(class_of(decl))) {
4✔
3999
               if (is_type)
3✔
4000
                  sem_error(t, "type %s does not have a range",
1✔
4001
                            type_pp(tree_type(decl)));
4002
               else
4003
                  sem_error(t, "object %s does not have a range",
2✔
4004
                            istr(tree_ident(decl)));
4005
            }
4006
            else {
4007
               assert(error_count() > 0);  // Checked in parser
1✔
4008
               return false;
4009
            }
4010
         }
4011

4012
         if (is_type && type_is_unconstrained(name_type))
4,309✔
4013
            sem_error(t, "cannot use attribute %s with unconstrained array "
1✔
4014
                      "type %s", istr(attr), type_pp(name_type));
4015

4016
         if (!sem_check_dimension_attr(t, tab))
4,308✔
UNCOV
4017
            return false;
×
4018

4019
         return true;
4020
      }
4021

4022
   case ATTR_LENGTH:
7,503✔
4023
      {
4024
         type_t type = get_type_or_null(name);
7,503✔
4025
         if (type == NULL)
7,503✔
4026
            sem_error(name, "prefix does not have LENGTH attribute");
1✔
4027
         else if (type_is_none(type))
7,502✔
4028
            return false;
4029
         else if (!type_is_array(type)
7,502✔
4030
                  && !(standard() >= STD_19 && type_is_discrete(type)))
11✔
4031
            sem_error(name, "prefix of attribute LENGTH must be an array%s "
1✔
4032
                      "but have type %s",
4033
                      standard() >= STD_19 ? " or a discrete type" : "",
4034
                      type_pp(type));
4035

4036
         if (!sem_check_dimension_attr(t, tab))
7,501✔
UNCOV
4037
            return false;
×
4038

4039
         return true;
4040
      }
4041

4042
   case ATTR_LEFT:
8,207✔
4043
   case ATTR_RIGHT:
4044
   case ATTR_LOW:
4045
   case ATTR_HIGH:
4046
   case ATTR_ASCENDING:
4047
      {
4048
         type_t type = tree_type(name);
8,207✔
4049

4050
         if (!sem_check_dimension_attr(t, tab))
8,207✔
4051
            return false;
4052

4053
         if (!type_is_array(type) && !type_is_scalar(type))
8,206✔
UNCOV
4054
            sem_error(t, "prefix does not have attribute %s", istr(attr));
×
4055

4056
         return true;
4057
      }
4058

4059
   case ATTR_LAST_EVENT:
59✔
4060
   case ATTR_LAST_ACTIVE:
4061
      if (!sem_check_attr_param(t, NULL, 0, 0, tab))
59✔
4062
         return false;
4063

4064
      if (!sem_check_signal_attr(t))
59✔
4065
         return false;
1✔
4066

4067
      return true;
4068

4069
   case ATTR_EVENT:
500✔
4070
   case ATTR_ACTIVE:
4071
   case ATTR_LAST_VALUE:
4072
      if (!sem_check_signal_attr(t))
500✔
UNCOV
4073
         return false;
×
4074

4075
      return true;
4076

4077
   case ATTR_PATH_NAME:
745✔
4078
   case ATTR_INSTANCE_NAME:
4079
   case ATTR_SIMPLE_NAME:
4080
      if (!sem_is_named_entity(name))
745✔
4081
         sem_error(t, "prefix of %s attribute must be a named entity",
2✔
4082
                   istr(attr));
4083

4084
      tree_set_flag(name, TREE_F_FORMAL_NAME);
743✔
4085
      return true;
743✔
4086

4087
   case ATTR_STABLE:
49✔
4088
   case ATTR_QUIET:
4089
      if (!sem_check_valid_implicit_signal(t, tab))
49✔
4090
         return false;
4091
      // Fall-through
4092
   case ATTR_DELAYED:
4093
      {
4094
         if (!sem_check_signal_attr(t))
130✔
4095
            return false;
4096

4097
         type_t std_time = std_type(NULL, STD_TIME);
130✔
4098
         if (tree_params(t) > 0) {
130✔
4099
            tree_t value = tree_value(tree_param(t, 0));
48✔
4100

4101
            if (!sem_check(value, tab))
48✔
4102
               return false;
4103

4104
            if (!sem_check_type(value, std_time))
48✔
4105
               sem_error(value, "attribute %s parameter must have type %s",
1✔
4106
                         istr(attr), type_pp(std_time));
4107
         }
4108
         else
4109
            add_param(t, sem_int_lit(std_time, 0), P_POS, NULL);
82✔
4110

4111
         return true;
4112
      }
4113
   case ATTR_TRANSACTION:
17✔
4114
      if (!sem_check_signal_attr(t))
17✔
UNCOV
4115
         return false;
×
4116

4117
      return true;
4118

4119
   case ATTR_DRIVING_VALUE:
58✔
4120
   case ATTR_DRIVING:
4121
      return sem_check_driving(t);
58✔
4122

4123
   case ATTR_IMAGE:
1,055✔
4124
   case ATTR_VALUE:
4125
      {
4126
         if (named_type == NULL && standard() >= STD_19
1,055✔
4127
             && tree_params(t) == 0) {
5✔
4128
            // LCS2016-18 allows attribute with object prefix
4129
            named_type = get_type_or_null(name);
5✔
4130
            add_param(t, name, P_POS, NULL);
5✔
4131
         }
4132

4133
         if (named_type == NULL)
1,055✔
4134
            sem_error(t, "prefix of attribute %s must be a type", istr(attr));
1✔
4135
         else if (!type_is_representable(named_type))
1,054✔
4136
            sem_error(t, "cannot use attribute %s with non-%s type %s",
1✔
4137
                      istr(attr),
4138
                      standard() < STD_19 ? "scalar" : "representable",
4139
                      type_pp(named_type));
4140

4141
         type_t std_string = std_type(NULL, STD_STRING);
1,053✔
4142
         type_t arg_type = predef == ATTR_IMAGE ? named_type : std_string;
1,053✔
4143
         if (!sem_check_attr_param(t, arg_type, 1, 1, tab))
1,053✔
4144
            return false;
1✔
4145

4146
         return true;
4147
      }
4148

4149
   case ATTR_LEFTOF:
230✔
4150
   case ATTR_RIGHTOF:
4151
   case ATTR_PRED:
4152
   case ATTR_SUCC:
4153
   case ATTR_POS:
4154
   case ATTR_VAL:
4155
      {
4156
         if (named_type == NULL && standard() >= STD_19)
230✔
4157
            named_type = get_type_or_null(name);   // LCS2016-08 relaxation
3✔
4158

4159
         if (named_type == NULL)
230✔
UNCOV
4160
            sem_error(t, "prefix of attribute %s must be a type", istr(attr));
×
4161

4162
         type_t name_type = tree_type(name);
230✔
4163

4164
         if (!type_is_discrete(name_type) && !type_is_physical(name_type))
230✔
UNCOV
4165
            sem_error(t, "prefix of attribute %s must be a discrete or "
×
4166
                      "physical type", istr(attr));
4167

4168
         if (predef == ATTR_VAL) {
230✔
4169
            // Parameter may be any integer type
4170
            if (tree_params(t) != 1)
87✔
4171
               sem_error(t, "attribute VAL requires a parameter");
1✔
4172

4173
            type_t ptype = tree_type(tree_value(tree_param(t, 0)));
86✔
4174
            if (!type_is_integer(ptype))
86✔
4175
               sem_error(t, "parameter of attribute VAL must have an integer "
1✔
4176
                         "type but found %s", type_pp(ptype));
4177
         }
4178
         else if (!sem_check_attr_param(t, name_type, 1, 1, tab))
143✔
4179
            return false;
×
4180

4181
         return true;
4182
      }
4183

4184
   case ATTR_BASE:
1✔
4185
      sem_error(t, "BASE attribute is allowed only as the prefix of the name "
1✔
4186
                "of another attribute");
4187

4188
   case ATTR_ELEMENT:
1✔
4189
   case ATTR_SUBTYPE:
4190
      sem_error(t, "%s attribute is only allowed in a type mark", istr(attr));
1✔
4191

4192
   case ATTR_CONVERSE:
18✔
4193
      if (type_kind(tree_type(name)) != T_VIEW)
18✔
4194
         sem_error(t, "prefix of 'CONVERSE attribute must be a named mode "
1✔
4195
                   "view or alias thereof");
4196

4197
      return true;
4198

4199
   case ATTR_REFLECT:
96✔
4200
      if (get_type_or_null(name) == NULL)
96✔
4201
         sem_error(t, "prefix of attribute REFLECT is not a type mark or "
1✔
4202
                   "an object with a type");
4203
      else if (named_type != NULL && type_is_unconstrained(named_type))
95✔
4204
         sem_error(t, "prefix of 'REFLECT attribute must be a fully "
1✔
4205
                   "constrained subtype");
4206

4207
      return true;
4208

4209
   case ATTR_USER:
215✔
4210
      if (!tree_has_value(t))
215✔
4211
         return false;
4212

4213
      if (!sem_static_name(name, sem_globally_static)) {
213✔
UNCOV
4214
         if (tree_kind(name) == T_REF)
×
4215
            sem_error(name, "%s is not a static name", istr(tree_ident(name)));
×
4216
         else
UNCOV
4217
            sem_error(name, "invalid attribute reference");
×
4218
      }
4219

4220
      return true;
4221

UNCOV
4222
   default:
×
4223
      fatal_trace("unhandled attribute kind %d", predef);
×
4224
   }
4225
}
4226

4227
static bool sem_check_qualified(tree_t t, nametab_t *tab)
4,096✔
4228
{
4229
   if (tree_has_value(t)) {
4,096✔
4230
      tree_t value = tree_value(t);
3,808✔
4231

4232
      if (!sem_check(value, tab))
3,808✔
4233
         return false;
4234

4235
      // LRM 08 section 9.3.5 qualified expressions: the operand shall have
4236
      // the same type as the base type of the type mark
4237
      type_t base = type_base_recur(tree_type(t));
3,808✔
4238
      if (!sem_check_type(value, base)) {
3,808✔
4239
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(value));
1✔
4240
         diag_printf(d, "operand of qualified expression must have type %s",
1✔
4241
                     type_pp(base));
4242
         diag_hint(d, tree_loc(value), "operand has type %s",
1✔
4243
                   type_pp(tree_type(value)));
4244
         diag_lrm(d, STD_08, "9.3.5");
1✔
4245
         diag_emit(d);
1✔
4246
         return false;
1✔
4247
      }
4248
   }
4249

4250
   return true;
4251
}
4252

4253
static bool sem_static_signal_name(tree_t t)
1,124✔
4254
{
4255
   if (!sem_static_name(t, sem_globally_static))
1,124✔
4256
      return false;
4257

4258
   tree_t ref = name_to_ref(t);
1,122✔
4259
   if (ref != NULL && !tree_has_ref(ref))
1,122✔
4260
      return true;  // Suppress cascading error
4261

4262
   return ref != NULL && class_of(tree_ref(ref)) == C_SIGNAL;
1,125✔
4263
}
4264

4265
static bool sem_check_port_actual(formal_map_t *formals, int nformals,
2,839✔
4266
                                  tree_t param, tree_t unit, nametab_t *tab)
4267
{
4268
   tree_t value = tree_value(param);
2,839✔
4269
   tree_t decl = NULL;
2,839✔
4270
   type_t type = NULL;
2,839✔
4271

4272
   switch (tree_subkind(param)) {
2,839✔
4273
   case P_POS:
1,118✔
4274
      {
4275
         const int pos = tree_pos(param);
1,118✔
4276
         if (pos >= nformals)
1,118✔
4277
            sem_error(value, "found at least %d positional actuals but %s "
4✔
4278
                      "has only %d port%s", pos + 1, istr(tree_ident(unit)),
4279
                      nformals, nformals == 1 ? "" : "s");
4280
         if (formals[pos].have)
1,116✔
UNCOV
4281
            sem_error(value, "formal port %s already has an actual",
×
4282
                      istr(tree_ident(formals[pos].decl)));
4283
         formals[pos].have = true;
1,116✔
4284
         decl = formals[pos].decl;
1,116✔
4285
         type = tree_type(decl);
1,116✔
4286
      }
4287
      break;
1,116✔
4288

4289
   case P_NAMED:
1,721✔
4290
      {
4291
         tree_t name = tree_name(param);
1,721✔
4292
         tree_t ref = name;
1,721✔
4293
         tree_t conv = NULL;
1,721✔
4294

4295
         switch (tree_kind(name)) {
1,721✔
4296
         case T_FCALL:
1✔
4297
            if (tree_params(name) != 1)
1✔
UNCOV
4298
               sem_error(name, "output conversion function must have "
×
4299
                         "exactly one parameter");
4300

4301
            // The parser would have replaced any other valid conversion
4302
            // function with T_CONV_FUNC
4303
            sem_error(name, "invalid output conversion %s",
1✔
4304
                      istr(tree_ident(name)));
4305
            break;
63✔
4306

4307
         case T_CONV_FUNC:
63✔
4308
         case T_TYPE_CONV:
4309
            conv = name;
63✔
4310
            name = ref = tree_value(name);
63✔
4311
            break;
63✔
4312

4313
         default:
4314
            break;
4315
         }
4316

4317
         ref = name_to_ref(ref);
1,720✔
4318
         assert(ref != NULL && tree_kind(ref) == T_REF);
1,720✔
4319

4320
         for (int i = 0; i < nformals; i++) {
6,986✔
4321
            if (tree_ident(formals[i].decl) == tree_ident(ref)) {
6,984✔
4322
               if (formals[i].have && !formals[i].partial)
1,718✔
4323
                  sem_error(value, "formal port %s already has an actual",
1✔
4324
                            istr(tree_ident(formals[i].decl)));
4325
               formals[i].have    = true;
1,717✔
4326
               formals[i].partial = (tree_kind(name) != T_REF);
1,717✔
4327
               decl = formals[i].decl;
1,717✔
4328
               tree_set_flag(ref, TREE_F_FORMAL_NAME);
1,717✔
4329
               break;
1,717✔
4330
            }
4331
         }
4332

4333
         if (decl == NULL)
1,719✔
4334
            sem_error(value, "%s has no port named %s",
2✔
4335
                      istr(tree_ident(unit)), istr(tree_ident(ref)));
4336

4337
         if (!sem_static_name(name, sem_locally_static))
1,717✔
4338
            sem_error(name, "formal name must be locally static");
3✔
4339

4340
         if (conv != NULL) {
1,714✔
4341
            port_mode_t mode = tree_subkind(decl);
63✔
4342

4343
            type = tree_type((mode == PORT_INOUT) ? name : conv);
116✔
4344

4345
            if (mode == PORT_IN)
63✔
4346
               sem_error(name, "output conversion not allowed for formal "
1✔
4347
                         "%s with mode IN", istr(tree_ident(decl)));
4348

4349
            if (tree_kind(value) == T_OPEN)
62✔
4350
               sem_error(name, "output conversion for formal %s must not "
1✔
4351
                         "have OPEN actual", istr(tree_ident(decl)));
4352
         }
4353
         else
4354
            type = tree_type(name);
1,651✔
4355

4356
         break;
4357
      }
4358
   }
4359

4360
   assert(type != NULL);
2,828✔
4361

4362
   if (!sem_check(value, tab))
2,828✔
4363
      return false;
4364

4365
   type_t value_type = tree_type(value);
2,827✔
4366

4367
   if (!sem_check_mapped_type(value, type, get_generic_map(tab)))
2,827✔
4368
      sem_error(value, "type of actual %s does not match type %s of formal "
1✔
4369
                "port %s", type_pp(value_type), type_pp(type),
4370
                istr(tree_ident(decl)));
4371

4372
   const port_mode_t mode = tree_subkind(decl);
2,826✔
4373

4374
   if (tree_kind(value) == T_OPEN) {
2,826✔
4375
      if ((mode == PORT_IN) && !tree_has_value(decl))
42✔
4376
         sem_error(value, "unconnected port %s with mode IN must have a "
1✔
4377
                   "default value", istr(tree_ident(decl)));
4378

4379
      if ((mode != PORT_IN) && type_is_unconstrained(tree_type(decl)))
41✔
4380
         sem_error(value, "port %s of unconstrained type %s cannot "
1✔
4381
                   "be unconnected", istr(tree_ident(decl)), type_pp(type));
4382
   }
4383

4384
   // Check for type conversions and conversion functions
4385
   // These only apply if the class of the formal is not constant
4386

4387
   tree_t actual = NULL;
2,824✔
4388
   const tree_kind_t kind = tree_kind(value);
2,824✔
4389
   if (kind == T_TYPE_CONV || kind == T_CONV_FUNC) {
2,824✔
4390
      // Conversion functions are in LRM 93 section 4.3.2.2
4391
      actual = tree_value(value);
80✔
4392

4393
      // LRM 93 section 3.2.1.1 result of a type conversion in an
4394
      // association list cannot be an unconstrained array type
4395
      if (type_is_unconstrained(value_type)
80✔
4396
          && type_is_unconstrained(type))
26✔
4397
         sem_error(value, "result of conversion for unconstrained formal "
1✔
4398
                   "%s must be a constrained array type",
4399
                   istr(tree_ident(decl)));
4400

4401
      if (mode == PORT_OUT)
79✔
4402
         sem_error(value, "conversion not allowed for formal %s with "
1✔
4403
                   "mode OUT", istr(tree_ident(decl)));
4404
   }
4405
   else
4406
      actual = value;    // No conversion
4407

4408
   if (mode == PORT_IN) {
2,822✔
4409
      tree_t ref = name_to_ref(actual);
1,660✔
4410
      bool is_static = true;
1,660✔
4411
      if (ref != NULL && class_of(ref) == C_SIGNAL)
1,660✔
4412
         is_static = sem_static_name(actual, sem_globally_static);
1,519✔
4413
      else
4414
         is_static = sem_globally_static(actual);
141✔
4415

4416
      // LRM 08 section 6.5.6.3 the actual is converted to a concurrent
4417
      // signal assignment to an anonymous signal that is then
4418
      // associated with the formal
4419
      if (!is_static && standard() >= STD_08) {
1,668✔
4420
         tree_t w = tree_new(T_WAVEFORM);
8✔
4421
         tree_set_loc(w, tree_loc(value));
8✔
4422
         tree_set_value(w, value);
8✔
4423

4424
         tree_set_value(param, w);
8✔
4425
      }
4426
      else if (!is_static)
1,652✔
4427
         sem_error(value, "actual associated with port %s of mode IN must be "
4✔
4428
                   "a globally static expression or static signal name",
4429
                   istr(tree_ident(decl)));
4430
   }
4431
   else if (mode == PORT_INOUT && tree_class(decl) == C_VARIABLE) {
1,162✔
4432
      // VHDL-2019 additions for shared variable ports
4433
      tree_t ref = name_to_ref(value);
5✔
4434
      if (ref == NULL || class_of(ref) != C_VARIABLE)
5✔
4435
         sem_error(value, "actual associated with formal variable port %s "
1✔
4436
                   "must either be a shared variable or a formal variable port "
4437
                   "of another design entity", istr(tree_ident(decl)));
4438
   }
4439
   else if (mode == PORT_ARRAY_VIEW || mode == PORT_RECORD_VIEW) {
1,157✔
4440
      if (!sem_static_signal_name(actual))
44✔
4441
         sem_error(value, "actual associated with port %s with mode view "
1✔
4442
                   "indication must be a static signal name",
4443
                   istr(tree_ident(decl)));
4444

4445
      if (!sem_check_compatible_view(decl, actual))
43✔
4446
         return false;
1✔
4447
   }
4448
   else if (mode != PORT_IN && tree_kind(actual) != T_OPEN
1,113✔
4449
            && !sem_static_signal_name(actual)) {
1,080✔
4450
      sem_error(value, "actual associated with port %s of mode %s must be "
4✔
4451
                "a static signal name or OPEN",
4452
                istr(tree_ident(decl)), port_mode_str(tree_subkind(decl)));
4453
   }
4454

4455
   return true;
4456
}
4457

4458
static bool sem_check_port_map(tree_t t, tree_t unit, nametab_t *tab)
1,487✔
4459
{
4460
   // Check there is an actual for each formal port generic
4461
   // Rules for maps are described in LRM 93 section 5.2.1.2
4462

4463
   const int nformals = tree_ports(unit);
1,487✔
4464
   const int nactuals = tree_params(t);
1,487✔
4465

4466
   bool ok = true;
1,487✔
4467

4468
   formal_map_t *formals LOCAL = xmalloc_array(nformals, sizeof(formal_map_t));
2,974✔
4469

4470
   for (int i = 0; i < nformals; i++) {
4,956✔
4471
      formals[i].decl    = tree_port(unit, i);
3,469✔
4472
      formals[i].have    = false;
3,469✔
4473
      formals[i].partial = false;
3,469✔
4474
   }
4475

4476
   for (int i = 0; i < nactuals; i++) {
4,336✔
4477
      tree_t p = tree_param(t, i);
2,849✔
4478
      if (tree_subkind(p) != P_NAMED)
2,849✔
4479
         continue;
1,119✔
4480

4481
      tree_t name = tree_name(p);
1,730✔
4482

4483
      ok &= sem_check(name, tab);
1,730✔
4484

4485
      const tree_kind_t name_kind = tree_kind(name);
1,730✔
4486
      if ((name_kind == T_ARRAY_REF || name_kind == T_ARRAY_SLICE)
1,730✔
4487
          && tree_kind(tree_value(p)) == T_OPEN && standard() < STD_19) {
161✔
4488
         diag_t *d = pedantic_diag(p);
1✔
4489
         if (d != NULL) {
1✔
4490
            diag_printf(d, "sub-elements of composite port cannot be "
1✔
4491
                        "associated with OPEN");
4492
            diag_emit(d);
1✔
4493
         }
4494
      }
4495
   }
4496

4497
   if (!ok)
1,487✔
4498
      return false;
4499

4500
   for (int i = 0; i < nactuals; i++) {
4,321✔
4501
      tree_t actual = tree_param(t, i);
2,839✔
4502
      ok &= sem_check_port_actual(formals, nformals, actual, unit, tab);
2,839✔
4503

4504
      if (!ok && tree_subkind(actual) == P_POS && i >= nformals)
2,839✔
4505
         break;   // Prevent useless repeated errors
4506
   }
4507

4508
   if (tree_kind(t) == T_BINDING)
1,484✔
4509
      return ok;
4510

4511
   for (int i = 0; i < nformals; i++) {
4,052✔
4512
      if (!formals[i].have) {
2,708✔
4513
         port_mode_t mode = tree_subkind(formals[i].decl);
306✔
4514

4515
         if (mode == PORT_IN && !tree_has_value(formals[i].decl)) {
306✔
4516
            error_at(tree_loc(t), "missing actual for port %s of "
4✔
4517
                     "mode IN without a default expression",
4518
                     istr(tree_ident(formals[i].decl)));
4✔
4519
         }
4520
         else if (mode == PORT_ARRAY_VIEW || mode == PORT_RECORD_VIEW)
302✔
4521
            error_at(tree_loc(t), "missing actual for port %s with "
2✔
4522
                     "mode view indication %s",
4523
                     istr(tree_ident(formals[i].decl)),
1✔
4524
                     type_pp(tree_type(tree_value(formals[i].decl))));
1✔
4525

4526
         type_t ftype = tree_type(formals[i].decl);
306✔
4527
         if (mode != PORT_IN && type_is_unconstrained(ftype)) {
306✔
4528
            error_at(tree_loc(t), "missing actual for port %s with "
1✔
4529
                     "unconstrained array type",
4530
                     istr(tree_ident(formals[i].decl)));
1✔
4531
         }
4532
      }
4533
   }
4534

4535
   return ok;
4536
}
4537

4538
static bool sem_check_generic_actual(formal_map_t *formals, int nformals,
1,248✔
4539
                                     tree_t param, tree_t unit, nametab_t *tab)
4540
{
4541
   tree_t value = tree_value(param), decl = NULL;
1,248✔
4542
   type_t type = NULL;
1,248✔
4543

4544
   switch (tree_subkind(param)) {
1,248✔
4545
   case P_POS:
420✔
4546
      {
4547
         const int pos = tree_pos(param);
420✔
4548
         if (pos >= nformals)
420✔
4549
            sem_error(value, "found at least %d positional actuals but %s "
1✔
4550
                      "has only %d generic%s", pos + 1, istr(tree_ident(unit)),
4551
                      nformals, nformals == 1 ? "" : "s");
4552
         else if (formals[pos].have)
419✔
UNCOV
4553
            sem_error(value, "formal generic %s already has an actual",
×
4554
                      istr(tree_ident(formals[pos].decl)));
4555
         else if (tree_flags(formals[pos].decl) & TREE_F_PREDEFINED) {
419✔
4556
            diag_t *d = diag_new(DIAG_WARN, tree_loc(param));
2✔
4557
            diag_printf(d, "positional generic actual is associated with "
2✔
4558
                        "implicit generic subprogram %s for type %s",
4559
                        istr(tree_ident(formals[pos].decl)),
4560
                        type_pp(tree_type(tree_port(formals[pos].decl, 0))));
4561
            diag_hint(d, NULL, "use a named association if this was intended");
2✔
4562
            diag_emit(d);
2✔
4563
         }
4564

4565
         formals[pos].have = true;
419✔
4566
         decl = formals[pos].decl;
419✔
4567
         type = get_type_or_null(decl);
419✔
4568
      }
4569
      break;
419✔
4570

4571
   case P_NAMED:
828✔
4572
      {
4573
         tree_t name = tree_name(param);
828✔
4574
         tree_t ref = name_to_ref(name);
828✔
4575

4576
         if (ref == NULL)
828✔
4577
            sem_error(name, "invalid name in generic map");
1✔
4578
         else if (!tree_has_ref(ref))
827✔
4579
            return false;
4580

4581
         tree_t d = tree_ref(ref);
825✔
4582
         for (int i = 0; i < nformals; i++) {
1,840✔
4583
            if (formals[i].decl == d) {
1,839✔
4584
               if (formals[i].have && !formals[i].partial)
824✔
UNCOV
4585
                  sem_error(value, "generic %s already has an actual",
×
4586
                            istr(tree_ident(formals[i].decl)));
4587
               formals[i].have    = true;
824✔
4588
               formals[i].partial = (tree_kind(name) != T_REF);
824✔
4589
               decl = d;
824✔
4590
               tree_set_flag(ref, TREE_F_FORMAL_NAME);
824✔
4591
               break;
824✔
4592
            }
4593
         }
4594

4595
         if (decl == NULL)
825✔
4596
            sem_error(name, "%s is not a formal generic of %s",
1✔
4597
                      istr(tree_ident(ref)), istr(tree_ident(unit)));
4598

4599
         if (tree_class(decl) == C_CONSTANT || tree_kind(name) != T_REF) {
824✔
4600
            // Do not check package or type for names here as that will
4601
            // throw an error
4602
            if (!sem_check(name, tab))
608✔
4603
               return false;
4604

4605
            if (!sem_static_name(name, sem_locally_static))
608✔
4606
               sem_error(name, "formal generic name must be a locally "
1✔
4607
                         "static name");
4608
         }
4609

4610
         type = get_type_or_null(name);
823✔
4611
         break;
823✔
4612
      }
4613
   }
4614

4615
   switch (tree_class(decl)) {
1,242✔
4616
   case C_TYPE:
150✔
4617
      // The parser already called map_generic_type
4618
      assert(tree_kind(value) == T_TYPE_REF);
150✔
4619
      assert(type_kind(type) == T_GENERIC);
150✔
4620

4621
      type_t map = tree_type(value);
150✔
4622
      if (type_is_none(map))
150✔
4623
         return false;
4624

4625
      static const char *class_strings[] = {
147✔
4626
         [GTYPE_SCALAR] = "a scalar",
4627
         [GTYPE_DISCRETE] = "a discrete",
4628
         [GTYPE_INTEGER] = "an integer",
4629
         [GTYPE_FLOATING] = "a floating-point",
4630
         [GTYPE_PHYSICAL] = "a physical",
4631
         [GTYPE_ACCESS] = "an access",
4632
         [GTYPE_ARRAY] = "an array",
4633
         [GTYPE_FILE] = "a file",
4634
      };
4635

4636
      const gtype_class_t class = type_subkind(type);
147✔
4637
      if (!type_matches_class(map, class))
147✔
4638
         sem_error(param, "cannot map type %s to generic interface type %s "
8✔
4639
                   "which requires %s type", type_pp(map),
4640
                   istr(tree_ident(decl)), class_strings[class]);
4641
      else if (class == GTYPE_ACCESS || class == GTYPE_FILE) {
139✔
4642
         type_t expect = type_designated(type);
8✔
4643
         type_t actual = type_designated(map);
8✔
4644

4645
         if (type_is_generic(expect)) {
8✔
4646
            const gtype_class_t expect_class = type_subkind(expect);
4✔
4647
            if (!type_matches_class(actual, expect_class))
4✔
4648
               sem_error(param, "cannot map type %s to generic interface type "
1✔
4649
                         "%s as the designated type %s is not %s type",
4650
                         type_pp(map), istr(tree_ident(decl)),
4651
                         type_pp(actual), class_strings[expect_class]);
4652
         }
4653
         else if (!type_eq(actual, expect))
4✔
4654
            sem_error(param, "cannot map type %s to generic interface type "
2✔
4655
                      "%s as the designated type %s is not %s",
4656
                      type_pp(map), istr(tree_ident(decl)),
4657
                      type_pp(actual), type_pp(expect));
4658
      }
4659
      else if (class == GTYPE_ARRAY) {
131✔
4660
         const int nindex = type_indexes(type);
6✔
4661
         if (nindex != dimension_of(map))
6✔
4662
            sem_error(param, "cannot map type %s to generic interface "
1✔
4663
                      "type %s as it has %d dimensions but the incomplete "
4664
                      "type definition has %d", type_pp(map),
4665
                      istr(tree_ident(decl)), dimension_of(map), nindex);
4666

4667
         for (int i = 0; i < nindex; i++) {
8✔
4668
            type_t itype = type_index(type, i);
5✔
4669
            type_t imap = index_type_of(map, i);
5✔
4670

4671
            if (type_is_generic(itype)) {
5✔
4672
               const gtype_class_t expect_class = type_subkind(itype);
2✔
4673
               if (!type_matches_class(imap, expect_class))
2✔
4674
                  sem_error(param, "cannot map type %s to generic interface "
1✔
4675
                            "type %s as the index type %s of the %s dimension "
4676
                            "is not %s type", type_pp(map),
4677
                            istr(tree_ident(decl)), type_pp(imap),
4678
                            ordinal_str(i + 1), class_strings[expect_class]);
4679
            }
4680
            else if (!type_eq(imap, itype))
3✔
4681
               sem_error(param, "cannot map type %s to generic interface type "
4✔
4682
                         "%s as the index type %s of the %s dimension is not "
4683
                         "%s", type_pp(map), istr(tree_ident(decl)),
4684
                         type_pp(imap), ordinal_str(i + 1), type_pp(itype));
4685
         }
4686

4687
         type_t expect = type_elem(type);
3✔
4688
         type_t actual = type_elem(map);
3✔
4689

4690
          if (type_is_generic(expect)) {
3✔
4691
            const gtype_class_t expect_class = type_subkind(expect);
2✔
4692
            if (!type_matches_class(actual, expect_class))
2✔
4693
               sem_error(param, "cannot map type %s to generic interface type "
1✔
4694
                         "%s as the element type %s is not %s type",
4695
                         type_pp(map), istr(tree_ident(decl)),
4696
                         type_pp(actual), class_strings[expect_class]);
4697
         }
4698
         else if (!type_eq(actual, expect))
1✔
4699
            sem_error(param, "cannot map type %s to generic interface type "
1✔
4700
                      "%s as the element type %s is not %s",
4701
                      type_pp(map), istr(tree_ident(decl)),
4702
                      type_pp(actual), type_pp(expect));
4703
      }
4704

4705
      break;
4706

4707
   case C_PACKAGE:
51✔
4708
      {
4709
         tree_t pack = NULL;
51✔
4710
         if (tree_kind(value) == T_REF && tree_has_ref(value))
51✔
4711
            pack = tree_ref(value);
50✔
4712

4713
         if (pack == NULL || tree_kind(pack) != T_PACK_INST)
50✔
4714
            sem_error(value, "actual for generic %s is not an "
2✔
4715
                      "instantiated package name", istr(tree_ident(decl)));
4716
         else if (!tree_has_ref(pack))
49✔
4717
            return false;   // Was parse error
4718

4719
         tree_t map = tree_value(decl);
49✔
4720
         if (!tree_has_ref(map))
49✔
4721
            return false;   // Was earlier error
4722

4723
         assert(tree_kind(map) == T_PACKAGE_MAP);
48✔
4724

4725
         tree_t base = tree_ref(pack);
48✔
4726
         tree_t expect = tree_ref(map);
48✔
4727

4728
         if (tree_ident(base) != tree_ident(expect))
48✔
4729
            sem_error(value, "expected an instance of package %s but have "
1✔
4730
                      "instance of %s for generic %s", istr(tree_ident(expect)),
4731
                      istr(tree_ident(base)), istr(tree_ident(decl)));
4732

4733
         map_generic_package(tab, expect, pack);
47✔
4734
      }
4735
      break;
47✔
4736

4737
   case C_FUNCTION:
45✔
4738
   case C_PROCEDURE:
4739
      if (!sem_check(value, tab))
45✔
4740
         return false;
4741

4742
      if (!type_eq_map(tree_type(value), type, get_generic_map(tab)))
43✔
UNCOV
4743
         sem_error(value, "type of actual %s does not match type %s of formal "
×
4744
                   "generic %s", type_pp(tree_type(value)), type_pp(type),
4745
                   istr(tree_ident(decl)));
4746

4747
      assert(tree_kind(value) == T_REF);
43✔
4748

4749
      if (!tree_has_ref(value))
43✔
4750
         return false;
4751

4752
      map_generic_subprogram(tab, decl, tree_ref(value));
43✔
4753
      break;
43✔
4754

4755
   case C_CONSTANT:
996✔
4756
      if (!sem_check(value, tab))
996✔
4757
         return false;
4758

4759
      if (!sem_check_mapped_type(value, type, get_generic_map(tab)))
993✔
4760
         sem_error(value, "type of actual %s does not match type %s of formal "
6✔
4761
                   "generic %s", type_pp(tree_type(value)), type_pp(type),
4762
                   istr(tree_ident(decl)));
4763

4764
      if (!sem_globally_static(value))
987✔
4765
         sem_error(value, "actual associated with generic %s must be "
2✔
4766
                   "a globally static expression", istr(tree_ident(decl)));
4767

4768
      break;
4769

4770
   default:
4771
      // Was an earlier error
4772
      break;
4773
   }
4774

4775
   return true;
4776
}
4777

4778
static bool sem_check_generic_map(tree_t t, tree_t unit, nametab_t *tab)
1,775✔
4779
{
4780
   // Check there is an actual for each formal generic
4781
   // Rules for maps are described in LRM 93 section 5.2.1.2
4782

4783
   const int nformals = tree_generics(unit);
1,775✔
4784
   const int nactuals = tree_genmaps(t);
1,775✔
4785

4786
   formal_map_t *formals LOCAL = xmalloc_array(nformals, sizeof(formal_map_t));
3,550✔
4787

4788
   for (int i = 0; i < nformals; i++) {
3,745✔
4789
      formals[i].decl    = tree_generic(unit, i);
1,970✔
4790
      formals[i].have    = false;
1,970✔
4791
      formals[i].partial = false;
1,970✔
4792
   }
4793

4794
   bool ok = true;
4795

4796
   for (int i = 0; i < nactuals; i++) {
3,022✔
4797
      tree_t actual = tree_genmap(t, i);
1,248✔
4798
      ok &= sem_check_generic_actual(formals, nformals, actual, unit, tab);
1,248✔
4799

4800
      if (!ok && tree_subkind(actual) == P_POS && i >= nformals)
1,248✔
4801
         break;   // Prevent useless repeated errors
4802
   }
4803

4804
   if (tree_kind(t) == T_BINDING)
1,775✔
4805
      return ok;
4806

4807
   for (int i = 0; i < nformals; i++) {
3,502✔
4808
      if (formals[i].have)
1,867✔
4809
         continue;
1,192✔
4810
      else if (tree_has_value(formals[i].decl)) {
675✔
4811
         tree_t value = tree_value(formals[i].decl);
439✔
4812
         if (tree_kind(value) == T_BOX) {
439✔
4813
            // Need to look up the matching subprogram now while we still
4814
            // have the symbol table
4815
            map_generic_box(tab, t, formals[i].decl, i);
336✔
4816
         }
4817
      }
4818
      else if (tree_flags(formals[i].decl) & TREE_F_PREDEFINED)
236✔
4819
         map_generic_predef(tab, t, formals[i].decl, i);
230✔
4820
      else
4821
         error_at(tree_loc(t), "missing actual for generic %s without a "
6✔
4822
                  "default expression", istr(tree_ident(formals[i].decl)));
6✔
4823
   }
4824

4825
   return ok;
4826
}
4827

4828
static bool sem_check_instance(tree_t t, nametab_t *tab)
1,206✔
4829
{
4830
   if (!tree_has_ref(t))
1,206✔
4831
      return false;
4832

4833
   tree_t unit = primary_unit_of(tree_ref(t));
1,197✔
4834

4835
   if (tree_has_spec(t)) {
1,197✔
4836
      tree_t spec = tree_spec(t);
61✔
4837

4838
      if (tree_class(t) != C_COMPONENT) {
61✔
4839
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(spec));
2✔
4840
         diag_printf(d, "specification may only be used with component"
2✔
4841
                     " instances");
4842
         diag_hint(d, tree_loc(spec), "specification for %s",
2✔
4843
                   istr(tree_ident(t)));
4844
         diag_hint(d, tree_loc(t), "%s instance", class_str(tree_class(t)));
2✔
4845
         diag_emit(d);
2✔
4846
         return false;
2✔
4847
      }
4848

4849
      assert(tree_kind(unit) == T_COMPONENT);   // Checked by parser
59✔
4850

4851
      if (tree_has_ref(spec) && tree_ref(spec) != unit) {
59✔
4852
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(spec));
2✔
4853
         diag_printf(d, "component mismatch for instance %s: expected %s "
2✔
4854
                     "but specification has %s", istr(tree_ident(t)),
4855
                     istr(tree_ident(unit)), istr(tree_ident(tree_ref(spec))));
4856
         diag_hint(d, tree_loc(spec), "specification has component %s",
2✔
4857
                   istr(tree_ident(tree_ref(spec))));
4858
         diag_hint(d, tree_loc(t), "instance of component %s",
2✔
4859
                   istr(tree_ident(unit)));
4860
         diag_emit(d);
2✔
4861
         return false;
2✔
4862
      }
4863
   }
4864

4865
   if (!sem_check_generic_map(t, unit, tab))
1,193✔
4866
      return false;
4867

4868
   if (!sem_check_port_map(t, unit, tab))
1,161✔
4869
      return false;
29✔
4870

4871
   return true;
4872
}
4873

4874
static bool sem_check_cond(tree_t t, nametab_t *tab)
11,667✔
4875
{
4876
   if (tree_has_value(t)) {
11,667✔
4877
      type_t std_bool = std_type(NULL, STD_BOOLEAN);
9,398✔
4878

4879
      tree_t value = tree_value(t);
9,398✔
4880
      if (!sem_check(value, tab))
9,398✔
4881
         return false;
4882

4883
      if (!sem_check_type(value, std_bool))
9,393✔
4884
         sem_error(value, "type of condition must be %s but have %s",
2✔
4885
                   type_pp(std_bool), type_pp(tree_type(value)));
4886

4887
      if (!sem_readable(value))
9,391✔
UNCOV
4888
         return false;
×
4889
   }
4890

4891
   return true;
4892
}
4893

4894
static bool sem_check_if(tree_t t, nametab_t *tab)
8,436✔
4895
{
4896
   bool ok = true;
8,436✔
4897
   const int nconds = tree_conds(t);
8,436✔
4898
   for (int i = 0; i < nconds; i++)
19,956✔
4899
      ok &= sem_check_cond(tree_cond(t, i), tab);
11,520✔
4900

4901
   return ok;
8,436✔
4902
}
4903

4904
static bool sem_static_subtype(type_t type, static_fn_t fn)
265,451✔
4905
{
4906
   // Rules for locally static subtypes are in LRM 93 7.4.1
4907

4908
   if (type_is_unconstrained(type))
265,451✔
4909
      return false;
4910

4911
   if (type_is_scalar(type))
239,602✔
4912
      return true;
4913

4914
   if (type_is_record(type)) {
236,919✔
4915
      const int nfields = type_fields(type);
24✔
4916
      for (int i = 0; i < nfields; i++) {
78✔
4917
         if (!sem_static_subtype(tree_type(type_field(type, i)), fn))
54✔
4918
            return false;
4919
      }
4920

4921
      return true;
4922
   }
4923

4924
   switch (type_kind(type)) {
236,895✔
4925
   case T_SUBTYPE:
236,895✔
4926
      {
4927
         const int ndims = dimension_of(type);
236,895✔
4928
         for (int i = 0; i < ndims; i++) {
265,660✔
4929
            if (!(*fn)(range_of(type, i)))
236,931✔
4930
               return false;
4931
         }
4932

4933
         return true;
4934
      }
4935
   default:
4936
      return true;
4937
   }
4938
}
4939

4940
static bool sem_ieee_locally_static(tree_t decl)
58,824✔
4941
{
4942
   // Subprograms definined in certain IEEE packages are treated the
4943
   // same as builtin operators in VHDL-2008
4944

4945
   if (standard() < STD_08)
58,824✔
4946
      return false;
4947

4948
   ident_t unit_name = tree_ident(tree_container(decl));
31,359✔
4949

4950
   switch (is_well_known(unit_name)) {
31,359✔
4951
   case W_NUMERIC_STD:
4952
   case W_NUMERIC_BIT:
4953
   case W_IEEE_1164:
4954
   case W_NUMERIC_BIT_UNSIGNED:
4955
   case W_NUMERIC_STD_UNSIGNED:
4956
      return true;
4957
   default:
22,974✔
4958
      return false;
22,974✔
4959
   }
4960
}
4961

4962
static bool sem_locally_static(tree_t t)
14,333,600✔
4963
{
4964
   // Rules for locally static expressions are in LRM 93 7.4.1
4965

4966
   type_t type = tree_type(t);
14,389,500✔
4967
   tree_kind_t kind = tree_kind(t);
14,389,500✔
4968

4969
   if (type_is_none(type))
14,389,500✔
4970
      return true;   // Prevents further cascading errors
4971

4972
   // Any literal other than of type time
4973
   if (kind == T_LITERAL) {
14,389,500✔
4974
      if (tree_subkind(t) == L_PHYSICAL)
139,348✔
4975
         return !type_eq(type, std_type(NULL, STD_TIME));
1,467✔
4976
      else
4977
         return true;
4978
   }
4979
   else if (kind == T_STRING)
14,250,200✔
4980
      return true;
4981
   else if ((kind == T_REF) && (tree_kind(tree_ref(t)) == T_ENUM_LIT))
14,232,100✔
4982
      return true;
4983
   else if (kind == T_OPEN)
14,057,000✔
4984
      return true;
4985

4986
   if (kind == T_REF) {
14,057,000✔
4987
      tree_t decl = tree_ref(t);
439,419✔
4988
      const tree_kind_t dkind = tree_kind(decl);
439,419✔
4989

4990
      // A constant reference (other than a deferred constant) with a
4991
      // locally static value
4992
      if (dkind == T_CONST_DECL) {
439,419✔
4993
         if (tree_has_value(decl))
39,316✔
4994
            return sem_locally_static(tree_value(decl));
38,964✔
4995
         else
4996
            return false;
4997
      }
4998

4999
      // An alias of a locally static name
5000
      if (dkind == T_ALIAS)
400,103✔
5001
         return sem_locally_static(tree_value(decl));
1,582✔
5002

5003
      // [2008] A generic reference with a locally static subtype
5004
      if (dkind == T_GENERIC_DECL && (standard() >= STD_08 || relaxed_rules()))
398,521✔
5005
         return sem_static_subtype(tree_type(decl), sem_locally_static);
1,294✔
5006
   }
5007

5008
   // A locally static range
5009
   if (kind == T_RANGE) {
14,014,800✔
5010
      switch (tree_subkind(t)) {
255,717✔
5011
      case RANGE_TO:
255,133✔
5012
      case RANGE_DOWNTO:
5013
         return sem_locally_static(tree_left(t))
255,133✔
5014
            && sem_locally_static(tree_right(t));
255,591✔
5015

5016
      case RANGE_EXPR:
584✔
5017
         return sem_locally_static(tree_value(t));
584✔
5018

5019
      default:
5020
         return false;
5021
      }
5022
   }
5023

5024
   // A function call of an implicit operator or [2008] an operation
5025
   // defined in one of the packages STD_LOGIC_1164, NUMERIC_BIT,
5026
   // NUMERIC_STD, NUMERIC_BIT_UNSIGNED, or NUMERIC_STD_UNSIGNED in
5027
   // library IEEE whose actuals are locally static expressions.
5028
   if (kind == T_FCALL) {
13,759,100✔
5029
      if (!tree_has_ref(t))
12,977,000✔
5030
         return true;  // Suppress further errors
5031
      else if (tree_flags(t) & TREE_F_LOCALLY_STATIC)
12,977,000✔
5032
         return true;
5033

5034
      tree_t decl = tree_ref(t);
12,840,100✔
5035
      if (tree_kind(decl) == T_GENERIC_DECL)
12,840,100✔
5036
         return false;   // Not known at this point
5037
      else if (!is_builtin(tree_subkind(decl))
12,839,900✔
5038
               && !sem_ieee_locally_static(decl))
58,824✔
5039
         return false;
5040

5041
      const int nparams = tree_params(t);
12,789,400✔
5042
      for (int i = 0; i < nparams; i++) {
12,813,600✔
5043
         if (!sem_locally_static(tree_value(tree_param(t, i))))
12,801,900✔
5044
            return false;
5045
      }
5046

5047
      return true;
5048
   }
5049

5050
   if (kind == T_ATTR_REF) {
782,045✔
5051
      // A predefined attribute other than those listed below whose prefix
5052
      // prefix is either a locally static subtype or is an object that is
5053
      // of a locally static subtype
5054
      const attr_kind_t predef = tree_subkind(t);
242,083✔
5055
      if (predef == ATTR_EVENT || predef == ATTR_ACTIVE
242,083✔
5056
          || predef == ATTR_LAST_EVENT || predef == ATTR_LAST_ACTIVE
242,083✔
5057
          || predef == ATTR_LAST_VALUE || predef == ATTR_DRIVING
241,168✔
5058
          || predef == ATTR_DRIVING_VALUE || predef == ATTR_PATH_NAME
241,051✔
5059
          || predef == ATTR_INSTANCE_NAME || predef == ATTR_SIMPLE_NAME)
240,298✔
5060
         return false;
5061
      else if (!tree_has_value(t)) {
237,275✔
5062
         type_t type = tree_type(tree_name(t));
235,918✔
5063
         return sem_static_subtype(type, sem_locally_static);
235,918✔
5064
      }
5065

5066
      // Whose actual parameter (if any) is a locally static expression
5067
      const int nparams = tree_params(t);
1,357✔
5068
      for (int i = 0; i < nparams; i++) {
1,522✔
5069
         if (!sem_locally_static(tree_value(tree_param(t, i))))
240✔
5070
            return false;
5071
      }
5072

5073
      // A user-defined attribute whose value is a locally static expression
5074
      assert(tree_has_value(t));
1,282✔
5075
      return sem_locally_static(tree_value(t));
1,282✔
5076
   }
5077

5078
   // A qualified expression whose operand is locally static
5079
   if (kind == T_QUALIFIED)
539,962✔
5080
      return sem_locally_static(tree_value(t));
4,630✔
5081

5082
   // A type conversion whose expression is locally static
5083
   if (kind == T_TYPE_CONV)
535,332✔
5084
      return sem_locally_static(tree_value(t));
2,598✔
5085

5086
   // Aggregates must have locally static range and all elements
5087
   // must have locally static values
5088
   if (kind == T_AGGREGATE) {
532,734✔
5089
      if (type_is_unconstrained(type))
17,446✔
5090
         return false;
5091

5092
      if (type_is_array(type)) {
17,335✔
5093
         if (!sem_locally_static(range_of(type, 0)))
16,881✔
5094
            return false;
5095
      }
5096

5097
      const int nassocs = tree_assocs(t);
17,239✔
5098
      for (int i = 0; i < nassocs; i++) {
188,192✔
5099
         tree_t a = tree_assoc(t, i);
171,221✔
5100
         if ((tree_subkind(a) == A_NAMED) && !sem_locally_static(tree_name(a)))
171,221✔
5101
            return false;
5102

5103
         if (!sem_locally_static(tree_value(a)))
171,221✔
5104
            return false;
5105
      }
5106

5107
      return true;
5108
   }
5109

5110
   // A record field name
5111
   if ((kind == T_REF) && (tree_kind(tree_ref(t)) == T_FIELD_DECL))
515,288✔
5112
      return true;
5113

5114
   const bool std08_rules = standard() >= STD_08 || relaxed_rules();
515,077✔
5115

5116
   // [2008] An indexed name whose prefix and index expressions are
5117
   // locally static
5118
   if (std08_rules && kind == T_ARRAY_REF) {
515,077✔
5119
      const int nparams = tree_params(t);
5,741✔
5120
      for (int i = 0; i < nparams; i++) {
7,160✔
5121
         if (!sem_locally_static(tree_value(tree_param(t, i))))
5,795✔
5122
            return false;
5123
      }
5124

5125
      return sem_locally_static(tree_value(t));
1,365✔
5126
   }
5127

5128
   // [2008] A slice name whose prefix and range is locally static
5129
   if (std08_rules && kind == T_ARRAY_SLICE) {
509,336✔
5130
      if (!sem_locally_static(tree_range(t, 0)))
1,482✔
5131
         return false;
5132

5133
      return sem_locally_static(tree_value(t));
276✔
5134
   }
5135

5136
   // [2008] A selected name whose prefix is locally static
5137
   if (std08_rules && kind == T_RECORD_REF)
507,854✔
5138
      return sem_locally_static(tree_value(t));
4,630✔
5139

5140
   return false;
5141
}
5142

5143
static bool sem_static_name(tree_t t, static_fn_t check_fn)
9,122✔
5144
{
5145
   // Rules for static names are in LRM 93 6.1
5146

5147
   switch (tree_kind(t)) {
10,228✔
5148
   case T_REF:
8,222✔
5149
      {
5150
         tree_t decl = tree_ref(t);
8,222✔
5151
         switch (tree_kind(decl)) {
8,222✔
5152
         case T_SIGNAL_DECL:
5153
         case T_VAR_DECL:
5154
         case T_CONST_DECL:
5155
         case T_PORT_DECL:
5156
         case T_TYPE_DECL:
5157
         case T_SUBTYPE_DECL:
5158
         case T_ENTITY:
5159
         case T_ARCH:
5160
         case T_PACK_BODY:
5161
         case T_PACKAGE:
5162
         case T_FUNC_BODY:
5163
         case T_PROC_BODY:
5164
         case T_FUNC_DECL:
5165
         case T_PROC_DECL:
5166
         case T_PROCESS:
5167
         case T_BLOCK:
5168
         case T_ENUM_LIT:
5169
         case T_IMPLICIT_SIGNAL:
5170
         case T_GENERIC_DECL:
5171
         case T_PARAM_DECL:
5172
         case T_CONCURRENT:
5173
         case T_VIEW_DECL:
5174
            return true;
5175
         case T_ALIAS:
26✔
5176
            return sem_static_name(tree_value(decl), check_fn);
26✔
UNCOV
5177
         default:
×
5178
            return false;
×
5179
         }
5180
      }
5181

5182
   case T_EXTERNAL_NAME:
5183
      return true;
5184

5185
   case T_RECORD_REF:
1,077✔
5186
      return sem_static_name(tree_value(t), check_fn);
1,077✔
5187

5188
   case T_ARRAY_REF:
768✔
5189
      {
5190
         if (!sem_static_name(tree_value(t), check_fn))
768✔
5191
            return false;
5192

5193
         const int nparams = tree_params(t);
768✔
5194
         for (int i = 0; i < nparams; i++) {
1,528✔
5195
            if (!(*check_fn)(tree_value(tree_param(t, i))))
771✔
5196
               return false;
5197
         }
5198

5199
         return true;
5200
      }
5201

5202
   case T_ARRAY_SLICE:
137✔
5203
      {
5204
         if (!sem_static_name(tree_value(t), check_fn))
137✔
5205
            return false;
5206

5207
         return (*check_fn)(tree_range(t, 0));
137✔
5208
      }
5209

5210
   case T_ATTR_REF:
3✔
5211
      {
5212
         switch (tree_subkind(t)) {
3✔
5213
         case ATTR_DELAYED:
3✔
5214
         case ATTR_STABLE:
5215
         case ATTR_QUIET:
5216
         case ATTR_TRANSACTION:
5217
            return sem_static_name(tree_name(t), check_fn);
3✔
5218
         default:
5219
            return false;
5220
         }
5221
      }
5222

5223
   default:
5✔
5224
      return false;
5✔
5225
   }
5226
}
5227

5228
static bool sem_globally_static(tree_t t)
764,189✔
5229
{
5230
   // Rules for globally static expressions are in LRM 93 7.4.2
5231

5232
   type_t type = tree_type(t);
768,480✔
5233
   tree_kind_t kind = tree_kind(t);
768,480✔
5234

5235
   if (type_is_none(type))
768,480✔
5236
      return true;   // Prevents further cascading errors
5237

5238
   // A literal of type TIME
5239

5240
   if (type_eq(type, std_type(NULL, STD_TIME))) {
768,480✔
5241
      if (kind == T_REF && tree_kind(tree_ref(t)) == T_UNIT_DECL)
5,584✔
5242
         return true;
5243
      else if (kind == T_LITERAL && tree_subkind(t) == L_PHYSICAL)
5,544✔
5244
         return true;
5245
   }
5246

5247
   // A locally static primary
5248

5249
   if (sem_locally_static(t))
767,787✔
5250
      return true;
5251

5252
   // A generic constant, generate parameter, or constant
5253

5254
   if (kind == T_REF) {
519,744✔
5255
      tree_t decl = tree_ref(t);
100,310✔
5256
      const tree_kind_t decl_kind = tree_kind(decl);
100,310✔
5257
      return decl_kind == T_GENERIC_DECL || decl_kind == T_CONST_DECL;
100,310✔
5258
   }
5259
   else if (kind == T_EXTERNAL_NAME)
419,434✔
5260
      return tree_class(t) == C_CONSTANT;
60✔
5261

5262
   // An alias whose aliased name is globally static
5263

5264
   if (kind == T_ALIAS)
419,374✔
UNCOV
5265
      return sem_globally_static(tree_value(t));
×
5266

5267
   if (kind == T_RANGE) {
419,374✔
5268
      if (tree_subkind(t) == RANGE_EXPR)
26,598✔
5269
         return sem_globally_static(tree_value(t));
6✔
5270

5271
      if (!sem_globally_static(tree_left(t)))
26,592✔
5272
         return false;
5273

5274
      if (!sem_globally_static(tree_right(t)))
26,589✔
5275
         return false;
5276

5277
      return true;
26,579✔
5278
   }
5279

5280
   // Aggregates must have globally static range and all elements
5281
   // must have globally static values
5282
   if (kind == T_AGGREGATE) {
392,776✔
5283
      if (type_is_array(type)
64✔
5284
          && !type_is_unconstrained(type)
44✔
5285
          && !sem_globally_static(range_of(type, 0)))
17✔
5286
         return false;
5287

5288
      const int nassocs = tree_assocs(t);
64✔
5289
      for (int i = 0; i < nassocs; i++) {
114✔
5290
         tree_t a = tree_assoc(t, i);
81✔
5291
         if ((tree_subkind(a) == A_NAMED) && !sem_globally_static(tree_name(a)))
81✔
5292
            return false;
5293

5294
         if (!sem_globally_static(tree_value(a)))
81✔
5295
            return false;
5296
      }
5297

5298
      return true;
5299
   }
5300

5301
   // A function call of a pure function with globally static actuals
5302
   if (kind == T_FCALL) {
392,712✔
5303
      tree_t decl = tree_ref(t);
321,240✔
5304
      if (tree_flags(decl) & TREE_F_IMPURE)
321,240✔
5305
         return false;
5306

5307
      bool all_static = true;
317,978✔
5308
      const int nparams = tree_params(t);
317,978✔
5309
      for (int i = 0; i < nparams; i++) {
943,952✔
5310
         tree_t p = tree_param(t, i);
625,974✔
5311
         all_static = all_static && sem_globally_static(tree_value(p));
703,963✔
5312
      }
5313
      return all_static;
317,978✔
5314
   }
5315

5316
   if (kind == T_ATTR_REF) {
71,472✔
5317
      const attr_kind_t predef = tree_subkind(t);
34,448✔
5318

5319
      // A predefined attribute that is one of 'SIMPLE_NAME,
5320
      // 'INSTANCE_NAME, or 'PATH_NAME
5321
      if (predef == ATTR_SIMPLE_NAME || predef == ATTR_INSTANCE_NAME
34,448✔
5322
          || predef == ATTR_PATH_NAME)
33,608✔
5323
         return true;   // Clause j
5324

5325
      // A predefined attribute other than those listed below whose
5326
      // prefix is either a globally static subtype or is an object or
5327
      // function call that is of a globally static subtype, or in 2008,
5328
      // a prefix which is a appropriate for a globally static attribute
5329
      if (predef == ATTR_EVENT || predef == ATTR_ACTIVE
33,356✔
5330
          || predef == ATTR_LAST_EVENT || predef == ATTR_LAST_ACTIVE
33,356✔
5331
          || predef == ATTR_LAST_VALUE || predef == ATTR_DRIVING
33,073✔
5332
          || predef == ATTR_DRIVING_VALUE)
33,026✔
5333
         return false;   // Clause k
5334
      else if (predef == ATTR_USER) {
33,002✔
5335
         // A user-defined attribute whose value is a globally static
5336
         // expression
5337
         return sem_globally_static(tree_value(t));
120✔
5338
      }
5339

5340
      tree_t name = tree_name(t);
32,882✔
5341

5342
      if (standard() >= STD_08 || relaxed_rules()) {
32,882✔
5343
         // LRM 08 section 9.4.3: A prefix is appropriate for a globally
5344
         // static attribute if it denotes a signal, a constant, a type
5345
         // or subtype, a globally static function call, a variable that
5346
         // is not of an access type, or a variable of an access type
5347
         // whose designated subtype is fully constrained.
5348

5349
         switch (tree_kind(name)) {
4,827✔
5350
         case T_REF:
4,750✔
5351
            {
5352
               tree_t decl = tree_ref(name);
4,750✔
5353
               const tree_kind_t dkind = tree_kind(decl);
4,750✔
5354
               if (dkind == T_VAR_DECL && type_is_access(tree_type(name)))
4,750✔
5355
                  return false;
5356
               else
5357
                  return dkind == T_CONST_DECL || dkind == T_SIGNAL_DECL
4,750✔
5358
                     || dkind == T_TYPE_DECL || dkind == T_VAR_DECL
4,739✔
5359
                     || dkind == T_SUBTYPE_DECL || dkind == T_PORT_DECL
4,094✔
5360
                     || dkind == T_GENERIC_DECL;
13,570✔
5361
            }
5362
         case T_FCALL:
5363
            return sem_globally_static(name);
5364
         default:
5365
            break;
5366
         }
5367
      }
28,055✔
5368

5369
      type_t type = get_type_or_null(name);
28,132✔
5370
      if (type == NULL)
28,132✔
5371
         return false;
5372

5373
      return sem_static_subtype(type, sem_globally_static);
28,132✔
5374
   }
5375

5376
   // A qualified expression whose operand is globally static
5377

5378
   if (kind == T_QUALIFIED)
37,024✔
5379
      return sem_globally_static(tree_value(t));
231✔
5380

5381
   // A type conversion whose operand is globally static
5382

5383
   if (kind == T_TYPE_CONV)
36,793✔
5384
      return sem_globally_static(tree_value(t));
986✔
5385

5386
   // TODO: clauses o, p
5387

5388
   // A sub-element or slice where indexes are globally static
5389

5390
   if (kind == T_ARRAY_REF) {
35,807✔
5391
      if (!sem_globally_static(tree_value(t)))
29,750✔
5392
         return false;
5393

5394
      const int nparams = tree_params(t);
24,940✔
5395
      for (int i = 0; i < nparams; i++) {
49,490✔
5396
         if (!sem_globally_static(tree_value(tree_param(t, i))))
24,966✔
5397
            return false;
5398
      }
5399

5400
      return true;
5401
   }
5402
   else if (kind == T_ARRAY_SLICE) {
6,057✔
5403
      if (!sem_globally_static(tree_value(t)))
698✔
5404
         return false;
5405

5406
      if (!sem_globally_static(tree_range(t, 0)))
42✔
5407
         return false;
5408

5409
      return true;
42✔
5410
   }
5411
   else if (kind == T_RECORD_REF)
5,359✔
5412
      return sem_globally_static(tree_value(t));
2,948✔
5413

5414
   return false;
5415
}
5416

5417
static bool sem_check_case(tree_t t, nametab_t *tab)
560✔
5418
{
5419
   tree_t test = tree_value(t);
560✔
5420
   if (!sem_check(test, tab))
560✔
5421
      return false;
5422

5423
   type_t type = tree_type(test);
556✔
5424

5425
   // LRM 93 8.8 if the type of the expression is an array then it must be
5426
   // a one dimensional character array type
5427

5428
   const bool is_1d_character_array = type_is_character_array(type);
556✔
5429
   const bool valid = is_1d_character_array || type_is_discrete(type);
556✔
5430

5431
   if (!valid) {
556✔
5432
      diag_t *d = diag_new(DIAG_ERROR, tree_loc(test));
2✔
5433
      diag_printf(d, "case expression must have a discrete type or one "
2✔
5434
                  "dimensional character array type");
5435
      if (type_is_array(type) && dimension_of(type) != 1)
2✔
5436
         diag_hint(d, tree_loc(test), "array has %d dimensions",
1✔
5437
                   dimension_of(type));
5438
      else if (type_is_array(type))
1✔
5439
         diag_hint(d, tree_loc(test), "type %s is not a character array",
1✔
5440
                   type_pp(type));
5441
      else
UNCOV
5442
         diag_hint(d, tree_loc(test), "type is %s", type_pp(type));
×
5443
      diag_lrm(d, STD_08, "10.9");
2✔
5444
      diag_emit(d);
2✔
5445
      return false;
2✔
5446
   }
5447

5448
   if (is_1d_character_array && standard() < STD_08) {
554✔
5449
      // VHDL-93 requires a locally static subtype, relaxed in later
5450
      // revisions
5451
      if (!sem_static_subtype(type, sem_locally_static))
53✔
5452
         sem_error(test, "case expression must have locally static subtype");
2✔
5453
   }
5454

5455
   static_fn_t static_fn = sem_locally_static;
552✔
5456
   const char *static_str = "locally";
552✔
5457

5458
   if (tree_kind(t) == T_CASE_GENERATE) {
552✔
5459
      static_fn = sem_globally_static;
7✔
5460
      static_str = "globally";
7✔
5461
   }
5462

5463
   const int nstmts = tree_stmts(t);
552✔
5464
   for (int i = 0; i < nstmts; i++) {
3,082✔
5465
      tree_t alt = tree_stmt(t, i);
2,543✔
5466

5467
      const int nassocs = tree_assocs(alt);
2,543✔
5468
      for (int j = 0; j < nassocs; j++) {
5,691✔
5469
         tree_t a = tree_assoc(alt, j);
3,161✔
5470
         switch (tree_subkind(a)) {
3,161✔
5471
         case A_OTHERS:
400✔
5472
            if (j != nassocs - 1 || i != nstmts - 1) {
400✔
5473
               diag_t *d = diag_new(DIAG_ERROR, tree_loc(a));
1✔
5474
               diag_printf(d, "others choice must appear last");
1✔
5475
               diag_hint(d, tree_loc(a), "others choice");
1✔
5476

5477
               tree_t more = j + 1 < nassocs
2✔
5478
                  ? tree_assoc(alt, j + 1) : tree_assoc(tree_stmt(t, i + 1), 0);
1✔
5479
               diag_hint(d, tree_loc(more), "further choices follow this");
1✔
5480

5481
               diag_emit(d);
1✔
5482
               return false;
1✔
5483
            }
5484
            break;
5485

5486
         case A_NAMED:
2,718✔
5487
            {
5488
               tree_t name = tree_name(a);
2,718✔
5489
               if (!sem_check(name, tab))
2,718✔
5490
                  return false;
5491

5492
               if (!sem_check_type(name, type))
2,718✔
5493
                  sem_error(name, "case choice must have type %s but found %s",
1✔
5494
                            type_pp(type), type_pp(tree_type(name)));
5495
               else if (!(*static_fn)(name))
2,717✔
5496
                  sem_error(name, "case choice must be %s static", static_str);
8✔
5497
            }
5498
            break;
5499

5500
         case A_RANGE:
43✔
5501
            {
5502
               tree_t r = tree_range(a, 0);
43✔
5503
               if (!sem_check_discrete_range(r, type, tab))
43✔
5504
                  return false;
5505

5506
               switch (tree_subkind(r)) {
41✔
5507
               case RANGE_TO:
33✔
5508
               case RANGE_DOWNTO:
5509
                  if (!(*static_fn)(tree_left(r)))
33✔
UNCOV
5510
                     sem_error(tree_left(r), "left index of case choice "
×
5511
                               "range is not %s static", static_str);
5512
                  else if (!(*static_fn)(tree_right(r)))
33✔
5513
                     sem_error(tree_right(r), "right index of case choice "
1✔
5514
                               "range is not %s static", static_str);
5515
                  break;
5516

5517
               case RANGE_EXPR:
8✔
5518
                  if (!(*static_fn)(tree_value(r)))
8✔
UNCOV
5519
                     sem_error(tree_value(r), "range expression is not %s "
×
5520
                               "static", static_str);
5521
                  break;
5522

5523
               default:
5524
                  return false;
5525
               }
5526
            }
5527
            break;
5528
         }
5529
      }
3,148✔
5530
   }
5531

5532
   return true;
5533
}
5534

5535
static bool sem_check_match_case(tree_t t, nametab_t *tab)
40✔
5536
{
5537
   // Matching case statement is in LRM 08 section 10.9
5538

5539
   if (!sem_check_case(t, tab))
40✔
5540
      return false;
5541

5542
   tree_t value = tree_value(t);
39✔
5543
   type_t type = tree_type(value);
39✔
5544

5545
   type_t std_bit = std_type(NULL, STD_BIT);
39✔
5546
   type_t std_logic = ieee_type(IEEE_STD_ULOGIC);
39✔
5547

5548
   type_t elem = type;
39✔
5549
   if (type_is_array(type) && dimension_of(type) == 1)
39✔
5550
      elem = type_elem(type);
31✔
5551

5552
   if (!type_eq(elem, std_bit) && !type_eq(elem, std_logic)) {
39✔
5553
      diag_t *d = diag_new(DIAG_ERROR, tree_loc(value));
1✔
5554
      diag_printf(d, "type of expression in a matching case statement must be "
1✔
5555
                  "BIT, STD_ULOGIC, or a one-dimensional array of these types");
5556
      diag_hint(d, tree_loc(value), "type is %s", type_pp(type));
1✔
5557
      diag_lrm(d, STD_08, "10.9");
1✔
5558
      diag_emit(d);
1✔
5559
      return false;
1✔
5560
   }
5561

5562
   return true;
5563
}
5564

5565
static bool sem_check_return(tree_t t, nametab_t *tab)
12,355✔
5566
{
5567
   tree_t sub = find_enclosing(tab, S_SUBPROGRAM);
12,355✔
5568
   if (sub == NULL)
12,355✔
5569
      sem_error(t, "return statement not allowed outside subprogram");
3✔
5570

5571
   if (tree_has_value(t)) {
12,352✔
5572
      if (tree_kind(sub) == T_PROC_BODY)
12,008✔
5573
         sem_error(t, "cannot return a value from a procedure");
1✔
5574

5575
      type_t expect = type_result(tree_type(sub));
12,007✔
5576

5577
      if (!sem_check(tree_value(t), tab))
12,007✔
5578
         return false;
5579

5580
      if (!sem_check_type(tree_value(t), expect))
12,004✔
5581
         sem_error(t, "expected return type %s but have %s",
2✔
5582
                   type_pp(expect), type_pp(tree_type(tree_value(t))));
5583
   }
5584

5585
   return true;
5586
}
5587

5588
static bool sem_check_cond_return(tree_t t, nametab_t *tab)
7✔
5589
{
5590
   tree_t sub = find_enclosing(tab, S_SUBPROGRAM);
7✔
5591
   if (sub == NULL)
7✔
5592
      sem_error(t, "return statement not allowed outside subprogram");
1✔
5593

5594
   if (tree_kind(sub) != T_PROC_BODY)
6✔
5595
      sem_error(t, "conditional return statement without value is only "
1✔
5596
                "valid inside a procedure");
5597

5598
   tree_t value = tree_value(t);
5✔
5599
   if (!sem_check(value, tab))
5✔
5600
      return false;
5601

5602
   type_t std_bool = std_type(NULL, STD_BOOLEAN);
5✔
5603

5604
   if (!sem_check_type(value, std_bool))
5✔
5605
      sem_error(value, "type of condition must be %s but have %s",
1✔
5606
                type_pp(std_bool), type_pp(tree_type(value)));
5607

5608
   return true;
5609
}
5610

5611
static bool sem_check_while(tree_t t, nametab_t *tab)
455✔
5612
{
5613
   type_t std_bool = std_type(NULL, STD_BOOLEAN);
455✔
5614

5615
   tree_t value = tree_value(t);
455✔
5616
   if (!sem_check(value, tab))
455✔
5617
      return false;
5618

5619
   if (!sem_check_type(value, std_bool))
455✔
5620
      sem_error(value, "type of loop condition must be %s but is %s",
1✔
5621
                type_pp(std_bool), type_pp(tree_type(value)));
5622

5623
   return true;
5624
}
5625

5626
static bool sem_check_for(tree_t t, nametab_t *tab)
2,300✔
5627
{
5628
   if (!sem_check_discrete_range(tree_range(t, 0), NULL, tab))
2,300✔
5629
      return false;
5630

5631
   tree_t idecl = tree_decl(t, 0);
2,286✔
5632

5633
   if (!sem_check_subtype(idecl, tree_type(idecl), tab))
2,286✔
UNCOV
5634
      return false;
×
5635

5636
   return true;
5637
}
5638

5639
static bool sem_check_block(tree_t t, nametab_t *tab)
186✔
5640
{
5641
   if (!sem_check_generic_map(t, t, tab))
186✔
5642
      return false;
5643

5644
   if (!sem_check_port_map(t, t, tab))
186✔
5645
      return false;
5646

5647
   const int ndecls = tree_decls(t);
186✔
5648
   for (int i = 0; i < ndecls; i++) {
722✔
5649
      tree_t d = tree_decl(t, i);
536✔
5650
      sem_check_static_elab(d);
536✔
5651
   }
5652

5653
   return true;
5654
}
5655

5656
static bool sem_check_loop_control(tree_t t, nametab_t *tab)
403✔
5657
{
5658
   if (tree_has_value(t)) {
403✔
5659
      tree_t value = tree_value(t);
195✔
5660
      if (!sem_check(value, tab))
195✔
5661
         return false;
5662

5663
      type_t std_bool = std_type(NULL, STD_BOOLEAN);
195✔
5664
      if (!type_eq(tree_type(value), std_bool))
195✔
5665
         sem_error(value, "type of %s condition must be %s but is %s",
3✔
5666
                   (tree_kind(t) == T_EXIT) ? "exit" : "next",
5667
                   type_pp(std_bool), type_pp(tree_type(value)));
5668
   }
5669

5670
   return true;
5671
}
5672

5673
static bool sem_check_attr_decl(tree_t t)
113✔
5674
{
5675
   if (!sem_no_access_file_or_protected(t, tree_type(t), "attributes"))
113✔
5676
      return false;
5✔
5677

5678
   return true;
5679
}
5680

5681
static bool sem_check_attr_spec(tree_t t, nametab_t *tab)
283✔
5682
{
5683
   tree_t value = tree_value(t);
283✔
5684
   if (!sem_check(value, tab))
283✔
5685
      return false;
5686

5687
   type_t type = tree_type(t);
283✔
5688
   if (!sem_check_type(value, type))
283✔
5689
      sem_error(t, "expected attribute specification for %s to have type %s "
2✔
5690
                "but found %s", istr(tree_ident(t)), type_pp(type),
5691
                type_pp(tree_type(value)));
5692

5693
   if (!tree_has_ref(t))
281✔
5694
      return false;
5695

5696
   tree_t decl = tree_ref(t);
269✔
5697
   const class_t class = tree_class(t);
269✔
5698

5699
   if (class_of(decl) != class)
269✔
5700
      sem_error(t, "class of object %s is %s not %s",
1✔
5701
                istr(tree_ident(decl)), class_str(class_of(decl)),
5702
                class_str(class));
5703

5704
   switch (is_well_known(tree_ident(t))) {
268✔
5705
   case W_NEVER_WAITS:
104✔
5706
      {
5707
         bool flag;
104✔
5708
         if (!folded_bool(value, &flag))
104✔
5709
            sem_error(value, "expression must be a BOOLEAN literal");
2✔
5710
         else if (class != C_PROCEDURE)
103✔
5711
            sem_error(t, "NEVER_WAITS attribute can only be applied to "
1✔
5712
                      "procedures");
5713
         else if (flag)
102✔
5714
            tree_set_flag(decl, TREE_F_NEVER_WAITS);
101✔
5715
      }
5716
      break;
102✔
5717

5718
   case W_FOREIGN:
74✔
5719
      {
5720
         // See LRM 08 section 20.2.4.3
5721

5722
         if (tree_kind(value) != T_STRING)
74✔
5723
            sem_error(value, "FOREIGN attribute must have string "
2✔
5724
                      "literal value");
5725

5726
         const int nchars = tree_chars(value);
73✔
5727
         char *buf LOCAL = xmalloc(nchars + 1);
145✔
5728
         for (int i = 0; i < nchars; i++)
1,623✔
5729
            buf[i] = tree_pos(tree_ref(tree_char(value, i)));
1,550✔
5730
         buf[nchars] = '\0';
73✔
5731

5732
         subprogram_kind_t kind = S_FOREIGN;
73✔
5733
         char *p = strtok(buf, " ");
73✔
5734
         if (strcmp(p, "VHPIDIRECT") == 0) {
73✔
5735
            p = strtok(NULL, " ");
15✔
5736
            if (p != NULL) {
15✔
5737
               // The object library specifier is silently ignored
5738
               char *p2 = strtok(NULL, " ");
15✔
5739
               if (p2 != NULL) p = p2;
15✔
5740
            }
5741
         }
5742
         else if (strcmp(p, "INTERNAL") == 0) {
58✔
5743
            p = strtok(NULL, " ");
9✔
5744
            kind = S_INTERNAL;
9✔
5745
         }
5746
         else if (strtok(NULL, " ") != NULL)
49✔
5747
            sem_error(value, "failed to parse FOREIGN attribute");
1✔
5748

5749
         ident_t name = ident_new(p);
72✔
5750
         tree_set_ident2(decl, name);
72✔
5751

5752
         tree_set_subkind(decl, kind);
72✔
5753
         tree_set_flag(decl, TREE_F_NEVER_WAITS);
72✔
5754
      }
5755
      break;
5756

5757
   default:
5758
      break;
5759
   }
5760

5761
   return true;
5762
}
5763

5764
static bool sem_check_if_generate(tree_t t, nametab_t *tab)
140✔
5765
{
5766
   const int nconds = tree_conds(t);
140✔
5767
   for (int i = 0; i < nconds; i++) {
284✔
5768
      tree_t cond = tree_cond(t, i);
147✔
5769

5770
      if (!sem_check_cond(cond, tab))
147✔
5771
         return false;
5772

5773
      if (tree_has_value(cond)) {
145✔
5774
         tree_t value = tree_value(cond);
139✔
5775
         if (!sem_globally_static(value))
139✔
5776
            sem_error(value, "condition of generate statement must be static");
145✔
5777
      }
5778
   }
5779

5780
   return true;
5781
}
5782

5783
static bool sem_check_for_generate(tree_t t, nametab_t *tab)
130✔
5784
{
5785
   tree_t r = tree_range(t, 0);
130✔
5786
   if (!sem_check_discrete_range(r, NULL, tab))
130✔
5787
      return false;
5788

5789
   if (!sem_globally_static(r))
129✔
5790
      sem_error(r, "range of generate statement must be static");
1✔
5791

5792
   tree_t idecl = tree_decl(t, 0);
128✔
5793
   assert(tree_kind(idecl) == T_GENERIC_DECL);
128✔
5794

5795
   if (!sem_check_subtype(idecl, tree_type(idecl), tab))
128✔
UNCOV
5796
      return false;
×
5797

5798
   return true;
5799
}
5800

5801
static bool sem_check_open(tree_t t)
5802
{
5803
   return true;
5804
}
5805

5806
static bool sem_check_file_decl(tree_t t, nametab_t *tab)
140✔
5807
{
5808
   // Rules for file declarations are in LRM 93 section 4.3.1.4
5809

5810
   type_t type = tree_type(t);
140✔
5811

5812
   if (type_kind(type) != T_FILE)
140✔
5813
      sem_error(t, "file declarations must have file type");
1✔
5814

5815
   if (tree_has_value(t)) {
139✔
5816
      tree_t value = tree_value(t);
43✔
5817
      if (!sem_check(value, tab))
43✔
5818
         return false;
5819

5820
      if (!sem_check_type(value, std_type(NULL, STD_STRING)))
43✔
5821
         sem_error(value, "file name must have type STRING");
1✔
5822

5823
      tree_t mode = tree_file_mode(t);
42✔
5824
      if (!sem_check(mode, tab))
42✔
5825
         return false;
5826

5827
      if (!sem_check_type(mode, std_type(NULL, STD_FILE_OPEN_KIND)))
41✔
5828
         sem_error(mode, "open mode must have type FILE_OPEN_KIND");
1✔
5829
   }
5830

5831
   tree_t sub = find_enclosing(tab, S_SUBPROGRAM);
136✔
5832
   const bool in_pure_func =
272✔
5833
      sub != NULL && tree_kind(sub) == T_FUNC_BODY
21✔
5834
      && !(tree_flags(sub) & TREE_F_IMPURE);
138✔
5835

5836
   if (in_pure_func) {
136✔
5837
      diag_t *d = pedantic_diag(t);
1✔
5838
      if (d != NULL) {
1✔
5839
         diag_printf(d, "cannot declare a file object in a pure function");
1✔
5840
         diag_emit(d);
1✔
5841
      }
5842
   }
5843

5844
   return true;
5845
}
5846

5847
static bool sem_check_new(tree_t t, nametab_t *tab)
540✔
5848
{
5849
   // Rules for allocators are in LRM 93 section 7.3.6
5850

5851
   tree_t value = tree_value(t);
540✔
5852
   type_t access_type = tree_type(t);
540✔
5853

5854
   if (type_is_none(access_type))
540✔
5855
      return false;
5856

5857
   assert(type_is_access(access_type));
539✔
5858
   assert(tree_kind(value) == T_QUALIFIED);
539✔
5859

5860
   if (!sem_check(value, tab))
539✔
5861
      return false;
5862

5863
   type_t type = tree_type(value);
539✔
5864

5865
   if (type_is_none(type))
539✔
5866
      return false;
5867

5868
   if (!sem_check_subtype(value, type, tab))
536✔
5869
      return false;
5870

5871
   const bool has_initial = tree_has_value(value);
536✔
5872

5873
   if (!has_initial && type_is_unconstrained(type))
536✔
5874
      sem_error(t, "unconstrained array type %s not allowed in allocator "
1✔
5875
                "expression", type_pp(type));
5876
   else if (type_is_incomplete(type))
535✔
5877
      sem_error(t, "incomplete type %s found in allocator expression",
1✔
5878
                type_pp(type));
5879
   else if (has_initial && type_is_protected(type))
534✔
5880
      sem_error(t, "protected type %s cannot have initial value",
1✔
5881
                type_pp(type));
5882

5883
   type_t designated = type_designated(access_type);
533✔
5884

5885
   if (!type_eq(type, designated))
533✔
5886
      sem_error(value, "type of allocator expresion %s does not match "
1✔
5887
                "access type %s", type_pp(type), type_pp(designated));
5888

5889
   return true;
5890
}
5891

5892
static bool sem_check_all(tree_t t, nametab_t *tab)
2,333✔
5893
{
5894
   tree_t value = tree_value(t);
2,333✔
5895
   if (!sem_check(value, tab))
2,333✔
5896
      return false;
5897

5898
   type_t value_type = tree_type(value);
2,332✔
5899

5900
   if (type_is_none(value_type))
2,332✔
5901
      return false;
5902

5903
   if (!type_is_access(value_type)) {
2,331✔
5904
      diag_t *d = diag_new(DIAG_ERROR, tree_loc(value));
2✔
5905
      diag_printf(d, "prefix of a selected name with suffix ALL must "
2✔
5906
                  "have access type");
5907
      diag_hint(d, tree_loc(value), "prefix has type %s", type_pp(value_type));
2✔
5908
      diag_lrm(d, STD_08, "8.3");
2✔
5909
      diag_emit(d);
2✔
5910
      return false;
2✔
5911
   }
5912

5913
   return true;
5914
}
5915

5916
static bool sem_check_binding(tree_t t, nametab_t *tab)
143✔
5917
{
5918
   if (!tree_has_ref(t))
143✔
5919
      return false;
5920

5921
   tree_t unit = primary_unit_of(tree_ref(t));
140✔
5922
   if (tree_kind(unit) == T_ENTITY) {
140✔
5923
      if (!sem_check_generic_map(t, unit, tab))
140✔
5924
         return false;
5925

5926
      if (!sem_check_port_map(t, unit, tab))
140✔
UNCOV
5927
         return false;
×
5928
   }
5929

5930
   return true;
5931
}
5932

5933
static bool sem_check_block_config(tree_t t, nametab_t *tab)
5934
{
5935
   return true;
5936
}
5937

5938
static bool sem_check_spec(tree_t t, nametab_t *tab)
150✔
5939
{
5940
   if (!tree_has_ref(t))
150✔
5941
      return false;
5942

5943
   tree_t comp = tree_ref(t);
146✔
5944
   assert(tree_kind(comp) == T_COMPONENT);
146✔
5945

5946
   if (!tree_has_value(t))
146✔
5947
      return true;
5948

5949
   tree_t bind = tree_value(t);
143✔
5950
   assert(tree_kind(bind) == T_BINDING);
143✔
5951

5952
   if (!sem_check(bind, tab))
143✔
5953
      return false;
5954

5955
   tree_t entity = primary_unit_of(tree_ref(bind));
140✔
5956
   assert(tree_kind(entity) == T_ENTITY);
140✔
5957

5958
   bool ok = true;
140✔
5959

5960
   const int c_ngenerics = tree_generics(comp);
140✔
5961
   const int e_ngenerics = tree_generics(entity);
140✔
5962
   const int b_genmaps = tree_genmaps(bind);
140✔
5963

5964
   for (int i = 0; i < c_ngenerics; i++) {
218✔
5965
      tree_t cg = tree_generic(comp, i);
78✔
5966

5967
      tree_t rebind = NULL;
78✔
5968
      for (int j = 0; rebind == NULL && j < b_genmaps; j++) {
123✔
5969
         tree_t value = tree_value(tree_genmap(bind, j));
45✔
5970
         if (tree_kind(value) == T_REF && tree_ref(value) == cg)
45✔
5971
            rebind = value;
24✔
5972
      }
5973

5974
      if (rebind != NULL)
78✔
5975
         continue;   // Ignore for now
24✔
5976

5977
      tree_t match = NULL;
5978
      for (int j = 0; match == NULL && j < e_ngenerics; j++) {
313✔
5979
         tree_t eg = tree_generic(entity, j);
259✔
5980
         if (tree_ident(eg) == tree_ident(cg))
259✔
5981
            match = eg;
53✔
5982
      }
5983

5984
      if (match == NULL) {
54✔
5985
         if (!tree_has_value(cg)) {
1✔
5986
            diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
1✔
5987
            diag_printf(d, "generic %s in component %s without a default value "
1✔
5988
                        "has no corresponding generic in entity %s",
5989
                        istr(tree_ident(cg)), istr(tree_ident(comp)),
5990
                        istr(tree_ident(entity)));
5991
            diag_hint(d, tree_loc(cg), "generic %s declared here",
1✔
5992
                      istr(tree_ident(cg)));
5993
            diag_emit(d);
1✔
5994
         }
5995

5996
         ok = false;
1✔
5997
         continue;
1✔
5998
      }
5999

6000
      type_t ctype = tree_type(cg);
53✔
6001
      type_t etype = tree_type(match);
53✔
6002
      if (!type_eq(ctype, etype)) {
53✔
6003
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
1✔
6004
         diag_printf(d, "generic %s in component %s has type %s which is "
1✔
6005
                     "incompatible with type %s in entity %s",
6006
                     istr(tree_ident(cg)), istr(tree_ident(comp)),
6007
                     type_pp2(ctype, etype), type_pp2(etype, ctype),
6008
                     istr(tree_ident(entity)));
6009
         diag_hint(d, tree_loc(cg), "declaration of generic %s in component",
1✔
6010
                   istr(tree_ident(cg)));
6011
         diag_hint(d, tree_loc(match), "declaration of generic %s in entity",
1✔
6012
                   istr(tree_ident(match)));
6013
         diag_emit(d);
1✔
6014

6015
         ok = false;
1✔
6016
         continue;
1✔
6017
      }
6018
   }
6019

6020
   const int c_nports = tree_ports(comp);
140✔
6021
   const int e_nports = tree_ports(entity);
140✔
6022
   const int b_nparams = tree_params(bind);
140✔
6023

6024
   for (int i = 0; i < c_nports; i++) {
940✔
6025
      tree_t cp = tree_port(comp, i);
800✔
6026

6027
      tree_t rebind = NULL;
800✔
6028
      for (int j = 0; rebind == NULL && j < b_nparams; j++) {
878✔
6029
         tree_t value = tree_value(tree_param(bind, j));
78✔
6030
         if (tree_kind(value) == T_REF && tree_ref(value) == cp)
78✔
6031
            rebind = value;
42✔
6032
      }
6033

6034
      if (rebind != NULL)
800✔
6035
         continue;   // Ignore for now
42✔
6036

6037
      tree_t match = NULL;
6038
      for (int j = 0; match == NULL && j < e_nports; j++) {
6,793✔
6039
         tree_t ep = tree_port(entity, j);
6,035✔
6040
         if (tree_ident(ep) == tree_ident(cp))
6,035✔
6041
            match = ep;
705✔
6042
      }
6043

6044
      if (match == NULL) {
758✔
6045
         const bool open_ok =
106✔
6046
            tree_has_value(cp)
53✔
6047
            || (tree_subkind(cp) == PORT_OUT
53✔
6048
                && !type_is_unconstrained(tree_type(cp)));
28✔
6049

6050
         if (!open_ok) {
53✔
6051
            diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
2✔
6052
            diag_printf(d, "port %s in component %s without a default value "
2✔
6053
                        "has no corresponding port in entity %s",
6054
                        istr(tree_ident(cp)), istr(tree_ident(comp)),
6055
                        istr(tree_ident(entity)));
6056
            diag_hint(d, tree_loc(cp), "port %s declared here",
2✔
6057
                      istr(tree_ident(cp)));
6058
            diag_emit(d);
2✔
6059
         }
6060

6061
         ok = false;
53✔
6062
         continue;
53✔
6063
      }
6064

6065
      type_t ctype = tree_type(cp);
705✔
6066
      type_t etype = tree_type(match);
705✔
6067
      if (!type_eq(ctype, etype)) {
705✔
6068
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
1✔
6069
         diag_printf(d, "port %s in component %s has type %s which is "
1✔
6070
                     "incompatible with type %s in entity %s",
6071
                     istr(tree_ident(cp)), istr(tree_ident(comp)),
6072
                     type_pp2(ctype, etype), type_pp2(etype, ctype),
6073
                     istr(tree_ident(entity)));
6074
         diag_hint(d, tree_loc(cp), "declaration of port %s in component",
1✔
6075
                   istr(tree_ident(cp)));
6076
         diag_hint(d, tree_loc(match), "declaration of port %s in entity",
1✔
6077
                   istr(tree_ident(match)));
6078
         diag_emit(d);
1✔
6079

6080
         ok = false;
1✔
6081
         continue;
1✔
6082
      }
6083
   }
6084

6085
   return ok;
6086
}
6087

6088
static bool sem_check_configuration(tree_t t, nametab_t *tab)
6089
{
6090
   if (!tree_has_primary(t))
6091
      return false;   // Was parse error
6092

6093
   tree_t of = tree_primary(t);
6094

6095
   // LRM 08 section 3.4.1: for a configuration of a given design
6096
   // entity, both the configuration declaration and the corresponding
6097
   // entity declaration shall reside in the same library
6098
   ident_t elib = ident_until(tree_ident(of), '.');
6099
   if (standard() < STD_19 && elib != lib_name(lib_work())) {
6100
      diag_t *d = pedantic_diag(t);
6101
      if (d != NULL) {
6102
         ident_t ename = ident_rfrom(tree_ident(of), '.');
6103
         diag_printf(d, "configuration declaration %s must reside in the "
6104
                     "same library as entity %s", istr(tree_ident(t)),
6105
                     istr(ename));
6106
         diag_hint(d, NULL, "entity %s is in library %s which is not the same "
6107
                   "as the current working library %s",
6108
                   istr(ename), istr(elib), istr(lib_name(lib_work())));
6109
         diag_lrm(d, STD_08, "3.4");
6110
         diag_emit(d);
6111
         return false;
6112
      }
6113
   }
6114

6115
   return true;
6116
}
6117

6118
static bool sem_check_prot_body(tree_t t, nametab_t *tab)
6119
{
6120
   // Rules for protected type bodies are in LRM 00 section 3.5.2
6121

6122
   type_t type = tree_type(t);
6123
   if (type_is_none(type))
6124
      return false;
6125

6126
   return true;
6127
}
6128

6129
static bool sem_check_implicit_signal(tree_t t, nametab_t *tab)
20✔
6130
{
6131
   tree_t value = tree_value(t);
20✔
6132
   type_t type  = tree_type(t);
20✔
6133

6134
   if (!sem_check(value, tab))
20✔
6135
      return false;
6136

6137
   switch (tree_subkind(t)) {
20✔
6138
   case IMPLICIT_GUARD:
20✔
6139
      if (!sem_check_type(value, type))
20✔
6140
         sem_error(value, "guard expression must have type %s but "
1✔
6141
                   "found %s", type_pp2(type, tree_type(value)),
6142
                   type_pp2(tree_type(value), type));
6143
      break;
6144
   }
6145

6146
   return true;
6147
}
6148

6149
static bool sem_check_context_decl(tree_t t, nametab_t *tab)
16✔
6150
{
6151
   // Context declarations are in LRM 08 section 13.3
6152

6153
   if (!sem_check_context_clause(t, tab))
16✔
6154
      return false;
1✔
6155

6156
   return true;
6157
}
6158

6159
static bool sem_check_context_ref(tree_t t, nametab_t *tab)
12✔
6160
{
6161
   if (standard() >= STD_08) {
12✔
6162
      tree_t unit = find_enclosing(tab, S_DESIGN_UNIT);
12✔
6163

6164
      if (unit != NULL && tree_kind(unit) == T_CONTEXT) {
12✔
6165
         // LRM 08 section 13.3
6166
         ident_t prefix = ident_until(tree_ident(t), '.');
3✔
6167
         if (prefix == well_known(W_WORK))
3✔
6168
            sem_error(t, "selected name in context declaration context "
1✔
6169
                      "reference may not have WORK as a prefix");
6170
      }
6171
   }
6172

6173
   return true;
6174
}
6175

6176
static bool sem_check_disconnect(tree_t t)
8✔
6177
{
6178
   if (!tree_has_ref(t))
8✔
6179
      return false;
6180

6181
   tree_t decl = tree_ref(t);
8✔
6182
   if (class_of(decl) != C_SIGNAL || !is_guarded_signal(decl))
8✔
6183
      sem_error(t, "signal name %s in disconnection specification must denote "
3✔
6184
                "a guarded signal", istr(tree_ident(t)));
6185

6186
   type_t type = tree_type(t);
5✔
6187
   if (!type_eq(tree_type(decl), type))
5✔
6188
      sem_error(t, "type of declared signal %s does not match type %s in "
1✔
6189
                "disconnection specification", type_pp(tree_type(decl)),
6190
                type_pp(type));
6191

6192
   tree_t delay = tree_delay(t);
4✔
6193
   type_t std_time = std_type(NULL, STD_TIME);
4✔
6194
   if (!sem_check_type(delay, std_time))
4✔
6195
      sem_error(delay, "time expression in disconnection specification must "
1✔
6196
                "have type %s but found %s", type_pp(std_time),
6197
                type_pp(tree_type(delay)));
6198

6199
   if (!sem_globally_static(delay))
3✔
6200
      sem_error(delay, "time expression in disconnection specificiation "
1✔
6201
                "must be static");
6202

6203
   return true;
6204
}
6205

6206
static bool sem_check_conv_func(tree_t t, nametab_t *tab)
120✔
6207
{
6208
   if (type_is_none(tree_type(t)))
120✔
6209
      return false;
6210
   else if (!tree_has_ref(t))
120✔
6211
      return false;
6212

6213
   if (!sem_check(tree_value(t), tab))
120✔
UNCOV
6214
      return false;
×
6215

6216
   return true;
6217
}
6218

6219
static bool sem_check_concurrent(tree_t t, nametab_t *tab)
2,755✔
6220
{
6221
   if (tree_has_guard(t) && !sem_check_guard(tree_guard(t)))
2,755✔
6222
      return false;
6223

6224
   if (tree_stmts(t) == 0)
2,753✔
6225
      return false;   // Was parse error
6226

6227
   return sem_check(tree_stmt(t, 0), tab);
2,753✔
6228
}
6229

6230
static bool sem_check_external_name(tree_t t, nametab_t *tab)
105✔
6231
{
6232
   const int nparts = tree_parts(t);
105✔
6233
   for (int i = 0; i < nparts; i++) {
482✔
6234
      tree_t pe = tree_part(t, i);
379✔
6235
      switch (tree_subkind(pe)) {
379✔
6236
      case PE_GENERATE:
28✔
6237
         {
6238
            tree_t value = tree_value(pe);
28✔
6239
            if (!sem_globally_static(value))
28✔
6240
               sem_error(value, "generate index must be a static expression");
1✔
6241
         }
6242
         break;
6243
      case PE_RELATIVE:
79✔
6244
         // Relative pathnames must have enclosing concurrent region
6245
         if (find_enclosing(tab, S_CONCURRENT_BLOCK) == NULL) {
79✔
6246
            diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
1✔
6247
            diag_printf(d, "relative pathname has no enclosing "
1✔
6248
                        "concurrent region");
6249
            diag_lrm(d, STD_08, "8.7");
1✔
6250
            diag_emit(d);
1✔
6251
            return false;
1✔
6252
         }
6253
      }
6254
   }
6255

6256
   // Cannot do any more checking until elaboration
6257
   return true;
6258
}
6259

6260
static port_mode_t sem_default_force_mode(tree_t target)
72✔
6261
{
6262
   // Rules for default force mode in LRM 08 section 10.5.2.1
6263

6264
   tree_t ref = name_to_ref(target);
72✔
6265
   if (ref == NULL || !tree_has_ref(ref))
72✔
6266
      return PORT_IN;
14✔
6267

6268
   tree_t decl = tree_ref(ref);
58✔
6269
   const tree_kind_t dkind = tree_kind(decl);
58✔
6270
   if (dkind == T_PORT_DECL || dkind == T_PARAM_DECL) {
58✔
6271
      switch (tree_subkind(decl)) {
6✔
6272
      case PORT_OUT:
6273
      case PORT_INOUT:
6274
      case PORT_BUFFER:
6275
         return PORT_OUT;
UNCOV
6276
      default:
×
6277
         return PORT_IN;
×
6278
      }
6279
   }
6280

6281
   return PORT_IN;
6282
}
6283

6284
static bool sem_check_force_target(tree_t target, port_mode_t mode,
83✔
6285
                                   const char *what)
6286
{
6287
   tree_t decl = sem_check_lvalue(target);
83✔
6288
   if (decl == NULL)
83✔
6289
      sem_error(target, "target of simple %s assignment must be a "
1✔
6290
                "signal name", what);
6291

6292
   switch (tree_kind(decl)) {
82✔
6293
   case T_SIGNAL_DECL:
6294
      break;
6295

6296
   case T_PORT_DECL:
9✔
6297
   case T_PARAM_DECL:
6298
      if (tree_class(decl) != C_SIGNAL) {
9✔
UNCOV
6299
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(target));
×
6300
         diag_printf(d, "%s is not a valid target of simple %s assignment",
×
6301
                     what, istr(tree_ident(decl)));
UNCOV
6302
         diag_hint(d, tree_loc(target), "target of simple %s assignment", what);
×
6303
         diag_hint(d, tree_loc(decl), "declared with class %s",
×
6304
                   class_str(tree_class(decl)));
UNCOV
6305
         diag_emit(d);
×
6306
         return false;
×
6307
      }
6308
      else if (mode == PORT_OUT && tree_subkind(decl) == PORT_IN)
9✔
6309
         sem_error(target, "force mode OUT may not be used with target "
1✔
6310
                   "of mode IN");
6311
      break;
6312

6313
      case T_EXTERNAL_NAME:
26✔
6314
         if (tree_class(decl) != C_SIGNAL) {
26✔
6315
            tree_t tail = tree_part(decl, tree_parts(decl) - 1);
1✔
6316
            diag_t *d = diag_new(DIAG_ERROR, tree_loc(target));
1✔
6317
            diag_printf(d, "external name %s is not a valid target of "
1✔
6318
                        "simple %s assignment", istr(tree_ident(tail)), what);
6319
            diag_hint(d, tree_loc(target), "target of signal assignment");
1✔
6320
            diag_hint(d, tree_loc(decl), "declared with class %s",
1✔
6321
                      class_str(tree_class(decl)));
6322
            diag_emit(d);
1✔
6323
            return false;
1✔
6324
         }
6325
         break;
6326

6327
   case T_VAR_DECL:
2✔
6328
   case T_CONST_DECL:
6329
      {
6330
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(target));
2✔
6331
         diag_printf(d, "%s %s is not a valid target of simple %s assignment",
2✔
6332
                     class_str(class_of(decl)), istr(tree_ident(decl)), what);
6333
         diag_hint(d, tree_loc(target), "target of simple %s assignment", what);
2✔
6334
         diag_hint(d, tree_loc(decl), "declared as %s",
2✔
6335
                   class_str(class_of(decl)));
6336
         diag_emit(d);
2✔
6337
         return false;
2✔
6338
      }
6339

UNCOV
6340
   default:
×
6341
      sem_error(target, "invalid target of simple %s assignment", what);
×
6342
   }
6343

6344
   return true;
6345
}
6346

6347
static bool sem_check_force(tree_t t, nametab_t *tab)
52✔
6348
{
6349
   tree_t target = tree_target(t);
52✔
6350

6351
   if (!sem_check(target, tab))
52✔
6352
      return false;
6353

6354
   port_mode_t mode = tree_subkind(t);
51✔
6355
   if (mode == PORT_INVALID)
51✔
6356
      tree_set_subkind(t, (mode = sem_default_force_mode(target)));
42✔
6357

6358
   if (!sem_check_force_target(target, mode, "force"))
51✔
6359
      return false;
6360

6361
   tree_t value = tree_value(t);
48✔
6362
   if (!sem_check(value, tab))
48✔
6363
      return false;
6364

6365
   type_t expect = tree_type(target);
48✔
6366

6367
   if (!sem_check_type(value, expect))
48✔
6368
      sem_error(t, "type of force expression %s does not match type of "
2✔
6369
                "target %s", type_pp2(tree_type(value), expect),
6370
                type_pp2(expect, tree_type(value)));
6371

6372
   return true;
6373
}
6374

6375
static bool sem_check_release(tree_t t, nametab_t *tab)
33✔
6376
{
6377
   tree_t target = tree_target(t);
33✔
6378

6379
   if (!sem_check(target, tab))
33✔
6380
      return false;
6381

6382
   port_mode_t mode = tree_subkind(t);
32✔
6383
   if (mode == PORT_INVALID)
32✔
6384
      tree_set_subkind(t, (mode = sem_default_force_mode(target)));
30✔
6385

6386
   if (!sem_check_force_target(target, mode, "release"))
32✔
6387
      return false;
2✔
6388

6389
   return true;
6390
}
6391

6392
static bool sem_check_prot_ref(tree_t t, nametab_t *tab)
6393
{
6394
   if (standard() >= STD_19)
6395
      return true;   // Alias of private variable method
6396

6397
   // There are no legal ways this can appear here and should always
6398
   // have been converted to a call
6399
   assert(error_count() > 0);
6400

6401
   return false;
6402
}
6403

6404
static bool sem_check_view_decl(tree_t t, nametab_t *tab)
6405
{
6406
   type_t type = tree_type(t);
6407
   if (type_is_none(type))
6408
      return false;
6409

6410
   assert(type_kind(type) == T_VIEW);
6411

6412
   type_t rtype = type_designated(type);
6413
   if (!type_is_record(rtype)) {
6414
      assert(error_count() > 0);   // Checked by parser
6415
      return false;
6416
   }
6417
   else if (type_is_resolved(rtype))
6418
      sem_error(t, "subtype indication of a mode view declaration "
6419
                "must denote an unresolved record type");
6420

6421
   const int nfields = type_fields(rtype);
6422

6423
   LOCAL_BIT_MASK have;
6424
   mask_init(&have, nfields);
6425

6426
   const int nelems = type_fields(type);
6427
   for (int i = 0; i < nelems; i++) {
6428
      tree_t e = type_field(type, i);
6429
      assert(tree_kind(e) == T_VIEW_ELEMENT);
6430

6431
      if (!tree_has_ref(e))
6432
         return false;
6433

6434
      tree_t f = tree_ref(e);
6435
      assert(tree_kind(f) == T_FIELD_DECL);
6436

6437
      const int pos = tree_pos(f);
6438
      if (mask_test(&have, pos))
6439
         sem_error(e, "duplicate mode view element definition for field %s",
6440
                   istr(tree_ident(e)));
6441

6442
      mask_set(&have, pos);
6443

6444
      switch (tree_subkind(e)) {
6445
      case PORT_LINKAGE:
6446
         sem_error(e, "element mode indication cannot have mode LINKAGE");
6447

6448
      case PORT_RECORD_VIEW:
6449
      case PORT_ARRAY_VIEW:
6450
         {
6451
            tree_t name = tree_value(e);
6452
            type_t type = tree_type(e);
6453
            type_t view_type = tree_type(name);
6454

6455
            if (type_is_none(view_type))
6456
               return false;
6457

6458
            if (type_kind(view_type) != T_VIEW)
6459
               sem_error(name, "name in element mode view indication of field "
6460
                         "%s does not denote a mode view", istr(tree_ident(f)));
6461

6462
            type_t elem_type = type;
6463
            if (tree_subkind(e) == PORT_ARRAY_VIEW) {
6464
               if (!type_is_array(type))
6465
                  sem_error(e, "field %s with array mode view indication has "
6466
                            "non-array type %s", istr(tree_ident(f)),
6467
                            type_pp(type));
6468

6469
               elem_type = type_elem(type);
6470
            }
6471

6472
            if (!type_eq(elem_type, type_designated(view_type)))
6473
               sem_error(e, "field %s subtype %s is not compatible with mode "
6474
                         "view %s", istr(tree_ident(f)), type_pp(elem_type),
6475
                         type_pp(view_type));
6476
         }
6477
         break;
6478
      }
6479
   }
6480

6481
   if (mask_popcount(&have) != nfields) {
6482
      LOCAL_TEXT_BUF tb = tb_new();
6483
      for (int i = 0, missing = 0; i < nfields; i++) {
6484
         if (!mask_test(&have, i))
6485
            tb_printf(tb, "%s%s", missing++ > 0 ? ", " : "",
6486
                      istr(tree_ident(type_field(rtype, i))));
6487
      }
6488

6489
      sem_error(t, "missing mode view element defintion for %s", tb_get(tb));
6490
   }
6491

6492
   return true;
6493
}
6494

6495
static bool sem_check_cond_value(tree_t t, nametab_t *tab)
45✔
6496
{
6497
   type_t type = tree_type(t);
45✔
6498
   if (type_is_none(type))
45✔
6499
      return false;
6500

6501
   type_t std_bool = std_type(NULL, STD_BOOLEAN);
45✔
6502

6503
   const int nconds = tree_conds(t);
45✔
6504
   for (int i = 0; i < nconds; i++) {
144✔
6505
      tree_t cond = tree_cond(t, i);
103✔
6506
      assert(tree_kind(cond) == T_COND_EXPR);
103✔
6507

6508
      if (tree_has_value(cond)) {
103✔
6509
         tree_t value = tree_value(cond);
64✔
6510
         if (!sem_check(value, tab))
64✔
6511
            return false;
6512

6513
         if (!sem_check_type(value, std_bool))
64✔
6514
            sem_error(value, "type of condition must be %s but have %s",
2✔
6515
                      type_pp(std_bool), type_pp(tree_type(value)));
6516
      }
6517
      else
6518
         assert(i == nconds - 1);
39✔
6519

6520
      if (tree_has_result(cond)) {
101✔
6521
         tree_t result = tree_result(cond);
91✔
6522
         if (!sem_check(result, tab))
91✔
6523
            return false;
6524

6525
         if (!sem_check_type(result, type))
91✔
6526
            sem_error(result, "expected type of conditional expression to be "
101✔
6527
                      "%s but is %s", type_pp(type),
6528
                      type_pp(tree_type(result)));
6529
      }
6530
   }
6531

6532
   return true;
6533
}
6534

6535
static bool sem_check_sequence(tree_t t, nametab_t *tab)
6536
{
6537
   const int ndecls = tree_decls(t);
6538
   for (int i = 0; i < ndecls; i++) {
6539
      // Mark all constant declarations as they need to be treated
6540
      // specially when calculating longest static prefix
6541
      tree_t d = tree_decl(t, i);
6542
      if (tree_kind(d) == T_CONST_DECL)
6543
         tree_set_flag(d, TREE_F_SEQ_BLOCK);
6544
   }
6545

6546
   return true;
6547
}
6548

6549
static bool sem_check_prot_decl(tree_t t, nametab_t *tab)
6550
{
6551
   const int ndecls = tree_decls(t);
6552
   for (int i = 0; i < ndecls; i++) {
6553
      tree_t d = tree_decl(t, i);
6554
      if (tree_kind(d) == T_ALIAS) {
6555
         // LRM 19 section 5.6.2: it is an error if an alias declared
6556
         // within a protected type declaration denotes anything other
6557
         // than a method of a protected type
6558
         tree_t value = tree_value(d);
6559
         if (tree_kind(value) != T_PROT_REF)
6560
            sem_error(d, "an alias declared within a protected type "
6561
                      "declaration must denote a protected type method");
6562
      }
6563
   }
6564

6565
   return true;
6566
}
6567

6568
bool sem_check(tree_t t, nametab_t *tab)
631,272✔
6569
{
6570
   switch (tree_kind(t)) {
631,272✔
6571
   case T_ARCH:
4,077✔
6572
      return sem_check_arch(t, tab);
4,077✔
6573
   case T_PACKAGE:
1,195✔
6574
      return sem_check_package(t, tab);
1,195✔
6575
   case T_ENTITY:
4,068✔
6576
      return sem_check_entity(t, tab);
4,068✔
6577
   case T_TYPE_DECL:
4,243✔
6578
      return sem_check_type_decl(t, tab);
4,243✔
6579
   case T_SUBTYPE_DECL:
1,293✔
6580
      return sem_check_subtype_decl(t, tab);
1,293✔
6581
   case T_PORT_DECL:
2,898✔
6582
      return sem_check_port_decl(t, tab);
2,898✔
6583
   case T_PARAM_DECL:
29,251✔
6584
      return sem_check_param_decl(t, tab);
29,251✔
6585
   case T_GENERIC_DECL:
1,588✔
6586
      return sem_check_generic_decl(t, tab);
1,588✔
6587
   case T_SIGNAL_DECL:
4,798✔
6588
      return sem_check_signal_decl(t, tab);
4,798✔
6589
   case T_VAR_DECL:
10,626✔
6590
      return sem_check_var_decl(t, tab);
10,626✔
6591
   case T_CONST_DECL:
6,177✔
6592
      return sem_check_const_decl(t, tab);
6,177✔
6593
   case T_PROCESS:
4,031✔
6594
      return sem_check_process(t, tab);
4,031✔
6595
   case T_VAR_ASSIGN:
15,823✔
6596
      return sem_check_var_assign(t, tab);
15,823✔
6597
   case T_SIGNAL_ASSIGN:
4,172✔
6598
      return sem_check_signal_assign(t, tab);
4,172✔
6599
   case T_FCALL:
77,075✔
6600
   case T_PROT_FCALL:
6601
      return sem_check_fcall(t, tab);
77,075✔
6602
   case T_LITERAL:
81,743✔
6603
      return sem_check_literal(t);
81,743✔
6604
   case T_STRING:
23,732✔
6605
      return sem_check_string_literal(t);
23,732✔
6606
   case T_REF:
170,447✔
6607
      return sem_check_ref(t, tab);
170,447✔
6608
   case T_WAIT:
7,806✔
6609
      return sem_check_wait(t, tab);
7,806✔
6610
   case T_ASSERT:
17,692✔
6611
      return sem_check_assert(t, tab);
17,692✔
6612
   case T_QUALIFIED:
4,096✔
6613
      return sem_check_qualified(t, tab);
4,096✔
6614
   case T_FUNC_DECL:
5,672✔
6615
      return sem_check_func_decl(t, tab);
5,672✔
6616
   case T_AGGREGATE:
7,916✔
6617
      return sem_check_aggregate(t, tab);
7,916✔
6618
   case T_ATTR_REF:
18,832✔
6619
      return sem_check_attr_ref(t, false, tab);
18,832✔
6620
   case T_ARRAY_REF:
13,465✔
6621
      return sem_check_array_ref(t, tab);
13,465✔
6622
   case T_ARRAY_SLICE:
1,949✔
6623
      return sem_check_array_slice(t, tab);
1,949✔
6624
   case T_INSTANCE:
1,206✔
6625
      return sem_check_instance(t, tab);
1,206✔
6626
   case T_IF:
8,436✔
6627
      return sem_check_if(t, tab);
8,436✔
6628
   case T_NULL:
6629
      return true;
6630
   case T_PACK_BODY:
641✔
6631
      return sem_check_pack_body(t, tab);
641✔
6632
   case T_FUNC_BODY:
6,758✔
6633
      return sem_check_func_body(t, tab);
6,758✔
6634
   case T_RETURN:
12,355✔
6635
      return sem_check_return(t, tab);
12,355✔
6636
   case T_COND_RETURN:
7✔
6637
      return sem_check_cond_return(t, tab);
7✔
6638
   case T_COND_ASSIGN:
1,650✔
6639
      return sem_check_cond_assign(t, tab);
1,650✔
6640
   case T_WHILE:
455✔
6641
      return sem_check_while(t, tab);
455✔
6642
   case T_ALIAS:
1,392✔
6643
      return sem_check_alias(t, tab);
1,392✔
6644
   case T_FOR:
2,300✔
6645
      return sem_check_for(t, tab);
2,300✔
6646
   case T_PROC_DECL:
1,046✔
6647
      return sem_check_proc_decl(t, tab);
1,046✔
6648
   case T_PROC_BODY:
1,965✔
6649
      return sem_check_proc_body(t, tab);
1,965✔
6650
   case T_BLOCK:
186✔
6651
      return sem_check_block(t, tab);
186✔
6652
   case T_CASE:
513✔
6653
   case T_SELECT:
6654
      return sem_check_case(t, tab);
513✔
6655
   case T_EXIT:
403✔
6656
   case T_NEXT:
6657
      return sem_check_loop_control(t, tab);
403✔
6658
   case T_PCALL:
7,535✔
6659
   case T_PROT_PCALL:
6660
      return sem_check_pcall(t, tab);
7,535✔
6661
   case T_ATTR_SPEC:
283✔
6662
      return sem_check_attr_spec(t, tab);
283✔
6663
   case T_ATTR_DECL:
113✔
6664
      return sem_check_attr_decl(t);
113✔
6665
   case T_COMPONENT:
243✔
6666
      return sem_check_component(t, tab);
243✔
6667
   case T_IF_GENERATE:
140✔
6668
      return sem_check_if_generate(t, tab);
140✔
6669
   case T_FOR_GENERATE:
130✔
6670
      return sem_check_for_generate(t, tab);
130✔
6671
   case T_CASE_GENERATE:
7✔
6672
      return sem_check_case(t, tab);
7✔
6673
   case T_OPEN:
67✔
6674
      return sem_check_open(t);
67✔
6675
   case T_FIELD_DECL:
3,311✔
6676
      return sem_check_field_decl(t);
3,311✔
6677
   case T_FILE_DECL:
140✔
6678
      return sem_check_file_decl(t, tab);
140✔
6679
   case T_NEW:
540✔
6680
      return sem_check_new(t, tab);
540✔
6681
   case T_ALL:
2,333✔
6682
      return sem_check_all(t, tab);
2,333✔
6683
   case T_RECORD_REF:
8,161✔
6684
      return sem_check_record_ref(t, tab);
8,161✔
6685
   case T_UNIT_DECL:
142✔
6686
      return sem_check_unit_decl(t);
142✔
6687
   case T_USE:
13,139✔
6688
      return sem_check_use_clause(t, tab);
13,139✔
6689
   case T_TYPE_CONV:
4,440✔
6690
      return sem_check_conversion(t, tab);
4,440✔
6691
   case T_SPEC:
150✔
6692
      return sem_check_spec(t, tab);
150✔
6693
   case T_BINDING:
143✔
6694
      return sem_check_binding(t, tab);
143✔
6695
   case T_LIBRARY:
22,091✔
6696
      return sem_check_library_clause(t, tab);
22,091✔
6697
   case T_CONFIGURATION:
94✔
6698
      return sem_check_configuration(t, tab);
94✔
6699
   case T_PROT_BODY:
182✔
6700
      return sem_check_prot_body(t, tab);
182✔
6701
   case T_CONTEXT:
16✔
6702
      return sem_check_context_decl(t, tab);
16✔
6703
   case T_CONTEXT_REF:
12✔
6704
      return sem_check_context_ref(t, tab);
12✔
6705
   case T_BLOCK_CONFIG:
135✔
6706
      return sem_check_block_config(t, tab);
135✔
6707
   case T_IMPLICIT_SIGNAL:
20✔
6708
      return sem_check_implicit_signal(t, tab);
20✔
6709
   case T_DISCONNECT:
8✔
6710
      return sem_check_disconnect(t);
8✔
6711
   case T_GROUP:
6712
   case T_GROUP_TEMPLATE:
6713
   case T_BOX:
6714
   case T_PSL:
6715
      return true;
6716
   case T_CONV_FUNC:
120✔
6717
      return sem_check_conv_func(t, tab);
120✔
6718
   case T_CONCURRENT:
2,755✔
6719
      return sem_check_concurrent(t, tab);
2,755✔
6720
   case T_PACK_INST:
197✔
6721
      return sem_check_pack_inst(t, tab);
197✔
6722
   case T_EXTERNAL_NAME:
105✔
6723
      return sem_check_external_name(t, tab);
105✔
6724
   case T_FORCE:
52✔
6725
      return sem_check_force(t, tab);
52✔
6726
   case T_RELEASE:
33✔
6727
      return sem_check_release(t, tab);
33✔
6728
   case T_PROT_REF:
15✔
6729
      return sem_check_prot_ref(t, tab);
15✔
6730
   case T_MATCH_CASE:
40✔
6731
   case T_MATCH_SELECT:
6732
      return sem_check_match_case(t, tab);
40✔
6733
   case T_FUNC_INST:
57✔
6734
   case T_PROC_INST:
6735
      return sem_check_subprogram_inst(t, tab);
57✔
6736
   case T_VIEW_DECL:
44✔
6737
      return sem_check_view_decl(t, tab);
44✔
6738
   case T_COND_VALUE:
45✔
6739
      return sem_check_cond_value(t, tab);
45✔
6740
   case T_SEQUENCE:
8✔
6741
      return sem_check_sequence(t, tab);
8✔
6742
   case T_PROT_DECL:
188✔
6743
      return sem_check_prot_decl(t, tab);
188✔
UNCOV
6744
   default:
×
6745
      sem_error(t, "cannot check %s", tree_kind_str(tree_kind(t)));
×
6746
   }
6747
}
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