naming 2/2 - use naming_db_path provider
[hiphop-php.git] / hphp / hhbbc / eval-cell.h
bloba37683ef43c1750d9ca9c3a90a85c4e834aed650
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-present Facebook, Inc. (http://www.facebook.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
16 #ifndef incl_HHBBC_EVAL_CELL_H_
17 #define incl_HHBBC_EVAL_CELL_H_
19 #include <stdexcept>
20 #include <exception>
22 #include <folly/ScopeGuard.h>
23 #include <folly/Optional.h>
25 #include "hphp/runtime/base/array-data.h"
26 #include "hphp/runtime/base/execution-context.h"
27 #include "hphp/runtime/base/string-data.h"
28 #include "hphp/runtime/base/tv-refcount.h"
29 #include "hphp/runtime/vm/repo.h"
31 #include "hphp/hhbbc/options.h"
32 #include "hphp/hhbbc/type-system.h"
34 namespace HPHP { namespace HHBBC {
36 //////////////////////////////////////////////////////////////////////
39 * When constant-evaluating certain operations, it's possible they
40 * will return non-static objects, or throw exceptions (e.g. tvAdd()
41 * with an array and an int).
43 * This routine converts these things back to types. In the case of
44 * an exception it returns TInitCell.
46 template<class Pred>
47 folly::Optional<Type> eval_cell(Pred p) {
48 try {
49 assert(!RuntimeOption::EvalJit);
50 ThrowAllErrorsSetter taes;
52 TypedValue c = p();
53 if (isRefcountedType(c.m_type)) {
54 if (c.m_type == KindOfString &&
55 c.m_data.pstr->size() > Repo::get().stringLengthLimit()) {
56 tvDecRefCountable(&c);
57 return TStr;
59 tvAsVariant(&c).setEvalScalar();
63 * We need to get rid of statics if we're not actually going to do
64 * constant propagation. When ConstantProp is on, the types we
65 * create here can reflect that we'll be changing bytecode later
66 * to actually make these into non-reference-counted SStr or
67 * SArrs. If we leave the bytecode alone, though, it generally
68 * won't actually be static at runtime.
70 auto const t = from_cell(c);
71 return options.ConstantProp ? t : loosen_staticness(t);
72 } catch (const Object&) {
73 return folly::none;
74 } catch (const std::exception&) {
75 return folly::none;
76 } catch (...) {
77 always_assert_flog(0, "a non-std::exception was thrown in eval_cell");
81 template<typename Pred>
82 folly::Optional<typename std::result_of<Pred()>::type>
83 eval_cell_value(Pred p) {
84 try {
85 ThrowAllErrorsSetter taes;
86 return p();
87 } catch (const Object&) {
88 return folly::none;
89 } catch (const std::exception&) {
90 return folly::none;
91 } catch (...) {
92 always_assert_flog(0, "a non-std::exception was thrown in eval_cell_value");
96 //////////////////////////////////////////////////////////////////////
100 #endif