Allow parameter promotion in constructors
[hiphop-php.git] / hphp / compiler / type_annotation.h
blobcc87d003c207b8427a3402e22aa55ae1611cc77e
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-2013 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 +----------------------------------------------------------------------+
17 #ifndef incl_HPHP_TYPEANNOTATION_H_
18 #define incl_HPHP_TYPEANNOTATION_H_
20 #include "hphp/util/base.h"
22 namespace HPHP {
23 ///////////////////////////////////////////////////////////////////////////////
25 DECLARE_BOOST_TYPES(TypeAnnotation);
27 /**
28 * A class that represents a type annotation. Type annotations are used
29 * for arguments in function/method definitions and class fields.
30 * For simple types (e.g. int, string, C, etc.) m_typeArgs and m_typeList are
31 * null and the name is all we need.
32 * For "composite" types the m_typeArgs is set to point to the type list.
33 * The type list is a an instance of this class with m_typeList pointing to
34 * the next type (linked list). So for the type
35 * Map<int, string>
36 * the representation would be
38 * __________________
39 * | m_name("Map") | __________________
40 * | m_typeArgs |--> | m_name("int") |
41 * | m_typeList(null) | | m_typeArgs(null) | ___________________
42 * |__________________| | m_typeList |--> | m_name("string") |
43 * |__________________| | m_typeArgs(null) |
44 * | m_typeList(null) |
45 * |___________________|
47 * Map<int, Vector<string>> is
49 * __________________
50 * | m_name("Map") | __________________
51 * | m_typeArgs |--> | m_name("int") |
52 * | m_typeList(null) | | m_typeArgs(null) | ___________________
53 * |__________________| | m_typeList |--> | m_name("Vector") |
54 * |__________________| | m_typeArgs |---
55 * | m_typeList(null) | |
56 * |___________________| |
57 * |
58 * |
59 * __________________ |
60 * | m_name(string) | <--
61 * | m_typeArgs(null) |
62 * | m_typeList(null) |
63 * |__________________|
65 class TypeAnnotation {
66 public:
67 TypeAnnotation(const std::string &name, TypeAnnotationPtr typeArgs);
69 void setNullable() { m_nullable = true; }
70 void setSoft() { m_soft = true; }
71 void setTuple() { m_tuple = true; }
72 void setFunction() { m_function = true; }
73 void setXHP() { m_xhp = true; }
74 void setTypeVar() { m_typevar = true; }
76 bool isNullable() const { return m_nullable; }
77 bool isSoft() const { return m_soft; }
78 bool isTuple() const { return m_tuple; }
79 bool isFunction() const { return m_function; }
80 bool isXHP() const { return m_xhp; }
81 bool isTypeVar() const { return m_typevar; }
84 * Return a shallow copy of this TypeAnnotation, except with
85 * nullability stripped.
87 TypeAnnotation stripNullable() const {
88 auto ret = *this;
89 ret.m_nullable = false;
90 return ret;
94 * Return whether this TypeAnnotation is equal to the "mixed" type.
96 bool isMixed() const { return !m_name.compare("mixed"); }
99 * Returns whether this TypeAnnotation is "simple"---as described
100 * above, this implies it has only one level of depth. Both the
101 * type list and type args are null.
103 * It may however be soft or nullable, or a function type, etc.
105 bool isSimple() const { return !m_typeList && !m_typeArgs; }
108 * Return a string for this annotation that is a type hint for
109 * normal "vanilla" php. This means <?hh-specific annotations (such
110 * as ?Foo or @Foo) are going to be stripped, as well as the deep
111 * information about a type. (E.g. for Vector<string> this will
112 * return "Vector".)
114 std::string vanillaName() const;
117 * Returns a complete string name of this type-annotation, including
118 * <?hh-specific extensions, any type parameter list, etc.
120 std::string fullName() const;
123 * Fill the vector in input with all the types used in this annotation
124 * as simple names.
125 * Vector<Map<string, int>> would return
126 * [Vector, Map, string, int]
128 void getAllSimpleNames(std::vector<std::string>& names) const;
131 * Add a new element to this type list for this TypeAnnotation.
133 void appendToTypeList(TypeAnnotationPtr typeList);
135 private:
136 void functionTypeName(std::string &name) const;
137 void xhpTypeName(std::string &name) const;
138 void tupleTypeName(std::string &name) const;
139 void genericTypeName(std::string &name) const;
141 private:
142 std::string m_name;
143 TypeAnnotationPtr m_typeArgs;
144 TypeAnnotationPtr m_typeList;
145 unsigned m_nullable : 1;
146 unsigned m_soft : 1;
147 unsigned m_tuple : 1;
148 unsigned m_function : 1;
149 unsigned m_xhp : 1;
150 unsigned m_typevar : 1;
155 #endif