Skip to content
0.2.0-dev Download ZIP
173 lines ·7.9 KB Raw
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#include "core/object/method_info.h"
34#include "core/object/property_info.h"
35
36String DocData::get_default_value_string(const Variant &p_value) {
37 const Variant::Type type = p_value.get_type();
38 if (type == Variant::ARRAY) {
39 return Variant(Array(p_value, 0, StringName(), Variant())).get_construct_string().replace_char('\n', ' ');
40 } else if (type == Variant::DICTIONARY) {
41 return Variant(Dictionary(p_value, 0, StringName(), Variant(), 0, StringName(), Variant())).get_construct_string().replace_char('\n', ' ');
42 } else if (type == Variant::INT) {
43 return itos(p_value);
44 } else if (type == Variant::FLOAT) {
45 // Since some values are 32-bit internally, use 32-bit for all
46 // documentation values to avoid garbage digits at the end.
47 const String s = String::num_scientific((float)p_value);
48 // Use float literals for floats in the documentation for clarity.
49 if (s != "inf" && s != "-inf" && s != "nan") {
50 if (!s.contains_char('.') && !s.contains_char('e')) {
51 return s + ".0";
52 }
53 }
54 return s;
55 } else {
56 return p_value.get_construct_string().replace_char('\n', ' ');
57 }
58}
59
60void DocData::return_doc_from_retinfo(DocData::MethodDoc &p_method, const PropertyInfo &p_retinfo) {
61 if (p_retinfo.type == Variant::INT && p_retinfo.hint == PROPERTY_HINT_INT_IS_POINTER) {
62 p_method.return_type = p_retinfo.hint_string;
63 if (p_method.return_type.is_empty()) {
64 p_method.return_type = "void*";
65 } else {
66 p_method.return_type += "*";
67 }
68 } else if (p_retinfo.type == Variant::INT && p_retinfo.usage & (PROPERTY_USAGE_CLASS_IS_ENUM | PROPERTY_USAGE_CLASS_IS_BITFIELD)) {
69 p_method.return_enum = p_retinfo.class_name;
70 if (p_method.return_enum.begins_with("_")) { //proxy class
71 p_method.return_enum = p_method.return_enum.substr(1);
72 }
73 p_method.return_is_bitfield = p_retinfo.usage & PROPERTY_USAGE_CLASS_IS_BITFIELD;
74 p_method.return_type = "int";
75 } else if (p_retinfo.class_name != StringName()) {
76 p_method.return_type = p_retinfo.class_name;
77 } else if (p_retinfo.type == Variant::ARRAY && p_retinfo.hint == PROPERTY_HINT_ARRAY_TYPE) {
78 p_method.return_type = p_retinfo.hint_string + "[]";
79 } else if (p_retinfo.type == Variant::DICTIONARY && p_retinfo.hint == PROPERTY_HINT_DICTIONARY_TYPE) {
80 p_method.return_type = "Dictionary[" + p_retinfo.hint_string.replace(";", ", ") + "]";
81 } else if (p_retinfo.hint == PROPERTY_HINT_RESOURCE_TYPE) {
82 p_method.return_type = p_retinfo.hint_string;
83 } else if (p_retinfo.type == Variant::NIL && p_retinfo.usage & PROPERTY_USAGE_NIL_IS_VARIANT) {
84 p_method.return_type = "Variant";
85 } else if (p_retinfo.type == Variant::NIL) {
86 p_method.return_type = "void";
87 } else {
88 p_method.return_type = Variant::get_type_name(p_retinfo.type);
89 }
90}
91
92void DocData::argument_doc_from_arginfo(DocData::ArgumentDoc &p_argument, const PropertyInfo &p_arginfo) {
93 p_argument.name = p_arginfo.name;
94
95 if (p_arginfo.type == Variant::INT && p_arginfo.hint == PROPERTY_HINT_INT_IS_POINTER) {
96 p_argument.type = p_arginfo.hint_string;
97 if (p_argument.type.is_empty()) {
98 p_argument.type = "void*";
99 } else {
100 p_argument.type += "*";
101 }
102 } else if (p_arginfo.type == Variant::INT && p_arginfo.usage & (PROPERTY_USAGE_CLASS_IS_ENUM | PROPERTY_USAGE_CLASS_IS_BITFIELD)) {
103 p_argument.enumeration = p_arginfo.class_name;
104 if (p_argument.enumeration.begins_with("_")) { //proxy class
105 p_argument.enumeration = p_argument.enumeration.substr(1);
106 }
107 p_argument.is_bitfield = p_arginfo.usage & PROPERTY_USAGE_CLASS_IS_BITFIELD;
108 p_argument.type = "int";
109 } else if (p_arginfo.class_name != StringName()) {
110 p_argument.type = p_arginfo.class_name;
111 } else if (p_arginfo.type == Variant::ARRAY && p_arginfo.hint == PROPERTY_HINT_ARRAY_TYPE) {
112 p_argument.type = p_arginfo.hint_string + "[]";
113 } else if (p_arginfo.type == Variant::DICTIONARY && p_arginfo.hint == PROPERTY_HINT_DICTIONARY_TYPE) {
114 p_argument.type = "Dictionary[" + p_arginfo.hint_string.replace(";", ", ") + "]";
115 } else if (p_arginfo.hint == PROPERTY_HINT_RESOURCE_TYPE) {
116 p_argument.type = p_arginfo.hint_string;
117 } else if (p_arginfo.type == Variant::NIL) {
118 // Parameters cannot be void, so PROPERTY_USAGE_NIL_IS_VARIANT is not necessary
119 p_argument.type = "Variant";
120 } else {
121 p_argument.type = Variant::get_type_name(p_arginfo.type);
122 }
123}
124
125void DocData::method_doc_from_methodinfo(DocData::MethodDoc &p_method, const MethodInfo &p_methodinfo, const String &p_desc) {
126 p_method.name = p_methodinfo.name;
127 p_method.description = p_desc;
128
129 if (p_methodinfo.flags & METHOD_FLAG_VIRTUAL) {
130 p_method.qualifiers = "virtual";
131 }
132
133 if (p_methodinfo.flags & METHOD_FLAG_VIRTUAL_REQUIRED) {
134 if (!p_method.qualifiers.is_empty()) {
135 p_method.qualifiers += " ";
136 }
137 p_method.qualifiers += "required";
138 }
139
140 if (p_methodinfo.flags & METHOD_FLAG_CONST) {
141 if (!p_method.qualifiers.is_empty()) {
142 p_method.qualifiers += " ";
143 }
144 p_method.qualifiers += "const";
145 }
146
147 if (p_methodinfo.flags & METHOD_FLAG_VARARG) {
148 if (!p_method.qualifiers.is_empty()) {
149 p_method.qualifiers += " ";
150 }
151 p_method.qualifiers += "vararg";
152 }
153
154 if (p_methodinfo.flags & METHOD_FLAG_STATIC) {
155 if (!p_method.qualifiers.is_empty()) {
156 p_method.qualifiers += " ";
157 }
158 p_method.qualifiers += "static";
159 }
160
161 return_doc_from_retinfo(p_method, p_methodinfo.return_val);
162
163 for (int64_t i = 0; i < p_methodinfo.arguments.size(); ++i) {
164 DocData::ArgumentDoc argument;
165 argument_doc_from_arginfo(argument, p_methodinfo.arguments[i]);
166 int64_t default_arg_index = i - (p_methodinfo.arguments.size() - p_methodinfo.default_arguments.size());
167 if (default_arg_index >= 0) {
168 Variant default_arg = p_methodinfo.default_arguments[default_arg_index];
169 argument.default_value = get_default_value_string(default_arg);
170 }
171 p_method.arguments.push_back(argument);
172 }
173}