DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (585fe4556335)

VCS Links

Line Code
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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358
# vim: set ts=4 sw=4 tw=99 et:
# 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/.

from __future__ import print_function

import os
import sys

from ipdl.ast import CxxInclude, Decl, Loc, QualifiedId, StructDecl
from ipdl.ast import TypeSpec, UnionDecl, UsingStmt, Visitor
from ipdl.ast import ASYNC, SYNC, INTR
from ipdl.ast import IN, OUT, INOUT
from ipdl.ast import NOT_NESTED, INSIDE_SYNC_NESTED, INSIDE_CPOW_NESTED
import ipdl.builtin as builtin

_DELETE_MSG = '__delete__'


class TypeVisitor:
    def __init__(self):
        self.visited = set()

    def defaultVisit(self, node, *args):
        raise Exception("INTERNAL ERROR: no visitor for node type `%s'" %
                        (node.__class__.__name__))

    def visitVoidType(self, v, *args):
        pass

    def visitImportedCxxType(self, t, *args):
        pass

    def visitMessageType(self, m, *args):
        for param in m.params:
            param.accept(self, *args)
        for ret in m.returns:
            ret.accept(self, *args)
        if m.cdtype is not None:
            m.cdtype.accept(self, *args)

    def visitProtocolType(self, p, *args):
        # NB: don't visit manager and manages. a naive default impl
        # could result in an infinite loop
        pass

    def visitActorType(self, a, *args):
        a.protocol.accept(self, *args)

    def visitStructType(self, s, *args):
        if s in self.visited:
            return

        self.visited.add(s)
        for field in s.fields:
            field.accept(self, *args)

    def visitUnionType(self, u, *args):
        if u in self.visited:
            return

        self.visited.add(u)
        for component in u.components:
            component.accept(self, *args)

    def visitArrayType(self, a, *args):
        a.basetype.accept(self, *args)

    def visitMaybeType(self, m, *args):
        m.basetype.accept(self, *args)

    def visitUniquePtrType(self, m, *args):
        m.basetype.accept(self, *args)

    def visitShmemType(self, s, *args):
        pass

    def visitByteBufType(self, s, *args):
        pass

    def visitShmemChmodType(self, c, *args):
        c.shmem.accept(self)

    def visitFDType(self, s, *args):
        pass

    def visitEndpointType(self, s, *args):
        pass

    def visitManagedEndpointType(self, s, *args):
        pass


class Type:
    def __cmp__(self, o):
        return cmp(self.fullname(), o.fullname())

    def __eq__(self, o):
        return (self.__class__ == o.__class__
                and self.fullname() == o.fullname())

    def __hash__(self):
        return hash(self.fullname())

    # Is this a C++ type?
    def isCxx(self):
        return False
    # Is this an IPDL type?

    def isIPDL(self):
        return False
    # Is this type neither compound nor an array?

    def isAtom(self):
        return False

    def isRefcounted(self):
        return False

    def isUniquePtr(self):
        return False

    def typename(self):
        return self.__class__.__name__

    def name(self):
        raise NotImplementedError

    def fullname(self):
        raise NotImplementedError

    def accept(self, visitor, *args):
        visit = getattr(visitor, 'visit' + self.__class__.__name__, None)
        if visit is None:
            return getattr(visitor, 'defaultVisit')(self, *args)
        return visit(self, *args)


class VoidType(Type):
    def isCxx(self):
        return True

    def isIPDL(self):
        return False

    def isAtom(self):
        return True

    def name(self): return 'void'

    def fullname(self): return 'void'


VOID = VoidType()

# --------------------


class ImportedCxxType(Type):
    def __init__(self, qname, refcounted, moveonly):
        assert isinstance(qname, QualifiedId)
        self.loc = qname.loc
        self.qname = qname
        self.refcounted = refcounted
        self.moveonly = moveonly

    def isCxx(self):
        return True

    def isAtom(self):
        return True

    def isRefcounted(self):
        return self.refcounted

    def isMoveonly(self):
        return self.moveonly

    def name(self):
        return self.qname.baseid

    def fullname(self):
        return str(self.qname)

# --------------------


class IPDLType(Type):
    def isIPDL(self): return True

    def isMessage(self): return False

    def isProtocol(self): return False

    def isActor(self): return False

    def isStruct(self): return False

    def isUnion(self): return False

    def isArray(self): return False

    def isMaybe(self): return False

    def isAtom(self): return True

    def isCompound(self): return False

    def isShmem(self): return False

    def isByteBuf(self): return False

    def isFD(self): return False

    def isEndpoint(self): return False

    def isManagedEndpoint(self): return False

    def isAsync(self): return self.sendSemantics == ASYNC

    def isSync(self): return self.sendSemantics == SYNC

    def isInterrupt(self): return self.sendSemantics is INTR

    def hasReply(self): return (self.isSync() or self.isInterrupt())

    def hasBaseType(self): return False

    @classmethod
    def convertsTo(cls, lesser, greater):
        if (lesser.nestedRange[0] < greater.nestedRange[0] or
                lesser.nestedRange[1] > greater.nestedRange[1]):
            return False

        # Protocols that use intr semantics are not allowed to use
        # message nesting.
        if (greater.isInterrupt() and
                lesser.nestedRange != (NOT_NESTED, NOT_NESTED)):
            return False

        if lesser.isAsync():
            return True
        elif lesser.isSync() and not greater.isAsync():
            return True
        elif greater.isInterrupt():
            return True

        return False

    def needsMoreJuiceThan(self, o):
        return not IPDLType.convertsTo(self, o)


class MessageType(IPDLType):
    def __init__(self, nested, prio, sendSemantics, direction,
                 ctor=False, dtor=False, cdtype=None, compress=False,
                 verify=False):
        assert not (ctor and dtor)
        assert not (ctor or dtor) or cdtype is not None

        self.nested = nested
        self.prio = prio
        self.nestedRange = (nested, nested)
        self.sendSemantics = sendSemantics
        self.direction = direction
        self.params = []
        self.returns = []
        self.ctor = ctor
        self.dtor = dtor
        self.cdtype = cdtype
        self.compress = compress
        self.verify = verify

    def isMessage(self): return True

    def isCtor(self): return self.ctor

    def isDtor(self): return self.dtor

    def constructedType(self): return self.cdtype

    def isIn(self): return self.direction is IN

    def isOut(self): return self.direction is OUT

    def isInout(self): return self.direction is INOUT

    def hasReply(self): return len(self.returns) or IPDLType.hasReply(self)

    def hasImplicitActorParam(self):
        return self.isCtor() or self.isDtor()


class ProtocolType(IPDLType):
    def __init__(self, qname, nested, sendSemantics, refcounted):
        self.qname = qname
        self.nestedRange = (NOT_NESTED, nested)
        self.sendSemantics = sendSemantics
        self.managers = []           # ProtocolType
        self.manages = []
        self.hasDelete = False
        self.refcounted = refcounted

    def isProtocol(self):
        return True

    def isRefcounted(self):
        return self.refcounted

    def name(self):
        return self.qname.baseid

    def fullname(self):
        return str(self.qname)

    def addManager(self, mgrtype):
        assert mgrtype.isIPDL() and mgrtype.isProtocol()
        self.managers.append(mgrtype)

    def managedBy(self, mgr):
        self.managers = list(mgr)

    def toplevel(self):
        if self.isToplevel():
            return self
        for mgr in self.managers:
            if mgr is not self:
                return mgr.toplevel()

    def toplevels(self):
        if self.isToplevel():
            return [self]
        toplevels = list()
        for mgr in self.managers:
            if mgr is not self:
                toplevels.extend(mgr.toplevels())
        return set(toplevels)

    def isManagerOf(self, pt):
        for managed in self.manages:
            if pt is managed:
                return True
        return False

    def isManagedBy(self, pt):
        return pt in self.managers

    def isManager(self):
        return len(self.manages) > 0

    def isManaged(self):
        return 0 < len(self.managers)

    def isToplevel(self):
        return not self.isManaged()

    def manager(self):
        assert 1 == len(self.managers)
        for mgr in self.managers:
            return mgr


class ActorType(IPDLType):
    def __init__(self, protocol, nullable=False):
        self.protocol = protocol
        self.nullable = nullable

    def isActor(self):
        return True

    def isRefcounted(self):
        return self.protocol.isRefcounted()

    def name(self):
        return self.protocol.name()

    def fullname(self):
        return self.protocol.fullname()


class _CompoundType(IPDLType):
    def __init__(self):
        self.defined = False            # bool
        self.mutualRec = set()          # set(_CompoundType | ArrayType)

    def isAtom(self):
        return False

    def isCompound(self):
        return True

    def itercomponents(self):
        raise Exception('"pure virtual" method')

    def mutuallyRecursiveWith(self, t, exploring=None):
        '''|self| is mutually recursive with |t| iff |self| and |t|
are in a cycle in the type graph rooted at |self|.  This function
looks for such a cycle and returns True if found.'''
        if exploring is None:
            exploring = set()

        if t.isAtom():
            return False
        elif t is self or t in self.mutualRec:
            return True
        elif t.hasBaseType():
            isrec = self.mutuallyRecursiveWith(t.basetype, exploring)
            if isrec:
                self.mutualRec.add(t)
            return isrec
        elif t in exploring:
            return False

        exploring.add(t)
        for c in t.itercomponents():
            if self.mutuallyRecursiveWith(c, exploring):
                self.mutualRec.add(c)
                return True
        exploring.remove(t)

        return False


class StructType(_CompoundType):
    def __init__(self, qname, fields):
        _CompoundType.__init__(self)
        self.qname = qname
        self.fields = fields            # [ Type ]

    def isStruct(self): return True

    def itercomponents(self):
        for f in self.fields:
            yield f

    def name(self): return self.qname.baseid

    def fullname(self): return str(self.qname)


class UnionType(_CompoundType):
    def __init__(self, qname, components):
        _CompoundType.__init__(self)
        self.qname = qname
        self.components = components    # [ Type ]

    def isUnion(self): return True

    def itercomponents(self):
        for c in self.components:
            yield c

    def name(self): return self.qname.baseid

    def fullname(self): return str(self.qname)


class ArrayType(IPDLType):
    def __init__(self, basetype):
        self.basetype = basetype

    def isAtom(self): return False

    def isArray(self): return True

    def hasBaseType(self): return True

    def name(self): return self.basetype.name() + '[]'

    def fullname(self): return self.basetype.fullname() + '[]'


class MaybeType(IPDLType):
    def __init__(self, basetype):
        self.basetype = basetype

    def isAtom(self): return False

    def isMaybe(self): return True

    def hasBaseType(self): return True

    def name(self): return self.basetype.name() + '?'

    def fullname(self): return self.basetype.fullname() + '?'


class ShmemType(IPDLType):
    def __init__(self, qname):
        self.qname = qname

    def isShmem(self): return True

    def name(self):
        return self.qname.baseid

    def fullname(self):
        return str(self.qname)


class ByteBufType(IPDLType):
    def __init__(self, qname):
        self.qname = qname

    def isByteBuf(self): return True

    def name(self):
        return self.qname.baseid

    def fullname(self):
        return str(self.qname)


class FDType(IPDLType):
    def __init__(self, qname):
        self.qname = qname

    def isFD(self): return True

    def name(self):
        return self.qname.baseid

    def fullname(self):
        return str(self.qname)


class EndpointType(IPDLType):
    def __init__(self, qname):
        self.qname = qname

    def isEndpoint(self): return True

    def name(self):
        return self.qname.baseid

    def fullname(self):
        return str(self.qname)


class ManagedEndpointType(IPDLType):
    def __init__(self, qname):
        self.qname = qname

    def isManagedEndpoint(self): return True

    def name(self):
        return self.qname.baseid

    def fullname(self):
        return str(self.qname)


class UniquePtrType(IPDLType):
    def __init__(self, basetype):
        self.basetype = basetype

    def isAtom(self): return False

    def isUniquePtr(self): return True

    def hasBaseType(self): return True

    def name(self):
        return 'UniquePtr<' + self.basetype.name() + '>'

    def fullname(self):
        return 'mozilla::UniquePtr<' + self.basetype.fullname() + '>'


def iteractortypes(t, visited=None):
    """Iterate over any actor(s) buried in |type|."""
    if visited is None:
        visited = set()

    # XXX |yield| semantics makes it hard to use TypeVisitor
    if not t.isIPDL():
        return
    elif t.isActor():
        yield t
    elif t.hasBaseType():
        for actor in iteractortypes(t.basetype, visited):
            yield actor
    elif t.isCompound() and t not in visited:
        visited.add(t)
        for c in t.itercomponents():
            for actor in iteractortypes(c, visited):
                yield actor


def hasshmem(type):
    """Return true iff |type| is shmem or has it buried within."""
    class found:
        pass

    class findShmem(TypeVisitor):
        def visitShmemType(self, s): raise found()
    try:
        type.accept(findShmem())
    except found:
        return True
    return False


# --------------------
_builtinloc = Loc('<builtin>', 0)


def makeBuiltinUsing(tname):
    quals = tname.split('::')
    base = quals.pop()
    quals = quals[0:]
    return UsingStmt(_builtinloc,
                     TypeSpec(_builtinloc,
                              QualifiedId(_builtinloc, base, quals)))


builtinUsing = [makeBuiltinUsing(t) for t in builtin.Types]
builtinHeaderIncludes = [CxxInclude(_builtinloc, f) for f in builtin.HeaderIncludes]


def errormsg(loc, fmt, *args):
    while not isinstance(loc, Loc):
        if loc is None:
            loc = Loc.NONE
        else:
            loc = loc.loc
    return '%s: error: %s' % (str(loc), fmt % args)

# --------------------


class SymbolTable:
    def __init__(self, errors):
        self.errors = errors
        self.scopes = [{}]   # stack({})
        self.currentScope = self.scopes[0]

    def enterScope(self):
        assert isinstance(self.scopes[0], dict)
        assert isinstance(self.currentScope, dict)

        self.scopes.append({})
        self.currentScope = self.scopes[-1]

    def exitScope(self):
        symtab = self.scopes.pop()
        assert self.currentScope is symtab

        self.currentScope = self.scopes[-1]

        assert isinstance(self.scopes[0], dict)
        assert isinstance(self.currentScope, dict)

    def lookup(self, sym):
        # NB: since IPDL doesn't allow any aliased names of different types,
        # it doesn't matter in which order we walk the scope chain to resolve
        # |sym|
        for scope in self.scopes:
            decl = scope.get(sym, None)
            if decl is not None:
                return decl
        return None

    def declare(self, decl):
        assert decl.progname or decl.shortname or decl.fullname
        assert decl.loc
        assert decl.type

        def tryadd(name):
            olddecl = self.lookup(name)
            if olddecl is not None:
                self.errors.append(errormsg(
                    decl.loc,
                    "redeclaration of symbol `%s', first declared at %s",
                    name, olddecl.loc))
                return
            self.currentScope[name] = decl
            decl.scope = self.currentScope

        if decl.progname:
            tryadd(decl.progname)
        if decl.shortname:
            tryadd(decl.shortname)
        if decl.fullname:
            tryadd(decl.fullname)


class TypeCheck:
    '''This pass sets the .decl attribute of AST nodes for which that is relevant;
a decl says where, with what type, and under what name(s) a node was
declared.

With this information, it type checks the AST.'''

    def __init__(self):
        # NB: no IPDL compile will EVER print a warning.  A program has
        # one of two attributes: it is either well typed, or not well typed.
        self.errors = []       # [ string ]

    def check(self, tu, errout=sys.stderr):
        def runpass(tcheckpass):
            tu.accept(tcheckpass)
            if len(self.errors):
                self.reportErrors(errout)
                return False
            return True

        # tag each relevant node with "decl" information, giving type, name,
        # and location of declaration
        if not runpass(GatherDecls(builtinUsing, self.errors)):
            return False

        # now that the nodes have decls, type checking is much easier.
        if not runpass(CheckTypes(self.errors)):
            return False

        return True

    def reportErrors(self, errout):
        for error in self.errors:
            print(error, file=errout)


class TcheckVisitor(Visitor):
    def __init__(self, errors):
        self.errors = errors

    def error(self, loc, fmt, *args):
        self.errors.append(errormsg(loc, fmt, *args))


class GatherDecls(TcheckVisitor):
    def __init__(self, builtinUsing, errors):
        TcheckVisitor.__init__(self, errors)

        # |self.symtab| is the symbol table for the translation unit
        # currently being visited
        self.symtab = None
        self.builtinUsing = builtinUsing

    def declare(self, loc, type, shortname=None, fullname=None, progname=None):
        d = Decl(loc)
        d.type = type
        d.progname = progname
        d.shortname = shortname
        d.fullname = fullname
        self.symtab.declare(d)
        return d

    def visitTranslationUnit(self, tu):
        # all TranslationUnits declare symbols in global scope
        if hasattr(tu, 'visited'):
            return
        tu.visited = True
        savedSymtab = self.symtab
        self.symtab = SymbolTable(self.errors)

        # pretend like the translation unit "using"-ed these for the
        # sake of type checking and C++ code generation
        tu.builtinUsing = self.builtinUsing

        # for everyone's sanity, enforce that the filename and tu name
        # match
        basefilename = os.path.basename(tu.filename)
        expectedfilename = '%s.ipdl' % (tu.name)
        if not tu.protocol:
            # header
            expectedfilename += 'h'
        if basefilename != expectedfilename:
            self.error(tu.loc,
                       "expected file for translation unit `%s' to be named `%s'; instead it's named `%s'",  # NOQA: E501
                       tu.name, expectedfilename, basefilename)

        if tu.protocol:
            assert tu.name == tu.protocol.name

            p = tu.protocol

            # FIXME/cjones: it's a little weird and counterintuitive
            # to put both the namespace and non-namespaced name in the
            # global scope.  try to figure out something better; maybe
            # a type-neutral |using| that works for C++ and protocol
            # types?
            qname = p.qname()
            fullname = str(qname)
            p.decl = self.declare(
                loc=p.loc,
                type=ProtocolType(qname, p.nested, p.sendSemantics, p.refcounted),
                shortname=p.name,
                fullname=None if 0 == len(qname.quals) else fullname)

            p.parentEndpointDecl = self.declare(
                loc=p.loc,
                type=EndpointType(QualifiedId(p.loc, 'Endpoint<' +
                                              fullname + 'Parent>', ['mozilla', 'ipc'])),
                shortname='Endpoint<' + p.name + 'Parent>')
            p.childEndpointDecl = self.declare(
                loc=p.loc,
                type=EndpointType(QualifiedId(p.loc, 'Endpoint<' +
                                              fullname + 'Child>', ['mozilla', 'ipc'])),
                shortname='Endpoint<' + p.name + 'Child>')

            p.parentManagedEndpointDecl = self.declare(
                loc=p.loc,
                type=ManagedEndpointType(QualifiedId(p.loc, 'ManagedEndpoint<' +
                                                     fullname + 'Parent>',
                                                     ['mozilla', 'ipc'])),
                shortname='ManagedEndpoint<' + p.name + 'Parent>')
            p.childManagedEndpointDecl = self.declare(
                loc=p.loc,
                type=ManagedEndpointType(QualifiedId(p.loc, 'ManagedEndpoint<' +
                                                     fullname + 'Child>',
                                                     ['mozilla', 'ipc'])),
                shortname='ManagedEndpoint<' + p.name + 'Child>')

            # XXX ugh, this sucks.  but we need this information to compute
            # what friend decls we need in generated C++
            p.decl.type._ast = p

        # make sure we have decls for all dependent protocols
        for pinc in tu.includes:
            pinc.accept(self)

        # declare imported (and builtin) C++ types
        for using in tu.builtinUsing:
            using.accept(self)
        for using in tu.using:
            using.accept(self)

        # first pass to "forward-declare" all structs and unions in
        # order to support recursive definitions
        for su in tu.structsAndUnions:
            self.declareStructOrUnion(su)

        # second pass to check each definition
        for su in tu.structsAndUnions:
            su.accept(self)

        if tu.protocol:
            # grab symbols in the protocol itself
            p.accept(self)

        self.symtab = savedSymtab

    def declareStructOrUnion(self, su):
        if hasattr(su, 'decl'):
            self.symtab.declare(su.decl)
            return

        qname = su.qname()
        if 0 == len(qname.quals):
            fullname = None
        else:
            fullname = str(qname)

        if isinstance(su, StructDecl):
            sutype = StructType(qname, [])
        elif isinstance(su, UnionDecl):
            sutype = UnionType(qname, [])
        else:
            assert 0 and 'unknown type'

        # XXX more suckage.  this time for pickling structs/unions
        # declared in headers.
        sutype._ast = su

        su.decl = self.declare(
            loc=su.loc,
            type=sutype,
            shortname=su.name,
            fullname=fullname)

    def visitInclude(self, inc):
        if inc.tu is None:
            self.error(
                inc.loc,
                "(type checking here will be unreliable because of an earlier error)")
            return
        inc.tu.accept(self)
        if inc.tu.protocol:
            self.symtab.declare(inc.tu.protocol.decl)
            self.symtab.declare(inc.tu.protocol.parentEndpointDecl)
            self.symtab.declare(inc.tu.protocol.childEndpointDecl)
            self.symtab.declare(inc.tu.protocol.parentManagedEndpointDecl)
            self.symtab.declare(inc.tu.protocol.childManagedEndpointDecl)
        else:
            # This is a header.  Import its "exported" globals into
            # our scope.
            for using in inc.tu.using:
                using.accept(self)
            for su in inc.tu.structsAndUnions:
                self.declareStructOrUnion(su)

    def visitStructDecl(self, sd):
        # If we've already processed this struct, don't do it again.
        if hasattr(sd, 'visited'):
            return

        stype = sd.decl.type

        self.symtab.enterScope()
        sd.visited = True

        for f in sd.fields:
            ftypedecl = self.symtab.lookup(str(f.typespec))
            if ftypedecl is None:
                self.error(f.loc, "field `%s' of struct `%s' has unknown type `%s'",
                           f.name, sd.name, str(f.typespec))
                continue

            f.decl = self.declare(
                loc=f.loc,
                type=self._canonicalType(ftypedecl.type, f.typespec),
                shortname=f.name,
                fullname=None)
            stype.fields.append(f.decl.type)

        self.symtab.exitScope()

    def visitUnionDecl(self, ud):
        utype = ud.decl.type

        # If we've already processed this union, don't do it again.
        if len(utype.components):
            return

        for c in ud.components:
            cdecl = self.symtab.lookup(str(c))
            if cdecl is None:
                self.error(c.loc, "unknown component type `%s' of union `%s'",
                           str(c), ud.name)
                continue
            utype.components.append(self._canonicalType(cdecl.type, c))

    def visitUsingStmt(self, using):
        fullname = str(using.type)
        if (using.type.basename() == fullname) or using.type.uniqueptr:
            # Prevent generation of typedefs.  If basename == fullname then
            # there is nothing to typedef.  With UniquePtrs, basenames
            # are generic so typedefs would be illegal.
            fullname = None
        if fullname == 'mozilla::ipc::Shmem':
            ipdltype = ShmemType(using.type.spec)
        elif fullname == 'mozilla::ipc::ByteBuf':
            ipdltype = ByteBufType(using.type.spec)
        elif fullname == 'mozilla::ipc::FileDescriptor':
            ipdltype = FDType(using.type.spec)
        else:
            ipdltype = ImportedCxxType(using.type.spec, using.isRefcounted(), using.isMoveonly())
            existingType = self.symtab.lookup(ipdltype.fullname())
            if existingType and existingType.fullname == ipdltype.fullname():
                if ipdltype.isRefcounted() != existingType.type.isRefcounted():
                    self.error(using.loc, "inconsistent refcounted status of type `%s`",
                               str(using.type))
                if ipdltype.isMoveonly() != existingType.type.isMoveonly():
                    self.error(using.loc, "inconsistent moveonly status of type `%s`",
                               str(using.type))
                using.decl = existingType
                return
        using.decl = self.declare(
            loc=using.loc,
            type=ipdltype,
            shortname=using.type.basename(),
            fullname=fullname)

    def visitProtocol(self, p):
        # protocol scope
        self.symtab.enterScope()

        seenmgrs = set()
        for mgr in p.managers:
            if mgr.name in seenmgrs:
                self.error(mgr.loc, "manager `%s' appears multiple times",
                           mgr.name)
                continue

            seenmgrs.add(mgr.name)
            mgr.of = p
            mgr.accept(self)

        for managed in p.managesStmts:
            managed.manager = p
            managed.accept(self)

        if not (p.managers or p.messageDecls or p.managesStmts):
            self.error(p.loc,
                       "top-level protocol `%s' cannot be empty",
                       p.name)

        setattr(self, 'currentProtocolDecl', p.decl)
        for msg in p.messageDecls:
            msg.accept(self)
        del self.currentProtocolDecl

        p.decl.type.hasDelete = (not not self.symtab.lookup(_DELETE_MSG))
        if not (p.decl.type.hasDelete or p.decl.type.isToplevel()):
            self.error(
                p.loc,
                "destructor declaration `%s(...)' required for managed protocol `%s'",
                _DELETE_MSG, p.name)

        # FIXME/cjones declare all the little C++ thingies that will
        # be generated.  they're not relevant to IPDL itself, but
        # those ("invisible") symbols can clash with others in the
        # IPDL spec, and we'd like to catch those before C++ compilers
        # are allowed to obfuscate the error

        self.symtab.exitScope()

    def visitManager(self, mgr):
        mgrdecl = self.symtab.lookup(mgr.name)
        pdecl = mgr.of.decl
        assert pdecl

        pname, mgrname = pdecl.shortname, mgr.name
        loc = mgr.loc

        if mgrdecl is None:
            self.error(
                loc,
                "protocol `%s' referenced as |manager| of `%s' has not been declared",
                mgrname, pname)
        elif not isinstance(mgrdecl.type, ProtocolType):
            self.error(
                loc,
                "entity `%s' referenced as |manager| of `%s' is not of `protocol' type; instead it is of type `%s'",  # NOQA: E501
                mgrname, pname, mgrdecl.type.typename())
        else:
            mgr.decl = mgrdecl
            pdecl.type.addManager(mgrdecl.type)

    def visitManagesStmt(self, mgs):
        mgsdecl = self.symtab.lookup(mgs.name)
        pdecl = mgs.manager.decl
        assert pdecl

        pname, mgsname = pdecl.shortname, mgs.name
        loc = mgs.loc

        if mgsdecl is None:
            self.error(loc,
                       "protocol `%s', managed by `%s', has not been declared",
                       mgsname, pname)
        elif not isinstance(mgsdecl.type, ProtocolType):
            self.error(
                loc,
                "%s declares itself managing a non-`protocol' entity `%s' of type `%s'",
                pname, mgsname, mgsdecl.type.typename())
        else:
            mgs.decl = mgsdecl
            pdecl.type.manages.append(mgsdecl.type)

    def visitMessageDecl(self, md):
        msgname = md.name
        loc = md.loc

        isctor = False
        isdtor = False
        cdtype = None

        decl = self.symtab.lookup(msgname)
        if decl is not None and decl.type.isProtocol():
            # probably a ctor.  we'll check validity later.
            msgname += 'Constructor'
            isctor = True
            cdtype = decl.type
        elif decl is not None:
            self.error(loc, "message name `%s' already declared as `%s'",
                       msgname, decl.type.typename())
            # if we error here, no big deal; move on to find more

        if _DELETE_MSG == msgname:
            isdtor = True
            cdtype = self.currentProtocolDecl.type

        # enter message scope
        self.symtab.enterScope()

        msgtype = MessageType(md.nested, md.prio, md.sendSemantics, md.direction,
                              ctor=isctor, dtor=isdtor, cdtype=cdtype,
                              compress=md.compress, verify=md.verify)

        # replace inparam Param nodes with proper Decls
        def paramToDecl(param):
            ptname = param.typespec.basename()
            ploc = param.typespec.loc

            ptdecl = self.symtab.lookup(ptname)
            if ptdecl is None:
                self.error(
                    ploc,
                    "argument typename `%s' of message `%s' has not been declared",
                    ptname, msgname)
                ptype = VOID
            else:
                ptype = self._canonicalType(ptdecl.type, param.typespec)
            return self.declare(loc=ploc,
                                type=ptype,
                                progname=param.name)

        for i, inparam in enumerate(md.inParams):
            pdecl = paramToDecl(inparam)
            msgtype.params.append(pdecl.type)
            md.inParams[i] = pdecl
        for i, outparam in enumerate(md.outParams):
            pdecl = paramToDecl(outparam)
            msgtype.returns.append(pdecl.type)
            md.outParams[i] = pdecl

        self.symtab.exitScope()

        md.decl = self.declare(
            loc=loc,
            type=msgtype,
            progname=msgname)
        md.protocolDecl = self.currentProtocolDecl
        md.decl._md = md

    def _canonicalType(self, itype, typespec):
        loc = typespec.loc
        if itype.isIPDL():
            if itype.isProtocol():
                itype = ActorType(itype,
                                  nullable=typespec.nullable)

        if typespec.nullable and not (itype.isIPDL() and itype.isActor()):
            self.error(
                loc,
                "`nullable' qualifier for type `%s' makes no sense",
                itype.name())

        if typespec.array:
            itype = ArrayType(itype)

        if typespec.maybe:
            itype = MaybeType(itype)

        if typespec.uniqueptr:
            itype = UniquePtrType(itype)

        return itype


# -----------------------------------------------------------------------------

def checkcycles(p, stack=None):
    cycles = []

    if stack is None:
        stack = []

    for cp in p.manages:
        # special case for self-managed protocols
        if cp is p:
            continue

        if cp in stack:
            return [stack + [p, cp]]
        cycles += checkcycles(cp, stack + [p])

    return cycles


def formatcycles(cycles):
    r = []
    for cycle in cycles:
        s = " -> ".join([ptype.name() for ptype in cycle])
        r.append("`%s'" % s)
    return ", ".join(r)


def fullyDefined(t, exploring=None):
    '''The rules for "full definition" of a type are
  defined(atom)             := true
  defined(array basetype)   := defined(basetype)
  defined(struct f1 f2...)  := defined(f1) and defined(f2) and ...
  defined(union c1 c2 ...)  := defined(c1) or defined(c2) or ...
'''
    if exploring is None:
        exploring = set()

    if t.isAtom():
        return True
    elif t.hasBaseType():
        return fullyDefined(t.basetype, exploring)
    elif t.defined:
        return True
    assert t.isCompound()

    if t in exploring:
        return False

    exploring.add(t)
    for c in t.itercomponents():
        cdefined = fullyDefined(c, exploring)
        if t.isStruct() and not cdefined:
            t.defined = False
            break
        elif t.isUnion() and cdefined:
            t.defined = True
            break
    else:
        if t.isStruct():
            t.defined = True
        elif t.isUnion():
            t.defined = False
    exploring.remove(t)

    return t.defined


class CheckTypes(TcheckVisitor):
    def __init__(self, errors):
        TcheckVisitor.__init__(self, errors)
        self.visited = set()
        self.ptype = None

    def visitInclude(self, inc):
        if inc.tu.filename in self.visited:
            return
        self.visited.add(inc.tu.filename)
        if inc.tu.protocol:
            inc.tu.protocol.accept(self)

    def visitStructDecl(self, sd):
        if not fullyDefined(sd.decl.type):
            self.error(sd.decl.loc,
                       "struct `%s' is only partially defined", sd.name)

    def visitUnionDecl(self, ud):
        if not fullyDefined(ud.decl.type):
            self.error(ud.decl.loc,
                       "union `%s' is only partially defined", ud.name)

    def visitProtocol(self, p):
        self.ptype = p.decl.type

        # check that we require no more "power" than our manager protocols
        ptype, pname = p.decl.type, p.decl.shortname

        for mgrtype in ptype.managers:
            if mgrtype is not None and ptype.needsMoreJuiceThan(mgrtype):
                self.error(
                    p.decl.loc,
                    "protocol `%s' requires more powerful send semantics than its manager `%s' provides",  # NOQA: E501
                    pname, mgrtype.name())

        if ptype.isToplevel():
            cycles = checkcycles(p.decl.type)
            if cycles:
                self.error(
                    p.decl.loc,
                    "cycle(s) detected in manager/manages hierarchy: %s",
                    formatcycles(cycles))

        if 1 == len(ptype.managers) and ptype is ptype.manager():
            self.error(
                p.decl.loc,
                "top-level protocol `%s' cannot manage itself",
                p.name)

        return Visitor.visitProtocol(self, p)

    def visitManagesStmt(self, mgs):
        pdecl = mgs.manager.decl
        ptype, pname = pdecl.type, pdecl.shortname

        mgsdecl = mgs.decl
        mgstype, mgsname = mgsdecl.type, mgsdecl.shortname

        loc = mgs.loc

        # we added this information; sanity check it
        assert ptype.isManagerOf(mgstype)

        # check that the "managed" protocol agrees
        if not mgstype.isManagedBy(ptype):
            self.error(
                loc,
                "|manages| declaration in protocol `%s' does not match any |manager| declaration in protocol `%s'",  # NOQA: E501
                pname, mgsname)

    def visitManager(self, mgr):
        pdecl = mgr.of.decl
        ptype, pname = pdecl.type, pdecl.shortname

        mgrdecl = mgr.decl
        mgrtype, mgrname = mgrdecl.type, mgrdecl.shortname

        # we added this information; sanity check it
        assert ptype.isManagedBy(mgrtype)

        loc = mgr.loc

        # check that the "manager" protocol agrees
        if not mgrtype.isManagerOf(ptype):
            self.error(
                loc,
                "|manager| declaration in protocol `%s' does not match any |manages| declaration in protocol `%s'",  # NOQA: E501
                pname, mgrname)

    def visitMessageDecl(self, md):
        mtype, mname = md.decl.type, md.decl.progname
        ptype, pname = md.protocolDecl.type, md.protocolDecl.shortname

        loc = md.decl.loc

        if mtype.nested == INSIDE_SYNC_NESTED and not mtype.isSync():
            self.error(
                loc,
                "inside_sync nested messages must be sync (here, message `%s' in protocol `%s')",
                mname, pname)

        if mtype.nested == INSIDE_CPOW_NESTED and (mtype.isOut() or mtype.isInout()):
            self.error(
                loc,
                "inside_cpow nested parent-to-child messages are verboten (here, message `%s' in protocol `%s')",  # NOQA: E501
                mname, pname)

        # We allow inside_sync messages that are themselves sync to be sent from the
        # parent. Normal and inside_cpow nested messages that are sync can only come from
        # the child.
        if mtype.isSync() and mtype.nested == NOT_NESTED and (mtype.isOut() or mtype.isInout()):
            self.error(
                loc,
                "sync parent-to-child messages are verboten (here, message `%s' in protocol `%s')",
                mname, pname)

        if mtype.needsMoreJuiceThan(ptype):
            self.error(
                loc,
                "message `%s' requires more powerful send semantics than its protocol `%s' provides",  # NOQA: E501
                mname, pname)

        if (mtype.isCtor() or mtype.isDtor()) and mtype.isAsync() and mtype.returns:
            self.error(loc,
                       "asynchronous ctor/dtor message `%s' declares return values",
                       mname)

        if (mtype.compress and
                (not mtype.isAsync() or mtype.isCtor() or mtype.isDtor())):

            if mtype.isCtor() or mtype.isDtor():
                message_type = "constructor" if mtype.isCtor() else "destructor"
                error_message = ("%s messages can't use compression (here, in protocol `%s')" %
                                 (message_type, pname))
            else:
                error_message = ("message `%s' in protocol `%s' requests compression but is not async" %  # NOQA: E501
                                 (mname, pname))

            self.error(loc, error_message)

        if mtype.isCtor() and not ptype.isManagerOf(mtype.constructedType()):
            self.error(
                loc,
                "ctor for protocol `%s', which is not managed by protocol `%s'",
                mname[:-len('constructor')], pname)