Commit Graph

76 Commits

Author SHA1 Message Date
Douglas Gregor 71b209dea6 Properly disambiguate between an elaborated-type-specifier and a
type-parameter within a template parameter list. Found by inspection. 

llvm-svn: 105462
2010-06-04 07:30:15 +00:00
Douglas Gregor 20c38a7c58 Improve recovery when we see a dependent template name that is missing
the required "template" keyword, using the same heuristics we do for
dependent template names in member access expressions, e.g.,

test/SemaTemplate/dependent-template-recover.cpp:11:8: error: use 'template'
      keyword to treat 'getAs' as a dependent template name
    T::getAs<U>();
       ^
       template 

Fixes PR5404.

llvm-svn: 104409
2010-05-21 23:43:39 +00:00
Douglas Gregor 786123dc48 Improve parser recovery when we encounter a dependent template name
that is missing the 'template' keyword, e.g., 

  t->getAs<T>()

where getAs is a member of an unknown specialization. C++ requires
that we treat "getAs" as a value, but that would fail to parse since T
is the name of a type. We would then fail at the '>', since a type
cannot be followed by a '>'.

This is a very common error for C++ programmers to make, especially
since GCC occasionally allows it when it shouldn't (as does Visual
C++). So, when we are in this case, we use tentative parsing to see if
the tokens starting at "<" can only be parsed as a template argument
list. If so, we produce a diagnostic with a fix-it that states that
the 'template' keyword is needed:

test/SemaTemplate/dependent-template-recover.cpp:5:8: error: 'template' keyword
      is required to treat 'getAs' as a dependent template name
    t->getAs<T>();
       ^
       template 

This is just a start of this patch; I'd like to apply the same
approach to everywhere that a template-id with dependent template name
can be parsed.

llvm-svn: 104406
2010-05-21 23:18:07 +00:00
John McCall b54367d2f8 Propagate access specifiers to anonymous union members nested within classes.
Fixes <rdar://problem/7987650>.

llvm-svn: 104376
2010-05-21 20:45:30 +00:00
Douglas Gregor a771f46c82 Reinstate my CodeModificationHint -> FixItHint renaming patch, without
the C-only "optimization".

llvm-svn: 100022
2010-03-31 17:46:05 +00:00
Douglas Gregor 30e631862f Revert r100008, which inexplicably breaks the clang-i686-darwin10 builder
llvm-svn: 100018
2010-03-31 17:25:35 +00:00
Douglas Gregor 3baad0d4f7 Rename CodeModificationHint to FixItHint, since we've been using the
term "fix-it" everywhere and even *I* get tired of long names
sometimes. No functionality change.

llvm-svn: 100008
2010-03-31 15:31:50 +00:00
Sebastian Redl b0e3e1bf67 When placing an annotation token over an existing annotation token, make sure that the new token's range extends to the end of the old token. Assert that in AnnotateCachedTokens. Fixes PR6248.
llvm-svn: 95555
2010-02-08 19:35:18 +00:00
Douglas Gregor 9de54ea41b Reimplement constructor declarator parsing to cope with template-ids
that name constructors, the endless joys of out-of-line constructor
definitions, and various other corner cases that the previous hack
never imagined. Fixes PR5688 and tightens up semantic analysis for
constructor names.

Additionally, fixed a problem where we wouldn't properly enter the
declarator scope of a parenthesized declarator. We were entering the
scope, then leaving it when we saw the ")"; now, we re-enter the
declarator scope before parsing the parameter list.

Note that we are forced to perform some tentative parsing within a
class (call it C) to tell the difference between

  C(int); // constructor

and

  C (f)(int); // member function

which is rather unfortunate. And, although it isn't necessary for
correctness, we use the same tentative-parsing mechanism for
out-of-line constructors to improve diagnostics in icky cases like:

  C::C C::f(int); // error: C::C refers to the constructor name, but
                  // we complain nicely and recover by treating it as
                  // a type.

llvm-svn: 93322
2010-01-13 17:31:36 +00:00
Eli Friedman affd5fdf37 Make sure to give an error for template argument lists followed by junk.
llvm-svn: 92177
2009-12-27 22:31:18 +00:00
Chris Lattner b2434d558d Second half of r91023, saving files is good.
llvm-svn: 91024
2009-12-10 00:45:15 +00:00
Nuno Lopes 221c1fd63e spread 'const' love to some variables. this considerably reduces the amount of dirty data around.
llvm-svn: 91002
2009-12-10 00:07:02 +00:00
Benjamin Kramer 337e3a5fea Remove remaining VISIBILITY_HIDDEN from anonymous namespaces.
llvm-svn: 90044
2009-11-28 19:45:26 +00:00
Alexis Hunt 96d5c76498 Added rudimentary C++0x attribute support.
The following attributes are currently supported in C++0x attribute
lists (and in GNU ones as well):
 - align() - semantics believed to be conformant to n3000, except for
   redeclarations and what entities it may apply to
 - final - semantics believed to be conformant to CWG issue 817's proposed
   wording, except for redeclarations
 - noreturn - semantics believed to be conformant to n3000, except for
   redeclarations
 - carries_dependency - currently ignored (this is an optimization hint)

llvm-svn: 89543
2009-11-21 08:43:09 +00:00
Douglas Gregor 26aedb7460 Implement C++ [temp.param]p2 correctly, looking ahead when we see a
"typename" parameter to distinguish between non-type and type template
parameters. Fixes the actual bug in PR5559.

llvm-svn: 89532
2009-11-21 02:07:55 +00:00
Douglas Gregor ade9bcd72e Cope with extraneous "template" keyword when providing an out-of-line
definition of a member template (or a member thereof). Fixes PR5566.

llvm-svn: 89512
2009-11-20 23:39:24 +00:00
Douglas Gregor c998409cce Remove an overly-eager assertion when replacing tokens with an
annotation token, because some of the tokens we're annotating might
not be in the set of cached tokens (we could have consumed them
unconditionally).

Also, move the tentative parsing from ParseTemplateTemplateArgument
into the one caller that needs it, improving recovery.

llvm-svn: 86904
2009-11-12 00:03:40 +00:00
Douglas Gregor 9167f8bbd5 Introduce a new representation for template template
parameters. Rather than storing them as either declarations (for the
non-dependent case) or expressions (for the dependent case), we now
(always) store them as TemplateNames. 

The primary change here is to add a new kind of TemplateArgument,
which stores a TemplateName. However, making that change ripples to
every switch on a TemplateArgument's kind, also affecting
TemplateArgumentLocInfo/TemplateArgumentLoc, default template
arguments for template template parameters, type-checking of template
template arguments, etc.

This change is light on testing. It should fix several pre-existing
problems with template template parameters, such as:
  - the inability to use dependent template names as template template
  arguments
  - template template parameter default arguments cannot be
  instantiation

However, there are enough pieces missing that more implementation is
required before we can adequately test template template parameters. 

llvm-svn: 86777
2009-11-11 01:00:40 +00:00
Benjamin Kramer 2c9a91c290 Silence warning.
llvm-svn: 86719
2009-11-10 21:29:56 +00:00
Douglas Gregor b53edfb8dc Improve parsing of template arguments to lay the foundation for
handling template template parameters properly. This refactoring:

  - Parses template template arguments as id-expressions, representing
    the result of the parse as a template name (Action::TemplateTy)
    rather than as an expression (lame!).

  - Represents all parsed template arguments via a new parser-specific
    type, ParsedTemplateArgument, which stores the kind of template
    argument (type, non-type, template) along with all of the source
    information about the template argument. This replaces an ad hoc
    set of 3 vectors (one for a void*, which was either a type or an
    expression; one for a bit telling whether the first was a type or
    an expression; and one for a single source location pointing at
    the template argument).

  - Moves TemplateIdAnnotation into the new Parse/Template.h. It never
    belonged in the Basic library anyway.

llvm-svn: 86708
2009-11-10 19:49:08 +00:00
Douglas Gregor 3552259059 Properly replace (cxxscope, template-id) annotation tokens with a
single typename annotation token when backtracing. Fixes PR5350.

llvm-svn: 86034
2009-11-04 18:18:19 +00:00
John McCall 28a6aeab7e Change our basic strategy for avoiding deprecation warnings when the decl use
appears in a deprecated context.  In the new strategy, we emit the warnings
as usual unless we're currently parsing a declaration, where "declaration" is
restricted to mean a decl group or a few special cases in Objective C.  If
we *are* parsing a declaration, we queue up the deprecation warnings until
the declaration has been completely parsed, and then emit them only if the
decl is not deprecated.
We also standardize the bookkeeping for deprecation so as to avoid special cases.

llvm-svn: 85998
2009-11-04 02:18:39 +00:00
Douglas Gregor 71395fa1d5 Implement support for parsing dependent template-ids that refer to
overloaded operators, e.g.,

  p->template operator+<T>()

llvm-svn: 85989
2009-11-04 00:56:37 +00:00
Douglas Gregor 1d0015f8e1 Improved fix for PR3844, which recovers better for class template
partial specializations and explicit instantiations of non-templates.

llvm-svn: 85620
2009-10-30 22:09:44 +00:00
Mike Stump 11289f4280 Remove tabs, and whitespace cleanups.
llvm-svn: 81346
2009-09-09 15:08:12 +00:00
Douglas Gregor 43e75176ec Parse extern templates, pass that information all the way to Sema,
then drop it on the floor.

llvm-svn: 80989
2009-09-04 06:33:52 +00:00
Douglas Gregor a3dff8e37a Keep track of the template parameter depth properly when we have
member templates declared inside other templates. This allows us to
match out-of-line definitions of member function templates within
class templates to the declarations within the class template. We
still can't handle out-of-line definitions for member class templates,
however.

llvm-svn: 79955
2009-08-24 23:03:25 +00:00
Douglas Gregor 3447e76762 Initial support for parsing and representation of member function templates.
llvm-svn: 79570
2009-08-20 22:52:58 +00:00
Douglas Gregor 468535e552 Fix a typo in a variable name
llvm-svn: 79558
2009-08-20 18:46:05 +00:00
John McCall ef50e99783 sp.
llvm-svn: 77656
2009-07-31 02:20:35 +00:00
Douglas Gregor e93e46c690 Implement support for out-of-line definitions of the class members of class
templates, e.g.,

  template<typename T>
  struct Outer {
    struct Inner;
  };

  template<typename T>
  struct Outer<T>::Inner {
    // ...
  };

Implementing this feature required some extensions to ActOnTag, which
now takes a set of template parameter lists, and is the precursor to
removing the ActOnClassTemplate function from the parser Action
interface. The reason for this approach is simple: the parser cannot
tell the difference between a class template definition and the
definition of a member of a class template; both have template
parameter lists, and semantic analysis determines what that template
parameter list means.

There is still some cleanup to do with ActOnTag and
ActOnClassTemplate. This commit provides the basic functionality we
need, however.

llvm-svn: 76820
2009-07-22 23:48:44 +00:00
Chris Lattner 5558e9fc55 fix PR4452, a crash on invalid. The error recovery is still terrible in this case
but at least we don't crash :)

llvm-svn: 74264
2009-06-26 04:27:47 +00:00
Douglas Gregor 17a7c1297a Make sure that the template parameter lists get from the parser down to ActOnFunctionDeclarator for function template definitions
llvm-svn: 74040
2009-06-24 00:54:41 +00:00
Douglas Gregor b52fabb2a8 Start propagating template parameter lists to the right places to
handle function templates. There's no actual code for function
templates yet, but at least we complain about typedef templates.

llvm-svn: 74021
2009-06-23 23:11:28 +00:00
Douglas Gregor 0b6a6242ed Rework the way we track which declarations are "used" during
compilation, and (hopefully) introduce RAII objects for changing the
"potentially evaluated" state at all of the necessary places within
Sema and Parser. Other changes:

  - Set the unevaluated/potentially-evaluated context appropriately
    during template instantiation.
  - We now recognize three different states while parsing or
    instantiating expressions: unevaluated, potentially evaluated, and
    potentially potentially evaluated (for C++'s typeid).
  - When we're in a potentially potentially-evaluated context, queue
    up MarkDeclarationReferenced calls in a stack. For C++ typeid
    expressions that are potentially evaluated, we will play back
    these MarkDeclarationReferenced calls when we exit the
    corresponding potentially potentially-evaluated context.
  - Non-type template arguments are now parsed as constant
    expressions, so they are not potentially-evaluated.

llvm-svn: 73899
2009-06-22 20:57:11 +00:00
Anders Carlsson f986ba7528 Address more comments from Doug.
llvm-svn: 73267
2009-06-12 23:09:56 +00:00
Anders Carlsson 01e9e93485 Parse support for C++0x type parameter packs.
llvm-svn: 73247
2009-06-12 19:58:00 +00:00
Douglas Gregor 269f0b1b69 Merge the ASTVector and ASTOwningVector templates, since they offered
redundant functionality. The result (ASTOwningVector) lives in
clang/Parse/Ownership.h and is used by both the parser and semantic
analysis. No intended functionality change.

llvm-svn: 72214
2009-05-21 16:25:11 +00:00
Jay Foad 7d0479f2c2 Use v.data() instead of &v[0] when SmallVector v might be empty.
llvm-svn: 72210
2009-05-21 09:52:38 +00:00
Douglas Gregor 1b57ff32a8 Implement parsing for explicit instantiations of class templates, e.g.,
template class X<int>;

This also cleans up the propagation of template information through
declaration parsing, which is used to improve some diagnostics.

llvm-svn: 71608
2009-05-12 23:25:50 +00:00
Douglas Gregor 2399628cea Refactor the parsing of declarations so that template declarations can
parse just a single declaration and provide a reasonable diagnostic
when the "only one declarator per template declaration" rule is
violated. This eliminates some ugly, ugly hackery where we used to
require thatn the layout of a DeclGroup of a single element be the
same as the layout of a single declaration.

llvm-svn: 71596
2009-05-12 21:31:51 +00:00
Chris Lattner 49836b448e fix a FIXME, providing accurate source range info for DeclStmt's. The end
of the range is now the ';' location.  For something like this:

$ cat t2.c 

#define bool int
void f(int x, int y) {
bool b = !x && y;
}

We used to produce:
$ clang-cc t2.c -ast-dump 
typedef char *__builtin_va_list;

void f(int x, int y)
(CompoundStmt 0x2201f10 <t2.c:3:22, line:5:1>
  (DeclStmt 0x2201ef0 <line:2:14>                   <----
    0x2201a20 "int b =
      (BinaryOperator 0x2201ed0 <line:4:10, col:16> 'int' '&&'
        (UnaryOperator 0x2201e90 <col:10, col:11> 'int' prefix '!'
          (DeclRefExpr 0x2201c90 <col:11> 'int' ParmVar='x' 0x2201a50))
        (DeclRefExpr 0x2201eb0 <col:16> 'int' ParmVar='y' 0x2201e10))")


Now we produce:

$ clang-cc t2.c -ast-dump
typedef char *__builtin_va_list;

void f(int x, int y)
(CompoundStmt 0x2201f10 <t2.c:3:22, line:5:1>
  (DeclStmt 0x2201ef0 <line:2:14, line:4:17>         <------
    0x2201a20 "int b =
      (BinaryOperator 0x2201ed0 <col:10, col:16> 'int' '&&'
        (UnaryOperator 0x2201e90 <col:10, col:11> 'int' prefix '!'
          (DeclRefExpr 0x2201c90 <col:11> 'int' ParmVar='x' 0x2201a50))
        (DeclRefExpr 0x2201eb0 <col:16> 'int' ParmVar='y' 0x2201e10))")

llvm-svn: 68288
2009-04-02 04:16:50 +00:00
Douglas Gregor fe3d7d0880 Make parsing a semantic analysis a little more robust following Sema
failures that involve malformed types, e.g., "typename X::foo" where
"foo" isn't a type, or "std::vector<void>" that doens't instantiate
properly.

Similarly, be a bit smarter in our handling of ambiguities that occur
in Sema::getTypeName, to eliminate duplicate error messages about
ambiguous name lookup.

This eliminates two XFAILs in test/SemaCXX, one of which was crying
out to us, trying to tell us that we were producing repeated error
messages.

llvm-svn: 68251
2009-04-01 21:51:26 +00:00
Douglas Gregor b67535d1b6 Parsing and AST representation for dependent template names that occur
within nested-name-specifiers, e.g., for the "apply" in

  typename MetaFun::template apply<T1, T2>::type

At present, we can't instantiate these nested-name-specifiers, so our
testing is sketchy.

llvm-svn: 68081
2009-03-31 00:43:58 +00:00
Douglas Gregor dc572a3266 Improve the representation of template names in the AST. This
representation handles the various ways in which one can name a
template, including unqualified references ("vector"), qualified
references ("std::vector"), and dependent template names
("MetaFun::template apply").

One immediate effect of this change is that the representation of
nested-name-specifiers in type names for class template
specializations (e.g., std::vector<int>) is more accurate. Rather than
representing std::vector<int> as

  std::(vector<int>)

we represent it as

  (std::vector)<int>

which more closely follows the C++ grammar. 

Additionally, templates are no longer represented as declarations
(DeclPtrTy) in Parse-Sema interactions. Instead, I've introduced a new
OpaquePtr type (TemplateTy) that holds the representation of a
TemplateName. This will simplify the handling of dependent
template-names, once we get there.

llvm-svn: 68074
2009-03-30 22:58:21 +00:00
Chris Lattner 5bbb3c8ad9 Push DeclGroup much farther throughout the compiler. Now the various
productions (except the already broken ObjC cases like @class X,Y;) in 
the parser that can produce more than one Decl return a DeclGroup instead
of a Decl, etc.

This allows elimination of the Decl::NextDeclarator field, and exposes
various clients that should look at all decls in a group, but which were
only looking at one (such as the dumper, printer, etc).  These have been
fixed.

Still TODO:

1) there are some FIXME's in the code about potentially using
DeclGroup for better location info.
2) ParseObjCAtDirectives should return a DeclGroup due to @class etc.
3) I'm not sure what is going on with StmtIterator.cpp, or if it can
   be radically simplified now.
4) I put a truly horrible hack in ParseTemplate.cpp.

I plan to bring up #3/4 on the mailing list, but don't plan to tackle
#1/2 in the short term.

llvm-svn: 68002
2009-03-29 16:50:03 +00:00
Chris Lattner 83f095cc7e Introduce a new OpaquePtr<N> struct type, which is a simple POD wrapper for a
pointer.  Its purpose in life is to be a glorified void*, but which does not
implicitly convert to void* or other OpaquePtr's with a different UID.

Introduce Action::DeclPtrTy which is a typedef for OpaquePtr<0>.  Change the 
entire parser/sema interface to use DeclPtrTy instead of DeclTy*.  This
makes the C++ compiler enforce that these aren't convertible to other opaque
types.

We should also convert ExprTy, StmtTy, TypeTy, AttrTy, BaseTy, etc,
but I don't plan to do that in the short term.

The one outstanding known problem with this patch is that we lose the 
bitmangling optimization where ActionResult<DeclPtrTy> doesn't know how to
bitmangle the success bit into the low bit of DeclPtrTy.  I will rectify
this with a subsequent patch.

llvm-svn: 67952
2009-03-28 19:18:32 +00:00
Anders Carlsson dfbbdf6fd5 Handle parsing of templates in member declarations. Pass the AccessSpecifier all the way down to ActOnClassTemplate.
Doug, Sebastian: Plz review! :)

llvm-svn: 67723
2009-03-26 00:52:18 +00:00
Douglas Gregor 96977da72c Clean up and document code modification hints.
llvm-svn: 65641
2009-02-27 17:53:17 +00:00
Douglas Gregor 87f95b0a6a Introduce code modification hints into the diagnostics system. When we
know how to recover from an error, we can attach a hint to the
diagnostic that states how to modify the code, which can be one of:

  - Insert some new code (a text string) at a particular source
    location
  - Remove the code within a given range
  - Replace the code within a given range with some new code (a text
    string)

Right now, we use these hints to annotate diagnostic information. For
example, if one uses the '>>' in a template argument in C++98, as in
this code:

  template<int I> class B { };
  B<1000 >> 2> *b1;

we'll warn that the behavior will change in C++0x. The fix is to
insert parenthese, so we use code insertion annotations to illustrate
where the parentheses go:

test.cpp:10:10: warning: use of right-shift operator ('>>') in template
argument will require parentheses in C++0x
  B<1000 >> 2> *b1;
         ^
    (        )


Use of these annotations is partially implemented for HTML
diagnostics, but it's not (yet) producing valid HTML, which may be
related to PR2386, so it has been #if 0'd out.

In this future, we could consider hooking this mechanism up to the
rewriter to actually try to fix these problems during compilation (or,
after a compilation whose only errors have fixes). For now, however, I
suggest that we use these code modification hints whenever we can, so
that we get better diagnostics now and will have better coverage when
we find better ways to use this information.

This also fixes PR3410 by placing the complaint about missing tokens
just after the previous token (rather than at the location of the next
token).

llvm-svn: 65570
2009-02-26 21:00:50 +00:00