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

nickg / nvc / 23165126813

16 Mar 2026 08:45PM UTC coverage: 92.475% (-0.006%) from 92.481%
23165126813

Pull #1447

github

web-flow
Merge fe21da529 into 74caf141c
Pull Request #1447: Adding VHDL-2019 range records.

108 of 122 new or added lines in 5 files covered. (88.52%)

410 existing lines in 4 files now uncovered.

77899 of 84238 relevant lines covered (92.47%)

444363.26 hits per line

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

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

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

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

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

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

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

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

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

68
static bool sem_check_resolution(type_t type, tree_t res)
355✔
69
{
70
   // Resolution functions are described in LRM 08 section 4.6
71

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

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

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

96
   if (!tree_has_ref(res))
354✔
97
      return false;
98

99
   tree_t fdecl = tree_ref(res);
350✔
100
   assert(is_subprogram(fdecl));
350✔
101

102
   type_t ftype = tree_type(fdecl);
350✔
103

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

108
   // Must take a single parameter of class constant that is a one
109
   // dimenstional array array of the base type
110

111
   if (type_params(ftype) != 1)
349✔
112
      sem_error(res, "resolution function must have a single argument");
1✔
113

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

119
   if (!type_eq(type_elem(param), type))
347✔
120
      sem_error(res, "parameter of resolution function must be "
1✔
121
                "an array of %pT but found %pT", type, type_elem(param));
122

123
   tree_t p = tree_port(fdecl, 0);
346✔
124

125
   if (dimension_of(param) != 1) {
346✔
126
      diag_t *d = diag_new(DIAG_ERROR, tree_loc(res));
1✔
127
      diag_printf(d, "parameter of resolution function must be a one "
1✔
128
                  "dimensional array");
129
      diag_hint(d, tree_loc(p), "parameter %pI declarared with type %pT",
1✔
130
                tree_ident(p), param);
131
      diag_lrm(d, STD_93, "2.4");
1✔
132
      diag_emit(d);
1✔
133
      return false;
1✔
134
   }
135

136
   if (tree_class(p) != C_CONSTANT) {
345✔
137
      diag_t *d = diag_new(DIAG_ERROR, tree_loc(res));
1✔
138
      diag_printf(d, "resolution function parameter must have class constant");
1✔
139
      diag_hint(d, tree_loc(p), "parameter %pI declarared with class %s",
1✔
140
                tree_ident(p), class_str(tree_class(p)));
141
      diag_lrm(d, STD_93, "2.4");
1✔
142
      diag_emit(d);
1✔
143
      return false;
1✔
144
   }
145

146
   // Return type must be the resolved type
147

148
   if (!type_eq(type_result(ftype), type))
344✔
149
      sem_error(res, "result of resolution function must be %pT but have %pT",
1✔
150
                type, type_result(ftype));
151

152
   return true;
153
}
154

155
static bool sem_check_range(tree_t r, type_t expect, nametab_t *tab)
23,092✔
156
{
157
   if (expect != NULL && type_is_none(expect))
23,092✔
158
      return false;   // Prevent cascading errors
159

160
   switch (tree_subkind(r)) {
23,071✔
161
   case RANGE_EXPR:
5,122✔
162
      {
163
         tree_t expr = tree_value(r);
5,122✔
164

165
         type_t type = tree_type(expr);
5,122✔
166
         if (type_is_none(type))
5,122✔
167
            return false;   // Was earlier error
168

169
         if (tree_kind(expr) != T_ATTR_REF)
5,119✔
UNCOV
170
            sem_error(expr, "invalid expression in range constraint");
×
171

172
         const attr_kind_t kind = tree_subkind(expr);
5,119✔
173
         if (kind != ATTR_RANGE && kind != ATTR_REVERSE_RANGE)
5,119✔
174
            sem_error(expr, "cannot use attribute %pI as range",
2✔
175
                      tree_ident(expr));
176

177
         if (!sem_check_attr_ref(expr, true, tab))
5,117✔
178
            return false;
179

180
         if (expect && !sem_check_type(expr, expect, tab))
5,112✔
181
            sem_error(expr, "expected type of range bound to be %pT but is %pT",
2✔
182
                      expect, type);
183
      }
184
      break;
185

186
   case RANGE_TO:
17,948✔
187
   case RANGE_DOWNTO:
188
      {
189
         tree_t left = tree_left(r);
17,948✔
190
         if (!sem_check(left, tab))
17,948✔
191
            return false;
192

193
         tree_t right = tree_right(r);
17,947✔
194
         if (!sem_check(right, tab))
17,947✔
195
            return false;
196

197
         if (expect != NULL) {
17,944✔
198
            if (!sem_check_same_type(left, right))
17,721✔
199
               sem_error(right, "type mismatch in range: left is %pT,"
3✔
200
                         " right is %pT", tree_type(left), tree_type(right));
201

202
            if (!sem_check_type(left, expect, tab))
17,718✔
203
               sem_error(r, "expected type of range bounds to be %pT but"
4✔
204
                         " have %pT", expect, tree_type(left));
205

206
            // This cannot fail because we know left and right have the
207
            // same type and left is equal to expect, but we still need
208
            // to call sem_check_type for the implicit conversion
209
            sem_check_type(right, expect, tab);
17,714✔
210
            sem_check_type(r, expect, tab);
17,714✔
211
         }
212
      }
213
      break;
214
   }
215

216
   return true;
217
}
218

219
static bool sem_check_discrete_range(tree_t r, type_t expect, nametab_t *tab)
19,400✔
220
{
221
   if (!sem_check_range(r, expect ?: tree_type(r), tab))
19,400✔
222
      return false;
223

224
   const range_kind_t kind = tree_subkind(r);
19,362✔
225
   type_t type = tree_type(r);
19,362✔
226
   if (type_is_none(type) || kind == RANGE_ERROR)
19,362✔
227
      return false;
228

229
   if (!type_is_discrete(type))
19,361✔
230
      sem_error(r, "type of range bounds %pT is not discrete", type);
2✔
231

232
   // See LRM 93 section 3.2.1.1: universal integer bound must be a
233
   // numeric literal or attribute. Later LRMs relax the wording here.
234
   if (standard() < STD_00 && !relaxed_rules() && kind != RANGE_EXPR) {
19,359✔
235
      tree_t left  = tree_left(r);
7,320✔
236
      tree_t right = tree_right(r);
7,320✔
237

238
      type_t left_type  = tree_type(left);
7,320✔
239
      type_t right_type = tree_type(right);
7,320✔
240

241
      if (type_is_universal(left_type) && type_is_universal(right_type)) {
7,320✔
242
         tree_kind_t lkind = tree_kind(left);
1✔
243
         tree_kind_t rkind = tree_kind(right);
1✔
244

245
         const bool invalid =
2✔
246
            lkind != T_LITERAL && lkind != T_ATTR_REF
1✔
247
            && rkind != T_LITERAL && rkind != T_ATTR_REF;
1✔
248

249
         if (invalid)
1✔
UNCOV
250
            sem_error(r, "universal integer bound must be numeric"
×
251
                      " literal or attribute");
252
      }
253
   }
254

255
   return true;
256
}
257

258
static bool sem_check_constraint(tree_t constraint, type_t base, nametab_t *tab)
16,790✔
259
{
260
   const constraint_kind_t consk = tree_subkind(constraint);
16,790✔
261
   switch (consk) {
16,790✔
262
   case C_RANGE:
3,464✔
263
      if (!type_is_scalar(base))
3,464✔
264
         sem_error(constraint, "range constraint cannot be used with "
2✔
265
                   "non-scalar type %pT", base);
266
      break;
267

268
   case C_INDEX:
12,946✔
269
      if (!type_is_array(base))
12,946✔
270
         sem_error(constraint, "index constraint cannot be used with "
5✔
271
                   "non-array type %pT", base);
272
      break;
273

274
   case C_RECORD:
380✔
275
      {
276
         if (!type_is_record(base))
380✔
UNCOV
277
            sem_error(constraint, "record element constraint cannot be used "
×
278
                      "with non-record type %pT", base);
279

280
         // Range list is overloaded to hold record element constraints
281
         const int nelem = tree_ranges(constraint);
380✔
282
         for (int i = 0; i < nelem; i++) {
994✔
283
            tree_t ei = tree_range(constraint, i);
619✔
284
            assert(tree_kind(ei) == T_ELEM_CONSTRAINT);
619✔
285

286
            if (!tree_has_ref(ei))
619✔
287
               return false;   // Was parse error
288

289
            tree_t decl = tree_ref(ei);
616✔
290
            assert(tree_kind(decl) == T_FIELD_DECL);  // Checked by parser
616✔
291

292
            type_t ftype = tree_type(decl);
616✔
293
            if (!type_is_unconstrained(ftype))
616✔
294
               sem_error(constraint, "field %pI in record element constraint "
1✔
295
                         "is already constrained", tree_ident(decl));
296

297
            type_t sub = tree_type(ei);
615✔
298
            if (!sem_check_subtype(decl, sub, tab))
615✔
299
               return false;
300

301
            // Check for duplicate element constraints
302
            tree_t fi = tree_ref(ei);
615✔
303
            for (int j = 0; j < i; j++) {
1,028✔
304
               tree_t ej = tree_range(constraint, j);
414✔
305
               if (tree_pos(tree_ref(ej)) == tree_pos(fi))
414✔
306
                  sem_error(ei, "duplicate record element constraint for "
414✔
307
                            "field %pI", tree_ident(fi));
308
            }
309
         }
310

311
         // Code belows handles index and range constraints
312
         return true;
313
      }
314
   }
315

316
   if (type_is_array(base)) {
16,403✔
317
      if (type_kind(base) == T_SUBTYPE && !type_is_unconstrained(base))
12,941✔
318
         sem_error(constraint, "cannot change constraints of constrained "
2✔
319
                   "array type %pT", base);
320
   }
321
   else if (type_is_record(base) && standard() < STD_08)
3,462✔
UNCOV
322
      sem_error(constraint, "record subtype may not have constraints "
×
323
                "in VHDL-%s", standard_text(standard()));
324

325
   const int ndims_base = type_is_array(base) ? dimension_of(base) : 1;
16,401✔
326
   const int ndims = tree_ranges(constraint);
16,401✔
327

328
   if (ndims != ndims_base)
16,401✔
329
      sem_error(constraint, "expected %d constraints for type %pT but found %d",
2✔
330
                ndims_base, base, ndims);
331

332
   for (int i = 0; i < ndims; i++) {
33,193✔
333
      tree_t r = tree_range(constraint, i);
16,807✔
334
      type_t index = index_type_of(base, i);
16,807✔
335

336
      switch (consk) {
16,807✔
337
      case C_INDEX:
13,345✔
338
         if (!sem_check_discrete_range(r, index, tab))
13,345✔
339
            return false;
340
         break;
341

342
      case C_RANGE:
3,462✔
343
         if (!sem_check_range(r, index, tab))
3,462✔
344
            return false;
345
         break;
346

347
      default:
348
         break;
349
      }
350
   }
351

352
   return true;
353
}
354

355
static bool sem_check_subtype_helper(tree_t decl, type_t type, nametab_t *tab)
17,333✔
356
{
357
   // Shared code for checking subtype declarations and implicit subtypes
358

359
   type_t base = type_base(type);
17,333✔
360
   if (type_is_none(base))
17,333✔
361
      return false;
362
   else if (type_is_access(base))
17,322✔
363
      base = type_designated(base);
2✔
364

365
   assert(!is_anonymous_subtype(base));
17,322✔
366

367
   if (type_is_protected(base))
17,322✔
368
      sem_error(decl, "subtypes may not have protected base types");
1✔
369
   else if (!sem_check_incomplete(decl, type))
17,321✔
370
      return false;
371

372
   if (type_has_constraint(type)) {
17,320✔
373
      tree_t cons = type_constraint(type);
16,790✔
374
      if (!sem_check_constraint(cons, base, tab))
16,790✔
375
         return false;
376

377
      // Check the subtype does not already have an index constraint in
378
      // this position
379
      for (type_t b = base; type_kind(b) == T_SUBTYPE; b = type_base(b)) {
18,445✔
380
         if (type_has_constraint(b)) {
1,685✔
381
            tree_t econs = type_constraint(b);
92✔
382
            if (tree_subkind(econs) == C_INDEX) {
92✔
383
               diag_t *d = diag_new(DIAG_ERROR, tree_loc(cons));
1✔
384
               diag_printf(d, "duplicate index constraint for type %pT", base);
1✔
385
               diag_hint(d, tree_loc(econs), "already constrained here");
1✔
386
               diag_emit(d);
1✔
387
               return false;
1✔
388
            }
389
         }
390
      }
391
   }
392

393
   if (type_is_array(type) && type_has_elem(type)) {
17,290✔
394
      type_t elem = type_elem(type);
404✔
395
      if (is_anonymous_subtype(elem)) {
404✔
396
         // Anonymous subtype created for array element constraint
397
         assert(standard() >= STD_08);
316✔
398

399
         if (!sem_check_subtype_helper(decl, elem, tab))
316✔
400
            return false;
401
      }
402
   }
403

404
   if (type_has_resolution(type)) {
17,289✔
405
      if (!sem_check_resolution(type_base(type), type_resolution(type)))
355✔
406
         return false;
12✔
407
   }
408

409
   return true;
410
}
411

412
static bool sem_check_subtype(tree_t decl, type_t type, nametab_t *tab)
79,381✔
413
{
414
   // Check an anonymous subtype at the point of use
415

416
   if (type_kind(type) != T_SUBTYPE)
79,381✔
417
      return true;
418
   else if (type_has_ident(type))
29,787✔
419
      return true;   // Explicitly declared subtype
420

421
   return sem_check_subtype_helper(decl, type, tab);
14,661✔
422
}
423

424
static bool sem_check_use_clause(tree_t c, nametab_t *tab)
16,048✔
425
{
426
   if (standard() >= STD_08) {
16,048✔
427
      tree_t unit = find_enclosing(tab, S_DESIGN_UNIT);
6,270✔
428

429
      if (unit != NULL && tree_kind(unit) == T_CONTEXT) {
6,270✔
430
         // LRM 08 section 13.3
431
         ident_t prefix = ident_until(tree_ident(c), '.');
35✔
432
         if (prefix == well_known(W_WORK))
35✔
433
            sem_error(c, "selected name in context declaration use clause "
1✔
434
                      "may not have WORK as a prefix");
435
      }
436
   }
437

438
   return true;
439
}
440

441
static bool sem_check_library_clause(tree_t t, nametab_t *tab)
27,157✔
442
{
443
   if (standard() >= STD_08) {
27,157✔
444
      ident_t name = tree_ident(t);
10,245✔
445
      tree_t unit = find_enclosing(tab, S_DESIGN_UNIT);
10,245✔
446

447
      if (unit != NULL && tree_kind(unit) == T_CONTEXT) {
10,245✔
448
         // LRM 08 section 13.3
449
         if (name == well_known(W_WORK))
13✔
450
            sem_error(t, "library clause in a context declaration may not have "
1✔
451
                      "logical library name WORK");
452
      }
453
   }
454

455
   return true;
456
}
457

458
static bool sem_check_context_clause(tree_t t, nametab_t *tab)
13,040✔
459
{
460
   // Ignore the implicit WORK and STD with context declarations
461
   const int ignore = tree_kind(t) == T_CONTEXT ? 2 : 0;
13,040✔
462

463
   bool ok = true;
13,040✔
464
   const int ncontexts = tree_contexts(t);
13,040✔
465
   for (int n = ignore; n < ncontexts; n++)
56,266✔
466
      ok = sem_check(tree_context(t, n), tab) && ok;
43,229✔
467

468
   return ok;
13,040✔
469
}
470

471
static bool sem_check_readable(tree_t t)
218,198✔
472
{
473
   switch (tree_kind(t)) {
228,752✔
474
   case T_REF:
90,557✔
475
      {
476
         if (tree_flags(t) & TREE_F_FORMAL_NAME)
90,557✔
477
            return true;   // Name appearing in formal
478

479
         tree_t decl = tree_ref(t);
90,557✔
480
         switch (tree_kind(decl)) {
90,557✔
481
         case T_PORT_DECL:
1,991✔
482
            {
483
               const port_mode_t mode = tree_subkind(decl);
1,991✔
484
               if (mode == PORT_OUT && standard() < STD_08) {
1,991✔
485
                  diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
9✔
486
                  diag_printf(d, "cannot read output port %s",
9✔
487
                              istr(tree_ident(t)));
488
                  diag_hint(d, tree_loc(decl), "%s declared with mode OUT",
9✔
489
                            istr(tree_ident(decl)));
490
                  diag_hint(d, tree_loc(t), "read here");
9✔
491
                  diag_hint(d, NULL, "outputs can be read with "
9✔
492
                            "$bold$--std=2008$$");
493
                  diag_emit(d);
9✔
494
                  return false;
9✔
495
               }
496
               else if (mode == PORT_LINKAGE)
1,982✔
497
                  sem_error(t, "linkage port %s may not be read except as "
1✔
498
                            "an actual corresponding to an interface of mode "
499
                            "linkage", istr(tree_ident(t)));
500
            }
501
            break;
502

503
         case T_PARAM_DECL:
25,814✔
504
            if (tree_subkind(decl) == PORT_OUT) {
25,814✔
505
               // LRM 08 section 6.5.2: The value of the interface
506
               // object is allowed to be updated and, provided it is
507
               // not a signal parameter, read.
508
               if (standard() < STD_08)
5✔
509
                  sem_error(t, "cannot read OUT parameter %s",
3✔
510
                            istr(tree_ident(t)));
511
               else if (tree_class(decl) == C_SIGNAL) {
2✔
512
                  diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
1✔
513
                  diag_printf(d, "cannot read OUT signal parameter %s",
1✔
514
                              istr(tree_ident(t)));
515
                  diag_hint(d, NULL, "OUT parameters can be read provided they "
1✔
516
                            "are not signal parameters");
517
                  diag_lrm(d, STD_08, "6.5.2");
1✔
518
                  diag_emit(d);
1✔
519
                  return false;
1✔
520
               }
521
            }
522
            break;
523

524
         default:
525
            break;
526
         }
527

528
         return true;
529
      }
530

531
   case T_ARRAY_REF:
10,554✔
532
   case T_ARRAY_SLICE:
533
      return sem_check_readable(tree_value(t));
10,554✔
534

535
   default:
536
      return true;
537
   }
538
}
539

540
static bool sem_check_type(tree_t t, type_t expect, nametab_t *tab)
403,190✔
541
{
542
   type_t actual = tree_type(t);
403,190✔
543

544
   if (type_eq_map(actual, expect, get_generic_map(tab)))
403,190✔
545
      return true;
546

547
   // Supress cascading errors
548
   if (type_is_none(actual) || type_is_none(expect))
320✔
549
      return true;
5✔
550

551
   return false;
552
}
553

554
static bool sem_check_same_type(tree_t left, tree_t right)
36,286✔
555
{
556
   type_t left_type  = tree_type(left);
36,286✔
557
   type_t right_type = tree_type(right);
36,286✔
558

559
   if (type_eq(left_type, right_type))
36,286✔
560
      return true;
561

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

566
   return false;
567
}
568

569
static bool sem_has_access(type_t t)
73,539✔
570
{
571
   // returns true if the type is an access type or is a composite
572
   // type that contains a subelement of an access type
573
   type_t base = type_base_recur(t);
105,887✔
574

575
   if (type_is_access(base))
105,887✔
576
      return true;
577

578
   if (type_is_array(base))
105,028✔
579
      return sem_has_access(type_elem(base));
32,348✔
580

581
   if (type_is_record(base)) {
72,680✔
582
      const int nfields = type_fields(base);
4,907✔
583
      for (int i = 0; i < nfields; i++) {
23,378✔
584
         if (sem_has_access(tree_type(type_field(base, i))))
18,564✔
585
            return true;
586
      }
587
   }
588

589
   return false;
590
}
591

592
static bool sem_check_type_decl(tree_t t, nametab_t *tab)
5,231✔
593
{
594
   type_t type = tree_type(t);
5,231✔
595

596
   // Nothing more to do for incomplete types
597
   if (type_kind(type) == T_INCOMPLETE)
5,231✔
598
      return true;
599

600
   type_kind_t kind = type_kind(type);
5,183✔
601

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

611
      type = type_base(type);
803✔
612
      kind = type_kind(type);
803✔
613
      assert(kind == T_ARRAY);
803✔
614
   }
615

616
   switch (kind) {
5,179✔
617
   case T_ARRAY:
2,675✔
618
      {
619
         type_t elem_type = type_elem(type);
2,675✔
620
         if (!sem_check_subtype(t, elem_type, tab))
2,675✔
621
            return false;
622

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

632
         if (type_is_file(elem_type))
2,674✔
633
            sem_error(t, "array %pI cannot have element of file type",
1✔
634
                      tree_ident(t));
635
         else if (type_is_protected(elem_type))
2,673✔
636
            sem_error(t, "array %pI cannot have element of protected type",
1✔
637
                      tree_ident(t));
638
         else if (!sem_check_incomplete(t, elem_type))
2,672✔
639
            return false;
640

641
         const int nindex = type_indexes(type);
2,671✔
642
         for (int i = 0; i < nindex; i++) {
5,703✔
643
            type_t index_type = type_index(type, i);
3,037✔
644
            if (type_is_none(index_type))
3,037✔
645
               return false;
646
            else if (!type_is_discrete(index_type))
3,034✔
647
               sem_error(t, "index type %pT is not discrete", index_type);
3,034✔
648
         }
649

650
         return true;
651
      }
652

653
   case T_ENUM:
654
      return true;
655

656
   case T_PHYSICAL:
54✔
657
      {
658
         const int nunits = type_units(type);
54✔
659
         for (int i = 0; i < nunits; i++) {
239✔
660
            tree_t u = type_unit(type, i);
187✔
661
            tree_set_type(u, type);
187✔
662
            if (!sem_check(u, tab))
187✔
663
               return false;
664

665
            tree_t value = tree_value(u);
187✔
666

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

673
            if (i > 0 && !sem_check_type(value, type, tab))
186✔
674
               sem_error(value, "secondary unit %pI must have type %pT",
186✔
675
                         tree_ident(u), type);
676
         }
677
      }
678

679
      // Fall-through
680
   case T_INTEGER:
681
      {
682
         tree_t r = type_dim(type, 0);
207✔
683

684
         if (!sem_check_range(r, NULL, tab))
207✔
685
            return false;
686

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

704
         case RANGE_EXPR:
3✔
705
            if (!type_is_integer(tree_type(r)))
3✔
706
               sem_error(r, "type of range bounds must be of some integer "
1✔
707
                         "type but have %pT", tree_type(r));
708
            break;
709
         }
710

711
         if (!sem_locally_static(r))
203✔
712
            sem_error(r, "range constraint of type %pT must be locally static",
1✔
713
                      type);
714

715
         tree_set_type(r, type);
202✔
716
         return true;
202✔
717
      }
718

719
   case T_REAL:
23✔
720
      {
721
         tree_t r = type_dim(type, 0);
23✔
722

723
         if (!sem_check_range(r, NULL, tab))
23✔
724
            return false;
725

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

745
         case RANGE_EXPR:
1✔
746
            assert(type_is_real(tree_type(r)));   // Unreachable
1✔
747
            break;
748
         }
749

750
         if (!sem_locally_static(r))
20✔
751
            sem_error(r, "range constraint of type %pT must be locally static",
1✔
752
                      type);
753

754
         tree_set_type(r, type);
19✔
755
         return true;
19✔
756
      }
757

758
   case T_RECORD:
1,351✔
759
      {
760
         const int nfields = type_fields(type);
1,351✔
761
         for (int i = 0; i < nfields; i++) {
5,333✔
762
            tree_t f = type_field(type, i);
3,988✔
763

764
            if (!sem_check(f, tab))
3,988✔
765
               return false;
766

767
            // Each field name must be distinct
768
            ident_t f_name = tree_ident(f);
3,987✔
769
            for (int j = 0; j < i; j++) {
12,941✔
770
               tree_t fj = type_field(type, j);
8,955✔
771
               if (ident_casecmp(f_name, tree_ident(fj))) {
8,955✔
772
                  diag_t *d = diag_new(DIAG_ERROR, tree_loc(f));
1✔
773
                  diag_printf(d, "duplicate field name %pI", f_name);
1✔
774
                  diag_hint(d, tree_loc(fj), "previously declared here");
1✔
775
                  diag_hint(d, tree_loc(f), "declared again here");
1✔
776
                  diag_emit(d);
1✔
777
                  return false;
1✔
778
               }
779
            }
780

781
            type_t f_type = tree_type(f);
3,986✔
782

783
            if (!sem_check_subtype(f, f_type, tab))
3,986✔
784
               return false;
785

786
            // Recursive record types are not allowed
787
            if (type_eq(type, f_type))
3,985✔
UNCOV
788
               sem_error(f, "recursive record types are not allowed");
×
789

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

809
         return true;
810
      }
811

812
   case T_FILE:
101✔
813
      // Rules for file types are in LRM 93 section 3.4
814
      {
815
         type_t f = type_designated(type);
101✔
816

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

831
         if (sem_has_access(f))
98✔
832
            sem_error(t, "type %s has a subelement with an access type",
4✔
833
                      type_pp(f));
834

835
         type_t base_f = type_base_recur(f);
94✔
836
         if (type_is_array(base_f) && dimension_of(base_f) > 1)
94✔
837
            sem_error(t, "array type for file type must be one-dimensional");
2✔
838

839
         return true;
840
      }
841

842
   case T_ACCESS:
332✔
843
      // Rules for access types are in LRM 93 section 3.3
844
      {
845
         type_t designated = type_designated(type);
332✔
846

847
         if (!sem_check_subtype(t, designated, tab))
332✔
848
            return false;
849

850
         if (standard() < STD_19) {
331✔
851
            if (type_is_file(designated))
274✔
852
               sem_error(t, "access type %s cannot designate file type",
1✔
853
                         istr(tree_ident(t)));
854

855
            if (type_is_protected(designated))
273✔
856
               sem_error(t, "access type %s cannot designate protected type",
1✔
857
                         istr(tree_ident(t)));
858
         }
859

860
         return true;
861
      }
862

863
   case T_PROTECTED:
864
   default:
865
      return true;
866
   }
867
}
868

869
static bool sem_check_subtype_decl(tree_t t, nametab_t *tab)
1,549✔
870
{
871
   type_t type = tree_type(t);
1,549✔
872
   assert(type_kind(type) == T_SUBTYPE);
1,549✔
873
   assert(type_has_ident(type));
1,549✔
874

875
   return sem_check_subtype_helper(t, type, tab);
1,549✔
876
}
877

878
static bool sem_check_incomplete(tree_t t, type_t type)
126,712✔
879
{
880
   if (type_is_incomplete(type)) {
126,712✔
881
      diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
13✔
882
      diag_printf(d, "invalid use of incomplete type %pT", type);
13✔
883
      diag_hint(d, NULL, "prior to the end of the corresponding full type "
13✔
884
                "declaration, an incomplete type may only be used as the "
885
                "designated type of an access type declaration");
886
      diag_lrm(d, STD_08, "5.4.2");
13✔
887
      diag_emit(d);
13✔
888

889
      return false;
13✔
890
   }
891

892
   return true;
893
}
894

895
static bool sem_no_access_file_or_protected(tree_t t, type_t type, const char *what)
13,948✔
896
{
897
   // constants, signals, attributes, generics, ports
898
   // may not be of an access, file, or protected type, or
899
   // of a composite type with a subelement of an access type
900

901
   if (type_is_access(type))
13,948✔
902
      sem_error(t, "%s may not have access type", what);
4✔
903

904
   if (sem_has_access(type))
13,944✔
905
      sem_error(t, "%s may not have a type with a subelement of access type", what);
6✔
906

907
   if (type_is_protected(type))
13,938✔
908
      sem_error(t, "%s may not have protected type", what);
3✔
909

910
   if (type_is_file(type))
13,935✔
911
      sem_error(t, "%s may not have file type", what);
4✔
912

913
   return true;
914
}
915

916
static void sem_unconstrained_decl_hint(diag_t *d, type_t type)
15✔
917
{
918
   if (!type_is_record(type))
15✔
919
      return;
920

921
   // Tell the user which field is unconstrained
922

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

935
static bool sem_check_const_decl(tree_t t, nametab_t *tab)
7,123✔
936
{
937
   type_t type = tree_type(t);
7,123✔
938

939
   if (!sem_check_subtype(t, type, tab))
7,123✔
940
      return false;
941
   else if (type_is_none(type))
7,119✔
942
      return false;
943
   else if (!sem_check_incomplete(t, type))
7,104✔
944
      return false;
945

946
   if (!sem_no_access_file_or_protected(t, type, "constants"))
7,104✔
947
      return false;
948

949
   tree_t fwd = find_forward_decl(tab, t);
7,099✔
950

951
   if (tree_has_value(t)) {
7,099✔
952
      tree_t value = tree_value(t);
6,683✔
953
      if (!sem_check(value, tab))
6,683✔
954
         return false;
955

956
      if (!sem_check_type(value, type, tab))
6,651✔
957
         sem_error(value, "type of initial value %pT does not match type "
5✔
958
                   "of declaration %pT", tree_type(value), type);
959

960
      if (fwd == NULL && type_is_unconstrained(type))
6,646✔
961
         tree_set_type(t, (type = merge_constraints(type, tree_type(value))));
1,029✔
962

963
      // A constant with a globaly static value should be treated as
964
      // globally static regardless of where it is declared
965
      if (sem_globally_static(value)) {
6,646✔
966
         tree_set_flag(t, TREE_F_GLOBALLY_STATIC);
4,787✔
967

968
         // A reference to a constant with a locally static subype and
969
         // locally static value is locally static
970
         if (sem_locally_static(value)
4,787✔
971
             && sem_static_subtype(type, sem_locally_static))
3,718✔
972
            tree_set_flag(t, TREE_F_LOCALLY_STATIC);
3,585✔
973
      }
974
   }
975
   else if (tree_kind(find_enclosing(tab, S_DESIGN_UNIT)) != T_PACKAGE)
416✔
976
      sem_error(t, "deferred constant declarations are only permitted "
1✔
977
                "in packages");
978

979
   if (fwd != NULL && !type_strict_eq(tree_type(fwd), type)) {
7,061✔
980
      diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
1✔
981
      diag_printf(d, "expected type %pT for deferred constant %pI but "
1✔
982
                  "found %pT", tree_type(fwd), tree_ident(t), type);
983
      diag_hint(d, tree_loc(fwd), "originally declared with type %pT",
1✔
984
                tree_type(fwd));
985
      diag_hint(d, tree_loc(t), "type here is %pT", type);
1✔
986
      diag_emit(d);
1✔
987
      return false;
1✔
988
   }
989

990
   return true;
991
}
992

993
static bool sem_check_signal_decl(tree_t t, nametab_t *tab)
6,739✔
994
{
995
   type_t type = tree_type(t);
6,739✔
996

997
   if (!sem_check_subtype(t, type, tab))
6,739✔
998
      return false;
999
   else if (type_is_none(type))
6,729✔
1000
      return false;
1001

1002
   const bool is_unconstrained = type_is_unconstrained(type);
6,727✔
1003

1004
   if (is_unconstrained) {
6,727✔
1005
      if (standard() < STD_19) {
30✔
1006
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
6✔
1007
         diag_printf(d, "declaration of signal %pI cannot have unconstrained "
6✔
1008
                     "type %pT", tree_ident(t), type);
1009
         sem_unconstrained_decl_hint(d, type);
6✔
1010
         diag_emit(d);
6✔
1011
         return false;
6✔
1012
      }
1013
      else if (!tree_has_value(t)) {
24✔
1014
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
1✔
1015
         diag_printf(d, "declaration of signal %pI without an initial value "
1✔
1016
                     "cannot have unconstrained type %pT", tree_ident(t), type);
1017
         sem_unconstrained_decl_hint(d, type);
1✔
1018
         diag_emit(d);
1✔
1019
         return false;
1✔
1020
      }
1021
   }
1022
   else if (!sem_check_incomplete(t, type))
6,697✔
1023
      return false;
1024

1025
   if (!sem_no_access_file_or_protected(t, type, "signals"))
6,720✔
1026
      return false;
1027

1028
   if (is_guarded_signal(t) && !type_is_resolved(type))
6,713✔
1029
      sem_error(t, "guarded signal must have resolved subtype");
1✔
1030

1031
   if (tree_has_value(t)) {
6,712✔
1032
      tree_t value = tree_value(t);
2,010✔
1033
      if (!sem_check(value, tab))
2,010✔
1034
         return false;
1035

1036
      if (!sem_check_type(value, type, tab))
2,007✔
UNCOV
1037
         sem_error(value, "type of initial value %pT does not match type "
×
1038
                   "of declaration %pT", tree_type(value), type);
1039

1040
      if (is_unconstrained)  // Infer constraints from intial value
2,007✔
1041
         tree_set_type(t, merge_constraints(type, tree_type(value)));
23✔
1042
   }
1043

1044
   return true;
1045
}
1046

1047
static bool sem_check_var_decl(tree_t t, nametab_t *tab)
12,671✔
1048
{
1049
   type_t type = tree_type(t);
12,671✔
1050

1051
   if (!sem_check_subtype(t, type, tab))
12,671✔
1052
      return false;
1053
   else if (type_is_none(type))
12,668✔
1054
      return false;
1055

1056
   const bool is_unconstrained = type_is_unconstrained(type);
12,655✔
1057

1058
   if (is_unconstrained) {
12,655✔
1059
      if (standard() < STD_19) {
34✔
1060
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
6✔
1061
         diag_printf(d, "declaration of variable %pI cannot have unconstrained "
6✔
1062
                     "type %pT", tree_ident(t), type);
1063
         sem_unconstrained_decl_hint(d, type);
6✔
1064
         diag_emit(d);
6✔
1065
         return false;
6✔
1066
      }
1067
      else if (!tree_has_value(t)) {
28✔
1068
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
2✔
1069
         diag_printf(d, "declaration of variable %pI without an initial value "
2✔
1070
                     "cannot have unconstrained type %pT", tree_ident(t), type);
1071
         sem_unconstrained_decl_hint(d, type);
2✔
1072
         diag_emit(d);
2✔
1073
         return false;
2✔
1074
      }
1075
   }
1076
   else if (!sem_check_incomplete(t, type))
12,621✔
1077
      return false;
1078

1079
   if (tree_has_value(t)) {
12,646✔
1080
      if (type_kind(type) == T_PROTECTED)
2,325✔
1081
         sem_error(t, "variable %s with protected type may not have an "
1✔
1082
                   "initial value", istr(tree_ident(t)));
1083

1084
      tree_t value = tree_value(t);
2,324✔
1085
      if (!sem_check(value, tab))
2,324✔
1086
         return false;
1087

1088
      if (!sem_check_type(value, type, tab))
2,309✔
1089
         sem_error(value, "type of initial value %pT does not match type "
1✔
1090
                   "of declaration %pT", tree_type(value), type);
1091

1092
      if (is_unconstrained)  // Infer constraints from intial value
2,308✔
1093
         tree_set_type(t, merge_constraints(type, tree_type(value)));
26✔
1094
   }
1095

1096
   // From VHDL-2000 onwards shared variables must be protected types
1097
   if (standard() >= STD_00) {
12,629✔
1098
      if ((tree_flags(t) & TREE_F_SHARED) && !type_is_protected(type)) {
7,630✔
1099
         diag_t *d = pedantic_diag(tree_loc(t));
7✔
1100
         if (d != NULL) {
7✔
1101
            diag_printf(d, "shared variable %pI must have protected type",
7✔
1102
                        tree_ident(t));
1103
            diag_emit(d);
7✔
1104
         }
1105
      }
1106
   }
1107

1108
   if (type_is_protected(type)) {
12,629✔
1109
      ident_t typeid = ident_rfrom(type_ident(type), '.');
202✔
1110
      tree_t pt = get_local_decl(tab, NULL, typeid, 0);
202✔
1111
      if (pt != NULL && tree_kind(pt) == T_PROT_DECL) {
202✔
1112
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
1✔
1113
         diag_printf(d, "cannot declare instance of protected type %pT "
1✔
1114
                     "before its body has been elaborated", type);
1115
         diag_hint(d, tree_loc(pt), "%pT declared here", type);
1✔
1116
         diag_lrm(d, STD_08, "14.4.2");
1✔
1117
         diag_emit(d);
1✔
1118
         return false;
1✔
1119
      }
1120

1121
      // The 2019 standard needs access to the instance and path name at
1122
      // the point of declaration
1123
      if (standard() >= STD_19)
201✔
1124
         tree_set_global_flags(t, TREE_GF_INSTANCE_NAME | TREE_GF_PATH_NAME);
53✔
1125
   }
1126

1127
   return true;
1128
}
1129

1130
static bool sem_check_param_decl(tree_t t, nametab_t *tab)
34,592✔
1131
{
1132
   type_t type = tree_type(t);
34,592✔
1133

1134
   if (!sem_check_subtype(t, type, tab))
34,592✔
1135
      return false;
1136
   else if (!sem_check_incomplete(t, type))
34,592✔
1137
      return false;
1138

1139
   // See LRM 93 section 3.3 for restrictions
1140

1141
   const type_kind_t kind = type_base_kind(type);
34,591✔
1142
   const class_t class = tree_class(t);
34,591✔
1143
   const port_mode_t mode = tree_subkind(t);
34,591✔
1144

1145
   switch (mode) {
34,591✔
1146
   case PORT_BUFFER:
1✔
1147
      sem_error(t, "subprogram formal parameters cannot have mode BUFFER");
1✔
1148
      break;
1149
   case PORT_LINKAGE:
1✔
1150
      sem_error(t, "subprogram formal parameters cannot have mode LINKAGE");
1✔
1151
      break;
1152
   default:
1153
      break;
34,589✔
1154
   }
1155

1156
   if (kind == T_FILE && class != C_FILE)
34,589✔
1157
      sem_error(t, "formal parameter %s with file type must have class FILE",
1✔
1158
                istr(tree_ident(t)));
1159

1160
   if (kind != T_FILE && class == C_FILE)
34,588✔
1161
      sem_error(t, "formal parameter %s with class FILE must have file type",
1✔
1162
                istr(tree_ident(t)));
1163

1164
   if ((kind == T_ACCESS || kind == T_PROTECTED) && class != C_VARIABLE)
34,587✔
1165
      sem_error(t, "formal parameter %s with %s type must have class VARIABLE",
6✔
1166
                istr(tree_ident(t)),
1167
                kind == T_ACCESS ? "access" : "protected");
1168

1169
   if (sem_has_access(type) && class != C_VARIABLE)
34,582✔
1170
      sem_error(t, "formal parameter %s with type containing an access type "
4✔
1171
                "must have class VARIABLE", istr(tree_ident(t)));
1172

1173
   if (class == C_CONSTANT && mode != PORT_IN)
34,578✔
1174
      sem_error(t, "parameter of class CONSTANT must have mode IN");
2✔
1175

1176
   // LRM 08 section 4.2.2.3
1177
   if (class == C_SIGNAL && tree_flags(t) & TREE_F_BUS)
34,576✔
1178
      sem_error(t, "formal signal parameter declaration may "
1✔
1179
                "not include the reserved word BUS");
1180

1181
   if (class == C_PACKAGE || class == C_TYPE)
34,575✔
1182
      sem_error(t, "parameter interface list cannot contain %s "
2✔
1183
                "interface declaration", class_str(class));
1184

1185
   if (mode == PORT_RECORD_VIEW || mode == PORT_ARRAY_VIEW) {
34,573✔
1186
      tree_t name = tree_value(t);
21✔
1187
      type_t view_type = tree_type(name);
21✔
1188

1189
      if (type_is_none(view_type))
21✔
1190
         return false;
1191

1192
      if (type_kind(view_type) != T_VIEW)
21✔
1193
         sem_error(name, "name in mode view indication of parameter %s does "
1✔
1194
                   "not denote a mode view", istr(tree_ident(t)));
1195

1196
      type_t elem_type = type;
20✔
1197
      if (mode == PORT_ARRAY_VIEW) {
20✔
1198
         if (!type_is_array(type))
1✔
1199
            sem_error(t, "parameter %s with array mode view indication has "
1✔
1200
                      "non-array type %s", istr(tree_ident(t)), type_pp(type));
1201

UNCOV
1202
         elem_type = type_elem(type);
×
1203
      }
1204

1205
      if (!type_eq(elem_type, type_designated(view_type)))
19✔
1206
         sem_error(t, "subtype %s is not compatible with mode "
1✔
1207
                   "view %s", type_pp(elem_type), type_pp(view_type));
1208
   }
1209
   else if (tree_has_value(t)) {
34,552✔
1210
      tree_t value = tree_value(t);
5,264✔
1211
      if (!sem_check(value, tab))
5,264✔
1212
         return false;
1213

1214
      if (!sem_check_type(value, type, tab))
5,262✔
1215
         sem_error(value, "type of default value %s does not match type "
2✔
1216
                   "of declaration %s", type_pp(tree_type(value)),
1217
                   type_pp(type));
1218

1219
      switch (class) {
5,260✔
1220
      case C_SIGNAL:
1✔
1221
         sem_error(t, "parameter of class SIGNAL cannot have a "
1✔
1222
                   "default value");
1223
         break;
1224

1225
      case C_VARIABLE:
6✔
1226
         if (mode == PORT_OUT || mode == PORT_INOUT)
6✔
1227
            sem_error(t, "parameter of class VARIABLE with mode OUT or "
2✔
1228
                      "INOUT cannot have a default value");
1229
         break;
1230

1231
      default:
1232
         break;
1233
      }
1234

1235
      if (!sem_globally_static(value)) {
5,257✔
1236
         diag_t *d = pedantic_diag(tree_loc(value));
8✔
1237
         if (d != NULL) {
8✔
1238
            diag_printf(d, "default value must be a static expression");
5✔
1239
            diag_emit(d);
5✔
1240
         }
1241
      }
1242

1243
      if (kind == T_PROTECTED)
5,257✔
1244
         sem_error(t, "parameter with protected type cannot have "
1✔
1245
                   "a default value");
1246
   }
1247

1248
   return true;
1249
}
1250

1251
static bool sem_check_port_decl(tree_t t, nametab_t *tab)
3,988✔
1252
{
1253
   type_t type = tree_type(t);
3,988✔
1254

1255
   if (type_is_none(type))
3,988✔
1256
      return false;
1257
   else if (!sem_check_subtype(t, type, tab))
3,983✔
1258
      return false;
1259
   else if (!sem_check_incomplete(t, type))
3,982✔
1260
      return false;
1261

1262
   const class_t class = tree_class(t);
3,981✔
1263
   const port_mode_t mode = tree_subkind(t);
3,981✔
1264

1265
   if (class == C_VARIABLE) {
3,981✔
1266
      if (standard() < STD_19) {
7✔
1267
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
1✔
1268
         diag_printf(d, "ports may not have variable class in VHDL-%s",
1✔
1269
                     standard_text(standard()));
1270
         diag_hint(d, NULL, "pass $bold$--std=2019$$ to enable this "
1✔
1271
                   "feature");
1272
         diag_emit(d);
1✔
1273
         return false;
1✔
1274
      }
1275

1276
      if (mode != PORT_INOUT)
6✔
1277
         sem_error(t, "formal variable port %s must have mode INOUT",
1✔
1278
                   istr(tree_ident(t)));
1279

1280
      if (!type_is_protected(type))
5✔
1281
         sem_error(t, "formal variable port %s must have protected type",
1✔
1282
                   istr(tree_ident(t)));
1283
   }
1284
   else if (class != C_SIGNAL)
3,974✔
1285
      sem_error(t, "invalid object class %s for port %s",
2✔
1286
                class_str(class), istr(tree_ident(t)));
1287

1288
   if (type_is_access(type))
3,976✔
1289
      sem_error(t, "port %s cannot be declared with access type %s",
1✔
1290
                istr(tree_ident(t)), type_pp(type));
1291

1292
   if (sem_has_access(type))
3,975✔
1293
      sem_error(t, "port %s cannot be declared with type %s which has a "
2✔
1294
                "subelement of access type", istr(tree_ident(t)),
1295
                type_pp(type));
1296

1297
   if (class != C_VARIABLE && type_is_protected(type)) {
3,973✔
1298
      diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
1✔
1299
      diag_printf(d, "port %s with class %s cannot be declared with "
1✔
1300
                  "protected type %s", istr(tree_ident(t)),
1301
                  class_str(class), type_pp(type));
1302
      diag_hint(d, NULL, "ports with variable class can be of protected "
1✔
1303
                "type in VHDL-2019");
1304
      diag_hint(d, NULL, "pass $bold$--std=2019$$ to enable this "
1✔
1305
                "feature");
1306
      diag_emit(d);
1✔
1307
      return false;
1✔
1308
   }
1309

1310
   if (type_is_file(type))
3,972✔
1311
      sem_error(t, "port %s cannot be declared with file type %s",
1✔
1312
                istr(tree_ident(t)), type_pp(type));
1313

1314
   if (mode == PORT_RECORD_VIEW || mode == PORT_ARRAY_VIEW) {
3,971✔
1315
      tree_t name = tree_value(t);
83✔
1316
      type_t view_type = tree_type(name);
83✔
1317

1318
      if (type_is_none(view_type))
83✔
1319
         return false;
1320

1321
      if (type_kind(view_type) != T_VIEW)
83✔
UNCOV
1322
         sem_error(name, "name in mode view indication of port %s does not "
×
1323
                   "denote a mode view", istr(tree_ident(t)));
1324

1325
      type_t elem_type = type;
83✔
1326
      if (mode == PORT_ARRAY_VIEW) {
83✔
1327
         if (!type_is_array(type))
19✔
1328
            sem_error(t, "port %s with array mode view indication has "
1✔
1329
                      "non-array type %s", istr(tree_ident(t)), type_pp(type));
1330

1331
         elem_type = type_elem(type);
18✔
1332
      }
1333

1334
      if (!type_eq(elem_type, type_designated(view_type)))
82✔
1335
         sem_error(t, "subtype %s is not compatible with mode "
2✔
1336
                   "view %s", type_pp(elem_type), type_pp(view_type));
1337
   }
1338
   else if (tree_has_value(t)) {
3,888✔
1339
      tree_t value = tree_value(t);
338✔
1340
      if (!sem_check(value, tab))
338✔
1341
         return false;
1342

1343
      if (mode == PORT_LINKAGE)
338✔
1344
         sem_error(t, "port with mode LINKAGE cannot have a default value");
1✔
1345

1346
      if (!sem_check_type(value, type, tab))
337✔
UNCOV
1347
         sem_error(value, "type of default value %s does not match type "
×
1348
                   "of declaration %s", type_pp(tree_type(value)),
1349
                   type_pp(type));
1350
   }
1351

1352
   return true;
1353
}
1354

1355
static bool sem_check_generic_decl(tree_t t, nametab_t *tab)
2,398✔
1356
{
1357
   const class_t class = tree_class(t);
2,398✔
1358
   switch (class) {
2,398✔
1359
   case C_CONSTANT:
1360
   case C_TYPE:
1361
   case C_FUNCTION:
1362
   case C_PROCEDURE:
1363
      break;
2,319✔
1364

1365
   case C_PACKAGE:
74✔
1366
      {
1367
         tree_t map = tree_value(t);
74✔
1368
         if (!tree_has_ref(map))
74✔
1369
            return false;   // Was earlier error
1370

1371
         assert(tree_kind(map) == T_PACKAGE_MAP);
71✔
1372

1373
         tree_t pack = tree_ref(map);
71✔
1374
         assert(is_uninstantiated_package(pack));
71✔
1375

1376
         switch (tree_subkind(map)) {
71✔
1377
         case PACKAGE_MAP_DEFAULT:
6✔
1378
            {
1379
               // Check each generic in the uninstantiated package has a
1380
               // default value
1381
               const int ngenerics = tree_generics(pack);
6✔
1382
               for (int i = 0; i < ngenerics; i++) {
18✔
1383
                  tree_t g = tree_generic(pack, i);
13✔
1384
                  if (!tree_has_value(g)) {
13✔
1385
                     diag_t *d = diag_new(DIAG_ERROR, tree_loc(map));
1✔
1386
                     diag_printf(d, "generic %s in package %s does not have a "
1✔
1387
                                 "default value", istr(tree_ident(g)),
1388
                                 istr(tree_ident(pack)));
1389
                     diag_hint(d, tree_loc(g), "%s declared here",
1✔
1390
                               istr(tree_ident(g)));
1391
                     diag_lrm(d, STD_08, "6.5.5");
1✔
1392

1393
                     diag_emit(d);
1✔
1394
                     return false;
1✔
1395
                  }
1396
               }
1397
            }
1398
            break;
1399

1400
         case PACKAGE_MAP_MATCHING:
9✔
1401
            sem_check_generic_map(map, pack, tab);
9✔
1402
            break;
9✔
1403

1404
         case PACKAGE_MAP_BOX:
1405
            break;
1406
         }
1407

1408
         return true;
1409
      }
1410
   default:
5✔
1411
      sem_error(t, "invalid object class %s for generic %s",
5✔
1412
                class_str(tree_class(t)), istr(tree_ident(t)));
1413
   }
1414

1415
   type_t type = tree_type(t);
2,319✔
1416

1417
   if (!sem_check_subtype(t, type, tab))
2,319✔
1418
      return false;
1419
   else if (type_is_none(type))
2,319✔
1420
      return false;
1421
   else if (!sem_check_incomplete(t, type))
2,318✔
1422
      return false;
1423

1424
   if (class != C_TYPE) {
2,317✔
1425
      if (type_is_access(type))
2,068✔
1426
         sem_error(t, "generic %s may not have access type",
1✔
1427
                   istr(tree_ident(t)));
1428
      else if (sem_has_access(type))
2,067✔
1429
         sem_error(t, "generic %s may not have a type with a subelement of "
2✔
1430
                   "access type", istr(tree_ident(t)));
1431
      else if (type_is_protected(type))
2,065✔
1432
         sem_error(t, "generic %s may not have protected type",
1✔
1433
                   istr(tree_ident(t)));
1434
      else if (type_is_file(type))
2,064✔
1435
         sem_error(t, "generic %s may not have file type", istr(tree_ident(t)));
1✔
1436
   }
1437

1438
   if (class == C_CONSTANT && tree_subkind(t) != PORT_IN)
2,312✔
1439
      sem_error(t, "generic %s with class CONSTANT must have mode IN",
1✔
1440
                istr(tree_ident(t)));
1441

1442
   if (tree_has_value(t)) {
2,311✔
1443
      tree_t value = tree_value(t);
1,199✔
1444
      if (!sem_check(value, tab))
1,199✔
1445
         return false;
1446

1447
      if (!sem_check_type(value, type, tab))
1,195✔
UNCOV
1448
         sem_error(value, "type of default value %s does not match type "
×
1449
                   "of declaration %s", type_pp(tree_type(value)),
1450
                   type_pp(type));
1451
   }
1452

1453
   if (tree_flags(t) & TREE_F_LOCALLY_STATIC) {
2,307✔
1454
      // For a generic declaration in a pacakage or subprogram to be
1455
      // locally static it must also have a locally static subtype
1456
      if (!sem_static_subtype(type, sem_locally_static))
310✔
1457
         tree_clear_flag(t, TREE_F_LOCALLY_STATIC);
10✔
1458
   }
1459

1460
   return true;
1461
}
1462

1463
static bool sem_check_field_decl(tree_t t)
3,988✔
1464
{
1465
   type_t type = tree_type(t);
3,988✔
1466

1467
   if (!sem_check_incomplete(t, type))
3,988✔
1468
      return false;
1✔
1469

1470
   return true;
1471
}
1472

1473
static bool sem_check_unit_decl(tree_t t)
187✔
1474
{
1475
   return true;
187✔
1476
}
1477

1478
static bool sem_check_alias(tree_t t, nametab_t *tab)
1,616✔
1479
{
1480
   // Rules for aliases are given in LRM 93 section 4.3.3
1481

1482
   tree_t value = tree_value(t);
1,616✔
1483
   type_t type = get_type_or_null(t);
1,616✔
1484

1485
   const tree_kind_t value_kind = tree_kind(value);
1,616✔
1486

1487
   if (type != NULL && type_is_subprogram(type)) {
1,616✔
1488
      // Alias of subprogram or enumeration literal
1489
      // Rules for matching signatures are in LRM 93 section 2.3.2
1490
      return true;
1491
   }
1492
   else if (tree_flags(t) & TREE_F_NONOBJECT_ALIAS) {
1,088✔
1493
      // TODO: cannot be label
1494
      return true;
1495
   }
1496
   else if (value_kind == T_REF && tree_has_ref(value)) {
1,085✔
1497
      tree_t decl = tree_ref(value);
908✔
1498
      if (aliased_type_decl(decl) != NULL)
908✔
1499
         return true;   // Alias of type
1500
      else if (tree_kind(decl) == T_VIEW_DECL)
839✔
1501
         return true;   // Alias of view declaration
1502
   }
1503
   else if (value_kind == T_ATTR_REF && is_type_attribute(tree_subkind(value)))
177✔
1504
      return true;   // Alias of type
1505

1506
   // Alias of object
1507
   if (!sem_check(value, tab))
1,007✔
1508
      return false;
1509

1510
   if (value_kind == T_ATTR_REF && tree_subkind(value) == ATTR_CONVERSE) {
997✔
1511
      // Special case handling for
1512
      //   https://gitlab.com/IEEE-P1076/VHDL-Issues/-/issues/293
1513
      return true;
1514
   }
1515
   else if (!sem_static_name(value, sem_globally_static)) {
962✔
1516
      diag_t *d = diag_new(DIAG_ERROR, tree_loc(value));
5✔
1517
      diag_printf(d, "aliased name is not static");
5✔
1518
      diag_lrm(d, STD_93, "6.1");
5✔
1519
      diag_emit(d);
5✔
1520
      return false;
5✔
1521
   }
1522

1523
   if (type != NULL) {
957✔
1524
      // Alias declaration had optional subtype indication
1525

1526
      if (!sem_check_subtype(t, type, tab))
849✔
1527
         return false;
1528

1529
      if (value_kind == T_EXTERNAL_NAME) {
849✔
1530
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
1✔
1531
         diag_printf(d, "an alias of an external name cannot have a "
1✔
1532
                     "subtype indication");
1533
         diag_lrm(d, STD_08, "6.6.2");
1✔
1534
         diag_emit(d);
1✔
1535
         return false;
1✔
1536
      }
1537

1538
      if (!sem_check_type(value, type, tab))
848✔
1539
         sem_error(t, "type of aliased object %pT does not match expected "
1✔
1540
                   "type %pT", tree_type(value), type);
1541

1542
      if (opt_get_int(OPT_RELAXED) && type_is_unconstrained(type)) {
847✔
1543
         // If the type of the aliased object is unconstrained then
1544
         // use its subtype instead of the subtype declared by the
1545
         // alias.  This is required for some UVVM sources.
1546
         type_t obj_type = tree_type(value);
1✔
1547
         if (!type_is_unconstrained(obj_type))
1✔
1548
            tree_set_type(t, obj_type);
1✔
1549
      }
1550
   }
1551
   else
1552
      type = tree_type(value);
108✔
1553

1554
   if (standard() < STD_08 && type_is_array(type) && dimension_of(type) > 1) {
955✔
1555
      diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
1✔
1556
      diag_printf(d, "object alias may not have multidimensional array type "
1✔
1557
                  "in VHDL-%s", standard_text(standard()));
1558
      diag_lrm(d, STD_93, "4.3.3.1");
1✔
1559
      diag_emit(d);
1✔
1560
      return false;
1✔
1561
   }
1562

1563
   return true;
1564
}
1565

1566
static void sem_missing_body_cb(tree_t t, tree_t parent, nametab_t *tab)
14,677✔
1567
{
1568
   if (parent == NULL || !opt_get_int(OPT_MISSING_BODY))
14,677✔
1569
      return;
727✔
1570
   else if (have_name_errors(tab))
13,950✔
1571
      return;   // May be missing due to parse errors
1572

1573
   const tree_kind_t kind = tree_kind(parent);
13,942✔
1574
   if (kind == T_PACKAGE || kind == T_PROT_DECL)
13,942✔
1575
      return;   // Should be in body
1576

1577
   type_t type = tree_type(t);
7,192✔
1578
   tree_t body = NULL;
7,192✔
1579
   int nth = 0;
7,192✔
1580
   do {
27,638✔
1581
      body = get_local_decl(tab, NULL, tree_ident(t), nth++);
27,638✔
1582
   } while (body != NULL && !type_eq(tree_type(body), type));
27,638✔
1583

1584
   if (body == NULL || !is_body(body)) {
7,192✔
1585
      diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
23✔
1586
      diag_printf(d, "missing body for ");
23✔
1587
      switch (tree_kind(t)) {
23✔
1588
      case T_PROT_DECL: diag_printf(d, "protected type "); break;
1✔
1589
      case T_PROC_DECL: diag_printf(d, "procedure "); break;
10✔
1590
      case T_FUNC_DECL: diag_printf(d, "function "); break;
12✔
1591
      default: break;
1592
      }
1593

1594
      diag_printf(d, "%s", type_pp(type));
23✔
1595
      diag_suppress(d, type_has_error(type));
23✔
1596

1597
      diag_hint(d, tree_loc(parent), "body not found in %s",
23✔
1598
                istr(tree_ident(parent)));
1599

1600
      diag_hint(d, tree_loc(t), "%s declared here", type_pp(type));
23✔
1601

1602
      diag_emit(d);
23✔
1603
   }
1604
}
1605

1606
static bool sem_check_func_ports(tree_t t, nametab_t *tab)
13,980✔
1607
{
1608
   const vhdl_standard_t std = standard();
13,980✔
1609

1610
   ident_t id = tree_ident(t);
13,980✔
1611
   if (is_operator_symbol(id)) {
13,980✔
1612
      const int nports = tree_ports(t);
5,462✔
1613
      const well_known_t wk = is_well_known(id);
5,462✔
1614
      if (wk >= W_OP_AND && wk <= W_OP_XNOR) {
5,462✔
1615
         if (std >= STD_08 && (nports < 1 || nports > 2))
902✔
UNCOV
1616
            sem_error(t, "logical operator must have either one or two "
×
1617
                      "operands");
1618
         else if (std < STD_08 && nports != 2)
902✔
1619
            sem_error(t, "logical operator must have two operands");
1✔
1620
      }
1621
      else if (wk >= W_OP_ABS && wk <= W_OP_CCONV && nports != 1)
4,560✔
1622
         sem_error(t, "unary operator must have one operand");
1✔
1623
      else if ((wk >= W_OP_EQUAL && wk <= W_OP_EXPONENT && nports != 2)
4,559✔
1624
               || (wk >= W_OP_MATCH_EQUAL && wk <= W_OP_MATCH_GREATER_EQUAL
4,559✔
1625
                   && nports != 2)
1626
               || (wk >= W_OP_ADD && wk <= W_OP_MINUS
4,558✔
1627
                   && (nports < 1 || nports > 2)))
1,068✔
1628
         sem_error(t, "binary operator must have two operands");
1✔
1629
   }
1630

1631
   const bool pure = !(tree_flags(t) & TREE_F_IMPURE);
13,977✔
1632

1633
   if (!pure && std >= STD_19)
13,977✔
1634
      return true;   // LCS2016_002 relaxed rules for impure functions
1635

1636
   const int nports = tree_ports(t);
13,549✔
1637
   for (int i = 0; i < nports; i++) {
35,986✔
1638
      tree_t p = tree_port(t, i);
22,437✔
1639
      if (tree_subkind(p) != PORT_IN) {
22,437✔
1640
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(p));
4✔
1641
         diag_printf(d, "%sfunction parameters must have mode IN",
5✔
1642
                     std < STD_19 ? "" : "pure ");
1643
         diag_hint(d, tree_loc(p), "parameter %s has mode %s",
4✔
1644
                   istr(tree_ident(p)), port_mode_str(tree_subkind(p)));
4✔
1645
         diag_lrm(d, STD_08, "4.2.2.1");
4✔
1646
         diag_emit(d);
4✔
1647
      }
1648
      else if (tree_class(p) == C_VARIABLE) {
22,433✔
1649
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(p));
3✔
1650
         diag_printf(d, "class of %sfunction parameters must be CONSTANT, "
4✔
1651
                     "SIGNAL, or FILE", std < STD_19 ? "" : "pure ");
1652
         diag_hint(d, tree_loc(p), "parameter %s has class %s",
3✔
1653
                   istr(tree_ident(p)), class_str(tree_class(p)));
1654
         diag_lrm(d, STD_08, "4.2.2.1");
3✔
1655
         diag_emit(d);
3✔
1656
      }
1657
   }
1658

1659
   return true;
1660
}
1661

1662
static bool sem_check_protected_method(tree_t t, nametab_t *tab)
467✔
1663
{
1664
   if (standard() >= STD_19)
467✔
1665
      return true;   // Relaxed in LCS2016_04
1666

1667
   const int nports = tree_ports(t);
287✔
1668
   for (int i = 0; i < nports; i++) {
475✔
1669
      tree_t p = tree_port(t, i);
188✔
1670
      type_t type = tree_type(p);
188✔
1671

1672
      if (sem_has_access(type)) {
188✔
1673
         diag_t *d = pedantic_diag(tree_loc(p));
2✔
1674
         if (d != NULL) {
2✔
1675
            diag_printf(d, "parameters of protected type methods cannot be of "
2✔
1676
                        "an access type or a composite type containing an "
1677
                        "access type");
1678
            if (type_is_access(type))
2✔
1679
               diag_hint(d, tree_loc(p), "type of %s is %s which is an "
1✔
1680
                         "access type", istr(tree_ident(p)), type_pp(type));
1681
            else
1682
               diag_hint(d, tree_loc(p), "type of %s is %s which has an "
1✔
1683
                         "access type as a subelement",
1684
                         istr(tree_ident(p)), type_pp(type));
1685
            diag_lrm(d, STD_08, "5.6.2");
2✔
1686
            diag_emit(d);
2✔
1687
         }
1688
      }
1689
      else if (type_is_file(type)) {
186✔
1690
         diag_t *d = pedantic_diag(tree_loc(p));
1✔
1691
         if (d != NULL) {
1✔
1692
            diag_printf(d, "parameters of protected type methods cannot be of "
1✔
1693
                        "a file type");
1694
            diag_hint(d, tree_loc(p), "type of %s is %s which is a file type",
1✔
1695
                      istr(tree_ident(p)), type_pp(type));
1696
            diag_lrm(d, STD_08, "5.6.2");
1✔
1697
            diag_emit(d);
1✔
1698
         }
1699
      }
1700
   }
1701

1702
   if (tree_kind(t) == T_FUNC_DECL) {
287✔
1703
      type_t result = type_result(tree_type(t));
121✔
1704
      if (sem_has_access(result) || type_is_file(result)) {
121✔
1705
         diag_t *d = pedantic_diag(tree_loc(t));
1✔
1706
         if (d != NULL) {
1✔
1707
            diag_printf(d, "return type of a protected type method cannot be "
1✔
1708
                        "of a file type, access type, or a composite type with "
1709
                        "a subelement that is an access type");
1710
            diag_lrm(d, STD_08, "5.6.2");
1✔
1711
            diag_emit(d);
1✔
1712
         }
1713
      }
1714
   }
1715

1716
   return true;
1717
}
1718

1719
static bool sem_check_func_result(tree_t t)
13,977✔
1720
{
1721
   type_t result = type_result(tree_type(t));
13,977✔
1722

1723
   if (type_is_protected(result))
13,977✔
1724
      sem_error(t, "function result subtype may not denote a protected type");
1✔
1725
   else if (type_is_file(result))
13,976✔
1726
      sem_error(t, "function result subtype may not denote a file type");
1✔
1727
   else if (!sem_check_incomplete(t, result))
13,975✔
1728
      return false;
1✔
1729

1730
   return true;
1731
}
1732

1733
static bool sem_check_func_decl(tree_t t, nametab_t *tab)
6,240✔
1734
{
1735
   const tree_flags_t flags = tree_flags(t);
6,240✔
1736
   if (flags & TREE_F_PREDEFINED)
6,240✔
1737
      return true;
1738

1739
   if (!sem_check_func_ports(t, tab))
6,240✔
1740
      return false;
1741

1742
   if (!sem_check_func_result(t))
6,237✔
1743
      return false;
1744

1745
   if ((flags & TREE_F_PROTECTED) && !sem_check_protected_method(t, tab))
6,235✔
1746
      return false;
1747

1748
   defer_check(tab, sem_missing_body_cb, t);
6,235✔
1749
   return true;
6,235✔
1750
}
1751

1752
static bool sem_compare_interfaces(tree_t decl, tree_t body, int nth,
14,220✔
1753
                                   tree_t (*getfn)(tree_t, unsigned),
1754
                                   const char *what)
1755
{
1756
   tree_t dport = (*getfn)(decl, nth);
14,220✔
1757
   tree_t bport = (*getfn)(body, nth);
14,220✔
1758

1759
   tree_flags_t dflags = tree_flags(dport);
14,220✔
1760
   tree_flags_t bflags = tree_flags(bport);
14,220✔
1761

1762
   ident_t dname = tree_ident(dport);
14,220✔
1763
   ident_t bname = tree_ident(bport);
14,220✔
1764

1765
   if (dname != bname) {
14,220✔
1766
      diag_t *d = diag_new(DIAG_ERROR, tree_loc(bport));
2✔
1767
      diag_printf(d, "%s name %s in subprogram %s body does not match "
2✔
1768
                  "name %s in declaration", what,
1769
                  istr(bname), istr(tree_ident(body)), istr(dname));
1770
      diag_hint(d, tree_loc(dport), "%s %s has name %s in specification",
2✔
1771
                ordinal_str(nth + 1), what, istr(dname));
1772
      diag_hint(d, tree_loc(bport), "%s %s has name %s in body",
2✔
1773
                ordinal_str(nth + 1), what, istr(bname));
1774
      diag_emit(d);
2✔
1775
      return false;
2✔
1776
   }
1777

1778
   const bool dcont = !!(tree_flags(dport) & TREE_F_CONTINUATION);
14,218✔
1779
   const bool bcont = !!(tree_flags(bport) & TREE_F_CONTINUATION);
14,218✔
1780

1781
   if (dcont != bcont) {
14,218✔
1782
      diag_t *d = pedantic_diag(tree_loc(bport));
1✔
1783
      if (d != NULL) {
1✔
1784
         diag_printf(d, "declaration of %s %s in subprogram body does not "
1✔
1785
                     "match specification", what, istr(bname));
1786

1787
         int bseq = 1;
1✔
1788
         for (tree_t it = bport; tree_flags(it) & TREE_F_CONTINUATION;
1✔
UNCOV
1789
              it = (*getfn)(body, nth - bseq++));
×
1790

1791
         diag_hint(d, tree_loc(bport), "%s appears %s in identifier list",
1✔
1792
                   istr(bname), ordinal_str(bseq));
1793

1794
         int dseq = 1;
1✔
1795
         for (tree_t it = dport; tree_flags(it) & TREE_F_CONTINUATION;
2✔
1796
              it = (*getfn)(decl, nth - dseq++));
1✔
1797

1798
         diag_hint(d, tree_loc(dport), "%s appears %s in identifier list",
1✔
1799
                   istr(dname), ordinal_str(dseq));
1800

1801
         diag_lrm(d, STD_08, "4.10");
1✔
1802
         diag_emit(d);
1✔
1803
         return false;
1✔
1804
      }
1805
   }
1806
   else if (dcont && bcont)
14,217✔
1807
      return true;   // Already checked first declaration in list
1808

1809
   type_t dtype = tree_type(dport);
13,032✔
1810
   type_t btype = tree_type(bport);
13,032✔
1811

1812
   // Do not use type_eq here as subtype must exactly match
1813
   if (!type_strict_eq(btype, dtype)) {
13,032✔
1814
     diag_t *d = diag_new(DIAG_ERROR, tree_loc(bport));
3✔
1815
     diag_printf(d, "subtype of %s %s does not match type %s in "
3✔
1816
                 "specification", what, istr(bname), type_pp(dtype));
1817
     diag_hint(d, tree_loc(dport), "%s %s declared with type %s",
3✔
1818
               what, istr(dname), type_pp(dtype));
1819
     diag_hint(d, tree_loc(bport), "%s %s declared with type %s ",
3✔
1820
               what, istr(bname), type_pp(btype));
1821
     diag_emit(d);
3✔
1822
     return false;
3✔
1823
   }
1824

1825
   const port_mode_t dmode = tree_subkind(dport);
13,029✔
1826
   const port_mode_t bmode = tree_subkind(bport);
13,029✔
1827

1828
   if (dmode != bmode) {
13,029✔
1829
     diag_t *d = diag_new(DIAG_ERROR, tree_loc(bport));
2✔
1830
     diag_printf(d, "%s %s of subprogram body %s with mode %s does not "
2✔
1831
                 "match mode %s in specification", what, istr(dname),
1832
                 istr(tree_ident(body)), port_mode_str(bmode),
1833
                 port_mode_str(dmode));
1834
     diag_hint(d, tree_loc(dport), "%s %s declared with mode %s",
2✔
1835
               what, istr(dname), port_mode_str(dmode));
1836
     diag_hint(d, tree_loc(bport), "%s %s declared with mode %s",
2✔
1837
               what, istr(bname), port_mode_str(bmode));
1838
     diag_emit(d);
2✔
1839
     return false;
2✔
1840
   }
1841

1842
   const bool bmode_explicit = !!(bflags & TREE_F_EXPLICIT_MODE);
13,027✔
1843
   const bool dmode_explicit = !!(dflags & TREE_F_EXPLICIT_MODE);
13,027✔
1844

1845
   if (bmode_explicit != dmode_explicit) {
13,027✔
1846
      diag_t *d = pedantic_diag(tree_loc(bport));
3✔
1847
      if (d != NULL) {
3✔
1848
         const char *dmode_str =
6✔
1849
            dmode_explicit ? port_mode_str(dmode) : "default";
3✔
1850
         const char *bmode_str =
6✔
1851
            bmode_explicit ? port_mode_str(bmode) : "default";
3✔
1852

1853
         diag_printf(d, "mode indication of subprogram %s %s %s was %s in "
3✔
1854
                     "specification but %s in body", istr(tree_ident(body)),
1855
                     what, istr(dname), dmode_str, bmode_str);
1856
         diag_hint(d, tree_loc(dport), "%s %s declared with %s mode in "
3✔
1857
                   "specification", what, istr(dname), dmode_str);
1858
         diag_hint(d, tree_loc(bport), "%s %s declared with %s mode in body",
3✔
1859
                   what, istr(bname), bmode_str);
1860
         diag_lrm(d, STD_08, "4.10");
3✔
1861
         diag_emit(d);
3✔
1862
         return false;
3✔
1863
      }
1864
   }
1865

1866
   const class_t dclass = tree_class(dport);
13,024✔
1867
   const class_t bclass = tree_class(bport);
13,024✔
1868

1869
   if (dclass != bclass) {
13,024✔
1870
     diag_t *d = diag_new(DIAG_ERROR, tree_loc(bport));
3✔
1871
     diag_printf(d, "class %s of subprogram body %s %s %s does not "
3✔
1872
                 "match class %s in specification", class_str(bclass),
1873
                 istr(tree_ident(body)), what, istr(dname), class_str(dclass));
1874
     diag_hint(d, tree_loc(dport), "%s %s declared with class %s in "
3✔
1875
               "subprogram specification", what, istr(dname),
1876
               class_str(dclass));
1877
     diag_hint(d, tree_loc(bport), "%s %s declared with class %s in "
3✔
1878
               "subprogram body", what, istr(bname), class_str(bclass));
1879
     diag_emit(d);
3✔
1880
     return false;
3✔
1881
   }
1882

1883
   const bool bclass_explicit = !!(bflags & TREE_F_EXPLICIT_CLASS);
13,021✔
1884
   const bool dclass_explicit = !!(dflags & TREE_F_EXPLICIT_CLASS);
13,021✔
1885

1886
   if (bclass_explicit != dclass_explicit) {
13,021✔
1887
      diag_t *d = pedantic_diag(tree_loc(bport));
3✔
1888
      if (d != NULL) {
3✔
1889
         const char *dclass_str =
4✔
1890
            dclass_explicit ? class_str(dclass) : "default";
2✔
1891
         const char *bclass_str =
4✔
1892
            bclass_explicit ? class_str(bclass) : "default";
2✔
1893

1894
         diag_printf(d, "class of subprogram %s %s %s was %s in specification "
2✔
1895
                     "but %s in body", istr(tree_ident(body)),
1896
                     what, istr(dname), dclass_str, bclass_str);
1897
         diag_hint(d, tree_loc(dport), "%s %s declared with %s class in "
2✔
1898
                   "specification", what, istr(dname), dclass_str);
1899
         diag_hint(d, tree_loc(bport), "%s %s declared with %s class in body",
2✔
1900
                   what, istr(bname), bclass_str);
1901
         diag_lrm(d, STD_08, "4.10");
2✔
1902
         diag_emit(d);
2✔
1903
         return false;
2✔
1904
      }
1905
   }
1906

1907
   tree_t bdef = tree_has_value(bport) ? tree_value(bport) : NULL;
13,019✔
1908
   tree_t ddef = tree_has_value(dport) ? tree_value(dport) : NULL;
13,019✔
1909

1910
   if (bdef == NULL && ddef == NULL)
13,019✔
1911
      return true;
1912

1913
   const tree_kind_t bkind = bdef ? tree_kind(bdef) : T_LAST_TREE_KIND;
2,392✔
1914
   const tree_kind_t dkind = ddef ? tree_kind(ddef) : T_LAST_TREE_KIND;
2,392✔
1915

1916
   // Work around some mismatches caused by folding
1917
   if (bdef != NULL && ddef != NULL && bkind != dkind)
2,392✔
1918
      return true;
1919

1920
   if (dkind == bkind) {
2,357✔
1921
      // This only covers a few simple cases
1922
      switch (dkind) {
2,356✔
1923
      case T_LITERAL:
288✔
1924
         if (tree_subkind(bdef) == L_PHYSICAL
288✔
1925
             && tree_subkind(ddef) == L_PHYSICAL)
88✔
1926
            return true;   // TODO: bug with folding physical literals
1927
         // Fall-through
1928
      case T_STRING:
1929
         if (same_tree(bdef, ddef))
453✔
1930
            return true;
1931
         break;
1932

1933
      case T_REF:
1,778✔
1934
      case T_FCALL:
1935
         {
1936
            if (!tree_has_ref(bdef) || !tree_has_ref(ddef))
1,778✔
UNCOV
1937
               return true; // Was parse error, ignore it
×
1938

1939
            tree_t bref = tree_ref(bdef);
1,778✔
1940
            tree_t dref = tree_ref(ddef);
1,778✔
1941

1942
            if (bref == dref)
1,778✔
1943
               return true;
1944

1945
            // Work around mismatch introduced by folding
1946
            const tree_kind_t brefkind = tree_kind(bref);
228✔
1947
            if (brefkind == T_CONST_DECL || brefkind == T_GENERIC_DECL)
228✔
1948
               return true;
1949
            else if (bkind == T_FCALL && brefkind == T_FUNC_BODY
2✔
1950
                     && type_eq(tree_type(bref), tree_type(dref)))
1✔
1951
               return true;
1952
         }
1953
         break;
1954

1955
      default:
1956
         return true;
1957
      }
1958
   }
1959

1960
   diag_t *d = diag_new(DIAG_ERROR, tree_loc(bport));
4✔
1961
   diag_printf(d, "default value of %s %s in subprogram body %s does not "
4✔
1962
               "match declaration", what, istr(dname), istr(tree_ident(body)));
1963
   diag_hint(d, tree_loc(dport), "parameter was originally declared here");
4✔
1964
   diag_hint(d, tree_loc(bport), "body has different default value");
4✔
1965
   diag_emit(d);
4✔
1966

1967
   return false;
4✔
1968
}
1969

1970
static bool sem_check_conforming(tree_t decl, tree_t body)
7,008✔
1971
{
1972
   // Conformance rules are in LRM 08 section 4.10
1973
   // Note we don't implement strict lexical conformance here
1974

1975
   bool ok = true;
7,008✔
1976

1977
   const bool dimpure = !!(tree_flags(decl) & TREE_F_IMPURE);
7,008✔
1978
   const bool bimpure = !!(tree_flags(body) & TREE_F_IMPURE);
7,008✔
1979

1980
   if (dimpure != bimpure) {
7,008✔
1981
      diag_t *d = diag_new(DIAG_ERROR, tree_loc(body));
2✔
1982
      diag_printf(d, "function %s declaration was %s but body is %s",
4✔
1983
                  istr(tree_ident(body)), dimpure ? "impure" : "pure",
1984
                  bimpure ? "impure" : "pure");
1985
      diag_hint(d, tree_loc(decl), "declaration was %s",
2✔
1986
                dimpure ? "impure" : "pure");
1987
      diag_hint(d, tree_loc(body), "expecting keyword %s to match declaration",
3✔
1988
                bimpure ? "IMPURE" : "PURE");
1989
      diag_emit(d);
2✔
1990
      ok = false;
2✔
1991
   }
1992

1993
   // This must be true or they would be considered different overloads
1994
   assert(tree_ports(decl) == tree_ports(body));
7,008✔
1995

1996
   const int nports = tree_ports(decl);
7,008✔
1997
   for (int i = 0; i < nports; i++)
21,158✔
1998
      ok &= sem_compare_interfaces(decl, body, i, tree_port, "parameter");
14,150✔
1999

2000
   const int ngenerics = tree_generics(decl);
7,008✔
2001
   if (ngenerics != tree_generics(body)) {
7,008✔
2002
      diag_t *d = diag_new(DIAG_ERROR, tree_loc(body));
1✔
2003
      diag_printf(d, "subprogram %s declaration has %d generic%s but body "
2✔
2004
                  "has %d", istr(tree_ident(body)), ngenerics,
2005
                  ngenerics > 1 ? "s" : "", tree_generics(body));
2006
      diag_hint(d, tree_loc(decl), "declaration with %d generics", ngenerics);
1✔
2007
      diag_hint(d, tree_loc(body), "body has %d generics", tree_generics(body));
1✔
2008
      diag_emit(d);
1✔
2009
      ok = false;
1✔
2010
   }
2011
   else {
2012
      for (int i = 0; i < ngenerics; i++)
7,077✔
2013
         ok &= sem_compare_interfaces(decl, body, i, tree_generic, "generic");
70✔
2014
   }
2015

2016
   type_t dtype = tree_type(decl);
7,008✔
2017
   type_t btype = tree_type(body);
7,008✔
2018

2019
   if (type_has_result(dtype) && type_has_result(btype)) {
7,008✔
2020
      type_t dresult = type_result(dtype);
5,968✔
2021
      type_t bresult = type_result(btype);
5,968✔
2022

2023
      if (!type_strict_eq(dresult, bresult)) {
5,968✔
2024
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(body));
1✔
2025
         diag_printf(d, "return type of function body %s does not match type "
1✔
2026
                     "%s in specification", istr(tree_ident(body)),
2027
                     type_pp(dresult));
2028
         diag_hint(d, tree_loc(decl), "specification has return type %s",
1✔
2029
                   type_pp(dresult));
2030
         diag_hint(d, tree_loc(body), "body has return type %s ",
1✔
2031
                   type_pp(bresult));
2032
         diag_emit(d);
1✔
2033
         ok = false;
1✔
2034
      }
2035
   }
2036

2037
   return ok;
7,008✔
2038
}
2039

2040
static bool sem_check_func_body(tree_t t, nametab_t *tab)
7,740✔
2041
{
2042
   if (!sem_check_func_ports(t, tab))
7,740✔
2043
      return false;
2044

2045
   if (!sem_check_func_result(t))
7,740✔
2046
      return false;
2047

2048
   tree_t fwd = find_forward_decl(tab, t);
7,739✔
2049
   if (fwd != NULL && !sem_check_conforming(fwd, t))
7,739✔
2050
      return false;
17✔
2051

2052
   return true;
2053
}
2054

2055
static bool sem_check_proc_decl(tree_t t, nametab_t *tab)
1,201✔
2056
{
2057
   if (is_operator_symbol(tree_ident(t)))
1,201✔
UNCOV
2058
      sem_error(t, "procedure name must be an identifier");
×
2059

2060
   const tree_flags_t flags = tree_flags(t);
1,201✔
2061
   if (flags & TREE_F_PREDEFINED)
1,201✔
2062
      return true;
2063
   else if ((flags & TREE_F_PROTECTED) && !sem_check_protected_method(t, tab))
1,201✔
2064
      return false;
2065

2066
   defer_check(tab, sem_missing_body_cb, t);
1,201✔
2067
   return true;
1,201✔
2068
}
2069

2070
static bool sem_check_proc_body(tree_t t, nametab_t *tab)
2,380✔
2071
{
2072
   tree_t fwd = find_forward_decl(tab, t);
2,380✔
2073
   if (fwd != NULL && !sem_check_conforming(fwd, t))
2,380✔
2074
      return false;
2075

2076
   if (fwd == NULL && is_operator_symbol(tree_ident(t)))
2,374✔
2077
      sem_error(t, "procedure name must be an identifier");
1✔
2078

2079
   // Cleared by wait statement or pcall
2080
   tree_set_flag(t, TREE_F_NEVER_WAITS);
2,373✔
2081

2082
   return true;
2,373✔
2083
}
2084

2085
static bool sem_check_subprogram_inst(tree_t t, nametab_t *tab)
137✔
2086
{
2087
   if (tree_generics(t) == 0)
137✔
2088
      return false;   // Was a parse error
2089

2090
   if (!sem_check_generic_map(t, t, tab))
130✔
2091
      return false;
2✔
2092

2093
   // Other declarations were checked on the uninstantiated subprogram
2094

2095
   return true;
2096
}
2097

2098
static bool sem_check_sensitivity(tree_t t, nametab_t *tab)
15,378✔
2099
{
2100
   const int ntriggers = tree_triggers(t);
15,378✔
2101
   for (int i = 0; i < ntriggers; i++) {
16,768✔
2102
      tree_t r = tree_trigger(t, i);
1,398✔
2103
      if (tree_kind(r) == T_ALL)
1,398✔
2104
         continue;
57✔
2105
      else if (!sem_check(r, tab) || !sem_check_readable(r))
1,341✔
2106
         return false;
5✔
2107

2108
      if (!sem_static_name(r, sem_globally_static))
1,336✔
2109
         sem_error(r, "name in sensitivity list is not a static signal name");
2✔
2110

2111
      if (class_of(r) != C_SIGNAL) {
1,334✔
2112
         tree_t ref = name_to_ref(r);
1✔
2113
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(r));
1✔
2114
         if (ref != NULL) {
1✔
2115
            tree_t decl = tree_ref(ref);
1✔
2116
            diag_printf(d, "name %s in sensitivity list is not a signal",
1✔
2117
                        istr(tree_ident(decl)));
2118
            diag_hint(d, tree_loc(r), "%s is a %s", istr(tree_ident(decl)),
1✔
2119
                      class_str(class_of(decl)));
2120
         }
2121
         else
UNCOV
2122
            diag_printf(d, "name in sensitivity list is not a signal");
×
2123
         diag_emit(d);
1✔
2124
         return false;
1✔
2125
      }
2126
   }
2127

2128
   return true;
2129
}
2130

2131
static void sem_check_missing_wait(tree_t t, nametab_t *tab)
5,156✔
2132
{
2133
   if (!opt_get_int(OPT_MISSING_WAIT))
5,156✔
2134
      return;
2135

2136
   const int ntriggers = tree_triggers(t);
4,490✔
2137
   if (ntriggers > 0)
4,490✔
2138
      return;
2139

2140
   const tree_flags_t flags = tree_flags(t);
4,085✔
2141
   if (flags & TREE_F_HAS_WAIT)
4,085✔
2142
      return;
2143

2144
   diag_t *d = diag_new(DIAG_WARN, tree_loc(t));
56✔
2145
   diag_printf(d, "potential infinite loop in process");
56✔
2146
   if (!(flags & TREE_F_SYNTHETIC_NAME))
56✔
2147
      diag_printf(d, " %s", istr(tree_ident(t)));
3✔
2148
   diag_printf(d, " with no sensitivity list and no wait statements.");
56✔
2149
   diag_emit(d);
56✔
2150
}
2151

2152
static bool sem_check_process(tree_t t, nametab_t *tab)
5,158✔
2153
{
2154
   if (!sem_check_sensitivity(t, tab))
5,158✔
2155
      return false;
2156

2157
   sem_check_missing_wait(t, tab);
5,156✔
2158

2159
   return true;
5,156✔
2160
}
2161

2162
static bool sem_check_package(tree_t t, nametab_t *tab)
1,531✔
2163
{
2164
   if (!sem_check_context_clause(t, tab))
1,531✔
2165
      return false;
2166

2167
   if (tree_genmaps(t) > 0 && !sem_check_generic_map(t, t, tab))
1,531✔
2168
      return false;
2169

2170
   // Subprogram bodies are not allowed in package specification
2171
   const int ndecls = tree_decls(t);
1,531✔
2172
   for (int i = 0; i < ndecls; i++) {
31,199✔
2173
     tree_t d = tree_decl(t, i);
29,670✔
2174
     tree_kind_t kind = tree_kind(d);
29,670✔
2175
     if ((kind == T_FUNC_BODY) || (kind == T_PROC_BODY))
29,670✔
2176
       sem_error(d, "subprogram body is not allowed in package specification");
29,670✔
2177
   }
2178

2179
   return true;
2180
}
2181

2182
static bool sem_check_pack_inst(tree_t t, nametab_t *tab)
285✔
2183
{
2184
   if (tree_generics(t) == 0)
285✔
2185
      return false;   // Was a parse error
2186

2187
   if (!sem_check_generic_map(t, t, tab))
282✔
2188
      return false;
6✔
2189

2190
   // Other declarations were checked on the uninstantiated package
2191

2192
   return true;
2193
}
2194

2195
static bool sem_check_missing_bodies(tree_t secondary, nametab_t *tab)
1,013✔
2196
{
2197
   // Check for any missing subprogram or protected type bodies, and
2198
   // deferred constants which were not given values and
2199
   tree_t primary = tree_primary(secondary);
1,013✔
2200
   const int ndecls = tree_decls(primary);
1,013✔
2201
   for (int i = 0; i < ndecls; i++) {
20,866✔
2202
      tree_t d = tree_decl(primary, i);
19,855✔
2203
      switch (tree_kind(d)) {
19,855✔
2204
      case T_FUNC_DECL:
15,604✔
2205
      case T_PROC_DECL:
2206
         if (tree_flags(d) & TREE_F_PREDEFINED)
15,604✔
2207
            break;
2208
         // Fall-through
2209
      case T_PROT_DECL:
2210
         sem_missing_body_cb(d, secondary, tab);
7,114✔
2211
         break;
7,114✔
2212
      case T_CONST_DECL:
1,199✔
2213
         if (!tree_has_value(d)) {
1,199✔
2214
            tree_t d2 = get_local_decl(tab, NULL, tree_ident(d), 0);
407✔
2215
            if (d2 == NULL || !tree_has_value(d2))
407✔
2216
               sem_error(d, "deferred constant %s was not given a value in the "
2✔
2217
                         "package body", istr(tree_ident(d)));
2218
         }
2219
      default:
2220
         break;
2221
      }
2222
   }
2223

2224
   return true;
2225
}
2226

2227
static bool sem_check_pack_body(tree_t t, nametab_t *tab)
810✔
2228
{
2229
   if (!tree_has_primary(t))
810✔
2230
      return false;
2231

2232
   if (!sem_check_context_clause(t, tab))
805✔
2233
      return false;
2234

2235
   if (!sem_check_missing_bodies(t, tab))
805✔
2236
      return false;
2✔
2237

2238
   return true;
2239
}
2240

2241
static bool sem_check_component(tree_t t, nametab_t *tab)
372✔
2242
{
2243
   return true;
372✔
2244
}
2245

2246
static void sem_passive_cb(tree_t t, void *context)
1✔
2247
{
2248
   tree_t s = context;
1✔
2249

2250
   diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
1✔
2251
   diag_printf(d, "signal assignment statement not allowed inside passive "
1✔
2252
               "process");
2253
   diag_hint(d, tree_loc(s), "process in entity statement part must "
1✔
2254
             "be passive");
2255
   diag_hint(d, tree_loc(t), "signal assignment statement");
1✔
2256
   diag_lrm(d, STD_93, "1.1.3");
1✔
2257
   diag_lrm(d, STD_93, "9.2");
1✔
2258

2259
   diag_emit(d);
1✔
2260
}
1✔
2261

2262
static bool sem_check_entity(tree_t t, nametab_t *tab)
5,338✔
2263
{
2264
   if (!sem_check_context_clause(t, tab))
5,338✔
2265
      return false;
2266

2267
   // All processes in entity statement part must be passive
2268
   const int nstmts = tree_stmts(t);
5,338✔
2269
   for (int i = 0; i < nstmts; i++) {
5,375✔
2270
      tree_t s = tree_stmt(t, i);
37✔
2271
      tree_visit_only(s, sem_passive_cb, s, T_SIGNAL_ASSIGN);
37✔
2272
   }
2273

2274
   return true;
2275
}
2276

2277
static bool sem_check_arch(tree_t t, nametab_t *tab)
5,350✔
2278
{
2279
   if (!tree_has_primary(t))
5,350✔
2280
      return false;
2281

2282
   if (!sem_check_context_clause(t, tab))
5,347✔
UNCOV
2283
      return false;
×
2284

2285
   return true;
2286
}
2287

2288
static tree_t sem_check_lvalue(tree_t t)
34,817✔
2289
{
2290
   switch (tree_kind(t)) {
78,740✔
2291
   case T_REF:
34,927✔
2292
      return sem_check_lvalue(tree_ref(t));
34,927✔
2293
   case T_ARRAY_SLICE:
8,996✔
2294
   case T_ARRAY_REF:
2295
   case T_ALIAS:
2296
   case T_RECORD_REF:
2297
   case T_ALL:
2298
      return sem_check_lvalue(tree_value(t));
8,996✔
2299
   case T_VAR_DECL:
2300
   case T_SIGNAL_DECL:
2301
   case T_PORT_DECL:
2302
   case T_CONST_DECL:
2303
   case T_IMPLICIT_SIGNAL:
2304
   case T_PARAM_DECL:
2305
   case T_EXTERNAL_NAME:
2306
      return t;
2307
   default:
10✔
2308
      return NULL;
10✔
2309
   }
2310
}
2311

2312
static bool sem_check_aggregate_target_element(tree_t target, int nth)
369✔
2313
{
2314
   tree_t a = tree_assoc(target, nth);
369✔
2315
   tree_t value = tree_value(a);
369✔
2316

2317
   if (tree_kind(value) != T_AGGREGATE) {
369✔
2318
      if (!sem_static_name(value, sem_locally_static))
351✔
2319
         sem_error(value, "aggregate element must be locally static name");
2✔
2320
   }
2321

2322
   const assoc_kind_t kind = tree_subkind(a);
367✔
2323
   switch (kind) {
367✔
2324
   case A_RANGE:
2✔
2325
      if (standard() >= STD_08) {
2✔
2326
         // LRM 08 section 10.6.2.1: it is an error if the element
2327
         // association contains a choice that is a discrete range and
2328
         // an expression of a type other than the aggregate type.
2329
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(value));
1✔
2330
         diag_printf(d, "range choice expression must have same type "
1✔
2331
                     "as aggregate");
2332
         diag_hint(d, tree_loc(value), "expression type is %s but "
1✔
2333
                   "aggregate is %s", type_pp(tree_type(value)),
2334
                   type_pp(tree_type(target)));
2335
         diag_lrm(d, STD_08, "10.6.2");
1✔
2336
         diag_emit(d);
1✔
2337
         return false;
1✔
2338
      }
2339
      // Fall-through
2340
   case A_OTHERS:
2341
      sem_error(a, "%s association not allowed in aggregate target",
4✔
2342
                assoc_kind_str(kind));
2343
   case A_NAMED:
2344
   case A_POS:
2345
   case A_SLICE:
2346
   case A_CONCAT:
2347
      break;
2348
   }
2349

2350
   const int nassocs = tree_assocs(target);
362✔
2351
   for (int j = nth + 1; j < nassocs; j++) {
650✔
2352
      tree_t cmp = tree_value(tree_assoc(target, j));
290✔
2353
      if (same_tree(value, cmp)) {
290✔
2354
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(cmp));
2✔
2355
         diag_printf(d, "%s %s is identifed more than once in "
4✔
2356
                     "aggregate target", class_str(class_of(cmp)),
2357
                     tree_kind(cmp) == T_REF ?
2✔
2358
                     istr(tree_ident(cmp)) : "subelement");
2✔
2359
         diag_hint(d, tree_loc(value), "first seen here");
2✔
2360
         diag_lrm(d, STD_08, "10.5.2");
2✔
2361
         diag_emit(d);
2✔
2362
         return false;
2✔
2363
      }
2364
   }
2365

2366
   return true;
2367
}
2368

2369
static bool sem_check_variable_target(tree_t target)
18,745✔
2370
{
2371
   if (tree_kind(target) == T_AGGREGATE) {
18,745✔
2372
      // Rules for aggregate variable targets in LRM 93 section 8.5
2373

2374
      type_t type = tree_type(target);
76✔
2375
      if (!type_is_composite(type))
76✔
UNCOV
2376
         sem_error(target, "aggregate target of variable assignment has "
×
2377
                   "non-composite type %s", type_pp(tree_type(target)));
2378

2379
      const int nassocs = tree_assocs(target);
76✔
2380
      for (int i = 0; i < nassocs; i++) {
237✔
2381
         tree_t a = tree_assoc(target, i);
167✔
2382
         tree_t value = tree_value(a);
167✔
2383

2384
         if (!sem_check_variable_target(value))
167✔
2385
            return false;
2386

2387
         if (!sem_check_aggregate_target_element(target, i))
166✔
2388
            return false;
2389
      }
2390
   }
2391
   else {
2392
      tree_t decl = sem_check_lvalue(target);
18,669✔
2393
      const tree_kind_t kind = decl ? tree_kind(decl) : T_LAST_TREE_KIND;
18,669✔
2394

2395
      const bool suitable = kind == T_VAR_DECL
37,337✔
2396
         || (kind == T_PARAM_DECL && tree_class(decl) == C_VARIABLE)
3,251✔
2397
         || (kind == T_EXTERNAL_NAME && tree_class(decl) == C_VARIABLE);
18,675✔
2398

2399
      if (!suitable) {
18,669✔
2400
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(target));
6✔
2401
         diag_printf(d, "target of variable assignment must be a variable "
6✔
2402
                     "name or aggregate");
2403

2404
         tree_t ref = name_to_ref(target);
6✔
2405
         if (ref != NULL && tree_has_ref(ref))
6✔
2406
            diag_hint(d, tree_loc(target), "%s is a %s", istr(tree_ident(ref)),
5✔
2407
                      class_str(class_of(tree_ref(ref))));
2408

2409
         diag_emit(d);
6✔
2410
         return false;
6✔
2411
      }
2412
      else if (kind == T_PARAM_DECL && tree_subkind(decl) == PORT_IN) {
18,663✔
2413
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(target));
1✔
2414
         diag_printf(d, "cannot assign to parameter %s with mode IN",
1✔
2415
                     istr(tree_ident(decl)));
2416
         diag_hint(d, tree_loc(decl), "%s declared with mode IN",
1✔
2417
                   istr(tree_ident(decl)));
2418
         diag_hint(d, tree_loc(target), "target of variable assignment");
1✔
2419
         diag_emit(d);
1✔
2420
         return false;
1✔
2421
      }
2422
      else if (type_is_protected(tree_type(target)))
18,662✔
2423
         sem_error(target, "may not assign to variable of a protected type");
1✔
2424
   }
2425

2426
   return true;
2427
}
2428

2429
static bool sem_check_var_assign(tree_t t, nametab_t *tab)
18,695✔
2430
{
2431
   tree_t target = tree_target(t);
18,695✔
2432
   tree_t value = tree_value(t);
18,695✔
2433

2434
   if (!sem_check(target, tab))
18,695✔
2435
      return false;
2436

2437
   if (!sem_check(value, tab))
18,669✔
2438
      return false;
2439

2440
   if (!sem_check_readable(value))
18,579✔
2441
      return false;
2442

2443
   if (!sem_check_variable_target(target))
18,578✔
2444
      return false;
2445

2446
   if (!sem_check_same_type(value, target)) {
18,565✔
2447
      type_t target_type = tree_type(target);
11✔
2448
      type_t value_type  = tree_type(value);
11✔
2449
      sem_error(t, "type of value %pT does not match type of target %pT",
11✔
2450
                value_type, target_type);
2451
   }
2452

2453
   return true;
2454
}
2455

2456
static bool sem_check_waveforms(tree_t t, tree_t target, nametab_t *tab)
8,110✔
2457
{
2458
   type_t std_time = std_type(NULL, STD_TIME);
8,110✔
2459
   type_t expect = tree_type(target);
8,110✔
2460

2461
   const int nwaves = tree_waveforms(t);
8,110✔
2462
   for (int i = 0; i < nwaves; i++) {
16,444✔
2463
      tree_t waveform = tree_waveform(t, i);
8,355✔
2464

2465
      if (tree_has_value(waveform)) {
8,355✔
2466
         tree_t value = tree_value(waveform);
8,331✔
2467

2468
         if (!sem_check(value, tab))
8,331✔
2469
            return false;
2470

2471
         if (!sem_check_readable(value))
8,322✔
2472
            return false;
2473

2474
         if (!sem_check_type(value, expect, tab))
8,321✔
2475
            sem_error(t, "type of value %pT does not match type of target %pT",
6✔
2476
                      tree_type(value), expect);
2477
      }
2478
      else {
2479
         tree_t decl = sem_check_lvalue(target);
24✔
2480
         if (decl != NULL && !is_guarded_signal(decl))
24✔
2481
            sem_error(waveform, "a null waveform element is only valid when "
5✔
2482
                      "the target is a guarded signal");
2483
      }
2484

2485
      if (tree_has_delay(waveform)) {
8,334✔
2486
         tree_t delay = tree_delay(waveform);
965✔
2487
         if (!sem_check(delay, tab))
965✔
2488
            return false;
2489

2490
         if (!sem_check_type(delay, std_time, tab))
965✔
2491
            sem_error(delay, "type of delay must be %pT but have %pT",
8,334✔
2492
                      std_time, tree_type(delay));
2493
      }
2494
   }
2495

2496
   return true;
2497
}
2498

2499
static tree_t sem_check_view_target(tree_t target)
291✔
2500
{
2501
   switch (tree_kind(target)) {
320✔
2502
   case T_REF:
197✔
2503
      {
2504
         tree_t decl = tree_ref(target);
197✔
2505
         switch (tree_kind(decl)) {
197✔
2506
         case T_PORT_DECL:
104✔
2507
         case T_PARAM_DECL:
2508
            {
2509
               const port_mode_t mode = tree_subkind(decl);
104✔
2510
               if (mode == PORT_ARRAY_VIEW || mode == PORT_RECORD_VIEW)
104✔
2511
                  return tree_value(decl);
101✔
2512
            }
2513
         default:
2514
            return NULL;
2515
         }
2516
      }
2517

2518
   case T_ARRAY_REF:
29✔
2519
   case T_ARRAY_SLICE:
2520
      return sem_check_view_target(tree_value(target));
29✔
2521

2522
   case T_RECORD_REF:
94✔
2523
      {
2524
         tree_t view = sem_check_view_target(tree_value(target));
94✔
2525
         if (view == NULL)
94✔
2526
            return NULL;
2527

2528
         bool converse = false;
84✔
2529
         tree_t f = tree_ref(target);
84✔
2530
         tree_t e = find_element_mode_indication(view, f, &converse);
84✔
2531
         if (e == NULL)
84✔
2532
            return NULL;
2533

2534
         if (converse_mode(e, converse) == PORT_IN) {
84✔
2535
            tree_t port = tree_ref(name_to_ref(target));
3✔
2536
            diag_t *d = diag_new(DIAG_ERROR, tree_loc(target));
3✔
2537
            diag_printf(d, "cannot assign to element %s of port %s which has "
3✔
2538
                        "mode IN from mode view indication",
2539
                        istr(tree_ident(e)), istr(tree_ident(port)));
2540
            diag_hint(d, tree_loc(target), "target of signal assignment");
3✔
2541
            diag_hint(d, tree_loc(port), "sub-element %s of %s declared with "
3✔
2542
                      "mode IN due to mode view indication",
2543
                      istr(tree_ident(e)), istr(tree_ident(port)));
2544
            diag_emit(d);
3✔
2545
            return NULL;
3✔
2546
         }
2547

2548
         return NULL;
2549
      }
2550

2551
   default:
2552
      return NULL;
2553
   }
2554
}
2555

2556
static bool sem_check_signal_target(tree_t target, nametab_t *tab, bool guarded)
8,179✔
2557
{
2558
   if (tree_kind(target) == T_AGGREGATE) {
8,179✔
2559
      // Rules for aggregate signal targets in LRM 93 section 8.4
2560

2561
      type_t type = tree_type(target);
86✔
2562
      if (!type_is_composite(type))
86✔
UNCOV
2563
         sem_error(target, "aggregate target of signal assignment has "
×
2564
                   "non-composite type %s", type_pp(type));
2565

2566
      bool has_guarded = false, has_unguarded = false;
86✔
2567
      const int nassocs = tree_assocs(target);
86✔
2568
      for (int i = 0; i < nassocs; i++) {
285✔
2569
         tree_t a = tree_assoc(target, i);
208✔
2570
         tree_t value = tree_value(a);
208✔
2571

2572
         if (!sem_check_signal_target(value, tab, guarded))
208✔
2573
            return false;
2574

2575
         if (!sem_check_aggregate_target_element(target, i))
203✔
2576
            return false;
2577

2578
         tree_t ref = name_to_ref(value);
199✔
2579
         if (ref != NULL && is_guarded_signal(tree_ref(ref)))
199✔
2580
            has_guarded = true;
2581
         else
2582
            has_unguarded = true;
2583
      }
2584

2585
      if (has_guarded && has_unguarded) {
77✔
2586
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(target));
1✔
2587
         diag_printf(d, "aggregate target of signal assignment contains both "
1✔
2588
                     "guarded and unguarded signals");
2589
         diag_lrm(d, STD_08, "11.6");
1✔
2590
         diag_emit(d);
1✔
2591
         return false;
1✔
2592
      }
2593

2594
      return true;
2595
   }
2596
   else {
2597
      tree_t decl = sem_check_lvalue(target);
8,093✔
2598
      if (decl == NULL)
8,093✔
2599
         sem_error(target, "target of signal assignment must be a signal "
3✔
2600
                   "name or aggregate");
2601

2602
      if (is_guarded_signal(decl) && !guarded) {
8,090✔
2603
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(target));
1✔
2604
         diag_printf(d, "guarded signal %s cannot be the target of an "
1✔
2605
                     "unguarded assignment", istr(tree_ident(decl)));
2606
         diag_hint(d, tree_loc(decl), "%s declared here",
1✔
2607
                   istr(tree_ident(decl)));
2608
         diag_lrm(d, STD_08, "11.6");
1✔
2609
         diag_emit(d);
1✔
2610
         return false;
1✔
2611
      }
2612

2613
      switch (tree_kind(decl)) {
8,089✔
2614
      case T_SIGNAL_DECL:
6,267✔
2615
         {
2616
            tree_t sub = find_enclosing(tab, S_SUBPROGRAM);
6,267✔
2617
            if (sub != NULL && find_enclosing(tab, S_PROCESS) == NULL) {
6,267✔
2618
               // LRM 08 section 10.5.2.2: if a signal assignment appears
2619
               // in a procedure not contained within a process then the
2620
               // target must be a formal parameter
2621
               diag_t *d = diag_new(DIAG_ERROR, tree_loc(target));
4✔
2622
               diag_printf(d, "signal %s is not a formal parameter and "
4✔
2623
                           "subprogram %s is not contained within a process "
2624
                           "statement", istr(tree_ident(decl)),
2625
                           type_pp(tree_type(sub)));
2626
               diag_lrm(d, STD_08, "10.5.2.2");
4✔
2627
               diag_emit(d);
4✔
2628
               return false;
4✔
2629
            }
2630
         }
2631
         break;
2632

2633
      case T_IMPLICIT_SIGNAL:
1✔
2634
         sem_error(target, "implicit signal may not be assigned");
1✔
2635

2636
      case T_PORT_DECL:
1,792✔
2637
      case T_PARAM_DECL:
2638
         {
2639
            const port_mode_t mode = tree_subkind(decl);
1,792✔
2640
            if (mode == PORT_IN) {
1,792✔
2641
               diag_t *d = diag_new(DIAG_ERROR, tree_loc(target));
5✔
2642
               diag_printf(d, "cannot assign to input %s %s",
11✔
2643
                           tree_kind(decl) == T_PORT_DECL
5✔
2644
                           ? "port" : "parameter",
2645
                           istr(tree_ident(decl)));
2646
               diag_hint(d, tree_loc(target), "target of signal assignment");
5✔
2647
               diag_hint(d, tree_loc(decl), "%s declared with mode IN",
5✔
2648
                         istr(tree_ident(decl)));
2649
               diag_emit(d);
5✔
2650
               return false;
5✔
2651
            }
2652
            else if (mode == PORT_ARRAY_VIEW || mode == PORT_RECORD_VIEW) {
1,787✔
2653
               tree_t view = sem_check_view_target(target);
88✔
2654
               if (view != NULL) {
88✔
2655
                  tree_t inport = NULL;
4✔
2656
                  type_t view_type = tree_type(view);
4✔
2657
                  const int nelems = type_fields(view_type);
4✔
2658
                  for (int i = 0; i < nelems; i++) {
10✔
2659
                     tree_t e = type_field(view_type, i);
7✔
2660
                     const port_mode_t mode = tree_subkind(e);
7✔
2661
                     if (mode == PORT_IN || mode == PORT_ARRAY_VIEW
7✔
2662
                         || mode == PORT_RECORD_VIEW) {
6✔
2663
                        // This is not correct for nested mode view
2664
                        // indications but seems like a very obscure
2665
                        // corner case
2666
                        inport = e;
2667
                        break;
2668
                     }
2669
                  }
2670

2671
                  if (inport != NULL) {
4✔
2672
                     diag_t *d = diag_new(DIAG_ERROR, tree_loc(target));
1✔
2673
                     diag_printf(d, "cannot assign to port %s with mode view "
1✔
2674
                                 "indication as one or more sub-elements have "
2675
                                 "mode IN", istr(tree_ident(decl)));
2676
                     diag_hint(d, tree_loc(target),
1✔
2677
                               "target of signal assignment");
2678
                     diag_hint(d, tree_loc(inport),
1✔
2679
                               "element %s declared with mode IN",
2680
                               istr(tree_ident(inport)));
2681
                     diag_emit(d);
1✔
2682
                     return false;
1✔
2683
                  }
2684
               }
2685

2686
               return true;
87✔
2687
            }
2688
            else if (mode == PORT_LINKAGE)
1,699✔
2689
               sem_error(target, "linkage port %s may not be updated except as "
1✔
2690
                         "an actual corresponding to an interface of mode "
2691
                         "linkage", istr(tree_ident(decl)));
2692
            else if (tree_class(decl) != C_SIGNAL) {
1,698✔
2693
               diag_t *d = diag_new(DIAG_ERROR, tree_loc(target));
1✔
2694
               diag_printf(d, "%s is not a valid target of signal assignment",
1✔
2695
                           istr(tree_ident(decl)));
2696
               diag_hint(d, tree_loc(target), "target of signal assignment");
1✔
2697
               diag_hint(d, tree_loc(decl), "declared with class %s",
1✔
2698
                         class_str(tree_class(decl)));
2699
               diag_emit(d);
1✔
2700
               return false;
1✔
2701
            }
2702
         }
2703
         break;
2704

2705
      case T_EXTERNAL_NAME:
28✔
2706
         {
2707
            if (tree_class(decl) != C_SIGNAL) {
28✔
UNCOV
2708
               tree_t tail = tree_part(decl, tree_parts(decl) - 1);
×
2709
               diag_t *d = diag_new(DIAG_ERROR, tree_loc(target));
×
2710
               diag_printf(d, "external name %s is not a valid target of "
×
2711
                           "signal assignment", istr(tree_ident(tail)));
UNCOV
2712
               diag_hint(d, tree_loc(target), "target of signal assignment");
×
2713
               diag_hint(d, tree_loc(decl), "declared with class %s",
×
2714
                         class_str(tree_class(decl)));
UNCOV
2715
               diag_emit(d);
×
2716
               return false;
×
2717
            }
2718

2719
            tree_t sub = find_enclosing(tab, S_SUBPROGRAM);
28✔
2720
            if (sub != NULL && find_enclosing(tab, S_PROCESS) == NULL)
28✔
2721
               sem_error(target, "cannot create driver for external name as "
1✔
2722
                         "subprogram %s is not contained within a process "
2723
                         "statement", type_pp(tree_type(sub)));
2724
         }
2725
         break;
2726

2727
      case T_VAR_DECL:
1✔
2728
      case T_CONST_DECL:
2729
         {
2730
            diag_t *d = diag_new(DIAG_ERROR, tree_loc(target));
1✔
2731
            diag_printf(d, "%s %s is not a valid target of signal assignment",
1✔
2732
                        class_str(class_of(decl)), istr(tree_ident(decl)));
2733
            diag_hint(d, tree_loc(target), "target of signal assignment");
1✔
2734
            diag_hint(d, tree_loc(decl), "declared as %s",
1✔
2735
                      class_str(class_of(decl)));
2736
            diag_emit(d);
1✔
2737
            return false;
1✔
2738
         }
2739

UNCOV
2740
      default:
×
2741
         sem_error(target, "invalid target of signal assignment");
×
2742
      }
2743

2744
      return true;
7,987✔
2745
   }
2746
}
2747

2748
static bool sem_check_reject(tree_t t, nametab_t *tab)
724✔
2749
{
2750
   if (!sem_check(t, tab))
724✔
2751
      return false;
2752

2753
   if (!type_eq(tree_type(t), std_type(NULL, STD_TIME)))
724✔
2754
      sem_error(t, "reject interval must have type TIME but have %s",
1✔
2755
                type_pp(tree_type(t)));
2756

2757
   return true;
2758
}
2759

2760
static bool sem_check_signal_assign(tree_t t, nametab_t *tab)
5,690✔
2761
{
2762
   tree_t target = tree_target(t);
5,690✔
2763

2764
   if (!sem_check(target, tab))
5,690✔
2765
      return false;
2766

2767
   if (!sem_check_signal_target(target, tab, true))
5,685✔
2768
      return false;
2769

2770
   if (!sem_check_waveforms(t, target, tab))
5,670✔
2771
      return false;
2772

2773
   if (tree_has_reject(t) && !sem_check_reject(tree_reject(t), tab))
5,653✔
UNCOV
2774
      return false;
×
2775

2776
   return true;
2777
}
2778

2779
static bool sem_check_guard(tree_t t, nametab_t *tab)
14✔
2780
{
2781
   assert(tree_kind(t) == T_GUARD);
14✔
2782

2783
   if (!sem_check_type(t, std_type(NULL, STD_BOOLEAN), tab))
14✔
2784
      sem_error(t, "guard signal must have BOOLEAN type but found %s",
1✔
2785
                type_pp(tree_type(t)));
2786

2787
   tree_t decl = tree_ref(t);
13✔
2788
   switch (tree_kind(decl)) {
13✔
2789
   case T_SIGNAL_DECL:
2790
   case T_IMPLICIT_SIGNAL:
2791
      break;
UNCOV
2792
   case T_PORT_DECL:
×
2793
      if (tree_class(decl) == C_SIGNAL)
×
2794
         break;
2795
      // Fall-through
2796
   default:
2797
      {
2798
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
1✔
2799
         diag_printf(d, "assignment guard must be a signal");
1✔
2800
         diag_hint(d, tree_loc(decl), "%s is a %s", istr(tree_ident(decl)),
1✔
2801
                   class_str(class_of(decl)));
2802
         diag_hint(d, tree_loc(t), "guarded statement");
1✔
2803
         diag_emit(d);
1✔
2804
         return false;
1✔
2805
      }
2806
   }
2807

2808
   return true;
2809
}
2810

2811
static bool sem_check_cond_assign(tree_t t, nametab_t *tab)
2,289✔
2812
{
2813
   tree_t target = tree_target(t);
2,289✔
2814

2815
   if (!sem_check(target, tab))
2,289✔
2816
      return false;
2817

2818
   const bool has_guard = tree_has_guard(t);
2,286✔
2819

2820
   if (!sem_check_signal_target(target, tab, has_guard))
2,286✔
2821
      return false;
2822

2823
   if (has_guard && !sem_check_guard(tree_guard(t), tab))
2,277✔
2824
      return false;
2825

2826
   type_t std_bool = std_type(NULL, STD_BOOLEAN);
2,275✔
2827

2828
   const int nconds = tree_conds(t);
2,275✔
2829
   for (int i = 0; i < nconds; i++) {
4,711✔
2830
      tree_t c = tree_cond(t, i);
2,442✔
2831

2832
      if (tree_has_value(c)) {
2,442✔
2833
         tree_t test = tree_value(c);
256✔
2834

2835
         if (!sem_check(test, tab))
256✔
2836
            return false;
2837

2838
         if (!type_eq(tree_type(test), std_bool))
256✔
2839
            sem_error(test, "type of condition must be BOOLEAN");
1✔
2840
      }
2841

2842
      assert(tree_stmts(c) == 1);
2,441✔
2843
      tree_t a = tree_stmt(c, 0);
2,441✔
2844

2845
      assert(tree_kind(a) == T_SIGNAL_ASSIGN);
2,441✔
2846
      assert(tree_target(a) == target);
2,441✔
2847

2848
      if (tree_has_reject(a) && !sem_check_reject(tree_reject(a), tab))
2,441✔
2849
         return false;
2850

2851
      if (!sem_check_waveforms(a, target, tab))
2,440✔
2852
         return false;
2853
   }
2854

2855
   return true;
2856
}
2857

2858
static bool sem_check_closely_related(type_t from, type_t to, tree_t where)
10,279✔
2859
{
2860
   if (type_eq(to, from))
10,279✔
2861
      return true;
2862

2863
   // Conversions are allowed between any abstract numeric types
2864
   if (type_is_numeric(from) && type_is_numeric(to))
5,684✔
2865
      return true;
2866

2867
   // Suppress cascading errors
2868
   if (type_is_none(from) || type_is_none(to))
2,546✔
2869
      return true;
1✔
2870

2871
   char *reason = NULL;
2,545✔
2872

2873
   if (type_is_array(from) && type_is_array(to)) {
2,545✔
2874
      const int from_dims = dimension_of(from);
2,520✔
2875
      const int to_dims = dimension_of(to);
2,520✔
2876

2877
      // Types must have same dimensionality
2878
      if (from_dims != to_dims) {
2,520✔
2879
         reason = xasprintf("%s has %d dimension%s but %s has %d",
1✔
2880
                            type_pp2(from, to), from_dims,
2881
                            from_dims == 1 ? "" : "s",
2882
                            type_pp2(to, from), to_dims);
2883
         goto not_closely_related;
1✔
2884
      }
2885

2886
      // Index types the same or closely related
2887
      for (int i = 0; i < from_dims; i++) {
5,038✔
2888
         type_t from_index = index_type_of(from, i);
2,520✔
2889
         type_t to_index = index_type_of(to, i);
2,520✔
2890

2891
         if (!sem_check_closely_related(from_index, to_index, NULL)) {
2,520✔
2892
            reason = xasprintf("%s index type of %s is %s which is not closely "
1✔
2893
                               "related to the %s index type of %s",
2894
                               ordinal_str(i + 1), type_pp2(from, to),
2895
                               type_pp(from_index), ordinal_str(i + 1),
2896
                               type_pp2(to, from));
2897
            goto not_closely_related;
1✔
2898
         }
2899
      }
2900

2901
      type_t from_e = type_elem(from);
2,518✔
2902
      type_t to_e = type_elem(to);
2,518✔
2903

2904
      if (standard() >= STD_08) {
2,518✔
2905
         // Element types must be closely related
2906
         if (!sem_check_closely_related(from_e, to_e, NULL)) {
1,938✔
2907
            reason = xasprintf("element type %s is not closely related to %s",
1✔
2908
                               type_pp2(from_e, to_e), type_pp2(to_e, from_e));
2909
            goto not_closely_related;
1✔
2910
         }
2911
      }
2912
      else {
2913
         // Element types must be the same
2914
         if (!type_eq(from_e, to_e)) {
580✔
2915
            reason = xasprintf("element type %s does not match %s",
1✔
2916
                               type_pp2(from_e, to_e), type_pp2(to_e, from_e));
2917
            goto not_closely_related;
1✔
2918
         }
2919
      }
2920

2921
      return true;
2,516✔
2922
   }
2923

2924
   if (type_is_record(from) && type_is_record(to) && standard() >= STD_19) {
25✔
2925
      // Each element of the target type must have a matching element in
2926
      // the from type
2927
      const int from_nf = type_fields(from);
21✔
2928
      const int to_nf = type_fields(to);
21✔
2929

2930
      for (int i = 0; i < to_nf; i++) {
68✔
2931
         tree_t to_f = type_field(to, i), from_f = NULL;
48✔
2932
         type_t to_ftype = tree_type(to_f);
48✔
2933
         ident_t name = tree_ident(to_f);
48✔
2934

2935
         for (int j = 0; j < from_nf && from_f == NULL; j++) {
130✔
2936
            tree_t f = type_field(from, j);
82✔
2937
            if (tree_ident(f) != name)
82✔
2938
               continue;
35✔
2939

2940
            type_t from_ftype = tree_type(f);
47✔
2941
            if (!sem_check_closely_related(from_ftype, to_ftype, NULL))
47✔
2942
               break;
2943

2944
            from_f = f;
2945
         }
2946

2947
         if (from_f != NULL)
48✔
2948
            continue;
47✔
2949

2950
         reason = xasprintf("field %s in record type %s has no matching "
1✔
2951
                            "element in type %s", istr(tree_ident(to_f)),
2952
                            type_pp2(to, from), type_pp2(from, to));
2953
         goto not_closely_related;
1✔
2954
      }
2955

2956
      return true;
2957
   }
2958

2959
 not_closely_related:
4✔
2960
   if (where != NULL) {
9✔
2961
      const loc_t *loc = tree_loc(where);
7✔
2962
      diag_t *d = diag_new(DIAG_ERROR, loc);
7✔
2963
      diag_printf(d, "conversion only allowed between closely related types");
7✔
2964
      if (reason != NULL)
7✔
2965
         diag_hint(d, loc, "%s", reason);
5✔
2966
      else
2967
         diag_hint(d, loc, "%pT and %pT are not closely related", from, to);
2✔
2968
      diag_lrm(d, STD_93, "7.3.5");
7✔
2969
      diag_emit(d);
7✔
2970

2971
      free(reason);
7✔
2972
   }
2973

2974
   return false;
2975
}
2976

2977
static bool sem_check_conversion(tree_t t, nametab_t *tab)
5,774✔
2978
{
2979
   // Type conversions are described in LRM 93 section 7.3.5
2980

2981
   tree_t value = tree_value(t);
5,774✔
2982
   if (!sem_check(value, tab))
5,774✔
2983
      return false;
2984

2985
   return sem_check_closely_related(tree_type(value), tree_type(t), t);
5,774✔
2986
}
2987

2988
static bool sem_check_compatible_view(tree_t formal, tree_t actual)
109✔
2989
{
2990
   type_t type = tree_type(formal);
109✔
2991

2992
   type_t elem_type = type;
109✔
2993
   if (tree_subkind(formal) == PORT_ARRAY_VIEW)
109✔
2994
      elem_type = type_elem(type);
18✔
2995

2996
   tree_t formal_view = tree_value(formal);
109✔
2997

2998
   tree_t actual_view = sem_check_view_target(actual);
109✔
2999
   if (actual_view != NULL) {
109✔
3000
      // Associating an interface with another interface: check the
3001
      // mode of each element is compatible
3002
      const int nfields = type_fields(elem_type);
13✔
3003
      for (int i = 0; i < nfields; i++) {
43✔
3004
         tree_t f = type_field(elem_type, i);
32✔
3005

3006
         bool formal_converse = false;
32✔
3007
         tree_t formal_elem = find_element_mode_indication(formal_view, f,
32✔
3008
                                                           &formal_converse);
3009

3010
         bool actual_converse = false;
32✔
3011
         tree_t actual_elem = find_element_mode_indication(actual_view, f,
32✔
3012
                                                           &actual_converse);
3013

3014
         const port_mode_t formal_mode =
32✔
3015
            converse_mode(formal_elem, formal_converse);
32✔
3016

3017
         const port_mode_t actual_mode =
32✔
3018
            converse_mode(actual_elem, actual_converse);
32✔
3019

3020
         if (formal_mode != actual_mode) {
32✔
3021
            diag_t *d = diag_new(DIAG_ERROR, tree_loc(actual));
2✔
3022
            diag_printf(d, "mode view indication of formal %s %s "
5✔
3023
                        "element %s is not compatible with actual",
3024
                        tree_kind(formal) == T_PORT_DECL ? "port" : "parameter",
2✔
3025
                        istr(tree_ident(formal)), istr(tree_ident(f)));
3026
            diag_hint(d, tree_loc(actual_view), "actual has mode %s from "
2✔
3027
                      "mode view indication on port %s",
3028
                      port_mode_str(actual_mode),
3029
                      istr(tree_ident(name_to_ref(actual))));
3030
            diag_hint(d, tree_loc(formal_view), "formal has mode %s",
2✔
3031
                      port_mode_str(formal_mode));
3032
            diag_emit(d);
2✔
3033
            return false;
2✔
3034
         }
3035
      }
3036
   }
3037

3038
   return true;
3039
}
3040

3041
static bool sem_check_call_args(tree_t t, tree_t decl, nametab_t *tab)
96,617✔
3042
{
3043
   const int nparams = tree_params(t);
96,617✔
3044
   const int nports  = tree_ports(decl);
96,617✔
3045

3046
   if (is_uninstantiated_subprogram(decl)) {
96,617✔
3047
      // Allow recursive calls to the same subprogram
3048
      tree_t sub = find_enclosing(tab, S_SUBPROGRAM);
5✔
3049
      if (sub != decl)
5✔
3050
         sem_error(t, "cannot call uninstantiated %s %s",
2✔
3051
                   class_str(class_of(decl)), istr(tree_ident(decl)));
3052
   }
3053

3054
   tree_t *map LOCAL = xcalloc_array(nports, sizeof(tree_t));
193,230✔
3055

3056
   bool have_named = false;
96,615✔
3057
   for (int i = 0; i < nparams; i++) {
274,902✔
3058
      tree_t param = tree_param(t, i), port = NULL;
178,383✔
3059
      type_t port_type = NULL;
178,383✔
3060
      bool partial = false;
178,383✔
3061
      int index = -1;
178,383✔
3062
      switch (tree_subkind(param)) {
178,383✔
3063
      case P_POS:
174,526✔
3064
         if (have_named)
174,526✔
3065
            sem_error(param, "positional parameters must precede named "
6✔
3066
                      "parameters");
3067
         else if ((index = tree_pos(param)) >= nports) {
174,520✔
3068
            diag_t *d = diag_new(DIAG_ERROR, tree_loc(param));
1✔
3069
            diag_printf(d, "too many positional parameters for subprogram %pT",
1✔
3070
                        tree_type(decl));
3071
            diag_hint(d, tree_loc(param), "%s positional parameter",
1✔
3072
                      ordinal_str(index + 1));
3073
            diag_hint(d, tree_loc(decl), "%s %s has %d formal parameter%s",
2✔
3074
                      class_str(class_of(decl)), istr(tree_ident(decl)),
3075
                      nports, nports > 1 ? "s" : "");
3076
            diag_emit(d);
1✔
3077
            return false;
1✔
3078
         }
3079
         else {
3080
            port = tree_port(decl, index);
174,519✔
3081
            port_type = tree_type(port);
174,519✔
3082
         }
3083
         break;
174,519✔
3084

3085
      case P_NAMED:
3,857✔
3086
         {
3087
            have_named = true;
3,857✔
3088

3089
            tree_t name = tree_name(param);
3,857✔
3090
            const tree_kind_t name_kind = tree_kind(name);
3,857✔
3091
            if (name_kind == T_TYPE_CONV || name_kind == T_FCALL)
3,857✔
3092
               sem_error(name, "sorry, conversions are not yet supported here");
1✔
3093
            else if (!sem_check(name, tab))
3,856✔
3094
               return false;
3095

3096
            if (tree_kind(name) == T_TYPE_CONV) {
3,851✔
UNCOV
3097
               type_t to_type = tree_type(name);
×
3098
               type_t from_type = tree_type(tree_value(name));
×
3099

UNCOV
3100
               const bool missing_constraints =
×
3101
                  type_is_unconstrained(from_type)
×
3102
                  && type_is_unconstrained(to_type);
×
3103

UNCOV
3104
               if (missing_constraints) {
×
3105
                  diag_t *d = diag_new(DIAG_ERROR, tree_loc(name));
×
3106
                  diag_printf(d, "result of conversion for unconstrained "
×
3107
                              "formal %s must be a constrained array type",
3108
                              istr(tree_ident(decl)));
UNCOV
3109
                  diag_lrm(d, STD_93, "3.2.1.1");
×
3110
                  diag_emit(d);
×
3111
                  return false;
×
3112
               }
3113
            }
3114

3115
            tree_t ref = name_to_ref(name);
3,851✔
3116
            assert(ref != NULL);
3,851✔
3117

3118
            if ((partial = (ref != name))) {
3,851✔
3119
               tree_t value = tree_value(name);
57✔
3120
               if (tree_kind(value) != T_REF)
57✔
UNCOV
3121
                  sem_error(name, "sorry, this form of named parameter is "
×
3122
                            "not supported");
3123
            }
3124

3125
            ident_t id = tree_ident(ref);
3,851✔
3126
            for (int j = 0; j < nports; j++) {
16,026✔
3127
               tree_t p = tree_port(decl, j);
16,025✔
3128
               if (tree_ident(p) == id) {
16,025✔
3129
                  index = j;
3130
                  port = p;
3131
                  break;
3132
               }
3133
            }
3134

3135
            if (index == -1) {
3,851✔
3136
               diag_t *d = diag_new(DIAG_ERROR, tree_loc(param));
1✔
3137
               diag_printf(d, "subprogram %pT has no parameter named %pI",
1✔
3138
                           tree_type(decl), id);
3139
               diag_hint(d, tree_loc(decl), "subprogram defined here");
1✔
3140
               diag_emit(d);
1✔
3141
               return false;
1✔
3142
            }
3143

3144
            if (!tree_has_ref(ref)) {
3,850✔
3145
               // Should have generated an error during overload
3146
               // resolution
UNCOV
3147
               assert(error_count() > 0);
×
3148
               return false;
3149
            }
3150

3151
            // Set the ref again here because solve_types may have set it
3152
            // to the wrong overload
3153
            if (tree_ref(ref) != port)
3,850✔
3154
               tree_set_name(param, (name = change_ref(name, port)));
791✔
3155

3156
            port_type = tree_type(name);
3,850✔
3157
         }
3158
      }
3159

3160
      class_t class    = tree_class(port);
178,369✔
3161
      port_mode_t mode = tree_subkind(port);
178,369✔
3162

3163
      if (map[index] != NULL && (!partial || tree_kind(map[index]) == T_REF))
178,369✔
3164
         sem_error(param, "formal parameter %pI already has an associated "
2✔
3165
                   "actual", tree_ident(port));
3166

3167
      map[index] = param;
178,367✔
3168

3169
      tree_t value = tree_value(param);
178,367✔
3170
      if (!sem_check(value, tab))
178,367✔
3171
         return false;
3172

3173
      if (!sem_check_type(value, port_type, tab))
178,315✔
3174
         sem_error(value, "type of actual %pT does not match formal %pI "
6✔
3175
                   "type %pT", tree_type(value), tree_ident(port), port_type);
3176

3177
      // LRM 08 sections 4.2.2.2 and 4.2.2.3
3178
      if (class == C_VARIABLE || class == C_SIGNAL) {
178,309✔
3179
         tree_t decl = sem_check_lvalue(value);
7,926✔
3180
         if (decl == NULL || class_of(value) != class) {
7,926✔
3181
            diag_t *d = diag_new(DIAG_ERROR, tree_loc(value));
9✔
3182
            diag_printf(d, "actual for formal %s with class %s must be "
13✔
3183
                        "a name denoting a %s", istr(tree_ident(port)),
3184
                        class == C_VARIABLE ? "VARIABLE" : "SIGNAL",
3185
                        class_str(class));
3186
            if (decl == NULL)
9✔
3187
               diag_hint(d, tree_loc(value), "actual designator is not a name");
5✔
3188
            else if (tree_kind(decl) == T_EXTERNAL_NAME)
4✔
3189
               diag_hint(d, tree_loc(value), "external name has class %s",
1✔
3190
                         class_str(tree_class(decl)));
3191
            else
3192
               diag_hint(d, tree_loc(value), "object %s has class %s",
3✔
3193
                         istr(tree_ident(decl)), class_str(class_of(decl)));
3194
            diag_lrm(d, STD_08, class == C_SIGNAL ? "4.2.2.3" : "4.2.2.2");
14✔
3195
            diag_emit(d);
9✔
3196
            return false;
9✔
3197
         }
3198

3199
         // Check OUT and INOUT parameters can be assigned to
3200
         if (mode == PORT_OUT || mode == PORT_INOUT) {
7,917✔
3201
            const tree_kind_t decl_kind = tree_kind(decl);
6,682✔
3202
            if ((decl_kind == T_PARAM_DECL || decl_kind == T_PORT_DECL)
6,682✔
3203
                && tree_subkind(decl) == PORT_IN) {
1,127✔
3204
               const char *what =
2✔
3205
                  decl_kind == T_PARAM_DECL ? "parameter" : "port";
1✔
3206
               diag_t *d = diag_new(DIAG_ERROR, tree_loc(value));
1✔
3207
               diag_printf(d, "cannot associate %s %s of mode IN with "
1✔
3208
                           "formal %s of mode %s", what,
3209
                           istr(tree_ident(decl)), istr(tree_ident(port)),
3210
                           port_mode_str(mode));
3211
               diag_hint(d, tree_loc(decl), "%s declared with mode %s",
1✔
3212
                         istr(tree_ident(decl)),
3213
                         port_mode_str(tree_subkind(decl)));
1✔
3214
               diag_hint(d, tree_loc(value), "associated with %s %s %s here",
1✔
3215
                         port_mode_str(mode), what, istr(tree_ident(port)));
3216
               diag_emit(d);
1✔
3217
               return false;
1✔
3218
            }
3219
            else if (decl_kind == T_SIGNAL_DECL && class == C_SIGNAL) {
6,681✔
3220
               tree_t sub = find_enclosing(tab, S_SUBPROGRAM);
244✔
3221
               if (sub != NULL && find_enclosing(tab, S_PROCESS) == NULL) {
244✔
3222
                  // LRM 08 section 10.5.2.2: if a signal is associated
3223
                  // with an inout or out signal parameter in a subprogram
3224
                  // call within a procedure not contained within a process
3225
                  // then the target must be a formal parameter
3226
                  diag_t *d = diag_new(DIAG_ERROR, tree_loc(value));
2✔
3227
                  diag_printf(d, "signal %pI is not a formal parameter and "
2✔
3228
                              "subprogram %pT is not contained within a "
3229
                              "process statement", tree_ident(decl),
3230
                              tree_type(sub));
3231
                  diag_lrm(d, STD_08, "10.5.2.2");
2✔
3232
                  diag_emit(d);
2✔
3233
                  return false;
2✔
3234
               }
3235
            }
3236
         }
3237
         else if ((mode == PORT_ARRAY_VIEW || mode == PORT_RECORD_VIEW)
1,235✔
3238
                  && !sem_check_compatible_view(port, value))
18✔
3239
            return false;
3240
      }
3241

3242
      if (class == C_SIGNAL && !sem_static_name(value, sem_globally_static)) {
178,296✔
3243
         diag_t *d = pedantic_diag(tree_loc(value));
4✔
3244
         if (d != NULL) {
4✔
3245
            diag_printf(d, "actual associated with signal parameter %s must be "
4✔
3246
                        "denoted by a static signal name",
3247
                        istr(tree_ident(port)));
3248
            diag_hint(d, tree_loc(value), "not a static signal name");
4✔
3249
            diag_lrm(d, STD_08, "4.2.2.3");
4✔
3250
            diag_lrm(d, STD_08, "8.1");
4✔
3251
            diag_emit(d);
4✔
3252
            return false;
4✔
3253
         }
3254
      }
3255

3256
      // Check IN and INOUT parameters can be read
3257
      if (tree_kind(t) != T_ATTR_REF) {
178,292✔
3258
         const port_mode_t mode = tree_subkind(port);
178,292✔
3259
         if (mode == PORT_IN || mode == PORT_INOUT) {
178,292✔
3260
            if (!sem_check_readable(value))
175,780✔
3261
               return false;
3262
         }
3263
      }
3264

3265
      if (tree_kind(value) == T_OPEN && !tree_has_value(port)) {
178,288✔
3266
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(param));
1✔
3267
         diag_printf(d, "OPEN actual for formal parameter %s without "
1✔
3268
                     "default value", istr(tree_ident(port)));
3269
         diag_hint(d, tree_loc(port), "%s declared here",
1✔
3270
                   istr(tree_ident(port)));
3271
         diag_emit(d);
1✔
3272
         return false;
1✔
3273
      }
3274
   }
3275

3276
   for (int i = 0; i < nports; i++) {
281,111✔
3277
      if (map[i] == NULL) {
184,595✔
3278
         tree_t port = tree_port(decl, i);
6,359✔
3279
         if (!tree_has_value(port))
6,359✔
3280
            sem_error(t, "missing actual for formal parameter %s without "
2✔
3281
                      "default value", istr(tree_ident(port)));
3282
         else {
3283
            const port_mode_t mode = tree_subkind(port);
6,357✔
3284
            if (mode == PORT_ARRAY_VIEW || mode == PORT_RECORD_VIEW)
6,357✔
3285
               sem_error(t, "missing actual for formal parameter %pI with "
184,593✔
3286
                         "mode view indication %pT", tree_ident(port),
3287
                         tree_type(tree_value(port)));
3288
         }
3289
      }
3290
   }
3291

3292
   return true;
3293
}
3294

3295
static bool sem_check_fcall(tree_t t, nametab_t *tab)
87,750✔
3296
{
3297
   if (!tree_has_ref(t))
87,750✔
3298
      return false;
3299

3300
   if (tree_kind(t) == T_PROT_FCALL && tree_has_name(t)) {
87,668✔
3301
      tree_t name = tree_name(t);
855✔
3302
      if (!sem_check(name, tab))
855✔
3303
         return false;
3304
   }
3305

3306
   tree_t decl = tree_ref(t), sub;
87,668✔
3307
   const tree_flags_t flags = tree_flags(decl);
87,668✔
3308

3309
   if ((flags & TREE_F_IMPURE) && (sub = find_enclosing(tab, S_SUBPROGRAM))) {
87,668✔
3310
      // Pure function may not call an impure function
3311
      if (tree_kind(sub) == T_FUNC_BODY && !(tree_flags(sub) & TREE_F_IMPURE)) {
1,429✔
3312
         diag_t *d = pedantic_diag(tree_loc(t));
2✔
3313
         if (d != NULL) {
2✔
3314
            diag_printf(d, "pure function %pI cannot call impure function %pI",
2✔
3315
                        tree_ident(sub), tree_ident(decl));
3316
            diag_emit(d);
2✔
3317
         }
3318
      }
3319

3320
      // Propagate impurity flags
3321
      tree_set_flag(sub, flags & (TREE_F_IMPURE_FILE | TREE_F_IMPURE_SHARED));
1,429✔
3322
   }
3323
   else if (tree_kind(decl) == T_FUNC_DECL && !(flags & TREE_F_PREDEFINED)
86,239✔
3324
            && is_same_region(tab, decl) && opt_get_int(OPT_MISSING_BODY)) {
13,771✔
3325
      diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
4✔
3326
      diag_printf(d, "subprogram %pT called before its body has been "
4✔
3327
                  "elaborated", tree_type(decl));
3328
      diag_hint(d, tree_loc(decl), "%pT declared here", tree_type(decl));
4✔
3329
      diag_lrm(d, STD_08, "14.4.2");
4✔
3330
      diag_emit(d);
4✔
3331
      return false;
4✔
3332
   }
3333

3334
   if (!sem_check_call_args(t, decl, tab))
87,664✔
3335
      return false;
3336

3337
   if (sem_locally_static(t))
87,606✔
3338
      tree_set_flag(t, TREE_F_LOCALLY_STATIC | TREE_F_GLOBALLY_STATIC);
12,247✔
3339
   else if (sem_globally_static(t))
75,359✔
3340
      tree_set_flag(t, TREE_F_GLOBALLY_STATIC);
11,999✔
3341

3342
   return true;
3343
}
3344

3345
static bool sem_check_pcall(tree_t t, nametab_t *tab)
8,996✔
3346
{
3347
   if (!tree_has_ref(t))
8,996✔
3348
      return false;
3349

3350
   const bool is_protected = (tree_kind(t) == T_PROT_PCALL);
8,955✔
3351
   if (is_protected && tree_has_name(t)
8,955✔
3352
       && !sem_check(tree_name(t), tab))
200✔
3353
      return false;
3354

3355
   tree_t decl = tree_ref(t);
8,954✔
3356

3357
   switch (class_of(decl)) {
8,954✔
3358
   case C_PROCEDURE:
3359
      break;
8,953✔
3360
   case C_FUNCTION:
1✔
3361
      sem_error(t, "function %pT cannot be called as a procedure",
1✔
3362
                tree_type(decl));
UNCOV
3363
   default:
×
3364
      // All other errors should be caught at parsing stage
UNCOV
3365
      assert(error_count() > 0);
×
3366
      return false;
3367
   }
3368

3369
   if (!sem_check_call_args(t, decl, tab))
8,953✔
3370
      return false;
3371

3372
   const tree_flags_t flags = tree_flags(decl);
8,910✔
3373

3374
   const bool never_waits = is_protected || !!(flags & TREE_F_NEVER_WAITS);
8,910✔
3375
   const bool has_wait = !is_protected && !!(flags & TREE_F_HAS_WAIT);
8,910✔
3376

3377
   assert(!never_waits || !has_wait);
8,910✔
3378

3379
   tree_t sub = find_enclosing(tab, S_SUBPROGRAM);
8,910✔
3380
   if (sub != NULL) {
8,910✔
3381
      if (!never_waits)
4,079✔
3382
         tree_clear_flag(sub, TREE_F_NEVER_WAITS);
168✔
3383

3384
      if (has_wait)
4,079✔
3385
         tree_set_flag(sub, TREE_F_HAS_WAIT);
28✔
3386

3387
      if (flags & TREE_F_IMPURE_FILE)
4,079✔
3388
         tree_set_flag(sub, TREE_F_IMPURE_FILE);
246✔
3389

3390
      if (flags & TREE_F_IMPURE_SHARED)
4,079✔
3391
         tree_set_flag(sub, TREE_F_IMPURE_SHARED);
6✔
3392

3393
      const bool in_func = tree_kind(sub) == T_FUNC_BODY;
4,079✔
3394
      const bool in_pure_func = in_func && !(tree_flags(sub) & TREE_F_IMPURE);
4,079✔
3395

3396
      if (has_wait && in_func)
4,079✔
3397
         sem_error(t, "function %pI cannot call procedure %pI which contains "
2✔
3398
                   "a wait statement", tree_ident(sub), tree_ident(decl));
3399
      else if ((flags & TREE_F_IMPURE_FILE) && in_pure_func) {
4,077✔
3400
         diag_t *d = pedantic_diag(tree_loc(t));
1✔
3401
         if (d != NULL) {
1✔
3402
            diag_printf(d, "pure function %pI cannot call procedure %pI which "
1✔
3403
                        "references a file object", tree_ident(sub),
3404
                        tree_ident(decl));
3405
            diag_emit(d);
1✔
3406
         }
3407
      }
3408
      else if ((flags & TREE_F_IMPURE_SHARED) && in_pure_func) {
4,076✔
3409
         diag_t *d = pedantic_diag(tree_loc(t));
4✔
3410
         if (d != NULL) {
4✔
3411
            diag_printf(d, "pure function %pI cannot call procedure %pI which "
4✔
3412
                        "references a shared variable", tree_ident(sub),
3413
                        tree_ident(decl));
3414
            diag_emit(d);
4✔
3415
         }
3416
      }
3417
   }
3418

3419
   if (!never_waits) {
8,908✔
3420
      // Procedure may wait, suppress infinite loop warning
3421
      tree_t proc = find_enclosing(tab, S_PROCESS);
923✔
3422
      if (proc != NULL)
923✔
3423
         tree_set_flag(proc, TREE_F_HAS_WAIT);
738✔
3424
   }
3425

3426
   return true;
3427
}
3428

3429
static bool sem_check_wait(tree_t t, nametab_t *tab)
10,228✔
3430
{
3431
   if (tree_has_delay(t)) {
10,228✔
3432
      type_t std_time = std_type(NULL, STD_TIME);
5,459✔
3433
      tree_t delay = tree_delay(t);
5,459✔
3434

3435
      if (!sem_check(delay, tab))
5,459✔
3436
         return false;
3437

3438
      if (!sem_check_type(delay, std_time, tab))
5,458✔
3439
         sem_error(delay, "type of delay must be %pT but have %pT",
2✔
3440
                   std_time, tree_type(delay));
3441
   }
3442

3443
   if (tree_has_value(t)) {
10,225✔
3444
      type_t std_bool = std_type(NULL, STD_BOOLEAN);
322✔
3445
      tree_t value = tree_value(t);
322✔
3446

3447
      if (!sem_check(value, tab))
322✔
3448
         return false;
3449

3450
      if (!sem_check_type(value, std_bool, tab))
322✔
3451
         sem_error(value, "type of condition must be BOOLEAN but have %pT",
1✔
3452
                   tree_type(value));
3453
   }
3454

3455
   if (find_enclosing(tab, S_PROTECTED))
10,224✔
3456
      sem_error(t, "wait statement not allowed in protected subprogram body");
1✔
3457

3458
   tree_t sub = find_enclosing(tab, S_SUBPROGRAM);
10,223✔
3459
   if (sub != NULL) {
10,223✔
3460
      if (tree_kind(sub) == T_FUNC_BODY)
489✔
3461
         sem_error(t, "wait statement not allowed in function body");
1✔
3462

3463
      tree_clear_flag(sub, TREE_F_NEVER_WAITS);
488✔
3464
      tree_set_flag(sub, TREE_F_HAS_WAIT);
488✔
3465
   }
3466

3467
   tree_t proc = find_enclosing(tab, S_PROCESS);
10,222✔
3468
   if (proc != NULL) {
10,222✔
3469
      // No wait statements allowed in process with sensitivity list
3470
      if (tree_triggers(proc) > 0)
9,768✔
3471
         sem_error(t, "wait statement not allowed in process with "
2✔
3472
                  "sensitvity list");
3473

3474
      tree_set_flag(proc, TREE_F_HAS_WAIT);
9,766✔
3475
   }
3476

3477
   return sem_check_sensitivity(t, tab);
10,220✔
3478
}
3479

3480
static bool sem_check_assert(tree_t t, nametab_t *tab)
18,036✔
3481
{
3482
   // Rules for asserion statements are in LRM 93 section 8.2
3483

3484
   tree_t value = tree_value(t);
18,036✔
3485
   if (!sem_check(value, tab))
18,036✔
3486
      return false;
3487

3488
   type_t std_bool = std_type(NULL, STD_BOOLEAN);
17,984✔
3489
   if (!sem_check_type(value, std_bool, tab))
17,984✔
3490
      sem_error(value, "type of assertion expression must be %pT but "
1✔
3491
                "is %pT", std_bool, tree_type(value));
3492

3493
   if (tree_has_message(t)) {
17,983✔
3494
      tree_t message = tree_message(t);
6,314✔
3495
      if (!sem_check(message, tab))
6,314✔
3496
         return false;
3497

3498
      type_t std_string = std_type(NULL, STD_STRING);
6,314✔
3499
      if (!sem_check_type(message, std_string, tab))
6,314✔
UNCOV
3500
         sem_error(message, "type of message be %pT but is %pT",
×
3501
                   std_string, tree_type(message));
3502
   }
3503

3504
   if (tree_has_severity(t)) {
17,983✔
3505
      tree_t severity = tree_severity(t);
5,871✔
3506
      if (!sem_check(severity, tab))
5,871✔
3507
         return false;
3508

3509
      type_t std_severity = std_type(NULL, STD_SEVERITY_LEVEL);
5,871✔
3510
      if (!sem_check_type(severity, std_severity, tab))
5,871✔
UNCOV
3511
         sem_error(severity, "type of severity must be %pT but is %pT",
×
3512
                   std_severity, tree_type(severity));
3513
   }
3514

3515
   return true;
3516
}
3517

3518
static bool sem_check_report(tree_t t, nametab_t *tab)
2,190✔
3519
{
3520
   tree_t message = tree_message(t);
2,190✔
3521
   if (!sem_check(message, tab))
2,190✔
3522
      return false;
3523

3524
   type_t std_string = std_type(NULL, STD_STRING);
2,180✔
3525
   if (!sem_check_type(message, std_string, tab))
2,180✔
3526
      sem_error(message, "type of message be %s but is %s",
1✔
3527
                type_pp(std_string), type_pp(tree_type(message)));
3528

3529
   if (tree_has_severity(t)) {
2,179✔
3530
      tree_t severity = tree_severity(t);
461✔
3531
      if (!sem_check(severity, tab))
461✔
3532
         return false;
3533

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

3540
   return true;
3541
}
3542

3543
static bool sem_check_string_literal(tree_t t)
26,038✔
3544
{
3545
   // String literals are in LRM 93 section 7.3.1
3546

3547
   type_t type = tree_type(t);
26,038✔
3548
   type_t elem = type_base_recur(type_elem(type));
26,038✔
3549

3550
   if (type_is_none(elem))
26,038✔
3551
      return false;
3552

3553
   const int nlits = type_enum_literals(elem);
26,038✔
3554
   const int nchars = tree_chars(t);
26,038✔
3555
   for (int i = 0; i < nchars; i++) {
387,229✔
3556
      tree_t ch = tree_char(t, i);
361,194✔
3557

3558
      ident_t ch_i = tree_ident(ch);
361,194✔
3559
      bool valid = false;
361,194✔
3560
      for (int j = 0; !valid && (j < nlits); j++) {
25,525,763✔
3561
         tree_t lit = type_enum_literal(elem, j);
25,164,569✔
3562
         if (ch_i == tree_ident(lit))
25,164,569✔
3563
            valid = true;
361,191✔
3564
      }
3565

3566
      if (!valid)
361,194✔
3567
         sem_error(t, "invalid character %s in string literal of type %s",
361,194✔
3568
                   istr(ch_i), type_pp(type));
3569
   }
3570

3571
   return true;
3572
}
3573

3574
static bool sem_check_literal(tree_t t)
96,345✔
3575
{
3576
   type_t type = tree_type(t);
96,345✔
3577
   if (type_is_none(type))
96,345✔
3578
      return false;
3✔
3579

3580
   return true;
3581
}
3582

3583
static bool sem_check_array_aggregate(tree_t t, nametab_t *tab)
7,187✔
3584
{
3585
   type_t composite_type = tree_type(t);
7,187✔
3586
   type_t base_type = type_base_recur(composite_type);
7,187✔
3587

3588
   type_t elem_type = NULL;
7,187✔
3589
   const int ndims = dimension_of(composite_type);
7,187✔
3590
   if (ndims == 1)
7,187✔
3591
      elem_type = type_elem(base_type);
6,829✔
3592
   else {
3593
      // Higher dimensions must be specified with a sub-aggregate or
3594
      // string literal
3595
      tree_t a0 = tree_value(tree_assoc(t, 0));
358✔
3596
      const tree_kind_t a0_kind = tree_kind(a0);
358✔
3597
      if (a0_kind != T_AGGREGATE && a0_kind != T_STRING)
358✔
3598
         sem_error(a0, "second dimension of %d dimensional array type %s must "
1✔
3599
                   "be specified by a sub-aggregate, string, or bit-string "
3600
                   "literal", ndims, type_pp(composite_type));
3601

3602
      // The parser will have constructed a type with ndims - 1
3603
      // dimensions.
3604
      elem_type = tree_type(tree_value(tree_assoc(t, 0)));
357✔
3605
   }
3606

3607
   type_t index_type = index_type_of(composite_type, 0);
7,186✔
3608

3609
   bool have_named = false;
7,186✔
3610
   bool have_pos = false;
7,186✔
3611

3612
   const int nassocs = tree_assocs(t);
7,186✔
3613
   for (int i = 0; i < nassocs; i++) {
41,795✔
3614
      tree_t a = tree_assoc(t, i);
34,620✔
3615

3616
      const assoc_kind_t akind = tree_subkind(a);
34,620✔
3617
      switch (akind) {
34,620✔
3618
      case A_RANGE:
674✔
3619
      case A_SLICE:
3620
         {
3621
            tree_t r = tree_range(a, 0);
674✔
3622
            if (!sem_check_discrete_range(r, index_type, tab))
674✔
3623
               return false;
3624

3625
            have_named = true;
3626
         }
3627
         break;
3628

3629
      case A_NAMED:
1,624✔
3630
         {
3631
            tree_t name = tree_name(a);
1,624✔
3632

3633
            if (!sem_check(name, tab))
1,624✔
3634
               return false;
3635

3636
            if (!sem_check_type(name, index_type, tab))
1,623✔
3637
               sem_error(name, "type of array aggregate choice %s does not "
1✔
3638
                         "match %s index type %s", type_pp(tree_type(name)),
3639
                         type_pp(composite_type), type_pp(index_type));
3640

3641
            have_named = true;
3642
         }
3643
         break;
3644

3645
      case A_POS:
29,568✔
3646
      case A_CONCAT:
3647
         have_pos = true;
29,568✔
3648
         break;
29,568✔
3649

3650
      case A_OTHERS:
3651
         break;
3652
      }
3653

3654
      tree_t value = tree_value(a);
34,615✔
3655

3656
      if (!sem_check(value, tab))
34,615✔
3657
         return false;
3658

3659
      if (!sem_check_type(value, elem_type, tab)) {
34,612✔
3660
         // LRM 08 section 9.3.3.3 allows the association to be of the
3661
         // base aggregate type as well
3662
         const bool allow_slice =
484✔
3663
            (akind == A_CONCAT || akind == A_SLICE)
242✔
3664
            || (ndims == 1 && standard() >= STD_08
242✔
3665
                && (akind == A_POS || akind == A_RANGE));
3✔
3666

3667
         if (allow_slice && !sem_check_type(value, composite_type, tab))
242✔
3668
            sem_error(value, "type of %s association %s does not match "
1✔
3669
                      "aggregate element type %s or the aggregate type "
3670
                      "itself %s", assoc_kind_str(akind),
3671
                      type_pp(tree_type(value)), type_pp(elem_type),
3672
                      type_pp(composite_type));
3673
         else if (!allow_slice)
241✔
3674
            sem_error(value, "type of %s association %s does not match "
2✔
3675
                      "aggregate element type %s", assoc_kind_str(akind),
3676
                      type_pp(tree_type(value)), type_pp(elem_type));
3677
         else
3678
            assert(akind == A_CONCAT || akind == A_SLICE);
239✔
3679
      }
3680
   }
3681

3682
   // Named and positional associations cannot be mixed in array
3683
   // aggregates
3684

3685
   if (have_named && have_pos)
7,175✔
3686
      sem_error(t, "named and positional associations cannot be "
2✔
3687
                "mixed in array aggregates");
3688

3689
   // If a choice is not locally static then it must be the only element
3690

3691
   if (have_named && nassocs > 1) {
7,173✔
3692
      for (int i = 0; i < nassocs; i++) {
2,345✔
3693
         tree_t a = tree_assoc(t, i);
1,852✔
3694
         tree_t choice = NULL;
1,852✔
3695
         switch (tree_subkind(a)) {
1,852✔
3696
         case A_NAMED: choice = tree_name(a); break;
1,466✔
3697
         case A_SLICE:
177✔
3698
         case A_RANGE: choice = tree_range(a, 0); break;
177✔
3699
         }
3700

3701
         if (choice && !sem_locally_static(choice))
1,643✔
3702
            sem_error(choice, "a choice that is not locally static is allowed"
1,852✔
3703
                      " only if the array aggregate contains a single element"
3704
                      " association");
3705
      }
3706
   }
3707

3708
   return true;
3709
}
3710

3711
static bool sem_check_record_aggregate(tree_t t, nametab_t *tab)
2,563✔
3712
{
3713
   // Checks for record aggregates are given in LRM 93 section 7.3.2.1
3714

3715
   type_t composite_type = tree_type(t);
2,563✔
3716
   type_t base_type = type_base_recur(composite_type);
2,563✔
3717

3718
   const int nfields = type_fields(base_type);
2,563✔
3719
   int pos = 0;
2,563✔
3720

3721
   LOCAL_BIT_MASK have;
5,126✔
3722
   mask_init(&have, nfields);
2,563✔
3723

3724
   const int nassocs = tree_assocs(t);
2,563✔
3725
   for (int i = 0; i < nassocs; i++) {
8,809✔
3726
      tree_t a = tree_assoc(t, i);
6,262✔
3727
      int f = -1;
6,262✔
3728

3729
      switch (tree_subkind(a)) {
6,262✔
3730
      case A_NAMED:
1,144✔
3731
         {
3732
            tree_t name = tree_name(a);
1,144✔
3733
            if (tree_kind(name) != T_REF)
1,144✔
3734
               sem_error(name, "association choice must be a field name");
4✔
3735
            else if (!tree_has_ref(name))
1,140✔
3736
               return false;   // Was parse error
3737

3738
            tree_t fdecl = tree_ref(name);
1,138✔
3739
            if (tree_kind(fdecl) != T_FIELD_DECL)
1,138✔
3740
               return false;   // Was parse error
3741

3742
            f = tree_pos(fdecl);
1,136✔
3743
         }
3744
         break;
1,136✔
3745

3746
      case A_POS:
4,992✔
3747
         {
3748
            if (pos >= nfields)
4,992✔
3749
               sem_error(a, "%d positional associations given but record type"
1✔
3750
                         " %s only has %d fields", pos + 1,
3751
                         type_pp(composite_type), nfields);
3752

3753
            f = pos++;
4,991✔
3754
         }
3755
         break;
4,991✔
3756

3757
      case A_OTHERS:
3758
         f = -1;
3759
         break;
3760

3761
      case A_RANGE:
2✔
3762
         {
3763
            diag_t *d = diag_new(DIAG_ERROR, tree_loc(tree_range(a, 0)));
2✔
3764
            diag_printf(d, "an element association with a choice that is "
2✔
3765
                        "a discrete range is only allowed in an array "
3766
                        "aggregate");
3767
            diag_lrm(d, STD_08, "9.3.3");
2✔
3768
            diag_emit(d);
2✔
3769
            return false;
2✔
3770
         }
3771

UNCOV
3772
      case A_SLICE:
×
3773
      case A_CONCAT:
3774
         should_not_reach_here();
3775
      }
3776

3777
      int nmatched = 0;
6,251✔
3778
      for (int j = 0; j < nfields; j++) {
29,241✔
3779
         if ((f != -1) && (f != j))
22,994✔
3780
            continue;
16,486✔
3781

3782
         tree_t field = type_field(base_type, j);
6,508✔
3783
         type_t field_type = tree_type(field);
6,508✔
3784

3785
         if (mask_test(&have, j)) {
6,508✔
3786
            if (f == -1)
42✔
3787
               continue;
40✔
3788

3789
            tree_t ak = NULL;
3790
            for (int k = 0; k < i; k++) {
2✔
3791
               ak = tree_assoc(t, k);
2✔
3792
               if (tree_subkind(ak) == A_POS && tree_pos(ak) == j)
2✔
3793
                  break;
3794
               else if (tree_pos(tree_ref(tree_name(ak))) == j)
1✔
3795
                  break;
3796
            }
3797
            assert(ak != NULL);
2✔
3798

3799
            diag_t *d = diag_new(DIAG_ERROR, tree_loc(a));
2✔
3800
            diag_printf(d, "field %s was already given a value by earlier "
2✔
3801
                        "%s choice", istr(tree_ident(field)),
3802
                        assoc_kind_str(tree_subkind(ak)));
2✔
3803
            diag_hint(d, tree_loc(ak), "first choice associated with field %s",
2✔
3804
                      istr(tree_ident(field)));
3805
            diag_hint(d, tree_loc(a), "duplicate choice here");
2✔
3806
            diag_emit(d);
2✔
3807
            return false;
2✔
3808
         }
3809

3810
         tree_t value = tree_value(a);
6,466✔
3811

3812
         if (!sem_check(value, tab))
6,466✔
3813
            return false;
3814

3815
         if (!sem_check_type(value, field_type, tab))
6,466✔
3816
            sem_error(value, "type of value %pT does not match type %pT"
2✔
3817
                      " of field %pI", tree_type(value), field_type,
3818
                      tree_ident(field));
3819

3820
         mask_set(&have, j);
6,464✔
3821
         nmatched++;
6,464✔
3822
      }
3823

3824
      if (f == -1 && nmatched == 0)
6,247✔
3825
         sem_error(a, "others association must represent at least one element");
6,247✔
3826
   }
3827

3828
   for (int i = 0; i < nfields; i++) {
9,001✔
3829
      if (!mask_test(&have, i)) {
6,456✔
3830
         tree_t field = type_field(base_type, i);
2✔
3831
         sem_error(t, "field %pI does not have a value", tree_ident(field));
6,456✔
3832
      }
3833
   }
3834

3835
   return true;
3836
}
3837

3838
static bool sem_check_aggregate(tree_t t, nametab_t *tab)
9,760✔
3839
{
3840
   // Rules for aggregates are in LRM 93 section 7.3.2
3841

3842
   type_t composite_type = tree_type(t);
9,760✔
3843

3844
   if (type_is_none(composite_type))
9,760✔
3845
      return false;
3846
   assert(type_is_composite(composite_type));
9,753✔
3847

3848
   // All positional associations must appear before named associations
3849
   // and those must appear before any others association
3850

3851
   enum { POS, NAMED, OTHERS } state = POS;
9,753✔
3852

3853
   const int nassocs = tree_assocs(t);
9,753✔
3854
   for (int i = 0; i < nassocs; i++) {
50,647✔
3855
      tree_t a = tree_assoc(t, i);
40,897✔
3856

3857
      switch (tree_subkind(a)) {
40,897✔
3858
      case A_POS:
34,563✔
3859
      case A_CONCAT:
3860
         if (state > POS)
34,563✔
3861
            sem_error(a, "positional associations must appear "
1✔
3862
                      "first in aggregate");
3863
         break;
3864

3865
      case A_NAMED:
3,452✔
3866
      case A_RANGE:
3867
      case A_SLICE:
3868
         if (state > NAMED)
3,452✔
3869
            sem_error(a, "named association must not follow "
1✔
3870
                      "others association in aggregate");
3871
         state = NAMED;
3872
         break;
3873

3874
      case A_OTHERS:
2,882✔
3875
         if (state == OTHERS)
2,882✔
3876
            sem_error(a, "only a single others association "
1✔
3877
                      "allowed in aggregate");
3878
         state = OTHERS;
3879
         break;
3880
      }
3881
   }
3882

3883
   if (type_is_array(composite_type))
9,750✔
3884
      return sem_check_array_aggregate(t, tab);
7,187✔
3885
   else
3886
      return sem_check_record_aggregate(t, tab);
2,563✔
3887
}
3888

3889
static bool sem_check_ref(tree_t t, nametab_t *tab)
194,262✔
3890
{
3891
   if (!tree_has_ref(t))
194,262✔
3892
      return false;
3893

3894
   type_t type = get_type_or_null(t);
194,210✔
3895
   if (type != NULL && type_is_none(type))
194,210✔
3896
      return false;
3897

3898
   tree_t decl = tree_ref(t);
194,200✔
3899
   const tree_kind_t kind = tree_kind(decl);
194,200✔
3900

3901
   switch (kind) {
194,200✔
3902
   case T_PORT_DECL:
3903
   case T_VAR_DECL:
3904
   case T_SIGNAL_DECL:
3905
   case T_FILE_DECL:
3906
   case T_ENUM_LIT:
3907
   case T_UNIT_DECL:
3908
   case T_FUNC_DECL:
3909
   case T_FUNC_BODY:
3910
   case T_FUNC_INST:
3911
   case T_PROC_DECL:
3912
   case T_PROC_BODY:
3913
   case T_PROC_INST:
3914
   case T_IMPLICIT_SIGNAL:
3915
   case T_PARAM_DECL:
3916
      break;
3917

3918
   case T_CONST_DECL:
20,680✔
3919
      if (!tree_has_value(decl) && is_same_region(tab, decl)) {
20,680✔
3920
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
1✔
3921
         diag_printf(d, "cannot reference deferred constant %s before the "
1✔
3922
                     "elaboration of the corresponding full declaration",
3923
                     istr(tree_ident(decl)));
3924
         diag_hint(d, tree_loc(decl), "%s declared here",
1✔
3925
                   istr(tree_ident(decl)));
3926
         diag_lrm(d, STD_08, "4.8");
1✔
3927
         diag_emit(d);
1✔
3928
         return false;
1✔
3929
      }
3930
      break;
3931

3932
   case T_ALIAS:
2,309✔
3933
      {
3934
         switch (class_of(decl)) {
2,309✔
3935
         case C_VARIABLE:
3936
         case C_SIGNAL:
3937
         case C_CONSTANT:
3938
         case C_LITERAL:
3939
            break;
3940

3941
         case C_DEFAULT:
3942
            return false;   // Must have been an earlier parse error
3943

3944
         default:
1✔
3945
            sem_error(t, "invalid use of alias %s", istr(tree_ident(decl)));
1✔
3946
         }
3947
      }
3948
      break;
3949

3950
   case T_GENERIC_DECL:
6,121✔
3951
      if (tree_class(decl) == C_CONSTANT)
6,121✔
3952
         break;
3953
      // Fall-through
3954

3955
   default:
3956
      {
3957
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
4✔
3958
         diag_printf(d, "invalid use of %s %s", class_str(class_of(decl)),
4✔
3959
                     istr(tree_ident(t)));
3960
         diag_hint(d, tree_loc(decl), "%s declared here",
4✔
3961
                   istr(tree_ident(decl)));
3962
         diag_emit(d);
4✔
3963
         return false;
4✔
3964
      }
3965
   }
3966

3967
   tree_t sub = find_enclosing(tab, S_SUBPROGRAM);
194,194✔
3968
   if (sub != NULL) {
194,194✔
3969
      if (kind == T_FILE_DECL)
107,387✔
3970
         tree_set_flag(sub, TREE_F_IMPURE_FILE);
350✔
3971
      else if (kind == T_VAR_DECL && (tree_flags(decl) & TREE_F_SHARED))
107,037✔
3972
         tree_set_flag(sub, TREE_F_IMPURE_SHARED);
64✔
3973

3974
      const bool is_pure_func =
214,774✔
3975
         tree_kind(sub) == T_FUNC_BODY && !(tree_flags(sub) & TREE_F_IMPURE);
107,387✔
3976

3977
      if (is_pure_func) {
107,387✔
3978
         const class_t class = class_of(decl);
59,748✔
3979
         if (class == C_VARIABLE || class == C_SIGNAL || class == C_FILE) {
59,748✔
3980
            tree_t container = get_container(tab, decl);
16,797✔
3981
            if (container != NULL && container != sub) {
16,797✔
3982
               diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
2✔
3983
               diag_printf(d, "cannot reference %s %s in pure function %s",
2✔
3984
                           class_str(class), istr(tree_ident(decl)),
3985
                           istr(tree_ident(sub)));
3986
               diag_lrm(d, STD_08, "4.3");
2✔
3987
               diag_emit(d);
2✔
3988
               return false;
2✔
3989
            }
3990
         }
3991
      }
3992
   }
3993

3994
   return true;
3995
}
3996

3997
static bool sem_check_name_prefix(tree_t t, nametab_t *tab, const char *what)
31,872✔
3998
{
3999
   // The prefix of a name may only be function call or another name
4000
   switch (tree_kind(t)) {
31,872✔
4001
   case T_FCALL:
4002
   case T_PROT_FCALL:
4003
   case T_REF:
4004
   case T_ATTR_REF:
4005
   case T_ALL:
4006
   case T_ARRAY_REF:
4007
   case T_ARRAY_SLICE:
4008
   case T_RECORD_REF:
4009
   case T_EXTERNAL_NAME:
4010
      break;
4011

4012
   default:
2✔
4013
      {
4014
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
2✔
4015
         diag_printf(d, "the prefix of %s must be a name or a "
2✔
4016
                     "function call", what);
4017
         diag_lrm(d, STD_08, "8.1");
2✔
4018
         diag_emit(d);
2✔
4019
         return false;
2✔
4020
      }
4021
   }
4022

4023
   return true;
4024
}
4025

4026
static bool sem_check_record_ref(tree_t t, nametab_t *tab)
9,225✔
4027
{
4028
   tree_t value = tree_value(t);
9,225✔
4029
   if (!sem_check(value, tab))
9,225✔
4030
      return false;
4031

4032
   if (!tree_has_ref(t))
9,219✔
4033
      return false;
4034

4035
   if (!sem_check_name_prefix(value, tab, "a selected name"))
9,213✔
UNCOV
4036
      return false;
×
4037

4038
   return true;
4039
}
4040

4041
static bool sem_check_array_ref(tree_t t, nametab_t *tab)
16,474✔
4042
{
4043
   tree_t value = tree_value(t);
16,474✔
4044
   if (!sem_check(value, tab))
16,474✔
4045
      return false;
4046

4047
   if (!sem_check_name_prefix(value, tab, "an indexed name"))
16,468✔
4048
      return false;
4049

4050
   type_t type = tree_type(value);
16,467✔
4051

4052
   if (!type_is_array(type))
16,467✔
4053
      return false;  // Checked earlier
4054

4055
   const int nindex  = dimension_of(type);
16,462✔
4056
   const int nparams = tree_params(t);
16,462✔
4057

4058
   if (nparams != nindex)
16,462✔
4059
      sem_error(t, "prefix of indexed name has %d dimensions but %d "
3✔
4060
                "indices given", nindex, nparams);
4061

4062
   bool ok = true;
4063
   for (int i = 0; i < nparams; i++) {
34,681✔
4064
      tree_t p = tree_param(t, i);
18,224✔
4065
      assert(tree_subkind(p) == P_POS);
18,224✔
4066

4067
      type_t expect = index_type_of(type, i);
18,224✔
4068
      tree_t value = tree_value(p);
18,224✔
4069

4070
      ok = sem_check(value, tab) && ok;
18,224✔
4071

4072
      if (ok && !sem_check_type(value, expect, tab))
18,224✔
4073
         sem_error(value, "type of index %s does not match type of "
18,224✔
4074
                   "array dimension %s",
4075
                   type_pp(tree_type(value)),
4076
                   type_pp(expect));
4077
   }
4078

4079
   return ok;
4080
}
4081

4082
static bool sem_check_array_slice(tree_t t, nametab_t *tab)
2,406✔
4083
{
4084
   tree_t value = tree_value(t);
2,406✔
4085
   if (!sem_check(value, tab))
2,406✔
4086
      return false;
4087

4088
   if (!sem_check_name_prefix(value, tab, "a slice name"))
2,405✔
4089
      return false;
4090

4091
   type_t array_type = tree_type(value);
2,404✔
4092

4093
   if (type_is_none(array_type))
2,404✔
4094
      return false;
4095
   else if (!sem_check_incomplete(t, array_type))
2,404✔
4096
      return false;
4097
   else if (!type_is_array(array_type))
2,403✔
4098
      sem_error(t, "type of slice prefix %s is not an array",
5✔
4099
                type_pp(array_type));
4100

4101
   tree_t r = tree_range(t, 0);
2,398✔
4102
   if (!sem_check_discrete_range(r, index_type_of(array_type, 0), tab))
2,398✔
4103
      return false;
4104

4105
   const bool unconstrained = type_is_unconstrained(array_type);
2,388✔
4106
   const range_kind_t prefix_dir =
4,776✔
4107
      unconstrained ? RANGE_EXPR : direction_of(array_type, 0);
2,388✔
4108

4109
   const range_kind_t rkind = tree_subkind(r);
2,388✔
4110
   const bool wrong_dir =
4,776✔
4111
      !unconstrained
2,388✔
4112
      && rkind != prefix_dir
2,388✔
4113
      && (rkind == RANGE_TO || rkind == RANGE_DOWNTO)
155✔
4114
      && (prefix_dir == RANGE_TO || prefix_dir == RANGE_DOWNTO);
2,388✔
4115

4116
   if (wrong_dir) {
2,388✔
4117
      const char *text[] = { "TO", "DOWNTO", "?", "??", "???" };
2✔
4118
      sem_error(t, "range direction of slice %s does not match prefix %s",
2✔
4119
                text[rkind], text[prefix_dir]);
4120
   }
4121

4122
   return true;
4123
}
4124

4125
static bool sem_check_signal_attr(tree_t t)
1,125✔
4126
{
4127
   tree_t name = tree_name(t);
1,153✔
4128

4129
   if (tree_kind(name) == T_ATTR_REF)
1,153✔
4130
      return sem_check_signal_attr(name);
4131

4132
   tree_t ref = name_to_ref(name);
1,125✔
4133
   if (ref != NULL && class_of(ref) == C_SIGNAL)
1,125✔
4134
      return true;
4135

4136
   sem_error(t, "prefix of attribute %s must denote a signal",
2✔
4137
             istr(tree_ident(t)));
4138
}
4139

4140
static bool sem_check_driving(tree_t t)
69✔
4141
{
4142
   // See LRM 08 section 16.2.4 for special rules about 'DRIVING and
4143
   // 'DRIVING_VALUE
4144

4145
   if (!sem_check_signal_attr(t))
69✔
4146
      return false;
4147

4148
   tree_t ref = name_to_ref(tree_name(t));
68✔
4149
   if (ref == NULL || !tree_has_ref(ref))
68✔
UNCOV
4150
      return false;
×
4151

4152
   tree_t decl = tree_ref(ref);
68✔
4153
   if (tree_kind(decl) == T_PORT_DECL) {
68✔
4154
      const port_mode_t mode = tree_subkind(decl);
13✔
4155
      if (mode != PORT_OUT && mode != PORT_INOUT && mode != PORT_BUFFER)
13✔
4156
         sem_error(t, "prefix of attribute %s must denote a signal or a port "
1✔
4157
                   "with mode IN, INOUT, or BUFFER", istr(tree_ident(t)));
4158
   }
4159

4160
   // TODO: check within a process
4161

4162
   return true;
4163
}
4164

4165
static bool sem_check_attr_param(tree_t t, type_t expect, int min, int max,
1,527✔
4166
                                 nametab_t *tab)
4167
{
4168
   const int nparams = tree_params(t);
1,527✔
4169
   if (nparams == 0 && min > 0)
1,527✔
UNCOV
4170
      sem_error(t, "attribute %s requires a parameter", istr(tree_ident(t)));
×
4171
   else if (nparams > max)
1,527✔
UNCOV
4172
      sem_error(t, "too many parameters for attribute %s", istr(tree_ident(t)));
×
4173
   else if (nparams == 1) {
1,527✔
4174
      tree_t dim = tree_value(tree_param(t, 0));
1,415✔
4175
      if (!sem_check(dim, tab))
1,415✔
4176
         return false;
4177

4178
      tree_t value = tree_value(tree_param(t, 0));
1,411✔
4179
      if (!sem_check_type(value, expect, tab))
1,411✔
UNCOV
4180
         sem_error(t, "expected type %s for attribute %s parameter but "
×
4181
                   "have %s", type_pp(expect), istr(tree_ident(t)),
4182
                   type_pp(tree_type(value)));
4183
   }
4184

4185
   return true;
4186
}
4187

4188
static bool sem_check_dimension_attr(tree_t t, nametab_t *tab)
23,314✔
4189
{
4190
   const int nparams = tree_params(t);
23,314✔
4191
   if (nparams == 0)
23,314✔
4192
      return true;
4193

4194
   assert(nparams == 1);   // Enforced by parser
1,684✔
4195

4196
   tree_t dim = tree_value(tree_param(t, 0));
1,684✔
4197
   if (!sem_check(dim, tab))
1,684✔
4198
      return false;
4199

4200
   // The parameter must be a locally static expression of type
4201
   // universal_integer
4202

4203
   type_t uint = std_type(NULL, STD_UNIVERSAL_INTEGER);
1,683✔
4204
   type_t dimtype = tree_type(dim);
1,683✔
4205
   if (!type_eq(dimtype, uint)) {
1,683✔
4206
      diag_t *d;
4✔
4207
      if (type_is_integer(dimtype))
4✔
4208
         d = pedantic_diag(tree_loc(dim));
4✔
4209
      else
UNCOV
4210
         d = diag_new(DIAG_ERROR, tree_loc(dim));
×
4211

4212
      if (d != NULL) {
4✔
4213
         diag_printf(d, "dimension parameter of attribute %s must be a locally "
4✔
4214
                     "static expression of type universal_integer",
4215
                     istr(tree_ident(t)));
4216
         diag_hint(d, tree_loc(dim), "expression has type %s",
4✔
4217
                   type_pp(tree_type(dim)));
4218
         diag_emit(d);
4✔
4219
         return false;
4✔
4220
      }
4221
   }
4222

4223
   if (!sem_locally_static(dim))
1,679✔
UNCOV
4224
      sem_error(dim, "dimension parameter of attribute %s must be a locally "
×
4225
                "static expression", istr(tree_ident(t)));
4226

4227
   if (!type_is_array(tree_type(tree_name(t))))
1,679✔
UNCOV
4228
      sem_error(t, "prefix of attribute %s with dimension is not an array",
×
4229
                istr(tree_ident(t)));
4230

4231
   return true;
4232
}
4233

4234
static bool sem_is_named_entity(tree_t t)
821✔
4235
{
4236
   const tree_kind_t kind = tree_kind(t);
821✔
4237
   if (kind != T_REF)
821✔
4238
      return false;
4239

4240
   tree_t decl = tree_ref(t);
821✔
4241

4242
   switch (tree_kind(decl)) {
821✔
4243
   case T_SIGNAL_DECL:  case T_VAR_DECL:       case T_PORT_DECL:
4244
   case T_ALIAS:        case T_ENTITY:         case T_ARCH:
4245
   case T_PACKAGE:      case T_PACK_BODY:      case T_BLOCK:
4246
   case T_FILE_DECL:    case T_CONST_DECL:     case T_FUNC_DECL:
4247
   case T_FUNC_BODY:    case T_PROC_DECL:      case T_PROC_BODY:
4248
   case T_PROCESS:      case T_GENERIC_DECL:   case T_PARAM_DECL:
4249
   case T_INSTANCE:     case T_PROT_DECL:      case T_PROT_BODY:
4250
   case T_TYPE_DECL:    case T_SUBTYPE_DECL:   case T_FOR_GENERATE:
4251
   case T_IF_GENERATE:  case T_CASE_GENERATE:  case T_COMPONENT:
4252
      return true;
UNCOV
4253
   case T_IMPLICIT_SIGNAL:
×
4254
      return tree_subkind(decl) == IMPLICIT_GUARD;   // See LRM 93 section 4.3
×
4255
   default:
1✔
4256
      return false;
1✔
4257
   }
4258
}
4259

4260
static bool sem_check_attr_prefix(tree_t t, bool allow_range, nametab_t *tab,
28,412✔
4261
                                  type_t *named_type)
4262
{
4263
   switch (tree_kind(t)) {
28,412✔
4264
   case T_REF:
26,500✔
4265
      {
4266
         if (!tree_has_ref(t))
26,500✔
4267
            return false;
4268

4269
         tree_t decl = tree_ref(t), type_decl;
26,495✔
4270
         if ((type_decl = aliased_type_decl(decl)) != NULL)
26,495✔
4271
            *named_type = tree_type(type_decl);
4,940✔
4272

4273
         return true;
4274
      }
4275

4276
   case T_ATTR_REF:
144✔
4277
      {
4278
         if (is_type_attribute(tree_subkind(t)))
144✔
4279
            *named_type = tree_type(t);
65✔
4280
         else {
4281
            if (!sem_check_attr_ref(t, allow_range, tab))
79✔
UNCOV
4282
               return false;
×
4283
         }
4284

4285
         return true;
4286
      }
4287

4288
   case T_RECORD_REF:
1,193✔
4289
      {
4290
         if (!tree_has_ref(t))
1,193✔
4291
            return false;
4292

4293
         tree_t value = tree_value(t);
1,193✔
4294
         if (!sem_check_attr_prefix(value, false, tab, named_type))
1,193✔
4295
            return false;
4296

4297
         if (!sem_check_name_prefix(t, tab, "a selected name"))
1,193✔
4298
            return false;
4299

4300
         if (*named_type != NULL)
1,193✔
4301
            *named_type = tree_type(tree_ref(t));
5✔
4302

4303
         return true;
4304
      }
4305

4306
   default:
575✔
4307
      return sem_check(t, tab);
575✔
4308
   }
4309
}
4310

4311
static bool sem_check_attr_ref(tree_t t, bool allow_range, nametab_t *tab)
27,219✔
4312
{
4313
   // Attribute names are in LRM 93 section 6.6
4314

4315
   tree_t name = tree_name(t);
27,219✔
4316
   type_t named_type = NULL;
27,219✔
4317

4318
   ident_t attr = tree_ident(t);
27,219✔
4319
   const attr_kind_t predef = tree_subkind(t);
27,219✔
4320

4321
   const bool prefix_can_be_range =
27,219✔
4322
      predef == ATTR_LOW || predef == ATTR_HIGH || predef == ATTR_LEFT
4323
      || predef == ATTR_RIGHT || predef == ATTR_ASCENDING
27,219✔
4324
      || predef == ATTR_VALUE;
27,219✔
4325

4326
   if (!sem_check_attr_prefix(name, prefix_can_be_range, tab, &named_type))
27,219✔
4327
      return false;
4328

4329
   if (predef == ATTR_INSTANCE_NAME)
27,212✔
4330
      tree_set_global_flags(t, TREE_GF_INSTANCE_NAME);
515✔
4331
   else if (predef == ATTR_PATH_NAME)
26,697✔
4332
      tree_set_global_flags(t, TREE_GF_PATH_NAME);
229✔
4333

4334
   switch (predef) {
27,212✔
4335
   case ATTR_RANGE:
5,160✔
4336
   case ATTR_REVERSE_RANGE:
4337
      {
4338
         type_t type = tree_type(name);
5,160✔
4339
         if (type_is_none(type))
5,160✔
4340
            return false;
4341
         else if (!allow_range)
5,160✔
UNCOV
4342
            sem_error(t, "range expression not allowed here");
×
4343

4344
         if (!sem_check_dimension_attr(t, tab))
5,160✔
4345
            return false;
4346

4347
         if (named_type != NULL) {
5,160✔
4348
            // Range attribute of type
4349
            if (named_type != NULL && type_is_unconstrained(type))
1,138✔
4350
               sem_error(t, "cannot use attribute %s with unconstrained array "
1✔
4351
                         "type %s", istr(attr), type_pp(type));
4352
         }
4353
         else if (!type_is_array(type)) {
4,022✔
4354
            diag_t *d = diag_new(DIAG_ERROR, tree_loc(name));
3✔
4355
            diag_printf(d, "object prefix of attribute %s must be an array",
3✔
4356
                        istr(attr));
4357
            diag_hint(d, tree_loc(name), "prefix has type %s", type_pp(type));
3✔
4358
            diag_emit(d);
3✔
4359
            return false;
3✔
4360
         }
4361

4362
         return true;
4363
      }
4364

4365
   case ATTR_LENGTH:
8,175✔
4366
      {
4367
         type_t type = get_type_or_null(name);
8,175✔
4368
         if (type == NULL)
8,175✔
UNCOV
4369
            sem_error(name, "prefix does not have LENGTH attribute");
×
4370
         else if (type_is_none(type))
8,175✔
4371
            return false;
4372
         else if (!sem_check_incomplete(t, type))
8,174✔
4373
            return false;
4374
         else if (!type_is_array(type)
8,173✔
4375
                  && !(standard() >= STD_19 && type_is_discrete(type)))
14✔
4376
            sem_error(name, "prefix of attribute LENGTH must be an array%s "
1✔
4377
                      "but have type %s",
4378
                      standard() >= STD_19 ? " or a discrete type" : "",
4379
                      type_pp(type));
4380

4381
         if (!sem_check_dimension_attr(t, tab))
8,172✔
4382
            return false;
3✔
4383

4384
         return true;
4385
      }
4386

4387
   case ATTR_LEFT:
9,983✔
4388
   case ATTR_RIGHT:
4389
   case ATTR_LOW:
4390
   case ATTR_HIGH:
4391
   case ATTR_ASCENDING:
4392
      {
4393
         type_t type = tree_type(name);
9,983✔
4394

4395
         if (type_is_none(type))
9,983✔
4396
            return false;
4397
         else if (!sem_check_incomplete(t, type))
9,983✔
4398
            return false;
4399
         else if (!sem_check_dimension_attr(t, tab))
9,982✔
4400
            return false;
4401

4402
         if (type_is_scalar(type)) {
9,980✔
4403
            bool prefix_range = false;
2,123✔
4404
            if (tree_kind(name) == T_ATTR_REF) {
2,123✔
4405
               const attr_kind_t prefix_attr = tree_subkind(name);
25✔
4406
               prefix_range = (prefix_attr == ATTR_RANGE
25✔
4407
                               || prefix_attr == ATTR_REVERSE_RANGE);
25✔
4408
            }
4409

4410
            if (named_type == NULL && !prefix_range && standard() < STD_19) {
2,123✔
4411
               diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
1✔
4412
               diag_printf(d, "attribute %s cannot be used with scalar objects "
1✔
4413
                           "in VHDL-%s", istr(attr), standard_text(standard()));
4414
               diag_hint(d, NULL, "pass $bold$--std=2019$$ to enable this "
1✔
4415
                         "feature");
4416
               diag_emit(d);
1✔
4417
               return false;
1✔
4418
            }
4419
         }
4420
         else if (!type_is_array(type))
7,857✔
4421
            sem_error(t, "prefix does not have attribute %s", istr(attr));
1✔
4422

4423
         return true;
4424
      }
4425

4426
   case ATTR_LAST_EVENT:
114✔
4427
   case ATTR_LAST_ACTIVE:
4428
      if (!sem_check_readable(name))
114✔
4429
         return false;
4430
      else if (!sem_check_attr_param(t, NULL, 0, 0, tab))
112✔
4431
         return false;
4432
      else if (!sem_check_signal_attr(t))
112✔
4433
         return false;
1✔
4434

4435
      return true;
4436

4437
   case ATTR_EVENT:
670✔
4438
   case ATTR_ACTIVE:
4439
   case ATTR_LAST_VALUE:
4440
      if (!sem_check_readable(name))
670✔
4441
         return false;
4442
      else if (!sem_check_signal_attr(t))
668✔
UNCOV
4443
         return false;
×
4444

4445
      return true;
4446

4447
   case ATTR_PATH_NAME:
821✔
4448
   case ATTR_INSTANCE_NAME:
4449
   case ATTR_SIMPLE_NAME:
4450
      if (!sem_is_named_entity(name))
821✔
4451
         sem_error(t, "prefix of %s attribute must be a named entity",
1✔
4452
                   istr(attr));
4453

4454
      tree_set_flag(name, TREE_F_ATTR_PREFIX);
820✔
4455
      return true;
820✔
4456

4457
   case ATTR_QUIET:
246✔
4458
   case ATTR_STABLE:
4459
   case ATTR_DELAYED:
4460
      {
4461
         if (!sem_check_readable(name))
246✔
4462
            return false;
4463
         else if (!sem_check_signal_attr(t))
244✔
4464
            return false;
4465

4466
         if (tree_params(t) > 0) {
244✔
4467
            tree_t value = tree_value(tree_param(t, 0));
88✔
4468

4469
            if (!sem_check(value, tab))
88✔
4470
               return false;
4471

4472
            type_t std_time = std_type(NULL, STD_TIME);
86✔
4473
            if (!sem_check_type(value, std_time, tab))
86✔
4474
               sem_error(value, "parameter of attribute %s must have type %s",
1✔
4475
                         istr(attr), type_pp(std_time));
4476
            else if (!sem_globally_static(value))
85✔
4477
               sem_error(value, "parameter of attribute %s must be a static "
1✔
4478
                         "expression", istr(attr));
4479
         }
4480

4481
         return true;
4482
      }
4483

4484
   case ATTR_TRANSACTION:
33✔
4485
      if (!sem_check_readable(name))
33✔
4486
         return false;
4487
      else if (!sem_check_signal_attr(t))
32✔
UNCOV
4488
         return false;
×
4489

4490
      return true;
4491

4492
   case ATTR_DRIVING_VALUE:
69✔
4493
   case ATTR_DRIVING:
4494
      return sem_check_driving(t);
69✔
4495

4496
   case ATTR_IMAGE:
1,254✔
4497
   case ATTR_VALUE:
4498
      {
4499
         const bool std_2019 = standard() >= STD_19;
1,254✔
4500

4501
         // VHDL 2019: 'range'value returns a range record
4502
         if (predef == ATTR_VALUE && std_2019
1,254✔
4503
             && tree_kind(name) == T_ATTR_REF
60✔
4504
             && tree_subkind(name) == ATTR_RANGE) {
27✔
4505
            type_t range_type = tree_type(name);
27✔
4506
            if (range_type == NULL || type_is_none(range_type))
27✔
NEW
4507
               return false;
×
4508
            else if (!type_is_scalar(range_type))
27✔
NEW
4509
               sem_error(t, "prefix of 'RANGE'VALUE must be a range "
×
4510
                         "of a scalar type but have %s",
4511
                         type_pp(range_type));
4512
            return true;
4513
         }
4514

4515
         if (named_type == NULL && std_2019 && tree_params(t) == 0) {
1,227✔
4516
            // LCS2016-18 allows attribute with object prefix
4517
            named_type = get_type_or_null(name);
37✔
4518
            add_param(t, name, P_POS, NULL);
37✔
4519
         }
4520

4521
         if (named_type == NULL)
1,227✔
UNCOV
4522
            sem_error(t, "prefix of attribute %s must be a type", istr(attr));
×
4523
         if (!std_2019 && !type_is_scalar(named_type))
1,227✔
4524
            sem_error(t, "cannot use attribute %s with non-scalar type %s",
2✔
4525
                      istr(attr), type_pp(named_type));
4526
         else if (std_2019 && !type_is_representable(named_type))
1,225✔
4527
            sem_error(t, "cannot use attribute %s with non-representable "
1✔
4528
                      "type %s", istr(attr), type_pp(named_type));
4529

4530
         type_t std_string = std_type(NULL, STD_STRING);
1,224✔
4531
         type_t arg_type = predef == ATTR_IMAGE ? named_type : std_string;
1,224✔
4532
         if (!sem_check_attr_param(t, arg_type, 1, 1, tab))
1,224✔
4533
            return false;
4✔
4534

4535
         return true;
4536
      }
4537

4538
   case ATTR_LEFTOF:
301✔
4539
   case ATTR_RIGHTOF:
4540
   case ATTR_PRED:
4541
   case ATTR_SUCC:
4542
   case ATTR_POS:
4543
   case ATTR_VAL:
4544
      {
4545
         if (named_type == NULL && standard() >= STD_19)
301✔
4546
            named_type = get_type_or_null(name);   // LCS2016-08 relaxation
3✔
4547

4548
         if (named_type == NULL)
301✔
UNCOV
4549
            sem_error(t, "prefix of attribute %s must be a type", istr(attr));
×
4550

4551
         type_t name_type = tree_type(name);
301✔
4552

4553
         if (!type_is_discrete(name_type) && !type_is_physical(name_type))
301✔
UNCOV
4554
            sem_error(t, "prefix of attribute %s must be a discrete or "
×
4555
                      "physical type", istr(attr));
4556

4557
         if (predef == ATTR_VAL) {
301✔
4558
            // Parameter may be any integer type
4559
            if (tree_params(t) != 1)
110✔
4560
               sem_error(t, "attribute VAL requires a parameter");
1✔
4561

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

4570
         return true;
4571
      }
4572

4573
   case ATTR_BASE:
1✔
4574
      sem_error(t, "BASE attribute is allowed only as the prefix of the name "
1✔
4575
                "of another attribute");
4576

4577
   case ATTR_ELEMENT:
1✔
4578
   case ATTR_SUBTYPE:
4579
   case ATTR_INDEX:
4580
   case ATTR_RECORD:
4581
      sem_error(t, "%s attribute is only allowed in a type mark", istr(attr));
1✔
4582

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

4588
      return true;
4589

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

4598
      return true;
4599

4600
   case ATTR_USER:
238✔
4601
      if (!tree_has_value(t))
238✔
4602
         return false;
4603

4604
      if (!sem_static_name(name, sem_globally_static)) {
236✔
UNCOV
4605
         if (tree_kind(name) == T_REF)
×
UNCOV
4606
            sem_error(name, "%s is not a static name", istr(tree_ident(name)));
×
4607
         else
UNCOV
4608
            sem_error(name, "invalid attribute reference");
×
4609
      }
4610

4611
      return true;
4612

UNCOV
4613
   default:
×
4614
      fatal_trace("unhandled attribute kind %d", predef);
4615
   }
4616
}
4617

4618
static bool sem_check_qualified(tree_t t, nametab_t *tab)
4,074✔
4619
{
4620
   if (tree_has_value(t)) {
4,074✔
4621
      tree_t value = tree_value(t);
3,749✔
4622

4623
      if (!sem_check(value, tab))
3,749✔
4624
         return false;
4625

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

4641
   return true;
4642
}
4643

4644
static bool sem_static_signal_name(tree_t t)
1,585✔
4645
{
4646
   if (!sem_static_name(t, sem_globally_static))
1,585✔
4647
      return false;
4648

4649
   return class_of(t) == C_SIGNAL;
1,583✔
4650
}
4651

4652
static bool sem_check_port_actual(formal_map_t *formals, int nformals,
3,942✔
4653
                                  tree_t param, tree_t unit, int pos,
4654
                                  nametab_t *tab)
4655
{
4656
   tree_t value = tree_value(param), name = NULL, out_conv = NULL;
3,942✔
4657
   tree_t decl = NULL;
3,942✔
4658
   type_t type = NULL;
3,942✔
4659

4660
   switch (tree_subkind(param)) {
3,942✔
4661
   case P_POS:
1,519✔
4662
      {
4663
         const int pos = tree_pos(param);
1,519✔
4664
         if (pos >= nformals)
1,519✔
4665
            sem_error(value, "found at least %d positional actuals but %s "
4✔
4666
                      "has only %d port%s", pos + 1, istr(tree_ident(unit)),
4667
                      nformals, nformals == 1 ? "" : "s");
4668
         if (formals[pos].state >= MAP_PARTIAL)
1,517✔
UNCOV
4669
            sem_error(value, "formal port %s already has an actual",
×
4670
                      istr(tree_ident(formals[pos].decl)));
4671
         formals[pos].state = MAP_FULL;
1,517✔
4672
         decl = formals[pos].decl;
1,517✔
4673
         type = tree_type(decl);
1,517✔
4674
      }
4675
      break;
1,517✔
4676

4677
   case P_NAMED:
2,423✔
4678
      {
4679
         tree_t ref = (name = tree_name(param));
2,423✔
4680

4681
         switch (tree_kind(name)) {
2,423✔
4682
         case T_FCALL:
1✔
4683
            if (tree_params(name) != 1)
1✔
UNCOV
4684
               sem_error(name, "output conversion function must have "
×
4685
                         "exactly one parameter");
4686

4687
            // The parser would have replaced any other valid conversion
4688
            // function with T_CONV_FUNC
4689
            sem_error(name, "invalid output conversion %s",
1✔
4690
                      istr(tree_ident(name)));
4691
            break;
4692

4693
         case T_CONV_FUNC:
96✔
4694
         case T_TYPE_CONV:
4695
            out_conv = name;
96✔
4696
            name = ref = tree_value(name);
96✔
4697
            break;
96✔
4698

4699
         default:
4700
            break;
4701
         }
4702

4703
         ref = name_to_ref(ref);
2,422✔
4704
         assert(ref != NULL && tree_kind(ref) == T_REF);
2,422✔
4705

4706
         if (!tree_has_ref(ref))
2,422✔
4707
            return false;
4708

4709
         tree_t d = tree_ref(ref);
2,422✔
4710
         for (int i = 0; i < nformals; i++) {
9,310✔
4711
            if (formals[i].decl != d)
9,308✔
4712
               continue;
6,888✔
4713
            else if (formals[i].state == MAP_FULL)
2,420✔
4714
               sem_error(value, "formal port %s already has an actual",
1✔
4715
                         istr(tree_ident(formals[i].decl)));
4716
            else if (formals[i].state == MAP_PARTIAL
2,419✔
4717
                     && formals[i].last_pos != pos - 1) {
423✔
4718
               diag_t *d = diag_new(DIAG_ERROR, tree_loc(name));
1✔
4719
               diag_printf(d, "associations for formal port %s must appear in "
1✔
4720
                           "a contiguous sequence",
4721
                           istr(tree_ident(formals[i].decl)));
4722
               diag_lrm(d, STD_08, "6.5.7");
1✔
4723
               diag_emit(d);
1✔
4724
               return false;
1✔
4725
            }
4726

4727
            formals[i].state = ref == name ? MAP_FULL : MAP_PARTIAL;
2,418✔
4728
            formals[i].last_pos = pos;
2,418✔
4729
            decl = formals[i].decl;
2,418✔
4730
            tree_set_flag(ref, TREE_F_FORMAL_NAME);
2,418✔
4731
            break;
2,418✔
4732
         }
4733

4734
         if (decl == NULL)
2,420✔
4735
            sem_error(value, "%s has no port named %s",
2✔
4736
                      istr(tree_ident(unit)), istr(tree_ident(ref)));
4737

4738
         if (!sem_static_name(name, sem_locally_static))
2,418✔
4739
            sem_error(name, "formal name must be locally static");
3✔
4740

4741
         if (out_conv != NULL) {
2,415✔
4742
            port_mode_t mode = tree_subkind(decl);
96✔
4743

4744
            type = tree_type((mode == PORT_INOUT) ? name : out_conv);
170✔
4745

4746
            if (mode == PORT_IN)
96✔
4747
               sem_error(name, "output conversion not allowed for formal "
1✔
4748
                         "%s with mode IN", istr(tree_ident(decl)));
4749

4750
            if (tree_kind(value) == T_OPEN)
95✔
4751
               sem_error(name, "output conversion for formal %s must not "
1✔
4752
                         "have OPEN actual", istr(tree_ident(decl)));
4753
         }
4754
         else
4755
            type = tree_type(name);
2,319✔
4756

4757
         break;
4758
      }
4759
   }
4760

4761
   assert(type != NULL);
3,930✔
4762

4763
   if (!sem_check(value, tab))
3,930✔
4764
      return false;
4765

4766
   const tree_kind_t kind = tree_kind(value);
3,927✔
4767
   tree_t expr = kind == T_INERTIAL ? tree_value(value) : value;
3,927✔
4768

4769
   type_t value_type = tree_type(expr), atype;
3,927✔
4770

4771
   if (!sem_check_type(expr, type, tab)) {
3,927✔
4772
      diag_t *d = diag_new(DIAG_ERROR, tree_loc(value));
3✔
4773
      diag_printf(d, "type of actual %pT does not match type %pT of formal "
3✔
4774
                  "port %pI", value_type, type, tree_ident(decl));
4775

4776
      if (type_is_generic(type)) {
3✔
4777
         hash_t *map = get_generic_map(tab);
1✔
4778
         if (map != NULL && (atype = hash_get(map, type)))
1✔
4779
            diag_hint(d, NULL, "generic type %pT is mapped to %pT",
1✔
4780
                      type, atype);
4781
      }
4782

4783
      diag_emit(d);
3✔
4784
      return false;
3✔
4785
   }
4786

4787
   const port_mode_t mode = tree_subkind(decl);
3,924✔
4788

4789
   if (kind == T_OPEN) {
3,924✔
4790
      if ((mode == PORT_IN) && !tree_has_value(decl))
64✔
4791
         sem_error(value, "unconnected port %pI with mode IN must have a "
1✔
4792
                   "default value", tree_ident(decl));
4793

4794
      if ((mode != PORT_IN) && type_is_unconstrained(tree_type(decl)))
63✔
4795
         sem_error(value, "port %pI of unconstrained type %pT cannot "
1✔
4796
                   "be unconnected", tree_ident(decl), type);
4797
   }
4798

4799
   // Check for type conversions and conversion functions
4800
   // These only apply if the class of the formal is not constant
4801

4802
   tree_t actual;
3,922✔
4803
   if (kind == T_TYPE_CONV || kind == T_CONV_FUNC) {
3,922✔
4804
      // Conversion functions are in LRM 93 section 4.3.2.2
4805
      actual = tree_value(value);
146✔
4806

4807
      // LRM 93 section 3.2.1.1 result of a type conversion in an
4808
      // association list cannot be an unconstrained array type
4809
      if (type_is_unconstrained(value_type) && type_is_unconstrained(type)) {
146✔
4810
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(value));
1✔
4811
         diag_printf(d, "result of conversion for unconstrained formal "
1✔
4812
                     "%pI must be a constrained array type",
4813
                     tree_ident(decl));
4814
         diag_lrm(d, STD_93, "3.2.1.1");
1✔
4815
         diag_emit(d);
1✔
4816
         return false;
1✔
4817
      }
4818

4819
      if (mode == PORT_OUT)
145✔
4820
         sem_error(value, "conversion not allowed for formal %pI with "
1✔
4821
                   "mode OUT", tree_ident(decl));
4822
      else if (mode == PORT_INOUT && out_conv == NULL)
144✔
4823
         sem_error(value, "INOUT port %pI has output conversion but no "
1✔
4824
                   "corresponding input conversion", tree_ident(decl));
4825
   }
4826
   else
4827
      actual = value;    // No conversion
4828

4829
   tree_t ref = name_to_ref(actual);
3,919✔
4830

4831
   if (mode == PORT_IN && kind != T_INERTIAL) {
3,919✔
4832
      bool is_static = true;
2,259✔
4833
      if (ref != NULL && class_of(ref) == C_SIGNAL)
2,259✔
4834
         is_static = sem_static_name(actual, sem_globally_static);
2,062✔
4835
      else
4836
         is_static = sem_globally_static(actual);
197✔
4837

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

4856
         tree_t w = tree_new(T_INERTIAL);
26✔
4857
         tree_set_loc(w, tree_loc(value));
26✔
4858
         tree_set_value(w, value);
26✔
4859
         tree_set_type(w, tree_type(value));
26✔
4860

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

4881
      if (!sem_check_compatible_view(decl, actual))
91✔
4882
         return false;
4883
   }
4884
   else if (mode != PORT_IN && tree_kind(actual) != T_OPEN
1,563✔
4885
            && !sem_static_signal_name(actual)) {
1,493✔
4886
      sem_error(value, "actual associated with port %pI of mode %s must be "
4✔
4887
                "a static signal name or OPEN",
4888
                tree_ident(decl), port_mode_str(tree_subkind(decl)));
4889
   }
4890

4891
   // Check connections between ports
4892
   if (ref != NULL && tree_has_ref(ref)) {
3,907✔
4893
      tree_t odecl = tree_ref(ref);
3,700✔
4894
      if (tree_kind(odecl) == T_PORT_DECL) {
3,700✔
4895
         const port_mode_t omode = tree_subkind(odecl);
330✔
4896

4897
         // TODO: how to handle views?
4898
         bool ok = omode == PORT_ARRAY_VIEW || omode == PORT_RECORD_VIEW;
330✔
4899

4900
         switch (mode) {
330✔
4901
         case PORT_IN:
169✔
4902
            ok |= omode == PORT_IN || omode == PORT_INOUT;
169✔
4903
            if (standard() >= STD_02)
169✔
4904
               ok |= omode == PORT_BUFFER;
89✔
4905
            if (standard() >= STD_08)
169✔
4906
               ok |= omode == PORT_OUT;
89✔
4907
            break;
4908
         case PORT_OUT:
114✔
4909
            ok |= omode == PORT_OUT || omode == PORT_INOUT;
114✔
4910
            if (standard() >= STD_02)
114✔
4911
               ok |= omode == PORT_BUFFER;
47✔
4912
            break;
4913
         case PORT_INOUT:
28✔
4914
            ok |= omode == PORT_INOUT;
28✔
4915
            if (standard() >= STD_02)
28✔
4916
               ok |= omode == PORT_BUFFER;
1✔
4917
            if (standard() >= STD_08)
28✔
4918
               ok |= omode == PORT_OUT;
1✔
4919
            break;
4920
         case PORT_BUFFER:
3✔
4921
            ok |= omode == PORT_BUFFER;
3✔
4922
            if (standard() >= STD_02)
3✔
4923
               ok |= omode == PORT_OUT || omode == PORT_INOUT;
1✔
4924
            break;
4925
         default:
4926
            ok = true;
4927
            break;
4928
         }
4929

4930
         if (!ok) {
314✔
4931
            diag_t *d = diag_new(DIAG_ERROR, tree_loc(value));
9✔
4932
            diag_printf(d, "port %pI with mode %s cannot be associated "
9✔
4933
                        "with formal port %pI with mode %s",
4934
                        tree_ident(odecl), port_mode_str(omode),
4935
                        tree_ident(decl), port_mode_str(mode));
4936
            diag_hint(d, tree_loc(decl), "formal port %pI declared here",
9✔
4937
                      tree_ident(decl));
4938
            diag_hint(d, tree_loc(odecl), "port %pI declared here",
9✔
4939
                      tree_ident(odecl));
4940
            diag_emit(d);
9✔
4941
            return false;
9✔
4942
         }
4943
      }
4944
   }
4945

4946
   return true;
4947
}
4948

4949
static bool sem_check_port_map(tree_t t, tree_t unit, nametab_t *tab)
2,052✔
4950
{
4951
   // Check there is an actual for each formal port generic
4952
   // Rules for maps are described in LRM 93 section 5.2.1.2
4953

4954
   const int nformals = tree_ports(unit);
2,052✔
4955
   const int nactuals = tree_params(t);
2,052✔
4956

4957
   bool ok = true;
2,052✔
4958

4959
   formal_map_t *formals LOCAL = xmalloc_array(nformals, sizeof(formal_map_t));
4,104✔
4960

4961
   for (int i = 0; i < nformals; i++) {
5,897✔
4962
      formals[i].decl     = tree_port(unit, i);
3,845✔
4963
      formals[i].state    = MAP_MISSING;
3,845✔
4964
      formals[i].last_pos = -1;
3,845✔
4965
   }
4966

4967
   for (int i = 0; i < nactuals; i++) {
6,005✔
4968
      tree_t p = tree_param(t, i);
3,953✔
4969
      if (tree_subkind(p) != P_NAMED)
3,953✔
4970
         continue;
1,520✔
4971

4972
      tree_t name = tree_name(p);
2,433✔
4973

4974
      ok &= sem_check(name, tab);
2,433✔
4975

4976
      const tree_kind_t name_kind = tree_kind(name);
2,433✔
4977
      if ((name_kind == T_ARRAY_REF || name_kind == T_ARRAY_SLICE)
2,433✔
4978
          && tree_kind(tree_value(p)) == T_OPEN && standard() < STD_19) {
191✔
4979
         diag_t *d = pedantic_diag(tree_loc(p));
1✔
4980
         if (d != NULL) {
1✔
4981
            diag_printf(d, "sub-elements of composite port cannot be "
1✔
4982
                        "associated with OPEN");
4983
            diag_emit(d);
1✔
4984
         }
4985
      }
4986
   }
4987

4988
   if (!ok)
2,052✔
4989
      return false;
4990

4991
   for (int i = 0; i < nactuals; i++) {
5,988✔
4992
      tree_t actual = tree_param(t, i);
3,942✔
4993
      ok &= sem_check_port_actual(formals, nformals, actual, unit, i, tab);
3,942✔
4994

4995
      if (!ok && tree_subkind(actual) == P_POS && i >= nformals)
3,942✔
4996
         break;   // Prevent useless repeated errors
4997
   }
4998

4999
   if (tree_kind(t) == T_BINDING)
2,048✔
5000
      return ok;
5001

5002
   for (int i = 0; i < nformals; i++) {
5,692✔
5003
      if (formals[i].state == MAP_MISSING) {
3,706✔
5004
         port_mode_t mode = tree_subkind(formals[i].decl);
319✔
5005

5006
         if (mode == PORT_IN && !tree_has_value(formals[i].decl)) {
319✔
5007
            error_at(tree_loc(t), "missing actual for port %pI of "
4✔
5008
                     "mode IN without a default expression",
5009
                     tree_ident(formals[i].decl));
4✔
5010
         }
5011
         else if (mode == PORT_ARRAY_VIEW || mode == PORT_RECORD_VIEW)
315✔
5012
            error_at(tree_loc(t), "missing actual for port %pI with "
2✔
5013
                     "mode view indication %pT", tree_ident(formals[i].decl),
1✔
5014
                     tree_type(tree_value(formals[i].decl)));
1✔
5015

5016
         type_t ftype = tree_type(formals[i].decl);
319✔
5017
         if (mode != PORT_IN && type_is_unconstrained(ftype))
319✔
5018
            error_at(tree_loc(t), "missing actual for port %pI with "
1✔
5019
                     "unconstrained array type", tree_ident(formals[i].decl));
1✔
5020
      }
5021
   }
5022

5023
   return ok;
5024
}
5025

5026
static bool sem_check_generic_actual(formal_map_t *formals, int nformals,
1,747✔
5027
                                     tree_t param, tree_t unit, int nth,
5028
                                     nametab_t *tab)
5029
{
5030
   tree_t name = NULL;
1,747✔
5031
   int pos = -1;
1,747✔
5032
   switch (tree_subkind(param)) {
1,747✔
5033
   case P_POS:
600✔
5034
      {
5035
         pos = tree_pos(param);
600✔
5036
         if (pos >= nformals)
600✔
5037
            sem_error(param, "found at least %d positional actuals but %s "
1✔
5038
                      "has only %d generic%s", pos + 1, istr(tree_ident(unit)),
5039
                      nformals, nformals == 1 ? "" : "s");
5040
         else if (tree_flags(formals[pos].decl) & TREE_F_PREDEFINED) {
599✔
5041
            diag_t *d = diag_new(DIAG_WARN, tree_loc(param));
2✔
5042
            diag_printf(d, "positional generic actual is associated with "
2✔
5043
                        "implicit generic subprogram %s for type %s",
5044
                        istr(tree_ident(formals[pos].decl)),
5045
                        type_pp(tree_type(tree_port(formals[pos].decl, 0))));
5046
            diag_hint(d, NULL, "use a named association if this was intended");
2✔
5047
            diag_emit(d);
2✔
5048
         }
5049
      }
5050
      break;
5051

5052
   case P_NAMED:
1,147✔
5053
      {
5054
         name = tree_name(param);
1,147✔
5055

5056
         tree_t ref = name_to_ref(name);
1,147✔
5057
         if (ref == NULL)
1,147✔
5058
            sem_error(name, "invalid name in generic map");
1✔
5059
         else if (!tree_has_ref(ref))
1,146✔
5060
            return false;
5061

5062
         tree_t d = tree_ref(ref);
1,143✔
5063
         for (pos = 0; pos < nformals; pos++) {
3,779✔
5064
            if (formals[pos].decl == d) {
2,635✔
5065
               tree_set_flag(ref, TREE_F_FORMAL_NAME);
1,142✔
5066
               break;
1,142✔
5067
            }
5068
         }
5069

5070
         if (pos == nformals)
1,143✔
5071
            sem_error(name, "%s is not a formal generic of %s",
1✔
5072
                      istr(tree_ident(ref)), istr(tree_ident(unit)));
5073
         break;
5074
      }
5075
   }
5076

5077
   assert(pos >= 0 && pos < nformals);
1,741✔
5078

5079
   tree_t decl = formals[pos].decl, value = tree_value(param);
1,741✔
5080
   type_t type = get_type_or_null(name ?: decl);
2,340✔
5081

5082
   const bool is_full = name == NULL || tree_kind(name) == T_REF;
1,741✔
5083
   const bool is_open = tree_kind(value) == T_OPEN;
1,741✔
5084

5085
   if ((is_open && formals[pos].state == MAP_PARTIAL)
1,741✔
5086
       || (!is_open && formals[pos].state == MAP_OPEN))
1,740✔
5087
      sem_error(param, "formal generic %s associated with OPEN cannot be "
2✔
5088
                "individually associated", istr(tree_ident(formals[pos].decl)));
5089

5090
   if (formals[pos].state >= (is_full ? MAP_PARTIAL : MAP_FULL))
1,755✔
UNCOV
5091
      sem_error(param, "formal generic %s already has an actual",
×
5092
                istr(tree_ident(formals[pos].decl)));
5093
   else if (formals[pos].state == MAP_PARTIAL
1,739✔
5094
            && formals[pos].last_pos != nth - 1) {
7✔
5095
      diag_t *d = diag_new(DIAG_ERROR, tree_loc(name));
1✔
5096
      diag_printf(d, "associations for formal generic %s must appear in "
1✔
5097
                  "a contiguous sequence",
5098
                  istr(tree_ident(formals[pos].decl)));
5099
      diag_lrm(d, STD_08, "6.5.7");
1✔
5100
      diag_emit(d);
1✔
5101
      return false;
1✔
5102
   }
5103

5104
   if (is_open)
1,738✔
5105
      formals[pos].state = MAP_OPEN;
9✔
5106
   else if (is_full)
1,729✔
5107
      formals[pos].state = MAP_FULL;
1,715✔
5108
   else {
5109
      formals[pos].state = MAP_PARTIAL;
14✔
5110
      formals[pos].last_pos = nth;
14✔
5111
   }
5112

5113
   if (is_open && !tree_has_value(decl))
1,738✔
5114
      sem_error(param, "generic %s without a default expression cannot "
1✔
5115
                "be associated with OPEN", istr(tree_ident(decl)));
5116
   else if (is_open)
1,737✔
5117
      return true;   // No further checking
5118

5119
   switch (tree_class(decl)) {
1,729✔
5120
   case C_TYPE:
308✔
5121
      // The parser already called map_generic_type
5122
      assert(tree_kind(value) == T_TYPE_REF);
308✔
5123
      assert(type_kind(type) == T_GENERIC);
308✔
5124

5125
      type_t map = tree_type(value);
308✔
5126
      if (type_is_none(map))
308✔
5127
         return false;
5128
      else if (!sem_check_incomplete(param, map))
304✔
5129
         return false;
5130

5131
      static const char *class_strings[] = {
303✔
5132
         [GTYPE_SCALAR] = "a scalar",
5133
         [GTYPE_DISCRETE] = "a discrete",
5134
         [GTYPE_INTEGER] = "an integer",
5135
         [GTYPE_FLOATING] = "a floating-point",
5136
         [GTYPE_PHYSICAL] = "a physical",
5137
         [GTYPE_ACCESS] = "an access",
5138
         [GTYPE_ARRAY] = "an array",
5139
         [GTYPE_FILE] = "a file",
5140
      };
5141

5142
      const gtype_class_t class = type_subkind(type);
303✔
5143
      if (!type_matches_class(map, class))
303✔
5144
         sem_error(param, "cannot map type %s to generic interface type %s "
8✔
5145
                   "which requires %s type", type_pp(map),
5146
                   istr(tree_ident(decl)), class_strings[class]);
5147
      else if (class == GTYPE_ACCESS || class == GTYPE_FILE) {
295✔
5148
         type_t expect = type_designated(type);
9✔
5149
         type_t actual = type_designated(map);
9✔
5150

5151
         if (type_is_generic(expect)) {
9✔
5152
            const gtype_class_t expect_class = type_subkind(expect);
5✔
5153
            if (!type_matches_class(actual, expect_class))
5✔
5154
               sem_error(param, "cannot map type %s to generic interface type "
1✔
5155
                         "%s as the designated type %s is not %s type",
5156
                         type_pp(map), istr(tree_ident(decl)),
5157
                         type_pp(actual), class_strings[expect_class]);
5158
         }
5159
         else if (!type_eq(actual, expect))
4✔
5160
            sem_error(param, "cannot map type %s to generic interface type "
2✔
5161
                      "%s as the designated type %s is not %s",
5162
                      type_pp(map), istr(tree_ident(decl)),
5163
                      type_pp(actual), type_pp(expect));
5164
      }
5165
      else if (class == GTYPE_ARRAY) {
286✔
5166
         const int nindex = type_indexes(type);
52✔
5167
         if (nindex != dimension_of(map))
52✔
5168
            sem_error(param, "cannot map type %s to generic interface "
1✔
5169
                      "type %s as it has %d dimensions but the incomplete "
5170
                      "type definition has %d", type_pp(map),
5171
                      istr(tree_ident(decl)), dimension_of(map), nindex);
5172

5173
         for (int i = 0; i < nindex; i++) {
100✔
5174
            type_t itype = type_index(type, i);
51✔
5175
            type_t imap = index_type_of(map, i);
51✔
5176

5177
            if (type_is_generic(itype)) {
51✔
5178
               const gtype_class_t expect_class = type_subkind(itype);
46✔
5179
               if (!type_matches_class(imap, expect_class))
46✔
5180
                  sem_error(param, "cannot map type %s to generic interface "
1✔
5181
                            "type %s as the index type %s of the %s dimension "
5182
                            "is not %s type", type_pp(map),
5183
                            istr(tree_ident(decl)), type_pp(imap),
5184
                            ordinal_str(i + 1), class_strings[expect_class]);
5185
            }
5186
            else if (!type_eq(imap, itype))
5✔
5187
               sem_error(param, "cannot map type %s to generic interface type "
50✔
5188
                         "%s as the index type %s of the %s dimension is not "
5189
                         "%s", type_pp(map), istr(tree_ident(decl)),
5190
                         type_pp(imap), ordinal_str(i + 1), type_pp(itype));
5191
         }
5192

5193
         type_t expect = type_elem(type);
49✔
5194
         type_t actual = type_elem(map);
49✔
5195

5196
          if (type_is_generic(expect)) {
49✔
5197
            const gtype_class_t expect_class = type_subkind(expect);
45✔
5198
            if (!type_matches_class(actual, expect_class))
45✔
5199
               sem_error(param, "cannot map type %s to generic interface type "
1✔
5200
                         "%s as the element type %s is not %s type",
5201
                         type_pp(map), istr(tree_ident(decl)),
5202
                         type_pp(actual), class_strings[expect_class]);
5203
         }
5204
         else if (!type_eq(actual, expect))
4✔
5205
            sem_error(param, "cannot map type %s to generic interface type "
1✔
5206
                      "%s as the element type %s is not %s",
5207
                      type_pp(map), istr(tree_ident(decl)),
5208
                      type_pp(actual), type_pp(expect));
5209
      }
5210

5211
      break;
5212

5213
   case C_PACKAGE:
80✔
5214
      {
5215
         tree_t pack = NULL;
80✔
5216
         if (tree_kind(value) == T_REF && tree_has_ref(value))
80✔
5217
            pack = tree_ref(value);
79✔
5218

5219
         if (pack == NULL || tree_kind(pack) != T_PACK_INST)
79✔
5220
            sem_error(value, "actual for generic %s is not an "
2✔
5221
                      "instantiated package name", istr(tree_ident(decl)));
5222
         else if (!tree_has_ref(pack))
78✔
5223
            return false;   // Was parse error
5224

5225
         tree_t map = tree_value(decl);
78✔
5226
         if (!tree_has_ref(map))
78✔
5227
            return false;   // Was earlier error
5228

5229
         assert(tree_kind(map) == T_PACKAGE_MAP);
77✔
5230

5231
         tree_t base = tree_ref(pack);
77✔
5232
         tree_t expect = tree_ref(map);
77✔
5233

5234
         if (tree_ident(base) != tree_ident(expect))
77✔
5235
            sem_error(value, "expected an instance of package %s but have "
1✔
5236
                      "instance of %s for generic %s", istr(tree_ident(expect)),
5237
                      istr(tree_ident(base)), istr(tree_ident(decl)));
5238

5239
         map_generic_package(tab, expect, pack);
76✔
5240
      }
5241
      break;
76✔
5242

5243
   case C_FUNCTION:
64✔
5244
   case C_PROCEDURE:
5245
      if (!sem_check(value, tab))
64✔
5246
         return false;
5247

5248
      if (!type_eq_map(tree_type(value), type, get_generic_map(tab)))
60✔
UNCOV
5249
         sem_error(value, "type of actual %s does not match type %s of formal "
×
5250
                   "generic %s", type_pp(tree_type(value)), type_pp(type),
5251
                   istr(tree_ident(decl)));
5252

5253
      assert(tree_kind(value) == T_REF);
60✔
5254

5255
      if (!tree_has_ref(value))
60✔
5256
         return false;
5257

5258
      tree_t sub = tree_ref(value);
60✔
5259
      assert(is_subprogram(sub));
60✔
5260

5261
      const bool pure_formal = !(tree_flags(decl) & TREE_F_IMPURE);
60✔
5262
      const bool pure_actual = !(tree_flags(sub) & TREE_F_IMPURE);
60✔
5263

5264
      if (pure_formal && !pure_actual) {
60✔
5265
         diag_t *d = pedantic_diag(tree_loc(value));
1✔
5266
         if (d != NULL) {
1✔
5267
            diag_printf(d, "cannot associate impure function %s with pure "
1✔
5268
                        "generic subprogram %s", type_pp(tree_type(value)),
5269
                        istr(tree_ident(decl)));
5270
            diag_emit(d);
1✔
5271
         }
5272
      }
5273

5274
      map_generic_subprogram(tab, decl, sub);
60✔
5275
      break;
60✔
5276

5277
   case C_CONSTANT:
1,277✔
5278
      if (name != NULL) {
1,277✔
5279
         if (!sem_check(name, tab))
810✔
5280
            return false;
5281

5282
         if (!sem_static_name(name, sem_locally_static))
809✔
5283
            sem_error(name, "formal generic name must be a locally "
1✔
5284
                      "static name");
5285
      }
5286

5287
      if (!sem_check(value, tab))
1,275✔
5288
         return false;
5289

5290
      if (!sem_check_type(value, type, tab))
1,272✔
5291
         sem_error(value, "type of actual %s does not match type %s of formal "
6✔
5292
                   "generic %s", type_pp(tree_type(value)), type_pp(type),
5293
                   istr(tree_ident(decl)));
5294

5295
      if (is_full) map_generic_const(tab, decl, value);
1,266✔
5296
      break;
5297

5298
   default:
5299
      // Was an earlier error
5300
      break;
5301
   }
5302

5303
   return true;
5304
}
5305

5306
static bool sem_check_generic_map(tree_t t, tree_t unit, nametab_t *tab)
2,504✔
5307
{
5308
   // Check there is an actual for each formal generic
5309
   // Rules for maps are described in LRM 93 section 5.2.1.2
5310

5311
   const int nformals = tree_generics(unit);
2,504✔
5312
   const int nactuals = tree_genmaps(t);
2,504✔
5313

5314
   formal_map_t *formals LOCAL = xmalloc_array(nformals, sizeof(formal_map_t));
2,504✔
5315

5316
   for (int i = 0; i < nformals; i++) {
6,153✔
5317
      formals[i].decl  = tree_generic(unit, i);
3,649✔
5318
      formals[i].state = MAP_MISSING;
3,649✔
5319
   }
5320

5321
   bool ok = true;
5322

5323
   for (int i = 0; i < nactuals; i++) {
4,250✔
5324
      tree_t actual = tree_genmap(t, i);
1,747✔
5325
      ok &= sem_check_generic_actual(formals, nformals, actual, unit, i, tab);
1,747✔
5326

5327
      if (!ok && tree_subkind(actual) == P_POS && i >= nformals)
1,747✔
5328
         break;   // Prevent useless repeated errors
5329
   }
5330

5331
   for (int i = 0; i < nformals; i++) {
6,153✔
5332
      if (formals[i].state >= MAP_PARTIAL)
3,649✔
5333
         continue;
1,723✔
5334

5335
      const class_t class = tree_class(formals[i].decl);
1,926✔
5336
      if (class == C_PACKAGE)
1,926✔
5337
         error_at(tree_loc(t), "missing actual for generic package %s",
1✔
5338
                  istr(tree_ident(formals[i].decl)));
1✔
5339
      else if (class == C_TYPE) {
1,925✔
5340
         error_at(tree_loc(t), "missing actual for generic type %s",
1✔
5341
                  istr(tree_ident(formals[i].decl)));
1✔
5342
         map_generic_type(tab, tree_type(formals[i].decl), type_new(T_NONE));
1✔
5343
      }
5344
      else if (tree_has_value(formals[i].decl)) {
1,924✔
5345
         tree_t value = tree_value(formals[i].decl);
1,918✔
5346
         if (tree_kind(value) == T_BOX) {
1,918✔
5347
            // Need to look up the matching subprogram now while we still
5348
            // have the symbol table
5349
            map_generic_box(tab, t, formals[i].decl, i);
1,654✔
5350
         }
5351
      }
5352
      else if (formals[i].state == MAP_MISSING) {
6✔
5353
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
5✔
5354
         diag_printf(d, "missing actual for generic %s without a "
5✔
5355
                     "default expression", istr(tree_ident(formals[i].decl)));
5✔
5356
         diag_hint(d, tree_loc(formals[i].decl), "generic %s declared here",
5✔
5357
                   istr(tree_ident(formals[i].decl)));
5✔
5358
         diag_emit(d);
5✔
5359
      }
5360
   }
5361

5362
   return ok;
2,504✔
5363
}
5364

5365
static bool sem_check_instance(tree_t t, nametab_t *tab)
1,611✔
5366
{
5367
   if (!tree_has_ref(t))
1,611✔
5368
      return false;
5369

5370
   tree_t unit = primary_unit_of(tree_ref(t));
1,599✔
5371

5372
   if (tree_has_spec(t)) {
1,599✔
5373
      tree_t spec = tree_spec(t);
108✔
5374

5375
      if (tree_class(t) != C_COMPONENT) {
108✔
5376
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(spec));
2✔
5377
         diag_printf(d, "specification may only be used with component"
2✔
5378
                     " instances");
5379
         diag_hint(d, tree_loc(spec), "specification for %s",
2✔
5380
                   istr(tree_ident(t)));
5381
         diag_hint(d, tree_loc(t), "%s instance", class_str(tree_class(t)));
2✔
5382
         diag_emit(d);
2✔
5383
         return false;
2✔
5384
      }
5385

5386
      assert(tree_kind(unit) == T_COMPONENT);   // Checked by parser
106✔
5387

5388
      if (tree_has_ref(spec) && tree_ref(spec) != unit) {
106✔
5389
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(spec));
2✔
5390
         diag_printf(d, "component mismatch for instance %s: expected %s "
2✔
5391
                     "but specification has %s", istr(tree_ident(t)),
5392
                     istr(tree_ident(unit)), istr(tree_ident(tree_ref(spec))));
5393
         diag_hint(d, tree_loc(spec), "specification has component %s",
2✔
5394
                   istr(tree_ident(tree_ref(spec))));
5395
         diag_hint(d, tree_loc(t), "instance of component %s",
2✔
5396
                   istr(tree_ident(unit)));
5397
         diag_emit(d);
2✔
5398
         return false;
2✔
5399
      }
5400
   }
5401

5402
   if (!sem_check_generic_map(t, unit, tab))
1,595✔
5403
      return false;
5404

5405
   if (!sem_check_port_map(t, unit, tab))
1,564✔
5406
      return false;
34✔
5407

5408
   return true;
5409
}
5410

5411
static bool sem_check_cond(tree_t t, nametab_t *tab)
13,125✔
5412
{
5413
   if (tree_has_value(t)) {
13,125✔
5414
      type_t std_bool = std_type(NULL, STD_BOOLEAN);
10,532✔
5415

5416
      tree_t value = tree_value(t);
10,532✔
5417
      if (!sem_check(value, tab))
10,532✔
5418
         return false;
5419

5420
      if (!sem_check_type(value, std_bool, tab))
10,527✔
5421
         sem_error(value, "type of condition must be %s but have %s",
2✔
5422
                   type_pp(std_bool), type_pp(tree_type(value)));
5423

5424
      if (!sem_check_readable(value))
10,525✔
UNCOV
5425
         return false;
×
5426
   }
5427

5428
   return true;
5429
}
5430

5431
static bool sem_check_if(tree_t t, nametab_t *tab)
9,358✔
5432
{
5433
   bool ok = true;
9,358✔
5434
   const int nconds = tree_conds(t);
9,358✔
5435
   for (int i = 0; i < nconds; i++)
22,286✔
5436
      ok &= sem_check_cond(tree_cond(t, i), tab);
12,928✔
5437

5438
   return ok;
9,358✔
5439
}
5440

5441
static bool sem_static_subtype(type_t type, static_fn_t fn)
273,759✔
5442
{
5443
   // Rules for locally static subtypes are in LRM 93 7.4.1
5444

5445
   if (type_is_unconstrained(type))
273,759✔
5446
      return false;
5447

5448
   if (type_is_scalar(type))
250,240✔
5449
      return true;
5450

5451
   if (type_is_record(type)) {
240,474✔
5452
      const int nfields = type_fields(type);
308✔
5453
      for (int i = 0; i < nfields; i++) {
1,171✔
5454
         if (!sem_static_subtype(tree_type(type_field(type, i)), fn))
884✔
5455
            return false;
5456
      }
5457

5458
      return true;
5459
   }
5460

5461
   switch (type_kind(type)) {
240,166✔
5462
   case T_SUBTYPE:
240,114✔
5463
      {
5464
         const int ndims = dimension_of(type);
240,114✔
5465
         for (int i = 0; i < ndims; i++) {
272,207✔
5466
            if (!(*fn)(range_of(type, i)))
240,307✔
5467
               return false;
5468
         }
5469

5470
         return true;
5471
      }
5472
   default:
5473
      return true;
5474
   }
5475
}
5476

5477
static bool sem_locally_static(tree_t t)
14,265,492✔
5478
{
5479
   // Rules for locally static expressions are in LRM 93 7.4.1
5480

5481
   type_t type = tree_type(t);
14,290,248✔
5482
   tree_kind_t kind = tree_kind(t);
14,290,248✔
5483

5484
   if (type_is_none(type))
14,290,248✔
5485
      return true;   // Prevents further cascading errors
5486

5487
   // Any literal other than of type time
5488
   if (kind == T_LITERAL) {
14,290,241✔
5489
      if (tree_subkind(t) == L_PHYSICAL)
115,654✔
5490
         return !type_eq(type, std_type(NULL, STD_TIME));
1,547✔
5491
      else
5492
         return true;
5493
   }
5494
   else if (kind == T_STRING)
14,174,587✔
5495
      return true;
5496
   else if ((kind == T_REF) && (tree_kind(tree_ref(t)) == T_ENUM_LIT))
14,162,029✔
5497
      return true;
5498
   else if (kind == T_OPEN)
14,110,401✔
5499
      return true;
5500

5501
   if (kind == T_REF) {
14,110,379✔
5502
      tree_t decl = tree_ref(t);
469,120✔
5503
      const tree_kind_t dkind = tree_kind(decl);
469,120✔
5504

5505
      // A constant reference (other than a deferred constant) with a
5506
      // locally static value
5507
      if (dkind == T_CONST_DECL && (tree_flags(decl) & TREE_F_LOCALLY_STATIC))
469,120✔
5508
         return true;
5509

5510
      // An alias of a locally static name
5511
      if (dkind == T_ALIAS)
438,347✔
5512
         return sem_locally_static(tree_value(decl));
1,728✔
5513

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

5520
   // A locally static range
5521
   if (kind == T_RANGE) {
14,076,685✔
5522
      switch (tree_subkind(t)) {
249,423✔
5523
      case RANGE_TO:
244,581✔
5524
      case RANGE_DOWNTO:
5525
         return sem_locally_static(tree_left(t))
244,581✔
5526
            && sem_locally_static(tree_right(t));
245,106✔
5527

5528
      case RANGE_EXPR:
4,842✔
5529
         return sem_locally_static(tree_value(t));
4,842✔
5530

5531
      default:
5532
         return false;
5533
      }
5534
   }
5535

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

5546
      tree_t decl = tree_ref(t);
12,859,133✔
5547
      if (tree_kind(decl) == T_GENERIC_DECL)
12,859,133✔
5548
         return false;   // Not known at this point
5549
      else if (tree_subkind(decl) == S_USER)
12,858,089✔
5550
         return false;
5551

5552
      const int nparams = tree_params(t);
12,803,732✔
5553
      for (int i = 0; i < nparams; i++) {
12,830,003✔
5554
         if (!sem_locally_static(tree_value(tree_param(t, i))))
12,817,825✔
5555
            return false;
5556
      }
5557

5558
      return true;
5559
   }
5560

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

5573
      // Whose actual parameter (if any) is a locally static expression
5574
      const int nparams = tree_params(t);
244,433✔
5575
      for (int i = 0; i < nparams; i++) {
245,009✔
5576
         if (!sem_locally_static(tree_value(tree_param(t, i))))
3,215✔
5577
            return false;
5578
      }
5579

5580
      if (tree_has_value(t)) {
241,794✔
5581
         // A user-defined attribute whose value is a locally static expression
5582
         return sem_locally_static(tree_value(t));
1,412✔
5583
      }
5584

5585
      type_t type = tree_type(tree_name(t));
240,382✔
5586
      return sem_static_subtype(type, sem_locally_static);
240,382✔
5587
   }
5588

5589
   // A qualified expression whose operand is locally static
5590
   if (kind == T_QUALIFIED)
5591
      return sem_locally_static(tree_value(t));
4,157✔
5592

5593
   // A type conversion whose expression is locally static
5594
   if (kind == T_TYPE_CONV)
5595
      return sem_locally_static(tree_value(t));
3,666✔
5596

5597
   // Aggregates must have locally static range and all elements
5598
   // must have locally static values
5599
   if (kind == T_AGGREGATE) {
5600
      if (type_is_unconstrained(type))
7,189✔
5601
         return false;
5602

5603
      if (type_is_array(type)) {
7,079✔
5604
         if (!sem_locally_static(range_of(type, 0)))
5,994✔
5605
            return false;
5606
      }
5607

5608
      const int nassocs = tree_assocs(t);
6,820✔
5609
      for (int i = 0; i < nassocs; i++) {
58,589✔
5610
         tree_t a = tree_assoc(t, i);
52,738✔
5611
         if ((tree_subkind(a) == A_NAMED) && !sem_locally_static(tree_name(a)))
52,738✔
5612
            return false;
5613

5614
         if (!sem_locally_static(tree_value(a)))
52,738✔
5615
            return false;
5616
      }
5617

5618
      return true;
5619
   }
5620

5621
   // A record field name
5622
   if ((kind == T_REF) && (tree_kind(tree_ref(t)) == T_FIELD_DECL))
435,426✔
5623
      return true;
5624

5625
   const bool std08_rules = standard() >= STD_08 || relaxed_rules();
558,253✔
5626

5627
   // [2008] An indexed name whose prefix and index expressions are
5628
   // locally static
5629
   if (std08_rules && kind == T_ARRAY_REF) {
558,253✔
5630
      const int nparams = tree_params(t);
7,690✔
5631
      for (int i = 0; i < nparams; i++) {
9,782✔
5632
         if (!sem_locally_static(tree_value(tree_param(t, i))))
7,744✔
5633
            return false;
5634
      }
5635

5636
      return sem_locally_static(tree_value(t));
2,038✔
5637
   }
5638

5639
   // [2008] A slice name whose prefix and range is locally static
5640
   if (std08_rules && kind == T_ARRAY_SLICE) {
550,563✔
5641
      if (!sem_locally_static(tree_range(t, 0)))
1,852✔
5642
         return false;
5643

5644
      return sem_locally_static(tree_value(t));
599✔
5645
   }
5646

5647
   // [2008] A selected name whose prefix is locally static
5648
   if (std08_rules && kind == T_RECORD_REF)
548,711✔
5649
      return sem_locally_static(tree_value(t));
6,314✔
5650

5651
   return false;
5652
}
5653

5654
static bool sem_static_name(tree_t t, static_fn_t check_fn)
12,161✔
5655
{
5656
   // Rules for static names are in LRM 93 6.1
5657

5658
   switch (tree_kind(t)) {
13,396✔
5659
   case T_REF:
11,126✔
5660
      {
5661
         tree_t decl = tree_ref(t);
11,126✔
5662
         switch (tree_kind(decl)) {
11,126✔
5663
         case T_SIGNAL_DECL:
5664
         case T_VAR_DECL:
5665
         case T_CONST_DECL:
5666
         case T_PORT_DECL:
5667
         case T_TYPE_DECL:
5668
         case T_SUBTYPE_DECL:
5669
         case T_ENTITY:
5670
         case T_ARCH:
5671
         case T_PACK_BODY:
5672
         case T_PACKAGE:
5673
         case T_FUNC_BODY:
5674
         case T_PROC_BODY:
5675
         case T_FUNC_DECL:
5676
         case T_PROC_DECL:
5677
         case T_PROCESS:
5678
         case T_BLOCK:
5679
         case T_ENUM_LIT:
5680
         case T_IMPLICIT_SIGNAL:
5681
         case T_GENERIC_DECL:
5682
         case T_PARAM_DECL:
5683
         case T_CONCURRENT:
5684
         case T_VIEW_DECL:
5685
         case T_UNIT_DECL:
5686
            return true;
5687
         case T_ALIAS:
53✔
5688
            return sem_static_name(tree_value(decl), check_fn);
53✔
UNCOV
5689
         default:
×
UNCOV
5690
            return false;
×
5691
         }
5692
      }
5693

5694
   case T_EXTERNAL_NAME:
5695
      return true;
5696

5697
   case T_RECORD_REF:
1,176✔
5698
      return sem_static_name(tree_value(t), check_fn);
1,176✔
5699

5700
   case T_ARRAY_REF:
874✔
5701
      {
5702
         if (!sem_static_name(tree_value(t), check_fn))
874✔
5703
            return false;
5704

5705
         const int nparams = tree_params(t);
874✔
5706
         for (int i = 0; i < nparams; i++) {
1,740✔
5707
            if (!(*check_fn)(tree_value(tree_param(t, i))))
877✔
5708
               return false;
5709
         }
5710

5711
         return true;
5712
      }
5713

5714
   case T_ARRAY_SLICE:
162✔
5715
      {
5716
         if (!sem_static_name(tree_value(t), check_fn))
162✔
5717
            return false;
5718

5719
         return (*check_fn)(tree_range(t, 0));
162✔
5720
      }
5721

5722
   case T_ATTR_REF:
6✔
5723
      {
5724
         switch (tree_subkind(t)) {
6✔
5725
         case ATTR_DELAYED:
6✔
5726
         case ATTR_STABLE:
5727
         case ATTR_QUIET:
5728
         case ATTR_TRANSACTION:
5729
            return sem_static_name(tree_name(t), check_fn);
6✔
5730
         default:
5731
            return false;
5732
         }
5733
      }
5734

5735
   default:
4✔
5736
      return false;
4✔
5737
   }
5738
}
5739

5740
static bool sem_globally_static(tree_t t)
802,424✔
5741
{
5742
   // Rules for globally static expressions are in LRM 93 7.4.2
5743

5744
   type_t type = tree_type(t);
807,653✔
5745
   tree_kind_t kind = tree_kind(t);
807,653✔
5746

5747
   if (type_is_none(type))
807,653✔
5748
      return true;   // Prevents further cascading errors
5749

5750
   // A literal of type TIME
5751

5752
   if (type_eq(type, std_type(NULL, STD_TIME))) {
807,653✔
5753
      if (kind == T_REF && tree_kind(tree_ref(t)) == T_UNIT_DECL)
7,966✔
5754
         return true;
5755
      else if (kind == T_LITERAL && tree_subkind(t) == L_PHYSICAL)
7,887✔
5756
         return true;
5757
   }
5758

5759
   // A locally static primary
5760

5761
   if (sem_locally_static(t))
806,540✔
5762
      return true;
5763

5764
   // A generic constant, generate parameter, or constant
5765

5766
   if (kind == T_REF) {
550,055✔
5767
      tree_t decl = tree_ref(t);
109,243✔
5768
      switch (tree_kind(decl)) {
109,243✔
5769
      case T_GENERIC_DECL:
5770
         return true;
5771
      case T_CONST_DECL:
4,318✔
5772
         // Do not treat all constants as globally static, this is a
5773
         // defect in the LRM
5774
         return !!(tree_flags(decl) & TREE_F_GLOBALLY_STATIC);
4,318✔
5775
      default:
48,067✔
5776
         return false;
48,067✔
5777
      }
5778
   }
5779
   else if (kind == T_EXTERNAL_NAME)
5780
      return tree_class(t) == C_CONSTANT;
97✔
5781

5782
   // An alias whose aliased name is globally static
5783

5784
   if (kind == T_ALIAS)
UNCOV
5785
      return sem_globally_static(tree_value(t));
×
5786

5787
   if (kind == T_RANGE) {
5788
      if (tree_subkind(t) == RANGE_EXPR)
26,786✔
5789
         return sem_globally_static(tree_value(t));
19✔
5790

5791
      if (!sem_globally_static(tree_left(t)))
26,767✔
5792
         return false;
5793

5794
      if (!sem_globally_static(tree_right(t)))
26,750✔
5795
         return false;
5796

5797
      return true;
26,708✔
5798
   }
5799

5800
   // Aggregates must have globally static range and all elements
5801
   // must have globally static values
5802
   if (kind == T_AGGREGATE) {
5803
      if (type_is_array(type)
698✔
5804
          && !type_is_unconstrained(type)
484✔
5805
          && !sem_globally_static(range_of(type, 0)))
445✔
5806
         return false;
5807

5808
      const int nassocs = tree_assocs(t);
656✔
5809
      for (int i = 0; i < nassocs; i++) {
2,613✔
5810
         tree_t a = tree_assoc(t, i);
2,006✔
5811
         if ((tree_subkind(a) == A_NAMED) && !sem_globally_static(tree_name(a)))
2,006✔
5812
            return false;
5813

5814
         if (!sem_globally_static(tree_value(a)))
2,006✔
5815
            return false;
5816
      }
5817

5818
      return true;
5819
   }
5820

5821
   // A function call of a pure function with globally static actuals
5822
   if (kind == T_FCALL) {
5823
      tree_t decl = tree_ref(t);
336,838✔
5824
      if (tree_flags(decl) & TREE_F_IMPURE)
336,838✔
5825
         return false;
5826

5827
      bool all_static = true;
332,678✔
5828
      const int nparams = tree_params(t);
332,678✔
5829
      for (int i = 0; i < nparams; i++) {
986,992✔
5830
         tree_t p = tree_param(t, i);
654,314✔
5831
         all_static = all_static && sem_globally_static(tree_value(p));
747,110✔
5832
      }
5833
      return all_static;
332,678✔
5834
   }
5835

5836
   if (kind == T_ATTR_REF) {
5837
      const attr_kind_t predef = tree_subkind(t);
37,129✔
5838

5839
      // A predefined attribute that is one of 'SIMPLE_NAME,
5840
      // 'INSTANCE_NAME, or 'PATH_NAME
5841
      if (predef == ATTR_SIMPLE_NAME || predef == ATTR_INSTANCE_NAME
37,129✔
5842
          || predef == ATTR_PATH_NAME)
36,241✔
5843
         return true;   // Clause j
5844

5845
      // A predefined attribute other than those listed below whose
5846
      // prefix is either a globally static subtype or is an object or
5847
      // function call that is of a globally static subtype, or in 2008,
5848
      // a prefix which is a appropriate for a globally static attribute
5849
      if (predef == ATTR_EVENT || predef == ATTR_ACTIVE
35,962✔
5850
          || predef == ATTR_LAST_EVENT || predef == ATTR_LAST_ACTIVE
35,962✔
5851
          || predef == ATTR_LAST_VALUE || predef == ATTR_DRIVING
35,570✔
5852
          || predef == ATTR_DRIVING_VALUE)
35,496✔
5853
         return false;   // Clause k
5854
      else if (predef == ATTR_USER) {
35,468✔
5855
         // A user-defined attribute whose value is a globally static
5856
         // expression
5857
         return sem_globally_static(tree_value(t));
120✔
5858
      }
5859

5860
      const int nparams = tree_params(t);
35,348✔
5861
      for (int i = 0; i < nparams; i++) {
35,577✔
5862
         if (!sem_globally_static(tree_value(tree_param(t, i))))
879✔
5863
            return false;
5864
      }
5865

5866
      tree_t name = tree_name(t);
34,698✔
5867

5868
      if (standard() >= STD_08 || relaxed_rules()) {
34,698✔
5869
         // LRM 08 section 9.4.3: A prefix is appropriate for a globally
5870
         // static attribute if it denotes a signal, a constant, a type
5871
         // or subtype, a globally static function call, a variable that
5872
         // is not of an access type, or a variable of an access type
5873
         // whose designated subtype is fully constrained.
5874

5875
         if (tree_kind(name) == T_FCALL)
6,291✔
5876
            return sem_globally_static(name);
5877

5878
         tree_t ref = name_to_ref(name);
6,291✔
5879
         if (ref == NULL)
6,291✔
5880
            return false;
5881

5882
         tree_t decl = tree_ref(ref);
6,287✔
5883
         const tree_kind_t dkind = tree_kind(decl);
6,287✔
5884
         if (dkind == T_VAR_DECL && type_is_access(tree_type(name)))
6,287✔
5885
            return false;
5886

5887
         return dkind == T_CONST_DECL || dkind == T_SIGNAL_DECL
6,287✔
5888
            || dkind == T_TYPE_DECL || dkind == T_VAR_DECL
6,248✔
5889
            || dkind == T_SUBTYPE_DECL || dkind == T_PORT_DECL
5890
            || dkind == T_GENERIC_DECL;
12,535✔
5891
      }
5892

5893
      type_t type = get_type_or_null(name);
28,407✔
5894
      if (type == NULL)
28,407✔
5895
         return false;
5896

5897
      return sem_static_subtype(type, sem_globally_static);
28,407✔
5898
   }
5899

5900
   // A qualified expression whose operand is globally static
5901

5902
   if (kind == T_QUALIFIED)
5903
      return sem_globally_static(tree_value(t));
238✔
5904

5905
   // A type conversion whose operand is globally static
5906

5907
   if (kind == T_TYPE_CONV)
5908
      return sem_globally_static(tree_value(t));
1,108✔
5909

5910
   // TODO: clauses o, p
5911

5912
   // A sub-element or slice where indexes are globally static
5913

5914
   if (kind == T_ARRAY_REF) {
5915
      if (!sem_globally_static(tree_value(t)))
30,567✔
5916
         return false;
5917

5918
      const int nparams = tree_params(t);
24,986✔
5919
      for (int i = 0; i < nparams; i++) {
49,577✔
5920
         if (!sem_globally_static(tree_value(tree_param(t, i))))
25,024✔
5921
            return false;
5922
      }
5923

5924
      return true;
5925
   }
5926
   else if (kind == T_ARRAY_SLICE) {
5927
      if (!sem_globally_static(tree_value(t)))
934✔
5928
         return false;
5929

5930
      if (!sem_globally_static(tree_range(t, 0)))
90✔
5931
         return false;
5932

5933
      return true;
90✔
5934
   }
5935
   else if (kind == T_RECORD_REF)
5936
      return sem_globally_static(tree_value(t));
3,744✔
5937

5938
   return false;
5939
}
5940

5941
static bool sem_check_case(tree_t t, nametab_t *tab)
713✔
5942
{
5943
   tree_t test = tree_value(t);
713✔
5944
   if (!sem_check(test, tab))
713✔
5945
      return false;
5946

5947
   type_t type = tree_type(test);
709✔
5948

5949
   // LRM 93 8.8 if the type of the expression is an array then it must be
5950
   // a one dimensional character array type
5951

5952
   const bool is_1d_character_array = type_is_character_array(type);
709✔
5953
   const bool valid = is_1d_character_array || type_is_discrete(type);
709✔
5954

5955
   if (!valid) {
709✔
5956
      diag_t *d = diag_new(DIAG_ERROR, tree_loc(test));
2✔
5957
      diag_printf(d, "case expression must have a discrete type or one "
2✔
5958
                  "dimensional character array type");
5959
      if (type_is_array(type) && dimension_of(type) != 1)
2✔
5960
         diag_hint(d, tree_loc(test), "array has %d dimensions",
1✔
5961
                   dimension_of(type));
5962
      else if (type_is_array(type))
1✔
5963
         diag_hint(d, tree_loc(test), "type %s is not a character array",
1✔
5964
                   type_pp(type));
5965
      else
UNCOV
5966
         diag_hint(d, tree_loc(test), "type is %s", type_pp(type));
×
5967
      diag_lrm(d, STD_08, "10.9");
2✔
5968
      diag_emit(d);
2✔
5969
      return false;
2✔
5970
   }
5971

5972
   if (is_1d_character_array && standard() < STD_08) {
707✔
5973
      // VHDL-93 requires a locally static subtype, relaxed in later
5974
      // revisions
5975
      if (!sem_static_subtype(type, sem_locally_static))
54✔
5976
         sem_error(test, "case expression must have locally static subtype");
2✔
5977
   }
5978

5979
   static_fn_t static_fn = sem_locally_static;
705✔
5980
   const char *static_str = "locally";
705✔
5981

5982
   if (tree_kind(t) == T_CASE_GENERATE) {
705✔
5983
      static_fn = sem_globally_static;
19✔
5984
      static_str = "globally";
19✔
5985
   }
5986

5987
   const int nstmts = tree_stmts(t);
705✔
5988
   for (int i = 0; i < nstmts; i++) {
3,957✔
5989
      tree_t alt = tree_stmt(t, i);
3,266✔
5990

5991
      const int nchoices = tree_choices(alt);
3,266✔
5992
      for (int j = 0; j < nchoices; j++) {
7,243✔
5993
         tree_t a = tree_choice(alt, j);
3,991✔
5994
         if (tree_has_name(a)) {
3,991✔
5995
            tree_t name = tree_name(a);
3,448✔
5996
            if (!sem_check(name, tab))
3,448✔
5997
               return false;
5998

5999
            if (!sem_check_type(name, type, tab))
3,448✔
6000
               sem_error(name, "case choice must have type %s but found %s",
1✔
6001
                         type_pp(type), type_pp(tree_type(name)));
6002
            else if (!(*static_fn)(name))
3,447✔
6003
               sem_error(name, "case choice must be %s static", static_str);
9✔
6004
         }
6005
         else if (tree_ranges(a) > 0) {
543✔
6006
            tree_t r = tree_range(a, 0);
47✔
6007
            if (!sem_check_discrete_range(r, type, tab))
47✔
6008
               return false;
6009

6010
            switch (tree_subkind(r)) {
45✔
6011
            case RANGE_TO:
36✔
6012
            case RANGE_DOWNTO:
6013
               if (!(*static_fn)(tree_left(r)))
36✔
UNCOV
6014
                  sem_error(tree_left(r), "left index of case choice "
×
6015
                            "range is not %s static", static_str);
6016
               else if (!(*static_fn)(tree_right(r)))
36✔
6017
                  sem_error(tree_right(r), "right index of case choice "
1✔
6018
                            "range is not %s static", static_str);
6019
               break;
6020

6021
            case RANGE_EXPR:
9✔
6022
               if (!(*static_fn)(tree_value(r)))
9✔
UNCOV
6023
                  sem_error(tree_value(r), "range expression is not %s "
×
6024
                            "static", static_str);
6025
               break;
6026

6027
            default:
6028
               return false;
6029
            }
6030
         }
6031
         else if (j != nchoices - 1 || i != nstmts - 1) {
496✔
6032
            diag_t *d = diag_new(DIAG_ERROR, tree_loc(a));
1✔
6033
            diag_printf(d, "others choice must appear last");
1✔
6034
            diag_hint(d, tree_loc(a), "others choice");
1✔
6035

6036
            tree_t more = j + 1 < nchoices
2✔
UNCOV
6037
               ? tree_choice(alt, j + 1)
×
6038
               : tree_choice(tree_stmt(t, i + 1), 0);
1✔
6039
            diag_hint(d, tree_loc(more), "further choices follow this");
1✔
6040

6041
            diag_emit(d);
1✔
6042
            return false;
1✔
6043
         }
6044
      }
6045
   }
6046

6047
   return true;
6048
}
6049

6050
static bool sem_check_match_case(tree_t t, nametab_t *tab)
43✔
6051
{
6052
   // Matching case statement is in LRM 08 section 10.9
6053

6054
   if (!sem_check_case(t, tab))
43✔
6055
      return false;
6056

6057
   tree_t value = tree_value(t);
42✔
6058
   type_t type = tree_type(value);
42✔
6059

6060
   type_t std_bit = std_type(NULL, STD_BIT);
42✔
6061
   type_t std_logic = ieee_type(IEEE_STD_ULOGIC);
42✔
6062

6063
   type_t elem = type;
42✔
6064
   if (type_is_array(type) && dimension_of(type) == 1)
42✔
6065
      elem = type_elem(type);
34✔
6066

6067
   if (!type_eq(elem, std_bit) && !type_eq(elem, std_logic)) {
42✔
6068
      diag_t *d = diag_new(DIAG_ERROR, tree_loc(value));
1✔
6069
      diag_printf(d, "type of expression in a matching case statement must be "
1✔
6070
                  "BIT, STD_ULOGIC, or a one-dimensional array of these types");
6071
      diag_hint(d, tree_loc(value), "type is %s", type_pp(type));
1✔
6072
      diag_lrm(d, STD_08, "10.9");
1✔
6073
      diag_emit(d);
1✔
6074
      return false;
1✔
6075
   }
6076

6077
   return true;
6078
}
6079

6080
static bool sem_check_return(tree_t t, nametab_t *tab)
13,657✔
6081
{
6082
   tree_t sub = find_enclosing(tab, S_SUBPROGRAM);
13,657✔
6083
   if (sub == NULL)
13,657✔
6084
      sem_error(t, "return statement not allowed outside subprogram");
6✔
6085

6086
   if (tree_has_value(t)) {
13,651✔
6087
      if (tree_kind(sub) == T_PROC_BODY)
13,261✔
6088
         sem_error(t, "cannot return a value from a procedure");
1✔
6089

6090
      tree_t value = tree_value(t);
13,260✔
6091
      if (!sem_check(value, tab))
13,260✔
6092
         return false;
6093

6094
      type_t expect = tree_type(t);
13,254✔
6095
      if (!sem_check_type(value, expect, tab))
13,254✔
6096
         sem_error(t, "expected return type %s but have %s",
2✔
6097
                   type_pp(expect), type_pp(tree_type(value)));
6098
   }
6099
   else if (tree_kind(sub) == T_FUNC_BODY)
390✔
6100
      sem_error(t, "return in function must have an expression");
1✔
6101

6102
   return true;
6103
}
6104

6105
static bool sem_check_cond_return(tree_t t, nametab_t *tab)
7✔
6106
{
6107
   tree_t sub = find_enclosing(tab, S_SUBPROGRAM);
7✔
6108
   if (sub == NULL)
7✔
6109
      sem_error(t, "return statement not allowed outside subprogram");
1✔
6110

6111
   if (tree_kind(sub) != T_PROC_BODY)
6✔
6112
      sem_error(t, "conditional return statement without value is only "
1✔
6113
                "valid inside a procedure");
6114

6115
   tree_t value = tree_value(t);
5✔
6116
   if (!sem_check(value, tab))
5✔
6117
      return false;
6118

6119
   type_t std_bool = std_type(NULL, STD_BOOLEAN);
5✔
6120

6121
   if (!sem_check_type(value, std_bool, tab))
5✔
6122
      sem_error(value, "type of condition must be %s but have %s",
1✔
6123
                type_pp(std_bool), type_pp(tree_type(value)));
6124

6125
   return true;
6126
}
6127

6128
static bool sem_check_while(tree_t t, nametab_t *tab)
219✔
6129
{
6130
   type_t std_bool = std_type(NULL, STD_BOOLEAN);
219✔
6131

6132
   tree_t value = tree_value(t);
219✔
6133
   if (!sem_check(value, tab))
219✔
6134
      return false;
6135

6136
   if (!sem_check_type(value, std_bool, tab))
219✔
6137
      sem_error(value, "type of loop condition must be %s but is %s",
1✔
6138
                type_pp(std_bool), type_pp(tree_type(value)));
6139

6140
   return true;
6141
}
6142

6143
static bool sem_check_for(tree_t t, nametab_t *tab)
2,755✔
6144
{
6145
   if (!sem_check_discrete_range(tree_range(t, 0), NULL, tab))
2,755✔
6146
      return false;
6147

6148
   tree_t idecl = tree_decl(t, 0);
2,741✔
6149

6150
   if (!sem_check_subtype(idecl, tree_type(idecl), tab))
2,741✔
UNCOV
6151
      return false;
×
6152

6153
   return true;
6154
}
6155

6156
static bool sem_check_block(tree_t t, nametab_t *tab)
432✔
6157
{
6158
   if (!sem_check_generic_map(t, t, tab))
432✔
6159
      return false;
6160

6161
   if (!sem_check_port_map(t, t, tab))
426✔
6162
      return false;
5✔
6163

6164
   return true;
6165
}
6166

6167
static bool sem_check_loop_control(tree_t t, nametab_t *tab)
496✔
6168
{
6169
   if (tree_has_value(t)) {
496✔
6170
      tree_t value = tree_value(t);
232✔
6171
      if (!sem_check(value, tab))
232✔
6172
         return false;
6173

6174
      type_t std_bool = std_type(NULL, STD_BOOLEAN);
232✔
6175
      if (!type_eq(tree_type(value), std_bool))
232✔
6176
         sem_error(value, "type of %s condition must be %s but is %s",
3✔
6177
                   (tree_kind(t) == T_EXIT) ? "exit" : "next",
6178
                   type_pp(std_bool), type_pp(tree_type(value)));
6179
   }
6180

6181
   return true;
6182
}
6183

6184
static bool sem_check_attr_decl(tree_t t)
124✔
6185
{
6186
   if (!sem_no_access_file_or_protected(t, tree_type(t), "attributes"))
124✔
6187
      return false;
5✔
6188

6189
   return true;
6190
}
6191

6192
static bool sem_check_attr_spec(tree_t t, nametab_t *tab)
365✔
6193
{
6194
   tree_t value = tree_value(t);
365✔
6195
   if (!sem_check(value, tab))
365✔
6196
      return false;
6197

6198
   type_t type = tree_type(t);
365✔
6199
   if (!sem_check_type(value, type, tab))
365✔
6200
      sem_error(t, "expected attribute specification for %s to have type %s "
2✔
6201
                "but found %s", istr(tree_ident(t)), type_pp(type),
6202
                type_pp(tree_type(value)));
6203

6204
   if (!tree_has_ref(t))
363✔
6205
      return false;
6206

6207
   tree_t decl = tree_ref(t);
348✔
6208
   const class_t class = tree_class(t);
348✔
6209

6210
   if (class_of(decl) != class)
348✔
6211
      sem_error(t, "class of object %s is %s not %s",
1✔
6212
                istr(tree_ident(decl)), class_str(class_of(decl)),
6213
                class_str(class));
6214

6215
   switch (is_well_known(tree_ident(t))) {
347✔
6216
   case W_NEVER_WAITS:
118✔
6217
      {
6218
         bool flag;
118✔
6219
         if (!folded_bool(value, &flag))
118✔
6220
            sem_error(value, "expression must be a BOOLEAN literal");
2✔
6221
         else if (class != C_PROCEDURE)
117✔
6222
            sem_error(t, "NEVER_WAITS attribute can only be applied to "
1✔
6223
                      "procedures");
6224
         else if (flag && !tree_frozen(decl))
116✔
6225
            tree_set_flag(decl, TREE_F_NEVER_WAITS);
115✔
6226
      }
6227
      break;
116✔
6228

6229
   case W_FOREIGN:
72✔
6230
      if (!tree_frozen(decl))
72✔
6231
         tree_set_flag(decl, TREE_F_NEVER_WAITS);
48✔
6232
      break;
6233

6234
   default:
6235
      break;
6236
   }
6237

6238
   return true;
6239
}
6240

6241
static bool sem_check_if_generate(tree_t t, nametab_t *tab)
178✔
6242
{
6243
   const int nconds = tree_conds(t);
178✔
6244
   for (int i = 0; i < nconds; i++) {
372✔
6245
      tree_t cond = tree_cond(t, i);
197✔
6246

6247
      if (!sem_check_cond(cond, tab))
197✔
6248
         return false;
6249

6250
      if (tree_has_value(cond)) {
195✔
6251
         tree_t value = tree_value(cond);
183✔
6252
         if (!sem_globally_static(value))
183✔
6253
            sem_error(value, "condition of generate statement must be static");
195✔
6254
      }
6255
   }
6256

6257
   return true;
6258
}
6259

6260
static bool sem_check_for_generate(tree_t t, nametab_t *tab)
181✔
6261
{
6262
   tree_t r = tree_range(t, 0);
181✔
6263
   if (!sem_check_discrete_range(r, NULL, tab))
181✔
6264
      return false;
6265

6266
   if (!sem_globally_static(r))
179✔
6267
      sem_error(r, "range of generate statement must be static");
1✔
6268

6269
   tree_t idecl = tree_decl(t, 0);
178✔
6270
   assert(tree_kind(idecl) == T_GENERIC_DECL);
178✔
6271

6272
   if (!sem_check_subtype(idecl, tree_type(idecl), tab))
178✔
UNCOV
6273
      return false;
×
6274

6275
   return true;
6276
}
6277

6278
static bool sem_check_open(tree_t t)
84✔
6279
{
6280
   return true;
84✔
6281
}
6282

6283
static bool sem_check_file_decl(tree_t t, nametab_t *tab)
166✔
6284
{
6285
   // Rules for file declarations are in LRM 93 section 4.3.1.4
6286

6287
   if (!type_is_file(tree_type(t)))
166✔
6288
      sem_error(t, "file declarations must have file type");
1✔
6289

6290
   if (tree_has_value(t)) {
165✔
6291
      tree_t value = tree_value(t);
52✔
6292
      if (!sem_check(value, tab))
52✔
6293
         return false;
6294

6295
      if (!sem_check_type(value, std_type(NULL, STD_STRING), tab))
52✔
6296
         sem_error(value, "file name must have type STRING");
1✔
6297

6298
      tree_t mode = tree_file_mode(t);
51✔
6299
      if (!sem_check(mode, tab))
51✔
6300
         return false;
6301

6302
      if (!sem_check_type(mode, std_type(NULL, STD_FILE_OPEN_KIND), tab))
50✔
6303
         sem_error(mode, "open mode must have type FILE_OPEN_KIND");
1✔
6304
   }
6305

6306
   tree_t sub = find_enclosing(tab, S_SUBPROGRAM);
162✔
6307
   const bool in_pure_func =
324✔
6308
      sub != NULL && tree_kind(sub) == T_FUNC_BODY
29✔
6309
      && !(tree_flags(sub) & TREE_F_IMPURE);
168✔
6310

6311
   if (in_pure_func) {
162✔
6312
      diag_t *d = pedantic_diag(tree_loc(t));
1✔
6313
      if (d != NULL) {
1✔
6314
         diag_printf(d, "cannot declare a file object in a pure function");
1✔
6315
         diag_emit(d);
1✔
6316
      }
6317
   }
6318

6319
   return true;
6320
}
6321

6322
static bool sem_check_new(tree_t t, nametab_t *tab)
582✔
6323
{
6324
   // Rules for allocators are in LRM 93 section 7.3.6
6325

6326
   tree_t value = tree_value(t);
582✔
6327
   type_t access_type = tree_type(t);
582✔
6328

6329
   if (type_is_none(access_type))
582✔
6330
      return false;
6331

6332
   assert(type_is_access(access_type));
581✔
6333
   assert(tree_kind(value) == T_QUALIFIED);
581✔
6334

6335
   if (!sem_check(value, tab))
581✔
6336
      return false;
6337

6338
   type_t type = tree_type(value);
581✔
6339

6340
   if (type_is_none(type))
581✔
6341
      return false;
6342

6343
   if (!sem_check_subtype(value, type, tab))
578✔
6344
      return false;
6345

6346
   const bool has_initial = tree_has_value(value);
578✔
6347

6348
   if (!has_initial && type_is_unconstrained(type))
578✔
6349
      sem_error(t, "unconstrained array type %s not allowed in allocator "
1✔
6350
                "expression", type_pp(type));
6351
   else if (!sem_check_incomplete(t, type))
577✔
6352
      return false;
6353
   else if (type_is_protected(type)) {
576✔
6354
      if (has_initial)
11✔
6355
         sem_error(t, "protected type %s cannot have initial value",
1✔
6356
                   type_pp(type));
6357
      else if (standard() >= STD_19)
10✔
6358
         tree_set_global_flags(t, TREE_GF_INSTANCE_NAME | TREE_GF_PATH_NAME);
10✔
6359
   }
6360

6361
   type_t designated = type_designated(access_type);
575✔
6362

6363
   if (!type_eq(type, designated))
575✔
6364
      sem_error(value, "type of allocator expresion %s does not match "
1✔
6365
                "access type %s", type_pp(type), type_pp(designated));
6366

6367
   return true;
6368
}
6369

6370
static bool sem_check_all(tree_t t, nametab_t *tab)
2,595✔
6371
{
6372
   tree_t value = tree_value(t);
2,595✔
6373
   if (!sem_check(value, tab))
2,595✔
6374
      return false;
6375

6376
   if (!sem_check_name_prefix(value, tab, "a selected name"))
2,593✔
6377
      return false;
6378

6379
   type_t value_type = tree_type(value);
2,593✔
6380

6381
   if (type_is_none(value_type))
2,593✔
6382
      return false;
6383

6384
   if (!sem_check_readable(value))
2,593✔
6385
      return false;
6386

6387
   if (!type_is_access(value_type)) {
2,592✔
6388
      diag_t *d = diag_new(DIAG_ERROR, tree_loc(value));
2✔
6389
      diag_printf(d, "prefix of a selected name with suffix ALL must "
2✔
6390
                  "have access type");
6391
      diag_hint(d, tree_loc(value), "prefix has type %s", type_pp(value_type));
2✔
6392
      diag_lrm(d, STD_08, "8.3");
2✔
6393
      diag_emit(d);
2✔
6394
      return false;
2✔
6395
   }
6396

6397
   return true;
6398
}
6399

6400
static bool sem_check_binding(tree_t t, nametab_t *tab)
208✔
6401
{
6402
   if (!tree_has_ref(t))
208✔
6403
      return false;
6404

6405
   tree_t unit = primary_unit_of(tree_ref(t));
204✔
6406
   if (tree_kind(unit) == T_ENTITY) {
204✔
6407
      if (tree_genmaps(t) > 0 && !sem_check_generic_map(t, unit, tab))
204✔
6408
         return false;
6409

6410
      if (tree_params(t) > 0 && !sem_check_port_map(t, unit, tab))
204✔
UNCOV
6411
         return false;
×
6412
   }
6413

6414
   return true;
6415
}
6416

6417
static bool sem_check_block_config(tree_t t, nametab_t *tab)
168✔
6418
{
6419
   return true;
168✔
6420
}
6421

6422
static bool sem_check_spec(tree_t t, nametab_t *tab)
228✔
6423
{
6424
   if (!tree_has_ref(t))
228✔
6425
      return false;
6426

6427
   tree_t comp = tree_ref(t);
224✔
6428
   assert(tree_kind(comp) == T_COMPONENT);
224✔
6429

6430
   if (!tree_has_value(t))
224✔
6431
      return true;
6432

6433
   tree_t bind = tree_value(t);
208✔
6434
   assert(tree_kind(bind) == T_BINDING);
208✔
6435

6436
   if (!sem_check(bind, tab))
208✔
6437
      return false;
6438

6439
   tree_t unit = tree_ref(bind);
204✔
6440
   tree_t entity = primary_unit_of(unit);
204✔
6441
   assert(tree_kind(entity) == T_ENTITY);
204✔
6442

6443
   bool ok = true;
204✔
6444

6445
   if (tree_genmaps(bind) == 0) {
204✔
6446
      const int c_ngenerics = tree_generics(comp);
149✔
6447
      const int e_ngenerics = tree_generics(entity);
149✔
6448

6449
      bit_mask_t have;
149✔
6450
      mask_init(&have, e_ngenerics);
149✔
6451

6452
      bool have_named = false;
149✔
6453
      for (int i = 0; i < c_ngenerics; i++) {
333✔
6454
         tree_t cg = tree_generic(comp, i);
184✔
6455

6456
         int epos = 0;
184✔
6457
         tree_t match = NULL;
184✔
6458
         for (; epos < e_ngenerics; epos++) {
2,445✔
6459
            tree_t eg = tree_generic(entity, epos);
2,259✔
6460
            if (ident_casecmp(tree_ident(eg), tree_ident(cg))) {
2,259✔
6461
               match = eg;
182✔
6462
               mask_set(&have, epos);
182✔
6463
               break;
6464
            }
6465
         }
6466

6467
         if (match == NULL) {
184✔
6468
            diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
2✔
6469
            diag_printf(d, "generic %s in component %s has no corresponding "
2✔
6470
                        "generic in entity %s", istr(tree_ident(cg)),
6471
                        istr(tree_ident(comp)), istr(tree_ident(entity)));
6472
            diag_hint(d, tree_loc(cg), "generic %s declared here",
2✔
6473
                      istr(tree_ident(cg)));
6474
            diag_emit(d);
2✔
6475

6476
            ok = false;
2✔
6477
            continue;
2✔
6478
         }
6479

6480
         tree_t value;
182✔
6481
         if (tree_class(cg) == C_PACKAGE) {
182✔
6482
            tree_t pmap = tree_value(cg);
8✔
6483
            assert(tree_kind(pmap) == T_PACKAGE_MAP);
8✔
6484

6485
            tree_t expect = tree_value(match);
8✔
6486
            assert(tree_kind(expect) == T_PACKAGE_MAP);
8✔
6487

6488
            if (tree_ref(pmap) != tree_ref(expect)) {
8✔
6489
               diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
1✔
6490
               diag_printf(d, "generic package %s in component %s does not "
1✔
6491
                           "match entity %s", istr(tree_ident(cg)),
6492
                           istr(tree_ident(comp)), istr(tree_ident(entity)));
6493
               diag_hint(d, tree_loc(cg), "declaration of generic %s in "
1✔
6494
                         "component as instance of %s", istr(tree_ident(cg)),
6495
                         istr(tree_ident(pmap)));
6496
               diag_hint(d, tree_loc(match), "declaration of generic %s in "
1✔
6497
                         "entity as instance of %s", istr(tree_ident(match)),
6498
                         istr(tree_ident(expect)));
6499
               diag_emit(d);
1✔
6500

6501
               ok = false;
1✔
6502
               continue;
1✔
6503
            }
6504

6505
            value = tree_new(T_REF);
7✔
6506
            tree_set_ident(value, tree_ident(cg));
7✔
6507
            tree_set_ref(value, cg);
7✔
6508
         }
6509
         else {
6510
            type_t ctype = tree_type(cg);
174✔
6511
            type_t etype = tree_type(match);
174✔
6512
            if (!type_eq(ctype, etype)) {
174✔
6513
               diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
1✔
6514
               diag_printf(d, "generic %pI in component %pI has type %pT which "
1✔
6515
                           "is incompatible with type %pT in entity %pI",
6516
                           tree_ident(cg), tree_ident(comp), ctype, etype,
6517
                           tree_ident(entity));
6518
               diag_hint(d, tree_loc(cg), "declaration of generic %pI in "
1✔
6519
                         "component", tree_ident(cg));
6520
               diag_hint(d, tree_loc(match), "declaration of generic %pI in "
1✔
6521
                         "entity", tree_ident(match));
6522
               diag_emit(d);
1✔
6523

6524
               ok = false;
1✔
6525
               continue;
1✔
6526
            }
6527

6528
            value = make_ref(cg);
173✔
6529
         }
6530

6531
         tree_t map = tree_new(T_PARAM);
180✔
6532
         tree_set_loc(map, tree_loc(t));
180✔
6533
         tree_set_value(map, value);
180✔
6534

6535
         if (!have_named && epos == i) {
180✔
6536
            tree_set_subkind(map, P_POS);
101✔
6537
            tree_set_pos(map, epos);
101✔
6538
         }
6539
         else {
6540
            tree_set_subkind(map, P_NAMED);
79✔
6541
            tree_set_name(map, make_ref(match));
79✔
6542
            have_named = true;
79✔
6543
         }
6544

6545
         tree_add_genmap(bind, map);
180✔
6546
      }
6547

6548
      for (int i = 0; i < e_ngenerics; i++) {
362✔
6549
         tree_t eg = tree_generic(entity, i);
213✔
6550
         if (!mask_test(&have, i) && !tree_has_value(eg)) {
213✔
6551
            diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
3✔
6552
            diag_printf(d, "generic %pI in entity %pI without a default value "
3✔
6553
                        "has no corresponding generic in component %pI",
6554
                        tree_ident(eg), tree_ident(entity), tree_ident(comp));
6555
            diag_hint(d, tree_loc(eg), "generic %pI declared here",
3✔
6556
                      tree_ident(eg));
6557
            diag_emit(d);
3✔
6558

6559
            ok = false;
3✔
6560
            continue;
3✔
6561
         }
6562
      }
6563

6564
      mask_free(&have);
149✔
6565
   }
6566

6567
   if (tree_params(bind) == 0) {
204✔
6568
      const int c_nports = tree_ports(comp);
142✔
6569
      const int e_nports = tree_ports(entity);
142✔
6570

6571
      bit_mask_t have;
142✔
6572
      mask_init(&have, e_nports);
142✔
6573

6574
      bool have_named = false;
142✔
6575
      for (int i = 0; i < c_nports; i++) {
993✔
6576
         tree_t cp = tree_port(comp, i);
851✔
6577

6578
         int epos = 0;
851✔
6579
         tree_t match = NULL;
851✔
6580
         for (; match == NULL && epos < e_nports; epos++) {
8,102✔
6581
            tree_t ep = tree_port(entity, epos);
7,246✔
6582
            if (ident_casecmp(tree_ident(ep), tree_ident(cp))) {
7,246✔
6583
               match = ep;
846✔
6584
               mask_set(&have, epos);
846✔
6585
               break;
6586
            }
6587
         }
6588

6589
         if (match == NULL) {
851✔
6590
            diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
5✔
6591
            diag_printf(d, "port %s in component %s has no corresponding port "
5✔
6592
                        "in entity %s", istr(tree_ident(cp)),
6593
                        istr(tree_ident(comp)), istr(tree_ident(entity)));
6594
            diag_hint(d, tree_loc(cp), "port %s declared here",
5✔
6595
                      istr(tree_ident(cp)));
6596
            diag_emit(d);
5✔
6597

6598
            ok = false;
5✔
6599
            continue;
5✔
6600
         }
6601

6602
         type_t ctype = tree_type(cp);
846✔
6603
         type_t etype = tree_type(match);
846✔
6604
         if (!type_eq(ctype, etype)) {
846✔
6605
            diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
1✔
6606
            diag_printf(d, "port %pI in component %pI has type %pT which is "
1✔
6607
                        "incompatible with type %pT in entity %pI",
6608
                        tree_ident(cp), tree_ident(comp), ctype, etype,
6609
                        tree_ident(entity));
6610
            diag_hint(d, tree_loc(cp), "declaration of port %pI in component",
1✔
6611
                      tree_ident(cp));
6612
            diag_hint(d, tree_loc(match), "declaration of port %pI in entity",
1✔
6613
                      tree_ident(match));
6614
            diag_emit(d);
1✔
6615

6616
            ok = false;
1✔
6617
            continue;
1✔
6618
         }
6619

6620
         if (!have_named && epos == i)
845✔
6621
            add_param(bind, make_ref(cp), P_POS, NULL);
827✔
6622
         else {
6623
            add_param(bind, make_ref(cp), P_NAMED, make_ref(match));
18✔
6624
            have_named = true;
18✔
6625
         }
6626
      }
6627

6628
      for (int i = 0; i < e_nports; i++) {
995✔
6629
         if (mask_test(&have, i))
853✔
6630
            continue;
846✔
6631

6632
         tree_t ep = tree_port(entity, i);
7✔
6633

6634
         const bool open_ok =
14✔
6635
            tree_has_value(ep)
7✔
6636
            || (tree_subkind(ep) == PORT_OUT
7✔
6637
                && !type_is_unconstrained(tree_type(ep)));
1✔
6638

6639
         if (!open_ok) {
7✔
6640
            diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
2✔
6641
            diag_printf(d, "port %s in entity %s without a default value "
2✔
6642
                        "has no corresponding port in component %s",
6643
                        istr(tree_ident(ep)), istr(tree_ident(entity)),
6644
                        istr(tree_ident(comp)));
6645
            diag_hint(d, tree_loc(ep), "port %s declared here",
2✔
6646
                      istr(tree_ident(ep)));
6647
            diag_emit(d);
2✔
6648

6649
            ok = false;
2✔
6650
            continue;
2✔
6651
         }
6652
      }
6653
   }
6654

6655
   return ok;
6656
}
6657

6658
static bool sem_check_configuration(tree_t t, nametab_t *tab)
116✔
6659
{
6660
   if (!tree_has_primary(t))
116✔
6661
      return false;   // Was parse error
6662

6663
   tree_t of = tree_primary(t);
115✔
6664

6665
   // LRM 08 section 3.4.1: for a configuration of a given design
6666
   // entity, both the configuration declaration and the corresponding
6667
   // entity declaration shall reside in the same library
6668
   ident_t elib = ident_until(tree_ident(of), '.');
115✔
6669
   if (standard() < STD_19 && elib != lib_name(lib_work())) {
115✔
6670
      diag_t *d = pedantic_diag(tree_loc(t));
1✔
6671
      if (d != NULL) {
1✔
6672
         ident_t ename = ident_rfrom(tree_ident(of), '.');
1✔
6673
         diag_printf(d, "configuration declaration %s must reside in the "
1✔
6674
                     "same library as entity %s", istr(tree_ident(t)),
6675
                     istr(ename));
6676
         diag_hint(d, NULL, "entity %s is in library %s which is not the same "
1✔
6677
                   "as the current working library %s",
6678
                   istr(ename), istr(elib), istr(lib_name(lib_work())));
6679
         diag_lrm(d, STD_08, "3.4");
1✔
6680
         diag_emit(d);
1✔
6681
         return false;
1✔
6682
      }
6683
   }
6684

6685
   return true;
6686
}
6687

6688
static bool sem_check_prot_body(tree_t t, nametab_t *tab)
214✔
6689
{
6690
   // Rules for protected type bodies are in LRM 00 section 3.5.2
6691

6692
   type_t type = tree_type(t);
214✔
6693
   if (type_is_none(type))
214✔
6694
      return false;
6695

6696
   if (!sem_check_missing_bodies(t, tab))
208✔
UNCOV
6697
      return false;
×
6698

6699
   return true;
6700
}
6701

6702
static bool sem_check_implicit_signal(tree_t t, nametab_t *tab)
29✔
6703
{
6704
   tree_t value = tree_value(t);
29✔
6705
   type_t type  = tree_type(t);
29✔
6706

6707
   if (!sem_check(value, tab))
29✔
6708
      return false;
6709

6710
   switch (tree_subkind(t)) {
29✔
6711
   case IMPLICIT_GUARD:
29✔
6712
      if (!sem_check_type(value, type, tab))
29✔
6713
         sem_error(value, "guard expression must have type %pT but "
1✔
6714
                   "found %pT", type, tree_type(value));
6715
      break;
6716
   }
6717

6718
   return true;
6719
}
6720

6721
static bool sem_check_context_decl(tree_t t, nametab_t *tab)
19✔
6722
{
6723
   // Context declarations are in LRM 08 section 13.3
6724

6725
   if (!sem_check_context_clause(t, tab))
19✔
6726
      return false;
1✔
6727

6728
   return true;
6729
}
6730

6731
static bool sem_check_context_ref(tree_t t, nametab_t *tab)
21✔
6732
{
6733
   if (standard() >= STD_08) {
21✔
6734
      tree_t unit = find_enclosing(tab, S_DESIGN_UNIT);
21✔
6735

6736
      if (unit != NULL && tree_kind(unit) == T_CONTEXT) {
21✔
6737
         // LRM 08 section 13.3
6738
         ident_t prefix = ident_until(tree_ident(t), '.');
6✔
6739
         if (prefix == well_known(W_WORK))
6✔
6740
            sem_error(t, "selected name in context declaration context "
1✔
6741
                      "reference may not have WORK as a prefix");
6742
      }
6743
   }
6744

6745
   return true;
6746
}
6747

6748
static bool sem_check_disconnect(tree_t t, nametab_t *tab)
14✔
6749
{
6750
   if (!tree_has_ref(t))
14✔
6751
      return false;
6752

6753
   tree_t decl = tree_ref(t);
14✔
6754
   if (class_of(decl) != C_SIGNAL || !is_guarded_signal(decl))
14✔
6755
      sem_error(t, "signal name %pI in disconnection specification must denote "
3✔
6756
                "a guarded signal", tree_ident(t));
6757

6758
   type_t type = tree_type(t);
11✔
6759
   if (!type_eq(tree_type(decl), type))
11✔
6760
      sem_error(t, "type of declared signal %pT does not match type %pT in "
1✔
6761
                "disconnection specification", tree_type(decl), type);
6762

6763
   tree_t delay = tree_delay(t);
10✔
6764
   type_t std_time = std_type(NULL, STD_TIME);
10✔
6765
   if (!sem_check_type(delay, std_time, tab))
10✔
6766
      sem_error(delay, "time expression in disconnection specification must "
1✔
6767
                "have type %pT but found %pT", std_time, tree_type(delay));
6768

6769
   if (!sem_globally_static(delay))
9✔
6770
      sem_error(delay, "time expression in disconnection specificiation "
1✔
6771
                "must be static");
6772

6773
   return true;
6774
}
6775

6776
static bool sem_check_conv_func(tree_t t, nametab_t *tab)
201✔
6777
{
6778
   if (type_is_none(tree_type(t)))
201✔
6779
      return false;
6780
   else if (!tree_has_ref(t))
201✔
6781
      return false;
6782

6783
   tree_t value = tree_value(t);
201✔
6784
   if (!sem_check(value, tab))
201✔
6785
      return false;
6786

6787
   tree_t decl = tree_ref(t);
201✔
6788
   assert(tree_ports(decl) == 1);
201✔
6789

6790
   tree_t formal = tree_port(decl, 0);
201✔
6791
   type_t ftype = tree_type(formal);
201✔
6792
   if (!sem_check_type(value, ftype, tab))
201✔
6793
      sem_error(value, "type of conversion function actual %pT does not match "
2✔
6794
                "formal %pI type %pT", tree_type(value), tree_ident(formal),
6795
                ftype);
6796

6797
   return true;
6798
}
6799

6800
static bool sem_check_concurrent(tree_t t, nametab_t *tab)
3,481✔
6801
{
6802
   if (tree_stmts(t) == 0)
3,481✔
6803
      return false;   // Was parse error
6804

6805
   return sem_check(tree_stmt(t, 0), tab);
3,481✔
6806
}
6807

6808
static bool sem_check_external_name(tree_t t, nametab_t *tab)
184✔
6809
{
6810
   const int nparts = tree_parts(t);
184✔
6811
   for (int i = 0; i < nparts; i++) {
829✔
6812
      tree_t pe = tree_part(t, i);
647✔
6813
      switch (tree_subkind(pe)) {
647✔
6814
      case PE_GENERATE:
30✔
6815
         {
6816
            tree_t value = tree_value(pe);
30✔
6817
            if (!sem_globally_static(value))
30✔
6818
               sem_error(value, "generate index must be a static expression");
1✔
6819
         }
6820
         break;
6821
      case PE_RELATIVE:
133✔
6822
         // Relative pathnames must have enclosing concurrent region
6823
         if (find_enclosing(tab, S_CONCURRENT_BLOCK) == NULL) {
133✔
6824
            diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
1✔
6825
            diag_printf(d, "relative pathname has no enclosing "
1✔
6826
                        "concurrent region");
6827
            diag_lrm(d, STD_08, "8.7");
1✔
6828
            diag_emit(d);
1✔
6829
            return false;
1✔
6830
         }
6831
      }
6832
   }
6833

6834
   const class_t class = tree_class(t);
182✔
6835
   if (class != C_CONSTANT) {
182✔
6836
      tree_t sub = find_enclosing(tab, S_SUBPROGRAM);
147✔
6837

6838
      const bool is_pure_func =
294✔
6839
         sub != NULL
6840
         && tree_kind(sub) == T_FUNC_BODY
8✔
6841
         && !(tree_flags(sub) & TREE_F_IMPURE);
148✔
6842

6843
      if (is_pure_func)
147✔
6844
         sem_error(t, "cannot reference external name with class %s in pure "
1✔
6845
                   "function %pI", class_str(class), tree_ident(sub));
6846
   }
6847

6848
   // Cannot do any more checking until elaboration
6849
   return true;
6850
}
6851

6852
static port_mode_t sem_default_force_mode(tree_t target)
94✔
6853
{
6854
   // Rules for default force mode in LRM 08 section 10.5.2.1
6855

6856
   tree_t ref = name_to_ref(target);
94✔
6857
   if (ref == NULL || !tree_has_ref(ref))
94✔
6858
      return PORT_IN;
14✔
6859

6860
   tree_t decl = tree_ref(ref);
80✔
6861
   const tree_kind_t dkind = tree_kind(decl);
80✔
6862
   if (dkind == T_PORT_DECL || dkind == T_PARAM_DECL) {
80✔
6863
      switch (tree_subkind(decl)) {
6✔
6864
      case PORT_OUT:
6865
      case PORT_INOUT:
6866
      case PORT_BUFFER:
6867
         return PORT_OUT;
UNCOV
6868
      default:
×
UNCOV
6869
         return PORT_IN;
×
6870
      }
6871
   }
6872

6873
   return PORT_IN;
6874
}
6875

6876
static bool sem_check_force_target(tree_t target, port_mode_t mode,
105✔
6877
                                   const char *what)
6878
{
6879
   tree_t decl = sem_check_lvalue(target);
105✔
6880
   if (decl == NULL)
105✔
6881
      sem_error(target, "target of simple %s assignment must be a "
1✔
6882
                "signal name", what);
6883

6884
   switch (tree_kind(decl)) {
104✔
6885
   case T_SIGNAL_DECL:
6886
      break;
6887

6888
   case T_PORT_DECL:
9✔
6889
   case T_PARAM_DECL:
6890
      if (tree_class(decl) != C_SIGNAL) {
9✔
6891
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(target));
×
UNCOV
6892
         diag_printf(d, "%s is not a valid target of simple %s assignment",
×
6893
                     what, istr(tree_ident(decl)));
6894
         diag_hint(d, tree_loc(target), "target of simple %s assignment", what);
×
UNCOV
6895
         diag_hint(d, tree_loc(decl), "declared with class %s",
×
6896
                   class_str(tree_class(decl)));
UNCOV
6897
         diag_emit(d);
×
UNCOV
6898
         return false;
×
6899
      }
6900
      else if (mode == PORT_OUT && tree_subkind(decl) == PORT_IN)
9✔
6901
         sem_error(target, "force mode OUT may not be used with target "
1✔
6902
                   "of mode IN");
6903
      break;
6904

6905
      case T_EXTERNAL_NAME:
32✔
6906
         if (tree_class(decl) != C_SIGNAL) {
32✔
6907
            tree_t tail = tree_part(decl, tree_parts(decl) - 1);
1✔
6908
            diag_t *d = diag_new(DIAG_ERROR, tree_loc(target));
1✔
6909
            diag_printf(d, "external name %s is not a valid target of "
1✔
6910
                        "simple %s assignment", istr(tree_ident(tail)), what);
6911
            diag_hint(d, tree_loc(target), "target of signal assignment");
1✔
6912
            diag_hint(d, tree_loc(decl), "declared with class %s",
1✔
6913
                      class_str(tree_class(decl)));
6914
            diag_emit(d);
1✔
6915
            return false;
1✔
6916
         }
6917
         break;
6918

6919
   case T_VAR_DECL:
2✔
6920
   case T_CONST_DECL:
6921
      {
6922
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(target));
2✔
6923
         diag_printf(d, "%s %s is not a valid target of simple %s assignment",
2✔
6924
                     class_str(class_of(decl)), istr(tree_ident(decl)), what);
6925
         diag_hint(d, tree_loc(target), "target of simple %s assignment", what);
2✔
6926
         diag_hint(d, tree_loc(decl), "declared as %s",
2✔
6927
                   class_str(class_of(decl)));
6928
         diag_emit(d);
2✔
6929
         return false;
2✔
6930
      }
6931

UNCOV
6932
   default:
×
UNCOV
6933
      sem_error(target, "invalid target of simple %s assignment", what);
×
6934
   }
6935

6936
   return true;
6937
}
6938

6939
static bool sem_check_force(tree_t t, nametab_t *tab)
68✔
6940
{
6941
   tree_t target = tree_target(t);
68✔
6942

6943
   if (!sem_check(target, tab))
68✔
6944
      return false;
6945

6946
   port_mode_t mode = tree_subkind(t);
67✔
6947
   if (mode == PORT_INVALID)
67✔
6948
      tree_set_subkind(t, (mode = sem_default_force_mode(target)));
58✔
6949

6950
   if (!sem_check_force_target(target, mode, "force"))
67✔
6951
      return false;
6952

6953
   tree_t value = tree_value(t);
64✔
6954
   if (!sem_check(value, tab))
64✔
6955
      return false;
6956

6957
   type_t expect = tree_type(target);
64✔
6958

6959
   if (!sem_check_type(value, expect, tab))
64✔
6960
      sem_error(t, "type of force expression %pT does not match type of "
2✔
6961
                "target %pT", tree_type(value), expect);
6962

6963
   return true;
6964
}
6965

6966
static bool sem_check_release(tree_t t, nametab_t *tab)
39✔
6967
{
6968
   tree_t target = tree_target(t);
39✔
6969

6970
   if (!sem_check(target, tab))
39✔
6971
      return false;
6972

6973
   port_mode_t mode = tree_subkind(t);
38✔
6974
   if (mode == PORT_INVALID)
38✔
6975
      tree_set_subkind(t, (mode = sem_default_force_mode(target)));
36✔
6976

6977
   if (!sem_check_force_target(target, mode, "release"))
38✔
6978
      return false;
2✔
6979

6980
   return true;
6981
}
6982

6983
static bool sem_check_prot_ref(tree_t t, nametab_t *tab)
13✔
6984
{
6985
   if (standard() >= STD_19)
13✔
6986
      return true;   // Alias of private variable method
6987

6988
   // There are no legal ways this can appear here and should always
6989
   // have been converted to a call
UNCOV
6990
   assert(error_count() > 0);
×
6991

6992
   return false;
6993
}
6994

6995
static bool sem_check_view_decl(tree_t t, nametab_t *tab)
76✔
6996
{
6997
   type_t type = tree_type(t);
76✔
6998
   if (type_is_none(type))
76✔
6999
      return false;
7000

7001
   assert(type_kind(type) == T_VIEW);
76✔
7002

7003
   type_t rtype = type_designated(type);
76✔
7004
   if (!type_is_record(rtype)) {
76✔
7005
      assert(error_count() > 0);   // Checked by parser
1✔
7006
      return false;
7007
   }
7008
   else if (type_is_resolved(rtype))
75✔
7009
      sem_error(t, "subtype indication of a mode view declaration "
1✔
7010
                "must denote an unresolved record type");
7011

7012
   const int nfields = type_fields(rtype);
74✔
7013

7014
   LOCAL_BIT_MASK have;
148✔
7015
   mask_init(&have, nfields);
74✔
7016

7017
   const int nelems = type_fields(type);
74✔
7018
   for (int i = 0; i < nelems; i++) {
228✔
7019
      tree_t e = type_field(type, i);
161✔
7020
      assert(tree_kind(e) == T_VIEW_ELEMENT);
161✔
7021

7022
      if (!tree_has_ref(e))
161✔
7023
         return false;
7024

7025
      tree_t f = tree_ref(e);
159✔
7026
      assert(tree_kind(f) == T_FIELD_DECL);
159✔
7027

7028
      const int pos = tree_pos(f);
159✔
7029
      if (mask_test(&have, pos))
159✔
7030
         sem_error(e, "duplicate mode view element definition for field %pI",
1✔
7031
                   tree_ident(e));
7032

7033
      mask_set(&have, pos);
158✔
7034

7035
      switch (tree_subkind(e)) {
158✔
7036
      case PORT_LINKAGE:
1✔
7037
         sem_error(e, "element mode indication cannot have mode LINKAGE");
1✔
7038

7039
      case PORT_RECORD_VIEW:
18✔
7040
      case PORT_ARRAY_VIEW:
7041
         {
7042
            tree_t name = tree_value(e);
18✔
7043
            type_t type = tree_type(e);
18✔
7044
            type_t view_type = tree_type(name);
18✔
7045

7046
            if (type_is_none(view_type))
18✔
7047
               return false;
7048

7049
            if (type_kind(view_type) != T_VIEW)
18✔
7050
               sem_error(name, "name in element mode view indication of field "
1✔
7051
                         "%pI does not denote a mode view", tree_ident(f));
7052

7053
            type_t elem_type = type;
17✔
7054
            if (tree_subkind(e) == PORT_ARRAY_VIEW) {
17✔
7055
               if (!type_is_array(type))
2✔
7056
                  sem_error(e, "field %pI with array mode view indication has "
1✔
7057
                            "non-array type %pT", tree_ident(f), type);
7058

7059
               elem_type = type_elem(type);
1✔
7060
            }
7061

7062
            if (!type_eq(elem_type, type_designated(view_type)))
16✔
7063
               sem_error(e, "field %pI subtype %pT is not compatible with mode "
1✔
7064
                         "view %pT", tree_ident(f), elem_type, view_type);
7065
         }
7066
         break;
7067
      }
7068
   }
7069

7070
   if (mask_popcount(&have) != nfields) {
67✔
7071
      LOCAL_TEXT_BUF tb = tb_new();
1✔
7072
      for (int i = 0, missing = 0; i < nfields; i++) {
5✔
7073
         if (!mask_test(&have, i))
4✔
7074
            tb_printf(tb, "%s%s", missing++ > 0 ? ", " : "",
3✔
7075
                      istr(tree_ident(type_field(rtype, i))));
7076
      }
7077

7078
      sem_error(t, "missing mode view element defintion for %s", tb_get(tb));
1✔
7079
   }
7080

7081
   return true;
7082
}
7083

7084
static bool sem_check_cond_value(tree_t t, nametab_t *tab)
50✔
7085
{
7086
   type_t type = tree_type(t);
50✔
7087
   if (type_is_none(type))
50✔
7088
      return false;
7089

7090
   type_t std_bool = std_type(NULL, STD_BOOLEAN);
50✔
7091

7092
   const int nconds = tree_conds(t);
50✔
7093
   for (int i = 0; i < nconds; i++) {
162✔
7094
      tree_t cond = tree_cond(t, i);
116✔
7095
      assert(tree_kind(cond) == T_COND_EXPR);
116✔
7096

7097
      if (tree_has_value(cond)) {
116✔
7098
         tree_t value = tree_value(cond);
72✔
7099
         if (!sem_check(value, tab))
72✔
7100
            return false;
7101

7102
         if (!sem_check_type(value, std_bool, tab))
72✔
7103
            sem_error(value, "type of condition must be %pT but have %pT",
2✔
7104
                      std_bool, tree_type(value));
7105
      }
7106
      else
7107
         assert(i == nconds - 1);
44✔
7108

7109
      if (tree_has_result(cond)) {
114✔
7110
         tree_t result = tree_result(cond);
104✔
7111
         if (!sem_check(result, tab))
104✔
7112
            return false;
7113

7114
         if (!sem_check_type(result, type, tab))
104✔
7115
            sem_error(result, "expected type of conditional expression to be "
114✔
7116
                      "%pT but is %pT", type, tree_type(result));
7117
      }
7118
   }
7119

7120
   return true;
7121
}
7122

7123
static bool sem_check_prot_decl(tree_t t, nametab_t *tab)
218✔
7124
{
7125
   const int ndecls = tree_decls(t);
218✔
7126
   for (int i = 0; i < ndecls; i++) {
703✔
7127
      tree_t d = tree_decl(t, i);
486✔
7128
      if (tree_kind(d) == T_ALIAS) {
486✔
7129
         // LRM 19 section 5.6.2: it is an error if an alias declared
7130
         // within a protected type declaration denotes anything other
7131
         // than a method of a protected type
7132
         tree_t value = tree_value(d);
15✔
7133
         if (tree_kind(value) != T_PROT_REF)
15✔
7134
            sem_error(d, "an alias declared within a protected type "
486✔
7135
                      "declaration must denote a protected type method");
7136
      }
7137
   }
7138

7139
   defer_check(tab, sem_missing_body_cb, t);
217✔
7140
   return true;
217✔
7141
}
7142

7143
static bool sem_check_inertial(tree_t t, nametab_t *tab)
18✔
7144
{
7145
   if (!sem_check(tree_value(t), tab))
18✔
UNCOV
7146
      return false;
×
7147

7148
   return true;
7149
}
7150

7151
static bool sem_check_psl_union(tree_t t, nametab_t *tab)
25✔
7152
{
7153
   psl_check(tree_psl(t), tab);
25✔
7154
   return true;
25✔
7155
}
7156

7157
bool sem_check(tree_t t, nametab_t *tab)
739,594✔
7158
{
7159
   switch (tree_kind(t)) {
739,594✔
7160
   case T_ARCH:
5,350✔
7161
      return sem_check_arch(t, tab);
5,350✔
7162
   case T_PACKAGE:
1,531✔
7163
      return sem_check_package(t, tab);
1,531✔
7164
   case T_ENTITY:
5,338✔
7165
      return sem_check_entity(t, tab);
5,338✔
7166
   case T_TYPE_DECL:
5,231✔
7167
      return sem_check_type_decl(t, tab);
5,231✔
7168
   case T_SUBTYPE_DECL:
1,549✔
7169
      return sem_check_subtype_decl(t, tab);
1,549✔
7170
   case T_PORT_DECL:
3,988✔
7171
      return sem_check_port_decl(t, tab);
3,988✔
7172
   case T_PARAM_DECL:
34,592✔
7173
      return sem_check_param_decl(t, tab);
34,592✔
7174
   case T_GENERIC_DECL:
2,398✔
7175
      return sem_check_generic_decl(t, tab);
2,398✔
7176
   case T_SIGNAL_DECL:
6,739✔
7177
      return sem_check_signal_decl(t, tab);
6,739✔
7178
   case T_VAR_DECL:
12,671✔
7179
      return sem_check_var_decl(t, tab);
12,671✔
7180
   case T_CONST_DECL:
7,123✔
7181
      return sem_check_const_decl(t, tab);
7,123✔
7182
   case T_PROCESS:
5,158✔
7183
      return sem_check_process(t, tab);
5,158✔
7184
   case T_VAR_ASSIGN:
18,695✔
7185
      return sem_check_var_assign(t, tab);
18,695✔
7186
   case T_SIGNAL_ASSIGN:
5,690✔
7187
      return sem_check_signal_assign(t, tab);
5,690✔
7188
   case T_FCALL:
87,750✔
7189
   case T_PROT_FCALL:
7190
      return sem_check_fcall(t, tab);
87,750✔
7191
   case T_LITERAL:
96,345✔
7192
      return sem_check_literal(t);
96,345✔
7193
   case T_STRING:
26,038✔
7194
      return sem_check_string_literal(t);
26,038✔
7195
   case T_REF:
194,262✔
7196
      return sem_check_ref(t, tab);
194,262✔
7197
   case T_WAIT:
10,228✔
7198
      return sem_check_wait(t, tab);
10,228✔
7199
   case T_ASSERT:
18,036✔
7200
      return sem_check_assert(t, tab);
18,036✔
7201
   case T_REPORT:
2,190✔
7202
      return sem_check_report(t, tab);
2,190✔
7203
   case T_QUALIFIED:
4,074✔
7204
      return sem_check_qualified(t, tab);
4,074✔
7205
   case T_FUNC_DECL:
6,240✔
7206
      return sem_check_func_decl(t, tab);
6,240✔
7207
   case T_AGGREGATE:
9,760✔
7208
      return sem_check_aggregate(t, tab);
9,760✔
7209
   case T_ATTR_REF:
22,023✔
7210
      return sem_check_attr_ref(t, false, tab);
22,023✔
7211
   case T_ARRAY_REF:
16,474✔
7212
      return sem_check_array_ref(t, tab);
16,474✔
7213
   case T_ARRAY_SLICE:
2,406✔
7214
      return sem_check_array_slice(t, tab);
2,406✔
7215
   case T_INSTANCE:
1,611✔
7216
      return sem_check_instance(t, tab);
1,611✔
7217
   case T_IF:
9,358✔
7218
      return sem_check_if(t, tab);
9,358✔
7219
   case T_NULL:
7220
      return true;
7221
   case T_PACK_BODY:
810✔
7222
      return sem_check_pack_body(t, tab);
810✔
7223
   case T_FUNC_BODY:
7,740✔
7224
      return sem_check_func_body(t, tab);
7,740✔
7225
   case T_RETURN:
13,657✔
7226
      return sem_check_return(t, tab);
13,657✔
7227
   case T_COND_RETURN:
7✔
7228
      return sem_check_cond_return(t, tab);
7✔
7229
   case T_COND_ASSIGN:
2,289✔
7230
      return sem_check_cond_assign(t, tab);
2,289✔
7231
   case T_WHILE:
219✔
7232
      return sem_check_while(t, tab);
219✔
7233
   case T_ALIAS:
1,616✔
7234
      return sem_check_alias(t, tab);
1,616✔
7235
   case T_FOR:
2,755✔
7236
      return sem_check_for(t, tab);
2,755✔
7237
   case T_PROC_DECL:
1,201✔
7238
      return sem_check_proc_decl(t, tab);
1,201✔
7239
   case T_PROC_BODY:
2,380✔
7240
      return sem_check_proc_body(t, tab);
2,380✔
7241
   case T_BLOCK:
432✔
7242
      return sem_check_block(t, tab);
432✔
7243
   case T_CASE:
651✔
7244
   case T_SELECT:
7245
      return sem_check_case(t, tab);
651✔
7246
   case T_EXIT:
496✔
7247
   case T_NEXT:
7248
      return sem_check_loop_control(t, tab);
496✔
7249
   case T_PCALL:
8,996✔
7250
   case T_PROT_PCALL:
7251
      return sem_check_pcall(t, tab);
8,996✔
7252
   case T_ATTR_SPEC:
365✔
7253
      return sem_check_attr_spec(t, tab);
365✔
7254
   case T_ATTR_DECL:
124✔
7255
      return sem_check_attr_decl(t);
124✔
7256
   case T_COMPONENT:
372✔
7257
      return sem_check_component(t, tab);
372✔
7258
   case T_IF_GENERATE:
178✔
7259
      return sem_check_if_generate(t, tab);
178✔
7260
   case T_FOR_GENERATE:
181✔
7261
      return sem_check_for_generate(t, tab);
181✔
7262
   case T_CASE_GENERATE:
19✔
7263
      return sem_check_case(t, tab);
19✔
7264
   case T_OPEN:
84✔
7265
      return sem_check_open(t);
84✔
7266
   case T_FIELD_DECL:
3,988✔
7267
      return sem_check_field_decl(t);
3,988✔
7268
   case T_FILE_DECL:
166✔
7269
      return sem_check_file_decl(t, tab);
166✔
7270
   case T_NEW:
582✔
7271
      return sem_check_new(t, tab);
582✔
7272
   case T_ALL:
2,595✔
7273
      return sem_check_all(t, tab);
2,595✔
7274
   case T_RECORD_REF:
9,225✔
7275
      return sem_check_record_ref(t, tab);
9,225✔
7276
   case T_UNIT_DECL:
187✔
7277
      return sem_check_unit_decl(t);
187✔
7278
   case T_USE:
16,048✔
7279
      return sem_check_use_clause(t, tab);
16,048✔
7280
   case T_TYPE_CONV:
5,774✔
7281
      return sem_check_conversion(t, tab);
5,774✔
7282
   case T_SPEC:
228✔
7283
      return sem_check_spec(t, tab);
228✔
7284
   case T_BINDING:
208✔
7285
      return sem_check_binding(t, tab);
208✔
7286
   case T_LIBRARY:
27,157✔
7287
      return sem_check_library_clause(t, tab);
27,157✔
7288
   case T_CONFIGURATION:
116✔
7289
      return sem_check_configuration(t, tab);
116✔
7290
   case T_PROT_BODY:
214✔
7291
      return sem_check_prot_body(t, tab);
214✔
7292
   case T_CONTEXT:
19✔
7293
      return sem_check_context_decl(t, tab);
19✔
7294
   case T_CONTEXT_REF:
21✔
7295
      return sem_check_context_ref(t, tab);
21✔
7296
   case T_BLOCK_CONFIG:
168✔
7297
      return sem_check_block_config(t, tab);
168✔
7298
   case T_IMPLICIT_SIGNAL:
29✔
7299
      return sem_check_implicit_signal(t, tab);
29✔
7300
   case T_DISCONNECT:
14✔
7301
      return sem_check_disconnect(t, tab);
14✔
7302
   case T_GROUP:
7303
   case T_GROUP_TEMPLATE:
7304
   case T_BOX:
7305
   case T_PSL_DIRECT:
7306
   case T_PSL_DECL:
7307
   case T_PSL_FCALL:
7308
   case T_LOOP:
7309
   case T_SEQUENCE:
7310
      return true;
7311
   case T_PSL_UNION:
25✔
7312
      return sem_check_psl_union(t, tab);
25✔
7313
   case T_CONV_FUNC:
201✔
7314
      return sem_check_conv_func(t, tab);
201✔
7315
   case T_CONCURRENT:
3,481✔
7316
      return sem_check_concurrent(t, tab);
3,481✔
7317
   case T_PACK_INST:
285✔
7318
      return sem_check_pack_inst(t, tab);
285✔
7319
   case T_EXTERNAL_NAME:
184✔
7320
      return sem_check_external_name(t, tab);
184✔
7321
   case T_FORCE:
68✔
7322
      return sem_check_force(t, tab);
68✔
7323
   case T_RELEASE:
39✔
7324
      return sem_check_release(t, tab);
39✔
7325
   case T_PROT_REF:
13✔
7326
      return sem_check_prot_ref(t, tab);
13✔
7327
   case T_MATCH_CASE:
43✔
7328
   case T_MATCH_SELECT:
7329
      return sem_check_match_case(t, tab);
43✔
7330
   case T_FUNC_INST:
137✔
7331
   case T_PROC_INST:
7332
      return sem_check_subprogram_inst(t, tab);
137✔
7333
   case T_VIEW_DECL:
76✔
7334
      return sem_check_view_decl(t, tab);
76✔
7335
   case T_COND_VALUE:
50✔
7336
      return sem_check_cond_value(t, tab);
50✔
7337
   case T_PROT_DECL:
218✔
7338
      return sem_check_prot_decl(t, tab);
218✔
7339
   case T_INERTIAL:
18✔
7340
      return sem_check_inertial(t, tab);
18✔
UNCOV
7341
   default:
×
UNCOV
7342
      sem_error(t, "cannot check %s", tree_kind_str(tree_kind(t)));
×
7343
   }
7344
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc