Changes in / [f6bbc92:e49c308]


Ignore:
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • doc/theses/jiada_liang_MMath/CFAenum.tex

    rf6bbc92 re49c308  
    204204enum( char ) Letter { A = 'A', ... };
    205205enum( @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}
     207Enumeration @Greek@ may have more or less enums than @Letter@, but the enum values \emph{must} be from @Letter@.
     208Therefore, @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
    218210
    219211\section{Inheritance}
     
    244236However, the position of the underlying representation is the order of the enumerator in the new enumeration.
    245237\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@.
     238enum() E1 { A };
     239enum() E2 { B, C };
     240enum() E3 { inline E1, inline E2, D };
     241\end{cfa}
     242Here, @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
     247A subtype enumeration can be casted, or implicitly converted into its supertype, with a @safe@ cost.
    255248\begin{cfa}
    256249enum E2 e2 = C;
    257 posn( e2 );                     $\C[1.75in]{// 0}$
     250posn( e2 );                     $\C[1.75in]{// 1}$
    258251enum E3 e3 = e2;
    259 posn( e2 );                     $\C{// 1}$
     252posn( e2 );                     $\C{// 2}$
    260253void foo( E3 e );
    261254foo( e2 );
    262 posn( (E3)e2 );         $\C{// 1}$
     255posn( (E3)e2 );         $\C{// 2}$
    263256E3 e31 = B;
    264 posn( e31 );            $\C{// 0}\CRT$
     257posn( e31 );            $\C{// 1}\CRT$
    265258\end{cfa}
    266259The last expression is unambiguous.
    267260While 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@.
     261Hence, as discussed in \VRef{s:OpaqueEnum}, \CFA chooses position as a representation of the \CFA enum.
     262Therefore, conversion involves both a change of type and possibly position.
     263
     264When converting a subtype to a supertype, its position can only be a larger value.
     265The 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}
     271struct Enumerator;
     272struct CFAEnum {
     273        vector<variant<CFAEnum, Enumerator>> members;
     274};
     275pair<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}
    268294
    269295For the given function prototypes, the following calls are valid.
     
    287313Note, 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.
    288314
    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.
    338315
    339316\section{Control Structures}
  • doc/theses/jiada_liang_MMath/background.tex

    rf6bbc92 re49c308  
    487487
    488488In \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 implicitly, while being possible with an explicit cast. These conversions are often defined to have
     489Cast 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
    490490infinite conversion cost and non-infinite cast cost.
  • src/Validate/ImplementEnumFunc.cpp

    rf6bbc92 re49c308  
    210210                "value",
    211211                {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();
    213216}
    214217
Note: See TracChangeset for help on using the changeset viewer.