Bug 540004, part 1: Various and sundry trivial refactorings. r=bent
authorChris Jones <jones.chris.g@gmail.com>
Tue, 09 Feb 2010 18:02:53 -0600
changeset 46646 d0e79b58e9b82ea1d6ccc934b6010f39b2f0ccac
parent 46645 fe8bbab16f01319acd352bfccc5ba09e599fc40f
child 46647 b85532205c1b0685f423dacc77c7cbd1f29d161a
push id1
push userroot
push dateMon, 20 Oct 2014 17:29:22 +0000
reviewersbent
bugs540004
milestone1.9.3a2pre
Bug 540004, part 1: Various and sundry trivial refactorings. r=bent
ipc/ipdl/ipdl/lower.py
--- a/ipc/ipdl/ipdl/lower.py
+++ b/ipc/ipdl/ipdl/lower.py
@@ -2531,37 +2531,39 @@ class _GenerateProtocolActorCode(ipdl.as
     def visitProtocol(self, p):
         self.hdrfile.addthings([
             CppDirective('ifdef', 'DEBUG'),
             CppDirective('include', '"prenv.h"'),
             CppDirective('endif', '// DEBUG')
         ])
 
         self.protocol = p
+        ptype = p.decl.type
+        toplevel = p.decl.type.toplevel()
 
         # FIXME: all actors impl Iface for now
-        if p.decl.type.isManager() or 1:
+        if ptype.isManager() or 1:
             self.hdrfile.addthing(CppDirective('include', '"base/id_map.h"'))
 
         self.hdrfile.addthings([
             CppDirective('include', '"'+ p.channelHeaderFile() +'"'),
             Whitespace.NL ])
 
         self.cls = Class(
             self.clsname,
             inherits=[ Inherit(Type(p.fqListenerName()), viz='protected'),
                        Inherit(p.managerInterfaceType(), viz='protected') ],
             abstract=True)
 
-        friends = _FindFriends().findFriends(p.decl.type)
-        if p.decl.type.isManaged():
-            friends.update(p.decl.type.managers)
+        friends = _FindFriends().findFriends(ptype)
+        if ptype.isManaged():
+            friends.update(ptype.managers)
 
         # |friend| managed actors so that they can call our Dealloc*()
-        friends.update(p.decl.type.manages)
+        friends.update(ptype.manages)
 
         for friend in friends:
             self.hdrfile.addthings([
                 Whitespace.NL,
                 _makeForwardDecl(friend, self.prettyside),
                 Whitespace.NL
             ])
             self.cls.addstmts([
@@ -2594,17 +2596,17 @@ class _GenerateProtocolActorCode(ipdl.as
                     defaultRecv.addstmt(StmtReturn(ExprLiteral.TRUE))
                     self.cls.addstmt(defaultRecv)
                 else:
                     recvDecl.pure = 1
                     self.cls.addstmt(StmtDecl(recvDecl))
 
         for md in p.messageDecls:
             managed = md.decl.type.constructedType()
-            if not p.decl.type.isManagerOf(managed):
+            if not ptype.isManagerOf(managed):
                 continue
 
             # add the Alloc/Dealloc interface for managed actors
             actortype = md.actorDecl().bareType(self.side)
             
             self.cls.addstmt(StmtDecl(MethodDecl(
                 _allocMethod(managed).name,
                 params=md.makeCxxParams(side=self.side, implicit=0),
@@ -2631,17 +2633,17 @@ class _GenerateProtocolActorCode(ipdl.as
             [ Label.PRIVATE ]
             + self.standardTypedefs()
             + [ Whitespace.NL ]
         ))
 
         self.cls.addstmt(Label.PUBLIC)
         # Actor()
         ctor = ConstructorDefn(ConstructorDecl(self.clsname))
-        if p.decl.type.isToplevel():
+        if ptype.isToplevel():
             ctor.memberinits = [
                 ExprMemberInit(p.channelVar(), [
                     ExprCall(ExprVar('ALLOW_THIS_IN_INITIALIZER_LIST'),
                              [ ExprVar.THIS ]) ]),
                 ExprMemberInit(p.lastActorIdVar(),
                                [ p.actorIdInit(self.side) ])
             ]
         else:
@@ -2660,17 +2662,17 @@ class _GenerateProtocolActorCode(ipdl.as
         # ~Actor()
         dtor = DestructorDefn(
             DestructorDecl(self.clsname, virtual=True))
         dtor.addstmt(StmtExpr(ExprCall(ExprVar('MOZ_COUNT_DTOR'),
                                                [ ExprVar(self.clsname) ])))
 
         self.cls.addstmts([ dtor, Whitespace.NL ])
 
-        if p.decl.type.isToplevel():
+        if ptype.isToplevel():
             # Open()
             aTransportVar = ExprVar('aTransport')
             aThreadVar = ExprVar('aThread')
             processvar = ExprVar('aOtherProcess')
             openmeth = MethodDefn(
                 MethodDecl(
                     'Open',
                     params=[ Decl(Type('Channel::Transport', ptr=True),
@@ -2691,29 +2693,29 @@ class _GenerateProtocolActorCode(ipdl.as
                 Whitespace.NL ])
 
             # Close()
             closemeth = MethodDefn(MethodDecl('Close'))
             closemeth.addstmt(StmtExpr(
                 ExprCall(ExprSelect(p.channelVar(), '.', 'Close'))))
             self.cls.addstmts([ closemeth, Whitespace.NL ])
 
-        if not p.decl.type.isToplevel():
+        if not ptype.isToplevel():
             if 1 == len(p.managers):
                 ## manager()
                 managertype = p.managerActorType(self.side, ptr=1)
                 managermeth = MethodDefn(MethodDecl(
                     p.managerMethod().name, ret=managertype))
                 managermeth.addstmt(StmtReturn(
                     ExprCast(p.managerVar(), managertype, static=1)))
 
                 self.cls.addstmts([ managermeth, Whitespace.NL ])
 
         ## managed[T]()
-        for managed in p.decl.type.manages:
+        for managed in ptype.manages:
             arrvar = ExprVar('aArr')
             meth = MethodDefn(MethodDecl(
                 p.managedMethod(managed, self.side).name,
                 params=[ Decl(p.managedVarType(managed, self.side, ref=1),
                               arrvar.name) ],
                 const=1))
             meth.addstmt(StmtExpr(ExprAssn(
                 arrvar, p.managedVar(managed, self.side))))
@@ -2724,19 +2726,19 @@ class _GenerateProtocolActorCode(ipdl.as
         # save these away for use in message handler case stmts
         msgvar = ExprVar('msg')
         self.msgvar = msgvar
         replyvar = ExprVar('reply')
         self.replyvar = replyvar
         
         msgtype = ExprCall(ExprSelect(msgvar, '.', 'type'), [ ])
         self.asyncSwitch = StmtSwitch(msgtype)
-        if p.decl.type.toplevel().talksSync():
+        if toplevel.talksSync():
             self.syncSwitch = StmtSwitch(msgtype)
-            if p.decl.type.toplevel().talksRpc():
+            if toplevel.talksRpc():
                 self.rpcSwitch = StmtSwitch(msgtype)
 
         # implement Send*() methods and add dispatcher cases to
         # message switch()es
         for md in p.messageDecls:
             self.visitMessageDecl(md)
 
         # "hidden" message that passes shmem mappings from one process
@@ -2745,19 +2747,19 @@ class _GenerateProtocolActorCode(ipdl.as
             self.asyncSwitch.addcase(
                 CaseLabel('SHMEM_CREATED_MESSAGE_TYPE'),
                 self.genShmemCreatedHandler())
 
         # add default cases
         default = StmtBlock()
         default.addstmt(StmtReturn(_Result.NotKnown))
         self.asyncSwitch.addcase(DefaultLabel(), default)
-        if p.decl.type.toplevel().talksSync():
+        if toplevel.talksSync():
             self.syncSwitch.addcase(DefaultLabel(), default)
-            if p.decl.type.toplevel().talksRpc():
+            if toplevel.talksRpc():
                 self.rpcSwitch.addcase(DefaultLabel(), default)
 
 
         def makeHandlerMethod(name, switch, hasReply, dispatches=0):
             params = [ Decl(Type('Message', const=1, ref=1), msgvar.name) ]
             if hasReply:
                 params.append(Decl(Type('Message', ref=1, ptr=1),
                                    replyvar.name))
@@ -2791,66 +2793,74 @@ class _GenerateProtocolActorCode(ipdl.as
             # is only the default case; MSVC doesn't like that
             if switch.nr_cases > 1:
                 method.addstmt(switch)
             else:
                 method.addstmt(StmtReturn(_Result.NotKnown))
 
             return method
 
-        dispatches = (p.decl.type.isToplevel() and p.decl.type.isManager())
+        dispatches = (ptype.isToplevel() and ptype.isManager())
         self.cls.addstmts([
             makeHandlerMethod('OnMessageReceived', self.asyncSwitch,
                               hasReply=0, dispatches=dispatches),
             Whitespace.NL
         ])
-        if p.decl.type.toplevel().talksSync():
+        if toplevel.talksSync():
             self.cls.addstmts([
                 makeHandlerMethod('OnMessageReceived', self.syncSwitch,
                                   hasReply=1, dispatches=dispatches),
                 Whitespace.NL
             ])
-            if p.decl.type.toplevel().talksRpc():
+            if toplevel.talksRpc():
                 self.cls.addstmts([
                     makeHandlerMethod('OnCallReceived', self.rpcSwitch,
                                       hasReply=1, dispatches=dispatches),
                     Whitespace.NL
                 ])
 
         destroysubtreevar = ExprVar('DestroySubtree')
         deallocsubtreevar = ExprVar('DeallocSubtree')
 
         # OnChannelClose()
         onclose = MethodDefn(MethodDecl('OnChannelClose'))
-        onclose.addstmts([
-            StmtExpr(ExprCall(destroysubtreevar,
-                              args=[ _DestroyReason.NormalShutdown ])),
-            StmtExpr(ExprCall(deallocsubtreevar))
-        ])
+        if ptype.isToplevel():
+            onclose.addstmts([
+                StmtExpr(ExprCall(destroysubtreevar,
+                                  args=[ _DestroyReason.NormalShutdown ])),
+                StmtExpr(ExprCall(deallocsubtreevar))
+            ])
+        else:
+            onclose.addstmt(
+                _runtimeAbort("`OnClose' called on non-toplevel actor"))
         self.cls.addstmts([ onclose, Whitespace.NL ])
 
-        # OnChannelClose()
+        # OnChannelError()
         onerror = MethodDefn(MethodDecl('OnChannelError'))
-        onerror.addstmts([
-            StmtExpr(ExprCall(destroysubtreevar,
-                              args=[ _DestroyReason.AbnormalShutdown ])),
-            StmtExpr(ExprCall(deallocsubtreevar))
-        ])
+        if ptype.isToplevel():
+            onerror.addstmts([
+                StmtExpr(ExprCall(destroysubtreevar,
+                                  args=[ _DestroyReason.AbnormalShutdown ])),
+                StmtExpr(ExprCall(deallocsubtreevar))
+            ])
+        else:
+            onerror.addstmt(
+                _runtimeAbort("`OnError' called on non-toplevel actor"))
         self.cls.addstmts([ onerror, Whitespace.NL ])
 
         # FIXME/bug 535053: only manager protocols and non-manager
         # protocols with union types need Lookup().  we'll give it to
         # all for the time being (simpler)
-        if 1 or p.decl.type.isManager():
+        if 1 or ptype.isManager():
             self.cls.addstmts(self.implementManagerIface())
 
         if p.usesShmem():
             self.cls.addstmts(self.makeShmemIface())
 
-        if p.decl.type.isToplevel() and self.side is 'parent':
+        if ptype.isToplevel() and self.side is 'parent':
             ## bool GetMinidump(nsIFile** dump)
             self.cls.addstmt(Label.PROTECTED)
 
             otherpidvar = ExprVar('OtherSidePID')
             otherpid = MethodDefn(MethodDecl(
                 otherpidvar.name, params=[ ],
                 ret=Type('base::ProcessId'),
                 const=1))
@@ -2873,18 +2883,18 @@ class _GenerateProtocolActorCode(ipdl.as
                     args=[ ExprCall(otherpidvar), dumpvar ])),
                 CppDirective('else'),
                 StmtReturn(ExprLiteral.FALSE),
                 CppDirective('endif')
             ])
             self.cls.addstmts([ otherpid, Whitespace.NL,
                                 getdump, Whitespace.NL ])
 
-        if (p.decl.type.isToplevel() and self.side is 'parent'
-            and p.decl.type.talksRpc()):
+        if (ptype.isToplevel() and self.side is 'parent'
+            and ptype.talksRpc()):
             # offer BlockChild() and UnblockChild().
             # See ipc/glue/RPCChannel.h
             blockchild = MethodDefn(MethodDecl(
                 'BlockChild', ret=Type.BOOL))
             blockchild.addstmt(StmtReturn(ExprCall(
                 ExprSelect(p.channelVar(), '.', 'BlockChild'))))
 
             unblockchild = MethodDefn(MethodDecl(
@@ -2936,28 +2946,28 @@ class _GenerateProtocolActorCode(ipdl.as
         kidsvar = ExprVar('kids')
         ivar = ExprVar('i')
         ithkid = ExprIndex(kidsvar, ivar)
 
         destroysubtree = MethodDefn(MethodDecl(
             destroysubtreevar.name,
             params=[ Decl(_DestroyReason.Type(), whyvar.name) ]))
 
-        if p.decl.type.isManager():
+        if ptype.isManager():
             # only declare this for managers to avoid unused var warnings
             destroysubtree.addstmts([
                 StmtDecl(
                     Decl(_DestroyReason.Type(), subtreewhyvar.name),
                     init=ExprConditional(
                         ExprBinary(_DestroyReason.Deletion, '==', whyvar),
                         _DestroyReason.AncestorDeletion, whyvar)),
                 Whitespace.NL
             ])
 
-        for managed in p.decl.type.manages:
+        for managed in ptype.manages:
             foreachdestroy = StmtFor(
                 init=Param(Type.UINT32, ivar.name, ExprLiteral.ZERO),
                 cond=ExprBinary(ivar, '<', _callCxxArrayLength(kidsvar)),
                 update=ExprPrefixUnop(ivar, '++'))
             foreachdestroy.addstmt(StmtExpr(ExprCall(
                 ExprSelect(ithkid, '->', destroysubtreevar.name),
                 args=[ subtreewhyvar ])))
 
@@ -2975,17 +2985,17 @@ class _GenerateProtocolActorCode(ipdl.as
         # finally, destroy "us"
         destroysubtree.addstmt(StmtExpr(
             ExprCall(_destroyMethod(), args=[ whyvar ])))
         
         self.cls.addstmts([ destroysubtree, Whitespace.NL ])
 
         ## DeallocSubtree()
         deallocsubtree = MethodDefn(MethodDecl(deallocsubtreevar.name))
-        for managed in p.decl.type.manages:
+        for managed in ptype.manages:
             foreachrecurse = StmtFor(
                 init=Param(Type.UINT32, ivar.name, ExprLiteral.ZERO),
                 cond=ExprBinary(ivar, '<', _callCxxArrayLength(kidsvar)),
                 update=ExprPrefixUnop(ivar, '++'))
             foreachrecurse.addstmt(StmtExpr(ExprCall(
                 ExprSelect(ithkid, '->', deallocsubtreevar.name))))
 
             foreachdealloc = StmtFor(
@@ -3017,38 +3027,38 @@ class _GenerateProtocolActorCode(ipdl.as
             ])
             deallocsubtree.addstmt(block)
         # don't delete outselves: either the manager will do it, or
         # we're toplevel
         self.cls.addstmts([ deallocsubtree, Whitespace.NL ])
         
         ## private members
         self.cls.addstmt(StmtDecl(Decl(p.channelType(), 'mChannel')))
-        if p.decl.type.isToplevel():
+        if ptype.isToplevel():
             self.cls.addstmts([
                 StmtDecl(Decl(Type('IDMap', T=Type('ChannelListener')),
                               p.actorMapVar().name)),
                 StmtDecl(Decl(_actorIdType(), p.lastActorIdVar().name)),
                 StmtDecl(Decl(Type('ProcessHandle'),
                               p.otherProcessVar().name))
             ])
-        elif p.decl.type.isManaged():
+        elif ptype.isManaged():
             self.cls.addstmts([
                 StmtDecl(Decl(_actorIdType(), p.idVar().name)),
                 StmtDecl(Decl(p.managerInterfaceType(ptr=1),
                               p.managerVar().name))
             ])
         if p.usesShmem():
             self.cls.addstmts([
                 StmtDecl(Decl(Type('IDMap', T=_rawShmemType()),
                               p.shmemMapVar().name)),
                 StmtDecl(Decl(_shmemIdType(), p.lastShmemIdVar().name))
             ])
 
-        for managed in p.decl.type.manages:
+        for managed in ptype.manages:
             self.cls.addstmts([
                 Whitespace('// Sorted by pointer value\n', indent=1),
                 StmtDecl(Decl(
                     p.managedVarType(managed, self.side),
                     p.managedVar(managed, self.side).name)) ])
 
     def implementManagerIface(self):
         p = self.protocol