| [c6e6333] | 1 | // TODO: add error cases (e.g., use of field constructors for managed types, etc.) | 
|---|
|  | 2 |  | 
|---|
| [8e138da] | 3 | enum Color { R, G, B }; | 
|---|
|  | 4 |  | 
|---|
|  | 5 | // empty struct/union should have generated ctor/dtors | 
|---|
|  | 6 | union U {}; | 
|---|
|  | 7 | struct S {}; | 
|---|
|  | 8 |  | 
|---|
| [c6e6333] | 9 | struct SimpleUnion { | 
|---|
|  | 10 | int x; | 
|---|
|  | 11 | double y; | 
|---|
|  | 12 | char z; | 
|---|
|  | 13 | }; | 
|---|
|  | 14 |  | 
|---|
|  | 15 | struct SimpleStruct { | 
|---|
|  | 16 | int x; | 
|---|
|  | 17 | double y; | 
|---|
|  | 18 | char z; | 
|---|
|  | 19 | }; | 
|---|
|  | 20 |  | 
|---|
| [8e138da] | 21 | // struct/union with members with generated ctor/dtors should themselves have generated ctor/dtors | 
|---|
|  | 22 | union PopulatedUnion { | 
|---|
|  | 23 | Color c; | 
|---|
|  | 24 | U u; | 
|---|
|  | 25 | S s; | 
|---|
|  | 26 | }; | 
|---|
|  | 27 |  | 
|---|
|  | 28 | struct PopulatedStruct { | 
|---|
| [c6e6333] | 29 | Color c; | 
|---|
| [8e138da] | 30 | U u; | 
|---|
|  | 31 | S s; | 
|---|
|  | 32 | }; | 
|---|
|  | 33 |  | 
|---|
| [86f852b] | 34 | // dtype-static generic type is otype | 
|---|
|  | 35 | forall(dtype T) | 
|---|
|  | 36 | struct DtypeStaticStruct { | 
|---|
|  | 37 | T * data; | 
|---|
|  | 38 | short size; | 
|---|
|  | 39 | }; | 
|---|
|  | 40 |  | 
|---|
|  | 41 | forall(dtype T) | 
|---|
|  | 42 | union DtypeStaticUnion { | 
|---|
|  | 43 | T * data; | 
|---|
|  | 44 | short size; | 
|---|
|  | 45 | }; | 
|---|
|  | 46 |  | 
|---|
|  | 47 | // dynamic generic type is otype | 
|---|
|  | 48 | forall(otype T) | 
|---|
|  | 49 | struct DynamicStruct { | 
|---|
|  | 50 | T x; | 
|---|
|  | 51 | }; | 
|---|
|  | 52 |  | 
|---|
|  | 53 | forall(otype T) | 
|---|
|  | 54 | union DynamicUnion { | 
|---|
|  | 55 | T x; | 
|---|
|  | 56 | }; | 
|---|
|  | 57 |  | 
|---|
|  | 58 | // struct/union that contains a generic type is | 
|---|
|  | 59 | struct GenericContainingStruct { | 
|---|
|  | 60 | DynamicStruct(int) dsi; | 
|---|
|  | 61 | DynamicStruct(double) dsd; | 
|---|
|  | 62 | DynamicUnion(int) dui; | 
|---|
|  | 63 | DynamicUnion(double) dud; | 
|---|
|  | 64 | DtypeStaticStruct(int) dssi; | 
|---|
|  | 65 | DtypeStaticStruct(float) dssf; | 
|---|
|  | 66 | DtypeStaticUnion(int) dsui; | 
|---|
|  | 67 | DtypeStaticUnion(float) dsuf; | 
|---|
|  | 68 | }; | 
|---|
|  | 69 |  | 
|---|
|  | 70 | union GenericContainingUnion { | 
|---|
|  | 71 | DynamicStruct(int) dsi; | 
|---|
|  | 72 | DynamicStruct(double) dsd; | 
|---|
|  | 73 | DynamicUnion(int) dui; | 
|---|
|  | 74 | DynamicUnion(double) dud; | 
|---|
|  | 75 | DtypeStaticStruct(int) dssi; | 
|---|
|  | 76 | DtypeStaticStruct(float) dssf; | 
|---|
|  | 77 | DtypeStaticUnion(int) dsui; | 
|---|
|  | 78 | DtypeStaticUnion(float) dsuf; | 
|---|
|  | 79 | }; | 
|---|
|  | 80 |  | 
|---|
|  | 81 |  | 
|---|
|  | 82 | forall(otype T) | 
|---|
|  | 83 | T identity(T x) { return x; } | 
|---|
|  | 84 |  | 
|---|
|  | 85 | // can identity e if only sized or only the assertion, but the combination breaks... | 
|---|
|  | 86 | // forall(dtype T | sized(T) | { void ?{}(T &); }) | 
|---|
|  | 87 | // void identity(T x) {  } | 
|---|
|  | 88 |  | 
|---|
|  | 89 | #if ERR1 | 
|---|
| [b93a3de] | 90 | // managed type - defines a constructor - can't use field constructors | 
|---|
|  | 91 | struct Managed { | 
|---|
|  | 92 | int x; | 
|---|
|  | 93 | }; | 
|---|
|  | 94 |  | 
|---|
|  | 95 | void ?{}(Managed & m) { m.x = 0; } | 
|---|
|  | 96 |  | 
|---|
|  | 97 | // managed type since it contains a managed type - can't use field constructors | 
|---|
|  | 98 | struct InheritManaged { | 
|---|
|  | 99 | Managed m; | 
|---|
|  | 100 | }; | 
|---|
|  | 101 |  | 
|---|
| [86f852b] | 102 | Managed x = { 123 }; // error | 
|---|
|  | 103 | Managed y;           // okay | 
|---|
| [8e138da] | 104 |  | 
|---|
| [86f852b] | 105 | InheritManaged z = { y };  // error? | 
|---|
|  | 106 | #endif | 
|---|
| [c6e6333] | 107 |  | 
|---|
| [8e138da] | 108 | int main() { | 
|---|
|  | 109 | S s; | 
|---|
|  | 110 | U u; | 
|---|
| [c6e6333] | 111 | Color e; | 
|---|
| [8e138da] | 112 |  | 
|---|
| [86f852b] | 113 | // identity(R);  Color constant should be Color which is otype | 
|---|
|  | 114 | identity(e);  // Color should be an otype | 
|---|
| [8e138da] | 115 | identity(u);  // U should be an otype | 
|---|
|  | 116 | identity(s);  // S should be an otype | 
|---|
|  | 117 |  | 
|---|
| [b93a3de] | 118 | SimpleStruct ss; | 
|---|
|  | 119 | SimpleUnion su; | 
|---|
|  | 120 |  | 
|---|
|  | 121 | identity(ss); | 
|---|
|  | 122 | identity(su); | 
|---|
|  | 123 |  | 
|---|
| [8e138da] | 124 | PopulatedStruct ps; | 
|---|
|  | 125 | PopulatedUnion pu; | 
|---|
|  | 126 |  | 
|---|
|  | 127 | identity(ps); // should recursively be an otype | 
|---|
|  | 128 | identity(pu); // should recursively be an otype | 
|---|
| [b93a3de] | 129 |  | 
|---|
| [86f852b] | 130 | DynamicStruct(int) dsi; | 
|---|
|  | 131 | DynamicStruct(double) dsd; | 
|---|
|  | 132 | DynamicUnion(int) dui; | 
|---|
|  | 133 | DynamicUnion(double) dud; | 
|---|
|  | 134 | DtypeStaticStruct(int) dssi; | 
|---|
|  | 135 | DtypeStaticStruct(float) dssf; | 
|---|
|  | 136 | DtypeStaticUnion(int) dsui; | 
|---|
|  | 137 | DtypeStaticUnion(float) dsuf; | 
|---|
|  | 138 |  | 
|---|
|  | 139 | identity(dsi); | 
|---|
|  | 140 | identity(dsd); | 
|---|
|  | 141 | // identity(dui); // xxx - codegen errors in generated thunk _temp3 (Box-pass-generated assignment return-temporary) | 
|---|
|  | 142 | // identity(dud); | 
|---|
|  | 143 | identity(dssi); | 
|---|
|  | 144 | identity(dssf); | 
|---|
|  | 145 | identity(dsui); | 
|---|
|  | 146 | identity(dsuf); | 
|---|
|  | 147 |  | 
|---|
|  | 148 | GenericContainingStruct gcs; | 
|---|
|  | 149 | GenericContainingUnion gcu; | 
|---|
|  | 150 |  | 
|---|
|  | 151 | identity(gcs); | 
|---|
|  | 152 | identity(gcu); | 
|---|
| [8e138da] | 153 | } | 
|---|