General Information
Tutorials
Reference Manuals
Libraries
Translation Tasks
Tools
Administration
|
Tutorial on Type AnalysisArray TypesWe now add array types to our language. We specify that two array types are structural equivalent if their element types are equivalent, and if the types have the same number of elements. Hence, type equivalence is not only determined by the component types. Here is an example program that uses arrays, records, and type definitions in combination: ArrayExamp[63]== begin var int k; var int[5] pi, int[5] pj; var record int i, bool b, real[3] r end [2] rv; type bool[4] bt; var bt vbt, bt wbt; var real[6][7] m; pi[1] = k; vbt = wbt; rv[2].b = true; rv[1].r[k] = 3.2; m[1][k] = 1.0; end This macro is attached to a product file. We extend the grammar by notations for array type denoters and by indexed variables: Abstract array syntax[64]== RULE: TypeDenoter ::= ArrayType END; RULE: ArrayType ::= TypeDenoter '[' ArraySize ']' END; RULE: ArraySize ::= IntNumber END; RULE: Variable ::= Variable '[' Expression ']' END; This macro is invoked in definition 74. In this language an array type is described by two properties: the element type and the number of elements: Array type properties[65]== ElemType: DefTableKey; ElemNo: int; This macro is invoked in definition 73.
In the context of a type denotation for an Array type denoter[66]== SYMBOL ArrayType INHERITS TypeDenotation END; RULE: ArrayType ::= TypeDenoter '[' ArraySize ']' COMPUTE .GotTypeProp = ORDER (ResetElemType (ArrayType.Type, TypeDenoter.Type), ResetElemNo (ArrayType.Type, ArraySize.Size), ResetTypeName (ArrayType.Type, "array..."), ResetTypeLine (ArrayType.Type, LINE)); END; TERM IntNumber: int; SYMBOL ArraySize: Size: int; RULE: ArraySize ::= IntNumber COMPUTE ArraySize.Size = IntNumber; END; RULE: TypeDenoter ::= ArrayType COMPUTE TypeDenoter.Type = ArrayType.Type; END; This macro is invoked in definition 74.
Finally it is stated that array elements of type
void are not allowed. We can not simply compare RULE: ArrayType ::= TypeDenoter '[' ArraySize ']' COMPUTE IF (EQ (FinalType (TypeDenoter.Type), voidType), message (ERROR, "Wrong element type", 0, COORDREF)) <- INCLUDING Program.TypeIsSet; END; This macro is invoked in definition 74. Two array types are equivalent if and only if their element types are equivalent and if they have the same number of elements. In order to state the equivalence with respect to array sizes, we establish a bijective mapping between any array size that occurs in the program and a definition table key. That number mapping is computed by turning an array size into an identifier and then binding that identifier in a scope that serves just this purpose. Size mapping[68]== $/Tech/MakeName.gnrc:inst $/Name/CScope.gnrc+instance=SizeMap :inst This macro is invoked in definition 72. Array size mapping[69]== SYMBOL ArraySize INHERITS SizeMapIdDefScope END; RULE: ArraySize ::= IntNumber COMPUTE ArraySize.Sym = IdnNumb (0, IntNumber); END; This macro is invoked in definition 74.
The Array type equivalence[70]== RULE: ArrayType ::= TypeDenoter '[' ArraySize ']' COMPUTE ArrayType.GotType += AddTypeToBlock (ArrayType.Type, ArraySize.Key, VResetComponentTypes (ArrayType.Type, SingleDefTableKeyList (TypeDenoter.Type))) <- .GotTypeProp; END; This macro is invoked in definition 74.
Type analysis in the context of an indexed variable is
specified as a join of three expression subtrees:
The subcript expression is a separate expression subtree.
It has to be of type int, as specified by its Indexing[71]== RULE: Variable ::= Variable '[' Expression ']' COMPUTE PrimaryContext (Variable[1], GetElemType (Variable[2].Type, NoKey)); IF (EQ (GetElemType (Variable[2].Type, NoKey), NoKey), message (ERROR, "Not an array", 0, COORDREF)); Expression.Required = intType; END; This macro is invoked in definition 74. Array.specs[72]== Size mapping[68] This macro is attached to a product file. Array.pdl[73]== Array type properties[65] This macro is attached to a product file. Array.lido[74]== Abstract array syntax[64] Array type denoter[66] Array check element type [67] Array size mapping[69] Array type equivalence[70] Indexing[71] This macro is attached to a product file.
|