Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow creation of user-defined TypeMapping's. #172

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions compiler/src/Language/Bond/Codegen/TypeMapping.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ type system of a target programming language.
module Language.Bond.Codegen.TypeMapping
( -- * Mapping context
MappingContext(..)
, TypeMapping
, TypeMapping(..)
Copy link
Member

@chwarr chwarr Jun 3, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TypeMapping also refers to TypeNameBuilder. While we don't need to export that from the module because it's just a type alias, doing so would allow you to use TypeNameBuilder instead of Reader MappingContext Builder.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment for TypeMapping needs to be updated. It's no longer opaque.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is also worth considering if we shouldn't export some of the helpers that are usually needed to create a TypeMapping. In particular I'm thinking about elementTypeName, instanceTypeName, annotatedTypeName, aliasTypeName, declTypeName and declQualifiedTypeName.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed this, pushed updated commit.

, TypeNameBuilder
-- * Type mappings
, idlTypeMapping
, cppTypeMapping
Expand Down Expand Up @@ -48,6 +49,11 @@ module Language.Bond.Codegen.TypeMapping
, getNamespace
, getDeclNamespace
, customAliasMapping
-- * TypeMapping helper functions
, elementTypeName
, aliasTypeName
, declTypeName
, declQualifiedTypeName
) where

import Data.List
Expand All @@ -74,7 +80,7 @@ data MappingContext = MappingContext
, namespaces :: [Namespace]
}

-- | An opaque type representing a type mapping.
-- | A type representing a type mapping.
data TypeMapping = TypeMapping
{ language :: Maybe Language
, global :: Builder
Expand Down Expand Up @@ -237,6 +243,8 @@ typeName t = do
localWith :: (TypeMapping -> TypeMapping) -> TypeNameBuilder -> TypeNameBuilder
localWith f = local $ \c -> c { typeMapping = f $ typeMapping c }

-- | Builder for nested element types (e.g. list elements) in context of 'TypeNameBuilder' monad.
-- Used to implement 'mapType' function of 'TypeMapping'.
elementTypeName :: Type -> TypeNameBuilder
elementTypeName = localWith elementMapping . typeName

Expand All @@ -260,11 +268,15 @@ resolveNamespace MappingContext {..} ns =
declQualifiedName :: MappingContext -> Declaration -> QualifiedName
declQualifiedName c decl = getDeclNamespace c decl ++ [declName decl]

-- | Builder for the qualified name for a 'Declaration' in context of 'TypeNameBuilder' monad.
-- Used to implement 'mapType' function of 'TypeMapping'.
declQualifiedTypeName :: Declaration -> TypeNameBuilder
declQualifiedTypeName decl = do
ctx <- ask
return $ getDeclTypeName ctx decl

-- | Builder for the name for a 'Declaration' in context of 'TypeNameBuilder' monad.
-- Used to implement 'mapType' function of 'TypeMapping'.
declTypeName :: Declaration -> TypeNameBuilder
declTypeName decl = do
ctx <- ask
Expand All @@ -279,6 +291,8 @@ findAliasMapping ctx a = find isSameAlias $ aliasMapping ctx
isSameNs = namespaces ctx == declNamespaces a
isSameAlias m = aliasDeclName == aliasName m || isSameNs && [declName a] == aliasName m

-- | Builder for the type alias name in context of 'TypeNameBuilder' monad.
-- Used to implement 'mapType' function of 'TypeMapping'.
aliasTypeName :: Declaration -> [Type] -> TypeNameBuilder
aliasTypeName a args = do
ctx <- ask
Expand Down