Skip to content

Commit

Permalink
Provide ability to check type equality modulo opaque pointers
Browse files Browse the repository at this point in the history
This patch implements `eqTypeModuloOpaquePtrs` and `cmpTypeModuloOpaquePtrs`
functions, which provide coarser notions of type equality and comparison that
do not distinguish between `PtrTo` and `PtrOpaque`, unlike the `Eq` and `Ord`
instances for `Type`. This is useful to support code in which `PtrTo` and
`PtrOpaque` exist in the same file.

See GaloisInc#102.
  • Loading branch information
RyanGlScott committed May 30, 2023
1 parent 16bc5bd commit f32f773
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions src/Text/LLVM/AST.hs
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,60 @@ isPointer PtrOpaque = True
isPointer _ = False


-- | Like `Type'`, but where the 'PtrTo' and 'PtrOpaque' constructors have been
-- collapsed into a single 'PtrView' constructor. This provides a coarser notion
-- of type equality than what `Type'` provides, which distinguishes the two
-- types of pointers.
--
-- `TypeView'` is not used directly in any of the other AST types. Instead, it
-- is used only as an internal data type to power the 'eqTypeModuloOpaquePtrs'
-- and 'cmpTypeModuloOpaquePtrs' functions.
data TypeView' ident
= PrimTypeView PrimType
| AliasView ident
| ArrayView Word64 (TypeView' ident)
| FunTyView (TypeView' ident) [TypeView' ident] Bool
| PtrView
-- ^ The sole pointer type. Both 'PtrTo' and 'PtrOpaque' are mapped to
-- 'PtrView'.
| StructView [TypeView' ident]
| PackedStructView [TypeView' ident]
| VectorView Word64 (TypeView' ident)
| OpaqueView
-- ^ An opaque structure type, used to represent structure types that do not
-- forward-declared structure.
--
-- 'OpaqueView' should not be confused with opaque pointers, which are
-- mapped to 'PtrView'.
deriving (Eq, Ord)

-- | Convert a `Type'` value to a `TypeView'` value.
typeView :: Type' ident -> TypeView' ident
-- The two most important cases. Both forms of pointers are mapped to PtrView.
typeView (PtrTo _) = PtrView
typeView PtrOpaque = PtrView
-- All other cases are straightforward.
typeView (PrimType pt) = PrimTypeView pt
typeView (Alias lab) = AliasView lab
typeView (Array len et) = ArrayView len (typeView et)
typeView (FunTy ret args va) = FunTyView (typeView ret) (map typeView args) va
typeView (Struct fs) = StructView (map typeView fs)
typeView (PackedStruct fs) = PackedStructView (map typeView fs)
typeView (Vector len et) = VectorView len (typeView et)
typeView Opaque = OpaqueView

-- | Check two 'Type's for equality, but treat 'PtrOpaque' types as being equal
-- to @'PtrTo' ty@ types (for any type @ty@). This is a coarser notion of
-- equality than what is provided by the 'Eq' instance for 'Type'.
eqTypeModuloOpaquePtrs :: Eq ident => Type' ident -> Type' ident -> Bool
eqTypeModuloOpaquePtrs x y = typeView x == typeView y

-- | Compare two 'Type's, but treat 'PtrOpaque' types as being equal to
-- @'PtrTo' ty@ types (for any type @ty@). This is a coarser notion of ordering
-- than what is provided by the 'Ord' instance for 'Type'.
cmpTypeModuloOpaquePtrs :: Ord ident => Type' ident -> Type' ident -> Ordering
cmpTypeModuloOpaquePtrs x y = typeView x `compare` typeView y

-- Null Values -----------------------------------------------------------------

data NullResult lab
Expand Down

0 comments on commit f32f773

Please sign in to comment.