Commit d9f2bcd2 authored by darin's avatar darin Committed by Commit bot

Mojo: add support for {double,float}.{INFINITY,NEGATIVE_INFINITY,NAN}

With this change, it is possible to assign INFINITY, NEGATIVE_INFINITY or NAN as a default value for any double or float field.

BUG=402216

Review URL: https://codereview.chromium.org/474063002

Cr-Commit-Position: refs/heads/master@{#291864}
parent 18499aa5
......@@ -11,6 +11,14 @@ module sample {
const uint8 kTwelve = 12;
const uint64 kTooBigForSignedInt64 = 9999999999999999999;
const double kDoubleInfinity = double.INFINITY;
const double kDoubleNegativeInfinity = double.NEGATIVE_INFINITY;
const double kDoubleNaN = double.NAN;
const float kFloatInfinity = float.INFINITY;
const float kFloatNegativeInfinity = float.NEGATIVE_INFINITY;
const float kFloatNaN = float.NAN;
struct Bar {
enum Type {
VERTICAL = 1,
......@@ -76,6 +84,13 @@ struct DefaultsTest {
uint64 a23@23 = 0xFFFFFFFFFFFFFFFF;
int64 a24@24 = 0x123456789;
int64 a25@25 = -0x123456789;
double a26@26 = double.INFINITY;
double a27@27 = double.NEGATIVE_INFINITY;
double a28@28 = double.NAN;
float a29@29 = float.INFINITY;
float a30@30 = float.NEGATIVE_INFINITY;
float a31@31 = float.NAN;
};
struct StructWithHoleV1 {
......
......@@ -5,10 +5,16 @@
#if defined(__clang__)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-private-field"
#elif defined(_MSC_VER)
#pragma warning(push)
#pragma warning(disable:4056)
#pragma warning(disable:4756)
#endif
#include "{{module.path}}.h"
#include <math.h>
#include "mojo/public/cpp/bindings/lib/array_serialization.h"
#include "mojo/public/cpp/bindings/lib/bindings_serialization.h"
#include "mojo/public/cpp/bindings/lib/bounds_checker.h"
......@@ -83,4 +89,6 @@ const {{constant.kind|cpp_pod_type}} {{struct.name}}::{{constant.name}} = {{cons
#if defined(__clang__)
#pragma clang diagnostic pop
#elif defined(_MSC_VER)
#pragma warning(pop)
#endif
......@@ -223,6 +223,16 @@ def TranslateConstants(token, kind):
else:
name.append(token.name)
return "::".join(name)
if isinstance(token, mojom.BuiltinValue):
if token.value == "double.INFINITY" or token.value == "float.INFINITY":
return "INFINITY";
if token.value == "double.NEGATIVE_INFINITY" or \
token.value == "float.NEGATIVE_INFINITY":
return "-INFINITY";
if token.value == "double.NAN" or token.value == "float.NAN":
return "NAN";
return '%s%s' % (token, _kind_to_cpp_literal_suffix.get(kind, ''))
def ExpressionToText(value, kind=None):
......
......@@ -260,6 +260,19 @@ def ExpressionToText(context, token, kind_spec=''):
if number >= 2 ** 63:
number -= 2 ** 64
return '%dL' % number
if isinstance(token, mojom.BuiltinValue):
if token.value == "double.INFINITY":
return "java.lang.Double.POSITIVE_INFINITY"
if token.value == "double.NEGATIVE_INFINITY":
return "java.lang.Double.NEGATIVE_INFINITY"
if token.value == "double.NAN":
return "java.lang.Double.NaN"
if token.value == "float.INFINITY":
return "java.lang.Float.POSITIVE_INFINITY"
if token.value == "float.NEGATIVE_INFINITY":
return "java.lang.Float.NEGATIVE_INFINITY"
if token.value == "float.NAN":
return "java.lang.Float.NaN"
return token
def IsPointerArrayKind(kind):
......
......@@ -198,6 +198,16 @@ def TranslateConstants(token):
name.append(token.enum_name)
name.append(token.name)
return ".".join(name)
if isinstance(token, mojom.BuiltinValue):
if token.value == "double.INFINITY" or token.value == "float.INFINITY":
return "Infinity";
if token.value == "double.NEGATIVE_INFINITY" or \
token.value == "float.NEGATIVE_INFINITY":
return "-Infinity";
if token.value == "double.NAN" or token.value == "float.NAN":
return "NaN";
return token
......
......@@ -42,6 +42,17 @@ def istr(index, string):
rv.__index__ = index
return rv
builtin_values = frozenset([
"double.INFINITY",
"double.NEGATIVE_INFINITY",
"double.NAN",
"float.INFINITY",
"float.NEGATIVE_INFINITY",
"float.NAN"])
def IsBuiltinValue(value):
return value in builtin_values
def LookupKind(kinds, spec, scope):
"""Tries to find which Kind a spec refers to, given the scope in which its
referenced. Starts checking from the narrowest scope to most general. For
......@@ -85,11 +96,15 @@ def LookupValue(values, name, scope, kind):
return values.get(name)
def FixupExpression(module, value, scope, kind):
"""Translates an IDENTIFIER into a structured Value object."""
"""Translates an IDENTIFIER into a built-in value or structured NamedValue
object."""
if isinstance(value, tuple) and value[0] == 'IDENTIFIER':
# Allow user defined values to shadow builtins.
result = LookupValue(module.values, value[1], scope, kind)
if result:
return result
if IsBuiltinValue(value[1]):
return mojom.BuiltinValue(value[1])
return value
def KindToData(kind):
......
......@@ -144,6 +144,11 @@ class NamedValue(object):
self.name)
class BuiltinValue(object):
def __init__(self, value):
self.value = value
class EnumValue(NamedValue):
def __init__(self, module, enum, field):
NamedValue.__init__(self, module, enum.parent_kind, field.name)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment