Changes in / [f6bbc92:e49c308]
- Files:
-
- 3 edited
-
doc/theses/jiada_liang_MMath/CFAenum.tex (modified) (3 diffs)
-
doc/theses/jiada_liang_MMath/background.tex (modified) (1 diff)
-
src/Validate/ImplementEnumFunc.cpp (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
doc/theses/jiada_liang_MMath/CFAenum.tex
rf6bbc92 re49c308 204 204 enum( char ) Letter { A = 'A', ... }; 205 205 enum( @Letter@ ) Greek { Alph = A, Beta = B, ... }; // alphabet intersection 206 207 void foo(Letter l); 208 foo(Beta); $\C{// foo(value(Beta))}$ 209 \end{cfa} 210 Enumeration @Greek@ may have more or less enumerators than @Letter@, but the enum values \emph{must} be of a member of @Letter@. 211 Therefore, the set of @Greek@ enum value in a subset of the value set of type @Letter@. 212 @Letter@ is type compatible with enumeration @Letter@ thanks to \CFA inserts value conversion whenever @Letter@ be used 213 in place of @Greek@. On the other hand, @Letter@ enums are not type compatible with enumeration @Greek@. 214 As a result, @Greek@ becomes a logical subtype of @Letter@. 215 216 Subset defines an implicit subtyping relationship between two \CFA enumerations. \CFA also has 217 containment inheritance for \CFA enumerations where subtyping is explicitly structured. 206 \end{cfa} 207 Enumeration @Greek@ may have more or less enums than @Letter@, but the enum values \emph{must} be from @Letter@. 208 Therefore, @Greek@ enums are a subset of type @Letter@ and are type compatible with enumeration @Letter@, but @Letter@ enums are not type compatible with enumeration @Greek@. 209 218 210 219 211 \section{Inheritance} … … 244 236 However, the position of the underlying representation is the order of the enumerator in the new enumeration. 245 237 \begin{cfa} 246 enum() E1 { B }; $\C{// B}$ 247 enum() E2 { C, D }; $\C{// C D}$ 248 enum() E3 { inline E1, inline E2, E }; $\C{// {\color{red}[\(_{E1}\)} B {\color{red}]} {\color{red}[\(_{E2}\)} C D {\color{red}]} E}$ 249 enum() E4 { A, inline E3, F}; $\C{// A {\color{blue}[\(_{E3}\)} {\color{red}[\(_{E1}\)} B {\color{red}]} {\color{red}[\(_{E2}\)} C D {\color{red}]} E {\color{blue}]} F }$ 250 \end{cfa} 251 In the example above, @B@ has the position 0 in @E1@ and @E3@, but it at the position 1 in @E4@ as @A@ taking the 0 in @E4@. 252 @C@ is at the position 0 in @E2@, 1 in @E3@ and 2 in E4. @D@ is at the position 1 in @E2@, 2 in @E3@ and 3 in @E4@. 253 254 A subtype enumeration can be casted, or implicitly converted into its supertype, with a @safe@ cost. Such conversion is an @enumeration conversion@. 238 enum() E1 { A }; 239 enum() E2 { B, C }; 240 enum() E3 { inline E1, inline E2, D }; 241 \end{cfa} 242 Here, @A@ has position 0 in @E1@ and @E3@. 243 @B@ has position 0 in @E2@ and 1 in @E3@. 244 @C@ has position 1 in @E2@ and position 2 in @E3@. 245 @D@ has position 3 in @E3@. 246 247 A subtype enumeration can be casted, or implicitly converted into its supertype, with a @safe@ cost. 255 248 \begin{cfa} 256 249 enum E2 e2 = C; 257 posn( e2 ); $\C[1.75in]{// 0}$250 posn( e2 ); $\C[1.75in]{// 1}$ 258 251 enum E3 e3 = e2; 259 posn( e2 ); $\C{// 1}$252 posn( e2 ); $\C{// 2}$ 260 253 void foo( E3 e ); 261 254 foo( e2 ); 262 posn( (E3)e2 ); $\C{// 1}$255 posn( (E3)e2 ); $\C{// 2}$ 263 256 E3 e31 = B; 264 posn( e31 ); $\C{// 0}\CRT$257 posn( e31 ); $\C{// 1}\CRT$ 265 258 \end{cfa} 266 259 The last expression is unambiguous. 267 260 While both @E2.B@ and @E3.B@ are valid candidate, @E2.B@ has an associated safe cost and \CFA selects the zero cost candidate @E3.B@. 261 Hence, as discussed in \VRef{s:OpaqueEnum}, \CFA chooses position as a representation of the \CFA enum. 262 Therefore, conversion involves both a change of type and possibly position. 263 264 When converting a subtype to a supertype, its position can only be a larger value. 265 The difference between the position in the subtype and in the supertype is its \newterm{offset}. 266 \VRef[Figure]{s:OffsetSubtypeSuperType} show the algorithm to determine the offset for an subtype enumerator to its super type. 267 \PAB{You need to explain the algorithm.} 268 269 \begin{figure} 270 \begin{cfa} 271 struct Enumerator; 272 struct CFAEnum { 273 vector<variant<CFAEnum, Enumerator>> members; 274 }; 275 pair<bool, int> calculateEnumOffset( CFAEnum dst, Enumerator e ) { 276 int offset = 0; 277 for ( auto v: dst.members ) { 278 if ( v.holds_alternative<Enumerator>() ) { 279 auto m = v.get<Enumerator>(); 280 if ( m == e ) return make_pair( true, 0 ); 281 offset++; 282 } else { 283 auto p = calculateEnumOffset( v, e ); 284 if ( p.first ) return make_pair( true, offset + p.second ); 285 offset += p.second; 286 } 287 } 288 return make_pair( false, offset ); 289 } 290 \end{cfa} 291 \caption{Compute Offset from Subtype Enumerator to Super Type} 292 \label{s:OffsetSubtypeSuperType} 293 \end{figure} 268 294 269 295 For the given function prototypes, the following calls are valid. … … 287 313 Note, the validity of calls is the same for call-by-reference as for call-by-value, and @const@ restrictions are the same as for other types. 288 314 289 \subsection{Offset Calculation}290 As discussed in \VRef{s:OpaqueEnum}, \CFA chooses position as a representation of a \CFA enumeration variable.291 Because enumerators has different @position@ between subtype and supertype, \CFA might need to manipulate the representation whenever a cast or292 implicit conversion involves two \CFA enums. \CFA determine how a position is going to change with an @offset calculation@ function, reflects to293 enumerator as an arithmetic expression. Casting an enumeration of subtype to a supertype, the position can be unchanged or increase. The change294 of position is an @offset@.295 296 \begin{figure}297 \begin{cfa}298 struct Enumerator;299 struct CFAEnum { vector<variant<CFAEnum, Enumerator>> members; string name; };300 inline static bool operator==(CFAEnum& lhs, CFAEnum& rhs) { return lhs.name == rhs.name; }301 pair<bool, int> calculateEnumOffset(CFAEnum src, CFAEnum dst) {302 int offset = 0;303 if (src == dst) return make_pair(true, 0);304 for (auto v : dst.members) {305 if (holds_alternative<Enumerator>(v)) {306 offset++;307 } else {308 auto m = get<CFAEnum>(v);309 if (m == src) return make_pair(true, offset);310 auto dist = calculateEnumOffset(src, m);311 if (dist.first) {312 return make_pair(true, offset + dist.second);313 } else {314 offset += dist.second;315 }316 }317 }318 return make_pair(false, offset);319 }320 \end{cfa}321 \caption{Compute Offset from Subtype Enumeration to a Supertype}322 \label{s:OffsetSubtypeSuperType}323 \end{figure}324 325 Figure~\ref{s:OffsetSubtypeSuperType} shows a minimum of @offset calculation@, written in \CC. CFAEnum represents \CFA enumeration326 which has a vector of variants of Enumerator or CFAEnum. Two CFAEnums are differentiable by their unquie name.327 The algorithm takes two CFAEnums as parameters @src@ and @dst@, with @src@ being type of expression the conversion applies on,328 and @dst@ being type that the expression cast into. The algorithm returns a pair of value: when @src@ is convertible to @dst@ (@src@ is a subtype of @dst@),329 it returns boolean true and the offset. Otherwise, it returns false and the size of @src@ (total number of enumerators in @src@). The offset between a type and itself is defined330 as 0.331 332 The algorithm iterates over members in @dst@ to find @src@. If a member is an enumerator of @dst@, the positions of all subsequent members333 increment by one. If the current member is @dst@, the function returns true indicating "found" and the accumulated offset. Otherwise, the algorithm recurse334 into the current CFAEnum @m@ and find out if the @src@ is convertible to @m@. The @src@ being convertible to the current member @m@ means @src@335 is a "subtype of subtype" of @dst@. The offset between @src@ and @dst@ is the sum of the offset of @m@ in @dst@ and the offset of336 @src@ in @m@. If @src@ is not a subtype of @m@, the loop continue but with offset shifted by the size of @m@. The procedure reaches the end337 of the loop proves @src@ is not convertible to @dst@. It returns size of @src@ in case it is in a recurse innvocation and size is needed for offset update.338 315 339 316 \section{Control Structures} -
doc/theses/jiada_liang_MMath/background.tex
rf6bbc92 re49c308 487 487 488 488 In \CFA, the meaning of a C style cast is determined by its @Cast Cost@. For most cast expression resolution, a cast cost is equal to a conversion cost. 489 Cast cost exists as an independent matrix for conversion that cannot happen impl icitly, while being possible with an explicit cast. These conversions are often defined to have489 Cast cost exists as an independent matrix for conversion that cannot happen implcitly, while being possible with an explicit cast. These conversions are often defined to have 490 490 infinite conversion cost and non-infinite cast cost. -
src/Validate/ImplementEnumFunc.cpp
rf6bbc92 re49c308 210 210 "value", 211 211 {new ast::ObjectDecl(getLocation(), "_i", new ast::EnumInstType(decl))}, 212 {new ast::ObjectDecl(getLocation(), "_ret", decl->base)}); 212 {new ast::ObjectDecl(getLocation(), "_ret", 213 ast::deepCopy(decl->base))}); 214 // else 215 // return genQuasiValueProto(); 213 216 } 214 217
Note:
See TracChangeset
for help on using the changeset viewer.