Debian Codegraph @

core/doc_data.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* doc_data.cpp */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 #include "doc_data.h"
32
33 String DocData::get_default_value_string(const Variant &p_value) {
34 const Variant::Type type = p_value.get_type();
35 if (type == Variant::ARRAY) {
36 return Variant(Array(p_value, 0, StringName(), Variant())).get_construct_string().replace_char('\n', ' ');
37 } else if (type == Variant::DICTIONARY) {
38 return Variant(Dictionary(p_value, 0, StringName(), Variant(), 0, StringName(), Variant())).get_construct_string().replace_char('\n', ' ');
39 } else if (type == Variant::INT) {
40 return itos(p_value);
41 } else if (type == Variant::FLOAT) {
42 // Since some values are 32-bit internally, use 32-bit for all
43 // documentation values to avoid garbage digits at the end.
44 const String s = String::num_scientific((float)p_value);
45 // Use float literals for floats in the documentation for clarity.
46 if (s != "inf" && s != "-inf" && s != "nan") {
47 if (!s.contains_char('.') && !s.contains_char('e')) {
48 return s + ".0";
49 }
50 }
51 return s;
52 } else {
53 return p_value.get_construct_string().replace_char('\n', ' ');
54 }
55 }
56
57 void DocData::return_doc_from_retinfo(DocData::MethodDoc &p_method, const PropertyInfo &p_retinfo) {
58 if (p_retinfo.type == Variant::INT && p_retinfo.hint == PROPERTY_HINT_INT_IS_POINTER) {
59 p_method.return_type = p_retinfo.hint_string;
60 if (p_method.return_type.is_empty()) {
61 p_method.return_type = "void*";
62 } else {
63 p_method.return_type += "*";
64 }
65 } else if (p_retinfo.type == Variant::INT && p_retinfo.usage & (PROPERTY_USAGE_CLASS_IS_ENUM | PROPERTY_USAGE_CLASS_IS_BITFIELD)) {
66 p_method.return_enum = p_retinfo.class_name;
67 if (p_method.return_enum.begins_with("_")) { //proxy class
68 p_method.return_enum = p_method.return_enum.substr(1);
69 }
70 p_method.return_is_bitfield = p_retinfo.usage & PROPERTY_USAGE_CLASS_IS_BITFIELD;
71 p_method.return_type = "int";
72 } else if (p_retinfo.class_name != StringName()) {
73 p_method.return_type = p_retinfo.class_name;
74 } else if (p_retinfo.type == Variant::ARRAY && p_retinfo.hint == PROPERTY_HINT_ARRAY_TYPE) {
75 p_method.return_type = p_retinfo.hint_string + "[]";
76 } else if (p_retinfo.type == Variant::DICTIONARY && p_retinfo.hint == PROPERTY_HINT_DICTIONARY_TYPE) {
77 p_method.return_type = "Dictionary[" + p_retinfo.hint_string.replace(";", ", ") + "]";
78 } else if (p_retinfo.hint == PROPERTY_HINT_RESOURCE_TYPE) {
79 p_method.return_type = p_retinfo.hint_string;
80 } else if (p_retinfo.type == Variant::NIL && p_retinfo.usage & PROPERTY_USAGE_NIL_IS_VARIANT) {
81 p_method.return_type = "Variant";
82 } else if (p_retinfo.type == Variant::NIL) {
83 p_method.return_type = "void";
84 } else {
85 p_method.return_type = Variant::get_type_name(p_retinfo.type);
86 }
87 }
88
89 void DocData::argument_doc_from_arginfo(DocData::ArgumentDoc &p_argument, const PropertyInfo &p_arginfo) {
90 p_argument.name = p_arginfo.name;
91
92 if (p_arginfo.type == Variant::INT && p_arginfo.hint == PROPERTY_HINT_INT_IS_POINTER) {
93 p_argument.type = p_arginfo.hint_string;
94 if (p_argument.type.is_empty()) {
95 p_argument.type = "void*";
96 } else {
97 p_argument.type += "*";
98 }
99 } else if (p_arginfo.type == Variant::INT && p_arginfo.usage & (PROPERTY_USAGE_CLASS_IS_ENUM | PROPERTY_USAGE_CLASS_IS_BITFIELD)) {
100 p_argument.enumeration = p_arginfo.class_name;
101 if (p_argument.enumeration.begins_with("_")) { //proxy class
102 p_argument.enumeration = p_argument.enumeration.substr(1);
103 }
104 p_argument.is_bitfield = p_arginfo.usage & PROPERTY_USAGE_CLASS_IS_BITFIELD;
105 p_argument.type = "int";
106 } else if (p_arginfo.class_name != StringName()) {
107 p_argument.type = p_arginfo.class_name;
108 } else if (p_arginfo.type == Variant::ARRAY && p_arginfo.hint == PROPERTY_HINT_ARRAY_TYPE) {
109 p_argument.type = p_arginfo.hint_string + "[]";
110 } else if (p_arginfo.type == Variant::DICTIONARY && p_arginfo.hint == PROPERTY_HINT_DICTIONARY_TYPE) {
111 p_argument.type = "Dictionary[" + p_arginfo.hint_string.replace(";", ", ") + "]";
112 } else if (p_arginfo.hint == PROPERTY_HINT_RESOURCE_TYPE) {
113 p_argument.type = p_arginfo.hint_string;
114 } else if (p_arginfo.type == Variant::NIL) {
115 // Parameters cannot be void, so PROPERTY_USAGE_NIL_IS_VARIANT is not necessary
116 p_argument.type = "Variant";
117 } else {
118 p_argument.type = Variant::get_type_name(p_arginfo.type);
119 }
120 }
121
122 void DocData::method_doc_from_methodinfo(DocData::MethodDoc &p_method, const MethodInfo &p_methodinfo, const String &p_desc) {
123 p_method.name = p_methodinfo.name;
124 p_method.description = p_desc;
125
126 if (p_methodinfo.flags & METHOD_FLAG_VIRTUAL) {
127 p_method.qualifiers = "virtual";
128 }
129
130 if (p_methodinfo.flags & METHOD_FLAG_VIRTUAL_REQUIRED) {
131 if (!p_method.qualifiers.is_empty()) {
132 p_method.qualifiers += " ";
133 }
134 p_method.qualifiers += "required";
135 }
136
137 if (p_methodinfo.flags & METHOD_FLAG_CONST) {
138 if (!p_method.qualifiers.is_empty()) {
139 p_method.qualifiers += " ";
140 }
141 p_method.qualifiers += "const";
142 }
143
144 if (p_methodinfo.flags & METHOD_FLAG_VARARG) {
145 if (!p_method.qualifiers.is_empty()) {
146 p_method.qualifiers += " ";
147 }
148 p_method.qualifiers += "vararg";
149 }
150
151 if (p_methodinfo.flags & METHOD_FLAG_STATIC) {
152 if (!p_method.qualifiers.is_empty()) {
153 p_method.qualifiers += " ";
154 }
155 p_method.qualifiers += "static";
156 }
157
158 return_doc_from_retinfo(p_method, p_methodinfo.return_val);
159
160 for (int64_t i = 0; i < p_methodinfo.arguments.size(); ++i) {
161 DocData::ArgumentDoc argument;
162 argument_doc_from_arginfo(argument, p_methodinfo.arguments[i]);
163 int64_t default_arg_index = i - (p_methodinfo.arguments.size() - p_methodinfo.default_arguments.size());
164 if (default_arg_index >= 0) {
165 Variant default_arg = p_methodinfo.default_arguments[default_arg_index];
166 argument.default_value = get_default_value_string(default_arg);
167 }
168 p_method.arguments.push_back(argument);
169 }
170 }