Related articles |
---|
C++ templates and overloading dallago@sci.uniud.it (Ugo) (2000-05-01) |
Re: C++ templates and overloading bobduff@world.std.com (Robert A Duff) (2000-05-04) |
Re: C++ templates and overloading rkrayhawk@aol.com (2000-05-08) |
From: | Robert A Duff <bobduff@world.std.com> |
Newsgroups: | comp.compilers |
Date: | 4 May 2000 17:17:06 -0400 |
Organization: | The World Public Access UNIX, Brookline, MA |
References: | 00-05-007 |
Keywords: | C++, OOP |
Ugo <dallago@sci.uniud.it> writes:
> I'm designing a compiler for a proper subset of C++ which includes
> - function templates
> - overloading of functions
> I'm looking for some resources which talks about this problem, i.e.
> polymorphism/overloading in IMPERATIVE languages.
I believe that in C++, you can do overload resolution of function
calls in a single bottom-up pass over each statement. (I'm assuming
your parser builds a tree, which semantic analysis can then walk.)
For a function call F(...), first resolve all the arguments, which
determines their types. Then look up F in your symbol table, to get
the set of all visible F's. Throw away all the F's that have the
wrong parameter types. If you're left with exactly one F, then that's
the one to call. If you're left with zero, or more than one F, it's
an error. You probably want to do some special-casing to get decent
error messages.
C++ allows various implicit type conversions, which complicates the
type-matching.
In Ada, overload resolution requires *two* passes over each statement
(bottom-up, then top-down), because in Ada, the result type of a
function participates in overload resolution. Similarly for the
result type of a literal -- you can have overloaded enumeration
literals, for example. I prefer the Ada rule over the C++ rule.
- Bob
Return to the
comp.compilers page.
Search the
comp.compilers archives again.