|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the qt3to4 porting application of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #include "codemodelattributes.h" |
|
43 #include "tokenengine.h" |
|
44 |
|
45 QT_BEGIN_NAMESPACE |
|
46 |
|
47 using namespace CodeModel; |
|
48 using namespace TokenEngine; |
|
49 |
|
50 /* |
|
51 Walk the codemodel. |
|
52 */ |
|
53 void CodeModelAttributes::createAttributes(TranslationUnit translationUnit) |
|
54 { |
|
55 m_translationUnit = translationUnit; |
|
56 parseScope(const_cast<CodeModel::NamespaceScope *>(translationUnit.codeModel())); |
|
57 } |
|
58 |
|
59 /* |
|
60 Create attributes for each name use and assign to the token. |
|
61 */ |
|
62 void CodeModelAttributes::parseNameUse(CodeModel::NameUse *nameUse) |
|
63 { |
|
64 // Get the container for this token. |
|
65 TokenRef ref = nameUse->nameToken(); |
|
66 const int containerIndex = ref.containerIndex(); |
|
67 TokenAttributes *attributes = ref.tokenContainer().tokenAttributes(); |
|
68 |
|
69 if (!areAttributesEnabled(attributes)) |
|
70 return; |
|
71 |
|
72 // Test if the nameUse refers to a UnknownType. If so we add and |
|
73 // "unknown" attribute. |
|
74 if (TypeMember *typeMember = nameUse->declaration()->toTypeMember()) { |
|
75 if (typeMember->type()->toUnknownType()) { |
|
76 attributes->addAttribute(containerIndex, "unknown", nameUse->name()); |
|
77 return; |
|
78 } |
|
79 } |
|
80 |
|
81 // Add attributes this namnUse. |
|
82 attributes->addAttribute(containerIndex, "nameUse", nameUse->name()); |
|
83 attributes->addAttribute(containerIndex, "parentScope", |
|
84 nameUse->declaration()->parent()->name() ); |
|
85 if (CodeModel::Scope * skop = nameUse->declaration()->parent()->parent()) { |
|
86 attributes->addAttribute(containerIndex, "grandParentScope", skop->name()); |
|
87 } |
|
88 |
|
89 createNameTypeAttribute(nameUse); |
|
90 } |
|
91 |
|
92 /* |
|
93 Create attributes for members and assign to token. |
|
94 */ |
|
95 void CodeModelAttributes::parseMember(CodeModel::Member *member) |
|
96 { |
|
97 if(!member || member->name() == QByteArray()) |
|
98 return; |
|
99 |
|
100 //get the container for this token |
|
101 TokenRef ref = member->nameToken(); |
|
102 const int containerIndex = ref.containerIndex(); |
|
103 TokenAttributes *attributes = ref.tokenContainer().tokenAttributes(); |
|
104 |
|
105 if (areAttributesEnabled(attributes)) { |
|
106 //add attributes for this declaration |
|
107 static const QByteArray textDeclaration = "declaration"; |
|
108 attributes->addAttribute(containerIndex, textDeclaration, member->name()); |
|
109 createNameTypeAttribute(member); |
|
110 } |
|
111 CodeModelWalker::parseMember(member); |
|
112 } |
|
113 |
|
114 void CodeModelAttributes::parseFunctionMember(CodeModel::FunctionMember *member) |
|
115 { |
|
116 CodeModel::ArgumentCollection arguments = member->arguments(); |
|
117 CodeModel::ArgumentCollection::ConstIterator it = arguments.constBegin(); |
|
118 TokenRef ref = member->nameToken(); |
|
119 TokenAttributes *attributes = ref.tokenContainer().tokenAttributes(); |
|
120 |
|
121 if (areAttributesEnabled(attributes)) { |
|
122 while (it != arguments.constEnd()) { |
|
123 const int containerIndex = (*it)->nameToken().containerIndex(); |
|
124 const QByteArray name = (*it)->name(); |
|
125 attributes->addAttribute(containerIndex, "declaration", name); |
|
126 attributes->addAttribute(containerIndex, "nameType", "variable"); |
|
127 ++it; |
|
128 } |
|
129 } |
|
130 CodeModelWalker::parseFunctionMember(member); |
|
131 } |
|
132 |
|
133 /* |
|
134 NameType attributes gives information on what kind of member this is. |
|
135 */ |
|
136 void CodeModelAttributes::createNameTypeAttribute(CodeModel::Member *member) |
|
137 { |
|
138 if(!member) |
|
139 return; |
|
140 //get the container for the token accosiated with this member. |
|
141 TokenRef ref = member->nameToken(); |
|
142 const int containerIndex = ref.containerIndex(); |
|
143 TokenAttributes *attributes = ref.tokenContainer().tokenAttributes(); |
|
144 |
|
145 createNameTypeAttributeAtIndex(attributes, containerIndex, member); |
|
146 } |
|
147 |
|
148 /* |
|
149 A NameUse has the same NameType as the declaration it is referring to. |
|
150 */ |
|
151 void CodeModelAttributes::createNameTypeAttribute(CodeModel::NameUse *nameUse) |
|
152 { |
|
153 if(!nameUse) |
|
154 return; |
|
155 |
|
156 //get the container for the token accosiated with this NameUse. |
|
157 TokenRef ref = nameUse->nameToken(); |
|
158 const int containerIndex = ref.containerIndex(); |
|
159 TokenAttributes *attributes = ref.tokenContainer().tokenAttributes(); |
|
160 |
|
161 createNameTypeAttributeAtIndex(attributes, containerIndex, nameUse->declaration()); |
|
162 } |
|
163 |
|
164 void CodeModelAttributes::createNameTypeAttributeAtIndex(TokenEngine::TokenAttributes *attributes, |
|
165 int index, CodeModel::Member *member) |
|
166 { |
|
167 QByteArray nameType = "unknown"; |
|
168 if (member->toFunctionMember()) { |
|
169 nameType = "function"; |
|
170 } else if (CodeModel::VariableMember *variableMember = member->toVariableMember()) { |
|
171 if (variableMember->type()->toEnumType()) |
|
172 nameType = "enumerator"; |
|
173 else |
|
174 nameType = "variable"; |
|
175 } else if (CodeModel::TypeMember *typeMember = member->toTypeMember()) { |
|
176 if (CodeModel::Type *type = typeMember->type()) { |
|
177 if (type->toClassType()) { |
|
178 nameType = "class"; |
|
179 } else if (type->toEnumType()) { |
|
180 nameType = "enum"; |
|
181 } |
|
182 } |
|
183 } |
|
184 attributes->addAttribute(index, "nameType", nameType); |
|
185 } |
|
186 |
|
187 bool CodeModelAttributes::areAttributesEnabled(const TokenAttributes *attributes) const |
|
188 { |
|
189 static const QByteArray tstCreateAttributes("CreateAttributes"); |
|
190 static const QByteArray tstTrue("True"); |
|
191 return (attributes->attribute(tstCreateAttributes) == tstTrue); |
|
192 } |
|
193 |
|
194 |
|
195 QT_END_NAMESPACE |