Ignore:
Timestamp:
Jan 8, 2019, 11:31:21 AM (6 years ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, no_list, persistent-indexer, pthread-emulation, qualifiedEnum
Children:
08222c7, 274da98
Parents:
84b4d607 (diff), d5b2ac8 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' of plg.uwaterloo.ca:software/cfa/cfa-cc

File:
1 edited

Legend:

Unmodified
Added
Removed
  • tests/references.cfa

    r84b4d607 r25cdca5  
    99// Author           : Rob Schluntz
    1010// Created On       : Wed Aug 23 16:11:50 2017
    11 // Last Modified By : Rob Schluntz
    12 // Last Modified On : Wed Aug 23 16:12:03
    13 // Update Count     : 2
     11// Last Modified By : Peter A. Buhr
     12// Last Modified On : Tue Dec 25 14:31:48 2018
     13// Update Count     : 11
    1414//
    1515
     16#include <fstream.hfa>
     17
    1618struct Y { int i; };
    17 void ?{}(Y & y) { printf("Default constructing a Y\n"); }
    18 void ?{}(Y & y, Y other) { printf("Copy constructing a Y\n"); }
    19 void ^?{}(Y & y) { printf("Destructing a Y\n"); }
    20 Y ?=?(Y & y, Y other) { printf("Assigning a Y\n"); return y; }
    21 void ?{}(Y & y, int i) { printf("Value constructing a Y %d\n", i); y.i = i; }
     19void ?{}( Y & y ) { sout | "Default constructing a Y"; }
     20void ?{}( Y & y, Y other ) { sout | "Copy constructing a Y"; }
     21void ^?{}( Y & y ) { sout | "Destructing a Y"; }
     22Y ?=?( Y & y, Y other ) { sout | "Assigning a Y"; return y; }
     23void ?{}( Y & y, int i ) { sout | "Value constructing a Y" | i; y.i = i; }
    2224
    2325struct X { Y & r; Y y; };
    24 void ?{}(X & x) {
     26void ?{}( X & x ) {
    2527        // ensure that r is not implicitly constructed
    2628}
    27 void ?{}(X & x, X other) {
     29void ?{}( X & x, X other ) {
    2830        // ensure that r is not implicitly constructed
    2931}
    30 void ^?{}(X & x) {
     32void ^?{}( X & x ) {
    3133        // ensure that r is not implicitly destructed
    3234}
    33 X ?=?(X & x, X other) { return x; }
     35X ?=?( X & x, X other ) { return x; }
    3436
    3537// ensure that generated functions do not implicitly operate on references
     
    4850        int x = 123456, x2 = 789, *p1 = &x, **p2 = &p1, ***p3 = &p2,
    4951                &r1 = x,    &&r2 = r1,   &&&r3 = r2;
    50         ***p3 = 3;                          // change x
    51         **p3 = &x;                          // change p1
    52         *p3 = &p1;                          // change p2
    53         int y = 0, z = 11, & ar[3] = { x, y, z };    // initialize array of references
    54         // &ar[1] = &z;                        // change reference array element
    55         // typeof( ar[1] ) p = 3;              // is int, i.e., the type of referenced object
    56         // typeof( &ar[1] ) q = &x;            // is int *, i.e., the type of pointer
     52        ***p3 = 3;                                                                                      // change x
     53        **p3 = &x;                                                                                      // change p1
     54        *p3 = &p1;                                                                                      // change p2
     55        int y = 0, z = 11, & ar[3] = { x, y, z };                       // initialize array of references
     56                                                                                                                // &ar[1] = &z;                        // change reference array element
     57                                                                                                                // typeof( ar[1] ) p = 3;              // is int, i.e., the type of referenced object
     58                                                                                                                // typeof( &ar[1] ) q = &x;            // is int *, i.e., the type of pointer
    5759        // _Static_assert( sizeof( ar[1] ) == sizeof( int ), "Array type should be int." );   // is true, i.e., the size of referenced object
    5860        // _Static_assert( sizeof( &ar[1] ) == sizeof( int *), "Address of array should be int *." ); // is true, i.e., the size of a reference
    5961
    60         ((int*&)&r3) = &x;                  // change r1, (&*)**r3
     62        ((int*&)&r3) = &x;                                                                      // change r1, (&*)**r3
    6163        x = 3;
    6264        // test that basic reference properties are true - r1 should be an alias for x
    63         printf("%d %d %d\n", x, r1, &x == &r1);
     65        sout | x | r1 | &x == &r1;
    6466        r1 = 12;
    65         printf("%d %d %d\n", x, r1, &x == &r1);
     67        sout | x | r1 | &x == &r1;
    6668
    6769        // test that functions using basic references work
    68         printf("%d %d %d %d\n", toref(&x), toref(p1), toptr(r1) == toptr(x), toptr(r1) == &x);
     70        sout | toref( &x ) | toref( p1 ) | toptr( r1 ) == toptr( x ) | toptr( r1 ) == &x;
    6971
    7072        changeRef( x );
    7173        changeRef( y );
    7274        changeRef( z );
    73         printf("%d %d %d\n", x, y, z);
     75        sout | x | y | z;
    7476        changeRef( r1 );
    75         printf("%d %d\n", r1, x);
     77        sout | r1 | x;
    7678
    77         r3 = 6;                               // change x, ***r3
    78         printf("x = %d ; x2 = %d\n", x, x2);  // check that x was changed
    79         &r3 = &x2;                            // change r1 to refer to x2, (&*)**r3
    80         r3 = 999;                             // modify x2
    81         printf("x = %d ; x2 = %d\n", x, x2);  // check that x2 was changed
    82         ((int**&)&&r3) = p2;                  // change r2, (&(&*)*)*r3, ensure explicit cast to reference works
    83         r3 = 12345;                           // modify x
    84         printf("x = %d ; x2 = %d\n", x, x2);  // check that x was changed
    85         &&&r3 = p3;                           // change r3 to p3, (&(&(&*)*)*)r3
    86         ((int&)r3) = 22222;                   // modify x, ensure explicit cast to reference works
    87         printf("x = %d ; x2 = %d\n", x, x2);  // check that x was changed
     79        r3 = 6;                                                                                         // change x, ***r3
     80        sout | "x = " | x | " ; x2 = " | x2;                            // check that x was changed
     81        &r3 = &x2;                                                                                      // change r1 to refer to x2, (&*)**r3
     82        r3 = 999;                                                                                       // modify x2
     83        sout | "x = " | x | " ; x2 = " | x2;                            // check that x2 was changed
     84        ((int**&)&&r3) = p2;                                                            // change r2, (&(&*)*)*r3, ensure explicit cast to reference works
     85        r3 = 12345;                                                                                     // modify x
     86        sout | "x = " | x | " ; x2 = " | x2;                            // check that x was changed
     87        &&&r3 = p3;                                                                                     // change r3 to p3, (&(&(&*)*)*)r3
     88        ((int&)r3) = 22222;                                                                     // modify x, ensure explicit cast to reference works
     89        sout | "x = " | x | " ; x2 = " | x2;                                    // check that x was changed
    8890
    8991        // test that reference members are not implicitly constructed/destructed/assigned
     
    102104                struct S { double x, y; };
    103105                void f( int & i, int & j, S & s, int v[] ) {
    104                         printf("%d %d { %g, %g }, [%d, %d, %d]\n", i, j, s.[x, y], v[0], v[1], v[2]);
     106                        sout | i | j | "{ " | s.[x, y] | " }," | "[" | v[0] | "," | v[1] | "," | v[2] | "]";
    105107                }
    106                 void g(int & i) { printf("%d\n", i); }
    107                 void h(int &&& i) { printf("%d\n", i); }
     108                void g(int & i) { sout | i; }
     109                void h(int &&& i) { sout | i; }
    108110
    109                 int &&& r = 3;  // rvalue to reference
     111                int &&& r = 3;                                                                  // rvalue to reference
    110112                int i = r;
    111                 printf("%d %d\n", i, r);  // both 3
     113                sout | i | r;                                                                   // both 3
    112114
    113                 g( 3 );          // rvalue to reference
    114                 h( (int &&&)3 ); // rvalue to reference
     115                g( 3 );                                                                                 // rvalue to reference
     116                h( (int &&&)3 );                                                                // rvalue to reference
    115117
    116118                int a = 5, b = 4;
Note: See TracChangeset for help on using the changeset viewer.