VHDL Issue Number: 2119 Language_Version VHDL-2002 Classification LRM Examples, Notes and Appendices Summary Can't declare a protected type and object of that type in a single package Relevant_LRM_Sections 12.3.1.2 Type declarations 12.3.1.4 Object declarations Related_Issues Key_Words_and_Phrases protected body package shared variable Authors_Name Paul Butler Authors_Phone_Number 512 683 8743 Authors_Fax_Number Authors_Email_Address Paul.Butler@ni.com Authors_Affiliation National Instruments Authors_Address1 11500 N. Mopac Authors_Address2 Austin, TX 78759 Authors_Address3 Current Status: ISAC-Approved Superseded By: ------------------------ Date Submitted: 28 June 2007 Date Analyzed: 4 August 2007 Author of Analysis: Peter Ashenden Revision Number: 3 Date Last Revised: 18 October 2007 Description of Problem ---------------------- If a package contains a protected type declaration, the package body contains the protected body declaration. The elaboration rules seem to disallow declaring a shared variable of the protected type in the same package (the object elaboration includes elaborating its private data, but that cannot happen prior to elaborating the protected body). On the other hand, if I declare a protected type in an architecture declarative region, the protected body appears in the same region. In this case, the elaboration rules appear to allow me to declare a shared variable in the same region as the variable's type declaration. The difference in these cases is subtle; it caught me off guard and exposed a bug in my simulator. Proposed Resolution ------------------- I think declaring a type and objects of that type in the same package is common. If the restriction against shared variables in packages cannot be relaxed (assuming this analysis is correct), at least add some notes or example code in the LRM to point out the consequences of the elaboration rules. VASG-ISAC Analysis & Rationale ------------------------------ The submitter is correct that a shared variable cannot be declared in a package declaration if the type of the variable is a protected type also declared in the package declaration, and that this restriction follows from the elaboration rules. 12.3.1.4 specifies: The elaboration of an object of a protected type consists of the elaboration of the subtype indication, followed by creation of the object. Creation of the object consists of elaborating, in the order given, each of the declarative items in the protected type body. In order to proceed with elaboration of the declarative items in the protected type body, the protected type body must have been elaborated, since, as specified in 12.3.1.2: Elaboration of a protected type body has no effect other than to establish that the body, from then on, can be used during the elaboration of objects of the given protected type. For a protected type declared in a package declaration, the corresponding protected type body must be declared in the package body corresponding to the package declaration. This follows from the rule in 2.5 that a protected type body must not appear in a package declaration, together with the rule in 3.5 that each protected type declaration must have a protected type body immediately within the same declarative region, and the rule in 10.1 that a package declaration together with the corresponding package body jointly constitute a declarative region. Given a protected type declared in a package declaration, the corresponding protected type body is elaborated during elaboration of the package body. The precise time at which the package body is elaborated is not specified, but according to 12.1, it occurs eventually, after elaboration of the package declaration. Elaboration of a shared variable declaration within the package declaration occurs during elaboration of the package declaration. Consequently, the shared variable cannot be of the protected type declared in the package, since it cannot be elaborated. The submitter suggests that this situation does not obtain if the protected type and shared variable are both declared in an architecture body. That is true, provided the shared variable declaration occurs after the protected type body. 12.3 specifies that declarations in a declarative part are elaborated in order. If the shared variable is declared after the protected type body, the protected type body is elaborated prior to elaboration of the shared variable declaration, and so the latter can proceed. If the shared variable were declared before the protected type body (but after the protected type declaration), elaboration of the shared variable declaration could not proceed. The difference between this case and that of the declarations occurring in a package is that it is possible to order the declarations in the architecture body to enable elaboration, whereas it is not possible to so order the declarations in the package. The submitter suggests that the use case of declaring a protected type and a shared variable of that type in a package is common. The ISAC has not previously been apprised of such use cases. The ISAC notes that there are analogous cases of declaration and use in a package that are prohibited and that have not, to date, been the subject of issue reports. Two examples are: 1. Declaration and use of deferred constants: A deferred constant is not given a value until elaboration of the full constant declaration in the package body. 2.6 limits the use of the deferred constant within the package declaration to certain cases that do not require the value of the constant to be determined during elaboration of the usage. In particular, the constant cannot be used in the initial value expression of another constant. 2. A function declared in a package declaration cannot be used in an initial value expression of a constant declaration in the package. Elaboration of the constant declaration would require calling the function, which is not permitted until the function body in the package declaration has been elaborated. The use case described by the submitter, that of declaring a protected type and a shared variable in a package, can be implemented by separating the package into two distinct packages, one declaring the protected type, and the other declaring the shared variable. Elaboration of the shared variable declaration in the second package involves elaboration of both the first package declaration and its corresponding package body as prerequisite. Given that the restriction described by the submitter is one of a number of related restrictions, and that there is a work around, the ISAC is of the view that no change to the interpretation of 1076-2002 is required, and does not recommend change in future revisions. Should compelling use cases be identified, a requirement for language enhancement may be forwarded to the VASG. VASG-ISAC Recommendation for IEEE Std 1076-2002 ----------------------------------------------- No change. VASG-ISAC Recommendation for Future Revisions --------------------------------------------- No change.