summaryrefslogtreecommitdiffstats
path: root/ipc/ipdl/ipdl/__init__.py
blob: 50ceb4f95303ca329604d0af12584b29b9f09731 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

__all__ = [
    "gencxx",
    "genipdl",
    "parse",
    "typecheck",
    "writeifmodified",
    "checkSyncMessage",
    "checkFixedSyncMessages",
]

import os
import sys
from io import StringIO

from ipdl.cgen import IPDLCodeGen
from ipdl.lower import LowerToCxx, msgenums
from ipdl.parser import Parser, ParseError
from ipdl.type import TypeCheck
from ipdl.checker import checkSyncMessage, checkFixedSyncMessages

from ipdl.cxx.cgen import CxxCodeGen


def parse(specstring, filename="/stdin", includedirs=[], errout=sys.stderr):
    """Return an IPDL AST if parsing was successful.  Print errors to |errout|
    if it is not."""
    # The file type and name are later enforced by the type checker.
    # This is just a hint to the parser.
    prefix, ext = os.path.splitext(filename)
    name = os.path.basename(prefix)
    if ext == ".ipdlh":
        type = "header"
    else:
        type = "protocol"

    try:
        return Parser(type, name).parse(
            specstring, os.path.abspath(filename), includedirs
        )
    except ParseError as p:
        print(p, file=errout)
        return None


def typecheck(ast, errout=sys.stderr):
    """Return True iff |ast| is well typed.  Print errors to |errout| if
    it is not."""
    return TypeCheck().check(ast, errout)


def gencxx(ipdlfilename, ast, outheadersdir, outcppdir, segmentcapacitydict):
    headers, cpps = LowerToCxx().lower(ast, segmentcapacitydict)

    def resolveHeader(hdr):
        return [
            hdr,
            os.path.join(
                outheadersdir, *([ns.name for ns in ast.namespaces] + [hdr.name])
            ),
        ]

    def resolveCpp(cpp):
        return [cpp, os.path.join(outcppdir, cpp.name)]

    for ast, filename in [resolveHeader(hdr) for hdr in headers] + [
        resolveCpp(cpp) for cpp in cpps
    ]:
        tempfile = StringIO()
        CxxCodeGen(tempfile).cgen(ast)
        writeifmodified(tempfile.getvalue(), filename)


def genipdl(ast, outdir):
    return IPDLCodeGen().cgen(ast)


def genmsgenum(ast):
    return msgenums(ast.protocol, pretty=True)


def writeifmodified(contents, file):
    contents = contents.encode("utf-8")
    dir = os.path.dirname(file)
    os.path.exists(dir) or os.makedirs(dir)

    oldcontents = None
    if os.path.exists(file):
        fd = open(file, "rb")
        oldcontents = fd.read()
        fd.close()
    if oldcontents != contents:
        fd = open(file, "wb")
        fd.write(contents)
        fd.close()