|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2010 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 Qt Mobility Components. |
|
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 "GenTemplateInstance.h" |
|
43 #include "Overview.h" |
|
44 |
|
45 #include <Control.h> |
|
46 #include <Scope.h> |
|
47 #include <Names.h> |
|
48 #include <Symbols.h> |
|
49 #include <CoreTypes.h> |
|
50 #include <Literals.h> |
|
51 |
|
52 #include <QtCore/QVarLengthArray> |
|
53 #include <QtCore/QDebug> |
|
54 |
|
55 using namespace CPlusPlus; |
|
56 |
|
57 namespace { |
|
58 |
|
59 class ApplySubstitution |
|
60 { |
|
61 public: |
|
62 ApplySubstitution(const LookupContext &context, Symbol *symbol, const GenTemplateInstance::Substitution &substitution); |
|
63 ~ApplySubstitution(); |
|
64 |
|
65 Control *control() const { return context.control(); } |
|
66 |
|
67 FullySpecifiedType apply(const Name *name); |
|
68 FullySpecifiedType apply(const FullySpecifiedType &type); |
|
69 |
|
70 int findSubstitution(const Identifier *id) const; |
|
71 FullySpecifiedType applySubstitution(int index) const; |
|
72 |
|
73 private: |
|
74 class ApplyToType: protected TypeVisitor |
|
75 { |
|
76 public: |
|
77 ApplyToType(ApplySubstitution *q) |
|
78 : q(q) {} |
|
79 |
|
80 FullySpecifiedType operator()(const FullySpecifiedType &ty) |
|
81 { |
|
82 FullySpecifiedType previousType = switchType(ty); |
|
83 accept(ty.type()); |
|
84 return switchType(previousType); |
|
85 } |
|
86 |
|
87 protected: |
|
88 using TypeVisitor::visit; |
|
89 |
|
90 Control *control() const |
|
91 { return q->control(); } |
|
92 |
|
93 FullySpecifiedType switchType(const FullySpecifiedType &type) |
|
94 { |
|
95 FullySpecifiedType previousType = _type; |
|
96 _type = type; |
|
97 return previousType; |
|
98 } |
|
99 |
|
100 virtual void visit(VoidType *) |
|
101 { |
|
102 // nothing to do |
|
103 } |
|
104 |
|
105 virtual void visit(IntegerType *) |
|
106 { |
|
107 // nothing to do |
|
108 } |
|
109 |
|
110 virtual void visit(FloatType *) |
|
111 { |
|
112 // nothing to do |
|
113 } |
|
114 |
|
115 virtual void visit(PointerToMemberType *) |
|
116 { |
|
117 qDebug() << Q_FUNC_INFO; // ### TODO |
|
118 } |
|
119 |
|
120 virtual void visit(PointerType *ptrTy) |
|
121 { |
|
122 _type.setType(control()->pointerType(q->apply(ptrTy->elementType()))); |
|
123 } |
|
124 |
|
125 virtual void visit(ReferenceType *refTy) |
|
126 { |
|
127 _type.setType(control()->referenceType(q->apply(refTy->elementType()))); |
|
128 } |
|
129 |
|
130 virtual void visit(ArrayType *arrayTy) |
|
131 { |
|
132 _type.setType(control()->arrayType(q->apply(arrayTy->elementType()), arrayTy->size())); |
|
133 } |
|
134 |
|
135 virtual void visit(NamedType *ty) |
|
136 { |
|
137 FullySpecifiedType n = q->apply(ty->name()); |
|
138 _type.setType(n.type()); |
|
139 } |
|
140 |
|
141 virtual void visit(Function *funTy) |
|
142 { |
|
143 Function *fun = control()->newFunction(/*sourceLocation=*/ 0, funTy->name()); |
|
144 fun->setScope(funTy->scope()); |
|
145 fun->setConst(funTy->isConst()); |
|
146 fun->setVolatile(funTy->isVolatile()); |
|
147 fun->setVirtual(funTy->isVirtual()); |
|
148 fun->setAmbiguous(funTy->isAmbiguous()); |
|
149 fun->setVariadic(funTy->isVariadic()); |
|
150 |
|
151 fun->setReturnType(q->apply(funTy->returnType())); |
|
152 |
|
153 for (unsigned i = 0; i < funTy->argumentCount(); ++i) { |
|
154 Argument *originalArgument = funTy->argumentAt(i)->asArgument(); |
|
155 Argument *arg = control()->newArgument(/*sourceLocation*/ 0, |
|
156 originalArgument->name()); |
|
157 |
|
158 arg->setType(q->apply(originalArgument->type())); |
|
159 arg->setInitializer(originalArgument->initializer()); |
|
160 fun->arguments()->enterSymbol(arg); |
|
161 } |
|
162 |
|
163 _type.setType(fun); |
|
164 } |
|
165 |
|
166 virtual void visit(Namespace *) |
|
167 { |
|
168 qDebug() << Q_FUNC_INFO; |
|
169 } |
|
170 |
|
171 virtual void visit(Class *) |
|
172 { |
|
173 qDebug() << Q_FUNC_INFO; |
|
174 } |
|
175 |
|
176 virtual void visit(Enum *) |
|
177 { |
|
178 qDebug() << Q_FUNC_INFO; |
|
179 } |
|
180 |
|
181 virtual void visit(ForwardClassDeclaration *) |
|
182 { |
|
183 qDebug() << Q_FUNC_INFO; |
|
184 } |
|
185 |
|
186 virtual void visit(ObjCClass *) |
|
187 { |
|
188 qDebug() << Q_FUNC_INFO; |
|
189 } |
|
190 |
|
191 virtual void visit(ObjCProtocol *) |
|
192 { |
|
193 qDebug() << Q_FUNC_INFO; |
|
194 } |
|
195 |
|
196 virtual void visit(ObjCMethod *) |
|
197 { |
|
198 qDebug() << Q_FUNC_INFO; |
|
199 } |
|
200 |
|
201 virtual void visit(ObjCForwardClassDeclaration *) |
|
202 { |
|
203 qDebug() << Q_FUNC_INFO; |
|
204 } |
|
205 |
|
206 virtual void visit(ObjCForwardProtocolDeclaration *) |
|
207 { |
|
208 qDebug() << Q_FUNC_INFO; |
|
209 } |
|
210 |
|
211 private: |
|
212 ApplySubstitution *q; |
|
213 FullySpecifiedType _type; |
|
214 QHash<Symbol *, FullySpecifiedType> _processed; |
|
215 }; |
|
216 |
|
217 class ApplyToName: protected NameVisitor |
|
218 { |
|
219 public: |
|
220 ApplyToName(ApplySubstitution *q): q(q) {} |
|
221 |
|
222 FullySpecifiedType operator()(const Name *name) |
|
223 { |
|
224 FullySpecifiedType previousType = switchType(FullySpecifiedType()); |
|
225 accept(name); |
|
226 return switchType(previousType); |
|
227 } |
|
228 |
|
229 protected: |
|
230 Control *control() const |
|
231 { return q->control(); } |
|
232 |
|
233 int findSubstitution(const Identifier *id) const |
|
234 { return q->findSubstitution(id); } |
|
235 |
|
236 FullySpecifiedType applySubstitution(int index) const |
|
237 { return q->applySubstitution(index); } |
|
238 |
|
239 FullySpecifiedType switchType(const FullySpecifiedType &type) |
|
240 { |
|
241 FullySpecifiedType previousType = _type; |
|
242 _type = type; |
|
243 return previousType; |
|
244 } |
|
245 |
|
246 virtual void visit(const NameId *name) |
|
247 { |
|
248 int index = findSubstitution(name->identifier()); |
|
249 |
|
250 if (index != -1) |
|
251 _type = applySubstitution(index); |
|
252 |
|
253 else |
|
254 _type = control()->namedType(name); |
|
255 } |
|
256 |
|
257 virtual void visit(const TemplateNameId *name) |
|
258 { |
|
259 QVarLengthArray<FullySpecifiedType, 8> arguments(name->templateArgumentCount()); |
|
260 for (unsigned i = 0; i < name->templateArgumentCount(); ++i) { |
|
261 FullySpecifiedType argTy = name->templateArgumentAt(i); |
|
262 arguments[i] = q->apply(argTy); |
|
263 } |
|
264 |
|
265 const TemplateNameId *templId = control()->templateNameId(name->identifier(), |
|
266 arguments.data(), |
|
267 arguments.size()); |
|
268 _type = control()->namedType(templId); |
|
269 } |
|
270 |
|
271 virtual void visit(const QualifiedNameId *name) |
|
272 { |
|
273 QVarLengthArray<const Name *, 8> names(name->nameCount()); |
|
274 for (unsigned i = 0; i < name->nameCount(); ++i) { |
|
275 const Name *n = name->nameAt(i); |
|
276 |
|
277 if (const TemplateNameId *templId = n->asTemplateNameId()) { |
|
278 QVarLengthArray<FullySpecifiedType, 8> arguments(templId->templateArgumentCount()); |
|
279 for (unsigned templateArgIndex = 0; templateArgIndex < templId->templateArgumentCount(); ++templateArgIndex) { |
|
280 FullySpecifiedType argTy = templId->templateArgumentAt(templateArgIndex); |
|
281 arguments[templateArgIndex] = q->apply(argTy); |
|
282 } |
|
283 |
|
284 n = control()->templateNameId(templId->identifier(), arguments.data(), arguments.size()); |
|
285 } |
|
286 |
|
287 names[i] = n; |
|
288 } |
|
289 |
|
290 const QualifiedNameId *q = control()->qualifiedNameId(names.data(), names.size(), name->isGlobal()); |
|
291 _type = control()->namedType(q); |
|
292 } |
|
293 |
|
294 virtual void visit(const DestructorNameId *name) |
|
295 { |
|
296 Overview oo; |
|
297 qWarning() << "ignored name:" << oo(name); |
|
298 } |
|
299 |
|
300 virtual void visit(const OperatorNameId *name) |
|
301 { |
|
302 Overview oo; |
|
303 qWarning() << "ignored name:" << oo(name); |
|
304 } |
|
305 |
|
306 virtual void visit(const ConversionNameId *name) |
|
307 { |
|
308 Overview oo; |
|
309 qWarning() << "ignored name:" << oo(name); |
|
310 } |
|
311 |
|
312 virtual void visit(const SelectorNameId *name) |
|
313 { |
|
314 Overview oo; |
|
315 qWarning() << "ignored name:" << oo(name); |
|
316 } |
|
317 |
|
318 private: |
|
319 ApplySubstitution *q; |
|
320 FullySpecifiedType _type; |
|
321 }; |
|
322 |
|
323 public: // attributes |
|
324 LookupContext context; |
|
325 Symbol *symbol; |
|
326 GenTemplateInstance::Substitution substitution; |
|
327 ApplyToType applyToType; |
|
328 ApplyToName applyToName; |
|
329 }; |
|
330 |
|
331 ApplySubstitution::ApplySubstitution(const LookupContext &context, Symbol *symbol, |
|
332 const GenTemplateInstance::Substitution &substitution) |
|
333 : context(context), symbol(symbol), |
|
334 substitution(substitution), |
|
335 applyToType(this), applyToName(this) |
|
336 { } |
|
337 |
|
338 ApplySubstitution::~ApplySubstitution() |
|
339 { |
|
340 } |
|
341 |
|
342 FullySpecifiedType ApplySubstitution::apply(const Name *name) |
|
343 { |
|
344 FullySpecifiedType ty = applyToName(name); |
|
345 return ty; |
|
346 } |
|
347 |
|
348 FullySpecifiedType ApplySubstitution::apply(const FullySpecifiedType &type) |
|
349 { |
|
350 FullySpecifiedType ty = applyToType(type); |
|
351 return ty; |
|
352 } |
|
353 |
|
354 int ApplySubstitution::findSubstitution(const Identifier *id) const |
|
355 { |
|
356 Q_ASSERT(id != 0); |
|
357 |
|
358 for (int index = 0; index < substitution.size(); ++index) { |
|
359 QPair<const Identifier *, FullySpecifiedType> s = substitution.at(index); |
|
360 |
|
361 if (id->isEqualTo(s.first)) |
|
362 return index; |
|
363 } |
|
364 |
|
365 return -1; |
|
366 } |
|
367 |
|
368 FullySpecifiedType ApplySubstitution::applySubstitution(int index) const |
|
369 { |
|
370 Q_ASSERT(index != -1); |
|
371 Q_ASSERT(index < substitution.size()); |
|
372 |
|
373 return substitution.at(index).second; |
|
374 } |
|
375 |
|
376 } // end of anonymous namespace |
|
377 |
|
378 GenTemplateInstance::GenTemplateInstance(const LookupContext &context, const Substitution &substitution) |
|
379 : _symbol(0), |
|
380 _context(context), |
|
381 _substitution(substitution) |
|
382 { } |
|
383 |
|
384 FullySpecifiedType GenTemplateInstance::operator()(Symbol *symbol) |
|
385 { |
|
386 ApplySubstitution o(_context, symbol, _substitution); |
|
387 return o.apply(symbol->type()); |
|
388 } |
|
389 |
|
390 Control *GenTemplateInstance::control() const |
|
391 { return _context.control(); } |