Debian Codegraph @

CodeLite/language.h

raw
1 //////////////////////////////////////////////////////////////////////////////
2 //////////////////////////////////////////////////////////////////////////////
3 //
4 // copyright : (C) 2008 by Eran Ifrah
5 // file name : language.h
6 //
7 // -------------------------------------------------------------------------
8 // A
9 // _____ _ _ _ _
10 // / __ \ | | | | (_) |
11 // | / \/ ___ __| | ___| | _| |_ ___
12 // | | / _ \ / _ |/ _ \ | | | __/ _ )
13 // | \__/\ (_) | (_| | __/ |___| | || __/
14 // \____/\___/ \__,_|\___\_____/_|\__\___|
15 //
16 // F i l e
17 //
18 // This program is free software; you can redistribute it and/or modify
19 // it under the terms of the GNU General Public License as published by
20 // the Free Software Foundation; either version 2 of the License, or
21 // (at your option) any later version.
22 //
23 //////////////////////////////////////////////////////////////////////////////
24 //////////////////////////////////////////////////////////////////////////////
25 #ifndef CODELITE_LANGUAGE_H
26 #define CODELITE_LANGUAGE_H
27
28 #include "CxxTokenizer.h"
29 #include "CxxVariable.h"
30 #include "codelite_exports.h"
31 #include "comment.h"
32 #include "cpp_scanner.h"
33 #include "entry.h"
34 #include "expression_result.h"
35 #include "function.h"
36 #include "macros.h"
37 #include "parsedtoken.h"
38 #include "singleton.h"
39 #include "tokenizer.h"
40 #include "variable.h"
41 #include "y.tab.h"
42
43 #include <set>
44 #include <vector>
45 #include <wx/filename.h>
46
47 enum SearchFlags {
48 PartialMatch = 0x00000001,
49 ExactMatch = 0x00000002,
50 IgnoreCaseSensitive = 0x00000004,
51 ReplaceTokens = 0x00000008,
52 };
53
54 class TagsManager;
55
56 class TemplateHelper
57 {
58 std::vector<wxArrayString> templateInstantiationVector;
59 wxArrayString templateDeclaration;
60 wxString typeScope;
61 wxString typeName;
62
63 public:
64 TemplateHelper() {}
65 ~TemplateHelper() {}
66
67 void SetTemplateDeclaration(const wxString& templateDeclaration);
68 void SetTemplateInstantiation(const wxString& tempalteInstantiation);
69
70 void SetTemplateDeclaration(const wxArrayString& templateDeclaration)
71 {
72 this->templateDeclaration = templateDeclaration;
73 }
74 void SetTemplateInstantiation(const wxArrayString& templateInstantiation);
75
76 const wxArrayString& GetTemplateDeclaration() const { return templateDeclaration; }
77 bool IsTemplate() const { return templateDeclaration.IsEmpty() == false; }
78
79 void SetTypeScope(const wxString& typeScope) { this->typeScope = typeScope; }
80 const wxString& GetTypeScope() const { return typeScope; }
81 void SetTypeName(const wxString& typeName) { this->typeName = typeName; }
82 const wxString& GetTypeName() const { return typeName; }
83 wxString Substitute(const wxString& name);
84
85 void Clear();
86
87 wxString GetPath() const;
88 };
89
90 class WXDLLIMPEXP_CL Language
91 {
92 friend class LanguageST;
93 friend class TagEntry;
94 friend class TemplateHelper;
95 friend class TagsManager;
96
97 private:
98 std::map<char, char> m_braces;
99 std::vector<wxString> m_delimArr;
100 wxString m_expression;
101 CppScannerPtr m_scanner;
102 CxxTokenizer m_tokenScanner;
103 TagsManager* m_tm;
104 wxString m_visibleScope;
105 wxString m_lastFunctionSignature;
106 std::vector<wxString> m_additionalScopes; // collected by parsing 'using namespace XXX'
107 std::map<wxString, std::vector<wxString>> m_additionalScopesCache; // collected by parsing 'using namespace XXX'
108 TemplateHelper m_templateHelper;
109 std::set<wxString> m_templateArgs;
110 CxxVariable::Map_t m_locals;
111
112 protected:
113 void SetVisibleScope(const wxString& visibleScope) { this->m_visibleScope = visibleScope; }
114 const wxString& GetVisibleScope() const { return m_visibleScope; }
115 CxxVariable::Ptr_t FindLocalVariable(const wxString& name);
116 CxxVariable::Ptr_t FindVariableInScope(const wxString& scope, const wxString& name);
117
118 public:
119 /**
120 * Return the visible scope of syntax starting at depth zero.
121 *
122 * For example, given the following srcString:
123 * \code
124 * int main()
125 * {
126 * {
127 * int i =0;
128 * }
129 * }
130 * \endcode
131 *
132 * the output will be:
133 * \code
134 * int main()
135 * {
136 * ;
137 * }
138 * \code
139 *
140 * \note The scopes with depth greater then 0 will be removed.
141 * This function is useful for searching for local members.
142 * @param srcString Input string
143 * @return visible scope
144 */
145 wxString OptimizeScope(const wxString& srcString, int lastFuncLine, wxString& localsScope);
146
147 /**
148 * @brief given fileContent, locate the best line to place a class forward declaration
149 * statement
150 */
151 int GetBestLineForForwardDecl(const wxString& fileContent) const;
152
153 void CheckForTemplateAndTypedef(ParsedToken* token);
154
155 void SetLastFunctionSignature(const wxString& lastFunctionSignature)
156 {
157 this->m_lastFunctionSignature = lastFunctionSignature;
158 }
159 const wxString& GetLastFunctionSignature() const { return m_lastFunctionSignature; }
160
161 void SetAdditionalScopes(const std::vector<wxString>& additionalScopes, const wxString& filename);
162
163 /**
164 * @brief update the 'additional scopes' cache with list of scopes
165 * @param filename
166 * @param additionalScopes
167 */
168 void UpdateAdditionalScopesCache(const wxString& filename, const std::vector<wxString>& additionalScopes);
169 /**
170 * @brief clear the additional scopes cache
171 */
172 void ClearAdditionalScopesCache();
173
174 const std::vector<wxString>& GetAdditionalScopes() const;
175 /**
176 * Set the language specific auto completion delimeteres, for example: for C++ you should populate
177 * the array with { . , -> , :: }
178 * @param delimArr delimeter array
179 */
180 void SetAutoCompDeliemters(const std::vector<wxString>& delimArr);
181
182 /**
183 * @brief set the tags manager to be used by this language instance
184 * @param tm
185 */
186 void SetTagsManager(TagsManager* tm);
187
188 /**
189 * @brief return the available tags manager
190 * @return
191 */
192 TagsManager* GetTagsManager();
193
194 /**
195 * Parse comments from source file
196 * @param name file name
197 * @param comments [output] returned vector of comments
198 */
199 void ParseComments(const wxFileName& fileName, std::vector<CommentPtr>* comments);
200
201 //==========================================================
202 // New API based on the yacc grammar files
203 //==========================================================
204
205 /**
206 * Evaluate a C++ expression. for example, the following expression: '((Notebook*)book)->'
207 * will be processed into typeName=Notebook, and typeScope=<global> (assuming Notebook is not
208 * placed within any namespace)
209 * @param stmt c++ expression
210 * @param text text where this expression was found
211 * @param fn filename context
212 * @param lineno current line number
213 * @param indexer codelite_indexer helper
214 * @param typeName [output]
215 * @param typeScope [output]
216 * @param oper [output] return the operator used (::, ., ->)
217 * @param scopeTemplateInitList [output] return the scope tempalte intialization (e.g. "std::auto_ptr<wxString>
218 * str;" -> <wxString>
219 * @return true on success, false otherwise. The output fields are only to be checked with the return
220 * valus is 'true'
221 */
222 bool ProcessExpression(const wxString& expr, const wxString& text, const wxFileName& fn, int lineno,
223 wxString& typeName, wxString& typeScope, wxString& oper, wxString& scopeTemplateInitList);
224
225 /**
226 * return scope name from given input string
227 * @param in input string
228 * @return scope name or empty string
229 */
230 wxString GetScopeName(const wxString& in, std::vector<wxString>* additionlNS);
231
232 /**
233 * parse an expression and return the result. this functions uses
234 * the sqlite database as its symbol table
235 * @param in input string expression
236 * @return ExpressionResult, if it fails to parse it, check result.m_name.empty() for success
237 */
238 ExpressionResult ParseExpression(const wxString& in);
239
240 /**
241 * @brief process a token and resolve it into typeName / typeScope
242 * @param token
243 * @return
244 */
245 bool ProcessToken(TokenContainer* tokeContainer);
246
247 /**
248 * Collect local variables from given scope text (in) and an optional symbol name
249 * @param in scope to search for
250 * @param tags output, since we dont have full information about each token, all local variables returned are of
251 *type
252 * 'variable' with public access
253 * @param name optional name to look for (name can be partial).
254 */
255 void GetLocalVariables(const wxString& in, std::vector<TagEntryPtr>& tags, bool isFuncSignature,
256 const wxString& name = wxEmptyString, size_t flag = PartialMatch);
257
258 wxString ApplyCtagsReplacementTokens(const wxString& in);
259
260 bool VariableFromPattern(const wxString& pattern, const wxString& name, Variable& var);
261 bool FunctionFromPattern(TagEntryPtr tag, clFunction& foo);
262
263 /**
264 * @brief insert functionBody into clsname. This function will search for best location
265 * to place the function body. set visibility to 0 for 'pubilc' function, 1 for 'protected' and 2 for private
266 * return true if this function succeeded, false otherwise
267 */
268 bool InsertFunctionDecl(const wxString& clsname, const wxString& functionDecl, wxString& sourceContent,
269 int visibility = 0);
270
271 private:
272 bool DoSearchByNameAndScope(const wxString& name, const wxString& scopeName, std::vector<TagEntryPtr>& tags,
273 wxString& type, wxString& typeScope, bool testGlobalScope = true);
274
275 bool CorrectUsingNamespace(wxString& type, wxString& typeScope, const wxString& parentScope,
276 std::vector<TagEntryPtr>& tags);
277 /**
278 * Private constructor
279 */
280 Language();
281
282 /**
283 * Private destructor
284 */
285 virtual ~Language();
286
287 /**
288 * Return the next token and the delimiter found, the source string is taken from the
289 * m_tokenScanner member of this class
290 * @param token next token
291 * @param delim delimiter found
292 * @param subscriptOperator subscript operator was found
293 * @param funcArgList the first argument list passed (e.g. MyCall(a)->, funcArgList will contain (a))
294 * @return true if token was found false otherwise
295 */
296 bool NextToken(wxString& token, wxString& delim, bool& subscriptOperator, wxString& funcArgList);
297
298 /**
299 * @brief construct a list of tokens based on an expression and a give scope
300 * For example: parsing the expression: variable->GetName().AfterFirst()
301 * will yield a list of 3 tokens
302 * @param scopeName the current scope name of where the expression was found
303 * @return ParsedToken ptr (allocated on the heap. Must be freed by calling to ParsedToke::DeleteTokens( head ) or
304 * NULL incase
305 * parsing fails
306 */
307 ParsedToken* ParseTokens(const wxString& scopeName);
308
309 /**
310 * @brief Attempt to fix template results
311 * @param typeName the type name that was detected by the parser
312 * @param typeScope the type scope
313 */
314 bool OnTemplates(ParsedToken* token);
315
316 /**
317 * @brief attempt to expand 'typedef' to their actual value
318 */
319 bool OnTypedef(ParsedToken* token);
320 void DoSimpleTypedef(ParsedToken* token);
321
322 /**
323 * @brief expand reference operator (->) overloading
324 * @param typeName [input/output] the type name to check for the reference operator overloading
325 * @param typeScope [input/output] its scope
326 * @return true on success, false otherwise
327 */
328 bool OnArrowOperatorOverloading(ParsedToken* token);
329
330 /**
331 * @brief expand subscript operator
332 * @param typeName [input/output] the type name to check for the reference operator overloading
333 * @param typeScope [input/output] its scope
334 * @return true on success, false otherwise
335 */
336 bool OnSubscriptOperator(ParsedToken* token);
337
338 /**
339 * @brief run the user defined types as they appear in the 'Types' replacement table
340 * @param token current token
341 */
342 bool RunUserTypes(ParsedToken* token, const wxString& entryPath = wxEmptyString);
343
344 void ParseTemplateArgs(const wxString& argListStr, wxArrayString& argsList);
345 void ParseTemplateInitList(const wxString& argListStr, wxArrayString& argsList);
346 void DoRemoveTempalteInitialization(wxString& str, wxArrayString& tmplInitList);
347 void DoResolveTemplateInitializationList(wxArrayString& tmpInitList);
348 void DoFixFunctionUsingCtagsReturnValue(clFunction& foo, TagEntryPtr tag);
349 void DoReplaceTokens(wxString& inStr, const wxStringTable_t& ignoreTokens);
350 wxArrayString DoExtractTemplateDeclarationArgsFromScope();
351 wxArrayString DoExtractTemplateDeclarationArgs(ParsedToken* token);
352 wxArrayString DoExtractTemplateDeclarationArgs(TagEntryPtr tag);
353 void DoExtractTemplateInitListFromInheritance(TagEntryPtr tag, ParsedToken* token);
354 void DoExtractTemplateInitListFromInheritance(ParsedToken* token);
355 void DoExtractTemplateArgsFromSelf(ParsedToken* token);
356 void DoFixTokensFromVariable(TokenContainer* tokeContainer, const wxString& variableDecl);
357
358 // Wrapper methods to help the transition from the [wxString,wxString] API into the new ParsedToken
359 // code
360 bool DoIsTypeAndScopeExist(ParsedToken* token);
361 bool DoCorrectUsingNamespaces(ParsedToken* token, std::vector<TagEntryPtr>& tags);
362
363 /**
364 * @brief read the class name. This function assumes that the last token
365 * consumed by the scanner is 'class' (type: lexCLASS)
366 */
367 int DoReadClassName(CppScanner& scanner, wxString& clsname) const;
368 };
369
370 class WXDLLIMPEXP_CL LanguageST
371 {
372 public:
373 static void Free();
374 static Language* Get();
375 };
376
377 #endif // CODELITE_LANGUAGE_H