[cfe-commits] [PATCH] Implement C++0x deduced auto type

Richard Smith richard at metafoo.co.uk
Thu Feb 17 18:26:46 PST 2011


Hi,

The attached patch implements the C++0x deduced 'auto' type feature. This
should fix the following PRs:

Bug 8738 - C++'0x auto not implemented
Bug 9060 - Late-specified return type accepted for parenthesized declarator
Bug 9132 - clang accepts declarators other than a literal 'auto' as the
return type of a function with a trailing-return-type

(The latter two were fixed in order to allow more comprehensive testing of
deduced auto!)

The patch introduces a new type, AutoType, which carries a deduced type.
AutoType is usually just sugar around the deduced type. However, if the
type hasn't been deduced yet (either because the initializer has not been
attached or because the auto is within a template and the initializer was
type-dependent), the AutoType is canonical and is considered dependent.

Type can be queried for the contained auto type, for issuing diagnostics
and determining whether auto type deduction is necessary. This will find
the AutoType within the current type, without crossing declaration
boundaries (it won't find auto via a decltype, for instance). However,
this search is potentially non-trivial, so it is avoided where possible by
passing a flag to indicate whether the type specifier was auto. In the
template instantiation, case the flag is always true and we must do the
check.

A flag is introduced on VarDecl to indicate whether that VarDecl has an
auto type for which we are currently parsing the initializer, to allow us
to rapidly diagnose any uses of the value from within its initializer.

In the auto case, merging of types in redeclarations is deferred, since
the merging code is called before the initializer is attached and the auto
type is deduced. The checking is redone after auto type deduction.

The auto type deduction itself reuses the template function call type
deduction code. A new TreeTraversal is used to substitute the placeholder
AutoType for a template parameter or a deduced AutoType.


In implementing this patch, I've found a number of areas where the c++
draft standard is silent or unclear. These issues and my resolutions to
them are:

1) auto x(1, 2, 3);

The standard does not obviously specify that this is ill-formed. There's a
corner case with variadic templates where "auto x();" can be interpreted
as a variable declaration rather than a function declaration; that has the
same problem. I've assumed this is intended to be ill-formed. g++ rejects
it (with a terrible diagnostic about a comma-operator).

2) auto a = 0, b = { 1, 2, 3 }, (*f)() -> double;

To my reading of the standard, this is legal (it is the /deduced/ types
which must match: in the initializer-list case, that's the type it's a
list of). Since clang doesn't support initializer-lists yet, that part
will need handling later. AutoType doesn't store enough information to
diagnose that issue.

3) auto (*g)() -> auto = &f;

I'm reasonably confident this is ill-formed. [decl.spec.auto]p3 nearly
forbids this by saying 'auto shall appear as one of the decl-specifiers in
the decl-specifier-seq', which doesn't rule this out because a different
auto appears there! The 'Otherwise' at the start of p3 seems more
conclusive.


Thanks for your time, I look forward to whatever comments you may have!
Richard
-------------- next part --------------
A non-text attachment was scrubbed...
Name: clang-auto.diff
Type: text/x-patch
Size: 79680 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20110218/0cf0a882/attachment.bin>


More information about the cfe-commits mailing list