Changeset f6bbc92
- Timestamp:
- Aug 7, 2024, 6:48:57 AM (5 weeks ago)
- Branches:
- master
- Children:
- 3b10778
- Parents:
- e49c308 (diff), 1d8a349 (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. - Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/theses/jiada_liang_MMath/CFAenum.tex
re49c308 rf6bbc92 204 204 enum( char ) Letter { A = 'A', ... }; 205 205 enum( @Letter@ ) Greek { Alph = A, Beta = B, ... }; // alphabet intersection 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 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. 210 218 211 219 \section{Inheritance} … … 236 244 However, the position of the underlying representation is the order of the enumerator in the new enumeration. 237 245 \begin{cfa} 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. 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@. 248 255 \begin{cfa} 249 256 enum E2 e2 = C; 250 posn( e2 ); $\C[1.75in]{// 1}$257 posn( e2 ); $\C[1.75in]{// 0}$ 251 258 enum E3 e3 = e2; 252 posn( e2 ); $\C{// 2}$259 posn( e2 ); $\C{// 1}$ 253 260 void foo( E3 e ); 254 261 foo( e2 ); 255 posn( (E3)e2 ); $\C{// 2}$262 posn( (E3)e2 ); $\C{// 1}$ 256 263 E3 e31 = B; 257 posn( e31 ); $\C{// 1}\CRT$264 posn( e31 ); $\C{// 0}\CRT$ 258 265 \end{cfa} 259 266 The last expression is unambiguous. 260 267 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}294 268 295 269 For the given function prototypes, the following calls are valid. … … 313 287 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. 314 288 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 or 292 implicit conversion involves two \CFA enums. \CFA determine how a position is going to change with an @offset calculation@ function, reflects to 293 enumerator as an arithmetic expression. Casting an enumeration of subtype to a supertype, the position can be unchanged or increase. The change 294 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 enumeration 326 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 defined 330 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 members 333 increment by one. If the current member is @dst@, the function returns true indicating "found" and the accumulated offset. Otherwise, the algorithm recurse 334 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 of 336 @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 end 337 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. 315 338 316 339 \section{Control Structures} -
doc/theses/jiada_liang_MMath/background.tex
re49c308 rf6bbc92 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 citly, 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 implicitly, 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
re49c308 rf6bbc92 210 210 "value", 211 211 {new ast::ObjectDecl(getLocation(), "_i", new ast::EnumInstType(decl))}, 212 {new ast::ObjectDecl(getLocation(), "_ret", 213 ast::deepCopy(decl->base))}); 214 // else 215 // return genQuasiValueProto(); 212 {new ast::ObjectDecl(getLocation(), "_ret", decl->base)}); 216 213 } 217 214
Note: See TracChangeset
for help on using the changeset viewer.