โŒ About FreshRSS

Normal view

There are new articles available, click to refresh the page.
Before yesterdayNewest questions tagged ada - Stack Overflow

How do you use the Reference_Type in Ada Hashed_Maps?

I want to create a symbol table as a hash map and keep a list of pointers into the symbol table to keep track of symbol table entries that need to be updated. I probably can't use cursors as pointers since they might change when new items are added to the symbol table. I can't use access types as I don't know whether the Insert procedure stores the element passed to it or makes a copy and stores that. It looks like I'm supposed to use a Reference_Type. But the Reference_Type is causing me problems because it is unconstrained.

This doesn't work:

with Ada.Containers.Indefinite_Hashed_Maps;
with Ada.Containers.Vectors;
with Ada.Strings.Hash;

package Types is
type Memory_Address is mod 2**64;
type ID_Code        is (ID_VARIABLE, ID_LABEL); -- ...extend as necessary...

NULL_ADDRESS : constant Memory_Address := 0;

type Symbol_Table_Entry is record
    id      : ID_Code;
    address : Memory_Address;
end record;

package Symbol_Tables is new Ada.Containers.Indefinite_Hashed_Maps (
    Key_Type        => String,
    Element_Type    => Symbol_Table_Entry,
    Hash            => Ada.Strings.Hash,
    Equivalent_Keys => "="
);

subtype Symbol_Table is Symbol_Tables.Map;

subtype Symbol_Table_Pointer is Symbol_Tables.Reference_Type;

-- Quadruple: Red Dragon Book, p. 470

type Opcode is new Natural;

type Quadruple_List_Index is new Natural;

type Quadruple is record
    op     : Opcode;
    arg1   : Symbol_Table_Pointer;
    arg2   : Symbol_Table_Pointer;
    result : Symbol_Table_Pointer; -- lvalue or branch destination
end record;

package Quadruple_Lists is new Ada.Containers.Vectors (
    Index_Type   => Quadruple_List_Index,
    Element_Type => Quadruple
);

subtype Quadruple_List is Quadruple_Lists.Vector;

type Label_List is array(Natural range <>) of Quadruple_List_Index;

end Types;

I get "error: unconstrained subtype in component declaration" for the fields of Quadruple defined as Symbol_Table_Pointer. I have searched on the internet but have found no good examples of how to use Reference_Type. Could someone please explain how to make this work?

โŒ
โŒ