Commit Graph

590 Commits

Author SHA1 Message Date
Richard Smith f2b681b4d8 constexpr: diagnostic improvements for invalid lvalue-to-rvalue conversions in
constant expressions.

llvm-svn: 147035
2011-12-21 05:04:46 +00:00
Richard Smith 5894a9138c Evaluation support for ExprWithCleanups. We won't evaluate any expression which
actually requires non-trivial cleanups, so no cleanups need to be performed.

llvm-svn: 146916
2011-12-19 22:12:41 +00:00
Richard Smith d0b111c3dd Improve r146813 (for PR11595) to give an appropriate diagnostic.
llvm-svn: 146915
2011-12-19 22:01:37 +00:00
Richard Smith d0b4dd656d constexpr handling improvements. Produce detailed diagnostics when a 'constexpr'
variable is initialized by a non-constant expression, and pass in the variable
being declared so that earlier-initialized fields' values can be used.

Rearrange VarDecl init evaluation to make this possible, and in so doing fix a
long-standing issue in our C++ constant expression handling, where we would
mishandle cases like:

  extern const int a;
  const int n = a;
  const int a = 5;
  int arr[n];

Here, n is not initialized by a constant expression, so can't be used in an ICE,
even though the initialization expression would be an ICE if it appeared later
in the TU. This requires computing whether the initializer is an ICE eagerly,
and saving that information in PCH files.

llvm-svn: 146856
2011-12-19 06:19:21 +00:00
Richard Smith 0b973d091a PR11604: don't allow floating-literal-to-integer casts in ICEs if the (truncated)
floating literal value does not fit into the destination type. Such casts have
undefined behavior at translation time; treating them as non-ICE matches the
behavior of modern gcc versions.

llvm-svn: 146842
2011-12-18 02:33:09 +00:00
Eli Friedman 79281d1df9 Add a missing check before trying to evaluate a temporary. PR11595.
llvm-svn: 146813
2011-12-17 02:24:21 +00:00
Richard Smith f6f003af6a C++11 constexpr: Add note stacks containing backtraces if constant evaluation
fails within a call to a constexpr function. Add -fconstexpr-backtrace-limit
argument to driver and frontend, to control the maximum number of notes so
produced (default 10). Fix APValue printing to be able to pretty-print all
APValue types, and move the testing for this functionality from a unittest to
a -verify test now that it's visible in clang's output.

llvm-svn: 146749
2011-12-16 19:06:07 +00:00
Richard Smith 357362d00b Add checks and diagnostics for many of the cases which C++11 considers to not
be constant expressions.

llvm-svn: 146479
2011-12-13 06:39:58 +00:00
Richard Smith ff07af12df Clean up diagnostic wording for disallowed casts in C++11 constant expressions.
llvm-svn: 146395
2011-12-12 19:10:03 +00:00
Richard Smith 6d6ecc34f8 Implement C++11 constant expression cast restrictions.
llvm-svn: 146371
2011-12-12 12:46:16 +00:00
Richard Smith 187ef01404 Fix some not-yet-used diagnostic code in a template, which gcc spotted and clang
did not!

llvm-svn: 146366
2011-12-12 09:41:58 +00:00
Richard Smith 92b1ce0a80 Prepare constant expression infrastructure for the generation of richer
diagnostics. No functionality change.

llvm-svn: 146365
2011-12-12 09:28:41 +00:00
Richard Smith 036e2bd07a Add a fast path to the constant evaluator for integer literals. This speeds up
compilation of some translation units of SPEC's 445.gobmk by ~4%, and does not
seem to cause a measurable slowdown in other cases.

llvm-svn: 146306
2011-12-10 01:10:13 +00:00
Richard Smith f57d8cb131 C++11 constant expressions: Don't use CheckICE in C++11; instead, determine
whether an expression is a (core) constant expression as a side-effect of
evaluation. This takes us from accepting far too few expressions as ICEs to
accepting slightly too many -- fixes for the remaining cases are coming next.

The diagnostics produced when an expression is found to be non-constant are
currently quite poor (with generic wording but reasonable source locations),
and will be improved in subsequent commits.

llvm-svn: 146289
2011-12-09 22:58:01 +00:00
Argyrios Kyrtzidis fac35c0a9b In ExprEvaluatorBase::VisitOpaqueValueExpr() add a sanity check to avoid
infinite recursion due to bad OpaqueValueExpr.

llvm-svn: 146237
2011-12-09 02:44:48 +00:00
Richard Smith 10c7c9090e Replace the implementation of __builtin_constant_p (which was based on the GCC
documentation) with one based on what GCC's __builtin_constant_p is actually
intended to do (discovered by asking a friendly GCC developer).

In particular, an expression which folds to a pointer is now only considered to
be a "constant" by this builtin if it refers to the first character in a string
literal.

This fixes a rather subtle wrong-code issue when building with glibc. Given:

const char cs[4] = "abcd";
int f(const char *p) { return strncmp(p, cs, 4); }

... the macro magic for strncmp produces a (potentially crashing) call to
strlen(cs), because it expands to an expression starting with:

  __builtin_constant_p(cs) && strlen(cs) < 4 ? /* ... */

Under the secret true meaning of __builtin_constant_p, this is guaranteed to be
safe!

llvm-svn: 146236
2011-12-09 02:04:48 +00:00
Richard Smith 42d3af9d95 When folding the size of a global scope VLA to a constant, require the array
bound to not have side effects(!). Add constant-folding support for expressions
of void type, to ensure that we can still fold ((void)0, 1) as an array bound.

llvm-svn: 146000
2011-12-07 00:43:50 +00:00
Richard Smith 161f09abd7 Move vector bitcast handling in constant expressions from the expression
evaluator into constant initializer handling / IRGen. The practical consequence
of this is that the bitcast now lives in the constant's definition, rather than
in its uses.

The code in the constant expression evaluator was producing vectors of the wrong
type and size (and possibly of the wrong value for a big-endian int-to-vector
bitcast). We were getting away with this only because we don't yet support
constant-folding of any expressions which inspect vector values.

llvm-svn: 145981
2011-12-06 22:44:34 +00:00
Lang Hames d42bb47782 Make isWeakDecl available as a method on ValueDecl.
llvm-svn: 145845
2011-12-05 20:16:26 +00:00
Richard Smith 9a56882e53 Add driver arguments -ftemplate-depth=N and -fconstexpr-depth=N, with the same
semantics and defaults as the corresponding g++ arguments. The historical g++
argument -ftemplate-depth-N is kept for compatibility, but modern g++ versions
no longer document that option.

Add -cc1 argument -fconstexpr-depth N to implement the corresponding
functionality.

The -ftemplate-depth=N part of this fixes PR9890.

llvm-svn: 145045
2011-11-21 19:36:32 +00:00
Richard Smith 027bf11eb3 Constant expression evaluation: add support for evaluation of member pointers
and base-to-derived casts, and add proper handling of temporaries.

llvm-svn: 144926
2011-11-17 22:56:20 +00:00
Richard Smith 10f4d06b81 PR11391: Don't try to evaluate the LHS of a _Complex assignment as an rvalue.
llvm-svn: 144799
2011-11-16 17:22:48 +00:00
Richard Smith cf74da76db Fix PR11385: A pointer constant expression which has been cast via an integer is
not safely derived. Don't allow lvalue-to-rvalue conversions on the result of
dereferencing such a pointer.

llvm-svn: 144783
2011-11-16 07:18:12 +00:00
Richard Smith ce40ad677e Represent an APValue based on a Decl as that Decl, rather than a DeclRefExpr
or MemberExpr which refers to it. As a side-effect, MemberExprs which refer to
static member functions and static data members are now emitted as constant
expressions.

llvm-svn: 144468
2011-11-12 22:28:03 +00:00
Richard Smith 6804be5a2e Constant expression evalation: const_cast support.
llvm-svn: 144382
2011-11-11 08:28:03 +00:00
Richard Smith 6049446c47 Reduce the constexpr stack pressure somewhat. Hopefully this will be enough to
please the buildbots.

llvm-svn: 144375
2011-11-11 05:48:57 +00:00
Richard Smith e97cbd7b3a Constant expression evaluation: support for constexpr member functions. This
reinstates r144273; a combination of r144333's fix for NoOp rvalue-to-lvalue
casts and some corresponding changes here resolve the regression which that
caused.

This patch also adds support for some additional forms of member function call,
along with additional testing.

llvm-svn: 144369
2011-11-11 04:05:33 +00:00
Devang Patel 63104ad417 Revert r144273. It causes clang self-host build failure.
llvm-svn: 144296
2011-11-10 17:47:39 +00:00
Richard Smith 656d49d890 Constant expression evaluation: support for constexpr member functions.
llvm-svn: 144273
2011-11-10 09:31:24 +00:00
Richard Smith d62306a481 Constant expression evaluation: support for evaluation of structs and unions of
literal types, as well as derived-to-base casts for lvalues and
derived-to-virtual-base casts.

llvm-svn: 144265
2011-11-10 06:34:14 +00:00
Richard Smith 5686e75a7b Temporary fix for a performance problem Eli spotted. The APValue representation
is currently too inefficient to allow us to use it for array initializers, but
fortunately we usually don't yet need to evaluate such initializers.

llvm-svn: 144260
2011-11-10 03:30:42 +00:00
Richard Smith f8120cad16 Constant expression evaluation: support for default arguments.
llvm-svn: 144156
2011-11-09 02:12:41 +00:00
Richard Smith ec8dcd2716 Fix a cluster of related issues involving value-dependence and constant
expression evaluation:
 - When folding a non-value-dependent expression, we may try to use the
   initializer of a value-dependent variable. If that happens, give up.
 - In C++98, actually check that a const, non-volatile DeclRefExpr inside an ICE
   is of integral or enumeration type (a reference isn't OK!)
 - In C++11, DeclRefExprs for objects of const literal type initialized with
   value-dependent expressions are themselves value-dependent.
 - So are references initialized with value-dependent expressions (though this
   case is missing from the C++11 standard, along with many others).

llvm-svn: 144056
2011-11-08 01:31:09 +00:00
Richard Smith f3e9e43da4 Constant expression evaluation: support for arrays.
llvm-svn: 143922
2011-11-07 09:22:26 +00:00
John McCall ffc8ca2d84 Rip out CK_GetObjCProperty.
llvm-svn: 143910
2011-11-07 05:09:54 +00:00
Richard Smith 8081560048 Constant expression evaluation: preserve subobject designator when flattening a
core constant value down to an APValue.

llvm-svn: 143909
2011-11-07 05:07:52 +00:00
Richard Smith a08acd8588 Allow constexpr variables' initializers to be folded in C++11 mode. This
partially undoes the revert in r143491, but does not introduce any new instances
of the underlying issue (which is not yet fixed) in code which does not use
the 'constexpr' keyword.

llvm-svn: 143905
2011-11-07 03:22:51 +00:00
John McCall fe96e0b6be Change the AST representation of operations on Objective-C
property references to use a new PseudoObjectExpr
expression which pairs a syntactic form of the expression
with a set of semantic expressions implementing it.
This should significantly reduce the complexity required
elsewhere in the compiler to deal with these kinds of
expressions (e.g. IR generation's special l-value kind,
the static analyzer's Message abstraction), at the lower
cost of specifically dealing with the odd AST structure
of these expressions.  It should also greatly simplify
efforts to implement similar language features in the
future, most notably Managed C++'s properties and indexed
properties.

Most of the effort here is in dealing with the various
clients of the AST.  I've gone ahead and simplified the
ObjC rewriter's use of properties;  other clients, like
IR-gen and the static analyzer, have all the old
complexity *and* all the new complexity, at least
temporarily.  Many thanks to Ted for writing and advising
on the necessary changes to the static analyzer.

I've xfailed a small diagnostics regression in the static
analyzer at Ted's request.

llvm-svn: 143867
2011-11-06 09:01:30 +00:00
Benjamin Kramer 62144477eb Remove unused variables.
llvm-svn: 143696
2011-11-04 15:05:51 +00:00
Richard Smith ed5165ff52 Constant expression evaluation: refactor to start the groundwork for coping with
initializations which refer indirectly to elements of the object being
initialized.

llvm-svn: 143680
2011-11-04 05:33:44 +00:00
Richard Smith 96e0c101fe Constant expression evaluation: track the manner in which an lvalue was written,
to allow us to implement the C++11 rule that a non-active union member can't be
read, and use it to implement subobject access for string literals.

llvm-svn: 143677
2011-11-04 02:25:55 +00:00
Richard Smith e9e20dd302 Constant expression evaluation: although we don't know whether a literal will
be at the same address as another object, we do know it won't alias a null
pointer.

llvm-svn: 143674
2011-11-04 01:10:57 +00:00
Richard Smith cecf184e64 When constant-folding, don't look at the initializer of a global const variable
if it's marked as weak: that definition may not end up being used.

llvm-svn: 143496
2011-11-01 21:06:14 +00:00
Richard Smith 1f1f2d8ca3 Temporarily disable lvalue-to-rvalue conversions on const pointers while an
apparent miscompile triggered by this is investigated. This is essentially a
revert of r143298.

llvm-svn: 143491
2011-11-01 20:38:59 +00:00
Richard Smith fec0992aca Implement C++11 'constexpr calls must return constant expressions' rule, and
perform the code simplifications this rule allows.

llvm-svn: 143463
2011-11-01 16:57:24 +00:00
Eli Friedman 42fbd62d92 Some minor comment changes in constant-folding comparisons.
llvm-svn: 143391
2011-10-31 22:54:30 +00:00
Eli Friedman c6be94b3de Don't try to fold comparisons between the address of an object and an arbitrary integer constant. Fixes regression from r143334.
llvm-svn: 143374
2011-10-31 22:28:05 +00:00
Richard Smith dd78544d44 Refactoring and test for r143360. Support for array rvalue to pointer decay is
needed for C++11, and will follow later.

llvm-svn: 143363
2011-10-31 20:57:44 +00:00
Richard Smith fdc6a5956a Temporary fix for assert while evaluating array-to-pointer decay on array
rvalue. Test and better fix to follow.

llvm-svn: 143360
2011-10-31 20:20:33 +00:00
Richard Smith 4e4c78ffff C++11 generalized constant expression handling: evaluation support for
materialized temporaries.

llvm-svn: 143335
2011-10-31 05:52:43 +00:00
Richard Smith 83c682115b C++11 generalized constant expressions: evaluate equality comparisons between
arbitrary pointers, if those pointers don't point to weak objects or literals.

llvm-svn: 143334
2011-10-31 05:11:32 +00:00
Richard Smith 8b3497ee54 C++11 generalized constant expressions: support pointer comparisons where the
result is not unspecified.

llvm-svn: 143329
2011-10-31 01:37:14 +00:00
Richard Smith 390cd49906 Fix assert on constant expression evaluation of floating point increment.
llvm-svn: 143320
2011-10-30 23:17:09 +00:00
Richard Smith 9c8d1c5c64 Don't crash if a GCC binary conditional is used in a constant expression on an
integer-cast pointer value.

llvm-svn: 143299
2011-10-29 22:55:55 +00:00
Richard Smith 35a1f85466 constexpr evaluation: allow lvalue-to-rvalue conversion on any literal type, not
just integers and floating point types. Since we don't support evaluating class
types or performing lvalue-to-rvalue conversions on array elements yet, this
just means pointer types right now.

llvm-svn: 143298
2011-10-29 21:53:17 +00:00
Richard Smith 0b0a0b6174 constexpr function substitution:
Track the function invocation where an lvalue referring to a constexpr function
parameter originated from, and use it to substitute the correct argument and to
determine whether such an argument's lifetime has ended.

llvm-svn: 143296
2011-10-29 20:57:55 +00:00
Richard Smith 7b553f1b19 Rename Expr::Evaluate to Expr::EvaluateAsRValue to make it clear that it will
implicitly perform an lvalue-to-rvalue conversion if used on an lvalue
expression. Also improve the documentation of Expr::Evaluate* to indicate which
of them will accept expressions with side-effects.

llvm-svn: 143263
2011-10-29 00:50:52 +00:00
Richard Smith 472d495372 Fix assertion in constant expression evaluation. The LHS of a floating-point
binary operator isn't an rvalue if it's an assignment operator.

llvm-svn: 143250
2011-10-28 23:26:52 +00:00
Richard Smith 254a73d636 Initial support for C++11 constexpr function invocation substitution. Using
constexpr function arguments outside of their function (passing or returning
them by reference) does not work correctly yet.

Calling constexpr function templates does not work yet, since the bodies are not
instantiated until the end of the translation unit.

llvm-svn: 143234
2011-10-28 22:34:42 +00:00
Richard Smith 11562c5e10 Reinstate r142844 (reverted in r142872) now that lvalue-to-rvalue conversions
are present in all the necessary places:

In constant expression evaluation, evaluate lvalues as lvalues and rvalues as
rvalues. Remove special case for caching reference initialization and fix a
cyclic initialization crash in the process.

llvm-svn: 143204
2011-10-28 17:51:58 +00:00
Richard Smith 2d7bb04788 Clean up, as suggested by John.
llvm-svn: 142884
2011-10-25 00:21:54 +00:00
Richard Smith 733237db23 Revert r142844, it broke selfhost. The problem appears to be a missing
lvalue-to-rvalue conversion on the LHS operand of '->'.

llvm-svn: 142872
2011-10-24 23:14:33 +00:00
Richard Smith 62f6595122 Add explanatory comments for ICE checking in C99 mode.
llvm-svn: 142866
2011-10-24 22:35:48 +00:00
Richard Smith cbb407268e Constant expression evaluation: evaluate lvalues as lvalues, and rvalues as
rvalues, as C++11 constant evaluation semantics require. DeclRefs referring to
references can now use the normal initialization-caching codepath, which
incidentally fixes a crash in cyclic initialization of references.

llvm-svn: 142844
2011-10-24 21:07:08 +00:00
Richard Smith 4a6781215e Constant expression evaluation: factor out handling of ignored values.
llvm-svn: 142835
2011-10-24 18:44:57 +00:00
Richard Smith c3e31e7bb2 In accordance with the C89, C99 and C++98 standards, ICEs can only contain
floating-point literals if they are the immediate operands of casts.
ImplicitCastExpr is not a cast in the language-standards sense.

llvm-svn: 142832
2011-10-24 18:26:35 +00:00
Richard Smith 2790870de0 Constant expression evaluation: factor out VarDecl initializer evaluation and
caching.

llvm-svn: 142812
2011-10-24 17:54:18 +00:00
Richard Smith 2d4063412c Refactor vector constant expression evaluation to return bool like all the other
const expression evaluation subclasses, and remove some APValue copying and
malloc traffic in the process.

llvm-svn: 142733
2011-10-22 21:10:00 +00:00
Eli Friedman a4c2602b71 Initial implementation of __atomic_is_lock_free. The input is the size of an atomic type rather than an atomic type itself just to save some implementation pain; I can change that if it seems worthwhile.
I think this is the last hook needed for <atomic> besides defines for ATOMIC_CHAR_LOCK_FREE and friends.

llvm-svn: 142281
2011-10-17 21:44:23 +00:00
Richard Smith 725810a2bb Split apart the state accumulated during constant expression evaluation and the
end result. Use this split to propagate state information and diagnostics
through more of constant expression evaluation.

llvm-svn: 142159
2011-10-16 21:26:27 +00:00
Richard Smith 4ce706afc4 Constant expression evaluation: refactor value initialization and scalar list initialization into base class.
llvm-svn: 141717
2011-10-11 21:43:33 +00:00
Eli Friedman df14b3a837 Initial implementation of __atomic_* (everything except __atomic_is_lock_free).
llvm-svn: 141632
2011-10-11 02:20:01 +00:00
Eli Friedman ce3e02a343 Extend lvalue evaluation in ExprConstant.cpp to handle CK_LValueBitCast (which is completely trivial). PR8836.
llvm-svn: 141604
2011-10-11 00:13:24 +00:00
Richard Smith caf3390d44 Constant expression evaluation refactoring:
- Remodel Expr::EvaluateAsInt to behave like the other EvaluateAs* functions,
   and add Expr::EvaluateKnownConstInt to capture the current fold-or-assert
   behaviour.
 - Factor out evaluation of bitfield bit widths.
 - Fix a few places which would evaluate an expression twice: once to determine
   whether it is a constant expression, then again to get the value.

llvm-svn: 141561
2011-10-10 18:28:20 +00:00
Eli Friedman 76d4e43063 PR11040: CheckICE should not allow an lvalue bitcast as part of an integer constant expression.
llvm-svn: 140812
2011-09-29 21:49:34 +00:00
Peter Collingbourne fec95198aa Constant evaluation for pointer CXXScalarValueInitExpr
llvm-svn: 140613
2011-09-27 17:33:05 +00:00
Sebastian Redl 12757ab4cb Treat list-initialization of scalars as a first-class citizen in C++11.
Allow empty initializer lists for scalars, which mean value-initialization.
Constant evaluation for single-element and empty initializer lists for scalars.
Codegen for empty initializer lists for scalars.
Test case comes in next commit.

llvm-svn: 140459
2011-09-24 17:48:14 +00:00
David Blaikie aa347f9392 Removing a bunch of dead returns/breaks after llvm_unreachables.
llvm-svn: 140407
2011-09-23 20:26:49 +00:00
David Blaikie 83d382b1ca Switch assert(0/false) llvm_unreachable.
llvm-svn: 140367
2011-09-23 05:06:16 +00:00
John McCall 2d637d2e79 Rename the ARC cast kinds to start with "ARC".
llvm-svn: 139466
2011-09-10 06:18:15 +00:00
John McCall cd78e805e9 When converting a block pointer to an Objective-C pointer type, extend
the lifetime of the block by copying it to the heap, or else we'll get
a dangling reference because the code working with the non-block-typed
object will not know it needs to copy.

There is some danger here, e.g. with assigning a block literal to an
unsafe variable, but, well, it's an unsafe variable.

llvm-svn: 139451
2011-09-10 01:16:55 +00:00
John McCall 9320b87cff Give conversions of block pointers to ObjC pointers a different cast kind
than conversions of C pointers to ObjC pointers.  In order to ensure that
we've caught every case, add asserts to CastExpr that strictly determine
which cast kind is used for which kind of bit cast.

llvm-svn: 139352
2011-09-09 05:25:32 +00:00
Douglas Gregor e8bbc12152 Extend the ASTContext constructor to delay the initialization of
builtin types (When requested). This is another step toward making
ASTUnit build the ASTContext as needed when loading an AST file,
rather than doing so after the fact. No actual functionality change (yet).

llvm-svn: 138985
2011-09-02 00:18:52 +00:00
Chandler Carruth 41c6dcc734 Fix a crash-on-valid that has been here for a very long time:
const int &x = x;

This crashed by inifinetly recursing within the lvalue evaluation
routine. I've added a (somewhat) braindead way of preventing this
recursion. If folks have better suggestions for how to avoid it I'm all
ears.

That said, we have some work to do. This doesn't trigger a single
warning for uninitialized, self-initialized or otherwise completely
wrong code. In some senses, the crash was almost better.

llvm-svn: 138239
2011-08-22 17:24:56 +00:00
Chad Rosier 99ee7829ff After further discussion it has been determined that alignof should report
the preferred alignment.  Thus, revert r135934, r135935, and r135940.

llvm-svn: 136062
2011-07-26 07:03:04 +00:00
Chad Rosier b23ee96cd5 Allow target to specify about using minimum alignment vs preferred. Takes care of
FIXME: Override "preferred align" for double and long long for ARM apcs-gnu ABI. 
Also part of rdar://9802874

llvm-svn: 135940
2011-07-25 19:39:39 +00:00
Chris Lattner 0e62c1cc0b remove unneeded llvm:: namespace qualifiers on some core types now that LLVM.h imports
them into the clang namespace.

llvm-svn: 135852
2011-07-23 10:55:15 +00:00
Jeffrey Yasskin d0f079dad4 Use the new APFloat::convertToInt(APSInt) function to simplify uses of
convertToInt(integerParts*) and make them more reliable.

llvm-svn: 135279
2011-07-15 17:03:07 +00:00
John McCall 7c454bb8ce Create a new expression node, SubstNonTypeTemplateParmExpr,
to represent a fully-substituted non-type template parameter.
This should improve source fidelity, as well as being generically
useful for diagnostics and such.

llvm-svn: 135243
2011-07-15 05:09:51 +00:00
John McCall 4db5c3c83a In ARC, reclaim all return values of retainable type, not just those
where we have an immediate need of a retained value.

As an exception, don't do this when the call is made as the immediate
operand of a __bridge retain.  This is more in the way of a workaround
than an actual guarantee, so it's acceptable to be brittle here.

rdar://problem/9504800

llvm-svn: 134605
2011-07-07 06:58:02 +00:00
Eli Friedman a3c122db7e Zap a couple unnecessary loops.
llvm-svn: 134578
2011-07-07 01:54:01 +00:00
Abramo Bagnara 9ae292d810 Rewritten fix in r134139 to conform evaluation result to original evaluation context.
llvm-svn: 134320
2011-07-02 13:13:53 +00:00
Abramo Bagnara 2caedf449e Fixed enum constant evaluation assertions.
llvm-svn: 134139
2011-06-30 09:36:05 +00:00
Douglas Gregor fe31481f68 Introduce a new AST node describing reference binding to temporaries.
MaterializeTemporaryExpr captures a reference binding to a temporary
value, making explicit that the temporary value (a prvalue) needs to
be materialized into memory so that its address can be used. The
intended AST invariant here is that a reference will always bind to a
glvalue, and MaterializeTemporaryExpr will be used to convert prvalues
into glvalues for that binding to happen. For example, given

  const int& r = 1.0;

The initializer of "r" will be a MaterializeTemporaryExpr whose
subexpression is an implicit conversion from the double literal "1.0"
to an integer value. 

IR generation benefits most from this new node, since it was
previously guessing (badly) when to materialize temporaries for the
purposes of reference binding. There are likely more refactoring and
cleanups we could perform there, but the introduction of
MaterializeTemporaryExpr fixes PR9565, a case where IR generation
would effectively bind a const reference directly to a bitfield in a
struct. Addresses <rdar://problem/9552231>.

llvm-svn: 133521
2011-06-21 17:03:29 +00:00
John McCall 31168b077c Automatic Reference Counting.
Language-design credit goes to a lot of people, but I particularly want
to single out Blaine Garst and Patrick Beard for their contributions.

Compiler implementation credit goes to Argyrios, Doug, Fariborz, and myself,
in no particular order.

llvm-svn: 133103
2011-06-15 23:02:42 +00:00
Tanya Lattner 55808c1026 Add support for builtin astype:
__builtin_astype(): Used to reinterpreted as another data type of the same size using for both scalar and vector data types.
Added test case.

llvm-svn: 132612
2011-06-04 00:47:47 +00:00
Douglas Gregor fcafc6e3de Implement the initial part of C++0x [expr.const]p2, which specifies
that the unevaluated subexpressions of &&, ||, and ? : are not
considered when determining whether the expression is a constant
expression. Also, turn the "used in its own initializer" warning into
a runtime-behavior warning, so that it doesn't fire when a variable is
used as part of an unevaluated subexpression of its own initializer.

Fixes PR9999.

llvm-svn: 131968
2011-05-24 16:02:01 +00:00
Douglas Gregor 6ab2fa8f78 Introduce Type::isSignedIntegerOrEnumerationType() and
Type::isUnsignedIntegerOrEnumerationType(), which are like
Type::isSignedIntegerType() and Type::isUnsignedIntegerType() but also
consider the underlying type of a C++0x scoped enumeration type.

Audited all callers to the existing functions, switching those that
need to also handle scoped enumeration types (e.g., those that deal
with constant values) over to the new functions. Fixes PR9923 /
<rdar://problem/9447851>.

llvm-svn: 131735
2011-05-20 16:38:50 +00:00
Peter Collingbourne e92006860d Refactoring of constant expression evaluator
This introduces a generic base class for the expression evaluator
classes, which handles a few common expression types which were
previously handled separately in each class.  Also, the expression
evaluator now uses ConstStmtVisitor.

llvm-svn: 131281
2011-05-13 03:29:01 +00:00
John Wiegley 6242b6a688 Implementation of Embarcadero array type traits
Patch authored by John Wiegley.

These are array type traits used for parsing code that employs certain
features of the Embarcadero C++ compiler: __array_rank(T) and
__array_extent(T, Dim).

llvm-svn: 130351
2011-04-28 00:16:57 +00:00
John Wiegley f9f6584e95 t/clang/expr-traits
Patch authored by David Abrahams.

These two expression traits (__is_lvalue_expr, __is_rvalue_expr) are used for
parsing code that employs certain features of the Embarcadero C++ compiler.

llvm-svn: 130122
2011-04-25 06:54:41 +00:00
Argyrios Kyrtzidis b2ed28ea4b For
double data[20000000] = {0};

we would blow out the memory by creating 20M Exprs to fill out the initializer.

To fix this, if the initializer list initializes an array with more elements than
there are initializers in the list, have InitListExpr store a single 'ArrayFiller' expression
that specifies an expression to be used for value initialization of the rest of the elements.

Fixes rdar://9275920.

llvm-svn: 129896
2011-04-21 00:27:41 +00:00
Tanya Lattner 5ac257d738 Fix bug in vector initializer when initializing a vector with another vector.
Add test case.

llvm-svn: 129617
2011-04-15 22:42:59 +00:00
Peter Collingbourne 9114759641 C1X: implement generic selections
As an extension, generic selection support has been added for all
supported languages.  The syntax is the same as for C1X.

llvm-svn: 129554
2011-04-15 00:35:48 +00:00
John McCall 2979fe01da After some discussion with Doug, we decided that it made a lot more sense
for __unknown_anytype resolution to destructively modify the AST.  So that's
what it does now, which significantly simplifies some of the implementation.
Normal member calls work pretty cleanly now, and I added support for
propagating unknown-ness through &.

llvm-svn: 129331
2011-04-12 00:42:48 +00:00
John McCall 2d2e870745 More __unknown_anytype work.
llvm-svn: 129269
2011-04-11 07:02:50 +00:00
Anders Carlsson 8a01a751c9 Remove CK_DynamicToNull.
llvm-svn: 129265
2011-04-11 02:03:26 +00:00
Anders Carlsson c602006638 As a first step towards fixing PR9641, add a CK_DynamicToNull cast kind which
represents a dynamic cast where we know that the result is always null.

For example:

struct A {
  virtual ~A();
};
struct B final : A { };
struct C { };

bool f(B* b) {
  return dynamic_cast<C*>(b);
}

llvm-svn: 129256
2011-04-10 20:33:22 +00:00
John McCall 319963434c Basic, untested implementation for an "unknown any" type requested by LLDB.
The idea is that you can create a VarDecl with an unknown type, or a
FunctionDecl with an unknown return type, and it will still be valid to
access that object as long as you explicitly cast it at every use.  I'm
still going back and forth about how I want to test this effectively, but
I wanted to go ahead and provide a skeletal implementation for the LLDB
folks' benefit and because it also improves some diagnostic goodness for
placeholder expressions.

llvm-svn: 129065
2011-04-07 08:22:57 +00:00
Eli Friedman 9faf2f902c Silly mistake in integer constant folding cleanup.
llvm-svn: 128297
2011-03-25 19:07:11 +00:00
Anders Carlsson 6fc2204dde Fix some clang warnings.
llvm-svn: 128272
2011-03-25 11:22:47 +00:00
Eli Friedman 8bfbe3a01a Switch constant evaluation of float casts over to cast kinds.
llvm-svn: 128251
2011-03-25 00:54:52 +00:00
Eli Friedman c757de2326 Cleanup integral and vector constant evaluation of casts to use cast kinds.
llvm-svn: 128250
2011-03-25 00:43:55 +00:00
Douglas Gregor 20b2ebd785 Implement a new 'availability' attribute, that allows one to specify
which versions of an OS provide a certain facility. For example,

  void foo()
  __attribute__((availability(macosx,introduced=10.2,deprecated=10.4,obsoleted=10.6)));

says that the function "foo" was introduced in 10.2, deprecated in
10.4, and completely obsoleted in 10.6. This attribute ties in with
the deployment targets (e.g., -mmacosx-version-min=10.1 specifies that
we want to deploy back to Mac OS X 10.1). There are several concrete
behaviors that this attribute enables, as illustrated with the
function foo() above:

  - If we choose a deployment target >= Mac OS X 10.4, uses of "foo"
    will result in a deprecation warning, as if we had placed
    attribute((deprecated)) on it (but with a better diagnostic)
  - If we choose a deployment target >= Mac OS X 10.6, uses of "foo"
    will result in an "unavailable" warning (in C)/error (in C++), as
    if we had placed attribute((unavailable)) on it
  - If we choose a deployment target prior to 10.2, foo() is
    weak-imported (if it is a kind of entity that can be weak
    imported), as if we had placed the weak_import attribute on it.

Naturally, there can be multiple availability attributes on a
declaration, for different platforms; only the current platform
matters when checking availability attributes.

The only platforms this attribute currently works for are "ios" and
"macosx", since we already have -mxxxx-version-min flags for them and we
have experience there with macro tricks translating down to the
deprecated/unavailable/weak_import attributes. The end goal is to open
this up to other platforms, and even extension to other "platforms"
that are really libraries (say, through a #pragma clang
define_system), but that hasn't yet been designed and we may want to
shake out more issues with this narrower problem first.

Addresses <rdar://problem/6690412>.

As a drive-by bug-fix, if an entity is both deprecated and
unavailable, we only emit the "unavailable" diagnostic.

llvm-svn: 128127
2011-03-23 00:50:03 +00:00
Peter Collingbourne e190dee7a5 Add support for the OpenCL vec_step operator, by generalising and
extending the existing support for sizeof and alignof.  Original
patch by Guy Benyei.

llvm-svn: 127475
2011-03-11 19:24:49 +00:00
Ken Dyck dbc0191181 Overload IntExprEvaluator::Success() with a function that takes a CharUnits
parameter to tidy up the places where the expression is a size.

llvm-svn: 127454
2011-03-11 02:13:43 +00:00
Benjamin Kramer 024e619f2a Move private structs into anonymous namespaces.
llvm-svn: 126997
2011-03-04 13:12:48 +00:00
John McCall 4b13633082 Don't crash during constant-evaluation of 1/(1/0). PR9262.
llvm-svn: 126541
2011-02-26 08:27:17 +00:00
John McCall c07a0c7e48 Change the representation of GNU ?: expressions to use a different expression
class and to bind the shared value using OpaqueValueExpr.  This fixes an
unnoticed problem with deserialization of these expressions where the
deserialized form would lose the vital pointer-equality trait;  or rather,
it fixes it because this patch also does the right thing for deserializing
OVEs.

Change OVEs to not be a "temporary object" in the sense that copy elision is
permitted.

This new representation is not totally unawkward to work with, but I think
that's really part and parcel with the semantics we're modelling here.  In
particular, it's much easier to fix things like the copy elision bug and to
make the CFG look right.

I've tried to update the analyzer to deal with this in at least some          
obvious cases, and I think we get a much better CFG out, but the printing
of OpaqueValueExprs probably needs some work.

llvm-svn: 125744
2011-02-17 10:25:35 +00:00
Peter Collingbourne 41f8546233 AST, Sema, Serialization: add CUDAKernelCallExpr and related semantic actions
llvm-svn: 125217
2011-02-09 21:07:24 +00:00
John McCall bd06678921 Remove vtables from the Stmt hierarchy; this was pretty easy as
there were only three virtual methods of any significance.

The primary way to grab child iterators now is with
  Stmt::child_range children();
  Stmt::const_child_range children() const;
where a child_range is just a std::pair of iterators suitable for
being llvm::tie'd to some locals.  I've left the old child_begin()
and child_end() accessors in place, but it's probably a substantial
penalty to grab the iterators individually now, since the
switch-based dispatch is kindof inherently slower than vtable
dispatch.  Grabbing them together is probably a slight win over the
status quo, although of course we could've achieved that with vtables, too.

I also reclassified SwitchCase (correctly) as an abstract Stmt
class, which (as the first such class that wasn't an Expr subclass)
required some fiddling in a few places.

There are somewhat gross metaprogramming hooks in place to ensure
that new statements/expressions continue to implement
getSourceRange() and children().  I had to work around a recent clang
bug;  dgregor actually fixed it already, but I didn't want to
introduce a selfhosting dependency on ToT.

llvm-svn: 125183
2011-02-09 08:16:59 +00:00
John McCall c63de66c4f An insomniac stab at making block declarations list the variables they close
on, as well as more reliably limiting invalid references to locals from
nested scopes.

llvm-svn: 124721
2011-02-02 13:00:07 +00:00
Ken Dyck 02155cb8e8 Use RecordLayout::getBaseClassOffset() where CharUnits are needed instead of
converting getBaseClassOffsetInBits() to CharUnits.

llvm-svn: 124274
2011-01-26 02:17:08 +00:00
John McCall 4e81961a1e Fix the computation of alignment for fields of packed+aligned structs.
Part of the fix for PR8413.

llvm-svn: 123904
2011-01-20 07:57:12 +00:00
Ken Dyck 86a7fcc19c Replace calls to CharUnits::fromQuantity() with ones
ASTContext::toCharUnitsFromBits() when converting from bit sizes to char units.

llvm-svn: 123715
2011-01-18 01:56:16 +00:00
Douglas Gregor cdbc539aee Introduce a new expression kind, SubstNonTypeTemplateParmPackExpr,
that captures the substitution of a non-type template argument pack
for a non-type template parameter pack within a pack expansion that
cannot be fully expanded. This follows the approach taken by
SubstTemplateTypeParmPackType.

llvm-svn: 123506
2011-01-15 01:15:58 +00:00
Ken Dyck e9f3517f13 Replace a literal '8' with getCharWidth().
llvm-svn: 123421
2011-01-14 02:01:36 +00:00
Jay Foad 39c7980772 PR3558: mark "logically const" accessor methods in ASTContext as const,
and mark the fields they use as mutable. This allows us to remove a few
const_casts.

llvm-svn: 123314
2011-01-12 09:06:06 +00:00
Douglas Gregor 820ba7ba43 Implement the sizeof...(pack) expression to compute the length of a
parameter pack.

Note that we're missing proper libclang support for the new
SizeOfPackExpr expression node.

llvm-svn: 122813
2011-01-04 17:33:58 +00:00
Douglas Gregor e8e9dd624c Implement support for pack expansions whose pattern is a non-type
template argument (described by an expression, of course). For
example:

  template<int...> struct int_tuple { };

  template<int ...Values>
  struct square {
    typedef int_tuple<(Values*Values)...> type;
  };

It also lays the foundation for pack expansions in an initializer-list.
  

llvm-svn: 122751
2011-01-03 17:17:50 +00:00
Jeffrey Yasskin b3321531a8 Change all self assignments X=X to (void)X, so that we can turn on a
new gcc warning that complains on self-assignments and
self-initializations.  Fix one bug found by the warning, in which one
clang::OverloadCandidate constructor failed to initialize its
FunctionTemplate member.

llvm-svn: 122459
2010-12-23 01:01:28 +00:00
John McCall fcef3cf510 Rewrite ComplexExprEvaluator::VisitCastExpr to use cast kinds, and fix
the basic casting logic to insert intermediate casts and preserve the
exact complex-cast design.  Fixes a crash in the test suite.

llvm-svn: 121776
2010-12-14 17:51:41 +00:00
Abramo Bagnara 9e0e7096a3 Improved complex constants evaluation.
llvm-svn: 121616
2010-12-11 16:05:48 +00:00
Francois Pichet 34b2113250 Remove the TypesCompatibleExprClass AST node. Merge its functionality into BinaryTypeTraitExpr.
llvm-svn: 121298
2010-12-08 22:35:30 +00:00
Jay Foad 6d4db0c885 PR5207: Change APInt methods trunc(), sext(), zext(), sextOrTrunc() and
zextOrTrunc(), and APSInt methods extend(), extOrTrunc() and new method
trunc(), to be const and to return a new value instead of modifying the
object in place.

llvm-svn: 121121
2010-12-07 08:25:34 +00:00
Francois Pichet 9dfa3ce94f Type traits intrinsic implementation: __is_base_of(T, U)
New AST node introduced: BinaryTypeTraitExpr; to be reused for more intrinsics.

llvm-svn: 121074
2010-12-07 00:08:36 +00:00
John McCall 5d41378146 Rename CXXExprWithTemporaries -> ExprWithCleanups; there's no theoretical
reason this is limited to C++, and it's certainly not limited to temporaries.

llvm-svn: 120996
2010-12-06 08:20:24 +00:00
John McCall b7bd14fa08 Simplify the ASTs by consolidating ObjCImplicitGetterSetterExpr and ObjCPropertyRefExpr
into the latter.

llvm-svn: 120643
2010-12-02 01:19:52 +00:00
John McCall d50a27111c Kill CK_Unknown and flesh out the documentation for the existing CastKinds.
llvm-svn: 119331
2010-11-16 05:46:29 +00:00
John McCall 8d69a2160e Add a new expression kind, OpaqueValueExpr, which is useful for
certain internal type-checking procedures as well as for representing
certain implicitly-generated operations.  Uses to follow.

llvm-svn: 119289
2010-11-15 23:31:06 +00:00
John McCall d764625448 Add a few more complex-related cast kinds that arise due to arbitrary
implicit conversions;  the last batch was specific to promotions.
I think this is the full set we need.  I do think dividing the cast
kinds into floating and integral is probably a good idea.

Annotate a *lot* more C casts with useful cast kinds.

llvm-svn: 119036
2010-11-14 08:17:51 +00:00
John McCall c5e62b47af Introduce five new cast kinds for various conversions into and
between complex types.

llvm-svn: 118994
2010-11-13 09:02:35 +00:00
John McCall e84af4e486 Introduce a null-to-pointer implicit cast kind.
llvm-svn: 118966
2010-11-13 01:35:44 +00:00
John McCall 18a2c2c0b9 Split out -Wconversion warnings about constant precision into their
own subcategory, -Wconstant-conversion, which is on by default.

Tweak the constant folder to give better results in the invalid
case of a negative shift amount.

Implements rdar://problem/6792488

llvm-svn: 118636
2010-11-09 22:22:12 +00:00
Anders Carlsson fd88a6160d Rename getBaseClassOffset to getBaseClassOffsetInBits and introduce a getBaseClassOffset which returns the offset in CharUnits. Do the same thing for getVBaseClassOffset.
llvm-svn: 117881
2010-10-31 23:22:37 +00:00
Anders Carlsson 1827509176 Teach the constant expr evaluator about derived-to-base casts when no virtual bases are involved. Fixes PR5974.
llvm-svn: 117868
2010-10-31 20:41:46 +00:00
Anders Carlsson a5df61a341 Don't try to evaluate the LHS or RHS of a member pointer binary operation. Fixes PR8507.
llvm-svn: 117850
2010-10-31 01:21:47 +00:00
Fariborz Jahanian 681c0754d9 Eliminate usage of ObjCSuperExpr used for
'super' as receiver of property or a setter/getter
methods. //rdar: //8525788

llvm-svn: 116483
2010-10-14 16:04:05 +00:00
John McCall a2fabff4f6 Permit constant evaluation of const floating-point variables with
constant initializers.

llvm-svn: 116138
2010-10-09 01:34:31 +00:00
Sebastian Redl 8eb06f17c4 Eagerly evaluate type traits in Sema instead of lazily in AST. They actually need Sema access to be correct, fixes coming up.
llvm-svn: 113782
2010-09-13 20:56:31 +00:00
Sebastian Redl 5f0180d815 First version of a testcase, plus fixes.
llvm-svn: 113624
2010-09-10 20:55:47 +00:00
Sebastian Redl 4202c0f2a9 Define and implement CXXNoexceptExpr. Create it in Sema.
llvm-svn: 113623
2010-09-10 20:55:43 +00:00
Douglas Gregor 6a6dac2fbf Support strlen() and __builtin_strlen() as constant expressions with
the call argument is a string literal. Fixes
<rdar://problem/8413477>.

llvm-svn: 113580
2010-09-10 06:27:15 +00:00
Francois Pichet 5cc0a67f08 Fix warnings caused by new CXXUuidofExprClass enumerator.
llvm-svn: 113444
2010-09-08 23:47:05 +00:00
Eli Friedman 08e3cdcffa Get rid of unnecessary return.
llvm-svn: 113132
2010-09-06 00:31:37 +00:00
Eli Friedman 0b1fbd1394 PR7242: Make sure to use a different context for evaluating constant
initializers, so the result of the evaluation doesn't leak through
inconsistently.  Also, don't evaluate references to variables with
initializers with side-effects.

llvm-svn: 113128
2010-09-06 00:10:32 +00:00
Douglas Gregor 6429f5cf29 Eliminate CXXBindReferenceExpr, which was used in a ton of
well-intentioned but completely unused code.

llvm-svn: 112868
2010-09-02 21:50:02 +00:00
Alexis Hunt 3b7918625c Revert my user-defined literal commits - r1124{58,60,67} pending
some issues being sorted out.

llvm-svn: 112493
2010-08-30 17:47:05 +00:00
Alexis Hunt 79eb5469e0 Implement C++0x user-defined string literals.
The extra data stored on user-defined literal Tokens is stored in extra
allocated memory, which is managed by the PreprocessorLexer because there isn't
a better place to put it that makes sure it gets deallocated, but only after
it's used up. My testing has shown no significant slowdown as a result, but
independent testing would be appreciated.

llvm-svn: 112458
2010-08-29 21:26:48 +00:00
John McCall e302792b61 GCC didn't care for my attempt at API compatibility, so brute-force everything
to the new constants.

llvm-svn: 112047
2010-08-25 11:45:40 +00:00
Eli Friedman c3e9df3cc0 Move a bunch of code out of the ComplexExprEvaluator class definition;
no functionality change.

llvm-svn: 111207
2010-08-16 23:27:44 +00:00
Eli Friedman 9571953f36 PR7884: Fix the implementations of __real__ and __imag__ on real floats.
llvm-svn: 111080
2010-08-14 20:52:13 +00:00
Eli Friedman a2622dd266 Zap unused UnaryOperator::OffsetOf.
llvm-svn: 110996
2010-08-13 01:36:11 +00:00
Eli Friedman a170cd6257 Get rid of isObjectType; when C++ says "object type", it generally
just means "not a function type", not "not a function type or void". This
changes behavior slightly, but generally in a way which accepts more code.

llvm-svn: 110303
2010-08-05 02:49:48 +00:00
Dan Gohman 28ade550f4 Fix namespace polution.
llvm-svn: 109440
2010-07-26 21:25:24 +00:00
Douglas Gregor 51954276cc Introduce a new cast kind for an "lvalue bitcast", which handles
reinterpret_casts (possibly indirectly via C-style/functional casts)
on values, e.g., 

  int i;
  reinterpret_cast<short&>(i);

The IR generated for this is essentially the same as for

  *reinterpret_cast<short*>(&i).

Fixes PR6437, PR7593, and PR7344. 

llvm-svn: 108294
2010-07-13 23:17:26 +00:00
Douglas Gregor 747eb7840a Reinstate the fix for PR7556. A silly use of isTrivial() was
suppressing copies of objects with trivial copy constructors.

llvm-svn: 107857
2010-07-08 06:14:04 +00:00
Douglas Gregor e182370eda Revert r107828 and r107827, the fix for PR7556, which seems to be
breaking bootstrap on Linux.

llvm-svn: 107837
2010-07-07 23:37:33 +00:00
Douglas Gregor 6df2b8c3ac Rename CXXZeroInitValueExpr to CXXScalarValueInitExpr, to reflect its
newly-narrowed scope. No functionality change.

llvm-svn: 107828
2010-07-07 22:43:56 +00:00
John McCall 11086fcb65 Don't consider casted non-global pointers to be evaluatable.
Fixes rdar://problem/8154689

llvm-svn: 107755
2010-07-07 05:08:32 +00:00
Douglas Gregor b90df60b3b Introduce Type::isIntegralOrEnumerationType(), to cover those places
in C++ that involve both integral and enumeration types. Convert all
of the callers to Type::isIntegralType() that are meant to work with
both integral and enumeration types over to
Type::isIntegralOrEnumerationType(), to prepare to eliminate
enumeration types as integral types.

llvm-svn: 106071
2010-06-16 00:17:44 +00:00
John McCall 875679eea0 Fix the constant evaluator for AltiVec-style vector literals so that the
vector is filled with the given constant;  we were just initializing the
first element.

llvm-svn: 105824
2010-06-11 17:54:15 +00:00
Chandler Carruth e299ba66f5 When constant folding reference variables with an initializer to the
initializer, don't fold paramters. Their initializers are just default
arguments which can be overridden. This fixes some spectacular regressions due
to more things making it into the constant folding.

llvm-svn: 103904
2010-05-16 09:32:51 +00:00
Abramo Bagnara f819945365 Added Expr::EvaluateAsAnyLValue.
llvm-svn: 103780
2010-05-14 17:07:14 +00:00
John McCall 9500760829 Refactor the constant-evaluator so that it only supports a single form
of constant-evaluation.  Formerly you could control whether it accepted
local l-values or not;  now it always evaluates local l-values in the core
routines, but filters them out where consumed by the top-level routines.
This will make it much easier to cache evaluability.

llvm-svn: 103444
2010-05-10 23:27:23 +00:00
John McCall b1fb0d3610 The FP constant evaluator was missing a few cases of unary operators that return floats
but whose operand isn't a float:  specifically, __real__ and __imag__.  Instead
of filtering these out, just implement them.

Fixes <rdar://problem/7958272>.

llvm-svn: 103307
2010-05-07 22:08:54 +00:00
John McCall eb3e4f3481 Make that null-dereference fix a little clearer by rearranging some code.
llvm-svn: 103298
2010-05-07 21:34:32 +00:00
John McCall 06f0e09756 Fix a potential null dereference in the pointer-to-bool evaluator; caught by
Eli Friedman.

llvm-svn: 103297
2010-05-07 21:01:17 +00:00
John McCall 45d55e4f26 Change the pointer / lvalue constant evaluators to build into a target reference
and return a bool.

llvm-svn: 103296
2010-05-07 21:00:08 +00:00
John McCall 93d91dcdb7 Change the complex constant evaluator to return a bool instead of an APValue.
llvm-svn: 103268
2010-05-07 17:22:02 +00:00
Rafael Espindola a1f9cc1bec Fix PR4386 by implementing gcc's old behaviour (4.2) when initializing
variables with a comparison of a function pointer with 0.

llvm-svn: 103253
2010-05-07 15:18:43 +00:00
John McCall f0c4f356b8 Change Evaluate* in the constant evaluator to enforce being given an argument of
the right type.  It turns out that the code was already doing this.

llvm-svn: 103238
2010-05-07 05:46:35 +00:00
John McCall 864e396d0b Move CheckICE and isIntegerConstantExpr to ExprConstant.cpp because it seemed
like a good idea at the time.

llvm-svn: 103237
2010-05-07 05:32:02 +00:00
Mon P Wang 75c645c6d7 A not equal for an unordered relation should return true as specified in IEEE-754, e.g.,
NAN != NAN ? 1 : 0 should return 1.  Also fix the case for complex.

llvm-svn: 102598
2010-04-29 05:53:29 +00:00
Douglas Gregor d170206761 Teach __builtin_offsetof to compute the offsets of members of base
classes, since we only warn (not error) on offsetof() for non-POD
types. We store the base path within the OffsetOfExpr itself, then
evaluate the offsets within the constant evaluator.

llvm-svn: 102571
2010-04-29 00:18:15 +00:00
Douglas Gregor 882211c1da Completely reimplement __builtin_offsetof, based on a patch by Roberto
Amadini.

This change introduces a new expression node type, OffsetOfExpr, that
describes __builtin_offsetof. Previously, __builtin_offsetof was
implemented using a unary operator whose subexpression involved
various synthesized array-subscript and member-reference expressions,
which was ugly and made it very hard to instantiate as a
template. OffsetOfExpr represents the AST more faithfully, with proper
type source information and a more compact representation.

OffsetOfExpr also has support for dependent __builtin_offsetof
expressions; it can be value-dependent, but will never be
type-dependent (like sizeof or alignof). This commit introduces
template instantiation for __builtin_offsetof as well.

There are two major caveats to this patch:

  1) CodeGen cannot handle the case where __builtin_offsetof is not a
  constant expression, so it produces an error. So, to avoid
  regressing in C, we retain the old UnaryOperator-based
  __builtin_offsetof implementation in C while using the shiny new
  OffsetOfExpr implementation in C++. The old implementation can go
  away once we have proper CodeGen support for this case, which we
  expect won't cause much trouble in C++.

  2) __builtin_offsetof doesn't work well with non-POD class types,
  particularly when the designated field is found within a base
  class. I will address this in a subsequent patch.

Fixes PR5880 and a bunch of assertions when building Boost.Python
tests. 

llvm-svn: 102542
2010-04-28 22:16:22 +00:00
Douglas Gregor 8385a06929 Introduce Type::isStructureOrClassType(), which does the obvious
thing. Audit all uses of Type::isStructure(), changing those calls to
isStructureOrClassType() as needed (which is alsmost
everywhere). Fixes the remaining failure in Boost.Utility/Swap.

llvm-svn: 102386
2010-04-26 21:31:17 +00:00
Chris Lattner 882bdf2a8d remove some extraneous qualifiers.
llvm-svn: 101912
2010-04-20 17:13:14 +00:00
Chris Lattner a06794201d Teach HasSideEffect about InitListExprs. Not having
this caused us to codegen dead globals like this:

struct foo { int a; int b; };

static struct foo fooarray[] = {
  {1, 2},
  {4},
};

llvm-svn: 101150
2010-04-13 17:34:23 +00:00
Daniel Dunbar 4c43e31d21 Evaluate: Fix a subtle bug in the pointer evaluator in which we would do an
expression computation in the wrong bit-width, and end up generating a totally
bogus array reference (_g0+8589934546).
 - This showed up on Prolangs/cdecl.

llvm-svn: 99042
2010-03-20 05:53:45 +00:00
John McCall 1629149103 Support constant-evaluation of __builtin_nans* as well as the correct constant
evaluation of __builtin_nan*.  Most of the work to make this work is in LLVM.

Fixes <rdar://problem/7696712> and part of PR 5255.

llvm-svn: 97383
2010-02-28 13:00:19 +00:00
Eli Friedman d5c9399696 Fix for PR6274: teach constant folding to evaluate __builtin_expect.
llvm-svn: 96054
2010-02-13 00:10:10 +00:00
Anders Carlsson b0695ef011 Don't try to fold DeclRefExprs that point to ParmVarDecls. This had the side-effect of always folding the expression to the default argument of the parameter. For example:
void f(int a = 10) {
  return a;
}

would always return 10, regardless of the passed in argument.

This fixes another 600 test failures. We're now down to only 137 failures!

llvm-svn: 95262
2010-02-03 21:58:41 +00:00
Sebastian Redl 5ca7984bb4 In C++, an initializer on a variable doesn't necessarily mean it's the definition. With that in mind, rename getDefinition to getAnyInitializer (to distinguish it from getInit) and reimplement it in terms of isThisDeclarationADefinition. Update all code to use this new function.
llvm-svn: 94999
2010-02-01 20:16:42 +00:00
Ken Dyck 160146eba2 Change the return type of ASTContext::getDeclAlignInBytes() to CharUnits and,
now that the "InBytes" part of the name is implied by the return type, rename
it to getDeclAlign().

llvm-svn: 94681
2010-01-27 17:10:57 +00:00
David Chisnall 481e3a87fe Created __builtin___NSStringMakeConstantString() builtin, which generates constant Objective-C strings.
llvm-svn: 94274
2010-01-23 02:40:42 +00:00
Eric Christopher 99469702a3 Update and move around comments.
llvm-svn: 93942
2010-01-19 22:58:35 +00:00
Ken Dyck 02990837ad Convert the type of the LValue offset variable in APValue to CharUnits, moving
the LValue-related methods of APValue out of line to avoid header file leaching.

llvm-svn: 93512
2010-01-15 12:37:54 +00:00
Ken Dyck 40775003e6 Roll out ASTContext::getTypeSizeInChars(), replacing instances of
"ASTContext::getTypeSize() / 8". Replace [u]int64_t variables with CharUnits
ones as appropriate. 

Also rename RawType, fromRaw(), and getRaw() in CharUnits to QuantityType, 
fromQuantity(), and getQuantity() for clarity.

llvm-svn: 93153
2010-01-11 17:06:35 +00:00
John McCall 1be1c63634 Add Expr::EvaluateAsBooleanCondition(), which does unprincipled folding to
try to evaluate an expression as a constant boolean condition.  This has
the same intended semantics as used in folding conditional operators.

llvm-svn: 92805
2010-01-05 23:42:56 +00:00
Benjamin Kramer 0128f668a9 __builtin_object_size(ptr, type) returns -1 for type = {0,1} if there are any side-effects.
llvm-svn: 92453
2010-01-03 18:18:37 +00:00
Eli Friedman 847a2bce04 Fix PointerExprEvaluator::VisitCastExpr so it doesn't misfold C++ casts which
it doesn't know how to fold, like derived-to-base casts.

llvm-svn: 92173
2009-12-27 05:43:15 +00:00
Benjamin Kramer d20ef75b91 Remove some dead variables clang-analyzer found.
llvm-svn: 92162
2009-12-25 15:43:36 +00:00
Eric Christopher c87915629b Update for the intrinsic changes in llvm: the object size intrinsic
only takes a boolean second argument now. Update tests accordingly.
Currently the builtin still accepts the full range for compatibility.

llvm-svn: 91983
2009-12-23 03:49:37 +00:00
Benjamin Kramer 8a4ad4adc1 Use StringRef.getAsInteger instead of temporary string + strtol. No intended functionality change.
llvm-svn: 91118
2009-12-11 13:26:32 +00:00
Eli Friedman ee275c82ff Clean up enum constants so that they're finally sane. Fixes PR3173 and a
recently introduced crash.

llvm-svn: 91070
2009-12-10 22:29:29 +00:00
Eli Friedman f3da334da6 Fix for PR5447: teach Evaluate to deal with floating-point conditionals.
llvm-svn: 90521
2009-12-04 02:12:53 +00:00
Eli Friedman 1d6fb1669c Add recursion guards to ice-checking and evaluation for declrefs, so we
don't infinitely recurse for cases we can't evaluate.

llvm-svn: 90480
2009-12-03 20:31:57 +00:00
Benjamin Kramer 26222b666c Remove VISIBILITY_HIDDEN from lib/AST.
llvm-svn: 90043
2009-11-28 19:03:38 +00:00
Eli Friedman fb8a93fe1d Teach Evaluate to handle member expressions referring to enum constants and
static member constants. No significant visible difference at the moment
because it conservatively assumes the base has side effects. I'm planning to
use this for CodeGen.

llvm-svn: 89738
2009-11-24 05:28:59 +00:00
Sebastian Redl 22e2e5c423 Intercept sizeof and alignof references before they get into ASTContext methods. This fixes a crash when writing sizeof(Incomplete&), and lets ASTContext's methods do the right thing for CodeGen, which fixes PR5590.
llvm-svn: 89668
2009-11-23 17:18:46 +00:00
Eli Friedman 141fbf3f36 Add constant evaluation for comma operator with floating-point operand. Fixes
PR5449.

llvm-svn: 88885
2009-11-16 04:25:37 +00:00
Fariborz Jahanian 4127b8ef97 Added support for static variables which require
initialization before main. Fixes pr5396.

llvm-svn: 86145
2009-11-05 18:03:03 +00:00
Mike Stump 53f9ded62b Refine volatile handling, specifically, we must have the canonical
type to look at the volatile specifier.  I found these all from just
hand auditing the code.

llvm-svn: 85967
2009-11-03 23:25:48 +00:00
Chris Lattner 4f10559ba8 silence a warning.
llvm-svn: 85931
2009-11-03 19:48:51 +00:00
Douglas Gregor 0840cc02ce When determining whether a reference to a static data member is an
integral constant expression, make sure to find where the initializer
was provided---inside or outside the class definition---since that can
affect whether we have an integral constant expression (and, we need
to see the initializer itself).

llvm-svn: 85741
2009-11-01 20:32:48 +00:00
Mike Stump f3eb5ec2c0 Fix one more bug with __builtin_object_size.
llvm-svn: 85538
2009-10-29 23:34:20 +00:00
Mike Stump fa50290b49 Fix some issues Daniel pointed out.
llvm-svn: 85526
2009-10-29 20:48:09 +00:00
Chris Lattner edc56ef41c optimize out some ifdefs.
llvm-svn: 85453
2009-10-29 00:22:00 +00:00
Chris Lattner 6c4d255bf3 Implement clang support for indirect branch and address of label
using the new LLVM support for this.  This is temporarily hiding
behind horrible and ugly #ifdefs until the time when the optimizer
is stable (hopefully a week or so).  Until then, lets make it "opt in" :)

llvm-svn: 85446
2009-10-28 23:59:40 +00:00
Mike Stump 5179f308a2 Refine __builtin_object_size. Don't try and get a size for things
that don't have sizes.

llvm-svn: 85435
2009-10-28 21:22:24 +00:00
Mike Stump 876387ba4f __builtin_object_size refinements. Ensure we handle expressions with
side-effects up front, as when we switch to the llvm intrinsic call
for __builtin_object_size later, it will have two evaluations.

We also finish off the intrinsic version of the code so we can just
turn it on once llvm has the intrinsic.

llvm-svn: 85324
2009-10-27 22:09:17 +00:00
Mike Stump 5183a14bfb __builtin_object_size refinements. Also handle stack based objects. WIP.
llvm-svn: 85174
2009-10-26 23:05:19 +00:00
Mike Stump 10bd7e1c5b __builtin_object_size refinements. When we run out of object, be sure
to clamp at 0 bytes left.  WIP.

llvm-svn: 85157
2009-10-26 21:38:39 +00:00
Mike Stump 99f11f769e Be sure to zero-extend. And refactor.
llvm-svn: 85140
2009-10-26 18:57:47 +00:00
Mike Stump 722cedfb0d __builtin_object_size refinements. WIP.
llvm-svn: 85136
2009-10-26 18:35:08 +00:00
Anders Carlsson de55f647ff Ignore No-op casts when evaluating lvalue expressions. Fixes PR5122.
llvm-svn: 83267
2009-10-03 16:30:22 +00:00
John McCall 8ccfcb51ee Refactor the representation of qualifiers to bring ExtQualType out of the
Type hierarchy.  Demote 'volatile' to extended-qualifier status.  Audit our
use of qualifiers and fix a few places that weren't dealing with qualifiers
quite right;  many more remain.

llvm-svn: 82705
2009-09-24 19:53:00 +00:00
Chris Lattner d545ad1301 implement support for __builtin_eh_return_data_regno on x86-32 and x86-64.
This implements PR5034 and rdar://6836445.

llvm-svn: 82614
2009-09-23 06:06:36 +00:00
John McCall 9dd450bb78 Change all the Type::getAsFoo() methods to specializations of Type::getAs().
Several of the existing methods were identical to their respective
specializations, and so have been removed entirely.  Several more 'leaf'
optimizations were introduced.

The getAsFoo() methods which imposed extra conditions, like
getAsObjCInterfacePointerType(), have been left in place.

llvm-svn: 82501
2009-09-21 23:43:11 +00:00
Anders Carlsson 04c3bf4fab Codegen support for nullptr from C++0x.
llvm-svn: 81835
2009-09-15 04:39:46 +00:00
Eli Friedman 7d45c4884a Add utility to evaluate lvalues which are an offset relative to a stack
location.  Patch by Enea Zaffanella.

llvm-svn: 81672
2009-09-13 10:17:44 +00:00
Mike Stump 11289f4280 Remove tabs, and whitespace cleanups.
llvm-svn: 81346
2009-09-09 15:08:12 +00:00
Eli Friedman 9ab0319b2b Get rid of mostly-unused, buggy method.
llvm-svn: 80432
2009-08-29 19:09:59 +00:00
Ted Kremenek c23c7e6a51 Change uses of:
Type::getAsReferenceType() -> Type::getAs<ReferenceType>()
  Type::getAsRecordType() -> Type::getAs<RecordType>()
  Type::getAsPointerType() -> Type::getAs<PointerType>()
  Type::getAsBlockPointerType() -> Type::getAs<BlockPointerType>()
  Type::getAsLValueReferenceType() -> Type::getAs<LValueReferenceType>()
  Type::getAsRValueReferenceType() -> Type::getAs<RValueReferenceType>()
  Type::getAsMemberPointerType() -> Type::getAs<MemberPointerType>()
  Type::getAsReferenceType() -> Type::getAs<ReferenceType>()
  Type::getAsTagType() -> Type::getAs<TagType>()
  
And remove Type::getAsReferenceType(), etc.

This change is similar to one I made a couple weeks ago, but that was partly
reverted pending some additional design discussion. With Doug's pending smart
pointer changes for Types, it seemed natural to take this approach.

llvm-svn: 77510
2009-07-29 21:53:49 +00:00
Douglas Gregor 79f83eda84 This patch fixes the implementations of the __has_trivial_destructor
and __has_trivial_constructor builtin pseudo-functions and
additionally implements __has_trivial_copy and __has_trivial_assign,
from John McCall!

llvm-svn: 76916
2009-07-23 23:49:00 +00:00
Anders Carlsson 15b73deeea Revert r75641.
llvm-svn: 76327
2009-07-18 19:43:29 +00:00
Ted Kremenek 8a286fbdb9 Per offline discussion with Steve Naroff, add back Type::getAsXXXType() methods
until Doug Gregor's Type smart pointer code lands (or more discussion occurs).
These methods just call the new Type::getAs<XXX> methods, so we still have
reduced implementation redundancy. Having explicit getAsXXXType() methods makes
it easier to set breakpoints in the debugger.

llvm-svn: 76193
2009-07-17 17:50:17 +00:00
Ted Kremenek b825c0ddc5 Replaced Type::getAsLValueReferenceType(), Type::getAsRValueReferenceType(), Type::getAsMemberPointerType(), Type::getAsTagType(), and Type::getAsRecordType() with their Type::getAs<XXX> equivalents.
llvm-svn: 76139
2009-07-17 01:20:38 +00:00
Ted Kremenek e3fb4b6524 Add member template 'Type::getAs<T>', which converts a Type* to a respective T*.
This method is intended to eventually replace the individual
Type::getAsXXXType<> methods.

The motivation behind this change is twofold:

1) Reduce redundant implementations of Type::getAsXXXType() methods. Most of
them are basically copy-and-paste.

2) By centralizing the implementation of the getAs<Type> logic we can more
smoothly move over to Doug Gregor's proposed canonical type smart pointer
scheme.

Along with this patch:

a) Removed 'Type::getAsPointerType()'; now clients use getAs<PointerType>.
b) Removed 'Type::getAsBlockPointerTypE()'; now clients use getAs<BlockPointerType>.

llvm-svn: 76098
2009-07-16 19:58:26 +00:00
Anders Carlsson 466fd0a16d Rename RecordLayout.h to ASTRecordLayout.h
llvm-svn: 75641
2009-07-14 17:29:11 +00:00
Steve Naroff 7cae42b07a This patch includes a conceptually simple, but very intrusive/pervasive change.
The idea is to segregate Objective-C "object" pointers from general C pointers (utilizing the recently added ObjCObjectPointerType). The fun starts in Sema::GetTypeForDeclarator(), where "SomeInterface *" is now represented by a single AST node (rather than a PointerType whose Pointee is an ObjCInterfaceType). Since a significant amount of code assumed ObjC object pointers where based on C pointers/structs, this patch is very tedious. It should also explain why it is hard to accomplish this in smaller, self-contained patches.

This patch does most of the "heavy lifting" related to moving from PointerType->ObjCObjectPointerType. It doesn't include all potential "cleanups". The good news is additional cleanups can be done later (some are noted in the code). This patch is so large that I didn't want to include any changes that are purely aesthetic.

By making the ObjC types truly built-in, they are much easier to work with (and require fewer "hacks"). For example, there is no need for ASTContext::isObjCIdStructType() or ASTContext::isObjCClassStructType()! We believe this change (and the follow-up cleanups) will pay dividends over time. 

Given the amount of code change, I do expect some fallout from this change (though it does pass all of the clang tests). If you notice any problems, please let us know asap! Thanks.

llvm-svn: 75314
2009-07-10 23:34:53 +00:00
Daniel Dunbar df4a58edd8 Fix thinko in r74506, test condition for floats was inverted.
- Refactored slightly to make control flow more obvious.

llvm-svn: 74637
2009-07-01 20:37:45 +00:00
Nate Begeman ef1a7fa89d Implement Eli's feedback for vecto constant expressions;
For ExtVectorType, initializer is splatted to all elements.
For VectorType, initializer is bitcast to vector type.

Verified that for VectorType, output is identical to gcc.

llvm-svn: 74600
2009-07-01 07:50:47 +00:00
Argyrios Kyrtzidis cfbfe78e9e De-ASTContext-ify DeclContext.
Remove ASTContext parameter from DeclContext's methods. This change cascaded down to other Decl's methods and changes to call sites started "escalating".
Timings using pre-tokenized "cocoa.h" showed only a ~1% increase in time run between and after this commit.

llvm-svn: 74506
2009-06-30 02:36:12 +00:00
Nate Begeman 2ffd384441 OpenCL 1.0 Support:
Add support for scalar to vector and partially initialized vector constant initializers. 

llvm-svn: 74299
2009-06-26 18:22:18 +00:00
Eli Friedman 334046a134 PR4351: Add constant evaluation for constructs like "foo == NULL", where
foo has a constant address.

llvm-svn: 73321
2009-06-14 02:17:33 +00:00
Chris Lattner 15ba94987a Sink the BuiltinInfo object from ASTContext into the
preprocessor and initialize it early in clang-cc.  This
ensures that __has_builtin works in all modes, not just
when ASTContext is around.

llvm-svn: 73319
2009-06-14 01:54:56 +00:00
Eli Friedman fa90b154ca Minor simplification.
llvm-svn: 72887
2009-06-04 20:23:20 +00:00
Eli Friedman 4a4fefcd29 PR4326: Handle constant evaluation for void* pointer subtraction
correctly.

llvm-svn: 72886
2009-06-04 20:04:03 +00:00