1099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith// RUN: %clang_cc1 -fsyntax-only -verify -std=c++98 -pedantic %s
21590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redl// C++ [expr.const]p1:
31590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redl//   In several places, C++ requires expressions that evaluate to an integral
41590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redl//   or enumeration constant: as array bounds, as case expressions, as
51590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redl//   bit-field lengths, as enumerator initializers, as static member
61590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redl//   initializers, and as integral or enumeration non-type template arguments.
71590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redl//   An integral constant-expression can involve only literals, enumerators,
81590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redl//   const variables or static data members of integral or enumeration types
91590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redl//   initialized with constant expressions, and sizeof expressions. Floating
101590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redl//   literals can appear only if they are cast to integral or enumeration types.
111590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redl
121590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redlenum Enum { eval = 1 };
131590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redlconst int cval = 2;
141590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redlconst Enum ceval = eval;
151590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redlstruct Struct {
161590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redl  static const int sval = 3;
171590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redl  static const Enum seval = eval;
181590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redl};
191590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redl
201590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redltemplate <int itval, Enum etval> struct C {
211590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redl  enum E {
221590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redl    v1 = 1,
231590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redl    v2 = eval,
241590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redl    v3 = cval,
251590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redl    v4 = ceval,
261590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redl    v5 = Struct::sval,
271590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redl    v6 = Struct::seval,
281590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redl    v7 = itval,
291590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redl    v8 = etval,
301590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redl    v9 = (int)1.5,
311590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redl    v10 = sizeof(Struct),
321590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redl    v11 = true? 1 + cval * Struct::sval ^ itval / (int)1.5 - sizeof(Struct) : 0
331590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redl  };
341590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redl  unsigned
351590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redl    b1 : 1,
361590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redl    b2 : eval,
371590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redl    b3 : cval,
381590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redl    b4 : ceval,
391590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redl    b5 : Struct::sval,
401590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redl    b6 : Struct::seval,
411590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redl    b7 : itval,
421590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redl    b8 : etval,
431590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redl    b9 : (int)1.5,
441590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redl    b10 : sizeof(Struct),
451590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redl    b11 : true? 1 + cval * Struct::sval ^ itval / (int)1.5 - sizeof(Struct) : 0
461590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redl    ;
471590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redl  static const int
481590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redl    i1 = 1,
491590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redl    i2 = eval,
501590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redl    i3 = cval,
511590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redl    i4 = ceval,
521590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redl    i5 = Struct::sval,
531590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redl    i6 = Struct::seval,
541590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redl    i7 = itval,
551590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redl    i8 = etval,
561590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redl    i9 = (int)1.5,
571590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redl    i10 = sizeof(Struct),
581590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redl    i11 = true? 1 + cval * Struct::sval ^ itval / (int)1.5 - sizeof(Struct) : 0
591590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redl    ;
600fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall  void f(int cond) {
610fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall    switch(cond) {
621590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redl    case    0 + 1:
631590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redl    case  100 + eval:
641590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redl    case  200 + cval:
651590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redl    case  300 + ceval:
661590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redl    case  400 + Struct::sval:
671590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redl    case  500 + Struct::seval:
681590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redl    case  600 + itval:
691590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redl    case  700 + etval:
701590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redl    case  800 + (int)1.5:
711590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redl    case  900 + sizeof(Struct):
721590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redl    case 1000 + (true? 1 + cval * Struct::sval ^
731590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redl                 itval / (int)1.5 - sizeof(Struct) : 0):
741590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redl      ;
751590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redl    }
761590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redl  }
771590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redl  typedef C<itval, etval> T0;
781590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redl};
791590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redl
801590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redltemplate struct C<1, eval>;
81af68d4ed6da11634e2915b3ca31da354369e4bc1Douglas Gregortemplate struct C<cval, ceval>;
82af68d4ed6da11634e2915b3ca31da354369e4bc1Douglas Gregortemplate struct C<Struct::sval, Struct::seval>;
83af68d4ed6da11634e2915b3ca31da354369e4bc1Douglas Gregor
84af68d4ed6da11634e2915b3ca31da354369e4bc1Douglas Gregorenum {
85af68d4ed6da11634e2915b3ca31da354369e4bc1Douglas Gregor  a = sizeof(int) == 8,
86af68d4ed6da11634e2915b3ca31da354369e4bc1Douglas Gregor  b = a? 8 : 4
87af68d4ed6da11634e2915b3ca31da354369e4bc1Douglas Gregor};
881e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith
891e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smithvoid diags(int n) {
901e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith  switch (n) {
91244ee7b89a483fd3764637abdf95de2893b437d0Richard Smith    case (1/0, 1): // expected-error {{not an integral constant expression}} expected-note {{division by zero}}
92244ee7b89a483fd3764637abdf95de2893b437d0Richard Smith    case (int)(1/0, 2.0): // expected-error {{not an integral constant expression}} expected-note {{division by zero}}
93244ee7b89a483fd3764637abdf95de2893b437d0Richard Smith    case __imag(1/0): // expected-error {{not an integral constant expression}} expected-note {{division by zero}}
94244ee7b89a483fd3764637abdf95de2893b437d0Richard Smith    case (int)__imag((double)(1/0)): // expected-error {{not an integral constant expression}} expected-note {{division by zero}}
951e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith      ;
961e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith  }
971e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith}
98db1822c6de43ff4aa5fa00234bf8222f6f4816e8Richard Smith
99db1822c6de43ff4aa5fa00234bf8222f6f4816e8Richard Smithnamespace IntOrEnum {
100db1822c6de43ff4aa5fa00234bf8222f6f4816e8Richard Smith  const int k = 0;
101db1822c6de43ff4aa5fa00234bf8222f6f4816e8Richard Smith  const int &p = k;
102db1822c6de43ff4aa5fa00234bf8222f6f4816e8Richard Smith  template<int n> struct S {};
103db1822c6de43ff4aa5fa00234bf8222f6f4816e8Richard Smith  S<p> s; // expected-error {{not an integral constant expression}}
104db1822c6de43ff4aa5fa00234bf8222f6f4816e8Richard Smith}
1052116b144cf07f2574d20517187eb8863645376ebRichard Smith
106099e7f647ccda915513f2b2ec53352dc756082d3Richard Smithextern const int recurse1;
107099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith// recurse2 cannot be used in a constant expression because it is not
108099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith// initialized by a constant expression. The same expression appearing later in
109099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith// the TU would be a constant expression, but here it is not.
110099e7f647ccda915513f2b2ec53352dc756082d3Richard Smithconst int recurse2 = recurse1;
111099e7f647ccda915513f2b2ec53352dc756082d3Richard Smithconst int recurse1 = 1;
112099e7f647ccda915513f2b2ec53352dc756082d3Richard Smithint array1[recurse1]; // ok
113099e7f647ccda915513f2b2ec53352dc756082d3Richard Smithint array2[recurse2]; // expected-warning {{variable length array}} expected-warning {{integer constant expression}}
114099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith
1152116b144cf07f2574d20517187eb8863645376ebRichard Smithnamespace FloatConvert {
1162116b144cf07f2574d20517187eb8863645376ebRichard Smith  typedef int a[(int)42.3];
1172116b144cf07f2574d20517187eb8863645376ebRichard Smith  typedef int a[(int)42.997];
118099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  typedef int b[(int)4e10]; // expected-warning {{variable length}} expected-error {{variable length}}
1192116b144cf07f2574d20517187eb8863645376ebRichard Smith}
120