inline-c-0.9.1.10: Write Haskell source files including C code inline. No FFI required.

Safe Haskell | None
Language | Haskell2010

Language.C.Types

Contents

- Types
- Parsing
- Convert to and from high-level views
- To english

Description

Views of C datatypes. While Language.C.Types.Parse defines datatypes for representing the concrete syntax tree of C types, this module provides friendlier views of C types, by turning them into a data type matching more closely how we read and think about types, both in Haskell and in C. To appreciate the difference, look at the difference between `ParameterDeclaration` and `ParameterDeclaration`.

As a bonus, routines are provided for describing types in natural language (English) -- see `describeParameterDeclaration` and `describeType`.

Synopsis

- data CIdentifier
- unCIdentifier :: CIdentifier -> String
- cIdentifierFromString :: Bool -> String -> Either String CIdentifier
- data StorageClassSpecifier
  - = TYPEDEF
  - | EXTERN
  - | STATIC
  - | AUTO
  - | REGISTER
- data TypeQualifier
  - = CONST
  - | RESTRICT
  - | VOLATILE
- data FunctionSpecifier = INLINE
- data ArrayType i
  - = VariablySized
  - | Unsized
  - | SizedByInteger Integer
  - | SizedByIdentifier i
- data Specifiers = Specifiers {
  - storageClassSpecifiers :: [StorageClassSpecifier]
  - typeQualifiers :: [TypeQualifier]
  - functionSpecifiers :: [FunctionSpecifier]
  }
- data Type i
  - = TypeSpecifier Specifiers TypeSpecifier
  - | Ptr [TypeQualifier] (Type i)
  - | Array (ArrayType i) (Type i)
  - | Proto (Type i) [ParameterDeclaration i]
- data TypeSpecifier
  - = Void
  - | Bool
  - | Char (Maybe Sign)
  - | Short Sign
  - | Int Sign
  - | Long Sign
  - | LLong Sign
  - | Float
  - | Double
  - | LDouble
  - | TypeName CIdentifier
  - | Struct CIdentifier
  - | Enum CIdentifier
  - | Template CIdentifier [TypeSpecifier]
  - | TemplateConst String
  - | TemplatePointer TypeSpecifier
- data Sign
  - = Signed
  - | Unsigned
- data ParameterDeclaration i = ParameterDeclaration {
  - parameterDeclarationId :: Maybe i
  - parameterDeclarationType :: Type i
  }
- type TypeNames = HashSet CIdentifier
- type CParser i (m :: Type -> Type) = (Monad m, Functor m, Applicative m, MonadPlus m, Parsing m, CharParsing m, TokenParsing m, LookAheadParsing m, MonadReader (CParserContext i) m, MonadFail m, Hashable i)
- data CParserContext i
- cCParserContext :: Bool -> TypeNames -> CParserContext CIdentifier
- runCParser :: Stream s Identity Char => CParserContext i -> String -> s -> ReaderT (CParserContext i) (Parsec s ()) a -> Either ParseError a
- quickCParser :: CParserContext i -> String -> ReaderT (CParserContext i) (Parsec String ()) a -> a
- quickCParser_ :: Bool -> String -> ReaderT (CParserContext CIdentifier) (Parsec String ()) a -> a
- parseParameterDeclaration :: (CParser i m, Pretty i) => m (ParameterDeclaration i)
- parseParameterList :: (CParser i m, Pretty i) => m [ParameterDeclaration i]
- parseIdentifier :: CParser i m => m i
- parseEnableCpp :: CParser i m => m Bool
- parseType :: (CParser i m, Pretty i) => m (Type i)
- data UntangleErr
  - = MultipleDataTypes [DeclarationSpecifier]
  - | NoDataTypes [DeclarationSpecifier]
  - | IllegalSpecifiers String [TypeSpecifier]
- untangleParameterDeclaration :: ParameterDeclaration i -> Either UntangleErr (ParameterDeclaration i)
- tangleParameterDeclaration :: ParameterDeclaration i -> ParameterDeclaration i
- describeParameterDeclaration :: Pretty i => ParameterDeclaration i -> Doc ann
- describeType :: Pretty i => Type i -> Doc ann

# Types

data CIdentifier Source #

A type for C identifiers.

#### Instances

Instances details

IsString CIdentifier Source # |
Instance details Defined in Language.C.Types.Parse Methods fromString :: String -> CIdentifier Source #
Show CIdentifier Source # |
Instance details Defined in Language.C.Types.Parse Methods showsPrec :: Int -> CIdentifier -> ShowS Source # show :: CIdentifier -> String Source # showList :: [CIdentifier] -> ShowS Source #
Eq CIdentifier Source # |
Instance details Defined in Language.C.Types.Parse Methods (==) :: CIdentifier -> CIdentifier -> Bool Source # (/=) :: CIdentifier -> CIdentifier -> Bool Source #
Ord CIdentifier Source # |

Instance details

Defined in Language.C.Types.Parse

Methods

compare :: CIdentifier -> CIdentifier -> Ordering Source #

(<) :: CIdentifier -> CIdentifier -> Bool Source #

(<=) :: CIdentifier -> CIdentifier -> Bool Source #

(>) :: CIdentifier -> CIdentifier -> Bool Source #

(>=) :: CIdentifier -> CIdentifier -> Bool Source #

max :: CIdentifier -> CIdentifier -> CIdentifier Source #

min :: CIdentifier -> CIdentifier -> CIdentifier Source #

Hashable CIdentifier Source # |
Instance details Defined in Language.C.Types.Parse Methods hashWithSalt :: Int -> CIdentifier -> Int Source # hash :: CIdentifier -> Int Source #
Pretty CIdentifier Source # |
Instance details Defined in Language.C.Types.Parse Methods pretty :: CIdentifier -> Doc ann Source # prettyList :: [CIdentifier] -> Doc ann Source #

unCIdentifier :: CIdentifier -> String Source #

cIdentifierFromString :: Bool -> String -> Either String CIdentifier Source #

data StorageClassSpecifier Source #

Constructors

TYPEDEF |
EXTERN |
STATIC |
AUTO |
REGISTER |

#### Instances

Instances details

Show StorageClassSpecifier Source # |
Instance details Defined in Language.C.Types.Parse Methods showsPrec :: Int -> StorageClassSpecifier -> ShowS Source # show :: StorageClassSpecifier -> String Source # showList :: [StorageClassSpecifier] -> ShowS Source #
Eq StorageClassSpecifier Source # |
Instance details Defined in Language.C.Types.Parse Methods (==) :: StorageClassSpecifier -> StorageClassSpecifier -> Bool Source # (/=) :: StorageClassSpecifier -> StorageClassSpecifier -> Bool Source #
Pretty StorageClassSpecifier Source # |
Instance details Defined in Language.C.Types.Parse Methods pretty :: StorageClassSpecifier -> Doc ann Source # prettyList :: [StorageClassSpecifier] -> Doc ann Source #

data TypeQualifier Source #

Constructors

CONST |
RESTRICT |
VOLATILE |

#### Instances

Instances details

Show TypeQualifier Source # |
Instance details Defined in Language.C.Types.Parse Methods showsPrec :: Int -> TypeQualifier -> ShowS Source # show :: TypeQualifier -> String Source # showList :: [TypeQualifier] -> ShowS Source #
Eq TypeQualifier Source # |
Instance details Defined in Language.C.Types.Parse Methods (==) :: TypeQualifier -> TypeQualifier -> Bool Source # (/=) :: TypeQualifier -> TypeQualifier -> Bool Source #
Pretty TypeQualifier Source # |
Instance details Defined in Language.C.Types.Parse Methods pretty :: TypeQualifier -> Doc ann Source # prettyList :: [TypeQualifier] -> Doc ann Source #

data FunctionSpecifier Source #

Constructors

INLINE |

#### Instances

Instances details

Show FunctionSpecifier Source # |
Instance details Defined in Language.C.Types.Parse Methods showsPrec :: Int -> FunctionSpecifier -> ShowS Source # show :: FunctionSpecifier -> String Source # showList :: [FunctionSpecifier] -> ShowS Source #
Eq FunctionSpecifier Source # |
Instance details Defined in Language.C.Types.Parse Methods (==) :: FunctionSpecifier -> FunctionSpecifier -> Bool Source # (/=) :: FunctionSpecifier -> FunctionSpecifier -> Bool Source #
Pretty FunctionSpecifier Source # |
Instance details Defined in Language.C.Types.Parse Methods pretty :: FunctionSpecifier -> Doc ann Source # prettyList :: [FunctionSpecifier] -> Doc ann Source #

data ArrayType i Source #

Constructors

VariablySized |
Unsized |
SizedByInteger Integer |
SizedByIdentifier i |

#### Instances

Instances details

Functor ArrayType Source # |
Instance details Defined in Language.C.Types.Parse Methods fmap :: (a -> b) -> ArrayType a -> ArrayType b Source # (<$) :: a -> ArrayType b -> ArrayType a Source #
Foldable ArrayType Source # |

Instance details

Defined in Language.C.Types.Parse

Methods

fold :: Monoid m => ArrayType m -> m Source #

foldMap :: Monoid m => (a -> m) -> ArrayType a -> m Source #

foldMap' :: Monoid m => (a -> m) -> ArrayType a -> m Source #

foldr :: (a -> b -> b) -> b -> ArrayType a -> b Source #

foldr' :: (a -> b -> b) -> b -> ArrayType a -> b Source #

foldl :: (b -> a -> b) -> b -> ArrayType a -> b Source #

foldl' :: (b -> a -> b) -> b -> ArrayType a -> b Source #

foldr1 :: (a -> a -> a) -> ArrayType a -> a Source #

foldl1 :: (a -> a -> a) -> ArrayType a -> a Source #

toList :: ArrayType a -> [a] Source #

null :: ArrayType a -> Bool Source #

length :: ArrayType a -> Int Source #

elem :: Eq a => a -> ArrayType a -> Bool Source #

maximum :: Ord a => ArrayType a -> a Source #

minimum :: Ord a => ArrayType a -> a Source #

sum :: Num a => ArrayType a -> a Source #

product :: Num a => ArrayType a -> a Source #

Traversable ArrayType Source # |
Instance details Defined in Language.C.Types.Parse Methods traverse :: Applicative f => (a -> f b) -> ArrayType a -> f (ArrayType b) Source # sequenceA :: Applicative f => ArrayType (f a) -> f (ArrayType a) Source # mapM :: Monad m => (a -> m b) -> ArrayType a -> m (ArrayType b) Source # sequence :: Monad m => ArrayType (m a) -> m (ArrayType a) Source #
Show i => Show (ArrayType i) Source # |
Instance details Defined in Language.C.Types.Parse Methods showsPrec :: Int -> ArrayType i -> ShowS Source # show :: ArrayType i -> String Source # showList :: [ArrayType i] -> ShowS Source #
Eq i => Eq (ArrayType i) Source # |
Instance details Defined in Language.C.Types.Parse Methods (==) :: ArrayType i -> ArrayType i -> Bool Source # (/=) :: ArrayType i -> ArrayType i -> Bool Source #
Pretty i => Pretty (ArrayType i) Source # |
Instance details Defined in Language.C.Types.Parse Methods pretty :: ArrayType i -> Doc ann Source # prettyList :: [ArrayType i] -> Doc ann Source #

data Specifiers Source #

Constructors

Specifiers |

Fields

- storageClassSpecifiers :: [StorageClassSpecifier]
- typeQualifiers :: [TypeQualifier]
- functionSpecifiers :: [FunctionSpecifier]

#### Instances

Instances details

Monoid Specifiers Source # |
Instance details Defined in Language.C.Types Methods mempty :: Specifiers Source # mappend :: Specifiers -> Specifiers -> Specifiers Source # mconcat :: [Specifiers] -> Specifiers Source #
Semigroup Specifiers Source # |
Instance details Defined in Language.C.Types Methods (<>) :: Specifiers -> Specifiers -> Specifiers Source # sconcat :: NonEmpty Specifiers -> Specifiers Source # stimes :: Integral b => b -> Specifiers -> Specifiers Source #
Show Specifiers Source # |
Instance details Defined in Language.C.Types Methods showsPrec :: Int -> Specifiers -> ShowS Source # show :: Specifiers -> String Source # showList :: [Specifiers] -> ShowS Source #
Eq Specifiers Source # |
Instance details Defined in Language.C.Types Methods (==) :: Specifiers -> Specifiers -> Bool Source # (/=) :: Specifiers -> Specifiers -> Bool Source #

data Type i Source #

Constructors

TypeSpecifier Specifiers TypeSpecifier |
Ptr [TypeQualifier] (Type i) |
Array (ArrayType i) (Type i) |
Proto (Type i) [ParameterDeclaration i] |

#### Instances

Instances details

Functor Type Source # |
Instance details Defined in Language.C.Types Methods fmap :: (a -> b) -> Type a -> Type b Source # (<$) :: a -> Type b -> Type a Source #
Foldable Type Source # |

Instance details

Defined in Language.C.Types

Methods

fold :: Monoid m => Type m -> m Source #

foldMap :: Monoid m => (a -> m) -> Type a -> m Source #

foldMap' :: Monoid m => (a -> m) -> Type a -> m Source #

foldr :: (a -> b -> b) -> b -> Type a -> b Source #

foldr' :: (a -> b -> b) -> b -> Type a -> b Source #

foldl :: (b -> a -> b) -> b -> Type a -> b Source #

foldl' :: (b -> a -> b) -> b -> Type a -> b Source #

foldr1 :: (a -> a -> a) -> Type a -> a Source #

foldl1 :: (a -> a -> a) -> Type a -> a Source #

toList :: Type a -> [a] Source #

null :: Type a -> Bool Source #

length :: Type a -> Int Source #

elem :: Eq a => a -> Type a -> Bool Source #

maximum :: Ord a => Type a -> a Source #

minimum :: Ord a => Type a -> a Source #

sum :: Num a => Type a -> a Source #

product :: Num a => Type a -> a Source #

Traversable Type Source # |
Instance details Defined in Language.C.Types Methods traverse :: Applicative f => (a -> f b) -> Type a -> f (Type b) Source # sequenceA :: Applicative f => Type (f a) -> f (Type a) Source # mapM :: Monad m => (a -> m b) -> Type a -> m (Type b) Source # sequence :: Monad m => Type (m a) -> m (Type a) Source #
Show i => Show (Type i) Source # |
Instance details Defined in Language.C.Types Methods showsPrec :: Int -> Type i -> ShowS Source # show :: Type i -> String Source # showList :: [Type i] -> ShowS Source #
Eq i => Eq (Type i) Source # |
Instance details Defined in Language.C.Types Methods (==) :: Type i -> Type i -> Bool Source # (/=) :: Type i -> Type i -> Bool Source #
Pretty i => Pretty (Type i) Source # |
Instance details Defined in Language.C.Types Methods pretty :: Type i -> Doc ann Source # prettyList :: [Type i] -> Doc ann Source #

data TypeSpecifier Source #

Constructors

Void |
Bool |
Char (Maybe Sign) |
Short Sign |
Int Sign |
Long Sign |
LLong Sign |
Float |
Double |
LDouble |
TypeName CIdentifier |
Struct CIdentifier |
Enum CIdentifier |
Template CIdentifier [TypeSpecifier] |
TemplateConst String |
TemplatePointer TypeSpecifier |

#### Instances

Instances details

Show TypeSpecifier Source # |
Instance details Defined in Language.C.Types Methods showsPrec :: Int -> TypeSpecifier -> ShowS Source # show :: TypeSpecifier -> String Source # showList :: [TypeSpecifier] -> ShowS Source #
Eq TypeSpecifier Source # |
Instance details Defined in Language.C.Types Methods (==) :: TypeSpecifier -> TypeSpecifier -> Bool Source # (/=) :: TypeSpecifier -> TypeSpecifier -> Bool Source #
Ord TypeSpecifier Source # |

Instance details

Defined in Language.C.Types

Methods

compare :: TypeSpecifier -> TypeSpecifier -> Ordering Source #

(<) :: TypeSpecifier -> TypeSpecifier -> Bool Source #

(<=) :: TypeSpecifier -> TypeSpecifier -> Bool Source #

(>) :: TypeSpecifier -> TypeSpecifier -> Bool Source #

(>=) :: TypeSpecifier -> TypeSpecifier -> Bool Source #

max :: TypeSpecifier -> TypeSpecifier -> TypeSpecifier Source #

min :: TypeSpecifier -> TypeSpecifier -> TypeSpecifier Source #

Pretty TypeSpecifier Source # |
Instance details Defined in Language.C.Types Methods pretty :: TypeSpecifier -> Doc ann Source # prettyList :: [TypeSpecifier] -> Doc ann Source #

data Sign Source #

Constructors

Signed |
Unsigned |

#### Instances

Instances details

Show Sign Source # |
Instance details Defined in Language.C.Types Methods showsPrec :: Int -> Sign -> ShowS Source # show :: Sign -> String Source # showList :: [Sign] -> ShowS Source #
Eq Sign Source # |
Instance details Defined in Language.C.Types Methods (==) :: Sign -> Sign -> Bool Source # (/=) :: Sign -> Sign -> Bool Source #
Ord Sign Source # |
Instance details Defined in Language.C.Types Methods compare :: Sign -> Sign -> Ordering Source # (<) :: Sign -> Sign -> Bool Source # (<=) :: Sign -> Sign -> Bool Source # (>) :: Sign -> Sign -> Bool Source # (>=) :: Sign -> Sign -> Bool Source # max :: Sign -> Sign -> Sign Source # min :: Sign -> Sign -> Sign Source #

data ParameterDeclaration i Source #

Constructors

ParameterDeclaration |

Fields

- parameterDeclarationId :: Maybe i
- parameterDeclarationType :: Type i

#### Instances

Instances details

Functor ParameterDeclaration Source # |
Instance details Defined in Language.C.Types Methods fmap :: (a -> b) -> ParameterDeclaration a -> ParameterDeclaration b Source # (<$) :: a -> ParameterDeclaration b -> ParameterDeclaration a Source #
Foldable ParameterDeclaration Source # |

Instance details

Defined in Language.C.Types

Methods

fold :: Monoid m => ParameterDeclaration m -> m Source #

foldMap :: Monoid m => (a -> m) -> ParameterDeclaration a -> m Source #

foldMap' :: Monoid m => (a -> m) -> ParameterDeclaration a -> m Source #

foldr :: (a -> b -> b) -> b -> ParameterDeclaration a -> b Source #

foldr' :: (a -> b -> b) -> b -> ParameterDeclaration a -> b Source #

foldl :: (b -> a -> b) -> b -> ParameterDeclaration a -> b Source #

foldl' :: (b -> a -> b) -> b -> ParameterDeclaration a -> b Source #

foldr1 :: (a -> a -> a) -> ParameterDeclaration a -> a Source #

foldl1 :: (a -> a -> a) -> ParameterDeclaration a -> a Source #

toList :: ParameterDeclaration a -> [a] Source #

null :: ParameterDeclaration a -> Bool Source #

length :: ParameterDeclaration a -> Int Source #

elem :: Eq a => a -> ParameterDeclaration a -> Bool Source #

maximum :: Ord a => ParameterDeclaration a -> a Source #

minimum :: Ord a => ParameterDeclaration a -> a Source #

sum :: Num a => ParameterDeclaration a -> a Source #

product :: Num a => ParameterDeclaration a -> a Source #

Traversable ParameterDeclaration Source # |

Instance details

Defined in Language.C.Types

Methods

traverse :: Applicative f => (a -> f b) -> ParameterDeclaration a -> f (ParameterDeclaration b) Source #

sequenceA :: Applicative f => ParameterDeclaration (f a) -> f (ParameterDeclaration a) Source #

mapM :: Monad m => (a -> m b) -> ParameterDeclaration a -> m (ParameterDeclaration b) Source #

sequence :: Monad m => ParameterDeclaration (m a) -> m (ParameterDeclaration a) Source #

Show i => Show (ParameterDeclaration i) Source # |
Instance details Defined in Language.C.Types Methods showsPrec :: Int -> ParameterDeclaration i -> ShowS Source # show :: ParameterDeclaration i -> String Source # showList :: [ParameterDeclaration i] -> ShowS Source #
Eq i => Eq (ParameterDeclaration i) Source # |
Instance details Defined in Language.C.Types Methods (==) :: ParameterDeclaration i -> ParameterDeclaration i -> Bool Source # (/=) :: ParameterDeclaration i -> ParameterDeclaration i -> Bool Source #
Pretty i => Pretty (ParameterDeclaration i) Source # |
Instance details Defined in Language.C.Types Methods pretty :: ParameterDeclaration i -> Doc ann Source # prettyList :: [ParameterDeclaration i] -> Doc ann Source #

# Parsing

type TypeNames = HashSet CIdentifier Source #

A collection of named types (typedefs)

type CParser i (m :: Type -> Type) = (Monad m, Functor m, Applicative m, MonadPlus m, Parsing m, CharParsing m, TokenParsing m, LookAheadParsing m, MonadReader (CParserContext i) m, MonadFail m, Hashable i) Source #

All the parsing is done using the type classes provided by the `parsers` package. You can use the parsing routines with any of the parsers that implement the classes, such as `parsec` or `trifecta`.

We parametrize the parsing by the type of the variable identifiers, `i`. We do so because we use this parser to implement anti-quoters referring to Haskell variables, and thus we need to parse Haskell identifiers in certain positions.

data CParserContext i Source #

cCParserContext :: Bool -> TypeNames -> CParserContext CIdentifier Source #

runCParser Source #

Arguments

:: Stream s Identity Char |
=> CParserContext i |
-> String | Source name.
-> s | String to parse.
-> ReaderT (CParserContext i) (Parsec s ()) a | Parser. Anything with type `forall m. CParser i m => m a` is a valid argument.
-> Either ParseError a |

Runs a ``CParser`` using `parsec`.

quickCParser Source #

Arguments

:: CParserContext i |
-> String | String to parse.
-> ReaderT (CParserContext i) (Parsec String ()) a | Parser. Anything with type `forall m. CParser i m => m a` is a valid argument.
-> a |

Useful for quick testing. Uses `"quickCParser"` as source name, and throws an `error` if parsing fails.

quickCParser_ Source #

Arguments

:: Bool |
-> String | String to parse.
-> ReaderT (CParserContext CIdentifier) (Parsec String ()) a | Parser. Anything with type `forall m. CParser i m => m a` is a valid argument.
-> a |

Like `quickCParser`, but uses ``cCParserContext` (`const` `False`)` as `CParserContext`.

parseParameterDeclaration :: (CParser i m, Pretty i) => m (ParameterDeclaration i) Source #

parseParameterList :: (CParser i m, Pretty i) => m [ParameterDeclaration i] Source #

parseIdentifier :: CParser i m => m i Source #

parseEnableCpp :: CParser i m => m Bool Source #

parseType :: (CParser i m, Pretty i) => m (Type i) Source #

# Convert to and from high-level views

data UntangleErr Source #

Constructors

MultipleDataTypes [DeclarationSpecifier] |
NoDataTypes [DeclarationSpecifier] |
IllegalSpecifiers String [TypeSpecifier] |

#### Instances

Instances details

Show UntangleErr Source # |
Instance details Defined in Language.C.Types Methods showsPrec :: Int -> UntangleErr -> ShowS Source # show :: UntangleErr -> String Source # showList :: [UntangleErr] -> ShowS Source #
Eq UntangleErr Source # |
Instance details Defined in Language.C.Types Methods (==) :: UntangleErr -> UntangleErr -> Bool Source # (/=) :: UntangleErr -> UntangleErr -> Bool Source #
Pretty UntangleErr Source # |
Instance details Defined in Language.C.Types Methods pretty :: UntangleErr -> Doc ann Source # prettyList :: [UntangleErr] -> Doc ann Source #

untangleParameterDeclaration :: ParameterDeclaration i -> Either UntangleErr (ParameterDeclaration i) Source #

tangleParameterDeclaration :: ParameterDeclaration i -> ParameterDeclaration i Source #

# To english

describeParameterDeclaration :: Pretty i => ParameterDeclaration i -> Doc ann Source #

describeType :: Pretty i => Type i -> Doc ann Source #