Skip to content

Commit

Permalink
feat: Generate ${type}_Ptr classes to wrap C pointers.
Browse files Browse the repository at this point in the history
  • Loading branch information
iphydf committed Jan 7, 2024
1 parent 1469f8d commit 86a8fe3
Show file tree
Hide file tree
Showing 4 changed files with 212 additions and 105 deletions.
16 changes: 10 additions & 6 deletions src/Apigen/Language/PyDsl.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import Apigen.Types (BitSize (..), BuiltinType (..),
Module (..))
import Data.Text (Text)
import qualified Data.Text as Text
import Language.Cimple (Lexeme (..), lexemeText)
import Language.Cimple (Lexeme (..), LexemeClass)
import Prelude hiding ((<$>))
import Text.PrettyPrint.ANSI.Leijen

Expand Down Expand Up @@ -88,7 +88,7 @@ ppConstness ConstThis = text "True"
ppConstness MutableThis = text "False"

ppGenerated :: Generated -> Doc
ppGenerated = ppCtor . show
ppGenerated = ppCtor . ("Generated." <>) . show

ppBuiltinType :: BuiltinType -> Doc
ppBuiltinType Void = ppCtor "Void"
Expand All @@ -111,10 +111,14 @@ ppMaybe _ Nothing = text "None"
ppMaybe f (Just x) = f x

ppLexeme :: Lexeme Name -> Doc
ppLexeme = ppName . lexemeText

ppName :: Name -> Doc
ppName (ns, name) = hgo "Name" [ppList (text . show) ns, ppList (text . show) name]
ppLexeme (L _ c s) = ppName c s

ppName :: LexemeClass -> Name -> Doc
ppName c (ns, name) = hgo "Name"
[ ppCtor . ("LexemeClass." <>) . show $ c
, ppList (text . show) ns
, ppList (text . show) name
]

renderS :: Doc -> String
renderS = flip displayS "" . renderSmart 1 120
Expand Down
29 changes: 15 additions & 14 deletions src/Apigen/Parser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ runSimplify decls = do
go :: NodeF (Lexeme SId) [Sym] -> M a [Sym]
-- {-
go (PreprocInclude (L _ LitSysInclude _)) = return []
go (TyPointer [ConstType (BuiltinType UInt{}) ]) = return []
go (VarDecl [] _ []) = return []
go (FunctionPrototype [] _ _) = return []

go (PreprocIfndef (L _ _ SYM_APIGEN_IGNORE) _ es) = return es

Expand All @@ -74,10 +77,9 @@ go (Enumerator name _) = return [EnumMember name]
go (EnumConsts (Just name) enums ) = mkEnum name enums
go (EnumDecl name enums _) = mkEnum name enums

go (TyPointer [ BuiltinType ty@UInt{} ]) = return [ ArrayType ty]
go (TyPointer [ConstType (BuiltinType ty@UInt{}) ]) = return [ConstArrayType ty]
go (TyPointer [ConstType (BuiltinType Char )]) = return [BuiltinType String]
go (TyPointer [ConstType (Typename ty )]) = return [ConstPointerType ty]
go (TyPointer [ConstType (BuiltinType Char)]) = return [BuiltinType String]
go (TyPointer [ Typename ty ]) = return [ PointerType ty]
go (TyPointer [ConstType (Typename ty )]) = return [ConstPointerType ty]

go (DeclSpecArray (Just [expr])) = return [SizedArrayType (BuiltinType Void) expr]
go (DeclSpecArray Nothing) = return [ArrayType Void]
Expand All @@ -93,7 +95,6 @@ go (FunctionCall [Ref (L _ _ SYM_max)] [[a], [b]]) = return [Max a b]

go (TyConst [ty]) = return [ConstType ty]
go (TyPointer [BuiltinType Void]) = return [BuiltinType VoidPtr]
go (TyPointer [Typename ty]) = return [PointerType ty]
go (TyPointer ty@[CallbackType{}]) = return ty

go (AggregateDecl ty@[TypeDecl _]) = return ty
Expand All @@ -103,18 +104,18 @@ go (TyUserDefined ty) = return [Typename ty]
go (TyFunc ty) = return [CallbackType ty]
go (Typedef [Typename ty] _) = return [TypeDecl ty]

go (TyStd (L _ _ TY_void)) = return [BuiltinType Void]
go (TyStd (L _ _ TY_char)) = return [BuiltinType Char]
go (TyStd (L _ _ TY_bool)) = return [BuiltinType Bool]
go (TyStd (L _ _ TY_int8_t)) = return [BuiltinType (SInt B8)]
go (TyStd (L _ _ TY_uint8_t)) = return [BuiltinType (UInt B8)]
go (TyStd (L _ _ TY_int16_t)) = return [BuiltinType (SInt B16)]
go (TyStd (L _ _ TY_void )) = return [BuiltinType Void]
go (TyStd (L _ _ TY_char )) = return [BuiltinType Char]
go (TyStd (L _ _ TY_bool )) = return [BuiltinType Bool]
go (TyStd (L _ _ TY_int8_t )) = return [BuiltinType (SInt B8)]
go (TyStd (L _ _ TY_uint8_t )) = return [BuiltinType (UInt B8)]
go (TyStd (L _ _ TY_int16_t )) = return [BuiltinType (SInt B16)]
go (TyStd (L _ _ TY_uint16_t)) = return [BuiltinType (UInt B16)]
go (TyStd (L _ _ TY_int32_t)) = return [BuiltinType (SInt B32)]
go (TyStd (L _ _ TY_int32_t )) = return [BuiltinType (SInt B32)]
go (TyStd (L _ _ TY_uint32_t)) = return [BuiltinType (UInt B32)]
go (TyStd (L _ _ TY_int64_t)) = return [BuiltinType (SInt B64)]
go (TyStd (L _ _ TY_int64_t )) = return [BuiltinType (SInt B64)]
go (TyStd (L _ _ TY_uint64_t)) = return [BuiltinType (UInt B64)]
go (TyStd (L _ _ TY_size_t)) = return [BuiltinType SizeT]
go (TyStd (L _ _ TY_size_t )) = return [BuiltinType SizeT]

go (PreprocDefineConst name _) = return [Define name]
go (VarDecl [ty] name []) = return [Var ty name]
Expand Down
75 changes: 43 additions & 32 deletions tools/apigen.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,19 @@
from typing import Optional


class Generated(Enum):
GeneratedToString = 0
GeneratedFromInt = 1


class LexemeClass(Enum):
IdVar = 0
IdConst = 1
IdSueType = 2
IdFuncType = 3
LitInteger = 4


class CType:
pass

Expand Down Expand Up @@ -30,17 +43,9 @@ class UInt(CType):
bitSize: int


@dataclass
class Generated:
function: str


GeneratedToString = Generated("to_string")
GeneratedFromInt = Generated("from_int")


@dataclass
class Name:
kind: LexemeClass
ns: list[str]
name: list[str]

Expand All @@ -49,6 +54,10 @@ class Decl:
pass


class Type(Decl):
pass


@dataclass
class Namespace(Decl):
name: list[str]
Expand All @@ -74,26 +83,28 @@ class Enumeration(Decl):


@dataclass
class Property(Decl):
name: Name
prop: Decl


@dataclass
class ValueProp(Decl):
class Prop(Decl):
type: Decl
get: Optional[Decl]
set: Optional[Decl]


@dataclass
class ArrayProp(Decl):
type: Decl
get: Optional[Decl]
set: Optional[Decl]
class ValueProp(Prop):
pass


@dataclass
class ArrayProp(Prop):
size: Optional[Decl]


@dataclass
class Property(Decl):
name: Name
prop: Prop


@dataclass
class Ref(Decl):
name: Name
Expand Down Expand Up @@ -154,54 +165,54 @@ class Define(Decl):


@dataclass
class Typename(Decl):
class Typename(Type):
name: Name


@dataclass
class BuiltinType(Decl):
class BuiltinType(Type):
type: CType


@dataclass
class CallbackType(Decl):
class CallbackType(Type):
type: Name


@dataclass
class PointerType(Decl):
class PointerType(Type):
type: Name


@dataclass
class ConstPointerType(Decl):
class ConstPointerType(Type):
type: Name


@dataclass
class SizedArrayType(Decl):
type: Decl
class SizedArrayType(Type):
type: Type
size: Decl


@dataclass
class ArrayType(Decl):
class ArrayType(Type):
type: CType


@dataclass
class UserArrayType(Decl):
class UserArrayType(Type):
type: Name


@dataclass
class ConstArrayType(Decl):
class ConstArrayType(Type):
type: CType


@dataclass
class ConstType(Decl):
type: Decl
class ConstType(Type):
type: Type


@dataclass
Expand Down
Loading

0 comments on commit 86a8fe3

Please sign in to comment.