home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / lang / ada / 2396 < prev    next >
Encoding:
Text File  |  1992-08-21  |  3.8 KB  |  83 lines

  1. Newsgroups: comp.lang.ada
  2. Path: sparky!uunet!noc.near.net!inmet!spock!stt
  3. From: stt@spock.camb.inmet.com (Tucker Taft)
  4. Subject: Re: Ada 9x Dispatching Question
  5. Message-ID: <1992Aug21.145044.5567@inmet.camb.inmet.com>
  6. Keywords: Ada Dispatching Polymorphism
  7. Sender: news@inmet.camb.inmet.com
  8. Nntp-Posting-Host: spock
  9. Organization: Intermetrics Inc, Cambridge MA
  10. References: <334@sps.com> <1992Aug17.215219.967@inmet.camb.inmet.com> <9223120.6784@mulga.cs.mu.OZ.AU>
  11. Date: Fri, 21 Aug 1992 14:50:44 GMT
  12. Lines: 69
  13.  
  14. In article <9223120.6784@mulga.cs.mu.OZ.AU> 
  15. fjh@munta.cs.mu.OZ.AU (Fergus James HENDERSON) writes:
  16.  
  17. >bwhite@cobra.camb.inmet.com (Bill White) writes:
  18. >
  19. >>The MRT has discussed this at length in the past.  Tucker believes that
  20. >>it is important to have a syntactic marker to tell when a call is
  21. >>dispatching.  An alternative rule would be the C++ rule, where all
  22. >>dispatching operations dispatch always.  (Ada9X dispatching operations
  23. >>are roughly equivalent to C++ virtual operations.)  This means that the 
  24. >>compiler could not replace a dispatching call with a non-dispatching
  25. >>call even if the compiler knows where the routine is going to go
  26. >>anyway.  This is considered expensive.
  27. >
  28. >I don't understand why the compiler can't replace a dispatching call
  29. >with a non-dispatching one if it really does know the exact type of
  30. >the operand.  This is quite possible in C++. Indeed, in C++ you can
  31. >declare a function to be both inline and virtual, so that if the
  32. >compiler manages to avoid the dispatch, it can then insert the code
  33. >directly and avoid any function call altogether.
  34.  
  35. The compiler can certainly do that if it knows the "exact type"
  36. at compile time.  However, for a by-reference parameter in
  37. C++, one never knows the "exact type" at compile-time.
  38. On the other hand, with an object or a by-value parameter,
  39. a C++ compiler always knows at compile-time the "exact type."
  40. And as you illustrate, a decent optimizer can track the
  41. value of a reference presuming it is initialized locally.
  42.  
  43. However, we are talking here exclusively about parameters.
  44. Unless you inline the function, the C++ compiler has no idea
  45. what is the exact type or a by-reference parameter.  
  46.  
  47. In Ada 9X, we distinguish explicitly between "class-wide" types
  48. and "specific" types.  If a formal parameter has a specific type,
  49. then all direct uses of that parameter later treat it as that specific type.
  50. If a formal parameter has a class-wide type, or if one converts
  51. an expression of a specific type to a class-wide type, one
  52. then has a "dynamically tagged" expression, and run-time dispatch
  53. is used to locate the correct body.
  54.  
  55. Another way to think of it is that the parameter is "viewed" as being
  56. of a specific type, but upon conversion to a class-wide type,
  57. it is viewed as dynamically tagged.  A similar distinction can
  58. be made in C++.  Explicit qualification like p->c1::func can
  59. be used to prevent dispatching when p is a reference,
  60. whereas p->func always dispatches.  
  61.  
  62. In Ada 9X you have three choices, given a formal parameter 
  63. p of some specific type c1.  You can call the "c1" version of
  64. func, by simply saying "func(p)."  You can call some other specific 
  65. ancestor's type's (like c0) version of func by saying "func(c0(p))."
  66. Or you can "redispatch" by saying "func(c1'class(p))" which
  67. will look at the tag of p at runtime and decide which func to call.
  68.  
  69. The Ada 9X approach is consistent with the way derived types
  70. work in Ada 83, with the addition of the "class-wide" alternative.
  71.  
  72. If the parameter p is itself of a class-wide type, then func(p) will
  73. always dispatch, which can be "turned off" by explicitly
  74. converting p to some specific type, such as func(c2(p)) which
  75. will verify that p is in the class rooted at c2, and then call
  76. c2's func.
  77.  
  78. >Fergus Henderson             fjh@munta.cs.mu.OZ.AU      
  79.  
  80. S. Tucker Taft   stt@inmet.com
  81. Intermetrics, Inc.
  82. Cambridge, MA  02138
  83.