Author Topic: Integer Literal checking  (Read 13309 times)

bas

  • Full Member
  • ***
  • Posts: 220
    • View Profile
Integer Literal checking
« on: March 12, 2014, 01:22:11 PM »
Integer Literals in code are constants like 10, 20, -100, etc
The analyser in c2c currently distinguishes between 3 types of expressions:
fully compile-time-constant:  basically any number
partial compile-time-contant: a + 10
not compile time constant: a, calc()

Full:
char a = 128;   -> error 128 not in range of char, range [0, 127]
char b = 800 - 700;    // ok, since 100 is in range
So the entire expression is evaluated and checked against the range.

None:
char a = b;    // error if type of b is larger than char.
So just the types are checked

Partial: (this is where it starts to get fuzzy)
char a = 128 + b;  // (b = char) so should this be allowed?
or the previous example:
char b = 800 - 700 + a;   // char a, should this be allowed?

Here there are some choices I think:
1 don't check any ranges of partial ctc expressions
2 only allow subexpressions to be of same < smaller type/value
3 check if range could be ok(Clang has a crude implementation of this)

I would prefer option 2, since it is the most safe option, but it would
restrict programmers too much perhaps. I think this would be ok,
but what do you guys think?

Bas




DerSaidin

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Integer Literal checking
« Reply #1 on: March 24, 2014, 11:54:46 AM »
Option 2 seems reasonable to me.

Also keep in mind you will be able to allow more complex constant expressions later (if you find it needed), but it would be harder to take them away.

Being able to call functions and stuff with C++ constexpr is cool. What do you think of using the same rules as C++ constexpr?

bas

  • Full Member
  • ***
  • Posts: 220
    • View Profile
Re: Integer Literal checking
« Reply #2 on: March 27, 2014, 09:03:54 AM »
I currently implemented Option 2 and I think it works just fine.

constexpr is indeed a nice option. I wonder if LLVM's IR can optimize functions like factorial etc?
It would not a problem to add a constexpr concept later on. The flags are already present in the
AST for other const stuff like numbers and constants etc.