Commit 5b8035fa authored by Kenton Varda's avatar Kenton Varda

Cleanup: Remove some redundant fields in descriptor structures.

parent 5d83e1db
......@@ -21,7 +21,7 @@
-- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
-- SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
module Compiler where
module Compiler (Status(..), parseAndCompileFile) where
import Grammar
import Semantics
......@@ -257,14 +257,6 @@ compileValue pos (StructType _) _ = makeExpectError pos "parenthesized list of f
compileValue pos (InterfaceType _) _ = makeError pos "Interfaces can't have default values."
compileValue pos (ListType _) _ = makeExpectError pos "list"
makeFileMemberMap :: FileDesc -> Map.Map String Desc
makeFileMemberMap desc = Map.fromList allMembers where
allMembers = [ (usingName m, DescUsing m) | m <- fileUsings desc ]
++ [ (constantName m, DescConstant m) | m <- fileConstants desc ]
++ [ (enumName m, DescEnum m) | m <- fileEnums desc ]
++ [ (structName m, DescStruct m) | m <- fileStructs desc ]
++ [ (interfaceName m, DescInterface m) | m <- fileInterfaces desc ]
descAsType _ (DescEnum desc) = succeed (EnumType desc)
descAsType _ (DescStruct desc) = succeed (StructType desc)
descAsType _ (DescInterface desc) = succeed (InterfaceType desc)
......@@ -595,7 +587,7 @@ compileDecl scope (EnumDecl (Located _ name) annotations decls) =
, enumerants = [d | DescEnumerant d <- members]
, enumAnnotations = compiledAnnotations
, enumMemberMap = memberMap
, enumStatements = members
, enumMembers = members
})))
compileDecl scope@(DescEnum parent)
......@@ -631,14 +623,9 @@ compileDecl scope (StructDecl (Located _ name) annotations decls) =
, structPacking = packing
, structFields = fields
, structUnions = unions
, structNestedUsings = [d | DescUsing d <- members]
, structNestedConstants = [d | DescConstant d <- members]
, structNestedEnums = [d | DescEnum d <- members]
, structNestedStructs = [d | DescStruct d <- members]
, structNestedInterfaces = [d | DescInterface d <- members]
, structAnnotations = compiledAnnotations
, structMemberMap = memberMap
, structStatements = members
, structMembers = members
, structFieldPackingMap = fieldPackingMap
})))
......@@ -663,7 +650,7 @@ compileDecl scope@(DescStruct parent)
, unionFields = fields
, unionAnnotations = compiledAnnotations
, unionMemberMap = memberMap
, unionStatements = members
, unionMembers = members
, unionFieldDiscriminantMap = discriminantMap
})))
compileDecl _ (UnionDecl (Located pos name) _ _ _) =
......@@ -711,15 +698,10 @@ compileDecl scope (InterfaceDecl (Located _ name) annotations decls) =
{ interfaceName = name
, interfaceId = theId
, interfaceParent = scope
, interfaceMethods = [d | DescMethod d <- members]
, interfaceNestedUsings = [d | DescUsing d <- members]
, interfaceNestedConstants = [d | DescConstant d <- members]
, interfaceNestedEnums = [d | DescEnum d <- members]
, interfaceNestedStructs = [d | DescStruct d <- members]
, interfaceNestedInterfaces = [d | DescInterface d <- members]
, interfaceMethods = [d | DescMethod d <- members]
, interfaceAnnotations = compiledAnnotations
, interfaceMemberMap = memberMap
, interfaceStatements = members
, interfaceMembers = members
})))
compileDecl scope@(DescInterface parent)
......@@ -783,15 +765,10 @@ compileFile name decls annotations importMap =
, fileImports = Map.elems importMap
, fileRuntimeImports =
Set.fromList $ map fileName $ concatMap descRuntimeImports members
, fileUsings = [d | DescUsing d <- members]
, fileConstants = [d | DescConstant d <- members]
, fileEnums = [d | DescEnum d <- members]
, fileStructs = [d | DescStruct d <- members]
, fileInterfaces = [d | DescInterface d <- members]
, fileAnnotations = compiledAnnotations
, fileMemberMap = memberMap
, fileImportMap = importMap
, fileStatements = members
, fileMembers = members
})
dedup :: Ord a => [a] -> [a]
......@@ -802,15 +779,10 @@ emptyFileDesc filename = FileDesc
, fileId = Nothing
, fileImports = []
, fileRuntimeImports = Set.empty
, fileUsings = []
, fileConstants = []
, fileEnums = []
, fileStructs = []
, fileInterfaces = []
, fileAnnotations = Map.empty
, fileMemberMap = Map.empty
, fileImportMap = Map.empty
, fileStatements = []
, fileMembers = []
}
parseAndCompileFile :: Monad m
......
......@@ -286,11 +286,11 @@ structContext parent desc = mkStrContext context where
context "structDataSize" = MuVariable $ packingDataSize $ structPacking desc
context "structReferenceCount" = MuVariable $ packingReferenceCount $ structPacking desc
context "structNestedEnums" =
MuList $ map (enumContext context) $ structNestedEnums desc
MuList $ map (enumContext context) [m | DescEnum m <- structMembers desc]
context "structNestedStructs" =
MuList $ map (childContext context . structName) $ structNestedStructs desc
MuList $ map (childContext context . structName) [m | DescStruct m <- structMembers desc]
context "structNestedInterfaces" =
MuList $ map (childContext context . interfaceName) $ structNestedInterfaces desc
MuList $ map (childContext context . interfaceName) [m | DescInterface m <- structMembers desc]
context s = parent s
typeContext parent desc = mkStrContext context where
......@@ -328,7 +328,7 @@ fileContext desc = mkStrContext context where
context "fileIncludeGuard" = MuVariable $
"CAPNPROTO_INCLUDED_" ++ hashString (fileName desc ++ ':':fromMaybe "" (fileId desc))
context "fileNamespaces" = MuList $ map (namespaceContext context) namespace
context "fileEnums" = MuList $ map (enumContext context) $ fileEnums desc
context "fileEnums" = MuList $ map (enumContext context) [e | DescEnum e <- fileMembers desc]
context "fileTypes" = MuList $ map (typeContext context) flattenedMembers
context "fileImports" = MuList $ map (importContext context . fst)
$ filter isImportUsed $ Map.toList $ fileImportMap desc
......
......@@ -335,15 +335,10 @@ data FileDesc = FileDesc
-- Set of imports which are used at runtime, i.e. not just for annotations.
-- The set contains file names matching files in fileImports.
, fileRuntimeImports :: Set.Set String
, fileUsings :: [UsingDesc]
, fileConstants :: [ConstantDesc]
, fileEnums :: [EnumDesc]
, fileStructs :: [StructDesc]
, fileInterfaces :: [InterfaceDesc]
, fileAnnotations :: AnnotationMap
, fileMemberMap :: MemberMap
, fileImportMap :: Map.Map String FileDesc
, fileStatements :: [Desc]
, fileMembers :: [Desc]
}
data UsingDesc = UsingDesc
......@@ -372,10 +367,10 @@ data EnumDesc = EnumDesc
, enumerants :: [EnumerantDesc]
, enumAnnotations :: AnnotationMap
, enumMemberMap :: MemberMap
, enumStatements :: [Desc]
, enumMembers :: [Desc]
}
enumRuntimeImports desc = concatMap descRuntimeImports $ enumStatements desc
enumRuntimeImports desc = concatMap descRuntimeImports $ enumMembers desc
data EnumerantDesc = EnumerantDesc
{ enumerantName :: String
......@@ -394,14 +389,9 @@ data StructDesc = StructDesc
, structPacking :: PackingState
, structFields :: [FieldDesc]
, structUnions :: [UnionDesc]
, structNestedUsings :: [UsingDesc]
, structNestedConstants :: [ConstantDesc]
, structNestedEnums :: [EnumDesc]
, structNestedStructs :: [StructDesc]
, structNestedInterfaces :: [InterfaceDesc]
, structAnnotations :: AnnotationMap
, structMemberMap :: MemberMap
, structStatements :: [Desc]
, structMembers :: [Desc]
-- Don't use this directly, use the members of FieldDesc and UnionDesc.
-- This field is exposed here only because I was too lazy to create a way to pass it on
......@@ -409,7 +399,7 @@ data StructDesc = StructDesc
, structFieldPackingMap :: Map.Map Integer (Integer, PackingState)
}
structRuntimeImports desc = concatMap descRuntimeImports $ structStatements desc
structRuntimeImports desc = concatMap descRuntimeImports $ structMembers desc
data UnionDesc = UnionDesc
{ unionName :: String
......@@ -421,13 +411,13 @@ data UnionDesc = UnionDesc
, unionFields :: [FieldDesc]
, unionAnnotations :: AnnotationMap
, unionMemberMap :: MemberMap
, unionStatements :: [Desc]
, unionMembers :: [Desc]
-- Maps field numbers to discriminants for all fields in the union.
, unionFieldDiscriminantMap :: Map.Map Integer Integer
}
unionRuntimeImports desc = concatMap descRuntimeImports $ unionStatements desc
unionRuntimeImports desc = concatMap descRuntimeImports $ unionMembers desc
data FieldDesc = FieldDesc
{ fieldName :: String
......@@ -449,17 +439,12 @@ data InterfaceDesc = InterfaceDesc
, interfaceId :: Maybe String
, interfaceParent :: Desc
, interfaceMethods :: [MethodDesc]
, interfaceNestedUsings :: [UsingDesc]
, interfaceNestedConstants :: [ConstantDesc]
, interfaceNestedEnums :: [EnumDesc]
, interfaceNestedStructs :: [StructDesc]
, interfaceNestedInterfaces :: [InterfaceDesc]
, interfaceAnnotations :: AnnotationMap
, interfaceMemberMap :: MemberMap
, interfaceStatements :: [Desc]
, interfaceMembers :: [Desc]
}
interfaceRuntimeImports desc = concatMap descRuntimeImports $ interfaceStatements desc
interfaceRuntimeImports desc = concatMap descRuntimeImports $ interfaceMembers desc
data MethodDesc = MethodDesc
{ methodName :: String
......@@ -506,7 +491,7 @@ descToCode indent self@(DescFile desc) = printf "# %s\n%s%s%s"
Just i -> printf "$id(%s);\n" $ show i
Nothing -> "")
(concatMap ((++ ";\n") . annotationCode self) $ Map.toList $ fileAnnotations desc)
(concatMap (descToCode indent) (fileStatements desc))
(concatMap (descToCode indent) (fileMembers desc))
descToCode indent (DescUsing desc) = printf "%susing %s = %s;\n" indent
(usingName desc)
(descQualifiedName (usingParent desc) (usingTarget desc))
......@@ -518,7 +503,7 @@ descToCode indent self@(DescConstant desc) = printf "%sconst %s: %s = %s%s;\n" i
descToCode indent self@(DescEnum desc) = printf "%senum %s%s {\n%s%s}\n" indent
(enumName desc)
(annotationsCode self)
(blockCode indent (enumStatements desc))
(blockCode indent (enumMembers desc))
indent
descToCode indent self@(DescEnumerant desc) = printf "%s%s @%d%s;\n" indent
(enumerantName desc) (enumerantNumber desc)
......@@ -526,7 +511,7 @@ descToCode indent self@(DescEnumerant desc) = printf "%s%s @%d%s;\n" indent
descToCode indent self@(DescStruct desc) = printf "%sstruct %s%s {\n%s%s}\n" indent
(structName desc)
(annotationsCode self)
(blockCode indent (structStatements desc))
(blockCode indent (structMembers desc))
indent
descToCode indent self@(DescField desc) = printf "%s%s@%d%s: %s%s%s; # %s\n" indent
(fieldName desc) (fieldNumber desc)
......@@ -545,12 +530,12 @@ descToCode indent self@(DescUnion desc) = printf "%sunion %s@%d%s { # [%d, %d)\
(unionName desc) (unionNumber desc)
(annotationsCode self)
(unionTagOffset desc * 16) (unionTagOffset desc * 16 + 16)
(blockCode indent $ unionStatements desc)
(blockCode indent $ unionMembers desc)
indent
descToCode indent self@(DescInterface desc) = printf "%sinterface %s%s {\n%s%s}\n" indent
(interfaceName desc)
(annotationsCode self)
(blockCode indent (interfaceStatements desc))
(blockCode indent (interfaceMembers desc))
indent
descToCode indent self@(DescMethod desc) = printf "%s%s@%d(%s): %s%s" indent
(methodName desc) (methodNumber desc)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment