1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
|
<HTML>
<HEAD>
<!-- This HTML file has been created by texi2html 1.29
from ../tnf/type.tnf on 12 Febuary 2003 -->
<TITLE>Type analysis tasks - Properties of Types</TITLE>
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#0000EE" VLINK="#551A8B" ALINK="#FF0000" BACKGROUND="gifs/bg.gif">
<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0" VALIGN=BOTTOM>
<TR VALIGN=BOTTOM>
<TD WIDTH="160" VALIGN=BOTTOM><IMG SRC="gifs/elilogo.gif" BORDER=0> </TD>
<TD WIDTH="25" VALIGN=BOTTOM><img src="gifs/empty.gif" WIDTH=25 HEIGHT=25></TD>
<TD ALIGN=LEFT WIDTH="600" VALIGN=BOTTOM><IMG SRC="gifs/title.gif"></TD>
</TR>
</TABLE>
<HR size=1 noshade width=785 align=left>
<TABLE BORDER=0 CELLSPACING=2 CELLPADDING=0>
<TR>
<TD VALIGN=TOP WIDTH="160">
<h4>General Information</h4>
<table BORDER=0 CELLSPACING=0 CELLPADDING=0>
<tr valign=top><td><img src="gifs/gelbekugel.gif" WIDTH=7 HEIGHT=7 ALT=" o"> </td><td><a href="index.html">Eli: Translator Construction Made Easy</a></td></tr>
<tr valign=top><td><img src="gifs/gelbekugel.gif" WIDTH=7 HEIGHT=7 ALT=" o"> </td><td><a href="gindex_toc.html">Global Index</a></td></tr>
<tr valign=top><td><img src="gifs/gelbekugel.gif" WIDTH=7 HEIGHT=7 ALT=" o"> </td><td><a href="faq_toc.html" >Frequently Asked Questions</a> </td></tr>
</table>
<h4>Tutorials</h4>
<table BORDER=0 CELLSPACING=0 CELLPADDING=0>
<tr valign=top><td><img src="gifs/gelbekugel.gif" WIDTH=7 HEIGHT=7 ALT=" o"> </td><td><a href="EliRefCard_toc.html">Quick Reference Card</a></td></tr>
<tr valign=top><td><img src="gifs/gelbekugel.gif" WIDTH=7 HEIGHT=7 ALT=" o"> </td><td><a href="novice_toc.html">Guide For new Eli Users</a></td></tr>
<tr valign=top><td><img src="gifs/gelbekugel.gif" WIDTH=7 HEIGHT=7 ALT=" o"> </td><td><a href="news_toc.html">Release Notes of Eli</a></td></tr>
<tr valign=top><td><img src="gifs/gelbekugel.gif" WIDTH=7 HEIGHT=7 ALT=" o"> </td><td><a href="nametutorial_toc.html">Tutorial on Name Analysis</a></td></tr>
<tr valign=top><td><img src="gifs/gelbekugel.gif" WIDTH=7 HEIGHT=7 ALT=" o"> </td><td><a href="typetutorial_toc.html">Tutorial on Type Analysis</a></td></tr>
</table>
<h4>Reference Manuals</h4>
<table BORDER=0 CELLSPACING=0 CELLPADDING=0>
<tr valign=top><td><img src="gifs/gelbekugel.gif" WIDTH=7 HEIGHT=7 ALT=" o"> </td><td><a href="ui_toc.html">User Interface</a></td></tr>
<tr valign=top><td><img src="gifs/gelbekugel.gif" WIDTH=7 HEIGHT=7 ALT=" o"> </td><td><a href="pp_toc.html">Eli products and parameters</a></td></tr>
<tr valign=top><td><img src="gifs/gelbekugel.gif" WIDTH=7 HEIGHT=7 ALT=" o"> </td><td><a href="lidoref_toc.html">LIDO Reference Manual</a></td></tr>
</table>
<h4>Libraries</h4>
<table BORDER=0 CELLSPACING=0 CELLPADDING=0>
<tr valign=top><td><img src="gifs/gelbekugel.gif" WIDTH=7 HEIGHT=7 ALT=" o"> </td><td><a href="lib_toc.html">Eli library routines</a></td></tr>
<tr valign=top><td><img src="gifs/gelbekugel.gif" WIDTH=7 HEIGHT=7 ALT=" o"> </td><td><a href="modlib_toc.html">Specification Module Library</a></td></tr>
</table>
<h4>Translation Tasks</h4>
<table BORDER=0 CELLSPACING=0 CELLPADDING=0>
<tr valign=top><td><img src="gifs/gelbekugel.gif" WIDTH=7 HEIGHT=7 ALT=" o"> </td><td><a href="lex_toc.html">Lexical analysis specification</a></td></tr>
<tr valign=top><td><img src="gifs/gelbekugel.gif" WIDTH=7 HEIGHT=7 ALT=" o"> </td><td><a href="syntax_toc.html">Syntactic Analysis Manual</a></td></tr>
<tr valign=top><td><img src="gifs/gelbekugel.gif" WIDTH=7 HEIGHT=7 ALT=" o"> </td><td><a href="comptrees_toc.html">Computation in Trees</a></td></tr>
</table>
<h4>Tools</h4>
<table BORDER=0 CELLSPACING=0 CELLPADDING=0>
<tr valign=top><td><img src="gifs/gelbekugel.gif" WIDTH=7 HEIGHT=7 ALT=" o"> </td><td><a href="lcl_toc.html">LIGA Control Language</a> </td></tr>
<tr valign=top><td><img src="gifs/gelbekugel.gif" WIDTH=7 HEIGHT=7 ALT=" o"> </td><td><a href="show_toc.html">Debugging Information for LIDO</a> </td></tr>
<tr valign=top><td><img src="gifs/gelbekugel.gif" WIDTH=7 HEIGHT=7 ALT=" o"> </td><td><a href="gorto_toc.html">Graphical ORder TOol</a> </td></tr>
</table>
<p>
<table BORDER=0 CELLSPACING=0 CELLPADDING=0>
<tr valign=top><td><img src="gifs/gelbekugel.gif" WIDTH=7 HEIGHT=7 ALT=" o"> </td><td><a href="fw_toc.html">FunnelWeb User's Manual</a> </td></tr>
</table>
<p>
<table BORDER=0 CELLSPACING=0 CELLPADDING=0>
<tr valign=top><td><img src="gifs/gelbekugel.gif" WIDTH=7 HEIGHT=7 ALT=" o"> </td><td><a href="ptg_toc.html">Pattern-based Text Generator</a> </td></tr>
<tr valign=top><td><img src="gifs/gelbekugel.gif" WIDTH=7 HEIGHT=7 ALT=" o"> </td><td><a href="deftbl_toc.html">Property Definition Language</a> </td></tr>
<tr valign=top><td><img src="gifs/gelbekugel.gif" WIDTH=7 HEIGHT=7 ALT=" o"> </td><td><a href="oil_toc.html">Operator Identification Language</a> </td></tr>
<tr valign=top><td><img src="gifs/gelbekugel.gif" WIDTH=7 HEIGHT=7 ALT=" o"> </td><td><a href="tp_toc.html">Tree Grammar Specification Language</a> </td></tr>
<tr valign=top><td><img src="gifs/gelbekugel.gif" WIDTH=7 HEIGHT=7 ALT=" o"> </td><td><a href="clp_toc.html">Command Line Processing</a> </td></tr>
<tr valign=top><td><img src="gifs/gelbekugel.gif" WIDTH=7 HEIGHT=7 ALT=" o"> </td><td><a href="cola_toc.html">COLA Options Reference Manual</a> </td></tr>
</table>
<p>
<table BORDER=0 CELLSPACING=0 CELLPADDING=0>
<tr valign=top><td><img src="gifs/gelbekugel.gif" WIDTH=7 HEIGHT=7 ALT=" o"> </td><td><a href="idem_toc.html">Generating Unparsing Code</a> </td></tr>
</table>
<p>
<table BORDER=0 CELLSPACING=0 CELLPADDING=0>
<tr valign=top><td><img src="gifs/gelbekugel.gif" WIDTH=7 HEIGHT=7 ALT=" o"> </td><td><a href="mon_toc.html">Monitoring a Processor's Execution</a> </td></tr>
</table>
<h4>Administration</h4>
<table BORDER=0 CELLSPACING=0 CELLPADDING=0>
<tr valign=top><td><img src="gifs/gelbekugel.gif" WIDTH=7 HEIGHT=7 ALT=" o"> </td><td><a href="sysadmin_toc.html">System Administration Guide</a> </td></tr>
</table>
<HR WIDTH="100%">
<CENTER> <A HREF="mailto:elibugs@cs.colorado.edu"><IMG SRC="gifs/button_mail.gif" NOSAVE BORDER=0 HEIGHT=32 WIDTH=32></A><A HREF="mailto:elibugs@cs.colorado.edu">Questions, Comments, ....</A></CENTER>
</TD>
<TD VALIGN=TOP WIDTH="25"><img src="gifs/empty.gif" WIDTH=25 HEIGHT=25></TD>
<TD VALIGN=TOP WIDTH="600">
<H1>Type analysis tasks</H1>
<P>
<IMG SRC="gifs/empty.gif" WIDTH=25 HEIGHT=25 ALT=""><A HREF="type_1.html"><IMG SRC="gifs/prev.gif" ALT="Previous Chapter" BORDER="0"></A>
<IMG SRC="gifs/empty.gif" WIDTH=25 HEIGHT=25 ALT=""><A HREF="type_3.html"><IMG SRC="gifs/next.gif" ALT="Next Chapter" BORDER="0"></A>
<IMG SRC="gifs/empty.gif" WIDTH=25 HEIGHT=25 ALT=""><A HREF="type_toc.html"><IMG SRC="gifs/up.gif" ALT="Table of Contents" BORDER="0"></A>
<IMG SRC="gifs/empty.gif" WIDTH=25 HEIGHT=25 ALT="">
<HR size=1 noshade width=600 align=left>
<H1><A NAME="SEC3" HREF="type_toc.html#SEC3">Properties of Types</A></H1>
<P>
In general it is necessary to associate properties to
types that carry characterizing information which is either
used for purposes of type analysis, e.g. to check compatibility
of two types or to determine the element type of an array, or for
translation purposes, e.g. the size of data objects in the
target code. It is an important design issue in type analysis
to identify the characterizing properties of types.
<P>
In this section four common type classes are used to demonstrate
principles of type properties, and their use in type analysis.
Solutions for other types may be derived from these by
analogy.
Here only properties are described that are used in type analysis
itself, disregarding properties for the transformation task.
<P>
For our solution we use the module for basic type analysis
(see <A HREF="type_1.html#SEC1">Basic Type Analysis</A>)
and some modules of the property library (see <A HREF="prop_toc.html">Property Library of Association of properties to definition</A>).
<P>
<DL COMPACT>
<DT><CODE>ArrayType</CODE>
<DD>Array Types
<DT><CODE>PointerType</CODE>
<DD>Pointer Types
<DT><CODE>FunctionType</CODE>
<DD>Function Types
<DT><CODE>ScopeTypes</CODE>
<DD>Types Having Scope Properties
</DL>
<A NAME="IDX51"></A>
<P>
Array types and pointer types are used to demonstrate
how new types are introduced
by declarations in a program, as opposed to predefined types,
and that type properties refer to other types.
<P>
Function types are used to demonstrate type properties that are
lists of types which are specified using the <CODE>LidoList</CODE> module.
<P>
Record types are used to demonstrate type properties that are
scopes which are specified using modules of the <CODE>Name</CODE> library.
<A NAME="IDX52"></A>
<A NAME="IDX53"></A>
<P>
<H2><A NAME="SEC4" HREF="type_toc.html#SEC4">Array Types</A></H2>
<P>
In this section we use array types to demonstrate how new types are introduced
by declarations in a program, as opposed to predefined types.
Furthermore it is shown how type properties refer to other types.
We assume that an array is described by the type and the number of its elements,
like in C.
The denotation of an array type shall introduce a new type that is diffierent
from any other type, as in Pascal. For ease of our example we do not
consider type rules that allow two different array types to be compatible.
<P>
The language of our running example is extended by <CODE>TypeDenoter</CODE>s
for arrays and by indexed <CODE>Variable</CODE>s as specified by the
concrete productions:
<PRE>
TypeDenoter: ArrayType.
ArrayType: TypeDenoter '[' IntNumber ']'.
Variable: Variable '[' Expression ']'.
</PRE>
The form chosen for <CODE>TypeDenoter</CODE>s keeps the grammar simple and
orthogonal, but leads to a bit unconventional notation for declaration
of array variables and array type names:
<PRE>
type int[10] Vector;
var Vector v;
var float[5] a;
v[6] = 1; a[3] = 2.5;
</PRE>
<P>
An array type is described here by two properties introduced by the
<CODE>.pdl</CODE> specification
<PRE>
ElemType: DefTableKey;
ElemNo: int;
</PRE>
<P>
The rule that any type denoter introduces a new array type is reflected
by using the module role <CODE>TypeDenotation</CODE> unchanged
(see <A HREF="type_1.html#SEC1">Basic Type Analysis</A>).
It provides a computation for <CODE>ArrayType.Type</CODE> being
a new, unique type key:
<A NAME="IDX54"></A>
<P>
<PRE>
SYMBOL ArrayType INHERITS TypeDenotation END;
RULE: ArrayType ::= TypeDenoter '[' IntNumber ']' COMPUTE
ArrayType.GotType =
ORDER
(ResetElemType (ArrayType.Type, TypeDenoter.Type),
ResetElemNo (ArrayType.Type, IntNumber));
END;
RULE: TypeDenoter ::= ArrayType COMPUTE
TypeDenoter.Type = ArrayType.Type;
END;
</PRE>
Association of the type properties establishes the postcondition
<CODE>ArrayType.GotType</CODE>. The module roles ensure that the
properties are set before accessed.
<P>
In the context of an indexed <CODE>Variable</CODE> the type
is expected to be an array type, as checked in the second
computation.
The first computation accesses the element type.
The call of <CODE>TransDefer</CODE> ensures that <CODE>Type</CODE>
attributes in expression contexts are type keys rather than
deferred keys. This is a convention that is introduced by the
module roles, and is carried on here.
<PRE>
RULE: Variable ::= Variable '[' Expression ']' COMPUTE
Variable[1].Type =
TransDefer (GetElemType (Variable[2].Type, NoKey));
IF (EQ (Variable[1].Type, NoKey),
message (ERROR, "index applied to non array", 0, COORDREF));
Expression.ReqType = intType;
END;
</PRE>
<A NAME="IDX55"></A>
<P>
<H2><A NAME="SEC5" HREF="type_toc.html#SEC5">Pointer Types</A></H2>
<P>
In this section we introduce pointer types to the language
of our running example. A type denoted <CODE>t !</CODE> shall
be the type of objects that point to objects of type <CODE>t</CODE>.
Types <CODE>t !</CODE> and <CODE>s !</CODE> shall be equivalent if <CODE>s</CODE>
and <CODE>t</CODE> are equivalent, due to type definitions that simply
rename a type. (This rule is similar to that of C, but different
from that of Pascal where any ocurrence of a pointer type denoter
introduces a new type.)
<P>
The concrete grammar of our running example is extended by the
productions:
<PRE>
TypeDenoter: PointerType.
PointerType: TypeDenoter '!'.
Variable: Variable '!'.
Variable: Variable '&'.
</PRE>
Two new <CODE>Variable</CODE> notations are introduced:
<CODE>v !</CODE> denotes the object which the value of the pointer variable
<CODE>v</CODE> points to. <CODE>v &</CODE> yields the address of the variable
<CODE>v</CODE>, it has the type pointer to <CODE>t</CODE> if <CODE>v</CODE> is a variable
of type <CODE>t</CODE>.
<P>
Due to our equivalence rules all variables in the following example
have the equivalent types:
<PRE>
type int ! IntPtr;
type IntPtr IP;
var IntPtr i, IP j, int ! k;
...
i! = j!;
</PRE>
<P>
The property <CODE>PointsTo</CODE> is introduced to describe pointer types:
<PRE>
PointsTo: DefTableKey [KReset];
</PRE>
(The operation <CODE>KReset</CODE> is described below.)
<P>
The following computation introduces the notation of
pointer types using the module role <CODE>TypeDenotation</CODE>.
The computation establishes the postcondition as required for
<CODE>TypeDenotation</CODE>s:
<A NAME="IDX56"></A>
<P>
<PRE>
SYMBOL PointerType INHERITS TypeDenotation END;
RULE: PointerType ::= TypeDenoter '!' COMPUTE
PointerType.GotType =
ResetPointsTo (PointerType.Type, TypeDenoter.Type);
END;
RULE: TypeDenoter ::= PointerType COMPUTE
TypeDenoter.Type = PointerType.Type;
END;
</PRE>
<P>
If we do not take further means any two occurrences of a pointer
type notation would denote different types.
The equivalence rule described above has to be implemented in functions
which compare types. Caution has to be taken to avoid non-termination
of such functions if applied to recursively defined types.
More details can be found in <CODE>$/Type/Examples/Type.fw</CODE>.
<P>
For the contents operation applied to a <CODE>Variable</CODE> we access
the <CODE>PointsTo</CODE> relation that holds if the <CODE>Variable</CODE> really
has a pointer type:
<PRE>
RULE: Variable ::= Variable '!' COMPUTE
Variable[1].Type =
TransDefer (GetPointsTo (Variable[2].Type, NoKey));
IF (EQ (Variable[1].Type, NoKey),
message (ERROR, "is not a pointer variable", 0, COORDREF));
END;
</PRE>
The call of <CODE>TransDefer</CODE> ensures that <CODE>Type</CODE>
attributes in expression contexts are type keys rather than
deferred keys. A convention that is introduced by the
module roles, and carried on here.
<P>
The address operator implicitly creates a pointer type
that <CODE>PointsTo</CODE> the type of the variable.
Both properties <CODE>PointsTo</CODE> and <CODE>IsType</CODE> are set
for that new type key.
<PRE>
RULE: Variable ::= Variable '&' COMPUTE
Variable[1].Type =
KResetPointsTo
(KResetIsType (NewKey (), 1),
Variable[2].Type);
END;
</PRE>
<A NAME="IDX57"></A>
<A NAME="IDX58"></A>
<P>
The <CODE>KReset</CODE> operations set the property and return the
first argument. That mechanism is obtained from the module
See <A HREF="prop_10.html#SEC10">Some Useful PDL Specifications of Association of properties to definitions</A>, which is automatically instantiated when
using the basic type module.
<A NAME="IDX59"></A>
<A NAME="IDX60"></A>
<A NAME="IDX61"></A>
<A NAME="IDX62"></A>
<P>
<H2><A NAME="SEC6" HREF="type_toc.html#SEC6">Function Types</A></H2>
<P>
Function types are an example for types that have properties which
are lists of types, the types of the parameters.
They are also used here to demonstrate an application of the
<CODE>LidoList</CODE> module (see <A HREF="adt_1.html#SEC1">Lists in LIDO Specifications of Abstract data types to be used in specifications</A>).
<P>
The type of a function is described by its result type and
the list of the types of the parameters. Type analysis of a function
call has to check the parameter types of the called function
against the corresponding argument types, and has to yield the
result type as the type of the call.
<P>
We extend the concrete grammar of our running example by productions
for function declarations and calls:
<PRE>
Declaration: FunctionDecl.
FunctionDecl: 'fun' DefIdent Function ';'.
Function: FunctionHead Block.
FunctionHead: '(' Parameters ')' TypeDenoter.
Parameters: [Parameter // ','].
Parameter: TypeDenoter Ident.
Expression: Expression '(' Arguments ')'.
Arguments: [Argument // ','].
Argument: Expression.
</PRE>
<A NAME="IDX63"></A>
<P>
In the context of a function declaration
the list of parameter types is composed and associated as a
property of the function type. In the context of a function
call that property is accessed, the list is decomposed, and
its elements - the formal parameter types - are compared with
the types of the arguments.
<P>
In order to describe lists of parameter types we instantiate
the <CODE>LidoList</CODE> module for the element type <CODE>DefTableKey</CODE>
as described in (See <A HREF="adt_1.html#SEC1">Lists in LIDO Specifications of Abstract data types to be used in specifications</A>,):
<PRE>
$/Adt/LidoList.gnrc+instance=DefTableKey+referto=deftbl:inst
</PRE>
<P>
A function type is described by two properties, the result type and
and the list of parameter types as introduced by the following
PDL specifications:
<PRE>
ParamTypes: DefTableKeyList; "DefTableKeyList.h"
ResultType: DefTableKey;
</PRE>
where the <CODE>DefTableKeyList</CODE> type and the file defining it
is obtained from the module instance.
<A NAME="IDX64"></A>
<A NAME="IDX65"></A>
<A NAME="IDX66"></A>
<P>
Several module roles are to be combined in the contexts of
function declarations:
The <CODE>Function</CODE> is a <CODE>RangeScope</CODE> where the parameters are
bound.
The <CODE>FunctionDecl</CODE> is a <CODE>TypedDefinition</CODE>
for the function identifier.
The <CODE>FunctionHead</CODE> is a <CODE>TypeDenotation</CODE> describing
the signature of the function.
The <CODE>Parameter</CODE> is a <CODE>TypedDefinition</CODE>.
<PRE>
SYMBOL Function INHERITS RangeScope END;
SYMBOL FunctionDecl INHERITS TypedDefinition END;
RULE: FunctionDecl ::= 'fun' DefIdent Function ';' COMPUTE
FunctionDecl.Type = Function.Type;
END;
SYMBOL FunctionHead INHERITS TypeDenotation END;
RULE: FunctionHead ::= '(' Parameters ')' TypeDenoter COMPUTE
FunctionHead.GotType =
ORDER
(ResetResultType (FunctionHead.Type, TypeDenoter.Type),
ResetParamTypes (FunctionHead.Type,
Parameters.DefTableKeyList));
END;
SYMBOL Parameter INHERITS TypedDefinition END;
</PRE>
Language rules that determine when the types of two functions
are equal have to be implemented in the functions for
type comparison. An example can be found in
<CODE>$/Type/Examples/Type.fw</CODE>.
<P>
The parameter type list is composed using the list construction
roles of the <CODE>LidoList</CODE> module:
<PRE>
SYMBOL Parameters INHERITS DefTableKeyListRoot END;
SYMBOL Parameter INHERITS DefTableKeyListElem END;
RULE: Parameter ::= TypeDenoter DefIdent COMPUTE
Parameter.DefTableKeyElem = TypeDenoter.Type;
END;
</PRE>
<A NAME="IDX67"></A>
<A NAME="IDX68"></A>
<P>
In the context of a call the type of the <CODE>Expression</CODE> is expected to
be a function type. The parameter type list is decomposed using
the list decomposition roles of the <CODE>LidoList</CODE> module,
and checked for missing arguments:
<PRE>
SYMBOL Arguments INHERITS DefTableKeyDeListRoot END;
RULE: Expression ::= Expression '(' Arguments ')' COMPUTE
Arguments.DefTableKeyList =
GetParamTypes (Expression[2].Type, NULLDefTableKeyList);
Expression[1].Type =
TransDefer (GetResultType (Expression[2].Type, NoKey));
IF (EQ (Expression[1].Type, NoKey),
message (ERROR, "call applied to non function", 0, COORDREF));
Expression[2].ReqType = Expression[2].Type;
IF (NE (Arguments.DefTableKeyListTail, NULLDefTableKeyList),
message (ERROR, "arguments missing", 0, COORDREF));
END;
</PRE>
The type of each formal parameter is obtained from the decomposed list
and stated to be the required type of the actual argument:
<PRE>
SYMBOL Argument INHERITS DefTableKeyDeListElem END;
RULE: Argument ::= Expression COMPUTE
Expression.ReqType = Argument.DefTableKeyElem;
IF (EQ (Argument.DefTableKeyElem, NoDefTableKey),
message (ERROR, "too many arguments", 0, COORDREF));
END;
</PRE>
<P>
To obey the naming requirements of the <CODE>LidoList</CODE> module we have to define
the name <CODE>NoDefTableKey</CODE> for missing list elements in a <CODE>.head</CODE>
specification:
<PRE>
#define NoDefTableKey NoKey
</PRE>
<A NAME="IDX69"></A>
<A NAME="IDX70"></A>
<A NAME="IDX71"></A>
<P>
<H2><A NAME="SEC7" HREF="type_toc.html#SEC7">Types Having Scope Properties</A></H2>
<P>
In this section we demonstrate types that have a property which
is a scope, i.e. a set of object definitions. Examples for
such types are records as in Pascal or C, or classes as
in object-oriented languages.
This task of type analysis is closely related to the name analysis
task: The value of such a property is the scope of a certain range,
e.g. the definitions of record components,
or the members of a class.
Variables of such a type may be used in component or member selections,
again, a name analysis task.
<P>
In See <A HREF="name_5.html#SEC11">Scopes Being Properties of Objects of Name analysis according to scope rules</A>,
it is shown how scopes are used as properties of defined entities.
Here we extend that concept towards typed objects using record
types as an example.
<P>
A notation for record types is introduced into our language:
<PRE>
type record int a; bool b; end r;
var r rv, int i;
rv.a = 1;
i = rv.a;
</PRE>
Here, a record type <CODE>r</CODE> is defined, a variable <CODE>vr</CODE>
of that type is declared, and component selections are used.
<P>
The concrete grammar is extended by productions for record type
notations and for component selections:
<PRE>
TypeDenoter: RecordType.
RecordType: 'record' ObjDecls 'end'.
Variable: Variable '.' SelectIdent.
SelectIdent: Ident.
</PRE>
<A NAME="IDX72"></A>
<P>
We use an instance of the See <A HREF="name_5.html#SEC13">Scope Properties Algol-like of Name analysis according to scope rules</A>, module.
The name analysis role <CODE>RangeScopeProp</CODE> specifies the
record type to be a range. Its scope of component definitions
is associated to the <CODE>ScopeKey</CODE>. The <CODE>ScopeKey</CODE>
is specified to be the type key created by the role
<CODE>TypeDenotation</CODE>. The default for the <CODE>GotType</CODE>
attribute need not be overridden, because the <CODE>AlgScopeProp</CODE>
module takes care for setting the scope property before using it:
<P>
<PRE>
SYMBOL RecordType INHERITS TypeDenotation, RangeScopeProp
COMPUTE
SYNT.ScopeKey = SYNT.Type;
END;
RULE: TypeDenoter ::= RecordType COMPUTE
TypeDenoter.Type = RecordType.Type;
END;
</PRE>
<A NAME="IDX73"></A>
<A NAME="IDX74"></A>
<A NAME="IDX75"></A>
<P>
The selection identifier is bound in a scope that is obtained from
a scope property. Hence, it has the same computational roles as
specified for the <CODE>QualIdent</CODE> of the scope operator <CODE>::</CODE>
in (see <A HREF="name_5.html#SEC13">Scope Properties Algol-like of Name analysis according to scope rules</A>), i.e. <CODE>IdUseScopeProp</CODE>,
<CODE>ChkIdUse</CODE>, and <CODE>IdentOcc</CODE>.
For type analysis it has additionally the roles for an applied
identifier occurrence of a typed object.
<P>
<PRE>
SYMBOL SelectIdent INHERITS
IdUseScopeProp, ChkIdUse, IdentOcc,
TypedUseId, ChkTypedUseId
END;
RULE: Variable ::= Variable '.' SelectIdent COMPUTE
SelectIdent.Scope = GetScope (Variable[2].Type, NoEnv)
<- INCLUDING RootScope.GotScopeProp;
Variable[1].Type = SelectIdent.Type;
IF (EQ (SelectIdent.Scope, NoEnv),
message (ERROR, "selection applied to non record type",
0, COORDREF));
END;
</PRE>
Here the scope property for the selection is obtained from
the type of the selected
variable. The precondition ensures
that the scope properties have been set.
<A NAME="IDX76"></A>
<A NAME="IDX77"></A>
<A NAME="IDX78"></A>
<A NAME="IDX79"></A>
<P>
<HR size=1 noshade width=600 align=left>
<P>
<IMG SRC="gifs/empty.gif" WIDTH=25 HEIGHT=25 ALT=""><A HREF="type_1.html"><IMG SRC="gifs/prev.gif" ALT="Previous Chapter" BORDER="0"></A>
<IMG SRC="gifs/empty.gif" WIDTH=25 HEIGHT=25 ALT=""><A HREF="type_3.html"><IMG SRC="gifs/next.gif" ALT="Next Chapter" BORDER="0"></A>
<IMG SRC="gifs/empty.gif" WIDTH=25 HEIGHT=25 ALT=""><A HREF="type_toc.html"><IMG SRC="gifs/up.gif" ALT="Table of Contents" BORDER="0"></A>
<IMG SRC="gifs/empty.gif" WIDTH=25 HEIGHT=25 ALT="">
<HR size=1 noshade width=600 align=left>
</TD>
</TR>
</TABLE>
</BODY></HTML>
|