From: Ulya Trofimovich Date: Sat, 28 Nov 2015 11:36:41 +0000 (+0000) Subject: Don't use overloaded constructors with integral types. X-Git-Tag: 0.15.3~23 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=f65caba520107cb88a7258df07fdf2936557b008;p=re2c Don't use overloaded constructors with integral types. This causes ambiguity in overload resolution on OS X: src/codegen/skeleton/generate_data.cc:308:30: error: ambiguous conversion for functional-style cast from 'const size_t' (aka 'const unsigned long') to 'Node::covers_t' (aka 'u32lim_t<1024 * 1024 * 1024>') const Node::covers_t size = Node::covers_t (len) * Node::covers_t (count); ^~~~~~~~~~~~~~~~~~~ ./src/util/u32lim.h:20:11: note: candidate constructor explicit u32lim_t (uint32_t x) ^ ./src/util/u32lim.h:23:11: note: candidate constructor explicit u32lim_t (uint64_t x) Use static constructor-like methods with expliit names. --- diff --git a/re2c/src/codegen/skeleton/control_flow.cc b/re2c/src/codegen/skeleton/control_flow.cc index e5fa3fe3..976740eb 100644 --- a/re2c/src/codegen/skeleton/control_flow.cc +++ b/re2c/src/codegen/skeleton/control_flow.cc @@ -10,17 +10,17 @@ Node::nakeds_t Node::naked_ways (const way_t & prefix, std::vector & ways { if (!rule.rank.is_none ()) { - return nakeds_t (0u); + return nakeds_t::from32(0u); } else if (end ()) { ways.push_back (prefix); - return nakeds_t (prefix.size ()); + return nakeds_t::from64(prefix.size ()); } else if (loop < 2) { local_inc _ (loop); - nakeds_t size (0u); + nakeds_t size = nakeds_t::from32(0u); for (arcsets_t::iterator i = arcsets.begin (); i != arcsets.end (); ++i) { way_t w = prefix; @@ -35,7 +35,7 @@ Node::nakeds_t Node::naked_ways (const way_t & prefix, std::vector & ways } else { - return nakeds_t (0u); + return nakeds_t::from32(0u); } } diff --git a/re2c/src/codegen/skeleton/generate_data.cc b/re2c/src/codegen/skeleton/generate_data.cc index 2faf76e2..db780b75 100644 --- a/re2c/src/codegen/skeleton/generate_data.cc +++ b/re2c/src/codegen/skeleton/generate_data.cc @@ -58,11 +58,11 @@ Node::permuts_t Node::sizeof_permutate (permuts_t wid, permuts_t len) else if (loop < 2) { local_inc _ (loop); - permuts_t size (0u); - const permuts_t new_len = len + permuts_t (1u); + permuts_t size = permuts_t::from32(0u); + const permuts_t new_len = len + permuts_t::from32(1u); for (arcs_t::iterator i = arcs.begin (); i != arcs.end (); ++i) { - const permuts_t new_wid = wid * permuts_t (i->second.size ()); + const permuts_t new_wid = wid * permuts_t::from64(i->second.size ()); if (new_wid.overflow ()) { return permuts_t::limit (); @@ -77,7 +77,7 @@ Node::permuts_t Node::sizeof_permutate (permuts_t wid, permuts_t len) } else { - return permuts_t (0u); + return permuts_t::from32(0u); } } @@ -145,7 +145,7 @@ template template Node::covers_t Node::cover (const multipath_t & prefix, FILE * input, FILE * keys) { - covers_t size (0u); + covers_t size = covers_t::from32(0u); if (end () && suffix == NULL) { suffix = new path_t (rule, ctx); @@ -180,7 +180,7 @@ template void Skeleton::generate_paths_cunit_key (FILE * input, FILE * keys) { multipath_t prefix (nodes->rule, nodes->ctx); - if (nodes->sizeof_permutate (Node::permuts_t (1u), Node::permuts_t (0u)).overflow ()) + if (nodes->sizeof_permutate (Node::permuts_t::from32(1u), Node::permuts_t::from32(0u)).overflow ()) { if (nodes->cover (prefix, input, keys).overflow ()) { @@ -305,7 +305,7 @@ template count = std::max (count, prefix[i]->size ()); } - const Node::covers_t size = Node::covers_t (len) * Node::covers_t (count); + const Node::covers_t size = Node::covers_t::from64(len) * Node::covers_t::from64(count); if (!size.overflow ()) { // input diff --git a/re2c/src/util/u32lim.h b/re2c/src/util/u32lim.h index bda1784a..d9c356cc 100644 --- a/re2c/src/util/u32lim.h +++ b/re2c/src/util/u32lim.h @@ -11,12 +11,6 @@ template class u32lim_t { uint32_t value; - -public: - // implicit conversion is forbidden, because - // operands should be converted before operation: - // uint32_t x, y; ... u32lim_t z = x + y; - // will result in 32-bit addition and may overflow explicit u32lim_t (uint32_t x) : value (x < LIMIT ? x : LIMIT) {} @@ -24,6 +18,16 @@ public: : value (x < LIMIT ? static_cast (x) : LIMIT) {} +public: + // implicit conversion is forbidden, because + // operands should be converted before operation: + // uint32_t x, y; ... u32lim_t z = x + y; + // will result in 32-bit addition and may overflow + // Don't export overloaded constructors: it breaks OS X builds + // ('size_t' causes resolution ambiguity) + static u32lim_t from32 (uint32_t x) { return u32lim_t(x); } + static u32lim_t from64 (uint64_t x) { return u32lim_t(x); } + static u32lim_t limit () { return u32lim_t (LIMIT);