Commit Graph

89 Commits

Author SHA1 Message Date
Cary R 860761f9c6 More cppcheck fixes - part 2 2025-10-20 23:54:15 -07:00
Martin Whitaker e22831553d Improve identifier lexical position accuracy in declarations.
Enhance the lists of identifiers and declaration assignments generated
by the parser to associate each identifier with its lexical_pos. Also do
this for single items in complex parser rules where the location passed
to the pform is not the location of the identifier.
2024-02-19 18:16:35 +00:00
Lars-Peter Clausen f6a51bc9db Add support for binding function/task arguments by name
In addition to providing positional arguments for task and functions
SystemVerilog allows to bind arguments by name. This is similar to how
module ports can be bound by name.

```
task t(int a, int b); ... endtask
...
t(.b(1), .a(2));
```

Extend the parser and elaboration stage to be able to handle this. During
elaboration the named argument list is transformed into a purely positional
list so that later stages like synthesis do not have to care about the
names.

For system functions and tasks all arguments must be unnamed, otherwise an
error will be reported.

In addition to functions and tasks arguments can also be bound by name for
the various different ways of invoking a class constructor.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2023-08-20 08:20:54 -07:00
Lars-Peter Clausen 520b00095c Remove unused `named_number_t` type
The last user of the named_number_t type was removed in commit 2f474358d9
("2f474358d99929ec625a46690d1be6939ed67064"). Remove the type as well.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2023-08-19 10:12:00 -07:00
Lars-Peter Clausen e24aa18a80 Add common implementation for scoped symbol search
In SystemVerilog identifiers can usually have an additional package scope
in which they should be resolved. At the moment there are many places in
the code base that handle the resolution of the package scope.

Add a common data type for package scoped paths as well as a
symbol_search() variant that works on package scoped identifiers. This
allows to handle package scope resolution in a central place.

Having the code in a central place makes it easier to ensure consistent and
correct behavior. E.g. there are currently some corner case bugs that are
common to all implementations. With the common implementation it only has
to be fixed in one place.

It will also make it easier to eventually implement class scoped
identifiers.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2023-01-16 12:26:24 -08:00
Lars-Peter Clausen e0ec2ed386 Do not allow to create objects of virtual classes
It is not allowed to create objects of virtual classes. Currently the
virtual keyword is accepted by the parser, but otherwise ignored.

Keep track of whether a class is virtual and generate an error when the
class new operator is used for a virtual type.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2022-12-13 21:27:53 -08:00
Lars-Peter Clausen 5ef847ea87 Support type parameters
SystemVerilog supports type parameters. These are similar to value
parameters, but they allow to pass a type to a module or similar when
instantiating it.

E.g.

```
module A #(parameter type T = int);
endmodule

module B;
  A #(.T(real)) i_a;
endmodule
```

Add support for handling type parameters.

For the vlog95 and vhdl backends type parameters, similar to typedefs, get
replaced with their actual value. For modules with non-local type
parameters for each module instance a unique module or architecture is
generated with the actual type.

Querying type parameters through VPI is not yet supported.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2022-12-11 13:50:14 -08:00
Lars-Peter Clausen cdc9629ce7 Add support for forward type declarations
SystemVerilog supports forward type declarations. This allows to declare a
type identifier and use it, e.g. in a signal declaration, before declaring
what the actual type is. The type still needs to be fully defined
eventually in the same scope as its forward type declaration. E.g.

```
typedef T;
T x;
typedef int T;
```

The forward type definition can also contain the kind of the type it is
going to be. E.g struct, union, class, etc. The LRM calls this the basic
type. If the actual type is not of the basic type specified in the forward
type declaration this is an error. E.g.

```
typedef struct T;
typedef int T; // Error, int is not a struct
```

It is legal to have more than one forward type declaration for the same
type name, as long as the basic type is the compatible. It is even legal to
have a forward type declaration after the actual type has already been
declared. E.g.

```
typedef T;
typedef int T;
typedef T;
```

Implement support for forward type definitions as part of the new
typedef_t. The basic type will be attached to the typedef_t.

The compatibility of the basic type for multiple forward type declarations
will be checked in the parser. The compatibility of the basic type to the
actual type will be checked during elaboration, once the actual type is
known.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2022-12-11 10:19:59 -08:00
Lars-Peter Clausen 8ee9d6b5ea Detect circular type definitions
With forward type declarations it is possible to create a circular type
definition where a type resolves to itself. E.g.

```
typedef T1;
typedef T1 T2;
typedef T2 T1;
```

Flag a type as elaborating when elaboration of the type is started and
clear it when elaboration finishes. If the elaboration function is entered
again while the flag is still set a circular type has been detected and an
error is reported.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2022-12-11 10:18:23 -08:00
Lars-Peter Clausen 2e0d6d5af1 Allow to attach additional information to typedefs
Currently typedefs are just a pointer to a data_type_t.

Currently typedefs are implemented by setting the name field of a
data_type_t when a typedef of the type is declared. This works mostly, but
there are some corner cases that can't be supported.

E.g. a typedef of a typedef does not work as it overwrites the name field
of the same data_type_t multiple times.

Forward typedefs can also not be supported since forward typedefs allow to
reference a type before it has been declared.

There are also some problems with type identifier references from a
higher-level scope if there is a type identifier in the current scope with
the same name, but it is declared after the type identifier has been
referenced. E.g. in the following x should be a vector fo width 8, but it
will be a vector of width 4, because while the right type is used it is
elaborated in the wrong scope.

```
localparam A = 8;
typedef logic [A-1:0] T;
module M;
  localparam A = 4;
  T x;
  typedef int T;
endmodule
```

Furthermore typedefs used for the type of ports are elaborated in the wrong
scope.

To handle these corner case issues introduce a data_type_t for typedefs.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2022-12-11 10:18:22 -08:00
Lars-Peter Clausen b7fce4a66e Improve handling of type identifier references
Currently when referencing a typedef this gets replaced with the
`data_type_t` that the typedef points to. This works for most cases, but
there are some corner cases where it breaks down.

E.g. it is possible to have a scoped type identifier which references a
type defined in a package. For such type identifiers, only the data_type_t
itself is remembered, but not the package scope. This will cause the type
identifier to be elaborated in the wrong scope.

Furthermore type identifiers of vector types used for module or task port
might not be elaborated in the correct scope.

Introduce a new `typeref_t` which has `data_type_t` as a base type and can
be used as the data type for a signal. A new instance of a `typeref_t` is
created when referencing a type identifier. The `typeref_t` remembers both
the data type and the scope of the type identifier.

When elaborating the `typeref_t` the elaboration is passed through to the
referenced `data_type_t`. But special care is taken to lookup the right
scope first.

With the new approach also typedefs of typedefs are supported. This
previously did not work because chained typedefs all reference the same
`data_type_t`, but each typedef sets the `name` field of the `data_type_t`.
So the second typedef overwrites the first typedef and a lookup of the
scope of the first typedef by name will fail as it will return the scope of
the second typedef.

This refactoring also allows to define clear ownership of a data_type_t
instance. This e.g. means that an array type owns its base type and the
base type can be freed when the array type itself is freed. The same is
true for signals and class properties, they now own their data type and the
data type can be freed when the signal or property is freed.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2022-10-15 12:32:22 +02:00
Lars-Peter Clausen 0a2cbcb4b7 Remove pre C++11 compatibility code for unique_ptr
There is compatibility code that defines unique_ptr  as auto_ptr if the C++
version is before C++11.

But there are already other parts of the codebase that do require C++11 and
the minimum required version to build the project is C++11. So remove the
compat code as it is no longer needed.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2022-10-15 12:32:02 +02:00
Lars-Peter Clausen e799a09605 data_type_t: Remove figure_packed_base_type() method
The figure_packed_base_type() method can be used to check whether a type is
2-state or 4-state at parse time. The parser no longer cares about the
specific type of a data type. The figure_packed_base_type() function is
no longer used, so remove it.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2022-09-15 10:07:10 +02:00
Lars-Peter Clausen e15b125da8 Replace svector with std::vector
The custom `svector` class is essentially a subset of `std::vector`. There
is no inherent advantage to using `svector`. Both have the same memory
footprint.

`svector` was designed to be of static size, but there are a few places in
the parser where it has to grow at runtime. Handling this becomes a bit
easier by switching to `std::vector` since it is possible to use its
methods which take care of resizing the vector.

This also allows to remove the unused parameter of the `lgate` struct
constructor, which was only needed for compatibility with `svector`.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2022-05-15 21:56:48 +02:00
Lars-Peter Clausen dddc41891e lgate: Inherit from LineInfo
The lgate struct has its own fields for tracking file and line number,
while everything else that has this information attached inherits from the
LineInfo class.

Make lgate also inherit from LineInfo for consistency.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2022-04-24 17:00:46 +02:00
Lars-Peter Clausen 724d7d4282 Consolidate unpacked array type elaboration
There are currently two implementations for elaborating unpacked array
types. One that is used when elaborating a signal with an unpacked array
type and one that is used everywhere else using the elaborate_type()
infrastructure.

The elaborate_type() implementation is less complete and for example does
not support bounded queue types.

Consolidate both into a single implementation to reduce duplicated code and
get consistent behavior. This for example makes sure that the maximum queue
size is respected when used as a function return type.

Nested data structures of arrays, dynamic arrays or queues are not yet
supported. In the current implementation when encountering such a type an
assert will be triggered and the application crashes. In the new
implementation an error message will be printed without crashing the
application.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2022-04-18 09:58:02 +02:00
Lars-Peter Clausen bcae0304a3 Set line info on class properties
This will allow to generate error messages that point to the right line if
there is something wrong or not supported in a class property declaration.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2022-04-17 09:14:38 +02:00
Lars-Peter Clausen f6042033d0 Correctly handle separate port type declaration for `integer` and `time`
When using non-ANSI style port declarations it is possible to declare the
port direction and the data type for the port in separate statements. E.g.

```
input x;
reg x;
```

When using packed array dimensions they must match for both declarations.
E.g.

```
input [3:0] x;
reg [3:0] x;
```

But this only applies for vector types, i.e. the packed dimension is
explicitly declared. It does not apply to the `integer` and `time` types,
which have an implicit packed dimension.

The current implementation requires that even for `integer` and `time`
types the implicit dimension needs to be explicitly declared in the port
direction. E.g. the following will result in a elaboration error
complaining about a packed dimension mismatch.

```
module test;
  output x;
  integer x;
endmodule
```

Currently the parser creates a vector_type_t for `time` and `integer`. This
means that e.g. `time` and `reg [63:0]` are indistinguishable during
elaboration, even though they require different behavior.

To fix let the atom2_type_t handle `integer` and `time`. Since it no longer
exclusively handles 2-state types, rename it to atom_type_t.

This also fixes a problem with the vlog95 target unit tests. The vlog95
target translates

```
module test(output integer x);
endmodule
```

to

```
module test(x);
  output x;
  integer x;
endmodule
```

which then fails when being elaborated again. There were some regression
tests that were failing because of this that will now pass.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2022-03-28 10:40:06 +02:00
Lars-Peter Clausen 4bf0d62cd1 Support type identifier base type for enum
The base type for an enum type can be a type identifier for a typedef as
long as it resolves to a vector or integer type with at most one packed
dimension. This is described in section 6.19 ("Enumerations") of the LRM
(1800-2017). E.g.

```
typedef bit [3:0] T;
enum T {
 A
} e;
```

Add support for this by allowing to specify a type identifier as the base
type for an enum in the parser. During elaboration it is checked whether
the type identifier resolves to a valid enum base type.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2022-03-25 21:55:34 +01:00
Lars-Peter Clausen 7f4013ac66 data_type_t::elaborate_type(): Make return type const
For some data types the value returned by the `elaborate_type()` method is
shared among different signals of that type. E.g. all string or real types
get elaborated to the same ivl_type_s. This means the returned value must
not be modified, otherwise the data type for unrelated signals might get
changed.

To enforce this and protect against accidental breakage make the return
type of the `elaborate_type()` and the related `elaborate_type_raw()`
methods const.

Note that `ivl_type_t` is used for the new return type which is a typedef
for `const ivl_type_s*`.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2022-03-14 22:06:12 +01:00
Lars-Peter Clausen f67cdddecf Don't make input `integer` ports variables
In Verilog module input ports can only have a packed dimensions and a
signed flag, but no explicit data type.

In SystemVerilog an explicit data type can be specified for module input
ports. Such a port is a net, regardless of the data type, unless
explicitly made a variable using the `var` keyword.

This works for the most part in the current implementation, but for some
data types such as `reg` and `integer` the input port is turned into a
variable. And since input port's can't be variables in the current
implementation this results in an error.

Fix this by completely removing the `reg_flag` that is used to indicate
that a certain data type is always a variable. There is no such restriction
on data types for SystemVerilog and for Verilog there are already checks in
place that a input port can only have an implicit (or real) data type.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2022-03-03 10:30:28 +01:00
Lars-Peter Clausen 057cd700fe netenum_t: Fix line info
enum_type_t inherits from LineInfo, but also has a LineInfo field called
`li`.

When a enum_type_t is created the LineInfo of the object itself is set to
the location where the type is declared.

The `li` field gets set when a signal of the enum_type_t is created to the
location where the signal is created. The `li` field is then used when
elaborating a netenum_t to set the line information on the netenum_t.

This works fine when the enum is directly used to declare a signal, since
the location of the type and signal declaration are the same and there is
only one signal of that type.

But when using a typedef and declaring multiple signals with the same type
the `li` field will be repeatedly set and eventually point to the last
signal declaration of that type.

On the other hand when using or declaring an enum as part of an aggregate
type such as an array, struct or class the line info will never be
set.

This can cause misleading error messages. E.g.

```
typedef enum {
  A, B = A
} e_t;

struct packed {
  e_t e;
} s;
```

will generate

```
:0: error: Enumeration name B and A have the same value: 32'sd0
```

To fix this use the LineInfo that was assigned to the enum_type_t itself
when it was declared and remove the `li` field.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2022-01-23 20:07:00 +01:00
Lars-Peter Clausen 09ac7c207e Add support for signed packed structs/unions
packed structs and packed unions as a whole can either be signed or
unsigned. This information is used when it is used as a primary in an
expression, i.e. without accessing any of the members.

Add support for parsing and elaborating signed structs.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2022-01-16 16:16:24 +01:00
Lars-Peter Clausen adf64f3cc8 Fix base type for nested types with enum or atom2 sub-types
To determine the base type structs and packed arrays call the
figure_packed_base_type() for their sub-types.

This method is not defined for enum or atom2 types and the default
implementation returns IVL_VT_NO_TYPE.

As a result packed arrays of enum or atom2 types and packed structs with
members of enum or atom2 types get elaborated with IVL_VT_NO_TYPE
as the base type.

For example

```
struct packed {
  bit signed [31:0] x;
} s1;
```

gets elaborated with a base type of IVL_VT_BOOL, while

```
struct packed {
  int x;
} s2;
```

gets elaborated with a base type of IVL_VT_NONE.

To fix this define the figure_packed_base_type() for enum_type_t and
atom2_type_t.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2022-01-08 15:41:49 +01:00
Martin Whitaker ecbbb60fb6 Remove "using namespace std" from compiler header files and fix the fallout. 2021-11-04 16:55:03 +00:00
Stephen Williams 38b3c8efb2 Rework symbol_search function.
There are too many ad hoc handlers of symbol_search partial results.
Rewrite symbol_search to clean up things like partial results and
member/method detections. Use this reworked symbol_search function
to rewrite expression elaborate for the PECallFunction expressions.
2021-01-17 19:33:52 -08:00
Stephen Williams 752401b88c output ports of real type are variables, not wires. 2020-12-29 22:00:04 -08:00
Stephen Williams 16646c547c Rework parsing of parameter types
Use the common data_type_or_implicit rules to support type
definitions for parameters. This eliminates a bunch of special
rules in parse.y, and opens the door for parameters having
more complex types.
2020-12-27 21:17:57 -08:00
Stephen Williams 156644d91e Detect and complain about some constructor chain errors
This.new is not allowed.

super.new beyond the first statement is not allowed.

And while I'm at it, clean up the use of "@" and "#" in
the code as tokens for this and super.
2020-11-22 15:31:40 -08:00
Martin Whitaker 732a763188 Record typedef name in data_type_t struct.
This will be used to locate the scope where the type was declared.
2019-12-22 11:21:05 +00:00
Martin Whitaker b88d91c617 Create new base class for all named items that can be added to a scope.
Provide a helper function to identify the derived classes when reporting
errors.
2019-09-27 22:19:30 +01:00
Martin Whitaker 3612076943 Undo redefinition of unique_ptr at end of header files.
It seems that clang both defines __cplusplus < 201103L and provides
unique_ptr (GitHub issue #215).
2018-11-02 21:22:10 +00:00
Martin Whitaker 78317a2799 Fix auto_ptr deprecated warnings when building with recent GCC. 2018-10-06 17:15:31 +01:00
Martin Whitaker 6ba2bee977 Fix for br974 - support SV types in non-ansi port declarations. 2016-04-04 20:40:30 +01:00
Larry Doolittle 2739f83702 Spelling fixes in C and C++ comments 2015-06-04 15:00:29 -07:00
Cary R ac2e8dd6cd Report that packed arrays can not currently be elaborated in all cases 2014-11-24 19:11:08 -08:00
Cary R c25538d750 Pass the integer type for enumerations to the IVL target stage 2014-10-31 18:09:19 -07:00
Stephen Williams 712f394224 Elaborate classes in packages. 2014-10-10 18:53:53 -07:00
Stephen Williams 480668fee6 Add support for classes defined in $root scope. 2014-09-15 17:37:30 -07:00
Stephen Williams f602ae84ab Elaborate foreach loops as synthetic for loops.
Create an implicit scope to hold the index variable, and
generate a for loop to perform the functionality of the
foreach.
2014-08-21 16:44:46 -07:00
Stephen Williams 6d052d4ff7 Handle queue types and expressions at pform level. 2014-08-21 16:44:45 -07:00
Cary R d6b6b76015 Update header files to use a more standard name to prevent rereading
This is from github report #16. There are likely a few more issues
that need to be addressed though this takes care of the major ones.
2014-07-23 13:42:56 -07:00
Stephen Williams ec0c66ff25 Fix internal error elaborating types used in different scopes
Some types, i.e. vector types with parameterized dimensions,
may have different elaboration results in different scopes.
Handle those cases in the elaboration caches.
2014-05-08 15:05:08 -07:00
Stephen Williams fc42f40770 Fix problem with enum types in re-used modules
When a module is instantiated multiple times, the enum
types contained within would cause trouble. This fixes
that by elaborating in proper scope context.
2014-01-11 19:19:15 -08:00
Stephen Williams 6ec31517a9 Implement $bits(type) to get the size of a type
In the process, I implemented a means to get at
previously elaborated types from the pform type
pointer.
2014-01-11 19:19:15 -08:00
Stephen Williams 819770a6c4 Handle enumerations as packed struct/union members.
There were also some subtleties related to using enumerations
from typedefs and using them in multiple places. Fix various
bugs related to those issues.
2013-12-07 12:20:28 -08:00
Stephen Williams 49756a8e7a Unions through pform. 2013-12-07 12:20:28 -08:00
Stephen Williams 9bd9c8f301 Implement class extends arguments.
This implements constructor arguments right after the base class
name in the class extends syntax.
2013-11-10 18:03:19 -08:00
Stephen Williams 9f83882bcc Parse/elaborate class derivation
This gets us down to working classes, but does not yet implement
calls to the parent constructor, implicitly or explicitly.
2013-11-10 18:03:19 -08:00
Stephen Williams 5084a23417 Sorry messages for calls to superclass constructor. 2013-09-20 20:44:57 -07:00