• 沒有找到結果。

tag is 0 for Invalid, 1 for Valid

8.26 Interface classes

A set of classes may be created that can be viewed as all having a common set of behaviors. Such a common set of behaviors may be created using interface classes. An interface class makes it unnecessary for related classes to share a common abstract superclass or for that superclass to contain all method definitions needed by all subclasses. A non-interface class can be declared as implementing one or more interface classes. This creates a requirement for the non-interface class to provide implementations for a set of methods that shall satisfy the requirements of a virtual method override (see 8.20).

An interfaceclass shall only contain pure virtual methods (see 8.21), type declarations (see 6.18), and parameter declarations (see 6.20, 8.25). Constraint blocks, covergroups, and nested classes (see 8.23) shall

VERIFICATION LANGUAGE

not be allowed in an interface class. An interface class shall not be nested within another class. An interface class can inherit from one or more interface classes through the extends keyword, meaning that it inherits all the member types, pure virtual methods and parameters of the interface classes it extends, except for any member types and parameters that it may hide. In the case of multiple inheritance, name conflicts may occur that must be resolved (see 8.26.6).

Classes can implement one or more interface classes through the implements keyword. No member types or parameters are inherited through the implements keyword. A subclass implicitly implements all of the interface classes implemented by its superclass. In the following example, class C implicitly implements interface class A and has all of the requirements and capabilities as if it explicitly implemented interface class A:

Each pure virtual method from an interface class shall have a virtual method implementation in order to be implemented by a non-abstract class. When an interface class is implemented by a class, the required implementations of interface class methods may be provided by inherited virtual method implementations.

A virtual class shall define or inherit a pure virtual method prototype or virtual method implementation for each purevirtual method prototype in each implemented interfaceclass. The keyword virtual shall be used unless the virtual method is inherited.

A variable whose declared type is an interface class type may have as its value a reference to any instance of a class that implements the specified interface class (see 8.22). It is not sufficient that a class provides implementations for all the pure virtual methods of an interface class; the class or one of its superclasses shall be declared to implement the interface class through the implements keyword, or else the class does not implement the interface class.

The following is a simple example of interface classes.

interface class PutImp#(type PUT_T = logic);

pure virtual function void put(PUT_T a);

endclass

interface class GetImp#(type GET_T = logic);

pure virtual function GET_T get();

endclass

class Fifo#(type T = logic, int DEPTH=1) implements PutImp#(T), GetImp#(T);

T myFifo [$:DEPTH-1];

virtual function void put(T a);

myFifo.push_back(a);

class Stack#(type T = logic, int DEPTH=1) implements PutImp#(T), GetImp#(T);

T myFifo [$:DEPTH-1];

virtual function void put(T a);

myFifo.push_front(a);

endfunction

virtual function T get();

get = myFifo.pop_front();

endfunction endclass

The example has two interface classes, PutImp and GetImp, which contain prototype pure virtual methods put and get. The Fifo and Stack classes use the keyword implements to implement the PutImp and GetImp interface classes and they provide implementations for put and get. These classes therefore share common behaviors without sharing a common implementation.

8.26.1 Interface class syntax

interface_class_declaration ::= // from A.1.2

interfaceclass class_identifier [ parameter_port_list ] [ extends interface_class_type { , interface_class_type } ] ; { interface_class_item }

endclass [ : class_identifier]

interface_class_item ::=

type_declaration

| { attribute_instance } interface_class_method

| local_parameter_declaration ;

| parameter_declaration7;

| ;

interface_class_method ::=

purevirtual method_prototype ;

7) In a parameter_declaration that is a class_item, the parameter keyword shall be a synonym for the localparam keyword.

Syntax 8-3—Class syntax (excerpt from Annex A)

8.26.2 Extends versus implements

Conceptually extends is a mechanism to add to or modify the behavior of a superclass while implements is a requirement to provide implementations for the pure virtual methods in an interface class. When a class is extended, all members of the class are inherited into the subclass. When an interface class is implemented, nothing is inherited.

An interface class may extend, but not implement, one or more interface classes, meaning that the interface subclass inherits members from multiple interface classes and may add additional member types, pure virtual method prototypes, and parameters. A class or virtual class may implement, but not extend, one or more interface classes. Because virtual classes are abstract, they are not required to fully define the methods from their implemented classes (see 8.26.7). The following highlights these differences:

— An interface class

— may extend zero or more interface classes

— may not implement an interface class

— may not extend a class or virtual class

— may not implement a class or virtual class

VERIFICATION LANGUAGE

— A class or virtual class

— may not extend an interface class

— may implement zero or more interface classes

— may extend at most one other class or virtual class

— may not implement a class or virtual class

— may simultaneously extend a class and implement interface classes

In the following example, a class is both extending a base class and implementing two interface classes:

interface class PutImp#(type PUT_T = logic);

pure virtual function void put(PUT_T a);

endclass

interface class GetImp#(type GET_T = logic);

pure virtual function GET_T get();

endclass

class MyQueue #(type T = logic, int DEPTH = 1);

T PipeQueue[$:DEPTH-1];

virtual function void put(T a);

PipeQueue.push_back(a);

In this example, the PipeQueue property and deleteQ method are inherited in the Fifo class. In addition the Fifo class is also implementing the PutImp and GetImp interface classes so it shall provide implementations for the put and get methods, respectively.

The following example demonstrates that multiple types can be parameterized in the class definition and the resolved types used in the implemented classes PutImp and GetImp.

virtual class XFifo#(type T_in = logic, type T_out = logic, int DEPTH = 1) extends MyQueue#(T_out)

implements PutImp#(T_in), GetImp#(T_out);

pure virtual function T_out translate(T_in a);

virtual function void put(T_in a);

PipeQueue.push_back(translate(a));

An inherited virtual method can provide the implementation for a method of an implemented interface class.

Here is an example: