File: map_named_args.cc

package info (click to toggle)
iverilog 13.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 34,160 kB
  • sloc: cpp: 114,001; ansic: 65,058; yacc: 10,610; sh: 4,286; vhdl: 3,246; makefile: 1,884; perl: 1,813; python: 579; csh: 2
file content (75 lines) | stat: -rw-r--r-- 1,866 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// SPDX-FileCopyrightText: 2023-2025 Lars-Peter Clausen <lars@metafoo.de>
// SPDX-License-Identifier: GPL-2.0-or-later

#include "PExpr.h"
#include "ivl_assert.h"
#include "map_named_args.h"
#include "netlist.h"

#include <iostream>

std::vector<PExpr*> map_named_args(Design *des,
			           const std::vector<perm_string> &names,
			           const std::vector<named_pexpr_t> &parms)
{
      std::vector<PExpr*> args(names.size());

      bool has_named = false;
      for (size_t i = 0; i < parms.size(); i++) {
	    if (parms[i].name.nil()) {
		  if (!parms[i].parm)
			continue;

		  if (has_named) {
		      std::cerr << parms[i].get_fileline() << ": error: "
		           << "Positional argument must preceded "
			   << "named arguments."
			   << std::endl;
		  } else if (i < args.size()) {
			args[i] = parms[i].parm;
		  }

		  continue;
	    }
	    has_named = true;

	    bool found = false;
	    for (size_t j = 0; j < names.size(); j++) {
		  if (names[j] == parms[i].name) {
			if (args[j]) {
			      std::cerr << parms[i].get_fileline() << ": error: "
			           << "Argument `"
				   << parms[i].name
				   << "` has already been specified."
				   << std::endl;
			      des->errors++;
			} else {
			      args[j] = parms[i].parm;
			}
			found = true;
			break;
		  }
	    }
	    if (!found) {
		  std::cerr << parms[i].get_fileline() << ": error: "
		       << "No argument called `"
		       << parms[i].name << "`."
		       << std::endl;
		  des->errors++;
	    }
      }

      return args;
}

std::vector<PExpr*> map_named_args(Design *des, const NetBaseDef *def,
				   const std::vector<named_pexpr_t> &parms,
				   unsigned int off)
{
      std::vector<perm_string> names;

      for (size_t j = off; j < def->port_count(); j++)
	    names.push_back(def->port(j)->name());

      return map_named_args(des, names, parms);
}