Changeset f6bbc92


Ignore:
Timestamp:
Aug 7, 2024, 6:48:57 AM (5 weeks ago)
Author:
Peter A. Buhr <pabuhr@…>
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.
Message:

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

Files:
3 edited

Legend:

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

    re49c308 rf6bbc92  
    204204enum( char ) Letter { A = 'A', ... };
    205205enum( @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
     207void foo(Letter l);
     208foo(Beta);                      $\C{// foo(value(Beta))}$
     209\end{cfa}
     210Enumeration @Greek@ may have more or less enumerators than @Letter@, but the enum values \emph{must} be of a member of @Letter@.
     211Therefore, 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
     213in place of @Greek@. On the other hand, @Letter@ enums are not type compatible with enumeration @Greek@.
     214As a result, @Greek@ becomes a logical subtype of @Letter@.
     215
     216Subset defines an implicit subtyping relationship between two \CFA enumerations. \CFA also has
     217containment inheritance for \CFA enumerations where subtyping is explicitly structured.
    210218
    211219\section{Inheritance}
     
    236244However, the position of the underlying representation is the order of the enumerator in the new enumeration.
    237245\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.
     246enum() E1 { B };                                                                $\C{// B}$                                             
     247enum() E2 { C, D };                                                             $\C{// C D}$
     248enum() E3 { inline E1, inline E2, E };                  $\C{// {\color{red}[\(_{E1}\)} B {\color{red}]} {\color{red}[\(_{E2}\)} C D {\color{red}]} E}$
     249enum() 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}
     251In 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
     254A subtype enumeration can be casted, or implicitly converted into its supertype, with a @safe@ cost. Such conversion is an @enumeration conversion@.
    248255\begin{cfa}
    249256enum E2 e2 = C;
    250 posn( e2 );                     $\C[1.75in]{// 1}$
     257posn( e2 );                     $\C[1.75in]{// 0}$
    251258enum E3 e3 = e2;
    252 posn( e2 );                     $\C{// 2}$
     259posn( e2 );                     $\C{// 1}$
    253260void foo( E3 e );
    254261foo( e2 );
    255 posn( (E3)e2 );         $\C{// 2}$
     262posn( (E3)e2 );         $\C{// 1}$
    256263E3 e31 = B;
    257 posn( e31 );            $\C{// 1}\CRT$
     264posn( e31 );            $\C{// 0}\CRT$
    258265\end{cfa}
    259266The last expression is unambiguous.
    260267While 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}
    294268
    295269For the given function prototypes, the following calls are valid.
     
    313287Note, 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.
    314288
     289\subsection{Offset Calculation}
     290As discussed in \VRef{s:OpaqueEnum}, \CFA chooses position as a representation of a \CFA enumeration variable.
     291Because enumerators has different @position@ between subtype and supertype, \CFA might need to manipulate the representation whenever a cast or
     292implicit conversion involves two \CFA enums. \CFA determine how a position is going to change with an @offset calculation@ function, reflects to
     293enumerator as an arithmetic expression. Casting an enumeration of subtype to a supertype, the position can be unchanged or increase. The change
     294of position is an @offset@. 
     295
     296\begin{figure}
     297\begin{cfa}
     298struct Enumerator;
     299struct CFAEnum { vector<variant<CFAEnum, Enumerator>> members; string name; };
     300inline static bool operator==(CFAEnum& lhs, CFAEnum& rhs) { return lhs.name == rhs.name; }
     301pair<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
     325Figure~\ref{s:OffsetSubtypeSuperType} shows a minimum of @offset calculation@, written in \CC. CFAEnum represents \CFA enumeration
     326which has a vector of variants of Enumerator or CFAEnum. Two CFAEnums are differentiable by their unquie name.
     327The algorithm takes two CFAEnums as parameters @src@ and @dst@, with @src@ being type of expression the conversion applies on,
     328and @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@),
     329it 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
     330as 0.
     331
     332The algorithm iterates over members in @dst@ to find @src@. If a member is an enumerator of @dst@, the positions of all subsequent members
     333increment by one. If the current member is @dst@, the function returns true indicating "found" and the accumulated offset. Otherwise, the algorithm recurse
     334into 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@
     335is 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
     337of 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.
    315338
    316339\section{Control Structures}
  • doc/theses/jiada_liang_MMath/background.tex

    re49c308 rf6bbc92  
    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 implcitly, 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 implicitly, 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

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