From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.5-pre1 (2020-06-20) on ip-172-31-74-118.ec2.internal X-Spam-Level: X-Spam-Status: No, score=-1.9 required=3.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.5-pre1 Date: 11 Apr 93 04:12:19 GMT From: seas.gwu.edu!mfeldman@uunet.uu.net (Michael Feldman) Subject: Rep specs and conversion among derived types Message-ID: <1993Apr11.041219.28979@seas.gwu.edu> List-Id: Here's one to try on your favorite compiler, if you are interested in storage reps. I discovered a section in the Ada Rationale a couple of years ago, quite by accident, that pointed me in the direction of this program. It never occurred to me to try an explicit conversion between related types with different reps, but I relaized in the end that this was indeed quite intentional. Caution: this is a short program with lots of tricks. Try this at home, but if you try it on a project, please encapsulate the hell out of it. Pat yourself on the back if you understand this code. I've left it uncommented to give you a nice challenge. Good luck. Post strange results, please. Mike Feldman (see code following sig) ------------------------------------------------------------------------ Michael B. Feldman co-chair, SIGAda Education Committee Professor, Dept. of Electrical Engineering and Computer Science School of Engineering and Applied Science The George Washington University Washington, DC 20052 USA (202) 994-5253 (voice) (202) 994-5296 (fax) mfeldman@seas.gwu.edu (Internet) "The most important thing is to be sincere, and once you've learned how to fake that, you've got it made." -- old show-business adage ------------------------------------------------------------------------ with UNCHECKED_CONVERSION; with TEXT_IO; procedure REPDEMO is type INT_32 is range -2**31..2**31-1; package MY_INT_IO is new TEXT_IO.INTEGER_IO(INT_32); type DIRECTION is (UP, DOWN, LEFT, RIGHT); type D_ARRAY is ARRAY(1..4) of DIRECTION; type PD_ARRAY is new D_ARRAY; pragma PACK(PD_ARRAY); function D_TO_INT is NEW UNCHECKED_CONVERSION (SOURCE => D_ARRAY, TARGET => INT_32); function PD_TO_INT is NEW UNCHECKED_CONVERSION (SOURCE => PD_ARRAY, TARGET => INT_32); type NEWDIRECTION is new DIRECTION; for NEWDIRECTION use (1,2,4,8); type N_ARRAY is ARRAY(1..4) of NEWDIRECTION; type PN_ARRAY is new N_ARRAY; pragma PACK(PN_ARRAY); function N_TO_INT is new UNCHECKED_CONVERSION (SOURCE => N_ARRAY, TARGET => INT_32); function PN_TO_INT is new UNCHECKED_CONVERSION (SOURCE => PN_ARRAY, TARGET => INT_32); DIR: DIRECTION := UP; NEWDIR: DIRECTION := UP; D: D_ARRAY := (UP, DOWN, LEFT, RIGHT); N: N_ARRAY; PD: PD_ARRAY; PN: PN_ARRAY; begin N := (NEWDIRECTION(D(1)), NEWDIRECTION(D(2)), NEWDIRECTION(D(3)), NEWDIRECTION(D(4))); PD := PD_ARRAY(D); PN := PN_ARRAY(N); MY_INT_IO.PUT(D'SIZE); TEXT_IO.NEW_LINE; MY_INT_IO.PUT(PD'SIZE); TEXT_IO.NEW_LINE; MY_INT_IO.PUT(N'SIZE); TEXT_IO.NEW_LINE; MY_INT_IO.PUT(PN'SIZE); TEXT_IO.NEW_LINE; MY_INT_IO.PUT(DIR'SIZE); TEXT_IO.NEW_LINE; MY_INT_IO.PUT(NEWDIR'SIZE); TEXT_IO.NEW_LINE; MY_INT_IO.PUT(ITEM => D_TO_INT(D),BASE=>16); TEXT_IO.NEW_LINE; MY_INT_IO.PUT(ITEM => PD_TO_INT(PD),BASE=>16); TEXT_IO.NEW_LINE; MY_INT_IO.PUT(ITEM => N_TO_INT(N),BASE=>16); TEXT_IO.NEW_LINE; MY_INT_IO.PUT(ITEM => PN_TO_INT(PN),BASE=>16); TEXT_IO.NEW_LINE; end REPDEMO;