1 #ifndef DOCPP_H
2 #define DOCPP_H
3 
4 #include <Core/Core.h>
5 
6 namespace Upp {
7 
8 // These functions can only but executed by single thread, unless said otherwise
9 
10 bool   IsCPPFile(const String& file);
11 bool   IsHFile(const String& path);
12 
13 String GetStdDefs();
14 
15 void   SetPPDefs(const String& defs);
16 
17 void   PPSync(const String& include_path);
18 
19 enum Kind {
20 	STRUCT,
21 	STRUCTTEMPLATE,
22 	TYPEDEF,
23 	CONSTRUCTOR,
24 	DESTRUCTOR,
25 	FUNCTION,
26 	INSTANCEFUNCTION,
27 	CLASSFUNCTION,
28 	FUNCTIONTEMPLATE,
29 	INSTANCEFUNCTIONTEMPLATE,
30 	CLASSFUNCTIONTEMPLATE,
31 	INLINEFRIEND,
32 	VARIABLE,
33 	INSTANCEVARIABLE,
34 	CLASSVARIABLE,
35 	ENUM,
36 	MACRO,
37 	FRIENDCLASS,
38 	NAMESPACE,
39 };
40 
IsCppType(int i)41 inline bool IsCppType(int i)
42 {
43 	return i >= STRUCT && i <= TYPEDEF || i == FRIENDCLASS;
44 }
45 
IsCppCode(int i)46 inline bool IsCppCode(int i) {
47 	return i >= CONSTRUCTOR && i <= INLINEFRIEND;
48 };
49 
IsCppData(int i)50 inline bool IsCppData(int i) {
51 	return i >= VARIABLE && i <= ENUM;
52 }
53 
IsCppMacro(int i)54 inline bool IsCppMacro(int i) {
55 	return i == MACRO;
56 }
57 
IsCppTemplate(int i)58 inline bool IsCppTemplate(int i) {
59 	return i == STRUCTTEMPLATE || i >= FUNCTIONTEMPLATE && i <= CLASSFUNCTIONTEMPLATE;
60 }
61 
62 void   CleanPP();
63 void   SerializePPFiles(Stream& s);
64 void   SweepPPFiles(const Index<String>& keep);
65 
66 void InvalidateFileTimeCache();
67 void InvalidateFileTimeCache(const String& path);
68 Time GetFileTimeCached(const String& path);
69 
70 String               NormalizeSourcePath(const String& path, const String& currdir);
71 String               NormalizeSourcePath(const String& path);
72 
73 void                 ClearSources();
74 const Index<String>& GetAllSources();
75 void                 GatherSources(const String& path);
76 String               GetMasterFile(const String& file);
77 const VectorMap<String, String>& GetAllSourceMasters();
78 
79 const char **CppKeyword();
80 
81 enum CppAccess {
82 	PUBLIC,
83 	PROTECTED,
84 	PRIVATE,
85 };
86 
87 enum FileTypeEnum {
88 	FILE_H,
89 	FILE_HPP,
90 	FILE_CPP,
91 	FILE_C,
92 	FILE_OTHER,
93 };
94 
95 struct CppItem {
96 	String         qitem;
97 	String         item;
98 	String         name;
99 	String         uname;
100 	String         natural;
101 	String         type;
102 	String         qtype;
103 	String         tparam; // complete template parameters list, like '<class T>'
104 	String         param;
105 	String         pname;
106 	String         ptype; // fn: types of parameters, struct: base classes
107 	String         qptype;
108 	String         tname;
109 	String         ctname;
110 	String         using_namespaces;
111 	byte           access = PUBLIC;
112 	byte           kind = STRUCT;
113 	int16          at = 0;
114 	bool           virt = false;
115 
116 	bool           decla = false;
117 	bool           lvalue = false;
118 	bool           isptr = false;
119 
120 	byte           filetype = FILE_OTHER;
121 	bool           impl = false;
122 	int            file = 0;
123 	int            line = 0;
124 
125 	bool           qualify = true;
126 
IsTypeCppItem127 	bool           IsType() const      { return IsCppType(kind); }
IsCodeCppItem128 	bool           IsCode() const      { return IsCppCode(kind); }
IsDataCppItem129 	bool           IsData() const      { return IsCppData(kind); }
IsMacroCppItem130 	bool           IsMacro() const     { return IsCppMacro(kind); }
IsTemplateCppItem131 	bool           IsTemplate() const  { return IsCppTemplate(kind); }
132 
133 	void           Serialize(Stream& s);
134 
135 	void           Dump(Stream& s) const;
136 	String         ToString() const;
137 };
138 
139 int FindItem(const Array<CppItem>& x, const String& qitem);
140 int FindName(const Array<CppItem>& x, const String& name, int i = 0);
141 
142 struct CppBase : ArrayMap<String, Array<CppItem> > {
143 	String         types_md5;
144 	Index<String>  namespaces;
145 
146 	bool           IsType(int i) const;
147 	void           Sweep(const Index<int>& file, bool keep = true);
RemoveFilesCppBase148 	void           RemoveFiles(const Index<int>& remove_file) { Sweep(remove_file, false); }
149 	void           RemoveFile(int filei);
150 	void           Append(CppBase&& base);
151 
152 	void           Dump(Stream& s);
153 };
154 
155 class ScopeInfo { // information about scope
156 	bool           bvalid; // baselist is valid
157 	bool           nvalid; // scopes is valid
158 	Vector<String> baselist; // list of all base classes of scope
159 	Vector<String> scopes; // list of scopes (Upp::String::Init::, Upp::String::, Upp::)
160 	int            scopei; // index of this scope in base
161 	String         usings; // using namespaces contained in scopes
162 
163 	void           Bases(int i, Vector<int>& g);
164 	void           Init();
165 
166 public:
167 	const CppBase&            base;
168 	VectorMap<String, String> cache;
169 
170 	const Vector<String>& GetBases();
171 	const Vector<String>& GetScopes(const String& usings);
GetScope()172 	int                   GetScope() const              { return scopei; }
NoBases()173 	void                  NoBases()                     { baselist.Clear(); bvalid = true; }
174 
175 	ScopeInfo(const CppBase& base, int scopei = -1);
176 	ScopeInfo(int scopei, const CppBase& base);
177 	ScopeInfo(const CppBase& base, const String& scope);
178 	ScopeInfo(const ScopeInfo& f);
179 };
180 
181 struct ParserContext {
182 	struct Context {
183 		String         ns;
184 		String         scope;
185 		String         ctname;
186 		Vector<int>    tparam;
187 		Index<int>     typenames;
188 		int            access;
189 		String         namespace_using;
190 
191 		void operator<<=(const Context& t);
192 
193 		String  Dump() const;
194 
ContextParserContext::Context195 		Context() {}
196 		rval_default(Context);
197 	};
198 
199 	Context     context;
200 	String      current_scope;
201 	String      current_key;
202 	String      current_name;
203 	CppItem     current;
204 	bool        inbody;
205 
206 	struct Local : Moveable<Local> {
207 		String type;
208 		bool   isptr;
209 		int    line;
210 	};
211 
212 	VectorMap<String, Local>  local;
213 
214 	Vector<String> GetNamespaces() const;
IsInBodyParserContext215 	bool           IsInBody() const                   { return inbody; }
216 };
217 
218 // Parse CAN be run in parallel
219 void Parse(CppBase& base, const String& src, int file, int filetype, const String& path,
220            Event<int, const String&> error, const Vector<String>& namespace_stack,
221            const Index<String>& namespace_using);
222 
223 
224 String NoTemplatePars(const String& type);
225 
226 // PreprocessParse CAN be run in parallel
227 void PreprocessParse(CppBase& base, Stream& in, int file, const String& path,
228                      Event<int, const String&> error);
229 String PreprocessCpp(const String& src, const String& path);
230 
231 ParserContext AssistParse(const String& src, const String& path_, Event<int, const String&> error,
232                           Function<String(String, String, String)> qualify);
233 
234 void SimpleParse(CppBase& cpp, const String& txt, const String& cls);
235 
236 void   Qualify(CppBase& base);
237 
238 String Qualify(const CppBase& base, const String& scope, const String& type, const String& usings);
239 
240 String ResolveTParam(const CppBase& codebase, const String& type, const Vector<String>& tparam);
241 void   ResolveTParam(const CppBase& codebase, Vector<String>& type, const Vector<String>& tparam);
242 
243 String ParseTemplatedType(const String& type, Vector<String>& tparam);
244 
245 bool HasCPPFileKeyword(const String& path, const String& id);
246 
247 String GetPPMD5(const String& fn);
248 
249 // GetDependeciesMD5 CAN be run in parallel
250 String GetDependeciesMD5(const String& path, Index<String>& visited);
251 
252 Index<String> GetExpressionType(const CppBase& codebase, const ParserContext& parser, const Vector<String>& xp);
253 
254 }
255 
256 #endif
257